Coverage Report

Created: 2026-04-04 08:16

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
7.76k
{
55
7.76k
  const char *strings;
56
7.76k
  char *name;
57
58
7.76k
  strings = _bfd_coff_read_string_table (abfd);
59
7.76k
  if (strings == NULL)
60
132
    return NULL;
61
7.62k
  if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
62
178
    return NULL;
63
7.45k
  strings += strindex;
64
7.45k
  name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
65
7.45k
  if (name == NULL)
66
0
    return NULL;
67
7.45k
  strcpy (name, strings);
68
69
7.45k
  return name;
70
7.45k
}
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
501
{
79
501
  unsigned i;
80
501
  uint32_t val;
81
82
501
  val = 0;
83
2.89k
  for (i = 0; i < len; i++)
84
2.61k
    {
85
2.61k
      char c = str[i];
86
2.61k
      unsigned d;
87
88
2.61k
      if (c >= 'A' && c <= 'Z')
89
1.47k
  d = c - 'A';
90
1.13k
      else if (c >= 'a' && c <= 'z')
91
268
  d = c - 'a' + 26;
92
868
      else if (c >= '0' && c <= '9')
93
204
  d = c - '0' + 52;
94
664
      else if (c == '+')
95
94
  d = 62;
96
570
      else if (c == '/')
97
434
  d = 63;
98
136
      else
99
136
  return false;
100
101
      /* Check for overflow. */
102
2.47k
      if ((val >> 26) != 0)
103
87
  return false;
104
105
2.39k
      val = (val << 6) + d;
106
2.39k
    }
107
108
278
  *res = val;
109
278
  return true;
110
501
}
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.06M
{
120
2.06M
  asection *newsect;
121
2.06M
  char *name;
122
2.06M
  bool result = true;
123
2.06M
  flagword flags;
124
125
2.06M
  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.06M
  if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
134
846k
      && hdr->s_name[0] == '/')
135
9.75k
    {
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
9.75k
      bfd_coff_set_long_section_names (abfd, true);
141
142
9.75k
      if (hdr->s_name[1] == '/')
143
501
  {
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
501
    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
501
    if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
155
223
      return false;
156
157
278
    name = extract_long_section_name (abfd, strindex);
158
278
    if (name == NULL)
159
106
      return false;
160
278
  }
161
9.25k
      else
162
9.25k
  {
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
9.25k
    char buf[SCNNMLEN];
166
9.25k
    long strindex;
167
9.25k
    char *p;
168
169
9.25k
    memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
170
9.25k
    buf[SCNNMLEN - 1] = '\0';
171
9.25k
    strindex = strtol (buf, &p, 10);
172
9.25k
    if (*p == '\0' && strindex >= 0)
173
7.48k
      {
174
7.48k
        name = extract_long_section_name (abfd, strindex);
175
7.48k
        if (name == NULL)
176
204
    return false;
177
7.48k
      }
178
9.25k
  }
179
9.75k
    }
180
181
2.06M
  if (name == NULL)
