Coverage Report

Created: 2026-09-14 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/coffgen.c
Line
Count
Source
1
/* Support for the generic parts of COFF, for BFD.
2
   Copyright (C) 1990-2026 Free Software Foundation, Inc.
3
   Written by Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
/* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
23
   Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
24
25
/* This file contains COFF code that is not dependent on any
26
   particular COFF target.  There is only one version of this file in
27
   libbfd.a, so no target specific code may be put in here.  Or, to
28
   put it another way,
29
30
   ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31
32
   If you need to add some target specific behaviour, add a new hook
33
   function to bfd_coff_backend_data.
34
35
   Some of these functions are also called by the ECOFF routines.
36
   Those functions may not use any COFF specific information, such as
37
   coff_data (abfd).  */
38
39
#include "sysdep.h"
40
#include <limits.h>
41
#include "bfd.h"
42
#include "libbfd.h"
43
#include "coff/internal.h"
44
#include "libcoff.h"
45
#include "elf-bfd.h"
46
#include "hashtab.h"
47
#include "safe-ctype.h"
48
49
/* Extract a long section name at STRINDEX and copy it to the bfd objstack.
50
   Return NULL in case of error.  */
51
52
static char *
53
extract_long_section_name(bfd *abfd, unsigned long strindex)
54
10.9k
{
55
10.9k
  const char *strings;
56
10.9k
  char *name;
57
58
10.9k
  strings = _bfd_coff_read_string_table (abfd);
59
10.9k
  if (strings == NULL)
60
180
    return NULL;
61
10.7k
  if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
62
204
    return NULL;
63
10.5k
  strings += strindex;
64
10.5k
  name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
65
10.5k
  if (name == NULL)
66
0
    return NULL;
67
10.5k
  strcpy (name, strings);
68
69
10.5k
  return name;
70
10.5k
}
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
824
{
79
824
  unsigned i;
80
824
  uint32_t val;
81
82
824
  val = 0;
83
4.89k
  for (i = 0; i < len; i++)
84
4.40k
    {
85
4.40k
      char c = str[i];
86
4.40k
      unsigned d;
87
88
4.40k
      if (c >= 'A' && c <= 'Z')
89
2.73k
  d = c - 'A';
90
1.66k
      else if (c >= 'a' && c <= 'z')
91
294
  d = c - 'a' + 26;
92
1.37k
      else if (c >= '0' && c <= '9')
93
310
  d = c - '0' + 52;
94
1.06k
      else if (c == '+')
95
172
  d = 62;
96
893
      else if (c == '/')
97
699
  d = 63;
98
194
      else
99
194
  return false;
100
101
      /* Check for overflow. */
102
4.20k
      if ((val >> 26) != 0)
103
135
  return false;
104
105
4.07k
      val = (val << 6) + d;
106
4.07k
    }
107
108
495
  *res = val;
109
495
  return true;
110
824
}
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.36M
{
120
2.36M
  asection *newsect;
121
2.36M
  char *name;
122
2.36M
  bool result = true;
123
2.36M
  flagword flags;
124
125
2.36M
  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.36M
  if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
134
1.04M
      && hdr->s_name[0] == '/')
135
14.5k
    {
136
      /* Flag that this BFD uses long names, even though the format might
137
   expect them to be off by default.  This won't directly affect the
138
   format of any output BFD created from this one, but the information
139
   can be used to decide what to do.  */
140
14.5k
      bfd_coff_set_long_section_names (abfd, true);
141
142
14.5k
      if (hdr->s_name[1] == '/')
143
824
  {
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
824
    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
824
    if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
155
329
      return false;
156
157
495
    name = extract_long_section_name (abfd, strindex);
158
495
    if (name == NULL)
159
137
      return false;
160
495
  }
161
13.7k
      else
162
13.7k
  {
163
    /* PE classic long section name.  The '/' is followed by the index
164
       in the strtab.  The index is formatted as a decimal string.  */
165
13.7k
    char buf[SCNNMLEN];
166
13.7k
    long strindex;
167
13.7k
    char *p;
168
169
13.7k
    memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
170
13.7k
    buf[SCNNMLEN - 1] = '\0';
171
13.7k
    strindex = strtol (buf, &p, 10);
172
13.7k
    if (*p == '\0' && strindex >= 0)
173
10.4k
      {
174
10.4k
        name = extract_long_section_name (abfd, strindex);
175
10.4k
        if (name == NULL)
176
247
    return false;
177
10.4k
      }
178
13.7k
  }
179
14.5k
    }
180
181
2.36M
  if (name == NULL)
182
2.35M
    {
183
      /* Assorted wastage to null-terminate the name, thanks AT&T! */
184
2.35M
      name = (char *) bfd_alloc (abfd,
185
2.35M
         (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
186
2.35M
      if (name == NULL)
187
0
  return false;
188
2.35M
      strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
189
2.35M
      name[sizeof (hdr->s_name)] = 0;
190
2.35M
    }
191
192
2.36M
  newsect = bfd_make_section_anyway (abfd, name);
193
2.36M
  if (newsect == NULL)
194
0
    return false;
195
196
2.36M
  newsect->vma = hdr->s_vaddr;
197
2.36M
  newsect->lma = hdr->s_paddr;
198
2.36M
  newsect->size = hdr->s_size;
199
2.36M
  newsect->filepos = hdr->s_scnptr;
200
2.36M
  newsect->rel_filepos = hdr->s_relptr;
201
2.36M
  newsect->reloc_count = hdr->s_nreloc;
202
203
2.36M
  bfd_coff_set_alignment_hook (abfd, newsect, hdr);
204
205
2.36M
  newsect->line_filepos = hdr->s_lnnoptr;
206
207
2.36M
  newsect->lineno_count = hdr->s_nlnno;
208
2.36M
  newsect->userdata = NULL;
209
2.36M
  newsect->next = NULL;
210
2.36M
  newsect->target_index = target_index;
211
212
2.36M
  if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
213
38.1k
    result = false;
214
215
  /* At least on i386-coff, the line number count for a shared library
216
     section must be ignored.  */
217
2.36M
  if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
218
221k
    newsect->lineno_count = 0;
219
220
2.36M
  if (hdr->s_nreloc != 0)
221
1.16M
    flags |= SEC_RELOC;
222
  /* FIXME: should this check 'hdr->s_size > 0'.  */
223
2.36M
  if (hdr->s_scnptr != 0)
224
1.70M
    flags |= SEC_HAS_CONTENTS;
225
226
2.36M
  newsect->flags = flags;
227
228
  /* Compress/decompress DWARF debug sections.  */
229
2.36M
  if ((flags & SEC_DEBUGGING) != 0
230
237k
      && (flags & SEC_HAS_CONTENTS) != 0
231
202k
      && (startswith (name, ".debug_")
232
197k
    || startswith (name, ".zdebug_")
233
194k
    || startswith (name, ".gnu.debuglto_.debug_")
234
194k
    || startswith (name, ".gnu.linkonce.wi.")))
235
7.60k
    {
236
7.60k
      enum { nothing, compress, decompress } action = nothing;
237
238
7.60k
      if (bfd_is_section_compressed (abfd, newsect))
239
397
  {
240
    /* Compressed section.  Check if we should decompress.  */
241
397
    if ((abfd->flags & BFD_DECOMPRESS))
242
91
      action = decompress;
243
397
  }
244
7.20k
      else
245
7.20k
  {
246
    /* Normal section.  Check if we should compress.  */
247
7.20k
    if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
248
2.91k
      action = compress;
249
7.20k
  }
250
251
7.60k
      if (action == compress)
252
2.91k
  {
253
2.91k
    if (!bfd_init_section_compress_status (abfd, newsect))
254
433
      {
255
433
        _bfd_error_handler
256
    /* xgettext:c-format */
257
433
    (_("%pB: unable to compress section %s"), abfd, name);
258
433
        return false;
259
433
      }
260
2.91k
  }
261
4.68k
      else if (action == decompress)
262
91
  {
263
91
    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
91
    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
91
  }
281
7.60k
    }
282
283
2.36M
  return result;
284
2.36M
}
285
286
void
287
coff_object_cleanup (bfd *abfd)
288
216k
{
289
216k
  struct coff_tdata *td = coff_data (abfd);
290
216k
  if (td != NULL)
291
71.7k
    {
292
71.7k
      if (td->section_by_index)
293
0
  htab_delete (td->section_by_index);
294
71.7k
      if (td->section_by_target_index)
295
1
  htab_delete (td->section_by_target_index);
296
71.7k
      if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
297
27.2k
  htab_delete (pe_data (abfd)->comdat_hash);
298
71.7k
    }
299
216k
}
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
118k
    abfd->flags |= HAS_LINENO;
322
159k
  if (!(internal_f->f_flags & F_LSYMS))
323
109k
    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
129k
    abfd->flags |= HAS_SYMS;
332
333
159k
  if (internal_a != (struct internal_aouthdr *) NULL)
334
60.5k
    abfd->start_address = internal_a->entry;
335
98.9k
  else
336
98.9k
    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.01k
    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
255
    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.47M
      for (i = 0; i < nscns; i++)
360
2.36M
  {
361
2.36M
    struct internal_scnhdr tmp;
362
2.36M
    bfd_coff_swap_scnhdr_in (abfd,
363
2.36M
           (void *) (external_sections + i * scnhsz),
364
2.36M
           (void *) & tmp);
365
2.36M
    if (! make_a_section_from_file (abfd, &tmp, i + 1))
366
39.2k
      goto fail;
367
2.36M
  }
368
150k
    }
