Coverage Report

Created: 2026-11-21 09:00

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