1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2020 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" */
31 /* Whether the symbol is a local_symbol. */
32 unsigned int local_symbol
: 1;
34 /* Weather symbol has been written. */
35 unsigned int written
: 1;
37 /* Whether symbol value has been completely resolved (used during
38 final pass over symbol table). */
39 unsigned int resolved
: 1;
41 /* Whether the symbol value is currently being resolved (used to
42 detect loops in symbol dependencies). */
43 unsigned int resolving
: 1;
45 /* Whether the symbol value is used in a reloc. This is used to
46 ensure that symbols used in relocs are written out, even if they
47 are local and would otherwise not be. */
48 unsigned int used_in_reloc
: 1;
50 /* Whether the symbol is used as an operand or in an expression.
51 NOTE: Not all the backends keep this information accurate;
52 backends which use this bit are responsible for setting it when
53 a symbol is used in backend routines. */
54 unsigned int used
: 1;
56 /* Whether the symbol can be re-defined. */
57 unsigned int volatil
: 1;
59 /* Whether the symbol is a forward reference. */
60 unsigned int forward_ref
: 1;
62 /* This is set if the symbol is defined in an MRI common section.
63 We handle such sections as single common symbols, so symbols
64 defined within them must be treated specially by the relocation
66 unsigned int mri_common
: 1;
68 /* This is set if the symbol is set with a .weakref directive. */
69 unsigned int weakrefr
: 1;
71 /* This is set when the symbol is referenced as part of a .weakref
72 directive, but only if the symbol was not in the symbol table
73 before. It is cleared as soon as any direct reference to the
75 unsigned int weakrefd
: 1;
78 /* A pointer in the symbol may point to either a complete symbol
79 (struct symbol below) or to a local symbol (struct local_symbol
80 defined here). The symbol code can detect the case by examining
81 the first field which is present in both structs.
83 We do this because we ordinarily only need a small amount of
84 information for a local symbol. The symbol table takes up a lot of
85 space, and storing less information for a local symbol can make a
86 big difference in assembler memory usage when assembling a large
91 /* Symbol flags. Only local_symbol and resolved are relevant. */
92 struct symbol_flags flags
;
94 /* Hash value calculated from name. */
97 /* The symbol name. */
100 /* The symbol frag. */
103 /* The symbol section. */
106 /* The value of the symbol. */
110 /* The information we keep for a symbol. The symbol table holds
111 pointers both to this and to local_symbol structures. The first
112 three fields must be identical to struct local_symbol, and the size
113 should be the same as or smaller than struct local_symbol.
114 Fields that don't fit go to an extension structure. */
119 struct symbol_flags flags
;
121 /* Hash value calculated from name. */
124 /* The symbol name. */
127 /* Pointer to the frag this symbol is attached to, if any.
134 /* Extra symbol fields that won't fit. */
138 /* Extra fields to make up a full symbol. */
142 /* The value of the symbol. */
145 /* Forwards and backwards chain pointers. */
147 struct symbol
*previous
;
149 #ifdef OBJ_SYMFIELD_TYPE
150 OBJ_SYMFIELD_TYPE obj
;
153 #ifdef TC_SYMFIELD_TYPE
158 typedef union symbol_entry
160 struct local_symbol lsy
;
164 /* Hash function for a symbol_entry. */
167 hash_symbol_entry (const void *e
)
169 symbol_entry_t
*entry
= (symbol_entry_t
*) e
;
170 if (entry
->sy
.hash
== 0)
171 entry
->sy
.hash
= htab_hash_string (entry
->sy
.name
);
173 return entry
->sy
.hash
;
176 /* Equality function for a symbol_entry. */
179 eq_symbol_entry (const void *a
, const void *b
)
181 const symbol_entry_t
*ea
= (const symbol_entry_t
*) a
;
182 const symbol_entry_t
*eb
= (const symbol_entry_t
*) b
;
184 return (ea
->sy
.hash
== eb
->sy
.hash
185 && strcmp (ea
->sy
.name
, eb
->sy
.name
) == 0);
189 symbol_entry_find (htab_t table
, const char *name
)
191 hashval_t hash
= htab_hash_string (name
);
192 symbol_entry_t needle
= { { { 0 }, hash
, name
, 0, 0, 0 } };
193 return htab_find_with_hash (table
, &needle
, hash
);
197 /* This is non-zero if symbols are case sensitive, which is the
199 int symbols_case_sensitive
= 1;
201 #ifndef WORKING_DOT_WORD
202 extern int new_broken_words
;
205 static htab_t sy_hash
;
207 /* Below are commented in "symbols.h". */
208 symbolS
*symbol_rootP
;
209 symbolS
*symbol_lastP
;
211 struct xsymbol abs_symbol_x
;
213 struct xsymbol dot_symbol_x
;
216 #define debug_verify_symchain verify_symbol_chain
218 #define debug_verify_symchain(root, last) ((void) 0)
221 #define DOLLAR_LABEL_CHAR '\001'
222 #define LOCAL_LABEL_CHAR '\002'
224 #ifndef TC_LABEL_IS_LOCAL
225 #define TC_LABEL_IS_LOCAL(name) 0
228 struct obstack notes
;
230 /* The name of an external symbol which is
231 used to make weak PE symbol names unique. */
232 const char * an_external_name
;
235 static const char *save_symbol_name (const char *);
236 static void fb_label_init (void);
237 static long dollar_label_instance (long);
238 static long fb_label_instance (long);
240 static void print_binary (FILE *, const char *, expressionS
*);
242 /* Return a pointer to a new symbol. Die if we can't make a new
243 symbol. Fill in the symbol's values. Add symbol to end of symbol
246 This function should be called in the general case of creating a
247 symbol. However, if the output file symbol table has already been
248 set, and you are certain that this symbol won't be wanted in the
249 output file, you can call symbol_create. */
252 symbol_new (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
254 symbolS
*symbolP
= symbol_create (name
, segment
, frag
, valu
);
256 /* Link to end of symbol chain. */
257 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
262 /* Save a symbol name on a permanent obstack, and convert it according
263 to the object file format. */
266 save_symbol_name (const char *name
)
271 gas_assert (name
!= NULL
);
272 name_length
= strlen (name
) + 1; /* +1 for \0. */
273 obstack_grow (¬es
, name
, name_length
);
274 ret
= (char *) obstack_finish (¬es
);
276 #ifdef tc_canonicalize_symbol_name
277 ret
= tc_canonicalize_symbol_name (ret
);
280 if (! symbols_case_sensitive
)
284 for (s
= ret
; *s
!= '\0'; s
++)
292 symbol_init (symbolS
*symbolP
, const char *name
, asection
*sec
,
293 fragS
*frag
, valueT valu
)
295 symbolP
->frag
= frag
;
296 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
297 if (symbolP
->bsym
== NULL
)
298 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
299 symbolP
->bsym
->name
= name
;
300 symbolP
->bsym
->section
= sec
;
302 S_SET_VALUE (symbolP
, valu
);
304 symbol_clear_list_pointers (symbolP
);
306 obj_symbol_new_hook (symbolP
);
308 #ifdef tc_symbol_new_hook
309 tc_symbol_new_hook (symbolP
);
313 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
316 symbol_create (const char *name
, segT segment
, fragS
*frag
, valueT valu
)
318 const char *preserved_copy_of_name
;
322 preserved_copy_of_name
= save_symbol_name (name
);
324 size
= sizeof (symbolS
) + sizeof (struct xsymbol
);
325 symbolP
= (symbolS
*) obstack_alloc (¬es
, size
);
327 /* symbol must be born in some fixed state. This seems as good as any. */
328 memset (symbolP
, 0, size
);
329 symbolP
->name
= preserved_copy_of_name
;
330 symbolP
->x
= (struct xsymbol
*) (symbolP
+ 1);
332 symbol_init (symbolP
, preserved_copy_of_name
, segment
, frag
, valu
);
338 /* Local symbol support. If we can get away with it, we keep only a
339 small amount of information for local symbols. */
341 /* Used for statistics. */
343 static unsigned long local_symbol_count
;
344 static unsigned long local_symbol_conversion_count
;
346 /* Create a local symbol and insert it into the local hash table. */
348 struct local_symbol
*
349 local_symbol_make (const char *name
, segT section
, fragS
*frag
, valueT val
)
351 const char *name_copy
;
352 struct local_symbol
*ret
;
353 struct symbol_flags flags
= { .local_symbol
= 1, .resolved
= 0 };
355 ++local_symbol_count
;
357 name_copy
= save_symbol_name (name
);
359 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
362 ret
->name
= name_copy
;
364 ret
->section
= section
;
367 htab_insert (sy_hash
, ret
, 1);
372 /* Convert a local symbol into a real symbol. */
375 local_symbol_convert (void *sym
)
377 symbol_entry_t
*ent
= (symbol_entry_t
*) sym
;
378 struct xsymbol
*xtra
;
381 gas_assert (ent
->lsy
.flags
.local_symbol
);
383 ++local_symbol_conversion_count
;
385 xtra
= (struct xsymbol
*) obstack_alloc (¬es
, sizeof (*xtra
));
386 memset (xtra
, 0, sizeof (*xtra
));
387 val
= ent
->lsy
.value
;
390 /* Local symbols are always either defined or used. */
391 ent
->sy
.flags
.used
= 1;
392 ent
->sy
.flags
.local_symbol
= 0;
394 symbol_init (&ent
->sy
, ent
->lsy
.name
, ent
->lsy
.section
, ent
->lsy
.frag
, val
);
395 symbol_append (&ent
->sy
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
401 define_sym_at_dot (symbolS
*symbolP
)
403 symbolP
->frag
= frag_now
;
404 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
405 S_SET_SEGMENT (symbolP
, now_seg
);
408 /* We have just seen "<name>:".
409 Creates a struct symbol unless it already exists.
411 Gripes if we are redefining a symbol incompatibly (and ignores it). */
414 colon (/* Just seen "x:" - rattle symbols & frags. */
415 const char *sym_name
/* Symbol name, as a canonical string. */
416 /* We copy this string: OK to alter later. */)
418 symbolS
*symbolP
; /* Symbol we are working with. */
420 /* Sun local labels go out of scope whenever a non-local symbol is
422 if (LOCAL_LABELS_DOLLAR
423 && !bfd_is_local_label_name (stdoutput
, sym_name
))
424 dollar_label_clear ();
426 #ifndef WORKING_DOT_WORD
427 if (new_broken_words
)
429 struct broken_word
*a
;
434 if (now_seg
== absolute_section
)
436 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
440 possible_bytes
= (md_short_jump_size
441 + new_broken_words
* md_long_jump_size
);
444 frag_opcode
= frag_var (rs_broken_word
,
448 (symbolS
*) broken_words
,
452 /* We want to store the pointer to where to insert the jump
453 table in the fr_opcode of the rs_broken_word frag. This
454 requires a little hackery. */
456 && (frag_tmp
->fr_type
!= rs_broken_word
457 || frag_tmp
->fr_opcode
))
458 frag_tmp
= frag_tmp
->fr_next
;
460 frag_tmp
->fr_opcode
= frag_opcode
;
461 new_broken_words
= 0;
463 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
464 a
->dispfrag
= frag_tmp
;
466 #endif /* WORKING_DOT_WORD */
468 #ifdef obj_frob_colon
469 obj_frob_colon (sym_name
);
472 if ((symbolP
= symbol_find (sym_name
)) != 0)
474 S_CLEAR_WEAKREFR (symbolP
);
475 #ifdef RESOLVE_SYMBOL_REDEFINITION
476 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
479 /* Now check for undefined symbols. */
480 if (symbolP
->flags
.local_symbol
)
482 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
484 if (locsym
->section
!= undefined_section
485 && (locsym
->frag
!= frag_now
486 || locsym
->section
!= now_seg
487 || locsym
->value
!= frag_now_fix ()))
489 as_bad (_("symbol `%s' is already defined"), sym_name
);
493 locsym
->section
= now_seg
;
494 locsym
->frag
= frag_now
;
495 locsym
->value
= frag_now_fix ();
497 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
498 || S_IS_COMMON (symbolP
)
499 || S_IS_VOLATILE (symbolP
))
501 if (S_IS_VOLATILE (symbolP
))
503 symbolP
= symbol_clone (symbolP
, 1);
504 S_SET_VALUE (symbolP
, 0);
505 S_CLEAR_VOLATILE (symbolP
);
507 if (S_GET_VALUE (symbolP
) == 0)
509 define_sym_at_dot (symbolP
);
512 #endif /* if we have one, it better be zero. */
517 /* There are still several cases to check:
519 A .comm/.lcomm symbol being redefined as initialized
522 A .comm/.lcomm symbol being redefined with a larger
525 This only used to be allowed on VMS gas, but Sun cc
526 on the sparc also depends on it. */
528 if (((!S_IS_DEBUG (symbolP
)
529 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
530 && S_IS_EXTERNAL (symbolP
))
531 || S_GET_SEGMENT (symbolP
) == bss_section
)
532 && (now_seg
== data_section
533 || now_seg
== bss_section
534 || now_seg
== S_GET_SEGMENT (symbolP
)))
536 /* Select which of the 2 cases this is. */
537 if (now_seg
!= data_section
)
539 /* New .comm for prev .comm symbol.
541 If the new size is larger we just change its
542 value. If the new size is smaller, we ignore
544 if (S_GET_VALUE (symbolP
)
545 < ((unsigned) frag_now_fix ()))
547 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
552 /* It is a .comm/.lcomm being converted to initialized
554 define_sym_at_dot (symbolP
);
559 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
560 static const char *od_buf
= "";
564 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
565 sprintf (od_buf
, "%d.%d.",
566 S_GET_OTHER (symbolP
),
567 S_GET_DESC (symbolP
));
569 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
571 segment_name (S_GET_SEGMENT (symbolP
)),
573 (long) S_GET_VALUE (symbolP
));
575 } /* if the undefined symbol has no value */
579 /* Don't blow up if the definition is the same. */
580 if (!(frag_now
== symbolP
->frag
581 && S_GET_VALUE (symbolP
) == frag_now_fix ()
582 && S_GET_SEGMENT (symbolP
) == now_seg
))
584 as_bad (_("symbol `%s' is already defined"), sym_name
);
585 symbolP
= symbol_clone (symbolP
, 0);
586 define_sym_at_dot (symbolP
);
591 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
593 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
, frag_now
,
598 symbolP
= symbol_new (sym_name
, now_seg
, frag_now
, frag_now_fix ());
600 symbol_table_insert (symbolP
);
603 if (mri_common_symbol
!= NULL
)
605 /* This symbol is actually being defined within an MRI common
606 section. This requires special handling. */
607 if (symbolP
->flags
.local_symbol
)
608 symbolP
= local_symbol_convert (symbolP
);
609 symbolP
->x
->value
.X_op
= O_symbol
;
610 symbolP
->x
->value
.X_add_symbol
= mri_common_symbol
;
611 symbolP
->x
->value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
612 symbolP
->frag
= &zero_address_frag
;
613 S_SET_SEGMENT (symbolP
, expr_section
);
614 symbolP
->flags
.mri_common
= 1;
618 tc_frob_label (symbolP
);
620 #ifdef obj_frob_label
621 obj_frob_label (symbolP
);
627 /* Die if we can't insert the symbol. */
630 symbol_table_insert (symbolS
*symbolP
)
634 htab_insert (sy_hash
, symbolP
, 1);
637 /* If a symbol name does not exist, create it as undefined, and insert
638 it into the symbol table. Return a pointer to it. */
641 symbol_find_or_make (const char *name
)
645 symbolP
= symbol_find (name
);
649 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
651 symbolP
= md_undefined_symbol ((char *) name
);
655 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
656 &zero_address_frag
, 0);
660 symbolP
= symbol_make (name
);
662 symbol_table_insert (symbolP
);
663 } /* if symbol wasn't found */
669 symbol_make (const char *name
)
673 /* Let the machine description default it, e.g. for register names. */
674 symbolP
= md_undefined_symbol ((char *) name
);
677 symbolP
= symbol_new (name
, undefined_section
, &zero_address_frag
, 0);
683 symbol_clone (symbolS
*orgsymP
, int replace
)
686 asymbol
*bsymorg
, *bsymnew
;
688 /* Make sure we never clone the dot special symbol. */
689 gas_assert (orgsymP
!= &dot_symbol
);
691 /* When cloning a local symbol it isn't absolutely necessary to
692 convert the original, but converting makes the code much
693 simpler to cover this unexpected case. As of 2020-08-21
694 symbol_clone won't be called on a local symbol. */
695 if (orgsymP
->flags
.local_symbol
)
696 orgsymP
= local_symbol_convert (orgsymP
);
697 bsymorg
= orgsymP
->bsym
;
699 newsymP
= (symbolS
*) obstack_alloc (¬es
, (sizeof (symbolS
)
700 + sizeof (struct xsymbol
)));
702 newsymP
->x
= (struct xsymbol
*) (newsymP
+ 1);
703 *newsymP
->x
= *orgsymP
->x
;
704 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
706 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
707 newsymP
->bsym
= bsymnew
;
708 bsymnew
->name
= bsymorg
->name
;
709 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
710 bsymnew
->section
= bsymorg
->section
;
711 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
712 bfd_asymbol_bfd (bsymnew
), bsymnew
);
714 #ifdef obj_symbol_clone_hook
715 obj_symbol_clone_hook (newsymP
, orgsymP
);
718 #ifdef tc_symbol_clone_hook
719 tc_symbol_clone_hook (newsymP
, orgsymP
);
724 if (symbol_rootP
== orgsymP
)
725 symbol_rootP
= newsymP
;
726 else if (orgsymP
->x
->previous
)
728 orgsymP
->x
->previous
->x
->next
= newsymP
;
729 orgsymP
->x
->previous
= NULL
;
731 if (symbol_lastP
== orgsymP
)
732 symbol_lastP
= newsymP
;
733 else if (orgsymP
->x
->next
)
734 orgsymP
->x
->next
->x
->previous
= newsymP
;
736 /* Symbols that won't be output can't be external. */
737 S_CLEAR_EXTERNAL (orgsymP
);
738 orgsymP
->x
->previous
= orgsymP
->x
->next
= orgsymP
;
739 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
741 symbol_table_insert (newsymP
);
745 /* Symbols that won't be output can't be external. */
746 S_CLEAR_EXTERNAL (newsymP
);
747 newsymP
->x
->previous
= newsymP
->x
->next
= newsymP
;
753 /* Referenced symbols, if they are forward references, need to be cloned
754 (without replacing the original) so that the value of the referenced
755 symbols at the point of use is saved by the clone. */
757 #undef symbol_clone_if_forward_ref
759 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
761 if (symbolP
&& !symbolP
->flags
.local_symbol
)
763 symbolS
*orig_add_symbol
= symbolP
->x
->value
.X_add_symbol
;
764 symbolS
*orig_op_symbol
= symbolP
->x
->value
.X_op_symbol
;
765 symbolS
*add_symbol
= orig_add_symbol
;
766 symbolS
*op_symbol
= orig_op_symbol
;
768 if (symbolP
->flags
.forward_ref
)
773 /* assign_symbol() clones volatile symbols; pre-existing expressions
774 hold references to the original instance, but want the current
775 value. Just repeat the lookup. */
776 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
777 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
778 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
779 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
782 /* Re-using resolving here, as this routine cannot get called from
783 symbol resolution code. */
784 if ((symbolP
->bsym
->section
== expr_section
785 || symbolP
->flags
.forward_ref
)
786 && !symbolP
->flags
.resolving
)
788 symbolP
->flags
.resolving
= 1;
789 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
790 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
791 symbolP
->flags
.resolving
= 0;
794 if (symbolP
->flags
.forward_ref
795 || add_symbol
!= orig_add_symbol
796 || op_symbol
!= orig_op_symbol
)
798 if (symbolP
!= &dot_symbol
)
800 symbolP
= symbol_clone (symbolP
, 0);
801 symbolP
->flags
.resolving
= 0;
805 symbolP
= symbol_temp_new_now ();
806 #ifdef tc_new_dot_label
807 tc_new_dot_label (symbolP
);
812 symbolP
->x
->value
.X_add_symbol
= add_symbol
;
813 symbolP
->x
->value
.X_op_symbol
= op_symbol
;
820 symbol_temp_new (segT seg
, fragS
*frag
, valueT ofs
)
822 return symbol_new (FAKE_LABEL_NAME
, seg
, frag
, ofs
);
826 symbol_temp_new_now (void)
828 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix ());
832 symbol_temp_new_now_octets (void)
834 return symbol_temp_new (now_seg
, frag_now
, frag_now_fix_octets ());
838 symbol_temp_make (void)
840 return symbol_make (FAKE_LABEL_NAME
);
843 /* Implement symbol table lookup.
844 In: A symbol's name as a string: '\0' can't be part of a symbol name.
845 Out: NULL if the name was not in the symbol table, else the address
846 of a struct symbol associated with that name. */
849 symbol_find_exact (const char *name
)
851 return symbol_find_exact_noref (name
, 0);
855 symbol_find_exact_noref (const char *name
, int noref
)
857 symbolS
*sym
= symbol_entry_find (sy_hash
, name
);
859 /* Any references to the symbol, except for the reference in
860 .weakref, must clear this flag, such that the symbol does not
861 turn into a weak symbol. Note that we don't have to handle the
862 local_symbol case, since a weakrefd is always promoted out of the
863 local_symbol table when it is turned into a weak symbol. */
865 S_CLEAR_WEAKREFD (sym
);
871 symbol_find (const char *name
)
873 return symbol_find_noref (name
, 0);
877 symbol_find_noref (const char *name
, int noref
)
882 #ifdef tc_canonicalize_symbol_name
884 copy
= xstrdup (name
);
885 name
= tc_canonicalize_symbol_name (copy
);
889 if (! symbols_case_sensitive
)
898 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
900 while ((c
= *orig
++) != '\0')
901 *copy
++ = TOUPPER (c
);
905 copy
= (char *) name
;
908 result
= symbol_find_exact_noref (name
, noref
);
913 /* Once upon a time, symbols were kept in a singly linked list. At
914 least coff needs to be able to rearrange them from time to time, for
915 which a doubly linked list is much more convenient. Loic did these
916 as macros which seemed dangerous to me so they're now functions.
919 /* Link symbol ADDME after symbol TARGET in the chain. */
922 symbol_append (symbolS
*addme
, symbolS
*target
,
923 symbolS
**rootPP
, symbolS
**lastPP
)
925 extern int symbol_table_frozen
;
926 if (symbol_table_frozen
)
928 if (addme
->flags
.local_symbol
)
930 if (target
!= NULL
&& target
->flags
.local_symbol
)
935 know (*rootPP
== NULL
);
936 know (*lastPP
== NULL
);
937 addme
->x
->next
= NULL
;
938 addme
->x
->previous
= NULL
;
942 } /* if the list is empty */
944 if (target
->x
->next
!= NULL
)
946 target
->x
->next
->x
->previous
= addme
;
950 know (*lastPP
== target
);
952 } /* if we have a next */
954 addme
->x
->next
= target
->x
->next
;
955 target
->x
->next
= addme
;
956 addme
->x
->previous
= target
;
958 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
961 /* Set the chain pointers of SYMBOL to null. */
964 symbol_clear_list_pointers (symbolS
*symbolP
)
966 if (symbolP
->flags
.local_symbol
)
968 symbolP
->x
->next
= NULL
;
969 symbolP
->x
->previous
= NULL
;
972 /* Remove SYMBOLP from the list. */
975 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
977 if (symbolP
->flags
.local_symbol
)
980 if (symbolP
== *rootPP
)
982 *rootPP
= symbolP
->x
->next
;
983 } /* if it was the root */
985 if (symbolP
== *lastPP
)
987 *lastPP
= symbolP
->x
->previous
;
988 } /* if it was the tail */
990 if (symbolP
->x
->next
!= NULL
)
992 symbolP
->x
->next
->x
->previous
= symbolP
->x
->previous
;
995 if (symbolP
->x
->previous
!= NULL
)
997 symbolP
->x
->previous
->x
->next
= symbolP
->x
->next
;
1000 debug_verify_symchain (*rootPP
, *lastPP
);
1003 /* Link symbol ADDME before symbol TARGET in the chain. */
1006 symbol_insert (symbolS
*addme
, symbolS
*target
,
1007 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
1009 extern int symbol_table_frozen
;
1010 if (symbol_table_frozen
)
1012 if (addme
->flags
.local_symbol
)
1014 if (target
->flags
.local_symbol
)
1017 if (target
->x
->previous
!= NULL
)
1019 target
->x
->previous
->x
->next
= addme
;
1023 know (*rootPP
== target
);
1025 } /* if not first */
1027 addme
->x
->previous
= target
->x
->previous
;
1028 target
->x
->previous
= addme
;
1029 addme
->x
->next
= target
;
1031 debug_verify_symchain (*rootPP
, *lastPP
);
1035 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
1037 symbolS
*symbolP
= rootP
;
1039 if (symbolP
== NULL
)
1042 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
1044 gas_assert (symbolP
->bsym
!= NULL
);
1045 gas_assert (symbolP
->flags
.local_symbol
== 0);
1046 gas_assert (symbolP
->x
->next
->x
->previous
== symbolP
);
1049 gas_assert (lastP
== symbolP
);
1053 symbol_on_chain (symbolS
*s
, symbolS
*rootPP
, symbolS
*lastPP
)
1055 return (!s
->flags
.local_symbol
1056 && ((s
->x
->next
!= s
1057 && s
->x
->next
!= NULL
1058 && s
->x
->next
->x
->previous
== s
)
1060 && ((s
->x
->previous
!= s
1061 && s
->x
->previous
!= NULL
1062 && s
->x
->previous
->x
->next
== s
)
1066 #ifdef OBJ_COMPLEX_RELC
1069 use_complex_relocs_for (symbolS
* symp
)
1071 switch (symp
->x
->value
.X_op
)
1081 case O_bit_inclusive_or
:
1083 case O_bit_exclusive_or
:
1095 if ((S_IS_COMMON (symp
->x
->value
.X_op_symbol
)
1096 || S_IS_LOCAL (symp
->x
->value
.X_op_symbol
))
1097 && S_IS_DEFINED (symp
->x
->value
.X_op_symbol
)
1098 && S_GET_SEGMENT (symp
->x
->value
.X_op_symbol
) != expr_section
)
1105 if ((S_IS_COMMON (symp
->x
->value
.X_add_symbol
)
1106 || S_IS_LOCAL (symp
->x
->value
.X_add_symbol
))
1107 && S_IS_DEFINED (symp
->x
->value
.X_add_symbol
)
1108 && S_GET_SEGMENT (symp
->x
->value
.X_add_symbol
) != expr_section
)
1121 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
1125 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1126 segT seg_right
= S_GET_SEGMENT (right
);
1135 case O_uminus
: opname
= "-"; break;
1136 case O_bit_not
: opname
= "~"; break;
1137 case O_logical_not
: opname
= "!"; break;
1138 case O_multiply
: opname
= "*"; break;
1139 case O_divide
: opname
= "/"; break;
1140 case O_modulus
: opname
= "%"; break;
1141 case O_left_shift
: opname
= "<<"; break;
1142 case O_right_shift
: opname
= ">>"; break;
1143 case O_bit_inclusive_or
: opname
= "|"; break;
1144 case O_bit_or_not
: opname
= "|~"; break;
1145 case O_bit_exclusive_or
: opname
= "^"; break;
1146 case O_bit_and
: opname
= "&"; break;
1147 case O_add
: opname
= "+"; break;
1148 case O_subtract
: opname
= "-"; break;
1149 case O_eq
: opname
= "=="; break;
1150 case O_ne
: opname
= "!="; break;
1151 case O_lt
: opname
= "<"; break;
1152 case O_le
: opname
= "<="; break;
1153 case O_ge
: opname
= ">="; break;
1154 case O_gt
: opname
= ">"; break;
1155 case O_logical_and
: opname
= "&&"; break;
1156 case O_logical_or
: opname
= "||"; break;
1159 if (expr_symbol_where (symp
, &file
, &line
))
1162 as_bad_where (file
, line
,
1163 _("invalid operands (%s and %s sections) for `%s'"),
1164 seg_left
->name
, seg_right
->name
, opname
);
1166 as_bad_where (file
, line
,
1167 _("invalid operand (%s section) for `%s'"),
1168 seg_right
->name
, opname
);
1172 const char *sname
= S_GET_NAME (symp
);
1175 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1176 seg_left
->name
, seg_right
->name
, opname
, sname
);
1178 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1179 seg_right
->name
, opname
, sname
);
1183 /* Resolve the value of a symbol. This is called during the final
1184 pass over the symbol table to resolve any symbols with complex
1188 resolve_symbol_value (symbolS
*symp
)
1194 if (symp
->flags
.local_symbol
)
1196 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1198 final_val
= locsym
->value
;
1199 if (locsym
->flags
.resolved
)
1202 /* Symbols whose section has SEC_ELF_OCTETS set,
1203 resolve to octets instead of target bytes. */
1204 if (locsym
->section
->flags
& SEC_OCTETS
)
1205 final_val
+= locsym
->frag
->fr_address
;
1207 final_val
+= locsym
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1211 locsym
->value
= final_val
;
1212 locsym
->flags
.resolved
= 1;
1218 if (symp
->flags
.resolved
)
1221 while (symp
->x
->value
.X_op
== O_symbol
)
1223 final_val
+= symp
->x
->value
.X_add_number
;
1224 symp
= symp
->x
->value
.X_add_symbol
;
1225 if (symp
->flags
.local_symbol
)
1227 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1228 final_val
+= locsym
->value
;
1231 if (!symp
->flags
.resolved
)
1234 if (symp
->x
->value
.X_op
== O_constant
)
1235 final_val
+= symp
->x
->value
.X_add_number
;
1242 final_seg
= S_GET_SEGMENT (symp
);
1244 if (symp
->flags
.resolving
)
1247 as_bad (_("symbol definition loop encountered at `%s'"),
1252 #ifdef OBJ_COMPLEX_RELC
1253 else if (final_seg
== expr_section
1254 && use_complex_relocs_for (symp
))
1256 symbolS
* relc_symbol
= NULL
;
1257 char * relc_symbol_name
= NULL
;
1259 relc_symbol_name
= symbol_relc_make_expr (& symp
->x
->value
);
1261 /* For debugging, print out conversion input & output. */
1263 print_expr (& symp
->x
->value
);
1264 if (relc_symbol_name
)
1265 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1268 if (relc_symbol_name
!= NULL
)
1269 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1270 &zero_address_frag
, 0);
1272 if (relc_symbol
== NULL
)
1274 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1280 symbol_table_insert (relc_symbol
);
1282 /* S_CLEAR_EXTERNAL (relc_symbol); */
1283 if (symp
->bsym
->flags
& BSF_SRELC
)
1284 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1286 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1287 /* symp->bsym->flags |= BSF_RELC; */
1288 copy_symbol_attributes (symp
, relc_symbol
);
1289 symp
->x
->value
.X_op
= O_symbol
;
1290 symp
->x
->value
.X_add_symbol
= relc_symbol
;
1291 symp
->x
->value
.X_add_number
= 0;
1296 final_seg
= undefined_section
;
1297 goto exit_dont_set_value
;
1302 symbolS
*add_symbol
, *op_symbol
;
1303 offsetT left
, right
;
1304 segT seg_left
, seg_right
;
1308 symp
->flags
.resolving
= 1;
1310 /* Help out with CSE. */
1311 add_symbol
= symp
->x
->value
.X_add_symbol
;
1312 op_symbol
= symp
->x
->value
.X_op_symbol
;
1313 final_val
= symp
->x
->value
.X_add_number
;
1314 op
= symp
->x
->value
.X_op
;
1327 /* Symbols whose section has SEC_ELF_OCTETS set,
1328 resolve to octets instead of target bytes. */
1329 if (symp
->bsym
->section
->flags
& SEC_OCTETS
)
1330 final_val
+= symp
->frag
->fr_address
;
1332 final_val
+= symp
->frag
->fr_address
/ OCTETS_PER_BYTE
;
1333 if (final_seg
== expr_section
)
1334 final_seg
= absolute_section
;
1343 left
= resolve_symbol_value (add_symbol
);
1344 seg_left
= S_GET_SEGMENT (add_symbol
);
1346 symp
->x
->value
.X_op_symbol
= NULL
;
1349 if (S_IS_WEAKREFR (symp
))
1351 gas_assert (final_val
== 0);
1352 if (S_IS_WEAKREFR (add_symbol
))
1354 gas_assert (add_symbol
->x
->value
.X_op
== O_symbol
1355 && add_symbol
->x
->value
.X_add_number
== 0);
1356 add_symbol
= add_symbol
->x
->value
.X_add_symbol
;
1357 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1358 symp
->x
->value
.X_add_symbol
= add_symbol
;
1362 if (symp
->flags
.mri_common
)
1364 /* This is a symbol inside an MRI common section. The
1365 relocation routines are going to handle it specially.
1366 Don't change the value. */
1367 resolved
= symbol_resolved_p (add_symbol
);
1371 /* Don't leave symbol loops. */
1373 && !add_symbol
->flags
.local_symbol
1374 && add_symbol
->flags
.resolving
)
1377 if (finalize_syms
&& final_val
== 0)
1379 if (add_symbol
->flags
.local_symbol
)
1380 add_symbol
= local_symbol_convert (add_symbol
);
1381 copy_symbol_attributes (symp
, add_symbol
);
1384 /* If we have equated this symbol to an undefined or common
1385 symbol, keep X_op set to O_symbol, and don't change
1386 X_add_number. This permits the routine which writes out
1387 relocation to detect this case, and convert the
1388 relocation to be against the symbol to which this symbol
1390 if (seg_left
== undefined_section
1391 || bfd_is_com_section (seg_left
)
1392 #if defined (OBJ_COFF) && defined (TE_PE)
1393 || S_IS_WEAK (add_symbol
)
1396 && ((final_seg
== expr_section
1397 && seg_left
!= expr_section
1398 && seg_left
!= absolute_section
)
1399 || symbol_shadow_p (symp
))))
1403 symp
->x
->value
.X_op
= O_symbol
;
1404 symp
->x
->value
.X_add_symbol
= add_symbol
;
1405 symp
->x
->value
.X_add_number
= final_val
;
1406 /* Use X_op_symbol as a flag. */
1407 symp
->x
->value
.X_op_symbol
= add_symbol
;
1409 final_seg
= seg_left
;
1410 final_val
+= symp
->frag
->fr_address
+ left
;
1411 resolved
= symbol_resolved_p (add_symbol
);
1412 symp
->flags
.resolving
= 0;
1413 goto exit_dont_set_value
;
1417 final_val
+= symp
->frag
->fr_address
+ left
;
1418 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1419 final_seg
= seg_left
;
1422 resolved
= symbol_resolved_p (add_symbol
);
1423 if (S_IS_WEAKREFR (symp
))
1425 symp
->flags
.resolving
= 0;
1426 goto exit_dont_set_value
;
1433 left
= resolve_symbol_value (add_symbol
);
1434 seg_left
= S_GET_SEGMENT (add_symbol
);
1436 /* By reducing these to the relevant dyadic operator, we get
1437 !S -> S == 0 permitted on anything,
1438 -S -> 0 - S only permitted on absolute
1439 ~S -> S ^ ~0 only permitted on absolute */
1440 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1442 report_op_error (symp
, NULL
, op
, add_symbol
);
1444 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1445 final_seg
= absolute_section
;
1449 else if (op
== O_logical_not
)
1454 final_val
+= left
+ symp
->frag
->fr_address
;
1456 resolved
= symbol_resolved_p (add_symbol
);
1464 case O_bit_inclusive_or
:
1466 case O_bit_exclusive_or
:
1478 left
= resolve_symbol_value (add_symbol
);
1479 right
= resolve_symbol_value (op_symbol
);
1480 seg_left
= S_GET_SEGMENT (add_symbol
);
1481 seg_right
= S_GET_SEGMENT (op_symbol
);
1483 /* Simplify addition or subtraction of a constant by folding the
1484 constant into X_add_number. */
1487 if (seg_right
== absolute_section
)
1492 else if (seg_left
== absolute_section
)
1495 add_symbol
= op_symbol
;
1497 seg_left
= seg_right
;
1501 else if (op
== O_subtract
)
1503 if (seg_right
== absolute_section
)
1511 /* Equality and non-equality tests are permitted on anything.
1512 Subtraction, and other comparison operators are permitted if
1513 both operands are in the same section. Otherwise, both
1514 operands must be absolute. We already handled the case of
1515 addition or subtraction of a constant above. This will
1516 probably need to be changed for an object file format which
1517 supports arbitrary expressions. */
1518 if (!(seg_left
== absolute_section
1519 && seg_right
== absolute_section
)
1520 && !(op
== O_eq
|| op
== O_ne
)
1521 && !((op
== O_subtract
1522 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1523 && seg_left
== seg_right
1524 && (seg_left
!= undefined_section
1525 || add_symbol
== op_symbol
)))
1527 /* Don't emit messages unless we're finalizing the symbol value,
1528 otherwise we may get the same message multiple times. */
1530 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1531 /* However do not move the symbol into the absolute section
1532 if it cannot currently be resolved - this would confuse
1533 other parts of the assembler into believing that the
1534 expression had been evaluated to zero. */
1540 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1541 final_seg
= absolute_section
;
1543 /* Check for division by zero. */
1544 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1546 /* If seg_right is not absolute_section, then we've
1547 already issued a warning about using a bad symbol. */
1548 if (seg_right
== absolute_section
&& finalize_syms
)
1553 if (expr_symbol_where (symp
, &file
, &line
))
1554 as_bad_where (file
, line
, _("division by zero"));
1556 as_bad (_("division by zero when setting `%s'"),
1563 switch (symp
->x
->value
.X_op
)
1565 case O_multiply
: left
*= right
; break;
1566 case O_divide
: left
/= right
; break;
1567 case O_modulus
: left
%= right
; break;
1568 case O_left_shift
: left
<<= right
; break;
1569 case O_right_shift
: left
>>= right
; break;
1570 case O_bit_inclusive_or
: left
|= right
; break;
1571 case O_bit_or_not
: left
|= ~right
; break;
1572 case O_bit_exclusive_or
: left
^= right
; break;
1573 case O_bit_and
: left
&= right
; break;
1574 case O_add
: left
+= right
; break;
1575 case O_subtract
: left
-= right
; break;
1578 left
= (left
== right
&& seg_left
== seg_right
1579 && (seg_left
!= undefined_section
1580 || add_symbol
== op_symbol
)
1581 ? ~ (offsetT
) 0 : 0);
1582 if (symp
->x
->value
.X_op
== O_ne
)
1585 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1586 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1587 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1588 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1589 case O_logical_and
: left
= left
&& right
; break;
1590 case O_logical_or
: left
= left
|| right
; break;
1595 /* See PR 20895 for a reproducer. */
1596 as_bad (_("Invalid operation on symbol"));
1597 goto exit_dont_set_value
;
1603 final_val
+= symp
->frag
->fr_address
+ left
;
1604 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1606 if (seg_left
== undefined_section
1607 || seg_right
== undefined_section
)
1608 final_seg
= undefined_section
;
1609 else if (seg_left
== absolute_section
)
1610 final_seg
= seg_right
;
1612 final_seg
= seg_left
;
1614 resolved
= (symbol_resolved_p (add_symbol
)
1615 && symbol_resolved_p (op_symbol
));
1620 /* Give an error (below) if not in expr_section. We don't
1621 want to worry about expr_section symbols, because they
1622 are fictional (they are created as part of expression
1623 resolution), and any problems may not actually mean
1628 symp
->flags
.resolving
= 0;
1632 S_SET_VALUE (symp
, final_val
);
1634 exit_dont_set_value
:
1635 /* Always set the segment, even if not finalizing the value.
1636 The segment is used to determine whether a symbol is defined. */
1637 S_SET_SEGMENT (symp
, final_seg
);
1639 /* Don't worry if we can't resolve an expr_section symbol. */
1643 symp
->flags
.resolved
= 1;
1644 else if (S_GET_SEGMENT (symp
) != expr_section
)
1646 as_bad (_("can't resolve value for symbol `%s'"),
1648 symp
->flags
.resolved
= 1;
1655 /* A static function passed to hash_traverse. */
1658 resolve_local_symbol (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1660 symbol_entry_t
*entry
= *((symbol_entry_t
**) slot
);
1661 if (entry
->sy
.flags
.local_symbol
)
1662 resolve_symbol_value (&entry
->sy
);
1667 /* Resolve all local symbols. */
1670 resolve_local_symbol_values (void)
1672 htab_traverse (sy_hash
, resolve_local_symbol
, NULL
);
1675 /* Obtain the current value of a symbol without changing any
1676 sub-expressions used. */
1679 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1681 symbolS
*symbolP
= *symbolPP
;
1683 if (symbolP
->flags
.local_symbol
)
1685 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1687 *valueP
= locsym
->value
;
1688 *segP
= locsym
->section
;
1689 *fragPP
= locsym
->frag
;
1693 expressionS exp
= symbolP
->x
->value
;
1695 if (!symbolP
->flags
.resolved
&& exp
.X_op
!= O_illegal
)
1699 if (symbolP
->flags
.resolving
)
1701 symbolP
->flags
.resolving
= 1;
1702 resolved
= resolve_expression (&exp
);
1703 symbolP
->flags
.resolving
= 0;
1711 if (!symbol_equated_p (symbolP
))
1716 symbolP
= exp
.X_add_symbol
;
1723 *symbolPP
= symbolP
;
1725 /* A bogus input file can result in resolve_expression()
1726 generating a local symbol, so we have to check again. */
1727 if (symbolP
->flags
.local_symbol
)
1729 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1731 *valueP
= locsym
->value
;
1732 *segP
= locsym
->section
;
1733 *fragPP
= locsym
->frag
;
1737 *valueP
= exp
.X_add_number
;
1738 *segP
= symbolP
->bsym
->section
;
1739 *fragPP
= symbolP
->frag
;
1742 if (*segP
== expr_section
)
1745 case O_constant
: *segP
= absolute_section
; break;
1746 case O_register
: *segP
= reg_section
; break;
1754 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1755 They are *really* local. That is, they go out of scope whenever we see a
1756 label that isn't local. Also, like fb labels, there can be multiple
1757 instances of a dollar label. Therefor, we name encode each instance with
1758 the instance number, keep a list of defined symbols separate from the real
1759 symbol table, and we treat these buggers as a sparse array. */
1761 static long *dollar_labels
;
1762 static long *dollar_label_instances
;
1763 static char *dollar_label_defines
;
1764 static size_t dollar_label_count
;
1765 static size_t dollar_label_max
;
1768 dollar_label_defined (long label
)
1772 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1774 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1776 return dollar_label_defines
[i
- dollar_labels
];
1778 /* If we get here, label isn't defined. */
1783 dollar_label_instance (long label
)
1787 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1789 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1791 return (dollar_label_instances
[i
- dollar_labels
]);
1793 /* If we get here, we haven't seen the label before.
1794 Therefore its instance count is zero. */
1799 dollar_label_clear (void)
1801 if (dollar_label_count
)
1802 memset (dollar_label_defines
, '\0', dollar_label_count
);
1805 #define DOLLAR_LABEL_BUMP_BY 10
1808 define_dollar_label (long label
)
1812 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1815 ++dollar_label_instances
[i
- dollar_labels
];
1816 dollar_label_defines
[i
- dollar_labels
] = 1;
1820 /* If we get to here, we don't have label listed yet. */
1822 if (dollar_labels
== NULL
)
1824 dollar_labels
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1825 dollar_label_instances
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1826 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1827 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1828 dollar_label_count
= 0;
1830 else if (dollar_label_count
== dollar_label_max
)
1832 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1833 dollar_labels
= XRESIZEVEC (long, dollar_labels
, dollar_label_max
);
1834 dollar_label_instances
= XRESIZEVEC (long, dollar_label_instances
,
1836 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1838 } /* if we needed to grow */
1840 dollar_labels
[dollar_label_count
] = label
;
1841 dollar_label_instances
[dollar_label_count
] = 1;
1842 dollar_label_defines
[dollar_label_count
] = 1;
1843 ++dollar_label_count
;
1846 /* Caller must copy returned name: we re-use the area for the next name.
1848 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1849 where n is the label number and m is the instance number. "L" makes
1850 it a label discarded unless debugging and "^A"('\1') ensures no
1851 ordinary symbol SHOULD get the same name as a local label
1852 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1854 fb labels get the same treatment, except that ^B is used in place
1857 char * /* Return local label name. */
1858 dollar_label_name (long n
, /* we just saw "n$:" : n a number. */
1859 int augend
/* 0 for current instance, 1 for new instance. */)
1862 /* Returned to caller, then copied. Used for created names ("4f"). */
1863 static char symbol_name_build
[24];
1866 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1869 know (augend
== 0 || augend
== 1);
1870 p
= symbol_name_build
;
1871 #ifdef LOCAL_LABEL_PREFIX
1872 *p
++ = LOCAL_LABEL_PREFIX
;
1876 /* Next code just does sprintf( {}, "%d", n); */
1878 q
= symbol_name_temporary
;
1879 for (*q
++ = 0, i
= n
; i
; ++q
)
1884 while ((*p
= *--q
) != '\0')
1887 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1889 /* Instance number. */
1890 q
= symbol_name_temporary
;
1891 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1896 while ((*p
++ = *--q
) != '\0');
1898 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1899 return symbol_name_build
;
1902 /* Somebody else's idea of local labels. They are made by "n:" where n
1903 is any decimal digit. Refer to them with
1904 "nb" for previous (backward) n:
1905 or "nf" for next (forward) n:.
1907 We do a little better and let n be any number, not just a single digit, but
1908 since the other guy's assembler only does ten, we treat the first ten
1911 Like someone else's assembler, we have one set of local label counters for
1912 entire assembly, not one set per (sub)segment like in most assemblers. This
1913 implies that one can refer to a label in another segment, and indeed some
1914 crufty compilers have done just that.
1916 Since there could be a LOT of these things, treat them as a sparse
1919 #define FB_LABEL_SPECIAL (10)
1921 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1922 static long *fb_labels
;
1923 static long *fb_label_instances
;
1924 static long fb_label_count
;
1925 static long fb_label_max
;
1927 /* This must be more than FB_LABEL_SPECIAL. */
1928 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1931 fb_label_init (void)
1933 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1936 /* Add one to the instance number of this fb label. */
1939 fb_label_instance_inc (long label
)
1943 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1945 ++fb_low_counter
[label
];
1949 if (fb_labels
!= NULL
)
1951 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1952 i
< fb_labels
+ fb_label_count
; ++i
)
1956 ++fb_label_instances
[i
- fb_labels
];
1958 } /* if we find it */
1959 } /* for each existing label */
1962 /* If we get to here, we don't have label listed yet. */
1964 if (fb_labels
== NULL
)
1966 fb_labels
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1967 fb_label_instances
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
1968 fb_label_max
= FB_LABEL_BUMP_BY
;
1969 fb_label_count
= FB_LABEL_SPECIAL
;
1972 else if (fb_label_count
== fb_label_max
)
1974 fb_label_max
+= FB_LABEL_BUMP_BY
;
1975 fb_labels
= XRESIZEVEC (long, fb_labels
, fb_label_max
);
1976 fb_label_instances
= XRESIZEVEC (long, fb_label_instances
, fb_label_max
);
1977 } /* if we needed to grow */
1979 fb_labels
[fb_label_count
] = label
;
1980 fb_label_instances
[fb_label_count
] = 1;
1985 fb_label_instance (long label
)
1989 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
1991 return (fb_low_counter
[label
]);
1994 if (fb_labels
!= NULL
)
1996 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1997 i
< fb_labels
+ fb_label_count
; ++i
)
2001 return (fb_label_instances
[i
- fb_labels
]);
2002 } /* if we find it */
2003 } /* for each existing label */
2006 /* We didn't find the label, so this must be a reference to the
2011 /* Caller must copy returned name: we re-use the area for the next name.
2013 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2014 where n is the label number and m is the instance number. "L" makes
2015 it a label discarded unless debugging and "^B"('\2') ensures no
2016 ordinary symbol SHOULD get the same name as a local label
2017 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2019 dollar labels get the same treatment, except that ^A is used in
2022 char * /* Return local label name. */
2023 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
2024 long augend
/* 0 for nb, 1 for n:, nf. */)
2027 /* Returned to caller, then copied. Used for created names ("4f"). */
2028 static char symbol_name_build
[24];
2031 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
2035 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
2037 know ((unsigned long) augend
<= 1);
2039 p
= symbol_name_build
;
2040 #ifdef LOCAL_LABEL_PREFIX
2041 *p
++ = LOCAL_LABEL_PREFIX
;
2045 /* Next code just does sprintf( {}, "%d", n); */
2047 q
= symbol_name_temporary
;
2048 for (*q
++ = 0, i
= n
; i
; ++q
)
2053 while ((*p
= *--q
) != '\0')
2056 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
2058 /* Instance number. */
2059 q
= symbol_name_temporary
;
2060 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
2065 while ((*p
++ = *--q
) != '\0');
2067 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2068 return (symbol_name_build
);
2071 /* Decode name that may have been generated by foo_label_name() above.
2072 If the name wasn't generated by foo_label_name(), then return it
2073 unaltered. This is used for error messages. */
2076 decode_local_label_name (char *s
)
2079 char *symbol_decode
;
2081 int instance_number
;
2083 const char *message_format
;
2086 #ifdef LOCAL_LABEL_PREFIX
2087 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
2091 if (s
[lindex
] != 'L')
2094 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
2095 label_number
= (10 * label_number
) + *p
- '0';
2097 if (*p
== DOLLAR_LABEL_CHAR
)
2099 else if (*p
== LOCAL_LABEL_CHAR
)
2104 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
2105 instance_number
= (10 * instance_number
) + *p
- '0';
2107 message_format
= _("\"%d\" (instance number %d of a %s label)");
2108 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
2109 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
2111 return symbol_decode
;
2114 /* Get the value of a symbol. */
2117 S_GET_VALUE (symbolS
*s
)
2119 if (s
->flags
.local_symbol
)
2120 return resolve_symbol_value (s
);
2122 if (!s
->flags
.resolved
)
2124 valueT val
= resolve_symbol_value (s
);
2128 if (S_IS_WEAKREFR (s
))
2129 return S_GET_VALUE (s
->x
->value
.X_add_symbol
);
2131 if (s
->x
->value
.X_op
!= O_constant
)
2133 if (! s
->flags
.resolved
2134 || s
->x
->value
.X_op
!= O_symbol
2135 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
2136 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2139 return (valueT
) s
->x
->value
.X_add_number
;
2142 /* Set the value of a symbol. */
2145 S_SET_VALUE (symbolS
*s
, valueT val
)
2147 if (s
->flags
.local_symbol
)
2149 ((struct local_symbol
*) s
)->value
= val
;
2153 s
->x
->value
.X_op
= O_constant
;
2154 s
->x
->value
.X_add_number
= (offsetT
) val
;
2155 s
->x
->value
.X_unsigned
= 0;
2156 S_CLEAR_WEAKREFR (s
);
2160 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2162 if (dest
->flags
.local_symbol
)
2163 dest
= local_symbol_convert (dest
);
2164 if (src
->flags
.local_symbol
)
2165 src
= local_symbol_convert (src
);
2167 /* In an expression, transfer the settings of these flags.
2168 The user can override later, of course. */
2169 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2170 | BSF_GNU_INDIRECT_FUNCTION)
2171 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2173 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2174 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2177 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2178 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2183 S_IS_FUNCTION (symbolS
*s
)
2187 if (s
->flags
.local_symbol
)
2190 flags
= s
->bsym
->flags
;
2192 return (flags
& BSF_FUNCTION
) != 0;
2196 S_IS_EXTERNAL (symbolS
*s
)
2200 if (s
->flags
.local_symbol
)
2203 flags
= s
->bsym
->flags
;
2206 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2209 return (flags
& BSF_GLOBAL
) != 0;
2213 S_IS_WEAK (symbolS
*s
)
2215 if (s
->flags
.local_symbol
)
2217 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2218 could probably handle a WEAKREFR as always weak though. E.g., if
2219 the referenced symbol has lost its weak status, there's no reason
2220 to keep handling the weakrefr as if it was weak. */
2221 if (S_IS_WEAKREFR (s
))
2222 return S_IS_WEAK (s
->x
->value
.X_add_symbol
);
2223 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2227 S_IS_WEAKREFR (symbolS
*s
)
2229 if (s
->flags
.local_symbol
)
2231 return s
->flags
.weakrefr
!= 0;
2235 S_IS_WEAKREFD (symbolS
*s
)
2237 if (s
->flags
.local_symbol
)
2239 return s
->flags
.weakrefd
!= 0;
2243 S_IS_COMMON (symbolS
*s
)
2245 if (s
->flags
.local_symbol
)
2247 return bfd_is_com_section (s
->bsym
->section
);
2251 S_IS_DEFINED (symbolS
*s
)
2253 if (s
->flags
.local_symbol
)
2254 return ((struct local_symbol
*) s
)->section
!= undefined_section
;
2255 return s
->bsym
->section
!= undefined_section
;
2259 #ifndef EXTERN_FORCE_RELOC
2260 #define EXTERN_FORCE_RELOC IS_ELF
2263 /* Return true for symbols that should not be reduced to section
2264 symbols or eliminated from expressions, because they may be
2265 overridden by the linker. */
2267 S_FORCE_RELOC (symbolS
*s
, int strict
)
2270 if (s
->flags
.local_symbol
)
2271 sec
= ((struct local_symbol
*) s
)->section
;
2275 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2276 || (EXTERN_FORCE_RELOC
2277 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2278 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2280 sec
= s
->bsym
->section
;
2282 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2286 S_IS_DEBUG (symbolS
*s
)
2288 if (s
->flags
.local_symbol
)
2290 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2296 S_IS_LOCAL (symbolS
*s
)
2301 if (s
->flags
.local_symbol
)
2304 flags
= s
->bsym
->flags
;
2307 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2310 if (bfd_asymbol_section (s
->bsym
) == reg_section
)
2313 if (flag_strip_local_absolute
2314 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2315 the source file even when the object file is stripped. */
2316 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2317 && bfd_asymbol_section (s
->bsym
) == absolute_section
)
2320 name
= S_GET_NAME (s
);
2321 return (name
!= NULL
2323 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2324 || strchr (name
, LOCAL_LABEL_CHAR
)
2325 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2326 || strchr (name
, FAKE_LABEL_CHAR
)
2328 || TC_LABEL_IS_LOCAL (name
)
2329 || (! flag_keep_locals
2330 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2333 && name
[1] == '?')))));
2337 S_IS_STABD (symbolS
*s
)
2339 return S_GET_NAME (s
) == 0;
2343 S_CAN_BE_REDEFINED (const symbolS
*s
)
2345 if (s
->flags
.local_symbol
)
2346 return (((struct local_symbol
*) s
)->frag
2347 == &predefined_address_frag
);
2348 /* Permit register names to be redefined. */
2349 return s
->bsym
->section
== reg_section
;
2353 S_IS_VOLATILE (const symbolS
*s
)
2355 if (s
->flags
.local_symbol
)
2357 return s
->flags
.volatil
;
2361 S_IS_FORWARD_REF (const symbolS
*s
)
2363 if (s
->flags
.local_symbol
)
2365 return s
->flags
.forward_ref
;
2369 S_GET_NAME (symbolS
*s
)
2375 S_GET_SEGMENT (symbolS
*s
)
2377 if (s
->flags
.local_symbol
)
2378 return ((struct local_symbol
*) s
)->section
;
2379 return s
->bsym
->section
;
2383 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2385 if (s
->flags
.local_symbol
)
2387 ((struct local_symbol
*) s
)->section
= seg
;
2391 /* Don't reassign section symbols. The direct reason is to prevent seg
2392 faults assigning back to const global symbols such as *ABS*, but it
2393 shouldn't happen anyway. */
2394 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2396 if (s
->bsym
->section
!= seg
)
2400 s
->bsym
->section
= seg
;
2404 S_SET_EXTERNAL (symbolS
*s
)
2406 if (s
->flags
.local_symbol
)
2407 s
= local_symbol_convert (s
);
2408 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2410 /* Let .weak override .global. */
2413 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2415 /* Do not reassign section symbols. */
2416 as_warn (_("section symbols are already global"));
2419 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2420 if (S_GET_SEGMENT (s
) == reg_section
)
2422 as_bad ("can't make register symbol `%s' global",
2427 s
->bsym
->flags
|= BSF_GLOBAL
;
2428 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2431 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2432 an_external_name
= S_GET_NAME (s
);
2437 S_CLEAR_EXTERNAL (symbolS
*s
)
2439 if (s
->flags
.local_symbol
)
2441 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2443 /* Let .weak override. */
2446 s
->bsym
->flags
|= BSF_LOCAL
;
2447 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2451 S_SET_WEAK (symbolS
*s
)
2453 if (s
->flags
.local_symbol
)
2454 s
= local_symbol_convert (s
);
2455 #ifdef obj_set_weak_hook
2456 obj_set_weak_hook (s
);
2458 s
->bsym
->flags
|= BSF_WEAK
;
2459 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2463 S_SET_WEAKREFR (symbolS
*s
)
2465 if (s
->flags
.local_symbol
)
2466 s
= local_symbol_convert (s
);
2467 s
->flags
.weakrefr
= 1;
2468 /* If the alias was already used, make sure we mark the target as
2469 used as well, otherwise it might be dropped from the symbol
2470 table. This may have unintended side effects if the alias is
2471 later redirected to another symbol, such as keeping the unused
2472 previous target in the symbol table. Since it will be weak, it's
2475 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2479 S_CLEAR_WEAKREFR (symbolS
*s
)
2481 if (s
->flags
.local_symbol
)
2483 s
->flags
.weakrefr
= 0;
2487 S_SET_WEAKREFD (symbolS
*s
)
2489 if (s
->flags
.local_symbol
)
2490 s
= local_symbol_convert (s
);
2491 s
->flags
.weakrefd
= 1;
2496 S_CLEAR_WEAKREFD (symbolS
*s
)
2498 if (s
->flags
.local_symbol
)
2500 if (s
->flags
.weakrefd
)
2502 s
->flags
.weakrefd
= 0;
2503 /* If a weakref target symbol is weak, then it was never
2504 referenced directly before, not even in a .global directive,
2505 so decay it to local. If it remains undefined, it will be
2506 later turned into a global, like any other undefined
2508 if (s
->bsym
->flags
& BSF_WEAK
)
2510 #ifdef obj_clear_weak_hook
2511 obj_clear_weak_hook (s
);
2513 s
->bsym
->flags
&= ~BSF_WEAK
;
2514 s
->bsym
->flags
|= BSF_LOCAL
;
2520 S_SET_THREAD_LOCAL (symbolS
*s
)
2522 if (s
->flags
.local_symbol
)
2523 s
= local_symbol_convert (s
);
2524 if (bfd_is_com_section (s
->bsym
->section
)
2525 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2527 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2528 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2529 as_bad (_("Accessing function `%s' as thread-local object"),
2531 else if (! bfd_is_und_section (s
->bsym
->section
)
2532 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2533 as_bad (_("Accessing `%s' as thread-local object"),
2538 S_SET_NAME (symbolS
*s
, const char *name
)
2541 if (s
->flags
.local_symbol
)
2543 s
->bsym
->name
= name
;
2547 S_SET_VOLATILE (symbolS
*s
)
2549 if (s
->flags
.local_symbol
)
2550 s
= local_symbol_convert (s
);
2551 s
->flags
.volatil
= 1;
2555 S_CLEAR_VOLATILE (symbolS
*s
)
2557 if (!s
->flags
.local_symbol
)
2558 s
->flags
.volatil
= 0;
2562 S_SET_FORWARD_REF (symbolS
*s
)
2564 if (s
->flags
.local_symbol
)
2565 s
= local_symbol_convert (s
);
2566 s
->flags
.forward_ref
= 1;
2569 /* Return the previous symbol in a chain. */
2572 symbol_previous (symbolS
*s
)
2574 if (s
->flags
.local_symbol
)
2576 return s
->x
->previous
;
2579 /* Return the next symbol in a chain. */
2582 symbol_next (symbolS
*s
)
2584 if (s
->flags
.local_symbol
)
2589 /* Return a pointer to the value of a symbol as an expression. */
2592 symbol_get_value_expression (symbolS
*s
)
2594 if (s
->flags
.local_symbol
)
2595 s
= local_symbol_convert (s
);
2596 return &s
->x
->value
;
2599 /* Set the value of a symbol to an expression. */
2602 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2604 if (s
->flags
.local_symbol
)
2605 s
= local_symbol_convert (s
);
2607 S_CLEAR_WEAKREFR (s
);
2610 /* Return whether 2 symbols are the same. */
2613 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2618 /* Return a pointer to the X_add_number component of a symbol. */
2621 symbol_X_add_number (symbolS
*s
)
2623 if (s
->flags
.local_symbol
)
2624 return (offsetT
*) &((struct local_symbol
*) s
)->value
;
2626 return &s
->x
->value
.X_add_number
;
2629 /* Set the value of SYM to the current position in the current segment. */
2632 symbol_set_value_now (symbolS
*sym
)
2634 S_SET_SEGMENT (sym
, now_seg
);
2635 S_SET_VALUE (sym
, frag_now_fix ());
2636 symbol_set_frag (sym
, frag_now
);
2639 /* Set the frag of a symbol. */
2642 symbol_set_frag (symbolS
*s
, fragS
*f
)
2644 if (s
->flags
.local_symbol
)
2646 ((struct local_symbol
*) s
)->frag
= f
;
2650 S_CLEAR_WEAKREFR (s
);
2653 /* Return the frag of a symbol. */
2656 symbol_get_frag (symbolS
*s
)
2658 if (s
->flags
.local_symbol
)
2659 return ((struct local_symbol
*) s
)->frag
;
2663 /* Mark a symbol as having been used. */
2666 symbol_mark_used (symbolS
*s
)
2668 if (s
->flags
.local_symbol
)
2671 if (S_IS_WEAKREFR (s
))
2672 symbol_mark_used (s
->x
->value
.X_add_symbol
);
2675 /* Clear the mark of whether a symbol has been used. */
2678 symbol_clear_used (symbolS
*s
)
2680 if (s
->flags
.local_symbol
)
2681 s
= local_symbol_convert (s
);
2685 /* Return whether a symbol has been used. */
2688 symbol_used_p (symbolS
*s
)
2690 if (s
->flags
.local_symbol
)
2692 return s
->flags
.used
;
2695 /* Mark a symbol as having been used in a reloc. */
2698 symbol_mark_used_in_reloc (symbolS
*s
)
2700 if (s
->flags
.local_symbol
)
2701 s
= local_symbol_convert (s
);
2702 s
->flags
.used_in_reloc
= 1;
2705 /* Clear the mark of whether a symbol has been used in a reloc. */
2708 symbol_clear_used_in_reloc (symbolS
*s
)
2710 if (s
->flags
.local_symbol
)
2712 s
->flags
.used_in_reloc
= 0;
2715 /* Return whether a symbol has been used in a reloc. */
2718 symbol_used_in_reloc_p (symbolS
*s
)
2720 if (s
->flags
.local_symbol
)
2722 return s
->flags
.used_in_reloc
;
2725 /* Mark a symbol as an MRI common symbol. */
2728 symbol_mark_mri_common (symbolS
*s
)
2730 if (s
->flags
.local_symbol
)
2731 s
= local_symbol_convert (s
);
2732 s
->flags
.mri_common
= 1;
2735 /* Clear the mark of whether a symbol is an MRI common symbol. */
2738 symbol_clear_mri_common (symbolS
*s
)
2740 if (s
->flags
.local_symbol
)
2742 s
->flags
.mri_common
= 0;
2745 /* Return whether a symbol is an MRI common symbol. */
2748 symbol_mri_common_p (symbolS
*s
)
2750 if (s
->flags
.local_symbol
)
2752 return s
->flags
.mri_common
;
2755 /* Mark a symbol as having been written. */
2758 symbol_mark_written (symbolS
*s
)
2760 if (s
->flags
.local_symbol
)
2762 s
->flags
.written
= 1;
2765 /* Clear the mark of whether a symbol has been written. */
2768 symbol_clear_written (symbolS
*s
)
2770 if (s
->flags
.local_symbol
)
2772 s
->flags
.written
= 0;
2775 /* Return whether a symbol has been written. */
2778 symbol_written_p (symbolS
*s
)
2780 if (s
->flags
.local_symbol
)
2782 return s
->flags
.written
;
2785 /* Mark a symbol has having been resolved. */
2788 symbol_mark_resolved (symbolS
*s
)
2790 s
->flags
.resolved
= 1;
2793 /* Return whether a symbol has been resolved. */
2796 symbol_resolved_p (symbolS
*s
)
2798 return s
->flags
.resolved
;
2801 /* Return whether a symbol is a section symbol. */
2804 symbol_section_p (symbolS
*s
)
2806 if (s
->flags
.local_symbol
)
2808 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2811 /* Return whether a symbol is equated to another symbol. */
2814 symbol_equated_p (symbolS
*s
)
2816 if (s
->flags
.local_symbol
)
2818 return s
->x
->value
.X_op
== O_symbol
;
2821 /* Return whether a symbol is equated to another symbol, and should be
2822 treated specially when writing out relocs. */
2825 symbol_equated_reloc_p (symbolS
*s
)
2827 if (s
->flags
.local_symbol
)
2829 /* X_op_symbol, normally not used for O_symbol, is set by
2830 resolve_symbol_value to flag expression syms that have been
2832 return (s
->x
->value
.X_op
== O_symbol
2833 #if defined (OBJ_COFF) && defined (TE_PE)
2836 && ((s
->flags
.resolved
&& s
->x
->value
.X_op_symbol
!= NULL
)
2837 || ! S_IS_DEFINED (s
)
2838 || S_IS_COMMON (s
)));
2841 /* Return whether a symbol has a constant value. */
2844 symbol_constant_p (symbolS
*s
)
2846 if (s
->flags
.local_symbol
)
2848 return s
->x
->value
.X_op
== O_constant
;
2851 /* Return whether a symbol was cloned and thus removed from the global
2855 symbol_shadow_p (symbolS
*s
)
2857 if (s
->flags
.local_symbol
)
2859 return s
->x
->next
== s
;
2862 /* If S is a struct symbol return S, otherwise return NULL. */
2865 symbol_symbolS (symbolS
*s
)
2867 if (s
->flags
.local_symbol
)
2872 /* Return the BFD symbol for a symbol. */
2875 symbol_get_bfdsym (symbolS
*s
)
2877 if (s
->flags
.local_symbol
)
2878 s
= local_symbol_convert (s
);
2882 /* Set the BFD symbol for a symbol. */
2885 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2887 if (s
->flags
.local_symbol
)
2888 s
= local_symbol_convert (s
);
2889 /* Usually, it is harmless to reset a symbol to a BFD section
2890 symbol. For example, obj_elf_change_section sets the BFD symbol
2891 of an old symbol with the newly created section symbol. But when
2892 we have multiple sections with the same name, the newly created
2893 section may have the same name as an old section. We check if the
2894 old symbol has been already marked as a section symbol before
2896 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2898 /* else XXX - What do we do now ? */
2901 #ifdef OBJ_SYMFIELD_TYPE
2903 /* Get a pointer to the object format information for a symbol. */
2906 symbol_get_obj (symbolS
*s
)
2908 if (s
->flags
.local_symbol
)
2909 s
= local_symbol_convert (s
);
2913 /* Set the object format information for a symbol. */
2916 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2918 if (s
->flags
.local_symbol
)
2919 s
= local_symbol_convert (s
);
2923 #endif /* OBJ_SYMFIELD_TYPE */
2925 #ifdef TC_SYMFIELD_TYPE
2927 /* Get a pointer to the processor information for a symbol. */
2930 symbol_get_tc (symbolS
*s
)
2932 if (s
->flags
.local_symbol
)
2933 s
= local_symbol_convert (s
);
2937 /* Set the processor information for a symbol. */
2940 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2942 if (s
->flags
.local_symbol
)
2943 s
= local_symbol_convert (s
);
2947 #endif /* TC_SYMFIELD_TYPE */
2952 symbol_lastP
= NULL
;
2953 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2954 sy_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
2955 NULL
, xcalloc
, free
);
2957 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2958 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
2960 abs_symbol
.x
= &abs_symbol_x
;
2961 abs_symbol
.x
->value
.X_op
= O_constant
;
2962 abs_symbol
.frag
= &zero_address_frag
;
2964 if (LOCAL_LABELS_FB
)
2969 dot_symbol_init (void)
2971 dot_symbol
.name
= ".";
2972 dot_symbol
.flags
.forward_ref
= 1;
2973 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2974 if (dot_symbol
.bsym
== NULL
)
2975 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2976 dot_symbol
.bsym
->name
= ".";
2977 dot_symbol
.x
= &dot_symbol_x
;
2978 dot_symbol
.x
->value
.X_op
= O_constant
;
2983 /* Maximum indent level.
2984 Available for modification inside a gdb session. */
2985 static int max_indent_level
= 8;
2988 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2990 const char *name
= S_GET_NAME (sym
);
2991 if (!name
|| !name
[0])
2993 fprintf (file
, "sym ");
2994 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
2995 fprintf (file
, " %s", name
);
2997 if (sym
->flags
.local_symbol
)
2999 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
3001 if (locsym
->frag
!= &zero_address_frag
3002 && locsym
->frag
!= NULL
)
3004 fprintf (file
, " frag ");
3005 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) locsym
->frag
));
3007 if (locsym
->flags
.resolved
)
3008 fprintf (file
, " resolved");
3009 fprintf (file
, " local");
3013 if (sym
->frag
!= &zero_address_frag
)
3015 fprintf (file
, " frag ");
3016 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->frag
));
3018 if (sym
->flags
.written
)
3019 fprintf (file
, " written");
3020 if (sym
->flags
.resolved
)
3021 fprintf (file
, " resolved");
3022 else if (sym
->flags
.resolving
)
3023 fprintf (file
, " resolving");
3024 if (sym
->flags
.used_in_reloc
)
3025 fprintf (file
, " used-in-reloc");
3026 if (sym
->flags
.used
)
3027 fprintf (file
, " used");
3028 if (S_IS_LOCAL (sym
))
3029 fprintf (file
, " local");
3030 if (S_IS_EXTERNAL (sym
))
3031 fprintf (file
, " extern");
3032 if (S_IS_WEAK (sym
))
3033 fprintf (file
, " weak");
3034 if (S_IS_DEBUG (sym
))
3035 fprintf (file
, " debug");
3036 if (S_IS_DEFINED (sym
))
3037 fprintf (file
, " defined");
3039 if (S_IS_WEAKREFR (sym
))
3040 fprintf (file
, " weakrefr");
3041 if (S_IS_WEAKREFD (sym
))
3042 fprintf (file
, " weakrefd");
3043 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
3044 if (symbol_resolved_p (sym
))
3046 segT s
= S_GET_SEGMENT (sym
);
3048 if (s
!= undefined_section
3049 && s
!= expr_section
)
3050 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
3052 else if (indent_level
< max_indent_level
3053 && S_GET_SEGMENT (sym
) != undefined_section
)
3056 fprintf (file
, "\n%*s<", indent_level
* 4, "");
3057 if (sym
->flags
.local_symbol
)
3058 fprintf (file
, "constant %lx",
3059 (unsigned long) ((struct local_symbol
*) sym
)->value
);
3061 print_expr_1 (file
, &sym
->x
->value
);
3062 fprintf (file
, ">");
3069 print_symbol_value (symbolS
*sym
)
3072 print_symbol_value_1 (stderr
, sym
);
3073 fprintf (stderr
, "\n");
3077 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
3080 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
3081 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3082 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3083 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3084 fprintf (file
, ">");
3089 print_expr_1 (FILE *file
, expressionS
*exp
)
3091 fprintf (file
, "expr ");
3092 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
3093 fprintf (file
, " ");
3097 fprintf (file
, "illegal");
3100 fprintf (file
, "absent");
3103 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
3107 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
3108 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3109 fprintf (file
, ">");
3111 if (exp
->X_add_number
)
3112 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
3113 (unsigned long) exp
->X_add_number
);
3117 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
3120 fprintf (file
, "big");
3123 fprintf (file
, "uminus -<");
3125 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3126 fprintf (file
, ">");
3127 goto maybe_print_addnum
;
3129 fprintf (file
, "bit_not");
3132 print_binary (file
, "multiply", exp
);
3135 print_binary (file
, "divide", exp
);
3138 print_binary (file
, "modulus", exp
);
3141 print_binary (file
, "lshift", exp
);
3144 print_binary (file
, "rshift", exp
);
3146 case O_bit_inclusive_or
:
3147 print_binary (file
, "bit_ior", exp
);
3149 case O_bit_exclusive_or
:
3150 print_binary (file
, "bit_xor", exp
);
3153 print_binary (file
, "bit_and", exp
);
3156 print_binary (file
, "eq", exp
);
3159 print_binary (file
, "ne", exp
);
3162 print_binary (file
, "lt", exp
);
3165 print_binary (file
, "le", exp
);
3168 print_binary (file
, "ge", exp
);
3171 print_binary (file
, "gt", exp
);
3174 print_binary (file
, "logical_and", exp
);
3177 print_binary (file
, "logical_or", exp
);
3181 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3182 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3183 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3184 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3185 fprintf (file
, ">");
3186 goto maybe_print_addnum
;
3189 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3190 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3191 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3192 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3193 fprintf (file
, ">");
3194 goto maybe_print_addnum
;
3196 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3203 print_expr (expressionS
*exp
)
3205 print_expr_1 (stderr
, exp
);
3206 fprintf (stderr
, "\n");
3210 symbol_print_statistics (FILE *file
)
3212 htab_print_statistics (file
, "symbol table", sy_hash
);
3213 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3214 local_symbol_count
, local_symbol_conversion_count
);
3217 #ifdef OBJ_COMPLEX_RELC
3219 /* Convert given symbol to a new complex-relocation symbol name. This
3220 may be a recursive function, since it might be called for non-leaf
3221 nodes (plain symbols) in the expression tree. The caller owns the
3222 returning string, so should free it eventually. Errors are
3223 indicated via as_bad and a NULL return value. The given symbol
3224 is marked with used_in_reloc. */
3227 symbol_relc_make_sym (symbolS
* sym
)
3229 char * terminal
= NULL
;
3234 gas_assert (sym
!= NULL
);
3236 /* Recurse to symbol_relc_make_expr if this symbol
3237 is defined as an expression or a plain value. */
3238 if ( S_GET_SEGMENT (sym
) == expr_section
3239 || S_GET_SEGMENT (sym
) == absolute_section
)
3240 return symbol_relc_make_expr (symbol_get_value_expression (sym
));
3242 /* This may be a "fake symbol", referring to ".".
3243 Write out a special null symbol to refer to this position. */
3244 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3245 return xstrdup (".");
3247 /* We hope this is a plain leaf symbol. Construct the encoding
3248 as {S,s}II...:CCCCCCC....
3249 where 'S'/'s' means section symbol / plain symbol
3250 III is decimal for the symbol name length
3251 CCC is the symbol name itself. */
3252 symbol_mark_used_in_reloc (sym
);
3254 sname
= S_GET_NAME (sym
);
3255 sname_len
= strlen (sname
);
3256 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3258 terminal
= XNEWVEC (char, (1 /* S or s */
3259 + 8 /* sname_len in decimal */
3261 + sname_len
/* name itself */
3264 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3268 /* Convert given value to a new complex-relocation symbol name. This
3269 is a non-recursive function, since it is be called for leaf nodes
3270 (plain values) in the expression tree. The caller owns the
3271 returning string, so should free() it eventually. No errors. */
3274 symbol_relc_make_value (offsetT val
)
3276 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3279 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3283 /* Convert given expression to a new complex-relocation symbol name.
3284 This is a recursive function, since it traverses the entire given
3285 expression tree. The caller owns the returning string, so should
3286 free() it eventually. Errors are indicated via as_bad() and a NULL
3290 symbol_relc_make_expr (expressionS
* exp
)
3292 const char * opstr
= NULL
; /* Operator prefix string. */
3293 int arity
= 0; /* Arity of this operator. */
3294 char * operands
[3]; /* Up to three operands. */
3295 char * concat_string
= NULL
;
3297 operands
[0] = operands
[1] = operands
[2] = NULL
;
3299 gas_assert (exp
!= NULL
);
3301 /* Match known operators -> fill in opstr, arity, operands[] and fall
3302 through to construct subexpression fragments; may instead return
3303 string directly for leaf nodes. */
3305 /* See expr.h for the meaning of all these enums. Many operators
3306 have an unnatural arity (X_add_number implicitly added). The
3307 conversion logic expands them to explicit "+" subexpressions. */
3312 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3317 return symbol_relc_make_value (exp
->X_add_number
);
3320 if (exp
->X_add_number
)
3324 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3325 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3329 return symbol_relc_make_sym (exp
->X_add_symbol
);
3331 /* Helper macros for nesting nodes. */
3333 #define HANDLE_XADD_OPT1(str_) \
3334 if (exp->X_add_number) \
3337 opstr = "+:" str_; \
3338 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3339 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3346 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3350 #define HANDLE_XADD_OPT2(str_) \
3351 if (exp->X_add_number) \
3354 opstr = "+:" str_; \
3355 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3356 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3357 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3363 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3364 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3368 /* Nesting nodes. */
3370 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3371 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3372 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3373 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3374 case O_divide
: HANDLE_XADD_OPT2 ("/");
3375 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3376 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3377 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3378 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3379 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3380 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3381 case O_add
: HANDLE_XADD_OPT2 ("+");
3382 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3383 case O_eq
: HANDLE_XADD_OPT2 ("==");
3384 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3385 case O_lt
: HANDLE_XADD_OPT2 ("<");
3386 case O_le
: HANDLE_XADD_OPT2 ("<=");
3387 case O_ge
: HANDLE_XADD_OPT2 (">=");
3388 case O_gt
: HANDLE_XADD_OPT2 (">");
3389 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3390 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3393 /* Validate & reject early. */
3394 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3396 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3398 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3402 concat_string
= NULL
;
3403 else if (arity
== 0)
3404 concat_string
= xstrdup (opstr
);
3405 else if (arity
== 1)
3406 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3407 else if (arity
== 2)
3408 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3411 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3412 operands
[2], (char *) NULL
);
3414 /* Free operand strings (not opstr). */
3415 if (arity
>= 1) xfree (operands
[0]);
3416 if (arity
>= 2) xfree (operands
[1]);
3417 if (arity
>= 3) xfree (operands
[2]);
3419 return concat_string
;