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