Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/cofflink.c
Line
Count
Source
1
/* COFF specific linker code.
2
   Copyright (C) 1994-2026 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor, 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
/* This file contains the COFF backend linker code.  */
23
24
#include "sysdep.h"
25
#include "bfd.h"
26
#include "bfdlink.h"
27
#include "libbfd.h"
28
#include "coff/internal.h"
29
#include "libcoff.h"
30
#include "elf-bfd.h"
31
#include "safe-ctype.h"
32
33
static bool coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
34
static bool coff_link_check_archive_element
35
  (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
36
   bool *);
37
static bool coff_link_add_symbols (bfd *, struct bfd_link_info *);
38
39
static bool
40
coff_link_hash_pe_weak_external_has_real_fallback
41
  (struct coff_link_hash_entry *h)
42
0
{
43
0
  struct coff_link_hash_entry *h2;
44
0
  unsigned long symndx;
45
46
0
  if (h->symbol_class != C_NT_WEAK
47
0
      || h->numaux != 1
48
0
      || h->aux == NULL
49
0
      || h->auxbfd == NULL
50
0
      || ! obj_pe (h->auxbfd)
51
0
      || obj_coff_sym_hashes (h->auxbfd) == NULL)
52
0
    return false;
53
54
  /* The PE weak-external aux entry names the fallback symbol by raw
55
     symbol index.  Look up the corresponding link hash entry so we can
56
     test the fallback's resolved state, not just its object-file entry.  */
57
0
  symndx = h->aux->x_sym.x_tagndx.u32;
58
0
  if (symndx >= obj_raw_syment_count (h->auxbfd))
59
0
    return false;
60
61
0
  h2 = obj_coff_sym_hashes (h->auxbfd)[symndx];
62
0
  if (h2 == NULL)
63
0
    return false;
64
65
0
  while (h2->root.type == bfd_link_hash_indirect
66
0
   || h2->root.type == bfd_link_hash_warning)
67
0
    h2 = (struct coff_link_hash_entry *) h2->root.u.i.link;
68
69
  /* A weak declaration with no fallback uses the absolute-zero null
70
     symbol.  Only a defined real fallback means this archive member has
71
     already satisfied the weak external.  */
72
0
  return ((h2->root.type == bfd_link_hash_defined
73
0
     || h2->root.type == bfd_link_hash_defweak)
74
0
    && !(bfd_is_abs_section (h2->root.u.def.section)
75
0
         && h2->root.u.def.value == 0));
76
0
}
77
78
/* Return TRUE if SYM is a weak, external symbol.  */
79
#define IS_WEAK_EXTERNAL(abfd, sym)     \
80
0
  ((sym).n_sclass == C_WEAKEXT        \
81
0
   || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
82
83
/* Return TRUE if SYM is an external symbol.  */
84
#define IS_EXTERNAL(abfd, sym)        \
85
0
  ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
86
87
/* Define macros so that the ISFCN, et. al., macros work correctly.
88
   These macros are defined in include/coff/internal.h in terms of
89
   N_TMASK, etc.  These definitions require a user to define local
90
   variables with the appropriate names, and with values from the
91
   coff_data (abfd) structure.  */
92
93
0
#define N_TMASK n_tmask
94
0
#define N_BTSHFT n_btshft
95
0
#define N_BTMASK n_btmask
96
97
/* Create an entry in a COFF linker hash table.  */
98
99
struct bfd_hash_entry *
100
_bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
101
           struct bfd_hash_table *table,
102
           const char *string)
103
0
{
104
0
  struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
105
106
  /* Allocate the structure if it has not already been allocated by a
107
     subclass.  */
108
0
  if (ret == (struct coff_link_hash_entry *) NULL)
109
0
    ret = ((struct coff_link_hash_entry *)
110
0
     bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
111
0
  if (ret == (struct coff_link_hash_entry *) NULL)
112
0
    return (struct bfd_hash_entry *) ret;
113
114
  /* Call the allocation method of the superclass.  */
115
0
  ret = ((struct coff_link_hash_entry *)
116
0
   _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
117
0
         table, string));
118
0
  if (ret != (struct coff_link_hash_entry *) NULL)
119
0
    {
120
      /* Set local fields.  */
121
0
      ret->indx = -1;
122
0
      ret->type = T_NULL;
123
0
      ret->symbol_class = C_NULL;
124
0
      ret->numaux = 0;
125
0
      ret->auxbfd = NULL;
126
0
      ret->aux = NULL;
127
0
    }
128
129
0
  return (struct bfd_hash_entry *) ret;
130
0
}
131
132
struct bfd_hash_entry *
133
_decoration_hash_newfunc (struct bfd_hash_entry *entry,
134
        struct bfd_hash_table *table,
135
        const char *string)
136
0
{
137
0
  struct decoration_hash_entry *ret = (struct decoration_hash_entry *) entry;
138
139
  /* Allocate the structure if it has not already been allocated by a
140
     subclass.  */
141
0
  if (ret == NULL)
142
0
    {
143
0
      ret = (struct decoration_hash_entry *)
144
0
      bfd_hash_allocate (table, sizeof (struct decoration_hash_entry));
145
0
      if (ret == NULL)
146
0
  return NULL;
147
0
    }
148
149
  /* Call the allocation method of the superclass.  */
150
0
  ret = (struct decoration_hash_entry *)
151
0
  _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
152
0
  if (ret != NULL)
153
0
    {
154
0
      ret->decorated_link = NULL;
155
0
    }
156
157
0
  return (struct bfd_hash_entry *) ret;
158
0
}
159
160
/* Initialize a COFF linker hash table.  */
161
162
bool
163
_bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
164
        bfd *abfd,
165
        struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
166
                   struct bfd_hash_table *,
167
                   const char *),
168
        unsigned int entsize)
169
0
{
170
0
  memset (&table->stab_info, 0, sizeof (table->stab_info));
171
172
0
  return bfd_hash_table_init (&table->decoration_hash,
173
0
            _decoration_hash_newfunc,
174
0
            sizeof (struct decoration_hash_entry))
175
0
   &&_bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
176
0
}
177
178
/* Create a COFF linker hash table.  */
179
180
struct bfd_link_hash_table *
181
_bfd_coff_link_hash_table_create (bfd *abfd)
182
0
{
183
0
  struct coff_link_hash_table *ret;
184
0
  size_t amt = sizeof (struct coff_link_hash_table);
185
186
0
  ret = (struct coff_link_hash_table *) bfd_malloc (amt);
187
0
  if (ret == NULL)
188
0
    return NULL;
189
190
0
  if (! _bfd_coff_link_hash_table_init (ret, abfd,
191
0
          _bfd_coff_link_hash_newfunc,
192
0
          sizeof (struct coff_link_hash_entry)))
193
0
    {
194
0
      free (ret);
195
0
      return (struct bfd_link_hash_table *) NULL;
196
0
    }
197
0
  return &ret->root;
198
0
}
199
200
/* Create an entry in a COFF debug merge hash table.  */
201
202
struct bfd_hash_entry *
203
_bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
204
            struct bfd_hash_table *table,
205
            const char *string)