182
2.05M
    {
183
      /* Assorted wastage to null-terminate the name, thanks AT&T! */
184
2.05M
      name = (char *) bfd_alloc (abfd,
185
2.05M
         (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
186
2.05M
      if (name == NULL)
187
0
  return false;
188
2.05M
      strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
189
2.05M
      name[sizeof (hdr->s_name)] = 0;
190
2.05M
    }
191
192
2.06M
  newsect = bfd_make_section_anyway (abfd, name);
193
2.06M
  if (newsect == NULL)
194
0
    return false;
195
196
2.06M
  newsect->vma = hdr->s_vaddr;
197
2.06M
  newsect->lma = hdr->s_paddr;
198
2.06M
  newsect->size = hdr->s_size;
199
2.06M
  newsect->filepos = hdr->s_scnptr;
200
2.06M
  newsect->rel_filepos = hdr->s_relptr;
201
2.06M
  newsect->reloc_count = hdr->s_nreloc;
202
203
2.06M
  bfd_coff_set_alignment_hook (abfd, newsect, hdr);
204
205
2.06M
  newsect->line_filepos = hdr->s_lnnoptr;
206
207
2.06M
  newsect->lineno_count = hdr->s_nlnno;
208
2.06M
  newsect->userdata = NULL;
209
2.06M
  newsect->next = NULL;
210
2.06M
  newsect->target_index = target_index;
211
212
2.06M
  if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
213
26.8k
    result = false;
214
215
  /* At least on i386-coff, the line number count for a shared library
216
     section must be ignored.  */
217
2.06M
  if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
218
199k
    newsect->lineno_count = 0;
219
220
2.06M
  if (hdr->s_nreloc != 0)
221
1.02M
    flags |= SEC_RELOC;
222
  /* FIXME: should this check 'hdr->s_size > 0'.  */
223
2.06M
  if (hdr->s_scnptr != 0)
224
1.48M
    flags |= SEC_HAS_CONTENTS;
225
226
2.06M
  newsect->flags = flags;
227
228
  /* Compress/decompress DWARF debug sections.  */
229
2.06M
  if ((flags & SEC_DEBUGGING) != 0
230
179k
      && (flags & SEC_HAS_CONTENTS) != 0
231
152k
      && (startswith (name, ".debug_")
232
151k
    || startswith (name, ".zdebug_")
233
150k
    || startswith (name, ".gnu.debuglto_.debug_")
234
150k
    || startswith (name, ".gnu.linkonce.wi.")))
235
2.77k
    {
236
2.77k
      enum { nothing, compress, decompress } action = nothing;
237
238
2.77k
      if (bfd_is_section_compressed (abfd, newsect))
239
220
  {
240
    /* Compressed section.  Check if we should decompress.  */
241
220
    if ((abfd->flags & BFD_DECOMPRESS))
242
0
      action = decompress;
243
220
  }
244
2.55k
      else
245
2.55k
  {
246
    /* Normal section.  Check if we should compress.  */
247
2.55k
    if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
248
234
      action = compress;
249
2.55k
  }
250
251
2.77k
      if (action == compress)
252
234
  {
253
234
    if (!bfd_init_section_compress_status (abfd, newsect))
254
40
      {
255
40
        _bfd_error_handler
256
    /* xgettext:c-format */
257
40
    (_("%pB: unable to compress section %s"), abfd, name);
258
40
        return false;
259
40
      }
260
234
  }
261
2.54k
      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
2.77k
    }
282
283
2.06M
  return result;
284
2.06M
}
285
286
void
287
coff_object_cleanup (bfd *abfd)
288
116k
{
289
116k
  struct coff_tdata *td = coff_data (abfd);
290
116k
  if (td != NULL)
291
47.4k
    {
292
47.4k
      if (td->section_by_index)
293
0
  htab_delete (td->section_by_index);
294
47.4k
      if (td->section_by_target_index)
295
0
  htab_delete (td->section_by_target_index);
296
47.4k
      if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
297
21.7k
  htab_delete (pe_data (abfd)->comdat_hash);
298
47.4k
    }
299
116k
}
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
103k
{
309
103k
  flagword oflags = abfd->flags;
310
103k
  bfd_vma ostart = bfd_get_start_address (abfd);
311
103k
  void * tdata;
312
103k
  bfd_size_type readsize; /* Length of file_info.  */
313
103k
  unsigned int scnhsz;
314
103k
  char *external_sections;
315
316
103k
  if (!(internal_f->f_flags & F_RELFLG))
317
77.1k
    abfd->flags |= HAS_RELOC;
318
103k
  if ((internal_f->f_flags & F_EXEC))
319
27.8k
    abfd->flags |= EXEC_P;
320
103k
  if (!(internal_f->f_flags & F_LNNO))
321
77.4k
    abfd->flags |= HAS_LINENO;
322
103k
  if (!(internal_f->f_flags & F_LSYMS))
323
72.6k
    abfd->flags |= HAS_LOCALS;
324
325
  /* FIXME: How can we set D_PAGED correctly?  */
326
103k
  if ((internal_f->f_flags & F_EXEC) != 0)
327
27.8k
    abfd->flags |= D_PAGED;
328
329
103k
  abfd->symcount = internal_f->f_nsyms;
330
103k
  if (internal_f->f_nsyms)
331
85.4k
    abfd->flags |= HAS_SYMS;
332
333
103k
  if (internal_a != (struct internal_aouthdr *) NULL)
334
43.8k
    abfd->start_address = internal_a->entry;
335
60.0k
  else
336
60.0k
    abfd->start_address = 0;
337
338
  /* Set up the tdata area.  ECOFF uses its own routine, and overrides
339
     abfd->flags.  */
340
103k
  tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
341
103k
  if (tdata == NULL)
342
0
    goto fail2;
343
344
103k
  scnhsz = bfd_coff_scnhsz (abfd);
345
103k
  readsize = (bfd_size_type) nscns * scnhsz;
346
103k
  external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
347
103k
  if (!external_sections)
348
2.68k
    goto fail;
349
350
  /* Set the arch/mach *before* swapping in sections; section header swapping
351
     may depend on arch/mach info.  */
352
101k
  if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
353
29
    goto fail;
354
355
  /* Now copy data as required; construct all asections etc.  */
356
101k
  if (nscns != 0)
357
99.0k
    {
358
99.0k
      unsigned int i;
359
2.13M
      for (i = 0; i < nscns; i++)
360
2.06M
  {
361
2.06M
    struct internal_scnhdr tmp;
362
2.06M
    bfd_coff_swap_scnhdr_in (abfd,
363
2.06M
           (void *) (external_sections + i * scnhsz),
364
2.06M
           (void *) & tmp);
365
2.06M
    if (! make_a_section_from_file (abfd, &tmp, i + 1))
366
27.4k
      goto fail;
367
2.06M
  }
368
99.0k
    }
369
370
73.7k
  _bfd_coff_free_symbols (abfd);
371
73.7k
  return coff_object_cleanup;
372
373
30.1k
 fail:
374
30.1k
  coff_object_cleanup (abfd);
375
30.1k
  _bfd_coff_free_symbols (abfd);
376
30.1k
  bfd_release (abfd, tdata);
377
30.1k
 fail2:
378
30.1k
  abfd->flags = oflags;
379
30.1k
  abfd->start_address = ostart;
380
30.1k
  return NULL;
381
30.1k
}
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
3.94M
{
389
3.94M
  bfd_size_type filhsz;
390
3.94M
  bfd_size_type aoutsz;
391
3.94M
  unsigned int nscns;
392
3.94M
  void * filehdr;
393
3.94M
  struct internal_filehdr internal_f;
394
3.94M
  struct internal_aouthdr internal_a;
395
396
  /* Figure out how much to read.  */
397
3.94M
  filhsz = bfd_coff_filhsz (abfd);
398
3.94M
  aoutsz = bfd_coff_aoutsz (abfd);
399
400
3.94M
  filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
401
3.94M
  if (filehdr == NULL)
402
80.5k
    {
403
80.5k
      if (bfd_get_error () != bfd_error_system_call)
404
80.4k
  bfd_set_error (bfd_error_wrong_format);
405
80.5k
      return NULL;
406
80.5k
    }
407
3.86M
  bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
408
3.86M
  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
3.86M
  if (! bfd_coff_bad_format_hook (abfd, &internal_f)
419
60.8k
      || internal_f.f_opthdr > aoutsz)
420
3.81M
    {
421
3.81M
      bfd_set_error (bfd_error_wrong_format);
422
3.81M
      return NULL;
423
3.81M
    }
424
53.7k
  nscns = internal_f.f_nscns;
425
426
53.7k
  if (internal_f.f_opthdr)
427
14.4k
    {
428
14.4k
      void * opthdr;
429
430
14.4k
      opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
431
14.4k
      if (opthdr == NULL)
432
100
  return NULL;
433
      /* PR 17512: file: 11056-1136-0.004.  */
434
14.3k
      if (internal_f.f_opthdr < aoutsz)
435
13.7k
  memset (((char *) opthdr) + internal_f.f_opthdr, 0,
436
13.7k
    aoutsz - internal_f.f_opthdr);
437
438
14.3k
      bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
439
14.3k
      bfd_release (abfd, opthdr);
440
14.3k
    }
441
442
53.6k
  return coff_real_object_p (abfd, nscns, &internal_f,
443
53.6k
           (internal_f.f_opthdr != 0
444
53.6k
            ? &internal_a
445
53.6k
            : (struct internal_aouthdr *) NULL));
446
53.7k
}
447
448
static hashval_t
449
htab_hash_section_target_index (const void * entry)
450
432k
{
451
432k
  const struct bfd_section * sec = entry;
452
432k
  return sec->target_index;
453
432k
}
454
455
static int
456
htab_eq_section_target_index (const void * e1, const void * e2)
457
109k
{
458
109k
  const struct bfd_section * sec1 = e1;
459
109k
  const struct bfd_section * sec2 = e2;
460
109k
  return sec1->target_index == sec2->target_index;
461
109k
}
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
336k
{
468
336k
  if (section_index == N_ABS)
469
2.89k
    return bfd_abs_section_ptr;
470
333k
  if (section_index == N_UNDEF)
471
190k
    return bfd_und_section_ptr;
472
142k
  if (section_index == N_DEBUG)
473
134
    return bfd_abs_section_ptr;
474
475
142k
  struct bfd_section *answer;
476
142k
  htab_t table = coff_data (abfd)->section_by_target_index;
477
478
142k
  if (!table)
479
7.54k
    {
480
7.54k
      table = htab_create (10, htab_hash_section_target_index,
481
7.54k
         htab_eq_section_target_index, NULL);
482
7.54k
      if (table == NULL)
483
0
  return bfd_und_section_ptr;
484
7.54k
      coff_data (abfd)->section_by_target_index = table;
485
7.54k
    }
486
487
142k
  if (htab_elements (table) == 0)
488
13.0k
    {
489
158k
      for (answer = abfd->sections; answer; answer = answer->next)
490
145k
  {
491
145k
    void **slot = htab_find_slot (table, answer, INSERT);
492
145k
    if (slot == NULL)
493
0
      return bfd_und_section_ptr;
494
145k
    *slot = answer;
495
145k
  }
496
13.0k
    }
497
498
142k
  struct bfd_section needle;
499
142k
  needle.target_index = section_index;
500
501
142k
  answer = htab_find (table, &needle);
502
142k
  if (answer != NULL)
503
15.9k
    return answer;
504
505
  /* Cover the unlikely case of sections added after the first call to
506
     this function.  */
507
3.48M
  for (answer = abfd->sections; answer; answer = answer->next)
508
3.35M
    if (answer->target_index == section_index)
509
508
      {
510
508
  void **slot = htab_find_slot (table, answer, INSERT);
511
508
  if (slot != NULL)
512
508
    *slot = answer;
513
508
  return answer;
514
508
      }
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
126k
  return bfd_und_section_ptr;
519
126k
}
520
521
/* Get the upper bound of a COFF symbol table.  */
522
523
long
524
coff_get_symtab_upper_bound (bfd *abfd)
525
13.1k
{
526
13.1k
  if (!bfd_coff_slurp_symbol_table (abfd))
527
12.3k
    return -1;
528
529
792
  return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
530
13.1k
}
531
532
/* Canonicalize a COFF symbol table.  */
533
534
long
535
coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
536
792
{
537
792
  unsigned int counter;
538
792
  coff_symbol_type *symbase;
539
792
  coff_symbol_type **location = (coff_symbol_type **) alocation;
540
541
792
  if (!bfd_coff_slurp_symbol_table (abfd))
542
0
    return -1;
543
544
792
  symbase = obj_symbols (abfd);
545
792
  counter = bfd_get_symcount (abfd);
546
15.9k
  while (counter-- > 0)
547
15.1k
    *location++ = symbase++;
548
549
792
  *location = NULL;
550
551
792
  return bfd_get_symcount (abfd);
552
792
}
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
524k
{
562
  /* FIXME: It's not clear this will work correctly if sizeof
563
     (_n_zeroes) != 4.  */
564
524k
  if (sym->_n._n_n._n_zeroes != 0
565
321k
      || sym->_n._n_n._n_offset == 0)
566
342k
    {
567
342k
      memcpy (buf, sym->_n._n_name, SYMNMLEN);
568
342k
      buf[SYMNMLEN] = '\0';
569
342k
      return buf;
570
342k
    }
571
181k
  else
572
181k
    {
573
181k
      const char *strings;
574
575
181k
      BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
576
181k
      strings = obj_coff_strings (abfd);
577
181k
      if (strings == NULL)
578
91.9k
  {
579
91.9k
    strings = _bfd_coff_read_string_table (abfd);
580
91.9k
    if (strings == NULL)
581
85.9k
      return NULL;
582
91.9k
  }
583
95.9k
      if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
584
79.1k
  return NULL;
585
16.8k
      return strings + sym->_n._n_n._n_offset;
586
95.9k
    }
587
524k
}
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
82
{
688
82
  unsigned int limit = bfd_get_symcount (abfd);
689
82
  unsigned int i;
690
82
  int total = 0;
691
82
  asymbol **p;
692
82
  asection *s;
693
694
82
  if (limit == 0)
695
56
    {
696
      /* This may be from the backend linker, in which case the
697
   lineno_count in the sections is correct.  */
698
224
      for (s = abfd->sections; s != NULL; s = s->next)
699
168
  total += s->lineno_count;
700
56
      return total;
701
56
    }
702
703
334
  for (s = abfd->sections; s != NULL; s = s->next)
704
308
    BFD_ASSERT (s->lineno_count == 0);
705
706
1.61k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
707
1.58k
    {
708
1.58k
      asymbol *q_maybe = *p;
709
710
1.58k
      if (bfd_asymbol_bfd (q_maybe) != NULL
711
1.58k
    && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
712
1.58k
  {
713
1.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
1.58k
    if (q->lineno != NULL
719
0
        && 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
1.58k
  }
739
1.58k
    }
740
741
26
  return total;
742
82
}
743
744
static void
745
fixup_symbol_value (bfd *abfd,
746
        coff_symbol_type *coff_symbol_ptr,
747
        struct internal_syment *syment)
748
1.58k
{
749
  /* Normalize the symbol flags.  */
750
1.58k
  if (coff_symbol_ptr->symbol.section
751
1.58k
      && bfd_is_com_section (coff_symbol_ptr->symbol.section))
752
2
    {
753
      /* A common symbol is undefined with a value.  */
754
2
      syment->n_scnum = N_UNDEF;
755
2
      syment->n_value = coff_symbol_ptr->symbol.value;
756
2
    }
757
1.58k
  else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
758
53
     && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
759
53
    {
760
53
      syment->n_value = coff_symbol_ptr->symbol.value;
761
53
    }
762
1.53k
  else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
763
1.52k
    {
764
1.52k
      syment->n_scnum = N_UNDEF;
765
1.52k
      syment->n_value = 0;
766
1.52k
    }
767
  /* FIXME: Do we need to handle the absolute section here?  */
768
4
  else
769
4
    {
770
4
      if (coff_symbol_ptr->symbol.section)
771
4
  {
772
4
    syment->n_scnum =
773
4
      coff_symbol_ptr->symbol.section->output_section->target_index;
774
775
4
    syment->n_value = (coff_symbol_ptr->symbol.value
776
4
           + coff_symbol_ptr->symbol.section->output_offset);
777
4
    if (! obj_pe (abfd))
778
3
      {
779
3
        syment->n_value += (syment->n_sclass == C_STATLAB)
780
3
    ? coff_symbol_ptr->symbol.section->output_section->lma
781
3
    : coff_symbol_ptr->symbol.section->output_section->vma;
782
3
      }
783
4
  }
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
4
    }
792
1.58k
}
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
26
{
804
26
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
805
26
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
806
26
  unsigned int native_index = 0;
807
26
  struct internal_syment *last_file = NULL;
808
26
  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
26
  {
821
26
    asymbol **newsyms;
822
26
    unsigned int i;
823
26
    bfd_size_type amt;
824
825
26
    amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
826
26
    newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
827
26
    if (!newsyms)
828
0
      return false;
829
26
    bfd_ptr->outsymbols = newsyms;
830
1.61k
    for (i = 0; i < symbol_count; i++)
831
1.58k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
832
1.58k
    || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
833
6
        && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
834
4
        && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
835
4
      || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
836
4
          == 0))))
837
8
  *newsyms++ = symbol_ptr_ptr[i];
838
839
1.61k
    for (i = 0; i < symbol_count; i++)
840
1.58k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
841
1.58k
    && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
842
6
    && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
843
4
        || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
844
4
      && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
845
4
          != 0))))