369
370
116k
  _bfd_coff_free_symbols (abfd);
371
372
116k
  if (obj_pe (abfd) && pe_data (abfd)->comdat_hash != NULL)
373
23.7k
    {
374
23.7k
      htab_delete (pe_data (abfd)->comdat_hash);
375
23.7k
      pe_data (abfd)->comdat_hash = NULL;
376
23.7k
    }
377
378
116k
  return coff_object_cleanup;
379
380
43.4k
 fail:
381
43.4k
  coff_object_cleanup (abfd);
382
43.4k
  _bfd_coff_free_symbols (abfd);
383
43.4k
  bfd_release (abfd, tdata);
384
43.4k
 fail2:
385
43.4k
  abfd->flags = oflags;
386
43.4k
  abfd->start_address = ostart;
387
43.4k
  return NULL;
388
43.4k
}
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.71M
{
396
6.71M
  bfd_size_type filhsz;
397
6.71M
  bfd_size_type aoutsz;
398
6.71M
  unsigned int nscns;
399
6.71M
  void * filehdr;
400
6.71M
  struct internal_filehdr internal_f;
401
6.71M
  struct internal_aouthdr internal_a;
402
403
  /* Figure out how much to read.  */
404
6.71M
  filhsz = bfd_coff_filhsz (abfd);
405
6.71M
  aoutsz = bfd_coff_aoutsz (abfd);
406
407
6.71M
  filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
408
6.71M
  if (filehdr == NULL)
409
256k
    {
410
256k
      if (bfd_get_error () != bfd_error_system_call)
411
250k
  bfd_set_error (bfd_error_wrong_format);
412
256k
      return NULL;
413
256k
    }
414
6.45M
  bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
415
6.45M
  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.45M
  if (! bfd_coff_bad_format_hook (abfd, &internal_f)
426
105k
      || internal_f.f_opthdr > aoutsz)
427
6.36M
    {
428
6.36M
      bfd_set_error (bfd_error_wrong_format);
429
6.36M
      return NULL;
430
6.36M
    }
431
93.0k
  nscns = internal_f.f_nscns;
432
433
93.0k
  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
156
  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
92.8k
  return coff_real_object_p (abfd, nscns, &internal_f,
450
92.8k
           (internal_f.f_opthdr != 0
451
92.8k
            ? &internal_a
452
92.8k
            : (struct internal_aouthdr *) NULL));
453
93.0k
}
454
455
static hashval_t
456
htab_hash_section_target_index (const void * entry)
457
559k
{
458
559k
  const struct bfd_section * sec = entry;
459
559k
  return sec->target_index;
460
559k
}
461
462
static int
463
htab_eq_section_target_index (const void * e1, const void * e2)
464
142k
{
465
142k
  const struct bfd_section * sec1 = e1;
466
142k
  const struct bfd_section * sec2 = e2;
467
142k
  return sec1->target_index == sec2->target_index;
468
142k
}
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
459k
{
475
459k
  if (section_index == N_ABS)
476
3.66k
    return bfd_abs_section_ptr;
477
455k
  if (section_index == N_UNDEF)
478
256k
    return bfd_und_section_ptr;
479
199k
  if (section_index == N_DEBUG)
480
289
    return bfd_abs_section_ptr;
481
482
199k
  struct bfd_section *answer;
483
199k
  htab_t table = coff_data (abfd)->section_by_target_index;
484
485
199k
  if (!table)
486
16.0k
    {
487
16.0k
      table = htab_create (10, htab_hash_section_target_index,
488
16.0k
         htab_eq_section_target_index, NULL);
489
16.0k
      if (table == NULL)
490
0
  return bfd_und_section_ptr;
491
16.0k
      coff_data (abfd)->section_by_target_index = table;
492
16.0k
    }
493
494
199k
  if (htab_elements (table) == 0)
495
27.8k
    {
496
219k
      for (answer = abfd->sections; answer; answer = answer->next)
497
191k
  {
498
191k
    void **slot = htab_find_slot (table, answer, INSERT);
499
191k
    if (slot == NULL)
500
0
      return bfd_und_section_ptr;
501
191k
    *slot = answer;
502
191k
  }
503
27.8k
    }
504
505
199k
  struct bfd_section needle;
506
199k
  needle.target_index = section_index;
507
508
199k
  answer = htab_find (table, &needle);
509
199k
  if (answer != NULL)
510
21.3k
    return answer;
511
512
  /* Cover the unlikely case of sections added after the first call to
513
     this function.  */
514
4.31M
  for (answer = abfd->sections; answer; answer = answer->next)
515
4.13M
    if (answer->target_index == section_index)
516
485
      {
517
485
  void **slot = htab_find_slot (table, answer, INSERT);
518
485
  if (slot != NULL)
519
485
    *slot = answer;
520
485
  return answer;
521
485
      }
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
177k
  return bfd_und_section_ptr;
526
177k
}
527
528
/* Get the upper bound of a COFF symbol table.  */
529
530
long
531
coff_get_symtab_upper_bound (bfd *abfd)
532
31.7k
{
533
31.7k
  if (!bfd_coff_slurp_symbol_table (abfd))
534
26.6k
    return -1;
535
536
5.16k
  return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
537
31.7k
}
538
539
/* Canonicalize a COFF symbol table.  */
540
541
long
542
coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
543
5.16k
{
544
5.16k
  unsigned int counter;
545
5.16k
  coff_symbol_type *symbase;
546
5.16k
  coff_symbol_type **location = (coff_symbol_type **) alocation;
547
548
5.16k
  if (!bfd_coff_slurp_symbol_table (abfd))
549
0
    return -1;
550
551
5.16k
  symbase = obj_symbols (abfd);
552
5.16k
  counter = bfd_get_symcount (abfd);
553
36.3k
  while (counter-- > 0)
554
31.1k
    *location++ = symbase++;
555
556
5.16k
  *location = NULL;
557
558
5.16k
  return bfd_get_symcount (abfd);
559
5.16k
}
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
670k
{
569
  /* FIXME: It's not clear this will work correctly if sizeof
570
     (_n_zeroes) != 4.  */
571
670k
  if (sym->_n._n_n._n_zeroes != 0
572
405k
      || sym->_n._n_n._n_offset == 0)
573
435k
    {
574
435k
      memcpy (buf, sym->_n._n_name, SYMNMLEN);
575
435k
      buf[SYMNMLEN] = '\0';
576
435k
      return buf;
577
435k
    }
578
234k
  else
579
234k
    {
580
234k
      const char *strings;
581
582
234k
      BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
583
234k
      strings = obj_coff_strings (abfd);
584
234k
      if (strings == NULL)
585
117k
  {
586
117k
    strings = _bfd_coff_read_string_table (abfd);
587
117k
    if (strings == NULL)
588
109k
      return NULL;
589
117k
  }
590
125k
      if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
591
108k
  return NULL;
592
16.7k
      return strings + sym->_n._n_n._n_offset;
593
125k
    }
594
670k
}
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
1.41k
{
695
1.41k
  unsigned int limit = bfd_get_symcount (abfd);
696
1.41k
  unsigned int i;
697
1.41k
  int total = 0;
698
1.41k
  asymbol **p;
699
1.41k
  asection *s;
700
701
1.41k
  if (limit == 0)
702
1.02k
    {
703
      /* This may be from the backend linker, in which case the
704
   lineno_count in the sections is correct.  */
705
2.37k
      for (s = abfd->sections; s != NULL; s = s->next)
706
1.35k
  total += s->lineno_count;
707
1.02k
      return total;
708
1.02k
    }
709
710
1.44k
  for (s = abfd->sections; s != NULL; s = s->next)
711
1.05k
    BFD_ASSERT (s->lineno_count == 0);
712
713
4.80k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
714
4.40k
    {
715
4.40k
      asymbol *q_maybe = *p;
716
717
4.40k
      if (bfd_asymbol_bfd (q_maybe) != NULL
718
4.40k
    && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
719
4.40k
  {
720
4.40k
    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
4.40k
    if (q->lineno != NULL
726
6
        && 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
14
    {
734
14
      asection * sec = q->symbol.section->output_section;
735
736
      /* Do not try to update fields in read-only sections.  */
737
14
      if (! bfd_is_const_section (sec))
738
14
        sec->lineno_count ++;
739
740
14
      ++total;
741
14
      ++l;
742
14
    }
743
14
        while (l->line_number != 0);
744
2
      }
745
4.40k
  }
746
4.40k
    }
747
748
398
  return total;
749
1.41k
}
750
751
static void
752
fixup_symbol_value (bfd *abfd,
753
        coff_symbol_type *coff_symbol_ptr,
754
        struct internal_syment *syment)
755
4.35k
{
756
  /* Normalize the symbol flags.  */
757
4.35k
  if (coff_symbol_ptr->symbol.section
758
4.35k
      && bfd_is_com_section (coff_symbol_ptr->symbol.section))
759
36
    {
760
      /* A common symbol is undefined with a value.  */
761
36
      syment->n_scnum = N_UNDEF;
762
36
      syment->n_value = coff_symbol_ptr->symbol.value;
763
36
    }
764
4.32k
  else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
765
630
     && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
766
627
    {
767
627
      syment->n_value = coff_symbol_ptr->symbol.value;
768
627
    }
769
3.69k
  else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
770
3.54k
    {
771
3.54k
      syment->n_scnum = N_UNDEF;
772
3.54k
      syment->n_value = 0;
773
3.54k
    }
774
  /* FIXME: Do we need to handle the absolute section here?  */
775
150
  else
776
150
    {
777
150
      if (coff_symbol_ptr->symbol.section)
778
150
  {
779
150
    syment->n_scnum =
780
150
      coff_symbol_ptr->symbol.section->output_section->target_index;
781
782
150
    syment->n_value = (coff_symbol_ptr->symbol.value
783
150
           + coff_symbol_ptr->symbol.section->output_offset);
784
150
    if (! obj_pe (abfd))
785
19
      {
786
19
        syment->n_value += (syment->n_sclass == C_STATLAB)
787
19
    ? coff_symbol_ptr->symbol.section->output_section->lma
788
19
    : coff_symbol_ptr->symbol.section->output_section->vma;
789
19
      }
790
150
  }
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
150
    }
799
4.35k
}
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
398
{
811
398
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
812
398
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
813
398
  unsigned int native_index = 0;
814
398
  struct internal_syment *last_file = NULL;
815
398
  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
398
  {
828
398
    asymbol **newsyms;
829
398
    unsigned int i;
830
398
    bfd_size_type amt;
831
832
398
    amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
833
398
    newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
834
398
    if (!newsyms)
835
0
      return false;
836
398
    bfd_ptr->outsymbols = newsyms;
837
4.80k
    for (i = 0; i < symbol_count; i++)
838
4.40k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
839
4.36k
    || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
840
200
        && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
841
164
        && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
842
164
      || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
843
164
          == 0))))
