1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2022 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" */
36 /* Whether the symbol is a local_symbol. */
37 unsigned int local_symbol
: 1;
39 /* Weather symbol has been written. */
40 unsigned int written
: 1;
42 /* Whether symbol value has been completely resolved (used during
43 final pass over symbol table). */
44 unsigned int resolved
: 1;
46 /* Whether the symbol value is currently being resolved (used to
47 detect loops in symbol dependencies). */
48 unsigned int resolving
: 1;
50 /* Whether the symbol value is used in a reloc. This is used to
51 ensure that symbols used in relocs are written out, even if they
52 are local and would otherwise not be. */
53 unsigned int used_in_reloc
: 1;
55 /* Whether the symbol is used as an operand or in an expression.
56 NOTE: Not all the backends keep this information accurate;
57 backends which use this bit are responsible for setting it when
58 a symbol is used in backend routines. */
59 unsigned int used
: 1;
61 /* Whether the symbol can be re-defined. */
62 unsigned int volatil
: 1;
64 /* Whether the symbol is a forward reference. */
65 unsigned int forward_ref
: 1;
67 /* This is set if the symbol is defined in an MRI common section.
68 We handle such sections as single common symbols, so symbols
69 defined within them must be treated specially by the relocation
71 unsigned int mri_common
: 1;
73 /* This is set if the symbol is set with a .weakref directive. */
74 unsigned int weakrefr
: 1;
76 /* This is set when the symbol is referenced as part of a .weakref
77 directive, but only if the symbol was not in the symbol table
78 before. It is cleared as soon as any direct reference to the
80 unsigned int weakrefd
: 1;
82 /* Whether the symbol has been marked to be removed by a .symver
84 unsigned int removed
: 1;
86 /* Set when a warning about the symbol containing multibyte characters
88 unsigned int multibyte_warned
: 1;
91 /* A pointer in the symbol may point to either a complete symbol
92 (struct symbol below) or to a local symbol (struct local_symbol
93 defined here). The symbol code can detect the case by examining
94 the first field which is present in both structs.
96 We do this because we ordinarily only need a small amount of
97 information for a local symbol. The symbol table takes up a lot of
98 space, and storing less information for a local symbol can make a
99 big difference in assembler memory usage when assembling a large
104 /* Symbol flags. Only local_symbol and resolved are relevant. */
105 struct symbol_flags flags
;
107 /* Hash value calculated from name. */
110 /* The symbol name. */
113 /* The symbol frag. */
116 /* The symbol section. */
119 /* The value of the symbol. */
123 /* The information we keep for a symbol. The symbol table holds
124 pointers both to this and to local_symbol structures. The first
125 three fields must be identical to struct local_symbol, and the size
126 should be the same as or smaller than struct local_symbol.
127 Fields that don't fit go to an extension structure. */
132 struct symbol_flags flags
;
134 /* Hash value calculated from name. */
137 /* The symbol name. */
140 /* Pointer to the frag this symbol is attached to, if any.
147 /* Extra symbol fields that won't fit. */
151 /* Extra fields to make up a full symbol. */
155 /* The value of the symbol. */
158 /* Forwards and backwards chain pointers. */
160 struct symbol
*previous
;
162 #ifdef OBJ_SYMFIELD_TYPE
163 OBJ_SYMFIELD_TYPE obj
;
166 #ifdef TC_SYMFIELD_TYPE
171 typedef union symbol_entry
173 struct local_symbol lsy
;
177 /* Hash function for a symbol_entry. */
180 hash_symbol_entry (const void *e
)
182 symbol_entry_t
*entry
= (symbol_entry_t
*) e
;
183 if (entry
->sy
.hash
== 0)
184 entry
->sy
.hash
= htab_hash_string (entry
->sy
.name
);
186 return entry
->sy
.hash
;
189 /* Equality function for a symbol_entry. */
192 eq_symbol_entry (const void *a
, const void *b
)
194 const symbol_entry_t
*ea
= (const symbol_entry_t
*) a
;
195 const symbol_entry_t
*eb
= (const symbol_entry_t
*) b
;
197 return (ea
->sy
.hash
== eb
->sy
.hash
198 && strcmp (ea
->sy
.name
, eb
->sy
.name
) == 0);
202 symbol_entry_find (htab_t table
, const char *name
)
204 hashval_t hash
= htab_hash_string (name
);
205 symbol_entry_t needle
= { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
206 hash
, name
, 0, 0, 0 } };
207 return htab_find_with_hash (table
, &needle
, hash
);
211 /* This is non-zero if symbols are case sensitive, which is the
213 int symbols_case_sensitive
= 1;
215 #ifndef WORKING_DOT_WORD
216 extern int new_broken_words
;
219 static htab_t sy_hash
;
221 /* Below are commented in "symbols.h". */
222 symbolS
*symbol_rootP
;
223 symbolS
*symbol_lastP
;
225 struct xsymbol abs_symbol_x
;
227 struct xsymbol dot_symbol_x
;
230 #define debug_verify_symchain verify_symbol_chain
232 #define debug_verify_symchain(root, last) ((void) 0)
235 #define DOLLAR_LABEL_CHAR '\001'
236 #define LOCAL_LABEL_CHAR '\002'
238 #ifndef TC_LABEL_IS_LOCAL
239 #define TC_LABEL_IS_LOCAL(name) 0
242 struct obstack notes
;
244 /* The name of an external symbol which is
245 used to make weak PE symbol names unique. */
246 const char * an_external_name
;
249 /* Return a pointer to a new symbol. Die if we can't make a new
250 symbol. Fill in the symbol's values. Add symbol to end of symbol
253 This function should be called in the general case of creating a
254 symbol. However, if the output file symbol table has already been
255 set, and you are certain that this symbol won't be wanted in the
256 output file, you can call symbol_create. */
259 symbol_new (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
261 symbolS
*symbolP
= symbol_create (name
, segment
, frag
, valu
);
263 /* Link to end of symbol chain. */
264 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
269 /* Save a symbol name on a permanent obstack, and convert it according
270 to the object file format. */
273 save_symbol_name (const char *name
)
278 gas_assert (name
!= NULL
);
279 name_length
= strlen (name
) + 1; /* +1 for \0. */
280 obstack_grow (¬es
, name
, name_length
);
281 ret
= (char *) obstack_finish (¬es
);
283 #ifdef tc_canonicalize_symbol_name
284 ret
= tc_canonicalize_symbol_name (ret
);
287 if (! symbols_case_sensitive
)
291 for (s
= ret
; *s
!= '\0'; s
++)
299 symbol_init (symbolS
*symbolP
, const char *name
, asection
*sec
,
300 fragS
*frag
, valueT valu
)
302 symbolP
->frag
= frag
;
303 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
304 if (symbolP
->bsym
== NULL
)
305 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
306 symbolP
->bsym
->name
= name
;
307 symbolP
->bsym
->section
= sec
;
309 if (multibyte_handling
== multibyte_warn_syms
310 && ! symbolP
->flags
.local_symbol
311 && sec
!= undefined_section
312 && ! symbolP
->flags
.multibyte_warned
313 && scan_for_multibyte_characters ((const unsigned char *) name
,
314 (const unsigned char *) name
+ strlen (name
),
315 false /* Do not warn. */))
317 as_warn (_("symbol '%s' contains multibyte characters"), name
);
318 symbolP
->flags
.multibyte_warned
= 1;
321 S_SET_VALUE (symbolP
, valu
);
323 symbol_clear_list_pointers (symbolP
);
325 obj_symbol_new_hook (symbolP
);
327 #ifdef tc_symbol_new_hook
328 tc_symbol_new_hook (symbolP
);
332 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
335 symbol_create (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
337 const char *preserved_copy_of_name
;
341 preserved_copy_of_name
= save_symbol_name (name
);
343 size
= sizeof (symbolS
) + sizeof (struct xsymbol
);
344 symbolP
= (symbolS
*) obstack_alloc (¬es
, size
);
346 /* symbol must be born in some fixed state. This seems as good as any. */
347 memset (symbolP
, 0, size
);
348 symbolP
->name
= preserved_copy_of_name
;
349 symbolP
->x
= (struct xsymbol
*) (symbolP
+ 1);
351 symbol_init (symbolP
, preserved_copy_of_name
, segment
, frag
, valu
);
357 /* Local symbol support. If we can get away with it, we keep only a
358 small amount of information for local symbols. */
360 /* Used for statistics. */
362 static unsigned long local_symbol_count
;
363 static unsigned long local_symbol_conversion_count
;
365 /* Create a local symbol and insert it into the local hash table. */
367 struct local_symbol
*
368 local_symbol_make (const char *name
, segT section
, fragS
*frag
, valueT val
)
370 const char *name_copy
;
371 struct local_symbol
*ret
;
372 struct symbol_flags flags
= { .local_symbol
= 1, .resolved
= 0 };
374 ++local_symbol_count
;
376 name_copy
= save_symbol_name (name
);
378 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
381 ret
->name
= name_copy
;
383 ret
->section
= section
;
386 htab_insert (sy_hash
, ret
, 1);
391 /* Convert a local symbol into a real symbol. */
394 local_symbol_convert (void *sym
)
396 symbol_entry_t
*ent
= (symbol_entry_t
*) sym
;
397 struct xsymbol
*xtra
;
400 gas_assert (ent
->lsy
.flags
.local_symbol
);
402 ++local_symbol_conversion_count
;
404 xtra
= (struct xsymbol
*) obstack_alloc (¬es
, sizeof (*xtra
));
405 memset (xtra
, 0, sizeof (*xtra
));
406 val
= ent
->lsy
.value
;
409 /* Local symbols are always either defined or used. */
410 ent
->sy
.flags
.used
= 1;
411 ent
->sy
.flags
.local_symbol
= 0;
413 symbol_init (&ent
->sy
, ent
->lsy
.name
, ent
->lsy
.section
, ent
->lsy
.frag
, val
);
414 symbol_append (&ent
->sy
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
420 define_sym_at_dot (symbolS
*symbolP
)
422 symbolP
->frag
= frag_now
;
423 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
424 S_SET_SEGMENT (symbolP
, now_seg
);
427 /* We have just seen "<name>:".
428 Creates a struct symbol unless it already exists.
430 Gripes if we are redefining a symbol incompatibly (and ignores it). */
433 colon (/* Just seen "x:" - rattle symbols & frags. */
434 const char *sym_name
/* Symbol name, as a canonical string. */
435 /* We copy this string: OK to alter later. */)
437 symbolS
*symbolP
; /* Symbol we are working with. */
439 /* Sun local labels go out of scope whenever a non-local symbol is
441 if (LOCAL_LABELS_DOLLAR
442 && !bfd_is_local_label_name (stdoutput
, sym_name
))
443 dollar_label_clear ();
445 #ifndef WORKING_DOT_WORD
446 if (new_broken_words
)
448 struct broken_word
*a
;
453 if (now_seg
== absolute_section
)
455 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
459 possible_bytes
= (md_short_jump_size
460 + new_broken_words
* md_long_jump_size
);
463 frag_opcode
= frag_var (rs_broken_word
,
467 (symbolS
*) broken_words
,
471 /* We want to store the pointer to where to insert the jump
472 table in the fr_opcode of the rs_broken_word frag. This
473 requires a little hackery. */
475 && (frag_tmp
->fr_type
!= rs_broken_word
476 || frag_tmp
->fr_opcode
))
477 frag_tmp
= frag_tmp
->fr_next
;
479 frag_tmp
->fr_opcode
= frag_opcode
;
480 new_broken_words
= 0;
482 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
483 a
->dispfrag
= frag_tmp
;
485 #endif /* WORKING_DOT_WORD */
487 #ifdef obj_frob_colon
488 obj_frob_colon (sym_name
);
491 if ((symbolP
= symbol_find (sym_name
)) != 0)
493 S_CLEAR_WEAKREFR (symbolP
);
494 #ifdef RESOLVE_SYMBOL_REDEFINITION
495 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
498 /* Now check for undefined symbols. */
499 if (symbolP
->flags
.local_symbol
)
501 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
503 if (locsym
->section
!= undefined_section
504 && (locsym
->frag
!= frag_now
505 || locsym
->section
!= now_seg
506 || locsym
->value
!= frag_now_fix ()))
508 as_bad (_("symbol `%s' is already defined"), sym_name
);
512 locsym
->section
= now_seg
;
513 locsym
->frag
= frag_now
;
514 locsym
->value
= frag_now_fix ();
516 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
517 || S_IS_COMMON (symbolP
)
518 || S_IS_VOLATILE (symbolP
))
520 if (S_IS_VOLATILE (symbolP
))
522 symbolP
= symbol_clone (symbolP
, 1);
523 S_SET_VALUE (symbolP
, 0);
524 S_CLEAR_VOLATILE (symbolP
);
526 if (S_GET_VALUE (symbolP
) == 0)
528 define_sym_at_dot (symbolP
);
531 #endif /* if we have one, it better be zero. */
536 /* There are still several cases to check:
538 A .comm/.lcomm symbol being redefined as initialized
541 A .comm/.lcomm symbol being redefined with a larger
544 This only used to be allowed on VMS gas, but Sun cc
545 on the sparc also depends on it. */
547 if (((!S_IS_DEBUG (symbolP
)
548 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
549 && S_IS_EXTERNAL (symbolP
))
550 || S_GET_SEGMENT (symbolP
) == bss_section
)
551 && (now_seg
== data_section
552 || now_seg
== bss_section
553 || now_seg
== S_GET_SEGMENT (symbolP
)))
555 /* Select which of the 2 cases this is. */
556 if (now_seg
!= data_section
)
558 /* New .comm for prev .comm symbol.
560 If the new size is larger we just change its
561 value. If the new size is smaller, we ignore
563 if (S_GET_VALUE (symbolP
)
564 < ((unsigned) frag_now_fix ()))
566 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
571 /* It is a .comm/.lcomm being converted to initialized
573 define_sym_at_dot (symbolP
);
578 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
579 static const char *od_buf
= "";
583 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
584 sprintf (od_buf
, "%d.%d.",
585 S_GET_OTHER (symbolP
),
586 S_GET_DESC (symbolP
));
588 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
590 segment_name (S_GET_SEGMENT (symbolP
)),
592 (long) S_GET_VALUE (symbolP
));
594 } /* if the undefined symbol has no value */
598 /* Don't blow up if the definition is the same. */
599 if (!(frag_now
== symbolP
->frag
600 && S_GET_VALUE (symbolP
) == frag_now_fix ()
601 && S_GET_SEGMENT (symbolP
) == now_seg
))
603 as_bad (_("symbol `%s' is already defined"), sym_name
);
604 symbolP
= symbol_clone (symbolP
, 0);
605 define_sym_at_dot (symbolP
);
610 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
612 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
, frag_now
,
617 symbolP
= symbol_new (sym_name
, now_seg
, frag_now
, frag_now_fix ());
619 symbol_table_insert (symbolP
);
622 if (mri_common_symbol
!= NULL
)
624 /* This symbol is actually being defined within an MRI common
625 section. This requires special handling. */
626 if (symbolP
->flags
.local_symbol
)
627 symbolP
= local_symbol_convert (symbolP
);
628 symbolP
->x
->value
.X_op
= O_symbol
;
629 symbolP
->x
->value
.X_add_symbol
= mri_common_symbol
;
630 symbolP
->x
->value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
631 symbolP
->frag
= &zero_address_frag
;
632 S_SET_SEGMENT (symbolP
, expr_section
);
633 symbolP
->flags
.mri_common
= 1;
637 tc_frob_label (symbolP
);
639 #ifdef obj_frob_label
640 obj_frob_label (symbolP
);
646 /* Die if we can't insert the symbol. */
649 symbol_table_insert (symbolS
*symbolP
)
653 htab_insert (sy_hash
, symbolP
, 1);
656 /* If a symbol name does not exist, create it as undefined, and insert
657 it into the symbol table. Return a pointer to it. */
660 symbol_find_or_make (const char *name
)
664 symbolP
= symbol_find (name
);
668 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
670 symbolP
= md_undefined_symbol ((char *) name
);
674 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
675 &zero_address_frag
, 0);
679 symbolP
= symbol_make (name
);
681 symbol_table_insert (symbolP
);
682 } /* if symbol wasn't found */
688 symbol_make (const char *name
)
692 /* Let the machine description default it, e.g. for register names. */
693 symbolP
= md_undefined_symbol ((char *) name
);
696 symbolP
= symbol_new (name
, undefined_section
, &zero_address_frag
, 0);
702 symbol_clone (symbolS
*orgsymP
, int replace
)
705 asymbol
*bsymorg
, *bsymnew
;
707 /* Make sure we never clone the dot special symbol. */
708 gas_assert (orgsymP
!= &dot_symbol
);
710 /* When cloning a local symbol it isn't absolutely necessary to
711 convert the original, but converting makes the code much
712 simpler to cover this unexpected case. As of 2020-08-21
713 symbol_clone won't be called on a local symbol. */
714 if (orgsymP
->flags
.local_symbol
)
715 orgsymP
= local_symbol_convert (orgsymP
);
716 bsymorg
= orgsymP
->bsym
;
718 newsymP
= (symbolS
*) obstack_alloc (¬es
, (sizeof (symbolS
)
719 + sizeof (struct xsymbol
)));
721 newsymP
->x
= (struct xsymbol
*) (newsymP
+ 1);
722 *newsymP
->x
= *orgsymP
->x
;
723 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
725 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
726 newsymP
->bsym
= bsymnew
;
727 bsymnew
->name
= bsymorg
->name
;
728 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
729 bsymnew
->section
= bsymorg
->section
;
730 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
731 bfd_asymbol_bfd (bsymnew
), bsymnew
);
733 #ifdef obj_symbol_clone_hook
734 obj_symbol_clone_hook (newsymP
, orgsymP
);
737 #ifdef tc_symbol_clone_hook
738 tc_symbol_clone_hook (newsymP
, orgsymP
);
743 if (symbol_rootP
== orgsymP
)
744 symbol_rootP
= newsymP
;
745 else if (orgsymP
->x
->previous
)
747 orgsymP
->x
->previous
->x
->next
= newsymP
;
748 orgsymP
->x
->previous
= NULL
;
750 if (symbol_lastP
== orgsymP
)
751 symbol_lastP
= newsymP
;
752 else if (orgsymP
->x
->next
)
753 orgsymP
->x
->next
->x
->previous
= newsymP
;
755 /* Symbols that won't be output can't be external. */
756 S_CLEAR_EXTERNAL (orgsymP
);
757 orgsymP
->x
->previous
= orgsymP
->x
->next
= orgsymP
;
758 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
760 symbol_table_insert (newsymP
);
764 /* Symbols that won't be output can't be external. */
765 S_CLEAR_EXTERNAL (newsymP
);
766 newsymP
->x
->previous
= newsymP
->x
->next
= newsymP
;
772 /* Referenced symbols, if they are forward references, need to be cloned
773 (without replacing the original) so that the value of the referenced
774 symbols at the point of use is saved by the clone. */
776 #undef symbol_clone_if_forward_ref
778 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
780 if (symbolP
&& !symbolP
->flags
.local_symbol
)
782 symbolS
*orig_add_symbol
= symbolP
->x
->value
.X_add_symbol
;
783 symbolS
*orig_op_symbol
= symbolP
->x
->value
.X_op_symbol
;
784 symbolS
*add_symbol
= orig_add_symbol
;
785 symbolS
*op_symbol
= orig_op_symbol
;
787 if (symbolP
->flags
.forward_ref
)
792 /* assign_symbol() clones volatile symbols; pre-existing expressions
793 hold references to the original instance, but want the current
794 value. Just repeat the lookup. */
795 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
796 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
797 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
798 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
801 /* Re-using resolving here, as this routine cannot get called from
802 symbol resolution code. */
803 if ((symbolP
->bsym
->section
== expr_section
804 || symbolP
->flags
.forward_ref
)
805 && !symbolP
->flags
.resolving
)
807 symbolP
->flags
.resolving
= 1;
808 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
809 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
810 symbolP
->flags
.resolving
= 0;
813 if (symbolP
->flags
.forward_ref
814 || add_symbol
!= orig_add_symbol
815 || op_symbol
!= orig_op_symbol
)
817 if (symbolP
!= &dot_symbol
)
819 symbolP
= symbol_clone (symbolP
, 0);
820 symbolP
->flags
.resolving
= 0;
824 symbolP
= symbol_temp_new_now ();
825 #ifdef tc_new_dot_label
826 tc_new_dot_label (symbolP
);
831 symbolP
->x
->value
.X_add_symbol
= add_symbol
;
832 symbolP
->x
->value
.X_op_symbol
= op_symbol
;
839 symbol_temp_new (segT seg
, fragS
*frag
, valueT ofs
)
841 return symbol_new (FAKE_LABEL_NAME
, seg
, frag
, ofs
);
845 symbol_temp_new_now (void)
847 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix ());
851 symbol_temp_new_now_octets (void)
853 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix_octets ());
857 symbol_temp_make (void)
859 return symbol_make (FAKE_LABEL_NAME
);
862 /* Implement symbol table lookup.
863 In: A symbol's name as a string: '\0' can't be part of a symbol name.
864 Out: NULL if the name was not in the symbol table, else the address
865 of a struct symbol associated with that name. */
868 symbol_find_exact (const char *name
)
870 return symbol_find_exact_noref (name
, 0);
874 symbol_find_exact_noref (const char *name
, int noref
)
876 symbolS
*sym
= symbol_entry_find (sy_hash
, name
);
878 /* Any references to the symbol, except for the reference in
879 .weakref, must clear this flag, such that the symbol does not
880 turn into a weak symbol. Note that we don't have to handle the
881 local_symbol case, since a weakrefd is always promoted out of the
882 local_symbol table when it is turned into a weak symbol. */
884 S_CLEAR_WEAKREFD (sym
);
890 symbol_find (const char *name
)
892 return symbol_find_noref (name
, 0);
896 symbol_find_noref (const char *name
, int noref
)
901 #ifdef tc_canonicalize_symbol_name
903 copy
= xstrdup (name
);
904 name
= tc_canonicalize_symbol_name (copy
);
908 if (! symbols_case_sensitive
)
917 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
919 while ((c
= *orig
++) != '\0')
920 *copy
++ = TOUPPER (c
);
924 copy
= (char *) name
;
927 result
= symbol_find_exact_noref (name
, noref
);
932 /* Once upon a time, symbols were kept in a singly linked list. At
933 least coff needs to be able to rearrange them from time to time, for
934 which a doubly linked list is much more convenient. Loic did these
935 as macros which seemed dangerous to me so they're now functions.
938 /* Link symbol ADDME after symbol TARGET in the chain. */
941 symbol_append (symbolS
*addme
, symbolS
*target
,
942 symbolS
**rootPP
, symbolS
**lastPP
)
944 extern int symbol_table_frozen
;
945 if (symbol_table_frozen
)
947 if (addme
->flags
.local_symbol
)
949 if (target
!= NULL
&& target
->flags
.local_symbol
)
954 know (*rootPP
== NULL
);
955 know (*lastPP
== NULL
);
956 addme
->x
->next
= NULL
;
957 addme
->x
->previous
= NULL
;
961 } /* if the list is empty */
963 if (target
->x
->next
!= NULL
)
965 target
->x
->next
->x
->previous
= addme
;
969 know (*lastPP
== target
);
971 } /* if we have a next */
973 addme
->x
->next
= target
->x
->next
;
974 target
->x
->next
= addme
;
975 addme
->x
->previous
= target
;
977 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
980 /* Set the chain pointers of SYMBOL to null. */
983 symbol_clear_list_pointers (symbolS
*symbolP
)
985 if (symbolP
->flags
.local_symbol
)
987 symbolP
->x
->next
= NULL
;
988 symbolP
->x
->previous
= NULL
;
991 /* Remove SYMBOLP from the list. */
994 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
996 if (symbolP
->flags
.local_symbol
)
999 if (symbolP
== *rootPP
)
1001 *rootPP
= symbolP
->x
->next
;
1002 } /* if it was the root */
1004 if (symbolP
== *lastPP
)
1006 *lastPP
= symbolP
->x
->previous
;
1007 } /* if it was the tail */
1009 if (symbolP
->x
->next
!= NULL
)
1011 symbolP
->x
->next
->x
->previous
= symbolP
->x
->previous
;
1014 if (symbolP
->x
->previous
!= NULL
)
1016 symbolP
->x
->previous
->x
->next
= symbolP
->x
->next
;
1017 } /* if not first */
1019 debug_verify_symchain (*rootPP
, *lastPP
);
1022 /* Link symbol ADDME before symbol TARGET in the chain. */
1025 symbol_insert (symbolS
*addme
, symbolS
*target
,
1026 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
1028 extern int symbol_table_frozen
;
1029 if (symbol_table_frozen
)
1031 if (addme
->flags
.local_symbol
)
1033 if (target
->flags
.local_symbol
)
1036 if (target
->x
->previous
!= NULL
)
1038 target
->x
->previous
->x
->next
= addme
;
1042 know (*rootPP
== target
);
1044 } /* if not first */
1046 addme
->x
->previous
= target
->x
->previous
;
1047 target
->x
->previous
= addme
;
1048 addme
->x
->next
= target
;
1050 debug_verify_symchain (*rootPP
, *lastPP
);
1054 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
1056 symbolS
*symbolP
= rootP
;
1058 if (symbolP
== NULL
)
1061 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
1063 gas_assert (symbolP
->bsym
!= NULL
);
1064 gas_assert (symbolP
->flags
.local_symbol
== 0);
1065 gas_assert (symbolP
->x
->next
->x
->previous
== symbolP
);
1068 gas_assert (lastP
== symbolP
);
1072 symbol_on_chain (symbolS
*s
, symbolS
*rootPP
, symbolS
*lastPP
)
1074 return (!s
->flags
.local_symbol
1075 && ((s
->x
->next
!= s
1076 && s
->x
->next
!= NULL
1077 && s
->x
->next
->x
->previous
== s
)
1079 && ((s
->x
->previous
!= s
1080 && s
->x
->previous
!= NULL
1081 && s
->x
->previous
->x
->next
== s
)
1085 #ifdef OBJ_COMPLEX_RELC
1088 use_complex_relocs_for (symbolS
* symp
)
1090 switch (symp
->x
->value
.X_op
)
1100 case O_bit_inclusive_or
:
1102 case O_bit_exclusive_or
:
1114 if ((S_IS_COMMON (symp
->x
->value
.X_op_symbol
)
1115 || S_IS_LOCAL (symp
->x
->value
.X_op_symbol
))
1116 && S_IS_DEFINED (symp
->x
->value
.X_op_symbol
)
1117 && S_GET_SEGMENT (symp
->x
->value
.X_op_symbol
) != expr_section
)
1124 if ((S_IS_COMMON (symp
->x
->value
.X_add_symbol
)
1125 || S_IS_LOCAL (symp
->x
->value
.X_add_symbol
))
1126 && S_IS_DEFINED (symp
->x
->value
.X_add_symbol
)
1127 && S_GET_SEGMENT (symp
->x
->value
.X_add_symbol
) != expr_section
)
1140 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
1144 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1145 segT seg_right
= S_GET_SEGMENT (right
);
1154 case O_uminus
: opname
= "-"; break;
1155 case O_bit_not
: opname
= "~"; break;
1156 case O_logical_not
: opname
= "!"; break;
1157 case O_multiply
: opname
= "*"; break;
1158 case O_divide
: opname
= "/"; break;
1159 case O_modulus
: opname
= "%"; break;
1160 case O_left_shift
: opname
= "<<"; break;
1161 case O_right_shift
: opname
= ">>"; break;
1162 case O_bit_inclusive_or
: opname
= "|"; break;
1163 case O_bit_or_not
: opname
= "|~"; break;
1164 case O_bit_exclusive_or
: opname
= "^"; break;
1165 case O_bit_and
: opname
= "&"; break;
1166 case O_add
: opname
= "+"; break;
1167 case O_subtract
: opname
= "-"; break;
1168 case O_eq
: opname
= "=="; break;
1169 case O_ne
: opname
= "!="; break;
1170 case O_lt
: opname
= "<"; break;
1171 case O_le
: opname
= "<="; break;
1172 case O_ge
: opname
= ">="; break;
1173 case O_gt
: opname
= ">"; break;
1174 case O_logical_and
: opname
= "&&"; break;
1175 case O_logical_or
: opname
= "||"; break;
1178 if (expr_symbol_where (symp
, &file
, &line
))
1181 as_bad_where (file
, line
,
1182 _("invalid operands (%s and %s sections) for `%s'"),
1183 seg_left
->name
, seg_right
->name
, opname
);
1185 as_bad_where (file
, line
,
1186 _("invalid operand (%s section) for `%s'"),
1187 seg_right
->name
, opname
);
1191 const char *sname
= S_GET_NAME (symp
);
1194 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1195 seg_left
->name
, seg_right
->name
, opname
, sname
);
1197 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1198 seg_right
->name
, opname
, sname
);
1202 /* Resolve the value of a symbol. This is called during the final
1203 pass over the symbol table to resolve any symbols with complex
1207 resolve_symbol_value (symbolS
*symp
)
1213 if (symp
->flags
.local_symbol
)
1215 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1217 final_val
= locsym
->value
;
1218 if (locsym
->flags
.resolved
)
1221 /* Symbols whose section has SEC_ELF_OCTETS set,
1222 resolve to octets instead of target bytes. */
1223 if (locsym
->section
->flags
& SEC_OCTETS
)
1224 final_val
+= locsym
->frag
->fr_address
;
1226 final_val
+= locsym
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1230 locsym
->value
= final_val
;
1231 locsym
->flags
.resolved
= 1;
1237 if (symp
->flags
.resolved
)
1240 while (symp
->x
->value
.X_op
== O_symbol
)
1242 final_val
+= symp
->x
->value
.X_add_number
;
1243 symp
= symp
->x
->value
.X_add_symbol
;
1244 if (symp
->flags
.local_symbol
)
1246 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1247 final_val
+= locsym
->value
;
1250 if (!symp
->flags
.resolved
)
1253 if (symp
->x
->value
.X_op
== O_constant
)
1254 final_val
+= symp
->x
->value
.X_add_number
;
1261 final_seg
= S_GET_SEGMENT (symp
);
1263 if (symp
->flags
.resolving
)
1266 as_bad (_("symbol definition loop encountered at `%s'"),
1271 #ifdef OBJ_COMPLEX_RELC
1272 else if (final_seg
== expr_section
1273 && use_complex_relocs_for (symp
))
1275 symbolS
* relc_symbol
= NULL
;
1276 char * relc_symbol_name
= NULL
;
1278 relc_symbol_name
= symbol_relc_make_expr (& symp
->x
->value
);
1280 /* For debugging, print out conversion input & output. */
1282 print_expr (& symp
->x
->value
);
1283 if (relc_symbol_name
)
1284 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1287 if (relc_symbol_name
!= NULL
)
1288 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1289 &zero_address_frag
, 0);
1291 if (relc_symbol
== NULL
)
1293 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1299 symbol_table_insert (relc_symbol
);
1301 /* S_CLEAR_EXTERNAL (relc_symbol); */
1302 if (symp
->bsym
->flags
& BSF_SRELC
)
1303 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1305 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1306 /* symp->bsym->flags |= BSF_RELC; */
1307 copy_symbol_attributes (symp
, relc_symbol
);
1308 symp
->x
->value
.X_op
= O_symbol
;
1309 symp
->x
->value
.X_add_symbol
= relc_symbol
;
1310 symp
->x
->value
.X_add_number
= 0;
1315 final_seg
= undefined_section
;
1316 goto exit_dont_set_value
;
1321 symbolS
*add_symbol
, *op_symbol
;
1322 offsetT left
, right
;
1323 segT seg_left
, seg_right
;
1327 symp
->flags
.resolving
= 1;
1329 /* Help out with CSE. */
1330 add_symbol
= symp
->x
->value
.X_add_symbol
;
1331 op_symbol
= symp
->x
->value
.X_op_symbol
;
1332 final_val
= symp
->x
->value
.X_add_number
;
1333 op
= symp
->x
->value
.X_op
;
1346 /* Symbols whose section has SEC_ELF_OCTETS set,
1347 resolve to octets instead of target bytes. */
1348 if (symp
->bsym
->section
->flags
& SEC_OCTETS
)
1349 final_val
+= symp
->frag
->fr_address
;
1351 final_val
+= symp
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1352 if (final_seg
== expr_section
)
1353 final_seg
= absolute_section
;
1362 left
= resolve_symbol_value (add_symbol
);
1363 seg_left
= S_GET_SEGMENT (add_symbol
);
1365 symp
->x
->value
.X_op_symbol
= NULL
;
1368 if (S_IS_WEAKREFR (symp
))
1370 gas_assert (final_val
== 0);
1371 if (S_IS_WEAKREFR (add_symbol
))
1373 gas_assert (add_symbol
->x
->value
.X_op
== O_symbol
1374 && add_symbol
->x
->value
.X_add_number
== 0);
1375 add_symbol
= add_symbol
->x
->value
.X_add_symbol
;
1376 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1377 symp
->x
->value
.X_add_symbol
= add_symbol
;
1381 if (symp
->flags
.mri_common
)
1383 /* This is a symbol inside an MRI common section. The
1384 relocation routines are going to handle it specially.
1385 Don't change the value. */
1386 resolved
= symbol_resolved_p (add_symbol
);
1390 /* Don't leave symbol loops. */
1392 && !add_symbol
->flags
.local_symbol
1393 && add_symbol
->flags
.resolving
)
1396 if (finalize_syms
&& final_val
== 0
1398 /* Avoid changing symp's "within" when dealing with
1399 AIX debug symbols. For some storage classes, "within"
1400 have a special meaning.
1401 C_DWARF should behave like on Linux, thus this check
1402 isn't done to be closer. */
1403 && ((symbol_get_bfdsym (symp
)->flags
& BSF_DEBUGGING
) == 0
1404 || (S_GET_STORAGE_CLASS (symp
) == C_DWARF
))
1408 if (add_symbol
->flags
.local_symbol
)
1409 add_symbol
= local_symbol_convert (add_symbol
);
1410 copy_symbol_attributes (symp
, add_symbol
);
1413 /* If we have equated this symbol to an undefined or common
1414 symbol, keep X_op set to O_symbol, and don't change
1415 X_add_number. This permits the routine which writes out
1416 relocation to detect this case, and convert the
1417 relocation to be against the symbol to which this symbol
1419 if (seg_left
== undefined_section
1420 || bfd_is_com_section (seg_left
)
1421 #if defined (OBJ_COFF) && defined (TE_PE)
1422 || S_IS_WEAK (add_symbol
)
1425 && ((final_seg
== expr_section
1426 && seg_left
!= expr_section
1427 && seg_left
!= absolute_section
)
1428 || symbol_shadow_p (symp
))))
1432 symp
->x
->value
.X_op
= O_symbol
;
1433 symp
->x
->value
.X_add_symbol
= add_symbol
;
1434 symp
->x
->value
.X_add_number
= final_val
;
1435 /* Use X_op_symbol as a flag. */
1436 symp
->x
->value
.X_op_symbol
= add_symbol
;
1438 final_seg
= seg_left
;
1439 final_val
+= symp
->frag
->fr_address
+ left
;
1440 resolved
= symbol_resolved_p (add_symbol
);
1441 symp
->flags
.resolving
= 0;
1442 goto exit_dont_set_value
;
1446 final_val
+= symp
->frag
->fr_address
+ left
;
1447 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1448 final_seg
= seg_left
;
1451 resolved
= symbol_resolved_p (add_symbol
);
1452 if (S_IS_WEAKREFR (symp
))
1454 symp
->flags
.resolving
= 0;
1455 goto exit_dont_set_value
;
1462 left
= resolve_symbol_value (add_symbol
);
1463 seg_left
= S_GET_SEGMENT (add_symbol
);
1465 /* By reducing these to the relevant dyadic operator, we get
1466 !S -> S == 0 permitted on anything,
1467 -S -> 0 - S only permitted on absolute
1468 ~S -> S ^ ~0 only permitted on absolute */
1469 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1471 report_op_error (symp
, NULL
, op
, add_symbol
);
1473 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1474 final_seg
= absolute_section
;
1478 else if (op
== O_logical_not
)
1483 final_val
+= left
+ symp
->frag
->fr_address
;
1485 resolved
= symbol_resolved_p (add_symbol
);
1493 case O_bit_inclusive_or
:
1495 case O_bit_exclusive_or
:
1507 left
= resolve_symbol_value (add_symbol
);
1508 right
= resolve_symbol_value (op_symbol
);
1509 seg_left
= S_GET_SEGMENT (add_symbol
);
1510 seg_right
= S_GET_SEGMENT (op_symbol
);
1512 /* Simplify addition or subtraction of a constant by folding the
1513 constant into X_add_number. */
1516 if (seg_right
== absolute_section
)
1521 else if (seg_left
== absolute_section
)
1524 add_symbol
= op_symbol
;
1526 seg_left
= seg_right
;
1530 else if (op
== O_subtract
)
1532 if (seg_right
== absolute_section
)
1540 /* Equality and non-equality tests are permitted on anything.
1541 Subtraction, and other comparison operators are permitted if
1542 both operands are in the same section. Otherwise, both
1543 operands must be absolute. We already handled the case of
1544 addition or subtraction of a constant above. This will
1545 probably need to be changed for an object file format which
1546 supports arbitrary expressions. */
1547 if (!(seg_left
== absolute_section
1548 && seg_right
== absolute_section
)
1549 && !(op
== O_eq
|| op
== O_ne
)
1550 && !((op
== O_subtract
1551 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1552 && seg_left
== seg_right
1553 && (seg_left
!= undefined_section
1554 || add_symbol
== op_symbol
)))
1556 /* Don't emit messages unless we're finalizing the symbol value,
1557 otherwise we may get the same message multiple times. */
1559 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1560 /* However do not move the symbol into the absolute section
1561 if it cannot currently be resolved - this would confuse
1562 other parts of the assembler into believing that the
1563 expression had been evaluated to zero. */
1569 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1570 final_seg
= absolute_section
;
1572 /* Check for division by zero. */
1573 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1575 /* If seg_right is not absolute_section, then we've
1576 already issued a warning about using a bad symbol. */
1577 if (seg_right
== absolute_section
&& finalize_syms
)
1582 if (expr_symbol_where (symp
, &file
, &line
))
1583 as_bad_where (file
, line
, _("division by zero"));
1585 as_bad (_("division by zero when setting `%s'"),
1591 if ((op
== O_left_shift
|| op
== O_right_shift
)
1592 && (valueT
) right
>= sizeof (valueT
) * CHAR_BIT
)
1594 as_warn_value_out_of_range (_("shift count"), right
, 0,
1595 sizeof (valueT
) * CHAR_BIT
- 1,
1600 switch (symp
->x
->value
.X_op
)
1602 case O_multiply
: left
*= right
; break;
1603 case O_divide
: left
/= right
; break;
1604 case O_modulus
: left
%= right
; break;
1606 left
= (valueT
) left
<< (valueT
) right
; break;
1608 left
= (valueT
) left
>> (valueT
) right
; break;
1609 case O_bit_inclusive_or
: left
|= right
; break;
1610 case O_bit_or_not
: left
|= ~right
; break;
1611 case O_bit_exclusive_or
: left
^= right
; break;
1612 case O_bit_and
: left
&= right
; break;
1613 case O_add
: left
+= right
; break;
1614 case O_subtract
: left
-= right
; break;
1617 left
= (left
== right
&& seg_left
== seg_right
1618 && (seg_left
!= undefined_section
1619 || add_symbol
== op_symbol
)
1620 ? ~ (offsetT
) 0 : 0);
1621 if (symp
->x
->value
.X_op
== O_ne
)
1624 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1625 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1626 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1627 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1628 case O_logical_and
: left
= left
&& right
; break;
1629 case O_logical_or
: left
= left
|| right
; break;
1634 /* See PR 20895 for a reproducer. */
1635 as_bad (_("Invalid operation on symbol"));
1636 goto exit_dont_set_value
;
1642 final_val
+= symp
->frag
->fr_address
+ left
;
1643 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1645 if (seg_left
== undefined_section
1646 || seg_right
== undefined_section
)
1647 final_seg
= undefined_section
;
1648 else if (seg_left
== absolute_section
)
1649 final_seg
= seg_right
;
1651 final_seg
= seg_left
;
1653 resolved
= (symbol_resolved_p (add_symbol
)
1654 && symbol_resolved_p (op_symbol
));
1659 /* Give an error (below) if not in expr_section. We don't
1660 want to worry about expr_section symbols, because they
1661 are fictional (they are created as part of expression
1662 resolution), and any problems may not actually mean
1667 symp
->flags
.resolving
= 0;
1671 S_SET_VALUE (symp
, final_val
);
1673 exit_dont_set_value
:
1674 /* Always set the segment, even if not finalizing the value.
1675 The segment is used to determine whether a symbol is defined. */
1676 S_SET_SEGMENT (symp
, final_seg
);
1678 /* Don't worry if we can't resolve an expr_section symbol. */
1682 symp
->flags
.resolved
= 1;
1683 else if (S_GET_SEGMENT (symp
) != expr_section
)
1685 as_bad (_("can't resolve value for symbol `%s'"),
1687 symp
->flags
.resolved
= 1;
1694 /* A static function passed to hash_traverse. */
1697 resolve_local_symbol (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1699 symbol_entry_t
*entry
= *((symbol_entry_t
**) slot
);
1700 if (entry
->sy
.flags
.local_symbol
)
1701 resolve_symbol_value (&entry
->sy
);
1706 /* Resolve all local symbols. */
1709 resolve_local_symbol_values (void)
1711 htab_traverse (sy_hash
, resolve_local_symbol
, NULL
);
1714 /* Obtain the current value of a symbol without changing any
1715 sub-expressions used. */
1718 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1720 symbolS
*symbolP
= *symbolPP
;
1722 if (symbolP
->flags
.local_symbol
)
1724 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1726 *valueP
= locsym
->value
;
1727 *segP
= locsym
->section
;
1728 *fragPP
= locsym
->frag
;
1732 expressionS exp
= symbolP
->x
->value
;
1734 if (!symbolP
->flags
.resolved
&& exp
.X_op
!= O_illegal
)
1738 if (symbolP
->flags
.resolving
)
1740 symbolP
->flags
.resolving
= 1;
1741 resolved
= resolve_expression (&exp
);
1742 symbolP
->flags
.resolving
= 0;
1750 if (!symbol_equated_p (symbolP
))
1755 symbolP
= exp
.X_add_symbol
;
1762 *symbolPP
= symbolP
;
1764 /* A bogus input file can result in resolve_expression()
1765 generating a local symbol, so we have to check again. */
1766 if (symbolP
->flags
.local_symbol
)
1768 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1770 *valueP
= locsym
->value
;
1771 *segP
= locsym
->section
;
1772 *fragPP
= locsym
->frag
;
1776 *valueP
= exp
.X_add_number
;
1777 *segP
= symbolP
->bsym
->section
;
1778 *fragPP
= symbolP
->frag
;
1781 if (*segP
== expr_section
)
1784 case O_constant
: *segP
= absolute_section
; break;
1785 case O_register
: *segP
= reg_section
; break;
1793 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1794 They are *really* local. That is, they go out of scope whenever we see a
1795 label that isn't local. Also, like fb labels, there can be multiple
1796 instances of a dollar label. Therefor, we name encode each instance with
1797 the instance number, keep a list of defined symbols separate from the real
1798 symbol table, and we treat these buggers as a sparse array. */
1800 typedef unsigned int dollar_ent
;
1801 static dollar_ent
*dollar_labels
;
1802 static dollar_ent
*dollar_label_instances
;
1803 static char *dollar_label_defines
;
1804 static size_t dollar_label_count
;
1805 static size_t dollar_label_max
;
1808 dollar_label_defined (unsigned int label
)
1812 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1814 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1816 return dollar_label_defines
[i
- dollar_labels
];
1818 /* If we get here, label isn't defined. */
1823 dollar_label_instance (unsigned int label
)
1827 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1829 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1831 return (dollar_label_instances
[i
- dollar_labels
]);
1833 /* If we get here, we haven't seen the label before.
1834 Therefore its instance count is zero. */
1839 dollar_label_clear (void)
1841 if (dollar_label_count
)
1842 memset (dollar_label_defines
, '\0', dollar_label_count
);
1845 #define DOLLAR_LABEL_BUMP_BY 10
1848 define_dollar_label (unsigned int label
)
1852 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1855 ++dollar_label_instances
[i
- dollar_labels
];
1856 dollar_label_defines
[i
- dollar_labels
] = 1;
1860 /* If we get to here, we don't have label listed yet. */
1862 if (dollar_labels
== NULL
)
1864 dollar_labels
= XNEWVEC (dollar_ent
, DOLLAR_LABEL_BUMP_BY
);
1865 dollar_label_instances
= XNEWVEC (dollar_ent
, DOLLAR_LABEL_BUMP_BY
);
1866 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1867 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1868 dollar_label_count
= 0;
1870 else if (dollar_label_count
== dollar_label_max
)
1872 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1873 dollar_labels
= XRESIZEVEC (dollar_ent
, dollar_labels
,
1875 dollar_label_instances
= XRESIZEVEC (dollar_ent
,
1876 dollar_label_instances
,
1878 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1880 } /* if we needed to grow */
1882 dollar_labels
[dollar_label_count
] = label
;
1883 dollar_label_instances
[dollar_label_count
] = 1;
1884 dollar_label_defines
[dollar_label_count
] = 1;
1885 ++dollar_label_count
;
1888 /* Caller must copy returned name: we re-use the area for the next name.
1890 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1891 where n is the label number and m is the instance number. "L" makes
1892 it a label discarded unless debugging and "^A"('\1') ensures no
1893 ordinary symbol SHOULD get the same name as a local label
1894 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1896 fb labels get the same treatment, except that ^B is used in place
1899 AUGEND is 0 for current instance, 1 for new instance. */
1902 dollar_label_name (unsigned int n
, unsigned int augend
)
1904 /* Returned to caller, then copied. Used for created names ("4f"). */
1905 static char symbol_name_build
[24];
1906 char *p
= symbol_name_build
;
1908 #ifdef LOCAL_LABEL_PREFIX
1909 *p
++ = LOCAL_LABEL_PREFIX
;
1911 sprintf (p
, "L%u%c%u",
1912 n
, DOLLAR_LABEL_CHAR
, dollar_label_instance (n
) + augend
);
1913 return symbol_name_build
;
1916 /* Somebody else's idea of local labels. They are made by "n:" where n
1917 is any decimal digit. Refer to them with
1918 "nb" for previous (backward) n:
1919 or "nf" for next (forward) n:.
1921 We do a little better and let n be any number, not just a single digit, but
1922 since the other guy's assembler only does ten, we treat the first ten
1925 Like someone else's assembler, we have one set of local label counters for
1926 entire assembly, not one set per (sub)segment like in most assemblers. This
1927 implies that one can refer to a label in another segment, and indeed some
1928 crufty compilers have done just that.
1930 Since there could be a LOT of these things, treat them as a sparse
1933 #define FB_LABEL_SPECIAL (10)
1935 typedef unsigned int fb_ent
;
1936 static fb_ent fb_low_counter
[FB_LABEL_SPECIAL
];
1937 static fb_ent
*fb_labels
;
1938 static fb_ent
*fb_label_instances
;
1939 static size_t fb_label_count
;
1940 static size_t fb_label_max
;
1942 /* This must be more than FB_LABEL_SPECIAL. */
1943 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1946 fb_label_init (void)
1948 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1951 /* Add one to the instance number of this fb label. */
1954 fb_label_instance_inc (unsigned int label
)
1958 if (label
< FB_LABEL_SPECIAL
)
1960 ++fb_low_counter
[label
];
1964 if (fb_labels
!= NULL
)
1966 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1967 i
< fb_labels
+ fb_label_count
; ++i
)
1971 ++fb_label_instances
[i
- fb_labels
];
1973 } /* if we find it */
1974 } /* for each existing label */
1977 /* If we get to here, we don't have label listed yet. */
1979 if (fb_labels
== NULL
)
1981 fb_labels
= XNEWVEC (fb_ent
, FB_LABEL_BUMP_BY
);
1982 fb_label_instances
= XNEWVEC (fb_ent
, FB_LABEL_BUMP_BY
);
1983 fb_label_max
= FB_LABEL_BUMP_BY
;
1984 fb_label_count
= FB_LABEL_SPECIAL
;
1987 else if (fb_label_count
== fb_label_max
)
1989 fb_label_max
+= FB_LABEL_BUMP_BY
;
1990 fb_labels
= XRESIZEVEC (fb_ent
, fb_labels
, fb_label_max
);
1991 fb_label_instances
= XRESIZEVEC (fb_ent
, fb_label_instances
,
1993 } /* if we needed to grow */
1995 fb_labels
[fb_label_count
] = label
;
1996 fb_label_instances
[fb_label_count
] = 1;
2001 fb_label_instance (unsigned int label
)
2005 if (label
< FB_LABEL_SPECIAL
)
2006 return (fb_low_counter
[label
]);
2008 if (fb_labels
!= NULL
)
2010 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
2011 i
< fb_labels
+ fb_label_count
; ++i
)
2014 return (fb_label_instances
[i
- fb_labels
]);
2018 /* We didn't find the label, so this must be a reference to the
2023 /* Caller must copy returned name: we re-use the area for the next name.
2025 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2026 where n is the label number and m is the instance number. "L" makes
2027 it a label discarded unless debugging and "^B"('\2') ensures no
2028 ordinary symbol SHOULD get the same name as a local label
2029 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2031 dollar labels get the same treatment, except that ^A is used in
2034 AUGEND is 0 for nb, 1 for n:, nf. */
2037 fb_label_name (unsigned int n
, unsigned int augend
)
2039 /* Returned to caller, then copied. Used for created names ("4f"). */
2040 static char symbol_name_build
[24];
2041 char *p
= symbol_name_build
;
2044 know (augend
<= 2 /* See mmix_fb_label. */);
2049 #ifdef LOCAL_LABEL_PREFIX
2050 *p
++ = LOCAL_LABEL_PREFIX
;
2052 sprintf (p
, "L%u%c%u",
2053 n
, LOCAL_LABEL_CHAR
, fb_label_instance (n
) + augend
);
2054 return symbol_name_build
;
2057 /* Decode name that may have been generated by foo_label_name() above.
2058 If the name wasn't generated by foo_label_name(), then return it
2059 unaltered. This is used for error messages. */
2062 decode_local_label_name (char *s
)
2065 char *symbol_decode
;
2067 int instance_number
;
2069 const char *message_format
;
2072 #ifdef LOCAL_LABEL_PREFIX
2073 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
2077 if (s
[lindex
] != 'L')
2080 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
2081 label_number
= (10 * label_number
) + *p
- '0';
2083 if (*p
== DOLLAR_LABEL_CHAR
)
2085 else if (*p
== LOCAL_LABEL_CHAR
)
2090 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
2091 instance_number
= (10 * instance_number
) + *p
- '0';
2093 message_format
= _("\"%d\" (instance number %d of a %s label)");
2094 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
2095 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
2097 return symbol_decode
;
2100 /* Get the value of a symbol. */
2103 S_GET_VALUE (symbolS
*s
)
2105 if (s
->flags
.local_symbol
)
2106 return resolve_symbol_value (s
);
2108 if (!s
->flags
.resolved
)
2110 valueT val
= resolve_symbol_value (s
);
2114 if (S_IS_WEAKREFR (s
))
2115 return S_GET_VALUE (s
->x
->value
.X_add_symbol
);
2117 if (s
->x
->value
.X_op
!= O_constant
)
2119 if (! s
->flags
.resolved
2120 || s
->x
->value
.X_op
!= O_symbol
2121 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
2122 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2125 return (valueT
) s
->x
->value
.X_add_number
;
2128 /* Set the value of a symbol. */
2131 S_SET_VALUE (symbolS
*s
, valueT val
)
2133 if (s
->flags
.local_symbol
)
2135 ((struct local_symbol
*) s
)->value
= val
;
2139 s
->x
->value
.X_op
= O_constant
;
2140 s
->x
->value
.X_add_number
= (offsetT
) val
;
2141 s
->x
->value
.X_unsigned
= 0;
2142 S_CLEAR_WEAKREFR (s
);
2146 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2148 if (dest
->flags
.local_symbol
)
2149 dest
= local_symbol_convert (dest
);
2150 if (src
->flags
.local_symbol
)
2151 src
= local_symbol_convert (src
);
2153 /* In an expression, transfer the settings of these flags.
2154 The user can override later, of course. */
2155 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2156 | BSF_GNU_INDIRECT_FUNCTION)
2157 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2159 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2160 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2163 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2164 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2169 S_IS_FUNCTION (symbolS
*s
)
2173 if (s
->flags
.local_symbol
)
2176 flags
= s
->bsym
->flags
;
2178 return (flags
& BSF_FUNCTION
) != 0;
2182 S_IS_EXTERNAL (symbolS
*s
)
2186 if (s
->flags
.local_symbol
)
2189 flags
= s
->bsym
->flags
;
2192 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2195 return (flags
& BSF_GLOBAL
) != 0;
2199 S_IS_WEAK (symbolS
*s
)
2201 if (s
->flags
.local_symbol
)
2203 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2204 could probably handle a WEAKREFR as always weak though. E.g., if
2205 the referenced symbol has lost its weak status, there's no reason
2206 to keep handling the weakrefr as if it was weak. */
2207 if (S_IS_WEAKREFR (s
))
2208 return S_IS_WEAK (s
->x
->value
.X_add_symbol
);
2209 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2213 S_IS_WEAKREFR (symbolS
*s
)
2215 if (s
->flags
.local_symbol
)
2217 return s
->flags
.weakrefr
!= 0;
2221 S_IS_WEAKREFD (symbolS
*s
)
2223 if (s
->flags
.local_symbol
)
2225 return s
->flags
.weakrefd
!= 0;
2229 S_IS_COMMON (symbolS
*s
)
2231 if (s
->flags
.local_symbol
)
2233 return bfd_is_com_section (s
->bsym
->section
);
2237 S_IS_DEFINED (symbolS
*s
)
2239 if (s
->flags
.local_symbol
)
2240 return ((struct local_symbol
*) s
)->section
!= undefined_section
;
2241 return s
->bsym
->section
!= undefined_section
;
2245 #ifndef EXTERN_FORCE_RELOC
2246 #define EXTERN_FORCE_RELOC IS_ELF
2249 /* Return true for symbols that should not be reduced to section
2250 symbols or eliminated from expressions, because they may be
2251 overridden by the linker. */
2253 S_FORCE_RELOC (symbolS
*s
, int strict
)
2256 if (s
->flags
.local_symbol
)
2257 sec
= ((struct local_symbol
*) s
)->section
;
2261 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2262 || (EXTERN_FORCE_RELOC
2263 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2264 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2266 sec
= s
->bsym
->section
;
2268 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2272 S_IS_DEBUG (symbolS
*s
)
2274 if (s
->flags
.local_symbol
)
2276 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2282 S_IS_LOCAL (symbolS
*s
)
2287 if (s
->flags
.local_symbol
)
2290 flags
= s
->bsym
->flags
;
2293 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2296 if (bfd_asymbol_section (s
->bsym
) == reg_section
)
2299 if (flag_strip_local_absolute
2300 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2301 the source file even when the object file is stripped. */
2302 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2303 && bfd_asymbol_section (s
->bsym
) == absolute_section
)
2306 name
= S_GET_NAME (s
);
2307 return (name
!= NULL
2309 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2310 || strchr (name
, LOCAL_LABEL_CHAR
)
2311 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2312 || strchr (name
, FAKE_LABEL_CHAR
)
2314 || TC_LABEL_IS_LOCAL (name
)
2315 || (! flag_keep_locals
2316 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2319 && name
[1] == '?')))));
2323 S_IS_STABD (symbolS
*s
)
2325 return S_GET_NAME (s
) == 0;
2329 S_CAN_BE_REDEFINED (const symbolS
*s
)
2331 if (s
->flags
.local_symbol
)
2332 return (((struct local_symbol
*) s
)->frag
2333 == &predefined_address_frag
);
2334 /* Permit register names to be redefined. */
2335 return s
->bsym
->section
== reg_section
;
2339 S_IS_VOLATILE (const symbolS
*s
)
2341 if (s
->flags
.local_symbol
)
2343 return s
->flags
.volatil
;
2347 S_IS_FORWARD_REF (const symbolS
*s
)
2349 if (s
->flags
.local_symbol
)
2351 return s
->flags
.forward_ref
;
2355 S_GET_NAME (symbolS
*s
)
2361 S_GET_SEGMENT (symbolS
*s
)
2363 if (s
->flags
.local_symbol
)
2364 return ((struct local_symbol
*) s
)->section
;
2365 return s
->bsym
->section
;
2369 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2371 if (s
->flags
.local_symbol
)
2373 ((struct local_symbol
*) s
)->section
= seg
;
2377 /* Don't reassign section symbols. The direct reason is to prevent seg
2378 faults assigning back to const global symbols such as *ABS*, but it
2379 shouldn't happen anyway. */
2380 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2382 if (s
->bsym
->section
!= seg
)
2387 if (multibyte_handling
== multibyte_warn_syms
2388 && ! s
->flags
.local_symbol
2389 && seg
!= undefined_section
2390 && ! s
->flags
.multibyte_warned
2391 && scan_for_multibyte_characters ((const unsigned char *) s
->name
,
2392 (const unsigned char *) s
->name
+ strlen (s
->name
),
2395 as_warn (_("symbol '%s' contains multibyte characters"), s
->name
);
2396 s
->flags
.multibyte_warned
= 1;
2399 s
->bsym
->section
= seg
;
2404 S_SET_EXTERNAL (symbolS
*s
)
2406 if (s
->flags
.local_symbol
)
2407 s
= local_symbol_convert (s
);
2408 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2410 /* Let .weak override .global. */
2413 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2415 /* Do not reassign section symbols. */
2416 as_warn (_("can't make section symbol global"));
2419 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2420 if (S_GET_SEGMENT (s
) == reg_section
)
2422 as_bad (_("can't make register symbol global"));
2426 s
->bsym
->flags
|= BSF_GLOBAL
;
2427 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2430 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2431 an_external_name
= S_GET_NAME (s
);
2436 S_CLEAR_EXTERNAL (symbolS
*s
)
2438 if (s
->flags
.local_symbol
)
2440 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2442 /* Let .weak override. */
2445 s
->bsym
->flags
|= BSF_LOCAL
;
2446 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2450 S_SET_WEAK (symbolS
*s
)
2452 if (s
->flags
.local_symbol
)
2453 s
= local_symbol_convert (s
);
2454 #ifdef obj_set_weak_hook
2455 obj_set_weak_hook (s
);
2457 s
->bsym
->flags
|= BSF_WEAK
;
2458 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2462 S_SET_WEAKREFR (symbolS
*s
)
2464 if (s
->flags
.local_symbol
)
2465 s
= local_symbol_convert (s
);
2466 s
->flags
.weakrefr
= 1;
2467 /* If the alias was already used, make sure we mark the target as
2468 used as well, otherwise it might be dropped from the symbol
2469 table. This may have unintended side effects if the alias is
2470 later redirected to another symbol, such as keeping the unused
2471 previous target in the symbol table. Since it will be weak, it's
2474 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2478 S_CLEAR_WEAKREFR (symbolS
*s
)
2480 if (s
->flags
.local_symbol
)
2482 s
->flags
.weakrefr
= 0;
2486 S_SET_WEAKREFD (symbolS
*s
)
2488 if (s
->flags
.local_symbol
)
2489 s
= local_symbol_convert (s
);
2490 s
->flags
.weakrefd
= 1;
2495 S_CLEAR_WEAKREFD (symbolS
*s
)
2497 if (s
->flags
.local_symbol
)
2499 if (s
->flags
.weakrefd
)
2501 s
->flags
.weakrefd
= 0;
2502 /* If a weakref target symbol is weak, then it was never
2503 referenced directly before, not even in a .global directive,
2504 so decay it to local. If it remains undefined, it will be
2505 later turned into a global, like any other undefined
2507 if (s
->bsym
->flags
& BSF_WEAK
)
2509 #ifdef obj_clear_weak_hook
2510 obj_clear_weak_hook (s
);
2512 s
->bsym
->flags
&= ~BSF_WEAK
;
2513 s
->bsym
->flags
|= BSF_LOCAL
;
2519 S_SET_THREAD_LOCAL (symbolS
*s
)
2521 if (s
->flags
.local_symbol
)
2522 s
= local_symbol_convert (s
);
2523 if (bfd_is_com_section (s
->bsym
->section
)
2524 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2526 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2527 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2528 as_bad (_("Accessing function `%s' as thread-local object"),
2530 else if (! bfd_is_und_section (s
->bsym
->section
)
2531 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2532 as_bad (_("Accessing `%s' as thread-local object"),
2537 S_SET_NAME (symbolS
*s
, const char *name
)
2540 if (s
->flags
.local_symbol
)
2542 s
->bsym
->name
= name
;
2546 S_SET_VOLATILE (symbolS
*s
)
2548 if (s
->flags
.local_symbol
)
2549 s
= local_symbol_convert (s
);
2550 s
->flags
.volatil
= 1;
2554 S_CLEAR_VOLATILE (symbolS
*s
)
2556 if (!s
->flags
.local_symbol
)
2557 s
->flags
.volatil
= 0;
2561 S_SET_FORWARD_REF (symbolS
*s
)
2563 if (s
->flags
.local_symbol
)
2564 s
= local_symbol_convert (s
);
2565 s
->flags
.forward_ref
= 1;
2568 /* Return the previous symbol in a chain. */
2571 symbol_previous (symbolS
*s
)
2573 if (s
->flags
.local_symbol
)
2575 return s
->x
->previous
;
2578 /* Return the next symbol in a chain. */
2581 symbol_next (symbolS
*s
)
2583 if (s
->flags
.local_symbol
)
2588 /* Return a pointer to the value of a symbol as an expression. */
2591 symbol_get_value_expression (symbolS
*s
)
2593 if (s
->flags
.local_symbol
)
2594 s
= local_symbol_convert (s
);
2595 return &s
->x
->value
;
2598 /* Set the value of a symbol to an expression. */
2601 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2603 if (s
->flags
.local_symbol
)
2604 s
= local_symbol_convert (s
);
2606 S_CLEAR_WEAKREFR (s
);
2609 /* Return whether 2 symbols are the same. */
2612 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2617 /* Return a pointer to the X_add_number component of a symbol. */
2620 symbol_X_add_number (symbolS
*s
)
2622 if (s
->flags
.local_symbol
)
2623 return (offsetT
*) &((struct local_symbol
*) s
)->value
;
2625 return &s
->x
->value
.X_add_number
;
2628 /* Set the value of SYM to the current position in the current segment. */
2631 symbol_set_value_now (symbolS
*sym
)
2633 S_SET_SEGMENT (sym
, now_seg
);
2634 S_SET_VALUE (sym
, frag_now_fix ());
2635 symbol_set_frag (sym
, frag_now
);
2638 /* Set the frag of a symbol. */
2641 symbol_set_frag (symbolS
*s
, fragS
*f
)
2643 if (s
->flags
.local_symbol
)
2645 ((struct local_symbol
*) s
)->frag
= f
;
2649 S_CLEAR_WEAKREFR (s
);
2652 /* Return the frag of a symbol. */
2655 symbol_get_frag (symbolS
*s
)
2657 if (s
->flags
.local_symbol
)
2658 return ((struct local_symbol
*) s
)->frag
;
2662 /* Mark a symbol as having been used. */
2665 symbol_mark_used (symbolS
*s
)
2667 if (s
->flags
.local_symbol
)
2670 if (S_IS_WEAKREFR (s
))
2671 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2674 /* Clear the mark of whether a symbol has been used. */
2677 symbol_clear_used (symbolS
*s
)
2679 if (s
->flags
.local_symbol
)
2680 s
= local_symbol_convert (s
);
2684 /* Return whether a symbol has been used. */
2687 symbol_used_p (symbolS
*s
)
2689 if (s
->flags
.local_symbol
)
2691 return s
->flags
.used
;
2694 /* Mark a symbol as having been used in a reloc. */
2697 symbol_mark_used_in_reloc (symbolS
*s
)
2699 if (s
->flags
.local_symbol
)
2700 s
= local_symbol_convert (s
);
2701 s
->flags
.used_in_reloc
= 1;
2704 /* Clear the mark of whether a symbol has been used in a reloc. */
2707 symbol_clear_used_in_reloc (symbolS
*s
)
2709 if (s
->flags
.local_symbol
)
2711 s
->flags
.used_in_reloc
= 0;
2714 /* Return whether a symbol has been used in a reloc. */
2717 symbol_used_in_reloc_p (symbolS
*s
)
2719 if (s
->flags
.local_symbol
)
2721 return s
->flags
.used_in_reloc
;
2724 /* Mark a symbol as an MRI common symbol. */
2727 symbol_mark_mri_common (symbolS
*s
)
2729 if (s
->flags
.local_symbol
)
2730 s
= local_symbol_convert (s
);
2731 s
->flags
.mri_common
= 1;
2734 /* Clear the mark of whether a symbol is an MRI common symbol. */
2737 symbol_clear_mri_common (symbolS
*s
)
2739 if (s
->flags
.local_symbol
)
2741 s
->flags
.mri_common
= 0;
2744 /* Return whether a symbol is an MRI common symbol. */
2747 symbol_mri_common_p (symbolS
*s
)
2749 if (s
->flags
.local_symbol
)
2751 return s
->flags
.mri_common
;
2754 /* Mark a symbol as having been written. */
2757 symbol_mark_written (symbolS
*s
)
2759 if (s
->flags
.local_symbol
)
2761 s
->flags
.written
= 1;
2764 /* Clear the mark of whether a symbol has been written. */
2767 symbol_clear_written (symbolS
*s
)
2769 if (s
->flags
.local_symbol
)
2771 s
->flags
.written
= 0;
2774 /* Return whether a symbol has been written. */
2777 symbol_written_p (symbolS
*s
)
2779 if (s
->flags
.local_symbol
)
2781 return s
->flags
.written
;
2784 /* Mark a symbol as to be removed. */
2787 symbol_mark_removed (symbolS
*s
)
2789 if (s
->flags
.local_symbol
)
2791 s
->flags
.removed
= 1;
2794 /* Return whether a symbol has been marked to be removed. */
2797 symbol_removed_p (symbolS
*s
)
2799 if (s
->flags
.local_symbol
)
2801 return s
->flags
.removed
;
2804 /* Mark a symbol has having been resolved. */
2807 symbol_mark_resolved (symbolS
*s
)
2809 s
->flags
.resolved
= 1;
2812 /* Return whether a symbol has been resolved. */
2815 symbol_resolved_p (symbolS
*s
)
2817 return s
->flags
.resolved
;
2820 /* Return whether a symbol is a section symbol. */
2823 symbol_section_p (symbolS
*s
)
2825 if (s
->flags
.local_symbol
)
2827 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2830 /* Return whether a symbol is equated to another symbol. */
2833 symbol_equated_p (symbolS
*s
)
2835 if (s
->flags
.local_symbol
)
2837 return s
->x
->value
.X_op
== O_symbol
;
2840 /* Return whether a symbol is equated to another symbol, and should be
2841 treated specially when writing out relocs. */
2844 symbol_equated_reloc_p (symbolS
*s
)
2846 if (s
->flags
.local_symbol
)
2848 /* X_op_symbol, normally not used for O_symbol, is set by
2849 resolve_symbol_value to flag expression syms that have been
2851 return (s
->x
->value
.X_op
== O_symbol
2852 #if defined (OBJ_COFF) && defined (TE_PE)
2855 && ((s
->flags
.resolved
&& s
->x
->value
.X_op_symbol
!= NULL
)
2856 || ! S_IS_DEFINED (s
)
2857 || S_IS_COMMON (s
)));
2860 /* Return whether a symbol has a constant value. */
2863 symbol_constant_p (symbolS
*s
)
2865 if (s
->flags
.local_symbol
)
2867 return s
->x
->value
.X_op
== O_constant
;
2870 /* Return whether a symbol was cloned and thus removed from the global
2874 symbol_shadow_p (symbolS
*s
)
2876 if (s
->flags
.local_symbol
)
2878 return s
->x
->next
== s
;
2881 /* If S is a struct symbol return S, otherwise return NULL. */
2884 symbol_symbolS (symbolS
*s
)
2886 if (s
->flags
.local_symbol
)
2891 /* Return the BFD symbol for a symbol. */
2894 symbol_get_bfdsym (symbolS
*s
)
2896 if (s
->flags
.local_symbol
)
2897 s
= local_symbol_convert (s
);
2901 /* Set the BFD symbol for a symbol. */
2904 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2906 if (s
->flags
.local_symbol
)
2907 s
= local_symbol_convert (s
);
2908 /* Usually, it is harmless to reset a symbol to a BFD section
2909 symbol. For example, obj_elf_change_section sets the BFD symbol
2910 of an old symbol with the newly created section symbol. But when
2911 we have multiple sections with the same name, the newly created
2912 section may have the same name as an old section. We check if the
2913 old symbol has been already marked as a section symbol before
2915 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2917 /* else XXX - What do we do now ? */
2920 #ifdef OBJ_SYMFIELD_TYPE
2922 /* Get a pointer to the object format information for a symbol. */
2925 symbol_get_obj (symbolS
*s
)
2927 if (s
->flags
.local_symbol
)
2928 s
= local_symbol_convert (s
);
2932 /* Set the object format information for a symbol. */
2935 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2937 if (s
->flags
.local_symbol
)
2938 s
= local_symbol_convert (s
);
2942 #endif /* OBJ_SYMFIELD_TYPE */
2944 #ifdef TC_SYMFIELD_TYPE
2946 /* Get a pointer to the processor information for a symbol. */
2949 symbol_get_tc (symbolS
*s
)
2951 if (s
->flags
.local_symbol
)
2952 s
= local_symbol_convert (s
);
2956 /* Set the processor information for a symbol. */
2959 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2961 if (s
->flags
.local_symbol
)
2962 s
= local_symbol_convert (s
);
2966 #endif /* TC_SYMFIELD_TYPE */
2971 symbol_lastP
= NULL
;
2972 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2973 sy_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
2974 NULL
, xcalloc
, free
);
2976 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2977 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
2979 abs_symbol
.x
= &abs_symbol_x
;
2980 abs_symbol
.x
->value
.X_op
= O_constant
;
2981 abs_symbol
.frag
= &zero_address_frag
;
2983 if (LOCAL_LABELS_FB
)
2988 dot_symbol_init (void)
2990 dot_symbol
.name
= ".";
2991 dot_symbol
.flags
.forward_ref
= 1;
2992 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2993 if (dot_symbol
.bsym
== NULL
)
2994 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2995 dot_symbol
.bsym
->name
= ".";
2996 dot_symbol
.x
= &dot_symbol_x
;
2997 dot_symbol
.x
->value
.X_op
= O_constant
;
3002 /* Maximum indent level.
3003 Available for modification inside a gdb session. */
3004 static int max_indent_level
= 8;
3007 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
3009 const char *name
= S_GET_NAME (sym
);
3010 if (!name
|| !name
[0])
3012 fprintf (file
, "sym ");
3013 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
3014 fprintf (file
, " %s", name
);
3016 if (sym
->flags
.local_symbol
)
3018 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
3020 if (locsym
->frag
!= &zero_address_frag
3021 && locsym
->frag
!= NULL
)
3023 fprintf (file
, " frag ");
3024 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) locsym
->frag
));
3026 if (locsym
->flags
.resolved
)
3027 fprintf (file
, " resolved");
3028 fprintf (file
, " local");
3032 if (sym
->frag
!= &zero_address_frag
)
3034 fprintf (file
, " frag ");
3035 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->frag
));
3037 if (sym
->flags
.written
)
3038 fprintf (file
, " written");
3039 if (sym
->flags
.resolved
)
3040 fprintf (file
, " resolved");
3041 else if (sym
->flags
.resolving
)
3042 fprintf (file
, " resolving");
3043 if (sym
->flags
.used_in_reloc
)
3044 fprintf (file
, " used-in-reloc");
3045 if (sym
->flags
.used
)
3046 fprintf (file
, " used");
3047 if (S_IS_LOCAL (sym
))
3048 fprintf (file
, " local");
3049 if (S_IS_EXTERNAL (sym
))
3050 fprintf (file
, " extern");
3051 if (S_IS_WEAK (sym
))
3052 fprintf (file
, " weak");
3053 if (S_IS_DEBUG (sym
))
3054 fprintf (file
, " debug");
3055 if (S_IS_DEFINED (sym
))
3056 fprintf (file
, " defined");
3058 if (S_IS_WEAKREFR (sym
))
3059 fprintf (file
, " weakrefr");
3060 if (S_IS_WEAKREFD (sym
))
3061 fprintf (file
, " weakrefd");
3062 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
3063 if (symbol_resolved_p (sym
))
3065 segT s
= S_GET_SEGMENT (sym
);
3067 if (s
!= undefined_section
3068 && s
!= expr_section
)
3069 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
3071 else if (indent_level
< max_indent_level
3072 && S_GET_SEGMENT (sym
) != undefined_section
)
3075 fprintf (file
, "\n%*s<", indent_level
* 4, "");
3076 if (sym
->flags
.local_symbol
)
3077 fprintf (file
, "constant %lx",
3078 (unsigned long) ((struct local_symbol
*) sym
)->value
);
3080 print_expr_1 (file
, &sym
->x
->value
);
3081 fprintf (file
, ">");
3088 print_symbol_value (symbolS
*sym
)
3091 print_symbol_value_1 (stderr
, sym
);
3092 fprintf (stderr
, "\n");
3096 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
3099 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
3100 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3101 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3102 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3103 fprintf (file
, ">");
3108 print_expr_1 (FILE *file
, expressionS
*exp
)
3110 fprintf (file
, "expr ");
3111 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
3112 fprintf (file
, " ");
3116 fprintf (file
, "illegal");
3119 fprintf (file
, "absent");
3122 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
3126 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
3127 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3128 fprintf (file
, ">");
3130 if (exp
->X_add_number
)
3131 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
3132 (unsigned long) exp
->X_add_number
);
3136 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
3139 fprintf (file
, "big");
3142 fprintf (file
, "uminus -<");
3144 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3145 fprintf (file
, ">");
3146 goto maybe_print_addnum
;
3148 fprintf (file
, "bit_not");
3151 print_binary (file
, "multiply", exp
);
3154 print_binary (file
, "divide", exp
);
3157 print_binary (file
, "modulus", exp
);
3160 print_binary (file
, "lshift", exp
);
3163 print_binary (file
, "rshift", exp
);
3165 case O_bit_inclusive_or
:
3166 print_binary (file
, "bit_ior", exp
);
3168 case O_bit_exclusive_or
:
3169 print_binary (file
, "bit_xor", exp
);
3172 print_binary (file
, "bit_and", exp
);
3175 print_binary (file
, "eq", exp
);
3178 print_binary (file
, "ne", exp
);
3181 print_binary (file
, "lt", exp
);
3184 print_binary (file
, "le", exp
);
3187 print_binary (file
, "ge", exp
);
3190 print_binary (file
, "gt", exp
);
3193 print_binary (file
, "logical_and", exp
);
3196 print_binary (file
, "logical_or", exp
);
3200 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3201 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3202 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3203 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3204 fprintf (file
, ">");
3205 goto maybe_print_addnum
;
3208 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3209 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3210 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3211 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3212 fprintf (file
, ">");
3213 goto maybe_print_addnum
;
3215 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3222 print_expr (expressionS
*exp
)
3224 print_expr_1 (stderr
, exp
);
3225 fprintf (stderr
, "\n");
3229 symbol_print_statistics (FILE *file
)
3231 htab_print_statistics (file
, "symbol table", sy_hash
);
3232 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3233 local_symbol_count
, local_symbol_conversion_count
);
3236 #ifdef OBJ_COMPLEX_RELC
3238 /* Convert given symbol to a new complex-relocation symbol name. This
3239 may be a recursive function, since it might be called for non-leaf
3240 nodes (plain symbols) in the expression tree. The caller owns the
3241 returning string, so should free it eventually. Errors are
3242 indicated via as_bad and a NULL return value. The given symbol
3243 is marked with used_in_reloc. */
3246 symbol_relc_make_sym (symbolS
* sym
)
3248 char * terminal
= NULL
;
3253 gas_assert (sym
!= NULL
);
3255 /* Recurse to symbol_relc_make_expr if this symbol
3256 is defined as an expression or a plain value. */
3257 if ( S_GET_SEGMENT (sym
) == expr_section
3258 || S_GET_SEGMENT (sym
) == absolute_section
)
3259 return symbol_relc_make_expr (symbol_get_value_expression (sym
));
3261 /* This may be a "fake symbol", referring to ".".
3262 Write out a special null symbol to refer to this position. */
3263 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3264 return xstrdup (".");
3266 /* We hope this is a plain leaf symbol. Construct the encoding
3267 as {S,s}II...:CCCCCCC....
3268 where 'S'/'s' means section symbol / plain symbol
3269 III is decimal for the symbol name length
3270 CCC is the symbol name itself. */
3271 symbol_mark_used_in_reloc (sym
);
3273 sname
= S_GET_NAME (sym
);
3274 sname_len
= strlen (sname
);
3275 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3277 terminal
= XNEWVEC (char, (1 /* S or s */
3278 + 8 /* sname_len in decimal */
3280 + sname_len
/* name itself */
3283 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3287 /* Convert given value to a new complex-relocation symbol name. This
3288 is a non-recursive function, since it is be called for leaf nodes
3289 (plain values) in the expression tree. The caller owns the
3290 returning string, so should free() it eventually. No errors. */
3293 symbol_relc_make_value (offsetT val
)
3295 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3298 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3302 /* Convert given expression to a new complex-relocation symbol name.
3303 This is a recursive function, since it traverses the entire given
3304 expression tree. The caller owns the returning string, so should
3305 free() it eventually. Errors are indicated via as_bad() and a NULL
3309 symbol_relc_make_expr (expressionS
* exp
)
3311 const char * opstr
= NULL
; /* Operator prefix string. */
3312 int arity
= 0; /* Arity of this operator. */
3313 char * operands
[3]; /* Up to three operands. */
3314 char * concat_string
= NULL
;
3316 operands
[0] = operands
[1] = operands
[2] = NULL
;
3318 gas_assert (exp
!= NULL
);
3320 /* Match known operators -> fill in opstr, arity, operands[] and fall
3321 through to construct subexpression fragments; may instead return
3322 string directly for leaf nodes. */
3324 /* See expr.h for the meaning of all these enums. Many operators
3325 have an unnatural arity (X_add_number implicitly added). The
3326 conversion logic expands them to explicit "+" subexpressions. */
3331 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3336 return symbol_relc_make_value (exp
->X_add_number
);
3339 if (exp
->X_add_number
)
3343 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3344 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3348 return symbol_relc_make_sym (exp
->X_add_symbol
);
3350 /* Helper macros for nesting nodes. */
3352 #define HANDLE_XADD_OPT1(str_) \
3353 if (exp->X_add_number) \
3356 opstr = "+:" str_; \
3357 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3358 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3365 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3369 #define HANDLE_XADD_OPT2(str_) \
3370 if (exp->X_add_number) \
3373 opstr = "+:" str_; \
3374 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3375 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3376 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3382 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3383 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3387 /* Nesting nodes. */
3389 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3390 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3391 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3392 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3393 case O_divide
: HANDLE_XADD_OPT2 ("/");
3394 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3395 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3396 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3397 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3398 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3399 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3400 case O_add
: HANDLE_XADD_OPT2 ("+");
3401 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3402 case O_eq
: HANDLE_XADD_OPT2 ("==");
3403 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3404 case O_lt
: HANDLE_XADD_OPT2 ("<");
3405 case O_le
: HANDLE_XADD_OPT2 ("<=");
3406 case O_ge
: HANDLE_XADD_OPT2 (">=");
3407 case O_gt
: HANDLE_XADD_OPT2 (">");
3408 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3409 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3412 /* Validate & reject early. */
3413 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3415 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3417 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3421 concat_string
= NULL
;
3422 else if (arity
== 0)
3423 concat_string
= xstrdup (opstr
);
3424 else if (arity
== 1)
3425 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3426 else if (arity
== 2)
3427 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3430 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3431 operands
[2], (char *) NULL
);
3433 /* Free operand strings (not opstr). */
3434 if (arity
>= 1) xfree (operands
[0]);
3435 if (arity
>= 2) xfree (operands
[1]);
3436 if (arity
>= 3) xfree (operands
[2]);
3438 return concat_string
;