1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
27 #include "struc-symbol.h"
30 /* This is non-zero if symbols are case sensitive, which is the
32 int symbols_case_sensitive
= 1;
34 #ifndef WORKING_DOT_WORD
35 extern int new_broken_words
;
38 /* symbol-name => struct symbol pointer */
39 static struct hash_control
*sy_hash
;
41 /* Table of local symbols. */
42 static struct hash_control
*local_hash
;
44 /* Below are commented in "symbols.h". */
45 symbolS
*symbol_rootP
;
46 symbolS
*symbol_lastP
;
51 #define debug_verify_symchain verify_symbol_chain
53 #define debug_verify_symchain(root, last) ((void) 0)
56 #define DOLLAR_LABEL_CHAR '\001'
57 #define LOCAL_LABEL_CHAR '\002'
59 #ifndef TC_LABEL_IS_LOCAL
60 #define TC_LABEL_IS_LOCAL(name) 0
65 /* The name of an external symbol which is
66 used to make weak PE symbol names unique. */
67 const char * an_external_name
;
70 static const char *save_symbol_name (const char *);
71 static void fb_label_init (void);
72 static long dollar_label_instance (long);
73 static long fb_label_instance (long);
75 static void print_binary (FILE *, const char *, expressionS
*);
77 /* Return a pointer to a new symbol. Die if we can't make a new
78 symbol. Fill in the symbol's values. Add symbol to end of symbol
81 This function should be called in the general case of creating a
82 symbol. However, if the output file symbol table has already been
83 set, and you are certain that this symbol won't be wanted in the
84 output file, you can call symbol_create. */
87 symbol_new (const char *name
, segT segment
, valueT valu
, fragS
*frag
)
89 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
91 /* Link to end of symbol chain. */
93 extern int symbol_table_frozen
;
94 if (symbol_table_frozen
)
97 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
102 /* Save a symbol name on a permanent obstack, and convert it according
103 to the object file format. */
106 save_symbol_name (const char *name
)
111 gas_assert (name
!= NULL
);
112 name_length
= strlen (name
) + 1; /* +1 for \0. */
113 obstack_grow (¬es
, name
, name_length
);
114 ret
= (char *) obstack_finish (¬es
);
116 #ifdef tc_canonicalize_symbol_name
117 ret
= tc_canonicalize_symbol_name (ret
);
120 if (! symbols_case_sensitive
)
124 for (s
= ret
; *s
!= '\0'; s
++)
132 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
133 segT segment
, /* Segment identifier (SEG_<something>). */
134 valueT valu
, /* Symbol value. */
135 fragS
*frag
/* Associated fragment. */)
137 const char *preserved_copy_of_name
;
140 preserved_copy_of_name
= save_symbol_name (name
);
142 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
144 /* symbol must be born in some fixed state. This seems as good as any. */
145 memset (symbolP
, 0, sizeof (symbolS
));
147 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
148 if (symbolP
->bsym
== NULL
)
149 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
150 S_SET_NAME (symbolP
, preserved_copy_of_name
);
152 S_SET_SEGMENT (symbolP
, segment
);
153 S_SET_VALUE (symbolP
, valu
);
154 symbol_clear_list_pointers (symbolP
);
156 symbolP
->sy_frag
= frag
;
158 obj_symbol_new_hook (symbolP
);
160 #ifdef tc_symbol_new_hook
161 tc_symbol_new_hook (symbolP
);
168 /* Local symbol support. If we can get away with it, we keep only a
169 small amount of information for local symbols. */
171 static symbolS
*local_symbol_convert (struct local_symbol
*);
173 /* Used for statistics. */
175 static unsigned long local_symbol_count
;
176 static unsigned long local_symbol_conversion_count
;
178 /* This macro is called with a symbol argument passed by reference.
179 It returns whether this is a local symbol. If necessary, it
180 changes its argument to the real symbol. */
182 #define LOCAL_SYMBOL_CHECK(s) \
183 (s->sy_flags.sy_local_symbol \
184 ? (local_symbol_converted_p ((struct local_symbol *) s) \
185 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
190 /* Create a local symbol and insert it into the local hash table. */
192 struct local_symbol
*
193 local_symbol_make (const char *name
, segT section
, valueT val
, fragS
*frag
)
195 const char *name_copy
;
196 struct local_symbol
*ret
;
198 ++local_symbol_count
;
200 name_copy
= save_symbol_name (name
);
202 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
203 ret
->lsy_flags
.sy_local_symbol
= 1;
204 ret
->lsy_flags
.sy_resolved
= 0;
205 ret
->lsy_name
= name_copy
;
206 ret
->lsy_section
= section
;
207 local_symbol_set_frag (ret
, frag
);
208 ret
->lsy_value
= val
;
210 hash_jam (local_hash
, name_copy
, (void *) ret
);
215 /* Convert a local symbol into a real symbol. Note that we do not
216 reclaim the space used by the local symbol. */
219 local_symbol_convert (struct local_symbol
*locsym
)
223 gas_assert (locsym
->lsy_flags
.sy_local_symbol
);
224 if (local_symbol_converted_p (locsym
))
225 return local_symbol_get_real_symbol (locsym
);
227 ++local_symbol_conversion_count
;
229 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
230 local_symbol_get_frag (locsym
));
232 if (local_symbol_resolved_p (locsym
))
233 ret
->sy_flags
.sy_resolved
= 1;
235 /* Local symbols are always either defined or used. */
236 ret
->sy_flags
.sy_used
= 1;
238 #ifdef TC_LOCAL_SYMFIELD_CONVERT
239 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
242 symbol_table_insert (ret
);
244 local_symbol_mark_converted (locsym
);
245 local_symbol_set_real_symbol (locsym
, ret
);
247 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
253 define_sym_at_dot (symbolS
*symbolP
)
255 symbolP
->sy_frag
= frag_now
;
256 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
257 S_SET_SEGMENT (symbolP
, now_seg
);
260 /* We have just seen "<name>:".
261 Creates a struct symbol unless it already exists.
263 Gripes if we are redefining a symbol incompatibly (and ignores it). */
266 colon (/* Just seen "x:" - rattle symbols & frags. */
267 const char *sym_name
/* Symbol name, as a canonical string. */
268 /* We copy this string: OK to alter later. */)
270 symbolS
*symbolP
; /* Symbol we are working with. */
272 /* Sun local labels go out of scope whenever a non-local symbol is
274 if (LOCAL_LABELS_DOLLAR
275 && !bfd_is_local_label_name (stdoutput
, sym_name
))
276 dollar_label_clear ();
278 #ifndef WORKING_DOT_WORD
279 if (new_broken_words
)
281 struct broken_word
*a
;
286 if (now_seg
== absolute_section
)
288 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
292 possible_bytes
= (md_short_jump_size
293 + new_broken_words
* md_long_jump_size
);
296 frag_opcode
= frag_var (rs_broken_word
,
300 (symbolS
*) broken_words
,
304 /* We want to store the pointer to where to insert the jump
305 table in the fr_opcode of the rs_broken_word frag. This
306 requires a little hackery. */
308 && (frag_tmp
->fr_type
!= rs_broken_word
309 || frag_tmp
->fr_opcode
))
310 frag_tmp
= frag_tmp
->fr_next
;
312 frag_tmp
->fr_opcode
= frag_opcode
;
313 new_broken_words
= 0;
315 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
316 a
->dispfrag
= frag_tmp
;
318 #endif /* WORKING_DOT_WORD */
320 #ifdef obj_frob_colon
321 obj_frob_colon (sym_name
);
324 if ((symbolP
= symbol_find (sym_name
)) != 0)
326 S_CLEAR_WEAKREFR (symbolP
);
327 #ifdef RESOLVE_SYMBOL_REDEFINITION
328 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
331 /* Now check for undefined symbols. */
332 if (LOCAL_SYMBOL_CHECK (symbolP
))
334 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
336 if (locsym
->lsy_section
!= undefined_section
337 && (local_symbol_get_frag (locsym
) != frag_now
338 || locsym
->lsy_section
!= now_seg
339 || locsym
->lsy_value
!= frag_now_fix ()))
341 as_bad (_("symbol `%s' is already defined"), sym_name
);
345 locsym
->lsy_section
= now_seg
;
346 local_symbol_set_frag (locsym
, frag_now
);
347 locsym
->lsy_value
= frag_now_fix ();
349 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
350 || S_IS_COMMON (symbolP
)
351 || S_IS_VOLATILE (symbolP
))
353 if (S_IS_VOLATILE (symbolP
))
355 symbolP
= symbol_clone (symbolP
, 1);
356 S_SET_VALUE (symbolP
, 0);
357 S_CLEAR_VOLATILE (symbolP
);
359 if (S_GET_VALUE (symbolP
) == 0)
361 define_sym_at_dot (symbolP
);
364 #endif /* if we have one, it better be zero. */
369 /* There are still several cases to check:
371 A .comm/.lcomm symbol being redefined as initialized
374 A .comm/.lcomm symbol being redefined with a larger
377 This only used to be allowed on VMS gas, but Sun cc
378 on the sparc also depends on it. */
380 if (((!S_IS_DEBUG (symbolP
)
381 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
382 && S_IS_EXTERNAL (symbolP
))
383 || S_GET_SEGMENT (symbolP
) == bss_section
)
384 && (now_seg
== data_section
385 || now_seg
== bss_section
386 || now_seg
== S_GET_SEGMENT (symbolP
)))
388 /* Select which of the 2 cases this is. */
389 if (now_seg
!= data_section
)
391 /* New .comm for prev .comm symbol.
393 If the new size is larger we just change its
394 value. If the new size is smaller, we ignore
396 if (S_GET_VALUE (symbolP
)
397 < ((unsigned) frag_now_fix ()))
399 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
404 /* It is a .comm/.lcomm being converted to initialized
406 define_sym_at_dot (symbolP
);
411 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
412 static const char *od_buf
= "";
416 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
417 sprintf (od_buf
, "%d.%d.",
418 S_GET_OTHER (symbolP
),
419 S_GET_DESC (symbolP
));
421 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
423 segment_name (S_GET_SEGMENT (symbolP
)),
425 (long) S_GET_VALUE (symbolP
));
427 } /* if the undefined symbol has no value */
431 /* Don't blow up if the definition is the same. */
432 if (!(frag_now
== symbolP
->sy_frag
433 && S_GET_VALUE (symbolP
) == frag_now_fix ()
434 && S_GET_SEGMENT (symbolP
) == now_seg
))
436 as_bad (_("symbol `%s' is already defined"), sym_name
);
437 symbolP
= symbol_clone (symbolP
, 0);
438 define_sym_at_dot (symbolP
);
443 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
445 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
446 (valueT
) frag_now_fix (),
451 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
454 symbol_table_insert (symbolP
);
457 if (mri_common_symbol
!= NULL
)
459 /* This symbol is actually being defined within an MRI common
460 section. This requires special handling. */
461 if (LOCAL_SYMBOL_CHECK (symbolP
))
462 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
463 symbolP
->sy_value
.X_op
= O_symbol
;
464 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
465 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
466 symbolP
->sy_frag
= &zero_address_frag
;
467 S_SET_SEGMENT (symbolP
, expr_section
);
468 symbolP
->sy_flags
.sy_mri_common
= 1;
472 tc_frob_label (symbolP
);
474 #ifdef obj_frob_label
475 obj_frob_label (symbolP
);
481 /* Die if we can't insert the symbol. */
484 symbol_table_insert (symbolS
*symbolP
)
486 const char *error_string
;
489 know (S_GET_NAME (symbolP
));
491 if (LOCAL_SYMBOL_CHECK (symbolP
))
493 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
495 if (error_string
!= NULL
)
496 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
497 S_GET_NAME (symbolP
), error_string
);
501 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (void *) symbolP
)))
503 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
504 S_GET_NAME (symbolP
), error_string
);
508 /* If a symbol name does not exist, create it as undefined, and insert
509 it into the symbol table. Return a pointer to it. */
512 symbol_find_or_make (const char *name
)
516 symbolP
= symbol_find (name
);
520 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
522 symbolP
= md_undefined_symbol ((char *) name
);
526 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
532 symbolP
= symbol_make (name
);
534 symbol_table_insert (symbolP
);
535 } /* if symbol wasn't found */
541 symbol_make (const char *name
)
545 /* Let the machine description default it, e.g. for register names. */
546 symbolP
= md_undefined_symbol ((char *) name
);
549 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
555 symbol_clone (symbolS
*orgsymP
, int replace
)
558 asymbol
*bsymorg
, *bsymnew
;
560 /* Make sure we never clone the dot special symbol. */
561 gas_assert (orgsymP
!= &dot_symbol
);
563 /* Running local_symbol_convert on a clone that's not the one currently
564 in local_hash would incorrectly replace the hash entry. Thus the
565 symbol must be converted here. Note that the rest of the function
566 depends on not encountering an unconverted symbol. */
567 if (LOCAL_SYMBOL_CHECK (orgsymP
))
568 orgsymP
= local_symbol_convert ((struct local_symbol
*) orgsymP
);
569 bsymorg
= orgsymP
->bsym
;
571 newsymP
= (symbolS
*) obstack_alloc (¬es
, sizeof (*newsymP
));
573 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
575 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
576 newsymP
->bsym
= bsymnew
;
577 bsymnew
->name
= bsymorg
->name
;
578 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
579 bsymnew
->section
= bsymorg
->section
;
580 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
581 bfd_asymbol_bfd (bsymnew
), bsymnew
);
583 #ifdef obj_symbol_clone_hook
584 obj_symbol_clone_hook (newsymP
, orgsymP
);
587 #ifdef tc_symbol_clone_hook
588 tc_symbol_clone_hook (newsymP
, orgsymP
);
593 if (symbol_rootP
== orgsymP
)
594 symbol_rootP
= newsymP
;
595 else if (orgsymP
->sy_previous
)
597 orgsymP
->sy_previous
->sy_next
= newsymP
;
598 orgsymP
->sy_previous
= NULL
;
600 if (symbol_lastP
== orgsymP
)
601 symbol_lastP
= newsymP
;
602 else if (orgsymP
->sy_next
)
603 orgsymP
->sy_next
->sy_previous
= newsymP
;
605 /* Symbols that won't be output can't be external. */
606 S_CLEAR_EXTERNAL (orgsymP
);
607 orgsymP
->sy_previous
= orgsymP
->sy_next
= orgsymP
;
608 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
610 symbol_table_insert (newsymP
);
614 /* Symbols that won't be output can't be external. */
615 S_CLEAR_EXTERNAL (newsymP
);
616 newsymP
->sy_previous
= newsymP
->sy_next
= newsymP
;
622 /* If S is a local symbol that has been converted, return the
623 converted symbol. Otherwise return S. */
625 static inline symbolS
*
626 get_real_sym (symbolS
*s
)
629 && s
->sy_flags
.sy_local_symbol
630 && local_symbol_converted_p ((struct local_symbol
*) s
))
631 s
= local_symbol_get_real_symbol ((struct local_symbol
*) s
);
635 /* Referenced symbols, if they are forward references, need to be cloned
636 (without replacing the original) so that the value of the referenced
637 symbols at the point of use is saved by the clone. */
639 #undef symbol_clone_if_forward_ref
641 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
643 if (symbolP
&& !LOCAL_SYMBOL_CHECK (symbolP
))
645 symbolS
*orig_add_symbol
= get_real_sym (symbolP
->sy_value
.X_add_symbol
);
646 symbolS
*orig_op_symbol
= get_real_sym (symbolP
->sy_value
.X_op_symbol
);
647 symbolS
*add_symbol
= orig_add_symbol
;
648 symbolS
*op_symbol
= orig_op_symbol
;
650 if (symbolP
->sy_flags
.sy_forward_ref
)
655 /* assign_symbol() clones volatile symbols; pre-existing expressions
656 hold references to the original instance, but want the current
657 value. Just repeat the lookup. */
658 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
659 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
660 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
661 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
664 /* Re-using sy_resolving here, as this routine cannot get called from
665 symbol resolution code. */
666 if ((symbolP
->bsym
->section
== expr_section
667 || symbolP
->sy_flags
.sy_forward_ref
)
668 && !symbolP
->sy_flags
.sy_resolving
)
670 symbolP
->sy_flags
.sy_resolving
= 1;
671 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
672 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
673 symbolP
->sy_flags
.sy_resolving
= 0;
676 if (symbolP
->sy_flags
.sy_forward_ref
677 || add_symbol
!= orig_add_symbol
678 || op_symbol
!= orig_op_symbol
)
680 if (symbolP
!= &dot_symbol
)
682 symbolP
= symbol_clone (symbolP
, 0);
683 symbolP
->sy_flags
.sy_resolving
= 0;
687 symbolP
= symbol_temp_new_now ();
688 #ifdef tc_new_dot_label
689 tc_new_dot_label (symbolP
);
694 symbolP
->sy_value
.X_add_symbol
= add_symbol
;
695 symbolP
->sy_value
.X_op_symbol
= op_symbol
;
702 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
704 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
708 symbol_temp_new_now (void)
710 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
714 symbol_temp_make (void)
716 return symbol_make (FAKE_LABEL_NAME
);
719 /* Implement symbol table lookup.
720 In: A symbol's name as a string: '\0' can't be part of a symbol name.
721 Out: NULL if the name was not in the symbol table, else the address
722 of a struct symbol associated with that name. */
725 symbol_find_exact (const char *name
)
727 return symbol_find_exact_noref (name
, 0);
731 symbol_find_exact_noref (const char *name
, int noref
)
733 struct local_symbol
*locsym
;
736 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
738 return (symbolS
*) locsym
;
740 sym
= ((symbolS
*) hash_find (sy_hash
, name
));
742 /* Any references to the symbol, except for the reference in
743 .weakref, must clear this flag, such that the symbol does not
744 turn into a weak symbol. Note that we don't have to handle the
745 local_symbol case, since a weakrefd is always promoted out of the
746 local_symbol table when it is turned into a weak symbol. */
748 S_CLEAR_WEAKREFD (sym
);
754 symbol_find (const char *name
)
756 return symbol_find_noref (name
, 0);
760 symbol_find_noref (const char *name
, int noref
)
765 #ifdef tc_canonicalize_symbol_name
767 copy
= xstrdup (name
);
768 name
= tc_canonicalize_symbol_name (copy
);
772 if (! symbols_case_sensitive
)
781 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
783 while ((c
= *orig
++) != '\0')
784 *copy
++ = TOUPPER (c
);
789 copy
= (char *) name
;
792 result
= symbol_find_exact_noref (name
, noref
);
798 /* Once upon a time, symbols were kept in a singly linked list. At
799 least coff needs to be able to rearrange them from time to time, for
800 which a doubly linked list is much more convenient. Loic did these
801 as macros which seemed dangerous to me so they're now functions.
804 /* Link symbol ADDME after symbol TARGET in the chain. */
807 symbol_append (symbolS
*addme
, symbolS
*target
,
808 symbolS
**rootPP
, symbolS
**lastPP
)
810 if (LOCAL_SYMBOL_CHECK (addme
))
812 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
817 know (*rootPP
== NULL
);
818 know (*lastPP
== NULL
);
819 addme
->sy_next
= NULL
;
820 addme
->sy_previous
= NULL
;
824 } /* if the list is empty */
826 if (target
->sy_next
!= NULL
)
828 target
->sy_next
->sy_previous
= addme
;
832 know (*lastPP
== target
);
834 } /* if we have a next */
836 addme
->sy_next
= target
->sy_next
;
837 target
->sy_next
= addme
;
838 addme
->sy_previous
= target
;
840 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
843 /* Set the chain pointers of SYMBOL to null. */
846 symbol_clear_list_pointers (symbolS
*symbolP
)
848 if (LOCAL_SYMBOL_CHECK (symbolP
))
850 symbolP
->sy_next
= NULL
;
851 symbolP
->sy_previous
= NULL
;
854 /* Remove SYMBOLP from the list. */
857 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
859 if (LOCAL_SYMBOL_CHECK (symbolP
))
862 if (symbolP
== *rootPP
)
864 *rootPP
= symbolP
->sy_next
;
865 } /* if it was the root */
867 if (symbolP
== *lastPP
)
869 *lastPP
= symbolP
->sy_previous
;
870 } /* if it was the tail */
872 if (symbolP
->sy_next
!= NULL
)
874 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
877 if (symbolP
->sy_previous
!= NULL
)
879 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
882 debug_verify_symchain (*rootPP
, *lastPP
);
885 /* Link symbol ADDME before symbol TARGET in the chain. */
888 symbol_insert (symbolS
*addme
, symbolS
*target
,
889 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
891 if (LOCAL_SYMBOL_CHECK (addme
))
893 if (LOCAL_SYMBOL_CHECK (target
))
896 if (target
->sy_previous
!= NULL
)
898 target
->sy_previous
->sy_next
= addme
;
902 know (*rootPP
== target
);
906 addme
->sy_previous
= target
->sy_previous
;
907 target
->sy_previous
= addme
;
908 addme
->sy_next
= target
;
910 debug_verify_symchain (*rootPP
, *lastPP
);
914 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
916 symbolS
*symbolP
= rootP
;
921 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
923 gas_assert (symbolP
->bsym
!= NULL
);
924 gas_assert (symbolP
->sy_flags
.sy_local_symbol
== 0);
925 gas_assert (symbolP
->sy_next
->sy_previous
== symbolP
);
928 gas_assert (lastP
== symbolP
);
931 #ifdef OBJ_COMPLEX_RELC
934 use_complex_relocs_for (symbolS
* symp
)
936 switch (symp
->sy_value
.X_op
)
946 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
947 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
949 (S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
950 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
))
959 case O_bit_inclusive_or
:
961 case O_bit_exclusive_or
:
974 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
975 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
977 (S_IS_COMMON (symp
->sy_value
.X_op_symbol
)
978 || S_IS_LOCAL (symp
->sy_value
.X_op_symbol
))
980 && S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
981 && S_IS_DEFINED (symp
->sy_value
.X_op_symbol
)
982 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
983 && S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) != expr_section
)
995 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
999 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1000 segT seg_right
= S_GET_SEGMENT (right
);
1009 case O_uminus
: opname
= "-"; break;
1010 case O_bit_not
: opname
= "~"; break;
1011 case O_logical_not
: opname
= "!"; break;
1012 case O_multiply
: opname
= "*"; break;
1013 case O_divide
: opname
= "/"; break;
1014 case O_modulus
: opname
= "%"; break;
1015 case O_left_shift
: opname
= "<<"; break;
1016 case O_right_shift
: opname
= ">>"; break;
1017 case O_bit_inclusive_or
: opname
= "|"; break;
1018 case O_bit_or_not
: opname
= "|~"; break;
1019 case O_bit_exclusive_or
: opname
= "^"; break;
1020 case O_bit_and
: opname
= "&"; break;
1021 case O_add
: opname
= "+"; break;
1022 case O_subtract
: opname
= "-"; break;
1023 case O_eq
: opname
= "=="; break;
1024 case O_ne
: opname
= "!="; break;
1025 case O_lt
: opname
= "<"; break;
1026 case O_le
: opname
= "<="; break;
1027 case O_ge
: opname
= ">="; break;
1028 case O_gt
: opname
= ">"; break;
1029 case O_logical_and
: opname
= "&&"; break;
1030 case O_logical_or
: opname
= "||"; break;
1033 if (expr_symbol_where (symp
, &file
, &line
))
1036 as_bad_where (file
, line
,
1037 _("invalid operands (%s and %s sections) for `%s'"),
1038 seg_left
->name
, seg_right
->name
, opname
);
1040 as_bad_where (file
, line
,
1041 _("invalid operand (%s section) for `%s'"),
1042 seg_right
->name
, opname
);
1046 const char *sname
= S_GET_NAME (symp
);
1049 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1050 seg_left
->name
, seg_right
->name
, opname
, sname
);
1052 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1053 seg_right
->name
, opname
, sname
);
1057 /* Resolve the value of a symbol. This is called during the final
1058 pass over the symbol table to resolve any symbols with complex
1062 resolve_symbol_value (symbolS
*symp
)
1065 valueT final_val
= 0;
1068 if (LOCAL_SYMBOL_CHECK (symp
))
1070 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1072 final_val
= locsym
->lsy_value
;
1073 if (local_symbol_resolved_p (locsym
))
1076 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
1080 locsym
->lsy_value
= final_val
;
1081 local_symbol_mark_resolved (locsym
);
1087 if (symp
->sy_flags
.sy_resolved
)
1089 if (symp
->sy_value
.X_op
== O_constant
)
1090 return (valueT
) symp
->sy_value
.X_add_number
;
1096 final_seg
= S_GET_SEGMENT (symp
);
1098 if (symp
->sy_flags
.sy_resolving
)
1101 as_bad (_("symbol definition loop encountered at `%s'"),
1106 #ifdef OBJ_COMPLEX_RELC
1107 else if (final_seg
== expr_section
1108 && use_complex_relocs_for (symp
))
1110 symbolS
* relc_symbol
= NULL
;
1111 char * relc_symbol_name
= NULL
;
1113 relc_symbol_name
= symbol_relc_make_expr (& symp
->sy_value
);
1115 /* For debugging, print out conversion input & output. */
1117 print_expr (& symp
->sy_value
);
1118 if (relc_symbol_name
)
1119 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1122 if (relc_symbol_name
!= NULL
)
1123 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1124 0, & zero_address_frag
);
1126 if (relc_symbol
== NULL
)
1128 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1134 symbol_table_insert (relc_symbol
);
1136 /* S_CLEAR_EXTERNAL (relc_symbol); */
1137 if (symp
->bsym
->flags
& BSF_SRELC
)
1138 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1140 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1141 /* symp->bsym->flags |= BSF_RELC; */
1142 copy_symbol_attributes (symp
, relc_symbol
);
1143 symp
->sy_value
.X_op
= O_symbol
;
1144 symp
->sy_value
.X_add_symbol
= relc_symbol
;
1145 symp
->sy_value
.X_add_number
= 0;
1149 final_seg
= undefined_section
;
1150 goto exit_dont_set_value
;
1155 symbolS
*add_symbol
, *op_symbol
;
1156 offsetT left
, right
;
1157 segT seg_left
, seg_right
;
1161 symp
->sy_flags
.sy_resolving
= 1;
1163 /* Help out with CSE. */
1164 add_symbol
= symp
->sy_value
.X_add_symbol
;
1165 op_symbol
= symp
->sy_value
.X_op_symbol
;
1166 final_val
= symp
->sy_value
.X_add_number
;
1167 op
= symp
->sy_value
.X_op
;
1180 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
1181 if (final_seg
== expr_section
)
1182 final_seg
= absolute_section
;
1191 left
= resolve_symbol_value (add_symbol
);
1192 seg_left
= S_GET_SEGMENT (add_symbol
);
1194 symp
->sy_value
.X_op_symbol
= NULL
;
1197 if (S_IS_WEAKREFR (symp
))
1199 gas_assert (final_val
== 0);
1200 if (S_IS_WEAKREFR (add_symbol
))
1202 gas_assert (add_symbol
->sy_value
.X_op
== O_symbol
1203 && add_symbol
->sy_value
.X_add_number
== 0);
1204 add_symbol
= add_symbol
->sy_value
.X_add_symbol
;
1205 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1206 symp
->sy_value
.X_add_symbol
= add_symbol
;
1210 if (symp
->sy_flags
.sy_mri_common
)
1212 /* This is a symbol inside an MRI common section. The
1213 relocation routines are going to handle it specially.
1214 Don't change the value. */
1215 resolved
= symbol_resolved_p (add_symbol
);
1219 if (finalize_syms
&& final_val
== 0)
1221 if (LOCAL_SYMBOL_CHECK (add_symbol
))
1222 add_symbol
= local_symbol_convert ((struct local_symbol
*)
1224 copy_symbol_attributes (symp
, add_symbol
);
1227 /* If we have equated this symbol to an undefined or common
1228 symbol, keep X_op set to O_symbol, and don't change
1229 X_add_number. This permits the routine which writes out
1230 relocation to detect this case, and convert the
1231 relocation to be against the symbol to which this symbol
1233 if (! S_IS_DEFINED (add_symbol
)
1234 #if defined (OBJ_COFF) && defined (TE_PE)
1235 || S_IS_WEAK (add_symbol
)
1237 || S_IS_COMMON (add_symbol
))
1241 symp
->sy_value
.X_op
= O_symbol
;
1242 symp
->sy_value
.X_add_symbol
= add_symbol
;
1243 symp
->sy_value
.X_add_number
= final_val
;
1244 /* Use X_op_symbol as a flag. */
1245 symp
->sy_value
.X_op_symbol
= add_symbol
;
1247 final_seg
= seg_left
;
1249 resolved
= symbol_resolved_p (add_symbol
);
1250 symp
->sy_flags
.sy_resolving
= 0;
1251 goto exit_dont_set_value
;
1253 else if (finalize_syms
1254 && ((final_seg
== expr_section
&& seg_left
!= expr_section
)
1255 || symbol_shadow_p (symp
)))
1257 /* If the symbol is an expression symbol, do similarly
1258 as for undefined and common syms above. Handles
1259 "sym +/- expr" where "expr" cannot be evaluated
1260 immediately, and we want relocations to be against
1261 "sym", eg. because it is weak. */
1262 symp
->sy_value
.X_op
= O_symbol
;
1263 symp
->sy_value
.X_add_symbol
= add_symbol
;
1264 symp
->sy_value
.X_add_number
= final_val
;
1265 symp
->sy_value
.X_op_symbol
= add_symbol
;
1266 final_seg
= seg_left
;
1267 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1268 resolved
= symbol_resolved_p (add_symbol
);
1269 symp
->sy_flags
.sy_resolving
= 0;
1270 goto exit_dont_set_value
;
1274 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1275 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1276 final_seg
= seg_left
;
1279 resolved
= symbol_resolved_p (add_symbol
);
1280 if (S_IS_WEAKREFR (symp
))
1282 symp
->sy_flags
.sy_resolving
= 0;
1283 goto exit_dont_set_value
;
1290 left
= resolve_symbol_value (add_symbol
);
1291 seg_left
= S_GET_SEGMENT (add_symbol
);
1293 /* By reducing these to the relevant dyadic operator, we get
1294 !S -> S == 0 permitted on anything,
1295 -S -> 0 - S only permitted on absolute
1296 ~S -> S ^ ~0 only permitted on absolute */
1297 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1299 report_op_error (symp
, NULL
, op
, add_symbol
);
1301 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1302 final_seg
= absolute_section
;
1306 else if (op
== O_logical_not
)
1311 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1313 resolved
= symbol_resolved_p (add_symbol
);
1321 case O_bit_inclusive_or
:
1323 case O_bit_exclusive_or
:
1335 left
= resolve_symbol_value (add_symbol
);
1336 right
= resolve_symbol_value (op_symbol
);
1337 seg_left
= S_GET_SEGMENT (add_symbol
);
1338 seg_right
= S_GET_SEGMENT (op_symbol
);
1340 /* Simplify addition or subtraction of a constant by folding the
1341 constant into X_add_number. */
1344 if (seg_right
== absolute_section
)
1349 else if (seg_left
== absolute_section
)
1352 add_symbol
= op_symbol
;
1354 seg_left
= seg_right
;
1358 else if (op
== O_subtract
)
1360 if (seg_right
== absolute_section
)
1368 /* Equality and non-equality tests are permitted on anything.
1369 Subtraction, and other comparison operators are permitted if
1370 both operands are in the same section. Otherwise, both
1371 operands must be absolute. We already handled the case of
1372 addition or subtraction of a constant above. This will
1373 probably need to be changed for an object file format which
1374 supports arbitrary expressions. */
1375 if (!(seg_left
== absolute_section
1376 && seg_right
== absolute_section
)
1377 && !(op
== O_eq
|| op
== O_ne
)
1378 && !((op
== O_subtract
1379 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1380 && seg_left
== seg_right
1381 && (seg_left
!= undefined_section
1382 || add_symbol
== op_symbol
)))
1384 /* Don't emit messages unless we're finalizing the symbol value,
1385 otherwise we may get the same message multiple times. */
1387 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1388 /* However do not move the symbol into the absolute section
1389 if it cannot currently be resolved - this would confuse
1390 other parts of the assembler into believing that the
1391 expression had been evaluated to zero. */
1397 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1398 final_seg
= absolute_section
;
1400 /* Check for division by zero. */
1401 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1403 /* If seg_right is not absolute_section, then we've
1404 already issued a warning about using a bad symbol. */
1405 if (seg_right
== absolute_section
&& finalize_syms
)
1410 if (expr_symbol_where (symp
, &file
, &line
))
1411 as_bad_where (file
, line
, _("division by zero"));
1413 as_bad (_("division by zero when setting `%s'"),
1420 switch (symp
->sy_value
.X_op
)
1422 case O_multiply
: left
*= right
; break;
1423 case O_divide
: left
/= right
; break;
1424 case O_modulus
: left
%= right
; break;
1425 case O_left_shift
: left
<<= right
; break;
1426 case O_right_shift
: left
>>= right
; break;
1427 case O_bit_inclusive_or
: left
|= right
; break;
1428 case O_bit_or_not
: left
|= ~right
; break;
1429 case O_bit_exclusive_or
: left
^= right
; break;
1430 case O_bit_and
: left
&= right
; break;
1431 case O_add
: left
+= right
; break;
1432 case O_subtract
: left
-= right
; break;
1435 left
= (left
== right
&& seg_left
== seg_right
1436 && (seg_left
!= undefined_section
1437 || add_symbol
== op_symbol
)
1438 ? ~ (offsetT
) 0 : 0);
1439 if (symp
->sy_value
.X_op
== O_ne
)
1442 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1443 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1444 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1445 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1446 case O_logical_and
: left
= left
&& right
; break;
1447 case O_logical_or
: left
= left
|| right
; break;
1452 /* See PR 20895 for a reproducer. */
1453 as_bad (_("Invalid operation on symbol"));
1454 goto exit_dont_set_value
;
1460 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1461 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1463 if (seg_left
== undefined_section
1464 || seg_right
== undefined_section
)
1465 final_seg
= undefined_section
;
1466 else if (seg_left
== absolute_section
)
1467 final_seg
= seg_right
;
1469 final_seg
= seg_left
;
1471 resolved
= (symbol_resolved_p (add_symbol
)
1472 && symbol_resolved_p (op_symbol
));
1477 /* Give an error (below) if not in expr_section. We don't
1478 want to worry about expr_section symbols, because they
1479 are fictional (they are created as part of expression
1480 resolution), and any problems may not actually mean
1485 symp
->sy_flags
.sy_resolving
= 0;
1489 S_SET_VALUE (symp
, final_val
);
1491 exit_dont_set_value
:
1492 /* Always set the segment, even if not finalizing the value.
1493 The segment is used to determine whether a symbol is defined. */
1494 S_SET_SEGMENT (symp
, final_seg
);
1496 /* Don't worry if we can't resolve an expr_section symbol. */
1500 symp
->sy_flags
.sy_resolved
= 1;
1501 else if (S_GET_SEGMENT (symp
) != expr_section
)
1503 as_bad (_("can't resolve value for symbol `%s'"),
1505 symp
->sy_flags
.sy_resolved
= 1;
1512 static void resolve_local_symbol (const char *, void *);
1514 /* A static function passed to hash_traverse. */
1517 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, void *value
)
1520 resolve_symbol_value ((symbolS
*) value
);
1523 /* Resolve all local symbols. */
1526 resolve_local_symbol_values (void)
1528 hash_traverse (local_hash
, resolve_local_symbol
);
1531 /* Obtain the current value of a symbol without changing any
1532 sub-expressions used. */
1535 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1537 symbolS
*symbolP
= *symbolPP
;
1539 if (LOCAL_SYMBOL_CHECK (symbolP
))
1541 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1543 *valueP
= locsym
->lsy_value
;
1544 *segP
= locsym
->lsy_section
;
1545 *fragPP
= local_symbol_get_frag (locsym
);
1549 expressionS exp
= symbolP
->sy_value
;
1551 if (!symbolP
->sy_flags
.sy_resolved
&& exp
.X_op
!= O_illegal
)
1555 if (symbolP
->sy_flags
.sy_resolving
)
1557 symbolP
->sy_flags
.sy_resolving
= 1;
1558 resolved
= resolve_expression (&exp
);
1559 symbolP
->sy_flags
.sy_resolving
= 0;
1567 if (!symbol_equated_p (symbolP
))
1572 symbolP
= exp
.X_add_symbol
;
1579 *symbolPP
= symbolP
;
1581 /* A bogus input file can result in resolve_expression()
1582 generating a local symbol, so we have to check again. */
1583 if (LOCAL_SYMBOL_CHECK (symbolP
))
1585 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1587 *valueP
= locsym
->lsy_value
;
1588 *segP
= locsym
->lsy_section
;
1589 *fragPP
= local_symbol_get_frag (locsym
);
1593 *valueP
= exp
.X_add_number
;
1594 *segP
= symbolP
->bsym
->section
;
1595 *fragPP
= symbolP
->sy_frag
;
1598 if (*segP
== expr_section
)
1601 case O_constant
: *segP
= absolute_section
; break;
1602 case O_register
: *segP
= reg_section
; break;
1610 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1611 They are *really* local. That is, they go out of scope whenever we see a
1612 label that isn't local. Also, like fb labels, there can be multiple
1613 instances of a dollar label. Therefor, we name encode each instance with
1614 the instance number, keep a list of defined symbols separate from the real
1615 symbol table, and we treat these buggers as a sparse array. */
1617 static long *dollar_labels
;
1618 static long *dollar_label_instances
;
1619 static char *dollar_label_defines
;
1620 static unsigned long dollar_label_count
;
1621 static unsigned long dollar_label_max
;
1624 dollar_label_defined (long label
)
1628 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1630 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1632 return dollar_label_defines
[i
- dollar_labels
];
1634 /* If we get here, label isn't defined. */
1639 dollar_label_instance (long label
)
1643 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1645 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1647 return (dollar_label_instances
[i
- dollar_labels
]);
1649 /* If we get here, we haven't seen the label before.
1650 Therefore its instance count is zero. */
1655 dollar_label_clear (void)
1657 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1660 #define DOLLAR_LABEL_BUMP_BY 10
1663 define_dollar_label (long label
)
1667 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1670 ++dollar_label_instances
[i
- dollar_labels
];
1671 dollar_label_defines
[i
- dollar_labels
] = 1;
1675 /* If we get to here, we don't have label listed yet. */
1677 if (dollar_labels
== NULL
)
1679 dollar_labels
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1680 dollar_label_instances
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1681 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1682 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1683 dollar_label_count
= 0;
1685 else if (dollar_label_count
== dollar_label_max
)
1687 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1688 dollar_labels
= XRESIZEVEC (long, dollar_labels
, dollar_label_max
);
1689 dollar_label_instances
= XRESIZEVEC (long, dollar_label_instances
,
1691 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1693 } /* if we needed to grow */
1695 dollar_labels
[dollar_label_count
] = label
;
1696 dollar_label_instances
[dollar_label_count
] = 1;
1697 dollar_label_defines
[dollar_label_count
] = 1;
1698 ++dollar_label_count
;
1701 /* Caller must copy returned name: we re-use the area for the next name.
1703 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1704 where n is the label number and m is the instance number. "L" makes
1705 it a label discarded unless debugging and "^A"('\1') ensures no
1706 ordinary symbol SHOULD get the same name as a local label
1707 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1709 fb labels get the same treatment, except that ^B is used in place
1712 char * /* Return local label name. */
1713 dollar_label_name (long n
, /* we just saw "n$:" : n a number. */
1714 int augend
/* 0 for current instance, 1 for new instance. */)
1717 /* Returned to caller, then copied. Used for created names ("4f"). */
1718 static char symbol_name_build
[24];
1721 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1724 know (augend
== 0 || augend
== 1);
1725 p
= symbol_name_build
;
1726 #ifdef LOCAL_LABEL_PREFIX
1727 *p
++ = LOCAL_LABEL_PREFIX
;
1731 /* Next code just does sprintf( {}, "%d", n); */
1733 q
= symbol_name_temporary
;
1734 for (*q
++ = 0, i
= n
; i
; ++q
)
1739 while ((*p
= *--q
) != '\0')
1742 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1744 /* Instance number. */
1745 q
= symbol_name_temporary
;
1746 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1751 while ((*p
++ = *--q
) != '\0');
1753 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1754 return symbol_name_build
;
1757 /* Somebody else's idea of local labels. They are made by "n:" where n
1758 is any decimal digit. Refer to them with
1759 "nb" for previous (backward) n:
1760 or "nf" for next (forward) n:.
1762 We do a little better and let n be any number, not just a single digit, but
1763 since the other guy's assembler only does ten, we treat the first ten
1766 Like someone else's assembler, we have one set of local label counters for
1767 entire assembly, not one set per (sub)segment like in most assemblers. This
1768 implies that one can refer to a label in another segment, and indeed some
1769 crufty compilers have done just that.
1771 Since there could be a LOT of these things, treat them as a sparse
1774 #define FB_LABEL_SPECIAL (10)
1776 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1777 static long *fb_labels
;
1778 static long *fb_label_instances
;
1779 static long fb_label_count
;
1780 static long fb_label_max
;
1782 /* This must be more than FB_LABEL_SPECIAL. */
1783 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1786 fb_label_init (void)
1788 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1791 /* Add one to the instance number of this fb label. */
1794 fb_label_instance_inc (long label
)
1798 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1800 ++fb_low_counter
[label
];
1804 if (fb_labels
!= NULL
)
1806 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1807 i
< fb_labels
+ fb_label_count
; ++i
)
1811 ++fb_label_instances
[i
- fb_labels
];
1813 } /* if we find it */
1814 } /* for each existing label */
1817 /* If we get to here, we don't have label listed yet. */
1819 if (fb_labels
== NULL
)
1821 fb_labels
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1822 fb_label_instances
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1823 fb_label_max
= FB_LABEL_BUMP_BY
;
1824 fb_label_count
= FB_LABEL_SPECIAL
;
1827 else if (fb_label_count
== fb_label_max
)
1829 fb_label_max
+= FB_LABEL_BUMP_BY
;
1830 fb_labels
= XRESIZEVEC (long, fb_labels
, fb_label_max
);
1831 fb_label_instances
= XRESIZEVEC (long, fb_label_instances
, fb_label_max
);
1832 } /* if we needed to grow */
1834 fb_labels
[fb_label_count
] = label
;
1835 fb_label_instances
[fb_label_count
] = 1;
1840 fb_label_instance (long label
)
1844 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1846 return (fb_low_counter
[label
]);
1849 if (fb_labels
!= NULL
)
1851 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1852 i
< fb_labels
+ fb_label_count
; ++i
)
1856 return (fb_label_instances
[i
- fb_labels
]);
1857 } /* if we find it */
1858 } /* for each existing label */
1861 /* We didn't find the label, so this must be a reference to the
1866 /* Caller must copy returned name: we re-use the area for the next name.
1868 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
1869 where n is the label number and m is the instance number. "L" makes
1870 it a label discarded unless debugging and "^B"('\2') ensures no
1871 ordinary symbol SHOULD get the same name as a local label
1872 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1874 dollar labels get the same treatment, except that ^A is used in
1877 char * /* Return local label name. */
1878 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1879 long augend
/* 0 for nb, 1 for n:, nf. */)
1882 /* Returned to caller, then copied. Used for created names ("4f"). */
1883 static char symbol_name_build
[24];
1886 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1890 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1892 know ((unsigned long) augend
<= 1);
1894 p
= symbol_name_build
;
1895 #ifdef LOCAL_LABEL_PREFIX
1896 *p
++ = LOCAL_LABEL_PREFIX
;
1900 /* Next code just does sprintf( {}, "%d", n); */
1902 q
= symbol_name_temporary
;
1903 for (*q
++ = 0, i
= n
; i
; ++q
)
1908 while ((*p
= *--q
) != '\0')
1911 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1913 /* Instance number. */
1914 q
= symbol_name_temporary
;
1915 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1920 while ((*p
++ = *--q
) != '\0');
1922 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1923 return (symbol_name_build
);
1926 /* Decode name that may have been generated by foo_label_name() above.
1927 If the name wasn't generated by foo_label_name(), then return it
1928 unaltered. This is used for error messages. */
1931 decode_local_label_name (char *s
)
1934 char *symbol_decode
;
1936 int instance_number
;
1938 const char *message_format
;
1941 #ifdef LOCAL_LABEL_PREFIX
1942 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
1946 if (s
[lindex
] != 'L')
1949 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
1950 label_number
= (10 * label_number
) + *p
- '0';
1952 if (*p
== DOLLAR_LABEL_CHAR
)
1954 else if (*p
== LOCAL_LABEL_CHAR
)
1959 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1960 instance_number
= (10 * instance_number
) + *p
- '0';
1962 message_format
= _("\"%d\" (instance number %d of a %s label)");
1963 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
1964 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1966 return symbol_decode
;
1969 /* Get the value of a symbol. */
1972 S_GET_VALUE (symbolS
*s
)
1974 if (LOCAL_SYMBOL_CHECK (s
))
1975 return resolve_symbol_value (s
);
1977 if (!s
->sy_flags
.sy_resolved
)
1979 valueT val
= resolve_symbol_value (s
);
1983 if (S_IS_WEAKREFR (s
))
1984 return S_GET_VALUE (s
->sy_value
.X_add_symbol
);
1986 if (s
->sy_value
.X_op
!= O_constant
)
1988 if (! s
->sy_flags
.sy_resolved
1989 || s
->sy_value
.X_op
!= O_symbol
1990 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1991 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1994 return (valueT
) s
->sy_value
.X_add_number
;
1997 /* Set the value of a symbol. */
2000 S_SET_VALUE (symbolS
*s
, valueT val
)
2002 if (LOCAL_SYMBOL_CHECK (s
))
2004 ((struct local_symbol
*) s
)->lsy_value
= val
;
2008 s
->sy_value
.X_op
= O_constant
;
2009 s
->sy_value
.X_add_number
= (offsetT
) val
;
2010 s
->sy_value
.X_unsigned
= 0;
2011 S_CLEAR_WEAKREFR (s
);
2015 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2017 if (LOCAL_SYMBOL_CHECK (dest
))
2018 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
2019 if (LOCAL_SYMBOL_CHECK (src
))
2020 src
= local_symbol_convert ((struct local_symbol
*) src
);
2022 /* In an expression, transfer the settings of these flags.
2023 The user can override later, of course. */
2024 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2025 | BSF_GNU_INDIRECT_FUNCTION)
2026 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2028 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2029 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2032 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2033 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2038 S_IS_FUNCTION (symbolS
*s
)
2042 if (LOCAL_SYMBOL_CHECK (s
))
2045 flags
= s
->bsym
->flags
;
2047 return (flags
& BSF_FUNCTION
) != 0;
2051 S_IS_EXTERNAL (symbolS
*s
)
2055 if (LOCAL_SYMBOL_CHECK (s
))
2058 flags
= s
->bsym
->flags
;
2061 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2064 return (flags
& BSF_GLOBAL
) != 0;
2068 S_IS_WEAK (symbolS
*s
)
2070 if (LOCAL_SYMBOL_CHECK (s
))
2072 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2073 could probably handle a WEAKREFR as always weak though. E.g., if
2074 the referenced symbol has lost its weak status, there's no reason
2075 to keep handling the weakrefr as if it was weak. */
2076 if (S_IS_WEAKREFR (s
))
2077 return S_IS_WEAK (s
->sy_value
.X_add_symbol
);
2078 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2082 S_IS_WEAKREFR (symbolS
*s
)
2084 if (LOCAL_SYMBOL_CHECK (s
))
2086 return s
->sy_flags
.sy_weakrefr
!= 0;
2090 S_IS_WEAKREFD (symbolS
*s
)
2092 if (LOCAL_SYMBOL_CHECK (s
))
2094 return s
->sy_flags
.sy_weakrefd
!= 0;
2098 S_IS_COMMON (symbolS
*s
)
2100 if (LOCAL_SYMBOL_CHECK (s
))
2102 return bfd_is_com_section (s
->bsym
->section
);
2106 S_IS_DEFINED (symbolS
*s
)
2108 if (LOCAL_SYMBOL_CHECK (s
))
2109 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
2110 return s
->bsym
->section
!= undefined_section
;
2114 #ifndef EXTERN_FORCE_RELOC
2115 #define EXTERN_FORCE_RELOC IS_ELF
2118 /* Return true for symbols that should not be reduced to section
2119 symbols or eliminated from expressions, because they may be
2120 overridden by the linker. */
2122 S_FORCE_RELOC (symbolS
*s
, int strict
)
2125 if (LOCAL_SYMBOL_CHECK (s
))
2126 sec
= ((struct local_symbol
*) s
)->lsy_section
;
2130 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2131 || (EXTERN_FORCE_RELOC
2132 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2133 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2135 sec
= s
->bsym
->section
;
2137 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2141 S_IS_DEBUG (symbolS
*s
)
2143 if (LOCAL_SYMBOL_CHECK (s
))
2145 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2151 S_IS_LOCAL (symbolS
*s
)
2156 if (LOCAL_SYMBOL_CHECK (s
))
2159 flags
= s
->bsym
->flags
;
2162 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2165 if (bfd_get_section (s
->bsym
) == reg_section
)
2168 if (flag_strip_local_absolute
2169 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2170 the source file even when the object file is stripped. */
2171 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2172 && bfd_get_section (s
->bsym
) == absolute_section
)
2175 name
= S_GET_NAME (s
);
2176 return (name
!= NULL
2178 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2179 || strchr (name
, LOCAL_LABEL_CHAR
)
2180 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2181 || strchr (name
, FAKE_LABEL_CHAR
)
2183 || TC_LABEL_IS_LOCAL (name
)
2184 || (! flag_keep_locals
2185 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2188 && name
[1] == '?')))));
2192 S_IS_STABD (symbolS
*s
)
2194 return S_GET_NAME (s
) == 0;
2198 S_CAN_BE_REDEFINED (const symbolS
*s
)
2200 if (LOCAL_SYMBOL_CHECK (s
))
2201 return (local_symbol_get_frag ((struct local_symbol
*) s
)
2202 == &predefined_address_frag
);
2203 /* Permit register names to be redefined. */
2204 return s
->bsym
->section
== reg_section
;
2208 S_IS_VOLATILE (const symbolS
*s
)
2210 if (LOCAL_SYMBOL_CHECK (s
))
2212 return s
->sy_flags
.sy_volatile
;
2216 S_IS_FORWARD_REF (const symbolS
*s
)
2218 if (LOCAL_SYMBOL_CHECK (s
))
2220 return s
->sy_flags
.sy_forward_ref
;
2224 S_GET_NAME (symbolS
*s
)
2226 if (LOCAL_SYMBOL_CHECK (s
))
2227 return ((struct local_symbol
*) s
)->lsy_name
;
2228 return s
->bsym
->name
;
2232 S_GET_SEGMENT (symbolS
*s
)
2234 if (LOCAL_SYMBOL_CHECK (s
))
2235 return ((struct local_symbol
*) s
)->lsy_section
;
2236 return s
->bsym
->section
;
2240 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2242 /* Don't reassign section symbols. The direct reason is to prevent seg
2243 faults assigning back to const global symbols such as *ABS*, but it
2244 shouldn't happen anyway. */
2246 if (LOCAL_SYMBOL_CHECK (s
))
2248 if (seg
== reg_section
)
2249 s
= local_symbol_convert ((struct local_symbol
*) s
);
2252 ((struct local_symbol
*) s
)->lsy_section
= seg
;
2257 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2259 if (s
->bsym
->section
!= seg
)
2263 s
->bsym
->section
= seg
;
2267 S_SET_EXTERNAL (symbolS
*s
)
2269 if (LOCAL_SYMBOL_CHECK (s
))
2270 s
= local_symbol_convert ((struct local_symbol
*) s
);
2271 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2273 /* Let .weak override .global. */
2276 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2278 /* Do not reassign section symbols. */
2279 as_warn (_("section symbols are already global"));
2282 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2283 if (S_GET_SEGMENT (s
) == reg_section
)
2285 as_bad ("can't make register symbol `%s' global",
2290 s
->bsym
->flags
|= BSF_GLOBAL
;
2291 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2294 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2295 an_external_name
= S_GET_NAME (s
);
2300 S_CLEAR_EXTERNAL (symbolS
*s
)
2302 if (LOCAL_SYMBOL_CHECK (s
))
2304 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2306 /* Let .weak override. */
2309 s
->bsym
->flags
|= BSF_LOCAL
;
2310 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2314 S_SET_WEAK (symbolS
*s
)
2316 if (LOCAL_SYMBOL_CHECK (s
))
2317 s
= local_symbol_convert ((struct local_symbol
*) s
);
2318 #ifdef obj_set_weak_hook
2319 obj_set_weak_hook (s
);
2321 s
->bsym
->flags
|= BSF_WEAK
;
2322 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2326 S_SET_WEAKREFR (symbolS
*s
)
2328 if (LOCAL_SYMBOL_CHECK (s
))
2329 s
= local_symbol_convert ((struct local_symbol
*) s
);
2330 s
->sy_flags
.sy_weakrefr
= 1;
2331 /* If the alias was already used, make sure we mark the target as
2332 used as well, otherwise it might be dropped from the symbol
2333 table. This may have unintended side effects if the alias is
2334 later redirected to another symbol, such as keeping the unused
2335 previous target in the symbol table. Since it will be weak, it's
2337 if (s
->sy_flags
.sy_used
)
2338 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2342 S_CLEAR_WEAKREFR (symbolS
*s
)
2344 if (LOCAL_SYMBOL_CHECK (s
))
2346 s
->sy_flags
.sy_weakrefr
= 0;
2350 S_SET_WEAKREFD (symbolS
*s
)
2352 if (LOCAL_SYMBOL_CHECK (s
))
2353 s
= local_symbol_convert ((struct local_symbol
*) s
);
2354 s
->sy_flags
.sy_weakrefd
= 1;
2359 S_CLEAR_WEAKREFD (symbolS
*s
)
2361 if (LOCAL_SYMBOL_CHECK (s
))
2363 if (s
->sy_flags
.sy_weakrefd
)
2365 s
->sy_flags
.sy_weakrefd
= 0;
2366 /* If a weakref target symbol is weak, then it was never
2367 referenced directly before, not even in a .global directive,
2368 so decay it to local. If it remains undefined, it will be
2369 later turned into a global, like any other undefined
2371 if (s
->bsym
->flags
& BSF_WEAK
)
2373 #ifdef obj_clear_weak_hook
2374 obj_clear_weak_hook (s
);
2376 s
->bsym
->flags
&= ~BSF_WEAK
;
2377 s
->bsym
->flags
|= BSF_LOCAL
;
2383 S_SET_THREAD_LOCAL (symbolS
*s
)
2385 if (LOCAL_SYMBOL_CHECK (s
))
2386 s
= local_symbol_convert ((struct local_symbol
*) s
);
2387 if (bfd_is_com_section (s
->bsym
->section
)
2388 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2390 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2391 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2392 as_bad (_("Accessing function `%s' as thread-local object"),
2394 else if (! bfd_is_und_section (s
->bsym
->section
)
2395 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2396 as_bad (_("Accessing `%s' as thread-local object"),
2401 S_SET_NAME (symbolS
*s
, const char *name
)
2403 if (LOCAL_SYMBOL_CHECK (s
))
2405 ((struct local_symbol
*) s
)->lsy_name
= name
;
2408 s
->bsym
->name
= name
;
2412 S_SET_VOLATILE (symbolS
*s
)
2414 if (LOCAL_SYMBOL_CHECK (s
))
2415 s
= local_symbol_convert ((struct local_symbol
*) s
);
2416 s
->sy_flags
.sy_volatile
= 1;
2420 S_CLEAR_VOLATILE (symbolS
*s
)
2422 if (!LOCAL_SYMBOL_CHECK (s
))
2423 s
->sy_flags
.sy_volatile
= 0;
2427 S_SET_FORWARD_REF (symbolS
*s
)
2429 if (LOCAL_SYMBOL_CHECK (s
))
2430 s
= local_symbol_convert ((struct local_symbol
*) s
);
2431 s
->sy_flags
.sy_forward_ref
= 1;
2434 /* Return the previous symbol in a chain. */
2437 symbol_previous (symbolS
*s
)
2439 if (LOCAL_SYMBOL_CHECK (s
))
2441 return s
->sy_previous
;
2444 /* Return the next symbol in a chain. */
2447 symbol_next (symbolS
*s
)
2449 if (LOCAL_SYMBOL_CHECK (s
))
2454 /* Return a pointer to the value of a symbol as an expression. */
2457 symbol_get_value_expression (symbolS
*s
)
2459 if (LOCAL_SYMBOL_CHECK (s
))
2460 s
= local_symbol_convert ((struct local_symbol
*) s
);
2461 return &s
->sy_value
;
2464 /* Set the value of a symbol to an expression. */
2467 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2469 if (LOCAL_SYMBOL_CHECK (s
))
2470 s
= local_symbol_convert ((struct local_symbol
*) s
);
2472 S_CLEAR_WEAKREFR (s
);
2475 /* Return whether 2 symbols are the same. */
2478 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2480 s1
= get_real_sym (s1
);
2481 s2
= get_real_sym (s2
);
2485 /* Return a pointer to the X_add_number component of a symbol. */
2488 symbol_X_add_number (symbolS
*s
)
2490 if (LOCAL_SYMBOL_CHECK (s
))
2491 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
2493 return &s
->sy_value
.X_add_number
;
2496 /* Set the value of SYM to the current position in the current segment. */
2499 symbol_set_value_now (symbolS
*sym
)
2501 S_SET_SEGMENT (sym
, now_seg
);
2502 S_SET_VALUE (sym
, frag_now_fix ());
2503 symbol_set_frag (sym
, frag_now
);
2506 /* Set the frag of a symbol. */
2509 symbol_set_frag (symbolS
*s
, fragS
*f
)
2511 if (LOCAL_SYMBOL_CHECK (s
))
2513 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2517 S_CLEAR_WEAKREFR (s
);
2520 /* Return the frag of a symbol. */
2523 symbol_get_frag (symbolS
*s
)
2525 if (LOCAL_SYMBOL_CHECK (s
))
2526 return local_symbol_get_frag ((struct local_symbol
*) s
);
2530 /* Mark a symbol as having been used. */
2533 symbol_mark_used (symbolS
*s
)
2535 if (LOCAL_SYMBOL_CHECK (s
))
2537 s
->sy_flags
.sy_used
= 1;
2538 if (S_IS_WEAKREFR (s
))
2539 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2542 /* Clear the mark of whether a symbol has been used. */
2545 symbol_clear_used (symbolS
*s
)
2547 if (LOCAL_SYMBOL_CHECK (s
))
2548 s
= local_symbol_convert ((struct local_symbol
*) s
);
2549 s
->sy_flags
.sy_used
= 0;
2552 /* Return whether a symbol has been used. */
2555 symbol_used_p (symbolS
*s
)
2557 if (LOCAL_SYMBOL_CHECK (s
))
2559 return s
->sy_flags
.sy_used
;
2562 /* Mark a symbol as having been used in a reloc. */
2565 symbol_mark_used_in_reloc (symbolS
*s
)
2567 if (LOCAL_SYMBOL_CHECK (s
))
2568 s
= local_symbol_convert ((struct local_symbol
*) s
);
2569 s
->sy_flags
.sy_used_in_reloc
= 1;
2572 /* Clear the mark of whether a symbol has been used in a reloc. */
2575 symbol_clear_used_in_reloc (symbolS
*s
)
2577 if (LOCAL_SYMBOL_CHECK (s
))
2579 s
->sy_flags
.sy_used_in_reloc
= 0;
2582 /* Return whether a symbol has been used in a reloc. */
2585 symbol_used_in_reloc_p (symbolS
*s
)
2587 if (LOCAL_SYMBOL_CHECK (s
))
2589 return s
->sy_flags
.sy_used_in_reloc
;
2592 /* Mark a symbol as an MRI common symbol. */
2595 symbol_mark_mri_common (symbolS
*s
)
2597 if (LOCAL_SYMBOL_CHECK (s
))
2598 s
= local_symbol_convert ((struct local_symbol
*) s
);
2599 s
->sy_flags
.sy_mri_common
= 1;
2602 /* Clear the mark of whether a symbol is an MRI common symbol. */
2605 symbol_clear_mri_common (symbolS
*s
)
2607 if (LOCAL_SYMBOL_CHECK (s
))
2609 s
->sy_flags
.sy_mri_common
= 0;
2612 /* Return whether a symbol is an MRI common symbol. */
2615 symbol_mri_common_p (symbolS
*s
)
2617 if (LOCAL_SYMBOL_CHECK (s
))
2619 return s
->sy_flags
.sy_mri_common
;
2622 /* Mark a symbol as having been written. */
2625 symbol_mark_written (symbolS
*s
)
2627 if (LOCAL_SYMBOL_CHECK (s
))
2629 s
->sy_flags
.sy_written
= 1;
2632 /* Clear the mark of whether a symbol has been written. */
2635 symbol_clear_written (symbolS
*s
)
2637 if (LOCAL_SYMBOL_CHECK (s
))
2639 s
->sy_flags
.sy_written
= 0;
2642 /* Return whether a symbol has been written. */
2645 symbol_written_p (symbolS
*s
)
2647 if (LOCAL_SYMBOL_CHECK (s
))
2649 return s
->sy_flags
.sy_written
;
2652 /* Mark a symbol has having been resolved. */
2655 symbol_mark_resolved (symbolS
*s
)
2657 if (LOCAL_SYMBOL_CHECK (s
))
2659 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2662 s
->sy_flags
.sy_resolved
= 1;
2665 /* Return whether a symbol has been resolved. */
2668 symbol_resolved_p (symbolS
*s
)
2670 if (LOCAL_SYMBOL_CHECK (s
))
2671 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2672 return s
->sy_flags
.sy_resolved
;
2675 /* Return whether a symbol is a section symbol. */
2678 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2680 if (LOCAL_SYMBOL_CHECK (s
))
2682 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2685 /* Return whether a symbol is equated to another symbol. */
2688 symbol_equated_p (symbolS
*s
)
2690 if (LOCAL_SYMBOL_CHECK (s
))
2692 return s
->sy_value
.X_op
== O_symbol
;
2695 /* Return whether a symbol is equated to another symbol, and should be
2696 treated specially when writing out relocs. */
2699 symbol_equated_reloc_p (symbolS
*s
)
2701 if (LOCAL_SYMBOL_CHECK (s
))
2703 /* X_op_symbol, normally not used for O_symbol, is set by
2704 resolve_symbol_value to flag expression syms that have been
2706 return (s
->sy_value
.X_op
== O_symbol
2707 #if defined (OBJ_COFF) && defined (TE_PE)
2710 && ((s
->sy_flags
.sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2711 || ! S_IS_DEFINED (s
)
2712 || S_IS_COMMON (s
)));
2715 /* Return whether a symbol has a constant value. */
2718 symbol_constant_p (symbolS
*s
)
2720 if (LOCAL_SYMBOL_CHECK (s
))
2722 return s
->sy_value
.X_op
== O_constant
;
2725 /* Return whether a symbol was cloned and thus removed from the global
2729 symbol_shadow_p (symbolS
*s
)
2731 if (LOCAL_SYMBOL_CHECK (s
))
2733 return s
->sy_next
== s
;
2736 /* Return the BFD symbol for a symbol. */
2739 symbol_get_bfdsym (symbolS
*s
)
2741 if (LOCAL_SYMBOL_CHECK (s
))
2742 s
= local_symbol_convert ((struct local_symbol
*) s
);
2746 /* Set the BFD symbol for a symbol. */
2749 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2751 if (LOCAL_SYMBOL_CHECK (s
))
2752 s
= local_symbol_convert ((struct local_symbol
*) s
);
2753 /* Usually, it is harmless to reset a symbol to a BFD section
2754 symbol. For example, obj_elf_change_section sets the BFD symbol
2755 of an old symbol with the newly created section symbol. But when
2756 we have multiple sections with the same name, the newly created
2757 section may have the same name as an old section. We check if the
2758 old symbol has been already marked as a section symbol before
2760 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2762 /* else XXX - What do we do now ? */
2765 #ifdef OBJ_SYMFIELD_TYPE
2767 /* Get a pointer to the object format information for a symbol. */
2770 symbol_get_obj (symbolS
*s
)
2772 if (LOCAL_SYMBOL_CHECK (s
))
2773 s
= local_symbol_convert ((struct local_symbol
*) s
);
2777 /* Set the object format information for a symbol. */
2780 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2782 if (LOCAL_SYMBOL_CHECK (s
))
2783 s
= local_symbol_convert ((struct local_symbol
*) s
);
2787 #endif /* OBJ_SYMFIELD_TYPE */
2789 #ifdef TC_SYMFIELD_TYPE
2791 /* Get a pointer to the processor information for a symbol. */
2794 symbol_get_tc (symbolS
*s
)
2796 if (LOCAL_SYMBOL_CHECK (s
))
2797 s
= local_symbol_convert ((struct local_symbol
*) s
);
2801 /* Set the processor information for a symbol. */
2804 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2806 if (LOCAL_SYMBOL_CHECK (s
))
2807 s
= local_symbol_convert ((struct local_symbol
*) s
);
2811 #endif /* TC_SYMFIELD_TYPE */
2816 symbol_lastP
= NULL
;
2817 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2818 sy_hash
= hash_new ();
2819 local_hash
= hash_new ();
2821 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2822 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2823 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
2825 abs_symbol
.sy_value
.X_op
= O_constant
;
2826 abs_symbol
.sy_frag
= &zero_address_frag
;
2828 if (LOCAL_LABELS_FB
)
2833 dot_symbol_init (void)
2835 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2836 if (dot_symbol
.bsym
== NULL
)
2837 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2838 dot_symbol
.bsym
->name
= ".";
2839 dot_symbol
.sy_flags
.sy_forward_ref
= 1;
2840 dot_symbol
.sy_value
.X_op
= O_constant
;
2845 /* Maximum indent level.
2846 Available for modification inside a gdb session. */
2847 static int max_indent_level
= 8;
2850 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2852 const char *name
= S_GET_NAME (sym
);
2853 if (!name
|| !name
[0])
2855 fprintf (file
, "sym ");
2856 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
2857 fprintf (file
, " %s", name
);
2859 if (LOCAL_SYMBOL_CHECK (sym
))
2861 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2863 if (local_symbol_get_frag (locsym
) != & zero_address_frag
2864 && local_symbol_get_frag (locsym
) != NULL
)
2866 fprintf (file
, " frag ");
2867 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) local_symbol_get_frag (locsym
)));
2869 if (local_symbol_resolved_p (locsym
))
2870 fprintf (file
, " resolved");
2871 fprintf (file
, " local");
2875 if (sym
->sy_frag
!= &zero_address_frag
)
2877 fprintf (file
, " frag ");
2878 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->sy_frag
));
2880 if (sym
->sy_flags
.sy_written
)
2881 fprintf (file
, " written");
2882 if (sym
->sy_flags
.sy_resolved
)
2883 fprintf (file
, " resolved");
2884 else if (sym
->sy_flags
.sy_resolving
)
2885 fprintf (file
, " resolving");
2886 if (sym
->sy_flags
.sy_used_in_reloc
)
2887 fprintf (file
, " used-in-reloc");
2888 if (sym
->sy_flags
.sy_used
)
2889 fprintf (file
, " used");
2890 if (S_IS_LOCAL (sym
))
2891 fprintf (file
, " local");
2892 if (S_IS_EXTERNAL (sym
))
2893 fprintf (file
, " extern");
2894 if (S_IS_WEAK (sym
))
2895 fprintf (file
, " weak");
2896 if (S_IS_DEBUG (sym
))
2897 fprintf (file
, " debug");
2898 if (S_IS_DEFINED (sym
))
2899 fprintf (file
, " defined");
2901 if (S_IS_WEAKREFR (sym
))
2902 fprintf (file
, " weakrefr");
2903 if (S_IS_WEAKREFD (sym
))
2904 fprintf (file
, " weakrefd");
2905 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2906 if (symbol_resolved_p (sym
))
2908 segT s
= S_GET_SEGMENT (sym
);
2910 if (s
!= undefined_section
2911 && s
!= expr_section
)
2912 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
2914 else if (indent_level
< max_indent_level
2915 && S_GET_SEGMENT (sym
) != undefined_section
)
2918 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2919 if (LOCAL_SYMBOL_CHECK (sym
))
2920 fprintf (file
, "constant %lx",
2921 (unsigned long) ((struct local_symbol
*) sym
)->lsy_value
);
2923 print_expr_1 (file
, &sym
->sy_value
);
2924 fprintf (file
, ">");
2931 print_symbol_value (symbolS
*sym
)
2934 print_symbol_value_1 (stderr
, sym
);
2935 fprintf (stderr
, "\n");
2939 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2942 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2943 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2944 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2945 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2946 fprintf (file
, ">");
2951 print_expr_1 (FILE *file
, expressionS
*exp
)
2953 fprintf (file
, "expr ");
2954 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
2955 fprintf (file
, " ");
2959 fprintf (file
, "illegal");
2962 fprintf (file
, "absent");
2965 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
2969 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2970 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2971 fprintf (file
, ">");
2973 if (exp
->X_add_number
)
2974 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2975 (unsigned long) exp
->X_add_number
);
2979 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2982 fprintf (file
, "big");
2985 fprintf (file
, "uminus -<");
2987 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2988 fprintf (file
, ">");
2989 goto maybe_print_addnum
;
2991 fprintf (file
, "bit_not");
2994 print_binary (file
, "multiply", exp
);
2997 print_binary (file
, "divide", exp
);
3000 print_binary (file
, "modulus", exp
);
3003 print_binary (file
, "lshift", exp
);
3006 print_binary (file
, "rshift", exp
);
3008 case O_bit_inclusive_or
:
3009 print_binary (file
, "bit_ior", exp
);
3011 case O_bit_exclusive_or
:
3012 print_binary (file
, "bit_xor", exp
);
3015 print_binary (file
, "bit_and", exp
);
3018 print_binary (file
, "eq", exp
);
3021 print_binary (file
, "ne", exp
);
3024 print_binary (file
, "lt", exp
);
3027 print_binary (file
, "le", exp
);
3030 print_binary (file
, "ge", exp
);
3033 print_binary (file
, "gt", exp
);
3036 print_binary (file
, "logical_and", exp
);
3039 print_binary (file
, "logical_or", exp
);
3043 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3044 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3045 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3046 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3047 fprintf (file
, ">");
3048 goto maybe_print_addnum
;
3051 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3052 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3053 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3054 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3055 fprintf (file
, ">");
3056 goto maybe_print_addnum
;
3058 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3065 print_expr (expressionS
*exp
)
3067 print_expr_1 (stderr
, exp
);
3068 fprintf (stderr
, "\n");
3072 symbol_print_statistics (FILE *file
)
3074 hash_print_statistics (file
, "symbol table", sy_hash
);
3075 hash_print_statistics (file
, "mini local symbol table", local_hash
);
3076 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3077 local_symbol_count
, local_symbol_conversion_count
);
3080 #ifdef OBJ_COMPLEX_RELC
3082 /* Convert given symbol to a new complex-relocation symbol name. This
3083 may be a recursive function, since it might be called for non-leaf
3084 nodes (plain symbols) in the expression tree. The caller owns the
3085 returning string, so should free it eventually. Errors are
3086 indicated via as_bad and a NULL return value. The given symbol
3087 is marked with sy_used_in_reloc. */
3090 symbol_relc_make_sym (symbolS
* sym
)
3092 char * terminal
= NULL
;
3097 gas_assert (sym
!= NULL
);
3099 /* Recurse to symbol_relc_make_expr if this symbol
3100 is defined as an expression or a plain value. */
3101 if ( S_GET_SEGMENT (sym
) == expr_section
3102 || S_GET_SEGMENT (sym
) == absolute_section
)
3103 return symbol_relc_make_expr (& sym
->sy_value
);
3105 /* This may be a "fake symbol", referring to ".".
3106 Write out a special null symbol to refer to this position. */
3107 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3108 return xstrdup (".");
3110 /* We hope this is a plain leaf symbol. Construct the encoding
3111 as {S,s}II...:CCCCCCC....
3112 where 'S'/'s' means section symbol / plain symbol
3113 III is decimal for the symbol name length
3114 CCC is the symbol name itself. */
3115 symbol_mark_used_in_reloc (sym
);
3117 sname
= S_GET_NAME (sym
);
3118 sname_len
= strlen (sname
);
3119 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3121 terminal
= XNEWVEC (char, (1 /* S or s */
3122 + 8 /* sname_len in decimal */
3124 + sname_len
/* name itself */
3127 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3131 /* Convert given value to a new complex-relocation symbol name. This
3132 is a non-recursive function, since it is be called for leaf nodes
3133 (plain values) in the expression tree. The caller owns the
3134 returning string, so should free() it eventually. No errors. */
3137 symbol_relc_make_value (offsetT val
)
3139 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3142 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3146 /* Convert given expression to a new complex-relocation symbol name.
3147 This is a recursive function, since it traverses the entire given
3148 expression tree. The caller owns the returning string, so should
3149 free() it eventually. Errors are indicated via as_bad() and a NULL
3153 symbol_relc_make_expr (expressionS
* exp
)
3155 const char * opstr
= NULL
; /* Operator prefix string. */
3156 int arity
= 0; /* Arity of this operator. */
3157 char * operands
[3]; /* Up to three operands. */
3158 char * concat_string
= NULL
;
3160 operands
[0] = operands
[1] = operands
[2] = NULL
;
3162 gas_assert (exp
!= NULL
);
3164 /* Match known operators -> fill in opstr, arity, operands[] and fall
3165 through to construct subexpression fragments; may instead return
3166 string directly for leaf nodes. */
3168 /* See expr.h for the meaning of all these enums. Many operators
3169 have an unnatural arity (X_add_number implicitly added). The
3170 conversion logic expands them to explicit "+" subexpressions. */
3175 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3180 return symbol_relc_make_value (exp
->X_add_number
);
3183 if (exp
->X_add_number
)
3187 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3188 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3192 return symbol_relc_make_sym (exp
->X_add_symbol
);
3194 /* Helper macros for nesting nodes. */
3196 #define HANDLE_XADD_OPT1(str_) \
3197 if (exp->X_add_number) \
3200 opstr = "+:" str_; \
3201 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3202 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3209 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3213 #define HANDLE_XADD_OPT2(str_) \
3214 if (exp->X_add_number) \
3217 opstr = "+:" str_; \
3218 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3219 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3220 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3226 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3227 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3231 /* Nesting nodes. */
3233 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3234 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3235 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3236 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3237 case O_divide
: HANDLE_XADD_OPT2 ("/");
3238 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3239 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3240 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3241 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3242 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3243 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3244 case O_add
: HANDLE_XADD_OPT2 ("+");
3245 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3246 case O_eq
: HANDLE_XADD_OPT2 ("==");
3247 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3248 case O_lt
: HANDLE_XADD_OPT2 ("<");
3249 case O_le
: HANDLE_XADD_OPT2 ("<=");
3250 case O_ge
: HANDLE_XADD_OPT2 (">=");
3251 case O_gt
: HANDLE_XADD_OPT2 (">");
3252 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3253 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3256 /* Validate & reject early. */
3257 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3259 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3261 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3265 concat_string
= NULL
;
3266 else if (arity
== 0)
3267 concat_string
= xstrdup (opstr
);
3268 else if (arity
== 1)
3269 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3270 else if (arity
== 2)
3271 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3274 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3275 operands
[2], (char *) NULL
);
3277 /* Free operand strings (not opstr). */
3278 if (arity
>= 1) xfree (operands
[0]);
3279 if (arity
>= 2) xfree (operands
[1]);
3280 if (arity
>= 3) xfree (operands
[2]);
3282 return concat_string
;