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