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'
64 /* The name of an external symbol which is
65 used to make weak PE symbol names unique. */
66 const char * an_external_name
;
69 static char *save_symbol_name (const char *);
70 static void fb_label_init (void);
71 static long dollar_label_instance (long);
72 static long fb_label_instance (long);
74 static void print_binary (FILE *, const char *, expressionS
*);
76 /* Return a pointer to a new symbol. Die if we can't make a new
77 symbol. Fill in the symbol's values. Add symbol to end of symbol
80 This function should be called in the general case of creating a
81 symbol. However, if the output file symbol table has already been
82 set, and you are certain that this symbol won't be wanted in the
83 output file, you can call symbol_create. */
86 symbol_new (const char *name
, segT segment
, valueT valu
, fragS
*frag
)
88 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
90 /* Link to end of symbol chain. */
92 extern int symbol_table_frozen
;
93 if (symbol_table_frozen
)
96 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
101 /* Save a symbol name on a permanent obstack, and convert it according
102 to the object file format. */
105 save_symbol_name (const char *name
)
107 unsigned int name_length
;
110 name_length
= strlen (name
) + 1; /* +1 for \0. */
111 obstack_grow (¬es
, name
, name_length
);
112 ret
= (char *) obstack_finish (¬es
);
114 #ifdef tc_canonicalize_symbol_name
115 ret
= tc_canonicalize_symbol_name (ret
);
118 if (! symbols_case_sensitive
)
122 for (s
= ret
; *s
!= '\0'; s
++)
130 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
131 segT segment
, /* Segment identifier (SEG_<something>). */
132 valueT valu
, /* Symbol value. */
133 fragS
*frag
/* Associated fragment. */)
135 char *preserved_copy_of_name
;
138 preserved_copy_of_name
= save_symbol_name (name
);
140 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
142 /* symbol must be born in some fixed state. This seems as good as any. */
143 memset (symbolP
, 0, sizeof (symbolS
));
145 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
146 if (symbolP
->bsym
== NULL
)
147 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
148 S_SET_NAME (symbolP
, preserved_copy_of_name
);
150 S_SET_SEGMENT (symbolP
, segment
);
151 S_SET_VALUE (symbolP
, valu
);
152 symbol_clear_list_pointers (symbolP
);
154 symbolP
->sy_frag
= frag
;
156 obj_symbol_new_hook (symbolP
);
158 #ifdef tc_symbol_new_hook
159 tc_symbol_new_hook (symbolP
);
166 /* Local symbol support. If we can get away with it, we keep only a
167 small amount of information for local symbols. */
169 static symbolS
*local_symbol_convert (struct local_symbol
*);
171 /* Used for statistics. */
173 static unsigned long local_symbol_count
;
174 static unsigned long local_symbol_conversion_count
;
176 /* This macro is called with a symbol argument passed by reference.
177 It returns whether this is a local symbol. If necessary, it
178 changes its argument to the real symbol. */
180 #define LOCAL_SYMBOL_CHECK(s) \
182 ? (local_symbol_converted_p ((struct local_symbol *) s) \
183 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
188 /* Create a local symbol and insert it into the local hash table. */
190 static struct local_symbol
*
191 local_symbol_make (const char *name
, segT section
, valueT val
, fragS
*frag
)
194 struct local_symbol
*ret
;
196 ++local_symbol_count
;
198 name_copy
= save_symbol_name (name
);
200 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
201 ret
->lsy_marker
= NULL
;
202 ret
->lsy_name
= name_copy
;
203 ret
->lsy_section
= section
;
204 local_symbol_set_frag (ret
, frag
);
205 ret
->lsy_value
= val
;
207 hash_jam (local_hash
, name_copy
, (void *) ret
);
212 /* Convert a local symbol into a real symbol. Note that we do not
213 reclaim the space used by the local symbol. */
216 local_symbol_convert (struct local_symbol
*locsym
)
220 gas_assert (locsym
->lsy_marker
== NULL
);
221 if (local_symbol_converted_p (locsym
))
222 return local_symbol_get_real_symbol (locsym
);
224 ++local_symbol_conversion_count
;
226 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
227 local_symbol_get_frag (locsym
));
229 if (local_symbol_resolved_p (locsym
))
230 ret
->sy_resolved
= 1;
232 /* Local symbols are always either defined or used. */
235 #ifdef TC_LOCAL_SYMFIELD_CONVERT
236 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
239 symbol_table_insert (ret
);
241 local_symbol_mark_converted (locsym
);
242 local_symbol_set_real_symbol (locsym
, ret
);
244 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
250 define_sym_at_dot (symbolS
*symbolP
)
252 symbolP
->sy_frag
= frag_now
;
253 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
254 S_SET_SEGMENT (symbolP
, now_seg
);
257 /* We have just seen "<name>:".
258 Creates a struct symbol unless it already exists.
260 Gripes if we are redefining a symbol incompatibly (and ignores it). */
263 colon (/* Just seen "x:" - rattle symbols & frags. */
264 const char *sym_name
/* Symbol name, as a cannonical string. */
265 /* We copy this string: OK to alter later. */)
267 register symbolS
*symbolP
; /* Symbol we are working with. */
269 /* Sun local labels go out of scope whenever a non-local symbol is
271 if (LOCAL_LABELS_DOLLAR
272 && !bfd_is_local_label_name (stdoutput
, sym_name
))
273 dollar_label_clear ();
275 #ifndef WORKING_DOT_WORD
276 if (new_broken_words
)
278 struct broken_word
*a
;
283 if (now_seg
== absolute_section
)
285 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
289 possible_bytes
= (md_short_jump_size
290 + new_broken_words
* md_long_jump_size
);
293 frag_opcode
= frag_var (rs_broken_word
,
297 (symbolS
*) broken_words
,
301 /* We want to store the pointer to where to insert the jump
302 table in the fr_opcode of the rs_broken_word frag. This
303 requires a little hackery. */
305 && (frag_tmp
->fr_type
!= rs_broken_word
306 || frag_tmp
->fr_opcode
))
307 frag_tmp
= frag_tmp
->fr_next
;
309 frag_tmp
->fr_opcode
= frag_opcode
;
310 new_broken_words
= 0;
312 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
313 a
->dispfrag
= frag_tmp
;
315 #endif /* WORKING_DOT_WORD */
317 if ((symbolP
= symbol_find (sym_name
)) != 0)
319 S_CLEAR_WEAKREFR (symbolP
);
320 #ifdef RESOLVE_SYMBOL_REDEFINITION
321 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
324 /* Now check for undefined symbols. */
325 if (LOCAL_SYMBOL_CHECK (symbolP
))
327 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
329 if (locsym
->lsy_section
!= undefined_section
330 && (local_symbol_get_frag (locsym
) != frag_now
331 || locsym
->lsy_section
!= now_seg
332 || locsym
->lsy_value
!= frag_now_fix ()))
334 as_bad (_("symbol `%s' is already defined"), sym_name
);
338 locsym
->lsy_section
= now_seg
;
339 local_symbol_set_frag (locsym
, frag_now
);
340 locsym
->lsy_value
= frag_now_fix ();
342 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
343 || S_IS_COMMON (symbolP
)
344 || S_IS_VOLATILE (symbolP
))
346 if (S_IS_VOLATILE (symbolP
))
348 symbolP
= symbol_clone (symbolP
, 1);
349 S_SET_VALUE (symbolP
, 0);
350 S_CLEAR_VOLATILE (symbolP
);
352 if (S_GET_VALUE (symbolP
) == 0)
354 define_sym_at_dot (symbolP
);
357 #endif /* if we have one, it better be zero. */
362 /* There are still several cases to check:
364 A .comm/.lcomm symbol being redefined as initialized
367 A .comm/.lcomm symbol being redefined with a larger
370 This only used to be allowed on VMS gas, but Sun cc
371 on the sparc also depends on it. */
373 if (((!S_IS_DEBUG (symbolP
)
374 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
375 && S_IS_EXTERNAL (symbolP
))
376 || S_GET_SEGMENT (symbolP
) == bss_section
)
377 && (now_seg
== data_section
378 || now_seg
== bss_section
379 || now_seg
== S_GET_SEGMENT (symbolP
)))
381 /* Select which of the 2 cases this is. */
382 if (now_seg
!= data_section
)
384 /* New .comm for prev .comm symbol.
386 If the new size is larger we just change its
387 value. If the new size is smaller, we ignore
389 if (S_GET_VALUE (symbolP
)
390 < ((unsigned) frag_now_fix ()))
392 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
397 /* It is a .comm/.lcomm being converted to initialized
399 define_sym_at_dot (symbolP
);
404 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
405 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
406 static const char *od_buf
= "";
410 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
411 sprintf (od_buf
, "%d.%d.",
412 S_GET_OTHER (symbolP
),
413 S_GET_DESC (symbolP
));
415 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
417 segment_name (S_GET_SEGMENT (symbolP
)),
419 (long) S_GET_VALUE (symbolP
));
421 } /* if the undefined symbol has no value */
425 /* Don't blow up if the definition is the same. */
426 if (!(frag_now
== symbolP
->sy_frag
427 && S_GET_VALUE (symbolP
) == frag_now_fix ()
428 && S_GET_SEGMENT (symbolP
) == now_seg
))
430 as_bad (_("symbol `%s' is already defined"), sym_name
);
431 symbolP
= symbol_clone (symbolP
, 0);
432 define_sym_at_dot (symbolP
);
437 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
439 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
440 (valueT
) frag_now_fix (),
445 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
448 symbol_table_insert (symbolP
);
451 if (mri_common_symbol
!= NULL
)
453 /* This symbol is actually being defined within an MRI common
454 section. This requires special handling. */
455 if (LOCAL_SYMBOL_CHECK (symbolP
))
456 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
457 symbolP
->sy_value
.X_op
= O_symbol
;
458 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
459 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
460 symbolP
->sy_frag
= &zero_address_frag
;
461 S_SET_SEGMENT (symbolP
, expr_section
);
462 symbolP
->sy_mri_common
= 1;
466 tc_frob_label (symbolP
);
468 #ifdef obj_frob_label
469 obj_frob_label (symbolP
);
475 /* Die if we can't insert the symbol. */
478 symbol_table_insert (symbolS
*symbolP
)
480 register const char *error_string
;
483 know (S_GET_NAME (symbolP
));
485 if (LOCAL_SYMBOL_CHECK (symbolP
))
487 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
489 if (error_string
!= NULL
)
490 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
491 S_GET_NAME (symbolP
), error_string
);
495 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (void *) symbolP
)))
497 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
498 S_GET_NAME (symbolP
), error_string
);
502 /* If a symbol name does not exist, create it as undefined, and insert
503 it into the symbol table. Return a pointer to it. */
506 symbol_find_or_make (const char *name
)
508 register symbolS
*symbolP
;
510 symbolP
= symbol_find (name
);
514 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
516 symbolP
= md_undefined_symbol ((char *) name
);
520 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
526 symbolP
= symbol_make (name
);
528 symbol_table_insert (symbolP
);
529 } /* if symbol wasn't found */
535 symbol_make (const char *name
)
539 /* Let the machine description default it, e.g. for register names. */
540 symbolP
= md_undefined_symbol ((char *) name
);
543 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
549 symbol_clone (symbolS
*orgsymP
, int replace
)
552 asymbol
*bsymorg
, *bsymnew
;
554 /* Make sure we never clone the dot special symbol. */
555 gas_assert (orgsymP
!= &dot_symbol
);
557 /* Running local_symbol_convert on a clone that's not the one currently
558 in local_hash would incorrectly replace the hash entry. Thus the
559 symbol must be converted here. Note that the rest of the function
560 depends on not encountering an unconverted symbol. */
561 if (LOCAL_SYMBOL_CHECK (orgsymP
))
562 orgsymP
= local_symbol_convert ((struct local_symbol
*) orgsymP
);
563 bsymorg
= orgsymP
->bsym
;
565 newsymP
= (symbolS
*) obstack_alloc (¬es
, sizeof (*newsymP
));
567 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
569 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
570 newsymP
->bsym
= bsymnew
;
571 bsymnew
->name
= bsymorg
->name
;
572 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
573 bsymnew
->section
= bsymorg
->section
;
574 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
575 bfd_asymbol_bfd (bsymnew
), bsymnew
);
577 #ifdef obj_symbol_clone_hook
578 obj_symbol_clone_hook (newsymP
, orgsymP
);
581 #ifdef tc_symbol_clone_hook
582 tc_symbol_clone_hook (newsymP
, orgsymP
);
587 if (symbol_rootP
== orgsymP
)
588 symbol_rootP
= newsymP
;
589 else if (orgsymP
->sy_previous
)
591 orgsymP
->sy_previous
->sy_next
= newsymP
;
592 orgsymP
->sy_previous
= NULL
;
594 if (symbol_lastP
== orgsymP
)
595 symbol_lastP
= newsymP
;
596 else if (orgsymP
->sy_next
)
597 orgsymP
->sy_next
->sy_previous
= newsymP
;
599 /* Symbols that won't be output can't be external. */
600 S_CLEAR_EXTERNAL (orgsymP
);
601 orgsymP
->sy_previous
= orgsymP
->sy_next
= orgsymP
;
602 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
604 symbol_table_insert (newsymP
);
608 /* Symbols that won't be output can't be external. */
609 S_CLEAR_EXTERNAL (newsymP
);
610 newsymP
->sy_previous
= newsymP
->sy_next
= newsymP
;
616 /* Referenced symbols, if they are forward references, need to be cloned
617 (without replacing the original) so that the value of the referenced
618 symbols at the point of use . */
620 #undef symbol_clone_if_forward_ref
622 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
624 if (symbolP
&& !LOCAL_SYMBOL_CHECK (symbolP
))
626 symbolS
*add_symbol
= symbolP
->sy_value
.X_add_symbol
;
627 symbolS
*op_symbol
= symbolP
->sy_value
.X_op_symbol
;
629 if (symbolP
->sy_forward_ref
)
634 /* assign_symbol() clones volatile symbols; pre-existing expressions
635 hold references to the original instance, but want the current
636 value. Just repeat the lookup. */
637 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
638 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
639 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
640 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
643 /* Re-using sy_resolving here, as this routine cannot get called from
644 symbol resolution code. */
645 if ((symbolP
->bsym
->section
== expr_section
|| symbolP
->sy_forward_ref
)
646 && !symbolP
->sy_resolving
)
648 symbolP
->sy_resolving
= 1;
649 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
650 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
651 symbolP
->sy_resolving
= 0;
654 if (symbolP
->sy_forward_ref
655 || add_symbol
!= symbolP
->sy_value
.X_add_symbol
656 || op_symbol
!= symbolP
->sy_value
.X_op_symbol
)
658 if (symbolP
!= &dot_symbol
)
660 symbolP
= symbol_clone (symbolP
, 0);
661 symbolP
->sy_resolving
= 0;
665 symbolP
= symbol_temp_new_now ();
666 #ifdef tc_new_dot_label
667 tc_new_dot_label (symbolP
);
672 symbolP
->sy_value
.X_add_symbol
= add_symbol
;
673 symbolP
->sy_value
.X_op_symbol
= op_symbol
;
680 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
682 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
686 symbol_temp_new_now (void)
688 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
692 symbol_temp_make (void)
694 return symbol_make (FAKE_LABEL_NAME
);
697 /* Implement symbol table lookup.
698 In: A symbol's name as a string: '\0' can't be part of a symbol name.
699 Out: NULL if the name was not in the symbol table, else the address
700 of a struct symbol associated with that name. */
703 symbol_find_exact (const char *name
)
705 return symbol_find_exact_noref (name
, 0);
709 symbol_find_exact_noref (const char *name
, int noref
)
711 struct local_symbol
*locsym
;
714 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
716 return (symbolS
*) locsym
;
718 sym
= ((symbolS
*) hash_find (sy_hash
, name
));
720 /* Any references to the symbol, except for the reference in
721 .weakref, must clear this flag, such that the symbol does not
722 turn into a weak symbol. Note that we don't have to handle the
723 local_symbol case, since a weakrefd is always promoted out of the
724 local_symbol table when it is turned into a weak symbol. */
726 S_CLEAR_WEAKREFD (sym
);
732 symbol_find (const char *name
)
734 return symbol_find_noref (name
, 0);
738 symbol_find_noref (const char *name
, int noref
)
740 #ifdef tc_canonicalize_symbol_name
743 size_t len
= strlen (name
) + 1;
745 copy
= (char *) alloca (len
);
746 memcpy (copy
, name
, len
);
747 name
= tc_canonicalize_symbol_name (copy
);
751 if (! symbols_case_sensitive
)
758 name
= copy
= (char *) alloca (strlen (name
) + 1);
760 while ((c
= *orig
++) != '\0')
762 *copy
++ = TOUPPER (c
);
767 return symbol_find_exact_noref (name
, noref
);
770 /* Once upon a time, symbols were kept in a singly linked list. At
771 least coff needs to be able to rearrange them from time to time, for
772 which a doubly linked list is much more convenient. Loic did these
773 as macros which seemed dangerous to me so they're now functions.
776 /* Link symbol ADDME after symbol TARGET in the chain. */
779 symbol_append (symbolS
*addme
, symbolS
*target
,
780 symbolS
**rootPP
, symbolS
**lastPP
)
782 if (LOCAL_SYMBOL_CHECK (addme
))
784 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
789 know (*rootPP
== NULL
);
790 know (*lastPP
== NULL
);
791 addme
->sy_next
= NULL
;
792 addme
->sy_previous
= NULL
;
796 } /* if the list is empty */
798 if (target
->sy_next
!= NULL
)
800 target
->sy_next
->sy_previous
= addme
;
804 know (*lastPP
== target
);
806 } /* if we have a next */
808 addme
->sy_next
= target
->sy_next
;
809 target
->sy_next
= addme
;
810 addme
->sy_previous
= target
;
812 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
815 /* Set the chain pointers of SYMBOL to null. */
818 symbol_clear_list_pointers (symbolS
*symbolP
)
820 if (LOCAL_SYMBOL_CHECK (symbolP
))
822 symbolP
->sy_next
= NULL
;
823 symbolP
->sy_previous
= NULL
;
826 /* Remove SYMBOLP from the list. */
829 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
831 if (LOCAL_SYMBOL_CHECK (symbolP
))
834 if (symbolP
== *rootPP
)
836 *rootPP
= symbolP
->sy_next
;
837 } /* if it was the root */
839 if (symbolP
== *lastPP
)
841 *lastPP
= symbolP
->sy_previous
;
842 } /* if it was the tail */
844 if (symbolP
->sy_next
!= NULL
)
846 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
849 if (symbolP
->sy_previous
!= NULL
)
851 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
854 debug_verify_symchain (*rootPP
, *lastPP
);
857 /* Link symbol ADDME before symbol TARGET in the chain. */
860 symbol_insert (symbolS
*addme
, symbolS
*target
,
861 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
863 if (LOCAL_SYMBOL_CHECK (addme
))
865 if (LOCAL_SYMBOL_CHECK (target
))
868 if (target
->sy_previous
!= NULL
)
870 target
->sy_previous
->sy_next
= addme
;
874 know (*rootPP
== target
);
878 addme
->sy_previous
= target
->sy_previous
;
879 target
->sy_previous
= addme
;
880 addme
->sy_next
= target
;
882 debug_verify_symchain (*rootPP
, *lastPP
);
886 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
888 symbolS
*symbolP
= rootP
;
893 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
895 gas_assert (symbolP
->bsym
!= NULL
);
896 gas_assert (symbolP
->sy_next
->sy_previous
== symbolP
);
899 gas_assert (lastP
== symbolP
);
902 #ifdef OBJ_COMPLEX_RELC
905 use_complex_relocs_for (symbolS
* symp
)
907 switch (symp
->sy_value
.X_op
)
917 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
918 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
920 (S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
921 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
))
930 case O_bit_inclusive_or
:
932 case O_bit_exclusive_or
:
945 if ( (S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
946 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
948 (S_IS_COMMON (symp
->sy_value
.X_op_symbol
)
949 || S_IS_LOCAL (symp
->sy_value
.X_op_symbol
))
951 && S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
952 && S_IS_DEFINED (symp
->sy_value
.X_op_symbol
)
953 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
954 && S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) != expr_section
)
966 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
970 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
971 segT seg_right
= S_GET_SEGMENT (right
);
980 case O_uminus
: opname
= "-"; break;
981 case O_bit_not
: opname
= "~"; break;
982 case O_logical_not
: opname
= "!"; break;
983 case O_multiply
: opname
= "*"; break;
984 case O_divide
: opname
= "/"; break;
985 case O_modulus
: opname
= "%"; break;
986 case O_left_shift
: opname
= "<<"; break;
987 case O_right_shift
: opname
= ">>"; break;
988 case O_bit_inclusive_or
: opname
= "|"; break;
989 case O_bit_or_not
: opname
= "|~"; break;
990 case O_bit_exclusive_or
: opname
= "^"; break;
991 case O_bit_and
: opname
= "&"; break;
992 case O_add
: opname
= "+"; break;
993 case O_subtract
: opname
= "-"; break;
994 case O_eq
: opname
= "=="; break;
995 case O_ne
: opname
= "!="; break;
996 case O_lt
: opname
= "<"; break;
997 case O_le
: opname
= "<="; break;
998 case O_ge
: opname
= ">="; break;
999 case O_gt
: opname
= ">"; break;
1000 case O_logical_and
: opname
= "&&"; break;
1001 case O_logical_or
: opname
= "||"; break;
1004 if (expr_symbol_where (symp
, &file
, &line
))
1007 as_bad_where (file
, line
,
1008 _("invalid operands (%s and %s sections) for `%s'"),
1009 seg_left
->name
, seg_right
->name
, opname
);
1011 as_bad_where (file
, line
,
1012 _("invalid operand (%s section) for `%s'"),
1013 seg_right
->name
, opname
);
1017 const char *sname
= S_GET_NAME (symp
);
1020 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1021 seg_left
->name
, seg_right
->name
, opname
, sname
);
1023 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1024 seg_right
->name
, opname
, sname
);
1028 /* Resolve the value of a symbol. This is called during the final
1029 pass over the symbol table to resolve any symbols with complex
1033 resolve_symbol_value (symbolS
*symp
)
1036 valueT final_val
= 0;
1039 if (LOCAL_SYMBOL_CHECK (symp
))
1041 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1043 final_val
= locsym
->lsy_value
;
1044 if (local_symbol_resolved_p (locsym
))
1047 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
1051 locsym
->lsy_value
= final_val
;
1052 local_symbol_mark_resolved (locsym
);
1058 if (symp
->sy_resolved
)
1060 if (symp
->sy_value
.X_op
== O_constant
)
1061 return (valueT
) symp
->sy_value
.X_add_number
;
1067 final_seg
= S_GET_SEGMENT (symp
);
1069 if (symp
->sy_resolving
)
1072 as_bad (_("symbol definition loop encountered at `%s'"),
1077 #ifdef OBJ_COMPLEX_RELC
1078 else if (final_seg
== expr_section
1079 && use_complex_relocs_for (symp
))
1081 symbolS
* relc_symbol
= NULL
;
1082 char * relc_symbol_name
= NULL
;
1084 relc_symbol_name
= symbol_relc_make_expr (& symp
->sy_value
);
1086 /* For debugging, print out conversion input & output. */
1088 print_expr (& symp
->sy_value
);
1089 if (relc_symbol_name
)
1090 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1093 if (relc_symbol_name
!= NULL
)
1094 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1095 0, & zero_address_frag
);
1097 if (relc_symbol
== NULL
)
1099 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1105 symbol_table_insert (relc_symbol
);
1107 /* S_CLEAR_EXTERNAL (relc_symbol); */
1108 if (symp
->bsym
->flags
& BSF_SRELC
)
1109 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1111 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1112 /* symp->bsym->flags |= BSF_RELC; */
1113 copy_symbol_attributes (symp
, relc_symbol
);
1114 symp
->sy_value
.X_op
= O_symbol
;
1115 symp
->sy_value
.X_add_symbol
= relc_symbol
;
1116 symp
->sy_value
.X_add_number
= 0;
1120 final_seg
= undefined_section
;
1121 goto exit_dont_set_value
;
1126 symbolS
*add_symbol
, *op_symbol
;
1127 offsetT left
, right
;
1128 segT seg_left
, seg_right
;
1132 symp
->sy_resolving
= 1;
1134 /* Help out with CSE. */
1135 add_symbol
= symp
->sy_value
.X_add_symbol
;
1136 op_symbol
= symp
->sy_value
.X_op_symbol
;
1137 final_val
= symp
->sy_value
.X_add_number
;
1138 op
= symp
->sy_value
.X_op
;
1151 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
1152 if (final_seg
== expr_section
)
1153 final_seg
= absolute_section
;
1162 left
= resolve_symbol_value (add_symbol
);
1163 seg_left
= S_GET_SEGMENT (add_symbol
);
1165 symp
->sy_value
.X_op_symbol
= NULL
;
1168 if (S_IS_WEAKREFR (symp
))
1170 gas_assert (final_val
== 0);
1171 if (S_IS_WEAKREFR (add_symbol
))
1173 gas_assert (add_symbol
->sy_value
.X_op
== O_symbol
1174 && add_symbol
->sy_value
.X_add_number
== 0);
1175 add_symbol
= add_symbol
->sy_value
.X_add_symbol
;
1176 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1177 symp
->sy_value
.X_add_symbol
= add_symbol
;
1181 if (symp
->sy_mri_common
)
1183 /* This is a symbol inside an MRI common section. The
1184 relocation routines are going to handle it specially.
1185 Don't change the value. */
1186 resolved
= symbol_resolved_p (add_symbol
);
1190 if (finalize_syms
&& final_val
== 0)
1192 if (LOCAL_SYMBOL_CHECK (add_symbol
))
1193 add_symbol
= local_symbol_convert ((struct local_symbol
*)
1195 copy_symbol_attributes (symp
, add_symbol
);
1198 /* If we have equated this symbol to an undefined or common
1199 symbol, keep X_op set to O_symbol, and don't change
1200 X_add_number. This permits the routine which writes out
1201 relocation to detect this case, and convert the
1202 relocation to be against the symbol to which this symbol
1204 if (! S_IS_DEFINED (add_symbol
)
1205 #if defined (OBJ_COFF) && defined (TE_PE)
1206 || S_IS_WEAK (add_symbol
)
1208 || S_IS_COMMON (add_symbol
))
1212 symp
->sy_value
.X_op
= O_symbol
;
1213 symp
->sy_value
.X_add_symbol
= add_symbol
;
1214 symp
->sy_value
.X_add_number
= final_val
;
1215 /* Use X_op_symbol as a flag. */
1216 symp
->sy_value
.X_op_symbol
= add_symbol
;
1218 final_seg
= seg_left
;
1220 resolved
= symbol_resolved_p (add_symbol
);
1221 symp
->sy_resolving
= 0;
1222 goto exit_dont_set_value
;
1224 else if (finalize_syms
1225 && ((final_seg
== expr_section
&& seg_left
!= expr_section
)
1226 || symbol_shadow_p (symp
)))
1228 /* If the symbol is an expression symbol, do similarly
1229 as for undefined and common syms above. Handles
1230 "sym +/- expr" where "expr" cannot be evaluated
1231 immediately, and we want relocations to be against
1232 "sym", eg. because it is weak. */
1233 symp
->sy_value
.X_op
= O_symbol
;
1234 symp
->sy_value
.X_add_symbol
= add_symbol
;
1235 symp
->sy_value
.X_add_number
= final_val
;
1236 symp
->sy_value
.X_op_symbol
= add_symbol
;
1237 final_seg
= seg_left
;
1238 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1239 resolved
= symbol_resolved_p (add_symbol
);
1240 symp
->sy_resolving
= 0;
1241 goto exit_dont_set_value
;
1245 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1246 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1247 final_seg
= seg_left
;
1250 resolved
= symbol_resolved_p (add_symbol
);
1251 if (S_IS_WEAKREFR (symp
))
1252 goto exit_dont_set_value
;
1258 left
= resolve_symbol_value (add_symbol
);
1259 seg_left
= S_GET_SEGMENT (add_symbol
);
1261 /* By reducing these to the relevant dyadic operator, we get
1262 !S -> S == 0 permitted on anything,
1263 -S -> 0 - S only permitted on absolute
1264 ~S -> S ^ ~0 only permitted on absolute */
1265 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1267 report_op_error (symp
, NULL
, op
, add_symbol
);
1269 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1270 final_seg
= absolute_section
;
1274 else if (op
== O_logical_not
)
1279 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1281 resolved
= symbol_resolved_p (add_symbol
);
1289 case O_bit_inclusive_or
:
1291 case O_bit_exclusive_or
:
1303 left
= resolve_symbol_value (add_symbol
);
1304 right
= resolve_symbol_value (op_symbol
);
1305 seg_left
= S_GET_SEGMENT (add_symbol
);
1306 seg_right
= S_GET_SEGMENT (op_symbol
);
1308 /* Simplify addition or subtraction of a constant by folding the
1309 constant into X_add_number. */
1312 if (seg_right
== absolute_section
)
1317 else if (seg_left
== absolute_section
)
1320 add_symbol
= op_symbol
;
1322 seg_left
= seg_right
;
1326 else if (op
== O_subtract
)
1328 if (seg_right
== absolute_section
)
1336 /* Equality and non-equality tests are permitted on anything.
1337 Subtraction, and other comparison operators are permitted if
1338 both operands are in the same section. Otherwise, both
1339 operands must be absolute. We already handled the case of
1340 addition or subtraction of a constant above. This will
1341 probably need to be changed for an object file format which
1342 supports arbitrary expressions, such as IEEE-695. */
1343 if (!(seg_left
== absolute_section
1344 && seg_right
== absolute_section
)
1345 && !(op
== O_eq
|| op
== O_ne
)
1346 && !((op
== O_subtract
1347 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1348 && seg_left
== seg_right
1349 && (seg_left
!= undefined_section
1350 || add_symbol
== op_symbol
)))
1352 /* Don't emit messages unless we're finalizing the symbol value,
1353 otherwise we may get the same message multiple times. */
1355 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1356 /* However do not move the symbol into the absolute section
1357 if it cannot currently be resolved - this would confuse
1358 other parts of the assembler into believing that the
1359 expression had been evaluated to zero. */
1365 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1366 final_seg
= absolute_section
;
1368 /* Check for division by zero. */
1369 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1371 /* If seg_right is not absolute_section, then we've
1372 already issued a warning about using a bad symbol. */
1373 if (seg_right
== absolute_section
&& finalize_syms
)
1378 if (expr_symbol_where (symp
, &file
, &line
))
1379 as_bad_where (file
, line
, _("division by zero"));
1381 as_bad (_("division by zero when setting `%s'"),
1388 switch (symp
->sy_value
.X_op
)
1390 case O_multiply
: left
*= right
; break;
1391 case O_divide
: left
/= right
; break;
1392 case O_modulus
: left
%= right
; break;
1393 case O_left_shift
: left
<<= right
; break;
1394 case O_right_shift
: left
>>= right
; break;
1395 case O_bit_inclusive_or
: left
|= right
; break;
1396 case O_bit_or_not
: left
|= ~right
; break;
1397 case O_bit_exclusive_or
: left
^= right
; break;
1398 case O_bit_and
: left
&= right
; break;
1399 case O_add
: left
+= right
; break;
1400 case O_subtract
: left
-= right
; break;
1403 left
= (left
== right
&& seg_left
== seg_right
1404 && (seg_left
!= undefined_section
1405 || add_symbol
== op_symbol
)
1406 ? ~ (offsetT
) 0 : 0);
1407 if (symp
->sy_value
.X_op
== O_ne
)
1410 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1411 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1412 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1413 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1414 case O_logical_and
: left
= left
&& right
; break;
1415 case O_logical_or
: left
= left
|| right
; break;
1419 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1420 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1422 if (seg_left
== undefined_section
1423 || seg_right
== undefined_section
)
1424 final_seg
= undefined_section
;
1425 else if (seg_left
== absolute_section
)
1426 final_seg
= seg_right
;
1428 final_seg
= seg_left
;
1430 resolved
= (symbol_resolved_p (add_symbol
)
1431 && symbol_resolved_p (op_symbol
));
1436 /* Give an error (below) if not in expr_section. We don't
1437 want to worry about expr_section symbols, because they
1438 are fictional (they are created as part of expression
1439 resolution), and any problems may not actually mean
1444 symp
->sy_resolving
= 0;
1448 S_SET_VALUE (symp
, final_val
);
1450 exit_dont_set_value
:
1451 /* Always set the segment, even if not finalizing the value.
1452 The segment is used to determine whether a symbol is defined. */
1453 S_SET_SEGMENT (symp
, final_seg
);
1455 /* Don't worry if we can't resolve an expr_section symbol. */
1459 symp
->sy_resolved
= 1;
1460 else if (S_GET_SEGMENT (symp
) != expr_section
)
1462 as_bad (_("can't resolve value for symbol `%s'"),
1464 symp
->sy_resolved
= 1;
1471 static void resolve_local_symbol (const char *, void *);
1473 /* A static function passed to hash_traverse. */
1476 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, void *value
)
1479 resolve_symbol_value ((symbolS
*) value
);
1482 /* Resolve all local symbols. */
1485 resolve_local_symbol_values (void)
1487 hash_traverse (local_hash
, resolve_local_symbol
);
1490 /* Obtain the current value of a symbol without changing any
1491 sub-expressions used. */
1494 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1496 symbolS
*symbolP
= *symbolPP
;
1498 if (LOCAL_SYMBOL_CHECK (symbolP
))
1500 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1502 *valueP
= locsym
->lsy_value
;
1503 *segP
= locsym
->lsy_section
;
1504 *fragPP
= local_symbol_get_frag (locsym
);
1508 expressionS exp
= symbolP
->sy_value
;
1510 if (!symbolP
->sy_resolved
&& exp
.X_op
!= O_illegal
)
1514 if (symbolP
->sy_resolving
)
1516 symbolP
->sy_resolving
= 1;
1517 resolved
= resolve_expression (&exp
);
1518 symbolP
->sy_resolving
= 0;
1526 if (!symbol_equated_p (symbolP
))
1531 symbolP
= exp
.X_add_symbol
;
1538 *symbolPP
= symbolP
;
1539 *valueP
= exp
.X_add_number
;
1540 *segP
= symbolP
->bsym
->section
;
1541 *fragPP
= symbolP
->sy_frag
;
1543 if (*segP
== expr_section
)
1546 case O_constant
: *segP
= absolute_section
; break;
1547 case O_register
: *segP
= reg_section
; break;
1555 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1556 They are *really* local. That is, they go out of scope whenever we see a
1557 label that isn't local. Also, like fb labels, there can be multiple
1558 instances of a dollar label. Therefor, we name encode each instance with
1559 the instance number, keep a list of defined symbols separate from the real
1560 symbol table, and we treat these buggers as a sparse array. */
1562 static long *dollar_labels
;
1563 static long *dollar_label_instances
;
1564 static char *dollar_label_defines
;
1565 static unsigned long dollar_label_count
;
1566 static unsigned long dollar_label_max
;
1569 dollar_label_defined (long label
)
1573 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1575 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1577 return dollar_label_defines
[i
- dollar_labels
];
1579 /* If we get here, label isn't defined. */
1584 dollar_label_instance (long label
)
1588 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1590 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1592 return (dollar_label_instances
[i
- dollar_labels
]);
1594 /* If we get here, we haven't seen the label before.
1595 Therefore its instance count is zero. */
1600 dollar_label_clear (void)
1602 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1605 #define DOLLAR_LABEL_BUMP_BY 10
1608 define_dollar_label (long label
)
1612 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1615 ++dollar_label_instances
[i
- dollar_labels
];
1616 dollar_label_defines
[i
- dollar_labels
] = 1;
1620 /* If we get to here, we don't have label listed yet. */
1622 if (dollar_labels
== NULL
)
1624 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1625 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1626 dollar_label_defines
= (char *) xmalloc (DOLLAR_LABEL_BUMP_BY
);
1627 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1628 dollar_label_count
= 0;
1630 else if (dollar_label_count
== dollar_label_max
)
1632 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1633 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
1634 dollar_label_max
* sizeof (long));
1635 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
1636 dollar_label_max
* sizeof (long));
1637 dollar_label_defines
= (char *) xrealloc (dollar_label_defines
, dollar_label_max
);
1638 } /* if we needed to grow */
1640 dollar_labels
[dollar_label_count
] = label
;
1641 dollar_label_instances
[dollar_label_count
] = 1;
1642 dollar_label_defines
[dollar_label_count
] = 1;
1643 ++dollar_label_count
;
1646 /* Caller must copy returned name: we re-use the area for the next name.
1648 The mth occurence of label n: is turned into the symbol "Ln^Am"
1649 where n is the label number and m is the instance number. "L" makes
1650 it a label discarded unless debugging and "^A"('\1') ensures no
1651 ordinary symbol SHOULD get the same name as a local label
1652 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1654 fb labels get the same treatment, except that ^B is used in place
1657 char * /* Return local label name. */
1658 dollar_label_name (register long n
, /* we just saw "n$:" : n a number. */
1659 register int augend
/* 0 for current instance, 1 for new instance. */)
1662 /* Returned to caller, then copied. Used for created names ("4f"). */
1663 static char symbol_name_build
[24];
1666 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1669 know (augend
== 0 || augend
== 1);
1670 p
= symbol_name_build
;
1671 #ifdef LOCAL_LABEL_PREFIX
1672 *p
++ = LOCAL_LABEL_PREFIX
;
1676 /* Next code just does sprintf( {}, "%d", n); */
1678 q
= symbol_name_temporary
;
1679 for (*q
++ = 0, i
= n
; i
; ++q
)
1684 while ((*p
= *--q
) != '\0')
1687 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1689 /* Instance number. */
1690 q
= symbol_name_temporary
;
1691 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1696 while ((*p
++ = *--q
) != '\0');;
1698 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1699 return symbol_name_build
;
1702 /* Somebody else's idea of local labels. They are made by "n:" where n
1703 is any decimal digit. Refer to them with
1704 "nb" for previous (backward) n:
1705 or "nf" for next (forward) n:.
1707 We do a little better and let n be any number, not just a single digit, but
1708 since the other guy's assembler only does ten, we treat the first ten
1711 Like someone else's assembler, we have one set of local label counters for
1712 entire assembly, not one set per (sub)segment like in most assemblers. This
1713 implies that one can refer to a label in another segment, and indeed some
1714 crufty compilers have done just that.
1716 Since there could be a LOT of these things, treat them as a sparse
1719 #define FB_LABEL_SPECIAL (10)
1721 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1722 static long *fb_labels
;
1723 static long *fb_label_instances
;
1724 static long fb_label_count
;
1725 static long fb_label_max
;
1727 /* This must be more than FB_LABEL_SPECIAL. */
1728 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1731 fb_label_init (void)
1733 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1736 /* Add one to the instance number of this fb label. */
1739 fb_label_instance_inc (long label
)
1743 if (label
< FB_LABEL_SPECIAL
)
1745 ++fb_low_counter
[label
];
1749 if (fb_labels
!= NULL
)
1751 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1752 i
< fb_labels
+ fb_label_count
; ++i
)
1756 ++fb_label_instances
[i
- fb_labels
];
1758 } /* if we find it */
1759 } /* for each existing label */
1762 /* If we get to here, we don't have label listed yet. */
1764 if (fb_labels
== NULL
)
1766 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1767 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1768 fb_label_max
= FB_LABEL_BUMP_BY
;
1769 fb_label_count
= FB_LABEL_SPECIAL
;
1772 else if (fb_label_count
== fb_label_max
)
1774 fb_label_max
+= FB_LABEL_BUMP_BY
;
1775 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1776 fb_label_max
* sizeof (long));
1777 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1778 fb_label_max
* sizeof (long));
1779 } /* if we needed to grow */
1781 fb_labels
[fb_label_count
] = label
;
1782 fb_label_instances
[fb_label_count
] = 1;
1787 fb_label_instance (long label
)
1791 if (label
< FB_LABEL_SPECIAL
)
1793 return (fb_low_counter
[label
]);
1796 if (fb_labels
!= NULL
)
1798 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1799 i
< fb_labels
+ fb_label_count
; ++i
)
1803 return (fb_label_instances
[i
- fb_labels
]);
1804 } /* if we find it */
1805 } /* for each existing label */
1808 /* We didn't find the label, so this must be a reference to the
1813 /* Caller must copy returned name: we re-use the area for the next name.
1815 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1816 where n is the label number and m is the instance number. "L" makes
1817 it a label discarded unless debugging and "^B"('\2') ensures no
1818 ordinary symbol SHOULD get the same name as a local label
1819 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1821 dollar labels get the same treatment, except that ^A is used in
1824 char * /* Return local label name. */
1825 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1826 long augend
/* 0 for nb, 1 for n:, nf. */)
1829 /* Returned to caller, then copied. Used for created names ("4f"). */
1830 static char symbol_name_build
[24];
1833 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1837 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1839 know ((unsigned long) augend
<= 1);
1841 p
= symbol_name_build
;
1842 #ifdef LOCAL_LABEL_PREFIX
1843 *p
++ = LOCAL_LABEL_PREFIX
;
1847 /* Next code just does sprintf( {}, "%d", n); */
1849 q
= symbol_name_temporary
;
1850 for (*q
++ = 0, i
= n
; i
; ++q
)
1855 while ((*p
= *--q
) != '\0')
1858 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1860 /* Instance number. */
1861 q
= symbol_name_temporary
;
1862 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1867 while ((*p
++ = *--q
) != '\0');;
1869 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1870 return (symbol_name_build
);
1873 /* Decode name that may have been generated by foo_label_name() above.
1874 If the name wasn't generated by foo_label_name(), then return it
1875 unaltered. This is used for error messages. */
1878 decode_local_label_name (char *s
)
1881 char *symbol_decode
;
1883 int instance_number
;
1885 const char *message_format
;
1888 #ifdef LOCAL_LABEL_PREFIX
1889 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
1893 if (s
[lindex
] != 'L')
1896 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
1897 label_number
= (10 * label_number
) + *p
- '0';
1899 if (*p
== DOLLAR_LABEL_CHAR
)
1901 else if (*p
== LOCAL_LABEL_CHAR
)
1906 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1907 instance_number
= (10 * instance_number
) + *p
- '0';
1909 message_format
= _("\"%d\" (instance number %d of a %s label)");
1910 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
1911 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1913 return symbol_decode
;
1916 /* Get the value of a symbol. */
1919 S_GET_VALUE (symbolS
*s
)
1921 if (LOCAL_SYMBOL_CHECK (s
))
1922 return resolve_symbol_value (s
);
1924 if (!s
->sy_resolved
)
1926 valueT val
= resolve_symbol_value (s
);
1930 if (S_IS_WEAKREFR (s
))
1931 return S_GET_VALUE (s
->sy_value
.X_add_symbol
);
1933 if (s
->sy_value
.X_op
!= O_constant
)
1935 if (! s
->sy_resolved
1936 || s
->sy_value
.X_op
!= O_symbol
1937 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1938 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1941 return (valueT
) s
->sy_value
.X_add_number
;
1944 /* Set the value of a symbol. */
1947 S_SET_VALUE (symbolS
*s
, valueT val
)
1949 if (LOCAL_SYMBOL_CHECK (s
))
1951 ((struct local_symbol
*) s
)->lsy_value
= val
;
1955 s
->sy_value
.X_op
= O_constant
;
1956 s
->sy_value
.X_add_number
= (offsetT
) val
;
1957 s
->sy_value
.X_unsigned
= 0;
1958 S_CLEAR_WEAKREFR (s
);
1962 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1964 if (LOCAL_SYMBOL_CHECK (dest
))
1965 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
1966 if (LOCAL_SYMBOL_CHECK (src
))
1967 src
= local_symbol_convert ((struct local_symbol
*) src
);
1969 /* In an expression, transfer the settings of these flags.
1970 The user can override later, of course. */
1971 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1972 | BSF_GNU_INDIRECT_FUNCTION)
1973 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1975 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1976 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1979 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1980 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1985 S_IS_FUNCTION (symbolS
*s
)
1989 if (LOCAL_SYMBOL_CHECK (s
))
1992 flags
= s
->bsym
->flags
;
1994 return (flags
& BSF_FUNCTION
) != 0;
1998 S_IS_EXTERNAL (symbolS
*s
)
2002 if (LOCAL_SYMBOL_CHECK (s
))
2005 flags
= s
->bsym
->flags
;
2008 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2011 return (flags
& BSF_GLOBAL
) != 0;
2015 S_IS_WEAK (symbolS
*s
)
2017 if (LOCAL_SYMBOL_CHECK (s
))
2019 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2020 could probably handle a WEAKREFR as always weak though. E.g., if
2021 the referenced symbol has lost its weak status, there's no reason
2022 to keep handling the weakrefr as if it was weak. */
2023 if (S_IS_WEAKREFR (s
))
2024 return S_IS_WEAK (s
->sy_value
.X_add_symbol
);
2025 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2029 S_IS_WEAKREFR (symbolS
*s
)
2031 if (LOCAL_SYMBOL_CHECK (s
))
2033 return s
->sy_weakrefr
!= 0;
2037 S_IS_WEAKREFD (symbolS
*s
)
2039 if (LOCAL_SYMBOL_CHECK (s
))
2041 return s
->sy_weakrefd
!= 0;
2045 S_IS_COMMON (symbolS
*s
)
2047 if (LOCAL_SYMBOL_CHECK (s
))
2049 return bfd_is_com_section (s
->bsym
->section
);
2053 S_IS_DEFINED (symbolS
*s
)
2055 if (LOCAL_SYMBOL_CHECK (s
))
2056 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
2057 return s
->bsym
->section
!= undefined_section
;
2061 #ifndef EXTERN_FORCE_RELOC
2062 #define EXTERN_FORCE_RELOC IS_ELF
2065 /* Return true for symbols that should not be reduced to section
2066 symbols or eliminated from expressions, because they may be
2067 overridden by the linker. */
2069 S_FORCE_RELOC (symbolS
*s
, int strict
)
2071 if (LOCAL_SYMBOL_CHECK (s
))
2072 return ((struct local_symbol
*) s
)->lsy_section
== undefined_section
;
2075 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2076 || (EXTERN_FORCE_RELOC
2077 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2078 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0
2079 || s
->bsym
->section
== undefined_section
2080 || bfd_is_com_section (s
->bsym
->section
));
2084 S_IS_DEBUG (symbolS
*s
)
2086 if (LOCAL_SYMBOL_CHECK (s
))
2088 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2094 S_IS_LOCAL (symbolS
*s
)
2099 if (LOCAL_SYMBOL_CHECK (s
))
2102 flags
= s
->bsym
->flags
;
2105 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2108 if (bfd_get_section (s
->bsym
) == reg_section
)
2111 if (flag_strip_local_absolute
2112 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2113 the source file even when the object file is stripped. */
2114 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2115 && bfd_get_section (s
->bsym
) == absolute_section
)
2118 name
= S_GET_NAME (s
);
2119 return (name
!= NULL
2121 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2122 || strchr (name
, LOCAL_LABEL_CHAR
)
2123 || (! flag_keep_locals
2124 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2127 && name
[1] == '?')))));
2131 S_IS_STABD (symbolS
*s
)
2133 return S_GET_NAME (s
) == 0;
2137 S_IS_VOLATILE (const symbolS
*s
)
2139 if (LOCAL_SYMBOL_CHECK (s
))
2141 return s
->sy_volatile
;
2145 S_IS_FORWARD_REF (const symbolS
*s
)
2147 if (LOCAL_SYMBOL_CHECK (s
))
2149 return s
->sy_forward_ref
;
2153 S_GET_NAME (symbolS
*s
)
2155 if (LOCAL_SYMBOL_CHECK (s
))
2156 return ((struct local_symbol
*) s
)->lsy_name
;
2157 return s
->bsym
->name
;
2161 S_GET_SEGMENT (symbolS
*s
)
2163 if (LOCAL_SYMBOL_CHECK (s
))
2164 return ((struct local_symbol
*) s
)->lsy_section
;
2165 return s
->bsym
->section
;
2169 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2171 /* Don't reassign section symbols. The direct reason is to prevent seg
2172 faults assigning back to const global symbols such as *ABS*, but it
2173 shouldn't happen anyway. */
2175 if (LOCAL_SYMBOL_CHECK (s
))
2177 if (seg
== reg_section
)
2178 s
= local_symbol_convert ((struct local_symbol
*) s
);
2181 ((struct local_symbol
*) s
)->lsy_section
= seg
;
2186 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2188 if (s
->bsym
->section
!= seg
)
2192 s
->bsym
->section
= seg
;
2196 S_SET_EXTERNAL (symbolS
*s
)
2198 if (LOCAL_SYMBOL_CHECK (s
))
2199 s
= local_symbol_convert ((struct local_symbol
*) s
);
2200 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2202 /* Let .weak override .global. */
2205 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2210 /* Do not reassign section symbols. */
2211 as_where (& file
, & line
);
2212 as_warn_where (file
, line
,
2213 _("section symbols are already global"));
2216 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2217 if (S_GET_SEGMENT (s
) == reg_section
)
2219 as_bad ("can't make register symbol `%s' global",
2224 s
->bsym
->flags
|= BSF_GLOBAL
;
2225 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2228 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2229 an_external_name
= S_GET_NAME (s
);
2234 S_CLEAR_EXTERNAL (symbolS
*s
)
2236 if (LOCAL_SYMBOL_CHECK (s
))
2238 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2240 /* Let .weak override. */
2243 s
->bsym
->flags
|= BSF_LOCAL
;
2244 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2248 S_SET_WEAK (symbolS
*s
)
2250 if (LOCAL_SYMBOL_CHECK (s
))
2251 s
= local_symbol_convert ((struct local_symbol
*) s
);
2252 #ifdef obj_set_weak_hook
2253 obj_set_weak_hook (s
);
2255 s
->bsym
->flags
|= BSF_WEAK
;
2256 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2260 S_SET_WEAKREFR (symbolS
*s
)
2262 if (LOCAL_SYMBOL_CHECK (s
))
2263 s
= local_symbol_convert ((struct local_symbol
*) s
);
2265 /* If the alias was already used, make sure we mark the target as
2266 used as well, otherwise it might be dropped from the symbol
2267 table. This may have unintended side effects if the alias is
2268 later redirected to another symbol, such as keeping the unused
2269 previous target in the symbol table. Since it will be weak, it's
2272 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2276 S_CLEAR_WEAKREFR (symbolS
*s
)
2278 if (LOCAL_SYMBOL_CHECK (s
))
2284 S_SET_WEAKREFD (symbolS
*s
)
2286 if (LOCAL_SYMBOL_CHECK (s
))
2287 s
= local_symbol_convert ((struct local_symbol
*) s
);
2293 S_CLEAR_WEAKREFD (symbolS
*s
)
2295 if (LOCAL_SYMBOL_CHECK (s
))
2300 /* If a weakref target symbol is weak, then it was never
2301 referenced directly before, not even in a .global directive,
2302 so decay it to local. If it remains undefined, it will be
2303 later turned into a global, like any other undefined
2305 if (s
->bsym
->flags
& BSF_WEAK
)
2307 #ifdef obj_clear_weak_hook
2308 obj_clear_weak_hook (s
);
2310 s
->bsym
->flags
&= ~BSF_WEAK
;
2311 s
->bsym
->flags
|= BSF_LOCAL
;
2317 S_SET_THREAD_LOCAL (symbolS
*s
)
2319 if (LOCAL_SYMBOL_CHECK (s
))
2320 s
= local_symbol_convert ((struct local_symbol
*) s
);
2321 if (bfd_is_com_section (s
->bsym
->section
)
2322 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2324 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2325 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2326 as_bad (_("Accessing function `%s' as thread-local object"),
2328 else if (! bfd_is_und_section (s
->bsym
->section
)
2329 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2330 as_bad (_("Accessing `%s' as thread-local object"),
2335 S_SET_NAME (symbolS
*s
, const char *name
)
2337 if (LOCAL_SYMBOL_CHECK (s
))
2339 ((struct local_symbol
*) s
)->lsy_name
= name
;
2342 s
->bsym
->name
= name
;
2346 S_SET_VOLATILE (symbolS
*s
)
2348 if (LOCAL_SYMBOL_CHECK (s
))
2349 s
= local_symbol_convert ((struct local_symbol
*) s
);
2354 S_CLEAR_VOLATILE (symbolS
*s
)
2356 if (!LOCAL_SYMBOL_CHECK (s
))
2361 S_SET_FORWARD_REF (symbolS
*s
)
2363 if (LOCAL_SYMBOL_CHECK (s
))
2364 s
= local_symbol_convert ((struct local_symbol
*) s
);
2365 s
->sy_forward_ref
= 1;
2368 /* Return the previous symbol in a chain. */
2371 symbol_previous (symbolS
*s
)
2373 if (LOCAL_SYMBOL_CHECK (s
))
2375 return s
->sy_previous
;
2378 /* Return the next symbol in a chain. */
2381 symbol_next (symbolS
*s
)
2383 if (LOCAL_SYMBOL_CHECK (s
))
2388 /* Return a pointer to the value of a symbol as an expression. */
2391 symbol_get_value_expression (symbolS
*s
)
2393 if (LOCAL_SYMBOL_CHECK (s
))
2394 s
= local_symbol_convert ((struct local_symbol
*) s
);
2395 return &s
->sy_value
;
2398 /* Set the value of a symbol to an expression. */
2401 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2403 if (LOCAL_SYMBOL_CHECK (s
))
2404 s
= local_symbol_convert ((struct local_symbol
*) s
);
2406 S_CLEAR_WEAKREFR (s
);
2409 /* Return whether 2 symbols are the same. */
2412 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2414 if (s1
->bsym
== NULL
2415 && local_symbol_converted_p ((struct local_symbol
*) s1
))
2416 s1
= local_symbol_get_real_symbol ((struct local_symbol
*) s1
);
2417 if (s2
->bsym
== NULL
2418 && local_symbol_converted_p ((struct local_symbol
*) s2
))
2419 s2
= local_symbol_get_real_symbol ((struct local_symbol
*) s2
);
2423 /* Return a pointer to the X_add_number component of a symbol. */
2426 symbol_X_add_number (symbolS
*s
)
2428 if (LOCAL_SYMBOL_CHECK (s
))
2429 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
2431 return &s
->sy_value
.X_add_number
;
2434 /* Set the value of SYM to the current position in the current segment. */
2437 symbol_set_value_now (symbolS
*sym
)
2439 S_SET_SEGMENT (sym
, now_seg
);
2440 S_SET_VALUE (sym
, frag_now_fix ());
2441 symbol_set_frag (sym
, frag_now
);
2444 /* Set the frag of a symbol. */
2447 symbol_set_frag (symbolS
*s
, fragS
*f
)
2449 if (LOCAL_SYMBOL_CHECK (s
))
2451 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2455 S_CLEAR_WEAKREFR (s
);
2458 /* Return the frag of a symbol. */
2461 symbol_get_frag (symbolS
*s
)
2463 if (LOCAL_SYMBOL_CHECK (s
))
2464 return local_symbol_get_frag ((struct local_symbol
*) s
);
2468 /* Mark a symbol as having been used. */
2471 symbol_mark_used (symbolS
*s
)
2473 if (LOCAL_SYMBOL_CHECK (s
))
2476 if (S_IS_WEAKREFR (s
))
2477 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2480 /* Clear the mark of whether a symbol has been used. */
2483 symbol_clear_used (symbolS
*s
)
2485 if (LOCAL_SYMBOL_CHECK (s
))
2486 s
= local_symbol_convert ((struct local_symbol
*) s
);
2490 /* Return whether a symbol has been used. */
2493 symbol_used_p (symbolS
*s
)
2495 if (LOCAL_SYMBOL_CHECK (s
))
2500 /* Mark a symbol as having been used in a reloc. */
2503 symbol_mark_used_in_reloc (symbolS
*s
)
2505 if (LOCAL_SYMBOL_CHECK (s
))
2506 s
= local_symbol_convert ((struct local_symbol
*) s
);
2507 s
->sy_used_in_reloc
= 1;
2510 /* Clear the mark of whether a symbol has been used in a reloc. */
2513 symbol_clear_used_in_reloc (symbolS
*s
)
2515 if (LOCAL_SYMBOL_CHECK (s
))
2517 s
->sy_used_in_reloc
= 0;
2520 /* Return whether a symbol has been used in a reloc. */
2523 symbol_used_in_reloc_p (symbolS
*s
)
2525 if (LOCAL_SYMBOL_CHECK (s
))
2527 return s
->sy_used_in_reloc
;
2530 /* Mark a symbol as an MRI common symbol. */
2533 symbol_mark_mri_common (symbolS
*s
)
2535 if (LOCAL_SYMBOL_CHECK (s
))
2536 s
= local_symbol_convert ((struct local_symbol
*) s
);
2537 s
->sy_mri_common
= 1;
2540 /* Clear the mark of whether a symbol is an MRI common symbol. */
2543 symbol_clear_mri_common (symbolS
*s
)
2545 if (LOCAL_SYMBOL_CHECK (s
))
2547 s
->sy_mri_common
= 0;
2550 /* Return whether a symbol is an MRI common symbol. */
2553 symbol_mri_common_p (symbolS
*s
)
2555 if (LOCAL_SYMBOL_CHECK (s
))
2557 return s
->sy_mri_common
;
2560 /* Mark a symbol as having been written. */
2563 symbol_mark_written (symbolS
*s
)
2565 if (LOCAL_SYMBOL_CHECK (s
))
2570 /* Clear the mark of whether a symbol has been written. */
2573 symbol_clear_written (symbolS
*s
)
2575 if (LOCAL_SYMBOL_CHECK (s
))
2580 /* Return whether a symbol has been written. */
2583 symbol_written_p (symbolS
*s
)
2585 if (LOCAL_SYMBOL_CHECK (s
))
2590 /* Mark a symbol has having been resolved. */
2593 symbol_mark_resolved (symbolS
*s
)
2595 if (LOCAL_SYMBOL_CHECK (s
))
2597 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2603 /* Return whether a symbol has been resolved. */
2606 symbol_resolved_p (symbolS
*s
)
2608 if (LOCAL_SYMBOL_CHECK (s
))
2609 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2610 return s
->sy_resolved
;
2613 /* Return whether a symbol is a section symbol. */
2616 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2618 if (LOCAL_SYMBOL_CHECK (s
))
2620 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2623 /* Return whether a symbol is equated to another symbol. */
2626 symbol_equated_p (symbolS
*s
)
2628 if (LOCAL_SYMBOL_CHECK (s
))
2630 return s
->sy_value
.X_op
== O_symbol
;
2633 /* Return whether a symbol is equated to another symbol, and should be
2634 treated specially when writing out relocs. */
2637 symbol_equated_reloc_p (symbolS
*s
)
2639 if (LOCAL_SYMBOL_CHECK (s
))
2641 /* X_op_symbol, normally not used for O_symbol, is set by
2642 resolve_symbol_value to flag expression syms that have been
2644 return (s
->sy_value
.X_op
== O_symbol
2645 #if defined (OBJ_COFF) && defined (TE_PE)
2648 && ((s
->sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2649 || ! S_IS_DEFINED (s
)
2650 || S_IS_COMMON (s
)));
2653 /* Return whether a symbol has a constant value. */
2656 symbol_constant_p (symbolS
*s
)
2658 if (LOCAL_SYMBOL_CHECK (s
))
2660 return s
->sy_value
.X_op
== O_constant
;
2663 /* Return whether a symbol was cloned and thus removed from the global
2667 symbol_shadow_p (symbolS
*s
)
2669 if (LOCAL_SYMBOL_CHECK (s
))
2671 return s
->sy_next
== s
;
2674 /* Return the BFD symbol for a symbol. */
2677 symbol_get_bfdsym (symbolS
*s
)
2679 if (LOCAL_SYMBOL_CHECK (s
))
2680 s
= local_symbol_convert ((struct local_symbol
*) s
);
2684 /* Set the BFD symbol for a symbol. */
2687 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2689 if (LOCAL_SYMBOL_CHECK (s
))
2690 s
= local_symbol_convert ((struct local_symbol
*) s
);
2691 /* Usually, it is harmless to reset a symbol to a BFD section
2692 symbol. For example, obj_elf_change_section sets the BFD symbol
2693 of an old symbol with the newly created section symbol. But when
2694 we have multiple sections with the same name, the newly created
2695 section may have the same name as an old section. We check if the
2696 old symbol has been already marked as a section symbol before
2698 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2700 /* else XXX - What do we do now ? */
2703 #ifdef OBJ_SYMFIELD_TYPE
2705 /* Get a pointer to the object format information for a symbol. */
2708 symbol_get_obj (symbolS
*s
)
2710 if (LOCAL_SYMBOL_CHECK (s
))
2711 s
= local_symbol_convert ((struct local_symbol
*) s
);
2715 /* Set the object format information for a symbol. */
2718 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2720 if (LOCAL_SYMBOL_CHECK (s
))
2721 s
= local_symbol_convert ((struct local_symbol
*) s
);
2725 #endif /* OBJ_SYMFIELD_TYPE */
2727 #ifdef TC_SYMFIELD_TYPE
2729 /* Get a pointer to the processor information for a symbol. */
2732 symbol_get_tc (symbolS
*s
)
2734 if (LOCAL_SYMBOL_CHECK (s
))
2735 s
= local_symbol_convert ((struct local_symbol
*) s
);
2739 /* Set the processor information for a symbol. */
2742 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2744 if (LOCAL_SYMBOL_CHECK (s
))
2745 s
= local_symbol_convert ((struct local_symbol
*) s
);
2749 #endif /* TC_SYMFIELD_TYPE */
2754 symbol_lastP
= NULL
;
2755 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2756 sy_hash
= hash_new ();
2757 local_hash
= hash_new ();
2759 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2760 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2761 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
2763 abs_symbol
.sy_value
.X_op
= O_constant
;
2764 abs_symbol
.sy_frag
= &zero_address_frag
;
2766 if (LOCAL_LABELS_FB
)
2771 dot_symbol_init (void)
2773 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
2774 if (dot_symbol
.bsym
== NULL
)
2775 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2776 dot_symbol
.bsym
->name
= ".";
2777 dot_symbol
.sy_forward_ref
= 1;
2778 dot_symbol
.sy_value
.X_op
= O_constant
;
2783 /* Maximum indent level.
2784 Available for modification inside a gdb session. */
2785 static int max_indent_level
= 8;
2788 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2790 const char *name
= S_GET_NAME (sym
);
2791 if (!name
|| !name
[0])
2793 fprintf (file
, "sym ");
2794 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
2795 fprintf (file
, " %s", name
);
2797 if (LOCAL_SYMBOL_CHECK (sym
))
2799 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2801 if (local_symbol_get_frag (locsym
) != & zero_address_frag
2802 && local_symbol_get_frag (locsym
) != NULL
)
2804 fprintf (file
, " frag ");
2805 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) local_symbol_get_frag (locsym
)));
2807 if (local_symbol_resolved_p (locsym
))
2808 fprintf (file
, " resolved");
2809 fprintf (file
, " local");
2813 if (sym
->sy_frag
!= &zero_address_frag
)
2815 fprintf (file
, " frag ");
2816 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->sy_frag
));
2819 fprintf (file
, " written");
2820 if (sym
->sy_resolved
)
2821 fprintf (file
, " resolved");
2822 else if (sym
->sy_resolving
)
2823 fprintf (file
, " resolving");
2824 if (sym
->sy_used_in_reloc
)
2825 fprintf (file
, " used-in-reloc");
2827 fprintf (file
, " used");
2828 if (S_IS_LOCAL (sym
))
2829 fprintf (file
, " local");
2830 if (S_IS_EXTERNAL (sym
))
2831 fprintf (file
, " extern");
2832 if (S_IS_WEAK (sym
))
2833 fprintf (file
, " weak");
2834 if (S_IS_DEBUG (sym
))
2835 fprintf (file
, " debug");
2836 if (S_IS_DEFINED (sym
))
2837 fprintf (file
, " defined");
2839 if (S_IS_WEAKREFR (sym
))
2840 fprintf (file
, " weakrefr");
2841 if (S_IS_WEAKREFD (sym
))
2842 fprintf (file
, " weakrefd");
2843 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2844 if (symbol_resolved_p (sym
))
2846 segT s
= S_GET_SEGMENT (sym
);
2848 if (s
!= undefined_section
2849 && s
!= expr_section
)
2850 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
2852 else if (indent_level
< max_indent_level
2853 && S_GET_SEGMENT (sym
) != undefined_section
)
2856 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2857 if (LOCAL_SYMBOL_CHECK (sym
))
2858 fprintf (file
, "constant %lx",
2859 (unsigned long) ((struct local_symbol
*) sym
)->lsy_value
);
2861 print_expr_1 (file
, &sym
->sy_value
);
2862 fprintf (file
, ">");
2869 print_symbol_value (symbolS
*sym
)
2872 print_symbol_value_1 (stderr
, sym
);
2873 fprintf (stderr
, "\n");
2877 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2880 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2881 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2882 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2883 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2884 fprintf (file
, ">");
2889 print_expr_1 (FILE *file
, expressionS
*exp
)
2891 fprintf (file
, "expr ");
2892 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
2893 fprintf (file
, " ");
2897 fprintf (file
, "illegal");
2900 fprintf (file
, "absent");
2903 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
2907 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2908 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2909 fprintf (file
, ">");
2911 if (exp
->X_add_number
)
2912 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2913 (unsigned long) exp
->X_add_number
);
2917 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2920 fprintf (file
, "big");
2923 fprintf (file
, "uminus -<");
2925 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2926 fprintf (file
, ">");
2927 goto maybe_print_addnum
;
2929 fprintf (file
, "bit_not");
2932 print_binary (file
, "multiply", exp
);
2935 print_binary (file
, "divide", exp
);
2938 print_binary (file
, "modulus", exp
);
2941 print_binary (file
, "lshift", exp
);
2944 print_binary (file
, "rshift", exp
);
2946 case O_bit_inclusive_or
:
2947 print_binary (file
, "bit_ior", exp
);
2949 case O_bit_exclusive_or
:
2950 print_binary (file
, "bit_xor", exp
);
2953 print_binary (file
, "bit_and", exp
);
2956 print_binary (file
, "eq", exp
);
2959 print_binary (file
, "ne", exp
);
2962 print_binary (file
, "lt", exp
);
2965 print_binary (file
, "le", exp
);
2968 print_binary (file
, "ge", exp
);
2971 print_binary (file
, "gt", exp
);
2974 print_binary (file
, "logical_and", exp
);
2977 print_binary (file
, "logical_or", exp
);
2981 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
2982 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2983 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2984 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2985 fprintf (file
, ">");
2986 goto maybe_print_addnum
;
2989 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
2990 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2991 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2992 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2993 fprintf (file
, ">");
2994 goto maybe_print_addnum
;
2996 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3003 print_expr (expressionS
*exp
)
3005 print_expr_1 (stderr
, exp
);
3006 fprintf (stderr
, "\n");
3010 symbol_print_statistics (FILE *file
)
3012 hash_print_statistics (file
, "symbol table", sy_hash
);
3013 hash_print_statistics (file
, "mini local symbol table", local_hash
);
3014 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3015 local_symbol_count
, local_symbol_conversion_count
);
3018 #ifdef OBJ_COMPLEX_RELC
3020 /* Convert given symbol to a new complex-relocation symbol name. This
3021 may be a recursive function, since it might be called for non-leaf
3022 nodes (plain symbols) in the expression tree. The caller owns the
3023 returning string, so should free it eventually. Errors are
3024 indicated via as_bad and a NULL return value. The given symbol
3025 is marked with sy_used_in_reloc. */
3028 symbol_relc_make_sym (symbolS
* sym
)
3030 char * terminal
= NULL
;
3035 gas_assert (sym
!= NULL
);
3037 /* Recurse to symbol_relc_make_expr if this symbol
3038 is defined as an expression or a plain value. */
3039 if ( S_GET_SEGMENT (sym
) == expr_section
3040 || S_GET_SEGMENT (sym
) == absolute_section
)
3041 return symbol_relc_make_expr (& sym
->sy_value
);
3043 /* This may be a "fake symbol" L0\001, referring to ".".
3044 Write out a special null symbol to refer to this position. */
3045 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3046 return xstrdup (".");
3048 /* We hope this is a plain leaf symbol. Construct the encoding
3049 as {S,s}II...:CCCCCCC....
3050 where 'S'/'s' means section symbol / plain symbol
3051 III is decimal for the symbol name length
3052 CCC is the symbol name itself. */
3053 symbol_mark_used_in_reloc (sym
);
3055 sname
= S_GET_NAME (sym
);
3056 sname_len
= strlen (sname
);
3057 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3059 terminal
= xmalloc (1 /* S or s */
3060 + 8 /* sname_len in decimal */
3062 + sname_len
/* name itself */
3065 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3069 /* Convert given value to a new complex-relocation symbol name. This
3070 is a non-recursive function, since it is be called for leaf nodes
3071 (plain values) in the expression tree. The caller owns the
3072 returning string, so should free() it eventually. No errors. */
3075 symbol_relc_make_value (offsetT val
)
3077 char * terminal
= xmalloc (28); /* Enough for long long. */
3080 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3084 /* Convert given expression to a new complex-relocation symbol name.
3085 This is a recursive function, since it traverses the entire given
3086 expression tree. The caller owns the returning string, so should
3087 free() it eventually. Errors are indicated via as_bad() and a NULL
3091 symbol_relc_make_expr (expressionS
* exp
)
3093 char * opstr
= NULL
; /* Operator prefix string. */
3094 int arity
= 0; /* Arity of this operator. */
3095 char * operands
[3]; /* Up to three operands. */
3096 char * concat_string
= NULL
;
3098 operands
[0] = operands
[1] = operands
[2] = NULL
;
3100 gas_assert (exp
!= NULL
);
3102 /* Match known operators -> fill in opstr, arity, operands[] and fall
3103 through to construct subexpression fragments; may instead return
3104 string directly for leaf nodes. */
3106 /* See expr.h for the meaning of all these enums. Many operators
3107 have an unnatural arity (X_add_number implicitly added). The
3108 conversion logic expands them to explicit "+" subexpressions. */
3113 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3118 return symbol_relc_make_value (exp
->X_add_number
);
3121 if (exp
->X_add_number
)
3125 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3126 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3130 return symbol_relc_make_sym (exp
->X_add_symbol
);
3132 /* Helper macros for nesting nodes. */
3134 #define HANDLE_XADD_OPT1(str_) \
3135 if (exp->X_add_number) \
3138 opstr = "+:" str_; \
3139 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3140 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3147 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3151 #define HANDLE_XADD_OPT2(str_) \
3152 if (exp->X_add_number) \
3155 opstr = "+:" str_; \
3156 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3157 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3158 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3164 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3165 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3169 /* Nesting nodes. */
3171 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3172 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3173 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3174 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3175 case O_divide
: HANDLE_XADD_OPT2 ("/");
3176 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3177 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3178 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3179 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3180 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3181 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3182 case O_add
: HANDLE_XADD_OPT2 ("+");
3183 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3184 case O_eq
: HANDLE_XADD_OPT2 ("==");
3185 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3186 case O_lt
: HANDLE_XADD_OPT2 ("<");
3187 case O_le
: HANDLE_XADD_OPT2 ("<=");
3188 case O_ge
: HANDLE_XADD_OPT2 (">=");
3189 case O_gt
: HANDLE_XADD_OPT2 (">");
3190 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3191 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3194 /* Validate & reject early. */
3195 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3197 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3199 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3203 concat_string
= NULL
;
3206 /* Allocate new string; include inter-operand padding gaps etc. */
3207 concat_string
= xmalloc (strlen (opstr
)
3209 + (arity
>= 1 ? (strlen (operands
[0]) + 1 ) : 0)
3210 + (arity
>= 2 ? (strlen (operands
[1]) + 1 ) : 0)
3211 + (arity
>= 3 ? (strlen (operands
[2]) + 0 ) : 0)
3213 gas_assert (concat_string
!= NULL
);
3215 /* Format the thing. */
3216 sprintf (concat_string
,
3217 (arity
== 0 ? "%s" :
3218 arity
== 1 ? "%s:%s" :
3219 arity
== 2 ? "%s:%s:%s" :
3220 /* arity == 3 */ "%s:%s:%s:%s"),
3221 opstr
, operands
[0], operands
[1], operands
[2]);
3224 /* Free operand strings (not opstr). */
3225 if (arity
>= 1) xfree (operands
[0]);
3226 if (arity
>= 2) xfree (operands
[1]);
3227 if (arity
>= 3) xfree (operands
[2]);
3229 return concat_string
;