206
0
{
207
0
  struct coff_debug_merge_hash_entry *ret =
208
0
    (struct coff_debug_merge_hash_entry *) entry;
209
210
  /* Allocate the structure if it has not already been allocated by a
211
     subclass.  */
212
0
  if (ret == (struct coff_debug_merge_hash_entry *) NULL)
213
0
    ret = ((struct coff_debug_merge_hash_entry *)
214
0
     bfd_hash_allocate (table,
215
0
            sizeof (struct coff_debug_merge_hash_entry)));
216
0
  if (ret == (struct coff_debug_merge_hash_entry *) NULL)
217
0
    return (struct bfd_hash_entry *) ret;
218
219
  /* Call the allocation method of the superclass.  */
220
0
  ret = ((struct coff_debug_merge_hash_entry *)
221
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
222
0
  if (ret != (struct coff_debug_merge_hash_entry *) NULL)
223
0
    {
224
      /* Set local fields.  */
225
0
      ret->types = NULL;
226
0
    }
227
228
0
  return (struct bfd_hash_entry *) ret;
229
0
}
230
231
/* Given a COFF BFD, add symbols to the global hash table as
232
   appropriate.  */
233
234
bool
235
_bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
236
0
{
237
0
  switch (bfd_get_format (abfd))
238
0
    {
239
0
    case bfd_object:
240
0
      return coff_link_add_object_symbols (abfd, info);
241
0
    case bfd_archive:
242
0
      return _bfd_generic_link_add_archive_symbols
243
0
  (abfd, info, coff_link_check_archive_element);
244
0
    default:
245
0
      bfd_set_error (bfd_error_wrong_format);
246
0
      return false;
247
0
    }
248
0
}
249
250
/* Add symbols from a COFF object file.  */
251
252
static bool
253
coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
254
0
{
255
0
  if (! _bfd_coff_get_external_symbols (abfd))
256
0
    return false;
257
0
  if (! coff_link_add_symbols (abfd, info))
258
0
    return false;
259
260
0
  if (! info->keep_memory
261
0
      && ! _bfd_coff_free_symbols (abfd))
262
0
    return false;
263
264
0
  return true;
265
0
}
266
267
/* Check a single archive element to see if we need to include it in
268
   the link.  *PNEEDED is set according to whether this element is
269
   needed in the link or not.  This is called via
270
   _bfd_generic_link_add_archive_symbols.  */
271
272
static bool
273
coff_link_check_archive_element (bfd *abfd,
274
         struct bfd_link_info *info,
275
         struct bfd_link_hash_entry *h,
276
         const char *name,
277
         bool *pneeded)
278
0
{
279
0
  *pneeded = false;
280
281
  /* PR 22369 - Skip non COFF objects in the archive.  */
282
0
  if (! bfd_family_coff (abfd))
283
0
    return true;
284
285
  /* We are only interested in symbols that are currently undefined.
286
     If a symbol is currently known to be common, COFF linkers do not
287
     bring in an object file which defines it.  */
288
0
  if (h->type != bfd_link_hash_undefined)
289
0
    return true;
290
291
  /* If the archive element has already been loaded then one
292
     of the symbols defined by that element might have been
293
     made undefined due to being in a discarded section.  */
294
0
  if (((struct coff_link_hash_entry *) h)->indx == -3)
295
0
    return true;
296
297
  /* A PE weak external can stay undefined even after its fallback has
298
     been defined by this archive member.  Avoid extracting the member
299
     again if the same archive is searched more than once.  */
300
0
  if (coff_link_hash_pe_weak_external_has_real_fallback
301
0
      ((struct coff_link_hash_entry *) h))
302
0
    return true;
303
304
  /* Include this element?  */
305
0
  if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
306
0
    return true;
307
0
  *pneeded = true;
308
309
0
  return bfd_link_add_symbols (abfd, info);
310
0
}
311
312
/* Add all the symbols from an object file to the hash table.  */
313
314
static bool
315
coff_link_add_symbols (bfd *abfd,
316
           struct bfd_link_info *info)
317
0
{
318
0
  unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
319
0
  unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
320
0
  unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
321
0
  bool keep_syms;
322
0
  bool default_copy;
323
0
  bfd_size_type symcount;
324
0
  struct coff_link_hash_entry **sym_hash;
325
0
  bfd_size_type symesz;
326
0
  bfd_byte *esym;
327
0
  bfd_byte *esym_end;
328
0
  bfd_size_type amt;
329
330
0
  symcount = obj_raw_syment_count (abfd);
331
332
0
  if (symcount == 0)
333
0
    return true;   /* Nothing to do.  */
334
335
  /* Keep the symbols during this function, in case the linker needs
336
     to read the generic symbols in order to report an error message.  */
337
0
  keep_syms = obj_coff_keep_syms (abfd);
338
0
  obj_coff_keep_syms (abfd) = true;
339
340
0
  if (info->keep_memory)
341
0
    default_copy = false;
342
0
  else
343
0
    default_copy = true;
344
345
  /* We keep a list of the linker hash table entries that correspond
346
     to particular symbols.  */
347
0
  amt = symcount * sizeof (struct coff_link_hash_entry *);
348
0
  sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
349
0
  if (sym_hash == NULL)
350
0
    goto error_return;
351
0
  obj_coff_sym_hashes (abfd) = sym_hash;
352
353
0
  symesz = bfd_coff_symesz (abfd);
354
0
  BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
355
0
  esym = (bfd_byte *) obj_coff_external_syms (abfd);
356
0
  esym_end = esym + symcount * symesz;
357
0
  while (esym < esym_end)
358
0
    {
359
0
      struct internal_syment sym;
360
0
      enum coff_symbol_classification classification;
361
0
      bool copy;
362
363
0
      bfd_coff_swap_sym_in (abfd, esym, &sym);
364
365
0
      classification = bfd_coff_classify_symbol (abfd, &sym);
366
0
      if (classification != COFF_SYMBOL_LOCAL)
367
0
  {
368
0
    const char *name;
369
0
    char buf[SYMNMLEN + 1];
370
0
    flagword flags;
371
0
    asection *section;
372
0
    bfd_vma value;
373
0
    bool addit;
374
0
    bool discarded = false;
375
376
    /* This symbol is externally visible.  */
377
378
0
    name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
379
0
    if (name == NULL)
380
0
      goto error_return;
381
382
    /* We must copy the name into memory if we got it from the
383
       syment itself, rather than the string table.  */
384
0
    copy = default_copy;
385
0
    if (sym._n._n_n._n_zeroes != 0
386
0
        || sym._n._n_n._n_offset == 0)
387
0
      copy = true;
388
389
0
    value = sym.n_value;
390
391
0
    switch (classification)
392
0
      {
393
0
      default:
394
0
        abort ();
395
396
0
      case COFF_SYMBOL_GLOBAL:
397
0
        flags = BSF_EXPORT | BSF_GLOBAL;
398
0
        section = coff_section_from_bfd_index (abfd, sym.n_scnum);
399
0
        if (discarded_section (section))
400
0
    {
401
0
      discarded = true;
402
0
      section = bfd_und_section_ptr;
403
0
    }
404
0
        else if (! obj_pe (abfd))
405
0
    value -= section->vma;
406
0
        break;
407
408
0
      case COFF_SYMBOL_UNDEFINED:
409
0
        flags = 0;
410
0
        section = bfd_und_section_ptr;
411
0
        break;
412
413
0
      case COFF_SYMBOL_COMMON:
414
0
        flags = BSF_GLOBAL;
415
0
        section = bfd_com_section_ptr;
416
0
        break;
417
418
0
      case COFF_SYMBOL_PE_SECTION:
419
0
        flags = BSF_SECTION_SYM | BSF_GLOBAL;
420
0
        section = coff_section_from_bfd_index (abfd, sym.n_scnum);
421
0
        if (discarded_section (section))
422
0
    section = bfd_und_section_ptr;
423
0
        break;
424
0
      }
425
426
0
    if (IS_WEAK_EXTERNAL (abfd, sym))
427
0
      flags = BSF_WEAK;
428
429
0
    addit = true;
430
431
    /* In the PE format, section symbols actually refer to the
432
       start of the output section.  We handle them specially
433
       here.  */
434
0
    if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
435
0
      {
436
0
        *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
437
0
             name, false, copy, false);
438
0
        if (*sym_hash != NULL)
439
0
    {
440
0
      if (((*sym_hash)->coff_link_hash_flags
441
0
           & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
442
0
          && (*sym_hash)->root.type != bfd_link_hash_undefined
443
0
          && (*sym_hash)->root.type != bfd_link_hash_undefweak)
444
0
        _bfd_error_handler
445
0
          (_("warning: symbol `%s' is both section and non-section"),
446
0
           name);
447
448
0
      addit = false;
449
0
    }
450
0
      }
451
452
    /* The Microsoft Visual C compiler does string pooling by
453
       hashing the constants to an internal symbol name, and
454
       relying on the linker comdat support to discard
455
       duplicate names.  However, if one string is a literal and
456
       one is a data initializer, one will end up in the .data
457
       section and one will end up in the .rdata section.  The
458
       Microsoft linker will combine them into the .data
459
       section, which seems to be wrong since it might cause the
460
       literal to change.
461
462
       As long as there are no external references to the
463
       symbols, which there shouldn't be, we can treat the .data
464
       and .rdata instances as separate symbols.  The comdat
465
       code in the linker will do the appropriate merging.  Here
466
       we avoid getting a multiple definition error for one of
467
       these special symbols.
468
469
       FIXME: I don't think this will work in the case where
470
       there are two object files which use the constants as a
471
       literal and two object files which use it as a data
472
       initializer.  One or the other of the second object files
473
       is going to wind up with an inappropriate reference.  */
474
0
    if (obj_pe (abfd)
475
0
        && (classification == COFF_SYMBOL_GLOBAL
476
0
      || classification == COFF_SYMBOL_PE_SECTION)
477
0
        && coff_section_data (abfd, section) != NULL
478
0
        && coff_section_data (abfd, section)->comdat != NULL
479
0
        && startswith (name, "??_")
480
0
        && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
481
0
      {
482
0
        if (*sym_hash == NULL)
483
0
    *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
484
0
               name, false, copy, false);
485
0
        if (*sym_hash != NULL
486
0
      && (*sym_hash)->root.type == bfd_link_hash_defined
487
0
      && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
488
0
      && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
489
0
           coff_section_data (abfd, section)->comdat->name) == 0)
490
0
    addit = false;
491
0
      }
492
493
0
    if (addit)
494
0
      {
495
0
        if (! (_bfd_generic_link_add_one_symbol
496
0
         (info, abfd, name, flags, section, value,
497
0
          (const char *) NULL, copy, false,
498
0
          (struct bfd_link_hash_entry **) sym_hash)))
499
0
    goto error_return;
500
501
0
        if (discarded)
502
0
    (*sym_hash)->indx = -3;
503
0
      }
504
505
0
    if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
506
0
      (*sym_hash)->coff_link_hash_flags |=
507
0
        COFF_LINK_HASH_PE_SECTION_SYMBOL;
508
509
    /* Limit the alignment of a common symbol to the possible
510
       alignment of a section.  There is no point to permitting
511
       a higher alignment for a common symbol: we can not
512
       guarantee it, and it may cause us to allocate extra space
513
       in the common section.  */
514
0
    if (section == bfd_com_section_ptr
515
0
        && (*sym_hash)->root.type == bfd_link_hash_common
516
0
        && ((*sym_hash)->root.u.c.p->alignment_power
517
0
      > bfd_coff_default_section_alignment_power (abfd)))
518
0
      (*sym_hash)->root.u.c.p->alignment_power
519
0
        = bfd_coff_default_section_alignment_power (abfd);
520
521
0
    if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
522
0
      {
523
        /* If we don't have any symbol information currently in
524
     the hash table, or if we are looking at a symbol
525
     definition, then update the symbol class and type in
526
     the hash table.  Also update if the incoming symbol is
527
     a weak external with an aux record (PE COFF weak alias)
528
     and the existing symbol is still undefined, so the
529
     fallback alias information is preserved for the linker's
530
     relocation resolution.  */
531
0
        if (((*sym_hash)->symbol_class == C_NULL
532
0
       && (*sym_hash)->type == T_NULL)
533
0
      || sym.n_scnum != 0
534
0
      || (sym.n_value != 0
535
0
          && (*sym_hash)->root.type != bfd_link_hash_defined
536
0
          && (*sym_hash)->root.type != bfd_link_hash_defweak)
537
0
      || (IS_WEAK_EXTERNAL (abfd, sym)
538
0
          && sym.n_numaux > 0
539
0
          && (*sym_hash)->root.type == bfd_link_hash_undefined))
540
0
    {
541
0
      (*sym_hash)->symbol_class = sym.n_sclass;
542
0
      if (sym.n_type != T_NULL)
543
0
        {
544
          /* We want to warn if the type changed, but not
545
       if it changed from an unspecified type.
546
       Testing the whole type byte may work, but the
547
       change from (e.g.) a function of unspecified
548
       type to function of known type also wants to
549
       skip the warning.  */
550
0
          if ((*sym_hash)->type != T_NULL
551
0
        && (*sym_hash)->type != sym.n_type
552
0
        && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
553
0
             && (BTYPE ((*sym_hash)->type) == T_NULL
554
0
           || BTYPE (sym.n_type) == T_NULL)))
555
0
      _bfd_error_handler
556
        /* xgettext: c-format */
557
0
        (_("warning: type of symbol `%s' changed"
558
0
           " from %d to %d in %pB"),
559
0
         name, (*sym_hash)->type, sym.n_type, abfd);
560
561
          /* We don't want to change from a meaningful
562
       base type to a null one, but if we know
563
       nothing, take what little we might now know.  */
564
0
          if (BTYPE (sym.n_type) != T_NULL
565
0
        || (*sym_hash)->type == T_NULL)
566
0
      (*sym_hash)->type = sym.n_type;
567
0
        }
568
0
      (*sym_hash)->auxbfd = abfd;
569
0
      if (sym.n_numaux != 0)
570
0
        {
571
0
          union internal_auxent *alloc;
572
0
          unsigned int i;
573
0
          bfd_byte *eaux;
574
0
          union internal_auxent *iaux;
575
576
0
          (*sym_hash)->numaux = sym.n_numaux;
577
0
          alloc = bfd_hash_allocate (&info->hash->table,
578
0
             (sym.n_numaux
579
0
              * sizeof (*alloc)));
580
0
          if (alloc == NULL)
581
0
      goto error_return;
582
0
          for (i = 0, eaux = esym + symesz, iaux = alloc;
583
0
         i < sym.n_numaux;
584
0
         i++, eaux += symesz, iaux++)
585
0
      bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
586
0
                sym.n_sclass, (int) i,
587
0
                sym.n_numaux, iaux);
588
0
          (*sym_hash)->aux = alloc;
589
0
        }
590
0
    }
591
592
        /* When two PE COFF weak externals meet (both with aux
593
     records specifying fallback aliases), prefer the one
594
     whose fallback resolves to a defined symbol over one
595
     whose fallback is undefined or NULL.  This
596
     handles the case where a weak declaration (with a
597
     fallback of NULL) is seen before a weak
598
     definition (with a fallback of the actual function
599
     body).  */
600
0
        else if (IS_WEAK_EXTERNAL (abfd, sym)
601
0
           && sym.n_numaux > 0
602
0
           && (*sym_hash)->root.type == bfd_link_hash_undefweak
603
0
           && (*sym_hash)->symbol_class == C_NT_WEAK
604
0
           && (*sym_hash)->numaux == 1)
605
0
    {
606
      /* Parse the incoming aux to get the fallback tagndx.  */
607
0
      union internal_auxent new_aux;
608
0
      unsigned long new_tagndx;
609
0
      unsigned long old_tagndx;
610
0
      struct coff_link_hash_entry *h2_new = NULL;
611
0
      struct coff_link_hash_entry *h2_old = NULL;
612
0
      bool new_is_real_fallback;
613
0
      bool old_is_unresolved_fallback;
614
615
0
      bfd_coff_swap_aux_in (abfd, esym + symesz, sym.n_type,
616
0
          sym.n_sclass, 0, sym.n_numaux,
617
0
          &new_aux);
618
0
      new_tagndx = new_aux.x_sym.x_tagndx.u32;
619
620
0
      if (new_tagndx < obj_raw_syment_count (abfd))
621
0
        h2_new = obj_coff_sym_hashes (abfd)[new_tagndx];
622
623
0
      old_tagndx = (*sym_hash)->aux->x_sym.x_tagndx.u32;
624
0
      if (old_tagndx
625
0
          < obj_raw_syment_count ((*sym_hash)->auxbfd))
626
0
        h2_old = obj_coff_sym_hashes
627
0
          ((*sym_hash)->auxbfd)[old_tagndx];
628
629
      /* Update if the new fallback is a real definition but
630
         the old one is not.  A weak declaration with no
631
         definition uses the COFF null symbol as its fallback.
632
         In the hash table that fallback looks like a defined
633
         absolute symbol with value zero, so treat that case as
634
         unresolved here.  */
635
0
      new_is_real_fallback
636
0
        = (h2_new != NULL
637
0
           && (h2_new->root.type == bfd_link_hash_defined
638
0
         || h2_new->root.type == bfd_link_hash_defweak)
639
0
           && !(bfd_is_abs_section (h2_new->root.u.def.section)
640
0
          && h2_new->root.u.def.value == 0));
641
0
      old_is_unresolved_fallback
642
0
        = (h2_old == NULL
643
0
           || h2_old->root.type == bfd_link_hash_undefined
644
0
           || h2_old->root.type == bfd_link_hash_undefweak
645
0
           || (h2_old->root.type == bfd_link_hash_defined
646
0
         && bfd_is_abs_section (h2_old->root.u.def.section)
647
0
         && h2_old->root.u.def.value == 0));
648
649
0
      if (new_is_real_fallback && old_is_unresolved_fallback)
650
0
        {
651
0
          union internal_auxent *alloc;
652
0
          unsigned int i;
653
0
          bfd_byte *eaux;
654
0
          union internal_auxent *iaux;
655
656
0
          (*sym_hash)->symbol_class = sym.n_sclass;
657
0
          (*sym_hash)->auxbfd = abfd;
658
0
          (*sym_hash)->numaux = sym.n_numaux;
659
0
          alloc = bfd_hash_allocate (&info->hash->table,
660
0
             (sym.n_numaux
661
0
              * sizeof (*alloc)));
662
0
          if (alloc == NULL)
663
0
      goto error_return;
664
0
          for (i = 0, eaux = esym + symesz, iaux = alloc;
665
0
         i < sym.n_numaux;
666
0
         i++, eaux += symesz, iaux++)
667
0
      bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
668
0
                sym.n_sclass, (int) i,
669
0
                sym.n_numaux, iaux);
670
0
          (*sym_hash)->aux = alloc;
671
0
        }
672
0
    }
673
0
      }
674
675
0
    if (classification == COFF_SYMBOL_PE_SECTION
676
0
        && (*sym_hash)->numaux != 0)
677
0
      {
678
        /* Some PE sections (such as .bss) have a zero size in
679
     the section header, but a non-zero size in the AUX
680
     record.  Correct that here.
681
682
     FIXME: This is not at all the right place to do this.
683
     For example, it won't help objdump.  This needs to be
684
     done when we swap in the section header.  */
685
0
        BFD_ASSERT ((*sym_hash)->numaux == 1);
686
0
        if (section->size == 0)
687
0
    section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
688
689
        /* FIXME: We could test whether the section sizes
690
     matches the size in the aux entry, but apparently
691
     that sometimes fails unexpectedly.  */
692
0
      }
693
0
  }
694
695
0
      esym += (sym.n_numaux + 1) * symesz;
696
0
      sym_hash += sym.n_numaux + 1;
697
0
    }
698
699
  /* If this is a non-traditional, non-relocatable link, try to
700
     optimize the handling of any .stab/.stabstr sections.  */
701
0
  if (! bfd_link_relocatable (info)
702
0
      && ! info->traditional_format
703
0
      && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
704
0
      && (info->strip != strip_all && info->strip != strip_debugger))
