Automatic date update in version.in
[binutils-gdb.git] / gas / symbols.c
blobb57dbfc64f42e131ebb501bccfcea3676bf80cfc
1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2024 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)
9 any later version.
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
19 02110-1301, USA. */
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
26 #include "subsegs.h"
27 #include "write.h"
28 #include "scfi.h"
30 #include <limits.h>
31 #ifndef CHAR_BIT
32 #define CHAR_BIT 8
33 #endif
35 struct symbol_flags
37 /* Whether the symbol is a local_symbol. */
38 unsigned int local_symbol : 1;
40 /* Weather symbol has been written. */
41 unsigned int written : 1;
43 /* Whether symbol value has been completely resolved (used during
44 final pass over symbol table). */
45 unsigned int resolved : 1;
47 /* Whether the symbol value is currently being resolved (used to
48 detect loops in symbol dependencies). */
49 unsigned int resolving : 1;
51 /* Whether the symbol value is used in a reloc. This is used to
52 ensure that symbols used in relocs are written out, even if they
53 are local and would otherwise not be. */
54 unsigned int used_in_reloc : 1;
56 /* Whether the symbol is used as an operand or in an expression.
57 NOTE: Not all the backends keep this information accurate;
58 backends which use this bit are responsible for setting it when
59 a symbol is used in backend routines. */
60 unsigned int used : 1;
62 /* Whether the symbol can be re-defined. */
63 unsigned int volatil : 1;
65 /* Whether the symbol is a forward reference, and whether such has
66 been determined. */
67 unsigned int forward_ref : 1;
68 unsigned int forward_resolved : 1;
70 /* This is set if the symbol is defined in an MRI common section.
71 We handle such sections as single common symbols, so symbols
72 defined within them must be treated specially by the relocation
73 routines. */
74 unsigned int mri_common : 1;
76 /* This is set if the symbol is set with a .weakref directive. */
77 unsigned int weakrefr : 1;
79 /* This is set when the symbol is referenced as part of a .weakref
80 directive, but only if the symbol was not in the symbol table
81 before. It is cleared as soon as any direct reference to the
82 symbol is present. */
83 unsigned int weakrefd : 1;
85 /* Whether the symbol has been marked to be removed by a .symver
86 directive. */
87 unsigned int removed : 1;
89 /* Set when a warning about the symbol containing multibyte characters
90 is generated. */
91 unsigned int multibyte_warned : 1;
94 /* A pointer in the symbol may point to either a complete symbol
95 (struct symbol below) or to a local symbol (struct local_symbol
96 defined here). The symbol code can detect the case by examining
97 the first field which is present in both structs.
99 We do this because we ordinarily only need a small amount of
100 information for a local symbol. The symbol table takes up a lot of
101 space, and storing less information for a local symbol can make a
102 big difference in assembler memory usage when assembling a large
103 file. */
105 struct local_symbol
107 /* Symbol flags. Only local_symbol and resolved are relevant. */
108 struct symbol_flags flags;
110 /* Hash value calculated from name. */
111 hashval_t hash;
113 /* The symbol name. */
114 const char *name;
116 /* The symbol frag. */
117 fragS *frag;
119 /* The symbol section. */
120 asection *section;
122 /* The value of the symbol. */
123 valueT value;
126 /* The information we keep for a symbol. The symbol table holds
127 pointers both to this and to local_symbol structures. The first
128 three fields must be identical to struct local_symbol, and the size
129 should be the same as or smaller than struct local_symbol.
130 Fields that don't fit go to an extension structure. */
132 struct symbol
134 /* Symbol flags. */
135 struct symbol_flags flags;
137 /* Hash value calculated from name. */
138 hashval_t hash;
140 /* The symbol name. */
141 const char *name;
143 /* Pointer to the frag this symbol is attached to, if any.
144 Otherwise, NULL. */
145 fragS *frag;
147 /* BFD symbol */
148 asymbol *bsym;
150 /* Extra symbol fields that won't fit. */
151 struct xsymbol *x;
154 /* Extra fields to make up a full symbol. */
156 struct xsymbol
158 /* The value of the symbol. */
159 expressionS value;
161 /* Forwards and backwards chain pointers. */
162 struct symbol *next;
163 struct symbol *previous;
165 #ifdef OBJ_SYMFIELD_TYPE
166 OBJ_SYMFIELD_TYPE obj;
167 #endif
169 #ifdef TC_SYMFIELD_TYPE
170 TC_SYMFIELD_TYPE tc;
171 #endif
174 typedef union symbol_entry
176 struct local_symbol lsy;
177 struct symbol sy;
178 } symbol_entry_t;
180 /* Hash function for a symbol_entry. */
182 static hashval_t
183 hash_symbol_entry (const void *e)
185 symbol_entry_t *entry = (symbol_entry_t *) e;
186 if (entry->sy.hash == 0)
187 entry->sy.hash = htab_hash_string (entry->sy.name);
189 return entry->sy.hash;
192 /* Equality function for a symbol_entry. */
194 static int
195 eq_symbol_entry (const void *a, const void *b)
197 const symbol_entry_t *ea = (const symbol_entry_t *) a;
198 const symbol_entry_t *eb = (const symbol_entry_t *) b;
200 return (ea->sy.hash == eb->sy.hash
201 && strcmp (ea->sy.name, eb->sy.name) == 0);
204 static void *
205 symbol_entry_find (htab_t table, const char *name)
207 hashval_t hash = htab_hash_string (name);
208 symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209 hash, name, 0, 0, 0 } };
210 return htab_find_with_hash (table, &needle, hash);
214 /* This is non-zero if symbols are case sensitive, which is the
215 default. */
216 int symbols_case_sensitive = 1;
218 #ifndef WORKING_DOT_WORD
219 extern int new_broken_words;
220 #endif
222 static htab_t sy_hash;
224 /* Below are commented in "symbols.h". */
225 symbolS *symbol_rootP;
226 symbolS *symbol_lastP;
227 symbolS abs_symbol;
228 struct xsymbol abs_symbol_x;
229 symbolS dot_symbol;
230 struct xsymbol dot_symbol_x;
232 #ifdef DEBUG_SYMS
233 #define debug_verify_symchain verify_symbol_chain
234 #else
235 #define debug_verify_symchain(root, last) ((void) 0)
236 #endif
238 #define DOLLAR_LABEL_CHAR '\001'
239 #define LOCAL_LABEL_CHAR '\002'
241 #ifndef TC_LABEL_IS_LOCAL
242 #define TC_LABEL_IS_LOCAL(name) 0
243 #endif
245 struct obstack notes;
247 /* Utility functions to allocate and duplicate memory on the notes
248 obstack, each like the corresponding function without "notes_"
249 prefix. All of these exit on an allocation failure. */
251 void *
252 notes_alloc (size_t size)
254 return obstack_alloc (&notes, size);
257 void *
258 notes_calloc (size_t n, size_t size)
260 size_t amt;
261 void *ret;
262 if (gas_mul_overflow (n, size, &amt))
264 obstack_alloc_failed_handler ();
265 abort ();
267 ret = notes_alloc (amt);
268 memset (ret, 0, amt);
269 return ret;
272 void *
273 notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
275 void *ret = obstack_alloc (&notes, alloc_size);
276 memcpy (ret, src, copy_size);
277 if (alloc_size > copy_size)
278 memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279 return ret;
282 char *
283 notes_strdup (const char *str)
285 size_t len = strlen (str) + 1;
286 return notes_memdup (str, len, len);
289 char *
290 notes_concat (const char *first, ...)
292 va_list args;
293 const char *str;
295 va_start (args, first);
296 for (str = first; str; str = va_arg (args, const char *))
298 size_t size = strlen (str);
299 obstack_grow (&notes, str, size);
301 va_end (args);
302 obstack_1grow (&notes, 0);
303 return obstack_finish (&notes);
306 /* Use with caution! Frees PTR and all more recently allocated memory
307 on the notes obstack. */
309 void
310 notes_free (void *ptr)
312 obstack_free (&notes, ptr);
315 #ifdef TE_PE
316 /* The name of an external symbol which is
317 used to make weak PE symbol names unique. */
318 const char * an_external_name;
319 #endif
321 /* Return a pointer to a new symbol. Die if we can't make a new
322 symbol. Fill in the symbol's values. Add symbol to end of symbol
323 chain.
325 This function should be called in the general case of creating a
326 symbol. However, if the output file symbol table has already been
327 set, and you are certain that this symbol won't be wanted in the
328 output file, you can call symbol_create. */
330 symbolS *
331 symbol_new (const char *name, segT segment, fragS *frag, valueT valu)
333 symbolS *symbolP = symbol_create (name, segment, frag, valu);
335 /* Link to end of symbol chain. */
336 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
338 return symbolP;
341 /* Save a symbol name on a permanent obstack, and convert it according
342 to the object file format. */
344 static const char *
345 save_symbol_name (const char *name)
347 char *ret;
349 gas_assert (name != NULL);
350 ret = notes_strdup (name);
352 #ifdef tc_canonicalize_symbol_name
353 ret = tc_canonicalize_symbol_name (ret);
354 #endif
356 if (! symbols_case_sensitive)
358 char *s;
360 for (s = ret; *s != '\0'; s++)
361 *s = TOUPPER (*s);
364 return ret;
367 static void
368 symbol_init (symbolS *symbolP, const char *name, asection *sec,
369 fragS *frag, valueT valu)
371 symbolP->frag = frag;
372 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373 if (symbolP->bsym == NULL)
374 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375 symbolP->bsym->name = name;
376 symbolP->bsym->section = sec;
378 if (multibyte_handling == multibyte_warn_syms
379 && ! symbolP->flags.local_symbol
380 && sec != undefined_section
381 && ! symbolP->flags.multibyte_warned
382 && scan_for_multibyte_characters ((const unsigned char *) name,
383 (const unsigned char *) name + strlen (name),
384 false /* Do not warn. */))
386 as_warn (_("symbol '%s' contains multibyte characters"), name);
387 symbolP->flags.multibyte_warned = 1;
390 S_SET_VALUE (symbolP, valu);
391 if (sec == reg_section)
392 symbolP->x->value.X_op = O_register;
394 symbol_clear_list_pointers (symbolP);
396 obj_symbol_new_hook (symbolP);
398 #ifdef tc_symbol_new_hook
399 tc_symbol_new_hook (symbolP);
400 #endif
403 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
405 symbolS *
406 symbol_create (const char *name, segT segment, fragS *frag, valueT valu)
408 const char *preserved_copy_of_name;
409 symbolS *symbolP;
410 size_t size;
412 preserved_copy_of_name = save_symbol_name (name);
414 size = sizeof (symbolS) + sizeof (struct xsymbol);
415 symbolP = notes_alloc (size);
417 /* symbol must be born in some fixed state. This seems as good as any. */
418 memset (symbolP, 0, size);
419 symbolP->name = preserved_copy_of_name;
420 symbolP->x = (struct xsymbol *) (symbolP + 1);
422 symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
424 return symbolP;
428 /* Local symbol support. If we can get away with it, we keep only a
429 small amount of information for local symbols. */
431 /* Used for statistics. */
433 static unsigned long local_symbol_count;
434 static unsigned long local_symbol_conversion_count;
436 /* Create a local symbol and insert it into the local hash table. */
438 struct local_symbol *
439 local_symbol_make (const char *name, segT section, fragS *frag, valueT val)
441 const char *name_copy;
442 struct local_symbol *ret;
443 struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
445 ++local_symbol_count;
447 name_copy = save_symbol_name (name);
449 ret = notes_alloc (sizeof *ret);
450 ret->flags = flags;
451 ret->hash = 0;
452 ret->name = name_copy;
453 ret->frag = frag;
454 ret->section = section;
455 ret->value = val;
457 htab_insert (sy_hash, ret, 1);
459 return ret;
462 /* Convert a local symbol into a real symbol. */
464 static symbolS *
465 local_symbol_convert (void *sym)
467 symbol_entry_t *ent = (symbol_entry_t *) sym;
468 struct xsymbol *xtra;
469 valueT val;
471 gas_assert (ent->lsy.flags.local_symbol);
473 ++local_symbol_conversion_count;
475 xtra = notes_alloc (sizeof (*xtra));
476 memset (xtra, 0, sizeof (*xtra));
477 val = ent->lsy.value;
478 ent->sy.x = xtra;
480 /* Local symbols are always either defined or used. */
481 ent->sy.flags.used = 1;
482 ent->sy.flags.local_symbol = 0;
484 symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
485 symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
487 return &ent->sy;
490 static void
491 define_sym_at_dot (symbolS *symbolP)
493 symbolP->frag = frag_now;
494 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
495 S_SET_SEGMENT (symbolP, now_seg);
498 /* We have just seen "<name>:".
499 Creates a struct symbol unless it already exists.
501 Gripes if we are redefining a symbol incompatibly (and ignores it). */
503 symbolS *
504 colon (/* Just seen "x:" - rattle symbols & frags. */
505 const char *sym_name /* Symbol name, as a canonical string. */
506 /* We copy this string: OK to alter later. */)
508 symbolS *symbolP; /* Symbol we are working with. */
510 /* Sun local labels go out of scope whenever a non-local symbol is
511 defined. */
512 if (LOCAL_LABELS_DOLLAR
513 && !bfd_is_local_label_name (stdoutput, sym_name))
514 dollar_label_clear ();
516 #ifndef WORKING_DOT_WORD
517 if (new_broken_words)
519 struct broken_word *a;
520 int possible_bytes;
521 fragS *frag_tmp;
522 char *frag_opcode;
524 if (now_seg == absolute_section)
526 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
527 return NULL;
530 possible_bytes = (md_short_jump_size
531 + new_broken_words * md_long_jump_size);
533 frag_tmp = frag_now;
534 frag_opcode = frag_var (rs_broken_word,
535 possible_bytes,
536 possible_bytes,
537 (relax_substateT) 0,
538 (symbolS *) broken_words,
539 (offsetT) 0,
540 NULL);
542 /* We want to store the pointer to where to insert the jump
543 table in the fr_opcode of the rs_broken_word frag. This
544 requires a little hackery. */
545 while (frag_tmp
546 && (frag_tmp->fr_type != rs_broken_word
547 || frag_tmp->fr_opcode))
548 frag_tmp = frag_tmp->fr_next;
549 know (frag_tmp);
550 frag_tmp->fr_opcode = frag_opcode;
551 new_broken_words = 0;
553 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
554 a->dispfrag = frag_tmp;
556 #endif /* WORKING_DOT_WORD */
558 #ifdef obj_frob_colon
559 obj_frob_colon (sym_name);
560 #endif
562 if ((symbolP = symbol_find (sym_name)) != 0)
564 S_CLEAR_WEAKREFR (symbolP);
565 #ifdef RESOLVE_SYMBOL_REDEFINITION
566 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
567 return symbolP;
568 #endif
569 /* Now check for undefined symbols. */
570 if (symbolP->flags.local_symbol)
572 struct local_symbol *locsym = (struct local_symbol *) symbolP;
574 if (locsym->section != undefined_section
575 && (locsym->frag != frag_now
576 || locsym->section != now_seg
577 || locsym->value != frag_now_fix ()))
579 as_bad (_("symbol `%s' is already defined"), sym_name);
580 return symbolP;
583 locsym->section = now_seg;
584 locsym->frag = frag_now;
585 locsym->value = frag_now_fix ();
587 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
588 || S_IS_COMMON (symbolP)
589 || S_IS_VOLATILE (symbolP))
591 if (S_IS_VOLATILE (symbolP))
593 symbolP = symbol_clone (symbolP, 1);
594 S_SET_VALUE (symbolP, 0);
595 S_CLEAR_VOLATILE (symbolP);
597 if (S_GET_VALUE (symbolP) == 0)
599 define_sym_at_dot (symbolP);
600 #ifdef N_UNDF
601 know (N_UNDF == 0);
602 #endif /* if we have one, it better be zero. */
605 else
607 /* There are still several cases to check:
609 A .comm/.lcomm symbol being redefined as initialized
610 data is OK
612 A .comm/.lcomm symbol being redefined with a larger
613 size is also OK
615 This only used to be allowed on VMS gas, but Sun cc
616 on the sparc also depends on it. */
618 if (((!S_IS_DEBUG (symbolP)
619 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
620 && S_IS_EXTERNAL (symbolP))
621 || S_GET_SEGMENT (symbolP) == bss_section)
622 && (now_seg == data_section
623 || now_seg == bss_section
624 || now_seg == S_GET_SEGMENT (symbolP)))
626 /* Select which of the 2 cases this is. */
627 if (now_seg != data_section)
629 /* New .comm for prev .comm symbol.
631 If the new size is larger we just change its
632 value. If the new size is smaller, we ignore
633 this symbol. */
634 if (S_GET_VALUE (symbolP)
635 < ((unsigned) frag_now_fix ()))
637 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
640 else
642 /* It is a .comm/.lcomm being converted to initialized
643 data. */
644 define_sym_at_dot (symbolP);
647 else
649 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
650 static const char *od_buf = "";
651 #else
652 char od_buf[100];
653 od_buf[0] = '\0';
654 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
655 sprintf (od_buf, "%d.%d.",
656 S_GET_OTHER (symbolP),
657 S_GET_DESC (symbolP));
658 #endif
659 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
660 sym_name,
661 segment_name (S_GET_SEGMENT (symbolP)),
662 od_buf,
663 (long) S_GET_VALUE (symbolP));
665 } /* if the undefined symbol has no value */
667 else
669 /* Don't blow up if the definition is the same. */
670 if (!(frag_now == symbolP->frag
671 && S_GET_VALUE (symbolP) == frag_now_fix ()
672 && S_GET_SEGMENT (symbolP) == now_seg))
674 as_bad (_("symbol `%s' is already defined"), sym_name);
675 symbolP = symbol_clone (symbolP, 0);
676 define_sym_at_dot (symbolP);
681 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
683 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
684 frag_now_fix ());
686 else
688 symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
690 symbol_table_insert (symbolP);
693 if (mri_common_symbol != NULL)
695 /* This symbol is actually being defined within an MRI common
696 section. This requires special handling. */
697 if (symbolP->flags.local_symbol)
698 symbolP = local_symbol_convert (symbolP);
699 symbolP->x->value.X_op = O_symbol;
700 symbolP->x->value.X_add_symbol = mri_common_symbol;
701 symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
702 symbolP->frag = &zero_address_frag;
703 S_SET_SEGMENT (symbolP, expr_section);
704 symbolP->flags.mri_common = 1;
707 #ifdef tc_frob_label
708 tc_frob_label (symbolP);
709 #endif
710 #ifdef obj_frob_label
711 obj_frob_label (symbolP);
712 #endif
713 if (flag_synth_cfi)
714 ginsn_frob_label (symbolP);
716 return symbolP;
719 /* Die if we can't insert the symbol. */
721 void
722 symbol_table_insert (symbolS *symbolP)
724 know (symbolP);
726 htab_insert (sy_hash, symbolP, 1);
729 /* If a symbol name does not exist, create it as undefined, and insert
730 it into the symbol table. Return a pointer to it. */
732 symbolS *
733 symbol_find_or_make (const char *name)
735 symbolS *symbolP;
737 symbolP = symbol_find (name);
739 if (symbolP == NULL)
741 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
743 symbolP = md_undefined_symbol ((char *) name);
744 if (symbolP != NULL)
745 return symbolP;
747 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
748 &zero_address_frag, 0);
749 return symbolP;
752 symbolP = symbol_make (name);
754 symbol_table_insert (symbolP);
755 } /* if symbol wasn't found */
757 return (symbolP);
760 symbolS *
761 symbol_make (const char *name)
763 symbolS *symbolP;
765 /* Let the machine description default it, e.g. for register names. */
766 symbolP = md_undefined_symbol ((char *) name);
768 if (!symbolP)
769 symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
771 return (symbolP);
774 symbolS *
775 symbol_clone (symbolS *orgsymP, int replace)
777 symbolS *newsymP;
778 asymbol *bsymorg, *bsymnew;
780 /* Make sure we never clone the dot special symbol. */
781 gas_assert (orgsymP != &dot_symbol);
783 /* When cloning a local symbol it isn't absolutely necessary to
784 convert the original, but converting makes the code much
785 simpler to cover this unexpected case. As of 2020-08-21
786 symbol_clone won't be called on a local symbol. */
787 if (orgsymP->flags.local_symbol)
788 orgsymP = local_symbol_convert (orgsymP);
789 bsymorg = orgsymP->bsym;
791 newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
792 *newsymP = *orgsymP;
793 newsymP->x = (struct xsymbol *) (newsymP + 1);
794 *newsymP->x = *orgsymP->x;
795 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
796 if (bsymnew == NULL)
797 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
798 newsymP->bsym = bsymnew;
799 bsymnew->name = bsymorg->name;
800 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
801 bsymnew->section = bsymorg->section;
802 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
803 bfd_asymbol_bfd (bsymnew), bsymnew);
805 #ifdef obj_symbol_clone_hook
806 obj_symbol_clone_hook (newsymP, orgsymP);
807 #endif
809 #ifdef tc_symbol_clone_hook
810 tc_symbol_clone_hook (newsymP, orgsymP);
811 #endif
813 if (replace)
815 if (symbol_rootP == orgsymP)
816 symbol_rootP = newsymP;
817 else if (orgsymP->x->previous)
819 orgsymP->x->previous->x->next = newsymP;
820 orgsymP->x->previous = NULL;
822 if (symbol_lastP == orgsymP)
823 symbol_lastP = newsymP;
824 else if (orgsymP->x->next)
825 orgsymP->x->next->x->previous = newsymP;
827 /* Symbols that won't be output can't be external. */
828 S_CLEAR_EXTERNAL (orgsymP);
829 orgsymP->x->previous = orgsymP->x->next = orgsymP;
830 debug_verify_symchain (symbol_rootP, symbol_lastP);
832 symbol_table_insert (newsymP);
834 else
836 /* Symbols that won't be output can't be external. */
837 S_CLEAR_EXTERNAL (newsymP);
838 newsymP->x->previous = newsymP->x->next = newsymP;
841 return newsymP;
844 /* Referenced symbols, if they are forward references, need to be cloned
845 (without replacing the original) so that the value of the referenced
846 symbols at the point of use is saved by the clone. */
848 #undef symbol_clone_if_forward_ref
849 symbolS *
850 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
852 if (symbolP
853 && !symbolP->flags.local_symbol
854 && !symbolP->flags.forward_resolved)
856 symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
857 symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
858 symbolS *add_symbol = orig_add_symbol;
859 symbolS *op_symbol = orig_op_symbol;
861 if (symbolP->flags.forward_ref)
862 is_forward = 1;
864 if (is_forward)
866 /* assign_symbol() clones volatile symbols; pre-existing expressions
867 hold references to the original instance, but want the current
868 value. Just repeat the lookup. */
869 if (add_symbol && S_IS_VOLATILE (add_symbol))
870 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
871 if (op_symbol && S_IS_VOLATILE (op_symbol))
872 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
875 /* Re-using resolving here, as this routine cannot get called from
876 symbol resolution code. */
877 if ((symbolP->bsym->section == expr_section
878 || symbolP->flags.forward_ref)
879 && !symbolP->flags.resolving)
881 symbolP->flags.resolving = 1;
882 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
883 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
884 symbolP->flags.resolving = 0;
887 if (symbolP->flags.forward_ref
888 || add_symbol != orig_add_symbol
889 || op_symbol != orig_op_symbol)
891 if (symbolP != &dot_symbol)
893 symbolP = symbol_clone (symbolP, 0);
894 symbolP->flags.resolving = 0;
896 else
898 symbolP = symbol_temp_new_now ();
899 #ifdef tc_new_dot_label
900 tc_new_dot_label (symbolP);
901 #endif
905 symbolP->x->value.X_add_symbol = add_symbol;
906 symbolP->x->value.X_op_symbol = op_symbol;
907 symbolP->flags.forward_resolved = 1;
910 return symbolP;
913 symbolS *
914 symbol_temp_new (segT seg, fragS *frag, valueT ofs)
916 return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
919 symbolS *
920 symbol_temp_new_now (void)
922 return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
925 symbolS *
926 symbol_temp_new_now_octets (void)
928 return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
931 symbolS *
932 symbol_temp_make (void)
934 return symbol_make (FAKE_LABEL_NAME);
937 /* Implement symbol table lookup.
938 In: A symbol's name as a string: '\0' can't be part of a symbol name.
939 Out: NULL if the name was not in the symbol table, else the address
940 of a struct symbol associated with that name. */
942 symbolS *
943 symbol_find_exact (const char *name)
945 return symbol_find_exact_noref (name, 0);
948 symbolS *
949 symbol_find_exact_noref (const char *name, int noref)
951 symbolS *sym = symbol_entry_find (sy_hash, name);
953 /* Any references to the symbol, except for the reference in
954 .weakref, must clear this flag, such that the symbol does not
955 turn into a weak symbol. Note that we don't have to handle the
956 local_symbol case, since a weakrefd is always promoted out of the
957 local_symbol table when it is turned into a weak symbol. */
958 if (sym && ! noref)
959 S_CLEAR_WEAKREFD (sym);
961 return sym;
964 symbolS *
965 symbol_find (const char *name)
967 return symbol_find_noref (name, 0);
970 symbolS *
971 symbol_find_noref (const char *name, int noref)
973 symbolS * result;
974 char * copy = NULL;
976 #ifdef tc_canonicalize_symbol_name
978 copy = xstrdup (name);
979 name = tc_canonicalize_symbol_name (copy);
981 #endif
983 if (! symbols_case_sensitive)
985 const char *orig;
986 char *copy2 = NULL;
987 unsigned char c;
989 orig = name;
990 if (copy != NULL)
991 copy2 = copy;
992 name = copy = XNEWVEC (char, strlen (name) + 1);
994 while ((c = *orig++) != '\0')
995 *copy++ = TOUPPER (c);
996 *copy = '\0';
998 free (copy2);
999 copy = (char *) name;
1002 result = symbol_find_exact_noref (name, noref);
1003 free (copy);
1004 return result;
1007 /* Once upon a time, symbols were kept in a singly linked list. At
1008 least coff needs to be able to rearrange them from time to time, for
1009 which a doubly linked list is much more convenient. Loic did these
1010 as macros which seemed dangerous to me so they're now functions.
1011 xoxorich. */
1013 /* Link symbol ADDME after symbol TARGET in the chain. */
1015 void
1016 symbol_append (symbolS *addme, symbolS *target,
1017 symbolS **rootPP, symbolS **lastPP)
1019 extern int symbol_table_frozen;
1020 if (symbol_table_frozen)
1021 abort ();
1022 if (addme->flags.local_symbol)
1023 abort ();
1024 if (target != NULL && target->flags.local_symbol)
1025 abort ();
1027 if (target == NULL)
1029 know (*rootPP == NULL);
1030 know (*lastPP == NULL);
1031 addme->x->next = NULL;
1032 addme->x->previous = NULL;
1033 *rootPP = addme;
1034 *lastPP = addme;
1035 return;
1036 } /* if the list is empty */
1038 if (target->x->next != NULL)
1040 target->x->next->x->previous = addme;
1042 else
1044 know (*lastPP == target);
1045 *lastPP = addme;
1046 } /* if we have a next */
1048 addme->x->next = target->x->next;
1049 target->x->next = addme;
1050 addme->x->previous = target;
1052 debug_verify_symchain (symbol_rootP, symbol_lastP);
1055 /* Set the chain pointers of SYMBOL to null. */
1057 void
1058 symbol_clear_list_pointers (symbolS *symbolP)
1060 if (symbolP->flags.local_symbol)
1061 abort ();
1062 symbolP->x->next = NULL;
1063 symbolP->x->previous = NULL;
1066 /* Remove SYMBOLP from the list. */
1068 void
1069 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1071 if (symbolP->flags.local_symbol)
1072 abort ();
1074 if (symbolP == *rootPP)
1076 *rootPP = symbolP->x->next;
1077 } /* if it was the root */
1079 if (symbolP == *lastPP)
1081 *lastPP = symbolP->x->previous;
1082 } /* if it was the tail */
1084 if (symbolP->x->next != NULL)
1086 symbolP->x->next->x->previous = symbolP->x->previous;
1087 } /* if not last */
1089 if (symbolP->x->previous != NULL)
1091 symbolP->x->previous->x->next = symbolP->x->next;
1092 } /* if not first */
1094 debug_verify_symchain (*rootPP, *lastPP);
1097 /* Link symbol ADDME before symbol TARGET in the chain. */
1099 void
1100 symbol_insert (symbolS *addme, symbolS *target,
1101 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
1103 extern int symbol_table_frozen;
1104 if (symbol_table_frozen)
1105 abort ();
1106 if (addme->flags.local_symbol)
1107 abort ();
1108 if (target->flags.local_symbol)
1109 abort ();
1111 if (target->x->previous != NULL)
1113 target->x->previous->x->next = addme;
1115 else
1117 know (*rootPP == target);
1118 *rootPP = addme;
1119 } /* if not first */
1121 addme->x->previous = target->x->previous;
1122 target->x->previous = addme;
1123 addme->x->next = target;
1125 debug_verify_symchain (*rootPP, *lastPP);
1128 void
1129 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
1131 symbolS *symbolP = rootP;
1133 if (symbolP == NULL)
1134 return;
1136 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1138 gas_assert (symbolP->bsym != NULL);
1139 gas_assert (symbolP->flags.local_symbol == 0);
1140 gas_assert (symbolP->x->next->x->previous == symbolP);
1143 gas_assert (lastP == symbolP);
1147 symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1149 return (!s->flags.local_symbol
1150 && ((s->x->next != s
1151 && s->x->next != NULL
1152 && s->x->next->x->previous == s)
1153 || s == lastPP)
1154 && ((s->x->previous != s
1155 && s->x->previous != NULL
1156 && s->x->previous->x->next == s)
1157 || s == rootPP));
1160 #ifdef OBJ_COMPLEX_RELC
1162 static int
1163 use_complex_relocs_for (symbolS * symp)
1165 switch (symp->x->value.X_op)
1167 case O_constant:
1168 return 0;
1170 case O_multiply:
1171 case O_divide:
1172 case O_modulus:
1173 case O_left_shift:
1174 case O_right_shift:
1175 case O_bit_inclusive_or:
1176 case O_bit_or_not:
1177 case O_bit_exclusive_or:
1178 case O_bit_and:
1179 case O_add:
1180 case O_subtract:
1181 case O_eq:
1182 case O_ne:
1183 case O_lt:
1184 case O_le:
1185 case O_ge:
1186 case O_gt:
1187 case O_logical_and:
1188 case O_logical_or:
1189 if ((S_IS_COMMON (symp->x->value.X_op_symbol)
1190 || S_IS_LOCAL (symp->x->value.X_op_symbol))
1191 && S_IS_DEFINED (symp->x->value.X_op_symbol)
1192 && S_GET_SEGMENT (symp->x->value.X_op_symbol) != expr_section)
1194 case O_symbol:
1195 case O_symbol_rva:
1196 case O_uminus:
1197 case O_bit_not:
1198 case O_logical_not:
1199 if ((S_IS_COMMON (symp->x->value.X_add_symbol)
1200 || S_IS_LOCAL (symp->x->value.X_add_symbol))
1201 && S_IS_DEFINED (symp->x->value.X_add_symbol)
1202 && S_GET_SEGMENT (symp->x->value.X_add_symbol) != expr_section)
1203 return 0;
1205 break;
1207 default:
1208 break;
1210 return 1;
1212 #endif
1214 static void
1215 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
1217 const char *file;
1218 unsigned int line;
1219 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1220 segT seg_right = S_GET_SEGMENT (right);
1221 const char *opname;
1223 switch (op)
1225 default:
1226 abort ();
1227 return;
1229 case O_uminus: opname = "-"; break;
1230 case O_bit_not: opname = "~"; break;
1231 case O_logical_not: opname = "!"; break;
1232 case O_multiply: opname = "*"; break;
1233 case O_divide: opname = "/"; break;
1234 case O_modulus: opname = "%"; break;
1235 case O_left_shift: opname = "<<"; break;
1236 case O_right_shift: opname = ">>"; break;
1237 case O_bit_inclusive_or: opname = "|"; break;
1238 case O_bit_or_not: opname = "|~"; break;
1239 case O_bit_exclusive_or: opname = "^"; break;
1240 case O_bit_and: opname = "&"; break;
1241 case O_add: opname = "+"; break;
1242 case O_subtract: opname = "-"; break;
1243 case O_eq: opname = "=="; break;
1244 case O_ne: opname = "!="; break;
1245 case O_lt: opname = "<"; break;
1246 case O_le: opname = "<="; break;
1247 case O_ge: opname = ">="; break;
1248 case O_gt: opname = ">"; break;
1249 case O_logical_and: opname = "&&"; break;
1250 case O_logical_or: opname = "||"; break;
1253 if (expr_symbol_where (symp, &file, &line))
1255 if (left)
1256 as_bad_where (file, line,
1257 _("invalid operands (%s and %s sections) for `%s'"),
1258 seg_left->name, seg_right->name, opname);
1259 else
1260 as_bad_where (file, line,
1261 _("invalid operand (%s section) for `%s'"),
1262 seg_right->name, opname);
1264 else
1266 const char *sname = S_GET_NAME (symp);
1268 if (left)
1269 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1270 seg_left->name, seg_right->name, opname, sname);
1271 else
1272 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1273 seg_right->name, opname, sname);
1277 /* Resolve the value of a symbol. This is called during the final
1278 pass over the symbol table to resolve any symbols with complex
1279 values. */
1281 valueT
1282 resolve_symbol_value (symbolS *symp)
1284 int resolved;
1285 valueT final_val;
1286 segT final_seg;
1288 if (symp->flags.local_symbol)
1290 struct local_symbol *locsym = (struct local_symbol *) symp;
1292 final_val = locsym->value;
1293 if (locsym->flags.resolved)
1294 return final_val;
1296 /* Symbols whose section has SEC_ELF_OCTETS set,
1297 resolve to octets instead of target bytes. */
1298 if (locsym->section->flags & SEC_OCTETS)
1299 final_val += locsym->frag->fr_address;
1300 else
1301 final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1303 if (finalize_syms)
1305 locsym->value = final_val;
1306 locsym->flags.resolved = 1;
1309 return final_val;
1312 if (symp->flags.resolved)
1314 final_val = 0;
1315 while (symp->x->value.X_op == O_symbol)
1317 final_val += symp->x->value.X_add_number;
1318 symp = symp->x->value.X_add_symbol;
1319 if (symp->flags.local_symbol)
1321 struct local_symbol *locsym = (struct local_symbol *) symp;
1322 final_val += locsym->value;
1323 return final_val;
1325 if (!symp->flags.resolved)
1326 return 0;
1328 if (symp->x->value.X_op == O_constant)
1329 final_val += symp->x->value.X_add_number;
1330 else
1331 final_val = 0;
1332 return final_val;
1335 resolved = 0;
1336 final_seg = S_GET_SEGMENT (symp);
1338 if (symp->flags.resolving)
1340 if (finalize_syms)
1341 as_bad (_("symbol definition loop encountered at `%s'"),
1342 S_GET_NAME (symp));
1343 final_val = 0;
1344 resolved = 1;
1346 #ifdef OBJ_COMPLEX_RELC
1347 else if (final_seg == expr_section
1348 && use_complex_relocs_for (symp))
1350 symbolS * relc_symbol = NULL;
1351 char * relc_symbol_name = NULL;
1353 relc_symbol_name = symbol_relc_make_expr (& symp->x->value);
1355 /* For debugging, print out conversion input & output. */
1356 #ifdef DEBUG_SYMS
1357 print_expr (& symp->x->value);
1358 if (relc_symbol_name)
1359 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1360 #endif
1362 if (relc_symbol_name != NULL)
1363 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1364 &zero_address_frag, 0);
1366 if (relc_symbol == NULL)
1368 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1369 S_GET_NAME (symp));
1370 resolved = 0;
1372 else
1374 symbol_table_insert (relc_symbol);
1376 /* S_CLEAR_EXTERNAL (relc_symbol); */
1377 if (symp->bsym->flags & BSF_SRELC)
1378 relc_symbol->bsym->flags |= BSF_SRELC;
1379 else
1380 relc_symbol->bsym->flags |= BSF_RELC;
1381 /* symp->bsym->flags |= BSF_RELC; */
1382 copy_symbol_attributes (symp, relc_symbol);
1383 symp->x->value.X_op = O_symbol;
1384 symp->x->value.X_add_symbol = relc_symbol;
1385 symp->x->value.X_add_number = 0;
1386 resolved = 1;
1389 final_val = 0;
1390 final_seg = undefined_section;
1391 goto exit_dont_set_value;
1393 #endif
1394 else
1396 symbolS *add_symbol, *op_symbol;
1397 offsetT left, right;
1398 segT seg_left, seg_right;
1399 operatorT op;
1400 int move_seg_ok;
1402 symp->flags.resolving = 1;
1404 /* Help out with CSE. */
1405 add_symbol = symp->x->value.X_add_symbol;
1406 op_symbol = symp->x->value.X_op_symbol;
1407 final_val = symp->x->value.X_add_number;
1408 op = symp->x->value.X_op;
1410 switch (op)
1412 default:
1413 BAD_CASE (op);
1414 break;
1416 case O_md1:
1417 case O_md2:
1418 case O_md3:
1419 case O_md4:
1420 case O_md5:
1421 case O_md6:
1422 case O_md7:
1423 case O_md8:
1424 case O_md9:
1425 case O_md10:
1426 case O_md11:
1427 case O_md12:
1428 case O_md13:
1429 case O_md14:
1430 case O_md15:
1431 case O_md16:
1432 case O_md17:
1433 case O_md18:
1434 case O_md19:
1435 case O_md20:
1436 case O_md21:
1437 case O_md22:
1438 case O_md23:
1439 case O_md24:
1440 case O_md25:
1441 case O_md26:
1442 case O_md27:
1443 case O_md28:
1444 case O_md29:
1445 case O_md30:
1446 case O_md31:
1447 case O_md32:
1448 #ifdef md_resolve_symbol
1449 resolved = md_resolve_symbol (symp, &final_val, &final_seg);
1450 if (resolved)
1451 break;
1452 #endif
1453 goto exit_dont_set_value;
1455 case O_absent:
1456 final_val = 0;
1457 /* Fall through. */
1459 case O_constant:
1460 /* Symbols whose section has SEC_ELF_OCTETS set,
1461 resolve to octets instead of target bytes. */
1462 if (symp->bsym->section->flags & SEC_OCTETS)
1463 final_val += symp->frag->fr_address;
1464 else
1465 final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1466 if (final_seg == expr_section)
1467 final_seg = absolute_section;
1468 /* Fall through. */
1470 case O_register:
1471 resolved = 1;
1472 break;
1474 case O_symbol:
1475 case O_symbol_rva:
1476 case O_secidx:
1477 left = resolve_symbol_value (add_symbol);
1478 seg_left = S_GET_SEGMENT (add_symbol);
1479 if (finalize_syms)
1480 symp->x->value.X_op_symbol = NULL;
1482 do_symbol:
1483 if (S_IS_WEAKREFR (symp))
1485 gas_assert (final_val == 0);
1486 if (S_IS_WEAKREFR (add_symbol))
1488 gas_assert (add_symbol->x->value.X_op == O_symbol
1489 && add_symbol->x->value.X_add_number == 0);
1490 add_symbol = add_symbol->x->value.X_add_symbol;
1491 gas_assert (! S_IS_WEAKREFR (add_symbol));
1492 symp->x->value.X_add_symbol = add_symbol;
1496 if (symp->flags.mri_common)
1498 /* This is a symbol inside an MRI common section. The
1499 relocation routines are going to handle it specially.
1500 Don't change the value. */
1501 resolved = symbol_resolved_p (add_symbol);
1502 break;
1505 /* Don't leave symbol loops. */
1506 if (finalize_syms
1507 && !add_symbol->flags.local_symbol
1508 && add_symbol->flags.resolving)
1509 break;
1511 if (finalize_syms && final_val == 0
1512 #ifdef OBJ_XCOFF
1513 /* Avoid changing symp's "within" when dealing with
1514 AIX debug symbols. For some storage classes, "within"
1515 have a special meaning.
1516 C_DWARF should behave like on Linux, thus this check
1517 isn't done to be closer. */
1518 && ((symbol_get_bfdsym (symp)->flags & BSF_DEBUGGING) == 0
1519 || (S_GET_STORAGE_CLASS (symp) == C_DWARF))
1520 #endif
1523 if (add_symbol->flags.local_symbol)
1524 add_symbol = local_symbol_convert (add_symbol);
1525 copy_symbol_attributes (symp, add_symbol);
1528 /* If we have equated this symbol to an undefined or common
1529 symbol, keep X_op set to O_symbol, and don't change
1530 X_add_number. This permits the routine which writes out
1531 relocation to detect this case, and convert the
1532 relocation to be against the symbol to which this symbol
1533 is equated. */
1534 if (seg_left == undefined_section
1535 || bfd_is_com_section (seg_left)
1536 #if defined (OBJ_COFF) && defined (TE_PE)
1537 || S_IS_WEAK (add_symbol)
1538 #endif
1539 || (finalize_syms
1540 && ((final_seg == expr_section
1541 && seg_left != expr_section
1542 && seg_left != absolute_section)
1543 || symbol_shadow_p (symp))))
1545 if (finalize_syms)
1547 symp->x->value.X_op = O_symbol;
1548 symp->x->value.X_add_symbol = add_symbol;
1549 symp->x->value.X_add_number = final_val;
1550 /* Use X_op_symbol as a flag. */
1551 symp->x->value.X_op_symbol = add_symbol;
1553 final_seg = seg_left;
1554 final_val += symp->frag->fr_address + left;
1555 resolved = symbol_resolved_p (add_symbol);
1556 symp->flags.resolving = 0;
1558 if (op == O_secidx && seg_left != undefined_section)
1560 final_val = 0;
1561 break;
1564 goto exit_dont_set_value;
1566 else
1568 final_val += symp->frag->fr_address + left;
1569 if (final_seg == expr_section || final_seg == undefined_section)
1570 final_seg = seg_left;
1573 resolved = symbol_resolved_p (add_symbol);
1574 if (S_IS_WEAKREFR (symp))
1576 symp->flags.resolving = 0;
1577 goto exit_dont_set_value;
1579 break;
1581 case O_uminus:
1582 case O_bit_not:
1583 case O_logical_not:
1584 left = resolve_symbol_value (add_symbol);
1585 seg_left = S_GET_SEGMENT (add_symbol);
1587 /* By reducing these to the relevant dyadic operator, we get
1588 !S -> S == 0 permitted on anything,
1589 -S -> 0 - S only permitted on absolute
1590 ~S -> S ^ ~0 only permitted on absolute */
1591 if (op != O_logical_not && seg_left != absolute_section
1592 && finalize_syms)
1593 report_op_error (symp, NULL, op, add_symbol);
1595 if (final_seg == expr_section || final_seg == undefined_section)
1596 final_seg = absolute_section;
1598 if (op == O_uminus)
1599 left = -left;
1600 else if (op == O_logical_not)
1601 left = !left;
1602 else
1603 left = ~left;
1605 final_val += left + symp->frag->fr_address;
1607 resolved = symbol_resolved_p (add_symbol);
1608 break;
1610 case O_multiply:
1611 case O_divide:
1612 case O_modulus:
1613 case O_left_shift:
1614 case O_right_shift:
1615 case O_bit_inclusive_or:
1616 case O_bit_or_not:
1617 case O_bit_exclusive_or:
1618 case O_bit_and:
1619 case O_add:
1620 case O_subtract:
1621 case O_eq:
1622 case O_ne:
1623 case O_lt:
1624 case O_le:
1625 case O_ge:
1626 case O_gt:
1627 case O_logical_and:
1628 case O_logical_or:
1629 left = resolve_symbol_value (add_symbol);
1630 right = resolve_symbol_value (op_symbol);
1631 seg_left = S_GET_SEGMENT (add_symbol);
1632 seg_right = S_GET_SEGMENT (op_symbol);
1634 /* Simplify addition or subtraction of a constant by folding the
1635 constant into X_add_number. */
1636 if (op == O_add)
1638 if (seg_right == absolute_section)
1640 final_val += right;
1641 goto do_symbol;
1643 else if (seg_left == absolute_section)
1645 final_val += left;
1646 add_symbol = op_symbol;
1647 left = right;
1648 seg_left = seg_right;
1649 goto do_symbol;
1652 else if (op == O_subtract)
1654 if (seg_right == absolute_section)
1656 final_val -= right;
1657 goto do_symbol;
1661 move_seg_ok = 1;
1662 /* Equality and non-equality tests are permitted on anything.
1663 Subtraction, and other comparison operators are permitted if
1664 both operands are in the same section. Otherwise, both
1665 operands must be absolute. We already handled the case of
1666 addition or subtraction of a constant above. This will
1667 probably need to be changed for an object file format which
1668 supports arbitrary expressions. */
1669 if (!(seg_left == absolute_section
1670 && seg_right == absolute_section)
1671 && !(op == O_eq || op == O_ne)
1672 && !((op == O_subtract
1673 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1674 && seg_left == seg_right
1675 && (seg_left != undefined_section
1676 || add_symbol == op_symbol)))
1678 /* Don't emit messages unless we're finalizing the symbol value,
1679 otherwise we may get the same message multiple times. */
1680 if (finalize_syms)
1681 report_op_error (symp, add_symbol, op, op_symbol);
1682 /* However do not move the symbol into the absolute section
1683 if it cannot currently be resolved - this would confuse
1684 other parts of the assembler into believing that the
1685 expression had been evaluated to zero. */
1686 else
1687 move_seg_ok = 0;
1690 if (move_seg_ok
1691 && (final_seg == expr_section || final_seg == undefined_section))
1692 final_seg = absolute_section;
1694 /* Check for division by zero. */
1695 if ((op == O_divide || op == O_modulus) && right == 0)
1697 /* If seg_right is not absolute_section, then we've
1698 already issued a warning about using a bad symbol. */
1699 if (seg_right == absolute_section && finalize_syms)
1701 const char *file;
1702 unsigned int line;
1704 if (expr_symbol_where (symp, &file, &line))
1705 as_bad_where (file, line, _("division by zero"));
1706 else
1707 as_bad (_("division by zero when setting `%s'"),
1708 S_GET_NAME (symp));
1711 right = 1;
1713 if ((op == O_left_shift || op == O_right_shift)
1714 && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1716 as_warn_value_out_of_range (_("shift count"), right, 0,
1717 sizeof (valueT) * CHAR_BIT - 1,
1718 NULL, 0);
1719 left = right = 0;
1722 switch (symp->x->value.X_op)
1724 case O_multiply: left *= right; break;
1725 case O_divide: left /= right; break;
1726 case O_modulus: left %= right; break;
1727 case O_left_shift:
1728 left = (valueT) left << (valueT) right; break;
1729 case O_right_shift:
1730 left = (valueT) left >> (valueT) right; break;
1731 case O_bit_inclusive_or: left |= right; break;
1732 case O_bit_or_not: left |= ~right; break;
1733 case O_bit_exclusive_or: left ^= right; break;
1734 case O_bit_and: left &= right; break;
1735 case O_add: left += right; break;
1736 case O_subtract: left -= right; break;
1737 case O_eq:
1738 case O_ne:
1739 left = (left == right && seg_left == seg_right
1740 && (seg_left != undefined_section
1741 || add_symbol == op_symbol)
1742 ? ~ (offsetT) 0 : 0);
1743 if (symp->x->value.X_op == O_ne)
1744 left = ~left;
1745 break;
1746 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1747 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1748 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1749 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1750 case O_logical_and: left = left && right; break;
1751 case O_logical_or: left = left || right; break;
1753 case O_illegal:
1754 case O_absent:
1755 case O_constant:
1756 /* See PR 20895 for a reproducer. */
1757 as_bad (_("Invalid operation on symbol"));
1758 goto exit_dont_set_value;
1760 default:
1761 abort ();
1764 final_val += symp->frag->fr_address + left;
1765 if (final_seg == expr_section || final_seg == undefined_section)
1767 if (seg_left == undefined_section
1768 || seg_right == undefined_section)
1769 final_seg = undefined_section;
1770 else if (seg_left == absolute_section)
1771 final_seg = seg_right;
1772 else
1773 final_seg = seg_left;
1775 resolved = (symbol_resolved_p (add_symbol)
1776 && symbol_resolved_p (op_symbol));
1777 break;
1779 case O_big:
1780 case O_illegal:
1781 /* Give an error (below) if not in expr_section. We don't
1782 want to worry about expr_section symbols, because they
1783 are fictional (they are created as part of expression
1784 resolution), and any problems may not actually mean
1785 anything. */
1786 break;
1789 symp->flags.resolving = 0;
1792 if (finalize_syms)
1793 S_SET_VALUE (symp, final_val);
1795 exit_dont_set_value:
1796 /* Always set the segment, even if not finalizing the value.
1797 The segment is used to determine whether a symbol is defined. */
1798 S_SET_SEGMENT (symp, final_seg);
1800 /* Don't worry if we can't resolve an expr_section symbol. */
1801 if (finalize_syms)
1803 if (resolved)
1804 symp->flags.resolved = 1;
1805 else if (S_GET_SEGMENT (symp) != expr_section)
1807 as_bad (_("can't resolve value for symbol `%s'"),
1808 S_GET_NAME (symp));
1809 symp->flags.resolved = 1;
1813 return final_val;
1816 /* A static function passed to hash_traverse. */
1818 static int
1819 resolve_local_symbol (void **slot, void *arg ATTRIBUTE_UNUSED)
1821 symbol_entry_t *entry = *((symbol_entry_t **) slot);
1822 if (entry->sy.flags.local_symbol)
1823 resolve_symbol_value (&entry->sy);
1825 return 1;
1828 /* Resolve all local symbols. */
1830 void
1831 resolve_local_symbol_values (void)
1833 htab_traverse_noresize (sy_hash, resolve_local_symbol, NULL);
1836 /* Obtain the current value of a symbol without changing any
1837 sub-expressions used. */
1840 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1842 symbolS *symbolP = *symbolPP;
1844 if (symbolP->flags.local_symbol)
1846 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1848 *valueP = locsym->value;
1849 *segP = locsym->section;
1850 *fragPP = locsym->frag;
1852 else
1854 expressionS exp = symbolP->x->value;
1856 if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1858 int resolved;
1860 if (symbolP->flags.resolving)
1861 return 0;
1862 symbolP->flags.resolving = 1;
1863 resolved = resolve_expression (&exp);
1864 symbolP->flags.resolving = 0;
1865 if (!resolved)
1866 return 0;
1868 switch (exp.X_op)
1870 case O_constant:
1871 case O_register:
1872 if (!symbol_equated_p (symbolP))
1873 break;
1874 /* Fallthru. */
1875 case O_symbol:
1876 case O_symbol_rva:
1877 symbolP = exp.X_add_symbol;
1878 break;
1879 default:
1880 return 0;
1884 *symbolPP = symbolP;
1886 /* A bogus input file can result in resolve_expression()
1887 generating a local symbol, so we have to check again. */
1888 if (symbolP->flags.local_symbol)
1890 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1892 *valueP = locsym->value;
1893 *segP = locsym->section;
1894 *fragPP = locsym->frag;
1896 else
1898 *valueP = exp.X_add_number;
1899 *segP = symbolP->bsym->section;
1900 *fragPP = symbolP->frag;
1903 if (*segP == expr_section)
1904 switch (exp.X_op)
1906 case O_constant: *segP = absolute_section; break;
1907 case O_register: *segP = reg_section; break;
1908 default: break;
1912 return 1;
1915 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1916 They are *really* local. That is, they go out of scope whenever we see a
1917 label that isn't local. Also, like fb labels, there can be multiple
1918 instances of a dollar label. Therefor, we name encode each instance with
1919 the instance number, keep a list of defined symbols separate from the real
1920 symbol table, and we treat these buggers as a sparse array. */
1922 typedef unsigned int dollar_ent;
1923 static dollar_ent *dollar_labels;
1924 static dollar_ent *dollar_label_instances;
1925 static char *dollar_label_defines;
1926 static size_t dollar_label_count;
1927 static size_t dollar_label_max;
1930 dollar_label_defined (unsigned int label)
1932 dollar_ent *i;
1934 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1936 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1937 if (*i == label)
1938 return dollar_label_defines[i - dollar_labels];
1940 /* If we get here, label isn't defined. */
1941 return 0;
1944 static unsigned int
1945 dollar_label_instance (unsigned int label)
1947 dollar_ent *i;
1949 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1951 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1952 if (*i == label)
1953 return (dollar_label_instances[i - dollar_labels]);
1955 /* If we get here, we haven't seen the label before.
1956 Therefore its instance count is zero. */
1957 return 0;
1960 void
1961 dollar_label_clear (void)
1963 if (dollar_label_count)
1964 memset (dollar_label_defines, '\0', dollar_label_count);
1967 #define DOLLAR_LABEL_BUMP_BY 10
1969 void
1970 define_dollar_label (unsigned int label)
1972 dollar_ent *i;
1974 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1975 if (*i == label)
1977 ++dollar_label_instances[i - dollar_labels];
1978 dollar_label_defines[i - dollar_labels] = 1;
1979 return;
1982 /* If we get to here, we don't have label listed yet. */
1984 if (dollar_labels == NULL)
1986 dollar_labels = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1987 dollar_label_instances = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1988 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1989 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1990 dollar_label_count = 0;
1992 else if (dollar_label_count == dollar_label_max)
1994 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1995 dollar_labels = XRESIZEVEC (dollar_ent, dollar_labels,
1996 dollar_label_max);
1997 dollar_label_instances = XRESIZEVEC (dollar_ent,
1998 dollar_label_instances,
1999 dollar_label_max);
2000 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
2001 dollar_label_max);
2002 } /* if we needed to grow */
2004 dollar_labels[dollar_label_count] = label;
2005 dollar_label_instances[dollar_label_count] = 1;
2006 dollar_label_defines[dollar_label_count] = 1;
2007 ++dollar_label_count;
2010 /* Caller must copy returned name: we re-use the area for the next name.
2012 The mth occurrence of label n: is turned into the symbol "Ln^Am"
2013 where n is the label number and m is the instance number. "L" makes
2014 it a label discarded unless debugging and "^A"('\1') ensures no
2015 ordinary symbol SHOULD get the same name as a local label
2016 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
2018 fb labels get the same treatment, except that ^B is used in place
2019 of ^A.
2021 AUGEND is 0 for current instance, 1 for new instance. */
2023 char *
2024 dollar_label_name (unsigned int n, unsigned int augend)
2026 /* Returned to caller, then copied. Used for created names ("4f"). */
2027 static char symbol_name_build[24];
2028 char *p = symbol_name_build;
2030 #ifdef LOCAL_LABEL_PREFIX
2031 *p++ = LOCAL_LABEL_PREFIX;
2032 #endif
2033 sprintf (p, "L%u%c%u",
2034 n, DOLLAR_LABEL_CHAR, dollar_label_instance (n) + augend);
2035 return symbol_name_build;
2038 /* Somebody else's idea of local labels. They are made by "n:" where n
2039 is any decimal digit. Refer to them with
2040 "nb" for previous (backward) n:
2041 or "nf" for next (forward) n:.
2043 We do a little better and let n be any number, not just a single digit, but
2044 since the other guy's assembler only does ten, we treat the first ten
2045 specially.
2047 Like someone else's assembler, we have one set of local label counters for
2048 entire assembly, not one set per (sub)segment like in most assemblers. This
2049 implies that one can refer to a label in another segment, and indeed some
2050 crufty compilers have done just that.
2052 Since there could be a LOT of these things, treat them as a sparse
2053 array. */
2055 #define FB_LABEL_SPECIAL (10)
2057 typedef unsigned int fb_ent;
2058 static fb_ent fb_low_counter[FB_LABEL_SPECIAL];
2059 static fb_ent *fb_labels;
2060 static fb_ent *fb_label_instances;
2061 static size_t fb_label_count;
2062 static size_t fb_label_max;
2064 /* This must be more than FB_LABEL_SPECIAL. */
2065 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
2067 static void
2068 fb_label_init (void)
2070 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
2073 /* Add one to the instance number of this fb label. */
2075 void
2076 fb_label_instance_inc (unsigned int label)
2078 fb_ent *i;
2080 if (label < FB_LABEL_SPECIAL)
2082 ++fb_low_counter[label];
2083 return;
2086 if (fb_labels != NULL)
2088 for (i = fb_labels + FB_LABEL_SPECIAL;
2089 i < fb_labels + fb_label_count; ++i)
2091 if (*i == label)
2093 ++fb_label_instances[i - fb_labels];
2094 return;
2095 } /* if we find it */
2096 } /* for each existing label */
2099 /* If we get to here, we don't have label listed yet. */
2101 if (fb_labels == NULL)
2103 fb_labels = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2104 fb_label_instances = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2105 fb_label_max = FB_LABEL_BUMP_BY;
2106 fb_label_count = FB_LABEL_SPECIAL;
2109 else if (fb_label_count == fb_label_max)
2111 fb_label_max += FB_LABEL_BUMP_BY;
2112 fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
2113 fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
2114 fb_label_max);
2115 } /* if we needed to grow */
2117 fb_labels[fb_label_count] = label;
2118 fb_label_instances[fb_label_count] = 1;
2119 ++fb_label_count;
2122 static unsigned int
2123 fb_label_instance (unsigned int label)
2125 fb_ent *i;
2127 if (label < FB_LABEL_SPECIAL)
2128 return (fb_low_counter[label]);
2130 if (fb_labels != NULL)
2132 for (i = fb_labels + FB_LABEL_SPECIAL;
2133 i < fb_labels + fb_label_count; ++i)
2135 if (*i == label)
2136 return (fb_label_instances[i - fb_labels]);
2140 /* We didn't find the label, so this must be a reference to the
2141 first instance. */
2142 return 0;
2145 /* Caller must copy returned name: we re-use the area for the next name.
2147 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2148 where n is the label number and m is the instance number. "L" makes
2149 it a label discarded unless debugging and "^B"('\2') ensures no
2150 ordinary symbol SHOULD get the same name as a local label
2151 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2153 dollar labels get the same treatment, except that ^A is used in
2154 place of ^B.
2156 AUGEND is 0 for nb, 1 for n:, nf. */
2158 char *
2159 fb_label_name (unsigned int n, unsigned int augend)
2161 /* Returned to caller, then copied. Used for created names ("4f"). */
2162 static char symbol_name_build[24];
2163 char *p = symbol_name_build;
2165 #ifdef TC_MMIX
2166 know (augend <= 2 /* See mmix_fb_label. */);
2167 #else
2168 know (augend <= 1);
2169 #endif
2171 #ifdef LOCAL_LABEL_PREFIX
2172 *p++ = LOCAL_LABEL_PREFIX;
2173 #endif
2174 sprintf (p, "L%u%c%u",
2175 n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2176 return symbol_name_build;
2179 /* Decode name that may have been generated by foo_label_name() above.
2180 If the name wasn't generated by foo_label_name(), then return it
2181 unaltered. This is used for error messages. */
2183 char *
2184 decode_local_label_name (char *s)
2186 char *p;
2187 char *symbol_decode;
2188 int label_number;
2189 int instance_number;
2190 const char *type;
2191 const char *message_format;
2192 int lindex = 0;
2194 #ifdef LOCAL_LABEL_PREFIX
2195 if (s[lindex] == LOCAL_LABEL_PREFIX)
2196 ++lindex;
2197 #endif
2199 if (s[lindex] != 'L')
2200 return s;
2202 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
2203 label_number = (10 * label_number) + *p - '0';
2205 if (*p == DOLLAR_LABEL_CHAR)
2206 type = "dollar";
2207 else if (*p == LOCAL_LABEL_CHAR)
2208 type = "fb";
2209 else
2210 return s;
2212 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
2213 instance_number = (10 * instance_number) + *p - '0';
2215 message_format = _("\"%d\" (instance number %d of a %s label)");
2216 symbol_decode = notes_alloc (strlen (message_format) + 30);
2217 sprintf (symbol_decode, message_format, label_number, instance_number, type);
2219 return symbol_decode;
2222 /* Get the value of a symbol. */
2224 valueT
2225 S_GET_VALUE_WHERE (symbolS *s, const char * file, unsigned int line)
2227 if (s->flags.local_symbol)
2228 return resolve_symbol_value (s);
2230 if (!s->flags.resolved)
2232 valueT val = resolve_symbol_value (s);
2233 if (!finalize_syms)
2234 return val;
2236 if (S_IS_WEAKREFR (s))
2237 return S_GET_VALUE (s->x->value.X_add_symbol);
2239 if (s->x->value.X_op != O_constant)
2241 if (! s->flags.resolved
2242 || s->x->value.X_op != O_symbol
2243 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
2245 if (strcmp (S_GET_NAME (s), FAKE_LABEL_NAME) == 0)
2246 as_bad_where (file, line, _("expression is too complex to be resolved or converted into relocations"));
2247 else if (file != NULL)
2248 as_bad_where (file, line, _("attempt to get value of unresolved symbol `%s'"),
2249 S_GET_NAME (s));
2250 else
2251 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2252 S_GET_NAME (s));
2255 return (valueT) s->x->value.X_add_number;
2258 valueT
2259 S_GET_VALUE (symbolS *s)
2261 return S_GET_VALUE_WHERE (s, NULL, 0);
2264 /* Set the value of a symbol. */
2266 void
2267 S_SET_VALUE (symbolS *s, valueT val)
2269 if (s->flags.local_symbol)
2271 ((struct local_symbol *) s)->value = val;
2272 return;
2275 s->x->value.X_op = O_constant;
2276 s->x->value.X_add_number = (offsetT) val;
2277 s->x->value.X_unsigned = 0;
2278 S_CLEAR_WEAKREFR (s);
2281 void
2282 copy_symbol_attributes (symbolS *dest, symbolS *src)
2284 if (dest->flags.local_symbol)
2285 dest = local_symbol_convert (dest);
2286 if (src->flags.local_symbol)
2287 src = local_symbol_convert (src);
2289 /* In an expression, transfer the settings of these flags.
2290 The user can override later, of course. */
2291 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2292 | BSF_GNU_INDIRECT_FUNCTION)
2293 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2295 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2296 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2297 #endif
2299 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2300 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2301 #endif
2305 S_IS_FUNCTION (const symbolS *s)
2307 flagword flags;
2309 if (s->flags.local_symbol)
2310 return 0;
2312 flags = s->bsym->flags;
2314 return (flags & BSF_FUNCTION) != 0;
2318 S_IS_EXTERNAL (const symbolS *s)
2320 flagword flags;
2322 if (s->flags.local_symbol)
2323 return 0;
2325 flags = s->bsym->flags;
2327 /* Sanity check. */
2328 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2329 abort ();
2331 return (flags & BSF_GLOBAL) != 0;
2335 S_IS_WEAK (const symbolS *s)
2337 if (s->flags.local_symbol)
2338 return 0;
2339 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2340 could probably handle a WEAKREFR as always weak though. E.g., if
2341 the referenced symbol has lost its weak status, there's no reason
2342 to keep handling the weakrefr as if it was weak. */
2343 if (S_IS_WEAKREFR (s))
2344 return S_IS_WEAK (s->x->value.X_add_symbol);
2345 return (s->bsym->flags & BSF_WEAK) != 0;
2349 S_IS_WEAKREFR (const symbolS *s)
2351 if (s->flags.local_symbol)
2352 return 0;
2353 return s->flags.weakrefr != 0;
2357 S_IS_WEAKREFD (const symbolS *s)
2359 if (s->flags.local_symbol)
2360 return 0;
2361 return s->flags.weakrefd != 0;
2365 S_IS_COMMON (const symbolS *s)
2367 if (s->flags.local_symbol)
2368 return 0;
2369 return bfd_is_com_section (s->bsym->section);
2373 S_IS_DEFINED (const symbolS *s)
2375 if (s->flags.local_symbol)
2376 return ((struct local_symbol *) s)->section != undefined_section;
2377 return s->bsym->section != undefined_section;
2381 #ifndef EXTERN_FORCE_RELOC
2382 #define EXTERN_FORCE_RELOC IS_ELF
2383 #endif
2385 /* Return true for symbols that should not be reduced to section
2386 symbols or eliminated from expressions, because they may be
2387 overridden by the linker. */
2389 S_FORCE_RELOC (const symbolS *s, int strict)
2391 segT sec;
2392 if (s->flags.local_symbol)
2393 sec = ((struct local_symbol *) s)->section;
2394 else
2396 if ((strict
2397 && ((s->bsym->flags & BSF_WEAK) != 0
2398 || (EXTERN_FORCE_RELOC
2399 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2400 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2401 return true;
2402 sec = s->bsym->section;
2404 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2408 S_IS_DEBUG (const symbolS *s)
2410 if (s->flags.local_symbol)
2411 return 0;
2412 if (s->bsym->flags & BSF_DEBUGGING)
2413 return 1;
2414 return 0;
2418 S_IS_LOCAL (const symbolS *s)
2420 flagword flags;
2421 const char *name;
2423 if (s->flags.local_symbol)
2424 return 1;
2426 if (S_IS_EXTERNAL (s))
2427 return 0;
2429 if (bfd_asymbol_section (s->bsym) == reg_section)
2430 return 1;
2432 flags = s->bsym->flags;
2434 if (flag_strip_local_absolute
2435 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2436 the source file even when the object file is stripped. */
2437 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2438 && bfd_asymbol_section (s->bsym) == absolute_section)
2439 return 1;
2441 name = S_GET_NAME (s);
2442 return (name != NULL
2443 && ! S_IS_DEBUG (s)
2444 && (strchr (name, DOLLAR_LABEL_CHAR)
2445 || strchr (name, LOCAL_LABEL_CHAR)
2446 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2447 || strchr (name, FAKE_LABEL_CHAR)
2448 #endif
2449 || TC_LABEL_IS_LOCAL (name)
2450 || (! flag_keep_locals
2451 && (bfd_is_local_label (stdoutput, s->bsym)
2452 || (flag_mri
2453 && name[0] == '?'
2454 && name[1] == '?')))));
2458 S_IS_STABD (const symbolS *s)
2460 return S_GET_NAME (s) == 0;
2464 S_CAN_BE_REDEFINED (const symbolS *s)
2466 if (s->flags.local_symbol)
2467 return (((struct local_symbol *) s)->frag
2468 == &predefined_address_frag);
2469 /* Permit register names to be redefined. */
2470 return s->x->value.X_op == O_register;
2474 S_IS_VOLATILE (const symbolS *s)
2476 if (s->flags.local_symbol)
2477 return 0;
2478 return s->flags.volatil;
2482 S_IS_FORWARD_REF (const symbolS *s)
2484 if (s->flags.local_symbol)
2485 return 0;
2486 return s->flags.forward_ref;
2489 const char *
2490 S_GET_NAME (const symbolS *s)
2492 return s->name;
2495 segT
2496 S_GET_SEGMENT (const symbolS *s)
2498 if (s->flags.local_symbol)
2499 return ((struct local_symbol *) s)->section;
2500 return s->bsym->section;
2503 void
2504 S_SET_SEGMENT (symbolS *s, segT seg)
2506 if (s->flags.local_symbol)
2508 ((struct local_symbol *) s)->section = seg;
2509 return;
2512 /* Don't reassign section symbols. The direct reason is to prevent seg
2513 faults assigning back to const global symbols such as *ABS*, but it
2514 shouldn't happen anyway. */
2515 if (s->bsym->flags & BSF_SECTION_SYM)
2517 if (s->bsym->section != seg)
2518 abort ();
2520 else
2522 if (multibyte_handling == multibyte_warn_syms
2523 && ! s->flags.local_symbol
2524 && seg != undefined_section
2525 && ! s->flags.multibyte_warned
2526 && scan_for_multibyte_characters ((const unsigned char *) s->name,
2527 (const unsigned char *) s->name + strlen (s->name),
2528 false))
2530 as_warn (_("symbol '%s' contains multibyte characters"), s->name);
2531 s->flags.multibyte_warned = 1;
2534 s->bsym->section = seg;
2538 void
2539 S_SET_EXTERNAL (symbolS *s)
2541 if (s->flags.local_symbol)
2542 s = local_symbol_convert (s);
2543 if ((s->bsym->flags & BSF_WEAK) != 0)
2545 /* Let .weak override .global. */
2546 return;
2548 if (s->bsym->flags & BSF_SECTION_SYM)
2550 /* Do not reassign section symbols. */
2551 as_warn (_("can't make section symbol global"));
2552 return;
2554 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2555 if (S_GET_SEGMENT (s) == reg_section)
2557 as_bad (_("can't make register symbol global"));
2558 return;
2560 #endif
2561 s->bsym->flags |= BSF_GLOBAL;
2562 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2564 #ifdef TE_PE
2565 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2566 an_external_name = S_GET_NAME (s);
2567 #endif
2570 void
2571 S_CLEAR_EXTERNAL (symbolS *s)
2573 if (s->flags.local_symbol)
2574 return;
2575 if ((s->bsym->flags & BSF_WEAK) != 0)
2577 /* Let .weak override. */
2578 return;
2580 s->bsym->flags |= BSF_LOCAL;
2581 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2584 void
2585 S_SET_WEAK (symbolS *s)
2587 if (s->flags.local_symbol)
2588 s = local_symbol_convert (s);
2589 #ifdef obj_set_weak_hook
2590 obj_set_weak_hook (s);
2591 #endif
2592 s->bsym->flags |= BSF_WEAK;
2593 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2596 void
2597 S_SET_WEAKREFR (symbolS *s)
2599 if (s->flags.local_symbol)
2600 s = local_symbol_convert (s);
2601 s->flags.weakrefr = 1;
2602 /* If the alias was already used, make sure we mark the target as
2603 used as well, otherwise it might be dropped from the symbol
2604 table. This may have unintended side effects if the alias is
2605 later redirected to another symbol, such as keeping the unused
2606 previous target in the symbol table. Since it will be weak, it's
2607 not a big deal. */
2608 if (s->flags.used)
2609 symbol_mark_used (s->x->value.X_add_symbol);
2612 void
2613 S_CLEAR_WEAKREFR (symbolS *s)
2615 if (s->flags.local_symbol)
2616 return;
2617 s->flags.weakrefr = 0;
2620 void
2621 S_SET_WEAKREFD (symbolS *s)
2623 if (s->flags.local_symbol)
2624 s = local_symbol_convert (s);
2625 s->flags.weakrefd = 1;
2626 S_SET_WEAK (s);
2629 void
2630 S_CLEAR_WEAKREFD (symbolS *s)
2632 if (s->flags.local_symbol)
2633 return;
2634 if (s->flags.weakrefd)
2636 s->flags.weakrefd = 0;
2637 /* If a weakref target symbol is weak, then it was never
2638 referenced directly before, not even in a .global directive,
2639 so decay it to local. If it remains undefined, it will be
2640 later turned into a global, like any other undefined
2641 symbol. */
2642 if (s->bsym->flags & BSF_WEAK)
2644 #ifdef obj_clear_weak_hook
2645 obj_clear_weak_hook (s);
2646 #endif
2647 s->bsym->flags &= ~BSF_WEAK;
2648 s->bsym->flags |= BSF_LOCAL;
2653 void
2654 S_SET_THREAD_LOCAL (symbolS *s)
2656 if (s->flags.local_symbol)
2657 s = local_symbol_convert (s);
2658 if (bfd_is_com_section (s->bsym->section)
2659 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2660 return;
2661 s->bsym->flags |= BSF_THREAD_LOCAL;
2662 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2663 as_bad (_("Accessing function `%s' as thread-local object"),
2664 S_GET_NAME (s));
2665 else if (! bfd_is_und_section (s->bsym->section)
2666 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2667 as_bad (_("Accessing `%s' as thread-local object"),
2668 S_GET_NAME (s));
2671 void
2672 S_SET_NAME (symbolS *s, const char *name)
2674 s->name = name;
2675 if (s->flags.local_symbol)
2676 return;
2677 s->bsym->name = name;
2680 void
2681 S_SET_VOLATILE (symbolS *s)
2683 if (s->flags.local_symbol)
2684 s = local_symbol_convert (s);
2685 s->flags.volatil = 1;
2688 void
2689 S_CLEAR_VOLATILE (symbolS *s)
2691 if (!s->flags.local_symbol)
2692 s->flags.volatil = 0;
2695 void
2696 S_SET_FORWARD_REF (symbolS *s)
2698 if (s->flags.local_symbol)
2699 s = local_symbol_convert (s);
2700 s->flags.forward_ref = 1;
2703 /* Return the previous symbol in a chain. */
2705 symbolS *
2706 symbol_previous (const symbolS *s)
2708 if (s->flags.local_symbol)
2709 abort ();
2710 return s->x->previous;
2713 /* Return the next symbol in a chain. */
2715 symbolS *
2716 symbol_next (const symbolS *s)
2718 if (s->flags.local_symbol)
2719 abort ();
2720 return s->x->next;
2723 /* Return a pointer to the value of a symbol as an expression. */
2725 expressionS *
2726 symbol_get_value_expression (symbolS *s)
2728 if (s->flags.local_symbol)
2729 s = local_symbol_convert (s);
2730 return &s->x->value;
2733 /* Set the value of a symbol to an expression. */
2735 void
2736 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2738 if (s->flags.local_symbol)
2739 s = local_symbol_convert (s);
2740 s->x->value = *exp;
2741 S_CLEAR_WEAKREFR (s);
2744 /* Return whether 2 symbols are the same. */
2747 symbol_same_p (const symbolS *s1, const symbolS *s2)
2749 return s1 == s2;
2752 /* Return a pointer to the X_add_number component of a symbol. */
2754 offsetT *
2755 symbol_X_add_number (const symbolS *s)
2757 if (s->flags.local_symbol)
2758 return (offsetT *) &((struct local_symbol *) s)->value;
2760 return &s->x->value.X_add_number;
2763 /* Set the value of SYM to the current position in the current segment. */
2765 void
2766 symbol_set_value_now (symbolS *sym)
2768 S_SET_SEGMENT (sym, now_seg);
2769 S_SET_VALUE (sym, frag_now_fix ());
2770 symbol_set_frag (sym, frag_now);
2773 /* Set the frag of a symbol. */
2775 void
2776 symbol_set_frag (symbolS *s, fragS *f)
2778 if (s->flags.local_symbol)
2780 ((struct local_symbol *) s)->frag = f;
2781 return;
2783 s->frag = f;
2784 S_CLEAR_WEAKREFR (s);
2787 /* Return the frag of a symbol. */
2789 fragS *
2790 symbol_get_frag (const symbolS *s)
2792 if (s->flags.local_symbol)
2793 return ((struct local_symbol *) s)->frag;
2794 return s->frag;
2797 /* Mark a symbol as having been used. */
2799 void
2800 symbol_mark_used (symbolS *s)
2802 if (s->flags.local_symbol)
2803 return;
2804 s->flags.used = 1;
2805 if (S_IS_WEAKREFR (s))
2806 symbol_mark_used (s->x->value.X_add_symbol);
2809 /* Clear the mark of whether a symbol has been used. */
2811 void
2812 symbol_clear_used (symbolS *s)
2814 if (s->flags.local_symbol)
2815 s = local_symbol_convert (s);
2816 s->flags.used = 0;
2819 /* Return whether a symbol has been used. */
2822 symbol_used_p (const symbolS *s)
2824 if (s->flags.local_symbol)
2825 return 1;
2826 return s->flags.used;
2829 /* Mark a symbol as having been used in a reloc. */
2831 void
2832 symbol_mark_used_in_reloc (symbolS *s)
2834 if (s->flags.local_symbol)
2835 s = local_symbol_convert (s);
2836 s->flags.used_in_reloc = 1;
2839 /* Clear the mark of whether a symbol has been used in a reloc. */
2841 void
2842 symbol_clear_used_in_reloc (symbolS *s)
2844 if (s->flags.local_symbol)
2845 return;
2846 s->flags.used_in_reloc = 0;
2849 /* Return whether a symbol has been used in a reloc. */
2852 symbol_used_in_reloc_p (const symbolS *s)
2854 if (s->flags.local_symbol)
2855 return 0;
2856 return s->flags.used_in_reloc;
2859 /* Mark a symbol as an MRI common symbol. */
2861 void
2862 symbol_mark_mri_common (symbolS *s)
2864 if (s->flags.local_symbol)
2865 s = local_symbol_convert (s);
2866 s->flags.mri_common = 1;
2869 /* Clear the mark of whether a symbol is an MRI common symbol. */
2871 void
2872 symbol_clear_mri_common (symbolS *s)
2874 if (s->flags.local_symbol)
2875 return;
2876 s->flags.mri_common = 0;
2879 /* Return whether a symbol is an MRI common symbol. */
2882 symbol_mri_common_p (const symbolS *s)
2884 if (s->flags.local_symbol)
2885 return 0;
2886 return s->flags.mri_common;
2889 /* Mark a symbol as having been written. */
2891 void
2892 symbol_mark_written (symbolS *s)
2894 if (s->flags.local_symbol)
2895 return;
2896 s->flags.written = 1;
2899 /* Clear the mark of whether a symbol has been written. */
2901 void
2902 symbol_clear_written (symbolS *s)
2904 if (s->flags.local_symbol)
2905 return;
2906 s->flags.written = 0;
2909 /* Return whether a symbol has been written. */
2912 symbol_written_p (const symbolS *s)
2914 if (s->flags.local_symbol)
2915 return 0;
2916 return s->flags.written;
2919 /* Mark a symbol as to be removed. */
2921 void
2922 symbol_mark_removed (symbolS *s)
2924 if (s->flags.local_symbol)
2925 return;
2926 s->flags.removed = 1;
2929 /* Return whether a symbol has been marked to be removed. */
2932 symbol_removed_p (const symbolS *s)
2934 if (s->flags.local_symbol)
2935 return 0;
2936 return s->flags.removed;
2939 /* Mark a symbol as having been resolved. */
2941 void
2942 symbol_mark_resolved (symbolS *s)
2944 s->flags.resolved = 1;
2947 /* Return whether a symbol has been resolved. */
2950 symbol_resolved_p (const symbolS *s)
2952 return s->flags.resolved;
2955 /* Mark a symbol as being resolved. */
2957 void
2958 symbol_mark_resolving (symbolS *s)
2960 s->flags.resolving = 1;
2963 void
2964 symbol_clear_resolving (symbolS *s)
2966 s->flags.resolving = 0;
2969 /* Return whether a symbol is being resolved. */
2972 symbol_resolving_p (const symbolS *s)
2974 return s->flags.resolving;
2977 /* Return whether a symbol is a section symbol. */
2980 symbol_section_p (const symbolS *s)
2982 if (s->flags.local_symbol)
2983 return 0;
2984 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2987 /* Return whether a symbol is equated to another symbol. */
2990 symbol_equated_p (const symbolS *s)
2992 if (s->flags.local_symbol)
2993 return 0;
2994 return s->x->value.X_op == O_symbol;
2997 /* Return whether a symbol is equated to another symbol, and should be
2998 treated specially when writing out relocs. */
3001 symbol_equated_reloc_p (const symbolS *s)
3003 if (s->flags.local_symbol)
3004 return 0;
3005 /* X_op_symbol, normally not used for O_symbol, is set by
3006 resolve_symbol_value to flag expression syms that have been
3007 equated. */
3008 return (s->x->value.X_op == O_symbol
3009 #if defined (OBJ_COFF) && defined (TE_PE)
3010 && ! S_IS_WEAK (s)
3011 #endif
3012 && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
3013 || ! S_IS_DEFINED (s)
3014 || S_IS_COMMON (s)));
3017 /* Return whether a symbol has a constant value. */
3020 symbol_constant_p (const symbolS *s)
3022 if (s->flags.local_symbol)
3023 return 1;
3024 return s->x->value.X_op == O_constant;
3027 /* Return whether a symbol was cloned and thus removed from the global
3028 symbol list. */
3031 symbol_shadow_p (const symbolS *s)
3033 if (s->flags.local_symbol)
3034 return 0;
3035 return s->x->next == s;
3038 /* If S is a struct symbol return S, otherwise return NULL. */
3040 symbolS *
3041 symbol_symbolS (symbolS *s)
3043 if (s->flags.local_symbol)
3044 return NULL;
3045 return s;
3048 /* Return the BFD symbol for a symbol. */
3050 asymbol *
3051 symbol_get_bfdsym (symbolS *s)
3053 if (s->flags.local_symbol)
3054 s = local_symbol_convert (s);
3055 return s->bsym;
3058 /* Set the BFD symbol for a symbol. */
3060 void
3061 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3063 if (s->flags.local_symbol)
3064 s = local_symbol_convert (s);
3065 /* Usually, it is harmless to reset a symbol to a BFD section
3066 symbol. For example, obj_elf_change_section sets the BFD symbol
3067 of an old symbol with the newly created section symbol. But when
3068 we have multiple sections with the same name, the newly created
3069 section may have the same name as an old section. We check if the
3070 old symbol has been already marked as a section symbol before
3071 resetting it. */
3072 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3073 s->bsym = bsym;
3074 /* else XXX - What do we do now ? */
3077 #ifdef OBJ_SYMFIELD_TYPE
3079 /* Get a pointer to the object format information for a symbol. */
3081 OBJ_SYMFIELD_TYPE *
3082 symbol_get_obj (symbolS *s)
3084 if (s->flags.local_symbol)
3085 s = local_symbol_convert (s);
3086 return &s->x->obj;
3089 /* Set the object format information for a symbol. */
3091 void
3092 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
3094 if (s->flags.local_symbol)
3095 s = local_symbol_convert (s);
3096 s->x->obj = *o;
3099 #endif /* OBJ_SYMFIELD_TYPE */
3101 #ifdef TC_SYMFIELD_TYPE
3103 /* Get a pointer to the processor information for a symbol. */
3105 TC_SYMFIELD_TYPE *
3106 symbol_get_tc (symbolS *s)
3108 if (s->flags.local_symbol)
3109 s = local_symbol_convert (s);
3110 return &s->x->tc;
3113 /* Set the processor information for a symbol. */
3115 void
3116 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
3118 if (s->flags.local_symbol)
3119 s = local_symbol_convert (s);
3120 s->x->tc = *o;
3123 #endif /* TC_SYMFIELD_TYPE */
3125 void
3126 symbol_begin (void)
3128 symbol_lastP = NULL;
3129 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
3130 sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3131 NULL, xcalloc, free);
3133 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3134 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3135 #endif
3136 abs_symbol.x = &abs_symbol_x;
3137 abs_symbol.x->value.X_op = O_constant;
3138 abs_symbol.frag = &zero_address_frag;
3140 if (LOCAL_LABELS_FB)
3141 fb_label_init ();
3144 void
3145 symbol_end (void)
3147 htab_delete (sy_hash);
3150 void
3151 dot_symbol_init (void)
3153 dot_symbol.name = ".";
3154 dot_symbol.flags.forward_ref = 1;
3155 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3156 if (dot_symbol.bsym == NULL)
3157 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3158 dot_symbol.bsym->name = ".";
3159 dot_symbol.x = &dot_symbol_x;
3160 dot_symbol.x->value.X_op = O_constant;
3163 int indent_level;
3165 /* Maximum indent level.
3166 Available for modification inside a gdb session. */
3167 static int max_indent_level = 8;
3169 void
3170 print_symbol_value_1 (FILE *file, symbolS *sym)
3172 const char *name = S_GET_NAME (sym);
3173 if (!name || !name[0])
3174 name = "(unnamed)";
3175 fprintf (file, "sym %p %s", sym, name);
3177 if (sym->flags.local_symbol)
3179 struct local_symbol *locsym = (struct local_symbol *) sym;
3181 if (locsym->frag != &zero_address_frag
3182 && locsym->frag != NULL)
3183 fprintf (file, " frag %p", locsym->frag);
3184 if (locsym->flags.resolved)
3185 fprintf (file, " resolved");
3186 fprintf (file, " local");
3188 else
3190 if (sym->frag != &zero_address_frag)
3191 fprintf (file, " frag %p", sym->frag);
3192 if (sym->flags.written)
3193 fprintf (file, " written");
3194 if (sym->flags.resolved)
3195 fprintf (file, " resolved");
3196 else if (sym->flags.resolving)
3197 fprintf (file, " resolving");
3198 if (sym->flags.used_in_reloc)
3199 fprintf (file, " used-in-reloc");
3200 if (sym->flags.used)
3201 fprintf (file, " used");
3202 if (S_IS_LOCAL (sym))
3203 fprintf (file, " local");
3204 if (S_IS_EXTERNAL (sym))
3205 fprintf (file, " extern");
3206 if (S_IS_WEAK (sym))
3207 fprintf (file, " weak");
3208 if (S_IS_DEBUG (sym))
3209 fprintf (file, " debug");
3210 if (S_IS_DEFINED (sym))
3211 fprintf (file, " defined");
3213 if (S_IS_WEAKREFR (sym))
3214 fprintf (file, " weakrefr");
3215 if (S_IS_WEAKREFD (sym))
3216 fprintf (file, " weakrefd");
3217 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3218 if (symbol_resolved_p (sym))
3220 segT s = S_GET_SEGMENT (sym);
3222 if (s != undefined_section
3223 && s != expr_section)
3224 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3226 else if (indent_level < max_indent_level
3227 && S_GET_SEGMENT (sym) != undefined_section)
3229 indent_level++;
3230 fprintf (file, "\n%*s<", indent_level * 4, "");
3231 if (sym->flags.local_symbol)
3232 fprintf (file, "constant %lx",
3233 (unsigned long) ((struct local_symbol *) sym)->value);
3234 else
3235 print_expr_1 (file, &sym->x->value);
3236 fprintf (file, ">");
3237 indent_level--;
3239 fflush (file);
3242 void
3243 print_symbol_value (symbolS *sym)
3245 indent_level = 0;
3246 print_symbol_value_1 (stderr, sym);
3247 fprintf (stderr, "\n");
3250 static void
3251 print_binary (FILE *file, const char *name, expressionS *exp)
3253 indent_level++;
3254 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3255 print_symbol_value_1 (file, exp->X_add_symbol);
3256 fprintf (file, ">\n%*s<", indent_level * 4, "");
3257 print_symbol_value_1 (file, exp->X_op_symbol);
3258 fprintf (file, ">");
3259 indent_level--;
3262 void
3263 print_expr_1 (FILE *file, expressionS *exp)
3265 fprintf (file, "expr %p ", exp);
3266 switch (exp->X_op)
3268 case O_illegal:
3269 fprintf (file, "illegal");
3270 break;
3271 case O_absent:
3272 fprintf (file, "absent");
3273 break;
3274 case O_constant:
3275 fprintf (file, "constant %" PRIx64, (uint64_t) exp->X_add_number);
3276 break;
3277 case O_symbol:
3278 indent_level++;
3279 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3280 print_symbol_value_1 (file, exp->X_add_symbol);
3281 fprintf (file, ">");
3282 maybe_print_addnum:
3283 if (exp->X_add_number)
3284 fprintf (file, "\n%*s%" PRIx64, indent_level * 4, "",
3285 (uint64_t) exp->X_add_number);
3286 indent_level--;
3287 break;
3288 case O_register:
3289 fprintf (file, "register #%d", (int) exp->X_add_number);
3290 break;
3291 case O_big:
3292 fprintf (file, "big");
3293 break;
3294 case O_uminus:
3295 fprintf (file, "uminus -<");
3296 indent_level++;
3297 print_symbol_value_1 (file, exp->X_add_symbol);
3298 fprintf (file, ">");
3299 goto maybe_print_addnum;
3300 case O_bit_not:
3301 fprintf (file, "bit_not");
3302 break;
3303 case O_multiply:
3304 print_binary (file, "multiply", exp);
3305 break;
3306 case O_divide:
3307 print_binary (file, "divide", exp);
3308 break;
3309 case O_modulus:
3310 print_binary (file, "modulus", exp);
3311 break;
3312 case O_left_shift:
3313 print_binary (file, "lshift", exp);
3314 break;
3315 case O_right_shift:
3316 print_binary (file, "rshift", exp);
3317 break;
3318 case O_bit_inclusive_or:
3319 print_binary (file, "bit_ior", exp);
3320 break;
3321 case O_bit_exclusive_or:
3322 print_binary (file, "bit_xor", exp);
3323 break;
3324 case O_bit_and:
3325 print_binary (file, "bit_and", exp);
3326 break;
3327 case O_eq:
3328 print_binary (file, "eq", exp);
3329 break;
3330 case O_ne:
3331 print_binary (file, "ne", exp);
3332 break;
3333 case O_lt:
3334 print_binary (file, "lt", exp);
3335 break;
3336 case O_le:
3337 print_binary (file, "le", exp);
3338 break;
3339 case O_ge:
3340 print_binary (file, "ge", exp);
3341 break;
3342 case O_gt:
3343 print_binary (file, "gt", exp);
3344 break;
3345 case O_logical_and:
3346 print_binary (file, "logical_and", exp);
3347 break;
3348 case O_logical_or:
3349 print_binary (file, "logical_or", exp);
3350 break;
3351 case O_add:
3352 indent_level++;
3353 fprintf (file, "add\n%*s<", indent_level * 4, "");
3354 print_symbol_value_1 (file, exp->X_add_symbol);
3355 fprintf (file, ">\n%*s<", indent_level * 4, "");
3356 print_symbol_value_1 (file, exp->X_op_symbol);
3357 fprintf (file, ">");
3358 goto maybe_print_addnum;
3359 case O_subtract:
3360 indent_level++;
3361 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3362 print_symbol_value_1 (file, exp->X_add_symbol);
3363 fprintf (file, ">\n%*s<", indent_level * 4, "");
3364 print_symbol_value_1 (file, exp->X_op_symbol);
3365 fprintf (file, ">");
3366 goto maybe_print_addnum;
3367 default:
3368 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3369 break;
3371 fflush (stdout);
3374 void
3375 print_expr (expressionS *exp)
3377 print_expr_1 (stderr, exp);
3378 fprintf (stderr, "\n");
3381 void
3382 symbol_print_statistics (FILE *file)
3384 htab_print_statistics (file, "symbol table", sy_hash);
3385 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3386 local_symbol_count, local_symbol_conversion_count);
3389 #ifdef OBJ_COMPLEX_RELC
3391 /* Convert given symbol to a new complex-relocation symbol name. This
3392 may be a recursive function, since it might be called for non-leaf
3393 nodes (plain symbols) in the expression tree. The caller owns the
3394 returning string, so should free it eventually. Errors are
3395 indicated via as_bad and a NULL return value. The given symbol
3396 is marked with used_in_reloc. */
3398 char *
3399 symbol_relc_make_sym (symbolS * sym)
3401 char * terminal = NULL;
3402 const char * sname;
3403 char typetag;
3404 int sname_len;
3406 gas_assert (sym != NULL);
3408 /* Recurse to symbol_relc_make_expr if this symbol
3409 is defined as an expression or a plain value. */
3410 if ( S_GET_SEGMENT (sym) == expr_section
3411 || S_GET_SEGMENT (sym) == absolute_section)
3412 return symbol_relc_make_expr (symbol_get_value_expression (sym));
3414 /* This may be a "fake symbol", referring to ".".
3415 Write out a special null symbol to refer to this position. */
3416 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3417 return xstrdup (".");
3419 /* We hope this is a plain leaf symbol. Construct the encoding
3420 as {S,s}II...:CCCCCCC....
3421 where 'S'/'s' means section symbol / plain symbol
3422 III is decimal for the symbol name length
3423 CCC is the symbol name itself. */
3424 symbol_mark_used_in_reloc (sym);
3426 sname = S_GET_NAME (sym);
3427 sname_len = strlen (sname);
3428 typetag = symbol_section_p (sym) ? 'S' : 's';
3430 terminal = XNEWVEC (char, (1 /* S or s */
3431 + 8 /* sname_len in decimal */
3432 + 1 /* _ spacer */
3433 + sname_len /* name itself */
3434 + 1 /* \0 */ ));
3436 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3437 return terminal;
3440 /* Convert given value to a new complex-relocation symbol name. This
3441 is a non-recursive function, since it is be called for leaf nodes
3442 (plain values) in the expression tree. The caller owns the
3443 returning string, so should free() it eventually. No errors. */
3445 char *
3446 symbol_relc_make_value (offsetT val)
3448 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3450 terminal[0] = '#';
3451 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3452 return terminal;
3455 /* Convert given expression to a new complex-relocation symbol name.
3456 This is a recursive function, since it traverses the entire given
3457 expression tree. The caller owns the returning string, so should
3458 free() it eventually. Errors are indicated via as_bad() and a NULL
3459 return value. */
3461 char *
3462 symbol_relc_make_expr (expressionS * exp)
3464 const char * opstr = NULL; /* Operator prefix string. */
3465 int arity = 0; /* Arity of this operator. */
3466 char * operands[3]; /* Up to three operands. */
3467 char * concat_string = NULL;
3469 operands[0] = operands[1] = operands[2] = NULL;
3471 gas_assert (exp != NULL);
3473 /* Match known operators -> fill in opstr, arity, operands[] and fall
3474 through to construct subexpression fragments; may instead return
3475 string directly for leaf nodes. */
3477 /* See expr.h for the meaning of all these enums. Many operators
3478 have an unnatural arity (X_add_number implicitly added). The
3479 conversion logic expands them to explicit "+" subexpressions. */
3481 switch (exp->X_op)
3483 default:
3484 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3485 break;
3487 /* Leaf nodes. */
3488 case O_constant:
3489 return symbol_relc_make_value (exp->X_add_number);
3491 case O_symbol:
3492 if (exp->X_add_number)
3494 arity = 2;
3495 opstr = "+";
3496 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3497 operands[1] = symbol_relc_make_value (exp->X_add_number);
3498 break;
3500 else
3501 return symbol_relc_make_sym (exp->X_add_symbol);
3503 /* Helper macros for nesting nodes. */
3505 #define HANDLE_XADD_OPT1(str_) \
3506 if (exp->X_add_number) \
3508 arity = 2; \
3509 opstr = "+:" str_; \
3510 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3511 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3512 break; \
3514 else \
3516 arity = 1; \
3517 opstr = str_; \
3518 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3520 break
3522 #define HANDLE_XADD_OPT2(str_) \
3523 if (exp->X_add_number) \
3525 arity = 3; \
3526 opstr = "+:" str_; \
3527 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3528 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3529 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3531 else \
3533 arity = 2; \
3534 opstr = str_; \
3535 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3536 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3538 break
3540 /* Nesting nodes. */
3542 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3543 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3544 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3545 case O_multiply: HANDLE_XADD_OPT2 ("*");
3546 case O_divide: HANDLE_XADD_OPT2 ("/");
3547 case O_modulus: HANDLE_XADD_OPT2 ("%");
3548 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3549 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3550 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3551 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3552 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3553 case O_add: HANDLE_XADD_OPT2 ("+");
3554 case O_subtract: HANDLE_XADD_OPT2 ("-");
3555 case O_eq: HANDLE_XADD_OPT2 ("==");
3556 case O_ne: HANDLE_XADD_OPT2 ("!=");
3557 case O_lt: HANDLE_XADD_OPT2 ("<");
3558 case O_le: HANDLE_XADD_OPT2 ("<=");
3559 case O_ge: HANDLE_XADD_OPT2 (">=");
3560 case O_gt: HANDLE_XADD_OPT2 (">");
3561 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3562 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3565 /* Validate & reject early. */
3566 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3567 opstr = NULL;
3568 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3569 opstr = NULL;
3570 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3571 opstr = NULL;
3573 if (opstr == NULL)
3574 concat_string = NULL;
3575 else if (arity == 0)
3576 concat_string = xstrdup (opstr);
3577 else if (arity == 1)
3578 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3579 else if (arity == 2)
3580 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3581 (char *) NULL);
3582 else
3583 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3584 operands[2], (char *) NULL);
3586 /* Free operand strings (not opstr). */
3587 if (arity >= 1) xfree (operands[0]);
3588 if (arity >= 2) xfree (operands[1]);
3589 if (arity >= 3) xfree (operands[2]);
3591 return concat_string;
3594 #endif