Add new style linker support to COFF backend. a29k only for now.
[external/binutils.git] / bfd / cofflink.c
1 /* COFF specific linker code.
2    Copyright 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 static struct bfd_hash_entry *coff_link_hash_newfunc
84   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
85 static boolean coff_link_add_object_symbols
86   PARAMS ((bfd *, struct bfd_link_info *));
87 static boolean coff_link_check_archive_element
88   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
89 static boolean coff_link_get_symbols PARAMS ((bfd *));
90 static const char *coff_read_string_table PARAMS ((bfd *));
91 static boolean coff_link_free_symbols PARAMS ((bfd *));
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          malloc (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 (! coff_link_get_symbols (abfd))
195     return false;
196   if (! coff_link_add_symbols (abfd, info))
197     return false;
198   if (! info->keep_memory)
199     {
200       if (! coff_link_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 (! coff_link_get_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 (! coff_link_free_symbols (abfd))
232         return false;
233     }
234
235   return true;
236 }
237
238 /* Read in the external symbols.  */
239
240 static boolean
241 coff_link_get_symbols (abfd)
242      bfd *abfd;
243 {
244   bfd_size_type symesz;
245   size_t size;
246   PTR syms;
247
248   if (obj_coff_external_syms (abfd) != NULL)
249     return true;
250
251   symesz = bfd_coff_symesz (abfd);
252
253   size = obj_raw_syment_count (abfd) * symesz;
254
255   syms = malloc (size);
256   if (syms == NULL && size != 0)
257     {
258       bfd_set_error (bfd_error_no_memory);
259       return false;
260     }
261
262   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
263       || bfd_read (syms, size, 1, abfd) != size)
264     {
265       if (syms != NULL)
266         free (syms);
267       return false;
268     }
269
270   obj_coff_external_syms (abfd) = syms;
271
272   return true;
273 }
274
275 /* Read in the external strings.  The strings are not loaded until
276    they are needed.  This is because we have no simple way of
277    detecting a missing string table in an archive.  */
278
279 static const char *
280 coff_read_string_table (abfd)
281      bfd *abfd;
282 {
283   char extstrsize[STRING_SIZE_SIZE];
284   size_t strsize;
285   char *strings;
286
287   if (obj_coff_strings (abfd) != NULL)
288     return obj_coff_strings (abfd);
289
290   if (bfd_seek (abfd,
291                 (obj_sym_filepos (abfd)
292                  + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
293                 SEEK_SET) != 0)
294     return NULL;
295     
296   if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
297     {
298       if (bfd_get_error () != bfd_error_file_truncated)
299         return NULL;
300
301       /* There is no string table.  */
302       strsize = STRING_SIZE_SIZE;
303     }
304   else
305     {
306 #if STRING_SIZE_SIZE == 4
307       strsize = bfd_h_get_32 (abfd, extstrsize);
308 #else
309  #error Change bfd_h_get_32
310 #endif
311     }
312
313   strings = malloc (strsize);
314   if (strings == NULL)
315     {
316       bfd_set_error (bfd_error_no_memory);
317       return NULL;
318     }
319
320   if (bfd_read (strings + STRING_SIZE_SIZE,
321                 strsize - STRING_SIZE_SIZE, 1, abfd)
322       != strsize - STRING_SIZE_SIZE)
323     {
324       free (strings);
325       return NULL;
326     }
327
328   obj_coff_strings (abfd) = strings;
329
330   return strings;
331 }
332
333 /* Free up the external symbols and strings read from a COFF file.  */
334
335 static boolean
336 coff_link_free_symbols (abfd)
337      bfd *abfd;
338 {
339   if (obj_coff_external_syms (abfd) != NULL)
340     {
341       free (obj_coff_external_syms (abfd));
342       obj_coff_external_syms (abfd) = NULL;
343     }
344   if (obj_coff_strings (abfd) != NULL)
345     {
346       free (obj_coff_strings (abfd));
347       obj_coff_strings (abfd) = NULL;
348     }
349   return true;
350 }
351
352 /* Look through the symbols to see if this object file should be
353    included in the link.  */
354
355 static boolean
356 coff_link_check_ar_symbols (abfd, info, pneeded)
357      bfd *abfd;
358      struct bfd_link_info *info;
359      boolean *pneeded;
360 {
361   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
362   const char *strings;
363   bfd_size_type symesz;
364   bfd_byte *esym;
365   bfd_byte *esym_end;
366
367   *pneeded = false;
368
369   sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
370   strings = NULL;
371
372   symesz = bfd_coff_symesz (abfd);
373   esym = (bfd_byte *) obj_coff_external_syms (abfd);
374   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
375   while (esym < esym_end)
376     {
377       struct internal_syment sym;
378
379       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
380
381       if ((sym.n_sclass == C_EXT
382            || (sym_is_global && (*sym_is_global) (abfd, &sym)))
383           && (sym.n_scnum != 0 || sym.n_value != 0))
384         {
385           const char *name;
386           char buf[SYMNMLEN + 1];
387           struct bfd_link_hash_entry *h;
388
389           /* This symbol is externally visible, and is defined by this
390              object file.  */
391
392           /* FIXME: It's not clear this will work correctly if sizeof
393              (_n_zeroes) != 4.  */
394           if (sym._n._n_n._n_zeroes != 0
395               || sym._n._n_n._n_offset == 0)
396             {
397               memcpy (buf, sym._n._n_name, SYMNMLEN);
398               buf[SYMNMLEN] = '\0';
399               name = buf;
400             }
401           else
402             {
403               BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
404               if (strings == NULL)
405                 {
406                   strings = coff_read_string_table (abfd);
407                   if (strings == NULL)
408                     return false;
409                 }
410               name = strings + sym._n._n_n._n_offset;
411             }
412
413           h = bfd_link_hash_lookup (info->hash, name, false, false, true);
414
415           /* We are only interested in symbols that are currently
416              undefined.  If a symbol is currently known to be common,
417              COFF linkers do not bring in an object file which defines
418              it.  */
419           if (h != (struct bfd_link_hash_entry *) NULL
420               && h->type == bfd_link_hash_undefined)
421             {
422               if (! (*info->callbacks->add_archive_element) (info, abfd, name))
423                 return false;
424               *pneeded = true;
425               return true;
426             }
427         }
428
429       esym += (sym.n_numaux + 1) * symesz;
430     }
431
432   /* We do not need this object file.  */
433   return true;
434 }
435
436 /* Add all the symbols from an object file to the hash table.  */
437
438 static boolean
439 coff_link_add_symbols (abfd, info)
440      bfd *abfd;
441      struct bfd_link_info *info;
442 {
443   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
444   const char *strings;
445   boolean default_copy;
446   bfd_size_type symcount;
447   struct coff_link_hash_entry **sym_hash;
448   bfd_size_type symesz;
449   bfd_byte *esym;
450   bfd_byte *esym_end;
451
452   sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
453   strings = NULL;
454
455   if (info->keep_memory)
456     default_copy = false;
457   else
458     default_copy = true;
459
460   symcount = obj_raw_syment_count (abfd);
461
462   /* We keep a list of the linker hash table entries that correspond
463      to particular symbols.  */
464   sym_hash = ((struct coff_link_hash_entry **)
465               bfd_alloc (abfd,
466                          ((size_t) symcount
467                           * sizeof (struct coff_link_hash_entry *))));
468   if (sym_hash == NULL && symcount != 0)
469     {
470       bfd_set_error (bfd_error_no_memory);
471       return false;
472     }
473   obj_coff_sym_hashes (abfd) = sym_hash;
474   memset (sym_hash, 0,
475           (size_t) symcount * sizeof (struct coff_link_hash_entry *));
476
477   symesz = bfd_coff_symesz (abfd);
478   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
479   esym = (bfd_byte *) obj_coff_external_syms (abfd);
480   esym_end = esym + symcount * symesz;
481   while (esym < esym_end)
482     {
483       struct internal_syment sym;
484       boolean copy;
485
486       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
487
488       if (sym.n_sclass == C_EXT
489           || (sym_is_global && (*sym_is_global) (abfd, &sym)))
490         {
491           const char *name;
492           char buf[SYMNMLEN + 1];
493           flagword flags;
494           asection *section;
495           bfd_vma value;
496
497           /* This symbol is externally visible.  */
498
499           /* FIXME: It's not clear this will work correctly if sizeof
500              (_n_zeroes) != 4.  */
501           copy = default_copy;
502           if (sym._n._n_n._n_zeroes == 0
503               && sym._n._n_n._n_offset != 0)
504             {
505               BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
506               if (strings == NULL)
507                 {
508                   strings = coff_read_string_table (abfd);
509                   if (strings == NULL)
510                     return false;
511                 }
512               name = strings + sym._n._n_n._n_offset;
513             }
514           else
515             {
516               memcpy (buf, sym._n._n_name, SYMNMLEN);
517               buf[SYMNMLEN] = '\0';
518               name = buf;
519               copy = true;
520             }
521
522           value = sym.n_value;
523
524           if (sym.n_scnum == 0)
525             {
526               if (value == 0)
527                 {
528                   flags = 0;
529                   section = bfd_und_section_ptr;
530                 }
531               else
532                 {
533                   flags = BSF_GLOBAL;
534                   section = bfd_com_section_ptr;
535                 }
536             }
537           else
538             {
539               flags = BSF_EXPORT | BSF_GLOBAL;
540               section = coff_section_from_bfd_index (abfd, sym.n_scnum);
541               value -= section->vma;
542             }
543
544           if (! (_bfd_generic_link_add_one_symbol
545                  (info, abfd, name, flags, section, value,
546                   (const char *) NULL, copy, false,
547                   (struct bfd_link_hash_entry **) sym_hash)))
548             return false;
549
550           if (info->hash->creator->flavour == bfd_get_flavour (abfd))
551             {
552               if (((*sym_hash)->class == C_NULL
553                    && (*sym_hash)->type == T_NULL)
554                   || sym.n_scnum != 0
555                   || (sym.n_value != 0
556                       && (*sym_hash)->root.type != bfd_link_hash_defined))
557                 {
558                   (*sym_hash)->class = sym.n_sclass;
559                   (*sym_hash)->type = sym.n_type;
560                   (*sym_hash)->numaux = sym.n_numaux;
561                   (*sym_hash)->auxbfd = abfd;
562                   if (sym.n_numaux != 0)
563                     {
564                       union internal_auxent *alloc;
565                       unsigned int i;
566                       bfd_byte *eaux;
567                       union internal_auxent *iaux;
568
569                       alloc = bfd_hash_allocate (&info->hash->table,
570                                                  (sym.n_numaux
571                                                   * sizeof (*alloc)));
572                       if (alloc == NULL)
573                         {
574                           bfd_set_error (bfd_error_no_memory);
575                           return false;
576                         }
577                       for (i = 0, eaux = esym + symesz, iaux = alloc;
578                            i < sym.n_numaux;
579                            i++, eaux += symesz, iaux++)
580                         bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
581                                               sym.n_sclass, i, sym.n_numaux,
582                                               (PTR) iaux);
583                       (*sym_hash)->aux = alloc;
584                     }
585                 }
586             }
587         }
588
589       esym += (sym.n_numaux + 1) * symesz;
590       sym_hash += sym.n_numaux + 1;
591     }
592
593   return true;
594 }
595
596 /* Do the final link step.  */
597
598 boolean
599 _bfd_coff_final_link (abfd, info)
600      bfd *abfd;
601      struct bfd_link_info *info;
602 {
603   bfd_size_type symesz;
604   struct coff_final_link_info finfo;
605   asection *o;
606   struct bfd_link_order *p;
607   size_t max_contents_size;
608   size_t max_sym_count;
609   size_t max_lineno_count;
610   size_t max_reloc_count;
611   size_t max_output_reloc_count;
612   file_ptr rel_filepos;
613   unsigned int relsz;
614   file_ptr line_filepos;
615   unsigned int linesz;
616   bfd *sub;
617   bfd_byte *external_relocs = NULL;
618   char strbuf[STRING_SIZE_SIZE];
619
620   symesz = bfd_coff_symesz (abfd);
621
622   finfo.info = info;
623   finfo.output_bfd = abfd;
624   finfo.strtab = NULL;
625   finfo.section_info = NULL;
626   finfo.last_file_index = -1;
627   finfo.internal_syms = NULL;
628   finfo.sec_ptrs = NULL;
629   finfo.sym_indices = NULL;
630   finfo.outsyms = NULL;
631   finfo.linenos = NULL;
632   finfo.contents = NULL;
633   finfo.external_relocs = NULL;
634   finfo.internal_relocs = NULL;
635
636   finfo.strtab = _bfd_stringtab_init ();
637   if (finfo.strtab == NULL)
638     goto error_return;
639
640   /* Compute the file positions for all the sections.  */
641   if (! abfd->output_has_begun)
642     bfd_coff_compute_section_file_positions (abfd);
643
644   /* Count the line numbers and relocation entries required for the
645      output file.  Set the file positions for the relocs.  */
646   rel_filepos = obj_relocbase (abfd);
647   relsz = bfd_coff_relsz (abfd);
648   max_contents_size = 0;
649   max_lineno_count = 0;
650   max_reloc_count = 0;
651   for (o = abfd->sections; o != NULL; o = o->next)
652     {
653       o->reloc_count = 0;
654       o->lineno_count = 0;
655       for (p = o->link_order_head; p != NULL; p = p->next)
656         {
657           if (p->type == bfd_indirect_link_order)
658             {
659               asection *sec;
660
661               sec = p->u.indirect.section;
662
663               if (info->strip == strip_none
664                   || info->strip == strip_some)
665                 o->lineno_count += sec->lineno_count;
666
667               if (info->relocateable)
668                 o->reloc_count += sec->reloc_count;
669
670               if (sec->_raw_size > max_contents_size)
671                 max_contents_size = sec->_raw_size;
672               if (sec->lineno_count > max_lineno_count)
673                 max_lineno_count = sec->lineno_count;
674               if (sec->reloc_count > max_reloc_count)
675                 max_reloc_count = sec->reloc_count;
676             }
677           else if (info->relocateable
678                    && (p->type == bfd_section_reloc_link_order
679                        || p->type == bfd_symbol_reloc_link_order))
680             ++o->reloc_count;
681         }
682       if (o->reloc_count == 0)
683         o->rel_filepos = 0;
684       else
685         {
686           o->flags |= SEC_RELOC;
687           o->rel_filepos = rel_filepos;
688           rel_filepos += o->reloc_count * relsz;
689         }
690     }
691
692   /* If doing a relocateable link, allocate space for the pointers we
693      need to keep.  */
694   if (info->relocateable)
695     {
696       unsigned int i;
697
698       finfo.section_info = ((struct coff_link_section_info *)
699                             malloc (abfd->section_count
700                                     * sizeof (struct coff_link_section_info)));
701       if (finfo.section_info == NULL)
702         {
703           bfd_set_error (bfd_error_no_memory);
704           goto error_return;
705         }
706       for (i = 0; i < abfd->section_count; i++)
707         {
708           finfo.section_info[i].relocs = NULL;
709           finfo.section_info[i].rel_hashes = NULL;
710         }
711     }
712
713   /* We now know the size of the relocs, so we can determine the file
714      positions of the line numbers.  */
715   line_filepos = rel_filepos;
716   linesz = bfd_coff_linesz (abfd);
717   max_output_reloc_count = 0;
718   for (o = abfd->sections; o != NULL; o = o->next)
719     {
720       if (o->lineno_count == 0)
721         o->line_filepos = 0;
722       else
723         {
724           o->line_filepos = line_filepos;
725           line_filepos += o->lineno_count * linesz;
726         }
727
728       if (o->reloc_count != 0)
729         {
730           /* We don't know the indices of global symbols until we have
731              written out all the local symbols.  For each section in
732              the output file, we keep an array of pointers to hash
733              table entries.  Each entry in the array corresponds to a
734              reloc.  When we find a reloc against a global symbol, we
735              set the corresponding entry in this array so that we can
736              fix up the symbol index after we have written out all the
737              local symbols.
738
739              Because of this problem, we also keep the relocs in
740              memory until the end of the link.  This wastes memory,
741              but only when doing a relocateable link, which is not the
742              common case.  */
743           BFD_ASSERT (info->relocateable);
744           finfo.section_info[o->target_index].relocs =
745             ((struct internal_reloc *)
746              malloc (o->reloc_count * sizeof (struct internal_reloc)));
747           finfo.section_info[o->target_index].rel_hashes =
748             ((struct coff_link_hash_entry **)
749              malloc (o->reloc_count
750                      * sizeof (struct coff_link_hash_entry *)));
751           if (finfo.section_info[o->target_index].relocs == NULL
752               || finfo.section_info[o->target_index].rel_hashes == NULL)
753             {
754               bfd_set_error (bfd_error_no_memory);
755               goto error_return;
756             }
757
758           if (o->reloc_count > max_output_reloc_count)
759             max_output_reloc_count = o->reloc_count;
760         }
761
762       /* Reset the reloc and lineno counts, so that we can use them to
763          count the number of entries we have output so far.  */
764       o->reloc_count = 0;
765       o->lineno_count = 0;
766     }
767
768   obj_sym_filepos (abfd) = line_filepos;
769
770   /* Figure out the largest number of symbols in an input BFD.  Take
771      the opportunity to clear the output_has_begun fields of all the
772      input BFD's.  */
773   max_sym_count = 0;
774   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
775     {
776       size_t sz;
777
778       sub->output_has_begun = false;
779       sz = obj_raw_syment_count (sub);
780       if (sz > max_sym_count)
781         max_sym_count = sz;
782     }
783
784   /* Allocate some buffers used while linking.  */
785   finfo.internal_syms = ((struct internal_syment *)
786                          malloc (max_sym_count
787                                  * sizeof (struct internal_syment)));
788   finfo.sec_ptrs = (asection **) malloc (max_sym_count * sizeof (asection *));
789   finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
790   finfo.outsyms = (bfd_byte *) malloc ((max_sym_count + 1) * symesz);
791   finfo.linenos = (bfd_byte *) malloc (max_lineno_count
792                                        * bfd_coff_linesz (abfd));
793   finfo.contents = (bfd_byte *) malloc (max_contents_size);
794   finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
795   if (! info->relocateable)
796     finfo.internal_relocs = ((struct internal_reloc *)
797                              malloc (max_reloc_count
798                                      * sizeof (struct internal_reloc)));
799   if ((finfo.internal_syms == NULL && max_sym_count > 0)
800       || (finfo.sec_ptrs == NULL && max_sym_count > 0)
801       || (finfo.sym_indices == NULL && max_sym_count > 0)
802       || finfo.outsyms == NULL
803       || (finfo.linenos == NULL && max_lineno_count > 0)
804       || (finfo.contents == NULL && max_contents_size > 0)
805       || (finfo.external_relocs == NULL && max_reloc_count > 0)
806       || (! info->relocateable
807           && finfo.internal_relocs == NULL
808           && max_reloc_count > 0))
809     {
810       bfd_set_error (bfd_error_no_memory);
811       goto error_return;
812     }
813
814   /* We now know the position of everything in the file, except that
815      we don't know the size of the symbol table and therefore we don't
816      know where the string table starts.  We just build the string
817      table in memory as we go along.  We process all the relocations
818      for a single input file at once.  */
819   obj_raw_syment_count (abfd) = 0;
820   for (o = abfd->sections; o != NULL; o = o->next)
821     {
822       for (p = o->link_order_head; p != NULL; p = p->next)
823         {
824           if (p->type == bfd_indirect_link_order
825               && (bfd_get_flavour (p->u.indirect.section->owner)
826                   == bfd_target_coff_flavour))
827             {
828               sub = p->u.indirect.section->owner;
829               if (! sub->output_has_begun)
830                 {
831                   if (! coff_link_input_bfd (&finfo, sub))
832                     goto error_return;
833                   sub->output_has_begun = true;
834                 }
835             }
836           else if (p->type == bfd_section_reloc_link_order
837                    || p->type == bfd_symbol_reloc_link_order)
838             {
839               if (! coff_reloc_link_order (abfd, &finfo, o, p))
840                 goto error_return;
841             }
842           else
843             {
844               if (! _bfd_default_link_order (abfd, info, o, p))
845                 goto error_return;
846             }
847         }
848     }
849
850   /* Free up the buffers used by coff_link_input_bfd.  */
851   if (finfo.internal_syms != NULL)
852     {
853       free (finfo.internal_syms);
854       finfo.internal_syms = NULL;
855     }
856   if (finfo.sec_ptrs != NULL)
857     {
858       free (finfo.sec_ptrs);
859       finfo.sec_ptrs = NULL;
860     }
861   if (finfo.sym_indices != NULL)
862     {
863       free (finfo.sym_indices);
864       finfo.sym_indices = NULL;
865     }
866   if (finfo.linenos != NULL)
867     {
868       free (finfo.linenos);
869       finfo.linenos = NULL;
870     }
871   if (finfo.contents != NULL)
872     {
873       free (finfo.contents);
874       finfo.contents = NULL;
875     }
876   if (finfo.external_relocs != NULL)
877     {
878       free (finfo.external_relocs);
879       finfo.external_relocs = NULL;
880     }
881   if (finfo.internal_relocs != NULL)
882     {
883       free (finfo.internal_relocs);
884       finfo.internal_relocs = NULL;
885     }
886
887   /* The value of the last C_FILE symbol is supposed to be the symbol
888      index of the first external symbol.  Write it out again if
889      necessary.  */
890   if (finfo.last_file_index != -1
891       && finfo.last_file.n_value != obj_raw_syment_count (abfd))
892     {
893       finfo.last_file.n_value = obj_raw_syment_count (abfd);
894       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
895                              (PTR) finfo.outsyms);
896       if (bfd_seek (abfd,
897                     (obj_sym_filepos (abfd)
898                      + finfo.last_file_index * symesz),
899                     SEEK_SET) != 0
900           || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
901         return false;
902     }
903
904   /* Write out the global symbols.  */
905   finfo.failed = false;
906   coff_link_hash_traverse (coff_hash_table (info), coff_write_global_sym,
907                            (PTR) &finfo);
908   if (finfo.failed)
909     goto error_return;
910
911   /* The outsyms buffer is used by coff_write_global_sym.  */
912   if (finfo.outsyms != NULL)
913     {
914       free (finfo.outsyms);
915       finfo.outsyms = NULL;
916     }
917
918   if (info->relocateable)
919     {
920       /* Now that we have written out all the global symbols, we know
921          the symbol indices to use for relocs against them, and we can
922          finally write out the relocs.  */
923       external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
924       if (external_relocs == NULL)
925         {
926           bfd_set_error (bfd_error_no_memory);
927           goto error_return;
928         }
929
930       for (o = abfd->sections; o != NULL; o = o->next)
931         {
932           struct internal_reloc *irel;
933           struct internal_reloc *irelend;
934           struct coff_link_hash_entry **rel_hash;
935           bfd_byte *erel;
936
937           if (o->reloc_count == 0)
938             continue;
939
940           irel = finfo.section_info[o->target_index].relocs;
941           irelend = irel + o->reloc_count;
942           rel_hash = finfo.section_info[o->target_index].rel_hashes;
943           erel = external_relocs;
944           for (; irel < irelend; irel++, rel_hash++, erel += relsz)
945             {
946               if (*rel_hash != NULL)
947                 {
948                   BFD_ASSERT ((*rel_hash)->indx >= 0);
949                   irel->r_symndx = (*rel_hash)->indx;
950                 }
951               bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
952             }
953
954           if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
955               || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
956                             abfd) != relsz * o->reloc_count)
957             goto error_return;
958         }
959
960       free (external_relocs);
961       external_relocs = NULL;
962     }
963
964   /* Free up the section information.  */
965   if (finfo.section_info != NULL)
966     {
967       unsigned int i;
968
969       for (i = 0; i < abfd->section_count; i++)
970         {
971           if (finfo.section_info[i].relocs != NULL)
972             free (finfo.section_info[i].relocs);
973           if (finfo.section_info[i].rel_hashes != NULL)
974             free (finfo.section_info[i].rel_hashes);
975         }
976       free (finfo.section_info);
977       finfo.section_info = NULL;
978     }
979
980   /* Write out the string table.  */
981   if (bfd_seek (abfd,
982                 (obj_sym_filepos (abfd)
983                  + obj_raw_syment_count (abfd) * symesz),
984                 SEEK_SET) != 0)
985     return false;
986
987 #if STRING_SIZE_SIZE == 4
988   bfd_h_put_32 (abfd,
989                 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
990                 strbuf);
991 #else
992  #error Change bfd_h_put_32
993 #endif
994
995   if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
996     return false;
997
998   if (! _bfd_stringtab_emit (abfd, finfo.strtab))
999     return false;
1000
1001   _bfd_stringtab_free (finfo.strtab);
1002
1003   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1004      not try to write out the symbols.  */
1005   bfd_get_symcount (abfd) = 0;
1006
1007   return true;
1008
1009  error_return:
1010   if (finfo.strtab != NULL)
1011     _bfd_stringtab_free (finfo.strtab);
1012   if (finfo.section_info != NULL)
1013     {
1014       unsigned int i;
1015
1016       for (i = 0; i < abfd->section_count; i++)
1017         {
1018           if (finfo.section_info[i].relocs != NULL)
1019             free (finfo.section_info[i].relocs);
1020           if (finfo.section_info[i].rel_hashes != NULL)
1021             free (finfo.section_info[i].rel_hashes);
1022         }
1023       free (finfo.section_info);
1024     }
1025   if (finfo.internal_syms != NULL)
1026     free (finfo.internal_syms);
1027   if (finfo.sec_ptrs != NULL)
1028     free (finfo.sec_ptrs);
1029   if (finfo.sym_indices != NULL)
1030     free (finfo.sym_indices);
1031   if (finfo.outsyms != NULL)
1032     free (finfo.outsyms);
1033   if (finfo.linenos != NULL)
1034     free (finfo.linenos);
1035   if (finfo.contents != NULL)
1036     free (finfo.contents);
1037   if (finfo.external_relocs != NULL)
1038     free (finfo.external_relocs);
1039   if (finfo.internal_relocs != NULL)
1040     free (finfo.internal_relocs);
1041   if (external_relocs != NULL)
1042     free (external_relocs);
1043   return false;
1044 }
1045
1046 /* Link an input file into the linker output file.  This function
1047    handles all the sections and relocations of the input file at once.  */
1048
1049 static boolean
1050 coff_link_input_bfd (finfo, input_bfd)
1051      struct coff_final_link_info *finfo;
1052      bfd *input_bfd;
1053 {
1054   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
1055   bfd *output_bfd;
1056   const char *strings;
1057   bfd_size_type syment_base;
1058   unsigned int n_tmask;
1059   unsigned int n_btshft;
1060   boolean copy, hash;
1061   bfd_size_type isymesz;
1062   bfd_size_type osymesz;
1063   bfd_size_type linesz;
1064   bfd_byte *esym;
1065   bfd_byte *esym_end;
1066   struct internal_syment *isymp;
1067   asection **secpp;
1068   long *indexp;
1069   long output_index;
1070   bfd_byte *outsym;
1071   struct coff_link_hash_entry **sym_hash;
1072   bfd_size_type relsz;
1073   asection *o;
1074
1075   /* Move all the symbols to the output file.  */
1076
1077   output_bfd = finfo->output_bfd;
1078   sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
1079   strings = NULL;
1080   syment_base = obj_raw_syment_count (output_bfd);
1081   isymesz = bfd_coff_symesz (input_bfd);
1082   osymesz = bfd_coff_symesz (output_bfd);
1083   linesz = bfd_coff_linesz (input_bfd);
1084   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1085
1086   n_tmask = coff_data (input_bfd)->local_n_tmask;
1087   n_btshft = coff_data (input_bfd)->local_n_btshft;
1088
1089   /* Define macros so that ISFCN, et. al., macros work correctly.  */
1090 #define N_TMASK n_tmask
1091 #define N_BTSHFT n_btshft
1092
1093   copy = false;
1094   if (! finfo->info->keep_memory)
1095     copy = true;
1096   hash = true;
1097   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1098     hash = false;
1099
1100   if (! coff_link_get_symbols (input_bfd))
1101     return false;
1102
1103   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1104   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1105   isymp = finfo->internal_syms;
1106   secpp = finfo->sec_ptrs;
1107   indexp = finfo->sym_indices;
1108   output_index = syment_base;
1109   outsym = finfo->outsyms;
1110   while (esym < esym_end)
1111     {
1112       struct internal_syment isym;
1113       boolean skip;
1114       boolean global;
1115       int add;
1116
1117       bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1118
1119       /* Make a copy of *isymp so that the relocate_section function
1120          always sees the original values.  This is more reliable than
1121          always recomputing the symbol value even if we are stripping
1122          the symbol.  */
1123       isym = *isymp;
1124
1125       *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1126       *indexp = -1;
1127
1128       skip = false;
1129       global = false;
1130       add = 1 + isym.n_numaux;
1131
1132       /* If we are stripping all symbols, we want to skip this one.  */
1133       if (finfo->info->strip == strip_all)
1134         skip = true;
1135
1136       if (! skip)
1137         {
1138           if (isym.n_sclass == C_EXT
1139               || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
1140             {
1141               /* This is a global symbol.  Global symbols come at the
1142                  end of the symbol table, so skip them for now.
1143                  Function symbols, however, are an exception, and are
1144                  not moved to the end.  */
1145               global = true;
1146               if (! ISFCN (isym.n_type))
1147                 skip = true;
1148             }
1149           else
1150             {
1151               /* This is a local symbol.  Skip it if we are discarding
1152                  local symbols.  */
1153               if (finfo->info->discard == discard_all)
1154                 skip = true;
1155             }
1156         }
1157
1158       /* If we stripping debugging symbols, and this is a debugging
1159          symbol, then skip it.  */
1160       if (! skip
1161           && finfo->info->strip == strip_debugger
1162           && isym.n_scnum == N_DEBUG)
1163         skip = true;
1164
1165       /* If some symbols are stripped based on the name, work out the
1166          name and decide whether to skip this symbol.  */
1167       if (! skip
1168           && (finfo->info->strip == strip_some
1169               || finfo->info->discard == discard_l))
1170         {
1171           const char *name;
1172           char buf[SYMNMLEN + 1];
1173
1174           if (isym._n._n_n._n_zeroes == 0
1175               && isym._n._n_n._n_offset != 0)
1176             {
1177               if (strings == NULL)
1178                 {
1179                   strings = coff_read_string_table (input_bfd);
1180                   if (strings == NULL)
1181                     return false;
1182                 }
1183               name = strings + isym._n._n_n._n_offset;
1184             }
1185           else
1186             {
1187               memcpy (buf, isym._n._n_name, SYMNMLEN);
1188               buf[SYMNMLEN] = '\0';
1189               name = buf;
1190             }
1191
1192           if ((finfo->info->strip == strip_some
1193                && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1194                                     false) == NULL))
1195               || (! global
1196                   && finfo->info->discard == discard_l
1197                   && strncmp (name, finfo->info->lprefix,
1198                               finfo->info->lprefix_len) == 0))
1199             skip = true;
1200         }
1201
1202       /* We now know whether we are to skip this symbol or not.  */
1203       if (! skip)
1204         {
1205           /* Adjust the symbol in order to output it.  */
1206
1207           if (isym._n._n_n._n_zeroes == 0
1208               && isym._n._n_n._n_offset != 0)
1209             {
1210               const char *name;
1211               bfd_size_type indx;
1212
1213               /* This symbol has a long name.  Enter it in the string
1214                  table we are building.  Note that we do not check
1215                  bfd_coff_symname_in_debug.  That is only true for
1216                  XCOFF, and XCOFF requires different linking code
1217                  anyhow.  */
1218               BFD_ASSERT (isym._n._n_n._n_offset >= STRING_SIZE_SIZE);
1219               if (strings == NULL)
1220                 {
1221                   strings = coff_read_string_table (input_bfd);
1222                   if (strings == NULL)
1223                     return false;
1224                 }
1225               name = strings + isym._n._n_n._n_offset;
1226               indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1227               if (indx == (bfd_size_type) -1)
1228                 return false;
1229               isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1230             }
1231
1232           if (isym.n_scnum > 0)
1233             {
1234               isym.n_scnum = (*secpp)->output_section->target_index;
1235               isym.n_value += ((*secpp)->output_section->vma
1236                                + (*secpp)->output_offset
1237                                - (*secpp)->vma);
1238             }
1239
1240           /* The value of a C_FILE symbol is the symbol index of the
1241              next C_FILE symbol.  The value of the last C_FILE symbol
1242              is the symbol index to the first external symbol
1243              (actually, coff_renumber_symbols does not get this
1244              right--it just sets the value of the last C_FILE symbol
1245              to zero--and nobody has ever complained about it).  We
1246              try to get this right, below, just before we write the
1247              symbols out, but in the general case we may have to write
1248              the symbol out twice.  */
1249           if (isym.n_sclass == C_FILE)
1250             {
1251               if (finfo->last_file_index != -1
1252                   && finfo->last_file.n_value != output_index)
1253                 {
1254                   /* We must correct the value of the last C_FILE entry.  */
1255                   finfo->last_file.n_value = output_index;
1256                   if (finfo->last_file_index >= syment_base)
1257                     {
1258                       /* The last C_FILE symbol is in this input file.  */
1259                       bfd_coff_swap_sym_out (output_bfd,
1260                                              (PTR) &finfo->last_file,
1261                                              (PTR) (finfo->outsyms
1262                                                     + ((finfo->last_file_index
1263                                                         - syment_base)
1264                                                        * osymesz)));
1265                     }
1266                   else
1267                     {
1268                       /* We have already written out the last C_FILE
1269                          symbol.  We need to write it out again.  We
1270                          borrow *outsym temporarily.  */
1271                       bfd_coff_swap_sym_out (output_bfd,
1272                                              (PTR) &finfo->last_file,
1273                                              (PTR) outsym);
1274                       if (bfd_seek (output_bfd,
1275                                     (obj_sym_filepos (output_bfd)
1276                                      + finfo->last_file_index * osymesz),
1277                                     SEEK_SET) != 0
1278                           || (bfd_write (outsym, osymesz, 1, output_bfd)
1279                               != osymesz))
1280                         return false;
1281                     }
1282                 }
1283
1284               finfo->last_file_index = output_index;
1285               finfo->last_file = isym;
1286             }
1287
1288           /* Output the symbol.  */
1289
1290           bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1291
1292           *indexp = output_index;
1293
1294           if (global)
1295             {
1296               long indx;
1297               struct coff_link_hash_entry *h;
1298
1299               indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1300                       / isymesz);
1301               h = obj_coff_sym_hashes (input_bfd)[indx];
1302               BFD_ASSERT (h != NULL);
1303               h->indx = output_index;
1304             }
1305
1306           output_index += add;
1307           outsym += add * osymesz;
1308         }
1309
1310       esym += add * isymesz;
1311       isymp += add;
1312       ++secpp;
1313       ++indexp;
1314       for (--add; add > 0; --add)
1315         {
1316           *secpp++ = NULL;
1317           *indexp++ = -1;
1318         }
1319     }
1320
1321   /* Fix up the aux entries.  This must be done in a separate pass,
1322      because we don't know the correct symbol indices until we have
1323      already decided which symbols we are going to keep.  */
1324
1325   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1326   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1327   isymp = finfo->internal_syms;
1328   indexp = finfo->sym_indices;
1329   sym_hash = obj_coff_sym_hashes (input_bfd);
1330   outsym = finfo->outsyms;
1331   while (esym < esym_end)
1332     {
1333       int add;
1334
1335       add = 1 + isymp->n_numaux;
1336
1337       if (*indexp < 0
1338           && (*sym_hash == NULL
1339               || (*sym_hash)->auxbfd != input_bfd))
1340         esym += add * isymesz;
1341       else
1342         {
1343           struct coff_link_hash_entry *h;
1344           int i;
1345
1346           h = NULL;
1347           if (*indexp < 0)
1348             {
1349               h = *sym_hash;
1350               BFD_ASSERT (h->numaux == isymp->n_numaux);
1351             }
1352
1353           esym += isymesz;
1354
1355           if (h == NULL)
1356             outsym += osymesz;
1357
1358           /* Handle the aux entries.  This handling is based on
1359              coff_pointerize_aux.  I don't know if it always correct.  */
1360           for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1361             {
1362               union internal_auxent aux;
1363               union internal_auxent *auxp;
1364
1365               if (h != NULL)
1366                 auxp = h->aux + i;
1367               else
1368                 {
1369                   bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1370                                         isymp->n_sclass, i, isymp->n_numaux,
1371                                         (PTR) &aux);
1372                   auxp = &aux;
1373                 }
1374
1375               if (isymp->n_sclass == C_FILE)
1376                 {
1377                   /* If this is a long filename, we must put it in the
1378                      string table.  */
1379                   if (auxp->x_file.x_n.x_zeroes == 0)
1380                     {
1381                       const char *filename;
1382                       bfd_size_type indx;
1383
1384                       BFD_ASSERT (auxp->x_file.x_n.x_offset
1385                                   >= STRING_SIZE_SIZE);
1386                       if (strings == NULL)
1387                         {
1388                           strings = coff_read_string_table (input_bfd);
1389                           if (strings == NULL)
1390                             return false;
1391                         }
1392                       filename = strings + auxp->x_file.x_n.x_offset;
1393                       indx = _bfd_stringtab_add (finfo->strtab, filename,
1394                                                  hash, copy);
1395                       if (indx == (bfd_size_type) -1)
1396                         return false;
1397                       auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1398                     }
1399                 }
1400               else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1401                 {
1402                   long indx;
1403
1404                   if (ISFCN (isymp->n_type)
1405                       || ISTAG (isymp->n_sclass)
1406                       || isymp->n_sclass == C_BLOCK)
1407                     {
1408                       indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1409                       if (indx > 0
1410                           && indx < obj_raw_syment_count (input_bfd))
1411                         {
1412                           /* We look forward through the symbol for
1413                              the index of the next symbol we are going
1414                              to include.  I don't know if this is
1415                              entirely right.  */
1416                           while (finfo->sym_indices[indx] < 0
1417                                  && indx < obj_raw_syment_count (input_bfd))
1418                             ++indx;
1419                           if (indx >= obj_raw_syment_count (input_bfd))
1420                             indx = output_index;
1421                           else
1422                             indx = finfo->sym_indices[indx];
1423                           auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1424                         }
1425                     }
1426
1427                   indx = auxp->x_sym.x_tagndx.l;
1428                   if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1429                     {
1430                       indx = finfo->sym_indices[indx];
1431                       if (indx < 0)
1432                         auxp->x_sym.x_tagndx.l = 0;
1433                       else
1434                         auxp->x_sym.x_tagndx.l = indx;
1435                     }
1436                 }
1437
1438               if (h == NULL)
1439                 {
1440                   bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1441                                          isymp->n_sclass, i, isymp->n_numaux,
1442                                          (PTR) outsym);
1443                   outsym += osymesz;
1444                 }
1445
1446               esym += isymesz;
1447             }
1448         }
1449
1450       indexp += add;
1451       isymp += add;
1452       sym_hash += add;
1453     }
1454
1455   /* Relocate the line numbers, unless we are stripping them.  */
1456   if (finfo->info->strip == strip_none
1457       || finfo->info->strip == strip_some)
1458     {
1459       for (o = input_bfd->sections; o != NULL; o = o->next)
1460         {
1461           bfd_vma offset;
1462           bfd_byte *eline;
1463           bfd_byte *elineend;
1464
1465           if (o->lineno_count == 0)
1466             continue;
1467
1468           if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1469               || bfd_read (finfo->linenos, linesz, o->lineno_count,
1470                            input_bfd) != linesz * o->lineno_count)
1471             return false;
1472
1473           offset = o->output_section->vma + o->output_offset - o->vma;
1474           eline = finfo->linenos;
1475           elineend = eline + linesz * o->lineno_count;
1476           for (; eline < elineend; eline += linesz)
1477             {
1478               struct internal_lineno iline;
1479
1480               bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1481
1482               if (iline.l_lnno != 0)
1483                 iline.l_addr.l_paddr += offset;
1484               else if (iline.l_addr.l_symndx >= 0
1485                        && (iline.l_addr.l_symndx
1486                            < obj_raw_syment_count (input_bfd)))
1487                 {
1488                   long indx;
1489
1490                   indx = finfo->sym_indices[iline.l_addr.l_symndx];
1491
1492                   if (indx < 0)
1493                     {
1494                       /* These line numbers are attached to a symbol
1495                          which we are stripping.  We should really
1496                          just discard the line numbers, but that would
1497                          be a pain because we have already counted
1498                          them.  */
1499                       indx = 0;
1500                     }
1501                   else
1502                     {
1503                       struct internal_syment is;
1504                       union internal_auxent ia;
1505
1506                       /* Fix up the lnnoptr field in the aux entry of
1507                          the symbol.  It turns out that we can't do
1508                          this when we modify the symbol aux entries,
1509                          because gas sometimes screws up the lnnoptr
1510                          field and makes it an offset from the start
1511                          of the line numbers rather than an absolute
1512                          file index.  */
1513                       bfd_coff_swap_sym_in (output_bfd,
1514                                             (PTR) (finfo->outsyms
1515                                                    + ((indx - syment_base)
1516                                                       * osymesz)),
1517                                             (PTR) &is);
1518                       if ((ISFCN (is.n_type)
1519                            || is.n_sclass == C_BLOCK)
1520                           && is.n_numaux >= 1)
1521                         {
1522                           PTR auxptr;
1523
1524                           auxptr = (PTR) (finfo->outsyms
1525                                           + ((indx - syment_base + 1)
1526                                              * osymesz));
1527                           bfd_coff_swap_aux_in (output_bfd, auxptr,
1528                                                 is.n_type, is.n_sclass,
1529                                                 0, is.n_numaux, (PTR) &ia);
1530                           ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
1531                             (o->output_section->line_filepos
1532                              + o->output_section->lineno_count * linesz
1533                              + eline - finfo->linenos);
1534                           bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
1535                                                  is.n_type, is.n_sclass, 0,
1536                                                  is.n_numaux, auxptr);
1537                         }
1538                     }
1539
1540                   iline.l_addr.l_symndx = indx;
1541                 }
1542
1543               bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1544             }
1545
1546           if (bfd_seek (output_bfd,
1547                         (o->output_section->line_filepos
1548                          + o->output_section->lineno_count * linesz),
1549                         SEEK_SET) != 0
1550               || bfd_write (finfo->linenos, linesz, o->lineno_count,
1551                             output_bfd) != linesz * o->lineno_count)
1552             return false;
1553
1554           o->output_section->lineno_count += o->lineno_count;
1555         }
1556     }
1557
1558   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1559      symbol will be the first symbol in the next input file.  In the
1560      normal case, this will save us from writing out the C_FILE symbol
1561      again.  */
1562   if (finfo->last_file_index >= syment_base)
1563     {
1564       finfo->last_file.n_value = output_index;
1565       bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
1566                              (PTR) (finfo->outsyms
1567                                     + ((finfo->last_file_index - syment_base)
1568                                        * osymesz)));
1569     }
1570
1571   /* Write the modified symbols to the output file.  */
1572   if (outsym > finfo->outsyms)
1573     {
1574       if (bfd_seek (output_bfd,
1575                     obj_sym_filepos (output_bfd) + syment_base * osymesz,
1576                     SEEK_SET) != 0
1577           || bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1578                         output_bfd) != outsym - finfo->outsyms)
1579         return false;
1580
1581       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1582                    + (outsym - finfo->outsyms) / osymesz)
1583                   == output_index);
1584
1585       obj_raw_syment_count (output_bfd) = output_index;
1586     }
1587
1588   /* Relocate the contents of each section.  */
1589   relsz = bfd_coff_relsz (input_bfd);
1590   for (o = input_bfd->sections; o != NULL; o = o->next)
1591     {
1592       if ((o->flags & SEC_HAS_CONTENTS) == 0)
1593         continue;
1594
1595       if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1596                                       (file_ptr) 0, o->_raw_size))
1597         return false;
1598
1599       if ((o->flags & SEC_RELOC) != 0)
1600         {
1601           int target_index;
1602           struct internal_reloc *internal_relocs;
1603           bfd_byte *erel;
1604           bfd_byte *erel_end;
1605           struct internal_reloc *irel;
1606
1607           /* Read in the relocs.  */
1608           if (bfd_seek (input_bfd, o->rel_filepos, SEEK_SET) != 0
1609               || (bfd_read (finfo->external_relocs, relsz, o->reloc_count,
1610                             input_bfd) != relsz * o->reloc_count))
1611             return false;
1612
1613           /* If we are doing a relocateable link, we keep the swapped
1614              in relocs in memory, and don't write them out until the
1615              end of the link.  */
1616           target_index = o->output_section->target_index;
1617           if (! finfo->info->relocateable)
1618             internal_relocs = finfo->internal_relocs;
1619           else
1620             internal_relocs = (finfo->section_info[target_index].relocs
1621                                + o->output_section->reloc_count);
1622
1623           /* Swap in the relocs.  */
1624           erel = finfo->external_relocs;
1625           erel_end = erel + relsz * o->reloc_count;
1626           irel = internal_relocs;
1627           for (; erel < erel_end; erel += relsz, irel++)
1628             bfd_coff_swap_reloc_in (input_bfd, (PTR) erel, (PTR) irel);
1629
1630           /* Call processor specific code to relocate the section
1631              contents.  */
1632           if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1633                                            input_bfd, o,
1634                                            finfo->contents,
1635                                            internal_relocs,
1636                                            finfo->internal_syms,
1637                                            finfo->sec_ptrs))
1638             return false;
1639
1640           if (finfo->info->relocateable)
1641             {
1642               bfd_vma offset;
1643               struct internal_reloc *irelend;
1644               struct coff_link_hash_entry **rel_hash;
1645
1646               offset = o->output_section->vma + o->output_offset - o->vma;
1647
1648               irel = internal_relocs;
1649               irelend = irel + o->reloc_count;
1650               rel_hash = finfo->section_info[target_index].rel_hashes;
1651               for (; irel < irelend; irel++, rel_hash++)
1652                 {
1653                   struct coff_link_hash_entry *h;
1654
1655                   *rel_hash = NULL;
1656
1657                   /* Adjust the reloc address and symbol index.  */
1658
1659                   irel->r_vaddr += offset;
1660
1661                   h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
1662                   if (h != NULL)
1663                     {
1664                       /* This is a global symbol.  */
1665                       if (h->indx >= 0)
1666                         irel->r_symndx = h->indx;
1667                       else
1668                         {
1669                           /* This symbol is being written at the end
1670                              of the file, and we do not yet know the
1671                              symbol index.  We save the pointer to the
1672                              hash table entry in the rel_hash list.
1673                              We set the indx field to -2 to indicate
1674                              that this symbol must not be stripped.  */
1675                           *rel_hash = h;
1676                           h->indx = -2;
1677                         }
1678                     }
1679                   else
1680                     {
1681                       long indx;
1682
1683                       indx = finfo->sym_indices[irel->r_symndx];
1684                       if (indx != -1)
1685                         irel->r_symndx = indx;
1686                       else
1687                         {
1688                           struct internal_syment *is;
1689                           const char *name;
1690                           char buf[SYMNMLEN + 1];
1691
1692                           /* This reloc is against a symbol we are
1693                              stripping.  It would be possible to
1694                              handle this case, but I don't think it's
1695                              worth it.  */
1696                           is = finfo->internal_syms + irel->r_symndx;
1697
1698                           if (is->_n._n_n._n_zeroes == 0
1699                               && is->_n._n_n._n_offset != 0)
1700                             {
1701                               if (strings == NULL)
1702                                 {
1703                                   strings = coff_read_string_table (input_bfd);
1704                                   if (strings == NULL)
1705                                     return false;
1706                                 }
1707                               name = strings + is->_n._n_n._n_offset;
1708                             }
1709                           else
1710                             {
1711                               memcpy (buf, is->_n._n_name, SYMNMLEN);
1712                               buf[SYMNMLEN] = '\0';
1713                               name = buf;
1714                             }
1715
1716                           if (! ((*finfo->info->callbacks->unattached_reloc)
1717                                  (finfo->info, name, input_bfd, o,
1718                                   irel->r_vaddr)))
1719                             return false;
1720                         }
1721                     }
1722                 }
1723
1724               o->output_section->reloc_count += o->reloc_count;
1725             }
1726         }
1727
1728       /* Write out the modified section contents.  */
1729       if (! bfd_set_section_contents (output_bfd, o->output_section,
1730                                       finfo->contents, o->output_offset,
1731                                       (o->_cooked_size != 0
1732                                        ? o->_cooked_size
1733                                        : o->_raw_size)))
1734         return false;
1735     }
1736
1737   if (! finfo->info->keep_memory)
1738     {
1739       if (! coff_link_free_symbols (input_bfd))
1740         return false;
1741     }
1742
1743   return true;
1744 }
1745
1746 /* Write out a global symbol.  Called via coff_link_hash_traverse.  */
1747
1748 static boolean
1749 coff_write_global_sym (h, data)
1750      struct coff_link_hash_entry *h;
1751      PTR data;
1752 {
1753   struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
1754   bfd *output_bfd;
1755   struct internal_syment isym;
1756   bfd_size_type symesz;
1757   unsigned int i;
1758
1759   output_bfd = finfo->output_bfd;
1760
1761   if (h->indx >= 0)
1762     return true;
1763
1764   if (h->indx != -2
1765       && (finfo->info->strip == strip_all
1766           || (finfo->info->strip == strip_some
1767               && (bfd_hash_lookup (finfo->info->keep_hash,
1768                                    h->root.root.string, false, false)
1769                   == NULL))))
1770     return true;
1771
1772   switch (h->root.type)
1773     {
1774     default:
1775     case bfd_link_hash_new:
1776       abort ();
1777       return false;
1778
1779     case bfd_link_hash_undefined:
1780     case bfd_link_hash_weak:
1781       isym.n_scnum = N_UNDEF;
1782       isym.n_value = 0;
1783       break;
1784
1785     case bfd_link_hash_defined:
1786       {
1787         asection *sec;
1788
1789         sec = h->root.u.def.section->output_section;
1790         if (bfd_is_abs_section (sec))
1791           isym.n_scnum = N_ABS;
1792         else
1793           isym.n_scnum = sec->target_index;
1794         isym.n_value = (h->root.u.def.value
1795                         + sec->vma
1796                         + h->root.u.def.section->output_offset);
1797       }
1798       break;
1799
1800     case bfd_link_hash_common:
1801       isym.n_scnum = N_UNDEF;
1802       isym.n_value = h->root.u.c.size;
1803       break;
1804
1805     case bfd_link_hash_indirect:
1806     case bfd_link_hash_warning:
1807       /* Just ignore these.  They can't be handled anyhow.  */
1808       return true;
1809     }
1810
1811   if (strlen (h->root.root.string) <= SYMNMLEN)
1812     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
1813   else
1814     {
1815       boolean hash;
1816       bfd_size_type indx;
1817
1818       hash = true;
1819       if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1820         hash = false;
1821       indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
1822                                  false);
1823       if (indx == (bfd_size_type) -1)
1824         {
1825           finfo->failed = true;
1826           return false;
1827         }
1828       isym._n._n_n._n_zeroes = 0;
1829       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1830     }
1831
1832   isym.n_sclass = h->class;
1833   isym.n_type = h->type;
1834
1835   if (isym.n_sclass == C_NULL)
1836     isym.n_sclass = C_EXT;
1837
1838   isym.n_numaux = h->numaux;
1839   
1840   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
1841
1842   symesz = bfd_coff_symesz (output_bfd);
1843
1844   if (bfd_seek (output_bfd,
1845                 (obj_sym_filepos (output_bfd)
1846                  + obj_raw_syment_count (output_bfd) * symesz),
1847                 SEEK_SET) != 0
1848       || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
1849     {
1850       finfo->failed = true;
1851       return false;
1852     }
1853
1854   h->indx = obj_raw_syment_count (output_bfd);
1855
1856   ++obj_raw_syment_count (output_bfd);
1857
1858   /* Write out any associated aux entries.  There normally will be
1859      none.  If there are any, I have no idea how to modify them.  */
1860   for (i = 0; i < isym.n_numaux; i++)
1861     {
1862       bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
1863                              isym.n_sclass, i, isym.n_numaux,
1864                              (PTR) finfo->outsyms);
1865       if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
1866         {
1867           finfo->failed = true;
1868           return false;
1869         }
1870       ++obj_raw_syment_count (output_bfd);
1871     }
1872
1873   return true;
1874 }
1875
1876 /* Handle a link order which is supposed to generate a reloc.  */
1877
1878 static boolean
1879 coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
1880      bfd *output_bfd;
1881      struct coff_final_link_info *finfo;
1882      asection *output_section;
1883      struct bfd_link_order *link_order;
1884 {
1885   const reloc_howto_type *howto;
1886   struct internal_reloc *irel;
1887   struct coff_link_hash_entry **rel_hash_ptr;
1888
1889   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
1890   if (howto == NULL)
1891     {
1892       bfd_set_error (bfd_error_bad_value);
1893       return false;
1894     }
1895
1896   if (link_order->u.reloc.p->addend != 0)
1897     {
1898       bfd_size_type size;
1899       bfd_byte *buf;
1900       bfd_reloc_status_type rstat;
1901       boolean ok;
1902
1903       size = bfd_get_reloc_size (howto);
1904       buf = (bfd_byte *) bfd_zmalloc (size);
1905       if (buf == NULL)
1906         {
1907           bfd_set_error (bfd_error_no_memory);
1908           return false;
1909         }
1910
1911       rstat = _bfd_relocate_contents (howto, output_bfd,
1912                                       link_order->u.reloc.p->addend, buf);
1913       switch (rstat)
1914         {
1915         case bfd_reloc_ok:
1916           break;
1917         default:
1918         case bfd_reloc_outofrange:
1919           abort ();
1920         case bfd_reloc_overflow:
1921           if (! ((*finfo->info->callbacks->reloc_overflow)
1922                  (finfo->info,
1923                   (link_order->type == bfd_section_reloc_link_order
1924                    ? bfd_section_name (output_bfd,
1925                                        link_order->u.reloc.p->u.section)
1926                    : link_order->u.reloc.p->u.name),
1927                   howto->name, link_order->u.reloc.p->addend,
1928                   (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
1929             {
1930               free (buf);
1931               return false;
1932             }
1933           break;
1934         }
1935       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
1936                                      (file_ptr) link_order->offset, size);
1937       free (buf);
1938       if (! ok)
1939         return false;
1940     }
1941
1942   /* Store the reloc information in the right place.  It will get
1943      swapped and written out at the end of the final_link routine.  */
1944
1945   irel = (finfo->section_info[output_section->target_index].relocs
1946           + output_section->reloc_count);
1947   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
1948                   + output_section->reloc_count);
1949
1950   memset (irel, 0, sizeof (struct internal_reloc));
1951   *rel_hash_ptr = NULL;
1952
1953   irel->r_vaddr = output_section->vma + link_order->offset;
1954
1955   if (link_order->type == bfd_section_reloc_link_order)
1956     {
1957       /* We need to somehow locate a symbol in the right section.  The
1958          symbol must either have a value of zero, or we must adjust
1959          the addend by the value of the symbol.  FIXME: Write this
1960          when we need it.  The old linker couldn't handle this anyhow.  */
1961       abort ();
1962       *rel_hash_ptr = NULL;
1963       irel->r_symndx = 0;
1964     }
1965   else
1966     {
1967       struct coff_link_hash_entry *h;
1968
1969       h = coff_link_hash_lookup (coff_hash_table (finfo->info),
1970                                  link_order->u.reloc.p->u.name,
1971                                  false, false, true);
1972       if (h != NULL)
1973         {
1974           if (h->indx >= 0)
1975             irel->r_symndx = h->indx;
1976           else
1977             {
1978               /* Set the index to -2 to force this symbol to get
1979                  written out.  */
1980               h->indx = -2;
1981               *rel_hash_ptr = h;
1982               irel->r_symndx = 0;
1983             }
1984         }
1985       else
1986         {
1987           if (! ((*finfo->info->callbacks->unattached_reloc)
1988                  (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
1989                   (asection *) NULL, (bfd_vma) 0)))
1990             return false;
1991           irel->r_symndx = 0;
1992         }
1993     }
1994
1995   /* FIXME: Is this always right?  */
1996   irel->r_type = howto->type;
1997
1998   /* r_size is only used on the RS/6000, which needs its own linker
1999      routines anyhow.  r_extern is only used for ECOFF.  */
2000
2001   /* FIXME: What is the right value for r_offset?  Is zero OK?  */
2002
2003   ++output_section->reloc_count;
2004
2005   return true;
2006 }