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