cofflink.c revision 218822
189099Sfjoe/* COFF specific linker code.
289099Sfjoe   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3119418Sobrien   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4119418Sobrien   Written by Ian Lance Taylor, Cygnus Support.
5119418Sobrien
689099Sfjoe   This file is part of BFD, the Binary File Descriptor library.
789099Sfjoe
889099Sfjoe   This program is free software; you can redistribute it and/or modify
989099Sfjoe   it under the terms of the GNU General Public License as published by
1089099Sfjoe   the Free Software Foundation; either version 2 of the License, or
1189099Sfjoe   (at your option) any later version.
1289099Sfjoe
1389099Sfjoe   This program is distributed in the hope that it will be useful,
1489099Sfjoe   but WITHOUT ANY WARRANTY; without even the implied warranty of
1589099Sfjoe   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1689099Sfjoe   GNU General Public License for more details.
1789099Sfjoe
1889099Sfjoe   You should have received a copy of the GNU General Public License
1989099Sfjoe   along with this program; if not, write to the Free Software
2089099Sfjoe   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2189099Sfjoe
2289099Sfjoe/* This file contains the COFF backend linker code.  */
2389099Sfjoe
2489099Sfjoe#include "sysdep.h"
2589099Sfjoe#include "bfd.h"
2689099Sfjoe#include "bfdlink.h"
2789099Sfjoe#include "libbfd.h"
2889099Sfjoe#include "coff/internal.h"
2989099Sfjoe#include "libcoff.h"
3089099Sfjoe#include "safe-ctype.h"
3189099Sfjoe
3289099Sfjoestatic bfd_boolean coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info);
3389099Sfjoestatic bfd_boolean coff_link_check_archive_element (bfd *abfd, struct bfd_link_info *info, bfd_boolean *pneeded);
3489099Sfjoestatic bfd_boolean coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info);
3589099Sfjoe
3689099Sfjoe/* Return TRUE if SYM is a weak, external symbol.  */
3789099Sfjoe#define IS_WEAK_EXTERNAL(abfd, sym)			\
3889099Sfjoe  ((sym).n_sclass == C_WEAKEXT				\
3989099Sfjoe   || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
4089099Sfjoe
4189099Sfjoe/* Return TRUE if SYM is an external symbol.  */
42109771Sfjoe#define IS_EXTERNAL(abfd, sym)				\
4389099Sfjoe  ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
4489099Sfjoe
4589099Sfjoe/* Define macros so that the ISFCN, et. al., macros work correctly.
4689099Sfjoe   These macros are defined in include/coff/internal.h in terms of
4789099Sfjoe   N_TMASK, etc.  These definitions require a user to define local
4889099Sfjoe   variables with the appropriate names, and with values from the
4989099Sfjoe   coff_data (abfd) structure.  */
5089099Sfjoe
5189099Sfjoe#define N_TMASK n_tmask
5289099Sfjoe#define N_BTSHFT n_btshft
5389099Sfjoe#define N_BTMASK n_btmask
5489099Sfjoe
5589099Sfjoe/* Create an entry in a COFF linker hash table.  */
5689099Sfjoe
5789099Sfjoestruct bfd_hash_entry *
5889099Sfjoe_bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
59257176Sglebius			     struct bfd_hash_table *table,
6089099Sfjoe			     const char *string)
6189099Sfjoe{
6289099Sfjoe  struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
6389099Sfjoe
6489099Sfjoe  /* Allocate the structure if it has not already been allocated by a
6589099Sfjoe     subclass.  */
6689099Sfjoe  if (ret == (struct coff_link_hash_entry *) NULL)
6789099Sfjoe    ret = ((struct coff_link_hash_entry *)
6889099Sfjoe	   bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
6989099Sfjoe  if (ret == (struct coff_link_hash_entry *) NULL)
7089099Sfjoe    return (struct bfd_hash_entry *) ret;
7189099Sfjoe
7289099Sfjoe  /* Call the allocation method of the superclass.  */
7389099Sfjoe  ret = ((struct coff_link_hash_entry *)
7489099Sfjoe	 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
7589099Sfjoe				 table, string));
7689099Sfjoe  if (ret != (struct coff_link_hash_entry *) NULL)
7789099Sfjoe    {
7889099Sfjoe      /* Set local fields.  */
7989099Sfjoe      ret->indx = -1;
8089099Sfjoe      ret->type = T_NULL;
8189099Sfjoe      ret->class = C_NULL;
8289099Sfjoe      ret->numaux = 0;
8389099Sfjoe      ret->auxbfd = NULL;
8489099Sfjoe      ret->aux = NULL;
8589099Sfjoe    }
8689099Sfjoe
8789099Sfjoe  return (struct bfd_hash_entry *) ret;
8889099Sfjoe}
8989099Sfjoe
9089099Sfjoe/* Initialize a COFF linker hash table.  */
9189099Sfjoe
9289099Sfjoebfd_boolean
9389099Sfjoe_bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
9489099Sfjoe				bfd *abfd,
9589099Sfjoe				struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
9689099Sfjoe								   struct bfd_hash_table *,
9789099Sfjoe								   const char *),
9889099Sfjoe				unsigned int entsize)
9989099Sfjoe{
10089099Sfjoe  memset (&table->stab_info, 0, sizeof (table->stab_info));
10189099Sfjoe  return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
10289099Sfjoe}
10389099Sfjoe
10489099Sfjoe/* Create a COFF linker hash table.  */
105148887Srwatson
10689099Sfjoestruct bfd_link_hash_table *
10789099Sfjoe_bfd_coff_link_hash_table_create (bfd *abfd)
108159529Sfjoe{
10989099Sfjoe  struct coff_link_hash_table *ret;
11089099Sfjoe  bfd_size_type amt = sizeof (struct coff_link_hash_table);
11189099Sfjoe
11289099Sfjoe  ret = bfd_malloc (amt);
11389099Sfjoe  if (ret == NULL)
11489099Sfjoe    return NULL;
11592739Salfred
116159529Sfjoe  if (! _bfd_coff_link_hash_table_init (ret, abfd,
117159529Sfjoe					_bfd_coff_link_hash_newfunc,
11892739Salfred					sizeof (struct coff_link_hash_entry)))
119159529Sfjoe    {
12092739Salfred      free (ret);
121199559Sjhb      return (struct bfd_link_hash_table *) NULL;
122159529Sfjoe    }
123159529Sfjoe  return &ret->root;
124159529Sfjoe}
12589099Sfjoe
12689099Sfjoe/* Create an entry in a COFF debug merge hash table.  */
12789099Sfjoe
12889099Sfjoestruct bfd_hash_entry *
12989099Sfjoe_bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
13089099Sfjoe				    struct bfd_hash_table *table,
13189099Sfjoe				    const char *string)
13289099Sfjoe{
13389099Sfjoe  struct coff_debug_merge_hash_entry *ret =
13489099Sfjoe    (struct coff_debug_merge_hash_entry *) entry;
135159529Sfjoe
13689099Sfjoe  /* Allocate the structure if it has not already been allocated by a
137159529Sfjoe     subclass.  */
138159529Sfjoe  if (ret == (struct coff_debug_merge_hash_entry *) NULL)
13989099Sfjoe    ret = ((struct coff_debug_merge_hash_entry *)
140159529Sfjoe	   bfd_hash_allocate (table,
14189099Sfjoe			      sizeof (struct coff_debug_merge_hash_entry)));
142159529Sfjoe  if (ret == (struct coff_debug_merge_hash_entry *) NULL)
143159529Sfjoe    return (struct bfd_hash_entry *) ret;
14489099Sfjoe
145159529Sfjoe  /* Call the allocation method of the superclass.  */
14689099Sfjoe  ret = ((struct coff_debug_merge_hash_entry *)
147159529Sfjoe	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
148159529Sfjoe  if (ret != (struct coff_debug_merge_hash_entry *) NULL)
14989099Sfjoe    {
15089099Sfjoe      /* Set local fields.  */
15189099Sfjoe      ret->types = NULL;
15289099Sfjoe    }
153121816Sbrooks
154121816Sbrooks  return (struct bfd_hash_entry *) ret;
15589099Sfjoe}
156121816Sbrooks
157147256Sbrooks/* Given a COFF BFD, add symbols to the global hash table as
15889099Sfjoe   appropriate.  */
15989099Sfjoe
160147256Sbrooksbfd_boolean
161159529Sfjoe_bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
162147256Sbrooks{
163147256Sbrooks  switch (bfd_get_format (abfd))
16489099Sfjoe    {
16589099Sfjoe    case bfd_object:
16689099Sfjoe      return coff_link_add_object_symbols (abfd, info);
16789099Sfjoe    case bfd_archive:
16889099Sfjoe      return _bfd_generic_link_add_archive_symbols
16989099Sfjoe	(abfd, info, coff_link_check_archive_element);
17089099Sfjoe    default:
17189099Sfjoe      bfd_set_error (bfd_error_wrong_format);
17289099Sfjoe      return FALSE;
17389099Sfjoe    }
17489099Sfjoe}
17589099Sfjoe
17689099Sfjoe/* Add symbols from a COFF object file.  */
17789099Sfjoe
17889099Sfjoestatic bfd_boolean
17989099Sfjoecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
18089099Sfjoe{
18189099Sfjoe  if (! _bfd_coff_get_external_symbols (abfd))
18289099Sfjoe    return FALSE;
18389099Sfjoe  if (! coff_link_add_symbols (abfd, info))
184159529Sfjoe    return FALSE;
18589099Sfjoe
186121752Sbrooks  if (! info->keep_memory
187121816Sbrooks      && ! _bfd_coff_free_symbols (abfd))
188121752Sbrooks    return FALSE;
189121752Sbrooks
190121752Sbrooks  return TRUE;
191121752Sbrooks}
192121752Sbrooks
193207554Ssobomax/* Look through the symbols to see if this object file should be
194159529Sfjoe   included in the link.  */
19589099Sfjoe
196121752Sbrooksstatic bfd_boolean
19789099Sfjoecoff_link_check_ar_symbols (bfd *abfd,
19889099Sfjoe			    struct bfd_link_info *info,
199121752Sbrooks			    bfd_boolean *pneeded)
200121752Sbrooks{
201121752Sbrooks  bfd_size_type symesz;
20289099Sfjoe  bfd_byte *esym;
20389099Sfjoe  bfd_byte *esym_end;
204159529Sfjoe
205199559Sjhb  *pneeded = FALSE;
20689099Sfjoe
207109771Sfjoe  symesz = bfd_coff_symesz (abfd);
20889099Sfjoe  esym = (bfd_byte *) obj_coff_external_syms (abfd);
20989099Sfjoe  esym_end = esym + obj_raw_syment_count (abfd) * symesz;
21089099Sfjoe  while (esym < esym_end)
21189099Sfjoe    {
21289099Sfjoe      struct internal_syment sym;
21389099Sfjoe      enum coff_symbol_classification classification;
21489099Sfjoe
21589099Sfjoe      bfd_coff_swap_sym_in (abfd, esym, &sym);
21689099Sfjoe
21789099Sfjoe      classification = bfd_coff_classify_symbol (abfd, &sym);
21889099Sfjoe      if (classification == COFF_SYMBOL_GLOBAL
21989099Sfjoe	  || classification == COFF_SYMBOL_COMMON)
22089099Sfjoe	{
221159529Sfjoe	  const char *name;
222159529Sfjoe	  char buf[SYMNMLEN + 1];
223159529Sfjoe	  struct bfd_link_hash_entry *h;
224159529Sfjoe
22589099Sfjoe	  /* This symbol is externally visible, and is defined by this
226159529Sfjoe             object file.  */
227159529Sfjoe	  name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
228159529Sfjoe	  if (name == NULL)
229159529Sfjoe	    return FALSE;
230159529Sfjoe	  h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
231148887Srwatson
232148887Srwatson	  /* Auto import.  */
233159529Sfjoe	  if (!h
23489099Sfjoe	      && info->pei386_auto_import
23589099Sfjoe	      && CONST_STRNEQ (name, "__imp_"))
23689099Sfjoe	    h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE);
23789099Sfjoe
23889099Sfjoe	  /* We are only interested in symbols that are currently
23989099Sfjoe	     undefined.  If a symbol is currently known to be common,
240159529Sfjoe	     COFF linkers do not bring in an object file which defines
24189099Sfjoe	     it.  */
24289099Sfjoe	  if (h != (struct bfd_link_hash_entry *) NULL
243159529Sfjoe	      && h->type == bfd_link_hash_undefined)
24489099Sfjoe	    {
24589099Sfjoe	      if (! (*info->callbacks->add_archive_element) (info, abfd, name))
24689099Sfjoe		return FALSE;
24789099Sfjoe	      *pneeded = TRUE;
24889099Sfjoe	      return TRUE;
249147256Sbrooks	    }
25089099Sfjoe	}
25189099Sfjoe
252104251Sbrooks      esym += (sym.n_numaux + 1) * symesz;
25389099Sfjoe    }
25489099Sfjoe
25589099Sfjoe  /* We do not need this object file.  */
25689099Sfjoe  return TRUE;
25789099Sfjoe}
25889099Sfjoe
25989099Sfjoe/* Check a single archive element to see if we need to include it in
26089099Sfjoe   the link.  *PNEEDED is set according to whether this element is
26189099Sfjoe   needed in the link or not.  This is called via
26289099Sfjoe   _bfd_generic_link_add_archive_symbols.  */
26389099Sfjoe
264104251Sbrooksstatic bfd_boolean
265104251Sbrookscoff_link_check_archive_element (bfd *abfd,
26689099Sfjoe				 struct bfd_link_info *info,
26789099Sfjoe				 bfd_boolean *pneeded)
26889099Sfjoe{
26989099Sfjoe  if (! _bfd_coff_get_external_symbols (abfd))
27089099Sfjoe    return FALSE;
27189099Sfjoe
27289099Sfjoe  if (! coff_link_check_ar_symbols (abfd, info, pneeded))
27389099Sfjoe    return FALSE;
27489099Sfjoe
27589099Sfjoe  if (*pneeded
27689099Sfjoe      && ! coff_link_add_symbols (abfd, info))
27789099Sfjoe    return FALSE;
278104251Sbrooks
279104251Sbrooks  if ((! info->keep_memory || ! *pneeded)
28089099Sfjoe      && ! _bfd_coff_free_symbols (abfd))
28189099Sfjoe    return FALSE;
28289099Sfjoe
28389099Sfjoe  return TRUE;
284104251Sbrooks}
285104251Sbrooks
28689099Sfjoe/* Add all the symbols from an object file to the hash table.  */
28789099Sfjoe
28889099Sfjoestatic bfd_boolean
28989099Sfjoecoff_link_add_symbols (bfd *abfd,
29089099Sfjoe		       struct bfd_link_info *info)
29189099Sfjoe{
29289099Sfjoe  unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
29389099Sfjoe  unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
29489099Sfjoe  unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
29589099Sfjoe  bfd_boolean keep_syms;
29689099Sfjoe  bfd_boolean default_copy;
29789099Sfjoe  bfd_size_type symcount;
29889099Sfjoe  struct coff_link_hash_entry **sym_hash;
29989099Sfjoe  bfd_size_type symesz;
300104251Sbrooks  bfd_byte *esym;
301104251Sbrooks  bfd_byte *esym_end;
30289099Sfjoe  bfd_size_type amt;
30389099Sfjoe
30489099Sfjoe  /* Keep the symbols during this function, in case the linker needs
30589099Sfjoe     to read the generic symbols in order to report an error message.  */
30689099Sfjoe  keep_syms = obj_coff_keep_syms (abfd);
30789099Sfjoe  obj_coff_keep_syms (abfd) = TRUE;
308148887Srwatson
309148887Srwatson  if (info->keep_memory)
31089099Sfjoe    default_copy = FALSE;
311199559Sjhb  else
312159529Sfjoe    default_copy = TRUE;
31389099Sfjoe
31489099Sfjoe  symcount = obj_raw_syment_count (abfd);
31589099Sfjoe
31689099Sfjoe  /* We keep a list of the linker hash table entries that correspond
31789099Sfjoe     to particular symbols.  */
31889099Sfjoe  amt = symcount * sizeof (struct coff_link_hash_entry *);
319159529Sfjoe  sym_hash = bfd_zalloc (abfd, amt);
32089099Sfjoe  if (sym_hash == NULL && symcount != 0)
32189099Sfjoe    goto error_return;
32289099Sfjoe  obj_coff_sym_hashes (abfd) = sym_hash;
32389099Sfjoe
32489099Sfjoe  symesz = bfd_coff_symesz (abfd);
32589099Sfjoe  BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
32689099Sfjoe  esym = (bfd_byte *) obj_coff_external_syms (abfd);
32789099Sfjoe  esym_end = esym + symcount * symesz;
32889099Sfjoe  while (esym < esym_end)
329199559Sjhb    {
330199559Sjhb      struct internal_syment sym;
33189099Sfjoe      enum coff_symbol_classification classification;
33289099Sfjoe      bfd_boolean copy;
333159529Sfjoe
334159529Sfjoe      bfd_coff_swap_sym_in (abfd, esym, &sym);
335159529Sfjoe
336159529Sfjoe      classification = bfd_coff_classify_symbol (abfd, &sym);
337159529Sfjoe      if (classification != COFF_SYMBOL_LOCAL)
338159529Sfjoe	{
339159529Sfjoe	  const char *name;
340159529Sfjoe	  char buf[SYMNMLEN + 1];
341159529Sfjoe	  flagword flags;
342159529Sfjoe	  asection *section;
34389099Sfjoe	  bfd_vma value;
34489099Sfjoe	  bfd_boolean addit;
34589099Sfjoe
34689099Sfjoe	  /* This symbol is externally visible.  */
34789099Sfjoe
348159529Sfjoe	  name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
34989099Sfjoe	  if (name == NULL)
35089099Sfjoe	    goto error_return;
351159529Sfjoe
35289099Sfjoe	  /* We must copy the name into memory if we got it from the
35389099Sfjoe             syment itself, rather than the string table.  */
35489099Sfjoe	  copy = default_copy;
355159529Sfjoe	  if (sym._n._n_n._n_zeroes != 0
35689099Sfjoe	      || sym._n._n_n._n_offset == 0)
35789099Sfjoe	    copy = TRUE;
358159529Sfjoe
35989099Sfjoe	  value = sym.n_value;
36089099Sfjoe
36189099Sfjoe	  switch (classification)
36289099Sfjoe	    {
36389099Sfjoe	    default:
364104251Sbrooks	      abort ();
36589099Sfjoe
36689099Sfjoe	    case COFF_SYMBOL_GLOBAL:
367148887Srwatson	      flags = BSF_EXPORT | BSF_GLOBAL;
36889099Sfjoe	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
36989099Sfjoe	      if (! obj_pe (abfd))
370159529Sfjoe		value -= section->vma;
37189099Sfjoe	      break;
37289099Sfjoe
37389099Sfjoe	    case COFF_SYMBOL_UNDEFINED:
37489099Sfjoe	      flags = 0;
37589099Sfjoe	      section = bfd_und_section_ptr;
37689099Sfjoe	      break;
37789099Sfjoe
37889099Sfjoe	    case COFF_SYMBOL_COMMON:
37989099Sfjoe	      flags = BSF_GLOBAL;
38089099Sfjoe	      section = bfd_com_section_ptr;
38189099Sfjoe	      break;
382104251Sbrooks
383104251Sbrooks	    case COFF_SYMBOL_PE_SECTION:
38489099Sfjoe	      flags = BSF_SECTION_SYM | BSF_GLOBAL;
38589099Sfjoe	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
38689099Sfjoe	      break;
38789099Sfjoe	    }
38889099Sfjoe
389109771Sfjoe	  if (IS_WEAK_EXTERNAL (abfd, sym))
39089099Sfjoe	    flags = BSF_WEAK;
39189099Sfjoe
39289099Sfjoe	  addit = TRUE;
39389099Sfjoe
39489099Sfjoe	  /* In the PE format, section symbols actually refer to the
39589099Sfjoe             start of the output section.  We handle them specially
39689099Sfjoe             here.  */
39789099Sfjoe	  if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
39889099Sfjoe	    {
39989099Sfjoe	      *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
40089099Sfjoe						 name, FALSE, copy, FALSE);
40189099Sfjoe	      if (*sym_hash != NULL)
40289099Sfjoe		{
40389099Sfjoe		  if (((*sym_hash)->coff_link_hash_flags
40489099Sfjoe		       & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
40589099Sfjoe		      && (*sym_hash)->root.type != bfd_link_hash_undefined
40689099Sfjoe		      && (*sym_hash)->root.type != bfd_link_hash_undefweak)
40789099Sfjoe		    (*_bfd_error_handler)
40889099Sfjoe		      ("Warning: symbol `%s' is both section and non-section",
40989099Sfjoe		       name);
41089099Sfjoe
41189099Sfjoe		  addit = FALSE;
41289099Sfjoe		}
41389099Sfjoe	    }
41489099Sfjoe
41589099Sfjoe	  /* The Microsoft Visual C compiler does string pooling by
41689099Sfjoe	     hashing the constants to an internal symbol name, and
41789099Sfjoe	     relying on the linker comdat support to discard
41889099Sfjoe	     duplicate names.  However, if one string is a literal and
41989099Sfjoe	     one is a data initializer, one will end up in the .data
42089099Sfjoe	     section and one will end up in the .rdata section.  The
42189099Sfjoe	     Microsoft linker will combine them into the .data
42289099Sfjoe	     section, which seems to be wrong since it might cause the
42389099Sfjoe	     literal to change.
42489099Sfjoe
42589099Sfjoe	     As long as there are no external references to the
42689099Sfjoe	     symbols, which there shouldn't be, we can treat the .data
42789099Sfjoe	     and .rdata instances as separate symbols.  The comdat
42889099Sfjoe	     code in the linker will do the appropriate merging.  Here
42989099Sfjoe	     we avoid getting a multiple definition error for one of
43089099Sfjoe	     these special symbols.
43189099Sfjoe
43289099Sfjoe	     FIXME: I don't think this will work in the case where
43389099Sfjoe	     there are two object files which use the constants as a
43489099Sfjoe	     literal and two object files which use it as a data
43589099Sfjoe	     initializer.  One or the other of the second object files
43689099Sfjoe	     is going to wind up with an inappropriate reference.  */
43789099Sfjoe	  if (obj_pe (abfd)
43889099Sfjoe	      && (classification == COFF_SYMBOL_GLOBAL
43989099Sfjoe		  || classification == COFF_SYMBOL_PE_SECTION)
44089099Sfjoe	      && coff_section_data (abfd, section) != NULL
44189099Sfjoe	      && coff_section_data (abfd, section)->comdat != NULL
44289099Sfjoe	      && CONST_STRNEQ (name, "??_")
44389099Sfjoe	      && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
44489099Sfjoe	    {
445148887Srwatson	      if (*sym_hash == NULL)
44689099Sfjoe		*sym_hash = coff_link_hash_lookup (coff_hash_table (info),
44789099Sfjoe						   name, FALSE, copy, FALSE);
448104251Sbrooks	      if (*sym_hash != NULL
449104251Sbrooks		  && (*sym_hash)->root.type == bfd_link_hash_defined
45089099Sfjoe		  && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
45189099Sfjoe		  && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
45289099Sfjoe			     coff_section_data (abfd, section)->comdat->name) == 0)
45389099Sfjoe		addit = FALSE;
45489099Sfjoe	    }
45589099Sfjoe
456148887Srwatson	  if (addit)
45789099Sfjoe	    {
45889099Sfjoe	      if (! (bfd_coff_link_add_one_symbol
45989099Sfjoe		     (info, abfd, name, flags, section, value,
46089099Sfjoe		      (const char *) NULL, copy, FALSE,
46189099Sfjoe		      (struct bfd_link_hash_entry **) sym_hash)))
462199559Sjhb		goto error_return;
46389099Sfjoe	    }
46489099Sfjoe
46589099Sfjoe	  if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
46689099Sfjoe	    (*sym_hash)->coff_link_hash_flags |=
46789099Sfjoe	      COFF_LINK_HASH_PE_SECTION_SYMBOL;
46889099Sfjoe
46989099Sfjoe	  /* Limit the alignment of a common symbol to the possible
47089099Sfjoe             alignment of a section.  There is no point to permitting
47189099Sfjoe             a higher alignment for a common symbol: we can not
472159529Sfjoe             guarantee it, and it may cause us to allocate extra space
47389099Sfjoe             in the common section.  */
474159529Sfjoe	  if (section == bfd_com_section_ptr
475159529Sfjoe	      && (*sym_hash)->root.type == bfd_link_hash_common
476159529Sfjoe	      && ((*sym_hash)->root.u.c.p->alignment_power
477159529Sfjoe		  > bfd_coff_default_section_alignment_power (abfd)))
478159529Sfjoe	    (*sym_hash)->root.u.c.p->alignment_power
479159529Sfjoe	      = bfd_coff_default_section_alignment_power (abfd);
480159529Sfjoe
481159529Sfjoe	  if (info->hash->creator->flavour == bfd_get_flavour (abfd))
482159529Sfjoe	    {
48389099Sfjoe	      /* If we don't have any symbol information currently in
484159529Sfjoe                 the hash table, or if we are looking at a symbol
48589099Sfjoe                 definition, then update the symbol class and type in
48689099Sfjoe                 the hash table.  */
48789099Sfjoe  	      if (((*sym_hash)->class == C_NULL
48889099Sfjoe  		   && (*sym_hash)->type == T_NULL)
48989099Sfjoe  		  || sym.n_scnum != 0
49089099Sfjoe  		  || (sym.n_value != 0
491159529Sfjoe  		      && (*sym_hash)->root.type != bfd_link_hash_defined
49289099Sfjoe  		      && (*sym_hash)->root.type != bfd_link_hash_defweak))
49389099Sfjoe  		{
49489099Sfjoe  		  (*sym_hash)->class = sym.n_sclass;
495159529Sfjoe  		  if (sym.n_type != T_NULL)
49689099Sfjoe  		    {
49789099Sfjoe  		      /* We want to warn if the type changed, but not
49889099Sfjoe  			 if it changed from an unspecified type.
49989099Sfjoe  			 Testing the whole type byte may work, but the
50089099Sfjoe  			 change from (e.g.) a function of unspecified
501147256Sbrooks  			 type to function of known type also wants to
50289099Sfjoe  			 skip the warning.  */
50389099Sfjoe  		      if ((*sym_hash)->type != T_NULL
50489099Sfjoe  			  && (*sym_hash)->type != sym.n_type
50589099Sfjoe  		          && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
506243857Sglebius  		               && (BTYPE ((*sym_hash)->type) == T_NULL
50789099Sfjoe  		                   || BTYPE (sym.n_type) == T_NULL)))
50889099Sfjoe  			(*_bfd_error_handler)
50989099Sfjoe  			  (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
51089099Sfjoe  			   abfd, name, (*sym_hash)->type, sym.n_type);
51189099Sfjoe
51289099Sfjoe  		      /* We don't want to change from a meaningful
51389099Sfjoe  			 base type to a null one, but if we know
51489099Sfjoe  			 nothing, take what little we might now know.  */
51589099Sfjoe  		      if (BTYPE (sym.n_type) != T_NULL
51689099Sfjoe  			  || (*sym_hash)->type == T_NULL)
51789099Sfjoe			(*sym_hash)->type = sym.n_type;
51889099Sfjoe  		    }
51989099Sfjoe  		  (*sym_hash)->auxbfd = abfd;
52089099Sfjoe		  if (sym.n_numaux != 0)
52189099Sfjoe		    {
52289099Sfjoe		      union internal_auxent *alloc;
52389099Sfjoe		      unsigned int i;
52489099Sfjoe		      bfd_byte *eaux;
52589099Sfjoe		      union internal_auxent *iaux;
52689099Sfjoe
52789099Sfjoe		      (*sym_hash)->numaux = sym.n_numaux;
528109771Sfjoe		      alloc = ((union internal_auxent *)
52989099Sfjoe			       bfd_hash_allocate (&info->hash->table,
53089099Sfjoe						  (sym.n_numaux
53189099Sfjoe						   * sizeof (*alloc))));
53289099Sfjoe		      if (alloc == NULL)
53389099Sfjoe			goto error_return;
53489099Sfjoe		      for (i = 0, eaux = esym + symesz, iaux = alloc;
53589099Sfjoe			   i < sym.n_numaux;
53689099Sfjoe			   i++, eaux += symesz, iaux++)
53789099Sfjoe			bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
53889099Sfjoe					      sym.n_sclass, (int) i,
53989099Sfjoe					      sym.n_numaux, iaux);
54089099Sfjoe		      (*sym_hash)->aux = alloc;
54189099Sfjoe		    }
54289099Sfjoe		}
543243857Sglebius	    }
54489099Sfjoe
54589099Sfjoe	  if (classification == COFF_SYMBOL_PE_SECTION
54689099Sfjoe	      && (*sym_hash)->numaux != 0)
54789099Sfjoe	    {
54889099Sfjoe	      /* Some PE sections (such as .bss) have a zero size in
54989099Sfjoe                 the section header, but a non-zero size in the AUX
55089099Sfjoe                 record.  Correct that here.
55189099Sfjoe
55289099Sfjoe		 FIXME: This is not at all the right place to do this.
55389099Sfjoe		 For example, it won't help objdump.  This needs to be
55489099Sfjoe		 done when we swap in the section header.  */
55589099Sfjoe	      BFD_ASSERT ((*sym_hash)->numaux == 1);
55689099Sfjoe	      if (section->size == 0)
55789099Sfjoe		section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
55889099Sfjoe
55989099Sfjoe	      /* FIXME: We could test whether the section sizes
56089099Sfjoe                 matches the size in the aux entry, but apparently
56189099Sfjoe                 that sometimes fails unexpectedly.  */
56289099Sfjoe	    }
56389099Sfjoe	}
56489099Sfjoe
56589099Sfjoe      esym += (sym.n_numaux + 1) * symesz;
56689099Sfjoe      sym_hash += sym.n_numaux + 1;
56789099Sfjoe    }
56889099Sfjoe
56989099Sfjoe  /* If this is a non-traditional, non-relocatable link, try to
570159529Sfjoe     optimize the handling of any .stab/.stabstr sections.  */
57189099Sfjoe  if (! info->relocatable
572159529Sfjoe      && ! info->traditional_format
57389099Sfjoe      && info->hash->creator->flavour == bfd_get_flavour (abfd)
57489099Sfjoe      && (info->strip != strip_all && info->strip != strip_debugger))
57589099Sfjoe    {
57689099Sfjoe      asection *stabstr;
57789099Sfjoe
57889099Sfjoe      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
57989099Sfjoe
58089099Sfjoe      if (stabstr != NULL)
58189099Sfjoe	{
58289099Sfjoe	  bfd_size_type string_offset = 0;
58389099Sfjoe	  asection *stab;
58489099Sfjoe
58589099Sfjoe	  for (stab = abfd->sections; stab; stab = stab->next)
58689099Sfjoe	    if (CONST_STRNEQ (stab->name, ".stab")
58789099Sfjoe		&& (!stab->name[5]
58889099Sfjoe		    || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
58989099Sfjoe	    {
590172568Skevlo	      struct coff_link_hash_table *table;
59189099Sfjoe	      struct coff_section_tdata *secdata
59289099Sfjoe		= coff_section_data (abfd, stab);
59389099Sfjoe
59489099Sfjoe	      if (secdata == NULL)
595104251Sbrooks		{
59689099Sfjoe		  amt = sizeof (struct coff_section_tdata);
59789099Sfjoe		  stab->used_by_bfd = bfd_zalloc (abfd, amt);
59889099Sfjoe		  if (stab->used_by_bfd == NULL)
59989099Sfjoe		    goto error_return;
600228471Sed		  secdata = coff_section_data (abfd, stab);
601159529Sfjoe		}
60289099Sfjoe
60389099Sfjoe	      table = coff_hash_table (info);
60489099Sfjoe
60589099Sfjoe	      if (! _bfd_link_section_stabs (abfd, &table->stab_info,
60689099Sfjoe					     stab, stabstr,
60789099Sfjoe					     &secdata->stab_info,
60889099Sfjoe					     &string_offset))
60989099Sfjoe		goto error_return;
61089099Sfjoe	    }
61189099Sfjoe	}
612147256Sbrooks    }
61389099Sfjoe
61489099Sfjoe  obj_coff_keep_syms (abfd) = keep_syms;
61589099Sfjoe
61689099Sfjoe  return TRUE;
61789099Sfjoe
61889099Sfjoe error_return:
61989099Sfjoe  obj_coff_keep_syms (abfd) = keep_syms;
62089099Sfjoe  return FALSE;
62189099Sfjoe}
62289099Sfjoe
623147256Sbrooks/* Do the final link step.  */
62489099Sfjoe
625199559Sjhbbfd_boolean
62689099Sfjoe_bfd_coff_final_link (bfd *abfd,
62789099Sfjoe		      struct bfd_link_info *info)
62889099Sfjoe{
62989099Sfjoe  bfd_size_type symesz;
63089099Sfjoe  struct coff_final_link_info finfo;
63189099Sfjoe  bfd_boolean debug_merge_allocated;
63289099Sfjoe  bfd_boolean long_section_names;
63389099Sfjoe  asection *o;
63489099Sfjoe  struct bfd_link_order *p;
63589099Sfjoe  bfd_size_type max_sym_count;
63689099Sfjoe  bfd_size_type max_lineno_count;
637148887Srwatson  bfd_size_type max_reloc_count;
63889099Sfjoe  bfd_size_type max_output_reloc_count;
63989099Sfjoe  bfd_size_type max_contents_size;
64089099Sfjoe  file_ptr rel_filepos;
64189099Sfjoe  unsigned int relsz;
64289099Sfjoe  file_ptr line_filepos;
64389099Sfjoe  unsigned int linesz;
64489099Sfjoe  bfd *sub;
64589099Sfjoe  bfd_byte *external_relocs = NULL;
64689099Sfjoe  char strbuf[STRING_SIZE_SIZE];
64789099Sfjoe  bfd_size_type amt;
64889099Sfjoe
64989099Sfjoe  symesz = bfd_coff_symesz (abfd);
65089099Sfjoe
65189099Sfjoe  finfo.info = info;
65289099Sfjoe  finfo.output_bfd = abfd;
65389099Sfjoe  finfo.strtab = NULL;
65489099Sfjoe  finfo.section_info = NULL;
655199559Sjhb  finfo.last_file_index = -1;
65689099Sfjoe  finfo.last_bf_index = -1;
65789099Sfjoe  finfo.internal_syms = NULL;
658104251Sbrooks  finfo.sec_ptrs = NULL;
659104251Sbrooks  finfo.sym_indices = NULL;
660104251Sbrooks  finfo.outsyms = NULL;
66189099Sfjoe  finfo.linenos = NULL;
66289099Sfjoe  finfo.contents = NULL;
66389099Sfjoe  finfo.external_relocs = NULL;
66489099Sfjoe  finfo.internal_relocs = NULL;
66589099Sfjoe  finfo.global_to_static = FALSE;
66689099Sfjoe  debug_merge_allocated = FALSE;
667199559Sjhb
66889099Sfjoe  coff_data (abfd)->link_info = info;
66989099Sfjoe
670104251Sbrooks  finfo.strtab = _bfd_stringtab_init ();
671104251Sbrooks  if (finfo.strtab == NULL)
67289099Sfjoe    goto error_return;
67389099Sfjoe
67489099Sfjoe  if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
67589099Sfjoe    goto error_return;
67689099Sfjoe  debug_merge_allocated = TRUE;
67789099Sfjoe
67889099Sfjoe  /* Compute the file positions for all the sections.  */
67989099Sfjoe  if (! abfd->output_has_begun)
68089099Sfjoe    {
681159529Sfjoe      if (! bfd_coff_compute_section_file_positions (abfd))
68289099Sfjoe	goto error_return;
68389099Sfjoe    }
68489099Sfjoe
68589099Sfjoe  /* Count the line numbers and relocation entries required for the
68689099Sfjoe     output file.  Set the file positions for the relocs.  */
68789099Sfjoe  rel_filepos = obj_relocbase (abfd);
68889099Sfjoe  relsz = bfd_coff_relsz (abfd);
68989099Sfjoe  max_contents_size = 0;
69089099Sfjoe  max_lineno_count = 0;
69189099Sfjoe  max_reloc_count = 0;
69289099Sfjoe
693147256Sbrooks  long_section_names = FALSE;
69489099Sfjoe  for (o = abfd->sections; o != NULL; o = o->next)
69589099Sfjoe    {
69689099Sfjoe      o->reloc_count = 0;
69789099Sfjoe      o->lineno_count = 0;
69889099Sfjoe      for (p = o->map_head.link_order; p != NULL; p = p->next)
699159529Sfjoe	{
700159529Sfjoe	  if (p->type == bfd_indirect_link_order)
70189099Sfjoe	    {
70289099Sfjoe	      asection *sec;
703159529Sfjoe
704159529Sfjoe	      sec = p->u.indirect.section;
70589099Sfjoe
706159529Sfjoe	      /* Mark all sections which are to be included in the
707159529Sfjoe		 link.  This will normally be every section.  We need
70889099Sfjoe		 to do this so that we can identify any sections which
70989099Sfjoe		 the linker has decided to not include.  */
710109771Sfjoe	      sec->linker_mark = TRUE;
711104251Sbrooks
712104251Sbrooks	      if (info->strip == strip_none
71389099Sfjoe		  || info->strip == strip_some)
71489099Sfjoe		o->lineno_count += sec->lineno_count;
71589099Sfjoe
71689099Sfjoe	      if (info->relocatable)
71789099Sfjoe		o->reloc_count += sec->reloc_count;
71889099Sfjoe
719147256Sbrooks	      if (sec->rawsize > max_contents_size)
72089099Sfjoe		max_contents_size = sec->rawsize;
72189099Sfjoe	      if (sec->size > max_contents_size)
72289099Sfjoe		max_contents_size = sec->size;
723121816Sbrooks	      if (sec->lineno_count > max_lineno_count)
724121816Sbrooks		max_lineno_count = sec->lineno_count;
72589099Sfjoe	      if (sec->reloc_count > max_reloc_count)
72689099Sfjoe		max_reloc_count = sec->reloc_count;
72789099Sfjoe	    }
72889099Sfjoe	  else if (info->relocatable
72989099Sfjoe		   && (p->type == bfd_section_reloc_link_order
73089099Sfjoe		       || p->type == bfd_symbol_reloc_link_order))
73189099Sfjoe	    ++o->reloc_count;
73289099Sfjoe	}
733147256Sbrooks      if (o->reloc_count == 0)
73489099Sfjoe	o->rel_filepos = 0;
73589099Sfjoe      else
73689099Sfjoe	{
73789099Sfjoe	  o->flags |= SEC_RELOC;
73889099Sfjoe	  o->rel_filepos = rel_filepos;
73989099Sfjoe	  rel_filepos += o->reloc_count * relsz;
74089099Sfjoe	  /* In PE COFF, if there are at least 0xffff relocations an
74189099Sfjoe	     extra relocation will be written out to encode the count.  */
74289099Sfjoe	  if (obj_pe (abfd) && o->reloc_count >= 0xffff)
74389099Sfjoe	    rel_filepos += relsz;
74489099Sfjoe	}
74589099Sfjoe
74689099Sfjoe      if (bfd_coff_long_section_names (abfd)
74789099Sfjoe	  && strlen (o->name) > SCNNMLEN)
74889099Sfjoe	{
74989099Sfjoe	  /* This section has a long name which must go in the string
75089099Sfjoe             table.  This must correspond to the code in
75189099Sfjoe             coff_write_object_contents which puts the string index
75289099Sfjoe             into the s_name field of the section header.  That is why
75389099Sfjoe             we pass hash as FALSE.  */
754121816Sbrooks	  if (_bfd_stringtab_add (finfo.strtab, o->name, FALSE, FALSE)
75589099Sfjoe	      == (bfd_size_type) -1)
756121816Sbrooks	    goto error_return;
75789099Sfjoe	  long_section_names = TRUE;
75889099Sfjoe	}
75989099Sfjoe    }
760159529Sfjoe
76189099Sfjoe  /* If doing a relocatable link, allocate space for the pointers we
76289099Sfjoe     need to keep.  */
76389099Sfjoe  if (info->relocatable)
76489099Sfjoe    {
765104251Sbrooks      unsigned int i;
766104251Sbrooks
76789099Sfjoe      /* We use section_count + 1, rather than section_count, because
76889099Sfjoe         the target_index fields are 1 based.  */
76989099Sfjoe      amt = abfd->section_count + 1;
77089099Sfjoe      amt *= sizeof (struct coff_link_section_info);
771109771Sfjoe      finfo.section_info = bfd_malloc (amt);
77289099Sfjoe      if (finfo.section_info == NULL)
77389099Sfjoe	goto error_return;
77489099Sfjoe      for (i = 0; i <= abfd->section_count; i++)
77589099Sfjoe	{
77689099Sfjoe	  finfo.section_info[i].relocs = NULL;
777172568Skevlo	  finfo.section_info[i].rel_hashes = NULL;
778121816Sbrooks	}
77989099Sfjoe    }
78089099Sfjoe
78189099Sfjoe  /* We now know the size of the relocs, so we can determine the file
78289099Sfjoe     positions of the line numbers.  */
78389099Sfjoe  line_filepos = rel_filepos;
78489099Sfjoe  linesz = bfd_coff_linesz (abfd);
78589099Sfjoe  max_output_reloc_count = 0;
78689099Sfjoe  for (o = abfd->sections; o != NULL; o = o->next)
78789099Sfjoe    {
78889099Sfjoe      if (o->lineno_count == 0)
78989099Sfjoe	o->line_filepos = 0;
79089099Sfjoe      else
79189099Sfjoe	{
79289099Sfjoe	  o->line_filepos = line_filepos;
79389099Sfjoe	  line_filepos += o->lineno_count * linesz;
79489099Sfjoe	}
795172568Skevlo
79689099Sfjoe      if (o->reloc_count != 0)
79789099Sfjoe	{
79889099Sfjoe	  /* We don't know the indices of global symbols until we have
79989099Sfjoe             written out all the local symbols.  For each section in
80089099Sfjoe             the output file, we keep an array of pointers to hash
801104251Sbrooks             table entries.  Each entry in the array corresponds to a
80289099Sfjoe             reloc.  When we find a reloc against a global symbol, we
80389099Sfjoe             set the corresponding entry in this array so that we can
80489099Sfjoe             fix up the symbol index after we have written out all the
80589099Sfjoe             local symbols.
80689099Sfjoe
80789099Sfjoe	     Because of this problem, we also keep the relocs in
80889099Sfjoe	     memory until the end of the link.  This wastes memory,
80989099Sfjoe	     but only when doing a relocatable link, which is not the
81089099Sfjoe	     common case.  */
81189099Sfjoe	  BFD_ASSERT (info->relocatable);
81289099Sfjoe	  amt = o->reloc_count;
81389099Sfjoe	  amt *= sizeof (struct internal_reloc);
81489099Sfjoe	  finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
815159529Sfjoe	  amt = o->reloc_count;
81689099Sfjoe	  amt *= sizeof (struct coff_link_hash_entry *);
81789099Sfjoe	  finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
81889099Sfjoe	  if (finfo.section_info[o->target_index].relocs == NULL
81989099Sfjoe	      || finfo.section_info[o->target_index].rel_hashes == NULL)
820159529Sfjoe	    goto error_return;
82189099Sfjoe
82289099Sfjoe	  if (o->reloc_count > max_output_reloc_count)
82389099Sfjoe	    max_output_reloc_count = o->reloc_count;
82489099Sfjoe	}
825109771Sfjoe
826104251Sbrooks      /* Reset the reloc and lineno counts, so that we can use them to
827104251Sbrooks	 count the number of entries we have output so far.  */
82889099Sfjoe      o->reloc_count = 0;
829159529Sfjoe      o->lineno_count = 0;
83089099Sfjoe    }
83189099Sfjoe
83289099Sfjoe  obj_sym_filepos (abfd) = line_filepos;
833159529Sfjoe
83489099Sfjoe  /* Figure out the largest number of symbols in an input BFD.  Take
83589099Sfjoe     the opportunity to clear the output_has_begun fields of all the
83689099Sfjoe     input BFD's.  */
837147256Sbrooks  max_sym_count = 0;
83889099Sfjoe  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
83989099Sfjoe    {
84089099Sfjoe      size_t sz;
841121816Sbrooks
842121816Sbrooks      sub->output_has_begun = FALSE;
84389099Sfjoe      sz = obj_raw_syment_count (sub);
84489099Sfjoe      if (sz > max_sym_count)
84589099Sfjoe	max_sym_count = sz;
84689099Sfjoe    }
84789099Sfjoe
84889099Sfjoe  /* Allocate some buffers used while linking.  */
84989099Sfjoe  amt = max_sym_count * sizeof (struct internal_syment);
85089099Sfjoe  finfo.internal_syms = bfd_malloc (amt);
85189099Sfjoe  amt = max_sym_count * sizeof (asection *);
85289099Sfjoe  finfo.sec_ptrs = bfd_malloc (amt);
85389099Sfjoe  amt = max_sym_count * sizeof (long);
85489099Sfjoe  finfo.sym_indices = bfd_malloc (amt);
85589099Sfjoe  finfo.outsyms = bfd_malloc ((max_sym_count + 1) * symesz);
85689099Sfjoe  amt = max_lineno_count * bfd_coff_linesz (abfd);
85789099Sfjoe  finfo.linenos = bfd_malloc (amt);
85889099Sfjoe  finfo.contents = bfd_malloc (max_contents_size);
859159529Sfjoe  amt = max_reloc_count * relsz;
86089099Sfjoe  finfo.external_relocs = bfd_malloc (amt);
86189099Sfjoe  if (! info->relocatable)
86289099Sfjoe    {
86389099Sfjoe      amt = max_reloc_count * sizeof (struct internal_reloc);
86489099Sfjoe      finfo.internal_relocs = bfd_malloc (amt);
865104251Sbrooks    }
86689099Sfjoe  if ((finfo.internal_syms == NULL && max_sym_count > 0)
86789099Sfjoe      || (finfo.sec_ptrs == NULL && max_sym_count > 0)
86889099Sfjoe      || (finfo.sym_indices == NULL && max_sym_count > 0)
86989099Sfjoe      || finfo.outsyms == NULL
870109771Sfjoe      || (finfo.linenos == NULL && max_lineno_count > 0)
87189099Sfjoe      || (finfo.contents == NULL && max_contents_size > 0)
87289099Sfjoe      || (finfo.external_relocs == NULL && max_reloc_count > 0)
87389099Sfjoe      || (! info->relocatable
87489099Sfjoe	  && finfo.internal_relocs == NULL
87589099Sfjoe	  && max_reloc_count > 0))
87689099Sfjoe    goto error_return;
87789099Sfjoe
878159529Sfjoe  /* We now know the position of everything in the file, except that
87989099Sfjoe     we don't know the size of the symbol table and therefore we don't
880148887Srwatson     know where the string table starts.  We just build the string
88189099Sfjoe     table in memory as we go along.  We process all the relocations
88289099Sfjoe     for a single input file at once.  */
88389099Sfjoe  obj_raw_syment_count (abfd) = 0;
88489099Sfjoe
885159529Sfjoe  if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
886148887Srwatson    {
88789099Sfjoe      if (! bfd_coff_start_final_link (abfd, info))
888148887Srwatson	goto error_return;
88989099Sfjoe    }
89089099Sfjoe
89189099Sfjoe  for (o = abfd->sections; o != NULL; o = o->next)
89289099Sfjoe    {
893159529Sfjoe      for (p = o->map_head.link_order; p != NULL; p = p->next)
89489099Sfjoe	{
895159529Sfjoe	  if (p->type == bfd_indirect_link_order
89689099Sfjoe	      && bfd_family_coff (p->u.indirect.section->owner))
89789099Sfjoe	    {
89889099Sfjoe	      sub = p->u.indirect.section->owner;
89989099Sfjoe	      if (! bfd_coff_link_output_has_begun (sub, & finfo))
90089099Sfjoe		{
90189099Sfjoe		  if (! _bfd_coff_link_input_bfd (&finfo, sub))
90289099Sfjoe		    goto error_return;
90389099Sfjoe		  sub->output_has_begun = TRUE;
90489099Sfjoe		}
90589099Sfjoe	    }
90689099Sfjoe	  else if (p->type == bfd_section_reloc_link_order
90789099Sfjoe		   || p->type == bfd_symbol_reloc_link_order)
90889099Sfjoe	    {
90989099Sfjoe	      if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
91089099Sfjoe		goto error_return;
91189099Sfjoe	    }
91289099Sfjoe	  else
913108470Sschweikh	    {
91489099Sfjoe	      if (! _bfd_default_link_order (abfd, info, o, p))
91589099Sfjoe		goto error_return;
91689099Sfjoe	    }
91789099Sfjoe	}
918199559Sjhb    }
91989099Sfjoe
920199559Sjhb  if (! bfd_coff_final_link_postscript (abfd, & finfo))
92189099Sfjoe    goto error_return;
922199559Sjhb
923199559Sjhb  /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
924199559Sjhb
925199559Sjhb  coff_debug_merge_hash_table_free (&finfo.debug_merge);
92689099Sfjoe  debug_merge_allocated = FALSE;
92789099Sfjoe
928  if (finfo.internal_syms != NULL)
929    {
930      free (finfo.internal_syms);
931      finfo.internal_syms = NULL;
932    }
933  if (finfo.sec_ptrs != NULL)
934    {
935      free (finfo.sec_ptrs);
936      finfo.sec_ptrs = NULL;
937    }
938  if (finfo.sym_indices != NULL)
939    {
940      free (finfo.sym_indices);
941      finfo.sym_indices = NULL;
942    }
943  if (finfo.linenos != NULL)
944    {
945      free (finfo.linenos);
946      finfo.linenos = NULL;
947    }
948  if (finfo.contents != NULL)
949    {
950      free (finfo.contents);
951      finfo.contents = NULL;
952    }
953  if (finfo.external_relocs != NULL)
954    {
955      free (finfo.external_relocs);
956      finfo.external_relocs = NULL;
957    }
958  if (finfo.internal_relocs != NULL)
959    {
960      free (finfo.internal_relocs);
961      finfo.internal_relocs = NULL;
962    }
963
964  /* The value of the last C_FILE symbol is supposed to be the symbol
965     index of the first external symbol.  Write it out again if
966     necessary.  */
967  if (finfo.last_file_index != -1
968      && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
969    {
970      file_ptr pos;
971
972      finfo.last_file.n_value = obj_raw_syment_count (abfd);
973      bfd_coff_swap_sym_out (abfd, &finfo.last_file,
974			     finfo.outsyms);
975
976      pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
977      if (bfd_seek (abfd, pos, SEEK_SET) != 0
978	  || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
979	return FALSE;
980    }
981
982  /* If doing task linking (ld --task-link) then make a pass through the
983     global symbols, writing out any that are defined, and making them
984     static.  */
985  if (info->task_link)
986    {
987      finfo.failed = FALSE;
988      coff_link_hash_traverse (coff_hash_table (info),
989			       _bfd_coff_write_task_globals, &finfo);
990      if (finfo.failed)
991	goto error_return;
992    }
993
994  /* Write out the global symbols.  */
995  finfo.failed = FALSE;
996  coff_link_hash_traverse (coff_hash_table (info),
997			   _bfd_coff_write_global_sym, &finfo);
998  if (finfo.failed)
999    goto error_return;
1000
1001  /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
1002  if (finfo.outsyms != NULL)
1003    {
1004      free (finfo.outsyms);
1005      finfo.outsyms = NULL;
1006    }
1007
1008  if (info->relocatable && max_output_reloc_count > 0)
1009    {
1010      /* Now that we have written out all the global symbols, we know
1011	 the symbol indices to use for relocs against them, and we can
1012	 finally write out the relocs.  */
1013      amt = max_output_reloc_count * relsz;
1014      external_relocs = bfd_malloc (amt);
1015      if (external_relocs == NULL)
1016	goto error_return;
1017
1018      for (o = abfd->sections; o != NULL; o = o->next)
1019	{
1020	  struct internal_reloc *irel;
1021	  struct internal_reloc *irelend;
1022	  struct coff_link_hash_entry **rel_hash;
1023	  bfd_byte *erel;
1024
1025	  if (o->reloc_count == 0)
1026	    continue;
1027
1028	  irel = finfo.section_info[o->target_index].relocs;
1029	  irelend = irel + o->reloc_count;
1030	  rel_hash = finfo.section_info[o->target_index].rel_hashes;
1031	  erel = external_relocs;
1032	  for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1033	    {
1034	      if (*rel_hash != NULL)
1035		{
1036		  BFD_ASSERT ((*rel_hash)->indx >= 0);
1037		  irel->r_symndx = (*rel_hash)->indx;
1038		}
1039	      bfd_coff_swap_reloc_out (abfd, irel, erel);
1040	    }
1041
1042	  if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1043	    goto error_return;
1044	  if (obj_pe (abfd) && o->reloc_count >= 0xffff)
1045	    {
1046	      /* In PE COFF, write the count of relocs as the first
1047		 reloc.  The header overflow bit will be set
1048		 elsewhere. */
1049	      struct internal_reloc incount;
1050	      bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1051
1052	      memset (&incount, 0, sizeof (incount));
1053	      incount.r_vaddr = o->reloc_count + 1;
1054	      bfd_coff_swap_reloc_out (abfd, (PTR) &incount, (PTR) excount);
1055	      if (bfd_bwrite (excount, relsz, abfd) != relsz)
1056		/* We'll leak, but it's an error anyway. */
1057		goto error_return;
1058	      free (excount);
1059	    }
1060	  if (bfd_bwrite (external_relocs,
1061			  (bfd_size_type) relsz * o->reloc_count, abfd)
1062	      != (bfd_size_type) relsz * o->reloc_count)
1063	    goto error_return;
1064	}
1065
1066      free (external_relocs);
1067      external_relocs = NULL;
1068    }
1069
1070  /* Free up the section information.  */
1071  if (finfo.section_info != NULL)
1072    {
1073      unsigned int i;
1074
1075      for (i = 0; i < abfd->section_count; i++)
1076	{
1077	  if (finfo.section_info[i].relocs != NULL)
1078	    free (finfo.section_info[i].relocs);
1079	  if (finfo.section_info[i].rel_hashes != NULL)
1080	    free (finfo.section_info[i].rel_hashes);
1081	}
1082      free (finfo.section_info);
1083      finfo.section_info = NULL;
1084    }
1085
1086  /* If we have optimized stabs strings, output them.  */
1087  if (coff_hash_table (info)->stab_info.stabstr != NULL)
1088    {
1089      if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1090	return FALSE;
1091    }
1092
1093  /* Write out the string table.  */
1094  if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1095    {
1096      file_ptr pos;
1097
1098      pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1099      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1100	return FALSE;
1101
1102#if STRING_SIZE_SIZE == 4
1103      H_PUT_32 (abfd,
1104		_bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1105		strbuf);
1106#else
1107 #error Change H_PUT_32 above
1108#endif
1109
1110      if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1111	  != STRING_SIZE_SIZE)
1112	return FALSE;
1113
1114      if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1115	return FALSE;
1116
1117      obj_coff_strings_written (abfd) = TRUE;
1118    }
1119
1120  _bfd_stringtab_free (finfo.strtab);
1121
1122  /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1123     not try to write out the symbols.  */
1124  bfd_get_symcount (abfd) = 0;
1125
1126  return TRUE;
1127
1128 error_return:
1129  if (debug_merge_allocated)
1130    coff_debug_merge_hash_table_free (&finfo.debug_merge);
1131  if (finfo.strtab != NULL)
1132    _bfd_stringtab_free (finfo.strtab);
1133  if (finfo.section_info != NULL)
1134    {
1135      unsigned int i;
1136
1137      for (i = 0; i < abfd->section_count; i++)
1138	{
1139	  if (finfo.section_info[i].relocs != NULL)
1140	    free (finfo.section_info[i].relocs);
1141	  if (finfo.section_info[i].rel_hashes != NULL)
1142	    free (finfo.section_info[i].rel_hashes);
1143	}
1144      free (finfo.section_info);
1145    }
1146  if (finfo.internal_syms != NULL)
1147    free (finfo.internal_syms);
1148  if (finfo.sec_ptrs != NULL)
1149    free (finfo.sec_ptrs);
1150  if (finfo.sym_indices != NULL)
1151    free (finfo.sym_indices);
1152  if (finfo.outsyms != NULL)
1153    free (finfo.outsyms);
1154  if (finfo.linenos != NULL)
1155    free (finfo.linenos);
1156  if (finfo.contents != NULL)
1157    free (finfo.contents);
1158  if (finfo.external_relocs != NULL)
1159    free (finfo.external_relocs);
1160  if (finfo.internal_relocs != NULL)
1161    free (finfo.internal_relocs);
1162  if (external_relocs != NULL)
1163    free (external_relocs);
1164  return FALSE;
1165}
1166
1167/* Parse out a -heap <reserved>,<commit> line.  */
1168
1169static char *
1170dores_com (char *ptr, bfd *output_bfd, int heap)
1171{
1172  if (coff_data(output_bfd)->pe)
1173    {
1174      int val = strtoul (ptr, &ptr, 0);
1175
1176      if (heap)
1177	pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1178      else
1179	pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1180
1181      if (ptr[0] == ',')
1182	{
1183	  val = strtoul (ptr+1, &ptr, 0);
1184	  if (heap)
1185	    pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1186	  else
1187	    pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1188	}
1189    }
1190  return ptr;
1191}
1192
1193static char *
1194get_name (char *ptr, char **dst)
1195{
1196  while (*ptr == ' ')
1197    ptr++;
1198  *dst = ptr;
1199  while (*ptr && *ptr != ' ')
1200    ptr++;
1201  *ptr = 0;
1202  return ptr+1;
1203}
1204
1205/* Process any magic embedded commands in a section called .drectve.  */
1206
1207static int
1208process_embedded_commands (bfd *output_bfd,
1209			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
1210			   bfd *abfd)
1211{
1212  asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1213  char *s;
1214  char *e;
1215  bfd_byte *copy;
1216
1217  if (!sec)
1218    return 1;
1219
1220  if (!bfd_malloc_and_get_section (abfd, sec, &copy))
1221    {
1222      if (copy != NULL)
1223	free (copy);
1224      return 0;
1225    }
1226  e = (char *) copy + sec->size;
1227
1228  for (s = (char *) copy; s < e ; )
1229    {
1230      if (s[0] != '-')
1231	{
1232	  s++;
1233	  continue;
1234	}
1235      if (CONST_STRNEQ (s, "-attr"))
1236	{
1237	  char *name;
1238	  char *attribs;
1239	  asection *asec;
1240	  int loop = 1;
1241	  int had_write = 0;
1242	  int had_exec= 0;
1243
1244	  s += 5;
1245	  s = get_name (s, &name);
1246	  s = get_name (s, &attribs);
1247
1248	  while (loop)
1249	    {
1250	      switch (*attribs++)
1251		{
1252		case 'W':
1253		  had_write = 1;
1254		  break;
1255		case 'R':
1256		  break;
1257		case 'S':
1258		  break;
1259		case 'X':
1260		  had_exec = 1;
1261		  break;
1262		default:
1263		  loop = 0;
1264		}
1265	    }
1266	  asec = bfd_get_section_by_name (abfd, name);
1267	  if (asec)
1268	    {
1269	      if (had_exec)
1270		asec->flags |= SEC_CODE;
1271	      if (!had_write)
1272		asec->flags |= SEC_READONLY;
1273	    }
1274	}
1275      else if (CONST_STRNEQ (s, "-heap"))
1276	s = dores_com (s + 5, output_bfd, 1);
1277
1278      else if (CONST_STRNEQ (s, "-stack"))
1279	s = dores_com (s + 6, output_bfd, 0);
1280
1281      else
1282	s++;
1283    }
1284  free (copy);
1285  return 1;
1286}
1287
1288/* Place a marker against all symbols which are used by relocations.
1289   This marker can be picked up by the 'do we skip this symbol ?'
1290   loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1291   that symbol.  */
1292
1293static void
1294mark_relocs (struct coff_final_link_info *finfo, bfd *input_bfd)
1295{
1296  asection * a;
1297
1298  if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1299    return;
1300
1301  for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1302    {
1303      struct internal_reloc *	internal_relocs;
1304      struct internal_reloc *	irel;
1305      struct internal_reloc *	irelend;
1306
1307      if ((a->flags & SEC_RELOC) == 0 || a->reloc_count  < 1)
1308	continue;
1309      /* Don't mark relocs in excluded sections.  */
1310      if (a->output_section == bfd_abs_section_ptr)
1311	continue;
1312
1313      /* Read in the relocs.  */
1314      internal_relocs = _bfd_coff_read_internal_relocs
1315	(input_bfd, a, FALSE,
1316	 finfo->external_relocs,
1317	 finfo->info->relocatable,
1318	 (finfo->info->relocatable
1319	  ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1320	  : finfo->internal_relocs)
1321	);
1322
1323      if (internal_relocs == NULL)
1324	continue;
1325
1326      irel     = internal_relocs;
1327      irelend  = irel + a->reloc_count;
1328
1329      /* Place a mark in the sym_indices array (whose entries have
1330	 been initialised to 0) for all of the symbols that are used
1331	 in the relocation table.  This will then be picked up in the
1332	 skip/don't-skip pass.  */
1333      for (; irel < irelend; irel++)
1334	finfo->sym_indices[ irel->r_symndx ] = -1;
1335    }
1336}
1337
1338/* Link an input file into the linker output file.  This function
1339   handles all the sections and relocations of the input file at once.  */
1340
1341bfd_boolean
1342_bfd_coff_link_input_bfd (struct coff_final_link_info *finfo, bfd *input_bfd)
1343{
1344  unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1345  unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1346  bfd_boolean (*adjust_symndx)
1347    (bfd *, struct bfd_link_info *, bfd *, asection *,
1348     struct internal_reloc *, bfd_boolean *);
1349  bfd *output_bfd;
1350  const char *strings;
1351  bfd_size_type syment_base;
1352  bfd_boolean copy, hash;
1353  bfd_size_type isymesz;
1354  bfd_size_type osymesz;
1355  bfd_size_type linesz;
1356  bfd_byte *esym;
1357  bfd_byte *esym_end;
1358  struct internal_syment *isymp;
1359  asection **secpp;
1360  long *indexp;
1361  unsigned long output_index;
1362  bfd_byte *outsym;
1363  struct coff_link_hash_entry **sym_hash;
1364  asection *o;
1365
1366  /* Move all the symbols to the output file.  */
1367
1368  output_bfd = finfo->output_bfd;
1369  strings = NULL;
1370  syment_base = obj_raw_syment_count (output_bfd);
1371  isymesz = bfd_coff_symesz (input_bfd);
1372  osymesz = bfd_coff_symesz (output_bfd);
1373  linesz = bfd_coff_linesz (input_bfd);
1374  BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1375
1376  copy = FALSE;
1377  if (! finfo->info->keep_memory)
1378    copy = TRUE;
1379  hash = TRUE;
1380  if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1381    hash = FALSE;
1382
1383  if (! _bfd_coff_get_external_symbols (input_bfd))
1384    return FALSE;
1385
1386  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1387  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1388  isymp = finfo->internal_syms;
1389  secpp = finfo->sec_ptrs;
1390  indexp = finfo->sym_indices;
1391  output_index = syment_base;
1392  outsym = finfo->outsyms;
1393
1394  if (coff_data (output_bfd)->pe
1395      && ! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1396    return FALSE;
1397
1398  /* If we are going to perform relocations and also strip/discard some
1399     symbols then we must make sure that we do not strip/discard those
1400     symbols that are going to be involved in the relocations.  */
1401  if ((   finfo->info->strip   != strip_none
1402       || finfo->info->discard != discard_none)
1403      && finfo->info->relocatable)
1404    {
1405      /* Mark the symbol array as 'not-used'.  */
1406      memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1407
1408      mark_relocs (finfo, input_bfd);
1409    }
1410
1411  while (esym < esym_end)
1412    {
1413      struct internal_syment isym;
1414      enum coff_symbol_classification classification;
1415      bfd_boolean skip;
1416      bfd_boolean global;
1417      bfd_boolean dont_skip_symbol;
1418      int add;
1419
1420      bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1421
1422      /* Make a copy of *isymp so that the relocate_section function
1423	 always sees the original values.  This is more reliable than
1424	 always recomputing the symbol value even if we are stripping
1425	 the symbol.  */
1426      isym = *isymp;
1427
1428      classification = bfd_coff_classify_symbol (input_bfd, &isym);
1429      switch (classification)
1430	{
1431	default:
1432	  abort ();
1433	case COFF_SYMBOL_GLOBAL:
1434	case COFF_SYMBOL_PE_SECTION:
1435	case COFF_SYMBOL_LOCAL:
1436	  *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1437	  break;
1438	case COFF_SYMBOL_COMMON:
1439	  *secpp = bfd_com_section_ptr;
1440	  break;
1441	case COFF_SYMBOL_UNDEFINED:
1442	  *secpp = bfd_und_section_ptr;
1443	  break;
1444	}
1445
1446      /* Extract the flag indicating if this symbol is used by a
1447         relocation.  */
1448      if ((finfo->info->strip != strip_none
1449	   || finfo->info->discard != discard_none)
1450	  && finfo->info->relocatable)
1451	dont_skip_symbol = *indexp;
1452      else
1453	dont_skip_symbol = FALSE;
1454
1455      *indexp = -1;
1456
1457      skip = FALSE;
1458      global = FALSE;
1459      add = 1 + isym.n_numaux;
1460
1461      /* If we are stripping all symbols, we want to skip this one.  */
1462      if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1463	skip = TRUE;
1464
1465      if (! skip)
1466	{
1467	  switch (classification)
1468	    {
1469	    default:
1470	      abort ();
1471	    case COFF_SYMBOL_GLOBAL:
1472	    case COFF_SYMBOL_COMMON:
1473	    case COFF_SYMBOL_PE_SECTION:
1474	      /* This is a global symbol.  Global symbols come at the
1475		 end of the symbol table, so skip them for now.
1476		 Locally defined function symbols, however, are an
1477		 exception, and are not moved to the end.  */
1478	      global = TRUE;
1479	      if (! ISFCN (isym.n_type))
1480		skip = TRUE;
1481	      break;
1482
1483	    case COFF_SYMBOL_UNDEFINED:
1484	      /* Undefined symbols are left for the end.  */
1485	      global = TRUE;
1486	      skip = TRUE;
1487	      break;
1488
1489	    case COFF_SYMBOL_LOCAL:
1490	      /* This is a local symbol.  Skip it if we are discarding
1491                 local symbols.  */
1492	      if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1493		skip = TRUE;
1494	      break;
1495	    }
1496	}
1497
1498#ifndef COFF_WITH_PE
1499      /* Skip section symbols for sections which are not going to be
1500	 emitted.  */
1501      if (!skip
1502	  && dont_skip_symbol == 0
1503	  && isym.n_sclass == C_STAT
1504	  && isym.n_type == T_NULL
1505          && isym.n_numaux > 0
1506	  && (*secpp)->output_section == bfd_abs_section_ptr)
1507	skip = TRUE;
1508#endif
1509
1510      /* If we stripping debugging symbols, and this is a debugging
1511         symbol, then skip it.  FIXME: gas sets the section to N_ABS
1512         for some types of debugging symbols; I don't know if this is
1513         a bug or not.  In any case, we handle it here.  */
1514      if (! skip
1515	  && finfo->info->strip == strip_debugger
1516	  && ! dont_skip_symbol
1517	  && (isym.n_scnum == N_DEBUG
1518	      || (isym.n_scnum == N_ABS
1519		  && (isym.n_sclass == C_AUTO
1520		      || isym.n_sclass == C_REG
1521		      || isym.n_sclass == C_MOS
1522		      || isym.n_sclass == C_MOE
1523		      || isym.n_sclass == C_MOU
1524		      || isym.n_sclass == C_ARG
1525		      || isym.n_sclass == C_REGPARM
1526		      || isym.n_sclass == C_FIELD
1527		      || isym.n_sclass == C_EOS))))
1528	skip = TRUE;
1529
1530      /* If some symbols are stripped based on the name, work out the
1531	 name and decide whether to skip this symbol.  */
1532      if (! skip
1533	  && (finfo->info->strip == strip_some
1534	      || finfo->info->discard == discard_l))
1535	{
1536	  const char *name;
1537	  char buf[SYMNMLEN + 1];
1538
1539	  name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1540	  if (name == NULL)
1541	    return FALSE;
1542
1543	  if (! dont_skip_symbol
1544	      && ((finfo->info->strip == strip_some
1545		   && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
1546				    FALSE) == NULL))
1547		   || (! global
1548		       && finfo->info->discard == discard_l
1549		       && bfd_is_local_label_name (input_bfd, name))))
1550	    skip = TRUE;
1551	}
1552
1553      /* If this is an enum, struct, or union tag, see if we have
1554         already output an identical type.  */
1555      if (! skip
1556	  && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1557	  && (isym.n_sclass == C_ENTAG
1558	      || isym.n_sclass == C_STRTAG
1559	      || isym.n_sclass == C_UNTAG)
1560	  && isym.n_numaux == 1)
1561	{
1562	  const char *name;
1563	  char buf[SYMNMLEN + 1];
1564	  struct coff_debug_merge_hash_entry *mh;
1565	  struct coff_debug_merge_type *mt;
1566	  union internal_auxent aux;
1567	  struct coff_debug_merge_element **epp;
1568	  bfd_byte *esl, *eslend;
1569	  struct internal_syment *islp;
1570	  bfd_size_type amt;
1571
1572	  name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1573	  if (name == NULL)
1574	    return FALSE;
1575
1576	  /* Ignore fake names invented by compiler; treat them all as
1577             the same name.  */
1578	  if (*name == '~' || *name == '.' || *name == '$'
1579	      || (*name == bfd_get_symbol_leading_char (input_bfd)
1580		  && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1581	    name = "";
1582
1583	  mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1584					     TRUE, TRUE);
1585	  if (mh == NULL)
1586	    return FALSE;
1587
1588	  /* Allocate memory to hold type information.  If this turns
1589             out to be a duplicate, we pass this address to
1590             bfd_release.  */
1591	  amt = sizeof (struct coff_debug_merge_type);
1592	  mt = bfd_alloc (input_bfd, amt);
1593	  if (mt == NULL)
1594	    return FALSE;
1595	  mt->class = isym.n_sclass;
1596
1597	  /* Pick up the aux entry, which points to the end of the tag
1598             entries.  */
1599	  bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1600				isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1601				&aux);
1602
1603	  /* Gather the elements.  */
1604	  epp = &mt->elements;
1605	  mt->elements = NULL;
1606	  islp = isymp + 2;
1607	  esl = esym + 2 * isymesz;
1608	  eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1609		    + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1610	  while (esl < eslend)
1611	    {
1612	      const char *elename;
1613	      char elebuf[SYMNMLEN + 1];
1614	      char *name_copy;
1615
1616	      bfd_coff_swap_sym_in (input_bfd, esl, islp);
1617
1618	      amt = sizeof (struct coff_debug_merge_element);
1619	      *epp = bfd_alloc (input_bfd, amt);
1620	      if (*epp == NULL)
1621		return FALSE;
1622
1623	      elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1624							elebuf);
1625	      if (elename == NULL)
1626		return FALSE;
1627
1628	      amt = strlen (elename) + 1;
1629	      name_copy = bfd_alloc (input_bfd, amt);
1630	      if (name_copy == NULL)
1631		return FALSE;
1632	      strcpy (name_copy, elename);
1633
1634	      (*epp)->name = name_copy;
1635	      (*epp)->type = islp->n_type;
1636	      (*epp)->tagndx = 0;
1637	      if (islp->n_numaux >= 1
1638		  && islp->n_type != T_NULL
1639		  && islp->n_sclass != C_EOS)
1640		{
1641		  union internal_auxent eleaux;
1642		  long indx;
1643
1644		  bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1645					islp->n_type, islp->n_sclass, 0,
1646					islp->n_numaux, &eleaux);
1647		  indx = eleaux.x_sym.x_tagndx.l;
1648
1649		  /* FIXME: If this tagndx entry refers to a symbol
1650		     defined later in this file, we just ignore it.
1651		     Handling this correctly would be tedious, and may
1652		     not be required.  */
1653		  if (indx > 0
1654		      && (indx
1655			  < ((esym -
1656			      (bfd_byte *) obj_coff_external_syms (input_bfd))
1657			     / (long) isymesz)))
1658		    {
1659		      (*epp)->tagndx = finfo->sym_indices[indx];
1660		      if ((*epp)->tagndx < 0)
1661			(*epp)->tagndx = 0;
1662		    }
1663		}
1664	      epp = &(*epp)->next;
1665	      *epp = NULL;
1666
1667	      esl += (islp->n_numaux + 1) * isymesz;
1668	      islp += islp->n_numaux + 1;
1669	    }
1670
1671	  /* See if we already have a definition which matches this
1672             type.  We always output the type if it has no elements,
1673             for simplicity.  */
1674	  if (mt->elements == NULL)
1675	    bfd_release (input_bfd, mt);
1676	  else
1677	    {
1678	      struct coff_debug_merge_type *mtl;
1679
1680	      for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1681		{
1682		  struct coff_debug_merge_element *me, *mel;
1683
1684		  if (mtl->class != mt->class)
1685		    continue;
1686
1687		  for (me = mt->elements, mel = mtl->elements;
1688		       me != NULL && mel != NULL;
1689		       me = me->next, mel = mel->next)
1690		    {
1691		      if (strcmp (me->name, mel->name) != 0
1692			  || me->type != mel->type
1693			  || me->tagndx != mel->tagndx)
1694			break;
1695		    }
1696
1697		  if (me == NULL && mel == NULL)
1698		    break;
1699		}
1700
1701	      if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1702		{
1703		  /* This is the first definition of this type.  */
1704		  mt->indx = output_index;
1705		  mt->next = mh->types;
1706		  mh->types = mt;
1707		}
1708	      else
1709		{
1710		  /* This is a redefinition which can be merged.  */
1711		  bfd_release (input_bfd, mt);
1712		  *indexp = mtl->indx;
1713		  add = (eslend - esym) / isymesz;
1714		  skip = TRUE;
1715		}
1716	    }
1717	}
1718
1719      /* We now know whether we are to skip this symbol or not.  */
1720      if (! skip)
1721	{
1722	  /* Adjust the symbol in order to output it.  */
1723
1724	  if (isym._n._n_n._n_zeroes == 0
1725	      && isym._n._n_n._n_offset != 0)
1726	    {
1727	      const char *name;
1728	      bfd_size_type indx;
1729
1730	      /* This symbol has a long name.  Enter it in the string
1731		 table we are building.  Note that we do not check
1732		 bfd_coff_symname_in_debug.  That is only true for
1733		 XCOFF, and XCOFF requires different linking code
1734		 anyhow.  */
1735	      name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1736	      if (name == NULL)
1737		return FALSE;
1738	      indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1739	      if (indx == (bfd_size_type) -1)
1740		return FALSE;
1741	      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1742	    }
1743
1744	  switch (isym.n_sclass)
1745	    {
1746	    case C_AUTO:
1747	    case C_MOS:
1748	    case C_EOS:
1749	    case C_MOE:
1750	    case C_MOU:
1751	    case C_UNTAG:
1752	    case C_STRTAG:
1753	    case C_ENTAG:
1754	    case C_TPDEF:
1755	    case C_ARG:
1756	    case C_USTATIC:
1757	    case C_REG:
1758	    case C_REGPARM:
1759	    case C_FIELD:
1760	      /* The symbol value should not be modified.  */
1761	      break;
1762
1763	    case C_FCN:
1764	      if (obj_pe (input_bfd)
1765		  && strcmp (isym.n_name, ".bf") != 0
1766		  && isym.n_scnum > 0)
1767		{
1768		  /* For PE, .lf and .ef get their value left alone,
1769		     while .bf gets relocated.  However, they all have
1770		     "real" section numbers, and need to be moved into
1771		     the new section.  */
1772		  isym.n_scnum = (*secpp)->output_section->target_index;
1773		  break;
1774		}
1775	      /* Fall through.  */
1776	    default:
1777	    case C_LABEL:  /* Not completely sure about these 2 */
1778	    case C_EXTDEF:
1779	    case C_BLOCK:
1780	    case C_EFCN:
1781	    case C_NULL:
1782	    case C_EXT:
1783	    case C_STAT:
1784	    case C_SECTION:
1785	    case C_NT_WEAK:
1786	      /* Compute new symbol location.  */
1787	    if (isym.n_scnum > 0)
1788	      {
1789		isym.n_scnum = (*secpp)->output_section->target_index;
1790		isym.n_value += (*secpp)->output_offset;
1791		if (! obj_pe (input_bfd))
1792		  isym.n_value -= (*secpp)->vma;
1793		if (! obj_pe (finfo->output_bfd))
1794		  isym.n_value += (*secpp)->output_section->vma;
1795	      }
1796	    break;
1797
1798	    case C_FILE:
1799	      /* The value of a C_FILE symbol is the symbol index of
1800		 the next C_FILE symbol.  The value of the last C_FILE
1801		 symbol is the symbol index to the first external
1802		 symbol (actually, coff_renumber_symbols does not get
1803		 this right--it just sets the value of the last C_FILE
1804		 symbol to zero--and nobody has ever complained about
1805		 it).  We try to get this right, below, just before we
1806		 write the symbols out, but in the general case we may
1807		 have to write the symbol out twice.  */
1808	      if (finfo->last_file_index != -1
1809		  && finfo->last_file.n_value != (bfd_vma) output_index)
1810		{
1811		  /* We must correct the value of the last C_FILE
1812                     entry.  */
1813		  finfo->last_file.n_value = output_index;
1814		  if ((bfd_size_type) finfo->last_file_index >= syment_base)
1815		    {
1816		      /* The last C_FILE symbol is in this input file.  */
1817		      bfd_coff_swap_sym_out (output_bfd,
1818					     &finfo->last_file,
1819					     (finfo->outsyms
1820					      + ((finfo->last_file_index
1821						  - syment_base)
1822						 * osymesz)));
1823		    }
1824		  else
1825		    {
1826		      file_ptr pos;
1827
1828		      /* We have already written out the last C_FILE
1829			 symbol.  We need to write it out again.  We
1830			 borrow *outsym temporarily.  */
1831		      bfd_coff_swap_sym_out (output_bfd,
1832					     &finfo->last_file, outsym);
1833		      pos = obj_sym_filepos (output_bfd);
1834		      pos += finfo->last_file_index * osymesz;
1835		      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1836			  || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1837			return FALSE;
1838		    }
1839		}
1840
1841	      finfo->last_file_index = output_index;
1842	      finfo->last_file = isym;
1843	      break;
1844	    }
1845
1846	  /* If doing task linking, convert normal global function symbols to
1847	     static functions.  */
1848	  if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1849	    isym.n_sclass = C_STAT;
1850
1851	  /* Output the symbol.  */
1852	  bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1853
1854	  *indexp = output_index;
1855
1856	  if (global)
1857	    {
1858	      long indx;
1859	      struct coff_link_hash_entry *h;
1860
1861	      indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1862		      / isymesz);
1863	      h = obj_coff_sym_hashes (input_bfd)[indx];
1864	      if (h == NULL)
1865		{
1866		  /* This can happen if there were errors earlier in
1867                     the link.  */
1868		  bfd_set_error (bfd_error_bad_value);
1869		  return FALSE;
1870		}
1871	      h->indx = output_index;
1872	    }
1873
1874	  output_index += add;
1875	  outsym += add * osymesz;
1876	}
1877
1878      esym += add * isymesz;
1879      isymp += add;
1880      ++secpp;
1881      ++indexp;
1882      for (--add; add > 0; --add)
1883	{
1884	  *secpp++ = NULL;
1885	  *indexp++ = -1;
1886	}
1887    }
1888
1889  /* Fix up the aux entries.  This must be done in a separate pass,
1890     because we don't know the correct symbol indices until we have
1891     already decided which symbols we are going to keep.  */
1892  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1893  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1894  isymp = finfo->internal_syms;
1895  indexp = finfo->sym_indices;
1896  sym_hash = obj_coff_sym_hashes (input_bfd);
1897  outsym = finfo->outsyms;
1898
1899  while (esym < esym_end)
1900    {
1901      int add;
1902
1903      add = 1 + isymp->n_numaux;
1904
1905      if ((*indexp < 0
1906	   || (bfd_size_type) *indexp < syment_base)
1907	  && (*sym_hash == NULL
1908	      || (*sym_hash)->auxbfd != input_bfd))
1909	esym += add * isymesz;
1910      else
1911	{
1912	  struct coff_link_hash_entry *h;
1913	  int i;
1914
1915	  h = NULL;
1916	  if (*indexp < 0)
1917	    {
1918	      h = *sym_hash;
1919
1920	      /* The m68k-motorola-sysv assembler will sometimes
1921                 generate two symbols with the same name, but only one
1922                 will have aux entries.  */
1923	      BFD_ASSERT (isymp->n_numaux == 0
1924			  || h->numaux == 0
1925			  || h->numaux == isymp->n_numaux);
1926	    }
1927
1928	  esym += isymesz;
1929
1930	  if (h == NULL)
1931	    outsym += osymesz;
1932
1933	  /* Handle the aux entries.  This handling is based on
1934	     coff_pointerize_aux.  I don't know if it always correct.  */
1935	  for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1936	    {
1937	      union internal_auxent aux;
1938	      union internal_auxent *auxp;
1939
1940	      if (h != NULL && h->aux != NULL && (h->numaux > i))
1941		auxp = h->aux + i;
1942	      else
1943		{
1944		  bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1945					isymp->n_sclass, i, isymp->n_numaux, &aux);
1946		  auxp = &aux;
1947		}
1948
1949	      if (isymp->n_sclass == C_FILE)
1950		{
1951		  /* If this is a long filename, we must put it in the
1952		     string table.  */
1953		  if (auxp->x_file.x_n.x_zeroes == 0
1954		      && auxp->x_file.x_n.x_offset != 0)
1955		    {
1956		      const char *filename;
1957		      bfd_size_type indx;
1958
1959		      BFD_ASSERT (auxp->x_file.x_n.x_offset
1960				  >= STRING_SIZE_SIZE);
1961		      if (strings == NULL)
1962			{
1963			  strings = _bfd_coff_read_string_table (input_bfd);
1964			  if (strings == NULL)
1965			    return FALSE;
1966			}
1967		      filename = strings + auxp->x_file.x_n.x_offset;
1968		      indx = _bfd_stringtab_add (finfo->strtab, filename,
1969						 hash, copy);
1970		      if (indx == (bfd_size_type) -1)
1971			return FALSE;
1972		      auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1973		    }
1974		}
1975	      else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1976		       && isymp->n_sclass != C_NT_WEAK)
1977		{
1978		  unsigned long indx;
1979
1980		  if (ISFCN (isymp->n_type)
1981		      || ISTAG (isymp->n_sclass)
1982		      || isymp->n_sclass == C_BLOCK
1983		      || isymp->n_sclass == C_FCN)
1984		    {
1985		      indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1986		      if (indx > 0
1987			  && indx < obj_raw_syment_count (input_bfd))
1988			{
1989			  /* We look forward through the symbol for
1990                             the index of the next symbol we are going
1991                             to include.  I don't know if this is
1992                             entirely right.  */
1993			  while ((finfo->sym_indices[indx] < 0
1994				  || ((bfd_size_type) finfo->sym_indices[indx]
1995				      < syment_base))
1996				 && indx < obj_raw_syment_count (input_bfd))
1997			    ++indx;
1998			  if (indx >= obj_raw_syment_count (input_bfd))
1999			    indx = output_index;
2000			  else
2001			    indx = finfo->sym_indices[indx];
2002			  auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2003			}
2004		    }
2005
2006		  indx = auxp->x_sym.x_tagndx.l;
2007		  if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2008		    {
2009		      long symindx;
2010
2011		      symindx = finfo->sym_indices[indx];
2012		      if (symindx < 0)
2013			auxp->x_sym.x_tagndx.l = 0;
2014		      else
2015			auxp->x_sym.x_tagndx.l = symindx;
2016		    }
2017
2018		  /* The .bf symbols are supposed to be linked through
2019		     the endndx field.  We need to carry this list
2020		     across object files.  */
2021		  if (i == 0
2022		      && h == NULL
2023		      && isymp->n_sclass == C_FCN
2024		      && (isymp->_n._n_n._n_zeroes != 0
2025			  || isymp->_n._n_n._n_offset == 0)
2026		      && isymp->_n._n_name[0] == '.'
2027		      && isymp->_n._n_name[1] == 'b'
2028		      && isymp->_n._n_name[2] == 'f'
2029		      && isymp->_n._n_name[3] == '\0')
2030		    {
2031		      if (finfo->last_bf_index != -1)
2032			{
2033			  finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2034			    *indexp;
2035
2036			  if ((bfd_size_type) finfo->last_bf_index
2037			      >= syment_base)
2038			    {
2039			      void *auxout;
2040
2041			      /* The last .bf symbol is in this input
2042				 file.  This will only happen if the
2043				 assembler did not set up the .bf
2044				 endndx symbols correctly.  */
2045			      auxout = (finfo->outsyms
2046					+ ((finfo->last_bf_index
2047					    - syment_base)
2048					   * osymesz));
2049
2050			      bfd_coff_swap_aux_out (output_bfd,
2051						     &finfo->last_bf,
2052						     isymp->n_type,
2053						     isymp->n_sclass,
2054						     0, isymp->n_numaux,
2055						     auxout);
2056			    }
2057			  else
2058			    {
2059			      file_ptr pos;
2060
2061			      /* We have already written out the last
2062                                 .bf aux entry.  We need to write it
2063                                 out again.  We borrow *outsym
2064                                 temporarily.  FIXME: This case should
2065                                 be made faster.  */
2066			      bfd_coff_swap_aux_out (output_bfd,
2067						     &finfo->last_bf,
2068						     isymp->n_type,
2069						     isymp->n_sclass,
2070						     0, isymp->n_numaux,
2071						     outsym);
2072			      pos = obj_sym_filepos (output_bfd);
2073			      pos += finfo->last_bf_index * osymesz;
2074			      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2075				  || (bfd_bwrite (outsym, osymesz, output_bfd)
2076				      != osymesz))
2077				return FALSE;
2078			    }
2079			}
2080
2081		      if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2082			finfo->last_bf_index = -1;
2083		      else
2084			{
2085			  /* The endndx field of this aux entry must
2086                             be updated with the symbol number of the
2087                             next .bf symbol.  */
2088			  finfo->last_bf = *auxp;
2089			  finfo->last_bf_index = (((outsym - finfo->outsyms)
2090						   / osymesz)
2091						  + syment_base);
2092			}
2093		    }
2094		}
2095
2096	      if (h == NULL)
2097		{
2098		  bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2099					 isymp->n_sclass, i, isymp->n_numaux,
2100					 outsym);
2101		  outsym += osymesz;
2102		}
2103
2104	      esym += isymesz;
2105	    }
2106	}
2107
2108      indexp += add;
2109      isymp += add;
2110      sym_hash += add;
2111    }
2112
2113  /* Relocate the line numbers, unless we are stripping them.  */
2114  if (finfo->info->strip == strip_none
2115      || finfo->info->strip == strip_some)
2116    {
2117      for (o = input_bfd->sections; o != NULL; o = o->next)
2118	{
2119	  bfd_vma offset;
2120	  bfd_byte *eline;
2121	  bfd_byte *elineend;
2122	  bfd_byte *oeline;
2123	  bfd_boolean skipping;
2124	  file_ptr pos;
2125	  bfd_size_type amt;
2126
2127	  /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2128	     build_link_order in ldwrite.c will not have created a
2129	     link order, which means that we will not have seen this
2130	     input section in _bfd_coff_final_link, which means that
2131	     we will not have allocated space for the line numbers of
2132	     this section.  I don't think line numbers can be
2133	     meaningful for a section which does not have
2134	     SEC_HAS_CONTENTS set, but, if they do, this must be
2135	     changed.  */
2136	  if (o->lineno_count == 0
2137	      || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2138	    continue;
2139
2140	  if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2141	      || bfd_bread (finfo->linenos, linesz * o->lineno_count,
2142			   input_bfd) != linesz * o->lineno_count)
2143	    return FALSE;
2144
2145	  offset = o->output_section->vma + o->output_offset - o->vma;
2146	  eline = finfo->linenos;
2147	  oeline = finfo->linenos;
2148	  elineend = eline + linesz * o->lineno_count;
2149	  skipping = FALSE;
2150	  for (; eline < elineend; eline += linesz)
2151	    {
2152	      struct internal_lineno iline;
2153
2154	      bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2155
2156	      if (iline.l_lnno != 0)
2157		iline.l_addr.l_paddr += offset;
2158	      else if (iline.l_addr.l_symndx >= 0
2159		       && ((unsigned long) iline.l_addr.l_symndx
2160			   < obj_raw_syment_count (input_bfd)))
2161		{
2162		  long indx;
2163
2164		  indx = finfo->sym_indices[iline.l_addr.l_symndx];
2165
2166		  if (indx < 0)
2167		    {
2168		      /* These line numbers are attached to a symbol
2169			 which we are stripping.  We must discard the
2170			 line numbers because reading them back with
2171			 no associated symbol (or associating them all
2172			 with symbol #0) will fail.  We can't regain
2173			 the space in the output file, but at least
2174			 they're dense.  */
2175		      skipping = TRUE;
2176		    }
2177		  else
2178		    {
2179		      struct internal_syment is;
2180		      union internal_auxent ia;
2181
2182		      /* Fix up the lnnoptr field in the aux entry of
2183			 the symbol.  It turns out that we can't do
2184			 this when we modify the symbol aux entries,
2185			 because gas sometimes screws up the lnnoptr
2186			 field and makes it an offset from the start
2187			 of the line numbers rather than an absolute
2188			 file index.  */
2189		      bfd_coff_swap_sym_in (output_bfd,
2190					    (finfo->outsyms
2191					     + ((indx - syment_base)
2192						* osymesz)), &is);
2193		      if ((ISFCN (is.n_type)
2194			   || is.n_sclass == C_BLOCK)
2195			  && is.n_numaux >= 1)
2196			{
2197			  void *auxptr;
2198
2199			  auxptr = (finfo->outsyms
2200				    + ((indx - syment_base + 1)
2201				       * osymesz));
2202			  bfd_coff_swap_aux_in (output_bfd, auxptr,
2203						is.n_type, is.n_sclass,
2204						0, is.n_numaux, &ia);
2205			  ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2206			    (o->output_section->line_filepos
2207			     + o->output_section->lineno_count * linesz
2208			     + eline - finfo->linenos);
2209			  bfd_coff_swap_aux_out (output_bfd, &ia,
2210						 is.n_type, is.n_sclass, 0,
2211						 is.n_numaux, auxptr);
2212			}
2213
2214		      skipping = FALSE;
2215		    }
2216
2217		  iline.l_addr.l_symndx = indx;
2218		}
2219
2220	      if (!skipping)
2221	        {
2222		  bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2223		  oeline += linesz;
2224		}
2225	    }
2226
2227	  pos = o->output_section->line_filepos;
2228	  pos += o->output_section->lineno_count * linesz;
2229	  amt = oeline - finfo->linenos;
2230	  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2231	      || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
2232	    return FALSE;
2233
2234	  o->output_section->lineno_count += amt / linesz;
2235	}
2236    }
2237
2238  /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2239     symbol will be the first symbol in the next input file.  In the
2240     normal case, this will save us from writing out the C_FILE symbol
2241     again.  */
2242  if (finfo->last_file_index != -1
2243      && (bfd_size_type) finfo->last_file_index >= syment_base)
2244    {
2245      finfo->last_file.n_value = output_index;
2246      bfd_coff_swap_sym_out (output_bfd, &finfo->last_file,
2247			     (finfo->outsyms
2248			      + ((finfo->last_file_index - syment_base)
2249				 * osymesz)));
2250    }
2251
2252  /* Write the modified symbols to the output file.  */
2253  if (outsym > finfo->outsyms)
2254    {
2255      file_ptr pos;
2256      bfd_size_type amt;
2257
2258      pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2259      amt = outsym - finfo->outsyms;
2260      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2261	  || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
2262	return FALSE;
2263
2264      BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2265		   + (outsym - finfo->outsyms) / osymesz)
2266		  == output_index);
2267
2268      obj_raw_syment_count (output_bfd) = output_index;
2269    }
2270
2271  /* Relocate the contents of each section.  */
2272  adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2273  for (o = input_bfd->sections; o != NULL; o = o->next)
2274    {
2275      bfd_byte *contents;
2276      struct coff_section_tdata *secdata;
2277
2278      if (! o->linker_mark)
2279	/* This section was omitted from the link.  */
2280	continue;
2281
2282      if ((o->flags & SEC_LINKER_CREATED) != 0)
2283	continue;
2284
2285      if ((o->flags & SEC_HAS_CONTENTS) == 0
2286	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2287	{
2288	  if ((o->flags & SEC_RELOC) != 0
2289	      && o->reloc_count != 0)
2290	    {
2291	      (*_bfd_error_handler)
2292		(_("%B: relocs in section `%A', but it has no contents"),
2293		 input_bfd, o);
2294	      bfd_set_error (bfd_error_no_contents);
2295	      return FALSE;
2296	    }
2297
2298	  continue;
2299	}
2300
2301      secdata = coff_section_data (input_bfd, o);
2302      if (secdata != NULL && secdata->contents != NULL)
2303	contents = secdata->contents;
2304      else
2305	{
2306	  bfd_size_type x = o->rawsize ? o->rawsize : o->size;
2307	  if (! bfd_get_section_contents (input_bfd, o, finfo->contents, 0, x))
2308	    return FALSE;
2309	  contents = finfo->contents;
2310	}
2311
2312      if ((o->flags & SEC_RELOC) != 0)
2313	{
2314	  int target_index;
2315	  struct internal_reloc *internal_relocs;
2316	  struct internal_reloc *irel;
2317
2318	  /* Read in the relocs.  */
2319	  target_index = o->output_section->target_index;
2320	  internal_relocs = (_bfd_coff_read_internal_relocs
2321			     (input_bfd, o, FALSE, finfo->external_relocs,
2322			      finfo->info->relocatable,
2323			      (finfo->info->relocatable
2324			       ? (finfo->section_info[target_index].relocs
2325				  + o->output_section->reloc_count)
2326			       : finfo->internal_relocs)));
2327	  if (internal_relocs == NULL)
2328	    return FALSE;
2329
2330	  /* Call processor specific code to relocate the section
2331             contents.  */
2332	  if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2333					   input_bfd, o,
2334					   contents,
2335					   internal_relocs,
2336					   finfo->internal_syms,
2337					   finfo->sec_ptrs))
2338	    return FALSE;
2339
2340	  if (finfo->info->relocatable)
2341	    {
2342	      bfd_vma offset;
2343	      struct internal_reloc *irelend;
2344	      struct coff_link_hash_entry **rel_hash;
2345
2346	      offset = o->output_section->vma + o->output_offset - o->vma;
2347	      irel = internal_relocs;
2348	      irelend = irel + o->reloc_count;
2349	      rel_hash = (finfo->section_info[target_index].rel_hashes
2350			  + o->output_section->reloc_count);
2351	      for (; irel < irelend; irel++, rel_hash++)
2352		{
2353		  struct coff_link_hash_entry *h;
2354		  bfd_boolean adjusted;
2355
2356		  *rel_hash = NULL;
2357
2358		  /* Adjust the reloc address and symbol index.  */
2359		  irel->r_vaddr += offset;
2360
2361		  if (irel->r_symndx == -1)
2362		    continue;
2363
2364		  if (adjust_symndx)
2365		    {
2366		      if (! (*adjust_symndx) (output_bfd, finfo->info,
2367					      input_bfd, o, irel,
2368					      &adjusted))
2369			return FALSE;
2370		      if (adjusted)
2371			continue;
2372		    }
2373
2374		  h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2375		  if (h != NULL)
2376		    {
2377		      /* This is a global symbol.  */
2378		      if (h->indx >= 0)
2379			irel->r_symndx = h->indx;
2380		      else
2381			{
2382			  /* This symbol is being written at the end
2383			     of the file, and we do not yet know the
2384			     symbol index.  We save the pointer to the
2385			     hash table entry in the rel_hash list.
2386			     We set the indx field to -2 to indicate
2387			     that this symbol must not be stripped.  */
2388			  *rel_hash = h;
2389			  h->indx = -2;
2390			}
2391		    }
2392		  else
2393		    {
2394		      long indx;
2395
2396		      indx = finfo->sym_indices[irel->r_symndx];
2397		      if (indx != -1)
2398			irel->r_symndx = indx;
2399		      else
2400			{
2401			  struct internal_syment *is;
2402			  const char *name;
2403			  char buf[SYMNMLEN + 1];
2404
2405			  /* This reloc is against a symbol we are
2406                             stripping.  This should have been handled
2407			     by the 'dont_skip_symbol' code in the while
2408			     loop at the top of this function.  */
2409			  is = finfo->internal_syms + irel->r_symndx;
2410
2411			  name = (_bfd_coff_internal_syment_name
2412				  (input_bfd, is, buf));
2413			  if (name == NULL)
2414			    return FALSE;
2415
2416			  if (! ((*finfo->info->callbacks->unattached_reloc)
2417				 (finfo->info, name, input_bfd, o,
2418				  irel->r_vaddr)))
2419			    return FALSE;
2420			}
2421		    }
2422		}
2423
2424	      o->output_section->reloc_count += o->reloc_count;
2425	    }
2426	}
2427
2428      /* Write out the modified section contents.  */
2429      if (secdata == NULL || secdata->stab_info == NULL)
2430	{
2431	  file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2432	  if (! bfd_set_section_contents (output_bfd, o->output_section,
2433					  contents, loc, o->size))
2434	    return FALSE;
2435	}
2436      else
2437	{
2438	  if (! (_bfd_write_section_stabs
2439		 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2440		  o, &secdata->stab_info, contents)))
2441	    return FALSE;
2442	}
2443    }
2444
2445  if (! finfo->info->keep_memory
2446      && ! _bfd_coff_free_symbols (input_bfd))
2447    return FALSE;
2448
2449  return TRUE;
2450}
2451
2452/* Write out a global symbol.  Called via coff_link_hash_traverse.  */
2453
2454bfd_boolean
2455_bfd_coff_write_global_sym (struct coff_link_hash_entry *h, void *data)
2456{
2457  struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2458  bfd *output_bfd;
2459  struct internal_syment isym;
2460  bfd_size_type symesz;
2461  unsigned int i;
2462  file_ptr pos;
2463
2464  output_bfd = finfo->output_bfd;
2465
2466  if (h->root.type == bfd_link_hash_warning)
2467    {
2468      h = (struct coff_link_hash_entry *) h->root.u.i.link;
2469      if (h->root.type == bfd_link_hash_new)
2470	return TRUE;
2471    }
2472
2473  if (h->indx >= 0)
2474    return TRUE;
2475
2476  if (h->indx != -2
2477      && (finfo->info->strip == strip_all
2478	  || (finfo->info->strip == strip_some
2479	      && (bfd_hash_lookup (finfo->info->keep_hash,
2480				   h->root.root.string, FALSE, FALSE)
2481		  == NULL))))
2482    return TRUE;
2483
2484  switch (h->root.type)
2485    {
2486    default:
2487    case bfd_link_hash_new:
2488    case bfd_link_hash_warning:
2489      abort ();
2490      return FALSE;
2491
2492    case bfd_link_hash_undefined:
2493    case bfd_link_hash_undefweak:
2494      isym.n_scnum = N_UNDEF;
2495      isym.n_value = 0;
2496      break;
2497
2498    case bfd_link_hash_defined:
2499    case bfd_link_hash_defweak:
2500      {
2501	asection *sec;
2502
2503	sec = h->root.u.def.section->output_section;
2504	if (bfd_is_abs_section (sec))
2505	  isym.n_scnum = N_ABS;
2506	else
2507	  isym.n_scnum = sec->target_index;
2508	isym.n_value = (h->root.u.def.value
2509			+ h->root.u.def.section->output_offset);
2510	if (! obj_pe (finfo->output_bfd))
2511	  isym.n_value += sec->vma;
2512      }
2513      break;
2514
2515    case bfd_link_hash_common:
2516      isym.n_scnum = N_UNDEF;
2517      isym.n_value = h->root.u.c.size;
2518      break;
2519
2520    case bfd_link_hash_indirect:
2521      /* Just ignore these.  They can't be handled anyhow.  */
2522      return TRUE;
2523    }
2524
2525  if (strlen (h->root.root.string) <= SYMNMLEN)
2526    strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2527  else
2528    {
2529      bfd_boolean hash;
2530      bfd_size_type indx;
2531
2532      hash = TRUE;
2533      if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2534	hash = FALSE;
2535      indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2536				 FALSE);
2537      if (indx == (bfd_size_type) -1)
2538	{
2539	  finfo->failed = TRUE;
2540	  return FALSE;
2541	}
2542      isym._n._n_n._n_zeroes = 0;
2543      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2544    }
2545
2546  isym.n_sclass = h->class;
2547  isym.n_type = h->type;
2548
2549  if (isym.n_sclass == C_NULL)
2550    isym.n_sclass = C_EXT;
2551
2552  /* If doing task linking and this is the pass where we convert
2553     defined globals to statics, then do that conversion now.  If the
2554     symbol is not being converted, just ignore it and it will be
2555     output during a later pass.  */
2556  if (finfo->global_to_static)
2557    {
2558      if (! IS_EXTERNAL (output_bfd, isym))
2559	return TRUE;
2560
2561      isym.n_sclass = C_STAT;
2562    }
2563
2564  /* When a weak symbol is not overridden by a strong one,
2565     turn it into an external symbol when not building a
2566     shared or relocatable object.  */
2567  if (! finfo->info->shared
2568      && ! finfo->info->relocatable
2569      && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2570    isym.n_sclass = C_EXT;
2571
2572  isym.n_numaux = h->numaux;
2573
2574  bfd_coff_swap_sym_out (output_bfd, &isym, finfo->outsyms);
2575
2576  symesz = bfd_coff_symesz (output_bfd);
2577
2578  pos = obj_sym_filepos (output_bfd);
2579  pos += obj_raw_syment_count (output_bfd) * symesz;
2580  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2581      || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2582    {
2583      finfo->failed = TRUE;
2584      return FALSE;
2585    }
2586
2587  h->indx = obj_raw_syment_count (output_bfd);
2588
2589  ++obj_raw_syment_count (output_bfd);
2590
2591  /* Write out any associated aux entries.  Most of the aux entries
2592     will have been modified in _bfd_coff_link_input_bfd.  We have to
2593     handle section aux entries here, now that we have the final
2594     relocation and line number counts.  */
2595  for (i = 0; i < isym.n_numaux; i++)
2596    {
2597      union internal_auxent *auxp;
2598
2599      auxp = h->aux + i;
2600
2601      /* Look for a section aux entry here using the same tests that
2602         coff_swap_aux_out uses.  */
2603      if (i == 0
2604	  && (isym.n_sclass == C_STAT
2605	      || isym.n_sclass == C_HIDDEN)
2606	  && isym.n_type == T_NULL
2607	  && (h->root.type == bfd_link_hash_defined
2608	      || h->root.type == bfd_link_hash_defweak))
2609	{
2610	  asection *sec;
2611
2612	  sec = h->root.u.def.section->output_section;
2613	  if (sec != NULL)
2614	    {
2615	      auxp->x_scn.x_scnlen = sec->size;
2616
2617	      /* For PE, an overflow on the final link reportedly does
2618                 not matter.  FIXME: Why not?  */
2619	      if (sec->reloc_count > 0xffff
2620		  && (! obj_pe (output_bfd)
2621		      || finfo->info->relocatable))
2622		(*_bfd_error_handler)
2623		  (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2624		   bfd_get_filename (output_bfd),
2625		   bfd_get_section_name (output_bfd, sec),
2626		   sec->reloc_count);
2627
2628	      if (sec->lineno_count > 0xffff
2629		  && (! obj_pe (output_bfd)
2630		      || finfo->info->relocatable))
2631		(*_bfd_error_handler)
2632		  (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2633		   bfd_get_filename (output_bfd),
2634		   bfd_get_section_name (output_bfd, sec),
2635		   sec->lineno_count);
2636
2637	      auxp->x_scn.x_nreloc = sec->reloc_count;
2638	      auxp->x_scn.x_nlinno = sec->lineno_count;
2639	      auxp->x_scn.x_checksum = 0;
2640	      auxp->x_scn.x_associated = 0;
2641	      auxp->x_scn.x_comdat = 0;
2642	    }
2643	}
2644
2645      bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2646			     isym.n_sclass, (int) i, isym.n_numaux,
2647			     finfo->outsyms);
2648      if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2649	{
2650	  finfo->failed = TRUE;
2651	  return FALSE;
2652	}
2653      ++obj_raw_syment_count (output_bfd);
2654    }
2655
2656  return TRUE;
2657}
2658
2659/* Write out task global symbols, converting them to statics.  Called
2660   via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
2661   the dirty work, if the symbol we are processing needs conversion.  */
2662
2663bfd_boolean
2664_bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2665{
2666  struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2667  bfd_boolean rtnval = TRUE;
2668  bfd_boolean save_global_to_static;
2669
2670  if (h->root.type == bfd_link_hash_warning)
2671    h = (struct coff_link_hash_entry *) h->root.u.i.link;
2672
2673  if (h->indx < 0)
2674    {
2675      switch (h->root.type)
2676	{
2677	case bfd_link_hash_defined:
2678	case bfd_link_hash_defweak:
2679	  save_global_to_static = finfo->global_to_static;
2680	  finfo->global_to_static = TRUE;
2681	  rtnval = _bfd_coff_write_global_sym (h, data);
2682	  finfo->global_to_static = save_global_to_static;
2683	  break;
2684	default:
2685	  break;
2686	}
2687    }
2688  return (rtnval);
2689}
2690
2691/* Handle a link order which is supposed to generate a reloc.  */
2692
2693bfd_boolean
2694_bfd_coff_reloc_link_order (bfd *output_bfd,
2695			    struct coff_final_link_info *finfo,
2696			    asection *output_section,
2697			    struct bfd_link_order *link_order)
2698{
2699  reloc_howto_type *howto;
2700  struct internal_reloc *irel;
2701  struct coff_link_hash_entry **rel_hash_ptr;
2702
2703  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2704  if (howto == NULL)
2705    {
2706      bfd_set_error (bfd_error_bad_value);
2707      return FALSE;
2708    }
2709
2710  if (link_order->u.reloc.p->addend != 0)
2711    {
2712      bfd_size_type size;
2713      bfd_byte *buf;
2714      bfd_reloc_status_type rstat;
2715      bfd_boolean ok;
2716      file_ptr loc;
2717
2718      size = bfd_get_reloc_size (howto);
2719      buf = bfd_zmalloc (size);
2720      if (buf == NULL)
2721	return FALSE;
2722
2723      rstat = _bfd_relocate_contents (howto, output_bfd,
2724				      (bfd_vma) link_order->u.reloc.p->addend,\
2725				      buf);
2726      switch (rstat)
2727	{
2728	case bfd_reloc_ok:
2729	  break;
2730	default:
2731	case bfd_reloc_outofrange:
2732	  abort ();
2733	case bfd_reloc_overflow:
2734	  if (! ((*finfo->info->callbacks->reloc_overflow)
2735		 (finfo->info, NULL,
2736		  (link_order->type == bfd_section_reloc_link_order
2737		   ? bfd_section_name (output_bfd,
2738				       link_order->u.reloc.p->u.section)
2739		   : link_order->u.reloc.p->u.name),
2740		  howto->name, link_order->u.reloc.p->addend,
2741		  (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2742	    {
2743	      free (buf);
2744	      return FALSE;
2745	    }
2746	  break;
2747	}
2748      loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2749      ok = bfd_set_section_contents (output_bfd, output_section, buf,
2750                                     loc, size);
2751      free (buf);
2752      if (! ok)
2753	return FALSE;
2754    }
2755
2756  /* Store the reloc information in the right place.  It will get
2757     swapped and written out at the end of the final_link routine.  */
2758  irel = (finfo->section_info[output_section->target_index].relocs
2759	  + output_section->reloc_count);
2760  rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2761		  + output_section->reloc_count);
2762
2763  memset (irel, 0, sizeof (struct internal_reloc));
2764  *rel_hash_ptr = NULL;
2765
2766  irel->r_vaddr = output_section->vma + link_order->offset;
2767
2768  if (link_order->type == bfd_section_reloc_link_order)
2769    {
2770      /* We need to somehow locate a symbol in the right section.  The
2771         symbol must either have a value of zero, or we must adjust
2772         the addend by the value of the symbol.  FIXME: Write this
2773         when we need it.  The old linker couldn't handle this anyhow.  */
2774      abort ();
2775      *rel_hash_ptr = NULL;
2776      irel->r_symndx = 0;
2777    }
2778  else
2779    {
2780      struct coff_link_hash_entry *h;
2781
2782      h = ((struct coff_link_hash_entry *)
2783	   bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2784					 link_order->u.reloc.p->u.name,
2785					 FALSE, FALSE, TRUE));
2786      if (h != NULL)
2787	{
2788	  if (h->indx >= 0)
2789	    irel->r_symndx = h->indx;
2790	  else
2791	    {
2792	      /* Set the index to -2 to force this symbol to get
2793		 written out.  */
2794	      h->indx = -2;
2795	      *rel_hash_ptr = h;
2796	      irel->r_symndx = 0;
2797	    }
2798	}
2799      else
2800	{
2801	  if (! ((*finfo->info->callbacks->unattached_reloc)
2802		 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2803		  (asection *) NULL, (bfd_vma) 0)))
2804	    return FALSE;
2805	  irel->r_symndx = 0;
2806	}
2807    }
2808
2809  /* FIXME: Is this always right?  */
2810  irel->r_type = howto->type;
2811
2812  /* r_size is only used on the RS/6000, which needs its own linker
2813     routines anyhow.  r_extern is only used for ECOFF.  */
2814
2815  /* FIXME: What is the right value for r_offset?  Is zero OK?  */
2816  ++output_section->reloc_count;
2817
2818  return TRUE;
2819}
2820
2821/* A basic reloc handling routine which may be used by processors with
2822   simple relocs.  */
2823
2824bfd_boolean
2825_bfd_coff_generic_relocate_section (bfd *output_bfd,
2826				    struct bfd_link_info *info,
2827				    bfd *input_bfd,
2828				    asection *input_section,
2829				    bfd_byte *contents,
2830				    struct internal_reloc *relocs,
2831				    struct internal_syment *syms,
2832				    asection **sections)
2833{
2834  struct internal_reloc *rel;
2835  struct internal_reloc *relend;
2836
2837  rel = relocs;
2838  relend = rel + input_section->reloc_count;
2839  for (; rel < relend; rel++)
2840    {
2841      long symndx;
2842      struct coff_link_hash_entry *h;
2843      struct internal_syment *sym;
2844      bfd_vma addend;
2845      bfd_vma val;
2846      reloc_howto_type *howto;
2847      bfd_reloc_status_type rstat;
2848
2849      symndx = rel->r_symndx;
2850
2851      if (symndx == -1)
2852	{
2853	  h = NULL;
2854	  sym = NULL;
2855	}
2856      else if (symndx < 0
2857	       || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2858	{
2859	  (*_bfd_error_handler)
2860	    ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
2861	  return FALSE;
2862	}
2863      else
2864	{
2865	  h = obj_coff_sym_hashes (input_bfd)[symndx];
2866	  sym = syms + symndx;
2867	}
2868
2869      /* COFF treats common symbols in one of two ways.  Either the
2870         size of the symbol is included in the section contents, or it
2871         is not.  We assume that the size is not included, and force
2872         the rtype_to_howto function to adjust the addend as needed.  */
2873      if (sym != NULL && sym->n_scnum != 0)
2874	addend = - sym->n_value;
2875      else
2876	addend = 0;
2877
2878      howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2879				       sym, &addend);
2880      if (howto == NULL)
2881	return FALSE;
2882
2883      /* If we are doing a relocatable link, then we can just ignore
2884         a PC relative reloc that is pcrel_offset.  It will already
2885         have the correct value.  If this is not a relocatable link,
2886         then we should ignore the symbol value.  */
2887      if (howto->pc_relative && howto->pcrel_offset)
2888	{
2889	  if (info->relocatable)
2890	    continue;
2891	  if (sym != NULL && sym->n_scnum != 0)
2892	    addend += sym->n_value;
2893	}
2894
2895      val = 0;
2896
2897      if (h == NULL)
2898	{
2899	  asection *sec;
2900
2901	  if (symndx == -1)
2902	    {
2903	      sec = bfd_abs_section_ptr;
2904	      val = 0;
2905	    }
2906	  else
2907	    {
2908	      sec = sections[symndx];
2909              val = (sec->output_section->vma
2910		     + sec->output_offset
2911		     + sym->n_value);
2912	      if (! obj_pe (input_bfd))
2913		val -= sec->vma;
2914	    }
2915	}
2916      else
2917	{
2918	  if (h->root.type == bfd_link_hash_defined
2919	      || h->root.type == bfd_link_hash_defweak)
2920	    {
2921	      /* Defined weak symbols are a GNU extension. */
2922	      asection *sec;
2923
2924	      sec = h->root.u.def.section;
2925	      val = (h->root.u.def.value
2926		     + sec->output_section->vma
2927		     + sec->output_offset);
2928	    }
2929
2930	  else if (h->root.type == bfd_link_hash_undefweak)
2931	    {
2932              if (h->class == C_NT_WEAK && h->numaux == 1)
2933		{
2934		  /* See _Microsoft Portable Executable and Common Object
2935                     File Format Specification_, section 5.5.3.
2936		     Note that weak symbols without aux records are a GNU
2937		     extension.
2938		     FIXME: All weak externals are treated as having
2939		     characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2940		     These behave as per SVR4 ABI:  A library member
2941		     will resolve a weak external only if a normal
2942		     external causes the library member to be linked.
2943		     See also linker.c: generic_link_check_archive_element. */
2944		  asection *sec;
2945		  struct coff_link_hash_entry *h2 =
2946		    input_bfd->tdata.coff_obj_data->sym_hashes[
2947		    h->aux->x_sym.x_tagndx.l];
2948
2949		  if (!h2 || h2->root.type == bfd_link_hash_undefined)
2950		    {
2951		      sec = bfd_abs_section_ptr;
2952		      val = 0;
2953		    }
2954		  else
2955		    {
2956		      sec = h2->root.u.def.section;
2957		      val = h2->root.u.def.value
2958			+ sec->output_section->vma + sec->output_offset;
2959		    }
2960		}
2961	      else
2962                /* This is a GNU extension.  */
2963		val = 0;
2964	    }
2965
2966	  else if (! info->relocatable)
2967	    {
2968	      if (! ((*info->callbacks->undefined_symbol)
2969		     (info, h->root.root.string, input_bfd, input_section,
2970		      rel->r_vaddr - input_section->vma, TRUE)))
2971		return FALSE;
2972	    }
2973	}
2974
2975      if (info->base_file)
2976	{
2977	  /* Emit a reloc if the backend thinks it needs it.  */
2978	  if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2979	    {
2980	      /* Relocation to a symbol in a section which isn't
2981		 absolute.  We output the address here to a file.
2982		 This file is then read by dlltool when generating the
2983		 reloc section.  Note that the base file is not
2984		 portable between systems.  We write out a long here,
2985		 and dlltool reads in a long.  */
2986	      long addr = (rel->r_vaddr
2987			   - input_section->vma
2988			   + input_section->output_offset
2989			   + input_section->output_section->vma);
2990	      if (coff_data (output_bfd)->pe)
2991		addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2992	      if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2993		  != sizeof (long))
2994		{
2995		  bfd_set_error (bfd_error_system_call);
2996		  return FALSE;
2997		}
2998	    }
2999	}
3000
3001      rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3002					contents,
3003					rel->r_vaddr - input_section->vma,
3004					val, addend);
3005
3006      switch (rstat)
3007	{
3008	default:
3009	  abort ();
3010	case bfd_reloc_ok:
3011	  break;
3012	case bfd_reloc_outofrange:
3013	  (*_bfd_error_handler)
3014	    (_("%B: bad reloc address 0x%lx in section `%A'"),
3015	     input_bfd, input_section, (unsigned long) rel->r_vaddr);
3016	  return FALSE;
3017	case bfd_reloc_overflow:
3018	  {
3019	    const char *name;
3020	    char buf[SYMNMLEN + 1];
3021
3022	    if (symndx == -1)
3023	      name = "*ABS*";
3024	    else if (h != NULL)
3025	      name = NULL;
3026	    else
3027	      {
3028		name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3029		if (name == NULL)
3030		  return FALSE;
3031	      }
3032
3033	    if (! ((*info->callbacks->reloc_overflow)
3034		   (info, (h ? &h->root : NULL), name, howto->name,
3035		    (bfd_vma) 0, input_bfd, input_section,
3036		    rel->r_vaddr - input_section->vma)))
3037	      return FALSE;
3038	  }
3039	}
3040    }
3041  return TRUE;
3042}
3043