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