Coverage Report

Created: 2026-11-21 09:00

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