Coverage Report

Created: 2026-07-12 09:22

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