Coverage Report

Created: 2026-04-04 08:16

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
    const asection *s = *secpp;
1861
1862
    /* Relocate section symbols for sections that are not going to
1863
       be emitted because they are duplicate of another one.  */
1864
0
    if (bfd_link_relocatable (flaginfo->info)
1865
0
        && isym.n_sclass == C_STAT
1866
0
        && isym.n_type == T_NULL
1867
0
        && isym.n_numaux > 0
1868
0
        && (s->output_section == bfd_abs_section_ptr
1869
0
      || bfd_section_removed_from_list
1870
0
         (output_bfd, s->output_section))
1871
0
        && s->kept_section)
1872
0
      s = s->kept_section;
1873
1874
0
    isym.n_scnum = s->output_section->target_index;
1875
0
    isym.n_value += s->output_offset;
1876
0
    if (! obj_pe (input_bfd))
1877
0
      isym.n_value -= s->vma;
1878
0
    if (! obj_pe (flaginfo->output_bfd))
1879
0
      isym.n_value += s->output_section->vma;
1880
0
        }
1881
0
      break;
1882
1883
0
      case C_FILE:
1884
        /* The value of a C_FILE symbol is the symbol index of
1885
     the next C_FILE symbol.  The value of the last C_FILE
1886
     symbol is the symbol index to the first external
1887
     symbol (actually, coff_renumber_symbols does not get
1888
     this right--it just sets the value of the last C_FILE
1889
     symbol to zero--and nobody has ever complained about
1890
     it).  We try to get this right, below, just before we
1891
     write the symbols out, but in the general case we may
1892
     have to write the symbol out twice.  */
1893
0
        if (flaginfo->last_file_index != -1
1894
0
      && flaginfo->last_file.n_value != (bfd_vma) output_index)
1895
0
    {
1896
      /* We must correct the value of the last C_FILE
1897
         entry.  */
1898
0
      flaginfo->last_file.n_value = output_index;
1899
0
      if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
1900
0
        {
1901
          /* The last C_FILE symbol is in this input file.  */
1902
0
          bfd_coff_swap_sym_out (output_bfd,
1903
0
               &flaginfo->last_file,
1904
0
               (flaginfo->outsyms
1905
0
                + ((flaginfo->last_file_index
1906
0
              - syment_base)
1907
0
             * osymesz)));
1908
0
        }
1909
0
      else
1910
0
        {
1911
0
          file_ptr pos;
1912
1913
          /* We have already written out the last C_FILE
1914
       symbol.  We need to write it out again.  We
1915
       borrow *outsym temporarily.  */
1916
0
          bfd_coff_swap_sym_out (output_bfd,
1917
0
               &flaginfo->last_file, outsym);
1918
0
          pos = obj_sym_filepos (output_bfd);
1919
0
          pos += flaginfo->last_file_index * osymesz;
1920
0
          if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1921
0
        || bfd_write (outsym, osymesz, output_bfd) != osymesz)
1922
0
      return false;
1923
0
        }
1924
0
    }
1925
1926
0
        flaginfo->last_file_index = output_index;
1927
0
        flaginfo->last_file = isym;
1928
0
        break;
1929
0
      }
1930
1931
    /* If doing task linking, convert normal global function symbols to
1932
       static functions.  */
1933
0
    if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1934
0
      isym.n_sclass = C_STAT;
1935
1936
    /* Output the symbol.  */
1937
0
    bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1938
1939
0
    *indexp = output_index;
1940
1941
0
    if (global)
1942
0
      {
1943
0
        long indx;
1944
0
        struct coff_link_hash_entry *h;
1945
1946
0
        indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1947
0
          / isymesz);
1948
0
        h = obj_coff_sym_hashes (input_bfd)[indx];
1949
0
        if (h == NULL)
1950
0
    {
1951
      /* This can happen if there were errors earlier in
1952
         the link.  */
1953
0
      bfd_set_error (bfd_error_bad_value);
1954
0
      return false;
1955
0
    }
1956
0
        h->indx = output_index;
1957
0
      }
1958
1959
0
    output_index += add;
1960
0
    outsym += add * osymesz;
1961
0
  }
1962
1963
0
      esym += add * isymesz;
1964
0
      isymp += add;
1965
0
      ++secpp;
1966
0
      ++indexp;
1967
0
      for (--add; add > 0; --add)
1968
0
  {
1969
0
    *secpp++ = NULL;
1970
0
    *indexp++ = -1;
1971
0
  }
1972
0
    }
1973
1974
  /* Fix up the aux entries.  This must be done in a separate pass,
1975
     because we don't know the correct symbol indices until we have
1976
     already decided which symbols we are going to keep.  */
1977
0
  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1978
0
  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1979
0
  isymp = flaginfo->internal_syms;
1980
0
  indexp = flaginfo->sym_indices;
1981
0
  sym_hash = obj_coff_sym_hashes (input_bfd);
1982
0
  outsym = flaginfo->outsyms;
1983
1984
0
  while (esym < esym_end)