844
112
  *newsyms++ = symbol_ptr_ptr[i];
845
846
4.80k
    for (i = 0; i < symbol_count; i++)
847
4.40k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
848
4.36k
    && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
849
200
    && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
850
164
        || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
851
164
      && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
852
164
          != 0))))
853
125
  *newsyms++ = symbol_ptr_ptr[i];
854
855
398
    *first_undef = newsyms - bfd_ptr->outsymbols;
856
857
4.80k
    for (i = 0; i < symbol_count; i++)
858
4.40k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
859
4.36k
    && bfd_is_und_section (symbol_ptr_ptr[i]->section))
860
4.16k
  *newsyms++ = symbol_ptr_ptr[i];
861
398
    *newsyms = (asymbol *) NULL;
862
398
    symbol_ptr_ptr = bfd_ptr->outsymbols;
863
398
  }
864
865
4.80k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
866
4.40k
    {
867
4.40k
      coff_symbol_type *coff_symbol_ptr;
868
869
4.40k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
870
4.40k
      symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
871
4.40k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
872
4.40k
  {
873
4.40k
    combined_entry_type *s = coff_symbol_ptr->native;
874
4.40k
    int i;
875
876
4.40k
    BFD_ASSERT (s->is_sym);
877
4.40k
    if (s->u.syment.n_sclass == C_FILE)
878
47
      {
879
47
        if (last_file != NULL)
880
8
    last_file->n_value = native_index;
881
47
        last_file = &(s->u.syment);
882
47
      }
883
4.35k
    else
884
      /* Modify the symbol values according to their section and
885
         type.  */
886
4.35k
      fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
887
888
17.3k
    for (i = 0; i < s->u.syment.n_numaux + 1; i++)
889
12.9k
      s[i].offset = native_index++;
890
4.40k
  }
891
0
      else
892
0
  native_index++;
893
4.40k
    }
894
895
398
  obj_conv_table_size (bfd_ptr) = native_index;
896
897
398
  return true;
898
398
}
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
398
{
907
398
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
908
398
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
909
398
  unsigned int symbol_index;
910
911
4.80k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
912
4.40k
    {
913
4.40k
      coff_symbol_type *coff_symbol_ptr;
914
915
4.40k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
916
4.40k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
917
4.40k
  {
918
4.40k
    int i;
919
4.40k
    combined_entry_type *s = coff_symbol_ptr->native;
920
921
4.40k
    BFD_ASSERT (s->is_sym);
922
4.40k
    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
4.40k
    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
12.9k
    for (i = 0; i < s->u.syment.n_numaux; i++)
943
8.56k
      {
944
8.56k
        combined_entry_type *a = s + i + 1;
945
946
8.56k
        BFD_ASSERT (! a->is_sym);
947
8.56k
        if (a->fix_tag)
948
1.93k
    {
949
1.93k
      a->u.auxent.x_sym.x_tagndx.u32 =
950
1.93k
        a->u.auxent.x_sym.x_tagndx.p->offset;
951
1.93k
      a->fix_tag = 0;
952
1.93k
    }
953
8.56k
        if (a->fix_end)
954
71
    {
955
71
      a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
956
71
        a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
957
71
      a->fix_end = 0;
958
71
    }
959
8.56k
        if (a->fix_scnlen)
960
0
    {
961
0
      a->u.auxent.x_csect.x_scnlen.u64 =
962
0
        a->u.auxent.x_csect.x_scnlen.p->offset;
963
0
      a->fix_scnlen = 0;
964
0
    }
965
8.56k
      }
966
4.40k
  }
967
4.40k
    }
968
398
}
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
33
{
977
33
  unsigned int str_length = strlen (str);
978
33
  unsigned int filnmlen = bfd_coff_filnmlen (abfd);
979
980
33
  if (bfd_coff_long_filenames (abfd))
981
18
    {
982
18
      if (str_length <= filnmlen)
983
17
  strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
984
1
      else
985
1
  {
986
1
    bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
987
988
1
    if (indx == (bfd_size_type) -1)
989
0
      return false;
990
991
1
    auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
992
1
    auxent->x_file.x_n.x_n.x_zeroes = 0;
993
1
  }
994
18
    }
995
15
  else
996
15
    {
997
15
      strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
998
15
      if (str_length > filnmlen)
999
3
  str[filnmlen] = '\0';
1000
15
    }
1001
1002
33
  return true;
1003
33
}
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
4.40k
{
1014
4.40k
  unsigned int name_length;
1015
4.40k
  char *name = (char *) (symbol->name);
1016
4.40k
  bfd_size_type indx;
1017
1018
4.40k
  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
4.40k
  name_length = strlen (name);
1025
1026
4.40k
  BFD_ASSERT (native->is_sym);
1027
4.40k
  if (native->u.syment.n_sclass == C_FILE
1028
47
      && native->u.syment.n_numaux > 0)
1029
33
    {
1030
33
      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
33
      else
1040
33
  strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1041
1042
33
      BFD_ASSERT (! (native + 1)->is_sym);
1043
33
      if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1044
33
             strtab, hash))
1045
0
  return false;
1046
33
    }
1047
4.37k
  else
