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