705
0
    {
706
0
      asection *stabstr;
707
708
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
709
710
0
      if (stabstr != NULL)
711
0
  {
712
0
    bfd_size_type string_offset = 0;
713
0
    asection *stab;
714
715
0
    for (stab = abfd->sections; stab; stab = stab->next)
716
0
      if (startswith (stab->name, ".stab")
717
0
    && (!stab->name[5]
718
0
        || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
719
0
      {
720
0
        struct coff_link_hash_table *table;
721
722
0
        table = coff_hash_table (info);
723
724
0
        if (! _bfd_link_section_stabs (abfd, &table->stab_info,
725
0
               stab, stabstr,
726
0
               &string_offset))
727
0
    goto error_return;
728
0
      }
729
0
  }
730
0
    }
731
732
0
  obj_coff_keep_syms (abfd) = keep_syms;
733
734
0
  return true;
735
736
0
 error_return:
737
0
  obj_coff_keep_syms (abfd) = keep_syms;
738
0
  return false;
739
0
}
740

741
/* Do the final link step.  */
742
743
bool
744
_bfd_coff_final_link (bfd *abfd,
745
          struct bfd_link_info *info)
746
0
{
747
0
  bfd_size_type symesz;
748
0
  struct coff_final_link_info flaginfo;
749
0
  bool debug_merge_allocated;
750
0
  bool long_section_names;
751
0
  asection *o;
752
0
  struct bfd_link_order *p;
753
0
  bfd_size_type max_sym_count;
754
0
  bfd_size_type max_lineno_count;
755
0
  bfd_size_type max_reloc_count;
756
0
  bfd_size_type max_output_reloc_count;
757
0
  bfd_size_type max_contents_size;
758
0
  file_ptr rel_filepos;
759
0
  unsigned int relsz;
760
0
  file_ptr line_filepos;
761
0
  unsigned int linesz;
762
0
  bfd *sub;
763
0
  bfd_byte *external_relocs = NULL;
764
0
  char strbuf[STRING_SIZE_SIZE];
765
0
  bfd_size_type amt;
766
767
0
  symesz = bfd_coff_symesz (abfd);
768
769
0
  flaginfo.info = info;
770
0
  flaginfo.output_bfd = abfd;
771
0
  flaginfo.strtab = NULL;
772
0
  flaginfo.section_info = NULL;
773
0
  flaginfo.last_file_index = -1;
774
0
  flaginfo.last_bf_index = -1;
775
0
  flaginfo.internal_syms = NULL;
776
0
  flaginfo.sec_ptrs = NULL;
777
0
  flaginfo.sym_indices = NULL;
778
0
  flaginfo.outsyms = NULL;
779
0
  flaginfo.linenos = NULL;
780
0
  flaginfo.contents = NULL;
781
0
  flaginfo.external_relocs = NULL;
782
0
  flaginfo.internal_relocs = NULL;
783
0
  flaginfo.global_to_static = false;
784
0
  debug_merge_allocated = false;
785
786
0
  coff_data (abfd)->link_info = info;
787
788
0
  flaginfo.strtab = _bfd_stringtab_init ();
789
0
  if (flaginfo.strtab == NULL)
790
0
    goto error_return;
791
792
0
  if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
793
0
    goto error_return;
794
0
  debug_merge_allocated = true;
795
796
  /* Compute the file positions for all the sections.  */
797
0
  if (! abfd->output_has_begun)
798
0
    {
799
0
      if (! bfd_coff_compute_section_file_positions (abfd))
800
0
  goto error_return;
801
0
    }
802
803
  /* Count the line numbers and relocation entries required for the
804
     output file.  Set the file positions for the relocs.  */
805
0
  rel_filepos = obj_relocbase (abfd);
806
0
  relsz = bfd_coff_relsz (abfd);
807
0
  max_contents_size = 0;
808
0
  max_lineno_count = 0;
809
0
  max_reloc_count = 0;
810
811
0
  long_section_names = false;
812
0
  for (o = abfd->sections; o != NULL; o = o->next)
813
0
    {
814
0
      o->reloc_count = 0;
815
0
      o->lineno_count = 0;
816
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
817
0
  {
818
0
    if (p->type == bfd_indirect_link_order)
819
0
      {
820
0
        asection *sec;
821
822
0
        sec = p->u.indirect.section;
823
824
        /* Mark all sections which are to be included in the
825
     link.  This will normally be every section.  We need
826
     to do this so that we can identify any sections which
827
     the linker has decided to not include.  */
828
0
        sec->linker_mark = true;
829
830
0
        if (info->strip == strip_none
831
0
      || info->strip == strip_some)
832
0
    o->lineno_count += sec->lineno_count;
833
834
0
        if (bfd_link_relocatable (info))
835
0
    o->reloc_count += sec->reloc_count;
836
837
0
        if (sec->rawsize > max_contents_size)
838
0
    max_contents_size = sec->rawsize;
839
0
        if (sec->size > max_contents_size)
840
0
    max_contents_size = sec->size;
841
0
        if (sec->lineno_count > max_lineno_count)
842
0
    max_lineno_count = sec->lineno_count;
843
0
        if (sec->reloc_count > max_reloc_count)
844
0
    max_reloc_count = sec->reloc_count;
845
0
      }
846
0
    else if (bfd_link_relocatable (info)
847
0
       && (p->type == bfd_section_reloc_link_order
848
0
           || p->type == bfd_symbol_reloc_link_order))
849
0
      ++o->reloc_count;
850
0
  }
851
0
      if (o->reloc_count == 0)
852
0
  o->rel_filepos = 0;
853
0
      else
854
0
  {
855
0
    o->flags |= SEC_RELOC;
856
0
    o->rel_filepos = rel_filepos;
857
0
    rel_filepos += o->reloc_count * relsz;
858
    /* In PE COFF, if there are at least 0xffff relocations an
859
       extra relocation will be written out to encode the count.  */
860
0
    if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
861
0
      rel_filepos += relsz;
862
0
  }
863
864
0
      if (bfd_coff_long_section_names (abfd)
865
0
    && strlen (o->name) > SCNNMLEN)
866
0
  {
867
    /* This section has a long name which must go in the string
868
       table.  This must correspond to the code in
869
       coff_write_object_contents which puts the string index
870
       into the s_name field of the section header.  That is why
871
       we pass hash as FALSE.  */
872
0
    if (_bfd_stringtab_add (flaginfo.strtab, o->name, false, false)
873
0
        == (bfd_size_type) -1)
874
0
      goto error_return;
875
0
    long_section_names = true;
876
0
  }
877
0
    }
878
879
  /* If doing a relocatable link, allocate space for the pointers we
880
     need to keep.  */
881
0
  if (bfd_link_relocatable (info))
882
0
    {
883
0
      unsigned int i;
884
885
      /* We use section_count + 1, rather than section_count, because
886
   the target_index fields are 1 based.  */
887
0
      amt = abfd->section_count + 1;
888
0
      amt *= sizeof (struct coff_link_section_info);
889
0
      flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
890
0
      if (flaginfo.section_info == NULL)
891
0
  goto error_return;
892
0
      for (i = 0; i <= abfd->section_count; i++)
893
0
  {
894
0
    flaginfo.section_info[i].relocs = NULL;
895
0
    flaginfo.section_info[i].rel_hashes = NULL;
896
0
  }
897
0
    }
898
899
  /* We now know the size of the relocs, so we can determine the file
900
     positions of the line numbers.  */
901
0
  line_filepos = rel_filepos;
902
0
  linesz = bfd_coff_linesz (abfd);
903
0
  max_output_reloc_count = 0;
904
0
  for (o = abfd->sections; o != NULL; o = o->next)
905
0
    {
906
0
      if (o->lineno_count == 0)
907
0
  o->line_filepos = 0;
908
0
      else
909
0
  {
910
0
    o->line_filepos = line_filepos;
911
0
    line_filepos += o->lineno_count * linesz;
912
0
  }
913
914
0
      if (o->reloc_count != 0)
915
0
  {
916
    /* We don't know the indices of global symbols until we have
917
       written out all the local symbols.  For each section in
918
       the output file, we keep an array of pointers to hash
919
       table entries.  Each entry in the array corresponds to a
920
       reloc.  When we find a reloc against a global symbol, we
921
       set the corresponding entry in this array so that we can
922
       fix up the symbol index after we have written out all the
923
       local symbols.
924
925
       Because of this problem, we also keep the relocs in
926
       memory until the end of the link.  This wastes memory,
927
       but only when doing a relocatable link, which is not the
928
       common case.  */
929
0
    BFD_ASSERT (bfd_link_relocatable (info));
930
0
    amt = o->reloc_count;
931
0
    amt *= sizeof (struct internal_reloc);
932
0
    flaginfo.section_info[o->target_index].relocs =
933
0
        (struct internal_reloc *) bfd_malloc (amt);
934
0
    amt = o->reloc_count;
935
0
    amt *= sizeof (struct coff_link_hash_entry *);
936
0
    flaginfo.section_info[o->target_index].rel_hashes =
937
0
        (struct coff_link_hash_entry **) bfd_malloc (amt);
938
0
    if (flaginfo.section_info[o->target_index].relocs == NULL
939
0
        || flaginfo.section_info[o->target_index].rel_hashes == NULL)
940
0
      goto error_return;
941
942
0
    if (o->reloc_count > max_output_reloc_count)
943
0
      max_output_reloc_count = o->reloc_count;
944
0
  }
945
946
      /* Reset the reloc and lineno counts, so that we can use them to
947
   count the number of entries we have output so far.  */
948
0
      o->reloc_count = 0;
949
0
      o->lineno_count = 0;
950
0
    }
951
952
0
  obj_sym_filepos (abfd) = line_filepos;
953
954
  /* Figure out the largest number of symbols in an input BFD.  Take
955
     the opportunity to clear the output_has_begun fields of all the
956
     input BFD's.  */
957
0
  max_sym_count = 0;
958
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
959
0
    {
960
0
      size_t sz;
961
962
0
      sub->output_has_begun = false;
963
0
      sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
964
0
      if (sz > max_sym_count)
965
0
  max_sym_count = sz;
966
0
    }
967
968
  /* Allocate some buffers used while linking.  */
969
0
  amt = max_sym_count * sizeof (struct internal_syment);
970
0
  flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
971
0
  amt = max_sym_count * sizeof (asection *);
972
0
  flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
973
0
  amt = max_sym_count * sizeof (long);
974
0
  flaginfo.sym_indices = (long int *) bfd_malloc (amt);
975
0
  flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
976
0
  amt = max_lineno_count * bfd_coff_linesz (abfd);
977
0
  flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
978
0
  flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
979
0
  amt = max_reloc_count * relsz;
980
0
  flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
981
0
  if (! bfd_link_relocatable (info))
982
0
    {
983
0
      amt = max_reloc_count * sizeof (struct internal_reloc);
984
0
      flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
985
0
    }
986
0
  if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
987
0
      || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
988
0
      || (flaginfo.sym_indices == NULL && max_sym_count > 0)
989
0
      || flaginfo.outsyms == NULL
990
0
      || (flaginfo.linenos == NULL && max_lineno_count > 0)
991
0
      || (flaginfo.contents == NULL && max_contents_size > 0)
992
0
      || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
993
0
      || (! bfd_link_relocatable (info)
994
0
    && flaginfo.internal_relocs == NULL
995
0
    && max_reloc_count > 0))
996
0
    goto error_return;
997
998
  /* We now know the position of everything in the file, except that
999
     we don't know the size of the symbol table and therefore we don't
1000
     know where the string table starts.  We just build the string
1001
     table in memory as we go along.  We process all the relocations
1002
     for a single input file at once.  */
1003
0
  obj_raw_syment_count (abfd) = 0;
1004
1005
0
  if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
1006
0
    {
1007
0
      if (! bfd_coff_start_final_link (abfd, info))
1008
0
  goto error_return;
1009
0
    }
1010
1011
0
  for (o = abfd->sections; o != NULL; o = o->next)
1012
0
    {
1013
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
1014
0
  {
1015
0
    if (p->type == bfd_indirect_link_order
1016
0
        && bfd_family_coff (p->u.indirect.section->owner))
1017
0
      {
1018
0
        sub = p->u.indirect.section->owner;
1019
0
        if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
1020
0
    {
1021
0
      if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
1022
0
        goto error_return;
1023
0
      sub->output_has_begun = true;
1024
0
    }
1025
0
      }
1026
0
    else if (p->type == bfd_section_reloc_link_order
1027
0
       || p->type == bfd_symbol_reloc_link_order)
1028
0
      {
1029
0
        if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
1030
0
    goto error_return;
1031
0
      }
1032
0
    else
1033
0
      {
1034
0
        if (! _bfd_default_link_order (abfd, info, o, p))
1035
0
    goto error_return;
1036
0
      }
1037
0
  }
1038
0
    }
1039
1040
0
  if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
1041
0
    {
1042
      /* Add local symbols from foreign inputs.  */
1043
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
1044
0
  {
1045
0
    unsigned int i;
1046
1047
0
    if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
1048
0
      continue;
1049
0
    for (i = 0; i < bfd_get_symcount (sub); ++i)
1050
0
      {
1051
0
        asymbol *sym = bfd_get_outsymbols (sub) [i];
1052
0
        file_ptr pos;
1053
0
        struct internal_syment isym;
1054
0
        bfd_vma written = 0;
1055
0
        bool rewrite = false;
1056
1057
0
        if ((sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
1058
0
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
1059
0
         | BSF_SYNTHETIC))
1060
0
      || ((sym->flags & BSF_DEBUGGING)
1061
0
          && ! (sym->flags & BSF_FILE)))
1062
0
    continue;
1063
1064
0
        if (! (sym->flags & BSF_LOCAL))
1065
0
    {
1066
      /* For ELF symbols try to represent their function-ness and
1067
         size, if available.  */
1068
0
      if (! (sym->flags & BSF_FUNCTION))
1069
0
        continue;
1070
1071
0
      const elf_symbol_type *elfsym = elf_symbol_from (sym);
1072
0
      if (!elfsym)
1073
0
        continue;
1074
1075
0
      struct coff_link_hash_entry *hent
1076
0
        = (struct coff_link_hash_entry *) bfd_hash_lookup
1077
0
      (&info->hash->table, bfd_asymbol_name (sym),
1078
0
       false, false);
1079
0
      if (!hent)
1080
0
        continue;
1081
1082
      /* coff_data (abfd)->local_n_btshft is what ought to be used
1083
         here, just that it's set only when reading in COFF
1084
         objects.  */
1085
0
      hent->type = DT_FCN << 4;
1086
0
      if (!elfsym->internal_elf_sym.st_size)
1087
0
        continue;
1088
1089
0
      hent->aux = bfd_zalloc (abfd, sizeof (*hent->aux));
1090
0
      if (!hent->aux)
1091
0
        continue;
1092
1093
0
      hent->numaux = 1;
1094
0
      hent->aux->x_sym.x_misc.x_fsize
1095
0
        = elfsym->internal_elf_sym.st_size;
1096
      /* FIXME ->x_sym.x_fcnary.x_fcn.x_endndx would better
1097
         also be set, yet that would likely need to happen
1098
         elsewhere anyway.  */
1099
1100
0
      continue;
1101
0
    }
1102
1103
        /* See if we are discarding symbols with this name.  */
1104
0
        if ((flaginfo.info->strip == strip_some
1105
0
       && (bfd_hash_lookup (flaginfo.info->keep_hash,
1106
0
          bfd_asymbol_name(sym), false, false)
1107
0
           == NULL))
1108
0
      || (((flaginfo.info->discard == discard_sec_merge
1109
0
      && (bfd_asymbol_section (sym)->flags & SEC_MERGE)
1110
0
      && ! bfd_link_relocatable (flaginfo.info))
1111
0
           || flaginfo.info->discard == discard_l)
1112
0
          && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
1113
0
    continue;
1114
1115
0
        pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
1116
0
               * symesz;
1117
0
        if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1118
0
    goto error_return;
1119
0
        if (! coff_write_alien_symbol(abfd, sym, &isym, &written,
1120
0
              flaginfo.strtab,
1121
0
              !flaginfo.info->traditional_format,
1122
0
              NULL, NULL))
1123
0
    goto error_return;
1124
1125
0
        if (isym.n_sclass == C_FILE)
1126
0
    {
1127
0
      if (flaginfo.last_file_index != -1)
1128
0
        {
1129
0
          flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1130
0
          bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1131
0
               flaginfo.outsyms);
1132
0
          pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
1133
0
                 * symesz;
1134
0
          rewrite = true;
1135
0
        }
1136
0
      flaginfo.last_file_index = obj_raw_syment_count (abfd);
1137
0
      flaginfo.last_file = isym;
1138
0
    }
1139
1140
0
        if (rewrite
1141
0
      && (bfd_seek (abfd, pos, SEEK_SET) != 0
1142
0
          || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz))
1143
0
    goto error_return;
1144
1145
0
        obj_raw_syment_count (abfd) += written;
1146
0
      }
1147
0
  }
1148
0
    }
1149
1150
0
  if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
1151
0
    goto error_return;
1152
1153
  /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
1154
1155
0
  coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1156
0
  debug_merge_allocated = false;
1157
1158
0
  free (flaginfo.internal_syms);
1159
0
  flaginfo.internal_syms = NULL;
1160
0
  free (flaginfo.sec_ptrs);
1161
0
  flaginfo.sec_ptrs = NULL;
1162
0
  free (flaginfo.sym_indices);
1163
0
  flaginfo.sym_indices = NULL;
1164
0
  free (flaginfo.linenos);
1165
0
  flaginfo.linenos = NULL;
1166
0
  free (flaginfo.contents);
1167
0
  flaginfo.contents = NULL;
1168
0
  free (flaginfo.external_relocs);
1169
0
  flaginfo.external_relocs = NULL;
1170
0
  free (flaginfo.internal_relocs);
1171
0
  flaginfo.internal_relocs = NULL;
1172
1173
  /* The value of the last C_FILE symbol is supposed to be the symbol
1174
     index of the first external symbol.  Write it out again if
1175
     necessary.  */
1176
0
  if (flaginfo.last_file_index != -1
1177
0
      && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
1178
0
    {
1179
0
      file_ptr pos;
1180
1181
0
      flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1182
0
      bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1183
0
           flaginfo.outsyms);
1184
1185
0
      pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
1186
0
      if (bfd_seek (abfd, pos, SEEK_SET) != 0
1187
0
    || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz)
1188
0
  return false;
1189
0
    }
1190
1191
  /* If doing task linking (ld --task-link) then make a pass through the
1192
     global symbols, writing out any that are defined, and making them
1193
     static.  */
1194
0
  if (info->task_link)
1195
0
    {
1196
0
      flaginfo.failed = false;
1197
0
      coff_link_hash_traverse (coff_hash_table (info),
1198
0
             _bfd_coff_write_task_globals, &flaginfo);
1199
0
      if (flaginfo.failed)
1200
0
  goto error_return;
1201
0
    }
1202
1203
  /* Write out the global symbols.  */
1204
0
  flaginfo.failed = false;
1205
0
  bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1206
0
  if (flaginfo.failed)
1207
0
    goto error_return;
1208
1209
  /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
1210
0
  free (flaginfo.outsyms);
1211
0
  flaginfo.outsyms = NULL;
1212
1213
0
  if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
1214
0
    {
1215
      /* Now that we have written out all the global symbols, we know
1216
   the symbol indices to use for relocs against them, and we can
1217
   finally write out the relocs.  */
1218
0
      amt = max_output_reloc_count * relsz;
1219
0
      external_relocs = (bfd_byte *) bfd_malloc (amt);
1220
0
      if (external_relocs == NULL)
1221
0
  goto error_return;
1222
1223
0
      for (o = abfd->sections; o != NULL; o = o->next)
1224
0
  {
1225
0
    struct internal_reloc *irel;
1226
0
    struct internal_reloc *irelend;
1227
0
    struct coff_link_hash_entry **rel_hash;
1228
0
    bfd_byte *erel;
1229
1230
0
    if (o->reloc_count == 0)
1231
0
      continue;
1232
1233
0
    irel = flaginfo.section_info[o->target_index].relocs;
1234
0
    irelend = irel + o->reloc_count;
1235
0
    rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
1236
0
    erel = external_relocs;
1237
0
    for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1238
0
      {
1239
0
        if (*rel_hash != NULL)
1240
0
    {
1241
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
1242
0
      irel->r_symndx = (*rel_hash)->indx;
1243
0
    }
1244
0
        bfd_coff_swap_reloc_out (abfd, irel, erel);
1245
0
      }
1246
1247
0
    if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1248
0
      goto error_return;
1249
0
    if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
1250
0
      {
1251
        /* In PE COFF, write the count of relocs as the first
1252
     reloc.  The header overflow bit will be set
1253
     elsewhere. */
1254
0
        struct internal_reloc incount;
1255
0
        bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1256
1257
0
        memset (&incount, 0, sizeof (incount));
1258
0
        incount.r_vaddr = o->reloc_count + 1;
1259
0
        bfd_coff_swap_reloc_out (abfd, &incount, excount);
1260
0
        if (bfd_write (excount, relsz, abfd) != relsz)
1261
    /* We'll leak, but it's an error anyway. */
1262
0
    goto error_return;
1263
0
        free (excount);
1264
0
      }
1265
0
    if (bfd_write (external_relocs,
1266
0
       (bfd_size_type) relsz * o->reloc_count, abfd)
1267
0
        != (bfd_size_type) relsz * o->reloc_count)
1268
0
      goto error_return;
1269
0
  }
1270
1271
0
      free (external_relocs);
1272
0
      external_relocs = NULL;
1273
0
    }
1274
1275
  /* Free up the section information.  */
1276
0
  if (flaginfo.section_info != NULL)
1277
0
    {
1278
0
      unsigned int i;
1279
1280
0
      for (i = 0; i < abfd->section_count; i++)
1281
0
  {
1282
0
    free (flaginfo.section_info[i].relocs);
1283
0
    free (flaginfo.section_info[i].rel_hashes);
1284
0
  }
1285
0
      free (flaginfo.section_info);
1286
0
      flaginfo.section_info = NULL;
1287
0
    }
1288
1289
  /* If we have optimized stabs strings, output them.  */
1290
0
  if (coff_hash_table (info)->stab_info.stabstr != NULL)
1291
0
    {
1292
0
      if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1293
0
  return false;
1294
0
    }
1295
1296
  /* Write out the string table.  */
1297
0
  if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1298
0
    {
1299
0
      file_ptr pos;
1300
1301
0
      pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1302
0
      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1303
0
  return false;
1304
1305
0
#if STRING_SIZE_SIZE == 4
1306
0
      H_PUT_32 (abfd,
1307
0
    _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
1308
0
    strbuf);
1309
#else
1310
 #error Change H_PUT_32 above
1311
#endif
1312
1313
0
      if (bfd_write (strbuf, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1314
0
  return false;
1315
1316
0
      if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
1317
0
  return false;
1318
1319
0
      obj_coff_strings_written (abfd) = true;
1320
0
    }
1321
1322
0
  _bfd_stringtab_free (flaginfo.strtab);
1323
1324
  /* Setting symcount to 0 will cause write_object_contents to
1325
     not try to write out the symbols.  */
1326
0
  abfd->symcount = 0;
1327
1328
0
  return true;
1329
1330
0
 error_return:
1331
0
  if (debug_merge_allocated)
1332
0
    coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1333
0
  if (flaginfo.strtab != NULL)
1334
0
    _bfd_stringtab_free (flaginfo.strtab);
1335
0
  if (flaginfo.section_info != NULL)
1336
0
    {
1337
0
      unsigned int i;
1338
1339
0
      for (i = 0; i < abfd->section_count; i++)
1340
0
  {
1341
0
    free (flaginfo.section_info[i].relocs);
1342
0
    free (flaginfo.section_info[i].rel_hashes);
1343
0
  }
1344
0
      free (flaginfo.section_info);
1345
0
    }
1346
0
  free (flaginfo.internal_syms);
1347
0
  free (flaginfo.sec_ptrs);
1348
0
  free (flaginfo.sym_indices);
1349
0
  free (flaginfo.outsyms);
1350
0
  free (flaginfo.linenos);
1351
0
  free (flaginfo.contents);
1352
0
  free (flaginfo.external_relocs);
1353
0
  free (flaginfo.internal_relocs);
1354
0
  free (external_relocs);
1355
0
  return false;
1356
0
}
1357
1358
/* Parse out a -heap <reserved>,<commit> line.  */
1359
1360
static char *
1361
dores_com (char *ptr, bfd *output_bfd, int heap)
1362
0
{
1363
0
  if (obj_pe (output_bfd))
1364
0
    {
1365
0
      int val = strtoul (ptr, &ptr, 0);
1366
1367
0
      if (heap)
1368
0
  pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1369
0
      else
1370
0
  pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1371
1372
0
      if (ptr[0] == ',')
1373
0
  {
1374
0
    val = strtoul (ptr+1, &ptr, 0);
1375
0
    if (heap)
1376
0
      pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1377
0
    else
1378
0
      pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1379
0
  }
1380
0
    }
1381
0
  return ptr;
1382
0
}
1383
1384
static char *
1385
get_name (char *ptr, char **dst)
1386
0
{
1387
0
  while (*ptr == ' ')
1388
0
    ptr++;
1389
0
  *dst = ptr;
1390
0
  while (*ptr && *ptr != ' ')
1391
0
    ptr++;
1392
0
  *ptr = 0;
1393
0
  return ptr+1;
1394
0
}
1395
1396
/* Process any magic embedded commands in a section called .drectve.  */
1397
1398
static int
1399
process_embedded_commands (bfd *output_bfd,
1400
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
1401
         bfd *abfd)
1402
0
{
1403
0
  asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1404
0
  char *s;
1405
0
  char *e;
1406
0
  bfd_byte *copy;
1407
1408
0
  if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0)
1409
0
    return 1;
1410
1411
0
  if (!bfd_malloc_and_get_section (abfd, sec, &copy))
1412
0
    {
1413
0
      free (copy);
1414
0
      return 0;
1415
0
    }
1416
0
  e = (char *) copy + sec->size;
1417
1418
0
  for (s = (char *) copy; s < e ; )
1419
0
    {
1420
0
      if (s[0] != '-')
1421
0
  {
1422
0
    s++;
1423
0
    continue;
1424
0
  }
1425
0
      if (startswith (s, "-attr"))
1426
0
  {
1427
0
    char *name;
1428
0
    char *attribs;
1429
0
    asection *asec;
1430
0
    int loop = 1;
1431
0
    int had_write = 0;
1432
0
    int had_exec= 0;
1433
1434
0
    s += 5;
1435
0
    s = get_name (s, &name);
1436
0
    s = get_name (s, &attribs);
1437
1438
0
    while (loop)
1439
0
      {
1440
0
        switch (*attribs++)
1441
0
    {
1442
0
    case 'W':
1443
0
      had_write = 1;
1444
0
      break;
1445
0
    case 'R':
1446
0
      break;
1447
0
    case 'S':
1448
0
      break;
1449
0
    case 'X':
1450
0
      had_exec = 1;
1451
0
      break;
1452
0
    default:
1453
0
      loop = 0;
1454
0
    }
1455
0
      }
1456
0
    asec = bfd_get_section_by_name (abfd, name);
1457
0
    if (asec)
1458
0
      {
1459
0
        if (had_exec)
1460
0
    asec->flags |= SEC_CODE;
1461
0
        if (!had_write)
1462
0
    asec->flags |= SEC_READONLY;
1463
0
      }
1464
0
  }
1465
0
      else if (startswith (s, "-heap"))
1466
0
  s = dores_com (s + 5, output_bfd, 1);
1467
1468
0
      else if (startswith (s, "-stack"))
1469
0
  s = dores_com (s + 6, output_bfd, 0);
1470
1471
      /* GNU extension for aligned commons.  */
1472
0
      else if (startswith (s, "-aligncomm:"))
1473
0
  {
1474
    /* Common symbols must be aligned on reading, as it
1475
    is too late to do anything here, after they have
1476
    already been allocated, so just skip the directive.  */
1477
0
    s += 11;
1478
0
  }
1479
1480
0
      else
1481
0
  s++;
1482
0
    }
1483
0
  free (copy);
1484
0
  return 1;
1485
0
}
1486
1487
/* Place a marker against all symbols which are used by relocations.
1488
   This marker can be picked up by the 'do we skip this symbol ?'
1489
   loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1490
   that symbol.  */
1491
1492
static void
1493
mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1494
0
{
1495
0
  asection * a;
1496
1497
0
  if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1498
0
    return;
1499
1500
0
  for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1501
0
    {
1502
0
      struct internal_reloc * internal_relocs;
1503
0
      struct internal_reloc * irel;
1504
0
      struct internal_reloc * irelend;
1505
1506
0
      if ((a->flags & SEC_RELOC) == 0 || a->reloc_count  < 1
1507
0
    || a->linker_mark == 0)
1508
0
  continue;
1509
      /* Don't mark relocs in excluded sections.  */
1510
0
      if (a->output_section == bfd_abs_section_ptr)
1511
0
  continue;
1512
1513
      /* Read in the relocs.  */
1514
0
      internal_relocs = bfd_coff_read_internal_relocs
1515
0
  (input_bfd, a, false,
1516
0
   flaginfo->external_relocs,
1517
0
   bfd_link_relocatable (flaginfo->info),
1518
0
   (bfd_link_relocatable (flaginfo->info)
1519
0
    ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1520
0
    : flaginfo->internal_relocs)
1521
0
  );
1522
1523
0
      if (internal_relocs == NULL)
1524
0
  continue;
1525
1526
0
      irel     = internal_relocs;
1527
0
      irelend  = irel + a->reloc_count;
1528
1529
      /* Place a mark in the sym_indices array (whose entries have
1530
   been initialised to 0) for all of the symbols that are used
1531
   in the relocation table.  This will then be picked up in the
1532
   skip/don't-skip pass.  */
1533
0
      for (; irel < irelend; irel++)
1534
0
  if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
1535
0
    flaginfo->sym_indices[irel->r_symndx] = -1;
1536
0
    }
1537
0
}
1538
1539
/* Link an input file into the linker output file.  This function
1540
   handles all the sections and relocations of the input file at once.  */
1541
1542
bool
1543
_bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1544
0
{
1545
0
  unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1546
0
  unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1547
0
  bool (*adjust_symndx)
1548
0
    (bfd *, struct bfd_link_info *, bfd *, asection *,
1549
0
     struct internal_reloc *, bool *);
1550
0
  bfd *output_bfd;
1551
0
  const char *strings;
1552
0
  bfd_size_type syment_base;
1553
0
  bool copy, hash;
1554
0
  bfd_size_type isymesz;
1555
0
  bfd_size_type osymesz;
1556
0
  bfd_size_type linesz;
1557
0
  bfd_byte *esym;
1558
0
  bfd_byte *esym_end;
1559
0
  struct internal_syment *isymp;
1560
0
  asection **secpp;
1561
0
  long *indexp;
1562
0
  unsigned long output_index;
1563
0
  bfd_byte *outsym;
1564
0
  struct coff_link_hash_entry **sym_hash;
1565
0
  asection *o;
1566
1567
  /* Move all the symbols to the output file.  */
1568
1569
0
  output_bfd = flaginfo->output_bfd;
1570
0
  strings = NULL;
1571
0
  syment_base = obj_raw_syment_count (output_bfd);
1572
0
  isymesz = bfd_coff_symesz (input_bfd);
1573
0
  osymesz = bfd_coff_symesz (output_bfd);
1574
0
  linesz = bfd_coff_linesz (input_bfd);
1575
0
  BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1576
1577
0
  copy = false;
1578
0
  if (! flaginfo->info->keep_memory)
1579
0
    copy = true;
1580
0
  hash = true;
1581
0
  if (flaginfo->info->traditional_format)
1582
0
    hash = false;
1583
1584
0
  if (! _bfd_coff_get_external_symbols (input_bfd))
1585
0
    return false;
1586
1587
0
  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1588
0
  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1589
0
  isymp = flaginfo->internal_syms;
1590
0
  secpp = flaginfo->sec_ptrs;
1591
0
  indexp = flaginfo->sym_indices;
1592
0
  output_index = syment_base;
1593
0
  outsym = flaginfo->outsyms;
1594
1595
0
  if (obj_pe (output_bfd)
1596
0
      && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
1597
0
    return false;
1598
1599
  /* If we are going to perform relocations and also strip/discard some
1600
     symbols then we must make sure that we do not strip/discard those
1601
     symbols that are going to be involved in the relocations.  */
1602
0
  if ((   flaginfo->info->strip   != strip_none
1603
0
       || flaginfo->info->discard != discard_none)
1604
0
      && bfd_link_relocatable (flaginfo->info))
1605
0
    {
1606
      /* Mark the symbol array as 'not-used'.  */
1607
0
      memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1608
1609
0
      mark_relocs (flaginfo, input_bfd);
1610
0
    }
1611
1612
0
  while (esym < esym_end)
1613
0
    {
1614
0
      struct internal_syment isym;
1615
0
      enum coff_symbol_classification classification;
1616
0
      bool skip;
1617
0
      bool global;
1618
0
      bool dont_skip_symbol;
1619
0
      int add;
1620
1621
0
      bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1622
1623
      /* Make a copy of *isymp so that the relocate_section function
1624
   always sees the original values.  This is more reliable than
1625
   always recomputing the symbol value even if we are stripping
1626
   the symbol.  */
1627
0
      isym = *isymp;
1628
1629
0
      classification = bfd_coff_classify_symbol (input_bfd, &isym);
1630
0
      switch (classification)
1631
0
  {
1632
0
  default:
1633
0
    abort ();
1634
0
  case COFF_SYMBOL_GLOBAL:
1635
0
  case COFF_SYMBOL_PE_SECTION:
1636
0
  case COFF_SYMBOL_LOCAL:
1637
0
    *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1638
0
    break;
1639
0
  case COFF_SYMBOL_COMMON:
1640
0
    *secpp = bfd_com_section_ptr;
1641
0
    break;
1642
0
  case COFF_SYMBOL_UNDEFINED:
1643
0
    *secpp = bfd_und_section_ptr;
1644
0
    break;
1645
0
  }
1646
1647
      /* Extract the flag indicating if this symbol is used by a
1648
   relocation.  */
1649
0
      if ((flaginfo->info->strip != strip_none
1650
0
     || flaginfo->info->discard != discard_none)
1651
0
    && bfd_link_relocatable (flaginfo->info))
1652
0
  dont_skip_symbol = *indexp;
1653
0
      else
1654
0
  dont_skip_symbol = false;
1655
1656
0
      *indexp = -1;
1657
1658
0
      skip = false;
1659
0
      global = false;
1660
0
      add = 1 + isym.n_numaux;
1661
1662
      /* If we are stripping all symbols, we want to skip this one.  */
1663
0
      if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
1664
0
  skip = true;
1665
1666
0
      if (! skip)
1667
0
  {
1668
0
    switch (classification)
1669
0
      {
1670
0
      default:
1671
0
        abort ();
1672
0
      case COFF_SYMBOL_GLOBAL:
1673
0
      case COFF_SYMBOL_COMMON:
1674
0
      case COFF_SYMBOL_PE_SECTION:
1675
        /* This is a global symbol.  Global symbols come at the
1676
     end of the symbol table, so skip them for now.
1677
     Locally defined function symbols, however, are an
1678
     exception, and are not moved to the end.  */
1679
0
        global = true;
1680
0
        if (! ISFCN (isym.n_type))
1681
0
    skip = true;
1682
0
        break;
1683
1684
0
      case COFF_SYMBOL_UNDEFINED:
1685
        /* Undefined symbols are left for the end.  */
1686
0
        global = true;
1687
0
        skip = true;
1688
0
        break;
1689
1690
0
      case COFF_SYMBOL_LOCAL:
1691
        /* This is a local symbol.  Skip it if we are discarding
1692
     local symbols.  */
1693
0
        if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
1694
0
    skip = true;
1695
0
        break;
1696
0
      }
1697
0
  }
1698
1699
0
#ifndef COFF_WITH_PE
1700
      /* Skip section symbols for sections which are not going to be
1701
   emitted.  */
1702
0
      if (!skip
1703
0
    && !dont_skip_symbol
1704
0
    && isym.n_sclass == C_STAT
1705
0
    && isym.n_type == T_NULL
1706
0
    && isym.n_numaux > 0
1707
0
    && ((*secpp)->output_section == bfd_abs_section_ptr
1708
0
        || bfd_section_removed_from_list (output_bfd,
1709
0
            (*secpp)->output_section)))
1710
0
  skip = true;
1711
0
#endif
1712
1713
      /* If we stripping debugging symbols, and this is a debugging
1714
   symbol, then skip it.  FIXME: gas sets the section to N_ABS
1715
   for some types of debugging symbols; I don't know if this is
1716
   a bug or not.  In any case, we handle it here.  */
1717
0
      if (! skip
1718
0
    && flaginfo->info->strip == strip_debugger
1719
0
    && ! dont_skip_symbol
1720
0
    && (isym.n_scnum == N_DEBUG
1721
0
        || (isym.n_scnum == N_ABS
1722
0
      && (isym.n_sclass == C_AUTO
1723
0
          || isym.n_sclass == C_REG
1724
0
          || isym.n_sclass == C_MOS
1725
0
          || isym.n_sclass == C_MOE
1726
0
          || isym.n_sclass == C_MOU
1727
0
          || isym.n_sclass == C_ARG
1728
0
          || isym.n_sclass == C_REGPARM
1729
0
          || isym.n_sclass == C_FIELD
1730
0
          || isym.n_sclass == C_EOS))))
1731
0
  skip = true;
1732
1733
      /* If some symbols are stripped based on the name, work out the
1734
   name and decide whether to skip this symbol.  */
1735
0
      if (! skip
1736
0
    && (flaginfo->info->strip == strip_some
1737
0
        || flaginfo->info->discard == discard_l))
1738
0
  {
1739
0
    const char *name;
1740
0
    char buf[SYMNMLEN + 1];
1741
1742
0
    name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1743
0
    if (name == NULL)
1744
0
      return false;
1745
1746
0
    if (! dont_skip_symbol
1747
0
        && ((flaginfo->info->strip == strip_some
1748
0
       && (bfd_hash_lookup (flaginfo->info->keep_hash, name, false,
1749
0
            false) == NULL))
1750
0
       || (! global
1751
0
           && flaginfo->info->discard == discard_l
1752
0
           && bfd_is_local_label_name (input_bfd, name))))
1753
0
      skip = true;
1754
0
  }
1755
1756
      /* If this is an enum, struct, or union tag, see if we have
1757
   already output an identical type.  */
1758
0
      if (! skip
1759
0
    && !flaginfo->info->traditional_format
1760
0
    && (isym.n_sclass == C_ENTAG
1761
0
        || isym.n_sclass == C_STRTAG
1762
0
        || isym.n_sclass == C_UNTAG)
1763
0
    && isym.n_numaux == 1)
1764
0
  {
1765
0
    const char *name;
1766
0
    char buf[SYMNMLEN + 1];
1767
0
    struct coff_debug_merge_hash_entry *mh;
1768
0
    struct coff_debug_merge_type *mt;
1769
0
    union internal_auxent aux;
1770
0
    struct coff_debug_merge_element **epp;
1771
0
    bfd_byte *esl, *eslend;
1772
0
    struct internal_syment *islp;
1773
0
    size_t amt;
1774
1775
0
    name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1776
0
    if (name == NULL)
1777
0
      return false;
1778
1779
    /* Ignore fake names invented by compiler; treat them all as
1780
       the same name.  */
1781
0
    if (*name == '~' || *name == '.' || *name == '$'
1782
0
        || (*name
1783
0
      && *name == bfd_get_symbol_leading_char (input_bfd)
1784
0
      && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1785
0
      name = "";
1786
1787
0
    mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
1788
0
               true, true);
1789
0
    if (mh == NULL)
1790
0
      return false;
1791
1792
    /* Allocate memory to hold type information.  If this turns
1793
       out to be a duplicate, we pass this address to
1794
       bfd_release.  */
1795
0
    amt = sizeof (struct coff_debug_merge_type);
1796
0
    mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1797
0
    if (mt == NULL)
1798
0
      return false;
1799
0
    mt->type_class = isym.n_sclass;
1800
1801
    /* Pick up the aux entry, which points to the end of the tag
1802
       entries.  */
1803
0
    bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1804
0
        isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1805
0
        &aux);
1806
1807
    /* Gather the elements.  */
1808
0
    epp = &mt->elements;
1809
0
    mt->elements = NULL;
1810
0
    islp = isymp + 2;
1811
0
    esl = esym + 2 * isymesz;
1812
0
    eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1813
0
        + aux.x_sym.x_fcnary.x_fcn.x_endndx.u32 * isymesz);
1814
0
    while (esl < eslend)
1815
0
      {
1816
0
        const char *elename;
1817
0
        char elebuf[SYMNMLEN + 1];
1818
0
        char *name_copy;
1819
1820
0
        bfd_coff_swap_sym_in (input_bfd, esl, islp);
1821
1822
0
        amt = sizeof (struct coff_debug_merge_element);
1823
0
        *epp = (struct coff_debug_merge_element *)
1824
0
      bfd_alloc (input_bfd, amt);
1825
0
        if (*epp == NULL)
1826
0
    return false;
1827
1828
0
        elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1829
0
              elebuf);
1830
0
        if (elename == NULL)
1831
0
    return false;
1832
1833
0
        amt = strlen (elename) + 1;
1834
0
        name_copy = (char *) bfd_alloc (input_bfd, amt);
1835
0
        if (name_copy == NULL)
1836
0
    return false;
1837
0
        strcpy (name_copy, elename);
1838
1839
0
        (*epp)->name = name_copy;
1840
0
        (*epp)->type = islp->n_type;
1841
0
        (*epp)->tagndx = 0;
1842
0
        if (islp->n_numaux >= 1
1843
0
      && islp->n_type != T_NULL
1844
0
      && islp->n_sclass != C_EOS)
1845
0
    {
1846
0
      union internal_auxent eleaux;
1847
0
      long indx;
1848
1849
0
      bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1850
0
          islp->n_type, islp->n_sclass, 0,
1851
0
          islp->n_numaux, &eleaux);
1852
0
      indx = eleaux.x_sym.x_tagndx.u32;
1853
1854
      /* FIXME: If this tagndx entry refers to a symbol
1855
         defined later in this file, we just ignore it.
1856
         Handling this correctly would be tedious, and may
1857
         not be required.  */
1858
0
      if (indx > 0
1859
0
          && (indx
1860
0
        < ((esym -
1861
0
            (bfd_byte *) obj_coff_external_syms (input_bfd))
1862
0
           / (long) isymesz)))
1863
0
        {
1864
0
          (*epp)->tagndx = flaginfo->sym_indices[indx];
1865
0
          if ((*epp)->tagndx < 0)
1866
0
      (*epp)->tagndx = 0;
1867
0
        }
1868
0
    }
1869
0
        epp = &(*epp)->next;
1870
0
        *epp = NULL;
1871
1872
0
        esl += (islp->n_numaux + 1) * isymesz;
1873
0
        islp += islp->n_numaux + 1;
1874
0
      }
1875
1876
    /* See if we already have a definition which matches this
1877
       type.  We always output the type if it has no elements,
1878
       for simplicity.  */
1879
0
    if (mt->elements == NULL)
1880
0
      bfd_release (input_bfd, mt);
1881
0
    else
1882
0
      {
1883
0
        struct coff_debug_merge_type *mtl;
1884
1885
0
        for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1886
0
    {
1887
0
      struct coff_debug_merge_element *me, *mel;
1888
1889
0
      if (mtl->type_class != mt->type_class)
1890
0
        continue;
1891
1892
0
      for (me = mt->elements, mel = mtl->elements;
1893
0
           me != NULL && mel != NULL;
1894
0
           me = me->next, mel = mel->next)
1895
0
        {
1896
0
          if (strcmp (me->name, mel->name) != 0
1897
0
        || me->type != mel->type
1898
0
        || me->tagndx != mel->tagndx)
1899
0
      break;
1900
0
        }
1901
1902
0
      if (me == NULL && mel == NULL)
1903
0
        break;
1904
0
    }
1905
1906
0
        if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1907
0
    {
1908
      /* This is the first definition of this type.  */
1909
0
      mt->indx = output_index;
1910
0
      mt->next = mh->types;
1911
0
      mh->types = mt;
1912
0
    }
1913
0
        else
1914
0
    {
1915
      /* This is a redefinition which can be merged.  */
1916
0
      bfd_release (input_bfd, mt);
1917
0
      *indexp = mtl->indx;
1918
0
      add = (eslend - esym) / isymesz;
1919
0
      skip = true;
1920
0
    }
1921
0
      }
1922
0
  }
1923
1924
      /* We now know whether we are to skip this symbol or not.  */
1925
0
      if (! skip)
1926
0
  {
1927
    /* Adjust the symbol in order to output it.  */
1928
1929
0
    if (isym._n._n_n._n_zeroes == 0
1930
0
        && isym._n._n_n._n_offset != 0)
1931
0
      {
1932
0
        const char *name;
1933
0
        bfd_size_type indx;
1934
1935
        /* This symbol has a long name.  Enter it in the string
1936
     table we are building.  Note that we do not check
1937
     bfd_coff_symname_in_debug.  That is only true for
1938
     XCOFF, and XCOFF requires different linking code
1939
     anyhow.  */
1940
0
        name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1941
0
        if (name == NULL)
1942
0
    return false;
1943
0
        indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
1944
0
        if (indx == (bfd_size_type) -1)
1945
0
    return false;
1946
0
        isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1947
0
      }
1948
1949
0
    switch (isym.n_sclass)
1950
0
      {
1951
0
      case C_AUTO:
1952
0
      case C_MOS:
1953
0
      case C_EOS:
1954
0
      case C_MOE:
1955
0
      case C_MOU:
1956
0
      case C_UNTAG:
1957
0
      case C_STRTAG:
1958
0
      case C_ENTAG:
1959
0
      case C_TPDEF:
1960
0
      case C_ARG:
1961
0
      case C_USTATIC:
1962
0
      case C_REG:
1963
0
      case C_REGPARM:
1964
0
      case C_FIELD:
1965
        /* The symbol value should not be modified.  */
1966
0
        break;
1967
1968
0
      case C_FCN:
1969
0
        if (obj_pe (input_bfd)
1970
0
      && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
1971
0
      && isym.n_scnum > 0)
1972
0
    {
1973
      /* For PE, .lf and .ef get their value left alone,
1974
         while .bf gets relocated.  However, they all have
1975
         "real" section numbers, and need to be moved into
1976
         the new section.  */
1977
0
      isym.n_scnum = (*secpp)->output_section->target_index;
1978
0
      break;
1979
0
    }
1980
        /* Fall through.  */
1981
0
      default:
1982
0
      case C_LABEL:  /* Not completely sure about these 2 */
1983
0
      case C_EXTDEF:
1984
0
      case C_BLOCK:
1985
0
      case C_EFCN:
1986
0
      case C_NULL:
1987
0
      case C_EXT:
1988
0
      case C_STAT:
1989
0
      case C_SECTION:
1990
0
      case C_NT_WEAK:
1991
        /* Compute new symbol location.  */
1992
0
      if (isym.n_scnum > 0)
1993
0
        {
1994
0
    const asection *s = *secpp;
1995
1996
    /* Relocate section symbols for sections that are not going to
1997
       be emitted because they are duplicate of another one.  */
1998
0
    if (bfd_link_relocatable (flaginfo->info)
1999
0
        && isym.n_sclass == C_STAT
2000
0
        && isym.n_type == T_NULL
2001
0
        && isym.n_numaux > 0
2002
0
        && (s->output_section == bfd_abs_section_ptr
2003
0
      || bfd_section_removed_from_list
2004
0
         (output_bfd, s->output_section))
2005
0
        && s->kept_section)
2006
0
      s = s->kept_section;
2007
2008
0
    isym.n_scnum = s->output_section->target_index;
2009
0
    isym.n_value += s->output_offset;
2010
0
    if (! obj_pe (input_bfd))
2011
0
      isym.n_value -= s->vma;
2012
0
    if (! obj_pe (flaginfo->output_bfd))
2013
0
      isym.n_value += s->output_section->vma;
2014
0
        }
2015
0
      break;
2016
2017
0
      case C_FILE:
2018
        /* The value of a C_FILE symbol is the symbol index of
2019
     the next C_FILE symbol.  The value of the last C_FILE
2020
     symbol is the symbol index to the first external
2021
     symbol (actually, coff_renumber_symbols does not get
2022
     this right--it just sets the value of the last C_FILE
2023
     symbol to zero--and nobody has ever complained about
2024
     it).  We try to get this right, below, just before we
2025
     write the symbols out, but in the general case we may
2026
     have to write the symbol out twice.  */
2027
0
        if (flaginfo->last_file_index != -1
2028
0
      && flaginfo->last_file.n_value != (bfd_vma) output_index)
2029
0
    {
2030
      /* We must correct the value of the last C_FILE
2031
         entry.  */
2032
0
      flaginfo->last_file.n_value = output_index;
2033
0
      if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
2034
0
        {
2035
          /* The last C_FILE symbol is in this input file.  */
2036
0
          bfd_coff_swap_sym_out (output_bfd,
2037
0
               &flaginfo->last_file,
2038
0
               (flaginfo->outsyms
2039
0
                + ((flaginfo->last_file_index
2040
0
              - syment_base)
2041
0
             * osymesz)));
2042
0
        }
2043
0
      else
2044
0
        {
2045
0
          file_ptr pos;
2046
2047
          /* We have already written out the last C_FILE
2048
       symbol.  We need to write it out again.  We
2049
       borrow *outsym temporarily.  */
2050
0
          bfd_coff_swap_sym_out (output_bfd,
2051
0
               &flaginfo->last_file, outsym);
2052
0
          pos = obj_sym_filepos (output_bfd);
2053
0
          pos += flaginfo->last_file_index * osymesz;
2054
0
          if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2055
0
        || bfd_write (outsym, osymesz, output_bfd) != osymesz)
2056
0
      return false;
2057
0
        }
2058
0
    }
2059
2060
0
        flaginfo->last_file_index = output_index;
2061
0
        flaginfo->last_file = isym;
2062
0
        break;
2063
0
      }
2064
2065
    /* If doing task linking, convert normal global function symbols to
2066
       static functions.  */
2067
0
    if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
2068
0
      isym.n_sclass = C_STAT;
2069
2070
    /* Output the symbol.  */
2071
0
    bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
2072
2073
0
    *indexp = output_index;
2074
2075
0
    if (global)
2076
0
      {
2077
0
        long indx;
2078
0
        struct coff_link_hash_entry *h;
2079
2080
0
        indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
2081
0
          / isymesz);
2082
0
        h = obj_coff_sym_hashes (input_bfd)[indx];
2083
0
        if (h == NULL)
2084
0
    {
2085
      /* This can happen if there were errors earlier in
2086
         the link.  */
2087
0
      bfd_set_error (bfd_error_bad_value);
2088
0
      return false;
2089
0
    }
2090
0
        h->indx = output_index;
2091
0
      }
