Coverage Report

Created: 2026-03-10 08:46

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