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