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