1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
31 /* Whether the symbol is a local_symbol. */
32 unsigned int sy_local_symbol
: 1;
34 /* Weather symbol has been written. */
35 unsigned int sy_written
: 1;
37 /* Whether symbol value has been completely resolved (used during
38 final pass over symbol table). */
39 unsigned int sy_resolved
: 1;
41 /* Whether the symbol value is currently being resolved (used to
42 detect loops in symbol dependencies). */
43 unsigned int sy_resolving
: 1;
45 /* Whether the symbol value is used in a reloc. This is used to
46 ensure that symbols used in relocs are written out, even if they
47 are local and would otherwise not be. */
48 unsigned int sy_used_in_reloc
: 1;
50 /* Whether the symbol is used as an operand or in an expression.
51 NOTE: Not all the backends keep this information accurate;
52 backends which use this bit are responsible for setting it when
53 a symbol is used in backend routines. */
54 unsigned int sy_used
: 1;
56 /* Whether the symbol can be re-defined. */
57 unsigned int sy_volatile
: 1;
59 /* Whether the symbol is a forward reference. */
60 unsigned int sy_forward_ref
: 1;
62 /* This is set if the symbol is defined in an MRI common section.
63 We handle such sections as single common symbols, so symbols
64 defined within them must be treated specially by the relocation
66 unsigned int sy_mri_common
: 1;
68 /* This is set if the symbol is set with a .weakref directive. */
69 unsigned int sy_weakrefr
: 1;
71 /* This is set when the symbol is referenced as part of a .weakref
72 directive, but only if the symbol was not in the symbol table
73 before. It is cleared as soon as any direct reference to the
75 unsigned int sy_weakrefd
: 1;
78 /* The information we keep for a symbol. Note that the symbol table
79 holds pointers both to this and to local_symbol structures. See
85 struct symbol_flags sy_flags
;
90 /* The value of the symbol. */
93 /* Forwards and (optionally) backwards chain pointers. */
94 struct symbol
*sy_next
;
95 struct symbol
*sy_previous
;
97 /* Pointer to the frag this symbol is attached to, if any.
101 #ifdef OBJ_SYMFIELD_TYPE
102 OBJ_SYMFIELD_TYPE sy_obj
;
105 #ifdef TC_SYMFIELD_TYPE
106 TC_SYMFIELD_TYPE sy_tc
;
109 #ifdef TARGET_SYMBOL_FIELDS
114 /* A pointer in the symbol may point to either a complete symbol
115 (struct symbol above) or to a local symbol (struct local_symbol
116 defined here). The symbol code can detect the case by examining
117 the first field which is present in both structs.
119 We do this because we ordinarily only need a small amount of
120 information for a local symbol. The symbol table takes up a lot of
121 space, and storing less information for a local symbol can make a
122 big difference in assembler memory usage when assembling a large
127 /* Symbol flags. Only sy_local_symbol and sy_resolved are relevant. */
128 struct symbol_flags lsy_flags
;
130 /* The symbol section. This also serves as a flag. If this is
131 reg_section, then this symbol has been converted into a regular
132 symbol, and lsy_sym points to it. */
135 /* The symbol name. */
136 const char *lsy_name
;
138 /* The symbol frag or the real symbol, depending upon the value in
146 /* The value of the symbol. */
149 #ifdef TC_LOCAL_SYMFIELD_TYPE
150 TC_LOCAL_SYMFIELD_TYPE lsy_tc
;
156 const char *symbol_name
;
161 typedef struct symbol_entry symbol_entry_t
;
163 /* Hash function for a symbol_entry. */
166 hash_symbol_entry (const void *e
)
168 symbol_entry_t
*entry
= (symbol_entry_t
*) e
;
169 if (entry
->hash
== 0)
170 entry
->hash
= htab_hash_string (entry
->symbol_name
);
175 /* Equality function for a symbol_entry. */
178 eq_symbol_entry (const void *a
, const void *b
)
180 const symbol_entry_t
*ea
= (const symbol_entry_t
*) a
;
181 const symbol_entry_t
*eb
= (const symbol_entry_t
*) b
;
183 return strcmp (ea
->symbol_name
, eb
->symbol_name
) == 0;
186 static symbol_entry_t
*
187 symbol_entry_alloc (const char *symbol_name
, void *symbol
)
189 symbol_entry_t
*entry
= XNEW (symbol_entry_t
);
190 entry
->symbol_name
= symbol_name
;
192 entry
->symbol
= symbol
;
197 symbol_entry_find (htab_t table
, const char *symbol_name
)
199 symbol_entry_t needle
= { symbol_name
, 0, NULL
};
200 symbol_entry_t
*entry
= htab_find (table
, &needle
);
201 return entry
!= NULL
? entry
->symbol
: NULL
;
205 #define local_symbol_converted_p(l) ((l)->lsy_section == reg_section)
206 #define local_symbol_mark_converted(l) ((l)->lsy_section = reg_section)
207 #define local_symbol_resolved_p(l) ((l)->lsy_flags.sy_resolved)
208 #define local_symbol_mark_resolved(l) ((l)->lsy_flags.sy_resolved = 1)
209 #define local_symbol_get_frag(l) ((l)->u.lsy_frag)
210 #define local_symbol_set_frag(l, f) ((l)->u.lsy_frag = (f))
211 #define local_symbol_get_real_symbol(l) ((l)->u.lsy_sym)
212 #define local_symbol_set_real_symbol(l, s) ((l)->u.lsy_sym = (s))
214 /* This is non-zero if symbols are case sensitive, which is the
216 int symbols_case_sensitive
= 1;
218 #ifndef WORKING_DOT_WORD
219 extern int new_broken_words
;
222 /* symbol-name => struct symbol pointer */
223 static htab_t sy_hash
;
225 /* Table of local symbols. */
226 static htab_t local_hash
;
228 /* Below are commented in "symbols.h". */
229 symbolS
*symbol_rootP
;
230 symbolS
*symbol_lastP
;
235 #define debug_verify_symchain verify_symbol_chain
237 #define debug_verify_symchain(root, last) ((void) 0)
240 #define DOLLAR_LABEL_CHAR '\001'
241 #define LOCAL_LABEL_CHAR '\002'
243 #ifndef TC_LABEL_IS_LOCAL
244 #define TC_LABEL_IS_LOCAL(name) 0
247 struct obstack notes
;
249 /* The name of an external symbol which is
250 used to make weak PE symbol names unique. */
251 const char * an_external_name
;
254 static const char *save_symbol_name (const char *);
255 static void fb_label_init (void);
256 static long dollar_label_instance (long);
257 static long fb_label_instance (long);
259 static void print_binary (FILE *, const char *, expressionS
*);
261 /* Return a pointer to a new symbol. Die if we can't make a new
262 symbol. Fill in the symbol's values. Add symbol to end of symbol
265 This function should be called in the general case of creating a
266 symbol. However, if the output file symbol table has already been
267 set, and you are certain that this symbol won't be wanted in the
268 output file, you can call symbol_create. */
271 symbol_new (const char *name
, segT segment
, valueT valu
, fragS
*frag
)
273 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
275 /* Link to end of symbol chain. */
277 extern int symbol_table_frozen
;
278 if (symbol_table_frozen
)
281 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
286 /* Save a symbol name on a permanent obstack, and convert it according
287 to the object file format. */
290 save_symbol_name (const char *name
)
295 gas_assert (name
!= NULL
);
296 name_length
= strlen (name
) + 1; /* +1 for \0. */
297 obstack_grow (¬es
, name
, name_length
);
298 ret
= (char *) obstack_finish (¬es
);
300 #ifdef tc_canonicalize_symbol_name
301 ret
= tc_canonicalize_symbol_name (ret
);
304 if (! symbols_case_sensitive
)
308 for (s
= ret
; *s
!= '\0'; s
++)
316 symbol_create (const char *name
, /* It is copied, the caller can destroy/modify. */
317 segT segment
, /* Segment identifier (SEG_<something>). */
318 valueT valu
, /* Symbol value. */
319 fragS
*frag
/* Associated fragment. */)
321 const char *preserved_copy_of_name
;
324 preserved_copy_of_name
= save_symbol_name (name
);
326 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
328 /* symbol must be born in some fixed state. This seems as good as any. */
329 memset (symbolP
, 0, sizeof (symbolS
));
331 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
332 if (symbolP
->bsym
== NULL
)
333 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
334 S_SET_NAME (symbolP
, preserved_copy_of_name
);
336 S_SET_SEGMENT (symbolP
, segment
);
337 S_SET_VALUE (symbolP
, valu
);
338 symbol_clear_list_pointers (symbolP
);
340 symbolP
->sy_frag
= frag
;
342 obj_symbol_new_hook (symbolP
);
344 #ifdef tc_symbol_new_hook
345 tc_symbol_new_hook (symbolP
);
352 /* Local symbol support. If we can get away with it, we keep only a
353 small amount of information for local symbols. */
355 static symbolS
*local_symbol_convert (struct local_symbol
*);
357 /* Used for statistics. */
359 static unsigned long local_symbol_count
;
360 static unsigned long local_symbol_conversion_count
;
362 /* This macro is called with a symbol argument passed by reference.
363 It returns whether this is a local symbol. If necessary, it
364 changes its argument to the real symbol. */
366 #define LOCAL_SYMBOL_CHECK(s) \
367 (s->sy_flags.sy_local_symbol \
368 ? (local_symbol_converted_p ((struct local_symbol *) s) \
369 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
374 /* Create a local symbol and insert it into the local hash table. */
376 struct local_symbol
*
377 local_symbol_make (const char *name
, segT section
, valueT val
, fragS
*frag
)
379 const char *name_copy
;
380 struct local_symbol
*ret
;
381 struct symbol_flags flags
= { .sy_local_symbol
= 1, .sy_resolved
= 0 };
383 ++local_symbol_count
;
385 name_copy
= save_symbol_name (name
);
387 ret
= (struct local_symbol
*) obstack_alloc (¬es
, sizeof *ret
);
388 ret
->lsy_flags
= flags
;
389 ret
->lsy_name
= name_copy
;
390 ret
->lsy_section
= section
;
391 local_symbol_set_frag (ret
, frag
);
392 ret
->lsy_value
= val
;
394 htab_insert (local_hash
, symbol_entry_alloc (name_copy
, ret
));
399 /* Convert a local symbol into a real symbol. Note that we do not
400 reclaim the space used by the local symbol. */
403 local_symbol_convert (struct local_symbol
*locsym
)
407 gas_assert (locsym
->lsy_flags
.sy_local_symbol
);
408 if (local_symbol_converted_p (locsym
))
409 return local_symbol_get_real_symbol (locsym
);
411 ++local_symbol_conversion_count
;
413 ret
= symbol_new (locsym
->lsy_name
, locsym
->lsy_section
, locsym
->lsy_value
,
414 local_symbol_get_frag (locsym
));
416 if (local_symbol_resolved_p (locsym
))
417 ret
->sy_flags
.sy_resolved
= 1;
419 /* Local symbols are always either defined or used. */
420 ret
->sy_flags
.sy_used
= 1;
422 #ifdef TC_LOCAL_SYMFIELD_CONVERT
423 TC_LOCAL_SYMFIELD_CONVERT (locsym
, ret
);
426 symbol_table_insert (ret
);
428 local_symbol_mark_converted (locsym
);
429 local_symbol_set_real_symbol (locsym
, ret
);
431 htab_insert (local_hash
, symbol_entry_alloc (locsym
->lsy_name
, NULL
));
437 define_sym_at_dot (symbolS
*symbolP
)
439 symbolP
->sy_frag
= frag_now
;
440 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
441 S_SET_SEGMENT (symbolP
, now_seg
);
444 /* We have just seen "<name>:".
445 Creates a struct symbol unless it already exists.
447 Gripes if we are redefining a symbol incompatibly (and ignores it). */
450 colon (/* Just seen "x:" - rattle symbols & frags. */
451 const char *sym_name
/* Symbol name, as a canonical string. */
452 /* We copy this string: OK to alter later. */)
454 symbolS
*symbolP
; /* Symbol we are working with. */
456 /* Sun local labels go out of scope whenever a non-local symbol is
458 if (LOCAL_LABELS_DOLLAR
459 && !bfd_is_local_label_name (stdoutput
, sym_name
))
460 dollar_label_clear ();
462 #ifndef WORKING_DOT_WORD
463 if (new_broken_words
)
465 struct broken_word
*a
;
470 if (now_seg
== absolute_section
)
472 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name
);
476 possible_bytes
= (md_short_jump_size
477 + new_broken_words
* md_long_jump_size
);
480 frag_opcode
= frag_var (rs_broken_word
,
484 (symbolS
*) broken_words
,
488 /* We want to store the pointer to where to insert the jump
489 table in the fr_opcode of the rs_broken_word frag. This
490 requires a little hackery. */
492 && (frag_tmp
->fr_type
!= rs_broken_word
493 || frag_tmp
->fr_opcode
))
494 frag_tmp
= frag_tmp
->fr_next
;
496 frag_tmp
->fr_opcode
= frag_opcode
;
497 new_broken_words
= 0;
499 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
500 a
->dispfrag
= frag_tmp
;
502 #endif /* WORKING_DOT_WORD */
504 #ifdef obj_frob_colon
505 obj_frob_colon (sym_name
);
508 if ((symbolP
= symbol_find (sym_name
)) != 0)
510 S_CLEAR_WEAKREFR (symbolP
);
511 #ifdef RESOLVE_SYMBOL_REDEFINITION
512 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
515 /* Now check for undefined symbols. */
516 if (LOCAL_SYMBOL_CHECK (symbolP
))
518 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
520 if (locsym
->lsy_section
!= undefined_section
521 && (local_symbol_get_frag (locsym
) != frag_now
522 || locsym
->lsy_section
!= now_seg
523 || locsym
->lsy_value
!= frag_now_fix ()))
525 as_bad (_("symbol `%s' is already defined"), sym_name
);
529 locsym
->lsy_section
= now_seg
;
530 local_symbol_set_frag (locsym
, frag_now
);
531 locsym
->lsy_value
= frag_now_fix ();
533 else if (!(S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
534 || S_IS_COMMON (symbolP
)
535 || S_IS_VOLATILE (symbolP
))
537 if (S_IS_VOLATILE (symbolP
))
539 symbolP
= symbol_clone (symbolP
, 1);
540 S_SET_VALUE (symbolP
, 0);
541 S_CLEAR_VOLATILE (symbolP
);
543 if (S_GET_VALUE (symbolP
) == 0)
545 define_sym_at_dot (symbolP
);
548 #endif /* if we have one, it better be zero. */
553 /* There are still several cases to check:
555 A .comm/.lcomm symbol being redefined as initialized
558 A .comm/.lcomm symbol being redefined with a larger
561 This only used to be allowed on VMS gas, but Sun cc
562 on the sparc also depends on it. */
564 if (((!S_IS_DEBUG (symbolP
)
565 && (!S_IS_DEFINED (symbolP
) || S_IS_COMMON (symbolP
))
566 && S_IS_EXTERNAL (symbolP
))
567 || S_GET_SEGMENT (symbolP
) == bss_section
)
568 && (now_seg
== data_section
569 || now_seg
== bss_section
570 || now_seg
== S_GET_SEGMENT (symbolP
)))
572 /* Select which of the 2 cases this is. */
573 if (now_seg
!= data_section
)
575 /* New .comm for prev .comm symbol.
577 If the new size is larger we just change its
578 value. If the new size is smaller, we ignore
580 if (S_GET_VALUE (symbolP
)
581 < ((unsigned) frag_now_fix ()))
583 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
588 /* It is a .comm/.lcomm being converted to initialized
590 define_sym_at_dot (symbolP
);
595 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
596 static const char *od_buf
= "";
600 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
601 sprintf (od_buf
, "%d.%d.",
602 S_GET_OTHER (symbolP
),
603 S_GET_DESC (symbolP
));
605 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
607 segment_name (S_GET_SEGMENT (symbolP
)),
609 (long) S_GET_VALUE (symbolP
));
611 } /* if the undefined symbol has no value */
615 /* Don't blow up if the definition is the same. */
616 if (!(frag_now
== symbolP
->sy_frag
617 && S_GET_VALUE (symbolP
) == frag_now_fix ()
618 && S_GET_SEGMENT (symbolP
) == now_seg
))
620 as_bad (_("symbol `%s' is already defined"), sym_name
);
621 symbolP
= symbol_clone (symbolP
, 0);
622 define_sym_at_dot (symbolP
);
627 else if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, sym_name
))
629 symbolP
= (symbolS
*) local_symbol_make (sym_name
, now_seg
,
630 (valueT
) frag_now_fix (),
635 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
638 symbol_table_insert (symbolP
);
641 if (mri_common_symbol
!= NULL
)
643 /* This symbol is actually being defined within an MRI common
644 section. This requires special handling. */
645 if (LOCAL_SYMBOL_CHECK (symbolP
))
646 symbolP
= local_symbol_convert ((struct local_symbol
*) symbolP
);
647 symbolP
->sy_value
.X_op
= O_symbol
;
648 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
649 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
650 symbolP
->sy_frag
= &zero_address_frag
;
651 S_SET_SEGMENT (symbolP
, expr_section
);
652 symbolP
->sy_flags
.sy_mri_common
= 1;
656 tc_frob_label (symbolP
);
658 #ifdef obj_frob_label
659 obj_frob_label (symbolP
);
665 /* Die if we can't insert the symbol. */
668 symbol_table_insert (symbolS
*symbolP
)
671 know (S_GET_NAME (symbolP
));
673 if (LOCAL_SYMBOL_CHECK (symbolP
))
674 htab_insert (local_hash
,
675 symbol_entry_alloc (S_GET_NAME (symbolP
),
676 (struct local_symbol
*)symbolP
));
678 htab_insert (sy_hash
, symbol_entry_alloc (S_GET_NAME (symbolP
),
679 (struct local_symbol
*)symbolP
));
682 /* If a symbol name does not exist, create it as undefined, and insert
683 it into the symbol table. Return a pointer to it. */
686 symbol_find_or_make (const char *name
)
690 symbolP
= symbol_find (name
);
694 if (! flag_keep_locals
&& bfd_is_local_label_name (stdoutput
, name
))
696 symbolP
= md_undefined_symbol ((char *) name
);
700 symbolP
= (symbolS
*) local_symbol_make (name
, undefined_section
,
706 symbolP
= symbol_make (name
);
708 symbol_table_insert (symbolP
);
709 } /* if symbol wasn't found */
715 symbol_make (const char *name
)
719 /* Let the machine description default it, e.g. for register names. */
720 symbolP
= md_undefined_symbol ((char *) name
);
723 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
729 symbol_clone (symbolS
*orgsymP
, int replace
)
732 asymbol
*bsymorg
, *bsymnew
;
734 /* Make sure we never clone the dot special symbol. */
735 gas_assert (orgsymP
!= &dot_symbol
);
737 /* Running local_symbol_convert on a clone that's not the one currently
738 in local_hash would incorrectly replace the hash entry. Thus the
739 symbol must be converted here. Note that the rest of the function
740 depends on not encountering an unconverted symbol. */
741 if (LOCAL_SYMBOL_CHECK (orgsymP
))
742 orgsymP
= local_symbol_convert ((struct local_symbol
*) orgsymP
);
743 bsymorg
= orgsymP
->bsym
;
745 newsymP
= (symbolS
*) obstack_alloc (¬es
, sizeof (*newsymP
));
747 bsymnew
= bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg
));
749 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
750 newsymP
->bsym
= bsymnew
;
751 bsymnew
->name
= bsymorg
->name
;
752 bsymnew
->flags
= bsymorg
->flags
& ~BSF_SECTION_SYM
;
753 bsymnew
->section
= bsymorg
->section
;
754 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg
), bsymorg
,
755 bfd_asymbol_bfd (bsymnew
), bsymnew
);
757 #ifdef obj_symbol_clone_hook
758 obj_symbol_clone_hook (newsymP
, orgsymP
);
761 #ifdef tc_symbol_clone_hook
762 tc_symbol_clone_hook (newsymP
, orgsymP
);
767 if (symbol_rootP
== orgsymP
)
768 symbol_rootP
= newsymP
;
769 else if (orgsymP
->sy_previous
)
771 orgsymP
->sy_previous
->sy_next
= newsymP
;
772 orgsymP
->sy_previous
= NULL
;
774 if (symbol_lastP
== orgsymP
)
775 symbol_lastP
= newsymP
;
776 else if (orgsymP
->sy_next
)
777 orgsymP
->sy_next
->sy_previous
= newsymP
;
779 /* Symbols that won't be output can't be external. */
780 S_CLEAR_EXTERNAL (orgsymP
);
781 orgsymP
->sy_previous
= orgsymP
->sy_next
= orgsymP
;
782 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
784 symbol_table_insert (newsymP
);
788 /* Symbols that won't be output can't be external. */
789 S_CLEAR_EXTERNAL (newsymP
);
790 newsymP
->sy_previous
= newsymP
->sy_next
= newsymP
;
796 /* If S is a local symbol that has been converted, return the
797 converted symbol. Otherwise return S. */
799 static inline symbolS
*
800 get_real_sym (symbolS
*s
)
803 && s
->sy_flags
.sy_local_symbol
804 && local_symbol_converted_p ((struct local_symbol
*) s
))
805 s
= local_symbol_get_real_symbol ((struct local_symbol
*) s
);
809 /* Referenced symbols, if they are forward references, need to be cloned
810 (without replacing the original) so that the value of the referenced
811 symbols at the point of use is saved by the clone. */
813 #undef symbol_clone_if_forward_ref
815 symbol_clone_if_forward_ref (symbolS
*symbolP
, int is_forward
)
817 if (symbolP
&& !LOCAL_SYMBOL_CHECK (symbolP
))
819 symbolS
*orig_add_symbol
= get_real_sym (symbolP
->sy_value
.X_add_symbol
);
820 symbolS
*orig_op_symbol
= get_real_sym (symbolP
->sy_value
.X_op_symbol
);
821 symbolS
*add_symbol
= orig_add_symbol
;
822 symbolS
*op_symbol
= orig_op_symbol
;
824 if (symbolP
->sy_flags
.sy_forward_ref
)
829 /* assign_symbol() clones volatile symbols; pre-existing expressions
830 hold references to the original instance, but want the current
831 value. Just repeat the lookup. */
832 if (add_symbol
&& S_IS_VOLATILE (add_symbol
))
833 add_symbol
= symbol_find_exact (S_GET_NAME (add_symbol
));
834 if (op_symbol
&& S_IS_VOLATILE (op_symbol
))
835 op_symbol
= symbol_find_exact (S_GET_NAME (op_symbol
));
838 /* Re-using sy_resolving here, as this routine cannot get called from
839 symbol resolution code. */
840 if ((symbolP
->bsym
->section
== expr_section
841 || symbolP
->sy_flags
.sy_forward_ref
)
842 && !symbolP
->sy_flags
.sy_resolving
)
844 symbolP
->sy_flags
.sy_resolving
= 1;
845 add_symbol
= symbol_clone_if_forward_ref (add_symbol
, is_forward
);
846 op_symbol
= symbol_clone_if_forward_ref (op_symbol
, is_forward
);
847 symbolP
->sy_flags
.sy_resolving
= 0;
850 if (symbolP
->sy_flags
.sy_forward_ref
851 || add_symbol
!= orig_add_symbol
852 || op_symbol
!= orig_op_symbol
)
854 if (symbolP
!= &dot_symbol
)
856 symbolP
= symbol_clone (symbolP
, 0);
857 symbolP
->sy_flags
.sy_resolving
= 0;
861 symbolP
= symbol_temp_new_now ();
862 #ifdef tc_new_dot_label
863 tc_new_dot_label (symbolP
);
868 symbolP
->sy_value
.X_add_symbol
= add_symbol
;
869 symbolP
->sy_value
.X_op_symbol
= op_symbol
;
876 symbol_temp_new (segT seg
, valueT ofs
, fragS
*frag
)
878 return symbol_new (FAKE_LABEL_NAME
, seg
, ofs
, frag
);
882 symbol_temp_new_now (void)
884 return symbol_temp_new (now_seg
, frag_now_fix (), frag_now
);
888 symbol_temp_new_now_octets (void)
890 return symbol_temp_new (now_seg
, frag_now_fix_octets (), frag_now
);
894 symbol_temp_make (void)
896 return symbol_make (FAKE_LABEL_NAME
);
899 /* Implement symbol table lookup.
900 In: A symbol's name as a string: '\0' can't be part of a symbol name.
901 Out: NULL if the name was not in the symbol table, else the address
902 of a struct symbol associated with that name. */
905 symbol_find_exact (const char *name
)
907 return symbol_find_exact_noref (name
, 0);
911 symbol_find_exact_noref (const char *name
, int noref
)
913 symbolS
*sym
= symbol_entry_find (local_hash
, name
);
917 sym
= symbol_entry_find (sy_hash
, name
);
919 /* Any references to the symbol, except for the reference in
920 .weakref, must clear this flag, such that the symbol does not
921 turn into a weak symbol. Note that we don't have to handle the
922 local_symbol case, since a weakrefd is always promoted out of the
923 local_symbol table when it is turned into a weak symbol. */
925 S_CLEAR_WEAKREFD (sym
);
931 symbol_find (const char *name
)
933 return symbol_find_noref (name
, 0);
937 symbol_find_noref (const char *name
, int noref
)
942 #ifdef tc_canonicalize_symbol_name
944 copy
= xstrdup (name
);
945 name
= tc_canonicalize_symbol_name (copy
);
949 if (! symbols_case_sensitive
)
958 name
= copy
= XNEWVEC (char, strlen (name
) + 1);
960 while ((c
= *orig
++) != '\0')
961 *copy
++ = TOUPPER (c
);
965 copy
= (char *) name
;
968 result
= symbol_find_exact_noref (name
, noref
);
973 /* Once upon a time, symbols were kept in a singly linked list. At
974 least coff needs to be able to rearrange them from time to time, for
975 which a doubly linked list is much more convenient. Loic did these
976 as macros which seemed dangerous to me so they're now functions.
979 /* Link symbol ADDME after symbol TARGET in the chain. */
982 symbol_append (symbolS
*addme
, symbolS
*target
,
983 symbolS
**rootPP
, symbolS
**lastPP
)
985 if (LOCAL_SYMBOL_CHECK (addme
))
987 if (target
!= NULL
&& LOCAL_SYMBOL_CHECK (target
))
992 know (*rootPP
== NULL
);
993 know (*lastPP
== NULL
);
994 addme
->sy_next
= NULL
;
995 addme
->sy_previous
= NULL
;
999 } /* if the list is empty */
1001 if (target
->sy_next
!= NULL
)
1003 target
->sy_next
->sy_previous
= addme
;
1007 know (*lastPP
== target
);
1009 } /* if we have a next */
1011 addme
->sy_next
= target
->sy_next
;
1012 target
->sy_next
= addme
;
1013 addme
->sy_previous
= target
;
1015 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
1018 /* Set the chain pointers of SYMBOL to null. */
1021 symbol_clear_list_pointers (symbolS
*symbolP
)
1023 if (LOCAL_SYMBOL_CHECK (symbolP
))
1025 symbolP
->sy_next
= NULL
;
1026 symbolP
->sy_previous
= NULL
;
1029 /* Remove SYMBOLP from the list. */
1032 symbol_remove (symbolS
*symbolP
, symbolS
**rootPP
, symbolS
**lastPP
)
1034 if (LOCAL_SYMBOL_CHECK (symbolP
))
1037 if (symbolP
== *rootPP
)
1039 *rootPP
= symbolP
->sy_next
;
1040 } /* if it was the root */
1042 if (symbolP
== *lastPP
)
1044 *lastPP
= symbolP
->sy_previous
;
1045 } /* if it was the tail */
1047 if (symbolP
->sy_next
!= NULL
)
1049 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
1052 if (symbolP
->sy_previous
!= NULL
)
1054 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
1055 } /* if not first */
1057 debug_verify_symchain (*rootPP
, *lastPP
);
1060 /* Link symbol ADDME before symbol TARGET in the chain. */
1063 symbol_insert (symbolS
*addme
, symbolS
*target
,
1064 symbolS
**rootPP
, symbolS
**lastPP ATTRIBUTE_UNUSED
)
1066 if (LOCAL_SYMBOL_CHECK (addme
))
1068 if (LOCAL_SYMBOL_CHECK (target
))
1071 if (target
->sy_previous
!= NULL
)
1073 target
->sy_previous
->sy_next
= addme
;
1077 know (*rootPP
== target
);
1079 } /* if not first */
1081 addme
->sy_previous
= target
->sy_previous
;
1082 target
->sy_previous
= addme
;
1083 addme
->sy_next
= target
;
1085 debug_verify_symchain (*rootPP
, *lastPP
);
1089 verify_symbol_chain (symbolS
*rootP
, symbolS
*lastP
)
1091 symbolS
*symbolP
= rootP
;
1093 if (symbolP
== NULL
)
1096 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
1098 gas_assert (symbolP
->bsym
!= NULL
);
1099 gas_assert (symbolP
->sy_flags
.sy_local_symbol
== 0);
1100 gas_assert (symbolP
->sy_next
->sy_previous
== symbolP
);
1103 gas_assert (lastP
== symbolP
);
1107 symbol_on_chain (symbolS
*s
, symbolS
*rootPP
, symbolS
*lastPP
)
1109 return (!LOCAL_SYMBOL_CHECK (s
)
1110 && ((s
->sy_next
!= s
1111 && s
->sy_next
!= NULL
1112 && s
->sy_next
->sy_previous
== s
)
1114 && ((s
->sy_previous
!= s
1115 && s
->sy_previous
!= NULL
1116 && s
->sy_previous
->sy_next
== s
)
1120 #ifdef OBJ_COMPLEX_RELC
1123 use_complex_relocs_for (symbolS
* symp
)
1125 switch (symp
->sy_value
.X_op
)
1135 case O_bit_inclusive_or
:
1137 case O_bit_exclusive_or
:
1149 if ((S_IS_COMMON (symp
->sy_value
.X_op_symbol
)
1150 || S_IS_LOCAL (symp
->sy_value
.X_op_symbol
))
1151 && S_IS_DEFINED (symp
->sy_value
.X_op_symbol
)
1152 && S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) != expr_section
)
1159 if ((S_IS_COMMON (symp
->sy_value
.X_add_symbol
)
1160 || S_IS_LOCAL (symp
->sy_value
.X_add_symbol
))
1161 && S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
1162 && S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
) != expr_section
)
1175 report_op_error (symbolS
*symp
, symbolS
*left
, operatorT op
, symbolS
*right
)
1179 segT seg_left
= left
? S_GET_SEGMENT (left
) : 0;
1180 segT seg_right
= S_GET_SEGMENT (right
);
1189 case O_uminus
: opname
= "-"; break;
1190 case O_bit_not
: opname
= "~"; break;
1191 case O_logical_not
: opname
= "!"; break;
1192 case O_multiply
: opname
= "*"; break;
1193 case O_divide
: opname
= "/"; break;
1194 case O_modulus
: opname
= "%"; break;
1195 case O_left_shift
: opname
= "<<"; break;
1196 case O_right_shift
: opname
= ">>"; break;
1197 case O_bit_inclusive_or
: opname
= "|"; break;
1198 case O_bit_or_not
: opname
= "|~"; break;
1199 case O_bit_exclusive_or
: opname
= "^"; break;
1200 case O_bit_and
: opname
= "&"; break;
1201 case O_add
: opname
= "+"; break;
1202 case O_subtract
: opname
= "-"; break;
1203 case O_eq
: opname
= "=="; break;
1204 case O_ne
: opname
= "!="; break;
1205 case O_lt
: opname
= "<"; break;
1206 case O_le
: opname
= "<="; break;
1207 case O_ge
: opname
= ">="; break;
1208 case O_gt
: opname
= ">"; break;
1209 case O_logical_and
: opname
= "&&"; break;
1210 case O_logical_or
: opname
= "||"; break;
1213 if (expr_symbol_where (symp
, &file
, &line
))
1216 as_bad_where (file
, line
,
1217 _("invalid operands (%s and %s sections) for `%s'"),
1218 seg_left
->name
, seg_right
->name
, opname
);
1220 as_bad_where (file
, line
,
1221 _("invalid operand (%s section) for `%s'"),
1222 seg_right
->name
, opname
);
1226 const char *sname
= S_GET_NAME (symp
);
1229 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1230 seg_left
->name
, seg_right
->name
, opname
, sname
);
1232 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1233 seg_right
->name
, opname
, sname
);
1237 /* Resolve the value of a symbol. This is called during the final
1238 pass over the symbol table to resolve any symbols with complex
1242 resolve_symbol_value (symbolS
*symp
)
1248 if (LOCAL_SYMBOL_CHECK (symp
))
1250 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1252 final_val
= locsym
->lsy_value
;
1253 if (local_symbol_resolved_p (locsym
))
1256 /* Symbols whose section has SEC_ELF_OCTETS set,
1257 resolve to octets instead of target bytes. */
1258 if (locsym
->lsy_section
->flags
& SEC_OCTETS
)
1259 final_val
+= local_symbol_get_frag (locsym
)->fr_address
;
1261 final_val
+= (local_symbol_get_frag (locsym
)->fr_address
1266 locsym
->lsy_value
= final_val
;
1267 local_symbol_mark_resolved (locsym
);
1273 if (symp
->sy_flags
.sy_resolved
)
1276 while (symp
->sy_value
.X_op
== O_symbol
)
1278 final_val
+= symp
->sy_value
.X_add_number
;
1279 symp
= symp
->sy_value
.X_add_symbol
;
1280 if (LOCAL_SYMBOL_CHECK (symp
))
1282 struct local_symbol
*locsym
= (struct local_symbol
*) symp
;
1283 final_val
+= locsym
->lsy_value
;
1286 if (!symp
->sy_flags
.sy_resolved
)
1289 if (symp
->sy_value
.X_op
== O_constant
)
1290 final_val
+= symp
->sy_value
.X_add_number
;
1297 final_seg
= S_GET_SEGMENT (symp
);
1299 if (symp
->sy_flags
.sy_resolving
)
1302 as_bad (_("symbol definition loop encountered at `%s'"),
1307 #ifdef OBJ_COMPLEX_RELC
1308 else if (final_seg
== expr_section
1309 && use_complex_relocs_for (symp
))
1311 symbolS
* relc_symbol
= NULL
;
1312 char * relc_symbol_name
= NULL
;
1314 relc_symbol_name
= symbol_relc_make_expr (& symp
->sy_value
);
1316 /* For debugging, print out conversion input & output. */
1318 print_expr (& symp
->sy_value
);
1319 if (relc_symbol_name
)
1320 fprintf (stderr
, "-> relc symbol: %s\n", relc_symbol_name
);
1323 if (relc_symbol_name
!= NULL
)
1324 relc_symbol
= symbol_new (relc_symbol_name
, undefined_section
,
1325 0, & zero_address_frag
);
1327 if (relc_symbol
== NULL
)
1329 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1335 symbol_table_insert (relc_symbol
);
1337 /* S_CLEAR_EXTERNAL (relc_symbol); */
1338 if (symp
->bsym
->flags
& BSF_SRELC
)
1339 relc_symbol
->bsym
->flags
|= BSF_SRELC
;
1341 relc_symbol
->bsym
->flags
|= BSF_RELC
;
1342 /* symp->bsym->flags |= BSF_RELC; */
1343 copy_symbol_attributes (symp
, relc_symbol
);
1344 symp
->sy_value
.X_op
= O_symbol
;
1345 symp
->sy_value
.X_add_symbol
= relc_symbol
;
1346 symp
->sy_value
.X_add_number
= 0;
1351 final_seg
= undefined_section
;
1352 goto exit_dont_set_value
;
1357 symbolS
*add_symbol
, *op_symbol
;
1358 offsetT left
, right
;
1359 segT seg_left
, seg_right
;
1363 symp
->sy_flags
.sy_resolving
= 1;
1365 /* Help out with CSE. */
1366 add_symbol
= symp
->sy_value
.X_add_symbol
;
1367 op_symbol
= symp
->sy_value
.X_op_symbol
;
1368 final_val
= symp
->sy_value
.X_add_number
;
1369 op
= symp
->sy_value
.X_op
;
1382 /* Symbols whose section has SEC_ELF_OCTETS set,
1383 resolve to octets instead of target bytes. */
1384 if (symp
->bsym
->section
->flags
& SEC_OCTETS
)
1385 final_val
+= symp
->sy_frag
->fr_address
;
1387 final_val
+= symp
->sy_frag
->fr_address
/ OCTETS_PER_BYTE
;
1388 if (final_seg
== expr_section
)
1389 final_seg
= absolute_section
;
1398 left
= resolve_symbol_value (add_symbol
);
1399 seg_left
= S_GET_SEGMENT (add_symbol
);
1401 symp
->sy_value
.X_op_symbol
= NULL
;
1404 if (S_IS_WEAKREFR (symp
))
1406 gas_assert (final_val
== 0);
1407 if (S_IS_WEAKREFR (add_symbol
))
1409 gas_assert (add_symbol
->sy_value
.X_op
== O_symbol
1410 && add_symbol
->sy_value
.X_add_number
== 0);
1411 add_symbol
= add_symbol
->sy_value
.X_add_symbol
;
1412 gas_assert (! S_IS_WEAKREFR (add_symbol
));
1413 symp
->sy_value
.X_add_symbol
= add_symbol
;
1417 if (symp
->sy_flags
.sy_mri_common
)
1419 /* This is a symbol inside an MRI common section. The
1420 relocation routines are going to handle it specially.
1421 Don't change the value. */
1422 resolved
= symbol_resolved_p (add_symbol
);
1426 /* Don't leave symbol loops. */
1428 && !LOCAL_SYMBOL_CHECK (add_symbol
)
1429 && add_symbol
->sy_flags
.sy_resolving
)
1432 if (finalize_syms
&& final_val
== 0)
1434 if (LOCAL_SYMBOL_CHECK (add_symbol
))
1435 add_symbol
= local_symbol_convert ((struct local_symbol
*)
1437 copy_symbol_attributes (symp
, add_symbol
);
1440 /* If we have equated this symbol to an undefined or common
1441 symbol, keep X_op set to O_symbol, and don't change
1442 X_add_number. This permits the routine which writes out
1443 relocation to detect this case, and convert the
1444 relocation to be against the symbol to which this symbol
1446 if (seg_left
== undefined_section
1447 || bfd_is_com_section (seg_left
)
1448 #if defined (OBJ_COFF) && defined (TE_PE)
1449 || S_IS_WEAK (add_symbol
)
1452 && ((final_seg
== expr_section
1453 && seg_left
!= expr_section
1454 && seg_left
!= absolute_section
)
1455 || symbol_shadow_p (symp
))))
1459 symp
->sy_value
.X_op
= O_symbol
;
1460 symp
->sy_value
.X_add_symbol
= add_symbol
;
1461 symp
->sy_value
.X_add_number
= final_val
;
1462 /* Use X_op_symbol as a flag. */
1463 symp
->sy_value
.X_op_symbol
= add_symbol
;
1465 final_seg
= seg_left
;
1466 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1467 resolved
= symbol_resolved_p (add_symbol
);
1468 symp
->sy_flags
.sy_resolving
= 0;
1469 goto exit_dont_set_value
;
1473 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1474 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1475 final_seg
= seg_left
;
1478 resolved
= symbol_resolved_p (add_symbol
);
1479 if (S_IS_WEAKREFR (symp
))
1481 symp
->sy_flags
.sy_resolving
= 0;
1482 goto exit_dont_set_value
;
1489 left
= resolve_symbol_value (add_symbol
);
1490 seg_left
= S_GET_SEGMENT (add_symbol
);
1492 /* By reducing these to the relevant dyadic operator, we get
1493 !S -> S == 0 permitted on anything,
1494 -S -> 0 - S only permitted on absolute
1495 ~S -> S ^ ~0 only permitted on absolute */
1496 if (op
!= O_logical_not
&& seg_left
!= absolute_section
1498 report_op_error (symp
, NULL
, op
, add_symbol
);
1500 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1501 final_seg
= absolute_section
;
1505 else if (op
== O_logical_not
)
1510 final_val
+= left
+ symp
->sy_frag
->fr_address
;
1512 resolved
= symbol_resolved_p (add_symbol
);
1520 case O_bit_inclusive_or
:
1522 case O_bit_exclusive_or
:
1534 left
= resolve_symbol_value (add_symbol
);
1535 right
= resolve_symbol_value (op_symbol
);
1536 seg_left
= S_GET_SEGMENT (add_symbol
);
1537 seg_right
= S_GET_SEGMENT (op_symbol
);
1539 /* Simplify addition or subtraction of a constant by folding the
1540 constant into X_add_number. */
1543 if (seg_right
== absolute_section
)
1548 else if (seg_left
== absolute_section
)
1551 add_symbol
= op_symbol
;
1553 seg_left
= seg_right
;
1557 else if (op
== O_subtract
)
1559 if (seg_right
== absolute_section
)
1567 /* Equality and non-equality tests are permitted on anything.
1568 Subtraction, and other comparison operators are permitted if
1569 both operands are in the same section. Otherwise, both
1570 operands must be absolute. We already handled the case of
1571 addition or subtraction of a constant above. This will
1572 probably need to be changed for an object file format which
1573 supports arbitrary expressions. */
1574 if (!(seg_left
== absolute_section
1575 && seg_right
== absolute_section
)
1576 && !(op
== O_eq
|| op
== O_ne
)
1577 && !((op
== O_subtract
1578 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
1579 && seg_left
== seg_right
1580 && (seg_left
!= undefined_section
1581 || add_symbol
== op_symbol
)))
1583 /* Don't emit messages unless we're finalizing the symbol value,
1584 otherwise we may get the same message multiple times. */
1586 report_op_error (symp
, add_symbol
, op
, op_symbol
);
1587 /* However do not move the symbol into the absolute section
1588 if it cannot currently be resolved - this would confuse
1589 other parts of the assembler into believing that the
1590 expression had been evaluated to zero. */
1596 && (final_seg
== expr_section
|| final_seg
== undefined_section
))
1597 final_seg
= absolute_section
;
1599 /* Check for division by zero. */
1600 if ((op
== O_divide
|| op
== O_modulus
) && right
== 0)
1602 /* If seg_right is not absolute_section, then we've
1603 already issued a warning about using a bad symbol. */
1604 if (seg_right
== absolute_section
&& finalize_syms
)
1609 if (expr_symbol_where (symp
, &file
, &line
))
1610 as_bad_where (file
, line
, _("division by zero"));
1612 as_bad (_("division by zero when setting `%s'"),
1619 switch (symp
->sy_value
.X_op
)
1621 case O_multiply
: left
*= right
; break;
1622 case O_divide
: left
/= right
; break;
1623 case O_modulus
: left
%= right
; break;
1624 case O_left_shift
: left
<<= right
; break;
1625 case O_right_shift
: left
>>= right
; break;
1626 case O_bit_inclusive_or
: left
|= right
; break;
1627 case O_bit_or_not
: left
|= ~right
; break;
1628 case O_bit_exclusive_or
: left
^= right
; break;
1629 case O_bit_and
: left
&= right
; break;
1630 case O_add
: left
+= right
; break;
1631 case O_subtract
: left
-= right
; break;
1634 left
= (left
== right
&& seg_left
== seg_right
1635 && (seg_left
!= undefined_section
1636 || add_symbol
== op_symbol
)
1637 ? ~ (offsetT
) 0 : 0);
1638 if (symp
->sy_value
.X_op
== O_ne
)
1641 case O_lt
: left
= left
< right
? ~ (offsetT
) 0 : 0; break;
1642 case O_le
: left
= left
<= right
? ~ (offsetT
) 0 : 0; break;
1643 case O_ge
: left
= left
>= right
? ~ (offsetT
) 0 : 0; break;
1644 case O_gt
: left
= left
> right
? ~ (offsetT
) 0 : 0; break;
1645 case O_logical_and
: left
= left
&& right
; break;
1646 case O_logical_or
: left
= left
|| right
; break;
1651 /* See PR 20895 for a reproducer. */
1652 as_bad (_("Invalid operation on symbol"));
1653 goto exit_dont_set_value
;
1659 final_val
+= symp
->sy_frag
->fr_address
+ left
;
1660 if (final_seg
== expr_section
|| final_seg
== undefined_section
)
1662 if (seg_left
== undefined_section
1663 || seg_right
== undefined_section
)
1664 final_seg
= undefined_section
;
1665 else if (seg_left
== absolute_section
)
1666 final_seg
= seg_right
;
1668 final_seg
= seg_left
;
1670 resolved
= (symbol_resolved_p (add_symbol
)
1671 && symbol_resolved_p (op_symbol
));
1676 /* Give an error (below) if not in expr_section. We don't
1677 want to worry about expr_section symbols, because they
1678 are fictional (they are created as part of expression
1679 resolution), and any problems may not actually mean
1684 symp
->sy_flags
.sy_resolving
= 0;
1688 S_SET_VALUE (symp
, final_val
);
1690 exit_dont_set_value
:
1691 /* Always set the segment, even if not finalizing the value.
1692 The segment is used to determine whether a symbol is defined. */
1693 S_SET_SEGMENT (symp
, final_seg
);
1695 /* Don't worry if we can't resolve an expr_section symbol. */
1699 symp
->sy_flags
.sy_resolved
= 1;
1700 else if (S_GET_SEGMENT (symp
) != expr_section
)
1702 as_bad (_("can't resolve value for symbol `%s'"),
1704 symp
->sy_flags
.sy_resolved
= 1;
1711 /* A static function passed to hash_traverse. */
1714 resolve_local_symbol (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1716 symbol_entry_t
*entry
= *((symbol_entry_t
**) slot
);
1717 if (entry
->symbol
!= NULL
)
1718 resolve_symbol_value ((symbolS
*) entry
->symbol
);
1723 /* Resolve all local symbols. */
1726 resolve_local_symbol_values (void)
1728 htab_traverse (local_hash
, resolve_local_symbol
, NULL
);
1731 /* Obtain the current value of a symbol without changing any
1732 sub-expressions used. */
1735 snapshot_symbol (symbolS
**symbolPP
, valueT
*valueP
, segT
*segP
, fragS
**fragPP
)
1737 symbolS
*symbolP
= *symbolPP
;
1739 if (LOCAL_SYMBOL_CHECK (symbolP
))
1741 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1743 *valueP
= locsym
->lsy_value
;
1744 *segP
= locsym
->lsy_section
;
1745 *fragPP
= local_symbol_get_frag (locsym
);
1749 expressionS exp
= symbolP
->sy_value
;
1751 if (!symbolP
->sy_flags
.sy_resolved
&& exp
.X_op
!= O_illegal
)
1755 if (symbolP
->sy_flags
.sy_resolving
)
1757 symbolP
->sy_flags
.sy_resolving
= 1;
1758 resolved
= resolve_expression (&exp
);
1759 symbolP
->sy_flags
.sy_resolving
= 0;
1767 if (!symbol_equated_p (symbolP
))
1772 symbolP
= exp
.X_add_symbol
;
1779 *symbolPP
= symbolP
;
1781 /* A bogus input file can result in resolve_expression()
1782 generating a local symbol, so we have to check again. */
1783 if (LOCAL_SYMBOL_CHECK (symbolP
))
1785 struct local_symbol
*locsym
= (struct local_symbol
*) symbolP
;
1787 *valueP
= locsym
->lsy_value
;
1788 *segP
= locsym
->lsy_section
;
1789 *fragPP
= local_symbol_get_frag (locsym
);
1793 *valueP
= exp
.X_add_number
;
1794 *segP
= symbolP
->bsym
->section
;
1795 *fragPP
= symbolP
->sy_frag
;
1798 if (*segP
== expr_section
)
1801 case O_constant
: *segP
= absolute_section
; break;
1802 case O_register
: *segP
= reg_section
; break;
1810 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1811 They are *really* local. That is, they go out of scope whenever we see a
1812 label that isn't local. Also, like fb labels, there can be multiple
1813 instances of a dollar label. Therefor, we name encode each instance with
1814 the instance number, keep a list of defined symbols separate from the real
1815 symbol table, and we treat these buggers as a sparse array. */
1817 static long *dollar_labels
;
1818 static long *dollar_label_instances
;
1819 static char *dollar_label_defines
;
1820 static unsigned long dollar_label_count
;
1821 static unsigned long dollar_label_max
;
1824 dollar_label_defined (long label
)
1828 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1830 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1832 return dollar_label_defines
[i
- dollar_labels
];
1834 /* If we get here, label isn't defined. */
1839 dollar_label_instance (long label
)
1843 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
1845 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1847 return (dollar_label_instances
[i
- dollar_labels
]);
1849 /* If we get here, we haven't seen the label before.
1850 Therefore its instance count is zero. */
1855 dollar_label_clear (void)
1857 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
1860 #define DOLLAR_LABEL_BUMP_BY 10
1863 define_dollar_label (long label
)
1867 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
1870 ++dollar_label_instances
[i
- dollar_labels
];
1871 dollar_label_defines
[i
- dollar_labels
] = 1;
1875 /* If we get to here, we don't have label listed yet. */
1877 if (dollar_labels
== NULL
)
1879 dollar_labels
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1880 dollar_label_instances
= XNEWVEC (long, DOLLAR_LABEL_BUMP_BY
);
1881 dollar_label_defines
= XNEWVEC (char, DOLLAR_LABEL_BUMP_BY
);
1882 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
1883 dollar_label_count
= 0;
1885 else if (dollar_label_count
== dollar_label_max
)
1887 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
1888 dollar_labels
= XRESIZEVEC (long, dollar_labels
, dollar_label_max
);
1889 dollar_label_instances
= XRESIZEVEC (long, dollar_label_instances
,
1891 dollar_label_defines
= XRESIZEVEC (char, dollar_label_defines
,
1893 } /* if we needed to grow */
1895 dollar_labels
[dollar_label_count
] = label
;
1896 dollar_label_instances
[dollar_label_count
] = 1;
1897 dollar_label_defines
[dollar_label_count
] = 1;
1898 ++dollar_label_count
;
1901 /* Caller must copy returned name: we re-use the area for the next name.
1903 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1904 where n is the label number and m is the instance number. "L" makes
1905 it a label discarded unless debugging and "^A"('\1') ensures no
1906 ordinary symbol SHOULD get the same name as a local label
1907 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1909 fb labels get the same treatment, except that ^B is used in place
1912 char * /* Return local label name. */
1913 dollar_label_name (long n
, /* we just saw "n$:" : n a number. */
1914 int augend
/* 0 for current instance, 1 for new instance. */)
1917 /* Returned to caller, then copied. Used for created names ("4f"). */
1918 static char symbol_name_build
[24];
1921 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
1924 know (augend
== 0 || augend
== 1);
1925 p
= symbol_name_build
;
1926 #ifdef LOCAL_LABEL_PREFIX
1927 *p
++ = LOCAL_LABEL_PREFIX
;
1931 /* Next code just does sprintf( {}, "%d", n); */
1933 q
= symbol_name_temporary
;
1934 for (*q
++ = 0, i
= n
; i
; ++q
)
1939 while ((*p
= *--q
) != '\0')
1942 *p
++ = DOLLAR_LABEL_CHAR
; /* ^A */
1944 /* Instance number. */
1945 q
= symbol_name_temporary
;
1946 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1951 while ((*p
++ = *--q
) != '\0');
1953 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1954 return symbol_name_build
;
1957 /* Somebody else's idea of local labels. They are made by "n:" where n
1958 is any decimal digit. Refer to them with
1959 "nb" for previous (backward) n:
1960 or "nf" for next (forward) n:.
1962 We do a little better and let n be any number, not just a single digit, but
1963 since the other guy's assembler only does ten, we treat the first ten
1966 Like someone else's assembler, we have one set of local label counters for
1967 entire assembly, not one set per (sub)segment like in most assemblers. This
1968 implies that one can refer to a label in another segment, and indeed some
1969 crufty compilers have done just that.
1971 Since there could be a LOT of these things, treat them as a sparse
1974 #define FB_LABEL_SPECIAL (10)
1976 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1977 static long *fb_labels
;
1978 static long *fb_label_instances
;
1979 static long fb_label_count
;
1980 static long fb_label_max
;
1982 /* This must be more than FB_LABEL_SPECIAL. */
1983 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1986 fb_label_init (void)
1988 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1991 /* Add one to the instance number of this fb label. */
1994 fb_label_instance_inc (long label
)
1998 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
2000 ++fb_low_counter
[label
];
2004 if (fb_labels
!= NULL
)
2006 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
2007 i
< fb_labels
+ fb_label_count
; ++i
)
2011 ++fb_label_instances
[i
- fb_labels
];
2013 } /* if we find it */
2014 } /* for each existing label */
2017 /* If we get to here, we don't have label listed yet. */
2019 if (fb_labels
== NULL
)
2021 fb_labels
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
2022 fb_label_instances
= XNEWVEC (long, FB_LABEL_BUMP_BY
);
2023 fb_label_max
= FB_LABEL_BUMP_BY
;
2024 fb_label_count
= FB_LABEL_SPECIAL
;
2027 else if (fb_label_count
== fb_label_max
)
2029 fb_label_max
+= FB_LABEL_BUMP_BY
;
2030 fb_labels
= XRESIZEVEC (long, fb_labels
, fb_label_max
);
2031 fb_label_instances
= XRESIZEVEC (long, fb_label_instances
, fb_label_max
);
2032 } /* if we needed to grow */
2034 fb_labels
[fb_label_count
] = label
;
2035 fb_label_instances
[fb_label_count
] = 1;
2040 fb_label_instance (long label
)
2044 if ((unsigned long) label
< FB_LABEL_SPECIAL
)
2046 return (fb_low_counter
[label
]);
2049 if (fb_labels
!= NULL
)
2051 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
2052 i
< fb_labels
+ fb_label_count
; ++i
)
2056 return (fb_label_instances
[i
- fb_labels
]);
2057 } /* if we find it */
2058 } /* for each existing label */
2061 /* We didn't find the label, so this must be a reference to the
2066 /* Caller must copy returned name: we re-use the area for the next name.
2068 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2069 where n is the label number and m is the instance number. "L" makes
2070 it a label discarded unless debugging and "^B"('\2') ensures no
2071 ordinary symbol SHOULD get the same name as a local label
2072 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2074 dollar labels get the same treatment, except that ^A is used in
2077 char * /* Return local label name. */
2078 fb_label_name (long n
, /* We just saw "n:", "nf" or "nb" : n a number. */
2079 long augend
/* 0 for nb, 1 for n:, nf. */)
2082 /* Returned to caller, then copied. Used for created names ("4f"). */
2083 static char symbol_name_build
[24];
2086 char symbol_name_temporary
[20]; /* Build up a number, BACKWARDS. */
2090 know ((unsigned long) augend
<= 2 /* See mmix_fb_label. */);
2092 know ((unsigned long) augend
<= 1);
2094 p
= symbol_name_build
;
2095 #ifdef LOCAL_LABEL_PREFIX
2096 *p
++ = LOCAL_LABEL_PREFIX
;
2100 /* Next code just does sprintf( {}, "%d", n); */
2102 q
= symbol_name_temporary
;
2103 for (*q
++ = 0, i
= n
; i
; ++q
)
2108 while ((*p
= *--q
) != '\0')
2111 *p
++ = LOCAL_LABEL_CHAR
; /* ^B */
2113 /* Instance number. */
2114 q
= symbol_name_temporary
;
2115 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
2120 while ((*p
++ = *--q
) != '\0');
2122 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2123 return (symbol_name_build
);
2126 /* Decode name that may have been generated by foo_label_name() above.
2127 If the name wasn't generated by foo_label_name(), then return it
2128 unaltered. This is used for error messages. */
2131 decode_local_label_name (char *s
)
2134 char *symbol_decode
;
2136 int instance_number
;
2138 const char *message_format
;
2141 #ifdef LOCAL_LABEL_PREFIX
2142 if (s
[lindex
] == LOCAL_LABEL_PREFIX
)
2146 if (s
[lindex
] != 'L')
2149 for (label_number
= 0, p
= s
+ lindex
+ 1; ISDIGIT (*p
); ++p
)
2150 label_number
= (10 * label_number
) + *p
- '0';
2152 if (*p
== DOLLAR_LABEL_CHAR
)
2154 else if (*p
== LOCAL_LABEL_CHAR
)
2159 for (instance_number
= 0, p
++; ISDIGIT (*p
); ++p
)
2160 instance_number
= (10 * instance_number
) + *p
- '0';
2162 message_format
= _("\"%d\" (instance number %d of a %s label)");
2163 symbol_decode
= (char *) obstack_alloc (¬es
, strlen (message_format
) + 30);
2164 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
2166 return symbol_decode
;
2169 /* Get the value of a symbol. */
2172 S_GET_VALUE (symbolS
*s
)
2174 if (LOCAL_SYMBOL_CHECK (s
))
2175 return resolve_symbol_value (s
);
2177 if (!s
->sy_flags
.sy_resolved
)
2179 valueT val
= resolve_symbol_value (s
);
2183 if (S_IS_WEAKREFR (s
))
2184 return S_GET_VALUE (s
->sy_value
.X_add_symbol
);
2186 if (s
->sy_value
.X_op
!= O_constant
)
2188 if (! s
->sy_flags
.sy_resolved
2189 || s
->sy_value
.X_op
!= O_symbol
2190 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
2191 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2194 return (valueT
) s
->sy_value
.X_add_number
;
2197 /* Set the value of a symbol. */
2200 S_SET_VALUE (symbolS
*s
, valueT val
)
2202 if (LOCAL_SYMBOL_CHECK (s
))
2204 ((struct local_symbol
*) s
)->lsy_value
= val
;
2208 s
->sy_value
.X_op
= O_constant
;
2209 s
->sy_value
.X_add_number
= (offsetT
) val
;
2210 s
->sy_value
.X_unsigned
= 0;
2211 S_CLEAR_WEAKREFR (s
);
2215 copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2217 if (LOCAL_SYMBOL_CHECK (dest
))
2218 dest
= local_symbol_convert ((struct local_symbol
*) dest
);
2219 if (LOCAL_SYMBOL_CHECK (src
))
2220 src
= local_symbol_convert ((struct local_symbol
*) src
);
2222 /* In an expression, transfer the settings of these flags.
2223 The user can override later, of course. */
2224 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2225 | BSF_GNU_INDIRECT_FUNCTION)
2226 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
2228 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2229 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2232 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2233 TC_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
2238 S_IS_FUNCTION (symbolS
*s
)
2242 if (LOCAL_SYMBOL_CHECK (s
))
2245 flags
= s
->bsym
->flags
;
2247 return (flags
& BSF_FUNCTION
) != 0;
2251 S_IS_EXTERNAL (symbolS
*s
)
2255 if (LOCAL_SYMBOL_CHECK (s
))
2258 flags
= s
->bsym
->flags
;
2261 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2264 return (flags
& BSF_GLOBAL
) != 0;
2268 S_IS_WEAK (symbolS
*s
)
2270 if (LOCAL_SYMBOL_CHECK (s
))
2272 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2273 could probably handle a WEAKREFR as always weak though. E.g., if
2274 the referenced symbol has lost its weak status, there's no reason
2275 to keep handling the weakrefr as if it was weak. */
2276 if (S_IS_WEAKREFR (s
))
2277 return S_IS_WEAK (s
->sy_value
.X_add_symbol
);
2278 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
2282 S_IS_WEAKREFR (symbolS
*s
)
2284 if (LOCAL_SYMBOL_CHECK (s
))
2286 return s
->sy_flags
.sy_weakrefr
!= 0;
2290 S_IS_WEAKREFD (symbolS
*s
)
2292 if (LOCAL_SYMBOL_CHECK (s
))
2294 return s
->sy_flags
.sy_weakrefd
!= 0;
2298 S_IS_COMMON (symbolS
*s
)
2300 if (LOCAL_SYMBOL_CHECK (s
))
2302 return bfd_is_com_section (s
->bsym
->section
);
2306 S_IS_DEFINED (symbolS
*s
)
2308 if (LOCAL_SYMBOL_CHECK (s
))
2309 return ((struct local_symbol
*) s
)->lsy_section
!= undefined_section
;
2310 return s
->bsym
->section
!= undefined_section
;
2314 #ifndef EXTERN_FORCE_RELOC
2315 #define EXTERN_FORCE_RELOC IS_ELF
2318 /* Return true for symbols that should not be reduced to section
2319 symbols or eliminated from expressions, because they may be
2320 overridden by the linker. */
2322 S_FORCE_RELOC (symbolS
*s
, int strict
)
2325 if (LOCAL_SYMBOL_CHECK (s
))
2326 sec
= ((struct local_symbol
*) s
)->lsy_section
;
2330 && ((s
->bsym
->flags
& BSF_WEAK
) != 0
2331 || (EXTERN_FORCE_RELOC
2332 && (s
->bsym
->flags
& BSF_GLOBAL
) != 0)))
2333 || (s
->bsym
->flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
2335 sec
= s
->bsym
->section
;
2337 return bfd_is_und_section (sec
) || bfd_is_com_section (sec
);
2341 S_IS_DEBUG (symbolS
*s
)
2343 if (LOCAL_SYMBOL_CHECK (s
))
2345 if (s
->bsym
->flags
& BSF_DEBUGGING
)
2351 S_IS_LOCAL (symbolS
*s
)
2356 if (LOCAL_SYMBOL_CHECK (s
))
2359 flags
= s
->bsym
->flags
;
2362 if ((flags
& BSF_LOCAL
) && (flags
& BSF_GLOBAL
))
2365 if (bfd_asymbol_section (s
->bsym
) == reg_section
)
2368 if (flag_strip_local_absolute
2369 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2370 the source file even when the object file is stripped. */
2371 && (flags
& (BSF_GLOBAL
| BSF_FILE
)) == 0
2372 && bfd_asymbol_section (s
->bsym
) == absolute_section
)
2375 name
= S_GET_NAME (s
);
2376 return (name
!= NULL
2378 && (strchr (name
, DOLLAR_LABEL_CHAR
)
2379 || strchr (name
, LOCAL_LABEL_CHAR
)
2380 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2381 || strchr (name
, FAKE_LABEL_CHAR
)
2383 || TC_LABEL_IS_LOCAL (name
)
2384 || (! flag_keep_locals
2385 && (bfd_is_local_label (stdoutput
, s
->bsym
)
2388 && name
[1] == '?')))));
2392 S_IS_STABD (symbolS
*s
)
2394 return S_GET_NAME (s
) == 0;
2398 S_CAN_BE_REDEFINED (const symbolS
*s
)
2400 if (LOCAL_SYMBOL_CHECK (s
))
2401 return (local_symbol_get_frag ((struct local_symbol
*) s
)
2402 == &predefined_address_frag
);
2403 /* Permit register names to be redefined. */
2404 return s
->bsym
->section
== reg_section
;
2408 S_IS_VOLATILE (const symbolS
*s
)
2410 if (LOCAL_SYMBOL_CHECK (s
))
2412 return s
->sy_flags
.sy_volatile
;
2416 S_IS_FORWARD_REF (const symbolS
*s
)
2418 if (LOCAL_SYMBOL_CHECK (s
))
2420 return s
->sy_flags
.sy_forward_ref
;
2424 S_GET_NAME (symbolS
*s
)
2426 if (LOCAL_SYMBOL_CHECK (s
))
2427 return ((struct local_symbol
*) s
)->lsy_name
;
2428 return s
->bsym
->name
;
2432 S_GET_SEGMENT (symbolS
*s
)
2434 if (LOCAL_SYMBOL_CHECK (s
))
2435 return ((struct local_symbol
*) s
)->lsy_section
;
2436 return s
->bsym
->section
;
2440 S_SET_SEGMENT (symbolS
*s
, segT seg
)
2442 /* Don't reassign section symbols. The direct reason is to prevent seg
2443 faults assigning back to const global symbols such as *ABS*, but it
2444 shouldn't happen anyway. */
2446 if (LOCAL_SYMBOL_CHECK (s
))
2448 if (seg
== reg_section
)
2449 s
= local_symbol_convert ((struct local_symbol
*) s
);
2452 ((struct local_symbol
*) s
)->lsy_section
= seg
;
2457 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2459 if (s
->bsym
->section
!= seg
)
2463 s
->bsym
->section
= seg
;
2467 S_SET_EXTERNAL (symbolS
*s
)
2469 if (LOCAL_SYMBOL_CHECK (s
))
2470 s
= local_symbol_convert ((struct local_symbol
*) s
);
2471 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2473 /* Let .weak override .global. */
2476 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
2478 /* Do not reassign section symbols. */
2479 as_warn (_("section symbols are already global"));
2482 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2483 if (S_GET_SEGMENT (s
) == reg_section
)
2485 as_bad ("can't make register symbol `%s' global",
2490 s
->bsym
->flags
|= BSF_GLOBAL
;
2491 s
->bsym
->flags
&= ~(BSF_LOCAL
| BSF_WEAK
);
2494 if (! an_external_name
&& S_GET_NAME(s
)[0] != '.')
2495 an_external_name
= S_GET_NAME (s
);
2500 S_CLEAR_EXTERNAL (symbolS
*s
)
2502 if (LOCAL_SYMBOL_CHECK (s
))
2504 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
2506 /* Let .weak override. */
2509 s
->bsym
->flags
|= BSF_LOCAL
;
2510 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
2514 S_SET_WEAK (symbolS
*s
)
2516 if (LOCAL_SYMBOL_CHECK (s
))
2517 s
= local_symbol_convert ((struct local_symbol
*) s
);
2518 #ifdef obj_set_weak_hook
2519 obj_set_weak_hook (s
);
2521 s
->bsym
->flags
|= BSF_WEAK
;
2522 s
->bsym
->flags
&= ~(BSF_GLOBAL
| BSF_LOCAL
);
2526 S_SET_WEAKREFR (symbolS
*s
)
2528 if (LOCAL_SYMBOL_CHECK (s
))
2529 s
= local_symbol_convert ((struct local_symbol
*) s
);
2530 s
->sy_flags
.sy_weakrefr
= 1;
2531 /* If the alias was already used, make sure we mark the target as
2532 used as well, otherwise it might be dropped from the symbol
2533 table. This may have unintended side effects if the alias is
2534 later redirected to another symbol, such as keeping the unused
2535 previous target in the symbol table. Since it will be weak, it's
2537 if (s
->sy_flags
.sy_used
)
2538 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2542 S_CLEAR_WEAKREFR (symbolS
*s
)
2544 if (LOCAL_SYMBOL_CHECK (s
))
2546 s
->sy_flags
.sy_weakrefr
= 0;
2550 S_SET_WEAKREFD (symbolS
*s
)
2552 if (LOCAL_SYMBOL_CHECK (s
))
2553 s
= local_symbol_convert ((struct local_symbol
*) s
);
2554 s
->sy_flags
.sy_weakrefd
= 1;
2559 S_CLEAR_WEAKREFD (symbolS
*s
)
2561 if (LOCAL_SYMBOL_CHECK (s
))
2563 if (s
->sy_flags
.sy_weakrefd
)
2565 s
->sy_flags
.sy_weakrefd
= 0;
2566 /* If a weakref target symbol is weak, then it was never
2567 referenced directly before, not even in a .global directive,
2568 so decay it to local. If it remains undefined, it will be
2569 later turned into a global, like any other undefined
2571 if (s
->bsym
->flags
& BSF_WEAK
)
2573 #ifdef obj_clear_weak_hook
2574 obj_clear_weak_hook (s
);
2576 s
->bsym
->flags
&= ~BSF_WEAK
;
2577 s
->bsym
->flags
|= BSF_LOCAL
;
2583 S_SET_THREAD_LOCAL (symbolS
*s
)
2585 if (LOCAL_SYMBOL_CHECK (s
))
2586 s
= local_symbol_convert ((struct local_symbol
*) s
);
2587 if (bfd_is_com_section (s
->bsym
->section
)
2588 && (s
->bsym
->flags
& BSF_THREAD_LOCAL
) != 0)
2590 s
->bsym
->flags
|= BSF_THREAD_LOCAL
;
2591 if ((s
->bsym
->flags
& BSF_FUNCTION
) != 0)
2592 as_bad (_("Accessing function `%s' as thread-local object"),
2594 else if (! bfd_is_und_section (s
->bsym
->section
)
2595 && (s
->bsym
->section
->flags
& SEC_THREAD_LOCAL
) == 0)
2596 as_bad (_("Accessing `%s' as thread-local object"),
2601 S_SET_NAME (symbolS
*s
, const char *name
)
2603 if (LOCAL_SYMBOL_CHECK (s
))
2605 ((struct local_symbol
*) s
)->lsy_name
= name
;
2608 s
->bsym
->name
= name
;
2612 S_SET_VOLATILE (symbolS
*s
)
2614 if (LOCAL_SYMBOL_CHECK (s
))
2615 s
= local_symbol_convert ((struct local_symbol
*) s
);
2616 s
->sy_flags
.sy_volatile
= 1;
2620 S_CLEAR_VOLATILE (symbolS
*s
)
2622 if (!LOCAL_SYMBOL_CHECK (s
))
2623 s
->sy_flags
.sy_volatile
= 0;
2627 S_SET_FORWARD_REF (symbolS
*s
)
2629 if (LOCAL_SYMBOL_CHECK (s
))
2630 s
= local_symbol_convert ((struct local_symbol
*) s
);
2631 s
->sy_flags
.sy_forward_ref
= 1;
2634 /* Return the previous symbol in a chain. */
2637 symbol_previous (symbolS
*s
)
2639 if (LOCAL_SYMBOL_CHECK (s
))
2641 return s
->sy_previous
;
2644 /* Return the next symbol in a chain. */
2647 symbol_next (symbolS
*s
)
2649 if (LOCAL_SYMBOL_CHECK (s
))
2654 /* Return a pointer to the value of a symbol as an expression. */
2657 symbol_get_value_expression (symbolS
*s
)
2659 if (LOCAL_SYMBOL_CHECK (s
))
2660 s
= local_symbol_convert ((struct local_symbol
*) s
);
2661 return &s
->sy_value
;
2664 /* Set the value of a symbol to an expression. */
2667 symbol_set_value_expression (symbolS
*s
, const expressionS
*exp
)
2669 if (LOCAL_SYMBOL_CHECK (s
))
2670 s
= local_symbol_convert ((struct local_symbol
*) s
);
2672 S_CLEAR_WEAKREFR (s
);
2675 /* Return whether 2 symbols are the same. */
2678 symbol_same_p (symbolS
*s1
, symbolS
*s2
)
2680 s1
= get_real_sym (s1
);
2681 s2
= get_real_sym (s2
);
2685 /* Return a pointer to the X_add_number component of a symbol. */
2688 symbol_X_add_number (symbolS
*s
)
2690 if (LOCAL_SYMBOL_CHECK (s
))
2691 return (offsetT
*) &((struct local_symbol
*) s
)->lsy_value
;
2693 return &s
->sy_value
.X_add_number
;
2696 /* Set the value of SYM to the current position in the current segment. */
2699 symbol_set_value_now (symbolS
*sym
)
2701 S_SET_SEGMENT (sym
, now_seg
);
2702 S_SET_VALUE (sym
, frag_now_fix ());
2703 symbol_set_frag (sym
, frag_now
);
2706 /* Set the frag of a symbol. */
2709 symbol_set_frag (symbolS
*s
, fragS
*f
)
2711 if (LOCAL_SYMBOL_CHECK (s
))
2713 local_symbol_set_frag ((struct local_symbol
*) s
, f
);
2717 S_CLEAR_WEAKREFR (s
);
2720 /* Return the frag of a symbol. */
2723 symbol_get_frag (symbolS
*s
)
2725 if (LOCAL_SYMBOL_CHECK (s
))
2726 return local_symbol_get_frag ((struct local_symbol
*) s
);
2730 /* Mark a symbol as having been used. */
2733 symbol_mark_used (symbolS
*s
)
2735 if (LOCAL_SYMBOL_CHECK (s
))
2737 s
->sy_flags
.sy_used
= 1;
2738 if (S_IS_WEAKREFR (s
))
2739 symbol_mark_used (s
->sy_value
.X_add_symbol
);
2742 /* Clear the mark of whether a symbol has been used. */
2745 symbol_clear_used (symbolS
*s
)
2747 if (LOCAL_SYMBOL_CHECK (s
))
2748 s
= local_symbol_convert ((struct local_symbol
*) s
);
2749 s
->sy_flags
.sy_used
= 0;
2752 /* Return whether a symbol has been used. */
2755 symbol_used_p (symbolS
*s
)
2757 if (LOCAL_SYMBOL_CHECK (s
))
2759 return s
->sy_flags
.sy_used
;
2762 /* Mark a symbol as having been used in a reloc. */
2765 symbol_mark_used_in_reloc (symbolS
*s
)
2767 if (LOCAL_SYMBOL_CHECK (s
))
2768 s
= local_symbol_convert ((struct local_symbol
*) s
);
2769 s
->sy_flags
.sy_used_in_reloc
= 1;
2772 /* Clear the mark of whether a symbol has been used in a reloc. */
2775 symbol_clear_used_in_reloc (symbolS
*s
)
2777 if (LOCAL_SYMBOL_CHECK (s
))
2779 s
->sy_flags
.sy_used_in_reloc
= 0;
2782 /* Return whether a symbol has been used in a reloc. */
2785 symbol_used_in_reloc_p (symbolS
*s
)
2787 if (LOCAL_SYMBOL_CHECK (s
))
2789 return s
->sy_flags
.sy_used_in_reloc
;
2792 /* Mark a symbol as an MRI common symbol. */
2795 symbol_mark_mri_common (symbolS
*s
)
2797 if (LOCAL_SYMBOL_CHECK (s
))
2798 s
= local_symbol_convert ((struct local_symbol
*) s
);
2799 s
->sy_flags
.sy_mri_common
= 1;
2802 /* Clear the mark of whether a symbol is an MRI common symbol. */
2805 symbol_clear_mri_common (symbolS
*s
)
2807 if (LOCAL_SYMBOL_CHECK (s
))
2809 s
->sy_flags
.sy_mri_common
= 0;
2812 /* Return whether a symbol is an MRI common symbol. */
2815 symbol_mri_common_p (symbolS
*s
)
2817 if (LOCAL_SYMBOL_CHECK (s
))
2819 return s
->sy_flags
.sy_mri_common
;
2822 /* Mark a symbol as having been written. */
2825 symbol_mark_written (symbolS
*s
)
2827 if (LOCAL_SYMBOL_CHECK (s
))
2829 s
->sy_flags
.sy_written
= 1;
2832 /* Clear the mark of whether a symbol has been written. */
2835 symbol_clear_written (symbolS
*s
)
2837 if (LOCAL_SYMBOL_CHECK (s
))
2839 s
->sy_flags
.sy_written
= 0;
2842 /* Return whether a symbol has been written. */
2845 symbol_written_p (symbolS
*s
)
2847 if (LOCAL_SYMBOL_CHECK (s
))
2849 return s
->sy_flags
.sy_written
;
2852 /* Mark a symbol has having been resolved. */
2855 symbol_mark_resolved (symbolS
*s
)
2857 if (LOCAL_SYMBOL_CHECK (s
))
2859 local_symbol_mark_resolved ((struct local_symbol
*) s
);
2862 s
->sy_flags
.sy_resolved
= 1;
2865 /* Return whether a symbol has been resolved. */
2868 symbol_resolved_p (symbolS
*s
)
2870 if (LOCAL_SYMBOL_CHECK (s
))
2871 return local_symbol_resolved_p ((struct local_symbol
*) s
);
2872 return s
->sy_flags
.sy_resolved
;
2875 /* Return whether a symbol is a section symbol. */
2878 symbol_section_p (symbolS
*s ATTRIBUTE_UNUSED
)
2880 if (LOCAL_SYMBOL_CHECK (s
))
2882 return (s
->bsym
->flags
& BSF_SECTION_SYM
) != 0;
2885 /* Return whether a symbol is equated to another symbol. */
2888 symbol_equated_p (symbolS
*s
)
2890 if (LOCAL_SYMBOL_CHECK (s
))
2892 return s
->sy_value
.X_op
== O_symbol
;
2895 /* Return whether a symbol is equated to another symbol, and should be
2896 treated specially when writing out relocs. */
2899 symbol_equated_reloc_p (symbolS
*s
)
2901 if (LOCAL_SYMBOL_CHECK (s
))
2903 /* X_op_symbol, normally not used for O_symbol, is set by
2904 resolve_symbol_value to flag expression syms that have been
2906 return (s
->sy_value
.X_op
== O_symbol
2907 #if defined (OBJ_COFF) && defined (TE_PE)
2910 && ((s
->sy_flags
.sy_resolved
&& s
->sy_value
.X_op_symbol
!= NULL
)
2911 || ! S_IS_DEFINED (s
)
2912 || S_IS_COMMON (s
)));
2915 /* Return whether a symbol has a constant value. */
2918 symbol_constant_p (symbolS
*s
)
2920 if (LOCAL_SYMBOL_CHECK (s
))
2922 return s
->sy_value
.X_op
== O_constant
;
2925 /* Return whether a symbol was cloned and thus removed from the global
2929 symbol_shadow_p (symbolS
*s
)
2931 if (LOCAL_SYMBOL_CHECK (s
))
2933 return s
->sy_next
== s
;
2936 /* If S was created as a struct symbol, return S, otherwise if S is a
2937 converted local_symbol return the converted symbol, otherwise
2941 symbol_symbolS (symbolS
*s
)
2943 if (LOCAL_SYMBOL_CHECK (s
))
2948 /* Return the BFD symbol for a symbol. */
2951 symbol_get_bfdsym (symbolS
*s
)
2953 if (LOCAL_SYMBOL_CHECK (s
))
2954 s
= local_symbol_convert ((struct local_symbol
*) s
);
2958 /* Set the BFD symbol for a symbol. */
2961 symbol_set_bfdsym (symbolS
*s
, asymbol
*bsym
)
2963 if (LOCAL_SYMBOL_CHECK (s
))
2964 s
= local_symbol_convert ((struct local_symbol
*) s
);
2965 /* Usually, it is harmless to reset a symbol to a BFD section
2966 symbol. For example, obj_elf_change_section sets the BFD symbol
2967 of an old symbol with the newly created section symbol. But when
2968 we have multiple sections with the same name, the newly created
2969 section may have the same name as an old section. We check if the
2970 old symbol has been already marked as a section symbol before
2972 if ((s
->bsym
->flags
& BSF_SECTION_SYM
) == 0)
2974 /* else XXX - What do we do now ? */
2977 #ifdef OBJ_SYMFIELD_TYPE
2979 /* Get a pointer to the object format information for a symbol. */
2982 symbol_get_obj (symbolS
*s
)
2984 if (LOCAL_SYMBOL_CHECK (s
))
2985 s
= local_symbol_convert ((struct local_symbol
*) s
);
2989 /* Set the object format information for a symbol. */
2992 symbol_set_obj (symbolS
*s
, OBJ_SYMFIELD_TYPE
*o
)
2994 if (LOCAL_SYMBOL_CHECK (s
))
2995 s
= local_symbol_convert ((struct local_symbol
*) s
);
2999 #endif /* OBJ_SYMFIELD_TYPE */
3001 #ifdef TC_SYMFIELD_TYPE
3003 /* Get a pointer to the processor information for a symbol. */
3006 symbol_get_tc (symbolS
*s
)
3008 if (LOCAL_SYMBOL_CHECK (s
))
3009 s
= local_symbol_convert ((struct local_symbol
*) s
);
3013 /* Set the processor information for a symbol. */
3016 symbol_set_tc (symbolS
*s
, TC_SYMFIELD_TYPE
*o
)
3018 if (LOCAL_SYMBOL_CHECK (s
))
3019 s
= local_symbol_convert ((struct local_symbol
*) s
);
3023 #endif /* TC_SYMFIELD_TYPE */
3028 symbol_lastP
= NULL
;
3029 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
3030 sy_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
3031 NULL
, xcalloc
, free
);
3032 local_hash
= htab_create_alloc (16, hash_symbol_entry
, eq_symbol_entry
,
3033 NULL
, xcalloc
, free
);
3035 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
3036 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3037 abs_symbol
.bsym
= bfd_abs_section_ptr
->symbol
;
3039 abs_symbol
.sy_value
.X_op
= O_constant
;
3040 abs_symbol
.sy_frag
= &zero_address_frag
;
3042 if (LOCAL_LABELS_FB
)
3047 dot_symbol_init (void)
3049 dot_symbol
.bsym
= bfd_make_empty_symbol (stdoutput
);
3050 if (dot_symbol
.bsym
== NULL
)
3051 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3052 dot_symbol
.bsym
->name
= ".";
3053 dot_symbol
.sy_flags
.sy_forward_ref
= 1;
3054 dot_symbol
.sy_value
.X_op
= O_constant
;
3059 /* Maximum indent level.
3060 Available for modification inside a gdb session. */
3061 static int max_indent_level
= 8;
3064 print_symbol_value_1 (FILE *file
, symbolS
*sym
)
3066 const char *name
= S_GET_NAME (sym
);
3067 if (!name
|| !name
[0])
3069 fprintf (file
, "sym ");
3070 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
));
3071 fprintf (file
, " %s", name
);
3073 if (LOCAL_SYMBOL_CHECK (sym
))
3075 struct local_symbol
*locsym
= (struct local_symbol
*) sym
;
3077 if (local_symbol_get_frag (locsym
) != & zero_address_frag
3078 && local_symbol_get_frag (locsym
) != NULL
)
3080 fprintf (file
, " frag ");
3081 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) local_symbol_get_frag (locsym
)));
3083 if (local_symbol_resolved_p (locsym
))
3084 fprintf (file
, " resolved");
3085 fprintf (file
, " local");
3089 if (sym
->sy_frag
!= &zero_address_frag
)
3091 fprintf (file
, " frag ");
3092 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) sym
->sy_frag
));
3094 if (sym
->sy_flags
.sy_written
)
3095 fprintf (file
, " written");
3096 if (sym
->sy_flags
.sy_resolved
)
3097 fprintf (file
, " resolved");
3098 else if (sym
->sy_flags
.sy_resolving
)
3099 fprintf (file
, " resolving");
3100 if (sym
->sy_flags
.sy_used_in_reloc
)
3101 fprintf (file
, " used-in-reloc");
3102 if (sym
->sy_flags
.sy_used
)
3103 fprintf (file
, " used");
3104 if (S_IS_LOCAL (sym
))
3105 fprintf (file
, " local");
3106 if (S_IS_EXTERNAL (sym
))
3107 fprintf (file
, " extern");
3108 if (S_IS_WEAK (sym
))
3109 fprintf (file
, " weak");
3110 if (S_IS_DEBUG (sym
))
3111 fprintf (file
, " debug");
3112 if (S_IS_DEFINED (sym
))
3113 fprintf (file
, " defined");
3115 if (S_IS_WEAKREFR (sym
))
3116 fprintf (file
, " weakrefr");
3117 if (S_IS_WEAKREFD (sym
))
3118 fprintf (file
, " weakrefd");
3119 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
3120 if (symbol_resolved_p (sym
))
3122 segT s
= S_GET_SEGMENT (sym
);
3124 if (s
!= undefined_section
3125 && s
!= expr_section
)
3126 fprintf (file
, " %lx", (unsigned long) S_GET_VALUE (sym
));
3128 else if (indent_level
< max_indent_level
3129 && S_GET_SEGMENT (sym
) != undefined_section
)
3132 fprintf (file
, "\n%*s<", indent_level
* 4, "");
3133 if (LOCAL_SYMBOL_CHECK (sym
))
3134 fprintf (file
, "constant %lx",
3135 (unsigned long) ((struct local_symbol
*) sym
)->lsy_value
);
3137 print_expr_1 (file
, &sym
->sy_value
);
3138 fprintf (file
, ">");
3145 print_symbol_value (symbolS
*sym
)
3148 print_symbol_value_1 (stderr
, sym
);
3149 fprintf (stderr
, "\n");
3153 print_binary (FILE *file
, const char *name
, expressionS
*exp
)
3156 fprintf (file
, "%s\n%*s<", name
, indent_level
* 4, "");
3157 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3158 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3159 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3160 fprintf (file
, ">");
3165 print_expr_1 (FILE *file
, expressionS
*exp
)
3167 fprintf (file
, "expr ");
3168 fprintf_vma (file
, (bfd_vma
) ((bfd_hostptr_t
) exp
));
3169 fprintf (file
, " ");
3173 fprintf (file
, "illegal");
3176 fprintf (file
, "absent");
3179 fprintf (file
, "constant %lx", (unsigned long) exp
->X_add_number
);
3183 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
3184 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3185 fprintf (file
, ">");
3187 if (exp
->X_add_number
)
3188 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
3189 (unsigned long) exp
->X_add_number
);
3193 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
3196 fprintf (file
, "big");
3199 fprintf (file
, "uminus -<");
3201 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3202 fprintf (file
, ">");
3203 goto maybe_print_addnum
;
3205 fprintf (file
, "bit_not");
3208 print_binary (file
, "multiply", exp
);
3211 print_binary (file
, "divide", exp
);
3214 print_binary (file
, "modulus", exp
);
3217 print_binary (file
, "lshift", exp
);
3220 print_binary (file
, "rshift", exp
);
3222 case O_bit_inclusive_or
:
3223 print_binary (file
, "bit_ior", exp
);
3225 case O_bit_exclusive_or
:
3226 print_binary (file
, "bit_xor", exp
);
3229 print_binary (file
, "bit_and", exp
);
3232 print_binary (file
, "eq", exp
);
3235 print_binary (file
, "ne", exp
);
3238 print_binary (file
, "lt", exp
);
3241 print_binary (file
, "le", exp
);
3244 print_binary (file
, "ge", exp
);
3247 print_binary (file
, "gt", exp
);
3250 print_binary (file
, "logical_and", exp
);
3253 print_binary (file
, "logical_or", exp
);
3257 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
3258 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3259 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3260 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3261 fprintf (file
, ">");
3262 goto maybe_print_addnum
;
3265 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
3266 print_symbol_value_1 (file
, exp
->X_add_symbol
);
3267 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
3268 print_symbol_value_1 (file
, exp
->X_op_symbol
);
3269 fprintf (file
, ">");
3270 goto maybe_print_addnum
;
3272 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
3279 print_expr (expressionS
*exp
)
3281 print_expr_1 (stderr
, exp
);
3282 fprintf (stderr
, "\n");
3286 symbol_print_statistics (FILE *file
)
3288 htab_print_statistics (file
, "symbol table", sy_hash
);
3289 htab_print_statistics (file
, "mini local symbol table", local_hash
);
3290 fprintf (file
, "%lu mini local symbols created, %lu converted\n",
3291 local_symbol_count
, local_symbol_conversion_count
);
3294 #ifdef OBJ_COMPLEX_RELC
3296 /* Convert given symbol to a new complex-relocation symbol name. This
3297 may be a recursive function, since it might be called for non-leaf
3298 nodes (plain symbols) in the expression tree. The caller owns the
3299 returning string, so should free it eventually. Errors are
3300 indicated via as_bad and a NULL return value. The given symbol
3301 is marked with sy_used_in_reloc. */
3304 symbol_relc_make_sym (symbolS
* sym
)
3306 char * terminal
= NULL
;
3311 gas_assert (sym
!= NULL
);
3313 /* Recurse to symbol_relc_make_expr if this symbol
3314 is defined as an expression or a plain value. */
3315 if ( S_GET_SEGMENT (sym
) == expr_section
3316 || S_GET_SEGMENT (sym
) == absolute_section
)
3317 return symbol_relc_make_expr (symbol_get_value_expression (sym
));
3319 /* This may be a "fake symbol", referring to ".".
3320 Write out a special null symbol to refer to this position. */
3321 if (! strcmp (S_GET_NAME (sym
), FAKE_LABEL_NAME
))
3322 return xstrdup (".");
3324 /* We hope this is a plain leaf symbol. Construct the encoding
3325 as {S,s}II...:CCCCCCC....
3326 where 'S'/'s' means section symbol / plain symbol
3327 III is decimal for the symbol name length
3328 CCC is the symbol name itself. */
3329 symbol_mark_used_in_reloc (sym
);
3331 sname
= S_GET_NAME (sym
);
3332 sname_len
= strlen (sname
);
3333 typetag
= symbol_section_p (sym
) ? 'S' : 's';
3335 terminal
= XNEWVEC (char, (1 /* S or s */
3336 + 8 /* sname_len in decimal */
3338 + sname_len
/* name itself */
3341 sprintf (terminal
, "%c%d:%s", typetag
, sname_len
, sname
);
3345 /* Convert given value to a new complex-relocation symbol name. This
3346 is a non-recursive function, since it is be called for leaf nodes
3347 (plain values) in the expression tree. The caller owns the
3348 returning string, so should free() it eventually. No errors. */
3351 symbol_relc_make_value (offsetT val
)
3353 char * terminal
= XNEWVEC (char, 28); /* Enough for long long. */
3356 bfd_sprintf_vma (stdoutput
, terminal
+ 1, val
);
3360 /* Convert given expression to a new complex-relocation symbol name.
3361 This is a recursive function, since it traverses the entire given
3362 expression tree. The caller owns the returning string, so should
3363 free() it eventually. Errors are indicated via as_bad() and a NULL
3367 symbol_relc_make_expr (expressionS
* exp
)
3369 const char * opstr
= NULL
; /* Operator prefix string. */
3370 int arity
= 0; /* Arity of this operator. */
3371 char * operands
[3]; /* Up to three operands. */
3372 char * concat_string
= NULL
;
3374 operands
[0] = operands
[1] = operands
[2] = NULL
;
3376 gas_assert (exp
!= NULL
);
3378 /* Match known operators -> fill in opstr, arity, operands[] and fall
3379 through to construct subexpression fragments; may instead return
3380 string directly for leaf nodes. */
3382 /* See expr.h for the meaning of all these enums. Many operators
3383 have an unnatural arity (X_add_number implicitly added). The
3384 conversion logic expands them to explicit "+" subexpressions. */
3389 as_bad ("Unknown expression operator (enum %d)", exp
->X_op
);
3394 return symbol_relc_make_value (exp
->X_add_number
);
3397 if (exp
->X_add_number
)
3401 operands
[0] = symbol_relc_make_sym (exp
->X_add_symbol
);
3402 operands
[1] = symbol_relc_make_value (exp
->X_add_number
);
3406 return symbol_relc_make_sym (exp
->X_add_symbol
);
3408 /* Helper macros for nesting nodes. */
3410 #define HANDLE_XADD_OPT1(str_) \
3411 if (exp->X_add_number) \
3414 opstr = "+:" str_; \
3415 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3416 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3423 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3427 #define HANDLE_XADD_OPT2(str_) \
3428 if (exp->X_add_number) \
3431 opstr = "+:" str_; \
3432 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3433 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3434 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3440 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3441 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3445 /* Nesting nodes. */
3447 case O_uminus
: HANDLE_XADD_OPT1 ("0-");
3448 case O_bit_not
: HANDLE_XADD_OPT1 ("~");
3449 case O_logical_not
: HANDLE_XADD_OPT1 ("!");
3450 case O_multiply
: HANDLE_XADD_OPT2 ("*");
3451 case O_divide
: HANDLE_XADD_OPT2 ("/");
3452 case O_modulus
: HANDLE_XADD_OPT2 ("%");
3453 case O_left_shift
: HANDLE_XADD_OPT2 ("<<");
3454 case O_right_shift
: HANDLE_XADD_OPT2 (">>");
3455 case O_bit_inclusive_or
: HANDLE_XADD_OPT2 ("|");
3456 case O_bit_exclusive_or
: HANDLE_XADD_OPT2 ("^");
3457 case O_bit_and
: HANDLE_XADD_OPT2 ("&");
3458 case O_add
: HANDLE_XADD_OPT2 ("+");
3459 case O_subtract
: HANDLE_XADD_OPT2 ("-");
3460 case O_eq
: HANDLE_XADD_OPT2 ("==");
3461 case O_ne
: HANDLE_XADD_OPT2 ("!=");
3462 case O_lt
: HANDLE_XADD_OPT2 ("<");
3463 case O_le
: HANDLE_XADD_OPT2 ("<=");
3464 case O_ge
: HANDLE_XADD_OPT2 (">=");
3465 case O_gt
: HANDLE_XADD_OPT2 (">");
3466 case O_logical_and
: HANDLE_XADD_OPT2 ("&&");
3467 case O_logical_or
: HANDLE_XADD_OPT2 ("||");
3470 /* Validate & reject early. */
3471 if (arity
>= 1 && ((operands
[0] == NULL
) || (strlen (operands
[0]) == 0)))
3473 if (arity
>= 2 && ((operands
[1] == NULL
) || (strlen (operands
[1]) == 0)))
3475 if (arity
>= 3 && ((operands
[2] == NULL
) || (strlen (operands
[2]) == 0)))
3479 concat_string
= NULL
;
3480 else if (arity
== 0)
3481 concat_string
= xstrdup (opstr
);
3482 else if (arity
== 1)
3483 concat_string
= concat (opstr
, ":", operands
[0], (char *) NULL
);
3484 else if (arity
== 2)
3485 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1],
3488 concat_string
= concat (opstr
, ":", operands
[0], ":", operands
[1], ":",
3489 operands
[2], (char *) NULL
);
3491 /* Free operand strings (not opstr). */
3492 if (arity
>= 1) xfree (operands
[0]);
3493 if (arity
>= 2) xfree (operands
[1]);
3494 if (arity
>= 3) xfree (operands
[2]);
3496 return concat_string
;