* ld-scripts/crossref.exp: Add -mcall-aixdesc to CFLAGS for
[binutils.git] / gas / symbols.c
blob78ec954f9b555ae8ed342aa3bd5c65170fab2365
1 /* symbols.c -symbol table-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
25 #include "as.h"
27 #include "safe-ctype.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 #define DOLLAR_LABEL_CHAR '\001'
59 #define LOCAL_LABEL_CHAR '\002'
61 struct obstack notes;
62 #ifdef USE_UNIQUE
63 /* The name of an external symbol which is
64 used to make weak PE symbol names unique. */
65 const char * an_external_name;
66 #endif
68 static char *save_symbol_name (const char *);
69 static void fb_label_init (void);
70 static long dollar_label_instance (long);
71 static long fb_label_instance (long);
73 static void print_binary (FILE *, const char *, expressionS *);
74 static void report_op_error (symbolS *, symbolS *, symbolS *);
76 /* Return a pointer to a new symbol. Die if we can't make a new
77 symbol. Fill in the symbol's values. Add symbol to end of symbol
78 chain.
80 This function should be called in the general case of creating a
81 symbol. However, if the output file symbol table has already been
82 set, and you are certain that this symbol won't be wanted in the
83 output file, you can call symbol_create. */
85 symbolS *
86 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
88 symbolS *symbolP = symbol_create (name, segment, valu, frag);
90 /* Link to end of symbol chain. */
92 extern int symbol_table_frozen;
93 if (symbol_table_frozen)
94 abort ();
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 (const char *name)
107 unsigned int name_length;
108 char *ret;
110 name_length = strlen (name) + 1; /* +1 for \0. */
111 obstack_grow (&notes, name, name_length);
112 ret = obstack_finish (&notes);
114 #ifdef tc_canonicalize_symbol_name
115 ret = tc_canonicalize_symbol_name (ret);
116 #endif
118 if (! symbols_case_sensitive)
120 char *s;
122 for (s = ret; *s != '\0'; s++)
123 *s = TOUPPER (*s);
126 return ret;
129 symbolS *
130 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
131 segT segment, /* Segment identifier (SEG_<something>). */
132 valueT valu, /* Symbol value. */
133 fragS *frag /* Associated fragment. */)
135 char *preserved_copy_of_name;
136 symbolS *symbolP;
138 preserved_copy_of_name = save_symbol_name (name);
140 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
142 /* symbol must be born in some fixed state. This seems as good as any. */
143 memset (symbolP, 0, sizeof (symbolS));
145 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
146 if (symbolP->bsym == NULL)
147 as_perror ("%s", "bfd_make_empty_symbol");
148 symbolP->bsym->udata.p = (PTR) symbolP;
149 S_SET_NAME (symbolP, preserved_copy_of_name);
151 S_SET_SEGMENT (symbolP, segment);
152 S_SET_VALUE (symbolP, valu);
153 symbol_clear_list_pointers (symbolP);
155 symbolP->sy_frag = frag;
157 obj_symbol_new_hook (symbolP);
159 #ifdef tc_symbol_new_hook
160 tc_symbol_new_hook (symbolP);
161 #endif
163 return symbolP;
167 /* Local symbol support. If we can get away with it, we keep only a
168 small amount of information for local symbols. */
170 static symbolS *local_symbol_convert (struct local_symbol *);
172 /* Used for statistics. */
174 static unsigned long local_symbol_count;
175 static unsigned long local_symbol_conversion_count;
177 /* This macro is called with a symbol argument passed by reference.
178 It returns whether this is a local symbol. If necessary, it
179 changes its argument to the real symbol. */
181 #define LOCAL_SYMBOL_CHECK(s) \
182 (s->bsym == NULL \
183 ? (local_symbol_converted_p ((struct local_symbol *) s) \
184 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
185 0) \
186 : 1) \
187 : 0)
189 /* Create a local symbol and insert it into the local hash table. */
191 static struct local_symbol *
192 local_symbol_make (const char *name, segT section, valueT value, fragS *frag)
194 char *name_copy;
195 struct local_symbol *ret;
197 ++local_symbol_count;
199 name_copy = save_symbol_name (name);
201 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
202 ret->lsy_marker = NULL;
203 ret->lsy_name = name_copy;
204 ret->lsy_section = section;
205 local_symbol_set_frag (ret, frag);
206 ret->lsy_value = value;
208 hash_jam (local_hash, name_copy, (PTR) ret);
210 return ret;
213 /* Convert a local symbol into a real symbol. Note that we do not
214 reclaim the space used by the local symbol. */
216 static symbolS *
217 local_symbol_convert (struct local_symbol *locsym)
219 symbolS *ret;
221 assert (locsym->lsy_marker == NULL);
222 if (local_symbol_converted_p (locsym))
223 return local_symbol_get_real_symbol (locsym);
225 ++local_symbol_conversion_count;
227 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
228 local_symbol_get_frag (locsym));
230 if (local_symbol_resolved_p (locsym))
231 ret->sy_resolved = 1;
233 /* Local symbols are always either defined or used. */
234 ret->sy_used = 1;
236 #ifdef TC_LOCAL_SYMFIELD_CONVERT
237 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
238 #endif
240 symbol_table_insert (ret);
242 local_symbol_mark_converted (locsym);
243 local_symbol_set_real_symbol (locsym, ret);
245 hash_jam (local_hash, locsym->lsy_name, NULL);
247 return ret;
250 /* We have just seen "<name>:".
251 Creates a struct symbol unless it already exists.
253 Gripes if we are redefining a symbol incompatibly (and ignores it). */
255 symbolS *
256 colon (/* Just seen "x:" - rattle symbols & frags. */
257 const char *sym_name /* Symbol name, as a cannonical string. */
258 /* We copy this string: OK to alter later. */)
260 register symbolS *symbolP; /* Symbol we are working with. */
262 /* Sun local labels go out of scope whenever a non-local symbol is
263 defined. */
264 if (LOCAL_LABELS_DOLLAR
265 && !bfd_is_local_label_name (stdoutput, sym_name))
266 dollar_label_clear ();
268 #ifndef WORKING_DOT_WORD
269 if (new_broken_words)
271 struct broken_word *a;
272 int possible_bytes;
273 fragS *frag_tmp;
274 char *frag_opcode;
276 if (now_seg == absolute_section)
278 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
279 return NULL;
282 possible_bytes = (md_short_jump_size
283 + new_broken_words * md_long_jump_size);
285 frag_tmp = frag_now;
286 frag_opcode = frag_var (rs_broken_word,
287 possible_bytes,
288 possible_bytes,
289 (relax_substateT) 0,
290 (symbolS *) broken_words,
291 (offsetT) 0,
292 NULL);
294 /* We want to store the pointer to where to insert the jump
295 table in the fr_opcode of the rs_broken_word frag. This
296 requires a little hackery. */
297 while (frag_tmp
298 && (frag_tmp->fr_type != rs_broken_word
299 || frag_tmp->fr_opcode))
300 frag_tmp = frag_tmp->fr_next;
301 know (frag_tmp);
302 frag_tmp->fr_opcode = frag_opcode;
303 new_broken_words = 0;
305 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
306 a->dispfrag = frag_tmp;
308 #endif /* WORKING_DOT_WORD */
310 if ((symbolP = symbol_find (sym_name)) != 0)
312 #ifdef RESOLVE_SYMBOL_REDEFINITION
313 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
314 return symbolP;
315 #endif
316 /* Now check for undefined symbols. */
317 if (LOCAL_SYMBOL_CHECK (symbolP))
319 struct local_symbol *locsym = (struct local_symbol *) symbolP;
321 if (locsym->lsy_section != undefined_section
322 && (local_symbol_get_frag (locsym) != frag_now
323 || locsym->lsy_section != now_seg
324 || locsym->lsy_value != frag_now_fix ()))
326 as_bad (_("symbol `%s' is already defined"), sym_name);
327 return symbolP;
330 locsym->lsy_section = now_seg;
331 local_symbol_set_frag (locsym, frag_now);
332 locsym->lsy_value = frag_now_fix ();
334 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
336 if (S_GET_VALUE (symbolP) == 0)
338 symbolP->sy_frag = frag_now;
339 #ifdef OBJ_VMS
340 S_SET_OTHER (symbolP, const_flag);
341 #endif
342 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
343 S_SET_SEGMENT (symbolP, now_seg);
344 #ifdef N_UNDF
345 know (N_UNDF == 0);
346 #endif /* if we have one, it better be zero. */
349 else
351 /* There are still several cases to check:
353 A .comm/.lcomm symbol being redefined as initialized
354 data is OK
356 A .comm/.lcomm symbol being redefined with a larger
357 size is also OK
359 This only used to be allowed on VMS gas, but Sun cc
360 on the sparc also depends on it. */
362 if (((!S_IS_DEBUG (symbolP)
363 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
364 && S_IS_EXTERNAL (symbolP))
365 || S_GET_SEGMENT (symbolP) == bss_section)
366 && (now_seg == data_section
367 || now_seg == S_GET_SEGMENT (symbolP)))
369 /* Select which of the 2 cases this is. */
370 if (now_seg != data_section)
372 /* New .comm for prev .comm symbol.
374 If the new size is larger we just change its
375 value. If the new size is smaller, we ignore
376 this symbol. */
377 if (S_GET_VALUE (symbolP)
378 < ((unsigned) frag_now_fix ()))
380 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
383 else
385 /* It is a .comm/.lcomm being converted to initialized
386 data. */
387 symbolP->sy_frag = frag_now;
388 #ifdef OBJ_VMS
389 S_SET_OTHER (symbolP, const_flag);
390 #endif
391 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
392 S_SET_SEGMENT (symbolP, now_seg); /* Keep N_EXT bit. */
395 else
397 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
398 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
399 static const char *od_buf = "";
400 #else
401 char od_buf[100];
402 od_buf[0] = '\0';
403 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
404 sprintf (od_buf, "%d.%d.",
405 S_GET_OTHER (symbolP),
406 S_GET_DESC (symbolP));
407 #endif
408 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
409 sym_name,
410 segment_name (S_GET_SEGMENT (symbolP)),
411 od_buf,
412 (long) S_GET_VALUE (symbolP));
414 } /* if the undefined symbol has no value */
416 else
418 /* Don't blow up if the definition is the same. */
419 if (!(frag_now == symbolP->sy_frag
420 && S_GET_VALUE (symbolP) == frag_now_fix ()
421 && S_GET_SEGMENT (symbolP) == now_seg))
422 as_bad (_("symbol `%s' is already defined"), sym_name);
426 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
428 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
429 (valueT) frag_now_fix (),
430 frag_now);
432 else
434 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
435 frag_now);
436 #ifdef OBJ_VMS
437 S_SET_OTHER (symbolP, const_flag);
438 #endif /* OBJ_VMS */
440 symbol_table_insert (symbolP);
443 if (mri_common_symbol != NULL)
445 /* This symbol is actually being defined within an MRI common
446 section. This requires special handling. */
447 if (LOCAL_SYMBOL_CHECK (symbolP))
448 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
449 symbolP->sy_value.X_op = O_symbol;
450 symbolP->sy_value.X_add_symbol = mri_common_symbol;
451 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
452 symbolP->sy_frag = &zero_address_frag;
453 S_SET_SEGMENT (symbolP, expr_section);
454 symbolP->sy_mri_common = 1;
457 #ifdef tc_frob_label
458 tc_frob_label (symbolP);
459 #endif
460 #ifdef obj_frob_label
461 obj_frob_label (symbolP);
462 #endif
464 return symbolP;
467 /* Die if we can't insert the symbol. */
469 void
470 symbol_table_insert (symbolS *symbolP)
472 register const char *error_string;
474 know (symbolP);
475 know (S_GET_NAME (symbolP));
477 if (LOCAL_SYMBOL_CHECK (symbolP))
479 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
480 (PTR) symbolP);
481 if (error_string != NULL)
482 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
483 S_GET_NAME (symbolP), error_string);
484 return;
487 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
489 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
490 S_GET_NAME (symbolP), error_string);
491 } /* on error */
494 /* If a symbol name does not exist, create it as undefined, and insert
495 it into the symbol table. Return a pointer to it. */
497 symbolS *
498 symbol_find_or_make (const char *name)
500 register symbolS *symbolP;
502 symbolP = symbol_find (name);
504 if (symbolP == NULL)
506 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
508 symbolP = md_undefined_symbol ((char *) name);
509 if (symbolP != NULL)
510 return symbolP;
512 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
513 (valueT) 0,
514 &zero_address_frag);
515 return symbolP;
518 symbolP = symbol_make (name);
520 symbol_table_insert (symbolP);
521 } /* if symbol wasn't found */
523 return (symbolP);
526 symbolS *
527 symbol_make (const char *name)
529 symbolS *symbolP;
531 /* Let the machine description default it, e.g. for register names. */
532 symbolP = md_undefined_symbol ((char *) name);
534 if (!symbolP)
535 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
537 return (symbolP);
540 symbolS *
541 symbol_clone (symbolS *orgsymP, int replace)
543 symbolS *newsymP;
545 /* Running local_symbol_convert on a clone that's not the one currently
546 in local_hash would incorrectly replace the hash entry. Thus the
547 symbol must be converted here. Note that the rest of the function
548 depends on not encountering an unconverted symbol. */
549 if (LOCAL_SYMBOL_CHECK (orgsymP))
550 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
552 know (S_IS_DEFINED (orgsymP));
554 newsymP = obstack_alloc (&notes, sizeof (*newsymP));
555 *newsymP = *orgsymP;
557 if (replace)
559 if (symbol_rootP == orgsymP)
560 symbol_rootP = newsymP;
561 else if (orgsymP->sy_previous)
563 orgsymP->sy_previous->sy_next = newsymP;
564 orgsymP->sy_previous = NULL;
566 if (symbol_lastP == orgsymP)
567 symbol_lastP = newsymP;
568 else if (orgsymP->sy_next)
569 orgsymP->sy_next->sy_previous = newsymP;
570 orgsymP->sy_next = NULL;
571 debug_verify_symchain (symbol_rootP, symbol_lastP);
573 symbol_table_insert (newsymP);
576 return newsymP;
579 /* Referenced symbols, if they are forward references, need to be cloned
580 (without replacing the original) so that the value of the referenced
581 symbols at the point of use . */
583 #undef symbol_clone_if_forward_ref
584 symbolS *
585 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
587 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
589 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
590 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
592 if (symbolP->sy_forward_ref)
593 is_forward = 1;
595 if (is_forward)
597 /* assign_symbol() clones volatile symbols; pre-existing expressions
598 hold references to the original instance, but want the current
599 value. Just repeat the lookup. */
600 if (add_symbol && S_IS_VOLATILE (add_symbol))
601 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
602 if (op_symbol && S_IS_VOLATILE (op_symbol))
603 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
606 /* Re-using sy_resolving here, as this routine cannot get called from
607 symbol resolution code. */
608 if (symbolP->bsym->section == expr_section && !symbolP->sy_resolving)
610 symbolP->sy_resolving = 1;
611 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
612 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
613 symbolP->sy_resolving = 0;
616 if (symbolP->sy_forward_ref
617 || add_symbol != symbolP->sy_value.X_add_symbol
618 || op_symbol != symbolP->sy_value.X_op_symbol)
619 symbolP = symbol_clone (symbolP, 0);
621 symbolP->sy_value.X_add_symbol = add_symbol;
622 symbolP->sy_value.X_op_symbol = op_symbol;
625 return symbolP;
628 symbolS *
629 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
631 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
634 symbolS *
635 symbol_temp_new_now (void)
637 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
640 symbolS *
641 symbol_temp_make (void)
643 return symbol_make (FAKE_LABEL_NAME);
646 /* Implement symbol table lookup.
647 In: A symbol's name as a string: '\0' can't be part of a symbol name.
648 Out: NULL if the name was not in the symbol table, else the address
649 of a struct symbol associated with that name. */
651 symbolS *
652 symbol_find_exact (const char *name)
654 struct local_symbol *locsym;
656 locsym = (struct local_symbol *) hash_find (local_hash, name);
657 if (locsym != NULL)
658 return (symbolS *) locsym;
660 return ((symbolS *) hash_find (sy_hash, name));
663 symbolS *
664 symbol_find (const char *name)
666 #ifdef tc_canonicalize_symbol_name
668 char *copy;
669 size_t len = strlen (name) + 1;
671 copy = (char *) alloca (len);
672 memcpy (copy, name, len);
673 name = tc_canonicalize_symbol_name (copy);
675 #endif
677 if (! symbols_case_sensitive)
679 char *copy;
680 const char *orig;
681 unsigned char c;
683 orig = name;
684 name = copy = (char *) alloca (strlen (name) + 1);
686 while ((c = *orig++) != '\0')
688 *copy++ = TOUPPER (c);
690 *copy = '\0';
693 return symbol_find_exact (name);
696 /* Once upon a time, symbols were kept in a singly linked list. At
697 least coff needs to be able to rearrange them from time to time, for
698 which a doubly linked list is much more convenient. Loic did these
699 as macros which seemed dangerous to me so they're now functions.
700 xoxorich. */
702 /* Link symbol ADDME after symbol TARGET in the chain. */
704 void
705 symbol_append (symbolS *addme, symbolS *target,
706 symbolS **rootPP, symbolS **lastPP)
708 if (LOCAL_SYMBOL_CHECK (addme))
709 abort ();
710 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
711 abort ();
713 if (target == NULL)
715 know (*rootPP == NULL);
716 know (*lastPP == NULL);
717 addme->sy_next = NULL;
718 addme->sy_previous = NULL;
719 *rootPP = addme;
720 *lastPP = addme;
721 return;
722 } /* if the list is empty */
724 if (target->sy_next != NULL)
726 target->sy_next->sy_previous = addme;
728 else
730 know (*lastPP == target);
731 *lastPP = addme;
732 } /* if we have a next */
734 addme->sy_next = target->sy_next;
735 target->sy_next = addme;
736 addme->sy_previous = target;
738 debug_verify_symchain (symbol_rootP, symbol_lastP);
741 /* Set the chain pointers of SYMBOL to null. */
743 void
744 symbol_clear_list_pointers (symbolS *symbolP)
746 if (LOCAL_SYMBOL_CHECK (symbolP))
747 abort ();
748 symbolP->sy_next = NULL;
749 symbolP->sy_previous = NULL;
752 /* Remove SYMBOLP from the list. */
754 void
755 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
757 if (LOCAL_SYMBOL_CHECK (symbolP))
758 abort ();
760 if (symbolP == *rootPP)
762 *rootPP = symbolP->sy_next;
763 } /* if it was the root */
765 if (symbolP == *lastPP)
767 *lastPP = symbolP->sy_previous;
768 } /* if it was the tail */
770 if (symbolP->sy_next != NULL)
772 symbolP->sy_next->sy_previous = symbolP->sy_previous;
773 } /* if not last */
775 if (symbolP->sy_previous != NULL)
777 symbolP->sy_previous->sy_next = symbolP->sy_next;
778 } /* if not first */
780 debug_verify_symchain (*rootPP, *lastPP);
783 /* Link symbol ADDME before symbol TARGET in the chain. */
785 void
786 symbol_insert (symbolS *addme, symbolS *target,
787 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
789 if (LOCAL_SYMBOL_CHECK (addme))
790 abort ();
791 if (LOCAL_SYMBOL_CHECK (target))
792 abort ();
794 if (target->sy_previous != NULL)
796 target->sy_previous->sy_next = addme;
798 else
800 know (*rootPP == target);
801 *rootPP = addme;
802 } /* if not first */
804 addme->sy_previous = target->sy_previous;
805 target->sy_previous = addme;
806 addme->sy_next = target;
808 debug_verify_symchain (*rootPP, *lastPP);
811 void
812 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
814 symbolS *symbolP = rootP;
816 if (symbolP == NULL)
817 return;
819 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
821 assert (symbolP->bsym != NULL);
822 assert (symbolP->sy_next->sy_previous == symbolP);
825 assert (lastP == symbolP);
828 static void
829 report_op_error (symbolS *symp, symbolS *left, symbolS *right)
831 char *file;
832 unsigned int line;
833 segT seg_left = S_GET_SEGMENT (left);
834 segT seg_right = right ? S_GET_SEGMENT (right) : 0;
836 if (expr_symbol_where (symp, &file, &line))
838 if (seg_left == undefined_section)
839 as_bad_where (file, line,
840 _("undefined symbol `%s' in operation"),
841 S_GET_NAME (left));
842 if (seg_right == undefined_section)
843 as_bad_where (file, line,
844 _("undefined symbol `%s' in operation"),
845 S_GET_NAME (right));
846 if (seg_left != undefined_section
847 && seg_right != undefined_section)
849 if (right)
850 as_bad_where (file, line,
851 _("invalid sections for operation on `%s' and `%s'"),
852 S_GET_NAME (left), S_GET_NAME (right));
853 else
854 as_bad_where (file, line,
855 _("invalid section for operation on `%s'"),
856 S_GET_NAME (left));
860 else
862 if (seg_left == undefined_section)
863 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
864 S_GET_NAME (left), S_GET_NAME (symp));
865 if (seg_right == undefined_section)
866 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
867 S_GET_NAME (right), S_GET_NAME (symp));
868 if (seg_left != undefined_section
869 && seg_right != undefined_section)
871 if (right)
872 as_bad_where (file, line,
873 _("invalid sections for operation on `%s' and `%s' setting `%s'"),
874 S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
875 else
876 as_bad_where (file, line,
877 _("invalid section for operation on `%s' setting `%s'"),
878 S_GET_NAME (left), S_GET_NAME (symp));
883 /* Resolve the value of a symbol. This is called during the final
884 pass over the symbol table to resolve any symbols with complex
885 values. */
887 valueT
888 resolve_symbol_value (symbolS *symp)
890 int resolved;
891 valueT final_val = 0;
892 segT final_seg;
894 if (LOCAL_SYMBOL_CHECK (symp))
896 struct local_symbol *locsym = (struct local_symbol *) symp;
898 final_val = locsym->lsy_value;
899 if (local_symbol_resolved_p (locsym))
900 return final_val;
902 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
904 if (finalize_syms)
906 locsym->lsy_value = final_val;
907 local_symbol_mark_resolved (locsym);
910 return final_val;
913 if (symp->sy_resolved)
915 if (symp->sy_value.X_op == O_constant)
916 return (valueT) symp->sy_value.X_add_number;
917 else
918 return 0;
921 resolved = 0;
922 final_seg = S_GET_SEGMENT (symp);
924 if (symp->sy_resolving)
926 if (finalize_syms)
927 as_bad (_("symbol definition loop encountered at `%s'"),
928 S_GET_NAME (symp));
929 final_val = 0;
930 resolved = 1;
932 else
934 symbolS *add_symbol, *op_symbol;
935 offsetT left, right;
936 segT seg_left, seg_right;
937 operatorT op;
938 int move_seg_ok;
940 symp->sy_resolving = 1;
942 /* Help out with CSE. */
943 add_symbol = symp->sy_value.X_add_symbol;
944 op_symbol = symp->sy_value.X_op_symbol;
945 final_val = symp->sy_value.X_add_number;
946 op = symp->sy_value.X_op;
948 switch (op)
950 default:
951 BAD_CASE (op);
952 break;
954 case O_absent:
955 final_val = 0;
956 /* Fall through. */
958 case O_constant:
959 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
960 if (final_seg == expr_section)
961 final_seg = absolute_section;
962 resolved = 1;
963 break;
965 case O_symbol:
966 case O_symbol_rva:
967 left = resolve_symbol_value (add_symbol);
968 seg_left = S_GET_SEGMENT (add_symbol);
969 if (finalize_syms)
970 symp->sy_value.X_op_symbol = NULL;
972 do_symbol:
973 if (symp->sy_mri_common)
975 /* This is a symbol inside an MRI common section. The
976 relocation routines are going to handle it specially.
977 Don't change the value. */
978 resolved = symbol_resolved_p (add_symbol);
979 break;
982 if (finalize_syms && final_val == 0)
984 if (LOCAL_SYMBOL_CHECK (add_symbol))
985 add_symbol = local_symbol_convert ((struct local_symbol *)
986 add_symbol);
987 copy_symbol_attributes (symp, add_symbol);
990 /* If we have equated this symbol to an undefined or common
991 symbol, keep X_op set to O_symbol, and don't change
992 X_add_number. This permits the routine which writes out
993 relocation to detect this case, and convert the
994 relocation to be against the symbol to which this symbol
995 is equated. */
996 if (! S_IS_DEFINED (add_symbol)
997 #if defined (OBJ_COFF) && defined (TE_PE)
998 || S_IS_WEAK (add_symbol)
999 #endif
1000 || S_IS_COMMON (add_symbol))
1002 if (finalize_syms)
1004 symp->sy_value.X_op = O_symbol;
1005 symp->sy_value.X_add_symbol = add_symbol;
1006 symp->sy_value.X_add_number = final_val;
1007 /* Use X_op_symbol as a flag. */
1008 symp->sy_value.X_op_symbol = add_symbol;
1009 final_seg = seg_left;
1011 final_val = 0;
1012 resolved = symbol_resolved_p (add_symbol);
1013 symp->sy_resolving = 0;
1014 goto exit_dont_set_value;
1016 else if (finalize_syms && final_seg == expr_section
1017 && seg_left != expr_section)
1019 /* If the symbol is an expression symbol, do similarly
1020 as for undefined and common syms above. Handles
1021 "sym +/- expr" where "expr" cannot be evaluated
1022 immediately, and we want relocations to be against
1023 "sym", eg. because it is weak. */
1024 symp->sy_value.X_op = O_symbol;
1025 symp->sy_value.X_add_symbol = add_symbol;
1026 symp->sy_value.X_add_number = final_val;
1027 symp->sy_value.X_op_symbol = add_symbol;
1028 final_seg = seg_left;
1029 final_val += symp->sy_frag->fr_address + left;
1030 resolved = symbol_resolved_p (add_symbol);
1031 symp->sy_resolving = 0;
1032 goto exit_dont_set_value;
1034 else
1036 final_val += symp->sy_frag->fr_address + left;
1037 if (final_seg == expr_section || final_seg == undefined_section)
1038 final_seg = seg_left;
1041 resolved = symbol_resolved_p (add_symbol);
1042 break;
1044 case O_uminus:
1045 case O_bit_not:
1046 case O_logical_not:
1047 left = resolve_symbol_value (add_symbol);
1048 seg_left = S_GET_SEGMENT (add_symbol);
1050 /* By reducing these to the relevant dyadic operator, we get
1051 !S -> S == 0 permitted on anything,
1052 -S -> 0 - S only permitted on absolute
1053 ~S -> S ^ ~0 only permitted on absolute */
1054 if (op != O_logical_not && seg_left != absolute_section
1055 && finalize_syms)
1056 report_op_error (symp, add_symbol, NULL);
1058 if (final_seg == expr_section || final_seg == undefined_section)
1059 final_seg = absolute_section;
1061 if (op == O_uminus)
1062 left = -left;
1063 else if (op == O_logical_not)
1064 left = !left;
1065 else
1066 left = ~left;
1068 final_val += left + symp->sy_frag->fr_address;
1070 resolved = symbol_resolved_p (add_symbol);
1071 break;
1073 case O_multiply:
1074 case O_divide:
1075 case O_modulus:
1076 case O_left_shift:
1077 case O_right_shift:
1078 case O_bit_inclusive_or:
1079 case O_bit_or_not:
1080 case O_bit_exclusive_or:
1081 case O_bit_and:
1082 case O_add:
1083 case O_subtract:
1084 case O_eq:
1085 case O_ne:
1086 case O_lt:
1087 case O_le:
1088 case O_ge:
1089 case O_gt:
1090 case O_logical_and:
1091 case O_logical_or:
1092 left = resolve_symbol_value (add_symbol);
1093 right = resolve_symbol_value (op_symbol);
1094 seg_left = S_GET_SEGMENT (add_symbol);
1095 seg_right = S_GET_SEGMENT (op_symbol);
1097 /* Simplify addition or subtraction of a constant by folding the
1098 constant into X_add_number. */
1099 if (op == O_add)
1101 if (seg_right == absolute_section)
1103 final_val += right;
1104 goto do_symbol;
1106 else if (seg_left == absolute_section)
1108 final_val += left;
1109 add_symbol = op_symbol;
1110 left = right;
1111 seg_left = seg_right;
1112 goto do_symbol;
1115 else if (op == O_subtract)
1117 if (seg_right == absolute_section)
1119 final_val -= right;
1120 goto do_symbol;
1124 move_seg_ok = 1;
1125 /* Equality and non-equality tests are permitted on anything.
1126 Subtraction, and other comparison operators are permitted if
1127 both operands are in the same section. Otherwise, both
1128 operands must be absolute. We already handled the case of
1129 addition or subtraction of a constant above. This will
1130 probably need to be changed for an object file format which
1131 supports arbitrary expressions, such as IEEE-695. */
1132 if (!(seg_left == absolute_section
1133 && seg_right == absolute_section)
1134 && !(op == O_eq || op == O_ne)
1135 && !((op == O_subtract
1136 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1137 && seg_left == seg_right
1138 && (seg_left != undefined_section
1139 || add_symbol == op_symbol)))
1141 /* Don't emit messages unless we're finalizing the symbol value,
1142 otherwise we may get the same message multiple times. */
1143 if (finalize_syms)
1144 report_op_error (symp, add_symbol, op_symbol);
1145 /* However do not move the symbol into the absolute section
1146 if it cannot currently be resolved - this would confuse
1147 other parts of the assembler into believing that the
1148 expression had been evaluated to zero. */
1149 else
1150 move_seg_ok = 0;
1153 if (move_seg_ok
1154 && (final_seg == expr_section || final_seg == undefined_section))
1155 final_seg = absolute_section;
1157 /* Check for division by zero. */
1158 if ((op == O_divide || op == O_modulus) && right == 0)
1160 /* If seg_right is not absolute_section, then we've
1161 already issued a warning about using a bad symbol. */
1162 if (seg_right == absolute_section && finalize_syms)
1164 char *file;
1165 unsigned int line;
1167 if (expr_symbol_where (symp, &file, &line))
1168 as_bad_where (file, line, _("division by zero"));
1169 else
1170 as_bad (_("division by zero when setting `%s'"),
1171 S_GET_NAME (symp));
1174 right = 1;
1177 switch (symp->sy_value.X_op)
1179 case O_multiply: left *= right; break;
1180 case O_divide: left /= right; break;
1181 case O_modulus: left %= right; break;
1182 case O_left_shift: left <<= right; break;
1183 case O_right_shift: left >>= right; break;
1184 case O_bit_inclusive_or: left |= right; break;
1185 case O_bit_or_not: left |= ~right; break;
1186 case O_bit_exclusive_or: left ^= right; break;
1187 case O_bit_and: left &= right; break;
1188 case O_add: left += right; break;
1189 case O_subtract: left -= right; break;
1190 case O_eq:
1191 case O_ne:
1192 left = (left == right && seg_left == seg_right
1193 && (seg_left != undefined_section
1194 || add_symbol == op_symbol)
1195 ? ~ (offsetT) 0 : 0);
1196 if (symp->sy_value.X_op == O_ne)
1197 left = ~left;
1198 break;
1199 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1200 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1201 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1202 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1203 case O_logical_and: left = left && right; break;
1204 case O_logical_or: left = left || right; break;
1205 default: abort ();
1208 final_val += symp->sy_frag->fr_address + left;
1209 if (final_seg == expr_section || final_seg == undefined_section)
1211 if (seg_left == undefined_section
1212 || seg_right == undefined_section)
1213 final_seg = undefined_section;
1214 else if (seg_left == absolute_section)
1215 final_seg = seg_right;
1216 else
1217 final_seg = seg_left;
1219 resolved = (symbol_resolved_p (add_symbol)
1220 && symbol_resolved_p (op_symbol));
1221 break;
1223 case O_register:
1224 case O_big:
1225 case O_illegal:
1226 /* Give an error (below) if not in expr_section. We don't
1227 want to worry about expr_section symbols, because they
1228 are fictional (they are created as part of expression
1229 resolution), and any problems may not actually mean
1230 anything. */
1231 break;
1234 symp->sy_resolving = 0;
1237 if (finalize_syms)
1238 S_SET_VALUE (symp, final_val);
1240 exit_dont_set_value:
1241 /* Always set the segment, even if not finalizing the value.
1242 The segment is used to determine whether a symbol is defined. */
1243 S_SET_SEGMENT (symp, final_seg);
1245 /* Don't worry if we can't resolve an expr_section symbol. */
1246 if (finalize_syms)
1248 if (resolved)
1249 symp->sy_resolved = 1;
1250 else if (S_GET_SEGMENT (symp) != expr_section)
1252 as_bad (_("can't resolve value for symbol `%s'"),
1253 S_GET_NAME (symp));
1254 symp->sy_resolved = 1;
1258 return final_val;
1261 static void resolve_local_symbol (const char *, PTR);
1263 /* A static function passed to hash_traverse. */
1265 static void
1266 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, PTR value)
1268 if (value != NULL)
1269 resolve_symbol_value (value);
1272 /* Resolve all local symbols. */
1274 void
1275 resolve_local_symbol_values (void)
1277 hash_traverse (local_hash, resolve_local_symbol);
1280 /* Obtain the current value of a symbol without changing any
1281 sub-expressions used. */
1284 snapshot_symbol (symbolS *symbolP, valueT *valueP, segT *segP, fragS **fragPP)
1286 if (LOCAL_SYMBOL_CHECK (symbolP))
1288 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1290 *valueP = locsym->lsy_value;
1291 *segP = locsym->lsy_section;
1292 *fragPP = local_symbol_get_frag (locsym);
1294 else
1296 expressionS expr = symbolP->sy_value;
1298 if (!symbolP->sy_resolved && expr.X_op != O_illegal)
1300 int resolved;
1302 if (symbolP->sy_resolving)
1303 return 0;
1304 symbolP->sy_resolving = 1;
1305 resolved = resolve_expression (&expr);
1306 symbolP->sy_resolving = 0;
1307 if (!resolved)
1308 return 0;
1310 switch (expr.X_op)
1312 case O_constant:
1313 case O_register:
1314 /* This check wouldn't be needed if pseudo_set() didn't set
1315 symbols equated to bare symbols to undefined_section. */
1316 if (symbolP->bsym->section != undefined_section
1317 || symbolP->sy_value.X_op != O_symbol)
1318 break;
1319 /* Fall thru. */
1320 case O_symbol:
1321 case O_symbol_rva:
1322 symbolP = expr.X_add_symbol;
1323 break;
1324 default:
1325 return 0;
1329 *valueP = expr.X_add_number;
1330 *segP = symbolP->bsym->section;
1331 *fragPP = symbolP->sy_frag;
1333 if (*segP == expr_section)
1334 switch (expr.X_op)
1336 case O_constant: *segP = absolute_section; break;
1337 case O_register: *segP = reg_section; break;
1338 default: break;
1342 return 1;
1345 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1346 They are *really* local. That is, they go out of scope whenever we see a
1347 label that isn't local. Also, like fb labels, there can be multiple
1348 instances of a dollar label. Therefor, we name encode each instance with
1349 the instance number, keep a list of defined symbols separate from the real
1350 symbol table, and we treat these buggers as a sparse array. */
1352 static long *dollar_labels;
1353 static long *dollar_label_instances;
1354 static char *dollar_label_defines;
1355 static unsigned long dollar_label_count;
1356 static unsigned long dollar_label_max;
1359 dollar_label_defined (long label)
1361 long *i;
1363 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1365 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1366 if (*i == label)
1367 return dollar_label_defines[i - dollar_labels];
1369 /* If we get here, label isn't defined. */
1370 return 0;
1373 static long
1374 dollar_label_instance (long label)
1376 long *i;
1378 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1380 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1381 if (*i == label)
1382 return (dollar_label_instances[i - dollar_labels]);
1384 /* If we get here, we haven't seen the label before.
1385 Therefore its instance count is zero. */
1386 return 0;
1389 void
1390 dollar_label_clear (void)
1392 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1395 #define DOLLAR_LABEL_BUMP_BY 10
1397 void
1398 define_dollar_label (long label)
1400 long *i;
1402 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1403 if (*i == label)
1405 ++dollar_label_instances[i - dollar_labels];
1406 dollar_label_defines[i - dollar_labels] = 1;
1407 return;
1410 /* If we get to here, we don't have label listed yet. */
1412 if (dollar_labels == NULL)
1414 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1415 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1416 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1417 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1418 dollar_label_count = 0;
1420 else if (dollar_label_count == dollar_label_max)
1422 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1423 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1424 dollar_label_max * sizeof (long));
1425 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1426 dollar_label_max * sizeof (long));
1427 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1428 } /* if we needed to grow */
1430 dollar_labels[dollar_label_count] = label;
1431 dollar_label_instances[dollar_label_count] = 1;
1432 dollar_label_defines[dollar_label_count] = 1;
1433 ++dollar_label_count;
1436 /* Caller must copy returned name: we re-use the area for the next name.
1438 The mth occurence of label n: is turned into the symbol "Ln^Am"
1439 where n is the label number and m is the instance number. "L" makes
1440 it a label discarded unless debugging and "^A"('\1') ensures no
1441 ordinary symbol SHOULD get the same name as a local label
1442 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1444 fb labels get the same treatment, except that ^B is used in place
1445 of ^A. */
1447 char * /* Return local label name. */
1448 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1449 register int augend /* 0 for current instance, 1 for new instance. */)
1451 long i;
1452 /* Returned to caller, then copied. Used for created names ("4f"). */
1453 static char symbol_name_build[24];
1454 register char *p;
1455 register char *q;
1456 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1458 know (n >= 0);
1459 know (augend == 0 || augend == 1);
1460 p = symbol_name_build;
1461 #ifdef LOCAL_LABEL_PREFIX
1462 *p++ = LOCAL_LABEL_PREFIX;
1463 #endif
1464 *p++ = 'L';
1466 /* Next code just does sprintf( {}, "%d", n); */
1467 /* Label number. */
1468 q = symbol_name_temporary;
1469 for (*q++ = 0, i = n; i; ++q)
1471 *q = i % 10 + '0';
1472 i /= 10;
1474 while ((*p = *--q) != '\0')
1475 ++p;
1477 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1479 /* Instance number. */
1480 q = symbol_name_temporary;
1481 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1483 *q = i % 10 + '0';
1484 i /= 10;
1486 while ((*p++ = *--q) != '\0');;
1488 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1489 return symbol_name_build;
1492 /* Somebody else's idea of local labels. They are made by "n:" where n
1493 is any decimal digit. Refer to them with
1494 "nb" for previous (backward) n:
1495 or "nf" for next (forward) n:.
1497 We do a little better and let n be any number, not just a single digit, but
1498 since the other guy's assembler only does ten, we treat the first ten
1499 specially.
1501 Like someone else's assembler, we have one set of local label counters for
1502 entire assembly, not one set per (sub)segment like in most assemblers. This
1503 implies that one can refer to a label in another segment, and indeed some
1504 crufty compilers have done just that.
1506 Since there could be a LOT of these things, treat them as a sparse
1507 array. */
1509 #define FB_LABEL_SPECIAL (10)
1511 static long fb_low_counter[FB_LABEL_SPECIAL];
1512 static long *fb_labels;
1513 static long *fb_label_instances;
1514 static long fb_label_count;
1515 static long fb_label_max;
1517 /* This must be more than FB_LABEL_SPECIAL. */
1518 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1520 static void
1521 fb_label_init (void)
1523 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1526 /* Add one to the instance number of this fb label. */
1528 void
1529 fb_label_instance_inc (long label)
1531 long *i;
1533 if (label < FB_LABEL_SPECIAL)
1535 ++fb_low_counter[label];
1536 return;
1539 if (fb_labels != NULL)
1541 for (i = fb_labels + FB_LABEL_SPECIAL;
1542 i < fb_labels + fb_label_count; ++i)
1544 if (*i == label)
1546 ++fb_label_instances[i - fb_labels];
1547 return;
1548 } /* if we find it */
1549 } /* for each existing label */
1552 /* If we get to here, we don't have label listed yet. */
1554 if (fb_labels == NULL)
1556 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1557 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1558 fb_label_max = FB_LABEL_BUMP_BY;
1559 fb_label_count = FB_LABEL_SPECIAL;
1562 else if (fb_label_count == fb_label_max)
1564 fb_label_max += FB_LABEL_BUMP_BY;
1565 fb_labels = (long *) xrealloc ((char *) fb_labels,
1566 fb_label_max * sizeof (long));
1567 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1568 fb_label_max * sizeof (long));
1569 } /* if we needed to grow */
1571 fb_labels[fb_label_count] = label;
1572 fb_label_instances[fb_label_count] = 1;
1573 ++fb_label_count;
1576 static long
1577 fb_label_instance (long label)
1579 long *i;
1581 if (label < FB_LABEL_SPECIAL)
1583 return (fb_low_counter[label]);
1586 if (fb_labels != NULL)
1588 for (i = fb_labels + FB_LABEL_SPECIAL;
1589 i < fb_labels + fb_label_count; ++i)
1591 if (*i == label)
1593 return (fb_label_instances[i - fb_labels]);
1594 } /* if we find it */
1595 } /* for each existing label */
1598 /* We didn't find the label, so this must be a reference to the
1599 first instance. */
1600 return 0;
1603 /* Caller must copy returned name: we re-use the area for the next name.
1605 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1606 where n is the label number and m is the instance number. "L" makes
1607 it a label discarded unless debugging and "^B"('\2') ensures no
1608 ordinary symbol SHOULD get the same name as a local label
1609 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1611 dollar labels get the same treatment, except that ^A is used in
1612 place of ^B. */
1614 char * /* Return local label name. */
1615 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1616 long augend /* 0 for nb, 1 for n:, nf. */)
1618 long i;
1619 /* Returned to caller, then copied. Used for created names ("4f"). */
1620 static char symbol_name_build[24];
1621 register char *p;
1622 register char *q;
1623 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1625 know (n >= 0);
1626 #ifdef TC_MMIX
1627 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1628 #else
1629 know ((unsigned long) augend <= 1);
1630 #endif
1631 p = symbol_name_build;
1632 #ifdef LOCAL_LABEL_PREFIX
1633 *p++ = LOCAL_LABEL_PREFIX;
1634 #endif
1635 *p++ = 'L';
1637 /* Next code just does sprintf( {}, "%d", n); */
1638 /* Label number. */
1639 q = symbol_name_temporary;
1640 for (*q++ = 0, i = n; i; ++q)
1642 *q = i % 10 + '0';
1643 i /= 10;
1645 while ((*p = *--q) != '\0')
1646 ++p;
1648 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1650 /* Instance number. */
1651 q = symbol_name_temporary;
1652 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1654 *q = i % 10 + '0';
1655 i /= 10;
1657 while ((*p++ = *--q) != '\0');;
1659 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1660 return (symbol_name_build);
1663 /* Decode name that may have been generated by foo_label_name() above.
1664 If the name wasn't generated by foo_label_name(), then return it
1665 unaltered. This is used for error messages. */
1667 char *
1668 decode_local_label_name (char *s)
1670 char *p;
1671 char *symbol_decode;
1672 int label_number;
1673 int instance_number;
1674 char *type;
1675 const char *message_format;
1676 int index = 0;
1678 #ifdef LOCAL_LABEL_PREFIX
1679 if (s[index] == LOCAL_LABEL_PREFIX)
1680 ++index;
1681 #endif
1683 if (s[index] != 'L')
1684 return s;
1686 for (label_number = 0, p = s + index + 1; ISDIGIT (*p); ++p)
1687 label_number = (10 * label_number) + *p - '0';
1689 if (*p == DOLLAR_LABEL_CHAR)
1690 type = "dollar";
1691 else if (*p == LOCAL_LABEL_CHAR)
1692 type = "fb";
1693 else
1694 return s;
1696 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1697 instance_number = (10 * instance_number) + *p - '0';
1699 message_format = _("\"%d\" (instance number %d of a %s label)");
1700 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1701 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1703 return symbol_decode;
1706 /* Get the value of a symbol. */
1708 valueT
1709 S_GET_VALUE (symbolS *s)
1711 if (LOCAL_SYMBOL_CHECK (s))
1712 return resolve_symbol_value (s);
1714 if (!s->sy_resolved)
1716 valueT val = resolve_symbol_value (s);
1717 if (!finalize_syms)
1718 return val;
1720 if (s->sy_value.X_op != O_constant)
1722 static symbolS *recur;
1724 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1725 may call S_GET_VALUE. We use a static symbol to avoid the
1726 immediate recursion. */
1727 if (recur == s)
1728 return (valueT) s->sy_value.X_add_number;
1729 recur = s;
1730 if (! s->sy_resolved
1731 || s->sy_value.X_op != O_symbol
1732 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1733 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1734 S_GET_NAME (s));
1735 recur = NULL;
1737 return (valueT) s->sy_value.X_add_number;
1740 /* Set the value of a symbol. */
1742 void
1743 S_SET_VALUE (symbolS *s, valueT val)
1745 if (LOCAL_SYMBOL_CHECK (s))
1747 ((struct local_symbol *) s)->lsy_value = val;
1748 return;
1751 s->sy_value.X_op = O_constant;
1752 s->sy_value.X_add_number = (offsetT) val;
1753 s->sy_value.X_unsigned = 0;
1756 void
1757 copy_symbol_attributes (symbolS *dest, symbolS *src)
1759 if (LOCAL_SYMBOL_CHECK (dest))
1760 dest = local_symbol_convert ((struct local_symbol *) dest);
1761 if (LOCAL_SYMBOL_CHECK (src))
1762 src = local_symbol_convert ((struct local_symbol *) src);
1764 /* In an expression, transfer the settings of these flags.
1765 The user can override later, of course. */
1766 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1767 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1769 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1770 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1771 #endif
1775 S_IS_FUNCTION (symbolS *s)
1777 flagword flags;
1779 if (LOCAL_SYMBOL_CHECK (s))
1780 return 0;
1782 flags = s->bsym->flags;
1784 return (flags & BSF_FUNCTION) != 0;
1788 S_IS_EXTERNAL (symbolS *s)
1790 flagword flags;
1792 if (LOCAL_SYMBOL_CHECK (s))
1793 return 0;
1795 flags = s->bsym->flags;
1797 /* Sanity check. */
1798 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1799 abort ();
1801 return (flags & BSF_GLOBAL) != 0;
1805 S_IS_WEAK (symbolS *s)
1807 if (LOCAL_SYMBOL_CHECK (s))
1808 return 0;
1809 return (s->bsym->flags & BSF_WEAK) != 0;
1813 S_IS_COMMON (symbolS *s)
1815 if (LOCAL_SYMBOL_CHECK (s))
1816 return 0;
1817 return bfd_is_com_section (s->bsym->section);
1821 S_IS_DEFINED (symbolS *s)
1823 if (LOCAL_SYMBOL_CHECK (s))
1824 return ((struct local_symbol *) s)->lsy_section != undefined_section;
1825 return s->bsym->section != undefined_section;
1829 #ifndef EXTERN_FORCE_RELOC
1830 #define EXTERN_FORCE_RELOC IS_ELF
1831 #endif
1833 /* Return true for symbols that should not be reduced to section
1834 symbols or eliminated from expressions, because they may be
1835 overridden by the linker. */
1837 S_FORCE_RELOC (symbolS *s, int strict)
1839 if (LOCAL_SYMBOL_CHECK (s))
1840 return ((struct local_symbol *) s)->lsy_section == undefined_section;
1842 return ((strict
1843 && ((s->bsym->flags & BSF_WEAK) != 0
1844 || (EXTERN_FORCE_RELOC
1845 && (s->bsym->flags & BSF_GLOBAL) != 0)))
1846 || s->bsym->section == undefined_section
1847 || bfd_is_com_section (s->bsym->section));
1851 S_IS_DEBUG (symbolS *s)
1853 if (LOCAL_SYMBOL_CHECK (s))
1854 return 0;
1855 if (s->bsym->flags & BSF_DEBUGGING)
1856 return 1;
1857 return 0;
1861 S_IS_LOCAL (symbolS *s)
1863 flagword flags;
1864 const char *name;
1866 if (LOCAL_SYMBOL_CHECK (s))
1867 return 1;
1869 flags = s->bsym->flags;
1871 /* Sanity check. */
1872 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1873 abort ();
1875 if (bfd_get_section (s->bsym) == reg_section)
1876 return 1;
1878 if (flag_strip_local_absolute
1879 /* Keep BSF_FILE symbols in order to allow debuggers to identify
1880 the source file even when the object file is stripped. */
1881 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
1882 && bfd_get_section (s->bsym) == absolute_section)
1883 return 1;
1885 name = S_GET_NAME (s);
1886 return (name != NULL
1887 && ! S_IS_DEBUG (s)
1888 && (strchr (name, DOLLAR_LABEL_CHAR)
1889 || strchr (name, LOCAL_LABEL_CHAR)
1890 || (! flag_keep_locals
1891 && (bfd_is_local_label (stdoutput, s->bsym)
1892 || (flag_mri
1893 && name[0] == '?'
1894 && name[1] == '?')))));
1898 S_IS_STABD (symbolS *s)
1900 return S_GET_NAME (s) == 0;
1904 S_IS_VOLATILE (const symbolS *s)
1906 if (LOCAL_SYMBOL_CHECK (s))
1907 return 0;
1908 return s->sy_volatile;
1912 S_IS_FORWARD_REF (const symbolS *s)
1914 if (LOCAL_SYMBOL_CHECK (s))
1915 return 0;
1916 return s->sy_forward_ref;
1919 const char *
1920 S_GET_NAME (symbolS *s)
1922 if (LOCAL_SYMBOL_CHECK (s))
1923 return ((struct local_symbol *) s)->lsy_name;
1924 return s->bsym->name;
1927 segT
1928 S_GET_SEGMENT (symbolS *s)
1930 if (LOCAL_SYMBOL_CHECK (s))
1931 return ((struct local_symbol *) s)->lsy_section;
1932 return s->bsym->section;
1935 void
1936 S_SET_SEGMENT (symbolS *s, segT seg)
1938 /* Don't reassign section symbols. The direct reason is to prevent seg
1939 faults assigning back to const global symbols such as *ABS*, but it
1940 shouldn't happen anyway. */
1942 if (LOCAL_SYMBOL_CHECK (s))
1944 if (seg == reg_section)
1945 s = local_symbol_convert ((struct local_symbol *) s);
1946 else
1948 ((struct local_symbol *) s)->lsy_section = seg;
1949 return;
1953 if (s->bsym->flags & BSF_SECTION_SYM)
1955 if (s->bsym->section != seg)
1956 abort ();
1958 else
1959 s->bsym->section = seg;
1962 void
1963 S_SET_EXTERNAL (symbolS *s)
1965 if (LOCAL_SYMBOL_CHECK (s))
1966 s = local_symbol_convert ((struct local_symbol *) s);
1967 if ((s->bsym->flags & BSF_WEAK) != 0)
1969 /* Let .weak override .global. */
1970 return;
1972 if (s->bsym->flags & BSF_SECTION_SYM)
1974 char * file;
1975 unsigned int line;
1977 /* Do not reassign section symbols. */
1978 as_where (& file, & line);
1979 as_warn_where (file, line,
1980 _("section symbols are already global"));
1981 return;
1983 s->bsym->flags |= BSF_GLOBAL;
1984 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
1986 #ifdef USE_UNIQUE
1987 if (! an_external_name && S_GET_NAME(s)[0] != '.')
1988 an_external_name = S_GET_NAME (s);
1989 #endif
1992 void
1993 S_CLEAR_EXTERNAL (symbolS *s)
1995 if (LOCAL_SYMBOL_CHECK (s))
1996 return;
1997 if ((s->bsym->flags & BSF_WEAK) != 0)
1999 /* Let .weak override. */
2000 return;
2002 s->bsym->flags |= BSF_LOCAL;
2003 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2006 void
2007 S_SET_WEAK (symbolS *s)
2009 if (LOCAL_SYMBOL_CHECK (s))
2010 s = local_symbol_convert ((struct local_symbol *) s);
2011 s->bsym->flags |= BSF_WEAK;
2012 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2015 void
2016 S_SET_THREAD_LOCAL (symbolS *s)
2018 if (LOCAL_SYMBOL_CHECK (s))
2019 s = local_symbol_convert ((struct local_symbol *) s);
2020 if (bfd_is_com_section (s->bsym->section)
2021 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2022 return;
2023 s->bsym->flags |= BSF_THREAD_LOCAL;
2024 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2025 as_bad (_("Accessing function `%s' as thread-local object"),
2026 S_GET_NAME (s));
2027 else if (! bfd_is_und_section (s->bsym->section)
2028 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2029 as_bad (_("Accessing `%s' as thread-local object"),
2030 S_GET_NAME (s));
2033 void
2034 S_SET_NAME (symbolS *s, const char *name)
2036 if (LOCAL_SYMBOL_CHECK (s))
2038 ((struct local_symbol *) s)->lsy_name = name;
2039 return;
2041 s->bsym->name = name;
2044 void
2045 S_SET_VOLATILE (symbolS *s)
2047 if (LOCAL_SYMBOL_CHECK (s))
2048 s = local_symbol_convert ((struct local_symbol *) s);
2049 s->sy_volatile = 1;
2052 void
2053 S_SET_FORWARD_REF (symbolS *s)
2055 if (LOCAL_SYMBOL_CHECK (s))
2056 s = local_symbol_convert ((struct local_symbol *) s);
2057 s->sy_forward_ref = 1;
2060 /* Return the previous symbol in a chain. */
2062 symbolS *
2063 symbol_previous (symbolS *s)
2065 if (LOCAL_SYMBOL_CHECK (s))
2066 abort ();
2067 return s->sy_previous;
2070 /* Return the next symbol in a chain. */
2072 symbolS *
2073 symbol_next (symbolS *s)
2075 if (LOCAL_SYMBOL_CHECK (s))
2076 abort ();
2077 return s->sy_next;
2080 /* Return a pointer to the value of a symbol as an expression. */
2082 expressionS *
2083 symbol_get_value_expression (symbolS *s)
2085 if (LOCAL_SYMBOL_CHECK (s))
2086 s = local_symbol_convert ((struct local_symbol *) s);
2087 return &s->sy_value;
2090 /* Set the value of a symbol to an expression. */
2092 void
2093 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2095 if (LOCAL_SYMBOL_CHECK (s))
2096 s = local_symbol_convert ((struct local_symbol *) s);
2097 s->sy_value = *exp;
2100 /* Return a pointer to the X_add_number component of a symbol. */
2102 offsetT *
2103 symbol_X_add_number (symbolS *s)
2105 if (LOCAL_SYMBOL_CHECK (s))
2106 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2108 return &s->sy_value.X_add_number;
2111 /* Set the value of SYM to the current position in the current segment. */
2113 void
2114 symbol_set_value_now (symbolS *sym)
2116 S_SET_SEGMENT (sym, now_seg);
2117 S_SET_VALUE (sym, frag_now_fix ());
2118 symbol_set_frag (sym, frag_now);
2121 /* Set the frag of a symbol. */
2123 void
2124 symbol_set_frag (symbolS *s, fragS *f)
2126 if (LOCAL_SYMBOL_CHECK (s))
2128 local_symbol_set_frag ((struct local_symbol *) s, f);
2129 return;
2131 s->sy_frag = f;
2134 /* Return the frag of a symbol. */
2136 fragS *
2137 symbol_get_frag (symbolS *s)
2139 if (LOCAL_SYMBOL_CHECK (s))
2140 return local_symbol_get_frag ((struct local_symbol *) s);
2141 return s->sy_frag;
2144 /* Mark a symbol as having been used. */
2146 void
2147 symbol_mark_used (symbolS *s)
2149 if (LOCAL_SYMBOL_CHECK (s))
2150 return;
2151 s->sy_used = 1;
2154 /* Clear the mark of whether a symbol has been used. */
2156 void
2157 symbol_clear_used (symbolS *s)
2159 if (LOCAL_SYMBOL_CHECK (s))
2160 s = local_symbol_convert ((struct local_symbol *) s);
2161 s->sy_used = 0;
2164 /* Return whether a symbol has been used. */
2167 symbol_used_p (symbolS *s)
2169 if (LOCAL_SYMBOL_CHECK (s))
2170 return 1;
2171 return s->sy_used;
2174 /* Mark a symbol as having been used in a reloc. */
2176 void
2177 symbol_mark_used_in_reloc (symbolS *s)
2179 if (LOCAL_SYMBOL_CHECK (s))
2180 s = local_symbol_convert ((struct local_symbol *) s);
2181 s->sy_used_in_reloc = 1;
2184 /* Clear the mark of whether a symbol has been used in a reloc. */
2186 void
2187 symbol_clear_used_in_reloc (symbolS *s)
2189 if (LOCAL_SYMBOL_CHECK (s))
2190 return;
2191 s->sy_used_in_reloc = 0;
2194 /* Return whether a symbol has been used in a reloc. */
2197 symbol_used_in_reloc_p (symbolS *s)
2199 if (LOCAL_SYMBOL_CHECK (s))
2200 return 0;
2201 return s->sy_used_in_reloc;
2204 /* Mark a symbol as an MRI common symbol. */
2206 void
2207 symbol_mark_mri_common (symbolS *s)
2209 if (LOCAL_SYMBOL_CHECK (s))
2210 s = local_symbol_convert ((struct local_symbol *) s);
2211 s->sy_mri_common = 1;
2214 /* Clear the mark of whether a symbol is an MRI common symbol. */
2216 void
2217 symbol_clear_mri_common (symbolS *s)
2219 if (LOCAL_SYMBOL_CHECK (s))
2220 return;
2221 s->sy_mri_common = 0;
2224 /* Return whether a symbol is an MRI common symbol. */
2227 symbol_mri_common_p (symbolS *s)
2229 if (LOCAL_SYMBOL_CHECK (s))
2230 return 0;
2231 return s->sy_mri_common;
2234 /* Mark a symbol as having been written. */
2236 void
2237 symbol_mark_written (symbolS *s)
2239 if (LOCAL_SYMBOL_CHECK (s))
2240 return;
2241 s->written = 1;
2244 /* Clear the mark of whether a symbol has been written. */
2246 void
2247 symbol_clear_written (symbolS *s)
2249 if (LOCAL_SYMBOL_CHECK (s))
2250 return;
2251 s->written = 0;
2254 /* Return whether a symbol has been written. */
2257 symbol_written_p (symbolS *s)
2259 if (LOCAL_SYMBOL_CHECK (s))
2260 return 0;
2261 return s->written;
2264 /* Mark a symbol has having been resolved. */
2266 void
2267 symbol_mark_resolved (symbolS *s)
2269 if (LOCAL_SYMBOL_CHECK (s))
2271 local_symbol_mark_resolved ((struct local_symbol *) s);
2272 return;
2274 s->sy_resolved = 1;
2277 /* Return whether a symbol has been resolved. */
2280 symbol_resolved_p (symbolS *s)
2282 if (LOCAL_SYMBOL_CHECK (s))
2283 return local_symbol_resolved_p ((struct local_symbol *) s);
2284 return s->sy_resolved;
2287 /* Return whether a symbol is a section symbol. */
2290 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2292 if (LOCAL_SYMBOL_CHECK (s))
2293 return 0;
2294 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2297 /* Return whether a symbol is equated to another symbol. */
2300 symbol_equated_p (symbolS *s)
2302 if (LOCAL_SYMBOL_CHECK (s))
2303 return 0;
2304 return s->sy_value.X_op == O_symbol;
2307 /* Return whether a symbol is equated to another symbol, and should be
2308 treated specially when writing out relocs. */
2311 symbol_equated_reloc_p (symbolS *s)
2313 if (LOCAL_SYMBOL_CHECK (s))
2314 return 0;
2315 /* X_op_symbol, normally not used for O_symbol, is set by
2316 resolve_symbol_value to flag expression syms that have been
2317 equated. */
2318 return (s->sy_value.X_op == O_symbol
2319 #if defined (OBJ_COFF) && defined (TE_PE)
2320 && ! S_IS_WEAK (s)
2321 #endif
2322 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2323 || ! S_IS_DEFINED (s)
2324 || S_IS_COMMON (s)));
2327 /* Return whether a symbol has a constant value. */
2330 symbol_constant_p (symbolS *s)
2332 if (LOCAL_SYMBOL_CHECK (s))
2333 return 1;
2334 return s->sy_value.X_op == O_constant;
2337 /* Return the BFD symbol for a symbol. */
2339 asymbol *
2340 symbol_get_bfdsym (symbolS *s)
2342 if (LOCAL_SYMBOL_CHECK (s))
2343 s = local_symbol_convert ((struct local_symbol *) s);
2344 return s->bsym;
2347 /* Set the BFD symbol for a symbol. */
2349 void
2350 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2352 if (LOCAL_SYMBOL_CHECK (s))
2353 s = local_symbol_convert ((struct local_symbol *) s);
2354 /* Usually, it is harmless to reset a symbol to a BFD section
2355 symbol. For example, obj_elf_change_section sets the BFD symbol
2356 of an old symbol with the newly created section symbol. But when
2357 we have multiple sections with the same name, the newly created
2358 section may have the same name as an old section. We check if the
2359 old symbol has been already marked as a section symbol before
2360 resetting it. */
2361 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2362 s->bsym = bsym;
2363 /* else XXX - What do we do now ? */
2366 #ifdef OBJ_SYMFIELD_TYPE
2368 /* Get a pointer to the object format information for a symbol. */
2370 OBJ_SYMFIELD_TYPE *
2371 symbol_get_obj (symbolS *s)
2373 if (LOCAL_SYMBOL_CHECK (s))
2374 s = local_symbol_convert ((struct local_symbol *) s);
2375 return &s->sy_obj;
2378 /* Set the object format information for a symbol. */
2380 void
2381 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2383 if (LOCAL_SYMBOL_CHECK (s))
2384 s = local_symbol_convert ((struct local_symbol *) s);
2385 s->sy_obj = *o;
2388 #endif /* OBJ_SYMFIELD_TYPE */
2390 #ifdef TC_SYMFIELD_TYPE
2392 /* Get a pointer to the processor information for a symbol. */
2394 TC_SYMFIELD_TYPE *
2395 symbol_get_tc (symbolS *s)
2397 if (LOCAL_SYMBOL_CHECK (s))
2398 s = local_symbol_convert ((struct local_symbol *) s);
2399 return &s->sy_tc;
2402 /* Set the processor information for a symbol. */
2404 void
2405 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2407 if (LOCAL_SYMBOL_CHECK (s))
2408 s = local_symbol_convert ((struct local_symbol *) s);
2409 s->sy_tc = *o;
2412 #endif /* TC_SYMFIELD_TYPE */
2414 void
2415 symbol_begin (void)
2417 symbol_lastP = NULL;
2418 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2419 sy_hash = hash_new ();
2420 local_hash = hash_new ();
2422 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2423 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2424 abs_symbol.bsym = bfd_abs_section.symbol;
2425 #endif
2426 abs_symbol.sy_value.X_op = O_constant;
2427 abs_symbol.sy_frag = &zero_address_frag;
2429 if (LOCAL_LABELS_FB)
2430 fb_label_init ();
2433 int indent_level;
2435 /* Maximum indent level.
2436 Available for modification inside a gdb session. */
2437 static int max_indent_level = 8;
2439 void
2440 print_symbol_value_1 (FILE *file, symbolS *sym)
2442 const char *name = S_GET_NAME (sym);
2443 if (!name || !name[0])
2444 name = "(unnamed)";
2445 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2447 if (LOCAL_SYMBOL_CHECK (sym))
2449 struct local_symbol *locsym = (struct local_symbol *) sym;
2450 if (local_symbol_get_frag (locsym) != &zero_address_frag
2451 && local_symbol_get_frag (locsym) != NULL)
2452 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2453 if (local_symbol_resolved_p (locsym))
2454 fprintf (file, " resolved");
2455 fprintf (file, " local");
2457 else
2459 if (sym->sy_frag != &zero_address_frag)
2460 fprintf (file, " frag %lx", (long) sym->sy_frag);
2461 if (sym->written)
2462 fprintf (file, " written");
2463 if (sym->sy_resolved)
2464 fprintf (file, " resolved");
2465 else if (sym->sy_resolving)
2466 fprintf (file, " resolving");
2467 if (sym->sy_used_in_reloc)
2468 fprintf (file, " used-in-reloc");
2469 if (sym->sy_used)
2470 fprintf (file, " used");
2471 if (S_IS_LOCAL (sym))
2472 fprintf (file, " local");
2473 if (S_IS_EXTERNAL (sym))
2474 fprintf (file, " extern");
2475 if (S_IS_DEBUG (sym))
2476 fprintf (file, " debug");
2477 if (S_IS_DEFINED (sym))
2478 fprintf (file, " defined");
2480 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2481 if (symbol_resolved_p (sym))
2483 segT s = S_GET_SEGMENT (sym);
2485 if (s != undefined_section
2486 && s != expr_section)
2487 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2489 else if (indent_level < max_indent_level
2490 && S_GET_SEGMENT (sym) != undefined_section)
2492 indent_level++;
2493 fprintf (file, "\n%*s<", indent_level * 4, "");
2494 if (LOCAL_SYMBOL_CHECK (sym))
2495 fprintf (file, "constant %lx",
2496 (long) ((struct local_symbol *) sym)->lsy_value);
2497 else
2498 print_expr_1 (file, &sym->sy_value);
2499 fprintf (file, ">");
2500 indent_level--;
2502 fflush (file);
2505 void
2506 print_symbol_value (symbolS *sym)
2508 indent_level = 0;
2509 print_symbol_value_1 (stderr, sym);
2510 fprintf (stderr, "\n");
2513 static void
2514 print_binary (FILE *file, const char *name, expressionS *exp)
2516 indent_level++;
2517 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2518 print_symbol_value_1 (file, exp->X_add_symbol);
2519 fprintf (file, ">\n%*s<", indent_level * 4, "");
2520 print_symbol_value_1 (file, exp->X_op_symbol);
2521 fprintf (file, ">");
2522 indent_level--;
2525 void
2526 print_expr_1 (FILE *file, expressionS *exp)
2528 fprintf (file, "expr %lx ", (long) exp);
2529 switch (exp->X_op)
2531 case O_illegal:
2532 fprintf (file, "illegal");
2533 break;
2534 case O_absent:
2535 fprintf (file, "absent");
2536 break;
2537 case O_constant:
2538 fprintf (file, "constant %lx", (long) exp->X_add_number);
2539 break;
2540 case O_symbol:
2541 indent_level++;
2542 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2543 print_symbol_value_1 (file, exp->X_add_symbol);
2544 fprintf (file, ">");
2545 maybe_print_addnum:
2546 if (exp->X_add_number)
2547 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2548 (long) exp->X_add_number);
2549 indent_level--;
2550 break;
2551 case O_register:
2552 fprintf (file, "register #%d", (int) exp->X_add_number);
2553 break;
2554 case O_big:
2555 fprintf (file, "big");
2556 break;
2557 case O_uminus:
2558 fprintf (file, "uminus -<");
2559 indent_level++;
2560 print_symbol_value_1 (file, exp->X_add_symbol);
2561 fprintf (file, ">");
2562 goto maybe_print_addnum;
2563 case O_bit_not:
2564 fprintf (file, "bit_not");
2565 break;
2566 case O_multiply:
2567 print_binary (file, "multiply", exp);
2568 break;
2569 case O_divide:
2570 print_binary (file, "divide", exp);
2571 break;
2572 case O_modulus:
2573 print_binary (file, "modulus", exp);
2574 break;
2575 case O_left_shift:
2576 print_binary (file, "lshift", exp);
2577 break;
2578 case O_right_shift:
2579 print_binary (file, "rshift", exp);
2580 break;
2581 case O_bit_inclusive_or:
2582 print_binary (file, "bit_ior", exp);
2583 break;
2584 case O_bit_exclusive_or:
2585 print_binary (file, "bit_xor", exp);
2586 break;
2587 case O_bit_and:
2588 print_binary (file, "bit_and", exp);
2589 break;
2590 case O_eq:
2591 print_binary (file, "eq", exp);
2592 break;
2593 case O_ne:
2594 print_binary (file, "ne", exp);
2595 break;
2596 case O_lt:
2597 print_binary (file, "lt", exp);
2598 break;
2599 case O_le:
2600 print_binary (file, "le", exp);
2601 break;
2602 case O_ge:
2603 print_binary (file, "ge", exp);
2604 break;
2605 case O_gt:
2606 print_binary (file, "gt", exp);
2607 break;
2608 case O_logical_and:
2609 print_binary (file, "logical_and", exp);
2610 break;
2611 case O_logical_or:
2612 print_binary (file, "logical_or", exp);
2613 break;
2614 case O_add:
2615 indent_level++;
2616 fprintf (file, "add\n%*s<", indent_level * 4, "");
2617 print_symbol_value_1 (file, exp->X_add_symbol);
2618 fprintf (file, ">\n%*s<", indent_level * 4, "");
2619 print_symbol_value_1 (file, exp->X_op_symbol);
2620 fprintf (file, ">");
2621 goto maybe_print_addnum;
2622 case O_subtract:
2623 indent_level++;
2624 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2625 print_symbol_value_1 (file, exp->X_add_symbol);
2626 fprintf (file, ">\n%*s<", indent_level * 4, "");
2627 print_symbol_value_1 (file, exp->X_op_symbol);
2628 fprintf (file, ">");
2629 goto maybe_print_addnum;
2630 default:
2631 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2632 break;
2634 fflush (stdout);
2637 void
2638 print_expr (expressionS *exp)
2640 print_expr_1 (stderr, exp);
2641 fprintf (stderr, "\n");
2644 void
2645 symbol_print_statistics (FILE *file)
2647 hash_print_statistics (file, "symbol table", sy_hash);
2648 hash_print_statistics (file, "mini local symbol table", local_hash);
2649 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2650 local_symbol_count, local_symbol_conversion_count);