symbols.c revision 1.1.1.11 1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
22
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
26 #include "subsegs.h"
27 #include "write.h"
28 #include "scfi.h"
29
30 #include <limits.h>
31 #ifndef CHAR_BIT
32 #define CHAR_BIT 8
33 #endif
34
35 struct symbol_flags
36 {
37 /* Whether the symbol is a local_symbol. */
38 unsigned int local_symbol : 1;
39
40 /* Weather symbol has been written. */
41 unsigned int written : 1;
42
43 /* Whether symbol value has been completely resolved (used during
44 final pass over symbol table). */
45 unsigned int resolved : 1;
46
47 /* Whether the symbol value is currently being resolved (used to
48 detect loops in symbol dependencies). */
49 unsigned int resolving : 1;
50
51 /* Whether the symbol value is used in a reloc. This is used to
52 ensure that symbols used in relocs are written out, even if they
53 are local and would otherwise not be. */
54 unsigned int used_in_reloc : 1;
55
56 /* Whether the symbol is used as an operand or in an expression.
57 NOTE: Not all the backends keep this information accurate;
58 backends which use this bit are responsible for setting it when
59 a symbol is used in backend routines. */
60 unsigned int used : 1;
61
62 /* Whether the symbol can be re-defined. */
63 unsigned int volatil : 1;
64
65 /* Whether the symbol is a forward reference, and whether such has
66 been determined. */
67 unsigned int forward_ref : 1;
68 unsigned int forward_resolved : 1;
69
70 /* This is set if the symbol is defined in an MRI common section.
71 We handle such sections as single common symbols, so symbols
72 defined within them must be treated specially by the relocation
73 routines. */
74 unsigned int mri_common : 1;
75
76 /* This is set if the symbol is set with a .weakref directive. */
77 unsigned int weakrefr : 1;
78
79 /* This is set when the symbol is referenced as part of a .weakref
80 directive, but only if the symbol was not in the symbol table
81 before. It is cleared as soon as any direct reference to the
82 symbol is present. */
83 unsigned int weakrefd : 1;
84
85 /* Whether the symbol has been marked to be removed by a .symver
86 directive. */
87 unsigned int removed : 1;
88
89 /* Set when a warning about the symbol containing multibyte characters
90 is generated. */
91 unsigned int multibyte_warned : 1;
92 };
93
94 /* A pointer in the symbol may point to either a complete symbol
95 (struct symbol below) or to a local symbol (struct local_symbol
96 defined here). The symbol code can detect the case by examining
97 the first field which is present in both structs.
98
99 We do this because we ordinarily only need a small amount of
100 information for a local symbol. The symbol table takes up a lot of
101 space, and storing less information for a local symbol can make a
102 big difference in assembler memory usage when assembling a large
103 file. */
104
105 struct local_symbol
106 {
107 /* Symbol flags. Only local_symbol and resolved are relevant. */
108 struct symbol_flags flags;
109
110 /* Hash value calculated from name. */
111 hashval_t hash;
112
113 /* The symbol name. */
114 const char *name;
115
116 /* The symbol frag. */
117 fragS *frag;
118
119 /* The symbol section. */
120 asection *section;
121
122 /* The value of the symbol. */
123 valueT value;
124 };
125
126 /* The information we keep for a symbol. The symbol table holds
127 pointers both to this and to local_symbol structures. The first
128 three fields must be identical to struct local_symbol, and the size
129 should be the same as or smaller than struct local_symbol.
130 Fields that don't fit go to an extension structure. */
131
132 struct symbol
133 {
134 /* Symbol flags. */
135 struct symbol_flags flags;
136
137 /* Hash value calculated from name. */
138 hashval_t hash;
139
140 /* The symbol name. */
141 const char *name;
142
143 /* Pointer to the frag this symbol is attached to, if any.
144 Otherwise, NULL. */
145 fragS *frag;
146
147 /* BFD symbol */
148 asymbol *bsym;
149
150 /* Extra symbol fields that won't fit. */
151 struct xsymbol *x;
152 };
153
154 /* Extra fields to make up a full symbol. */
155
156 struct xsymbol
157 {
158 /* The value of the symbol. */
159 expressionS value;
160
161 /* Forwards and backwards chain pointers. */
162 struct symbol *next;
163 struct symbol *previous;
164
165 #ifdef OBJ_SYMFIELD_TYPE
166 OBJ_SYMFIELD_TYPE obj;
167 #endif
168
169 #ifdef TC_SYMFIELD_TYPE
170 TC_SYMFIELD_TYPE tc;
171 #endif
172 };
173
174 typedef union symbol_entry
175 {
176 struct local_symbol lsy;
177 struct symbol sy;
178 } symbol_entry_t;
179
180 /* Hash function for a symbol_entry. */
181
182 static hashval_t
183 hash_symbol_entry (const void *e)
184 {
185 symbol_entry_t *entry = (symbol_entry_t *) e;
186 if (entry->sy.hash == 0)
187 entry->sy.hash = htab_hash_string (entry->sy.name);
188
189 return entry->sy.hash;
190 }
191
192 /* Equality function for a symbol_entry. */
193
194 static int
195 eq_symbol_entry (const void *a, const void *b)
196 {
197 const symbol_entry_t *ea = a;
198 const symbol_entry_t *eb = b;
199
200 return (ea->sy.hash == eb->sy.hash
201 && strcmp (ea->sy.name, eb->sy.name) == 0);
202 }
203
204 static void *
205 symbol_entry_find (htab_t table, const char *name)
206 {
207 hashval_t hash = htab_hash_string (name);
208 symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209 hash, name, 0, 0, 0 } };
210 return htab_find_with_hash (table, &needle, hash);
211 }
212
213
214 /* This is non-zero if symbols are case sensitive, which is the
215 default. */
216 int symbols_case_sensitive = 1;
217
218 #ifndef WORKING_DOT_WORD
219 extern int new_broken_words;
220 #endif
221
222 static htab_t sy_hash;
223
224 /* Below are commented in "symbols.h". */
225 symbolS *symbol_rootP;
226 symbolS *symbol_lastP;
227 symbolS abs_symbol;
228 struct xsymbol abs_symbol_x;
229 symbolS dot_symbol;
230 struct xsymbol dot_symbol_x;
231
232 #ifdef DEBUG_SYMS
233 #define debug_verify_symchain verify_symbol_chain
234 #else
235 #define debug_verify_symchain(root, last) ((void) 0)
236 #endif
237
238 #define DOLLAR_LABEL_CHAR '\001'
239 #define LOCAL_LABEL_CHAR '\002'
240
241 #ifndef TC_LABEL_IS_LOCAL
242 #define TC_LABEL_IS_LOCAL(name) 0
243 #endif
244
245 struct obstack notes;
246
247 /* Utility functions to allocate and duplicate memory on the notes
248 obstack, each like the corresponding function without "notes_"
249 prefix. All of these exit on an allocation failure. */
250
251 void *
252 notes_alloc (size_t size)
253 {
254 return obstack_alloc (¬es, size);
255 }
256
257 void *
258 notes_calloc (size_t n, size_t size)
259 {
260 size_t amt;
261 void *ret;
262 if (gas_mul_overflow (n, size, &amt))
263 {
264 obstack_alloc_failed_handler ();
265 abort ();
266 }
267 ret = notes_alloc (amt);
268 memset (ret, 0, amt);
269 return ret;
270 }
271
272 void *
273 notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
274 {
275 void *ret = obstack_alloc (¬es, alloc_size);
276 memcpy (ret, src, copy_size);
277 if (alloc_size > copy_size)
278 memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279 return ret;
280 }
281
282 char *
283 notes_strdup (const char *str)
284 {
285 size_t len = strlen (str) + 1;
286 return notes_memdup (str, len, len);
287 }
288
289 char *
290 notes_concat (const char *first, ...)
291 {
292 va_list args;
293 const char *str;
294
295 va_start (args, first);
296 for (str = first; str; str = va_arg (args, const char *))
297 {
298 size_t size = strlen (str);
299 obstack_grow (¬es, str, size);
300 }
301 va_end (args);
302 obstack_1grow (¬es, 0);
303 return obstack_finish (¬es);
304 }
305
306 /* Use with caution! Frees PTR and all more recently allocated memory
307 on the notes obstack. */
308
309 void
310 notes_free (void *ptr)
311 {
312 obstack_free (¬es, ptr);
313 }
314
315 #ifdef TE_PE
316 /* The name of an external symbol which is
317 used to make weak PE symbol names unique. */
318 const char * an_external_name;
319 #endif
320
321 /* Return a pointer to a new symbol. Die if we can't make a new
322 symbol. Fill in the symbol's values. Add symbol to end of symbol
323 chain.
324
325 This function should be called in the general case of creating a
326 symbol. However, if the output file symbol table has already been
327 set, and you are certain that this symbol won't be wanted in the
328 output file, you can call symbol_create. */
329
330 symbolS *
331 symbol_new (const char *name, segT segment, fragS *frag, valueT valu)
332 {
333 symbolS *symbolP = symbol_create (name, segment, frag, valu);
334
335 /* Link to end of symbol chain. */
336 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
337
338 return symbolP;
339 }
340
341 /* Save a symbol name on a permanent obstack, and convert it according
342 to the object file format. */
343
344 static const char *
345 save_symbol_name (const char *name)
346 {
347 char *ret;
348
349 gas_assert (name != NULL);
350 ret = notes_strdup (name);
351
352 #ifdef tc_canonicalize_symbol_name
353 ret = tc_canonicalize_symbol_name (ret);
354 #endif
355
356 if (! symbols_case_sensitive)
357 {
358 char *s;
359
360 for (s = ret; *s != '\0'; s++)
361 *s = TOUPPER (*s);
362 }
363
364 return ret;
365 }
366
367 static void
368 symbol_init (symbolS *symbolP, const char *name, asection *sec,
369 fragS *frag, valueT valu)
370 {
371 symbolP->frag = frag;
372 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373 if (symbolP->bsym == NULL)
374 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375 symbolP->bsym->name = name;
376 symbolP->bsym->section = sec;
377
378 if (multibyte_handling == multibyte_warn_syms
379 && ! symbolP->flags.local_symbol
380 && sec != undefined_section
381 && ! symbolP->flags.multibyte_warned
382 && scan_for_multibyte_characters ((const unsigned char *) name,
383 (const unsigned char *) name + strlen (name),
384 false /* Do not warn. */))
385 {
386 as_warn (_("symbol '%s' contains multibyte characters"), name);
387 symbolP->flags.multibyte_warned = 1;
388 }
389
390 S_SET_VALUE (symbolP, valu);
391 if (sec == reg_section)
392 symbolP->x->value.X_op = O_register;
393
394 symbol_clear_list_pointers (symbolP);
395
396 obj_symbol_new_hook (symbolP);
397
398 #ifdef tc_symbol_new_hook
399 tc_symbol_new_hook (symbolP);
400 #endif
401 }
402
403 /* Create a symbol. NAME is copied, the caller can destroy/modify. */
404
405 symbolS *
406 symbol_create (const char *name, segT segment, fragS *frag, valueT valu)
407 {
408 const char *preserved_copy_of_name;
409 symbolS *symbolP;
410 size_t size;
411
412 preserved_copy_of_name = save_symbol_name (name);
413
414 size = sizeof (symbolS) + sizeof (struct xsymbol);
415 symbolP = notes_alloc (size);
416
417 /* symbol must be born in some fixed state. This seems as good as any. */
418 memset (symbolP, 0, size);
419 symbolP->name = preserved_copy_of_name;
420 symbolP->x = (struct xsymbol *) (symbolP + 1);
421
422 symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
423
424 return symbolP;
425 }
426
427
429 /* Local symbol support. If we can get away with it, we keep only a
430 small amount of information for local symbols. */
431
432 /* Used for statistics. */
433
434 static unsigned long local_symbol_count;
435 static unsigned long local_symbol_conversion_count;
436
437 /* Create a local symbol and insert it into the local hash table. */
438
439 struct local_symbol *
440 local_symbol_make (const char *name, segT section, fragS *frag, valueT val)
441 {
442 const char *name_copy;
443 struct local_symbol *ret;
444 struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
445
446 ++local_symbol_count;
447
448 name_copy = save_symbol_name (name);
449
450 ret = notes_alloc (sizeof *ret);
451 ret->flags = flags;
452 ret->hash = 0;
453 ret->name = name_copy;
454 ret->frag = frag;
455 ret->section = section;
456 ret->value = val;
457
458 htab_insert (sy_hash, ret, 1);
459
460 return ret;
461 }
462
463 /* Convert a local symbol into a real symbol. */
464
465 static symbolS *
466 local_symbol_convert (void *sym)
467 {
468 symbol_entry_t *ent = sym;
469 struct xsymbol *xtra;
470 valueT val;
471
472 gas_assert (ent->lsy.flags.local_symbol);
473
474 ++local_symbol_conversion_count;
475
476 xtra = notes_alloc (sizeof (*xtra));
477 memset (xtra, 0, sizeof (*xtra));
478 val = ent->lsy.value;
479 ent->sy.x = xtra;
480
481 /* Local symbols are always either defined or used. */
482 ent->sy.flags.used = 1;
483 ent->sy.flags.local_symbol = 0;
484
485 symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
486 symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
487
488 return &ent->sy;
489 }
490
491 static void
493 define_sym_at_dot (symbolS *symbolP)
494 {
495 symbolP->frag = frag_now;
496 S_SET_VALUE (symbolP, frag_now_fix ());
497 S_SET_SEGMENT (symbolP, now_seg);
498 }
499
500 /* We have just seen "<name>:".
501 Creates a struct symbol unless it already exists.
502
503 Gripes if we are redefining a symbol incompatibly (and ignores it). */
504
505 symbolS *
506 colon (/* Just seen "x:" - rattle symbols & frags. */
507 const char *sym_name /* Symbol name, as a canonical string. */
508 /* We copy this string: OK to alter later. */)
509 {
510 symbolS *symbolP; /* Symbol we are working with. */
511
512 /* Sun local labels go out of scope whenever a non-local symbol is
513 defined. */
514 if (LOCAL_LABELS_DOLLAR
515 && !bfd_is_local_label_name (stdoutput, sym_name))
516 dollar_label_clear ();
517
518 #ifndef WORKING_DOT_WORD
519 if (new_broken_words)
520 {
521 struct broken_word *a;
522 int possible_bytes;
523 fragS *frag_tmp;
524 char *frag_opcode;
525
526 if (now_seg == absolute_section)
527 {
528 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
529 return NULL;
530 }
531
532 possible_bytes = (md_short_jump_size
533 + new_broken_words * md_long_jump_size);
534
535 frag_tmp = frag_now;
536 frag_opcode = frag_var (rs_broken_word, possible_bytes, possible_bytes,
537 0, (symbolS *) broken_words, 0, NULL);
538
539 /* We want to store the pointer to where to insert the jump
540 table in the fr_opcode of the rs_broken_word frag. This
541 requires a little hackery. */
542 while (frag_tmp
543 && (frag_tmp->fr_type != rs_broken_word
544 || frag_tmp->fr_opcode))
545 frag_tmp = frag_tmp->fr_next;
546 know (frag_tmp);
547 frag_tmp->fr_opcode = frag_opcode;
548 new_broken_words = 0;
549
550 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
551 a->dispfrag = frag_tmp;
552 }
553 #endif /* WORKING_DOT_WORD */
554
555 #ifdef obj_frob_colon
556 obj_frob_colon (sym_name);
557 #endif
558
559 if ((symbolP = symbol_find (sym_name)) != 0)
560 {
561 S_CLEAR_WEAKREFR (symbolP);
562 #ifdef RESOLVE_SYMBOL_REDEFINITION
563 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
564 return symbolP;
565 #endif
566 /* Now check for undefined symbols. */
567 if (symbolP->flags.local_symbol)
568 {
569 struct local_symbol *locsym = (struct local_symbol *) symbolP;
570
571 if (locsym->section != undefined_section
572 && (locsym->frag != frag_now
573 || locsym->section != now_seg
574 || locsym->value != frag_now_fix ()))
575 {
576 as_bad (_("symbol `%s' is already defined"), sym_name);
577 return symbolP;
578 }
579
580 locsym->section = now_seg;
581 locsym->frag = frag_now;
582 locsym->value = frag_now_fix ();
583 }
584 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
585 || S_IS_COMMON (symbolP)
586 || S_IS_VOLATILE (symbolP))
587 {
588 if (S_IS_VOLATILE (symbolP))
589 {
590 symbolP = symbol_clone (symbolP, 1);
591 S_SET_VALUE (symbolP, 0);
592 S_CLEAR_VOLATILE (symbolP);
593 }
594 if (S_GET_VALUE (symbolP) == 0)
595 {
596 define_sym_at_dot (symbolP);
597 #ifdef N_UNDF
598 know (N_UNDF == 0);
599 #endif /* if we have one, it better be zero. */
600
601 }
602 else
603 {
604 /* There are still several cases to check:
605
606 A .comm/.lcomm symbol being redefined as initialized
607 data is OK
608
609 A .comm/.lcomm symbol being redefined with a larger
610 size is also OK
611
612 This only used to be allowed on VMS gas, but Sun cc
613 on the sparc also depends on it. */
614
615 if (((!S_IS_DEBUG (symbolP)
616 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
617 && S_IS_EXTERNAL (symbolP))
618 || S_GET_SEGMENT (symbolP) == bss_section)
619 && (now_seg == data_section
620 || now_seg == bss_section
621 || now_seg == S_GET_SEGMENT (symbolP)))
622 {
623 /* Select which of the 2 cases this is. */
624 if (now_seg != data_section)
625 {
626 /* New .comm for prev .comm symbol.
627
628 If the new size is larger we just change its
629 value. If the new size is smaller, we ignore
630 this symbol. */
631 if (S_GET_VALUE (symbolP) < frag_now_fix ())
632 S_SET_VALUE (symbolP, frag_now_fix ());
633 }
634 else
635 {
636 /* It is a .comm/.lcomm being converted to initialized
637 data. */
638 define_sym_at_dot (symbolP);
639 }
640 }
641 else
642 {
643 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
644 static const char *od_buf = "";
645 #else
646 char od_buf[100];
647 od_buf[0] = '\0';
648 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
649 sprintf (od_buf, "%d.%d.",
650 S_GET_OTHER (symbolP),
651 S_GET_DESC (symbolP));
652 #endif
653 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
654 sym_name,
655 segment_name (S_GET_SEGMENT (symbolP)),
656 od_buf,
657 (long) S_GET_VALUE (symbolP));
658 }
659 } /* if the undefined symbol has no value */
660 }
661 else
662 {
663 /* Don't blow up if the definition is the same. */
664 if (!(frag_now == symbolP->frag
665 && S_GET_VALUE (symbolP) == frag_now_fix ()
666 && S_GET_SEGMENT (symbolP) == now_seg))
667 {
668 as_bad (_("symbol `%s' is already defined"), sym_name);
669 symbolP = symbol_clone (symbolP, 0);
670 define_sym_at_dot (symbolP);
671 }
672 }
673
674 }
675 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
676 {
677 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
678 frag_now_fix ());
679 }
680 else
681 {
682 symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
683
684 symbol_table_insert (symbolP);
685 }
686
687 if (mri_common_symbol != NULL)
688 {
689 /* This symbol is actually being defined within an MRI common
690 section. This requires special handling. */
691 if (symbolP->flags.local_symbol)
692 symbolP = local_symbol_convert (symbolP);
693 symbolP->x->value.X_op = O_symbol;
694 symbolP->x->value.X_add_symbol = mri_common_symbol;
695 symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
696 symbolP->frag = &zero_address_frag;
697 S_SET_SEGMENT (symbolP, expr_section);
698 symbolP->flags.mri_common = 1;
699 }
700
701 #ifdef tc_frob_label
702 tc_frob_label (symbolP);
703 #endif
704 #ifdef obj_frob_label
705 obj_frob_label (symbolP);
706 #endif
707 if (flag_synth_cfi)
708 ginsn_frob_label (symbolP);
709
710 return symbolP;
711 }
712
713 /* Die if we can't insert the symbol. */
715
716 void
717 symbol_table_insert (symbolS *symbolP)
718 {
719 know (symbolP);
720
721 htab_insert (sy_hash, symbolP, 1);
722 }
723
724 /* If a symbol name does not exist, create it as undefined, and insert
726 it into the symbol table. Return a pointer to it. */
727
728 symbolS *
729 symbol_find_or_make (const char *name)
730 {
731 symbolS *symbolP;
732
733 symbolP = symbol_find (name);
734
735 if (symbolP == NULL)
736 {
737 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
738 {
739 symbolP = md_undefined_symbol ((char *) name);
740 if (symbolP != NULL)
741 return symbolP;
742
743 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
744 &zero_address_frag, 0);
745 return symbolP;
746 }
747
748 symbolP = symbol_make (name);
749
750 symbol_table_insert (symbolP);
751 } /* if symbol wasn't found */
752
753 return symbolP;
754 }
755
756 symbolS *
757 symbol_make (const char *name)
758 {
759 symbolS *symbolP;
760
761 /* Let the machine description default it, e.g. for register names. */
762 symbolP = md_undefined_symbol ((char *) name);
763
764 if (!symbolP)
765 symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
766
767 return symbolP;
768 }
769
770 symbolS *
771 symbol_clone (symbolS *orgsymP, int replace)
772 {
773 symbolS *newsymP;
774 asymbol *bsymorg, *bsymnew;
775
776 /* Make sure we never clone the dot special symbol. */
777 gas_assert (orgsymP != &dot_symbol);
778
779 /* When cloning a local symbol it isn't absolutely necessary to
780 convert the original, but converting makes the code much
781 simpler to cover this unexpected case. As of 2020-08-21
782 symbol_clone won't be called on a local symbol. */
783 if (orgsymP->flags.local_symbol)
784 orgsymP = local_symbol_convert (orgsymP);
785 bsymorg = orgsymP->bsym;
786
787 newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
788 *newsymP = *orgsymP;
789 newsymP->x = (struct xsymbol *) (newsymP + 1);
790 *newsymP->x = *orgsymP->x;
791 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
792 if (bsymnew == NULL)
793 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
794 newsymP->bsym = bsymnew;
795 bsymnew->name = bsymorg->name;
796 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
797 bsymnew->section = bsymorg->section;
798 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
799 bfd_asymbol_bfd (bsymnew), bsymnew);
800
801 #ifdef obj_symbol_clone_hook
802 obj_symbol_clone_hook (newsymP, orgsymP);
803 #endif
804
805 #ifdef tc_symbol_clone_hook
806 tc_symbol_clone_hook (newsymP, orgsymP);
807 #endif
808
809 if (replace)
810 {
811 if (orgsymP->x->previous != NULL)
812 orgsymP->x->previous->x->next = newsymP;
813 else
814 symbol_rootP = newsymP;
815 if (orgsymP->x->next != NULL)
816 orgsymP->x->next->x->previous = newsymP;
817 else
818 symbol_lastP = newsymP;
819
820 /* Symbols that won't be output can't be external. */
821 S_CLEAR_EXTERNAL (orgsymP);
822 orgsymP->x->previous = orgsymP->x->next = orgsymP;
823 debug_verify_symchain (symbol_rootP, symbol_lastP);
824
825 symbol_table_insert (newsymP);
826 }
827 else
828 {
829 /* Symbols that won't be output can't be external. */
830 S_CLEAR_EXTERNAL (newsymP);
831 newsymP->x->previous = newsymP->x->next = newsymP;
832 }
833
834 return newsymP;
835 }
836
837 /* Referenced symbols, if they are forward references, need to be cloned
838 (without replacing the original) so that the value of the referenced
839 symbols at the point of use is saved by the clone. */
840
841 #undef symbol_clone_if_forward_ref
842 symbolS *
843 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
844 {
845 if (symbolP
846 && !symbolP->flags.local_symbol
847 && !symbolP->flags.forward_resolved)
848 {
849 symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
850 symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
851 symbolS *add_symbol = orig_add_symbol;
852 symbolS *op_symbol = orig_op_symbol;
853
854 if (symbolP->flags.forward_ref)
855 is_forward = 1;
856
857 if (is_forward)
858 {
859 /* assign_symbol() clones volatile symbols; pre-existing expressions
860 hold references to the original instance, but want the current
861 value. Just repeat the lookup. */
862 if (add_symbol && S_IS_VOLATILE (add_symbol))
863 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
864 if (op_symbol && S_IS_VOLATILE (op_symbol))
865 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
866 }
867
868 /* Re-using resolving here, as this routine cannot get called from
869 symbol resolution code. */
870 if ((symbolP->bsym->section == expr_section
871 || symbolP->flags.forward_ref)
872 && !symbolP->flags.resolving)
873 {
874 symbolP->flags.resolving = 1;
875 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
876 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
877 symbolP->flags.resolving = 0;
878 }
879
880 if (symbolP->flags.forward_ref
881 || add_symbol != orig_add_symbol
882 || op_symbol != orig_op_symbol)
883 {
884 if (symbolP != &dot_symbol)
885 {
886 symbolP = symbol_clone (symbolP, 0);
887 symbolP->flags.resolving = 0;
888 }
889 else
890 {
891 symbolP = symbol_temp_new_now ();
892 #ifdef tc_new_dot_label
893 tc_new_dot_label (symbolP);
894 #endif
895 }
896 }
897
898 symbolP->x->value.X_add_symbol = add_symbol;
899 symbolP->x->value.X_op_symbol = op_symbol;
900 symbolP->flags.forward_resolved = 1;
901 }
902
903 return symbolP;
904 }
905
906 symbolS *
907 symbol_temp_new (segT seg, fragS *frag, valueT ofs)
908 {
909 return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
910 }
911
912 symbolS *
913 symbol_temp_new_now (void)
914 {
915 return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
916 }
917
918 symbolS *
919 symbol_temp_new_now_octets (void)
920 {
921 return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
922 }
923
924 symbolS *
925 symbol_temp_make (void)
926 {
927 return symbol_make (FAKE_LABEL_NAME);
928 }
929
930 /* Implement symbol table lookup.
931 In: A symbol's name as a string: '\0' can't be part of a symbol name.
932 Out: NULL if the name was not in the symbol table, else the address
933 of a struct symbol associated with that name. */
934
935 symbolS *
936 symbol_find_exact (const char *name)
937 {
938 return symbol_find_exact_noref (name, 0);
939 }
940
941 symbolS *
942 symbol_find_exact_noref (const char *name, int noref)
943 {
944 symbolS *sym = symbol_entry_find (sy_hash, name);
945
946 /* Any references to the symbol, except for the reference in
947 .weakref, must clear this flag, such that the symbol does not
948 turn into a weak symbol. Note that we don't have to handle the
949 local_symbol case, since a weakrefd is always promoted out of the
950 local_symbol table when it is turned into a weak symbol. */
951 if (sym && ! noref)
952 S_CLEAR_WEAKREFD (sym);
953
954 return sym;
955 }
956
957 symbolS *
958 symbol_find (const char *name)
959 {
960 return symbol_find_noref (name, 0);
961 }
962
963 symbolS *
964 symbol_find_noref (const char *name, int noref)
965 {
966 symbolS * result;
967 char * copy = NULL;
968
969 #ifdef tc_canonicalize_symbol_name
970 {
971 copy = xstrdup (name);
972 name = tc_canonicalize_symbol_name (copy);
973 }
974 #endif
975
976 if (! symbols_case_sensitive)
977 {
978 const char *orig;
979 char *copy2 = NULL;
980 unsigned char c;
981
982 orig = name;
983 if (copy != NULL)
984 copy2 = copy;
985 name = copy = XNEWVEC (char, strlen (name) + 1);
986
987 while ((c = *orig++) != '\0')
988 *copy++ = TOUPPER (c);
989 *copy = '\0';
990
991 free (copy2);
992 copy = (char *) name;
993 }
994
995 result = symbol_find_exact_noref (name, noref);
996 free (copy);
997 return result;
998 }
999
1000 /* Once upon a time, symbols were kept in a singly linked list. At
1001 least coff needs to be able to rearrange them from time to time, for
1002 which a doubly linked list is much more convenient. Loic did these
1003 as macros which seemed dangerous to me so they're now functions.
1004 xoxorich. */
1005
1006 /* Link symbol ADDME after symbol TARGET in the chain. */
1007
1008 void
1009 symbol_append (symbolS *addme, symbolS *target,
1010 symbolS **rootPP, symbolS **lastPP)
1011 {
1012 extern int symbol_table_frozen;
1013 if (symbol_table_frozen)
1014 abort ();
1015 if (addme->flags.local_symbol)
1016 abort ();
1017 if (target != NULL && target->flags.local_symbol)
1018 abort ();
1019
1020 if (target == NULL)
1021 {
1022 know (*rootPP == NULL);
1023 know (*lastPP == NULL);
1024 addme->x->next = NULL;
1025 addme->x->previous = NULL;
1026 *rootPP = addme;
1027 *lastPP = addme;
1028 return;
1029 }
1030
1031 if (target->x->next != NULL)
1032 target->x->next->x->previous = addme;
1033 else
1034 *lastPP = addme;
1035
1036 addme->x->next = target->x->next;
1037 target->x->next = addme;
1038 addme->x->previous = target;
1039
1040 debug_verify_symchain (symbol_rootP, symbol_lastP);
1041 }
1042
1043 /* Set the chain pointers of SYMBOL to null. */
1044
1045 void
1046 symbol_clear_list_pointers (symbolS *symbolP)
1047 {
1048 if (symbolP->flags.local_symbol)
1049 abort ();
1050 symbolP->x->next = NULL;
1051 symbolP->x->previous = NULL;
1052 }
1053
1054 /* Remove SYMBOLP from the list. */
1055
1056 void
1057 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1058 {
1059 if (symbolP->flags.local_symbol)
1060 abort ();
1061
1062 if (symbolP->x->previous != NULL)
1063 symbolP->x->previous->x->next = symbolP->x->next;
1064 else
1065 *rootPP = symbolP->x->next;
1066
1067 if (symbolP->x->next != NULL)
1068 symbolP->x->next->x->previous = symbolP->x->previous;
1069 else
1070 *lastPP = symbolP->x->previous;
1071
1072 debug_verify_symchain (*rootPP, *lastPP);
1073 }
1074
1075 /* Link symbol ADDME before symbol TARGET in the chain. */
1076
1077 void
1078 symbol_insert (symbolS *addme, symbolS *target,
1079 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
1080 {
1081 extern int symbol_table_frozen;
1082 if (symbol_table_frozen)
1083 abort ();
1084 if (addme->flags.local_symbol)
1085 abort ();
1086 if (target->flags.local_symbol)
1087 abort ();
1088
1089 if (target->x->previous != NULL)
1090 target->x->previous->x->next = addme;
1091 else
1092 *rootPP = addme;
1093
1094 addme->x->previous = target->x->previous;
1095 target->x->previous = addme;
1096 addme->x->next = target;
1097
1098 debug_verify_symchain (*rootPP, *lastPP);
1099 }
1100
1101 void
1102 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
1103 {
1104 symbolS *symbolP = rootP;
1105
1106 if (symbolP == NULL)
1107 return;
1108
1109 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1110 {
1111 gas_assert (symbolP->bsym != NULL);
1112 gas_assert (symbolP->flags.local_symbol == 0);
1113 gas_assert (symbolP->x->next->x->previous == symbolP);
1114 }
1115
1116 gas_assert (lastP == symbolP);
1117 }
1118
1119 int
1120 symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1121 {
1122 return (!s->flags.local_symbol
1123 && ((s->x->next != s
1124 && s->x->next != NULL
1125 && s->x->next->x->previous == s)
1126 || s == lastPP)
1127 && ((s->x->previous != s
1128 && s->x->previous != NULL
1129 && s->x->previous->x->next == s)
1130 || s == rootPP));
1131 }
1132
1133 #ifdef OBJ_COMPLEX_RELC
1134
1135 static int
1136 use_complex_relocs_for (symbolS * symp)
1137 {
1138 switch (symp->x->value.X_op)
1139 {
1140 case O_constant:
1141 return 0;
1142
1143 case O_multiply:
1144 case O_divide:
1145 case O_modulus:
1146 case O_left_shift:
1147 case O_right_shift:
1148 case O_bit_inclusive_or:
1149 case O_bit_or_not:
1150 case O_bit_exclusive_or:
1151 case O_bit_and:
1152 case O_add:
1153 case O_subtract:
1154 case O_eq:
1155 case O_ne:
1156 case O_lt:
1157 case O_le:
1158 case O_ge:
1159 case O_gt:
1160 case O_logical_and:
1161 case O_logical_or:
1162 if ((S_IS_COMMON (symp->x->value.X_op_symbol)
1163 || S_IS_LOCAL (symp->x->value.X_op_symbol))
1164 && S_IS_DEFINED (symp->x->value.X_op_symbol)
1165 && S_GET_SEGMENT (symp->x->value.X_op_symbol) != expr_section)
1166 {
1167 case O_symbol:
1168 case O_symbol_rva:
1169 case O_uminus:
1170 case O_bit_not:
1171 case O_logical_not:
1172 if ((S_IS_COMMON (symp->x->value.X_add_symbol)
1173 || S_IS_LOCAL (symp->x->value.X_add_symbol))
1174 && S_IS_DEFINED (symp->x->value.X_add_symbol)
1175 && S_GET_SEGMENT (symp->x->value.X_add_symbol) != expr_section)
1176 return 0;
1177 }
1178 break;
1179
1180 default:
1181 break;
1182 }
1183 return 1;
1184 }
1185 #endif
1186
1187 static void
1188 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
1189 {
1190 const char *file;
1191 unsigned int line;
1192 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1193 segT seg_right = S_GET_SEGMENT (right);
1194 const char *opname;
1195
1196 switch (op)
1197 {
1198 default:
1199 abort ();
1200 return;
1201
1202 case O_uminus: opname = "-"; break;
1203 case O_bit_not: opname = "~"; break;
1204 case O_logical_not: opname = "!"; break;
1205 case O_multiply: opname = "*"; break;
1206 case O_divide: opname = "/"; break;
1207 case O_modulus: opname = "%"; break;
1208 case O_left_shift: opname = "<<"; break;
1209 case O_right_shift: opname = ">>"; break;
1210 case O_bit_inclusive_or: opname = "|"; break;
1211 case O_bit_or_not: opname = "|~"; break;
1212 case O_bit_exclusive_or: opname = "^"; break;
1213 case O_bit_and: opname = "&"; break;
1214 case O_add: opname = "+"; break;
1215 case O_subtract: opname = "-"; break;
1216 case O_eq: opname = "=="; break;
1217 case O_ne: opname = "!="; break;
1218 case O_lt: opname = "<"; break;
1219 case O_le: opname = "<="; break;
1220 case O_ge: opname = ">="; break;
1221 case O_gt: opname = ">"; break;
1222 case O_logical_and: opname = "&&"; break;
1223 case O_logical_or: opname = "||"; break;
1224 }
1225
1226 if (expr_symbol_where (symp, &file, &line))
1227 {
1228 if (left)
1229 as_bad_where (file, line,
1230 _("invalid operands (%s and %s sections) for `%s'"),
1231 seg_left->name, seg_right->name, opname);
1232 else
1233 as_bad_where (file, line,
1234 _("invalid operand (%s section) for `%s'"),
1235 seg_right->name, opname);
1236 }
1237 else
1238 {
1239 const char *sname = S_GET_NAME (symp);
1240
1241 if (left)
1242 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1243 seg_left->name, seg_right->name, opname, sname);
1244 else
1245 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1246 seg_right->name, opname, sname);
1247 }
1248 }
1249
1250 /* Resolve the value of a symbol. This is called during the final
1251 pass over the symbol table to resolve any symbols with complex
1252 values. */
1253
1254 valueT
1255 resolve_symbol_value (symbolS *symp)
1256 {
1257 int resolved;
1258 valueT final_val;
1259 segT final_seg;
1260
1261 if (symp->flags.local_symbol)
1262 {
1263 struct local_symbol *locsym = (struct local_symbol *) symp;
1264
1265 final_val = locsym->value;
1266 if (locsym->flags.resolved)
1267 return final_val;
1268
1269 /* Symbols whose section has SEC_ELF_OCTETS set,
1270 resolve to octets instead of target bytes. */
1271 if (locsym->section->flags & SEC_OCTETS)
1272 final_val += locsym->frag->fr_address;
1273 else
1274 final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1275
1276 if (finalize_syms)
1277 {
1278 locsym->value = final_val;
1279 locsym->flags.resolved = 1;
1280 }
1281
1282 return final_val;
1283 }
1284
1285 if (symp->flags.resolved)
1286 {
1287 final_val = 0;
1288 while (symp->x->value.X_op == O_symbol)
1289 {
1290 final_val += symp->x->value.X_add_number;
1291 symp = symp->x->value.X_add_symbol;
1292 if (symp->flags.local_symbol)
1293 {
1294 struct local_symbol *locsym = (struct local_symbol *) symp;
1295 final_val += locsym->value;
1296 return final_val;
1297 }
1298 if (!symp->flags.resolved)
1299 return 0;
1300 }
1301 if (symp->x->value.X_op == O_constant)
1302 final_val += symp->x->value.X_add_number;
1303 else
1304 final_val = 0;
1305 return final_val;
1306 }
1307
1308 resolved = 0;
1309 final_seg = S_GET_SEGMENT (symp);
1310
1311 if (symp->flags.resolving)
1312 {
1313 if (finalize_syms)
1314 as_bad (_("symbol definition loop encountered at `%s'"),
1315 S_GET_NAME (symp));
1316 final_val = 0;
1317 resolved = 1;
1318 }
1319 #ifdef OBJ_COMPLEX_RELC
1320 else if (final_seg == expr_section
1321 && use_complex_relocs_for (symp))
1322 {
1323 symbolS * relc_symbol = NULL;
1324 char * relc_symbol_name = NULL;
1325
1326 relc_symbol_name = symbol_relc_make_expr (& symp->x->value);
1327
1328 /* For debugging, print out conversion input & output. */
1329 #ifdef DEBUG_SYMS
1330 print_expr (& symp->x->value);
1331 if (relc_symbol_name)
1332 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1333 #endif
1334
1335 if (relc_symbol_name != NULL)
1336 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1337 &zero_address_frag, 0);
1338
1339 if (relc_symbol == NULL)
1340 {
1341 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1342 S_GET_NAME (symp));
1343 resolved = 0;
1344 }
1345 else
1346 {
1347 symbol_table_insert (relc_symbol);
1348
1349 /* S_CLEAR_EXTERNAL (relc_symbol); */
1350 if (symp->bsym->flags & BSF_SRELC)
1351 relc_symbol->bsym->flags |= BSF_SRELC;
1352 else
1353 relc_symbol->bsym->flags |= BSF_RELC;
1354 /* symp->bsym->flags |= BSF_RELC; */
1355 copy_symbol_attributes (symp, relc_symbol);
1356 symp->x->value.X_op = O_symbol;
1357 symp->x->value.X_add_symbol = relc_symbol;
1358 symp->x->value.X_add_number = 0;
1359 resolved = 1;
1360 }
1361
1362 final_val = 0;
1363 final_seg = undefined_section;
1364 goto exit_dont_set_value;
1365 }
1366 #endif
1367 else
1368 {
1369 symbolS *add_symbol, *op_symbol;
1370 offsetT left, right;
1371 segT seg_left, seg_right;
1372 operatorT op;
1373 int move_seg_ok;
1374
1375 symp->flags.resolving = 1;
1376
1377 /* Help out with CSE. */
1378 add_symbol = symp->x->value.X_add_symbol;
1379 op_symbol = symp->x->value.X_op_symbol;
1380 final_val = symp->x->value.X_add_number;
1381 op = symp->x->value.X_op;
1382
1383 switch (op)
1384 {
1385 default:
1386 BAD_CASE (op);
1387 break;
1388
1389 case O_md1:
1390 case O_md2:
1391 case O_md3:
1392 case O_md4:
1393 case O_md5:
1394 case O_md6:
1395 case O_md7:
1396 case O_md8:
1397 case O_md9:
1398 case O_md10:
1399 case O_md11:
1400 case O_md12:
1401 case O_md13:
1402 case O_md14:
1403 case O_md15:
1404 case O_md16:
1405 case O_md17:
1406 case O_md18:
1407 case O_md19:
1408 case O_md20:
1409 case O_md21:
1410 case O_md22:
1411 case O_md23:
1412 case O_md24:
1413 case O_md25:
1414 case O_md26:
1415 case O_md27:
1416 case O_md28:
1417 case O_md29:
1418 case O_md30:
1419 case O_md31:
1420 case O_md32:
1421 #ifdef md_resolve_symbol
1422 resolved = md_resolve_symbol (symp, &final_val, &final_seg);
1423 if (resolved)
1424 break;
1425 #endif
1426 goto exit_dont_set_value;
1427
1428 case O_absent:
1429 final_val = 0;
1430 /* Fall through. */
1431
1432 case O_constant:
1433 /* Symbols whose section has SEC_ELF_OCTETS set,
1434 resolve to octets instead of target bytes. */
1435 if (symp->bsym->section->flags & SEC_OCTETS)
1436 final_val += symp->frag->fr_address;
1437 else
1438 final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1439 if (final_seg == expr_section)
1440 final_seg = absolute_section;
1441 /* Fall through. */
1442
1443 case O_register:
1444 resolved = 1;
1445 break;
1446
1447 case O_symbol:
1448 case O_symbol_rva:
1449 case O_secidx:
1450 left = resolve_symbol_value (add_symbol);
1451 seg_left = S_GET_SEGMENT (add_symbol);
1452 if (finalize_syms)
1453 symp->x->value.X_op_symbol = NULL;
1454
1455 do_symbol:
1456 if (S_IS_WEAKREFR (symp))
1457 {
1458 gas_assert (final_val == 0);
1459 if (S_IS_WEAKREFR (add_symbol))
1460 {
1461 gas_assert (add_symbol->x->value.X_op == O_symbol
1462 && add_symbol->x->value.X_add_number == 0);
1463 add_symbol = add_symbol->x->value.X_add_symbol;
1464 gas_assert (! S_IS_WEAKREFR (add_symbol));
1465 symp->x->value.X_add_symbol = add_symbol;
1466 }
1467 }
1468
1469 if (symp->flags.mri_common)
1470 {
1471 /* This is a symbol inside an MRI common section. The
1472 relocation routines are going to handle it specially.
1473 Don't change the value. */
1474 resolved = symbol_resolved_p (add_symbol);
1475 break;
1476 }
1477
1478 /* Don't leave symbol loops. */
1479 if (finalize_syms
1480 && !add_symbol->flags.local_symbol
1481 && add_symbol->flags.resolving)
1482 break;
1483
1484 if (finalize_syms && final_val == 0
1485 #ifdef OBJ_XCOFF
1486 /* Avoid changing symp's "within" when dealing with
1487 AIX debug symbols. For some storage classes, "within"
1488 have a special meaning.
1489 C_DWARF should behave like on Linux, thus this check
1490 isn't done to be closer. */
1491 && ((symbol_get_bfdsym (symp)->flags & BSF_DEBUGGING) == 0
1492 || (S_GET_STORAGE_CLASS (symp) == C_DWARF))
1493 #endif
1494 )
1495 {
1496 if (add_symbol->flags.local_symbol)
1497 add_symbol = local_symbol_convert (add_symbol);
1498 copy_symbol_attributes (symp, add_symbol);
1499 }
1500
1501 /* If we have equated this symbol to an undefined or common
1502 symbol, keep X_op set to O_symbol, and don't change
1503 X_add_number. This permits the routine which writes out
1504 relocation to detect this case, and convert the
1505 relocation to be against the symbol to which this symbol
1506 is equated. */
1507 if (seg_left == undefined_section
1508 || bfd_is_com_section (seg_left)
1509 #if defined (OBJ_COFF) && defined (TE_PE)
1510 || S_IS_WEAK (add_symbol)
1511 #endif
1512 || (finalize_syms
1513 && ((final_seg == expr_section
1514 && seg_left != expr_section
1515 && seg_left != absolute_section)
1516 || symbol_shadow_p (symp))))
1517 {
1518 if (finalize_syms)
1519 {
1520 symp->x->value.X_op = O_symbol;
1521 symp->x->value.X_add_symbol = add_symbol;
1522 symp->x->value.X_add_number = final_val;
1523 /* Use X_op_symbol as a flag. */
1524 symp->x->value.X_op_symbol = add_symbol;
1525 }
1526 final_seg = seg_left;
1527 final_val += symp->frag->fr_address + left;
1528 resolved = symbol_resolved_p (add_symbol);
1529 symp->flags.resolving = 0;
1530
1531 if (op == O_secidx && seg_left != undefined_section)
1532 {
1533 final_val = 0;
1534 break;
1535 }
1536
1537 goto exit_dont_set_value;
1538 }
1539 else
1540 {
1541 final_val += symp->frag->fr_address + left;
1542 if (final_seg == expr_section || final_seg == undefined_section)
1543 final_seg = seg_left;
1544 }
1545
1546 resolved = symbol_resolved_p (add_symbol);
1547 if (S_IS_WEAKREFR (symp))
1548 {
1549 symp->flags.resolving = 0;
1550 goto exit_dont_set_value;
1551 }
1552 break;
1553
1554 case O_uminus:
1555 case O_bit_not:
1556 case O_logical_not:
1557 left = resolve_symbol_value (add_symbol);
1558 seg_left = S_GET_SEGMENT (add_symbol);
1559
1560 /* By reducing these to the relevant dyadic operator, we get
1561 !S -> S == 0 permitted on anything,
1562 -S -> 0 - S only permitted on absolute
1563 ~S -> S ^ ~0 only permitted on absolute */
1564 if (op != O_logical_not && seg_left != absolute_section
1565 && finalize_syms)
1566 report_op_error (symp, NULL, op, add_symbol);
1567
1568 if (final_seg == expr_section || final_seg == undefined_section)
1569 final_seg = absolute_section;
1570
1571 if (op == O_uminus)
1572 left = -(valueT) left;
1573 else if (op == O_logical_not)
1574 left = !left;
1575 else
1576 left = ~left;
1577
1578 final_val += left + symp->frag->fr_address;
1579
1580 resolved = symbol_resolved_p (add_symbol);
1581 break;
1582
1583 case O_multiply:
1584 case O_divide:
1585 case O_modulus:
1586 case O_left_shift:
1587 case O_right_shift:
1588 case O_bit_inclusive_or:
1589 case O_bit_or_not:
1590 case O_bit_exclusive_or:
1591 case O_bit_and:
1592 case O_add:
1593 case O_subtract:
1594 case O_eq:
1595 case O_ne:
1596 case O_lt:
1597 case O_le:
1598 case O_ge:
1599 case O_gt:
1600 case O_logical_and:
1601 case O_logical_or:
1602 left = resolve_symbol_value (add_symbol);
1603 right = resolve_symbol_value (op_symbol);
1604 seg_left = S_GET_SEGMENT (add_symbol);
1605 seg_right = S_GET_SEGMENT (op_symbol);
1606
1607 /* Simplify addition or subtraction of a constant by folding the
1608 constant into X_add_number. */
1609 if (op == O_add)
1610 {
1611 if (seg_right == absolute_section)
1612 {
1613 final_val += right;
1614 goto do_symbol;
1615 }
1616 else if (seg_left == absolute_section)
1617 {
1618 final_val += left;
1619 add_symbol = op_symbol;
1620 left = right;
1621 seg_left = seg_right;
1622 goto do_symbol;
1623 }
1624 }
1625 else if (op == O_subtract)
1626 {
1627 if (seg_right == absolute_section)
1628 {
1629 final_val -= right;
1630 goto do_symbol;
1631 }
1632 }
1633
1634 move_seg_ok = 1;
1635 /* Equality and non-equality tests are permitted on anything.
1636 Subtraction, and other comparison operators are permitted if
1637 both operands are in the same section. Otherwise, both
1638 operands must be absolute. We already handled the case of
1639 addition or subtraction of a constant above. This will
1640 probably need to be changed for an object file format which
1641 supports arbitrary expressions. */
1642 if (!(seg_left == absolute_section
1643 && seg_right == absolute_section)
1644 && !(op == O_eq || op == O_ne)
1645 && !((op == O_subtract
1646 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1647 && seg_left == seg_right
1648 && (seg_left != undefined_section
1649 || add_symbol == op_symbol)))
1650 {
1651 /* Don't emit messages unless we're finalizing the symbol value,
1652 otherwise we may get the same message multiple times. */
1653 if (finalize_syms)
1654 report_op_error (symp, add_symbol, op, op_symbol);
1655 /* However do not move the symbol into the absolute section
1656 if it cannot currently be resolved - this would confuse
1657 other parts of the assembler into believing that the
1658 expression had been evaluated to zero. */
1659 else
1660 move_seg_ok = 0;
1661 }
1662
1663 if (move_seg_ok
1664 && (final_seg == expr_section || final_seg == undefined_section))
1665 final_seg = absolute_section;
1666
1667 /* Check for division by zero. */
1668 if ((op == O_divide || op == O_modulus) && right == 0)
1669 {
1670 /* If seg_right is not absolute_section, then we've
1671 already issued a warning about using a bad symbol. */
1672 if (seg_right == absolute_section && finalize_syms)
1673 {
1674 const char *file;
1675 unsigned int line;
1676
1677 if (expr_symbol_where (symp, &file, &line))
1678 as_bad_where (file, line, _("division by zero"));
1679 else
1680 as_bad (_("division by zero when setting `%s'"),
1681 S_GET_NAME (symp));
1682 }
1683
1684 right = 1;
1685 }
1686 if ((op == O_left_shift || op == O_right_shift)
1687 && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1688 {
1689 as_warn_value_out_of_range (_("shift count"), right, 0,
1690 sizeof (valueT) * CHAR_BIT - 1,
1691 NULL, 0);
1692 left = right = 0;
1693 }
1694
1695 switch (symp->x->value.X_op)
1696 {
1697 /* See expr() for reasons of the use of valueT casts here. */
1698 case O_multiply: left *= (valueT) right; break;
1699
1700 /* See expr() for reasons of the special casing. */
1701 case O_divide:
1702 if (right == 1)
1703 break;
1704 if (right == -1)
1705 {
1706 left = -(valueT) left;
1707 break;
1708 }
1709 left /= right;
1710 break;
1711
1712 /* Again, see expr() for reasons of the special casing. */
1713 case O_modulus:
1714 if (right == 1 || right == -1)
1715 left = 0;
1716 else
1717 left %= right;
1718 break;
1719
1720 case O_left_shift:
1721 left = (valueT) left << (valueT) right; break;
1722 case O_right_shift:
1723 left = (valueT) left >> (valueT) right; break;
1724 case O_bit_inclusive_or: left |= right; break;
1725 case O_bit_or_not: left |= ~right; break;
1726 case O_bit_exclusive_or: left ^= right; break;
1727 case O_bit_and: left &= right; break;
1728 case O_add: left += (valueT) right; break;
1729 case O_subtract: left -= (valueT) right; break;
1730 case O_eq:
1731 case O_ne:
1732 left = (left == right && seg_left == seg_right
1733 && (seg_left != undefined_section
1734 || add_symbol == op_symbol)
1735 ? ~ (offsetT) 0 : 0);
1736 if (symp->x->value.X_op == O_ne)
1737 left = ~left;
1738 break;
1739 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1740 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1741 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1742 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1743 case O_logical_and: left = left && right; break;
1744 case O_logical_or: left = left || right; break;
1745
1746 case O_illegal:
1747 case O_absent:
1748 case O_constant:
1749 /* See PR 20895 for a reproducer. */
1750 as_bad (_("Invalid operation on symbol"));
1751 goto exit_dont_set_value;
1752
1753 default:
1754 abort ();
1755 }
1756
1757 final_val += symp->frag->fr_address + left;
1758 if (final_seg == expr_section || final_seg == undefined_section)
1759 {
1760 if (seg_left == undefined_section
1761 || seg_right == undefined_section)
1762 final_seg = undefined_section;
1763 else if (seg_left == absolute_section)
1764 final_seg = seg_right;
1765 else
1766 final_seg = seg_left;
1767 }
1768 resolved = (symbol_resolved_p (add_symbol)
1769 && symbol_resolved_p (op_symbol));
1770 break;
1771
1772 case O_big:
1773 case O_illegal:
1774 /* Give an error (below) if not in expr_section. We don't
1775 want to worry about expr_section symbols, because they
1776 are fictional (they are created as part of expression
1777 resolution), and any problems may not actually mean
1778 anything. */
1779 break;
1780 }
1781
1782 symp->flags.resolving = 0;
1783 }
1784
1785 if (finalize_syms)
1786 S_SET_VALUE (symp, final_val);
1787
1788 exit_dont_set_value:
1789 /* Always set the segment, even if not finalizing the value.
1790 The segment is used to determine whether a symbol is defined. */
1791 S_SET_SEGMENT (symp, final_seg);
1792
1793 /* Don't worry if we can't resolve an expr_section symbol. */
1794 if (finalize_syms)
1795 {
1796 if (resolved)
1797 symp->flags.resolved = 1;
1798 else if (S_GET_SEGMENT (symp) != expr_section)
1799 {
1800 as_bad (_("can't resolve value for symbol `%s'"),
1801 S_GET_NAME (symp));
1802 symp->flags.resolved = 1;
1803 }
1804 }
1805
1806 return final_val;
1807 }
1808
1809 /* A static function passed to hash_traverse. */
1810
1811 static int
1812 resolve_local_symbol (void **slot, void *arg ATTRIBUTE_UNUSED)
1813 {
1814 symbol_entry_t *entry = *((symbol_entry_t **) slot);
1815 if (entry->sy.flags.local_symbol)
1816 resolve_symbol_value (&entry->sy);
1817
1818 return 1;
1819 }
1820
1821 /* Resolve all local symbols. */
1822
1823 void
1824 resolve_local_symbol_values (void)
1825 {
1826 htab_traverse_noresize (sy_hash, resolve_local_symbol, NULL);
1827 }
1828
1829 /* Obtain the current value of a symbol without changing any
1830 sub-expressions used. */
1831
1832 int
1833 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1834 {
1835 symbolS *symbolP = *symbolPP;
1836
1837 if (symbolP->flags.local_symbol)
1838 {
1839 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1840
1841 *valueP = locsym->value;
1842 *segP = locsym->section;
1843 *fragPP = locsym->frag;
1844 }
1845 else
1846 {
1847 expressionS exp = symbolP->x->value;
1848
1849 if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1850 {
1851 int resolved;
1852
1853 if (symbolP->flags.resolving)
1854 return 0;
1855 symbolP->flags.resolving = 1;
1856 resolved = resolve_expression (&exp);
1857 symbolP->flags.resolving = 0;
1858 if (!resolved)
1859 return 0;
1860
1861 switch (exp.X_op)
1862 {
1863 case O_constant:
1864 case O_register:
1865 if (!symbol_equated_p (symbolP))
1866 break;
1867 /* Fallthru. */
1868 case O_symbol:
1869 case O_symbol_rva:
1870 symbolP = exp.X_add_symbol;
1871 break;
1872 default:
1873 return 0;
1874 }
1875 }
1876
1877 *symbolPP = symbolP;
1878
1879 /* A bogus input file can result in resolve_expression()
1880 generating a local symbol, so we have to check again. */
1881 if (symbolP->flags.local_symbol)
1882 {
1883 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1884
1885 *valueP = locsym->value;
1886 *segP = locsym->section;
1887 *fragPP = locsym->frag;
1888 }
1889 else
1890 {
1891 *valueP = exp.X_add_number;
1892 *segP = symbolP->bsym->section;
1893 *fragPP = symbolP->frag;
1894 }
1895
1896 if (*segP == expr_section)
1897 switch (exp.X_op)
1898 {
1899 case O_constant: *segP = absolute_section; break;
1900 case O_register: *segP = reg_section; break;
1901 default: break;
1902 }
1903 }
1904
1905 return 1;
1906 }
1907
1908 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1909 They are *really* local. That is, they go out of scope whenever we see a
1910 label that isn't local. Also, like fb labels, there can be multiple
1911 instances of a dollar label. Therefor, we name encode each instance with
1912 the instance number, keep a list of defined symbols separate from the real
1913 symbol table, and we treat these buggers as a sparse array. */
1914
1915 typedef unsigned int dollar_ent;
1916 static dollar_ent *dollar_labels;
1917 static dollar_ent *dollar_label_instances;
1918 static char *dollar_label_defines;
1919 static size_t dollar_label_count;
1920 static size_t dollar_label_max;
1921
1922 int
1923 dollar_label_defined (unsigned int label)
1924 {
1925 dollar_ent *i;
1926
1927 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1928
1929 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1930 if (*i == label)
1931 return dollar_label_defines[i - dollar_labels];
1932
1933 /* If we get here, label isn't defined. */
1934 return 0;
1935 }
1936
1937 static unsigned int
1938 dollar_label_instance (unsigned int label)
1939 {
1940 dollar_ent *i;
1941
1942 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1943
1944 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1945 if (*i == label)
1946 return (dollar_label_instances[i - dollar_labels]);
1947
1948 /* If we get here, we haven't seen the label before.
1949 Therefore its instance count is zero. */
1950 return 0;
1951 }
1952
1953 void
1954 dollar_label_clear (void)
1955 {
1956 if (dollar_label_count)
1957 memset (dollar_label_defines, '\0', dollar_label_count);
1958 }
1959
1960 #define DOLLAR_LABEL_BUMP_BY 10
1961
1962 void
1963 define_dollar_label (unsigned int label)
1964 {
1965 dollar_ent *i;
1966
1967 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1968 if (*i == label)
1969 {
1970 ++dollar_label_instances[i - dollar_labels];
1971 dollar_label_defines[i - dollar_labels] = 1;
1972 return;
1973 }
1974
1975 /* If we get to here, we don't have label listed yet. */
1976
1977 if (dollar_labels == NULL)
1978 {
1979 dollar_labels = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1980 dollar_label_instances = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1981 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1982 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1983 dollar_label_count = 0;
1984 }
1985 else if (dollar_label_count == dollar_label_max)
1986 {
1987 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1988 dollar_labels = XRESIZEVEC (dollar_ent, dollar_labels,
1989 dollar_label_max);
1990 dollar_label_instances = XRESIZEVEC (dollar_ent,
1991 dollar_label_instances,
1992 dollar_label_max);
1993 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
1994 dollar_label_max);
1995 } /* if we needed to grow */
1996
1997 dollar_labels[dollar_label_count] = label;
1998 dollar_label_instances[dollar_label_count] = 1;
1999 dollar_label_defines[dollar_label_count] = 1;
2000 ++dollar_label_count;
2001 }
2002
2003 /* Caller must copy returned name: we re-use the area for the next name.
2004
2005 The mth occurrence of label n: is turned into the symbol "Ln^Am"
2006 where n is the label number and m is the instance number. "L" makes
2007 it a label discarded unless debugging and "^A"('\1') ensures no
2008 ordinary symbol SHOULD get the same name as a local label
2009 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
2010
2011 fb labels get the same treatment, except that ^B is used in place
2012 of ^A.
2013
2014 AUGEND is 0 for current instance, 1 for new instance. */
2015
2016 char *
2017 dollar_label_name (unsigned int n, unsigned int augend)
2018 {
2019 /* Returned to caller, then copied. Used for created names ("4f"). */
2020 static char symbol_name_build[24];
2021 char *p = symbol_name_build;
2022
2023 #ifdef LOCAL_LABEL_PREFIX
2024 *p++ = LOCAL_LABEL_PREFIX;
2025 #endif
2026 sprintf (p, "L%u%c%u",
2027 n, DOLLAR_LABEL_CHAR, dollar_label_instance (n) + augend);
2028 return symbol_name_build;
2029 }
2030
2031 /* Somebody else's idea of local labels. They are made by "n:" where n
2032 is any decimal digit. Refer to them with
2033 "nb" for previous (backward) n:
2034 or "nf" for next (forward) n:.
2035
2036 We do a little better and let n be any number, not just a single digit, but
2037 since the other guy's assembler only does ten, we treat the first ten
2038 specially.
2039
2040 Like someone else's assembler, we have one set of local label counters for
2041 entire assembly, not one set per (sub)segment like in most assemblers. This
2042 implies that one can refer to a label in another segment, and indeed some
2043 crufty compilers have done just that.
2044
2045 Since there could be a LOT of these things, treat them as a sparse
2046 array. */
2047
2048 #define FB_LABEL_SPECIAL (10)
2049
2050 typedef unsigned int fb_ent;
2051 static fb_ent fb_low_counter[FB_LABEL_SPECIAL];
2052 static fb_ent *fb_labels;
2053 static fb_ent *fb_label_instances;
2054 static size_t fb_label_count;
2055 static size_t fb_label_max;
2056
2057 /* This must be more than FB_LABEL_SPECIAL. */
2058 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
2059
2060 static void
2061 fb_label_init (void)
2062 {
2063 memset (fb_low_counter, 0, sizeof (fb_low_counter));
2064 }
2065
2066 /* Add one to the instance number of this fb label. */
2067
2068 void
2069 fb_label_instance_inc (unsigned int label)
2070 {
2071 fb_ent *i;
2072
2073 if (label < FB_LABEL_SPECIAL)
2074 {
2075 ++fb_low_counter[label];
2076 return;
2077 }
2078
2079 if (fb_labels != NULL)
2080 {
2081 for (i = fb_labels + FB_LABEL_SPECIAL;
2082 i < fb_labels + fb_label_count; ++i)
2083 {
2084 if (*i == label)
2085 {
2086 ++fb_label_instances[i - fb_labels];
2087 return;
2088 } /* if we find it */
2089 } /* for each existing label */
2090 }
2091
2092 /* If we get to here, we don't have label listed yet. */
2093
2094 if (fb_labels == NULL)
2095 {
2096 fb_labels = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2097 fb_label_instances = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2098 fb_label_max = FB_LABEL_BUMP_BY;
2099 fb_label_count = FB_LABEL_SPECIAL;
2100
2101 }
2102 else if (fb_label_count == fb_label_max)
2103 {
2104 fb_label_max += FB_LABEL_BUMP_BY;
2105 fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
2106 fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
2107 fb_label_max);
2108 } /* if we needed to grow */
2109
2110 fb_labels[fb_label_count] = label;
2111 fb_label_instances[fb_label_count] = 1;
2112 ++fb_label_count;
2113 }
2114
2115 static unsigned int
2116 fb_label_instance (unsigned int label)
2117 {
2118 fb_ent *i;
2119
2120 if (label < FB_LABEL_SPECIAL)
2121 return (fb_low_counter[label]);
2122
2123 if (fb_labels != NULL)
2124 {
2125 for (i = fb_labels + FB_LABEL_SPECIAL;
2126 i < fb_labels + fb_label_count; ++i)
2127 {
2128 if (*i == label)
2129 return (fb_label_instances[i - fb_labels]);
2130 }
2131 }
2132
2133 /* We didn't find the label, so this must be a reference to the
2134 first instance. */
2135 return 0;
2136 }
2137
2138 /* Caller must copy returned name: we re-use the area for the next name.
2139
2140 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2141 where n is the label number and m is the instance number. "L" makes
2142 it a label discarded unless debugging and "^B"('\2') ensures no
2143 ordinary symbol SHOULD get the same name as a local label
2144 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2145
2146 dollar labels get the same treatment, except that ^A is used in
2147 place of ^B.
2148
2149 AUGEND is 0 for nb, 1 for n:, nf. */
2150
2151 char *
2152 fb_label_name (unsigned int n, unsigned int augend)
2153 {
2154 /* Returned to caller, then copied. Used for created names ("4f"). */
2155 static char symbol_name_build[24];
2156 char *p = symbol_name_build;
2157
2158 #ifdef TC_MMIX
2159 know (augend <= 2 /* See mmix_fb_label. */);
2160 #else
2161 know (augend <= 1);
2162 #endif
2163
2164 #ifdef LOCAL_LABEL_PREFIX
2165 *p++ = LOCAL_LABEL_PREFIX;
2166 #endif
2167 sprintf (p, "L%u%c%u",
2168 n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2169 return symbol_name_build;
2170 }
2171
2172 /* Decode name that may have been generated by foo_label_name() above.
2173 If the name wasn't generated by foo_label_name(), then return it
2174 unaltered. This is used for error messages. */
2175
2176 const char *
2177 decode_local_label_name (const char *s)
2178 {
2179 const char *p;
2180 char *symbol_decode;
2181 unsigned int label_number;
2182 unsigned int instance_number;
2183 const char *type;
2184 const char *message_format;
2185 unsigned int lindex = 0;
2186
2187 #ifdef LOCAL_LABEL_PREFIX
2188 if (s[lindex] == LOCAL_LABEL_PREFIX)
2189 ++lindex;
2190 #endif
2191
2192 if (s[lindex] != 'L')
2193 return s;
2194
2195 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
2196 label_number = (10 * label_number) + *p - '0';
2197
2198 if (*p == DOLLAR_LABEL_CHAR)
2199 type = "dollar";
2200 else if (*p == LOCAL_LABEL_CHAR)
2201 type = "fb";
2202 else
2203 return s;
2204
2205 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
2206 instance_number = (10 * instance_number) + *p - '0';
2207
2208 message_format = _("\"%u\" (instance number %u of a %s label)");
2209 symbol_decode = notes_alloc (strlen (message_format) + 30);
2210 sprintf (symbol_decode, message_format, label_number, instance_number, type);
2211
2212 return symbol_decode;
2213 }
2214
2215 /* Get the value of a symbol. */
2216
2217 valueT
2218 S_GET_VALUE_WHERE (symbolS *s, const char * file, unsigned int line)
2219 {
2220 if (s->flags.local_symbol)
2221 return resolve_symbol_value (s);
2222
2223 if (!s->flags.resolved)
2224 {
2225 valueT val = resolve_symbol_value (s);
2226 if (!finalize_syms)
2227 return val;
2228 }
2229 if (S_IS_WEAKREFR (s))
2230 return S_GET_VALUE (s->x->value.X_add_symbol);
2231
2232 if (s->x->value.X_op != O_constant)
2233 {
2234 if (! s->flags.resolved
2235 || s->x->value.X_op != O_symbol
2236 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
2237 {
2238 if (strcmp (S_GET_NAME (s), FAKE_LABEL_NAME) == 0)
2239 as_bad_where (file, line, _("expression is too complex to be resolved or converted into relocations"));
2240 else if (file != NULL)
2241 as_bad_where (file, line, _("attempt to get value of unresolved symbol `%s'"),
2242 S_GET_NAME (s));
2243 else
2244 as_bad (_("attempt to get value of unresolved symbol `%s'"),
2245 S_GET_NAME (s));
2246 }
2247 }
2248 return s->x->value.X_add_number;
2249 }
2250
2251 valueT
2252 S_GET_VALUE (symbolS *s)
2253 {
2254 return S_GET_VALUE_WHERE (s, NULL, 0);
2255 }
2256
2257 /* Set the value of a symbol. */
2258
2259 void
2260 S_SET_VALUE (symbolS *s, valueT val)
2261 {
2262 if (s->flags.local_symbol)
2263 {
2264 ((struct local_symbol *) s)->value = val;
2265 return;
2266 }
2267
2268 s->x->value.X_op = O_constant;
2269 s->x->value.X_add_number = (offsetT) val;
2270 s->x->value.X_unsigned = 0;
2271 S_CLEAR_WEAKREFR (s);
2272 }
2273
2274 void
2275 copy_symbol_attributes (symbolS *dest, symbolS *src)
2276 {
2277 if (dest->flags.local_symbol)
2278 dest = local_symbol_convert (dest);
2279 if (src->flags.local_symbol)
2280 src = local_symbol_convert (src);
2281
2282 /* In an expression, transfer the settings of these flags.
2283 The user can override later, of course. */
2284 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2285 | BSF_GNU_INDIRECT_FUNCTION)
2286 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2287
2288 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2289 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2290 #endif
2291
2292 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2293 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2294 #endif
2295 }
2296
2297 int
2298 S_IS_FUNCTION (const symbolS *s)
2299 {
2300 flagword flags;
2301
2302 if (s->flags.local_symbol)
2303 return 0;
2304
2305 flags = s->bsym->flags;
2306
2307 return (flags & BSF_FUNCTION) != 0;
2308 }
2309
2310 int
2311 S_IS_EXTERNAL (const symbolS *s)
2312 {
2313 flagword flags;
2314
2315 if (s->flags.local_symbol)
2316 return 0;
2317
2318 flags = s->bsym->flags;
2319
2320 /* Sanity check. */
2321 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2322 abort ();
2323
2324 return (flags & BSF_GLOBAL) != 0;
2325 }
2326
2327 int
2328 S_IS_WEAK (const symbolS *s)
2329 {
2330 if (s->flags.local_symbol)
2331 return 0;
2332 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2333 could probably handle a WEAKREFR as always weak though. E.g., if
2334 the referenced symbol has lost its weak status, there's no reason
2335 to keep handling the weakrefr as if it was weak. */
2336 if (S_IS_WEAKREFR (s))
2337 return S_IS_WEAK (s->x->value.X_add_symbol);
2338 return (s->bsym->flags & BSF_WEAK) != 0;
2339 }
2340
2341 int
2342 S_IS_WEAKREFR (const symbolS *s)
2343 {
2344 if (s->flags.local_symbol)
2345 return 0;
2346 return s->flags.weakrefr != 0;
2347 }
2348
2349 int
2350 S_IS_WEAKREFD (const symbolS *s)
2351 {
2352 if (s->flags.local_symbol)
2353 return 0;
2354 return s->flags.weakrefd != 0;
2355 }
2356
2357 int
2358 S_IS_COMMON (const symbolS *s)
2359 {
2360 if (s->flags.local_symbol)
2361 return 0;
2362 return bfd_is_com_section (s->bsym->section);
2363 }
2364
2365 int
2366 S_IS_DEFINED (const symbolS *s)
2367 {
2368 if (s->flags.local_symbol)
2369 return ((const struct local_symbol *) s)->section != undefined_section;
2370 return s->bsym->section != undefined_section;
2371 }
2372
2373
2374 #ifndef EXTERN_FORCE_RELOC
2375 #define EXTERN_FORCE_RELOC IS_ELF
2376 #endif
2377
2378 /* Return true for symbols that should not be reduced to section
2379 symbols or eliminated from expressions, because they may be
2380 overridden by the linker. */
2381 int
2382 S_FORCE_RELOC (const symbolS *s, int strict)
2383 {
2384 segT sec;
2385 if (s->flags.local_symbol)
2386 sec = ((const struct local_symbol *) s)->section;
2387 else
2388 {
2389 if ((strict
2390 && ((s->bsym->flags & BSF_WEAK) != 0
2391 || (EXTERN_FORCE_RELOC
2392 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2393 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2394 return true;
2395 sec = s->bsym->section;
2396 }
2397 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2398 }
2399
2400 int
2401 S_IS_DEBUG (const symbolS *s)
2402 {
2403 if (s->flags.local_symbol)
2404 return 0;
2405 if (s->bsym->flags & BSF_DEBUGGING)
2406 return 1;
2407 return 0;
2408 }
2409
2410 int
2411 S_IS_LOCAL (const symbolS *s)
2412 {
2413 flagword flags;
2414 const char *name;
2415
2416 if (s->flags.local_symbol)
2417 return 1;
2418
2419 if (S_IS_EXTERNAL (s))
2420 return 0;
2421
2422 if (bfd_asymbol_section (s->bsym) == reg_section)
2423 return 1;
2424
2425 flags = s->bsym->flags;
2426
2427 if (flag_strip_local_absolute > 0
2428 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2429 the source file even when the object file is stripped. */
2430 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2431 && bfd_asymbol_section (s->bsym) == absolute_section)
2432 return 1;
2433
2434 name = S_GET_NAME (s);
2435 return (name != NULL
2436 && ! S_IS_DEBUG (s)
2437 && (strchr (name, DOLLAR_LABEL_CHAR)
2438 || strchr (name, LOCAL_LABEL_CHAR)
2439 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2440 || strchr (name, FAKE_LABEL_CHAR)
2441 #endif
2442 || TC_LABEL_IS_LOCAL (name)
2443 || (! flag_keep_locals
2444 && (bfd_is_local_label (stdoutput, s->bsym)
2445 || (flag_mri
2446 && name[0] == '?'
2447 && name[1] == '?')))));
2448 }
2449
2450 int
2451 S_IS_STABD (const symbolS *s)
2452 {
2453 return S_GET_NAME (s) == 0;
2454 }
2455
2456 int
2457 S_CAN_BE_REDEFINED (const symbolS *s)
2458 {
2459 if (s->flags.local_symbol)
2460 return (((const struct local_symbol *) s)->frag
2461 == &predefined_address_frag);
2462 /* Permit register names to be redefined. */
2463 return s->x->value.X_op == O_register;
2464 }
2465
2466 int
2467 S_IS_VOLATILE (const symbolS *s)
2468 {
2469 if (s->flags.local_symbol)
2470 return 0;
2471 return s->flags.volatil;
2472 }
2473
2474 int
2475 S_IS_FORWARD_REF (const symbolS *s)
2476 {
2477 if (s->flags.local_symbol)
2478 return 0;
2479 return s->flags.forward_ref;
2480 }
2481
2482 const char *
2483 S_GET_NAME (const symbolS *s)
2484 {
2485 return s->name;
2486 }
2487
2488 segT
2489 S_GET_SEGMENT (const symbolS *s)
2490 {
2491 if (s->flags.local_symbol)
2492 return ((const struct local_symbol *) s)->section;
2493 return s->bsym->section;
2494 }
2495
2496 void
2497 S_SET_SEGMENT (symbolS *s, segT seg)
2498 {
2499 if (s->flags.local_symbol)
2500 {
2501 ((struct local_symbol *) s)->section = seg;
2502 return;
2503 }
2504
2505 /* Don't reassign section symbols. The direct reason is to prevent seg
2506 faults assigning back to const global symbols such as *ABS*, but it
2507 shouldn't happen anyway. */
2508 if (s->bsym->flags & BSF_SECTION_SYM)
2509 {
2510 if (s->bsym->section != seg)
2511 abort ();
2512 }
2513 else
2514 {
2515 if (multibyte_handling == multibyte_warn_syms
2516 && ! s->flags.local_symbol
2517 && seg != undefined_section
2518 && ! s->flags.multibyte_warned
2519 && scan_for_multibyte_characters ((const unsigned char *) s->name,
2520 (const unsigned char *) s->name + strlen (s->name),
2521 false))
2522 {
2523 as_warn (_("symbol '%s' contains multibyte characters"), s->name);
2524 s->flags.multibyte_warned = 1;
2525 }
2526
2527 s->bsym->section = seg;
2528 }
2529 }
2530
2531 void
2532 S_SET_EXTERNAL (symbolS *s)
2533 {
2534 if (s->flags.local_symbol)
2535 s = local_symbol_convert (s);
2536 if ((s->bsym->flags & BSF_WEAK) != 0)
2537 {
2538 /* Let .weak override .global. */
2539 return;
2540 }
2541 if (s->bsym->flags & BSF_SECTION_SYM)
2542 {
2543 /* Do not reassign section symbols. */
2544 as_warn (_("can't make section symbol global"));
2545 return;
2546 }
2547 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2548 if (S_GET_SEGMENT (s) == reg_section)
2549 {
2550 as_bad (_("can't make register symbol global"));
2551 return;
2552 }
2553 #endif
2554 s->bsym->flags |= BSF_GLOBAL;
2555 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2556
2557 #ifdef TE_PE
2558 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2559 an_external_name = S_GET_NAME (s);
2560 #endif
2561 }
2562
2563 void
2564 S_CLEAR_EXTERNAL (symbolS *s)
2565 {
2566 if (s->flags.local_symbol)
2567 return;
2568 if ((s->bsym->flags & BSF_WEAK) != 0)
2569 {
2570 /* Let .weak override. */
2571 return;
2572 }
2573 s->bsym->flags |= BSF_LOCAL;
2574 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2575 }
2576
2577 void
2578 S_SET_WEAK (symbolS *s)
2579 {
2580 if (s->flags.local_symbol)
2581 s = local_symbol_convert (s);
2582 #ifdef obj_set_weak_hook
2583 obj_set_weak_hook (s);
2584 #endif
2585 s->bsym->flags |= BSF_WEAK;
2586 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2587 }
2588
2589 void
2590 S_SET_WEAKREFR (symbolS *s)
2591 {
2592 if (s->flags.local_symbol)
2593 s = local_symbol_convert (s);
2594 s->flags.weakrefr = 1;
2595 /* If the alias was already used, make sure we mark the target as
2596 used as well, otherwise it might be dropped from the symbol
2597 table. This may have unintended side effects if the alias is
2598 later redirected to another symbol, such as keeping the unused
2599 previous target in the symbol table. Since it will be weak, it's
2600 not a big deal. */
2601 if (s->flags.used)
2602 symbol_mark_used (s->x->value.X_add_symbol);
2603 }
2604
2605 void
2606 S_CLEAR_WEAKREFR (symbolS *s)
2607 {
2608 if (s->flags.local_symbol)
2609 return;
2610 s->flags.weakrefr = 0;
2611 }
2612
2613 void
2614 S_SET_WEAKREFD (symbolS *s)
2615 {
2616 if (s->flags.local_symbol)
2617 s = local_symbol_convert (s);
2618 s->flags.weakrefd = 1;
2619 S_SET_WEAK (s);
2620 }
2621
2622 void
2623 S_CLEAR_WEAKREFD (symbolS *s)
2624 {
2625 if (s->flags.local_symbol)
2626 return;
2627 if (s->flags.weakrefd)
2628 {
2629 s->flags.weakrefd = 0;
2630 /* If a weakref target symbol is weak, then it was never
2631 referenced directly before, not even in a .global directive,
2632 so decay it to local. If it remains undefined, it will be
2633 later turned into a global, like any other undefined
2634 symbol. */
2635 if (s->bsym->flags & BSF_WEAK)
2636 {
2637 #ifdef obj_clear_weak_hook
2638 obj_clear_weak_hook (s);
2639 #endif
2640 s->bsym->flags &= ~BSF_WEAK;
2641 s->bsym->flags |= BSF_LOCAL;
2642 }
2643 }
2644 }
2645
2646 void
2647 S_SET_THREAD_LOCAL (symbolS *s)
2648 {
2649 if (s->flags.local_symbol)
2650 s = local_symbol_convert (s);
2651 if (bfd_is_com_section (s->bsym->section)
2652 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2653 return;
2654 s->bsym->flags |= BSF_THREAD_LOCAL;
2655 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2656 as_bad (_("Accessing function `%s' as thread-local object"),
2657 S_GET_NAME (s));
2658 else if (! bfd_is_und_section (s->bsym->section)
2659 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2660 as_bad (_("Accessing `%s' as thread-local object"),
2661 S_GET_NAME (s));
2662 }
2663
2664 void
2665 S_SET_NAME (symbolS *s, const char *name)
2666 {
2667 s->name = name;
2668 if (s->flags.local_symbol)
2669 return;
2670 s->bsym->name = name;
2671 }
2672
2673 void
2674 S_SET_VOLATILE (symbolS *s)
2675 {
2676 if (s->flags.local_symbol)
2677 s = local_symbol_convert (s);
2678 s->flags.volatil = 1;
2679 }
2680
2681 void
2682 S_CLEAR_VOLATILE (symbolS *s)
2683 {
2684 if (!s->flags.local_symbol)
2685 s->flags.volatil = 0;
2686 }
2687
2688 void
2689 S_SET_FORWARD_REF (symbolS *s)
2690 {
2691 if (s->flags.local_symbol)
2692 s = local_symbol_convert (s);
2693 s->flags.forward_ref = 1;
2694 }
2695
2696 /* Return the previous symbol in a chain. */
2697
2698 symbolS *
2699 symbol_previous (const symbolS *s)
2700 {
2701 if (s->flags.local_symbol)
2702 abort ();
2703 return s->x->previous;
2704 }
2705
2706 /* Return the next symbol in a chain. */
2707
2708 symbolS *
2709 symbol_next (const symbolS *s)
2710 {
2711 if (s->flags.local_symbol)
2712 abort ();
2713 return s->x->next;
2714 }
2715
2716 /* Return a pointer to the value of a symbol as an expression. */
2717
2718 expressionS *
2719 symbol_get_value_expression (symbolS *s)
2720 {
2721 if (s->flags.local_symbol)
2722 s = local_symbol_convert (s);
2723 return &s->x->value;
2724 }
2725
2726 /* Set the value of a symbol to an expression. */
2727
2728 void
2729 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2730 {
2731 if (s->flags.local_symbol)
2732 s = local_symbol_convert (s);
2733 s->x->value = *exp;
2734 S_CLEAR_WEAKREFR (s);
2735 }
2736
2737 /* Return whether 2 symbols are the same. */
2738
2739 int
2740 symbol_same_p (const symbolS *s1, const symbolS *s2)
2741 {
2742 return s1 == s2;
2743 }
2744
2745 /* Return a pointer to the X_add_number component of a symbol. */
2746
2747 offsetT *
2748 symbol_X_add_number (const symbolS *s)
2749 {
2750 if (s->flags.local_symbol)
2751 return (offsetT *) &((struct local_symbol *) s)->value;
2752
2753 return &s->x->value.X_add_number;
2754 }
2755
2756 /* Set the value of SYM to the current position in the current segment. */
2757
2758 void
2759 symbol_set_value_now (symbolS *sym)
2760 {
2761 S_SET_SEGMENT (sym, now_seg);
2762 S_SET_VALUE (sym, frag_now_fix ());
2763 symbol_set_frag (sym, frag_now);
2764 }
2765
2766 /* Set the frag of a symbol. */
2767
2768 void
2769 symbol_set_frag (symbolS *s, fragS *f)
2770 {
2771 if (s->flags.local_symbol)
2772 {
2773 ((struct local_symbol *) s)->frag = f;
2774 return;
2775 }
2776 s->frag = f;
2777 S_CLEAR_WEAKREFR (s);
2778 }
2779
2780 /* Return the frag of a symbol. */
2781
2782 fragS *
2783 symbol_get_frag (const symbolS *s)
2784 {
2785 if (s->flags.local_symbol)
2786 return ((struct local_symbol *) s)->frag;
2787 return s->frag;
2788 }
2789
2790 /* Return the frag of a symbol and the symbol's offset into that frag. */
2791
2792 fragS *symbol_get_frag_and_value (const symbolS *s, addressT *value)
2793 {
2794 if (s->flags.local_symbol)
2795 {
2796 const struct local_symbol *locsym = (const struct local_symbol *) s;
2797
2798 *value = locsym->value;
2799 return locsym->frag;
2800 }
2801
2802 gas_assert (s->x->value.X_op == O_constant);
2803 *value = s->x->value.X_add_number;
2804 return s->frag;
2805 }
2806
2807 /* Mark a symbol as having been used. */
2808
2809 void
2810 symbol_mark_used (symbolS *s)
2811 {
2812 if (s->flags.local_symbol)
2813 return;
2814 s->flags.used = 1;
2815 if (S_IS_WEAKREFR (s))
2816 symbol_mark_used (s->x->value.X_add_symbol);
2817 }
2818
2819 /* Clear the mark of whether a symbol has been used. */
2820
2821 void
2822 symbol_clear_used (symbolS *s)
2823 {
2824 if (s->flags.local_symbol)
2825 s = local_symbol_convert (s);
2826 s->flags.used = 0;
2827 }
2828
2829 /* Return whether a symbol has been used. */
2830
2831 int
2832 symbol_used_p (const symbolS *s)
2833 {
2834 if (s->flags.local_symbol)
2835 return 1;
2836 return s->flags.used;
2837 }
2838
2839 /* Mark a symbol as having been used in a reloc. */
2840
2841 void
2842 symbol_mark_used_in_reloc (symbolS *s)
2843 {
2844 if (s->flags.local_symbol)
2845 s = local_symbol_convert (s);
2846 s->flags.used_in_reloc = 1;
2847 }
2848
2849 /* Clear the mark of whether a symbol has been used in a reloc. */
2850
2851 void
2852 symbol_clear_used_in_reloc (symbolS *s)
2853 {
2854 if (s->flags.local_symbol)
2855 return;
2856 s->flags.used_in_reloc = 0;
2857 }
2858
2859 /* Return whether a symbol has been used in a reloc. */
2860
2861 int
2862 symbol_used_in_reloc_p (const symbolS *s)
2863 {
2864 if (s->flags.local_symbol)
2865 return 0;
2866 return s->flags.used_in_reloc;
2867 }
2868
2869 /* Mark a symbol as an MRI common symbol. */
2870
2871 void
2872 symbol_mark_mri_common (symbolS *s)
2873 {
2874 if (s->flags.local_symbol)
2875 s = local_symbol_convert (s);
2876 s->flags.mri_common = 1;
2877 }
2878
2879 /* Clear the mark of whether a symbol is an MRI common symbol. */
2880
2881 void
2882 symbol_clear_mri_common (symbolS *s)
2883 {
2884 if (s->flags.local_symbol)
2885 return;
2886 s->flags.mri_common = 0;
2887 }
2888
2889 /* Return whether a symbol is an MRI common symbol. */
2890
2891 int
2892 symbol_mri_common_p (const symbolS *s)
2893 {
2894 if (s->flags.local_symbol)
2895 return 0;
2896 return s->flags.mri_common;
2897 }
2898
2899 /* Mark a symbol as having been written. */
2900
2901 void
2902 symbol_mark_written (symbolS *s)
2903 {
2904 if (s->flags.local_symbol)
2905 return;
2906 s->flags.written = 1;
2907 }
2908
2909 /* Clear the mark of whether a symbol has been written. */
2910
2911 void
2912 symbol_clear_written (symbolS *s)
2913 {
2914 if (s->flags.local_symbol)
2915 return;
2916 s->flags.written = 0;
2917 }
2918
2919 /* Return whether a symbol has been written. */
2920
2921 int
2922 symbol_written_p (const symbolS *s)
2923 {
2924 if (s->flags.local_symbol)
2925 return 0;
2926 return s->flags.written;
2927 }
2928
2929 /* Mark a symbol as to be removed. */
2930
2931 void
2932 symbol_mark_removed (symbolS *s)
2933 {
2934 if (s->flags.local_symbol)
2935 return;
2936 s->flags.removed = 1;
2937 }
2938
2939 /* Return whether a symbol has been marked to be removed. */
2940
2941 int
2942 symbol_removed_p (const symbolS *s)
2943 {
2944 if (s->flags.local_symbol)
2945 return 0;
2946 return s->flags.removed;
2947 }
2948
2949 /* Mark a symbol as having been resolved. */
2950
2951 void
2952 symbol_mark_resolved (symbolS *s)
2953 {
2954 s->flags.resolved = 1;
2955 }
2956
2957 /* Return whether a symbol has been resolved. */
2958
2959 int
2960 symbol_resolved_p (const symbolS *s)
2961 {
2962 return s->flags.resolved;
2963 }
2964
2965 /* Mark a symbol as being resolved. */
2966
2967 void
2968 symbol_mark_resolving (symbolS *s)
2969 {
2970 s->flags.resolving = 1;
2971 }
2972
2973 void
2974 symbol_clear_resolving (symbolS *s)
2975 {
2976 s->flags.resolving = 0;
2977 }
2978
2979 /* Return whether a symbol is being resolved. */
2980
2981 int
2982 symbol_resolving_p (const symbolS *s)
2983 {
2984 return s->flags.resolving;
2985 }
2986
2987 /* Return whether a symbol is a section symbol. */
2988
2989 int
2990 symbol_section_p (const symbolS *s)
2991 {
2992 if (s->flags.local_symbol)
2993 return 0;
2994 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2995 }
2996
2997 /* Return whether a symbol is equated to another symbol. */
2998
2999 int
3000 symbol_equated_p (const symbolS *s)
3001 {
3002 if (s->flags.local_symbol)
3003 return 0;
3004 return s->x->value.X_op == O_symbol;
3005 }
3006
3007 /* Return whether a symbol is equated to another symbol, and should be
3008 treated specially when writing out relocs. */
3009
3010 int
3011 symbol_equated_reloc_p (const symbolS *s)
3012 {
3013 if (s->flags.local_symbol)
3014 return 0;
3015 /* X_op_symbol, normally not used for O_symbol, is set by
3016 resolve_symbol_value to flag expression syms that have been
3017 equated. */
3018 return (s->x->value.X_op == O_symbol
3019 #if defined (OBJ_COFF) && defined (TE_PE)
3020 && ! S_IS_WEAK (s)
3021 #endif
3022 && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
3023 || ! S_IS_DEFINED (s)
3024 || S_IS_COMMON (s)));
3025 }
3026
3027 /* Return whether a symbol has a constant value. */
3028
3029 int
3030 symbol_constant_p (const symbolS *s)
3031 {
3032 if (s->flags.local_symbol)
3033 return 1;
3034 return s->x->value.X_op == O_constant;
3035 }
3036
3037 /* Return whether a symbol was cloned and thus removed from the global
3038 symbol list. */
3039
3040 int
3041 symbol_shadow_p (const symbolS *s)
3042 {
3043 if (s->flags.local_symbol)
3044 return 0;
3045 return s->x->next == s;
3046 }
3047
3048 /* If S is a struct symbol return S, otherwise return NULL. */
3049
3050 symbolS *
3051 symbol_symbolS (symbolS *s)
3052 {
3053 if (s->flags.local_symbol)
3054 return NULL;
3055 return s;
3056 }
3057
3058 /* Return the BFD symbol for a symbol. */
3059
3060 asymbol *
3061 symbol_get_bfdsym (symbolS *s)
3062 {
3063 if (s->flags.local_symbol)
3064 s = local_symbol_convert (s);
3065 return s->bsym;
3066 }
3067
3068 /* Set the BFD symbol for a symbol. */
3069
3070 void
3071 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3072 {
3073 if (s->flags.local_symbol)
3074 s = local_symbol_convert (s);
3075 /* Usually, it is harmless to reset a symbol to a BFD section
3076 symbol. For example, obj_elf_change_section sets the BFD symbol
3077 of an old symbol with the newly created section symbol. But when
3078 we have multiple sections with the same name, the newly created
3079 section may have the same name as an old section. We check if the
3080 old symbol has been already marked as a section symbol before
3081 resetting it. */
3082 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3083 s->bsym = bsym;
3084 /* else XXX - What do we do now ? */
3085 }
3086
3087 #ifdef OBJ_SYMFIELD_TYPE
3088
3089 /* Get a pointer to the object format information for a symbol. */
3090
3091 OBJ_SYMFIELD_TYPE *
3092 symbol_get_obj (symbolS *s)
3093 {
3094 if (s->flags.local_symbol)
3095 s = local_symbol_convert (s);
3096 return &s->x->obj;
3097 }
3098
3099 /* Set the object format information for a symbol. */
3100
3101 void
3102 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
3103 {
3104 if (s->flags.local_symbol)
3105 s = local_symbol_convert (s);
3106 s->x->obj = *o;
3107 }
3108
3109 #endif /* OBJ_SYMFIELD_TYPE */
3110
3111 #ifdef TC_SYMFIELD_TYPE
3112
3113 /* Get a pointer to the processor information for a symbol. */
3114
3115 TC_SYMFIELD_TYPE *
3116 symbol_get_tc (symbolS *s)
3117 {
3118 if (s->flags.local_symbol)
3119 s = local_symbol_convert (s);
3120 return &s->x->tc;
3121 }
3122
3123 /* Set the processor information for a symbol. */
3124
3125 void
3126 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
3127 {
3128 if (s->flags.local_symbol)
3129 s = local_symbol_convert (s);
3130 s->x->tc = *o;
3131 }
3132
3133 #endif /* TC_SYMFIELD_TYPE */
3134
3135 void
3136 symbol_begin (void)
3137 {
3138 symbol_lastP = NULL;
3139 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
3140 sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3141 NULL, xcalloc, free);
3142
3143 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3144 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3145 #endif
3146 abs_symbol.x = &abs_symbol_x;
3147 abs_symbol.x->value.X_op = O_constant;
3148 abs_symbol.frag = &zero_address_frag;
3149
3150 if (LOCAL_LABELS_FB)
3151 fb_label_init ();
3152 }
3153
3154 void
3155 symbol_end (void)
3156 {
3157 htab_delete (sy_hash);
3158 }
3159
3160 void
3161 dot_symbol_init (void)
3162 {
3163 dot_symbol.name = ".";
3164 dot_symbol.flags.forward_ref = 1;
3165 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3166 if (dot_symbol.bsym == NULL)
3167 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3168 dot_symbol.bsym->name = ".";
3169 dot_symbol.x = &dot_symbol_x;
3170 dot_symbol.x->value.X_op = O_constant;
3171 }
3172
3173 int indent_level;
3175
3176 /* Maximum indent level.
3177 Available for modification inside a gdb session. */
3178 static int max_indent_level = 8;
3179
3180 void
3181 print_symbol_value_1 (FILE *file, symbolS *sym)
3182 {
3183 const char *name = S_GET_NAME (sym);
3184 if (!name || !name[0])
3185 name = "(unnamed)";
3186 fprintf (file, "sym %p %s", sym, name);
3187
3188 if (sym->flags.local_symbol)
3189 {
3190 struct local_symbol *locsym = (struct local_symbol *) sym;
3191
3192 if (locsym->frag != &zero_address_frag
3193 && locsym->frag != NULL)
3194 fprintf (file, " frag %p", locsym->frag);
3195 if (locsym->flags.resolved)
3196 fprintf (file, " resolved");
3197 fprintf (file, " local");
3198 }
3199 else
3200 {
3201 if (sym->frag != &zero_address_frag)
3202 fprintf (file, " frag %p", sym->frag);
3203 if (sym->flags.written)
3204 fprintf (file, " written");
3205 if (sym->flags.resolved)
3206 fprintf (file, " resolved");
3207 else if (sym->flags.resolving)
3208 fprintf (file, " resolving");
3209 if (sym->flags.used_in_reloc)
3210 fprintf (file, " used-in-reloc");
3211 if (sym->flags.used)
3212 fprintf (file, " used");
3213 if (S_IS_LOCAL (sym))
3214 fprintf (file, " local");
3215 if (S_IS_EXTERNAL (sym))
3216 fprintf (file, " extern");
3217 if (S_IS_WEAK (sym))
3218 fprintf (file, " weak");
3219 if (S_IS_DEBUG (sym))
3220 fprintf (file, " debug");
3221 if (S_IS_DEFINED (sym))
3222 fprintf (file, " defined");
3223 }
3224 if (S_IS_WEAKREFR (sym))
3225 fprintf (file, " weakrefr");
3226 if (S_IS_WEAKREFD (sym))
3227 fprintf (file, " weakrefd");
3228 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3229 if (symbol_resolved_p (sym))
3230 {
3231 segT s = S_GET_SEGMENT (sym);
3232
3233 if (s != undefined_section
3234 && s != expr_section)
3235 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3236 }
3237 else if (indent_level < max_indent_level
3238 && S_GET_SEGMENT (sym) != undefined_section)
3239 {
3240 indent_level++;
3241 fprintf (file, "\n%*s<", indent_level * 4, "");
3242 if (sym->flags.local_symbol)
3243 fprintf (file, "constant %lx",
3244 (unsigned long) ((struct local_symbol *) sym)->value);
3245 else
3246 print_expr_1 (file, &sym->x->value);
3247 fprintf (file, ">");
3248 indent_level--;
3249 }
3250 fflush (file);
3251 }
3252
3253 void
3254 print_symbol_value (symbolS *sym)
3255 {
3256 indent_level = 0;
3257 print_symbol_value_1 (stderr, sym);
3258 fprintf (stderr, "\n");
3259 }
3260
3261 static void
3262 print_binary (FILE *file, const char *name, expressionS *exp)
3263 {
3264 indent_level++;
3265 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3266 print_symbol_value_1 (file, exp->X_add_symbol);
3267 fprintf (file, ">\n%*s<", indent_level * 4, "");
3268 print_symbol_value_1 (file, exp->X_op_symbol);
3269 fprintf (file, ">");
3270 indent_level--;
3271 }
3272
3273 void
3274 print_expr_1 (FILE *file, expressionS *exp)
3275 {
3276 fprintf (file, "expr %p ", exp);
3277 switch (exp->X_op)
3278 {
3279 case O_illegal:
3280 fprintf (file, "illegal");
3281 break;
3282 case O_absent:
3283 fprintf (file, "absent");
3284 break;
3285 case O_constant:
3286 fprintf (file, "constant %" PRIx64, (uint64_t) exp->X_add_number);
3287 break;
3288 case O_symbol:
3289 indent_level++;
3290 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3291 print_symbol_value_1 (file, exp->X_add_symbol);
3292 fprintf (file, ">");
3293 maybe_print_addnum:
3294 if (exp->X_add_number)
3295 fprintf (file, "\n%*s%" PRIx64, indent_level * 4, "",
3296 (uint64_t) exp->X_add_number);
3297 indent_level--;
3298 break;
3299 case O_register:
3300 fprintf (file, "register #%d", (int) exp->X_add_number);
3301 break;
3302 case O_big:
3303 fprintf (file, "big");
3304 break;
3305 case O_uminus:
3306 fprintf (file, "uminus -<");
3307 indent_level++;
3308 print_symbol_value_1 (file, exp->X_add_symbol);
3309 fprintf (file, ">");
3310 goto maybe_print_addnum;
3311 case O_bit_not:
3312 fprintf (file, "bit_not");
3313 break;
3314 case O_multiply:
3315 print_binary (file, "multiply", exp);
3316 break;
3317 case O_divide:
3318 print_binary (file, "divide", exp);
3319 break;
3320 case O_modulus:
3321 print_binary (file, "modulus", exp);
3322 break;
3323 case O_left_shift:
3324 print_binary (file, "lshift", exp);
3325 break;
3326 case O_right_shift:
3327 print_binary (file, "rshift", exp);
3328 break;
3329 case O_bit_inclusive_or:
3330 print_binary (file, "bit_ior", exp);
3331 break;
3332 case O_bit_exclusive_or:
3333 print_binary (file, "bit_xor", exp);
3334 break;
3335 case O_bit_and:
3336 print_binary (file, "bit_and", exp);
3337 break;
3338 case O_eq:
3339 print_binary (file, "eq", exp);
3340 break;
3341 case O_ne:
3342 print_binary (file, "ne", exp);
3343 break;
3344 case O_lt:
3345 print_binary (file, "lt", exp);
3346 break;
3347 case O_le:
3348 print_binary (file, "le", exp);
3349 break;
3350 case O_ge:
3351 print_binary (file, "ge", exp);
3352 break;
3353 case O_gt:
3354 print_binary (file, "gt", exp);
3355 break;
3356 case O_logical_and:
3357 print_binary (file, "logical_and", exp);
3358 break;
3359 case O_logical_or:
3360 print_binary (file, "logical_or", exp);
3361 break;
3362 case O_add:
3363 indent_level++;
3364 fprintf (file, "add\n%*s<", indent_level * 4, "");
3365 print_symbol_value_1 (file, exp->X_add_symbol);
3366 fprintf (file, ">\n%*s<", indent_level * 4, "");
3367 print_symbol_value_1 (file, exp->X_op_symbol);
3368 fprintf (file, ">");
3369 goto maybe_print_addnum;
3370 case O_subtract:
3371 indent_level++;
3372 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3373 print_symbol_value_1 (file, exp->X_add_symbol);
3374 fprintf (file, ">\n%*s<", indent_level * 4, "");
3375 print_symbol_value_1 (file, exp->X_op_symbol);
3376 fprintf (file, ">");
3377 goto maybe_print_addnum;
3378 default:
3379 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3380 break;
3381 }
3382 fflush (stdout);
3383 }
3384
3385 void
3386 print_expr (expressionS *exp)
3387 {
3388 print_expr_1 (stderr, exp);
3389 fprintf (stderr, "\n");
3390 }
3391
3392 void
3393 symbol_print_statistics (FILE *file)
3394 {
3395 htab_print_statistics (file, "symbol table", sy_hash);
3396 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3397 local_symbol_count, local_symbol_conversion_count);
3398 }
3399
3400 #ifdef OBJ_COMPLEX_RELC
3401
3402 /* Convert given symbol to a new complex-relocation symbol name. This
3403 may be a recursive function, since it might be called for non-leaf
3404 nodes (plain symbols) in the expression tree. The caller owns the
3405 returning string, so should free it eventually. Errors are
3406 indicated via as_bad and a NULL return value. The given symbol
3407 is marked with used_in_reloc. */
3408
3409 char *
3410 symbol_relc_make_sym (symbolS * sym)
3411 {
3412 char * terminal = NULL;
3413 const char * sname;
3414 char typetag;
3415 int sname_len;
3416
3417 gas_assert (sym != NULL);
3418
3419 /* Recurse to symbol_relc_make_expr if this symbol
3420 is defined as an expression or a plain value. */
3421 if ( S_GET_SEGMENT (sym) == expr_section
3422 || S_GET_SEGMENT (sym) == absolute_section)
3423 return symbol_relc_make_expr (symbol_get_value_expression (sym));
3424
3425 /* This may be a "fake symbol", referring to ".".
3426 Write out a special null symbol to refer to this position. */
3427 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3428 return xstrdup (".");
3429
3430 /* We hope this is a plain leaf symbol. Construct the encoding
3431 as {S,s}II...:CCCCCCC....
3432 where 'S'/'s' means section symbol / plain symbol
3433 III is decimal for the symbol name length
3434 CCC is the symbol name itself. */
3435 symbol_mark_used_in_reloc (sym);
3436
3437 sname = S_GET_NAME (sym);
3438 sname_len = strlen (sname);
3439 typetag = symbol_section_p (sym) ? 'S' : 's';
3440
3441 terminal = XNEWVEC (char, (1 /* S or s */
3442 + 8 /* sname_len in decimal */
3443 + 1 /* _ spacer */
3444 + sname_len /* name itself */
3445 + 1 /* \0 */ ));
3446
3447 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3448 return terminal;
3449 }
3450
3451 /* Convert given value to a new complex-relocation symbol name. This
3452 is a non-recursive function, since it is be called for leaf nodes
3453 (plain values) in the expression tree. The caller owns the
3454 returning string, so should free() it eventually. No errors. */
3455
3456 char *
3457 symbol_relc_make_value (offsetT val)
3458 {
3459 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3460
3461 terminal[0] = '#';
3462 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3463 return terminal;
3464 }
3465
3466 /* Convert given expression to a new complex-relocation symbol name.
3467 This is a recursive function, since it traverses the entire given
3468 expression tree. The caller owns the returning string, so should
3469 free() it eventually. Errors are indicated via as_bad() and a NULL
3470 return value. */
3471
3472 char *
3473 symbol_relc_make_expr (expressionS * exp)
3474 {
3475 const char * opstr = NULL; /* Operator prefix string. */
3476 int arity = 0; /* Arity of this operator. */
3477 char * operands[3]; /* Up to three operands. */
3478 char * concat_string = NULL;
3479
3480 operands[0] = operands[1] = operands[2] = NULL;
3481
3482 gas_assert (exp != NULL);
3483
3484 /* Match known operators -> fill in opstr, arity, operands[] and fall
3485 through to construct subexpression fragments; may instead return
3486 string directly for leaf nodes. */
3487
3488 /* See expr.h for the meaning of all these enums. Many operators
3489 have an unnatural arity (X_add_number implicitly added). The
3490 conversion logic expands them to explicit "+" subexpressions. */
3491
3492 switch (exp->X_op)
3493 {
3494 default:
3495 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3496 break;
3497
3498 /* Leaf nodes. */
3499 case O_constant:
3500 return symbol_relc_make_value (exp->X_add_number);
3501
3502 case O_symbol:
3503 if (exp->X_add_number)
3504 {
3505 arity = 2;
3506 opstr = "+";
3507 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3508 operands[1] = symbol_relc_make_value (exp->X_add_number);
3509 break;
3510 }
3511 else
3512 return symbol_relc_make_sym (exp->X_add_symbol);
3513
3514 /* Helper macros for nesting nodes. */
3515
3516 #define HANDLE_XADD_OPT1(str_) \
3517 if (exp->X_add_number) \
3518 { \
3519 arity = 2; \
3520 opstr = "+:" str_; \
3521 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3522 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3523 break; \
3524 } \
3525 else \
3526 { \
3527 arity = 1; \
3528 opstr = str_; \
3529 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3530 } \
3531 break
3532
3533 #define HANDLE_XADD_OPT2(str_) \
3534 if (exp->X_add_number) \
3535 { \
3536 arity = 3; \
3537 opstr = "+:" str_; \
3538 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3539 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3540 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3541 } \
3542 else \
3543 { \
3544 arity = 2; \
3545 opstr = str_; \
3546 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3547 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3548 } \
3549 break
3550
3551 /* Nesting nodes. */
3552
3553 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3554 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3555 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3556 case O_multiply: HANDLE_XADD_OPT2 ("*");
3557 case O_divide: HANDLE_XADD_OPT2 ("/");
3558 case O_modulus: HANDLE_XADD_OPT2 ("%");
3559 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3560 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3561 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3562 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3563 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3564 case O_add: HANDLE_XADD_OPT2 ("+");
3565 case O_subtract: HANDLE_XADD_OPT2 ("-");
3566 case O_eq: HANDLE_XADD_OPT2 ("==");
3567 case O_ne: HANDLE_XADD_OPT2 ("!=");
3568 case O_lt: HANDLE_XADD_OPT2 ("<");
3569 case O_le: HANDLE_XADD_OPT2 ("<=");
3570 case O_ge: HANDLE_XADD_OPT2 (">=");
3571 case O_gt: HANDLE_XADD_OPT2 (">");
3572 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3573 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3574 }
3575
3576 /* Validate & reject early. */
3577 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3578 opstr = NULL;
3579 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3580 opstr = NULL;
3581 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3582 opstr = NULL;
3583
3584 if (opstr == NULL)
3585 concat_string = NULL;
3586 else if (arity == 0)
3587 concat_string = xstrdup (opstr);
3588 else if (arity == 1)
3589 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3590 else if (arity == 2)
3591 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3592 (char *) NULL);
3593 else
3594 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3595 operands[2], (char *) NULL);
3596
3597 /* Free operand strings (not opstr). */
3598 if (arity >= 1) xfree (operands[0]);
3599 if (arity >= 2) xfree (operands[1]);
3600 if (arity >= 3) xfree (operands[2]);
3601
3602 return concat_string;
3603 }
3604
3605 #endif
3606