846
2
  *newsyms++ = symbol_ptr_ptr[i];
847
848
26
    *first_undef = newsyms - bfd_ptr->outsymbols;
849
850
1.61k
    for (i = 0; i < symbol_count; i++)
851
1.58k
      if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
852
1.58k
    && bfd_is_und_section (symbol_ptr_ptr[i]->section))
853
1.57k
  *newsyms++ = symbol_ptr_ptr[i];
854
26
    *newsyms = (asymbol *) NULL;
855
26
    symbol_ptr_ptr = bfd_ptr->outsymbols;
856
26
  }
857
858
1.61k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
859
1.58k
    {
860
1.58k
      coff_symbol_type *coff_symbol_ptr;
861
862
1.58k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
863
1.58k
      symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
864
1.58k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
865
1.58k
  {
866
1.58k
    combined_entry_type *s = coff_symbol_ptr->native;
867
1.58k
    int i;
868
869
1.58k
    BFD_ASSERT (s->is_sym);
870
1.58k
    if (s->u.syment.n_sclass == C_FILE)
871
3
      {
872
3
        if (last_file != NULL)
873
0
    last_file->n_value = native_index;
874
3
        last_file = &(s->u.syment);
875
3
      }
876
1.58k
    else
877
      /* Modify the symbol values according to their section and
878
         type.  */
879
1.58k
      fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
880
881
6.57k
    for (i = 0; i < s->u.syment.n_numaux + 1; i++)
882
4.99k
      s[i].offset = native_index++;
883
1.58k
  }
884
0
      else
885
0
  native_index++;
886
1.58k
    }
887
888
26
  obj_conv_table_size (bfd_ptr) = native_index;
889
890
26
  return true;
891
26
}
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
26
{
900
26
  unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
901
26
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
902
26
  unsigned int symbol_index;
903
904
1.61k
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
905
1.58k
    {
906
1.58k
      coff_symbol_type *coff_symbol_ptr;
907
908
1.58k
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
909
1.58k
      if (coff_symbol_ptr && coff_symbol_ptr->native)
910
1.58k
  {
911
1.58k
    int i;
912
1.58k
    combined_entry_type *s = coff_symbol_ptr->native;
913
914
1.58k
    BFD_ASSERT (s->is_sym);
915
1.58k
    if (s->fix_value)
916
0
      {
917
        /* FIXME: We should use a union here.  */
918
0
        s->u.syment.n_value =
919
0
    (uintptr_t) ((combined_entry_type *)
920
0
           (uintptr_t) s->u.syment.n_value)->offset;
921
0
        s->fix_value = 0;
922
0
      }
923
1.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
4.99k
    for (i = 0; i < s->u.syment.n_numaux; i++)
936
3.40k
      {
937
3.40k
        combined_entry_type *a = s + i + 1;
938
939
3.40k
        BFD_ASSERT (! a->is_sym);
940
3.40k
        if (a->fix_tag)
941
1.52k
    {
942
1.52k
      a->u.auxent.x_sym.x_tagndx.u32 =
943
1.52k
        a->u.auxent.x_sym.x_tagndx.p->offset;
944
1.52k
      a->fix_tag = 0;
945
1.52k
    }
946
3.40k
        if (a->fix_end)
947
18
    {
948
18
      a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
949
18
        a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
950
18
      a->fix_end = 0;
951
18
    }
952
3.40k
        if (a->fix_scnlen)
953
0
    {
954
0
      a->u.auxent.x_csect.x_scnlen.u64 =
955
0
        a->u.auxent.x_csect.x_scnlen.p->offset;
956
0
      a->fix_scnlen = 0;
957
0
    }
958
3.40k
      }
959
1.58k
  }
960
1.58k
    }
961
26
}
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
3
{
970
3
  unsigned int str_length = strlen (str);
971
3
  unsigned int filnmlen = bfd_coff_filnmlen (abfd);
972
973
3
  if (bfd_coff_long_filenames (abfd))
974
3
    {
975
3
      if (str_length <= filnmlen)
976
3
  strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
977
0
      else
978
0
  {
979
0
    bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
980
981
0
    if (indx == (bfd_size_type) -1)
982
0
      return false;
983
984
0
    auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
985
0
    auxent->x_file.x_n.x_n.x_zeroes = 0;
986
0
  }
987
3
    }
988
0
  else
989
0
    {
990
0
      strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
991
0
      if (str_length > filnmlen)
992
0
  str[filnmlen] = '\0';
993
0
    }
994
995
3
  return true;
996
3
}
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
1.58k
{
1007
1.58k
  unsigned int name_length;
1008
1.58k
  char *name = (char *) (symbol->name);
1009
1.58k
  bfd_size_type indx;
1010
1011
1.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
1.58k
  name_length = strlen (name);
1018
1019
1.58k
  BFD_ASSERT (native->is_sym);
1020
1.58k
  if (native->u.syment.n_sclass == C_FILE
1021
3
      && native->u.syment.n_numaux > 0)
1022
3
    {
1023
3
      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
3
      else
1033
3
  strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1034
1035
3
      BFD_ASSERT (! (native + 1)->is_sym);
1036
3
      if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1037
3
             strtab, hash))
1038
0
  return false;
1039
3
    }
1040
1.58k
  else
1041
1.58k
    {
1042
1.58k
      if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1043
  /* This name will fit into the symbol neatly.  */
1044
1.58k
  strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1045
1046
0
      else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1047
0
  {
1048
0
    indx = _bfd_stringtab_add (strtab, name, hash, false);
1049
0
    if (indx == (bfd_size_type) -1)
1050
0
      return false;
1051
1052
0
    native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1053
0
    native->u.syment._n._n_n._n_zeroes = 0;
1054
0
  }
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
1.58k
    }
1094
1095
1.58k
  return true;
1096
1.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
1.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
1.58k
{
1116
1.58k
  unsigned int numaux = native->u.syment.n_numaux;
1117
1.58k
  int type = native->u.syment.n_type;
1118
1.58k
  int n_sclass = (int) native->u.syment.n_sclass;
1119
1.58k
  asection *output_section = symbol->section->output_section
1120
1.58k
             ? symbol->section->output_section
1121
1.58k
             : symbol->section;
1122
1.58k
  void * buf;
1123
1.58k
  bfd_size_type symesz;
1124
1125
1.58k
  BFD_ASSERT (native->is_sym);
1126
1127
1.58k
  if (native->u.syment.n_sclass == C_FILE)
1128
3
    symbol->flags |= BSF_DEBUGGING;
1129
1130
1.58k
  if (symbol->flags & BSF_DEBUGGING
1131
56
      && bfd_is_abs_section (symbol->section))
1132
0
    native->u.syment.n_scnum = N_DEBUG;
1133
1134
1.58k
  else if (bfd_is_abs_section (symbol->section))
1135
3
    native->u.syment.n_scnum = N_ABS;
1136
1137
1.58k
  else if (bfd_is_und_section (symbol->section))
1138
1.58k
    native->u.syment.n_scnum = N_UNDEF;
1139
1140
3
  else
1141
3
    native->u.syment.n_scnum =
1142
3
      output_section->target_index;
1143
1144
1.58k
  if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1145
1.58k
           debug_string_section_p, debug_string_size_p))
1146
0
    return false;
1147
1148
1.58k
  symesz = bfd_coff_symesz (abfd);
1149
1.58k
  buf = bfd_alloc (abfd, symesz);
1150
1.58k
  if (!buf)
1151
0
    return false;
1152
1.58k
  bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1153
1.58k
  if (bfd_write (buf, symesz, abfd) != symesz)
1154
0
    return false;
1155
1.58k
  bfd_release (abfd, buf);
1156
1157
1.58k
  if (native->u.syment.n_numaux > 0)
