Coverage Report

Created: 2026-03-10 08:46

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