[PATCH 16/57][Arm][GAS] Add support for MVE instructions: vdup, vddup, vdwdup, vidup...
[binutils-gdb.git] / gas / symbols.c
blob918028f87505ee84ad6d27e87bd9f1797297ebe8
1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2019 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"
29 struct symbol_flags
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
65 routines. */
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
74 symbol is present. */
75 unsigned int sy_weakrefd : 1;
77 /* This if set if the unit of the symbol value is "octets" instead
78 of "bytes". */
79 unsigned int sy_octets : 1;
82 /* The information we keep for a symbol. Note that the symbol table
83 holds pointers both to this and to local_symbol structures. See
84 below. */
86 struct symbol
88 /* Symbol flags. */
89 struct symbol_flags sy_flags;
91 /* BFD symbol */
92 asymbol *bsym;
94 /* The value of the symbol. */
95 expressionS sy_value;
97 /* Forwards and (optionally) backwards chain pointers. */
98 struct symbol *sy_next;
99 struct symbol *sy_previous;
101 /* Pointer to the frag this symbol is attached to, if any.
102 Otherwise, NULL. */
103 struct frag *sy_frag;
105 #ifdef OBJ_SYMFIELD_TYPE
106 OBJ_SYMFIELD_TYPE sy_obj;
107 #endif
109 #ifdef TC_SYMFIELD_TYPE
110 TC_SYMFIELD_TYPE sy_tc;
111 #endif
113 #ifdef TARGET_SYMBOL_FIELDS
114 TARGET_SYMBOL_FIELDS
115 #endif
118 /* A pointer in the symbol may point to either a complete symbol
119 (struct symbol above) or to a local symbol (struct local_symbol
120 defined here). The symbol code can detect the case by examining
121 the first field. It is always NULL for a local symbol.
123 We do this because we ordinarily only need a small amount of
124 information for a local symbol. The symbol table takes up a lot of
125 space, and storing less information for a local symbol can make a
126 big difference in assembler memory usage when assembling a large
127 file. */
129 struct local_symbol
131 /* Symbol flags. Only sy_local_symbol and sy_resolved are relevant. */
132 struct symbol_flags lsy_flags;
134 /* The symbol section. This also serves as a flag. If this is
135 reg_section, then this symbol has been converted into a regular
136 symbol, and lsy_sym points to it. */
137 segT lsy_section;
139 /* The symbol name. */
140 const char *lsy_name;
142 /* The symbol frag or the real symbol, depending upon the value in
143 lsy_section. */
144 union
146 fragS *lsy_frag;
147 symbolS *lsy_sym;
148 } u;
150 /* The value of the symbol. */
151 valueT lsy_value;
153 #ifdef TC_LOCAL_SYMFIELD_TYPE
154 TC_LOCAL_SYMFIELD_TYPE lsy_tc;
155 #endif
158 #define local_symbol_converted_p(l) ((l)->lsy_section == reg_section)
159 #define local_symbol_mark_converted(l) ((l)->lsy_section = reg_section)
160 #define local_symbol_resolved_p(l) ((l)->lsy_flags.sy_resolved)
161 #define local_symbol_mark_resolved(l) ((l)->lsy_flags.sy_resolved = 1)
162 #define local_symbol_get_frag(l) ((l)->u.lsy_frag)
163 #define local_symbol_set_frag(l, f) ((l)->u.lsy_frag = (f))
164 #define local_symbol_get_real_symbol(l) ((l)->u.lsy_sym)
165 #define local_symbol_set_real_symbol(l, s) ((l)->u.lsy_sym = (s))
167 /* This is non-zero if symbols are case sensitive, which is the
168 default. */
169 int symbols_case_sensitive = 1;
171 #ifndef WORKING_DOT_WORD
172 extern int new_broken_words;
173 #endif
175 /* symbol-name => struct symbol pointer */
176 static struct hash_control *sy_hash;
178 /* Table of local symbols. */
179 static struct hash_control *local_hash;
181 /* Below are commented in "symbols.h". */
182 symbolS *symbol_rootP;
183 symbolS *symbol_lastP;
184 symbolS abs_symbol;
185 symbolS dot_symbol;
187 #ifdef DEBUG_SYMS
188 #define debug_verify_symchain verify_symbol_chain
189 #else
190 #define debug_verify_symchain(root, last) ((void) 0)
191 #endif
193 #define DOLLAR_LABEL_CHAR '\001'
194 #define LOCAL_LABEL_CHAR '\002'
196 #ifndef TC_LABEL_IS_LOCAL
197 #define TC_LABEL_IS_LOCAL(name) 0
198 #endif
200 struct obstack notes;
201 #ifdef TE_PE
202 /* The name of an external symbol which is
203 used to make weak PE symbol names unique. */
204 const char * an_external_name;
205 #endif
207 static const char *save_symbol_name (const char *);
208 static void fb_label_init (void);
209 static long dollar_label_instance (long);
210 static long fb_label_instance (long);
212 static void print_binary (FILE *, const char *, expressionS *);
214 /* Return a pointer to a new symbol. Die if we can't make a new
215 symbol. Fill in the symbol's values. Add symbol to end of symbol
216 chain.
218 This function should be called in the general case of creating a
219 symbol. However, if the output file symbol table has already been
220 set, and you are certain that this symbol won't be wanted in the
221 output file, you can call symbol_create. */
223 symbolS *
224 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
226 symbolS *symbolP = symbol_create (name, segment, valu, frag);
228 /* Link to end of symbol chain. */
230 extern int symbol_table_frozen;
231 if (symbol_table_frozen)
232 abort ();
234 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
236 return symbolP;
239 /* Save a symbol name on a permanent obstack, and convert it according
240 to the object file format. */
242 static const char *
243 save_symbol_name (const char *name)
245 size_t name_length;
246 char *ret;
248 gas_assert (name != NULL);
249 name_length = strlen (name) + 1; /* +1 for \0. */
250 obstack_grow (&notes, name, name_length);
251 ret = (char *) obstack_finish (&notes);
253 #ifdef tc_canonicalize_symbol_name
254 ret = tc_canonicalize_symbol_name (ret);
255 #endif
257 if (! symbols_case_sensitive)
259 char *s;
261 for (s = ret; *s != '\0'; s++)
262 *s = TOUPPER (*s);
265 return ret;
268 symbolS *
269 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
270 segT segment, /* Segment identifier (SEG_<something>). */
271 valueT valu, /* Symbol value. */
272 fragS *frag /* Associated fragment. */)
274 const char *preserved_copy_of_name;
275 symbolS *symbolP;
277 preserved_copy_of_name = save_symbol_name (name);
279 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
281 /* symbol must be born in some fixed state. This seems as good as any. */
282 memset (symbolP, 0, sizeof (symbolS));
284 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
285 if (symbolP->bsym == NULL)
286 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
287 S_SET_NAME (symbolP, preserved_copy_of_name);
289 S_SET_SEGMENT (symbolP, segment);
290 S_SET_VALUE (symbolP, valu);
291 symbol_clear_list_pointers (symbolP);
293 symbolP->sy_frag = frag;
295 obj_symbol_new_hook (symbolP);
297 #ifdef tc_symbol_new_hook
298 tc_symbol_new_hook (symbolP);
299 #endif
301 return symbolP;
305 /* Local symbol support. If we can get away with it, we keep only a
306 small amount of information for local symbols. */
308 static symbolS *local_symbol_convert (struct local_symbol *);
310 /* Used for statistics. */
312 static unsigned long local_symbol_count;
313 static unsigned long local_symbol_conversion_count;
315 /* This macro is called with a symbol argument passed by reference.
316 It returns whether this is a local symbol. If necessary, it
317 changes its argument to the real symbol. */
319 #define LOCAL_SYMBOL_CHECK(s) \
320 (s->sy_flags.sy_local_symbol \
321 ? (local_symbol_converted_p ((struct local_symbol *) s) \
322 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
323 0) \
324 : 1) \
325 : 0)
327 /* Create a local symbol and insert it into the local hash table. */
329 struct local_symbol *
330 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
332 const char *name_copy;
333 struct local_symbol *ret;
335 ++local_symbol_count;
337 name_copy = save_symbol_name (name);
339 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
340 ret->lsy_flags.sy_local_symbol = 1;
341 ret->lsy_flags.sy_resolved = 0;
342 ret->lsy_name = name_copy;
343 ret->lsy_section = section;
344 local_symbol_set_frag (ret, frag);
345 ret->lsy_value = val;
347 hash_jam (local_hash, name_copy, (void *) ret);
349 return ret;
352 /* Convert a local symbol into a real symbol. Note that we do not
353 reclaim the space used by the local symbol. */
355 static symbolS *
356 local_symbol_convert (struct local_symbol *locsym)
358 symbolS *ret;
360 gas_assert (locsym->lsy_flags.sy_local_symbol);
361 if (local_symbol_converted_p (locsym))
362 return local_symbol_get_real_symbol (locsym);
364 ++local_symbol_conversion_count;
366 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
367 local_symbol_get_frag (locsym));
369 if (local_symbol_resolved_p (locsym))
370 ret->sy_flags.sy_resolved = 1;
372 /* Local symbols are always either defined or used. */
373 ret->sy_flags.sy_used = 1;
375 #ifdef TC_LOCAL_SYMFIELD_CONVERT
376 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
377 #endif
379 symbol_table_insert (ret);
381 local_symbol_mark_converted (locsym);
382 local_symbol_set_real_symbol (locsym, ret);
384 hash_jam (local_hash, locsym->lsy_name, NULL);
386 return ret;
389 static void
390 define_sym_at_dot (symbolS *symbolP)
392 symbolP->sy_frag = frag_now;
393 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
394 S_SET_SEGMENT (symbolP, now_seg);
397 /* We have just seen "<name>:".
398 Creates a struct symbol unless it already exists.
400 Gripes if we are redefining a symbol incompatibly (and ignores it). */
402 symbolS *
403 colon (/* Just seen "x:" - rattle symbols & frags. */
404 const char *sym_name /* Symbol name, as a canonical string. */
405 /* We copy this string: OK to alter later. */)
407 symbolS *symbolP; /* Symbol we are working with. */
409 /* Sun local labels go out of scope whenever a non-local symbol is
410 defined. */
411 if (LOCAL_LABELS_DOLLAR
412 && !bfd_is_local_label_name (stdoutput, sym_name))
413 dollar_label_clear ();
415 #ifndef WORKING_DOT_WORD
416 if (new_broken_words)
418 struct broken_word *a;
419 int possible_bytes;
420 fragS *frag_tmp;
421 char *frag_opcode;
423 if (now_seg == absolute_section)
425 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
426 return NULL;
429 possible_bytes = (md_short_jump_size
430 + new_broken_words * md_long_jump_size);
432 frag_tmp = frag_now;
433 frag_opcode = frag_var (rs_broken_word,
434 possible_bytes,
435 possible_bytes,
436 (relax_substateT) 0,
437 (symbolS *) broken_words,
438 (offsetT) 0,
439 NULL);
441 /* We want to store the pointer to where to insert the jump
442 table in the fr_opcode of the rs_broken_word frag. This
443 requires a little hackery. */
444 while (frag_tmp
445 && (frag_tmp->fr_type != rs_broken_word
446 || frag_tmp->fr_opcode))
447 frag_tmp = frag_tmp->fr_next;
448 know (frag_tmp);
449 frag_tmp->fr_opcode = frag_opcode;
450 new_broken_words = 0;
452 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
453 a->dispfrag = frag_tmp;
455 #endif /* WORKING_DOT_WORD */
457 #ifdef obj_frob_colon
458 obj_frob_colon (sym_name);
459 #endif
461 if ((symbolP = symbol_find (sym_name)) != 0)
463 S_CLEAR_WEAKREFR (symbolP);
464 #ifdef RESOLVE_SYMBOL_REDEFINITION
465 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
466 return symbolP;
467 #endif
468 /* Now check for undefined symbols. */
469 if (LOCAL_SYMBOL_CHECK (symbolP))
471 struct local_symbol *locsym = (struct local_symbol *) symbolP;
473 if (locsym->lsy_section != undefined_section
474 && (local_symbol_get_frag (locsym) != frag_now
475 || locsym->lsy_section != now_seg
476 || locsym->lsy_value != frag_now_fix ()))
478 as_bad (_("symbol `%s' is already defined"), sym_name);
479 return symbolP;
482 locsym->lsy_section = now_seg;
483 local_symbol_set_frag (locsym, frag_now);
484 locsym->lsy_value = frag_now_fix ();
486 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
487 || S_IS_COMMON (symbolP)
488 || S_IS_VOLATILE (symbolP))
490 if (S_IS_VOLATILE (symbolP))
492 symbolP = symbol_clone (symbolP, 1);
493 S_SET_VALUE (symbolP, 0);
494 S_CLEAR_VOLATILE (symbolP);
496 if (S_GET_VALUE (symbolP) == 0)
498 define_sym_at_dot (symbolP);
499 #ifdef N_UNDF
500 know (N_UNDF == 0);
501 #endif /* if we have one, it better be zero. */
504 else
506 /* There are still several cases to check:
508 A .comm/.lcomm symbol being redefined as initialized
509 data is OK
511 A .comm/.lcomm symbol being redefined with a larger
512 size is also OK
514 This only used to be allowed on VMS gas, but Sun cc
515 on the sparc also depends on it. */
517 if (((!S_IS_DEBUG (symbolP)
518 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
519 && S_IS_EXTERNAL (symbolP))
520 || S_GET_SEGMENT (symbolP) == bss_section)
521 && (now_seg == data_section
522 || now_seg == bss_section
523 || now_seg == S_GET_SEGMENT (symbolP)))
525 /* Select which of the 2 cases this is. */
526 if (now_seg != data_section)
528 /* New .comm for prev .comm symbol.
530 If the new size is larger we just change its
531 value. If the new size is smaller, we ignore
532 this symbol. */
533 if (S_GET_VALUE (symbolP)
534 < ((unsigned) frag_now_fix ()))
536 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
539 else
541 /* It is a .comm/.lcomm being converted to initialized
542 data. */
543 define_sym_at_dot (symbolP);
546 else
548 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
549 static const char *od_buf = "";
550 #else
551 char od_buf[100];
552 od_buf[0] = '\0';
553 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
554 sprintf (od_buf, "%d.%d.",
555 S_GET_OTHER (symbolP),
556 S_GET_DESC (symbolP));
557 #endif
558 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
559 sym_name,
560 segment_name (S_GET_SEGMENT (symbolP)),
561 od_buf,
562 (long) S_GET_VALUE (symbolP));
564 } /* if the undefined symbol has no value */
566 else
568 /* Don't blow up if the definition is the same. */
569 if (!(frag_now == symbolP->sy_frag
570 && S_GET_VALUE (symbolP) == frag_now_fix ()
571 && S_GET_SEGMENT (symbolP) == now_seg))
573 as_bad (_("symbol `%s' is already defined"), sym_name);
574 symbolP = symbol_clone (symbolP, 0);
575 define_sym_at_dot (symbolP);
580 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
582 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
583 (valueT) frag_now_fix (),
584 frag_now);
586 else
588 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
589 frag_now);
591 symbol_table_insert (symbolP);
594 if (mri_common_symbol != NULL)
596 /* This symbol is actually being defined within an MRI common
597 section. This requires special handling. */
598 if (LOCAL_SYMBOL_CHECK (symbolP))
599 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
600 symbolP->sy_value.X_op = O_symbol;
601 symbolP->sy_value.X_add_symbol = mri_common_symbol;
602 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
603 symbolP->sy_frag = &zero_address_frag;
604 S_SET_SEGMENT (symbolP, expr_section);
605 symbolP->sy_flags.sy_mri_common = 1;
608 #ifdef tc_frob_label
609 tc_frob_label (symbolP);
610 #endif
611 #ifdef obj_frob_label
612 obj_frob_label (symbolP);
613 #endif
615 return symbolP;
618 /* Die if we can't insert the symbol. */
620 void
621 symbol_table_insert (symbolS *symbolP)
623 const char *error_string;
625 know (symbolP);
626 know (S_GET_NAME (symbolP));
628 if (LOCAL_SYMBOL_CHECK (symbolP))
630 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
631 (void *) symbolP);
632 if (error_string != NULL)
633 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
634 S_GET_NAME (symbolP), error_string);
635 return;
638 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
640 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
641 S_GET_NAME (symbolP), error_string);
642 } /* on error */
645 /* If a symbol name does not exist, create it as undefined, and insert
646 it into the symbol table. Return a pointer to it. */
648 symbolS *
649 symbol_find_or_make (const char *name)
651 symbolS *symbolP;
653 symbolP = symbol_find (name);
655 if (symbolP == NULL)
657 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
659 symbolP = md_undefined_symbol ((char *) name);
660 if (symbolP != NULL)
661 return symbolP;
663 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
664 (valueT) 0,
665 &zero_address_frag);
666 return symbolP;
669 symbolP = symbol_make (name);
671 symbol_table_insert (symbolP);
672 } /* if symbol wasn't found */
674 return (symbolP);
677 symbolS *
678 symbol_make (const char *name)
680 symbolS *symbolP;
682 /* Let the machine description default it, e.g. for register names. */
683 symbolP = md_undefined_symbol ((char *) name);
685 if (!symbolP)
686 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
688 return (symbolP);
691 symbolS *
692 symbol_clone (symbolS *orgsymP, int replace)
694 symbolS *newsymP;
695 asymbol *bsymorg, *bsymnew;
697 /* Make sure we never clone the dot special symbol. */
698 gas_assert (orgsymP != &dot_symbol);
700 /* Running local_symbol_convert on a clone that's not the one currently
701 in local_hash would incorrectly replace the hash entry. Thus the
702 symbol must be converted here. Note that the rest of the function
703 depends on not encountering an unconverted symbol. */
704 if (LOCAL_SYMBOL_CHECK (orgsymP))
705 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
706 bsymorg = orgsymP->bsym;
708 newsymP = (symbolS *) obstack_alloc (&notes, sizeof (*newsymP));
709 *newsymP = *orgsymP;
710 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
711 if (bsymnew == NULL)
712 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
713 newsymP->bsym = bsymnew;
714 bsymnew->name = bsymorg->name;
715 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
716 bsymnew->section = bsymorg->section;
717 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
718 bfd_asymbol_bfd (bsymnew), bsymnew);
720 #ifdef obj_symbol_clone_hook
721 obj_symbol_clone_hook (newsymP, orgsymP);
722 #endif
724 #ifdef tc_symbol_clone_hook
725 tc_symbol_clone_hook (newsymP, orgsymP);
726 #endif
728 if (replace)
730 if (symbol_rootP == orgsymP)
731 symbol_rootP = newsymP;
732 else if (orgsymP->sy_previous)
734 orgsymP->sy_previous->sy_next = newsymP;
735 orgsymP->sy_previous = NULL;
737 if (symbol_lastP == orgsymP)
738 symbol_lastP = newsymP;
739 else if (orgsymP->sy_next)
740 orgsymP->sy_next->sy_previous = newsymP;
742 /* Symbols that won't be output can't be external. */
743 S_CLEAR_EXTERNAL (orgsymP);
744 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
745 debug_verify_symchain (symbol_rootP, symbol_lastP);
747 symbol_table_insert (newsymP);
749 else
751 /* Symbols that won't be output can't be external. */
752 S_CLEAR_EXTERNAL (newsymP);
753 newsymP->sy_previous = newsymP->sy_next = newsymP;
756 return newsymP;
759 /* If S is a local symbol that has been converted, return the
760 converted symbol. Otherwise return S. */
762 static inline symbolS *
763 get_real_sym (symbolS *s)
765 if (s != NULL
766 && s->sy_flags.sy_local_symbol
767 && local_symbol_converted_p ((struct local_symbol *) s))
768 s = local_symbol_get_real_symbol ((struct local_symbol *) s);
769 return s;
772 /* Referenced symbols, if they are forward references, need to be cloned
773 (without replacing the original) so that the value of the referenced
774 symbols at the point of use is saved by the clone. */
776 #undef symbol_clone_if_forward_ref
777 symbolS *
778 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
780 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
782 symbolS *orig_add_symbol = get_real_sym (symbolP->sy_value.X_add_symbol);
783 symbolS *orig_op_symbol = get_real_sym (symbolP->sy_value.X_op_symbol);
784 symbolS *add_symbol = orig_add_symbol;
785 symbolS *op_symbol = orig_op_symbol;
787 if (symbolP->sy_flags.sy_forward_ref)
788 is_forward = 1;
790 if (is_forward)
792 /* assign_symbol() clones volatile symbols; pre-existing expressions
793 hold references to the original instance, but want the current
794 value. Just repeat the lookup. */
795 if (add_symbol && S_IS_VOLATILE (add_symbol))
796 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
797 if (op_symbol && S_IS_VOLATILE (op_symbol))
798 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
801 /* Re-using sy_resolving here, as this routine cannot get called from
802 symbol resolution code. */
803 if ((symbolP->bsym->section == expr_section
804 || symbolP->sy_flags.sy_forward_ref)
805 && !symbolP->sy_flags.sy_resolving)
807 symbolP->sy_flags.sy_resolving = 1;
808 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
809 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
810 symbolP->sy_flags.sy_resolving = 0;
813 if (symbolP->sy_flags.sy_forward_ref
814 || add_symbol != orig_add_symbol
815 || op_symbol != orig_op_symbol)
817 if (symbolP != &dot_symbol)
819 symbolP = symbol_clone (symbolP, 0);
820 symbolP->sy_flags.sy_resolving = 0;
822 else
824 symbolP = symbol_temp_new_now ();
825 #ifdef tc_new_dot_label
826 tc_new_dot_label (symbolP);
827 #endif
831 symbolP->sy_value.X_add_symbol = add_symbol;
832 symbolP->sy_value.X_op_symbol = op_symbol;
835 return symbolP;
838 symbolS *
839 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
841 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
844 symbolS *
845 symbol_temp_new_now (void)
847 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
850 symbolS *
851 symbol_temp_new_now_octets (void)
853 symbolS * symb = symbol_temp_new (now_seg, frag_now_fix_octets (), frag_now);
854 symb->sy_flags.sy_octets = 1;
855 return symb;
858 symbolS *
859 symbol_temp_make (void)
861 return symbol_make (FAKE_LABEL_NAME);
864 /* Implement symbol table lookup.
865 In: A symbol's name as a string: '\0' can't be part of a symbol name.
866 Out: NULL if the name was not in the symbol table, else the address
867 of a struct symbol associated with that name. */
869 symbolS *
870 symbol_find_exact (const char *name)
872 return symbol_find_exact_noref (name, 0);
875 symbolS *
876 symbol_find_exact_noref (const char *name, int noref)
878 struct local_symbol *locsym;
879 symbolS* sym;
881 locsym = (struct local_symbol *) hash_find (local_hash, name);
882 if (locsym != NULL)
883 return (symbolS *) locsym;
885 sym = ((symbolS *) hash_find (sy_hash, name));
887 /* Any references to the symbol, except for the reference in
888 .weakref, must clear this flag, such that the symbol does not
889 turn into a weak symbol. Note that we don't have to handle the
890 local_symbol case, since a weakrefd is always promoted out of the
891 local_symbol table when it is turned into a weak symbol. */
892 if (sym && ! noref)
893 S_CLEAR_WEAKREFD (sym);
895 return sym;
898 symbolS *
899 symbol_find (const char *name)
901 return symbol_find_noref (name, 0);
904 symbolS *
905 symbol_find_noref (const char *name, int noref)
907 symbolS * result;
908 char * copy = NULL;
910 #ifdef tc_canonicalize_symbol_name
912 copy = xstrdup (name);
913 name = tc_canonicalize_symbol_name (copy);
915 #endif
917 if (! symbols_case_sensitive)
919 const char *orig;
920 char *copy2 = NULL;
921 unsigned char c;
923 orig = name;
924 if (copy != NULL)
925 copy2 = copy;
926 name = copy = XNEWVEC (char, strlen (name) + 1);
928 while ((c = *orig++) != '\0')
929 *copy++ = TOUPPER (c);
930 *copy = '\0';
932 if (copy2 != NULL)
933 free (copy2);
934 copy = (char *) name;
937 result = symbol_find_exact_noref (name, noref);
938 if (copy != NULL)
939 free (copy);
940 return result;
943 /* Once upon a time, symbols were kept in a singly linked list. At
944 least coff needs to be able to rearrange them from time to time, for
945 which a doubly linked list is much more convenient. Loic did these
946 as macros which seemed dangerous to me so they're now functions.
947 xoxorich. */
949 /* Link symbol ADDME after symbol TARGET in the chain. */
951 void
952 symbol_append (symbolS *addme, symbolS *target,
953 symbolS **rootPP, symbolS **lastPP)
955 if (LOCAL_SYMBOL_CHECK (addme))
956 abort ();
957 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
958 abort ();
960 if (target == NULL)
962 know (*rootPP == NULL);
963 know (*lastPP == NULL);
964 addme->sy_next = NULL;
965 addme->sy_previous = NULL;
966 *rootPP = addme;
967 *lastPP = addme;
968 return;
969 } /* if the list is empty */
971 if (target->sy_next != NULL)
973 target->sy_next->sy_previous = addme;
975 else
977 know (*lastPP == target);
978 *lastPP = addme;
979 } /* if we have a next */
981 addme->sy_next = target->sy_next;
982 target->sy_next = addme;
983 addme->sy_previous = target;
985 debug_verify_symchain (symbol_rootP, symbol_lastP);
988 /* Set the chain pointers of SYMBOL to null. */
990 void
991 symbol_clear_list_pointers (symbolS *symbolP)
993 if (LOCAL_SYMBOL_CHECK (symbolP))
994 abort ();
995 symbolP->sy_next = NULL;
996 symbolP->sy_previous = NULL;
999 /* Remove SYMBOLP from the list. */
1001 void
1002 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1004 if (LOCAL_SYMBOL_CHECK (symbolP))
1005 abort ();
1007 if (symbolP == *rootPP)
1009 *rootPP = symbolP->sy_next;
1010 } /* if it was the root */
1012 if (symbolP == *lastPP)
1014 *lastPP = symbolP->sy_previous;
1015 } /* if it was the tail */
1017 if (symbolP->sy_next != NULL)
1019 symbolP->sy_next->sy_previous = symbolP->sy_previous;
1020 } /* if not last */
1022 if (symbolP->sy_previous != NULL)
1024 symbolP->sy_previous->sy_next = symbolP->sy_next;
1025 } /* if not first */
1027 debug_verify_symchain (*rootPP, *lastPP);
1030 /* Link symbol ADDME before symbol TARGET in the chain. */
1032 void
1033 symbol_insert (symbolS *addme, symbolS *target,
1034 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
1036 if (LOCAL_SYMBOL_CHECK (addme))
1037 abort ();
1038 if (LOCAL_SYMBOL_CHECK (target))
1039 abort ();
1041 if (target->sy_previous != NULL)
1043 target->sy_previous->sy_next = addme;
1045 else
1047 know (*rootPP == target);
1048 *rootPP = addme;
1049 } /* if not first */
1051 addme->sy_previous = target->sy_previous;
1052 target->sy_previous = addme;
1053 addme->sy_next = target;
1055 debug_verify_symchain (*rootPP, *lastPP);
1058 void
1059 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
1061 symbolS *symbolP = rootP;
1063 if (symbolP == NULL)
1064 return;
1066 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1068 gas_assert (symbolP->bsym != NULL);
1069 gas_assert (symbolP->sy_flags.sy_local_symbol == 0);
1070 gas_assert (symbolP->sy_next->sy_previous == symbolP);
1073 gas_assert (lastP == symbolP);
1077 symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1079 return (!LOCAL_SYMBOL_CHECK (s)
1080 && ((s->sy_next != s
1081 && s->sy_next != NULL
1082 && s->sy_next->sy_previous == s)
1083 || s == lastPP)
1084 && ((s->sy_previous != s
1085 && s->sy_previous != NULL
1086 && s->sy_previous->sy_next == s)
1087 || s == rootPP));
1090 #ifdef OBJ_COMPLEX_RELC
1092 static int
1093 use_complex_relocs_for (symbolS * symp)
1095 switch (symp->sy_value.X_op)
1097 case O_constant:
1098 return 0;
1100 case O_multiply:
1101 case O_divide:
1102 case O_modulus:
1103 case O_left_shift:
1104 case O_right_shift:
1105 case O_bit_inclusive_or:
1106 case O_bit_or_not:
1107 case O_bit_exclusive_or:
1108 case O_bit_and:
1109 case O_add:
1110 case O_subtract:
1111 case O_eq:
1112 case O_ne:
1113 case O_lt:
1114 case O_le:
1115 case O_ge:
1116 case O_gt:
1117 case O_logical_and:
1118 case O_logical_or:
1119 if ((S_IS_COMMON (symp->sy_value.X_op_symbol)
1120 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
1121 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
1122 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
1124 case O_symbol:
1125 case O_symbol_rva:
1126 case O_uminus:
1127 case O_bit_not:
1128 case O_logical_not:
1129 if ((S_IS_COMMON (symp->sy_value.X_add_symbol)
1130 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
1131 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
1132 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section)
1133 return 0;
1135 break;
1137 default:
1138 break;
1140 return 1;
1142 #endif
1144 static void
1145 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
1147 const char *file;
1148 unsigned int line;
1149 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1150 segT seg_right = S_GET_SEGMENT (right);
1151 const char *opname;
1153 switch (op)
1155 default:
1156 abort ();
1157 return;
1159 case O_uminus: opname = "-"; break;
1160 case O_bit_not: opname = "~"; break;
1161 case O_logical_not: opname = "!"; break;
1162 case O_multiply: opname = "*"; break;
1163 case O_divide: opname = "/"; break;
1164 case O_modulus: opname = "%"; break;
1165 case O_left_shift: opname = "<<"; break;
1166 case O_right_shift: opname = ">>"; break;
1167 case O_bit_inclusive_or: opname = "|"; break;
1168 case O_bit_or_not: opname = "|~"; break;
1169 case O_bit_exclusive_or: opname = "^"; break;
1170 case O_bit_and: opname = "&"; break;
1171 case O_add: opname = "+"; break;
1172 case O_subtract: opname = "-"; break;
1173 case O_eq: opname = "=="; break;
1174 case O_ne: opname = "!="; break;
1175 case O_lt: opname = "<"; break;
1176 case O_le: opname = "<="; break;
1177 case O_ge: opname = ">="; break;
1178 case O_gt: opname = ">"; break;
1179 case O_logical_and: opname = "&&"; break;
1180 case O_logical_or: opname = "||"; break;
1183 if (expr_symbol_where (symp, &file, &line))
1185 if (left)
1186 as_bad_where (file, line,
1187 _("invalid operands (%s and %s sections) for `%s'"),
1188 seg_left->name, seg_right->name, opname);
1189 else
1190 as_bad_where (file, line,
1191 _("invalid operand (%s section) for `%s'"),
1192 seg_right->name, opname);
1194 else
1196 const char *sname = S_GET_NAME (symp);
1198 if (left)
1199 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1200 seg_left->name, seg_right->name, opname, sname);
1201 else
1202 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1203 seg_right->name, opname, sname);
1207 /* Resolve the value of a symbol. This is called during the final
1208 pass over the symbol table to resolve any symbols with complex
1209 values. */
1211 valueT
1212 resolve_symbol_value (symbolS *symp)
1214 int resolved;
1215 valueT final_val;
1216 segT final_seg;
1218 if (LOCAL_SYMBOL_CHECK (symp))
1220 struct local_symbol *locsym = (struct local_symbol *) symp;
1222 final_val = locsym->lsy_value;
1223 if (local_symbol_resolved_p (locsym))
1224 return final_val;
1226 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1228 if (finalize_syms)
1230 locsym->lsy_value = final_val;
1231 local_symbol_mark_resolved (locsym);
1234 return final_val;
1237 if (symp->sy_flags.sy_resolved)
1239 final_val = 0;
1240 while (symp->sy_value.X_op == O_symbol
1241 && symp->sy_value.X_add_symbol->sy_flags.sy_resolved)
1243 final_val += symp->sy_value.X_add_number;
1244 symp = symp->sy_value.X_add_symbol;
1246 if (symp->sy_value.X_op == O_constant)
1247 final_val += symp->sy_value.X_add_number;
1248 else
1249 final_val = 0;
1250 return final_val;
1253 resolved = 0;
1254 final_seg = S_GET_SEGMENT (symp);
1256 if (symp->sy_flags.sy_resolving)
1258 if (finalize_syms)
1259 as_bad (_("symbol definition loop encountered at `%s'"),
1260 S_GET_NAME (symp));
1261 final_val = 0;
1262 resolved = 1;
1264 #ifdef OBJ_COMPLEX_RELC
1265 else if (final_seg == expr_section
1266 && use_complex_relocs_for (symp))
1268 symbolS * relc_symbol = NULL;
1269 char * relc_symbol_name = NULL;
1271 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1273 /* For debugging, print out conversion input & output. */
1274 #ifdef DEBUG_SYMS
1275 print_expr (& symp->sy_value);
1276 if (relc_symbol_name)
1277 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1278 #endif
1280 if (relc_symbol_name != NULL)
1281 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1282 0, & zero_address_frag);
1284 if (relc_symbol == NULL)
1286 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1287 S_GET_NAME (symp));
1288 resolved = 0;
1290 else
1292 symbol_table_insert (relc_symbol);
1294 /* S_CLEAR_EXTERNAL (relc_symbol); */
1295 if (symp->bsym->flags & BSF_SRELC)
1296 relc_symbol->bsym->flags |= BSF_SRELC;
1297 else
1298 relc_symbol->bsym->flags |= BSF_RELC;
1299 /* symp->bsym->flags |= BSF_RELC; */
1300 copy_symbol_attributes (symp, relc_symbol);
1301 symp->sy_value.X_op = O_symbol;
1302 symp->sy_value.X_add_symbol = relc_symbol;
1303 symp->sy_value.X_add_number = 0;
1304 resolved = 1;
1307 final_val = 0;
1308 final_seg = undefined_section;
1309 goto exit_dont_set_value;
1311 #endif
1312 else
1314 symbolS *add_symbol, *op_symbol;
1315 offsetT left, right;
1316 segT seg_left, seg_right;
1317 operatorT op;
1318 int move_seg_ok;
1320 symp->sy_flags.sy_resolving = 1;
1322 /* Help out with CSE. */
1323 add_symbol = symp->sy_value.X_add_symbol;
1324 op_symbol = symp->sy_value.X_op_symbol;
1325 final_val = symp->sy_value.X_add_number;
1326 op = symp->sy_value.X_op;
1328 switch (op)
1330 default:
1331 BAD_CASE (op);
1332 break;
1334 case O_absent:
1335 final_val = 0;
1336 /* Fall through. */
1338 case O_constant:
1339 if (symp->sy_flags.sy_octets)
1340 final_val += symp->sy_frag->fr_address;
1341 else
1342 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1343 if (final_seg == expr_section)
1344 final_seg = absolute_section;
1345 /* Fall through. */
1347 case O_register:
1348 resolved = 1;
1349 break;
1351 case O_symbol:
1352 case O_symbol_rva:
1353 left = resolve_symbol_value (add_symbol);
1354 seg_left = S_GET_SEGMENT (add_symbol);
1355 if (finalize_syms)
1356 symp->sy_value.X_op_symbol = NULL;
1358 do_symbol:
1359 if (S_IS_WEAKREFR (symp))
1361 gas_assert (final_val == 0);
1362 if (S_IS_WEAKREFR (add_symbol))
1364 gas_assert (add_symbol->sy_value.X_op == O_symbol
1365 && add_symbol->sy_value.X_add_number == 0);
1366 add_symbol = add_symbol->sy_value.X_add_symbol;
1367 gas_assert (! S_IS_WEAKREFR (add_symbol));
1368 symp->sy_value.X_add_symbol = add_symbol;
1372 if (symp->sy_flags.sy_mri_common)
1374 /* This is a symbol inside an MRI common section. The
1375 relocation routines are going to handle it specially.
1376 Don't change the value. */
1377 resolved = symbol_resolved_p (add_symbol);
1378 break;
1381 if (finalize_syms && final_val == 0)
1383 if (LOCAL_SYMBOL_CHECK (add_symbol))
1384 add_symbol = local_symbol_convert ((struct local_symbol *)
1385 add_symbol);
1386 copy_symbol_attributes (symp, add_symbol);
1389 /* If we have equated this symbol to an undefined or common
1390 symbol, keep X_op set to O_symbol, and don't change
1391 X_add_number. This permits the routine which writes out
1392 relocation to detect this case, and convert the
1393 relocation to be against the symbol to which this symbol
1394 is equated. */
1395 if (seg_left == undefined_section
1396 || bfd_is_com_section (seg_left)
1397 #if defined (OBJ_COFF) && defined (TE_PE)
1398 || S_IS_WEAK (add_symbol)
1399 #endif
1400 || (finalize_syms
1401 && ((final_seg == expr_section
1402 && seg_left != expr_section
1403 && seg_left != absolute_section)
1404 || symbol_shadow_p (symp))))
1406 if (finalize_syms)
1408 symp->sy_value.X_op = O_symbol;
1409 symp->sy_value.X_add_symbol = add_symbol;
1410 symp->sy_value.X_add_number = final_val;
1411 /* Use X_op_symbol as a flag. */
1412 symp->sy_value.X_op_symbol = add_symbol;
1414 final_seg = seg_left;
1415 final_val += symp->sy_frag->fr_address + left;
1416 resolved = symbol_resolved_p (add_symbol);
1417 symp->sy_flags.sy_resolving = 0;
1418 goto exit_dont_set_value;
1420 else
1422 final_val += symp->sy_frag->fr_address + left;
1423 if (final_seg == expr_section || final_seg == undefined_section)
1424 final_seg = seg_left;
1427 resolved = symbol_resolved_p (add_symbol);
1428 if (S_IS_WEAKREFR (symp))
1430 symp->sy_flags.sy_resolving = 0;
1431 goto exit_dont_set_value;
1433 break;
1435 case O_uminus:
1436 case O_bit_not:
1437 case O_logical_not:
1438 left = resolve_symbol_value (add_symbol);
1439 seg_left = S_GET_SEGMENT (add_symbol);
1441 /* By reducing these to the relevant dyadic operator, we get
1442 !S -> S == 0 permitted on anything,
1443 -S -> 0 - S only permitted on absolute
1444 ~S -> S ^ ~0 only permitted on absolute */
1445 if (op != O_logical_not && seg_left != absolute_section
1446 && finalize_syms)
1447 report_op_error (symp, NULL, op, add_symbol);
1449 if (final_seg == expr_section || final_seg == undefined_section)
1450 final_seg = absolute_section;
1452 if (op == O_uminus)
1453 left = -left;
1454 else if (op == O_logical_not)
1455 left = !left;
1456 else
1457 left = ~left;
1459 final_val += left + symp->sy_frag->fr_address;
1461 resolved = symbol_resolved_p (add_symbol);
1462 break;
1464 case O_multiply:
1465 case O_divide:
1466 case O_modulus:
1467 case O_left_shift:
1468 case O_right_shift:
1469 case O_bit_inclusive_or:
1470 case O_bit_or_not:
1471 case O_bit_exclusive_or:
1472 case O_bit_and:
1473 case O_add:
1474 case O_subtract:
1475 case O_eq:
1476 case O_ne:
1477 case O_lt:
1478 case O_le:
1479 case O_ge:
1480 case O_gt:
1481 case O_logical_and:
1482 case O_logical_or:
1483 left = resolve_symbol_value (add_symbol);
1484 right = resolve_symbol_value (op_symbol);
1485 seg_left = S_GET_SEGMENT (add_symbol);
1486 seg_right = S_GET_SEGMENT (op_symbol);
1488 /* Simplify addition or subtraction of a constant by folding the
1489 constant into X_add_number. */
1490 if (op == O_add)
1492 if (seg_right == absolute_section)
1494 final_val += right;
1495 goto do_symbol;
1497 else if (seg_left == absolute_section)
1499 final_val += left;
1500 add_symbol = op_symbol;
1501 left = right;
1502 seg_left = seg_right;
1503 goto do_symbol;
1506 else if (op == O_subtract)
1508 if (seg_right == absolute_section)
1510 final_val -= right;
1511 goto do_symbol;
1515 move_seg_ok = 1;
1516 /* Equality and non-equality tests are permitted on anything.
1517 Subtraction, and other comparison operators are permitted if
1518 both operands are in the same section. Otherwise, both
1519 operands must be absolute. We already handled the case of
1520 addition or subtraction of a constant above. This will
1521 probably need to be changed for an object file format which
1522 supports arbitrary expressions. */
1523 if (!(seg_left == absolute_section
1524 && seg_right == absolute_section)
1525 && !(op == O_eq || op == O_ne)
1526 && !((op == O_subtract
1527 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1528 && seg_left == seg_right
1529 && (seg_left != undefined_section
1530 || add_symbol == op_symbol)))
1532 /* Don't emit messages unless we're finalizing the symbol value,
1533 otherwise we may get the same message multiple times. */
1534 if (finalize_syms)
1535 report_op_error (symp, add_symbol, op, op_symbol);
1536 /* However do not move the symbol into the absolute section
1537 if it cannot currently be resolved - this would confuse
1538 other parts of the assembler into believing that the
1539 expression had been evaluated to zero. */
1540 else
1541 move_seg_ok = 0;
1544 if (move_seg_ok
1545 && (final_seg == expr_section || final_seg == undefined_section))
1546 final_seg = absolute_section;
1548 /* Check for division by zero. */
1549 if ((op == O_divide || op == O_modulus) && right == 0)
1551 /* If seg_right is not absolute_section, then we've
1552 already issued a warning about using a bad symbol. */
1553 if (seg_right == absolute_section && finalize_syms)
1555 const char *file;
1556 unsigned int line;
1558 if (expr_symbol_where (symp, &file, &line))
1559 as_bad_where (file, line, _("division by zero"));
1560 else
1561 as_bad (_("division by zero when setting `%s'"),
1562 S_GET_NAME (symp));
1565 right = 1;
1568 switch (symp->sy_value.X_op)
1570 case O_multiply: left *= right; break;
1571 case O_divide: left /= right; break;
1572 case O_modulus: left %= right; break;
1573 case O_left_shift: left <<= right; break;
1574 case O_right_shift: left >>= right; break;
1575 case O_bit_inclusive_or: left |= right; break;
1576 case O_bit_or_not: left |= ~right; break;
1577 case O_bit_exclusive_or: left ^= right; break;
1578 case O_bit_and: left &= right; break;
1579 case O_add: left += right; break;
1580 case O_subtract: left -= right; break;
1581 case O_eq:
1582 case O_ne:
1583 left = (left == right && seg_left == seg_right
1584 && (seg_left != undefined_section
1585 || add_symbol == op_symbol)
1586 ? ~ (offsetT) 0 : 0);
1587 if (symp->sy_value.X_op == O_ne)
1588 left = ~left;
1589 break;
1590 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1591 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1592 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1593 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1594 case O_logical_and: left = left && right; break;
1595 case O_logical_or: left = left || right; break;
1597 case O_illegal:
1598 case O_absent:
1599 case O_constant:
1600 /* See PR 20895 for a reproducer. */
1601 as_bad (_("Invalid operation on symbol"));
1602 goto exit_dont_set_value;
1604 default:
1605 abort ();
1608 final_val += symp->sy_frag->fr_address + left;
1609 if (final_seg == expr_section || final_seg == undefined_section)
1611 if (seg_left == undefined_section
1612 || seg_right == undefined_section)
1613 final_seg = undefined_section;
1614 else if (seg_left == absolute_section)
1615 final_seg = seg_right;
1616 else
1617 final_seg = seg_left;
1619 resolved = (symbol_resolved_p (add_symbol)
1620 && symbol_resolved_p (op_symbol));
1621 break;
1623 case O_big:
1624 case O_illegal:
1625 /* Give an error (below) if not in expr_section. We don't
1626 want to worry about expr_section symbols, because they
1627 are fictional (they are created as part of expression
1628 resolution), and any problems may not actually mean
1629 anything. */
1630 break;
1633 symp->sy_flags.sy_resolving = 0;
1636 if (finalize_syms)
1637 S_SET_VALUE (symp, final_val);
1639 exit_dont_set_value:
1640 /* Always set the segment, even if not finalizing the value.
1641 The segment is used to determine whether a symbol is defined. */
1642 S_SET_SEGMENT (symp, final_seg);
1644 /* Don't worry if we can't resolve an expr_section symbol. */
1645 if (finalize_syms)
1647 if (resolved)
1648 symp->sy_flags.sy_resolved = 1;
1649 else if (S_GET_SEGMENT (symp) != expr_section)
1651 as_bad (_("can't resolve value for symbol `%s'"),
1652 S_GET_NAME (symp));
1653 symp->sy_flags.sy_resolved = 1;
1657 return final_val;
1660 static void resolve_local_symbol (const char *, void *);
1662 /* A static function passed to hash_traverse. */
1664 static void
1665 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1667 if (value != NULL)
1668 resolve_symbol_value ((symbolS *) value);
1671 /* Resolve all local symbols. */
1673 void
1674 resolve_local_symbol_values (void)
1676 hash_traverse (local_hash, resolve_local_symbol);
1679 /* Obtain the current value of a symbol without changing any
1680 sub-expressions used. */
1683 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1685 symbolS *symbolP = *symbolPP;
1687 if (LOCAL_SYMBOL_CHECK (symbolP))
1689 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1691 *valueP = locsym->lsy_value;
1692 *segP = locsym->lsy_section;
1693 *fragPP = local_symbol_get_frag (locsym);
1695 else
1697 expressionS exp = symbolP->sy_value;
1699 if (!symbolP->sy_flags.sy_resolved && exp.X_op != O_illegal)
1701 int resolved;
1703 if (symbolP->sy_flags.sy_resolving)
1704 return 0;
1705 symbolP->sy_flags.sy_resolving = 1;
1706 resolved = resolve_expression (&exp);
1707 symbolP->sy_flags.sy_resolving = 0;
1708 if (!resolved)
1709 return 0;
1711 switch (exp.X_op)
1713 case O_constant:
1714 case O_register:
1715 if (!symbol_equated_p (symbolP))
1716 break;
1717 /* Fallthru. */
1718 case O_symbol:
1719 case O_symbol_rva:
1720 symbolP = exp.X_add_symbol;
1721 break;
1722 default:
1723 return 0;
1727 *symbolPP = symbolP;
1729 /* A bogus input file can result in resolve_expression()
1730 generating a local symbol, so we have to check again. */
1731 if (LOCAL_SYMBOL_CHECK (symbolP))
1733 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1735 *valueP = locsym->lsy_value;
1736 *segP = locsym->lsy_section;
1737 *fragPP = local_symbol_get_frag (locsym);
1739 else
1741 *valueP = exp.X_add_number;
1742 *segP = symbolP->bsym->section;
1743 *fragPP = symbolP->sy_frag;
1746 if (*segP == expr_section)
1747 switch (exp.X_op)
1749 case O_constant: *segP = absolute_section; break;
1750 case O_register: *segP = reg_section; break;
1751 default: break;
1755 return 1;
1758 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1759 They are *really* local. That is, they go out of scope whenever we see a
1760 label that isn't local. Also, like fb labels, there can be multiple
1761 instances of a dollar label. Therefor, we name encode each instance with
1762 the instance number, keep a list of defined symbols separate from the real
1763 symbol table, and we treat these buggers as a sparse array. */
1765 static long *dollar_labels;
1766 static long *dollar_label_instances;
1767 static char *dollar_label_defines;
1768 static unsigned long dollar_label_count;
1769 static unsigned long dollar_label_max;
1772 dollar_label_defined (long label)
1774 long *i;
1776 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1778 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1779 if (*i == label)
1780 return dollar_label_defines[i - dollar_labels];
1782 /* If we get here, label isn't defined. */
1783 return 0;
1786 static long
1787 dollar_label_instance (long label)
1789 long *i;
1791 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1793 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1794 if (*i == label)
1795 return (dollar_label_instances[i - dollar_labels]);
1797 /* If we get here, we haven't seen the label before.
1798 Therefore its instance count is zero. */
1799 return 0;
1802 void
1803 dollar_label_clear (void)
1805 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1808 #define DOLLAR_LABEL_BUMP_BY 10
1810 void
1811 define_dollar_label (long label)
1813 long *i;
1815 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1816 if (*i == label)
1818 ++dollar_label_instances[i - dollar_labels];
1819 dollar_label_defines[i - dollar_labels] = 1;
1820 return;
1823 /* If we get to here, we don't have label listed yet. */
1825 if (dollar_labels == NULL)
1827 dollar_labels = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
1828 dollar_label_instances = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
1829 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1830 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1831 dollar_label_count = 0;
1833 else if (dollar_label_count == dollar_label_max)
1835 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1836 dollar_labels = XRESIZEVEC (long, dollar_labels, dollar_label_max);
1837 dollar_label_instances = XRESIZEVEC (long, dollar_label_instances,
1838 dollar_label_max);
1839 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
1840 dollar_label_max);
1841 } /* if we needed to grow */
1843 dollar_labels[dollar_label_count] = label;
1844 dollar_label_instances[dollar_label_count] = 1;
1845 dollar_label_defines[dollar_label_count] = 1;
1846 ++dollar_label_count;
1849 /* Caller must copy returned name: we re-use the area for the next name.
1851 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1852 where n is the label number and m is the instance number. "L" makes
1853 it a label discarded unless debugging and "^A"('\1') ensures no
1854 ordinary symbol SHOULD get the same name as a local label
1855 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1857 fb labels get the same treatment, except that ^B is used in place
1858 of ^A. */
1860 char * /* Return local label name. */
1861 dollar_label_name (long n, /* we just saw "n$:" : n a number. */
1862 int augend /* 0 for current instance, 1 for new instance. */)
1864 long i;
1865 /* Returned to caller, then copied. Used for created names ("4f"). */
1866 static char symbol_name_build[24];
1867 char *p;
1868 char *q;
1869 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1871 know (n >= 0);
1872 know (augend == 0 || augend == 1);
1873 p = symbol_name_build;
1874 #ifdef LOCAL_LABEL_PREFIX
1875 *p++ = LOCAL_LABEL_PREFIX;
1876 #endif
1877 *p++ = 'L';
1879 /* Next code just does sprintf( {}, "%d", n); */
1880 /* Label number. */
1881 q = symbol_name_temporary;
1882 for (*q++ = 0, i = n; i; ++q)
1884 *q = i % 10 + '0';
1885 i /= 10;
1887 while ((*p = *--q) != '\0')
1888 ++p;
1890 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1892 /* Instance number. */
1893 q = symbol_name_temporary;
1894 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1896 *q = i % 10 + '0';
1897 i /= 10;
1899 while ((*p++ = *--q) != '\0');
1901 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1902 return symbol_name_build;
1905 /* Somebody else's idea of local labels. They are made by "n:" where n
1906 is any decimal digit. Refer to them with
1907 "nb" for previous (backward) n:
1908 or "nf" for next (forward) n:.
1910 We do a little better and let n be any number, not just a single digit, but
1911 since the other guy's assembler only does ten, we treat the first ten
1912 specially.
1914 Like someone else's assembler, we have one set of local label counters for
1915 entire assembly, not one set per (sub)segment like in most assemblers. This
1916 implies that one can refer to a label in another segment, and indeed some
1917 crufty compilers have done just that.
1919 Since there could be a LOT of these things, treat them as a sparse
1920 array. */
1922 #define FB_LABEL_SPECIAL (10)
1924 static long fb_low_counter[FB_LABEL_SPECIAL];
1925 static long *fb_labels;
1926 static long *fb_label_instances;
1927 static long fb_label_count;
1928 static long fb_label_max;
1930 /* This must be more than FB_LABEL_SPECIAL. */
1931 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1933 static void
1934 fb_label_init (void)
1936 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1939 /* Add one to the instance number of this fb label. */
1941 void
1942 fb_label_instance_inc (long label)
1944 long *i;
1946 if ((unsigned long) label < FB_LABEL_SPECIAL)
1948 ++fb_low_counter[label];
1949 return;
1952 if (fb_labels != NULL)
1954 for (i = fb_labels + FB_LABEL_SPECIAL;
1955 i < fb_labels + fb_label_count; ++i)
1957 if (*i == label)
1959 ++fb_label_instances[i - fb_labels];
1960 return;
1961 } /* if we find it */
1962 } /* for each existing label */
1965 /* If we get to here, we don't have label listed yet. */
1967 if (fb_labels == NULL)
1969 fb_labels = XNEWVEC (long, FB_LABEL_BUMP_BY);
1970 fb_label_instances = XNEWVEC (long, FB_LABEL_BUMP_BY);
1971 fb_label_max = FB_LABEL_BUMP_BY;
1972 fb_label_count = FB_LABEL_SPECIAL;
1975 else if (fb_label_count == fb_label_max)
1977 fb_label_max += FB_LABEL_BUMP_BY;
1978 fb_labels = XRESIZEVEC (long, fb_labels, fb_label_max);
1979 fb_label_instances = XRESIZEVEC (long, fb_label_instances, fb_label_max);
1980 } /* if we needed to grow */
1982 fb_labels[fb_label_count] = label;
1983 fb_label_instances[fb_label_count] = 1;
1984 ++fb_label_count;
1987 static long
1988 fb_label_instance (long label)
1990 long *i;
1992 if ((unsigned long) label < FB_LABEL_SPECIAL)
1994 return (fb_low_counter[label]);
1997 if (fb_labels != NULL)
1999 for (i = fb_labels + FB_LABEL_SPECIAL;
2000 i < fb_labels + fb_label_count; ++i)
2002 if (*i == label)
2004 return (fb_label_instances[i - fb_labels]);
2005 } /* if we find it */
2006 } /* for each existing label */
2009 /* We didn't find the label, so this must be a reference to the
2010 first instance. */
2011 return 0;
2014 /* Caller must copy returned name: we re-use the area for the next name.
2016 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2017 where n is the label number and m is the instance number. "L" makes
2018 it a label discarded unless debugging and "^B"('\2') ensures no
2019 ordinary symbol SHOULD get the same name as a local label
2020 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2022 dollar labels get the same treatment, except that ^A is used in
2023 place of ^B. */
2025 char * /* Return local label name. */
2026 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
2027 long augend /* 0 for nb, 1 for n:, nf. */)
2029 long i;
2030 /* Returned to caller, then copied. Used for created names ("4f"). */
2031 static char symbol_name_build[24];
2032 char *p;
2033 char *q;
2034 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
2036 know (n >= 0);
2037 #ifdef TC_MMIX
2038 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
2039 #else
2040 know ((unsigned long) augend <= 1);
2041 #endif
2042 p = symbol_name_build;
2043 #ifdef LOCAL_LABEL_PREFIX
2044 *p++ = LOCAL_LABEL_PREFIX;
2045 #endif
2046 *p++ = 'L';
2048 /* Next code just does sprintf( {}, "%d", n); */
2049 /* Label number. */
2050 q = symbol_name_temporary;
2051 for (*q++ = 0, i = n; i; ++q)
2053 *q = i % 10 + '0';
2054 i /= 10;
2056 while ((*p = *--q) != '\0')
2057 ++p;
2059 *p++ = LOCAL_LABEL_CHAR; /* ^B */
2061 /* Instance number. */
2062 q = symbol_name_temporary;
2063 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
2065 *q = i % 10 + '0';
2066 i /= 10;
2068 while ((*p++ = *--q) != '\0');
2070 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2071 return (symbol_name_build);
2074 /* Decode name that may have been generated by foo_label_name() above.
2075 If the name wasn't generated by foo_label_name(), then return it
2076 unaltered. This is used for error messages. */
2078 char *
2079 decode_local_label_name (char *s)
2081 char *p;
2082 char *symbol_decode;
2083 int label_number;
2084 int instance_number;
2085 const char *type;
2086 const char *message_format;
2087 int lindex = 0;
2089 #ifdef LOCAL_LABEL_PREFIX
2090 if (s[lindex] == LOCAL_LABEL_PREFIX)
2091 ++lindex;
2092 #endif
2094 if (s[lindex] != 'L')
2095 return s;
2097 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
2098 label_number = (10 * label_number) + *p - '0';
2100 if (*p == DOLLAR_LABEL_CHAR)
2101 type = "dollar";
2102 else if (*p == LOCAL_LABEL_CHAR)
2103 type = "fb";
2104 else
2105 return s;
2107 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
2108 instance_number = (10 * instance_number) + *p - '0';
2110 message_format = _("\"%d\" (instance number %d of a %s label)");
2111 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
2112 sprintf (symbol_decode, message_format, label_number, instance_number, type);
2114 return symbol_decode;
2117 /* Get the value of a symbol. */
2119 valueT
2120 S_GET_VALUE (symbolS *s)
2122 if (LOCAL_SYMBOL_CHECK (s))
2123 return resolve_symbol_value (s);
2125 if (!s->sy_flags.sy_resolved)
2127 valueT val = resolve_symbol_value (s);
2128 if (!finalize_syms)
2129 return val;
2131 if (S_IS_WEAKREFR (s))
2132 return S_GET_VALUE (s->sy_value.X_add_symbol);
2134 if (s->sy_value.X_op != O_constant)
2136 if (! s->sy_flags.sy_resolved
2137 || s->sy_value.X_op != O_symbol
2138 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
2139 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2140 S_GET_NAME (s));
2142 return (valueT) s->sy_value.X_add_number;
2145 /* Set the value of a symbol. */
2147 void
2148 S_SET_VALUE (symbolS *s, valueT val)
2150 if (LOCAL_SYMBOL_CHECK (s))
2152 ((struct local_symbol *) s)->lsy_value = val;
2153 return;
2156 s->sy_value.X_op = O_constant;
2157 s->sy_value.X_add_number = (offsetT) val;
2158 s->sy_value.X_unsigned = 0;
2159 S_CLEAR_WEAKREFR (s);
2162 void
2163 copy_symbol_attributes (symbolS *dest, symbolS *src)
2165 if (LOCAL_SYMBOL_CHECK (dest))
2166 dest = local_symbol_convert ((struct local_symbol *) dest);
2167 if (LOCAL_SYMBOL_CHECK (src))
2168 src = local_symbol_convert ((struct local_symbol *) src);
2170 /* In an expression, transfer the settings of these flags.
2171 The user can override later, of course. */
2172 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2173 | BSF_GNU_INDIRECT_FUNCTION)
2174 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2176 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2177 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2178 #endif
2180 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2181 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2182 #endif
2186 S_IS_FUNCTION (symbolS *s)
2188 flagword flags;
2190 if (LOCAL_SYMBOL_CHECK (s))
2191 return 0;
2193 flags = s->bsym->flags;
2195 return (flags & BSF_FUNCTION) != 0;
2199 S_IS_EXTERNAL (symbolS *s)
2201 flagword flags;
2203 if (LOCAL_SYMBOL_CHECK (s))
2204 return 0;
2206 flags = s->bsym->flags;
2208 /* Sanity check. */
2209 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2210 abort ();
2212 return (flags & BSF_GLOBAL) != 0;
2216 S_IS_WEAK (symbolS *s)
2218 if (LOCAL_SYMBOL_CHECK (s))
2219 return 0;
2220 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2221 could probably handle a WEAKREFR as always weak though. E.g., if
2222 the referenced symbol has lost its weak status, there's no reason
2223 to keep handling the weakrefr as if it was weak. */
2224 if (S_IS_WEAKREFR (s))
2225 return S_IS_WEAK (s->sy_value.X_add_symbol);
2226 return (s->bsym->flags & BSF_WEAK) != 0;
2230 S_IS_WEAKREFR (symbolS *s)
2232 if (LOCAL_SYMBOL_CHECK (s))
2233 return 0;
2234 return s->sy_flags.sy_weakrefr != 0;
2238 S_IS_WEAKREFD (symbolS *s)
2240 if (LOCAL_SYMBOL_CHECK (s))
2241 return 0;
2242 return s->sy_flags.sy_weakrefd != 0;
2246 S_IS_COMMON (symbolS *s)
2248 if (LOCAL_SYMBOL_CHECK (s))
2249 return 0;
2250 return bfd_is_com_section (s->bsym->section);
2254 S_IS_DEFINED (symbolS *s)
2256 if (LOCAL_SYMBOL_CHECK (s))
2257 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2258 return s->bsym->section != undefined_section;
2262 #ifndef EXTERN_FORCE_RELOC
2263 #define EXTERN_FORCE_RELOC IS_ELF
2264 #endif
2266 /* Return true for symbols that should not be reduced to section
2267 symbols or eliminated from expressions, because they may be
2268 overridden by the linker. */
2270 S_FORCE_RELOC (symbolS *s, int strict)
2272 segT sec;
2273 if (LOCAL_SYMBOL_CHECK (s))
2274 sec = ((struct local_symbol *) s)->lsy_section;
2275 else
2277 if ((strict
2278 && ((s->bsym->flags & BSF_WEAK) != 0
2279 || (EXTERN_FORCE_RELOC
2280 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2281 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2282 return TRUE;
2283 sec = s->bsym->section;
2285 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2289 S_IS_DEBUG (symbolS *s)
2291 if (LOCAL_SYMBOL_CHECK (s))
2292 return 0;
2293 if (s->bsym->flags & BSF_DEBUGGING)
2294 return 1;
2295 return 0;
2299 S_IS_LOCAL (symbolS *s)
2301 flagword flags;
2302 const char *name;
2304 if (LOCAL_SYMBOL_CHECK (s))
2305 return 1;
2307 flags = s->bsym->flags;
2309 /* Sanity check. */
2310 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2311 abort ();
2313 if (bfd_get_section (s->bsym) == reg_section)
2314 return 1;
2316 if (flag_strip_local_absolute
2317 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2318 the source file even when the object file is stripped. */
2319 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2320 && bfd_get_section (s->bsym) == absolute_section)
2321 return 1;
2323 name = S_GET_NAME (s);
2324 return (name != NULL
2325 && ! S_IS_DEBUG (s)
2326 && (strchr (name, DOLLAR_LABEL_CHAR)
2327 || strchr (name, LOCAL_LABEL_CHAR)
2328 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2329 || strchr (name, FAKE_LABEL_CHAR)
2330 #endif
2331 || TC_LABEL_IS_LOCAL (name)
2332 || (! flag_keep_locals
2333 && (bfd_is_local_label (stdoutput, s->bsym)
2334 || (flag_mri
2335 && name[0] == '?'
2336 && name[1] == '?')))));
2340 S_IS_STABD (symbolS *s)
2342 return S_GET_NAME (s) == 0;
2346 S_CAN_BE_REDEFINED (const symbolS *s)
2348 if (LOCAL_SYMBOL_CHECK (s))
2349 return (local_symbol_get_frag ((struct local_symbol *) s)
2350 == &predefined_address_frag);
2351 /* Permit register names to be redefined. */
2352 return s->bsym->section == reg_section;
2356 S_IS_VOLATILE (const symbolS *s)
2358 if (LOCAL_SYMBOL_CHECK (s))
2359 return 0;
2360 return s->sy_flags.sy_volatile;
2364 S_IS_FORWARD_REF (const symbolS *s)
2366 if (LOCAL_SYMBOL_CHECK (s))
2367 return 0;
2368 return s->sy_flags.sy_forward_ref;
2371 const char *
2372 S_GET_NAME (symbolS *s)
2374 if (LOCAL_SYMBOL_CHECK (s))
2375 return ((struct local_symbol *) s)->lsy_name;
2376 return s->bsym->name;
2379 segT
2380 S_GET_SEGMENT (symbolS *s)
2382 if (LOCAL_SYMBOL_CHECK (s))
2383 return ((struct local_symbol *) s)->lsy_section;
2384 return s->bsym->section;
2387 void
2388 S_SET_SEGMENT (symbolS *s, segT seg)
2390 /* Don't reassign section symbols. The direct reason is to prevent seg
2391 faults assigning back to const global symbols such as *ABS*, but it
2392 shouldn't happen anyway. */
2394 if (LOCAL_SYMBOL_CHECK (s))
2396 if (seg == reg_section)
2397 s = local_symbol_convert ((struct local_symbol *) s);
2398 else
2400 ((struct local_symbol *) s)->lsy_section = seg;
2401 return;
2405 if (s->bsym->flags & BSF_SECTION_SYM)
2407 if (s->bsym->section != seg)
2408 abort ();
2410 else
2411 s->bsym->section = seg;
2414 void
2415 S_SET_EXTERNAL (symbolS *s)
2417 if (LOCAL_SYMBOL_CHECK (s))
2418 s = local_symbol_convert ((struct local_symbol *) s);
2419 if ((s->bsym->flags & BSF_WEAK) != 0)
2421 /* Let .weak override .global. */
2422 return;
2424 if (s->bsym->flags & BSF_SECTION_SYM)
2426 /* Do not reassign section symbols. */
2427 as_warn (_("section symbols are already global"));
2428 return;
2430 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2431 if (S_GET_SEGMENT (s) == reg_section)
2433 as_bad ("can't make register symbol `%s' global",
2434 S_GET_NAME (s));
2435 return;
2437 #endif
2438 s->bsym->flags |= BSF_GLOBAL;
2439 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2441 #ifdef TE_PE
2442 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2443 an_external_name = S_GET_NAME (s);
2444 #endif
2447 void
2448 S_CLEAR_EXTERNAL (symbolS *s)
2450 if (LOCAL_SYMBOL_CHECK (s))
2451 return;
2452 if ((s->bsym->flags & BSF_WEAK) != 0)
2454 /* Let .weak override. */
2455 return;
2457 s->bsym->flags |= BSF_LOCAL;
2458 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2461 void
2462 S_SET_WEAK (symbolS *s)
2464 if (LOCAL_SYMBOL_CHECK (s))
2465 s = local_symbol_convert ((struct local_symbol *) s);
2466 #ifdef obj_set_weak_hook
2467 obj_set_weak_hook (s);
2468 #endif
2469 s->bsym->flags |= BSF_WEAK;
2470 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2473 void
2474 S_SET_WEAKREFR (symbolS *s)
2476 if (LOCAL_SYMBOL_CHECK (s))
2477 s = local_symbol_convert ((struct local_symbol *) s);
2478 s->sy_flags.sy_weakrefr = 1;
2479 /* If the alias was already used, make sure we mark the target as
2480 used as well, otherwise it might be dropped from the symbol
2481 table. This may have unintended side effects if the alias is
2482 later redirected to another symbol, such as keeping the unused
2483 previous target in the symbol table. Since it will be weak, it's
2484 not a big deal. */
2485 if (s->sy_flags.sy_used)
2486 symbol_mark_used (s->sy_value.X_add_symbol);
2489 void
2490 S_CLEAR_WEAKREFR (symbolS *s)
2492 if (LOCAL_SYMBOL_CHECK (s))
2493 return;
2494 s->sy_flags.sy_weakrefr = 0;
2497 void
2498 S_SET_WEAKREFD (symbolS *s)
2500 if (LOCAL_SYMBOL_CHECK (s))
2501 s = local_symbol_convert ((struct local_symbol *) s);
2502 s->sy_flags.sy_weakrefd = 1;
2503 S_SET_WEAK (s);
2506 void
2507 S_CLEAR_WEAKREFD (symbolS *s)
2509 if (LOCAL_SYMBOL_CHECK (s))
2510 return;
2511 if (s->sy_flags.sy_weakrefd)
2513 s->sy_flags.sy_weakrefd = 0;
2514 /* If a weakref target symbol is weak, then it was never
2515 referenced directly before, not even in a .global directive,
2516 so decay it to local. If it remains undefined, it will be
2517 later turned into a global, like any other undefined
2518 symbol. */
2519 if (s->bsym->flags & BSF_WEAK)
2521 #ifdef obj_clear_weak_hook
2522 obj_clear_weak_hook (s);
2523 #endif
2524 s->bsym->flags &= ~BSF_WEAK;
2525 s->bsym->flags |= BSF_LOCAL;
2530 void
2531 S_SET_THREAD_LOCAL (symbolS *s)
2533 if (LOCAL_SYMBOL_CHECK (s))
2534 s = local_symbol_convert ((struct local_symbol *) s);
2535 if (bfd_is_com_section (s->bsym->section)
2536 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2537 return;
2538 s->bsym->flags |= BSF_THREAD_LOCAL;
2539 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2540 as_bad (_("Accessing function `%s' as thread-local object"),
2541 S_GET_NAME (s));
2542 else if (! bfd_is_und_section (s->bsym->section)
2543 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2544 as_bad (_("Accessing `%s' as thread-local object"),
2545 S_GET_NAME (s));
2548 void
2549 S_SET_NAME (symbolS *s, const char *name)
2551 if (LOCAL_SYMBOL_CHECK (s))
2553 ((struct local_symbol *) s)->lsy_name = name;
2554 return;
2556 s->bsym->name = name;
2559 void
2560 S_SET_VOLATILE (symbolS *s)
2562 if (LOCAL_SYMBOL_CHECK (s))
2563 s = local_symbol_convert ((struct local_symbol *) s);
2564 s->sy_flags.sy_volatile = 1;
2567 void
2568 S_CLEAR_VOLATILE (symbolS *s)
2570 if (!LOCAL_SYMBOL_CHECK (s))
2571 s->sy_flags.sy_volatile = 0;
2574 void
2575 S_SET_FORWARD_REF (symbolS *s)
2577 if (LOCAL_SYMBOL_CHECK (s))
2578 s = local_symbol_convert ((struct local_symbol *) s);
2579 s->sy_flags.sy_forward_ref = 1;
2582 /* Return the previous symbol in a chain. */
2584 symbolS *
2585 symbol_previous (symbolS *s)
2587 if (LOCAL_SYMBOL_CHECK (s))
2588 abort ();
2589 return s->sy_previous;
2592 /* Return the next symbol in a chain. */
2594 symbolS *
2595 symbol_next (symbolS *s)
2597 if (LOCAL_SYMBOL_CHECK (s))
2598 abort ();
2599 return s->sy_next;
2602 /* Return a pointer to the value of a symbol as an expression. */
2604 expressionS *
2605 symbol_get_value_expression (symbolS *s)
2607 if (LOCAL_SYMBOL_CHECK (s))
2608 s = local_symbol_convert ((struct local_symbol *) s);
2609 return &s->sy_value;
2612 /* Set the value of a symbol to an expression. */
2614 void
2615 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2617 if (LOCAL_SYMBOL_CHECK (s))
2618 s = local_symbol_convert ((struct local_symbol *) s);
2619 s->sy_value = *exp;
2620 S_CLEAR_WEAKREFR (s);
2623 /* Return whether 2 symbols are the same. */
2626 symbol_same_p (symbolS *s1, symbolS *s2)
2628 s1 = get_real_sym (s1);
2629 s2 = get_real_sym (s2);
2630 return s1 == s2;
2633 /* Return a pointer to the X_add_number component of a symbol. */
2635 offsetT *
2636 symbol_X_add_number (symbolS *s)
2638 if (LOCAL_SYMBOL_CHECK (s))
2639 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2641 return &s->sy_value.X_add_number;
2644 /* Set the value of SYM to the current position in the current segment. */
2646 void
2647 symbol_set_value_now (symbolS *sym)
2649 S_SET_SEGMENT (sym, now_seg);
2650 S_SET_VALUE (sym, frag_now_fix ());
2651 symbol_set_frag (sym, frag_now);
2654 /* Set the value of SYM to the current position in the current segment,
2655 in octets. */
2657 void
2658 symbol_set_value_now_octets (symbolS *sym)
2660 S_SET_SEGMENT (sym, now_seg);
2661 S_SET_VALUE (sym, frag_now_fix_octets ());
2662 symbol_set_frag (sym, frag_now);
2663 sym->sy_flags.sy_octets = 1;
2666 /* Set the frag of a symbol. */
2668 void
2669 symbol_set_frag (symbolS *s, fragS *f)
2671 if (LOCAL_SYMBOL_CHECK (s))
2673 local_symbol_set_frag ((struct local_symbol *) s, f);
2674 return;
2676 s->sy_frag = f;
2677 S_CLEAR_WEAKREFR (s);
2680 /* Return the frag of a symbol. */
2682 fragS *
2683 symbol_get_frag (symbolS *s)
2685 if (LOCAL_SYMBOL_CHECK (s))
2686 return local_symbol_get_frag ((struct local_symbol *) s);
2687 return s->sy_frag;
2690 /* Mark a symbol as having been used. */
2692 void
2693 symbol_mark_used (symbolS *s)
2695 if (LOCAL_SYMBOL_CHECK (s))
2696 return;
2697 s->sy_flags.sy_used = 1;
2698 if (S_IS_WEAKREFR (s))
2699 symbol_mark_used (s->sy_value.X_add_symbol);
2702 /* Clear the mark of whether a symbol has been used. */
2704 void
2705 symbol_clear_used (symbolS *s)
2707 if (LOCAL_SYMBOL_CHECK (s))
2708 s = local_symbol_convert ((struct local_symbol *) s);
2709 s->sy_flags.sy_used = 0;
2712 /* Return whether a symbol has been used. */
2715 symbol_used_p (symbolS *s)
2717 if (LOCAL_SYMBOL_CHECK (s))
2718 return 1;
2719 return s->sy_flags.sy_used;
2722 /* Mark a symbol as having been used in a reloc. */
2724 void
2725 symbol_mark_used_in_reloc (symbolS *s)
2727 if (LOCAL_SYMBOL_CHECK (s))
2728 s = local_symbol_convert ((struct local_symbol *) s);
2729 s->sy_flags.sy_used_in_reloc = 1;
2732 /* Clear the mark of whether a symbol has been used in a reloc. */
2734 void
2735 symbol_clear_used_in_reloc (symbolS *s)
2737 if (LOCAL_SYMBOL_CHECK (s))
2738 return;
2739 s->sy_flags.sy_used_in_reloc = 0;
2742 /* Return whether a symbol has been used in a reloc. */
2745 symbol_used_in_reloc_p (symbolS *s)
2747 if (LOCAL_SYMBOL_CHECK (s))
2748 return 0;
2749 return s->sy_flags.sy_used_in_reloc;
2752 /* Mark a symbol as an MRI common symbol. */
2754 void
2755 symbol_mark_mri_common (symbolS *s)
2757 if (LOCAL_SYMBOL_CHECK (s))
2758 s = local_symbol_convert ((struct local_symbol *) s);
2759 s->sy_flags.sy_mri_common = 1;
2762 /* Clear the mark of whether a symbol is an MRI common symbol. */
2764 void
2765 symbol_clear_mri_common (symbolS *s)
2767 if (LOCAL_SYMBOL_CHECK (s))
2768 return;
2769 s->sy_flags.sy_mri_common = 0;
2772 /* Return whether a symbol is an MRI common symbol. */
2775 symbol_mri_common_p (symbolS *s)
2777 if (LOCAL_SYMBOL_CHECK (s))
2778 return 0;
2779 return s->sy_flags.sy_mri_common;
2782 /* Mark a symbol as having been written. */
2784 void
2785 symbol_mark_written (symbolS *s)
2787 if (LOCAL_SYMBOL_CHECK (s))
2788 return;
2789 s->sy_flags.sy_written = 1;
2792 /* Clear the mark of whether a symbol has been written. */
2794 void
2795 symbol_clear_written (symbolS *s)
2797 if (LOCAL_SYMBOL_CHECK (s))
2798 return;
2799 s->sy_flags.sy_written = 0;
2802 /* Return whether a symbol has been written. */
2805 symbol_written_p (symbolS *s)
2807 if (LOCAL_SYMBOL_CHECK (s))
2808 return 0;
2809 return s->sy_flags.sy_written;
2812 /* Mark a symbol has having been resolved. */
2814 void
2815 symbol_mark_resolved (symbolS *s)
2817 if (LOCAL_SYMBOL_CHECK (s))
2819 local_symbol_mark_resolved ((struct local_symbol *) s);
2820 return;
2822 s->sy_flags.sy_resolved = 1;
2825 /* Return whether a symbol has been resolved. */
2828 symbol_resolved_p (symbolS *s)
2830 if (LOCAL_SYMBOL_CHECK (s))
2831 return local_symbol_resolved_p ((struct local_symbol *) s);
2832 return s->sy_flags.sy_resolved;
2835 /* Return whether a symbol is a section symbol. */
2838 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2840 if (LOCAL_SYMBOL_CHECK (s))
2841 return 0;
2842 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2845 /* Return whether a symbol is equated to another symbol. */
2848 symbol_equated_p (symbolS *s)
2850 if (LOCAL_SYMBOL_CHECK (s))
2851 return 0;
2852 return s->sy_value.X_op == O_symbol;
2855 /* Return whether a symbol is equated to another symbol, and should be
2856 treated specially when writing out relocs. */
2859 symbol_equated_reloc_p (symbolS *s)
2861 if (LOCAL_SYMBOL_CHECK (s))
2862 return 0;
2863 /* X_op_symbol, normally not used for O_symbol, is set by
2864 resolve_symbol_value to flag expression syms that have been
2865 equated. */
2866 return (s->sy_value.X_op == O_symbol
2867 #if defined (OBJ_COFF) && defined (TE_PE)
2868 && ! S_IS_WEAK (s)
2869 #endif
2870 && ((s->sy_flags.sy_resolved && s->sy_value.X_op_symbol != NULL)
2871 || ! S_IS_DEFINED (s)
2872 || S_IS_COMMON (s)));
2875 /* Return whether a symbol has a constant value. */
2878 symbol_constant_p (symbolS *s)
2880 if (LOCAL_SYMBOL_CHECK (s))
2881 return 1;
2882 return s->sy_value.X_op == O_constant;
2885 /* Return whether a symbol was cloned and thus removed from the global
2886 symbol list. */
2889 symbol_shadow_p (symbolS *s)
2891 if (LOCAL_SYMBOL_CHECK (s))
2892 return 0;
2893 return s->sy_next == s;
2896 /* If S was created as a struct symbol, return S, otherwise if S is a
2897 converted local_symbol return the converted symbol, otherwise
2898 return NULL. */
2900 symbolS *
2901 symbol_symbolS (symbolS *s)
2903 if (LOCAL_SYMBOL_CHECK (s))
2904 return NULL;
2905 return s;
2908 /* Return the BFD symbol for a symbol. */
2910 asymbol *
2911 symbol_get_bfdsym (symbolS *s)
2913 if (LOCAL_SYMBOL_CHECK (s))
2914 s = local_symbol_convert ((struct local_symbol *) s);
2915 return s->bsym;
2918 /* Set the BFD symbol for a symbol. */
2920 void
2921 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2923 if (LOCAL_SYMBOL_CHECK (s))
2924 s = local_symbol_convert ((struct local_symbol *) s);
2925 /* Usually, it is harmless to reset a symbol to a BFD section
2926 symbol. For example, obj_elf_change_section sets the BFD symbol
2927 of an old symbol with the newly created section symbol. But when
2928 we have multiple sections with the same name, the newly created
2929 section may have the same name as an old section. We check if the
2930 old symbol has been already marked as a section symbol before
2931 resetting it. */
2932 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2933 s->bsym = bsym;
2934 /* else XXX - What do we do now ? */
2937 /* Return whether symbol unit is "octets" (instead of "bytes"). */
2939 int symbol_octets_p (symbolS *s)
2941 return s->sy_flags.sy_octets;
2944 #ifdef OBJ_SYMFIELD_TYPE
2946 /* Get a pointer to the object format information for a symbol. */
2948 OBJ_SYMFIELD_TYPE *
2949 symbol_get_obj (symbolS *s)
2951 if (LOCAL_SYMBOL_CHECK (s))
2952 s = local_symbol_convert ((struct local_symbol *) s);
2953 return &s->sy_obj;
2956 /* Set the object format information for a symbol. */
2958 void
2959 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2961 if (LOCAL_SYMBOL_CHECK (s))
2962 s = local_symbol_convert ((struct local_symbol *) s);
2963 s->sy_obj = *o;
2966 #endif /* OBJ_SYMFIELD_TYPE */
2968 #ifdef TC_SYMFIELD_TYPE
2970 /* Get a pointer to the processor information for a symbol. */
2972 TC_SYMFIELD_TYPE *
2973 symbol_get_tc (symbolS *s)
2975 if (LOCAL_SYMBOL_CHECK (s))
2976 s = local_symbol_convert ((struct local_symbol *) s);
2977 return &s->sy_tc;
2980 /* Set the processor information for a symbol. */
2982 void
2983 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2985 if (LOCAL_SYMBOL_CHECK (s))
2986 s = local_symbol_convert ((struct local_symbol *) s);
2987 s->sy_tc = *o;
2990 #endif /* TC_SYMFIELD_TYPE */
2992 void
2993 symbol_begin (void)
2995 symbol_lastP = NULL;
2996 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2997 sy_hash = hash_new ();
2998 local_hash = hash_new ();
3000 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
3001 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3002 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3003 #endif
3004 abs_symbol.sy_value.X_op = O_constant;
3005 abs_symbol.sy_frag = &zero_address_frag;
3007 if (LOCAL_LABELS_FB)
3008 fb_label_init ();
3011 void
3012 dot_symbol_init (void)
3014 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3015 if (dot_symbol.bsym == NULL)
3016 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3017 dot_symbol.bsym->name = ".";
3018 dot_symbol.sy_flags.sy_forward_ref = 1;
3019 dot_symbol.sy_value.X_op = O_constant;
3022 int indent_level;
3024 /* Maximum indent level.
3025 Available for modification inside a gdb session. */
3026 static int max_indent_level = 8;
3028 void
3029 print_symbol_value_1 (FILE *file, symbolS *sym)
3031 const char *name = S_GET_NAME (sym);
3032 if (!name || !name[0])
3033 name = "(unnamed)";
3034 fprintf (file, "sym ");
3035 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
3036 fprintf (file, " %s", name);
3038 if (LOCAL_SYMBOL_CHECK (sym))
3040 struct local_symbol *locsym = (struct local_symbol *) sym;
3042 if (local_symbol_get_frag (locsym) != & zero_address_frag
3043 && local_symbol_get_frag (locsym) != NULL)
3045 fprintf (file, " frag ");
3046 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
3048 if (local_symbol_resolved_p (locsym))
3049 fprintf (file, " resolved");
3050 fprintf (file, " local");
3052 else
3054 if (sym->sy_frag != &zero_address_frag)
3056 fprintf (file, " frag ");
3057 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
3059 if (sym->sy_flags.sy_written)
3060 fprintf (file, " written");
3061 if (sym->sy_flags.sy_resolved)
3062 fprintf (file, " resolved");
3063 else if (sym->sy_flags.sy_resolving)
3064 fprintf (file, " resolving");
3065 if (sym->sy_flags.sy_used_in_reloc)
3066 fprintf (file, " used-in-reloc");
3067 if (sym->sy_flags.sy_used)
3068 fprintf (file, " used");
3069 if (S_IS_LOCAL (sym))
3070 fprintf (file, " local");
3071 if (S_IS_EXTERNAL (sym))
3072 fprintf (file, " extern");
3073 if (S_IS_WEAK (sym))
3074 fprintf (file, " weak");
3075 if (S_IS_DEBUG (sym))
3076 fprintf (file, " debug");
3077 if (S_IS_DEFINED (sym))
3078 fprintf (file, " defined");
3080 if (S_IS_WEAKREFR (sym))
3081 fprintf (file, " weakrefr");
3082 if (S_IS_WEAKREFD (sym))
3083 fprintf (file, " weakrefd");
3084 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3085 if (symbol_resolved_p (sym))
3087 segT s = S_GET_SEGMENT (sym);
3089 if (s != undefined_section
3090 && s != expr_section)
3091 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3093 else if (indent_level < max_indent_level
3094 && S_GET_SEGMENT (sym) != undefined_section)
3096 indent_level++;
3097 fprintf (file, "\n%*s<", indent_level * 4, "");
3098 if (LOCAL_SYMBOL_CHECK (sym))
3099 fprintf (file, "constant %lx",
3100 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
3101 else
3102 print_expr_1 (file, &sym->sy_value);
3103 fprintf (file, ">");
3104 indent_level--;
3106 fflush (file);
3109 void
3110 print_symbol_value (symbolS *sym)
3112 indent_level = 0;
3113 print_symbol_value_1 (stderr, sym);
3114 fprintf (stderr, "\n");
3117 static void
3118 print_binary (FILE *file, const char *name, expressionS *exp)
3120 indent_level++;
3121 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3122 print_symbol_value_1 (file, exp->X_add_symbol);
3123 fprintf (file, ">\n%*s<", indent_level * 4, "");
3124 print_symbol_value_1 (file, exp->X_op_symbol);
3125 fprintf (file, ">");
3126 indent_level--;
3129 void
3130 print_expr_1 (FILE *file, expressionS *exp)
3132 fprintf (file, "expr ");
3133 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
3134 fprintf (file, " ");
3135 switch (exp->X_op)
3137 case O_illegal:
3138 fprintf (file, "illegal");
3139 break;
3140 case O_absent:
3141 fprintf (file, "absent");
3142 break;
3143 case O_constant:
3144 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
3145 break;
3146 case O_symbol:
3147 indent_level++;
3148 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3149 print_symbol_value_1 (file, exp->X_add_symbol);
3150 fprintf (file, ">");
3151 maybe_print_addnum:
3152 if (exp->X_add_number)
3153 fprintf (file, "\n%*s%lx", indent_level * 4, "",
3154 (unsigned long) exp->X_add_number);
3155 indent_level--;
3156 break;
3157 case O_register:
3158 fprintf (file, "register #%d", (int) exp->X_add_number);
3159 break;
3160 case O_big:
3161 fprintf (file, "big");
3162 break;
3163 case O_uminus:
3164 fprintf (file, "uminus -<");
3165 indent_level++;
3166 print_symbol_value_1 (file, exp->X_add_symbol);
3167 fprintf (file, ">");
3168 goto maybe_print_addnum;
3169 case O_bit_not:
3170 fprintf (file, "bit_not");
3171 break;
3172 case O_multiply:
3173 print_binary (file, "multiply", exp);
3174 break;
3175 case O_divide:
3176 print_binary (file, "divide", exp);
3177 break;
3178 case O_modulus:
3179 print_binary (file, "modulus", exp);
3180 break;
3181 case O_left_shift:
3182 print_binary (file, "lshift", exp);
3183 break;
3184 case O_right_shift:
3185 print_binary (file, "rshift", exp);
3186 break;
3187 case O_bit_inclusive_or:
3188 print_binary (file, "bit_ior", exp);
3189 break;
3190 case O_bit_exclusive_or:
3191 print_binary (file, "bit_xor", exp);
3192 break;
3193 case O_bit_and:
3194 print_binary (file, "bit_and", exp);
3195 break;
3196 case O_eq:
3197 print_binary (file, "eq", exp);
3198 break;
3199 case O_ne:
3200 print_binary (file, "ne", exp);
3201 break;
3202 case O_lt:
3203 print_binary (file, "lt", exp);
3204 break;
3205 case O_le:
3206 print_binary (file, "le", exp);
3207 break;
3208 case O_ge:
3209 print_binary (file, "ge", exp);
3210 break;
3211 case O_gt:
3212 print_binary (file, "gt", exp);
3213 break;
3214 case O_logical_and:
3215 print_binary (file, "logical_and", exp);
3216 break;
3217 case O_logical_or:
3218 print_binary (file, "logical_or", exp);
3219 break;
3220 case O_add:
3221 indent_level++;
3222 fprintf (file, "add\n%*s<", indent_level * 4, "");
3223 print_symbol_value_1 (file, exp->X_add_symbol);
3224 fprintf (file, ">\n%*s<", indent_level * 4, "");
3225 print_symbol_value_1 (file, exp->X_op_symbol);
3226 fprintf (file, ">");
3227 goto maybe_print_addnum;
3228 case O_subtract:
3229 indent_level++;
3230 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3231 print_symbol_value_1 (file, exp->X_add_symbol);
3232 fprintf (file, ">\n%*s<", indent_level * 4, "");
3233 print_symbol_value_1 (file, exp->X_op_symbol);
3234 fprintf (file, ">");
3235 goto maybe_print_addnum;
3236 default:
3237 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3238 break;
3240 fflush (stdout);
3243 void
3244 print_expr (expressionS *exp)
3246 print_expr_1 (stderr, exp);
3247 fprintf (stderr, "\n");
3250 void
3251 symbol_print_statistics (FILE *file)
3253 hash_print_statistics (file, "symbol table", sy_hash);
3254 hash_print_statistics (file, "mini local symbol table", local_hash);
3255 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3256 local_symbol_count, local_symbol_conversion_count);
3259 #ifdef OBJ_COMPLEX_RELC
3261 /* Convert given symbol to a new complex-relocation symbol name. This
3262 may be a recursive function, since it might be called for non-leaf
3263 nodes (plain symbols) in the expression tree. The caller owns the
3264 returning string, so should free it eventually. Errors are
3265 indicated via as_bad and a NULL return value. The given symbol
3266 is marked with sy_used_in_reloc. */
3268 char *
3269 symbol_relc_make_sym (symbolS * sym)
3271 char * terminal = NULL;
3272 const char * sname;
3273 char typetag;
3274 int sname_len;
3276 gas_assert (sym != NULL);
3278 /* Recurse to symbol_relc_make_expr if this symbol
3279 is defined as an expression or a plain value. */
3280 if ( S_GET_SEGMENT (sym) == expr_section
3281 || S_GET_SEGMENT (sym) == absolute_section)
3282 return symbol_relc_make_expr (symbol_get_value_expression (sym));
3284 /* This may be a "fake symbol", referring to ".".
3285 Write out a special null symbol to refer to this position. */
3286 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3287 return xstrdup (".");
3289 /* We hope this is a plain leaf symbol. Construct the encoding
3290 as {S,s}II...:CCCCCCC....
3291 where 'S'/'s' means section symbol / plain symbol
3292 III is decimal for the symbol name length
3293 CCC is the symbol name itself. */
3294 symbol_mark_used_in_reloc (sym);
3296 sname = S_GET_NAME (sym);
3297 sname_len = strlen (sname);
3298 typetag = symbol_section_p (sym) ? 'S' : 's';
3300 terminal = XNEWVEC (char, (1 /* S or s */
3301 + 8 /* sname_len in decimal */
3302 + 1 /* _ spacer */
3303 + sname_len /* name itself */
3304 + 1 /* \0 */ ));
3306 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3307 return terminal;
3310 /* Convert given value to a new complex-relocation symbol name. This
3311 is a non-recursive function, since it is be called for leaf nodes
3312 (plain values) in the expression tree. The caller owns the
3313 returning string, so should free() it eventually. No errors. */
3315 char *
3316 symbol_relc_make_value (offsetT val)
3318 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3320 terminal[0] = '#';
3321 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3322 return terminal;
3325 /* Convert given expression to a new complex-relocation symbol name.
3326 This is a recursive function, since it traverses the entire given
3327 expression tree. The caller owns the returning string, so should
3328 free() it eventually. Errors are indicated via as_bad() and a NULL
3329 return value. */
3331 char *
3332 symbol_relc_make_expr (expressionS * exp)
3334 const char * opstr = NULL; /* Operator prefix string. */
3335 int arity = 0; /* Arity of this operator. */
3336 char * operands[3]; /* Up to three operands. */
3337 char * concat_string = NULL;
3339 operands[0] = operands[1] = operands[2] = NULL;
3341 gas_assert (exp != NULL);
3343 /* Match known operators -> fill in opstr, arity, operands[] and fall
3344 through to construct subexpression fragments; may instead return
3345 string directly for leaf nodes. */
3347 /* See expr.h for the meaning of all these enums. Many operators
3348 have an unnatural arity (X_add_number implicitly added). The
3349 conversion logic expands them to explicit "+" subexpressions. */
3351 switch (exp->X_op)
3353 default:
3354 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3355 break;
3357 /* Leaf nodes. */
3358 case O_constant:
3359 return symbol_relc_make_value (exp->X_add_number);
3361 case O_symbol:
3362 if (exp->X_add_number)
3364 arity = 2;
3365 opstr = "+";
3366 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3367 operands[1] = symbol_relc_make_value (exp->X_add_number);
3368 break;
3370 else
3371 return symbol_relc_make_sym (exp->X_add_symbol);
3373 /* Helper macros for nesting nodes. */
3375 #define HANDLE_XADD_OPT1(str_) \
3376 if (exp->X_add_number) \
3378 arity = 2; \
3379 opstr = "+:" str_; \
3380 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3381 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3382 break; \
3384 else \
3386 arity = 1; \
3387 opstr = str_; \
3388 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3390 break
3392 #define HANDLE_XADD_OPT2(str_) \
3393 if (exp->X_add_number) \
3395 arity = 3; \
3396 opstr = "+:" str_; \
3397 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3398 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3399 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3401 else \
3403 arity = 2; \
3404 opstr = str_; \
3405 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3406 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3408 break
3410 /* Nesting nodes. */
3412 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3413 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3414 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3415 case O_multiply: HANDLE_XADD_OPT2 ("*");
3416 case O_divide: HANDLE_XADD_OPT2 ("/");
3417 case O_modulus: HANDLE_XADD_OPT2 ("%");
3418 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3419 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3420 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3421 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3422 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3423 case O_add: HANDLE_XADD_OPT2 ("+");
3424 case O_subtract: HANDLE_XADD_OPT2 ("-");
3425 case O_eq: HANDLE_XADD_OPT2 ("==");
3426 case O_ne: HANDLE_XADD_OPT2 ("!=");
3427 case O_lt: HANDLE_XADD_OPT2 ("<");
3428 case O_le: HANDLE_XADD_OPT2 ("<=");
3429 case O_ge: HANDLE_XADD_OPT2 (">=");
3430 case O_gt: HANDLE_XADD_OPT2 (">");
3431 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3432 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3435 /* Validate & reject early. */
3436 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3437 opstr = NULL;
3438 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3439 opstr = NULL;
3440 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3441 opstr = NULL;
3443 if (opstr == NULL)
3444 concat_string = NULL;
3445 else if (arity == 0)
3446 concat_string = xstrdup (opstr);
3447 else if (arity == 1)
3448 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3449 else if (arity == 2)
3450 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3451 (char *) NULL);
3452 else
3453 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3454 operands[2], (char *) NULL);
3456 /* Free operand strings (not opstr). */
3457 if (arity >= 1) xfree (operands[0]);
3458 if (arity >= 2) xfree (operands[1]);
3459 if (arity >= 3) xfree (operands[2]);
3461 return concat_string;
3464 #endif