1048
4.37k
    {
1049
4.37k
      if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1050
  /* This name will fit into the symbol neatly.  */
1051
4.30k
  strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1052
1053
69
      else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1054
66
  {
1055
66
    indx = _bfd_stringtab_add (strtab, name, hash, false);
1056
66
    if (indx == (bfd_size_type) -1)
1057
0
      return false;
1058
1059
66
    native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1060
66
    native->u.syment._n._n_n._n_zeroes = 0;
1061
66
  }
1062
3
      else
1063
3
  {
1064
3
    file_ptr filepos;
1065
3
    bfd_byte buf[4];
1066
3
    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
3
    if (*debug_string_section_p == (asection *) NULL)
1074
2
      *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
1075
3
    filepos = bfd_tell (abfd);
1076
3
    if (prefix_len == 4)
1077
3
      bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
1078
0
    else
1079
3
      bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
1080
1081
3
    if (!bfd_set_section_contents (abfd,
1082
3
           *debug_string_section_p,
1083
3
           (void *) buf,
1084
3
           (file_ptr) *debug_string_size_p,
1085
3
           (bfd_size_type) prefix_len)
1086
3
        || !bfd_set_section_contents (abfd,
1087
3
              *debug_string_section_p,
1088
3
              (void *) symbol->name,
1089
3
              (file_ptr) (*debug_string_size_p
1090
3
              + prefix_len),
1091
3
              (bfd_size_type) name_length + 1))
1092
0
      abort ();
1093
3
    if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1094
0
      abort ();
1095
3
    native->u.syment._n._n_n._n_offset =
1096
3
        *debug_string_size_p + prefix_len;
1097
3
    native->u.syment._n._n_n._n_zeroes = 0;
1098
3
    *debug_string_size_p += name_length + 1 + prefix_len;
1099
3
  }
1100
4.37k
    }
1101
1102
4.40k
  return true;
1103
4.40k
}
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
4.40k
#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
4.40k
{
1123
4.40k
  unsigned int numaux = native->u.syment.n_numaux;
1124
4.40k
  int type = native->u.syment.n_type;
1125
4.40k
  int n_sclass = (int) native->u.syment.n_sclass;
1126
4.40k
  asection *output_section = symbol->section->output_section
1127
4.40k
             ? symbol->section->output_section
1128
4.40k
             : symbol->section;
1129
4.40k
  void * buf;
1130
4.40k
  bfd_size_type symesz;
1131
1132
4.40k
  BFD_ASSERT (native->is_sym);
1133
1134
4.40k
  if (native->u.syment.n_sclass == C_FILE)
1135
47
    symbol->flags |= BSF_DEBUGGING;
1136
1137
4.40k
  if (symbol->flags & BSF_DEBUGGING
1138
677
      && bfd_is_abs_section (symbol->section))
1139
15
    native->u.syment.n_scnum = N_DEBUG;
1140
1141
4.38k
  else if (bfd_is_abs_section (symbol->section))
1142
24
    native->u.syment.n_scnum = N_ABS;
1143
1144
4.36k
  else if (bfd_is_und_section (symbol->section))
1145
4.18k
    native->u.syment.n_scnum = N_UNDEF;
1146
1147
175
  else
1148
175
    native->u.syment.n_scnum =
1149
175
      output_section->target_index;
1150
1151
4.40k
  if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1152
4.40k
           debug_string_section_p, debug_string_size_p))
1153
0
    return false;
1154
1155
4.40k
  symesz = bfd_coff_symesz (abfd);
1156
4.40k
  buf = bfd_alloc (abfd, symesz);
1157
4.40k
  if (!buf)
1158
0
    return false;
1159
4.40k
  bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1160
4.40k
  if (bfd_write (buf, symesz, abfd) != symesz)
1161
0
    return false;
1162
4.40k
  bfd_release (abfd, buf);
1163
1164
4.40k
  if (native->u.syment.n_numaux > 0)
1165
1.06k
    {
1166
1.06k
      bfd_size_type auxesz;
1167
1.06k
      unsigned int j;
1168
1169
1.06k
      auxesz = bfd_coff_auxesz (abfd);
1170
1.06k
      buf = bfd_alloc (abfd, auxesz);
1171
1.06k
      if (!buf)
1172
0
  return false;
1173
9.63k
      for (j = 0; j < native->u.syment.n_numaux; j++)
1174
8.56k
  {
1175
8.56k
    BFD_ASSERT (! (native + j + 1)->is_sym);
1176
1177
    /* Adjust auxent only if this isn't the filename
1178
       auxiliary entry.  */
1179
8.56k
    if (native->u.syment.n_sclass == C_FILE
1180
805
        && (native + j + 1)->u.auxent.x_file.x_ftype
1181
1
        && (native + j + 1)->extrap)
1182
0
      coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1183
0
             &(native + j + 1)->u.auxent, strtab, hash);
1184
1185
8.56k
    bfd_coff_swap_aux_out (abfd,
1186
8.56k
         &((native + j + 1)->u.auxent),
1187
8.56k
         type, n_sclass, (int) j,
1188
8.56k
         native->u.syment.n_numaux,
1189
8.56k
         buf);
1190
8.56k
    if (bfd_write (buf, auxesz, abfd) != auxesz)
1191
0
      return false;
1192
8.56k
  }
1193
1.06k
      bfd_release (abfd, buf);
1194
1.06k
    }
1195
1196
  /* Store the index for use when we write out the relocs.  */
1197
4.40k
  set_index (symbol, *written);
1198
1199
4.40k
  *written += numaux + 1;
1200
4.40k
  return true;
1201
4.40k
}
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
4.40k
{
1334
4.40k
  combined_entry_type *native = symbol->native;
1335
4.40k
  alent *lineno = symbol->lineno;
1336
4.40k
  struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1337
1338
4.40k
  if ((!link_info || link_info->strip_discarded)
1339
4.40k
      && !bfd_is_abs_section (symbol->symbol.section)
1340
4.36k
      && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1341
0
    {
1342
0
      symbol->symbol.name = "";
1343
0
      return true;
1344
0
    }
1345
1346
4.40k
  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
4.40k
  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
14
      while (lineno[count].line_number != 0)
1366
12
  {
1367
12
    lineno[count].u.offset +=
1368
12
      (symbol->symbol.section->output_section->vma
1369
12
       + symbol->symbol.section->output_offset);
1370
12
    count++;
1371
12
  }
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
4.40k
  return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1380
4.40k
          strtab, true, debug_string_section_p,
1381
4.40k
          debug_string_size_p);
1382
4.40k
}
1383
1384
static void
1385
null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1386
        va_list ap ATTRIBUTE_UNUSED)
