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