2092
2093
0
    output_index += add;
2094
0
    outsym += add * osymesz;
2095
0
  }
2096
2097
0
      esym += add * isymesz;
2098
0
      isymp += add;
2099
0
      ++secpp;
2100
0
      ++indexp;
2101
0
      for (--add; add > 0; --add)
2102
0
  {
2103
0
    *secpp++ = NULL;
2104
0
    *indexp++ = -1;
2105
0
  }
2106
0
    }
2107
2108
  /* Fix up the aux entries.  This must be done in a separate pass,
2109
     because we don't know the correct symbol indices until we have
2110
     already decided which symbols we are going to keep.  */
2111
0
  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
2112
0
  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
2113
0
  isymp = flaginfo->internal_syms;
2114
0
  indexp = flaginfo->sym_indices;
2115
0
  sym_hash = obj_coff_sym_hashes (input_bfd);
2116
0
  outsym = flaginfo->outsyms;
2117
2118
0
  while (esym < esym_end)
2119
0
    {
2120
0
      int add;
2121
2122
0
      add = 1 + isymp->n_numaux;
2123
2124
0
      if ((*indexp < 0
2125
0
     || (bfd_size_type) *indexp < syment_base)
2126
0
    && (*sym_hash == NULL
2127
0
        || (*sym_hash)->auxbfd != input_bfd))
2128
0
  esym += add * isymesz;
2129
0
      else
2130
0
  {
2131
0
    struct coff_link_hash_entry *h;
2132
0
    int i;
2133
2134
0
    h = NULL;
2135
0
    if (*indexp < 0)
2136
0
      {
2137
0
        h = *sym_hash;
2138
2139
        /* The m68k-motorola-sysv assembler will sometimes
2140
     generate two symbols with the same name, but only one
2141
     will have aux entries.  */
2142
0
        BFD_ASSERT (isymp->n_numaux == 0
2143
0
        || h->numaux == 0
2144
0
        || h->numaux == isymp->n_numaux);
2145
0
      }
2146
2147
0
    esym += isymesz;
2148
2149
0
    if (h == NULL)
2150
0
      outsym += osymesz;
2151
2152
    /* Handle the aux entries.  This handling is based on
2153
       coff_pointerize_aux.  I don't know if it always correct.  */
2154
0
    for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
2155
0
      {
2156
0
        union internal_auxent aux;
2157
0
        union internal_auxent *auxp;
2158
2159
0
        if (h != NULL && h->aux != NULL && (h->numaux > i))
2160
0
    auxp = h->aux + i;
2161
0
        else
2162
0
    {
2163
0
      bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
2164
0
          isymp->n_sclass, i, isymp->n_numaux, &aux);
2165
0
      auxp = &aux;
2166
0
    }
2167
2168
0
        if (isymp->n_sclass == C_FILE)
2169
0
    {
2170
      /* If this is a long filename, we must put it in the
2171
         string table.  */
2172
0
      if (auxp->x_file.x_n.x_n.x_zeroes == 0
2173
0
          && auxp->x_file.x_n.x_n.x_offset != 0)
2174
0
        {
2175
0
          const char *filename;
2176
0
          bfd_size_type indx;
2177
2178
0
          BFD_ASSERT (auxp->x_file.x_n.x_n.x_offset
2179
0
          >= STRING_SIZE_SIZE);
2180
0
          if (strings == NULL)
2181
0
      {
2182
0
        strings = _bfd_coff_read_string_table (input_bfd);
2183
0
        if (strings == NULL)
2184
0
          return false;
2185
0
      }
2186
0
          if ((bfd_size_type) auxp->x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2187
0
      filename = _("<corrupt>");
2188
0
          else
2189
0
      filename = strings + auxp->x_file.x_n.x_n.x_offset;
2190
0
          indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2191
0
             hash, copy);
2192
0
          if (indx == (bfd_size_type) -1)
2193
0
      return false;
2194
0
          auxp->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
2195
0
        }
2196
0
    }
2197
0
        else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2198
0
           && isymp->n_sclass != C_NT_WEAK)
2199
0
    {
2200
0
      unsigned long indx;
2201
2202
0
      if (ISFCN (isymp->n_type)
2203
0
          || ISTAG (isymp->n_sclass)
2204
0
          || isymp->n_sclass == C_BLOCK
2205
0
          || isymp->n_sclass == C_FCN)
2206
0
        {
2207
0
          indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32;
2208
0
          if (indx > 0
2209
0
        && indx < obj_raw_syment_count (input_bfd))
2210
0
      {
2211
        /* We look forward through the symbol for
2212
           the index of the next symbol we are going
2213
           to include.  I don't know if this is
2214
           entirely right.  */
2215
0
        while ((flaginfo->sym_indices[indx] < 0
2216
0
          || ((bfd_size_type) flaginfo->sym_indices[indx]
2217
0
              < syment_base))
2218
0
         && indx < obj_raw_syment_count (input_bfd))
2219
0
          ++indx;
2220
0
        if (indx >= obj_raw_syment_count (input_bfd))
2221
0
          indx = output_index;
2222
0
        else
2223
0
          indx = flaginfo->sym_indices[indx];
2224
0
        auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 = indx;
2225
0
      }
2226
0
        }
2227
2228
0
      indx = auxp->x_sym.x_tagndx.u32;
2229
0
      if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2230
0
        {
2231
0
          long symindx;
2232
2233
0
          symindx = flaginfo->sym_indices[indx];
2234
0
          if (symindx < 0)
2235
0
      auxp->x_sym.x_tagndx.u32 = 0;
2236
0
          else
2237
0
      auxp->x_sym.x_tagndx.u32 = symindx;
2238
0
        }
2239
2240
      /* The .bf symbols are supposed to be linked through
2241
         the endndx field.  We need to carry this list
2242
         across object files.  */
2243
0
      if (i == 0
2244
0
          && h == NULL
2245
0
          && isymp->n_sclass == C_FCN
2246
0
          && (isymp->_n._n_n._n_zeroes != 0
2247
0
        || isymp->_n._n_n._n_offset == 0)
2248
0
          && isymp->_n._n_name[0] == '.'
2249
0
          && isymp->_n._n_name[1] == 'b'
2250
0
          && isymp->_n._n_name[2] == 'f'
2251
0
          && isymp->_n._n_name[3] == '\0')
2252
0
        {
2253
0
          if (flaginfo->last_bf_index != -1)
2254
0
      {
2255
0
        flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
2256
0
          *indexp;
2257
2258
0
        if ((bfd_size_type) flaginfo->last_bf_index
2259
0
            >= syment_base)
2260
0
          {
2261
0
            void *auxout;
2262
2263
            /* The last .bf symbol is in this input
2264
         file.  This will only happen if the
2265
         assembler did not set up the .bf
2266
         endndx symbols correctly.  */
2267
0
            auxout = (flaginfo->outsyms
2268
0
          + ((flaginfo->last_bf_index
2269
0
              - syment_base)
2270
0
             * osymesz));
2271
2272
0
            bfd_coff_swap_aux_out (output_bfd,
2273
0
                 &flaginfo->last_bf,
2274
0
                 isymp->n_type,
2275
0
                 isymp->n_sclass,
2276
0
                 0, isymp->n_numaux,
2277
0
                 auxout);
2278
0
          }
2279
0
        else
2280
0
          {
2281
0
            file_ptr pos;
2282
2283
            /* We have already written out the last
2284
         .bf aux entry.  We need to write it
2285
         out again.  We borrow *outsym
2286
         temporarily.  FIXME: This case should
2287
         be made faster.  */
2288
0
            bfd_coff_swap_aux_out (output_bfd,
2289
0
                 &flaginfo->last_bf,
2290
0
                 isymp->n_type,
2291
0
                 isymp->n_sclass,
2292
0
                 0, isymp->n_numaux,
2293
0
                 outsym);
2294
0
            pos = obj_sym_filepos (output_bfd);
2295
0
            pos += flaginfo->last_bf_index * osymesz;
2296
0
            if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2297
0
          || (bfd_write (outsym, osymesz, output_bfd)
2298
0
              != osymesz))
2299
0
        return false;
2300
0
          }
2301
0
      }
2302
2303
0
          if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 != 0)
2304
0
      flaginfo->last_bf_index = -1;
2305
0
          else
2306
0
      {
2307
        /* The endndx field of this aux entry must
2308
           be updated with the symbol number of the
2309
           next .bf symbol.  */
2310
0
        flaginfo->last_bf = *auxp;
2311
0
        flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2312
0
               / osymesz)
2313
0
              + syment_base);
