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