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