1985
0
    {
1986
0
      int add;
1987
1988
0
      add = 1 + isymp->n_numaux;
1989
1990
0
      if ((*indexp < 0
1991
0
     || (bfd_size_type) *indexp < syment_base)
1992
0
    && (*sym_hash == NULL
1993
0
        || (*sym_hash)->auxbfd != input_bfd))
1994
0
  esym += add * isymesz;
1995
0
      else
1996
0
  {
1997
0
    struct coff_link_hash_entry *h;
1998
0
    int i;
1999
2000
0
    h = NULL;
2001
0
    if (*indexp < 0)
2002
0
      {
2003
0
        h = *sym_hash;
2004
2005
        /* The m68k-motorola-sysv assembler will sometimes
2006
     generate two symbols with the same name, but only one
2007
     will have aux entries.  */
2008
0
        BFD_ASSERT (isymp->n_numaux == 0
2009
0
        || h->numaux == 0
2010
0
        || h->numaux == isymp->n_numaux);
2011
0
      }
2012
2013
0
    esym += isymesz;
2014
2015
0
    if (h == NULL)
2016
0
      outsym += osymesz;
2017
2018
    /* Handle the aux entries.  This handling is based on
2019
       coff_pointerize_aux.  I don't know if it always correct.  */
2020
0
    for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
2021
0
      {
2022
0
        union internal_auxent aux;
2023
0
        union internal_auxent *auxp;
2024
2025
0
        if (h != NULL && h->aux != NULL && (h->numaux > i))
2026
0
    auxp = h->aux + i;
2027
0
        else
2028
0
    {
2029
0
      bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
2030
0
          isymp->n_sclass, i, isymp->n_numaux, &aux);
2031
0
      auxp = &aux;
2032
0
    }
2033
2034
0
        if (isymp->n_sclass == C_FILE)
2035
0
    {
2036
      /* If this is a long filename, we must put it in the
2037
         string table.  */
2038
0
      if (auxp->x_file.x_n.x_n.x_zeroes == 0
2039
0
          && auxp->x_file.x_n.x_n.x_offset != 0)
2040
0
        {
2041
0
          const char *filename;
2042
0
          bfd_size_type indx;
2043
2044
0
          BFD_ASSERT (auxp->x_file.x_n.x_n.x_offset
2045
0
          >= STRING_SIZE_SIZE);
2046
0
          if (strings == NULL)
2047
0
      {
2048
0
        strings = _bfd_coff_read_string_table (input_bfd);
2049
0
        if (strings == NULL)
2050
0
          return false;
2051
0
      }
2052
0
          if ((bfd_size_type) auxp->x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2053
0
      filename = _("<corrupt>");
2054
0
          else
2055
0
      filename = strings + auxp->x_file.x_n.x_n.x_offset;
2056
0
          indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2057
0
             hash, copy);
2058
0
          if (indx == (bfd_size_type) -1)
2059
0
      return false;
2060
0
          auxp->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
2061
0
        }
2062
0
    }
2063
0
        else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2064
0
           && isymp->n_sclass != C_NT_WEAK)
2065
0
    {
2066
0
      unsigned long indx;
2067
2068
0
      if (ISFCN (isymp->n_type)
2069
0
          || ISTAG (isymp->n_sclass)
2070
0
          || isymp->n_sclass == C_BLOCK
2071
0
          || isymp->n_sclass == C_FCN)
2072
0
        {
2073
0
          indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32;
2074
0
          if (indx > 0
2075
0
        && indx < obj_raw_syment_count (input_bfd))
2076
0
      {
2077
        /* We look forward through the symbol for
2078
           the index of the next symbol we are going
2079
           to include.  I don't know if this is
2080
           entirely right.  */
2081
0
        while ((flaginfo->sym_indices[indx] < 0
2082
0
          || ((bfd_size_type) flaginfo->sym_indices[indx]
2083
0
              < syment_base))
2084
0
         && indx < obj_raw_syment_count (input_bfd))
2085
0
          ++indx;
2086
0
        if (indx >= obj_raw_syment_count (input_bfd))
2087
0
          indx = output_index;
2088
0
        else
2089
0
          indx = flaginfo->sym_indices[indx];
2090
0
        auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 = indx;
2091
0
      }
2092
0
        }
2093
2094
0
      indx = auxp->x_sym.x_tagndx.u32;
2095
0
      if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2096
0
        {
2097
0
          long symindx;
2098
2099
0
          symindx = flaginfo->sym_indices[indx];
2100
0
          if (symindx < 0)
2101
0
      auxp->x_sym.x_tagndx.u32 = 0;
2102
0
          else
2103
0
      auxp->x_sym.x_tagndx.u32 = symindx;
2104
0
        }
2105
2106
      /* The .bf symbols are supposed to be linked through
2107
         the endndx field.  We need to carry this list
2108
         across object files.  */
2109
0
      if (i == 0
2110
0
          && h == NULL
2111
0
          && isymp->n_sclass == C_FCN
2112
0
          && (isymp->_n._n_n._n_zeroes != 0
2113
0
        || isymp->_n._n_n._n_offset == 0)
2114
0
          && isymp->_n._n_name[0] == '.'
2115
0
          && isymp->_n._n_name[1] == 'b'
2116
0
          && isymp->_n._n_name[2] == 'f'
2117
0
          && isymp->_n._n_name[3] == '\0')
2118
0
        {
2119
0
          if (flaginfo->last_bf_index != -1)
2120
0
      {
2121
0
        flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
2122
0
          *indexp;
2123
2124
0
        if ((bfd_size_type) flaginfo->last_bf_index
2125
0
            >= syment_base)
2126
0
          {
2127
0
            void *auxout;
2128
2129
            /* The last .bf symbol is in this input
2130
         file.  This will only happen if the
2131
         assembler did not set up the .bf
2132
         endndx symbols correctly.  */
2133
0
            auxout = (flaginfo->outsyms
2134
0
          + ((flaginfo->last_bf_index
2135
0
              - syment_base)
2136
0
             * osymesz));
2137
2138
0
            bfd_coff_swap_aux_out (output_bfd,
2139
0
                 &flaginfo->last_bf,
2140
0
                 isymp->n_type,
2141
0
                 isymp->n_sclass,
2142
0
                 0, isymp->n_numaux,
2143
0
                 auxout);
2144
0
          }
2145
0
        else
2146
0
          {
2147
0
            file_ptr pos;
2148
2149
            /* We have already written out the last
2150
         .bf aux entry.  We need to write it
2151
         out again.  We borrow *outsym
2152
         temporarily.  FIXME: This case should
2153
         be made faster.  */
2154
0
            bfd_coff_swap_aux_out (output_bfd,
2155
0
                 &flaginfo->last_bf,
2156
0
                 isymp->n_type,
2157
0
                 isymp->n_sclass,
2158
0
                 0, isymp->n_numaux,
2159
0
                 outsym);
2160
0
            pos = obj_sym_filepos (output_bfd);
2161
0
            pos += flaginfo->last_bf_index * osymesz;
2162
0
            if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2163
0
          || (bfd_write (outsym, osymesz, output_bfd)
2164
0
              != osymesz))
2165
0
        return false;
2166
0
          }
2167
0
      }
2168
2169
0
          if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 != 0)
2170
0
      flaginfo->last_bf_index = -1;
2171
0
          else
2172
0
      {
2173
        /* The endndx field of this aux entry must
2174
           be updated with the symbol number of the
2175
           next .bf symbol.  */
2176
0
        flaginfo->last_bf = *auxp;
2177
0
        flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2178
0
               / osymesz)
2179
0
              + syment_base);
2180
0
      }
2181
0
        }
2182
0
    }
2183
2184
0
        if (h == NULL)
2185
0
    {
2186
0
      bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2187
0
           isymp->n_sclass, i, isymp->n_numaux,
2188
0
           outsym);
2189
0
      outsym += osymesz;
2190
0
    }
2191
2192
0
        esym += isymesz;
