* reloc.c (_bfd_relocate_contents): Permit bitfield relocations to
[binutils.git] / gas / symbols.c
blobd47371ed67abbc7045db972f4368b5c503895a72
1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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 /* symbol_new()
68 Return a pointer to a new symbol. Die if we can't make a new
69 symbol. Fill in the symbol's values. Add symbol to end of symbol
70 chain.
72 This function should be called in the general case of creating a
73 symbol. However, if the output file symbol table has already been
74 set, and you are certain that this symbol won't be wanted in the
75 output file, you can call symbol_create. */
77 symbolS *
78 symbol_new (name, segment, valu, frag)
79 const char *name;
80 segT segment;
81 valueT valu;
82 fragS *frag;
84 symbolS *symbolP = symbol_create (name, segment, valu, frag);
87 * Link to end of symbol chain.
89 #ifdef BFD_ASSEMBLER
91 extern int symbol_table_frozen;
92 if (symbol_table_frozen)
93 abort ();
95 #endif
96 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
98 return symbolP;
101 /* Save a symbol name on a permanent obstack, and convert it according
102 to the object file format. */
104 static char *
105 save_symbol_name (name)
106 const char *name;
108 unsigned int name_length;
109 char *ret;
111 name_length = strlen (name) + 1; /* +1 for \0 */
112 obstack_grow (&notes, name, name_length);
113 ret = obstack_finish (&notes);
115 #ifdef STRIP_UNDERSCORE
116 if (ret[0] == '_')
117 ++ret;
118 #endif
120 #ifdef tc_canonicalize_symbol_name
121 ret = tc_canonicalize_symbol_name (ret);
122 #endif
124 if (! symbols_case_sensitive)
126 unsigned char *s;
128 for (s = (unsigned char *) ret; *s != '\0'; s++)
129 if (islower (*s))
130 *s = toupper (*s);
133 return ret;
136 symbolS *
137 symbol_create (name, segment, valu, frag)
138 const char *name; /* It is copied, the caller can destroy/modify */
139 segT segment; /* Segment identifier (SEG_<something>) */
140 valueT valu; /* Symbol value */
141 fragS *frag; /* Associated fragment */
143 char *preserved_copy_of_name;
144 symbolS *symbolP;
146 preserved_copy_of_name = save_symbol_name (name);
148 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
150 /* symbol must be born in some fixed state. This seems as good as any. */
151 memset (symbolP, 0, sizeof (symbolS));
153 #ifdef BFD_ASSEMBLER
154 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
155 if (symbolP->bsym == NULL)
156 as_perror ("%s", "bfd_make_empty_symbol");
157 symbolP->bsym->udata.p = (PTR) symbolP;
158 #endif
159 S_SET_NAME (symbolP, preserved_copy_of_name);
161 S_SET_SEGMENT (symbolP, segment);
162 S_SET_VALUE (symbolP, valu);
163 symbol_clear_list_pointers (symbolP);
165 symbolP->sy_frag = frag;
166 #ifndef BFD_ASSEMBLER
167 symbolP->sy_number = ~0;
168 symbolP->sy_name_offset = (unsigned int) ~0;
169 #endif
171 obj_symbol_new_hook (symbolP);
173 #ifdef tc_symbol_new_hook
174 tc_symbol_new_hook (symbolP);
175 #endif
177 return symbolP;
180 #ifdef BFD_ASSEMBLER
182 /* Local symbol support. If we can get away with it, we keep only a
183 small amount of information for local symbols. */
185 static struct local_symbol *local_symbol_make PARAMS ((const char *, segT,
186 valueT, fragS *));
187 static symbolS *local_symbol_convert PARAMS ((struct local_symbol *));
189 /* Used for statistics. */
191 static unsigned long local_symbol_count;
192 static unsigned long local_symbol_conversion_count;
194 /* This macro is called with a symbol argument passed by reference.
195 It returns whether this is a local symbol. If necessary, it
196 changes its argument to the real symbol. */
198 #define LOCAL_SYMBOL_CHECK(s) \
199 (s->bsym == NULL \
200 ? (local_symbol_converted_p ((struct local_symbol *) s) \
201 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
202 0) \
203 : 1) \
204 : 0)
206 /* Create a local symbol and insert it into the local hash table. */
208 static struct local_symbol *
209 local_symbol_make (name, section, offset, frag)
210 const char *name;
211 segT section;
212 valueT offset;
213 fragS *frag;
215 char *name_copy;
216 struct local_symbol *ret;
218 ++local_symbol_count;
220 name_copy = save_symbol_name (name);
222 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
223 ret->lsy_marker = NULL;
224 ret->lsy_name = name_copy;
225 ret->lsy_section = section;
226 local_symbol_set_frag (ret, frag);
227 ret->lsy_offset = offset;
229 hash_jam (local_hash, name_copy, (PTR) ret);
231 return ret;
234 /* Convert a local symbol into a real symbol. Note that we do not
235 reclaim the space used by the local symbol. */
237 static symbolS *
238 local_symbol_convert (locsym)
239 struct local_symbol *locsym;
241 symbolS *ret;
243 assert (locsym->lsy_marker == NULL);
244 if (local_symbol_converted_p (locsym))
245 return local_symbol_get_real_symbol (locsym);
247 ++local_symbol_conversion_count;
249 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_offset,
250 local_symbol_get_frag (locsym));
252 if (local_symbol_resolved_p (locsym))
253 ret->sy_resolved = 1;
255 /* Local symbols are always either defined or used. */
256 ret->sy_used = 1;
258 symbol_table_insert (ret);
260 local_symbol_mark_converted (locsym);
261 local_symbol_set_real_symbol (locsym, ret);
263 hash_jam (local_hash, locsym->lsy_name, NULL);
265 return ret;
268 #else /* ! BFD_ASSEMBLER */
270 #define LOCAL_SYMBOL_CHECK(s) 0
271 #define local_symbol_convert(s) ((symbolS *) s)
273 #endif /* ! BFD_ASSEMBLER */
277 * colon()
279 * We have just seen "<name>:".
280 * Creates a struct symbol unless it already exists.
282 * Gripes if we are redefining a symbol incompatibly (and ignores it).
285 symbolS *
286 colon (sym_name) /* just seen "x:" - rattle symbols & frags */
287 const char *sym_name; /* symbol name, as a cannonical string */
288 /* We copy this string: OK to alter later. */
290 register symbolS *symbolP; /* symbol we are working with */
292 /* Sun local labels go out of scope whenever a non-local symbol is
293 defined. */
294 if (LOCAL_LABELS_DOLLAR)
296 int local;
298 #ifdef BFD_ASSEMBLER
299 local = bfd_is_local_label_name (stdoutput, sym_name);
300 #else
301 local = LOCAL_LABEL (sym_name);
302 #endif
304 if (! local)
305 dollar_label_clear ();
308 #ifndef WORKING_DOT_WORD
309 if (new_broken_words)
311 struct broken_word *a;
312 int possible_bytes;
313 fragS *frag_tmp;
314 char *frag_opcode;
316 extern const int md_short_jump_size;
317 extern const int md_long_jump_size;
318 possible_bytes = (md_short_jump_size
319 + new_broken_words * md_long_jump_size);
321 frag_tmp = frag_now;
322 frag_opcode = frag_var (rs_broken_word,
323 possible_bytes,
324 possible_bytes,
325 (relax_substateT) 0,
326 (symbolS *) broken_words,
327 (offsetT) 0,
328 NULL);
330 /* We want to store the pointer to where to insert the jump table in the
331 fr_opcode of the rs_broken_word frag. This requires a little
332 hackery. */
333 while (frag_tmp
334 && (frag_tmp->fr_type != rs_broken_word
335 || frag_tmp->fr_opcode))
336 frag_tmp = frag_tmp->fr_next;
337 know (frag_tmp);
338 frag_tmp->fr_opcode = frag_opcode;
339 new_broken_words = 0;
341 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
342 a->dispfrag = frag_tmp;
344 #endif /* WORKING_DOT_WORD */
346 if ((symbolP = symbol_find (sym_name)) != 0)
348 #ifdef RESOLVE_SYMBOL_REDEFINITION
349 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
350 return symbolP;
351 #endif
353 * Now check for undefined symbols
355 if (LOCAL_SYMBOL_CHECK (symbolP))
357 struct local_symbol *locsym = (struct local_symbol *) symbolP;
359 if (locsym->lsy_section != undefined_section
360 && (local_symbol_get_frag (locsym) != frag_now
361 || locsym->lsy_section != now_seg
362 || locsym->lsy_offset != frag_now_fix ()))
364 as_bad (_("Symbol %s already defined."), sym_name);
365 return symbolP;
368 locsym->lsy_section = now_seg;
369 local_symbol_set_frag (locsym, frag_now);
370 locsym->lsy_offset = frag_now_fix ();
372 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
374 if (S_GET_VALUE (symbolP) == 0)
376 symbolP->sy_frag = frag_now;
377 #ifdef OBJ_VMS
378 S_SET_OTHER(symbolP, const_flag);
379 #endif
380 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
381 S_SET_SEGMENT (symbolP, now_seg);
382 #ifdef N_UNDF
383 know (N_UNDF == 0);
384 #endif /* if we have one, it better be zero. */
387 else
390 * There are still several cases to check:
391 * A .comm/.lcomm symbol being redefined as
392 * initialized data is OK
393 * A .comm/.lcomm symbol being redefined with
394 * a larger size is also OK
396 * This only used to be allowed on VMS gas, but Sun cc
397 * on the sparc also depends on it.
400 if (((!S_IS_DEBUG (symbolP)
401 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
402 && S_IS_EXTERNAL (symbolP))
403 || S_GET_SEGMENT (symbolP) == bss_section)
404 && (now_seg == data_section
405 || now_seg == S_GET_SEGMENT (symbolP)))
408 * Select which of the 2 cases this is
410 if (now_seg != data_section)
413 * New .comm for prev .comm symbol.
414 * If the new size is larger we just
415 * change its value. If the new size
416 * is smaller, we ignore this symbol
418 if (S_GET_VALUE (symbolP)
419 < ((unsigned) frag_now_fix ()))
421 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
424 else
426 /* It is a .comm/.lcomm being converted to initialized
427 data. */
428 symbolP->sy_frag = frag_now;
429 #ifdef OBJ_VMS
430 S_SET_OTHER(symbolP, const_flag);
431 #endif
432 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
433 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
436 else
438 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
439 as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld."),
440 sym_name,
441 segment_name (S_GET_SEGMENT (symbolP)),
442 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
443 (long) S_GET_VALUE (symbolP));
444 #else
445 as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%ld."),
446 sym_name,
447 segment_name (S_GET_SEGMENT (symbolP)),
448 (long) S_GET_VALUE (symbolP));
449 #endif
451 } /* if the undefined symbol has no value */
453 else
455 /* Don't blow up if the definition is the same */
456 if (!(frag_now == symbolP->sy_frag
457 && S_GET_VALUE (symbolP) == frag_now_fix ()
458 && S_GET_SEGMENT (symbolP) == now_seg))
459 as_fatal (_("Symbol %s already defined."), sym_name);
460 } /* if this symbol is not yet defined */
463 #ifdef BFD_ASSEMBLER
464 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
466 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
467 (valueT) frag_now_fix (),
468 frag_now);
470 #endif /* BFD_ASSEMBLER */
471 else
473 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
474 frag_now);
475 #ifdef OBJ_VMS
476 S_SET_OTHER (symbolP, const_flag);
477 #endif /* OBJ_VMS */
479 symbol_table_insert (symbolP);
480 } /* if we have seen this symbol before */
482 if (mri_common_symbol != NULL)
484 /* This symbol is actually being defined within an MRI common
485 section. This requires special handling. */
486 if (LOCAL_SYMBOL_CHECK (symbolP))
487 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
488 symbolP->sy_value.X_op = O_symbol;
489 symbolP->sy_value.X_add_symbol = mri_common_symbol;
490 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
491 symbolP->sy_frag = &zero_address_frag;
492 S_SET_SEGMENT (symbolP, expr_section);
493 symbolP->sy_mri_common = 1;
496 #ifdef tc_frob_label
497 tc_frob_label (symbolP);
498 #endif
499 #ifdef obj_frob_label
500 obj_frob_label (symbolP);
501 #endif
503 return symbolP;
508 * symbol_table_insert()
510 * Die if we can't insert the symbol.
514 void
515 symbol_table_insert (symbolP)
516 symbolS *symbolP;
518 register const char *error_string;
520 know (symbolP);
521 know (S_GET_NAME (symbolP));
523 if (LOCAL_SYMBOL_CHECK (symbolP))
525 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
526 (PTR) symbolP);
527 if (error_string != NULL)
528 as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
529 S_GET_NAME (symbolP), error_string);
530 return;
533 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
535 as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
536 S_GET_NAME (symbolP), error_string);
537 } /* on error */
538 } /* symbol_table_insert() */
541 * symbol_find_or_make()
543 * If a symbol name does not exist, create it as undefined, and insert
544 * it into the symbol table. Return a pointer to it.
546 symbolS *
547 symbol_find_or_make (name)
548 const char *name;
550 register symbolS *symbolP;
552 symbolP = symbol_find (name);
554 if (symbolP == NULL)
556 #ifdef BFD_ASSEMBLER
557 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
559 symbolP = md_undefined_symbol ((char *) name);
560 if (symbolP != NULL)
561 return symbolP;
563 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
564 (valueT) 0,
565 &zero_address_frag);
566 return symbolP;
568 #endif
570 symbolP = symbol_make (name);
572 symbol_table_insert (symbolP);
573 } /* if symbol wasn't found */
575 return (symbolP);
576 } /* symbol_find_or_make() */
578 symbolS *
579 symbol_make (name)
580 CONST char *name;
582 symbolS *symbolP;
584 /* Let the machine description default it, e.g. for register names. */
585 symbolP = md_undefined_symbol ((char *) name);
587 if (!symbolP)
588 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
590 return (symbolP);
591 } /* symbol_make() */
594 * symbol_find()
596 * Implement symbol table lookup.
597 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
598 * Out: NULL if the name was not in the symbol table, else the address
599 * of a struct symbol associated with that name.
602 symbolS *
603 symbol_find (name)
604 CONST char *name;
606 #ifdef STRIP_UNDERSCORE
607 return (symbol_find_base (name, 1));
608 #else /* STRIP_UNDERSCORE */
609 return (symbol_find_base (name, 0));
610 #endif /* STRIP_UNDERSCORE */
611 } /* symbol_find() */
613 symbolS *
614 symbol_find_base (name, strip_underscore)
615 CONST char *name;
616 int strip_underscore;
618 struct local_symbol *locsym;
620 if (strip_underscore && *name == '_')
621 name++;
623 #ifdef tc_canonicalize_symbol_name
625 char *copy;
626 size_t len = strlen (name) + 1;
628 copy = (char *) alloca (len);
629 memcpy (copy, name, len);
630 name = tc_canonicalize_symbol_name (copy);
632 #endif
634 if (! symbols_case_sensitive)
636 char *copy;
637 const char *orig;
638 unsigned char c;
640 orig = name;
641 name = copy = (char *) alloca (strlen (name) + 1);
643 while ((c = *orig++) != '\0')
645 if (islower (c))
646 c = toupper (c);
647 *copy++ = c;
649 *copy = '\0';
652 locsym = (struct local_symbol *) hash_find (local_hash, name);
653 if (locsym != NULL)
654 return (symbolS *) locsym;
656 return ((symbolS *) hash_find (sy_hash, name));
660 * Once upon a time, symbols were kept in a singly linked list. At
661 * least coff needs to be able to rearrange them from time to time, for
662 * which a doubly linked list is much more convenient. Loic did these
663 * as macros which seemed dangerous to me so they're now functions.
664 * xoxorich.
667 /* Link symbol ADDME after symbol TARGET in the chain. */
668 void
669 symbol_append (addme, target, rootPP, lastPP)
670 symbolS *addme;
671 symbolS *target;
672 symbolS **rootPP;
673 symbolS **lastPP;
675 if (LOCAL_SYMBOL_CHECK (addme))
676 abort ();
677 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
678 abort ();
680 if (target == NULL)
682 know (*rootPP == NULL);
683 know (*lastPP == NULL);
684 addme->sy_next = NULL;
685 #ifdef SYMBOLS_NEED_BACKPOINTERS
686 addme->sy_previous = NULL;
687 #endif
688 *rootPP = addme;
689 *lastPP = addme;
690 return;
691 } /* if the list is empty */
693 if (target->sy_next != NULL)
695 #ifdef SYMBOLS_NEED_BACKPOINTERS
696 target->sy_next->sy_previous = addme;
697 #endif /* SYMBOLS_NEED_BACKPOINTERS */
699 else
701 know (*lastPP == target);
702 *lastPP = addme;
703 } /* if we have a next */
705 addme->sy_next = target->sy_next;
706 target->sy_next = addme;
708 #ifdef SYMBOLS_NEED_BACKPOINTERS
709 addme->sy_previous = target;
710 #endif /* SYMBOLS_NEED_BACKPOINTERS */
712 debug_verify_symchain (symbol_rootP, symbol_lastP);
715 /* Set the chain pointers of SYMBOL to null. */
716 void
717 symbol_clear_list_pointers (symbolP)
718 symbolS *symbolP;
720 if (LOCAL_SYMBOL_CHECK (symbolP))
721 abort ();
722 symbolP->sy_next = NULL;
723 #ifdef SYMBOLS_NEED_BACKPOINTERS
724 symbolP->sy_previous = NULL;
725 #endif
728 #ifdef SYMBOLS_NEED_BACKPOINTERS
729 /* Remove SYMBOLP from the list. */
730 void
731 symbol_remove (symbolP, rootPP, lastPP)
732 symbolS *symbolP;
733 symbolS **rootPP;
734 symbolS **lastPP;
736 if (LOCAL_SYMBOL_CHECK (symbolP))
737 abort ();
739 if (symbolP == *rootPP)
741 *rootPP = symbolP->sy_next;
742 } /* if it was the root */
744 if (symbolP == *lastPP)
746 *lastPP = symbolP->sy_previous;
747 } /* if it was the tail */
749 if (symbolP->sy_next != NULL)
751 symbolP->sy_next->sy_previous = symbolP->sy_previous;
752 } /* if not last */
754 if (symbolP->sy_previous != NULL)
756 symbolP->sy_previous->sy_next = symbolP->sy_next;
757 } /* if not first */
759 debug_verify_symchain (*rootPP, *lastPP);
762 /* Link symbol ADDME before symbol TARGET in the chain. */
763 void
764 symbol_insert (addme, target, rootPP, lastPP)
765 symbolS *addme;
766 symbolS *target;
767 symbolS **rootPP;
768 symbolS **lastPP;
770 if (LOCAL_SYMBOL_CHECK (addme))
771 abort ();
772 if (LOCAL_SYMBOL_CHECK (target))
773 abort ();
775 if (target->sy_previous != NULL)
777 target->sy_previous->sy_next = addme;
779 else
781 know (*rootPP == target);
782 *rootPP = addme;
783 } /* if not first */
785 addme->sy_previous = target->sy_previous;
786 target->sy_previous = addme;
787 addme->sy_next = target;
789 debug_verify_symchain (*rootPP, *lastPP);
792 #endif /* SYMBOLS_NEED_BACKPOINTERS */
794 void
795 verify_symbol_chain (rootP, lastP)
796 symbolS *rootP;
797 symbolS *lastP;
799 symbolS *symbolP = rootP;
801 if (symbolP == NULL)
802 return;
804 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
806 #ifdef BFD_ASSEMBLER
807 assert (symbolP->bsym != NULL);
808 #endif
809 #ifdef SYMBOLS_NEED_BACKPOINTERS
810 assert (symbolP->sy_next->sy_previous == symbolP);
811 #else
812 /* Walk the list anyways, to make sure pointers are still good. */
814 #endif /* SYMBOLS_NEED_BACKPOINTERS */
817 assert (lastP == symbolP);
820 void
821 verify_symbol_chain_2 (sym)
822 symbolS *sym;
824 symbolS *p = sym, *n = sym;
825 #ifdef SYMBOLS_NEED_BACKPOINTERS
826 while (symbol_previous (p))
827 p = symbol_previous (p);
828 #endif
829 while (symbol_next (n))
830 n = symbol_next (n);
831 verify_symbol_chain (p, n);
834 /* Resolve the value of a symbol. This is called during the final
835 pass over the symbol table to resolve any symbols with complex
836 values. */
838 valueT
839 resolve_symbol_value (symp, finalize)
840 symbolS *symp;
841 int finalize;
843 int resolved;
844 valueT final_val;
845 segT final_seg;
847 if (LOCAL_SYMBOL_CHECK (symp))
849 struct local_symbol *locsym = (struct local_symbol *) symp;
851 if (local_symbol_resolved_p (locsym))
852 return locsym->lsy_offset;
854 final_val = (local_symbol_get_frag (locsym)->fr_address
855 + locsym->lsy_offset);
857 if (finalize)
859 locsym->lsy_offset = final_val;
860 local_symbol_mark_resolved (locsym);
863 return final_val;
866 if (symp->sy_resolved)
868 if (symp->sy_value.X_op == O_constant)
869 return (valueT) symp->sy_value.X_add_number;
870 else
871 return 0;
874 resolved = 0;
875 final_seg = S_GET_SEGMENT (symp);
877 if (symp->sy_resolving)
879 if (finalize)
880 as_bad (_("Symbol definition loop encountered at %s"), S_GET_NAME (symp));
881 final_val = 0;
882 resolved = 1;
884 else
886 symbolS *add_symbol, *op_symbol;
887 offsetT left, right;
888 segT seg_left, seg_right;
889 operatorT op;
891 symp->sy_resolving = 1;
893 /* Help out with CSE. */
894 add_symbol = symp->sy_value.X_add_symbol;
895 op_symbol = symp->sy_value.X_op_symbol;
896 final_val = symp->sy_value.X_add_number;
897 op = symp->sy_value.X_op;
899 switch (op)
901 default:
902 BAD_CASE (op);
903 break;
905 case O_absent:
906 final_val = 0;
907 /* Fall through. */
909 case O_constant:
910 final_val += symp->sy_frag->fr_address;
911 if (final_seg == expr_section)
912 final_seg = absolute_section;
913 resolved = 1;
914 break;
916 case O_symbol:
917 case O_symbol_rva:
918 left = resolve_symbol_value (add_symbol, finalize);
919 do_symbol:
921 if (symp->sy_mri_common)
923 /* This is a symbol inside an MRI common section. The
924 relocation routines are going to handle it specially.
925 Don't change the value. */
926 resolved = symbol_resolved_p (add_symbol);
927 break;
930 if (finalize && final_val == 0)
932 if (LOCAL_SYMBOL_CHECK (add_symbol))
933 add_symbol = local_symbol_convert ((struct local_symbol *)
934 add_symbol);
935 copy_symbol_attributes (symp, add_symbol);
938 /* If we have equated this symbol to an undefined symbol, we
939 keep X_op set to O_symbol, and we don't change
940 X_add_number. This permits the routine which writes out
941 relocation to detect this case, and convert the
942 relocation to be against the symbol to which this symbol
943 is equated. */
944 if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
946 if (finalize)
948 S_SET_SEGMENT (symp, S_GET_SEGMENT (add_symbol));
949 symp->sy_value.X_op = O_symbol;
950 symp->sy_value.X_add_symbol = add_symbol;
951 symp->sy_value.X_add_number = final_val;
953 final_val = 0;
954 resolved = symbol_resolved_p (add_symbol);
955 goto exit_dont_set_value;
957 else
959 final_val += symp->sy_frag->fr_address + left;
960 if (final_seg == expr_section || final_seg == undefined_section)
961 final_seg = S_GET_SEGMENT (add_symbol);
964 resolved = symbol_resolved_p (add_symbol);
965 break;
967 case O_uminus:
968 case O_bit_not:
969 case O_logical_not:
970 left = resolve_symbol_value (add_symbol, finalize);
972 if (op == O_uminus)
973 left = -left;
974 else if (op == O_logical_not)
975 left = !left;
976 else
977 left = ~left;
979 final_val += left + symp->sy_frag->fr_address;
980 if (final_seg == expr_section || final_seg == undefined_section)
981 final_seg = absolute_section;
983 resolved = symbol_resolved_p (add_symbol);
984 break;
986 case O_multiply:
987 case O_divide:
988 case O_modulus:
989 case O_left_shift:
990 case O_right_shift:
991 case O_bit_inclusive_or:
992 case O_bit_or_not:
993 case O_bit_exclusive_or:
994 case O_bit_and:
995 case O_add:
996 case O_subtract:
997 case O_eq:
998 case O_ne:
999 case O_lt:
1000 case O_le:
1001 case O_ge:
1002 case O_gt:
1003 case O_logical_and:
1004 case O_logical_or:
1005 left = resolve_symbol_value (add_symbol, finalize);
1006 right = resolve_symbol_value (op_symbol, finalize);
1007 seg_left = S_GET_SEGMENT (add_symbol);
1008 seg_right = S_GET_SEGMENT (op_symbol);
1010 /* Simplify addition or subtraction of a constant by folding the
1011 constant into X_add_number. */
1012 if (op == O_add || op == O_subtract)
1014 if (seg_right == absolute_section)
1016 if (op == O_add)
1017 final_val += right;
1018 else
1019 final_val -= right;
1020 op = O_symbol;
1021 op_symbol = NULL;
1022 goto do_symbol;
1024 else if (seg_left == absolute_section && op == O_add)
1026 op = O_symbol;
1027 final_val += left;
1028 add_symbol = op_symbol;
1029 left = right;
1030 op_symbol = NULL;
1031 goto do_symbol;
1035 /* Subtraction is permitted if both operands are in the same
1036 section. Otherwise, both operands must be absolute. We
1037 already handled the case of addition or subtraction of a
1038 constant above. This will probably need to be changed
1039 for an object file format which supports arbitrary
1040 expressions, such as IEEE-695. */
1041 /* Don't emit messages unless we're finalizing the symbol value,
1042 otherwise we may get the same message multiple times. */
1043 if ((seg_left != absolute_section
1044 || seg_right != absolute_section)
1045 && (op != O_subtract
1046 || seg_left != seg_right
1047 || seg_left == undefined_section)
1048 && finalize)
1050 char *file;
1051 unsigned int line;
1053 if (expr_symbol_where (symp, &file, &line))
1055 if (seg_left == undefined_section)
1056 as_bad_where (file, line,
1057 _("undefined symbol %s in operation"),
1058 S_GET_NAME (symp->sy_value.X_add_symbol));
1059 if (seg_right == undefined_section)
1060 as_bad_where (file, line,
1061 _("undefined symbol %s in operation"),
1062 S_GET_NAME (symp->sy_value.X_op_symbol));
1063 if (seg_left != undefined_section
1064 && seg_right != undefined_section)
1065 as_bad_where (file, line, _("invalid section for operation"));
1067 else
1069 if (seg_left == undefined_section)
1070 as_bad (_("undefined symbol %s in operation setting %s"),
1071 S_GET_NAME (symp->sy_value.X_add_symbol),
1072 S_GET_NAME (symp));
1073 if (seg_right == undefined_section)
1074 as_bad (_("undefined symbol %s in operation setting %s"),
1075 S_GET_NAME (symp->sy_value.X_op_symbol),
1076 S_GET_NAME (symp));
1077 if (seg_left != undefined_section
1078 && seg_right != undefined_section)
1079 as_bad (_("invalid section for operation setting %s"),
1080 S_GET_NAME (symp));
1084 /* Check for division by zero. */
1085 if ((op == O_divide || op == O_modulus) && right == 0)
1087 /* If seg_right is not absolute_section, then we've
1088 already issued a warning about using a bad symbol. */
1089 if (seg_right == absolute_section && finalize)
1091 char *file;
1092 unsigned int line;
1094 if (expr_symbol_where (symp, &file, &line))
1095 as_bad_where (file, line, _("division by zero"));
1096 else
1097 as_bad (_("division by zero when setting %s"),
1098 S_GET_NAME (symp));
1101 right = 1;
1104 switch (symp->sy_value.X_op)
1106 case O_multiply: left *= right; break;
1107 case O_divide: left /= right; break;
1108 case O_modulus: left %= right; break;
1109 case O_left_shift: left <<= right; break;
1110 case O_right_shift: left >>= right; break;
1111 case O_bit_inclusive_or: left |= right; break;
1112 case O_bit_or_not: left |= ~right; break;
1113 case O_bit_exclusive_or: left ^= right; break;
1114 case O_bit_and: left &= right; break;
1115 case O_add: left += right; break;
1116 case O_subtract: left -= right; break;
1117 case O_eq: left = left == right ? ~ (offsetT) 0 : 0; break;
1118 case O_ne: left = left != right ? ~ (offsetT) 0 : 0; break;
1119 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1120 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1121 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1122 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1123 case O_logical_and: left = left && right; break;
1124 case O_logical_or: left = left || right; break;
1125 default: abort ();
1128 final_val += symp->sy_frag->fr_address + left;
1129 if (final_seg == expr_section || final_seg == undefined_section)
1130 final_seg = absolute_section;
1131 resolved = (symbol_resolved_p (add_symbol)
1132 && symbol_resolved_p (op_symbol));
1133 break;
1135 case O_register:
1136 case O_big:
1137 case O_illegal:
1138 /* Give an error (below) if not in expr_section. We don't
1139 want to worry about expr_section symbols, because they
1140 are fictional (they are created as part of expression
1141 resolution), and any problems may not actually mean
1142 anything. */
1143 break;
1146 symp->sy_resolving = 0;
1149 if (finalize)
1151 S_SET_VALUE (symp, final_val);
1153 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
1154 /* The old a.out backend does not handle S_SET_SEGMENT correctly
1155 for a stab symbol, so we use this bad hack. */
1156 if (final_seg != S_GET_SEGMENT (symp))
1157 #endif
1158 S_SET_SEGMENT (symp, final_seg);
1161 exit_dont_set_value:
1162 /* Don't worry if we can't resolve an expr_section symbol. */
1163 if (finalize)
1165 if (resolved)
1166 symp->sy_resolved = 1;
1167 else if (S_GET_SEGMENT (symp) != expr_section)
1169 as_bad (_("can't resolve value for symbol \"%s\""), S_GET_NAME (symp));
1170 symp->sy_resolved = 1;
1174 return final_val;
1177 #ifdef BFD_ASSEMBLER
1179 static void resolve_local_symbol PARAMS ((const char *, PTR));
1181 /* A static function passed to hash_traverse. */
1183 static void
1184 resolve_local_symbol (key, value)
1185 const char *key;
1186 PTR value;
1188 if (value != NULL)
1189 resolve_symbol_value (value, 1);
1192 #endif
1194 /* Resolve all local symbols. */
1196 void
1197 resolve_local_symbol_values ()
1199 #ifdef BFD_ASSEMBLER
1200 hash_traverse (local_hash, resolve_local_symbol);
1201 #endif
1204 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1205 They are *really* local. That is, they go out of scope whenever we see a
1206 label that isn't local. Also, like fb labels, there can be multiple
1207 instances of a dollar label. Therefor, we name encode each instance with
1208 the instance number, keep a list of defined symbols separate from the real
1209 symbol table, and we treat these buggers as a sparse array. */
1211 static long *dollar_labels;
1212 static long *dollar_label_instances;
1213 static char *dollar_label_defines;
1214 static unsigned long dollar_label_count;
1215 static unsigned long dollar_label_max;
1217 int
1218 dollar_label_defined (label)
1219 long label;
1221 long *i;
1223 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1225 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1226 if (*i == label)
1227 return dollar_label_defines[i - dollar_labels];
1229 /* if we get here, label isn't defined */
1230 return 0;
1231 } /* dollar_label_defined() */
1233 static long
1234 dollar_label_instance (label)
1235 long label;
1237 long *i;
1239 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1241 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1242 if (*i == label)
1243 return (dollar_label_instances[i - dollar_labels]);
1245 /* If we get here, we haven't seen the label before, therefore its instance
1246 count is zero. */
1247 return 0;
1250 void
1251 dollar_label_clear ()
1253 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1256 #define DOLLAR_LABEL_BUMP_BY 10
1258 void
1259 define_dollar_label (label)
1260 long label;
1262 long *i;
1264 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1265 if (*i == label)
1267 ++dollar_label_instances[i - dollar_labels];
1268 dollar_label_defines[i - dollar_labels] = 1;
1269 return;
1272 /* if we get to here, we don't have label listed yet. */
1274 if (dollar_labels == NULL)
1276 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1277 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1278 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1279 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1280 dollar_label_count = 0;
1282 else if (dollar_label_count == dollar_label_max)
1284 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1285 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1286 dollar_label_max * sizeof (long));
1287 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1288 dollar_label_max * sizeof (long));
1289 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1290 } /* if we needed to grow */
1292 dollar_labels[dollar_label_count] = label;
1293 dollar_label_instances[dollar_label_count] = 1;
1294 dollar_label_defines[dollar_label_count] = 1;
1295 ++dollar_label_count;
1299 * dollar_label_name()
1301 * Caller must copy returned name: we re-use the area for the next name.
1303 * The mth occurence of label n: is turned into the symbol "Ln^Am"
1304 * where n is the label number and m is the instance number. "L" makes
1305 * it a label discarded unless debugging and "^A"('\1') ensures no
1306 * ordinary symbol SHOULD get the same name as a local label
1307 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1309 * fb labels get the same treatment, except that ^B is used in place of ^A.
1312 char * /* Return local label name. */
1313 dollar_label_name (n, augend)
1314 register long n; /* we just saw "n$:" : n a number */
1315 register int augend; /* 0 for current instance, 1 for new instance */
1317 long i;
1318 /* Returned to caller, then copied. used for created names ("4f") */
1319 static char symbol_name_build[24];
1320 register char *p;
1321 register char *q;
1322 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1324 know (n >= 0);
1325 know (augend == 0 || augend == 1);
1326 p = symbol_name_build;
1327 *p++ = 'L';
1329 /* Next code just does sprintf( {}, "%d", n); */
1330 /* label number */
1331 q = symbol_name_temporary;
1332 for (*q++ = 0, i = n; i; ++q)
1334 *q = i % 10 + '0';
1335 i /= 10;
1337 while ((*p = *--q) != '\0')
1338 ++p;
1340 *p++ = 1; /* ^A */
1342 /* instance number */
1343 q = symbol_name_temporary;
1344 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1346 *q = i % 10 + '0';
1347 i /= 10;
1349 while ((*p++ = *--q) != '\0');;
1351 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1352 return symbol_name_build;
1356 * Sombody else's idea of local labels. They are made by "n:" where n
1357 * is any decimal digit. Refer to them with
1358 * "nb" for previous (backward) n:
1359 * or "nf" for next (forward) n:.
1361 * We do a little better and let n be any number, not just a single digit, but
1362 * since the other guy's assembler only does ten, we treat the first ten
1363 * specially.
1365 * Like someone else's assembler, we have one set of local label counters for
1366 * entire assembly, not one set per (sub)segment like in most assemblers. This
1367 * implies that one can refer to a label in another segment, and indeed some
1368 * crufty compilers have done just that.
1370 * Since there could be a LOT of these things, treat them as a sparse array.
1373 #define FB_LABEL_SPECIAL (10)
1375 static long fb_low_counter[FB_LABEL_SPECIAL];
1376 static long *fb_labels;
1377 static long *fb_label_instances;
1378 static long fb_label_count;
1379 static long fb_label_max;
1381 /* this must be more than FB_LABEL_SPECIAL */
1382 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1384 static void
1385 fb_label_init ()
1387 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1388 } /* fb_label_init() */
1390 /* add one to the instance number of this fb label */
1391 void
1392 fb_label_instance_inc (label)
1393 long label;
1395 long *i;
1397 if (label < FB_LABEL_SPECIAL)
1399 ++fb_low_counter[label];
1400 return;
1403 if (fb_labels != NULL)
1405 for (i = fb_labels + FB_LABEL_SPECIAL;
1406 i < fb_labels + fb_label_count; ++i)
1408 if (*i == label)
1410 ++fb_label_instances[i - fb_labels];
1411 return;
1412 } /* if we find it */
1413 } /* for each existing label */
1416 /* if we get to here, we don't have label listed yet. */
1418 if (fb_labels == NULL)
1420 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1421 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1422 fb_label_max = FB_LABEL_BUMP_BY;
1423 fb_label_count = FB_LABEL_SPECIAL;
1426 else if (fb_label_count == fb_label_max)
1428 fb_label_max += FB_LABEL_BUMP_BY;
1429 fb_labels = (long *) xrealloc ((char *) fb_labels,
1430 fb_label_max * sizeof (long));
1431 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1432 fb_label_max * sizeof (long));
1433 } /* if we needed to grow */
1435 fb_labels[fb_label_count] = label;
1436 fb_label_instances[fb_label_count] = 1;
1437 ++fb_label_count;
1440 static long
1441 fb_label_instance (label)
1442 long label;
1444 long *i;
1446 if (label < FB_LABEL_SPECIAL)
1448 return (fb_low_counter[label]);
1451 if (fb_labels != NULL)
1453 for (i = fb_labels + FB_LABEL_SPECIAL;
1454 i < fb_labels + fb_label_count; ++i)
1456 if (*i == label)
1458 return (fb_label_instances[i - fb_labels]);
1459 } /* if we find it */
1460 } /* for each existing label */
1463 /* We didn't find the label, so this must be a reference to the
1464 first instance. */
1465 return 0;
1469 * fb_label_name()
1471 * Caller must copy returned name: we re-use the area for the next name.
1473 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1474 * where n is the label number and m is the instance number. "L" makes
1475 * it a label discarded unless debugging and "^B"('\2') ensures no
1476 * ordinary symbol SHOULD get the same name as a local label
1477 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1479 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1481 char * /* Return local label name. */
1482 fb_label_name (n, augend)
1483 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1484 long augend; /* 0 for nb, 1 for n:, nf */
1486 long i;
1487 /* Returned to caller, then copied. used for created names ("4f") */
1488 static char symbol_name_build[24];
1489 register char *p;
1490 register char *q;
1491 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1493 know (n >= 0);
1494 know (augend == 0 || augend == 1);
1495 p = symbol_name_build;
1496 *p++ = 'L';
1498 /* Next code just does sprintf( {}, "%d", n); */
1499 /* label number */
1500 q = symbol_name_temporary;
1501 for (*q++ = 0, i = n; i; ++q)
1503 *q = i % 10 + '0';
1504 i /= 10;
1506 while ((*p = *--q) != '\0')
1507 ++p;
1509 *p++ = 2; /* ^B */
1511 /* instance number */
1512 q = symbol_name_temporary;
1513 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1515 *q = i % 10 + '0';
1516 i /= 10;
1518 while ((*p++ = *--q) != '\0');;
1520 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1521 return (symbol_name_build);
1522 } /* fb_label_name() */
1525 * decode name that may have been generated by foo_label_name() above. If
1526 * the name wasn't generated by foo_label_name(), then return it unaltered.
1527 * This is used for error messages.
1530 char *
1531 decode_local_label_name (s)
1532 char *s;
1534 char *p;
1535 char *symbol_decode;
1536 int label_number;
1537 int instance_number;
1538 char *type;
1539 const char *message_format = _("\"%d\" (instance number %d of a %s label)");
1541 if (s[0] != 'L')
1542 return s;
1544 for (label_number = 0, p = s + 1; isdigit ((unsigned char) *p); ++p)
1545 label_number = (10 * label_number) + *p - '0';
1547 if (*p == 1)
1548 type = "dollar";
1549 else if (*p == 2)
1550 type = "fb";
1551 else
1552 return s;
1554 for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1555 instance_number = (10 * instance_number) + *p - '0';
1557 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1558 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1560 return symbol_decode;
1563 /* Get the value of a symbol. */
1565 valueT
1566 S_GET_VALUE (s)
1567 symbolS *s;
1569 if (LOCAL_SYMBOL_CHECK (s))
1570 return ((struct local_symbol *) s)->lsy_offset;
1572 if (!s->sy_resolved && s->sy_value.X_op != O_constant)
1573 resolve_symbol_value (s, 1);
1574 if (s->sy_value.X_op != O_constant)
1576 static symbolS *recur;
1578 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1579 may call S_GET_VALUE. We use a static symbol to avoid the
1580 immediate recursion. */
1581 if (recur == s)
1582 return (valueT) s->sy_value.X_add_number;
1583 recur = s;
1584 if (! s->sy_resolved
1585 || s->sy_value.X_op != O_symbol
1586 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1587 as_bad (_("Attempt to get value of unresolved symbol %s"),
1588 S_GET_NAME (s));
1589 recur = NULL;
1591 return (valueT) s->sy_value.X_add_number;
1594 /* Set the value of a symbol. */
1596 void
1597 S_SET_VALUE (s, val)
1598 symbolS *s;
1599 valueT val;
1601 if (LOCAL_SYMBOL_CHECK (s))
1603 ((struct local_symbol *) s)->lsy_offset = val;
1604 return;
1607 s->sy_value.X_op = O_constant;
1608 s->sy_value.X_add_number = (offsetT) val;
1609 s->sy_value.X_unsigned = 0;
1612 void
1613 copy_symbol_attributes (dest, src)
1614 symbolS *dest, *src;
1616 if (LOCAL_SYMBOL_CHECK (dest))
1617 dest = local_symbol_convert ((struct local_symbol *) dest);
1618 if (LOCAL_SYMBOL_CHECK (src))
1619 src = local_symbol_convert ((struct local_symbol *) src);
1621 #ifdef BFD_ASSEMBLER
1622 /* In an expression, transfer the settings of these flags.
1623 The user can override later, of course. */
1624 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1625 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1626 #endif
1628 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1629 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1630 #endif
1633 #ifdef BFD_ASSEMBLER
1636 S_IS_FUNCTION (s)
1637 symbolS *s;
1639 flagword flags;
1641 if (LOCAL_SYMBOL_CHECK (s))
1642 return 0;
1644 flags = s->bsym->flags;
1646 return (flags & BSF_FUNCTION) != 0;
1650 S_IS_EXTERNAL (s)
1651 symbolS *s;
1653 flagword flags;
1655 if (LOCAL_SYMBOL_CHECK (s))
1656 return 0;
1658 flags = s->bsym->flags;
1660 /* sanity check */
1661 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1662 abort ();
1664 return (flags & BSF_GLOBAL) != 0;
1668 S_IS_WEAK (s)
1669 symbolS *s;
1671 if (LOCAL_SYMBOL_CHECK (s))
1672 return 0;
1673 return (s->bsym->flags & BSF_WEAK) != 0;
1677 S_IS_COMMON (s)
1678 symbolS *s;
1680 if (LOCAL_SYMBOL_CHECK (s))
1681 return 0;
1682 return bfd_is_com_section (s->bsym->section);
1686 S_IS_DEFINED (s)
1687 symbolS *s;
1689 if (LOCAL_SYMBOL_CHECK (s))
1690 return ((struct local_symbol *) s)->lsy_section != undefined_section;
1691 return s->bsym->section != undefined_section;
1695 S_IS_DEBUG (s)
1696 symbolS *s;
1698 if (LOCAL_SYMBOL_CHECK (s))
1699 return 0;
1700 if (s->bsym->flags & BSF_DEBUGGING)
1701 return 1;
1702 return 0;
1706 S_IS_LOCAL (s)
1707 symbolS *s;
1709 flagword flags;
1710 const char *name;
1712 if (LOCAL_SYMBOL_CHECK (s))
1713 return 1;
1715 flags = s->bsym->flags;
1717 /* sanity check */
1718 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1719 abort ();
1721 if (bfd_get_section (s->bsym) == reg_section)
1722 return 1;
1724 if (flag_strip_local_absolute
1725 && (flags & BSF_GLOBAL) == 0
1726 && bfd_get_section (s->bsym) == absolute_section)
1727 return 1;
1729 name = S_GET_NAME (s);
1730 return (name != NULL
1731 && ! S_IS_DEBUG (s)
1732 && (strchr (name, '\001')
1733 || strchr (name, '\002')
1734 || (! flag_keep_locals
1735 && (bfd_is_local_label (stdoutput, s->bsym)
1736 || (flag_mri
1737 && name[0] == '?'
1738 && name[1] == '?')))));
1742 S_IS_EXTERN (s)
1743 symbolS *s;
1745 return S_IS_EXTERNAL (s);
1749 S_IS_STABD (s)
1750 symbolS *s;
1752 return S_GET_NAME (s) == 0;
1755 CONST char *
1756 S_GET_NAME (s)
1757 symbolS *s;
1759 if (LOCAL_SYMBOL_CHECK (s))
1760 return ((struct local_symbol *) s)->lsy_name;
1761 return s->bsym->name;
1764 segT
1765 S_GET_SEGMENT (s)
1766 symbolS *s;
1768 if (LOCAL_SYMBOL_CHECK (s))
1769 return ((struct local_symbol *) s)->lsy_section;
1770 return s->bsym->section;
1773 void
1774 S_SET_SEGMENT (s, seg)
1775 symbolS *s;
1776 segT seg;
1778 /* Don't reassign section symbols. The direct reason is to prevent seg
1779 faults assigning back to const global symbols such as *ABS*, but it
1780 shouldn't happen anyway. */
1782 if (LOCAL_SYMBOL_CHECK (s))
1784 if (seg == reg_section)
1785 s = local_symbol_convert ((struct local_symbol *) s);
1786 else
1788 ((struct local_symbol *) s)->lsy_section = seg;
1789 return;
1793 if (s->bsym->flags & BSF_SECTION_SYM)
1795 if (s->bsym->section != seg)
1796 abort();
1798 else
1799 s->bsym->section = seg;
1802 void
1803 S_SET_EXTERNAL (s)
1804 symbolS *s;
1806 if (LOCAL_SYMBOL_CHECK (s))
1807 s = local_symbol_convert ((struct local_symbol *) s);
1808 if ((s->bsym->flags & BSF_WEAK) != 0)
1810 /* Let .weak override .global. */
1811 return;
1813 s->bsym->flags |= BSF_GLOBAL;
1814 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1817 void
1818 S_CLEAR_EXTERNAL (s)
1819 symbolS *s;
1821 if (LOCAL_SYMBOL_CHECK (s))
1822 return;
1823 if ((s->bsym->flags & BSF_WEAK) != 0)
1825 /* Let .weak override. */
1826 return;
1828 s->bsym->flags |= BSF_LOCAL;
1829 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1832 void
1833 S_SET_WEAK (s)
1834 symbolS *s;
1836 if (LOCAL_SYMBOL_CHECK (s))
1837 s = local_symbol_convert ((struct local_symbol *) s);
1838 s->bsym->flags |= BSF_WEAK;
1839 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1842 void
1843 S_SET_NAME (s, name)
1844 symbolS *s;
1845 char *name;
1847 if (LOCAL_SYMBOL_CHECK (s))
1849 ((struct local_symbol *) s)->lsy_name = name;
1850 return;
1852 s->bsym->name = name;
1854 #endif /* BFD_ASSEMBLER */
1856 #ifdef SYMBOLS_NEED_BACKPOINTERS
1858 /* Return the previous symbol in a chain. */
1860 symbolS *
1861 symbol_previous (s)
1862 symbolS *s;
1864 if (LOCAL_SYMBOL_CHECK (s))
1865 abort ();
1866 return s->sy_previous;
1869 #endif /* SYMBOLS_NEED_BACKPOINTERS */
1871 /* Return the next symbol in a chain. */
1873 symbolS *
1874 symbol_next (s)
1875 symbolS *s;
1877 if (LOCAL_SYMBOL_CHECK (s))
1878 abort ();
1879 return s->sy_next;
1882 /* Return a pointer to the value of a symbol as an expression. */
1884 expressionS *
1885 symbol_get_value_expression (s)
1886 symbolS *s;
1888 if (LOCAL_SYMBOL_CHECK (s))
1889 s = local_symbol_convert ((struct local_symbol *) s);
1890 return &s->sy_value;
1893 /* Set the value of a symbol to an expression. */
1895 void
1896 symbol_set_value_expression (s, exp)
1897 symbolS *s;
1898 const expressionS *exp;
1900 if (LOCAL_SYMBOL_CHECK (s))
1901 s = local_symbol_convert ((struct local_symbol *) s);
1902 s->sy_value = *exp;
1905 /* Set the frag of a symbol. */
1907 void
1908 symbol_set_frag (s, f)
1909 symbolS *s;
1910 fragS *f;
1912 if (LOCAL_SYMBOL_CHECK (s))
1914 local_symbol_set_frag ((struct local_symbol *) s, f);
1915 return;
1917 s->sy_frag = f;
1920 /* Return the frag of a symbol. */
1922 fragS *
1923 symbol_get_frag (s)
1924 symbolS *s;
1926 if (LOCAL_SYMBOL_CHECK (s))
1927 return local_symbol_get_frag ((struct local_symbol *) s);
1928 return s->sy_frag;
1931 /* Mark a symbol as having been used. */
1933 void
1934 symbol_mark_used (s)
1935 symbolS *s;
1937 if (LOCAL_SYMBOL_CHECK (s))
1938 return;
1939 s->sy_used = 1;
1942 /* Clear the mark of whether a symbol has been used. */
1944 void
1945 symbol_clear_used (s)
1946 symbolS *s;
1948 if (LOCAL_SYMBOL_CHECK (s))
1949 s = local_symbol_convert ((struct local_symbol *) s);
1950 s->sy_used = 0;
1953 /* Return whether a symbol has been used. */
1956 symbol_used_p (s)
1957 symbolS *s;
1959 if (LOCAL_SYMBOL_CHECK (s))
1960 return 1;
1961 return s->sy_used;
1964 /* Mark a symbol as having been used in a reloc. */
1966 void
1967 symbol_mark_used_in_reloc (s)
1968 symbolS *s;
1970 if (LOCAL_SYMBOL_CHECK (s))
1971 s = local_symbol_convert ((struct local_symbol *) s);
1972 s->sy_used_in_reloc = 1;
1975 /* Clear the mark of whether a symbol has been used in a reloc. */
1977 void
1978 symbol_clear_used_in_reloc (s)
1979 symbolS *s;
1981 if (LOCAL_SYMBOL_CHECK (s))
1982 return;
1983 s->sy_used_in_reloc = 0;
1986 /* Return whether a symbol has been used in a reloc. */
1989 symbol_used_in_reloc_p (s)
1990 symbolS *s;
1992 if (LOCAL_SYMBOL_CHECK (s))
1993 return 0;
1994 return s->sy_used_in_reloc;
1997 /* Mark a symbol as an MRI common symbol. */
1999 void
2000 symbol_mark_mri_common (s)
2001 symbolS *s;
2003 if (LOCAL_SYMBOL_CHECK (s))
2004 s = local_symbol_convert ((struct local_symbol *) s);
2005 s->sy_mri_common = 1;
2008 /* Clear the mark of whether a symbol is an MRI common symbol. */
2010 void
2011 symbol_clear_mri_common (s)
2012 symbolS *s;
2014 if (LOCAL_SYMBOL_CHECK (s))
2015 return;
2016 s->sy_mri_common = 0;
2019 /* Return whether a symbol is an MRI common symbol. */
2022 symbol_mri_common_p (s)
2023 symbolS *s;
2025 if (LOCAL_SYMBOL_CHECK (s))
2026 return 0;
2027 return s->sy_mri_common;
2030 /* Mark a symbol as having been written. */
2032 void
2033 symbol_mark_written (s)
2034 symbolS *s;
2036 if (LOCAL_SYMBOL_CHECK (s))
2037 return;
2038 s->written = 1;
2041 /* Clear the mark of whether a symbol has been written. */
2043 void
2044 symbol_clear_written (s)
2045 symbolS *s;
2047 if (LOCAL_SYMBOL_CHECK (s))
2048 return;
2049 s->written = 0;
2052 /* Return whether a symbol has been written. */
2055 symbol_written_p (s)
2056 symbolS *s;
2058 if (LOCAL_SYMBOL_CHECK (s))
2059 return 0;
2060 return s->written;
2063 /* Mark a symbol has having been resolved. */
2065 void
2066 symbol_mark_resolved (s)
2067 symbolS *s;
2069 if (LOCAL_SYMBOL_CHECK (s))
2071 local_symbol_mark_resolved ((struct local_symbol *) s);
2072 return;
2074 s->sy_resolved = 1;
2077 /* Return whether a symbol has been resolved. */
2080 symbol_resolved_p (s)
2081 symbolS *s;
2083 if (LOCAL_SYMBOL_CHECK (s))
2084 return local_symbol_resolved_p ((struct local_symbol *) s);
2085 return s->sy_resolved;
2088 /* Return whether a symbol is a section symbol. */
2091 symbol_section_p (s)
2092 symbolS *s;
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 ();
2235 int indent_level;
2237 /* Maximum indent level.
2238 Available for modification inside a gdb session. */
2239 int max_indent_level = 8;
2241 #if 0
2243 static void
2244 indent ()
2246 printf ("%*s", indent_level * 4, "");
2249 #endif
2251 void
2252 print_symbol_value_1 (file, sym)
2253 FILE *file;
2254 symbolS *sym;
2256 const char *name = S_GET_NAME (sym);
2257 if (!name || !name[0])
2258 name = "(unnamed)";
2259 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2261 if (LOCAL_SYMBOL_CHECK (sym))
2263 struct local_symbol *locsym = (struct local_symbol *) sym;
2264 if (local_symbol_get_frag (locsym) != &zero_address_frag
2265 && local_symbol_get_frag (locsym) != NULL)
2266 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2267 if (local_symbol_resolved_p (locsym))
2268 fprintf (file, " resolved");
2269 fprintf (file, " local");
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 if (LOCAL_SYMBOL_CHECK (sym))
2309 fprintf (file, "constant %lx",
2310 (long) ((struct local_symbol *) sym)->lsy_offset);
2311 else
2312 print_expr_1 (file, &sym->sy_value);
2313 fprintf (file, ">");
2314 indent_level--;
2316 fflush (file);
2319 void
2320 print_symbol_value (sym)
2321 symbolS *sym;
2323 indent_level = 0;
2324 print_symbol_value_1 (stderr, sym);
2325 fprintf (stderr, "\n");
2328 static void
2329 print_binary (file, name, exp)
2330 FILE *file;
2331 const char * name;
2332 expressionS *exp;
2334 indent_level++;
2335 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2336 print_symbol_value_1 (file, exp->X_add_symbol);
2337 fprintf (file, ">\n%*s<", indent_level * 4, "");
2338 print_symbol_value_1 (file, exp->X_op_symbol);
2339 fprintf (file, ">");
2340 indent_level--;
2343 void
2344 print_expr_1 (file, exp)
2345 FILE *file;
2346 expressionS *exp;
2348 fprintf (file, "expr %lx ", (long) exp);
2349 switch (exp->X_op)
2351 case O_illegal:
2352 fprintf (file, "illegal");
2353 break;
2354 case O_absent:
2355 fprintf (file, "absent");
2356 break;
2357 case O_constant:
2358 fprintf (file, "constant %lx", (long) exp->X_add_number);
2359 break;
2360 case O_symbol:
2361 indent_level++;
2362 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2363 print_symbol_value_1 (file, exp->X_add_symbol);
2364 fprintf (file, ">");
2365 maybe_print_addnum:
2366 if (exp->X_add_number)
2367 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2368 (long) exp->X_add_number);
2369 indent_level--;
2370 break;
2371 case O_register:
2372 fprintf (file, "register #%d", (int) exp->X_add_number);
2373 break;
2374 case O_big:
2375 fprintf (file, "big");
2376 break;
2377 case O_uminus:
2378 fprintf (file, "uminus -<");
2379 indent_level++;
2380 print_symbol_value_1 (file, exp->X_add_symbol);
2381 fprintf (file, ">");
2382 goto maybe_print_addnum;
2383 case O_bit_not:
2384 fprintf (file, "bit_not");
2385 break;
2386 case O_multiply:
2387 print_binary (file, "multiply", exp);
2388 break;
2389 case O_divide:
2390 print_binary (file, "divide", exp);
2391 break;
2392 case O_modulus:
2393 print_binary (file, "modulus", exp);
2394 break;
2395 case O_left_shift:
2396 print_binary (file, "lshift", exp);
2397 break;
2398 case O_right_shift:
2399 print_binary (file, "rshift", exp);
2400 break;
2401 case O_bit_inclusive_or:
2402 print_binary (file, "bit_ior", exp);
2403 break;
2404 case O_bit_exclusive_or:
2405 print_binary (file, "bit_xor", exp);
2406 break;
2407 case O_bit_and:
2408 print_binary (file, "bit_and", exp);
2409 break;
2410 case O_eq:
2411 print_binary (file, "eq", exp);
2412 break;
2413 case O_ne:
2414 print_binary (file, "ne", exp);
2415 break;
2416 case O_lt:
2417 print_binary (file, "lt", exp);
2418 break;
2419 case O_le:
2420 print_binary (file, "le", exp);
2421 break;
2422 case O_ge:
2423 print_binary (file, "ge", exp);
2424 break;
2425 case O_gt:
2426 print_binary (file, "gt", exp);
2427 break;
2428 case O_logical_and:
2429 print_binary (file, "logical_and", exp);
2430 break;
2431 case O_logical_or:
2432 print_binary (file, "logical_or", exp);
2433 break;
2434 case O_add:
2435 indent_level++;
2436 fprintf (file, "add\n%*s<", indent_level * 4, "");
2437 print_symbol_value_1 (file, exp->X_add_symbol);
2438 fprintf (file, ">\n%*s<", indent_level * 4, "");
2439 print_symbol_value_1 (file, exp->X_op_symbol);
2440 fprintf (file, ">");
2441 goto maybe_print_addnum;
2442 case O_subtract:
2443 indent_level++;
2444 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2445 print_symbol_value_1 (file, exp->X_add_symbol);
2446 fprintf (file, ">\n%*s<", indent_level * 4, "");
2447 print_symbol_value_1 (file, exp->X_op_symbol);
2448 fprintf (file, ">");
2449 goto maybe_print_addnum;
2450 default:
2451 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2452 break;
2454 fflush (stdout);
2457 void
2458 print_expr (exp)
2459 expressionS *exp;
2461 print_expr_1 (stderr, exp);
2462 fprintf (stderr, "\n");
2465 void
2466 symbol_print_statistics (file)
2467 FILE *file;
2469 hash_print_statistics (file, "symbol table", sy_hash);
2470 #ifdef BFD_ASSEMBLER
2471 hash_print_statistics (file, "mini local symbol table", local_hash);
2472 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2473 local_symbol_count, local_symbol_conversion_count);
2474 #endif
2477 /* end of symbols.c */