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