2193
0
      }
2194
0
  }
2195
2196
0
      indexp += add;
2197
0
      isymp += add;
2198
0
      sym_hash += add;
2199
0
    }
2200
2201
  /* Relocate the line numbers, unless we are stripping them.  */
2202
0
  if (flaginfo->info->strip == strip_none
2203
0
      || flaginfo->info->strip == strip_some)
2204
0
    {
2205
0
      for (o = input_bfd->sections; o != NULL; o = o->next)
2206
0
  {
2207
0
    bfd_vma offset;
2208
0
    bfd_byte *eline;
2209
0
    bfd_byte *elineend;
2210
0
    bfd_byte *oeline;
2211
0
    bool skipping;
2212
0
    file_ptr pos;
2213
0
    bfd_size_type amt;
2214
2215
    /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2216
       build_link_order in ldwrite.c will not have created a
2217
       link order, which means that we will not have seen this
2218
       input section in _bfd_coff_final_link, which means that
2219
       we will not have allocated space for the line numbers of
2220
       this section.  I don't think line numbers can be
2221
       meaningful for a section which does not have
2222
       SEC_HAS_CONTENTS set, but, if they do, this must be
2223
       changed.  */
2224
0
    if (o->lineno_count == 0
2225
0
        || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2226
0
      continue;
2227
2228
0
    if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2229
0
        || bfd_read (flaginfo->linenos, linesz * o->lineno_count,
2230
0
         input_bfd) != linesz * o->lineno_count)
2231
0
      return false;
2232
2233
0
    offset = o->output_section->vma + o->output_offset - o->vma;
2234
0
    eline = flaginfo->linenos;
2235
0
    oeline = flaginfo->linenos;
2236
0
    elineend = eline + linesz * o->lineno_count;
2237
0
    skipping = false;
2238
0
    for (; eline < elineend; eline += linesz)
2239
0
      {
2240
0
        struct internal_lineno iline;
2241
2242
0
        bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2243
2244
0
        if (iline.l_lnno != 0)
2245
0
    iline.l_addr.l_paddr += offset;
2246
0
        else if (iline.l_addr.l_symndx >= 0
2247
0
           && ((unsigned long) iline.l_addr.l_symndx
2248
0
         < obj_raw_syment_count (input_bfd)))
2249
0
    {
2250
0
      long indx;
2251
2252
0
      indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2253
2254
0
      if (indx < 0)
2255
0
        {
2256
          /* These line numbers are attached to a symbol
2257
       which we are stripping.  We must discard the
2258
       line numbers because reading them back with
2259
       no associated symbol (or associating them all
2260
       with symbol #0) will fail.  We can't regain
2261
       the space in the output file, but at least
2262
       they're dense.  */
2263
0
          skipping = true;
2264
0
        }
2265
0
      else
2266
0
        {
2267
0
          struct internal_syment is;
2268
0
          union internal_auxent ia;
2269
2270
          /* Fix up the lnnoptr field in the aux entry of
2271
       the symbol.  It turns out that we can't do
2272
       this when we modify the symbol aux entries,
2273
       because gas sometimes screws up the lnnoptr
2274
       field and makes it an offset from the start
2275
       of the line numbers rather than an absolute
2276
       file index.  */
2277
0
          bfd_coff_swap_sym_in (output_bfd,
2278
0
              (flaginfo->outsyms
2279
0
               + ((indx - syment_base)
2280
0
            * osymesz)), &is);
2281
0
          if ((ISFCN (is.n_type)
2282
0
         || is.n_sclass == C_BLOCK)
2283
0
        && is.n_numaux >= 1)
2284
0
      {
2285
0
        void *auxptr;
2286
2287
0
        auxptr = (flaginfo->outsyms
2288
0
            + ((indx - syment_base + 1)
2289
0
               * osymesz));
2290
0
        bfd_coff_swap_aux_in (output_bfd, auxptr,
2291
0
            is.n_type, is.n_sclass,
2292
0
            0, is.n_numaux, &ia);
2293
0
        ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2294
0
          (o->output_section->line_filepos
2295
0
           + o->output_section->lineno_count * linesz
2296
0
           + eline - flaginfo->linenos);
2297
0
        bfd_coff_swap_aux_out (output_bfd, &ia,
2298
0
             is.n_type, is.n_sclass, 0,
2299
0
             is.n_numaux, auxptr);
2300
0
      }
2301
2302
0
          skipping = false;
2303
0
        }
2304
2305
0
      iline.l_addr.l_symndx = indx;
2306
0
    }
2307
2308
0
        if (!skipping)
2309
0
    {
2310
0
      bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2311
0
      oeline += linesz;
2312
0
    }
2313
0
      }
2314
2315
0
    pos = o->output_section->line_filepos;
2316
0
    pos += o->output_section->lineno_count * linesz;
2317
0
    amt = oeline - flaginfo->linenos;
2318
0
    if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2319
0
        || bfd_write (flaginfo->linenos, amt, output_bfd) != amt)
2320
0
      return false;
2321
2322
0
    o->output_section->lineno_count += amt / linesz;
2323
0
  }
2324
0
    }
2325
2326
  /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2327
     symbol will be the first symbol in the next input file.  In the
2328
     normal case, this will save us from writing out the C_FILE symbol
2329
     again.  */
2330
0
  if (flaginfo->last_file_index != -1
2331
0
      && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2332
0
    {
2333
0
      flaginfo->last_file.n_value = output_index;
2334
0
      bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2335
0
           (flaginfo->outsyms
2336
0
            + ((flaginfo->last_file_index - syment_base)
2337
0
         * osymesz)));
2338
0
    }
2339
2340
  /* Write the modified symbols to the output file.  */
2341
0
  if (outsym > flaginfo->outsyms)
2342
0
    {
2343
0
      file_ptr pos;
2344
0
      bfd_size_type amt;
2345
2346
0
      pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2347
0
      amt = outsym - flaginfo->outsyms;
2348
0
      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2349
0
    || bfd_write (flaginfo->outsyms, amt, output_bfd) != amt)
2350
0
  return false;
2351
2352
0
      BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2353
0
       + (outsym - flaginfo->outsyms) / osymesz)
2354
0
      == output_index);
2355
2356
0
      obj_raw_syment_count (output_bfd) = output_index;
2357
0
    }
2358
2359
  /* Relocate the contents of each section.  */
