gas: S_GET_{NAME,SEGMENT}() don't alter their input symbol
[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 if (S_IS_EXTERNAL (s))
2424 return 0;
2425
2426 if (bfd_asymbol_section (s->bsym) == reg_section)
2427 return 1;
2428
2429 flags = s->bsym->flags;
2430
2431 if (flag_strip_local_absolute
2432 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2433 the source file even when the object file is stripped. */
2434 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2435 && bfd_asymbol_section (s->bsym) == absolute_section)
2436 return 1;
2437
2438 name = S_GET_NAME (s);
2439 return (name != NULL
2440 && ! S_IS_DEBUG (s)
2441 && (strchr (name, DOLLAR_LABEL_CHAR)
2442 || strchr (name, LOCAL_LABEL_CHAR)
2443 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2444 || strchr (name, FAKE_LABEL_CHAR)
2445 #endif
2446 || TC_LABEL_IS_LOCAL (name)
2447 || (! flag_keep_locals
2448 && (bfd_is_local_label (stdoutput, s->bsym)
2449 || (flag_mri
2450 && name[0] == '?'
2451 && name[1] == '?')))));
2452 }
2453
2454 int
2455 S_IS_STABD (symbolS *s)
2456 {
2457 return S_GET_NAME (s) == 0;
2458 }
2459
2460 int
2461 S_CAN_BE_REDEFINED (const symbolS *s)
2462 {
2463 if (s->flags.local_symbol)
2464 return (((struct local_symbol *) s)->frag
2465 == &predefined_address_frag);
2466 /* Permit register names to be redefined. */
2467 return s->x->value.X_op == O_register;
2468 }
2469
2470 int
2471 S_IS_VOLATILE (const symbolS *s)
2472 {
2473 if (s->flags.local_symbol)
2474 return 0;
2475 return s->flags.volatil;
2476 }
2477
2478 int
2479 S_IS_FORWARD_REF (const symbolS *s)
2480 {
2481 if (s->flags.local_symbol)
2482 return 0;
2483 return s->flags.forward_ref;
2484 }
2485
2486 const char *
2487 S_GET_NAME (const symbolS *s)
2488 {
2489 return s->name;
2490 }
2491
2492 segT
2493 S_GET_SEGMENT (const symbolS *s)
2494 {
2495 if (s->flags.local_symbol)
2496 return ((struct local_symbol *) s)->section;
2497 return s->bsym->section;
2498 }
2499
2500 void
2501 S_SET_SEGMENT (symbolS *s, segT seg)
2502 {
2503 if (s->flags.local_symbol)
2504 {
2505 ((struct local_symbol *) s)->section = seg;
2506 return;
2507 }
2508
2509 /* Don't reassign section symbols. The direct reason is to prevent seg
2510 faults assigning back to const global symbols such as *ABS*, but it
2511 shouldn't happen anyway. */
2512 if (s->bsym->flags & BSF_SECTION_SYM)
2513 {
2514 if (s->bsym->section != seg)
2515 abort ();
2516 }
2517 else
2518 {
2519 if (multibyte_handling == multibyte_warn_syms
2520 && ! s->flags.local_symbol
2521 && seg != undefined_section
2522 && ! s->flags.multibyte_warned
2523 && scan_for_multibyte_characters ((const unsigned char *) s->name,
2524 (const unsigned char *) s->name + strlen (s->name),
2525 false))
2526 {
2527 as_warn (_("symbol '%s' contains multibyte characters"), s->name);
2528 s->flags.multibyte_warned = 1;
2529 }
2530
2531 s->bsym->section = seg;
2532 }
2533 }
2534
2535 void
2536 S_SET_EXTERNAL (symbolS *s)
2537 {
2538 if (s->flags.local_symbol)
2539 s = local_symbol_convert (s);
2540 if ((s->bsym->flags & BSF_WEAK) != 0)
2541 {
2542 /* Let .weak override .global. */
2543 return;
2544 }
2545 if (s->bsym->flags & BSF_SECTION_SYM)
2546 {
2547 /* Do not reassign section symbols. */
2548 as_warn (_("can't make section symbol global"));
2549 return;
2550 }
2551 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2552 if (S_GET_SEGMENT (s) == reg_section)
2553 {
2554 as_bad (_("can't make register symbol global"));
2555 return;
2556 }
2557 #endif
2558 s->bsym->flags |= BSF_GLOBAL;
2559 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2560
2561 #ifdef TE_PE
2562 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2563 an_external_name = S_GET_NAME (s);
2564 #endif
2565 }
2566
2567 void
2568 S_CLEAR_EXTERNAL (symbolS *s)
2569 {
2570 if (s->flags.local_symbol)
2571 return;
2572 if ((s->bsym->flags & BSF_WEAK) != 0)
2573 {
2574 /* Let .weak override. */
2575 return;
2576 }
2577 s->bsym->flags |= BSF_LOCAL;
2578 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2579 }
2580
2581 void
2582 S_SET_WEAK (symbolS *s)
2583 {
2584 if (s->flags.local_symbol)
2585 s = local_symbol_convert (s);
2586 #ifdef obj_set_weak_hook
2587 obj_set_weak_hook (s);
2588 #endif
2589 s->bsym->flags |= BSF_WEAK;
2590 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2591 }
2592
2593 void
2594 S_SET_WEAKREFR (symbolS *s)
2595 {
2596 if (s->flags.local_symbol)
2597 s = local_symbol_convert (s);
2598 s->flags.weakrefr = 1;
2599 /* If the alias was already used, make sure we mark the target as
2600 used as well, otherwise it might be dropped from the symbol
2601 table. This may have unintended side effects if the alias is
2602 later redirected to another symbol, such as keeping the unused
2603 previous target in the symbol table. Since it will be weak, it's
2604 not a big deal. */
2605 if (s->flags.used)
2606 symbol_mark_used (s->x->value.X_add_symbol);
2607 }
2608
2609 void
2610 S_CLEAR_WEAKREFR (symbolS *s)
2611 {
2612 if (s->flags.local_symbol)
2613 return;
2614 s->flags.weakrefr = 0;
2615 }
2616
2617 void
2618 S_SET_WEAKREFD (symbolS *s)
2619 {
2620 if (s->flags.local_symbol)
2621 s = local_symbol_convert (s);
2622 s->flags.weakrefd = 1;
2623 S_SET_WEAK (s);
2624 }
2625
2626 void
2627 S_CLEAR_WEAKREFD (symbolS *s)
2628 {
2629 if (s->flags.local_symbol)
2630 return;
2631 if (s->flags.weakrefd)
2632 {
2633 s->flags.weakrefd = 0;
2634 /* If a weakref target symbol is weak, then it was never
2635 referenced directly before, not even in a .global directive,
2636 so decay it to local. If it remains undefined, it will be
2637 later turned into a global, like any other undefined
2638 symbol. */
2639 if (s->bsym->flags & BSF_WEAK)
2640 {
2641 #ifdef obj_clear_weak_hook
2642 obj_clear_weak_hook (s);
2643 #endif
2644 s->bsym->flags &= ~BSF_WEAK;
2645 s->bsym->flags |= BSF_LOCAL;
2646 }
2647 }
2648 }
2649
2650 void
2651 S_SET_THREAD_LOCAL (symbolS *s)
2652 {
2653 if (s->flags.local_symbol)
2654 s = local_symbol_convert (s);
2655 if (bfd_is_com_section (s->bsym->section)
2656 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2657 return;
2658 s->bsym->flags |= BSF_THREAD_LOCAL;
2659 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2660 as_bad (_("Accessing function `%s' as thread-local object"),
2661 S_GET_NAME (s));
2662 else if (! bfd_is_und_section (s->bsym->section)
2663 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2664 as_bad (_("Accessing `%s' as thread-local object"),
2665 S_GET_NAME (s));
2666 }
2667
2668 void
2669 S_SET_NAME (symbolS *s, const char *name)
2670 {
2671 s->name = name;
2672 if (s->flags.local_symbol)
2673 return;
2674 s->bsym->name = name;
2675 }
2676
2677 void
2678 S_SET_VOLATILE (symbolS *s)
2679 {
2680 if (s->flags.local_symbol)
2681 s = local_symbol_convert (s);
2682 s->flags.volatil = 1;
2683 }
2684
2685 void
2686 S_CLEAR_VOLATILE (symbolS *s)
2687 {
2688 if (!s->flags.local_symbol)
2689 s->flags.volatil = 0;
2690 }
2691
2692 void
2693 S_SET_FORWARD_REF (symbolS *s)
2694 {
2695 if (s->flags.local_symbol)
2696 s = local_symbol_convert (s);
2697 s->flags.forward_ref = 1;
2698 }
2699
2700 /* Return the previous symbol in a chain. */
2701
2702 symbolS *
2703 symbol_previous (symbolS *s)
2704 {
2705 if (s->flags.local_symbol)
2706 abort ();
2707 return s->x->previous;
2708 }
2709
2710 /* Return the next symbol in a chain. */
2711
2712 symbolS *
2713 symbol_next (symbolS *s)
2714 {
2715 if (s->flags.local_symbol)
2716 abort ();
2717 return s->x->next;
2718 }
2719
2720 /* Return a pointer to the value of a symbol as an expression. */
2721
2722 expressionS *
2723 symbol_get_value_expression (symbolS *s)
2724 {
2725 if (s->flags.local_symbol)
2726 s = local_symbol_convert (s);
2727 return &s->x->value;
2728 }
2729
2730 /* Set the value of a symbol to an expression. */
2731
2732 void
2733 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2734 {
2735 if (s->flags.local_symbol)
2736 s = local_symbol_convert (s);
2737 s->x->value = *exp;
2738 S_CLEAR_WEAKREFR (s);
2739 }
2740
2741 /* Return whether 2 symbols are the same. */
2742
2743 int
2744 symbol_same_p (symbolS *s1, symbolS *s2)
2745 {
2746 return s1 == s2;
2747 }
2748
2749 /* Return a pointer to the X_add_number component of a symbol. */
2750
2751 offsetT *
2752 symbol_X_add_number (symbolS *s)
2753 {
2754 if (s->flags.local_symbol)
2755 return (offsetT *) &((struct local_symbol *) s)->value;
2756
2757 return &s->x->value.X_add_number;
2758 }
2759
2760 /* Set the value of SYM to the current position in the current segment. */
2761
2762 void
2763 symbol_set_value_now (symbolS *sym)
2764 {
2765 S_SET_SEGMENT (sym, now_seg);
2766 S_SET_VALUE (sym, frag_now_fix ());
2767 symbol_set_frag (sym, frag_now);
2768 }
2769
2770 /* Set the frag of a symbol. */
2771
2772 void
2773 symbol_set_frag (symbolS *s, fragS *f)
2774 {
2775 if (s->flags.local_symbol)
2776 {
2777 ((struct local_symbol *) s)->frag = f;
2778 return;
2779 }
2780 s->frag = f;
2781 S_CLEAR_WEAKREFR (s);
2782 }
2783
2784 /* Return the frag of a symbol. */
2785
2786 fragS *
2787 symbol_get_frag (symbolS *s)
2788 {
2789 if (s->flags.local_symbol)
2790 return ((struct local_symbol *) s)->frag;
2791 return s->frag;
2792 }
2793
2794 /* Mark a symbol as having been used. */
2795
2796 void
2797 symbol_mark_used (symbolS *s)
2798 {
2799 if (s->flags.local_symbol)
2800 return;
2801 s->flags.used = 1;
2802 if (S_IS_WEAKREFR (s))
2803 symbol_mark_used (s->x->value.X_add_symbol);
2804 }
2805
2806 /* Clear the mark of whether a symbol has been used. */
2807
2808 void
2809 symbol_clear_used (symbolS *s)
2810 {
2811 if (s->flags.local_symbol)
2812 s = local_symbol_convert (s);
2813 s->flags.used = 0;
2814 }
2815
2816 /* Return whether a symbol has been used. */
2817
2818 int
2819 symbol_used_p (symbolS *s)
2820 {
2821 if (s->flags.local_symbol)
2822 return 1;
2823 return s->flags.used;
2824 }
2825
2826 /* Mark a symbol as having been used in a reloc. */
2827
2828 void
2829 symbol_mark_used_in_reloc (symbolS *s)
2830 {
2831 if (s->flags.local_symbol)
2832 s = local_symbol_convert (s);
2833 s->flags.used_in_reloc = 1;
2834 }
2835
2836 /* Clear the mark of whether a symbol has been used in a reloc. */
2837
2838 void
2839 symbol_clear_used_in_reloc (symbolS *s)
2840 {
2841 if (s->flags.local_symbol)
2842 return;
2843 s->flags.used_in_reloc = 0;
2844 }
2845
2846 /* Return whether a symbol has been used in a reloc. */
2847
2848 int
2849 symbol_used_in_reloc_p (symbolS *s)
2850 {
2851 if (s->flags.local_symbol)
2852 return 0;
2853 return s->flags.used_in_reloc;
2854 }
2855
2856 /* Mark a symbol as an MRI common symbol. */
2857
2858 void
2859 symbol_mark_mri_common (symbolS *s)
2860 {
2861 if (s->flags.local_symbol)
2862 s = local_symbol_convert (s);
2863 s->flags.mri_common = 1;
2864 }
2865
2866 /* Clear the mark of whether a symbol is an MRI common symbol. */
2867
2868 void
2869 symbol_clear_mri_common (symbolS *s)
2870 {
2871 if (s->flags.local_symbol)
2872 return;
2873 s->flags.mri_common = 0;
2874 }
2875
2876 /* Return whether a symbol is an MRI common symbol. */
2877
2878 int
2879 symbol_mri_common_p (symbolS *s)
2880 {
2881 if (s->flags.local_symbol)
2882 return 0;
2883 return s->flags.mri_common;
2884 }
2885
2886 /* Mark a symbol as having been written. */
2887
2888 void
2889 symbol_mark_written (symbolS *s)
2890 {
2891 if (s->flags.local_symbol)
2892 return;
2893 s->flags.written = 1;
2894 }
2895
2896 /* Clear the mark of whether a symbol has been written. */
2897
2898 void
2899 symbol_clear_written (symbolS *s)
2900 {
2901 if (s->flags.local_symbol)
2902 return;
2903 s->flags.written = 0;
2904 }
2905
2906 /* Return whether a symbol has been written. */
2907
2908 int
2909 symbol_written_p (symbolS *s)
2910 {
2911 if (s->flags.local_symbol)
2912 return 0;
2913 return s->flags.written;
2914 }
2915
2916 /* Mark a symbol as to be removed. */
2917
2918 void
2919 symbol_mark_removed (symbolS *s)
2920 {
2921 if (s->flags.local_symbol)
2922 return;
2923 s->flags.removed = 1;
2924 }
2925
2926 /* Return whether a symbol has been marked to be removed. */
2927
2928 int
2929 symbol_removed_p (symbolS *s)
2930 {
2931 if (s->flags.local_symbol)
2932 return 0;
2933 return s->flags.removed;
2934 }
2935
2936 /* Mark a symbol has having been resolved. */
2937
2938 void
2939 symbol_mark_resolved (symbolS *s)
2940 {
2941 s->flags.resolved = 1;
2942 }
2943
2944 /* Return whether a symbol has been resolved. */
2945
2946 int
2947 symbol_resolved_p (symbolS *s)
2948 {
2949 return s->flags.resolved;
2950 }
2951
2952 /* Return whether a symbol is a section symbol. */
2953
2954 int
2955 symbol_section_p (symbolS *s)
2956 {
2957 if (s->flags.local_symbol)
2958 return 0;
2959 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2960 }
2961
2962 /* Return whether a symbol is equated to another symbol. */
2963
2964 int
2965 symbol_equated_p (symbolS *s)
2966 {
2967 if (s->flags.local_symbol)
2968 return 0;
2969 return s->x->value.X_op == O_symbol;
2970 }
2971
2972 /* Return whether a symbol is equated to another symbol, and should be
2973 treated specially when writing out relocs. */
2974
2975 int
2976 symbol_equated_reloc_p (symbolS *s)
2977 {
2978 if (s->flags.local_symbol)
2979 return 0;
2980 /* X_op_symbol, normally not used for O_symbol, is set by
2981 resolve_symbol_value to flag expression syms that have been
2982 equated. */
2983 return (s->x->value.X_op == O_symbol
2984 #if defined (OBJ_COFF) && defined (TE_PE)
2985 && ! S_IS_WEAK (s)
2986 #endif
2987 && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
2988 || ! S_IS_DEFINED (s)
2989 || S_IS_COMMON (s)));
2990 }
2991
2992 /* Return whether a symbol has a constant value. */
2993
2994 int
2995 symbol_constant_p (symbolS *s)
2996 {
2997 if (s->flags.local_symbol)
2998 return 1;
2999 return s->x->value.X_op == O_constant;
3000 }
3001
3002 /* Return whether a symbol was cloned and thus removed from the global
3003 symbol list. */
3004
3005 int
3006 symbol_shadow_p (symbolS *s)
3007 {
3008 if (s->flags.local_symbol)
3009 return 0;
3010 return s->x->next == s;
3011 }
3012
3013 /* If S is a struct symbol return S, otherwise return NULL. */
3014
3015 symbolS *
3016 symbol_symbolS (symbolS *s)
3017 {
3018 if (s->flags.local_symbol)
3019 return NULL;
3020 return s;
3021 }
3022
3023 /* Return the BFD symbol for a symbol. */
3024
3025 asymbol *
3026 symbol_get_bfdsym (symbolS *s)
3027 {
3028 if (s->flags.local_symbol)
3029 s = local_symbol_convert (s);
3030 return s->bsym;
3031 }
3032
3033 /* Set the BFD symbol for a symbol. */
3034
3035 void
3036 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3037 {
3038 if (s->flags.local_symbol)
3039 s = local_symbol_convert (s);
3040 /* Usually, it is harmless to reset a symbol to a BFD section
3041 symbol. For example, obj_elf_change_section sets the BFD symbol
3042 of an old symbol with the newly created section symbol. But when
3043 we have multiple sections with the same name, the newly created
3044 section may have the same name as an old section. We check if the
3045 old symbol has been already marked as a section symbol before
3046 resetting it. */
3047 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3048 s->bsym = bsym;
3049 /* else XXX - What do we do now ? */
3050 }
3051
3052 #ifdef OBJ_SYMFIELD_TYPE
3053
3054 /* Get a pointer to the object format information for a symbol. */
3055
3056 OBJ_SYMFIELD_TYPE *
3057 symbol_get_obj (symbolS *s)
3058 {
3059 if (s->flags.local_symbol)
3060 s = local_symbol_convert (s);
3061 return &s->x->obj;
3062 }
3063
3064 /* Set the object format information for a symbol. */
3065
3066 void
3067 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
3068 {
3069 if (s->flags.local_symbol)
3070 s = local_symbol_convert (s);
3071 s->x->obj = *o;
3072 }
3073
3074 #endif /* OBJ_SYMFIELD_TYPE */
3075
3076 #ifdef TC_SYMFIELD_TYPE
3077
3078 /* Get a pointer to the processor information for a symbol. */
3079
3080 TC_SYMFIELD_TYPE *
3081 symbol_get_tc (symbolS *s)
3082 {
3083 if (s->flags.local_symbol)
3084 s = local_symbol_convert (s);
3085 return &s->x->tc;
3086 }
3087
3088 /* Set the processor information for a symbol. */
3089
3090 void
3091 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
3092 {
3093 if (s->flags.local_symbol)
3094 s = local_symbol_convert (s);
3095 s->x->tc = *o;
3096 }
3097
3098 #endif /* TC_SYMFIELD_TYPE */
3099
3100 void
3101 symbol_begin (void)
3102 {
3103 symbol_lastP = NULL;
3104 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
3105 sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3106 NULL, xcalloc, free);
3107
3108 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3109 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3110 #endif
3111 abs_symbol.x = &abs_symbol_x;
3112 abs_symbol.x->value.X_op = O_constant;
3113 abs_symbol.frag = &zero_address_frag;
3114
3115 if (LOCAL_LABELS_FB)
3116 fb_label_init ();
3117 }
3118
3119 void
3120 symbol_end (void)
3121 {
3122 htab_delete (sy_hash);
3123 }
3124
3125 void
3126 dot_symbol_init (void)
3127 {
3128 dot_symbol.name = ".";
3129 dot_symbol.flags.forward_ref = 1;
3130 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3131 if (dot_symbol.bsym == NULL)
3132 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3133 dot_symbol.bsym->name = ".";
3134 dot_symbol.x = &dot_symbol_x;
3135 dot_symbol.x->value.X_op = O_constant;
3136 }
3137 \f
3138 int indent_level;
3139
3140 /* Maximum indent level.
3141 Available for modification inside a gdb session. */
3142 static int max_indent_level = 8;
3143
3144 void
3145 print_symbol_value_1 (FILE *file, symbolS *sym)
3146 {
3147 const char *name = S_GET_NAME (sym);
3148 if (!name || !name[0])
3149 name = "(unnamed)";
3150 fprintf (file, "sym %p %s", sym, name);
3151
3152 if (sym->flags.local_symbol)
3153 {
3154 struct local_symbol *locsym = (struct local_symbol *) sym;
3155
3156 if (locsym->frag != &zero_address_frag
3157 && locsym->frag != NULL)
3158 fprintf (file, " frag %p", locsym->frag);
3159 if (locsym->flags.resolved)
3160 fprintf (file, " resolved");
3161 fprintf (file, " local");
3162 }
3163 else
3164 {
3165 if (sym->frag != &zero_address_frag)
3166 fprintf (file, " frag %p", sym->frag);
3167 if (sym->flags.written)
3168 fprintf (file, " written");
3169 if (sym->flags.resolved)
3170 fprintf (file, " resolved");
3171 else if (sym->flags.resolving)
3172 fprintf (file, " resolving");
3173 if (sym->flags.used_in_reloc)
3174 fprintf (file, " used-in-reloc");
3175 if (sym->flags.used)
3176 fprintf (file, " used");
3177 if (S_IS_LOCAL (sym))
3178 fprintf (file, " local");
3179 if (S_IS_EXTERNAL (sym))
3180 fprintf (file, " extern");
3181 if (S_IS_WEAK (sym))
3182 fprintf (file, " weak");
3183 if (S_IS_DEBUG (sym))
3184 fprintf (file, " debug");
3185 if (S_IS_DEFINED (sym))
3186 fprintf (file, " defined");
3187 }
3188 if (S_IS_WEAKREFR (sym))
3189 fprintf (file, " weakrefr");
3190 if (S_IS_WEAKREFD (sym))
3191 fprintf (file, " weakrefd");
3192 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3193 if (symbol_resolved_p (sym))
3194 {
3195 segT s = S_GET_SEGMENT (sym);
3196
3197 if (s != undefined_section
3198 && s != expr_section)
3199 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3200 }
3201 else if (indent_level < max_indent_level
3202 && S_GET_SEGMENT (sym) != undefined_section)
3203 {
3204 indent_level++;
3205 fprintf (file, "\n%*s<", indent_level * 4, "");
3206 if (sym->flags.local_symbol)
3207 fprintf (file, "constant %lx",
3208 (unsigned long) ((struct local_symbol *) sym)->value);
3209 else
3210 print_expr_1 (file, &sym->x->value);
3211 fprintf (file, ">");
3212 indent_level--;
3213 }
3214 fflush (file);
3215 }
3216
3217 void
3218 print_symbol_value (symbolS *sym)
3219 {
3220 indent_level = 0;
3221 print_symbol_value_1 (stderr, sym);
3222 fprintf (stderr, "\n");
3223 }
3224
3225 static void
3226 print_binary (FILE *file, const char *name, expressionS *exp)
3227 {
3228 indent_level++;
3229 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3230 print_symbol_value_1 (file, exp->X_add_symbol);
3231 fprintf (file, ">\n%*s<", indent_level * 4, "");
3232 print_symbol_value_1 (file, exp->X_op_symbol);
3233 fprintf (file, ">");
3234 indent_level--;
3235 }
3236
3237 void
3238 print_expr_1 (FILE *file, expressionS *exp)
3239 {
3240 fprintf (file, "expr %p ", exp);
3241 switch (exp->X_op)
3242 {
3243 case O_illegal:
3244 fprintf (file, "illegal");
3245 break;
3246 case O_absent:
3247 fprintf (file, "absent");
3248 break;
3249 case O_constant:
3250 fprintf (file, "constant %" PRIx64, (uint64_t) exp->X_add_number);
3251 break;
3252 case O_symbol:
3253 indent_level++;
3254 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3255 print_symbol_value_1 (file, exp->X_add_symbol);
3256 fprintf (file, ">");
3257 maybe_print_addnum:
3258 if (exp->X_add_number)
3259 fprintf (file, "\n%*s%" PRIx64, indent_level * 4, "",
3260 (uint64_t) exp->X_add_number);
3261 indent_level--;
3262 break;
3263 case O_register:
3264 fprintf (file, "register #%d", (int) exp->X_add_number);
3265 break;
3266 case O_big:
3267 fprintf (file, "big");
3268 break;
3269 case O_uminus:
3270 fprintf (file, "uminus -<");
3271 indent_level++;
3272 print_symbol_value_1 (file, exp->X_add_symbol);
3273 fprintf (file, ">");
3274 goto maybe_print_addnum;
3275 case O_bit_not:
3276 fprintf (file, "bit_not");
3277 break;
3278 case O_multiply:
3279 print_binary (file, "multiply", exp);
3280 break;
3281 case O_divide:
3282 print_binary (file, "divide", exp);
3283 break;
3284 case O_modulus:
3285 print_binary (file, "modulus", exp);
3286 break;
3287 case O_left_shift:
3288 print_binary (file, "lshift", exp);
3289 break;
3290 case O_right_shift:
3291 print_binary (file, "rshift", exp);
3292 break;
3293 case O_bit_inclusive_or:
3294 print_binary (file, "bit_ior", exp);
3295 break;
3296 case O_bit_exclusive_or:
3297 print_binary (file, "bit_xor", exp);
3298 break;
3299 case O_bit_and:
3300 print_binary (file, "bit_and", exp);
3301 break;
3302 case O_eq:
3303 print_binary (file, "eq", exp);
3304 break;
3305 case O_ne:
3306 print_binary (file, "ne", exp);
3307 break;
3308 case O_lt:
3309 print_binary (file, "lt", exp);
3310 break;
3311 case O_le:
3312 print_binary (file, "le", exp);
3313 break;
3314 case O_ge:
3315 print_binary (file, "ge", exp);
3316 break;
3317 case O_gt:
3318 print_binary (file, "gt", exp);
3319 break;
3320 case O_logical_and:
3321 print_binary (file, "logical_and", exp);
3322 break;
3323 case O_logical_or:
3324 print_binary (file, "logical_or", exp);
3325 break;
3326 case O_add:
3327 indent_level++;
3328 fprintf (file, "add\n%*s<", indent_level * 4, "");
3329 print_symbol_value_1 (file, exp->X_add_symbol);
3330 fprintf (file, ">\n%*s<", indent_level * 4, "");
3331 print_symbol_value_1 (file, exp->X_op_symbol);
3332 fprintf (file, ">");
3333 goto maybe_print_addnum;
3334 case O_subtract:
3335 indent_level++;
3336 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3337 print_symbol_value_1 (file, exp->X_add_symbol);
3338 fprintf (file, ">\n%*s<", indent_level * 4, "");
3339 print_symbol_value_1 (file, exp->X_op_symbol);
3340 fprintf (file, ">");
3341 goto maybe_print_addnum;
3342 default:
3343 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3344 break;
3345 }
3346 fflush (stdout);
3347 }
3348
3349 void
3350 print_expr (expressionS *exp)
3351 {
3352 print_expr_1 (stderr, exp);
3353 fprintf (stderr, "\n");
3354 }
3355
3356 void
3357 symbol_print_statistics (FILE *file)
3358 {
3359 htab_print_statistics (file, "symbol table", sy_hash);
3360 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3361 local_symbol_count, local_symbol_conversion_count);
3362 }
3363
3364 #ifdef OBJ_COMPLEX_RELC
3365
3366 /* Convert given symbol to a new complex-relocation symbol name. This
3367 may be a recursive function, since it might be called for non-leaf
3368 nodes (plain symbols) in the expression tree. The caller owns the
3369 returning string, so should free it eventually. Errors are
3370 indicated via as_bad and a NULL return value. The given symbol
3371 is marked with used_in_reloc. */
3372
3373 char *
3374 symbol_relc_make_sym (symbolS * sym)
3375 {
3376 char * terminal = NULL;
3377 const char * sname;
3378 char typetag;
3379 int sname_len;
3380
3381 gas_assert (sym != NULL);
3382
3383 /* Recurse to symbol_relc_make_expr if this symbol
3384 is defined as an expression or a plain value. */
3385 if ( S_GET_SEGMENT (sym) == expr_section
3386 || S_GET_SEGMENT (sym) == absolute_section)
3387 return symbol_relc_make_expr (symbol_get_value_expression (sym));
3388
3389 /* This may be a "fake symbol", referring to ".".
3390 Write out a special null symbol to refer to this position. */
3391 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3392 return xstrdup (".");
3393
3394 /* We hope this is a plain leaf symbol. Construct the encoding
3395 as {S,s}II...:CCCCCCC....
3396 where 'S'/'s' means section symbol / plain symbol
3397 III is decimal for the symbol name length
3398 CCC is the symbol name itself. */
3399 symbol_mark_used_in_reloc (sym);
3400
3401 sname = S_GET_NAME (sym);
3402 sname_len = strlen (sname);
3403 typetag = symbol_section_p (sym) ? 'S' : 's';
3404
3405 terminal = XNEWVEC (char, (1 /* S or s */
3406 + 8 /* sname_len in decimal */
3407 + 1 /* _ spacer */
3408 + sname_len /* name itself */
3409 + 1 /* \0 */ ));
3410
3411 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3412 return terminal;
3413 }
3414
3415 /* Convert given value to a new complex-relocation symbol name. This
3416 is a non-recursive function, since it is be called for leaf nodes
3417 (plain values) in the expression tree. The caller owns the
3418 returning string, so should free() it eventually. No errors. */
3419
3420 char *
3421 symbol_relc_make_value (offsetT val)
3422 {
3423 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3424
3425 terminal[0] = '#';
3426 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3427 return terminal;
3428 }
3429
3430 /* Convert given expression to a new complex-relocation symbol name.
3431 This is a recursive function, since it traverses the entire given
3432 expression tree. The caller owns the returning string, so should
3433 free() it eventually. Errors are indicated via as_bad() and a NULL
3434 return value. */
3435
3436 char *
3437 symbol_relc_make_expr (expressionS * exp)
3438 {
3439 const char * opstr = NULL; /* Operator prefix string. */
3440 int arity = 0; /* Arity of this operator. */
3441 char * operands[3]; /* Up to three operands. */
3442 char * concat_string = NULL;
3443
3444 operands[0] = operands[1] = operands[2] = NULL;
3445
3446 gas_assert (exp != NULL);
3447
3448 /* Match known operators -> fill in opstr, arity, operands[] and fall
3449 through to construct subexpression fragments; may instead return
3450 string directly for leaf nodes. */
3451
3452 /* See expr.h for the meaning of all these enums. Many operators
3453 have an unnatural arity (X_add_number implicitly added). The
3454 conversion logic expands them to explicit "+" subexpressions. */
3455
3456 switch (exp->X_op)
3457 {
3458 default:
3459 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3460 break;
3461
3462 /* Leaf nodes. */
3463 case O_constant:
3464 return symbol_relc_make_value (exp->X_add_number);
3465
3466 case O_symbol:
3467 if (exp->X_add_number)
3468 {
3469 arity = 2;
3470 opstr = "+";
3471 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3472 operands[1] = symbol_relc_make_value (exp->X_add_number);
3473 break;
3474 }
3475 else
3476 return symbol_relc_make_sym (exp->X_add_symbol);
3477
3478 /* Helper macros for nesting nodes. */
3479
3480 #define HANDLE_XADD_OPT1(str_) \
3481 if (exp->X_add_number) \
3482 { \
3483 arity = 2; \
3484 opstr = "+:" str_; \
3485 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3486 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3487 break; \
3488 } \
3489 else \
3490 { \
3491 arity = 1; \
3492 opstr = str_; \
3493 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3494 } \
3495 break
3496
3497 #define HANDLE_XADD_OPT2(str_) \
3498 if (exp->X_add_number) \
3499 { \
3500 arity = 3; \
3501 opstr = "+:" str_; \
3502 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3503 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3504 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3505 } \
3506 else \
3507 { \
3508 arity = 2; \
3509 opstr = str_; \
3510 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3511 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3512 } \
3513 break
3514
3515 /* Nesting nodes. */
3516
3517 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3518 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3519 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3520 case O_multiply: HANDLE_XADD_OPT2 ("*");
3521 case O_divide: HANDLE_XADD_OPT2 ("/");
3522 case O_modulus: HANDLE_XADD_OPT2 ("%");
3523 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3524 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3525 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3526 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3527 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3528 case O_add: HANDLE_XADD_OPT2 ("+");
3529 case O_subtract: HANDLE_XADD_OPT2 ("-");
3530 case O_eq: HANDLE_XADD_OPT2 ("==");
3531 case O_ne: HANDLE_XADD_OPT2 ("!=");
3532 case O_lt: HANDLE_XADD_OPT2 ("<");
3533 case O_le: HANDLE_XADD_OPT2 ("<=");
3534 case O_ge: HANDLE_XADD_OPT2 (">=");
3535 case O_gt: HANDLE_XADD_OPT2 (">");
3536 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3537 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3538 }
3539
3540 /* Validate & reject early. */
3541 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3542 opstr = NULL;
3543 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3544 opstr = NULL;
3545 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3546 opstr = NULL;
3547
3548 if (opstr == NULL)
3549 concat_string = NULL;
3550 else if (arity == 0)
3551 concat_string = xstrdup (opstr);
3552 else if (arity == 1)
3553 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3554 else if (arity == 2)
3555 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3556 (char *) NULL);
3557 else
3558 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3559 operands[2], (char *) NULL);
3560
3561 /* Free operand strings (not opstr). */
3562 if (arity >= 1) xfree (operands[0]);
3563 if (arity >= 2) xfree (operands[1]);
3564 if (arity >= 3) xfree (operands[2]);
3565
3566 return concat_string;
3567 }
3568
3569 #endif