Coverage Report

Created: 2026-05-11 07:54

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