Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/bfd/coffgen.c
Line
Count
Source (jump to first uncovered line)
1
/* Support for the generic parts of COFF, for BFD.
2
   Copyright (C) 1990-2025 Free Software Foundation, Inc.
3
   Written by Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
/* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
23
   Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
24
25
/* This file contains COFF code that is not dependent on any
26
   particular COFF target.  There is only one version of this file in
27
   libbfd.a, so no target specific code may be put in here.  Or, to
28
   put it another way,
29
30
   ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31
32
   If you need to add some target specific behaviour, add a new hook
33
   function to bfd_coff_backend_data.
34
35
   Some of these functions are also called by the ECOFF routines.
36
   Those functions may not use any COFF specific information, such as
37
   coff_data (abfd).  */
38
39
#include "sysdep.h"
40
#include <limits.h>
41
#include "bfd.h"
42
#include "libbfd.h"
43
#include "coff/internal.h"
44
#include "libcoff.h"
45
#include "elf-bfd.h"
46
#include "hashtab.h"
47
#include "safe-ctype.h"
48
49
/* Extract a long section name at STRINDEX and copy it to the bfd objstack.
50
   Return NULL in case of error.  */
51
52
static char *
53
extract_long_section_name(bfd *abfd, unsigned long strindex)
54
33.1k
{
55
33.1k
  const char *strings;
56
33.1k
  char *name;
57
58
33.1k
  strings = _bfd_coff_read_string_table (abfd);
59
33.1k
  if (strings == NULL)
60
7.80k
    return NULL;
61
25.3k
  if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
62
1.58k
    return NULL;
63
23.7k
  strings += strindex;
64
23.7k
  name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
65
23.7k
  if (name == NULL)
66
0
    return NULL;
67
23.7k
  strcpy (name, strings);
68
69
23.7k
  return name;
70
23.7k
}
71
72
/* Decode a base 64 coded string at STR of length LEN, and write the result
73
   to RES.  Return true on success.
74
   Return false in case of invalid character or overflow.  */
75
76
static bool
77
decode_base64 (const char *str, unsigned len, uint32_t *res)
78
10.6k
{
79
10.6k
  unsigned i;
80
10.6k
  uint32_t val;
81
82
10.6k
  val = 0;
83
47.6k
  for (i = 0; i < len; i++)
84
46.4k
    {
85
46.4k
      char c = str[i];
86
46.4k
      unsigned d;
87
88
46.4k
      if (c >= 'A' && c <= 'Z')
89
6.11k
  d = c - 'A';
90
40.3k
      else if (c >= 'a' && c <= 'z')
91
2.47k
  d = c - 'a' + 26;
92
37.8k
      else if (c >= '0' && c <= '9')
93
3.85k
  d = c - '0' + 52;
94
33.9k
      else if (c == '+')
95
1.11k
  d = 62;
96
32.8k
      else if (c == '/')
97
26.3k
  d = 63;
98
6.50k
      else
99
6.50k
  return false;
100
101
      /* Check for overflow. */
102
39.9k
      if ((val >> 26) != 0)
103
2.91k
  return false;
104
105
37.0k
      val = (val << 6) + d;
106
37.0k
    }
107
108
1.20k
  *res = val;
109
1.20k
  return true;
110
10.6k
}
111
112
/* Take a section header read from a coff file (in HOST byte order),
113
   and make a BFD "section" out of it.  This is used by ECOFF.  */
114
115
static bool
116
make_a_section_from_file (bfd *abfd,
117
        struct internal_scnhdr *hdr,
118
        unsigned int target_index)
119
90.9M
{
120
90.9M
  asection *newsect;
121
90.9M
  char *name;
122
90.9M
  bool result = true;
123
90.9M
  flagword flags;
124
125
90.9M
  name = NULL;
126
127
  /* Handle long section names as in PE.  On reading, we want to
128
    accept long names if the format permits them at all, regardless
129
    of the current state of the flag that dictates if we would generate
130
    them in outputs; this construct checks if that is the case by
131
    attempting to set the flag, without changing its state; the call
132
    will fail for formats that do not support long names at all.  */
133
90.9M
  if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
134
90.9M
      && hdr->s_name[0] == '/')
135
63.9k
    {
136
      /* Flag that this BFD uses long names, even though the format might
137
   expect them to be off by default.  This won't directly affect the
138
   format of any output BFD created from this one, but the information
139
   can be used to decide what to do.  */
140
63.9k
      bfd_coff_set_long_section_names (abfd, true);
141
142
63.9k
      if (hdr->s_name[1] == '/')
143
10.6k
  {
144
    /* LLVM extension: the '/' is followed by another '/' and then by
145
       the index in the strtab encoded in base64 without NUL at the
146
       end.  */
147
10.6k
    uint32_t strindex;
148
149
    /* Decode the index.  No overflow is expected as the string table
150
       length is at most 2^32 - 1 (the length is written on the first
151
       four bytes).
152
       Also, contrary to RFC 4648, all the characters must be decoded,
153
       there is no padding.  */
154
10.6k
    if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
155
9.41k
      return false;
156
157
1.20k
    name = extract_long_section_name (abfd, strindex);
158
1.20k
    if (name == NULL)
159
1.14k
      return false;
160
1.20k
  }
161
53.3k
      else
162
53.3k
  {
163
    /* PE classic long section name.  The '/' is followed by the index
164
       in the strtab.  The index is formatted as a decimal string.  */
165
53.3k
    char buf[SCNNMLEN];
166
53.3k
    long strindex;
167
53.3k
    char *p;
168
169
53.3k
    memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
170
53.3k
    buf[SCNNMLEN - 1] = '\0';
171
53.3k
    strindex = strtol (buf, &p, 10);
172
53.3k
    if (*p == '\0' && strindex >= 0)
173
31.9k
      {
174
31.9k
        name = extract_long_section_name (abfd, strindex);
175
31.9k
        if (name == NULL)
176
8.24k
    return false;
177
31.9k
      }
178
53.3k
  }
179
63.9k
    }
180
181
90.8M
  if (name == NULL)
182
90.8M
    {
183
      /* Assorted wastage to null-terminate the name, thanks AT&T! */
184
90.8M
      name = (char *) bfd_alloc (abfd,
185
90.8M
         (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
186
90.8M
      if (name == NULL)
187
0
  return false;
188
90.8M
      strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
189
90.8M
      name[sizeof (hdr->s_name)] = 0;
190
90.8M
    }
191
192
90.8M
  newsect = bfd_make_section_anyway (abfd, name);
193
90.8M
  if (newsect == NULL)
194
0
    return false;
195
196
90.8M
  newsect->vma = hdr->s_vaddr;
197
90.8M
  newsect->lma = hdr->s_paddr;
198
90.8M
  newsect->size = hdr->s_size;
199
90.8M
  newsect->filepos = hdr->s_scnptr;
200
90.8M
  newsect->rel_filepos = hdr->s_relptr;
201
90.8M
  newsect->reloc_count = hdr->s_nreloc;
202
203
90.8M
  bfd_coff_set_alignment_hook (abfd, newsect, hdr);
204
205
90.8M
  newsect->line_filepos = hdr->s_lnnoptr;
206
207
90.8M
  newsect->lineno_count = hdr->s_nlnno;
208
90.8M
  newsect->userdata = NULL;
209
90.8M
  newsect->next = NULL;
210
90.8M
  newsect->target_index = target_index;
211
212
90.8M
  if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
213
662k
    result = false;
214
215
  /* At least on i386-coff, the line number count for a shared library
216
     section must be ignored.  */
217
90.8M
  if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
218
16.7M
    newsect->lineno_count = 0;
219
220
90.8M
  if (hdr->s_nreloc != 0)
221
60.7M
    flags |= SEC_RELOC;
222
  /* FIXME: should this check 'hdr->s_size > 0'.  */
223
90.8M
  if (hdr->s_scnptr != 0)
224
70.5M
    flags |= SEC_HAS_CONTENTS;
225
226
90.8M
  newsect->flags = flags;
227
228
  /* Compress/decompress DWARF debug sections.  */
229
90.8M
  if ((flags & SEC_DEBUGGING) != 0
230
90.8M
      && (flags & SEC_HAS_CONTENTS) != 0
231
90.8M
      && (startswith (name, ".debug_")
232
2.38M
    || startswith (name, ".zdebug_")
233
2.38M
    || startswith (name, ".gnu.debuglto_.debug_")
234
2.38M
    || startswith (name, ".gnu.linkonce.wi.")))
235
41.8k
    {
236
41.8k
      enum { nothing, compress, decompress } action = nothing;
237
238
41.8k
      if (bfd_is_section_compressed (abfd, newsect))
239
1.92k
  {
240
    /* Compressed section.  Check if we should decompress.  */
241
1.92k
    if ((abfd->flags & BFD_DECOMPRESS))
242
1
      action = decompress;
243
1.92k
  }
244
39.9k
      else
245
39.9k
  {
246
    /* Normal section.  Check if we should compress.  */
247
39.9k
    if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
248
7.22k
      action = compress;
249
39.9k
  }
250
251
41.8k
      if (action == compress)
252
7.22k
  {
253
7.22k
    if (!bfd_init_section_compress_status (abfd, newsect))
254
964
      {
255
964
        _bfd_error_handler
256
    /* xgettext:c-format */
257
964
    (_("%pB: unable to compress section %s"), abfd, name);
258
964
        return false;
259
964
      }
260
7.22k
  }
261
34.6k
      else if (action == decompress)
262
1
  {
263
1
    if (!bfd_init_section_decompress_status (abfd, newsect))
264
0
      {
265
0
        _bfd_error_handler
266
    /* xgettext:c-format */
267
0
    (_("%pB: unable to decompress section %s"), abfd, name);
268
0
        return false;
269
0
      }
270
1
    if (abfd->is_linker_input
271
1
        && name[1] == 'z')
272
0
      {
273
        /* Rename section from .zdebug_* to .debug_* so that ld
274
     scripts will see this section as a debug section.  */
275
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
276
0
        if (new_name == NULL)
277
0
    return false;
278
0
        bfd_rename_section (newsect, new_name);
279
0
      }
280
1
  }
281
41.8k
    }
282
283
90.8M
  return result;
284
90.8M
}
285
286
void
287
coff_object_cleanup (bfd *abfd)
288
1.30M
{
289
1.30M
  struct coff_tdata *td = coff_data (abfd);
290
1.30M
  if (td != NULL)
291
1.20M
    {
292
1.20M
      if (td->section_by_index)
293
0
  htab_delete (td->section_by_index);
294
1.20M
      if (td->section_by_target_index)
295
0
  htab_delete (td->section_by_target_index);
296
1.20M
      if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
297
541k
  htab_delete (pe_data (abfd)->comdat_hash);
298
1.20M
    }
299
1.30M
}
300
301
/* Read in a COFF object and make it into a BFD.  This is used by
302
   ECOFF as well.  */
303
bfd_cleanup
304
coff_real_object_p (bfd *abfd,
305
        unsigned nscns,
306
        struct internal_filehdr *internal_f,
307
        struct internal_aouthdr *internal_a)
308
2.29M
{
309
2.29M
  flagword oflags = abfd->flags;
310
2.29M
  bfd_vma ostart = bfd_get_start_address (abfd);
311
2.29M
  void * tdata;
312
2.29M
  bfd_size_type readsize; /* Length of file_info.  */
313
2.29M
  unsigned int scnhsz;
314
2.29M
  char *external_sections;
315
316
2.29M
  if (!(internal_f->f_flags & F_RELFLG))
317
1.75M
    abfd->flags |= HAS_RELOC;
318
2.29M
  if ((internal_f->f_flags & F_EXEC))
319
574k
    abfd->flags |= EXEC_P;
320
2.29M
  if (!(internal_f->f_flags & F_LNNO))
321
1.76M
    abfd->flags |= HAS_LINENO;
322
2.29M
  if (!(internal_f->f_flags & F_LSYMS))
323
1.71M
    abfd->flags |= HAS_LOCALS;
324
325
  /* FIXME: How can we set D_PAGED correctly?  */
326
2.29M
  if ((internal_f->f_flags & F_EXEC) != 0)
327
574k
    abfd->flags |= D_PAGED;
328
329
2.29M
  abfd->symcount = internal_f->f_nsyms;
330
2.29M
  if (internal_f->f_nsyms)
331
1.87M
    abfd->flags |= HAS_SYMS;
332
333
2.29M
  if (internal_a != (struct internal_aouthdr *) NULL)
334
1.00M
    abfd->start_address = internal_a->entry;
335
1.29M
  else
336
1.29M
    abfd->start_address = 0;
337
338
  /* Set up the tdata area.  ECOFF uses its own routine, and overrides
339
     abfd->flags.  */
340
2.29M
  tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
341
2.29M
  if (tdata == NULL)
342
0
    goto fail2;
343
344
2.29M
  scnhsz = bfd_coff_scnhsz (abfd);
345
2.29M
  readsize = (bfd_size_type) nscns * scnhsz;
346
2.29M
  external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
347
2.29M
  if (!external_sections)
348
99.2k
    goto fail;
349
350
  /* Set the arch/mach *before* swapping in sections; section header swapping
351
     may depend on arch/mach info.  */
352
2.20M
  if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
353
3.39k
    goto fail;
354
355
  /* Now copy data as required; construct all asections etc.  */
356
2.19M
  if (nscns != 0)
357
2.11M
    {
358
2.11M
      unsigned int i;
359
92.3M
      for (i = 0; i < nscns; i++)
360
90.9M
  {
361
90.9M
    struct internal_scnhdr tmp;
362
90.9M
    bfd_coff_swap_scnhdr_in (abfd,
363
90.9M
           (void *) (external_sections + i * scnhsz),
364
90.9M
           (void *) & tmp);
365
90.9M
    if (! make_a_section_from_file (abfd, &tmp, i + 1))
366
682k
      goto fail;
367
90.9M
  }
368
2.11M
    }
369
370
1.51M
  _bfd_coff_free_symbols (abfd);
371
1.51M
  return coff_object_cleanup;
372
373
784k
 fail:
374
784k
  coff_object_cleanup (abfd);
375
784k
  _bfd_coff_free_symbols (abfd);
376
784k
  bfd_release (abfd, tdata);
377
784k
 fail2:
378
784k
  abfd->flags = oflags;
379
784k
  abfd->start_address = ostart;
380
784k
  return NULL;
381
784k
}
382
383
/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
384
   not a COFF file.  This is also used by ECOFF.  */
385
386
bfd_cleanup
387
coff_object_p (bfd *abfd)
388
100M
{
389
100M
  bfd_size_type filhsz;
390
100M
  bfd_size_type aoutsz;
391
100M
  unsigned int nscns;
392
100M
  void * filehdr;
393
100M
  struct internal_filehdr internal_f;
394
100M
  struct internal_aouthdr internal_a;
395
396
  /* Figure out how much to read.  */
397
100M
  filhsz = bfd_coff_filhsz (abfd);
398
100M
  aoutsz = bfd_coff_aoutsz (abfd);
399
400
100M
  filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
401
100M
  if (filehdr == NULL)
402
2.75M
    {
403
2.75M
      if (bfd_get_error () != bfd_error_system_call)
404
2.72M
  bfd_set_error (bfd_error_wrong_format);
405
2.75M
      return NULL;
406
2.75M
    }
407
97.4M
  bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
408
97.4M
  bfd_release (abfd, filehdr);
409
410
  /* The XCOFF format has two sizes for the f_opthdr.  SMALL_AOUTSZ
411
     (less than aoutsz) used in object files and AOUTSZ (equal to
412
     aoutsz) in executables.  The bfd_coff_swap_aouthdr_in function
413
     expects this header to be aoutsz bytes in length, so we use that
414
     value in the call to bfd_alloc below.  But we must be careful to
415
     only read in f_opthdr bytes in the call to bfd_read.  We should
416
     also attempt to catch corrupt or non-COFF binaries with a strange
417
     value for f_opthdr.  */
418
97.4M
  if (! bfd_coff_bad_format_hook (abfd, &internal_f)
419
97.4M
      || internal_f.f_opthdr > aoutsz)
420
96.0M
    {
421
96.0M
      bfd_set_error (bfd_error_wrong_format);
422
96.0M
      return NULL;
423
96.0M
    }
424
1.33M
  nscns = internal_f.f_nscns;
425
426
1.33M
  if (internal_f.f_opthdr)
427
458k
    {
428
458k
      void * opthdr;
429
430
458k
      opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
431
458k
      if (opthdr == NULL)
432
3.34k
  return NULL;
433
      /* PR 17512: file: 11056-1136-0.004.  */
434
455k
      if (internal_f.f_opthdr < aoutsz)
435
418k
  memset (((char *) opthdr) + internal_f.f_opthdr, 0,
436
418k
    aoutsz - internal_f.f_opthdr);
437
438
455k
      bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
439
455k
      bfd_release (abfd, opthdr);
440
455k
    }
441
442
1.33M
  return coff_real_object_p (abfd, nscns, &internal_f,
443
1.33M
           (internal_f.f_opthdr != 0
444
1.33M
            ? &internal_a
445
1.33M
            : (struct internal_aouthdr *) NULL));
446
1.33M
}
447
448
static hashval_t
449
htab_hash_section_target_index (const void * entry)
450
10.0M
{
451
10.0M
  const struct bfd_section * sec = entry;
452
10.0M
  return sec->target_index;
453
10.0M
}
454
455
static int
456
htab_eq_section_target_index (const void * e1, const void * e2)
457
6.22M
{
458
6.22M
  const struct bfd_section * sec1 = e1;
459
6.22M
  const struct bfd_section * sec2 = e2;
460
6.22M
  return sec1->target_index == sec2->target_index;
461
6.22M
}
462
463
/* Get the BFD section from a COFF symbol section number.  */
464
465
asection *
466
coff_section_from_bfd_index (bfd *abfd, int section_index)
467
7.59M
{
468
7.59M
  if (section_index == N_ABS)
469
69.4k
    return bfd_abs_section_ptr;
470
7.52M
  if (section_index == N_UNDEF)
471
2.33M
    return bfd_und_section_ptr;
472
5.18M
  if (section_index == N_DEBUG)
473
3.48k
    return bfd_abs_section_ptr;
474
475
5.18M
  struct bfd_section *answer;
476
5.18M
  htab_t table = coff_data (abfd)->section_by_target_index;
477
478
5.18M
  if (!table)
479
94.2k
    {
480
94.2k
      table = htab_create (10, htab_hash_section_target_index,
481
94.2k
         htab_eq_section_target_index, NULL);
482
94.2k
      if (table == NULL)
483
0
  return bfd_und_section_ptr;
484
94.2k
      coff_data (abfd)->section_by_target_index = table;
485
94.2k
    }
486
487
5.18M
  if (htab_elements (table) == 0)
488
147k
    {
489
2.11M
      for (answer = abfd->sections; answer; answer = answer->next)
490
1.96M
  {
491
1.96M
    void **slot = htab_find_slot (table, answer, INSERT);
492
1.96M
    if (slot == NULL)
493
0
      return bfd_und_section_ptr;
494
1.96M
    *slot = answer;
495
1.96M
  }
496
147k
    }
497
498
5.18M
  struct bfd_section needle;
499
5.18M
  needle.target_index = section_index;
500
501
5.18M
  answer = htab_find (table, &needle);
502
5.18M
  if (answer != NULL)
503
484k
    return answer;
504
505
  /* Cover the unlikely case of sections added after the first call to
506
     this function.  */
507
5.34G
  for (answer = abfd->sections; answer; answer = answer->next)
508
5.34G
    if (answer->target_index == section_index)
509
164k
      {
510
164k
  void **slot = htab_find_slot (table, answer, INSERT);
511
164k
  if (slot != NULL)
512
164k
    *slot = answer;
513
164k
  return answer;
514
164k
      }
515
516
  /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
517
     has a bad symbol table in biglitpow.o.  */
518
4.53M
  return bfd_und_section_ptr;
519
4.70M
}
520
521
/* Get the upper bound of a COFF symbol table.  */
522
523
long
524
coff_get_symtab_upper_bound (bfd *abfd)
525
22.1k
{
526
22.1k
  if (!bfd_coff_slurp_symbol_table (abfd))
527
20.1k
    return -1;
528
529
1.99k
  return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
530
22.1k
}
531
532
/* Canonicalize a COFF symbol table.  */
533
534
long
535
coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
536
1.99k
{
537
1.99k
  unsigned int counter;
538
1.99k
  coff_symbol_type *symbase;
539
1.99k
  coff_symbol_type **location = (coff_symbol_type **) alocation;
540
541
1.99k
  if (!bfd_coff_slurp_symbol_table (abfd))
542
0
    return -1;
543
544
1.99k
  symbase = obj_symbols (abfd);
545
1.99k
  counter = bfd_get_symcount (abfd);
546
79.0k
  while (counter-- > 0)
547
77.0k
    *location++ = symbase++;
548
549
1.99k
  *location = NULL;
550
551
1.99k
  return bfd_get_symcount (abfd);
552
1.99k
}
553
554
/* Get the name of a symbol.  The caller must pass in a buffer of size
555
   >= SYMNMLEN + 1.  */
556
557
const char *
558
_bfd_coff_internal_syment_name (bfd *abfd,
559
        const struct internal_syment *sym,
560
        char *buf)
561
13.3M
{
562
  /* FIXME: It's not clear this will work correctly if sizeof
563
     (_n_zeroes) != 4.  */
564
13.3M
  if (sym->_n._n_n._n_zeroes != 0
565
13.3M
      || sym->_n._n_n._n_offset == 0)
566
9.60M
    {
567
9.60M
      memcpy (buf, sym->_n._n_name, SYMNMLEN);
568
9.60M
      buf[SYMNMLEN] = '\0';
569
9.60M
      return buf;
570
9.60M
    }
571
3.75M
  else
572
3.75M
    {
573
3.75M
      const char *strings;
574
575
3.75M
      BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
576
3.75M
      strings = obj_coff_strings (abfd);
577
3.75M
      if (strings == NULL)
578
2.39M
  {
579
2.39M
    strings = _bfd_coff_read_string_table (abfd);
580
2.39M
    if (strings == NULL)
581
2.31M
      return NULL;
582
2.39M
  }
583
1.44M
      if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
584
1.23M
  return NULL;
585
214k
      return strings + sym->_n._n_n._n_offset;
586
1.44M
    }
587
13.3M
}
588
589
/* Read in and swap the relocs.  This returns a buffer holding the
590
   relocs for section SEC in file ABFD.  If CACHE is TRUE and
591
   INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
592
   the function is called again.  If EXTERNAL_RELOCS is not NULL, it
593
   is a buffer large enough to hold the unswapped relocs.  If
594
   INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
595
   the swapped relocs.  If REQUIRE_INTERNAL is TRUE, then the return
596
   value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
597
598
struct internal_reloc *
599
_bfd_coff_read_internal_relocs (bfd *abfd,
600
        asection *sec,
601
        bool cache,
602
        bfd_byte *external_relocs,
603
        bool require_internal,
604
        struct internal_reloc *internal_relocs)
605
0
{
606
0
  bfd_size_type relsz;
607
0
  bfd_byte *free_external = NULL;
608
0
  struct internal_reloc *free_internal = NULL;
609
0
  bfd_byte *erel;
610
0
  bfd_byte *erel_end;
611
0
  struct internal_reloc *irel;
612
0
  bfd_size_type amt;
613
614
0
  if (sec->reloc_count == 0)
615
0
    return internal_relocs; /* Nothing to do.  */
616
617
0
  if (coff_section_data (abfd, sec) != NULL
618
0
      && coff_section_data (abfd, sec)->relocs != NULL)
619
0
    {
620
0
      if (! require_internal)
621
0
  return coff_section_data (abfd, sec)->relocs;
622
0
      memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
623
0
        sec->reloc_count * sizeof (struct internal_reloc));
624
0
      return internal_relocs;
625
0
    }