1158
152
    {
1159
152
      bfd_size_type auxesz;
1160
152
      unsigned int j;
1161
1162
152
      auxesz = bfd_coff_auxesz (abfd);
1163
152
      buf = bfd_alloc (abfd, auxesz);
1164
152
      if (!buf)
1165
0
  return false;
1166
3.55k
      for (j = 0; j < native->u.syment.n_numaux; j++)
1167
3.40k
  {
1168
3.40k
    BFD_ASSERT (! (native + j + 1)->is_sym);
1169
1170
    /* Adjust auxent only if this isn't the filename
1171
       auxiliary entry.  */
1172
3.40k
    if (native->u.syment.n_sclass == C_FILE
1173
262
        && (native + j + 1)->u.auxent.x_file.x_ftype
1174
0
        && (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
3.40k
    bfd_coff_swap_aux_out (abfd,
1179
3.40k
         &((native + j + 1)->u.auxent),
1180
3.40k
         type, n_sclass, (int) j,
1181
3.40k
         native->u.syment.n_numaux,
1182
3.40k
         buf);
1183
3.40k
    if (bfd_write (buf, auxesz, abfd) != auxesz)
1184
0
      return false;
1185
3.40k
  }
1186
152
      bfd_release (abfd, buf);
1187
152
    }
1188
1189
  /* Store the index for use when we write out the relocs.  */
1190
1.58k
  set_index (symbol, *written);
1191
1192
1.58k
  *written += numaux + 1;
1193
1.58k
  return true;
1194
1.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
1.58k
{
1327
1.58k
  combined_entry_type *native = symbol->native;
1328
1.58k
  alent *lineno = symbol->lineno;
1329
1.58k
  struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1330
1331
1.58k
  if ((!link_info || link_info->strip_discarded)
1332
1.58k
      && !bfd_is_abs_section (symbol->symbol.section)
1333
1.58k
      && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1334
0
    {
1335
0
      symbol->symbol.name = "";
1336
0
      return true;
1337
0
    }
1338
1339
1.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
1.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
1.58k
  return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1373
1.58k
          strtab, true, debug_string_section_p,
1374
1.58k
          debug_string_size_p);
1375
1.58k
}
1376
1377
static void
1378
null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1379
        va_list ap ATTRIBUTE_UNUSED)
1380
1.49k
{
1381
1.49k
}
1382
1383
/* Write out the COFF symbols.  */
1384
1385
bool
1386
coff_write_symbols (bfd *abfd)
1387
26
{
1388
26
  struct bfd_strtab_hash *strtab;
1389
26
  asection *debug_string_section;
1390
26
  bfd_size_type debug_string_size;
1391
26
  unsigned int i;
1392
26
  unsigned int limit = bfd_get_symcount (abfd);
1393
26
  bfd_vma written = 0;
1394
26
  asymbol **p;
1395
1396
26
  debug_string_section = NULL;
1397
26
  debug_string_size = 0;
1398
1399
26
  strtab = _bfd_stringtab_init ();
1400
26
  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
26
  if (bfd_coff_long_section_names (abfd))
1408
8
    {
1409
8
      asection *o;
1410
1411
143
      for (o = abfd->sections; o != NULL; o = o->next)
1412
135
  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
8
    }
1417
1418
  /* Seek to the right place.  */
1419
26
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1420
0
    return false;
1421
1422
  /* Output all the symbols we have.  */
1423
26
  written = 0;
1424
1.61k
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1425
1.58k
    {
1426
1.58k
      asymbol *symbol = *p;
1427
1.58k
      coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1428
1429
1.58k
      if (c_symbol == (coff_symbol_type *) NULL
1430
1.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
1.58k
      else
1438
1.58k
  {
1439
1.58k
    if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1440
1.58k
      {
1441
1.58k
        bfd_error_handler_type current_error_handler;
1442
1.58k
        enum coff_symbol_classification sym_class;
1443
1.58k
        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
1.58k
        current_error_handler = bfd_set_error_handler (null_error_handler);
1450
1.58k
        BFD_ASSERT (c_symbol->native->is_sym);
1451
1.58k
        sym_class = bfd_coff_classify_symbol (abfd,
1452
1.58k
                &c_symbol->native->u.syment);
1453
1.58k
        (void) bfd_set_error_handler (current_error_handler);
1454
1455
1.58k
        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
1.58k
        if (symbol->flags & BSF_WEAK)
1465
8
    *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1466
1.58k
        else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1467
0
    *n_sclass = C_STAT;
1468
1.58k
        else if (symbol->flags & BSF_GLOBAL
1469
29
           && (sym_class != COFF_SYMBOL_GLOBAL
1470
#ifdef COFF_WITH_PE
1471
         || *n_sclass == C_NT_WEAK
1472
#endif
1473
0
         || *n_sclass == C_WEAKEXT))
1474
29
    c_symbol->native->u.syment.n_sclass = C_EXT;
1475
1.58k
      }
1476
1477
1.58k
    if (!coff_write_native_symbol (abfd, c_symbol, &written,
1478
1.58k
           strtab, &debug_string_section,
1479
1.58k
           &debug_string_size))
1480
0
      return false;
1481
1.58k
  }
1482
1.58k
    }
1483
1484
26
  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
26
  {
1492
26
    bfd_byte buffer[STRING_SIZE_SIZE];
1493
1494
26
#if STRING_SIZE_SIZE == 4
1495
26
    H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1496
#else
1497
 #error Change H_PUT_32
1498
#endif
1499
26
    if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1500
0
      return false;
1501
1502
26
    if (! _bfd_stringtab_emit (abfd, strtab))
1503
0
      return false;
1504
26
  }
1505
1506
26
  _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
26
  BFD_ASSERT (debug_string_size == 0
1514
26
        || (debug_string_section != (asection *) NULL
1515
26
      && (BFD_ALIGN (debug_string_size,
1516
26
         1 << debug_string_section->alignment_power)
1517
26
          == debug_string_section->size)));
1518
1519
26
  return true;
1520
26
}
1521
1522
bool
1523
coff_write_linenumbers (bfd *abfd)
1524
26
{
1525
26
  asection *s;
1526
26
  bfd_size_type linesz;
1527
26
  void * buff;
1528
1529
26
  linesz = bfd_coff_linesz (abfd);
1530
26
  buff = bfd_alloc (abfd, linesz);
1531
26
  if (!buff)
1532
0
    return false;
1533
334
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1534
308
    {
1535
308
      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
308
    }
1576
26
  bfd_release (abfd, buff);
1577
26
  return true;
1578
26
}
1579
1580
alent *
1581
coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1582
6
{
1583
6
  return coffsymbol (symbol)->lineno;
1584
6
}
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.39M
{
1596
1.39M
  unsigned int type = symbol->u.syment.n_type;
1597
1.39M
  unsigned int n_sclass = symbol->u.syment.n_sclass;
1598
1599
1.39M
  BFD_ASSERT (symbol->is_sym);
1600
1.39M
  if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1601
98.0k
    {
1602
98.0k
      if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1603
98.0k
    (abfd, table_base, symbol, indaux, auxent))
1604
1.26k
  return;
1605
98.0k
    }
1606
1607
  /* Don't bother if this is a file or a section.  */
1608
1.39M
  if (n_sclass == C_STAT && type == T_NULL)
1609
14.1k
    return;
1610
1.38M
  if (n_sclass == C_FILE)
1611
73.8k
    return;
1612
1.30M
  if (n_sclass == C_DWARF)
1613
13.1k
    return;
1614
1615
1.29M
  BFD_ASSERT (! auxent->is_sym);
1616
  /* Otherwise patch up.  */
1617
1.29M
#define N_TMASK coff_data  (abfd)->local_n_tmask
1618
1.29M
#define N_BTSHFT coff_data (abfd)->local_n_btshft
1619
1620
1.29M
  if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1621
948k
       || n_sclass == C_FCN)
1622
360k
      && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1623
262k
      && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1624
262k
    < obj_raw_syment_count (abfd)))
1625
21.8k
    {
1626
21.8k
      auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1627
21.8k
  table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1628
21.8k
      auxent->fix_end = 1;
1629
21.8k
    }
1630
1631
  /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1632
     generate one, so we must be careful to ignore it.  */
1633
1.29M
  if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1634
451k
    {
1635
451k
      auxent->u.auxent.x_sym.x_tagndx.p =
1636
451k
  table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1637
451k
      auxent->fix_tag = 1;
1638
451k
    }
