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