2314
0
      }
2315
0
        }
2316
0
    }
2317
2318
0
        if (h == NULL)
2319
0
    {
2320
0
      bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2321
0
           isymp->n_sclass, i, isymp->n_numaux,
2322
0
           outsym);
2323
0
      outsym += osymesz;
2324
0
    }
2325
2326
0
        esym += isymesz;
2327
0
      }
2328
0
  }
2329
2330
0
      indexp += add;
2331
0
      isymp += add;
2332
0
      sym_hash += add;
2333
0
    }
2334
2335
  /* Relocate the line numbers, unless we are stripping them.  */
2336
0
  if (flaginfo->info->strip == strip_none
2337
0
      || flaginfo->info->strip == strip_some)
2338
0
    {
2339
0
      for (o = input_bfd->sections; o != NULL; o = o->next)
2340
0
  {
2341
0
    bfd_vma offset;
2342
0
    bfd_byte *eline;
2343
0
    bfd_byte *elineend;
2344
0
    bfd_byte *oeline;
2345
0
    bool skipping;
2346
0
    file_ptr pos;
2347
0
    bfd_size_type amt;
2348
2349
    /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2350
       build_link_order in ldwrite.c will not have created a
2351
       link order, which means that we will not have seen this
2352
       input section in _bfd_coff_final_link, which means that
2353
       we will not have allocated space for the line numbers of
2354
       this section.  I don't think line numbers can be
2355
       meaningful for a section which does not have
2356
       SEC_HAS_CONTENTS set, but, if they do, this must be
2357
       changed.  */
2358
0
    if (o->lineno_count == 0
2359
0
        || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2360
0
      continue;
2361
2362
0
    if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2363
0
        || bfd_read (flaginfo->linenos, linesz * o->lineno_count,
2364
0
         input_bfd) != linesz * o->lineno_count)
2365
0
      return false;
2366
2367
0
    offset = o->output_section->vma + o->output_offset - o->vma;
2368
0
    eline = flaginfo->linenos;
2369
0
    oeline = flaginfo->linenos;
2370
0
    elineend = eline + linesz * o->lineno_count;
2371
0
    skipping = false;
2372
0
    for (; eline < elineend; eline += linesz)
2373
0
      {
2374
0
        struct internal_lineno iline;
2375
2376
0
        bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2377
2378
0
        if (iline.l_lnno != 0)
2379
0
    iline.l_addr.l_paddr += offset;
2380
0
        else if (iline.l_addr.l_symndx >= 0
2381
0
           && ((unsigned long) iline.l_addr.l_symndx
2382
0
         < obj_raw_syment_count (input_bfd)))
2383
0
    {
2384
0
      long indx;
2385
2386
0
      indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2387
2388
0
      if (indx < 0)
2389
0
        {
2390
          /* These line numbers are attached to a symbol
2391
       which we are stripping.  We must discard the
2392
       line numbers because reading them back with
2393
       no associated symbol (or associating them all
2394
       with symbol #0) will fail.  We can't regain
2395
       the space in the output file, but at least
2396
       they're dense.  */
2397
0
          skipping = true;
2398
0
        }
2399
0
      else
2400
0
        {
2401
0
          struct internal_syment is;
2402
0
          union internal_auxent ia;
2403
2404
          /* Fix up the lnnoptr field in the aux entry of
2405
       the symbol.  It turns out that we can't do
2406
       this when we modify the symbol aux entries,
2407
       because gas sometimes screws up the lnnoptr
2408
       field and makes it an offset from the start
2409
       of the line numbers rather than an absolute
2410
       file index.  */
2411
0
          bfd_coff_swap_sym_in (output_bfd,
2412
0
              (flaginfo->outsyms
2413
0
               + ((indx - syment_base)
2414
0
            * osymesz)), &is);
2415
0
          if ((ISFCN (is.n_type)
2416
0
         || is.n_sclass == C_BLOCK)
2417
0
        && is.n_numaux >= 1)
2418
0
      {
2419
0
        void *auxptr;
2420
2421
0
        auxptr = (flaginfo->outsyms
2422
0
            + ((indx - syment_base + 1)
2423
0
               * osymesz));
2424
0
        bfd_coff_swap_aux_in (output_bfd, auxptr,
2425
0
            is.n_type, is.n_sclass,
2426
0
            0, is.n_numaux, &ia);
2427
0
        ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2428
0
          (o->output_section->line_filepos
2429
0
           + o->output_section->lineno_count * linesz
2430
0
           + eline - flaginfo->linenos);
2431
0
        bfd_coff_swap_aux_out (output_bfd, &ia,
2432
0
             is.n_type, is.n_sclass, 0,
2433
0
             is.n_numaux, auxptr);
2434
0
      }
2435
2436
0
          skipping = false;
2437
0
        }
2438
2439
0
      iline.l_addr.l_symndx = indx;
2440
0
    }
2441
2442
0
        if (!skipping)
2443
0
    {
2444
0
      bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2445
0
      oeline += linesz;
2446
0
    }
2447
0
      }
2448
2449
0
    pos = o->output_section->line_filepos;
2450
0
    pos += o->output_section->lineno_count * linesz;
2451
0
    amt = oeline - flaginfo->linenos;
2452
0
    if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2453
0
        || bfd_write (flaginfo->linenos, amt, output_bfd) != amt)
2454
0
      return false;
2455
2456
0
    o->output_section->lineno_count += amt / linesz;
2457
0
  }
