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