Coverage Report

Created: 2025-07-08 11:15

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