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