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