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