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