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