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