626
627
0
  relsz = bfd_coff_relsz (abfd);
628
629
0
  amt = sec->reloc_count * relsz;
630
0
  if (external_relocs == NULL)
631
0
    {
632
0
      free_external = (bfd_byte *) bfd_malloc (amt);
633
0
      if (free_external == NULL)
634
0
  goto error_return;
635
0
      external_relocs = free_external;
636
0
    }
637
638
0
  if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
639
0
      || bfd_read (external_relocs, amt, abfd) != amt)
640
0
    goto error_return;
641
642
0
  if (internal_relocs == NULL)
643
0
    {
644
0
      amt = sec->reloc_count;
645
0
      amt *= sizeof (struct internal_reloc);
646
0
      free_internal = (struct internal_reloc *) bfd_malloc (amt);
647
0
      if (free_internal == NULL)
648
0
  goto error_return;
649
0
      internal_relocs = free_internal;
650
0
    }
651
652
  /* Swap in the relocs.  */
653
0
  erel = external_relocs;
654
0
  erel_end = erel + relsz * sec->reloc_count;
655
0
  irel = internal_relocs;
656
0
  for (; erel < erel_end; erel += relsz, irel++)
657
0
    bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
658
659
0
  free (free_external);
660
0
  free_external = NULL;
661
662
0
  if (cache && free_internal != NULL)
663
0
    {
664
0
      if (coff_section_data (abfd, sec) == NULL)
665
0
  {
666
0
    amt = sizeof (struct coff_section_tdata);
667
0
    sec->used_by_bfd = bfd_zalloc (abfd, amt);
668
0
    if (sec->used_by_bfd == NULL)
669
0
      goto error_return;
670
0
    coff_section_data (abfd, sec)->contents = NULL;
671
0
  }
672
0
      coff_section_data (abfd, sec)->relocs = free_internal;
673
0
    }
674
675
0
  return internal_relocs;
676
677
0
 error_return:
678
0
  free (free_external);
679
0
  free (free_internal);
680
0
  return NULL;
681
0
}
682
683
/* Set lineno_count for the output sections of a COFF file.  */
684
685
int
686
coff_count_linenumbers (bfd *abfd)
687
31
{
688
31
  unsigned int limit = bfd_get_symcount (abfd);
689
31
  unsigned int i;
690
31
  int total = 0;
691
31
  asymbol **p;
692
31
  asection *s;
693
694
31
  if (limit == 0)
695
27
    {
696
      /* This may be from the backend linker, in which case the
697
   lineno_count in the sections is correct.  */
698
84
      for (s = abfd->sections; s != NULL; s = s->next)
699
57
  total += s->lineno_count;
700
27
      return total;
701
27
    }
702
703
42
  for (s = abfd->sections; s != NULL; s = s->next)
704
38
    BFD_ASSERT (s->lineno_count == 0);
705
706
6.25k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
707
6.25k
    {
708
6.25k
      asymbol *q_maybe = *p;
709
710
6.25k
      if (bfd_asymbol_bfd (q_maybe) != NULL
711
6.25k
    && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
712
6.25k
  {
713
6.25k
    coff_symbol_type *q = coffsymbol (q_maybe);
714
715
    /* The AIX 4.1 compiler can sometimes generate line numbers
716
       attached to debugging symbols.  We try to simply ignore
717
       those here.  */
718
6.25k
    if (q->lineno != NULL
719
6.25k
        && q->symbol.section->owner != NULL)
720
0
      {
721
        /* This symbol has line numbers.  Increment the owning
722
     section's linenumber count.  */
723
0
        alent *l = q->lineno;
724
725
0
        do
726
0
    {
727
0
      asection * sec = q->symbol.section->output_section;
728
729
      /* Do not try to update fields in read-only sections.  */
730
0
      if (! bfd_is_const_section (sec))
731
0
        sec->lineno_count ++;
732
733
0
      ++total;
734
0
      ++l;
735
0
    }
736
0
        while (l->line_number != 0);
737
0
      }
738
6.25k
  }
739
6.25k
    }
740
741
4
  return total;
742
31
}
743
744
static void
745
fixup_symbol_value (bfd *abfd,
746
        coff_symbol_type *coff_symbol_ptr,
747
        struct internal_syment *syment)
748
6.22k
{
749
  /* Normalize the symbol flags.  */
750
6.22k
  if (coff_symbol_ptr->symbol.section
751
6.22k
      && bfd_is_com_section (coff_symbol_ptr->symbol.section))
752
0
    {
753
      /* A common symbol is undefined with a value.  */
754
0
      syment->n_scnum = N_UNDEF;
755
0
      syment->n_value = coff_symbol_ptr->symbol.value;
756
0
    }
757
6.22k
  else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
758
6.22k
     && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
759
3
    {
760
3
      syment->n_value = coff_symbol_ptr->symbol.value;
761
3
    }
762
6.22k
  else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
763
5.85k
    {
764
5.85k
      syment->n_scnum = N_UNDEF;
765
5.85k
      syment->n_value = 0;
766
5.85k
    }
767
  /* FIXME: Do we need to handle the absolute section here?  */
768
368
  else
769
368
    {
770
368
      if (coff_symbol_ptr->symbol.section)
771
368
  {
772
368
    syment->n_scnum =
773
368
      coff_symbol_ptr->symbol.section->output_section->target_index;
774
775
368
    syment->n_value = (coff_symbol_ptr->symbol.value
776
368
           + coff_symbol_ptr->symbol.section->output_offset);
777
368
    if (! obj_pe (abfd))
778
0
      {
779
0
        syment->n_value += (syment->n_sclass == C_STATLAB)
780
0
    ? coff_symbol_ptr->symbol.section->output_section->lma
781
0
    : coff_symbol_ptr->symbol.section->output_section->vma;
782
0
      }
783
368
  }
784
0
      else
785
0
  {
786
0
    BFD_ASSERT (0);
787
    /* This can happen, but I don't know why yet (steve@cygnus.com) */
788
0
    syment->n_scnum = N_ABS;
789
0
    syment->n_value = coff_symbol_ptr->symbol.value;
790
0
  }
791
368
    }
792
6.22k
}
793
794
/* Run through all the symbols in the symbol table and work out what
795
   their indexes into the symbol table will be when output.
796
797
   Coff requires that each C_FILE symbol points to the next one in the
798
   chain, and that the last one points to the first external symbol. We
799
   do that here too.  */
800
801
bool
802
coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
803
4
{
804
4
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
805
4
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
806
4
  unsigned int native_index = 0;
807
4
  struct internal_syment *last_file = NULL;
808
4
  unsigned int symbol_index;
809
810
  /* COFF demands that undefined symbols come after all other symbols.
811
     Since we don't need to impose this extra knowledge on all our
812
     client programs, deal with that here.  Sort the symbol table;
813
     just move the undefined symbols to the end, leaving the rest
814
     alone.  The O'Reilly book says that defined global symbols come
815
     at the end before the undefined symbols, so we do that here as
816
     well.  */
817
  /* @@ Do we have some condition we could test for, so we don't always
818
     have to do this?  I don't think relocatability is quite right, but
819
     I'm not certain.  [raeburn:19920508.1711EST]  */
820
4
  {
821
4
    asymbol **newsyms;
822
4
    unsigned int i;
823
4
    bfd_size_type amt;
824
825
4
    amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
826
4
    newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
827
4
    if (!newsyms)
828
0
      return false;
829
4
    bfd_ptr->outsymbols = newsyms;
830
6.25k
    for (i = 0; i < symbol_count; i++)
831
6.25k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
832
6.25k
    || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
833
6.22k
        && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
834
6.22k
        && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
835
370
      || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
836
370
          == 0))))