1387
3.18k
{
1388
3.18k
}
1389
1390
/* Write out the COFF symbols.  */
1391
1392
bool
1393
coff_write_symbols (bfd *abfd)
1394
408
{
1395
408
  struct bfd_strtab_hash *strtab;
1396
408
  asection *debug_string_section;
1397
408
  bfd_size_type debug_string_size;
1398
408
  unsigned int i;
1399
408
  unsigned int limit = bfd_get_symcount (abfd);
1400
408
  bfd_vma written = 0;
1401
408
  asymbol **p;
1402
1403
408
  debug_string_section = NULL;
1404
408
  debug_string_size = 0;
1405
1406
408
  strtab = _bfd_stringtab_init ();
1407
408
  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
408
  if (bfd_coff_long_section_names (abfd))
1415
93
    {
1416
93
      asection *o;
1417
1418
286
      for (o = abfd->sections; o != NULL; o = o->next)
1419
193
  if (strlen (o->name) > SCNNMLEN
1420
10
      && _bfd_stringtab_add (strtab, o->name, false, false)
1421
10
         == (bfd_size_type) -1)
1422
0
    return false;
1423
93
    }
1424
1425
  /* Seek to the right place.  */
1426
408
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1427
0
    return false;
1428
1429
  /* Output all the symbols we have.  */
1430
408
  written = 0;
1431
4.81k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1432
4.40k
    {
1433
4.40k
      asymbol *symbol = *p;
1434
4.40k
      coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1435
1436
4.40k
      if (c_symbol == (coff_symbol_type *) NULL
1437
4.40k
    || 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
4.40k
      else
1445
4.40k
  {
1446
4.40k
    if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1447
4.34k
      {
1448
4.34k
        bfd_error_handler_type current_error_handler;
1449
4.34k
        enum coff_symbol_classification sym_class;
1450
4.34k
        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
4.34k
        current_error_handler = bfd_set_error_handler (null_error_handler);
1457
4.34k
        BFD_ASSERT (c_symbol->native->is_sym);
1458
4.34k
        sym_class = bfd_coff_classify_symbol (abfd,
1459
4.34k
                &c_symbol->native->u.syment);
1460
4.34k
        (void) bfd_set_error_handler (current_error_handler);
1461
1462
4.34k
        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
4.34k
        if (symbol->flags & BSF_WEAK)
1472
61
    *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1473
4.28k
        else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1474
0
    *n_sclass = C_STAT;
1475
4.28k
        else if (symbol->flags & BSF_GLOBAL
1476
302
           && (sym_class != COFF_SYMBOL_GLOBAL
1477
#ifdef COFF_WITH_PE
1478
         || *n_sclass == C_NT_WEAK
1479
#endif
1480
45
         || *n_sclass == C_WEAKEXT))
1481
257
    c_symbol->native->u.syment.n_sclass = C_EXT;
1482
4.34k
      }
1483
1484
4.40k
    if (!coff_write_native_symbol (abfd, c_symbol, &written,
1485
4.40k
           strtab, &debug_string_section,
1486
4.40k
           &debug_string_size))
1487
0
      return false;
1488
4.40k
  }
1489
4.40k
    }
1490
1491
408
  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
408
  {
1499
408
    bfd_byte buffer[STRING_SIZE_SIZE];
1500
1501
408
#if STRING_SIZE_SIZE == 4
1502
408
    H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1503
#else
1504
 #error Change H_PUT_32
1505
#endif
1506
408
    if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1507
0
      return false;
1508
1509
408
    if (! _bfd_stringtab_emit (abfd, strtab))
1510
0
      return false;
1511
408
  }
1512
1513
408
  _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
408
  BFD_ASSERT (debug_string_size == 0
1521
408
        || (debug_string_section != (asection *) NULL
1522
408
      && (BFD_ALIGN (debug_string_size,
1523
408
         1 << debug_string_section->alignment_power)
1524
408
          == debug_string_section->size)));
1525
1526
408
  return true;
1527
408
}
1528
1529
bool
1530
coff_write_linenumbers (bfd *abfd)
1531
398
{
1532
398
  asection *s;
1533
398
  bfd_size_type linesz;
1534
398
  void * buff;
1535
1536
398
  linesz = bfd_coff_linesz (abfd);
1537
398
  buff = bfd_alloc (abfd, linesz);
1538
398
  if (!buff)
1539
0
    return false;
1540
1.45k
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1541
1.05k
    {
1542
1.05k
      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
5
    while (*q)
1549
3
      {
1550
3
        asymbol *p = *q;
1551
3
        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
14
          while (l->line_number)
1569
12
      {
1570
12
        out.l_lnno = l->line_number;
1571
12
        out.l_addr.l_symndx = l->u.offset;
1572
12
        bfd_coff_swap_lineno_out (abfd, &out, buff);
1573
12
        if (bfd_write (buff, linesz, abfd) != linesz)
1574
0
          return false;
1575
12
        l++;
1576
12
      }
1577
2
        }
1578
2
    }
1579
3
        q++;
1580
3
      }
1581
2
  }
1582
1.05k
    }
1583
398
  bfd_release (abfd, buff);
1584
398
  return true;
1585
398
}
1586
1587
alent *
1588
coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1589
62
{
1590
62
  return coffsymbol (symbol)->lineno;
1591
62
}
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.64M
{
1603
1.64M
  unsigned int type = symbol->u.syment.n_type;
1604
1.64M
  unsigned int n_sclass = symbol->u.syment.n_sclass;
1605
1606
1.64M
  BFD_ASSERT (symbol->is_sym);
1607
1.64M
  if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1608
114k
    {
1609
114k
      if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1610
114k
    (abfd, table_base, symbol, indaux, auxent))
1611
2.23k
  return;
1612
114k
    }
1613
1614
  /* Don't bother if this is a file or a section.  */
1615
1.64M
  if (n_sclass == C_STAT && type == T_NULL)
1616
28.9k
    return;
1617
1.61M
  if (n_sclass == C_FILE)
1618
114k
    return;
1619
1.50M
  if (n_sclass == C_DWARF)
1620
11.3k
    return;
1621
1622
1.49M
  BFD_ASSERT (! auxent->is_sym);
1623
  /* Otherwise patch up.  */
1624
1.49M
#define N_TMASK coff_data  (abfd)->local_n_tmask
1625
1.49M
#define N_BTSHFT coff_data (abfd)->local_n_btshft
1626
1627
1.49M
  if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1628
1.09M
       || n_sclass == C_FCN)
1629
410k
      && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1630
299k
      && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1631
299k
    < obj_raw_syment_count (abfd)))
1632
24.0k
    {
1633
24.0k
      auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1634
24.0k
  table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1635
24.0k
      auxent->fix_end = 1;
1636
24.0k
    }
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.49M
  if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1641
510k
    {
1642
510k
      auxent->u.auxent.x_sym.x_tagndx.p =
1643
510k
  table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1644
510k
      auxent->fix_tag = 1;
1645
510k
    }
1646
1.49M
}
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
76
{
1655
76
  char *debug_section;
1656
76
  file_ptr position;
1657
76
  bfd_size_type sec_size;
1658
1659
76
  asection *sect = bfd_get_section_by_name (abfd, ".debug");
1660
1661
76
  if (!sect)
1662
55
    {
1663
55
      bfd_set_error (bfd_error_no_debug_section);
1664
55
      return NULL;
1665
55
    }
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
21
  position = bfd_tell (abfd);
1672
21
  if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1673
3
    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
5
    return NULL;
1679
13
  debug_section[sec_size] = 0;
1680
1681
13
  if (bfd_seek (abfd, position, SEEK_SET) != 0)
1682
0
    return NULL;
1683
1684
13
  * sect_return = sect;
1685
13
  return debug_section;
1686
13
}
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
27.1k
{
1695
27.1k
  size_t len;
1696
27.1k
  char *newname;
1697
1698
294k
  for (len = 0; len < maxlen; ++len)
1699
280k
    if (name[len] == '\0')
1700
13.2k
      break;
1701
1702
27.1k
  if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1703
0
    return NULL;
1704
1705
27.1k
  strncpy (newname, name, len);
1706
27.1k
  newname[len] = '\0';
1707
27.1k
  return newname;
1708
27.1k
}
1709
1710
/* Read in the external symbols.  */
1711
1712
bool
1713
_bfd_coff_get_external_symbols (bfd *abfd)
1714
165k
{
1715
165k
  size_t symesz;
1716
165k
  size_t size;
1717
165k
  void * syms;
1718
165k
  ufile_ptr filesize;
1719
1720
165k
  if (obj_coff_external_syms (abfd) != NULL)
1721
20.2k
    return true;
1722
1723
145k
  symesz = bfd_coff_symesz (abfd);
1724
145k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1725
10.4k
    {
1726
10.4k
      bfd_set_error (bfd_error_file_truncated);
1727
10.4k
      return false;
1728
10.4k
    }
1729
1730
134k
  if (size == 0)
1731
31.1k
    return true;
1732
1733
103k
  filesize = bfd_get_file_size (abfd);
1734
103k
  if (filesize != 0
1735
103k
      && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1736
70.5k
    || size > filesize - obj_sym_filepos (abfd)))
1737
52.8k
    {
1738
52.8k
      bfd_set_error (bfd_error_file_truncated);
1739
52.8k
      return false;
1740
52.8k
    }
1741
1742
50.9k
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1743
0
    return false;
1744
50.9k
  syms = _bfd_malloc_and_read (abfd, size, size);
1745
50.9k
  obj_coff_external_syms (abfd) = syms;
1746
50.9k
  return syms != NULL;
1747
50.9k
}
1748
1749
/* Read in the external strings.  The strings are not loaded until
1750
   they are needed.  This is because we have no simple way of
1751
   detecting a missing string table in an archive.  If the strings
1752
   are loaded then the STRINGS and STRINGS_LEN fields in the
1753
   coff_tdata structure will be set.  */
1754
1755
const char *
1756
_bfd_coff_read_string_table (bfd *abfd)
1757
140k
{
1758
140k
  char extstrsize[STRING_SIZE_SIZE];
1759
140k
  bfd_size_type strsize;
1760
140k
  char *strings;
1761
140k
  ufile_ptr pos;
1762
140k
  ufile_ptr filesize;
1763
140k
  size_t symesz;
1764
140k
  size_t size;
1765
1766
140k
  if (obj_coff_strings (abfd) != NULL)
1767
6.01k
    return obj_coff_strings (abfd);
1768
1769
134k
  if (obj_sym_filepos (abfd) == 0)
1770
1.01k
    {
1771
1.01k
      bfd_set_error (bfd_error_no_symbols);
1772
1.01k
      return NULL;
1773
1.01k
    }
1774
1775
133k
  symesz = bfd_coff_symesz (abfd);
1776
133k
  pos = obj_sym_filepos (abfd);
1777
133k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1778
133k
      || pos + size < pos)