1639
1.29M
}
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
33
{
1648
33
  char *debug_section;
1649
33
  file_ptr position;
1650
33
  bfd_size_type sec_size;
1651
1652
33
  asection *sect = bfd_get_section_by_name (abfd, ".debug");
1653
1654
33
  if (!sect)
1655
20
    {
1656
20
      bfd_set_error (bfd_error_no_debug_section);
1657
20
      return NULL;
1658
20
    }
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
13
  position = bfd_tell (abfd);
1665
13
  if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1666
2
    return NULL;
1667
1668
11
  sec_size = sect->size;
1669
11
  debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
1670
11
  if (debug_section == NULL)
1671
8
    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
14.6k
{
1688
14.6k
  size_t len;
1689
14.6k
  char *newname;
1690
1691
189k
  for (len = 0; len < maxlen; ++len)
1692
180k
    if (name[len] == '\0')
1693
6.15k
      break;
1694
1695
14.6k
  if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1696
0
    return NULL;
1697
1698
14.6k
  strncpy (newname, name, len);
1699
14.6k
  newname[len] = '\0';
1700
14.6k
  return newname;
1701
14.6k
}
1702
1703
/* Read in the external symbols.  */
1704
1705
bool
1706
_bfd_coff_get_external_symbols (bfd *abfd)
1707
112k
{
1708
112k
  size_t symesz;
1709
112k
  size_t size;
1710
112k
  void * syms;
1711
112k
  ufile_ptr filesize;
1712
1713
112k
  if (obj_coff_external_syms (abfd) != NULL)
1714
12.2k
    return true;
1715
1716
100k
  symesz = bfd_coff_symesz (abfd);
1717
100k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1718
8.10k
    {
1719
8.10k
      bfd_set_error (bfd_error_file_truncated);
1720
8.10k
      return false;
1721
8.10k
    }
1722
1723
91.9k
  if (size == 0)
1724
23.0k
    return true;
1725
1726
68.8k
  filesize = bfd_get_file_size (abfd);
1727
68.8k
  if (filesize != 0
1728
68.8k
      && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1729
43.9k
    || size > filesize - obj_sym_filepos (abfd)))
1730
39.7k
    {
1731
39.7k
      bfd_set_error (bfd_error_file_truncated);
1732
39.7k
      return false;
1733
39.7k
    }
1734
1735
29.1k
  if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1736
0
    return false;
1737
29.1k
  syms = _bfd_malloc_and_read (abfd, size, size);
1738
29.1k
  obj_coff_external_syms (abfd) = syms;
1739
29.1k
  return syms != NULL;
1740
29.1k
}
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
106k
{
1751
106k
  char extstrsize[STRING_SIZE_SIZE];
1752
106k
  bfd_size_type strsize;
1753
106k
  char *strings;
1754
106k
  ufile_ptr pos;
1755
106k
  ufile_ptr filesize;
1756
106k
  size_t symesz;
1757
106k
  size_t size;
1758
1759
106k
  if (obj_coff_strings (abfd) != NULL)
1760
3.88k
    return obj_coff_strings (abfd);
1761
1762
102k
  if (obj_sym_filepos (abfd) == 0)
1763
555
    {
1764
555
      bfd_set_error (bfd_error_no_symbols);
1765
555
      return NULL;
1766
555
    }
1767
1768
102k
  symesz = bfd_coff_symesz (abfd);
1769
102k
  pos = obj_sym_filepos (abfd);
1770
102k
  if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1771
102k
      || pos + size < pos)
1772
27
    {
1773
27
      bfd_set_error (bfd_error_file_truncated);
1774
27
      return NULL;
1775
27
    }
1776
1777
102k
  if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1778
0
    return NULL;
1779
1780
102k
  if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1781
4.45k
    {
1782
4.45k
      if (bfd_get_error () != bfd_error_file_truncated)
1783
540
  return NULL;
1784
1785
      /* There is no string table.  */
1786
3.91k
      strsize = STRING_SIZE_SIZE;
1787
3.91k
    }
1788
97.9k
  else
1789
97.9k
    {
1790
97.9k
#if STRING_SIZE_SIZE == 4
1791
97.9k
      strsize = H_GET_32 (abfd, extstrsize);
1792
#else
1793
 #error Change H_GET_32
1794
#endif
1795
97.9k
    }
1796
1797
101k
  filesize = bfd_get_file_size (abfd);
1798
101k
  if (strsize < STRING_SIZE_SIZE
1799
77.9k
      || (filesize != 0 && strsize > filesize))
1800
82.0k
    {
1801
82.0k
      _bfd_error_handler
1802
  /* xgettext: c-format */
1803
82.0k
  (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1804
82.0k
      bfd_set_error (bfd_error_bad_value);
1805
82.0k
      return NULL;
1806
82.0k
    }
1807
1808
19.8k
  strings = (char *) bfd_malloc (strsize + 1);
1809
19.8k
  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
19.8k
  memset (strings, 0, STRING_SIZE_SIZE);
1817
1818
19.8k
  if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1819
19.8k
      != strsize - STRING_SIZE_SIZE)
1820
5.81k
    {
1821
5.81k
      free (strings);
1822
5.81k
      return NULL;
1823
5.81k
    }
1824
1825
14.0k
  obj_coff_strings (abfd) = strings;
1826
14.0k
  obj_coff_strings_len (abfd) = strsize;
1827
  /* Terminate the string table, just in case.  */
1828
14.0k
  strings[strsize] = 0;
1829
14.0k
  return strings;
1830
19.8k
}
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
159k
{
1837
159k
  if (! bfd_family_coff (abfd))
1838
3.88k
    return false;
1839
1840
155k
  if (obj_coff_external_syms (abfd) != NULL
1841
21.5k
      && ! obj_coff_keep_syms (abfd))
1842
21.1k
    {
1843
21.1k
      free (obj_coff_external_syms (abfd));
1844
21.1k
      obj_coff_external_syms (abfd) = NULL;
1845
21.1k
    }
1846
1847
155k
  if (obj_coff_strings (abfd) != NULL
1848
14.3k
      && ! obj_coff_keep_strings (abfd))
1849
14.0k
    {
1850
14.0k
      free (obj_coff_strings (abfd));
1851
14.0k
      obj_coff_strings (abfd) = NULL;
1852
14.0k
      obj_coff_strings_len (abfd) = 0;
1853
14.0k
    }
1854
1855
155k
  return true;
1856
159k
}
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
18.5k
{
1866
18.5k
  combined_entry_type *internal;
1867
18.5k
  combined_entry_type *internal_ptr;
1868
18.5k
  size_t symesz;
1869
18.5k
  char *raw_src;
1870
18.5k
  char *raw_end;
1871
18.5k
  const char *string_table = NULL;
1872
18.5k
  asection * debug_sec = NULL;
1873
18.5k
  char *debug_sec_data = NULL;
1874
18.5k
  bfd_size_type size;
1875
1876
18.5k
  if (obj_raw_syments (abfd) != NULL)
1877
0
    return obj_raw_syments (abfd);
1878
1879
18.5k
  if (! _bfd_coff_get_external_symbols (abfd))
1880
6.54k
    return NULL;
1881
1882
12.0k
  size = obj_raw_syment_count (abfd);
1883
  /* Check for integer overflow.  */
1884
12.0k
  if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1885
0
    return NULL;
1886
12.0k
  size *= sizeof (combined_entry_type);
1887
12.0k
  internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1888
12.0k
  if (internal == NULL && size != 0)
1889
0
    return NULL;
1890
1891
12.0k
  raw_src = (char *) obj_coff_external_syms (abfd);
1892
1893
  /* Mark the end of the symbols.  */
1894
12.0k
  symesz = bfd_coff_symesz (abfd);
1895
12.0k
  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
12.0k
  for (internal_ptr = internal;
1902
366k
       raw_src < raw_end;
1903
354k
       raw_src += symesz, internal_ptr++)
1904
359k
    {
1905
359k
      unsigned int i;
1906
1907
359k
      bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1908
359k
          (void *) & internal_ptr->u.syment);
1909
359k
      internal_ptr->is_sym = true;
1910
359k
      combined_entry_type *sym = internal_ptr;
1911
1912
      /* PR 17512: Prevent buffer overrun.  */
1913
359k
      if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1914
2.02k
  return NULL;
1915
1916
1.75M
      for (i = 0; i < sym->u.syment.n_numaux; i++)
1917
1.39M
  {
1918
1.39M
    internal_ptr++;
1919
1.39M
    raw_src += symesz;
1920
1921
1.39M
    bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1922
1.39M
        sym->u.syment.n_type,
1923
1.39M
        sym->u.syment.n_sclass,
1924
1.39M
        (int) i, sym->u.syment.n_numaux,
1925
1.39M
        &(internal_ptr->u.auxent));
1926
1927
1.39M
    internal_ptr->is_sym = false;
1928
1.39M
    coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1929
1.39M
  }
1930
1931
357k
      if (sym->u.syment.n_sclass == C_FILE
1932
3.30k
    && sym->u.syment.n_numaux > 0)
1933
1.09k
  {
1934
1.09k
    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.09k
    BFD_ASSERT (! aux->is_sym);
1939
1940
1.09k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1941
176
      {
1942
        /* The filename is a long one, point into the string table.  */
1943
176
        if (string_table == NULL)
1944
96
    {
1945
96
      string_table = _bfd_coff_read_string_table (abfd);
1946
96
      if (string_table == NULL)
1947
58
        return NULL;
1948
96
    }
1949
1950
118
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1951
118
      >= obj_coff_strings_len (abfd))
1952
63
    sym->u.syment._n._n_n._n_offset =
1953
63
      (uintptr_t) bfd_symbol_error_name;
1954
55
        else
1955
55
    sym->u.syment._n._n_n._n_offset =
1956
55
      (uintptr_t) (string_table
1957
55
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
1958
118
      }
1959
917
    else
1960
917
      {
1961
        /* Ordinary short filename, put into memory anyway.  The
1962
     Microsoft PE tools sometimes store a filename in
1963
     multiple AUX entries.  */
1964
917
        size_t len;
1965
917
        char *src;
1966
917
        if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1967
415
    {
1968
415
      len = sym->u.syment.n_numaux * symesz;
1969
415
      src = raw_src - (len - symesz);
1970
415
    }
1971
502
        else
1972
502
    {
1973
502
      len = bfd_coff_filnmlen (abfd);
1974
502
      src = aux->u.auxent.x_file.x_n.x_fname;
1975
502
    }
1976
917
        sym->u.syment._n._n_n._n_offset =
1977
917
    (uintptr_t) copy_name (abfd, src, len);
1978
917
      }
1979
1980
    /* Normalize other strings available in C_FILE aux entries.  */
1981
1.03k
    if (!obj_pe (abfd))
1982
560
      for (int numaux = 1;
1983
25.5k
     numaux < sym->u.syment.n_numaux;
1984
24.9k
     numaux++)
1985
25.1k
        {
1986
25.1k
    aux = sym + numaux + 1;
1987
25.1k
    BFD_ASSERT (! aux->is_sym);
1988
1989
25.1k
    if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1990
11.4k
      {
1991
        /* The string information is a long one, point
1992
           into the string table.  */
1993
11.4k
        if (string_table == NULL)
1994
244
          {
1995
244
      string_table = _bfd_coff_read_string_table (abfd);
1996
244
      if (string_table == NULL)
1997
154
        return NULL;
1998
244
          }
1999
2000
11.2k
        if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
2001
11.2k
      >= obj_coff_strings_len (abfd))
2002
5.16k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
2003
5.16k
      (uintptr_t) bfd_symbol_error_name;
2004
6.08k
        else
2005
6.08k
          aux->u.auxent.x_file.x_n.x_n.x_offset =
2006
6.08k
      (uintptr_t) (string_table
2007
6.08k
             + aux->u.auxent.x_file.x_n.x_n.x_offset);
2008
11.2k
      }
2009
13.7k
    else
2010
13.7k
      aux->u.auxent.x_file.x_n.x_n.x_offset =
2011
13.7k
        ((uintptr_t)
2012
13.7k
         copy_name (abfd,
2013
13.7k
        aux->u.auxent.x_file.x_n.x_fname,
2014
13.7k
        bfd_coff_filnmlen (abfd)));
2015
25.1k
        }
