Coverage Report

Created: 2025-06-24 06:45

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