1779
45
    {
1780
45
      bfd_set_error (bfd_error_file_truncated);
1781
45
      return NULL;
1782
45
    }
1783
1784
133k
  if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1785
0
    return NULL;
1786
1787
133k
  if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1788
8.32k
    {
1789
8.32k
      if (bfd_get_error () != bfd_error_file_truncated)
1790
930
  return NULL;
1791
1792
      /* There is no string table.  */
1793
7.39k
      strsize = STRING_SIZE_SIZE;
1794
7.39k
    }
1795
124k
  else
1796
124k
    {
1797
124k
#if STRING_SIZE_SIZE == 4
1798
124k
      strsize = H_GET_32 (abfd, extstrsize);
1799
#else
1800
 #error Change H_GET_32
1801
#endif
1802
124k
    }
1803
1804
132k
  filesize = bfd_get_file_size (abfd);
1805
132k
  if (strsize < STRING_SIZE_SIZE
1806
100k
      || (filesize != 0 && strsize > filesize))
1807
103k
    {
1808
103k
      _bfd_error_handler
1809
  /* xgettext: c-format */
1810
103k
  (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1811
103k
      bfd_set_error (bfd_error_bad_value);
1812
103k
      return NULL;
1813
103k
    }
1814
1815
28.7k
  strings = (char *) bfd_malloc (strsize + 1);
1816
28.7k
  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
28.7k
  memset (strings, 0, STRING_SIZE_SIZE);
1824
1825
28.7k
  if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1826
28.7k
      != strsize - STRING_SIZE_SIZE)
1827
8.09k
    {
1828
8.09k
      free (strings);
1829
8.09k
      return NULL;
1830
8.09k
    }
1831
1832
20.6k
  obj_coff_strings (abfd) = strings;
1833
20.6k
  obj_coff_strings_len (abfd) = strsize;
1834
  /* Terminate the string table, just in case.  */
1835
20.6k
  strings[strsize] = 0;
1836
20.6k
  return strings;
1837
28.7k
}
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
251k
{
1844
251k
  if (! bfd_family_coff (abfd))
1845
7.00k
    return false;
1846
1847
244k
  if (obj_coff_external_syms (abfd) != NULL
1848
33.7k
      && ! obj_coff_keep_syms (abfd))
1849
32.9k
    {
1850
32.9k
      free (obj_coff_external_syms (abfd));
1851
32.9k
      obj_coff_external_syms (abfd) = NULL;
1852
32.9k
    }
1853
1854
244k
  if (obj_coff_strings (abfd) != NULL
1855
21.5k
      && ! obj_coff_keep_strings (abfd))
1856
20.6k
    {
1857
20.6k
      free (obj_coff_strings (abfd));
1858
20.6k
      obj_coff_strings (abfd) = NULL;
1859
20.6k
      obj_coff_strings_len (abfd) = 0;
1860
20.6k
    }
1861
1862
244k
  return true;
1863
251k
}
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
38.9k
{
1873
38.9k
  combined_entry_type *internal;
1874
38.9k
  combined_entry_type *internal_ptr;
1875
38.9k
  size_t symesz;
1876
38.9k
  char *raw_src;
1877
38.9k
  char *raw_end;
1878
38.9k
  const char *string_table = NULL;
1879
38.9k
  asection * debug_sec = NULL;
1880
38.9k
  char *debug_sec_data = NULL;
1881
38.9k
  size_t size;
1882
1883
38.9k
  if (obj_raw_syments (abfd) != NULL)
1884
0
    return obj_raw_syments (abfd);
1885
1886
38.9k
  if (! _bfd_coff_get_external_symbols (abfd))
1887
11.7k
    return NULL;
1888
1889
  /* Check for integer overflow.  */
1890
27.1k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
1891
27.1k
       sizeof (combined_entry_type), &size))
1892
0
    {
1893
0
      bfd_set_error (bfd_error_file_too_big);
1894
0
      return NULL;
1895
0
    }
1896
27.1k
  internal = bfd_zalloc (abfd, size);
1897
27.1k
  if (internal == NULL && size != 0)
1898
0
    return NULL;
1899
1900
27.1k
  raw_src = (char *) obj_coff_external_syms (abfd);
1901
1902
  /* Mark the end of the symbols.  */
1903
27.1k
  symesz = bfd_coff_symesz (abfd);
1904
27.1k
  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
27.1k
  for (internal_ptr = internal;
1911
528k
       raw_src < raw_end;
1912
501k
       raw_src += symesz, internal_ptr++)
1913
510k
    {
1914
510k
      unsigned int i;
1915
1916
510k
      bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1917
510k
          (void *) & internal_ptr->u.syment);
1918
510k
      internal_ptr->is_sym = true;
1919
510k
      combined_entry_type *sym = internal_ptr;
1920
1921
      /* PR 17512: Prevent buffer overrun.  */
1922
510k
      if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1923
4.26k
  {
1924
4.26k
    char buf[SYMNMLEN + 1];
1925
4.26k
    const char *name;
1926
1927
4.26k
    name = _bfd_coff_internal_syment_name (abfd, &sym->u.syment, buf);
1928
4.26k
    _bfd_error_handler
1929
      /* xgettext:c-format */
1930
4.26k
      (_("%pB: class %d symbol '%s' has missing aux entries"),
1931
4.26k
       abfd, sym->u.syment.n_sclass, name ? name : "");
1932
4.26k
    return NULL;
1933
4.26k
  }
1934
1935
2.15M
      for (i = 0; i < sym->u.syment.n_numaux; i++)
1936
1.64M
  {
1937
1.64M
    internal_ptr++;
1938
1.64M
    raw_src += symesz;
1939
1940
1.64M
    bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1941
1.64M
        sym->u.syment.n_type,
1942
1.64M
        sym->u.syment.n_sclass,
1943
1.64M
        (int) i, sym->u.syment.n_numaux,
1944
1.64M
        &(internal_ptr->u.auxent));
1945
1946
1.64M
    internal_ptr->is_sym = false;
1947
1.64M
    coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1948
1.64M
  }
1949
1950
506k
      if (sym->u.syment.n_sclass == C_FILE
1951
5.93k
    && sym->u.syment.n_numaux > 0)
1952
2.44k
  {
1953
2.44k
    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.44k
    BFD_ASSERT (! aux->is_sym);
1958
1959
2.44k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1960
475
      {
1961
        /* The filename is a long one, point into the string table.  */
1962
475
        if (string_table == NULL)
1963
335
    {
1964
335
      string_table = _bfd_coff_read_string_table (abfd);
1965
335
      if (string_table == NULL)
1966
187
        return NULL;
1967
335
    }
1968
1969
288
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1970
288
      >= obj_coff_strings_len (abfd))
1971
163
    sym->u.syment._n._n_n._n_offset =
1972
163
      (uintptr_t) bfd_symbol_error_name;
1973
125
        else
1974
125
    sym->u.syment._n._n_n._n_offset =
1975
125
      (uintptr_t) (string_table
1976
125
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
1977
288
      }
1978
1.97k
    else
1979
1.97k
      {
1980
        /* Ordinary short filename, put into memory anyway.  The
1981
     Microsoft PE tools sometimes store a filename in
1982
     multiple AUX entries.  */
1983
1.97k
        size_t len;
1984
1.97k
        char *src;
1985
1.97k
        if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1986
513
    {
1987
513
      len = sym->u.syment.n_numaux * symesz;
1988
513
      src = raw_src - (len - symesz);
1989
513
    }
1990
1.46k
        else
1991
1.46k
    {
1992
1.46k
      len = bfd_coff_filnmlen (abfd);
1993
1.46k
      src = aux->u.auxent.x_file.x_n.x_fname;
1994
1.46k
    }
1995
1.97k
        sym->u.syment._n._n_n._n_offset =
1996
1.97k
    (uintptr_t) copy_name (abfd, src, len);
1997
1.97k
      }
1998
1999
    /* Normalize other strings available in C_FILE aux entries.  */
2000
2.26k
    if (!obj_pe (abfd))
2001
1.41k
      for (int numaux = 1;
2002
45.5k
     numaux < sym->u.syment.n_numaux;
2003
44.1k
     numaux++)
2004
44.4k
        {
2005
44.4k
    aux = sym + numaux + 1;
2006
44.4k
    BFD_ASSERT (! aux->is_sym);
2007
2008
44.4k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
2009
19.3k
      {
2010
        /* The string information is a long one, point
2011
           into the string table.  */
2012
19.3k
        if (string_table == NULL)
2013
581
          {
2014
581
      string_table = _bfd_coff_read_string_table (abfd);
2015
581
      if (string_table == NULL)
2016
344
        return NULL;
2017
581
          }
2018
2019
18.9k
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
2020
18.9k
      >= obj_coff_strings_len (abfd))
2021
9.91k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
2022
9.91k
      (uintptr_t) bfd_symbol_error_name;
2023
9.06k
        else
2024
9.06k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
2025
9.06k
      (uintptr_t) (string_table
2026
9.06k
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
2027
18.9k
      }
2028
25.1k
    else
2029
25.1k
      aux->u.auxent.x_file.x_n.x_n.x_offset =
2030
25.1k
        ((uintptr_t)
2031
25.1k
         copy_name (abfd,
2032
25.1k
        aux->u.auxent.x_file.x_n.x_fname,
2033
25.1k
        bfd_coff_filnmlen (abfd)));
2034
44.4k
        }
2035
2036
2.26k
  }