837
333
  *newsyms++ = symbol_ptr_ptr[i];
838
839
6.25k
    for (i = 0; i < symbol_count; i++)
840
6.25k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
841
6.25k
    && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
842
6.25k
    && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
843
370
        || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
844
370
      && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
845
370
          != 0))))
846
64
  *newsyms++ = symbol_ptr_ptr[i];
847
848
4
    *first_undef = newsyms - bfd_ptr->outsymbols;
849
850
6.25k
    for (i = 0; i < symbol_count; i++)
851
6.25k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
852
6.25k
    && bfd_is_und_section (symbol_ptr_ptr[i]->section))
853
5.85k
  *newsyms++ = symbol_ptr_ptr[i];
854
4
    *newsyms = (asymbol *) NULL;
855
4
    symbol_ptr_ptr = bfd_ptr->outsymbols;
856
4
  }
857
858
6.25k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
859
6.25k
    {
860
6.25k
      coff_symbol_type *coff_symbol_ptr;
861
862
6.25k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
863
6.25k
      symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
864
6.25k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
865
6.25k
  {
866
6.25k
    combined_entry_type *s = coff_symbol_ptr->native;
867
6.25k
    int i;
868
869
6.25k
    BFD_ASSERT (s->is_sym);
870
6.25k
    if (s->u.syment.n_sclass == C_FILE)
871
28
      {
872
28
        if (last_file != NULL)
873
27
    last_file->n_value = native_index;
874
28
        last_file = &(s->u.syment);
875
28
      }
876
6.22k
    else
877
      /* Modify the symbol values according to their section and
878
         type.  */
879
6.22k
      fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
880
881
13.0k
    for (i = 0; i < s->u.syment.n_numaux + 1; i++)
882
6.81k
      s[i].offset = native_index++;
883
6.25k
  }
884
0
      else
885
0
  native_index++;
886
6.25k
    }
887
888
4
  obj_conv_table_size (bfd_ptr) = native_index;
889
890
4
  return true;
891
4
}
892
893
/* Run thorough the symbol table again, and fix it so that all
894
   pointers to entries are changed to the entries' index in the output
895
   symbol table.  */
896
897
void
898
coff_mangle_symbols (bfd *bfd_ptr)
899
4
{
900
4
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
901
4
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
902
4
  unsigned int symbol_index;
903
904
6.25k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
905
6.25k
    {
906
6.25k
      coff_symbol_type *coff_symbol_ptr;
907
908
6.25k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
909
6.25k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
910
6.25k
  {
911
6.25k
    int i;
912
6.25k
    combined_entry_type *s = coff_symbol_ptr->native;
913
914
6.25k
    BFD_ASSERT (s->is_sym);
915
6.25k
    if (s->fix_value)
916
0
      {
917
        /* FIXME: We should use a union here.  */
918
0
        s->u.syment.n_value =
919
0
    (uintptr_t) ((combined_entry_type *)
920
0
           (uintptr_t) s->u.syment.n_value)->offset;
921
0
        s->fix_value = 0;
922
0
      }
923
6.25k
    if (s->fix_line)
924
0
      {
925
        /* The value is the offset into the line number entries
926
     for the symbol's section.  On output, the symbol's
927
     section should be N_DEBUG.  */
928
0
        s->u.syment.n_value =
929
0
    (coff_symbol_ptr->symbol.section->output_section->line_filepos
930
0
     + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
931
0
        coff_symbol_ptr->symbol.section =
932
0
    coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
933
0
        BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
934
0
      }
935
6.81k
    for (i = 0; i < s->u.syment.n_numaux; i++)
936
558
      {
937
558
        combined_entry_type *a = s + i + 1;
938
939
558
        BFD_ASSERT (! a->is_sym);
940
558
        if (a->fix_tag)
941
327
    {
942
327
      a->u.auxent.x_sym.x_tagndx.u32 =
943
327
        a->u.auxent.x_sym.x_tagndx.p->offset;
944
327
      a->fix_tag = 0;
945
327
    }
946
558
        if (a->fix_end)
947
0
    {
948
0
      a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
949
0
        a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
950
0
      a->fix_end = 0;
951
0
    }
952
558
        if (a->fix_scnlen)
953
0
    {
954
0
      a->u.auxent.x_csect.x_scnlen.u64 =
955
0
        a->u.auxent.x_csect.x_scnlen.p->offset;
956
0
      a->fix_scnlen = 0;
957
0
    }
958
558
      }
959
6.25k
  }
960
6.25k
    }
961
4
}
962
963
static bool
964
coff_write_auxent_fname (bfd *abfd,
965
       char *str,
966
       union internal_auxent *auxent,
967
       struct bfd_strtab_hash *strtab,
968
       bool hash)
969
28
{
970
28
  unsigned int str_length = strlen (str);
971
28
  unsigned int filnmlen = bfd_coff_filnmlen (abfd);
972
973
28
  if (bfd_coff_long_filenames (abfd))
974
28
    {
975
28
      if (str_length <= filnmlen)
976
25
  strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
977
3
      else
978
3
  {
979
3
    bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
980
981
3
    if (indx == (bfd_size_type) -1)
982
0
      return false;
983
984
3
    auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
985
3
    auxent->x_file.x_n.x_n.x_zeroes = 0;
986
3
  }
987
28
    }
988
0
  else
989
0
    {
990
0
      strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
991
0
      if (str_length > filnmlen)
992
0
  str[filnmlen] = '\0';
993
0
    }
994
995
28
  return true;
996
28
}
997
998
static bool
999
coff_fix_symbol_name (bfd *abfd,
1000
          asymbol *symbol,
1001
          combined_entry_type *native,
1002
          struct bfd_strtab_hash *strtab,
1003
          bool hash,
1004
          asection **debug_string_section_p,
1005
          bfd_size_type *debug_string_size_p)
1006
6.25k
{
1007
6.25k
  unsigned int name_length;
1008
6.25k
  char *name = (char *) (symbol->name);
1009
6.25k
  bfd_size_type indx;
1010
1011
6.25k
  if (name == NULL)
1012
0
    {
1013
      /* COFF symbols always have names, so we'll make one up.  */
1014
0
      symbol->name = "strange";
1015
0
      name = (char *) symbol->name;
1016
0
    }
1017
6.25k
  name_length = strlen (name);
1018
1019
6.25k
  BFD_ASSERT (native->is_sym);
1020
6.25k
  if (native->u.syment.n_sclass == C_FILE
1021
6.25k
      && native->u.syment.n_numaux > 0)
1022
28
    {
1023
28
      if (bfd_coff_force_symnames_in_strings (abfd))
1024
0
  {
1025
0
    indx = _bfd_stringtab_add (strtab, ".file", hash, false);
1026
0
    if (indx == (bfd_size_type) -1)
1027
0
      return false;
1028
1029
0
    native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1030
0
    native->u.syment._n._n_n._n_zeroes = 0;
1031
0
  }
1032
28
      else
1033
28
  strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1034
1035
28
      BFD_ASSERT (! (native + 1)->is_sym);
1036
28
      if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1037
28
             strtab, hash))
1038
0
  return false;
1039
28
    }
1040
6.22k
  else
1041
6.22k
    {
1042
6.22k
      if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1043
  /* This name will fit into the symbol neatly.  */
1044
6.03k
  strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1045
1046
188
      else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1047
188
  {
1048
188
    indx = _bfd_stringtab_add (strtab, name, hash, false);
1049
188
    if (indx == (bfd_size_type) -1)
1050
0
      return false;
1051
1052
188
    native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1053
188
    native->u.syment._n._n_n._n_zeroes = 0;
1054
188
  }
1055
0
      else
1056
0
  {
1057
0
    file_ptr filepos;
1058
0
    bfd_byte buf[4];
1059
0
    int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
1060
1061
    /* This name should be written into the .debug section.  For
1062
       some reason each name is preceded by a two byte length
1063
       and also followed by a null byte.  FIXME: We assume that
1064
       the .debug section has already been created, and that it
1065
       is large enough.  */
1066
0
    if (*debug_string_section_p == (asection *) NULL)
1067
0
      *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
1068
0
    filepos = bfd_tell (abfd);
1069
0
    if (prefix_len == 4)
1070
0
      bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
1071
0
    else
1072
0
      bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
1073
1074
0
    if (!bfd_set_section_contents (abfd,
1075
0
           *debug_string_section_p,
1076
0
           (void *) buf,
1077
0
           (file_ptr) *debug_string_size_p,
1078
0
           (bfd_size_type) prefix_len)
1079
0
        || !bfd_set_section_contents (abfd,
1080
0
              *debug_string_section_p,
1081
0
              (void *) symbol->name,
1082
0
              (file_ptr) (*debug_string_size_p
1083
0
              + prefix_len),
1084
0
              (bfd_size_type) name_length + 1))
1085
0
      abort ();
1086
0
    if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1087
0
      abort ();
1088
0
    native->u.syment._n._n_n._n_offset =
1089
0
        *debug_string_size_p + prefix_len;
1090
0
    native->u.syment._n._n_n._n_zeroes = 0;
1091
0
    *debug_string_size_p += name_length + 1 + prefix_len;
1092
0
  }
1093
6.22k
    }
1094
1095
6.25k
  return true;
1096
6.25k
}
1097
1098
/* We need to keep track of the symbol index so that when we write out
1099
   the relocs we can get the index for a symbol.  This method is a
1100
   hack.  FIXME.  */
1101
1102
6.25k
#define set_index(symbol, idx)  ((symbol)->udata.i = (idx))
1103
1104
/* Write a symbol out to a COFF file.  */
1105
1106
static bool
1107
coff_write_symbol (bfd *abfd,
1108
       asymbol *symbol,
1109
       combined_entry_type *native,
1110
       bfd_vma *written,
1111
       struct bfd_strtab_hash *strtab,
1112
       bool hash,
1113
       asection **debug_string_section_p,
1114
       bfd_size_type *debug_string_size_p)
1115
6.25k
{
1116
6.25k
  unsigned int numaux = native->u.syment.n_numaux;
1117
6.25k
  int type = native->u.syment.n_type;
1118
6.25k
  int n_sclass = (int) native->u.syment.n_sclass;
1119
6.25k
  asection *output_section = symbol->section->output_section
1120
6.25k
             ? symbol->section->output_section
1121
6.25k
             : symbol->section;
1122
6.25k
  void * buf;
1123
6.25k
  bfd_size_type symesz;
1124
1125
6.25k
  BFD_ASSERT (native->is_sym);
1126
1127
6.25k
  if (native->u.syment.n_sclass == C_FILE)
1128
28
    symbol->flags |= BSF_DEBUGGING;
1129
1130
6.25k
  if (symbol->flags & BSF_DEBUGGING
1131
6.25k
      && bfd_is_abs_section (symbol->section))
1132
29
    native->u.syment.n_scnum = N_DEBUG;
1133
1134
6.22k
  else if (bfd_is_abs_section (symbol->section))
1135
25
    native->u.syment.n_scnum = N_ABS;
1136
1137
6.19k
  else if (bfd_is_und_section (symbol->section))
1138
5.85k
    native->u.syment.n_scnum = N_UNDEF;
1139
1140
343
  else
1141
343
    native->u.syment.n_scnum =
1142
343
      output_section->target_index;
1143
1144
6.25k
  if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1145
6.25k
           debug_string_section_p, debug_string_size_p))
1146
0
    return false;
1147
1148
6.25k
  symesz = bfd_coff_symesz (abfd);
1149
6.25k
  buf = bfd_alloc (abfd, symesz);
1150
6.25k
  if (!buf)
1151
0
    return false;
1152
6.25k
  bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1153
6.25k
  if (bfd_write (buf, symesz, abfd) != symesz)
1154
0
    return false;
1155
6.25k
  bfd_release (abfd, buf);
1156
1157
6.25k
  if (native->u.syment.n_numaux > 0)
1158
252
    {
1159
252
      bfd_size_type auxesz;
1160
252
      unsigned int j;
1161
1162
252
      auxesz = bfd_coff_auxesz (abfd);
1163
252
      buf = bfd_alloc (abfd, auxesz);
1164
252
      if (!buf)
1165
0
  return false;
1166
810
      for (j = 0; j < native->u.syment.n_numaux; j++)
1167
558
  {
1168
558
    BFD_ASSERT (! (native + j + 1)->is_sym);
1169
1170
    /* Adjust auxent only if this isn't the filename
1171
       auxiliary entry.  */
1172
558
    if (native->u.syment.n_sclass == C_FILE
1173
558
        && (native + j + 1)->u.auxent.x_file.x_ftype
1174
558
        && (native + j + 1)->extrap)
1175
0
      coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1176
0
             &(native + j + 1)->u.auxent, strtab, hash);
1177
1178
558
    bfd_coff_swap_aux_out (abfd,
1179
558
         &((native + j + 1)->u.auxent),
1180
558
         type, n_sclass, (int) j,
1181
558
         native->u.syment.n_numaux,
1182
558
         buf);
1183
558
    if (bfd_write (buf, auxesz, abfd) != auxesz)
1184
0
      return false;
1185
558
  }
1186
252
      bfd_release (abfd, buf);
1187
252
    }
1188
1189
  /* Store the index for use when we write out the relocs.  */
1190
6.25k
  set_index (symbol, *written);
1191
1192
6.25k
  *written += numaux + 1;
1193
6.25k
  return true;
1194
6.25k
}
1195
1196
/* Write out a symbol to a COFF file that does not come from a COFF
1197
   file originally.  This symbol may have been created by the linker,
1198
   or we may be linking a non COFF file to a COFF file.  */
1199
1200
bool
1201
coff_write_alien_symbol (bfd *abfd,
1202
       asymbol *symbol,
1203
       struct internal_syment *isym,
1204
       bfd_vma *written,
1205
       struct bfd_strtab_hash *strtab,
1206
       bool hash,
1207
       asection **debug_string_section_p,
1208
       bfd_size_type *debug_string_size_p)
