1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
36 /* Whether the symbol is a local_symbol. */
37 unsigned int local_symbol : 1;
39 /* Weather symbol has been written. */
40 unsigned int written : 1;
42 /* Whether symbol value has been completely resolved (used during
43 final pass over symbol table). */
44 unsigned int resolved : 1;
46 /* Whether the symbol value is currently being resolved (used to
47 detect loops in symbol dependencies). */
48 unsigned int resolving : 1;
50 /* Whether the symbol value is used in a reloc. This is used to
51 ensure that symbols used in relocs are written out, even if they
52 are local and would otherwise not be. */
53 unsigned int used_in_reloc : 1;
55 /* Whether the symbol is used as an operand or in an expression.
56 NOTE: Not all the backends keep this information accurate;
57 backends which use this bit are responsible for setting it when
58 a symbol is used in backend routines. */
59 unsigned int used : 1;
61 /* Whether the symbol can be re-defined. */
62 unsigned int volatil : 1;
64 /* Whether the symbol is a forward reference, and whether such has
66 unsigned int forward_ref : 1;
67 unsigned int forward_resolved : 1;
69 /* This is set if the symbol is defined in an MRI common section.
70 We handle such sections as single common symbols, so symbols
71 defined within them must be treated specially by the relocation
73 unsigned int mri_common : 1;
75 /* This is set if the symbol is set with a .weakref directive. */
76 unsigned int weakrefr : 1;
78 /* This is set when the symbol is referenced as part of a .weakref
79 directive, but only if the symbol was not in the symbol table
80 before. It is cleared as soon as any direct reference to the
82 unsigned int weakrefd : 1;
84 /* Whether the symbol has been marked to be removed by a .symver
86 unsigned int removed : 1;
88 /* Set when a warning about the symbol containing multibyte characters
90 unsigned int multibyte_warned : 1;
93 /* A pointer in the symbol may point to either a complete symbol
94 (struct symbol below) or to a local symbol (struct local_symbol
95 defined here). The symbol code can detect the case by examining
96 the first field which is present in both structs.
98 We do this because we ordinarily only need a small amount of
99 information for a local symbol. The symbol table takes up a lot of
100 space, and storing less information for a local symbol can make a
101 big difference in assembler memory usage when assembling a large
106 /* Symbol flags. Only local_symbol and resolved are relevant. */
107 struct symbol_flags flags;
109 /* Hash value calculated from name. */
112 /* The symbol name. */
115 /* The symbol frag. */
118 /* The symbol section. */
121 /* The value of the symbol. */
125 /* The information we keep for a symbol. The symbol table holds
126 pointers both to this and to local_symbol structures. The first
127 three fields must be identical to struct local_symbol, and the size
128 should be the same as or smaller than struct local_symbol.
129 Fields that don't fit go to an extension structure. */
134 struct symbol_flags flags;
136 /* Hash value calculated from name. */
139 /* The symbol name. */
142 /* Pointer to the frag this symbol is attached to, if any.
149 /* Extra symbol fields that won't fit. */
153 /* Extra fields to make up a full symbol. */
157 /* The value of the symbol. */
160 /* Forwards and backwards chain pointers. */
162 struct symbol *previous;
164 #ifdef OBJ_SYMFIELD_TYPE
165 OBJ_SYMFIELD_TYPE obj;
168 #ifdef TC_SYMFIELD_TYPE
173 typedef union symbol_entry
175 struct local_symbol lsy;
179 /* Hash function for a symbol_entry. */
182 hash_symbol_entry (const void *e)
184 symbol_entry_t *entry = (symbol_entry_t *) e;
185 if (entry->sy.hash == 0)
186 entry->sy.hash = htab_hash_string (entry->sy.name);
188 return entry->sy.hash;
191 /* Equality function for a symbol_entry. */
194 eq_symbol_entry (const void *a, const void *b)
196 const symbol_entry_t *ea = (const symbol_entry_t *) a;
197 const symbol_entry_t *eb = (const symbol_entry_t *) b;
199 return (ea->sy.hash == eb->sy.hash
200 && strcmp (ea->sy.name, eb->sy.name) == 0);
204 symbol_entry_find (htab_t table, const char *name)
206 hashval_t hash = htab_hash_string (name);
207 symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
208 hash, name, 0, 0, 0 } };
209 return htab_find_with_hash (table, &needle, hash);
213 /* This is non-zero if symbols are case sensitive, which is the
215 int symbols_case_sensitive = 1;
217 #ifndef WORKING_DOT_WORD
218 extern int new_broken_words;
221 static htab_t sy_hash;
223 /* Below are commented in "symbols.h". */
224 symbolS *symbol_rootP;
225 symbolS *symbol_lastP;
227 struct xsymbol abs_symbol_x;
229 struct xsymbol dot_symbol_x;
232 #define debug_verify_symchain verify_symbol_chain
234 #define debug_verify_symchain(root, last) ((void) 0)
237 #define DOLLAR_LABEL_CHAR '\001'
238 #define LOCAL_LABEL_CHAR '\002'
240 #ifndef TC_LABEL_IS_LOCAL
241 #define TC_LABEL_IS_LOCAL(name) 0
244 struct obstack notes;
246 /* The name of an external symbol which is
247 used to make weak PE symbol names unique. */
248 const char * an_external_name;
251 /* Return a pointer to a new symbol. Die if we can't make a new
252 symbol. Fill in the symbol's values. Add symbol to end of symbol
255 This function should be called in the general case of creating a
256 symbol. However, if the output file symbol table has already been
257 set, and you are certain that this symbol won't be wanted in the
258 output file, you can call symbol_create. */
261 symbol_new (const char *name, segT segment, fragS *frag, valueT valu)
263 symbolS *symbolP = symbol_create (name, segment, frag, valu);
265 /* Link to end of symbol chain. */
266 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
271 /* Save a symbol name on a permanent obstack, and convert it according
272 to the object file format. */
275 save_symbol_name (const char *name)
280 gas_assert (name != NULL);
281 name_length = strlen (name) + 1; /* +1 for \0. */
282 obstack_grow (¬es, name, name_length);
283 ret = (char *) obstack_finish (¬es);
285 #ifdef tc_canonicalize_symbol_name
286 ret = tc_canonicalize_symbol_name (ret);
289 if (! symbols_case_sensitive)
293 for (s = ret; *s != '\0'; s++)
301 symbol_init (symbolS *symbolP, const char *name, asection *sec,
302 fragS *frag, valueT valu)
304 symbolP->frag = frag;
305 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
306 if (symbolP->bsym == NULL)
307 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
308 symbolP->bsym->name = name;
309 symbolP->bsym->section = sec;
311 if (multibyte_handling == multibyte_warn_syms
312 && ! symbolP->flags.local_symbol
313 && sec != undefined_section
314 && ! symbolP->flags.multibyte_warned
315 && scan_for_multibyte_characters ((const unsigned char *) name,
316 (const unsigned char *) name + strlen (name),
317 false /* Do not warn. */))
319 as_warn (_("symbol '%s' contains multibyte characters"), name);
320 symbolP->flags.multibyte_warned = 1;
323 S_SET_VALUE (symbolP, valu);
325 symbol_clear_list_pointers (symbolP);
327 obj_symbol_new_hook (symbolP);
329 #ifdef tc_symbol_new_hook
330 tc_symbol_new_hook (symbolP);
334 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
337 symbol_create (const char *name, segT segment, fragS *frag, valueT valu)
339 const char *preserved_copy_of_name;
343 preserved_copy_of_name = save_symbol_name (name);
345 size = sizeof (symbolS) + sizeof (struct xsymbol);
346 symbolP = (symbolS *) obstack_alloc (¬es, size);
348 /* symbol must be born in some fixed state. This seems as good as any. */
349 memset (symbolP, 0, size);
350 symbolP->name = preserved_copy_of_name;
351 symbolP->x = (struct xsymbol *) (symbolP + 1);
353 symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
359 /* Local symbol support. If we can get away with it, we keep only a
360 small amount of information for local symbols. */
362 /* Used for statistics. */
364 static unsigned long local_symbol_count;
365 static unsigned long local_symbol_conversion_count;
367 /* Create a local symbol and insert it into the local hash table. */
369 struct local_symbol *
370 local_symbol_make (const char *name, segT section, fragS *frag, valueT val)
372 const char *name_copy;
373 struct local_symbol *ret;
374 struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
376 ++local_symbol_count;
378 name_copy = save_symbol_name (name);
380 ret = (struct local_symbol *) obstack_alloc (¬es, sizeof *ret);
383 ret->name = name_copy;
385 ret->section = section;
388 htab_insert (sy_hash, ret, 1);
393 /* Convert a local symbol into a real symbol. */
396 local_symbol_convert (void *sym)
398 symbol_entry_t *ent = (symbol_entry_t *) sym;
399 struct xsymbol *xtra;
402 gas_assert (ent->lsy.flags.local_symbol);
404 ++local_symbol_conversion_count;
406 xtra = (struct xsymbol *) obstack_alloc (¬es, sizeof (*xtra));
407 memset (xtra, 0, sizeof (*xtra));
408 val = ent->lsy.value;
411 /* Local symbols are always either defined or used. */
412 ent->sy.flags.used = 1;
413 ent->sy.flags.local_symbol = 0;
415 symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
416 symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
422 define_sym_at_dot (symbolS *symbolP)
424 symbolP->frag = frag_now;
425 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
426 S_SET_SEGMENT (symbolP, now_seg);
429 /* We have just seen "<name>:".
430 Creates a struct symbol unless it already exists.
432 Gripes if we are redefining a symbol incompatibly (and ignores it). */
435 colon (/* Just seen "x:" - rattle symbols & frags. */
436 const char *sym_name /* Symbol name, as a canonical string. */
437 /* We copy this string: OK to alter later. */)
439 symbolS *symbolP; /* Symbol we are working with. */
441 /* Sun local labels go out of scope whenever a non-local symbol is
443 if (LOCAL_LABELS_DOLLAR
444 && !bfd_is_local_label_name (stdoutput, sym_name))
445 dollar_label_clear ();
447 #ifndef WORKING_DOT_WORD
448 if (new_broken_words)
450 struct broken_word *a;
455 if (now_seg == absolute_section)
457 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
461 possible_bytes = (md_short_jump_size
462 + new_broken_words * md_long_jump_size);
465 frag_opcode = frag_var (rs_broken_word,
469 (symbolS *) broken_words,
473 /* We want to store the pointer to where to insert the jump
474 table in the fr_opcode of the rs_broken_word frag. This
475 requires a little hackery. */
477 && (frag_tmp->fr_type != rs_broken_word
478 || frag_tmp->fr_opcode))
479 frag_tmp = frag_tmp->fr_next;
481 frag_tmp->fr_opcode = frag_opcode;
482 new_broken_words = 0;
484 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
485 a->dispfrag = frag_tmp;
487 #endif /* WORKING_DOT_WORD */
489 #ifdef obj_frob_colon
490 obj_frob_colon (sym_name);
493 if ((symbolP = symbol_find (sym_name)) != 0)
495 S_CLEAR_WEAKREFR (symbolP);
496 #ifdef RESOLVE_SYMBOL_REDEFINITION
497 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
500 /* Now check for undefined symbols. */
501 if (symbolP->flags.local_symbol)
503 struct local_symbol *locsym = (struct local_symbol *) symbolP;
505 if (locsym->section != undefined_section
506 && (locsym->frag != frag_now
507 || locsym->section != now_seg
508 || locsym->value != frag_now_fix ()))
510 as_bad (_("symbol `%s' is already defined"), sym_name);
514 locsym->section = now_seg;
515 locsym->frag = frag_now;
516 locsym->value = frag_now_fix ();
518 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
519 || S_IS_COMMON (symbolP)
520 || S_IS_VOLATILE (symbolP))
522 if (S_IS_VOLATILE (symbolP))
524 symbolP = symbol_clone (symbolP, 1);
525 S_SET_VALUE (symbolP, 0);
526 S_CLEAR_VOLATILE (symbolP);
528 if (S_GET_VALUE (symbolP) == 0)
530 define_sym_at_dot (symbolP);
533 #endif /* if we have one, it better be zero. */
538 /* There are still several cases to check:
540 A .comm/.lcomm symbol being redefined as initialized
543 A .comm/.lcomm symbol being redefined with a larger
546 This only used to be allowed on VMS gas, but Sun cc
547 on the sparc also depends on it. */
549 if (((!S_IS_DEBUG (symbolP)
550 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
551 && S_IS_EXTERNAL (symbolP))
552 || S_GET_SEGMENT (symbolP) == bss_section)
553 && (now_seg == data_section
554 || now_seg == bss_section
555 || now_seg == S_GET_SEGMENT (symbolP)))
557 /* Select which of the 2 cases this is. */
558 if (now_seg != data_section)
560 /* New .comm for prev .comm symbol.
562 If the new size is larger we just change its
563 value. If the new size is smaller, we ignore
565 if (S_GET_VALUE (symbolP)
566 < ((unsigned) frag_now_fix ()))
568 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
573 /* It is a .comm/.lcomm being converted to initialized
575 define_sym_at_dot (symbolP);
580 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
581 static const char *od_buf = "";
585 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
586 sprintf (od_buf, "%d.%d.",
587 S_GET_OTHER (symbolP),
588 S_GET_DESC (symbolP));
590 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
592 segment_name (S_GET_SEGMENT (symbolP)),
594 (long) S_GET_VALUE (symbolP));
596 } /* if the undefined symbol has no value */
600 /* Don't blow up if the definition is the same. */
601 if (!(frag_now == symbolP->frag
602 && S_GET_VALUE (symbolP) == frag_now_fix ()
603 && S_GET_SEGMENT (symbolP) == now_seg))
605 as_bad (_("symbol `%s' is already defined"), sym_name);
606 symbolP = symbol_clone (symbolP, 0);
607 define_sym_at_dot (symbolP);
612 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
614 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
619 symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
621 symbol_table_insert (symbolP);
624 if (mri_common_symbol != NULL)
626 /* This symbol is actually being defined within an MRI common
627 section. This requires special handling. */
628 if (symbolP->flags.local_symbol)
629 symbolP = local_symbol_convert (symbolP);
630 symbolP->x->value.X_op = O_symbol;
631 symbolP->x->value.X_add_symbol = mri_common_symbol;
632 symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
633 symbolP->frag = &zero_address_frag;
634 S_SET_SEGMENT (symbolP, expr_section);
635 symbolP->flags.mri_common = 1;
639 tc_frob_label (symbolP);
641 #ifdef obj_frob_label
642 obj_frob_label (symbolP);
648 /* Die if we can't insert the symbol. */
651 symbol_table_insert (symbolS *symbolP)
655 htab_insert (sy_hash, symbolP, 1);
658 /* If a symbol name does not exist, create it as undefined, and insert
659 it into the symbol table. Return a pointer to it. */
662 symbol_find_or_make (const char *name)
666 symbolP = symbol_find (name);
670 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
672 symbolP = md_undefined_symbol ((char *) name);
676 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
677 &zero_address_frag, 0);
681 symbolP = symbol_make (name);
683 symbol_table_insert (symbolP);
684 } /* if symbol wasn't found */
690 symbol_make (const char *name)
694 /* Let the machine description default it, e.g. for register names. */
695 symbolP = md_undefined_symbol ((char *) name);
698 symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
704 symbol_clone (symbolS *orgsymP, int replace)
707 asymbol *bsymorg, *bsymnew;
709 /* Make sure we never clone the dot special symbol. */
710 gas_assert (orgsymP != &dot_symbol);
712 /* When cloning a local symbol it isn't absolutely necessary to
713 convert the original, but converting makes the code much
714 simpler to cover this unexpected case. As of 2020-08-21
715 symbol_clone won't be called on a local symbol. */
716 if (orgsymP->flags.local_symbol)
717 orgsymP = local_symbol_convert (orgsymP);
718 bsymorg = orgsymP->bsym;
720 newsymP = (symbolS *) obstack_alloc (¬es, (sizeof (symbolS)
721 + sizeof (struct xsymbol)));
723 newsymP->x = (struct xsymbol *) (newsymP + 1);
724 *newsymP->x = *orgsymP->x;
725 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
727 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
728 newsymP->bsym = bsymnew;
729 bsymnew->name = bsymorg->name;
730 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
731 bsymnew->section = bsymorg->section;
732 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
733 bfd_asymbol_bfd (bsymnew), bsymnew);
735 #ifdef obj_symbol_clone_hook
736 obj_symbol_clone_hook (newsymP, orgsymP);
739 #ifdef tc_symbol_clone_hook
740 tc_symbol_clone_hook (newsymP, orgsymP);
745 if (symbol_rootP == orgsymP)
746 symbol_rootP = newsymP;
747 else if (orgsymP->x->previous)
749 orgsymP->x->previous->x->next = newsymP;
750 orgsymP->x->previous = NULL;
752 if (symbol_lastP == orgsymP)
753 symbol_lastP = newsymP;
754 else if (orgsymP->x->next)
755 orgsymP->x->next->x->previous = newsymP;
757 /* Symbols that won't be output can't be external. */
758 S_CLEAR_EXTERNAL (orgsymP);
759 orgsymP->x->previous = orgsymP->x->next = orgsymP;
760 debug_verify_symchain (symbol_rootP, symbol_lastP);
762 symbol_table_insert (newsymP);
766 /* Symbols that won't be output can't be external. */
767 S_CLEAR_EXTERNAL (newsymP);
768 newsymP->x->previous = newsymP->x->next = newsymP;
774 /* Referenced symbols, if they are forward references, need to be cloned
775 (without replacing the original) so that the value of the referenced
776 symbols at the point of use is saved by the clone. */
778 #undef symbol_clone_if_forward_ref
780 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
783 && !symbolP->flags.local_symbol
784 && !symbolP->flags.forward_resolved)
786 symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
787 symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
788 symbolS *add_symbol = orig_add_symbol;
789 symbolS *op_symbol = orig_op_symbol;
791 if (symbolP->flags.forward_ref)
796 /* assign_symbol() clones volatile symbols; pre-existing expressions
797 hold references to the original instance, but want the current
798 value. Just repeat the lookup. */
799 if (add_symbol && S_IS_VOLATILE (add_symbol))
800 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
801 if (op_symbol && S_IS_VOLATILE (op_symbol))
802 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
805 /* Re-using resolving here, as this routine cannot get called from
806 symbol resolution code. */
807 if ((symbolP->bsym->section == expr_section
808 || symbolP->flags.forward_ref)
809 && !symbolP->flags.resolving)
811 symbolP->flags.resolving = 1;
812 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
813 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
814 symbolP->flags.resolving = 0;
817 if (symbolP->flags.forward_ref
818 || add_symbol != orig_add_symbol
819 || op_symbol != orig_op_symbol)
821 if (symbolP != &dot_symbol)
823 symbolP = symbol_clone (symbolP, 0);
824 symbolP->flags.resolving = 0;
828 symbolP = symbol_temp_new_now ();
829 #ifdef tc_new_dot_label
830 tc_new_dot_label (symbolP);
835 symbolP->x->value.X_add_symbol = add_symbol;
836 symbolP->x->value.X_op_symbol = op_symbol;
837 symbolP->flags.forward_resolved = 1;
844 symbol_temp_new (segT seg, fragS *frag, valueT ofs)
846 return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
850 symbol_temp_new_now (void)
852 return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
856 symbol_temp_new_now_octets (void)
858 return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
862 symbol_temp_make (void)
864 return symbol_make (FAKE_LABEL_NAME);
867 /* Implement symbol table lookup.
868 In: A symbol's name as a string: '\0' can't be part of a symbol name.
869 Out: NULL if the name was not in the symbol table, else the address
870 of a struct symbol associated with that name. */
873 symbol_find_exact (const char *name)
875 return symbol_find_exact_noref (name, 0);
879 symbol_find_exact_noref (const char *name, int noref)
881 symbolS *sym = symbol_entry_find (sy_hash, name);
883 /* Any references to the symbol, except for the reference in
884 .weakref, must clear this flag, such that the symbol does not
885 turn into a weak symbol. Note that we don't have to handle the
886 local_symbol case, since a weakrefd is always promoted out of the
887 local_symbol table when it is turned into a weak symbol. */
889 S_CLEAR_WEAKREFD (sym);
895 symbol_find (const char *name)
897 return symbol_find_noref (name, 0);
901 symbol_find_noref (const char *name, int noref)
906 #ifdef tc_canonicalize_symbol_name
908 copy = xstrdup (name);
909 name = tc_canonicalize_symbol_name (copy);
913 if (! symbols_case_sensitive)
922 name = copy = XNEWVEC (char, strlen (name) + 1);
924 while ((c = *orig++) != '\0')
925 *copy++ = TOUPPER (c);
929 copy = (char *) name;
932 result = symbol_find_exact_noref (name, noref);
937 /* Once upon a time, symbols were kept in a singly linked list. At
938 least coff needs to be able to rearrange them from time to time, for
939 which a doubly linked list is much more convenient. Loic did these
940 as macros which seemed dangerous to me so they're now functions.
943 /* Link symbol ADDME after symbol TARGET in the chain. */
946 symbol_append (symbolS *addme, symbolS *target,
947 symbolS **rootPP, symbolS **lastPP)
949 extern int symbol_table_frozen;
950 if (symbol_table_frozen)
952 if (addme->flags.local_symbol)
954 if (target != NULL && target->flags.local_symbol)
959 know (*rootPP == NULL);
960 know (*lastPP == NULL);
961 addme->x->next = NULL;
962 addme->x->previous = NULL;
966 } /* if the list is empty */
968 if (target->x->next != NULL)
970 target->x->next->x->previous = addme;
974 know (*lastPP == target);
976 } /* if we have a next */
978 addme->x->next = target->x->next;
979 target->x->next = addme;
980 addme->x->previous = target;
982 debug_verify_symchain (symbol_rootP, symbol_lastP);
985 /* Set the chain pointers of SYMBOL to null. */
988 symbol_clear_list_pointers (symbolS *symbolP)
990 if (symbolP->flags.local_symbol)
992 symbolP->x->next = NULL;
993 symbolP->x->previous = NULL;
996 /* Remove SYMBOLP from the list. */
999 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1001 if (symbolP->flags.local_symbol)
1004 if (symbolP == *rootPP)
1006 *rootPP = symbolP->x->next;
1007 } /* if it was the root */
1009 if (symbolP == *lastPP)
1011 *lastPP = symbolP->x->previous;
1012 } /* if it was the tail */
1014 if (symbolP->x->next != NULL)
1016 symbolP->x->next->x->previous = symbolP->x->previous;
1019 if (symbolP->x->previous != NULL)
1021 symbolP->x->previous->x->next = symbolP->x->next;
1022 } /* if not first */
1024 debug_verify_symchain (*rootPP, *lastPP);
1027 /* Link symbol ADDME before symbol TARGET in the chain. */
1030 symbol_insert (symbolS *addme, symbolS *target,
1031 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
1033 extern int symbol_table_frozen;
1034 if (symbol_table_frozen)
1036 if (addme->flags.local_symbol)
1038 if (target->flags.local_symbol)
1041 if (target->x->previous != NULL)
1043 target->x->previous->x->next = addme;
1047 know (*rootPP == target);
1049 } /* if not first */
1051 addme->x->previous = target->x->previous;
1052 target->x->previous = addme;
1053 addme->x->next = target;
1055 debug_verify_symchain (*rootPP, *lastPP);
1059 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
1061 symbolS *symbolP = rootP;
1063 if (symbolP == NULL)
1066 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1068 gas_assert (symbolP->bsym != NULL);
1069 gas_assert (symbolP->flags.local_symbol == 0);
1070 gas_assert (symbolP->x->next->x->previous == symbolP);
1073 gas_assert (lastP == symbolP);
1077 symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1079 return (!s->flags.local_symbol
1080 && ((s->x->next != s
1081 && s->x->next != NULL
1082 && s->x->next->x->previous == s)
1084 && ((s->x->previous != s
1085 && s->x->previous != NULL
1086 && s->x->previous->x->next == s)
1090 #ifdef OBJ_COMPLEX_RELC
1093 use_complex_relocs_for (symbolS * symp)
1095 switch (symp->x->value.X_op)
1105 case O_bit_inclusive_or:
1107 case O_bit_exclusive_or:
1119 if ((S_IS_COMMON (symp->x->value.X_op_symbol)
1120 || S_IS_LOCAL (symp->x->value.X_op_symbol))
1121 && S_IS_DEFINED (symp->x->value.X_op_symbol)
1122 && S_GET_SEGMENT (symp->x->value.X_op_symbol) != expr_section)
1129 if ((S_IS_COMMON (symp->x->value.X_add_symbol)
1130 || S_IS_LOCAL (symp->x->value.X_add_symbol))
1131 && S_IS_DEFINED (symp->x->value.X_add_symbol)
1132 && S_GET_SEGMENT (symp->x->value.X_add_symbol) != expr_section)
1145 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
1149 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1150 segT seg_right = S_GET_SEGMENT (right);
1159 case O_uminus: opname = "-"; break;
1160 case O_bit_not: opname = "~"; break;
1161 case O_logical_not: opname = "!"; break;
1162 case O_multiply: opname = "*"; break;
1163 case O_divide: opname = "/"; break;
1164 case O_modulus: opname = "%"; break;
1165 case O_left_shift: opname = "<<"; break;
1166 case O_right_shift: opname = ">>"; break;
1167 case O_bit_inclusive_or: opname = "|"; break;
1168 case O_bit_or_not: opname = "|~"; break;
1169 case O_bit_exclusive_or: opname = "^"; break;
1170 case O_bit_and: opname = "&"; break;
1171 case O_add: opname = "+"; break;
1172 case O_subtract: opname = "-"; break;
1173 case O_eq: opname = "=="; break;
1174 case O_ne: opname = "!="; break;
1175 case O_lt: opname = "<"; break;
1176 case O_le: opname = "<="; break;
1177 case O_ge: opname = ">="; break;
1178 case O_gt: opname = ">"; break;
1179 case O_logical_and: opname = "&&"; break;
1180 case O_logical_or: opname = "||"; break;
1183 if (expr_symbol_where (symp, &file, &line))
1186 as_bad_where (file, line,
1187 _("invalid operands (%s and %s sections) for `%s'"),
1188 seg_left->name, seg_right->name, opname);
1190 as_bad_where (file, line,
1191 _("invalid operand (%s section) for `%s'"),
1192 seg_right->name, opname);
1196 const char *sname = S_GET_NAME (symp);
1199 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1200 seg_left->name, seg_right->name, opname, sname);
1202 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1203 seg_right->name, opname, sname);
1207 /* Resolve the value of a symbol. This is called during the final
1208 pass over the symbol table to resolve any symbols with complex
1212 resolve_symbol_value (symbolS *symp)
1218 if (symp->flags.local_symbol)
1220 struct local_symbol *locsym = (struct local_symbol *) symp;
1222 final_val = locsym->value;
1223 if (locsym->flags.resolved)
1226 /* Symbols whose section has SEC_ELF_OCTETS set,
1227 resolve to octets instead of target bytes. */
1228 if (locsym->section->flags & SEC_OCTETS)
1229 final_val += locsym->frag->fr_address;
1231 final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1235 locsym->value = final_val;
1236 locsym->flags.resolved = 1;
1242 if (symp->flags.resolved)
1245 while (symp->x->value.X_op == O_symbol)
1247 final_val += symp->x->value.X_add_number;
1248 symp = symp->x->value.X_add_symbol;
1249 if (symp->flags.local_symbol)
1251 struct local_symbol *locsym = (struct local_symbol *) symp;
1252 final_val += locsym->value;
1255 if (!symp->flags.resolved)
1258 if (symp->x->value.X_op == O_constant)
1259 final_val += symp->x->value.X_add_number;
1266 final_seg = S_GET_SEGMENT (symp);
1268 if (symp->flags.resolving)
1271 as_bad (_("symbol definition loop encountered at `%s'"),
1276 #ifdef OBJ_COMPLEX_RELC
1277 else if (final_seg == expr_section
1278 && use_complex_relocs_for (symp))
1280 symbolS * relc_symbol = NULL;
1281 char * relc_symbol_name = NULL;
1283 relc_symbol_name = symbol_relc_make_expr (& symp->x->value);
1285 /* For debugging, print out conversion input & output. */
1287 print_expr (& symp->x->value);
1288 if (relc_symbol_name)
1289 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1292 if (relc_symbol_name != NULL)
1293 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1294 &zero_address_frag, 0);
1296 if (relc_symbol == NULL)
1298 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1304 symbol_table_insert (relc_symbol);
1306 /* S_CLEAR_EXTERNAL (relc_symbol); */
1307 if (symp->bsym->flags & BSF_SRELC)
1308 relc_symbol->bsym->flags |= BSF_SRELC;
1310 relc_symbol->bsym->flags |= BSF_RELC;
1311 /* symp->bsym->flags |= BSF_RELC; */
1312 copy_symbol_attributes (symp, relc_symbol);
1313 symp->x->value.X_op = O_symbol;
1314 symp->x->value.X_add_symbol = relc_symbol;
1315 symp->x->value.X_add_number = 0;
1320 final_seg = undefined_section;
1321 goto exit_dont_set_value;
1326 symbolS *add_symbol, *op_symbol;
1327 offsetT left, right;
1328 segT seg_left, seg_right;
1332 symp->flags.resolving = 1;
1334 /* Help out with CSE. */
1335 add_symbol = symp->x->value.X_add_symbol;
1336 op_symbol = symp->x->value.X_op_symbol;
1337 final_val = symp->x->value.X_add_number;
1338 op = symp->x->value.X_op;
1351 /* Symbols whose section has SEC_ELF_OCTETS set,
1352 resolve to octets instead of target bytes. */
1353 if (symp->bsym->section->flags & SEC_OCTETS)
1354 final_val += symp->frag->fr_address;
1356 final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1357 if (final_seg == expr_section)
1358 final_seg = absolute_section;
1368 left = resolve_symbol_value (add_symbol);
1369 seg_left = S_GET_SEGMENT (add_symbol);
1371 symp->x->value.X_op_symbol = NULL;
1374 if (S_IS_WEAKREFR (symp))
1376 gas_assert (final_val == 0);
1377 if (S_IS_WEAKREFR (add_symbol))
1379 gas_assert (add_symbol->x->value.X_op == O_symbol
1380 && add_symbol->x->value.X_add_number == 0);
1381 add_symbol = add_symbol->x->value.X_add_symbol;
1382 gas_assert (! S_IS_WEAKREFR (add_symbol));
1383 symp->x->value.X_add_symbol = add_symbol;
1387 if (symp->flags.mri_common)
1389 /* This is a symbol inside an MRI common section. The
1390 relocation routines are going to handle it specially.
1391 Don't change the value. */
1392 resolved = symbol_resolved_p (add_symbol);
1396 /* Don't leave symbol loops. */
1398 && !add_symbol->flags.local_symbol
1399 && add_symbol->flags.resolving)
1402 if (finalize_syms && final_val == 0
1404 /* Avoid changing symp's "within" when dealing with
1405 AIX debug symbols. For some storage classes, "within"
1406 have a special meaning.
1407 C_DWARF should behave like on Linux, thus this check
1408 isn't done to be closer. */
1409 && ((symbol_get_bfdsym (symp)->flags & BSF_DEBUGGING) == 0
1410 || (S_GET_STORAGE_CLASS (symp) == C_DWARF))
1414 if (add_symbol->flags.local_symbol)
1415 add_symbol = local_symbol_convert (add_symbol);
1416 copy_symbol_attributes (symp, add_symbol);
1419 /* If we have equated this symbol to an undefined or common
1420 symbol, keep X_op set to O_symbol, and don't change
1421 X_add_number. This permits the routine which writes out
1422 relocation to detect this case, and convert the
1423 relocation to be against the symbol to which this symbol
1425 if (seg_left == undefined_section
1426 || bfd_is_com_section (seg_left)
1427 #if defined (OBJ_COFF) && defined (TE_PE)
1428 || S_IS_WEAK (add_symbol)
1431 && ((final_seg == expr_section
1432 && seg_left != expr_section
1433 && seg_left != absolute_section)
1434 || symbol_shadow_p (symp))))
1438 symp->x->value.X_op = O_symbol;
1439 symp->x->value.X_add_symbol = add_symbol;
1440 symp->x->value.X_add_number = final_val;
1441 /* Use X_op_symbol as a flag. */
1442 symp->x->value.X_op_symbol = add_symbol;
1444 final_seg = seg_left;
1445 final_val += symp->frag->fr_address + left;
1446 resolved = symbol_resolved_p (add_symbol);
1447 symp->flags.resolving = 0;
1449 if (op == O_secidx && seg_left != undefined_section)
1455 goto exit_dont_set_value;
1459 final_val += symp->frag->fr_address + left;
1460 if (final_seg == expr_section || final_seg == undefined_section)
1461 final_seg = seg_left;
1464 resolved = symbol_resolved_p (add_symbol);
1465 if (S_IS_WEAKREFR (symp))
1467 symp->flags.resolving = 0;
1468 goto exit_dont_set_value;
1475 left = resolve_symbol_value (add_symbol);
1476 seg_left = S_GET_SEGMENT (add_symbol);
1478 /* By reducing these to the relevant dyadic operator, we get
1479 !S -> S == 0 permitted on anything,
1480 -S -> 0 - S only permitted on absolute
1481 ~S -> S ^ ~0 only permitted on absolute */
1482 if (op != O_logical_not && seg_left != absolute_section
1484 report_op_error (symp, NULL, op, add_symbol);
1486 if (final_seg == expr_section || final_seg == undefined_section)
1487 final_seg = absolute_section;
1491 else if (op == O_logical_not)
1496 final_val += left + symp->frag->fr_address;
1498 resolved = symbol_resolved_p (add_symbol);
1506 case O_bit_inclusive_or:
1508 case O_bit_exclusive_or:
1520 left = resolve_symbol_value (add_symbol);
1521 right = resolve_symbol_value (op_symbol);
1522 seg_left = S_GET_SEGMENT (add_symbol);
1523 seg_right = S_GET_SEGMENT (op_symbol);
1525 /* Simplify addition or subtraction of a constant by folding the
1526 constant into X_add_number. */
1529 if (seg_right == absolute_section)
1534 else if (seg_left == absolute_section)
1537 add_symbol = op_symbol;
1539 seg_left = seg_right;
1543 else if (op == O_subtract)
1545 if (seg_right == absolute_section)
1553 /* Equality and non-equality tests are permitted on anything.
1554 Subtraction, and other comparison operators are permitted if
1555 both operands are in the same section. Otherwise, both
1556 operands must be absolute. We already handled the case of
1557 addition or subtraction of a constant above. This will
1558 probably need to be changed for an object file format which
1559 supports arbitrary expressions. */
1560 if (!(seg_left == absolute_section
1561 && seg_right == absolute_section)
1562 && !(op == O_eq || op == O_ne)
1563 && !((op == O_subtract
1564 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1565 && seg_left == seg_right
1566 && (seg_left != undefined_section
1567 || add_symbol == op_symbol)))
1569 /* Don't emit messages unless we're finalizing the symbol value,
1570 otherwise we may get the same message multiple times. */
1572 report_op_error (symp, add_symbol, op, op_symbol);
1573 /* However do not move the symbol into the absolute section
1574 if it cannot currently be resolved - this would confuse
1575 other parts of the assembler into believing that the
1576 expression had been evaluated to zero. */
1582 && (final_seg == expr_section || final_seg == undefined_section))
1583 final_seg = absolute_section;
1585 /* Check for division by zero. */
1586 if ((op == O_divide || op == O_modulus) && right == 0)
1588 /* If seg_right is not absolute_section, then we've
1589 already issued a warning about using a bad symbol. */
1590 if (seg_right == absolute_section && finalize_syms)
1595 if (expr_symbol_where (symp, &file, &line))
1596 as_bad_where (file, line, _("division by zero"));
1598 as_bad (_("division by zero when setting `%s'"),
1604 if ((op == O_left_shift || op == O_right_shift)
1605 && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1607 as_warn_value_out_of_range (_("shift count"), right, 0,
1608 sizeof (valueT) * CHAR_BIT - 1,
1613 switch (symp->x->value.X_op)
1615 case O_multiply: left *= right; break;
1616 case O_divide: left /= right; break;
1617 case O_modulus: left %= right; break;
1619 left = (valueT) left << (valueT) right; break;
1621 left = (valueT) left >> (valueT) right; break;
1622 case O_bit_inclusive_or: left |= right; break;
1623 case O_bit_or_not: left |= ~right; break;
1624 case O_bit_exclusive_or: left ^= right; break;
1625 case O_bit_and: left &= right; break;
1626 case O_add: left += right; break;
1627 case O_subtract: left -= right; break;
1630 left = (left == right && seg_left == seg_right
1631 && (seg_left != undefined_section
1632 || add_symbol == op_symbol)
1633 ? ~ (offsetT) 0 : 0);
1634 if (symp->x->value.X_op == O_ne)
1637 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1638 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1639 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1640 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1641 case O_logical_and: left = left && right; break;
1642 case O_logical_or: left = left || right; break;
1647 /* See PR 20895 for a reproducer. */
1648 as_bad (_("Invalid operation on symbol"));
1649 goto exit_dont_set_value;
1655 final_val += symp->frag->fr_address + left;
1656 if (final_seg == expr_section || final_seg == undefined_section)
1658 if (seg_left == undefined_section
1659 || seg_right == undefined_section)
1660 final_seg = undefined_section;
1661 else if (seg_left == absolute_section)
1662 final_seg = seg_right;
1664 final_seg = seg_left;
1666 resolved = (symbol_resolved_p (add_symbol)
1667 && symbol_resolved_p (op_symbol));
1672 /* Give an error (below) if not in expr_section. We don't
1673 want to worry about expr_section symbols, because they
1674 are fictional (they are created as part of expression
1675 resolution), and any problems may not actually mean
1680 symp->flags.resolving = 0;
1684 S_SET_VALUE (symp, final_val);
1686 exit_dont_set_value:
1687 /* Always set the segment, even if not finalizing the value.
1688 The segment is used to determine whether a symbol is defined. */
1689 S_SET_SEGMENT (symp, final_seg);
1691 /* Don't worry if we can't resolve an expr_section symbol. */
1695 symp->flags.resolved = 1;
1696 else if (S_GET_SEGMENT (symp) != expr_section)
1698 as_bad (_("can't resolve value for symbol `%s'"),
1700 symp->flags.resolved = 1;
1707 /* A static function passed to hash_traverse. */
1710 resolve_local_symbol (void **slot, void *arg ATTRIBUTE_UNUSED)
1712 symbol_entry_t *entry = *((symbol_entry_t **) slot);
1713 if (entry->sy.flags.local_symbol)
1714 resolve_symbol_value (&entry->sy);
1719 /* Resolve all local symbols. */
1722 resolve_local_symbol_values (void)
1724 htab_traverse (sy_hash, resolve_local_symbol, NULL);
1727 /* Obtain the current value of a symbol without changing any
1728 sub-expressions used. */
1731 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1733 symbolS *symbolP = *symbolPP;
1735 if (symbolP->flags.local_symbol)
1737 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1739 *valueP = locsym->value;
1740 *segP = locsym->section;
1741 *fragPP = locsym->frag;
1745 expressionS exp = symbolP->x->value;
1747 if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1751 if (symbolP->flags.resolving)
1753 symbolP->flags.resolving = 1;
1754 resolved = resolve_expression (&exp);
1755 symbolP->flags.resolving = 0;
1763 if (!symbol_equated_p (symbolP))
1768 symbolP = exp.X_add_symbol;
1775 *symbolPP = symbolP;
1777 /* A bogus input file can result in resolve_expression()
1778 generating a local symbol, so we have to check again. */
1779 if (symbolP->flags.local_symbol)
1781 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1783 *valueP = locsym->value;
1784 *segP = locsym->section;
1785 *fragPP = locsym->frag;
1789 *valueP = exp.X_add_number;
1790 *segP = symbolP->bsym->section;
1791 *fragPP = symbolP->frag;
1794 if (*segP == expr_section)
1797 case O_constant: *segP = absolute_section; break;
1798 case O_register: *segP = reg_section; break;
1806 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1807 They are *really* local. That is, they go out of scope whenever we see a
1808 label that isn't local. Also, like fb labels, there can be multiple
1809 instances of a dollar label. Therefor, we name encode each instance with
1810 the instance number, keep a list of defined symbols separate from the real
1811 symbol table, and we treat these buggers as a sparse array. */
1813 typedef unsigned int dollar_ent;
1814 static dollar_ent *dollar_labels;
1815 static dollar_ent *dollar_label_instances;
1816 static char *dollar_label_defines;
1817 static size_t dollar_label_count;
1818 static size_t dollar_label_max;
1821 dollar_label_defined (unsigned int label)
1825 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1827 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1829 return dollar_label_defines[i - dollar_labels];
1831 /* If we get here, label isn't defined. */
1836 dollar_label_instance (unsigned int label)
1840 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1842 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1844 return (dollar_label_instances[i - dollar_labels]);
1846 /* If we get here, we haven't seen the label before.
1847 Therefore its instance count is zero. */
1852 dollar_label_clear (void)
1854 if (dollar_label_count)
1855 memset (dollar_label_defines, '\0', dollar_label_count);
1858 #define DOLLAR_LABEL_BUMP_BY 10
1861 define_dollar_label (unsigned int label)
1865 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1868 ++dollar_label_instances[i - dollar_labels];
1869 dollar_label_defines[i - dollar_labels] = 1;
1873 /* If we get to here, we don't have label listed yet. */
1875 if (dollar_labels == NULL)
1877 dollar_labels = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1878 dollar_label_instances = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1879 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1880 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1881 dollar_label_count = 0;
1883 else if (dollar_label_count == dollar_label_max)
1885 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1886 dollar_labels = XRESIZEVEC (dollar_ent, dollar_labels,
1888 dollar_label_instances = XRESIZEVEC (dollar_ent,
1889 dollar_label_instances,
1891 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
1893 } /* if we needed to grow */
1895 dollar_labels[dollar_label_count] = label;
1896 dollar_label_instances[dollar_label_count] = 1;
1897 dollar_label_defines[dollar_label_count] = 1;
1898 ++dollar_label_count;
1901 /* Caller must copy returned name: we re-use the area for the next name.
1903 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1904 where n is the label number and m is the instance number. "L" makes
1905 it a label discarded unless debugging and "^A"('\1') ensures no
1906 ordinary symbol SHOULD get the same name as a local label
1907 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1909 fb labels get the same treatment, except that ^B is used in place
1912 AUGEND is 0 for current instance, 1 for new instance. */
1915 dollar_label_name (unsigned int n, unsigned int augend)
1917 /* Returned to caller, then copied. Used for created names ("4f"). */
1918 static char symbol_name_build[24];
1919 char *p = symbol_name_build;
1921 #ifdef LOCAL_LABEL_PREFIX
1922 *p++ = LOCAL_LABEL_PREFIX;
1924 sprintf (p, "L%u%c%u",
1925 n, DOLLAR_LABEL_CHAR, dollar_label_instance (n) + augend);
1926 return symbol_name_build;
1929 /* Somebody else's idea of local labels. They are made by "n:" where n
1930 is any decimal digit. Refer to them with
1931 "nb" for previous (backward) n:
1932 or "nf" for next (forward) n:.
1934 We do a little better and let n be any number, not just a single digit, but
1935 since the other guy's assembler only does ten, we treat the first ten
1938 Like someone else's assembler, we have one set of local label counters for
1939 entire assembly, not one set per (sub)segment like in most assemblers. This
1940 implies that one can refer to a label in another segment, and indeed some
1941 crufty compilers have done just that.
1943 Since there could be a LOT of these things, treat them as a sparse
1946 #define FB_LABEL_SPECIAL (10)
1948 typedef unsigned int fb_ent;
1949 static fb_ent fb_low_counter[FB_LABEL_SPECIAL];
1950 static fb_ent *fb_labels;
1951 static fb_ent *fb_label_instances;
1952 static size_t fb_label_count;
1953 static size_t fb_label_max;
1955 /* This must be more than FB_LABEL_SPECIAL. */
1956 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1959 fb_label_init (void)
1961 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1964 /* Add one to the instance number of this fb label. */
1967 fb_label_instance_inc (unsigned int label)
1971 if (label < FB_LABEL_SPECIAL)
1973 ++fb_low_counter[label];
1977 if (fb_labels != NULL)
1979 for (i = fb_labels + FB_LABEL_SPECIAL;
1980 i < fb_labels + fb_label_count; ++i)
1984 ++fb_label_instances[i - fb_labels];
1986 } /* if we find it */
1987 } /* for each existing label */
1990 /* If we get to here, we don't have label listed yet. */
1992 if (fb_labels == NULL)
1994 fb_labels = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
1995 fb_label_instances = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
1996 fb_label_max = FB_LABEL_BUMP_BY;
1997 fb_label_count = FB_LABEL_SPECIAL;
2000 else if (fb_label_count == fb_label_max)
2002 fb_label_max += FB_LABEL_BUMP_BY;
2003 fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
2004 fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
2006 } /* if we needed to grow */
2008 fb_labels[fb_label_count] = label;
2009 fb_label_instances[fb_label_count] = 1;
2014 fb_label_instance (unsigned int label)
2018 if (label < FB_LABEL_SPECIAL)
2019 return (fb_low_counter[label]);
2021 if (fb_labels != NULL)
2023 for (i = fb_labels + FB_LABEL_SPECIAL;
2024 i < fb_labels + fb_label_count; ++i)
2027 return (fb_label_instances[i - fb_labels]);
2031 /* We didn't find the label, so this must be a reference to the
2036 /* Caller must copy returned name: we re-use the area for the next name.
2038 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2039 where n is the label number and m is the instance number. "L" makes
2040 it a label discarded unless debugging and "^B"('\2') ensures no
2041 ordinary symbol SHOULD get the same name as a local label
2042 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2044 dollar labels get the same treatment, except that ^A is used in
2047 AUGEND is 0 for nb, 1 for n:, nf. */
2050 fb_label_name (unsigned int n, unsigned int augend)
2052 /* Returned to caller, then copied. Used for created names ("4f"). */
2053 static char symbol_name_build[24];
2054 char *p = symbol_name_build;
2057 know (augend <= 2 /* See mmix_fb_label. */);
2062 #ifdef LOCAL_LABEL_PREFIX
2063 *p++ = LOCAL_LABEL_PREFIX;
2065 sprintf (p, "L%u%c%u",
2066 n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2067 return symbol_name_build;
2070 /* Decode name that may have been generated by foo_label_name() above.
2071 If the name wasn't generated by foo_label_name(), then return it
2072 unaltered. This is used for error messages. */
2075 decode_local_label_name (char *s)
2078 char *symbol_decode;
2080 int instance_number;
2082 const char *message_format;
2085 #ifdef LOCAL_LABEL_PREFIX
2086 if (s[lindex] == LOCAL_LABEL_PREFIX)
2090 if (s[lindex] != 'L')
2093 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
2094 label_number = (10 * label_number) + *p - '0';
2096 if (*p == DOLLAR_LABEL_CHAR)
2098 else if (*p == LOCAL_LABEL_CHAR)
2103 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
2104 instance_number = (10 * instance_number) + *p - '0';
2106 message_format = _("\"%d\" (instance number %d of a %s label)");
2107 symbol_decode = (char *) obstack_alloc (¬es, strlen (message_format) + 30);
2108 sprintf (symbol_decode, message_format, label_number, instance_number, type);
2110 return symbol_decode;
2113 /* Get the value of a symbol. */
2116 S_GET_VALUE (symbolS *s)
2118 if (s->flags.local_symbol)
2119 return resolve_symbol_value (s);
2121 if (!s->flags.resolved)
2123 valueT val = resolve_symbol_value (s);
2127 if (S_IS_WEAKREFR (s))
2128 return S_GET_VALUE (s->x->value.X_add_symbol);
2130 if (s->x->value.X_op != O_constant)
2132 if (! s->flags.resolved
2133 || s->x->value.X_op != O_symbol
2134 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
2135 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2138 return (valueT) s->x->value.X_add_number;
2141 /* Set the value of a symbol. */
2144 S_SET_VALUE (symbolS *s, valueT val)
2146 if (s->flags.local_symbol)
2148 ((struct local_symbol *) s)->value = val;
2152 s->x->value.X_op = O_constant;
2153 s->x->value.X_add_number = (offsetT) val;
2154 s->x->value.X_unsigned = 0;
2155 S_CLEAR_WEAKREFR (s);
2159 copy_symbol_attributes (symbolS *dest, symbolS *src)
2161 if (dest->flags.local_symbol)
2162 dest = local_symbol_convert (dest);
2163 if (src->flags.local_symbol)
2164 src = local_symbol_convert (src);
2166 /* In an expression, transfer the settings of these flags.
2167 The user can override later, of course. */
2168 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2169 | BSF_GNU_INDIRECT_FUNCTION)
2170 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2172 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2173 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2176 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2177 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2182 S_IS_FUNCTION (symbolS *s)
2186 if (s->flags.local_symbol)
2189 flags = s->bsym->flags;
2191 return (flags & BSF_FUNCTION) != 0;
2195 S_IS_EXTERNAL (symbolS *s)
2199 if (s->flags.local_symbol)
2202 flags = s->bsym->flags;
2205 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2208 return (flags & BSF_GLOBAL) != 0;
2212 S_IS_WEAK (symbolS *s)
2214 if (s->flags.local_symbol)
2216 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2217 could probably handle a WEAKREFR as always weak though. E.g., if
2218 the referenced symbol has lost its weak status, there's no reason
2219 to keep handling the weakrefr as if it was weak. */
2220 if (S_IS_WEAKREFR (s))
2221 return S_IS_WEAK (s->x->value.X_add_symbol);
2222 return (s->bsym->flags & BSF_WEAK) != 0;
2226 S_IS_WEAKREFR (symbolS *s)
2228 if (s->flags.local_symbol)
2230 return s->flags.weakrefr != 0;
2234 S_IS_WEAKREFD (symbolS *s)
2236 if (s->flags.local_symbol)
2238 return s->flags.weakrefd != 0;
2242 S_IS_COMMON (symbolS *s)
2244 if (s->flags.local_symbol)
2246 return bfd_is_com_section (s->bsym->section);
2250 S_IS_DEFINED (symbolS *s)
2252 if (s->flags.local_symbol)
2253 return ((struct local_symbol *) s)->section != undefined_section;
2254 return s->bsym->section != undefined_section;
2258 #ifndef EXTERN_FORCE_RELOC
2259 #define EXTERN_FORCE_RELOC IS_ELF
2262 /* Return true for symbols that should not be reduced to section
2263 symbols or eliminated from expressions, because they may be
2264 overridden by the linker. */
2266 S_FORCE_RELOC (symbolS *s, int strict)
2269 if (s->flags.local_symbol)
2270 sec = ((struct local_symbol *) s)->section;
2274 && ((s->bsym->flags & BSF_WEAK) != 0
2275 || (EXTERN_FORCE_RELOC
2276 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2277 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2279 sec = s->bsym->section;
2281 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2285 S_IS_DEBUG (symbolS *s)
2287 if (s->flags.local_symbol)
2289 if (s->bsym->flags & BSF_DEBUGGING)
2295 S_IS_LOCAL (symbolS *s)
2300 if (s->flags.local_symbol)
2303 flags = s->bsym->flags;
2306 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2309 if (bfd_asymbol_section (s->bsym) == reg_section)
2312 if (flag_strip_local_absolute
2313 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2314 the source file even when the object file is stripped. */
2315 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2316 && bfd_asymbol_section (s->bsym) == absolute_section)
2319 name = S_GET_NAME (s);
2320 return (name != NULL
2322 && (strchr (name, DOLLAR_LABEL_CHAR)
2323 || strchr (name, LOCAL_LABEL_CHAR)
2324 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2325 || strchr (name, FAKE_LABEL_CHAR)
2327 || TC_LABEL_IS_LOCAL (name)
2328 || (! flag_keep_locals
2329 && (bfd_is_local_label (stdoutput, s->bsym)
2332 && name[1] == '?')))));
2336 S_IS_STABD (symbolS *s)
2338 return S_GET_NAME (s) == 0;
2342 S_CAN_BE_REDEFINED (const symbolS *s)
2344 if (s->flags.local_symbol)
2345 return (((struct local_symbol *) s)->frag
2346 == &predefined_address_frag);
2347 /* Permit register names to be redefined. */
2348 return s->bsym->section == reg_section;
2352 S_IS_VOLATILE (const symbolS *s)
2354 if (s->flags.local_symbol)
2356 return s->flags.volatil;
2360 S_IS_FORWARD_REF (const symbolS *s)
2362 if (s->flags.local_symbol)
2364 return s->flags.forward_ref;
2368 S_GET_NAME (symbolS *s)
2374 S_GET_SEGMENT (symbolS *s)
2376 if (s->flags.local_symbol)
2377 return ((struct local_symbol *) s)->section;
2378 return s->bsym->section;
2382 S_SET_SEGMENT (symbolS *s, segT seg)
2384 if (s->flags.local_symbol)
2386 ((struct local_symbol *) s)->section = seg;
2390 /* Don't reassign section symbols. The direct reason is to prevent seg
2391 faults assigning back to const global symbols such as *ABS*, but it
2392 shouldn't happen anyway. */
2393 if (s->bsym->flags & BSF_SECTION_SYM)
2395 if (s->bsym->section != seg)
2400 if (multibyte_handling == multibyte_warn_syms
2401 && ! s->flags.local_symbol
2402 && seg != undefined_section
2403 && ! s->flags.multibyte_warned
2404 && scan_for_multibyte_characters ((const unsigned char *) s->name,
2405 (const unsigned char *) s->name + strlen (s->name),
2408 as_warn (_("symbol '%s' contains multibyte characters"), s->name);
2409 s->flags.multibyte_warned = 1;
2412 s->bsym->section = seg;
2417 S_SET_EXTERNAL (symbolS *s)
2419 if (s->flags.local_symbol)
2420 s = local_symbol_convert (s);
2421 if ((s->bsym->flags & BSF_WEAK) != 0)
2423 /* Let .weak override .global. */
2426 if (s->bsym->flags & BSF_SECTION_SYM)
2428 /* Do not reassign section symbols. */
2429 as_warn (_("can't make section symbol global"));
2432 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2433 if (S_GET_SEGMENT (s) == reg_section)
2435 as_bad (_("can't make register symbol global"));
2439 s->bsym->flags |= BSF_GLOBAL;
2440 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2443 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2444 an_external_name = S_GET_NAME (s);
2449 S_CLEAR_EXTERNAL (symbolS *s)
2451 if (s->flags.local_symbol)
2453 if ((s->bsym->flags & BSF_WEAK) != 0)
2455 /* Let .weak override. */
2458 s->bsym->flags |= BSF_LOCAL;
2459 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2463 S_SET_WEAK (symbolS *s)
2465 if (s->flags.local_symbol)
2466 s = local_symbol_convert (s);
2467 #ifdef obj_set_weak_hook
2468 obj_set_weak_hook (s);
2470 s->bsym->flags |= BSF_WEAK;
2471 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2475 S_SET_WEAKREFR (symbolS *s)
2477 if (s->flags.local_symbol)
2478 s = local_symbol_convert (s);
2479 s->flags.weakrefr = 1;
2480 /* If the alias was already used, make sure we mark the target as
2481 used as well, otherwise it might be dropped from the symbol
2482 table. This may have unintended side effects if the alias is
2483 later redirected to another symbol, such as keeping the unused
2484 previous target in the symbol table. Since it will be weak, it's
2487 symbol_mark_used (s->x->value.X_add_symbol);
2491 S_CLEAR_WEAKREFR (symbolS *s)
2493 if (s->flags.local_symbol)
2495 s->flags.weakrefr = 0;
2499 S_SET_WEAKREFD (symbolS *s)
2501 if (s->flags.local_symbol)
2502 s = local_symbol_convert (s);
2503 s->flags.weakrefd = 1;
2508 S_CLEAR_WEAKREFD (symbolS *s)
2510 if (s->flags.local_symbol)
2512 if (s->flags.weakrefd)
2514 s->flags.weakrefd = 0;
2515 /* If a weakref target symbol is weak, then it was never
2516 referenced directly before, not even in a .global directive,
2517 so decay it to local. If it remains undefined, it will be
2518 later turned into a global, like any other undefined
2520 if (s->bsym->flags & BSF_WEAK)
2522 #ifdef obj_clear_weak_hook
2523 obj_clear_weak_hook (s);
2525 s->bsym->flags &= ~BSF_WEAK;
2526 s->bsym->flags |= BSF_LOCAL;
2532 S_SET_THREAD_LOCAL (symbolS *s)
2534 if (s->flags.local_symbol)
2535 s = local_symbol_convert (s);
2536 if (bfd_is_com_section (s->bsym->section)
2537 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2539 s->bsym->flags |= BSF_THREAD_LOCAL;
2540 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2541 as_bad (_("Accessing function `%s' as thread-local object"),
2543 else if (! bfd_is_und_section (s->bsym->section)
2544 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2545 as_bad (_("Accessing `%s' as thread-local object"),
2550 S_SET_NAME (symbolS *s, const char *name)
2553 if (s->flags.local_symbol)
2555 s->bsym->name = name;
2559 S_SET_VOLATILE (symbolS *s)
2561 if (s->flags.local_symbol)
2562 s = local_symbol_convert (s);
2563 s->flags.volatil = 1;
2567 S_CLEAR_VOLATILE (symbolS *s)
2569 if (!s->flags.local_symbol)
2570 s->flags.volatil = 0;
2574 S_SET_FORWARD_REF (symbolS *s)
2576 if (s->flags.local_symbol)
2577 s = local_symbol_convert (s);
2578 s->flags.forward_ref = 1;
2581 /* Return the previous symbol in a chain. */
2584 symbol_previous (symbolS *s)
2586 if (s->flags.local_symbol)
2588 return s->x->previous;
2591 /* Return the next symbol in a chain. */
2594 symbol_next (symbolS *s)
2596 if (s->flags.local_symbol)
2601 /* Return a pointer to the value of a symbol as an expression. */
2604 symbol_get_value_expression (symbolS *s)
2606 if (s->flags.local_symbol)
2607 s = local_symbol_convert (s);
2608 return &s->x->value;
2611 /* Set the value of a symbol to an expression. */
2614 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2616 if (s->flags.local_symbol)
2617 s = local_symbol_convert (s);
2619 S_CLEAR_WEAKREFR (s);
2622 /* Return whether 2 symbols are the same. */
2625 symbol_same_p (symbolS *s1, symbolS *s2)
2630 /* Return a pointer to the X_add_number component of a symbol. */
2633 symbol_X_add_number (symbolS *s)
2635 if (s->flags.local_symbol)
2636 return (offsetT *) &((struct local_symbol *) s)->value;
2638 return &s->x->value.X_add_number;
2641 /* Set the value of SYM to the current position in the current segment. */
2644 symbol_set_value_now (symbolS *sym)
2646 S_SET_SEGMENT (sym, now_seg);
2647 S_SET_VALUE (sym, frag_now_fix ());
2648 symbol_set_frag (sym, frag_now);
2651 /* Set the frag of a symbol. */
2654 symbol_set_frag (symbolS *s, fragS *f)
2656 if (s->flags.local_symbol)
2658 ((struct local_symbol *) s)->frag = f;
2662 S_CLEAR_WEAKREFR (s);
2665 /* Return the frag of a symbol. */
2668 symbol_get_frag (symbolS *s)
2670 if (s->flags.local_symbol)
2671 return ((struct local_symbol *) s)->frag;
2675 /* Mark a symbol as having been used. */
2678 symbol_mark_used (symbolS *s)
2680 if (s->flags.local_symbol)
2683 if (S_IS_WEAKREFR (s))
2684 symbol_mark_used (s->x->value.X_add_symbol);
2687 /* Clear the mark of whether a symbol has been used. */
2690 symbol_clear_used (symbolS *s)
2692 if (s->flags.local_symbol)
2693 s = local_symbol_convert (s);
2697 /* Return whether a symbol has been used. */
2700 symbol_used_p (symbolS *s)
2702 if (s->flags.local_symbol)
2704 return s->flags.used;
2707 /* Mark a symbol as having been used in a reloc. */
2710 symbol_mark_used_in_reloc (symbolS *s)
2712 if (s->flags.local_symbol)
2713 s = local_symbol_convert (s);
2714 s->flags.used_in_reloc = 1;
2717 /* Clear the mark of whether a symbol has been used in a reloc. */
2720 symbol_clear_used_in_reloc (symbolS *s)
2722 if (s->flags.local_symbol)
2724 s->flags.used_in_reloc = 0;
2727 /* Return whether a symbol has been used in a reloc. */
2730 symbol_used_in_reloc_p (symbolS *s)
2732 if (s->flags.local_symbol)
2734 return s->flags.used_in_reloc;
2737 /* Mark a symbol as an MRI common symbol. */
2740 symbol_mark_mri_common (symbolS *s)
2742 if (s->flags.local_symbol)
2743 s = local_symbol_convert (s);
2744 s->flags.mri_common = 1;
2747 /* Clear the mark of whether a symbol is an MRI common symbol. */
2750 symbol_clear_mri_common (symbolS *s)
2752 if (s->flags.local_symbol)
2754 s->flags.mri_common = 0;
2757 /* Return whether a symbol is an MRI common symbol. */
2760 symbol_mri_common_p (symbolS *s)
2762 if (s->flags.local_symbol)
2764 return s->flags.mri_common;
2767 /* Mark a symbol as having been written. */
2770 symbol_mark_written (symbolS *s)
2772 if (s->flags.local_symbol)
2774 s->flags.written = 1;
2777 /* Clear the mark of whether a symbol has been written. */
2780 symbol_clear_written (symbolS *s)
2782 if (s->flags.local_symbol)
2784 s->flags.written = 0;
2787 /* Return whether a symbol has been written. */
2790 symbol_written_p (symbolS *s)
2792 if (s->flags.local_symbol)
2794 return s->flags.written;
2797 /* Mark a symbol as to be removed. */
2800 symbol_mark_removed (symbolS *s)
2802 if (s->flags.local_symbol)
2804 s->flags.removed = 1;
2807 /* Return whether a symbol has been marked to be removed. */
2810 symbol_removed_p (symbolS *s)
2812 if (s->flags.local_symbol)
2814 return s->flags.removed;
2817 /* Mark a symbol has having been resolved. */
2820 symbol_mark_resolved (symbolS *s)
2822 s->flags.resolved = 1;
2825 /* Return whether a symbol has been resolved. */
2828 symbol_resolved_p (symbolS *s)
2830 return s->flags.resolved;
2833 /* Return whether a symbol is a section symbol. */
2836 symbol_section_p (symbolS *s)
2838 if (s->flags.local_symbol)
2840 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2843 /* Return whether a symbol is equated to another symbol. */
2846 symbol_equated_p (symbolS *s)
2848 if (s->flags.local_symbol)
2850 return s->x->value.X_op == O_symbol;
2853 /* Return whether a symbol is equated to another symbol, and should be
2854 treated specially when writing out relocs. */
2857 symbol_equated_reloc_p (symbolS *s)
2859 if (s->flags.local_symbol)
2861 /* X_op_symbol, normally not used for O_symbol, is set by
2862 resolve_symbol_value to flag expression syms that have been
2864 return (s->x->value.X_op == O_symbol
2865 #if defined (OBJ_COFF) && defined (TE_PE)
2868 && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
2869 || ! S_IS_DEFINED (s)
2870 || S_IS_COMMON (s)));
2873 /* Return whether a symbol has a constant value. */
2876 symbol_constant_p (symbolS *s)
2878 if (s->flags.local_symbol)
2880 return s->x->value.X_op == O_constant;
2883 /* Return whether a symbol was cloned and thus removed from the global
2887 symbol_shadow_p (symbolS *s)
2889 if (s->flags.local_symbol)
2891 return s->x->next == s;
2894 /* If S is a struct symbol return S, otherwise return NULL. */
2897 symbol_symbolS (symbolS *s)
2899 if (s->flags.local_symbol)
2904 /* Return the BFD symbol for a symbol. */
2907 symbol_get_bfdsym (symbolS *s)
2909 if (s->flags.local_symbol)
2910 s = local_symbol_convert (s);
2914 /* Set the BFD symbol for a symbol. */
2917 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2919 if (s->flags.local_symbol)
2920 s = local_symbol_convert (s);
2921 /* Usually, it is harmless to reset a symbol to a BFD section
2922 symbol. For example, obj_elf_change_section sets the BFD symbol
2923 of an old symbol with the newly created section symbol. But when
2924 we have multiple sections with the same name, the newly created
2925 section may have the same name as an old section. We check if the
2926 old symbol has been already marked as a section symbol before
2928 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2930 /* else XXX - What do we do now ? */
2933 #ifdef OBJ_SYMFIELD_TYPE
2935 /* Get a pointer to the object format information for a symbol. */
2938 symbol_get_obj (symbolS *s)
2940 if (s->flags.local_symbol)
2941 s = local_symbol_convert (s);
2945 /* Set the object format information for a symbol. */
2948 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2950 if (s->flags.local_symbol)
2951 s = local_symbol_convert (s);
2955 #endif /* OBJ_SYMFIELD_TYPE */
2957 #ifdef TC_SYMFIELD_TYPE
2959 /* Get a pointer to the processor information for a symbol. */
2962 symbol_get_tc (symbolS *s)
2964 if (s->flags.local_symbol)
2965 s = local_symbol_convert (s);
2969 /* Set the processor information for a symbol. */
2972 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2974 if (s->flags.local_symbol)
2975 s = local_symbol_convert (s);
2979 #endif /* TC_SYMFIELD_TYPE */
2984 symbol_lastP = NULL;
2985 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2986 sy_hash = htab_create_alloc (16, hash_symbol_entry, eq_symbol_entry,
2987 NULL, xcalloc, free);
2989 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2990 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
2992 abs_symbol.x = &abs_symbol_x;
2993 abs_symbol.x->value.X_op = O_constant;
2994 abs_symbol.frag = &zero_address_frag;
2996 if (LOCAL_LABELS_FB)
3001 dot_symbol_init (void)
3003 dot_symbol.name = ".";
3004 dot_symbol.flags.forward_ref = 1;
3005 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3006 if (dot_symbol.bsym == NULL)
3007 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3008 dot_symbol.bsym->name = ".";
3009 dot_symbol.x = &dot_symbol_x;
3010 dot_symbol.x->value.X_op = O_constant;
3015 /* Maximum indent level.
3016 Available for modification inside a gdb session. */
3017 static int max_indent_level = 8;
3020 print_symbol_value_1 (FILE *file, symbolS *sym)
3022 const char *name = S_GET_NAME (sym);
3023 if (!name || !name[0])
3025 fprintf (file, "sym ");
3026 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
3027 fprintf (file, " %s", name);
3029 if (sym->flags.local_symbol)
3031 struct local_symbol *locsym = (struct local_symbol *) sym;
3033 if (locsym->frag != &zero_address_frag
3034 && locsym->frag != NULL)
3036 fprintf (file, " frag ");
3037 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) locsym->frag));
3039 if (locsym->flags.resolved)
3040 fprintf (file, " resolved");
3041 fprintf (file, " local");
3045 if (sym->frag != &zero_address_frag)
3047 fprintf (file, " frag ");
3048 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->frag));
3050 if (sym->flags.written)
3051 fprintf (file, " written");
3052 if (sym->flags.resolved)
3053 fprintf (file, " resolved");
3054 else if (sym->flags.resolving)
3055 fprintf (file, " resolving");
3056 if (sym->flags.used_in_reloc)
3057 fprintf (file, " used-in-reloc");
3058 if (sym->flags.used)
3059 fprintf (file, " used");
3060 if (S_IS_LOCAL (sym))
3061 fprintf (file, " local");
3062 if (S_IS_EXTERNAL (sym))
3063 fprintf (file, " extern");
3064 if (S_IS_WEAK (sym))
3065 fprintf (file, " weak");
3066 if (S_IS_DEBUG (sym))
3067 fprintf (file, " debug");
3068 if (S_IS_DEFINED (sym))
3069 fprintf (file, " defined");
3071 if (S_IS_WEAKREFR (sym))
3072 fprintf (file, " weakrefr");
3073 if (S_IS_WEAKREFD (sym))
3074 fprintf (file, " weakrefd");
3075 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3076 if (symbol_resolved_p (sym))
3078 segT s = S_GET_SEGMENT (sym);
3080 if (s != undefined_section
3081 && s != expr_section)
3082 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3084 else if (indent_level < max_indent_level
3085 && S_GET_SEGMENT (sym) != undefined_section)
3088 fprintf (file, "\n%*s<", indent_level * 4, "");
3089 if (sym->flags.local_symbol)
3090 fprintf (file, "constant %lx",
3091 (unsigned long) ((struct local_symbol *) sym)->value);
3093 print_expr_1 (file, &sym->x->value);
3094 fprintf (file, ">");
3101 print_symbol_value (symbolS *sym)
3104 print_symbol_value_1 (stderr, sym);
3105 fprintf (stderr, "\n");
3109 print_binary (FILE *file, const char *name, expressionS *exp)
3112 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3113 print_symbol_value_1 (file, exp->X_add_symbol);
3114 fprintf (file, ">\n%*s<", indent_level * 4, "");
3115 print_symbol_value_1 (file, exp->X_op_symbol);
3116 fprintf (file, ">");
3121 print_expr_1 (FILE *file, expressionS *exp)
3123 fprintf (file, "expr ");
3124 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
3125 fprintf (file, " ");
3129 fprintf (file, "illegal");
3132 fprintf (file, "absent");
3135 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
3139 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3140 print_symbol_value_1 (file, exp->X_add_symbol);
3141 fprintf (file, ">");
3143 if (exp->X_add_number)
3144 fprintf (file, "\n%*s%lx", indent_level * 4, "",
3145 (unsigned long) exp->X_add_number);
3149 fprintf (file, "register #%d", (int) exp->X_add_number);
3152 fprintf (file, "big");
3155 fprintf (file, "uminus -<");
3157 print_symbol_value_1 (file, exp->X_add_symbol);
3158 fprintf (file, ">");
3159 goto maybe_print_addnum;
3161 fprintf (file, "bit_not");
3164 print_binary (file, "multiply", exp);
3167 print_binary (file, "divide", exp);
3170 print_binary (file, "modulus", exp);
3173 print_binary (file, "lshift", exp);
3176 print_binary (file, "rshift", exp);
3178 case O_bit_inclusive_or:
3179 print_binary (file, "bit_ior", exp);
3181 case O_bit_exclusive_or:
3182 print_binary (file, "bit_xor", exp);
3185 print_binary (file, "bit_and", exp);
3188 print_binary (file, "eq", exp);
3191 print_binary (file, "ne", exp);
3194 print_binary (file, "lt", exp);
3197 print_binary (file, "le", exp);
3200 print_binary (file, "ge", exp);
3203 print_binary (file, "gt", exp);
3206 print_binary (file, "logical_and", exp);
3209 print_binary (file, "logical_or", exp);
3213 fprintf (file, "add\n%*s<", indent_level * 4, "");
3214 print_symbol_value_1 (file, exp->X_add_symbol);
3215 fprintf (file, ">\n%*s<", indent_level * 4, "");
3216 print_symbol_value_1 (file, exp->X_op_symbol);
3217 fprintf (file, ">");
3218 goto maybe_print_addnum;
3221 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3222 print_symbol_value_1 (file, exp->X_add_symbol);
3223 fprintf (file, ">\n%*s<", indent_level * 4, "");
3224 print_symbol_value_1 (file, exp->X_op_symbol);
3225 fprintf (file, ">");
3226 goto maybe_print_addnum;
3228 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3235 print_expr (expressionS *exp)
3237 print_expr_1 (stderr, exp);
3238 fprintf (stderr, "\n");
3242 symbol_print_statistics (FILE *file)
3244 htab_print_statistics (file, "symbol table", sy_hash);
3245 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3246 local_symbol_count, local_symbol_conversion_count);
3249 #ifdef OBJ_COMPLEX_RELC
3251 /* Convert given symbol to a new complex-relocation symbol name. This
3252 may be a recursive function, since it might be called for non-leaf
3253 nodes (plain symbols) in the expression tree. The caller owns the
3254 returning string, so should free it eventually. Errors are
3255 indicated via as_bad and a NULL return value. The given symbol
3256 is marked with used_in_reloc. */
3259 symbol_relc_make_sym (symbolS * sym)
3261 char * terminal = NULL;
3266 gas_assert (sym != NULL);
3268 /* Recurse to symbol_relc_make_expr if this symbol
3269 is defined as an expression or a plain value. */
3270 if ( S_GET_SEGMENT (sym) == expr_section
3271 || S_GET_SEGMENT (sym) == absolute_section)
3272 return symbol_relc_make_expr (symbol_get_value_expression (sym));
3274 /* This may be a "fake symbol", referring to ".".
3275 Write out a special null symbol to refer to this position. */
3276 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3277 return xstrdup (".");
3279 /* We hope this is a plain leaf symbol. Construct the encoding
3280 as {S,s}II...:CCCCCCC....
3281 where 'S'/'s' means section symbol / plain symbol
3282 III is decimal for the symbol name length
3283 CCC is the symbol name itself. */
3284 symbol_mark_used_in_reloc (sym);
3286 sname = S_GET_NAME (sym);
3287 sname_len = strlen (sname);
3288 typetag = symbol_section_p (sym) ? 'S' : 's';
3290 terminal = XNEWVEC (char, (1 /* S or s */
3291 + 8 /* sname_len in decimal */
3293 + sname_len /* name itself */
3296 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3300 /* Convert given value to a new complex-relocation symbol name. This
3301 is a non-recursive function, since it is be called for leaf nodes
3302 (plain values) in the expression tree. The caller owns the
3303 returning string, so should free() it eventually. No errors. */
3306 symbol_relc_make_value (offsetT val)
3308 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3311 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3315 /* Convert given expression to a new complex-relocation symbol name.
3316 This is a recursive function, since it traverses the entire given
3317 expression tree. The caller owns the returning string, so should
3318 free() it eventually. Errors are indicated via as_bad() and a NULL
3322 symbol_relc_make_expr (expressionS * exp)
3324 const char * opstr = NULL; /* Operator prefix string. */
3325 int arity = 0; /* Arity of this operator. */
3326 char * operands[3]; /* Up to three operands. */
3327 char * concat_string = NULL;
3329 operands[0] = operands[1] = operands[2] = NULL;
3331 gas_assert (exp != NULL);
3333 /* Match known operators -> fill in opstr, arity, operands[] and fall
3334 through to construct subexpression fragments; may instead return
3335 string directly for leaf nodes. */
3337 /* See expr.h for the meaning of all these enums. Many operators
3338 have an unnatural arity (X_add_number implicitly added). The
3339 conversion logic expands them to explicit "+" subexpressions. */
3344 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3349 return symbol_relc_make_value (exp->X_add_number);
3352 if (exp->X_add_number)
3356 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3357 operands[1] = symbol_relc_make_value (exp->X_add_number);
3361 return symbol_relc_make_sym (exp->X_add_symbol);
3363 /* Helper macros for nesting nodes. */
3365 #define HANDLE_XADD_OPT1(str_) \
3366 if (exp->X_add_number) \
3369 opstr = "+:" str_; \
3370 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3371 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3378 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3382 #define HANDLE_XADD_OPT2(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_sym (exp->X_op_symbol); \
3389 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3395 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3396 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3400 /* Nesting nodes. */
3402 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3403 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3404 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3405 case O_multiply: HANDLE_XADD_OPT2 ("*");
3406 case O_divide: HANDLE_XADD_OPT2 ("/");
3407 case O_modulus: HANDLE_XADD_OPT2 ("%");
3408 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3409 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3410 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3411 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3412 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3413 case O_add: HANDLE_XADD_OPT2 ("+");
3414 case O_subtract: HANDLE_XADD_OPT2 ("-");
3415 case O_eq: HANDLE_XADD_OPT2 ("==");
3416 case O_ne: HANDLE_XADD_OPT2 ("!=");
3417 case O_lt: HANDLE_XADD_OPT2 ("<");
3418 case O_le: HANDLE_XADD_OPT2 ("<=");
3419 case O_ge: HANDLE_XADD_OPT2 (">=");
3420 case O_gt: HANDLE_XADD_OPT2 (">");
3421 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3422 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3425 /* Validate & reject early. */
3426 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3428 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3430 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3434 concat_string = NULL;
3435 else if (arity == 0)
3436 concat_string = xstrdup (opstr);
3437 else if (arity == 1)
3438 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3439 else if (arity == 2)
3440 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3443 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3444 operands[2], (char *) NULL);
3446 /* Free operand strings (not opstr). */
3447 if (arity >= 1) xfree (operands[0]);
3448 if (arity >= 2) xfree (operands[1]);
3449 if (arity >= 3) xfree (operands[2]);
3451 return concat_string;