2360
0
  adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2361
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
2362
0
    {
2363
0
      bfd_byte *contents;
2364
0
      struct coff_section_tdata *secdata;
2365
2366
0
      if (! o->linker_mark)
2367
  /* This section was omitted from the link.  */
2368
0
  continue;
2369
2370
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
2371
0
  continue;
2372
2373
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
2374
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2375
0
  {
2376
0
    if ((o->flags & SEC_RELOC) != 0
2377
0
        && o->reloc_count != 0)
2378
0
      {
2379
0
        _bfd_error_handler
2380
    /* xgettext: c-format */
2381
0
    (_("%pB: relocs in section `%pA', but it has no contents"),
2382
0
     input_bfd, o);
2383
0
        bfd_set_error (bfd_error_no_contents);
2384
0
        return false;
2385
0
      }
2386
2387
0
    continue;
2388
0
  }
2389
2390
0
      secdata = coff_section_data (input_bfd, o);
2391
0
      if (secdata != NULL && secdata->contents != NULL)
2392
0
  contents = secdata->contents;
2393
0
      else
2394
0
  {
2395
0
    contents = flaginfo->contents;
2396
0
    if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2397
0
      return false;
2398
0
  }
2399
2400
0
      if ((o->flags & SEC_RELOC) != 0)
2401
0
  {
2402
0
    int target_index;
2403
0
    struct internal_reloc *internal_relocs;
2404
0
    struct internal_reloc *irel;
2405
2406
    /* Read in the relocs.  */
2407
0
    target_index = o->output_section->target_index;
2408
0
    internal_relocs = (bfd_coff_read_internal_relocs
2409
0
           (input_bfd, o, false, flaginfo->external_relocs,
2410
0
            bfd_link_relocatable (flaginfo->info),
2411
0
            (bfd_link_relocatable (flaginfo->info)
2412
0
             ? (flaginfo->section_info[target_index].relocs
2413
0
          + o->output_section->reloc_count)
2414
0
             : flaginfo->internal_relocs)));
2415
0
    if (internal_relocs == NULL
2416
0
        && o->reloc_count > 0)
2417
0
      return false;
2418
2419
    /* Run through the relocs looking for relocs against symbols
2420
       coming from discarded sections and complain about them.  */
2421
0
    irel = internal_relocs;
2422
0
    for (; irel < &internal_relocs[o->reloc_count]; irel++)
2423
0
      {
2424
0
        struct coff_link_hash_entry *h;
2425
0
        asection *ps = NULL;
2426
0
        unsigned long symndx = irel->r_symndx;
2427
0
        if (symndx >= obj_raw_syment_count (input_bfd))
2428
0
    continue;
2429
0
        h = obj_coff_sym_hashes (input_bfd)[symndx];
2430
0
        if (h == NULL)
2431
0
    continue;
2432
0
        while (h->root.type == bfd_link_hash_indirect
2433
0
         || h->root.type == bfd_link_hash_warning)
2434
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
2435
0
        if (h->root.type == bfd_link_hash_defined
2436
0
      || h->root.type == bfd_link_hash_defweak)
2437
0
    ps = h->root.u.def.section;
2438
0
        if (ps == NULL)
2439
0
    continue;
2440
        /* Complain if definition comes from an excluded section.  */
2441
0
        if (ps->flags & SEC_EXCLUDE)
2442
0
    (*flaginfo->info->callbacks->einfo)
2443
      /* xgettext: c-format */
2444
0
      (_("%X`%s' referenced in section `%pA' of %pB: "
2445
0
         "defined in discarded section `%pA' of %pB\n"),
2446
0
       h->root.root.string, o, input_bfd, ps, ps->owner);
2447
0
      }
2448
2449
    /* Call processor specific code to relocate the section
2450
       contents.  */
2451
0
    if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2452
0
             input_bfd, o,
2453
0
             contents,
2454
0
             internal_relocs,
2455
0
             flaginfo->internal_syms,
2456
0
             flaginfo->sec_ptrs))
2457
0
      return false;
2458
2459
0
    if (bfd_link_relocatable (flaginfo->info))
2460
0
      {
2461
0
        bfd_vma offset;
2462
0
        struct internal_reloc *irelend;
2463
0
        struct coff_link_hash_entry **rel_hash;
2464
2465
0
        offset = o->output_section->vma + o->output_offset - o->vma;
2466
0
        irel = internal_relocs;
2467
0
        irelend = irel + o->reloc_count;
2468
0
        rel_hash = (flaginfo->section_info[target_index].rel_hashes
2469
0
        + o->output_section->reloc_count);
2470
0
        for (; irel < irelend; irel++, rel_hash++)
2471
0
    {
2472
0
      struct coff_link_hash_entry *h;
2473
0
      bool adjusted;
2474
2475
0
      *rel_hash = NULL;
2476
2477
      /* Adjust the reloc address and symbol index.  */
2478
0
      irel->r_vaddr += offset;
2479
2480
0
      if ((unsigned long) irel->r_symndx
2481
0
          >= obj_raw_syment_count (input_bfd))
2482
0
        continue;
2483
2484
0
      if (adjust_symndx)
2485
0
        {
2486
0
          if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2487
0
                input_bfd, o, irel,
2488
0
                &adjusted))
2489
0
      return false;
2490
0
          if (adjusted)
2491
0
      continue;
2492
0
        }
2493
2494
0
      h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2495
0
      if (h != NULL)
2496
0
        {
2497
          /* This is a global symbol.  */
2498
0
          if (h->indx >= 0)
2499
0
      irel->r_symndx = h->indx;
2500
0
          else
2501
0
      {
2502
        /* This symbol is being written at the end
2503
           of the file, and we do not yet know the
2504
           symbol index.  We save the pointer to the
2505
           hash table entry in the rel_hash list.
2506
           We set the indx field to -2 to indicate
2507
           that this symbol must not be stripped.  */
2508
0
        *rel_hash = h;
2509
0
        h->indx = -2;
2510
0
      }
2511
0
        }
2512
0
      else
2513
0
        {
2514
0
          long indx;
2515
2516
0
          indx = flaginfo->sym_indices[irel->r_symndx];
2517
0
          if (indx != -1)
2518
0
      irel->r_symndx = indx;
2519
0
          else
2520
0
      {
2521
0
        struct internal_syment *is;
2522
0
        const char *name;
2523
0
        char buf[SYMNMLEN + 1];
2524
2525
        /* This reloc is against a symbol we are
2526
           stripping.  This should have been handled
2527
           by the 'dont_skip_symbol' code in the while
2528
           loop at the top of this function.  */
2529
0
        is = flaginfo->internal_syms + irel->r_symndx;
2530
2531
0
        name = (_bfd_coff_internal_syment_name
2532
0
          (input_bfd, is, buf));
2533
0
        if (name == NULL)
2534
0
          return false;
2535
2536
0
        (*flaginfo->info->callbacks->unattached_reloc)
2537
0
          (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2538
0
      }
2539
0
        }
2540
0
    }
