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
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 2, or (at your option)
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, 59 Temple Place - Suite 330, Boston, MA
23 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
27 #include "safe-ctype.h"
28 #include "obstack.h" /* For "symbols.h" */
31 #include "struc-symbol.h"
33 /* This is non-zero if symbols are case sensitive, which is the
35 int symbols_case_sensitive = 1;
37 #ifndef WORKING_DOT_WORD
38 extern int new_broken_words;
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;
53 #define debug_verify_symchain verify_symbol_chain
55 #define debug_verify_symchain(root, last) ((void) 0)
58 #define DOLLAR_LABEL_CHAR '\001'
59 #define LOCAL_LABEL_CHAR '\002'
63 /* The name of an external symbol which is
64 used to make weak PE symbol names unique. */
65 const char * an_external_name;
68 static char *save_symbol_name (const char *);
69 static void fb_label_init (void);
70 static long dollar_label_instance (long);
71 static long fb_label_instance (long);
73 static void print_binary (FILE *, const char *, expressionS *);
74 static void report_op_error (symbolS *, symbolS *, symbolS *);
76 /* Return a pointer to a new symbol. Die if we can't make a new
77 symbol. Fill in the symbol's values. Add symbol to end of symbol
80 This function should be called in the general case of creating a
81 symbol. However, if the output file symbol table has already been
82 set, and you are certain that this symbol won't be wanted in the
83 output file, you can call symbol_create. */
86 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
88 symbolS *symbolP = symbol_create (name, segment, valu, frag);
90 /* Link to end of symbol chain. */
93 extern int symbol_table_frozen;
94 if (symbol_table_frozen)
98 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
103 /* Save a symbol name on a permanent obstack, and convert it according
104 to the object file format. */
107 save_symbol_name (const char *name)
109 unsigned int name_length;
112 name_length = strlen (name) + 1; /* +1 for \0. */
113 obstack_grow (¬es, name, name_length);
114 ret = obstack_finish (¬es);
116 #ifdef STRIP_UNDERSCORE
121 #ifdef tc_canonicalize_symbol_name
122 ret = tc_canonicalize_symbol_name (ret);
125 if (! symbols_case_sensitive)
129 for (s = ret; *s != '\0'; s++)
137 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
138 segT segment, /* Segment identifier (SEG_<something>). */
139 valueT valu, /* Symbol value. */
140 fragS *frag /* Associated fragment. */)
142 char *preserved_copy_of_name;
145 preserved_copy_of_name = save_symbol_name (name);
147 symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
149 /* symbol must be born in some fixed state. This seems as good as any. */
150 memset (symbolP, 0, sizeof (symbolS));
153 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
154 if (symbolP->bsym == NULL)
155 as_perror ("%s", "bfd_make_empty_symbol");
156 symbolP->bsym->udata.p = (PTR) symbolP;
158 S_SET_NAME (symbolP, preserved_copy_of_name);
160 S_SET_SEGMENT (symbolP, segment);
161 S_SET_VALUE (symbolP, valu);
162 symbol_clear_list_pointers (symbolP);
164 symbolP->sy_frag = frag;
165 #ifndef BFD_ASSEMBLER
166 symbolP->sy_number = ~0;
167 symbolP->sy_name_offset = (unsigned int) ~0;
170 obj_symbol_new_hook (symbolP);
172 #ifdef tc_symbol_new_hook
173 tc_symbol_new_hook (symbolP);
181 /* Local symbol support. If we can get away with it, we keep only a
182 small amount of information for local symbols. */
184 static symbolS *local_symbol_convert (struct local_symbol *);
186 /* Used for statistics. */
188 static unsigned long local_symbol_count;
189 static unsigned long local_symbol_conversion_count;
191 /* This macro is called with a symbol argument passed by reference.
192 It returns whether this is a local symbol. If necessary, it
193 changes its argument to the real symbol. */
195 #define LOCAL_SYMBOL_CHECK(s) \
197 ? (local_symbol_converted_p ((struct local_symbol *) s) \
198 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
203 /* Create a local symbol and insert it into the local hash table. */
205 struct local_symbol *
206 local_symbol_make (const char *name, segT section, valueT value, fragS *frag)
209 struct local_symbol *ret;
211 ++local_symbol_count;
213 name_copy = save_symbol_name (name);
215 ret = (struct local_symbol *) obstack_alloc (¬es, sizeof *ret);
216 ret->lsy_marker = NULL;
217 ret->lsy_name = name_copy;
218 ret->lsy_section = section;
219 local_symbol_set_frag (ret, frag);
220 ret->lsy_value = value;
222 hash_jam (local_hash, name_copy, (PTR) ret);
227 /* Convert a local symbol into a real symbol. Note that we do not
228 reclaim the space used by the local symbol. */
231 local_symbol_convert (struct local_symbol *locsym)
235 assert (locsym->lsy_marker == NULL);
236 if (local_symbol_converted_p (locsym))
237 return local_symbol_get_real_symbol (locsym);
239 ++local_symbol_conversion_count;
241 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
242 local_symbol_get_frag (locsym));
244 if (local_symbol_resolved_p (locsym))
245 ret->sy_resolved = 1;
247 /* Local symbols are always either defined or used. */
250 #ifdef TC_LOCAL_SYMFIELD_CONVERT
251 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
254 symbol_table_insert (ret);
256 local_symbol_mark_converted (locsym);
257 local_symbol_set_real_symbol (locsym, ret);
259 hash_jam (local_hash, locsym->lsy_name, NULL);
264 #else /* ! BFD_ASSEMBLER */
266 #define LOCAL_SYMBOL_CHECK(s) 0
267 #define local_symbol_convert(s) ((symbolS *) s)
269 #endif /* ! BFD_ASSEMBLER */
271 /* We have just seen "<name>:".
272 Creates a struct symbol unless it already exists.
274 Gripes if we are redefining a symbol incompatibly (and ignores it). */
277 colon (/* Just seen "x:" - rattle symbols & frags. */
278 const char *sym_name /* Symbol name, as a cannonical string. */
279 /* We copy this string: OK to alter later. */)
281 register symbolS *symbolP; /* Symbol we are working with. */
283 /* Sun local labels go out of scope whenever a non-local symbol is
285 if (LOCAL_LABELS_DOLLAR)
290 local = bfd_is_local_label_name (stdoutput, sym_name);
292 local = LOCAL_LABEL (sym_name);
296 dollar_label_clear ();
299 #ifndef WORKING_DOT_WORD
300 if (new_broken_words)
302 struct broken_word *a;
307 if (now_seg == absolute_section)
309 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
313 possible_bytes = (md_short_jump_size
314 + new_broken_words * md_long_jump_size);
317 frag_opcode = frag_var (rs_broken_word,
321 (symbolS *) broken_words,
325 /* We want to store the pointer to where to insert the jump
326 table in the fr_opcode of the rs_broken_word frag. This
327 requires a little hackery. */
329 && (frag_tmp->fr_type != rs_broken_word
330 || frag_tmp->fr_opcode))
331 frag_tmp = frag_tmp->fr_next;
333 frag_tmp->fr_opcode = frag_opcode;
334 new_broken_words = 0;
336 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
337 a->dispfrag = frag_tmp;
339 #endif /* WORKING_DOT_WORD */
341 if ((symbolP = symbol_find (sym_name)) != 0)
343 #ifdef RESOLVE_SYMBOL_REDEFINITION
344 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
347 /* Now check for undefined symbols. */
348 if (LOCAL_SYMBOL_CHECK (symbolP))
351 struct local_symbol *locsym = (struct local_symbol *) symbolP;
353 if (locsym->lsy_section != undefined_section
354 && (local_symbol_get_frag (locsym) != frag_now
355 || locsym->lsy_section != now_seg
356 || locsym->lsy_value != frag_now_fix ()))
358 as_bad (_("symbol `%s' is already defined"), sym_name);
362 locsym->lsy_section = now_seg;
363 local_symbol_set_frag (locsym, frag_now);
364 locsym->lsy_value = frag_now_fix ();
367 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
369 if (S_GET_VALUE (symbolP) == 0)
371 symbolP->sy_frag = frag_now;
373 S_SET_OTHER (symbolP, const_flag);
375 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
376 S_SET_SEGMENT (symbolP, now_seg);
379 #endif /* if we have one, it better be zero. */
384 /* There are still several cases to check:
386 A .comm/.lcomm symbol being redefined as initialized
389 A .comm/.lcomm symbol being redefined with a larger
392 This only used to be allowed on VMS gas, but Sun cc
393 on the sparc also depends on it. */
395 if (((!S_IS_DEBUG (symbolP)
396 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
397 && S_IS_EXTERNAL (symbolP))
398 || S_GET_SEGMENT (symbolP) == bss_section)
399 && (now_seg == data_section
400 || now_seg == S_GET_SEGMENT (symbolP)))
402 /* Select which of the 2 cases this is. */
403 if (now_seg != data_section)
405 /* New .comm for prev .comm symbol.
407 If the new size is larger we just change its
408 value. If the new size is smaller, we ignore
410 if (S_GET_VALUE (symbolP)
411 < ((unsigned) frag_now_fix ()))
413 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
418 /* It is a .comm/.lcomm being converted to initialized
420 symbolP->sy_frag = frag_now;
422 S_SET_OTHER (symbolP, const_flag);
424 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
425 S_SET_SEGMENT (symbolP, now_seg); /* Keep N_EXT bit. */
430 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
431 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
432 static const char *od_buf = "";
437 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
439 sprintf (od_buf, "%d.%d.",
440 S_GET_OTHER (symbolP),
441 S_GET_DESC (symbolP));
443 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
445 segment_name (S_GET_SEGMENT (symbolP)),
447 (long) S_GET_VALUE (symbolP));
449 } /* if the undefined symbol has no value */
453 /* Don't blow up if the definition is the same. */
454 if (!(frag_now == symbolP->sy_frag
455 && S_GET_VALUE (symbolP) == frag_now_fix ()
456 && S_GET_SEGMENT (symbolP) == now_seg))
457 as_bad (_("symbol `%s' is already defined"), sym_name);
462 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
464 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
465 (valueT) frag_now_fix (),
468 #endif /* BFD_ASSEMBLER */
471 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
474 S_SET_OTHER (symbolP, const_flag);
477 symbol_table_insert (symbolP);
480 if (mri_common_symbol != NULL)
482 /* This symbol is actually being defined within an MRI common
483 section. This requires special handling. */
484 if (LOCAL_SYMBOL_CHECK (symbolP))
485 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
486 symbolP->sy_value.X_op = O_symbol;
487 symbolP->sy_value.X_add_symbol = mri_common_symbol;
488 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
489 symbolP->sy_frag = &zero_address_frag;
490 S_SET_SEGMENT (symbolP, expr_section);
491 symbolP->sy_mri_common = 1;
495 tc_frob_label (symbolP);
497 #ifdef obj_frob_label
498 obj_frob_label (symbolP);
504 /* Die if we can't insert the symbol. */
507 symbol_table_insert (symbolS *symbolP)
509 register const char *error_string;
512 know (S_GET_NAME (symbolP));
514 if (LOCAL_SYMBOL_CHECK (symbolP))
516 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
518 if (error_string != NULL)
519 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
520 S_GET_NAME (symbolP), error_string);
524 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
526 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
527 S_GET_NAME (symbolP), error_string);
531 /* If a symbol name does not exist, create it as undefined, and insert
532 it into the symbol table. Return a pointer to it. */
535 symbol_find_or_make (const char *name)
537 register symbolS *symbolP;
539 symbolP = symbol_find (name);
544 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
546 symbolP = md_undefined_symbol ((char *) name);
550 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
557 symbolP = symbol_make (name);
559 symbol_table_insert (symbolP);
560 } /* if symbol wasn't found */
566 symbol_make (const char *name)
570 /* Let the machine description default it, e.g. for register names. */
571 symbolP = md_undefined_symbol ((char *) name);
574 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
580 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
582 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
586 symbol_temp_new_now (void)
588 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
592 symbol_temp_make (void)
594 return symbol_make (FAKE_LABEL_NAME);
597 /* Implement symbol table lookup.
598 In: A symbol's name as a string: '\0' can't be part of a symbol name.
599 Out: NULL if the name was not in the symbol table, else the address
600 of a struct symbol associated with that name. */
603 symbol_find (const char *name)
605 #ifdef STRIP_UNDERSCORE
606 return (symbol_find_base (name, 1));
607 #else /* STRIP_UNDERSCORE */
608 return (symbol_find_base (name, 0));
609 #endif /* STRIP_UNDERSCORE */
613 symbol_find_exact (const char *name)
617 struct local_symbol *locsym;
619 locsym = (struct local_symbol *) hash_find (local_hash, name);
621 return (symbolS *) locsym;
625 return ((symbolS *) hash_find (sy_hash, name));
629 symbol_find_base (const char *name, int strip_underscore)
631 if (strip_underscore && *name == '_')
634 #ifdef tc_canonicalize_symbol_name
637 size_t len = strlen (name) + 1;
639 copy = (char *) alloca (len);
640 memcpy (copy, name, len);
641 name = tc_canonicalize_symbol_name (copy);
645 if (! symbols_case_sensitive)
652 name = copy = (char *) alloca (strlen (name) + 1);
654 while ((c = *orig++) != '\0')
656 *copy++ = TOUPPER (c);
661 return symbol_find_exact (name);
664 /* Once upon a time, symbols were kept in a singly linked list. At
665 least coff needs to be able to rearrange them from time to time, for
666 which a doubly linked list is much more convenient. Loic did these
667 as macros which seemed dangerous to me so they're now functions.
670 /* Link symbol ADDME after symbol TARGET in the chain. */
673 symbol_append (symbolS *addme, symbolS *target,
674 symbolS **rootPP, symbolS **lastPP)
676 if (LOCAL_SYMBOL_CHECK (addme))
678 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
683 know (*rootPP == NULL);
684 know (*lastPP == NULL);
685 addme->sy_next = NULL;
686 #ifdef SYMBOLS_NEED_BACKPOINTERS
687 addme->sy_previous = NULL;
692 } /* if the list is empty */
694 if (target->sy_next != NULL)
696 #ifdef SYMBOLS_NEED_BACKPOINTERS
697 target->sy_next->sy_previous = addme;
698 #endif /* SYMBOLS_NEED_BACKPOINTERS */
702 know (*lastPP == target);
704 } /* if we have a next */
706 addme->sy_next = target->sy_next;
707 target->sy_next = addme;
709 #ifdef SYMBOLS_NEED_BACKPOINTERS
710 addme->sy_previous = target;
711 #endif /* SYMBOLS_NEED_BACKPOINTERS */
713 debug_verify_symchain (symbol_rootP, symbol_lastP);
716 /* Set the chain pointers of SYMBOL to null. */
719 symbol_clear_list_pointers (symbolS *symbolP)
721 if (LOCAL_SYMBOL_CHECK (symbolP))
723 symbolP->sy_next = NULL;
724 #ifdef SYMBOLS_NEED_BACKPOINTERS
725 symbolP->sy_previous = NULL;
729 #ifdef SYMBOLS_NEED_BACKPOINTERS
730 /* Remove SYMBOLP from the list. */
733 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
735 if (LOCAL_SYMBOL_CHECK (symbolP))
738 if (symbolP == *rootPP)
740 *rootPP = symbolP->sy_next;
741 } /* if it was the root */
743 if (symbolP == *lastPP)
745 *lastPP = symbolP->sy_previous;
746 } /* if it was the tail */
748 if (symbolP->sy_next != NULL)
750 symbolP->sy_next->sy_previous = symbolP->sy_previous;
753 if (symbolP->sy_previous != NULL)
755 symbolP->sy_previous->sy_next = symbolP->sy_next;
758 debug_verify_symchain (*rootPP, *lastPP);
761 /* Link symbol ADDME before symbol TARGET in the chain. */
764 symbol_insert (symbolS *addme, symbolS *target,
765 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
767 if (LOCAL_SYMBOL_CHECK (addme))
769 if (LOCAL_SYMBOL_CHECK (target))
772 if (target->sy_previous != NULL)
774 target->sy_previous->sy_next = addme;
778 know (*rootPP == target);
782 addme->sy_previous = target->sy_previous;
783 target->sy_previous = addme;
784 addme->sy_next = target;
786 debug_verify_symchain (*rootPP, *lastPP);
789 #endif /* SYMBOLS_NEED_BACKPOINTERS */
792 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
794 symbolS *symbolP = rootP;
799 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
802 assert (symbolP->bsym != NULL);
804 #ifdef SYMBOLS_NEED_BACKPOINTERS
805 assert (symbolP->sy_next->sy_previous == symbolP);
807 /* Walk the list anyways, to make sure pointers are still good. */
809 #endif /* SYMBOLS_NEED_BACKPOINTERS */
812 assert (lastP == symbolP);
816 verify_symbol_chain_2 (symbolS *sym)
818 symbolS *p = sym, *n = sym;
819 #ifdef SYMBOLS_NEED_BACKPOINTERS
820 while (symbol_previous (p))
821 p = symbol_previous (p);
823 while (symbol_next (n))
825 verify_symbol_chain (p, n);
829 report_op_error (symbolS *symp, symbolS *left, symbolS *right)
833 segT seg_left = S_GET_SEGMENT (left);
834 segT seg_right = right ? S_GET_SEGMENT (right) : 0;
836 if (expr_symbol_where (symp, &file, &line))
838 if (seg_left == undefined_section)
839 as_bad_where (file, line,
840 _("undefined symbol `%s' in operation"),
842 if (seg_right == undefined_section)
843 as_bad_where (file, line,
844 _("undefined symbol `%s' in operation"),
846 if (seg_left != undefined_section
847 && seg_right != undefined_section)
850 as_bad_where (file, line,
851 _("invalid sections for operation on `%s' and `%s'"),
852 S_GET_NAME (left), S_GET_NAME (right));
854 as_bad_where (file, line,
855 _("invalid section for operation on `%s'"),
862 if (seg_left == undefined_section)
863 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
864 S_GET_NAME (left), S_GET_NAME (symp));
865 if (seg_right == undefined_section)
866 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
867 S_GET_NAME (right), S_GET_NAME (symp));
868 if (seg_left != undefined_section
869 && seg_right != undefined_section)
872 as_bad_where (file, line,
873 _("invalid sections for operation on `%s' and `%s' setting `%s'"),
874 S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
876 as_bad_where (file, line,
877 _("invalid section for operation on `%s' setting `%s'"),
878 S_GET_NAME (left), S_GET_NAME (symp));
883 /* Resolve the value of a symbol. This is called during the final
884 pass over the symbol table to resolve any symbols with complex
888 resolve_symbol_value (symbolS *symp)
891 valueT final_val = 0;
895 if (LOCAL_SYMBOL_CHECK (symp))
897 struct local_symbol *locsym = (struct local_symbol *) symp;
899 final_val = locsym->lsy_value;
900 if (local_symbol_resolved_p (locsym))
903 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
907 locsym->lsy_value = final_val;
908 local_symbol_mark_resolved (locsym);
915 if (symp->sy_resolved)
917 if (symp->sy_value.X_op == O_constant)
918 return (valueT) symp->sy_value.X_add_number;
924 final_seg = S_GET_SEGMENT (symp);
926 if (symp->sy_resolving)
929 as_bad (_("symbol definition loop encountered at `%s'"),
936 symbolS *add_symbol, *op_symbol;
938 segT seg_left, seg_right;
941 symp->sy_resolving = 1;
943 /* Help out with CSE. */
944 add_symbol = symp->sy_value.X_add_symbol;
945 op_symbol = symp->sy_value.X_op_symbol;
946 final_val = symp->sy_value.X_add_number;
947 op = symp->sy_value.X_op;
960 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
961 if (final_seg == expr_section)
962 final_seg = absolute_section;
968 left = resolve_symbol_value (add_symbol);
969 seg_left = S_GET_SEGMENT (add_symbol);
971 symp->sy_value.X_op_symbol = NULL;
974 if (symp->sy_mri_common)
976 /* This is a symbol inside an MRI common section. The
977 relocation routines are going to handle it specially.
978 Don't change the value. */
979 resolved = symbol_resolved_p (add_symbol);
983 if (finalize_syms && final_val == 0)
985 if (LOCAL_SYMBOL_CHECK (add_symbol))
986 add_symbol = local_symbol_convert ((struct local_symbol *)
988 copy_symbol_attributes (symp, add_symbol);
991 /* If we have equated this symbol to an undefined or common
992 symbol, keep X_op set to O_symbol, and don't change
993 X_add_number. This permits the routine which writes out
994 relocation to detect this case, and convert the
995 relocation to be against the symbol to which this symbol
997 if (! S_IS_DEFINED (add_symbol)
998 #if defined (OBJ_COFF) && defined (TE_PE) && (defined(BFD_ASSEMBLER) || defined(S_IS_WEAK))
999 || S_IS_WEAK (add_symbol)
1001 || S_IS_COMMON (add_symbol))
1005 symp->sy_value.X_op = O_symbol;
1006 symp->sy_value.X_add_symbol = add_symbol;
1007 symp->sy_value.X_add_number = final_val;
1008 /* Use X_op_symbol as a flag. */
1009 symp->sy_value.X_op_symbol = add_symbol;
1010 final_seg = seg_left;
1013 resolved = symbol_resolved_p (add_symbol);
1014 symp->sy_resolving = 0;
1015 goto exit_dont_set_value;
1017 else if (finalize_syms && final_seg == expr_section
1018 && seg_left != expr_section)
1020 /* If the symbol is an expression symbol, do similarly
1021 as for undefined and common syms above. Handles
1022 "sym +/- expr" where "expr" cannot be evaluated
1023 immediately, and we want relocations to be against
1024 "sym", eg. because it is weak. */
1025 symp->sy_value.X_op = O_symbol;
1026 symp->sy_value.X_add_symbol = add_symbol;
1027 symp->sy_value.X_add_number = final_val;
1028 symp->sy_value.X_op_symbol = add_symbol;
1029 final_seg = seg_left;
1030 final_val += symp->sy_frag->fr_address + left;
1031 resolved = symbol_resolved_p (add_symbol);
1032 symp->sy_resolving = 0;
1033 goto exit_dont_set_value;
1037 final_val += symp->sy_frag->fr_address + left;
1038 if (final_seg == expr_section || final_seg == undefined_section)
1039 final_seg = seg_left;
1042 resolved = symbol_resolved_p (add_symbol);
1048 left = resolve_symbol_value (add_symbol);
1049 seg_left = S_GET_SEGMENT (add_symbol);
1051 /* By reducing these to the relevant dyadic operator, we get
1052 !S -> S == 0 permitted on anything,
1053 -S -> 0 - S only permitted on absolute
1054 ~S -> S ^ ~0 only permitted on absolute */
1055 if (op != O_logical_not && seg_left != absolute_section
1057 report_op_error (symp, add_symbol, NULL);
1059 if (final_seg == expr_section || final_seg == undefined_section)
1060 final_seg = absolute_section;
1064 else if (op == O_logical_not)
1069 final_val += left + symp->sy_frag->fr_address;
1071 resolved = symbol_resolved_p (add_symbol);
1079 case O_bit_inclusive_or:
1081 case O_bit_exclusive_or:
1093 left = resolve_symbol_value (add_symbol);
1094 right = resolve_symbol_value (op_symbol);
1095 seg_left = S_GET_SEGMENT (add_symbol);
1096 seg_right = S_GET_SEGMENT (op_symbol);
1098 /* Simplify addition or subtraction of a constant by folding the
1099 constant into X_add_number. */
1102 if (seg_right == absolute_section)
1107 else if (seg_left == absolute_section)
1110 add_symbol = op_symbol;
1112 seg_left = seg_right;
1116 else if (op == O_subtract)
1118 if (seg_right == absolute_section)
1125 /* Equality and non-equality tests are permitted on anything.
1126 Subtraction, and other comparison operators are permitted if
1127 both operands are in the same section. Otherwise, both
1128 operands must be absolute. We already handled the case of
1129 addition or subtraction of a constant above. This will
1130 probably need to be changed for an object file format which
1131 supports arbitrary expressions, such as IEEE-695.
1133 Don't emit messages unless we're finalizing the symbol value,
1134 otherwise we may get the same message multiple times. */
1136 && !(seg_left == absolute_section
1137 && seg_right == absolute_section)
1138 && !(op == O_eq || op == O_ne)
1139 && !((op == O_subtract
1140 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1141 && seg_left == seg_right
1142 && (seg_left != undefined_section
1143 || add_symbol == op_symbol)))
1144 report_op_error (symp, add_symbol, op_symbol);
1146 if (final_seg == expr_section || final_seg == undefined_section)
1147 final_seg = absolute_section;
1149 /* Check for division by zero. */
1150 if ((op == O_divide || op == O_modulus) && right == 0)
1152 /* If seg_right is not absolute_section, then we've
1153 already issued a warning about using a bad symbol. */
1154 if (seg_right == absolute_section && finalize_syms)
1159 if (expr_symbol_where (symp, &file, &line))
1160 as_bad_where (file, line, _("division by zero"));
1162 as_bad (_("division by zero when setting `%s'"),
1169 switch (symp->sy_value.X_op)
1171 case O_multiply: left *= right; break;
1172 case O_divide: left /= right; break;
1173 case O_modulus: left %= right; break;
1174 case O_left_shift: left <<= right; break;
1175 case O_right_shift: left >>= right; break;
1176 case O_bit_inclusive_or: left |= right; break;
1177 case O_bit_or_not: left |= ~right; break;
1178 case O_bit_exclusive_or: left ^= right; break;
1179 case O_bit_and: left &= right; break;
1180 case O_add: left += right; break;
1181 case O_subtract: left -= right; break;
1184 left = (left == right && seg_left == seg_right
1185 && (seg_left != undefined_section
1186 || add_symbol == op_symbol)
1187 ? ~ (offsetT) 0 : 0);
1188 if (symp->sy_value.X_op == O_ne)
1191 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1192 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1193 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1194 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1195 case O_logical_and: left = left && right; break;
1196 case O_logical_or: left = left || right; break;
1200 final_val += symp->sy_frag->fr_address + left;
1201 if (final_seg == expr_section || final_seg == undefined_section)
1203 if (seg_left == undefined_section
1204 || seg_right == undefined_section)
1205 final_seg = undefined_section;
1206 else if (seg_left == absolute_section)
1207 final_seg = seg_right;
1209 final_seg = seg_left;
1211 resolved = (symbol_resolved_p (add_symbol)
1212 && symbol_resolved_p (op_symbol));
1218 /* Give an error (below) if not in expr_section. We don't
1219 want to worry about expr_section symbols, because they
1220 are fictional (they are created as part of expression
1221 resolution), and any problems may not actually mean
1226 symp->sy_resolving = 0;
1230 S_SET_VALUE (symp, final_val);
1232 exit_dont_set_value:
1233 /* Always set the segment, even if not finalizing the value.
1234 The segment is used to determine whether a symbol is defined. */
1235 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
1236 /* The old a.out backend does not handle S_SET_SEGMENT correctly
1237 for a stab symbol, so we use this bad hack. */
1238 if (final_seg != S_GET_SEGMENT (symp))
1240 S_SET_SEGMENT (symp, final_seg);
1242 /* Don't worry if we can't resolve an expr_section symbol. */
1246 symp->sy_resolved = 1;
1247 else if (S_GET_SEGMENT (symp) != expr_section)
1249 as_bad (_("can't resolve value for symbol `%s'"),
1251 symp->sy_resolved = 1;
1258 #ifdef BFD_ASSEMBLER
1260 static void resolve_local_symbol (const char *, PTR);
1262 /* A static function passed to hash_traverse. */
1265 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, PTR value)
1268 resolve_symbol_value (value);
1273 /* Resolve all local symbols. */
1276 resolve_local_symbol_values (void)
1278 #ifdef BFD_ASSEMBLER
1279 hash_traverse (local_hash, resolve_local_symbol);
1283 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1284 They are *really* local. That is, they go out of scope whenever we see a
1285 label that isn't local. Also, like fb labels, there can be multiple
1286 instances of a dollar label. Therefor, we name encode each instance with
1287 the instance number, keep a list of defined symbols separate from the real
1288 symbol table, and we treat these buggers as a sparse array. */
1290 static long *dollar_labels;
1291 static long *dollar_label_instances;
1292 static char *dollar_label_defines;
1293 static unsigned long dollar_label_count;
1294 static unsigned long dollar_label_max;
1297 dollar_label_defined (long label)
1301 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1303 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1305 return dollar_label_defines[i - dollar_labels];
1307 /* If we get here, label isn't defined. */
1312 dollar_label_instance (long label)
1316 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1318 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1320 return (dollar_label_instances[i - dollar_labels]);
1322 /* If we get here, we haven't seen the label before.
1323 Therefore its instance count is zero. */
1328 dollar_label_clear (void)
1330 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1333 #define DOLLAR_LABEL_BUMP_BY 10
1336 define_dollar_label (long label)
1340 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1343 ++dollar_label_instances[i - dollar_labels];
1344 dollar_label_defines[i - dollar_labels] = 1;
1348 /* If we get to here, we don't have label listed yet. */
1350 if (dollar_labels == NULL)
1352 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1353 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1354 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1355 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1356 dollar_label_count = 0;
1358 else if (dollar_label_count == dollar_label_max)
1360 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1361 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1362 dollar_label_max * sizeof (long));
1363 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1364 dollar_label_max * sizeof (long));
1365 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1366 } /* if we needed to grow */
1368 dollar_labels[dollar_label_count] = label;
1369 dollar_label_instances[dollar_label_count] = 1;
1370 dollar_label_defines[dollar_label_count] = 1;
1371 ++dollar_label_count;
1374 /* Caller must copy returned name: we re-use the area for the next name.
1376 The mth occurence of label n: is turned into the symbol "Ln^Am"
1377 where n is the label number and m is the instance number. "L" makes
1378 it a label discarded unless debugging and "^A"('\1') ensures no
1379 ordinary symbol SHOULD get the same name as a local label
1380 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1382 fb labels get the same treatment, except that ^B is used in place
1385 char * /* Return local label name. */
1386 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1387 register int augend /* 0 for current instance, 1 for new instance. */)
1390 /* Returned to caller, then copied. Used for created names ("4f"). */
1391 static char symbol_name_build[24];
1394 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1397 know (augend == 0 || augend == 1);
1398 p = symbol_name_build;
1399 #ifdef LOCAL_LABEL_PREFIX
1400 *p++ = LOCAL_LABEL_PREFIX;
1404 /* Next code just does sprintf( {}, "%d", n); */
1406 q = symbol_name_temporary;
1407 for (*q++ = 0, i = n; i; ++q)
1412 while ((*p = *--q) != '\0')
1415 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1417 /* Instance number. */
1418 q = symbol_name_temporary;
1419 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1424 while ((*p++ = *--q) != '\0');;
1426 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1427 return symbol_name_build;
1430 /* Somebody else's idea of local labels. They are made by "n:" where n
1431 is any decimal digit. Refer to them with
1432 "nb" for previous (backward) n:
1433 or "nf" for next (forward) n:.
1435 We do a little better and let n be any number, not just a single digit, but
1436 since the other guy's assembler only does ten, we treat the first ten
1439 Like someone else's assembler, we have one set of local label counters for
1440 entire assembly, not one set per (sub)segment like in most assemblers. This
1441 implies that one can refer to a label in another segment, and indeed some
1442 crufty compilers have done just that.
1444 Since there could be a LOT of these things, treat them as a sparse
1447 #define FB_LABEL_SPECIAL (10)
1449 static long fb_low_counter[FB_LABEL_SPECIAL];
1450 static long *fb_labels;
1451 static long *fb_label_instances;
1452 static long fb_label_count;
1453 static long fb_label_max;
1455 /* This must be more than FB_LABEL_SPECIAL. */
1456 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1459 fb_label_init (void)
1461 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1464 /* Add one to the instance number of this fb label. */
1467 fb_label_instance_inc (long label)
1471 if (label < FB_LABEL_SPECIAL)
1473 ++fb_low_counter[label];
1477 if (fb_labels != NULL)
1479 for (i = fb_labels + FB_LABEL_SPECIAL;
1480 i < fb_labels + fb_label_count; ++i)
1484 ++fb_label_instances[i - fb_labels];
1486 } /* if we find it */
1487 } /* for each existing label */
1490 /* If we get to here, we don't have label listed yet. */
1492 if (fb_labels == NULL)
1494 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1495 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1496 fb_label_max = FB_LABEL_BUMP_BY;
1497 fb_label_count = FB_LABEL_SPECIAL;
1500 else if (fb_label_count == fb_label_max)
1502 fb_label_max += FB_LABEL_BUMP_BY;
1503 fb_labels = (long *) xrealloc ((char *) fb_labels,
1504 fb_label_max * sizeof (long));
1505 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1506 fb_label_max * sizeof (long));
1507 } /* if we needed to grow */
1509 fb_labels[fb_label_count] = label;
1510 fb_label_instances[fb_label_count] = 1;
1515 fb_label_instance (long label)
1519 if (label < FB_LABEL_SPECIAL)
1521 return (fb_low_counter[label]);
1524 if (fb_labels != NULL)
1526 for (i = fb_labels + FB_LABEL_SPECIAL;
1527 i < fb_labels + fb_label_count; ++i)
1531 return (fb_label_instances[i - fb_labels]);
1532 } /* if we find it */
1533 } /* for each existing label */
1536 /* We didn't find the label, so this must be a reference to the
1541 /* Caller must copy returned name: we re-use the area for the next name.
1543 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1544 where n is the label number and m is the instance number. "L" makes
1545 it a label discarded unless debugging and "^B"('\2') ensures no
1546 ordinary symbol SHOULD get the same name as a local label
1547 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1549 dollar labels get the same treatment, except that ^A is used in
1552 char * /* Return local label name. */
1553 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1554 long augend /* 0 for nb, 1 for n:, nf. */)
1557 /* Returned to caller, then copied. Used for created names ("4f"). */
1558 static char symbol_name_build[24];
1561 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1566 (unsigned long) augend <= 2 /* See mmix_fb_label. */
1568 (unsigned long) augend <= 1
1571 p = symbol_name_build;
1572 #ifdef LOCAL_LABEL_PREFIX
1573 *p++ = LOCAL_LABEL_PREFIX;
1577 /* Next code just does sprintf( {}, "%d", n); */
1579 q = symbol_name_temporary;
1580 for (*q++ = 0, i = n; i; ++q)
1585 while ((*p = *--q) != '\0')
1588 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1590 /* Instance number. */
1591 q = symbol_name_temporary;
1592 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1597 while ((*p++ = *--q) != '\0');;
1599 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1600 return (symbol_name_build);
1603 /* Decode name that may have been generated by foo_label_name() above.
1604 If the name wasn't generated by foo_label_name(), then return it
1605 unaltered. This is used for error messages. */
1608 decode_local_label_name (char *s)
1611 char *symbol_decode;
1613 int instance_number;
1615 const char *message_format;
1618 #ifdef LOCAL_LABEL_PREFIX
1619 if (s[index] == LOCAL_LABEL_PREFIX)
1623 if (s[index] != 'L')
1626 for (label_number = 0, p = s + index + 1; ISDIGIT (*p); ++p)
1627 label_number = (10 * label_number) + *p - '0';
1629 if (*p == DOLLAR_LABEL_CHAR)
1631 else if (*p == LOCAL_LABEL_CHAR)
1636 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1637 instance_number = (10 * instance_number) + *p - '0';
1639 message_format = _("\"%d\" (instance number %d of a %s label)");
1640 symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30);
1641 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1643 return symbol_decode;
1646 /* Get the value of a symbol. */
1649 S_GET_VALUE (symbolS *s)
1651 #ifdef BFD_ASSEMBLER
1652 if (LOCAL_SYMBOL_CHECK (s))
1653 return resolve_symbol_value (s);
1656 if (!s->sy_resolved)
1658 valueT val = resolve_symbol_value (s);
1662 if (s->sy_value.X_op != O_constant)
1664 static symbolS *recur;
1666 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1667 may call S_GET_VALUE. We use a static symbol to avoid the
1668 immediate recursion. */
1670 return (valueT) s->sy_value.X_add_number;
1672 if (! s->sy_resolved
1673 || s->sy_value.X_op != O_symbol
1674 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1675 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1679 return (valueT) s->sy_value.X_add_number;
1682 /* Set the value of a symbol. */
1685 S_SET_VALUE (symbolS *s, valueT val)
1687 #ifdef BFD_ASSEMBLER
1688 if (LOCAL_SYMBOL_CHECK (s))
1690 ((struct local_symbol *) s)->lsy_value = val;
1695 s->sy_value.X_op = O_constant;
1696 s->sy_value.X_add_number = (offsetT) val;
1697 s->sy_value.X_unsigned = 0;
1701 copy_symbol_attributes (symbolS *dest, symbolS *src)
1703 if (LOCAL_SYMBOL_CHECK (dest))
1704 dest = local_symbol_convert ((struct local_symbol *) dest);
1705 if (LOCAL_SYMBOL_CHECK (src))
1706 src = local_symbol_convert ((struct local_symbol *) src);
1708 #ifdef BFD_ASSEMBLER
1709 /* In an expression, transfer the settings of these flags.
1710 The user can override later, of course. */
1711 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1712 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1715 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1716 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1720 #ifdef BFD_ASSEMBLER
1723 S_IS_FUNCTION (symbolS *s)
1727 if (LOCAL_SYMBOL_CHECK (s))
1730 flags = s->bsym->flags;
1732 return (flags & BSF_FUNCTION) != 0;
1736 S_IS_EXTERNAL (symbolS *s)
1740 if (LOCAL_SYMBOL_CHECK (s))
1743 flags = s->bsym->flags;
1746 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1749 return (flags & BSF_GLOBAL) != 0;
1753 S_IS_WEAK (symbolS *s)
1755 if (LOCAL_SYMBOL_CHECK (s))
1757 return (s->bsym->flags & BSF_WEAK) != 0;
1761 S_IS_COMMON (symbolS *s)
1763 if (LOCAL_SYMBOL_CHECK (s))
1765 return bfd_is_com_section (s->bsym->section);
1769 S_IS_DEFINED (symbolS *s)
1771 if (LOCAL_SYMBOL_CHECK (s))
1772 return ((struct local_symbol *) s)->lsy_section != undefined_section;
1773 return s->bsym->section != undefined_section;
1777 #ifndef EXTERN_FORCE_RELOC
1778 #define EXTERN_FORCE_RELOC IS_ELF
1781 /* Return true for symbols that should not be reduced to section
1782 symbols or eliminated from expressions, because they may be
1783 overridden by the linker. */
1785 S_FORCE_RELOC (symbolS *s, int strict)
1787 if (LOCAL_SYMBOL_CHECK (s))
1788 return ((struct local_symbol *) s)->lsy_section == undefined_section;
1791 && ((s->bsym->flags & BSF_WEAK) != 0
1792 || (EXTERN_FORCE_RELOC
1793 && (s->bsym->flags & BSF_GLOBAL) != 0)))
1794 || s->bsym->section == undefined_section
1795 || bfd_is_com_section (s->bsym->section));
1799 S_IS_DEBUG (symbolS *s)
1801 if (LOCAL_SYMBOL_CHECK (s))
1803 if (s->bsym->flags & BSF_DEBUGGING)
1809 S_IS_LOCAL (symbolS *s)
1814 if (LOCAL_SYMBOL_CHECK (s))
1817 flags = s->bsym->flags;
1820 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1823 if (bfd_get_section (s->bsym) == reg_section)
1826 if (flag_strip_local_absolute
1827 /* Keep BSF_FILE symbols in order to allow debuggers to identify
1828 the source file even when the object file is stripped. */
1829 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
1830 && bfd_get_section (s->bsym) == absolute_section)
1833 name = S_GET_NAME (s);
1834 return (name != NULL
1836 && (strchr (name, DOLLAR_LABEL_CHAR)
1837 || strchr (name, LOCAL_LABEL_CHAR)
1838 || (! flag_keep_locals
1839 && (bfd_is_local_label (stdoutput, s->bsym)
1842 && name[1] == '?')))));
1846 S_IS_EXTERN (symbolS *s)
1848 return S_IS_EXTERNAL (s);
1852 S_IS_STABD (symbolS *s)
1854 return S_GET_NAME (s) == 0;
1858 S_GET_NAME (symbolS *s)
1860 if (LOCAL_SYMBOL_CHECK (s))
1861 return ((struct local_symbol *) s)->lsy_name;
1862 return s->bsym->name;
1866 S_GET_SEGMENT (symbolS *s)
1868 if (LOCAL_SYMBOL_CHECK (s))
1869 return ((struct local_symbol *) s)->lsy_section;
1870 return s->bsym->section;
1874 S_SET_SEGMENT (symbolS *s, segT seg)
1876 /* Don't reassign section symbols. The direct reason is to prevent seg
1877 faults assigning back to const global symbols such as *ABS*, but it
1878 shouldn't happen anyway. */
1880 if (LOCAL_SYMBOL_CHECK (s))
1882 if (seg == reg_section)
1883 s = local_symbol_convert ((struct local_symbol *) s);
1886 ((struct local_symbol *) s)->lsy_section = seg;
1891 if (s->bsym->flags & BSF_SECTION_SYM)
1893 if (s->bsym->section != seg)
1897 s->bsym->section = seg;
1901 S_SET_EXTERNAL (symbolS *s)
1903 if (LOCAL_SYMBOL_CHECK (s))
1904 s = local_symbol_convert ((struct local_symbol *) s);
1905 if ((s->bsym->flags & BSF_WEAK) != 0)
1907 /* Let .weak override .global. */
1910 if (s->bsym->flags & BSF_SECTION_SYM)
1915 /* Do not reassign section symbols. */
1916 as_where (& file, & line);
1917 as_warn_where (file, line,
1918 _("section symbols are already global"));
1921 s->bsym->flags |= BSF_GLOBAL;
1922 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
1925 if (! an_external_name && S_GET_NAME(s)[0] != '.')
1926 an_external_name = S_GET_NAME (s);
1931 S_CLEAR_EXTERNAL (symbolS *s)
1933 if (LOCAL_SYMBOL_CHECK (s))
1935 if ((s->bsym->flags & BSF_WEAK) != 0)
1937 /* Let .weak override. */
1940 s->bsym->flags |= BSF_LOCAL;
1941 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
1945 S_SET_WEAK (symbolS *s)
1947 if (LOCAL_SYMBOL_CHECK (s))
1948 s = local_symbol_convert ((struct local_symbol *) s);
1949 s->bsym->flags |= BSF_WEAK;
1950 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
1954 S_SET_THREAD_LOCAL (symbolS *s)
1956 if (LOCAL_SYMBOL_CHECK (s))
1957 s = local_symbol_convert ((struct local_symbol *) s);
1958 if (bfd_is_com_section (s->bsym->section)
1959 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
1961 s->bsym->flags |= BSF_THREAD_LOCAL;
1962 if ((s->bsym->flags & BSF_FUNCTION) != 0)
1963 as_bad (_("Accessing function `%s' as thread-local object"),
1965 else if (! bfd_is_und_section (s->bsym->section)
1966 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
1967 as_bad (_("Accessing `%s' as thread-local object"),
1972 S_SET_NAME (symbolS *s, const char *name)
1974 if (LOCAL_SYMBOL_CHECK (s))
1976 ((struct local_symbol *) s)->lsy_name = name;
1979 s->bsym->name = name;
1981 #endif /* BFD_ASSEMBLER */
1983 #ifdef SYMBOLS_NEED_BACKPOINTERS
1985 /* Return the previous symbol in a chain. */
1988 symbol_previous (symbolS *s)
1990 if (LOCAL_SYMBOL_CHECK (s))
1992 return s->sy_previous;
1995 #endif /* SYMBOLS_NEED_BACKPOINTERS */
1997 /* Return the next symbol in a chain. */
2000 symbol_next (symbolS *s)
2002 if (LOCAL_SYMBOL_CHECK (s))
2007 /* Return a pointer to the value of a symbol as an expression. */
2010 symbol_get_value_expression (symbolS *s)
2012 if (LOCAL_SYMBOL_CHECK (s))
2013 s = local_symbol_convert ((struct local_symbol *) s);
2014 return &s->sy_value;
2017 /* Set the value of a symbol to an expression. */
2020 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2022 if (LOCAL_SYMBOL_CHECK (s))
2023 s = local_symbol_convert ((struct local_symbol *) s);
2027 /* Set the value of SYM to the current position in the current segment. */
2030 symbol_set_value_now (symbolS *sym)
2032 S_SET_SEGMENT (sym, now_seg);
2033 S_SET_VALUE (sym, frag_now_fix ());
2034 symbol_set_frag (sym, frag_now);
2037 /* Set the frag of a symbol. */
2040 symbol_set_frag (symbolS *s, fragS *f)
2042 #ifdef BFD_ASSEMBLER
2043 if (LOCAL_SYMBOL_CHECK (s))
2045 local_symbol_set_frag ((struct local_symbol *) s, f);
2052 /* Return the frag of a symbol. */
2055 symbol_get_frag (symbolS *s)
2057 #ifdef BFD_ASSEMBLER
2058 if (LOCAL_SYMBOL_CHECK (s))
2059 return local_symbol_get_frag ((struct local_symbol *) s);
2064 /* Mark a symbol as having been used. */
2067 symbol_mark_used (symbolS *s)
2069 if (LOCAL_SYMBOL_CHECK (s))
2074 /* Clear the mark of whether a symbol has been used. */
2077 symbol_clear_used (symbolS *s)
2079 if (LOCAL_SYMBOL_CHECK (s))
2080 s = local_symbol_convert ((struct local_symbol *) s);
2084 /* Return whether a symbol has been used. */
2087 symbol_used_p (symbolS *s)
2089 if (LOCAL_SYMBOL_CHECK (s))
2094 /* Mark a symbol as having been used in a reloc. */
2097 symbol_mark_used_in_reloc (symbolS *s)
2099 if (LOCAL_SYMBOL_CHECK (s))
2100 s = local_symbol_convert ((struct local_symbol *) s);
2101 s->sy_used_in_reloc = 1;
2104 /* Clear the mark of whether a symbol has been used in a reloc. */
2107 symbol_clear_used_in_reloc (symbolS *s)
2109 if (LOCAL_SYMBOL_CHECK (s))
2111 s->sy_used_in_reloc = 0;
2114 /* Return whether a symbol has been used in a reloc. */
2117 symbol_used_in_reloc_p (symbolS *s)
2119 if (LOCAL_SYMBOL_CHECK (s))
2121 return s->sy_used_in_reloc;
2124 /* Mark a symbol as an MRI common symbol. */
2127 symbol_mark_mri_common (symbolS *s)
2129 if (LOCAL_SYMBOL_CHECK (s))
2130 s = local_symbol_convert ((struct local_symbol *) s);
2131 s->sy_mri_common = 1;
2134 /* Clear the mark of whether a symbol is an MRI common symbol. */
2137 symbol_clear_mri_common (symbolS *s)
2139 if (LOCAL_SYMBOL_CHECK (s))
2141 s->sy_mri_common = 0;
2144 /* Return whether a symbol is an MRI common symbol. */
2147 symbol_mri_common_p (symbolS *s)
2149 if (LOCAL_SYMBOL_CHECK (s))
2151 return s->sy_mri_common;
2154 /* Mark a symbol as having been written. */
2157 symbol_mark_written (symbolS *s)
2159 if (LOCAL_SYMBOL_CHECK (s))
2164 /* Clear the mark of whether a symbol has been written. */
2167 symbol_clear_written (symbolS *s)
2169 if (LOCAL_SYMBOL_CHECK (s))
2174 /* Return whether a symbol has been written. */
2177 symbol_written_p (symbolS *s)
2179 if (LOCAL_SYMBOL_CHECK (s))
2184 /* Mark a symbol has having been resolved. */
2187 symbol_mark_resolved (symbolS *s)
2189 #ifdef BFD_ASSEMBLER
2190 if (LOCAL_SYMBOL_CHECK (s))
2192 local_symbol_mark_resolved ((struct local_symbol *) s);
2199 /* Return whether a symbol has been resolved. */
2202 symbol_resolved_p (symbolS *s)
2204 #ifdef BFD_ASSEMBLER
2205 if (LOCAL_SYMBOL_CHECK (s))
2206 return local_symbol_resolved_p ((struct local_symbol *) s);
2208 return s->sy_resolved;
2211 /* Return whether a symbol is a section symbol. */
2214 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2216 if (LOCAL_SYMBOL_CHECK (s))
2218 #ifdef BFD_ASSEMBLER
2219 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2226 /* Return whether a symbol is equated to another symbol. */
2229 symbol_equated_p (symbolS *s)
2231 if (LOCAL_SYMBOL_CHECK (s))
2233 return s->sy_value.X_op == O_symbol;
2236 /* Return whether a symbol is equated to another symbol, and should be
2237 treated specially when writing out relocs. */
2240 symbol_equated_reloc_p (symbolS *s)
2242 if (LOCAL_SYMBOL_CHECK (s))
2244 /* X_op_symbol, normally not used for O_symbol, is set by
2245 resolve_symbol_value to flag expression syms that have been
2247 return (s->sy_value.X_op == O_symbol
2248 #if defined (OBJ_COFF) && defined (TE_PE) && (defined(BFD_ASSEMBLER) || defined(S_IS_WEAK))
2251 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2252 || ! S_IS_DEFINED (s)
2253 || S_IS_COMMON (s)));
2256 /* Return whether a symbol has a constant value. */
2259 symbol_constant_p (symbolS *s)
2261 if (LOCAL_SYMBOL_CHECK (s))
2263 return s->sy_value.X_op == O_constant;
2266 #ifdef BFD_ASSEMBLER
2268 /* Return the BFD symbol for a symbol. */
2271 symbol_get_bfdsym (symbolS *s)
2273 if (LOCAL_SYMBOL_CHECK (s))
2274 s = local_symbol_convert ((struct local_symbol *) s);
2278 /* Set the BFD symbol for a symbol. */
2281 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2283 if (LOCAL_SYMBOL_CHECK (s))
2284 s = local_symbol_convert ((struct local_symbol *) s);
2285 /* Usually, it is harmless to reset a symbol to a BFD section
2286 symbol. For example, obj_elf_change_section sets the BFD symbol
2287 of an old symbol with the newly created section symbol. But when
2288 we have multiple sections with the same name, the newly created
2289 section may have the same name as an old section. We check if the
2290 old symbol has been already marked as a section symbol before
2292 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2294 /* else XXX - What do we do now ? */
2297 #endif /* BFD_ASSEMBLER */
2299 #ifdef OBJ_SYMFIELD_TYPE
2301 /* Get a pointer to the object format information for a symbol. */
2304 symbol_get_obj (symbolS *s)
2306 if (LOCAL_SYMBOL_CHECK (s))
2307 s = local_symbol_convert ((struct local_symbol *) s);
2311 /* Set the object format information for a symbol. */
2314 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2316 if (LOCAL_SYMBOL_CHECK (s))
2317 s = local_symbol_convert ((struct local_symbol *) s);
2321 #endif /* OBJ_SYMFIELD_TYPE */
2323 #ifdef TC_SYMFIELD_TYPE
2325 /* Get a pointer to the processor information for a symbol. */
2328 symbol_get_tc (symbolS *s)
2330 if (LOCAL_SYMBOL_CHECK (s))
2331 s = local_symbol_convert ((struct local_symbol *) s);
2335 /* Set the processor information for a symbol. */
2338 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2340 if (LOCAL_SYMBOL_CHECK (s))
2341 s = local_symbol_convert ((struct local_symbol *) s);
2345 #endif /* TC_SYMFIELD_TYPE */
2350 symbol_lastP = NULL;
2351 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2352 sy_hash = hash_new ();
2353 #ifdef BFD_ASSEMBLER
2354 local_hash = hash_new ();
2357 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2358 #ifdef BFD_ASSEMBLER
2359 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2360 abs_symbol.bsym = bfd_abs_section.symbol;
2363 /* Can't initialise a union. Sigh. */
2364 S_SET_SEGMENT (&abs_symbol, absolute_section);
2366 abs_symbol.sy_value.X_op = O_constant;
2367 abs_symbol.sy_frag = &zero_address_frag;
2369 if (LOCAL_LABELS_FB)
2375 /* Maximum indent level.
2376 Available for modification inside a gdb session. */
2377 int max_indent_level = 8;
2380 print_symbol_value_1 (FILE *file, symbolS *sym)
2382 const char *name = S_GET_NAME (sym);
2383 if (!name || !name[0])
2385 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2387 if (LOCAL_SYMBOL_CHECK (sym))
2389 #ifdef BFD_ASSEMBLER
2390 struct local_symbol *locsym = (struct local_symbol *) sym;
2391 if (local_symbol_get_frag (locsym) != &zero_address_frag
2392 && local_symbol_get_frag (locsym) != NULL)
2393 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2394 if (local_symbol_resolved_p (locsym))
2395 fprintf (file, " resolved");
2396 fprintf (file, " local");
2401 if (sym->sy_frag != &zero_address_frag)
2402 fprintf (file, " frag %lx", (long) sym->sy_frag);
2404 fprintf (file, " written");
2405 if (sym->sy_resolved)
2406 fprintf (file, " resolved");
2407 else if (sym->sy_resolving)
2408 fprintf (file, " resolving");
2409 if (sym->sy_used_in_reloc)
2410 fprintf (file, " used-in-reloc");
2412 fprintf (file, " used");
2413 if (S_IS_LOCAL (sym))
2414 fprintf (file, " local");
2415 if (S_IS_EXTERN (sym))
2416 fprintf (file, " extern");
2417 if (S_IS_DEBUG (sym))
2418 fprintf (file, " debug");
2419 if (S_IS_DEFINED (sym))
2420 fprintf (file, " defined");
2422 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2423 if (symbol_resolved_p (sym))
2425 segT s = S_GET_SEGMENT (sym);
2427 if (s != undefined_section
2428 && s != expr_section)
2429 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2431 else if (indent_level < max_indent_level
2432 && S_GET_SEGMENT (sym) != undefined_section)
2435 fprintf (file, "\n%*s<", indent_level * 4, "");
2436 #ifdef BFD_ASSEMBLER
2437 if (LOCAL_SYMBOL_CHECK (sym))
2438 fprintf (file, "constant %lx",
2439 (long) ((struct local_symbol *) sym)->lsy_value);
2442 print_expr_1 (file, &sym->sy_value);
2443 fprintf (file, ">");
2450 print_symbol_value (symbolS *sym)
2453 print_symbol_value_1 (stderr, sym);
2454 fprintf (stderr, "\n");
2458 print_binary (FILE *file, const char *name, expressionS *exp)
2461 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2462 print_symbol_value_1 (file, exp->X_add_symbol);
2463 fprintf (file, ">\n%*s<", indent_level * 4, "");
2464 print_symbol_value_1 (file, exp->X_op_symbol);
2465 fprintf (file, ">");
2470 print_expr_1 (FILE *file, expressionS *exp)
2472 fprintf (file, "expr %lx ", (long) exp);
2476 fprintf (file, "illegal");
2479 fprintf (file, "absent");
2482 fprintf (file, "constant %lx", (long) exp->X_add_number);
2486 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2487 print_symbol_value_1 (file, exp->X_add_symbol);
2488 fprintf (file, ">");
2490 if (exp->X_add_number)
2491 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2492 (long) exp->X_add_number);
2496 fprintf (file, "register #%d", (int) exp->X_add_number);
2499 fprintf (file, "big");
2502 fprintf (file, "uminus -<");
2504 print_symbol_value_1 (file, exp->X_add_symbol);
2505 fprintf (file, ">");
2506 goto maybe_print_addnum;
2508 fprintf (file, "bit_not");
2511 print_binary (file, "multiply", exp);
2514 print_binary (file, "divide", exp);
2517 print_binary (file, "modulus", exp);
2520 print_binary (file, "lshift", exp);
2523 print_binary (file, "rshift", exp);
2525 case O_bit_inclusive_or:
2526 print_binary (file, "bit_ior", exp);
2528 case O_bit_exclusive_or:
2529 print_binary (file, "bit_xor", exp);
2532 print_binary (file, "bit_and", exp);
2535 print_binary (file, "eq", exp);
2538 print_binary (file, "ne", exp);
2541 print_binary (file, "lt", exp);
2544 print_binary (file, "le", exp);
2547 print_binary (file, "ge", exp);
2550 print_binary (file, "gt", exp);
2553 print_binary (file, "logical_and", exp);
2556 print_binary (file, "logical_or", exp);
2560 fprintf (file, "add\n%*s<", indent_level * 4, "");
2561 print_symbol_value_1 (file, exp->X_add_symbol);
2562 fprintf (file, ">\n%*s<", indent_level * 4, "");
2563 print_symbol_value_1 (file, exp->X_op_symbol);
2564 fprintf (file, ">");
2565 goto maybe_print_addnum;
2568 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2569 print_symbol_value_1 (file, exp->X_add_symbol);
2570 fprintf (file, ">\n%*s<", indent_level * 4, "");
2571 print_symbol_value_1 (file, exp->X_op_symbol);
2572 fprintf (file, ">");
2573 goto maybe_print_addnum;
2575 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2582 print_expr (expressionS *exp)
2584 print_expr_1 (stderr, exp);
2585 fprintf (stderr, "\n");
2589 symbol_print_statistics (FILE *file)
2591 hash_print_statistics (file, "symbol table", sy_hash);
2592 #ifdef BFD_ASSEMBLER
2593 hash_print_statistics (file, "mini local symbol table", local_hash);
2594 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2595 local_symbol_count, local_symbol_conversion_count);