Coverage Report

Created: 2023-08-28 06:31

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