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. */
93 extern int symbol_table_frozen
;
94 if (symbol_table_frozen
)
98 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
103 /* Save a symbol name on a permanent obstack, and convert it according
104 to the object file format. */
107 save_symbol_name (const char *name
)
109 unsigned int name_length
;
112 name_length
= strlen (name
) + 1; /* +1 for \0. */
113 obstack_grow (¬es
, name
, name_length
);
114 ret
= obstack_finish (¬es
);
116 #ifdef tc_canonicalize_symbol_name
117 ret
= tc_canonicalize_symbol_name (ret
);
120 if (! symbols_case_sensitive
)
124 for (s
= ret
; *s
!= '\0'; s
++)
132 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
133 segT segment
, /* Segment identifier (SEG_<something>). */
134 valueT valu
, /* Symbol value. */
135 fragS
*frag
/* Associated fragment. */)
137 char *preserved_copy_of_name
;
140 preserved_copy_of_name
= save_symbol_name (name
);
142 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
144 /* symbol must be born in some fixed state. This seems as good as any. */
145 memset (symbolP
, 0, sizeof (symbolS
));
148 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
149 if (symbolP
->bsym
== NULL
)
150 as_perror ("%s", "bfd_make_empty_symbol");
151 symbolP
->bsym
->udata
.p
= (PTR
) symbolP
;
153 S_SET_NAME (symbolP
, preserved_copy_of_name
);
155 S_SET_SEGMENT (symbolP
, segment
);
156 S_SET_VALUE (symbolP
, valu
);
157 symbol_clear_list_pointers (symbolP
);
159 symbolP
->sy_frag
= frag
;
160 #ifndef BFD_ASSEMBLER
161 symbolP
->sy_number
= ~0;
162 symbolP
->sy_name_offset
= (unsigned int) ~0;
165 obj_symbol_new_hook (symbolP
);
167 #ifdef tc_symbol_new_hook
168 tc_symbol_new_hook (symbolP
);
176 /* Local symbol support. If we can get away with it, we keep only a
177 small amount of information for local symbols. */
179 static symbolS
*local_symbol_convert (struct local_symbol
*);
181 /* Used for statistics. */
183 static unsigned long local_symbol_count
;
184 static unsigned long local_symbol_conversion_count
;
186 /* This macro is called with a symbol argument passed by reference.
187 It returns whether this is a local symbol. If necessary, it
188 changes its argument to the real symbol. */
190 #define LOCAL_SYMBOL_CHECK(s) \
192 ? (local_symbol_converted_p ((struct local_symbol *) s) \
193 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
198 /* Create a local symbol and insert it into the local hash table. */
200 static struct local_symbol
*
201 local_symbol_make (const char *name
, segT section
, valueT value
, fragS
*frag
)
204 struct local_symbol
*ret
;
206 ++local_symbol_count
;
208 name_copy
= save_symbol_name (name
);
210 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
211 ret
->lsy_marker
= NULL
;
212 ret
->lsy_name
= name_copy
;
213 ret
->lsy_section
= section
;
214 local_symbol_set_frag (ret
, frag
);
215 ret
->lsy_value
= value
;
217 hash_jam (local_hash
, name_copy
, (PTR
) ret
);
222 /* Convert a local symbol into a real symbol. Note that we do not
223 reclaim the space used by the local symbol. */
226 local_symbol_convert (struct local_symbol
*locsym
)
230 assert (locsym
->lsy_marker
== NULL
);
231 if (local_symbol_converted_p (locsym
))
232 return local_symbol_get_real_symbol (locsym
);
234 ++local_symbol_conversion_count
;
236 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
237 local_symbol_get_frag (locsym
));
239 if (local_symbol_resolved_p (locsym
))
240 ret
->sy_resolved
= 1;
242 /* Local symbols are always either defined or used. */
245 #ifdef TC_LOCAL_SYMFIELD_CONVERT
246 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
249 symbol_table_insert (ret
);
251 local_symbol_mark_converted (locsym
);
252 local_symbol_set_real_symbol (locsym
, ret
);
254 hash_jam (local_hash
, locsym
->lsy_name
, NULL
);
259 #else /* ! BFD_ASSEMBLER */
261 #define LOCAL_SYMBOL_CHECK(s) 0
262 #define local_symbol_convert(s) ((symbolS *) s)
264 #endif /* ! BFD_ASSEMBLER */
266 /* We have just seen "<name>:".
267 Creates a struct symbol unless it already exists.
269 Gripes if we are redefining a symbol incompatibly (and ignores it). */
272 colon (/* Just seen "x:" - rattle symbols & frags. */
273 const char *sym_name
/* Symbol name, as a cannonical string. */
274 /* We copy this string: OK to alter later. */)
276 register symbolS
*symbolP
; /* Symbol we are working with. */
278 /* Sun local labels go out of scope whenever a non-local symbol is
280 if (LOCAL_LABELS_DOLLAR
)
285 local
= bfd_is_local_label_name (stdoutput
, sym_name
);
287 local
= LOCAL_LABEL (sym_name
);
291 dollar_label_clear ();
294 #ifndef WORKING_DOT_WORD
295 if (new_broken_words
)
297 struct broken_word
*a
;
302 if (now_seg
== absolute_section
)
304 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
308 possible_bytes
= (md_short_jump_size
309 + new_broken_words
* md_long_jump_size
);
312 frag_opcode
= frag_var (rs_broken_word
,
316 (symbolS
*) broken_words
,
320 /* We want to store the pointer to where to insert the jump
321 table in the fr_opcode of the rs_broken_word frag. This
322 requires a little hackery. */
324 && (frag_tmp
->fr_type
!= rs_broken_word
325 || frag_tmp
->fr_opcode
))
326 frag_tmp
= frag_tmp
->fr_next
;
328 frag_tmp
->fr_opcode
= frag_opcode
;
329 new_broken_words
= 0;
331 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
332 a
->dispfrag
= frag_tmp
;
334 #endif /* WORKING_DOT_WORD */
336 if ((symbolP
= symbol_find (sym_name
)) != 0)
338 #ifdef RESOLVE_SYMBOL_REDEFINITION
339 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
342 /* Now check for undefined symbols. */
343 if (LOCAL_SYMBOL_CHECK (symbolP
))
346 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
348 if (locsym
->lsy_section
!= undefined_section
349 && (local_symbol_get_frag (locsym
) != frag_now
350 || locsym
->lsy_section
!= now_seg
351 || locsym
->lsy_value
!= frag_now_fix ()))
353 as_bad (_("symbol `%s' is already defined"), sym_name
);
357 locsym
->lsy_section
= now_seg
;
358 local_symbol_set_frag (locsym
, frag_now
);
359 locsym
->lsy_value
= frag_now_fix ();
362 else if (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
364 if (S_GET_VALUE (symbolP
) == 0)
366 symbolP
->sy_frag
= frag_now
;
368 S_SET_OTHER (symbolP
, const_flag
);
370 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
371 S_SET_SEGMENT (symbolP
, now_seg
);
374 #endif /* if we have one, it better be zero. */
379 /* There are still several cases to check:
381 A .comm/.lcomm symbol being redefined as initialized
384 A .comm/.lcomm symbol being redefined with a larger
387 This only used to be allowed on VMS gas, but Sun cc
388 on the sparc also depends on it. */
390 if (((!S_IS_DEBUG (symbolP
)
391 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
392 && S_IS_EXTERNAL (symbolP
))
393 || S_GET_SEGMENT (symbolP
) == bss_section
)
394 && (now_seg
== data_section
395 || now_seg
== S_GET_SEGMENT (symbolP
)))
397 /* Select which of the 2 cases this is. */
398 if (now_seg
!= data_section
)
400 /* New .comm for prev .comm symbol.
402 If the new size is larger we just change its
403 value. If the new size is smaller, we ignore
405 if (S_GET_VALUE (symbolP
)
406 < ((unsigned) frag_now_fix ()))
408 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
413 /* It is a .comm/.lcomm being converted to initialized
415 symbolP
->sy_frag
= frag_now
;
417 S_SET_OTHER (symbolP
, const_flag
);
419 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
420 S_SET_SEGMENT (symbolP
, now_seg
); /* Keep N_EXT bit. */
425 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
426 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
427 static const char *od_buf
= "";
432 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
434 sprintf (od_buf
, "%d.%d.",
435 S_GET_OTHER (symbolP
),
436 S_GET_DESC (symbolP
));
438 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
440 segment_name (S_GET_SEGMENT (symbolP
)),
442 (long) S_GET_VALUE (symbolP
));
444 } /* if the undefined symbol has no value */
448 /* Don't blow up if the definition is the same. */
449 if (!(frag_now
== symbolP
->sy_frag
450 && S_GET_VALUE (symbolP
) == frag_now_fix ()
451 && S_GET_SEGMENT (symbolP
) == now_seg
))
452 as_bad (_("symbol `%s' is already defined"), sym_name
);
457 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
459 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
460 (valueT
) frag_now_fix (),
463 #endif /* BFD_ASSEMBLER */
466 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
469 S_SET_OTHER (symbolP
, const_flag
);
472 symbol_table_insert (symbolP
);
475 if (mri_common_symbol
!= NULL
)
477 /* This symbol is actually being defined within an MRI common
478 section. This requires special handling. */
479 if (LOCAL_SYMBOL_CHECK (symbolP
))
480 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
481 symbolP
->sy_value
.X_op
= O_symbol
;
482 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
483 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
484 symbolP
->sy_frag
= &zero_address_frag
;
485 S_SET_SEGMENT (symbolP
, expr_section
);
486 symbolP
->sy_mri_common
= 1;
490 tc_frob_label (symbolP
);
492 #ifdef obj_frob_label
493 obj_frob_label (symbolP
);
499 /* Die if we can't insert the symbol. */
502 symbol_table_insert (symbolS
*symbolP
)
504 register const char *error_string
;
507 know (S_GET_NAME (symbolP
));
509 if (LOCAL_SYMBOL_CHECK (symbolP
))
511 error_string
= hash_jam (local_hash
, S_GET_NAME (symbolP
),
513 if (error_string
!= NULL
)
514 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
515 S_GET_NAME (symbolP
), error_string
);
519 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
521 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
522 S_GET_NAME (symbolP
), error_string
);
526 /* If a symbol name does not exist, create it as undefined, and insert
527 it into the symbol table. Return a pointer to it. */
530 symbol_find_or_make (const char *name
)
532 register symbolS
*symbolP
;
534 symbolP
= symbol_find (name
);
539 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
541 symbolP
= md_undefined_symbol ((char *) name
);
545 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
552 symbolP
= symbol_make (name
);
554 symbol_table_insert (symbolP
);
555 } /* if symbol wasn't found */
561 symbol_make (const char *name
)
565 /* Let the machine description default it, e.g. for register names. */
566 symbolP
= md_undefined_symbol ((char *) name
);
569 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
575 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
577 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
581 symbol_temp_new_now (void)
583 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
587 symbol_temp_make (void)
589 return symbol_make (FAKE_LABEL_NAME
);
592 /* Implement symbol table lookup.
593 In: A symbol's name as a string: '\0' can't be part of a symbol name.
594 Out: NULL if the name was not in the symbol table, else the address
595 of a struct symbol associated with that name. */
598 symbol_find_exact (const char *name
)
602 struct local_symbol
*locsym
;
604 locsym
= (struct local_symbol
*) hash_find (local_hash
, name
);
606 return (symbolS
*) locsym
;
610 return ((symbolS
*) hash_find (sy_hash
, name
));
614 symbol_find (const char *name
)
616 #ifdef tc_canonicalize_symbol_name
619 size_t len
= strlen (name
) + 1;
621 copy
= (char *) alloca (len
);
622 memcpy (copy
, name
, len
);
623 name
= tc_canonicalize_symbol_name (copy
);
627 if (! symbols_case_sensitive
)
634 name
= copy
= (char *) alloca (strlen (name
) + 1);
636 while ((c
= *orig
++) != '\0')
638 *copy
++ = TOUPPER (c
);
643 return symbol_find_exact (name
);
646 /* Once upon a time, symbols were kept in a singly linked list. At
647 least coff needs to be able to rearrange them from time to time, for
648 which a doubly linked list is much more convenient. Loic did these
649 as macros which seemed dangerous to me so they're now functions.
652 /* Link symbol ADDME after symbol TARGET in the chain. */
655 symbol_append (symbolS
*addme
, symbolS
*target
,
656 symbolS
**rootPP
, symbolS
**lastPP
)
658 if (LOCAL_SYMBOL_CHECK (addme
))
660 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
665 know (*rootPP
== NULL
);
666 know (*lastPP
== NULL
);
667 addme
->sy_next
= NULL
;
668 #ifdef SYMBOLS_NEED_BACKPOINTERS
669 addme
->sy_previous
= NULL
;
674 } /* if the list is empty */
676 if (target
->sy_next
!= NULL
)
678 #ifdef SYMBOLS_NEED_BACKPOINTERS
679 target
->sy_next
->sy_previous
= addme
;
680 #endif /* SYMBOLS_NEED_BACKPOINTERS */
684 know (*lastPP
== target
);
686 } /* if we have a next */
688 addme
->sy_next
= target
->sy_next
;
689 target
->sy_next
= addme
;
691 #ifdef SYMBOLS_NEED_BACKPOINTERS
692 addme
->sy_previous
= target
;
693 #endif /* SYMBOLS_NEED_BACKPOINTERS */
695 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
698 /* Set the chain pointers of SYMBOL to null. */
701 symbol_clear_list_pointers (symbolS
*symbolP
)
703 if (LOCAL_SYMBOL_CHECK (symbolP
))
705 symbolP
->sy_next
= NULL
;
706 #ifdef SYMBOLS_NEED_BACKPOINTERS
707 symbolP
->sy_previous
= NULL
;
711 #ifdef SYMBOLS_NEED_BACKPOINTERS
712 /* Remove SYMBOLP from the list. */
715 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
717 if (LOCAL_SYMBOL_CHECK (symbolP
))
720 if (symbolP
== *rootPP
)
722 *rootPP
= symbolP
->sy_next
;
723 } /* if it was the root */
725 if (symbolP
== *lastPP
)
727 *lastPP
= symbolP
->sy_previous
;
728 } /* if it was the tail */
730 if (symbolP
->sy_next
!= NULL
)
732 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
735 if (symbolP
->sy_previous
!= NULL
)
737 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
740 debug_verify_symchain (*rootPP
, *lastPP
);
743 /* Link symbol ADDME before symbol TARGET in the chain. */
746 symbol_insert (symbolS
*addme
, symbolS
*target
,
747 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
749 if (LOCAL_SYMBOL_CHECK (addme
))
751 if (LOCAL_SYMBOL_CHECK (target
))
754 if (target
->sy_previous
!= NULL
)
756 target
->sy_previous
->sy_next
= addme
;
760 know (*rootPP
== target
);
764 addme
->sy_previous
= target
->sy_previous
;
765 target
->sy_previous
= addme
;
766 addme
->sy_next
= target
;
768 debug_verify_symchain (*rootPP
, *lastPP
);
771 #endif /* SYMBOLS_NEED_BACKPOINTERS */
774 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
776 symbolS
*symbolP
= rootP
;
781 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
784 assert (symbolP
->bsym
!= NULL
);
786 #ifdef SYMBOLS_NEED_BACKPOINTERS
787 assert (symbolP
->sy_next
->sy_previous
== symbolP
);
789 /* Walk the list anyways, to make sure pointers are still good. */
791 #endif /* SYMBOLS_NEED_BACKPOINTERS */
794 assert (lastP
== symbolP
);
798 report_op_error (symbolS
*symp
, symbolS
*left
, symbolS
*right
)
802 segT seg_left
= S_GET_SEGMENT (left
);
803 segT seg_right
= right
? S_GET_SEGMENT (right
) : 0;
805 if (expr_symbol_where (symp
, &file
, &line
))
807 if (seg_left
== undefined_section
)
808 as_bad_where (file
, line
,
809 _("undefined symbol `%s' in operation"),
811 if (seg_right
== undefined_section
)
812 as_bad_where (file
, line
,
813 _("undefined symbol `%s' in operation"),
815 if (seg_left
!= undefined_section
816 && seg_right
!= undefined_section
)
819 as_bad_where (file
, line
,
820 _("invalid sections for operation on `%s' and `%s'"),
821 S_GET_NAME (left
), S_GET_NAME (right
));
823 as_bad_where (file
, line
,
824 _("invalid section for operation on `%s'"),
831 if (seg_left
== undefined_section
)
832 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
833 S_GET_NAME (left
), S_GET_NAME (symp
));
834 if (seg_right
== undefined_section
)
835 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
836 S_GET_NAME (right
), S_GET_NAME (symp
));
837 if (seg_left
!= undefined_section
838 && seg_right
!= undefined_section
)
841 as_bad_where (file
, line
,
842 _("invalid sections for operation on `%s' and `%s' setting `%s'"),
843 S_GET_NAME (left
), S_GET_NAME (right
), S_GET_NAME (symp
));
845 as_bad_where (file
, line
,
846 _("invalid section for operation on `%s' setting `%s'"),
847 S_GET_NAME (left
), S_GET_NAME (symp
));
852 /* Resolve the value of a symbol. This is called during the final
853 pass over the symbol table to resolve any symbols with complex
857 resolve_symbol_value (symbolS
*symp
)
860 valueT final_val
= 0;
864 if (LOCAL_SYMBOL_CHECK (symp
))
866 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
868 final_val
= locsym
->lsy_value
;
869 if (local_symbol_resolved_p (locsym
))
872 final_val
+= local_symbol_get_frag (locsym
)->fr_address
/ OCTETS_PER_BYTE
;
876 locsym
->lsy_value
= final_val
;
877 local_symbol_mark_resolved (locsym
);
884 if (symp
->sy_resolved
)
886 if (symp
->sy_value
.X_op
== O_constant
)
887 return (valueT
) symp
->sy_value
.X_add_number
;
893 final_seg
= S_GET_SEGMENT (symp
);
895 if (symp
->sy_resolving
)
898 as_bad (_("symbol definition loop encountered at `%s'"),
905 symbolS
*add_symbol
, *op_symbol
;
907 segT seg_left
, seg_right
;
910 symp
->sy_resolving
= 1;
912 /* Help out with CSE. */
913 add_symbol
= symp
->sy_value
.X_add_symbol
;
914 op_symbol
= symp
->sy_value
.X_op_symbol
;
915 final_val
= symp
->sy_value
.X_add_number
;
916 op
= symp
->sy_value
.X_op
;
929 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
930 if (final_seg
== expr_section
)
931 final_seg
= absolute_section
;
937 left
= resolve_symbol_value (add_symbol
);
938 seg_left
= S_GET_SEGMENT (add_symbol
);
940 symp
->sy_value
.X_op_symbol
= NULL
;
943 if (symp
->sy_mri_common
)
945 /* This is a symbol inside an MRI common section. The
946 relocation routines are going to handle it specially.
947 Don't change the value. */
948 resolved
= symbol_resolved_p (add_symbol
);
952 if (finalize_syms
&& final_val
== 0)
954 if (LOCAL_SYMBOL_CHECK (add_symbol
))
955 add_symbol
= local_symbol_convert ((struct local_symbol
*)
957 copy_symbol_attributes (symp
, add_symbol
);
960 /* If we have equated this symbol to an undefined or common
961 symbol, keep X_op set to O_symbol, and don't change
962 X_add_number. This permits the routine which writes out
963 relocation to detect this case, and convert the
964 relocation to be against the symbol to which this symbol
966 if (! S_IS_DEFINED (add_symbol
)
967 #if defined (OBJ_COFF) && defined (TE_PE) && (defined(BFD_ASSEMBLER) || defined(S_IS_WEAK))
968 || S_IS_WEAK (add_symbol
)
970 || S_IS_COMMON (add_symbol
))
974 symp
->sy_value
.X_op
= O_symbol
;
975 symp
->sy_value
.X_add_symbol
= add_symbol
;
976 symp
->sy_value
.X_add_number
= final_val
;
977 /* Use X_op_symbol as a flag. */
978 symp
->sy_value
.X_op_symbol
= add_symbol
;
979 final_seg
= seg_left
;
982 resolved
= symbol_resolved_p (add_symbol
);
983 symp
->sy_resolving
= 0;
984 goto exit_dont_set_value
;
986 else if (finalize_syms
&& final_seg
== expr_section
987 && seg_left
!= expr_section
)
989 /* If the symbol is an expression symbol, do similarly
990 as for undefined and common syms above. Handles
991 "sym +/- expr" where "expr" cannot be evaluated
992 immediately, and we want relocations to be against
993 "sym", eg. because it is weak. */
994 symp
->sy_value
.X_op
= O_symbol
;
995 symp
->sy_value
.X_add_symbol
= add_symbol
;
996 symp
->sy_value
.X_add_number
= final_val
;
997 symp
->sy_value
.X_op_symbol
= add_symbol
;
998 final_seg
= seg_left
;
999 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1000 resolved
= symbol_resolved_p (add_symbol
);
1001 symp
->sy_resolving
= 0;
1002 goto exit_dont_set_value
;
1006 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1007 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1008 final_seg
= seg_left
;
1011 resolved
= symbol_resolved_p (add_symbol
);
1017 left
= resolve_symbol_value (add_symbol
);
1018 seg_left
= S_GET_SEGMENT (add_symbol
);
1020 /* By reducing these to the relevant dyadic operator, we get
1021 !S -> S == 0 permitted on anything,
1022 -S -> 0 - S only permitted on absolute
1023 ~S -> S ^ ~0 only permitted on absolute */
1024 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1026 report_op_error (symp
, add_symbol
, NULL
);
1028 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1029 final_seg
= absolute_section
;
1033 else if (op
== O_logical_not
)
1038 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1040 resolved
= symbol_resolved_p (add_symbol
);
1048 case O_bit_inclusive_or
:
1050 case O_bit_exclusive_or
:
1062 left
= resolve_symbol_value (add_symbol
);
1063 right
= resolve_symbol_value (op_symbol
);
1064 seg_left
= S_GET_SEGMENT (add_symbol
);
1065 seg_right
= S_GET_SEGMENT (op_symbol
);
1067 /* Simplify addition or subtraction of a constant by folding the
1068 constant into X_add_number. */
1071 if (seg_right
== absolute_section
)
1076 else if (seg_left
== absolute_section
)
1079 add_symbol
= op_symbol
;
1081 seg_left
= seg_right
;
1085 else if (op
== O_subtract
)
1087 if (seg_right
== absolute_section
)
1094 /* Equality and non-equality tests are permitted on anything.
1095 Subtraction, and other comparison operators are permitted if
1096 both operands are in the same section. Otherwise, both
1097 operands must be absolute. We already handled the case of
1098 addition or subtraction of a constant above. This will
1099 probably need to be changed for an object file format which
1100 supports arbitrary expressions, such as IEEE-695.
1102 Don't emit messages unless we're finalizing the symbol value,
1103 otherwise we may get the same message multiple times. */
1105 && !(seg_left
== absolute_section
1106 && seg_right
== absolute_section
)
1107 && !(op
== O_eq
|| op
== O_ne
)
1108 && !((op
== O_subtract
1109 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1110 && seg_left
== seg_right
1111 && (seg_left
!= undefined_section
1112 || add_symbol
== op_symbol
)))
1113 report_op_error (symp
, add_symbol
, op_symbol
);
1115 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1116 final_seg
= absolute_section
;
1118 /* Check for division by zero. */
1119 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1121 /* If seg_right is not absolute_section, then we've
1122 already issued a warning about using a bad symbol. */
1123 if (seg_right
== absolute_section
&& finalize_syms
)
1128 if (expr_symbol_where (symp
, &file
, &line
))
1129 as_bad_where (file
, line
, _("division by zero"));
1131 as_bad (_("division by zero when setting `%s'"),
1138 switch (symp
->sy_value
.X_op
)
1140 case O_multiply
: left
*= right
; break;
1141 case O_divide
: left
/= right
; break;
1142 case O_modulus
: left
%= right
; break;
1143 case O_left_shift
: left
<<= right
; break;
1144 case O_right_shift
: left
>>= right
; break;
1145 case O_bit_inclusive_or
: left
|= right
; break;
1146 case O_bit_or_not
: left
|= ~right
; break;
1147 case O_bit_exclusive_or
: left
^= right
; break;
1148 case O_bit_and
: left
&= right
; break;
1149 case O_add
: left
+= right
; break;
1150 case O_subtract
: left
-= right
; break;
1153 left
= (left
== right
&& seg_left
== seg_right
1154 && (seg_left
!= undefined_section
1155 || add_symbol
== op_symbol
)
1156 ? ~ (offsetT
) 0 : 0);
1157 if (symp
->sy_value
.X_op
== O_ne
)
1160 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1161 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1162 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1163 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1164 case O_logical_and
: left
= left
&& right
; break;
1165 case O_logical_or
: left
= left
|| right
; break;
1169 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1170 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1172 if (seg_left
== undefined_section
1173 || seg_right
== undefined_section
)
1174 final_seg
= undefined_section
;
1175 else if (seg_left
== absolute_section
)
1176 final_seg
= seg_right
;
1178 final_seg
= seg_left
;
1180 resolved
= (symbol_resolved_p (add_symbol
)
1181 && symbol_resolved_p (op_symbol
));
1187 /* Give an error (below) if not in expr_section. We don't
1188 want to worry about expr_section symbols, because they
1189 are fictional (they are created as part of expression
1190 resolution), and any problems may not actually mean
1195 symp
->sy_resolving
= 0;
1199 S_SET_VALUE (symp
, final_val
);
1201 exit_dont_set_value
:
1202 /* Always set the segment, even if not finalizing the value.
1203 The segment is used to determine whether a symbol is defined. */
1204 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
1205 /* The old a.out backend does not handle S_SET_SEGMENT correctly
1206 for a stab symbol, so we use this bad hack. */
1207 if (final_seg
!= S_GET_SEGMENT (symp
))
1209 S_SET_SEGMENT (symp
, final_seg
);
1211 /* Don't worry if we can't resolve an expr_section symbol. */
1215 symp
->sy_resolved
= 1;
1216 else if (S_GET_SEGMENT (symp
) != expr_section
)
1218 as_bad (_("can't resolve value for symbol `%s'"),
1220 symp
->sy_resolved
= 1;
1227 #ifdef BFD_ASSEMBLER
1229 static void resolve_local_symbol (const char *, PTR
);
1231 /* A static function passed to hash_traverse. */
1234 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED
, PTR value
)
1237 resolve_symbol_value (value
);
1242 /* Resolve all local symbols. */
1245 resolve_local_symbol_values (void)
1247 #ifdef BFD_ASSEMBLER
1248 hash_traverse (local_hash
, resolve_local_symbol
);
1252 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1253 They are *really* local. That is, they go out of scope whenever we see a
1254 label that isn't local. Also, like fb labels, there can be multiple
1255 instances of a dollar label. Therefor, we name encode each instance with
1256 the instance number, keep a list of defined symbols separate from the real
1257 symbol table, and we treat these buggers as a sparse array. */
1259 static long *dollar_labels
;
1260 static long *dollar_label_instances
;
1261 static char *dollar_label_defines
;
1262 static unsigned long dollar_label_count
;
1263 static unsigned long dollar_label_max
;
1266 dollar_label_defined (long label
)
1270 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1272 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1274 return dollar_label_defines
[i
- dollar_labels
];
1276 /* If we get here, label isn't defined. */
1281 dollar_label_instance (long label
)
1285 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1287 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1289 return (dollar_label_instances
[i
- dollar_labels
]);
1291 /* If we get here, we haven't seen the label before.
1292 Therefore its instance count is zero. */
1297 dollar_label_clear (void)
1299 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1302 #define DOLLAR_LABEL_BUMP_BY 10
1305 define_dollar_label (long label
)
1309 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1312 ++dollar_label_instances
[i
- dollar_labels
];
1313 dollar_label_defines
[i
- dollar_labels
] = 1;
1317 /* If we get to here, we don't have label listed yet. */
1319 if (dollar_labels
== NULL
)
1321 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1322 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
1323 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
1324 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1325 dollar_label_count
= 0;
1327 else if (dollar_label_count
== dollar_label_max
)
1329 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1330 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
1331 dollar_label_max
* sizeof (long));
1332 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
1333 dollar_label_max
* sizeof (long));
1334 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
1335 } /* if we needed to grow */
1337 dollar_labels
[dollar_label_count
] = label
;
1338 dollar_label_instances
[dollar_label_count
] = 1;
1339 dollar_label_defines
[dollar_label_count
] = 1;
1340 ++dollar_label_count
;
1343 /* Caller must copy returned name: we re-use the area for the next name.
1345 The mth occurence of label n: is turned into the symbol "Ln^Am"
1346 where n is the label number and m is the instance number. "L" makes
1347 it a label discarded unless debugging and "^A"('\1') ensures no
1348 ordinary symbol SHOULD get the same name as a local label
1349 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1351 fb labels get the same treatment, except that ^B is used in place
1354 char * /* Return local label name. */
1355 dollar_label_name (register long n
, /* we just saw "n$:" : n a number. */
1356 register int augend
/* 0 for current instance, 1 for new instance. */)
1359 /* Returned to caller, then copied. Used for created names ("4f"). */
1360 static char symbol_name_build
[24];
1363 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1366 know (augend
== 0 || augend
== 1);
1367 p
= symbol_name_build
;
1368 #ifdef LOCAL_LABEL_PREFIX
1369 *p
++ = LOCAL_LABEL_PREFIX
;
1373 /* Next code just does sprintf( {}, "%d", n); */
1375 q
= symbol_name_temporary
;
1376 for (*q
++ = 0, i
= n
; i
; ++q
)
1381 while ((*p
= *--q
) != '\0')
1384 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1386 /* Instance number. */
1387 q
= symbol_name_temporary
;
1388 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1393 while ((*p
++ = *--q
) != '\0');;
1395 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1396 return symbol_name_build
;
1399 /* Somebody else's idea of local labels. They are made by "n:" where n
1400 is any decimal digit. Refer to them with
1401 "nb" for previous (backward) n:
1402 or "nf" for next (forward) n:.
1404 We do a little better and let n be any number, not just a single digit, but
1405 since the other guy's assembler only does ten, we treat the first ten
1408 Like someone else's assembler, we have one set of local label counters for
1409 entire assembly, not one set per (sub)segment like in most assemblers. This
1410 implies that one can refer to a label in another segment, and indeed some
1411 crufty compilers have done just that.
1413 Since there could be a LOT of these things, treat them as a sparse
1416 #define FB_LABEL_SPECIAL (10)
1418 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1419 static long *fb_labels
;
1420 static long *fb_label_instances
;
1421 static long fb_label_count
;
1422 static long fb_label_max
;
1424 /* This must be more than FB_LABEL_SPECIAL. */
1425 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1428 fb_label_init (void)
1430 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1433 /* Add one to the instance number of this fb label. */
1436 fb_label_instance_inc (long label
)
1440 if (label
< FB_LABEL_SPECIAL
)
1442 ++fb_low_counter
[label
];
1446 if (fb_labels
!= NULL
)
1448 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1449 i
< fb_labels
+ fb_label_count
; ++i
)
1453 ++fb_label_instances
[i
- fb_labels
];
1455 } /* if we find it */
1456 } /* for each existing label */
1459 /* If we get to here, we don't have label listed yet. */
1461 if (fb_labels
== NULL
)
1463 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1464 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1465 fb_label_max
= FB_LABEL_BUMP_BY
;
1466 fb_label_count
= FB_LABEL_SPECIAL
;
1469 else if (fb_label_count
== fb_label_max
)
1471 fb_label_max
+= FB_LABEL_BUMP_BY
;
1472 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1473 fb_label_max
* sizeof (long));
1474 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1475 fb_label_max
* sizeof (long));
1476 } /* if we needed to grow */
1478 fb_labels
[fb_label_count
] = label
;
1479 fb_label_instances
[fb_label_count
] = 1;
1484 fb_label_instance (long label
)
1488 if (label
< FB_LABEL_SPECIAL
)
1490 return (fb_low_counter
[label
]);
1493 if (fb_labels
!= NULL
)
1495 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1496 i
< fb_labels
+ fb_label_count
; ++i
)
1500 return (fb_label_instances
[i
- fb_labels
]);
1501 } /* if we find it */
1502 } /* for each existing label */
1505 /* We didn't find the label, so this must be a reference to the
1510 /* Caller must copy returned name: we re-use the area for the next name.
1512 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1513 where n is the label number and m is the instance number. "L" makes
1514 it a label discarded unless debugging and "^B"('\2') ensures no
1515 ordinary symbol SHOULD get the same name as a local label
1516 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1518 dollar labels get the same treatment, except that ^A is used in
1521 char * /* Return local label name. */
1522 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
1523 long augend
/* 0 for nb, 1 for n:, nf. */)
1526 /* Returned to caller, then copied. Used for created names ("4f"). */
1527 static char symbol_name_build
[24];
1530 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1534 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
1536 know ((unsigned long) augend
<= 1);
1538 p
= symbol_name_build
;
1539 #ifdef LOCAL_LABEL_PREFIX
1540 *p
++ = LOCAL_LABEL_PREFIX
;
1544 /* Next code just does sprintf( {}, "%d", n); */
1546 q
= symbol_name_temporary
;
1547 for (*q
++ = 0, i
= n
; i
; ++q
)
1552 while ((*p
= *--q
) != '\0')
1555 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
1557 /* Instance number. */
1558 q
= symbol_name_temporary
;
1559 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1564 while ((*p
++ = *--q
) != '\0');;
1566 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1567 return (symbol_name_build
);
1570 /* Decode name that may have been generated by foo_label_name() above.
1571 If the name wasn't generated by foo_label_name(), then return it
1572 unaltered. This is used for error messages. */
1575 decode_local_label_name (char *s
)
1578 char *symbol_decode
;
1580 int instance_number
;
1582 const char *message_format
;
1585 #ifdef LOCAL_LABEL_PREFIX
1586 if (s
[index
] == LOCAL_LABEL_PREFIX
)
1590 if (s
[index
] != 'L')
1593 for (label_number
= 0, p
= s
+ index
+ 1; ISDIGIT (*p
); ++p
)
1594 label_number
= (10 * label_number
) + *p
- '0';
1596 if (*p
== DOLLAR_LABEL_CHAR
)
1598 else if (*p
== LOCAL_LABEL_CHAR
)
1603 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
1604 instance_number
= (10 * instance_number
) + *p
- '0';
1606 message_format
= _("\"%d\" (instance number %d of a %s label)");
1607 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1608 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1610 return symbol_decode
;
1613 /* Get the value of a symbol. */
1616 S_GET_VALUE (symbolS
*s
)
1618 #ifdef BFD_ASSEMBLER
1619 if (LOCAL_SYMBOL_CHECK (s
))
1620 return resolve_symbol_value (s
);
1623 if (!s
->sy_resolved
)
1625 valueT val
= resolve_symbol_value (s
);
1629 if (s
->sy_value
.X_op
!= O_constant
)
1631 static symbolS
*recur
;
1633 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1634 may call S_GET_VALUE. We use a static symbol to avoid the
1635 immediate recursion. */
1637 return (valueT
) s
->sy_value
.X_add_number
;
1639 if (! s
->sy_resolved
1640 || s
->sy_value
.X_op
!= O_symbol
1641 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1642 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1646 return (valueT
) s
->sy_value
.X_add_number
;
1649 /* Set the value of a symbol. */
1652 S_SET_VALUE (symbolS
*s
, valueT val
)
1654 #ifdef BFD_ASSEMBLER
1655 if (LOCAL_SYMBOL_CHECK (s
))
1657 ((struct local_symbol
*) s
)->lsy_value
= val
;
1662 s
->sy_value
.X_op
= O_constant
;
1663 s
->sy_value
.X_add_number
= (offsetT
) val
;
1664 s
->sy_value
.X_unsigned
= 0;
1668 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1670 if (LOCAL_SYMBOL_CHECK (dest
))
1671 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
1672 if (LOCAL_SYMBOL_CHECK (src
))
1673 src
= local_symbol_convert ((struct local_symbol
*) src
);
1675 #ifdef BFD_ASSEMBLER
1676 /* In an expression, transfer the settings of these flags.
1677 The user can override later, of course. */
1678 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1679 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1682 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1683 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1687 #ifdef BFD_ASSEMBLER
1690 S_IS_FUNCTION (symbolS
*s
)
1694 if (LOCAL_SYMBOL_CHECK (s
))
1697 flags
= s
->bsym
->flags
;
1699 return (flags
& BSF_FUNCTION
) != 0;
1703 S_IS_EXTERNAL (symbolS
*s
)
1707 if (LOCAL_SYMBOL_CHECK (s
))
1710 flags
= s
->bsym
->flags
;
1713 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
1716 return (flags
& BSF_GLOBAL
) != 0;
1720 S_IS_WEAK (symbolS
*s
)
1722 if (LOCAL_SYMBOL_CHECK (s
))
1724 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
1728 S_IS_COMMON (symbolS
*s
)
1730 if (LOCAL_SYMBOL_CHECK (s
))
1732 return bfd_is_com_section (s
->bsym
->section
);
1736 S_IS_DEFINED (symbolS
*s
)
1738 if (LOCAL_SYMBOL_CHECK (s
))
1739 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
1740 return s
->bsym
->section
!= undefined_section
;
1744 #ifndef EXTERN_FORCE_RELOC
1745 #define EXTERN_FORCE_RELOC IS_ELF
1748 /* Return true for symbols that should not be reduced to section
1749 symbols or eliminated from expressions, because they may be
1750 overridden by the linker. */
1752 S_FORCE_RELOC (symbolS
*s
, int strict
)
1754 if (LOCAL_SYMBOL_CHECK (s
))
1755 return ((struct local_symbol
*) s
)->lsy_section
== undefined_section
;
1758 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
1759 || (EXTERN_FORCE_RELOC
1760 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
1761 || s
->bsym
->section
== undefined_section
1762 || bfd_is_com_section (s
->bsym
->section
));
1766 S_IS_DEBUG (symbolS
*s
)
1768 if (LOCAL_SYMBOL_CHECK (s
))
1770 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1776 S_IS_LOCAL (symbolS
*s
)
1781 if (LOCAL_SYMBOL_CHECK (s
))
1784 flags
= s
->bsym
->flags
;
1787 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
1790 if (bfd_get_section (s
->bsym
) == reg_section
)
1793 if (flag_strip_local_absolute
1794 /* Keep BSF_FILE symbols in order to allow debuggers to identify
1795 the source file even when the object file is stripped. */
1796 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
1797 && bfd_get_section (s
->bsym
) == absolute_section
)
1800 name
= S_GET_NAME (s
);
1801 return (name
!= NULL
1803 && (strchr (name
, DOLLAR_LABEL_CHAR
)
1804 || strchr (name
, LOCAL_LABEL_CHAR
)
1805 || (! flag_keep_locals
1806 && (bfd_is_local_label (stdoutput
, s
->bsym
)
1809 && name
[1] == '?')))));
1813 S_IS_STABD (symbolS
*s
)
1815 return S_GET_NAME (s
) == 0;
1819 S_GET_NAME (symbolS
*s
)
1821 if (LOCAL_SYMBOL_CHECK (s
))
1822 return ((struct local_symbol
*) s
)->lsy_name
;
1823 return s
->bsym
->name
;
1827 S_GET_SEGMENT (symbolS
*s
)
1829 if (LOCAL_SYMBOL_CHECK (s
))
1830 return ((struct local_symbol
*) s
)->lsy_section
;
1831 return s
->bsym
->section
;
1835 S_SET_SEGMENT (symbolS
*s
, segT seg
)
1837 /* Don't reassign section symbols. The direct reason is to prevent seg
1838 faults assigning back to const global symbols such as *ABS*, but it
1839 shouldn't happen anyway. */
1841 if (LOCAL_SYMBOL_CHECK (s
))
1843 if (seg
== reg_section
)
1844 s
= local_symbol_convert ((struct local_symbol
*) s
);
1847 ((struct local_symbol
*) s
)->lsy_section
= seg
;
1852 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
1854 if (s
->bsym
->section
!= seg
)
1858 s
->bsym
->section
= seg
;
1862 S_SET_EXTERNAL (symbolS
*s
)
1864 if (LOCAL_SYMBOL_CHECK (s
))
1865 s
= local_symbol_convert ((struct local_symbol
*) s
);
1866 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1868 /* Let .weak override .global. */
1871 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
1876 /* Do not reassign section symbols. */
1877 as_where (& file
, & line
);
1878 as_warn_where (file
, line
,
1879 _("section symbols are already global"));
1882 s
->bsym
->flags
|= BSF_GLOBAL
;
1883 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
1886 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
1887 an_external_name
= S_GET_NAME (s
);
1892 S_CLEAR_EXTERNAL (symbolS
*s
)
1894 if (LOCAL_SYMBOL_CHECK (s
))
1896 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1898 /* Let .weak override. */
1901 s
->bsym
->flags
|= BSF_LOCAL
;
1902 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
1906 S_SET_WEAK (symbolS
*s
)
1908 if (LOCAL_SYMBOL_CHECK (s
))
1909 s
= local_symbol_convert ((struct local_symbol
*) s
);
1910 s
->bsym
->flags
|= BSF_WEAK
;
1911 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
1915 S_SET_THREAD_LOCAL (symbolS
*s
)
1917 if (LOCAL_SYMBOL_CHECK (s
))
1918 s
= local_symbol_convert ((struct local_symbol
*) s
);
1919 if (bfd_is_com_section (s
->bsym
->section
)
1920 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
1922 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
1923 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
1924 as_bad (_("Accessing function `%s' as thread-local object"),
1926 else if (! bfd_is_und_section (s
->bsym
->section
)
1927 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
1928 as_bad (_("Accessing `%s' as thread-local object"),
1933 S_SET_NAME (symbolS
*s
, const char *name
)
1935 if (LOCAL_SYMBOL_CHECK (s
))
1937 ((struct local_symbol
*) s
)->lsy_name
= name
;
1940 s
->bsym
->name
= name
;
1942 #endif /* BFD_ASSEMBLER */
1944 #ifdef SYMBOLS_NEED_BACKPOINTERS
1946 /* Return the previous symbol in a chain. */
1949 symbol_previous (symbolS
*s
)
1951 if (LOCAL_SYMBOL_CHECK (s
))
1953 return s
->sy_previous
;
1956 #endif /* SYMBOLS_NEED_BACKPOINTERS */
1958 /* Return the next symbol in a chain. */
1961 symbol_next (symbolS
*s
)
1963 if (LOCAL_SYMBOL_CHECK (s
))
1968 /* Return a pointer to the value of a symbol as an expression. */
1971 symbol_get_value_expression (symbolS
*s
)
1973 if (LOCAL_SYMBOL_CHECK (s
))
1974 s
= local_symbol_convert ((struct local_symbol
*) s
);
1975 return &s
->sy_value
;
1978 /* Set the value of a symbol to an expression. */
1981 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
1983 if (LOCAL_SYMBOL_CHECK (s
))
1984 s
= local_symbol_convert ((struct local_symbol
*) s
);
1988 /* Return a pointer to the X_add_number component of a symbol. */
1991 symbol_X_add_number (symbolS
*s
)
1993 #ifdef BFD_ASSEMBLER
1994 if (LOCAL_SYMBOL_CHECK (s
))
1995 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
1998 return &s
->sy_value
.X_add_number
;
2001 /* Set the value of SYM to the current position in the current segment. */
2004 symbol_set_value_now (symbolS
*sym
)
2006 S_SET_SEGMENT (sym
, now_seg
);
2007 S_SET_VALUE (sym
, frag_now_fix ());
2008 symbol_set_frag (sym
, frag_now
);
2011 /* Set the frag of a symbol. */
2014 symbol_set_frag (symbolS
*s
, fragS
*f
)
2016 #ifdef BFD_ASSEMBLER
2017 if (LOCAL_SYMBOL_CHECK (s
))
2019 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2026 /* Return the frag of a symbol. */
2029 symbol_get_frag (symbolS
*s
)
2031 #ifdef BFD_ASSEMBLER
2032 if (LOCAL_SYMBOL_CHECK (s
))
2033 return local_symbol_get_frag ((struct local_symbol
*) s
);
2038 /* Mark a symbol as having been used. */
2041 symbol_mark_used (symbolS
*s
)
2043 if (LOCAL_SYMBOL_CHECK (s
))
2048 /* Clear the mark of whether a symbol has been used. */
2051 symbol_clear_used (symbolS
*s
)
2053 if (LOCAL_SYMBOL_CHECK (s
))
2054 s
= local_symbol_convert ((struct local_symbol
*) s
);
2058 /* Return whether a symbol has been used. */
2061 symbol_used_p (symbolS
*s
)
2063 if (LOCAL_SYMBOL_CHECK (s
))
2068 /* Mark a symbol as having been used in a reloc. */
2071 symbol_mark_used_in_reloc (symbolS
*s
)
2073 if (LOCAL_SYMBOL_CHECK (s
))
2074 s
= local_symbol_convert ((struct local_symbol
*) s
);
2075 s
->sy_used_in_reloc
= 1;
2078 /* Clear the mark of whether a symbol has been used in a reloc. */
2081 symbol_clear_used_in_reloc (symbolS
*s
)
2083 if (LOCAL_SYMBOL_CHECK (s
))
2085 s
->sy_used_in_reloc
= 0;
2088 /* Return whether a symbol has been used in a reloc. */
2091 symbol_used_in_reloc_p (symbolS
*s
)
2093 if (LOCAL_SYMBOL_CHECK (s
))
2095 return s
->sy_used_in_reloc
;
2098 /* Mark a symbol as an MRI common symbol. */
2101 symbol_mark_mri_common (symbolS
*s
)
2103 if (LOCAL_SYMBOL_CHECK (s
))
2104 s
= local_symbol_convert ((struct local_symbol
*) s
);
2105 s
->sy_mri_common
= 1;
2108 /* Clear the mark of whether a symbol is an MRI common symbol. */
2111 symbol_clear_mri_common (symbolS
*s
)
2113 if (LOCAL_SYMBOL_CHECK (s
))
2115 s
->sy_mri_common
= 0;
2118 /* Return whether a symbol is an MRI common symbol. */
2121 symbol_mri_common_p (symbolS
*s
)
2123 if (LOCAL_SYMBOL_CHECK (s
))
2125 return s
->sy_mri_common
;
2128 /* Mark a symbol as having been written. */
2131 symbol_mark_written (symbolS
*s
)
2133 if (LOCAL_SYMBOL_CHECK (s
))
2138 /* Clear the mark of whether a symbol has been written. */
2141 symbol_clear_written (symbolS
*s
)
2143 if (LOCAL_SYMBOL_CHECK (s
))
2148 /* Return whether a symbol has been written. */
2151 symbol_written_p (symbolS
*s
)
2153 if (LOCAL_SYMBOL_CHECK (s
))
2158 /* Mark a symbol has having been resolved. */
2161 symbol_mark_resolved (symbolS
*s
)
2163 #ifdef BFD_ASSEMBLER
2164 if (LOCAL_SYMBOL_CHECK (s
))
2166 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2173 /* Return whether a symbol has been resolved. */
2176 symbol_resolved_p (symbolS
*s
)
2178 #ifdef BFD_ASSEMBLER
2179 if (LOCAL_SYMBOL_CHECK (s
))
2180 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2182 return s
->sy_resolved
;
2185 /* Return whether a symbol is a section symbol. */
2188 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2190 if (LOCAL_SYMBOL_CHECK (s
))
2192 #ifdef BFD_ASSEMBLER
2193 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2200 /* Return whether a symbol is equated to another symbol. */
2203 symbol_equated_p (symbolS
*s
)
2205 if (LOCAL_SYMBOL_CHECK (s
))
2207 return s
->sy_value
.X_op
== O_symbol
;
2210 /* Return whether a symbol is equated to another symbol, and should be
2211 treated specially when writing out relocs. */
2214 symbol_equated_reloc_p (symbolS
*s
)
2216 if (LOCAL_SYMBOL_CHECK (s
))
2218 /* X_op_symbol, normally not used for O_symbol, is set by
2219 resolve_symbol_value to flag expression syms that have been
2221 return (s
->sy_value
.X_op
== O_symbol
2222 #if defined (OBJ_COFF) && defined (TE_PE) && (defined(BFD_ASSEMBLER) || defined(S_IS_WEAK))
2225 && ((s
->sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2226 || ! S_IS_DEFINED (s
)
2227 || S_IS_COMMON (s
)));
2230 /* Return whether a symbol has a constant value. */
2233 symbol_constant_p (symbolS
*s
)
2235 if (LOCAL_SYMBOL_CHECK (s
))
2237 return s
->sy_value
.X_op
== O_constant
;
2240 #ifdef BFD_ASSEMBLER
2242 /* Return the BFD symbol for a symbol. */
2245 symbol_get_bfdsym (symbolS
*s
)
2247 if (LOCAL_SYMBOL_CHECK (s
))
2248 s
= local_symbol_convert ((struct local_symbol
*) s
);
2252 /* Set the BFD symbol for a symbol. */
2255 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2257 if (LOCAL_SYMBOL_CHECK (s
))
2258 s
= local_symbol_convert ((struct local_symbol
*) s
);
2259 /* Usually, it is harmless to reset a symbol to a BFD section
2260 symbol. For example, obj_elf_change_section sets the BFD symbol
2261 of an old symbol with the newly created section symbol. But when
2262 we have multiple sections with the same name, the newly created
2263 section may have the same name as an old section. We check if the
2264 old symbol has been already marked as a section symbol before
2266 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2268 /* else XXX - What do we do now ? */
2271 #endif /* BFD_ASSEMBLER */
2273 #ifdef OBJ_SYMFIELD_TYPE
2275 /* Get a pointer to the object format information for a symbol. */
2278 symbol_get_obj (symbolS
*s
)
2280 if (LOCAL_SYMBOL_CHECK (s
))
2281 s
= local_symbol_convert ((struct local_symbol
*) s
);
2285 /* Set the object format information for a symbol. */
2288 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2290 if (LOCAL_SYMBOL_CHECK (s
))
2291 s
= local_symbol_convert ((struct local_symbol
*) s
);
2295 #endif /* OBJ_SYMFIELD_TYPE */
2297 #ifdef TC_SYMFIELD_TYPE
2299 /* Get a pointer to the processor information for a symbol. */
2302 symbol_get_tc (symbolS
*s
)
2304 if (LOCAL_SYMBOL_CHECK (s
))
2305 s
= local_symbol_convert ((struct local_symbol
*) s
);
2309 /* Set the processor information for a symbol. */
2312 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
2314 if (LOCAL_SYMBOL_CHECK (s
))
2315 s
= local_symbol_convert ((struct local_symbol
*) s
);
2319 #endif /* TC_SYMFIELD_TYPE */
2324 symbol_lastP
= NULL
;
2325 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
2326 sy_hash
= hash_new ();
2327 #ifdef BFD_ASSEMBLER
2328 local_hash
= hash_new ();
2331 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
2332 #ifdef BFD_ASSEMBLER
2333 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2334 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
2337 /* Can't initialise a union. Sigh. */
2338 S_SET_SEGMENT (&abs_symbol
, absolute_section
);
2340 abs_symbol
.sy_value
.X_op
= O_constant
;
2341 abs_symbol
.sy_frag
= &zero_address_frag
;
2343 if (LOCAL_LABELS_FB
)
2349 /* Maximum indent level.
2350 Available for modification inside a gdb session. */
2351 static int max_indent_level
= 8;
2354 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
2356 const char *name
= S_GET_NAME (sym
);
2357 if (!name
|| !name
[0])
2359 fprintf (file
, "sym %lx %s", (unsigned long) sym
, name
);
2361 if (LOCAL_SYMBOL_CHECK (sym
))
2363 #ifdef BFD_ASSEMBLER
2364 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
2365 if (local_symbol_get_frag (locsym
) != &zero_address_frag
2366 && local_symbol_get_frag (locsym
) != NULL
)
2367 fprintf (file
, " frag %lx", (long) local_symbol_get_frag (locsym
));
2368 if (local_symbol_resolved_p (locsym
))
2369 fprintf (file
, " resolved");
2370 fprintf (file
, " local");
2375 if (sym
->sy_frag
!= &zero_address_frag
)
2376 fprintf (file
, " frag %lx", (long) sym
->sy_frag
);
2378 fprintf (file
, " written");
2379 if (sym
->sy_resolved
)
2380 fprintf (file
, " resolved");
2381 else if (sym
->sy_resolving
)
2382 fprintf (file
, " resolving");
2383 if (sym
->sy_used_in_reloc
)
2384 fprintf (file
, " used-in-reloc");
2386 fprintf (file
, " used");
2387 if (S_IS_LOCAL (sym
))
2388 fprintf (file
, " local");
2389 if (S_IS_EXTERNAL (sym
))
2390 fprintf (file
, " extern");
2391 if (S_IS_DEBUG (sym
))
2392 fprintf (file
, " debug");
2393 if (S_IS_DEFINED (sym
))
2394 fprintf (file
, " defined");
2396 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
2397 if (symbol_resolved_p (sym
))
2399 segT s
= S_GET_SEGMENT (sym
);
2401 if (s
!= undefined_section
2402 && s
!= expr_section
)
2403 fprintf (file
, " %lx", (long) S_GET_VALUE (sym
));
2405 else if (indent_level
< max_indent_level
2406 && S_GET_SEGMENT (sym
) != undefined_section
)
2409 fprintf (file
, "\n%*s<", indent_level
* 4, "");
2410 #ifdef BFD_ASSEMBLER
2411 if (LOCAL_SYMBOL_CHECK (sym
))
2412 fprintf (file
, "constant %lx",
2413 (long) ((struct local_symbol
*) sym
)->lsy_value
);
2416 print_expr_1 (file
, &sym
->sy_value
);
2417 fprintf (file
, ">");
2424 print_symbol_value (symbolS
*sym
)
2427 print_symbol_value_1 (stderr
, sym
);
2428 fprintf (stderr
, "\n");
2432 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
2435 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
2436 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2437 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2438 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2439 fprintf (file
, ">");
2444 print_expr_1 (FILE *file
, expressionS
*exp
)
2446 fprintf (file
, "expr %lx ", (long) exp
);
2450 fprintf (file
, "illegal");
2453 fprintf (file
, "absent");
2456 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
2460 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
2461 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2462 fprintf (file
, ">");
2464 if (exp
->X_add_number
)
2465 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
2466 (long) exp
->X_add_number
);
2470 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
2473 fprintf (file
, "big");
2476 fprintf (file
, "uminus -<");
2478 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2479 fprintf (file
, ">");
2480 goto maybe_print_addnum
;
2482 fprintf (file
, "bit_not");
2485 print_binary (file
, "multiply", exp
);
2488 print_binary (file
, "divide", exp
);
2491 print_binary (file
, "modulus", exp
);
2494 print_binary (file
, "lshift", exp
);
2497 print_binary (file
, "rshift", exp
);
2499 case O_bit_inclusive_or
:
2500 print_binary (file
, "bit_ior", exp
);
2502 case O_bit_exclusive_or
:
2503 print_binary (file
, "bit_xor", exp
);
2506 print_binary (file
, "bit_and", exp
);
2509 print_binary (file
, "eq", exp
);
2512 print_binary (file
, "ne", exp
);
2515 print_binary (file
, "lt", exp
);
2518 print_binary (file
, "le", exp
);
2521 print_binary (file
, "ge", exp
);
2524 print_binary (file
, "gt", exp
);
2527 print_binary (file
, "logical_and", exp
);
2530 print_binary (file
, "logical_or", exp
);
2534 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
2535 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2536 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2537 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2538 fprintf (file
, ">");
2539 goto maybe_print_addnum
;
2542 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
2543 print_symbol_value_1 (file
, exp
->X_add_symbol
);
2544 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
2545 print_symbol_value_1 (file
, exp
->X_op_symbol
);
2546 fprintf (file
, ">");
2547 goto maybe_print_addnum
;
2549 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
2556 print_expr (expressionS
*exp
)
2558 print_expr_1 (stderr
, exp
);
2559 fprintf (stderr
, "\n");
2563 symbol_print_statistics (FILE *file
)
2565 hash_print_statistics (file
, "symbol table", sy_hash
);
2566 #ifdef BFD_ASSEMBLER
2567 hash_print_statistics (file
, "mini local symbol table", local_hash
);
2568 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
2569 local_symbol_count
, local_symbol_conversion_count
);