1 /* symbols.c -symbol table-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
27 #include "safe-ctype.h"
28 #include "obstack.h" /* For "symbols.h" */
31 #include "struc-symbol.h"
33 /* This is non-zero if symbols are case sensitive, which is the
35 int symbols_case_sensitive
= 1;
37 #ifndef WORKING_DOT_WORD
38 extern int new_broken_words
;
41 /* symbol-name => struct symbol pointer */
42 static struct hash_control
*sy_hash
;
44 /* Table of local symbols. */
45 static struct hash_control
*local_hash
;
47 /* Below are commented in "symbols.h". */
48 symbolS
*symbol_rootP
;
49 symbolS
*symbol_lastP
;
54 #define debug_verify_symchain verify_symbol_chain
56 #define debug_verify_symchain(root, last) ((void) 0)
59 #define DOLLAR_LABEL_CHAR '\001'
60 #define LOCAL_LABEL_CHAR '\002'
62 #ifndef TC_LABEL_IS_LOCAL
63 #define TC_LABEL_IS_LOCAL(name) 0
68 /* The name of an external symbol which is
69 used to make weak PE symbol names unique. */
70 const char * an_external_name
;
73 static char *save_symbol_name (const char *);
74 static void fb_label_init (void);
75 static long dollar_label_instance (long);
76 static long fb_label_instance (long);
78 static void print_binary (FILE *, const char *, expressionS
*);
80 /* Return a pointer to a new symbol. Die if we can't make a new
81 symbol. Fill in the symbol's values. Add symbol to end of symbol
84 This function should be called in the general case of creating a
85 symbol. However, if the output file symbol table has already been
86 set, and you are certain that this symbol won't be wanted in the
87 output file, you can call symbol_create. */
90 symbol_new (const char *name
, segT segment
, valueT valu
, fragS
*frag
)
92 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
94 /* Link to end of symbol chain. */
96 extern int symbol_table_frozen
;
97 if (symbol_table_frozen
)
100 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
105 /* Save a symbol name on a permanent obstack, and convert it according
106 to the object file format. */
109 save_symbol_name (const char *name
)
111 unsigned int name_length
;
114 name_length
= strlen (name
) + 1; /* +1 for \0. */
115 obstack_grow (¬es
, name
, name_length
);
116 ret
= (char *) obstack_finish (¬es
);
118 #ifdef tc_canonicalize_symbol_name
119 ret
= tc_canonicalize_symbol_name (ret
);
122 if (! symbols_case_sensitive
)
126 for (s
= ret
; *s
!= '\0'; s
++)
134 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
135 segT segment
, /* Segment identifier (SEG_<something>). */
136 valueT valu
, /* Symbol value. */
137 fragS
*frag
/* Associated fragment. */)
139 char *preserved_copy_of_name
;
142 preserved_copy_of_name
= save_symbol_name (name
);
144 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
146 /* symbol must be born in some fixed state. This seems as good as any. */
147 memset (symbolP
, 0, sizeof (symbolS
));
149 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
150 if (symbolP
->bsym
== NULL
)
151 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
152 S_SET_NAME (symbolP
, preserved_copy_of_name
);
154 S_SET_SEGMENT (symbolP
, segment
);
155 S_SET_VALUE (symbolP
, valu
);
156 symbol_clear_list_pointers (symbolP
);
158 symbolP
->sy_frag
= frag
;
160 obj_symbol_new_hook (symbolP
);
162 #ifdef tc_symbol_new_hook
163 tc_symbol_new_hook (symbolP
);
170 /* Local symbol support. If we can get away with it, we keep only a
171 small amount of information for local symbols. */
173 static symbolS
*local_symbol_convert (struct local_symbol
*);
175 /* Used for statistics. */
177 static unsigned long local_symbol_count
;
178 static unsigned long local_symbol_conversion_count
;
180 /* This macro is called with a symbol argument passed by reference.
181 It returns whether this is a local symbol. If necessary, it
182 changes its argument to the real symbol. */
184 #define LOCAL_SYMBOL_CHECK(s) \
186 ? (local_symbol_converted_p ((struct local_symbol *) s) \
187 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
192 /* Create a local symbol and insert it into the local hash table. */
194 static struct local_symbol
*
195 local_symbol_make (const char *name
, segT section
, valueT val
, fragS
*frag
)
198 struct local_symbol
*ret
;
200 ++local_symbol_count
;
202 name_copy
= save_symbol_name (name
);
204 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
205 ret
->lsy_marker
= NULL
;
206 ret
->lsy_name
= name_copy
;
207 ret
->lsy_section
= section
;
208 local_symbol_set_frag (ret
, frag
);
209 ret
->lsy_value
= val
;
211 hash_jam (local_hash
, name_copy
, (void *) ret
);
216 /* Convert a local symbol into a real symbol. Note that we do not
217 reclaim the space used by the local symbol. */
220 local_symbol_convert (struct local_symbol
*locsym
)
224 gas_assert (locsym
->lsy_marker
== NULL
);
225 if (local_symbol_converted_p (locsym
))
226 return local_symbol_get_real_symbol (locsym
);
228 ++local_symbol_conversion_count
;
230 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
231 local_symbol_get_frag (locsym
));
233 if (local_symbol_resolved_p (locsym
))
234 ret
->sy_resolved
= 1;
236 /* Local symbols are always either defined or used. */
239 #ifdef TC_LOCAL_SYMFIELD_CONVERT
240 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
243 symbol_table_insert (ret
);
245 local_symbol_mark_converted (locsym
);
246 local_symbol_set_real_symbol (locsym
, ret
);
248 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
254 define_sym_at_dot (symbolS
*symbolP
)
256 symbolP
->sy_frag
= frag_now
;
257 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
258 S_SET_SEGMENT (symbolP
, now_seg
);
261 /* We have just seen "<name>:".
262 Creates a struct symbol unless it already exists.
264 Gripes if we are redefining a symbol incompatibly (and ignores it). */
267 colon (/* Just seen "x:" - rattle symbols & frags. */
268 const char *sym_name
/* Symbol name, as a cannonical string. */
269 /* We copy this string: OK to alter later. */)
271 register symbolS
*symbolP
; /* Symbol we are working with. */
273 /* Sun local labels go out of scope whenever a non-local symbol is
275 if (LOCAL_LABELS_DOLLAR
276 && !bfd_is_local_label_name (stdoutput
, sym_name
))
277 dollar_label_clear ();
279 #ifndef WORKING_DOT_WORD
280 if (new_broken_words
)
282 struct broken_word
*a
;
287 if (now_seg
== absolute_section
)
289 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
293 possible_bytes
= (md_short_jump_size
294 + new_broken_words
* md_long_jump_size
);
297 frag_opcode
= frag_var (rs_broken_word
,
301 (symbolS
*) broken_words
,
305 /* We want to store the pointer to where to insert the jump
306 table in the fr_opcode of the rs_broken_word frag. This
307 requires a little hackery. */
309 && (frag_tmp
->fr_type
!= rs_broken_word
310 || frag_tmp
->fr_opcode
))
311 frag_tmp
= frag_tmp
->fr_next
;
313 frag_tmp
->fr_opcode
= frag_opcode
;
314 new_broken_words
= 0;
316 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
317 a
->dispfrag
= frag_tmp
;
319 #endif /* WORKING_DOT_WORD */
321 if ((symbolP
= symbol_find (sym_name
)) != 0)
323 S_CLEAR_WEAKREFR (symbolP
);
324 #ifdef RESOLVE_SYMBOL_REDEFINITION
325 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
328 /* Now check for undefined symbols. */
329 if (LOCAL_SYMBOL_CHECK (symbolP
))
331 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
333 if (locsym
->lsy_section
!= undefined_section
334 && (local_symbol_get_frag (locsym
) != frag_now
335 || locsym
->lsy_section
!= now_seg
336 || locsym
->lsy_value
!= frag_now_fix ()))
338 as_bad (_("symbol `%s' is already defined"), sym_name
);
342 locsym
->lsy_section
= now_seg
;
343 local_symbol_set_frag (locsym
, frag_now
);
344 locsym
->lsy_value
= frag_now_fix ();
346 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
347 || S_IS_COMMON (symbolP
)
348 || S_IS_VOLATILE (symbolP
))
350 if (S_IS_VOLATILE (symbolP
))
352 symbolP
= symbol_clone (symbolP
, 1);
353 S_SET_VALUE (symbolP
, 0);
354 S_CLEAR_VOLATILE (symbolP
);
356 if (S_GET_VALUE (symbolP
) == 0)
358 define_sym_at_dot (symbolP
);
361 #endif /* if we have one, it better be zero. */
366 /* There are still several cases to check:
368 A .comm/.lcomm symbol being redefined as initialized
371 A .comm/.lcomm symbol being redefined with a larger
374 This only used to be allowed on VMS gas, but Sun cc
375 on the sparc also depends on it. */
377 if (((!S_IS_DEBUG (symbolP
)
378 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
379 && S_IS_EXTERNAL (symbolP
))
380 || S_GET_SEGMENT (symbolP
) == bss_section
)
381 && (now_seg
== data_section
382 || now_seg
== bss_section
383 || now_seg
== S_GET_SEGMENT (symbolP
)))
385 /* Select which of the 2 cases this is. */
386 if (now_seg
!= data_section
)
388 /* New .comm for prev .comm symbol.
390 If the new size is larger we just change its
391 value. If the new size is smaller, we ignore
393 if (S_GET_VALUE (symbolP
)
394 < ((unsigned) frag_now_fix ()))
396 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
401 /* It is a .comm/.lcomm being converted to initialized
403 define_sym_at_dot (symbolP
);
408 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
409 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
410 static const char *od_buf
= "";
414 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
415 sprintf (od_buf
, "%d.%d.",
416 S_GET_OTHER (symbolP
),
417 S_GET_DESC (symbolP
));
419 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
421 segment_name (S_GET_SEGMENT (symbolP
)),
423 (long) S_GET_VALUE (symbolP
));
425 } /* if the undefined symbol has no value */
429 /* Don't blow up if the definition is the same. */
430 if (!(frag_now
== symbolP
->sy_frag
431 && S_GET_VALUE (symbolP
) == frag_now_fix ()
432 && S_GET_SEGMENT (symbolP
) == now_seg
))
434 as_bad (_("symbol `%s' is already defined"), sym_name
);
435 symbolP
= symbol_clone (symbolP
, 0);
436 define_sym_at_dot (symbolP
);
441 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
443 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
444 (valueT
) frag_now_fix (),
449 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
452 symbol_table_insert (symbolP
);
455 if (mri_common_symbol
!= NULL
)
457 /* This symbol is actually being defined within an MRI common
458 section. This requires special handling. */
459 if (LOCAL_SYMBOL_CHECK (symbolP
))
460 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
461 symbolP
->sy_value
.X_op
= O_symbol
;
462 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
463 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
464 symbolP
->sy_frag
= &zero_address_frag
;
465 S_SET_SEGMENT (symbolP
, expr_section
);
466 symbolP
->sy_mri_common
= 1;
470 tc_frob_label (symbolP
);
472 #ifdef obj_frob_label
473 obj_frob_label (symbolP
);
479 /* Die if we can't insert the symbol. */
482 symbol_table_insert (symbolS
*symbolP
)
484 register const char *error_string
;
487 know (S_GET_NAME (symbolP
));
489 if (LOCAL_SYMBOL_CHECK (symbolP
))
491 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
493 if (error_string
!= NULL
)
494 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
495 S_GET_NAME (symbolP
), error_string
);
499 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (void *) symbolP
)))
501 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
502 S_GET_NAME (symbolP
), error_string
);
506 /* If a symbol name does not exist, create it as undefined, and insert
507 it into the symbol table. Return a pointer to it. */
510 symbol_find_or_make (const char *name
)
512 register symbolS
*symbolP
;
514 symbolP
= symbol_find (name
);
518 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
520 symbolP
= md_undefined_symbol ((char *) name
);
524 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
530 symbolP
= symbol_make (name
);
532 symbol_table_insert (symbolP
);
533 } /* if symbol wasn't found */
539 symbol_make (const char *name
)
543 /* Let the machine description default it, e.g. for register names. */
544 symbolP
= md_undefined_symbol ((char *) name
);
547 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
553 symbol_clone (symbolS
*orgsymP
, int replace
)
556 asymbol
*bsymorg
, *bsymnew
;
558 /* Make sure we never clone the dot special symbol. */
559 gas_assert (orgsymP
!= &dot_symbol
);
561 /* Running local_symbol_convert on a clone that's not the one currently
562 in local_hash would incorrectly replace the hash entry. Thus the
563 symbol must be converted here. Note that the rest of the function
564 depends on not encountering an unconverted symbol. */
565 if (LOCAL_SYMBOL_CHECK (orgsymP
))
566 orgsymP
= local_symbol_convert ((struct local_symbol
*) orgsymP
);
567 bsymorg
= orgsymP
->bsym
;
569 newsymP
= (symbolS
*) obstack_alloc (¬es
, sizeof (*newsymP
));
571 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
573 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
574 newsymP
->bsym
= bsymnew
;
575 bsymnew
->name
= bsymorg
->name
;
576 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
577 bsymnew
->section
= bsymorg
->section
;
578 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
579 bfd_asymbol_bfd (bsymnew
), bsymnew
);
581 #ifdef obj_symbol_clone_hook
582 obj_symbol_clone_hook (newsymP
, orgsymP
);
585 #ifdef tc_symbol_clone_hook
586 tc_symbol_clone_hook (newsymP
, orgsymP
);
591 if (symbol_rootP
== orgsymP
)
592 symbol_rootP
= newsymP
;
593 else if (orgsymP
->sy_previous
)
595 orgsymP
->sy_previous
->sy_next
= newsymP
;
596 orgsymP
->sy_previous
= NULL
;
598 if (symbol_lastP
== orgsymP
)
599 symbol_lastP
= newsymP
;
600 else if (orgsymP
->sy_next
)
601 orgsymP
->sy_next
->sy_previous
= newsymP
;
603 /* Symbols that won't be output can't be external. */
604 S_CLEAR_EXTERNAL (orgsymP
);
605 orgsymP
->sy_previous
= orgsymP
->sy_next
= orgsymP
;
606 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
608 symbol_table_insert (newsymP
);
612 /* Symbols that won't be output can't be external. */
613 S_CLEAR_EXTERNAL (newsymP
);
614 newsymP
->sy_previous
= newsymP
->sy_next
= newsymP
;
620 /* Referenced symbols, if they are forward references, need to be cloned
621 (without replacing the original) so that the value of the referenced
622 symbols at the point of use . */
624 #undef symbol_clone_if_forward_ref
626 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
628 if (symbolP
&& !LOCAL_SYMBOL_CHECK (symbolP
))
630 symbolS
*add_symbol
= symbolP
->sy_value
.X_add_symbol
;
631 symbolS
*op_symbol
= symbolP
->sy_value
.X_op_symbol
;
633 if (symbolP
->sy_forward_ref
)
638 /* assign_symbol() clones volatile symbols; pre-existing expressions
639 hold references to the original instance, but want the current
640 value. Just repeat the lookup. */
641 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
642 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
643 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
644 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
647 /* Re-using sy_resolving here, as this routine cannot get called from
648 symbol resolution code. */
649 if ((symbolP
->bsym
->section
== expr_section
|| symbolP
->sy_forward_ref
)
650 && !symbolP
->sy_resolving
)
652 symbolP
->sy_resolving
= 1;
653 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
654 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
655 symbolP
->sy_resolving
= 0;
658 if (symbolP
->sy_forward_ref
659 || add_symbol
!= symbolP
->sy_value
.X_add_symbol
660 || op_symbol
!= symbolP
->sy_value
.X_op_symbol
)
662 if (symbolP
!= &dot_symbol
)
664 symbolP
= symbol_clone (symbolP
, 0);
665 symbolP
->sy_resolving
= 0;
669 symbolP
= symbol_temp_new_now ();
670 #ifdef tc_new_dot_label
671 tc_new_dot_label (symbolP
);
676 symbolP
->sy_value
.X_add_symbol
= add_symbol
;
677 symbolP
->sy_value
.X_op_symbol
= op_symbol
;
684 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
686 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
690 symbol_temp_new_now (void)
692 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
696 symbol_temp_make (void)
698 return symbol_make (FAKE_LABEL_NAME
);
701 /* Implement symbol table lookup.
702 In: A symbol's name as a string: '\0' can't be part of a symbol name.
703 Out: NULL if the name was not in the symbol table, else the address
704 of a struct symbol associated with that name. */
707 symbol_find_exact (const char *name
)
709 return symbol_find_exact_noref (name
, 0);
713 symbol_find_exact_noref (const char *name
, int noref
)
715 struct local_symbol
*locsym
;
718 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
720 return (symbolS
*) locsym
;
722 sym
= ((symbolS
*) hash_find (sy_hash
, name
));
724 /* Any references to the symbol, except for the reference in
725 .weakref, must clear this flag, such that the symbol does not
726 turn into a weak symbol. Note that we don't have to handle the
727 local_symbol case, since a weakrefd is always promoted out of the
728 local_symbol table when it is turned into a weak symbol. */
730 S_CLEAR_WEAKREFD (sym
);
736 symbol_find (const char *name
)
738 return symbol_find_noref (name
, 0);
742 symbol_find_noref (const char *name
, int noref
)
744 #ifdef tc_canonicalize_symbol_name
747 size_t len
= strlen (name
) + 1;
749 copy
= (char *) alloca (len
);
750 memcpy (copy
, name
, len
);
751 name
= tc_canonicalize_symbol_name (copy
);
755 if (! symbols_case_sensitive
)
762 name
= copy
= (char *) alloca (strlen (name
) + 1);
764 while ((c
= *orig
++) != '\0')
766 *copy
++ = TOUPPER (c
);
771 return symbol_find_exact_noref (name
, noref
);
774 /* Once upon a time, symbols were kept in a singly linked list. At
775 least coff needs to be able to rearrange them from time to time, for
776 which a doubly linked list is much more convenient. Loic did these
777 as macros which seemed dangerous to me so they're now functions.
780 /* Link symbol ADDME after symbol TARGET in the chain. */
783 symbol_append (symbolS
*addme
, symbolS
*target
,
784 symbolS
**rootPP
, symbolS
**lastPP
)
786 if (LOCAL_SYMBOL_CHECK (addme
))
788 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
793 know (*rootPP
== NULL
);
794 know (*lastPP
== NULL
);
795 addme
->sy_next
= NULL
;
796 addme
->sy_previous
= NULL
;
800 } /* if the list is empty */
802 if (target
->sy_next
!= NULL
)
804 target
->sy_next
->sy_previous
= addme
;
808 know (*lastPP
== target
);
810 } /* if we have a next */
812 addme
->sy_next
= target
->sy_next
;
813 target
->sy_next
= addme
;
814 addme
->sy_previous
= target
;
816 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
819 /* Set the chain pointers of SYMBOL to null. */
822 symbol_clear_list_pointers (symbolS
*symbolP
)
824 if (LOCAL_SYMBOL_CHECK (symbolP
))
826 symbolP
->sy_next
= NULL
;
827 symbolP
->sy_previous
= NULL
;
830 /* Remove SYMBOLP from the list. */
833 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
835 if (LOCAL_SYMBOL_CHECK (symbolP
))
838 if (symbolP
== *rootPP
)
840 *rootPP
= symbolP
->sy_next
;
841 } /* if it was the root */
843 if (symbolP
== *lastPP
)
845 *lastPP
= symbolP
->sy_previous
;
846 } /* if it was the tail */
848 if (symbolP
->sy_next
!= NULL
)
850 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
853 if (symbolP
->sy_previous
!= NULL
)
855 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
858 debug_verify_symchain (*rootPP
, *lastPP
);
861 /* Link symbol ADDME before symbol TARGET in the chain. */
864 symbol_insert (symbolS
*addme
, symbolS
*target
,
865 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
867 if (LOCAL_SYMBOL_CHECK (addme
))
869 if (LOCAL_SYMBOL_CHECK (target
))
872 if (target
->sy_previous
!= NULL
)
874 target
->sy_previous
->sy_next
= addme
;
878 know (*rootPP
== target
);
882 addme
->sy_previous
= target
->sy_previous
;
883 target
->sy_previous
= addme
;
884 addme
->sy_next
= target
;
886 debug_verify_symchain (*rootPP
, *lastPP
);
890 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
892 symbolS
*symbolP
= rootP
;
897 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
899 gas_assert (symbolP
->bsym
!= NULL
);
900 gas_assert (symbolP
->sy_next
->sy_previous
== symbolP
);
903 gas_assert (lastP
== symbolP
);
906 #ifdef OBJ_COMPLEX_RELC
909 use_complex_relocs_for (symbolS
* symp
)
911 switch (symp
->sy_value
.X_op
)
921 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
922 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
924 (S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
925 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
))
934 case O_bit_inclusive_or
:
936 case O_bit_exclusive_or
:
949 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
950 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
952 (S_IS_COMMON (symp
->sy_value
.X_op_symbol
)
953 || S_IS_LOCAL (symp
->sy_value
.X_op_symbol
))
955 && S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
956 && S_IS_DEFINED (symp
->sy_value
.X_op_symbol
)
957 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
958 && S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) != expr_section
)
970 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
974 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
975 segT seg_right
= S_GET_SEGMENT (right
);
984 case O_uminus
: opname
= "-"; break;
985 case O_bit_not
: opname
= "~"; break;
986 case O_logical_not
: opname
= "!"; break;
987 case O_multiply
: opname
= "*"; break;
988 case O_divide
: opname
= "/"; break;
989 case O_modulus
: opname
= "%"; break;
990 case O_left_shift
: opname
= "<<"; break;
991 case O_right_shift
: opname
= ">>"; break;
992 case O_bit_inclusive_or
: opname
= "|"; break;
993 case O_bit_or_not
: opname
= "|~"; break;
994 case O_bit_exclusive_or
: opname
= "^"; break;
995 case O_bit_and
: opname
= "&"; break;
996 case O_add
: opname
= "+"; break;
997 case O_subtract
: opname
= "-"; break;
998 case O_eq
: opname
= "=="; break;
999 case O_ne
: opname
= "!="; break;
1000 case O_lt
: opname
= "<"; break;
1001 case O_le
: opname
= "<="; break;
1002 case O_ge
: opname
= ">="; break;
1003 case O_gt
: opname
= ">"; break;
1004 case O_logical_and
: opname
= "&&"; break;
1005 case O_logical_or
: opname
= "||"; break;
1008 if (expr_symbol_where (symp
, &file
, &line
))
1011 as_bad_where (file
, line
,
1012 _("invalid operands (%s and %s sections) for `%s'"),
1013 seg_left
->name
, seg_right
->name
, opname
);
1015 as_bad_where (file
, line
,
1016 _("invalid operand (%s section) for `%s'"),
1017 seg_right
->name
, opname
);
1021 const char *sname
= S_GET_NAME (symp
);
1024 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1025 seg_left
->name
, seg_right
->name
, opname
, sname
);
1027 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1028 seg_right
->name
, opname
, sname
);
1032 /* Resolve the value of a symbol. This is called during the final
1033 pass over the symbol table to resolve any symbols with complex
1037 resolve_symbol_value (symbolS
*symp
)
1040 valueT final_val
= 0;
1043 if (LOCAL_SYMBOL_CHECK (symp
))
1045 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1047 final_val
= locsym
->lsy_value
;
1048 if (local_symbol_resolved_p (locsym
))
1051 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
1055 locsym
->lsy_value
= final_val
;
1056 local_symbol_mark_resolved (locsym
);
1062 if (symp
->sy_resolved
)
1064 if (symp
->sy_value
.X_op
== O_constant
)
1065 return (valueT
) symp
->sy_value
.X_add_number
;
1071 final_seg
= S_GET_SEGMENT (symp
);
1073 if (symp
->sy_resolving
)
1076 as_bad (_("symbol definition loop encountered at `%s'"),
1081 #ifdef OBJ_COMPLEX_RELC
1082 else if (final_seg
== expr_section
1083 && use_complex_relocs_for (symp
))
1085 symbolS
* relc_symbol
= NULL
;
1086 char * relc_symbol_name
= NULL
;
1088 relc_symbol_name
= symbol_relc_make_expr (& symp
->sy_value
);
1090 /* For debugging, print out conversion input & output. */
1092 print_expr (& symp
->sy_value
);
1093 if (relc_symbol_name
)
1094 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1097 if (relc_symbol_name
!= NULL
)
1098 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1099 0, & zero_address_frag
);
1101 if (relc_symbol
== NULL
)
1103 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1109 symbol_table_insert (relc_symbol
);
1111 /* S_CLEAR_EXTERNAL (relc_symbol); */
1112 if (symp
->bsym
->flags
& BSF_SRELC
)
1113 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1115 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1116 /* symp->bsym->flags |= BSF_RELC; */
1117 copy_symbol_attributes (symp
, relc_symbol
);
1118 symp
->sy_value
.X_op
= O_symbol
;
1119 symp
->sy_value
.X_add_symbol
= relc_symbol
;
1120 symp
->sy_value
.X_add_number
= 0;
1124 final_seg
= undefined_section
;
1125 goto exit_dont_set_value
;
1130 symbolS
*add_symbol
, *op_symbol
;
1131 offsetT left
, right
;
1132 segT seg_left
, seg_right
;
1136 symp
->sy_resolving
= 1;
1138 /* Help out with CSE. */
1139 add_symbol
= symp
->sy_value
.X_add_symbol
;
1140 op_symbol
= symp
->sy_value
.X_op_symbol
;
1141 final_val
= symp
->sy_value
.X_add_number
;
1142 op
= symp
->sy_value
.X_op
;
1155 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
1156 if (final_seg
== expr_section
)
1157 final_seg
= absolute_section
;
1166 left
= resolve_symbol_value (add_symbol
);
1167 seg_left
= S_GET_SEGMENT (add_symbol
);
1169 symp
->sy_value
.X_op_symbol
= NULL
;
1172 if (S_IS_WEAKREFR (symp
))
1174 gas_assert (final_val
== 0);
1175 if (S_IS_WEAKREFR (add_symbol
))
1177 gas_assert (add_symbol
->sy_value
.X_op
== O_symbol
1178 && add_symbol
->sy_value
.X_add_number
== 0);
1179 add_symbol
= add_symbol
->sy_value
.X_add_symbol
;
1180 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1181 symp
->sy_value
.X_add_symbol
= add_symbol
;
1185 if (symp
->sy_mri_common
)
1187 /* This is a symbol inside an MRI common section. The
1188 relocation routines are going to handle it specially.
1189 Don't change the value. */
1190 resolved
= symbol_resolved_p (add_symbol
);
1194 if (finalize_syms
&& final_val
== 0)
1196 if (LOCAL_SYMBOL_CHECK (add_symbol
))
1197 add_symbol
= local_symbol_convert ((struct local_symbol
*)
1199 copy_symbol_attributes (symp
, add_symbol
);
1202 /* If we have equated this symbol to an undefined or common
1203 symbol, keep X_op set to O_symbol, and don't change
1204 X_add_number. This permits the routine which writes out
1205 relocation to detect this case, and convert the
1206 relocation to be against the symbol to which this symbol
1208 if (! S_IS_DEFINED (add_symbol
)
1209 #if defined (OBJ_COFF) && defined (TE_PE)
1210 || S_IS_WEAK (add_symbol
)
1212 || S_IS_COMMON (add_symbol
))
1216 symp
->sy_value
.X_op
= O_symbol
;
1217 symp
->sy_value
.X_add_symbol
= add_symbol
;
1218 symp
->sy_value
.X_add_number
= final_val
;
1219 /* Use X_op_symbol as a flag. */
1220 symp
->sy_value
.X_op_symbol
= add_symbol
;
1222 final_seg
= seg_left
;
1224 resolved
= symbol_resolved_p (add_symbol
);
1225 symp
->sy_resolving
= 0;
1226 goto exit_dont_set_value
;
1228 else if (finalize_syms
1229 && ((final_seg
== expr_section
&& seg_left
!= expr_section
)
1230 || symbol_shadow_p (symp
)))
1232 /* If the symbol is an expression symbol, do similarly
1233 as for undefined and common syms above. Handles
1234 "sym +/- expr" where "expr" cannot be evaluated
1235 immediately, and we want relocations to be against
1236 "sym", eg. because it is weak. */
1237 symp
->sy_value
.X_op
= O_symbol
;
1238 symp
->sy_value
.X_add_symbol
= add_symbol
;
1239 symp
->sy_value
.X_add_number
= final_val
;
1240 symp
->sy_value
.X_op_symbol
= add_symbol
;
1241 final_seg
= seg_left
;
1242 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1243 resolved
= symbol_resolved_p (add_symbol
);
1244 symp
->sy_resolving
= 0;
1245 goto exit_dont_set_value
;
1249 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1250 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1251 final_seg
= seg_left
;
1254 resolved
= symbol_resolved_p (add_symbol
);
1255 if (S_IS_WEAKREFR (symp
))
1256 goto exit_dont_set_value
;
1262 left
= resolve_symbol_value (add_symbol
);
1263 seg_left
= S_GET_SEGMENT (add_symbol
);
1265 /* By reducing these to the relevant dyadic operator, we get
1266 !S -> S == 0 permitted on anything,
1267 -S -> 0 - S only permitted on absolute
1268 ~S -> S ^ ~0 only permitted on absolute */
1269 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1271 report_op_error (symp
, NULL
, op
, add_symbol
);
1273 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1274 final_seg
= absolute_section
;
1278 else if (op
== O_logical_not
)
1283 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1285 resolved
= symbol_resolved_p (add_symbol
);
1293 case O_bit_inclusive_or
:
1295 case O_bit_exclusive_or
:
1307 left
= resolve_symbol_value (add_symbol
);
1308 right
= resolve_symbol_value (op_symbol
);
1309 seg_left
= S_GET_SEGMENT (add_symbol
);
1310 seg_right
= S_GET_SEGMENT (op_symbol
);
1312 /* Simplify addition or subtraction of a constant by folding the
1313 constant into X_add_number. */
1316 if (seg_right
== absolute_section
)
1321 else if (seg_left
== absolute_section
)
1324 add_symbol
= op_symbol
;
1326 seg_left
= seg_right
;
1330 else if (op
== O_subtract
)
1332 if (seg_right
== absolute_section
)
1340 /* Equality and non-equality tests are permitted on anything.
1341 Subtraction, and other comparison operators are permitted if
1342 both operands are in the same section. Otherwise, both
1343 operands must be absolute. We already handled the case of
1344 addition or subtraction of a constant above. This will
1345 probably need to be changed for an object file format which
1346 supports arbitrary expressions, such as IEEE-695. */
1347 if (!(seg_left
== absolute_section
1348 && seg_right
== absolute_section
)
1349 && !(op
== O_eq
|| op
== O_ne
)
1350 && !((op
== O_subtract
1351 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1352 && seg_left
== seg_right
1353 && (seg_left
!= undefined_section
1354 || add_symbol
== op_symbol
)))
1356 /* Don't emit messages unless we're finalizing the symbol value,
1357 otherwise we may get the same message multiple times. */
1359 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1360 /* However do not move the symbol into the absolute section
1361 if it cannot currently be resolved - this would confuse
1362 other parts of the assembler into believing that the
1363 expression had been evaluated to zero. */
1369 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1370 final_seg
= absolute_section
;
1372 /* Check for division by zero. */
1373 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1375 /* If seg_right is not absolute_section, then we've
1376 already issued a warning about using a bad symbol. */
1377 if (seg_right
== absolute_section
&& finalize_syms
)
1382 if (expr_symbol_where (symp
, &file
, &line
))
1383 as_bad_where (file
, line
, _("division by zero"));
1385 as_bad (_("division by zero when setting `%s'"),
1392 switch (symp
->sy_value
.X_op
)
1394 case O_multiply
: left
*= right
; break;
1395 case O_divide
: left
/= right
; break;
1396 case O_modulus
: left
%= right
; break;
1397 case O_left_shift
: left
<<= right
; break;
1398 case O_right_shift
: left
>>= right
; break;
1399 case O_bit_inclusive_or
: left
|= right
; break;
1400 case O_bit_or_not
: left
|= ~right
; break;
1401 case O_bit_exclusive_or
: left
^= right
; break;
1402 case O_bit_and
: left
&= right
; break;
1403 case O_add
: left
+= right
; break;
1404 case O_subtract
: left
-= right
; break;
1407 left
= (left
== right
&& seg_left
== seg_right
1408 && (seg_left
!= undefined_section
1409 || add_symbol
== op_symbol
)
1410 ? ~ (offsetT
) 0 : 0);
1411 if (symp
->sy_value
.X_op
== O_ne
)
1414 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1415 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1416 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1417 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1418 case O_logical_and
: left
= left
&& right
; break;
1419 case O_logical_or
: left
= left
|| right
; break;
1423 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1424 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1426 if (seg_left
== undefined_section
1427 || seg_right
== undefined_section
)
1428 final_seg
= undefined_section
;
1429 else if (seg_left
== absolute_section
)
1430 final_seg
= seg_right
;
1432 final_seg
= seg_left
;
1434 resolved
= (symbol_resolved_p (add_symbol
)
1435 && symbol_resolved_p (op_symbol
));
1440 /* Give an error (below) if not in expr_section. We don't
1441 want to worry about expr_section symbols, because they
1442 are fictional (they are created as part of expression
1443 resolution), and any problems may not actually mean
1448 symp
->sy_resolving
= 0;
1452 S_SET_VALUE (symp
, final_val
);
1454 exit_dont_set_value
:
1455 /* Always set the segment, even if not finalizing the value.
1456 The segment is used to determine whether a symbol is defined. */
1457 S_SET_SEGMENT (symp
, final_seg
);
1459 /* Don't worry if we can't resolve an expr_section symbol. */
1463 symp
->sy_resolved
= 1;
1464 else if (S_GET_SEGMENT (symp
) != expr_section
)
1466 as_bad (_("can't resolve value for symbol `%s'"),
1468 symp
->sy_resolved
= 1;
1475 static void resolve_local_symbol (const char *, void *);
1477 /* A static function passed to hash_traverse. */
1480 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, void *value
)
1483 resolve_symbol_value ((symbolS
*) value
);
1486 /* Resolve all local symbols. */
1489 resolve_local_symbol_values (void)
1491 hash_traverse (local_hash
, resolve_local_symbol
);
1494 /* Obtain the current value of a symbol without changing any
1495 sub-expressions used. */
1498 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1500 symbolS
*symbolP
= *symbolPP
;
1502 if (LOCAL_SYMBOL_CHECK (symbolP
))
1504 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1506 *valueP
= locsym
->lsy_value
;
1507 *segP
= locsym
->lsy_section
;
1508 *fragPP
= local_symbol_get_frag (locsym
);
1512 expressionS exp
= symbolP
->sy_value
;
1514 if (!symbolP
->sy_resolved
&& exp
.X_op
!= O_illegal
)
1518 if (symbolP
->sy_resolving
)
1520 symbolP
->sy_resolving
= 1;
1521 resolved
= resolve_expression (&exp
);
1522 symbolP
->sy_resolving
= 0;
1530 if (!symbol_equated_p (symbolP
))
1535 symbolP
= exp
.X_add_symbol
;
1542 *symbolPP
= symbolP
;
1543 *valueP
= exp
.X_add_number
;
1544 *segP
= symbolP
->bsym
->section
;
1545 *fragPP
= symbolP
->sy_frag
;
1547 if (*segP
== expr_section
)
1550 case O_constant
: *segP
= absolute_section
; break;
1551 case O_register
: *segP
= reg_section
; break;
1559 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1560 They are *really* local. That is, they go out of scope whenever we see a
1561 label that isn't local. Also, like fb labels, there can be multiple
1562 instances of a dollar label. Therefor, we name encode each instance with
1563 the instance number, keep a list of defined symbols separate from the real
1564 symbol table, and we treat these buggers as a sparse array. */
1566 static long *dollar_labels
;
1567 static long *dollar_label_instances
;
1568 static char *dollar_label_defines
;
1569 static unsigned long dollar_label_count
;
1570 static unsigned long dollar_label_max
;
1573 dollar_label_defined (long label
)
1577 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1579 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1581 return dollar_label_defines
[i
- dollar_labels
];
1583 /* If we get here, label isn't defined. */
1588 dollar_label_instance (long label
)
1592 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1594 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1596 return (dollar_label_instances
[i
- dollar_labels
]);
1598 /* If we get here, we haven't seen the label before.
1599 Therefore its instance count is zero. */
1604 dollar_label_clear (void)
1606 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1609 #define DOLLAR_LABEL_BUMP_BY 10
1612 define_dollar_label (long label
)
1616 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1619 ++dollar_label_instances
[i
- dollar_labels
];
1620 dollar_label_defines
[i
- dollar_labels
] = 1;
1624 /* If we get to here, we don't have label listed yet. */
1626 if (dollar_labels
== NULL
)
1628 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1629 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1630 dollar_label_defines
= (char *) xmalloc (DOLLAR_LABEL_BUMP_BY
);
1631 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1632 dollar_label_count
= 0;
1634 else if (dollar_label_count
== dollar_label_max
)
1636 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1637 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
1638 dollar_label_max
* sizeof (long));
1639 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
1640 dollar_label_max
* sizeof (long));
1641 dollar_label_defines
= (char *) xrealloc (dollar_label_defines
, dollar_label_max
);
1642 } /* if we needed to grow */
1644 dollar_labels
[dollar_label_count
] = label
;
1645 dollar_label_instances
[dollar_label_count
] = 1;
1646 dollar_label_defines
[dollar_label_count
] = 1;
1647 ++dollar_label_count
;
1650 /* Caller must copy returned name: we re-use the area for the next name.
1652 The mth occurence of label n: is turned into the symbol "Ln^Am"
1653 where n is the label number and m is the instance number. "L" makes
1654 it a label discarded unless debugging and "^A"('\1') ensures no
1655 ordinary symbol SHOULD get the same name as a local label
1656 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1658 fb labels get the same treatment, except that ^B is used in place
1661 char * /* Return local label name. */
1662 dollar_label_name (register long n
, /* we just saw "n$:" : n a number. */
1663 register int augend
/* 0 for current instance, 1 for new instance. */)
1666 /* Returned to caller, then copied. Used for created names ("4f"). */
1667 static char symbol_name_build
[24];
1670 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1673 know (augend
== 0 || augend
== 1);
1674 p
= symbol_name_build
;
1675 #ifdef LOCAL_LABEL_PREFIX
1676 *p
++ = LOCAL_LABEL_PREFIX
;
1680 /* Next code just does sprintf( {}, "%d", n); */
1682 q
= symbol_name_temporary
;
1683 for (*q
++ = 0, i
= n
; i
; ++q
)
1688 while ((*p
= *--q
) != '\0')
1691 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1693 /* Instance number. */
1694 q
= symbol_name_temporary
;
1695 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1700 while ((*p
++ = *--q
) != '\0');;
1702 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1703 return symbol_name_build
;
1706 /* Somebody else's idea of local labels. They are made by "n:" where n
1707 is any decimal digit. Refer to them with
1708 "nb" for previous (backward) n:
1709 or "nf" for next (forward) n:.
1711 We do a little better and let n be any number, not just a single digit, but
1712 since the other guy's assembler only does ten, we treat the first ten
1715 Like someone else's assembler, we have one set of local label counters for
1716 entire assembly, not one set per (sub)segment like in most assemblers. This
1717 implies that one can refer to a label in another segment, and indeed some
1718 crufty compilers have done just that.
1720 Since there could be a LOT of these things, treat them as a sparse
1723 #define FB_LABEL_SPECIAL (10)
1725 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1726 static long *fb_labels
;
1727 static long *fb_label_instances
;
1728 static long fb_label_count
;
1729 static long fb_label_max
;
1731 /* This must be more than FB_LABEL_SPECIAL. */
1732 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1735 fb_label_init (void)
1737 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1740 /* Add one to the instance number of this fb label. */
1743 fb_label_instance_inc (long label
)
1747 if (label
< FB_LABEL_SPECIAL
)
1749 ++fb_low_counter
[label
];
1753 if (fb_labels
!= NULL
)
1755 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1756 i
< fb_labels
+ fb_label_count
; ++i
)
1760 ++fb_label_instances
[i
- fb_labels
];
1762 } /* if we find it */
1763 } /* for each existing label */
1766 /* If we get to here, we don't have label listed yet. */
1768 if (fb_labels
== NULL
)
1770 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1771 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1772 fb_label_max
= FB_LABEL_BUMP_BY
;
1773 fb_label_count
= FB_LABEL_SPECIAL
;
1776 else if (fb_label_count
== fb_label_max
)
1778 fb_label_max
+= FB_LABEL_BUMP_BY
;
1779 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1780 fb_label_max
* sizeof (long));
1781 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1782 fb_label_max
* sizeof (long));
1783 } /* if we needed to grow */
1785 fb_labels
[fb_label_count
] = label
;
1786 fb_label_instances
[fb_label_count
] = 1;
1791 fb_label_instance (long label
)
1795 if (label
< FB_LABEL_SPECIAL
)
1797 return (fb_low_counter
[label
]);
1800 if (fb_labels
!= NULL
)
1802 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1803 i
< fb_labels
+ fb_label_count
; ++i
)
1807 return (fb_label_instances
[i
- fb_labels
]);
1808 } /* if we find it */
1809 } /* for each existing label */
1812 /* We didn't find the label, so this must be a reference to the
1817 /* Caller must copy returned name: we re-use the area for the next name.
1819 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1820 where n is the label number and m is the instance number. "L" makes
1821 it a label discarded unless debugging and "^B"('\2') ensures no
1822 ordinary symbol SHOULD get the same name as a local label
1823 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1825 dollar labels get the same treatment, except that ^A is used in
1828 char * /* Return local label name. */
1829 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1830 long augend
/* 0 for nb, 1 for n:, nf. */)
1833 /* Returned to caller, then copied. Used for created names ("4f"). */
1834 static char symbol_name_build
[24];
1837 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1841 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1843 know ((unsigned long) augend
<= 1);
1845 p
= symbol_name_build
;
1846 #ifdef LOCAL_LABEL_PREFIX
1847 *p
++ = LOCAL_LABEL_PREFIX
;
1851 /* Next code just does sprintf( {}, "%d", n); */
1853 q
= symbol_name_temporary
;
1854 for (*q
++ = 0, i
= n
; i
; ++q
)
1859 while ((*p
= *--q
) != '\0')
1862 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1864 /* Instance number. */
1865 q
= symbol_name_temporary
;
1866 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1871 while ((*p
++ = *--q
) != '\0');;
1873 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1874 return (symbol_name_build
);
1877 /* Decode name that may have been generated by foo_label_name() above.
1878 If the name wasn't generated by foo_label_name(), then return it
1879 unaltered. This is used for error messages. */
1882 decode_local_label_name (char *s
)
1885 char *symbol_decode
;
1887 int instance_number
;
1889 const char *message_format
;
1892 #ifdef LOCAL_LABEL_PREFIX
1893 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
1897 if (s
[lindex
] != 'L')
1900 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
1901 label_number
= (10 * label_number
) + *p
- '0';
1903 if (*p
== DOLLAR_LABEL_CHAR
)
1905 else if (*p
== LOCAL_LABEL_CHAR
)
1910 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1911 instance_number
= (10 * instance_number
) + *p
- '0';
1913 message_format
= _("\"%d\" (instance number %d of a %s label)");
1914 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
1915 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1917 return symbol_decode
;
1920 /* Get the value of a symbol. */
1923 S_GET_VALUE (symbolS
*s
)
1925 if (LOCAL_SYMBOL_CHECK (s
))
1926 return resolve_symbol_value (s
);
1928 if (!s
->sy_resolved
)
1930 valueT val
= resolve_symbol_value (s
);
1934 if (S_IS_WEAKREFR (s
))
1935 return S_GET_VALUE (s
->sy_value
.X_add_symbol
);
1937 if (s
->sy_value
.X_op
!= O_constant
)
1939 if (! s
->sy_resolved
1940 || s
->sy_value
.X_op
!= O_symbol
1941 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1942 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1945 return (valueT
) s
->sy_value
.X_add_number
;
1948 /* Set the value of a symbol. */
1951 S_SET_VALUE (symbolS
*s
, valueT val
)
1953 if (LOCAL_SYMBOL_CHECK (s
))
1955 ((struct local_symbol
*) s
)->lsy_value
= val
;
1959 s
->sy_value
.X_op
= O_constant
;
1960 s
->sy_value
.X_add_number
= (offsetT
) val
;
1961 s
->sy_value
.X_unsigned
= 0;
1962 S_CLEAR_WEAKREFR (s
);
1966 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1968 if (LOCAL_SYMBOL_CHECK (dest
))
1969 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
1970 if (LOCAL_SYMBOL_CHECK (src
))
1971 src
= local_symbol_convert ((struct local_symbol
*) src
);
1973 /* In an expression, transfer the settings of these flags.
1974 The user can override later, of course. */
1975 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1976 | BSF_GNU_INDIRECT_FUNCTION)
1977 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1979 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1980 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1983 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1984 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1989 S_IS_FUNCTION (symbolS
*s
)
1993 if (LOCAL_SYMBOL_CHECK (s
))
1996 flags
= s
->bsym
->flags
;
1998 return (flags
& BSF_FUNCTION
) != 0;
2002 S_IS_EXTERNAL (symbolS
*s
)
2006 if (LOCAL_SYMBOL_CHECK (s
))
2009 flags
= s
->bsym
->flags
;
2012 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2015 return (flags
& BSF_GLOBAL
) != 0;
2019 S_IS_WEAK (symbolS
*s
)
2021 if (LOCAL_SYMBOL_CHECK (s
))
2023 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2024 could probably handle a WEAKREFR as always weak though. E.g., if
2025 the referenced symbol has lost its weak status, there's no reason
2026 to keep handling the weakrefr as if it was weak. */
2027 if (S_IS_WEAKREFR (s
))
2028 return S_IS_WEAK (s
->sy_value
.X_add_symbol
);
2029 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2033 S_IS_WEAKREFR (symbolS
*s
)
2035 if (LOCAL_SYMBOL_CHECK (s
))
2037 return s
->sy_weakrefr
!= 0;
2041 S_IS_WEAKREFD (symbolS
*s
)
2043 if (LOCAL_SYMBOL_CHECK (s
))
2045 return s
->sy_weakrefd
!= 0;
2049 S_IS_COMMON (symbolS
*s
)
2051 if (LOCAL_SYMBOL_CHECK (s
))
2053 return bfd_is_com_section (s
->bsym
->section
);
2057 S_IS_DEFINED (symbolS
*s
)
2059 if (LOCAL_SYMBOL_CHECK (s
))
2060 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
2061 return s
->bsym
->section
!= undefined_section
;
2065 #ifndef EXTERN_FORCE_RELOC
2066 #define EXTERN_FORCE_RELOC IS_ELF
2069 /* Return true for symbols that should not be reduced to section
2070 symbols or eliminated from expressions, because they may be
2071 overridden by the linker. */
2073 S_FORCE_RELOC (symbolS
*s
, int strict
)
2075 if (LOCAL_SYMBOL_CHECK (s
))
2076 return ((struct local_symbol
*) s
)->lsy_section
== undefined_section
;
2079 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2080 || (EXTERN_FORCE_RELOC
2081 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2082 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0
2083 || s
->bsym
->section
== undefined_section
2084 || bfd_is_com_section (s
->bsym
->section
));
2088 S_IS_DEBUG (symbolS
*s
)
2090 if (LOCAL_SYMBOL_CHECK (s
))
2092 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2098 S_IS_LOCAL (symbolS
*s
)
2103 if (LOCAL_SYMBOL_CHECK (s
))
2106 flags
= s
->bsym
->flags
;
2109 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2112 if (bfd_get_section (s
->bsym
) == reg_section
)
2115 if (flag_strip_local_absolute
2116 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2117 the source file even when the object file is stripped. */
2118 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2119 && bfd_get_section (s
->bsym
) == absolute_section
)
2122 name
= S_GET_NAME (s
);
2123 return (name
!= NULL
2125 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2126 || strchr (name
, LOCAL_LABEL_CHAR
)
2127 || TC_LABEL_IS_LOCAL (name
)
2128 || (! flag_keep_locals
2129 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2132 && name
[1] == '?')))));
2136 S_IS_STABD (symbolS
*s
)
2138 return S_GET_NAME (s
) == 0;
2142 S_IS_VOLATILE (const symbolS
*s
)
2144 if (LOCAL_SYMBOL_CHECK (s
))
2146 return s
->sy_volatile
;
2150 S_IS_FORWARD_REF (const symbolS
*s
)
2152 if (LOCAL_SYMBOL_CHECK (s
))
2154 return s
->sy_forward_ref
;
2158 S_GET_NAME (symbolS
*s
)
2160 if (LOCAL_SYMBOL_CHECK (s
))
2161 return ((struct local_symbol
*) s
)->lsy_name
;
2162 return s
->bsym
->name
;
2166 S_GET_SEGMENT (symbolS
*s
)
2168 if (LOCAL_SYMBOL_CHECK (s
))
2169 return ((struct local_symbol
*) s
)->lsy_section
;
2170 return s
->bsym
->section
;
2174 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2176 /* Don't reassign section symbols. The direct reason is to prevent seg
2177 faults assigning back to const global symbols such as *ABS*, but it
2178 shouldn't happen anyway. */
2180 if (LOCAL_SYMBOL_CHECK (s
))
2182 if (seg
== reg_section
)
2183 s
= local_symbol_convert ((struct local_symbol
*) s
);
2186 ((struct local_symbol
*) s
)->lsy_section
= seg
;
2191 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2193 if (s
->bsym
->section
!= seg
)
2197 s
->bsym
->section
= seg
;
2201 S_SET_EXTERNAL (symbolS
*s
)
2203 if (LOCAL_SYMBOL_CHECK (s
))
2204 s
= local_symbol_convert ((struct local_symbol
*) s
);
2205 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2207 /* Let .weak override .global. */
2210 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2215 /* Do not reassign section symbols. */
2216 as_where (& file
, & line
);
2217 as_warn_where (file
, line
,
2218 _("section symbols are already global"));
2221 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2222 if (S_GET_SEGMENT (s
) == reg_section
)
2224 as_bad ("can't make register symbol `%s' global",
2229 s
->bsym
->flags
|= BSF_GLOBAL
;
2230 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2233 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2234 an_external_name
= S_GET_NAME (s
);
2239 S_CLEAR_EXTERNAL (symbolS
*s
)
2241 if (LOCAL_SYMBOL_CHECK (s
))
2243 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2245 /* Let .weak override. */
2248 s
->bsym
->flags
|= BSF_LOCAL
;
2249 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2253 S_SET_WEAK (symbolS
*s
)
2255 if (LOCAL_SYMBOL_CHECK (s
))
2256 s
= local_symbol_convert ((struct local_symbol
*) s
);
2257 #ifdef obj_set_weak_hook
2258 obj_set_weak_hook (s
);
2260 s
->bsym
->flags
|= BSF_WEAK
;
2261 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2265 S_SET_WEAKREFR (symbolS
*s
)
2267 if (LOCAL_SYMBOL_CHECK (s
))
2268 s
= local_symbol_convert ((struct local_symbol
*) s
);
2270 /* If the alias was already used, make sure we mark the target as
2271 used as well, otherwise it might be dropped from the symbol
2272 table. This may have unintended side effects if the alias is
2273 later redirected to another symbol, such as keeping the unused
2274 previous target in the symbol table. Since it will be weak, it's
2277 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2281 S_CLEAR_WEAKREFR (symbolS
*s
)
2283 if (LOCAL_SYMBOL_CHECK (s
))
2289 S_SET_WEAKREFD (symbolS
*s
)
2291 if (LOCAL_SYMBOL_CHECK (s
))
2292 s
= local_symbol_convert ((struct local_symbol
*) s
);
2298 S_CLEAR_WEAKREFD (symbolS
*s
)
2300 if (LOCAL_SYMBOL_CHECK (s
))
2305 /* If a weakref target symbol is weak, then it was never
2306 referenced directly before, not even in a .global directive,
2307 so decay it to local. If it remains undefined, it will be
2308 later turned into a global, like any other undefined
2310 if (s
->bsym
->flags
& BSF_WEAK
)
2312 #ifdef obj_clear_weak_hook
2313 obj_clear_weak_hook (s
);
2315 s
->bsym
->flags
&= ~BSF_WEAK
;
2316 s
->bsym
->flags
|= BSF_LOCAL
;
2322 S_SET_THREAD_LOCAL (symbolS
*s
)
2324 if (LOCAL_SYMBOL_CHECK (s
))
2325 s
= local_symbol_convert ((struct local_symbol
*) s
);
2326 if (bfd_is_com_section (s
->bsym
->section
)
2327 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2329 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2330 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2331 as_bad (_("Accessing function `%s' as thread-local object"),
2333 else if (! bfd_is_und_section (s
->bsym
->section
)
2334 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2335 as_bad (_("Accessing `%s' as thread-local object"),
2340 S_SET_NAME (symbolS
*s
, const char *name
)
2342 if (LOCAL_SYMBOL_CHECK (s
))
2344 ((struct local_symbol
*) s
)->lsy_name
= name
;
2347 s
->bsym
->name
= name
;
2351 S_SET_VOLATILE (symbolS
*s
)
2353 if (LOCAL_SYMBOL_CHECK (s
))
2354 s
= local_symbol_convert ((struct local_symbol
*) s
);
2359 S_CLEAR_VOLATILE (symbolS
*s
)
2361 if (!LOCAL_SYMBOL_CHECK (s
))
2366 S_SET_FORWARD_REF (symbolS
*s
)
2368 if (LOCAL_SYMBOL_CHECK (s
))
2369 s
= local_symbol_convert ((struct local_symbol
*) s
);
2370 s
->sy_forward_ref
= 1;
2373 /* Return the previous symbol in a chain. */
2376 symbol_previous (symbolS
*s
)
2378 if (LOCAL_SYMBOL_CHECK (s
))
2380 return s
->sy_previous
;
2383 /* Return the next symbol in a chain. */
2386 symbol_next (symbolS
*s
)
2388 if (LOCAL_SYMBOL_CHECK (s
))
2393 /* Return a pointer to the value of a symbol as an expression. */
2396 symbol_get_value_expression (symbolS
*s
)
2398 if (LOCAL_SYMBOL_CHECK (s
))
2399 s
= local_symbol_convert ((struct local_symbol
*) s
);
2400 return &s
->sy_value
;
2403 /* Set the value of a symbol to an expression. */
2406 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2408 if (LOCAL_SYMBOL_CHECK (s
))
2409 s
= local_symbol_convert ((struct local_symbol
*) s
);
2411 S_CLEAR_WEAKREFR (s
);
2414 /* Return whether 2 symbols are the same. */
2417 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2419 if (s1
->bsym
== NULL
2420 && local_symbol_converted_p ((struct local_symbol
*) s1
))
2421 s1
= local_symbol_get_real_symbol ((struct local_symbol
*) s1
);
2422 if (s2
->bsym
== NULL
2423 && local_symbol_converted_p ((struct local_symbol
*) s2
))
2424 s2
= local_symbol_get_real_symbol ((struct local_symbol
*) s2
);
2428 /* Return a pointer to the X_add_number component of a symbol. */
2431 symbol_X_add_number (symbolS
*s
)
2433 if (LOCAL_SYMBOL_CHECK (s
))
2434 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
2436 return &s
->sy_value
.X_add_number
;
2439 /* Set the value of SYM to the current position in the current segment. */
2442 symbol_set_value_now (symbolS
*sym
)
2444 S_SET_SEGMENT (sym
, now_seg
);
2445 S_SET_VALUE (sym
, frag_now_fix ());
2446 symbol_set_frag (sym
, frag_now
);
2449 /* Set the frag of a symbol. */
2452 symbol_set_frag (symbolS
*s
, fragS
*f
)
2454 if (LOCAL_SYMBOL_CHECK (s
))
2456 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2460 S_CLEAR_WEAKREFR (s
);
2463 /* Return the frag of a symbol. */
2466 symbol_get_frag (symbolS
*s
)
2468 if (LOCAL_SYMBOL_CHECK (s
))
2469 return local_symbol_get_frag ((struct local_symbol
*) s
);
2473 /* Mark a symbol as having been used. */
2476 symbol_mark_used (symbolS
*s
)
2478 if (LOCAL_SYMBOL_CHECK (s
))
2481 if (S_IS_WEAKREFR (s
))
2482 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2485 /* Clear the mark of whether a symbol has been used. */
2488 symbol_clear_used (symbolS
*s
)
2490 if (LOCAL_SYMBOL_CHECK (s
))
2491 s
= local_symbol_convert ((struct local_symbol
*) s
);
2495 /* Return whether a symbol has been used. */
2498 symbol_used_p (symbolS
*s
)
2500 if (LOCAL_SYMBOL_CHECK (s
))
2505 /* Mark a symbol as having been used in a reloc. */
2508 symbol_mark_used_in_reloc (symbolS
*s
)
2510 if (LOCAL_SYMBOL_CHECK (s
))
2511 s
= local_symbol_convert ((struct local_symbol
*) s
);
2512 s
->sy_used_in_reloc
= 1;
2515 /* Clear the mark of whether a symbol has been used in a reloc. */
2518 symbol_clear_used_in_reloc (symbolS
*s
)
2520 if (LOCAL_SYMBOL_CHECK (s
))
2522 s
->sy_used_in_reloc
= 0;
2525 /* Return whether a symbol has been used in a reloc. */
2528 symbol_used_in_reloc_p (symbolS
*s
)
2530 if (LOCAL_SYMBOL_CHECK (s
))
2532 return s
->sy_used_in_reloc
;
2535 /* Mark a symbol as an MRI common symbol. */
2538 symbol_mark_mri_common (symbolS
*s
)
2540 if (LOCAL_SYMBOL_CHECK (s
))
2541 s
= local_symbol_convert ((struct local_symbol
*) s
);
2542 s
->sy_mri_common
= 1;
2545 /* Clear the mark of whether a symbol is an MRI common symbol. */
2548 symbol_clear_mri_common (symbolS
*s
)
2550 if (LOCAL_SYMBOL_CHECK (s
))
2552 s
->sy_mri_common
= 0;
2555 /* Return whether a symbol is an MRI common symbol. */
2558 symbol_mri_common_p (symbolS
*s
)
2560 if (LOCAL_SYMBOL_CHECK (s
))
2562 return s
->sy_mri_common
;
2565 /* Mark a symbol as having been written. */
2568 symbol_mark_written (symbolS
*s
)
2570 if (LOCAL_SYMBOL_CHECK (s
))
2575 /* Clear the mark of whether a symbol has been written. */
2578 symbol_clear_written (symbolS
*s
)
2580 if (LOCAL_SYMBOL_CHECK (s
))
2585 /* Return whether a symbol has been written. */
2588 symbol_written_p (symbolS
*s
)
2590 if (LOCAL_SYMBOL_CHECK (s
))
2595 /* Mark a symbol has having been resolved. */
2598 symbol_mark_resolved (symbolS
*s
)
2600 if (LOCAL_SYMBOL_CHECK (s
))
2602 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2608 /* Return whether a symbol has been resolved. */
2611 symbol_resolved_p (symbolS
*s
)
2613 if (LOCAL_SYMBOL_CHECK (s
))
2614 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2615 return s
->sy_resolved
;
2618 /* Return whether a symbol is a section symbol. */
2621 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2623 if (LOCAL_SYMBOL_CHECK (s
))
2625 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2628 /* Return whether a symbol is equated to another symbol. */
2631 symbol_equated_p (symbolS
*s
)
2633 if (LOCAL_SYMBOL_CHECK (s
))
2635 return s
->sy_value
.X_op
== O_symbol
;
2638 /* Return whether a symbol is equated to another symbol, and should be
2639 treated specially when writing out relocs. */
2642 symbol_equated_reloc_p (symbolS
*s
)
2644 if (LOCAL_SYMBOL_CHECK (s
))
2646 /* X_op_symbol, normally not used for O_symbol, is set by
2647 resolve_symbol_value to flag expression syms that have been
2649 return (s
->sy_value
.X_op
== O_symbol
2650 #if defined (OBJ_COFF) && defined (TE_PE)
2653 && ((s
->sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2654 || ! S_IS_DEFINED (s
)
2655 || S_IS_COMMON (s
)));
2658 /* Return whether a symbol has a constant value. */
2661 symbol_constant_p (symbolS
*s
)
2663 if (LOCAL_SYMBOL_CHECK (s
))
2665 return s
->sy_value
.X_op
== O_constant
;
2668 /* Return whether a symbol was cloned and thus removed from the global
2672 symbol_shadow_p (symbolS
*s
)
2674 if (LOCAL_SYMBOL_CHECK (s
))
2676 return s
->sy_next
== s
;
2679 /* Return the BFD symbol for a symbol. */
2682 symbol_get_bfdsym (symbolS
*s
)
2684 if (LOCAL_SYMBOL_CHECK (s
))
2685 s
= local_symbol_convert ((struct local_symbol
*) s
);
2689 /* Set the BFD symbol for a symbol. */
2692 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2694 if (LOCAL_SYMBOL_CHECK (s
))
2695 s
= local_symbol_convert ((struct local_symbol
*) s
);
2696 /* Usually, it is harmless to reset a symbol to a BFD section
2697 symbol. For example, obj_elf_change_section sets the BFD symbol
2698 of an old symbol with the newly created section symbol. But when
2699 we have multiple sections with the same name, the newly created
2700 section may have the same name as an old section. We check if the
2701 old symbol has been already marked as a section symbol before
2703 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2705 /* else XXX - What do we do now ? */
2708 #ifdef OBJ_SYMFIELD_TYPE
2710 /* Get a pointer to the object format information for a symbol. */
2713 symbol_get_obj (symbolS
*s
)
2715 if (LOCAL_SYMBOL_CHECK (s
))
2716 s
= local_symbol_convert ((struct local_symbol
*) s
);
2720 /* Set the object format information for a symbol. */
2723 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2725 if (LOCAL_SYMBOL_CHECK (s
))
2726 s
= local_symbol_convert ((struct local_symbol
*) s
);
2730 #endif /* OBJ_SYMFIELD_TYPE */
2732 #ifdef TC_SYMFIELD_TYPE
2734 /* Get a pointer to the processor information for a symbol. */
2737 symbol_get_tc (symbolS
*s
)
2739 if (LOCAL_SYMBOL_CHECK (s
))
2740 s
= local_symbol_convert ((struct local_symbol
*) s
);
2744 /* Set the processor information for a symbol. */
2747 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2749 if (LOCAL_SYMBOL_CHECK (s
))
2750 s
= local_symbol_convert ((struct local_symbol
*) s
);
2754 #endif /* TC_SYMFIELD_TYPE */
2759 symbol_lastP
= NULL
;
2760 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2761 sy_hash
= hash_new ();
2762 local_hash
= hash_new ();
2764 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2765 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2766 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
2768 abs_symbol
.sy_value
.X_op
= O_constant
;
2769 abs_symbol
.sy_frag
= &zero_address_frag
;
2771 if (LOCAL_LABELS_FB
)
2776 dot_symbol_init (void)
2778 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2779 if (dot_symbol
.bsym
== NULL
)
2780 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2781 dot_symbol
.bsym
->name
= ".";
2782 dot_symbol
.sy_forward_ref
= 1;
2783 dot_symbol
.sy_value
.X_op
= O_constant
;
2788 /* Maximum indent level.
2789 Available for modification inside a gdb session. */
2790 static int max_indent_level
= 8;
2793 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2795 const char *name
= S_GET_NAME (sym
);
2796 if (!name
|| !name
[0])
2798 fprintf (file
, "sym ");
2799 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
2800 fprintf (file
, " %s", name
);
2802 if (LOCAL_SYMBOL_CHECK (sym
))
2804 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2806 if (local_symbol_get_frag (locsym
) != & zero_address_frag
2807 && local_symbol_get_frag (locsym
) != NULL
)
2809 fprintf (file
, " frag ");
2810 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) local_symbol_get_frag (locsym
)));
2812 if (local_symbol_resolved_p (locsym
))
2813 fprintf (file
, " resolved");
2814 fprintf (file
, " local");
2818 if (sym
->sy_frag
!= &zero_address_frag
)
2820 fprintf (file
, " frag ");
2821 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->sy_frag
));
2824 fprintf (file
, " written");
2825 if (sym
->sy_resolved
)
2826 fprintf (file
, " resolved");
2827 else if (sym
->sy_resolving
)
2828 fprintf (file
, " resolving");
2829 if (sym
->sy_used_in_reloc
)
2830 fprintf (file
, " used-in-reloc");
2832 fprintf (file
, " used");
2833 if (S_IS_LOCAL (sym
))
2834 fprintf (file
, " local");
2835 if (S_IS_EXTERNAL (sym
))
2836 fprintf (file
, " extern");
2837 if (S_IS_WEAK (sym
))
2838 fprintf (file
, " weak");
2839 if (S_IS_DEBUG (sym
))
2840 fprintf (file
, " debug");
2841 if (S_IS_DEFINED (sym
))
2842 fprintf (file
, " defined");
2844 if (S_IS_WEAKREFR (sym
))
2845 fprintf (file
, " weakrefr");
2846 if (S_IS_WEAKREFD (sym
))
2847 fprintf (file
, " weakrefd");
2848 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2849 if (symbol_resolved_p (sym
))
2851 segT s
= S_GET_SEGMENT (sym
);
2853 if (s
!= undefined_section
2854 && s
!= expr_section
)
2855 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
2857 else if (indent_level
< max_indent_level
2858 && S_GET_SEGMENT (sym
) != undefined_section
)
2861 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2862 if (LOCAL_SYMBOL_CHECK (sym
))
2863 fprintf (file
, "constant %lx",
2864 (unsigned long) ((struct local_symbol
*) sym
)->lsy_value
);
2866 print_expr_1 (file
, &sym
->sy_value
);
2867 fprintf (file
, ">");
2874 print_symbol_value (symbolS
*sym
)
2877 print_symbol_value_1 (stderr
, sym
);
2878 fprintf (stderr
, "\n");
2882 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2885 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2886 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2887 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2888 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2889 fprintf (file
, ">");
2894 print_expr_1 (FILE *file
, expressionS
*exp
)
2896 fprintf (file
, "expr ");
2897 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
2898 fprintf (file
, " ");
2902 fprintf (file
, "illegal");
2905 fprintf (file
, "absent");
2908 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
2912 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2913 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2914 fprintf (file
, ">");
2916 if (exp
->X_add_number
)
2917 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2918 (unsigned long) exp
->X_add_number
);
2922 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2925 fprintf (file
, "big");
2928 fprintf (file
, "uminus -<");
2930 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2931 fprintf (file
, ">");
2932 goto maybe_print_addnum
;
2934 fprintf (file
, "bit_not");
2937 print_binary (file
, "multiply", exp
);
2940 print_binary (file
, "divide", exp
);
2943 print_binary (file
, "modulus", exp
);
2946 print_binary (file
, "lshift", exp
);
2949 print_binary (file
, "rshift", exp
);
2951 case O_bit_inclusive_or
:
2952 print_binary (file
, "bit_ior", exp
);
2954 case O_bit_exclusive_or
:
2955 print_binary (file
, "bit_xor", exp
);
2958 print_binary (file
, "bit_and", exp
);
2961 print_binary (file
, "eq", exp
);
2964 print_binary (file
, "ne", exp
);
2967 print_binary (file
, "lt", exp
);
2970 print_binary (file
, "le", exp
);
2973 print_binary (file
, "ge", exp
);
2976 print_binary (file
, "gt", exp
);
2979 print_binary (file
, "logical_and", exp
);
2982 print_binary (file
, "logical_or", exp
);
2986 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
2987 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2988 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2989 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2990 fprintf (file
, ">");
2991 goto maybe_print_addnum
;
2994 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
2995 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2996 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2997 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2998 fprintf (file
, ">");
2999 goto maybe_print_addnum
;
3001 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3008 print_expr (expressionS
*exp
)
3010 print_expr_1 (stderr
, exp
);
3011 fprintf (stderr
, "\n");
3015 symbol_print_statistics (FILE *file
)
3017 hash_print_statistics (file
, "symbol table", sy_hash
);
3018 hash_print_statistics (file
, "mini local symbol table", local_hash
);
3019 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3020 local_symbol_count
, local_symbol_conversion_count
);
3023 #ifdef OBJ_COMPLEX_RELC
3025 /* Convert given symbol to a new complex-relocation symbol name. This
3026 may be a recursive function, since it might be called for non-leaf
3027 nodes (plain symbols) in the expression tree. The caller owns the
3028 returning string, so should free it eventually. Errors are
3029 indicated via as_bad and a NULL return value. The given symbol
3030 is marked with sy_used_in_reloc. */
3033 symbol_relc_make_sym (symbolS
* sym
)
3035 char * terminal
= NULL
;
3040 gas_assert (sym
!= NULL
);
3042 /* Recurse to symbol_relc_make_expr if this symbol
3043 is defined as an expression or a plain value. */
3044 if ( S_GET_SEGMENT (sym
) == expr_section
3045 || S_GET_SEGMENT (sym
) == absolute_section
)
3046 return symbol_relc_make_expr (& sym
->sy_value
);
3048 /* This may be a "fake symbol" L0\001, referring to ".".
3049 Write out a special null symbol to refer to this position. */
3050 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3051 return xstrdup (".");
3053 /* We hope this is a plain leaf symbol. Construct the encoding
3054 as {S,s}II...:CCCCCCC....
3055 where 'S'/'s' means section symbol / plain symbol
3056 III is decimal for the symbol name length
3057 CCC is the symbol name itself. */
3058 symbol_mark_used_in_reloc (sym
);
3060 sname
= S_GET_NAME (sym
);
3061 sname_len
= strlen (sname
);
3062 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3064 terminal
= xmalloc (1 /* S or s */
3065 + 8 /* sname_len in decimal */
3067 + sname_len
/* name itself */
3070 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3074 /* Convert given value to a new complex-relocation symbol name. This
3075 is a non-recursive function, since it is be called for leaf nodes
3076 (plain values) in the expression tree. The caller owns the
3077 returning string, so should free() it eventually. No errors. */
3080 symbol_relc_make_value (offsetT val
)
3082 char * terminal
= xmalloc (28); /* Enough for long long. */
3085 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3089 /* Convert given expression to a new complex-relocation symbol name.
3090 This is a recursive function, since it traverses the entire given
3091 expression tree. The caller owns the returning string, so should
3092 free() it eventually. Errors are indicated via as_bad() and a NULL
3096 symbol_relc_make_expr (expressionS
* exp
)
3098 char * opstr
= NULL
; /* Operator prefix string. */
3099 int arity
= 0; /* Arity of this operator. */
3100 char * operands
[3]; /* Up to three operands. */
3101 char * concat_string
= NULL
;
3103 operands
[0] = operands
[1] = operands
[2] = NULL
;
3105 gas_assert (exp
!= NULL
);
3107 /* Match known operators -> fill in opstr, arity, operands[] and fall
3108 through to construct subexpression fragments; may instead return
3109 string directly for leaf nodes. */
3111 /* See expr.h for the meaning of all these enums. Many operators
3112 have an unnatural arity (X_add_number implicitly added). The
3113 conversion logic expands them to explicit "+" subexpressions. */
3118 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3123 return symbol_relc_make_value (exp
->X_add_number
);
3126 if (exp
->X_add_number
)
3130 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3131 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3135 return symbol_relc_make_sym (exp
->X_add_symbol
);
3137 /* Helper macros for nesting nodes. */
3139 #define HANDLE_XADD_OPT1(str_) \
3140 if (exp->X_add_number) \
3143 opstr = "+:" str_; \
3144 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3145 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3152 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3156 #define HANDLE_XADD_OPT2(str_) \
3157 if (exp->X_add_number) \
3160 opstr = "+:" str_; \
3161 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3162 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3163 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3169 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3170 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3174 /* Nesting nodes. */
3176 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3177 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3178 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3179 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3180 case O_divide
: HANDLE_XADD_OPT2 ("/");
3181 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3182 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3183 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3184 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3185 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3186 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3187 case O_add
: HANDLE_XADD_OPT2 ("+");
3188 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3189 case O_eq
: HANDLE_XADD_OPT2 ("==");
3190 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3191 case O_lt
: HANDLE_XADD_OPT2 ("<");
3192 case O_le
: HANDLE_XADD_OPT2 ("<=");
3193 case O_ge
: HANDLE_XADD_OPT2 (">=");
3194 case O_gt
: HANDLE_XADD_OPT2 (">");
3195 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3196 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3199 /* Validate & reject early. */
3200 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3202 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3204 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3208 concat_string
= NULL
;
3211 /* Allocate new string; include inter-operand padding gaps etc. */
3212 concat_string
= xmalloc (strlen (opstr
)
3214 + (arity
>= 1 ? (strlen (operands
[0]) + 1 ) : 0)
3215 + (arity
>= 2 ? (strlen (operands
[1]) + 1 ) : 0)
3216 + (arity
>= 3 ? (strlen (operands
[2]) + 0 ) : 0)
3218 gas_assert (concat_string
!= NULL
);
3220 /* Format the thing. */
3221 sprintf (concat_string
,
3222 (arity
== 0 ? "%s" :
3223 arity
== 1 ? "%s:%s" :
3224 arity
== 2 ? "%s:%s:%s" :
3225 /* arity == 3 */ "%s:%s:%s:%s"),
3226 opstr
, operands
[0], operands
[1], operands
[2]);
3229 /* Free operand strings (not opstr). */
3230 if (arity
>= 1) xfree (operands
[0]);
3231 if (arity
>= 2) xfree (operands
[1]);
3232 if (arity
>= 3) xfree (operands
[2]);
3234 return concat_string
;