Coverage Report

Created: 2026-10-02 09:53

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