2541
2542
0
        o->output_section->reloc_count += o->reloc_count;
2543
0
      }
2544
0
  }
2545
2546
      /* Write out the modified section contents.  */
2547
0
      if (o->sec_info_type != SEC_INFO_TYPE_STABS)
2548
0
  {
2549
0
    file_ptr loc = (o->output_offset
2550
0
        * bfd_octets_per_byte (output_bfd, o));
2551
0
    if (! bfd_set_section_contents (output_bfd, o->output_section,
2552
0
            contents, loc, o->size))
2553
0
      return false;
2554
0
  }
2555
0
      else
2556
0
  {
2557
0
    if (! (_bfd_write_section_stabs
2558
0
     (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2559
0
      o, contents)))
2560
0
      return false;
2561
0
  }
2562
0
    }
2563
2564
0
  if (! flaginfo->info->keep_memory
2565
0
      && ! _bfd_coff_free_symbols (input_bfd))
2566
0
    return false;
2567
2568
0
  return true;
2569
0
}
2570
2571
/* Write out a global symbol.  Called via bfd_hash_traverse.  */
2572
2573
bool
2574
_bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2575
0
{
2576
0
  struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2577
0
  struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2578
0
  bfd *output_bfd;
2579
0
  struct internal_syment isym;
2580
0
  bfd_size_type symesz;
2581
0
  unsigned int i;
2582
0
  file_ptr pos;
2583
2584
0
  output_bfd = flaginfo->output_bfd;
2585
2586
0
  if (h->root.type == bfd_link_hash_warning)
2587
0
    {
2588
0
      h = (struct coff_link_hash_entry *) h->root.u.i.link;
2589
0
      if (h->root.type == bfd_link_hash_new)
2590
0
  return true;
2591
0
    }
2592
2593
0
  if (h->indx >= 0)
2594
0
    return true;
2595
2596
0
  if (h->indx != -2
2597
0
      && (flaginfo->info->strip == strip_all
2598
0
    || (flaginfo->info->strip == strip_some
2599
0
        && (bfd_hash_lookup (flaginfo->info->keep_hash,
2600
0
           h->root.root.string, false, false)
2601
0
      == NULL))))
2602
0
    return true;
2603
2604
0
  switch (h->root.type)
2605
0
    {
2606
0
    default:
2607
0
    case bfd_link_hash_new:
2608
0
    case bfd_link_hash_warning:
2609
0
      abort ();
2610
0
      return false;
2611
2612
0
    case bfd_link_hash_undefined:
2613
0
      if (h->indx == -3)
2614
0
  return true;
2615
      /* Fall through.  */
2616
0
    case bfd_link_hash_undefweak:
2617
0
      isym.n_scnum = N_UNDEF;
2618
0
      isym.n_value = 0;
2619
0
      break;
2620
2621
0
    case bfd_link_hash_defined:
2622
0
    case bfd_link_hash_defweak:
2623
0
      {
2624
0
  asection *sec;
2625
2626
0
  sec = h->root.u.def.section;
2627
0
  isym.n_value = h->root.u.def.value;
2628
0
  if (sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2629
0
    isym.n_value =
2630
0
      _bfd_merged_section_offset (output_bfd, &sec, isym.n_value);
2631
0
  isym.n_value += sec->output_offset;
2632
2633
0
  sec = sec->output_section;
2634
0
  if (bfd_is_abs_section (sec))
2635
0
    isym.n_scnum = N_ABS;
2636
0
  else
2637
0
    isym.n_scnum = sec->target_index;
2638
0
  if (! obj_pe (flaginfo->output_bfd))
2639
0
    isym.n_value += sec->vma;
2640
0
#ifdef BFD64
2641
0
  if (isym.n_value > (bfd_vma) 0xffffffff)
2642
0
    {
2643
0
      if (! h->root.linker_def)
2644
0
        _bfd_error_handler
2645
0
    (_("%pB: stripping non-representable symbol '%s' "
2646
0
       "(value 0x%" PRIx64 ")"),
2647
0
     output_bfd, h->root.root.string, isym.n_value);
2648
0
      return true;
2649
0
    }
2650
0
#endif
2651
0
      }
2652
0
      break;
2653
2654
0
    case bfd_link_hash_common:
2655
0
      isym.n_scnum = N_UNDEF;
2656
0
      isym.n_value = h->root.u.c.size;
2657
0
      break;
2658
2659
0
    case bfd_link_hash_indirect:
2660
      /* Just ignore these.  They can't be handled anyhow.  */
2661
0
      return true;
2662
0
    }
2663
2664
0
  if (strlen (h->root.root.string) <= SYMNMLEN)
2665
0
    strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2666
0
  else
2667
0
    {
2668
0
      bool hash;
2669
0
      bfd_size_type indx;
2670
2671
0
      hash = true;
2672
0
      if (flaginfo->info->traditional_format)
2673
0
  hash = false;
2674
0
      indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2675
0
         false);
2676
0
      if (indx == (bfd_size_type) -1)
2677
0
  {
2678
0
    flaginfo->failed = true;
2679
0
    return false;
2680
0
  }
2681
0
      isym._n._n_n._n_zeroes = 0;
2682
0
      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2683
0
    }
2684
2685
0
  isym.n_sclass = h->symbol_class;
2686
0
  isym.n_type = h->type;
2687
2688
0
  if (isym.n_sclass == C_NULL)
2689
0
    isym.n_sclass = C_EXT;
2690
2691
  /* If doing task linking and this is the pass where we convert
2692
     defined globals to statics, then do that conversion now.  If the
2693
     symbol is not being converted, just ignore it and it will be
2694
     output during a later pass.  */
2695
0
  if (flaginfo->global_to_static)
2696
0
    {
2697
0
      if (! IS_EXTERNAL (output_bfd, isym))
2698
0
  return true;
2699
2700
0
      isym.n_sclass = C_STAT;
2701
0
    }
2702
2703
  /* When a weak symbol is not overridden by a strong one,
2704
     turn it into an external symbol when not building a
2705
     shared or relocatable object.  */
2706
0
  if (! bfd_link_pic (flaginfo->info)
2707
0
      && ! bfd_link_relocatable (flaginfo->info)
2708
0
      && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2709
0
    isym.n_sclass = C_EXT;
2710
2711
0
  isym.n_numaux = h->numaux;
2712
2713
0
  bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2714
2715
0
  symesz = bfd_coff_symesz (output_bfd);
2716
2717
0
  pos = obj_sym_filepos (output_bfd);
2718
0
  pos += obj_raw_syment_count (output_bfd) * symesz;
2719
0
  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2720
0
      || bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2721
0
    {
2722
0
      flaginfo->failed = true;
2723
0
      return false;
2724
0
    }
2725
2726
0
  h->indx = obj_raw_syment_count (output_bfd);
2727
2728
0
  ++obj_raw_syment_count (output_bfd);
2729
2730
  /* Write out any associated aux entries.  Most of the aux entries
2731
     will have been modified in _bfd_coff_link_input_bfd.  We have to
2732
     handle section aux entries here, now that we have the final
2733
     relocation and line number counts.  */
2734
0
  for (i = 0; i < isym.n_numaux; i++)
2735
0
    {
2736
0
      union internal_auxent *auxp;
2737
2738
0
      auxp = h->aux + i;
2739
2740
      /* Look for a section aux entry here using the same tests that
2741
   coff_swap_aux_out uses.  */
2742
0
      if (i == 0
2743
0
    && (isym.n_sclass == C_STAT
2744
0
        || isym.n_sclass == C_HIDDEN)
2745
0
    && isym.n_type == T_NULL
2746
0
    && (h->root.type == bfd_link_hash_defined
2747
0
        || h->root.type == bfd_link_hash_defweak))
2748
0
  {
2749
0
    asection *sec;
2750
2751
0
    sec = h->root.u.def.section->output_section;
2752
0
    if (sec != NULL)
2753
0
      {
2754
0
        auxp->x_scn.x_scnlen = sec->size;
2755
2756
        /* For PE, an overflow on the final link reportedly does
2757
     not matter.  FIXME: Why not?  */
2758
0
        if (sec->reloc_count > 0xffff
2759
0
      && (! obj_pe (output_bfd)
2760
0
          || bfd_link_relocatable (flaginfo->info)))
2761
0
    _bfd_error_handler
2762
      /* xgettext: c-format */
2763
0
      (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2764
0
       output_bfd, sec, sec->reloc_count);
2765
2766
0
        if (sec->lineno_count > 0xffff
2767
0
      && (! obj_pe (output_bfd)
2768
0
          || bfd_link_relocatable (flaginfo->info)))
2769
0
    _bfd_error_handler
2770
      /* xgettext: c-format */
2771
0
      (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2772
0
       output_bfd, sec, sec->lineno_count);
2773
2774
0
        auxp->x_scn.x_nreloc = sec->reloc_count;
2775
0
        auxp->x_scn.x_nlinno = sec->lineno_count;
2776
0
        auxp->x_scn.x_checksum = 0;
2777
0
        auxp->x_scn.x_associated = 0;
2778
0
        auxp->x_scn.x_comdat = 0;
2779
0
      }
2780
0
  }
2781
2782
0
      bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2783
0
           isym.n_sclass, (int) i, isym.n_numaux,
2784
0
           flaginfo->outsyms);
2785
0
      if (bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2786
0
  {
2787
0
    flaginfo->failed = true;
2788
0
    return false;
2789
0
  }
2790
0
      ++obj_raw_syment_count (output_bfd);
2791
0
    }
2792
2793
0
  return true;
2794
0
}
2795
2796
/* Write out task global symbols, converting them to statics.  Called
2797
   via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
2798
   the dirty work, if the symbol we are processing needs conversion.  */
2799
2800
bool
2801
_bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2802
0
{
2803
0
  struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2804
0
  bool rtnval = true;
2805
0
  bool save_global_to_static;
2806
2807
0
  if (h->root.type == bfd_link_hash_warning)
2808
0
    h = (struct coff_link_hash_entry *) h->root.u.i.link;
2809
2810
0
  if (h->indx < 0)
2811
0
    {
2812
0
      switch (h->root.type)
2813
0
  {
2814
0
  case bfd_link_hash_defined:
2815
0
  case bfd_link_hash_defweak:
2816
0
    save_global_to_static = flaginfo->global_to_static;
2817
0
    flaginfo->global_to_static = true;
2818
0
    rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2819
0
    flaginfo->global_to_static = save_global_to_static;
2820
0
    break;
2821
0
  default:
2822
0
    break;
2823
0
  }
2824
0
    }
2825
0
  return (rtnval);
2826
0
}
2827
2828
/* Handle a link order which is supposed to generate a reloc.  */
2829
2830
bool
2831
_bfd_coff_reloc_link_order (bfd *output_bfd,
2832
          struct coff_final_link_info *flaginfo,
2833
          asection *output_section,
2834
          struct bfd_link_order *link_order)
2835
0
{
2836
0
  reloc_howto_type *howto;
2837
0
  struct internal_reloc *irel;
2838
0
  struct coff_link_hash_entry **rel_hash_ptr;
2839
2840
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2841
0
  if (howto == NULL)
2842
0
    {
2843
0
      bfd_set_error (bfd_error_bad_value);
2844
0
      return false;
2845
0
    }
2846
2847
0
  if (link_order->u.reloc.p->addend != 0)
2848
0
    {
2849
0
      bfd_size_type size;
2850
0
      bfd_byte *buf;
2851
0
      bfd_reloc_status_type rstat;
2852
0
      bool ok;
2853
0
      file_ptr loc;
2854
2855
0
      size = bfd_get_reloc_size (howto);
2856
0
      buf = (bfd_byte *) bfd_zmalloc (size);
2857
0
      if (buf == NULL && size != 0)
2858
0
  return false;
2859
2860
0
      rstat = _bfd_relocate_contents (howto, output_bfd,
2861
0
              (bfd_vma) link_order->u.reloc.p->addend,
2862
0
              buf);
2863
0
      switch (rstat)
2864
0
  {
2865
0
  case bfd_reloc_ok:
2866
0
    break;
2867
0
  default:
2868
0
  case bfd_reloc_outofrange:
2869
0
    abort ();
2870
0
  case bfd_reloc_overflow:
2871
0
    (*flaginfo->info->callbacks->reloc_overflow)
2872
0
      (flaginfo->info, NULL,
2873
0
       (link_order->type == bfd_section_reloc_link_order
2874
0
        ? bfd_section_name (link_order->u.reloc.p->u.section)
2875
0
        : link_order->u.reloc.p->u.name),
2876
0
       howto->name, link_order->u.reloc.p->addend,
2877
0
       (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2878
0
    break;
2879
0
  }
2880
0
      loc = link_order->offset * bfd_octets_per_byte (output_bfd,
2881
0
                  output_section);
2882
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
2883
0
             loc, size);
2884
0
      free (buf);
2885
0
      if (! ok)
2886
0
  return false;
2887
0
    }
2888
2889
  /* Store the reloc information in the right place.  It will get
2890
     swapped and written out at the end of the final_link routine.  */
2891
0
  irel = (flaginfo->section_info[output_section->target_index].relocs
2892
0
    + output_section->reloc_count);
2893
0
  rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
2894
0
      + output_section->reloc_count);
2895
2896
0
  memset (irel, 0, sizeof (struct internal_reloc));
2897
0
  *rel_hash_ptr = NULL;
2898
2899
0
  irel->r_vaddr = output_section->vma + link_order->offset;
2900
2901
0
  if (link_order->type == bfd_section_reloc_link_order)
2902
0
    {
2903
      /* We need to somehow locate a symbol in the right section.  The
2904
   symbol must either have a value of zero, or we must adjust
2905
   the addend by the value of the symbol.  FIXME: Write this
2906
   when we need it.  The old linker couldn't handle this anyhow.  */
2907
0
      abort ();
2908
0
      *rel_hash_ptr = NULL;
2909
0
      irel->r_symndx = 0;
2910
0
    }
2911
0
  else
2912
0
    {
2913
0
      struct coff_link_hash_entry *h;
2914
2915
0
      h = ((struct coff_link_hash_entry *)
2916
0
     bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
2917
0
           link_order->u.reloc.p->u.name,
2918
0
           false, false, true));
2919
0
      if (h != NULL)
2920
0
  {
2921
0
    if (h->indx >= 0)
2922
0
      irel->r_symndx = h->indx;
2923
0
    else
2924
0
      {
2925
        /* Set the index to -2 to force this symbol to get
2926
     written out.  */
2927
0
        h->indx = -2;
2928
0
        *rel_hash_ptr = h;
2929
0
        irel->r_symndx = 0;
2930
0
      }
2931
0
  }
2932
0
      else
2933
0
  {
2934
0
    (*flaginfo->info->callbacks->unattached_reloc)
2935
0
      (flaginfo->info, link_order->u.reloc.p->u.name,
2936
0
       (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2937
0
    irel->r_symndx = 0;
2938
0
  }
2939
0
    }
2940
2941
  /* FIXME: Is this always right?  */
2942
0
  irel->r_type = howto->type;
2943
2944
  /* r_size is only used on the RS/6000, which needs its own linker
2945
     routines anyhow.  r_extern is only used for ECOFF.  */
2946
2947
  /* FIXME: What is the right value for r_offset?  Is zero OK?  */
2948
0
  ++output_section->reloc_count;
2949
2950
0
  return true;
2951
0
}
2952
2953
/* A basic reloc handling routine which may be used by processors with
2954
   simple relocs.  */
2955
2956
bool
2957
_bfd_coff_generic_relocate_section (bfd *output_bfd,
2958
            struct bfd_link_info *info,
2959
            bfd *input_bfd,
2960
            asection *input_section,
2961
            bfd_byte *contents,
2962
            struct internal_reloc *relocs,
2963
            struct internal_syment *syms,
2964
            asection **sections)
2965
0
{
2966
0
  struct internal_reloc *rel;
2967
0
  struct internal_reloc *relend;
2968
2969
0
  rel = relocs;
2970
0
  relend = rel + input_section->reloc_count;
2971
0
  for (; rel < relend; rel++)
2972
0
    {
2973
0
      long symndx;
2974
0
      struct coff_link_hash_entry *h;
2975
0
      struct internal_syment *sym;
2976
0
      bfd_vma addend;
2977
0
      bfd_vma val;
2978
0
      asection *sec;
2979
0
      reloc_howto_type *howto;
2980
0
      bfd_reloc_status_type rstat;
2981
2982
0
      symndx = rel->r_symndx;
2983
2984
0
      if (symndx == -1)
2985
0
  {
2986
0
    h = NULL;
2987
0
    sym = NULL;
2988
0
  }
2989
0
      else if ((unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2990
0
  {
2991
0
    _bfd_error_handler
2992
      /* xgettext: c-format */
2993
0
      (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
2994
0
    return false;
2995
0
  }
2996
0
      else
2997
0
  {
2998
0
    h = obj_coff_sym_hashes (input_bfd)[symndx];
2999
0
    sym = syms + symndx;
3000
0
  }
3001
3002
      /* COFF treats common symbols in one of two ways.  Either the
3003
   size of the symbol is included in the section contents, or it
3004
   is not.  We assume that the size is not included, and force
3005
   the rtype_to_howto function to adjust the addend as needed.  */
3006
0
      if (sym != NULL && sym->n_scnum != 0)
3007
0
  addend = - sym->n_value;
3008
0
      else
3009
0
  addend = 0;
3010
3011
0
      howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
3012
0
               sym, &addend);
3013
0
      if (howto == NULL)
3014
0
  return false;
3015
3016
      /* If we are doing a relocatable link, then we can just ignore
3017
   a PC relative reloc that is pcrel_offset.  It will already
3018
   have the correct value.  If this is not a relocatable link,
3019
   then we should ignore the symbol value.  */
3020
0
      if (howto->pc_relative && howto->pcrel_offset)
3021
0
  {
3022
0
    if (bfd_link_relocatable (info))
3023
0
      continue;
3024
0
    if (sym != NULL && sym->n_scnum != 0)
3025
0
      addend += sym->n_value;
3026
0
  }
3027
3028
0
      val = 0;
3029
0
      sec = NULL;
3030
0
      if (h == NULL)
3031
0
  {
3032
0
    if (symndx == -1)
3033
0
      {
3034
0
        sec = bfd_abs_section_ptr;
3035
0
        val = 0;
3036
0
      }
3037
0
    else
3038
0
      {
3039
0
        sec = sections[symndx];
3040
3041
        /* PR 19623: Relocations against symbols in
3042
     the absolute sections should ignored.
3043
     PR 29807: Also ignore relocs against file symbols or
3044
     other such nonsense in fuzzed objects.  */
3045
0
        if (sec == NULL || bfd_is_abs_section (sec))
3046
0
    continue;
3047
3048
0
        val = (sec->output_section->vma
3049
0
         + sec->output_offset
3050
0
         + sym->n_value);
3051
0
        if (! obj_pe (input_bfd))
3052
0
    val -= sec->vma;
3053
0
      }
3054
0
  }
3055
0
      else
3056
0
  {
3057
0
    if (h->root.type == bfd_link_hash_defined
3058
        /* Defined weak symbols are a GNU extension.  */
3059
0
        || h->root.type == bfd_link_hash_defweak)
3060
0
      {
3061
0
        sec = h->root.u.def.section;
3062
0
        BFD_ASSERT (sec->output_section != NULL);
3063
0
        val = (h->root.u.def.value
3064
0
         + sec->output_section->vma
3065
0
         + sec->output_offset);
3066
0
      }
3067
3068
0
    else if (h->root.type == bfd_link_hash_undefweak)
3069
0
      {
3070
0
        if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
3071
0
    {
3072
      /* See _Microsoft Portable Executable and Common Object
3073
         File Format Specification_, section 5.5.3.
3074
         Note that weak symbols without aux records are a GNU
3075
         extension.
3076
         FIXME: All weak externals are treated as having
3077
         characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3078
         These behave as per SVR4 ABI:  A library member
3079
         will resolve a weak external only if a normal
3080
         external causes the library member to be linked.
3081
         See also linker.c: generic_link_check_archive_element. */
3082
0
      struct coff_link_hash_entry *h2 = NULL;
3083
0
      unsigned long symndx2 = h->aux->x_sym.x_tagndx.u32;
3084
0
      if (symndx2 < obj_raw_syment_count (h->auxbfd))
3085
0
        h2 = obj_coff_sym_hashes (h->auxbfd)[symndx2];
3086
3087
0
      if (!h2 || h2->root.type == bfd_link_hash_undefined)
3088
0
        {
3089
0
          sec = bfd_abs_section_ptr;
3090
0
          val = 0;
3091
0
        }
3092
0
      else
3093
0
        {
3094
0
          sec = h2->root.u.def.section;
3095
0
          val = h2->root.u.def.value
3096
0
      + sec->output_section->vma + sec->output_offset;
3097
0
        }
3098
0
    }
3099
0
        else
3100
    /* This is a GNU extension.  */
3101
0
    val = 0;
3102
0
      }
3103
3104
0
    else if (! bfd_link_relocatable (info))
3105
0
      {
3106
0
        (*info->callbacks->undefined_symbol)
3107
0
    (info, h->root.root.string, input_bfd, input_section,
3108
0
     rel->r_vaddr - input_section->vma, true);
3109
        /* Stop the linker from issueing errors about truncated relocs
3110
     referencing this undefined symbol by giving it an address
3111
     that should be in range.  */
3112
0
        val = input_section->output_section->vma;
3113
0
      }
3114
0
  }
3115
3116
      /* If the input section defining the symbol has been discarded
3117
   then zero this reloc field.  */
3118
0
      if (sec != NULL && discarded_section (sec))
3119
0
  {
3120
0
    _bfd_clear_contents (howto, input_bfd, input_section,
3121
0
             contents, rel->r_vaddr - input_section->vma);
3122
0
    continue;
3123
0
  }
3124
3125
0
      if (info->base_file)
3126
0
  {
3127
    /* Emit a reloc if the backend thinks it needs it.  */
3128
0
    if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3129
0
      {
3130
        /* Relocation to a symbol in a section which isn't
3131
     absolute.  We output the address here to a file.
3132
     This file is then read by dlltool when generating the
3133
     reloc section.  Note that the base file is not
3134
     portable between systems.  We write out a bfd_vma here,
3135
     and dlltool reads in a bfd_vma.  */
3136
0
        bfd_vma addr = (rel->r_vaddr
3137
0
         - input_section->vma
3138
0
         + input_section->output_offset
3139
0
         + input_section->output_section->vma);
3140
0
        if (obj_pe (output_bfd))
3141
0
    addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3142
0
        if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3143
0
      != sizeof (bfd_vma))
3144
0
    {
3145
0
      bfd_set_error (bfd_error_system_call);
3146
0
      return false;
3147
0
    }
3148
0
      }
3149
0
  }
3150
3151
0
      rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3152
0
          contents,
3153
0
          rel->r_vaddr - input_section->vma,
3154
0
          val, addend);
3155
3156
0
      switch (rstat)
3157
0
  {
3158
0
  default:
3159
0
    abort ();
3160
0
  case bfd_reloc_ok:
3161
0
    break;
3162
0
  case bfd_reloc_outofrange:
3163
0
    _bfd_error_handler
3164
      /* xgettext: c-format */
3165
0
      (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3166
0
       input_bfd, (uint64_t) rel->r_vaddr, input_section);
3167
0
    return false;
3168
0
  case bfd_reloc_overflow:
3169
0
    {
3170
      /* Ignore any weak undef symbols that may have overflowed.  Due to
3171
         PR ld/19011 the base address is now in the upper 64-bit address
3172
         range.  This means that when _bfd_final_link_relocate calculates
3173
         the overlow it takes the distance between the symbol and the VMA
3174
         which will now always overflow as 0 - 64-bit addr > 32-bit range
3175
         of the relocation.  This ends up creating PR ld/26659.  */
3176
0
      if (val == 0
3177
    /* Reverse the hack where 4 is subtracted from the addend.  */
3178
0
    && (addend + 4) == 0
3179
0
    && sym->n_sclass == C_NT_WEAK
3180
0
    && bfd_coff_classify_symbol (output_bfd, sym)
3181
0
         == COFF_SYMBOL_UNDEFINED)
3182
0
        break;
3183
3184
0
      const char *name;
3185
0
      char buf[SYMNMLEN + 1];
3186
3187
0
      if (symndx == -1)
3188
0
        name = "*ABS*";
3189
0
      else if (h != NULL)
3190
0
        name = NULL;
3191
0
      else
3192
0
        {
3193
0
    name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3194
0
    if (name == NULL)
3195
0
      return false;
3196
0
        }
3197
3198
0
      (*info->callbacks->reloc_overflow)
3199
0
        (info, (h ? &h->root : NULL), name, howto->name,
3200
0
         (bfd_vma) 0, input_bfd, input_section,
3201
0
         rel->r_vaddr - input_section->vma);
3202
0
    }
3203
0
  }
3204
0
    }
3205
3206
0
  return true;
3207
0
}