1209
0
{
1210
0
  combined_entry_type *native;
1211
0
  combined_entry_type dummy[2];
1212
0
  asection *output_section = symbol->section->output_section
1213
0
             ? symbol->section->output_section
1214
0
             : symbol->section;
1215
0
  struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1216
0
  bool ret;
1217
1218
0
  if ((!link_info || link_info->strip_discarded)
1219
0
      && !bfd_is_abs_section (symbol->section)
1220
0
      && symbol->section->output_section == bfd_abs_section_ptr)
1221
0
    {
1222
0
      symbol->name = "";
1223
0
      if (isym != NULL)
1224
0
  memset (isym, 0, sizeof (*isym));
1225
0
      return true;
1226
0
    }
1227
0
  memset (dummy, 0, sizeof dummy);
1228
0
  native = dummy;
1229
0
  native->is_sym = true;
1230
0
  native[1].is_sym = false;
1231
0
  native->u.syment.n_type = T_NULL;
1232
0
  native->u.syment.n_flags = 0;
1233
0
  native->u.syment.n_numaux = 0;
1234
0
  if (bfd_is_und_section (symbol->section))
1235
0
    {
1236
0
      native->u.syment.n_scnum = N_UNDEF;
1237
0
      native->u.syment.n_value = symbol->value;
1238
0
    }
1239
0
  else if (bfd_is_com_section (symbol->section))
1240
0
    {
1241
0
      native->u.syment.n_scnum = N_UNDEF;
1242
0
      native->u.syment.n_value = symbol->value;
1243
0
    }
1244
0
  else if (symbol->flags & BSF_FILE)
1245
0
    {
1246
0
      native->u.syment.n_scnum = N_DEBUG;
1247
0
      native->u.syment.n_numaux = 1;
1248
0
    }
1249
0
  else if (symbol->flags & BSF_DEBUGGING)
1250
0
    {
1251
      /* There isn't much point to writing out a debugging symbol
1252
   unless we are prepared to convert it into COFF debugging
1253
   format.  So, we just ignore them.  We must clobber the symbol
1254
   name to keep it from being put in the string table.  */
1255
0
      symbol->name = "";
1256
0
      if (isym != NULL)
1257
0
  memset (isym, 0, sizeof (*isym));
1258
0
      return true;
1259
0
    }
1260
0
  else
1261
0
    {
1262
0
      native->u.syment.n_scnum = output_section->target_index;
1263
0
      native->u.syment.n_value = (symbol->value
1264
0
          + symbol->section->output_offset);
1265
0
      if (! obj_pe (abfd))
1266
0
  native->u.syment.n_value += output_section->vma;
1267
1268
      /* Copy the any flags from the file header into the symbol.
1269
   FIXME: Why?  */
1270
0
      {
1271
0
  coff_symbol_type *c = coff_symbol_from (symbol);
1272
0
  if (c != (coff_symbol_type *) NULL)
1273
0
    native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1274
0
      }
1275
1276
0
      const elf_symbol_type *elfsym = elf_symbol_from (symbol);
1277
0
      if (elfsym
1278
0
    && (symbol->flags & BSF_FUNCTION)
1279
0
    && elfsym->internal_elf_sym.st_size)
1280
0
  {
1281
    /* coff_data (abfd)->local_n_btshft is what ought to be used here,
1282
       just that it's set only when reading in COFF objects.  */
1283
0
    native->u.syment.n_type = DT_FCN << 4;
1284
0
    native->u.syment.n_numaux = 1;
1285
0
    native[1].u.auxent.x_sym.x_misc.x_fsize
1286
0
      = elfsym->internal_elf_sym.st_size;
1287
    /* FIXME .u.auxent.x_sym.x_fcnary.x_fcn.x_endndx would better also
1288
       be set, which would require updating the field once the next
1289
       function is seen.  */
1290
0
  }
1291
0
    }
1292
1293
0
  if (symbol->flags & BSF_FILE)
1294
0
    native->u.syment.n_sclass = C_FILE;
1295
0
  else if (symbol->flags & BSF_LOCAL)
1296
0
    native->u.syment.n_sclass = C_STAT;
1297
0
  else if (symbol->flags & BSF_WEAK)
1298
0
    native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1299
0
  else
1300
0
    native->u.syment.n_sclass = C_EXT;
1301
1302
0
  ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1303
0
         debug_string_section_p, debug_string_size_p);
1304
0
  if (isym != NULL)
1305
0
    *isym = native->u.syment;
1306
0
  return ret;
1307
0
}
1308
1309
/* Write a native symbol to a COFF file.  */
1310
1311
static bool
1312
coff_write_native_symbol (bfd *abfd,
1313
        coff_symbol_type *symbol,
1314
        bfd_vma *written,
1315
        struct bfd_strtab_hash *strtab,
1316
        asection **debug_string_section_p,
1317
        bfd_size_type *debug_string_size_p)
1318
6.25k
{
1319
6.25k
  combined_entry_type *native = symbol->native;
1320
6.25k
  alent *lineno = symbol->lineno;
1321
6.25k
  struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1322
1323
6.25k
  if ((!link_info || link_info->strip_discarded)
1324
6.25k
      && !bfd_is_abs_section (symbol->symbol.section)
1325
6.25k
      && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1326
0
    {
1327
0
      symbol->symbol.name = "";
1328
0
      return true;
1329
0
    }
1330
1331
6.25k
  BFD_ASSERT (native->is_sym);
1332
  /* If this symbol has an associated line number, we must store the
1333
     symbol index in the line number field.  We also tag the auxent to
1334
     point to the right place in the lineno table.  */
1335
6.25k
  if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1336
0
    {
1337
0
      unsigned int count = 0;
1338
1339
0
      lineno[count].u.offset = *written;
1340
0
      if (native->u.syment.n_numaux)
1341
0
  {
1342
0
    union internal_auxent *a = &((native + 1)->u.auxent);
1343
1344
0
    a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1345
0
      symbol->symbol.section->output_section->moving_line_filepos;
1346
0
  }
1347
1348
      /* Count and relocate all other linenumbers.  */
1349
0
      count++;
1350
0
      while (lineno[count].line_number != 0)
1351
0
  {
1352
0
    lineno[count].u.offset +=
1353
0
      (symbol->symbol.section->output_section->vma
1354
0
       + symbol->symbol.section->output_offset);
1355
0
    count++;
1356
0
  }
1357
0
      symbol->done_lineno = true;
1358
1359
0
      if (! bfd_is_const_section (symbol->symbol.section->output_section))
1360
0
  symbol->symbol.section->output_section->moving_line_filepos +=
1361
0
    count * bfd_coff_linesz (abfd);
1362
0
    }
1363
1364
6.25k
  return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1365
6.25k
          strtab, true, debug_string_section_p,
1366
6.25k
          debug_string_size_p);
1367
6.25k
}
1368
1369
static void
1370
null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1371
        va_list ap ATTRIBUTE_UNUSED)
1372
5.83k
{
1373
5.83k
}
1374
1375
/* Write out the COFF symbols.  */
1376
1377
bool
1378
coff_write_symbols (bfd *abfd)
1379
6
{
1380
6
  struct bfd_strtab_hash *strtab;
1381
6
  asection *debug_string_section;
1382
6
  bfd_size_type debug_string_size;
1383
6
  unsigned int i;
1384
6
  unsigned int limit = bfd_get_symcount (abfd);
1385
6
  bfd_vma written = 0;
1386
6
  asymbol **p;
1387
1388
6
  debug_string_section = NULL;
1389
6
  debug_string_size = 0;
1390
1391
6
  strtab = _bfd_stringtab_init ();
1392
6
  if (strtab == NULL)
1393
0
    return false;
1394
1395
  /* If this target supports long section names, they must be put into
1396
     the string table.  This is supported by PE.  This code must
1397
     handle section names just as they are handled in
1398
     coff_write_object_contents.  This is why we pass hash as FALSE below.  */
1399
6
  if (bfd_coff_long_section_names (abfd))
1400
3
    {
1401
3
      asection *o;
1402
1403
42
      for (o = abfd->sections; o != NULL; o = o->next)
1404
39
  if (strlen (o->name) > SCNNMLEN
1405
39
      && _bfd_stringtab_add (strtab, o->name, false, false)
1406
15
         == (bfd_size_type) -1)
1407
0
    return false;
1408
3
    }
1409
1410
  /* Seek to the right place.  */
1411
6
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1412
0
    return false;
1413
1414
  /* Output all the symbols we have.  */
1415
6
  written = 0;
1416
6.25k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1417
6.25k
    {
1418
6.25k
      asymbol *symbol = *p;
1419
6.25k
      coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1420
1421
6.25k
      if (c_symbol == (coff_symbol_type *) NULL
1422
6.25k
    || c_symbol->native == (combined_entry_type *) NULL)
1423
0
  {
1424
0
    if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1425
0
          strtab, true, &debug_string_section,
1426
0
          &debug_string_size))
1427
0
      return false;
1428
0
  }
1429
6.25k
      else
1430
6.25k
  {
1431
6.25k
    if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1432
6.25k
      {
1433
6.25k
        bfd_error_handler_type current_error_handler;
1434
6.25k
        enum coff_symbol_classification sym_class;
1435
6.25k
        unsigned char *n_sclass;
1436
1437
        /* Suppress error reporting by bfd_coff_classify_symbol.
1438
     Error messages can be generated when we are processing a local
1439
     symbol which has no associated section and we do not have to
1440
     worry about this, all we need to know is that it is local.  */
1441
6.25k
        current_error_handler = bfd_set_error_handler (null_error_handler);
1442
6.25k
        BFD_ASSERT (c_symbol->native->is_sym);
1443
6.25k
        sym_class = bfd_coff_classify_symbol (abfd,
1444
6.25k
                &c_symbol->native->u.syment);
1445
6.25k
        (void) bfd_set_error_handler (current_error_handler);
1446
1447
6.25k
        n_sclass = &c_symbol->native->u.syment.n_sclass;
1448
1449
        /* If the symbol class has been changed (eg objcopy/ld script/etc)
1450
     we cannot retain the existing sclass from the original symbol.
1451
     Weak symbols only have one valid sclass, so just set it always.
1452
     If it is not local class and should be, set it C_STAT.
1453
     If it is global and not classified as global, or if it is
1454
     weak (which is also classified as global), set it C_EXT.  */
1455
1456
6.25k
        if (symbol->flags & BSF_WEAK)
1457
0
    *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1458
6.25k
        else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1459
0
    *n_sclass = C_STAT;
1460
6.25k
        else if (symbol->flags & BSF_GLOBAL
1461
6.25k
           && (sym_class != COFF_SYMBOL_GLOBAL
1462
#ifdef COFF_WITH_PE
1463
         || *n_sclass == C_NT_WEAK
1464
#endif
1465
91
         || *n_sclass == C_WEAKEXT))
1466
25
    c_symbol->native->u.syment.n_sclass = C_EXT;
1467
6.25k
      }
1468
1469
6.25k
    if (!coff_write_native_symbol (abfd, c_symbol, &written,
1470
6.25k
           strtab, &debug_string_section,
1471
6.25k
           &debug_string_size))
1472
0
      return false;
1473
6.25k
  }
1474
6.25k
    }
1475
1476
6
  obj_raw_syment_count (abfd) = written;
1477
1478
  /* Now write out strings.
1479
1480
     We would normally not write anything here if there are no strings, but
1481
     we'll write out 4 so that any stupid coff reader which tries to read the
1482
     string table even when there isn't one won't croak.  */
1483
6
  {
1484
6
    bfd_byte buffer[STRING_SIZE_SIZE];
1485
1486
6
#if STRING_SIZE_SIZE == 4
1487
6
    H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1488
#else
1489
 #error Change H_PUT_32
1490
#endif
1491
6
    if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1492
0
      return false;
1493
1494
6
    if (! _bfd_stringtab_emit (abfd, strtab))
1495
0
      return false;
1496
6
  }
1497
1498
6
  _bfd_stringtab_free (strtab);
1499
1500
  /* Make sure the .debug section was created to be the correct size.
1501
     We should create it ourselves on the fly, but we don't because
1502
     BFD won't let us write to any section until we know how large all
1503
     the sections are.  We could still do it by making another pass
1504
     over the symbols.  FIXME.  */
1505
6
  BFD_ASSERT (debug_string_size == 0
1506
6
        || (debug_string_section != (asection *) NULL
1507
6
      && (BFD_ALIGN (debug_string_size,
1508
6
         1 << debug_string_section->alignment_power)
1509
6
          == debug_string_section->size)));
1510
1511
6
  return true;
1512
6
}
1513
1514
bool
1515
coff_write_linenumbers (bfd *abfd)
1516
4
{
1517
4
  asection *s;
1518
4
  bfd_size_type linesz;
1519
4
  void * buff;
1520
1521
4
  linesz = bfd_coff_linesz (abfd);
1522
4
  buff = bfd_alloc (abfd, linesz);
1523
4
  if (!buff)
1524
0
    return false;
1525
42
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1526
38
    {
1527
38
      if (s->lineno_count)
1528
0
  {
1529
0
    asymbol **q = abfd->outsymbols;
1530
0
    if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1531
0
      return false;
1532
    /* Find all the linenumbers in this section.  */
1533
0
    while (*q)
1534
0
      {
1535
0
        asymbol *p = *q;
1536
0
        if (p->section->output_section == s)
1537
0
    {
1538
0
      alent *l =
1539
0
      BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1540
0
          (bfd_asymbol_bfd (p), p));
1541
0
      if (l)
1542
0
        {
1543
          /* Found a linenumber entry, output.  */
1544
0
          struct internal_lineno out;
1545
1546
0
          memset ((void *) & out, 0, sizeof (out));
1547
0
          out.l_lnno = 0;
1548
0
          out.l_addr.l_symndx = l->u.offset;
1549
0
          bfd_coff_swap_lineno_out (abfd, &out, buff);
1550
0
          if (bfd_write (buff, linesz, abfd) != linesz)
1551
0
      return false;
1552
0
          l++;
1553
0
          while (l->line_number)
1554
0
      {
1555
0
        out.l_lnno = l->line_number;
1556
0
        out.l_addr.l_symndx = l->u.offset;
1557
0
        bfd_coff_swap_lineno_out (abfd, &out, buff);
1558
0
        if (bfd_write (buff, linesz, abfd) != linesz)
1559
0
          return false;
1560
0
        l++;
1561
0
      }
1562
0
        }
1563
0
    }
1564
0
        q++;
1565
0
      }
1566
0
  }
1567
38
    }
1568
4
  bfd_release (abfd, buff);
1569
4
  return true;
1570
4
}
1571
1572
alent *
1573
coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1574
565
{
1575
565
  return coffsymbol (symbol)->lineno;
1576
565
}
1577
1578
/* This function transforms the offsets into the symbol table into
1579
   pointers to syments.  */
1580
1581
static void
1582
coff_pointerize_aux (bfd *abfd,
1583
         combined_entry_type *table_base,
1584
         combined_entry_type *symbol,
1585
         unsigned int indaux,
1586
         combined_entry_type *auxent)
1587
26.9M
{
1588
26.9M
  unsigned int type = symbol->u.syment.n_type;
1589
26.9M
  unsigned int n_sclass = symbol->u.syment.n_sclass;
1590
1591
26.9M
  BFD_ASSERT (symbol->is_sym);
1592
26.9M
  if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1593
5.14M
    {
1594
5.14M
      if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1595
5.14M
    (abfd, table_base, symbol, indaux, auxent))
1596
22.7k
  return;
1597
5.14M
    }
1598
1599
  /* Don't bother if this is a file or a section.  */
1600
26.9M
  if (n_sclass == C_STAT && type == T_NULL)
1601
50.0k
    return;
1602
26.9M
  if (n_sclass == C_FILE)
1603
423k
    return;
1604
26.4M
  if (n_sclass == C_DWARF)
1605
79.2k
    return;
1606
1607
26.3M
  BFD_ASSERT (! auxent->is_sym);
1608
  /* Otherwise patch up.  */
1609
26.3M
#define N_TMASK coff_data  (abfd)->local_n_tmask
1610
26.3M
#define N_BTSHFT coff_data (abfd)->local_n_btshft
1611
1612
26.3M
  if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1613
26.3M
       || n_sclass == C_FCN)
1614
26.3M
      && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1615
26.3M
      && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1616
3.12M
    < obj_raw_syment_count (abfd)))
1617
250k
    {
1618
250k
      auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1619
250k
  table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1620
250k
      auxent->fix_end = 1;
1621
250k
    }
1622
1623
  /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1624
     generate one, so we must be careful to ignore it.  */
1625
26.3M
  if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1626
11.5M
    {
1627
11.5M
      auxent->u.auxent.x_sym.x_tagndx.p =
1628
11.5M
  table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1629
11.5M
      auxent->fix_tag = 1;
1630
11.5M
    }
1631
26.3M
}
1632
1633
/* Allocate space for the ".debug" section, and read it.
1634
   We did not read the debug section until now, because
1635
   we didn't want to go to the trouble until someone needed it.  */
