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