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