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, 51 Franklin Street - Fifth Floor, 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. */
92 extern int symbol_table_frozen;
93 if (symbol_table_frozen)
96 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
101 /* Save a symbol name on a permanent obstack, and convert it according
102 to the object file format. */
105 save_symbol_name (const char *name)
107 unsigned int name_length;
110 name_length = strlen (name) + 1; /* +1 for \0. */
111 obstack_grow (¬es, name, name_length);
112 ret = obstack_finish (¬es);
114 #ifdef tc_canonicalize_symbol_name
115 ret = tc_canonicalize_symbol_name (ret);
118 if (! symbols_case_sensitive)
122 for (s = ret; *s != '\0'; s++)
130 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
131 segT segment, /* Segment identifier (SEG_<something>). */
132 valueT valu, /* Symbol value. */
133 fragS *frag /* Associated fragment. */)
135 char *preserved_copy_of_name;
138 preserved_copy_of_name = save_symbol_name (name);
140 symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
142 /* symbol must be born in some fixed state. This seems as good as any. */
143 memset (symbolP, 0, sizeof (symbolS));
145 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
146 if (symbolP->bsym == NULL)
147 as_perror ("%s", "bfd_make_empty_symbol");
148 symbolP->bsym->udata.p = (PTR) symbolP;
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) \
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 static struct local_symbol *
192 local_symbol_make (const char *name, segT section, valueT value, fragS *frag)
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_marker = NULL;
203 ret->lsy_name = name_copy;
204 ret->lsy_section = section;
205 local_symbol_set_frag (ret, frag);
206 ret->lsy_value = value;
208 hash_jam (local_hash, name_copy, (PTR) ret);
213 /* Convert a local symbol into a real symbol. Note that we do not
214 reclaim the space used by the local symbol. */
217 local_symbol_convert (struct local_symbol *locsym)
221 assert (locsym->lsy_marker == NULL);
222 if (local_symbol_converted_p (locsym))
223 return local_symbol_get_real_symbol (locsym);
225 ++local_symbol_conversion_count;
227 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
228 local_symbol_get_frag (locsym));
230 if (local_symbol_resolved_p (locsym))
231 ret->sy_resolved = 1;
233 /* Local symbols are always either defined or used. */
236 #ifdef TC_LOCAL_SYMFIELD_CONVERT
237 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
240 symbol_table_insert (ret);
242 local_symbol_mark_converted (locsym);
243 local_symbol_set_real_symbol (locsym, ret);
245 hash_jam (local_hash, locsym->lsy_name, NULL);
250 /* We have just seen "<name>:".
251 Creates a struct symbol unless it already exists.
253 Gripes if we are redefining a symbol incompatibly (and ignores it). */
256 colon (/* Just seen "x:" - rattle symbols & frags. */
257 const char *sym_name /* Symbol name, as a cannonical string. */
258 /* We copy this string: OK to alter later. */)
260 register symbolS *symbolP; /* Symbol we are working with. */
262 /* Sun local labels go out of scope whenever a non-local symbol is
264 if (LOCAL_LABELS_DOLLAR
265 && !bfd_is_local_label_name (stdoutput, sym_name))
266 dollar_label_clear ();
268 #ifndef WORKING_DOT_WORD
269 if (new_broken_words)
271 struct broken_word *a;
276 if (now_seg == absolute_section)
278 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
282 possible_bytes = (md_short_jump_size
283 + new_broken_words * md_long_jump_size);
286 frag_opcode = frag_var (rs_broken_word,
290 (symbolS *) broken_words,
294 /* We want to store the pointer to where to insert the jump
295 table in the fr_opcode of the rs_broken_word frag. This
296 requires a little hackery. */
298 && (frag_tmp->fr_type != rs_broken_word
299 || frag_tmp->fr_opcode))
300 frag_tmp = frag_tmp->fr_next;
302 frag_tmp->fr_opcode = frag_opcode;
303 new_broken_words = 0;
305 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
306 a->dispfrag = frag_tmp;
308 #endif /* WORKING_DOT_WORD */
310 if ((symbolP = symbol_find (sym_name)) != 0)
312 #ifdef RESOLVE_SYMBOL_REDEFINITION
313 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
316 /* Now check for undefined symbols. */
317 if (LOCAL_SYMBOL_CHECK (symbolP))
319 struct local_symbol *locsym = (struct local_symbol *) symbolP;
321 if (locsym->lsy_section != undefined_section
322 && (local_symbol_get_frag (locsym) != frag_now
323 || locsym->lsy_section != now_seg
324 || locsym->lsy_value != frag_now_fix ()))
326 as_bad (_("symbol `%s' is already defined"), sym_name);
330 locsym->lsy_section = now_seg;
331 local_symbol_set_frag (locsym, frag_now);
332 locsym->lsy_value = frag_now_fix ();
334 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
336 if (S_GET_VALUE (symbolP) == 0)
338 symbolP->sy_frag = frag_now;
340 S_SET_OTHER (symbolP, const_flag);
342 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
343 S_SET_SEGMENT (symbolP, now_seg);
346 #endif /* if we have one, it better be zero. */
351 /* There are still several cases to check:
353 A .comm/.lcomm symbol being redefined as initialized
356 A .comm/.lcomm symbol being redefined with a larger
359 This only used to be allowed on VMS gas, but Sun cc
360 on the sparc also depends on it. */
362 if (((!S_IS_DEBUG (symbolP)
363 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
364 && S_IS_EXTERNAL (symbolP))
365 || S_GET_SEGMENT (symbolP) == bss_section)
366 && (now_seg == data_section
367 || now_seg == S_GET_SEGMENT (symbolP)))
369 /* Select which of the 2 cases this is. */
370 if (now_seg != data_section)
372 /* New .comm for prev .comm symbol.
374 If the new size is larger we just change its
375 value. If the new size is smaller, we ignore
377 if (S_GET_VALUE (symbolP)
378 < ((unsigned) frag_now_fix ()))
380 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
385 /* It is a .comm/.lcomm being converted to initialized
387 symbolP->sy_frag = frag_now;
389 S_SET_OTHER (symbolP, const_flag);
391 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
392 S_SET_SEGMENT (symbolP, now_seg); /* Keep N_EXT bit. */
397 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
398 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
399 static const char *od_buf = "";
403 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
404 sprintf (od_buf, "%d.%d.",
405 S_GET_OTHER (symbolP),
406 S_GET_DESC (symbolP));
408 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
410 segment_name (S_GET_SEGMENT (symbolP)),
412 (long) S_GET_VALUE (symbolP));
414 } /* if the undefined symbol has no value */
418 /* Don't blow up if the definition is the same. */
419 if (!(frag_now == symbolP->sy_frag
420 && S_GET_VALUE (symbolP) == frag_now_fix ()
421 && S_GET_SEGMENT (symbolP) == now_seg))
422 as_bad (_("symbol `%s' is already defined"), sym_name);
426 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
428 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
429 (valueT) frag_now_fix (),
434 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
437 S_SET_OTHER (symbolP, const_flag);
440 symbol_table_insert (symbolP);
443 if (mri_common_symbol != NULL)
445 /* This symbol is actually being defined within an MRI common
446 section. This requires special handling. */
447 if (LOCAL_SYMBOL_CHECK (symbolP))
448 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
449 symbolP->sy_value.X_op = O_symbol;
450 symbolP->sy_value.X_add_symbol = mri_common_symbol;
451 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
452 symbolP->sy_frag = &zero_address_frag;
453 S_SET_SEGMENT (symbolP, expr_section);
454 symbolP->sy_mri_common = 1;
458 tc_frob_label (symbolP);
460 #ifdef obj_frob_label
461 obj_frob_label (symbolP);
467 /* Die if we can't insert the symbol. */
470 symbol_table_insert (symbolS *symbolP)
472 register const char *error_string;
475 know (S_GET_NAME (symbolP));
477 if (LOCAL_SYMBOL_CHECK (symbolP))
479 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
481 if (error_string != NULL)
482 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
483 S_GET_NAME (symbolP), error_string);
487 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
489 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
490 S_GET_NAME (symbolP), error_string);
494 /* If a symbol name does not exist, create it as undefined, and insert
495 it into the symbol table. Return a pointer to it. */
498 symbol_find_or_make (const char *name)
500 register symbolS *symbolP;
502 symbolP = symbol_find (name);
506 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
508 symbolP = md_undefined_symbol ((char *) name);
512 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
518 symbolP = symbol_make (name);
520 symbol_table_insert (symbolP);
521 } /* if symbol wasn't found */
527 symbol_make (const char *name)
531 /* Let the machine description default it, e.g. for register names. */
532 symbolP = md_undefined_symbol ((char *) name);
535 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
541 symbol_clone (symbolS *orgsymP, int replace)
545 /* Running local_symbol_convert on a clone that's not the one currently
546 in local_hash would incorrectly replace the hash entry. Thus the
547 symbol must be converted here. Note that the rest of the function
548 depends on not encountering an unconverted symbol. */
549 if (LOCAL_SYMBOL_CHECK (orgsymP))
550 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
552 know (S_IS_DEFINED (orgsymP));
554 newsymP = obstack_alloc (¬es, sizeof (*newsymP));
559 if (symbol_rootP == orgsymP)
560 symbol_rootP = newsymP;
561 else if (orgsymP->sy_previous)
563 orgsymP->sy_previous->sy_next = newsymP;
564 orgsymP->sy_previous = NULL;
566 if (symbol_lastP == orgsymP)
567 symbol_lastP = newsymP;
568 else if (orgsymP->sy_next)
569 orgsymP->sy_next->sy_previous = newsymP;
570 orgsymP->sy_next = NULL;
571 debug_verify_symchain (symbol_rootP, symbol_lastP);
573 symbol_table_insert (newsymP);
579 /* Referenced symbols, if they are forward references, need to be cloned
580 (without replacing the original) so that the value of the referenced
581 symbols at the point of use . */
583 #undef symbol_clone_if_forward_ref
585 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
587 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
589 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
590 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
592 if (symbolP->sy_forward_ref)
597 /* assign_symbol() clones volatile symbols; pre-existing expressions
598 hold references to the original instance, but want the current
599 value. Just repeat the lookup. */
600 if (add_symbol && S_IS_VOLATILE (add_symbol))
601 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
602 if (op_symbol && S_IS_VOLATILE (op_symbol))
603 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
606 /* Re-using sy_resolving here, as this routine cannot get called from
607 symbol resolution code. */
608 if (symbolP->bsym->section == expr_section && !symbolP->sy_resolving)
610 symbolP->sy_resolving = 1;
611 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
612 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
613 symbolP->sy_resolving = 0;
616 if (symbolP->sy_forward_ref
617 || add_symbol != symbolP->sy_value.X_add_symbol
618 || op_symbol != symbolP->sy_value.X_op_symbol)
619 symbolP = symbol_clone (symbolP, 0);
621 symbolP->sy_value.X_add_symbol = add_symbol;
622 symbolP->sy_value.X_op_symbol = op_symbol;
629 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
631 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
635 symbol_temp_new_now (void)
637 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
641 symbol_temp_make (void)
643 return symbol_make (FAKE_LABEL_NAME);
646 /* Implement symbol table lookup.
647 In: A symbol's name as a string: '\0' can't be part of a symbol name.
648 Out: NULL if the name was not in the symbol table, else the address
649 of a struct symbol associated with that name. */
652 symbol_find_exact (const char *name)
654 struct local_symbol *locsym;
656 locsym = (struct local_symbol *) hash_find (local_hash, name);
658 return (symbolS *) locsym;
660 return ((symbolS *) hash_find (sy_hash, name));
664 symbol_find (const char *name)
666 #ifdef tc_canonicalize_symbol_name
669 size_t len = strlen (name) + 1;
671 copy = (char *) alloca (len);
672 memcpy (copy, name, len);
673 name = tc_canonicalize_symbol_name (copy);
677 if (! symbols_case_sensitive)
684 name = copy = (char *) alloca (strlen (name) + 1);
686 while ((c = *orig++) != '\0')
688 *copy++ = TOUPPER (c);
693 return symbol_find_exact (name);
696 /* Once upon a time, symbols were kept in a singly linked list. At
697 least coff needs to be able to rearrange them from time to time, for
698 which a doubly linked list is much more convenient. Loic did these
699 as macros which seemed dangerous to me so they're now functions.
702 /* Link symbol ADDME after symbol TARGET in the chain. */
705 symbol_append (symbolS *addme, symbolS *target,
706 symbolS **rootPP, symbolS **lastPP)
708 if (LOCAL_SYMBOL_CHECK (addme))
710 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
715 know (*rootPP == NULL);
716 know (*lastPP == NULL);
717 addme->sy_next = NULL;
718 addme->sy_previous = NULL;
722 } /* if the list is empty */
724 if (target->sy_next != NULL)
726 target->sy_next->sy_previous = addme;
730 know (*lastPP == target);
732 } /* if we have a next */
734 addme->sy_next = target->sy_next;
735 target->sy_next = addme;
736 addme->sy_previous = target;
738 debug_verify_symchain (symbol_rootP, symbol_lastP);
741 /* Set the chain pointers of SYMBOL to null. */
744 symbol_clear_list_pointers (symbolS *symbolP)
746 if (LOCAL_SYMBOL_CHECK (symbolP))
748 symbolP->sy_next = NULL;
749 symbolP->sy_previous = NULL;
752 /* Remove SYMBOLP from the list. */
755 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
757 if (LOCAL_SYMBOL_CHECK (symbolP))
760 if (symbolP == *rootPP)
762 *rootPP = symbolP->sy_next;
763 } /* if it was the root */
765 if (symbolP == *lastPP)
767 *lastPP = symbolP->sy_previous;
768 } /* if it was the tail */
770 if (symbolP->sy_next != NULL)
772 symbolP->sy_next->sy_previous = symbolP->sy_previous;
775 if (symbolP->sy_previous != NULL)
777 symbolP->sy_previous->sy_next = symbolP->sy_next;
780 debug_verify_symchain (*rootPP, *lastPP);
783 /* Link symbol ADDME before symbol TARGET in the chain. */
786 symbol_insert (symbolS *addme, symbolS *target,
787 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
789 if (LOCAL_SYMBOL_CHECK (addme))
791 if (LOCAL_SYMBOL_CHECK (target))
794 if (target->sy_previous != NULL)
796 target->sy_previous->sy_next = addme;
800 know (*rootPP == target);
804 addme->sy_previous = target->sy_previous;
805 target->sy_previous = addme;
806 addme->sy_next = target;
808 debug_verify_symchain (*rootPP, *lastPP);
812 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
814 symbolS *symbolP = rootP;
819 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
821 assert (symbolP->bsym != NULL);
822 assert (symbolP->sy_next->sy_previous == symbolP);
825 assert (lastP == symbolP);
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;
894 if (LOCAL_SYMBOL_CHECK (symp))
896 struct local_symbol *locsym = (struct local_symbol *) symp;
898 final_val = locsym->lsy_value;
899 if (local_symbol_resolved_p (locsym))
902 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
906 locsym->lsy_value = final_val;
907 local_symbol_mark_resolved (locsym);
913 if (symp->sy_resolved)
915 if (symp->sy_value.X_op == O_constant)
916 return (valueT) symp->sy_value.X_add_number;
922 final_seg = S_GET_SEGMENT (symp);
924 if (symp->sy_resolving)
927 as_bad (_("symbol definition loop encountered at `%s'"),
934 symbolS *add_symbol, *op_symbol;
936 segT seg_left, seg_right;
940 symp->sy_resolving = 1;
942 /* Help out with CSE. */
943 add_symbol = symp->sy_value.X_add_symbol;
944 op_symbol = symp->sy_value.X_op_symbol;
945 final_val = symp->sy_value.X_add_number;
946 op = symp->sy_value.X_op;
959 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
960 if (final_seg == expr_section)
961 final_seg = absolute_section;
967 left = resolve_symbol_value (add_symbol);
968 seg_left = S_GET_SEGMENT (add_symbol);
970 symp->sy_value.X_op_symbol = NULL;
973 if (symp->sy_mri_common)
975 /* This is a symbol inside an MRI common section. The
976 relocation routines are going to handle it specially.
977 Don't change the value. */
978 resolved = symbol_resolved_p (add_symbol);
982 if (finalize_syms && final_val == 0)
984 if (LOCAL_SYMBOL_CHECK (add_symbol))
985 add_symbol = local_symbol_convert ((struct local_symbol *)
987 copy_symbol_attributes (symp, add_symbol);
990 /* If we have equated this symbol to an undefined or common
991 symbol, keep X_op set to O_symbol, and don't change
992 X_add_number. This permits the routine which writes out
993 relocation to detect this case, and convert the
994 relocation to be against the symbol to which this symbol
996 if (! S_IS_DEFINED (add_symbol)
997 #if defined (OBJ_COFF) && defined (TE_PE)
998 || S_IS_WEAK (add_symbol)
1000 || S_IS_COMMON (add_symbol))
1004 symp->sy_value.X_op = O_symbol;
1005 symp->sy_value.X_add_symbol = add_symbol;
1006 symp->sy_value.X_add_number = final_val;
1007 /* Use X_op_symbol as a flag. */
1008 symp->sy_value.X_op_symbol = add_symbol;
1009 final_seg = seg_left;
1012 resolved = symbol_resolved_p (add_symbol);
1013 symp->sy_resolving = 0;
1014 goto exit_dont_set_value;
1016 else if (finalize_syms && final_seg == expr_section
1017 && seg_left != expr_section)
1019 /* If the symbol is an expression symbol, do similarly
1020 as for undefined and common syms above. Handles
1021 "sym +/- expr" where "expr" cannot be evaluated
1022 immediately, and we want relocations to be against
1023 "sym", eg. because it is weak. */
1024 symp->sy_value.X_op = O_symbol;
1025 symp->sy_value.X_add_symbol = add_symbol;
1026 symp->sy_value.X_add_number = final_val;
1027 symp->sy_value.X_op_symbol = add_symbol;
1028 final_seg = seg_left;
1029 final_val += symp->sy_frag->fr_address + left;
1030 resolved = symbol_resolved_p (add_symbol);
1031 symp->sy_resolving = 0;
1032 goto exit_dont_set_value;
1036 final_val += symp->sy_frag->fr_address + left;
1037 if (final_seg == expr_section || final_seg == undefined_section)
1038 final_seg = seg_left;
1041 resolved = symbol_resolved_p (add_symbol);
1047 left = resolve_symbol_value (add_symbol);
1048 seg_left = S_GET_SEGMENT (add_symbol);
1050 /* By reducing these to the relevant dyadic operator, we get
1051 !S -> S == 0 permitted on anything,
1052 -S -> 0 - S only permitted on absolute
1053 ~S -> S ^ ~0 only permitted on absolute */
1054 if (op != O_logical_not && seg_left != absolute_section
1056 report_op_error (symp, add_symbol, NULL);
1058 if (final_seg == expr_section || final_seg == undefined_section)
1059 final_seg = absolute_section;
1063 else if (op == O_logical_not)
1068 final_val += left + symp->sy_frag->fr_address;
1070 resolved = symbol_resolved_p (add_symbol);
1078 case O_bit_inclusive_or:
1080 case O_bit_exclusive_or:
1092 left = resolve_symbol_value (add_symbol);
1093 right = resolve_symbol_value (op_symbol);
1094 seg_left = S_GET_SEGMENT (add_symbol);
1095 seg_right = S_GET_SEGMENT (op_symbol);
1097 /* Simplify addition or subtraction of a constant by folding the
1098 constant into X_add_number. */
1101 if (seg_right == absolute_section)
1106 else if (seg_left == absolute_section)
1109 add_symbol = op_symbol;
1111 seg_left = seg_right;
1115 else if (op == O_subtract)
1117 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. */
1132 if (!(seg_left == absolute_section
1133 && seg_right == absolute_section)
1134 && !(op == O_eq || op == O_ne)
1135 && !((op == O_subtract
1136 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1137 && seg_left == seg_right
1138 && (seg_left != undefined_section
1139 || add_symbol == op_symbol)))
1141 /* Don't emit messages unless we're finalizing the symbol value,
1142 otherwise we may get the same message multiple times. */
1144 report_op_error (symp, add_symbol, op_symbol);
1145 /* However do not move the symbol into the absolute section
1146 if it cannot currently be resolved - this would confuse
1147 other parts of the assembler into believing that the
1148 expression had been evaluated to zero. */
1154 && (final_seg == expr_section || final_seg == undefined_section))
1155 final_seg = absolute_section;
1157 /* Check for division by zero. */
1158 if ((op == O_divide || op == O_modulus) && right == 0)
1160 /* If seg_right is not absolute_section, then we've
1161 already issued a warning about using a bad symbol. */
1162 if (seg_right == absolute_section && finalize_syms)
1167 if (expr_symbol_where (symp, &file, &line))
1168 as_bad_where (file, line, _("division by zero"));
1170 as_bad (_("division by zero when setting `%s'"),
1177 switch (symp->sy_value.X_op)
1179 case O_multiply: left *= right; break;
1180 case O_divide: left /= right; break;
1181 case O_modulus: left %= right; break;
1182 case O_left_shift: left <<= right; break;
1183 case O_right_shift: left >>= right; break;
1184 case O_bit_inclusive_or: left |= right; break;
1185 case O_bit_or_not: left |= ~right; break;
1186 case O_bit_exclusive_or: left ^= right; break;
1187 case O_bit_and: left &= right; break;
1188 case O_add: left += right; break;
1189 case O_subtract: left -= right; break;
1192 left = (left == right && seg_left == seg_right
1193 && (seg_left != undefined_section
1194 || add_symbol == op_symbol)
1195 ? ~ (offsetT) 0 : 0);
1196 if (symp->sy_value.X_op == O_ne)
1199 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1200 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1201 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1202 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1203 case O_logical_and: left = left && right; break;
1204 case O_logical_or: left = left || right; break;
1208 final_val += symp->sy_frag->fr_address + left;
1209 if (final_seg == expr_section || final_seg == undefined_section)
1211 if (seg_left == undefined_section
1212 || seg_right == undefined_section)
1213 final_seg = undefined_section;
1214 else if (seg_left == absolute_section)
1215 final_seg = seg_right;
1217 final_seg = seg_left;
1219 resolved = (symbol_resolved_p (add_symbol)
1220 && symbol_resolved_p (op_symbol));
1226 /* Give an error (below) if not in expr_section. We don't
1227 want to worry about expr_section symbols, because they
1228 are fictional (they are created as part of expression
1229 resolution), and any problems may not actually mean
1234 symp->sy_resolving = 0;
1238 S_SET_VALUE (symp, final_val);
1240 exit_dont_set_value:
1241 /* Always set the segment, even if not finalizing the value.
1242 The segment is used to determine whether a symbol is defined. */
1243 S_SET_SEGMENT (symp, final_seg);
1245 /* Don't worry if we can't resolve an expr_section symbol. */
1249 symp->sy_resolved = 1;
1250 else if (S_GET_SEGMENT (symp) != expr_section)
1252 as_bad (_("can't resolve value for symbol `%s'"),
1254 symp->sy_resolved = 1;
1261 static void resolve_local_symbol (const char *, PTR);
1263 /* A static function passed to hash_traverse. */
1266 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, PTR value)
1269 resolve_symbol_value (value);
1272 /* Resolve all local symbols. */
1275 resolve_local_symbol_values (void)
1277 hash_traverse (local_hash, resolve_local_symbol);
1280 /* Obtain the current value of a symbol without changing any
1281 sub-expressions used. */
1284 snapshot_symbol (symbolS *symbolP, valueT *valueP, segT *segP, fragS **fragPP)
1286 if (LOCAL_SYMBOL_CHECK (symbolP))
1288 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1290 *valueP = locsym->lsy_value;
1291 *segP = locsym->lsy_section;
1292 *fragPP = local_symbol_get_frag (locsym);
1296 expressionS expr = symbolP->sy_value;
1298 if (!symbolP->sy_resolved && expr.X_op != O_illegal)
1302 if (symbolP->sy_resolving)
1304 symbolP->sy_resolving = 1;
1305 resolved = resolve_expression (&expr);
1306 symbolP->sy_resolving = 0;
1314 /* This check wouldn't be needed if pseudo_set() didn't set
1315 symbols equated to bare symbols to undefined_section. */
1316 if (symbolP->bsym->section != undefined_section
1317 || symbolP->sy_value.X_op != O_symbol)
1322 symbolP = expr.X_add_symbol;
1329 *valueP = expr.X_add_number;
1330 *segP = symbolP->bsym->section;
1331 *fragPP = symbolP->sy_frag;
1333 if (*segP == expr_section)
1336 case O_constant: *segP = absolute_section; break;
1337 case O_register: *segP = reg_section; break;
1345 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1346 They are *really* local. That is, they go out of scope whenever we see a
1347 label that isn't local. Also, like fb labels, there can be multiple
1348 instances of a dollar label. Therefor, we name encode each instance with
1349 the instance number, keep a list of defined symbols separate from the real
1350 symbol table, and we treat these buggers as a sparse array. */
1352 static long *dollar_labels;
1353 static long *dollar_label_instances;
1354 static char *dollar_label_defines;
1355 static unsigned long dollar_label_count;
1356 static unsigned long dollar_label_max;
1359 dollar_label_defined (long label)
1363 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1365 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1367 return dollar_label_defines[i - dollar_labels];
1369 /* If we get here, label isn't defined. */
1374 dollar_label_instance (long label)
1378 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1380 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1382 return (dollar_label_instances[i - dollar_labels]);
1384 /* If we get here, we haven't seen the label before.
1385 Therefore its instance count is zero. */
1390 dollar_label_clear (void)
1392 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1395 #define DOLLAR_LABEL_BUMP_BY 10
1398 define_dollar_label (long label)
1402 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1405 ++dollar_label_instances[i - dollar_labels];
1406 dollar_label_defines[i - dollar_labels] = 1;
1410 /* If we get to here, we don't have label listed yet. */
1412 if (dollar_labels == NULL)
1414 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1415 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1416 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1417 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1418 dollar_label_count = 0;
1420 else if (dollar_label_count == dollar_label_max)
1422 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1423 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1424 dollar_label_max * sizeof (long));
1425 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1426 dollar_label_max * sizeof (long));
1427 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1428 } /* if we needed to grow */
1430 dollar_labels[dollar_label_count] = label;
1431 dollar_label_instances[dollar_label_count] = 1;
1432 dollar_label_defines[dollar_label_count] = 1;
1433 ++dollar_label_count;
1436 /* Caller must copy returned name: we re-use the area for the next name.
1438 The mth occurence of label n: is turned into the symbol "Ln^Am"
1439 where n is the label number and m is the instance number. "L" makes
1440 it a label discarded unless debugging and "^A"('\1') ensures no
1441 ordinary symbol SHOULD get the same name as a local label
1442 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1444 fb labels get the same treatment, except that ^B is used in place
1447 char * /* Return local label name. */
1448 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1449 register int augend /* 0 for current instance, 1 for new instance. */)
1452 /* Returned to caller, then copied. Used for created names ("4f"). */
1453 static char symbol_name_build[24];
1456 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1459 know (augend == 0 || augend == 1);
1460 p = symbol_name_build;
1461 #ifdef LOCAL_LABEL_PREFIX
1462 *p++ = LOCAL_LABEL_PREFIX;
1466 /* Next code just does sprintf( {}, "%d", n); */
1468 q = symbol_name_temporary;
1469 for (*q++ = 0, i = n; i; ++q)
1474 while ((*p = *--q) != '\0')
1477 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1479 /* Instance number. */
1480 q = symbol_name_temporary;
1481 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1486 while ((*p++ = *--q) != '\0');;
1488 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1489 return symbol_name_build;
1492 /* Somebody else's idea of local labels. They are made by "n:" where n
1493 is any decimal digit. Refer to them with
1494 "nb" for previous (backward) n:
1495 or "nf" for next (forward) n:.
1497 We do a little better and let n be any number, not just a single digit, but
1498 since the other guy's assembler only does ten, we treat the first ten
1501 Like someone else's assembler, we have one set of local label counters for
1502 entire assembly, not one set per (sub)segment like in most assemblers. This
1503 implies that one can refer to a label in another segment, and indeed some
1504 crufty compilers have done just that.
1506 Since there could be a LOT of these things, treat them as a sparse
1509 #define FB_LABEL_SPECIAL (10)
1511 static long fb_low_counter[FB_LABEL_SPECIAL];
1512 static long *fb_labels;
1513 static long *fb_label_instances;
1514 static long fb_label_count;
1515 static long fb_label_max;
1517 /* This must be more than FB_LABEL_SPECIAL. */
1518 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1521 fb_label_init (void)
1523 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1526 /* Add one to the instance number of this fb label. */
1529 fb_label_instance_inc (long label)
1533 if (label < FB_LABEL_SPECIAL)
1535 ++fb_low_counter[label];
1539 if (fb_labels != NULL)
1541 for (i = fb_labels + FB_LABEL_SPECIAL;
1542 i < fb_labels + fb_label_count; ++i)
1546 ++fb_label_instances[i - fb_labels];
1548 } /* if we find it */
1549 } /* for each existing label */
1552 /* If we get to here, we don't have label listed yet. */
1554 if (fb_labels == NULL)
1556 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1557 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1558 fb_label_max = FB_LABEL_BUMP_BY;
1559 fb_label_count = FB_LABEL_SPECIAL;
1562 else if (fb_label_count == fb_label_max)
1564 fb_label_max += FB_LABEL_BUMP_BY;
1565 fb_labels = (long *) xrealloc ((char *) fb_labels,
1566 fb_label_max * sizeof (long));
1567 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1568 fb_label_max * sizeof (long));
1569 } /* if we needed to grow */
1571 fb_labels[fb_label_count] = label;
1572 fb_label_instances[fb_label_count] = 1;
1577 fb_label_instance (long label)
1581 if (label < FB_LABEL_SPECIAL)
1583 return (fb_low_counter[label]);
1586 if (fb_labels != NULL)
1588 for (i = fb_labels + FB_LABEL_SPECIAL;
1589 i < fb_labels + fb_label_count; ++i)
1593 return (fb_label_instances[i - fb_labels]);
1594 } /* if we find it */
1595 } /* for each existing label */
1598 /* We didn't find the label, so this must be a reference to the
1603 /* Caller must copy returned name: we re-use the area for the next name.
1605 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1606 where n is the label number and m is the instance number. "L" makes
1607 it a label discarded unless debugging and "^B"('\2') ensures no
1608 ordinary symbol SHOULD get the same name as a local label
1609 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1611 dollar labels get the same treatment, except that ^A is used in
1614 char * /* Return local label name. */
1615 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1616 long augend /* 0 for nb, 1 for n:, nf. */)
1619 /* Returned to caller, then copied. Used for created names ("4f"). */
1620 static char symbol_name_build[24];
1623 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1627 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1629 know ((unsigned long) augend <= 1);
1631 p = symbol_name_build;
1632 #ifdef LOCAL_LABEL_PREFIX
1633 *p++ = LOCAL_LABEL_PREFIX;
1637 /* Next code just does sprintf( {}, "%d", n); */
1639 q = symbol_name_temporary;
1640 for (*q++ = 0, i = n; i; ++q)
1645 while ((*p = *--q) != '\0')
1648 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1650 /* Instance number. */
1651 q = symbol_name_temporary;
1652 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1657 while ((*p++ = *--q) != '\0');;
1659 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1660 return (symbol_name_build);
1663 /* Decode name that may have been generated by foo_label_name() above.
1664 If the name wasn't generated by foo_label_name(), then return it
1665 unaltered. This is used for error messages. */
1668 decode_local_label_name (char *s)
1671 char *symbol_decode;
1673 int instance_number;
1675 const char *message_format;
1678 #ifdef LOCAL_LABEL_PREFIX
1679 if (s[index] == LOCAL_LABEL_PREFIX)
1683 if (s[index] != 'L')
1686 for (label_number = 0, p = s + index + 1; ISDIGIT (*p); ++p)
1687 label_number = (10 * label_number) + *p - '0';
1689 if (*p == DOLLAR_LABEL_CHAR)
1691 else if (*p == LOCAL_LABEL_CHAR)
1696 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1697 instance_number = (10 * instance_number) + *p - '0';
1699 message_format = _("\"%d\" (instance number %d of a %s label)");
1700 symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30);
1701 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1703 return symbol_decode;
1706 /* Get the value of a symbol. */
1709 S_GET_VALUE (symbolS *s)
1711 if (LOCAL_SYMBOL_CHECK (s))
1712 return resolve_symbol_value (s);
1714 if (!s->sy_resolved)
1716 valueT val = resolve_symbol_value (s);
1720 if (s->sy_value.X_op != O_constant)
1722 static symbolS *recur;
1724 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1725 may call S_GET_VALUE. We use a static symbol to avoid the
1726 immediate recursion. */
1728 return (valueT) s->sy_value.X_add_number;
1730 if (! s->sy_resolved
1731 || s->sy_value.X_op != O_symbol
1732 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1733 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1737 return (valueT) s->sy_value.X_add_number;
1740 /* Set the value of a symbol. */
1743 S_SET_VALUE (symbolS *s, valueT val)
1745 if (LOCAL_SYMBOL_CHECK (s))
1747 ((struct local_symbol *) s)->lsy_value = val;
1751 s->sy_value.X_op = O_constant;
1752 s->sy_value.X_add_number = (offsetT) val;
1753 s->sy_value.X_unsigned = 0;
1757 copy_symbol_attributes (symbolS *dest, symbolS *src)
1759 if (LOCAL_SYMBOL_CHECK (dest))
1760 dest = local_symbol_convert ((struct local_symbol *) dest);
1761 if (LOCAL_SYMBOL_CHECK (src))
1762 src = local_symbol_convert ((struct local_symbol *) src);
1764 /* In an expression, transfer the settings of these flags.
1765 The user can override later, of course. */
1766 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1767 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1769 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1770 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1775 S_IS_FUNCTION (symbolS *s)
1779 if (LOCAL_SYMBOL_CHECK (s))
1782 flags = s->bsym->flags;
1784 return (flags & BSF_FUNCTION) != 0;
1788 S_IS_EXTERNAL (symbolS *s)
1792 if (LOCAL_SYMBOL_CHECK (s))
1795 flags = s->bsym->flags;
1798 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1801 return (flags & BSF_GLOBAL) != 0;
1805 S_IS_WEAK (symbolS *s)
1807 if (LOCAL_SYMBOL_CHECK (s))
1809 return (s->bsym->flags & BSF_WEAK) != 0;
1813 S_IS_COMMON (symbolS *s)
1815 if (LOCAL_SYMBOL_CHECK (s))
1817 return bfd_is_com_section (s->bsym->section);
1821 S_IS_DEFINED (symbolS *s)
1823 if (LOCAL_SYMBOL_CHECK (s))
1824 return ((struct local_symbol *) s)->lsy_section != undefined_section;
1825 return s->bsym->section != undefined_section;
1829 #ifndef EXTERN_FORCE_RELOC
1830 #define EXTERN_FORCE_RELOC IS_ELF
1833 /* Return true for symbols that should not be reduced to section
1834 symbols or eliminated from expressions, because they may be
1835 overridden by the linker. */
1837 S_FORCE_RELOC (symbolS *s, int strict)
1839 if (LOCAL_SYMBOL_CHECK (s))
1840 return ((struct local_symbol *) s)->lsy_section == undefined_section;
1843 && ((s->bsym->flags & BSF_WEAK) != 0
1844 || (EXTERN_FORCE_RELOC
1845 && (s->bsym->flags & BSF_GLOBAL) != 0)))
1846 || s->bsym->section == undefined_section
1847 || bfd_is_com_section (s->bsym->section));
1851 S_IS_DEBUG (symbolS *s)
1853 if (LOCAL_SYMBOL_CHECK (s))
1855 if (s->bsym->flags & BSF_DEBUGGING)
1861 S_IS_LOCAL (symbolS *s)
1866 if (LOCAL_SYMBOL_CHECK (s))
1869 flags = s->bsym->flags;
1872 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1875 if (bfd_get_section (s->bsym) == reg_section)
1878 if (flag_strip_local_absolute
1879 /* Keep BSF_FILE symbols in order to allow debuggers to identify
1880 the source file even when the object file is stripped. */
1881 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
1882 && bfd_get_section (s->bsym) == absolute_section)
1885 name = S_GET_NAME (s);
1886 return (name != NULL
1888 && (strchr (name, DOLLAR_LABEL_CHAR)
1889 || strchr (name, LOCAL_LABEL_CHAR)
1890 || (! flag_keep_locals
1891 && (bfd_is_local_label (stdoutput, s->bsym)
1894 && name[1] == '?')))));
1898 S_IS_STABD (symbolS *s)
1900 return S_GET_NAME (s) == 0;
1904 S_IS_VOLATILE (const symbolS *s)
1906 if (LOCAL_SYMBOL_CHECK (s))
1908 return s->sy_volatile;
1912 S_IS_FORWARD_REF (const symbolS *s)
1914 if (LOCAL_SYMBOL_CHECK (s))
1916 return s->sy_forward_ref;
1920 S_GET_NAME (symbolS *s)
1922 if (LOCAL_SYMBOL_CHECK (s))
1923 return ((struct local_symbol *) s)->lsy_name;
1924 return s->bsym->name;
1928 S_GET_SEGMENT (symbolS *s)
1930 if (LOCAL_SYMBOL_CHECK (s))
1931 return ((struct local_symbol *) s)->lsy_section;
1932 return s->bsym->section;
1936 S_SET_SEGMENT (symbolS *s, segT seg)
1938 /* Don't reassign section symbols. The direct reason is to prevent seg
1939 faults assigning back to const global symbols such as *ABS*, but it
1940 shouldn't happen anyway. */
1942 if (LOCAL_SYMBOL_CHECK (s))
1944 if (seg == reg_section)
1945 s = local_symbol_convert ((struct local_symbol *) s);
1948 ((struct local_symbol *) s)->lsy_section = seg;
1953 if (s->bsym->flags & BSF_SECTION_SYM)
1955 if (s->bsym->section != seg)
1959 s->bsym->section = seg;
1963 S_SET_EXTERNAL (symbolS *s)
1965 if (LOCAL_SYMBOL_CHECK (s))
1966 s = local_symbol_convert ((struct local_symbol *) s);
1967 if ((s->bsym->flags & BSF_WEAK) != 0)
1969 /* Let .weak override .global. */
1972 if (s->bsym->flags & BSF_SECTION_SYM)
1977 /* Do not reassign section symbols. */
1978 as_where (& file, & line);
1979 as_warn_where (file, line,
1980 _("section symbols are already global"));
1983 s->bsym->flags |= BSF_GLOBAL;
1984 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
1987 if (! an_external_name && S_GET_NAME(s)[0] != '.')
1988 an_external_name = S_GET_NAME (s);
1993 S_CLEAR_EXTERNAL (symbolS *s)
1995 if (LOCAL_SYMBOL_CHECK (s))
1997 if ((s->bsym->flags & BSF_WEAK) != 0)
1999 /* Let .weak override. */
2002 s->bsym->flags |= BSF_LOCAL;
2003 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2007 S_SET_WEAK (symbolS *s)
2009 if (LOCAL_SYMBOL_CHECK (s))
2010 s = local_symbol_convert ((struct local_symbol *) s);
2011 s->bsym->flags |= BSF_WEAK;
2012 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2016 S_SET_THREAD_LOCAL (symbolS *s)
2018 if (LOCAL_SYMBOL_CHECK (s))
2019 s = local_symbol_convert ((struct local_symbol *) s);
2020 if (bfd_is_com_section (s->bsym->section)
2021 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2023 s->bsym->flags |= BSF_THREAD_LOCAL;
2024 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2025 as_bad (_("Accessing function `%s' as thread-local object"),
2027 else if (! bfd_is_und_section (s->bsym->section)
2028 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2029 as_bad (_("Accessing `%s' as thread-local object"),
2034 S_SET_NAME (symbolS *s, const char *name)
2036 if (LOCAL_SYMBOL_CHECK (s))
2038 ((struct local_symbol *) s)->lsy_name = name;
2041 s->bsym->name = name;
2045 S_SET_VOLATILE (symbolS *s)
2047 if (LOCAL_SYMBOL_CHECK (s))
2048 s = local_symbol_convert ((struct local_symbol *) s);
2053 S_SET_FORWARD_REF (symbolS *s)
2055 if (LOCAL_SYMBOL_CHECK (s))
2056 s = local_symbol_convert ((struct local_symbol *) s);
2057 s->sy_forward_ref = 1;
2060 /* Return the previous symbol in a chain. */
2063 symbol_previous (symbolS *s)
2065 if (LOCAL_SYMBOL_CHECK (s))
2067 return s->sy_previous;
2070 /* Return the next symbol in a chain. */
2073 symbol_next (symbolS *s)
2075 if (LOCAL_SYMBOL_CHECK (s))
2080 /* Return a pointer to the value of a symbol as an expression. */
2083 symbol_get_value_expression (symbolS *s)
2085 if (LOCAL_SYMBOL_CHECK (s))
2086 s = local_symbol_convert ((struct local_symbol *) s);
2087 return &s->sy_value;
2090 /* Set the value of a symbol to an expression. */
2093 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2095 if (LOCAL_SYMBOL_CHECK (s))
2096 s = local_symbol_convert ((struct local_symbol *) s);
2100 /* Return a pointer to the X_add_number component of a symbol. */
2103 symbol_X_add_number (symbolS *s)
2105 if (LOCAL_SYMBOL_CHECK (s))
2106 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2108 return &s->sy_value.X_add_number;
2111 /* Set the value of SYM to the current position in the current segment. */
2114 symbol_set_value_now (symbolS *sym)
2116 S_SET_SEGMENT (sym, now_seg);
2117 S_SET_VALUE (sym, frag_now_fix ());
2118 symbol_set_frag (sym, frag_now);
2121 /* Set the frag of a symbol. */
2124 symbol_set_frag (symbolS *s, fragS *f)
2126 if (LOCAL_SYMBOL_CHECK (s))
2128 local_symbol_set_frag ((struct local_symbol *) s, f);
2134 /* Return the frag of a symbol. */
2137 symbol_get_frag (symbolS *s)
2139 if (LOCAL_SYMBOL_CHECK (s))
2140 return local_symbol_get_frag ((struct local_symbol *) s);
2144 /* Mark a symbol as having been used. */
2147 symbol_mark_used (symbolS *s)
2149 if (LOCAL_SYMBOL_CHECK (s))
2154 /* Clear the mark of whether a symbol has been used. */
2157 symbol_clear_used (symbolS *s)
2159 if (LOCAL_SYMBOL_CHECK (s))
2160 s = local_symbol_convert ((struct local_symbol *) s);
2164 /* Return whether a symbol has been used. */
2167 symbol_used_p (symbolS *s)
2169 if (LOCAL_SYMBOL_CHECK (s))
2174 /* Mark a symbol as having been used in a reloc. */
2177 symbol_mark_used_in_reloc (symbolS *s)
2179 if (LOCAL_SYMBOL_CHECK (s))
2180 s = local_symbol_convert ((struct local_symbol *) s);
2181 s->sy_used_in_reloc = 1;
2184 /* Clear the mark of whether a symbol has been used in a reloc. */
2187 symbol_clear_used_in_reloc (symbolS *s)
2189 if (LOCAL_SYMBOL_CHECK (s))
2191 s->sy_used_in_reloc = 0;
2194 /* Return whether a symbol has been used in a reloc. */
2197 symbol_used_in_reloc_p (symbolS *s)
2199 if (LOCAL_SYMBOL_CHECK (s))
2201 return s->sy_used_in_reloc;
2204 /* Mark a symbol as an MRI common symbol. */
2207 symbol_mark_mri_common (symbolS *s)
2209 if (LOCAL_SYMBOL_CHECK (s))
2210 s = local_symbol_convert ((struct local_symbol *) s);
2211 s->sy_mri_common = 1;
2214 /* Clear the mark of whether a symbol is an MRI common symbol. */
2217 symbol_clear_mri_common (symbolS *s)
2219 if (LOCAL_SYMBOL_CHECK (s))
2221 s->sy_mri_common = 0;
2224 /* Return whether a symbol is an MRI common symbol. */
2227 symbol_mri_common_p (symbolS *s)
2229 if (LOCAL_SYMBOL_CHECK (s))
2231 return s->sy_mri_common;
2234 /* Mark a symbol as having been written. */
2237 symbol_mark_written (symbolS *s)
2239 if (LOCAL_SYMBOL_CHECK (s))
2244 /* Clear the mark of whether a symbol has been written. */
2247 symbol_clear_written (symbolS *s)
2249 if (LOCAL_SYMBOL_CHECK (s))
2254 /* Return whether a symbol has been written. */
2257 symbol_written_p (symbolS *s)
2259 if (LOCAL_SYMBOL_CHECK (s))
2264 /* Mark a symbol has having been resolved. */
2267 symbol_mark_resolved (symbolS *s)
2269 if (LOCAL_SYMBOL_CHECK (s))
2271 local_symbol_mark_resolved ((struct local_symbol *) s);
2277 /* Return whether a symbol has been resolved. */
2280 symbol_resolved_p (symbolS *s)
2282 if (LOCAL_SYMBOL_CHECK (s))
2283 return local_symbol_resolved_p ((struct local_symbol *) s);
2284 return s->sy_resolved;
2287 /* Return whether a symbol is a section symbol. */
2290 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2292 if (LOCAL_SYMBOL_CHECK (s))
2294 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2297 /* Return whether a symbol is equated to another symbol. */
2300 symbol_equated_p (symbolS *s)
2302 if (LOCAL_SYMBOL_CHECK (s))
2304 return s->sy_value.X_op == O_symbol;
2307 /* Return whether a symbol is equated to another symbol, and should be
2308 treated specially when writing out relocs. */
2311 symbol_equated_reloc_p (symbolS *s)
2313 if (LOCAL_SYMBOL_CHECK (s))
2315 /* X_op_symbol, normally not used for O_symbol, is set by
2316 resolve_symbol_value to flag expression syms that have been
2318 return (s->sy_value.X_op == O_symbol
2319 #if defined (OBJ_COFF) && defined (TE_PE)
2322 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2323 || ! S_IS_DEFINED (s)
2324 || S_IS_COMMON (s)));
2327 /* Return whether a symbol has a constant value. */
2330 symbol_constant_p (symbolS *s)
2332 if (LOCAL_SYMBOL_CHECK (s))
2334 return s->sy_value.X_op == O_constant;
2337 /* Return the BFD symbol for a symbol. */
2340 symbol_get_bfdsym (symbolS *s)
2342 if (LOCAL_SYMBOL_CHECK (s))
2343 s = local_symbol_convert ((struct local_symbol *) s);
2347 /* Set the BFD symbol for a symbol. */
2350 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2352 if (LOCAL_SYMBOL_CHECK (s))
2353 s = local_symbol_convert ((struct local_symbol *) s);
2354 /* Usually, it is harmless to reset a symbol to a BFD section
2355 symbol. For example, obj_elf_change_section sets the BFD symbol
2356 of an old symbol with the newly created section symbol. But when
2357 we have multiple sections with the same name, the newly created
2358 section may have the same name as an old section. We check if the
2359 old symbol has been already marked as a section symbol before
2361 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2363 /* else XXX - What do we do now ? */
2366 #ifdef OBJ_SYMFIELD_TYPE
2368 /* Get a pointer to the object format information for a symbol. */
2371 symbol_get_obj (symbolS *s)
2373 if (LOCAL_SYMBOL_CHECK (s))
2374 s = local_symbol_convert ((struct local_symbol *) s);
2378 /* Set the object format information for a symbol. */
2381 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2383 if (LOCAL_SYMBOL_CHECK (s))
2384 s = local_symbol_convert ((struct local_symbol *) s);
2388 #endif /* OBJ_SYMFIELD_TYPE */
2390 #ifdef TC_SYMFIELD_TYPE
2392 /* Get a pointer to the processor information for a symbol. */
2395 symbol_get_tc (symbolS *s)
2397 if (LOCAL_SYMBOL_CHECK (s))
2398 s = local_symbol_convert ((struct local_symbol *) s);
2402 /* Set the processor information for a symbol. */
2405 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2407 if (LOCAL_SYMBOL_CHECK (s))
2408 s = local_symbol_convert ((struct local_symbol *) s);
2412 #endif /* TC_SYMFIELD_TYPE */
2417 symbol_lastP = NULL;
2418 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2419 sy_hash = hash_new ();
2420 local_hash = hash_new ();
2422 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2423 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2424 abs_symbol.bsym = bfd_abs_section.symbol;
2426 abs_symbol.sy_value.X_op = O_constant;
2427 abs_symbol.sy_frag = &zero_address_frag;
2429 if (LOCAL_LABELS_FB)
2435 /* Maximum indent level.
2436 Available for modification inside a gdb session. */
2437 static int max_indent_level = 8;
2440 print_symbol_value_1 (FILE *file, symbolS *sym)
2442 const char *name = S_GET_NAME (sym);
2443 if (!name || !name[0])
2445 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2447 if (LOCAL_SYMBOL_CHECK (sym))
2449 struct local_symbol *locsym = (struct local_symbol *) sym;
2450 if (local_symbol_get_frag (locsym) != &zero_address_frag
2451 && local_symbol_get_frag (locsym) != NULL)
2452 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2453 if (local_symbol_resolved_p (locsym))
2454 fprintf (file, " resolved");
2455 fprintf (file, " local");
2459 if (sym->sy_frag != &zero_address_frag)
2460 fprintf (file, " frag %lx", (long) sym->sy_frag);
2462 fprintf (file, " written");
2463 if (sym->sy_resolved)
2464 fprintf (file, " resolved");
2465 else if (sym->sy_resolving)
2466 fprintf (file, " resolving");
2467 if (sym->sy_used_in_reloc)
2468 fprintf (file, " used-in-reloc");
2470 fprintf (file, " used");
2471 if (S_IS_LOCAL (sym))
2472 fprintf (file, " local");
2473 if (S_IS_EXTERNAL (sym))
2474 fprintf (file, " extern");
2475 if (S_IS_DEBUG (sym))
2476 fprintf (file, " debug");
2477 if (S_IS_DEFINED (sym))
2478 fprintf (file, " defined");
2480 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2481 if (symbol_resolved_p (sym))
2483 segT s = S_GET_SEGMENT (sym);
2485 if (s != undefined_section
2486 && s != expr_section)
2487 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2489 else if (indent_level < max_indent_level
2490 && S_GET_SEGMENT (sym) != undefined_section)
2493 fprintf (file, "\n%*s<", indent_level * 4, "");
2494 if (LOCAL_SYMBOL_CHECK (sym))
2495 fprintf (file, "constant %lx",
2496 (long) ((struct local_symbol *) sym)->lsy_value);
2498 print_expr_1 (file, &sym->sy_value);
2499 fprintf (file, ">");
2506 print_symbol_value (symbolS *sym)
2509 print_symbol_value_1 (stderr, sym);
2510 fprintf (stderr, "\n");
2514 print_binary (FILE *file, const char *name, expressionS *exp)
2517 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2518 print_symbol_value_1 (file, exp->X_add_symbol);
2519 fprintf (file, ">\n%*s<", indent_level * 4, "");
2520 print_symbol_value_1 (file, exp->X_op_symbol);
2521 fprintf (file, ">");
2526 print_expr_1 (FILE *file, expressionS *exp)
2528 fprintf (file, "expr %lx ", (long) exp);
2532 fprintf (file, "illegal");
2535 fprintf (file, "absent");
2538 fprintf (file, "constant %lx", (long) exp->X_add_number);
2542 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2543 print_symbol_value_1 (file, exp->X_add_symbol);
2544 fprintf (file, ">");
2546 if (exp->X_add_number)
2547 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2548 (long) exp->X_add_number);
2552 fprintf (file, "register #%d", (int) exp->X_add_number);
2555 fprintf (file, "big");
2558 fprintf (file, "uminus -<");
2560 print_symbol_value_1 (file, exp->X_add_symbol);
2561 fprintf (file, ">");
2562 goto maybe_print_addnum;
2564 fprintf (file, "bit_not");
2567 print_binary (file, "multiply", exp);
2570 print_binary (file, "divide", exp);
2573 print_binary (file, "modulus", exp);
2576 print_binary (file, "lshift", exp);
2579 print_binary (file, "rshift", exp);
2581 case O_bit_inclusive_or:
2582 print_binary (file, "bit_ior", exp);
2584 case O_bit_exclusive_or:
2585 print_binary (file, "bit_xor", exp);
2588 print_binary (file, "bit_and", exp);
2591 print_binary (file, "eq", exp);
2594 print_binary (file, "ne", exp);
2597 print_binary (file, "lt", exp);
2600 print_binary (file, "le", exp);
2603 print_binary (file, "ge", exp);
2606 print_binary (file, "gt", exp);
2609 print_binary (file, "logical_and", exp);
2612 print_binary (file, "logical_or", exp);
2616 fprintf (file, "add\n%*s<", indent_level * 4, "");
2617 print_symbol_value_1 (file, exp->X_add_symbol);
2618 fprintf (file, ">\n%*s<", indent_level * 4, "");
2619 print_symbol_value_1 (file, exp->X_op_symbol);
2620 fprintf (file, ">");
2621 goto maybe_print_addnum;
2624 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2625 print_symbol_value_1 (file, exp->X_add_symbol);
2626 fprintf (file, ">\n%*s<", indent_level * 4, "");
2627 print_symbol_value_1 (file, exp->X_op_symbol);
2628 fprintf (file, ">");
2629 goto maybe_print_addnum;
2631 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2638 print_expr (expressionS *exp)
2640 print_expr_1 (stderr, exp);
2641 fprintf (stderr, "\n");
2645 symbol_print_statistics (FILE *file)
2647 hash_print_statistics (file, "symbol table", sy_hash);
2648 hash_print_statistics (file, "mini local symbol table", local_hash);
2649 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2650 local_symbol_count, local_symbol_conversion_count);