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