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