Coverage Report

Created: 2026-10-02 09:53

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
26.0k
{
185
26.0k
  symbol_entry_t *entry = (symbol_entry_t *) e;
186
26.0k
  if (entry->sy.hash == 0)
187
12.2k
    entry->sy.hash = htab_hash_string (entry->sy.name);
188
189
26.0k
  return entry->sy.hash;
190
26.0k
}
191
192
/* Equality function for a symbol_entry.  */
193
194
static int
195
eq_symbol_entry (const void *a, const void *b)
196
141k
{
197
141k
  const symbol_entry_t *ea = a;
198
141k
  const symbol_entry_t *eb = b;
199
200
141k
  return (ea->sy.hash == eb->sy.hash
201
118k
    && strcmp (ea->sy.name, eb->sy.name) == 0);
202
141k
}
203
204
static void *
205
symbol_entry_find (htab_t table, const char *name)
206
126k
{
207
126k
  hashval_t hash = htab_hash_string (name);
208
126k
  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209
126k
            hash, name, 0, 0, 0 } };
210
126k
  return htab_find_with_hash (table, &needle, hash);
211
126k
}
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
19.2k
#define debug_verify_symchain(root, last) ((void) 0)
236
#endif
237
238
0
#define DOLLAR_LABEL_CHAR '\001'
239
8.27k
#define LOCAL_LABEL_CHAR  '\002'
240
241
#ifndef TC_LABEL_IS_LOCAL
242
0
#define TC_LABEL_IS_LOCAL(name) 0
243
#endif
244
245
struct obstack notes;
246
247
/* Utility functions to allocate and duplicate memory on the notes
248
   obstack, each like the corresponding function without "notes_"
249
   prefix.  All of these exit on an allocation failure.  */
250
251
void *
252
notes_alloc (size_t size)
253
1.00M
{
254
1.00M
  return obstack_alloc (&notes, size);
255
1.00M
}
256
257
void *
258
notes_calloc (size_t n, size_t size)
259
924k
{
260
924k
  size_t amt;
261
924k
  void *ret;
262
924k
  if (_bfd_mul_overflow (n, size, &amt))
263
0
    {
264
0
      obstack_alloc_failed_handler ();
265
0
      abort ();
266
0
    }
267
924k
  ret = notes_alloc (amt);
268
924k
  memset (ret, 0, amt);
269
924k
  return ret;
270
924k
}
271
272
void *
273
notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
274
47.0k
{
275
47.0k
  void *ret = obstack_alloc (&notes, alloc_size);
276
47.0k
  memcpy (ret, src, copy_size);
277
47.0k
  if (alloc_size > copy_size)
278
11
    memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279
47.0k
  return ret;
280
47.0k
}
281
282
char *
283
notes_strdup (const char *str)
284
47.0k
{
285
47.0k
  size_t len = strlen (str) + 1;
286
47.0k
  return notes_memdup (str, len, len);
287
47.0k
}
288
289
char *
290
notes_concat (const char *first, ...)
291
23
{
292
23
  va_list args;
293
23
  const char *str;
294
295
23
  va_start (args, first);
296
69
  for (str = first; str; str = va_arg (args, const char *))
297
46
    {
298
46
      size_t size = strlen (str);
299
46
      obstack_grow (&notes, str, size);
300
46
    }
301
23
  va_end (args);
302
23
  obstack_1grow (&notes, 0);
303
23
  return obstack_finish (&notes);
304
23
}
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
11.8k
{
333
11.8k
  symbolS *symbolP = symbol_create (name, segment, frag, valu);
334
335
  /* Link to end of symbol chain.  */
336
11.8k
  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
337
338
11.8k
  return symbolP;
339
11.8k
}
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
46.9k
{
347
46.9k
  char *ret;
348
349
46.9k
  gas_assert (name != NULL);
350
46.9k
  ret = notes_strdup (name);
351
352
#ifdef tc_canonicalize_symbol_name
353
  ret = tc_canonicalize_symbol_name (ret);
354
#endif
355
356
46.9k
  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
46.9k
  return ret;
365
46.9k
}
366
367
static void
368
symbol_init (symbolS *symbolP, const char *name, asection *sec,
369
       fragS *frag, valueT valu)
370
38.4k
{
371
38.4k
  symbolP->frag = frag;
372
38.4k
  symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373
38.4k
  if (symbolP->bsym == NULL)
374
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375
38.4k
  symbolP->bsym->name = name;
376
38.4k
  symbolP->bsym->section = sec;
377
378
38.4k
  if (multibyte_handling == multibyte_warn_syms
379
0
      && ! symbolP->flags.local_symbol
380
0
      && sec != undefined_section
381
0
      && ! symbolP->flags.multibyte_warned
382
0
      && scan_for_multibyte_characters ((const unsigned char *) name,
383
0
          (const unsigned char *) name + strlen (name),
384
0
          false /* Do not warn.  */))
385
0
    {
386
0
      as_warn (_("symbol '%s' contains multibyte characters"), name);
387
0
      symbolP->flags.multibyte_warned = 1;
388
0
    }
389
390
38.4k
  S_SET_VALUE (symbolP, valu);
391
38.4k
  if (sec == reg_section)
392
1.71k
    symbolP->x->value.X_op = O_register;
393
394
38.4k
  symbol_clear_list_pointers (symbolP);
395
396
38.4k
  obj_symbol_new_hook (symbolP);
397
398
#ifdef tc_symbol_new_hook
399
  tc_symbol_new_hook (symbolP);
400
#endif
401
38.4k
}
402
403
/* Create a symbol.  NAME is copied, the caller can destroy/modify.  */
404
405
symbolS *
406
symbol_create (const char *name, segT segment, fragS *frag, valueT valu)
407
38.4k
{
408
38.4k
  const char *preserved_copy_of_name;
409
38.4k
  symbolS *symbolP;
410
38.4k
  size_t size;
411
412
38.4k
  preserved_copy_of_name = save_symbol_name (name);
413
414
38.4k
  size = sizeof (symbolS) + sizeof (struct xsymbol);
415
38.4k
  symbolP = notes_alloc (size);
416
417
  /* symbol must be born in some fixed state.  This seems as good as any.  */
418
38.4k
  memset (symbolP, 0, size);
419
38.4k
  symbolP->name = preserved_copy_of_name;
420
38.4k
  symbolP->x = (struct xsymbol *) (symbolP + 1);
421
422
38.4k
  symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
423
424
38.4k
  return symbolP;
425
38.4k
}
426

427
428
/* Local symbol support.  If we can get away with it, we keep only a
429
   small amount of information for local symbols.  */
430
431
/* Used for statistics.  */
432
433
static unsigned long local_symbol_count;
434
static unsigned long local_symbol_conversion_count;
435
436
/* Create a local symbol and insert it into the local hash table.  */
437
438
struct local_symbol *
439
local_symbol_make (const char *name, segT section, fragS *frag, valueT val)
440
8.58k
{
441
8.58k
  const char *name_copy;
442
8.58k
  struct local_symbol *ret;
443
8.58k
  struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
444
445
8.58k
  ++local_symbol_count;
446
447
8.58k
  name_copy = save_symbol_name (name);
448
449
8.58k
  ret = notes_alloc (sizeof *ret);
450
8.58k
  ret->flags = flags;
451
8.58k
  ret->hash = 0;
452
8.58k
  ret->name = name_copy;
453
8.58k
  ret->frag = frag;
454
8.58k
  ret->section = section;
455
8.58k
  ret->value = val;
456
457
8.58k
  htab_insert (sy_hash, ret, 1);
458
459
8.58k
  return ret;
460
8.58k
}
461
462
/* Convert a local symbol into a real symbol.  */
463
464
static symbolS *
465
local_symbol_convert (void *sym)
466
20
{
467
20
  symbol_entry_t *ent = sym;
468
20
  struct xsymbol *xtra;
469
20
  valueT val;
470
471
20
  gas_assert (ent->lsy.flags.local_symbol);
472
473
20
  ++local_symbol_conversion_count;
474
475
20
  xtra = notes_alloc (sizeof (*xtra));
476
20
  memset (xtra, 0, sizeof (*xtra));
477
20
  val = ent->lsy.value;
478
20
  ent->sy.x = xtra;
479
480
  /* Local symbols are always either defined or used.  */
481
20
  ent->sy.flags.used = 1;
482
20
  ent->sy.flags.local_symbol = 0;
483
484
20
  symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
485
20
  symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
486
487
20
  return &ent->sy;
488
20
}
489

