Coverage Report

Created: 2026-05-11 07:54

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
24.1k
{
185
24.1k
  symbol_entry_t *entry = (symbol_entry_t *) e;
186
24.1k
  if (entry->sy.hash == 0)
187
6.55k
    entry->sy.hash = htab_hash_string (entry->sy.name);
188
189
24.1k
  return entry->sy.hash;
190
24.1k
}
191
192
/* Equality function for a symbol_entry.  */
193
194
static int
195
eq_symbol_entry (const void *a, const void *b)
196
128k
{
197
128k
  const symbol_entry_t *ea = a;
198
128k
  const symbol_entry_t *eb = b;
199
200
128k
  return (ea->sy.hash == eb->sy.hash
201
127k
    && strcmp (ea->sy.name, eb->sy.name) == 0);
202
128k
}
203
204
static void *
205
symbol_entry_find (htab_t table, const char *name)
206
120k
{
207
120k
  hashval_t hash = htab_hash_string (name);
208
120k
  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209
120k
            hash, name, 0, 0, 0 } };
210
120k
  return htab_find_with_hash (table, &needle, hash);
211
120k
}
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
24.9k
#define debug_verify_symchain(root, last) ((void) 0)
236
#endif
237
238
9
#define DOLLAR_LABEL_CHAR '\001'
239
1.53k
#define LOCAL_LABEL_CHAR  '\002'
240
241
#ifndef TC_LABEL_IS_LOCAL
242
9
#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
1.66M
{
254
1.66M
  return obstack_alloc (&notes, size);
255
1.66M
}
256
257
void *
258
notes_calloc (size_t n, size_t size)
259
1.55M
{
260
1.55M
  size_t amt;
261
1.55M
  void *ret;
262
1.55M
  if (gas_mul_overflow (n, size, &amt))
263
0
    {
264
0
      obstack_alloc_failed_handler ();
265
0
      abort ();
266
0
    }
267
1.55M
  ret = notes_alloc (amt);
268
1.55M
  memset (ret, 0, amt);
269
1.55M
  return ret;
270
1.55M
}
271
272
void *
273
notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
274
50.8k
{
275
50.8k
  void *ret = obstack_alloc (&notes, alloc_size);
276
50.8k
  memcpy (ret, src, copy_size);
277
50.8k
  if (alloc_size > copy_size)
278
131
    memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279
50.8k
  return ret;
280
50.8k
}
281
282
char *
283
notes_strdup (const char *str)
284
50.7k
{
285
50.7k
  size_t len = strlen (str) + 1;
286
50.7k
  return notes_memdup (str, len, len);
287
50.7k
}
288
289
char *
290
notes_concat (const char *first, ...)
291
0
{
292
0
  va_list args;
293
0
  const char *str;
294
295
0
  va_start (args, first);
296
0
  for (str = first; str; str = va_arg (args, const char *))
297
0
    {
298
0
      size_t size = strlen (str);
299
0
      obstack_grow (&notes, str, size);
300
0
    }
301
0
  va_end (args);
302
0
  obstack_1grow (&notes, 0);
303
0
  return obstack_finish (&notes);
304
0
}
305
306
/* Use with caution!  Frees PTR and all more recently allocated memory
307
   on the notes obstack.  */
308
309
void
310
notes_free (void *ptr)
311
0
{
312
0
  obstack_free (&notes, ptr);
313
0
}
314
315
#ifdef TE_PE
316
/* The name of an external symbol which is
317
   used to make weak PE symbol names unique.  */
318
const char * an_external_name;
319
#endif
320
321
/* Return a pointer to a new symbol.  Die if we can't make a new
322
   symbol.  Fill in the symbol's values.  Add symbol to end of symbol
323
   chain.
324
325
   This function should be called in the general case of creating a
326
   symbol.  However, if the output file symbol table has already been
327
   set, and you are certain that this symbol won't be wanted in the
328
   output file, you can call symbol_create.  */
329
330
symbolS *
331
symbol_new (const char *name, segT segment, fragS *frag, valueT valu)
332
7.76k
{
333
7.76k
  symbolS *symbolP = symbol_create (name, segment, frag, valu);
334
335
  /* Link to end of symbol chain.  */
336
7.76k
  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
337
338
7.76k
  return symbolP;
339
7.76k
}
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
50.6k
{
347
50.6k
  char *ret;
348
349
50.6k
  gas_assert (name != NULL);
350
50.6k
  ret = notes_strdup (name);
351
352
#ifdef tc_canonicalize_symbol_name
353
  ret = tc_canonicalize_symbol_name (ret);
354
#endif
355
356
50.6k
  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
50.6k
  return ret;
365
50.6k
}
366
367
static void
368
symbol_init (symbolS *symbolP, const char *name, asection *sec,
369
       fragS *frag, valueT valu)
370
48.8k
{
371
48.8k
  symbolP->frag = frag;
372
48.8k
  symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373
48.8k
  if (symbolP->bsym == NULL)
374
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375
48.8k
  symbolP->bsym->name = name;
376
48.8k
  symbolP->bsym->section = sec;
377
378
48.8k
  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
48.8k
  S_SET_VALUE (symbolP, valu);
391
48.8k
  if (sec == reg_section)
392
909
    symbolP->x->value.X_op = O_register;
393
394
48.8k
  symbol_clear_list_pointers (symbolP);
395
396
48.8k
  obj_symbol_new_hook (symbolP);
397
398
#ifdef tc_symbol_new_hook
399
  tc_symbol_new_hook (symbolP);
400
#endif
401
48.8k
}
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
48.7k
{
408
48.7k
  const char *preserved_copy_of_name;
409
48.7k
  symbolS *symbolP;
410
48.7k
  size_t size;
411
412
48.7k
  preserved_copy_of_name = save_symbol_name (name);
413
414
48.7k
  size = sizeof (symbolS) + sizeof (struct xsymbol);
415
48.7k
  symbolP = notes_alloc (size);
416
417
  /* symbol must be born in some fixed state.  This seems as good as any.  */
418
48.7k
  memset (symbolP, 0, size);
419
48.7k
  symbolP->name = preserved_copy_of_name;
420
48.7k
  symbolP->x = (struct xsymbol *) (symbolP + 1);
421
422
48.7k
  symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
423
424
48.7k
  return symbolP;
425
48.7k
}
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
1.88k
{
441
1.88k
  const char *name_copy;
442
1.88k
  struct local_symbol *ret;
443
1.88k
  struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
444
445
1.88k
  ++local_symbol_count;
446
447
1.88k
  name_copy = save_symbol_name (name);
448
449
1.88k
  ret = notes_alloc (sizeof *ret);
450
1.88k
  ret->flags = flags;
451
1.88k
  ret->hash = 0;
452
1.88k
  ret->name = name_copy;
453
1.88k
  ret->frag = frag;
454
1.88k
  ret->section = section;
455
1.88k
  ret->value = val;
456
457
1.88k
  htab_insert (sy_hash, ret, 1);
458
459
1.88k
  return ret;
460
1.88k
}
461
462
/* Convert a local symbol into a real symbol.  */
463
464
static symbolS *
465
local_symbol_convert (void *sym)
466
46
{
467
46
  symbol_entry_t *ent = sym;
468
46
  struct xsymbol *xtra;
469
46
  valueT val;
470
471
46
  gas_assert (ent->lsy.flags.local_symbol);
472
473
46
  ++local_symbol_conversion_count;
474
475
46
  xtra = notes_alloc (sizeof (*xtra));
476
46
  memset (xtra, 0, sizeof (*xtra));
477
46
  val = ent->lsy.value;
478
46
  ent->sy.x = xtra;
479
480
  /* Local symbols are always either defined or used.  */
481
46
  ent->sy.flags.used = 1;
482
46
  ent->sy.flags.local_symbol = 0;
483
484
46
  symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
485
46
  symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
486
487
46
  return &ent->sy;
488
46
}
489

