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