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