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;
82 /* Whether the symbol has been marked to be removed by a .symver
84 unsigned int removed
: 1;
87 /* A pointer in the symbol may point to either a complete symbol
88 (struct symbol below) or to a local symbol (struct local_symbol
89 defined here). The symbol code can detect the case by examining
90 the first field which is present in both structs.
92 We do this because we ordinarily only need a small amount of
93 information for a local symbol. The symbol table takes up a lot of
94 space, and storing less information for a local symbol can make a
95 big difference in assembler memory usage when assembling a large
100 /* Symbol flags. Only local_symbol and resolved are relevant. */
101 struct symbol_flags flags
;
103 /* Hash value calculated from name. */
106 /* The symbol name. */
109 /* The symbol frag. */
112 /* The symbol section. */
115 /* The value of the symbol. */
119 /* The information we keep for a symbol. The symbol table holds
120 pointers both to this and to local_symbol structures. The first
121 three fields must be identical to struct local_symbol, and the size
122 should be the same as or smaller than struct local_symbol.
123 Fields that don't fit go to an extension structure. */
128 struct symbol_flags flags
;
130 /* Hash value calculated from name. */
133 /* The symbol name. */
136 /* Pointer to the frag this symbol is attached to, if any.
143 /* Extra symbol fields that won't fit. */
147 /* Extra fields to make up a full symbol. */
151 /* The value of the symbol. */
154 /* Forwards and backwards chain pointers. */
156 struct symbol
*previous
;
158 #ifdef OBJ_SYMFIELD_TYPE
159 OBJ_SYMFIELD_TYPE obj
;
162 #ifdef TC_SYMFIELD_TYPE
167 typedef union symbol_entry
169 struct local_symbol lsy
;
173 /* Hash function for a symbol_entry. */
176 hash_symbol_entry (const void *e
)
178 symbol_entry_t
*entry
= (symbol_entry_t
*) e
;
179 if (entry
->sy
.hash
== 0)
180 entry
->sy
.hash
= htab_hash_string (entry
->sy
.name
);
182 return entry
->sy
.hash
;
185 /* Equality function for a symbol_entry. */
188 eq_symbol_entry (const void *a
, const void *b
)
190 const symbol_entry_t
*ea
= (const symbol_entry_t
*) a
;
191 const symbol_entry_t
*eb
= (const symbol_entry_t
*) b
;
193 return (ea
->sy
.hash
== eb
->sy
.hash
194 && strcmp (ea
->sy
.name
, eb
->sy
.name
) == 0);
198 symbol_entry_find (htab_t table
, const char *name
)
200 hashval_t hash
= htab_hash_string (name
);
201 symbol_entry_t needle
= { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
202 hash
, name
, 0, 0, 0 } };
203 return htab_find_with_hash (table
, &needle
, hash
);
207 /* This is non-zero if symbols are case sensitive, which is the
209 int symbols_case_sensitive
= 1;
211 #ifndef WORKING_DOT_WORD
212 extern int new_broken_words
;
215 static htab_t sy_hash
;
217 /* Below are commented in "symbols.h". */
218 symbolS
*symbol_rootP
;
219 symbolS
*symbol_lastP
;
221 struct xsymbol abs_symbol_x
;
223 struct xsymbol dot_symbol_x
;
226 #define debug_verify_symchain verify_symbol_chain
228 #define debug_verify_symchain(root, last) ((void) 0)
231 #define DOLLAR_LABEL_CHAR '\001'
232 #define LOCAL_LABEL_CHAR '\002'
234 #ifndef TC_LABEL_IS_LOCAL
235 #define TC_LABEL_IS_LOCAL(name) 0
238 struct obstack notes
;
240 /* The name of an external symbol which is
241 used to make weak PE symbol names unique. */
242 const char * an_external_name
;
245 static const char *save_symbol_name (const char *);
246 static void fb_label_init (void);
247 static long dollar_label_instance (long);
248 static long fb_label_instance (long);
250 static void print_binary (FILE *, const char *, expressionS
*);
252 /* Return a pointer to a new symbol. Die if we can't make a new
253 symbol. Fill in the symbol's values. Add symbol to end of symbol
256 This function should be called in the general case of creating a
257 symbol. However, if the output file symbol table has already been
258 set, and you are certain that this symbol won't be wanted in the
259 output file, you can call symbol_create. */
262 symbol_new (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
264 symbolS
*symbolP
= symbol_create (name
, segment
, frag
, valu
);
266 /* Link to end of symbol chain. */
267 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
272 /* Save a symbol name on a permanent obstack, and convert it according
273 to the object file format. */
276 save_symbol_name (const char *name
)
281 gas_assert (name
!= NULL
);
282 name_length
= strlen (name
) + 1; /* +1 for \0. */
283 obstack_grow (¬es
, name
, name_length
);
284 ret
= (char *) obstack_finish (¬es
);
286 #ifdef tc_canonicalize_symbol_name
287 ret
= tc_canonicalize_symbol_name (ret
);
290 if (! symbols_case_sensitive
)
294 for (s
= ret
; *s
!= '\0'; s
++)
302 symbol_init (symbolS
*symbolP
, const char *name
, asection
*sec
,
303 fragS
*frag
, valueT valu
)
305 symbolP
->frag
= frag
;
306 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
307 if (symbolP
->bsym
== NULL
)
308 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
309 symbolP
->bsym
->name
= name
;
310 symbolP
->bsym
->section
= sec
;
312 S_SET_VALUE (symbolP
, valu
);
314 symbol_clear_list_pointers (symbolP
);
316 obj_symbol_new_hook (symbolP
);
318 #ifdef tc_symbol_new_hook
319 tc_symbol_new_hook (symbolP
);
323 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
326 symbol_create (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
328 const char *preserved_copy_of_name
;
332 preserved_copy_of_name
= save_symbol_name (name
);
334 size
= sizeof (symbolS
) + sizeof (struct xsymbol
);
335 symbolP
= (symbolS
*) obstack_alloc (¬es
, size
);
337 /* symbol must be born in some fixed state. This seems as good as any. */
338 memset (symbolP
, 0, size
);
339 symbolP
->name
= preserved_copy_of_name
;
340 symbolP
->x
= (struct xsymbol
*) (symbolP
+ 1);
342 symbol_init (symbolP
, preserved_copy_of_name
, segment
, frag
, valu
);
348 /* Local symbol support. If we can get away with it, we keep only a
349 small amount of information for local symbols. */
351 /* Used for statistics. */
353 static unsigned long local_symbol_count
;
354 static unsigned long local_symbol_conversion_count
;
356 /* Create a local symbol and insert it into the local hash table. */
358 struct local_symbol
*
359 local_symbol_make (const char *name
, segT section
, fragS
*frag
, valueT val
)
361 const char *name_copy
;
362 struct local_symbol
*ret
;
363 struct symbol_flags flags
= { .local_symbol
= 1, .resolved
= 0 };
365 ++local_symbol_count
;
367 name_copy
= save_symbol_name (name
);
369 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
372 ret
->name
= name_copy
;
374 ret
->section
= section
;
377 htab_insert (sy_hash
, ret
, 1);
382 /* Convert a local symbol into a real symbol. */
385 local_symbol_convert (void *sym
)
387 symbol_entry_t
*ent
= (symbol_entry_t
*) sym
;
388 struct xsymbol
*xtra
;
391 gas_assert (ent
->lsy
.flags
.local_symbol
);
393 ++local_symbol_conversion_count
;
395 xtra
= (struct xsymbol
*) obstack_alloc (¬es
, sizeof (*xtra
));
396 memset (xtra
, 0, sizeof (*xtra
));
397 val
= ent
->lsy
.value
;
400 /* Local symbols are always either defined or used. */
401 ent
->sy
.flags
.used
= 1;
402 ent
->sy
.flags
.local_symbol
= 0;
404 symbol_init (&ent
->sy
, ent
->lsy
.name
, ent
->lsy
.section
, ent
->lsy
.frag
, val
);
405 symbol_append (&ent
->sy
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
411 define_sym_at_dot (symbolS
*symbolP
)
413 symbolP
->frag
= frag_now
;
414 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
415 S_SET_SEGMENT (symbolP
, now_seg
);
418 /* We have just seen "<name>:".
419 Creates a struct symbol unless it already exists.
421 Gripes if we are redefining a symbol incompatibly (and ignores it). */
424 colon (/* Just seen "x:" - rattle symbols & frags. */
425 const char *sym_name
/* Symbol name, as a canonical string. */
426 /* We copy this string: OK to alter later. */)
428 symbolS
*symbolP
; /* Symbol we are working with. */
430 /* Sun local labels go out of scope whenever a non-local symbol is
432 if (LOCAL_LABELS_DOLLAR
433 && !bfd_is_local_label_name (stdoutput
, sym_name
))
434 dollar_label_clear ();
436 #ifndef WORKING_DOT_WORD
437 if (new_broken_words
)
439 struct broken_word
*a
;
444 if (now_seg
== absolute_section
)
446 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
450 possible_bytes
= (md_short_jump_size
451 + new_broken_words
* md_long_jump_size
);
454 frag_opcode
= frag_var (rs_broken_word
,
458 (symbolS
*) broken_words
,
462 /* We want to store the pointer to where to insert the jump
463 table in the fr_opcode of the rs_broken_word frag. This
464 requires a little hackery. */
466 && (frag_tmp
->fr_type
!= rs_broken_word
467 || frag_tmp
->fr_opcode
))
468 frag_tmp
= frag_tmp
->fr_next
;
470 frag_tmp
->fr_opcode
= frag_opcode
;
471 new_broken_words
= 0;
473 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
474 a
->dispfrag
= frag_tmp
;
476 #endif /* WORKING_DOT_WORD */
478 #ifdef obj_frob_colon
479 obj_frob_colon (sym_name
);
482 if ((symbolP
= symbol_find (sym_name
)) != 0)
484 S_CLEAR_WEAKREFR (symbolP
);
485 #ifdef RESOLVE_SYMBOL_REDEFINITION
486 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
489 /* Now check for undefined symbols. */
490 if (symbolP
->flags
.local_symbol
)
492 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
494 if (locsym
->section
!= undefined_section
495 && (locsym
->frag
!= frag_now
496 || locsym
->section
!= now_seg
497 || locsym
->value
!= frag_now_fix ()))
499 as_bad (_("symbol `%s' is already defined"), sym_name
);
503 locsym
->section
= now_seg
;
504 locsym
->frag
= frag_now
;
505 locsym
->value
= frag_now_fix ();
507 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
508 || S_IS_COMMON (symbolP
)
509 || S_IS_VOLATILE (symbolP
))
511 if (S_IS_VOLATILE (symbolP
))
513 symbolP
= symbol_clone (symbolP
, 1);
514 S_SET_VALUE (symbolP
, 0);
515 S_CLEAR_VOLATILE (symbolP
);
517 if (S_GET_VALUE (symbolP
) == 0)
519 define_sym_at_dot (symbolP
);
522 #endif /* if we have one, it better be zero. */
527 /* There are still several cases to check:
529 A .comm/.lcomm symbol being redefined as initialized
532 A .comm/.lcomm symbol being redefined with a larger
535 This only used to be allowed on VMS gas, but Sun cc
536 on the sparc also depends on it. */
538 if (((!S_IS_DEBUG (symbolP
)
539 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
540 && S_IS_EXTERNAL (symbolP
))
541 || S_GET_SEGMENT (symbolP
) == bss_section
)
542 && (now_seg
== data_section
543 || now_seg
== bss_section
544 || now_seg
== S_GET_SEGMENT (symbolP
)))
546 /* Select which of the 2 cases this is. */
547 if (now_seg
!= data_section
)
549 /* New .comm for prev .comm symbol.
551 If the new size is larger we just change its
552 value. If the new size is smaller, we ignore
554 if (S_GET_VALUE (symbolP
)
555 < ((unsigned) frag_now_fix ()))
557 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
562 /* It is a .comm/.lcomm being converted to initialized
564 define_sym_at_dot (symbolP
);
569 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
570 static const char *od_buf
= "";
574 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
575 sprintf (od_buf
, "%d.%d.",
576 S_GET_OTHER (symbolP
),
577 S_GET_DESC (symbolP
));
579 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
581 segment_name (S_GET_SEGMENT (symbolP
)),
583 (long) S_GET_VALUE (symbolP
));
585 } /* if the undefined symbol has no value */
589 /* Don't blow up if the definition is the same. */
590 if (!(frag_now
== symbolP
->frag
591 && S_GET_VALUE (symbolP
) == frag_now_fix ()
592 && S_GET_SEGMENT (symbolP
) == now_seg
))
594 as_bad (_("symbol `%s' is already defined"), sym_name
);
595 symbolP
= symbol_clone (symbolP
, 0);
596 define_sym_at_dot (symbolP
);
601 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
603 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
, frag_now
,
608 symbolP
= symbol_new (sym_name
, now_seg
, frag_now
, frag_now_fix ());
610 symbol_table_insert (symbolP
);
613 if (mri_common_symbol
!= NULL
)
615 /* This symbol is actually being defined within an MRI common
616 section. This requires special handling. */
617 if (symbolP
->flags
.local_symbol
)
618 symbolP
= local_symbol_convert (symbolP
);
619 symbolP
->x
->value
.X_op
= O_symbol
;
620 symbolP
->x
->value
.X_add_symbol
= mri_common_symbol
;
621 symbolP
->x
->value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
622 symbolP
->frag
= &zero_address_frag
;
623 S_SET_SEGMENT (symbolP
, expr_section
);
624 symbolP
->flags
.mri_common
= 1;
628 tc_frob_label (symbolP
);
630 #ifdef obj_frob_label
631 obj_frob_label (symbolP
);
637 /* Die if we can't insert the symbol. */
640 symbol_table_insert (symbolS
*symbolP
)
644 htab_insert (sy_hash
, symbolP
, 1);
647 /* If a symbol name does not exist, create it as undefined, and insert
648 it into the symbol table. Return a pointer to it. */
651 symbol_find_or_make (const char *name
)
655 symbolP
= symbol_find (name
);
659 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
661 symbolP
= md_undefined_symbol ((char *) name
);
665 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
666 &zero_address_frag
, 0);
670 symbolP
= symbol_make (name
);
672 symbol_table_insert (symbolP
);
673 } /* if symbol wasn't found */
679 symbol_make (const char *name
)
683 /* Let the machine description default it, e.g. for register names. */
684 symbolP
= md_undefined_symbol ((char *) name
);
687 symbolP
= symbol_new (name
, undefined_section
, &zero_address_frag
, 0);
693 symbol_clone (symbolS
*orgsymP
, int replace
)
696 asymbol
*bsymorg
, *bsymnew
;
698 /* Make sure we never clone the dot special symbol. */
699 gas_assert (orgsymP
!= &dot_symbol
);
701 /* When cloning a local symbol it isn't absolutely necessary to
702 convert the original, but converting makes the code much
703 simpler to cover this unexpected case. As of 2020-08-21
704 symbol_clone won't be called on a local symbol. */
705 if (orgsymP
->flags
.local_symbol
)
706 orgsymP
= local_symbol_convert (orgsymP
);
707 bsymorg
= orgsymP
->bsym
;
709 newsymP
= (symbolS
*) obstack_alloc (¬es
, (sizeof (symbolS
)
710 + sizeof (struct xsymbol
)));
712 newsymP
->x
= (struct xsymbol
*) (newsymP
+ 1);
713 *newsymP
->x
= *orgsymP
->x
;
714 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
716 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
717 newsymP
->bsym
= bsymnew
;
718 bsymnew
->name
= bsymorg
->name
;
719 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
720 bsymnew
->section
= bsymorg
->section
;
721 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
722 bfd_asymbol_bfd (bsymnew
), bsymnew
);
724 #ifdef obj_symbol_clone_hook
725 obj_symbol_clone_hook (newsymP
, orgsymP
);
728 #ifdef tc_symbol_clone_hook
729 tc_symbol_clone_hook (newsymP
, orgsymP
);
734 if (symbol_rootP
== orgsymP
)
735 symbol_rootP
= newsymP
;
736 else if (orgsymP
->x
->previous
)
738 orgsymP
->x
->previous
->x
->next
= newsymP
;
739 orgsymP
->x
->previous
= NULL
;
741 if (symbol_lastP
== orgsymP
)
742 symbol_lastP
= newsymP
;
743 else if (orgsymP
->x
->next
)
744 orgsymP
->x
->next
->x
->previous
= newsymP
;
746 /* Symbols that won't be output can't be external. */
747 S_CLEAR_EXTERNAL (orgsymP
);
748 orgsymP
->x
->previous
= orgsymP
->x
->next
= orgsymP
;
749 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
751 symbol_table_insert (newsymP
);
755 /* Symbols that won't be output can't be external. */
756 S_CLEAR_EXTERNAL (newsymP
);
757 newsymP
->x
->previous
= newsymP
->x
->next
= newsymP
;
763 /* Referenced symbols, if they are forward references, need to be cloned
764 (without replacing the original) so that the value of the referenced
765 symbols at the point of use is saved by the clone. */
767 #undef symbol_clone_if_forward_ref
769 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
771 if (symbolP
&& !symbolP
->flags
.local_symbol
)
773 symbolS
*orig_add_symbol
= symbolP
->x
->value
.X_add_symbol
;
774 symbolS
*orig_op_symbol
= symbolP
->x
->value
.X_op_symbol
;
775 symbolS
*add_symbol
= orig_add_symbol
;
776 symbolS
*op_symbol
= orig_op_symbol
;
778 if (symbolP
->flags
.forward_ref
)
783 /* assign_symbol() clones volatile symbols; pre-existing expressions
784 hold references to the original instance, but want the current
785 value. Just repeat the lookup. */
786 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
787 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
788 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
789 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
792 /* Re-using resolving here, as this routine cannot get called from
793 symbol resolution code. */
794 if ((symbolP
->bsym
->section
== expr_section
795 || symbolP
->flags
.forward_ref
)
796 && !symbolP
->flags
.resolving
)
798 symbolP
->flags
.resolving
= 1;
799 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
800 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
801 symbolP
->flags
.resolving
= 0;
804 if (symbolP
->flags
.forward_ref
805 || add_symbol
!= orig_add_symbol
806 || op_symbol
!= orig_op_symbol
)
808 if (symbolP
!= &dot_symbol
)
810 symbolP
= symbol_clone (symbolP
, 0);
811 symbolP
->flags
.resolving
= 0;
815 symbolP
= symbol_temp_new_now ();
816 #ifdef tc_new_dot_label
817 tc_new_dot_label (symbolP
);
822 symbolP
->x
->value
.X_add_symbol
= add_symbol
;
823 symbolP
->x
->value
.X_op_symbol
= op_symbol
;
830 symbol_temp_new (segT seg
, fragS
*frag
, valueT ofs
)
832 return symbol_new (FAKE_LABEL_NAME
, seg
, frag
, ofs
);
836 symbol_temp_new_now (void)
838 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix ());
842 symbol_temp_new_now_octets (void)
844 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix_octets ());
848 symbol_temp_make (void)
850 return symbol_make (FAKE_LABEL_NAME
);
853 /* Implement symbol table lookup.
854 In: A symbol's name as a string: '\0' can't be part of a symbol name.
855 Out: NULL if the name was not in the symbol table, else the address
856 of a struct symbol associated with that name. */
859 symbol_find_exact (const char *name
)
861 return symbol_find_exact_noref (name
, 0);
865 symbol_find_exact_noref (const char *name
, int noref
)
867 symbolS
*sym
= symbol_entry_find (sy_hash
, name
);
869 /* Any references to the symbol, except for the reference in
870 .weakref, must clear this flag, such that the symbol does not
871 turn into a weak symbol. Note that we don't have to handle the
872 local_symbol case, since a weakrefd is always promoted out of the
873 local_symbol table when it is turned into a weak symbol. */
875 S_CLEAR_WEAKREFD (sym
);
881 symbol_find (const char *name
)
883 return symbol_find_noref (name
, 0);
887 symbol_find_noref (const char *name
, int noref
)
892 #ifdef tc_canonicalize_symbol_name
894 copy
= xstrdup (name
);
895 name
= tc_canonicalize_symbol_name (copy
);
899 if (! symbols_case_sensitive
)
908 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
910 while ((c
= *orig
++) != '\0')
911 *copy
++ = TOUPPER (c
);
915 copy
= (char *) name
;
918 result
= symbol_find_exact_noref (name
, noref
);
923 /* Once upon a time, symbols were kept in a singly linked list. At
924 least coff needs to be able to rearrange them from time to time, for
925 which a doubly linked list is much more convenient. Loic did these
926 as macros which seemed dangerous to me so they're now functions.
929 /* Link symbol ADDME after symbol TARGET in the chain. */
932 symbol_append (symbolS
*addme
, symbolS
*target
,
933 symbolS
**rootPP
, symbolS
**lastPP
)
935 extern int symbol_table_frozen
;
936 if (symbol_table_frozen
)
938 if (addme
->flags
.local_symbol
)
940 if (target
!= NULL
&& target
->flags
.local_symbol
)
945 know (*rootPP
== NULL
);
946 know (*lastPP
== NULL
);
947 addme
->x
->next
= NULL
;
948 addme
->x
->previous
= NULL
;
952 } /* if the list is empty */
954 if (target
->x
->next
!= NULL
)
956 target
->x
->next
->x
->previous
= addme
;
960 know (*lastPP
== target
);
962 } /* if we have a next */
964 addme
->x
->next
= target
->x
->next
;
965 target
->x
->next
= addme
;
966 addme
->x
->previous
= target
;
968 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
971 /* Set the chain pointers of SYMBOL to null. */
974 symbol_clear_list_pointers (symbolS
*symbolP
)
976 if (symbolP
->flags
.local_symbol
)
978 symbolP
->x
->next
= NULL
;
979 symbolP
->x
->previous
= NULL
;
982 /* Remove SYMBOLP from the list. */
985 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
987 if (symbolP
->flags
.local_symbol
)
990 if (symbolP
== *rootPP
)
992 *rootPP
= symbolP
->x
->next
;
993 } /* if it was the root */
995 if (symbolP
== *lastPP
)
997 *lastPP
= symbolP
->x
->previous
;
998 } /* if it was the tail */
1000 if (symbolP
->x
->next
!= NULL
)
1002 symbolP
->x
->next
->x
->previous
= symbolP
->x
->previous
;
1005 if (symbolP
->x
->previous
!= NULL
)
1007 symbolP
->x
->previous
->x
->next
= symbolP
->x
->next
;
1008 } /* if not first */
1010 debug_verify_symchain (*rootPP
, *lastPP
);
1013 /* Link symbol ADDME before symbol TARGET in the chain. */
1016 symbol_insert (symbolS
*addme
, symbolS
*target
,
1017 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
1019 extern int symbol_table_frozen
;
1020 if (symbol_table_frozen
)
1022 if (addme
->flags
.local_symbol
)
1024 if (target
->flags
.local_symbol
)
1027 if (target
->x
->previous
!= NULL
)
1029 target
->x
->previous
->x
->next
= addme
;
1033 know (*rootPP
== target
);
1035 } /* if not first */
1037 addme
->x
->previous
= target
->x
->previous
;
1038 target
->x
->previous
= addme
;
1039 addme
->x
->next
= target
;
1041 debug_verify_symchain (*rootPP
, *lastPP
);
1045 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
1047 symbolS
*symbolP
= rootP
;
1049 if (symbolP
== NULL
)
1052 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
1054 gas_assert (symbolP
->bsym
!= NULL
);
1055 gas_assert (symbolP
->flags
.local_symbol
== 0);
1056 gas_assert (symbolP
->x
->next
->x
->previous
== symbolP
);
1059 gas_assert (lastP
== symbolP
);
1063 symbol_on_chain (symbolS
*s
, symbolS
*rootPP
, symbolS
*lastPP
)
1065 return (!s
->flags
.local_symbol
1066 && ((s
->x
->next
!= s
1067 && s
->x
->next
!= NULL
1068 && s
->x
->next
->x
->previous
== s
)
1070 && ((s
->x
->previous
!= s
1071 && s
->x
->previous
!= NULL
1072 && s
->x
->previous
->x
->next
== s
)
1076 #ifdef OBJ_COMPLEX_RELC
1079 use_complex_relocs_for (symbolS
* symp
)
1081 switch (symp
->x
->value
.X_op
)
1091 case O_bit_inclusive_or
:
1093 case O_bit_exclusive_or
:
1105 if ((S_IS_COMMON (symp
->x
->value
.X_op_symbol
)
1106 || S_IS_LOCAL (symp
->x
->value
.X_op_symbol
))
1107 && S_IS_DEFINED (symp
->x
->value
.X_op_symbol
)
1108 && S_GET_SEGMENT (symp
->x
->value
.X_op_symbol
) != expr_section
)
1115 if ((S_IS_COMMON (symp
->x
->value
.X_add_symbol
)
1116 || S_IS_LOCAL (symp
->x
->value
.X_add_symbol
))
1117 && S_IS_DEFINED (symp
->x
->value
.X_add_symbol
)
1118 && S_GET_SEGMENT (symp
->x
->value
.X_add_symbol
) != expr_section
)
1131 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
1135 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1136 segT seg_right
= S_GET_SEGMENT (right
);
1145 case O_uminus
: opname
= "-"; break;
1146 case O_bit_not
: opname
= "~"; break;
1147 case O_logical_not
: opname
= "!"; break;
1148 case O_multiply
: opname
= "*"; break;
1149 case O_divide
: opname
= "/"; break;
1150 case O_modulus
: opname
= "%"; break;
1151 case O_left_shift
: opname
= "<<"; break;
1152 case O_right_shift
: opname
= ">>"; break;
1153 case O_bit_inclusive_or
: opname
= "|"; break;
1154 case O_bit_or_not
: opname
= "|~"; break;
1155 case O_bit_exclusive_or
: opname
= "^"; break;
1156 case O_bit_and
: opname
= "&"; break;
1157 case O_add
: opname
= "+"; break;
1158 case O_subtract
: opname
= "-"; break;
1159 case O_eq
: opname
= "=="; break;
1160 case O_ne
: opname
= "!="; break;
1161 case O_lt
: opname
= "<"; break;
1162 case O_le
: opname
= "<="; break;
1163 case O_ge
: opname
= ">="; break;
1164 case O_gt
: opname
= ">"; break;
1165 case O_logical_and
: opname
= "&&"; break;
1166 case O_logical_or
: opname
= "||"; break;
1169 if (expr_symbol_where (symp
, &file
, &line
))
1172 as_bad_where (file
, line
,
1173 _("invalid operands (%s and %s sections) for `%s'"),
1174 seg_left
->name
, seg_right
->name
, opname
);
1176 as_bad_where (file
, line
,
1177 _("invalid operand (%s section) for `%s'"),
1178 seg_right
->name
, opname
);
1182 const char *sname
= S_GET_NAME (symp
);
1185 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1186 seg_left
->name
, seg_right
->name
, opname
, sname
);
1188 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1189 seg_right
->name
, opname
, sname
);
1193 /* Resolve the value of a symbol. This is called during the final
1194 pass over the symbol table to resolve any symbols with complex
1198 resolve_symbol_value (symbolS
*symp
)
1204 if (symp
->flags
.local_symbol
)
1206 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1208 final_val
= locsym
->value
;
1209 if (locsym
->flags
.resolved
)
1212 /* Symbols whose section has SEC_ELF_OCTETS set,
1213 resolve to octets instead of target bytes. */
1214 if (locsym
->section
->flags
& SEC_OCTETS
)
1215 final_val
+= locsym
->frag
->fr_address
;
1217 final_val
+= locsym
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1221 locsym
->value
= final_val
;
1222 locsym
->flags
.resolved
= 1;
1228 if (symp
->flags
.resolved
)
1231 while (symp
->x
->value
.X_op
== O_symbol
)
1233 final_val
+= symp
->x
->value
.X_add_number
;
1234 symp
= symp
->x
->value
.X_add_symbol
;
1235 if (symp
->flags
.local_symbol
)
1237 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1238 final_val
+= locsym
->value
;
1241 if (!symp
->flags
.resolved
)
1244 if (symp
->x
->value
.X_op
== O_constant
)
1245 final_val
+= symp
->x
->value
.X_add_number
;
1252 final_seg
= S_GET_SEGMENT (symp
);
1254 if (symp
->flags
.resolving
)
1257 as_bad (_("symbol definition loop encountered at `%s'"),
1262 #ifdef OBJ_COMPLEX_RELC
1263 else if (final_seg
== expr_section
1264 && use_complex_relocs_for (symp
))
1266 symbolS
* relc_symbol
= NULL
;
1267 char * relc_symbol_name
= NULL
;
1269 relc_symbol_name
= symbol_relc_make_expr (& symp
->x
->value
);
1271 /* For debugging, print out conversion input & output. */
1273 print_expr (& symp
->x
->value
);
1274 if (relc_symbol_name
)
1275 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1278 if (relc_symbol_name
!= NULL
)
1279 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1280 &zero_address_frag
, 0);
1282 if (relc_symbol
== NULL
)
1284 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1290 symbol_table_insert (relc_symbol
);
1292 /* S_CLEAR_EXTERNAL (relc_symbol); */
1293 if (symp
->bsym
->flags
& BSF_SRELC
)
1294 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1296 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1297 /* symp->bsym->flags |= BSF_RELC; */
1298 copy_symbol_attributes (symp
, relc_symbol
);
1299 symp
->x
->value
.X_op
= O_symbol
;
1300 symp
->x
->value
.X_add_symbol
= relc_symbol
;
1301 symp
->x
->value
.X_add_number
= 0;
1306 final_seg
= undefined_section
;
1307 goto exit_dont_set_value
;
1312 symbolS
*add_symbol
, *op_symbol
;
1313 offsetT left
, right
;
1314 segT seg_left
, seg_right
;
1318 symp
->flags
.resolving
= 1;
1320 /* Help out with CSE. */
1321 add_symbol
= symp
->x
->value
.X_add_symbol
;
1322 op_symbol
= symp
->x
->value
.X_op_symbol
;
1323 final_val
= symp
->x
->value
.X_add_number
;
1324 op
= symp
->x
->value
.X_op
;
1337 /* Symbols whose section has SEC_ELF_OCTETS set,
1338 resolve to octets instead of target bytes. */
1339 if (symp
->bsym
->section
->flags
& SEC_OCTETS
)
1340 final_val
+= symp
->frag
->fr_address
;
1342 final_val
+= symp
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1343 if (final_seg
== expr_section
)
1344 final_seg
= absolute_section
;
1353 left
= resolve_symbol_value (add_symbol
);
1354 seg_left
= S_GET_SEGMENT (add_symbol
);
1356 symp
->x
->value
.X_op_symbol
= NULL
;
1359 if (S_IS_WEAKREFR (symp
))
1361 gas_assert (final_val
== 0);
1362 if (S_IS_WEAKREFR (add_symbol
))
1364 gas_assert (add_symbol
->x
->value
.X_op
== O_symbol
1365 && add_symbol
->x
->value
.X_add_number
== 0);
1366 add_symbol
= add_symbol
->x
->value
.X_add_symbol
;
1367 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1368 symp
->x
->value
.X_add_symbol
= add_symbol
;
1372 if (symp
->flags
.mri_common
)
1374 /* This is a symbol inside an MRI common section. The
1375 relocation routines are going to handle it specially.
1376 Don't change the value. */
1377 resolved
= symbol_resolved_p (add_symbol
);
1381 /* Don't leave symbol loops. */
1383 && !add_symbol
->flags
.local_symbol
1384 && add_symbol
->flags
.resolving
)
1387 if (finalize_syms
&& final_val
== 0
1389 /* Avoid changing symp's "within" when dealing with
1390 AIX debug symbols. For some storage classes, "within"
1391 have a special meaning.
1392 C_DWARF should behave like on Linux, thus this check
1393 isn't done to be closer. */
1394 && ((symbol_get_bfdsym (symp
)->flags
& BSF_DEBUGGING
) == 0
1395 || (S_GET_STORAGE_CLASS (symp
) == C_DWARF
))
1399 if (add_symbol
->flags
.local_symbol
)
1400 add_symbol
= local_symbol_convert (add_symbol
);
1401 copy_symbol_attributes (symp
, add_symbol
);
1404 /* If we have equated this symbol to an undefined or common
1405 symbol, keep X_op set to O_symbol, and don't change
1406 X_add_number. This permits the routine which writes out
1407 relocation to detect this case, and convert the
1408 relocation to be against the symbol to which this symbol
1410 if (seg_left
== undefined_section
1411 || bfd_is_com_section (seg_left
)
1412 #if defined (OBJ_COFF) && defined (TE_PE)
1413 || S_IS_WEAK (add_symbol
)
1416 && ((final_seg
== expr_section
1417 && seg_left
!= expr_section
1418 && seg_left
!= absolute_section
)
1419 || symbol_shadow_p (symp
))))
1423 symp
->x
->value
.X_op
= O_symbol
;
1424 symp
->x
->value
.X_add_symbol
= add_symbol
;
1425 symp
->x
->value
.X_add_number
= final_val
;
1426 /* Use X_op_symbol as a flag. */
1427 symp
->x
->value
.X_op_symbol
= add_symbol
;
1429 final_seg
= seg_left
;
1430 final_val
+= symp
->frag
->fr_address
+ left
;
1431 resolved
= symbol_resolved_p (add_symbol
);
1432 symp
->flags
.resolving
= 0;
1433 goto exit_dont_set_value
;
1437 final_val
+= symp
->frag
->fr_address
+ left
;
1438 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1439 final_seg
= seg_left
;
1442 resolved
= symbol_resolved_p (add_symbol
);
1443 if (S_IS_WEAKREFR (symp
))
1445 symp
->flags
.resolving
= 0;
1446 goto exit_dont_set_value
;
1453 left
= resolve_symbol_value (add_symbol
);
1454 seg_left
= S_GET_SEGMENT (add_symbol
);
1456 /* By reducing these to the relevant dyadic operator, we get
1457 !S -> S == 0 permitted on anything,
1458 -S -> 0 - S only permitted on absolute
1459 ~S -> S ^ ~0 only permitted on absolute */
1460 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1462 report_op_error (symp
, NULL
, op
, add_symbol
);
1464 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1465 final_seg
= absolute_section
;
1469 else if (op
== O_logical_not
)
1474 final_val
+= left
+ symp
->frag
->fr_address
;
1476 resolved
= symbol_resolved_p (add_symbol
);
1484 case O_bit_inclusive_or
:
1486 case O_bit_exclusive_or
:
1498 left
= resolve_symbol_value (add_symbol
);
1499 right
= resolve_symbol_value (op_symbol
);
1500 seg_left
= S_GET_SEGMENT (add_symbol
);
1501 seg_right
= S_GET_SEGMENT (op_symbol
);
1503 /* Simplify addition or subtraction of a constant by folding the
1504 constant into X_add_number. */
1507 if (seg_right
== absolute_section
)
1512 else if (seg_left
== absolute_section
)
1515 add_symbol
= op_symbol
;
1517 seg_left
= seg_right
;
1521 else if (op
== O_subtract
)
1523 if (seg_right
== absolute_section
)
1531 /* Equality and non-equality tests are permitted on anything.
1532 Subtraction, and other comparison operators are permitted if
1533 both operands are in the same section. Otherwise, both
1534 operands must be absolute. We already handled the case of
1535 addition or subtraction of a constant above. This will
1536 probably need to be changed for an object file format which
1537 supports arbitrary expressions. */
1538 if (!(seg_left
== absolute_section
1539 && seg_right
== absolute_section
)
1540 && !(op
== O_eq
|| op
== O_ne
)
1541 && !((op
== O_subtract
1542 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1543 && seg_left
== seg_right
1544 && (seg_left
!= undefined_section
1545 || add_symbol
== op_symbol
)))
1547 /* Don't emit messages unless we're finalizing the symbol value,
1548 otherwise we may get the same message multiple times. */
1550 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1551 /* However do not move the symbol into the absolute section
1552 if it cannot currently be resolved - this would confuse
1553 other parts of the assembler into believing that the
1554 expression had been evaluated to zero. */
1560 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1561 final_seg
= absolute_section
;
1563 /* Check for division by zero. */
1564 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1566 /* If seg_right is not absolute_section, then we've
1567 already issued a warning about using a bad symbol. */
1568 if (seg_right
== absolute_section
&& finalize_syms
)
1573 if (expr_symbol_where (symp
, &file
, &line
))
1574 as_bad_where (file
, line
, _("division by zero"));
1576 as_bad (_("division by zero when setting `%s'"),
1582 if ((op
== O_left_shift
|| op
== O_right_shift
)
1583 && (valueT
) right
>= sizeof (valueT
) * CHAR_BIT
)
1585 as_warn_value_out_of_range (_("shift count"), right
, 0,
1586 sizeof (valueT
) * CHAR_BIT
- 1,
1591 switch (symp
->x
->value
.X_op
)
1593 case O_multiply
: left
*= right
; break;
1594 case O_divide
: left
/= right
; break;
1595 case O_modulus
: left
%= right
; break;
1597 left
= (valueT
) left
<< (valueT
) right
; break;
1599 left
= (valueT
) left
>> (valueT
) right
; break;
1600 case O_bit_inclusive_or
: left
|= right
; break;
1601 case O_bit_or_not
: left
|= ~right
; break;
1602 case O_bit_exclusive_or
: left
^= right
; break;
1603 case O_bit_and
: left
&= right
; break;
1604 case O_add
: left
+= right
; break;
1605 case O_subtract
: left
-= right
; break;
1608 left
= (left
== right
&& seg_left
== seg_right
1609 && (seg_left
!= undefined_section
1610 || add_symbol
== op_symbol
)
1611 ? ~ (offsetT
) 0 : 0);
1612 if (symp
->x
->value
.X_op
== O_ne
)
1615 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1616 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1617 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1618 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1619 case O_logical_and
: left
= left
&& right
; break;
1620 case O_logical_or
: left
= left
|| right
; break;
1625 /* See PR 20895 for a reproducer. */
1626 as_bad (_("Invalid operation on symbol"));
1627 goto exit_dont_set_value
;
1633 final_val
+= symp
->frag
->fr_address
+ left
;
1634 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1636 if (seg_left
== undefined_section
1637 || seg_right
== undefined_section
)
1638 final_seg
= undefined_section
;
1639 else if (seg_left
== absolute_section
)
1640 final_seg
= seg_right
;
1642 final_seg
= seg_left
;
1644 resolved
= (symbol_resolved_p (add_symbol
)
1645 && symbol_resolved_p (op_symbol
));
1650 /* Give an error (below) if not in expr_section. We don't
1651 want to worry about expr_section symbols, because they
1652 are fictional (they are created as part of expression
1653 resolution), and any problems may not actually mean
1658 symp
->flags
.resolving
= 0;
1662 S_SET_VALUE (symp
, final_val
);
1664 exit_dont_set_value
:
1665 /* Always set the segment, even if not finalizing the value.
1666 The segment is used to determine whether a symbol is defined. */
1667 S_SET_SEGMENT (symp
, final_seg
);
1669 /* Don't worry if we can't resolve an expr_section symbol. */
1673 symp
->flags
.resolved
= 1;
1674 else if (S_GET_SEGMENT (symp
) != expr_section
)
1676 as_bad (_("can't resolve value for symbol `%s'"),
1678 symp
->flags
.resolved
= 1;
1685 /* A static function passed to hash_traverse. */
1688 resolve_local_symbol (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1690 symbol_entry_t
*entry
= *((symbol_entry_t
**) slot
);
1691 if (entry
->sy
.flags
.local_symbol
)
1692 resolve_symbol_value (&entry
->sy
);
1697 /* Resolve all local symbols. */
1700 resolve_local_symbol_values (void)
1702 htab_traverse (sy_hash
, resolve_local_symbol
, NULL
);
1705 /* Obtain the current value of a symbol without changing any
1706 sub-expressions used. */
1709 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1711 symbolS
*symbolP
= *symbolPP
;
1713 if (symbolP
->flags
.local_symbol
)
1715 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1717 *valueP
= locsym
->value
;
1718 *segP
= locsym
->section
;
1719 *fragPP
= locsym
->frag
;
1723 expressionS exp
= symbolP
->x
->value
;
1725 if (!symbolP
->flags
.resolved
&& exp
.X_op
!= O_illegal
)
1729 if (symbolP
->flags
.resolving
)
1731 symbolP
->flags
.resolving
= 1;
1732 resolved
= resolve_expression (&exp
);
1733 symbolP
->flags
.resolving
= 0;
1741 if (!symbol_equated_p (symbolP
))
1746 symbolP
= exp
.X_add_symbol
;
1753 *symbolPP
= symbolP
;
1755 /* A bogus input file can result in resolve_expression()
1756 generating a local symbol, so we have to check again. */
1757 if (symbolP
->flags
.local_symbol
)
1759 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1761 *valueP
= locsym
->value
;
1762 *segP
= locsym
->section
;
1763 *fragPP
= locsym
->frag
;
1767 *valueP
= exp
.X_add_number
;
1768 *segP
= symbolP
->bsym
->section
;
1769 *fragPP
= symbolP
->frag
;
1772 if (*segP
== expr_section
)
1775 case O_constant
: *segP
= absolute_section
; break;
1776 case O_register
: *segP
= reg_section
; break;
1784 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1785 They are *really* local. That is, they go out of scope whenever we see a
1786 label that isn't local. Also, like fb labels, there can be multiple
1787 instances of a dollar label. Therefor, we name encode each instance with
1788 the instance number, keep a list of defined symbols separate from the real
1789 symbol table, and we treat these buggers as a sparse array. */
1791 static long *dollar_labels
;
1792 static long *dollar_label_instances
;
1793 static char *dollar_label_defines
;
1794 static size_t dollar_label_count
;
1795 static size_t dollar_label_max
;
1798 dollar_label_defined (long label
)
1802 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1804 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1806 return dollar_label_defines
[i
- dollar_labels
];
1808 /* If we get here, label isn't defined. */
1813 dollar_label_instance (long label
)
1817 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1819 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1821 return (dollar_label_instances
[i
- dollar_labels
]);
1823 /* If we get here, we haven't seen the label before.
1824 Therefore its instance count is zero. */
1829 dollar_label_clear (void)
1831 if (dollar_label_count
)
1832 memset (dollar_label_defines
, '\0', dollar_label_count
);
1835 #define DOLLAR_LABEL_BUMP_BY 10
1838 define_dollar_label (long label
)
1842 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1845 ++dollar_label_instances
[i
- dollar_labels
];
1846 dollar_label_defines
[i
- dollar_labels
] = 1;
1850 /* If we get to here, we don't have label listed yet. */
1852 if (dollar_labels
== NULL
)
1854 dollar_labels
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1855 dollar_label_instances
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1856 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1857 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1858 dollar_label_count
= 0;
1860 else if (dollar_label_count
== dollar_label_max
)
1862 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1863 dollar_labels
= XRESIZEVEC (long, dollar_labels
, dollar_label_max
);
1864 dollar_label_instances
= XRESIZEVEC (long, dollar_label_instances
,
1866 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1868 } /* if we needed to grow */
1870 dollar_labels
[dollar_label_count
] = label
;
1871 dollar_label_instances
[dollar_label_count
] = 1;
1872 dollar_label_defines
[dollar_label_count
] = 1;
1873 ++dollar_label_count
;
1876 /* Caller must copy returned name: we re-use the area for the next name.
1878 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1879 where n is the label number and m is the instance number. "L" makes
1880 it a label discarded unless debugging and "^A"('\1') ensures no
1881 ordinary symbol SHOULD get the same name as a local label
1882 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1884 fb labels get the same treatment, except that ^B is used in place
1887 char * /* Return local label name. */
1888 dollar_label_name (long n
, /* we just saw "n$:" : n a number. */
1889 int augend
/* 0 for current instance, 1 for new instance. */)
1892 /* Returned to caller, then copied. Used for created names ("4f"). */
1893 static char symbol_name_build
[24];
1896 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1899 know (augend
== 0 || augend
== 1);
1900 p
= symbol_name_build
;
1901 #ifdef LOCAL_LABEL_PREFIX
1902 *p
++ = LOCAL_LABEL_PREFIX
;
1906 /* Next code just does sprintf( {}, "%d", n); */
1908 q
= symbol_name_temporary
;
1909 for (*q
++ = 0, i
= n
; i
; ++q
)
1914 while ((*p
= *--q
) != '\0')
1917 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1919 /* Instance number. */
1920 q
= symbol_name_temporary
;
1921 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1926 while ((*p
++ = *--q
) != '\0');
1928 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1929 return symbol_name_build
;
1932 /* Somebody else's idea of local labels. They are made by "n:" where n
1933 is any decimal digit. Refer to them with
1934 "nb" for previous (backward) n:
1935 or "nf" for next (forward) n:.
1937 We do a little better and let n be any number, not just a single digit, but
1938 since the other guy's assembler only does ten, we treat the first ten
1941 Like someone else's assembler, we have one set of local label counters for
1942 entire assembly, not one set per (sub)segment like in most assemblers. This
1943 implies that one can refer to a label in another segment, and indeed some
1944 crufty compilers have done just that.
1946 Since there could be a LOT of these things, treat them as a sparse
1949 #define FB_LABEL_SPECIAL (10)
1951 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1952 static long *fb_labels
;
1953 static long *fb_label_instances
;
1954 static long fb_label_count
;
1955 static long fb_label_max
;
1957 /* This must be more than FB_LABEL_SPECIAL. */
1958 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1961 fb_label_init (void)
1963 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1966 /* Add one to the instance number of this fb label. */
1969 fb_label_instance_inc (long label
)
1973 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1975 ++fb_low_counter
[label
];
1979 if (fb_labels
!= NULL
)
1981 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1982 i
< fb_labels
+ fb_label_count
; ++i
)
1986 ++fb_label_instances
[i
- fb_labels
];
1988 } /* if we find it */
1989 } /* for each existing label */
1992 /* If we get to here, we don't have label listed yet. */
1994 if (fb_labels
== NULL
)
1996 fb_labels
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1997 fb_label_instances
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1998 fb_label_max
= FB_LABEL_BUMP_BY
;
1999 fb_label_count
= FB_LABEL_SPECIAL
;
2002 else if (fb_label_count
== fb_label_max
)
2004 fb_label_max
+= FB_LABEL_BUMP_BY
;
2005 fb_labels
= XRESIZEVEC (long, fb_labels
, fb_label_max
);
2006 fb_label_instances
= XRESIZEVEC (long, fb_label_instances
, fb_label_max
);
2007 } /* if we needed to grow */
2009 fb_labels
[fb_label_count
] = label
;
2010 fb_label_instances
[fb_label_count
] = 1;
2015 fb_label_instance (long label
)
2019 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
2021 return (fb_low_counter
[label
]);
2024 if (fb_labels
!= NULL
)
2026 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
2027 i
< fb_labels
+ fb_label_count
; ++i
)
2031 return (fb_label_instances
[i
- fb_labels
]);
2032 } /* if we find it */
2033 } /* for each existing label */
2036 /* We didn't find the label, so this must be a reference to the
2041 /* Caller must copy returned name: we re-use the area for the next name.
2043 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2044 where n is the label number and m is the instance number. "L" makes
2045 it a label discarded unless debugging and "^B"('\2') ensures no
2046 ordinary symbol SHOULD get the same name as a local label
2047 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2049 dollar labels get the same treatment, except that ^A is used in
2052 char * /* Return local label name. */
2053 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
2054 long augend
/* 0 for nb, 1 for n:, nf. */)
2057 /* Returned to caller, then copied. Used for created names ("4f"). */
2058 static char symbol_name_build
[24];
2061 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
2065 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
2067 know ((unsigned long) augend
<= 1);
2069 p
= symbol_name_build
;
2070 #ifdef LOCAL_LABEL_PREFIX
2071 *p
++ = LOCAL_LABEL_PREFIX
;
2075 /* Next code just does sprintf( {}, "%d", n); */
2077 q
= symbol_name_temporary
;
2078 for (*q
++ = 0, i
= n
; i
; ++q
)
2083 while ((*p
= *--q
) != '\0')
2086 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
2088 /* Instance number. */
2089 q
= symbol_name_temporary
;
2090 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
2095 while ((*p
++ = *--q
) != '\0');
2097 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2098 return (symbol_name_build
);
2101 /* Decode name that may have been generated by foo_label_name() above.
2102 If the name wasn't generated by foo_label_name(), then return it
2103 unaltered. This is used for error messages. */
2106 decode_local_label_name (char *s
)
2109 char *symbol_decode
;
2111 int instance_number
;
2113 const char *message_format
;
2116 #ifdef LOCAL_LABEL_PREFIX
2117 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
2121 if (s
[lindex
] != 'L')
2124 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
2125 label_number
= (10 * label_number
) + *p
- '0';
2127 if (*p
== DOLLAR_LABEL_CHAR
)
2129 else if (*p
== LOCAL_LABEL_CHAR
)
2134 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
2135 instance_number
= (10 * instance_number
) + *p
- '0';
2137 message_format
= _("\"%d\" (instance number %d of a %s label)");
2138 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
2139 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
2141 return symbol_decode
;
2144 /* Get the value of a symbol. */
2147 S_GET_VALUE (symbolS
*s
)
2149 if (s
->flags
.local_symbol
)
2150 return resolve_symbol_value (s
);
2152 if (!s
->flags
.resolved
)
2154 valueT val
= resolve_symbol_value (s
);
2158 if (S_IS_WEAKREFR (s
))
2159 return S_GET_VALUE (s
->x
->value
.X_add_symbol
);
2161 if (s
->x
->value
.X_op
!= O_constant
)
2163 if (! s
->flags
.resolved
2164 || s
->x
->value
.X_op
!= O_symbol
2165 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
2166 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2169 return (valueT
) s
->x
->value
.X_add_number
;
2172 /* Set the value of a symbol. */
2175 S_SET_VALUE (symbolS
*s
, valueT val
)
2177 if (s
->flags
.local_symbol
)
2179 ((struct local_symbol
*) s
)->value
= val
;
2183 s
->x
->value
.X_op
= O_constant
;
2184 s
->x
->value
.X_add_number
= (offsetT
) val
;
2185 s
->x
->value
.X_unsigned
= 0;
2186 S_CLEAR_WEAKREFR (s
);
2190 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2192 if (dest
->flags
.local_symbol
)
2193 dest
= local_symbol_convert (dest
);
2194 if (src
->flags
.local_symbol
)
2195 src
= local_symbol_convert (src
);
2197 /* In an expression, transfer the settings of these flags.
2198 The user can override later, of course. */
2199 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2200 | BSF_GNU_INDIRECT_FUNCTION)
2201 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2203 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2204 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2207 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2208 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2213 S_IS_FUNCTION (symbolS
*s
)
2217 if (s
->flags
.local_symbol
)
2220 flags
= s
->bsym
->flags
;
2222 return (flags
& BSF_FUNCTION
) != 0;
2226 S_IS_EXTERNAL (symbolS
*s
)
2230 if (s
->flags
.local_symbol
)
2233 flags
= s
->bsym
->flags
;
2236 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2239 return (flags
& BSF_GLOBAL
) != 0;
2243 S_IS_WEAK (symbolS
*s
)
2245 if (s
->flags
.local_symbol
)
2247 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2248 could probably handle a WEAKREFR as always weak though. E.g., if
2249 the referenced symbol has lost its weak status, there's no reason
2250 to keep handling the weakrefr as if it was weak. */
2251 if (S_IS_WEAKREFR (s
))
2252 return S_IS_WEAK (s
->x
->value
.X_add_symbol
);
2253 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2257 S_IS_WEAKREFR (symbolS
*s
)
2259 if (s
->flags
.local_symbol
)
2261 return s
->flags
.weakrefr
!= 0;
2265 S_IS_WEAKREFD (symbolS
*s
)
2267 if (s
->flags
.local_symbol
)
2269 return s
->flags
.weakrefd
!= 0;
2273 S_IS_COMMON (symbolS
*s
)
2275 if (s
->flags
.local_symbol
)
2277 return bfd_is_com_section (s
->bsym
->section
);
2281 S_IS_DEFINED (symbolS
*s
)
2283 if (s
->flags
.local_symbol
)
2284 return ((struct local_symbol
*) s
)->section
!= undefined_section
;
2285 return s
->bsym
->section
!= undefined_section
;
2289 #ifndef EXTERN_FORCE_RELOC
2290 #define EXTERN_FORCE_RELOC IS_ELF
2293 /* Return true for symbols that should not be reduced to section
2294 symbols or eliminated from expressions, because they may be
2295 overridden by the linker. */
2297 S_FORCE_RELOC (symbolS
*s
, int strict
)
2300 if (s
->flags
.local_symbol
)
2301 sec
= ((struct local_symbol
*) s
)->section
;
2305 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2306 || (EXTERN_FORCE_RELOC
2307 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2308 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2310 sec
= s
->bsym
->section
;
2312 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2316 S_IS_DEBUG (symbolS
*s
)
2318 if (s
->flags
.local_symbol
)
2320 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2326 S_IS_LOCAL (symbolS
*s
)
2331 if (s
->flags
.local_symbol
)
2334 flags
= s
->bsym
->flags
;
2337 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2340 if (bfd_asymbol_section (s
->bsym
) == reg_section
)
2343 if (flag_strip_local_absolute
2344 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2345 the source file even when the object file is stripped. */
2346 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2347 && bfd_asymbol_section (s
->bsym
) == absolute_section
)
2350 name
= S_GET_NAME (s
);
2351 return (name
!= NULL
2353 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2354 || strchr (name
, LOCAL_LABEL_CHAR
)
2355 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2356 || strchr (name
, FAKE_LABEL_CHAR
)
2358 || TC_LABEL_IS_LOCAL (name
)
2359 || (! flag_keep_locals
2360 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2363 && name
[1] == '?')))));
2367 S_IS_STABD (symbolS
*s
)
2369 return S_GET_NAME (s
) == 0;
2373 S_CAN_BE_REDEFINED (const symbolS
*s
)
2375 if (s
->flags
.local_symbol
)
2376 return (((struct local_symbol
*) s
)->frag
2377 == &predefined_address_frag
);
2378 /* Permit register names to be redefined. */
2379 return s
->bsym
->section
== reg_section
;
2383 S_IS_VOLATILE (const symbolS
*s
)
2385 if (s
->flags
.local_symbol
)
2387 return s
->flags
.volatil
;
2391 S_IS_FORWARD_REF (const symbolS
*s
)
2393 if (s
->flags
.local_symbol
)
2395 return s
->flags
.forward_ref
;
2399 S_GET_NAME (symbolS
*s
)
2405 S_GET_SEGMENT (symbolS
*s
)
2407 if (s
->flags
.local_symbol
)
2408 return ((struct local_symbol
*) s
)->section
;
2409 return s
->bsym
->section
;
2413 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2415 if (s
->flags
.local_symbol
)
2417 ((struct local_symbol
*) s
)->section
= seg
;
2421 /* Don't reassign section symbols. The direct reason is to prevent seg
2422 faults assigning back to const global symbols such as *ABS*, but it
2423 shouldn't happen anyway. */
2424 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2426 if (s
->bsym
->section
!= seg
)
2430 s
->bsym
->section
= seg
;
2434 S_SET_EXTERNAL (symbolS
*s
)
2436 if (s
->flags
.local_symbol
)
2437 s
= local_symbol_convert (s
);
2438 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2440 /* Let .weak override .global. */
2443 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2445 /* Do not reassign section symbols. */
2446 as_warn (_("can't make section symbol global"));
2449 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2450 if (S_GET_SEGMENT (s
) == reg_section
)
2452 as_bad (_("can't make register symbol global"));
2456 s
->bsym
->flags
|= BSF_GLOBAL
;
2457 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2460 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2461 an_external_name
= S_GET_NAME (s
);
2466 S_CLEAR_EXTERNAL (symbolS
*s
)
2468 if (s
->flags
.local_symbol
)
2470 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2472 /* Let .weak override. */
2475 s
->bsym
->flags
|= BSF_LOCAL
;
2476 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2480 S_SET_WEAK (symbolS
*s
)
2482 if (s
->flags
.local_symbol
)
2483 s
= local_symbol_convert (s
);
2484 #ifdef obj_set_weak_hook
2485 obj_set_weak_hook (s
);
2487 s
->bsym
->flags
|= BSF_WEAK
;
2488 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2492 S_SET_WEAKREFR (symbolS
*s
)
2494 if (s
->flags
.local_symbol
)
2495 s
= local_symbol_convert (s
);
2496 s
->flags
.weakrefr
= 1;
2497 /* If the alias was already used, make sure we mark the target as
2498 used as well, otherwise it might be dropped from the symbol
2499 table. This may have unintended side effects if the alias is
2500 later redirected to another symbol, such as keeping the unused
2501 previous target in the symbol table. Since it will be weak, it's
2504 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2508 S_CLEAR_WEAKREFR (symbolS
*s
)
2510 if (s
->flags
.local_symbol
)
2512 s
->flags
.weakrefr
= 0;
2516 S_SET_WEAKREFD (symbolS
*s
)
2518 if (s
->flags
.local_symbol
)
2519 s
= local_symbol_convert (s
);
2520 s
->flags
.weakrefd
= 1;
2525 S_CLEAR_WEAKREFD (symbolS
*s
)
2527 if (s
->flags
.local_symbol
)
2529 if (s
->flags
.weakrefd
)
2531 s
->flags
.weakrefd
= 0;
2532 /* If a weakref target symbol is weak, then it was never
2533 referenced directly before, not even in a .global directive,
2534 so decay it to local. If it remains undefined, it will be
2535 later turned into a global, like any other undefined
2537 if (s
->bsym
->flags
& BSF_WEAK
)
2539 #ifdef obj_clear_weak_hook
2540 obj_clear_weak_hook (s
);
2542 s
->bsym
->flags
&= ~BSF_WEAK
;
2543 s
->bsym
->flags
|= BSF_LOCAL
;
2549 S_SET_THREAD_LOCAL (symbolS
*s
)
2551 if (s
->flags
.local_symbol
)
2552 s
= local_symbol_convert (s
);
2553 if (bfd_is_com_section (s
->bsym
->section
)
2554 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2556 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2557 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2558 as_bad (_("Accessing function `%s' as thread-local object"),
2560 else if (! bfd_is_und_section (s
->bsym
->section
)
2561 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2562 as_bad (_("Accessing `%s' as thread-local object"),
2567 S_SET_NAME (symbolS
*s
, const char *name
)
2570 if (s
->flags
.local_symbol
)
2572 s
->bsym
->name
= name
;
2576 S_SET_VOLATILE (symbolS
*s
)
2578 if (s
->flags
.local_symbol
)
2579 s
= local_symbol_convert (s
);
2580 s
->flags
.volatil
= 1;
2584 S_CLEAR_VOLATILE (symbolS
*s
)
2586 if (!s
->flags
.local_symbol
)
2587 s
->flags
.volatil
= 0;
2591 S_SET_FORWARD_REF (symbolS
*s
)
2593 if (s
->flags
.local_symbol
)
2594 s
= local_symbol_convert (s
);
2595 s
->flags
.forward_ref
= 1;
2598 /* Return the previous symbol in a chain. */
2601 symbol_previous (symbolS
*s
)
2603 if (s
->flags
.local_symbol
)
2605 return s
->x
->previous
;
2608 /* Return the next symbol in a chain. */
2611 symbol_next (symbolS
*s
)
2613 if (s
->flags
.local_symbol
)
2618 /* Return a pointer to the value of a symbol as an expression. */
2621 symbol_get_value_expression (symbolS
*s
)
2623 if (s
->flags
.local_symbol
)
2624 s
= local_symbol_convert (s
);
2625 return &s
->x
->value
;
2628 /* Set the value of a symbol to an expression. */
2631 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2633 if (s
->flags
.local_symbol
)
2634 s
= local_symbol_convert (s
);
2636 S_CLEAR_WEAKREFR (s
);
2639 /* Return whether 2 symbols are the same. */
2642 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2647 /* Return a pointer to the X_add_number component of a symbol. */
2650 symbol_X_add_number (symbolS
*s
)
2652 if (s
->flags
.local_symbol
)
2653 return (offsetT
*) &((struct local_symbol
*) s
)->value
;
2655 return &s
->x
->value
.X_add_number
;
2658 /* Set the value of SYM to the current position in the current segment. */
2661 symbol_set_value_now (symbolS
*sym
)
2663 S_SET_SEGMENT (sym
, now_seg
);
2664 S_SET_VALUE (sym
, frag_now_fix ());
2665 symbol_set_frag (sym
, frag_now
);
2668 /* Set the frag of a symbol. */
2671 symbol_set_frag (symbolS
*s
, fragS
*f
)
2673 if (s
->flags
.local_symbol
)
2675 ((struct local_symbol
*) s
)->frag
= f
;
2679 S_CLEAR_WEAKREFR (s
);
2682 /* Return the frag of a symbol. */
2685 symbol_get_frag (symbolS
*s
)
2687 if (s
->flags
.local_symbol
)
2688 return ((struct local_symbol
*) s
)->frag
;
2692 /* Mark a symbol as having been used. */
2695 symbol_mark_used (symbolS
*s
)
2697 if (s
->flags
.local_symbol
)
2700 if (S_IS_WEAKREFR (s
))
2701 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2704 /* Clear the mark of whether a symbol has been used. */
2707 symbol_clear_used (symbolS
*s
)
2709 if (s
->flags
.local_symbol
)
2710 s
= local_symbol_convert (s
);
2714 /* Return whether a symbol has been used. */
2717 symbol_used_p (symbolS
*s
)
2719 if (s
->flags
.local_symbol
)
2721 return s
->flags
.used
;
2724 /* Mark a symbol as having been used in a reloc. */
2727 symbol_mark_used_in_reloc (symbolS
*s
)
2729 if (s
->flags
.local_symbol
)
2730 s
= local_symbol_convert (s
);
2731 s
->flags
.used_in_reloc
= 1;
2734 /* Clear the mark of whether a symbol has been used in a reloc. */
2737 symbol_clear_used_in_reloc (symbolS
*s
)
2739 if (s
->flags
.local_symbol
)
2741 s
->flags
.used_in_reloc
= 0;
2744 /* Return whether a symbol has been used in a reloc. */
2747 symbol_used_in_reloc_p (symbolS
*s
)
2749 if (s
->flags
.local_symbol
)
2751 return s
->flags
.used_in_reloc
;
2754 /* Mark a symbol as an MRI common symbol. */
2757 symbol_mark_mri_common (symbolS
*s
)
2759 if (s
->flags
.local_symbol
)
2760 s
= local_symbol_convert (s
);
2761 s
->flags
.mri_common
= 1;
2764 /* Clear the mark of whether a symbol is an MRI common symbol. */
2767 symbol_clear_mri_common (symbolS
*s
)
2769 if (s
->flags
.local_symbol
)
2771 s
->flags
.mri_common
= 0;
2774 /* Return whether a symbol is an MRI common symbol. */
2777 symbol_mri_common_p (symbolS
*s
)
2779 if (s
->flags
.local_symbol
)
2781 return s
->flags
.mri_common
;
2784 /* Mark a symbol as having been written. */
2787 symbol_mark_written (symbolS
*s
)
2789 if (s
->flags
.local_symbol
)
2791 s
->flags
.written
= 1;
2794 /* Clear the mark of whether a symbol has been written. */
2797 symbol_clear_written (symbolS
*s
)
2799 if (s
->flags
.local_symbol
)
2801 s
->flags
.written
= 0;
2804 /* Return whether a symbol has been written. */
2807 symbol_written_p (symbolS
*s
)
2809 if (s
->flags
.local_symbol
)
2811 return s
->flags
.written
;
2814 /* Mark a symbol as to be removed. */
2817 symbol_mark_removed (symbolS
*s
)
2819 if (s
->flags
.local_symbol
)
2821 s
->flags
.removed
= 1;
2824 /* Return whether a symbol has been marked to be removed. */
2827 symbol_removed_p (symbolS
*s
)
2829 if (s
->flags
.local_symbol
)
2831 return s
->flags
.removed
;
2834 /* Mark a symbol has having been resolved. */
2837 symbol_mark_resolved (symbolS
*s
)
2839 s
->flags
.resolved
= 1;
2842 /* Return whether a symbol has been resolved. */
2845 symbol_resolved_p (symbolS
*s
)
2847 return s
->flags
.resolved
;
2850 /* Return whether a symbol is a section symbol. */
2853 symbol_section_p (symbolS
*s
)
2855 if (s
->flags
.local_symbol
)
2857 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2860 /* Return whether a symbol is equated to another symbol. */
2863 symbol_equated_p (symbolS
*s
)
2865 if (s
->flags
.local_symbol
)
2867 return s
->x
->value
.X_op
== O_symbol
;
2870 /* Return whether a symbol is equated to another symbol, and should be
2871 treated specially when writing out relocs. */
2874 symbol_equated_reloc_p (symbolS
*s
)
2876 if (s
->flags
.local_symbol
)
2878 /* X_op_symbol, normally not used for O_symbol, is set by
2879 resolve_symbol_value to flag expression syms that have been
2881 return (s
->x
->value
.X_op
== O_symbol
2882 #if defined (OBJ_COFF) && defined (TE_PE)
2885 && ((s
->flags
.resolved
&& s
->x
->value
.X_op_symbol
!= NULL
)
2886 || ! S_IS_DEFINED (s
)
2887 || S_IS_COMMON (s
)));
2890 /* Return whether a symbol has a constant value. */
2893 symbol_constant_p (symbolS
*s
)
2895 if (s
->flags
.local_symbol
)
2897 return s
->x
->value
.X_op
== O_constant
;
2900 /* Return whether a symbol was cloned and thus removed from the global
2904 symbol_shadow_p (symbolS
*s
)
2906 if (s
->flags
.local_symbol
)
2908 return s
->x
->next
== s
;
2911 /* If S is a struct symbol return S, otherwise return NULL. */
2914 symbol_symbolS (symbolS
*s
)
2916 if (s
->flags
.local_symbol
)
2921 /* Return the BFD symbol for a symbol. */
2924 symbol_get_bfdsym (symbolS
*s
)
2926 if (s
->flags
.local_symbol
)
2927 s
= local_symbol_convert (s
);
2931 /* Set the BFD symbol for a symbol. */
2934 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2936 if (s
->flags
.local_symbol
)
2937 s
= local_symbol_convert (s
);
2938 /* Usually, it is harmless to reset a symbol to a BFD section
2939 symbol. For example, obj_elf_change_section sets the BFD symbol
2940 of an old symbol with the newly created section symbol. But when
2941 we have multiple sections with the same name, the newly created
2942 section may have the same name as an old section. We check if the
2943 old symbol has been already marked as a section symbol before
2945 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2947 /* else XXX - What do we do now ? */
2950 #ifdef OBJ_SYMFIELD_TYPE
2952 /* Get a pointer to the object format information for a symbol. */
2955 symbol_get_obj (symbolS
*s
)
2957 if (s
->flags
.local_symbol
)
2958 s
= local_symbol_convert (s
);
2962 /* Set the object format information for a symbol. */
2965 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2967 if (s
->flags
.local_symbol
)
2968 s
= local_symbol_convert (s
);
2972 #endif /* OBJ_SYMFIELD_TYPE */
2974 #ifdef TC_SYMFIELD_TYPE
2976 /* Get a pointer to the processor information for a symbol. */
2979 symbol_get_tc (symbolS
*s
)
2981 if (s
->flags
.local_symbol
)
2982 s
= local_symbol_convert (s
);
2986 /* Set the processor information for a symbol. */
2989 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2991 if (s
->flags
.local_symbol
)
2992 s
= local_symbol_convert (s
);
2996 #endif /* TC_SYMFIELD_TYPE */
3001 symbol_lastP
= NULL
;
3002 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
3003 sy_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
3004 NULL
, xcalloc
, free
);
3006 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3007 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
3009 abs_symbol
.x
= &abs_symbol_x
;
3010 abs_symbol
.x
->value
.X_op
= O_constant
;
3011 abs_symbol
.frag
= &zero_address_frag
;
3013 if (LOCAL_LABELS_FB
)
3018 dot_symbol_init (void)
3020 dot_symbol
.name
= ".";
3021 dot_symbol
.flags
.forward_ref
= 1;
3022 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
3023 if (dot_symbol
.bsym
== NULL
)
3024 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3025 dot_symbol
.bsym
->name
= ".";
3026 dot_symbol
.x
= &dot_symbol_x
;
3027 dot_symbol
.x
->value
.X_op
= O_constant
;
3032 /* Maximum indent level.
3033 Available for modification inside a gdb session. */
3034 static int max_indent_level
= 8;
3037 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
3039 const char *name
= S_GET_NAME (sym
);
3040 if (!name
|| !name
[0])
3042 fprintf (file
, "sym ");
3043 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
3044 fprintf (file
, " %s", name
);
3046 if (sym
->flags
.local_symbol
)
3048 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
3050 if (locsym
->frag
!= &zero_address_frag
3051 && locsym
->frag
!= NULL
)
3053 fprintf (file
, " frag ");
3054 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) locsym
->frag
));
3056 if (locsym
->flags
.resolved
)
3057 fprintf (file
, " resolved");
3058 fprintf (file
, " local");
3062 if (sym
->frag
!= &zero_address_frag
)
3064 fprintf (file
, " frag ");
3065 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->frag
));
3067 if (sym
->flags
.written
)
3068 fprintf (file
, " written");
3069 if (sym
->flags
.resolved
)
3070 fprintf (file
, " resolved");
3071 else if (sym
->flags
.resolving
)
3072 fprintf (file
, " resolving");
3073 if (sym
->flags
.used_in_reloc
)
3074 fprintf (file
, " used-in-reloc");
3075 if (sym
->flags
.used
)
3076 fprintf (file
, " used");
3077 if (S_IS_LOCAL (sym
))
3078 fprintf (file
, " local");
3079 if (S_IS_EXTERNAL (sym
))
3080 fprintf (file
, " extern");
3081 if (S_IS_WEAK (sym
))
3082 fprintf (file
, " weak");
3083 if (S_IS_DEBUG (sym
))
3084 fprintf (file
, " debug");
3085 if (S_IS_DEFINED (sym
))
3086 fprintf (file
, " defined");
3088 if (S_IS_WEAKREFR (sym
))
3089 fprintf (file
, " weakrefr");
3090 if (S_IS_WEAKREFD (sym
))
3091 fprintf (file
, " weakrefd");
3092 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
3093 if (symbol_resolved_p (sym
))
3095 segT s
= S_GET_SEGMENT (sym
);
3097 if (s
!= undefined_section
3098 && s
!= expr_section
)
3099 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
3101 else if (indent_level
< max_indent_level
3102 && S_GET_SEGMENT (sym
) != undefined_section
)
3105 fprintf (file
, "\n%*s<", indent_level
* 4, "");
3106 if (sym
->flags
.local_symbol
)
3107 fprintf (file
, "constant %lx",
3108 (unsigned long) ((struct local_symbol
*) sym
)->value
);
3110 print_expr_1 (file
, &sym
->x
->value
);
3111 fprintf (file
, ">");
3118 print_symbol_value (symbolS
*sym
)
3121 print_symbol_value_1 (stderr
, sym
);
3122 fprintf (stderr
, "\n");
3126 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
3129 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
3130 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3131 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3132 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3133 fprintf (file
, ">");
3138 print_expr_1 (FILE *file
, expressionS
*exp
)
3140 fprintf (file
, "expr ");
3141 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
3142 fprintf (file
, " ");
3146 fprintf (file
, "illegal");
3149 fprintf (file
, "absent");
3152 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
3156 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
3157 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3158 fprintf (file
, ">");
3160 if (exp
->X_add_number
)
3161 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
3162 (unsigned long) exp
->X_add_number
);
3166 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
3169 fprintf (file
, "big");
3172 fprintf (file
, "uminus -<");
3174 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3175 fprintf (file
, ">");
3176 goto maybe_print_addnum
;
3178 fprintf (file
, "bit_not");
3181 print_binary (file
, "multiply", exp
);
3184 print_binary (file
, "divide", exp
);
3187 print_binary (file
, "modulus", exp
);
3190 print_binary (file
, "lshift", exp
);
3193 print_binary (file
, "rshift", exp
);
3195 case O_bit_inclusive_or
:
3196 print_binary (file
, "bit_ior", exp
);
3198 case O_bit_exclusive_or
:
3199 print_binary (file
, "bit_xor", exp
);
3202 print_binary (file
, "bit_and", exp
);
3205 print_binary (file
, "eq", exp
);
3208 print_binary (file
, "ne", exp
);
3211 print_binary (file
, "lt", exp
);
3214 print_binary (file
, "le", exp
);
3217 print_binary (file
, "ge", exp
);
3220 print_binary (file
, "gt", exp
);
3223 print_binary (file
, "logical_and", exp
);
3226 print_binary (file
, "logical_or", exp
);
3230 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3231 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3232 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3233 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3234 fprintf (file
, ">");
3235 goto maybe_print_addnum
;
3238 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3239 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3240 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3241 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3242 fprintf (file
, ">");
3243 goto maybe_print_addnum
;
3245 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3252 print_expr (expressionS
*exp
)
3254 print_expr_1 (stderr
, exp
);
3255 fprintf (stderr
, "\n");
3259 symbol_print_statistics (FILE *file
)
3261 htab_print_statistics (file
, "symbol table", sy_hash
);
3262 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3263 local_symbol_count
, local_symbol_conversion_count
);
3266 #ifdef OBJ_COMPLEX_RELC
3268 /* Convert given symbol to a new complex-relocation symbol name. This
3269 may be a recursive function, since it might be called for non-leaf
3270 nodes (plain symbols) in the expression tree. The caller owns the
3271 returning string, so should free it eventually. Errors are
3272 indicated via as_bad and a NULL return value. The given symbol
3273 is marked with used_in_reloc. */
3276 symbol_relc_make_sym (symbolS
* sym
)
3278 char * terminal
= NULL
;
3283 gas_assert (sym
!= NULL
);
3285 /* Recurse to symbol_relc_make_expr if this symbol
3286 is defined as an expression or a plain value. */
3287 if ( S_GET_SEGMENT (sym
) == expr_section
3288 || S_GET_SEGMENT (sym
) == absolute_section
)
3289 return symbol_relc_make_expr (symbol_get_value_expression (sym
));
3291 /* This may be a "fake symbol", referring to ".".
3292 Write out a special null symbol to refer to this position. */
3293 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3294 return xstrdup (".");
3296 /* We hope this is a plain leaf symbol. Construct the encoding
3297 as {S,s}II...:CCCCCCC....
3298 where 'S'/'s' means section symbol / plain symbol
3299 III is decimal for the symbol name length
3300 CCC is the symbol name itself. */
3301 symbol_mark_used_in_reloc (sym
);
3303 sname
= S_GET_NAME (sym
);
3304 sname_len
= strlen (sname
);
3305 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3307 terminal
= XNEWVEC (char, (1 /* S or s */
3308 + 8 /* sname_len in decimal */
3310 + sname_len
/* name itself */
3313 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3317 /* Convert given value to a new complex-relocation symbol name. This
3318 is a non-recursive function, since it is be called for leaf nodes
3319 (plain values) in the expression tree. The caller owns the
3320 returning string, so should free() it eventually. No errors. */
3323 symbol_relc_make_value (offsetT val
)
3325 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3328 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3332 /* Convert given expression to a new complex-relocation symbol name.
3333 This is a recursive function, since it traverses the entire given
3334 expression tree. The caller owns the returning string, so should
3335 free() it eventually. Errors are indicated via as_bad() and a NULL
3339 symbol_relc_make_expr (expressionS
* exp
)
3341 const char * opstr
= NULL
; /* Operator prefix string. */
3342 int arity
= 0; /* Arity of this operator. */
3343 char * operands
[3]; /* Up to three operands. */
3344 char * concat_string
= NULL
;
3346 operands
[0] = operands
[1] = operands
[2] = NULL
;
3348 gas_assert (exp
!= NULL
);
3350 /* Match known operators -> fill in opstr, arity, operands[] and fall
3351 through to construct subexpression fragments; may instead return
3352 string directly for leaf nodes. */
3354 /* See expr.h for the meaning of all these enums. Many operators
3355 have an unnatural arity (X_add_number implicitly added). The
3356 conversion logic expands them to explicit "+" subexpressions. */
3361 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3366 return symbol_relc_make_value (exp
->X_add_number
);
3369 if (exp
->X_add_number
)
3373 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3374 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3378 return symbol_relc_make_sym (exp
->X_add_symbol
);
3380 /* Helper macros for nesting nodes. */
3382 #define HANDLE_XADD_OPT1(str_) \
3383 if (exp->X_add_number) \
3386 opstr = "+:" str_; \
3387 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3388 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3395 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3399 #define HANDLE_XADD_OPT2(str_) \
3400 if (exp->X_add_number) \
3403 opstr = "+:" str_; \
3404 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3405 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3406 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3412 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3413 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3417 /* Nesting nodes. */
3419 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3420 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3421 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3422 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3423 case O_divide
: HANDLE_XADD_OPT2 ("/");
3424 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3425 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3426 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3427 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3428 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3429 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3430 case O_add
: HANDLE_XADD_OPT2 ("+");
3431 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3432 case O_eq
: HANDLE_XADD_OPT2 ("==");
3433 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3434 case O_lt
: HANDLE_XADD_OPT2 ("<");
3435 case O_le
: HANDLE_XADD_OPT2 ("<=");
3436 case O_ge
: HANDLE_XADD_OPT2 (">=");
3437 case O_gt
: HANDLE_XADD_OPT2 (">");
3438 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3439 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3442 /* Validate & reject early. */
3443 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3445 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3447 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3451 concat_string
= NULL
;
3452 else if (arity
== 0)
3453 concat_string
= xstrdup (opstr
);
3454 else if (arity
== 1)
3455 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3456 else if (arity
== 2)
3457 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3460 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3461 operands
[2], (char *) NULL
);
3463 /* Free operand strings (not opstr). */
3464 if (arity
>= 1) xfree (operands
[0]);
3465 if (arity
>= 2) xfree (operands
[1]);
3466 if (arity
>= 3) xfree (operands
[2]);
3468 return concat_string
;