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