1636
1637
static char *
1638
build_debug_section (bfd *abfd, asection ** sect_return)
1639
690
{
1640
690
  char *debug_section;
1641
690
  file_ptr position;
1642
690
  bfd_size_type sec_size;
1643
1644
690
  asection *sect = bfd_get_section_by_name (abfd, ".debug");
1645
1646
690
  if (!sect)
1647
250
    {
1648
250
      bfd_set_error (bfd_error_no_debug_section);
1649
250
      return NULL;
1650
250
    }
1651
1652
  /* Seek to the beginning of the `.debug' section and read it.
1653
     Save the current position first; it is needed by our caller.
1654
     Then read debug section and reset the file pointer.  */
1655
1656
440
  position = bfd_tell (abfd);
1657
440
  if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1658
3
    return NULL;
1659
1660
437
  sec_size = sect->size;
1661
437
  debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
1662
437
  if (debug_section == NULL)
1663
4
    return NULL;
1664
433
  debug_section[sec_size] = 0;
1665
1666
433
  if (bfd_seek (abfd, position, SEEK_SET) != 0)
1667
0
    return NULL;
1668
1669
433
  * sect_return = sect;
1670
433
  return debug_section;
1671
433
}
1672
1673
/* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1674
   \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
1675
   be \0-terminated.  */
1676
1677
static char *
1678
copy_name (bfd *abfd, char *name, size_t maxlen)
1679
113k
{
1680
113k
  size_t len;
1681
113k
  char *newname;
1682
1683
3.50M
  for (len = 0; len < maxlen; ++len)
1684
3.42M
    if (name[len] == '\0')
1685
37.3k
      break;
1686
1687
113k
  if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1688
0
    return NULL;
1689
1690
113k
  strncpy (newname, name, len);
1691
113k
  newname[len] = '\0';
1692
113k
  return newname;
1693
113k
}
1694
1695
/* Read in the external symbols.  */
1696
1697
bool
1698
_bfd_coff_get_external_symbols (bfd *abfd)
1699
893k
{
1700
893k
  size_t symesz;
1701
893k
  size_t size;
1702
893k
  void * syms;
1703
893k
  ufile_ptr filesize;
1704
1705
893k
  if (obj_coff_external_syms (abfd) != NULL)
1706
29.8k
    return true;
1707
1708
863k
  symesz = bfd_coff_symesz (abfd);
1709
863k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1710
40.9k
    {
1711
40.9k
      bfd_set_error (bfd_error_file_truncated);
1712
40.9k
      return false;
1713
40.9k
    }
1714
1715
822k
  if (size == 0)
1716
121k
    return true;
1717
1718
700k
  filesize = bfd_get_file_size (abfd);
1719
700k
  if (filesize != 0
1720
700k
      && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1721
700k
    || size > filesize - obj_sym_filepos (abfd)))
1722
260k
    {
1723
260k
      bfd_set_error (bfd_error_file_truncated);
1724
260k
      return false;
1725
260k
    }
1726
1727
440k
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1728
0
    return false;
1729
440k
  syms = _bfd_malloc_and_read (abfd, size, size);
1730
440k
  obj_coff_external_syms (abfd) = syms;
1731
440k
  return syms != NULL;
1732
440k
}
1733
1734
/* Read in the external strings.  The strings are not loaded until
1735
   they are needed.  This is because we have no simple way of
1736
   detecting a missing string table in an archive.  If the strings
1737
   are loaded then the STRINGS and STRINGS_LEN fields in the
1738
   coff_tdata structure will be set.  */
1739
1740
const char *
1741
_bfd_coff_read_string_table (bfd *abfd)
1742
2.44M
{
1743
2.44M
  char extstrsize[STRING_SIZE_SIZE];
1744
2.44M
  bfd_size_type strsize;
1745
2.44M
  char *strings;
1746
2.44M
  ufile_ptr pos;
1747
2.44M
  ufile_ptr filesize;
1748
2.44M
  size_t symesz;
1749
2.44M
  size_t size;
1750
1751
2.44M
  if (obj_coff_strings (abfd) != NULL)
1752
6.44k
    return obj_coff_strings (abfd);
1753
1754
2.43M
  if (obj_sym_filepos (abfd) == 0)
1755
1.90k
    {
1756
1.90k
      bfd_set_error (bfd_error_no_symbols);
1757
1.90k
      return NULL;
1758
1.90k
    }
1759
1760
2.43M
  symesz = bfd_coff_symesz (abfd);
1761
2.43M
  pos = obj_sym_filepos (abfd);
1762
2.43M
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1763
2.43M
      || pos + size < pos)
1764
1.49k
    {
1765
1.49k
      bfd_set_error (bfd_error_file_truncated);
1766
1.49k
      return NULL;
1767
1.49k
    }
1768
1769
2.43M
  if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1770
0
    return NULL;
1771
1772
2.43M
  if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1773
70.7k
    {
1774
70.7k
      if (bfd_get_error () != bfd_error_file_truncated)
1775
4.28k
  return NULL;
1776
1777
      /* There is no string table.  */
1778
66.4k
      strsize = STRING_SIZE_SIZE;
1779
66.4k
    }
1780
2.36M
  else
1781
2.36M
    {
1782
2.36M
#if STRING_SIZE_SIZE == 4
1783
2.36M
      strsize = H_GET_32 (abfd, extstrsize);
1784
#else
1785
 #error Change H_GET_32
1786
#endif
1787
2.36M
    }
1788
1789
2.42M
  filesize = bfd_get_file_size (abfd);
1790
2.42M
  if (strsize < STRING_SIZE_SIZE
1791
2.42M
      || (filesize != 0 && strsize > filesize))
1792
2.19M
    {
1793
2.19M
      _bfd_error_handler
1794
  /* xgettext: c-format */
1795
2.19M
  (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1796
2.19M
      bfd_set_error (bfd_error_bad_value);
1797
2.19M
      return NULL;
1798
2.19M
    }
1799
1800
231k
  strings = (char *) bfd_malloc (strsize + 1);
1801
231k
  if (strings == NULL)
1802
0
    return NULL;
1803
1804
  /* PR 17521 file: 079-54929-0.004.
1805
     A corrupt file could contain an index that points into the first
1806
     STRING_SIZE_SIZE bytes of the string table, so make sure that
1807
     they are zero.  */
1808
231k
  memset (strings, 0, STRING_SIZE_SIZE);
1809
1810
231k
  if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1811
231k
      != strsize - STRING_SIZE_SIZE)
1812
125k
    {
1813
125k
      free (strings);
1814
125k
      return NULL;
1815
125k
    }
1816
1817
106k
  obj_coff_strings (abfd) = strings;
1818
106k
  obj_coff_strings_len (abfd) = strsize;
1819
  /* Terminate the string table, just in case.  */
1820
106k
  strings[strsize] = 0;
1821
106k
  return strings;
1822
231k
}
1823
1824
/* Free up the external symbols and strings read from a COFF file.  */
1825
1826
bool
1827
_bfd_coff_free_symbols (bfd *abfd)
1828
3.38M
{
1829
3.38M
  if (! bfd_family_coff (abfd))
1830
68.6k
    return false;
1831
1832
3.31M
  if (obj_coff_external_syms (abfd) != NULL
1833
3.31M
      && ! obj_coff_keep_syms (abfd))
1834
402k
    {
1835
402k
      free (obj_coff_external_syms (abfd));
1836
402k
      obj_coff_external_syms (abfd) = NULL;
1837
402k
    }
1838
1839
3.31M
  if (obj_coff_strings (abfd) != NULL
1840
3.31M
      && ! obj_coff_keep_strings (abfd))
1841
106k
    {
1842
106k
      free (obj_coff_strings (abfd));
1843
106k
      obj_coff_strings (abfd) = NULL;
1844
106k
      obj_coff_strings_len (abfd) = 0;
1845
106k
    }
1846
1847
3.31M
  return true;
1848
3.38M
}
1849
1850
/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1851
   knit the symbol names into a normalized form.  By normalized here I
1852
   mean that all symbols have an n_offset pointer that points to a null-
1853
   terminated string.  */
1854
1855
combined_entry_type *
1856
coff_get_normalized_symtab (bfd *abfd)
1857
42.9k
{
1858
42.9k
  combined_entry_type *internal;
1859
42.9k
  combined_entry_type *internal_ptr;
1860
42.9k
  size_t symesz;
1861
42.9k
  char *raw_src;
1862
42.9k
  char *raw_end;
1863
42.9k
  const char *string_table = NULL;
1864
42.9k
  asection * debug_sec = NULL;
1865
42.9k
  char *debug_sec_data = NULL;
1866
42.9k
  bfd_size_type size;
1867
1868
42.9k
  if (obj_raw_syments (abfd) != NULL)
1869
0
    return obj_raw_syments (abfd);
1870
1871
42.9k
  if (! _bfd_coff_get_external_symbols (abfd))
1872
17.0k
    return NULL;
1873
1874
25.9k
  size = obj_raw_syment_count (abfd);
1875
  /* Check for integer overflow.  */
1876
25.9k
  if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1877
0
    return NULL;
1878
25.9k
  size *= sizeof (combined_entry_type);
1879
25.9k
  internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1880
25.9k
  if (internal == NULL && size != 0)
1881
0
    return NULL;
1882
1883
25.9k
  raw_src = (char *) obj_coff_external_syms (abfd);
1884
1885
  /* Mark the end of the symbols.  */
1886
25.9k
  symesz = bfd_coff_symesz (abfd);
1887
25.9k
  raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1888
1889
  /* FIXME SOMEDAY.  A string table size of zero is very weird, but
1890
     probably possible.  If one shows up, it will probably kill us.  */
1891
1892
  /* Swap all the raw entries.  */
1893
25.9k
  for (internal_ptr = internal;
1894
3.52M
       raw_src < raw_end;
1895
3.49M
       raw_src += symesz, internal_ptr++)
1896
3.51M
    {
1897
3.51M
      unsigned int i;
1898
1899
3.51M
      bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1900
3.51M
          (void *) & internal_ptr->u.syment);
1901
3.51M
      internal_ptr->is_sym = true;
1902
3.51M
      combined_entry_type *sym = internal_ptr;
1903
1904
      /* PR 17512: Prevent buffer overrun.  */
1905
3.51M
      if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1906
3.93k
  return NULL;
1907
1908
30.4M
      for (i = 0; i < sym->u.syment.n_numaux; i++)
1909
26.9M
  {
1910
26.9M
    internal_ptr++;
1911
26.9M
    raw_src += symesz;
1912
1913
26.9M
    bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1914
26.9M
        sym->u.syment.n_type,
1915
26.9M
        sym->u.syment.n_sclass,
1916
26.9M
        (int) i, sym->u.syment.n_numaux,
1917
26.9M
        &(internal_ptr->u.auxent));
1918
1919
26.9M
    internal_ptr->is_sym = false;
1920
26.9M
    coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1921
26.9M
  }
1922
1923
3.50M
      if (sym->u.syment.n_sclass == C_FILE
1924
3.50M
    && sym->u.syment.n_numaux > 0)
1925
7.19k
  {
1926
7.19k
    combined_entry_type * aux = sym + 1;
1927
1928
    /* Make a file symbol point to the name in the auxent, since
1929
       the text ".file" is redundant.  */
1930
7.19k
    BFD_ASSERT (! aux->is_sym);
1931
1932
7.19k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1933
1.28k
      {
1934
        /* The filename is a long one, point into the string table.  */
1935
1.28k
        if (string_table == NULL)
1936
151
    {
1937
151
      string_table = _bfd_coff_read_string_table (abfd);
1938
151
      if (string_table == NULL)
1939
83
        return NULL;
1940
151
    }
1941
1942
1.20k
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1943
1.20k
      >= obj_coff_strings_len (abfd))
1944
807
    sym->u.syment._n._n_n._n_offset =
1945
807
      (uintptr_t) bfd_symbol_error_name;
1946
398
        else
1947
398
    sym->u.syment._n._n_n._n_offset =
1948
398
      (uintptr_t) (string_table
1949
398
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
1950
1.20k
      }
1951
5.90k
    else
1952
5.90k
      {
1953
        /* Ordinary short filename, put into memory anyway.  The
1954
     Microsoft PE tools sometimes store a filename in
1955
     multiple AUX entries.  */
1956
5.90k
        size_t len;
1957
5.90k
        char *src;
1958
5.90k
        if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1959
2.49k
    {
1960
2.49k
      len = sym->u.syment.n_numaux * symesz;
1961
2.49k
      src = raw_src - (len - symesz);
1962
2.49k
    }
1963
3.41k
        else
1964
3.41k
    {
1965
3.41k
      len = bfd_coff_filnmlen (abfd);
1966
3.41k
      src = aux->u.auxent.x_file.x_n.x_fname;
1967
3.41k
    }
1968
5.90k
        sym->u.syment._n._n_n._n_offset =
1969
5.90k
    (uintptr_t) copy_name (abfd, src, len);
1970
5.90k
      }
1971
1972
    /* Normalize other strings available in C_FILE aux entries.  */
1973
7.11k
    if (!obj_pe (abfd))
1974
2.71k
      for (int numaux = 1;
1975
152k
     numaux < sym->u.syment.n_numaux;
1976
149k
     numaux++)
1977
149k
        {
1978
149k
    aux = sym + numaux + 1;
1979
149k
    BFD_ASSERT (! aux->is_sym);
1980
1981
149k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1982
42.2k
      {
1983
        /* The string information is a long one, point
1984
           into the string table.  */
1985
42.2k
        if (string_table == NULL)
1986
367
          {
1987
367
      string_table = _bfd_coff_read_string_table (abfd);
1988
367
      if (string_table == NULL)
1989
179
        return NULL;
1990
367
          }
1991
1992
42.0k
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1993
42.0k
      >= obj_coff_strings_len (abfd))
1994
23.1k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
1995
23.1k
      (uintptr_t) bfd_symbol_error_name;
1996
18.9k
        else
1997
18.9k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
1998
18.9k
      (uintptr_t) (string_table
1999
18.9k
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
2000
42.0k
      }
2001
107k
    else
2002
107k
      aux->u.auxent.x_file.x_n.x_n.x_offset =
2003
107k
        ((uintptr_t)
2004
107k
         copy_name (abfd,
2005
107k
        aux->u.auxent.x_file.x_n.x_fname,
2006
107k
        bfd_coff_filnmlen (abfd)));
2007
149k
        }
2008
2009
7.11k
  }
2010
3.49M
      else
2011
3.49M
  {
2012
3.49M
    if (sym->u.syment._n._n_n._n_zeroes != 0)
2013
1.10M
      {
2014
        /* This is a "short" name.  Make it long.  */
2015
1.10M
        char *newstring;
2016
2017
        /* Find the length of this string without walking into memory
2018
     that isn't ours.  */
2019
4.53M
        for (i = 0; i < SYMNMLEN; ++i)
2020
4.25M
    if (sym->u.syment._n._n_name[i] == '\0')
2021
827k
      break;
2022
2023
1.10M
        newstring = bfd_alloc (abfd, i + 1);
2024
1.10M
        if (newstring == NULL)
2025
0
    return NULL;
2026
1.10M
        memcpy (newstring, sym->u.syment._n._n_name, i);
2027
1.10M
        newstring[i] = 0;
2028
1.10M
        sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2029
1.10M
        sym->u.syment._n._n_n._n_zeroes = 0;
2030
1.10M
      }
2031
2.39M
    else if (sym->u.syment._n._n_n._n_offset == 0)
2032
1.17M
      sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2033
1.22M
    else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2034
1.21M
      {
2035
        /* Long name already.  Point symbol at the string in the
2036
     table.  */
2037
1.21M
        if (string_table == NULL)
2038
17.2k
    {
2039
17.2k
      string_table = _bfd_coff_read_string_table (abfd);
2040
17.2k
      if (string_table == NULL)
2041
8.92k
        return NULL;
2042
17.2k
    }
2043
1.20M
        if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2044
968k
    sym->u.syment._n._n_n._n_offset =
2045
968k
      (uintptr_t) bfd_symbol_error_name;
2046
241k
        else
2047
241k
    sym->u.syment._n._n_n._n_offset =
2048
241k
      (uintptr_t) (string_table
2049
241k
             + sym->u.syment._n._n_n._n_offset);
2050
1.20M
      }
2051
3.67k
    else
2052
3.67k
      {
2053
        /* Long name in debug section.  Very similar.  */
2054
3.67k
        if (debug_sec_data == NULL)
2055
690
    {
2056
690
      debug_sec_data = build_debug_section (abfd, &debug_sec);
2057
690
      if (debug_sec_data == NULL)
2058
257
        return NULL;
2059
690
    }
2060
        /* PR binutils/17512: Catch out of range offsets into
2061
     the debug data.  */
2062
3.42k
        if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2063
3.37k
    sym->u.syment._n._n_n._n_offset =
2064
3.37k
      (uintptr_t) bfd_symbol_error_name;
2065
49
        else
2066
49
    sym->u.syment._n._n_n._n_offset =
2067
49
      (uintptr_t) (debug_sec_data
2068
49
             + sym->u.syment._n._n_n._n_offset);
2069
3.42k
      }
2070
3.49M
  }
