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