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