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. */
26 #include "safe-ctype.h"
27 #include "obstack.h" /* For "symbols.h" */
29 #include "struc-symbol.h"
31 /* This is non-zero if symbols are case sensitive, which is the
33 int symbols_case_sensitive
= 1;
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words
;
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control
*sy_hash
;
42 /* Table of local symbols. */
43 static struct hash_control
*local_hash
;
45 /* Below are commented in "symbols.h". */
46 symbolS
*symbol_rootP
;
47 symbolS
*symbol_lastP
;
52 #define debug_verify_symchain verify_symbol_chain
54 #define debug_verify_symchain(root, last) ((void) 0)
57 #define DOLLAR_LABEL_CHAR '\001'
58 #define LOCAL_LABEL_CHAR '\002'
60 #ifndef TC_LABEL_IS_LOCAL
61 #define TC_LABEL_IS_LOCAL(name) 0
66 /* The name of an external symbol which is
67 used to make weak PE symbol names unique. */
68 const char * an_external_name
;
71 static char *save_symbol_name (const char *);
72 static void fb_label_init (void);
73 static long dollar_label_instance (long);
74 static long fb_label_instance (long);
76 static void print_binary (FILE *, const char *, expressionS
*);
78 /* Return a pointer to a new symbol. Die if we can't make a new
79 symbol. Fill in the symbol's values. Add symbol to end of symbol
82 This function should be called in the general case of creating a
83 symbol. However, if the output file symbol table has already been
84 set, and you are certain that this symbol won't be wanted in the
85 output file, you can call symbol_create. */
88 symbol_new (const char *name
, segT segment
, valueT valu
, fragS
*frag
)
90 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
92 /* Link to end of symbol chain. */
94 extern int symbol_table_frozen
;
95 if (symbol_table_frozen
)
98 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
103 /* Save a symbol name on a permanent obstack, and convert it according
104 to the object file format. */
107 save_symbol_name (const char *name
)
109 unsigned int name_length
;
112 name_length
= strlen (name
) + 1; /* +1 for \0. */
113 obstack_grow (¬es
, name
, name_length
);
114 ret
= (char *) obstack_finish (¬es
);
116 #ifdef tc_canonicalize_symbol_name
117 ret
= tc_canonicalize_symbol_name (ret
);
120 if (! symbols_case_sensitive
)
124 for (s
= ret
; *s
!= '\0'; s
++)
132 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
133 segT segment
, /* Segment identifier (SEG_<something>). */
134 valueT valu
, /* Symbol value. */
135 fragS
*frag
/* Associated fragment. */)
137 char *preserved_copy_of_name
;
140 preserved_copy_of_name
= save_symbol_name (name
);
142 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
144 /* symbol must be born in some fixed state. This seems as good as any. */
145 memset (symbolP
, 0, sizeof (symbolS
));
147 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
148 if (symbolP
->bsym
== NULL
)
149 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
150 S_SET_NAME (symbolP
, preserved_copy_of_name
);
152 S_SET_SEGMENT (symbolP
, segment
);
153 S_SET_VALUE (symbolP
, valu
);
154 symbol_clear_list_pointers (symbolP
);
156 symbolP
->sy_frag
= frag
;
158 obj_symbol_new_hook (symbolP
);
160 #ifdef tc_symbol_new_hook
161 tc_symbol_new_hook (symbolP
);
168 /* Local symbol support. If we can get away with it, we keep only a
169 small amount of information for local symbols. */
171 static symbolS
*local_symbol_convert (struct local_symbol
*);
173 /* Used for statistics. */
175 static unsigned long local_symbol_count
;
176 static unsigned long local_symbol_conversion_count
;
178 /* This macro is called with a symbol argument passed by reference.
179 It returns whether this is a local symbol. If necessary, it
180 changes its argument to the real symbol. */
182 #define LOCAL_SYMBOL_CHECK(s) \
183 (s->sy_flags.sy_local_symbol \
184 ? (local_symbol_converted_p ((struct local_symbol *) s) \
185 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
190 /* Create a local symbol and insert it into the local hash table. */
192 struct local_symbol
*
193 local_symbol_make (const char *name
, segT section
, valueT val
, fragS
*frag
)
196 struct local_symbol
*ret
;
198 ++local_symbol_count
;
200 name_copy
= save_symbol_name (name
);
202 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
203 ret
->lsy_flags
.sy_local_symbol
= 1;
204 ret
->lsy_flags
.sy_resolved
= 0;
205 ret
->lsy_name
= name_copy
;
206 ret
->lsy_section
= section
;
207 local_symbol_set_frag (ret
, frag
);
208 ret
->lsy_value
= val
;
210 hash_jam (local_hash
, name_copy
, (void *) ret
);
215 /* Convert a local symbol into a real symbol. Note that we do not
216 reclaim the space used by the local symbol. */
219 local_symbol_convert (struct local_symbol
*locsym
)
223 gas_assert (locsym
->lsy_flags
.sy_local_symbol
);
224 if (local_symbol_converted_p (locsym
))
225 return local_symbol_get_real_symbol (locsym
);
227 ++local_symbol_conversion_count
;
229 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
230 local_symbol_get_frag (locsym
));
232 if (local_symbol_resolved_p (locsym
))
233 ret
->sy_flags
.sy_resolved
= 1;
235 /* Local symbols are always either defined or used. */
236 ret
->sy_flags
.sy_used
= 1;
238 #ifdef TC_LOCAL_SYMFIELD_CONVERT
239 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
242 symbol_table_insert (ret
);
244 local_symbol_mark_converted (locsym
);
245 local_symbol_set_real_symbol (locsym
, ret
);
247 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
253 define_sym_at_dot (symbolS
*symbolP
)
255 symbolP
->sy_frag
= frag_now
;
256 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
257 S_SET_SEGMENT (symbolP
, now_seg
);
260 /* We have just seen "<name>:".
261 Creates a struct symbol unless it already exists.
263 Gripes if we are redefining a symbol incompatibly (and ignores it). */
266 colon (/* Just seen "x:" - rattle symbols & frags. */
267 const char *sym_name
/* Symbol name, as a cannonical string. */
268 /* We copy this string: OK to alter later. */)
270 register symbolS
*symbolP
; /* Symbol we are working with. */
272 /* Sun local labels go out of scope whenever a non-local symbol is
274 if (LOCAL_LABELS_DOLLAR
275 && !bfd_is_local_label_name (stdoutput
, sym_name
))
276 dollar_label_clear ();
278 #ifndef WORKING_DOT_WORD
279 if (new_broken_words
)
281 struct broken_word
*a
;
286 if (now_seg
== absolute_section
)
288 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
292 possible_bytes
= (md_short_jump_size
293 + new_broken_words
* md_long_jump_size
);
296 frag_opcode
= frag_var (rs_broken_word
,
300 (symbolS
*) broken_words
,
304 /* We want to store the pointer to where to insert the jump
305 table in the fr_opcode of the rs_broken_word frag. This
306 requires a little hackery. */
308 && (frag_tmp
->fr_type
!= rs_broken_word
309 || frag_tmp
->fr_opcode
))
310 frag_tmp
= frag_tmp
->fr_next
;
312 frag_tmp
->fr_opcode
= frag_opcode
;
313 new_broken_words
= 0;
315 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
316 a
->dispfrag
= frag_tmp
;
318 #endif /* WORKING_DOT_WORD */
320 #ifdef obj_frob_colon
321 obj_frob_colon (sym_name
);
324 if ((symbolP
= symbol_find (sym_name
)) != 0)
326 S_CLEAR_WEAKREFR (symbolP
);
327 #ifdef RESOLVE_SYMBOL_REDEFINITION
328 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
331 /* Now check for undefined symbols. */
332 if (LOCAL_SYMBOL_CHECK (symbolP
))
334 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
336 if (locsym
->lsy_section
!= undefined_section
337 && (local_symbol_get_frag (locsym
) != frag_now
338 || locsym
->lsy_section
!= now_seg
339 || locsym
->lsy_value
!= frag_now_fix ()))
341 as_bad (_("symbol `%s' is already defined"), sym_name
);
345 locsym
->lsy_section
= now_seg
;
346 local_symbol_set_frag (locsym
, frag_now
);
347 locsym
->lsy_value
= frag_now_fix ();
349 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
350 || S_IS_COMMON (symbolP
)
351 || S_IS_VOLATILE (symbolP
))
353 if (S_IS_VOLATILE (symbolP
))
355 symbolP
= symbol_clone (symbolP
, 1);
356 S_SET_VALUE (symbolP
, 0);
357 S_CLEAR_VOLATILE (symbolP
);
359 if (S_GET_VALUE (symbolP
) == 0)
361 define_sym_at_dot (symbolP
);
364 #endif /* if we have one, it better be zero. */
369 /* There are still several cases to check:
371 A .comm/.lcomm symbol being redefined as initialized
374 A .comm/.lcomm symbol being redefined with a larger
377 This only used to be allowed on VMS gas, but Sun cc
378 on the sparc also depends on it. */
380 if (((!S_IS_DEBUG (symbolP
)
381 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
382 && S_IS_EXTERNAL (symbolP
))
383 || S_GET_SEGMENT (symbolP
) == bss_section
)
384 && (now_seg
== data_section
385 || now_seg
== bss_section
386 || now_seg
== S_GET_SEGMENT (symbolP
)))
388 /* Select which of the 2 cases this is. */
389 if (now_seg
!= data_section
)
391 /* New .comm for prev .comm symbol.
393 If the new size is larger we just change its
394 value. If the new size is smaller, we ignore
396 if (S_GET_VALUE (symbolP
)
397 < ((unsigned) frag_now_fix ()))
399 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
404 /* It is a .comm/.lcomm being converted to initialized
406 define_sym_at_dot (symbolP
);
411 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
412 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
413 static const char *od_buf
= "";
417 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
418 sprintf (od_buf
, "%d.%d.",
419 S_GET_OTHER (symbolP
),
420 S_GET_DESC (symbolP
));
422 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
424 segment_name (S_GET_SEGMENT (symbolP
)),
426 (long) S_GET_VALUE (symbolP
));
428 } /* if the undefined symbol has no value */
432 /* Don't blow up if the definition is the same. */
433 if (!(frag_now
== symbolP
->sy_frag
434 && S_GET_VALUE (symbolP
) == frag_now_fix ()
435 && S_GET_SEGMENT (symbolP
) == now_seg
))
437 as_bad (_("symbol `%s' is already defined"), sym_name
);
438 symbolP
= symbol_clone (symbolP
, 0);
439 define_sym_at_dot (symbolP
);
444 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
446 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
447 (valueT
) frag_now_fix (),
452 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
455 symbol_table_insert (symbolP
);
458 if (mri_common_symbol
!= NULL
)
460 /* This symbol is actually being defined within an MRI common
461 section. This requires special handling. */
462 if (LOCAL_SYMBOL_CHECK (symbolP
))
463 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
464 symbolP
->sy_value
.X_op
= O_symbol
;
465 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
466 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
467 symbolP
->sy_frag
= &zero_address_frag
;
468 S_SET_SEGMENT (symbolP
, expr_section
);
469 symbolP
->sy_flags
.sy_mri_common
= 1;
473 tc_frob_label (symbolP
);
475 #ifdef obj_frob_label
476 obj_frob_label (symbolP
);
482 /* Die if we can't insert the symbol. */
485 symbol_table_insert (symbolS
*symbolP
)
487 register const char *error_string
;
490 know (S_GET_NAME (symbolP
));
492 if (LOCAL_SYMBOL_CHECK (symbolP
))
494 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
496 if (error_string
!= NULL
)
497 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
498 S_GET_NAME (symbolP
), error_string
);
502 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (void *) symbolP
)))
504 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
505 S_GET_NAME (symbolP
), error_string
);
509 /* If a symbol name does not exist, create it as undefined, and insert
510 it into the symbol table. Return a pointer to it. */
513 symbol_find_or_make (const char *name
)
515 register symbolS
*symbolP
;
517 symbolP
= symbol_find (name
);
521 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
523 symbolP
= md_undefined_symbol ((char *) name
);
527 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
533 symbolP
= symbol_make (name
);
535 symbol_table_insert (symbolP
);
536 } /* if symbol wasn't found */
542 symbol_make (const char *name
)
546 /* Let the machine description default it, e.g. for register names. */
547 symbolP
= md_undefined_symbol ((char *) name
);
550 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
556 symbol_clone (symbolS
*orgsymP
, int replace
)
559 asymbol
*bsymorg
, *bsymnew
;
561 /* Make sure we never clone the dot special symbol. */
562 gas_assert (orgsymP
!= &dot_symbol
);
564 /* Running local_symbol_convert on a clone that's not the one currently
565 in local_hash would incorrectly replace the hash entry. Thus the
566 symbol must be converted here. Note that the rest of the function
567 depends on not encountering an unconverted symbol. */
568 if (LOCAL_SYMBOL_CHECK (orgsymP
))
569 orgsymP
= local_symbol_convert ((struct local_symbol
*) orgsymP
);
570 bsymorg
= orgsymP
->bsym
;
572 newsymP
= (symbolS
*) obstack_alloc (¬es
, sizeof (*newsymP
));
574 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
576 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
577 newsymP
->bsym
= bsymnew
;
578 bsymnew
->name
= bsymorg
->name
;
579 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
580 bsymnew
->section
= bsymorg
->section
;
581 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
582 bfd_asymbol_bfd (bsymnew
), bsymnew
);
584 #ifdef obj_symbol_clone_hook
585 obj_symbol_clone_hook (newsymP
, orgsymP
);
588 #ifdef tc_symbol_clone_hook
589 tc_symbol_clone_hook (newsymP
, orgsymP
);
594 if (symbol_rootP
== orgsymP
)
595 symbol_rootP
= newsymP
;
596 else if (orgsymP
->sy_previous
)
598 orgsymP
->sy_previous
->sy_next
= newsymP
;
599 orgsymP
->sy_previous
= NULL
;
601 if (symbol_lastP
== orgsymP
)
602 symbol_lastP
= newsymP
;
603 else if (orgsymP
->sy_next
)
604 orgsymP
->sy_next
->sy_previous
= newsymP
;
606 /* Symbols that won't be output can't be external. */
607 S_CLEAR_EXTERNAL (orgsymP
);
608 orgsymP
->sy_previous
= orgsymP
->sy_next
= orgsymP
;
609 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
611 symbol_table_insert (newsymP
);
615 /* Symbols that won't be output can't be external. */
616 S_CLEAR_EXTERNAL (newsymP
);
617 newsymP
->sy_previous
= newsymP
->sy_next
= newsymP
;
623 /* Referenced symbols, if they are forward references, need to be cloned
624 (without replacing the original) so that the value of the referenced
625 symbols at the point of use . */
627 #undef symbol_clone_if_forward_ref
629 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
631 if (symbolP
&& !LOCAL_SYMBOL_CHECK (symbolP
))
633 symbolS
*add_symbol
= symbolP
->sy_value
.X_add_symbol
;
634 symbolS
*op_symbol
= symbolP
->sy_value
.X_op_symbol
;
636 if (symbolP
->sy_flags
.sy_forward_ref
)
641 /* assign_symbol() clones volatile symbols; pre-existing expressions
642 hold references to the original instance, but want the current
643 value. Just repeat the lookup. */
644 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
645 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
646 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
647 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
650 /* Re-using sy_resolving here, as this routine cannot get called from
651 symbol resolution code. */
652 if ((symbolP
->bsym
->section
== expr_section
653 || symbolP
->sy_flags
.sy_forward_ref
)
654 && !symbolP
->sy_flags
.sy_resolving
)
656 symbolP
->sy_flags
.sy_resolving
= 1;
657 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
658 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
659 symbolP
->sy_flags
.sy_resolving
= 0;
662 if (symbolP
->sy_flags
.sy_forward_ref
663 || add_symbol
!= symbolP
->sy_value
.X_add_symbol
664 || op_symbol
!= symbolP
->sy_value
.X_op_symbol
)
666 if (symbolP
!= &dot_symbol
)
668 symbolP
= symbol_clone (symbolP
, 0);
669 symbolP
->sy_flags
.sy_resolving
= 0;
673 symbolP
= symbol_temp_new_now ();
674 #ifdef tc_new_dot_label
675 tc_new_dot_label (symbolP
);
680 symbolP
->sy_value
.X_add_symbol
= add_symbol
;
681 symbolP
->sy_value
.X_op_symbol
= op_symbol
;
688 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
690 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
694 symbol_temp_new_now (void)
696 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
700 symbol_temp_make (void)
702 return symbol_make (FAKE_LABEL_NAME
);
705 /* Implement symbol table lookup.
706 In: A symbol's name as a string: '\0' can't be part of a symbol name.
707 Out: NULL if the name was not in the symbol table, else the address
708 of a struct symbol associated with that name. */
711 symbol_find_exact (const char *name
)
713 return symbol_find_exact_noref (name
, 0);
717 symbol_find_exact_noref (const char *name
, int noref
)
719 struct local_symbol
*locsym
;
722 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
724 return (symbolS
*) locsym
;
726 sym
= ((symbolS
*) hash_find (sy_hash
, name
));
728 /* Any references to the symbol, except for the reference in
729 .weakref, must clear this flag, such that the symbol does not
730 turn into a weak symbol. Note that we don't have to handle the
731 local_symbol case, since a weakrefd is always promoted out of the
732 local_symbol table when it is turned into a weak symbol. */
734 S_CLEAR_WEAKREFD (sym
);
740 symbol_find (const char *name
)
742 return symbol_find_noref (name
, 0);
746 symbol_find_noref (const char *name
, int noref
)
748 #ifdef tc_canonicalize_symbol_name
751 size_t len
= strlen (name
) + 1;
753 copy
= (char *) alloca (len
);
754 memcpy (copy
, name
, len
);
755 name
= tc_canonicalize_symbol_name (copy
);
759 if (! symbols_case_sensitive
)
766 name
= copy
= (char *) alloca (strlen (name
) + 1);
768 while ((c
= *orig
++) != '\0')
770 *copy
++ = TOUPPER (c
);
775 return symbol_find_exact_noref (name
, noref
);
778 /* Once upon a time, symbols were kept in a singly linked list. At
779 least coff needs to be able to rearrange them from time to time, for
780 which a doubly linked list is much more convenient. Loic did these
781 as macros which seemed dangerous to me so they're now functions.
784 /* Link symbol ADDME after symbol TARGET in the chain. */
787 symbol_append (symbolS
*addme
, symbolS
*target
,
788 symbolS
**rootPP
, symbolS
**lastPP
)
790 if (LOCAL_SYMBOL_CHECK (addme
))
792 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
797 know (*rootPP
== NULL
);
798 know (*lastPP
== NULL
);
799 addme
->sy_next
= NULL
;
800 addme
->sy_previous
= NULL
;
804 } /* if the list is empty */
806 if (target
->sy_next
!= NULL
)
808 target
->sy_next
->sy_previous
= addme
;
812 know (*lastPP
== target
);
814 } /* if we have a next */
816 addme
->sy_next
= target
->sy_next
;
817 target
->sy_next
= addme
;
818 addme
->sy_previous
= target
;
820 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
823 /* Set the chain pointers of SYMBOL to null. */
826 symbol_clear_list_pointers (symbolS
*symbolP
)
828 if (LOCAL_SYMBOL_CHECK (symbolP
))
830 symbolP
->sy_next
= NULL
;
831 symbolP
->sy_previous
= NULL
;
834 /* Remove SYMBOLP from the list. */
837 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
839 if (LOCAL_SYMBOL_CHECK (symbolP
))
842 if (symbolP
== *rootPP
)
844 *rootPP
= symbolP
->sy_next
;
845 } /* if it was the root */
847 if (symbolP
== *lastPP
)
849 *lastPP
= symbolP
->sy_previous
;
850 } /* if it was the tail */
852 if (symbolP
->sy_next
!= NULL
)
854 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
857 if (symbolP
->sy_previous
!= NULL
)
859 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
862 debug_verify_symchain (*rootPP
, *lastPP
);
865 /* Link symbol ADDME before symbol TARGET in the chain. */
868 symbol_insert (symbolS
*addme
, symbolS
*target
,
869 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
871 if (LOCAL_SYMBOL_CHECK (addme
))
873 if (LOCAL_SYMBOL_CHECK (target
))
876 if (target
->sy_previous
!= NULL
)
878 target
->sy_previous
->sy_next
= addme
;
882 know (*rootPP
== target
);
886 addme
->sy_previous
= target
->sy_previous
;
887 target
->sy_previous
= addme
;
888 addme
->sy_next
= target
;
890 debug_verify_symchain (*rootPP
, *lastPP
);
894 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
896 symbolS
*symbolP
= rootP
;
901 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
903 gas_assert (symbolP
->bsym
!= NULL
);
904 gas_assert (symbolP
->sy_flags
.sy_local_symbol
== 0);
905 gas_assert (symbolP
->sy_next
->sy_previous
== symbolP
);
908 gas_assert (lastP
== symbolP
);
911 #ifdef OBJ_COMPLEX_RELC
914 use_complex_relocs_for (symbolS
* symp
)
916 switch (symp
->sy_value
.X_op
)
926 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
927 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
929 (S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
930 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
))
939 case O_bit_inclusive_or
:
941 case O_bit_exclusive_or
:
954 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
955 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
957 (S_IS_COMMON (symp
->sy_value
.X_op_symbol
)
958 || S_IS_LOCAL (symp
->sy_value
.X_op_symbol
))
960 && S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
961 && S_IS_DEFINED (symp
->sy_value
.X_op_symbol
)
962 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
963 && S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) != expr_section
)
975 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
979 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
980 segT seg_right
= S_GET_SEGMENT (right
);
989 case O_uminus
: opname
= "-"; break;
990 case O_bit_not
: opname
= "~"; break;
991 case O_logical_not
: opname
= "!"; break;
992 case O_multiply
: opname
= "*"; break;
993 case O_divide
: opname
= "/"; break;
994 case O_modulus
: opname
= "%"; break;
995 case O_left_shift
: opname
= "<<"; break;
996 case O_right_shift
: opname
= ">>"; break;
997 case O_bit_inclusive_or
: opname
= "|"; break;
998 case O_bit_or_not
: opname
= "|~"; break;
999 case O_bit_exclusive_or
: opname
= "^"; break;
1000 case O_bit_and
: opname
= "&"; break;
1001 case O_add
: opname
= "+"; break;
1002 case O_subtract
: opname
= "-"; break;
1003 case O_eq
: opname
= "=="; break;
1004 case O_ne
: opname
= "!="; break;
1005 case O_lt
: opname
= "<"; break;
1006 case O_le
: opname
= "<="; break;
1007 case O_ge
: opname
= ">="; break;
1008 case O_gt
: opname
= ">"; break;
1009 case O_logical_and
: opname
= "&&"; break;
1010 case O_logical_or
: opname
= "||"; break;
1013 if (expr_symbol_where (symp
, &file
, &line
))
1016 as_bad_where (file
, line
,
1017 _("invalid operands (%s and %s sections) for `%s'"),
1018 seg_left
->name
, seg_right
->name
, opname
);
1020 as_bad_where (file
, line
,
1021 _("invalid operand (%s section) for `%s'"),
1022 seg_right
->name
, opname
);
1026 const char *sname
= S_GET_NAME (symp
);
1029 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1030 seg_left
->name
, seg_right
->name
, opname
, sname
);
1032 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1033 seg_right
->name
, opname
, sname
);
1037 /* Resolve the value of a symbol. This is called during the final
1038 pass over the symbol table to resolve any symbols with complex
1042 resolve_symbol_value (symbolS
*symp
)
1045 valueT final_val
= 0;
1048 if (LOCAL_SYMBOL_CHECK (symp
))
1050 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1052 final_val
= locsym
->lsy_value
;
1053 if (local_symbol_resolved_p (locsym
))
1056 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
1060 locsym
->lsy_value
= final_val
;
1061 local_symbol_mark_resolved (locsym
);
1067 if (symp
->sy_flags
.sy_resolved
)
1069 if (symp
->sy_value
.X_op
== O_constant
)
1070 return (valueT
) symp
->sy_value
.X_add_number
;
1076 final_seg
= S_GET_SEGMENT (symp
);
1078 if (symp
->sy_flags
.sy_resolving
)
1081 as_bad (_("symbol definition loop encountered at `%s'"),
1086 #ifdef OBJ_COMPLEX_RELC
1087 else if (final_seg
== expr_section
1088 && use_complex_relocs_for (symp
))
1090 symbolS
* relc_symbol
= NULL
;
1091 char * relc_symbol_name
= NULL
;
1093 relc_symbol_name
= symbol_relc_make_expr (& symp
->sy_value
);
1095 /* For debugging, print out conversion input & output. */
1097 print_expr (& symp
->sy_value
);
1098 if (relc_symbol_name
)
1099 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1102 if (relc_symbol_name
!= NULL
)
1103 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1104 0, & zero_address_frag
);
1106 if (relc_symbol
== NULL
)
1108 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1114 symbol_table_insert (relc_symbol
);
1116 /* S_CLEAR_EXTERNAL (relc_symbol); */
1117 if (symp
->bsym
->flags
& BSF_SRELC
)
1118 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1120 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1121 /* symp->bsym->flags |= BSF_RELC; */
1122 copy_symbol_attributes (symp
, relc_symbol
);
1123 symp
->sy_value
.X_op
= O_symbol
;
1124 symp
->sy_value
.X_add_symbol
= relc_symbol
;
1125 symp
->sy_value
.X_add_number
= 0;
1129 final_seg
= undefined_section
;
1130 goto exit_dont_set_value
;
1135 symbolS
*add_symbol
, *op_symbol
;
1136 offsetT left
, right
;
1137 segT seg_left
, seg_right
;
1141 symp
->sy_flags
.sy_resolving
= 1;
1143 /* Help out with CSE. */
1144 add_symbol
= symp
->sy_value
.X_add_symbol
;
1145 op_symbol
= symp
->sy_value
.X_op_symbol
;
1146 final_val
= symp
->sy_value
.X_add_number
;
1147 op
= symp
->sy_value
.X_op
;
1160 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
1161 if (final_seg
== expr_section
)
1162 final_seg
= absolute_section
;
1171 left
= resolve_symbol_value (add_symbol
);
1172 seg_left
= S_GET_SEGMENT (add_symbol
);
1174 symp
->sy_value
.X_op_symbol
= NULL
;
1177 if (S_IS_WEAKREFR (symp
))
1179 gas_assert (final_val
== 0);
1180 if (S_IS_WEAKREFR (add_symbol
))
1182 gas_assert (add_symbol
->sy_value
.X_op
== O_symbol
1183 && add_symbol
->sy_value
.X_add_number
== 0);
1184 add_symbol
= add_symbol
->sy_value
.X_add_symbol
;
1185 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1186 symp
->sy_value
.X_add_symbol
= add_symbol
;
1190 if (symp
->sy_flags
.sy_mri_common
)
1192 /* This is a symbol inside an MRI common section. The
1193 relocation routines are going to handle it specially.
1194 Don't change the value. */
1195 resolved
= symbol_resolved_p (add_symbol
);
1199 if (finalize_syms
&& final_val
== 0)
1201 if (LOCAL_SYMBOL_CHECK (add_symbol
))
1202 add_symbol
= local_symbol_convert ((struct local_symbol
*)
1204 copy_symbol_attributes (symp
, add_symbol
);
1207 /* If we have equated this symbol to an undefined or common
1208 symbol, keep X_op set to O_symbol, and don't change
1209 X_add_number. This permits the routine which writes out
1210 relocation to detect this case, and convert the
1211 relocation to be against the symbol to which this symbol
1213 if (! S_IS_DEFINED (add_symbol
)
1214 #if defined (OBJ_COFF) && defined (TE_PE)
1215 || S_IS_WEAK (add_symbol
)
1217 || S_IS_COMMON (add_symbol
))
1221 symp
->sy_value
.X_op
= O_symbol
;
1222 symp
->sy_value
.X_add_symbol
= add_symbol
;
1223 symp
->sy_value
.X_add_number
= final_val
;
1224 /* Use X_op_symbol as a flag. */
1225 symp
->sy_value
.X_op_symbol
= add_symbol
;
1227 final_seg
= seg_left
;
1229 resolved
= symbol_resolved_p (add_symbol
);
1230 symp
->sy_flags
.sy_resolving
= 0;
1231 goto exit_dont_set_value
;
1233 else if (finalize_syms
1234 && ((final_seg
== expr_section
&& seg_left
!= expr_section
)
1235 || symbol_shadow_p (symp
)))
1237 /* If the symbol is an expression symbol, do similarly
1238 as for undefined and common syms above. Handles
1239 "sym +/- expr" where "expr" cannot be evaluated
1240 immediately, and we want relocations to be against
1241 "sym", eg. because it is weak. */
1242 symp
->sy_value
.X_op
= O_symbol
;
1243 symp
->sy_value
.X_add_symbol
= add_symbol
;
1244 symp
->sy_value
.X_add_number
= final_val
;
1245 symp
->sy_value
.X_op_symbol
= add_symbol
;
1246 final_seg
= seg_left
;
1247 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1248 resolved
= symbol_resolved_p (add_symbol
);
1249 symp
->sy_flags
.sy_resolving
= 0;
1250 goto exit_dont_set_value
;
1254 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1255 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1256 final_seg
= seg_left
;
1259 resolved
= symbol_resolved_p (add_symbol
);
1260 if (S_IS_WEAKREFR (symp
))
1261 goto exit_dont_set_value
;
1267 left
= resolve_symbol_value (add_symbol
);
1268 seg_left
= S_GET_SEGMENT (add_symbol
);
1270 /* By reducing these to the relevant dyadic operator, we get
1271 !S -> S == 0 permitted on anything,
1272 -S -> 0 - S only permitted on absolute
1273 ~S -> S ^ ~0 only permitted on absolute */
1274 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1276 report_op_error (symp
, NULL
, op
, add_symbol
);
1278 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1279 final_seg
= absolute_section
;
1283 else if (op
== O_logical_not
)
1288 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1290 resolved
= symbol_resolved_p (add_symbol
);
1298 case O_bit_inclusive_or
:
1300 case O_bit_exclusive_or
:
1312 left
= resolve_symbol_value (add_symbol
);
1313 right
= resolve_symbol_value (op_symbol
);
1314 seg_left
= S_GET_SEGMENT (add_symbol
);
1315 seg_right
= S_GET_SEGMENT (op_symbol
);
1317 /* Simplify addition or subtraction of a constant by folding the
1318 constant into X_add_number. */
1321 if (seg_right
== absolute_section
)
1326 else if (seg_left
== absolute_section
)
1329 add_symbol
= op_symbol
;
1331 seg_left
= seg_right
;
1335 else if (op
== O_subtract
)
1337 if (seg_right
== absolute_section
)
1345 /* Equality and non-equality tests are permitted on anything.
1346 Subtraction, and other comparison operators are permitted if
1347 both operands are in the same section. Otherwise, both
1348 operands must be absolute. We already handled the case of
1349 addition or subtraction of a constant above. This will
1350 probably need to be changed for an object file format which
1351 supports arbitrary expressions, such as IEEE-695. */
1352 if (!(seg_left
== absolute_section
1353 && seg_right
== absolute_section
)
1354 && !(op
== O_eq
|| op
== O_ne
)
1355 && !((op
== O_subtract
1356 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1357 && seg_left
== seg_right
1358 && (seg_left
!= undefined_section
1359 || add_symbol
== op_symbol
)))
1361 /* Don't emit messages unless we're finalizing the symbol value,
1362 otherwise we may get the same message multiple times. */
1364 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1365 /* However do not move the symbol into the absolute section
1366 if it cannot currently be resolved - this would confuse
1367 other parts of the assembler into believing that the
1368 expression had been evaluated to zero. */
1374 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1375 final_seg
= absolute_section
;
1377 /* Check for division by zero. */
1378 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1380 /* If seg_right is not absolute_section, then we've
1381 already issued a warning about using a bad symbol. */
1382 if (seg_right
== absolute_section
&& finalize_syms
)
1387 if (expr_symbol_where (symp
, &file
, &line
))
1388 as_bad_where (file
, line
, _("division by zero"));
1390 as_bad (_("division by zero when setting `%s'"),
1397 switch (symp
->sy_value
.X_op
)
1399 case O_multiply
: left
*= right
; break;
1400 case O_divide
: left
/= right
; break;
1401 case O_modulus
: left
%= right
; break;
1402 case O_left_shift
: left
<<= right
; break;
1403 case O_right_shift
: left
>>= right
; break;
1404 case O_bit_inclusive_or
: left
|= right
; break;
1405 case O_bit_or_not
: left
|= ~right
; break;
1406 case O_bit_exclusive_or
: left
^= right
; break;
1407 case O_bit_and
: left
&= right
; break;
1408 case O_add
: left
+= right
; break;
1409 case O_subtract
: left
-= right
; break;
1412 left
= (left
== right
&& seg_left
== seg_right
1413 && (seg_left
!= undefined_section
1414 || add_symbol
== op_symbol
)
1415 ? ~ (offsetT
) 0 : 0);
1416 if (symp
->sy_value
.X_op
== O_ne
)
1419 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1420 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1421 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1422 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1423 case O_logical_and
: left
= left
&& right
; break;
1424 case O_logical_or
: left
= left
|| right
; break;
1428 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1429 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1431 if (seg_left
== undefined_section
1432 || seg_right
== undefined_section
)
1433 final_seg
= undefined_section
;
1434 else if (seg_left
== absolute_section
)
1435 final_seg
= seg_right
;
1437 final_seg
= seg_left
;
1439 resolved
= (symbol_resolved_p (add_symbol
)
1440 && symbol_resolved_p (op_symbol
));
1445 /* Give an error (below) if not in expr_section. We don't
1446 want to worry about expr_section symbols, because they
1447 are fictional (they are created as part of expression
1448 resolution), and any problems may not actually mean
1453 symp
->sy_flags
.sy_resolving
= 0;
1457 S_SET_VALUE (symp
, final_val
);
1459 exit_dont_set_value
:
1460 /* Always set the segment, even if not finalizing the value.
1461 The segment is used to determine whether a symbol is defined. */
1462 S_SET_SEGMENT (symp
, final_seg
);
1464 /* Don't worry if we can't resolve an expr_section symbol. */
1468 symp
->sy_flags
.sy_resolved
= 1;
1469 else if (S_GET_SEGMENT (symp
) != expr_section
)
1471 as_bad (_("can't resolve value for symbol `%s'"),
1473 symp
->sy_flags
.sy_resolved
= 1;
1480 static void resolve_local_symbol (const char *, void *);
1482 /* A static function passed to hash_traverse. */
1485 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, void *value
)
1488 resolve_symbol_value ((symbolS
*) value
);
1491 /* Resolve all local symbols. */
1494 resolve_local_symbol_values (void)
1496 hash_traverse (local_hash
, resolve_local_symbol
);
1499 /* Obtain the current value of a symbol without changing any
1500 sub-expressions used. */
1503 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1505 symbolS
*symbolP
= *symbolPP
;
1507 if (LOCAL_SYMBOL_CHECK (symbolP
))
1509 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1511 *valueP
= locsym
->lsy_value
;
1512 *segP
= locsym
->lsy_section
;
1513 *fragPP
= local_symbol_get_frag (locsym
);
1517 expressionS exp
= symbolP
->sy_value
;
1519 if (!symbolP
->sy_flags
.sy_resolved
&& exp
.X_op
!= O_illegal
)
1523 if (symbolP
->sy_flags
.sy_resolving
)
1525 symbolP
->sy_flags
.sy_resolving
= 1;
1526 resolved
= resolve_expression (&exp
);
1527 symbolP
->sy_flags
.sy_resolving
= 0;
1535 if (!symbol_equated_p (symbolP
))
1540 symbolP
= exp
.X_add_symbol
;
1547 *symbolPP
= symbolP
;
1548 *valueP
= exp
.X_add_number
;
1549 *segP
= symbolP
->bsym
->section
;
1550 *fragPP
= symbolP
->sy_frag
;
1552 if (*segP
== expr_section
)
1555 case O_constant
: *segP
= absolute_section
; break;
1556 case O_register
: *segP
= reg_section
; break;
1564 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1565 They are *really* local. That is, they go out of scope whenever we see a
1566 label that isn't local. Also, like fb labels, there can be multiple
1567 instances of a dollar label. Therefor, we name encode each instance with
1568 the instance number, keep a list of defined symbols separate from the real
1569 symbol table, and we treat these buggers as a sparse array. */
1571 static long *dollar_labels
;
1572 static long *dollar_label_instances
;
1573 static char *dollar_label_defines
;
1574 static unsigned long dollar_label_count
;
1575 static unsigned long dollar_label_max
;
1578 dollar_label_defined (long label
)
1582 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1584 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1586 return dollar_label_defines
[i
- dollar_labels
];
1588 /* If we get here, label isn't defined. */
1593 dollar_label_instance (long label
)
1597 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1599 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1601 return (dollar_label_instances
[i
- dollar_labels
]);
1603 /* If we get here, we haven't seen the label before.
1604 Therefore its instance count is zero. */
1609 dollar_label_clear (void)
1611 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1614 #define DOLLAR_LABEL_BUMP_BY 10
1617 define_dollar_label (long label
)
1621 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1624 ++dollar_label_instances
[i
- dollar_labels
];
1625 dollar_label_defines
[i
- dollar_labels
] = 1;
1629 /* If we get to here, we don't have label listed yet. */
1631 if (dollar_labels
== NULL
)
1633 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1634 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1635 dollar_label_defines
= (char *) xmalloc (DOLLAR_LABEL_BUMP_BY
);
1636 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1637 dollar_label_count
= 0;
1639 else if (dollar_label_count
== dollar_label_max
)
1641 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1642 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
1643 dollar_label_max
* sizeof (long));
1644 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
1645 dollar_label_max
* sizeof (long));
1646 dollar_label_defines
= (char *) xrealloc (dollar_label_defines
, dollar_label_max
);
1647 } /* if we needed to grow */
1649 dollar_labels
[dollar_label_count
] = label
;
1650 dollar_label_instances
[dollar_label_count
] = 1;
1651 dollar_label_defines
[dollar_label_count
] = 1;
1652 ++dollar_label_count
;
1655 /* Caller must copy returned name: we re-use the area for the next name.
1657 The mth occurence of label n: is turned into the symbol "Ln^Am"
1658 where n is the label number and m is the instance number. "L" makes
1659 it a label discarded unless debugging and "^A"('\1') ensures no
1660 ordinary symbol SHOULD get the same name as a local label
1661 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1663 fb labels get the same treatment, except that ^B is used in place
1666 char * /* Return local label name. */
1667 dollar_label_name (register long n
, /* we just saw "n$:" : n a number. */
1668 register int augend
/* 0 for current instance, 1 for new instance. */)
1671 /* Returned to caller, then copied. Used for created names ("4f"). */
1672 static char symbol_name_build
[24];
1675 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1678 know (augend
== 0 || augend
== 1);
1679 p
= symbol_name_build
;
1680 #ifdef LOCAL_LABEL_PREFIX
1681 *p
++ = LOCAL_LABEL_PREFIX
;
1685 /* Next code just does sprintf( {}, "%d", n); */
1687 q
= symbol_name_temporary
;
1688 for (*q
++ = 0, i
= n
; i
; ++q
)
1693 while ((*p
= *--q
) != '\0')
1696 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1698 /* Instance number. */
1699 q
= symbol_name_temporary
;
1700 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1705 while ((*p
++ = *--q
) != '\0');;
1707 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1708 return symbol_name_build
;
1711 /* Somebody else's idea of local labels. They are made by "n:" where n
1712 is any decimal digit. Refer to them with
1713 "nb" for previous (backward) n:
1714 or "nf" for next (forward) n:.
1716 We do a little better and let n be any number, not just a single digit, but
1717 since the other guy's assembler only does ten, we treat the first ten
1720 Like someone else's assembler, we have one set of local label counters for
1721 entire assembly, not one set per (sub)segment like in most assemblers. This
1722 implies that one can refer to a label in another segment, and indeed some
1723 crufty compilers have done just that.
1725 Since there could be a LOT of these things, treat them as a sparse
1728 #define FB_LABEL_SPECIAL (10)
1730 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1731 static long *fb_labels
;
1732 static long *fb_label_instances
;
1733 static long fb_label_count
;
1734 static long fb_label_max
;
1736 /* This must be more than FB_LABEL_SPECIAL. */
1737 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1740 fb_label_init (void)
1742 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1745 /* Add one to the instance number of this fb label. */
1748 fb_label_instance_inc (long label
)
1752 if (label
< FB_LABEL_SPECIAL
)
1754 ++fb_low_counter
[label
];
1758 if (fb_labels
!= NULL
)
1760 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1761 i
< fb_labels
+ fb_label_count
; ++i
)
1765 ++fb_label_instances
[i
- fb_labels
];
1767 } /* if we find it */
1768 } /* for each existing label */
1771 /* If we get to here, we don't have label listed yet. */
1773 if (fb_labels
== NULL
)
1775 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1776 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1777 fb_label_max
= FB_LABEL_BUMP_BY
;
1778 fb_label_count
= FB_LABEL_SPECIAL
;
1781 else if (fb_label_count
== fb_label_max
)
1783 fb_label_max
+= FB_LABEL_BUMP_BY
;
1784 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1785 fb_label_max
* sizeof (long));
1786 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1787 fb_label_max
* sizeof (long));
1788 } /* if we needed to grow */
1790 fb_labels
[fb_label_count
] = label
;
1791 fb_label_instances
[fb_label_count
] = 1;
1796 fb_label_instance (long label
)
1800 if (label
< FB_LABEL_SPECIAL
)
1802 return (fb_low_counter
[label
]);
1805 if (fb_labels
!= NULL
)
1807 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1808 i
< fb_labels
+ fb_label_count
; ++i
)
1812 return (fb_label_instances
[i
- fb_labels
]);
1813 } /* if we find it */
1814 } /* for each existing label */
1817 /* We didn't find the label, so this must be a reference to the
1822 /* Caller must copy returned name: we re-use the area for the next name.
1824 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1825 where n is the label number and m is the instance number. "L" makes
1826 it a label discarded unless debugging and "^B"('\2') ensures no
1827 ordinary symbol SHOULD get the same name as a local label
1828 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1830 dollar labels get the same treatment, except that ^A is used in
1833 char * /* Return local label name. */
1834 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1835 long augend
/* 0 for nb, 1 for n:, nf. */)
1838 /* Returned to caller, then copied. Used for created names ("4f"). */
1839 static char symbol_name_build
[24];
1842 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1846 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1848 know ((unsigned long) augend
<= 1);
1850 p
= symbol_name_build
;
1851 #ifdef LOCAL_LABEL_PREFIX
1852 *p
++ = LOCAL_LABEL_PREFIX
;
1856 /* Next code just does sprintf( {}, "%d", n); */
1858 q
= symbol_name_temporary
;
1859 for (*q
++ = 0, i
= n
; i
; ++q
)
1864 while ((*p
= *--q
) != '\0')
1867 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1869 /* Instance number. */
1870 q
= symbol_name_temporary
;
1871 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1876 while ((*p
++ = *--q
) != '\0');;
1878 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1879 return (symbol_name_build
);
1882 /* Decode name that may have been generated by foo_label_name() above.
1883 If the name wasn't generated by foo_label_name(), then return it
1884 unaltered. This is used for error messages. */
1887 decode_local_label_name (char *s
)
1890 char *symbol_decode
;
1892 int instance_number
;
1894 const char *message_format
;
1897 #ifdef LOCAL_LABEL_PREFIX
1898 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
1902 if (s
[lindex
] != 'L')
1905 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
1906 label_number
= (10 * label_number
) + *p
- '0';
1908 if (*p
== DOLLAR_LABEL_CHAR
)
1910 else if (*p
== LOCAL_LABEL_CHAR
)
1915 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1916 instance_number
= (10 * instance_number
) + *p
- '0';
1918 message_format
= _("\"%d\" (instance number %d of a %s label)");
1919 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
1920 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1922 return symbol_decode
;
1925 /* Get the value of a symbol. */
1928 S_GET_VALUE (symbolS
*s
)
1930 if (LOCAL_SYMBOL_CHECK (s
))
1931 return resolve_symbol_value (s
);
1933 if (!s
->sy_flags
.sy_resolved
)
1935 valueT val
= resolve_symbol_value (s
);
1939 if (S_IS_WEAKREFR (s
))
1940 return S_GET_VALUE (s
->sy_value
.X_add_symbol
);
1942 if (s
->sy_value
.X_op
!= O_constant
)
1944 if (! s
->sy_flags
.sy_resolved
1945 || s
->sy_value
.X_op
!= O_symbol
1946 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1947 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1950 return (valueT
) s
->sy_value
.X_add_number
;
1953 /* Set the value of a symbol. */
1956 S_SET_VALUE (symbolS
*s
, valueT val
)
1958 if (LOCAL_SYMBOL_CHECK (s
))
1960 ((struct local_symbol
*) s
)->lsy_value
= val
;
1964 s
->sy_value
.X_op
= O_constant
;
1965 s
->sy_value
.X_add_number
= (offsetT
) val
;
1966 s
->sy_value
.X_unsigned
= 0;
1967 S_CLEAR_WEAKREFR (s
);
1971 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1973 if (LOCAL_SYMBOL_CHECK (dest
))
1974 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
1975 if (LOCAL_SYMBOL_CHECK (src
))
1976 src
= local_symbol_convert ((struct local_symbol
*) src
);
1978 /* In an expression, transfer the settings of these flags.
1979 The user can override later, of course. */
1980 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1981 | BSF_GNU_INDIRECT_FUNCTION)
1982 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1984 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1985 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1988 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1989 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1994 S_IS_FUNCTION (symbolS
*s
)
1998 if (LOCAL_SYMBOL_CHECK (s
))
2001 flags
= s
->bsym
->flags
;
2003 return (flags
& BSF_FUNCTION
) != 0;
2007 S_IS_EXTERNAL (symbolS
*s
)
2011 if (LOCAL_SYMBOL_CHECK (s
))
2014 flags
= s
->bsym
->flags
;
2017 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2020 return (flags
& BSF_GLOBAL
) != 0;
2024 S_IS_WEAK (symbolS
*s
)
2026 if (LOCAL_SYMBOL_CHECK (s
))
2028 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2029 could probably handle a WEAKREFR as always weak though. E.g., if
2030 the referenced symbol has lost its weak status, there's no reason
2031 to keep handling the weakrefr as if it was weak. */
2032 if (S_IS_WEAKREFR (s
))
2033 return S_IS_WEAK (s
->sy_value
.X_add_symbol
);
2034 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2038 S_IS_WEAKREFR (symbolS
*s
)
2040 if (LOCAL_SYMBOL_CHECK (s
))
2042 return s
->sy_flags
.sy_weakrefr
!= 0;
2046 S_IS_WEAKREFD (symbolS
*s
)
2048 if (LOCAL_SYMBOL_CHECK (s
))
2050 return s
->sy_flags
.sy_weakrefd
!= 0;
2054 S_IS_COMMON (symbolS
*s
)
2056 if (LOCAL_SYMBOL_CHECK (s
))
2058 return bfd_is_com_section (s
->bsym
->section
);
2062 S_IS_DEFINED (symbolS
*s
)
2064 if (LOCAL_SYMBOL_CHECK (s
))
2065 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
2066 return s
->bsym
->section
!= undefined_section
;
2070 #ifndef EXTERN_FORCE_RELOC
2071 #define EXTERN_FORCE_RELOC IS_ELF
2074 /* Return true for symbols that should not be reduced to section
2075 symbols or eliminated from expressions, because they may be
2076 overridden by the linker. */
2078 S_FORCE_RELOC (symbolS
*s
, int strict
)
2080 if (LOCAL_SYMBOL_CHECK (s
))
2081 return ((struct local_symbol
*) s
)->lsy_section
== undefined_section
;
2084 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2085 || (EXTERN_FORCE_RELOC
2086 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2087 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0
2088 || s
->bsym
->section
== undefined_section
2089 || bfd_is_com_section (s
->bsym
->section
));
2093 S_IS_DEBUG (symbolS
*s
)
2095 if (LOCAL_SYMBOL_CHECK (s
))
2097 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2103 S_IS_LOCAL (symbolS
*s
)
2108 if (LOCAL_SYMBOL_CHECK (s
))
2111 flags
= s
->bsym
->flags
;
2114 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2117 if (bfd_get_section (s
->bsym
) == reg_section
)
2120 if (flag_strip_local_absolute
2121 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2122 the source file even when the object file is stripped. */
2123 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2124 && bfd_get_section (s
->bsym
) == absolute_section
)
2127 name
= S_GET_NAME (s
);
2128 return (name
!= NULL
2130 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2131 || strchr (name
, LOCAL_LABEL_CHAR
)
2132 || TC_LABEL_IS_LOCAL (name
)
2133 || (! flag_keep_locals
2134 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2137 && name
[1] == '?')))));
2141 S_IS_STABD (symbolS
*s
)
2143 return S_GET_NAME (s
) == 0;
2147 S_CAN_BE_REDEFINED (const symbolS
*s
)
2149 if (LOCAL_SYMBOL_CHECK (s
))
2150 return (local_symbol_get_frag ((struct local_symbol
*) s
)
2151 == &predefined_address_frag
);
2152 /* Permit register names to be redefined. */
2153 return s
->bsym
->section
== reg_section
;
2157 S_IS_VOLATILE (const symbolS
*s
)
2159 if (LOCAL_SYMBOL_CHECK (s
))
2161 return s
->sy_flags
.sy_volatile
;
2165 S_IS_FORWARD_REF (const symbolS
*s
)
2167 if (LOCAL_SYMBOL_CHECK (s
))
2169 return s
->sy_flags
.sy_forward_ref
;
2173 S_GET_NAME (symbolS
*s
)
2175 if (LOCAL_SYMBOL_CHECK (s
))
2176 return ((struct local_symbol
*) s
)->lsy_name
;
2177 return s
->bsym
->name
;
2181 S_GET_SEGMENT (symbolS
*s
)
2183 if (LOCAL_SYMBOL_CHECK (s
))
2184 return ((struct local_symbol
*) s
)->lsy_section
;
2185 return s
->bsym
->section
;
2189 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2191 /* Don't reassign section symbols. The direct reason is to prevent seg
2192 faults assigning back to const global symbols such as *ABS*, but it
2193 shouldn't happen anyway. */
2195 if (LOCAL_SYMBOL_CHECK (s
))
2197 if (seg
== reg_section
)
2198 s
= local_symbol_convert ((struct local_symbol
*) s
);
2201 ((struct local_symbol
*) s
)->lsy_section
= seg
;
2206 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2208 if (s
->bsym
->section
!= seg
)
2212 s
->bsym
->section
= seg
;
2216 S_SET_EXTERNAL (symbolS
*s
)
2218 if (LOCAL_SYMBOL_CHECK (s
))
2219 s
= local_symbol_convert ((struct local_symbol
*) s
);
2220 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2222 /* Let .weak override .global. */
2225 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2230 /* Do not reassign section symbols. */
2231 as_where (& file
, & line
);
2232 as_warn_where (file
, line
,
2233 _("section symbols are already global"));
2236 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2237 if (S_GET_SEGMENT (s
) == reg_section
)
2239 as_bad ("can't make register symbol `%s' global",
2244 s
->bsym
->flags
|= BSF_GLOBAL
;
2245 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2248 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2249 an_external_name
= S_GET_NAME (s
);
2254 S_CLEAR_EXTERNAL (symbolS
*s
)
2256 if (LOCAL_SYMBOL_CHECK (s
))
2258 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2260 /* Let .weak override. */
2263 s
->bsym
->flags
|= BSF_LOCAL
;
2264 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2268 S_SET_WEAK (symbolS
*s
)
2270 if (LOCAL_SYMBOL_CHECK (s
))
2271 s
= local_symbol_convert ((struct local_symbol
*) s
);
2272 #ifdef obj_set_weak_hook
2273 obj_set_weak_hook (s
);
2275 s
->bsym
->flags
|= BSF_WEAK
;
2276 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2280 S_SET_WEAKREFR (symbolS
*s
)
2282 if (LOCAL_SYMBOL_CHECK (s
))
2283 s
= local_symbol_convert ((struct local_symbol
*) s
);
2284 s
->sy_flags
.sy_weakrefr
= 1;
2285 /* If the alias was already used, make sure we mark the target as
2286 used as well, otherwise it might be dropped from the symbol
2287 table. This may have unintended side effects if the alias is
2288 later redirected to another symbol, such as keeping the unused
2289 previous target in the symbol table. Since it will be weak, it's
2291 if (s
->sy_flags
.sy_used
)
2292 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2296 S_CLEAR_WEAKREFR (symbolS
*s
)
2298 if (LOCAL_SYMBOL_CHECK (s
))
2300 s
->sy_flags
.sy_weakrefr
= 0;
2304 S_SET_WEAKREFD (symbolS
*s
)
2306 if (LOCAL_SYMBOL_CHECK (s
))
2307 s
= local_symbol_convert ((struct local_symbol
*) s
);
2308 s
->sy_flags
.sy_weakrefd
= 1;
2313 S_CLEAR_WEAKREFD (symbolS
*s
)
2315 if (LOCAL_SYMBOL_CHECK (s
))
2317 if (s
->sy_flags
.sy_weakrefd
)
2319 s
->sy_flags
.sy_weakrefd
= 0;
2320 /* If a weakref target symbol is weak, then it was never
2321 referenced directly before, not even in a .global directive,
2322 so decay it to local. If it remains undefined, it will be
2323 later turned into a global, like any other undefined
2325 if (s
->bsym
->flags
& BSF_WEAK
)
2327 #ifdef obj_clear_weak_hook
2328 obj_clear_weak_hook (s
);
2330 s
->bsym
->flags
&= ~BSF_WEAK
;
2331 s
->bsym
->flags
|= BSF_LOCAL
;
2337 S_SET_THREAD_LOCAL (symbolS
*s
)
2339 if (LOCAL_SYMBOL_CHECK (s
))
2340 s
= local_symbol_convert ((struct local_symbol
*) s
);
2341 if (bfd_is_com_section (s
->bsym
->section
)
2342 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2344 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2345 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2346 as_bad (_("Accessing function `%s' as thread-local object"),
2348 else if (! bfd_is_und_section (s
->bsym
->section
)
2349 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2350 as_bad (_("Accessing `%s' as thread-local object"),
2355 S_SET_NAME (symbolS
*s
, const char *name
)
2357 if (LOCAL_SYMBOL_CHECK (s
))
2359 ((struct local_symbol
*) s
)->lsy_name
= name
;
2362 s
->bsym
->name
= name
;
2366 S_SET_VOLATILE (symbolS
*s
)
2368 if (LOCAL_SYMBOL_CHECK (s
))
2369 s
= local_symbol_convert ((struct local_symbol
*) s
);
2370 s
->sy_flags
.sy_volatile
= 1;
2374 S_CLEAR_VOLATILE (symbolS
*s
)
2376 if (!LOCAL_SYMBOL_CHECK (s
))
2377 s
->sy_flags
.sy_volatile
= 0;
2381 S_SET_FORWARD_REF (symbolS
*s
)
2383 if (LOCAL_SYMBOL_CHECK (s
))
2384 s
= local_symbol_convert ((struct local_symbol
*) s
);
2385 s
->sy_flags
.sy_forward_ref
= 1;
2388 /* Return the previous symbol in a chain. */
2391 symbol_previous (symbolS
*s
)
2393 if (LOCAL_SYMBOL_CHECK (s
))
2395 return s
->sy_previous
;
2398 /* Return the next symbol in a chain. */
2401 symbol_next (symbolS
*s
)
2403 if (LOCAL_SYMBOL_CHECK (s
))
2408 /* Return a pointer to the value of a symbol as an expression. */
2411 symbol_get_value_expression (symbolS
*s
)
2413 if (LOCAL_SYMBOL_CHECK (s
))
2414 s
= local_symbol_convert ((struct local_symbol
*) s
);
2415 return &s
->sy_value
;
2418 /* Set the value of a symbol to an expression. */
2421 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2423 if (LOCAL_SYMBOL_CHECK (s
))
2424 s
= local_symbol_convert ((struct local_symbol
*) s
);
2426 S_CLEAR_WEAKREFR (s
);
2429 /* Return whether 2 symbols are the same. */
2432 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2434 if (s1
->sy_flags
.sy_local_symbol
2435 && local_symbol_converted_p ((struct local_symbol
*) s1
))
2436 s1
= local_symbol_get_real_symbol ((struct local_symbol
*) s1
);
2437 if (s2
->sy_flags
.sy_local_symbol
2438 && local_symbol_converted_p ((struct local_symbol
*) s2
))
2439 s2
= local_symbol_get_real_symbol ((struct local_symbol
*) s2
);
2443 /* Return a pointer to the X_add_number component of a symbol. */
2446 symbol_X_add_number (symbolS
*s
)
2448 if (LOCAL_SYMBOL_CHECK (s
))
2449 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
2451 return &s
->sy_value
.X_add_number
;
2454 /* Set the value of SYM to the current position in the current segment. */
2457 symbol_set_value_now (symbolS
*sym
)
2459 S_SET_SEGMENT (sym
, now_seg
);
2460 S_SET_VALUE (sym
, frag_now_fix ());
2461 symbol_set_frag (sym
, frag_now
);
2464 /* Set the frag of a symbol. */
2467 symbol_set_frag (symbolS
*s
, fragS
*f
)
2469 if (LOCAL_SYMBOL_CHECK (s
))
2471 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2475 S_CLEAR_WEAKREFR (s
);
2478 /* Return the frag of a symbol. */
2481 symbol_get_frag (symbolS
*s
)
2483 if (LOCAL_SYMBOL_CHECK (s
))
2484 return local_symbol_get_frag ((struct local_symbol
*) s
);
2488 /* Mark a symbol as having been used. */
2491 symbol_mark_used (symbolS
*s
)
2493 if (LOCAL_SYMBOL_CHECK (s
))
2495 s
->sy_flags
.sy_used
= 1;
2496 if (S_IS_WEAKREFR (s
))
2497 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2500 /* Clear the mark of whether a symbol has been used. */
2503 symbol_clear_used (symbolS
*s
)
2505 if (LOCAL_SYMBOL_CHECK (s
))
2506 s
= local_symbol_convert ((struct local_symbol
*) s
);
2507 s
->sy_flags
.sy_used
= 0;
2510 /* Return whether a symbol has been used. */
2513 symbol_used_p (symbolS
*s
)
2515 if (LOCAL_SYMBOL_CHECK (s
))
2517 return s
->sy_flags
.sy_used
;
2520 /* Mark a symbol as having been used in a reloc. */
2523 symbol_mark_used_in_reloc (symbolS
*s
)
2525 if (LOCAL_SYMBOL_CHECK (s
))
2526 s
= local_symbol_convert ((struct local_symbol
*) s
);
2527 s
->sy_flags
.sy_used_in_reloc
= 1;
2530 /* Clear the mark of whether a symbol has been used in a reloc. */
2533 symbol_clear_used_in_reloc (symbolS
*s
)
2535 if (LOCAL_SYMBOL_CHECK (s
))
2537 s
->sy_flags
.sy_used_in_reloc
= 0;
2540 /* Return whether a symbol has been used in a reloc. */
2543 symbol_used_in_reloc_p (symbolS
*s
)
2545 if (LOCAL_SYMBOL_CHECK (s
))
2547 return s
->sy_flags
.sy_used_in_reloc
;
2550 /* Mark a symbol as an MRI common symbol. */
2553 symbol_mark_mri_common (symbolS
*s
)
2555 if (LOCAL_SYMBOL_CHECK (s
))
2556 s
= local_symbol_convert ((struct local_symbol
*) s
);
2557 s
->sy_flags
.sy_mri_common
= 1;
2560 /* Clear the mark of whether a symbol is an MRI common symbol. */
2563 symbol_clear_mri_common (symbolS
*s
)
2565 if (LOCAL_SYMBOL_CHECK (s
))
2567 s
->sy_flags
.sy_mri_common
= 0;
2570 /* Return whether a symbol is an MRI common symbol. */
2573 symbol_mri_common_p (symbolS
*s
)
2575 if (LOCAL_SYMBOL_CHECK (s
))
2577 return s
->sy_flags
.sy_mri_common
;
2580 /* Mark a symbol as having been written. */
2583 symbol_mark_written (symbolS
*s
)
2585 if (LOCAL_SYMBOL_CHECK (s
))
2587 s
->sy_flags
.sy_written
= 1;
2590 /* Clear the mark of whether a symbol has been written. */
2593 symbol_clear_written (symbolS
*s
)
2595 if (LOCAL_SYMBOL_CHECK (s
))
2597 s
->sy_flags
.sy_written
= 0;
2600 /* Return whether a symbol has been written. */
2603 symbol_written_p (symbolS
*s
)
2605 if (LOCAL_SYMBOL_CHECK (s
))
2607 return s
->sy_flags
.sy_written
;
2610 /* Mark a symbol has having been resolved. */
2613 symbol_mark_resolved (symbolS
*s
)
2615 if (LOCAL_SYMBOL_CHECK (s
))
2617 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2620 s
->sy_flags
.sy_resolved
= 1;
2623 /* Return whether a symbol has been resolved. */
2626 symbol_resolved_p (symbolS
*s
)
2628 if (LOCAL_SYMBOL_CHECK (s
))
2629 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2630 return s
->sy_flags
.sy_resolved
;
2633 /* Return whether a symbol is a section symbol. */
2636 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2638 if (LOCAL_SYMBOL_CHECK (s
))
2640 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2643 /* Return whether a symbol is equated to another symbol. */
2646 symbol_equated_p (symbolS
*s
)
2648 if (LOCAL_SYMBOL_CHECK (s
))
2650 return s
->sy_value
.X_op
== O_symbol
;
2653 /* Return whether a symbol is equated to another symbol, and should be
2654 treated specially when writing out relocs. */
2657 symbol_equated_reloc_p (symbolS
*s
)
2659 if (LOCAL_SYMBOL_CHECK (s
))
2661 /* X_op_symbol, normally not used for O_symbol, is set by
2662 resolve_symbol_value to flag expression syms that have been
2664 return (s
->sy_value
.X_op
== O_symbol
2665 #if defined (OBJ_COFF) && defined (TE_PE)
2668 && ((s
->sy_flags
.sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2669 || ! S_IS_DEFINED (s
)
2670 || S_IS_COMMON (s
)));
2673 /* Return whether a symbol has a constant value. */
2676 symbol_constant_p (symbolS
*s
)
2678 if (LOCAL_SYMBOL_CHECK (s
))
2680 return s
->sy_value
.X_op
== O_constant
;
2683 /* Return whether a symbol was cloned and thus removed from the global
2687 symbol_shadow_p (symbolS
*s
)
2689 if (LOCAL_SYMBOL_CHECK (s
))
2691 return s
->sy_next
== s
;
2694 /* Return the BFD symbol for a symbol. */
2697 symbol_get_bfdsym (symbolS
*s
)
2699 if (LOCAL_SYMBOL_CHECK (s
))
2700 s
= local_symbol_convert ((struct local_symbol
*) s
);
2704 /* Set the BFD symbol for a symbol. */
2707 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2709 if (LOCAL_SYMBOL_CHECK (s
))
2710 s
= local_symbol_convert ((struct local_symbol
*) s
);
2711 /* Usually, it is harmless to reset a symbol to a BFD section
2712 symbol. For example, obj_elf_change_section sets the BFD symbol
2713 of an old symbol with the newly created section symbol. But when
2714 we have multiple sections with the same name, the newly created
2715 section may have the same name as an old section. We check if the
2716 old symbol has been already marked as a section symbol before
2718 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2720 /* else XXX - What do we do now ? */
2723 #ifdef OBJ_SYMFIELD_TYPE
2725 /* Get a pointer to the object format information for a symbol. */
2728 symbol_get_obj (symbolS
*s
)
2730 if (LOCAL_SYMBOL_CHECK (s
))
2731 s
= local_symbol_convert ((struct local_symbol
*) s
);
2735 /* Set the object format information for a symbol. */
2738 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2740 if (LOCAL_SYMBOL_CHECK (s
))
2741 s
= local_symbol_convert ((struct local_symbol
*) s
);
2745 #endif /* OBJ_SYMFIELD_TYPE */
2747 #ifdef TC_SYMFIELD_TYPE
2749 /* Get a pointer to the processor information for a symbol. */
2752 symbol_get_tc (symbolS
*s
)
2754 if (LOCAL_SYMBOL_CHECK (s
))
2755 s
= local_symbol_convert ((struct local_symbol
*) s
);
2759 /* Set the processor information for a symbol. */
2762 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2764 if (LOCAL_SYMBOL_CHECK (s
))
2765 s
= local_symbol_convert ((struct local_symbol
*) s
);
2769 #endif /* TC_SYMFIELD_TYPE */
2774 symbol_lastP
= NULL
;
2775 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2776 sy_hash
= hash_new ();
2777 local_hash
= hash_new ();
2779 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2780 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2781 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
2783 abs_symbol
.sy_value
.X_op
= O_constant
;
2784 abs_symbol
.sy_frag
= &zero_address_frag
;
2786 if (LOCAL_LABELS_FB
)
2791 dot_symbol_init (void)
2793 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2794 if (dot_symbol
.bsym
== NULL
)
2795 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2796 dot_symbol
.bsym
->name
= ".";
2797 dot_symbol
.sy_flags
.sy_forward_ref
= 1;
2798 dot_symbol
.sy_value
.X_op
= O_constant
;
2803 /* Maximum indent level.
2804 Available for modification inside a gdb session. */
2805 static int max_indent_level
= 8;
2808 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2810 const char *name
= S_GET_NAME (sym
);
2811 if (!name
|| !name
[0])
2813 fprintf (file
, "sym ");
2814 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
2815 fprintf (file
, " %s", name
);
2817 if (LOCAL_SYMBOL_CHECK (sym
))
2819 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2821 if (local_symbol_get_frag (locsym
) != & zero_address_frag
2822 && local_symbol_get_frag (locsym
) != NULL
)
2824 fprintf (file
, " frag ");
2825 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) local_symbol_get_frag (locsym
)));
2827 if (local_symbol_resolved_p (locsym
))
2828 fprintf (file
, " resolved");
2829 fprintf (file
, " local");
2833 if (sym
->sy_frag
!= &zero_address_frag
)
2835 fprintf (file
, " frag ");
2836 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->sy_frag
));
2838 if (sym
->sy_flags
.sy_written
)
2839 fprintf (file
, " written");
2840 if (sym
->sy_flags
.sy_resolved
)
2841 fprintf (file
, " resolved");
2842 else if (sym
->sy_flags
.sy_resolving
)
2843 fprintf (file
, " resolving");
2844 if (sym
->sy_flags
.sy_used_in_reloc
)
2845 fprintf (file
, " used-in-reloc");
2846 if (sym
->sy_flags
.sy_used
)
2847 fprintf (file
, " used");
2848 if (S_IS_LOCAL (sym
))
2849 fprintf (file
, " local");
2850 if (S_IS_EXTERNAL (sym
))
2851 fprintf (file
, " extern");
2852 if (S_IS_WEAK (sym
))
2853 fprintf (file
, " weak");
2854 if (S_IS_DEBUG (sym
))
2855 fprintf (file
, " debug");
2856 if (S_IS_DEFINED (sym
))
2857 fprintf (file
, " defined");
2859 if (S_IS_WEAKREFR (sym
))
2860 fprintf (file
, " weakrefr");
2861 if (S_IS_WEAKREFD (sym
))
2862 fprintf (file
, " weakrefd");
2863 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2864 if (symbol_resolved_p (sym
))
2866 segT s
= S_GET_SEGMENT (sym
);
2868 if (s
!= undefined_section
2869 && s
!= expr_section
)
2870 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
2872 else if (indent_level
< max_indent_level
2873 && S_GET_SEGMENT (sym
) != undefined_section
)
2876 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2877 if (LOCAL_SYMBOL_CHECK (sym
))
2878 fprintf (file
, "constant %lx",
2879 (unsigned long) ((struct local_symbol
*) sym
)->lsy_value
);
2881 print_expr_1 (file
, &sym
->sy_value
);
2882 fprintf (file
, ">");
2889 print_symbol_value (symbolS
*sym
)
2892 print_symbol_value_1 (stderr
, sym
);
2893 fprintf (stderr
, "\n");
2897 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2900 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2901 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2902 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2903 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2904 fprintf (file
, ">");
2909 print_expr_1 (FILE *file
, expressionS
*exp
)
2911 fprintf (file
, "expr ");
2912 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
2913 fprintf (file
, " ");
2917 fprintf (file
, "illegal");
2920 fprintf (file
, "absent");
2923 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
2927 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2928 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2929 fprintf (file
, ">");
2931 if (exp
->X_add_number
)
2932 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2933 (unsigned long) exp
->X_add_number
);
2937 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2940 fprintf (file
, "big");
2943 fprintf (file
, "uminus -<");
2945 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2946 fprintf (file
, ">");
2947 goto maybe_print_addnum
;
2949 fprintf (file
, "bit_not");
2952 print_binary (file
, "multiply", exp
);
2955 print_binary (file
, "divide", exp
);
2958 print_binary (file
, "modulus", exp
);
2961 print_binary (file
, "lshift", exp
);
2964 print_binary (file
, "rshift", exp
);
2966 case O_bit_inclusive_or
:
2967 print_binary (file
, "bit_ior", exp
);
2969 case O_bit_exclusive_or
:
2970 print_binary (file
, "bit_xor", exp
);
2973 print_binary (file
, "bit_and", exp
);
2976 print_binary (file
, "eq", exp
);
2979 print_binary (file
, "ne", exp
);
2982 print_binary (file
, "lt", exp
);
2985 print_binary (file
, "le", exp
);
2988 print_binary (file
, "ge", exp
);
2991 print_binary (file
, "gt", exp
);
2994 print_binary (file
, "logical_and", exp
);
2997 print_binary (file
, "logical_or", exp
);
3001 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3002 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3003 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3004 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3005 fprintf (file
, ">");
3006 goto maybe_print_addnum
;
3009 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3010 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3011 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3012 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3013 fprintf (file
, ">");
3014 goto maybe_print_addnum
;
3016 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3023 print_expr (expressionS
*exp
)
3025 print_expr_1 (stderr
, exp
);
3026 fprintf (stderr
, "\n");
3030 symbol_print_statistics (FILE *file
)
3032 hash_print_statistics (file
, "symbol table", sy_hash
);
3033 hash_print_statistics (file
, "mini local symbol table", local_hash
);
3034 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3035 local_symbol_count
, local_symbol_conversion_count
);
3038 #ifdef OBJ_COMPLEX_RELC
3040 /* Convert given symbol to a new complex-relocation symbol name. This
3041 may be a recursive function, since it might be called for non-leaf
3042 nodes (plain symbols) in the expression tree. The caller owns the
3043 returning string, so should free it eventually. Errors are
3044 indicated via as_bad and a NULL return value. The given symbol
3045 is marked with sy_used_in_reloc. */
3048 symbol_relc_make_sym (symbolS
* sym
)
3050 char * terminal
= NULL
;
3055 gas_assert (sym
!= NULL
);
3057 /* Recurse to symbol_relc_make_expr if this symbol
3058 is defined as an expression or a plain value. */
3059 if ( S_GET_SEGMENT (sym
) == expr_section
3060 || S_GET_SEGMENT (sym
) == absolute_section
)
3061 return symbol_relc_make_expr (& sym
->sy_value
);
3063 /* This may be a "fake symbol" L0\001, referring to ".".
3064 Write out a special null symbol to refer to this position. */
3065 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3066 return xstrdup (".");
3068 /* We hope this is a plain leaf symbol. Construct the encoding
3069 as {S,s}II...:CCCCCCC....
3070 where 'S'/'s' means section symbol / plain symbol
3071 III is decimal for the symbol name length
3072 CCC is the symbol name itself. */
3073 symbol_mark_used_in_reloc (sym
);
3075 sname
= S_GET_NAME (sym
);
3076 sname_len
= strlen (sname
);
3077 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3079 terminal
= xmalloc (1 /* S or s */
3080 + 8 /* sname_len in decimal */
3082 + sname_len
/* name itself */
3085 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3089 /* Convert given value to a new complex-relocation symbol name. This
3090 is a non-recursive function, since it is be called for leaf nodes
3091 (plain values) in the expression tree. The caller owns the
3092 returning string, so should free() it eventually. No errors. */
3095 symbol_relc_make_value (offsetT val
)
3097 char * terminal
= xmalloc (28); /* Enough for long long. */
3100 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3104 /* Convert given expression to a new complex-relocation symbol name.
3105 This is a recursive function, since it traverses the entire given
3106 expression tree. The caller owns the returning string, so should
3107 free() it eventually. Errors are indicated via as_bad() and a NULL
3111 symbol_relc_make_expr (expressionS
* exp
)
3113 char * opstr
= NULL
; /* Operator prefix string. */
3114 int arity
= 0; /* Arity of this operator. */
3115 char * operands
[3]; /* Up to three operands. */
3116 char * concat_string
= NULL
;
3118 operands
[0] = operands
[1] = operands
[2] = NULL
;
3120 gas_assert (exp
!= NULL
);
3122 /* Match known operators -> fill in opstr, arity, operands[] and fall
3123 through to construct subexpression fragments; may instead return
3124 string directly for leaf nodes. */
3126 /* See expr.h for the meaning of all these enums. Many operators
3127 have an unnatural arity (X_add_number implicitly added). The
3128 conversion logic expands them to explicit "+" subexpressions. */
3133 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3138 return symbol_relc_make_value (exp
->X_add_number
);
3141 if (exp
->X_add_number
)
3145 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3146 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3150 return symbol_relc_make_sym (exp
->X_add_symbol
);
3152 /* Helper macros for nesting nodes. */
3154 #define HANDLE_XADD_OPT1(str_) \
3155 if (exp->X_add_number) \
3158 opstr = "+:" str_; \
3159 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3160 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3167 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3171 #define HANDLE_XADD_OPT2(str_) \
3172 if (exp->X_add_number) \
3175 opstr = "+:" str_; \
3176 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3177 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3178 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3184 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3185 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3189 /* Nesting nodes. */
3191 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3192 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3193 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3194 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3195 case O_divide
: HANDLE_XADD_OPT2 ("/");
3196 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3197 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3198 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3199 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3200 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3201 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3202 case O_add
: HANDLE_XADD_OPT2 ("+");
3203 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3204 case O_eq
: HANDLE_XADD_OPT2 ("==");
3205 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3206 case O_lt
: HANDLE_XADD_OPT2 ("<");
3207 case O_le
: HANDLE_XADD_OPT2 ("<=");
3208 case O_ge
: HANDLE_XADD_OPT2 (">=");
3209 case O_gt
: HANDLE_XADD_OPT2 (">");
3210 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3211 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3214 /* Validate & reject early. */
3215 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3217 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3219 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3223 concat_string
= NULL
;
3226 /* Allocate new string; include inter-operand padding gaps etc. */
3227 concat_string
= xmalloc (strlen (opstr
)
3229 + (arity
>= 1 ? (strlen (operands
[0]) + 1 ) : 0)
3230 + (arity
>= 2 ? (strlen (operands
[1]) + 1 ) : 0)
3231 + (arity
>= 3 ? (strlen (operands
[2]) + 0 ) : 0)
3233 gas_assert (concat_string
!= NULL
);
3235 /* Format the thing. */
3236 sprintf (concat_string
,
3237 (arity
== 0 ? "%s" :
3238 arity
== 1 ? "%s:%s" :
3239 arity
== 2 ? "%s:%s:%s" :
3240 /* arity == 3 */ "%s:%s:%s:%s"),
3241 opstr
, operands
[0], operands
[1], operands
[2]);
3244 /* Free operand strings (not opstr). */
3245 if (arity
>= 1) xfree (operands
[0]);
3246 if (arity
>= 2) xfree (operands
[1]);
3247 if (arity
>= 3) xfree (operands
[2]);
3249 return concat_string
;