Coverage Report

Created: 2025-06-24 06:45

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