490
static void
491
define_sym_at_dot (symbolS *symbolP)
492
1.59k
{
493
1.59k
  symbolP->frag = frag_now;
494
1.59k
  S_SET_VALUE (symbolP, frag_now_fix ());
495
1.59k
  S_SET_SEGMENT (symbolP, now_seg);
496
1.59k
}
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
6.61k
{
508
6.61k
  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
6.61k
  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
6.61k
  if ((symbolP = symbol_find (sym_name)) != 0)
558
5.26k
    {
559
5.26k
      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
5.26k
      if (symbolP->flags.local_symbol)
566
153
  {
567
153
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
568
569
153
    if (locsym->section != undefined_section
570
4
        && (locsym->frag != frag_now
571
3
      || locsym->section != now_seg
572
3
      || locsym->value != frag_now_fix ()))
573
1
      {
574
1
        as_bad (_("symbol `%s' is already defined"), sym_name);
575
1
        return symbolP;
576
1
      }
577
578
152
    locsym->section = now_seg;
579
152
    locsym->frag = frag_now;
580
152
    locsym->value = frag_now_fix ();
581
152
  }
582
5.11k
      else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
583
5.11k
         || S_IS_COMMON (symbolP)
584
5.11k
         || S_IS_VOLATILE (symbolP))
585
3
  {
586
3
    if (S_IS_VOLATILE (symbolP))
587
1
      {
588
1
        symbolP = symbol_clone (symbolP, 1);
589
1
        S_SET_VALUE (symbolP, 0);
590
1
        S_CLEAR_VOLATILE (symbolP);
591
1
      }
592
3
    if (S_GET_VALUE (symbolP) == 0)
593
3
      {
594
3
        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
3
      }
600
0
    else
601
0
      {
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
0
        if (((!S_IS_DEBUG (symbolP)
614
0
        && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
615
0
        && S_IS_EXTERNAL (symbolP))
616
0
       || S_GET_SEGMENT (symbolP) == bss_section)
617
0
      && (now_seg == data_section
618
0
          || now_seg == bss_section
619
0
          || now_seg == S_GET_SEGMENT (symbolP)))
620
0
    {
621
      /* Select which of the 2 cases this is.  */
622
0
      if (now_seg != data_section)
623
0
        {
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
0
          if (S_GET_VALUE (symbolP) < frag_now_fix ())
630
0
      S_SET_VALUE (symbolP, frag_now_fix ());
631
0
        }
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
0
    }
639
0
        else
640
0
    {
641
0
#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
642
0
      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
0
      as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
652
0
          sym_name,
653
0
          segment_name (S_GET_SEGMENT (symbolP)),
654
0
          od_buf,
655
0
          (long) S_GET_VALUE (symbolP));
656
0
    }
657
0
      }      /* if the undefined symbol has no value  */
658
3
  }
659
5.11k
      else
660
5.11k
  {
661
    /* Don't blow up if the definition is the same.  */
662
5.11k
    if (!(frag_now == symbolP->frag
663
3.97k
    && S_GET_VALUE (symbolP) == frag_now_fix ()
664
3.52k
    && S_GET_SEGMENT (symbolP) == now_seg))
665
1.58k
      {
666
1.58k
        as_bad (_("symbol `%s' is already defined"), sym_name);
667
1.58k
        symbolP = symbol_clone (symbolP, 0);
668
1.58k
        define_sym_at_dot (symbolP);
669
1.58k
      }
670
5.11k
  }
671
672
5.26k
    }
673
1.34k
  else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
674
1.18k
    {
675
1.18k
      symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
676
1.18k
                 frag_now_fix ());
677
1.18k
    }
678
158
  else
679
158
    {
680
158
      symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
681
682
158
      symbol_table_insert (symbolP);
683
158
    }
684
685
6.60k
  if (mri_common_symbol != NULL)
686
23
    {
687
      /* This symbol is actually being defined within an MRI common
688
   section.  This requires special handling.  */
689
23
      if (symbolP->flags.local_symbol)
690
18
  symbolP = local_symbol_convert (symbolP);
691
23
      symbolP->x->value.X_op = O_symbol;
692
23
      symbolP->x->value.X_add_symbol = mri_common_symbol;
693
23
      symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
694
23
      symbolP->frag = &zero_address_frag;
695
23
      S_SET_SEGMENT (symbolP, expr_section);
696
23
      symbolP->flags.mri_common = 1;
697
23
    }
698
699
#ifdef tc_frob_label
700
  tc_frob_label (symbolP);
701
#endif
702
6.60k
#ifdef obj_frob_label
703
6.60k
  obj_frob_label (symbolP);
704
6.60k
#endif
705
6.60k
  if (flag_synth_cfi)
706
0
    ginsn_frob_label (symbolP);
707
708
6.60k
  return symbolP;
709
6.61k
}
710

711
/* Die if we can't insert the symbol.  */
712
713
void
714
symbol_table_insert (symbolS *symbolP)
715
22.2k
{
716
22.2k
  know (symbolP);
717
718
22.2k
  htab_insert (sy_hash, symbolP, 1);
719
22.2k
}
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
76.4k
{
727
76.4k
  symbolS *symbolP;
728
729
76.4k
  symbolP = symbol_find (name);
730
731
76.4k
  if (symbolP == NULL)
732
2.19k
    {
733
2.19k
      if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
734
219
  {
735
219
    symbolP = md_undefined_symbol ((char *) name);
736
219
    if (symbolP != NULL)
737
0
      return symbolP;
738
739
219
    symbolP = (symbolS *) local_symbol_make (name, undefined_section,
740
219
               &zero_address_frag, 0);
741
219
    return symbolP;
742
219
  }
743
744
1.97k
      symbolP = symbol_make (name);
745
746
1.97k
      symbol_table_insert (symbolP);
747
1.97k
    }        /* if symbol wasn't found */
748
749
76.2k
  return symbolP;
750
76.4k
}
751
752
symbolS *
753
symbol_make (const char *name)
754
2.47k
{
755
2.47k
  symbolS *symbolP;
756
757
  /* Let the machine description default it, e.g. for register names.  */
758
2.47k
  symbolP = md_undefined_symbol ((char *) name);
759
760
2.47k
  if (!symbolP)
761
2.45k
    symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
762
763
2.47k
  return symbolP;
764
2.47k
}
765
766
symbolS *
767
symbol_clone (symbolS *orgsymP, int replace)
768
19.1k
{
769
19.1k
  symbolS *newsymP;
770
19.1k
  asymbol *bsymorg, *bsymnew;
771
772
  /* Make sure we never clone the dot special symbol.  */
773
19.1k
  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
19.1k
  if (orgsymP->flags.local_symbol)
780
0
    orgsymP = local_symbol_convert (orgsymP);
781
19.1k
  bsymorg = orgsymP->bsym;
782
783
19.1k
  newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
784
19.1k
  *newsymP = *orgsymP;
785
19.1k
  newsymP->x = (struct xsymbol *) (newsymP + 1);
786
19.1k
  *newsymP->x = *orgsymP->x;
787
19.1k
  bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
788
19.1k
  if (bsymnew == NULL)
789
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
790
19.1k
  newsymP->bsym = bsymnew;
791
19.1k
  bsymnew->name = bsymorg->name;
792
19.1k
  bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
793
19.1k
  bsymnew->section = bsymorg->section;
794
19.1k
  bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), &orgsymP->bsym,
