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
4 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 2, 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
;
53 #define debug_verify_symchain verify_symbol_chain
55 #define debug_verify_symchain(root, last) ((void) 0)
58 #define DOLLAR_LABEL_CHAR '\001'
59 #define LOCAL_LABEL_CHAR '\002'
63 /* The name of an external symbol which is
64 used to make weak PE symbol names unique. */
65 const char * an_external_name
;
68 static char *save_symbol_name (const char *);
69 static void fb_label_init (void);
70 static long dollar_label_instance (long);
71 static long fb_label_instance (long);
73 static void print_binary (FILE *, const char *, expressionS
*);
74 static void report_op_error (symbolS
*, symbolS
*, symbolS
*);
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
= 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_perror ("%s", "bfd_make_empty_symbol");
148 symbolP
->bsym
->udata
.p
= (PTR
) symbolP
;
149 S_SET_NAME (symbolP
, preserved_copy_of_name
);
151 S_SET_SEGMENT (symbolP
, segment
);
152 S_SET_VALUE (symbolP
, valu
);
153 symbol_clear_list_pointers (symbolP
);
155 symbolP
->sy_frag
= frag
;
157 obj_symbol_new_hook (symbolP
);
159 #ifdef tc_symbol_new_hook
160 tc_symbol_new_hook (symbolP
);
167 /* Local symbol support. If we can get away with it, we keep only a
168 small amount of information for local symbols. */
170 static symbolS
*local_symbol_convert (struct local_symbol
*);
172 /* Used for statistics. */
174 static unsigned long local_symbol_count
;
175 static unsigned long local_symbol_conversion_count
;
177 /* This macro is called with a symbol argument passed by reference.
178 It returns whether this is a local symbol. If necessary, it
179 changes its argument to the real symbol. */
181 #define LOCAL_SYMBOL_CHECK(s) \
183 ? (local_symbol_converted_p ((struct local_symbol *) s) \
184 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
189 /* Create a local symbol and insert it into the local hash table. */
191 static struct local_symbol
*
192 local_symbol_make (const char *name
, segT section
, valueT value
, fragS
*frag
)
195 struct local_symbol
*ret
;
197 ++local_symbol_count
;
199 name_copy
= save_symbol_name (name
);
201 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
202 ret
->lsy_marker
= NULL
;
203 ret
->lsy_name
= name_copy
;
204 ret
->lsy_section
= section
;
205 local_symbol_set_frag (ret
, frag
);
206 ret
->lsy_value
= value
;
208 hash_jam (local_hash
, name_copy
, (PTR
) ret
);
213 /* Convert a local symbol into a real symbol. Note that we do not
214 reclaim the space used by the local symbol. */
217 local_symbol_convert (struct local_symbol
*locsym
)
221 assert (locsym
->lsy_marker
== NULL
);
222 if (local_symbol_converted_p (locsym
))
223 return local_symbol_get_real_symbol (locsym
);
225 ++local_symbol_conversion_count
;
227 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
228 local_symbol_get_frag (locsym
));
230 if (local_symbol_resolved_p (locsym
))
231 ret
->sy_resolved
= 1;
233 /* Local symbols are always either defined or used. */
236 #ifdef TC_LOCAL_SYMFIELD_CONVERT
237 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
240 symbol_table_insert (ret
);
242 local_symbol_mark_converted (locsym
);
243 local_symbol_set_real_symbol (locsym
, ret
);
245 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
250 /* We have just seen "<name>:".
251 Creates a struct symbol unless it already exists.
253 Gripes if we are redefining a symbol incompatibly (and ignores it). */
256 colon (/* Just seen "x:" - rattle symbols & frags. */
257 const char *sym_name
/* Symbol name, as a cannonical string. */
258 /* We copy this string: OK to alter later. */)
260 register symbolS
*symbolP
; /* Symbol we are working with. */
262 /* Sun local labels go out of scope whenever a non-local symbol is
264 if (LOCAL_LABELS_DOLLAR
265 && !bfd_is_local_label_name (stdoutput
, sym_name
))
266 dollar_label_clear ();
268 #ifndef WORKING_DOT_WORD
269 if (new_broken_words
)
271 struct broken_word
*a
;
276 if (now_seg
== absolute_section
)
278 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
282 possible_bytes
= (md_short_jump_size
283 + new_broken_words
* md_long_jump_size
);
286 frag_opcode
= frag_var (rs_broken_word
,
290 (symbolS
*) broken_words
,
294 /* We want to store the pointer to where to insert the jump
295 table in the fr_opcode of the rs_broken_word frag. This
296 requires a little hackery. */
298 && (frag_tmp
->fr_type
!= rs_broken_word
299 || frag_tmp
->fr_opcode
))
300 frag_tmp
= frag_tmp
->fr_next
;
302 frag_tmp
->fr_opcode
= frag_opcode
;
303 new_broken_words
= 0;
305 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
306 a
->dispfrag
= frag_tmp
;
308 #endif /* WORKING_DOT_WORD */
310 if ((symbolP
= symbol_find (sym_name
)) != 0)
312 #ifdef RESOLVE_SYMBOL_REDEFINITION
313 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
316 /* Now check for undefined symbols. */
317 if (LOCAL_SYMBOL_CHECK (symbolP
))
319 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
321 if (locsym
->lsy_section
!= undefined_section
322 && (local_symbol_get_frag (locsym
) != frag_now
323 || locsym
->lsy_section
!= now_seg
324 || locsym
->lsy_value
!= frag_now_fix ()))
326 as_bad (_("symbol `%s' is already defined"), sym_name
);
330 locsym
->lsy_section
= now_seg
;
331 local_symbol_set_frag (locsym
, frag_now
);
332 locsym
->lsy_value
= frag_now_fix ();
334 else if (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
336 if (S_GET_VALUE (symbolP
) == 0)
338 symbolP
->sy_frag
= frag_now
;
340 S_SET_OTHER (symbolP
, const_flag
);
342 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
343 S_SET_SEGMENT (symbolP
, now_seg
);
346 #endif /* if we have one, it better be zero. */
351 /* There are still several cases to check:
353 A .comm/.lcomm symbol being redefined as initialized
356 A .comm/.lcomm symbol being redefined with a larger
359 This only used to be allowed on VMS gas, but Sun cc
360 on the sparc also depends on it. */
362 if (((!S_IS_DEBUG (symbolP
)
363 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
364 && S_IS_EXTERNAL (symbolP
))
365 || S_GET_SEGMENT (symbolP
) == bss_section
)
366 && (now_seg
== data_section
367 || now_seg
== S_GET_SEGMENT (symbolP
)))
369 /* Select which of the 2 cases this is. */
370 if (now_seg
!= data_section
)
372 /* New .comm for prev .comm symbol.
374 If the new size is larger we just change its
375 value. If the new size is smaller, we ignore
377 if (S_GET_VALUE (symbolP
)
378 < ((unsigned) frag_now_fix ()))
380 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
385 /* It is a .comm/.lcomm being converted to initialized
387 symbolP
->sy_frag
= frag_now
;
389 S_SET_OTHER (symbolP
, const_flag
);
391 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
392 S_SET_SEGMENT (symbolP
, now_seg
); /* Keep N_EXT bit. */
397 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
398 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
399 static const char *od_buf
= "";
403 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
404 sprintf (od_buf
, "%d.%d.",
405 S_GET_OTHER (symbolP
),
406 S_GET_DESC (symbolP
));
408 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
410 segment_name (S_GET_SEGMENT (symbolP
)),
412 (long) S_GET_VALUE (symbolP
));
414 } /* if the undefined symbol has no value */
418 /* Don't blow up if the definition is the same. */
419 if (!(frag_now
== symbolP
->sy_frag
420 && S_GET_VALUE (symbolP
) == frag_now_fix ()
421 && S_GET_SEGMENT (symbolP
) == now_seg
))
422 as_bad (_("symbol `%s' is already defined"), sym_name
);
426 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
428 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
429 (valueT
) frag_now_fix (),
434 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
437 S_SET_OTHER (symbolP
, const_flag
);
440 symbol_table_insert (symbolP
);
443 if (mri_common_symbol
!= NULL
)
445 /* This symbol is actually being defined within an MRI common
446 section. This requires special handling. */
447 if (LOCAL_SYMBOL_CHECK (symbolP
))
448 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
449 symbolP
->sy_value
.X_op
= O_symbol
;
450 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
451 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
452 symbolP
->sy_frag
= &zero_address_frag
;
453 S_SET_SEGMENT (symbolP
, expr_section
);
454 symbolP
->sy_mri_common
= 1;
458 tc_frob_label (symbolP
);
460 #ifdef obj_frob_label
461 obj_frob_label (symbolP
);
467 /* Die if we can't insert the symbol. */
470 symbol_table_insert (symbolS
*symbolP
)
472 register const char *error_string
;
475 know (S_GET_NAME (symbolP
));
477 if (LOCAL_SYMBOL_CHECK (symbolP
))
479 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
481 if (error_string
!= NULL
)
482 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
483 S_GET_NAME (symbolP
), error_string
);
487 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
489 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
490 S_GET_NAME (symbolP
), error_string
);
494 /* If a symbol name does not exist, create it as undefined, and insert
495 it into the symbol table. Return a pointer to it. */
498 symbol_find_or_make (const char *name
)
500 register symbolS
*symbolP
;
502 symbolP
= symbol_find (name
);
506 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
508 symbolP
= md_undefined_symbol ((char *) name
);
512 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
518 symbolP
= symbol_make (name
);
520 symbol_table_insert (symbolP
);
521 } /* if symbol wasn't found */
527 symbol_make (const char *name
)
531 /* Let the machine description default it, e.g. for register names. */
532 symbolP
= md_undefined_symbol ((char *) name
);
535 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
541 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
543 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
547 symbol_temp_new_now (void)
549 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
553 symbol_temp_make (void)
555 return symbol_make (FAKE_LABEL_NAME
);
558 /* Implement symbol table lookup.
559 In: A symbol's name as a string: '\0' can't be part of a symbol name.
560 Out: NULL if the name was not in the symbol table, else the address
561 of a struct symbol associated with that name. */
564 symbol_find_exact (const char *name
)
566 struct local_symbol
*locsym
;
568 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
570 return (symbolS
*) locsym
;
572 return ((symbolS
*) hash_find (sy_hash
, name
));
576 symbol_find (const char *name
)
578 #ifdef tc_canonicalize_symbol_name
581 size_t len
= strlen (name
) + 1;
583 copy
= (char *) alloca (len
);
584 memcpy (copy
, name
, len
);
585 name
= tc_canonicalize_symbol_name (copy
);
589 if (! symbols_case_sensitive
)
596 name
= copy
= (char *) alloca (strlen (name
) + 1);
598 while ((c
= *orig
++) != '\0')
600 *copy
++ = TOUPPER (c
);
605 return symbol_find_exact (name
);
608 /* Once upon a time, symbols were kept in a singly linked list. At
609 least coff needs to be able to rearrange them from time to time, for
610 which a doubly linked list is much more convenient. Loic did these
611 as macros which seemed dangerous to me so they're now functions.
614 /* Link symbol ADDME after symbol TARGET in the chain. */
617 symbol_append (symbolS
*addme
, symbolS
*target
,
618 symbolS
**rootPP
, symbolS
**lastPP
)
620 if (LOCAL_SYMBOL_CHECK (addme
))
622 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
627 know (*rootPP
== NULL
);
628 know (*lastPP
== NULL
);
629 addme
->sy_next
= NULL
;
630 addme
->sy_previous
= NULL
;
634 } /* if the list is empty */
636 if (target
->sy_next
!= NULL
)
638 target
->sy_next
->sy_previous
= addme
;
642 know (*lastPP
== target
);
644 } /* if we have a next */
646 addme
->sy_next
= target
->sy_next
;
647 target
->sy_next
= addme
;
648 addme
->sy_previous
= target
;
650 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
653 /* Set the chain pointers of SYMBOL to null. */
656 symbol_clear_list_pointers (symbolS
*symbolP
)
658 if (LOCAL_SYMBOL_CHECK (symbolP
))
660 symbolP
->sy_next
= NULL
;
661 symbolP
->sy_previous
= NULL
;
664 /* Remove SYMBOLP from the list. */
667 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
669 if (LOCAL_SYMBOL_CHECK (symbolP
))
672 if (symbolP
== *rootPP
)
674 *rootPP
= symbolP
->sy_next
;
675 } /* if it was the root */
677 if (symbolP
== *lastPP
)
679 *lastPP
= symbolP
->sy_previous
;
680 } /* if it was the tail */
682 if (symbolP
->sy_next
!= NULL
)
684 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
687 if (symbolP
->sy_previous
!= NULL
)
689 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
692 debug_verify_symchain (*rootPP
, *lastPP
);
695 /* Link symbol ADDME before symbol TARGET in the chain. */
698 symbol_insert (symbolS
*addme
, symbolS
*target
,
699 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
701 if (LOCAL_SYMBOL_CHECK (addme
))
703 if (LOCAL_SYMBOL_CHECK (target
))
706 if (target
->sy_previous
!= NULL
)
708 target
->sy_previous
->sy_next
= addme
;
712 know (*rootPP
== target
);
716 addme
->sy_previous
= target
->sy_previous
;
717 target
->sy_previous
= addme
;
718 addme
->sy_next
= target
;
720 debug_verify_symchain (*rootPP
, *lastPP
);
724 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
726 symbolS
*symbolP
= rootP
;
731 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
733 assert (symbolP
->bsym
!= NULL
);
734 assert (symbolP
->sy_next
->sy_previous
== symbolP
);
737 assert (lastP
== symbolP
);
741 report_op_error (symbolS
*symp
, symbolS
*left
, symbolS
*right
)
745 segT seg_left
= S_GET_SEGMENT (left
);
746 segT seg_right
= right
? S_GET_SEGMENT (right
) : 0;
748 if (expr_symbol_where (symp
, &file
, &line
))
750 if (seg_left
== undefined_section
)
751 as_bad_where (file
, line
,
752 _("undefined symbol `%s' in operation"),
754 if (seg_right
== undefined_section
)
755 as_bad_where (file
, line
,
756 _("undefined symbol `%s' in operation"),
758 if (seg_left
!= undefined_section
759 && seg_right
!= undefined_section
)
762 as_bad_where (file
, line
,
763 _("invalid sections for operation on `%s' and `%s'"),
764 S_GET_NAME (left
), S_GET_NAME (right
));
766 as_bad_where (file
, line
,
767 _("invalid section for operation on `%s'"),
774 if (seg_left
== undefined_section
)
775 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
776 S_GET_NAME (left
), S_GET_NAME (symp
));
777 if (seg_right
== undefined_section
)
778 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
779 S_GET_NAME (right
), S_GET_NAME (symp
));
780 if (seg_left
!= undefined_section
781 && seg_right
!= undefined_section
)
784 as_bad_where (file
, line
,
785 _("invalid sections for operation on `%s' and `%s' setting `%s'"),
786 S_GET_NAME (left
), S_GET_NAME (right
), S_GET_NAME (symp
));
788 as_bad_where (file
, line
,
789 _("invalid section for operation on `%s' setting `%s'"),
790 S_GET_NAME (left
), S_GET_NAME (symp
));
795 /* Resolve the value of a symbol. This is called during the final
796 pass over the symbol table to resolve any symbols with complex
800 resolve_symbol_value (symbolS
*symp
)
803 valueT final_val
= 0;
806 if (LOCAL_SYMBOL_CHECK (symp
))
808 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
810 final_val
= locsym
->lsy_value
;
811 if (local_symbol_resolved_p (locsym
))
814 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
818 locsym
->lsy_value
= final_val
;
819 local_symbol_mark_resolved (locsym
);
825 if (symp
->sy_resolved
)
827 if (symp
->sy_value
.X_op
== O_constant
)
828 return (valueT
) symp
->sy_value
.X_add_number
;
834 final_seg
= S_GET_SEGMENT (symp
);
836 if (symp
->sy_resolving
)
839 as_bad (_("symbol definition loop encountered at `%s'"),
846 symbolS
*add_symbol
, *op_symbol
;
848 segT seg_left
, seg_right
;
852 symp
->sy_resolving
= 1;
854 /* Help out with CSE. */
855 add_symbol
= symp
->sy_value
.X_add_symbol
;
856 op_symbol
= symp
->sy_value
.X_op_symbol
;
857 final_val
= symp
->sy_value
.X_add_number
;
858 op
= symp
->sy_value
.X_op
;
871 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
872 if (final_seg
== expr_section
)
873 final_seg
= absolute_section
;
879 left
= resolve_symbol_value (add_symbol
);
880 seg_left
= S_GET_SEGMENT (add_symbol
);
882 symp
->sy_value
.X_op_symbol
= NULL
;
885 if (symp
->sy_mri_common
)
887 /* This is a symbol inside an MRI common section. The
888 relocation routines are going to handle it specially.
889 Don't change the value. */
890 resolved
= symbol_resolved_p (add_symbol
);
894 if (finalize_syms
&& final_val
== 0)
896 if (LOCAL_SYMBOL_CHECK (add_symbol
))
897 add_symbol
= local_symbol_convert ((struct local_symbol
*)
899 copy_symbol_attributes (symp
, add_symbol
);
902 /* If we have equated this symbol to an undefined or common
903 symbol, keep X_op set to O_symbol, and don't change
904 X_add_number. This permits the routine which writes out
905 relocation to detect this case, and convert the
906 relocation to be against the symbol to which this symbol
908 if (! S_IS_DEFINED (add_symbol
)
909 #if defined (OBJ_COFF) && defined (TE_PE)
910 || S_IS_WEAK (add_symbol
)
912 || S_IS_COMMON (add_symbol
))
916 symp
->sy_value
.X_op
= O_symbol
;
917 symp
->sy_value
.X_add_symbol
= add_symbol
;
918 symp
->sy_value
.X_add_number
= final_val
;
919 /* Use X_op_symbol as a flag. */
920 symp
->sy_value
.X_op_symbol
= add_symbol
;
921 final_seg
= seg_left
;
924 resolved
= symbol_resolved_p (add_symbol
);
925 symp
->sy_resolving
= 0;
926 goto exit_dont_set_value
;
928 else if (finalize_syms
&& final_seg
== expr_section
929 && seg_left
!= expr_section
)
931 /* If the symbol is an expression symbol, do similarly
932 as for undefined and common syms above. Handles
933 "sym +/- expr" where "expr" cannot be evaluated
934 immediately, and we want relocations to be against
935 "sym", eg. because it is weak. */
936 symp
->sy_value
.X_op
= O_symbol
;
937 symp
->sy_value
.X_add_symbol
= add_symbol
;
938 symp
->sy_value
.X_add_number
= final_val
;
939 symp
->sy_value
.X_op_symbol
= add_symbol
;
940 final_seg
= seg_left
;
941 final_val
+= symp
->sy_frag
->fr_address
+ left
;
942 resolved
= symbol_resolved_p (add_symbol
);
943 symp
->sy_resolving
= 0;
944 goto exit_dont_set_value
;
948 final_val
+= symp
->sy_frag
->fr_address
+ left
;
949 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
950 final_seg
= seg_left
;
953 resolved
= symbol_resolved_p (add_symbol
);
959 left
= resolve_symbol_value (add_symbol
);
960 seg_left
= S_GET_SEGMENT (add_symbol
);
962 /* By reducing these to the relevant dyadic operator, we get
963 !S -> S == 0 permitted on anything,
964 -S -> 0 - S only permitted on absolute
965 ~S -> S ^ ~0 only permitted on absolute */
966 if (op
!= O_logical_not
&& seg_left
!= absolute_section
968 report_op_error (symp
, add_symbol
, NULL
);
970 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
971 final_seg
= absolute_section
;
975 else if (op
== O_logical_not
)
980 final_val
+= left
+ symp
->sy_frag
->fr_address
;
982 resolved
= symbol_resolved_p (add_symbol
);
990 case O_bit_inclusive_or
:
992 case O_bit_exclusive_or
:
1004 left
= resolve_symbol_value (add_symbol
);
1005 right
= resolve_symbol_value (op_symbol
);
1006 seg_left
= S_GET_SEGMENT (add_symbol
);
1007 seg_right
= S_GET_SEGMENT (op_symbol
);
1009 /* Simplify addition or subtraction of a constant by folding the
1010 constant into X_add_number. */
1013 if (seg_right
== absolute_section
)
1018 else if (seg_left
== absolute_section
)
1021 add_symbol
= op_symbol
;
1023 seg_left
= seg_right
;
1027 else if (op
== O_subtract
)
1029 if (seg_right
== absolute_section
)
1037 /* Equality and non-equality tests are permitted on anything.
1038 Subtraction, and other comparison operators are permitted if
1039 both operands are in the same section. Otherwise, both
1040 operands must be absolute. We already handled the case of
1041 addition or subtraction of a constant above. This will
1042 probably need to be changed for an object file format which
1043 supports arbitrary expressions, such as IEEE-695. */
1044 if (!(seg_left
== absolute_section
1045 && seg_right
== absolute_section
)
1046 && !(op
== O_eq
|| op
== O_ne
)
1047 && !((op
== O_subtract
1048 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1049 && seg_left
== seg_right
1050 && (seg_left
!= undefined_section
1051 || add_symbol
== op_symbol
)))
1053 /* Don't emit messages unless we're finalizing the symbol value,
1054 otherwise we may get the same message multiple times. */
1056 report_op_error (symp
, add_symbol
, op_symbol
);
1057 /* However do not move the symbol into the absolute section
1058 if it cannot currently be resolved - this would confuse
1059 other parts of the assembler into believing that the
1060 expression had been evaluated to zero. */
1066 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1067 final_seg
= absolute_section
;
1069 /* Check for division by zero. */
1070 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1072 /* If seg_right is not absolute_section, then we've
1073 already issued a warning about using a bad symbol. */
1074 if (seg_right
== absolute_section
&& finalize_syms
)
1079 if (expr_symbol_where (symp
, &file
, &line
))
1080 as_bad_where (file
, line
, _("division by zero"));
1082 as_bad (_("division by zero when setting `%s'"),
1089 switch (symp
->sy_value
.X_op
)
1091 case O_multiply
: left
*= right
; break;
1092 case O_divide
: left
/= right
; break;
1093 case O_modulus
: left
%= right
; break;
1094 case O_left_shift
: left
<<= right
; break;
1095 case O_right_shift
: left
>>= right
; break;
1096 case O_bit_inclusive_or
: left
|= right
; break;
1097 case O_bit_or_not
: left
|= ~right
; break;
1098 case O_bit_exclusive_or
: left
^= right
; break;
1099 case O_bit_and
: left
&= right
; break;
1100 case O_add
: left
+= right
; break;
1101 case O_subtract
: left
-= right
; break;
1104 left
= (left
== right
&& seg_left
== seg_right
1105 && (seg_left
!= undefined_section
1106 || add_symbol
== op_symbol
)
1107 ? ~ (offsetT
) 0 : 0);
1108 if (symp
->sy_value
.X_op
== O_ne
)
1111 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1112 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1113 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1114 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1115 case O_logical_and
: left
= left
&& right
; break;
1116 case O_logical_or
: left
= left
|| right
; break;
1120 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1121 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1123 if (seg_left
== undefined_section
1124 || seg_right
== undefined_section
)
1125 final_seg
= undefined_section
;
1126 else if (seg_left
== absolute_section
)
1127 final_seg
= seg_right
;
1129 final_seg
= seg_left
;
1131 resolved
= (symbol_resolved_p (add_symbol
)
1132 && symbol_resolved_p (op_symbol
));
1138 /* Give an error (below) if not in expr_section. We don't
1139 want to worry about expr_section symbols, because they
1140 are fictional (they are created as part of expression
1141 resolution), and any problems may not actually mean
1146 symp
->sy_resolving
= 0;
1150 S_SET_VALUE (symp
, final_val
);
1152 exit_dont_set_value
:
1153 /* Always set the segment, even if not finalizing the value.
1154 The segment is used to determine whether a symbol is defined. */
1155 S_SET_SEGMENT (symp
, final_seg
);
1157 /* Don't worry if we can't resolve an expr_section symbol. */
1161 symp
->sy_resolved
= 1;
1162 else if (S_GET_SEGMENT (symp
) != expr_section
)
1164 as_bad (_("can't resolve value for symbol `%s'"),
1166 symp
->sy_resolved
= 1;
1173 static void resolve_local_symbol (const char *, PTR
);
1175 /* A static function passed to hash_traverse. */
1178 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, PTR value
)
1181 resolve_symbol_value (value
);
1184 /* Resolve all local symbols. */
1187 resolve_local_symbol_values (void)
1189 hash_traverse (local_hash
, resolve_local_symbol
);
1192 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1193 They are *really* local. That is, they go out of scope whenever we see a
1194 label that isn't local. Also, like fb labels, there can be multiple
1195 instances of a dollar label. Therefor, we name encode each instance with
1196 the instance number, keep a list of defined symbols separate from the real
1197 symbol table, and we treat these buggers as a sparse array. */
1199 static long *dollar_labels
;
1200 static long *dollar_label_instances
;
1201 static char *dollar_label_defines
;
1202 static unsigned long dollar_label_count
;
1203 static unsigned long dollar_label_max
;
1206 dollar_label_defined (long label
)
1210 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1212 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1214 return dollar_label_defines
[i
- dollar_labels
];
1216 /* If we get here, label isn't defined. */
1221 dollar_label_instance (long label
)
1225 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1227 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1229 return (dollar_label_instances
[i
- dollar_labels
]);
1231 /* If we get here, we haven't seen the label before.
1232 Therefore its instance count is zero. */
1237 dollar_label_clear (void)
1239 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1242 #define DOLLAR_LABEL_BUMP_BY 10
1245 define_dollar_label (long label
)
1249 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1252 ++dollar_label_instances
[i
- dollar_labels
];
1253 dollar_label_defines
[i
- dollar_labels
] = 1;
1257 /* If we get to here, we don't have label listed yet. */
1259 if (dollar_labels
== NULL
)
1261 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1262 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1263 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
1264 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1265 dollar_label_count
= 0;
1267 else if (dollar_label_count
== dollar_label_max
)
1269 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1270 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
1271 dollar_label_max
* sizeof (long));
1272 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
1273 dollar_label_max
* sizeof (long));
1274 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
1275 } /* if we needed to grow */
1277 dollar_labels
[dollar_label_count
] = label
;
1278 dollar_label_instances
[dollar_label_count
] = 1;
1279 dollar_label_defines
[dollar_label_count
] = 1;
1280 ++dollar_label_count
;
1283 /* Caller must copy returned name: we re-use the area for the next name.
1285 The mth occurence of label n: is turned into the symbol "Ln^Am"
1286 where n is the label number and m is the instance number. "L" makes
1287 it a label discarded unless debugging and "^A"('\1') ensures no
1288 ordinary symbol SHOULD get the same name as a local label
1289 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1291 fb labels get the same treatment, except that ^B is used in place
1294 char * /* Return local label name. */
1295 dollar_label_name (register long n
, /* we just saw "n$:" : n a number. */
1296 register int augend
/* 0 for current instance, 1 for new instance. */)
1299 /* Returned to caller, then copied. Used for created names ("4f"). */
1300 static char symbol_name_build
[24];
1303 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1306 know (augend
== 0 || augend
== 1);
1307 p
= symbol_name_build
;
1308 #ifdef LOCAL_LABEL_PREFIX
1309 *p
++ = LOCAL_LABEL_PREFIX
;
1313 /* Next code just does sprintf( {}, "%d", n); */
1315 q
= symbol_name_temporary
;
1316 for (*q
++ = 0, i
= n
; i
; ++q
)
1321 while ((*p
= *--q
) != '\0')
1324 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1326 /* Instance number. */
1327 q
= symbol_name_temporary
;
1328 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1333 while ((*p
++ = *--q
) != '\0');;
1335 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1336 return symbol_name_build
;
1339 /* Somebody else's idea of local labels. They are made by "n:" where n
1340 is any decimal digit. Refer to them with
1341 "nb" for previous (backward) n:
1342 or "nf" for next (forward) n:.
1344 We do a little better and let n be any number, not just a single digit, but
1345 since the other guy's assembler only does ten, we treat the first ten
1348 Like someone else's assembler, we have one set of local label counters for
1349 entire assembly, not one set per (sub)segment like in most assemblers. This
1350 implies that one can refer to a label in another segment, and indeed some
1351 crufty compilers have done just that.
1353 Since there could be a LOT of these things, treat them as a sparse
1356 #define FB_LABEL_SPECIAL (10)
1358 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1359 static long *fb_labels
;
1360 static long *fb_label_instances
;
1361 static long fb_label_count
;
1362 static long fb_label_max
;
1364 /* This must be more than FB_LABEL_SPECIAL. */
1365 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1368 fb_label_init (void)
1370 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1373 /* Add one to the instance number of this fb label. */
1376 fb_label_instance_inc (long label
)
1380 if (label
< FB_LABEL_SPECIAL
)
1382 ++fb_low_counter
[label
];
1386 if (fb_labels
!= NULL
)
1388 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1389 i
< fb_labels
+ fb_label_count
; ++i
)
1393 ++fb_label_instances
[i
- fb_labels
];
1395 } /* if we find it */
1396 } /* for each existing label */
1399 /* If we get to here, we don't have label listed yet. */
1401 if (fb_labels
== NULL
)
1403 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1404 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1405 fb_label_max
= FB_LABEL_BUMP_BY
;
1406 fb_label_count
= FB_LABEL_SPECIAL
;
1409 else if (fb_label_count
== fb_label_max
)
1411 fb_label_max
+= FB_LABEL_BUMP_BY
;
1412 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1413 fb_label_max
* sizeof (long));
1414 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1415 fb_label_max
* sizeof (long));
1416 } /* if we needed to grow */
1418 fb_labels
[fb_label_count
] = label
;
1419 fb_label_instances
[fb_label_count
] = 1;
1424 fb_label_instance (long label
)
1428 if (label
< FB_LABEL_SPECIAL
)
1430 return (fb_low_counter
[label
]);
1433 if (fb_labels
!= NULL
)
1435 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1436 i
< fb_labels
+ fb_label_count
; ++i
)
1440 return (fb_label_instances
[i
- fb_labels
]);
1441 } /* if we find it */
1442 } /* for each existing label */
1445 /* We didn't find the label, so this must be a reference to the
1450 /* Caller must copy returned name: we re-use the area for the next name.
1452 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1453 where n is the label number and m is the instance number. "L" makes
1454 it a label discarded unless debugging and "^B"('\2') ensures no
1455 ordinary symbol SHOULD get the same name as a local label
1456 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1458 dollar labels get the same treatment, except that ^A is used in
1461 char * /* Return local label name. */
1462 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1463 long augend
/* 0 for nb, 1 for n:, nf. */)
1466 /* Returned to caller, then copied. Used for created names ("4f"). */
1467 static char symbol_name_build
[24];
1470 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1474 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1476 know ((unsigned long) augend
<= 1);
1478 p
= symbol_name_build
;
1479 #ifdef LOCAL_LABEL_PREFIX
1480 *p
++ = LOCAL_LABEL_PREFIX
;
1484 /* Next code just does sprintf( {}, "%d", n); */
1486 q
= symbol_name_temporary
;
1487 for (*q
++ = 0, i
= n
; i
; ++q
)
1492 while ((*p
= *--q
) != '\0')
1495 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1497 /* Instance number. */
1498 q
= symbol_name_temporary
;
1499 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1504 while ((*p
++ = *--q
) != '\0');;
1506 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1507 return (symbol_name_build
);
1510 /* Decode name that may have been generated by foo_label_name() above.
1511 If the name wasn't generated by foo_label_name(), then return it
1512 unaltered. This is used for error messages. */
1515 decode_local_label_name (char *s
)
1518 char *symbol_decode
;
1520 int instance_number
;
1522 const char *message_format
;
1525 #ifdef LOCAL_LABEL_PREFIX
1526 if (s
[index
] == LOCAL_LABEL_PREFIX
)
1530 if (s
[index
] != 'L')
1533 for (label_number
= 0, p
= s
+ index
+ 1; ISDIGIT (*p
); ++p
)
1534 label_number
= (10 * label_number
) + *p
- '0';
1536 if (*p
== DOLLAR_LABEL_CHAR
)
1538 else if (*p
== LOCAL_LABEL_CHAR
)
1543 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1544 instance_number
= (10 * instance_number
) + *p
- '0';
1546 message_format
= _("\"%d\" (instance number %d of a %s label)");
1547 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1548 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1550 return symbol_decode
;
1553 /* Get the value of a symbol. */
1556 S_GET_VALUE (symbolS
*s
)
1558 if (LOCAL_SYMBOL_CHECK (s
))
1559 return resolve_symbol_value (s
);
1561 if (!s
->sy_resolved
)
1563 valueT val
= resolve_symbol_value (s
);
1567 if (s
->sy_value
.X_op
!= O_constant
)
1569 static symbolS
*recur
;
1571 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1572 may call S_GET_VALUE. We use a static symbol to avoid the
1573 immediate recursion. */
1575 return (valueT
) s
->sy_value
.X_add_number
;
1577 if (! s
->sy_resolved
1578 || s
->sy_value
.X_op
!= O_symbol
1579 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1580 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1584 return (valueT
) s
->sy_value
.X_add_number
;
1587 /* Set the value of a symbol. */
1590 S_SET_VALUE (symbolS
*s
, valueT val
)
1592 if (LOCAL_SYMBOL_CHECK (s
))
1594 ((struct local_symbol
*) s
)->lsy_value
= val
;
1598 s
->sy_value
.X_op
= O_constant
;
1599 s
->sy_value
.X_add_number
= (offsetT
) val
;
1600 s
->sy_value
.X_unsigned
= 0;
1604 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1606 if (LOCAL_SYMBOL_CHECK (dest
))
1607 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
1608 if (LOCAL_SYMBOL_CHECK (src
))
1609 src
= local_symbol_convert ((struct local_symbol
*) src
);
1611 /* In an expression, transfer the settings of these flags.
1612 The user can override later, of course. */
1613 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1614 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1616 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1617 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1622 S_IS_FUNCTION (symbolS
*s
)
1626 if (LOCAL_SYMBOL_CHECK (s
))
1629 flags
= s
->bsym
->flags
;
1631 return (flags
& BSF_FUNCTION
) != 0;
1635 S_IS_EXTERNAL (symbolS
*s
)
1639 if (LOCAL_SYMBOL_CHECK (s
))
1642 flags
= s
->bsym
->flags
;
1645 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
1648 return (flags
& BSF_GLOBAL
) != 0;
1652 S_IS_WEAK (symbolS
*s
)
1654 if (LOCAL_SYMBOL_CHECK (s
))
1656 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
1660 S_IS_COMMON (symbolS
*s
)
1662 if (LOCAL_SYMBOL_CHECK (s
))
1664 return bfd_is_com_section (s
->bsym
->section
);
1668 S_IS_DEFINED (symbolS
*s
)
1670 if (LOCAL_SYMBOL_CHECK (s
))
1671 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
1672 return s
->bsym
->section
!= undefined_section
;
1676 #ifndef EXTERN_FORCE_RELOC
1677 #define EXTERN_FORCE_RELOC IS_ELF
1680 /* Return true for symbols that should not be reduced to section
1681 symbols or eliminated from expressions, because they may be
1682 overridden by the linker. */
1684 S_FORCE_RELOC (symbolS
*s
, int strict
)
1686 if (LOCAL_SYMBOL_CHECK (s
))
1687 return ((struct local_symbol
*) s
)->lsy_section
== undefined_section
;
1690 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
1691 || (EXTERN_FORCE_RELOC
1692 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
1693 || s
->bsym
->section
== undefined_section
1694 || bfd_is_com_section (s
->bsym
->section
));
1698 S_IS_DEBUG (symbolS
*s
)
1700 if (LOCAL_SYMBOL_CHECK (s
))
1702 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1708 S_IS_LOCAL (symbolS
*s
)
1713 if (LOCAL_SYMBOL_CHECK (s
))
1716 flags
= s
->bsym
->flags
;
1719 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
1722 if (bfd_get_section (s
->bsym
) == reg_section
)
1725 if (flag_strip_local_absolute
1726 /* Keep BSF_FILE symbols in order to allow debuggers to identify
1727 the source file even when the object file is stripped. */
1728 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
1729 && bfd_get_section (s
->bsym
) == absolute_section
)
1732 name
= S_GET_NAME (s
);
1733 return (name
!= NULL
1735 && (strchr (name
, DOLLAR_LABEL_CHAR
)
1736 || strchr (name
, LOCAL_LABEL_CHAR
)
1737 || (! flag_keep_locals
1738 && (bfd_is_local_label (stdoutput
, s
->bsym
)
1741 && name
[1] == '?')))));
1745 S_IS_STABD (symbolS
*s
)
1747 return S_GET_NAME (s
) == 0;
1751 S_GET_NAME (symbolS
*s
)
1753 if (LOCAL_SYMBOL_CHECK (s
))
1754 return ((struct local_symbol
*) s
)->lsy_name
;
1755 return s
->bsym
->name
;
1759 S_GET_SEGMENT (symbolS
*s
)
1761 if (LOCAL_SYMBOL_CHECK (s
))
1762 return ((struct local_symbol
*) s
)->lsy_section
;
1763 return s
->bsym
->section
;
1767 S_SET_SEGMENT (symbolS
*s
, segT seg
)
1769 /* Don't reassign section symbols. The direct reason is to prevent seg
1770 faults assigning back to const global symbols such as *ABS*, but it
1771 shouldn't happen anyway. */
1773 if (LOCAL_SYMBOL_CHECK (s
))
1775 if (seg
== reg_section
)
1776 s
= local_symbol_convert ((struct local_symbol
*) s
);
1779 ((struct local_symbol
*) s
)->lsy_section
= seg
;
1784 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
1786 if (s
->bsym
->section
!= seg
)
1790 s
->bsym
->section
= seg
;
1794 S_SET_EXTERNAL (symbolS
*s
)
1796 if (LOCAL_SYMBOL_CHECK (s
))
1797 s
= local_symbol_convert ((struct local_symbol
*) s
);
1798 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1800 /* Let .weak override .global. */
1803 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
1808 /* Do not reassign section symbols. */
1809 as_where (& file
, & line
);
1810 as_warn_where (file
, line
,
1811 _("section symbols are already global"));
1814 s
->bsym
->flags
|= BSF_GLOBAL
;
1815 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
1818 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
1819 an_external_name
= S_GET_NAME (s
);
1824 S_CLEAR_EXTERNAL (symbolS
*s
)
1826 if (LOCAL_SYMBOL_CHECK (s
))
1828 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1830 /* Let .weak override. */
1833 s
->bsym
->flags
|= BSF_LOCAL
;
1834 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
1838 S_SET_WEAK (symbolS
*s
)
1840 if (LOCAL_SYMBOL_CHECK (s
))
1841 s
= local_symbol_convert ((struct local_symbol
*) s
);
1842 s
->bsym
->flags
|= BSF_WEAK
;
1843 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
1847 S_SET_THREAD_LOCAL (symbolS
*s
)
1849 if (LOCAL_SYMBOL_CHECK (s
))
1850 s
= local_symbol_convert ((struct local_symbol
*) s
);
1851 if (bfd_is_com_section (s
->bsym
->section
)
1852 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
1854 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
1855 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
1856 as_bad (_("Accessing function `%s' as thread-local object"),
1858 else if (! bfd_is_und_section (s
->bsym
->section
)
1859 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
1860 as_bad (_("Accessing `%s' as thread-local object"),
1865 S_SET_NAME (symbolS
*s
, const char *name
)
1867 if (LOCAL_SYMBOL_CHECK (s
))
1869 ((struct local_symbol
*) s
)->lsy_name
= name
;
1872 s
->bsym
->name
= name
;
1875 /* Return the previous symbol in a chain. */
1878 symbol_previous (symbolS
*s
)
1880 if (LOCAL_SYMBOL_CHECK (s
))
1882 return s
->sy_previous
;
1885 /* Return the next symbol in a chain. */
1888 symbol_next (symbolS
*s
)
1890 if (LOCAL_SYMBOL_CHECK (s
))
1895 /* Return a pointer to the value of a symbol as an expression. */
1898 symbol_get_value_expression (symbolS
*s
)
1900 if (LOCAL_SYMBOL_CHECK (s
))
1901 s
= local_symbol_convert ((struct local_symbol
*) s
);
1902 return &s
->sy_value
;
1905 /* Set the value of a symbol to an expression. */
1908 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
1910 if (LOCAL_SYMBOL_CHECK (s
))
1911 s
= local_symbol_convert ((struct local_symbol
*) s
);
1915 /* Return a pointer to the X_add_number component of a symbol. */
1918 symbol_X_add_number (symbolS
*s
)
1920 if (LOCAL_SYMBOL_CHECK (s
))
1921 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
1923 return &s
->sy_value
.X_add_number
;
1926 /* Set the value of SYM to the current position in the current segment. */
1929 symbol_set_value_now (symbolS
*sym
)
1931 S_SET_SEGMENT (sym
, now_seg
);
1932 S_SET_VALUE (sym
, frag_now_fix ());
1933 symbol_set_frag (sym
, frag_now
);
1936 /* Set the frag of a symbol. */
1939 symbol_set_frag (symbolS
*s
, fragS
*f
)
1941 if (LOCAL_SYMBOL_CHECK (s
))
1943 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
1949 /* Return the frag of a symbol. */
1952 symbol_get_frag (symbolS
*s
)
1954 if (LOCAL_SYMBOL_CHECK (s
))
1955 return local_symbol_get_frag ((struct local_symbol
*) s
);
1959 /* Mark a symbol as having been used. */
1962 symbol_mark_used (symbolS
*s
)
1964 if (LOCAL_SYMBOL_CHECK (s
))
1969 /* Clear the mark of whether a symbol has been used. */
1972 symbol_clear_used (symbolS
*s
)
1974 if (LOCAL_SYMBOL_CHECK (s
))
1975 s
= local_symbol_convert ((struct local_symbol
*) s
);
1979 /* Return whether a symbol has been used. */
1982 symbol_used_p (symbolS
*s
)
1984 if (LOCAL_SYMBOL_CHECK (s
))
1989 /* Mark a symbol as having been used in a reloc. */
1992 symbol_mark_used_in_reloc (symbolS
*s
)
1994 if (LOCAL_SYMBOL_CHECK (s
))
1995 s
= local_symbol_convert ((struct local_symbol
*) s
);
1996 s
->sy_used_in_reloc
= 1;
1999 /* Clear the mark of whether a symbol has been used in a reloc. */
2002 symbol_clear_used_in_reloc (symbolS
*s
)
2004 if (LOCAL_SYMBOL_CHECK (s
))
2006 s
->sy_used_in_reloc
= 0;
2009 /* Return whether a symbol has been used in a reloc. */
2012 symbol_used_in_reloc_p (symbolS
*s
)
2014 if (LOCAL_SYMBOL_CHECK (s
))
2016 return s
->sy_used_in_reloc
;
2019 /* Mark a symbol as an MRI common symbol. */
2022 symbol_mark_mri_common (symbolS
*s
)
2024 if (LOCAL_SYMBOL_CHECK (s
))
2025 s
= local_symbol_convert ((struct local_symbol
*) s
);
2026 s
->sy_mri_common
= 1;
2029 /* Clear the mark of whether a symbol is an MRI common symbol. */
2032 symbol_clear_mri_common (symbolS
*s
)
2034 if (LOCAL_SYMBOL_CHECK (s
))
2036 s
->sy_mri_common
= 0;
2039 /* Return whether a symbol is an MRI common symbol. */
2042 symbol_mri_common_p (symbolS
*s
)
2044 if (LOCAL_SYMBOL_CHECK (s
))
2046 return s
->sy_mri_common
;
2049 /* Mark a symbol as having been written. */
2052 symbol_mark_written (symbolS
*s
)
2054 if (LOCAL_SYMBOL_CHECK (s
))
2059 /* Clear the mark of whether a symbol has been written. */
2062 symbol_clear_written (symbolS
*s
)
2064 if (LOCAL_SYMBOL_CHECK (s
))
2069 /* Return whether a symbol has been written. */
2072 symbol_written_p (symbolS
*s
)
2074 if (LOCAL_SYMBOL_CHECK (s
))
2079 /* Mark a symbol has having been resolved. */
2082 symbol_mark_resolved (symbolS
*s
)
2084 if (LOCAL_SYMBOL_CHECK (s
))
2086 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2092 /* Return whether a symbol has been resolved. */
2095 symbol_resolved_p (symbolS
*s
)
2097 if (LOCAL_SYMBOL_CHECK (s
))
2098 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2099 return s
->sy_resolved
;
2102 /* Return whether a symbol is a section symbol. */
2105 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2107 if (LOCAL_SYMBOL_CHECK (s
))
2109 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2112 /* Return whether a symbol is equated to another symbol. */
2115 symbol_equated_p (symbolS
*s
)
2117 if (LOCAL_SYMBOL_CHECK (s
))
2119 return s
->sy_value
.X_op
== O_symbol
;
2122 /* Return whether a symbol is equated to another symbol, and should be
2123 treated specially when writing out relocs. */
2126 symbol_equated_reloc_p (symbolS
*s
)
2128 if (LOCAL_SYMBOL_CHECK (s
))
2130 /* X_op_symbol, normally not used for O_symbol, is set by
2131 resolve_symbol_value to flag expression syms that have been
2133 return (s
->sy_value
.X_op
== O_symbol
2134 #if defined (OBJ_COFF) && defined (TE_PE)
2137 && ((s
->sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2138 || ! S_IS_DEFINED (s
)
2139 || S_IS_COMMON (s
)));
2142 /* Return whether a symbol has a constant value. */
2145 symbol_constant_p (symbolS
*s
)
2147 if (LOCAL_SYMBOL_CHECK (s
))
2149 return s
->sy_value
.X_op
== O_constant
;
2152 /* Return the BFD symbol for a symbol. */
2155 symbol_get_bfdsym (symbolS
*s
)
2157 if (LOCAL_SYMBOL_CHECK (s
))
2158 s
= local_symbol_convert ((struct local_symbol
*) s
);
2162 /* Set the BFD symbol for a symbol. */
2165 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2167 if (LOCAL_SYMBOL_CHECK (s
))
2168 s
= local_symbol_convert ((struct local_symbol
*) s
);
2169 /* Usually, it is harmless to reset a symbol to a BFD section
2170 symbol. For example, obj_elf_change_section sets the BFD symbol
2171 of an old symbol with the newly created section symbol. But when
2172 we have multiple sections with the same name, the newly created
2173 section may have the same name as an old section. We check if the
2174 old symbol has been already marked as a section symbol before
2176 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2178 /* else XXX - What do we do now ? */
2181 #ifdef OBJ_SYMFIELD_TYPE
2183 /* Get a pointer to the object format information for a symbol. */
2186 symbol_get_obj (symbolS
*s
)
2188 if (LOCAL_SYMBOL_CHECK (s
))
2189 s
= local_symbol_convert ((struct local_symbol
*) s
);
2193 /* Set the object format information for a symbol. */
2196 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2198 if (LOCAL_SYMBOL_CHECK (s
))
2199 s
= local_symbol_convert ((struct local_symbol
*) s
);
2203 #endif /* OBJ_SYMFIELD_TYPE */
2205 #ifdef TC_SYMFIELD_TYPE
2207 /* Get a pointer to the processor information for a symbol. */
2210 symbol_get_tc (symbolS
*s
)
2212 if (LOCAL_SYMBOL_CHECK (s
))
2213 s
= local_symbol_convert ((struct local_symbol
*) s
);
2217 /* Set the processor information for a symbol. */
2220 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2222 if (LOCAL_SYMBOL_CHECK (s
))
2223 s
= local_symbol_convert ((struct local_symbol
*) s
);
2227 #endif /* TC_SYMFIELD_TYPE */
2232 symbol_lastP
= NULL
;
2233 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2234 sy_hash
= hash_new ();
2235 local_hash
= hash_new ();
2237 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2238 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2239 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
2241 abs_symbol
.sy_value
.X_op
= O_constant
;
2242 abs_symbol
.sy_frag
= &zero_address_frag
;
2244 if (LOCAL_LABELS_FB
)
2250 /* Maximum indent level.
2251 Available for modification inside a gdb session. */
2252 static int max_indent_level
= 8;
2255 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2257 const char *name
= S_GET_NAME (sym
);
2258 if (!name
|| !name
[0])
2260 fprintf (file
, "sym %lx %s", (unsigned long) sym
, name
);
2262 if (LOCAL_SYMBOL_CHECK (sym
))
2264 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2265 if (local_symbol_get_frag (locsym
) != &zero_address_frag
2266 && local_symbol_get_frag (locsym
) != NULL
)
2267 fprintf (file
, " frag %lx", (long) local_symbol_get_frag (locsym
));
2268 if (local_symbol_resolved_p (locsym
))
2269 fprintf (file
, " resolved");
2270 fprintf (file
, " local");
2274 if (sym
->sy_frag
!= &zero_address_frag
)
2275 fprintf (file
, " frag %lx", (long) sym
->sy_frag
);
2277 fprintf (file
, " written");
2278 if (sym
->sy_resolved
)
2279 fprintf (file
, " resolved");
2280 else if (sym
->sy_resolving
)
2281 fprintf (file
, " resolving");
2282 if (sym
->sy_used_in_reloc
)
2283 fprintf (file
, " used-in-reloc");
2285 fprintf (file
, " used");
2286 if (S_IS_LOCAL (sym
))
2287 fprintf (file
, " local");
2288 if (S_IS_EXTERNAL (sym
))
2289 fprintf (file
, " extern");
2290 if (S_IS_DEBUG (sym
))
2291 fprintf (file
, " debug");
2292 if (S_IS_DEFINED (sym
))
2293 fprintf (file
, " defined");
2295 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2296 if (symbol_resolved_p (sym
))
2298 segT s
= S_GET_SEGMENT (sym
);
2300 if (s
!= undefined_section
2301 && s
!= expr_section
)
2302 fprintf (file
, " %lx", (long) S_GET_VALUE (sym
));
2304 else if (indent_level
< max_indent_level
2305 && S_GET_SEGMENT (sym
) != undefined_section
)
2308 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2309 if (LOCAL_SYMBOL_CHECK (sym
))
2310 fprintf (file
, "constant %lx",
2311 (long) ((struct local_symbol
*) sym
)->lsy_value
);
2313 print_expr_1 (file
, &sym
->sy_value
);
2314 fprintf (file
, ">");
2321 print_symbol_value (symbolS
*sym
)
2324 print_symbol_value_1 (stderr
, sym
);
2325 fprintf (stderr
, "\n");
2329 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2332 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2333 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2334 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2335 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2336 fprintf (file
, ">");
2341 print_expr_1 (FILE *file
, expressionS
*exp
)
2343 fprintf (file
, "expr %lx ", (long) exp
);
2347 fprintf (file
, "illegal");
2350 fprintf (file
, "absent");
2353 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
2357 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2358 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2359 fprintf (file
, ">");
2361 if (exp
->X_add_number
)
2362 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2363 (long) exp
->X_add_number
);
2367 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2370 fprintf (file
, "big");
2373 fprintf (file
, "uminus -<");
2375 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2376 fprintf (file
, ">");
2377 goto maybe_print_addnum
;
2379 fprintf (file
, "bit_not");
2382 print_binary (file
, "multiply", exp
);
2385 print_binary (file
, "divide", exp
);
2388 print_binary (file
, "modulus", exp
);
2391 print_binary (file
, "lshift", exp
);
2394 print_binary (file
, "rshift", exp
);
2396 case O_bit_inclusive_or
:
2397 print_binary (file
, "bit_ior", exp
);
2399 case O_bit_exclusive_or
:
2400 print_binary (file
, "bit_xor", exp
);
2403 print_binary (file
, "bit_and", exp
);
2406 print_binary (file
, "eq", exp
);
2409 print_binary (file
, "ne", exp
);
2412 print_binary (file
, "lt", exp
);
2415 print_binary (file
, "le", exp
);
2418 print_binary (file
, "ge", exp
);
2421 print_binary (file
, "gt", exp
);
2424 print_binary (file
, "logical_and", exp
);
2427 print_binary (file
, "logical_or", exp
);
2431 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
2432 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2433 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2434 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2435 fprintf (file
, ">");
2436 goto maybe_print_addnum
;
2439 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
2440 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2441 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2442 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2443 fprintf (file
, ">");
2444 goto maybe_print_addnum
;
2446 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
2453 print_expr (expressionS
*exp
)
2455 print_expr_1 (stderr
, exp
);
2456 fprintf (stderr
, "\n");
2460 symbol_print_statistics (FILE *file
)
2462 hash_print_statistics (file
, "symbol table", sy_hash
);
2463 hash_print_statistics (file
, "mini local symbol table", local_hash
);
2464 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
2465 local_symbol_count
, local_symbol_conversion_count
);