2458
0
    }
2459
2460
  /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2461
     symbol will be the first symbol in the next input file.  In the
2462
     normal case, this will save us from writing out the C_FILE symbol
2463
     again.  */
2464
0
  if (flaginfo->last_file_index != -1
2465
0
      && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2466
0
    {
2467
0
      flaginfo->last_file.n_value = output_index;
2468
0
      bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2469
0
           (flaginfo->outsyms
2470
0
            + ((flaginfo->last_file_index - syment_base)
2471
0
         * osymesz)));
2472
0
    }
2473
2474
  /* Write the modified symbols to the output file.  */
2475
0
  if (outsym > flaginfo->outsyms)
2476
0
    {
2477
0
      file_ptr pos;
2478
0
      bfd_size_type amt;
2479
2480
0
      pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2481
0
      amt = outsym - flaginfo->outsyms;
2482
0
      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2483
0
    || bfd_write (flaginfo->outsyms, amt, output_bfd) != amt)
2484
0
  return false;
2485
2486
0
      BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2487
0
       + (outsym - flaginfo->outsyms) / osymesz)
2488
0
      == output_index);
2489
2490
0
      obj_raw_syment_count (output_bfd) = output_index;
2491
0
    }
2492
2493
  /* Relocate the contents of each section.  */
