gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
252b5132 1/* symbols.c -symbol table-
b3adc24a 2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
7c743825 21/* #define DEBUG_SYMS / * to debug symbol list maintenance. */
252b5132 22
252b5132 23#include "as.h"
3882b010 24#include "safe-ctype.h"
252b5132
RH
25#include "obstack.h" /* For "symbols.h" */
26#include "subsegs.h"
2469b3c5 27#include "write.h"
49309057 28
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
2a50b401 117 the first field which is present in both structs.
8d1015a8
AM
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 329 struct local_symbol *ret;
0e1d094e 330 struct symbol_flags flags = { .sy_local_symbol = 1, .sy_resolved = 0 };
49309057
ILT
331
332 ++local_symbol_count;
333
334 name_copy = save_symbol_name (name);
335
1e9cc1c2 336 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
0e1d094e 337 ret->lsy_flags = flags;
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
d18d1999
CE
846symbolS *
847symbol_temp_new_now_octets (void)
848{
e2b9a97e 849 return symbol_temp_new (now_seg, frag_now_fix_octets (), frag_now);
d18d1999
CE
850}
851
b7d6ed97 852symbolS *
74937d39 853symbol_temp_make (void)
b7d6ed97 854{
756d1d01 855 return symbol_make (FAKE_LABEL_NAME);
b7d6ed97
RH
856}
857
7c743825
KH
858/* Implement symbol table lookup.
859 In: A symbol's name as a string: '\0' can't be part of a symbol name.
860 Out: NULL if the name was not in the symbol table, else the address
861 of a struct symbol associated with that name. */
252b5132 862
9758f3fc 863symbolS *
74937d39 864symbol_find_exact (const char *name)
06e77878
AO
865{
866 return symbol_find_exact_noref (name, 0);
867}
868
869symbolS *
870symbol_find_exact_noref (const char *name, int noref)
9758f3fc 871{
7be1c489 872 struct local_symbol *locsym;
06e77878 873 symbolS* sym;
9758f3fc 874
7be1c489
AM
875 locsym = (struct local_symbol *) hash_find (local_hash, name);
876 if (locsym != NULL)
877 return (symbolS *) locsym;
9758f3fc 878
06e77878
AO
879 sym = ((symbolS *) hash_find (sy_hash, name));
880
881 /* Any references to the symbol, except for the reference in
882 .weakref, must clear this flag, such that the symbol does not
883 turn into a weak symbol. Note that we don't have to handle the
884 local_symbol case, since a weakrefd is always promoted out of the
885 local_symbol table when it is turned into a weak symbol. */
886 if (sym && ! noref)
887 S_CLEAR_WEAKREFD (sym);
888
889 return sym;
9758f3fc
AM
890}
891
252b5132 892symbolS *
91c4c449 893symbol_find (const char *name)
06e77878
AO
894{
895 return symbol_find_noref (name, 0);
896}
897
898symbolS *
899symbol_find_noref (const char *name, int noref)
252b5132 900{
e1fa0163
NC
901 symbolS * result;
902 char * copy = NULL;
903
252b5132
RH
904#ifdef tc_canonicalize_symbol_name
905 {
e1fa0163 906 copy = xstrdup (name);
252b5132
RH
907 name = tc_canonicalize_symbol_name (copy);
908 }
909#endif
910
911 if (! symbols_case_sensitive)
912 {
03987ced 913 const char *orig;
e1fa0163 914 char *copy2 = NULL;
03987ced
RH
915 unsigned char c;
916
917 orig = name;
e1fa0163
NC
918 if (copy != NULL)
919 copy2 = copy;
325801bd 920 name = copy = XNEWVEC (char, strlen (name) + 1);
03987ced
RH
921
922 while ((c = *orig++) != '\0')
e1fa0163 923 *copy++ = TOUPPER (c);
03987ced 924 *copy = '\0';
e1fa0163 925
9fbb53c7 926 free (copy2);
e1fa0163 927 copy = (char *) name;
252b5132
RH
928 }
929
e1fa0163 930 result = symbol_find_exact_noref (name, noref);
9fbb53c7 931 free (copy);
e1fa0163 932 return result;
252b5132
RH
933}
934
7c743825
KH
935/* Once upon a time, symbols were kept in a singly linked list. At
936 least coff needs to be able to rearrange them from time to time, for
937 which a doubly linked list is much more convenient. Loic did these
938 as macros which seemed dangerous to me so they're now functions.
939 xoxorich. */
940
941/* Link symbol ADDME after symbol TARGET in the chain. */
252b5132 942
7c743825 943void
74937d39
KH
944symbol_append (symbolS *addme, symbolS *target,
945 symbolS **rootPP, symbolS **lastPP)
252b5132 946{
49309057
ILT
947 if (LOCAL_SYMBOL_CHECK (addme))
948 abort ();
949 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
950 abort ();
951
252b5132
RH
952 if (target == NULL)
953 {
954 know (*rootPP == NULL);
955 know (*lastPP == NULL);
956 addme->sy_next = NULL;
252b5132 957 addme->sy_previous = NULL;
252b5132
RH
958 *rootPP = addme;
959 *lastPP = addme;
960 return;
7c743825 961 } /* if the list is empty */
252b5132
RH
962
963 if (target->sy_next != NULL)
964 {
252b5132 965 target->sy_next->sy_previous = addme;
252b5132
RH
966 }
967 else
968 {
969 know (*lastPP == target);
970 *lastPP = addme;
7c743825 971 } /* if we have a next */
252b5132
RH
972
973 addme->sy_next = target->sy_next;
974 target->sy_next = addme;
252b5132 975 addme->sy_previous = target;
252b5132
RH
976
977 debug_verify_symchain (symbol_rootP, symbol_lastP);
978}
979
7c743825
KH
980/* Set the chain pointers of SYMBOL to null. */
981
982void
74937d39 983symbol_clear_list_pointers (symbolS *symbolP)
252b5132 984{
49309057
ILT
985 if (LOCAL_SYMBOL_CHECK (symbolP))
986 abort ();
252b5132 987 symbolP->sy_next = NULL;
252b5132 988 symbolP->sy_previous = NULL;
252b5132
RH
989}
990
7c743825
KH
991/* Remove SYMBOLP from the list. */
992
993void
74937d39 994symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
252b5132 995{
49309057
ILT
996 if (LOCAL_SYMBOL_CHECK (symbolP))
997 abort ();
998
252b5132
RH
999 if (symbolP == *rootPP)
1000 {
1001 *rootPP = symbolP->sy_next;
7c743825 1002 } /* if it was the root */
252b5132
RH
1003
1004 if (symbolP == *lastPP)
1005 {
1006 *lastPP = symbolP->sy_previous;
7c743825 1007 } /* if it was the tail */
252b5132
RH
1008
1009 if (symbolP->sy_next != NULL)
1010 {
1011 symbolP->sy_next->sy_previous = symbolP->sy_previous;
7c743825 1012 } /* if not last */
252b5132
RH
1013
1014 if (symbolP->sy_previous != NULL)
1015 {
1016 symbolP->sy_previous->sy_next = symbolP->sy_next;
7c743825 1017 } /* if not first */
252b5132
RH
1018
1019 debug_verify_symchain (*rootPP, *lastPP);
1020}
1021
7c743825
KH
1022/* Link symbol ADDME before symbol TARGET in the chain. */
1023
1024void
74937d39
KH
1025symbol_insert (symbolS *addme, symbolS *target,
1026 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
252b5132 1027{
49309057
ILT
1028 if (LOCAL_SYMBOL_CHECK (addme))
1029 abort ();
1030 if (LOCAL_SYMBOL_CHECK (target))
1031 abort ();
1032
252b5132
RH
1033 if (target->sy_previous != NULL)
1034 {
1035 target->sy_previous->sy_next = addme;
1036 }
1037 else
1038 {
1039 know (*rootPP == target);
1040 *rootPP = addme;
7c743825 1041 } /* if not first */
252b5132
RH
1042
1043 addme->sy_previous = target->sy_previous;
1044 target->sy_previous = addme;
1045 addme->sy_next = target;
1046
1047 debug_verify_symchain (*rootPP, *lastPP);
1048}
1049
7c743825 1050void
74937d39 1051verify_symbol_chain (symbolS *rootP, symbolS *lastP)
252b5132
RH
1052{
1053 symbolS *symbolP = rootP;
1054
1055 if (symbolP == NULL)
1056 return;
1057
1058 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1059 {
9c2799c2 1060 gas_assert (symbolP->bsym != NULL);
158184ac 1061 gas_assert (symbolP->sy_flags.sy_local_symbol == 0);
9c2799c2 1062 gas_assert (symbolP->sy_next->sy_previous == symbolP);
252b5132
RH
1063 }
1064
9c2799c2 1065 gas_assert (lastP == symbolP);
252b5132
RH
1066}
1067
8d1015a8
AM
1068int
1069symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1070{
1071 return (!LOCAL_SYMBOL_CHECK (s)
1072 && ((s->sy_next != s
1073 && s->sy_next != NULL
1074 && s->sy_next->sy_previous == s)
1075 || s == lastPP)
1076 && ((s->sy_previous != s
1077 && s->sy_previous != NULL
1078 && s->sy_previous->sy_next == s)
1079 || s == rootPP));
1080}
1081
280d71bf
DB
1082#ifdef OBJ_COMPLEX_RELC
1083
1084static int
1085use_complex_relocs_for (symbolS * symp)
1086{
1087 switch (symp->sy_value.X_op)
1088 {
1089 case O_constant:
1090 return 0;
1091
280d71bf
DB
1092 case O_multiply:
1093 case O_divide:
1094 case O_modulus:
1095 case O_left_shift:
1096 case O_right_shift:
1097 case O_bit_inclusive_or:
1098 case O_bit_or_not:
1099 case O_bit_exclusive_or:
1100 case O_bit_and:
1101 case O_add:
1102 case O_subtract:
1103 case O_eq:
1104 case O_ne:
1105 case O_lt:
1106 case O_le:
1107 case O_ge:
1108 case O_gt:
1109 case O_logical_and:
1110 case O_logical_or:
0f1309c8 1111 if ((S_IS_COMMON (symp->sy_value.X_op_symbol)
280d71bf 1112 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
280d71bf 1113 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
280d71bf 1114 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
0f1309c8
AM
1115 {
1116 case O_symbol:
1117 case O_symbol_rva:
1118 case O_uminus:
1119 case O_bit_not:
1120 case O_logical_not:
1121 if ((S_IS_COMMON (symp->sy_value.X_add_symbol)
1122 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
1123 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
1124 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section)
1125 return 0;
1126 }
280d71bf 1127 break;
34bca508 1128
280d71bf
DB
1129 default:
1130 break;
1131 }
1132 return 1;
1133}
1134#endif
1135
0909233e 1136static void
5a0ade8b 1137report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
0909233e 1138{
3b4dbbbf 1139 const char *file;
0909233e 1140 unsigned int line;
5a0ade8b
AM
1141 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1142 segT seg_right = S_GET_SEGMENT (right);
1143 const char *opname;
1144
1145 switch (op)
1146 {
1147 default:
1148 abort ();
1149 return;
1150
1151 case O_uminus: opname = "-"; break;
1152 case O_bit_not: opname = "~"; break;
1153 case O_logical_not: opname = "!"; break;
1154 case O_multiply: opname = "*"; break;
1155 case O_divide: opname = "/"; break;
1156 case O_modulus: opname = "%"; break;
1157 case O_left_shift: opname = "<<"; break;
1158 case O_right_shift: opname = ">>"; break;
1159 case O_bit_inclusive_or: opname = "|"; break;
1160 case O_bit_or_not: opname = "|~"; break;
1161 case O_bit_exclusive_or: opname = "^"; break;
1162 case O_bit_and: opname = "&"; break;
1163 case O_add: opname = "+"; break;
1164 case O_subtract: opname = "-"; break;
1165 case O_eq: opname = "=="; break;
1166 case O_ne: opname = "!="; break;
1167 case O_lt: opname = "<"; break;
1168 case O_le: opname = "<="; break;
1169 case O_ge: opname = ">="; break;
1170 case O_gt: opname = ">"; break;
1171 case O_logical_and: opname = "&&"; break;
1172 case O_logical_or: opname = "||"; break;
1173 }
1d3b2b27 1174
0909233e
AM
1175 if (expr_symbol_where (symp, &file, &line))
1176 {
5a0ade8b 1177 if (left)
0909233e 1178 as_bad_where (file, line,
5a0ade8b
AM
1179 _("invalid operands (%s and %s sections) for `%s'"),
1180 seg_left->name, seg_right->name, opname);
1181 else
0909233e 1182 as_bad_where (file, line,
5a0ade8b
AM
1183 _("invalid operand (%s section) for `%s'"),
1184 seg_right->name, opname);
0909233e
AM
1185 }
1186 else
1187 {
5a0ade8b
AM
1188 const char *sname = S_GET_NAME (symp);
1189
1190 if (left)
1191 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1192 seg_left->name, seg_right->name, opname, sname);
1193 else
1194 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1195 seg_right->name, opname, sname);
0909233e
AM
1196 }
1197}
1198
252b5132
RH
1199/* Resolve the value of a symbol. This is called during the final
1200 pass over the symbol table to resolve any symbols with complex
1201 values. */
1202
1203valueT
74937d39 1204resolve_symbol_value (symbolS *symp)
252b5132
RH
1205{
1206 int resolved;
1903f138 1207 valueT final_val;
252b5132
RH
1208 segT final_seg;
1209
49309057
ILT
1210 if (LOCAL_SYMBOL_CHECK (symp))
1211 {
1212 struct local_symbol *locsym = (struct local_symbol *) symp;
1213
bd780143 1214 final_val = locsym->lsy_value;
49309057 1215 if (local_symbol_resolved_p (locsym))
bd780143 1216 return final_val;
49309057 1217
61826503
CE
1218 /* Symbols whose section has SEC_ELF_OCTETS set,
1219 resolve to octets instead of target bytes. */
1220 if (locsym->lsy_section->flags & SEC_OCTETS)
1221 final_val += local_symbol_get_frag (locsym)->fr_address;
1222 else
1223 final_val += (local_symbol_get_frag (locsym)->fr_address
1224 / OCTETS_PER_BYTE);
49309057 1225
6386f3a7 1226 if (finalize_syms)
49309057 1227 {
bd780143 1228 locsym->lsy_value = final_val;
49309057
ILT
1229 local_symbol_mark_resolved (locsym);
1230 }
1231
1232 return final_val;
1233 }
1234
158184ac 1235 if (symp->sy_flags.sy_resolved)
252b5132 1236 {
1903f138 1237 final_val = 0;
2a50b401 1238 while (symp->sy_value.X_op == O_symbol)
1903f138
AM
1239 {
1240 final_val += symp->sy_value.X_add_number;
1241 symp = symp->sy_value.X_add_symbol;
2a50b401
AM
1242 if (LOCAL_SYMBOL_CHECK (symp))
1243 {
1244 struct local_symbol *locsym = (struct local_symbol *) symp;
1245 final_val += locsym->lsy_value;
1246 return final_val;
1247 }
1248 if (!symp->sy_flags.sy_resolved)
1249 return 0;
1903f138 1250 }
252b5132 1251 if (symp->sy_value.X_op == O_constant)
1903f138 1252 final_val += symp->sy_value.X_add_number;
252b5132 1253 else
1903f138
AM
1254 final_val = 0;
1255 return final_val;
252b5132
RH
1256 }
1257
1258 resolved = 0;
1259 final_seg = S_GET_SEGMENT (symp);
1260
158184ac 1261 if (symp->sy_flags.sy_resolving)
252b5132 1262 {
6386f3a7 1263 if (finalize_syms)
0e389e77 1264 as_bad (_("symbol definition loop encountered at `%s'"),
7c743825 1265 S_GET_NAME (symp));
252b5132
RH
1266 final_val = 0;
1267 resolved = 1;
1268 }
280d71bf
DB
1269#ifdef OBJ_COMPLEX_RELC
1270 else if (final_seg == expr_section
1271 && use_complex_relocs_for (symp))
1272 {
1273 symbolS * relc_symbol = NULL;
1274 char * relc_symbol_name = NULL;
1275
1276 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1277
1278 /* For debugging, print out conversion input & output. */
1279#ifdef DEBUG_SYMS
1280 print_expr (& symp->sy_value);
1281 if (relc_symbol_name)
1282 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1283#endif
1284
1285 if (relc_symbol_name != NULL)
1286 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1287 0, & zero_address_frag);
1288
1289 if (relc_symbol == NULL)
1290 {
1291 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1292 S_GET_NAME (symp));
1293 resolved = 0;
1294 }
1295 else
1296 {
1297 symbol_table_insert (relc_symbol);
1298
1299 /* S_CLEAR_EXTERNAL (relc_symbol); */
1300 if (symp->bsym->flags & BSF_SRELC)
1301 relc_symbol->bsym->flags |= BSF_SRELC;
1302 else
34bca508 1303 relc_symbol->bsym->flags |= BSF_RELC;
280d71bf
DB
1304 /* symp->bsym->flags |= BSF_RELC; */
1305 copy_symbol_attributes (symp, relc_symbol);
1306 symp->sy_value.X_op = O_symbol;
1307 symp->sy_value.X_add_symbol = relc_symbol;
1308 symp->sy_value.X_add_number = 0;
1309 resolved = 1;
1310 }
1311
1903f138 1312 final_val = 0;
280d71bf
DB
1313 final_seg = undefined_section;
1314 goto exit_dont_set_value;
1315 }
1316#endif
252b5132
RH
1317 else
1318 {
1319 symbolS *add_symbol, *op_symbol;
1320 offsetT left, right;
1321 segT seg_left, seg_right;
1322 operatorT op;
2d034539 1323 int move_seg_ok;
252b5132 1324
158184ac 1325 symp->sy_flags.sy_resolving = 1;
252b5132
RH
1326
1327 /* Help out with CSE. */
1328 add_symbol = symp->sy_value.X_add_symbol;
1329 op_symbol = symp->sy_value.X_op_symbol;
1330 final_val = symp->sy_value.X_add_number;
1331 op = symp->sy_value.X_op;
1332
1333 switch (op)
1334 {
1335 default:
1336 BAD_CASE (op);
1337 break;
1338
1339 case O_absent:
1340 final_val = 0;
1341 /* Fall through. */
1342
1343 case O_constant:
61826503
CE
1344 /* Symbols whose section has SEC_ELF_OCTETS set,
1345 resolve to octets instead of target bytes. */
1346 if (symp->bsym->section->flags & SEC_OCTETS)
1347 final_val += symp->sy_frag->fr_address;
1348 else
1349 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
252b5132
RH
1350 if (final_seg == expr_section)
1351 final_seg = absolute_section;
db557034
AM
1352 /* Fall through. */
1353
1354 case O_register:
252b5132
RH
1355 resolved = 1;
1356 break;
1357
1358 case O_symbol:
1359 case O_symbol_rva:
6386f3a7 1360 left = resolve_symbol_value (add_symbol);
e0890092
AM
1361 seg_left = S_GET_SEGMENT (add_symbol);
1362 if (finalize_syms)
1363 symp->sy_value.X_op_symbol = NULL;
252b5132 1364
e0890092 1365 do_symbol:
06e77878
AO
1366 if (S_IS_WEAKREFR (symp))
1367 {
9c2799c2 1368 gas_assert (final_val == 0);
06e77878
AO
1369 if (S_IS_WEAKREFR (add_symbol))
1370 {
9c2799c2 1371 gas_assert (add_symbol->sy_value.X_op == O_symbol
06e77878
AO
1372 && add_symbol->sy_value.X_add_number == 0);
1373 add_symbol = add_symbol->sy_value.X_add_symbol;
9c2799c2 1374 gas_assert (! S_IS_WEAKREFR (add_symbol));
06e77878
AO
1375 symp->sy_value.X_add_symbol = add_symbol;
1376 }
1377 }
1378
158184ac 1379 if (symp->sy_flags.sy_mri_common)
252b5132
RH
1380 {
1381 /* This is a symbol inside an MRI common section. The
e0890092
AM
1382 relocation routines are going to handle it specially.
1383 Don't change the value. */
49309057 1384 resolved = symbol_resolved_p (add_symbol);
252b5132
RH
1385 break;
1386 }
1387
2a50b401
AM
1388 /* Don't leave symbol loops. */
1389 if (finalize_syms
d402189f 1390 && !LOCAL_SYMBOL_CHECK (add_symbol)
2a50b401
AM
1391 && add_symbol->sy_flags.sy_resolving)
1392 break;
1393
6386f3a7 1394 if (finalize_syms && final_val == 0)
49309057
ILT
1395 {
1396 if (LOCAL_SYMBOL_CHECK (add_symbol))
1397 add_symbol = local_symbol_convert ((struct local_symbol *)
1398 add_symbol);
1399 copy_symbol_attributes (symp, add_symbol);
1400 }
252b5132 1401
e0890092
AM
1402 /* If we have equated this symbol to an undefined or common
1403 symbol, keep X_op set to O_symbol, and don't change
1404 X_add_number. This permits the routine which writes out
1405 relocation to detect this case, and convert the
1406 relocation to be against the symbol to which this symbol
1407 is equated. */
1903f138
AM
1408 if (seg_left == undefined_section
1409 || bfd_is_com_section (seg_left)
7be1c489 1410#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
1411 || S_IS_WEAK (add_symbol)
1412#endif
1903f138
AM
1413 || (finalize_syms
1414 && ((final_seg == expr_section
1415 && seg_left != expr_section
1416 && seg_left != absolute_section)
1417 || symbol_shadow_p (symp))))
252b5132 1418 {
6386f3a7 1419 if (finalize_syms)
252b5132 1420 {
252b5132
RH
1421 symp->sy_value.X_op = O_symbol;
1422 symp->sy_value.X_add_symbol = add_symbol;
1423 symp->sy_value.X_add_number = final_val;
e0890092
AM
1424 /* Use X_op_symbol as a flag. */
1425 symp->sy_value.X_op_symbol = add_symbol;
252b5132 1426 }
5a0ade8b 1427 final_seg = seg_left;
e0890092
AM
1428 final_val += symp->sy_frag->fr_address + left;
1429 resolved = symbol_resolved_p (add_symbol);
158184ac 1430 symp->sy_flags.sy_resolving = 0;
e0890092
AM
1431 goto exit_dont_set_value;
1432 }
252b5132
RH
1433 else
1434 {
1435 final_val += symp->sy_frag->fr_address + left;
1436 if (final_seg == expr_section || final_seg == undefined_section)
e0890092 1437 final_seg = seg_left;
252b5132
RH
1438 }
1439
49309057 1440 resolved = symbol_resolved_p (add_symbol);
06e77878 1441 if (S_IS_WEAKREFR (symp))
22987cec
AM
1442 {
1443 symp->sy_flags.sy_resolving = 0;
1444 goto exit_dont_set_value;
1445 }
252b5132
RH
1446 break;
1447
1448 case O_uminus:
1449 case O_bit_not:
1450 case O_logical_not:
6386f3a7 1451 left = resolve_symbol_value (add_symbol);
784b640d 1452 seg_left = S_GET_SEGMENT (add_symbol);
252b5132 1453
0909233e
AM
1454 /* By reducing these to the relevant dyadic operator, we get
1455 !S -> S == 0 permitted on anything,
1456 -S -> 0 - S only permitted on absolute
1457 ~S -> S ^ ~0 only permitted on absolute */
1458 if (op != O_logical_not && seg_left != absolute_section
1459 && finalize_syms)
5a0ade8b 1460 report_op_error (symp, NULL, op, add_symbol);
1d3b2b27 1461
0909233e
AM
1462 if (final_seg == expr_section || final_seg == undefined_section)
1463 final_seg = absolute_section;
1d3b2b27 1464
252b5132
RH
1465 if (op == O_uminus)
1466 left = -left;
1467 else if (op == O_logical_not)
1468 left = !left;
1469 else
1470 left = ~left;
1471
1472 final_val += left + symp->sy_frag->fr_address;
252b5132 1473
49309057 1474 resolved = symbol_resolved_p (add_symbol);
252b5132
RH
1475 break;
1476
1477 case O_multiply:
1478 case O_divide:
1479 case O_modulus:
1480 case O_left_shift:
1481 case O_right_shift:
1482 case O_bit_inclusive_or:
1483 case O_bit_or_not:
1484 case O_bit_exclusive_or:
1485 case O_bit_and:
1486 case O_add:
1487 case O_subtract:
1488 case O_eq:
1489 case O_ne:
1490 case O_lt:
1491 case O_le:
1492 case O_ge:
1493 case O_gt:
1494 case O_logical_and:
1495 case O_logical_or:
6386f3a7
AM
1496 left = resolve_symbol_value (add_symbol);
1497 right = resolve_symbol_value (op_symbol);
252b5132
RH
1498 seg_left = S_GET_SEGMENT (add_symbol);
1499 seg_right = S_GET_SEGMENT (op_symbol);
1500
1501 /* Simplify addition or subtraction of a constant by folding the
1502 constant into X_add_number. */
e0890092 1503 if (op == O_add)
252b5132
RH
1504 {
1505 if (seg_right == absolute_section)
1506 {
e0890092 1507 final_val += right;
252b5132
RH
1508 goto do_symbol;
1509 }
e0890092 1510 else if (seg_left == absolute_section)
252b5132 1511 {
252b5132
RH
1512 final_val += left;
1513 add_symbol = op_symbol;
1514 left = right;
e0890092
AM
1515 seg_left = seg_right;
1516 goto do_symbol;
1517 }
1518 }
1519 else if (op == O_subtract)
1520 {
1521 if (seg_right == absolute_section)
1522 {
1523 final_val -= right;
252b5132
RH
1524 goto do_symbol;
1525 }
1526 }
1527
2d034539 1528 move_seg_ok = 1;
e0890092
AM
1529 /* Equality and non-equality tests are permitted on anything.
1530 Subtraction, and other comparison operators are permitted if
1531 both operands are in the same section. Otherwise, both
1532 operands must be absolute. We already handled the case of
1533 addition or subtraction of a constant above. This will
1534 probably need to be changed for an object file format which
fdef3943 1535 supports arbitrary expressions. */
2d034539 1536 if (!(seg_left == absolute_section
5a0ade8b 1537 && seg_right == absolute_section)
0909233e
AM
1538 && !(op == O_eq || op == O_ne)
1539 && !((op == O_subtract
1540 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1541 && seg_left == seg_right
1542 && (seg_left != undefined_section
1543 || add_symbol == op_symbol)))
2d034539
NC
1544 {
1545 /* Don't emit messages unless we're finalizing the symbol value,
1546 otherwise we may get the same message multiple times. */
1547 if (finalize_syms)
5a0ade8b 1548 report_op_error (symp, add_symbol, op, op_symbol);
2d034539
NC
1549 /* However do not move the symbol into the absolute section
1550 if it cannot currently be resolved - this would confuse
1551 other parts of the assembler into believing that the
1552 expression had been evaluated to zero. */
1553 else
1554 move_seg_ok = 0;
1555 }
1d3b2b27 1556
2d034539
NC
1557 if (move_seg_ok
1558 && (final_seg == expr_section || final_seg == undefined_section))
0909233e 1559 final_seg = absolute_section;
252b5132
RH
1560
1561 /* Check for division by zero. */
1562 if ((op == O_divide || op == O_modulus) && right == 0)
1563 {
1564 /* If seg_right is not absolute_section, then we've
e0890092 1565 already issued a warning about using a bad symbol. */
6386f3a7 1566 if (seg_right == absolute_section && finalize_syms)
252b5132 1567 {
3b4dbbbf 1568 const char *file;
252b5132
RH
1569 unsigned int line;
1570
1571 if (expr_symbol_where (symp, &file, &line))
1572 as_bad_where (file, line, _("division by zero"));
1573 else
0e389e77 1574 as_bad (_("division by zero when setting `%s'"),
252b5132
RH
1575 S_GET_NAME (symp));
1576 }
1577
1578 right = 1;
1579 }
1580
1581 switch (symp->sy_value.X_op)
1582 {
1583 case O_multiply: left *= right; break;
1584 case O_divide: left /= right; break;
1585 case O_modulus: left %= right; break;
1586 case O_left_shift: left <<= right; break;
1587 case O_right_shift: left >>= right; break;
1588 case O_bit_inclusive_or: left |= right; break;
1589 case O_bit_or_not: left |= ~right; break;
1590 case O_bit_exclusive_or: left ^= right; break;
1591 case O_bit_and: left &= right; break;
1592 case O_add: left += right; break;
1593 case O_subtract: left -= right; break;
e0890092
AM
1594 case O_eq:
1595 case O_ne:
1596 left = (left == right && seg_left == seg_right
1597 && (seg_left != undefined_section
1598 || add_symbol == op_symbol)
1599 ? ~ (offsetT) 0 : 0);
1600 if (symp->sy_value.X_op == O_ne)
1601 left = ~left;
1602 break;
252b5132
RH
1603 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1604 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1605 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1606 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1607 case O_logical_and: left = left && right; break;
1608 case O_logical_or: left = left || right; break;
6d6ad65b
NC
1609
1610 case O_illegal:
1611 case O_absent:
1612 case O_constant:
1613 /* See PR 20895 for a reproducer. */
1614 as_bad (_("Invalid operation on symbol"));
1615 goto exit_dont_set_value;
1616
1617 default:
1618 abort ();
252b5132
RH
1619 }
1620
1621 final_val += symp->sy_frag->fr_address + left;
1622 if (final_seg == expr_section || final_seg == undefined_section)
784b640d
AM
1623 {
1624 if (seg_left == undefined_section
1625 || seg_right == undefined_section)
1626 final_seg = undefined_section;
1627 else if (seg_left == absolute_section)
1628 final_seg = seg_right;
1629 else
1630 final_seg = seg_left;
1631 }
49309057
ILT
1632 resolved = (symbol_resolved_p (add_symbol)
1633 && symbol_resolved_p (op_symbol));
7c743825 1634 break;
252b5132 1635
252b5132
RH
1636 case O_big:
1637 case O_illegal:
1638 /* Give an error (below) if not in expr_section. We don't
1639 want to worry about expr_section symbols, because they
1640 are fictional (they are created as part of expression
1641 resolution), and any problems may not actually mean
1642 anything. */
1643 break;
1644 }
1645
158184ac 1646 symp->sy_flags.sy_resolving = 0;
252b5132
RH
1647 }
1648
6386f3a7 1649 if (finalize_syms)
05bdb37e 1650 S_SET_VALUE (symp, final_val);
252b5132 1651
dc1e8a47 1652 exit_dont_set_value:
05bdb37e
AM
1653 /* Always set the segment, even if not finalizing the value.
1654 The segment is used to determine whether a symbol is defined. */
05bdb37e 1655 S_SET_SEGMENT (symp, final_seg);
252b5132 1656
252b5132 1657 /* Don't worry if we can't resolve an expr_section symbol. */
6386f3a7 1658 if (finalize_syms)
252b5132
RH
1659 {
1660 if (resolved)
158184ac 1661 symp->sy_flags.sy_resolved = 1;
252b5132
RH
1662 else if (S_GET_SEGMENT (symp) != expr_section)
1663 {
0e389e77 1664 as_bad (_("can't resolve value for symbol `%s'"),
7c743825 1665 S_GET_NAME (symp));
158184ac 1666 symp->sy_flags.sy_resolved = 1;
252b5132
RH
1667 }
1668 }
1669
1670 return final_val;
1671}
1672
5a49b8ac 1673static void resolve_local_symbol (const char *, void *);
49309057
ILT
1674
1675/* A static function passed to hash_traverse. */
1676
1677static void
5a49b8ac 1678resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
49309057
ILT
1679{
1680 if (value != NULL)
1e9cc1c2 1681 resolve_symbol_value ((symbolS *) value);
49309057
ILT
1682}
1683
49309057
ILT
1684/* Resolve all local symbols. */
1685
1686void
74937d39 1687resolve_local_symbol_values (void)
49309057 1688{
49309057 1689 hash_traverse (local_hash, resolve_local_symbol);
49309057
ILT
1690}
1691
9497f5ac
NC
1692/* Obtain the current value of a symbol without changing any
1693 sub-expressions used. */
1694
1695int
2e1e12b1 1696snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
9497f5ac 1697{
2e1e12b1
JB
1698 symbolS *symbolP = *symbolPP;
1699
9497f5ac
NC
1700 if (LOCAL_SYMBOL_CHECK (symbolP))
1701 {
1702 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1703
1704 *valueP = locsym->lsy_value;
1705 *segP = locsym->lsy_section;
1706 *fragPP = local_symbol_get_frag (locsym);
1707 }
1708 else
1709 {
91d6fa6a 1710 expressionS exp = symbolP->sy_value;
9497f5ac 1711
158184ac 1712 if (!symbolP->sy_flags.sy_resolved && exp.X_op != O_illegal)
9497f5ac
NC
1713 {
1714 int resolved;
1715
158184ac 1716 if (symbolP->sy_flags.sy_resolving)
9497f5ac 1717 return 0;
158184ac 1718 symbolP->sy_flags.sy_resolving = 1;
91d6fa6a 1719 resolved = resolve_expression (&exp);
158184ac 1720 symbolP->sy_flags.sy_resolving = 0;
9497f5ac
NC
1721 if (!resolved)
1722 return 0;
1723
91d6fa6a 1724 switch (exp.X_op)
9497f5ac
NC
1725 {
1726 case O_constant:
1727 case O_register:
2e1e12b1 1728 if (!symbol_equated_p (symbolP))
9497f5ac 1729 break;
2b0f3761 1730 /* Fallthru. */
9497f5ac
NC
1731 case O_symbol:
1732 case O_symbol_rva:
91d6fa6a 1733 symbolP = exp.X_add_symbol;
9497f5ac
NC
1734 break;
1735 default:
1736 return 0;
1737 }
1738 }
1739
d96eea71 1740 *symbolPP = symbolP;
e78bb25c
NC
1741
1742 /* A bogus input file can result in resolve_expression()
1743 generating a local symbol, so we have to check again. */
1744 if (LOCAL_SYMBOL_CHECK (symbolP))
1745 {
1746 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1747
1748 *valueP = locsym->lsy_value;
1749 *segP = locsym->lsy_section;
1750 *fragPP = local_symbol_get_frag (locsym);
1751 }
1752 else
1753 {
1754 *valueP = exp.X_add_number;
1755 *segP = symbolP->bsym->section;
1756 *fragPP = symbolP->sy_frag;
1757 }
9497f5ac
NC
1758
1759 if (*segP == expr_section)
91d6fa6a 1760 switch (exp.X_op)
9497f5ac
NC
1761 {
1762 case O_constant: *segP = absolute_section; break;
1763 case O_register: *segP = reg_section; break;
1764 default: break;
1765 }
1766 }
1767
1768 return 1;
1769}
1770
252b5132
RH
1771/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1772 They are *really* local. That is, they go out of scope whenever we see a
1773 label that isn't local. Also, like fb labels, there can be multiple
1774 instances of a dollar label. Therefor, we name encode each instance with
1775 the instance number, keep a list of defined symbols separate from the real
1776 symbol table, and we treat these buggers as a sparse array. */
1777
1778static long *dollar_labels;
1779static long *dollar_label_instances;
1780static char *dollar_label_defines;
1781static unsigned long dollar_label_count;
1782static unsigned long dollar_label_max;
1783
7c743825 1784int
74937d39 1785dollar_label_defined (long label)
252b5132
RH
1786{
1787 long *i;
1788
1789 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1790
1791 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1792 if (*i == label)
1793 return dollar_label_defines[i - dollar_labels];
1794
7c743825 1795 /* If we get here, label isn't defined. */
252b5132 1796 return 0;
7c743825 1797}
252b5132
RH
1798
1799static long
74937d39 1800dollar_label_instance (long label)
252b5132
RH
1801{
1802 long *i;
1803
1804 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1805
1806 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1807 if (*i == label)
1808 return (dollar_label_instances[i - dollar_labels]);
1809
7c743825
KH
1810 /* If we get here, we haven't seen the label before.
1811 Therefore its instance count is zero. */
252b5132
RH
1812 return 0;
1813}
1814
7c743825 1815void
74937d39 1816dollar_label_clear (void)
252b5132
RH
1817{
1818 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1819}
1820
1821#define DOLLAR_LABEL_BUMP_BY 10
1822
7c743825 1823void
74937d39 1824define_dollar_label (long label)
252b5132
RH
1825{
1826 long *i;
1827
1828 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1829 if (*i == label)
1830 {
1831 ++dollar_label_instances[i - dollar_labels];
1832 dollar_label_defines[i - dollar_labels] = 1;
1833 return;
1834 }
1835
7c743825 1836 /* If we get to here, we don't have label listed yet. */
252b5132
RH
1837
1838 if (dollar_labels == NULL)
1839 {
325801bd
TS
1840 dollar_labels = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
1841 dollar_label_instances = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
add39d23 1842 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
252b5132
RH
1843 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1844 dollar_label_count = 0;
1845 }
1846 else if (dollar_label_count == dollar_label_max)
1847 {
1848 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
325801bd
TS
1849 dollar_labels = XRESIZEVEC (long, dollar_labels, dollar_label_max);
1850 dollar_label_instances = XRESIZEVEC (long, dollar_label_instances,
1851 dollar_label_max);
add39d23
TS
1852 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
1853 dollar_label_max);
7c743825 1854 } /* if we needed to grow */
252b5132
RH
1855
1856 dollar_labels[dollar_label_count] = label;
1857 dollar_label_instances[dollar_label_count] = 1;
1858 dollar_label_defines[dollar_label_count] = 1;
1859 ++dollar_label_count;
1860}
1861
7c743825
KH
1862/* Caller must copy returned name: we re-use the area for the next name.
1863
2b0f3761 1864 The mth occurrence of label n: is turned into the symbol "Ln^Am"
7c743825
KH
1865 where n is the label number and m is the instance number. "L" makes
1866 it a label discarded unless debugging and "^A"('\1') ensures no
1867 ordinary symbol SHOULD get the same name as a local label
1868 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1869
1870 fb labels get the same treatment, except that ^B is used in place
1871 of ^A. */
1872
1873char * /* Return local label name. */
ed9e98c2
AM
1874dollar_label_name (long n, /* we just saw "n$:" : n a number. */
1875 int augend /* 0 for current instance, 1 for new instance. */)
252b5132
RH
1876{
1877 long i;
7c743825 1878 /* Returned to caller, then copied. Used for created names ("4f"). */
252b5132 1879 static char symbol_name_build[24];
ed9e98c2
AM
1880 char *p;
1881 char *q;
7c743825 1882 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
252b5132
RH
1883
1884 know (n >= 0);
1885 know (augend == 0 || augend == 1);
1886 p = symbol_name_build;
f84dd1f0
NC
1887#ifdef LOCAL_LABEL_PREFIX
1888 *p++ = LOCAL_LABEL_PREFIX;
1889#endif
252b5132
RH
1890 *p++ = 'L';
1891
7c743825
KH
1892 /* Next code just does sprintf( {}, "%d", n); */
1893 /* Label number. */
252b5132
RH
1894 q = symbol_name_temporary;
1895 for (*q++ = 0, i = n; i; ++q)
1896 {
1897 *q = i % 10 + '0';
1898 i /= 10;
1899 }
1900 while ((*p = *--q) != '\0')
1901 ++p;
1902
aa257fcd 1903 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
252b5132 1904
7c743825 1905 /* Instance number. */
252b5132
RH
1906 q = symbol_name_temporary;
1907 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1908 {
1909 *q = i % 10 + '0';
1910 i /= 10;
1911 }
5bb3703f 1912 while ((*p++ = *--q) != '\0');
252b5132 1913
7c743825 1914 /* The label, as a '\0' ended string, starts at symbol_name_build. */
252b5132
RH
1915 return symbol_name_build;
1916}
1917
47eebc20 1918/* Somebody else's idea of local labels. They are made by "n:" where n
7c743825
KH
1919 is any decimal digit. Refer to them with
1920 "nb" for previous (backward) n:
1921 or "nf" for next (forward) n:.
1922
1923 We do a little better and let n be any number, not just a single digit, but
1924 since the other guy's assembler only does ten, we treat the first ten
1925 specially.
1926
1927 Like someone else's assembler, we have one set of local label counters for
1928 entire assembly, not one set per (sub)segment like in most assemblers. This
1929 implies that one can refer to a label in another segment, and indeed some
1930 crufty compilers have done just that.
1931
1932 Since there could be a LOT of these things, treat them as a sparse
1933 array. */
252b5132
RH
1934
1935#define FB_LABEL_SPECIAL (10)
1936
1937static long fb_low_counter[FB_LABEL_SPECIAL];
1938static long *fb_labels;
1939static long *fb_label_instances;
1940static long fb_label_count;
1941static long fb_label_max;
1942
7c743825 1943/* This must be more than FB_LABEL_SPECIAL. */
252b5132
RH
1944#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1945
7c743825 1946static void
74937d39 1947fb_label_init (void)
252b5132
RH
1948{
1949 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
7c743825 1950}
252b5132 1951
7c743825
KH
1952/* Add one to the instance number of this fb label. */
1953
1954void
74937d39 1955fb_label_instance_inc (long label)
252b5132
RH
1956{
1957 long *i;
1958
b4e6cb80 1959 if ((unsigned long) label < FB_LABEL_SPECIAL)
252b5132
RH
1960 {
1961 ++fb_low_counter[label];
1962 return;
1963 }
1964
1965 if (fb_labels != NULL)
1966 {
1967 for (i = fb_labels + FB_LABEL_SPECIAL;
1968 i < fb_labels + fb_label_count; ++i)
1969 {
1970 if (*i == label)
1971 {
1972 ++fb_label_instances[i - fb_labels];
1973 return;
7c743825
KH
1974 } /* if we find it */
1975 } /* for each existing label */
252b5132
RH
1976 }
1977
7c743825 1978 /* If we get to here, we don't have label listed yet. */
252b5132
RH
1979
1980 if (fb_labels == NULL)
1981 {
325801bd
TS
1982 fb_labels = XNEWVEC (long, FB_LABEL_BUMP_BY);
1983 fb_label_instances = XNEWVEC (long, FB_LABEL_BUMP_BY);
252b5132
RH
1984 fb_label_max = FB_LABEL_BUMP_BY;
1985 fb_label_count = FB_LABEL_SPECIAL;
1986
1987 }
1988 else if (fb_label_count == fb_label_max)
1989 {
1990 fb_label_max += FB_LABEL_BUMP_BY;
325801bd
TS
1991 fb_labels = XRESIZEVEC (long, fb_labels, fb_label_max);
1992 fb_label_instances = XRESIZEVEC (long, fb_label_instances, fb_label_max);
7c743825 1993 } /* if we needed to grow */
252b5132
RH
1994
1995 fb_labels[fb_label_count] = label;
1996 fb_label_instances[fb_label_count] = 1;
1997 ++fb_label_count;
1998}
1999
7c743825 2000static long
74937d39 2001fb_label_instance (long label)
252b5132
RH
2002{
2003 long *i;
2004
b4e6cb80 2005 if ((unsigned long) label < FB_LABEL_SPECIAL)
252b5132
RH
2006 {
2007 return (fb_low_counter[label]);
2008 }
2009
2010 if (fb_labels != NULL)
2011 {
2012 for (i = fb_labels + FB_LABEL_SPECIAL;
2013 i < fb_labels + fb_label_count; ++i)
2014 {
2015 if (*i == label)
2016 {
2017 return (fb_label_instances[i - fb_labels]);
7c743825
KH
2018 } /* if we find it */
2019 } /* for each existing label */
252b5132
RH
2020 }
2021
2022 /* We didn't find the label, so this must be a reference to the
2023 first instance. */
2024 return 0;
2025}
2026
7c743825
KH
2027/* Caller must copy returned name: we re-use the area for the next name.
2028
2b0f3761 2029 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
7c743825
KH
2030 where n is the label number and m is the instance number. "L" makes
2031 it a label discarded unless debugging and "^B"('\2') ensures no
2032 ordinary symbol SHOULD get the same name as a local label
2033 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2034
2035 dollar labels get the same treatment, except that ^A is used in
2036 place of ^B. */
2037
2038char * /* Return local label name. */
74937d39
KH
2039fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
2040 long augend /* 0 for nb, 1 for n:, nf. */)
252b5132
RH
2041{
2042 long i;
7c743825 2043 /* Returned to caller, then copied. Used for created names ("4f"). */
252b5132 2044 static char symbol_name_build[24];
ed9e98c2
AM
2045 char *p;
2046 char *q;
7c743825 2047 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
252b5132
RH
2048
2049 know (n >= 0);
a76903bf 2050#ifdef TC_MMIX
71ba24a1 2051 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
a76903bf 2052#else
71ba24a1 2053 know ((unsigned long) augend <= 1);
a76903bf 2054#endif
252b5132 2055 p = symbol_name_build;
aa257fcd
NC
2056#ifdef LOCAL_LABEL_PREFIX
2057 *p++ = LOCAL_LABEL_PREFIX;
2058#endif
252b5132
RH
2059 *p++ = 'L';
2060
7c743825
KH
2061 /* Next code just does sprintf( {}, "%d", n); */
2062 /* Label number. */
252b5132
RH
2063 q = symbol_name_temporary;
2064 for (*q++ = 0, i = n; i; ++q)
2065 {
2066 *q = i % 10 + '0';
2067 i /= 10;
2068 }
2069 while ((*p = *--q) != '\0')
2070 ++p;
2071
aa257fcd 2072 *p++ = LOCAL_LABEL_CHAR; /* ^B */
252b5132 2073
7c743825 2074 /* Instance number. */
252b5132
RH
2075 q = symbol_name_temporary;
2076 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
2077 {
2078 *q = i % 10 + '0';
2079 i /= 10;
2080 }
5bb3703f 2081 while ((*p++ = *--q) != '\0');
252b5132 2082
7c743825 2083 /* The label, as a '\0' ended string, starts at symbol_name_build. */
252b5132 2084 return (symbol_name_build);
7c743825 2085}
252b5132 2086
7c743825
KH
2087/* Decode name that may have been generated by foo_label_name() above.
2088 If the name wasn't generated by foo_label_name(), then return it
2089 unaltered. This is used for error messages. */
252b5132
RH
2090
2091char *
74937d39 2092decode_local_label_name (char *s)
252b5132
RH
2093{
2094 char *p;
2095 char *symbol_decode;
2096 int label_number;
2097 int instance_number;
cd0bbe6e 2098 const char *type;
d95767bf 2099 const char *message_format;
91d6fa6a 2100 int lindex = 0;
43ad3147 2101
aa257fcd 2102#ifdef LOCAL_LABEL_PREFIX
91d6fa6a
NC
2103 if (s[lindex] == LOCAL_LABEL_PREFIX)
2104 ++lindex;
aa257fcd 2105#endif
43ad3147 2106
91d6fa6a 2107 if (s[lindex] != 'L')
252b5132
RH
2108 return s;
2109
91d6fa6a 2110 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
252b5132
RH
2111 label_number = (10 * label_number) + *p - '0';
2112
aa257fcd 2113 if (*p == DOLLAR_LABEL_CHAR)
252b5132 2114 type = "dollar";
aa257fcd 2115 else if (*p == LOCAL_LABEL_CHAR)
252b5132
RH
2116 type = "fb";
2117 else
2118 return s;
2119
3882b010 2120 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
252b5132
RH
2121 instance_number = (10 * instance_number) + *p - '0';
2122
d95767bf 2123 message_format = _("\"%d\" (instance number %d of a %s label)");
1e9cc1c2 2124 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
252b5132
RH
2125 sprintf (symbol_decode, message_format, label_number, instance_number, type);
2126
2127 return symbol_decode;
2128}
2129
2130/* Get the value of a symbol. */
2131
2132valueT
74937d39 2133S_GET_VALUE (symbolS *s)
252b5132 2134{
49309057 2135 if (LOCAL_SYMBOL_CHECK (s))
ac62c346 2136 return resolve_symbol_value (s);
49309057 2137
158184ac 2138 if (!s->sy_flags.sy_resolved)
e46d99eb 2139 {
6386f3a7
AM
2140 valueT val = resolve_symbol_value (s);
2141 if (!finalize_syms)
e46d99eb
AM
2142 return val;
2143 }
06e77878
AO
2144 if (S_IS_WEAKREFR (s))
2145 return S_GET_VALUE (s->sy_value.X_add_symbol);
2146
252b5132
RH
2147 if (s->sy_value.X_op != O_constant)
2148 {
158184ac 2149 if (! s->sy_flags.sy_resolved
252b5132
RH
2150 || s->sy_value.X_op != O_symbol
2151 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
0e389e77 2152 as_bad (_("attempt to get value of unresolved symbol `%s'"),
252b5132 2153 S_GET_NAME (s));
252b5132
RH
2154 }
2155 return (valueT) s->sy_value.X_add_number;
2156}
2157
2158/* Set the value of a symbol. */
2159
2160void
74937d39 2161S_SET_VALUE (symbolS *s, valueT val)
252b5132 2162{
49309057
ILT
2163 if (LOCAL_SYMBOL_CHECK (s))
2164 {
bd780143 2165 ((struct local_symbol *) s)->lsy_value = val;
49309057
ILT
2166 return;
2167 }
2168
252b5132
RH
2169 s->sy_value.X_op = O_constant;
2170 s->sy_value.X_add_number = (offsetT) val;
2171 s->sy_value.X_unsigned = 0;
06e77878 2172 S_CLEAR_WEAKREFR (s);
252b5132
RH
2173}
2174
2175void
74937d39 2176copy_symbol_attributes (symbolS *dest, symbolS *src)
252b5132 2177{
49309057 2178 if (LOCAL_SYMBOL_CHECK (dest))
7f2f689c 2179 dest = local_symbol_convert ((struct local_symbol *) dest);
49309057 2180 if (LOCAL_SYMBOL_CHECK (src))
7f2f689c 2181 src = local_symbol_convert ((struct local_symbol *) src);
49309057 2182
252b5132
RH
2183 /* In an expression, transfer the settings of these flags.
2184 The user can override later, of course. */
ad04f5ce
L
2185#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2186 | BSF_GNU_INDIRECT_FUNCTION)
252b5132 2187 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
252b5132
RH
2188
2189#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2190 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2191#endif
794ba86a
DJ
2192
2193#ifdef TC_COPY_SYMBOL_ATTRIBUTES
2194 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2195#endif
252b5132
RH
2196}
2197
252b5132 2198int
74937d39 2199S_IS_FUNCTION (symbolS *s)
252b5132 2200{
49309057
ILT
2201 flagword flags;
2202
2203 if (LOCAL_SYMBOL_CHECK (s))
2204 return 0;
2205
2206 flags = s->bsym->flags;
252b5132
RH
2207
2208 return (flags & BSF_FUNCTION) != 0;
2209}
2210
2211int
74937d39 2212S_IS_EXTERNAL (symbolS *s)
252b5132 2213{
49309057
ILT
2214 flagword flags;
2215
2216 if (LOCAL_SYMBOL_CHECK (s))
2217 return 0;
2218
2219 flags = s->bsym->flags;
252b5132 2220
7c743825 2221 /* Sanity check. */
252b5132
RH
2222 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2223 abort ();
2224
2225 return (flags & BSF_GLOBAL) != 0;
2226}
2227
2228int
74937d39 2229S_IS_WEAK (symbolS *s)
252b5132 2230{
49309057
ILT
2231 if (LOCAL_SYMBOL_CHECK (s))
2232 return 0;
06e77878
AO
2233 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2234 could probably handle a WEAKREFR as always weak though. E.g., if
2235 the referenced symbol has lost its weak status, there's no reason
2236 to keep handling the weakrefr as if it was weak. */
2237 if (S_IS_WEAKREFR (s))
2238 return S_IS_WEAK (s->sy_value.X_add_symbol);
252b5132
RH
2239 return (s->bsym->flags & BSF_WEAK) != 0;
2240}
2241
06e77878
AO
2242int
2243S_IS_WEAKREFR (symbolS *s)
2244{
2245 if (LOCAL_SYMBOL_CHECK (s))
2246 return 0;
158184ac 2247 return s->sy_flags.sy_weakrefr != 0;
06e77878
AO
2248}
2249
2250int
2251S_IS_WEAKREFD (symbolS *s)
2252{
2253 if (LOCAL_SYMBOL_CHECK (s))
2254 return 0;
158184ac 2255 return s->sy_flags.sy_weakrefd != 0;
06e77878
AO
2256}
2257
252b5132 2258int
74937d39 2259S_IS_COMMON (symbolS *s)
252b5132 2260{
49309057
ILT
2261 if (LOCAL_SYMBOL_CHECK (s))
2262 return 0;
252b5132
RH
2263 return bfd_is_com_section (s->bsym->section);
2264}
2265
2266int
74937d39 2267S_IS_DEFINED (symbolS *s)
252b5132 2268{
49309057
ILT
2269 if (LOCAL_SYMBOL_CHECK (s))
2270 return ((struct local_symbol *) s)->lsy_section != undefined_section;
252b5132
RH
2271 return s->bsym->section != undefined_section;
2272}
2273
a161fe53
AM
2274
2275#ifndef EXTERN_FORCE_RELOC
2276#define EXTERN_FORCE_RELOC IS_ELF
2277#endif
2278
2279/* Return true for symbols that should not be reduced to section
2280 symbols or eliminated from expressions, because they may be
2281 overridden by the linker. */
2282int
74937d39 2283S_FORCE_RELOC (symbolS *s, int strict)
a161fe53 2284{
f2d830a5 2285 segT sec;
a161fe53 2286 if (LOCAL_SYMBOL_CHECK (s))
f2d830a5
AM
2287 sec = ((struct local_symbol *) s)->lsy_section;
2288 else
2289 {
2290 if ((strict
ae6063d4
AM
2291 && ((s->bsym->flags & BSF_WEAK) != 0
2292 || (EXTERN_FORCE_RELOC
2293 && (s->bsym->flags & BSF_GLOBAL) != 0)))
f2d830a5
AM
2294 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2295 return TRUE;
2296 sec = s->bsym->section;
2297 }
2298 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
a161fe53
AM
2299}
2300
252b5132 2301int
74937d39 2302S_IS_DEBUG (symbolS *s)
252b5132 2303{
49309057
ILT
2304 if (LOCAL_SYMBOL_CHECK (s))
2305 return 0;
252b5132
RH
2306 if (s->bsym->flags & BSF_DEBUGGING)
2307 return 1;
2308 return 0;
2309}
2310
2311int
74937d39 2312S_IS_LOCAL (symbolS *s)
252b5132 2313{
49309057 2314 flagword flags;
252b5132
RH
2315 const char *name;
2316
49309057
ILT
2317 if (LOCAL_SYMBOL_CHECK (s))
2318 return 1;
2319
2320 flags = s->bsym->flags;
2321
7c743825 2322 /* Sanity check. */
252b5132
RH
2323 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2324 abort ();
2325
e6f7f6d1 2326 if (bfd_asymbol_section (s->bsym) == reg_section)
252b5132
RH
2327 return 1;
2328
2329 if (flag_strip_local_absolute
bb82af9f
NC
2330 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2331 the source file even when the object file is stripped. */
2332 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
e6f7f6d1 2333 && bfd_asymbol_section (s->bsym) == absolute_section)
252b5132
RH
2334 return 1;
2335
2336 name = S_GET_NAME (s);
2337 return (name != NULL
2338 && ! S_IS_DEBUG (s)
aa257fcd
NC
2339 && (strchr (name, DOLLAR_LABEL_CHAR)
2340 || strchr (name, LOCAL_LABEL_CHAR)
2469b3c5
JW
2341#if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2342 || strchr (name, FAKE_LABEL_CHAR)
2343#endif
df58fc94 2344 || TC_LABEL_IS_LOCAL (name)
252b5132
RH
2345 || (! flag_keep_locals
2346 && (bfd_is_local_label (stdoutput, s->bsym)
2347 || (flag_mri
2348 && name[0] == '?'
2349 && name[1] == '?')))));
2350}
2351
252b5132 2352int
74937d39 2353S_IS_STABD (symbolS *s)
252b5132
RH
2354{
2355 return S_GET_NAME (s) == 0;
2356}
2357
6885131b
AM
2358int
2359S_CAN_BE_REDEFINED (const symbolS *s)
2360{
2361 if (LOCAL_SYMBOL_CHECK (s))
2362 return (local_symbol_get_frag ((struct local_symbol *) s)
2363 == &predefined_address_frag);
2364 /* Permit register names to be redefined. */
2365 return s->bsym->section == reg_section;
2366}
2367
9497f5ac
NC
2368int
2369S_IS_VOLATILE (const symbolS *s)
2370{
2371 if (LOCAL_SYMBOL_CHECK (s))
2372 return 0;
158184ac 2373 return s->sy_flags.sy_volatile;
9497f5ac
NC
2374}
2375
2376int
2377S_IS_FORWARD_REF (const symbolS *s)
2378{
2379 if (LOCAL_SYMBOL_CHECK (s))
2380 return 0;
158184ac 2381 return s->sy_flags.sy_forward_ref;
9497f5ac
NC
2382}
2383
9758f3fc 2384const char *
74937d39 2385S_GET_NAME (symbolS *s)
252b5132 2386{
49309057
ILT
2387 if (LOCAL_SYMBOL_CHECK (s))
2388 return ((struct local_symbol *) s)->lsy_name;
252b5132
RH
2389 return s->bsym->name;
2390}
2391
2392segT
74937d39 2393S_GET_SEGMENT (symbolS *s)
252b5132 2394{
49309057
ILT
2395 if (LOCAL_SYMBOL_CHECK (s))
2396 return ((struct local_symbol *) s)->lsy_section;
252b5132
RH
2397 return s->bsym->section;
2398}
2399
2400void
74937d39 2401S_SET_SEGMENT (symbolS *s, segT seg)
252b5132
RH
2402{
2403 /* Don't reassign section symbols. The direct reason is to prevent seg
2404 faults assigning back to const global symbols such as *ABS*, but it
2405 shouldn't happen anyway. */
2406
49309057
ILT
2407 if (LOCAL_SYMBOL_CHECK (s))
2408 {
2409 if (seg == reg_section)
2410 s = local_symbol_convert ((struct local_symbol *) s);
2411 else
2412 {
2413 ((struct local_symbol *) s)->lsy_section = seg;
2414 return;
2415 }
2416 }
2417
252b5132
RH
2418 if (s->bsym->flags & BSF_SECTION_SYM)
2419 {
2420 if (s->bsym->section != seg)
7c743825 2421 abort ();
252b5132
RH
2422 }
2423 else
2424 s->bsym->section = seg;
2425}
2426
2427void
74937d39 2428S_SET_EXTERNAL (symbolS *s)
252b5132 2429{
49309057
ILT
2430 if (LOCAL_SYMBOL_CHECK (s))
2431 s = local_symbol_convert ((struct local_symbol *) s);
252b5132
RH
2432 if ((s->bsym->flags & BSF_WEAK) != 0)
2433 {
2434 /* Let .weak override .global. */
2435 return;
2436 }
4d7c34bf
NC
2437 if (s->bsym->flags & BSF_SECTION_SYM)
2438 {
4d7c34bf 2439 /* Do not reassign section symbols. */
3b4dbbbf 2440 as_warn (_("section symbols are already global"));
4d7c34bf
NC
2441 return;
2442 }
97c4f2d9 2443#ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
d0548f34
L
2444 if (S_GET_SEGMENT (s) == reg_section)
2445 {
2446 as_bad ("can't make register symbol `%s' global",
2447 S_GET_NAME (s));
2448 return;
2449 }
97c4f2d9 2450#endif
252b5132 2451 s->bsym->flags |= BSF_GLOBAL;
7c743825 2452 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
977cdf5a 2453
22ba0981 2454#ifdef TE_PE
977cdf5a
NC
2455 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2456 an_external_name = S_GET_NAME (s);
2457#endif
252b5132
RH
2458}
2459
2460void
74937d39 2461S_CLEAR_EXTERNAL (symbolS *s)
252b5132 2462{
49309057
ILT
2463 if (LOCAL_SYMBOL_CHECK (s))
2464 return;
252b5132
RH
2465 if ((s->bsym->flags & BSF_WEAK) != 0)
2466 {
2467 /* Let .weak override. */
2468 return;
2469 }
2470 s->bsym->flags |= BSF_LOCAL;
7c743825 2471 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
252b5132
RH
2472}
2473
2474void
74937d39 2475S_SET_WEAK (symbolS *s)
252b5132 2476{
49309057
ILT
2477 if (LOCAL_SYMBOL_CHECK (s))
2478 s = local_symbol_convert ((struct local_symbol *) s);
06e77878
AO
2479#ifdef obj_set_weak_hook
2480 obj_set_weak_hook (s);
2481#endif
252b5132 2482 s->bsym->flags |= BSF_WEAK;
7c743825 2483 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
252b5132
RH
2484}
2485
06e77878
AO
2486void
2487S_SET_WEAKREFR (symbolS *s)
2488{
2489 if (LOCAL_SYMBOL_CHECK (s))
2490 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2491 s->sy_flags.sy_weakrefr = 1;
06e77878
AO
2492 /* If the alias was already used, make sure we mark the target as
2493 used as well, otherwise it might be dropped from the symbol
2494 table. This may have unintended side effects if the alias is
2495 later redirected to another symbol, such as keeping the unused
2496 previous target in the symbol table. Since it will be weak, it's
2497 not a big deal. */
158184ac 2498 if (s->sy_flags.sy_used)
06e77878
AO
2499 symbol_mark_used (s->sy_value.X_add_symbol);
2500}
2501
2502void
2503S_CLEAR_WEAKREFR (symbolS *s)
2504{
2505 if (LOCAL_SYMBOL_CHECK (s))
2506 return;
158184ac 2507 s->sy_flags.sy_weakrefr = 0;
06e77878
AO
2508}
2509
2510void
2511S_SET_WEAKREFD (symbolS *s)
2512{
2513 if (LOCAL_SYMBOL_CHECK (s))
2514 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2515 s->sy_flags.sy_weakrefd = 1;
06e77878
AO
2516 S_SET_WEAK (s);
2517}
2518
2519void
2520S_CLEAR_WEAKREFD (symbolS *s)
2521{
2522 if (LOCAL_SYMBOL_CHECK (s))
2523 return;
158184ac 2524 if (s->sy_flags.sy_weakrefd)
06e77878 2525 {
158184ac 2526 s->sy_flags.sy_weakrefd = 0;
06e77878
AO
2527 /* If a weakref target symbol is weak, then it was never
2528 referenced directly before, not even in a .global directive,
2529 so decay it to local. If it remains undefined, it will be
2530 later turned into a global, like any other undefined
2531 symbol. */
2532 if (s->bsym->flags & BSF_WEAK)
2533 {
2534#ifdef obj_clear_weak_hook
2535 obj_clear_weak_hook (s);
2536#endif
2537 s->bsym->flags &= ~BSF_WEAK;
2538 s->bsym->flags |= BSF_LOCAL;
2539 }
2540 }
2541}
2542
00f7efb6 2543void
74937d39 2544S_SET_THREAD_LOCAL (symbolS *s)
00f7efb6
JJ
2545{
2546 if (LOCAL_SYMBOL_CHECK (s))
2547 s = local_symbol_convert ((struct local_symbol *) s);
2548 if (bfd_is_com_section (s->bsym->section)
2549 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2550 return;
2551 s->bsym->flags |= BSF_THREAD_LOCAL;
2552 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2553 as_bad (_("Accessing function `%s' as thread-local object"),
2554 S_GET_NAME (s));
2555 else if (! bfd_is_und_section (s->bsym->section)
2556 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2557 as_bad (_("Accessing `%s' as thread-local object"),
2558 S_GET_NAME (s));
2559}
2560
252b5132 2561void
977cdf5a 2562S_SET_NAME (symbolS *s, const char *name)
252b5132 2563{
49309057
ILT
2564 if (LOCAL_SYMBOL_CHECK (s))
2565 {
2566 ((struct local_symbol *) s)->lsy_name = name;
2567 return;
2568 }
252b5132
RH
2569 s->bsym->name = name;
2570}
49309057 2571
9497f5ac
NC
2572void
2573S_SET_VOLATILE (symbolS *s)
2574{
2575 if (LOCAL_SYMBOL_CHECK (s))
2576 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2577 s->sy_flags.sy_volatile = 1;
9497f5ac
NC
2578}
2579
92757bc9
JB
2580void
2581S_CLEAR_VOLATILE (symbolS *s)
2582{
2583 if (!LOCAL_SYMBOL_CHECK (s))
158184ac 2584 s->sy_flags.sy_volatile = 0;
92757bc9
JB
2585}
2586
9497f5ac
NC
2587void
2588S_SET_FORWARD_REF (symbolS *s)
2589{
2590 if (LOCAL_SYMBOL_CHECK (s))
2591 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2592 s->sy_flags.sy_forward_ref = 1;
9497f5ac
NC
2593}
2594
49309057
ILT
2595/* Return the previous symbol in a chain. */
2596
2597symbolS *
74937d39 2598symbol_previous (symbolS *s)
49309057
ILT
2599{
2600 if (LOCAL_SYMBOL_CHECK (s))
2601 abort ();
2602 return s->sy_previous;
2603}
2604
49309057
ILT
2605/* Return the next symbol in a chain. */
2606
2607symbolS *
74937d39 2608symbol_next (symbolS *s)
49309057
ILT
2609{
2610 if (LOCAL_SYMBOL_CHECK (s))
2611 abort ();
2612 return s->sy_next;
2613}
2614
2615/* Return a pointer to the value of a symbol as an expression. */
2616
2617expressionS *
74937d39 2618symbol_get_value_expression (symbolS *s)
49309057
ILT
2619{
2620 if (LOCAL_SYMBOL_CHECK (s))
2621 s = local_symbol_convert ((struct local_symbol *) s);
2622 return &s->sy_value;
2623}
2624
2625/* Set the value of a symbol to an expression. */
2626
2627void
74937d39 2628symbol_set_value_expression (symbolS *s, const expressionS *exp)
49309057
ILT
2629{
2630 if (LOCAL_SYMBOL_CHECK (s))
2631 s = local_symbol_convert ((struct local_symbol *) s);
2632 s->sy_value = *exp;
06e77878 2633 S_CLEAR_WEAKREFR (s);
49309057
ILT
2634}
2635
087d837e
L
2636/* Return whether 2 symbols are the same. */
2637
2638int
2639symbol_same_p (symbolS *s1, symbolS *s2)
2640{
38cf168b
AM
2641 s1 = get_real_sym (s1);
2642 s2 = get_real_sym (s2);
087d837e
L
2643 return s1 == s2;
2644}
2645
be95a9c1
AM
2646/* Return a pointer to the X_add_number component of a symbol. */
2647
514d955d 2648offsetT *
be95a9c1
AM
2649symbol_X_add_number (symbolS *s)
2650{
be95a9c1 2651 if (LOCAL_SYMBOL_CHECK (s))
514d955d 2652 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
be95a9c1 2653
514d955d 2654 return &s->sy_value.X_add_number;
be95a9c1
AM
2655}
2656
b7d6ed97
RH
2657/* Set the value of SYM to the current position in the current segment. */
2658
2659void
74937d39 2660symbol_set_value_now (symbolS *sym)
b7d6ed97
RH
2661{
2662 S_SET_SEGMENT (sym, now_seg);
2663 S_SET_VALUE (sym, frag_now_fix ());
2664 symbol_set_frag (sym, frag_now);
2665}
2666
49309057
ILT
2667/* Set the frag of a symbol. */
2668
2669void
74937d39 2670symbol_set_frag (symbolS *s, fragS *f)
49309057
ILT
2671{
2672 if (LOCAL_SYMBOL_CHECK (s))
2673 {
2674 local_symbol_set_frag ((struct local_symbol *) s, f);
2675 return;
2676 }
2677 s->sy_frag = f;
06e77878 2678 S_CLEAR_WEAKREFR (s);
49309057
ILT
2679}
2680
2681/* Return the frag of a symbol. */
2682
2683fragS *
74937d39 2684symbol_get_frag (symbolS *s)
49309057
ILT
2685{
2686 if (LOCAL_SYMBOL_CHECK (s))
2687 return local_symbol_get_frag ((struct local_symbol *) s);
2688 return s->sy_frag;
2689}
2690
2691/* Mark a symbol as having been used. */
2692
2693void
74937d39 2694symbol_mark_used (symbolS *s)
49309057
ILT
2695{
2696 if (LOCAL_SYMBOL_CHECK (s))
2697 return;
158184ac 2698 s->sy_flags.sy_used = 1;
06e77878
AO
2699 if (S_IS_WEAKREFR (s))
2700 symbol_mark_used (s->sy_value.X_add_symbol);
49309057
ILT
2701}
2702
2703/* Clear the mark of whether a symbol has been used. */
2704
2705void
74937d39 2706symbol_clear_used (symbolS *s)
49309057
ILT
2707{
2708 if (LOCAL_SYMBOL_CHECK (s))
2709 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2710 s->sy_flags.sy_used = 0;
49309057
ILT
2711}
2712
2713/* Return whether a symbol has been used. */
2714
2715int
74937d39 2716symbol_used_p (symbolS *s)
49309057
ILT
2717{
2718 if (LOCAL_SYMBOL_CHECK (s))
2719 return 1;
158184ac 2720 return s->sy_flags.sy_used;
49309057
ILT
2721}
2722
2723/* Mark a symbol as having been used in a reloc. */
2724
2725void
74937d39 2726symbol_mark_used_in_reloc (symbolS *s)
49309057
ILT
2727{
2728 if (LOCAL_SYMBOL_CHECK (s))
2729 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2730 s->sy_flags.sy_used_in_reloc = 1;
49309057
ILT
2731}
2732
2733/* Clear the mark of whether a symbol has been used in a reloc. */
2734
2735void
74937d39 2736symbol_clear_used_in_reloc (symbolS *s)
49309057
ILT
2737{
2738 if (LOCAL_SYMBOL_CHECK (s))
2739 return;
158184ac 2740 s->sy_flags.sy_used_in_reloc = 0;
49309057
ILT
2741}
2742
2743/* Return whether a symbol has been used in a reloc. */
2744
2745int
74937d39 2746symbol_used_in_reloc_p (symbolS *s)
49309057
ILT
2747{
2748 if (LOCAL_SYMBOL_CHECK (s))
2749 return 0;
158184ac 2750 return s->sy_flags.sy_used_in_reloc;
49309057
ILT
2751}
2752
2753/* Mark a symbol as an MRI common symbol. */
2754
2755void
74937d39 2756symbol_mark_mri_common (symbolS *s)
49309057
ILT
2757{
2758 if (LOCAL_SYMBOL_CHECK (s))
2759 s = local_symbol_convert ((struct local_symbol *) s);
158184ac 2760 s->sy_flags.sy_mri_common = 1;
49309057
ILT
2761}
2762
2763/* Clear the mark of whether a symbol is an MRI common symbol. */
2764
2765void
74937d39 2766symbol_clear_mri_common (symbolS *s)
49309057
ILT
2767{
2768 if (LOCAL_SYMBOL_CHECK (s))
2769 return;
158184ac 2770 s->sy_flags.sy_mri_common = 0;
49309057
ILT
2771}
2772
2773/* Return whether a symbol is an MRI common symbol. */
2774
2775int
74937d39 2776symbol_mri_common_p (symbolS *s)
49309057
ILT
2777{
2778 if (LOCAL_SYMBOL_CHECK (s))
2779 return 0;
158184ac 2780 return s->sy_flags.sy_mri_common;
49309057
ILT
2781}
2782
2783/* Mark a symbol as having been written. */
2784
2785void
74937d39 2786symbol_mark_written (symbolS *s)
49309057
ILT
2787{
2788 if (LOCAL_SYMBOL_CHECK (s))
2789 return;
158184ac 2790 s->sy_flags.sy_written = 1;
49309057
ILT
2791}
2792
2793/* Clear the mark of whether a symbol has been written. */
2794
2795void
74937d39 2796symbol_clear_written (symbolS *s)
49309057
ILT
2797{
2798 if (LOCAL_SYMBOL_CHECK (s))
2799 return;
158184ac 2800 s->sy_flags.sy_written = 0;
49309057
ILT
2801}
2802
2803/* Return whether a symbol has been written. */
2804
2805int
74937d39 2806symbol_written_p (symbolS *s)
49309057
ILT
2807{
2808 if (LOCAL_SYMBOL_CHECK (s))
2809 return 0;
158184ac 2810 return s->sy_flags.sy_written;
49309057
ILT
2811}
2812
2813/* Mark a symbol has having been resolved. */
2814
2815void
74937d39 2816symbol_mark_resolved (symbolS *s)
49309057
ILT
2817{
2818 if (LOCAL_SYMBOL_CHECK (s))
2819 {
2820 local_symbol_mark_resolved ((struct local_symbol *) s);
2821 return;
2822 }
158184ac 2823 s->sy_flags.sy_resolved = 1;
49309057
ILT
2824}
2825
2826/* Return whether a symbol has been resolved. */
2827
2828int
74937d39 2829symbol_resolved_p (symbolS *s)
49309057
ILT
2830{
2831 if (LOCAL_SYMBOL_CHECK (s))
2832 return local_symbol_resolved_p ((struct local_symbol *) s);
158184ac 2833 return s->sy_flags.sy_resolved;
49309057
ILT
2834}
2835
2836/* Return whether a symbol is a section symbol. */
2837
2838int
74937d39 2839symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
49309057
ILT
2840{
2841 if (LOCAL_SYMBOL_CHECK (s))
2842 return 0;
49309057 2843 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
49309057
ILT
2844}
2845
2846/* Return whether a symbol is equated to another symbol. */
2847
2848int
74937d39 2849symbol_equated_p (symbolS *s)
49309057
ILT
2850{
2851 if (LOCAL_SYMBOL_CHECK (s))
2852 return 0;
2853 return s->sy_value.X_op == O_symbol;
2854}
2855
e0890092
AM
2856/* Return whether a symbol is equated to another symbol, and should be
2857 treated specially when writing out relocs. */
2858
2859int
74937d39 2860symbol_equated_reloc_p (symbolS *s)
e0890092
AM
2861{
2862 if (LOCAL_SYMBOL_CHECK (s))
2863 return 0;
2864 /* X_op_symbol, normally not used for O_symbol, is set by
2865 resolve_symbol_value to flag expression syms that have been
2866 equated. */
2867 return (s->sy_value.X_op == O_symbol
7be1c489 2868#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
2869 && ! S_IS_WEAK (s)
2870#endif
158184ac 2871 && ((s->sy_flags.sy_resolved && s->sy_value.X_op_symbol != NULL)
e0890092
AM
2872 || ! S_IS_DEFINED (s)
2873 || S_IS_COMMON (s)));
2874}
2875
49309057
ILT
2876/* Return whether a symbol has a constant value. */
2877
2878int
74937d39 2879symbol_constant_p (symbolS *s)
49309057
ILT
2880{
2881 if (LOCAL_SYMBOL_CHECK (s))
2882 return 1;
2883 return s->sy_value.X_op == O_constant;
2884}
2885
bdf128d6
JB
2886/* Return whether a symbol was cloned and thus removed from the global
2887 symbol list. */
2888
2889int
2890symbol_shadow_p (symbolS *s)
2891{
2892 if (LOCAL_SYMBOL_CHECK (s))
2893 return 0;
2894 return s->sy_next == s;
2895}
2896
8d1015a8
AM
2897/* If S was created as a struct symbol, return S, otherwise if S is a
2898 converted local_symbol return the converted symbol, otherwise
2899 return NULL. */
2900
2901symbolS *
2902symbol_symbolS (symbolS *s)
2903{
2904 if (LOCAL_SYMBOL_CHECK (s))
2905 return NULL;
2906 return s;
2907}
2908
49309057
ILT
2909/* Return the BFD symbol for a symbol. */
2910
2911asymbol *
74937d39 2912symbol_get_bfdsym (symbolS *s)
49309057
ILT
2913{
2914 if (LOCAL_SYMBOL_CHECK (s))
2915 s = local_symbol_convert ((struct local_symbol *) s);
2916 return s->bsym;
2917}
2918
2919/* Set the BFD symbol for a symbol. */
2920
2921void
74937d39 2922symbol_set_bfdsym (symbolS *s, asymbol *bsym)
49309057
ILT
2923{
2924 if (LOCAL_SYMBOL_CHECK (s))
2925 s = local_symbol_convert ((struct local_symbol *) s);
22fe14ad
NC
2926 /* Usually, it is harmless to reset a symbol to a BFD section
2927 symbol. For example, obj_elf_change_section sets the BFD symbol
2928 of an old symbol with the newly created section symbol. But when
2929 we have multiple sections with the same name, the newly created
2930 section may have the same name as an old section. We check if the
2931 old symbol has been already marked as a section symbol before
2932 resetting it. */
2933 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2934 s->bsym = bsym;
2935 /* else XXX - What do we do now ? */
49309057
ILT
2936}
2937
49309057
ILT
2938#ifdef OBJ_SYMFIELD_TYPE
2939
2940/* Get a pointer to the object format information for a symbol. */
2941
2942OBJ_SYMFIELD_TYPE *
74937d39 2943symbol_get_obj (symbolS *s)
49309057
ILT
2944{
2945 if (LOCAL_SYMBOL_CHECK (s))
2946 s = local_symbol_convert ((struct local_symbol *) s);
2947 return &s->sy_obj;
2948}
2949
2950/* Set the object format information for a symbol. */
2951
2952void
74937d39 2953symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
49309057
ILT
2954{
2955 if (LOCAL_SYMBOL_CHECK (s))
2956 s = local_symbol_convert ((struct local_symbol *) s);
2957 s->sy_obj = *o;
2958}
2959
2960#endif /* OBJ_SYMFIELD_TYPE */
2961
2962#ifdef TC_SYMFIELD_TYPE
2963
2964/* Get a pointer to the processor information for a symbol. */
2965
2966TC_SYMFIELD_TYPE *
74937d39 2967symbol_get_tc (symbolS *s)
49309057
ILT
2968{
2969 if (LOCAL_SYMBOL_CHECK (s))
2970 s = local_symbol_convert ((struct local_symbol *) s);
2971 return &s->sy_tc;
2972}
2973
2974/* Set the processor information for a symbol. */
2975
2976void
74937d39 2977symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
49309057
ILT
2978{
2979 if (LOCAL_SYMBOL_CHECK (s))
2980 s = local_symbol_convert ((struct local_symbol *) s);
2981 s->sy_tc = *o;
2982}
2983
2984#endif /* TC_SYMFIELD_TYPE */
2985
252b5132 2986void
74937d39 2987symbol_begin (void)
252b5132
RH
2988{
2989 symbol_lastP = NULL;
7c743825 2990 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
252b5132 2991 sy_hash = hash_new ();
49309057 2992 local_hash = hash_new ();
252b5132
RH
2993
2994 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
252b5132 2995#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
45dfa85a 2996 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
252b5132
RH
2997#endif
2998 abs_symbol.sy_value.X_op = O_constant;
2999 abs_symbol.sy_frag = &zero_address_frag;
3000
3001 if (LOCAL_LABELS_FB)
3002 fb_label_init ();
3003}
4a826962
MR
3004
3005void
3006dot_symbol_init (void)
3007{
3008 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3009 if (dot_symbol.bsym == NULL)
3010 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3011 dot_symbol.bsym->name = ".";
158184ac 3012 dot_symbol.sy_flags.sy_forward_ref = 1;
4a826962
MR
3013 dot_symbol.sy_value.X_op = O_constant;
3014}
252b5132
RH
3015\f
3016int indent_level;
3017
3018/* Maximum indent level.
3019 Available for modification inside a gdb session. */
87c245cc 3020static int max_indent_level = 8;
252b5132 3021
252b5132 3022void
74937d39 3023print_symbol_value_1 (FILE *file, symbolS *sym)
252b5132
RH
3024{
3025 const char *name = S_GET_NAME (sym);
3026 if (!name || !name[0])
3027 name = "(unnamed)";
d2df793a
NC
3028 fprintf (file, "sym ");
3029 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
3030 fprintf (file, " %s", name);
49309057
ILT
3031
3032 if (LOCAL_SYMBOL_CHECK (sym))
3033 {
3034 struct local_symbol *locsym = (struct local_symbol *) sym;
d2df793a
NC
3035
3036 if (local_symbol_get_frag (locsym) != & zero_address_frag
49309057 3037 && local_symbol_get_frag (locsym) != NULL)
d2df793a
NC
3038 {
3039 fprintf (file, " frag ");
3040 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
3041 }
49309057
ILT
3042 if (local_symbol_resolved_p (locsym))
3043 fprintf (file, " resolved");
3044 fprintf (file, " local");
3045 }
3046 else
3047 {
3048 if (sym->sy_frag != &zero_address_frag)
d2df793a
NC
3049 {
3050 fprintf (file, " frag ");
3051 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
3052 }
158184ac 3053 if (sym->sy_flags.sy_written)
49309057 3054 fprintf (file, " written");
158184ac 3055 if (sym->sy_flags.sy_resolved)
49309057 3056 fprintf (file, " resolved");
158184ac 3057 else if (sym->sy_flags.sy_resolving)
49309057 3058 fprintf (file, " resolving");
158184ac 3059 if (sym->sy_flags.sy_used_in_reloc)
49309057 3060 fprintf (file, " used-in-reloc");
158184ac 3061 if (sym->sy_flags.sy_used)
49309057
ILT
3062 fprintf (file, " used");
3063 if (S_IS_LOCAL (sym))
3064 fprintf (file, " local");
e97b3f28 3065 if (S_IS_EXTERNAL (sym))
49309057 3066 fprintf (file, " extern");
06e77878
AO
3067 if (S_IS_WEAK (sym))
3068 fprintf (file, " weak");
49309057
ILT
3069 if (S_IS_DEBUG (sym))
3070 fprintf (file, " debug");
3071 if (S_IS_DEFINED (sym))
3072 fprintf (file, " defined");
3073 }
06e77878
AO
3074 if (S_IS_WEAKREFR (sym))
3075 fprintf (file, " weakrefr");
3076 if (S_IS_WEAKREFD (sym))
3077 fprintf (file, " weakrefd");
252b5132 3078 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
49309057 3079 if (symbol_resolved_p (sym))
252b5132
RH
3080 {
3081 segT s = S_GET_SEGMENT (sym);
3082
3083 if (s != undefined_section
411863a4 3084 && s != expr_section)
0af1713e 3085 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
252b5132
RH
3086 }
3087 else if (indent_level < max_indent_level
3088 && S_GET_SEGMENT (sym) != undefined_section)
3089 {
3090 indent_level++;
3091 fprintf (file, "\n%*s<", indent_level * 4, "");
49309057
ILT
3092 if (LOCAL_SYMBOL_CHECK (sym))
3093 fprintf (file, "constant %lx",
0af1713e 3094 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
49309057
ILT
3095 else
3096 print_expr_1 (file, &sym->sy_value);
252b5132
RH
3097 fprintf (file, ">");
3098 indent_level--;
3099 }
3100 fflush (file);
3101}
3102
3103void
74937d39 3104print_symbol_value (symbolS *sym)
252b5132
RH
3105{
3106 indent_level = 0;
3107 print_symbol_value_1 (stderr, sym);
3108 fprintf (stderr, "\n");
3109}
3110
3111static void
74937d39 3112print_binary (FILE *file, const char *name, expressionS *exp)
252b5132
RH
3113{
3114 indent_level++;
3115 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3116 print_symbol_value_1 (file, exp->X_add_symbol);
3117 fprintf (file, ">\n%*s<", indent_level * 4, "");
3118 print_symbol_value_1 (file, exp->X_op_symbol);
3119 fprintf (file, ">");
3120 indent_level--;
3121}
3122
3123void
74937d39 3124print_expr_1 (FILE *file, expressionS *exp)
252b5132 3125{
d2df793a
NC
3126 fprintf (file, "expr ");
3127 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
3128 fprintf (file, " ");
252b5132
RH
3129 switch (exp->X_op)
3130 {
3131 case O_illegal:
3132 fprintf (file, "illegal");
3133 break;
3134 case O_absent:
3135 fprintf (file, "absent");
3136 break;
3137 case O_constant:
0af1713e 3138 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
252b5132
RH
3139 break;
3140 case O_symbol:
3141 indent_level++;
3142 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3143 print_symbol_value_1 (file, exp->X_add_symbol);
3144 fprintf (file, ">");
3145 maybe_print_addnum:
3146 if (exp->X_add_number)
3147 fprintf (file, "\n%*s%lx", indent_level * 4, "",
0af1713e 3148 (unsigned long) exp->X_add_number);
252b5132
RH
3149 indent_level--;
3150 break;
3151 case O_register:
3152 fprintf (file, "register #%d", (int) exp->X_add_number);
3153 break;
3154 case O_big:
3155 fprintf (file, "big");
3156 break;
3157 case O_uminus:
3158 fprintf (file, "uminus -<");
3159 indent_level++;
3160 print_symbol_value_1 (file, exp->X_add_symbol);
3161 fprintf (file, ">");
3162 goto maybe_print_addnum;
3163 case O_bit_not:
3164 fprintf (file, "bit_not");
3165 break;
3166 case O_multiply:
3167 print_binary (file, "multiply", exp);
3168 break;
3169 case O_divide:
3170 print_binary (file, "divide", exp);
3171 break;
3172 case O_modulus:
3173 print_binary (file, "modulus", exp);
3174 break;
3175 case O_left_shift:
3176 print_binary (file, "lshift", exp);
3177 break;
3178 case O_right_shift:
3179 print_binary (file, "rshift", exp);
3180 break;
3181 case O_bit_inclusive_or:
3182 print_binary (file, "bit_ior", exp);
3183 break;
3184 case O_bit_exclusive_or:
3185 print_binary (file, "bit_xor", exp);
3186 break;
3187 case O_bit_and:
3188 print_binary (file, "bit_and", exp);
3189 break;
3190 case O_eq:
3191 print_binary (file, "eq", exp);
3192 break;
3193 case O_ne:
3194 print_binary (file, "ne", exp);
3195 break;
3196 case O_lt:
3197 print_binary (file, "lt", exp);
3198 break;
3199 case O_le:
3200 print_binary (file, "le", exp);
3201 break;
3202 case O_ge:
3203 print_binary (file, "ge", exp);
3204 break;
3205 case O_gt:
3206 print_binary (file, "gt", exp);
3207 break;
3208 case O_logical_and:
3209 print_binary (file, "logical_and", exp);
3210 break;
3211 case O_logical_or:
3212 print_binary (file, "logical_or", exp);
3213 break;
3214 case O_add:
3215 indent_level++;
3216 fprintf (file, "add\n%*s<", indent_level * 4, "");
3217 print_symbol_value_1 (file, exp->X_add_symbol);
3218 fprintf (file, ">\n%*s<", indent_level * 4, "");
3219 print_symbol_value_1 (file, exp->X_op_symbol);
3220 fprintf (file, ">");
3221 goto maybe_print_addnum;
3222 case O_subtract:
3223 indent_level++;
3224 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3225 print_symbol_value_1 (file, exp->X_add_symbol);
3226 fprintf (file, ">\n%*s<", indent_level * 4, "");
3227 print_symbol_value_1 (file, exp->X_op_symbol);
3228 fprintf (file, ">");
3229 goto maybe_print_addnum;
3230 default:
3231 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3232 break;
3233 }
3234 fflush (stdout);
3235}
3236
3237void
74937d39 3238print_expr (expressionS *exp)
252b5132
RH
3239{
3240 print_expr_1 (stderr, exp);
3241 fprintf (stderr, "\n");
3242}
3243
3244void
74937d39 3245symbol_print_statistics (FILE *file)
252b5132
RH
3246{
3247 hash_print_statistics (file, "symbol table", sy_hash);
49309057
ILT
3248 hash_print_statistics (file, "mini local symbol table", local_hash);
3249 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3250 local_symbol_count, local_symbol_conversion_count);
252b5132 3251}
280d71bf
DB
3252
3253#ifdef OBJ_COMPLEX_RELC
3254
3255/* Convert given symbol to a new complex-relocation symbol name. This
6f12865c 3256 may be a recursive function, since it might be called for non-leaf
280d71bf 3257 nodes (plain symbols) in the expression tree. The caller owns the
6f12865c
AM
3258 returning string, so should free it eventually. Errors are
3259 indicated via as_bad and a NULL return value. The given symbol
280d71bf
DB
3260 is marked with sy_used_in_reloc. */
3261
3262char *
3263symbol_relc_make_sym (symbolS * sym)
3264{
3265 char * terminal = NULL;
3266 const char * sname;
3267 char typetag;
3268 int sname_len;
3269
9c2799c2 3270 gas_assert (sym != NULL);
280d71bf
DB
3271
3272 /* Recurse to symbol_relc_make_expr if this symbol
3273 is defined as an expression or a plain value. */
3274 if ( S_GET_SEGMENT (sym) == expr_section
3275 || S_GET_SEGMENT (sym) == absolute_section)
be0d3bbb 3276 return symbol_relc_make_expr (symbol_get_value_expression (sym));
280d71bf 3277
2469b3c5 3278 /* This may be a "fake symbol", referring to ".".
280d71bf
DB
3279 Write out a special null symbol to refer to this position. */
3280 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3281 return xstrdup (".");
3282
3283 /* We hope this is a plain leaf symbol. Construct the encoding
3284 as {S,s}II...:CCCCCCC....
3285 where 'S'/'s' means section symbol / plain symbol
3286 III is decimal for the symbol name length
3287 CCC is the symbol name itself. */
3288 symbol_mark_used_in_reloc (sym);
3289
3290 sname = S_GET_NAME (sym);
3291 sname_len = strlen (sname);
3292 typetag = symbol_section_p (sym) ? 'S' : 's';
3293
add39d23
TS
3294 terminal = XNEWVEC (char, (1 /* S or s */
3295 + 8 /* sname_len in decimal */
3296 + 1 /* _ spacer */
3297 + sname_len /* name itself */
3298 + 1 /* \0 */ ));
280d71bf
DB
3299
3300 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3301 return terminal;
3302}
3303
3304/* Convert given value to a new complex-relocation symbol name. This
3305 is a non-recursive function, since it is be called for leaf nodes
3306 (plain values) in the expression tree. The caller owns the
3307 returning string, so should free() it eventually. No errors. */
3308
3309char *
3310symbol_relc_make_value (offsetT val)
3311{
325801bd 3312 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
280d71bf
DB
3313
3314 terminal[0] = '#';
1a412f5f 3315 bfd_sprintf_vma (stdoutput, terminal + 1, val);
280d71bf
DB
3316 return terminal;
3317}
3318
3319/* Convert given expression to a new complex-relocation symbol name.
3320 This is a recursive function, since it traverses the entire given
3321 expression tree. The caller owns the returning string, so should
3322 free() it eventually. Errors are indicated via as_bad() and a NULL
3323 return value. */
3324
3325char *
3326symbol_relc_make_expr (expressionS * exp)
3327{
b9bb4a93 3328 const char * opstr = NULL; /* Operator prefix string. */
280d71bf
DB
3329 int arity = 0; /* Arity of this operator. */
3330 char * operands[3]; /* Up to three operands. */
3331 char * concat_string = NULL;
3332
3333 operands[0] = operands[1] = operands[2] = NULL;
3334
9c2799c2 3335 gas_assert (exp != NULL);
280d71bf
DB
3336
3337 /* Match known operators -> fill in opstr, arity, operands[] and fall
34bca508 3338 through to construct subexpression fragments; may instead return
280d71bf
DB
3339 string directly for leaf nodes. */
3340
34bca508 3341 /* See expr.h for the meaning of all these enums. Many operators
280d71bf
DB
3342 have an unnatural arity (X_add_number implicitly added). The
3343 conversion logic expands them to explicit "+" subexpressions. */
3344
3345 switch (exp->X_op)
3346 {
3347 default:
3348 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3349 break;
3350
3351 /* Leaf nodes. */
3352 case O_constant:
3353 return symbol_relc_make_value (exp->X_add_number);
3354
3355 case O_symbol:
34bca508
L
3356 if (exp->X_add_number)
3357 {
3358 arity = 2;
3359 opstr = "+";
280d71bf
DB
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 return symbol_relc_make_sym (exp->X_add_symbol);
3366
3367 /* Helper macros for nesting nodes. */
3368
3369#define HANDLE_XADD_OPT1(str_) \
3370 if (exp->X_add_number) \
3371 { \
3372 arity = 2; \
3373 opstr = "+:" str_; \
3374 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3375 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3376 break; \
3377 } \
3378 else \
3379 { \
3380 arity = 1; \
3381 opstr = str_; \
3382 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3383 } \
3384 break
34bca508 3385
280d71bf
DB
3386#define HANDLE_XADD_OPT2(str_) \
3387 if (exp->X_add_number) \
3388 { \
3389 arity = 3; \
3390 opstr = "+:" str_; \
3391 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3392 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3393 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3394 } \
3395 else \
3396 { \
3397 arity = 2; \
3398 opstr = str_; \
3399 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3400 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3401 } \
3402 break
3403
3404 /* Nesting nodes. */
3405
3406 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3407 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3408 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3409 case O_multiply: HANDLE_XADD_OPT2 ("*");
3410 case O_divide: HANDLE_XADD_OPT2 ("/");
3411 case O_modulus: HANDLE_XADD_OPT2 ("%");
3412 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3413 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3414 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3415 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3416 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3417 case O_add: HANDLE_XADD_OPT2 ("+");
3418 case O_subtract: HANDLE_XADD_OPT2 ("-");
3419 case O_eq: HANDLE_XADD_OPT2 ("==");
3420 case O_ne: HANDLE_XADD_OPT2 ("!=");
3421 case O_lt: HANDLE_XADD_OPT2 ("<");
3422 case O_le: HANDLE_XADD_OPT2 ("<=");
3423 case O_ge: HANDLE_XADD_OPT2 (">=");
3424 case O_gt: HANDLE_XADD_OPT2 (">");
3425 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3426 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3427 }
3428
3429 /* Validate & reject early. */
3430 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3431 opstr = NULL;
3432 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3433 opstr = NULL;
3434 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3435 opstr = NULL;
3436
3437 if (opstr == NULL)
3438 concat_string = NULL;
29a2809e
TS
3439 else if (arity == 0)
3440 concat_string = xstrdup (opstr);
3441 else if (arity == 1)
3442 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3443 else if (arity == 2)
3444 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3445 (char *) NULL);
280d71bf 3446 else
29a2809e
TS
3447 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3448 operands[2], (char *) NULL);
280d71bf
DB
3449
3450 /* Free operand strings (not opstr). */
3451 if (arity >= 1) xfree (operands[0]);
3452 if (arity >= 2) xfree (operands[1]);
3453 if (arity >= 3) xfree (operands[2]);
3454
3455 return concat_string;
3456}
3457
3458#endif
This page took 1.132758 seconds and 4 git commands to generate.