490
static void
491
define_sym_at_dot (symbolS *symbolP)
492
2.55k
{
493
2.55k
  symbolP->frag = frag_now;
494
2.55k
  S_SET_VALUE (symbolP, frag_now_fix ());
495
2.55k
  S_SET_SEGMENT (symbolP, now_seg);
496
2.55k
}
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
16.0k
{
508
16.0k
  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
16.0k
  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
16.0k
  if ((symbolP = symbol_find (sym_name)) != 0)
558
7.73k
    {
559
7.73k
      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
7.73k
      if (symbolP->flags.local_symbol)
566
0
  {
567
0
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
568
569
0
    if (locsym->section != undefined_section
570
0
        && (locsym->frag != frag_now
571
0
      || locsym->section != now_seg
572
0
      || locsym->value != frag_now_fix ()))
573
0
      {
574
0
        as_bad (_("symbol `%s' is already defined"), sym_name);
575
0
        return symbolP;
576
0
      }
577
578
0
    locsym->section = now_seg;
579
0
    locsym->frag = frag_now;
580
0
    locsym->value = frag_now_fix ();
581
0
  }
582
7.73k
      else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
583
7.73k
         || S_IS_COMMON (symbolP)
584
7.72k
         || S_IS_VOLATILE (symbolP))
585
14
  {
586
14
    if (S_IS_VOLATILE (symbolP))
587
0
      {
588
0
        symbolP = symbol_clone (symbolP, 1);
589
0
        S_SET_VALUE (symbolP, 0);
590
0
        S_CLEAR_VOLATILE (symbolP);
591
0
      }
592
14
    if (S_GET_VALUE (symbolP) == 0)
593
7
      {
594
7
        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
7
      }
600
7
    else
601
7
      {
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
7
        if (((!S_IS_DEBUG (symbolP)
614
7
        && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
615
7
        && S_IS_EXTERNAL (symbolP))
616
0
       || S_GET_SEGMENT (symbolP) == bss_section)
617
7
      && (now_seg == data_section
618
7
          || now_seg == bss_section
619
7
          || 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
7
        else
640
7
    {
641
7
#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
642
7
      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
7
      as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
652
7
          sym_name,
653
7
          segment_name (S_GET_SEGMENT (symbolP)),
654
7
          od_buf,
655
7
          (long) S_GET_VALUE (symbolP));
656
7
    }
657
7
      }      /* if the undefined symbol has no value  */
658
14
  }
659
7.72k
      else
660
7.72k
  {
661
    /* Don't blow up if the definition is the same.  */
662
7.72k
    if (!(frag_now == symbolP->frag
663
7.08k
    && S_GET_VALUE (symbolP) == frag_now_fix ()
664
5.17k
    && S_GET_SEGMENT (symbolP) == now_seg))
665
2.54k
      {
666
2.54k
        as_bad (_("symbol `%s' is already defined"), sym_name);
667
2.54k
        symbolP = symbol_clone (symbolP, 0);
668
2.54k
        define_sym_at_dot (symbolP);
669
2.54k
      }
670
7.72k
  }
671
672
7.73k
    }
673
8.35k
  else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
674
8.26k
    {
675
8.26k
      symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
676
8.26k
                 frag_now_fix ());
677
8.26k
    }
678
94
  else
679
94
    {
680
94
      symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
681
682
94
      symbol_table_insert (symbolP);
683
94
    }
684
685
16.0k
  if (mri_common_symbol != NULL)
686
3
    {
687
      /* This symbol is actually being defined within an MRI common
688
   section.  This requires special handling.  */
689
3
      if (symbolP->flags.local_symbol)
690
0
  symbolP = local_symbol_convert (symbolP);
691
3
      symbolP->x->value.X_op = O_symbol;
692
3
      symbolP->x->value.X_add_symbol = mri_common_symbol;
693
3
      symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
694
3
      symbolP->frag = &zero_address_frag;
695
3
      S_SET_SEGMENT (symbolP, expr_section);
696
3
      symbolP->flags.mri_common = 1;
697
3
    }
698
699
#ifdef tc_frob_label
700
  tc_frob_label (symbolP);
701
#endif
702
16.0k
#ifdef obj_frob_label
703
16.0k
  obj_frob_label (symbolP);
704
16.0k
#endif
705
16.0k
  if (flag_synth_cfi)
706
0
    ginsn_frob_label (symbolP);
707
708
16.0k
  return symbolP;
709
16.0k
}
710

711
/* Die if we can't insert the symbol.  */
712
713
void
714
symbol_table_insert (symbolS *symbolP)
715
11.3k
{
716
11.3k
  know (symbolP);
717
718
11.3k
  htab_insert (sy_hash, symbolP, 1);
719
11.3k
}
720

721
/* If a symbol name does not exist, create it as undefined, and insert
722
   it into the symbol table.  Return a pointer to it.  */
723
724
symbolS *
725
symbol_find_or_make (const char *name)
726
62.0k
{
727
62.0k
  symbolS *symbolP;
728
729
62.0k
  symbolP = symbol_find (name);
730
731
62.0k
  if (symbolP == NULL)
732
1.24k
    {
733
1.24k
      if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
734
43
  {
735
43
    symbolP = md_undefined_symbol ((char *) name);
736
43
    if (symbolP != NULL)
737
0
      return symbolP;
738
739
43
    symbolP = (symbolS *) local_symbol_make (name, undefined_section,
740
43
               &zero_address_frag, 0);
741
43
    return symbolP;
742
43
  }
743
744
1.20k
      symbolP = symbol_make (name);
745
746
1.20k
      symbol_table_insert (symbolP);
747
1.20k
    }        /* if symbol wasn't found */
748
749
62.0k
  return symbolP;
750
62.0k
}
751
752
symbolS *
753
symbol_make (const char *name)
754
2.30k
{
755
2.30k
  symbolS *symbolP;
756
757
  /* Let the machine description default it, e.g. for register names.  */
758
2.30k
  symbolP = md_undefined_symbol ((char *) name);
759
760
2.30k
  if (!symbolP)
761
2.29k
    symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
762
763
2.30k
  return symbolP;
764
2.30k
}
765
766
symbolS *
767
symbol_clone (symbolS *orgsymP, int replace)
768
10.2k
{
769
10.2k
  symbolS *newsymP;
770
10.2k
  asymbol *bsymorg, *bsymnew;
771
772
  /* Make sure we never clone the dot special symbol.  */
773
10.2k
  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
10.2k
  if (orgsymP->flags.local_symbol)
780
0
    orgsymP = local_symbol_convert (orgsymP);
781
10.2k
  bsymorg = orgsymP->bsym;
782
783
10.2k
  newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
784
10.2k
  *newsymP = *orgsymP;
785
10.2k
  newsymP->x = (struct xsymbol *) (newsymP + 1);
786
10.2k
  *newsymP->x = *orgsymP->x;
787
10.2k
  bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
788
10.2k
  if (bsymnew == NULL)
789
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
790
10.2k
  newsymP->bsym = bsymnew;
791
10.2k
  bsymnew->name = bsymorg->name;
792
10.2k
  bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
793
10.2k
  bsymnew->section = bsymorg->section;
794
10.2k
  bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), &orgsymP->bsym,
795
10.2k
        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
10.2k
  if (replace)
806
7.67k
    {
807
7.67k
      if (orgsymP->x->previous != NULL)
808
7.67k
  orgsymP->x->previous->x->next = newsymP;
809
0
      else
810
0
  symbol_rootP = newsymP;
811
7.67k
      if (orgsymP->x->next != NULL)
812
5.63k
  orgsymP->x->next->x->previous = newsymP;
813
2.04k
      else
814
2.04k
  symbol_lastP = newsymP;
815
816
      /* Symbols that won't be output can't be external.  */
817
7.67k
      S_CLEAR_EXTERNAL (orgsymP);
818
7.67k
      orgsymP->x->previous = orgsymP->x->next = orgsymP;
819
7.67k
      debug_verify_symchain (symbol_rootP, symbol_lastP);
820
821
7.67k
      symbol_table_insert (newsymP);
822
7.67k
    }
