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