2071
3.50M
    }
2072
2073
  /* Free the raw symbols.  */
2074
12.5k
  if (obj_coff_external_syms (abfd) != NULL
2075
12.5k
      && ! obj_coff_keep_syms (abfd))
2076
12.2k
    {
2077
12.2k
      free (obj_coff_external_syms (abfd));
2078
12.2k
      obj_coff_external_syms (abfd) = NULL;
2079
12.2k
    }
2080
2081
12.5k
  obj_raw_syments (abfd) = internal;
2082
12.5k
  BFD_ASSERT (obj_raw_syment_count (abfd)
2083
12.5k
        == (size_t) (internal_ptr - internal));
2084
2085
12.5k
  return internal;
2086
25.9k
}
2087
2088
long
2089
coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2090
121k
{
2091
121k
  size_t count, raw;
2092
2093
121k
  count = asect->reloc_count;
2094
121k
  if (count >= LONG_MAX / sizeof (arelent *)
2095
121k
      || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
2096
0
    {
2097
0
      bfd_set_error (bfd_error_file_too_big);
2098
0
      return -1;
2099
0
    }
2100
121k
  if (!bfd_write_p (abfd))
2101
121k
    {
2102
121k
      ufile_ptr filesize = bfd_get_file_size (abfd);
2103
121k
      if (filesize != 0 && raw > filesize)
2104
78.4k
  {
2105
78.4k
    bfd_set_error (bfd_error_file_truncated);
2106
78.4k
    return -1;
2107
78.4k
  }
2108
121k
    }
2109
43.5k
  return (count + 1) * sizeof (arelent *);
2110
121k
}
2111
2112
asymbol *
2113
coff_make_empty_symbol (bfd *abfd)
2114
84.9M
{
2115
84.9M
  size_t amt = sizeof (coff_symbol_type);
2116
84.9M
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2117
2118
84.9M
  if (new_symbol == NULL)
2119
0
    return NULL;
2120
84.9M
  new_symbol->symbol.section = 0;
2121
84.9M
  new_symbol->native = NULL;
2122
84.9M
  new_symbol->lineno = NULL;
2123
84.9M
  new_symbol->done_lineno = false;
2124
84.9M
  new_symbol->symbol.the_bfd = abfd;
2125
2126
84.9M
  return & new_symbol->symbol;
2127
84.9M
}
2128
2129
/* Make a debugging symbol.  */
2130
2131
asymbol *
2132
coff_bfd_make_debug_symbol (bfd *abfd)
2133
0
{
2134
0
  size_t amt = sizeof (coff_symbol_type);
2135
0
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2136
2137
0
  if (new_symbol == NULL)
2138
0
    return NULL;
2139
  /* @@ The 10 is a guess at a plausible maximum number of aux entries
2140
     (but shouldn't be a constant).  */
2141
0
  amt = sizeof (combined_entry_type) * 10;
2142
0
  new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2143
0
  if (!new_symbol->native)
2144
0
    return NULL;
2145
0
  new_symbol->native->is_sym = true;
2146
0
  new_symbol->symbol.section = bfd_abs_section_ptr;
2147
0
  new_symbol->symbol.flags = BSF_DEBUGGING;
2148
0
  new_symbol->lineno = NULL;
2149
0
  new_symbol->done_lineno = false;
2150
0
  new_symbol->symbol.the_bfd = abfd;
2151
2152
0
  return & new_symbol->symbol;
2153
0
}
2154
2155
void
2156
coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2157
10.4k
{
2158
10.4k
  bfd_symbol_info (symbol, ret);
2159
2160
10.4k
  if (coffsymbol (symbol)->native != NULL
2161
10.4k
      && coffsymbol (symbol)->native->fix_value
2162
10.4k
      && coffsymbol (symbol)->native->is_sym)
2163
0
    ret->value
2164
0
      = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2165
0
    - (uintptr_t) obj_raw_syments (abfd))
2166
0
   / sizeof (combined_entry_type));
2167
10.4k
}
2168
2169
/* Print out information about COFF symbol.  */
2170
2171
void
2172
coff_print_symbol (bfd *abfd,
2173
       void * filep,
2174
       asymbol *symbol,
2175
       bfd_print_symbol_type how)
2176
0
{
2177
0
  FILE * file = (FILE *) filep;
2178
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2179
0
       ? symbol->name : _("<corrupt>"));
2180
2181
0
  switch (how)
2182
0
    {
2183
0
    case bfd_print_symbol_name:
2184
0
      fprintf (file, "%s", symname);
2185
0
      break;
2186
2187
0
    case bfd_print_symbol_more:
2188
0
      fprintf (file, "coff %s %s",
2189
0
         coffsymbol (symbol)->native ? "n" : "g",
2190
0
         coffsymbol (symbol)->lineno ? "l" : " ");
2191
0
      break;
2192
2193
0
    case bfd_print_symbol_all:
2194
0
      if (coffsymbol (symbol)->native)
2195
0
  {
2196
0
    bfd_vma val;
2197
0
    unsigned int aux;
2198
0
    combined_entry_type *combined = coffsymbol (symbol)->native;
2199
0
    combined_entry_type *root = obj_raw_syments (abfd);
2200
0
    struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2201
2202
0
    fprintf (file, "[%3ld]", (long) (combined - root));
2203
2204
    /* PR 17512: file: 079-33786-0.001:0.1.  */
2205
0
    if (combined < obj_raw_syments (abfd)
2206
0
        || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2207
0
      {
2208
0
        fprintf (file, _("<corrupt info> %s"), symname);
2209
0
        break;
2210
0
      }
2211
2212
0
    BFD_ASSERT (combined->is_sym);
2213
0
    if (! combined->fix_value)
2214
0
      val = (bfd_vma) combined->u.syment.n_value;
2215
0
    else
2216
0
      val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2217
0
       / sizeof (combined_entry_type));
2218
2219
0
    fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2220
0
       combined->u.syment.n_scnum,
2221
0
       combined->u.syment.n_flags,
2222
0
       combined->u.syment.n_type,
2223
0
       combined->u.syment.n_sclass,
2224
0
       combined->u.syment.n_numaux);
2225
0
    bfd_fprintf_vma (abfd, file, val);
2226
0
    fprintf (file, " %s", symname);
2227
2228
0
    for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2229
0
      {
2230
0
        combined_entry_type *auxp = combined + aux + 1;
2231
0
        long tagndx;
2232
2233
0
        BFD_ASSERT (! auxp->is_sym);
2234
0
        if (auxp->fix_tag)
2235
0
    tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2236
0
        else
2237
0
    tagndx = auxp->u.auxent.x_sym.x_tagndx.u32;
2238
2239
0
        fprintf (file, "\n");
2240
2241
0
        if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2242
0
    continue;
2243
2244
0
        switch (combined->u.syment.n_sclass)
2245
0
    {
2246
0
    case C_FILE:
2247
0
      fprintf (file, "File ");
2248
      /* Add additional information if this isn't the filename
2249
         auxiliary entry.  */
2250
0
      if (auxp->u.auxent.x_file.x_ftype)
2251
0
        fprintf (file, "ftype %d fname \"%s\"",
2252
0
           auxp->u.auxent.x_file.x_ftype,
2253
0
           (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2254
0
      break;
2255
2256
0
    case C_DWARF:
2257
0
      fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64,
2258
0
         auxp->u.auxent.x_sect.x_scnlen,
2259
0
         auxp->u.auxent.x_sect.x_nreloc);
2260
0
      break;
2261
2262
0
    case C_STAT:
2263
0
      if (combined->u.syment.n_type == T_NULL)
2264
        /* Probably a section symbol ?  */
2265
0
        {
2266
0
          fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2267
0
             (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2268
0
             auxp->u.auxent.x_scn.x_nreloc,
2269
0
             auxp->u.auxent.x_scn.x_nlinno);
2270
0
          if (auxp->u.auxent.x_scn.x_checksum != 0
2271
0
        || auxp->u.auxent.x_scn.x_associated != 0
2272
0
        || auxp->u.auxent.x_scn.x_comdat != 0)
2273
0
      fprintf (file, " checksum 0x%x assoc %d comdat %d",
2274
0
         auxp->u.auxent.x_scn.x_checksum,
2275
0
         auxp->u.auxent.x_scn.x_associated,
2276
0
         auxp->u.auxent.x_scn.x_comdat);
2277
0
          break;
2278
0
        }
2279
      /* Fall through.  */
2280
0
    case C_EXT:
2281
0
    case C_AIX_WEAKEXT:
2282
0
      if (ISFCN (combined->u.syment.n_type))
2283
0
        {
2284
0
          long next, llnos;
2285
2286
0
          if (auxp->fix_end)
2287
0
      next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2288
0
             - root);
2289
0
          else
2290
0
      next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
2291
0
          llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2292
0
          fprintf (file,
2293
0
             "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2294
0
             tagndx,
2295
0
             (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2296
0
             llnos, next);
2297
0
          break;
2298
0
        }
2299
      /* Fall through.  */
2300
0
    default:
2301
0
      fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2302
0
         auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2303
0
         auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2304
0
         tagndx);
2305
0
      if (auxp->fix_end)
2306
0
        fprintf (file, " endndx %ld",
2307
0
           ((long)
2308
0
            (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2309
0
             - root)));
2310
0
      break;
2311
0
    }
2312
0
      }
2313
2314
0
    if (l)
2315
0
      {
2316
0
        fprintf (file, "\n%s :",
2317
0
           l->u.sym->name != bfd_symbol_error_name
2318
0
           ? l->u.sym->name : _("<corrupt>"));
2319
0
        l++;
2320
0
        while (l->line_number)
2321
0
    {
2322
0
      if (l->line_number > 0)
2323
0
        {
2324
0
          fprintf (file, "\n%4d : ", l->line_number);
2325
0
          bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2326
0
        }
2327
0
      l++;
2328
0
    }
2329
0
      }
2330
0
  }
2331
0
      else
2332
0
  {
2333
0
    bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2334
0
    fprintf (file, " %-5s %s %s %s",
2335
0
       symbol->section->name,
2336
0
       coffsymbol (symbol)->native ? "n" : "g",
2337
0
       coffsymbol (symbol)->lineno ? "l" : " ",
2338
0
       symname);
2339
0
  }
2340
0
    }
2341
0
}
2342
2343
/* Return whether a symbol name implies a local symbol.  In COFF,
2344
   local symbols generally start with ``.L''.  Most targets use this
2345
   function for the is_local_label_name entry point, but some may
2346
   override it.  */
2347
2348
bool
2349
_bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2350
             const char *name)
2351
0
{
2352
0
  return name[0] == '.' && name[1] == 'L';
2353
0
}
2354
2355
/* Provided a BFD, a section and an offset (in bytes, not octets) into the
2356
   section, calculate and return the name of the source file and the line
2357
   nearest to the wanted location.  */
2358
2359
bool
2360
coff_find_nearest_line_with_names (bfd *abfd,
2361
           asymbol **symbols,
2362
           asection *section,
2363
           bfd_vma offset,
2364
           const char **filename_ptr,
2365
           const char **functionname_ptr,
2366
           unsigned int *line_ptr,
2367
           const struct dwarf_debug_section *debug_sections)
2368
77.1k
{
2369
77.1k
  bool found;
2370
77.1k
  unsigned int i;
2371
77.1k
  unsigned int line_base;
2372
77.1k
  coff_data_type *cof = coff_data (abfd);
2373
  /* Run through the raw syments if available.  */
2374
77.1k
  combined_entry_type *p;
2375
77.1k
  combined_entry_type *pend;
2376
77.1k
  alent *l;
2377
77.1k
  struct coff_section_tdata *sec_data;
2378
77.1k
  size_t amt;
2379
2380
  /* Before looking through the symbol table, try to use a .stab
2381
     section to find the information.  */
2382
77.1k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2383
77.1k
               &found, filename_ptr,
2384
77.1k
               functionname_ptr, line_ptr,
2385
77.1k
               &coff_data(abfd)->line_info))
2386
113
    return false;
2387
2388
77.0k
  if (found)
2389
636
    return true;
2390
2391
  /* Also try examining DWARF2 debugging information.  */
2392
76.4k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2393
76.4k
             filename_ptr, functionname_ptr,
2394
76.4k
             line_ptr, NULL, debug_sections,
2395
76.4k
             &coff_data(abfd)->dwarf2_find_line_info))
2396
161
    return true;
2397
2398
76.2k
  sec_data = coff_section_data (abfd, section);
2399
2400
  /* If the DWARF lookup failed, but there is DWARF information available
2401
     then the problem might be that the file has been rebased.  This tool
2402
     changes the VMAs of all the sections, but it does not update the DWARF
2403
     information.  So try again, using a bias against the address sought.  */
2404
76.2k
  if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2405
76.2k
    {
2406
76.2k
      bfd_signed_vma bias = 0;
2407
2408
      /* Create a cache of the result for the next call.  */
2409
76.2k
      if (sec_data == NULL && section->owner == abfd)
2410
34.6k
  {
2411
34.6k
    amt = sizeof (struct coff_section_tdata);
2412
34.6k
    section->used_by_bfd = bfd_zalloc (abfd, amt);
2413
34.6k
    sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2414
34.6k
  }
2415
2416
76.2k
      if (sec_data != NULL && sec_data->saved_bias)
2417
7.01k
  bias = sec_data->bias;
2418
69.2k
      else if (symbols)
2419
795
  {
2420
795
    bias = _bfd_dwarf2_find_symbol_bias (symbols,
2421
795
                 & coff_data (abfd)->dwarf2_find_line_info);
2422
2423
795
    if (sec_data)
2424
795
      {
2425
795
        sec_data->saved_bias = true;
2426
795
        sec_data->bias = bias;
2427
795
      }
2428
795
  }
2429
2430
76.2k
      if (bias
2431
76.2k
    && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2432
832
              offset + bias,
2433
832
              filename_ptr, functionname_ptr,
2434
832
              line_ptr, NULL, debug_sections,
2435
832
              &coff_data(abfd)->dwarf2_find_line_info))
2436
230
  return true;
2437
76.2k
    }
2438
2439
76.0k
  *filename_ptr = 0;
2440
76.0k
  *functionname_ptr = 0;
2441
76.0k
  *line_ptr = 0;
2442
2443
  /* Don't try and find line numbers in a non coff file.  */
2444
76.0k
  if (!bfd_family_coff (abfd))
