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