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