1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2021 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;
83 /* A pointer in the symbol may point to either a complete symbol
84 (struct symbol below) or to a local symbol (struct local_symbol
85 defined here). The symbol code can detect the case by examining
86 the first field which is present in both structs.
88 We do this because we ordinarily only need a small amount of
89 information for a local symbol. The symbol table takes up a lot of
90 space, and storing less information for a local symbol can make a
91 big difference in assembler memory usage when assembling a large
96 /* Symbol flags. Only local_symbol and resolved are relevant. */
97 struct symbol_flags flags
;
99 /* Hash value calculated from name. */
102 /* The symbol name. */
105 /* The symbol frag. */
108 /* The symbol section. */
111 /* The value of the symbol. */
115 /* The information we keep for a symbol. The symbol table holds
116 pointers both to this and to local_symbol structures. The first
117 three fields must be identical to struct local_symbol, and the size
118 should be the same as or smaller than struct local_symbol.
119 Fields that don't fit go to an extension structure. */
124 struct symbol_flags flags
;
126 /* Hash value calculated from name. */
129 /* The symbol name. */
132 /* Pointer to the frag this symbol is attached to, if any.
139 /* Extra symbol fields that won't fit. */
143 /* Extra fields to make up a full symbol. */
147 /* The value of the symbol. */
150 /* Forwards and backwards chain pointers. */
152 struct symbol
*previous
;
154 #ifdef OBJ_SYMFIELD_TYPE
155 OBJ_SYMFIELD_TYPE obj
;
158 #ifdef TC_SYMFIELD_TYPE
163 typedef union symbol_entry
165 struct local_symbol lsy
;
169 /* Hash function for a symbol_entry. */
172 hash_symbol_entry (const void *e
)
174 symbol_entry_t
*entry
= (symbol_entry_t
*) e
;
175 if (entry
->sy
.hash
== 0)
176 entry
->sy
.hash
= htab_hash_string (entry
->sy
.name
);
178 return entry
->sy
.hash
;
181 /* Equality function for a symbol_entry. */
184 eq_symbol_entry (const void *a
, const void *b
)
186 const symbol_entry_t
*ea
= (const symbol_entry_t
*) a
;
187 const symbol_entry_t
*eb
= (const symbol_entry_t
*) b
;
189 return (ea
->sy
.hash
== eb
->sy
.hash
190 && strcmp (ea
->sy
.name
, eb
->sy
.name
) == 0);
194 symbol_entry_find (htab_t table
, const char *name
)
196 hashval_t hash
= htab_hash_string (name
);
197 symbol_entry_t needle
= { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
198 hash
, name
, 0, 0, 0 } };
199 return htab_find_with_hash (table
, &needle
, hash
);
203 /* This is non-zero if symbols are case sensitive, which is the
205 int symbols_case_sensitive
= 1;
207 #ifndef WORKING_DOT_WORD
208 extern int new_broken_words
;
211 static htab_t sy_hash
;
213 /* Below are commented in "symbols.h". */
214 symbolS
*symbol_rootP
;
215 symbolS
*symbol_lastP
;
217 struct xsymbol abs_symbol_x
;
219 struct xsymbol dot_symbol_x
;
222 #define debug_verify_symchain verify_symbol_chain
224 #define debug_verify_symchain(root, last) ((void) 0)
227 #define DOLLAR_LABEL_CHAR '\001'
228 #define LOCAL_LABEL_CHAR '\002'
230 #ifndef TC_LABEL_IS_LOCAL
231 #define TC_LABEL_IS_LOCAL(name) 0
234 struct obstack notes
;
236 /* The name of an external symbol which is
237 used to make weak PE symbol names unique. */
238 const char * an_external_name
;
241 static const char *save_symbol_name (const char *);
242 static void fb_label_init (void);
243 static long dollar_label_instance (long);
244 static long fb_label_instance (long);
246 static void print_binary (FILE *, const char *, expressionS
*);
248 /* Return a pointer to a new symbol. Die if we can't make a new
249 symbol. Fill in the symbol's values. Add symbol to end of symbol
252 This function should be called in the general case of creating a
253 symbol. However, if the output file symbol table has already been
254 set, and you are certain that this symbol won't be wanted in the
255 output file, you can call symbol_create. */
258 symbol_new (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
260 symbolS
*symbolP
= symbol_create (name
, segment
, frag
, valu
);
262 /* Link to end of symbol chain. */
263 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
268 /* Save a symbol name on a permanent obstack, and convert it according
269 to the object file format. */
272 save_symbol_name (const char *name
)
277 gas_assert (name
!= NULL
);
278 name_length
= strlen (name
) + 1; /* +1 for \0. */
279 obstack_grow (¬es
, name
, name_length
);
280 ret
= (char *) obstack_finish (¬es
);
282 #ifdef tc_canonicalize_symbol_name
283 ret
= tc_canonicalize_symbol_name (ret
);
286 if (! symbols_case_sensitive
)
290 for (s
= ret
; *s
!= '\0'; s
++)
298 symbol_init (symbolS
*symbolP
, const char *name
, asection
*sec
,
299 fragS
*frag
, valueT valu
)
301 symbolP
->frag
= frag
;
302 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
303 if (symbolP
->bsym
== NULL
)
304 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
305 symbolP
->bsym
->name
= name
;
306 symbolP
->bsym
->section
= sec
;
308 S_SET_VALUE (symbolP
, valu
);
310 symbol_clear_list_pointers (symbolP
);
312 obj_symbol_new_hook (symbolP
);
314 #ifdef tc_symbol_new_hook
315 tc_symbol_new_hook (symbolP
);
319 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
322 symbol_create (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
324 const char *preserved_copy_of_name
;
328 preserved_copy_of_name
= save_symbol_name (name
);
330 size
= sizeof (symbolS
) + sizeof (struct xsymbol
);
331 symbolP
= (symbolS
*) obstack_alloc (¬es
, size
);
333 /* symbol must be born in some fixed state. This seems as good as any. */
334 memset (symbolP
, 0, size
);
335 symbolP
->name
= preserved_copy_of_name
;
336 symbolP
->x
= (struct xsymbol
*) (symbolP
+ 1);
338 symbol_init (symbolP
, preserved_copy_of_name
, segment
, frag
, valu
);
344 /* Local symbol support. If we can get away with it, we keep only a
345 small amount of information for local symbols. */
347 /* Used for statistics. */
349 static unsigned long local_symbol_count
;
350 static unsigned long local_symbol_conversion_count
;
352 /* Create a local symbol and insert it into the local hash table. */
354 struct local_symbol
*
355 local_symbol_make (const char *name
, segT section
, fragS
*frag
, valueT val
)
357 const char *name_copy
;
358 struct local_symbol
*ret
;
359 struct symbol_flags flags
= { .local_symbol
= 1, .resolved
= 0 };
361 ++local_symbol_count
;
363 name_copy
= save_symbol_name (name
);
365 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
368 ret
->name
= name_copy
;
370 ret
->section
= section
;
373 htab_insert (sy_hash
, ret
, 1);
378 /* Convert a local symbol into a real symbol. */
381 local_symbol_convert (void *sym
)
383 symbol_entry_t
*ent
= (symbol_entry_t
*) sym
;
384 struct xsymbol
*xtra
;
387 gas_assert (ent
->lsy
.flags
.local_symbol
);
389 ++local_symbol_conversion_count
;
391 xtra
= (struct xsymbol
*) obstack_alloc (¬es
, sizeof (*xtra
));
392 memset (xtra
, 0, sizeof (*xtra
));
393 val
= ent
->lsy
.value
;
396 /* Local symbols are always either defined or used. */
397 ent
->sy
.flags
.used
= 1;
398 ent
->sy
.flags
.local_symbol
= 0;
400 symbol_init (&ent
->sy
, ent
->lsy
.name
, ent
->lsy
.section
, ent
->lsy
.frag
, val
);
401 symbol_append (&ent
->sy
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
407 define_sym_at_dot (symbolS
*symbolP
)
409 symbolP
->frag
= frag_now
;
410 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
411 S_SET_SEGMENT (symbolP
, now_seg
);
414 /* We have just seen "<name>:".
415 Creates a struct symbol unless it already exists.
417 Gripes if we are redefining a symbol incompatibly (and ignores it). */
420 colon (/* Just seen "x:" - rattle symbols & frags. */
421 const char *sym_name
/* Symbol name, as a canonical string. */
422 /* We copy this string: OK to alter later. */)
424 symbolS
*symbolP
; /* Symbol we are working with. */
426 /* Sun local labels go out of scope whenever a non-local symbol is
428 if (LOCAL_LABELS_DOLLAR
429 && !bfd_is_local_label_name (stdoutput
, sym_name
))
430 dollar_label_clear ();
432 #ifndef WORKING_DOT_WORD
433 if (new_broken_words
)
435 struct broken_word
*a
;
440 if (now_seg
== absolute_section
)
442 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
446 possible_bytes
= (md_short_jump_size
447 + new_broken_words
* md_long_jump_size
);
450 frag_opcode
= frag_var (rs_broken_word
,
454 (symbolS
*) broken_words
,
458 /* We want to store the pointer to where to insert the jump
459 table in the fr_opcode of the rs_broken_word frag. This
460 requires a little hackery. */
462 && (frag_tmp
->fr_type
!= rs_broken_word
463 || frag_tmp
->fr_opcode
))
464 frag_tmp
= frag_tmp
->fr_next
;
466 frag_tmp
->fr_opcode
= frag_opcode
;
467 new_broken_words
= 0;
469 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
470 a
->dispfrag
= frag_tmp
;
472 #endif /* WORKING_DOT_WORD */
474 #ifdef obj_frob_colon
475 obj_frob_colon (sym_name
);
478 if ((symbolP
= symbol_find (sym_name
)) != 0)
480 S_CLEAR_WEAKREFR (symbolP
);
481 #ifdef RESOLVE_SYMBOL_REDEFINITION
482 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
485 /* Now check for undefined symbols. */
486 if (symbolP
->flags
.local_symbol
)
488 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
490 if (locsym
->section
!= undefined_section
491 && (locsym
->frag
!= frag_now
492 || locsym
->section
!= now_seg
493 || locsym
->value
!= frag_now_fix ()))
495 as_bad (_("symbol `%s' is already defined"), sym_name
);
499 locsym
->section
= now_seg
;
500 locsym
->frag
= frag_now
;
501 locsym
->value
= frag_now_fix ();
503 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
504 || S_IS_COMMON (symbolP
)
505 || S_IS_VOLATILE (symbolP
))
507 if (S_IS_VOLATILE (symbolP
))
509 symbolP
= symbol_clone (symbolP
, 1);
510 S_SET_VALUE (symbolP
, 0);
511 S_CLEAR_VOLATILE (symbolP
);
513 if (S_GET_VALUE (symbolP
) == 0)
515 define_sym_at_dot (symbolP
);
518 #endif /* if we have one, it better be zero. */
523 /* There are still several cases to check:
525 A .comm/.lcomm symbol being redefined as initialized
528 A .comm/.lcomm symbol being redefined with a larger
531 This only used to be allowed on VMS gas, but Sun cc
532 on the sparc also depends on it. */
534 if (((!S_IS_DEBUG (symbolP
)
535 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
536 && S_IS_EXTERNAL (symbolP
))
537 || S_GET_SEGMENT (symbolP
) == bss_section
)
538 && (now_seg
== data_section
539 || now_seg
== bss_section
540 || now_seg
== S_GET_SEGMENT (symbolP
)))
542 /* Select which of the 2 cases this is. */
543 if (now_seg
!= data_section
)
545 /* New .comm for prev .comm symbol.
547 If the new size is larger we just change its
548 value. If the new size is smaller, we ignore
550 if (S_GET_VALUE (symbolP
)
551 < ((unsigned) frag_now_fix ()))
553 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
558 /* It is a .comm/.lcomm being converted to initialized
560 define_sym_at_dot (symbolP
);
565 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
566 static const char *od_buf
= "";
570 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
571 sprintf (od_buf
, "%d.%d.",
572 S_GET_OTHER (symbolP
),
573 S_GET_DESC (symbolP
));
575 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
577 segment_name (S_GET_SEGMENT (symbolP
)),
579 (long) S_GET_VALUE (symbolP
));
581 } /* if the undefined symbol has no value */
585 /* Don't blow up if the definition is the same. */
586 if (!(frag_now
== symbolP
->frag
587 && S_GET_VALUE (symbolP
) == frag_now_fix ()
588 && S_GET_SEGMENT (symbolP
) == now_seg
))
590 as_bad (_("symbol `%s' is already defined"), sym_name
);
591 symbolP
= symbol_clone (symbolP
, 0);
592 define_sym_at_dot (symbolP
);
597 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
599 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
, frag_now
,
604 symbolP
= symbol_new (sym_name
, now_seg
, frag_now
, frag_now_fix ());
606 symbol_table_insert (symbolP
);
609 if (mri_common_symbol
!= NULL
)
611 /* This symbol is actually being defined within an MRI common
612 section. This requires special handling. */
613 if (symbolP
->flags
.local_symbol
)
614 symbolP
= local_symbol_convert (symbolP
);
615 symbolP
->x
->value
.X_op
= O_symbol
;
616 symbolP
->x
->value
.X_add_symbol
= mri_common_symbol
;
617 symbolP
->x
->value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
618 symbolP
->frag
= &zero_address_frag
;
619 S_SET_SEGMENT (symbolP
, expr_section
);
620 symbolP
->flags
.mri_common
= 1;
624 tc_frob_label (symbolP
);
626 #ifdef obj_frob_label
627 obj_frob_label (symbolP
);
633 /* Die if we can't insert the symbol. */
636 symbol_table_insert (symbolS
*symbolP
)
640 htab_insert (sy_hash
, symbolP
, 1);
643 /* If a symbol name does not exist, create it as undefined, and insert
644 it into the symbol table. Return a pointer to it. */
647 symbol_find_or_make (const char *name
)
651 symbolP
= symbol_find (name
);
655 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
657 symbolP
= md_undefined_symbol ((char *) name
);
661 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
662 &zero_address_frag
, 0);
666 symbolP
= symbol_make (name
);
668 symbol_table_insert (symbolP
);
669 } /* if symbol wasn't found */
675 symbol_make (const char *name
)
679 /* Let the machine description default it, e.g. for register names. */
680 symbolP
= md_undefined_symbol ((char *) name
);
683 symbolP
= symbol_new (name
, undefined_section
, &zero_address_frag
, 0);
689 symbol_clone (symbolS
*orgsymP
, int replace
)
692 asymbol
*bsymorg
, *bsymnew
;
694 /* Make sure we never clone the dot special symbol. */
695 gas_assert (orgsymP
!= &dot_symbol
);
697 /* When cloning a local symbol it isn't absolutely necessary to
698 convert the original, but converting makes the code much
699 simpler to cover this unexpected case. As of 2020-08-21
700 symbol_clone won't be called on a local symbol. */
701 if (orgsymP
->flags
.local_symbol
)
702 orgsymP
= local_symbol_convert (orgsymP
);
703 bsymorg
= orgsymP
->bsym
;
705 newsymP
= (symbolS
*) obstack_alloc (¬es
, (sizeof (symbolS
)
706 + sizeof (struct xsymbol
)));
708 newsymP
->x
= (struct xsymbol
*) (newsymP
+ 1);
709 *newsymP
->x
= *orgsymP
->x
;
710 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
712 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
713 newsymP
->bsym
= bsymnew
;
714 bsymnew
->name
= bsymorg
->name
;
715 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
716 bsymnew
->section
= bsymorg
->section
;
717 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
718 bfd_asymbol_bfd (bsymnew
), bsymnew
);
720 #ifdef obj_symbol_clone_hook
721 obj_symbol_clone_hook (newsymP
, orgsymP
);
724 #ifdef tc_symbol_clone_hook
725 tc_symbol_clone_hook (newsymP
, orgsymP
);
730 if (symbol_rootP
== orgsymP
)
731 symbol_rootP
= newsymP
;
732 else if (orgsymP
->x
->previous
)
734 orgsymP
->x
->previous
->x
->next
= newsymP
;
735 orgsymP
->x
->previous
= NULL
;
737 if (symbol_lastP
== orgsymP
)
738 symbol_lastP
= newsymP
;
739 else if (orgsymP
->x
->next
)
740 orgsymP
->x
->next
->x
->previous
= newsymP
;
742 /* Symbols that won't be output can't be external. */
743 S_CLEAR_EXTERNAL (orgsymP
);
744 orgsymP
->x
->previous
= orgsymP
->x
->next
= orgsymP
;
745 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
747 symbol_table_insert (newsymP
);
751 /* Symbols that won't be output can't be external. */
752 S_CLEAR_EXTERNAL (newsymP
);
753 newsymP
->x
->previous
= newsymP
->x
->next
= newsymP
;
759 /* Referenced symbols, if they are forward references, need to be cloned
760 (without replacing the original) so that the value of the referenced
761 symbols at the point of use is saved by the clone. */
763 #undef symbol_clone_if_forward_ref
765 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
767 if (symbolP
&& !symbolP
->flags
.local_symbol
)
769 symbolS
*orig_add_symbol
= symbolP
->x
->value
.X_add_symbol
;
770 symbolS
*orig_op_symbol
= symbolP
->x
->value
.X_op_symbol
;
771 symbolS
*add_symbol
= orig_add_symbol
;
772 symbolS
*op_symbol
= orig_op_symbol
;
774 if (symbolP
->flags
.forward_ref
)
779 /* assign_symbol() clones volatile symbols; pre-existing expressions
780 hold references to the original instance, but want the current
781 value. Just repeat the lookup. */
782 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
783 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
784 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
785 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
788 /* Re-using resolving here, as this routine cannot get called from
789 symbol resolution code. */
790 if ((symbolP
->bsym
->section
== expr_section
791 || symbolP
->flags
.forward_ref
)
792 && !symbolP
->flags
.resolving
)
794 symbolP
->flags
.resolving
= 1;
795 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
796 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
797 symbolP
->flags
.resolving
= 0;
800 if (symbolP
->flags
.forward_ref
801 || add_symbol
!= orig_add_symbol
802 || op_symbol
!= orig_op_symbol
)
804 if (symbolP
!= &dot_symbol
)
806 symbolP
= symbol_clone (symbolP
, 0);
807 symbolP
->flags
.resolving
= 0;
811 symbolP
= symbol_temp_new_now ();
812 #ifdef tc_new_dot_label
813 tc_new_dot_label (symbolP
);
818 symbolP
->x
->value
.X_add_symbol
= add_symbol
;
819 symbolP
->x
->value
.X_op_symbol
= op_symbol
;
826 symbol_temp_new (segT seg
, fragS
*frag
, valueT ofs
)
828 return symbol_new (FAKE_LABEL_NAME
, seg
, frag
, ofs
);
832 symbol_temp_new_now (void)
834 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix ());
838 symbol_temp_new_now_octets (void)
840 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix_octets ());
844 symbol_temp_make (void)
846 return symbol_make (FAKE_LABEL_NAME
);
849 /* Implement symbol table lookup.
850 In: A symbol's name as a string: '\0' can't be part of a symbol name.
851 Out: NULL if the name was not in the symbol table, else the address
852 of a struct symbol associated with that name. */
855 symbol_find_exact (const char *name
)
857 return symbol_find_exact_noref (name
, 0);
861 symbol_find_exact_noref (const char *name
, int noref
)
863 symbolS
*sym
= symbol_entry_find (sy_hash
, name
);
865 /* Any references to the symbol, except for the reference in
866 .weakref, must clear this flag, such that the symbol does not
867 turn into a weak symbol. Note that we don't have to handle the
868 local_symbol case, since a weakrefd is always promoted out of the
869 local_symbol table when it is turned into a weak symbol. */
871 S_CLEAR_WEAKREFD (sym
);
877 symbol_find (const char *name
)
879 return symbol_find_noref (name
, 0);
883 symbol_find_noref (const char *name
, int noref
)
888 #ifdef tc_canonicalize_symbol_name
890 copy
= xstrdup (name
);
891 name
= tc_canonicalize_symbol_name (copy
);
895 if (! symbols_case_sensitive
)
904 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
906 while ((c
= *orig
++) != '\0')
907 *copy
++ = TOUPPER (c
);
911 copy
= (char *) name
;
914 result
= symbol_find_exact_noref (name
, noref
);
919 /* Once upon a time, symbols were kept in a singly linked list. At
920 least coff needs to be able to rearrange them from time to time, for
921 which a doubly linked list is much more convenient. Loic did these
922 as macros which seemed dangerous to me so they're now functions.
925 /* Link symbol ADDME after symbol TARGET in the chain. */
928 symbol_append (symbolS
*addme
, symbolS
*target
,
929 symbolS
**rootPP
, symbolS
**lastPP
)
931 extern int symbol_table_frozen
;
932 if (symbol_table_frozen
)
934 if (addme
->flags
.local_symbol
)
936 if (target
!= NULL
&& target
->flags
.local_symbol
)
941 know (*rootPP
== NULL
);
942 know (*lastPP
== NULL
);
943 addme
->x
->next
= NULL
;
944 addme
->x
->previous
= NULL
;
948 } /* if the list is empty */
950 if (target
->x
->next
!= NULL
)
952 target
->x
->next
->x
->previous
= addme
;
956 know (*lastPP
== target
);
958 } /* if we have a next */
960 addme
->x
->next
= target
->x
->next
;
961 target
->x
->next
= addme
;
962 addme
->x
->previous
= target
;
964 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
967 /* Set the chain pointers of SYMBOL to null. */
970 symbol_clear_list_pointers (symbolS
*symbolP
)
972 if (symbolP
->flags
.local_symbol
)
974 symbolP
->x
->next
= NULL
;
975 symbolP
->x
->previous
= NULL
;
978 /* Remove SYMBOLP from the list. */
981 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
983 if (symbolP
->flags
.local_symbol
)
986 if (symbolP
== *rootPP
)
988 *rootPP
= symbolP
->x
->next
;
989 } /* if it was the root */
991 if (symbolP
== *lastPP
)
993 *lastPP
= symbolP
->x
->previous
;
994 } /* if it was the tail */
996 if (symbolP
->x
->next
!= NULL
)
998 symbolP
->x
->next
->x
->previous
= symbolP
->x
->previous
;
1001 if (symbolP
->x
->previous
!= NULL
)
1003 symbolP
->x
->previous
->x
->next
= symbolP
->x
->next
;
1004 } /* if not first */
1006 debug_verify_symchain (*rootPP
, *lastPP
);
1009 /* Link symbol ADDME before symbol TARGET in the chain. */
1012 symbol_insert (symbolS
*addme
, symbolS
*target
,
1013 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
1015 extern int symbol_table_frozen
;
1016 if (symbol_table_frozen
)
1018 if (addme
->flags
.local_symbol
)
1020 if (target
->flags
.local_symbol
)
1023 if (target
->x
->previous
!= NULL
)
1025 target
->x
->previous
->x
->next
= addme
;
1029 know (*rootPP
== target
);
1031 } /* if not first */
1033 addme
->x
->previous
= target
->x
->previous
;
1034 target
->x
->previous
= addme
;
1035 addme
->x
->next
= target
;
1037 debug_verify_symchain (*rootPP
, *lastPP
);
1041 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
1043 symbolS
*symbolP
= rootP
;
1045 if (symbolP
== NULL
)
1048 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
1050 gas_assert (symbolP
->bsym
!= NULL
);
1051 gas_assert (symbolP
->flags
.local_symbol
== 0);
1052 gas_assert (symbolP
->x
->next
->x
->previous
== symbolP
);
1055 gas_assert (lastP
== symbolP
);
1059 symbol_on_chain (symbolS
*s
, symbolS
*rootPP
, symbolS
*lastPP
)
1061 return (!s
->flags
.local_symbol
1062 && ((s
->x
->next
!= s
1063 && s
->x
->next
!= NULL
1064 && s
->x
->next
->x
->previous
== s
)
1066 && ((s
->x
->previous
!= s
1067 && s
->x
->previous
!= NULL
1068 && s
->x
->previous
->x
->next
== s
)
1072 #ifdef OBJ_COMPLEX_RELC
1075 use_complex_relocs_for (symbolS
* symp
)
1077 switch (symp
->x
->value
.X_op
)
1087 case O_bit_inclusive_or
:
1089 case O_bit_exclusive_or
:
1101 if ((S_IS_COMMON (symp
->x
->value
.X_op_symbol
)
1102 || S_IS_LOCAL (symp
->x
->value
.X_op_symbol
))
1103 && S_IS_DEFINED (symp
->x
->value
.X_op_symbol
)
1104 && S_GET_SEGMENT (symp
->x
->value
.X_op_symbol
) != expr_section
)
1111 if ((S_IS_COMMON (symp
->x
->value
.X_add_symbol
)
1112 || S_IS_LOCAL (symp
->x
->value
.X_add_symbol
))
1113 && S_IS_DEFINED (symp
->x
->value
.X_add_symbol
)
1114 && S_GET_SEGMENT (symp
->x
->value
.X_add_symbol
) != expr_section
)
1127 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
1131 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1132 segT seg_right
= S_GET_SEGMENT (right
);
1141 case O_uminus
: opname
= "-"; break;
1142 case O_bit_not
: opname
= "~"; break;
1143 case O_logical_not
: opname
= "!"; break;
1144 case O_multiply
: opname
= "*"; break;
1145 case O_divide
: opname
= "/"; break;
1146 case O_modulus
: opname
= "%"; break;
1147 case O_left_shift
: opname
= "<<"; break;
1148 case O_right_shift
: opname
= ">>"; break;
1149 case O_bit_inclusive_or
: opname
= "|"; break;
1150 case O_bit_or_not
: opname
= "|~"; break;
1151 case O_bit_exclusive_or
: opname
= "^"; break;
1152 case O_bit_and
: opname
= "&"; break;
1153 case O_add
: opname
= "+"; break;
1154 case O_subtract
: opname
= "-"; break;
1155 case O_eq
: opname
= "=="; break;
1156 case O_ne
: opname
= "!="; break;
1157 case O_lt
: opname
= "<"; break;
1158 case O_le
: opname
= "<="; break;
1159 case O_ge
: opname
= ">="; break;
1160 case O_gt
: opname
= ">"; break;
1161 case O_logical_and
: opname
= "&&"; break;
1162 case O_logical_or
: opname
= "||"; break;
1165 if (expr_symbol_where (symp
, &file
, &line
))
1168 as_bad_where (file
, line
,
1169 _("invalid operands (%s and %s sections) for `%s'"),
1170 seg_left
->name
, seg_right
->name
, opname
);
1172 as_bad_where (file
, line
,
1173 _("invalid operand (%s section) for `%s'"),
1174 seg_right
->name
, opname
);
1178 const char *sname
= S_GET_NAME (symp
);
1181 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1182 seg_left
->name
, seg_right
->name
, opname
, sname
);
1184 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1185 seg_right
->name
, opname
, sname
);
1189 /* Resolve the value of a symbol. This is called during the final
1190 pass over the symbol table to resolve any symbols with complex
1194 resolve_symbol_value (symbolS
*symp
)
1200 if (symp
->flags
.local_symbol
)
1202 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1204 final_val
= locsym
->value
;
1205 if (locsym
->flags
.resolved
)
1208 /* Symbols whose section has SEC_ELF_OCTETS set,
1209 resolve to octets instead of target bytes. */
1210 if (locsym
->section
->flags
& SEC_OCTETS
)
1211 final_val
+= locsym
->frag
->fr_address
;
1213 final_val
+= locsym
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1217 locsym
->value
= final_val
;
1218 locsym
->flags
.resolved
= 1;
1224 if (symp
->flags
.resolved
)
1227 while (symp
->x
->value
.X_op
== O_symbol
)
1229 final_val
+= symp
->x
->value
.X_add_number
;
1230 symp
= symp
->x
->value
.X_add_symbol
;
1231 if (symp
->flags
.local_symbol
)
1233 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1234 final_val
+= locsym
->value
;
1237 if (!symp
->flags
.resolved
)
1240 if (symp
->x
->value
.X_op
== O_constant
)
1241 final_val
+= symp
->x
->value
.X_add_number
;
1248 final_seg
= S_GET_SEGMENT (symp
);
1250 if (symp
->flags
.resolving
)
1253 as_bad (_("symbol definition loop encountered at `%s'"),
1258 #ifdef OBJ_COMPLEX_RELC
1259 else if (final_seg
== expr_section
1260 && use_complex_relocs_for (symp
))
1262 symbolS
* relc_symbol
= NULL
;
1263 char * relc_symbol_name
= NULL
;
1265 relc_symbol_name
= symbol_relc_make_expr (& symp
->x
->value
);
1267 /* For debugging, print out conversion input & output. */
1269 print_expr (& symp
->x
->value
);
1270 if (relc_symbol_name
)
1271 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1274 if (relc_symbol_name
!= NULL
)
1275 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1276 &zero_address_frag
, 0);
1278 if (relc_symbol
== NULL
)
1280 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1286 symbol_table_insert (relc_symbol
);
1288 /* S_CLEAR_EXTERNAL (relc_symbol); */
1289 if (symp
->bsym
->flags
& BSF_SRELC
)
1290 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1292 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1293 /* symp->bsym->flags |= BSF_RELC; */
1294 copy_symbol_attributes (symp
, relc_symbol
);
1295 symp
->x
->value
.X_op
= O_symbol
;
1296 symp
->x
->value
.X_add_symbol
= relc_symbol
;
1297 symp
->x
->value
.X_add_number
= 0;
1302 final_seg
= undefined_section
;
1303 goto exit_dont_set_value
;
1308 symbolS
*add_symbol
, *op_symbol
;
1309 offsetT left
, right
;
1310 segT seg_left
, seg_right
;
1314 symp
->flags
.resolving
= 1;
1316 /* Help out with CSE. */
1317 add_symbol
= symp
->x
->value
.X_add_symbol
;
1318 op_symbol
= symp
->x
->value
.X_op_symbol
;
1319 final_val
= symp
->x
->value
.X_add_number
;
1320 op
= symp
->x
->value
.X_op
;
1333 /* Symbols whose section has SEC_ELF_OCTETS set,
1334 resolve to octets instead of target bytes. */
1335 if (symp
->bsym
->section
->flags
& SEC_OCTETS
)
1336 final_val
+= symp
->frag
->fr_address
;
1338 final_val
+= symp
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1339 if (final_seg
== expr_section
)
1340 final_seg
= absolute_section
;
1349 left
= resolve_symbol_value (add_symbol
);
1350 seg_left
= S_GET_SEGMENT (add_symbol
);
1352 symp
->x
->value
.X_op_symbol
= NULL
;
1355 if (S_IS_WEAKREFR (symp
))
1357 gas_assert (final_val
== 0);
1358 if (S_IS_WEAKREFR (add_symbol
))
1360 gas_assert (add_symbol
->x
->value
.X_op
== O_symbol
1361 && add_symbol
->x
->value
.X_add_number
== 0);
1362 add_symbol
= add_symbol
->x
->value
.X_add_symbol
;
1363 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1364 symp
->x
->value
.X_add_symbol
= add_symbol
;
1368 if (symp
->flags
.mri_common
)
1370 /* This is a symbol inside an MRI common section. The
1371 relocation routines are going to handle it specially.
1372 Don't change the value. */
1373 resolved
= symbol_resolved_p (add_symbol
);
1377 /* Don't leave symbol loops. */
1379 && !add_symbol
->flags
.local_symbol
1380 && add_symbol
->flags
.resolving
)
1383 if (finalize_syms
&& final_val
== 0)
1385 if (add_symbol
->flags
.local_symbol
)
1386 add_symbol
= local_symbol_convert (add_symbol
);
1387 copy_symbol_attributes (symp
, add_symbol
);
1390 /* If we have equated this symbol to an undefined or common
1391 symbol, keep X_op set to O_symbol, and don't change
1392 X_add_number. This permits the routine which writes out
1393 relocation to detect this case, and convert the
1394 relocation to be against the symbol to which this symbol
1396 if (seg_left
== undefined_section
1397 || bfd_is_com_section (seg_left
)
1398 #if defined (OBJ_COFF) && defined (TE_PE)
1399 || S_IS_WEAK (add_symbol
)
1402 && ((final_seg
== expr_section
1403 && seg_left
!= expr_section
1404 && seg_left
!= absolute_section
)
1405 || symbol_shadow_p (symp
))))
1409 symp
->x
->value
.X_op
= O_symbol
;
1410 symp
->x
->value
.X_add_symbol
= add_symbol
;
1411 symp
->x
->value
.X_add_number
= final_val
;
1412 /* Use X_op_symbol as a flag. */
1413 symp
->x
->value
.X_op_symbol
= add_symbol
;
1415 final_seg
= seg_left
;
1416 final_val
+= symp
->frag
->fr_address
+ left
;
1417 resolved
= symbol_resolved_p (add_symbol
);
1418 symp
->flags
.resolving
= 0;
1419 goto exit_dont_set_value
;
1423 final_val
+= symp
->frag
->fr_address
+ left
;
1424 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1425 final_seg
= seg_left
;
1428 resolved
= symbol_resolved_p (add_symbol
);
1429 if (S_IS_WEAKREFR (symp
))
1431 symp
->flags
.resolving
= 0;
1432 goto exit_dont_set_value
;
1439 left
= resolve_symbol_value (add_symbol
);
1440 seg_left
= S_GET_SEGMENT (add_symbol
);
1442 /* By reducing these to the relevant dyadic operator, we get
1443 !S -> S == 0 permitted on anything,
1444 -S -> 0 - S only permitted on absolute
1445 ~S -> S ^ ~0 only permitted on absolute */
1446 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1448 report_op_error (symp
, NULL
, op
, add_symbol
);
1450 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1451 final_seg
= absolute_section
;
1455 else if (op
== O_logical_not
)
1460 final_val
+= left
+ symp
->frag
->fr_address
;
1462 resolved
= symbol_resolved_p (add_symbol
);
1470 case O_bit_inclusive_or
:
1472 case O_bit_exclusive_or
:
1484 left
= resolve_symbol_value (add_symbol
);
1485 right
= resolve_symbol_value (op_symbol
);
1486 seg_left
= S_GET_SEGMENT (add_symbol
);
1487 seg_right
= S_GET_SEGMENT (op_symbol
);
1489 /* Simplify addition or subtraction of a constant by folding the
1490 constant into X_add_number. */
1493 if (seg_right
== absolute_section
)
1498 else if (seg_left
== absolute_section
)
1501 add_symbol
= op_symbol
;
1503 seg_left
= seg_right
;
1507 else if (op
== O_subtract
)
1509 if (seg_right
== absolute_section
)
1517 /* Equality and non-equality tests are permitted on anything.
1518 Subtraction, and other comparison operators are permitted if
1519 both operands are in the same section. Otherwise, both
1520 operands must be absolute. We already handled the case of
1521 addition or subtraction of a constant above. This will
1522 probably need to be changed for an object file format which
1523 supports arbitrary expressions. */
1524 if (!(seg_left
== absolute_section
1525 && seg_right
== absolute_section
)
1526 && !(op
== O_eq
|| op
== O_ne
)
1527 && !((op
== O_subtract
1528 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1529 && seg_left
== seg_right
1530 && (seg_left
!= undefined_section
1531 || add_symbol
== op_symbol
)))
1533 /* Don't emit messages unless we're finalizing the symbol value,
1534 otherwise we may get the same message multiple times. */
1536 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1537 /* However do not move the symbol into the absolute section
1538 if it cannot currently be resolved - this would confuse
1539 other parts of the assembler into believing that the
1540 expression had been evaluated to zero. */
1546 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1547 final_seg
= absolute_section
;
1549 /* Check for division by zero. */
1550 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1552 /* If seg_right is not absolute_section, then we've
1553 already issued a warning about using a bad symbol. */
1554 if (seg_right
== absolute_section
&& finalize_syms
)
1559 if (expr_symbol_where (symp
, &file
, &line
))
1560 as_bad_where (file
, line
, _("division by zero"));
1562 as_bad (_("division by zero when setting `%s'"),
1568 if ((op
== O_left_shift
|| op
== O_right_shift
)
1569 && (valueT
) right
>= sizeof (valueT
) * CHAR_BIT
)
1571 as_warn_value_out_of_range (_("shift count"), right
, 0,
1572 sizeof (valueT
) * CHAR_BIT
- 1,
1577 switch (symp
->x
->value
.X_op
)
1579 case O_multiply
: left
*= right
; break;
1580 case O_divide
: left
/= right
; break;
1581 case O_modulus
: left
%= right
; break;
1583 left
= (valueT
) left
<< (valueT
) right
; break;
1585 left
= (valueT
) left
>> (valueT
) right
; break;
1586 case O_bit_inclusive_or
: left
|= right
; break;
1587 case O_bit_or_not
: left
|= ~right
; break;
1588 case O_bit_exclusive_or
: left
^= right
; break;
1589 case O_bit_and
: left
&= right
; break;
1590 case O_add
: left
+= right
; break;
1591 case O_subtract
: left
-= right
; break;
1594 left
= (left
== right
&& seg_left
== seg_right
1595 && (seg_left
!= undefined_section
1596 || add_symbol
== op_symbol
)
1597 ? ~ (offsetT
) 0 : 0);
1598 if (symp
->x
->value
.X_op
== O_ne
)
1601 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1602 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1603 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1604 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1605 case O_logical_and
: left
= left
&& right
; break;
1606 case O_logical_or
: left
= left
|| right
; break;
1611 /* See PR 20895 for a reproducer. */
1612 as_bad (_("Invalid operation on symbol"));
1613 goto exit_dont_set_value
;
1619 final_val
+= symp
->frag
->fr_address
+ left
;
1620 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1622 if (seg_left
== undefined_section
1623 || seg_right
== undefined_section
)
1624 final_seg
= undefined_section
;
1625 else if (seg_left
== absolute_section
)
1626 final_seg
= seg_right
;
1628 final_seg
= seg_left
;
1630 resolved
= (symbol_resolved_p (add_symbol
)
1631 && symbol_resolved_p (op_symbol
));
1636 /* Give an error (below) if not in expr_section. We don't
1637 want to worry about expr_section symbols, because they
1638 are fictional (they are created as part of expression
1639 resolution), and any problems may not actually mean
1644 symp
->flags
.resolving
= 0;
1648 S_SET_VALUE (symp
, final_val
);
1650 exit_dont_set_value
:
1651 /* Always set the segment, even if not finalizing the value.
1652 The segment is used to determine whether a symbol is defined. */
1653 S_SET_SEGMENT (symp
, final_seg
);
1655 /* Don't worry if we can't resolve an expr_section symbol. */
1659 symp
->flags
.resolved
= 1;
1660 else if (S_GET_SEGMENT (symp
) != expr_section
)
1662 as_bad (_("can't resolve value for symbol `%s'"),
1664 symp
->flags
.resolved
= 1;
1671 /* A static function passed to hash_traverse. */
1674 resolve_local_symbol (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1676 symbol_entry_t
*entry
= *((symbol_entry_t
**) slot
);
1677 if (entry
->sy
.flags
.local_symbol
)
1678 resolve_symbol_value (&entry
->sy
);
1683 /* Resolve all local symbols. */
1686 resolve_local_symbol_values (void)
1688 htab_traverse (sy_hash
, resolve_local_symbol
, NULL
);
1691 /* Obtain the current value of a symbol without changing any
1692 sub-expressions used. */
1695 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1697 symbolS
*symbolP
= *symbolPP
;
1699 if (symbolP
->flags
.local_symbol
)
1701 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1703 *valueP
= locsym
->value
;
1704 *segP
= locsym
->section
;
1705 *fragPP
= locsym
->frag
;
1709 expressionS exp
= symbolP
->x
->value
;
1711 if (!symbolP
->flags
.resolved
&& exp
.X_op
!= O_illegal
)
1715 if (symbolP
->flags
.resolving
)
1717 symbolP
->flags
.resolving
= 1;
1718 resolved
= resolve_expression (&exp
);
1719 symbolP
->flags
.resolving
= 0;
1727 if (!symbol_equated_p (symbolP
))
1732 symbolP
= exp
.X_add_symbol
;
1739 *symbolPP
= symbolP
;
1741 /* A bogus input file can result in resolve_expression()
1742 generating a local symbol, so we have to check again. */
1743 if (symbolP
->flags
.local_symbol
)
1745 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1747 *valueP
= locsym
->value
;
1748 *segP
= locsym
->section
;
1749 *fragPP
= locsym
->frag
;
1753 *valueP
= exp
.X_add_number
;
1754 *segP
= symbolP
->bsym
->section
;
1755 *fragPP
= symbolP
->frag
;
1758 if (*segP
== expr_section
)
1761 case O_constant
: *segP
= absolute_section
; break;
1762 case O_register
: *segP
= reg_section
; break;
1770 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1771 They are *really* local. That is, they go out of scope whenever we see a
1772 label that isn't local. Also, like fb labels, there can be multiple
1773 instances of a dollar label. Therefor, we name encode each instance with
1774 the instance number, keep a list of defined symbols separate from the real
1775 symbol table, and we treat these buggers as a sparse array. */
1777 static long *dollar_labels
;
1778 static long *dollar_label_instances
;
1779 static char *dollar_label_defines
;
1780 static size_t dollar_label_count
;
1781 static size_t dollar_label_max
;
1784 dollar_label_defined (long label
)
1788 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1790 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1792 return dollar_label_defines
[i
- dollar_labels
];
1794 /* If we get here, label isn't defined. */
1799 dollar_label_instance (long label
)
1803 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1805 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1807 return (dollar_label_instances
[i
- dollar_labels
]);
1809 /* If we get here, we haven't seen the label before.
1810 Therefore its instance count is zero. */
1815 dollar_label_clear (void)
1817 if (dollar_label_count
)
1818 memset (dollar_label_defines
, '\0', dollar_label_count
);
1821 #define DOLLAR_LABEL_BUMP_BY 10
1824 define_dollar_label (long label
)
1828 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1831 ++dollar_label_instances
[i
- dollar_labels
];
1832 dollar_label_defines
[i
- dollar_labels
] = 1;
1836 /* If we get to here, we don't have label listed yet. */
1838 if (dollar_labels
== NULL
)
1840 dollar_labels
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1841 dollar_label_instances
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1842 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1843 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1844 dollar_label_count
= 0;
1846 else if (dollar_label_count
== dollar_label_max
)
1848 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1849 dollar_labels
= XRESIZEVEC (long, dollar_labels
, dollar_label_max
);
1850 dollar_label_instances
= XRESIZEVEC (long, dollar_label_instances
,
1852 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1854 } /* if we needed to grow */
1856 dollar_labels
[dollar_label_count
] = label
;
1857 dollar_label_instances
[dollar_label_count
] = 1;
1858 dollar_label_defines
[dollar_label_count
] = 1;
1859 ++dollar_label_count
;
1862 /* Caller must copy returned name: we re-use the area for the next name.
1864 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1865 where n is the label number and m is the instance number. "L" makes
1866 it a label discarded unless debugging and "^A"('\1') ensures no
1867 ordinary symbol SHOULD get the same name as a local label
1868 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1870 fb labels get the same treatment, except that ^B is used in place
1873 char * /* Return local label name. */
1874 dollar_label_name (long n
, /* we just saw "n$:" : n a number. */
1875 int augend
/* 0 for current instance, 1 for new instance. */)
1878 /* Returned to caller, then copied. Used for created names ("4f"). */
1879 static char symbol_name_build
[24];
1882 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1885 know (augend
== 0 || augend
== 1);
1886 p
= symbol_name_build
;
1887 #ifdef LOCAL_LABEL_PREFIX
1888 *p
++ = LOCAL_LABEL_PREFIX
;
1892 /* Next code just does sprintf( {}, "%d", n); */
1894 q
= symbol_name_temporary
;
1895 for (*q
++ = 0, i
= n
; i
; ++q
)
1900 while ((*p
= *--q
) != '\0')
1903 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1905 /* Instance number. */
1906 q
= symbol_name_temporary
;
1907 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1912 while ((*p
++ = *--q
) != '\0');
1914 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1915 return symbol_name_build
;
1918 /* Somebody else's idea of local labels. They are made by "n:" where n
1919 is any decimal digit. Refer to them with
1920 "nb" for previous (backward) n:
1921 or "nf" for next (forward) n:.
1923 We do a little better and let n be any number, not just a single digit, but
1924 since the other guy's assembler only does ten, we treat the first ten
1927 Like someone else's assembler, we have one set of local label counters for
1928 entire assembly, not one set per (sub)segment like in most assemblers. This
1929 implies that one can refer to a label in another segment, and indeed some
1930 crufty compilers have done just that.
1932 Since there could be a LOT of these things, treat them as a sparse
1935 #define FB_LABEL_SPECIAL (10)
1937 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1938 static long *fb_labels
;
1939 static long *fb_label_instances
;
1940 static long fb_label_count
;
1941 static long fb_label_max
;
1943 /* This must be more than FB_LABEL_SPECIAL. */
1944 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1947 fb_label_init (void)
1949 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1952 /* Add one to the instance number of this fb label. */
1955 fb_label_instance_inc (long label
)
1959 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1961 ++fb_low_counter
[label
];
1965 if (fb_labels
!= NULL
)
1967 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1968 i
< fb_labels
+ fb_label_count
; ++i
)
1972 ++fb_label_instances
[i
- fb_labels
];
1974 } /* if we find it */
1975 } /* for each existing label */
1978 /* If we get to here, we don't have label listed yet. */
1980 if (fb_labels
== NULL
)
1982 fb_labels
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1983 fb_label_instances
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1984 fb_label_max
= FB_LABEL_BUMP_BY
;
1985 fb_label_count
= FB_LABEL_SPECIAL
;
1988 else if (fb_label_count
== fb_label_max
)
1990 fb_label_max
+= FB_LABEL_BUMP_BY
;
1991 fb_labels
= XRESIZEVEC (long, fb_labels
, fb_label_max
);
1992 fb_label_instances
= XRESIZEVEC (long, fb_label_instances
, fb_label_max
);
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 (long label
)
2005 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
2007 return (fb_low_counter
[label
]);
2010 if (fb_labels
!= NULL
)
2012 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
2013 i
< fb_labels
+ fb_label_count
; ++i
)
2017 return (fb_label_instances
[i
- fb_labels
]);
2018 } /* if we find it */
2019 } /* for each existing label */
2022 /* We didn't find the label, so this must be a reference to the
2027 /* Caller must copy returned name: we re-use the area for the next name.
2029 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2030 where n is the label number and m is the instance number. "L" makes
2031 it a label discarded unless debugging and "^B"('\2') ensures no
2032 ordinary symbol SHOULD get the same name as a local label
2033 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2035 dollar labels get the same treatment, except that ^A is used in
2038 char * /* Return local label name. */
2039 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
2040 long augend
/* 0 for nb, 1 for n:, nf. */)
2043 /* Returned to caller, then copied. Used for created names ("4f"). */
2044 static char symbol_name_build
[24];
2047 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
2051 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
2053 know ((unsigned long) augend
<= 1);
2055 p
= symbol_name_build
;
2056 #ifdef LOCAL_LABEL_PREFIX
2057 *p
++ = LOCAL_LABEL_PREFIX
;
2061 /* Next code just does sprintf( {}, "%d", n); */
2063 q
= symbol_name_temporary
;
2064 for (*q
++ = 0, i
= n
; i
; ++q
)
2069 while ((*p
= *--q
) != '\0')
2072 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
2074 /* Instance number. */
2075 q
= symbol_name_temporary
;
2076 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
2081 while ((*p
++ = *--q
) != '\0');
2083 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2084 return (symbol_name_build
);
2087 /* Decode name that may have been generated by foo_label_name() above.
2088 If the name wasn't generated by foo_label_name(), then return it
2089 unaltered. This is used for error messages. */
2092 decode_local_label_name (char *s
)
2095 char *symbol_decode
;
2097 int instance_number
;
2099 const char *message_format
;
2102 #ifdef LOCAL_LABEL_PREFIX
2103 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
2107 if (s
[lindex
] != 'L')
2110 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
2111 label_number
= (10 * label_number
) + *p
- '0';
2113 if (*p
== DOLLAR_LABEL_CHAR
)
2115 else if (*p
== LOCAL_LABEL_CHAR
)
2120 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
2121 instance_number
= (10 * instance_number
) + *p
- '0';
2123 message_format
= _("\"%d\" (instance number %d of a %s label)");
2124 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
2125 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
2127 return symbol_decode
;
2130 /* Get the value of a symbol. */
2133 S_GET_VALUE (symbolS
*s
)
2135 if (s
->flags
.local_symbol
)
2136 return resolve_symbol_value (s
);
2138 if (!s
->flags
.resolved
)
2140 valueT val
= resolve_symbol_value (s
);
2144 if (S_IS_WEAKREFR (s
))
2145 return S_GET_VALUE (s
->x
->value
.X_add_symbol
);
2147 if (s
->x
->value
.X_op
!= O_constant
)
2149 if (! s
->flags
.resolved
2150 || s
->x
->value
.X_op
!= O_symbol
2151 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
2152 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2155 return (valueT
) s
->x
->value
.X_add_number
;
2158 /* Set the value of a symbol. */
2161 S_SET_VALUE (symbolS
*s
, valueT val
)
2163 if (s
->flags
.local_symbol
)
2165 ((struct local_symbol
*) s
)->value
= val
;
2169 s
->x
->value
.X_op
= O_constant
;
2170 s
->x
->value
.X_add_number
= (offsetT
) val
;
2171 s
->x
->value
.X_unsigned
= 0;
2172 S_CLEAR_WEAKREFR (s
);
2176 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2178 if (dest
->flags
.local_symbol
)
2179 dest
= local_symbol_convert (dest
);
2180 if (src
->flags
.local_symbol
)
2181 src
= local_symbol_convert (src
);
2183 /* In an expression, transfer the settings of these flags.
2184 The user can override later, of course. */
2185 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2186 | BSF_GNU_INDIRECT_FUNCTION)
2187 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2189 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2190 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2193 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2194 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2199 S_IS_FUNCTION (symbolS
*s
)
2203 if (s
->flags
.local_symbol
)
2206 flags
= s
->bsym
->flags
;
2208 return (flags
& BSF_FUNCTION
) != 0;
2212 S_IS_EXTERNAL (symbolS
*s
)
2216 if (s
->flags
.local_symbol
)
2219 flags
= s
->bsym
->flags
;
2222 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2225 return (flags
& BSF_GLOBAL
) != 0;
2229 S_IS_WEAK (symbolS
*s
)
2231 if (s
->flags
.local_symbol
)
2233 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2234 could probably handle a WEAKREFR as always weak though. E.g., if
2235 the referenced symbol has lost its weak status, there's no reason
2236 to keep handling the weakrefr as if it was weak. */
2237 if (S_IS_WEAKREFR (s
))
2238 return S_IS_WEAK (s
->x
->value
.X_add_symbol
);
2239 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2243 S_IS_WEAKREFR (symbolS
*s
)
2245 if (s
->flags
.local_symbol
)
2247 return s
->flags
.weakrefr
!= 0;
2251 S_IS_WEAKREFD (symbolS
*s
)
2253 if (s
->flags
.local_symbol
)
2255 return s
->flags
.weakrefd
!= 0;
2259 S_IS_COMMON (symbolS
*s
)
2261 if (s
->flags
.local_symbol
)
2263 return bfd_is_com_section (s
->bsym
->section
);
2267 S_IS_DEFINED (symbolS
*s
)
2269 if (s
->flags
.local_symbol
)
2270 return ((struct local_symbol
*) s
)->section
!= undefined_section
;
2271 return s
->bsym
->section
!= undefined_section
;
2275 #ifndef EXTERN_FORCE_RELOC
2276 #define EXTERN_FORCE_RELOC IS_ELF
2279 /* Return true for symbols that should not be reduced to section
2280 symbols or eliminated from expressions, because they may be
2281 overridden by the linker. */
2283 S_FORCE_RELOC (symbolS
*s
, int strict
)
2286 if (s
->flags
.local_symbol
)
2287 sec
= ((struct local_symbol
*) s
)->section
;
2291 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2292 || (EXTERN_FORCE_RELOC
2293 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2294 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2296 sec
= s
->bsym
->section
;
2298 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2302 S_IS_DEBUG (symbolS
*s
)
2304 if (s
->flags
.local_symbol
)
2306 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2312 S_IS_LOCAL (symbolS
*s
)
2317 if (s
->flags
.local_symbol
)
2320 flags
= s
->bsym
->flags
;
2323 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2326 if (bfd_asymbol_section (s
->bsym
) == reg_section
)
2329 if (flag_strip_local_absolute
2330 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2331 the source file even when the object file is stripped. */
2332 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2333 && bfd_asymbol_section (s
->bsym
) == absolute_section
)
2336 name
= S_GET_NAME (s
);
2337 return (name
!= NULL
2339 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2340 || strchr (name
, LOCAL_LABEL_CHAR
)
2341 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2342 || strchr (name
, FAKE_LABEL_CHAR
)
2344 || TC_LABEL_IS_LOCAL (name
)
2345 || (! flag_keep_locals
2346 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2349 && name
[1] == '?')))));
2353 S_IS_STABD (symbolS
*s
)
2355 return S_GET_NAME (s
) == 0;
2359 S_CAN_BE_REDEFINED (const symbolS
*s
)
2361 if (s
->flags
.local_symbol
)
2362 return (((struct local_symbol
*) s
)->frag
2363 == &predefined_address_frag
);
2364 /* Permit register names to be redefined. */
2365 return s
->bsym
->section
== reg_section
;
2369 S_IS_VOLATILE (const symbolS
*s
)
2371 if (s
->flags
.local_symbol
)
2373 return s
->flags
.volatil
;
2377 S_IS_FORWARD_REF (const symbolS
*s
)
2379 if (s
->flags
.local_symbol
)
2381 return s
->flags
.forward_ref
;
2385 S_GET_NAME (symbolS
*s
)
2391 S_GET_SEGMENT (symbolS
*s
)
2393 if (s
->flags
.local_symbol
)
2394 return ((struct local_symbol
*) s
)->section
;
2395 return s
->bsym
->section
;
2399 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2401 if (s
->flags
.local_symbol
)
2403 ((struct local_symbol
*) s
)->section
= seg
;
2407 /* Don't reassign section symbols. The direct reason is to prevent seg
2408 faults assigning back to const global symbols such as *ABS*, but it
2409 shouldn't happen anyway. */
2410 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2412 if (s
->bsym
->section
!= seg
)
2416 s
->bsym
->section
= seg
;
2420 S_SET_EXTERNAL (symbolS
*s
)
2422 if (s
->flags
.local_symbol
)
2423 s
= local_symbol_convert (s
);
2424 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2426 /* Let .weak override .global. */
2429 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2431 /* Do not reassign section symbols. */
2432 as_warn (_("can't make section symbol global"));
2435 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2436 if (S_GET_SEGMENT (s
) == reg_section
)
2438 as_bad (_("can't make register symbol global"));
2442 s
->bsym
->flags
|= BSF_GLOBAL
;
2443 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2446 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2447 an_external_name
= S_GET_NAME (s
);
2452 S_CLEAR_EXTERNAL (symbolS
*s
)
2454 if (s
->flags
.local_symbol
)
2456 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2458 /* Let .weak override. */
2461 s
->bsym
->flags
|= BSF_LOCAL
;
2462 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2466 S_SET_WEAK (symbolS
*s
)
2468 if (s
->flags
.local_symbol
)
2469 s
= local_symbol_convert (s
);
2470 #ifdef obj_set_weak_hook
2471 obj_set_weak_hook (s
);
2473 s
->bsym
->flags
|= BSF_WEAK
;
2474 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2478 S_SET_WEAKREFR (symbolS
*s
)
2480 if (s
->flags
.local_symbol
)
2481 s
= local_symbol_convert (s
);
2482 s
->flags
.weakrefr
= 1;
2483 /* If the alias was already used, make sure we mark the target as
2484 used as well, otherwise it might be dropped from the symbol
2485 table. This may have unintended side effects if the alias is
2486 later redirected to another symbol, such as keeping the unused
2487 previous target in the symbol table. Since it will be weak, it's
2490 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2494 S_CLEAR_WEAKREFR (symbolS
*s
)
2496 if (s
->flags
.local_symbol
)
2498 s
->flags
.weakrefr
= 0;
2502 S_SET_WEAKREFD (symbolS
*s
)
2504 if (s
->flags
.local_symbol
)
2505 s
= local_symbol_convert (s
);
2506 s
->flags
.weakrefd
= 1;
2511 S_CLEAR_WEAKREFD (symbolS
*s
)
2513 if (s
->flags
.local_symbol
)
2515 if (s
->flags
.weakrefd
)
2517 s
->flags
.weakrefd
= 0;
2518 /* If a weakref target symbol is weak, then it was never
2519 referenced directly before, not even in a .global directive,
2520 so decay it to local. If it remains undefined, it will be
2521 later turned into a global, like any other undefined
2523 if (s
->bsym
->flags
& BSF_WEAK
)
2525 #ifdef obj_clear_weak_hook
2526 obj_clear_weak_hook (s
);
2528 s
->bsym
->flags
&= ~BSF_WEAK
;
2529 s
->bsym
->flags
|= BSF_LOCAL
;
2535 S_SET_THREAD_LOCAL (symbolS
*s
)
2537 if (s
->flags
.local_symbol
)
2538 s
= local_symbol_convert (s
);
2539 if (bfd_is_com_section (s
->bsym
->section
)
2540 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2542 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2543 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2544 as_bad (_("Accessing function `%s' as thread-local object"),
2546 else if (! bfd_is_und_section (s
->bsym
->section
)
2547 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2548 as_bad (_("Accessing `%s' as thread-local object"),
2553 S_SET_NAME (symbolS
*s
, const char *name
)
2556 if (s
->flags
.local_symbol
)
2558 s
->bsym
->name
= name
;
2562 S_SET_VOLATILE (symbolS
*s
)
2564 if (s
->flags
.local_symbol
)
2565 s
= local_symbol_convert (s
);
2566 s
->flags
.volatil
= 1;
2570 S_CLEAR_VOLATILE (symbolS
*s
)
2572 if (!s
->flags
.local_symbol
)
2573 s
->flags
.volatil
= 0;
2577 S_SET_FORWARD_REF (symbolS
*s
)
2579 if (s
->flags
.local_symbol
)
2580 s
= local_symbol_convert (s
);
2581 s
->flags
.forward_ref
= 1;
2584 /* Return the previous symbol in a chain. */
2587 symbol_previous (symbolS
*s
)
2589 if (s
->flags
.local_symbol
)
2591 return s
->x
->previous
;
2594 /* Return the next symbol in a chain. */
2597 symbol_next (symbolS
*s
)
2599 if (s
->flags
.local_symbol
)
2604 /* Return a pointer to the value of a symbol as an expression. */
2607 symbol_get_value_expression (symbolS
*s
)
2609 if (s
->flags
.local_symbol
)
2610 s
= local_symbol_convert (s
);
2611 return &s
->x
->value
;
2614 /* Set the value of a symbol to an expression. */
2617 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2619 if (s
->flags
.local_symbol
)
2620 s
= local_symbol_convert (s
);
2622 S_CLEAR_WEAKREFR (s
);
2625 /* Return whether 2 symbols are the same. */
2628 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2633 /* Return a pointer to the X_add_number component of a symbol. */
2636 symbol_X_add_number (symbolS
*s
)
2638 if (s
->flags
.local_symbol
)
2639 return (offsetT
*) &((struct local_symbol
*) s
)->value
;
2641 return &s
->x
->value
.X_add_number
;
2644 /* Set the value of SYM to the current position in the current segment. */
2647 symbol_set_value_now (symbolS
*sym
)
2649 S_SET_SEGMENT (sym
, now_seg
);
2650 S_SET_VALUE (sym
, frag_now_fix ());
2651 symbol_set_frag (sym
, frag_now
);
2654 /* Set the frag of a symbol. */
2657 symbol_set_frag (symbolS
*s
, fragS
*f
)
2659 if (s
->flags
.local_symbol
)
2661 ((struct local_symbol
*) s
)->frag
= f
;
2665 S_CLEAR_WEAKREFR (s
);
2668 /* Return the frag of a symbol. */
2671 symbol_get_frag (symbolS
*s
)
2673 if (s
->flags
.local_symbol
)
2674 return ((struct local_symbol
*) s
)->frag
;
2678 /* Mark a symbol as having been used. */
2681 symbol_mark_used (symbolS
*s
)
2683 if (s
->flags
.local_symbol
)
2686 if (S_IS_WEAKREFR (s
))
2687 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2690 /* Clear the mark of whether a symbol has been used. */
2693 symbol_clear_used (symbolS
*s
)
2695 if (s
->flags
.local_symbol
)
2696 s
= local_symbol_convert (s
);
2700 /* Return whether a symbol has been used. */
2703 symbol_used_p (symbolS
*s
)
2705 if (s
->flags
.local_symbol
)
2707 return s
->flags
.used
;
2710 /* Mark a symbol as having been used in a reloc. */
2713 symbol_mark_used_in_reloc (symbolS
*s
)
2715 if (s
->flags
.local_symbol
)
2716 s
= local_symbol_convert (s
);
2717 s
->flags
.used_in_reloc
= 1;
2720 /* Clear the mark of whether a symbol has been used in a reloc. */
2723 symbol_clear_used_in_reloc (symbolS
*s
)
2725 if (s
->flags
.local_symbol
)
2727 s
->flags
.used_in_reloc
= 0;
2730 /* Return whether a symbol has been used in a reloc. */
2733 symbol_used_in_reloc_p (symbolS
*s
)
2735 if (s
->flags
.local_symbol
)
2737 return s
->flags
.used_in_reloc
;
2740 /* Mark a symbol as an MRI common symbol. */
2743 symbol_mark_mri_common (symbolS
*s
)
2745 if (s
->flags
.local_symbol
)
2746 s
= local_symbol_convert (s
);
2747 s
->flags
.mri_common
= 1;
2750 /* Clear the mark of whether a symbol is an MRI common symbol. */
2753 symbol_clear_mri_common (symbolS
*s
)
2755 if (s
->flags
.local_symbol
)
2757 s
->flags
.mri_common
= 0;
2760 /* Return whether a symbol is an MRI common symbol. */
2763 symbol_mri_common_p (symbolS
*s
)
2765 if (s
->flags
.local_symbol
)
2767 return s
->flags
.mri_common
;
2770 /* Mark a symbol as having been written. */
2773 symbol_mark_written (symbolS
*s
)
2775 if (s
->flags
.local_symbol
)
2777 s
->flags
.written
= 1;
2780 /* Clear the mark of whether a symbol has been written. */
2783 symbol_clear_written (symbolS
*s
)
2785 if (s
->flags
.local_symbol
)
2787 s
->flags
.written
= 0;
2790 /* Return whether a symbol has been written. */
2793 symbol_written_p (symbolS
*s
)
2795 if (s
->flags
.local_symbol
)
2797 return s
->flags
.written
;
2800 /* Mark a symbol has having been resolved. */
2803 symbol_mark_resolved (symbolS
*s
)
2805 s
->flags
.resolved
= 1;
2808 /* Return whether a symbol has been resolved. */
2811 symbol_resolved_p (symbolS
*s
)
2813 return s
->flags
.resolved
;
2816 /* Return whether a symbol is a section symbol. */
2819 symbol_section_p (symbolS
*s
)
2821 if (s
->flags
.local_symbol
)
2823 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2826 /* Return whether a symbol is equated to another symbol. */
2829 symbol_equated_p (symbolS
*s
)
2831 if (s
->flags
.local_symbol
)
2833 return s
->x
->value
.X_op
== O_symbol
;
2836 /* Return whether a symbol is equated to another symbol, and should be
2837 treated specially when writing out relocs. */
2840 symbol_equated_reloc_p (symbolS
*s
)
2842 if (s
->flags
.local_symbol
)
2844 /* X_op_symbol, normally not used for O_symbol, is set by
2845 resolve_symbol_value to flag expression syms that have been
2847 return (s
->x
->value
.X_op
== O_symbol
2848 #if defined (OBJ_COFF) && defined (TE_PE)
2851 && ((s
->flags
.resolved
&& s
->x
->value
.X_op_symbol
!= NULL
)
2852 || ! S_IS_DEFINED (s
)
2853 || S_IS_COMMON (s
)));
2856 /* Return whether a symbol has a constant value. */
2859 symbol_constant_p (symbolS
*s
)
2861 if (s
->flags
.local_symbol
)
2863 return s
->x
->value
.X_op
== O_constant
;
2866 /* Return whether a symbol was cloned and thus removed from the global
2870 symbol_shadow_p (symbolS
*s
)
2872 if (s
->flags
.local_symbol
)
2874 return s
->x
->next
== s
;
2877 /* If S is a struct symbol return S, otherwise return NULL. */
2880 symbol_symbolS (symbolS
*s
)
2882 if (s
->flags
.local_symbol
)
2887 /* Return the BFD symbol for a symbol. */
2890 symbol_get_bfdsym (symbolS
*s
)
2892 if (s
->flags
.local_symbol
)
2893 s
= local_symbol_convert (s
);
2897 /* Set the BFD symbol for a symbol. */
2900 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2902 if (s
->flags
.local_symbol
)
2903 s
= local_symbol_convert (s
);
2904 /* Usually, it is harmless to reset a symbol to a BFD section
2905 symbol. For example, obj_elf_change_section sets the BFD symbol
2906 of an old symbol with the newly created section symbol. But when
2907 we have multiple sections with the same name, the newly created
2908 section may have the same name as an old section. We check if the
2909 old symbol has been already marked as a section symbol before
2911 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2913 /* else XXX - What do we do now ? */
2916 #ifdef OBJ_SYMFIELD_TYPE
2918 /* Get a pointer to the object format information for a symbol. */
2921 symbol_get_obj (symbolS
*s
)
2923 if (s
->flags
.local_symbol
)
2924 s
= local_symbol_convert (s
);
2928 /* Set the object format information for a symbol. */
2931 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2933 if (s
->flags
.local_symbol
)
2934 s
= local_symbol_convert (s
);
2938 #endif /* OBJ_SYMFIELD_TYPE */
2940 #ifdef TC_SYMFIELD_TYPE
2942 /* Get a pointer to the processor information for a symbol. */
2945 symbol_get_tc (symbolS
*s
)
2947 if (s
->flags
.local_symbol
)
2948 s
= local_symbol_convert (s
);
2952 /* Set the processor information for a symbol. */
2955 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2957 if (s
->flags
.local_symbol
)
2958 s
= local_symbol_convert (s
);
2962 #endif /* TC_SYMFIELD_TYPE */
2967 symbol_lastP
= NULL
;
2968 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2969 sy_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
2970 NULL
, xcalloc
, free
);
2972 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2973 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
2975 abs_symbol
.x
= &abs_symbol_x
;
2976 abs_symbol
.x
->value
.X_op
= O_constant
;
2977 abs_symbol
.frag
= &zero_address_frag
;
2979 if (LOCAL_LABELS_FB
)
2984 dot_symbol_init (void)
2986 dot_symbol
.name
= ".";
2987 dot_symbol
.flags
.forward_ref
= 1;
2988 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2989 if (dot_symbol
.bsym
== NULL
)
2990 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2991 dot_symbol
.bsym
->name
= ".";
2992 dot_symbol
.x
= &dot_symbol_x
;
2993 dot_symbol
.x
->value
.X_op
= O_constant
;
2998 /* Maximum indent level.
2999 Available for modification inside a gdb session. */
3000 static int max_indent_level
= 8;
3003 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
3005 const char *name
= S_GET_NAME (sym
);
3006 if (!name
|| !name
[0])
3008 fprintf (file
, "sym ");
3009 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
3010 fprintf (file
, " %s", name
);
3012 if (sym
->flags
.local_symbol
)
3014 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
3016 if (locsym
->frag
!= &zero_address_frag
3017 && locsym
->frag
!= NULL
)
3019 fprintf (file
, " frag ");
3020 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) locsym
->frag
));
3022 if (locsym
->flags
.resolved
)
3023 fprintf (file
, " resolved");
3024 fprintf (file
, " local");
3028 if (sym
->frag
!= &zero_address_frag
)
3030 fprintf (file
, " frag ");
3031 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->frag
));
3033 if (sym
->flags
.written
)
3034 fprintf (file
, " written");
3035 if (sym
->flags
.resolved
)
3036 fprintf (file
, " resolved");
3037 else if (sym
->flags
.resolving
)
3038 fprintf (file
, " resolving");
3039 if (sym
->flags
.used_in_reloc
)
3040 fprintf (file
, " used-in-reloc");
3041 if (sym
->flags
.used
)
3042 fprintf (file
, " used");
3043 if (S_IS_LOCAL (sym
))
3044 fprintf (file
, " local");
3045 if (S_IS_EXTERNAL (sym
))
3046 fprintf (file
, " extern");
3047 if (S_IS_WEAK (sym
))
3048 fprintf (file
, " weak");
3049 if (S_IS_DEBUG (sym
))
3050 fprintf (file
, " debug");
3051 if (S_IS_DEFINED (sym
))
3052 fprintf (file
, " defined");
3054 if (S_IS_WEAKREFR (sym
))
3055 fprintf (file
, " weakrefr");
3056 if (S_IS_WEAKREFD (sym
))
3057 fprintf (file
, " weakrefd");
3058 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
3059 if (symbol_resolved_p (sym
))
3061 segT s
= S_GET_SEGMENT (sym
);
3063 if (s
!= undefined_section
3064 && s
!= expr_section
)
3065 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
3067 else if (indent_level
< max_indent_level
3068 && S_GET_SEGMENT (sym
) != undefined_section
)
3071 fprintf (file
, "\n%*s<", indent_level
* 4, "");
3072 if (sym
->flags
.local_symbol
)
3073 fprintf (file
, "constant %lx",
3074 (unsigned long) ((struct local_symbol
*) sym
)->value
);
3076 print_expr_1 (file
, &sym
->x
->value
);
3077 fprintf (file
, ">");
3084 print_symbol_value (symbolS
*sym
)
3087 print_symbol_value_1 (stderr
, sym
);
3088 fprintf (stderr
, "\n");
3092 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
3095 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
3096 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3097 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3098 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3099 fprintf (file
, ">");
3104 print_expr_1 (FILE *file
, expressionS
*exp
)
3106 fprintf (file
, "expr ");
3107 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
3108 fprintf (file
, " ");
3112 fprintf (file
, "illegal");
3115 fprintf (file
, "absent");
3118 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
3122 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
3123 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3124 fprintf (file
, ">");
3126 if (exp
->X_add_number
)
3127 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
3128 (unsigned long) exp
->X_add_number
);
3132 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
3135 fprintf (file
, "big");
3138 fprintf (file
, "uminus -<");
3140 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3141 fprintf (file
, ">");
3142 goto maybe_print_addnum
;
3144 fprintf (file
, "bit_not");
3147 print_binary (file
, "multiply", exp
);
3150 print_binary (file
, "divide", exp
);
3153 print_binary (file
, "modulus", exp
);
3156 print_binary (file
, "lshift", exp
);
3159 print_binary (file
, "rshift", exp
);
3161 case O_bit_inclusive_or
:
3162 print_binary (file
, "bit_ior", exp
);
3164 case O_bit_exclusive_or
:
3165 print_binary (file
, "bit_xor", exp
);
3168 print_binary (file
, "bit_and", exp
);
3171 print_binary (file
, "eq", exp
);
3174 print_binary (file
, "ne", exp
);
3177 print_binary (file
, "lt", exp
);
3180 print_binary (file
, "le", exp
);
3183 print_binary (file
, "ge", exp
);
3186 print_binary (file
, "gt", exp
);
3189 print_binary (file
, "logical_and", exp
);
3192 print_binary (file
, "logical_or", exp
);
3196 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3197 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3198 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3199 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3200 fprintf (file
, ">");
3201 goto maybe_print_addnum
;
3204 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3205 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3206 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3207 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3208 fprintf (file
, ">");
3209 goto maybe_print_addnum
;
3211 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3218 print_expr (expressionS
*exp
)
3220 print_expr_1 (stderr
, exp
);
3221 fprintf (stderr
, "\n");
3225 symbol_print_statistics (FILE *file
)
3227 htab_print_statistics (file
, "symbol table", sy_hash
);
3228 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3229 local_symbol_count
, local_symbol_conversion_count
);
3232 #ifdef OBJ_COMPLEX_RELC
3234 /* Convert given symbol to a new complex-relocation symbol name. This
3235 may be a recursive function, since it might be called for non-leaf
3236 nodes (plain symbols) in the expression tree. The caller owns the
3237 returning string, so should free it eventually. Errors are
3238 indicated via as_bad and a NULL return value. The given symbol
3239 is marked with used_in_reloc. */
3242 symbol_relc_make_sym (symbolS
* sym
)
3244 char * terminal
= NULL
;
3249 gas_assert (sym
!= NULL
);
3251 /* Recurse to symbol_relc_make_expr if this symbol
3252 is defined as an expression or a plain value. */
3253 if ( S_GET_SEGMENT (sym
) == expr_section
3254 || S_GET_SEGMENT (sym
) == absolute_section
)
3255 return symbol_relc_make_expr (symbol_get_value_expression (sym
));
3257 /* This may be a "fake symbol", referring to ".".
3258 Write out a special null symbol to refer to this position. */
3259 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3260 return xstrdup (".");
3262 /* We hope this is a plain leaf symbol. Construct the encoding
3263 as {S,s}II...:CCCCCCC....
3264 where 'S'/'s' means section symbol / plain symbol
3265 III is decimal for the symbol name length
3266 CCC is the symbol name itself. */
3267 symbol_mark_used_in_reloc (sym
);
3269 sname
= S_GET_NAME (sym
);
3270 sname_len
= strlen (sname
);
3271 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3273 terminal
= XNEWVEC (char, (1 /* S or s */
3274 + 8 /* sname_len in decimal */
3276 + sname_len
/* name itself */
3279 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3283 /* Convert given value to a new complex-relocation symbol name. This
3284 is a non-recursive function, since it is be called for leaf nodes
3285 (plain values) in the expression tree. The caller owns the
3286 returning string, so should free() it eventually. No errors. */
3289 symbol_relc_make_value (offsetT val
)
3291 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3294 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3298 /* Convert given expression to a new complex-relocation symbol name.
3299 This is a recursive function, since it traverses the entire given
3300 expression tree. The caller owns the returning string, so should
3301 free() it eventually. Errors are indicated via as_bad() and a NULL
3305 symbol_relc_make_expr (expressionS
* exp
)
3307 const char * opstr
= NULL
; /* Operator prefix string. */
3308 int arity
= 0; /* Arity of this operator. */
3309 char * operands
[3]; /* Up to three operands. */
3310 char * concat_string
= NULL
;
3312 operands
[0] = operands
[1] = operands
[2] = NULL
;
3314 gas_assert (exp
!= NULL
);
3316 /* Match known operators -> fill in opstr, arity, operands[] and fall
3317 through to construct subexpression fragments; may instead return
3318 string directly for leaf nodes. */
3320 /* See expr.h for the meaning of all these enums. Many operators
3321 have an unnatural arity (X_add_number implicitly added). The
3322 conversion logic expands them to explicit "+" subexpressions. */
3327 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3332 return symbol_relc_make_value (exp
->X_add_number
);
3335 if (exp
->X_add_number
)
3339 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3340 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3344 return symbol_relc_make_sym (exp
->X_add_symbol
);
3346 /* Helper macros for nesting nodes. */
3348 #define HANDLE_XADD_OPT1(str_) \
3349 if (exp->X_add_number) \
3352 opstr = "+:" str_; \
3353 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3354 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3361 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3365 #define HANDLE_XADD_OPT2(str_) \
3366 if (exp->X_add_number) \
3369 opstr = "+:" str_; \
3370 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3371 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3372 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3378 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3379 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3383 /* Nesting nodes. */
3385 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3386 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3387 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3388 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3389 case O_divide
: HANDLE_XADD_OPT2 ("/");
3390 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3391 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3392 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3393 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3394 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3395 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3396 case O_add
: HANDLE_XADD_OPT2 ("+");
3397 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3398 case O_eq
: HANDLE_XADD_OPT2 ("==");
3399 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3400 case O_lt
: HANDLE_XADD_OPT2 ("<");
3401 case O_le
: HANDLE_XADD_OPT2 ("<=");
3402 case O_ge
: HANDLE_XADD_OPT2 (">=");
3403 case O_gt
: HANDLE_XADD_OPT2 (">");
3404 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3405 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3408 /* Validate & reject early. */
3409 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3411 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3413 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3417 concat_string
= NULL
;
3418 else if (arity
== 0)
3419 concat_string
= xstrdup (opstr
);
3420 else if (arity
== 1)
3421 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3422 else if (arity
== 2)
3423 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3426 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3427 operands
[2], (char *) NULL
);
3429 /* Free operand strings (not opstr). */
3430 if (arity
>= 1) xfree (operands
[0]);
3431 if (arity
>= 2) xfree (operands
[1]);
3432 if (arity
>= 3) xfree (operands
[2]);
3434 return concat_string
;