2494
0
  adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2495
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
2496
0
    {
2497
0
      bfd_byte *contents;
2498
0
      struct coff_section_tdata *secdata;
2499
2500
0
      if (! o->linker_mark)
2501
  /* This section was omitted from the link.  */
2502
0
  continue;
2503
2504
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
2505
0
  continue;
2506
2507
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
2508
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2509
0
  {
2510
0
    if ((o->flags & SEC_RELOC) != 0
2511
0
        && o->reloc_count != 0)
2512
0
      {
2513
0
        _bfd_error_handler
2514
    /* xgettext: c-format */
2515
0
    (_("%pB: relocs in section `%pA', but it has no contents"),
2516
0
     input_bfd, o);
2517
0
        bfd_set_error (bfd_error_no_contents);
2518
0
        return false;
2519
0
      }
2520
2521
0
    continue;
2522
0
  }
2523
2524
0
      secdata = coff_section_data (input_bfd, o);
2525
0
      if (secdata != NULL && secdata->contents != NULL)
2526
0
  contents = secdata->contents;
2527
0
      else
2528
0
  {
2529
0
    contents = flaginfo->contents;
2530
0
    if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2531
0
      return false;
2532
0
  }
2533
2534
0
      if ((o->flags & SEC_RELOC) != 0)
2535
0
  {
2536
0
    int target_index;
2537
0
    struct internal_reloc *internal_relocs;
2538
0
    struct internal_reloc *irel;
2539
2540
    /* Read in the relocs.  */
2541
0
    target_index = o->output_section->target_index;
2542
0
    internal_relocs = (bfd_coff_read_internal_relocs
2543
0
           (input_bfd, o, false, flaginfo->external_relocs,
2544
0
            bfd_link_relocatable (flaginfo->info),
2545
0
            (bfd_link_relocatable (flaginfo->info)
2546
0
             ? (flaginfo->section_info[target_index].relocs
2547
0
          + o->output_section->reloc_count)
2548
0
             : flaginfo->internal_relocs)));
2549
0
    if (internal_relocs == NULL
2550
0
        && o->reloc_count > 0)
2551
0
      return false;
2552
2553
    /* Run through the relocs looking for relocs against symbols
2554
       coming from discarded sections and complain about them.  */
2555
0
    irel = internal_relocs;
2556
0
    for (; irel < &internal_relocs[o->reloc_count]; irel++)
2557
0
      {
2558
0
        struct coff_link_hash_entry *h;
2559
0
        asection *ps = NULL;
2560
0
        unsigned long symndx = irel->r_symndx;
2561
0
        if (symndx >= obj_raw_syment_count (input_bfd))
2562
0
    continue;
2563
0
        h = obj_coff_sym_hashes (input_bfd)[symndx];
2564
0
        if (h == NULL)
2565
0
    continue;
2566
0
        while (h->root.type == bfd_link_hash_indirect
2567
0
         || h->root.type == bfd_link_hash_warning)
2568
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
2569
0
        if (h->root.type == bfd_link_hash_defined
2570
0
      || h->root.type == bfd_link_hash_defweak)
2571
0
    ps = h->root.u.def.section;
2572
0
        if (ps == NULL)
2573
0
    continue;
2574
        /* Complain if definition comes from an excluded section.  */
2575
0
        if (ps->flags & SEC_EXCLUDE)
2576
0
    (*flaginfo->info->callbacks->einfo)
2577
      /* xgettext: c-format */
2578
0
      (_("%X`%s' referenced in section `%pA' of %pB: "
2579
0
         "defined in discarded section `%pA' of %pB\n"),
2580
0
       h->root.root.string, o, input_bfd, ps, ps->owner);
2581
0
      }
2582
2583
    /* Call processor specific code to relocate the section
2584
       contents.  */
2585
0
    if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2586
0
             input_bfd, o,
2587
0
             contents,
2588
0
             internal_relocs,
2589
0
             flaginfo->internal_syms,
2590
0
             flaginfo->sec_ptrs))
2591
0
      return false;
2592
2593
0
    if (bfd_link_relocatable (flaginfo->info))
2594
0
      {
2595
0
        bfd_vma offset;
2596
0
        struct internal_reloc *irelend;
2597
0
        struct coff_link_hash_entry **rel_hash;
2598
2599
0
        offset = o->output_section->vma + o->output_offset - o->vma;
2600
0
        irel = internal_relocs;
2601
0
        irelend = irel + o->reloc_count;
2602
0
        rel_hash = (flaginfo->section_info[target_index].rel_hashes
2603
0
        + o->output_section->reloc_count);
2604
0
        for (; irel < irelend; irel++, rel_hash++)
2605
0
    {
2606
0
      struct coff_link_hash_entry *h;
2607
0
      bool adjusted;
2608
2609
0
      *rel_hash = NULL;
2610
2611
      /* Adjust the reloc address and symbol index.  */
2612
0
      irel->r_vaddr += offset;
2613
2614
0
      if ((unsigned long) irel->r_symndx
2615
0
          >= obj_raw_syment_count (input_bfd))
2616
0
        continue;
2617
2618
0
      if (adjust_symndx)
2619
0
        {
2620
0
          if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2621
0
                input_bfd, o, irel,
2622
0
                &adjusted))
2623
0
      return false;
2624
0
          if (adjusted)
2625
0
      continue;
2626
0
        }
2627
2628
0
      h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2629
0
      if (h != NULL)
2630
0
        {
2631
          /* This is a global symbol.  */
2632
0
          if (h->indx >= 0)
2633
0
      irel->r_symndx = h->indx;
2634
0
          else
2635
0
      {
2636
        /* This symbol is being written at the end
2637
           of the file, and we do not yet know the
2638
           symbol index.  We save the pointer to the
2639
           hash table entry in the rel_hash list.
2640
           We set the indx field to -2 to indicate
2641
           that this symbol must not be stripped.  */
2642
0
        *rel_hash = h;
2643
0
        h->indx = -2;
2644
0
      }
2645
0
        }
2646
0
      else
2647
0
        {
2648
0
          long indx;
2649
2650
0
          indx = flaginfo->sym_indices[irel->r_symndx];
2651
0
          if (indx != -1)
2652
0
      irel->r_symndx = indx;
2653
0
          else
2654
0
      {
2655
0
        struct internal_syment *is;
2656
0
        const char *name;
2657
0
        char buf[SYMNMLEN + 1];
2658
2659
        /* This reloc is against a symbol we are
2660
           stripping.  This should have been handled
2661
           by the 'dont_skip_symbol' code in the while
2662
           loop at the top of this function.  */
2663
0
        is = flaginfo->internal_syms + irel->r_symndx;
2664
2665
0
        name = (_bfd_coff_internal_syment_name
2666
0
          (input_bfd, is, buf));
2667
0
        if (name == NULL)
2668
0
          return false;
2669
2670
0
        (*flaginfo->info->callbacks->unattached_reloc)
2671
0
          (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2672
0
      }
2673
0
        }
2674
0
    }
2675
2676
0
        o->output_section->reloc_count += o->reloc_count;
2677
0
      }
2678
0
  }
2679
2680
      /* Write out the modified section contents.  */
2681
0
      if (o->sec_info_type != SEC_INFO_TYPE_STABS)
2682
0
  {
2683
0
    file_ptr loc = (o->output_offset
2684
0
        * bfd_octets_per_byte (output_bfd, o));
2685
0
    if (! bfd_set_section_contents (output_bfd, o->output_section,
2686
0
            contents, loc, o->size))
2687
0
      return false;
2688
0
  }
2689
0
      else
2690
0
  {
2691
0
    if (! (_bfd_write_section_stabs
2692
0
     (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2693
0
      o, contents)))
2694
0
      return false;
2695
0
  }
2696
0
    }
2697
2698
0
  if (! flaginfo->info->keep_memory
2699
0
      && ! _bfd_coff_free_symbols (input_bfd))
2700
0
    return false;
2701
2702
0
  return true;