823
2.54k
  else
824
2.54k
    {
825
      /* Symbols that won't be output can't be external.  */
826
2.54k
      S_CLEAR_EXTERNAL (newsymP);
827
2.54k
      newsymP->x->previous = newsymP->x->next = newsymP;
828
2.54k
    }
829
830
10.2k
  return newsymP;
831
10.2k
}
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
340k
{
841
340k
  if (symbolP
842
85.8k
      && !symbolP->flags.local_symbol
843
84.2k
      && !symbolP->flags.forward_resolved)
844
6.90k
    {
845
6.90k
      symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
846
6.90k
      symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
847
6.90k
      symbolS *add_symbol = orig_add_symbol;
848
6.90k
      symbolS *op_symbol = orig_op_symbol;
849
850
6.90k
      if (symbolP->flags.forward_ref)
851
0
  is_forward = 1;
852
853
6.90k
      if (is_forward)
854
0
  {
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
0
    if (add_symbol && S_IS_VOLATILE (add_symbol))
859
0
      add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
860
0
    if (op_symbol && S_IS_VOLATILE (op_symbol))
861
0
      op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
862
0
  }
863
864
      /* Re-using resolving here, as this routine cannot get called from
865
   symbol resolution code.  */
866
6.90k
      if ((symbolP->bsym->section == expr_section
867
6.17k
     || symbolP->flags.forward_ref)
868
729
    && !symbolP->flags.resolving)
869
729
  {
870
729
    symbolP->flags.resolving = 1;
871
729
    add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
872
729
    op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
873
729
    symbolP->flags.resolving = 0;
874
729
  }
875
876
6.90k
      if (symbolP->flags.forward_ref
877
6.90k
    || add_symbol != orig_add_symbol
878
6.90k
    || op_symbol != orig_op_symbol)
879
0
  {
880
0
    if (symbolP != &dot_symbol)
881
0
      {
882
0
        symbolP = symbol_clone (symbolP, 0);
883
0
        symbolP->flags.resolving = 0;
884
0
      }
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
0
  }
893
894
6.90k
      symbolP->x->value.X_add_symbol = add_symbol;
895
6.90k
      symbolP->x->value.X_op_symbol = op_symbol;
896
6.90k
      symbolP->flags.forward_resolved = 1;
897
6.90k
    }
898
899
340k
  return symbolP;
900
340k
}
901
902
symbolS *
903
symbol_temp_new (segT seg, fragS *frag, valueT ofs)
904
7.39k
{
905
7.39k
  return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
906
7.39k
}
907
908
symbolS *
909
symbol_temp_new_now (void)
910
6.53k
{
911
6.53k
  return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
912
6.53k
}
913
914
symbolS *
915
symbol_temp_new_now_octets (void)
916
166
{
917
166
  return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
918
166
}
919
920
symbolS *
921
symbol_temp_make (void)
922
1.08k
{
923
1.08k
  return symbol_make (FAKE_LABEL_NAME);
924
1.08k
}
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
126k
{
940
126k
  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
126k
  if (sym && ! noref)
948
111k
    S_CLEAR_WEAKREFD (sym);
949
950
126k
  return sym;
951
126k
}
952
953
symbolS *
954
symbol_find (const char *name)
955
126k
{
956
126k
  return symbol_find_noref (name, 0);
957
126k
}
958
959
symbolS *
960
symbol_find_noref (const char *name, int noref)
961
126k
{
962
126k
  symbolS * result;
963
126k
  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
126k
  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
126k
  result = symbol_find_exact_noref (name, noref);
992
126k
  free (copy);
993
126k
  return result;
994
126k
}
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
11.8k
{
1008
11.8k
  extern int symbol_table_frozen;
1009
11.8k
  if (symbol_table_frozen)
1010
0
    abort ();
1011
11.8k
  if (addme->flags.local_symbol)
1012
0
    abort ();
1013
11.8k
  if (target != NULL && target->flags.local_symbol)
1014
0
    abort ();
1015
1016
11.8k
  if (target == NULL)
1017
281
    {
1018
281
      know (*rootPP == NULL);
1019
281
      know (*lastPP == NULL);
1020
281
      addme->x->next = NULL;
1021
281
      addme->x->previous = NULL;
1022
281
      *rootPP = addme;
1023
281
      *lastPP = addme;
1024
281
      return;
1025
281
    }
1026
1027
11.5k
  if (target->x->next != NULL)
1028
0
    target->x->next->x->previous = addme;
1029
11.5k
  else
1030
11.5k
    *lastPP = addme;
1031
1032
11.5k
  addme->x->next = target->x->next;
1033
11.5k
  target->x->next = addme;
1034
11.5k
  addme->x->previous = target;
1035
1036
11.5k
  debug_verify_symchain (symbol_rootP, symbol_lastP);
1037
11.5k
}
1038
1039
/* Set the chain pointers of SYMBOL to null.  */
1040
1041
void
1042
symbol_clear_list_pointers (symbolS *symbolP)
1043
38.4k
{
1044
38.4k
  if (symbolP->flags.local_symbol)
1045
0
    abort ();
1046
38.4k
  symbolP->x->next = NULL;
1047
38.4k
  symbolP->x->previous = NULL;
1048
38.4k
}
1049
1050
/* Remove SYMBOLP from the list.  */
1051
1052
void
1053
symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1054
18
{
1055
18
  if (symbolP->flags.local_symbol)
1056
0
    abort ();
1057
1058
18
  if (symbolP->x->previous != NULL)
1059
18
    symbolP->x->previous->x->next = symbolP->x->next;
1060
0
  else
1061
0
    *rootPP = symbolP->x->next;
1062
1063
18
  if (symbolP->x->next != NULL)
1064
0
    symbolP->x->next->x->previous = symbolP->x->previous;
1065
18
  else
1066
18
    *lastPP = symbolP->x->previous;
1067
1068
18
  debug_verify_symchain (*rootPP, *lastPP);
1069
18
}
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
18
{
1077
18
  extern int symbol_table_frozen;
1078
18
  if (symbol_table_frozen)
1079
0
    abort ();
1080
18
  if (addme->flags.local_symbol)
1081
0
    abort ();
1082
18
  if (target->flags.local_symbol)
1083
0
    abort ();
1084
1085
18
  if (target->x->previous != NULL)
1086
0
    target->x->previous->x->next = addme;
1087
18
  else
1088
18
    *rootPP = addme;
1089
1090
18
  addme->x->previous = target->x->previous;
1091
18
  target->x->previous = addme;
1092
18
  addme->x->next = target;
1093
1094
18
  debug_verify_symchain (*rootPP, *lastPP);
1095
18
}
1096
1097
void
1098
verify_symbol_chain (symbolS *rootP, symbolS *lastP)
1099
0
{
1100
0
  symbolS *symbolP = rootP;
1101
1102
0
  if (symbolP == NULL)
1103
0
    return;
1104
1105
0
  for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
1106
0
    {
1107
0
      gas_assert (symbolP->bsym != NULL);
1108
0
      gas_assert (symbolP->flags.local_symbol == 0);
1109
0
      gas_assert (symbolP->x->next->x->previous == symbolP);
1110
0
    }
1111
1112
0
  gas_assert (lastP == symbolP);
1113
0
}
1114
1115
int
1116
symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
1117
0
{
1118
0
  return (!s->flags.local_symbol
1119
0
    && ((s->x->next != s
1120
0
         && s->x->next != NULL
1121
0
         && s->x->next->x->previous == s)
1122
0
        || s == lastPP)
1123
0
    && ((s->x->previous != s
1124
0
         && s->x->previous != NULL
1125
0
         && s->x->previous->x->next == s)
1126
0
        || s == rootPP));
1127
0
}
1128
1129
#ifdef OBJ_COMPLEX_RELC
1130
1131
static int
1132
use_complex_relocs_for (symbolS * symp)
1133
{
1134
  switch (symp->x->value.X_op)
1135
    {
1136
    case O_constant:
1137
      return 0;
1138
1139
    case O_multiply:
1140
    case O_divide:
1141
    case O_modulus:
1142
    case O_left_shift:
1143
    case O_right_shift:
1144
    case O_bit_inclusive_or:
1145
    case O_bit_or_not:
1146
    case O_bit_exclusive_or:
1147
    case O_bit_and:
1148
    case O_add:
1149
    case O_subtract:
1150
    case O_eq:
1151
    case O_ne:
1152
    case O_lt:
1153
    case O_le:
1154
    case O_ge:
1155
    case O_gt:
1156
    case O_logical_and:
1157
    case O_logical_or:
1158
      if ((S_IS_COMMON (symp->x->value.X_op_symbol)
1159
     || S_IS_LOCAL (symp->x->value.X_op_symbol))
1160
    && S_IS_DEFINED (symp->x->value.X_op_symbol)
1161
    && S_GET_SEGMENT (symp->x->value.X_op_symbol) != expr_section)
1162
  {
1163
  case O_symbol:
1164
  case O_symbol_rva:
1165
  case O_uminus:
1166
  case O_bit_not:
1167
  case O_logical_not:
1168
    if ((S_IS_COMMON (symp->x->value.X_add_symbol)
1169
         || S_IS_LOCAL (symp->x->value.X_add_symbol))
1170
        && S_IS_DEFINED (symp->x->value.X_add_symbol)
1171
        && S_GET_SEGMENT (symp->x->value.X_add_symbol) != expr_section)
1172
      return 0;
1173
  }
1174
      break;
1175
1176
    default:
1177
      break;
1178
    }
1179
  return 1;
1180
}
1181
#endif
1182
1183
static void
1184
report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
1185
0
{
1186
0
  const char *file;
1187
0
  unsigned int line;
1188
0
  segT seg_left = left ? S_GET_SEGMENT (left) : 0;
1189
0
  segT seg_right = S_GET_SEGMENT (right);
1190
0
  const char *opname;
1191
1192
0
  switch (op)
1193
0
    {
1194
0
    default:
1195
0
      abort ();
1196
0
      return;
1197
1198
0
    case O_uminus:    opname = "-"; break;
1199
0
    case O_bit_not:   opname = "~"; break;
1200
0
    case O_logical_not:   opname = "!"; break;
1201
0
    case O_multiply:    opname = "*"; break;
1202
0
    case O_divide:    opname = "/"; break;
1203
0
    case O_modulus:   opname = "%"; break;
1204
0
    case O_left_shift:    opname = "<<"; break;
1205
0
    case O_right_shift:   opname = ">>"; break;
1206
0
    case O_bit_inclusive_or:  opname = "|"; break;
1207
0
    case O_bit_or_not:    opname = "|~"; break;
1208
0
    case O_bit_exclusive_or:  opname = "^"; break;
1209
0
    case O_bit_and:   opname = "&"; break;
1210
0
    case O_add:     opname = "+"; break;
1211
0
    case O_subtract:    opname = "-"; break;
1212
0
    case O_eq:      opname = "=="; break;
1213
0
    case O_ne:      opname = "!="; break;
1214
0
    case O_lt:      opname = "<"; break;
1215
0
    case O_le:      opname = "<="; break;
1216
0
    case O_ge:      opname = ">="; break;
1217
0
    case O_gt:      opname = ">"; break;
1218
0
    case O_logical_and:   opname = "&&"; break;
1219
0
    case O_logical_or:    opname = "||"; break;
1220
0
    }
1221
1222
0
  if (expr_symbol_where (symp, &file, &line))
1223
0
    {
1224
0
      if (left)
1225
0
  as_bad_where (file, line,
1226
0
          _("invalid operands (%s and %s sections) for `%s'"),
1227
0
          seg_left->name, seg_right->name, opname);
1228
0
      else
1229
0
  as_bad_where (file, line,
1230
0
          _("invalid operand (%s section) for `%s'"),
1231
0
          seg_right->name, opname);
1232
0
    }
1233
0
  else
1234
0
    {
1235
0
      const char *sname = S_GET_NAME (symp);
1236
1237
0
      if (left)
1238
0
  as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1239
0
    seg_left->name, seg_right->name, opname, sname);
1240
0
      else
1241
0
  as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1242
0
    seg_right->name, opname, sname);
