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