2703
0
}
2704
2705
/* Write out a global symbol.  Called via bfd_hash_traverse.  */
2706
2707
bool
2708
_bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2709
0
{
2710
0
  struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2711
0
  struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2712
0
  bfd *output_bfd;
2713
0
  struct internal_syment isym;
2714
0
  bfd_size_type symesz;
2715
0
  unsigned int i;
2716
0
  file_ptr pos;
2717
2718
0
  output_bfd = flaginfo->output_bfd;
2719
2720
0
  if (h->root.type == bfd_link_hash_warning)
2721
0
    {
2722
0
      h = (struct coff_link_hash_entry *) h->root.u.i.link;
2723
0
      if (h->root.type == bfd_link_hash_new)
2724
0
  return true;
2725
0
    }
2726
2727
0
  if (h->indx >= 0)
2728
0
    return true;
2729
2730
0
  if (h->indx != -2
2731
0
      && (flaginfo->info->strip == strip_all
2732
0
    || (flaginfo->info->strip == strip_some
2733
0
        && (bfd_hash_lookup (flaginfo->info->keep_hash,
2734
0
           h->root.root.string, false, false)
2735
0
      == NULL))))
2736
0
    return true;
2737
2738
0
  switch (h->root.type)
2739
0
    {
2740
0
    default:
2741
0
    case bfd_link_hash_new:
2742
0
    case bfd_link_hash_warning:
2743
0
      abort ();
2744
0
      return false;
2745
2746
0
    case bfd_link_hash_undefined:
2747
0
      if (h->indx == -3)
2748
0
  return true;
2749
      /* Fall through.  */
2750
0
    case bfd_link_hash_undefweak:
2751
0
      isym.n_scnum = N_UNDEF;
2752
0
      isym.n_value = 0;
2753
0
      break;
2754
2755
0
    case bfd_link_hash_defined:
2756
0
    case bfd_link_hash_defweak:
2757
0
      {
2758
0
  asection *sec;
2759
2760
0
  sec = h->root.u.def.section;
2761
0
  isym.n_value = h->root.u.def.value;
2762
0
  if (sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2763
0
    isym.n_value =
2764
0
      _bfd_merged_section_offset (output_bfd, &sec, isym.n_value);
2765
0
  isym.n_value += sec->output_offset;
2766
2767
0
  sec = sec->output_section;
2768
0
  if (bfd_is_abs_section (sec))
2769
0
    isym.n_scnum = N_ABS;
2770
0
  else
2771
0
    isym.n_scnum = sec->target_index;
2772
0
  if (! obj_pe (flaginfo->output_bfd))
2773
0
    isym.n_value += sec->vma;
2774
0
#ifdef BFD64
2775
0
  if (isym.n_value > (bfd_vma) 0xffffffff)
2776
0
    {
2777
0
      if (! h->root.linker_def)
2778
0
        _bfd_error_handler
2779
0
    (_("%pB: stripping non-representable symbol '%s' "
2780
0
       "(value 0x%" PRIx64 ")"),
2781
0
     output_bfd, h->root.root.string, isym.n_value);
2782
0
      return true;
2783
0
    }
2784
0
#endif
2785
0
      }
2786
0
      break;
2787
2788
0
    case bfd_link_hash_common:
2789
0
      isym.n_scnum = N_UNDEF;
2790
0
      isym.n_value = h->root.u.c.size;
2791
0
      break;
2792
2793
0
    case bfd_link_hash_indirect:
2794
      /* Just ignore these.  They can't be handled anyhow.  */
2795
0
      return true;
2796
0
    }
2797
2798
0
  if (strlen (h->root.root.string) <= SYMNMLEN)
2799
0
    strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2800
0
  else
2801
0
    {
2802
0
      bool hash;
2803
0
      bfd_size_type indx;
2804
2805
0
      hash = true;
2806
0
      if (flaginfo->info->traditional_format)
2807
0
  hash = false;
2808
0
      indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2809
0
         false);
2810
0
      if (indx == (bfd_size_type) -1)
2811
0
  {
2812
0
    flaginfo->failed = true;
2813
0
    return false;
2814
0
  }
2815
0
      isym._n._n_n._n_zeroes = 0;
2816
0
      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2817
0
    }
2818
2819
0
  isym.n_sclass = h->symbol_class;
2820
0
  isym.n_type = h->type;
2821
2822
0
  if (isym.n_sclass == C_NULL)
2823
0
    isym.n_sclass = C_EXT;
2824
2825
  /* If doing task linking and this is the pass where we convert
2826
     defined globals to statics, then do that conversion now.  If the
2827
     symbol is not being converted, just ignore it and it will be
2828
     output during a later pass.  */
2829
0
  if (flaginfo->global_to_static)
2830
0
    {
2831
0
      if (! IS_EXTERNAL (output_bfd, isym))
2832
0
  return true;
2833
2834
0
      isym.n_sclass = C_STAT;
2835
0
    }
2836
2837
  /* When a weak symbol is not overridden by a strong one,
2838
     turn it into an external symbol when not building a
2839
     shared or relocatable object.  */
2840
0
  if (! bfd_link_pic (flaginfo->info)
2841
0
      && ! bfd_link_relocatable (flaginfo->info)
2842
0
      && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2843
0
    isym.n_sclass = C_EXT;
2844
2845
0
  isym.n_numaux = h->numaux;
2846
2847
0
  bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2848
2849
0
  symesz = bfd_coff_symesz (output_bfd);
2850
2851
0
  pos = obj_sym_filepos (output_bfd);
2852
0
  pos += obj_raw_syment_count (output_bfd) * symesz;
2853
0
  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2854
0
      || bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2855
0
    {
2856
0
      flaginfo->failed = true;
2857
0
      return false;
2858
0
    }
2859
2860
0
  h->indx = obj_raw_syment_count (output_bfd);
2861
2862
0
  ++obj_raw_syment_count (output_bfd);
2863
2864
  /* Write out any associated aux entries.  Most of the aux entries
2865
     will have been modified in _bfd_coff_link_input_bfd.  We have to
2866
     handle section aux entries here, now that we have the final
2867
     relocation and line number counts.  */
2868
0
  for (i = 0; i < isym.n_numaux; i++)
2869
0
    {
2870
0
      union internal_auxent *auxp;
2871
2872
0
      auxp = h->aux + i;
2873
2874
      /* Look for a section aux entry here using the same tests that
2875
   coff_swap_aux_out uses.  */
2876
0
      if (i == 0
2877
0
    && (isym.n_sclass == C_STAT
2878
0
        || isym.n_sclass == C_HIDDEN)
2879
0
    && isym.n_type == T_NULL
2880
0
    && (h->root.type == bfd_link_hash_defined
2881
0
        || h->root.type == bfd_link_hash_defweak))
2882
0
  {
2883
0
    asection *sec;
2884
2885
0
    sec = h->root.u.def.section->output_section;
2886
0
    if (sec != NULL)
2887
0
      {
2888
0
        auxp->x_scn.x_scnlen = sec->size;
2889
2890
        /* For PE, an overflow on the final link reportedly does
2891
     not matter.  FIXME: Why not?  */
2892
0
        if (sec->reloc_count > 0xffff
2893
0
      && (! obj_pe (output_bfd)
2894
0
          || bfd_link_relocatable (flaginfo->info)))
2895
0
    _bfd_error_handler
2896
      /* xgettext: c-format */
2897
0
      (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2898
0
       output_bfd, sec, sec->reloc_count);
2899
2900
0
        if (sec->lineno_count > 0xffff
2901
0
      && (! obj_pe (output_bfd)
2902
0
          || bfd_link_relocatable (flaginfo->info)))
2903
0
    _bfd_error_handler
2904
      /* xgettext: c-format */
2905
0
      (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2906
0
       output_bfd, sec, sec->lineno_count);
2907
2908
0
        auxp->x_scn.x_nreloc = sec->reloc_count;
2909
0
        auxp->x_scn.x_nlinno = sec->lineno_count;
2910
0
        auxp->x_scn.x_checksum = 0;
2911
0
        auxp->x_scn.x_associated = 0;
2912
0
        auxp->x_scn.x_comdat = 0;
2913
0
      }
2914
0
  }
2915
2916
0
      bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2917
0
           isym.n_sclass, (int) i, isym.n_numaux,
2918
0
           flaginfo->outsyms);
2919
0
      if (bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2920
0
  {
2921
0
    flaginfo->failed = true;
2922
0
    return false;
2923
0
  }
2924
0
      ++obj_raw_syment_count (output_bfd);
2925
0
    }
2926
2927
0
  return true;
2928
0
}
2929
2930
/* Write out task global symbols, converting them to statics.  Called
2931
   via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
2932
   the dirty work, if the symbol we are processing needs conversion.  */
2933
2934
bool
2935
_bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2936
0
{
2937
0
  struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2938
0
  bool rtnval = true;
2939
0
  bool save_global_to_static;
2940
2941
0
  if (h->root.type == bfd_link_hash_warning)
2942
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
2943
2944
0
  if (h->indx < 0)
2945
0
    {
2946
0
      switch (h->root.type)
2947
0
  {
2948
0
  case bfd_link_hash_defined:
2949
0
  case bfd_link_hash_defweak:
2950
0
    save_global_to_static = flaginfo->global_to_static;
2951
0
    flaginfo->global_to_static = true;
2952
0
    rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2953
0
    flaginfo->global_to_static = save_global_to_static;
2954
0
    break;
2955
0
  default:
2956
0
    break;
2957
0
  }
2958
0
    }
2959
0
  return (rtnval);
2960
0
}
2961
2962
/* Handle a link order which is supposed to generate a reloc.  */
2963
2964
bool
2965
_bfd_coff_reloc_link_order (bfd *output_bfd,
2966
          struct coff_final_link_info *flaginfo,
2967
          asection *output_section,
2968
          struct bfd_link_order *link_order)
2969
0
{
2970
0
  reloc_howto_type *howto;
2971
0
  struct internal_reloc *irel;
2972
0
  struct coff_link_hash_entry **rel_hash_ptr;
2973
2974
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2975
0
  if (howto == NULL)
2976
0
    {
2977
0
      bfd_set_error (bfd_error_bad_value);
2978
0
      return false;
2979
0
    }
2980
2981
0
  if (link_order->u.reloc.p->addend != 0)
2982
0
    {
2983
0
      bfd_size_type size;
2984
0
      bfd_byte *buf;
2985
0
      bfd_reloc_status_type rstat;
2986
0
      bool ok;
2987
0
      file_ptr loc;
2988
2989
0
      size = bfd_get_reloc_size (howto);
2990
0
      buf = (bfd_byte *) bfd_zmalloc (size);
2991
0
      if (buf == NULL && size != 0)
2992
0
  return false;
2993
2994
0
      rstat = _bfd_relocate_contents (howto, output_bfd,
2995
0
              (bfd_vma) link_order->u.reloc.p->addend,
2996
0
              buf);
2997
0
      switch (rstat)
2998
0
  {
2999
0
  case bfd_reloc_ok:
3000
0
    break;
3001
0
  default:
3002
0
  case bfd_reloc_outofrange:
3003
0
    abort ();
3004
0
  case bfd_reloc_overflow:
3005
0
    (*flaginfo->info->callbacks->reloc_overflow)
3006
0
      (flaginfo->info, NULL,
3007
0
       (link_order->type == bfd_section_reloc_link_order
3008
0
        ? bfd_section_name (link_order->u.reloc.p->u.section)
3009
0
        : link_order->u.reloc.p->u.name),
3010
0
       howto->name, link_order->u.reloc.p->addend,
3011
0
       (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
3012
0
    break;
3013
0
  }
3014
0
      loc = link_order->offset * bfd_octets_per_byte (output_bfd,
3015
0
                  output_section);
3016
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
3017
0
             loc, size);
3018
0
      free (buf);
3019
0
      if (! ok)
3020
0
  return false;
3021
0
    }
3022
3023
  /* Store the reloc information in the right place.  It will get
3024
     swapped and written out at the end of the final_link routine.  */
3025
0
  irel = (flaginfo->section_info[output_section->target_index].relocs
3026
0
    + output_section->reloc_count);
3027
0
  rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
3028
0
      + output_section->reloc_count);
3029
3030
0
  memset (irel, 0, sizeof (struct internal_reloc));
3031
0
  *rel_hash_ptr = NULL;
3032
3033
0
  irel->r_vaddr = output_section->vma + link_order->offset;
3034
3035
0
  if (link_order->type == bfd_section_reloc_link_order)
3036
0
    {
3037
      /* We need to somehow locate a symbol in the right section.  The
3038
   symbol must either have a value of zero, or we must adjust
3039
   the addend by the value of the symbol.  FIXME: Write this
3040
   when we need it.  The old linker couldn't handle this anyhow.  */
3041
0
      abort ();
3042
0
      *rel_hash_ptr = NULL;
3043
0
      irel->r_symndx = 0;
3044
0
    }
3045
0
  else
3046
0
    {
3047
0
      struct coff_link_hash_entry *h;
3048
3049
0
      h = ((struct coff_link_hash_entry *)
3050
0
     bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
3051
0
           link_order->u.reloc.p->u.name,
3052
0
           false, false, true));
3053
0
      if (h != NULL)
3054
0
  {
3055
0
    if (h->indx >= 0)
3056
0
      irel->r_symndx = h->indx;
3057
0
    else
3058
0
      {
3059
        /* Set the index to -2 to force this symbol to get
3060
     written out.  */
3061
0
        h->indx = -2;
3062
0
        *rel_hash_ptr = h;
3063
0
        irel->r_symndx = 0;
3064
0
      }
3065
0
  }
3066
0
      else
3067
0
  {
3068
0
    (*flaginfo->info->callbacks->unattached_reloc)
3069
0
      (flaginfo->info, link_order->u.reloc.p->u.name,
3070
0
       (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
3071
0
    irel->r_symndx = 0;
3072
0
  }
3073
0
    }
3074
3075
  /* FIXME: Is this always right?  */
3076
0
  irel->r_type = howto->type;
3077
3078
  /* r_size is only used on the RS/6000, which needs its own linker
3079
     routines anyhow.  r_extern is only used for ECOFF.  */
3080
3081
  /* FIXME: What is the right value for r_offset?  Is zero OK?  */
3082
0
  ++output_section->reloc_count;
3083
3084
0
  return true;
3085
0
}
3086
3087
/* A basic reloc handling routine which may be used by processors with
3088
   simple relocs.  */
3089
3090
bool
3091
_bfd_coff_generic_relocate_section (bfd *output_bfd,
3092
            struct bfd_link_info *info,
3093
            bfd *input_bfd,
3094
            asection *input_section,
3095
            bfd_byte *contents,
3096
            struct internal_reloc *relocs,
3097
            struct internal_syment *syms,
3098
            asection **sections)
3099
0
{
3100
0
  struct internal_reloc *rel;
3101
0
  struct internal_reloc *relend;
3102
3103
0
  rel = relocs;
3104
0
  relend = rel + input_section->reloc_count;
3105
0
  for (; rel < relend; rel++)
3106
0
    {
3107
0
      long symndx;
3108
0
      struct coff_link_hash_entry *h;
3109
0
      struct internal_syment *sym;
3110
0
      bfd_vma addend;
3111
0
      bfd_vma val;
3112
0
      asection *sec;
3113
0
      reloc_howto_type *howto;
3114
0
      bfd_reloc_status_type rstat;
3115
3116
0
      symndx = rel->r_symndx;
3117
3118
0
      if (symndx == -1)
3119
0
  {
3120
0
    h = NULL;
3121
0
    sym = NULL;
3122
0
  }
3123
0
      else if ((unsigned long) symndx >= obj_raw_syment_count (input_bfd))
3124
0
  {
3125
0
    _bfd_error_handler
3126
      /* xgettext: c-format */
3127
0
      (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
3128
0
    return false;
3129
0
  }
3130
0
      else
3131
0
  {
3132
0
    h = obj_coff_sym_hashes (input_bfd)[symndx];
3133
0
    sym = syms + symndx;
3134
0
  }
3135
3136
      /* COFF treats common symbols in one of two ways.  Either the
3137
   size of the symbol is included in the section contents, or it
3138
   is not.  We assume that the size is not included, and force
3139
   the rtype_to_howto function to adjust the addend as needed.  */
3140
0
      if (sym != NULL && sym->n_scnum != 0)
3141
0
  addend = - sym->n_value;
3142
0
      else
3143
0
  addend = 0;
3144
3145
0
      howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
3146
0
               sym, &addend);
3147
0
      if (howto == NULL)
3148
0
  return false;
3149
3150
      /* If we are doing a relocatable link, then we can just ignore
3151
   a PC relative reloc that is pcrel_offset.  It will already
3152
   have the correct value.  If this is not a relocatable link,
3153
   then we should ignore the symbol value.  */
3154
0
      if (howto->pc_relative && howto->pcrel_offset)
3155
0
  {
3156
0
    if (bfd_link_relocatable (info))
3157
0
      continue;
3158
0
    if (sym != NULL && sym->n_scnum != 0)
3159
0
      addend += sym->n_value;
3160
0
  }
3161
3162
0
      val = 0;
3163
0
      sec = NULL;
3164
0
      if (h == NULL)
3165
0
  {
3166
0
    if (symndx == -1)
3167
0
      {
3168
0
        sec = bfd_abs_section_ptr;
3169
0
        val = 0;
3170
0
      }
3171
0
    else
3172
0
      {
3173
0
        sec = sections[symndx];
3174
3175
        /* PR 19623: Relocations against symbols in
3176
     the absolute sections should ignored.
3177
     PR 29807: Also ignore relocs against file symbols or
3178
     other such nonsense in fuzzed objects.  */
3179
0
        if (sec == NULL || bfd_is_abs_section (sec))
3180
0
    continue;
3181
3182
0
        val = (sec->output_section->vma
3183
0
         + sec->output_offset
3184
0
         + sym->n_value);
3185
0
        if (! obj_pe (input_bfd))
3186
0
    val -= sec->vma;
3187
0
      }
3188
0
  }
3189
0
      else
3190
0
  {
3191
0
    if (h->root.type == bfd_link_hash_defined
3192
        /* Defined weak symbols are a GNU extension.  */
3193
0
        || h->root.type == bfd_link_hash_defweak)
3194
0
      {
3195
0
        sec = h->root.u.def.section;
3196
0
        BFD_ASSERT (sec->output_section != NULL);
3197
0
        val = (h->root.u.def.value
3198
0
         + sec->output_section->vma
3199
0
         + sec->output_offset);
3200
0
      }
3201
3202
0
    else if (h->root.type == bfd_link_hash_undefweak
3203
0
       || (h->root.type == bfd_link_hash_undefined
3204
0
           && h->symbol_class == C_NT_WEAK && h->numaux == 1))
3205
0
      {
3206
        /* Weak undefined symbol: either GNU weak (no aux record) or
3207
     PE COFF weak external (C_NT_WEAK with aux record).
3208
     Also handles strong undefined symbols that carry PE weak
3209
     external metadata (when strong undef is seen before weak def,
3210
     the hash type stays bfd_link_hash_undefined but we preserve
3211
     the weak external class and aux for later resolution).  */
3212
3213
0
        bool is_pe_weak = (h->symbol_class == C_NT_WEAK && h->numaux == 1);
3214
3215
0
        if (is_pe_weak)
3216
0
    {
3217
      /* PE COFF weak external: resolve via fallback alias.
3218
         See _Microsoft Portable Executable and Common Object
3219
         File Format Specification_, section 5.5.3.
3220
         FIXME: All weak externals are treated as having
3221
         characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3222
         These behave as per SVR4 ABI:  A library member
3223
         will resolve a weak external only if a normal
3224
         external causes the library member to be linked.
3225
         See also linker.c: generic_link_check_archive_element. */
3226
0
      struct coff_link_hash_entry *h2 = NULL;
3227
0
      unsigned long symndx2 = h->aux->x_sym.x_tagndx.u32;
3228
3229
0
      if (symndx2 < obj_raw_syment_count (h->auxbfd))
3230
0
        h2 = obj_coff_sym_hashes (h->auxbfd)[symndx2];
3231
3232
0
      if (!h2 || h2->root.type == bfd_link_hash_undefined)
3233
0
        {
3234
          /* Fallback alias not found or still undefined.
3235
       Resolve to NULL.  */
3236
0
          sec = bfd_abs_section_ptr;
3237
0
          val = 0;
3238
0
        }
3239
0
      else
3240
0
        {
3241
          /* Use fallback alias target.  */
3242
0
          sec = h2->root.u.def.section;
3243
0
          val = h2->root.u.def.value
3244
0
      + sec->output_section->vma + sec->output_offset;
3245
0
        }
3246
0
    }
3247
0
        else
3248
0
    {
3249
      /* GNU extension: ELF-style weak symbol in COFF without
3250
         PE weak external aux record.  COFF has no native support
3251
         for weak symbols (unlike ELF where they're part of the
3252
         format).  PE COFF adds them via C_NT_WEAK storage class
3253
         with an aux record pointing to a fallback symbol.  GNU ld
3254
         extends this by allowing __attribute__((weak)) in COFF
3255
         objects even without the PE aux structure, treating them
3256
         like ELF weak symbols: resolve to NULL if not defined.  */
3257
0
      val = 0;
3258
0
    }
3259
0
      }
3260
3261
0
    else if (! bfd_link_relocatable (info))
3262
0
      {
3263
0
        (*info->callbacks->undefined_symbol)
3264
0
    (info, h->root.root.string, input_bfd, input_section,
3265
0
     rel->r_vaddr - input_section->vma, true);
3266
        /* Stop the linker from issueing errors about truncated relocs
3267
     referencing this undefined symbol by giving it an address
3268
     that should be in range.  */
3269
0
        val = input_section->output_section->vma;
3270
0
      }
3271
0
  }
3272
3273
      /* If the input section defining the symbol has been discarded
3274
   then zero this reloc field.  */
3275
0
      if (sec != NULL && discarded_section (sec))
3276
0
  {
3277
0
    _bfd_clear_contents (howto, input_bfd, input_section,
3278
0
             contents, rel->r_vaddr - input_section->vma);
3279
0
    continue;
3280
0
  }
3281
3282
0
      if (info->base_file)
3283
0
  {
3284
    /* Emit a reloc if the backend thinks it needs it.  */
3285
0
    if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3286
0
      {
3287
        /* Relocation to a symbol in a section which isn't
3288
     absolute.  We output the address here to a file.
3289
     This file is then read by dlltool when generating the
3290
     reloc section.  Note that the base file is not
3291
     portable between systems.  We write out a bfd_vma here,
3292
     and dlltool reads in a bfd_vma.  */
3293
0
        bfd_vma addr = (rel->r_vaddr
3294
0
         - input_section->vma
3295
0
         + input_section->output_offset
3296
0
         + input_section->output_section->vma);
3297
0
        if (obj_pe (output_bfd))
3298
0
    addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3299
0
        if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3300
0
      != sizeof (bfd_vma))
3301
0
    {
3302
0
      bfd_set_error (bfd_error_system_call);
3303
0
      return false;
3304
0
    }
3305
0
      }
3306
0
  }
3307
3308
0
      rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3309
0
          contents,
3310
0
          rel->r_vaddr - input_section->vma,
3311
0
          val, addend);
3312
3313
0
      switch (rstat)
3314
0
  {
3315
0
  default:
3316
0
    abort ();
3317
0
  case bfd_reloc_ok:
3318
0
    break;
3319
0
  case bfd_reloc_outofrange:
3320
0
    _bfd_error_handler
3321
      /* xgettext: c-format */
3322
0
      (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3323
0
       input_bfd, (uint64_t) rel->r_vaddr, input_section);
3324
0
    return false;
3325
0
  case bfd_reloc_overflow:
3326
0
    {
3327
      /* Ignore any weak undef symbols that may have overflowed.  Due to
3328
         PR ld/19011 the base address is now in the upper 64-bit address
3329
         range.  This means that when _bfd_final_link_relocate calculates
3330
         the overlow it takes the distance between the symbol and the VMA
3331
         which will now always overflow as 0 - 64-bit addr > 32-bit range
3332
         of the relocation.  This ends up creating PR ld/26659.  */
3333
0
      if (val == 0
3334
    /* Reverse the hack where 4 is subtracted from the addend.  */
3335
0
    && (addend + 4) == 0
3336
0
    && sym->n_sclass == C_NT_WEAK
3337
0
    && bfd_coff_classify_symbol (output_bfd, sym)
3338
0
         == COFF_SYMBOL_UNDEFINED)
3339
0
        break;
3340
3341
0
      const char *name;
3342
0
      char buf[SYMNMLEN + 1];
3343
3344
0
      if (symndx == -1)
3345
0
        name = "*ABS*";
3346
0
      else if (h != NULL)
3347
0
        name = NULL;
3348
0
      else
3349
0
        {
3350
0
    name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3351
0
    if (name == NULL)
3352
0
      return false;
3353
0
        }
3354
3355
0
      (*info->callbacks->reloc_overflow)
3356
0
        (info, (h ? &h->root : NULL), name, howto->name,
3357
0
         (bfd_vma) 0, input_bfd, input_section,
3358
0
         rel->r_vaddr - input_section->vma);
3359
0
    }
3360
0
  }
3361
0
    }
3362
3363
0
  return true;
3364
0
}