OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [cofflink.c] - Blame information for rev 167

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.