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