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