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