2016
2017
1.03k
  }
2018
355k
      else
2019
355k
  {
2020
355k
    if (sym->u.syment._n._n_n._n_zeroes != 0)
2021
122k
      {
2022
        /* This is a "short" name.  Make it long.  */
2023
122k
        char *newstring;
2024
2025
        /* Find the length of this string without walking into memory
2026
     that isn't ours.  */
2027
475k
        for (i = 0; i < SYMNMLEN; ++i)
2028
447k
    if (sym->u.syment._n._n_name[i] == '\0')
2029
94.8k
      break;
2030
2031
122k
        newstring = bfd_alloc (abfd, i + 1);
2032
122k
        if (newstring == NULL)
2033
0
    return NULL;
2034
122k
        memcpy (newstring, sym->u.syment._n._n_name, i);
2035
122k
        newstring[i] = 0;
2036
122k
        sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2037
122k
        sym->u.syment._n._n_n._n_zeroes = 0;
2038
122k
      }
2039
233k
    else if (sym->u.syment._n._n_n._n_offset == 0)
2040
118k
      sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2041
114k
    else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2042
114k
      {
2043
        /* Long name already.  Point symbol at the string in the
2044
     table.  */
2045
114k
        if (string_table == NULL)
2046
6.76k
    {
2047
6.76k
      string_table = _bfd_coff_read_string_table (abfd);
2048
6.76k
      if (string_table == NULL)
2049
2.60k
        return NULL;
2050
6.76k
    }
2051
112k
        if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2052
99.9k
    sym->u.syment._n._n_n._n_offset =
2053
99.9k
      (uintptr_t) bfd_symbol_error_name;
2054
12.2k
        else
2055
12.2k
    sym->u.syment._n._n_n._n_offset =
2056
12.2k
      (uintptr_t) (string_table
2057
12.2k
             + sym->u.syment._n._n_n._n_offset);
2058
112k
      }
2059
34
    else
2060
34
      {
2061
        /* Long name in debug section.  Very similar.  */
2062
34
        if (debug_sec_data == NULL)
2063
33
    {
2064
33
      debug_sec_data = build_debug_section (abfd, &debug_sec);
2065
33
      if (debug_sec_data == NULL)
2066
30
        return NULL;
2067
33
    }
2068
        /* PR binutils/17512: Catch out of range offsets into
2069
     the debug data.  */
2070
4
        if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2071
4
    sym->u.syment._n._n_n._n_offset =
2072
4
      (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
4
      }
2078
355k
  }
2079
357k
    }
2080
2081
  /* Free the raw symbols.  */
2082
7.17k
  if (obj_coff_external_syms (abfd) != NULL
2083
6.93k
      && ! obj_coff_keep_syms (abfd))
2084
6.93k
    {
2085
6.93k
      free (obj_coff_external_syms (abfd));
2086
6.93k
      obj_coff_external_syms (abfd) = NULL;
2087
6.93k
    }
2088
2089
7.17k
  obj_raw_syments (abfd) = internal;
2090
7.17k
  BFD_ASSERT (obj_raw_syment_count (abfd)
2091
7.17k
        == (size_t) (internal_ptr - internal));
2092
2093
7.17k
  return internal;
2094
12.0k
}
2095
2096
long
2097
coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2098
54.0k
{
2099
54.0k
  size_t count, raw;
2100
2101
54.0k
  count = asect->reloc_count;
2102
54.0k
  if (count >= LONG_MAX / sizeof (arelent *)
2103
54.0k
      || _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
54.0k
  if (!bfd_write_p (abfd))
2109
54.0k
    {
2110
54.0k
      ufile_ptr filesize = bfd_get_file_size (abfd);
2111
54.0k
      if (filesize != 0 && raw > filesize)
2112
31.4k
  {
2113
31.4k
    bfd_set_error (bfd_error_file_truncated);
2114
31.4k
    return -1;
2115
31.4k
  }
2116
54.0k
    }
2117
22.6k
  return (count + 1) * sizeof (arelent *);
2118
54.0k
}
2119
2120
asymbol *
2121
coff_make_empty_symbol (bfd *abfd)
2122
1.90M
{
2123
1.90M
  size_t amt = sizeof (coff_symbol_type);
2124
1.90M
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2125
2126
1.90M
  if (new_symbol == NULL)
2127
0
    return NULL;
2128
1.90M
  new_symbol->symbol.section = 0;
2129
1.90M
  new_symbol->native = NULL;
2130
1.90M
  new_symbol->lineno = NULL;
2131
1.90M
  new_symbol->done_lineno = false;
2132
1.90M
  new_symbol->symbol.the_bfd = abfd;
2133
2134
1.90M
  return & new_symbol->symbol;
2135
1.90M
}
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.48k
{
2166
2.48k
  bfd_symbol_info (symbol, ret);
2167
2168
2.48k
  if (coffsymbol (symbol)->native != NULL
2169
2.48k
      && 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.48k
}
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
35.9k
{
2377
35.9k
  bool found;
2378
35.9k
  unsigned int i;
2379
35.9k
  unsigned int line_base;
2380
35.9k
  coff_data_type *cof = coff_data (abfd);
2381
  /* Run through the raw syments if available.  */
2382
35.9k
  combined_entry_type *p;
2383
35.9k
  combined_entry_type *pend;
2384
35.9k
  alent *l;
2385
35.9k
  struct coff_section_tdata *sec_data;
2386
35.9k
  size_t amt;
2387
2388
  /* Before looking through the symbol table, try to use a .stab
2389
     section to find the information.  */
2390
35.9k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2391
35.9k
               &found, filename_ptr,
2392
35.9k
               functionname_ptr, line_ptr,
2393
35.9k
               &coff_data(abfd)->line_info))
2394
219
    return false;
2395
2396
35.6k
  if (found)
2397
602
    return true;
2398
2399
  /* Also try examining DWARF2 debugging information.  */
2400
35.0k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2401
35.0k
             filename_ptr, functionname_ptr,
2402
35.0k
             line_ptr, NULL, debug_sections,
2403
35.0k
             &coff_data(abfd)->dwarf2_find_line_info))
2404
0
    return true;
2405
2406
35.0k
  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
35.0k
  if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2413
35.0k
    {
2414
35.0k
      bfd_signed_vma bias = 0;
2415
2416
      /* Create a cache of the result for the next call.  */
2417
35.0k
      if (sec_data == NULL && section->owner == abfd)
2418
15.8k
  {
2419
15.8k
    amt = sizeof (struct coff_section_tdata);
2420
15.8k
    section->used_by_bfd = bfd_zalloc (abfd, amt);
2421
15.8k
    sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2422
15.8k
  }
2423
2424
35.0k
      if (sec_data != NULL && sec_data->saved_bias)
2425
501
  bias = sec_data->bias;
2426
34.5k
      else if (symbols)
2427
259
  {
2428
259
    bias = _bfd_dwarf2_find_symbol_bias (symbols,
2429
259
                 & coff_data (abfd)->dwarf2_find_line_info);
2430
2431
259
    if (sec_data)
2432
259
      {
2433
259
        sec_data->saved_bias = true;
2434
259
        sec_data->bias = bias;
2435
259
      }
2436
259
  }
2437
2438
35.0k
      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
35.0k
    }
2446
2447
35.0k
  *filename_ptr = 0;
2448
35.0k
  *functionname_ptr = 0;
2449
35.0k
  *line_ptr = 0;
2450
2451
  /* Don't try and find line numbers in a non coff file.  */
2452
35.0k
  if (!bfd_family_coff (abfd))
2453
0
    return false;
2454
2455
35.0k
  if (cof == NULL)
2456
0
    return false;
2457
2458
  /* Find the first C_FILE symbol.  */
2459
35.0k
  p = cof->raw_syments;
2460
35.0k
  if (!p)
2461
30.2k
    return false;
2462
2463
4.82k
  pend = p + cof->raw_syment_count;
2464
100k
  while (p < pend)
2465
96.1k
    {
2466
96.1k
      BFD_ASSERT (p->is_sym);
2467
96.1k
      if (p->u.syment.n_sclass == C_FILE)
2468
575
  break;
2469
95.5k
      p += 1 + p->u.syment.n_numaux;
2470
95.5k
    }
2471
2472
4.82k
  if (p < pend)