2037
503k
      else
2038
503k
  {
2039
503k
    if (sym->u.syment._n._n_n._n_zeroes != 0)
2040
182k
      {
2041
        /* This is a "short" name.  Make it long.  */
2042
182k
        char *newstring;
2043
2044
        /* Find the length of this string without walking into memory
2045
     that isn't ours.  */
2046
710k
        for (i = 0; i < SYMNMLEN; ++i)
2047
670k
    if (sym->u.syment._n._n_name[i] == '\0')
2048
143k
      break;
2049
2050
182k
        newstring = bfd_alloc (abfd, i + 1);
2051
182k
        if (newstring == NULL)
2052
0
    return NULL;
2053
182k
        memcpy (newstring, sym->u.syment._n._n_name, i);
2054
182k
        newstring[i] = 0;
2055
182k
        sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2056
182k
        sym->u.syment._n._n_n._n_zeroes = 0;
2057
182k
      }
2058
321k
    else if (sym->u.syment._n._n_n._n_offset == 0)
2059
164k
      sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2060
156k
    else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2061
156k
      {
2062
        /* Long name already.  Point symbol at the string in the
2063
     table.  */
2064
156k
        if (string_table == NULL)
2065
10.5k
    {
2066
10.5k
      string_table = _bfd_coff_read_string_table (abfd);
2067
10.5k
      if (string_table == NULL)
2068
3.71k
        return NULL;
2069
10.5k
    }
2070
152k
        if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2071
138k
    sym->u.syment._n._n_n._n_offset =
2072
138k
      (uintptr_t) bfd_symbol_error_name;
2073
13.9k
        else
2074
13.9k
    sym->u.syment._n._n_n._n_offset =
2075
13.9k
      (uintptr_t) (string_table
2076
13.9k
             + sym->u.syment._n._n_n._n_offset);
2077
152k
      }
2078
173
    else
2079
173
      {
2080
        /* Long name in debug section.  Very similar.  */
2081
173
        if (debug_sec_data == NULL)
2082
76
    {
2083
76
      debug_sec_data = build_debug_section (abfd, &debug_sec);
2084
76
      if (debug_sec_data == NULL)
2085
63
        return NULL;
2086
76
    }
2087
        /* PR binutils/17512: Catch out of range offsets into
2088
     the debug data.  */
2089
110
        if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2090
110
    sym->u.syment._n._n_n._n_offset =
2091
110
      (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
110
      }
2097
503k
  }
2098
506k
    }
2099
2100
  /* Free the raw symbols.  */
2101
18.5k
  if (obj_coff_external_syms (abfd) != NULL
2102
16.5k
      && ! obj_coff_keep_syms (abfd))
2103
16.5k
    {
2104
16.5k
      free (obj_coff_external_syms (abfd));
2105
16.5k
      obj_coff_external_syms (abfd) = NULL;
2106
16.5k
    }
2107
2108
18.5k
  obj_raw_syments (abfd) = internal;
2109
18.5k
  BFD_ASSERT (obj_raw_syment_count (abfd)
2110
18.5k
        == (size_t) (internal_ptr - internal));
2111
2112
18.5k
  return internal;
2113
27.1k
}
2114
2115
long
2116
coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2117
96.4k
{
2118
96.4k
  size_t count, raw;
2119
2120
96.4k
  count = asect->reloc_count;
2121
96.4k
  if (count >= LONG_MAX / sizeof (arelent *)
2122
96.4k
      || _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
96.4k
  if (!bfd_write_p (abfd))
2128
96.4k
    {
2129
96.4k
      ufile_ptr filesize = bfd_get_file_size (abfd);
2130
96.4k
      if (filesize != 0 && raw > filesize)
2131
58.0k
  {
2132
58.0k
    bfd_set_error (bfd_error_file_truncated);
2133
58.0k
    return -1;
2134
58.0k
  }
2135
96.4k
    }
2136
38.3k
  return (count + 1) * sizeof (arelent *);
2137
96.4k
}
2138
2139
asymbol *
2140
coff_make_empty_symbol (bfd *abfd)
2141
2.20M
{
2142
2.20M
  size_t amt = sizeof (coff_symbol_type);
2143
2.20M
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2144
2145
2.20M
  if (new_symbol == NULL)
2146
0
    return NULL;
2147
2.20M
  new_symbol->symbol.section = 0;
2148
2.20M
  new_symbol->native = NULL;
2149
2.20M
  new_symbol->lineno = NULL;
2150
2.20M
  new_symbol->done_lineno = false;
2151
2.20M
  new_symbol->symbol.the_bfd = abfd;
2152
2153
2.20M
  return & new_symbol->symbol;
2154
2.20M
}
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
5.79k
{
2185
5.79k
  bfd_symbol_info (symbol, ret);
2186
2187
5.79k
  if (coffsymbol (symbol)->native != NULL
2188
5.79k
      && 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
5.79k
}
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
20.2k
{
2396
20.2k
  bool found;
2397
20.2k
  unsigned int i;
2398
20.2k
  unsigned int line_base;
2399
20.2k
  coff_data_type *cof = coff_data (abfd);
2400
  /* Run through the raw syments if available.  */
2401
20.2k
  combined_entry_type *p;
2402
20.2k
  combined_entry_type *pend;
2403
20.2k
  alent *l;
2404
20.2k
  struct coff_section_tdata *sec_data;
2405
20.2k
  size_t amt;
2406
2407
  /* Before looking through the symbol table, try to use a .stab
2408
     section to find the information.  */
2409
20.2k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2410
20.2k
               &found, filename_ptr,
2411
20.2k
               functionname_ptr, line_ptr,
2412
20.2k
               &coff_data(abfd)->line_info))
2413
179
    return false;
2414
2415
20.0k
  if (found)
2416
251
    return true;
2417
2418
  /* Also try examining DWARF2 debugging information.  */
2419
19.8k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2420
19.8k
             filename_ptr, functionname_ptr,
2421
19.8k
             line_ptr, NULL, debug_sections,
2422
19.8k
             &coff_data(abfd)->dwarf2_find_line_info))
2423
0
    return true;
2424
2425
19.8k
  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
19.8k
  if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2432
19.8k
    {
2433
19.8k
      bfd_signed_vma bias = 0;
2434
2435
      /* Create a cache of the result for the next call.  */
2436
19.8k
      if (sec_data == NULL && section->owner == abfd)
2437
7.56k
  {
2438
7.56k
    amt = sizeof (struct coff_section_tdata);
2439
7.56k
    section->used_by_bfd = bfd_zalloc (abfd, amt);
2440
7.56k
    sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2441
7.56k
  }
2442
2443
19.8k
      if (sec_data != NULL && sec_data->saved_bias)
2444
2.67k
  bias = sec_data->bias;
2445
17.1k
      else if (symbols)
2446
1.00k
  {
2447
1.00k
    bias = _bfd_dwarf2_find_symbol_bias (symbols,
2448
1.00k
                 & coff_data (abfd)->dwarf2_find_line_info);
2449
2450
1.00k
    if (sec_data)
2451
1.00k
      {
2452
1.00k
        sec_data->saved_bias = true;
2453
1.00k
        sec_data->bias = bias;
2454
1.00k
      }
2455
1.00k
  }
2456
2457
19.8k
      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
19.8k
    }
2465
2466
19.8k
  *filename_ptr = 0;
2467
19.8k
  *functionname_ptr = 0;
