Coverage Report

Created: 2026-07-12 09:22

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