1243
0
    }
1244
0
}
1245
1246
/* Resolve the value of a symbol.  This is called during the final
1247
   pass over the symbol table to resolve any symbols with complex
1248
   values.  */
1249
1250
valueT
1251
resolve_symbol_value (symbolS *symp)
1252
22.9k
{
1253
22.9k
  int resolved;
1254
22.9k
  valueT final_val;
1255
22.9k
  segT final_seg;
1256
1257
22.9k
  if (symp->flags.local_symbol)
1258
42
    {
1259
42
      struct local_symbol *locsym = (struct local_symbol *) symp;
1260
1261
42
      final_val = locsym->value;
1262
42
      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
42
      if (locsym->section->flags & SEC_OCTETS)
1268
0
  final_val += locsym->frag->fr_address;
1269
42
      else
1270
42
  final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1271
1272
42
      if (finalize_syms)
1273
0
  {
1274
0
    locsym->value = final_val;
1275
0
    locsym->flags.resolved = 1;
1276
0
  }
1277
1278
42
      return final_val;
1279
42
    }
1280
1281
22.9k
  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
22.9k
  resolved = 0;
1305
22.9k
  final_seg = S_GET_SEGMENT (symp);
1306
1307
22.9k
  if (symp->flags.resolving)
1308
65
    {
1309
65
      if (finalize_syms)
1310
0
  as_bad (_("symbol definition loop encountered at `%s'"),
1311
0
    S_GET_NAME (symp));
1312
65
      final_val = 0;
1313
65
      resolved = 1;
1314
65
    }
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
22.8k
  else
1364
22.8k
    {
1365
22.8k
      symbolS *add_symbol, *op_symbol;
1366
22.8k
      offsetT left, right;
1367
22.8k
      segT seg_left, seg_right;
1368
22.8k
      operatorT op;
1369
22.8k
      int move_seg_ok;
1370
1371
22.8k
      symp->flags.resolving = 1;
1372
1373
      /* Help out with CSE.  */
1374
22.8k
      add_symbol = symp->x->value.X_add_symbol;
1375
22.8k
      op_symbol = symp->x->value.X_op_symbol;
1376
22.8k
      final_val = symp->x->value.X_add_number;
1377
22.8k
      op = symp->x->value.X_op;
1378
1379
22.8k
      switch (op)
1380
22.8k
  {
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
22.1k
  case O_constant:
1429
    /* Symbols whose section has SEC_ELF_OCTETS set,
1430
       resolve to octets instead of target bytes. */
1431
22.1k
    if (symp->bsym->section->flags & SEC_OCTETS)
1432
4
      final_val += symp->frag->fr_address;
1433
22.1k
    else
1434
22.1k
      final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1435
22.1k
    if (final_seg == expr_section)
1436
1
      final_seg = absolute_section;
1437
    /* Fall through.  */
1438
1439
22.1k
  case O_register:
1440
22.1k
    resolved = 1;
1441
22.1k
    break;
1442
1443
109
  case O_symbol:
1444
109
  case O_symbol_rva:
1445
109
  case O_secidx:
1446
109
    left = resolve_symbol_value (add_symbol);
1447
109
    seg_left = S_GET_SEGMENT (add_symbol);
1448
109
    if (finalize_syms)
1449
0
      symp->x->value.X_op_symbol = NULL;
1450
1451
121
  do_symbol:
1452
121
    if (S_IS_WEAKREFR (symp))
1453
85
      {
1454
85
        gas_assert (final_val == 0);
1455
85
        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
85
      }
1464
1465
121
    if (symp->flags.mri_common)
1466
3
      {
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
3
        resolved = symbol_resolved_p (add_symbol);
1471
3
        break;
1472
3
      }
1473
1474
    /* Don't leave symbol loops.  */
1475
118
    if (finalize_syms
1476
0
        && !add_symbol->flags.local_symbol
1477
0
        && add_symbol->flags.resolving)
1478
0
      break;
1479
1480
118
    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
118
        )
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
118
    if (seg_left == undefined_section
1504
98
        || bfd_is_com_section (seg_left)
1505
#if defined (OBJ_COFF) && defined (TE_PE)
1506
        || S_IS_WEAK (add_symbol)
1507
#endif
1508
98
        || (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
20
      {
1514
20
        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
20
        final_seg = seg_left;
1523
20
        final_val += symp->frag->fr_address + left;
1524
20
        resolved = symbol_resolved_p (add_symbol);
1525
20
        symp->flags.resolving = 0;
1526
1527
20
        if (op == O_secidx && seg_left != undefined_section)
1528
0
    {
1529
0
      final_val = 0;
1530
0
      break;
1531
0
    }
1532
1533
20
        goto exit_dont_set_value;
1534
20
      }
1535
98
    else
1536
98
      {
1537
98
        final_val += symp->frag->fr_address + left;
1538
98
        if (final_seg == expr_section || final_seg == undefined_section)
1539
6
    final_seg = seg_left;
1540
98
      }
1541
1542
98
    resolved = symbol_resolved_p (add_symbol);
1543
98
    if (S_IS_WEAKREFR (symp))
1544
85
      {
1545
85
        symp->flags.resolving = 0;
1546
85
        goto exit_dont_set_value;
1547
85
      }
1548
13
    break;
1549
1550
13
  case O_uminus:
1551
0
  case O_bit_not:
1552
0
  case O_logical_not:
1553
0
    left = resolve_symbol_value (add_symbol);
1554
0
    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
0
    if (op != O_logical_not && seg_left != absolute_section
1561
0
        && finalize_syms)
1562
0
      report_op_error (symp, NULL, op, add_symbol);
1563
1564
0
    if (final_seg == expr_section || final_seg == undefined_section)
1565
0
      final_seg = absolute_section;
1566
1567
0
    if (op == O_uminus)
1568
0
      left = -(valueT) left;
1569
0
    else if (op == O_logical_not)
1570
0
      left = !left;
1571
0
    else
1572
0
      left = ~left;
1573
1574
0
    final_val += left + symp->frag->fr_address;
1575
1576
0
    resolved = symbol_resolved_p (add_symbol);
1577
0
    break;
1578
1579
0
  case O_multiply:
1580
0
  case O_divide:
1581
0
  case O_modulus:
1582
0
  case O_left_shift:
1583
0
  case O_right_shift:
1584
0
  case O_bit_inclusive_or:
1585
0
  case O_bit_or_not:
1586
0
  case O_bit_exclusive_or:
1587
0
  case O_bit_and:
1588
6
  case O_add:
1589
216
  case O_subtract:
1590
270
  case O_eq:
1591
277
  case O_ne:
1592
277
  case O_lt:
1593
437
  case O_le:
1594
437
  case O_ge:
1595
584
  case O_gt:
1596
584
  case O_logical_and:
1597
584
  case O_logical_or:
1598
584
    left = resolve_symbol_value (add_symbol);
1599
584
    right = resolve_symbol_value (op_symbol);
1600
584
    seg_left = S_GET_SEGMENT (add_symbol);
1601
584
    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
584
    if (op == O_add)
1606
6
      {
1607
6
        if (seg_right == absolute_section)
1608
6
    {
1609
6
      final_val += right;
1610
6
      goto do_symbol;
1611
6
    }
1612
0
        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
6
      }
1621
578
    else if (op == O_subtract)
1622
210
      {
1623
210
        if (seg_right == absolute_section)
1624
6
    {
1625
6
      final_val -= right;
1626
6
      goto do_symbol;
1627
6
    }
1628
210
      }
1629
1630
572
    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
572
    if (!(seg_left == absolute_section
1639
239
    && seg_right == absolute_section)
1640
558
        && !(op == O_eq || op == O_ne)
1641
507
        && !((op == O_subtract
1642
303
        || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1643
507
       && seg_left == seg_right
1644
20
       && (seg_left != undefined_section
1645
19
           || add_symbol == op_symbol)))
1646
506
      {
1647
        /* Don't emit messages unless we're finalizing the symbol value,
1648
     otherwise we may get the same message multiple times.  */
1649
506
        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
506
        else
1656
506
    move_seg_ok = 0;
1657
506
      }
1658
1659
572
    if (move_seg_ok
1660
66
        && (final_seg == expr_section || final_seg == undefined_section))
1661
20
      final_seg = absolute_section;
1662
1663
    /* Check for division by zero.  */
1664
572
    if ((op == O_divide || op == O_modulus) && right == 0)
1665
0
      {
1666
        /* If seg_right is not absolute_section, then we've
1667
     already issued a warning about using a bad symbol.  */
1668
0
        if (seg_right == absolute_section && finalize_syms)
1669
0
    {
1670
0
      const char *file;
1671
0
      unsigned int line;
1672
1673
0
      if (expr_symbol_where (symp, &file, &line))
1674
0
        as_bad_where (file, line, _("division by zero"));
1675
0
      else
1676
0
        as_bad (_("division by zero when setting `%s'"),
1677
0
          S_GET_NAME (symp));
1678
0
    }
1679
1680
0
        right = 1;
1681
0
      }
1682
572
    if ((op == O_left_shift || op == O_right_shift)
1683
0
        && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1684
0
      {
1685
0
        as_warn_value_out_of_range (_("shift count"), right, 0,
1686
0
            sizeof (valueT) * CHAR_BIT - 1,
1687
0
            NULL, 0);
1688
0
        left = right = 0;
1689
0
      }
1690
1691
572
    switch (symp->x->value.X_op)
1692
572
      {
1693
      /* See expr() for reasons of the use of valueT casts here.  */
1694
0
      case O_multiply:    left *= (valueT) right; break;
1695
1696
      /* See expr() for reasons of the special casing.  */
1697
0
      case O_divide:
1698
0
        if (right == 1)
1699
0
    break;
1700
0
        if (right == -1)
1701
0
    {
1702
0
      left = -(valueT) left;
1703
0
      break;
1704
0
    }
1705
0
        left /= right;
1706
0
        break;
1707
1708
      /* Again, see expr() for reasons of the special casing.  */
1709
0
      case O_modulus:
1710
0
        if (right == 1 || right == -1)
1711
0
    left = 0;
1712
0
        else
1713
0
    left %= right;
1714
0
        break;
1715
1716
0
      case O_left_shift:
1717
0
        left = (valueT) left << (valueT) right; break;
1718
0
      case O_right_shift:
1719
0
        left = (valueT) left >> (valueT) right; break;
1720
0
      case O_bit_inclusive_or:  left |= right; break;
1721
0
      case O_bit_or_not:    left |= ~right; break;
1722
0
      case O_bit_exclusive_or:  left ^= right; break;
1723
0
      case O_bit_and:   left &= right; break;
1724
0
      case O_add:     left += (valueT) right; break;
1725
204
      case O_subtract:    left -= (valueT) right; break;
1726
54
      case O_eq:
1727
61
      case O_ne:
1728
61
        left = (left == right && seg_left == seg_right
1729
0
          && (seg_left != undefined_section
1730
0
        || add_symbol == op_symbol)
1731
61
          ? ~ (offsetT) 0 : 0);
1732
61
        if (symp->x->value.X_op == O_ne)
1733
7
    left = ~left;
1734
61
        break;
1735
0
      case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
1736
160
      case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
1737
0
      case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
1738
147
      case O_gt:  left = left >  right ? ~ (offsetT) 0 : 0; break;
1739
0
      case O_logical_and: left = left && right; break;
1740
0
      case O_logical_or:  left = left || right; break;
1741
1742
0
      case O_illegal:
1743
0
      case O_absent:
1744
0
      case O_constant:
1745
        /* See PR 20895 for a reproducer.  */
1746
0
        as_bad (_("Invalid operation on symbol"));
1747
0
        goto exit_dont_set_value;
1748
        
1749
0
      default:
1750
0
        abort ();
1751
572
      }
1752
1753
572
    final_val += symp->frag->fr_address + left;
1754
572
    if (final_seg == expr_section || final_seg == undefined_section)
1755
506
      {
1756
506
        if (seg_left == undefined_section
1757
203
      || seg_right == undefined_section)
1758
506
    final_seg = undefined_section;
1759
0
        else if (seg_left == absolute_section)
1760
0
    final_seg = seg_right;
1761
0
        else
1762
0
    final_seg = seg_left;
1763
506
      }
1764
572
    resolved = (symbol_resolved_p (add_symbol)
1765
0
          && symbol_resolved_p (op_symbol));
1766
572
    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
22.8k
  }
1777
1778
22.7k
      symp->flags.resolving = 0;
1779
22.7k
    }
1780
1781
22.8k
  if (finalize_syms)
1782
0
    S_SET_VALUE (symp, final_val);
1783
1784
22.9k
 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
22.9k
    S_SET_SEGMENT (symp, final_seg);
1788
1789
  /* Don't worry if we can't resolve an expr_section symbol.  */
1790
22.9k
  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
22.9k
  return final_val;
1803
22.8k
}
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
52.8k
{
1831
52.8k
  symbolS *symbolP = *symbolPP;
1832
1833
52.8k
  if (symbolP->flags.local_symbol)
1834
46
    {
1835
46
      struct local_symbol *locsym = (struct local_symbol *) symbolP;
1836
1837
46
      *valueP = locsym->value;
1838
46
      *segP = locsym->section;
1839
46
      *fragPP = locsym->frag;
1840
46
    }
1841
52.7k
  else
1842
52.7k
    {
1843
52.7k
      expressionS exp = symbolP->x->value;
1844
1845
52.7k
      if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1846
52.7k
  {
1847
52.7k
    int resolved;
1848
1849
52.7k
    if (symbolP->flags.resolving)
1850
0
      return 0;
1851
52.7k
    symbolP->flags.resolving = 1;
1852
52.7k
    resolved = resolve_expression (&exp);
1853
52.7k
    symbolP->flags.resolving = 0;
1854
52.7k
    if (!resolved)
1855
6.07k
      return 0;
1856
1857
46.6k
    switch (exp.X_op)
1858
46.6k
      {
1859
46.0k
      case O_constant:
1860
46.0k
      case O_register:
1861
46.0k
        if (!symbol_equated_p (symbolP))
1862
46.0k
    break;
1863
        /* Fallthru.  */
1864
634
      case O_symbol:
1865
634
      case O_symbol_rva:
1866
634
        symbolP = exp.X_add_symbol;
1867
634
        break;
1868
0
      default:
1869
0
        return 0;
1870
46.6k
      }
1871
46.6k
  }
1872
1873
46.6k
      *symbolPP = symbolP;
1874
46.6k
      *valueP = exp.X_add_number;
1875
1876
      /* We may have picked up a local symbol above: Check again.  */
1877
46.6k
      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
46.6k
      else
1899
46.6k
  {
1900
46.6k
    *segP = symbolP->bsym->section;
1901
46.6k
    *fragPP = symbolP->frag;
1902
46.6k
  }
1903
1904
46.6k
      if (*segP == expr_section)
1905
105
  switch (exp.X_op)
1906
105
    {
1907
99
    case O_constant: *segP = absolute_section; break;
1908
6
    case O_register: *segP = reg_section; break;
1909
0
    default: break;
1910
105
    }
1911
46.6k
    }
1912
1913
46.7k
  return 1;
1914
52.8k
}
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
18.7k
#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
281
{
2071
281
  memset (fb_low_counter, 0, sizeof (fb_low_counter));
2072
281
}
2073
2074
/* Add one to the instance number of this fb label.  */
2075
2076
void
2077
fb_label_instance_inc (unsigned int label)
2078
8.26k
{
2079
8.26k
  fb_ent *i;
2080
2081
8.26k
  if (label < FB_LABEL_SPECIAL)
2082
7.16k
    {
2083
7.16k
      ++fb_low_counter[label];
2084
7.16k
      return;
2085
7.16k
    }
2086
2087
1.09k
  if (fb_labels != NULL)
2088
1.09k
    {
2089
1.09k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2090
4.13k
     i < fb_labels + fb_label_count; ++i)
2091
4.12k
  {
2092
4.12k
    if (*i == label)
2093
1.07k
      {
2094
1.07k
        ++fb_label_instances[i - fb_labels];
2095
1.07k
        return;
2096
1.07k
      }      /* if we find it  */
2097
4.12k
  }      /* for each existing label  */
2098
1.09k
    }
2099
2100
  /* If we get to here, we don't have label listed yet.  */
2101
2102
18
  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
17
  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
18
  fb_labels[fb_label_count] = label;
2119
18
  fb_label_instances[fb_label_count] = 1;
2120
18
  ++fb_label_count;
2121
18
}
2122
2123
static unsigned int
2124
fb_label_instance (unsigned int label)
2125
8.27k
{
2126
8.27k
  fb_ent *i;
2127
2128
8.27k
  if (label < FB_LABEL_SPECIAL)
2129
7.18k
    return (fb_low_counter[label]);
2130
2131
1.09k
  if (fb_labels != NULL)
2132
1.09k
    {
2133
1.09k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2134
4.16k
     i < fb_labels + fb_label_count; ++i)
2135
4.15k
  {
2136
4.15k
    if (*i == label)
2137
1.09k
      return (fb_label_instances[i - fb_labels]);
2138
4.15k
  }
2139
1.09k
    }
2140
2141
  /* We didn't find the label, so this must be a reference to the
2142
     first instance.  */
2143
4
  return 0;
2144
1.09k
}
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
8.27k
{
2162
  /* Returned to caller, then copied.  Used for created names ("4f").  */
2163
8.27k
  static char symbol_name_build[24];
2164
8.27k
  char *p = symbol_name_build;
2165
2166
#ifdef TC_MMIX
2167
  know (augend <= 2 /* See mmix_fb_label.  */);
2168
#else
2169
8.27k
  know (augend <= 1);
2170
8.27k
#endif
2171
2172
8.27k
#ifdef LOCAL_LABEL_PREFIX
2173
8.27k
  *p++ = LOCAL_LABEL_PREFIX;
2174
8.27k
#endif
2175
8.27k
  sprintf (p, "L%u%c%u",
2176
8.27k
     n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2177
8.27k
  return symbol_name_build;
2178
8.27k
}
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
10.0k
{
2228
10.0k
  if (s->flags.local_symbol)
2229
42
    return resolve_symbol_value (s);
2230
2231
10.0k
  if (!s->flags.resolved)
2232
10.0k
    {
2233
10.0k
      valueT val = resolve_symbol_value (s);
2234
10.0k
      if (!finalize_syms)
2235
10.0k
  return val;
2236
10.0k
    }
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
10.0k
{
2262
10.0k
  return S_GET_VALUE_WHERE (s, NULL, 0);
2263
10.0k
}
2264
2265
/* Set the value of a symbol.  */
2266
2267
void
2268
S_SET_VALUE (symbolS *s, valueT val)
2269
396k
{
2270
396k
  if (s->flags.local_symbol)
2271
9
    {
2272
9
      ((struct local_symbol *) s)->value = val;
2273
9
      return;
2274
9
    }
2275
2276
396k
  s->x->value.X_op = O_constant;
2277
396k
  s->x->value.X_add_number = (offsetT) val;
2278
396k
  s->x->value.X_unsigned = 0;
2279
396k
  S_CLEAR_WEAKREFR (s);
2280
396k
}
2281
2282
void
2283
copy_symbol_attributes (symbolS *dest, symbolS *src)
2284
4.00k
{
2285
4.00k
  if (dest->flags.local_symbol)
2286
0
    dest = local_symbol_convert (dest);
2287
4.00k
  if (src->flags.local_symbol)
2288
3
    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
4.00k
#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2293
4.00k
       | BSF_GNU_INDIRECT_FUNCTION)
2294
4.00k
  dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2295
2296
4.00k
#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2297
4.00k
  OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2298
4.00k
#endif
2299
2300
#ifdef TC_COPY_SYMBOL_ATTRIBUTES
2301
  TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2302
#endif
2303
4.00k
}
2304
2305
int
2306
S_IS_FUNCTION (const symbolS *s)
2307
1.31k
{
2308
1.31k
  flagword flags;
2309
2310
1.31k
  if (s->flags.local_symbol)
2311
0
    return 0;
2312
2313
1.31k
  flags = s->bsym->flags;
2314
2315
1.31k
  return (flags & BSF_FUNCTION) != 0;
2316
1.31k
}
2317
2318
int
2319
S_IS_EXTERNAL (const symbolS *s)
2320
24
{
2321
24
  flagword flags;
2322
2323
24
  if (s->flags.local_symbol)
2324
0
    return 0;
2325
2326
24
  flags = s->bsym->flags;
2327
2328
  /* Sanity check.  */
2329
24
  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2330
0
    abort ();
2331
2332
24
  return (flags & BSF_GLOBAL) != 0;
2333
24
}
2334
2335
int
2336
S_IS_WEAK (const symbolS *s)
2337
19
{
2338
19
  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
19
  if (S_IS_WEAKREFR (s))
2345
0
    return S_IS_WEAK (s->x->value.X_add_symbol);
2346
19
  return (s->bsym->flags & BSF_WEAK) != 0;
2347
19
}
2348
2349
int
2350
S_IS_WEAKREFR (const symbolS *s)
2351
149k
{
2352
149k
  if (s->flags.local_symbol)
2353
0
    return 0;
2354
149k
  return s->flags.weakrefr != 0;
2355
149k
}
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
8.57k
{
2368
8.57k
  if (s->flags.local_symbol)
2369
0
    return 0;
2370
8.57k
  return bfd_is_com_section (s->bsym->section);
2371
8.57k
}
2372
2373
int
2374
S_IS_DEFINED (const symbolS *s)
2375
18.8k
{
2376
18.8k
  if (s->flags.local_symbol)
2377
17
    return ((const struct local_symbol *) s)->section != undefined_section;
2378
18.8k
  return s->bsym->section != undefined_section;
2379
18.8k
}
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
203
{
2392
203
  segT sec;
2393
203
  if (s->flags.local_symbol)
2394
0
    sec = ((const struct local_symbol *) s)->section;
2395
203
  else
2396
203
    {
2397
203
      if ((strict
2398
0
     && ((s->bsym->flags & BSF_WEAK) != 0
2399
0
         || (EXTERN_FORCE_RELOC
2400
0
       && (s->bsym->flags & BSF_GLOBAL) != 0)))
2401
203
    || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2402
0
  return true;
2403
203
      sec = s->bsym->section;
2404
203
    }
2405
203
  return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2406
203
}
2407
2408
int
2409
S_IS_DEBUG (const symbolS *s)
2410
7
{
2411
7
  if (s->flags.local_symbol)
2412
0
    return 0;
2413
7
  if (s->bsym->flags & BSF_DEBUGGING)
2414
0
    return 1;
2415
7
  return 0;
2416
7
}
2417
2418
int
2419
S_IS_LOCAL (const symbolS *s)
2420
3
{
2421
3
  flagword flags;
2422
3
  const char *name;
2423
2424
3
  if (s->flags.local_symbol)
2425
3
    return 1;
2426
2427
0
  if (S_IS_EXTERNAL (s))
2428
0
    return 0;
2429
2430
0
  if (bfd_asymbol_section (s->bsym) == reg_section)
2431
0
    return 1;
2432
2433
0
  flags = s->bsym->flags;
2434
2435
0
  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
0
  name = S_GET_NAME (s);
2443
0
  return (name != NULL
2444
0
    && ! S_IS_DEBUG (s)
2445
0
    && (strchr (name, DOLLAR_LABEL_CHAR)
2446
0
        || 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
0
        || (! flag_keep_locals
2452
0
      && (bfd_is_local_label (stdoutput, s->bsym)
2453
0
          || (flag_mri
2454
0
        && name[0] == '?'
2455
0
        && name[1] == '?')))));
2456
0
}
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
735
{
2467
735
  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
735
  return s->x->value.X_op == O_register;
2472
735
}
2473
2474
int
2475
S_IS_VOLATILE (const symbolS *s)
2476
23.5k
{
2477
23.5k
  if (s->flags.local_symbol)
2478
0
    return 0;
2479
23.5k
  return s->flags.volatil;
2480
23.5k
}
2481
2482
int
2483
S_IS_FORWARD_REF (const symbolS *s)
2484
13.1k
{
2485
13.1k
  if (s->flags.local_symbol)
2486
0
    return 0;
2487
13.1k
  return s->flags.forward_ref;
2488
13.1k
}
2489
2490
const char *
2491
S_GET_NAME (const symbolS *s)
2492
1.15k
{
2493
1.15k
  return s->name;
2494
1.15k
}
2495
2496
segT
2497
S_GET_SEGMENT (const symbolS *s)
2498
201k
{
2499
201k
  if (s->flags.local_symbol)
2500
3.29k
    return ((const struct local_symbol *) s)->section;
2501
198k
  return s->bsym->section;
2502
201k
}
2503
2504
void
2505
S_SET_SEGMENT (symbolS *s, segT seg)
2506
388k
{
2507
388k
  if (s->flags.local_symbol)
2508
1
    {
2509
1
      ((struct local_symbol *) s)->section = seg;
2510
1
      return;
2511
1
    }
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
388k
  if (s->bsym->flags & BSF_SECTION_SYM)
2517
0
    {
2518
0
      if (s->bsym->section != seg)
2519
0
  abort ();
2520
0
    }
2521
388k
  else
2522
388k
    {
2523
388k
      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
388k
      s->bsym->section = seg;
2536
388k
    }
2537
388k
}
2538
2539
void
2540
S_SET_EXTERNAL (symbolS *s)
2541
420
{
2542
420
  if (s->flags.local_symbol)
2543
8
    s = local_symbol_convert (s);
2544
420
  if ((s->bsym->flags & BSF_WEAK) != 0)
2545
0
    {
2546
      /* Let .weak override .global.  */
2547
0
      return;
2548
0
    }
2549
420
  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
420
#ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2556
420
  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
420
#endif
2562
420
  s->bsym->flags |= BSF_GLOBAL;
2563
420
  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
420
}
2570
2571
void
2572
S_CLEAR_EXTERNAL (symbolS *s)
2573
12.6k
{
2574
12.6k
  if (s->flags.local_symbol)
2575
0
    return;
2576
12.6k
  if ((s->bsym->flags & BSF_WEAK) != 0)
2577
30
    {
2578
      /* Let .weak override.  */
2579
30
      return;
2580
30
    }
2581
12.6k
  s->bsym->flags |= BSF_LOCAL;
2582
12.6k
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2583
12.6k
}
2584
2585
void
2586
S_SET_WEAK (symbolS *s)
2587
10
{
2588
10
  if (s->flags.local_symbol)
2589
0
    s = local_symbol_convert (s);
2590
#ifdef obj_set_weak_hook
2591
  obj_set_weak_hook (s);
2592
#endif
2593
10
  s->bsym->flags |= BSF_WEAK;
2594
10
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2595
10
}
2596
2597
void
2598
S_SET_WEAKREFR (symbolS *s)
2599
2
{
2600
2
  if (s->flags.local_symbol)
2601
0
    s = local_symbol_convert (s);
2602
2
  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
2
  if (s->flags.used)
2610
1
    symbol_mark_used (s->x->value.X_add_symbol);
2611
2
}
2612
2613
void
2614
S_CLEAR_WEAKREFR (symbolS *s)
2615
808k
{
2616
808k
  if (s->flags.local_symbol)
2617
0
    return;
2618
808k
  s->flags.weakrefr = 0;
2619
808k
}
2620
2621
void
2622
S_SET_WEAKREFD (symbolS *s)
2623
1
{
2624
1
  if (s->flags.local_symbol)
2625
0
    s = local_symbol_convert (s);
2626
1
  s->flags.weakrefd = 1;
2627
1
  S_SET_WEAK (s);
2628
1
}
2629
2630
void
2631
S_CLEAR_WEAKREFD (symbolS *s)
2632
111k
{
2633
111k
  if (s->flags.local_symbol)
2634
1.60k
    return;
2635
109k
  if (s->flags.weakrefd)
2636
0
    {
2637
0
      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
0
      if (s->bsym->flags & BSF_WEAK)
2644
0
  {
2645
#ifdef obj_clear_weak_hook
2646
    obj_clear_weak_hook (s);
2647
#endif
2648
0
    s->bsym->flags &= ~BSF_WEAK;
2649
0
    s->bsym->flags |= BSF_LOCAL;
2650
0
  }
2651
0
    }
2652
109k
}
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
7.89k
{
2684
7.89k
  if (s->flags.local_symbol)
2685
3
    s = local_symbol_convert (s);
2686
7.89k
  s->flags.volatil = 1;
2687
7.89k
}
2688
2689
void
2690
S_CLEAR_VOLATILE (symbolS *s)
2691
4
{
2692
4
  if (!s->flags.local_symbol)
2693
4
    s->flags.volatil = 0;
2694
4
}
2695
2696
void
2697
S_SET_FORWARD_REF (symbolS *s)
2698
32
{
2699
32
  if (s->flags.local_symbol)
2700
3
    s = local_symbol_convert (s);
2701
32
  s->flags.forward_ref = 1;
2702
32
}
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
1.13k
{
2719
1.13k
  if (s->flags.local_symbol)
2720
0
    abort ();
2721
1.13k
  return s->x->next;
2722
1.13k
}
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
22.5k
{
2729
22.5k
  if (s->flags.local_symbol)
2730
1
    s = local_symbol_convert (s);
2731
22.5k
  return &s->x->value;
2732
22.5k
}
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
33.5k
{
2739
33.5k
  if (s->flags.local_symbol)
2740
0
    s = local_symbol_convert (s);
2741
33.5k
  s->x->value = *exp;
2742
33.5k
  S_CLEAR_WEAKREFR (s);
2743
33.5k
}
2744
2745
/* Return whether 2 symbols are the same.  */
2746
2747
int
2748
symbol_same_p (const symbolS *s1, const symbolS *s2)
2749
12.7k
{
2750
12.7k
  return s1 == s2;
2751
12.7k
}
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
0
{
2758
0
  if (s->flags.local_symbol)
2759
0
    return (offsetT *) &((struct local_symbol *) s)->value;
2760
2761
0
  return &s->x->value.X_add_number;
2762
0
}
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
354k
{
2769
354k
  S_SET_SEGMENT (sym, now_seg);
2770
354k
  S_SET_VALUE (sym, frag_now_fix ());
2771
354k
  symbol_set_frag (sym, frag_now);
2772
354k
}
2773
2774
/* Set the frag of a symbol.  */
2775
2776
void
2777
symbol_set_frag (symbolS *s, fragS *f)
2778
362k
{
2779
362k
  if (s->flags.local_symbol)
2780
3
    {
2781
3
      ((struct local_symbol *) s)->frag = f;
2782
3
      return;
2783
3
    }
2784
362k
  s->frag = f;
2785
362k
  S_CLEAR_WEAKREFR (s);
2786
362k
}
2787
2788
/* Return the frag of a symbol.  */
2789
2790
fragS *
2791
symbol_get_frag (const symbolS *s)
2792
13.1k
{
2793
13.1k
  if (s->flags.local_symbol)
2794
32
    return ((struct local_symbol *) s)->frag;
2795
13.0k
  return s->frag;
2796
13.1k
}
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
4.83k
{
2802
4.83k
  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
4.83k
  gas_assert (s->x->value.X_op == O_constant);
2811
4.83k
  *value = s->x->value.X_add_number;
2812
4.83k
  return s->frag;
2813
4.83k
}
2814
2815
/* Mark a symbol as having been used.  */
2816
2817
void
2818
symbol_mark_used (symbolS *s)
2819
152k
{
2820
152k
  if (s->flags.local_symbol)
2821
3.26k
    return;
2822
149k
  s->flags.used = 1;
2823
149k
  if (S_IS_WEAKREFR (s))
2824
5
    symbol_mark_used (s->x->value.X_add_symbol);
2825
149k
}
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
28
{
2852
28
  if (s->flags.local_symbol)
2853
0
    s = local_symbol_convert (s);
2854
28
  s->flags.used_in_reloc = 1;
2855
28
}
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
693
{
2970
693
  return s->flags.resolved;
2971
693
}
2972
2973
/* Mark a symbol as being resolved.  */
2974
2975
void
2976
symbol_mark_resolving (symbolS *s)
2977
7.92k
{
2978
7.92k
  s->flags.resolving = 1;
2979
7.92k
}
2980
2981
void
2982
symbol_clear_resolving (symbolS *s)
2983
7.92k
{
2984
7.92k
  s->flags.resolving = 0;
2985
7.92k
}
2986
2987
/* Return whether a symbol is being resolved.  */
2988
2989
int
2990
symbol_resolving_p (const symbolS *s)
2991
24.4k
{
2992
24.4k
  return s->flags.resolving;
2993
24.4k
}
2994
2995
/* Return whether a symbol is a section symbol.  */
2996
2997
int
2998
symbol_section_p (const symbolS *s)
2999
38.0k
{
3000
38.0k
  if (s->flags.local_symbol)
3001
0
    return 0;
3002
38.0k
  return (s->bsym->flags & BSF_SECTION_SYM) != 0;
3003
38.0k
}
3004
3005
/* Return whether a symbol is equated to another symbol.  */
3006
3007
int
3008
symbol_equated_p (const symbolS *s)
3009
80.1k
{
3010
80.1k
  if (s->flags.local_symbol)
3011
32
    return 0;
3012
80.1k
  return s->x->value.X_op == O_symbol;
3013
80.1k
}
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
30.4k
{
3042
30.4k
  valueT add = 0;
3043
30.4k
  symbolS *ret = sym;
3044
32.7k
  while (ret && symbol_equated_p (ret))
3045
2.27k
    {
3046
2.27k
      const expressionS *e = symbol_get_value_expression (ret);
3047
2.27k
      add += e->X_add_number;
3048
2.27k
      ret->flags.resolving = 1;
3049
2.27k
      ret = e->X_add_symbol;
3050
2.27k
      if (ret->flags.resolving)
3051
1
  ret = NULL;
3052
2.27k
    }
3053
32.7k
  while (sym && sym->flags.resolving)
3054
2.27k
    {
3055
2.27k
      const expressionS *e = symbol_get_value_expression (sym);
3056
2.27k
      sym->flags.resolving = 0;
3057
2.27k
      sym = e->X_add_symbol;
3058
2.27k
    }
3059
30.4k
  *off = add;
3060
30.4k
  return ret;
3061
30.4k
}
3062
3063
/* Return whether a symbol has a constant value.  */
3064
3065
int
3066
symbol_constant_p (const symbolS *s)
3067
95
{
3068
95
  if (s->flags.local_symbol)
3069
0
    return 1;
3070
95
  return s->x->value.X_op == O_constant;
3071
95
}
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
26.5k
{
3099
26.5k
  if (s->flags.local_symbol)
3100
2
    s = local_symbol_convert (s);
3101
26.5k
  return s->bsym;
3102
26.5k
}
3103
3104
/* Set the BFD symbol for a symbol.  */
3105
3106
void
3107
symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3108
1.85k
{
3109
1.85k
  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.85k
  if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3119
1.85k
    s->bsym = bsym;
3120
  /* else XXX - What do we do now ?  */
3121
1.85k
}
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
46.8k
{
3130
46.8k
  if (s->flags.local_symbol)
3131
0
    s = local_symbol_convert (s);
3132
46.8k
  return &s->x->obj;
3133
46.8k
}
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
281
{
3174
281
  symbol_lastP = NULL;
3175
281
  symbol_rootP = NULL;   /* In case we have 0 symbols (!!)  */
3176
281
  sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3177
281
             NULL, xcalloc, free);
3178
3179
281
#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3180
281
  abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3181
281
#endif
3182
281
  abs_symbol.x = &abs_symbol_x;
3183
281
  abs_symbol.x->value.X_op = O_constant;
3184
281
  abs_symbol.frag = &zero_address_frag;
3185
3186
281
  if (LOCAL_LABELS_FB)
3187
281
    fb_label_init ();
3188
281
}
3189
3190
void
3191
symbol_end (void)
3192
281
{
3193
281
  if (ENABLE_LEAK_CHECK)
3194
281
    htab_delete (sy_hash);
3195
281
}
3196
3197
void
3198
dot_symbol_init (void)
3199
281
{
3200
281
  dot_symbol.name = ".";
3201
281
  dot_symbol.flags.forward_ref = 1;
3202
281
  dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3203
281
  if (dot_symbol.bsym == NULL)
3204
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3205
281
  dot_symbol.bsym->name = ".";
3206
281
  dot_symbol.x = &dot_symbol_x;
3207
281
  dot_symbol.x->value.X_op = O_constant;
3208
281
}
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