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