1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2016 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" */
27 #include "struc-symbol.h"
29 /* This is non-zero if symbols are case sensitive, which is the
31 int symbols_case_sensitive = 1;
33 #ifndef WORKING_DOT_WORD
34 extern int new_broken_words;
37 /* symbol-name => struct symbol pointer */
38 static struct hash_control *sy_hash;
40 /* Table of local symbols. */
41 static struct hash_control *local_hash;
43 /* Below are commented in "symbols.h". */
44 symbolS *symbol_rootP;
45 symbolS *symbol_lastP;
50 #define debug_verify_symchain verify_symbol_chain
52 #define debug_verify_symchain(root, last) ((void) 0)
55 #define DOLLAR_LABEL_CHAR '\001'
56 #define LOCAL_LABEL_CHAR '\002'
58 #ifndef TC_LABEL_IS_LOCAL
59 #define TC_LABEL_IS_LOCAL(name) 0
64 /* The name of an external symbol which is
65 used to make weak PE symbol names unique. */
66 const char * an_external_name;
69 static char *save_symbol_name (const char *);
70 static void fb_label_init (void);
71 static long dollar_label_instance (long);
72 static long fb_label_instance (long);
74 static void print_binary (FILE *, const char *, expressionS *);
76 /* Return a pointer to a new symbol. Die if we can't make a new
77 symbol. Fill in the symbol's values. Add symbol to end of symbol
80 This function should be called in the general case of creating a
81 symbol. However, if the output file symbol table has already been
82 set, and you are certain that this symbol won't be wanted in the
83 output file, you can call symbol_create. */
86 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
88 symbolS *symbolP = symbol_create (name, segment, valu, frag);
90 /* Link to end of symbol chain. */
92 extern int symbol_table_frozen;
93 if (symbol_table_frozen)
96 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
101 /* Save a symbol name on a permanent obstack, and convert it according
102 to the object file format. */
105 save_symbol_name (const char *name)
110 name_length = strlen (name) + 1; /* +1 for \0. */
111 obstack_grow (¬es, name, name_length);
112 ret = (char *) obstack_finish (¬es);
114 #ifdef tc_canonicalize_symbol_name
115 ret = tc_canonicalize_symbol_name (ret);
118 if (! symbols_case_sensitive)
122 for (s = ret; *s != '\0'; s++)
130 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
131 segT segment, /* Segment identifier (SEG_<something>). */
132 valueT valu, /* Symbol value. */
133 fragS *frag /* Associated fragment. */)
135 char *preserved_copy_of_name;
138 preserved_copy_of_name = save_symbol_name (name);
140 symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
142 /* symbol must be born in some fixed state. This seems as good as any. */
143 memset (symbolP, 0, sizeof (symbolS));
145 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
146 if (symbolP->bsym == NULL)
147 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
148 S_SET_NAME (symbolP, preserved_copy_of_name);
150 S_SET_SEGMENT (symbolP, segment);
151 S_SET_VALUE (symbolP, valu);
152 symbol_clear_list_pointers (symbolP);
154 symbolP->sy_frag = frag;
156 obj_symbol_new_hook (symbolP);
158 #ifdef tc_symbol_new_hook
159 tc_symbol_new_hook (symbolP);
166 /* Local symbol support. If we can get away with it, we keep only a
167 small amount of information for local symbols. */
169 static symbolS *local_symbol_convert (struct local_symbol *);
171 /* Used for statistics. */
173 static unsigned long local_symbol_count;
174 static unsigned long local_symbol_conversion_count;
176 /* This macro is called with a symbol argument passed by reference.
177 It returns whether this is a local symbol. If necessary, it
178 changes its argument to the real symbol. */
180 #define LOCAL_SYMBOL_CHECK(s) \
181 (s->sy_flags.sy_local_symbol \
182 ? (local_symbol_converted_p ((struct local_symbol *) s) \
183 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
188 /* Create a local symbol and insert it into the local hash table. */
190 struct local_symbol *
191 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
194 struct local_symbol *ret;
196 ++local_symbol_count;
198 name_copy = save_symbol_name (name);
200 ret = (struct local_symbol *) obstack_alloc (¬es, sizeof *ret);
201 ret->lsy_flags.sy_local_symbol = 1;
202 ret->lsy_flags.sy_resolved = 0;
203 ret->lsy_name = name_copy;
204 ret->lsy_section = section;
205 local_symbol_set_frag (ret, frag);
206 ret->lsy_value = val;
208 hash_jam (local_hash, name_copy, (void *) ret);
213 /* Convert a local symbol into a real symbol. Note that we do not
214 reclaim the space used by the local symbol. */
217 local_symbol_convert (struct local_symbol *locsym)
221 gas_assert (locsym->lsy_flags.sy_local_symbol);
222 if (local_symbol_converted_p (locsym))
223 return local_symbol_get_real_symbol (locsym);
225 ++local_symbol_conversion_count;
227 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
228 local_symbol_get_frag (locsym));
230 if (local_symbol_resolved_p (locsym))
231 ret->sy_flags.sy_resolved = 1;
233 /* Local symbols are always either defined or used. */
234 ret->sy_flags.sy_used = 1;
236 #ifdef TC_LOCAL_SYMFIELD_CONVERT
237 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
240 symbol_table_insert (ret);
242 local_symbol_mark_converted (locsym);
243 local_symbol_set_real_symbol (locsym, ret);
245 hash_jam (local_hash, locsym->lsy_name, NULL);
251 define_sym_at_dot (symbolS *symbolP)
253 symbolP->sy_frag = frag_now;
254 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
255 S_SET_SEGMENT (symbolP, now_seg);
258 /* We have just seen "<name>:".
259 Creates a struct symbol unless it already exists.
261 Gripes if we are redefining a symbol incompatibly (and ignores it). */
264 colon (/* Just seen "x:" - rattle symbols & frags. */
265 const char *sym_name /* Symbol name, as a cannonical string. */
266 /* We copy this string: OK to alter later. */)
268 symbolS *symbolP; /* Symbol we are working with. */
270 /* Sun local labels go out of scope whenever a non-local symbol is
272 if (LOCAL_LABELS_DOLLAR
273 && !bfd_is_local_label_name (stdoutput, sym_name))
274 dollar_label_clear ();
276 #ifndef WORKING_DOT_WORD
277 if (new_broken_words)
279 struct broken_word *a;
284 if (now_seg == absolute_section)
286 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
290 possible_bytes = (md_short_jump_size
291 + new_broken_words * md_long_jump_size);
294 frag_opcode = frag_var (rs_broken_word,
298 (symbolS *) broken_words,
302 /* We want to store the pointer to where to insert the jump
303 table in the fr_opcode of the rs_broken_word frag. This
304 requires a little hackery. */
306 && (frag_tmp->fr_type != rs_broken_word
307 || frag_tmp->fr_opcode))
308 frag_tmp = frag_tmp->fr_next;
310 frag_tmp->fr_opcode = frag_opcode;
311 new_broken_words = 0;
313 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
314 a->dispfrag = frag_tmp;
316 #endif /* WORKING_DOT_WORD */
318 #ifdef obj_frob_colon
319 obj_frob_colon (sym_name);
322 if ((symbolP = symbol_find (sym_name)) != 0)
324 S_CLEAR_WEAKREFR (symbolP);
325 #ifdef RESOLVE_SYMBOL_REDEFINITION
326 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
329 /* Now check for undefined symbols. */
330 if (LOCAL_SYMBOL_CHECK (symbolP))
332 struct local_symbol *locsym = (struct local_symbol *) symbolP;
334 if (locsym->lsy_section != undefined_section
335 && (local_symbol_get_frag (locsym) != frag_now
336 || locsym->lsy_section != now_seg
337 || locsym->lsy_value != frag_now_fix ()))
339 as_bad (_("symbol `%s' is already defined"), sym_name);
343 locsym->lsy_section = now_seg;
344 local_symbol_set_frag (locsym, frag_now);
345 locsym->lsy_value = frag_now_fix ();
347 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
348 || S_IS_COMMON (symbolP)
349 || S_IS_VOLATILE (symbolP))
351 if (S_IS_VOLATILE (symbolP))
353 symbolP = symbol_clone (symbolP, 1);
354 S_SET_VALUE (symbolP, 0);
355 S_CLEAR_VOLATILE (symbolP);
357 if (S_GET_VALUE (symbolP) == 0)
359 define_sym_at_dot (symbolP);
362 #endif /* if we have one, it better be zero. */
367 /* There are still several cases to check:
369 A .comm/.lcomm symbol being redefined as initialized
372 A .comm/.lcomm symbol being redefined with a larger
375 This only used to be allowed on VMS gas, but Sun cc
376 on the sparc also depends on it. */
378 if (((!S_IS_DEBUG (symbolP)
379 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
380 && S_IS_EXTERNAL (symbolP))
381 || S_GET_SEGMENT (symbolP) == bss_section)
382 && (now_seg == data_section
383 || now_seg == bss_section
384 || now_seg == S_GET_SEGMENT (symbolP)))
386 /* Select which of the 2 cases this is. */
387 if (now_seg != data_section)
389 /* New .comm for prev .comm symbol.
391 If the new size is larger we just change its
392 value. If the new size is smaller, we ignore
394 if (S_GET_VALUE (symbolP)
395 < ((unsigned) frag_now_fix ()))
397 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
402 /* It is a .comm/.lcomm being converted to initialized
404 define_sym_at_dot (symbolP);
409 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
410 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
411 static const char *od_buf = "";
415 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
416 sprintf (od_buf, "%d.%d.",
417 S_GET_OTHER (symbolP),
418 S_GET_DESC (symbolP));
420 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
422 segment_name (S_GET_SEGMENT (symbolP)),
424 (long) S_GET_VALUE (symbolP));
426 } /* if the undefined symbol has no value */
430 /* Don't blow up if the definition is the same. */
431 if (!(frag_now == symbolP->sy_frag
432 && S_GET_VALUE (symbolP) == frag_now_fix ()
433 && S_GET_SEGMENT (symbolP) == now_seg))
435 as_bad (_("symbol `%s' is already defined"), sym_name);
436 symbolP = symbol_clone (symbolP, 0);
437 define_sym_at_dot (symbolP);
442 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
444 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
445 (valueT) frag_now_fix (),
450 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
453 symbol_table_insert (symbolP);
456 if (mri_common_symbol != NULL)
458 /* This symbol is actually being defined within an MRI common
459 section. This requires special handling. */
460 if (LOCAL_SYMBOL_CHECK (symbolP))
461 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
462 symbolP->sy_value.X_op = O_symbol;
463 symbolP->sy_value.X_add_symbol = mri_common_symbol;
464 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
465 symbolP->sy_frag = &zero_address_frag;
466 S_SET_SEGMENT (symbolP, expr_section);
467 symbolP->sy_flags.sy_mri_common = 1;
471 tc_frob_label (symbolP);
473 #ifdef obj_frob_label
474 obj_frob_label (symbolP);
480 /* Die if we can't insert the symbol. */
483 symbol_table_insert (symbolS *symbolP)
485 const char *error_string;
488 know (S_GET_NAME (symbolP));
490 if (LOCAL_SYMBOL_CHECK (symbolP))
492 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
494 if (error_string != NULL)
495 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
496 S_GET_NAME (symbolP), error_string);
500 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
502 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
503 S_GET_NAME (symbolP), error_string);
507 /* If a symbol name does not exist, create it as undefined, and insert
508 it into the symbol table. Return a pointer to it. */
511 symbol_find_or_make (const char *name)
515 symbolP = symbol_find (name);
519 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
521 symbolP = md_undefined_symbol ((char *) name);
525 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
531 symbolP = symbol_make (name);
533 symbol_table_insert (symbolP);
534 } /* if symbol wasn't found */
540 symbol_make (const char *name)
544 /* Let the machine description default it, e.g. for register names. */
545 symbolP = md_undefined_symbol ((char *) name);
548 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
554 symbol_clone (symbolS *orgsymP, int replace)
557 asymbol *bsymorg, *bsymnew;
559 /* Make sure we never clone the dot special symbol. */
560 gas_assert (orgsymP != &dot_symbol);
562 /* Running local_symbol_convert on a clone that's not the one currently
563 in local_hash would incorrectly replace the hash entry. Thus the
564 symbol must be converted here. Note that the rest of the function
565 depends on not encountering an unconverted symbol. */
566 if (LOCAL_SYMBOL_CHECK (orgsymP))
567 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
568 bsymorg = orgsymP->bsym;
570 newsymP = (symbolS *) obstack_alloc (¬es, sizeof (*newsymP));
572 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
574 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
575 newsymP->bsym = bsymnew;
576 bsymnew->name = bsymorg->name;
577 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
578 bsymnew->section = bsymorg->section;
579 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
580 bfd_asymbol_bfd (bsymnew), bsymnew);
582 #ifdef obj_symbol_clone_hook
583 obj_symbol_clone_hook (newsymP, orgsymP);
586 #ifdef tc_symbol_clone_hook
587 tc_symbol_clone_hook (newsymP, orgsymP);
592 if (symbol_rootP == orgsymP)
593 symbol_rootP = newsymP;
594 else if (orgsymP->sy_previous)
596 orgsymP->sy_previous->sy_next = newsymP;
597 orgsymP->sy_previous = NULL;
599 if (symbol_lastP == orgsymP)
600 symbol_lastP = newsymP;
601 else if (orgsymP->sy_next)
602 orgsymP->sy_next->sy_previous = newsymP;
604 /* Symbols that won't be output can't be external. */
605 S_CLEAR_EXTERNAL (orgsymP);
606 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
607 debug_verify_symchain (symbol_rootP, symbol_lastP);
609 symbol_table_insert (newsymP);
613 /* Symbols that won't be output can't be external. */
614 S_CLEAR_EXTERNAL (newsymP);
615 newsymP->sy_previous = newsymP->sy_next = newsymP;
621 /* Referenced symbols, if they are forward references, need to be cloned
622 (without replacing the original) so that the value of the referenced
623 symbols at the point of use . */
625 #undef symbol_clone_if_forward_ref
627 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
629 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
631 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
632 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
634 if (symbolP->sy_flags.sy_forward_ref)
639 /* assign_symbol() clones volatile symbols; pre-existing expressions
640 hold references to the original instance, but want the current
641 value. Just repeat the lookup. */
642 if (add_symbol && S_IS_VOLATILE (add_symbol))
643 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
644 if (op_symbol && S_IS_VOLATILE (op_symbol))
645 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
648 /* Re-using sy_resolving here, as this routine cannot get called from
649 symbol resolution code. */
650 if ((symbolP->bsym->section == expr_section
651 || symbolP->sy_flags.sy_forward_ref)
652 && !symbolP->sy_flags.sy_resolving)
654 symbolP->sy_flags.sy_resolving = 1;
655 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
656 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
657 symbolP->sy_flags.sy_resolving = 0;
660 if (symbolP->sy_flags.sy_forward_ref
661 || add_symbol != symbolP->sy_value.X_add_symbol
662 || op_symbol != symbolP->sy_value.X_op_symbol)
664 if (symbolP != &dot_symbol)
666 symbolP = symbol_clone (symbolP, 0);
667 symbolP->sy_flags.sy_resolving = 0;
671 symbolP = symbol_temp_new_now ();
672 #ifdef tc_new_dot_label
673 tc_new_dot_label (symbolP);
678 symbolP->sy_value.X_add_symbol = add_symbol;
679 symbolP->sy_value.X_op_symbol = op_symbol;
686 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
688 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
692 symbol_temp_new_now (void)
694 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
698 symbol_temp_make (void)
700 return symbol_make (FAKE_LABEL_NAME);
703 /* Implement symbol table lookup.
704 In: A symbol's name as a string: '\0' can't be part of a symbol name.
705 Out: NULL if the name was not in the symbol table, else the address
706 of a struct symbol associated with that name. */
709 symbol_find_exact (const char *name)
711 return symbol_find_exact_noref (name, 0);
715 symbol_find_exact_noref (const char *name, int noref)
717 struct local_symbol *locsym;
720 locsym = (struct local_symbol *) hash_find (local_hash, name);
722 return (symbolS *) locsym;
724 sym = ((symbolS *) hash_find (sy_hash, name));
726 /* Any references to the symbol, except for the reference in
727 .weakref, must clear this flag, such that the symbol does not
728 turn into a weak symbol. Note that we don't have to handle the
729 local_symbol case, since a weakrefd is always promoted out of the
730 local_symbol table when it is turned into a weak symbol. */
732 S_CLEAR_WEAKREFD (sym);
738 symbol_find (const char *name)
740 return symbol_find_noref (name, 0);
744 symbol_find_noref (const char *name, int noref)
749 #ifdef tc_canonicalize_symbol_name
751 copy = xstrdup (name);
752 name = tc_canonicalize_symbol_name (copy);
756 if (! symbols_case_sensitive)
765 name = copy = xmalloc (strlen (name) + 1);
767 while ((c = *orig++) != '\0')
768 *copy++ = TOUPPER (c);
773 copy = (char *) name;
776 result = symbol_find_exact_noref (name, noref);
782 /* Once upon a time, symbols were kept in a singly linked list. At
783 least coff needs to be able to rearrange them from time to time, for
784 which a doubly linked list is much more convenient. Loic did these
785 as macros which seemed dangerous to me so they're now functions.
788 /* Link symbol ADDME after symbol TARGET in the chain. */
791 symbol_append (symbolS *addme, symbolS *target,
792 symbolS **rootPP, symbolS **lastPP)
794 if (LOCAL_SYMBOL_CHECK (addme))
796 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
801 know (*rootPP == NULL);
802 know (*lastPP == NULL);
803 addme->sy_next = NULL;
804 addme->sy_previous = NULL;
808 } /* if the list is empty */
810 if (target->sy_next != NULL)
812 target->sy_next->sy_previous = addme;
816 know (*lastPP == target);
818 } /* if we have a next */
820 addme->sy_next = target->sy_next;
821 target->sy_next = addme;
822 addme->sy_previous = target;
824 debug_verify_symchain (symbol_rootP, symbol_lastP);
827 /* Set the chain pointers of SYMBOL to null. */
830 symbol_clear_list_pointers (symbolS *symbolP)
832 if (LOCAL_SYMBOL_CHECK (symbolP))
834 symbolP->sy_next = NULL;
835 symbolP->sy_previous = NULL;
838 /* Remove SYMBOLP from the list. */
841 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
843 if (LOCAL_SYMBOL_CHECK (symbolP))
846 if (symbolP == *rootPP)
848 *rootPP = symbolP->sy_next;
849 } /* if it was the root */
851 if (symbolP == *lastPP)
853 *lastPP = symbolP->sy_previous;
854 } /* if it was the tail */
856 if (symbolP->sy_next != NULL)
858 symbolP->sy_next->sy_previous = symbolP->sy_previous;
861 if (symbolP->sy_previous != NULL)
863 symbolP->sy_previous->sy_next = symbolP->sy_next;
866 debug_verify_symchain (*rootPP, *lastPP);
869 /* Link symbol ADDME before symbol TARGET in the chain. */
872 symbol_insert (symbolS *addme, symbolS *target,
873 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
875 if (LOCAL_SYMBOL_CHECK (addme))
877 if (LOCAL_SYMBOL_CHECK (target))
880 if (target->sy_previous != NULL)
882 target->sy_previous->sy_next = addme;
886 know (*rootPP == target);
890 addme->sy_previous = target->sy_previous;
891 target->sy_previous = addme;
892 addme->sy_next = target;
894 debug_verify_symchain (*rootPP, *lastPP);
898 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
900 symbolS *symbolP = rootP;
905 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
907 gas_assert (symbolP->bsym != NULL);
908 gas_assert (symbolP->sy_flags.sy_local_symbol == 0);
909 gas_assert (symbolP->sy_next->sy_previous == symbolP);
912 gas_assert (lastP == symbolP);
915 #ifdef OBJ_COMPLEX_RELC
918 use_complex_relocs_for (symbolS * symp)
920 switch (symp->sy_value.X_op)
930 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
931 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
933 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
934 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
943 case O_bit_inclusive_or:
945 case O_bit_exclusive_or:
958 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
959 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
961 (S_IS_COMMON (symp->sy_value.X_op_symbol)
962 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
964 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
965 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
966 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
967 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
979 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
983 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
984 segT seg_right = S_GET_SEGMENT (right);
993 case O_uminus: opname = "-"; break;
994 case O_bit_not: opname = "~"; break;
995 case O_logical_not: opname = "!"; break;
996 case O_multiply: opname = "*"; break;
997 case O_divide: opname = "/"; break;
998 case O_modulus: opname = "%"; break;
999 case O_left_shift: opname = "<<"; break;
1000 case O_right_shift: opname = ">>"; break;
1001 case O_bit_inclusive_or: opname = "|"; break;
1002 case O_bit_or_not: opname = "|~"; break;
1003 case O_bit_exclusive_or: opname = "^"; break;
1004 case O_bit_and: opname = "&"; break;
1005 case O_add: opname = "+"; break;
1006 case O_subtract: opname = "-"; break;
1007 case O_eq: opname = "=="; break;
1008 case O_ne: opname = "!="; break;
1009 case O_lt: opname = "<"; break;
1010 case O_le: opname = "<="; break;
1011 case O_ge: opname = ">="; break;
1012 case O_gt: opname = ">"; break;
1013 case O_logical_and: opname = "&&"; break;
1014 case O_logical_or: opname = "||"; break;
1017 if (expr_symbol_where (symp, &file, &line))
1020 as_bad_where (file, line,
1021 _("invalid operands (%s and %s sections) for `%s'"),
1022 seg_left->name, seg_right->name, opname);
1024 as_bad_where (file, line,
1025 _("invalid operand (%s section) for `%s'"),
1026 seg_right->name, opname);
1030 const char *sname = S_GET_NAME (symp);
1033 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1034 seg_left->name, seg_right->name, opname, sname);
1036 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1037 seg_right->name, opname, sname);
1041 /* Resolve the value of a symbol. This is called during the final
1042 pass over the symbol table to resolve any symbols with complex
1046 resolve_symbol_value (symbolS *symp)
1049 valueT final_val = 0;
1052 if (LOCAL_SYMBOL_CHECK (symp))
1054 struct local_symbol *locsym = (struct local_symbol *) symp;
1056 final_val = locsym->lsy_value;
1057 if (local_symbol_resolved_p (locsym))
1060 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1064 locsym->lsy_value = final_val;
1065 local_symbol_mark_resolved (locsym);
1071 if (symp->sy_flags.sy_resolved)
1073 if (symp->sy_value.X_op == O_constant)
1074 return (valueT) symp->sy_value.X_add_number;
1080 final_seg = S_GET_SEGMENT (symp);
1082 if (symp->sy_flags.sy_resolving)
1085 as_bad (_("symbol definition loop encountered at `%s'"),
1090 #ifdef OBJ_COMPLEX_RELC
1091 else if (final_seg == expr_section
1092 && use_complex_relocs_for (symp))
1094 symbolS * relc_symbol = NULL;
1095 char * relc_symbol_name = NULL;
1097 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1099 /* For debugging, print out conversion input & output. */
1101 print_expr (& symp->sy_value);
1102 if (relc_symbol_name)
1103 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1106 if (relc_symbol_name != NULL)
1107 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1108 0, & zero_address_frag);
1110 if (relc_symbol == NULL)
1112 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1118 symbol_table_insert (relc_symbol);
1120 /* S_CLEAR_EXTERNAL (relc_symbol); */
1121 if (symp->bsym->flags & BSF_SRELC)
1122 relc_symbol->bsym->flags |= BSF_SRELC;
1124 relc_symbol->bsym->flags |= BSF_RELC;
1125 /* symp->bsym->flags |= BSF_RELC; */
1126 copy_symbol_attributes (symp, relc_symbol);
1127 symp->sy_value.X_op = O_symbol;
1128 symp->sy_value.X_add_symbol = relc_symbol;
1129 symp->sy_value.X_add_number = 0;
1133 final_seg = undefined_section;
1134 goto exit_dont_set_value;
1139 symbolS *add_symbol, *op_symbol;
1140 offsetT left, right;
1141 segT seg_left, seg_right;
1145 symp->sy_flags.sy_resolving = 1;
1147 /* Help out with CSE. */
1148 add_symbol = symp->sy_value.X_add_symbol;
1149 op_symbol = symp->sy_value.X_op_symbol;
1150 final_val = symp->sy_value.X_add_number;
1151 op = symp->sy_value.X_op;
1164 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1165 if (final_seg == expr_section)
1166 final_seg = absolute_section;
1175 left = resolve_symbol_value (add_symbol);
1176 seg_left = S_GET_SEGMENT (add_symbol);
1178 symp->sy_value.X_op_symbol = NULL;
1181 if (S_IS_WEAKREFR (symp))
1183 gas_assert (final_val == 0);
1184 if (S_IS_WEAKREFR (add_symbol))
1186 gas_assert (add_symbol->sy_value.X_op == O_symbol
1187 && add_symbol->sy_value.X_add_number == 0);
1188 add_symbol = add_symbol->sy_value.X_add_symbol;
1189 gas_assert (! S_IS_WEAKREFR (add_symbol));
1190 symp->sy_value.X_add_symbol = add_symbol;
1194 if (symp->sy_flags.sy_mri_common)
1196 /* This is a symbol inside an MRI common section. The
1197 relocation routines are going to handle it specially.
1198 Don't change the value. */
1199 resolved = symbol_resolved_p (add_symbol);
1203 if (finalize_syms && final_val == 0)
1205 if (LOCAL_SYMBOL_CHECK (add_symbol))
1206 add_symbol = local_symbol_convert ((struct local_symbol *)
1208 copy_symbol_attributes (symp, add_symbol);
1211 /* If we have equated this symbol to an undefined or common
1212 symbol, keep X_op set to O_symbol, and don't change
1213 X_add_number. This permits the routine which writes out
1214 relocation to detect this case, and convert the
1215 relocation to be against the symbol to which this symbol
1217 if (! S_IS_DEFINED (add_symbol)
1218 #if defined (OBJ_COFF) && defined (TE_PE)
1219 || S_IS_WEAK (add_symbol)
1221 || S_IS_COMMON (add_symbol))
1225 symp->sy_value.X_op = O_symbol;
1226 symp->sy_value.X_add_symbol = add_symbol;
1227 symp->sy_value.X_add_number = final_val;
1228 /* Use X_op_symbol as a flag. */
1229 symp->sy_value.X_op_symbol = add_symbol;
1231 final_seg = seg_left;
1233 resolved = symbol_resolved_p (add_symbol);
1234 symp->sy_flags.sy_resolving = 0;
1235 goto exit_dont_set_value;
1237 else if (finalize_syms
1238 && ((final_seg == expr_section && seg_left != expr_section)
1239 || symbol_shadow_p (symp)))
1241 /* If the symbol is an expression symbol, do similarly
1242 as for undefined and common syms above. Handles
1243 "sym +/- expr" where "expr" cannot be evaluated
1244 immediately, and we want relocations to be against
1245 "sym", eg. because it is weak. */
1246 symp->sy_value.X_op = O_symbol;
1247 symp->sy_value.X_add_symbol = add_symbol;
1248 symp->sy_value.X_add_number = final_val;
1249 symp->sy_value.X_op_symbol = add_symbol;
1250 final_seg = seg_left;
1251 final_val += symp->sy_frag->fr_address + left;
1252 resolved = symbol_resolved_p (add_symbol);
1253 symp->sy_flags.sy_resolving = 0;
1254 goto exit_dont_set_value;
1258 final_val += symp->sy_frag->fr_address + left;
1259 if (final_seg == expr_section || final_seg == undefined_section)
1260 final_seg = seg_left;
1263 resolved = symbol_resolved_p (add_symbol);
1264 if (S_IS_WEAKREFR (symp))
1265 goto exit_dont_set_value;
1271 left = resolve_symbol_value (add_symbol);
1272 seg_left = S_GET_SEGMENT (add_symbol);
1274 /* By reducing these to the relevant dyadic operator, we get
1275 !S -> S == 0 permitted on anything,
1276 -S -> 0 - S only permitted on absolute
1277 ~S -> S ^ ~0 only permitted on absolute */
1278 if (op != O_logical_not && seg_left != absolute_section
1280 report_op_error (symp, NULL, op, add_symbol);
1282 if (final_seg == expr_section || final_seg == undefined_section)
1283 final_seg = absolute_section;
1287 else if (op == O_logical_not)
1292 final_val += left + symp->sy_frag->fr_address;
1294 resolved = symbol_resolved_p (add_symbol);
1302 case O_bit_inclusive_or:
1304 case O_bit_exclusive_or:
1316 left = resolve_symbol_value (add_symbol);
1317 right = resolve_symbol_value (op_symbol);
1318 seg_left = S_GET_SEGMENT (add_symbol);
1319 seg_right = S_GET_SEGMENT (op_symbol);
1321 /* Simplify addition or subtraction of a constant by folding the
1322 constant into X_add_number. */
1325 if (seg_right == absolute_section)
1330 else if (seg_left == absolute_section)
1333 add_symbol = op_symbol;
1335 seg_left = seg_right;
1339 else if (op == O_subtract)
1341 if (seg_right == absolute_section)
1349 /* Equality and non-equality tests are permitted on anything.
1350 Subtraction, and other comparison operators are permitted if
1351 both operands are in the same section. Otherwise, both
1352 operands must be absolute. We already handled the case of
1353 addition or subtraction of a constant above. This will
1354 probably need to be changed for an object file format which
1355 supports arbitrary expressions, such as IEEE-695. */
1356 if (!(seg_left == absolute_section
1357 && seg_right == absolute_section)
1358 && !(op == O_eq || op == O_ne)
1359 && !((op == O_subtract
1360 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1361 && seg_left == seg_right
1362 && (seg_left != undefined_section
1363 || add_symbol == op_symbol)))
1365 /* Don't emit messages unless we're finalizing the symbol value,
1366 otherwise we may get the same message multiple times. */
1368 report_op_error (symp, add_symbol, op, op_symbol);
1369 /* However do not move the symbol into the absolute section
1370 if it cannot currently be resolved - this would confuse
1371 other parts of the assembler into believing that the
1372 expression had been evaluated to zero. */
1378 && (final_seg == expr_section || final_seg == undefined_section))
1379 final_seg = absolute_section;
1381 /* Check for division by zero. */
1382 if ((op == O_divide || op == O_modulus) && right == 0)
1384 /* If seg_right is not absolute_section, then we've
1385 already issued a warning about using a bad symbol. */
1386 if (seg_right == absolute_section && finalize_syms)
1391 if (expr_symbol_where (symp, &file, &line))
1392 as_bad_where (file, line, _("division by zero"));
1394 as_bad (_("division by zero when setting `%s'"),
1401 switch (symp->sy_value.X_op)
1403 case O_multiply: left *= right; break;
1404 case O_divide: left /= right; break;
1405 case O_modulus: left %= right; break;
1406 case O_left_shift: left <<= right; break;
1407 case O_right_shift: left >>= right; break;
1408 case O_bit_inclusive_or: left |= right; break;
1409 case O_bit_or_not: left |= ~right; break;
1410 case O_bit_exclusive_or: left ^= right; break;
1411 case O_bit_and: left &= right; break;
1412 case O_add: left += right; break;
1413 case O_subtract: left -= right; break;
1416 left = (left == right && seg_left == seg_right
1417 && (seg_left != undefined_section
1418 || add_symbol == op_symbol)
1419 ? ~ (offsetT) 0 : 0);
1420 if (symp->sy_value.X_op == O_ne)
1423 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1424 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1425 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1426 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1427 case O_logical_and: left = left && right; break;
1428 case O_logical_or: left = left || right; break;
1432 final_val += symp->sy_frag->fr_address + left;
1433 if (final_seg == expr_section || final_seg == undefined_section)
1435 if (seg_left == undefined_section
1436 || seg_right == undefined_section)
1437 final_seg = undefined_section;
1438 else if (seg_left == absolute_section)
1439 final_seg = seg_right;
1441 final_seg = seg_left;
1443 resolved = (symbol_resolved_p (add_symbol)
1444 && symbol_resolved_p (op_symbol));
1449 /* Give an error (below) if not in expr_section. We don't
1450 want to worry about expr_section symbols, because they
1451 are fictional (they are created as part of expression
1452 resolution), and any problems may not actually mean
1457 symp->sy_flags.sy_resolving = 0;
1461 S_SET_VALUE (symp, final_val);
1463 exit_dont_set_value:
1464 /* Always set the segment, even if not finalizing the value.
1465 The segment is used to determine whether a symbol is defined. */
1466 S_SET_SEGMENT (symp, final_seg);
1468 /* Don't worry if we can't resolve an expr_section symbol. */
1472 symp->sy_flags.sy_resolved = 1;
1473 else if (S_GET_SEGMENT (symp) != expr_section)
1475 as_bad (_("can't resolve value for symbol `%s'"),
1477 symp->sy_flags.sy_resolved = 1;
1484 static void resolve_local_symbol (const char *, void *);
1486 /* A static function passed to hash_traverse. */
1489 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1492 resolve_symbol_value ((symbolS *) value);
1495 /* Resolve all local symbols. */
1498 resolve_local_symbol_values (void)
1500 hash_traverse (local_hash, resolve_local_symbol);
1503 /* Obtain the current value of a symbol without changing any
1504 sub-expressions used. */
1507 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1509 symbolS *symbolP = *symbolPP;
1511 if (LOCAL_SYMBOL_CHECK (symbolP))
1513 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1515 *valueP = locsym->lsy_value;
1516 *segP = locsym->lsy_section;
1517 *fragPP = local_symbol_get_frag (locsym);
1521 expressionS exp = symbolP->sy_value;
1523 if (!symbolP->sy_flags.sy_resolved && exp.X_op != O_illegal)
1527 if (symbolP->sy_flags.sy_resolving)
1529 symbolP->sy_flags.sy_resolving = 1;
1530 resolved = resolve_expression (&exp);
1531 symbolP->sy_flags.sy_resolving = 0;
1539 if (!symbol_equated_p (symbolP))
1544 symbolP = exp.X_add_symbol;
1551 *symbolPP = symbolP;
1552 *valueP = exp.X_add_number;
1553 *segP = symbolP->bsym->section;
1554 *fragPP = symbolP->sy_frag;
1556 if (*segP == expr_section)
1559 case O_constant: *segP = absolute_section; break;
1560 case O_register: *segP = reg_section; break;
1568 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1569 They are *really* local. That is, they go out of scope whenever we see a
1570 label that isn't local. Also, like fb labels, there can be multiple
1571 instances of a dollar label. Therefor, we name encode each instance with
1572 the instance number, keep a list of defined symbols separate from the real
1573 symbol table, and we treat these buggers as a sparse array. */
1575 static long *dollar_labels;
1576 static long *dollar_label_instances;
1577 static char *dollar_label_defines;
1578 static unsigned long dollar_label_count;
1579 static unsigned long dollar_label_max;
1582 dollar_label_defined (long label)
1586 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1588 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1590 return dollar_label_defines[i - dollar_labels];
1592 /* If we get here, label isn't defined. */
1597 dollar_label_instance (long label)
1601 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1603 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1605 return (dollar_label_instances[i - dollar_labels]);
1607 /* If we get here, we haven't seen the label before.
1608 Therefore its instance count is zero. */
1613 dollar_label_clear (void)
1615 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1618 #define DOLLAR_LABEL_BUMP_BY 10
1621 define_dollar_label (long label)
1625 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1628 ++dollar_label_instances[i - dollar_labels];
1629 dollar_label_defines[i - dollar_labels] = 1;
1633 /* If we get to here, we don't have label listed yet. */
1635 if (dollar_labels == NULL)
1637 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1638 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1639 dollar_label_defines = (char *) xmalloc (DOLLAR_LABEL_BUMP_BY);
1640 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1641 dollar_label_count = 0;
1643 else if (dollar_label_count == dollar_label_max)
1645 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1646 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1647 dollar_label_max * sizeof (long));
1648 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1649 dollar_label_max * sizeof (long));
1650 dollar_label_defines = (char *) xrealloc (dollar_label_defines, dollar_label_max);
1651 } /* if we needed to grow */
1653 dollar_labels[dollar_label_count] = label;
1654 dollar_label_instances[dollar_label_count] = 1;
1655 dollar_label_defines[dollar_label_count] = 1;
1656 ++dollar_label_count;
1659 /* Caller must copy returned name: we re-use the area for the next name.
1661 The mth occurence of label n: is turned into the symbol "Ln^Am"
1662 where n is the label number and m is the instance number. "L" makes
1663 it a label discarded unless debugging and "^A"('\1') ensures no
1664 ordinary symbol SHOULD get the same name as a local label
1665 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1667 fb labels get the same treatment, except that ^B is used in place
1670 char * /* Return local label name. */
1671 dollar_label_name (long n, /* we just saw "n$:" : n a number. */
1672 int augend /* 0 for current instance, 1 for new instance. */)
1675 /* Returned to caller, then copied. Used for created names ("4f"). */
1676 static char symbol_name_build[24];
1679 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1682 know (augend == 0 || augend == 1);
1683 p = symbol_name_build;
1684 #ifdef LOCAL_LABEL_PREFIX
1685 *p++ = LOCAL_LABEL_PREFIX;
1689 /* Next code just does sprintf( {}, "%d", n); */
1691 q = symbol_name_temporary;
1692 for (*q++ = 0, i = n; i; ++q)
1697 while ((*p = *--q) != '\0')
1700 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1702 /* Instance number. */
1703 q = symbol_name_temporary;
1704 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1709 while ((*p++ = *--q) != '\0');
1711 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1712 return symbol_name_build;
1715 /* Somebody else's idea of local labels. They are made by "n:" where n
1716 is any decimal digit. Refer to them with
1717 "nb" for previous (backward) n:
1718 or "nf" for next (forward) n:.
1720 We do a little better and let n be any number, not just a single digit, but
1721 since the other guy's assembler only does ten, we treat the first ten
1724 Like someone else's assembler, we have one set of local label counters for
1725 entire assembly, not one set per (sub)segment like in most assemblers. This
1726 implies that one can refer to a label in another segment, and indeed some
1727 crufty compilers have done just that.
1729 Since there could be a LOT of these things, treat them as a sparse
1732 #define FB_LABEL_SPECIAL (10)
1734 static long fb_low_counter[FB_LABEL_SPECIAL];
1735 static long *fb_labels;
1736 static long *fb_label_instances;
1737 static long fb_label_count;
1738 static long fb_label_max;
1740 /* This must be more than FB_LABEL_SPECIAL. */
1741 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1744 fb_label_init (void)
1746 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1749 /* Add one to the instance number of this fb label. */
1752 fb_label_instance_inc (long label)
1756 if ((unsigned long) label < FB_LABEL_SPECIAL)
1758 ++fb_low_counter[label];
1762 if (fb_labels != NULL)
1764 for (i = fb_labels + FB_LABEL_SPECIAL;
1765 i < fb_labels + fb_label_count; ++i)
1769 ++fb_label_instances[i - fb_labels];
1771 } /* if we find it */
1772 } /* for each existing label */
1775 /* If we get to here, we don't have label listed yet. */
1777 if (fb_labels == NULL)
1779 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1780 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1781 fb_label_max = FB_LABEL_BUMP_BY;
1782 fb_label_count = FB_LABEL_SPECIAL;
1785 else if (fb_label_count == fb_label_max)
1787 fb_label_max += FB_LABEL_BUMP_BY;
1788 fb_labels = (long *) xrealloc ((char *) fb_labels,
1789 fb_label_max * sizeof (long));
1790 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1791 fb_label_max * sizeof (long));
1792 } /* if we needed to grow */
1794 fb_labels[fb_label_count] = label;
1795 fb_label_instances[fb_label_count] = 1;
1800 fb_label_instance (long label)
1804 if ((unsigned long) label < FB_LABEL_SPECIAL)
1806 return (fb_low_counter[label]);
1809 if (fb_labels != NULL)
1811 for (i = fb_labels + FB_LABEL_SPECIAL;
1812 i < fb_labels + fb_label_count; ++i)
1816 return (fb_label_instances[i - fb_labels]);
1817 } /* if we find it */
1818 } /* for each existing label */
1821 /* We didn't find the label, so this must be a reference to the
1826 /* Caller must copy returned name: we re-use the area for the next name.
1828 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1829 where n is the label number and m is the instance number. "L" makes
1830 it a label discarded unless debugging and "^B"('\2') ensures no
1831 ordinary symbol SHOULD get the same name as a local label
1832 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1834 dollar labels get the same treatment, except that ^A is used in
1837 char * /* Return local label name. */
1838 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1839 long augend /* 0 for nb, 1 for n:, nf. */)
1842 /* Returned to caller, then copied. Used for created names ("4f"). */
1843 static char symbol_name_build[24];
1846 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1850 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1852 know ((unsigned long) augend <= 1);
1854 p = symbol_name_build;
1855 #ifdef LOCAL_LABEL_PREFIX
1856 *p++ = LOCAL_LABEL_PREFIX;
1860 /* Next code just does sprintf( {}, "%d", n); */
1862 q = symbol_name_temporary;
1863 for (*q++ = 0, i = n; i; ++q)
1868 while ((*p = *--q) != '\0')
1871 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1873 /* Instance number. */
1874 q = symbol_name_temporary;
1875 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1880 while ((*p++ = *--q) != '\0');
1882 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1883 return (symbol_name_build);
1886 /* Decode name that may have been generated by foo_label_name() above.
1887 If the name wasn't generated by foo_label_name(), then return it
1888 unaltered. This is used for error messages. */
1891 decode_local_label_name (char *s)
1894 char *symbol_decode;
1896 int instance_number;
1898 const char *message_format;
1901 #ifdef LOCAL_LABEL_PREFIX
1902 if (s[lindex] == LOCAL_LABEL_PREFIX)
1906 if (s[lindex] != 'L')
1909 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1910 label_number = (10 * label_number) + *p - '0';
1912 if (*p == DOLLAR_LABEL_CHAR)
1914 else if (*p == LOCAL_LABEL_CHAR)
1919 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1920 instance_number = (10 * instance_number) + *p - '0';
1922 message_format = _("\"%d\" (instance number %d of a %s label)");
1923 symbol_decode = (char *) obstack_alloc (¬es, strlen (message_format) + 30);
1924 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1926 return symbol_decode;
1929 /* Get the value of a symbol. */
1932 S_GET_VALUE (symbolS *s)
1934 if (LOCAL_SYMBOL_CHECK (s))
1935 return resolve_symbol_value (s);
1937 if (!s->sy_flags.sy_resolved)
1939 valueT val = resolve_symbol_value (s);
1943 if (S_IS_WEAKREFR (s))
1944 return S_GET_VALUE (s->sy_value.X_add_symbol);
1946 if (s->sy_value.X_op != O_constant)
1948 if (! s->sy_flags.sy_resolved
1949 || s->sy_value.X_op != O_symbol
1950 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1951 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1954 return (valueT) s->sy_value.X_add_number;
1957 /* Set the value of a symbol. */
1960 S_SET_VALUE (symbolS *s, valueT val)
1962 if (LOCAL_SYMBOL_CHECK (s))
1964 ((struct local_symbol *) s)->lsy_value = val;
1968 s->sy_value.X_op = O_constant;
1969 s->sy_value.X_add_number = (offsetT) val;
1970 s->sy_value.X_unsigned = 0;
1971 S_CLEAR_WEAKREFR (s);
1975 copy_symbol_attributes (symbolS *dest, symbolS *src)
1977 if (LOCAL_SYMBOL_CHECK (dest))
1978 dest = local_symbol_convert ((struct local_symbol *) dest);
1979 if (LOCAL_SYMBOL_CHECK (src))
1980 src = local_symbol_convert ((struct local_symbol *) src);
1982 /* In an expression, transfer the settings of these flags.
1983 The user can override later, of course. */
1984 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1985 | BSF_GNU_INDIRECT_FUNCTION)
1986 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1988 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1989 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1992 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1993 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
1998 S_IS_FUNCTION (symbolS *s)
2002 if (LOCAL_SYMBOL_CHECK (s))
2005 flags = s->bsym->flags;
2007 return (flags & BSF_FUNCTION) != 0;
2011 S_IS_EXTERNAL (symbolS *s)
2015 if (LOCAL_SYMBOL_CHECK (s))
2018 flags = s->bsym->flags;
2021 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2024 return (flags & BSF_GLOBAL) != 0;
2028 S_IS_WEAK (symbolS *s)
2030 if (LOCAL_SYMBOL_CHECK (s))
2032 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2033 could probably handle a WEAKREFR as always weak though. E.g., if
2034 the referenced symbol has lost its weak status, there's no reason
2035 to keep handling the weakrefr as if it was weak. */
2036 if (S_IS_WEAKREFR (s))
2037 return S_IS_WEAK (s->sy_value.X_add_symbol);
2038 return (s->bsym->flags & BSF_WEAK) != 0;
2042 S_IS_WEAKREFR (symbolS *s)
2044 if (LOCAL_SYMBOL_CHECK (s))
2046 return s->sy_flags.sy_weakrefr != 0;
2050 S_IS_WEAKREFD (symbolS *s)
2052 if (LOCAL_SYMBOL_CHECK (s))
2054 return s->sy_flags.sy_weakrefd != 0;
2058 S_IS_COMMON (symbolS *s)
2060 if (LOCAL_SYMBOL_CHECK (s))
2062 return bfd_is_com_section (s->bsym->section);
2066 S_IS_DEFINED (symbolS *s)
2068 if (LOCAL_SYMBOL_CHECK (s))
2069 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2070 return s->bsym->section != undefined_section;
2074 #ifndef EXTERN_FORCE_RELOC
2075 #define EXTERN_FORCE_RELOC IS_ELF
2078 /* Return true for symbols that should not be reduced to section
2079 symbols or eliminated from expressions, because they may be
2080 overridden by the linker. */
2082 S_FORCE_RELOC (symbolS *s, int strict)
2084 if (LOCAL_SYMBOL_CHECK (s))
2085 return ((struct local_symbol *) s)->lsy_section == undefined_section;
2088 && ((s->bsym->flags & BSF_WEAK) != 0
2089 || (EXTERN_FORCE_RELOC
2090 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2091 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0
2092 || s->bsym->section == undefined_section
2093 || bfd_is_com_section (s->bsym->section));
2097 S_IS_DEBUG (symbolS *s)
2099 if (LOCAL_SYMBOL_CHECK (s))
2101 if (s->bsym->flags & BSF_DEBUGGING)
2107 S_IS_LOCAL (symbolS *s)
2112 if (LOCAL_SYMBOL_CHECK (s))
2115 flags = s->bsym->flags;
2118 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2121 if (bfd_get_section (s->bsym) == reg_section)
2124 if (flag_strip_local_absolute
2125 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2126 the source file even when the object file is stripped. */
2127 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2128 && bfd_get_section (s->bsym) == absolute_section)
2131 name = S_GET_NAME (s);
2132 return (name != NULL
2134 && (strchr (name, DOLLAR_LABEL_CHAR)
2135 || strchr (name, LOCAL_LABEL_CHAR)
2136 || TC_LABEL_IS_LOCAL (name)
2137 || (! flag_keep_locals
2138 && (bfd_is_local_label (stdoutput, s->bsym)
2141 && name[1] == '?')))));
2145 S_IS_STABD (symbolS *s)
2147 return S_GET_NAME (s) == 0;
2151 S_CAN_BE_REDEFINED (const symbolS *s)
2153 if (LOCAL_SYMBOL_CHECK (s))
2154 return (local_symbol_get_frag ((struct local_symbol *) s)
2155 == &predefined_address_frag);
2156 /* Permit register names to be redefined. */
2157 return s->bsym->section == reg_section;
2161 S_IS_VOLATILE (const symbolS *s)
2163 if (LOCAL_SYMBOL_CHECK (s))
2165 return s->sy_flags.sy_volatile;
2169 S_IS_FORWARD_REF (const symbolS *s)
2171 if (LOCAL_SYMBOL_CHECK (s))
2173 return s->sy_flags.sy_forward_ref;
2177 S_GET_NAME (symbolS *s)
2179 if (LOCAL_SYMBOL_CHECK (s))
2180 return ((struct local_symbol *) s)->lsy_name;
2181 return s->bsym->name;
2185 S_GET_SEGMENT (symbolS *s)
2187 if (LOCAL_SYMBOL_CHECK (s))
2188 return ((struct local_symbol *) s)->lsy_section;
2189 return s->bsym->section;
2193 S_SET_SEGMENT (symbolS *s, segT seg)
2195 /* Don't reassign section symbols. The direct reason is to prevent seg
2196 faults assigning back to const global symbols such as *ABS*, but it
2197 shouldn't happen anyway. */
2199 if (LOCAL_SYMBOL_CHECK (s))
2201 if (seg == reg_section)
2202 s = local_symbol_convert ((struct local_symbol *) s);
2205 ((struct local_symbol *) s)->lsy_section = seg;
2210 if (s->bsym->flags & BSF_SECTION_SYM)
2212 if (s->bsym->section != seg)
2216 s->bsym->section = seg;
2220 S_SET_EXTERNAL (symbolS *s)
2222 if (LOCAL_SYMBOL_CHECK (s))
2223 s = local_symbol_convert ((struct local_symbol *) s);
2224 if ((s->bsym->flags & BSF_WEAK) != 0)
2226 /* Let .weak override .global. */
2229 if (s->bsym->flags & BSF_SECTION_SYM)
2231 /* Do not reassign section symbols. */
2232 as_warn (_("section symbols are already global"));
2235 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2236 if (S_GET_SEGMENT (s) == reg_section)
2238 as_bad ("can't make register symbol `%s' global",
2243 s->bsym->flags |= BSF_GLOBAL;
2244 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2247 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2248 an_external_name = S_GET_NAME (s);
2253 S_CLEAR_EXTERNAL (symbolS *s)
2255 if (LOCAL_SYMBOL_CHECK (s))
2257 if ((s->bsym->flags & BSF_WEAK) != 0)
2259 /* Let .weak override. */
2262 s->bsym->flags |= BSF_LOCAL;
2263 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2267 S_SET_WEAK (symbolS *s)
2269 if (LOCAL_SYMBOL_CHECK (s))
2270 s = local_symbol_convert ((struct local_symbol *) s);
2271 #ifdef obj_set_weak_hook
2272 obj_set_weak_hook (s);
2274 s->bsym->flags |= BSF_WEAK;
2275 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2279 S_SET_WEAKREFR (symbolS *s)
2281 if (LOCAL_SYMBOL_CHECK (s))
2282 s = local_symbol_convert ((struct local_symbol *) s);
2283 s->sy_flags.sy_weakrefr = 1;
2284 /* If the alias was already used, make sure we mark the target as
2285 used as well, otherwise it might be dropped from the symbol
2286 table. This may have unintended side effects if the alias is
2287 later redirected to another symbol, such as keeping the unused
2288 previous target in the symbol table. Since it will be weak, it's
2290 if (s->sy_flags.sy_used)
2291 symbol_mark_used (s->sy_value.X_add_symbol);
2295 S_CLEAR_WEAKREFR (symbolS *s)
2297 if (LOCAL_SYMBOL_CHECK (s))
2299 s->sy_flags.sy_weakrefr = 0;
2303 S_SET_WEAKREFD (symbolS *s)
2305 if (LOCAL_SYMBOL_CHECK (s))
2306 s = local_symbol_convert ((struct local_symbol *) s);
2307 s->sy_flags.sy_weakrefd = 1;
2312 S_CLEAR_WEAKREFD (symbolS *s)
2314 if (LOCAL_SYMBOL_CHECK (s))
2316 if (s->sy_flags.sy_weakrefd)
2318 s->sy_flags.sy_weakrefd = 0;
2319 /* If a weakref target symbol is weak, then it was never
2320 referenced directly before, not even in a .global directive,
2321 so decay it to local. If it remains undefined, it will be
2322 later turned into a global, like any other undefined
2324 if (s->bsym->flags & BSF_WEAK)
2326 #ifdef obj_clear_weak_hook
2327 obj_clear_weak_hook (s);
2329 s->bsym->flags &= ~BSF_WEAK;
2330 s->bsym->flags |= BSF_LOCAL;
2336 S_SET_THREAD_LOCAL (symbolS *s)
2338 if (LOCAL_SYMBOL_CHECK (s))
2339 s = local_symbol_convert ((struct local_symbol *) s);
2340 if (bfd_is_com_section (s->bsym->section)
2341 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2343 s->bsym->flags |= BSF_THREAD_LOCAL;
2344 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2345 as_bad (_("Accessing function `%s' as thread-local object"),
2347 else if (! bfd_is_und_section (s->bsym->section)
2348 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2349 as_bad (_("Accessing `%s' as thread-local object"),
2354 S_SET_NAME (symbolS *s, const char *name)
2356 if (LOCAL_SYMBOL_CHECK (s))
2358 ((struct local_symbol *) s)->lsy_name = name;
2361 s->bsym->name = name;
2365 S_SET_VOLATILE (symbolS *s)
2367 if (LOCAL_SYMBOL_CHECK (s))
2368 s = local_symbol_convert ((struct local_symbol *) s);
2369 s->sy_flags.sy_volatile = 1;
2373 S_CLEAR_VOLATILE (symbolS *s)
2375 if (!LOCAL_SYMBOL_CHECK (s))
2376 s->sy_flags.sy_volatile = 0;
2380 S_SET_FORWARD_REF (symbolS *s)
2382 if (LOCAL_SYMBOL_CHECK (s))
2383 s = local_symbol_convert ((struct local_symbol *) s);
2384 s->sy_flags.sy_forward_ref = 1;
2387 /* Return the previous symbol in a chain. */
2390 symbol_previous (symbolS *s)
2392 if (LOCAL_SYMBOL_CHECK (s))
2394 return s->sy_previous;
2397 /* Return the next symbol in a chain. */
2400 symbol_next (symbolS *s)
2402 if (LOCAL_SYMBOL_CHECK (s))
2407 /* Return a pointer to the value of a symbol as an expression. */
2410 symbol_get_value_expression (symbolS *s)
2412 if (LOCAL_SYMBOL_CHECK (s))
2413 s = local_symbol_convert ((struct local_symbol *) s);
2414 return &s->sy_value;
2417 /* Set the value of a symbol to an expression. */
2420 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2422 if (LOCAL_SYMBOL_CHECK (s))
2423 s = local_symbol_convert ((struct local_symbol *) s);
2425 S_CLEAR_WEAKREFR (s);
2428 /* Return whether 2 symbols are the same. */
2431 symbol_same_p (symbolS *s1, symbolS *s2)
2433 if (s1->sy_flags.sy_local_symbol
2434 && local_symbol_converted_p ((struct local_symbol *) s1))
2435 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2436 if (s2->sy_flags.sy_local_symbol
2437 && local_symbol_converted_p ((struct local_symbol *) s2))
2438 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2442 /* Return a pointer to the X_add_number component of a symbol. */
2445 symbol_X_add_number (symbolS *s)
2447 if (LOCAL_SYMBOL_CHECK (s))
2448 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2450 return &s->sy_value.X_add_number;
2453 /* Set the value of SYM to the current position in the current segment. */
2456 symbol_set_value_now (symbolS *sym)
2458 S_SET_SEGMENT (sym, now_seg);
2459 S_SET_VALUE (sym, frag_now_fix ());
2460 symbol_set_frag (sym, frag_now);
2463 /* Set the frag of a symbol. */
2466 symbol_set_frag (symbolS *s, fragS *f)
2468 if (LOCAL_SYMBOL_CHECK (s))
2470 local_symbol_set_frag ((struct local_symbol *) s, f);
2474 S_CLEAR_WEAKREFR (s);
2477 /* Return the frag of a symbol. */
2480 symbol_get_frag (symbolS *s)
2482 if (LOCAL_SYMBOL_CHECK (s))
2483 return local_symbol_get_frag ((struct local_symbol *) s);
2487 /* Mark a symbol as having been used. */
2490 symbol_mark_used (symbolS *s)
2492 if (LOCAL_SYMBOL_CHECK (s))
2494 s->sy_flags.sy_used = 1;
2495 if (S_IS_WEAKREFR (s))
2496 symbol_mark_used (s->sy_value.X_add_symbol);
2499 /* Clear the mark of whether a symbol has been used. */
2502 symbol_clear_used (symbolS *s)
2504 if (LOCAL_SYMBOL_CHECK (s))
2505 s = local_symbol_convert ((struct local_symbol *) s);
2506 s->sy_flags.sy_used = 0;
2509 /* Return whether a symbol has been used. */
2512 symbol_used_p (symbolS *s)
2514 if (LOCAL_SYMBOL_CHECK (s))
2516 return s->sy_flags.sy_used;
2519 /* Mark a symbol as having been used in a reloc. */
2522 symbol_mark_used_in_reloc (symbolS *s)
2524 if (LOCAL_SYMBOL_CHECK (s))
2525 s = local_symbol_convert ((struct local_symbol *) s);
2526 s->sy_flags.sy_used_in_reloc = 1;
2529 /* Clear the mark of whether a symbol has been used in a reloc. */
2532 symbol_clear_used_in_reloc (symbolS *s)
2534 if (LOCAL_SYMBOL_CHECK (s))
2536 s->sy_flags.sy_used_in_reloc = 0;
2539 /* Return whether a symbol has been used in a reloc. */
2542 symbol_used_in_reloc_p (symbolS *s)
2544 if (LOCAL_SYMBOL_CHECK (s))
2546 return s->sy_flags.sy_used_in_reloc;
2549 /* Mark a symbol as an MRI common symbol. */
2552 symbol_mark_mri_common (symbolS *s)
2554 if (LOCAL_SYMBOL_CHECK (s))
2555 s = local_symbol_convert ((struct local_symbol *) s);
2556 s->sy_flags.sy_mri_common = 1;
2559 /* Clear the mark of whether a symbol is an MRI common symbol. */
2562 symbol_clear_mri_common (symbolS *s)
2564 if (LOCAL_SYMBOL_CHECK (s))
2566 s->sy_flags.sy_mri_common = 0;
2569 /* Return whether a symbol is an MRI common symbol. */
2572 symbol_mri_common_p (symbolS *s)
2574 if (LOCAL_SYMBOL_CHECK (s))
2576 return s->sy_flags.sy_mri_common;
2579 /* Mark a symbol as having been written. */
2582 symbol_mark_written (symbolS *s)
2584 if (LOCAL_SYMBOL_CHECK (s))
2586 s->sy_flags.sy_written = 1;
2589 /* Clear the mark of whether a symbol has been written. */
2592 symbol_clear_written (symbolS *s)
2594 if (LOCAL_SYMBOL_CHECK (s))
2596 s->sy_flags.sy_written = 0;
2599 /* Return whether a symbol has been written. */
2602 symbol_written_p (symbolS *s)
2604 if (LOCAL_SYMBOL_CHECK (s))
2606 return s->sy_flags.sy_written;
2609 /* Mark a symbol has having been resolved. */
2612 symbol_mark_resolved (symbolS *s)
2614 if (LOCAL_SYMBOL_CHECK (s))
2616 local_symbol_mark_resolved ((struct local_symbol *) s);
2619 s->sy_flags.sy_resolved = 1;
2622 /* Return whether a symbol has been resolved. */
2625 symbol_resolved_p (symbolS *s)
2627 if (LOCAL_SYMBOL_CHECK (s))
2628 return local_symbol_resolved_p ((struct local_symbol *) s);
2629 return s->sy_flags.sy_resolved;
2632 /* Return whether a symbol is a section symbol. */
2635 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2637 if (LOCAL_SYMBOL_CHECK (s))
2639 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2642 /* Return whether a symbol is equated to another symbol. */
2645 symbol_equated_p (symbolS *s)
2647 if (LOCAL_SYMBOL_CHECK (s))
2649 return s->sy_value.X_op == O_symbol;
2652 /* Return whether a symbol is equated to another symbol, and should be
2653 treated specially when writing out relocs. */
2656 symbol_equated_reloc_p (symbolS *s)
2658 if (LOCAL_SYMBOL_CHECK (s))
2660 /* X_op_symbol, normally not used for O_symbol, is set by
2661 resolve_symbol_value to flag expression syms that have been
2663 return (s->sy_value.X_op == O_symbol
2664 #if defined (OBJ_COFF) && defined (TE_PE)
2667 && ((s->sy_flags.sy_resolved && s->sy_value.X_op_symbol != NULL)
2668 || ! S_IS_DEFINED (s)
2669 || S_IS_COMMON (s)));
2672 /* Return whether a symbol has a constant value. */
2675 symbol_constant_p (symbolS *s)
2677 if (LOCAL_SYMBOL_CHECK (s))
2679 return s->sy_value.X_op == O_constant;
2682 /* Return whether a symbol was cloned and thus removed from the global
2686 symbol_shadow_p (symbolS *s)
2688 if (LOCAL_SYMBOL_CHECK (s))
2690 return s->sy_next == s;
2693 /* Return the BFD symbol for a symbol. */
2696 symbol_get_bfdsym (symbolS *s)
2698 if (LOCAL_SYMBOL_CHECK (s))
2699 s = local_symbol_convert ((struct local_symbol *) s);
2703 /* Set the BFD symbol for a symbol. */
2706 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2708 if (LOCAL_SYMBOL_CHECK (s))
2709 s = local_symbol_convert ((struct local_symbol *) s);
2710 /* Usually, it is harmless to reset a symbol to a BFD section
2711 symbol. For example, obj_elf_change_section sets the BFD symbol
2712 of an old symbol with the newly created section symbol. But when
2713 we have multiple sections with the same name, the newly created
2714 section may have the same name as an old section. We check if the
2715 old symbol has been already marked as a section symbol before
2717 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2719 /* else XXX - What do we do now ? */
2722 #ifdef OBJ_SYMFIELD_TYPE
2724 /* Get a pointer to the object format information for a symbol. */
2727 symbol_get_obj (symbolS *s)
2729 if (LOCAL_SYMBOL_CHECK (s))
2730 s = local_symbol_convert ((struct local_symbol *) s);
2734 /* Set the object format information for a symbol. */
2737 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2739 if (LOCAL_SYMBOL_CHECK (s))
2740 s = local_symbol_convert ((struct local_symbol *) s);
2744 #endif /* OBJ_SYMFIELD_TYPE */
2746 #ifdef TC_SYMFIELD_TYPE
2748 /* Get a pointer to the processor information for a symbol. */
2751 symbol_get_tc (symbolS *s)
2753 if (LOCAL_SYMBOL_CHECK (s))
2754 s = local_symbol_convert ((struct local_symbol *) s);
2758 /* Set the processor information for a symbol. */
2761 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2763 if (LOCAL_SYMBOL_CHECK (s))
2764 s = local_symbol_convert ((struct local_symbol *) s);
2768 #endif /* TC_SYMFIELD_TYPE */
2773 symbol_lastP = NULL;
2774 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2775 sy_hash = hash_new ();
2776 local_hash = hash_new ();
2778 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2779 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2780 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
2782 abs_symbol.sy_value.X_op = O_constant;
2783 abs_symbol.sy_frag = &zero_address_frag;
2785 if (LOCAL_LABELS_FB)
2790 dot_symbol_init (void)
2792 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2793 if (dot_symbol.bsym == NULL)
2794 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2795 dot_symbol.bsym->name = ".";
2796 dot_symbol.sy_flags.sy_forward_ref = 1;
2797 dot_symbol.sy_value.X_op = O_constant;
2802 /* Maximum indent level.
2803 Available for modification inside a gdb session. */
2804 static int max_indent_level = 8;
2807 print_symbol_value_1 (FILE *file, symbolS *sym)
2809 const char *name = S_GET_NAME (sym);
2810 if (!name || !name[0])
2812 fprintf (file, "sym ");
2813 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2814 fprintf (file, " %s", name);
2816 if (LOCAL_SYMBOL_CHECK (sym))
2818 struct local_symbol *locsym = (struct local_symbol *) sym;
2820 if (local_symbol_get_frag (locsym) != & zero_address_frag
2821 && local_symbol_get_frag (locsym) != NULL)
2823 fprintf (file, " frag ");
2824 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2826 if (local_symbol_resolved_p (locsym))
2827 fprintf (file, " resolved");
2828 fprintf (file, " local");
2832 if (sym->sy_frag != &zero_address_frag)
2834 fprintf (file, " frag ");
2835 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2837 if (sym->sy_flags.sy_written)
2838 fprintf (file, " written");
2839 if (sym->sy_flags.sy_resolved)
2840 fprintf (file, " resolved");
2841 else if (sym->sy_flags.sy_resolving)
2842 fprintf (file, " resolving");
2843 if (sym->sy_flags.sy_used_in_reloc)
2844 fprintf (file, " used-in-reloc");
2845 if (sym->sy_flags.sy_used)
2846 fprintf (file, " used");
2847 if (S_IS_LOCAL (sym))
2848 fprintf (file, " local");
2849 if (S_IS_EXTERNAL (sym))
2850 fprintf (file, " extern");
2851 if (S_IS_WEAK (sym))
2852 fprintf (file, " weak");
2853 if (S_IS_DEBUG (sym))
2854 fprintf (file, " debug");
2855 if (S_IS_DEFINED (sym))
2856 fprintf (file, " defined");
2858 if (S_IS_WEAKREFR (sym))
2859 fprintf (file, " weakrefr");
2860 if (S_IS_WEAKREFD (sym))
2861 fprintf (file, " weakrefd");
2862 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2863 if (symbol_resolved_p (sym))
2865 segT s = S_GET_SEGMENT (sym);
2867 if (s != undefined_section
2868 && s != expr_section)
2869 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2871 else if (indent_level < max_indent_level
2872 && S_GET_SEGMENT (sym) != undefined_section)
2875 fprintf (file, "\n%*s<", indent_level * 4, "");
2876 if (LOCAL_SYMBOL_CHECK (sym))
2877 fprintf (file, "constant %lx",
2878 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2880 print_expr_1 (file, &sym->sy_value);
2881 fprintf (file, ">");
2888 print_symbol_value (symbolS *sym)
2891 print_symbol_value_1 (stderr, sym);
2892 fprintf (stderr, "\n");
2896 print_binary (FILE *file, const char *name, expressionS *exp)
2899 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2900 print_symbol_value_1 (file, exp->X_add_symbol);
2901 fprintf (file, ">\n%*s<", indent_level * 4, "");
2902 print_symbol_value_1 (file, exp->X_op_symbol);
2903 fprintf (file, ">");
2908 print_expr_1 (FILE *file, expressionS *exp)
2910 fprintf (file, "expr ");
2911 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2912 fprintf (file, " ");
2916 fprintf (file, "illegal");
2919 fprintf (file, "absent");
2922 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2926 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2927 print_symbol_value_1 (file, exp->X_add_symbol);
2928 fprintf (file, ">");
2930 if (exp->X_add_number)
2931 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2932 (unsigned long) exp->X_add_number);
2936 fprintf (file, "register #%d", (int) exp->X_add_number);
2939 fprintf (file, "big");
2942 fprintf (file, "uminus -<");
2944 print_symbol_value_1 (file, exp->X_add_symbol);
2945 fprintf (file, ">");
2946 goto maybe_print_addnum;
2948 fprintf (file, "bit_not");
2951 print_binary (file, "multiply", exp);
2954 print_binary (file, "divide", exp);
2957 print_binary (file, "modulus", exp);
2960 print_binary (file, "lshift", exp);
2963 print_binary (file, "rshift", exp);
2965 case O_bit_inclusive_or:
2966 print_binary (file, "bit_ior", exp);
2968 case O_bit_exclusive_or:
2969 print_binary (file, "bit_xor", exp);
2972 print_binary (file, "bit_and", exp);
2975 print_binary (file, "eq", exp);
2978 print_binary (file, "ne", exp);
2981 print_binary (file, "lt", exp);
2984 print_binary (file, "le", exp);
2987 print_binary (file, "ge", exp);
2990 print_binary (file, "gt", exp);
2993 print_binary (file, "logical_and", exp);
2996 print_binary (file, "logical_or", exp);
3000 fprintf (file, "add\n%*s<", indent_level * 4, "");
3001 print_symbol_value_1 (file, exp->X_add_symbol);
3002 fprintf (file, ">\n%*s<", indent_level * 4, "");
3003 print_symbol_value_1 (file, exp->X_op_symbol);
3004 fprintf (file, ">");
3005 goto maybe_print_addnum;
3008 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3009 print_symbol_value_1 (file, exp->X_add_symbol);
3010 fprintf (file, ">\n%*s<", indent_level * 4, "");
3011 print_symbol_value_1 (file, exp->X_op_symbol);
3012 fprintf (file, ">");
3013 goto maybe_print_addnum;
3015 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3022 print_expr (expressionS *exp)
3024 print_expr_1 (stderr, exp);
3025 fprintf (stderr, "\n");
3029 symbol_print_statistics (FILE *file)
3031 hash_print_statistics (file, "symbol table", sy_hash);
3032 hash_print_statistics (file, "mini local symbol table", local_hash);
3033 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3034 local_symbol_count, local_symbol_conversion_count);
3037 #ifdef OBJ_COMPLEX_RELC
3039 /* Convert given symbol to a new complex-relocation symbol name. This
3040 may be a recursive function, since it might be called for non-leaf
3041 nodes (plain symbols) in the expression tree. The caller owns the
3042 returning string, so should free it eventually. Errors are
3043 indicated via as_bad and a NULL return value. The given symbol
3044 is marked with sy_used_in_reloc. */
3047 symbol_relc_make_sym (symbolS * sym)
3049 char * terminal = NULL;
3054 gas_assert (sym != NULL);
3056 /* Recurse to symbol_relc_make_expr if this symbol
3057 is defined as an expression or a plain value. */
3058 if ( S_GET_SEGMENT (sym) == expr_section
3059 || S_GET_SEGMENT (sym) == absolute_section)
3060 return symbol_relc_make_expr (& sym->sy_value);
3062 /* This may be a "fake symbol" L0\001, referring to ".".
3063 Write out a special null symbol to refer to this position. */
3064 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3065 return xstrdup (".");
3067 /* We hope this is a plain leaf symbol. Construct the encoding
3068 as {S,s}II...:CCCCCCC....
3069 where 'S'/'s' means section symbol / plain symbol
3070 III is decimal for the symbol name length
3071 CCC is the symbol name itself. */
3072 symbol_mark_used_in_reloc (sym);
3074 sname = S_GET_NAME (sym);
3075 sname_len = strlen (sname);
3076 typetag = symbol_section_p (sym) ? 'S' : 's';
3078 terminal = xmalloc (1 /* S or s */
3079 + 8 /* sname_len in decimal */
3081 + sname_len /* name itself */
3084 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3088 /* Convert given value to a new complex-relocation symbol name. This
3089 is a non-recursive function, since it is be called for leaf nodes
3090 (plain values) in the expression tree. The caller owns the
3091 returning string, so should free() it eventually. No errors. */
3094 symbol_relc_make_value (offsetT val)
3096 char * terminal = xmalloc (28); /* Enough for long long. */
3099 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3103 /* Convert given expression to a new complex-relocation symbol name.
3104 This is a recursive function, since it traverses the entire given
3105 expression tree. The caller owns the returning string, so should
3106 free() it eventually. Errors are indicated via as_bad() and a NULL
3110 symbol_relc_make_expr (expressionS * exp)
3112 char * opstr = NULL; /* Operator prefix string. */
3113 int arity = 0; /* Arity of this operator. */
3114 char * operands[3]; /* Up to three operands. */
3115 char * concat_string = NULL;
3117 operands[0] = operands[1] = operands[2] = NULL;
3119 gas_assert (exp != NULL);
3121 /* Match known operators -> fill in opstr, arity, operands[] and fall
3122 through to construct subexpression fragments; may instead return
3123 string directly for leaf nodes. */
3125 /* See expr.h for the meaning of all these enums. Many operators
3126 have an unnatural arity (X_add_number implicitly added). The
3127 conversion logic expands them to explicit "+" subexpressions. */
3132 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3137 return symbol_relc_make_value (exp->X_add_number);
3140 if (exp->X_add_number)
3144 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3145 operands[1] = symbol_relc_make_value (exp->X_add_number);
3149 return symbol_relc_make_sym (exp->X_add_symbol);
3151 /* Helper macros for nesting nodes. */
3153 #define HANDLE_XADD_OPT1(str_) \
3154 if (exp->X_add_number) \
3157 opstr = "+:" str_; \
3158 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3159 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3166 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3170 #define HANDLE_XADD_OPT2(str_) \
3171 if (exp->X_add_number) \
3174 opstr = "+:" str_; \
3175 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3176 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3177 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3183 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3184 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3188 /* Nesting nodes. */
3190 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3191 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3192 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3193 case O_multiply: HANDLE_XADD_OPT2 ("*");
3194 case O_divide: HANDLE_XADD_OPT2 ("/");
3195 case O_modulus: HANDLE_XADD_OPT2 ("%");
3196 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3197 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3198 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3199 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3200 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3201 case O_add: HANDLE_XADD_OPT2 ("+");
3202 case O_subtract: HANDLE_XADD_OPT2 ("-");
3203 case O_eq: HANDLE_XADD_OPT2 ("==");
3204 case O_ne: HANDLE_XADD_OPT2 ("!=");
3205 case O_lt: HANDLE_XADD_OPT2 ("<");
3206 case O_le: HANDLE_XADD_OPT2 ("<=");
3207 case O_ge: HANDLE_XADD_OPT2 (">=");
3208 case O_gt: HANDLE_XADD_OPT2 (">");
3209 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3210 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3213 /* Validate & reject early. */
3214 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3216 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3218 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3222 concat_string = NULL;
3225 /* Allocate new string; include inter-operand padding gaps etc. */
3226 concat_string = xmalloc (strlen (opstr)
3228 + (arity >= 1 ? (strlen (operands[0]) + 1 ) : 0)
3229 + (arity >= 2 ? (strlen (operands[1]) + 1 ) : 0)
3230 + (arity >= 3 ? (strlen (operands[2]) + 0 ) : 0)
3232 gas_assert (concat_string != NULL);
3234 /* Format the thing. */
3235 sprintf (concat_string,
3236 (arity == 0 ? "%s" :
3237 arity == 1 ? "%s:%s" :
3238 arity == 2 ? "%s:%s:%s" :
3239 /* arity == 3 */ "%s:%s:%s:%s"),
3240 opstr, operands[0], operands[1], operands[2]);
3243 /* Free operand strings (not opstr). */
3244 if (arity >= 1) xfree (operands[0]);
3245 if (arity >= 2) xfree (operands[1]);
3246 if (arity >= 3) xfree (operands[2]);
3248 return concat_string;