Coverage Report

Created: 2026-07-25 10:20

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