2473
575
    {
2474
575
      bfd_vma sec_vma;
2475
575
      bfd_vma maxdiff;
2476
2477
      /* Look through the C_FILE symbols to find the best one.  */
2478
575
      sec_vma = bfd_section_vma (section);
2479
575
      *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2480
575
      maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2481
581
      while (1)
2482
581
  {
2483
581
    bfd_vma file_addr;
2484
581
    combined_entry_type *p2;
2485
2486
581
    for (p2 = p + 1 + p->u.syment.n_numaux;
2487
15.1k
         p2 < pend;
2488
14.5k
         p2 += 1 + p2->u.syment.n_numaux)
2489
14.7k
      {
2490
14.7k
        BFD_ASSERT (p2->is_sym);
2491
14.7k
        if (p2->u.syment.n_scnum > 0
2492
5.98k
      && (section
2493
5.98k
          == coff_section_from_bfd_index (abfd,
2494
5.98k
                  p2->u.syment.n_scnum)))
2495
64
    break;
2496
14.7k
        if (p2->u.syment.n_sclass == C_FILE)
2497
191
    {
2498
191
      p2 = pend;
2499
191
      break;
2500
191
    }
2501
14.7k
      }
2502
581
    if (p2 >= pend)
2503
517
      break;
2504
2505
64
    file_addr = (bfd_vma) p2->u.syment.n_value;
2506
    /* PR 11512: Include the section address of the function name symbol.  */
2507
64
    if (p2->u.syment.n_scnum > 0)
2508
64
      file_addr += coff_section_from_bfd_index (abfd,
2509
64
                  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
64
    if (p2 < pend
2513
64
        && offset + sec_vma >= file_addr
2514
61
        && offset + sec_vma - file_addr <= maxdiff)
2515
61
      {
2516
61
        *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2517
61
        maxdiff = offset + sec_vma - p2->u.syment.n_value;
2518
61
      }
2519
2520
64
    if (p->u.syment.n_value >= cof->raw_syment_count)
2521
48
      break;
2522
2523
    /* Avoid endless loops on erroneous files by ensuring that
2524
       we always move forward in the file.  */
2525
16
    if (p >= cof->raw_syments + p->u.syment.n_value)
2526
8
      break;
2527
2528
8
    p = cof->raw_syments + p->u.syment.n_value;
2529
8
    if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2530
2
      break;
2531
8
  }
2532
575
    }
2533
2534
4.82k
  if (section->lineno_count == 0)
2535
2.12k
    {
2536
2.12k
      *functionname_ptr = NULL;
2537
2.12k
      *line_ptr = 0;
2538
2.12k
      return true;
2539
2.12k
    }
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.69k
  if (sec_data != NULL
2545
2.69k
      && sec_data->i > 0
2546
1.39k
      && offset >= sec_data->offset)
2547
897
    {
2548
897
      i = sec_data->i;
2549
897
      *functionname_ptr = sec_data->function;
2550
897
      line_base = sec_data->line_base;
2551
897
    }
2552
1.80k
  else
2553
1.80k
    {
2554
1.80k
      i = 0;
2555
1.80k
      line_base = 0;
2556
1.80k
    }
2557
2558
2.69k
  if (section->lineno != NULL)
2559
754
    {
2560
754
      bfd_vma last_value = 0;
2561
2562
754
      l = &section->lineno[i];
2563
2564
3.31k
      for (; i < section->lineno_count; i++)
2565
3.03k
  {
2566
3.03k
    if (l->line_number == 0)
2567
2.30k
      {
2568
        /* Get the symbol this line number points at.  */
2569
2.30k
        coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2570
2.30k
        if (coff->symbol.value > offset)
2571
190
    break;
2572
2573
2.11k
        *functionname_ptr = coff->symbol.name;
2574
2.11k
        last_value = coff->symbol.value;
2575
2.11k
        if (coff->native)
2576
2.11k
    {
2577
2.11k
      combined_entry_type *s = coff->native;
2578
2579
2.11k
      BFD_ASSERT (s->is_sym);
2580
2.11k
      s = s + 1 + s->u.syment.n_numaux;
2581
2582
      /* In XCOFF a debugging symbol can follow the
2583
         function symbol.  */
2584
2.11k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2585
2.11k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2586
1.99k
          && s->u.syment.n_scnum == N_DEBUG)
2587
26
        s = s + 1 + s->u.syment.n_numaux;
2588
2589
      /* S should now point to the .bf of the function.  */
2590
2.11k
      if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2591
2.11k
           < obj_raw_syment_count (abfd) * sizeof (*s))
2592
1.97k
          && s->u.syment.n_numaux)
2593
728
        {
2594
          /* The linenumber is stored in the auxent.  */
2595
728
          union internal_auxent *a = &((s + 1)->u.auxent);
2596
2597
728
          line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2598
728
          *line_ptr = line_base;
2599
728
        }
2600
2.11k
    }
2601
2.11k
      }
2602
729
    else
2603
729
      {
2604
729
        if (l->u.offset > offset)
2605
285
    break;
2606
444
        *line_ptr = l->line_number + line_base - 1;
2607
444
      }
2608
2.55k
    l++;
2609
2.55k
  }
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
754
      if (i >= section->lineno_count
2618
279
    && last_value != 0
2619
65
    && offset - last_value > 0x100)
2620
63
  {
2621
63
    *functionname_ptr = NULL;
2622
63
    *line_ptr = 0;
2623
63
  }
2624
754
    }
2625
2626
  /* Cache the results for the next call.  */
2627
2.69k
  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.69k
  if (sec_data != NULL)
2635
2.69k
    {
2636
2.69k
      sec_data->offset = offset;
2637
2.69k
      sec_data->i = i - 1;
2638
2.69k
      sec_data->function = *functionname_ptr;
2639
2.69k
      sec_data->line_base = line_base;
2640
2.69k
    }
2641
2642
2.69k
  return true;
2643
4.82k
}
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
35.9k
{
2655
35.9k
  if (discriminator_ptr)
2656
35.3k
    *discriminator_ptr = 0;
2657
35.9k
  return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2658
35.9k
              filename_ptr, functionname_ptr,
2659
35.9k
              line_ptr, dwarf_debug_sections);
2660
35.9k
}
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 = NULL;
2957
0
        unsigned long symndx2 = h->aux->x_sym.x_tagndx.u32;
2958
0
        if (symndx2 < obj_raw_syment_count (h->auxbfd))
2959
0
    h2 = obj_coff_sym_hashes (h->auxbfd)[symndx2];
2960
2961
0
        if (h2 && h2->root.type != bfd_link_hash_undefined)
2962
0
    return  h2->root.u.def.section;
2963
0
      }
2964
0
    break;
2965
2966
0
  case bfd_link_hash_undefined:
2967
0
  default:
2968
0
    break;
2969
0
  }
2970
0
      return NULL;
2971
0
    }
2972
2973
0
  return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2974
0
}
2975
2976
/* COOKIE->rel describes a relocation against section SEC, which is
2977
   a section we've decided to keep.  Return the section that contains
2978
   the relocation symbol, or NULL if no section contains it.  */
2979
2980
static asection *
2981
_bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2982
      coff_gc_mark_hook_fn gc_mark_hook,
2983
      struct coff_reloc_cookie *cookie)
2984
0
{
2985
0
  struct coff_link_hash_entry *h;
2986
0
  unsigned long rsym = cookie->rel->r_symndx;
2987
2988
0
  if (rsym >= obj_raw_syment_count (cookie->abfd))
2989
0
    return NULL;
2990
0
  h = cookie->sym_hashes[rsym];
2991
0
  if (h != NULL)
2992
0
    {
2993
0
      while (h->root.type == bfd_link_hash_indirect
2994
0
       || h->root.type == bfd_link_hash_warning)
2995
0
  h = (struct coff_link_hash_entry *) h->root.u.i.link;
2996
2997
0
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2998
0
    }
2999
3000
0
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
3001
0
        &(cookie->symbols
3002
0
          + obj_convert (sec->owner)[rsym])->native->u.syment);
3003
0
}
3004
3005
static bool _bfd_coff_gc_mark
3006
  (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
3007
3008
/* COOKIE->rel describes a relocation against section SEC, which is
3009
   a section we've decided to keep.  Mark the section that contains
3010
   the relocation symbol.  */
3011
3012
static bool
3013
_bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
3014
       asection *sec,
3015
       coff_gc_mark_hook_fn gc_mark_hook,
3016
       struct coff_reloc_cookie *cookie)
3017
0
{
3018
0
  asection *rsec;
3019
3020
0
  rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
3021
0
  if (rsec && !rsec->gc_mark)
3022
0
    {
3023
0
      if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
3024
0
  rsec->gc_mark = 1;
3025
0
      else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
3026
0
  return false;
3027
0
    }
3028
0
  return true;
3029
0
}
3030
3031
/* The mark phase of garbage collection.  For a given section, mark
3032
   it and any sections in this section's group, and all the sections
3033
   which define symbols to which it refers.  */
3034
3035
static bool
3036
_bfd_coff_gc_mark (struct bfd_link_info *info,
3037
       asection *sec,
3038
       coff_gc_mark_hook_fn gc_mark_hook)
3039
0
{
3040
0
  bool ret = true;
3041
3042
0
  sec->gc_mark = 1;
3043
3044
  /* Look through the section relocs.  */
3045
0
  if ((sec->flags & SEC_RELOC) != 0
3046
0
      && sec->reloc_count > 0)
3047
0
    {
3048
0
      struct coff_reloc_cookie cookie;
3049
3050
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec))
3051
0
  ret = false;
3052
0
      else
3053
0
  {
3054
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
3055
0
      {
3056
0
        if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
3057
0
    {
3058
0
      ret = false;
3059
0
      break;
3060
0
    }
3061
0
      }
3062
0
    fini_reloc_cookie_for_section (&cookie, sec);
3063
0
  }
3064
0
    }
3065
3066
0
  return ret;
3067
0
}
3068
3069
static bool
3070
_bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
3071
          coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
