OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [bfd/] [cofflink.c] - Blame information for rev 309

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

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

powered by: WebSVN 2.1.0

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