* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / gas / symbols.c
blob679534d48a03232f279aa1b20a2fc9dbae71ba38
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, 2006, 2007, 2008, 2009, 2010,
4 2011 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 3, 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"
26 #include "safe-ctype.h"
27 #include "obstack.h" /* For "symbols.h" */
28 #include "subsegs.h"
29 #include "struc-symbol.h"
31 /* This is non-zero if symbols are case sensitive, which is the
32 default. */
33 int symbols_case_sensitive = 1;
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words;
37 #endif
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control *sy_hash;
42 /* Table of local symbols. */
43 static struct hash_control *local_hash;
45 /* Below are commented in "symbols.h". */
46 symbolS *symbol_rootP;
47 symbolS *symbol_lastP;
48 symbolS abs_symbol;
49 symbolS dot_symbol;
51 #ifdef DEBUG_SYMS
52 #define debug_verify_symchain verify_symbol_chain
53 #else
54 #define debug_verify_symchain(root, last) ((void) 0)
55 #endif
57 #define DOLLAR_LABEL_CHAR '\001'
58 #define LOCAL_LABEL_CHAR '\002'
60 #ifndef TC_LABEL_IS_LOCAL
61 #define TC_LABEL_IS_LOCAL(name) 0
62 #endif
64 struct obstack notes;
65 #ifdef TE_PE
66 /* The name of an external symbol which is
67 used to make weak PE symbol names unique. */
68 const char * an_external_name;
69 #endif
71 static char *save_symbol_name (const char *);
72 static void fb_label_init (void);
73 static long dollar_label_instance (long);
74 static long fb_label_instance (long);
76 static void print_binary (FILE *, const char *, expressionS *);
78 /* Return a pointer to a new symbol. Die if we can't make a new
79 symbol. Fill in the symbol's values. Add symbol to end of symbol
80 chain.
82 This function should be called in the general case of creating a
83 symbol. However, if the output file symbol table has already been
84 set, and you are certain that this symbol won't be wanted in the
85 output file, you can call symbol_create. */
87 symbolS *
88 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
90 symbolS *symbolP = symbol_create (name, segment, valu, frag);
92 /* Link to end of symbol chain. */
94 extern int symbol_table_frozen;
95 if (symbol_table_frozen)
96 abort ();
98 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
100 return symbolP;
103 /* Save a symbol name on a permanent obstack, and convert it according
104 to the object file format. */
106 static char *
107 save_symbol_name (const char *name)
109 unsigned int name_length;
110 char *ret;
112 name_length = strlen (name) + 1; /* +1 for \0. */
113 obstack_grow (&notes, name, name_length);
114 ret = (char *) obstack_finish (&notes);
116 #ifdef tc_canonicalize_symbol_name
117 ret = tc_canonicalize_symbol_name (ret);
118 #endif
120 if (! symbols_case_sensitive)
122 char *s;
124 for (s = ret; *s != '\0'; s++)
125 *s = TOUPPER (*s);
128 return ret;
131 symbolS *
132 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
133 segT segment, /* Segment identifier (SEG_<something>). */
134 valueT valu, /* Symbol value. */
135 fragS *frag /* Associated fragment. */)
137 char *preserved_copy_of_name;
138 symbolS *symbolP;
140 preserved_copy_of_name = save_symbol_name (name);
142 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
144 /* symbol must be born in some fixed state. This seems as good as any. */
145 memset (symbolP, 0, sizeof (symbolS));
147 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
148 if (symbolP->bsym == NULL)
149 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
150 S_SET_NAME (symbolP, preserved_copy_of_name);
152 S_SET_SEGMENT (symbolP, segment);
153 S_SET_VALUE (symbolP, valu);
154 symbol_clear_list_pointers (symbolP);
156 symbolP->sy_frag = frag;
158 obj_symbol_new_hook (symbolP);
160 #ifdef tc_symbol_new_hook
161 tc_symbol_new_hook (symbolP);
162 #endif
164 return symbolP;
168 /* Local symbol support. If we can get away with it, we keep only a
169 small amount of information for local symbols. */
171 static symbolS *local_symbol_convert (struct local_symbol *);
173 /* Used for statistics. */
175 static unsigned long local_symbol_count;
176 static unsigned long local_symbol_conversion_count;
178 /* This macro is called with a symbol argument passed by reference.
179 It returns whether this is a local symbol. If necessary, it
180 changes its argument to the real symbol. */
182 #define LOCAL_SYMBOL_CHECK(s) \
183 (s->sy_flags.sy_local_symbol \
184 ? (local_symbol_converted_p ((struct local_symbol *) s) \
185 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
186 0) \
187 : 1) \
188 : 0)
190 /* Create a local symbol and insert it into the local hash table. */
192 struct local_symbol *
193 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
195 char *name_copy;
196 struct local_symbol *ret;
198 ++local_symbol_count;
200 name_copy = save_symbol_name (name);
202 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
203 ret->lsy_flags.sy_local_symbol = 1;
204 ret->lsy_flags.sy_resolved = 0;
205 ret->lsy_name = name_copy;
206 ret->lsy_section = section;
207 local_symbol_set_frag (ret, frag);
208 ret->lsy_value = val;
210 hash_jam (local_hash, name_copy, (void *) ret);
212 return ret;
215 /* Convert a local symbol into a real symbol. Note that we do not
216 reclaim the space used by the local symbol. */
218 static symbolS *
219 local_symbol_convert (struct local_symbol *locsym)
221 symbolS *ret;
223 gas_assert (locsym->lsy_flags.sy_local_symbol);
224 if (local_symbol_converted_p (locsym))
225 return local_symbol_get_real_symbol (locsym);
227 ++local_symbol_conversion_count;
229 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
230 local_symbol_get_frag (locsym));
232 if (local_symbol_resolved_p (locsym))
233 ret->sy_flags.sy_resolved = 1;
235 /* Local symbols are always either defined or used. */
236 ret->sy_flags.sy_used = 1;
238 #ifdef TC_LOCAL_SYMFIELD_CONVERT
239 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
240 #endif
242 symbol_table_insert (ret);
244 local_symbol_mark_converted (locsym);
245 local_symbol_set_real_symbol (locsym, ret);
247 hash_jam (local_hash, locsym->lsy_name, NULL);
249 return ret;
252 static void
253 define_sym_at_dot (symbolS *symbolP)
255 symbolP->sy_frag = frag_now;
256 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
257 S_SET_SEGMENT (symbolP, now_seg);
260 /* We have just seen "<name>:".
261 Creates a struct symbol unless it already exists.
263 Gripes if we are redefining a symbol incompatibly (and ignores it). */
265 symbolS *
266 colon (/* Just seen "x:" - rattle symbols & frags. */
267 const char *sym_name /* Symbol name, as a cannonical string. */
268 /* We copy this string: OK to alter later. */)
270 register symbolS *symbolP; /* Symbol we are working with. */
272 /* Sun local labels go out of scope whenever a non-local symbol is
273 defined. */
274 if (LOCAL_LABELS_DOLLAR
275 && !bfd_is_local_label_name (stdoutput, sym_name))
276 dollar_label_clear ();
278 #ifndef WORKING_DOT_WORD
279 if (new_broken_words)
281 struct broken_word *a;
282 int possible_bytes;
283 fragS *frag_tmp;
284 char *frag_opcode;
286 if (now_seg == absolute_section)
288 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
289 return NULL;
292 possible_bytes = (md_short_jump_size
293 + new_broken_words * md_long_jump_size);
295 frag_tmp = frag_now;
296 frag_opcode = frag_var (rs_broken_word,
297 possible_bytes,
298 possible_bytes,
299 (relax_substateT) 0,
300 (symbolS *) broken_words,
301 (offsetT) 0,
302 NULL);
304 /* We want to store the pointer to where to insert the jump
305 table in the fr_opcode of the rs_broken_word frag. This
306 requires a little hackery. */
307 while (frag_tmp
308 && (frag_tmp->fr_type != rs_broken_word
309 || frag_tmp->fr_opcode))
310 frag_tmp = frag_tmp->fr_next;
311 know (frag_tmp);
312 frag_tmp->fr_opcode = frag_opcode;
313 new_broken_words = 0;
315 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
316 a->dispfrag = frag_tmp;
318 #endif /* WORKING_DOT_WORD */
320 #ifdef obj_frob_colon
321 obj_frob_colon (sym_name);
322 #endif
324 if ((symbolP = symbol_find (sym_name)) != 0)
326 S_CLEAR_WEAKREFR (symbolP);
327 #ifdef RESOLVE_SYMBOL_REDEFINITION
328 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
329 return symbolP;
330 #endif
331 /* Now check for undefined symbols. */
332 if (LOCAL_SYMBOL_CHECK (symbolP))
334 struct local_symbol *locsym = (struct local_symbol *) symbolP;
336 if (locsym->lsy_section != undefined_section
337 && (local_symbol_get_frag (locsym) != frag_now
338 || locsym->lsy_section != now_seg
339 || locsym->lsy_value != frag_now_fix ()))
341 as_bad (_("symbol `%s' is already defined"), sym_name);
342 return symbolP;
345 locsym->lsy_section = now_seg;
346 local_symbol_set_frag (locsym, frag_now);
347 locsym->lsy_value = frag_now_fix ();
349 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
350 || S_IS_COMMON (symbolP)
351 || S_IS_VOLATILE (symbolP))
353 if (S_IS_VOLATILE (symbolP))
355 symbolP = symbol_clone (symbolP, 1);
356 S_SET_VALUE (symbolP, 0);
357 S_CLEAR_VOLATILE (symbolP);
359 if (S_GET_VALUE (symbolP) == 0)
361 define_sym_at_dot (symbolP);
362 #ifdef N_UNDF
363 know (N_UNDF == 0);
364 #endif /* if we have one, it better be zero. */
367 else
369 /* There are still several cases to check:
371 A .comm/.lcomm symbol being redefined as initialized
372 data is OK
374 A .comm/.lcomm symbol being redefined with a larger
375 size is also OK
377 This only used to be allowed on VMS gas, but Sun cc
378 on the sparc also depends on it. */
380 if (((!S_IS_DEBUG (symbolP)
381 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
382 && S_IS_EXTERNAL (symbolP))
383 || S_GET_SEGMENT (symbolP) == bss_section)
384 && (now_seg == data_section
385 || now_seg == bss_section
386 || now_seg == S_GET_SEGMENT (symbolP)))
388 /* Select which of the 2 cases this is. */
389 if (now_seg != data_section)
391 /* New .comm for prev .comm symbol.
393 If the new size is larger we just change its
394 value. If the new size is smaller, we ignore
395 this symbol. */
396 if (S_GET_VALUE (symbolP)
397 < ((unsigned) frag_now_fix ()))
399 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
402 else
404 /* It is a .comm/.lcomm being converted to initialized
405 data. */
406 define_sym_at_dot (symbolP);
409 else
411 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
412 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
413 static const char *od_buf = "";
414 #else
415 char od_buf[100];
416 od_buf[0] = '\0';
417 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
418 sprintf (od_buf, "%d.%d.",
419 S_GET_OTHER (symbolP),
420 S_GET_DESC (symbolP));
421 #endif
422 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
423 sym_name,
424 segment_name (S_GET_SEGMENT (symbolP)),
425 od_buf,
426 (long) S_GET_VALUE (symbolP));
428 } /* if the undefined symbol has no value */
430 else
432 /* Don't blow up if the definition is the same. */
433 if (!(frag_now == symbolP->sy_frag
434 && S_GET_VALUE (symbolP) == frag_now_fix ()
435 && S_GET_SEGMENT (symbolP) == now_seg))
437 as_bad (_("symbol `%s' is already defined"), sym_name);
438 symbolP = symbol_clone (symbolP, 0);
439 define_sym_at_dot (symbolP);
444 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
446 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
447 (valueT) frag_now_fix (),
448 frag_now);
450 else
452 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
453 frag_now);
455 symbol_table_insert (symbolP);
458 if (mri_common_symbol != NULL)
460 /* This symbol is actually being defined within an MRI common
461 section. This requires special handling. */
462 if (LOCAL_SYMBOL_CHECK (symbolP))
463 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
464 symbolP->sy_value.X_op = O_symbol;
465 symbolP->sy_value.X_add_symbol = mri_common_symbol;
466 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
467 symbolP->sy_frag = &zero_address_frag;
468 S_SET_SEGMENT (symbolP, expr_section);
469 symbolP->sy_flags.sy_mri_common = 1;
472 #ifdef tc_frob_label
473 tc_frob_label (symbolP);
474 #endif
475 #ifdef obj_frob_label
476 obj_frob_label (symbolP);
477 #endif
479 return symbolP;
482 /* Die if we can't insert the symbol. */
484 void
485 symbol_table_insert (symbolS *symbolP)
487 register const char *error_string;
489 know (symbolP);
490 know (S_GET_NAME (symbolP));
492 if (LOCAL_SYMBOL_CHECK (symbolP))
494 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
495 (void *) symbolP);
496 if (error_string != NULL)
497 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
498 S_GET_NAME (symbolP), error_string);
499 return;
502 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
504 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
505 S_GET_NAME (symbolP), error_string);
506 } /* on error */
509 /* If a symbol name does not exist, create it as undefined, and insert
510 it into the symbol table. Return a pointer to it. */
512 symbolS *
513 symbol_find_or_make (const char *name)
515 register symbolS *symbolP;
517 symbolP = symbol_find (name);
519 if (symbolP == NULL)
521 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
523 symbolP = md_undefined_symbol ((char *) name);
524 if (symbolP != NULL)
525 return symbolP;
527 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
528 (valueT) 0,
529 &zero_address_frag);
530 return symbolP;
533 symbolP = symbol_make (name);
535 symbol_table_insert (symbolP);
536 } /* if symbol wasn't found */
538 return (symbolP);
541 symbolS *
542 symbol_make (const char *name)
544 symbolS *symbolP;
546 /* Let the machine description default it, e.g. for register names. */
547 symbolP = md_undefined_symbol ((char *) name);
549 if (!symbolP)
550 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
552 return (symbolP);
555 symbolS *
556 symbol_clone (symbolS *orgsymP, int replace)
558 symbolS *newsymP;
559 asymbol *bsymorg, *bsymnew;
561 /* Make sure we never clone the dot special symbol. */
562 gas_assert (orgsymP != &dot_symbol);
564 /* Running local_symbol_convert on a clone that's not the one currently
565 in local_hash would incorrectly replace the hash entry. Thus the
566 symbol must be converted here. Note that the rest of the function
567 depends on not encountering an unconverted symbol. */
568 if (LOCAL_SYMBOL_CHECK (orgsymP))
569 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
570 bsymorg = orgsymP->bsym;
572 newsymP = (symbolS *) obstack_alloc (&notes, sizeof (*newsymP));
573 *newsymP = *orgsymP;
574 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
575 if (bsymnew == NULL)
576 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
577 newsymP->bsym = bsymnew;
578 bsymnew->name = bsymorg->name;
579 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
580 bsymnew->section = bsymorg->section;
581 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
582 bfd_asymbol_bfd (bsymnew), bsymnew);
584 #ifdef obj_symbol_clone_hook
585 obj_symbol_clone_hook (newsymP, orgsymP);
586 #endif
588 #ifdef tc_symbol_clone_hook
589 tc_symbol_clone_hook (newsymP, orgsymP);
590 #endif
592 if (replace)
594 if (symbol_rootP == orgsymP)
595 symbol_rootP = newsymP;
596 else if (orgsymP->sy_previous)
598 orgsymP->sy_previous->sy_next = newsymP;
599 orgsymP->sy_previous = NULL;
601 if (symbol_lastP == orgsymP)
602 symbol_lastP = newsymP;
603 else if (orgsymP->sy_next)
604 orgsymP->sy_next->sy_previous = newsymP;
606 /* Symbols that won't be output can't be external. */
607 S_CLEAR_EXTERNAL (orgsymP);
608 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
609 debug_verify_symchain (symbol_rootP, symbol_lastP);
611 symbol_table_insert (newsymP);
613 else
615 /* Symbols that won't be output can't be external. */
616 S_CLEAR_EXTERNAL (newsymP);
617 newsymP->sy_previous = newsymP->sy_next = newsymP;
620 return newsymP;
623 /* Referenced symbols, if they are forward references, need to be cloned
624 (without replacing the original) so that the value of the referenced
625 symbols at the point of use . */
627 #undef symbol_clone_if_forward_ref
628 symbolS *
629 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
631 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
633 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
634 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
636 if (symbolP->sy_flags.sy_forward_ref)
637 is_forward = 1;
639 if (is_forward)
641 /* assign_symbol() clones volatile symbols; pre-existing expressions
642 hold references to the original instance, but want the current
643 value. Just repeat the lookup. */
644 if (add_symbol && S_IS_VOLATILE (add_symbol))
645 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
646 if (op_symbol && S_IS_VOLATILE (op_symbol))
647 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
650 /* Re-using sy_resolving here, as this routine cannot get called from
651 symbol resolution code. */
652 if ((symbolP->bsym->section == expr_section
653 || symbolP->sy_flags.sy_forward_ref)
654 && !symbolP->sy_flags.sy_resolving)
656 symbolP->sy_flags.sy_resolving = 1;
657 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
658 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
659 symbolP->sy_flags.sy_resolving = 0;
662 if (symbolP->sy_flags.sy_forward_ref
663 || add_symbol != symbolP->sy_value.X_add_symbol
664 || op_symbol != symbolP->sy_value.X_op_symbol)
666 if (symbolP != &dot_symbol)
668 symbolP = symbol_clone (symbolP, 0);
669 symbolP->sy_flags.sy_resolving = 0;
671 else
673 symbolP = symbol_temp_new_now ();
674 #ifdef tc_new_dot_label
675 tc_new_dot_label (symbolP);
676 #endif
680 symbolP->sy_value.X_add_symbol = add_symbol;
681 symbolP->sy_value.X_op_symbol = op_symbol;
684 return symbolP;
687 symbolS *
688 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
690 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
693 symbolS *
694 symbol_temp_new_now (void)
696 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
699 symbolS *
700 symbol_temp_make (void)
702 return symbol_make (FAKE_LABEL_NAME);
705 /* Implement symbol table lookup.
706 In: A symbol's name as a string: '\0' can't be part of a symbol name.
707 Out: NULL if the name was not in the symbol table, else the address
708 of a struct symbol associated with that name. */
710 symbolS *
711 symbol_find_exact (const char *name)
713 return symbol_find_exact_noref (name, 0);
716 symbolS *
717 symbol_find_exact_noref (const char *name, int noref)
719 struct local_symbol *locsym;
720 symbolS* sym;
722 locsym = (struct local_symbol *) hash_find (local_hash, name);
723 if (locsym != NULL)
724 return (symbolS *) locsym;
726 sym = ((symbolS *) hash_find (sy_hash, name));
728 /* Any references to the symbol, except for the reference in
729 .weakref, must clear this flag, such that the symbol does not
730 turn into a weak symbol. Note that we don't have to handle the
731 local_symbol case, since a weakrefd is always promoted out of the
732 local_symbol table when it is turned into a weak symbol. */
733 if (sym && ! noref)
734 S_CLEAR_WEAKREFD (sym);
736 return sym;
739 symbolS *
740 symbol_find (const char *name)
742 return symbol_find_noref (name, 0);
745 symbolS *
746 symbol_find_noref (const char *name, int noref)
748 #ifdef tc_canonicalize_symbol_name
750 char *copy;
751 size_t len = strlen (name) + 1;
753 copy = (char *) alloca (len);
754 memcpy (copy, name, len);
755 name = tc_canonicalize_symbol_name (copy);
757 #endif
759 if (! symbols_case_sensitive)
761 char *copy;
762 const char *orig;
763 unsigned char c;
765 orig = name;
766 name = copy = (char *) alloca (strlen (name) + 1);
768 while ((c = *orig++) != '\0')
770 *copy++ = TOUPPER (c);
772 *copy = '\0';
775 return symbol_find_exact_noref (name, noref);
778 /* Once upon a time, symbols were kept in a singly linked list. At
779 least coff needs to be able to rearrange them from time to time, for
780 which a doubly linked list is much more convenient. Loic did these
781 as macros which seemed dangerous to me so they're now functions.
782 xoxorich. */
784 /* Link symbol ADDME after symbol TARGET in the chain. */
786 void
787 symbol_append (symbolS *addme, symbolS *target,
788 symbolS **rootPP, symbolS **lastPP)
790 if (LOCAL_SYMBOL_CHECK (addme))
791 abort ();
792 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
793 abort ();
795 if (target == NULL)
797 know (*rootPP == NULL);
798 know (*lastPP == NULL);
799 addme->sy_next = NULL;
800 addme->sy_previous = NULL;
801 *rootPP = addme;
802 *lastPP = addme;
803 return;
804 } /* if the list is empty */
806 if (target->sy_next != NULL)
808 target->sy_next->sy_previous = addme;
810 else
812 know (*lastPP == target);
813 *lastPP = addme;
814 } /* if we have a next */
816 addme->sy_next = target->sy_next;
817 target->sy_next = addme;
818 addme->sy_previous = target;
820 debug_verify_symchain (symbol_rootP, symbol_lastP);
823 /* Set the chain pointers of SYMBOL to null. */
825 void
826 symbol_clear_list_pointers (symbolS *symbolP)
828 if (LOCAL_SYMBOL_CHECK (symbolP))
829 abort ();
830 symbolP->sy_next = NULL;
831 symbolP->sy_previous = NULL;
834 /* Remove SYMBOLP from the list. */
836 void
837 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
839 if (LOCAL_SYMBOL_CHECK (symbolP))
840 abort ();
842 if (symbolP == *rootPP)
844 *rootPP = symbolP->sy_next;
845 } /* if it was the root */
847 if (symbolP == *lastPP)
849 *lastPP = symbolP->sy_previous;
850 } /* if it was the tail */
852 if (symbolP->sy_next != NULL)
854 symbolP->sy_next->sy_previous = symbolP->sy_previous;
855 } /* if not last */
857 if (symbolP->sy_previous != NULL)
859 symbolP->sy_previous->sy_next = symbolP->sy_next;
860 } /* if not first */
862 debug_verify_symchain (*rootPP, *lastPP);
865 /* Link symbol ADDME before symbol TARGET in the chain. */
867 void
868 symbol_insert (symbolS *addme, symbolS *target,
869 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
871 if (LOCAL_SYMBOL_CHECK (addme))
872 abort ();
873 if (LOCAL_SYMBOL_CHECK (target))
874 abort ();
876 if (target->sy_previous != NULL)
878 target->sy_previous->sy_next = addme;
880 else
882 know (*rootPP == target);
883 *rootPP = addme;
884 } /* if not first */
886 addme->sy_previous = target->sy_previous;
887 target->sy_previous = addme;
888 addme->sy_next = target;
890 debug_verify_symchain (*rootPP, *lastPP);
893 void
894 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
896 symbolS *symbolP = rootP;
898 if (symbolP == NULL)
899 return;
901 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
903 gas_assert (symbolP->bsym != NULL);
904 gas_assert (symbolP->sy_flags.sy_local_symbol == 0);
905 gas_assert (symbolP->sy_next->sy_previous == symbolP);
908 gas_assert (lastP == symbolP);
911 #ifdef OBJ_COMPLEX_RELC
913 static int
914 use_complex_relocs_for (symbolS * symp)
916 switch (symp->sy_value.X_op)
918 case O_constant:
919 return 0;
921 case O_symbol:
922 case O_symbol_rva:
923 case O_uminus:
924 case O_bit_not:
925 case O_logical_not:
926 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
927 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
929 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
930 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
931 return 0;
932 break;
934 case O_multiply:
935 case O_divide:
936 case O_modulus:
937 case O_left_shift:
938 case O_right_shift:
939 case O_bit_inclusive_or:
940 case O_bit_or_not:
941 case O_bit_exclusive_or:
942 case O_bit_and:
943 case O_add:
944 case O_subtract:
945 case O_eq:
946 case O_ne:
947 case O_lt:
948 case O_le:
949 case O_ge:
950 case O_gt:
951 case O_logical_and:
952 case O_logical_or:
954 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
955 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
957 (S_IS_COMMON (symp->sy_value.X_op_symbol)
958 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
960 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
961 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
962 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
963 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
964 return 0;
965 break;
967 default:
968 break;
970 return 1;
972 #endif
974 static void
975 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
977 char *file;
978 unsigned int line;
979 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
980 segT seg_right = S_GET_SEGMENT (right);
981 const char *opname;
983 switch (op)
985 default:
986 abort ();
987 return;
989 case O_uminus: opname = "-"; break;
990 case O_bit_not: opname = "~"; break;
991 case O_logical_not: opname = "!"; break;
992 case O_multiply: opname = "*"; break;
993 case O_divide: opname = "/"; break;
994 case O_modulus: opname = "%"; break;
995 case O_left_shift: opname = "<<"; break;
996 case O_right_shift: opname = ">>"; break;
997 case O_bit_inclusive_or: opname = "|"; break;
998 case O_bit_or_not: opname = "|~"; break;
999 case O_bit_exclusive_or: opname = "^"; break;
1000 case O_bit_and: opname = "&"; break;
1001 case O_add: opname = "+"; break;
1002 case O_subtract: opname = "-"; break;
1003 case O_eq: opname = "=="; break;
1004 case O_ne: opname = "!="; break;
1005 case O_lt: opname = "<"; break;
1006 case O_le: opname = "<="; break;
1007 case O_ge: opname = ">="; break;
1008 case O_gt: opname = ">"; break;
1009 case O_logical_and: opname = "&&"; break;
1010 case O_logical_or: opname = "||"; break;
1013 if (expr_symbol_where (symp, &file, &line))
1015 if (left)
1016 as_bad_where (file, line,
1017 _("invalid operands (%s and %s sections) for `%s'"),
1018 seg_left->name, seg_right->name, opname);
1019 else
1020 as_bad_where (file, line,
1021 _("invalid operand (%s section) for `%s'"),
1022 seg_right->name, opname);
1024 else
1026 const char *sname = S_GET_NAME (symp);
1028 if (left)
1029 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1030 seg_left->name, seg_right->name, opname, sname);
1031 else
1032 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1033 seg_right->name, opname, sname);
1037 /* Resolve the value of a symbol. This is called during the final
1038 pass over the symbol table to resolve any symbols with complex
1039 values. */
1041 valueT
1042 resolve_symbol_value (symbolS *symp)
1044 int resolved;
1045 valueT final_val = 0;
1046 segT final_seg;
1048 if (LOCAL_SYMBOL_CHECK (symp))
1050 struct local_symbol *locsym = (struct local_symbol *) symp;
1052 final_val = locsym->lsy_value;
1053 if (local_symbol_resolved_p (locsym))
1054 return final_val;
1056 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1058 if (finalize_syms)
1060 locsym->lsy_value = final_val;
1061 local_symbol_mark_resolved (locsym);
1064 return final_val;
1067 if (symp->sy_flags.sy_resolved)
1069 if (symp->sy_value.X_op == O_constant)
1070 return (valueT) symp->sy_value.X_add_number;
1071 else
1072 return 0;
1075 resolved = 0;
1076 final_seg = S_GET_SEGMENT (symp);
1078 if (symp->sy_flags.sy_resolving)
1080 if (finalize_syms)
1081 as_bad (_("symbol definition loop encountered at `%s'"),
1082 S_GET_NAME (symp));
1083 final_val = 0;
1084 resolved = 1;
1086 #ifdef OBJ_COMPLEX_RELC
1087 else if (final_seg == expr_section
1088 && use_complex_relocs_for (symp))
1090 symbolS * relc_symbol = NULL;
1091 char * relc_symbol_name = NULL;
1093 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1095 /* For debugging, print out conversion input & output. */
1096 #ifdef DEBUG_SYMS
1097 print_expr (& symp->sy_value);
1098 if (relc_symbol_name)
1099 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1100 #endif
1102 if (relc_symbol_name != NULL)
1103 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1104 0, & zero_address_frag);
1106 if (relc_symbol == NULL)
1108 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1109 S_GET_NAME (symp));
1110 resolved = 0;
1112 else
1114 symbol_table_insert (relc_symbol);
1116 /* S_CLEAR_EXTERNAL (relc_symbol); */
1117 if (symp->bsym->flags & BSF_SRELC)
1118 relc_symbol->bsym->flags |= BSF_SRELC;
1119 else
1120 relc_symbol->bsym->flags |= BSF_RELC;
1121 /* symp->bsym->flags |= BSF_RELC; */
1122 copy_symbol_attributes (symp, relc_symbol);
1123 symp->sy_value.X_op = O_symbol;
1124 symp->sy_value.X_add_symbol = relc_symbol;
1125 symp->sy_value.X_add_number = 0;
1126 resolved = 1;
1129 final_seg = undefined_section;
1130 goto exit_dont_set_value;
1132 #endif
1133 else
1135 symbolS *add_symbol, *op_symbol;
1136 offsetT left, right;
1137 segT seg_left, seg_right;
1138 operatorT op;
1139 int move_seg_ok;
1141 symp->sy_flags.sy_resolving = 1;
1143 /* Help out with CSE. */
1144 add_symbol = symp->sy_value.X_add_symbol;
1145 op_symbol = symp->sy_value.X_op_symbol;
1146 final_val = symp->sy_value.X_add_number;
1147 op = symp->sy_value.X_op;
1149 switch (op)
1151 default:
1152 BAD_CASE (op);
1153 break;
1155 case O_absent:
1156 final_val = 0;
1157 /* Fall through. */
1159 case O_constant:
1160 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1161 if (final_seg == expr_section)
1162 final_seg = absolute_section;
1163 /* Fall through. */
1165 case O_register:
1166 resolved = 1;
1167 break;
1169 case O_symbol:
1170 case O_symbol_rva:
1171 left = resolve_symbol_value (add_symbol);
1172 seg_left = S_GET_SEGMENT (add_symbol);
1173 if (finalize_syms)
1174 symp->sy_value.X_op_symbol = NULL;
1176 do_symbol:
1177 if (S_IS_WEAKREFR (symp))
1179 gas_assert (final_val == 0);
1180 if (S_IS_WEAKREFR (add_symbol))
1182 gas_assert (add_symbol->sy_value.X_op == O_symbol
1183 && add_symbol->sy_value.X_add_number == 0);
1184 add_symbol = add_symbol->sy_value.X_add_symbol;
1185 gas_assert (! S_IS_WEAKREFR (add_symbol));
1186 symp->sy_value.X_add_symbol = add_symbol;
1190 if (symp->sy_flags.sy_mri_common)
1192 /* This is a symbol inside an MRI common section. The
1193 relocation routines are going to handle it specially.
1194 Don't change the value. */
1195 resolved = symbol_resolved_p (add_symbol);
1196 break;
1199 if (finalize_syms && final_val == 0)
1201 if (LOCAL_SYMBOL_CHECK (add_symbol))
1202 add_symbol = local_symbol_convert ((struct local_symbol *)
1203 add_symbol);
1204 copy_symbol_attributes (symp, add_symbol);
1207 /* If we have equated this symbol to an undefined or common
1208 symbol, keep X_op set to O_symbol, and don't change
1209 X_add_number. This permits the routine which writes out
1210 relocation to detect this case, and convert the
1211 relocation to be against the symbol to which this symbol
1212 is equated. */
1213 if (! S_IS_DEFINED (add_symbol)
1214 #if defined (OBJ_COFF) && defined (TE_PE)
1215 || S_IS_WEAK (add_symbol)
1216 #endif
1217 || S_IS_COMMON (add_symbol))
1219 if (finalize_syms)
1221 symp->sy_value.X_op = O_symbol;
1222 symp->sy_value.X_add_symbol = add_symbol;
1223 symp->sy_value.X_add_number = final_val;
1224 /* Use X_op_symbol as a flag. */
1225 symp->sy_value.X_op_symbol = add_symbol;
1227 final_seg = seg_left;
1228 final_val = 0;
1229 resolved = symbol_resolved_p (add_symbol);
1230 symp->sy_flags.sy_resolving = 0;
1231 goto exit_dont_set_value;
1233 else if (finalize_syms
1234 && ((final_seg == expr_section && seg_left != expr_section)
1235 || symbol_shadow_p (symp)))
1237 /* If the symbol is an expression symbol, do similarly
1238 as for undefined and common syms above. Handles
1239 "sym +/- expr" where "expr" cannot be evaluated
1240 immediately, and we want relocations to be against
1241 "sym", eg. because it is weak. */
1242 symp->sy_value.X_op = O_symbol;
1243 symp->sy_value.X_add_symbol = add_symbol;
1244 symp->sy_value.X_add_number = final_val;
1245 symp->sy_value.X_op_symbol = add_symbol;
1246 final_seg = seg_left;
1247 final_val += symp->sy_frag->fr_address + left;
1248 resolved = symbol_resolved_p (add_symbol);
1249 symp->sy_flags.sy_resolving = 0;
1250 goto exit_dont_set_value;
1252 else
1254 final_val += symp->sy_frag->fr_address + left;
1255 if (final_seg == expr_section || final_seg == undefined_section)
1256 final_seg = seg_left;
1259 resolved = symbol_resolved_p (add_symbol);
1260 if (S_IS_WEAKREFR (symp))
1261 goto exit_dont_set_value;
1262 break;
1264 case O_uminus:
1265 case O_bit_not:
1266 case O_logical_not:
1267 left = resolve_symbol_value (add_symbol);
1268 seg_left = S_GET_SEGMENT (add_symbol);
1270 /* By reducing these to the relevant dyadic operator, we get
1271 !S -> S == 0 permitted on anything,
1272 -S -> 0 - S only permitted on absolute
1273 ~S -> S ^ ~0 only permitted on absolute */
1274 if (op != O_logical_not && seg_left != absolute_section
1275 && finalize_syms)
1276 report_op_error (symp, NULL, op, add_symbol);
1278 if (final_seg == expr_section || final_seg == undefined_section)
1279 final_seg = absolute_section;
1281 if (op == O_uminus)
1282 left = -left;
1283 else if (op == O_logical_not)
1284 left = !left;
1285 else
1286 left = ~left;
1288 final_val += left + symp->sy_frag->fr_address;
1290 resolved = symbol_resolved_p (add_symbol);
1291 break;
1293 case O_multiply:
1294 case O_divide:
1295 case O_modulus:
1296 case O_left_shift:
1297 case O_right_shift:
1298 case O_bit_inclusive_or:
1299 case O_bit_or_not:
1300 case O_bit_exclusive_or:
1301 case O_bit_and:
1302 case O_add:
1303 case O_subtract:
1304 case O_eq:
1305 case O_ne:
1306 case O_lt:
1307 case O_le:
1308 case O_ge:
1309 case O_gt:
1310 case O_logical_and:
1311 case O_logical_or:
1312 left = resolve_symbol_value (add_symbol);
1313 right = resolve_symbol_value (op_symbol);
1314 seg_left = S_GET_SEGMENT (add_symbol);
1315 seg_right = S_GET_SEGMENT (op_symbol);
1317 /* Simplify addition or subtraction of a constant by folding the
1318 constant into X_add_number. */
1319 if (op == O_add)
1321 if (seg_right == absolute_section)
1323 final_val += right;
1324 goto do_symbol;
1326 else if (seg_left == absolute_section)
1328 final_val += left;
1329 add_symbol = op_symbol;
1330 left = right;
1331 seg_left = seg_right;
1332 goto do_symbol;
1335 else if (op == O_subtract)
1337 if (seg_right == absolute_section)
1339 final_val -= right;
1340 goto do_symbol;
1344 move_seg_ok = 1;
1345 /* Equality and non-equality tests are permitted on anything.
1346 Subtraction, and other comparison operators are permitted if
1347 both operands are in the same section. Otherwise, both
1348 operands must be absolute. We already handled the case of
1349 addition or subtraction of a constant above. This will
1350 probably need to be changed for an object file format which
1351 supports arbitrary expressions, such as IEEE-695. */
1352 if (!(seg_left == absolute_section
1353 && seg_right == absolute_section)
1354 && !(op == O_eq || op == O_ne)
1355 && !((op == O_subtract
1356 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1357 && seg_left == seg_right
1358 && (seg_left != undefined_section
1359 || add_symbol == op_symbol)))
1361 /* Don't emit messages unless we're finalizing the symbol value,
1362 otherwise we may get the same message multiple times. */
1363 if (finalize_syms)
1364 report_op_error (symp, add_symbol, op, op_symbol);
1365 /* However do not move the symbol into the absolute section
1366 if it cannot currently be resolved - this would confuse
1367 other parts of the assembler into believing that the
1368 expression had been evaluated to zero. */
1369 else
1370 move_seg_ok = 0;
1373 if (move_seg_ok
1374 && (final_seg == expr_section || final_seg == undefined_section))
1375 final_seg = absolute_section;
1377 /* Check for division by zero. */
1378 if ((op == O_divide || op == O_modulus) && right == 0)
1380 /* If seg_right is not absolute_section, then we've
1381 already issued a warning about using a bad symbol. */
1382 if (seg_right == absolute_section && finalize_syms)
1384 char *file;
1385 unsigned int line;
1387 if (expr_symbol_where (symp, &file, &line))
1388 as_bad_where (file, line, _("division by zero"));
1389 else
1390 as_bad (_("division by zero when setting `%s'"),
1391 S_GET_NAME (symp));
1394 right = 1;
1397 switch (symp->sy_value.X_op)
1399 case O_multiply: left *= right; break;
1400 case O_divide: left /= right; break;
1401 case O_modulus: left %= right; break;
1402 case O_left_shift: left <<= right; break;
1403 case O_right_shift: left >>= right; break;
1404 case O_bit_inclusive_or: left |= right; break;
1405 case O_bit_or_not: left |= ~right; break;
1406 case O_bit_exclusive_or: left ^= right; break;
1407 case O_bit_and: left &= right; break;
1408 case O_add: left += right; break;
1409 case O_subtract: left -= right; break;
1410 case O_eq:
1411 case O_ne:
1412 left = (left == right && seg_left == seg_right
1413 && (seg_left != undefined_section
1414 || add_symbol == op_symbol)
1415 ? ~ (offsetT) 0 : 0);
1416 if (symp->sy_value.X_op == O_ne)
1417 left = ~left;
1418 break;
1419 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1420 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1421 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1422 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1423 case O_logical_and: left = left && right; break;
1424 case O_logical_or: left = left || right; break;
1425 default: abort ();
1428 final_val += symp->sy_frag->fr_address + left;
1429 if (final_seg == expr_section || final_seg == undefined_section)
1431 if (seg_left == undefined_section
1432 || seg_right == undefined_section)
1433 final_seg = undefined_section;
1434 else if (seg_left == absolute_section)
1435 final_seg = seg_right;
1436 else
1437 final_seg = seg_left;
1439 resolved = (symbol_resolved_p (add_symbol)
1440 && symbol_resolved_p (op_symbol));
1441 break;
1443 case O_big:
1444 case O_illegal:
1445 /* Give an error (below) if not in expr_section. We don't
1446 want to worry about expr_section symbols, because they
1447 are fictional (they are created as part of expression
1448 resolution), and any problems may not actually mean
1449 anything. */
1450 break;
1453 symp->sy_flags.sy_resolving = 0;
1456 if (finalize_syms)
1457 S_SET_VALUE (symp, final_val);
1459 exit_dont_set_value:
1460 /* Always set the segment, even if not finalizing the value.
1461 The segment is used to determine whether a symbol is defined. */
1462 S_SET_SEGMENT (symp, final_seg);
1464 /* Don't worry if we can't resolve an expr_section symbol. */
1465 if (finalize_syms)
1467 if (resolved)
1468 symp->sy_flags.sy_resolved = 1;
1469 else if (S_GET_SEGMENT (symp) != expr_section)
1471 as_bad (_("can't resolve value for symbol `%s'"),
1472 S_GET_NAME (symp));
1473 symp->sy_flags.sy_resolved = 1;
1477 return final_val;
1480 static void resolve_local_symbol (const char *, void *);
1482 /* A static function passed to hash_traverse. */
1484 static void
1485 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1487 if (value != NULL)
1488 resolve_symbol_value ((symbolS *) value);
1491 /* Resolve all local symbols. */
1493 void
1494 resolve_local_symbol_values (void)
1496 hash_traverse (local_hash, resolve_local_symbol);
1499 /* Obtain the current value of a symbol without changing any
1500 sub-expressions used. */
1503 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1505 symbolS *symbolP = *symbolPP;
1507 if (LOCAL_SYMBOL_CHECK (symbolP))
1509 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1511 *valueP = locsym->lsy_value;
1512 *segP = locsym->lsy_section;
1513 *fragPP = local_symbol_get_frag (locsym);
1515 else
1517 expressionS exp = symbolP->sy_value;
1519 if (!symbolP->sy_flags.sy_resolved && exp.X_op != O_illegal)
1521 int resolved;
1523 if (symbolP->sy_flags.sy_resolving)
1524 return 0;
1525 symbolP->sy_flags.sy_resolving = 1;
1526 resolved = resolve_expression (&exp);
1527 symbolP->sy_flags.sy_resolving = 0;
1528 if (!resolved)
1529 return 0;
1531 switch (exp.X_op)
1533 case O_constant:
1534 case O_register:
1535 if (!symbol_equated_p (symbolP))
1536 break;
1537 /* Fall thru. */
1538 case O_symbol:
1539 case O_symbol_rva:
1540 symbolP = exp.X_add_symbol;
1541 break;
1542 default:
1543 return 0;
1547 *symbolPP = symbolP;
1548 *valueP = exp.X_add_number;
1549 *segP = symbolP->bsym->section;
1550 *fragPP = symbolP->sy_frag;
1552 if (*segP == expr_section)
1553 switch (exp.X_op)
1555 case O_constant: *segP = absolute_section; break;
1556 case O_register: *segP = reg_section; break;
1557 default: break;
1561 return 1;
1564 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1565 They are *really* local. That is, they go out of scope whenever we see a
1566 label that isn't local. Also, like fb labels, there can be multiple
1567 instances of a dollar label. Therefor, we name encode each instance with
1568 the instance number, keep a list of defined symbols separate from the real
1569 symbol table, and we treat these buggers as a sparse array. */
1571 static long *dollar_labels;
1572 static long *dollar_label_instances;
1573 static char *dollar_label_defines;
1574 static unsigned long dollar_label_count;
1575 static unsigned long dollar_label_max;
1578 dollar_label_defined (long label)
1580 long *i;
1582 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1584 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1585 if (*i == label)
1586 return dollar_label_defines[i - dollar_labels];
1588 /* If we get here, label isn't defined. */
1589 return 0;
1592 static long
1593 dollar_label_instance (long label)
1595 long *i;
1597 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1599 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1600 if (*i == label)
1601 return (dollar_label_instances[i - dollar_labels]);
1603 /* If we get here, we haven't seen the label before.
1604 Therefore its instance count is zero. */
1605 return 0;
1608 void
1609 dollar_label_clear (void)
1611 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1614 #define DOLLAR_LABEL_BUMP_BY 10
1616 void
1617 define_dollar_label (long label)
1619 long *i;
1621 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1622 if (*i == label)
1624 ++dollar_label_instances[i - dollar_labels];
1625 dollar_label_defines[i - dollar_labels] = 1;
1626 return;
1629 /* If we get to here, we don't have label listed yet. */
1631 if (dollar_labels == NULL)
1633 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1634 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1635 dollar_label_defines = (char *) xmalloc (DOLLAR_LABEL_BUMP_BY);
1636 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1637 dollar_label_count = 0;
1639 else if (dollar_label_count == dollar_label_max)
1641 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1642 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1643 dollar_label_max * sizeof (long));
1644 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1645 dollar_label_max * sizeof (long));
1646 dollar_label_defines = (char *) xrealloc (dollar_label_defines, dollar_label_max);
1647 } /* if we needed to grow */
1649 dollar_labels[dollar_label_count] = label;
1650 dollar_label_instances[dollar_label_count] = 1;
1651 dollar_label_defines[dollar_label_count] = 1;
1652 ++dollar_label_count;
1655 /* Caller must copy returned name: we re-use the area for the next name.
1657 The mth occurence of label n: is turned into the symbol "Ln^Am"
1658 where n is the label number and m is the instance number. "L" makes
1659 it a label discarded unless debugging and "^A"('\1') ensures no
1660 ordinary symbol SHOULD get the same name as a local label
1661 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1663 fb labels get the same treatment, except that ^B is used in place
1664 of ^A. */
1666 char * /* Return local label name. */
1667 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1668 register int augend /* 0 for current instance, 1 for new instance. */)
1670 long i;
1671 /* Returned to caller, then copied. Used for created names ("4f"). */
1672 static char symbol_name_build[24];
1673 register char *p;
1674 register char *q;
1675 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1677 know (n >= 0);
1678 know (augend == 0 || augend == 1);
1679 p = symbol_name_build;
1680 #ifdef LOCAL_LABEL_PREFIX
1681 *p++ = LOCAL_LABEL_PREFIX;
1682 #endif
1683 *p++ = 'L';
1685 /* Next code just does sprintf( {}, "%d", n); */
1686 /* Label number. */
1687 q = symbol_name_temporary;
1688 for (*q++ = 0, i = n; i; ++q)
1690 *q = i % 10 + '0';
1691 i /= 10;
1693 while ((*p = *--q) != '\0')
1694 ++p;
1696 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1698 /* Instance number. */
1699 q = symbol_name_temporary;
1700 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1702 *q = i % 10 + '0';
1703 i /= 10;
1705 while ((*p++ = *--q) != '\0');;
1707 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1708 return symbol_name_build;
1711 /* Somebody else's idea of local labels. They are made by "n:" where n
1712 is any decimal digit. Refer to them with
1713 "nb" for previous (backward) n:
1714 or "nf" for next (forward) n:.
1716 We do a little better and let n be any number, not just a single digit, but
1717 since the other guy's assembler only does ten, we treat the first ten
1718 specially.
1720 Like someone else's assembler, we have one set of local label counters for
1721 entire assembly, not one set per (sub)segment like in most assemblers. This
1722 implies that one can refer to a label in another segment, and indeed some
1723 crufty compilers have done just that.
1725 Since there could be a LOT of these things, treat them as a sparse
1726 array. */
1728 #define FB_LABEL_SPECIAL (10)
1730 static long fb_low_counter[FB_LABEL_SPECIAL];
1731 static long *fb_labels;
1732 static long *fb_label_instances;
1733 static long fb_label_count;
1734 static long fb_label_max;
1736 /* This must be more than FB_LABEL_SPECIAL. */
1737 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1739 static void
1740 fb_label_init (void)
1742 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1745 /* Add one to the instance number of this fb label. */
1747 void
1748 fb_label_instance_inc (long label)
1750 long *i;
1752 if (label < FB_LABEL_SPECIAL)
1754 ++fb_low_counter[label];
1755 return;
1758 if (fb_labels != NULL)
1760 for (i = fb_labels + FB_LABEL_SPECIAL;
1761 i < fb_labels + fb_label_count; ++i)
1763 if (*i == label)
1765 ++fb_label_instances[i - fb_labels];
1766 return;
1767 } /* if we find it */
1768 } /* for each existing label */
1771 /* If we get to here, we don't have label listed yet. */
1773 if (fb_labels == NULL)
1775 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1776 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1777 fb_label_max = FB_LABEL_BUMP_BY;
1778 fb_label_count = FB_LABEL_SPECIAL;
1781 else if (fb_label_count == fb_label_max)
1783 fb_label_max += FB_LABEL_BUMP_BY;
1784 fb_labels = (long *) xrealloc ((char *) fb_labels,
1785 fb_label_max * sizeof (long));
1786 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1787 fb_label_max * sizeof (long));
1788 } /* if we needed to grow */
1790 fb_labels[fb_label_count] = label;
1791 fb_label_instances[fb_label_count] = 1;
1792 ++fb_label_count;
1795 static long
1796 fb_label_instance (long label)
1798 long *i;
1800 if (label < FB_LABEL_SPECIAL)
1802 return (fb_low_counter[label]);
1805 if (fb_labels != NULL)
1807 for (i = fb_labels + FB_LABEL_SPECIAL;
1808 i < fb_labels + fb_label_count; ++i)
1810 if (*i == label)
1812 return (fb_label_instances[i - fb_labels]);
1813 } /* if we find it */
1814 } /* for each existing label */
1817 /* We didn't find the label, so this must be a reference to the
1818 first instance. */
1819 return 0;
1822 /* Caller must copy returned name: we re-use the area for the next name.
1824 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1825 where n is the label number and m is the instance number. "L" makes
1826 it a label discarded unless debugging and "^B"('\2') ensures no
1827 ordinary symbol SHOULD get the same name as a local label
1828 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1830 dollar labels get the same treatment, except that ^A is used in
1831 place of ^B. */
1833 char * /* Return local label name. */
1834 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1835 long augend /* 0 for nb, 1 for n:, nf. */)
1837 long i;
1838 /* Returned to caller, then copied. Used for created names ("4f"). */
1839 static char symbol_name_build[24];
1840 register char *p;
1841 register char *q;
1842 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1844 know (n >= 0);
1845 #ifdef TC_MMIX
1846 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1847 #else
1848 know ((unsigned long) augend <= 1);
1849 #endif
1850 p = symbol_name_build;
1851 #ifdef LOCAL_LABEL_PREFIX
1852 *p++ = LOCAL_LABEL_PREFIX;
1853 #endif
1854 *p++ = 'L';
1856 /* Next code just does sprintf( {}, "%d", n); */
1857 /* Label number. */
1858 q = symbol_name_temporary;
1859 for (*q++ = 0, i = n; i; ++q)
1861 *q = i % 10 + '0';
1862 i /= 10;
1864 while ((*p = *--q) != '\0')
1865 ++p;
1867 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1869 /* Instance number. */
1870 q = symbol_name_temporary;
1871 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1873 *q = i % 10 + '0';
1874 i /= 10;
1876 while ((*p++ = *--q) != '\0');;
1878 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1879 return (symbol_name_build);
1882 /* Decode name that may have been generated by foo_label_name() above.
1883 If the name wasn't generated by foo_label_name(), then return it
1884 unaltered. This is used for error messages. */
1886 char *
1887 decode_local_label_name (char *s)
1889 char *p;
1890 char *symbol_decode;
1891 int label_number;
1892 int instance_number;
1893 char *type;
1894 const char *message_format;
1895 int lindex = 0;
1897 #ifdef LOCAL_LABEL_PREFIX
1898 if (s[lindex] == LOCAL_LABEL_PREFIX)
1899 ++lindex;
1900 #endif
1902 if (s[lindex] != 'L')
1903 return s;
1905 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1906 label_number = (10 * label_number) + *p - '0';
1908 if (*p == DOLLAR_LABEL_CHAR)
1909 type = "dollar";
1910 else if (*p == LOCAL_LABEL_CHAR)
1911 type = "fb";
1912 else
1913 return s;
1915 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1916 instance_number = (10 * instance_number) + *p - '0';
1918 message_format = _("\"%d\" (instance number %d of a %s label)");
1919 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
1920 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1922 return symbol_decode;
1925 /* Get the value of a symbol. */
1927 valueT
1928 S_GET_VALUE (symbolS *s)
1930 if (LOCAL_SYMBOL_CHECK (s))
1931 return resolve_symbol_value (s);
1933 if (!s->sy_flags.sy_resolved)
1935 valueT val = resolve_symbol_value (s);
1936 if (!finalize_syms)
1937 return val;
1939 if (S_IS_WEAKREFR (s))
1940 return S_GET_VALUE (s->sy_value.X_add_symbol);
1942 if (s->sy_value.X_op != O_constant)
1944 if (! s->sy_flags.sy_resolved
1945 || s->sy_value.X_op != O_symbol
1946 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1947 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1948 S_GET_NAME (s));
1950 return (valueT) s->sy_value.X_add_number;
1953 /* Set the value of a symbol. */
1955 void
1956 S_SET_VALUE (symbolS *s, valueT val)
1958 if (LOCAL_SYMBOL_CHECK (s))
1960 ((struct local_symbol *) s)->lsy_value = val;
1961 return;
1964 s->sy_value.X_op = O_constant;
1965 s->sy_value.X_add_number = (offsetT) val;
1966 s->sy_value.X_unsigned = 0;
1967 S_CLEAR_WEAKREFR (s);
1970 void
1971 copy_symbol_attributes (symbolS *dest, symbolS *src)
1973 if (LOCAL_SYMBOL_CHECK (dest))
1974 dest = local_symbol_convert ((struct local_symbol *) dest);
1975 if (LOCAL_SYMBOL_CHECK (src))
1976 src = local_symbol_convert ((struct local_symbol *) src);
1978 /* In an expression, transfer the settings of these flags.
1979 The user can override later, of course. */
1980 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1981 | BSF_GNU_INDIRECT_FUNCTION)
1982 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1984 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1985 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1986 #endif
1988 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1989 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
1990 #endif
1994 S_IS_FUNCTION (symbolS *s)
1996 flagword flags;
1998 if (LOCAL_SYMBOL_CHECK (s))
1999 return 0;
2001 flags = s->bsym->flags;
2003 return (flags & BSF_FUNCTION) != 0;
2007 S_IS_EXTERNAL (symbolS *s)
2009 flagword flags;
2011 if (LOCAL_SYMBOL_CHECK (s))
2012 return 0;
2014 flags = s->bsym->flags;
2016 /* Sanity check. */
2017 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2018 abort ();
2020 return (flags & BSF_GLOBAL) != 0;
2024 S_IS_WEAK (symbolS *s)
2026 if (LOCAL_SYMBOL_CHECK (s))
2027 return 0;
2028 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2029 could probably handle a WEAKREFR as always weak though. E.g., if
2030 the referenced symbol has lost its weak status, there's no reason
2031 to keep handling the weakrefr as if it was weak. */
2032 if (S_IS_WEAKREFR (s))
2033 return S_IS_WEAK (s->sy_value.X_add_symbol);
2034 return (s->bsym->flags & BSF_WEAK) != 0;
2038 S_IS_WEAKREFR (symbolS *s)
2040 if (LOCAL_SYMBOL_CHECK (s))
2041 return 0;
2042 return s->sy_flags.sy_weakrefr != 0;
2046 S_IS_WEAKREFD (symbolS *s)
2048 if (LOCAL_SYMBOL_CHECK (s))
2049 return 0;
2050 return s->sy_flags.sy_weakrefd != 0;
2054 S_IS_COMMON (symbolS *s)
2056 if (LOCAL_SYMBOL_CHECK (s))
2057 return 0;
2058 return bfd_is_com_section (s->bsym->section);
2062 S_IS_DEFINED (symbolS *s)
2064 if (LOCAL_SYMBOL_CHECK (s))
2065 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2066 return s->bsym->section != undefined_section;
2070 #ifndef EXTERN_FORCE_RELOC
2071 #define EXTERN_FORCE_RELOC IS_ELF
2072 #endif
2074 /* Return true for symbols that should not be reduced to section
2075 symbols or eliminated from expressions, because they may be
2076 overridden by the linker. */
2078 S_FORCE_RELOC (symbolS *s, int strict)
2080 if (LOCAL_SYMBOL_CHECK (s))
2081 return ((struct local_symbol *) s)->lsy_section == undefined_section;
2083 return ((strict
2084 && ((s->bsym->flags & BSF_WEAK) != 0
2085 || (EXTERN_FORCE_RELOC
2086 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2087 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0
2088 || s->bsym->section == undefined_section
2089 || bfd_is_com_section (s->bsym->section));
2093 S_IS_DEBUG (symbolS *s)
2095 if (LOCAL_SYMBOL_CHECK (s))
2096 return 0;
2097 if (s->bsym->flags & BSF_DEBUGGING)
2098 return 1;
2099 return 0;
2103 S_IS_LOCAL (symbolS *s)
2105 flagword flags;
2106 const char *name;
2108 if (LOCAL_SYMBOL_CHECK (s))
2109 return 1;
2111 flags = s->bsym->flags;
2113 /* Sanity check. */
2114 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2115 abort ();
2117 if (bfd_get_section (s->bsym) == reg_section)
2118 return 1;
2120 if (flag_strip_local_absolute
2121 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2122 the source file even when the object file is stripped. */
2123 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2124 && bfd_get_section (s->bsym) == absolute_section)
2125 return 1;
2127 name = S_GET_NAME (s);
2128 return (name != NULL
2129 && ! S_IS_DEBUG (s)
2130 && (strchr (name, DOLLAR_LABEL_CHAR)
2131 || strchr (name, LOCAL_LABEL_CHAR)
2132 || TC_LABEL_IS_LOCAL (name)
2133 || (! flag_keep_locals
2134 && (bfd_is_local_label (stdoutput, s->bsym)
2135 || (flag_mri
2136 && name[0] == '?'
2137 && name[1] == '?')))));
2141 S_IS_STABD (symbolS *s)
2143 return S_GET_NAME (s) == 0;
2147 S_CAN_BE_REDEFINED (const symbolS *s)
2149 if (LOCAL_SYMBOL_CHECK (s))
2150 return (local_symbol_get_frag ((struct local_symbol *) s)
2151 == &predefined_address_frag);
2152 /* Permit register names to be redefined. */
2153 return s->bsym->section == reg_section;
2157 S_IS_VOLATILE (const symbolS *s)
2159 if (LOCAL_SYMBOL_CHECK (s))
2160 return 0;
2161 return s->sy_flags.sy_volatile;
2165 S_IS_FORWARD_REF (const symbolS *s)
2167 if (LOCAL_SYMBOL_CHECK (s))
2168 return 0;
2169 return s->sy_flags.sy_forward_ref;
2172 const char *
2173 S_GET_NAME (symbolS *s)
2175 if (LOCAL_SYMBOL_CHECK (s))
2176 return ((struct local_symbol *) s)->lsy_name;
2177 return s->bsym->name;
2180 segT
2181 S_GET_SEGMENT (symbolS *s)
2183 if (LOCAL_SYMBOL_CHECK (s))
2184 return ((struct local_symbol *) s)->lsy_section;
2185 return s->bsym->section;
2188 void
2189 S_SET_SEGMENT (symbolS *s, segT seg)
2191 /* Don't reassign section symbols. The direct reason is to prevent seg
2192 faults assigning back to const global symbols such as *ABS*, but it
2193 shouldn't happen anyway. */
2195 if (LOCAL_SYMBOL_CHECK (s))
2197 if (seg == reg_section)
2198 s = local_symbol_convert ((struct local_symbol *) s);
2199 else
2201 ((struct local_symbol *) s)->lsy_section = seg;
2202 return;
2206 if (s->bsym->flags & BSF_SECTION_SYM)
2208 if (s->bsym->section != seg)
2209 abort ();
2211 else
2212 s->bsym->section = seg;
2215 void
2216 S_SET_EXTERNAL (symbolS *s)
2218 if (LOCAL_SYMBOL_CHECK (s))
2219 s = local_symbol_convert ((struct local_symbol *) s);
2220 if ((s->bsym->flags & BSF_WEAK) != 0)
2222 /* Let .weak override .global. */
2223 return;
2225 if (s->bsym->flags & BSF_SECTION_SYM)
2227 char * file;
2228 unsigned int line;
2230 /* Do not reassign section symbols. */
2231 as_where (& file, & line);
2232 as_warn_where (file, line,
2233 _("section symbols are already global"));
2234 return;
2236 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2237 if (S_GET_SEGMENT (s) == reg_section)
2239 as_bad ("can't make register symbol `%s' global",
2240 S_GET_NAME (s));
2241 return;
2243 #endif
2244 s->bsym->flags |= BSF_GLOBAL;
2245 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2247 #ifdef TE_PE
2248 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2249 an_external_name = S_GET_NAME (s);
2250 #endif
2253 void
2254 S_CLEAR_EXTERNAL (symbolS *s)
2256 if (LOCAL_SYMBOL_CHECK (s))
2257 return;
2258 if ((s->bsym->flags & BSF_WEAK) != 0)
2260 /* Let .weak override. */
2261 return;
2263 s->bsym->flags |= BSF_LOCAL;
2264 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2267 void
2268 S_SET_WEAK (symbolS *s)
2270 if (LOCAL_SYMBOL_CHECK (s))
2271 s = local_symbol_convert ((struct local_symbol *) s);
2272 #ifdef obj_set_weak_hook
2273 obj_set_weak_hook (s);
2274 #endif
2275 s->bsym->flags |= BSF_WEAK;
2276 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2279 void
2280 S_SET_WEAKREFR (symbolS *s)
2282 if (LOCAL_SYMBOL_CHECK (s))
2283 s = local_symbol_convert ((struct local_symbol *) s);
2284 s->sy_flags.sy_weakrefr = 1;
2285 /* If the alias was already used, make sure we mark the target as
2286 used as well, otherwise it might be dropped from the symbol
2287 table. This may have unintended side effects if the alias is
2288 later redirected to another symbol, such as keeping the unused
2289 previous target in the symbol table. Since it will be weak, it's
2290 not a big deal. */
2291 if (s->sy_flags.sy_used)
2292 symbol_mark_used (s->sy_value.X_add_symbol);
2295 void
2296 S_CLEAR_WEAKREFR (symbolS *s)
2298 if (LOCAL_SYMBOL_CHECK (s))
2299 return;
2300 s->sy_flags.sy_weakrefr = 0;
2303 void
2304 S_SET_WEAKREFD (symbolS *s)
2306 if (LOCAL_SYMBOL_CHECK (s))
2307 s = local_symbol_convert ((struct local_symbol *) s);
2308 s->sy_flags.sy_weakrefd = 1;
2309 S_SET_WEAK (s);
2312 void
2313 S_CLEAR_WEAKREFD (symbolS *s)
2315 if (LOCAL_SYMBOL_CHECK (s))
2316 return;
2317 if (s->sy_flags.sy_weakrefd)
2319 s->sy_flags.sy_weakrefd = 0;
2320 /* If a weakref target symbol is weak, then it was never
2321 referenced directly before, not even in a .global directive,
2322 so decay it to local. If it remains undefined, it will be
2323 later turned into a global, like any other undefined
2324 symbol. */
2325 if (s->bsym->flags & BSF_WEAK)
2327 #ifdef obj_clear_weak_hook
2328 obj_clear_weak_hook (s);
2329 #endif
2330 s->bsym->flags &= ~BSF_WEAK;
2331 s->bsym->flags |= BSF_LOCAL;
2336 void
2337 S_SET_THREAD_LOCAL (symbolS *s)
2339 if (LOCAL_SYMBOL_CHECK (s))
2340 s = local_symbol_convert ((struct local_symbol *) s);
2341 if (bfd_is_com_section (s->bsym->section)
2342 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2343 return;
2344 s->bsym->flags |= BSF_THREAD_LOCAL;
2345 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2346 as_bad (_("Accessing function `%s' as thread-local object"),
2347 S_GET_NAME (s));
2348 else if (! bfd_is_und_section (s->bsym->section)
2349 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2350 as_bad (_("Accessing `%s' as thread-local object"),
2351 S_GET_NAME (s));
2354 void
2355 S_SET_NAME (symbolS *s, const char *name)
2357 if (LOCAL_SYMBOL_CHECK (s))
2359 ((struct local_symbol *) s)->lsy_name = name;
2360 return;
2362 s->bsym->name = name;
2365 void
2366 S_SET_VOLATILE (symbolS *s)
2368 if (LOCAL_SYMBOL_CHECK (s))
2369 s = local_symbol_convert ((struct local_symbol *) s);
2370 s->sy_flags.sy_volatile = 1;
2373 void
2374 S_CLEAR_VOLATILE (symbolS *s)
2376 if (!LOCAL_SYMBOL_CHECK (s))
2377 s->sy_flags.sy_volatile = 0;
2380 void
2381 S_SET_FORWARD_REF (symbolS *s)
2383 if (LOCAL_SYMBOL_CHECK (s))
2384 s = local_symbol_convert ((struct local_symbol *) s);
2385 s->sy_flags.sy_forward_ref = 1;
2388 /* Return the previous symbol in a chain. */
2390 symbolS *
2391 symbol_previous (symbolS *s)
2393 if (LOCAL_SYMBOL_CHECK (s))
2394 abort ();
2395 return s->sy_previous;
2398 /* Return the next symbol in a chain. */
2400 symbolS *
2401 symbol_next (symbolS *s)
2403 if (LOCAL_SYMBOL_CHECK (s))
2404 abort ();
2405 return s->sy_next;
2408 /* Return a pointer to the value of a symbol as an expression. */
2410 expressionS *
2411 symbol_get_value_expression (symbolS *s)
2413 if (LOCAL_SYMBOL_CHECK (s))
2414 s = local_symbol_convert ((struct local_symbol *) s);
2415 return &s->sy_value;
2418 /* Set the value of a symbol to an expression. */
2420 void
2421 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2423 if (LOCAL_SYMBOL_CHECK (s))
2424 s = local_symbol_convert ((struct local_symbol *) s);
2425 s->sy_value = *exp;
2426 S_CLEAR_WEAKREFR (s);
2429 /* Return whether 2 symbols are the same. */
2432 symbol_same_p (symbolS *s1, symbolS *s2)
2434 if (s1->sy_flags.sy_local_symbol
2435 && local_symbol_converted_p ((struct local_symbol *) s1))
2436 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2437 if (s2->sy_flags.sy_local_symbol
2438 && local_symbol_converted_p ((struct local_symbol *) s2))
2439 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2440 return s1 == s2;
2443 /* Return a pointer to the X_add_number component of a symbol. */
2445 offsetT *
2446 symbol_X_add_number (symbolS *s)
2448 if (LOCAL_SYMBOL_CHECK (s))
2449 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2451 return &s->sy_value.X_add_number;
2454 /* Set the value of SYM to the current position in the current segment. */
2456 void
2457 symbol_set_value_now (symbolS *sym)
2459 S_SET_SEGMENT (sym, now_seg);
2460 S_SET_VALUE (sym, frag_now_fix ());
2461 symbol_set_frag (sym, frag_now);
2464 /* Set the frag of a symbol. */
2466 void
2467 symbol_set_frag (symbolS *s, fragS *f)
2469 if (LOCAL_SYMBOL_CHECK (s))
2471 local_symbol_set_frag ((struct local_symbol *) s, f);
2472 return;
2474 s->sy_frag = f;
2475 S_CLEAR_WEAKREFR (s);
2478 /* Return the frag of a symbol. */
2480 fragS *
2481 symbol_get_frag (symbolS *s)
2483 if (LOCAL_SYMBOL_CHECK (s))
2484 return local_symbol_get_frag ((struct local_symbol *) s);
2485 return s->sy_frag;
2488 /* Mark a symbol as having been used. */
2490 void
2491 symbol_mark_used (symbolS *s)
2493 if (LOCAL_SYMBOL_CHECK (s))
2494 return;
2495 s->sy_flags.sy_used = 1;
2496 if (S_IS_WEAKREFR (s))
2497 symbol_mark_used (s->sy_value.X_add_symbol);
2500 /* Clear the mark of whether a symbol has been used. */
2502 void
2503 symbol_clear_used (symbolS *s)
2505 if (LOCAL_SYMBOL_CHECK (s))
2506 s = local_symbol_convert ((struct local_symbol *) s);
2507 s->sy_flags.sy_used = 0;
2510 /* Return whether a symbol has been used. */
2513 symbol_used_p (symbolS *s)
2515 if (LOCAL_SYMBOL_CHECK (s))
2516 return 1;
2517 return s->sy_flags.sy_used;
2520 /* Mark a symbol as having been used in a reloc. */
2522 void
2523 symbol_mark_used_in_reloc (symbolS *s)
2525 if (LOCAL_SYMBOL_CHECK (s))
2526 s = local_symbol_convert ((struct local_symbol *) s);
2527 s->sy_flags.sy_used_in_reloc = 1;
2530 /* Clear the mark of whether a symbol has been used in a reloc. */
2532 void
2533 symbol_clear_used_in_reloc (symbolS *s)
2535 if (LOCAL_SYMBOL_CHECK (s))
2536 return;
2537 s->sy_flags.sy_used_in_reloc = 0;
2540 /* Return whether a symbol has been used in a reloc. */
2543 symbol_used_in_reloc_p (symbolS *s)
2545 if (LOCAL_SYMBOL_CHECK (s))
2546 return 0;
2547 return s->sy_flags.sy_used_in_reloc;
2550 /* Mark a symbol as an MRI common symbol. */
2552 void
2553 symbol_mark_mri_common (symbolS *s)
2555 if (LOCAL_SYMBOL_CHECK (s))
2556 s = local_symbol_convert ((struct local_symbol *) s);
2557 s->sy_flags.sy_mri_common = 1;
2560 /* Clear the mark of whether a symbol is an MRI common symbol. */
2562 void
2563 symbol_clear_mri_common (symbolS *s)
2565 if (LOCAL_SYMBOL_CHECK (s))
2566 return;
2567 s->sy_flags.sy_mri_common = 0;
2570 /* Return whether a symbol is an MRI common symbol. */
2573 symbol_mri_common_p (symbolS *s)
2575 if (LOCAL_SYMBOL_CHECK (s))
2576 return 0;
2577 return s->sy_flags.sy_mri_common;
2580 /* Mark a symbol as having been written. */
2582 void
2583 symbol_mark_written (symbolS *s)
2585 if (LOCAL_SYMBOL_CHECK (s))
2586 return;
2587 s->sy_flags.sy_written = 1;
2590 /* Clear the mark of whether a symbol has been written. */
2592 void
2593 symbol_clear_written (symbolS *s)
2595 if (LOCAL_SYMBOL_CHECK (s))
2596 return;
2597 s->sy_flags.sy_written = 0;
2600 /* Return whether a symbol has been written. */
2603 symbol_written_p (symbolS *s)
2605 if (LOCAL_SYMBOL_CHECK (s))
2606 return 0;
2607 return s->sy_flags.sy_written;
2610 /* Mark a symbol has having been resolved. */
2612 void
2613 symbol_mark_resolved (symbolS *s)
2615 if (LOCAL_SYMBOL_CHECK (s))
2617 local_symbol_mark_resolved ((struct local_symbol *) s);
2618 return;
2620 s->sy_flags.sy_resolved = 1;
2623 /* Return whether a symbol has been resolved. */
2626 symbol_resolved_p (symbolS *s)
2628 if (LOCAL_SYMBOL_CHECK (s))
2629 return local_symbol_resolved_p ((struct local_symbol *) s);
2630 return s->sy_flags.sy_resolved;
2633 /* Return whether a symbol is a section symbol. */
2636 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2638 if (LOCAL_SYMBOL_CHECK (s))
2639 return 0;
2640 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2643 /* Return whether a symbol is equated to another symbol. */
2646 symbol_equated_p (symbolS *s)
2648 if (LOCAL_SYMBOL_CHECK (s))
2649 return 0;
2650 return s->sy_value.X_op == O_symbol;
2653 /* Return whether a symbol is equated to another symbol, and should be
2654 treated specially when writing out relocs. */
2657 symbol_equated_reloc_p (symbolS *s)
2659 if (LOCAL_SYMBOL_CHECK (s))
2660 return 0;
2661 /* X_op_symbol, normally not used for O_symbol, is set by
2662 resolve_symbol_value to flag expression syms that have been
2663 equated. */
2664 return (s->sy_value.X_op == O_symbol
2665 #if defined (OBJ_COFF) && defined (TE_PE)
2666 && ! S_IS_WEAK (s)
2667 #endif
2668 && ((s->sy_flags.sy_resolved && s->sy_value.X_op_symbol != NULL)
2669 || ! S_IS_DEFINED (s)
2670 || S_IS_COMMON (s)));
2673 /* Return whether a symbol has a constant value. */
2676 symbol_constant_p (symbolS *s)
2678 if (LOCAL_SYMBOL_CHECK (s))
2679 return 1;
2680 return s->sy_value.X_op == O_constant;
2683 /* Return whether a symbol was cloned and thus removed from the global
2684 symbol list. */
2687 symbol_shadow_p (symbolS *s)
2689 if (LOCAL_SYMBOL_CHECK (s))
2690 return 0;
2691 return s->sy_next == s;
2694 /* Return the BFD symbol for a symbol. */
2696 asymbol *
2697 symbol_get_bfdsym (symbolS *s)
2699 if (LOCAL_SYMBOL_CHECK (s))
2700 s = local_symbol_convert ((struct local_symbol *) s);
2701 return s->bsym;
2704 /* Set the BFD symbol for a symbol. */
2706 void
2707 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2709 if (LOCAL_SYMBOL_CHECK (s))
2710 s = local_symbol_convert ((struct local_symbol *) s);
2711 /* Usually, it is harmless to reset a symbol to a BFD section
2712 symbol. For example, obj_elf_change_section sets the BFD symbol
2713 of an old symbol with the newly created section symbol. But when
2714 we have multiple sections with the same name, the newly created
2715 section may have the same name as an old section. We check if the
2716 old symbol has been already marked as a section symbol before
2717 resetting it. */
2718 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2719 s->bsym = bsym;
2720 /* else XXX - What do we do now ? */
2723 #ifdef OBJ_SYMFIELD_TYPE
2725 /* Get a pointer to the object format information for a symbol. */
2727 OBJ_SYMFIELD_TYPE *
2728 symbol_get_obj (symbolS *s)
2730 if (LOCAL_SYMBOL_CHECK (s))
2731 s = local_symbol_convert ((struct local_symbol *) s);
2732 return &s->sy_obj;
2735 /* Set the object format information for a symbol. */
2737 void
2738 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2740 if (LOCAL_SYMBOL_CHECK (s))
2741 s = local_symbol_convert ((struct local_symbol *) s);
2742 s->sy_obj = *o;
2745 #endif /* OBJ_SYMFIELD_TYPE */
2747 #ifdef TC_SYMFIELD_TYPE
2749 /* Get a pointer to the processor information for a symbol. */
2751 TC_SYMFIELD_TYPE *
2752 symbol_get_tc (symbolS *s)
2754 if (LOCAL_SYMBOL_CHECK (s))
2755 s = local_symbol_convert ((struct local_symbol *) s);
2756 return &s->sy_tc;
2759 /* Set the processor information for a symbol. */
2761 void
2762 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2764 if (LOCAL_SYMBOL_CHECK (s))
2765 s = local_symbol_convert ((struct local_symbol *) s);
2766 s->sy_tc = *o;
2769 #endif /* TC_SYMFIELD_TYPE */
2771 void
2772 symbol_begin (void)
2774 symbol_lastP = NULL;
2775 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2776 sy_hash = hash_new ();
2777 local_hash = hash_new ();
2779 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2780 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2781 abs_symbol.bsym = bfd_abs_section.symbol;
2782 #endif
2783 abs_symbol.sy_value.X_op = O_constant;
2784 abs_symbol.sy_frag = &zero_address_frag;
2786 if (LOCAL_LABELS_FB)
2787 fb_label_init ();
2790 void
2791 dot_symbol_init (void)
2793 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2794 if (dot_symbol.bsym == NULL)
2795 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2796 dot_symbol.bsym->name = ".";
2797 dot_symbol.sy_flags.sy_forward_ref = 1;
2798 dot_symbol.sy_value.X_op = O_constant;
2801 int indent_level;
2803 /* Maximum indent level.
2804 Available for modification inside a gdb session. */
2805 static int max_indent_level = 8;
2807 void
2808 print_symbol_value_1 (FILE *file, symbolS *sym)
2810 const char *name = S_GET_NAME (sym);
2811 if (!name || !name[0])
2812 name = "(unnamed)";
2813 fprintf (file, "sym ");
2814 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2815 fprintf (file, " %s", name);
2817 if (LOCAL_SYMBOL_CHECK (sym))
2819 struct local_symbol *locsym = (struct local_symbol *) sym;
2821 if (local_symbol_get_frag (locsym) != & zero_address_frag
2822 && local_symbol_get_frag (locsym) != NULL)
2824 fprintf (file, " frag ");
2825 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2827 if (local_symbol_resolved_p (locsym))
2828 fprintf (file, " resolved");
2829 fprintf (file, " local");
2831 else
2833 if (sym->sy_frag != &zero_address_frag)
2835 fprintf (file, " frag ");
2836 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2838 if (sym->sy_flags.sy_written)
2839 fprintf (file, " written");
2840 if (sym->sy_flags.sy_resolved)
2841 fprintf (file, " resolved");
2842 else if (sym->sy_flags.sy_resolving)
2843 fprintf (file, " resolving");
2844 if (sym->sy_flags.sy_used_in_reloc)
2845 fprintf (file, " used-in-reloc");
2846 if (sym->sy_flags.sy_used)
2847 fprintf (file, " used");
2848 if (S_IS_LOCAL (sym))
2849 fprintf (file, " local");
2850 if (S_IS_EXTERNAL (sym))
2851 fprintf (file, " extern");
2852 if (S_IS_WEAK (sym))
2853 fprintf (file, " weak");
2854 if (S_IS_DEBUG (sym))
2855 fprintf (file, " debug");
2856 if (S_IS_DEFINED (sym))
2857 fprintf (file, " defined");
2859 if (S_IS_WEAKREFR (sym))
2860 fprintf (file, " weakrefr");
2861 if (S_IS_WEAKREFD (sym))
2862 fprintf (file, " weakrefd");
2863 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2864 if (symbol_resolved_p (sym))
2866 segT s = S_GET_SEGMENT (sym);
2868 if (s != undefined_section
2869 && s != expr_section)
2870 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2872 else if (indent_level < max_indent_level
2873 && S_GET_SEGMENT (sym) != undefined_section)
2875 indent_level++;
2876 fprintf (file, "\n%*s<", indent_level * 4, "");
2877 if (LOCAL_SYMBOL_CHECK (sym))
2878 fprintf (file, "constant %lx",
2879 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2880 else
2881 print_expr_1 (file, &sym->sy_value);
2882 fprintf (file, ">");
2883 indent_level--;
2885 fflush (file);
2888 void
2889 print_symbol_value (symbolS *sym)
2891 indent_level = 0;
2892 print_symbol_value_1 (stderr, sym);
2893 fprintf (stderr, "\n");
2896 static void
2897 print_binary (FILE *file, const char *name, expressionS *exp)
2899 indent_level++;
2900 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2901 print_symbol_value_1 (file, exp->X_add_symbol);
2902 fprintf (file, ">\n%*s<", indent_level * 4, "");
2903 print_symbol_value_1 (file, exp->X_op_symbol);
2904 fprintf (file, ">");
2905 indent_level--;
2908 void
2909 print_expr_1 (FILE *file, expressionS *exp)
2911 fprintf (file, "expr ");
2912 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2913 fprintf (file, " ");
2914 switch (exp->X_op)
2916 case O_illegal:
2917 fprintf (file, "illegal");
2918 break;
2919 case O_absent:
2920 fprintf (file, "absent");
2921 break;
2922 case O_constant:
2923 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2924 break;
2925 case O_symbol:
2926 indent_level++;
2927 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2928 print_symbol_value_1 (file, exp->X_add_symbol);
2929 fprintf (file, ">");
2930 maybe_print_addnum:
2931 if (exp->X_add_number)
2932 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2933 (unsigned long) exp->X_add_number);
2934 indent_level--;
2935 break;
2936 case O_register:
2937 fprintf (file, "register #%d", (int) exp->X_add_number);
2938 break;
2939 case O_big:
2940 fprintf (file, "big");
2941 break;
2942 case O_uminus:
2943 fprintf (file, "uminus -<");
2944 indent_level++;
2945 print_symbol_value_1 (file, exp->X_add_symbol);
2946 fprintf (file, ">");
2947 goto maybe_print_addnum;
2948 case O_bit_not:
2949 fprintf (file, "bit_not");
2950 break;
2951 case O_multiply:
2952 print_binary (file, "multiply", exp);
2953 break;
2954 case O_divide:
2955 print_binary (file, "divide", exp);
2956 break;
2957 case O_modulus:
2958 print_binary (file, "modulus", exp);
2959 break;
2960 case O_left_shift:
2961 print_binary (file, "lshift", exp);
2962 break;
2963 case O_right_shift:
2964 print_binary (file, "rshift", exp);
2965 break;
2966 case O_bit_inclusive_or:
2967 print_binary (file, "bit_ior", exp);
2968 break;
2969 case O_bit_exclusive_or:
2970 print_binary (file, "bit_xor", exp);
2971 break;
2972 case O_bit_and:
2973 print_binary (file, "bit_and", exp);
2974 break;
2975 case O_eq:
2976 print_binary (file, "eq", exp);
2977 break;
2978 case O_ne:
2979 print_binary (file, "ne", exp);
2980 break;
2981 case O_lt:
2982 print_binary (file, "lt", exp);
2983 break;
2984 case O_le:
2985 print_binary (file, "le", exp);
2986 break;
2987 case O_ge:
2988 print_binary (file, "ge", exp);
2989 break;
2990 case O_gt:
2991 print_binary (file, "gt", exp);
2992 break;
2993 case O_logical_and:
2994 print_binary (file, "logical_and", exp);
2995 break;
2996 case O_logical_or:
2997 print_binary (file, "logical_or", exp);
2998 break;
2999 case O_add:
3000 indent_level++;
3001 fprintf (file, "add\n%*s<", indent_level * 4, "");
3002 print_symbol_value_1 (file, exp->X_add_symbol);
3003 fprintf (file, ">\n%*s<", indent_level * 4, "");
3004 print_symbol_value_1 (file, exp->X_op_symbol);
3005 fprintf (file, ">");
3006 goto maybe_print_addnum;
3007 case O_subtract:
3008 indent_level++;
3009 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3010 print_symbol_value_1 (file, exp->X_add_symbol);
3011 fprintf (file, ">\n%*s<", indent_level * 4, "");
3012 print_symbol_value_1 (file, exp->X_op_symbol);
3013 fprintf (file, ">");
3014 goto maybe_print_addnum;
3015 default:
3016 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3017 break;
3019 fflush (stdout);
3022 void
3023 print_expr (expressionS *exp)
3025 print_expr_1 (stderr, exp);
3026 fprintf (stderr, "\n");
3029 void
3030 symbol_print_statistics (FILE *file)
3032 hash_print_statistics (file, "symbol table", sy_hash);
3033 hash_print_statistics (file, "mini local symbol table", local_hash);
3034 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3035 local_symbol_count, local_symbol_conversion_count);
3038 #ifdef OBJ_COMPLEX_RELC
3040 /* Convert given symbol to a new complex-relocation symbol name. This
3041 may be a recursive function, since it might be called for non-leaf
3042 nodes (plain symbols) in the expression tree. The caller owns the
3043 returning string, so should free it eventually. Errors are
3044 indicated via as_bad and a NULL return value. The given symbol
3045 is marked with sy_used_in_reloc. */
3047 char *
3048 symbol_relc_make_sym (symbolS * sym)
3050 char * terminal = NULL;
3051 const char * sname;
3052 char typetag;
3053 int sname_len;
3055 gas_assert (sym != NULL);
3057 /* Recurse to symbol_relc_make_expr if this symbol
3058 is defined as an expression or a plain value. */
3059 if ( S_GET_SEGMENT (sym) == expr_section
3060 || S_GET_SEGMENT (sym) == absolute_section)
3061 return symbol_relc_make_expr (& sym->sy_value);
3063 /* This may be a "fake symbol" L0\001, referring to ".".
3064 Write out a special null symbol to refer to this position. */
3065 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3066 return xstrdup (".");
3068 /* We hope this is a plain leaf symbol. Construct the encoding
3069 as {S,s}II...:CCCCCCC....
3070 where 'S'/'s' means section symbol / plain symbol
3071 III is decimal for the symbol name length
3072 CCC is the symbol name itself. */
3073 symbol_mark_used_in_reloc (sym);
3075 sname = S_GET_NAME (sym);
3076 sname_len = strlen (sname);
3077 typetag = symbol_section_p (sym) ? 'S' : 's';
3079 terminal = xmalloc (1 /* S or s */
3080 + 8 /* sname_len in decimal */
3081 + 1 /* _ spacer */
3082 + sname_len /* name itself */
3083 + 1 /* \0 */ );
3085 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3086 return terminal;
3089 /* Convert given value to a new complex-relocation symbol name. This
3090 is a non-recursive function, since it is be called for leaf nodes
3091 (plain values) in the expression tree. The caller owns the
3092 returning string, so should free() it eventually. No errors. */
3094 char *
3095 symbol_relc_make_value (offsetT val)
3097 char * terminal = xmalloc (28); /* Enough for long long. */
3099 terminal[0] = '#';
3100 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3101 return terminal;
3104 /* Convert given expression to a new complex-relocation symbol name.
3105 This is a recursive function, since it traverses the entire given
3106 expression tree. The caller owns the returning string, so should
3107 free() it eventually. Errors are indicated via as_bad() and a NULL
3108 return value. */
3110 char *
3111 symbol_relc_make_expr (expressionS * exp)
3113 char * opstr = NULL; /* Operator prefix string. */
3114 int arity = 0; /* Arity of this operator. */
3115 char * operands[3]; /* Up to three operands. */
3116 char * concat_string = NULL;
3118 operands[0] = operands[1] = operands[2] = NULL;
3120 gas_assert (exp != NULL);
3122 /* Match known operators -> fill in opstr, arity, operands[] and fall
3123 through to construct subexpression fragments; may instead return
3124 string directly for leaf nodes. */
3126 /* See expr.h for the meaning of all these enums. Many operators
3127 have an unnatural arity (X_add_number implicitly added). The
3128 conversion logic expands them to explicit "+" subexpressions. */
3130 switch (exp->X_op)
3132 default:
3133 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3134 break;
3136 /* Leaf nodes. */
3137 case O_constant:
3138 return symbol_relc_make_value (exp->X_add_number);
3140 case O_symbol:
3141 if (exp->X_add_number)
3143 arity = 2;
3144 opstr = "+";
3145 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3146 operands[1] = symbol_relc_make_value (exp->X_add_number);
3147 break;
3149 else
3150 return symbol_relc_make_sym (exp->X_add_symbol);
3152 /* Helper macros for nesting nodes. */
3154 #define HANDLE_XADD_OPT1(str_) \
3155 if (exp->X_add_number) \
3157 arity = 2; \
3158 opstr = "+:" str_; \
3159 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3160 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3161 break; \
3163 else \
3165 arity = 1; \
3166 opstr = str_; \
3167 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3169 break
3171 #define HANDLE_XADD_OPT2(str_) \
3172 if (exp->X_add_number) \
3174 arity = 3; \
3175 opstr = "+:" str_; \
3176 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3177 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3178 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3180 else \
3182 arity = 2; \
3183 opstr = str_; \
3184 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3185 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3187 break
3189 /* Nesting nodes. */
3191 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3192 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3193 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3194 case O_multiply: HANDLE_XADD_OPT2 ("*");
3195 case O_divide: HANDLE_XADD_OPT2 ("/");
3196 case O_modulus: HANDLE_XADD_OPT2 ("%");
3197 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3198 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3199 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3200 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3201 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3202 case O_add: HANDLE_XADD_OPT2 ("+");
3203 case O_subtract: HANDLE_XADD_OPT2 ("-");
3204 case O_eq: HANDLE_XADD_OPT2 ("==");
3205 case O_ne: HANDLE_XADD_OPT2 ("!=");
3206 case O_lt: HANDLE_XADD_OPT2 ("<");
3207 case O_le: HANDLE_XADD_OPT2 ("<=");
3208 case O_ge: HANDLE_XADD_OPT2 (">=");
3209 case O_gt: HANDLE_XADD_OPT2 (">");
3210 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3211 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3214 /* Validate & reject early. */
3215 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3216 opstr = NULL;
3217 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3218 opstr = NULL;
3219 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3220 opstr = NULL;
3222 if (opstr == NULL)
3223 concat_string = NULL;
3224 else
3226 /* Allocate new string; include inter-operand padding gaps etc. */
3227 concat_string = xmalloc (strlen (opstr)
3229 + (arity >= 1 ? (strlen (operands[0]) + 1 ) : 0)
3230 + (arity >= 2 ? (strlen (operands[1]) + 1 ) : 0)
3231 + (arity >= 3 ? (strlen (operands[2]) + 0 ) : 0)
3232 + 1);
3233 gas_assert (concat_string != NULL);
3235 /* Format the thing. */
3236 sprintf (concat_string,
3237 (arity == 0 ? "%s" :
3238 arity == 1 ? "%s:%s" :
3239 arity == 2 ? "%s:%s:%s" :
3240 /* arity == 3 */ "%s:%s:%s:%s"),
3241 opstr, operands[0], operands[1], operands[2]);
3244 /* Free operand strings (not opstr). */
3245 if (arity >= 1) xfree (operands[0]);
3246 if (arity >= 2) xfree (operands[1]);
3247 if (arity >= 3) xfree (operands[2]);
3249 return concat_string;
3252 #endif