2445
0
    return false;
2446
2447
76.0k
  if (cof == NULL)
2448
0
    return false;
2449
2450
  /* Find the first C_FILE symbol.  */
2451
76.0k
  p = cof->raw_syments;
2452
76.0k
  if (!p)
2453
63.8k
    return false;
2454
2455
12.1k
  pend = p + cof->raw_syment_count;
2456
526k
  while (p < pend)
2457
521k
    {
2458
521k
      BFD_ASSERT (p->is_sym);
2459
521k
      if (p->u.syment.n_sclass == C_FILE)
2460
6.34k
  break;
2461
514k
      p += 1 + p->u.syment.n_numaux;
2462
514k
    }
2463
2464
12.1k
  if (p < pend)
2465
6.34k
    {
2466
6.34k
      bfd_vma sec_vma;
2467
6.34k
      bfd_vma maxdiff;
2468
2469
      /* Look through the C_FILE symbols to find the best one.  */
2470
6.34k
      sec_vma = bfd_section_vma (section);
2471
6.34k
      *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2472
6.34k
      maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2473
34.1k
      while (1)
2474
34.1k
  {
2475
34.1k
    bfd_vma file_addr;
2476
34.1k
    combined_entry_type *p2;
2477
2478
34.1k
    for (p2 = p + 1 + p->u.syment.n_numaux;
2479
281k
         p2 < pend;
2480
247k
         p2 += 1 + p2->u.syment.n_numaux)
2481
280k
      {
2482
280k
        BFD_ASSERT (p2->is_sym);
2483
280k
        if (p2->u.syment.n_scnum > 0
2484
280k
      && (section
2485
200k
          == coff_section_from_bfd_index (abfd,
2486
200k
                  p2->u.syment.n_scnum)))
2487
29.0k
    break;
2488
251k
        if (p2->u.syment.n_sclass == C_FILE)
2489
4.68k
    {
2490
4.68k
      p2 = pend;
2491
4.68k
      break;
2492
4.68k
    }
2493
251k
      }
2494
34.1k
    if (p2 >= pend)
2495
5.15k
      break;
2496
2497
29.0k
    file_addr = (bfd_vma) p2->u.syment.n_value;
2498
    /* PR 11512: Include the section address of the function name symbol.  */
2499
29.0k
    if (p2->u.syment.n_scnum > 0)
2500
29.0k
      file_addr += coff_section_from_bfd_index (abfd,
2501
29.0k
                  p2->u.syment.n_scnum)->vma;
2502
    /* We use <= MAXDIFF here so that if we get a zero length
2503
       file, we actually use the next file entry.  */
2504
29.0k
    if (p2 < pend
2505
29.0k
        && offset + sec_vma >= file_addr
2506
29.0k
        && offset + sec_vma - file_addr <= maxdiff)
2507
18.9k
      {
2508
18.9k
        *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2509
18.9k
        maxdiff = offset + sec_vma - p2->u.syment.n_value;
2510
18.9k
      }
2511
2512
29.0k
    if (p->u.syment.n_value >= cof->raw_syment_count)
2513
84
      break;
2514
2515
    /* Avoid endless loops on erroneous files by ensuring that
2516
       we always move forward in the file.  */
2517
28.9k
    if (p >= cof->raw_syments + p->u.syment.n_value)
2518
49
      break;
2519
2520
28.8k
    p = cof->raw_syments + p->u.syment.n_value;
2521
28.8k
    if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2522
1.06k
      break;
2523
28.8k
  }
2524
6.34k
    }
2525
2526
12.1k
  if (section->lineno_count == 0)
2527
9.03k
    {
2528
9.03k
      *functionname_ptr = NULL;
2529
9.03k
      *line_ptr = 0;
2530
9.03k
      return true;
2531
9.03k
    }
2532
2533
  /* Now wander though the raw linenumbers of the section.
2534
     If we have been called on this section before, and the offset
2535
     we want is further down then we can prime the lookup loop.  */
2536
3.16k
  if (sec_data != NULL
2537
3.16k
      && sec_data->i > 0
2538
3.16k
      && offset >= sec_data->offset)
2539
1.01k
    {
2540
1.01k
      i = sec_data->i;
2541
1.01k
      *functionname_ptr = sec_data->function;
2542
1.01k
      line_base = sec_data->line_base;
2543
1.01k
    }
2544
2.14k
  else
2545
2.14k
    {
2546
2.14k
      i = 0;
2547
2.14k
      line_base = 0;
2548
2.14k
    }
2549
2550
3.16k
  if (section->lineno != NULL)
2551
1.21k
    {
2552
1.21k
      bfd_vma last_value = 0;
2553
2554
1.21k
      l = &section->lineno[i];
2555
2556
7.24k
      for (; i < section->lineno_count; i++)
2557
6.81k
  {
2558
6.81k
    if (l->line_number == 0)
2559
5.27k
      {
2560
        /* Get the symbol this line number points at.  */
2561
5.27k
        coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2562
5.27k
        if (coff->symbol.value > offset)
2563
310
    break;
2564
2565
4.96k
        *functionname_ptr = coff->symbol.name;
2566
4.96k
        last_value = coff->symbol.value;
2567
4.96k
        if (coff->native)
2568
4.96k
    {
2569
4.96k
      combined_entry_type *s = coff->native;
2570
2571
4.96k
      BFD_ASSERT (s->is_sym);
2572
4.96k
      s = s + 1 + s->u.syment.n_numaux;
2573
2574
      /* In XCOFF a debugging symbol can follow the
2575
         function symbol.  */
2576
4.96k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2577
4.96k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2578
4.96k
          && s->u.syment.n_scnum == N_DEBUG)
2579
104
        s = s + 1 + s->u.syment.n_numaux;
2580
2581
      /* S should now point to the .bf of the function.  */
2582
4.96k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2583
4.96k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2584
4.96k
          && s->u.syment.n_numaux)
2585
734
        {
2586
          /* The linenumber is stored in the auxent.  */
2587
734
          union internal_auxent *a = &((s + 1)->u.auxent);
2588
2589
734
          line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2590
734
          *line_ptr = line_base;
2591
734
        }
2592
4.96k
    }
2593
4.96k
      }
2594
1.53k
    else
2595
1.53k
      {
2596
1.53k
        if (l->u.offset > offset)
2597
477
    break;
2598
1.06k
        *line_ptr = l->line_number + line_base - 1;
2599
1.06k
      }
2600
6.02k
    l++;
2601
6.02k
  }
2602
2603
      /* If we fell off the end of the loop, then assume that this
2604
   symbol has no line number info.  Otherwise, symbols with no
2605
   line number info get reported with the line number of the
2606
   last line of the last symbol which does have line number
2607
   info.  We use 0x100 as a slop to account for cases where the
2608
   last line has executable code.  */
2609
1.21k
      if (i >= section->lineno_count
2610
1.21k
    && last_value != 0
2611
1.21k
    && offset - last_value > 0x100)
2612
154
  {
2613
154
    *functionname_ptr = NULL;
2614
154
    *line_ptr = 0;
2615
154
  }
2616
1.21k
    }
2617
2618
  /* Cache the results for the next call.  */
2619
3.16k
  if (sec_data == NULL && section->owner == abfd)
2620
0
    {
2621
0
      amt = sizeof (struct coff_section_tdata);
2622
0
      section->used_by_bfd = bfd_zalloc (abfd, amt);
2623
0
      sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2624
0
    }
2625
2626
3.16k
  if (sec_data != NULL)
2627
3.16k
    {
2628
3.16k
      sec_data->offset = offset;
2629
3.16k
      sec_data->i = i - 1;
2630
3.16k
      sec_data->function = *functionname_ptr;
2631
3.16k
      sec_data->line_base = line_base;
2632
3.16k
    }
2633
2634
3.16k
  return true;
2635
12.1k
}
2636
2637
bool
2638
coff_find_nearest_line (bfd *abfd,
2639
      asymbol **symbols,
2640
      asection *section,
2641
      bfd_vma offset,
2642
      const char **filename_ptr,
2643
      const char **functionname_ptr,
2644
      unsigned int *line_ptr,
2645
      unsigned int *discriminator_ptr)
2646
77.1k
{
2647
77.1k
  if (discriminator_ptr)
2648
69.0k
    *discriminator_ptr = 0;
2649
77.1k
  return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2650
77.1k
              filename_ptr, functionname_ptr,
2651
77.1k
              line_ptr, dwarf_debug_sections);
2652
77.1k
}
2653
2654
bool
2655
coff_find_inliner_info (bfd *abfd,
2656
      const char **filename_ptr,
2657
      const char **functionname_ptr,
2658
      unsigned int *line_ptr)
2659
0
{
2660
0
  bool found;
2661
2662
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2663
0
           functionname_ptr, line_ptr,
2664
0
           &coff_data(abfd)->dwarf2_find_line_info);
2665
0
  return (found);
2666
0
}
2667
2668
int
2669
coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2670
0
{
2671
0
  size_t size;
2672
2673
0
  if (!bfd_link_relocatable (info))
2674
0
    size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2675
0
  else
2676
0
    size = bfd_coff_filhsz (abfd);
2677
2678
0
  size += abfd->section_count * bfd_coff_scnhsz (abfd);
2679
0
  return size;
2680
0
}
2681
2682
/* Change the class of a coff symbol held by BFD.  */
2683
2684
bool
2685
bfd_coff_set_symbol_class (bfd *   abfd,
2686
         asymbol *   symbol,
2687
         unsigned int  symbol_class)
2688
0
{
2689
0
  coff_symbol_type * csym;
2690
2691
0
  csym = coff_symbol_from (symbol);
2692
0
  if (csym == NULL)
2693
0
    {
2694
0
      bfd_set_error (bfd_error_invalid_operation);
2695
0
      return false;
2696
0
    }
2697
0
  else if (csym->native == NULL)
2698
0
    {
2699
      /* This is an alien symbol which no native coff backend data.
2700
   We cheat here by creating a fake native entry for it and
2701
   then filling in the class.  This code is based on that in
2702
   coff_write_alien_symbol().  */
2703
2704
0
      combined_entry_type * native;
2705
0
      size_t amt = sizeof (* native);
2706
2707
0
      native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2708
0
      if (native == NULL)
2709
0
  return false;
2710
2711
0
      native->is_sym = true;
2712
0
      native->u.syment.n_type   = T_NULL;
2713
0
      native->u.syment.n_sclass = symbol_class;
2714
2715
0
      if (bfd_is_und_section (symbol->section))
2716
0
  {
2717
0
    native->u.syment.n_scnum = N_UNDEF;
2718
0
    native->u.syment.n_value = symbol->value;
2719
0
  }
2720
0
      else if (bfd_is_com_section (symbol->section))
2721
0
  {
2722
0
    native->u.syment.n_scnum = N_UNDEF;
2723
0
    native->u.syment.n_value = symbol->value;
2724
0
  }
2725
0
      else
2726
0
  {
2727
0
    native->u.syment.n_scnum =
2728
0
      symbol->section->output_section->target_index;
2729
0
    native->u.syment.n_value = (symbol->value
2730
0
              + symbol->section->output_offset);
2731
0
    if (! obj_pe (abfd))
2732
0
      native->u.syment.n_value += symbol->section->output_section->vma;
2733
2734
    /* Copy the any flags from the file header into the symbol.
2735
       FIXME: Why?  */
2736
0
    native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2737
0
  }
2738
2739
0
      csym->native = native;
2740
0
    }
2741
0
  else
2742
0
    csym->native->u.syment.n_sclass = symbol_class;
2743
2744
0
  return true;
2745
0
}
2746
2747
bool
2748
_bfd_coff_section_already_linked (bfd *abfd,
2749
          asection *sec,
2750
          struct bfd_link_info *info)
2751
0
{
2752
0
  flagword flags;
2753
0
  const char *name, *key;
2754
0
  struct bfd_section_already_linked *l;
2755
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
2756
0
  struct coff_comdat_info *s_comdat;
2757
2758
0
  if (sec->output_section == bfd_abs_section_ptr)
2759
0
    return false;
2760
2761
0
  flags = sec->flags;
2762
0
  if ((flags & SEC_LINK_ONCE) == 0)
2763
0
    return false;
2764
2765
  /* The COFF backend linker doesn't support group sections.  */
2766
0
  if ((flags & SEC_GROUP) != 0)
2767
0
    return false;
2768
2769
0
  name = bfd_section_name (sec);
2770
0
  s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2771
2772
0
  if (s_comdat != NULL)
2773
0
    key = s_comdat->name;
2774
0
  else
2775
0
    {
2776
0
      if (startswith (name, ".gnu.linkonce.")
2777
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2778
0
  key++;
2779
0
      else
2780
  /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2781
     .xdata$<key> and .pdata$<key> only the first of which has a
2782
     comdat key.  Should these all match the LTO IR key?  */
2783
0
  key = name;
2784
0
    }
2785
2786
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
2787
0
  if (!already_linked_list)
2788
0
    goto bad;
2789
2790
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
2791
0
    {
2792
0
      struct coff_comdat_info *l_comdat;
2793
2794
0
      l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2795
2796
      /* The section names must match, and both sections must be
2797
   comdat and have the same comdat name, or both sections must
2798
   be non-comdat.  LTO IR plugin sections are an exception.  They
2799
   are always named .gnu.linkonce.t.<key> (<key> is some string)
2800
   and match any comdat section with comdat name of <key>, and
2801
   any linkonce section with the same suffix, ie.
2802
   .gnu.linkonce.*.<key>.  */
2803
0
      if (((s_comdat != NULL) == (l_comdat != NULL)
2804
0
     && strcmp (name, l->sec->name) == 0)
2805
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
2806
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
2807
0
  {
2808
    /* The section has already been linked.  See if we should
2809
       issue a warning.  */
2810
0
    return _bfd_handle_already_linked (sec, l, info);
2811
0
  }
2812
0
    }
2813
2814
  /* This is the first section with this name.  Record it.  */
2815
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2816
0
    {
2817
0
    bad:
2818
0
      info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
2819
0
    }
2820
0
  return false;
2821
0
}
2822
2823
/* Initialize COOKIE for input bfd ABFD. */
2824
2825
static bool
2826
init_reloc_cookie (struct coff_reloc_cookie *cookie,
2827
       struct bfd_link_info *info ATTRIBUTE_UNUSED,
2828
       bfd *abfd)
2829
0
{
2830
  /* Sometimes the symbol table does not yet have been loaded here.  */
2831
0
  bfd_coff_slurp_symbol_table (abfd);
2832
2833
0
  cookie->abfd = abfd;
2834
0
  cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2835
2836
0
  cookie->symbols = obj_symbols (abfd);
2837
2838
0
  return true;
2839
0
}
2840
2841
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
2842
2843
static void
2844
fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2845
       bfd *abfd ATTRIBUTE_UNUSED)
2846
0
{
2847
  /* Nothing to do.  */
2848
0
}
2849
2850
/* Initialize the relocation information in COOKIE for input section SEC
2851
   of input bfd ABFD.  */
2852
2853
static bool
2854
init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2855
      struct bfd_link_info *info ATTRIBUTE_UNUSED,
2856
      bfd *abfd,
2857
      asection *sec)
2858
0
{
2859
0
  if (sec->reloc_count == 0)
2860
0
    {
2861
0
      cookie->rels = NULL;
2862
0
      cookie->relend = NULL;
2863
0
      cookie->rel = NULL;
2864
0
      return true;
2865
0
    }
2866
2867
0
  cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2868
0
             0, NULL);
2869
2870
0
  if (cookie->rels == NULL)
2871
0
    return false;
2872
2873
0
  cookie->rel = cookie->rels;
