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