795
19.1k
        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
19.1k
  if (replace)
806
17.5k
    {
807
17.5k
      if (orgsymP->x->previous != NULL)
808
17.5k
  orgsymP->x->previous->x->next = newsymP;
809
0
      else
810
0
  symbol_rootP = newsymP;
811
17.5k
      if (orgsymP->x->next != NULL)
812
16.9k
  orgsymP->x->next->x->previous = newsymP;
813
632
      else
814
632
  symbol_lastP = newsymP;
815
816
      /* Symbols that won't be output can't be external.  */
817
17.5k
      S_CLEAR_EXTERNAL (orgsymP);
818
17.5k
      orgsymP->x->previous = orgsymP->x->next = orgsymP;
819
17.5k
      debug_verify_symchain (symbol_rootP, symbol_lastP);
820
821
17.5k
      symbol_table_insert (newsymP);
822
17.5k
    }
823
1.60k
  else
824
1.60k
    {
825
      /* Symbols that won't be output can't be external.  */
826
1.60k
      S_CLEAR_EXTERNAL (newsymP);
827
1.60k
      newsymP->x->previous = newsymP->x->next = newsymP;
828
1.60k
    }
829
830
19.1k
  return newsymP;
831
19.1k
}
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
685k
{
841
685k
  if (symbolP
842
79.7k
      && !symbolP->flags.local_symbol
843
78.8k
      && !symbolP->flags.forward_resolved)
844
8.13k
    {
845
8.13k
      symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
846
8.13k
      symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
847
8.13k
      symbolS *add_symbol = orig_add_symbol;
848
8.13k
      symbolS *op_symbol = orig_op_symbol;
849
850
8.13k
      if (symbolP->flags.forward_ref)
851
12
  is_forward = 1;
852
853
8.13k
      if (is_forward)
854
12
  {
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
12
    if (add_symbol && S_IS_VOLATILE (add_symbol))
859
0
      add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
860
12
    if (op_symbol && S_IS_VOLATILE (op_symbol))
861
0
      op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
862
12
  }
863
864
      /* Re-using resolving here, as this routine cannot get called from
865
   symbol resolution code.  */
866
8.13k
      if ((symbolP->bsym->section == expr_section
867
3.22k
     || symbolP->flags.forward_ref)
868
4.92k
    && !symbolP->flags.resolving)
869
4.91k
  {
870
4.91k
    symbolP->flags.resolving = 1;
871
4.91k
    add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
872
4.91k
    op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
873
4.91k
    symbolP->flags.resolving = 0;
874
4.91k
  }
875
876
8.13k
      if (symbolP->flags.forward_ref
877
8.12k
    || add_symbol != orig_add_symbol
878
8.12k
    || op_symbol != orig_op_symbol)
879
12
  {
880
12
    if (symbolP != &dot_symbol)
881
12
      {
882
12
        symbolP = symbol_clone (symbolP, 0);
883
12
        symbolP->flags.resolving = 0;
884
12
      }
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
12
  }
893
894
8.13k
      symbolP->x->value.X_add_symbol = add_symbol;
895
8.13k
      symbolP->x->value.X_op_symbol = op_symbol;
896
8.13k
      symbolP->flags.forward_resolved = 1;
897
8.13k
    }
898
899
685k
  return symbolP;
900
685k
}
901
902
symbolS *
903
symbol_temp_new (segT seg, fragS *frag, valueT ofs)
904
3.31k
{
905
3.31k
  return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
906
3.31k
}
907
908
symbolS *
909
symbol_temp_new_now (void)
910
1.54k
{
911
1.54k
  return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
912
1.54k
}
913
914
symbolS *
915
symbol_temp_new_now_octets (void)
916
253
{
917
253
  return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
918
253
}
919
920
symbolS *
921
symbol_temp_make (void)
922
475
{
923
475
  return symbol_make (FAKE_LABEL_NAME);
924
475
}
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
0
{
934
0
  return symbol_find_exact_noref (name, 0);
935
0
}
936
937
symbolS *
938
symbol_find_exact_noref (const char *name, int noref)
939
120k
{
940
120k
  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
120k
  if (sym && ! noref)
948
109k
    S_CLEAR_WEAKREFD (sym);
949
950
120k
  return sym;
951
120k
}
952
953
symbolS *
954
symbol_find (const char *name)
955
120k
{
956
120k
  return symbol_find_noref (name, 0);
957
120k
}
958
959
symbolS *
960
symbol_find_noref (const char *name, int noref)
961
120k
{
962
120k
  symbolS * result;
963
120k
  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
120k
  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
120k
  result = symbol_find_exact_noref (name, noref);
992
120k
  free (copy);
993
120k
  return result;
994
120k
}
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
7.81k
{
1008
7.81k
  extern int symbol_table_frozen;
1009
7.81k
  if (symbol_table_frozen)
1010
0
    abort ();
1011
7.81k
  if (addme->flags.local_symbol)
1012
0
    abort ();
1013
7.81k
  if (target != NULL && target->flags.local_symbol)
1014
0
    abort ();
1015
1016
7.81k
  if (target == NULL)
1017
478
    {
1018
478
      know (*rootPP == NULL);
1019
478
      know (*lastPP == NULL);
1020
478
      addme->x->next = NULL;
1021
478
      addme->x->previous = NULL;
1022
478
      *rootPP = addme;
1023
478
      *lastPP = addme;
1024
478
      return;
1025
478
    }
1026
1027
7.33k
  if (target->x->next != NULL)
1028
0
    target->x->next->x->previous = addme;
1029
7.33k
  else
1030
7.33k
    *lastPP = addme;
1031
1032
7.33k
  addme->x->next = target->x->next;
1033
7.33k
  target->x->next = addme;
1034
7.33k
  addme->x->previous = target;
1035
1036
7.33k
  debug_verify_symchain (symbol_rootP, symbol_lastP);
1037
7.33k
}
1038
1039
/* Set the chain pointers of SYMBOL to null.  */
1040
1041
void
1042
symbol_clear_list_pointers (symbolS *symbolP)
1043
48.8k
{
1044
48.8k
  if (symbolP->flags.local_symbol)
1045
0
    abort ();
1046
48.8k
  symbolP->x->next = NULL;
1047
48.8k
  symbolP->x->previous = NULL;
1048
48.8k
}
1049
1050
/* Remove SYMBOLP from the list.  */
1051
1052
void
1053
symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1054
21
{
1055
21
  if (symbolP->flags.local_symbol)
1056
0
    abort ();
1057
1058
21
  if (symbolP->x->previous != NULL)
1059
21
    symbolP->x->previous->x->next = symbolP->x->next;
1060
0
  else
1061
0
    *rootPP = symbolP->x->next;
1062
1063
21
  if (symbolP->x->next != NULL)
1064
0
    symbolP->x->next->x->previous = symbolP->x->previous;
1065
21
  else
1066
21
    *lastPP = symbolP->x->previous;
1067
1068
21
  debug_verify_symchain (*rootPP, *lastPP);
1069
21
}
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
21
{
1077
21
  extern int symbol_table_frozen;
1078
21
  if (symbol_table_frozen)
1079
0
    abort ();
1080
21
  if (addme->flags.local_symbol)
1081
0
    abort ();
1082
21
  if (target->flags.local_symbol)
1083
0
    abort ();
1084
1085
21
  if (target->x->previous != NULL)
1086
0
    target->x->previous->x->next = addme;
1087
21
  else
1088
21
    *rootPP = addme;
1089
1090
21
  addme->x->previous = target->x->previous;
1091
21
  target->x->previous = addme;
1092
21
  addme->x->next = target;
1093
1094
21
  debug_verify_symchain (*rootPP, *lastPP);
1095
21
}
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
2.34k
{
1118
2.34k
  return (!s->flags.local_symbol
1119
2.34k
    && ((s->x->next != s
1120
2.34k
         && s->x->next != NULL
1121
4
         && s->x->next->x->previous == s)
1122
2.34k
        || s == lastPP)
1123
4
    && ((s->x->previous != s
1124
4
         && s->x->previous != NULL
1125
4
         && s->x->previous->x->next == s)
1126
0
        || s == rootPP));
1127
2.34k
}
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
64.6k
{
1253
64.6k
  int resolved;
1254
64.6k
  valueT final_val;
1255
64.6k
  segT final_seg;
1256
1257
64.6k
  if (symp->flags.local_symbol)
1258
899
    {
1259
899
      struct local_symbol *locsym = (struct local_symbol *) symp;
1260
1261
899
      final_val = locsym->value;
1262
899
      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
899
      if (locsym->section->flags & SEC_OCTETS)
1268
0
  final_val += locsym->frag->fr_address;
1269
899
      else
1270
899
  final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1271
1272
899
      if (finalize_syms)
1273
0
  {
1274
0
    locsym->value = final_val;
1275
0
    locsym->flags.resolved = 1;
1276
0
  }
1277
1278
899
      return final_val;
1279
899
    }
1280
1281
63.7k
  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
63.7k
  resolved = 0;
1305
63.7k
  final_seg = S_GET_SEGMENT (symp);
1306
1307
63.7k
  if (symp->flags.resolving)
1308
45
    {
1309
45
      if (finalize_syms)
1310
0
  as_bad (_("symbol definition loop encountered at `%s'"),
1311
0
    S_GET_NAME (symp));
1312
45
      final_val = 0;
1313
45
      resolved = 1;
1314
45
    }
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
63.6k
  else
1364
63.6k
    {
1365
63.6k
      symbolS *add_symbol, *op_symbol;
1366
63.6k
      offsetT left, right;
1367
63.6k
      segT seg_left, seg_right;
1368
63.6k
      operatorT op;
1369
63.6k
      int move_seg_ok;
1370
1371
63.6k
      symp->flags.resolving = 1;
1372
1373
      /* Help out with CSE.  */
1374
63.6k
      add_symbol = symp->x->value.X_add_symbol;
1375
63.6k
      op_symbol = symp->x->value.X_op_symbol;
1376
63.6k
      final_val = symp->x->value.X_add_number;
1377
63.6k
      op = symp->x->value.X_op;
1378
1379
63.6k
      switch (op)
1380
63.6k
  {
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
52.8k
  case O_constant:
1429
    /* Symbols whose section has SEC_ELF_OCTETS set,
1430
       resolve to octets instead of target bytes. */
1431
52.8k
    if (symp->bsym->section->flags & SEC_OCTETS)
1432
4
      final_val += symp->frag->fr_address;
1433
52.8k
    else
1434
52.8k
      final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1435
52.8k
    if (final_seg == expr_section)
1436
1
      final_seg = absolute_section;
1437
    /* Fall through.  */
1438
1439
52.8k
  case O_register:
1440
52.8k
    resolved = 1;
1441
52.8k
    break;
1442
1443
44
  case O_symbol:
1444
44
  case O_symbol_rva:
1445
44
  case O_secidx:
1446
44
    left = resolve_symbol_value (add_symbol);
1447
44
    seg_left = S_GET_SEGMENT (add_symbol);
1448
44
    if (finalize_syms)
1449
0
      symp->x->value.X_op_symbol = NULL;
1450
1451
70
  do_symbol:
1452
70
    if (S_IS_WEAKREFR (symp))
1453
34
      {
1454
34
        gas_assert (final_val == 0);
1455
34
        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
34
      }
1464
1465
70
    if (symp->flags.mri_common)
1466
2
      {
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
2
        resolved = symbol_resolved_p (add_symbol);
1471
2
        break;
1472
2
      }
1473
1474
    /* Don't leave symbol loops.  */
1475
68
    if (finalize_syms
1476
0
        && !add_symbol->flags.local_symbol
1477
0
        && add_symbol->flags.resolving)
1478
0
      break;
1479
1480
68
    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
68
        )
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
68
    if (seg_left == undefined_section
1504
43
        || bfd_is_com_section (seg_left)
1505
#if defined (OBJ_COFF) && defined (TE_PE)
1506
        || S_IS_WEAK (add_symbol)
1507
#endif
1508
43
        || (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
25
      {
1514
25
        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
25
        final_seg = seg_left;
1523
25
        final_val += symp->frag->fr_address + left;
1524
25
        resolved = symbol_resolved_p (add_symbol);
1525
25
        symp->flags.resolving = 0;
1526
1527
25
        if (op == O_secidx && seg_left != undefined_section)
1528
0
    {
1529
0
      final_val = 0;
1530
0
      break;
1531
0
    }
1532
1533
25
        goto exit_dont_set_value;
1534
25
      }
1535
43
    else
1536
43
      {
1537
43
        final_val += symp->frag->fr_address + left;
1538
43
        if (final_seg == expr_section || final_seg == undefined_section)
1539
3
    final_seg = seg_left;
1540
43
      }
1541
1542
43
    resolved = symbol_resolved_p (add_symbol);
1543
43
    if (S_IS_WEAKREFR (symp))
1544
34
      {
1545
34
        symp->flags.resolving = 0;
1546
34
        goto exit_dont_set_value;
1547
34
      }
1548
9
    break;
1549
1550
20
  case O_uminus:
1551
28
  case O_bit_not:
1552
28
  case O_logical_not:
1553
28
    left = resolve_symbol_value (add_symbol);
1554
28
    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
28
    if (op != O_logical_not && seg_left != absolute_section
1561
20
        && finalize_syms)
1562
0
      report_op_error (symp, NULL, op, add_symbol);
1563
1564
28
    if (final_seg == expr_section || final_seg == undefined_section)
1565
4
      final_seg = absolute_section;
1566
1567
28
    if (op == O_uminus)
1568
20
      left = -(valueT) left;
1569
8
    else if (op == O_logical_not)
1570
0
      left = !left;
1571
8
    else
1572
8
      left = ~left;
1573
1574
28
    final_val += left + symp->frag->fr_address;
1575
1576
28
    resolved = symbol_resolved_p (add_symbol);
1577
28
    break;
1578
1579
16
  case O_multiply:
1580
21
  case O_divide:
1581
4.43k
  case O_modulus:
1582
4.43k
  case O_left_shift:
1583
4.44k
  case O_right_shift:
1584
4.44k
  case O_bit_inclusive_or:
1585
6.20k
  case O_bit_or_not:
1586
6.20k
  case O_bit_exclusive_or:
1587
6.21k
  case O_bit_and:
1588
6.22k
  case O_add:
1589
10.7k
  case O_subtract:
1590
10.7k
  case O_eq:
1591
10.7k
  case O_ne:
1592
10.7k
  case O_lt:
1593
10.7k
  case O_le:
1594
10.7k
  case O_ge:
1595
10.7k
  case O_gt:
1596
10.7k
  case O_logical_and:
1597
10.7k
  case O_logical_or:
1598
10.7k
    left = resolve_symbol_value (add_symbol);
1599
10.7k
    right = resolve_symbol_value (op_symbol);
1600
10.7k
    seg_left = S_GET_SEGMENT (add_symbol);
1601
10.7k
    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
10.7k
    if (op == O_add)
1606
10
      {
1607
10
        if (seg_right == absolute_section)
1608
0
    {
1609
0
      final_val += right;
1610
0
      goto do_symbol;
1611
0
    }
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
10
      }
1621
10.7k
    else if (op == O_subtract)
1622
4.47k
      {
1623
4.47k
        if (seg_right == absolute_section)
1624
26
    {
1625
26
      final_val -= right;
1626
26
      goto do_symbol;
1627
26
    }
1628
4.47k
      }
1629
1630
10.7k
    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
10.7k
    if (!(seg_left == absolute_section
1639
4.45k
    && seg_right == absolute_section)
1640
10.7k
        && !(op == O_eq || op == O_ne)
1641
10.6k
        && !((op == O_subtract
1642
6.25k
        || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1643
4.47k
       && seg_left == seg_right
1644
27
       && (seg_left != undefined_section
1645
24
           || add_symbol == op_symbol)))
1646
10.6k
      {
1647
        /* Don't emit messages unless we're finalizing the symbol value,
1648
     otherwise we may get the same message multiple times.  */
1649
10.6k
        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
10.6k
        else
1656
10.6k
    move_seg_ok = 0;
1657
10.6k
      }
1658
1659
10.7k
    if (move_seg_ok
1660
52
        && (final_seg == expr_section || final_seg == undefined_section))
1661
18
      final_seg = absolute_section;
1662
1663
    /* Check for division by zero.  */
1664
10.7k
    if ((op == O_divide || op == O_modulus) && right == 0)
1665
889
      {
1666
        /* If seg_right is not absolute_section, then we've
1667
     already issued a warning about using a bad symbol.  */
1668
889
        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
889
        right = 1;
1681
889
      }
1682
10.7k
    if ((op == O_left_shift || op == O_right_shift)
1683
5
        && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1684
2
      {
1685
2
        as_warn_value_out_of_range (_("shift count"), right, 0,
1686
2
            sizeof (valueT) * CHAR_BIT - 1,
1687
2
            NULL, 0);
1688
2
        left = right = 0;
1689
2
      }
1690
1691
10.7k
    switch (symp->x->value.X_op)
1692
10.7k
      {
1693
      /* See expr() for reasons of the use of valueT casts here.  */
1694
16
      case O_multiply:    left *= (valueT) right; break;
1695
1696
      /* See expr() for reasons of the special casing.  */
1697
5
      case O_divide:
1698
5
        if (right == 1)
1699
1
    break;
1700
4
        if (right == -1)
1701
1
    {
1702
1
      left = -(valueT) left;
1703
1
      break;
1704
1
    }
1705
3
        left /= right;
1706
3
        break;
1707
1708
      /* Again, see expr() for reasons of the special casing.  */
1709
4.41k
      case O_modulus:
1710
4.41k
        if (right == 1 || right == -1)
1711
888
    left = 0;
1712
3.52k
        else
1713
3.52k
    left %= right;
1714
4.41k
        break;
1715
1716
0
      case O_left_shift:
1717
0
        left = (valueT) left << (valueT) right; break;
1718
5
      case O_right_shift:
1719
5
        left = (valueT) left >> (valueT) right; break;
1720
2
      case O_bit_inclusive_or:  left |= right; break;
1721
1.76k
      case O_bit_or_not:    left |= ~right; break;
1722
0
      case O_bit_exclusive_or:  left ^= right; break;
1723
9
      case O_bit_and:   left &= right; break;
1724
10
      case O_add:     left += (valueT) right; break;
1725
4.44k
      case O_subtract:    left -= (valueT) right; break;
1726
36
      case O_eq:
1727
36
      case O_ne:
1728
36
        left = (left == right && seg_left == seg_right
1729
0
          && (seg_left != undefined_section
1730
0
        || add_symbol == op_symbol)
1731
36
          ? ~ (offsetT) 0 : 0);
1732
36
        if (symp->x->value.X_op == O_ne)
1733
0
    left = ~left;
1734
36
        break;
1735
6
      case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
1736
4
      case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
1737
0
      case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
1738
14
      case O_gt:  left = left >  right ? ~ (offsetT) 0 : 0; break;
1739
1
      case O_logical_and: left = left && right; break;
1740
11
      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
10.7k
      }
1752
1753
10.7k
    final_val += symp->frag->fr_address + left;
1754
10.7k
    if (final_seg == expr_section || final_seg == undefined_section)
1755
10.6k
      {
1756
10.6k
        if (seg_left == undefined_section
1757
4.44k
      || seg_right == undefined_section)
1758
10.6k
    final_seg = undefined_section;
1759
2
        else if (seg_left == absolute_section)
1760
0
    final_seg = seg_right;
1761
2
        else
1762
2
    final_seg = seg_left;
1763
10.6k
      }
1764
10.7k
    resolved = (symbol_resolved_p (add_symbol)
1765
0
          && symbol_resolved_p (op_symbol));
1766
10.7k
    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
63.6k
  }
1777
1778
63.6k
      symp->flags.resolving = 0;
1779
63.6k
    }
1780
1781
63.6k
  if (finalize_syms)
1782
0
    S_SET_VALUE (symp, final_val);
1783
1784
63.7k
 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
63.7k
    S_SET_SEGMENT (symp, final_seg);
1788
1789
  /* Don't worry if we can't resolve an expr_section symbol.  */
1790
63.7k
  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
63.7k
  return final_val;
1803
63.6k
}
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
20.3k
{
1831
20.3k
  symbolS *symbolP = *symbolPP;
1832
1833
20.3k
  if (symbolP->flags.local_symbol)
1834
204
    {
1835
204
      struct local_symbol *locsym = (struct local_symbol *) symbolP;
1836
1837
204
      *valueP = locsym->value;
1838
204
      *segP = locsym->section;
1839
204
      *fragPP = locsym->frag;
1840
204
    }
1841
20.1k
  else
1842
20.1k
    {
1843
20.1k
      expressionS exp = symbolP->x->value;
1844
1845
20.1k
      if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1846
20.1k
  {
1847
20.1k
    int resolved;
1848
1849
20.1k
    if (symbolP->flags.resolving)
1850
0
      return 0;
1851
20.1k
    symbolP->flags.resolving = 1;
1852
20.1k
    resolved = resolve_expression (&exp);
1853
20.1k
    symbolP->flags.resolving = 0;
1854
20.1k
    if (!resolved)
1855
5.65k
      return 0;
1856
1857
14.4k
    switch (exp.X_op)
1858
14.4k
      {
1859
13.6k
      case O_constant:
1860
14.0k
      case O_register:
1861
14.0k
        if (!symbol_equated_p (symbolP))
1862
14.0k
    break;
1863
        /* Fallthru.  */
1864
470
      case O_symbol:
1865
470
      case O_symbol_rva:
1866
470
        symbolP = exp.X_add_symbol;
1867
470
        break;
1868
0
      default:
1869
0
        return 0;
1870
14.4k
      }
1871
14.4k
  }
1872
1873
14.4k
      *symbolPP = symbolP;
1874
14.4k
      *valueP = exp.X_add_number;
1875
1876
      /* We may have picked up a local symbol above: Check again.  */
1877
14.4k
      if (symbolP->flags.local_symbol)
1878
0
  {
1879
0
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
1880
1881
0
    if (locsym->section == expr_section
1882
0
        || locsym->section == absolute_section
1883
0
        || 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
0
    *segP = locsym->section;
1896
0
    *fragPP = locsym->frag;
1897
0
  }
1898
14.4k
      else
1899
14.4k
  {
1900
14.4k
    *segP = symbolP->bsym->section;
1901
14.4k
    *fragPP = symbolP->frag;
1902
14.4k
  }
1903
1904
14.4k
      if (*segP == expr_section)
1905
650
  switch (exp.X_op)
1906
650
    {
1907
596
    case O_constant: *segP = absolute_section; break;
1908
54
    case O_register: *segP = reg_section; break;
1909
0
    default: break;
1910
650
    }
1911
14.4k
    }
1912
1913
14.6k
  return 1;
1914
20.3k
}
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
4.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
478
{
2071
478
  memset (fb_low_counter, 0, sizeof (fb_low_counter));
2072
478
}
2073
2074
/* Add one to the instance number of this fb label.  */
2075
2076
void
2077
fb_label_instance_inc (unsigned int label)
2078
1.32k
{
2079
1.32k
  fb_ent *i;
2080
2081
1.32k
  if (label < FB_LABEL_SPECIAL)
2082
578
    {
2083
578
      ++fb_low_counter[label];
2084
578
      return;
2085
578
    }
2086
2087
751
  if (fb_labels != NULL)
2088
750
    {
2089
750
      for (i = fb_labels + FB_LABEL_SPECIAL;
2090
10.8k
     i < fb_labels + fb_label_count; ++i)
2091
10.7k
  {
2092
10.7k
    if (*i == label)
2093
729
      {
2094
729
        ++fb_label_instances[i - fb_labels];
2095
729
        return;
2096
729
      }      /* if we find it  */
2097
10.7k
  }      /* for each existing label  */
2098
750
    }
2099
2100
  /* If we get to here, we don't have label listed yet.  */
2101
2102
22
  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
21
  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
22
  fb_labels[fb_label_count] = label;
2119
22
  fb_label_instances[fb_label_count] = 1;
2120
22
  ++fb_label_count;
2121
22
}
2122
2123
static unsigned int
2124
fb_label_instance (unsigned int label)
2125
1.53k
{
2126
1.53k
  fb_ent *i;
2127
2128
1.53k
  if (label < FB_LABEL_SPECIAL)
2129
761
    return (fb_low_counter[label]);
2130
2131
769
  if (fb_labels != NULL)
2132
769
    {
2133
769
      for (i = fb_labels + FB_LABEL_SPECIAL;
2134
11.0k
     i < fb_labels + fb_label_count; ++i)
2135
11.0k
  {
2136
11.0k
    if (*i == label)
2137
751
      return (fb_label_instances[i - fb_labels]);
2138
11.0k
  }
2139
769
    }
2140
2141
  /* We didn't find the label, so this must be a reference to the
2142
     first instance.  */
2143
18
  return 0;
2144
769
}
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
1.53k
{
2162
  /* Returned to caller, then copied.  Used for created names ("4f").  */
2163
1.53k
  static char symbol_name_build[24];
2164
1.53k
  char *p = symbol_name_build;
2165
2166
#ifdef TC_MMIX
2167
  know (augend <= 2 /* See mmix_fb_label.  */);
2168
#else
2169
1.53k
  know (augend <= 1);
2170
1.53k
#endif
2171
2172
1.53k
#ifdef LOCAL_LABEL_PREFIX
2173
1.53k
  *p++ = LOCAL_LABEL_PREFIX;
2174
1.53k
#endif
2175
1.53k
  sprintf (p, "L%u%c%u",
2176
1.53k
     n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2177
1.53k
  return symbol_name_build;
2178
1.53k
}
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
29.2k
{
2228
29.2k
  if (s->flags.local_symbol)
2229
14
    return resolve_symbol_value (s);
2230
2231
29.2k
  if (!s->flags.resolved)
2232
29.2k
    {
2233
29.2k
      valueT val = resolve_symbol_value (s);
2234
29.2k
      if (!finalize_syms)
2235
29.2k
  return val;
2236
29.2k
    }
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
29.2k
{
2262
29.2k
  return S_GET_VALUE_WHERE (s, NULL, 0);
2263
29.2k
}
2264
2265
/* Set the value of a symbol.  */
2266
2267
void
2268
S_SET_VALUE (symbolS *s, valueT val)
2269
799k
{
2270
799k
  if (s->flags.local_symbol)
2271
0
    {
2272
0
      ((struct local_symbol *) s)->value = val;
2273
0
      return;
2274
0
    }
2275
2276
799k
  s->x->value.X_op = O_constant;
2277
799k
  s->x->value.X_add_number = (offsetT) val;
2278
799k
  s->x->value.X_unsigned = 0;
2279
799k
  S_CLEAR_WEAKREFR (s);
2280
799k
}
2281
2282
void
2283
copy_symbol_attributes (symbolS *dest, symbolS *src)
2284
2.90k
{
2285
2.90k
  if (dest->flags.local_symbol)
2286
0
    dest = local_symbol_convert (dest);
2287
2.90k
  if (src->flags.local_symbol)
2288
6
    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
2.90k
#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2293
2.90k
       | BSF_GNU_INDIRECT_FUNCTION)
2294
2.90k
  dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2295
2296
2.90k
#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2297
2.90k
  OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2298
2.90k
#endif
2299
2300
#ifdef TC_COPY_SYMBOL_ATTRIBUTES
2301
  TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2302
#endif
2303
2.90k
}
2304
2305
int
2306
S_IS_FUNCTION (const symbolS *s)
2307
2.23k
{
2308
2.23k
  flagword flags;
2309
2310
2.23k
  if (s->flags.local_symbol)
2311
0
    return 0;
2312
2313
2.23k
  flags = s->bsym->flags;
2314
2315
2.23k
  return (flags & BSF_FUNCTION) != 0;
2316
2.23k
}
2317
2318
int
2319
S_IS_EXTERNAL (const symbolS *s)
2320
8.15k
{
2321
8.15k
  flagword flags;
2322
2323
8.15k
  if (s->flags.local_symbol)
2324
0
    return 0;
2325
2326
8.15k
  flags = s->bsym->flags;
2327
2328
  /* Sanity check.  */
2329
8.15k
  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2330
0
    abort ();
2331
2332
8.15k
  return (flags & BSF_GLOBAL) != 0;
2333
8.15k
}
2334
2335
int
2336
S_IS_WEAK (const symbolS *s)
2337
43
{
2338
43
  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
43
  if (S_IS_WEAKREFR (s))
2345
0
    return S_IS_WEAK (s->x->value.X_add_symbol);
2346
43
  return (s->bsym->flags & BSF_WEAK) != 0;
2347
43
}
2348
2349
int
2350
S_IS_WEAKREFR (const symbolS *s)
2351
135k
{
2352
135k
  if (s->flags.local_symbol)
2353
0
    return 0;
2354
135k
  return s->flags.weakrefr != 0;
2355
135k
}
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
22.1k
{
2368
22.1k
  if (s->flags.local_symbol)
2369
0
    return 0;
2370
22.1k
  return bfd_is_com_section (s->bsym->section);
2371
22.1k
}
2372
2373
int
2374
S_IS_DEFINED (const symbolS *s)
2375
43.2k
{
2376
43.2k
  if (s->flags.local_symbol)
2377
17
    return ((const struct local_symbol *) s)->section != undefined_section;
2378
43.1k
  return s->bsym->section != undefined_section;
2379
43.2k
}
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
803
{
2392
803
  segT sec;
2393
803
  if (s->flags.local_symbol)
2394
17
    sec = ((const struct local_symbol *) s)->section;
2395
786
  else
2396
786
    {
2397
786
      if ((strict
2398
0
     && ((s->bsym->flags & BSF_WEAK) != 0
2399
0
         || (EXTERN_FORCE_RELOC
2400
0
       && (s->bsym->flags & BSF_GLOBAL) != 0)))
2401
786
    || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2402
0
  return true;
2403
786
      sec = s->bsym->section;
2404
786
    }
2405
803
  return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2406
803
}
2407
2408
int
2409
S_IS_DEBUG (const symbolS *s)
2410
9
{
2411
9
  if (s->flags.local_symbol)
2412
0
    return 0;
2413
9
  if (s->bsym->flags & BSF_DEBUGGING)
2414
0
    return 1;
2415
9
  return 0;
2416
9
}
2417
2418
int
2419
S_IS_LOCAL (const symbolS *s)
2420
8.09k
{
2421
8.09k
  flagword flags;
2422
8.09k
  const char *name;
2423
2424
8.09k
  if (s->flags.local_symbol)
2425
0
    return 1;
2426
2427
8.09k
  if (S_IS_EXTERNAL (s))
2428
8.08k
    return 0;
2429
2430
9
  if (bfd_asymbol_section (s->bsym) == reg_section)
2431
0
    return 1;
2432
2433
9
  flags = s->bsym->flags;
2434
2435
9
  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
9
  name = S_GET_NAME (s);
2443
9
  return (name != NULL
2444
9
    && ! S_IS_DEBUG (s)
2445
9
    && (strchr (name, DOLLAR_LABEL_CHAR)
2446
3
        || 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
3
        || (! flag_keep_locals
2452
3
      && (bfd_is_local_label (stdoutput, s->bsym)
2453
3
          || (flag_mri
2454
0
        && name[0] == '?'
2455
0
        && name[1] == '?')))));
2456
9
}
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
721
{
2467
721
  if (s->flags.local_symbol)
2468
0
    return (((const struct local_symbol *) s)->frag
2469
0
      == &predefined_address_frag);
2470
  /* Permit register names to be redefined.  */
2471
721
  return s->x->value.X_op == O_register;
2472
721
}
2473
2474
int
2475
S_IS_VOLATILE (const symbolS *s)
2476
40.5k
{
2477
40.5k
  if (s->flags.local_symbol)
2478
0
    return 0;
2479
40.5k
  return s->flags.volatil;
2480
40.5k
}
2481
2482
int
2483
S_IS_FORWARD_REF (const symbolS *s)
2484
31.8k
{
2485
31.8k
  if (s->flags.local_symbol)
2486
4
    return 0;
2487
31.8k
  return s->flags.forward_ref;
2488
31.8k
}
2489
2490
const char *
2491
S_GET_NAME (const symbolS *s)
2492
2.17k
{
2493
2.17k
  return s->name;
2494
2.17k
}
2495
2496
segT
2497
S_GET_SEGMENT (const symbolS *s)
2498
290k
{
2499
290k
  if (s->flags.local_symbol)
2500
2.54k
    return ((const struct local_symbol *) s)->section;
2501
287k
  return s->bsym->section;
2502
290k
}
2503
2504
void
2505
S_SET_SEGMENT (symbolS *s, segT seg)
2506
828k
{
2507
828k
  if (s->flags.local_symbol)
2508
0
    {
2509
0
      ((struct local_symbol *) s)->section = seg;
2510
0
      return;
2511
0
    }
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
828k
  if (s->bsym->flags & BSF_SECTION_SYM)
2517
9
    {
2518
9
      if (s->bsym->section != seg)
2519
0
  abort ();
2520
9
    }
2521
828k
  else
2522
828k
    {
2523
828k
      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
828k
      s->bsym->section = seg;
2536
828k
    }
2537
828k
}
2538
2539
void
2540
S_SET_EXTERNAL (symbolS *s)
2541
16.2k
{
2542
16.2k
  if (s->flags.local_symbol)
2543
0
    s = local_symbol_convert (s);
2544
16.2k
  if ((s->bsym->flags & BSF_WEAK) != 0)
2545
6
    {
2546
      /* Let .weak override .global.  */
2547
6
      return;
2548
6
    }
2549
16.2k
  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
16.2k
#ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2556
16.2k
  if (S_GET_SEGMENT (s) == reg_section)
2557
0
    {
2558
0
      as_bad (_("can't make register symbol global"));
2559
0
      return;
2560
0
    }
2561
16.2k
#endif
2562
16.2k
  s->bsym->flags |= BSF_GLOBAL;
2563
16.2k
  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
16.2k
}
2570
2571
void
2572
S_CLEAR_EXTERNAL (symbolS *s)
2573
21.8k
{
2574
21.8k
  if (s->flags.local_symbol)
2575
0
    return;
2576
21.8k
  if ((s->bsym->flags & BSF_WEAK) != 0)
2577
0
    {
2578
      /* Let .weak override.  */
2579
0
      return;
2580
0
    }
2581
21.8k
  s->bsym->flags |= BSF_LOCAL;
2582
21.8k
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2583
21.8k
}
2584
2585
void
2586
S_SET_WEAK (symbolS *s)
2587
97
{
2588
97
  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
97
  s->bsym->flags |= BSF_WEAK;
2594
97
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2595
97
}
2596
2597
void
2598
S_SET_WEAKREFR (symbolS *s)
2599
7
{
2600
7
  if (s->flags.local_symbol)
2601
0
    s = local_symbol_convert (s);
2602
7
  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
7
  if (s->flags.used)
2610
2
    symbol_mark_used (s->x->value.X_add_symbol);
2611
7
}
2612
2613
void
2614
S_CLEAR_WEAKREFR (symbolS *s)
2615
1.62M
{
2616
1.62M
  if (s->flags.local_symbol)
2617
153
    return;
2618
1.62M
  s->flags.weakrefr = 0;
2619
1.62M
}
2620
2621
void
2622
S_SET_WEAKREFD (symbolS *s)
2623
3
{
2624
3
  if (s->flags.local_symbol)
2625
0
    s = local_symbol_convert (s);
2626
3
  s->flags.weakrefd = 1;
2627
3
  S_SET_WEAK (s);
2628
3
}
2629
2630
void
2631
S_CLEAR_WEAKREFD (symbolS *s)
2632
109k
{
2633
109k
  if (s->flags.local_symbol)
2634
800
    return;
2635
108k
  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
108k
}
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
18.3k
{
2684
18.3k
  if (s->flags.local_symbol)
2685
7
    s = local_symbol_convert (s);
2686
18.3k
  s->flags.volatil = 1;
2687
18.3k
}
2688
2689
void
2690
S_CLEAR_VOLATILE (symbolS *s)
2691
7
{
2692
7
  if (!s->flags.local_symbol)
2693
7
    s->flags.volatil = 0;
2694
7
}
2695
2696
void
2697
S_SET_FORWARD_REF (symbolS *s)
2698
96
{
2699
96
  if (s->flags.local_symbol)
2700
6
    s = local_symbol_convert (s);
2701
96
  s->flags.forward_ref = 1;
2702
96
}
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.04k
{
2719
2.04k
  if (s->flags.local_symbol)
2720
0
    abort ();
2721
2.04k
  return s->x->next;
2722
2.04k
}
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
4.04k
{
2729
4.04k
  if (s->flags.local_symbol)
2730
0
    s = local_symbol_convert (s);
2731
4.04k
  return &s->x->value;
2732
4.04k
}
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
53.4k
{
2739
53.4k
  if (s->flags.local_symbol)
2740
0
    s = local_symbol_convert (s);
2741
53.4k
  s->x->value = *exp;
2742
53.4k
  S_CLEAR_WEAKREFR (s);
2743
53.4k
}
2744
2745
/* Return whether 2 symbols are the same.  */
2746
2747
int
2748
symbol_same_p (const symbolS *s1, const symbolS *s2)
2749
4.89k
{
2750
4.89k
  return s1 == s2;
2751
4.89k
}
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
4
{
2758
4
  if (s->flags.local_symbol)
2759
0
    return (offsetT *) &((struct local_symbol *) s)->value;
2760
2761
4
  return &s->x->value.X_add_number;
2762
4
}
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
727k
{
2769
727k
  S_SET_SEGMENT (sym, now_seg);
2770
727k
  S_SET_VALUE (sym, frag_now_fix ());
2771
727k
  symbol_set_frag (sym, frag_now);
2772
727k
}
2773
2774
/* Set the frag of a symbol.  */
2775
2776
void
2777
symbol_set_frag (symbolS *s, fragS *f)
2778
746k
{
2779
746k
  if (s->flags.local_symbol)
2780
0
    {
2781
0
      ((struct local_symbol *) s)->frag = f;
2782
0
      return;
2783
0
    }
2784
746k
  s->frag = f;
2785
746k
  S_CLEAR_WEAKREFR (s);
2786
746k
}
2787
2788
/* Return the frag of a symbol.  */
2789
2790
fragS *
2791
symbol_get_frag (const symbolS *s)
2792
26.4k
{
2793
26.4k
  if (s->flags.local_symbol)
2794
19
    return ((struct local_symbol *) s)->frag;
2795
26.4k
  return s->frag;
2796
26.4k
}
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
12.2k
{
2802
12.2k
  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
12.2k
  gas_assert (s->x->value.X_op == O_constant);
2811
12.2k
  *value = s->x->value.X_add_number;
2812
12.2k
  return s->frag;
2813
12.2k
}
2814
2815
/* Mark a symbol as having been used.  */
2816
2817
void
2818
symbol_mark_used (symbolS *s)
2819
136k
{
2820
136k
  if (s->flags.local_symbol)
2821
1.57k
    return;
2822
135k
  s->flags.used = 1;
2823
135k
  if (S_IS_WEAKREFR (s))
2824
5
    symbol_mark_used (s->x->value.X_add_symbol);
2825
135k
}
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
228
{
2852
228
  if (s->flags.local_symbol)
2853
0
    s = local_symbol_convert (s);
2854
228
  s->flags.used_in_reloc = 1;
2855
228
}
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
10.8k
{
2970
10.8k
  return s->flags.resolved;
2971
10.8k
}
2972
2973
/* Mark a symbol as being resolved.  */
2974
2975
void
2976
symbol_mark_resolving (symbolS *s)
2977
2.34k
{
2978
2.34k
  s->flags.resolving = 1;
2979
2.34k
}
2980
2981
void
2982
symbol_clear_resolving (symbolS *s)
2983
2.34k
{
2984
2.34k
  s->flags.resolving = 0;
2985
2.34k
}
2986
2987
/* Return whether a symbol is being resolved.  */
2988
2989
int
2990
symbol_resolving_p (const symbolS *s)
2991
15.0k
{
2992
15.0k
  return s->flags.resolving;
2993
15.0k
}
2994
2995
/* Return whether a symbol is a section symbol.  */
2996
2997
int
2998
symbol_section_p (const symbolS *s)
2999
20.1k
{
3000
20.1k
  if (s->flags.local_symbol)
3001
0
    return 0;
3002
20.1k
  return (s->bsym->flags & BSF_SECTION_SYM) != 0;
3003
20.1k
}
3004
3005
/* Return whether a symbol is equated to another symbol.  */
3006
3007
int
3008
symbol_equated_p (const symbolS *s)
3009
40.3k
{
3010
40.3k
  if (s->flags.local_symbol)
3011
16
    return 0;
3012
40.3k
  return s->x->value.X_op == O_symbol;
3013
40.3k
}
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
14.2k
{
3042
14.2k
  valueT add = 0;
3043
14.2k
  symbolS *ret = sym;
3044
14.4k
  while (ret && symbol_equated_p (ret))
3045
142
    {
3046
142
      const expressionS *e = symbol_get_value_expression (ret);
3047
142
      add += e->X_add_number;
3048
142
      ret->flags.resolving = 1;
3049
142
      ret = e->X_add_symbol;
3050
142
      if (ret->flags.resolving)
3051
0
  ret = NULL;
3052
142
    }
3053
14.4k
  while (sym && sym->flags.resolving)
3054
142
    {
3055
142
      const expressionS *e = symbol_get_value_expression (sym);
3056
142
      sym->flags.resolving = 0;
3057
142
      sym = e->X_add_symbol;
3058
142
    }
3059
14.2k
  *off = add;
3060
14.2k
  return ret;
3061
14.2k
}
3062
3063
/* Return whether a symbol has a constant value.  */
3064
3065
int
3066
symbol_constant_p (const symbolS *s)
3067
42
{
3068
42
  if (s->flags.local_symbol)
3069
0
    return 1;
3070
42
  return s->x->value.X_op == O_constant;
3071
42
}
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
52.5k
{
3099
52.5k
  if (s->flags.local_symbol)
3100
0
    s = local_symbol_convert (s);
3101
52.5k
  return s->bsym;
3102
52.5k
}
3103
3104
/* Set the BFD symbol for a symbol.  */
3105
3106
void
3107
symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3108
1.67k
{
3109
1.67k
  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
1.67k
  if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3119
1.67k
    s->bsym = bsym;
3120
  /* else XXX - What do we do now ?  */
3121
1.67k
}
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
71.0k
{
3130
71.0k
  if (s->flags.local_symbol)
3131
1
    s = local_symbol_convert (s);
3132
71.0k
  return &s->x->obj;
3133
71.0k
}
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
478
{
3174
478
  symbol_lastP = NULL;
3175
478
  symbol_rootP = NULL;   /* In case we have 0 symbols (!!)  */
3176
478
  sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3177
478
             NULL, xcalloc, free);
3178
3179
478
#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3180
478
  abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3181
478
#endif
3182
478
  abs_symbol.x = &abs_symbol_x;
3183
478
  abs_symbol.x->value.X_op = O_constant;
3184
478
  abs_symbol.frag = &zero_address_frag;
3185
3186
478
  if (LOCAL_LABELS_FB)
3187
478
    fb_label_init ();
3188
478
}
3189
3190
void
3191
symbol_end (void)
3192
478
{
3193
478
  if (ENABLE_LEAK_CHECK)
3194
478
    htab_delete (sy_hash);
3195
478
}
3196
3197
void
3198
dot_symbol_init (void)
3199
478
{
3200
478
  dot_symbol.name = ".";
3201
478
  dot_symbol.flags.forward_ref = 1;
3202
478
  dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3203
478
  if (dot_symbol.bsym == NULL)
3204
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3205
478
  dot_symbol.bsym->name = ".";
3206
478
  dot_symbol.x = &dot_symbol_x;
3207
478
  dot_symbol.x->value.X_op = O_constant;
3208
478
}
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