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