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