2874
0
  cookie->relend = (cookie->rels + sec->reloc_count);
2875
0
  return true;
2876
0
}
2877
2878
/* Free the memory allocated by init_reloc_cookie_rels,
2879
   if appropriate.  */
2880
2881
static void
2882
fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2883
      asection *sec)
2884
0
{
2885
0
  if (cookie->rels
2886
      /* PR 20401.  The relocs may not have been cached, so check first.
2887
   If the relocs were loaded by init_reloc_cookie_rels() then this
2888
   will be the case.  FIXME: Would performance be improved if the
2889
   relocs *were* cached ?  */
2890
0
      && coff_section_data (NULL, sec)
2891
0
      && coff_section_data (NULL, sec)->relocs != cookie->rels)
2892
0
    free (cookie->rels);
2893
0
}
2894
2895
/* Initialize the whole of COOKIE for input section SEC.  */
2896
2897
static bool
2898
init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2899
             struct bfd_link_info *info,
2900
             asection *sec)
2901
0
{
2902
0
  if (!init_reloc_cookie (cookie, info, sec->owner))
2903
0
    return false;
2904
2905
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2906
0
    {
2907
0
      fini_reloc_cookie (cookie, sec->owner);
2908
0
      return false;
2909
0
    }
2910
0
  return true;
2911
0
}
2912
2913
/* Free the memory allocated by init_reloc_cookie_for_section,
2914
   if appropriate.  */
2915
2916
static void
2917
fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2918
             asection *sec)
2919
0
{
2920
0
  fini_reloc_cookie_rels (cookie, sec);
2921
0
  fini_reloc_cookie (cookie, sec->owner);
2922
0
}
2923
2924
static asection *
2925
_bfd_coff_gc_mark_hook (asection *sec,
2926
      struct bfd_link_info *info ATTRIBUTE_UNUSED,
2927
      struct internal_reloc *rel ATTRIBUTE_UNUSED,
2928
      struct coff_link_hash_entry *h,
2929
      struct internal_syment *sym)
2930
0
{
2931
0
  if (h != NULL)
2932
0
    {
2933
0
      switch (h->root.type)
2934
0
  {
2935
0
  case bfd_link_hash_defined:
2936
0
  case bfd_link_hash_defweak:
2937
0
    return h->root.u.def.section;
2938
2939
0
  case bfd_link_hash_common:
2940
0
    return h->root.u.c.p->section;
2941
2942
0
  case bfd_link_hash_undefweak:
2943
0
    if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2944
0
      {
2945
        /* PE weak externals.  A weak symbol may include an auxiliary
2946
     record indicating that if the weak symbol is not resolved,
2947
     another external symbol is used instead.  */
2948
0
        struct coff_link_hash_entry *h2 =
2949
0
    h->auxbfd->tdata.coff_obj_data->sym_hashes
2950
0
    [h->aux->x_sym.x_tagndx.u32];
2951
2952
0
        if (h2 && h2->root.type != bfd_link_hash_undefined)
2953
0
    return  h2->root.u.def.section;
2954
0
      }
2955
0
    break;
2956
2957
0
  case bfd_link_hash_undefined:
2958
0
  default:
2959
0
    break;
2960
0
  }
2961
0
      return NULL;
2962
0
    }
2963
2964
0
  return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2965
0
}
2966
2967
/* COOKIE->rel describes a relocation against section SEC, which is
2968
   a section we've decided to keep.  Return the section that contains
2969
   the relocation symbol, or NULL if no section contains it.  */
2970
2971
static asection *
2972
_bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2973
      coff_gc_mark_hook_fn gc_mark_hook,
2974
      struct coff_reloc_cookie *cookie)
2975
0
{
2976
0
  struct coff_link_hash_entry *h;
2977
2978
0
  h = cookie->sym_hashes[cookie->rel->r_symndx];
2979
0
  if (h != NULL)
2980
0
    {
2981
0
      while (h->root.type == bfd_link_hash_indirect
2982
0
       || h->root.type == bfd_link_hash_warning)
2983
0
  h = (struct coff_link_hash_entry *) h->root.u.i.link;
2984
2985
0
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2986
0
    }
2987
2988
0
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2989
0
        &(cookie->symbols
2990
0
          + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2991
0
}
2992
2993
static bool _bfd_coff_gc_mark
2994
  (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2995
2996
/* COOKIE->rel describes a relocation against section SEC, which is
2997
   a section we've decided to keep.  Mark the section that contains
2998
   the relocation symbol.  */
2999
3000
static bool
3001
_bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
3002
       asection *sec,
3003
       coff_gc_mark_hook_fn gc_mark_hook,
3004
       struct coff_reloc_cookie *cookie)
3005
0
{
3006
0
  asection *rsec;
3007
3008
0
  rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
3009
0
  if (rsec && !rsec->gc_mark)
3010
0
    {
3011
0
      if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
3012
0
  rsec->gc_mark = 1;
3013
0
      else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
3014
0
  return false;
3015
0
    }
3016
0
  return true;
3017
0
}
3018
3019
/* The mark phase of garbage collection.  For a given section, mark
3020
   it and any sections in this section's group, and all the sections
3021
   which define symbols to which it refers.  */
3022
3023
static bool
3024
_bfd_coff_gc_mark (struct bfd_link_info *info,
3025
       asection *sec,
3026
       coff_gc_mark_hook_fn gc_mark_hook)
3027
0
{
3028
0
  bool ret = true;
3029
3030
0
  sec->gc_mark = 1;
3031
3032
  /* Look through the section relocs.  */
3033
0
  if ((sec->flags & SEC_RELOC) != 0
3034
0
      && sec->reloc_count > 0)
3035
0
    {
3036
0
      struct coff_reloc_cookie cookie;
3037
3038
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec))
3039
0
  ret = false;
3040
0
      else
3041
0
  {
3042
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
3043
0
      {
3044
0
        if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
3045
0
    {
3046
0
      ret = false;
3047
0
      break;
3048
0
    }
3049
0
      }
3050
0
    fini_reloc_cookie_for_section (&cookie, sec);
3051
0
  }
3052
0
    }
3053
3054
0
  return ret;
3055
0
}
3056
3057
static bool
3058
_bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
3059
          coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
3060
0
{
3061
0
  bfd *ibfd;
3062
3063
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3064
0
    {
3065
0
      asection *isec;
3066
0
      bool some_kept;
3067
3068
0
      if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
3069
0
  continue;
3070
3071
      /* Ensure all linker created sections are kept, and see whether
3072
   any other section is already marked.  */
3073
0
      some_kept = false;
3074
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3075
0
  {
3076
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
3077
0
      isec->gc_mark = 1;
3078
0
    else if (isec->gc_mark)
3079
0
      some_kept = true;
3080
0
  }
3081
3082
      /* If no section in this file will be kept, then we can
3083
   toss out debug sections.  */
3084
0
      if (!some_kept)
3085
0
  continue;
3086
3087
      /* Keep debug and special sections like .comment when they are
3088
   not part of a group, or when we have single-member groups.  */
3089
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3090
0
  if ((isec->flags & SEC_DEBUGGING) != 0
3091
0
      || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3092
0
    isec->gc_mark = 1;
3093
0
    }
3094
0
  return true;
3095
0
}
3096
3097
/* Sweep symbols in swept sections.  Called via coff_link_hash_traverse.  */
3098
3099
static bool
3100
coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
3101
          void *data ATTRIBUTE_UNUSED)
3102
0
{
3103
0
  if (h->root.type == bfd_link_hash_warning)
3104
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
3105
3106
0
  if ((h->root.type == bfd_link_hash_defined
3107
0
       || h->root.type == bfd_link_hash_defweak)
3108
0
      && !h->root.u.def.section->gc_mark
3109
0
      && !(h->root.u.def.section->owner->flags & DYNAMIC))
3110
0
    {
3111
      /* Do our best to hide the symbol.  */
3112
0
      h->root.u.def.section = bfd_und_section_ptr;
3113
0
      h->symbol_class = C_HIDDEN;
3114
0
    }
3115
3116
0
  return true;
3117
0
}
3118
3119
/* The sweep phase of garbage collection.  Remove all garbage sections.  */
3120
3121
typedef bool (*gc_sweep_hook_fn)
3122
  (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
3123
3124
static inline bool
3125
is_subsection (const char *str, const char *prefix)
3126
0
{
3127
0
  size_t n = strlen (prefix);
3128
0
  if (strncmp (str, prefix, n) != 0)
3129
0
    return false;
3130
0
  if (str[n] == 0)
3131
0
    return true;
3132
0
  else if (str[n] != '$')
3133
0
    return false;
3134
0
  return ISDIGIT (str[n + 1]) && str[n + 2] == 0;
3135
0
}
3136
3137
static bool
3138
coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3139
0
{
3140
0
  bfd *sub;
3141
3142
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3143
0
    {
3144
0
      asection *o;
3145
3146
0
      if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3147
0
  continue;
3148
3149
0
      for (o = sub->sections; o != NULL; o = o->next)
3150
0
  {
3151
      /* Keep debug and special sections.  */
3152
0
    if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3153
0
        || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3154
0
      o->gc_mark = 1;
3155
0
    else if (startswith (o->name, ".idata")
3156
0
       || startswith (o->name, ".pdata")
3157
0
       || startswith (o->name, ".xdata")
3158
0
       || is_subsection (o->name, ".didat")
3159
0
       || startswith (o->name, ".rsrc"))
3160
0
      o->gc_mark = 1;
3161
3162
0
    if (o->gc_mark)
3163
0
      continue;
3164
3165
    /* Skip sweeping sections already excluded.  */
3166
0
    if (o->flags & SEC_EXCLUDE)
3167
0
      continue;
3168
3169
    /* Since this is early in the link process, it is simple
3170
       to remove a section from the output.  */
3171
0
    o->flags |= SEC_EXCLUDE;
3172
3173
0
    if (info->print_gc_sections && o->size != 0)
3174
      /* xgettext: c-format */
3175
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3176
0
        o, sub);
3177
3178
#if 0
3179
    /* But we also have to update some of the relocation
3180
       info we collected before.  */
3181
    if (gc_sweep_hook
3182
        && (o->flags & SEC_RELOC) != 0
3183
        && o->reloc_count > 0
3184
        && !bfd_is_abs_section (o->output_section))
3185
      {
3186
        struct internal_reloc *internal_relocs;
3187
        bool r;
3188
3189
        internal_relocs
3190
    = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3191
               info->keep_memory);
3192
        if (internal_relocs == NULL)
3193
    return false;
3194
3195
        r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3196
3197
        if (coff_section_data (o)->relocs != internal_relocs)
3198
    free (internal_relocs);
3199
3200
        if (!r)
3201
    return false;
3202
      }
3203
#endif
3204
0
  }
3205
0
    }
3206
3207
  /* Remove the symbols that were in the swept sections from the dynamic
3208
     symbol table.  */
3209
0
  coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3210
0
         NULL);
3211
3212
0
  return true;
3213
0
}
3214
3215
/* Keep all sections containing symbols undefined on the command-line,
3216
   and the section containing the entry symbol.  */
3217
3218
static void
3219
_bfd_coff_gc_keep (struct bfd_link_info *info)
3220
0
{
3221
0
  struct bfd_sym_chain *sym;
3222
3223
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3224
0
    {
3225
0
      struct coff_link_hash_entry *h;
3226
3227
0
      h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3228
0
        false, false, false);
3229
3230
0
      if (h != NULL
3231
0
    && (h->root.type == bfd_link_hash_defined
3232
0
        || h->root.type == bfd_link_hash_defweak)
3233
0
    && !bfd_is_abs_section (h->root.u.def.section))
3234
0
  h->root.u.def.section->flags |= SEC_KEEP;
3235
0
    }
3236
0
}
3237
3238
/* Do mark and sweep of unused sections.  */
3239
3240
bool
3241
bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3242
0
{
3243
0
  bfd *sub;
3244
3245
  /* FIXME: Should we implement this? */
3246
#if 0
3247
  const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3248
3249
  if (!bed->can_gc_sections
3250
      || !is_coff_hash_table (info->hash))
3251
    {
3252
      _bfd_error_handler(_("warning: gc-sections option ignored"));
3253
      return true;
3254
    }
3255
#endif
3256
3257
0
  _bfd_coff_gc_keep (info);
3258
3259
  /* Grovel through relocs to find out who stays ...  */
3260
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3261
0
    {
3262
0
      asection *o;
3263
3264
0
      if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3265
0
  continue;
3266
3267
0
      for (o = sub->sections; o != NULL; o = o->next)
3268
0
  {
3269
0
    if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3270
0
         || startswith (o->name, ".vectors")
3271
0
         || startswith (o->name, ".ctors")
3272
0
         || startswith (o->name, ".dtors"))
3273
0
        && !o->gc_mark)
3274
0
      {
3275
0
        if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3276
0
    return false;
3277
0
      }
3278
0
  }
3279
0
    }
3280
3281
  /* Allow the backend to mark additional target specific sections.  */
3282
0
  _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3283
3284
  /* ... and mark SEC_EXCLUDE for those that go.  */
3285
0
  return coff_gc_sweep (abfd, info);
3286
0
}
3287
3288
/* Return name used to identify a comdat group.  */
3289
3290
const char *
3291
bfd_coff_group_name (bfd *abfd, const asection *sec)
3292
0
{
3293
0
  struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3294
0
  if (ci != NULL)
3295
0
    return ci->name;
3296
0
  return NULL;
3297
0
}
3298
3299
bool
3300
_bfd_coff_free_cached_info (bfd *abfd)
3301
1.15M
{
3302
1.15M
  struct coff_tdata *tdata;
3303
3304
1.15M
  if (bfd_family_coff (abfd)
3305
1.15M
      && (bfd_get_format (abfd) == bfd_object
3306
1.15M
    || bfd_get_format (abfd) == bfd_core)
3307
1.15M
      && (tdata = coff_data (abfd)) != NULL)
3308
1.08M
    {
3309
1.08M
      if (tdata->section_by_index)
3310
0
  {
3311
0
    htab_delete (tdata->section_by_index);
3312
0
    tdata->section_by_index = NULL;
3313
0
  }
3314
3315
1.08M
      if (tdata->section_by_target_index)
3316
94.2k
  {
3317
94.2k
    htab_delete (tdata->section_by_target_index);
3318
94.2k
    tdata->section_by_target_index = NULL;
3319
94.2k
  }
3320
3321
1.08M
      if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
3322
185k
  {
3323
185k
    htab_delete (pe_data (abfd)->comdat_hash);
3324
185k
    pe_data (abfd)->comdat_hash = NULL;
3325
185k
  }
3326
3327
1.08M
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3328
1.08M
      _bfd_stab_cleanup (abfd, &tdata->line_info);
3329
3330
      /* PR 25447:
3331
   Do not clear the keep_syms and keep_strings flags.
3332
   These may have been set by pe_ILF_build_a_bfd() indicating
3333
   that the syms and strings pointers are not to be freed.  */
3334
1.08M
      _bfd_coff_free_symbols (abfd);
3335
3336
      /* Free raw syms, and any other data bfd_alloc'd after raw syms
3337
   are read.  */
3338
1.08M
      if (!obj_coff_keep_raw_syms (abfd) && obj_raw_syments (abfd))
3339
12.5k
  {
3340
12.5k
    bfd_release (abfd, obj_raw_syments (abfd));
3341
12.5k
    obj_raw_syments (abfd) = NULL;
3342
12.5k
    obj_symbols (abfd) = NULL;
3343
12.5k
    obj_convert (abfd) = NULL;
3344
12.5k
  }
3345
1.08M
    }
3346
3347
1.15M
  return _bfd_generic_bfd_free_cached_info (abfd);
3348
1.15M
}