3072
0
{
3073
0
  bfd *ibfd;
3074
3075
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3076
0
    {
3077
0
      asection *isec;
3078
0
      bool some_kept;
3079
3080
0
      if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
3081
0
  continue;
3082
3083
      /* Ensure all linker created sections are kept, and see whether
3084
   any other section is already marked.  */
3085
0
      some_kept = false;
3086
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3087
0
  {
3088
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
3089
0
      isec->gc_mark = 1;
3090
0
    else if (isec->gc_mark)
3091
0
      some_kept = true;
3092
0
  }
3093
3094
      /* If no section in this file will be kept, then we can
3095
   toss out debug sections.  */
3096
0
      if (!some_kept)
3097
0
  continue;
3098
3099
      /* Keep debug and special sections like .comment when they are
3100
   not part of a group, or when we have single-member groups.  */
3101
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3102
0
  if ((isec->flags & SEC_DEBUGGING) != 0
3103
0
      || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3104
0
    isec->gc_mark = 1;
3105
0
    }
3106
0
  return true;
3107
0
}
3108
3109
/* Sweep symbols in swept sections.  Called via coff_link_hash_traverse.  */
3110
3111
static bool
3112
coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
3113
          void *data ATTRIBUTE_UNUSED)
3114
0
{
3115
0
  if (h->root.type == bfd_link_hash_warning)
3116
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
3117
3118
0
  if ((h->root.type == bfd_link_hash_defined
3119
0
       || h->root.type == bfd_link_hash_defweak)
3120
0
      && !h->root.u.def.section->gc_mark
3121
0
      && !(h->root.u.def.section->owner->flags & DYNAMIC))
3122
0
    {
3123
      /* Do our best to hide the symbol.  */
3124
0
      h->root.u.def.section = bfd_und_section_ptr;
3125
0
      h->symbol_class = C_HIDDEN;
3126
0
    }
3127
3128
0
  return true;
3129
0
}
3130
3131
/* The sweep phase of garbage collection.  Remove all garbage sections.  */
3132
3133
typedef bool (*gc_sweep_hook_fn)
3134
  (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
3135
3136
static inline bool
3137
is_subsection (const char *str, const char *prefix)
3138
0
{
3139
0
  size_t n = strlen (prefix);
3140
0
  if (strncmp (str, prefix, n) != 0)
3141
0
    return false;
3142
0
  if (str[n] == 0)
3143
0
    return true;
3144
0
  else if (str[n] != '$')
3145
0
    return false;
3146
0
  return ISDIGIT (str[n + 1]) && str[n + 2] == 0;
3147
0
}
3148
3149
static bool
3150
coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3151
0
{
3152
0
  bfd *sub;
3153
3154
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3155
0
    {
3156
0
      asection *o;
3157
3158
0
      if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3159
0
  continue;
3160
3161
0
      for (o = sub->sections; o != NULL; o = o->next)
3162
0
  {
3163
      /* Keep debug and special sections.  */
3164
0
    if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3165
0
        || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3166
0
      o->gc_mark = 1;
3167
0
    else if (startswith (o->name, ".idata")
3168
0
       || startswith (o->name, ".pdata")
3169
0
       || startswith (o->name, ".xdata")
3170
0
       || is_subsection (o->name, ".didat")
3171
0
       || startswith (o->name, ".rsrc"))
3172
0
      o->gc_mark = 1;
3173
3174
0
    if (o->gc_mark)
3175
0
      continue;
3176
3177
    /* Skip sweeping sections already excluded.  */
3178
0
    if (o->flags & SEC_EXCLUDE)
3179
0
      continue;
3180
3181
    /* Since this is early in the link process, it is simple
3182
       to remove a section from the output.  */
3183
0
    o->flags |= SEC_EXCLUDE;
3184
3185
0
    if (info->print_gc_sections && o->size != 0)
3186
      /* xgettext: c-format */
3187
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3188
0
        o, sub);
3189
3190
#if 0
3191
    /* But we also have to update some of the relocation
3192
       info we collected before.  */
3193
    if (gc_sweep_hook
3194
        && (o->flags & SEC_RELOC) != 0
3195
        && o->reloc_count > 0
3196
        && !bfd_is_abs_section (o->output_section))
3197
      {
3198
        struct internal_reloc *internal_relocs;
3199
        bool r;
3200
3201
        internal_relocs
3202
    = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3203
               info->keep_memory);
3204
        if (internal_relocs == NULL)
3205
    return false;
3206
3207
        r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3208
3209
        if (coff_section_data (o)->relocs != internal_relocs)
3210
    free (internal_relocs);
3211
3212
        if (!r)
3213
    return false;
3214
      }
3215
#endif
3216
0
  }
3217
0
    }
3218
3219
  /* Remove the symbols that were in the swept sections from the dynamic
3220
     symbol table.  */
3221
0
  coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3222
0
         NULL);
3223
3224
0
  return true;
3225
0
}
3226
3227
/* Keep all sections containing symbols undefined on the command-line,
3228
   and the section containing the entry symbol.  */
3229
3230
static void
3231
_bfd_coff_gc_keep (struct bfd_link_info *info)
3232
0
{
3233
0
  struct bfd_sym_chain *sym;
3234
3235
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3236
0
    {
3237
0
      struct coff_link_hash_entry *h;
3238
3239
0
      h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3240
0
        false, false, false);
3241
3242
0
      if (h != NULL
3243
0
    && (h->root.type == bfd_link_hash_defined
3244
0
        || h->root.type == bfd_link_hash_defweak)
3245
0
    && !bfd_is_abs_section (h->root.u.def.section))
3246
0
  h->root.u.def.section->flags |= SEC_KEEP;
3247
0
    }
3248
0
}
3249
3250
/* Do mark and sweep of unused sections.  */
3251
3252
bool
3253
_bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3254
0
{
3255
0
  bfd *sub;
3256
3257
  /* FIXME: Should we implement this? */
3258
#if 0
3259
  const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3260
3261
  if (!bed->can_gc_sections
3262
      || !is_coff_hash_table (info->hash))
3263
    {
3264
      _bfd_error_handler(_("warning: gc-sections option ignored"));
3265
      return true;
3266
    }
3267
#endif
3268
3269
0
  _bfd_coff_gc_keep (info);
3270
3271
  /* Grovel through relocs to find out who stays ...  */
3272
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3273
0
    {
3274
0
      asection *o;
3275
3276
0
      if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3277
0
  continue;
3278
3279
0
      for (o = sub->sections; o != NULL; o = o->next)
3280
0
  {
3281
0
    if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3282
0
         || startswith (o->name, ".vectors")
3283
0
         || startswith (o->name, ".ctors")
3284
0
         || startswith (o->name, ".dtors"))
3285
0
        && !o->gc_mark)
3286
0
      {
3287
0
        if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3288
0
    return false;
3289
0
      }
3290
0
  }
3291
0
    }
3292
3293
  /* Allow the backend to mark additional target specific sections.  */
3294
0
  _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3295
3296
  /* ... and mark SEC_EXCLUDE for those that go.  */
3297
0
  return coff_gc_sweep (abfd, info);
3298
0
}
3299
3300
/* Return name used to identify a comdat group.  */
3301
3302
const char *
3303
_bfd_coff_group_name (bfd *abfd, const asection *sec)
3304
0
{
3305
0
  struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3306
0
  if (ci != NULL)
3307
0
    return ci->name;
3308
0
  return NULL;
3309
0
}
3310
3311
bool
3312
_bfd_coff_free_cached_info (bfd *abfd)
3313
1.67M
{
3314
1.67M
  struct coff_tdata *tdata;
3315
3316
1.67M
  if (bfd_family_coff (abfd)
3317
1.67M
      && (bfd_get_format (abfd) == bfd_object
3318
1.61M
    || bfd_get_format (abfd) == bfd_core)
3319
55.4k
      && (tdata = coff_data (abfd)) != NULL)
3320
55.4k
    {
3321
55.4k
      if (tdata->section_by_index)
3322
0
  {
3323
0
    htab_delete (tdata->section_by_index);
3324
0
    tdata->section_by_index = NULL;
3325
0
  }
3326
3327
55.4k
      if (tdata->section_by_target_index)
3328
7.54k
  {
3329
7.54k
    htab_delete (tdata->section_by_target_index);
3330
7.54k
    tdata->section_by_target_index = NULL;
3331
7.54k
  }
3332
3333
55.4k
      if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
3334
13.3k
  {
3335
13.3k
    htab_delete (pe_data (abfd)->comdat_hash);
3336
13.3k
    pe_data (abfd)->comdat_hash = NULL;
3337
13.3k
  }
3338
3339
55.4k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3340
55.4k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
3341
3342
      /* PR 25447:
3343
   Do not clear the keep_syms and keep_strings flags.
3344
   These may have been set by pe_ILF_build_a_bfd() indicating
3345
   that the syms and strings pointers are not to be freed.  */
3346
55.4k
      _bfd_coff_free_symbols (abfd);
3347
3348
      /* Free raw syms, and any other data bfd_alloc'd after raw syms
3349
   are read.  */
3350
55.4k
      if (!obj_coff_keep_raw_syms (abfd) && obj_raw_syments (abfd))
3351
7.17k
  {
3352
7.17k
    bfd_release (abfd, obj_raw_syments (abfd));
3353
7.17k
    obj_raw_syments (abfd) = NULL;
3354
7.17k
    obj_symbols (abfd) = NULL;
3355
7.17k
    obj_convert (abfd) = NULL;
3356
7.17k
  }
3357
55.4k
    }
3358
3359
1.67M
  return _bfd_generic_bfd_free_cached_info (abfd);
3360
1.67M
}