2000-08-29 Michael Snyder <msnyder@seadog.cygnus.com>
[binutils.git] / gas / symbols.c
bloba9dac42f3166463c5a398ce58fcef42c7ba3247c
1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24 #include <ctype.h>
26 #include "as.h"
28 #include "obstack.h" /* For "symbols.h" */
29 #include "subsegs.h"
31 #include "struc-symbol.h"
33 /* This is non-zero if symbols are case sensitive, which is the
34 default. */
35 int symbols_case_sensitive = 1;
37 #ifndef WORKING_DOT_WORD
38 extern int new_broken_words;
39 #endif
41 /* symbol-name => struct symbol pointer */
42 static struct hash_control *sy_hash;
44 /* Table of local symbols. */
45 static struct hash_control *local_hash;
47 /* Below are commented in "symbols.h". */
48 symbolS *symbol_rootP;
49 symbolS *symbol_lastP;
50 symbolS abs_symbol;
52 #ifdef DEBUG_SYMS
53 #define debug_verify_symchain verify_symbol_chain
54 #else
55 #define debug_verify_symchain(root, last) ((void) 0)
56 #endif
58 struct obstack notes;
60 static void fb_label_init PARAMS ((void));
61 static long dollar_label_instance PARAMS ((long));
62 static long fb_label_instance PARAMS ((long));
64 static void print_binary PARAMS ((FILE *, const char *, expressionS *));
66 /* Return a pointer to a new symbol. Die if we can't make a new
67 symbol. Fill in the symbol's values. Add symbol to end of symbol
68 chain.
70 This function should be called in the general case of creating a
71 symbol. However, if the output file symbol table has already been
72 set, and you are certain that this symbol won't be wanted in the
73 output file, you can call symbol_create. */
75 symbolS *
76 symbol_new (name, segment, valu, frag)
77 const char *name;
78 segT segment;
79 valueT valu;
80 fragS *frag;
82 symbolS *symbolP = symbol_create (name, segment, valu, frag);
84 /* Link to end of symbol chain. */
85 #ifdef BFD_ASSEMBLER
87 extern int symbol_table_frozen;
88 if (symbol_table_frozen)
89 abort ();
91 #endif
92 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
94 return symbolP;
97 /* Save a symbol name on a permanent obstack, and convert it according
98 to the object file format. */
100 static char *
101 save_symbol_name (name)
102 const char *name;
104 unsigned int name_length;
105 char *ret;
107 name_length = strlen (name) + 1; /* +1 for \0. */
108 obstack_grow (&notes, name, name_length);
109 ret = obstack_finish (&notes);
111 #ifdef STRIP_UNDERSCORE
112 if (ret[0] == '_')
113 ++ret;
114 #endif
116 #ifdef tc_canonicalize_symbol_name
117 ret = tc_canonicalize_symbol_name (ret);
118 #endif
120 if (! symbols_case_sensitive)
122 unsigned char *s;
124 for (s = (unsigned char *) ret; *s != '\0'; s++)
125 if (islower (*s))
126 *s = toupper (*s);
129 return ret;
132 symbolS *
133 symbol_create (name, segment, valu, frag)
134 const char *name; /* It is copied, the caller can destroy/modify. */
135 segT segment; /* Segment identifier (SEG_<something>). */
136 valueT valu; /* Symbol value. */
137 fragS *frag; /* Associated fragment. */
139 char *preserved_copy_of_name;
140 symbolS *symbolP;
142 preserved_copy_of_name = save_symbol_name (name);
144 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
146 /* symbol must be born in some fixed state. This seems as good as any. */
147 memset (symbolP, 0, sizeof (symbolS));
149 #ifdef BFD_ASSEMBLER
150 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
151 if (symbolP->bsym == NULL)
152 as_perror ("%s", "bfd_make_empty_symbol");
153 symbolP->bsym->udata.p = (PTR) symbolP;
154 #endif
155 S_SET_NAME (symbolP, preserved_copy_of_name);
157 S_SET_SEGMENT (symbolP, segment);
158 S_SET_VALUE (symbolP, valu);
159 symbol_clear_list_pointers (symbolP);
161 symbolP->sy_frag = frag;
162 #ifndef BFD_ASSEMBLER
163 symbolP->sy_number = ~0;
164 symbolP->sy_name_offset = (unsigned int) ~0;
165 #endif
167 obj_symbol_new_hook (symbolP);
169 #ifdef tc_symbol_new_hook
170 tc_symbol_new_hook (symbolP);
171 #endif
173 return symbolP;
176 #ifdef BFD_ASSEMBLER
178 /* Local symbol support. If we can get away with it, we keep only a
179 small amount of information for local symbols. */
181 static struct local_symbol *local_symbol_make PARAMS ((const char *, segT,
182 valueT, fragS *));
183 static symbolS *local_symbol_convert PARAMS ((struct local_symbol *));
185 /* Used for statistics. */
187 static unsigned long local_symbol_count;
188 static unsigned long local_symbol_conversion_count;
190 /* This macro is called with a symbol argument passed by reference.
191 It returns whether this is a local symbol. If necessary, it
192 changes its argument to the real symbol. */
194 #define LOCAL_SYMBOL_CHECK(s) \
195 (s->bsym == NULL \
196 ? (local_symbol_converted_p ((struct local_symbol *) s) \
197 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
198 0) \
199 : 1) \
200 : 0)
202 /* Create a local symbol and insert it into the local hash table. */
204 static struct local_symbol *
205 local_symbol_make (name, section, offset, frag)
206 const char *name;
207 segT section;
208 valueT offset;
209 fragS *frag;
211 char *name_copy;
212 struct local_symbol *ret;
214 ++local_symbol_count;
216 name_copy = save_symbol_name (name);
218 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
219 ret->lsy_marker = NULL;
220 ret->lsy_name = name_copy;
221 ret->lsy_section = section;
222 local_symbol_set_frag (ret, frag);
223 ret->lsy_offset = offset;
225 hash_jam (local_hash, name_copy, (PTR) ret);
227 return ret;
230 /* Convert a local symbol into a real symbol. Note that we do not
231 reclaim the space used by the local symbol. */
233 static symbolS *
234 local_symbol_convert (locsym)
235 struct local_symbol *locsym;
237 symbolS *ret;
239 assert (locsym->lsy_marker == NULL);
240 if (local_symbol_converted_p (locsym))
241 return local_symbol_get_real_symbol (locsym);
243 ++local_symbol_conversion_count;
245 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_offset,
246 local_symbol_get_frag (locsym));
248 if (local_symbol_resolved_p (locsym))
249 ret->sy_resolved = 1;
251 /* Local symbols are always either defined or used. */
252 ret->sy_used = 1;
254 symbol_table_insert (ret);
256 local_symbol_mark_converted (locsym);
257 local_symbol_set_real_symbol (locsym, ret);
259 hash_jam (local_hash, locsym->lsy_name, NULL);
261 return ret;
264 #else /* ! BFD_ASSEMBLER */
266 #define LOCAL_SYMBOL_CHECK(s) 0
267 #define local_symbol_convert(s) ((symbolS *) s)
269 #endif /* ! BFD_ASSEMBLER */
271 /* We have just seen "<name>:".
272 Creates a struct symbol unless it already exists.
274 Gripes if we are redefining a symbol incompatibly (and ignores it). */
276 symbolS *
277 colon (sym_name) /* Just seen "x:" - rattle symbols & frags. */
278 const char *sym_name; /* Symbol name, as a cannonical string. */
279 /* We copy this string: OK to alter later. */
281 register symbolS *symbolP; /* Symbol we are working with. */
283 /* Sun local labels go out of scope whenever a non-local symbol is
284 defined. */
285 if (LOCAL_LABELS_DOLLAR)
287 int local;
289 #ifdef BFD_ASSEMBLER
290 local = bfd_is_local_label_name (stdoutput, sym_name);
291 #else
292 local = LOCAL_LABEL (sym_name);
293 #endif
295 if (! local)
296 dollar_label_clear ();
299 #ifndef WORKING_DOT_WORD
300 if (new_broken_words)
302 struct broken_word *a;
303 int possible_bytes;
304 fragS *frag_tmp;
305 char *frag_opcode;
307 extern const int md_short_jump_size;
308 extern const int md_long_jump_size;
309 possible_bytes = (md_short_jump_size
310 + new_broken_words * md_long_jump_size);
312 frag_tmp = frag_now;
313 frag_opcode = frag_var (rs_broken_word,
314 possible_bytes,
315 possible_bytes,
316 (relax_substateT) 0,
317 (symbolS *) broken_words,
318 (offsetT) 0,
319 NULL);
321 /* We want to store the pointer to where to insert the jump
322 table in the fr_opcode of the rs_broken_word frag. This
323 requires a little hackery. */
324 while (frag_tmp
325 && (frag_tmp->fr_type != rs_broken_word
326 || frag_tmp->fr_opcode))
327 frag_tmp = frag_tmp->fr_next;
328 know (frag_tmp);
329 frag_tmp->fr_opcode = frag_opcode;
330 new_broken_words = 0;
332 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
333 a->dispfrag = frag_tmp;
335 #endif /* WORKING_DOT_WORD */
337 if ((symbolP = symbol_find (sym_name)) != 0)
339 #ifdef RESOLVE_SYMBOL_REDEFINITION
340 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
341 return symbolP;
342 #endif
343 /* Now check for undefined symbols. */
344 if (LOCAL_SYMBOL_CHECK (symbolP))
346 #ifdef BFD_ASSEMBLER
347 struct local_symbol *locsym = (struct local_symbol *) symbolP;
349 if (locsym->lsy_section != undefined_section
350 && (local_symbol_get_frag (locsym) != frag_now
351 || locsym->lsy_section != now_seg
352 || locsym->lsy_offset != frag_now_fix ()))
354 as_bad (_("Symbol %s already defined."), sym_name);
355 return symbolP;
358 locsym->lsy_section = now_seg;
359 local_symbol_set_frag (locsym, frag_now);
360 locsym->lsy_offset = frag_now_fix ();
361 #endif
363 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
365 if (S_GET_VALUE (symbolP) == 0)
367 symbolP->sy_frag = frag_now;
368 #ifdef OBJ_VMS
369 S_SET_OTHER (symbolP, const_flag);
370 #endif
371 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
372 S_SET_SEGMENT (symbolP, now_seg);
373 #ifdef N_UNDF
374 know (N_UNDF == 0);
375 #endif /* if we have one, it better be zero. */
378 else
380 /* There are still several cases to check:
382 A .comm/.lcomm symbol being redefined as initialized
383 data is OK
385 A .comm/.lcomm symbol being redefined with a larger
386 size is also OK
388 This only used to be allowed on VMS gas, but Sun cc
389 on the sparc also depends on it. */
391 if (((!S_IS_DEBUG (symbolP)
392 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
393 && S_IS_EXTERNAL (symbolP))
394 || S_GET_SEGMENT (symbolP) == bss_section)
395 && (now_seg == data_section
396 || now_seg == S_GET_SEGMENT (symbolP)))
398 /* Select which of the 2 cases this is. */
399 if (now_seg != data_section)
401 /* New .comm for prev .comm symbol.
403 If the new size is larger we just change its
404 value. If the new size is smaller, we ignore
405 this symbol. */
406 if (S_GET_VALUE (symbolP)
407 < ((unsigned) frag_now_fix ()))
409 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
412 else
414 /* It is a .comm/.lcomm being converted to initialized
415 data. */
416 symbolP->sy_frag = frag_now;
417 #ifdef OBJ_VMS
418 S_SET_OTHER (symbolP, const_flag);
419 #endif
420 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
421 S_SET_SEGMENT (symbolP, now_seg); /* Keep N_EXT bit. */
424 else
426 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
427 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
428 static const char *od_buf = "";
429 #else
430 char od_buf[100];
431 od_buf[0] = '\0';
432 #ifdef BFD_ASSEMBLER
433 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
434 #endif
435 sprintf(od_buf, "%d.%d.",
436 S_GET_OTHER (symbolP),
437 S_GET_DESC (symbolP));
438 #endif
439 as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%s%ld."),
440 sym_name,
441 segment_name (S_GET_SEGMENT (symbolP)),
442 od_buf,
443 (long) S_GET_VALUE (symbolP));
445 } /* if the undefined symbol has no value */
447 else
449 /* Don't blow up if the definition is the same. */
450 if (!(frag_now == symbolP->sy_frag
451 && S_GET_VALUE (symbolP) == frag_now_fix ()
452 && S_GET_SEGMENT (symbolP) == now_seg))
453 as_fatal (_("Symbol %s already defined."), sym_name);
454 } /* if this symbol is not yet defined */
457 #ifdef BFD_ASSEMBLER
458 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
460 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
461 (valueT) frag_now_fix (),
462 frag_now);
464 #endif /* BFD_ASSEMBLER */
465 else
467 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
468 frag_now);
469 #ifdef OBJ_VMS
470 S_SET_OTHER (symbolP, const_flag);
471 #endif /* OBJ_VMS */
473 symbol_table_insert (symbolP);
474 } /* if we have seen this symbol before */
476 if (mri_common_symbol != NULL)
478 /* This symbol is actually being defined within an MRI common
479 section. This requires special handling. */
480 if (LOCAL_SYMBOL_CHECK (symbolP))
481 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
482 symbolP->sy_value.X_op = O_symbol;
483 symbolP->sy_value.X_add_symbol = mri_common_symbol;
484 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
485 symbolP->sy_frag = &zero_address_frag;
486 S_SET_SEGMENT (symbolP, expr_section);
487 symbolP->sy_mri_common = 1;
490 #ifdef tc_frob_label
491 tc_frob_label (symbolP);
492 #endif
493 #ifdef obj_frob_label
494 obj_frob_label (symbolP);
495 #endif
497 return symbolP;
500 /* Die if we can't insert the symbol. */
502 void
503 symbol_table_insert (symbolP)
504 symbolS *symbolP;
506 register const char *error_string;
508 know (symbolP);
509 know (S_GET_NAME (symbolP));
511 if (LOCAL_SYMBOL_CHECK (symbolP))
513 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
514 (PTR) symbolP);
515 if (error_string != NULL)
516 as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
517 S_GET_NAME (symbolP), error_string);
518 return;
521 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
523 as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
524 S_GET_NAME (symbolP), error_string);
525 } /* on error */
528 /* If a symbol name does not exist, create it as undefined, and insert
529 it into the symbol table. Return a pointer to it. */
531 symbolS *
532 symbol_find_or_make (name)
533 const char *name;
535 register symbolS *symbolP;
537 symbolP = symbol_find (name);
539 if (symbolP == NULL)
541 #ifdef BFD_ASSEMBLER
542 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
544 symbolP = md_undefined_symbol ((char *) name);
545 if (symbolP != NULL)
546 return symbolP;
548 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
549 (valueT) 0,
550 &zero_address_frag);
551 return symbolP;
553 #endif
555 symbolP = symbol_make (name);
557 symbol_table_insert (symbolP);
558 } /* if symbol wasn't found */
560 return (symbolP);
563 symbolS *
564 symbol_make (name)
565 CONST char *name;
567 symbolS *symbolP;
569 /* Let the machine description default it, e.g. for register names. */
570 symbolP = md_undefined_symbol ((char *) name);
572 if (!symbolP)
573 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
575 return (symbolP);
578 /* Implement symbol table lookup.
579 In: A symbol's name as a string: '\0' can't be part of a symbol name.
580 Out: NULL if the name was not in the symbol table, else the address
581 of a struct symbol associated with that name. */
583 symbolS *
584 symbol_find (name)
585 CONST char *name;
587 #ifdef STRIP_UNDERSCORE
588 return (symbol_find_base (name, 1));
589 #else /* STRIP_UNDERSCORE */
590 return (symbol_find_base (name, 0));
591 #endif /* STRIP_UNDERSCORE */
594 symbolS *
595 symbol_find_base (name, strip_underscore)
596 CONST char *name;
597 int strip_underscore;
599 if (strip_underscore && *name == '_')
600 name++;
602 #ifdef tc_canonicalize_symbol_name
604 char *copy;
605 size_t len = strlen (name) + 1;
607 copy = (char *) alloca (len);
608 memcpy (copy, name, len);
609 name = tc_canonicalize_symbol_name (copy);
611 #endif
613 if (! symbols_case_sensitive)
615 char *copy;
616 const char *orig;
617 unsigned char c;
619 orig = name;
620 name = copy = (char *) alloca (strlen (name) + 1);
622 while ((c = *orig++) != '\0')
624 if (islower (c))
625 c = toupper (c);
626 *copy++ = c;
628 *copy = '\0';
631 #ifdef BFD_ASSEMBLER
633 struct local_symbol *locsym;
635 locsym = (struct local_symbol *) hash_find (local_hash, name);
636 if (locsym != NULL)
637 return (symbolS *) locsym;
639 #endif
641 return ((symbolS *) hash_find (sy_hash, name));
644 /* Once upon a time, symbols were kept in a singly linked list. At
645 least coff needs to be able to rearrange them from time to time, for
646 which a doubly linked list is much more convenient. Loic did these
647 as macros which seemed dangerous to me so they're now functions.
648 xoxorich. */
650 /* Link symbol ADDME after symbol TARGET in the chain. */
652 void
653 symbol_append (addme, target, rootPP, lastPP)
654 symbolS *addme;
655 symbolS *target;
656 symbolS **rootPP;
657 symbolS **lastPP;
659 if (LOCAL_SYMBOL_CHECK (addme))
660 abort ();
661 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
662 abort ();
664 if (target == NULL)
666 know (*rootPP == NULL);
667 know (*lastPP == NULL);
668 addme->sy_next = NULL;
669 #ifdef SYMBOLS_NEED_BACKPOINTERS
670 addme->sy_previous = NULL;
671 #endif
672 *rootPP = addme;
673 *lastPP = addme;
674 return;
675 } /* if the list is empty */
677 if (target->sy_next != NULL)
679 #ifdef SYMBOLS_NEED_BACKPOINTERS
680 target->sy_next->sy_previous = addme;
681 #endif /* SYMBOLS_NEED_BACKPOINTERS */
683 else
685 know (*lastPP == target);
686 *lastPP = addme;
687 } /* if we have a next */
689 addme->sy_next = target->sy_next;
690 target->sy_next = addme;
692 #ifdef SYMBOLS_NEED_BACKPOINTERS
693 addme->sy_previous = target;
694 #endif /* SYMBOLS_NEED_BACKPOINTERS */
696 debug_verify_symchain (symbol_rootP, symbol_lastP);
699 /* Set the chain pointers of SYMBOL to null. */
701 void
702 symbol_clear_list_pointers (symbolP)
703 symbolS *symbolP;
705 if (LOCAL_SYMBOL_CHECK (symbolP))
706 abort ();
707 symbolP->sy_next = NULL;
708 #ifdef SYMBOLS_NEED_BACKPOINTERS
709 symbolP->sy_previous = NULL;
710 #endif
713 #ifdef SYMBOLS_NEED_BACKPOINTERS
714 /* Remove SYMBOLP from the list. */
716 void
717 symbol_remove (symbolP, rootPP, lastPP)
718 symbolS *symbolP;
719 symbolS **rootPP;
720 symbolS **lastPP;
722 if (LOCAL_SYMBOL_CHECK (symbolP))
723 abort ();
725 if (symbolP == *rootPP)
727 *rootPP = symbolP->sy_next;
728 } /* if it was the root */
730 if (symbolP == *lastPP)
732 *lastPP = symbolP->sy_previous;
733 } /* if it was the tail */
735 if (symbolP->sy_next != NULL)
737 symbolP->sy_next->sy_previous = symbolP->sy_previous;
738 } /* if not last */
740 if (symbolP->sy_previous != NULL)
742 symbolP->sy_previous->sy_next = symbolP->sy_next;
743 } /* if not first */
745 debug_verify_symchain (*rootPP, *lastPP);
748 /* Link symbol ADDME before symbol TARGET in the chain. */
750 void
751 symbol_insert (addme, target, rootPP, lastPP)
752 symbolS *addme;
753 symbolS *target;
754 symbolS **rootPP;
755 symbolS **lastPP ATTRIBUTE_UNUSED;
757 if (LOCAL_SYMBOL_CHECK (addme))
758 abort ();
759 if (LOCAL_SYMBOL_CHECK (target))
760 abort ();
762 if (target->sy_previous != NULL)
764 target->sy_previous->sy_next = addme;
766 else
768 know (*rootPP == target);
769 *rootPP = addme;
770 } /* if not first */
772 addme->sy_previous = target->sy_previous;
773 target->sy_previous = addme;
774 addme->sy_next = target;
776 debug_verify_symchain (*rootPP, *lastPP);
779 #endif /* SYMBOLS_NEED_BACKPOINTERS */
781 void
782 verify_symbol_chain (rootP, lastP)
783 symbolS *rootP;
784 symbolS *lastP;
786 symbolS *symbolP = rootP;
788 if (symbolP == NULL)
789 return;
791 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
793 #ifdef BFD_ASSEMBLER
794 assert (symbolP->bsym != NULL);
795 #endif
796 #ifdef SYMBOLS_NEED_BACKPOINTERS
797 assert (symbolP->sy_next->sy_previous == symbolP);
798 #else
799 /* Walk the list anyways, to make sure pointers are still good. */
801 #endif /* SYMBOLS_NEED_BACKPOINTERS */
804 assert (lastP == symbolP);
807 void
808 verify_symbol_chain_2 (sym)
809 symbolS *sym;
811 symbolS *p = sym, *n = sym;
812 #ifdef SYMBOLS_NEED_BACKPOINTERS
813 while (symbol_previous (p))
814 p = symbol_previous (p);
815 #endif
816 while (symbol_next (n))
817 n = symbol_next (n);
818 verify_symbol_chain (p, n);
821 /* Resolve the value of a symbol. This is called during the final
822 pass over the symbol table to resolve any symbols with complex
823 values. */
825 valueT
826 resolve_symbol_value (symp, finalize)
827 symbolS *symp;
828 int finalize;
830 int resolved;
831 valueT final_val;
832 segT final_seg;
834 #ifdef BFD_ASSEMBLER
835 if (LOCAL_SYMBOL_CHECK (symp))
837 struct local_symbol *locsym = (struct local_symbol *) symp;
839 if (local_symbol_resolved_p (locsym))
840 return locsym->lsy_offset / bfd_octets_per_byte (stdoutput);
842 final_val = (local_symbol_get_frag (locsym)->fr_address
843 + locsym->lsy_offset) / bfd_octets_per_byte (stdoutput);
845 if (finalize)
847 locsym->lsy_offset = final_val;
848 local_symbol_mark_resolved (locsym);
851 return final_val;
853 #endif
855 if (symp->sy_resolved)
857 if (symp->sy_value.X_op == O_constant)
858 return (valueT) symp->sy_value.X_add_number;
859 else
860 return 0;
863 resolved = 0;
864 final_seg = S_GET_SEGMENT (symp);
866 if (symp->sy_resolving)
868 if (finalize)
869 as_bad (_("Symbol definition loop encountered at %s"),
870 S_GET_NAME (symp));
871 final_val = 0;
872 resolved = 1;
874 else
876 symbolS *add_symbol, *op_symbol;
877 offsetT left, right;
878 segT seg_left, seg_right;
879 operatorT op;
881 symp->sy_resolving = 1;
883 /* Help out with CSE. */
884 add_symbol = symp->sy_value.X_add_symbol;
885 op_symbol = symp->sy_value.X_op_symbol;
886 final_val = symp->sy_value.X_add_number;
887 op = symp->sy_value.X_op;
889 switch (op)
891 default:
892 BAD_CASE (op);
893 break;
895 case O_absent:
896 final_val = 0;
897 /* Fall through. */
899 case O_constant:
900 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
901 if (final_seg == expr_section)
902 final_seg = absolute_section;
903 resolved = 1;
904 break;
906 case O_symbol:
907 case O_symbol_rva:
908 left = resolve_symbol_value (add_symbol, finalize);
909 do_symbol:
911 if (symp->sy_mri_common)
913 /* This is a symbol inside an MRI common section. The
914 relocation routines are going to handle it specially.
915 Don't change the value. */
916 resolved = symbol_resolved_p (add_symbol);
917 break;
920 if (finalize && final_val == 0)
922 if (LOCAL_SYMBOL_CHECK (add_symbol))
923 add_symbol = local_symbol_convert ((struct local_symbol *)
924 add_symbol);
925 copy_symbol_attributes (symp, add_symbol);
928 /* If we have equated this symbol to an undefined symbol, we
929 keep X_op set to O_symbol, and we don't change
930 X_add_number. This permits the routine which writes out
931 relocation to detect this case, and convert the
932 relocation to be against the symbol to which this symbol
933 is equated. */
934 if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
936 if (finalize)
938 S_SET_SEGMENT (symp, S_GET_SEGMENT (add_symbol));
939 symp->sy_value.X_op = O_symbol;
940 symp->sy_value.X_add_symbol = add_symbol;
941 symp->sy_value.X_add_number = final_val;
943 final_val = 0;
944 resolved = symbol_resolved_p (add_symbol);
945 goto exit_dont_set_value;
947 else
949 final_val += symp->sy_frag->fr_address + left;
950 if (final_seg == expr_section || final_seg == undefined_section)
951 final_seg = S_GET_SEGMENT (add_symbol);
954 resolved = symbol_resolved_p (add_symbol);
955 break;
957 case O_uminus:
958 case O_bit_not:
959 case O_logical_not:
960 left = resolve_symbol_value (add_symbol, finalize);
962 if (op == O_uminus)
963 left = -left;
964 else if (op == O_logical_not)
965 left = !left;
966 else
967 left = ~left;
969 final_val += left + symp->sy_frag->fr_address;
970 if (final_seg == expr_section || final_seg == undefined_section)
971 final_seg = absolute_section;
973 resolved = symbol_resolved_p (add_symbol);
974 break;
976 case O_multiply:
977 case O_divide:
978 case O_modulus:
979 case O_left_shift:
980 case O_right_shift:
981 case O_bit_inclusive_or:
982 case O_bit_or_not:
983 case O_bit_exclusive_or:
984 case O_bit_and:
985 case O_add:
986 case O_subtract:
987 case O_eq:
988 case O_ne:
989 case O_lt:
990 case O_le:
991 case O_ge:
992 case O_gt:
993 case O_logical_and:
994 case O_logical_or:
995 left = resolve_symbol_value (add_symbol, finalize);
996 right = resolve_symbol_value (op_symbol, finalize);
997 seg_left = S_GET_SEGMENT (add_symbol);
998 seg_right = S_GET_SEGMENT (op_symbol);
1000 /* Simplify addition or subtraction of a constant by folding the
1001 constant into X_add_number. */
1002 if (op == O_add || op == O_subtract)
1004 if (seg_right == absolute_section)
1006 if (op == O_add)
1007 final_val += right;
1008 else
1009 final_val -= right;
1010 op = O_symbol;
1011 op_symbol = NULL;
1012 goto do_symbol;
1014 else if (seg_left == absolute_section && op == O_add)
1016 op = O_symbol;
1017 final_val += left;
1018 add_symbol = op_symbol;
1019 left = right;
1020 op_symbol = NULL;
1021 goto do_symbol;
1025 /* Subtraction is permitted if both operands are in the same
1026 section. Otherwise, both operands must be absolute. We
1027 already handled the case of addition or subtraction of a
1028 constant above. This will probably need to be changed
1029 for an object file format which supports arbitrary
1030 expressions, such as IEEE-695. */
1031 /* Don't emit messages unless we're finalizing the symbol value,
1032 otherwise we may get the same message multiple times. */
1033 if ((seg_left != absolute_section
1034 || seg_right != absolute_section)
1035 && (op != O_subtract
1036 || seg_left != seg_right
1037 || seg_left == undefined_section)
1038 && finalize)
1040 char *file;
1041 unsigned int line;
1043 if (expr_symbol_where (symp, &file, &line))
1045 if (seg_left == undefined_section)
1046 as_bad_where (file, line,
1047 _("undefined symbol %s in operation"),
1048 S_GET_NAME (symp->sy_value.X_add_symbol));
1049 if (seg_right == undefined_section)
1050 as_bad_where (file, line,
1051 _("undefined symbol %s in operation"),
1052 S_GET_NAME (symp->sy_value.X_op_symbol));
1053 if (seg_left != undefined_section
1054 && seg_right != undefined_section)
1055 as_bad_where (file, line,
1056 _("invalid section for operation"));
1058 else
1060 if (seg_left == undefined_section)
1061 as_bad (_("undefined symbol %s in operation setting %s"),
1062 S_GET_NAME (symp->sy_value.X_add_symbol),
1063 S_GET_NAME (symp));
1064 if (seg_right == undefined_section)
1065 as_bad (_("undefined symbol %s in operation setting %s"),
1066 S_GET_NAME (symp->sy_value.X_op_symbol),
1067 S_GET_NAME (symp));
1068 if (seg_left != undefined_section
1069 && seg_right != undefined_section)
1070 as_bad (_("invalid section for operation setting %s"),
1071 S_GET_NAME (symp));
1075 /* Check for division by zero. */
1076 if ((op == O_divide || op == O_modulus) && right == 0)
1078 /* If seg_right is not absolute_section, then we've
1079 already issued a warning about using a bad symbol. */
1080 if (seg_right == absolute_section && finalize)
1082 char *file;
1083 unsigned int line;
1085 if (expr_symbol_where (symp, &file, &line))
1086 as_bad_where (file, line, _("division by zero"));
1087 else
1088 as_bad (_("division by zero when setting %s"),
1089 S_GET_NAME (symp));
1092 right = 1;
1095 switch (symp->sy_value.X_op)
1097 case O_multiply: left *= right; break;
1098 case O_divide: left /= right; break;
1099 case O_modulus: left %= right; break;
1100 case O_left_shift: left <<= right; break;
1101 case O_right_shift: left >>= right; break;
1102 case O_bit_inclusive_or: left |= right; break;
1103 case O_bit_or_not: left |= ~right; break;
1104 case O_bit_exclusive_or: left ^= right; break;
1105 case O_bit_and: left &= right; break;
1106 case O_add: left += right; break;
1107 case O_subtract: left -= right; break;
1108 case O_eq: left = left == right ? ~ (offsetT) 0 : 0; break;
1109 case O_ne: left = left != right ? ~ (offsetT) 0 : 0; break;
1110 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1111 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1112 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1113 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1114 case O_logical_and: left = left && right; break;
1115 case O_logical_or: left = left || right; break;
1116 default: abort ();
1119 final_val += symp->sy_frag->fr_address + left;
1120 if (final_seg == expr_section || final_seg == undefined_section)
1121 final_seg = absolute_section;
1122 resolved = (symbol_resolved_p (add_symbol)
1123 && symbol_resolved_p (op_symbol));
1124 break;
1126 case O_register:
1127 case O_big:
1128 case O_illegal:
1129 /* Give an error (below) if not in expr_section. We don't
1130 want to worry about expr_section symbols, because they
1131 are fictional (they are created as part of expression
1132 resolution), and any problems may not actually mean
1133 anything. */
1134 break;
1137 symp->sy_resolving = 0;
1140 if (finalize)
1142 S_SET_VALUE (symp, final_val);
1144 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
1145 /* The old a.out backend does not handle S_SET_SEGMENT correctly
1146 for a stab symbol, so we use this bad hack. */
1147 if (final_seg != S_GET_SEGMENT (symp))
1148 #endif
1149 S_SET_SEGMENT (symp, final_seg);
1152 exit_dont_set_value:
1153 /* Don't worry if we can't resolve an expr_section symbol. */
1154 if (finalize)
1156 if (resolved)
1157 symp->sy_resolved = 1;
1158 else if (S_GET_SEGMENT (symp) != expr_section)
1160 as_bad (_("can't resolve value for symbol \"%s\""),
1161 S_GET_NAME (symp));
1162 symp->sy_resolved = 1;
1166 return final_val;
1169 #ifdef BFD_ASSEMBLER
1171 static void resolve_local_symbol PARAMS ((const char *, PTR));
1173 /* A static function passed to hash_traverse. */
1175 static void
1176 resolve_local_symbol (key, value)
1177 const char *key ATTRIBUTE_UNUSED;
1178 PTR value;
1180 if (value != NULL)
1181 resolve_symbol_value (value, 1);
1184 #endif
1186 /* Resolve all local symbols. */
1188 void
1189 resolve_local_symbol_values ()
1191 #ifdef BFD_ASSEMBLER
1192 hash_traverse (local_hash, resolve_local_symbol);
1193 #endif
1196 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1197 They are *really* local. That is, they go out of scope whenever we see a
1198 label that isn't local. Also, like fb labels, there can be multiple
1199 instances of a dollar label. Therefor, we name encode each instance with
1200 the instance number, keep a list of defined symbols separate from the real
1201 symbol table, and we treat these buggers as a sparse array. */
1203 static long *dollar_labels;
1204 static long *dollar_label_instances;
1205 static char *dollar_label_defines;
1206 static unsigned long dollar_label_count;
1207 static unsigned long dollar_label_max;
1210 dollar_label_defined (label)
1211 long label;
1213 long *i;
1215 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1217 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1218 if (*i == label)
1219 return dollar_label_defines[i - dollar_labels];
1221 /* If we get here, label isn't defined. */
1222 return 0;
1225 static long
1226 dollar_label_instance (label)
1227 long label;
1229 long *i;
1231 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1233 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1234 if (*i == label)
1235 return (dollar_label_instances[i - dollar_labels]);
1237 /* If we get here, we haven't seen the label before.
1238 Therefore its instance count is zero. */
1239 return 0;
1242 void
1243 dollar_label_clear ()
1245 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1248 #define DOLLAR_LABEL_BUMP_BY 10
1250 void
1251 define_dollar_label (label)
1252 long label;
1254 long *i;
1256 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1257 if (*i == label)
1259 ++dollar_label_instances[i - dollar_labels];
1260 dollar_label_defines[i - dollar_labels] = 1;
1261 return;
1264 /* If we get to here, we don't have label listed yet. */
1266 if (dollar_labels == NULL)
1268 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1269 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1270 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1271 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1272 dollar_label_count = 0;
1274 else if (dollar_label_count == dollar_label_max)
1276 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1277 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1278 dollar_label_max * sizeof (long));
1279 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1280 dollar_label_max * sizeof (long));
1281 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1282 } /* if we needed to grow */
1284 dollar_labels[dollar_label_count] = label;
1285 dollar_label_instances[dollar_label_count] = 1;
1286 dollar_label_defines[dollar_label_count] = 1;
1287 ++dollar_label_count;
1290 /* Caller must copy returned name: we re-use the area for the next name.
1292 The mth occurence of label n: is turned into the symbol "Ln^Am"
1293 where n is the label number and m is the instance number. "L" makes
1294 it a label discarded unless debugging and "^A"('\1') ensures no
1295 ordinary symbol SHOULD get the same name as a local label
1296 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1298 fb labels get the same treatment, except that ^B is used in place
1299 of ^A. */
1301 char * /* Return local label name. */
1302 dollar_label_name (n, augend)
1303 register long n; /* we just saw "n$:" : n a number. */
1304 register int augend; /* 0 for current instance, 1 for new instance. */
1306 long i;
1307 /* Returned to caller, then copied. Used for created names ("4f"). */
1308 static char symbol_name_build[24];
1309 register char *p;
1310 register char *q;
1311 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1313 know (n >= 0);
1314 know (augend == 0 || augend == 1);
1315 p = symbol_name_build;
1316 #ifdef LOCAL_LABEL_PREFIX
1317 *p++ = LOCAL_LABEL_PREFIX;
1318 #endif
1319 *p++ = 'L';
1321 /* Next code just does sprintf( {}, "%d", n); */
1322 /* Label number. */
1323 q = symbol_name_temporary;
1324 for (*q++ = 0, i = n; i; ++q)
1326 *q = i % 10 + '0';
1327 i /= 10;
1329 while ((*p = *--q) != '\0')
1330 ++p;
1332 *p++ = 1; /* ^A */
1334 /* Instance number. */
1335 q = symbol_name_temporary;
1336 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1338 *q = i % 10 + '0';
1339 i /= 10;
1341 while ((*p++ = *--q) != '\0');;
1343 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1344 return symbol_name_build;
1347 /* Sombody else's idea of local labels. They are made by "n:" where n
1348 is any decimal digit. Refer to them with
1349 "nb" for previous (backward) n:
1350 or "nf" for next (forward) n:.
1352 We do a little better and let n be any number, not just a single digit, but
1353 since the other guy's assembler only does ten, we treat the first ten
1354 specially.
1356 Like someone else's assembler, we have one set of local label counters for
1357 entire assembly, not one set per (sub)segment like in most assemblers. This
1358 implies that one can refer to a label in another segment, and indeed some
1359 crufty compilers have done just that.
1361 Since there could be a LOT of these things, treat them as a sparse
1362 array. */
1364 #define FB_LABEL_SPECIAL (10)
1366 static long fb_low_counter[FB_LABEL_SPECIAL];
1367 static long *fb_labels;
1368 static long *fb_label_instances;
1369 static long fb_label_count;
1370 static long fb_label_max;
1372 /* This must be more than FB_LABEL_SPECIAL. */
1373 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1375 static void
1376 fb_label_init ()
1378 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1381 /* Add one to the instance number of this fb label. */
1383 void
1384 fb_label_instance_inc (label)
1385 long label;
1387 long *i;
1389 if (label < FB_LABEL_SPECIAL)
1391 ++fb_low_counter[label];
1392 return;
1395 if (fb_labels != NULL)
1397 for (i = fb_labels + FB_LABEL_SPECIAL;
1398 i < fb_labels + fb_label_count; ++i)
1400 if (*i == label)
1402 ++fb_label_instances[i - fb_labels];
1403 return;
1404 } /* if we find it */
1405 } /* for each existing label */
1408 /* If we get to here, we don't have label listed yet. */
1410 if (fb_labels == NULL)
1412 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1413 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1414 fb_label_max = FB_LABEL_BUMP_BY;
1415 fb_label_count = FB_LABEL_SPECIAL;
1418 else if (fb_label_count == fb_label_max)
1420 fb_label_max += FB_LABEL_BUMP_BY;
1421 fb_labels = (long *) xrealloc ((char *) fb_labels,
1422 fb_label_max * sizeof (long));
1423 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1424 fb_label_max * sizeof (long));
1425 } /* if we needed to grow */
1427 fb_labels[fb_label_count] = label;
1428 fb_label_instances[fb_label_count] = 1;
1429 ++fb_label_count;
1432 static long
1433 fb_label_instance (label)
1434 long label;
1436 long *i;
1438 if (label < FB_LABEL_SPECIAL)
1440 return (fb_low_counter[label]);
1443 if (fb_labels != NULL)
1445 for (i = fb_labels + FB_LABEL_SPECIAL;
1446 i < fb_labels + fb_label_count; ++i)
1448 if (*i == label)
1450 return (fb_label_instances[i - fb_labels]);
1451 } /* if we find it */
1452 } /* for each existing label */
1455 /* We didn't find the label, so this must be a reference to the
1456 first instance. */
1457 return 0;
1460 /* Caller must copy returned name: we re-use the area for the next name.
1462 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1463 where n is the label number and m is the instance number. "L" makes
1464 it a label discarded unless debugging and "^B"('\2') ensures no
1465 ordinary symbol SHOULD get the same name as a local label
1466 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1468 dollar labels get the same treatment, except that ^A is used in
1469 place of ^B. */
1471 char * /* Return local label name. */
1472 fb_label_name (n, augend)
1473 long n; /* We just saw "n:", "nf" or "nb" : n a number. */
1474 long augend; /* 0 for nb, 1 for n:, nf. */
1476 long i;
1477 /* Returned to caller, then copied. Used for created names ("4f"). */
1478 static char symbol_name_build[24];
1479 register char *p;
1480 register char *q;
1481 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1483 know (n >= 0);
1484 know (augend == 0 || augend == 1);
1485 p = symbol_name_build;
1486 *p++ = 'L';
1488 /* Next code just does sprintf( {}, "%d", n); */
1489 /* Label number. */
1490 q = symbol_name_temporary;
1491 for (*q++ = 0, i = n; i; ++q)
1493 *q = i % 10 + '0';
1494 i /= 10;
1496 while ((*p = *--q) != '\0')
1497 ++p;
1499 *p++ = 2; /* ^B */
1501 /* Instance number. */
1502 q = symbol_name_temporary;
1503 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1505 *q = i % 10 + '0';
1506 i /= 10;
1508 while ((*p++ = *--q) != '\0');;
1510 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1511 return (symbol_name_build);
1514 /* Decode name that may have been generated by foo_label_name() above.
1515 If the name wasn't generated by foo_label_name(), then return it
1516 unaltered. This is used for error messages. */
1518 char *
1519 decode_local_label_name (s)
1520 char *s;
1522 char *p;
1523 char *symbol_decode;
1524 int label_number;
1525 int instance_number;
1526 char *type;
1527 const char *message_format = _("\"%d\" (instance number %d of a %s label)");
1529 if (s[0] != 'L')
1530 return s;
1532 for (label_number = 0, p = s + 1; isdigit ((unsigned char) *p); ++p)
1533 label_number = (10 * label_number) + *p - '0';
1535 if (*p == 1)
1536 type = "dollar";
1537 else if (*p == 2)
1538 type = "fb";
1539 else
1540 return s;
1542 for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1543 instance_number = (10 * instance_number) + *p - '0';
1545 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1546 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1548 return symbol_decode;
1551 /* Get the value of a symbol. */
1553 valueT
1554 S_GET_VALUE (s)
1555 symbolS *s;
1557 #ifdef BFD_ASSEMBLER
1558 if (LOCAL_SYMBOL_CHECK (s))
1559 return ((struct local_symbol *) s)->lsy_offset;
1560 #endif
1562 if (!s->sy_resolved && s->sy_value.X_op != O_constant)
1563 resolve_symbol_value (s, 1);
1564 if (s->sy_value.X_op != O_constant)
1566 static symbolS *recur;
1568 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1569 may call S_GET_VALUE. We use a static symbol to avoid the
1570 immediate recursion. */
1571 if (recur == s)
1572 return (valueT) s->sy_value.X_add_number;
1573 recur = s;
1574 if (! s->sy_resolved
1575 || s->sy_value.X_op != O_symbol
1576 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1577 as_bad (_("Attempt to get value of unresolved symbol %s"),
1578 S_GET_NAME (s));
1579 recur = NULL;
1581 return (valueT) s->sy_value.X_add_number;
1584 /* Set the value of a symbol. */
1586 void
1587 S_SET_VALUE (s, val)
1588 symbolS *s;
1589 valueT val;
1591 #ifdef BFD_ASSEMBLER
1592 if (LOCAL_SYMBOL_CHECK (s))
1594 ((struct local_symbol *) s)->lsy_offset = val;
1595 return;
1597 #endif
1599 s->sy_value.X_op = O_constant;
1600 s->sy_value.X_add_number = (offsetT) val;
1601 s->sy_value.X_unsigned = 0;
1604 void
1605 copy_symbol_attributes (dest, src)
1606 symbolS *dest, *src;
1608 if (LOCAL_SYMBOL_CHECK (dest))
1609 dest = local_symbol_convert ((struct local_symbol *) dest);
1610 if (LOCAL_SYMBOL_CHECK (src))
1611 src = local_symbol_convert ((struct local_symbol *) src);
1613 #ifdef BFD_ASSEMBLER
1614 /* In an expression, transfer the settings of these flags.
1615 The user can override later, of course. */
1616 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1617 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1618 #endif
1620 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1621 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1622 #endif
1625 #ifdef BFD_ASSEMBLER
1628 S_IS_FUNCTION (s)
1629 symbolS *s;
1631 flagword flags;
1633 if (LOCAL_SYMBOL_CHECK (s))
1634 return 0;
1636 flags = s->bsym->flags;
1638 return (flags & BSF_FUNCTION) != 0;
1642 S_IS_EXTERNAL (s)
1643 symbolS *s;
1645 flagword flags;
1647 if (LOCAL_SYMBOL_CHECK (s))
1648 return 0;
1650 flags = s->bsym->flags;
1652 /* Sanity check. */
1653 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1654 abort ();
1656 return (flags & BSF_GLOBAL) != 0;
1660 S_IS_WEAK (s)
1661 symbolS *s;
1663 if (LOCAL_SYMBOL_CHECK (s))
1664 return 0;
1665 return (s->bsym->flags & BSF_WEAK) != 0;
1669 S_IS_COMMON (s)
1670 symbolS *s;
1672 if (LOCAL_SYMBOL_CHECK (s))
1673 return 0;
1674 return bfd_is_com_section (s->bsym->section);
1678 S_IS_DEFINED (s)
1679 symbolS *s;
1681 if (LOCAL_SYMBOL_CHECK (s))
1682 return ((struct local_symbol *) s)->lsy_section != undefined_section;
1683 return s->bsym->section != undefined_section;
1687 S_IS_DEBUG (s)
1688 symbolS *s;
1690 if (LOCAL_SYMBOL_CHECK (s))
1691 return 0;
1692 if (s->bsym->flags & BSF_DEBUGGING)
1693 return 1;
1694 return 0;
1698 S_IS_LOCAL (s)
1699 symbolS *s;
1701 flagword flags;
1702 const char *name;
1704 if (LOCAL_SYMBOL_CHECK (s))
1705 return 1;
1707 flags = s->bsym->flags;
1709 /* Sanity check. */
1710 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1711 abort ();
1713 if (bfd_get_section (s->bsym) == reg_section)
1714 return 1;
1716 if (flag_strip_local_absolute
1717 && (flags & BSF_GLOBAL) == 0
1718 && bfd_get_section (s->bsym) == absolute_section)
1719 return 1;
1721 name = S_GET_NAME (s);
1722 return (name != NULL
1723 && ! S_IS_DEBUG (s)
1724 && (strchr (name, '\001')
1725 || strchr (name, '\002')
1726 || (! flag_keep_locals
1727 && (bfd_is_local_label (stdoutput, s->bsym)
1728 || (flag_mri
1729 && name[0] == '?'
1730 && name[1] == '?')))));
1734 S_IS_EXTERN (s)
1735 symbolS *s;
1737 return S_IS_EXTERNAL (s);
1741 S_IS_STABD (s)
1742 symbolS *s;
1744 return S_GET_NAME (s) == 0;
1747 CONST char *
1748 S_GET_NAME (s)
1749 symbolS *s;
1751 if (LOCAL_SYMBOL_CHECK (s))
1752 return ((struct local_symbol *) s)->lsy_name;
1753 return s->bsym->name;
1756 segT
1757 S_GET_SEGMENT (s)
1758 symbolS *s;
1760 if (LOCAL_SYMBOL_CHECK (s))
1761 return ((struct local_symbol *) s)->lsy_section;
1762 return s->bsym->section;
1765 void
1766 S_SET_SEGMENT (s, seg)
1767 symbolS *s;
1768 segT seg;
1770 /* Don't reassign section symbols. The direct reason is to prevent seg
1771 faults assigning back to const global symbols such as *ABS*, but it
1772 shouldn't happen anyway. */
1774 if (LOCAL_SYMBOL_CHECK (s))
1776 if (seg == reg_section)
1777 s = local_symbol_convert ((struct local_symbol *) s);
1778 else
1780 ((struct local_symbol *) s)->lsy_section = seg;
1781 return;
1785 if (s->bsym->flags & BSF_SECTION_SYM)
1787 if (s->bsym->section != seg)
1788 abort ();
1790 else
1791 s->bsym->section = seg;
1794 void
1795 S_SET_EXTERNAL (s)
1796 symbolS *s;
1798 if (LOCAL_SYMBOL_CHECK (s))
1799 s = local_symbol_convert ((struct local_symbol *) s);
1800 if ((s->bsym->flags & BSF_WEAK) != 0)
1802 /* Let .weak override .global. */
1803 return;
1805 s->bsym->flags |= BSF_GLOBAL;
1806 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
1809 void
1810 S_CLEAR_EXTERNAL (s)
1811 symbolS *s;
1813 if (LOCAL_SYMBOL_CHECK (s))
1814 return;
1815 if ((s->bsym->flags & BSF_WEAK) != 0)
1817 /* Let .weak override. */
1818 return;
1820 s->bsym->flags |= BSF_LOCAL;
1821 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
1824 void
1825 S_SET_WEAK (s)
1826 symbolS *s;
1828 if (LOCAL_SYMBOL_CHECK (s))
1829 s = local_symbol_convert ((struct local_symbol *) s);
1830 s->bsym->flags |= BSF_WEAK;
1831 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
1834 void
1835 S_SET_NAME (s, name)
1836 symbolS *s;
1837 char *name;
1839 if (LOCAL_SYMBOL_CHECK (s))
1841 ((struct local_symbol *) s)->lsy_name = name;
1842 return;
1844 s->bsym->name = name;
1846 #endif /* BFD_ASSEMBLER */
1848 #ifdef SYMBOLS_NEED_BACKPOINTERS
1850 /* Return the previous symbol in a chain. */
1852 symbolS *
1853 symbol_previous (s)
1854 symbolS *s;
1856 if (LOCAL_SYMBOL_CHECK (s))
1857 abort ();
1858 return s->sy_previous;
1861 #endif /* SYMBOLS_NEED_BACKPOINTERS */
1863 /* Return the next symbol in a chain. */
1865 symbolS *
1866 symbol_next (s)
1867 symbolS *s;
1869 if (LOCAL_SYMBOL_CHECK (s))
1870 abort ();
1871 return s->sy_next;
1874 /* Return a pointer to the value of a symbol as an expression. */
1876 expressionS *
1877 symbol_get_value_expression (s)
1878 symbolS *s;
1880 if (LOCAL_SYMBOL_CHECK (s))
1881 s = local_symbol_convert ((struct local_symbol *) s);
1882 return &s->sy_value;
1885 /* Set the value of a symbol to an expression. */
1887 void
1888 symbol_set_value_expression (s, exp)
1889 symbolS *s;
1890 const expressionS *exp;
1892 if (LOCAL_SYMBOL_CHECK (s))
1893 s = local_symbol_convert ((struct local_symbol *) s);
1894 s->sy_value = *exp;
1897 /* Set the frag of a symbol. */
1899 void
1900 symbol_set_frag (s, f)
1901 symbolS *s;
1902 fragS *f;
1904 #ifdef BFD_ASSEMBLER
1905 if (LOCAL_SYMBOL_CHECK (s))
1907 local_symbol_set_frag ((struct local_symbol *) s, f);
1908 return;
1910 #endif
1911 s->sy_frag = f;
1914 /* Return the frag of a symbol. */
1916 fragS *
1917 symbol_get_frag (s)
1918 symbolS *s;
1920 #ifdef BFD_ASSEMBLER
1921 if (LOCAL_SYMBOL_CHECK (s))
1922 return local_symbol_get_frag ((struct local_symbol *) s);
1923 #endif
1924 return s->sy_frag;
1927 /* Mark a symbol as having been used. */
1929 void
1930 symbol_mark_used (s)
1931 symbolS *s;
1933 if (LOCAL_SYMBOL_CHECK (s))
1934 return;
1935 s->sy_used = 1;
1938 /* Clear the mark of whether a symbol has been used. */
1940 void
1941 symbol_clear_used (s)
1942 symbolS *s;
1944 if (LOCAL_SYMBOL_CHECK (s))
1945 s = local_symbol_convert ((struct local_symbol *) s);
1946 s->sy_used = 0;
1949 /* Return whether a symbol has been used. */
1952 symbol_used_p (s)
1953 symbolS *s;
1955 if (LOCAL_SYMBOL_CHECK (s))
1956 return 1;
1957 return s->sy_used;
1960 /* Mark a symbol as having been used in a reloc. */
1962 void
1963 symbol_mark_used_in_reloc (s)
1964 symbolS *s;
1966 if (LOCAL_SYMBOL_CHECK (s))
1967 s = local_symbol_convert ((struct local_symbol *) s);
1968 s->sy_used_in_reloc = 1;
1971 /* Clear the mark of whether a symbol has been used in a reloc. */
1973 void
1974 symbol_clear_used_in_reloc (s)
1975 symbolS *s;
1977 if (LOCAL_SYMBOL_CHECK (s))
1978 return;
1979 s->sy_used_in_reloc = 0;
1982 /* Return whether a symbol has been used in a reloc. */
1985 symbol_used_in_reloc_p (s)
1986 symbolS *s;
1988 if (LOCAL_SYMBOL_CHECK (s))
1989 return 0;
1990 return s->sy_used_in_reloc;
1993 /* Mark a symbol as an MRI common symbol. */
1995 void
1996 symbol_mark_mri_common (s)
1997 symbolS *s;
1999 if (LOCAL_SYMBOL_CHECK (s))
2000 s = local_symbol_convert ((struct local_symbol *) s);
2001 s->sy_mri_common = 1;
2004 /* Clear the mark of whether a symbol is an MRI common symbol. */
2006 void
2007 symbol_clear_mri_common (s)
2008 symbolS *s;
2010 if (LOCAL_SYMBOL_CHECK (s))
2011 return;
2012 s->sy_mri_common = 0;
2015 /* Return whether a symbol is an MRI common symbol. */
2018 symbol_mri_common_p (s)
2019 symbolS *s;
2021 if (LOCAL_SYMBOL_CHECK (s))
2022 return 0;
2023 return s->sy_mri_common;
2026 /* Mark a symbol as having been written. */
2028 void
2029 symbol_mark_written (s)
2030 symbolS *s;
2032 if (LOCAL_SYMBOL_CHECK (s))
2033 return;
2034 s->written = 1;
2037 /* Clear the mark of whether a symbol has been written. */
2039 void
2040 symbol_clear_written (s)
2041 symbolS *s;
2043 if (LOCAL_SYMBOL_CHECK (s))
2044 return;
2045 s->written = 0;
2048 /* Return whether a symbol has been written. */
2051 symbol_written_p (s)
2052 symbolS *s;
2054 if (LOCAL_SYMBOL_CHECK (s))
2055 return 0;
2056 return s->written;
2059 /* Mark a symbol has having been resolved. */
2061 void
2062 symbol_mark_resolved (s)
2063 symbolS *s;
2065 #ifdef BFD_ASSEMBLER
2066 if (LOCAL_SYMBOL_CHECK (s))
2068 local_symbol_mark_resolved ((struct local_symbol *) s);
2069 return;
2071 #endif
2072 s->sy_resolved = 1;
2075 /* Return whether a symbol has been resolved. */
2078 symbol_resolved_p (s)
2079 symbolS *s;
2081 #ifdef BFD_ASSEMBLER
2082 if (LOCAL_SYMBOL_CHECK (s))
2083 return local_symbol_resolved_p ((struct local_symbol *) s);
2084 #endif
2085 return s->sy_resolved;
2088 /* Return whether a symbol is a section symbol. */
2091 symbol_section_p (s)
2092 symbolS *s ATTRIBUTE_UNUSED;
2094 if (LOCAL_SYMBOL_CHECK (s))
2095 return 0;
2096 #ifdef BFD_ASSEMBLER
2097 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2098 #else
2099 /* FIXME. */
2100 return 0;
2101 #endif
2104 /* Return whether a symbol is equated to another symbol. */
2107 symbol_equated_p (s)
2108 symbolS *s;
2110 if (LOCAL_SYMBOL_CHECK (s))
2111 return 0;
2112 return s->sy_value.X_op == O_symbol;
2115 /* Return whether a symbol has a constant value. */
2118 symbol_constant_p (s)
2119 symbolS *s;
2121 if (LOCAL_SYMBOL_CHECK (s))
2122 return 1;
2123 return s->sy_value.X_op == O_constant;
2126 #ifdef BFD_ASSEMBLER
2128 /* Return the BFD symbol for a symbol. */
2130 asymbol *
2131 symbol_get_bfdsym (s)
2132 symbolS *s;
2134 if (LOCAL_SYMBOL_CHECK (s))
2135 s = local_symbol_convert ((struct local_symbol *) s);
2136 return s->bsym;
2139 /* Set the BFD symbol for a symbol. */
2141 void
2142 symbol_set_bfdsym (s, bsym)
2143 symbolS *s;
2144 asymbol *bsym;
2146 if (LOCAL_SYMBOL_CHECK (s))
2147 s = local_symbol_convert ((struct local_symbol *) s);
2148 s->bsym = bsym;
2151 #endif /* BFD_ASSEMBLER */
2153 #ifdef OBJ_SYMFIELD_TYPE
2155 /* Get a pointer to the object format information for a symbol. */
2157 OBJ_SYMFIELD_TYPE *
2158 symbol_get_obj (s)
2159 symbolS *s;
2161 if (LOCAL_SYMBOL_CHECK (s))
2162 s = local_symbol_convert ((struct local_symbol *) s);
2163 return &s->sy_obj;
2166 /* Set the object format information for a symbol. */
2168 void
2169 symbol_set_obj (s, o)
2170 symbolS *s;
2171 OBJ_SYMFIELD_TYPE *o;
2173 if (LOCAL_SYMBOL_CHECK (s))
2174 s = local_symbol_convert ((struct local_symbol *) s);
2175 s->sy_obj = *o;
2178 #endif /* OBJ_SYMFIELD_TYPE */
2180 #ifdef TC_SYMFIELD_TYPE
2182 /* Get a pointer to the processor information for a symbol. */
2184 TC_SYMFIELD_TYPE *
2185 symbol_get_tc (s)
2186 symbolS *s;
2188 if (LOCAL_SYMBOL_CHECK (s))
2189 s = local_symbol_convert ((struct local_symbol *) s);
2190 return &s->sy_tc;
2193 /* Set the processor information for a symbol. */
2195 void
2196 symbol_set_tc (s, o)
2197 symbolS *s;
2198 TC_SYMFIELD_TYPE *o;
2200 if (LOCAL_SYMBOL_CHECK (s))
2201 s = local_symbol_convert ((struct local_symbol *) s);
2202 s->sy_tc = *o;
2205 #endif /* TC_SYMFIELD_TYPE */
2207 void
2208 symbol_begin ()
2210 symbol_lastP = NULL;
2211 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2212 sy_hash = hash_new ();
2213 #ifdef BFD_ASSEMBLER
2214 local_hash = hash_new ();
2215 #endif
2217 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2218 #ifdef BFD_ASSEMBLER
2219 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2220 abs_symbol.bsym = bfd_abs_section.symbol;
2221 #endif
2222 #else
2223 /* Can't initialise a union. Sigh. */
2224 S_SET_SEGMENT (&abs_symbol, absolute_section);
2225 #endif
2226 abs_symbol.sy_value.X_op = O_constant;
2227 abs_symbol.sy_frag = &zero_address_frag;
2229 if (LOCAL_LABELS_FB)
2230 fb_label_init ();
2233 int indent_level;
2235 /* Maximum indent level.
2236 Available for modification inside a gdb session. */
2237 int max_indent_level = 8;
2239 #if 0
2241 static void
2242 indent ()
2244 printf ("%*s", indent_level * 4, "");
2247 #endif
2249 void
2250 print_symbol_value_1 (file, sym)
2251 FILE *file;
2252 symbolS *sym;
2254 const char *name = S_GET_NAME (sym);
2255 if (!name || !name[0])
2256 name = "(unnamed)";
2257 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2259 if (LOCAL_SYMBOL_CHECK (sym))
2261 #ifdef BFD_ASSEMBLER
2262 struct local_symbol *locsym = (struct local_symbol *) sym;
2263 if (local_symbol_get_frag (locsym) != &zero_address_frag
2264 && local_symbol_get_frag (locsym) != NULL)
2265 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2266 if (local_symbol_resolved_p (locsym))
2267 fprintf (file, " resolved");
2268 fprintf (file, " local");
2269 #endif
2271 else
2273 if (sym->sy_frag != &zero_address_frag)
2274 fprintf (file, " frag %lx", (long) sym->sy_frag);
2275 if (sym->written)
2276 fprintf (file, " written");
2277 if (sym->sy_resolved)
2278 fprintf (file, " resolved");
2279 else if (sym->sy_resolving)
2280 fprintf (file, " resolving");
2281 if (sym->sy_used_in_reloc)
2282 fprintf (file, " used-in-reloc");
2283 if (sym->sy_used)
2284 fprintf (file, " used");
2285 if (S_IS_LOCAL (sym))
2286 fprintf (file, " local");
2287 if (S_IS_EXTERN (sym))
2288 fprintf (file, " extern");
2289 if (S_IS_DEBUG (sym))
2290 fprintf (file, " debug");
2291 if (S_IS_DEFINED (sym))
2292 fprintf (file, " defined");
2294 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2295 if (symbol_resolved_p (sym))
2297 segT s = S_GET_SEGMENT (sym);
2299 if (s != undefined_section
2300 && s != expr_section)
2301 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2303 else if (indent_level < max_indent_level
2304 && S_GET_SEGMENT (sym) != undefined_section)
2306 indent_level++;
2307 fprintf (file, "\n%*s<", indent_level * 4, "");
2308 #ifdef BFD_ASSEMBLER
2309 if (LOCAL_SYMBOL_CHECK (sym))
2310 fprintf (file, "constant %lx",
2311 (long) ((struct local_symbol *) sym)->lsy_offset);
2312 else
2313 #endif
2314 print_expr_1 (file, &sym->sy_value);
2315 fprintf (file, ">");
2316 indent_level--;
2318 fflush (file);
2321 void
2322 print_symbol_value (sym)
2323 symbolS *sym;
2325 indent_level = 0;
2326 print_symbol_value_1 (stderr, sym);
2327 fprintf (stderr, "\n");
2330 static void
2331 print_binary (file, name, exp)
2332 FILE *file;
2333 const char *name;
2334 expressionS *exp;
2336 indent_level++;
2337 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2338 print_symbol_value_1 (file, exp->X_add_symbol);
2339 fprintf (file, ">\n%*s<", indent_level * 4, "");
2340 print_symbol_value_1 (file, exp->X_op_symbol);
2341 fprintf (file, ">");
2342 indent_level--;
2345 void
2346 print_expr_1 (file, exp)
2347 FILE *file;
2348 expressionS *exp;
2350 fprintf (file, "expr %lx ", (long) exp);
2351 switch (exp->X_op)
2353 case O_illegal:
2354 fprintf (file, "illegal");
2355 break;
2356 case O_absent:
2357 fprintf (file, "absent");
2358 break;
2359 case O_constant:
2360 fprintf (file, "constant %lx", (long) exp->X_add_number);
2361 break;
2362 case O_symbol:
2363 indent_level++;
2364 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2365 print_symbol_value_1 (file, exp->X_add_symbol);
2366 fprintf (file, ">");
2367 maybe_print_addnum:
2368 if (exp->X_add_number)
2369 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2370 (long) exp->X_add_number);
2371 indent_level--;
2372 break;
2373 case O_register:
2374 fprintf (file, "register #%d", (int) exp->X_add_number);
2375 break;
2376 case O_big:
2377 fprintf (file, "big");
2378 break;
2379 case O_uminus:
2380 fprintf (file, "uminus -<");
2381 indent_level++;
2382 print_symbol_value_1 (file, exp->X_add_symbol);
2383 fprintf (file, ">");
2384 goto maybe_print_addnum;
2385 case O_bit_not:
2386 fprintf (file, "bit_not");
2387 break;
2388 case O_multiply:
2389 print_binary (file, "multiply", exp);
2390 break;
2391 case O_divide:
2392 print_binary (file, "divide", exp);
2393 break;
2394 case O_modulus:
2395 print_binary (file, "modulus", exp);
2396 break;
2397 case O_left_shift:
2398 print_binary (file, "lshift", exp);
2399 break;
2400 case O_right_shift:
2401 print_binary (file, "rshift", exp);
2402 break;
2403 case O_bit_inclusive_or:
2404 print_binary (file, "bit_ior", exp);
2405 break;
2406 case O_bit_exclusive_or:
2407 print_binary (file, "bit_xor", exp);
2408 break;
2409 case O_bit_and:
2410 print_binary (file, "bit_and", exp);
2411 break;
2412 case O_eq:
2413 print_binary (file, "eq", exp);
2414 break;
2415 case O_ne:
2416 print_binary (file, "ne", exp);
2417 break;
2418 case O_lt:
2419 print_binary (file, "lt", exp);
2420 break;
2421 case O_le:
2422 print_binary (file, "le", exp);
2423 break;
2424 case O_ge:
2425 print_binary (file, "ge", exp);
2426 break;
2427 case O_gt:
2428 print_binary (file, "gt", exp);
2429 break;
2430 case O_logical_and:
2431 print_binary (file, "logical_and", exp);
2432 break;
2433 case O_logical_or:
2434 print_binary (file, "logical_or", exp);
2435 break;
2436 case O_add:
2437 indent_level++;
2438 fprintf (file, "add\n%*s<", indent_level * 4, "");
2439 print_symbol_value_1 (file, exp->X_add_symbol);
2440 fprintf (file, ">\n%*s<", indent_level * 4, "");
2441 print_symbol_value_1 (file, exp->X_op_symbol);
2442 fprintf (file, ">");
2443 goto maybe_print_addnum;
2444 case O_subtract:
2445 indent_level++;
2446 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2447 print_symbol_value_1 (file, exp->X_add_symbol);
2448 fprintf (file, ">\n%*s<", indent_level * 4, "");
2449 print_symbol_value_1 (file, exp->X_op_symbol);
2450 fprintf (file, ">");
2451 goto maybe_print_addnum;
2452 default:
2453 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2454 break;
2456 fflush (stdout);
2459 void
2460 print_expr (exp)
2461 expressionS *exp;
2463 print_expr_1 (stderr, exp);
2464 fprintf (stderr, "\n");
2467 void
2468 symbol_print_statistics (file)
2469 FILE *file;
2471 hash_print_statistics (file, "symbol table", sy_hash);
2472 #ifdef BFD_ASSEMBLER
2473 hash_print_statistics (file, "mini local symbol table", local_hash);
2474 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2475 local_symbol_count, local_symbol_conversion_count);
2476 #endif