2468
19.8k
  *line_ptr = 0;
2469
2470
  /* Don't try and find line numbers in a non coff file.  */
2471
19.8k
  if (!bfd_family_coff (abfd))
2472
0
    return false;
2473
2474
19.8k
  if (cof == NULL)
2475
0
    return false;
2476
2477
  /* Find the first C_FILE symbol.  */
2478
19.8k
  p = cof->raw_syments;
2479
19.8k
  if (!p)
2480
13.1k
    return false;
2481
2482
6.68k
  pend = p + cof->raw_syment_count;
2483
87.4k
  while (p < pend)
2484
81.2k
    {
2485
81.2k
      BFD_ASSERT (p->is_sym);
2486
81.2k
      if (p->u.syment.n_sclass == C_FILE)
2487
510
  break;
2488
80.7k
      p += 1 + p->u.syment.n_numaux;
2489
80.7k
    }
2490
2491
6.68k
  if (p < pend)
2492
510
    {
2493
510
      bfd_vma sec_vma;
2494
510
      bfd_vma maxdiff;
2495
2496
      /* Look through the C_FILE symbols to find the best one.  */
2497
510
      sec_vma = bfd_section_vma (section);
2498
510
      *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2499
510
      maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2500
510
      while (1)
2501
510
  {
2502
510
    bfd_vma file_addr;
2503
510
    combined_entry_type *p2;
2504
2505
510
    for (p2 = p + 1 + p->u.syment.n_numaux;
2506
11.3k
         p2 < pend;
2507
10.7k
         p2 += 1 + p2->u.syment.n_numaux)
2508
11.0k
      {
2509
11.0k
        BFD_ASSERT (p2->is_sym);
2510
11.0k
        if (p2->u.syment.n_scnum > 0
2511
5.18k
      && (section
2512
5.18k
          == coff_section_from_bfd_index (abfd,
2513
5.18k
                  p2->u.syment.n_scnum)))
2514
74
    break;
2515
10.9k
        if (p2->u.syment.n_sclass == C_FILE)
2516
182
    {
2517
182
      p2 = pend;
2518
182
      break;
2519
182
    }
2520
10.9k
      }
2521
510
    if (p2 >= pend)
2522
436
      break;
2523
2524
74
    file_addr = (bfd_vma) p2->u.syment.n_value;
2525
    /* PR 11512: Include the section address of the function name symbol.  */
2526
74
    if (p2->u.syment.n_scnum > 0)
2527
74
      file_addr += coff_section_from_bfd_index (abfd,
2528
74
                  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
74
    if (p2 < pend
2532
74
        && offset + sec_vma >= file_addr
2533
37
        && offset + sec_vma - file_addr <= maxdiff)
2534
37
      {
2535
37
        *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2536
37
        maxdiff = offset + sec_vma - p2->u.syment.n_value;
2537
37
      }
2538
2539
74
    if (p->u.syment.n_value >= cof->raw_syment_count)
2540
32
      break;
2541
2542
    /* Avoid endless loops on erroneous files by ensuring that
2543
       we always move forward in the file.  */
2544
42
    if (p >= cof->raw_syments + p->u.syment.n_value)
2545
33
      break;
2546
2547
9
    p = cof->raw_syments + p->u.syment.n_value;
2548
9
    if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2549
9
      break;
2550
9
  }
2551
510
    }
2552
2553
6.68k
  if (section->lineno_count == 0)
2554
4.35k
    {
2555
4.35k
      *functionname_ptr = NULL;
2556
4.35k
      *line_ptr = 0;
2557
4.35k
      return true;
2558
4.35k
    }
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
2.33k
  if (sec_data != NULL
2564
2.33k
      && sec_data->i > 0
2565
1.30k
      && offset >= sec_data->offset)
2566
880
    {
2567
880
      i = sec_data->i;
2568
880
      *functionname_ptr = sec_data->function;
2569
880
      line_base = sec_data->line_base;
2570
880
    }
2571
1.45k
  else
2572
1.45k
    {
2573
1.45k
      i = 0;
2574
1.45k
      line_base = 0;
2575
1.45k
    }
2576
2577
2.33k
  if (section->lineno != NULL)
2578
894
    {
2579
894
      bfd_vma last_value = 0;
2580
2581
894
      l = &section->lineno[i];
2582
2583
3.33k
      for (; i < section->lineno_count; i++)
2584
2.84k
  {
2585
2.84k
    if (l->line_number == 0)
2586
2.18k
      {
2587
        /* Get the symbol this line number points at.  */
2588
2.18k
        coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2589
2.18k
        if (coff->symbol.value > offset)
2590
135
    break;
2591
2592
2.04k
        *functionname_ptr = coff->symbol.name;
2593
2.04k
        last_value = coff->symbol.value;
2594
2.04k
        if (coff->native)
2595
2.04k
    {
2596
2.04k
      combined_entry_type *s = coff->native;
2597
2598
2.04k
      BFD_ASSERT (s->is_sym);
2599
2.04k
      s = s + 1 + s->u.syment.n_numaux;
2600
2601
      /* In XCOFF a debugging symbol can follow the
2602
         function symbol.  */
2603
2.04k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2604
2.04k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2605
1.88k
          && s->u.syment.n_scnum == N_DEBUG)
2606
76
        s = s + 1 + s->u.syment.n_numaux;
2607
2608
      /* S should now point to the .bf of the function.  */
2609
2.04k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2610
2.04k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2611
1.84k
          && s->u.syment.n_numaux)
2612
740
        {
2613
          /* The linenumber is stored in the auxent.  */
2614
740
          union internal_auxent *a = &((s + 1)->u.auxent);
2615
2616
740
          line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2617
740
          *line_ptr = line_base;
2618
740
        }
2619
2.04k
    }
2620
2.04k
      }
2621
661
    else
2622
661
      {
2623
661
        if (l->u.offset > offset)
2624
272
    break;
2625
389
        *line_ptr = l->line_number + line_base - 1;
2626
389
      }
2627
2.43k
    l++;
2628
2.43k
  }
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
894
      if (i >= section->lineno_count
2637
487
    && last_value != 0
2638
219
    && offset - last_value > 0x100)
2639
206
  {
2640
206
    *functionname_ptr = NULL;
2641
206
    *line_ptr = 0;
2642
206
  }
2643
894
    }
2644
2645
  /* Cache the results for the next call.  */
2646
2.33k
  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
2.33k
  if (sec_data != NULL)
2654
2.33k
    {
2655
2.33k
      sec_data->offset = offset;
2656
2.33k
      sec_data->i = i - 1;
2657
2.33k
      sec_data->function = *functionname_ptr;
2658
2.33k
      sec_data->line_base = line_base;
2659
2.33k
    }
2660
2661
2.33k
  return true;
2662
6.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
20.2k
{
2674
20.2k
  if (discriminator_ptr)
2675
16.7k
    *discriminator_ptr = 0;
2676
20.2k
  return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2677
20.2k
              filename_ptr, functionname_ptr,
2678
20.2k
              line_ptr, dwarf_debug_sections);
2679
20.2k
}
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
1.98M
{
3333
1.98M
  struct coff_tdata *tdata;
3334
3335
1.98M
  if (bfd_family_coff (abfd)
3336
1.98M
      && (bfd_get_format (abfd) == bfd_object
3337
1.88M
    || bfd_get_format (abfd) == bfd_core)
3338
92.1k
      && (tdata = coff_data (abfd)) != NULL)
3339
92.1k
    {
3340
92.1k
      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
92.1k
      if (tdata->section_by_target_index)
3347
16.0k
  {
3348
16.0k
    htab_delete (tdata->section_by_target_index);
3349
16.0k
    tdata->section_by_target_index = NULL;
3350
16.0k
  }
3351
3352
92.1k
      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
92.1k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3359
92.1k
      _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
92.1k
      _bfd_coff_free_symbols (abfd);
3366
3367
      /* Free raw syms, and any other data bfd_alloc'd after raw syms
3368
   are read.  */
3369
92.1k
      if (!obj_coff_keep_raw_syms (abfd) && obj_raw_syments (abfd))
3370
18.5k
  {
3371
18.5k
    bfd_release (abfd, obj_raw_syments (abfd));
3372
18.5k
    obj_raw_syments (abfd) = NULL;
3373
18.5k
    obj_symbols (abfd) = NULL;
3374
18.5k
    obj_convert (abfd) = NULL;
3375
18.5k
  }
3376
92.1k
    }
3377
3378
1.98M
  return _bfd_generic_bfd_free_cached_info (abfd);
3379
1.98M
}