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