Back to index

cell-binutils  2.17cvs20070401
cofflink.c
Go to the documentation of this file.
00001 /* COFF specific linker code.
00002    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
00003    2004, 2005, 2006 Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor, Cygnus Support.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* This file contains the COFF backend linker code.  */
00023 
00024 #include "bfd.h"
00025 #include "sysdep.h"
00026 #include "bfdlink.h"
00027 #include "libbfd.h"
00028 #include "coff/internal.h"
00029 #include "libcoff.h"
00030 #include "safe-ctype.h"
00031 
00032 static bfd_boolean coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info);
00033 static bfd_boolean coff_link_check_archive_element (bfd *abfd, struct bfd_link_info *info, bfd_boolean *pneeded);
00034 static bfd_boolean coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info);
00035 
00036 /* Return TRUE if SYM is a weak, external symbol.  */
00037 #define IS_WEAK_EXTERNAL(abfd, sym)                     \
00038   ((sym).n_sclass == C_WEAKEXT                          \
00039    || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
00040 
00041 /* Return TRUE if SYM is an external symbol.  */
00042 #define IS_EXTERNAL(abfd, sym)                          \
00043   ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
00044 
00045 /* Define macros so that the ISFCN, et. al., macros work correctly.
00046    These macros are defined in include/coff/internal.h in terms of
00047    N_TMASK, etc.  These definitions require a user to define local
00048    variables with the appropriate names, and with values from the
00049    coff_data (abfd) structure.  */
00050 
00051 #define N_TMASK n_tmask
00052 #define N_BTSHFT n_btshft
00053 #define N_BTMASK n_btmask
00054 
00055 /* Create an entry in a COFF linker hash table.  */
00056 
00057 struct bfd_hash_entry *
00058 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
00059                           struct bfd_hash_table *table,
00060                           const char *string)
00061 {
00062   struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
00063 
00064   /* Allocate the structure if it has not already been allocated by a
00065      subclass.  */
00066   if (ret == (struct coff_link_hash_entry *) NULL)
00067     ret = ((struct coff_link_hash_entry *)
00068           bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
00069   if (ret == (struct coff_link_hash_entry *) NULL)
00070     return (struct bfd_hash_entry *) ret;
00071 
00072   /* Call the allocation method of the superclass.  */
00073   ret = ((struct coff_link_hash_entry *)
00074         _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
00075                              table, string));
00076   if (ret != (struct coff_link_hash_entry *) NULL)
00077     {
00078       /* Set local fields.  */
00079       ret->indx = -1;
00080       ret->type = T_NULL;
00081       ret->class = C_NULL;
00082       ret->numaux = 0;
00083       ret->auxbfd = NULL;
00084       ret->aux = NULL;
00085     }
00086 
00087   return (struct bfd_hash_entry *) ret;
00088 }
00089 
00090 /* Initialize a COFF linker hash table.  */
00091 
00092 bfd_boolean
00093 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
00094                             bfd *abfd,
00095                             struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
00096                                                            struct bfd_hash_table *,
00097                                                            const char *),
00098                             unsigned int entsize)
00099 {
00100   memset (&table->stab_info, 0, sizeof (table->stab_info));
00101   return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
00102 }
00103 
00104 /* Create a COFF linker hash table.  */
00105 
00106 struct bfd_link_hash_table *
00107 _bfd_coff_link_hash_table_create (bfd *abfd)
00108 {
00109   struct coff_link_hash_table *ret;
00110   bfd_size_type amt = sizeof (struct coff_link_hash_table);
00111 
00112   ret = bfd_malloc (amt);
00113   if (ret == NULL)
00114     return NULL;
00115 
00116   if (! _bfd_coff_link_hash_table_init (ret, abfd,
00117                                    _bfd_coff_link_hash_newfunc,
00118                                    sizeof (struct coff_link_hash_entry)))
00119     {
00120       free (ret);
00121       return (struct bfd_link_hash_table *) NULL;
00122     }
00123   return &ret->root;
00124 }
00125 
00126 /* Create an entry in a COFF debug merge hash table.  */
00127 
00128 struct bfd_hash_entry *
00129 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
00130                                 struct bfd_hash_table *table,
00131                                 const char *string)
00132 {
00133   struct coff_debug_merge_hash_entry *ret =
00134     (struct coff_debug_merge_hash_entry *) entry;
00135 
00136   /* Allocate the structure if it has not already been allocated by a
00137      subclass.  */
00138   if (ret == (struct coff_debug_merge_hash_entry *) NULL)
00139     ret = ((struct coff_debug_merge_hash_entry *)
00140           bfd_hash_allocate (table,
00141                            sizeof (struct coff_debug_merge_hash_entry)));
00142   if (ret == (struct coff_debug_merge_hash_entry *) NULL)
00143     return (struct bfd_hash_entry *) ret;
00144 
00145   /* Call the allocation method of the superclass.  */
00146   ret = ((struct coff_debug_merge_hash_entry *)
00147         bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
00148   if (ret != (struct coff_debug_merge_hash_entry *) NULL)
00149     {
00150       /* Set local fields.  */
00151       ret->types = NULL;
00152     }
00153 
00154   return (struct bfd_hash_entry *) ret;
00155 }
00156 
00157 /* Given a COFF BFD, add symbols to the global hash table as
00158    appropriate.  */
00159 
00160 bfd_boolean
00161 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
00162 {
00163   switch (bfd_get_format (abfd))
00164     {
00165     case bfd_object:
00166       return coff_link_add_object_symbols (abfd, info);
00167     case bfd_archive:
00168       return _bfd_generic_link_add_archive_symbols
00169        (abfd, info, coff_link_check_archive_element);
00170     default:
00171       bfd_set_error (bfd_error_wrong_format);
00172       return FALSE;
00173     }
00174 }
00175 
00176 /* Add symbols from a COFF object file.  */
00177 
00178 static bfd_boolean
00179 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
00180 {
00181   if (! _bfd_coff_get_external_symbols (abfd))
00182     return FALSE;
00183   if (! coff_link_add_symbols (abfd, info))
00184     return FALSE;
00185 
00186   if (! info->keep_memory
00187       && ! _bfd_coff_free_symbols (abfd))
00188     return FALSE;
00189 
00190   return TRUE;
00191 }
00192 
00193 /* Look through the symbols to see if this object file should be
00194    included in the link.  */
00195 
00196 static bfd_boolean
00197 coff_link_check_ar_symbols (bfd *abfd,
00198                          struct bfd_link_info *info,
00199                          bfd_boolean *pneeded)
00200 {
00201   bfd_size_type symesz;
00202   bfd_byte *esym;
00203   bfd_byte *esym_end;
00204 
00205   *pneeded = FALSE;
00206 
00207   symesz = bfd_coff_symesz (abfd);
00208   esym = (bfd_byte *) obj_coff_external_syms (abfd);
00209   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
00210   while (esym < esym_end)
00211     {
00212       struct internal_syment sym;
00213       enum coff_symbol_classification classification;
00214 
00215       bfd_coff_swap_sym_in (abfd, esym, &sym);
00216 
00217       classification = bfd_coff_classify_symbol (abfd, &sym);
00218       if (classification == COFF_SYMBOL_GLOBAL
00219          || classification == COFF_SYMBOL_COMMON)
00220        {
00221          const char *name;
00222          char buf[SYMNMLEN + 1];
00223          struct bfd_link_hash_entry *h;
00224 
00225          /* This symbol is externally visible, and is defined by this
00226              object file.  */
00227          name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
00228          if (name == NULL)
00229            return FALSE;
00230          h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
00231 
00232          /* Auto import.  */
00233          if (!h
00234              && info->pei386_auto_import
00235              && CONST_STRNEQ (name, "__imp_"))
00236            h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE);
00237 
00238          /* We are only interested in symbols that are currently
00239             undefined.  If a symbol is currently known to be common,
00240             COFF linkers do not bring in an object file which defines
00241             it.  */
00242          if (h != (struct bfd_link_hash_entry *) NULL
00243              && h->type == bfd_link_hash_undefined)
00244            {
00245              if (! (*info->callbacks->add_archive_element) (info, abfd, name))
00246               return FALSE;
00247              *pneeded = TRUE;
00248              return TRUE;
00249            }
00250        }
00251 
00252       esym += (sym.n_numaux + 1) * symesz;
00253     }
00254 
00255   /* We do not need this object file.  */
00256   return TRUE;
00257 }
00258 
00259 /* Check a single archive element to see if we need to include it in
00260    the link.  *PNEEDED is set according to whether this element is
00261    needed in the link or not.  This is called via
00262    _bfd_generic_link_add_archive_symbols.  */
00263 
00264 static bfd_boolean
00265 coff_link_check_archive_element (bfd *abfd,
00266                              struct bfd_link_info *info,
00267                              bfd_boolean *pneeded)
00268 {
00269   if (! _bfd_coff_get_external_symbols (abfd))
00270     return FALSE;
00271 
00272   if (! coff_link_check_ar_symbols (abfd, info, pneeded))
00273     return FALSE;
00274 
00275   if (*pneeded
00276       && ! coff_link_add_symbols (abfd, info))
00277     return FALSE;
00278 
00279   if ((! info->keep_memory || ! *pneeded)
00280       && ! _bfd_coff_free_symbols (abfd))
00281     return FALSE;
00282 
00283   return TRUE;
00284 }
00285 
00286 /* Add all the symbols from an object file to the hash table.  */
00287 
00288 static bfd_boolean
00289 coff_link_add_symbols (bfd *abfd,
00290                      struct bfd_link_info *info)
00291 {
00292   unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
00293   unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
00294   unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
00295   bfd_boolean keep_syms;
00296   bfd_boolean default_copy;
00297   bfd_size_type symcount;
00298   struct coff_link_hash_entry **sym_hash;
00299   bfd_size_type symesz;
00300   bfd_byte *esym;
00301   bfd_byte *esym_end;
00302   bfd_size_type amt;
00303 
00304   /* Keep the symbols during this function, in case the linker needs
00305      to read the generic symbols in order to report an error message.  */
00306   keep_syms = obj_coff_keep_syms (abfd);
00307   obj_coff_keep_syms (abfd) = TRUE;
00308 
00309   if (info->keep_memory)
00310     default_copy = FALSE;
00311   else
00312     default_copy = TRUE;
00313 
00314   symcount = obj_raw_syment_count (abfd);
00315 
00316   /* We keep a list of the linker hash table entries that correspond
00317      to particular symbols.  */
00318   amt = symcount * sizeof (struct coff_link_hash_entry *);
00319   sym_hash = bfd_zalloc (abfd, amt);
00320   if (sym_hash == NULL && symcount != 0)
00321     goto error_return;
00322   obj_coff_sym_hashes (abfd) = sym_hash;
00323 
00324   symesz = bfd_coff_symesz (abfd);
00325   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
00326   esym = (bfd_byte *) obj_coff_external_syms (abfd);
00327   esym_end = esym + symcount * symesz;
00328   while (esym < esym_end)
00329     {
00330       struct internal_syment sym;
00331       enum coff_symbol_classification classification;
00332       bfd_boolean copy;
00333 
00334       bfd_coff_swap_sym_in (abfd, esym, &sym);
00335 
00336       classification = bfd_coff_classify_symbol (abfd, &sym);
00337       if (classification != COFF_SYMBOL_LOCAL)
00338        {
00339          const char *name;
00340          char buf[SYMNMLEN + 1];
00341          flagword flags;
00342          asection *section;
00343          bfd_vma value;
00344          bfd_boolean addit;
00345 
00346          /* This symbol is externally visible.  */
00347 
00348          name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
00349          if (name == NULL)
00350            goto error_return;
00351 
00352          /* We must copy the name into memory if we got it from the
00353              syment itself, rather than the string table.  */
00354          copy = default_copy;
00355          if (sym._n._n_n._n_zeroes != 0
00356              || sym._n._n_n._n_offset == 0)
00357            copy = TRUE;
00358 
00359          value = sym.n_value;
00360 
00361          switch (classification)
00362            {
00363            default:
00364              abort ();
00365 
00366            case COFF_SYMBOL_GLOBAL:
00367              flags = BSF_EXPORT | BSF_GLOBAL;
00368              section = coff_section_from_bfd_index (abfd, sym.n_scnum);
00369              if (! obj_pe (abfd))
00370               value -= section->vma;
00371              break;
00372 
00373            case COFF_SYMBOL_UNDEFINED:
00374              flags = 0;
00375              section = bfd_und_section_ptr;
00376              break;
00377 
00378            case COFF_SYMBOL_COMMON:
00379              flags = BSF_GLOBAL;
00380              section = bfd_com_section_ptr;
00381              break;
00382 
00383            case COFF_SYMBOL_PE_SECTION:
00384              flags = BSF_SECTION_SYM | BSF_GLOBAL;
00385              section = coff_section_from_bfd_index (abfd, sym.n_scnum);
00386              break;
00387            }
00388 
00389          if (IS_WEAK_EXTERNAL (abfd, sym))
00390            flags = BSF_WEAK;
00391 
00392          addit = TRUE;
00393 
00394          /* In the PE format, section symbols actually refer to the
00395              start of the output section.  We handle them specially
00396              here.  */
00397          if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
00398            {
00399              *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
00400                                            name, FALSE, copy, FALSE);
00401              if (*sym_hash != NULL)
00402               {
00403                 if (((*sym_hash)->coff_link_hash_flags
00404                      & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
00405                     && (*sym_hash)->root.type != bfd_link_hash_undefined
00406                     && (*sym_hash)->root.type != bfd_link_hash_undefweak)
00407                   (*_bfd_error_handler)
00408                     ("Warning: symbol `%s' is both section and non-section",
00409                      name);
00410 
00411                 addit = FALSE;
00412               }
00413            }
00414 
00415          /* The Microsoft Visual C compiler does string pooling by
00416             hashing the constants to an internal symbol name, and
00417             relying on the linker comdat support to discard
00418             duplicate names.  However, if one string is a literal and
00419             one is a data initializer, one will end up in the .data
00420             section and one will end up in the .rdata section.  The
00421             Microsoft linker will combine them into the .data
00422             section, which seems to be wrong since it might cause the
00423             literal to change.
00424 
00425             As long as there are no external references to the
00426             symbols, which there shouldn't be, we can treat the .data
00427             and .rdata instances as separate symbols.  The comdat
00428             code in the linker will do the appropriate merging.  Here
00429             we avoid getting a multiple definition error for one of
00430             these special symbols.
00431 
00432             FIXME: I don't think this will work in the case where
00433             there are two object files which use the constants as a
00434             literal and two object files which use it as a data
00435             initializer.  One or the other of the second object files
00436             is going to wind up with an inappropriate reference.  */
00437          if (obj_pe (abfd)
00438              && (classification == COFF_SYMBOL_GLOBAL
00439                 || classification == COFF_SYMBOL_PE_SECTION)
00440              && coff_section_data (abfd, section) != NULL
00441              && coff_section_data (abfd, section)->comdat != NULL
00442              && CONST_STRNEQ (name, "??_")
00443              && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
00444            {
00445              if (*sym_hash == NULL)
00446               *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
00447                                              name, FALSE, copy, FALSE);
00448              if (*sym_hash != NULL
00449                 && (*sym_hash)->root.type == bfd_link_hash_defined
00450                 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
00451                 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
00452                           coff_section_data (abfd, section)->comdat->name) == 0)
00453               addit = FALSE;
00454            }
00455 
00456          if (addit)
00457            {
00458              if (! (bfd_coff_link_add_one_symbol
00459                    (info, abfd, name, flags, section, value,
00460                     (const char *) NULL, copy, FALSE,
00461                     (struct bfd_link_hash_entry **) sym_hash)))
00462               goto error_return;
00463            }
00464 
00465          if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
00466            (*sym_hash)->coff_link_hash_flags |=
00467              COFF_LINK_HASH_PE_SECTION_SYMBOL;
00468 
00469          /* Limit the alignment of a common symbol to the possible
00470              alignment of a section.  There is no point to permitting
00471              a higher alignment for a common symbol: we can not
00472              guarantee it, and it may cause us to allocate extra space
00473              in the common section.  */
00474          if (section == bfd_com_section_ptr
00475              && (*sym_hash)->root.type == bfd_link_hash_common
00476              && ((*sym_hash)->root.u.c.p->alignment_power
00477                 > bfd_coff_default_section_alignment_power (abfd)))
00478            (*sym_hash)->root.u.c.p->alignment_power
00479              = bfd_coff_default_section_alignment_power (abfd);
00480 
00481          if (info->hash->creator->flavour == bfd_get_flavour (abfd))
00482            {
00483              /* If we don't have any symbol information currently in
00484                  the hash table, or if we are looking at a symbol
00485                  definition, then update the symbol class and type in
00486                  the hash table.  */
00487              if (((*sym_hash)->class == C_NULL
00488                  && (*sym_hash)->type == T_NULL)
00489                 || sym.n_scnum != 0
00490                 || (sym.n_value != 0
00491                     && (*sym_hash)->root.type != bfd_link_hash_defined
00492                     && (*sym_hash)->root.type != bfd_link_hash_defweak))
00493               {
00494                 (*sym_hash)->class = sym.n_sclass;
00495                 if (sym.n_type != T_NULL)
00496                   {
00497                     /* We want to warn if the type changed, but not
00498                       if it changed from an unspecified type.
00499                       Testing the whole type byte may work, but the
00500                       change from (e.g.) a function of unspecified
00501                       type to function of known type also wants to
00502                       skip the warning.  */
00503                     if ((*sym_hash)->type != T_NULL
00504                        && (*sym_hash)->type != sym.n_type
00505                         && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
00506                              && (BTYPE ((*sym_hash)->type) == T_NULL
00507                                  || BTYPE (sym.n_type) == T_NULL)))
00508                      (*_bfd_error_handler)
00509                        (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
00510                         abfd, name, (*sym_hash)->type, sym.n_type);
00511 
00512                     /* We don't want to change from a meaningful
00513                       base type to a null one, but if we know
00514                       nothing, take what little we might now know.  */
00515                     if (BTYPE (sym.n_type) != T_NULL
00516                        || (*sym_hash)->type == T_NULL)
00517                      (*sym_hash)->type = sym.n_type;
00518                   }
00519                 (*sym_hash)->auxbfd = abfd;
00520                 if (sym.n_numaux != 0)
00521                   {
00522                     union internal_auxent *alloc;
00523                     unsigned int i;
00524                     bfd_byte *eaux;
00525                     union internal_auxent *iaux;
00526 
00527                     (*sym_hash)->numaux = sym.n_numaux;
00528                     alloc = ((union internal_auxent *)
00529                             bfd_hash_allocate (&info->hash->table,
00530                                             (sym.n_numaux
00531                                              * sizeof (*alloc))));
00532                     if (alloc == NULL)
00533                      goto error_return;
00534                     for (i = 0, eaux = esym + symesz, iaux = alloc;
00535                         i < sym.n_numaux;
00536                         i++, eaux += symesz, iaux++)
00537                      bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
00538                                          sym.n_sclass, (int) i,
00539                                          sym.n_numaux, iaux);
00540                     (*sym_hash)->aux = alloc;
00541                   }
00542               }
00543            }
00544 
00545          if (classification == COFF_SYMBOL_PE_SECTION
00546              && (*sym_hash)->numaux != 0)
00547            {
00548              /* Some PE sections (such as .bss) have a zero size in
00549                  the section header, but a non-zero size in the AUX
00550                  record.  Correct that here.
00551 
00552                FIXME: This is not at all the right place to do this.
00553                For example, it won't help objdump.  This needs to be
00554                done when we swap in the section header.  */
00555              BFD_ASSERT ((*sym_hash)->numaux == 1);
00556              if (section->size == 0)
00557               section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
00558 
00559              /* FIXME: We could test whether the section sizes
00560                  matches the size in the aux entry, but apparently
00561                  that sometimes fails unexpectedly.  */
00562            }
00563        }
00564 
00565       esym += (sym.n_numaux + 1) * symesz;
00566       sym_hash += sym.n_numaux + 1;
00567     }
00568 
00569   /* If this is a non-traditional, non-relocatable link, try to
00570      optimize the handling of any .stab/.stabstr sections.  */
00571   if (! info->relocatable
00572       && ! info->traditional_format
00573       && info->hash->creator->flavour == bfd_get_flavour (abfd)
00574       && (info->strip != strip_all && info->strip != strip_debugger))
00575     {
00576       asection *stabstr;
00577 
00578       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
00579 
00580       if (stabstr != NULL)
00581        {
00582          bfd_size_type string_offset = 0;
00583          asection *stab;
00584          
00585          for (stab = abfd->sections; stab; stab = stab->next)
00586            if (CONST_STRNEQ (stab->name, ".stab")
00587               && (!stab->name[5]
00588                   || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
00589            {
00590              struct coff_link_hash_table *table;
00591              struct coff_section_tdata *secdata
00592               = coff_section_data (abfd, stab);
00593              
00594              if (secdata == NULL)
00595               {
00596                 amt = sizeof (struct coff_section_tdata);
00597                 stab->used_by_bfd = bfd_zalloc (abfd, amt);
00598                 if (stab->used_by_bfd == NULL)
00599                   goto error_return;
00600                 secdata = coff_section_data (abfd, stab);
00601               }
00602 
00603              table = coff_hash_table (info);
00604 
00605              if (! _bfd_link_section_stabs (abfd, &table->stab_info,
00606                                         stab, stabstr,
00607                                         &secdata->stab_info,
00608                                         &string_offset))
00609               goto error_return;
00610            }
00611        }
00612     }
00613 
00614   obj_coff_keep_syms (abfd) = keep_syms;
00615 
00616   return TRUE;
00617 
00618  error_return:
00619   obj_coff_keep_syms (abfd) = keep_syms;
00620   return FALSE;
00621 }
00622 
00623 /* Do the final link step.  */
00624 
00625 bfd_boolean
00626 _bfd_coff_final_link (bfd *abfd,
00627                     struct bfd_link_info *info)
00628 {
00629   bfd_size_type symesz;
00630   struct coff_final_link_info finfo;
00631   bfd_boolean debug_merge_allocated;
00632   bfd_boolean long_section_names;
00633   asection *o;
00634   struct bfd_link_order *p;
00635   bfd_size_type max_sym_count;
00636   bfd_size_type max_lineno_count;
00637   bfd_size_type max_reloc_count;
00638   bfd_size_type max_output_reloc_count;
00639   bfd_size_type max_contents_size;
00640   file_ptr rel_filepos;
00641   unsigned int relsz;
00642   file_ptr line_filepos;
00643   unsigned int linesz;
00644   bfd *sub;
00645   bfd_byte *external_relocs = NULL;
00646   char strbuf[STRING_SIZE_SIZE];
00647   bfd_size_type amt;
00648 
00649   symesz = bfd_coff_symesz (abfd);
00650 
00651   finfo.info = info;
00652   finfo.output_bfd = abfd;
00653   finfo.strtab = NULL;
00654   finfo.section_info = NULL;
00655   finfo.last_file_index = -1;
00656   finfo.last_bf_index = -1;
00657   finfo.internal_syms = NULL;
00658   finfo.sec_ptrs = NULL;
00659   finfo.sym_indices = NULL;
00660   finfo.outsyms = NULL;
00661   finfo.linenos = NULL;
00662   finfo.contents = NULL;
00663   finfo.external_relocs = NULL;
00664   finfo.internal_relocs = NULL;
00665   finfo.global_to_static = FALSE;
00666   debug_merge_allocated = FALSE;
00667 
00668   coff_data (abfd)->link_info = info;
00669 
00670   finfo.strtab = _bfd_stringtab_init ();
00671   if (finfo.strtab == NULL)
00672     goto error_return;
00673 
00674   if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
00675     goto error_return;
00676   debug_merge_allocated = TRUE;
00677 
00678   /* Compute the file positions for all the sections.  */
00679   if (! abfd->output_has_begun)
00680     {
00681       if (! bfd_coff_compute_section_file_positions (abfd))
00682        goto error_return;
00683     }
00684 
00685   /* Count the line numbers and relocation entries required for the
00686      output file.  Set the file positions for the relocs.  */
00687   rel_filepos = obj_relocbase (abfd);
00688   relsz = bfd_coff_relsz (abfd);
00689   max_contents_size = 0;
00690   max_lineno_count = 0;
00691   max_reloc_count = 0;
00692 
00693   long_section_names = FALSE;
00694   for (o = abfd->sections; o != NULL; o = o->next)
00695     {
00696       o->reloc_count = 0;
00697       o->lineno_count = 0;
00698       for (p = o->map_head.link_order; p != NULL; p = p->next)
00699        {
00700          if (p->type == bfd_indirect_link_order)
00701            {
00702              asection *sec;
00703 
00704              sec = p->u.indirect.section;
00705 
00706              /* Mark all sections which are to be included in the
00707                link.  This will normally be every section.  We need
00708                to do this so that we can identify any sections which
00709                the linker has decided to not include.  */
00710              sec->linker_mark = TRUE;
00711 
00712              if (info->strip == strip_none
00713                 || info->strip == strip_some)
00714               o->lineno_count += sec->lineno_count;
00715 
00716              if (info->relocatable)
00717               o->reloc_count += sec->reloc_count;
00718 
00719              if (sec->rawsize > max_contents_size)
00720               max_contents_size = sec->rawsize;
00721              if (sec->size > max_contents_size)
00722               max_contents_size = sec->size;
00723              if (sec->lineno_count > max_lineno_count)
00724               max_lineno_count = sec->lineno_count;
00725              if (sec->reloc_count > max_reloc_count)
00726               max_reloc_count = sec->reloc_count;
00727            }
00728          else if (info->relocatable
00729                  && (p->type == bfd_section_reloc_link_order
00730                      || p->type == bfd_symbol_reloc_link_order))
00731            ++o->reloc_count;
00732        }
00733       if (o->reloc_count == 0)
00734        o->rel_filepos = 0;
00735       else
00736        {
00737          o->flags |= SEC_RELOC;
00738          o->rel_filepos = rel_filepos;
00739          rel_filepos += o->reloc_count * relsz;
00740          /* In PE COFF, if there are at least 0xffff relocations an
00741             extra relocation will be written out to encode the count.  */
00742          if (obj_pe (abfd) && o->reloc_count >= 0xffff)
00743            rel_filepos += relsz;
00744        }
00745 
00746       if (bfd_coff_long_section_names (abfd)
00747          && strlen (o->name) > SCNNMLEN)
00748        {
00749          /* This section has a long name which must go in the string
00750              table.  This must correspond to the code in
00751              coff_write_object_contents which puts the string index
00752              into the s_name field of the section header.  That is why
00753              we pass hash as FALSE.  */
00754          if (_bfd_stringtab_add (finfo.strtab, o->name, FALSE, FALSE)
00755              == (bfd_size_type) -1)
00756            goto error_return;
00757          long_section_names = TRUE;
00758        }
00759     }
00760 
00761   /* If doing a relocatable link, allocate space for the pointers we
00762      need to keep.  */
00763   if (info->relocatable)
00764     {
00765       unsigned int i;
00766 
00767       /* We use section_count + 1, rather than section_count, because
00768          the target_index fields are 1 based.  */
00769       amt = abfd->section_count + 1;
00770       amt *= sizeof (struct coff_link_section_info);
00771       finfo.section_info = bfd_malloc (amt);
00772       if (finfo.section_info == NULL)
00773        goto error_return;
00774       for (i = 0; i <= abfd->section_count; i++)
00775        {
00776          finfo.section_info[i].relocs = NULL;
00777          finfo.section_info[i].rel_hashes = NULL;
00778        }
00779     }
00780 
00781   /* We now know the size of the relocs, so we can determine the file
00782      positions of the line numbers.  */
00783   line_filepos = rel_filepos;
00784   linesz = bfd_coff_linesz (abfd);
00785   max_output_reloc_count = 0;
00786   for (o = abfd->sections; o != NULL; o = o->next)
00787     {
00788       if (o->lineno_count == 0)
00789        o->line_filepos = 0;
00790       else
00791        {
00792          o->line_filepos = line_filepos;
00793          line_filepos += o->lineno_count * linesz;
00794        }
00795 
00796       if (o->reloc_count != 0)
00797        {
00798          /* We don't know the indices of global symbols until we have
00799              written out all the local symbols.  For each section in
00800              the output file, we keep an array of pointers to hash
00801              table entries.  Each entry in the array corresponds to a
00802              reloc.  When we find a reloc against a global symbol, we
00803              set the corresponding entry in this array so that we can
00804              fix up the symbol index after we have written out all the
00805              local symbols.
00806 
00807             Because of this problem, we also keep the relocs in
00808             memory until the end of the link.  This wastes memory,
00809             but only when doing a relocatable link, which is not the
00810             common case.  */
00811          BFD_ASSERT (info->relocatable);
00812          amt = o->reloc_count;
00813          amt *= sizeof (struct internal_reloc);
00814          finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
00815          amt = o->reloc_count;
00816          amt *= sizeof (struct coff_link_hash_entry *);
00817          finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
00818          if (finfo.section_info[o->target_index].relocs == NULL
00819              || finfo.section_info[o->target_index].rel_hashes == NULL)
00820            goto error_return;
00821 
00822          if (o->reloc_count > max_output_reloc_count)
00823            max_output_reloc_count = o->reloc_count;
00824        }
00825 
00826       /* Reset the reloc and lineno counts, so that we can use them to
00827         count the number of entries we have output so far.  */
00828       o->reloc_count = 0;
00829       o->lineno_count = 0;
00830     }
00831 
00832   obj_sym_filepos (abfd) = line_filepos;
00833 
00834   /* Figure out the largest number of symbols in an input BFD.  Take
00835      the opportunity to clear the output_has_begun fields of all the
00836      input BFD's.  */
00837   max_sym_count = 0;
00838   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
00839     {
00840       size_t sz;
00841 
00842       sub->output_has_begun = FALSE;
00843       sz = obj_raw_syment_count (sub);
00844       if (sz > max_sym_count)
00845        max_sym_count = sz;
00846     }
00847 
00848   /* Allocate some buffers used while linking.  */
00849   amt = max_sym_count * sizeof (struct internal_syment);
00850   finfo.internal_syms = bfd_malloc (amt);
00851   amt = max_sym_count * sizeof (asection *);
00852   finfo.sec_ptrs = bfd_malloc (amt);
00853   amt = max_sym_count * sizeof (long);
00854   finfo.sym_indices = bfd_malloc (amt);
00855   finfo.outsyms = bfd_malloc ((max_sym_count + 1) * symesz);
00856   amt = max_lineno_count * bfd_coff_linesz (abfd);
00857   finfo.linenos = bfd_malloc (amt);
00858   finfo.contents = bfd_malloc (max_contents_size);
00859   amt = max_reloc_count * relsz;
00860   finfo.external_relocs = bfd_malloc (amt);
00861   if (! info->relocatable)
00862     {
00863       amt = max_reloc_count * sizeof (struct internal_reloc);
00864       finfo.internal_relocs = bfd_malloc (amt);
00865     }
00866   if ((finfo.internal_syms == NULL && max_sym_count > 0)
00867       || (finfo.sec_ptrs == NULL && max_sym_count > 0)
00868       || (finfo.sym_indices == NULL && max_sym_count > 0)
00869       || finfo.outsyms == NULL
00870       || (finfo.linenos == NULL && max_lineno_count > 0)
00871       || (finfo.contents == NULL && max_contents_size > 0)
00872       || (finfo.external_relocs == NULL && max_reloc_count > 0)
00873       || (! info->relocatable
00874          && finfo.internal_relocs == NULL
00875          && max_reloc_count > 0))
00876     goto error_return;
00877 
00878   /* We now know the position of everything in the file, except that
00879      we don't know the size of the symbol table and therefore we don't
00880      know where the string table starts.  We just build the string
00881      table in memory as we go along.  We process all the relocations
00882      for a single input file at once.  */
00883   obj_raw_syment_count (abfd) = 0;
00884 
00885   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
00886     {
00887       if (! bfd_coff_start_final_link (abfd, info))
00888        goto error_return;
00889     }
00890 
00891   for (o = abfd->sections; o != NULL; o = o->next)
00892     {
00893       for (p = o->map_head.link_order; p != NULL; p = p->next)
00894        {
00895          if (p->type == bfd_indirect_link_order
00896              && bfd_family_coff (p->u.indirect.section->owner))
00897            {
00898              sub = p->u.indirect.section->owner;
00899              if (! bfd_coff_link_output_has_begun (sub, & finfo))
00900               {
00901                 if (! _bfd_coff_link_input_bfd (&finfo, sub))
00902                   goto error_return;
00903                 sub->output_has_begun = TRUE;
00904               }
00905            }
00906          else if (p->type == bfd_section_reloc_link_order
00907                  || p->type == bfd_symbol_reloc_link_order)
00908            {
00909              if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
00910               goto error_return;
00911            }
00912          else
00913            {
00914              if (! _bfd_default_link_order (abfd, info, o, p))
00915               goto error_return;
00916            }
00917        }
00918     }
00919 
00920   if (! bfd_coff_final_link_postscript (abfd, & finfo))
00921     goto error_return;
00922 
00923   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
00924 
00925   coff_debug_merge_hash_table_free (&finfo.debug_merge);
00926   debug_merge_allocated = FALSE;
00927 
00928   if (finfo.internal_syms != NULL)
00929     {
00930       free (finfo.internal_syms);
00931       finfo.internal_syms = NULL;
00932     }
00933   if (finfo.sec_ptrs != NULL)
00934     {
00935       free (finfo.sec_ptrs);
00936       finfo.sec_ptrs = NULL;
00937     }
00938   if (finfo.sym_indices != NULL)
00939     {
00940       free (finfo.sym_indices);
00941       finfo.sym_indices = NULL;
00942     }
00943   if (finfo.linenos != NULL)
00944     {
00945       free (finfo.linenos);
00946       finfo.linenos = NULL;
00947     }
00948   if (finfo.contents != NULL)
00949     {
00950       free (finfo.contents);
00951       finfo.contents = NULL;
00952     }
00953   if (finfo.external_relocs != NULL)
00954     {
00955       free (finfo.external_relocs);
00956       finfo.external_relocs = NULL;
00957     }
00958   if (finfo.internal_relocs != NULL)
00959     {
00960       free (finfo.internal_relocs);
00961       finfo.internal_relocs = NULL;
00962     }
00963 
00964   /* The value of the last C_FILE symbol is supposed to be the symbol
00965      index of the first external symbol.  Write it out again if
00966      necessary.  */
00967   if (finfo.last_file_index != -1
00968       && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
00969     {
00970       file_ptr pos;
00971 
00972       finfo.last_file.n_value = obj_raw_syment_count (abfd);
00973       bfd_coff_swap_sym_out (abfd, &finfo.last_file,
00974                           finfo.outsyms);
00975 
00976       pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
00977       if (bfd_seek (abfd, pos, SEEK_SET) != 0
00978          || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
00979        return FALSE;
00980     }
00981 
00982   /* If doing task linking (ld --task-link) then make a pass through the
00983      global symbols, writing out any that are defined, and making them
00984      static.  */
00985   if (info->task_link)
00986     {
00987       finfo.failed = FALSE;
00988       coff_link_hash_traverse (coff_hash_table (info),
00989                             _bfd_coff_write_task_globals, &finfo);
00990       if (finfo.failed)
00991        goto error_return;
00992     }
00993 
00994   /* Write out the global symbols.  */
00995   finfo.failed = FALSE;
00996   coff_link_hash_traverse (coff_hash_table (info),
00997                         _bfd_coff_write_global_sym, &finfo);
00998   if (finfo.failed)
00999     goto error_return;
01000 
01001   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
01002   if (finfo.outsyms != NULL)
01003     {
01004       free (finfo.outsyms);
01005       finfo.outsyms = NULL;
01006     }
01007 
01008   if (info->relocatable && max_output_reloc_count > 0)
01009     {
01010       /* Now that we have written out all the global symbols, we know
01011         the symbol indices to use for relocs against them, and we can
01012         finally write out the relocs.  */
01013       amt = max_output_reloc_count * relsz;
01014       external_relocs = bfd_malloc (amt);
01015       if (external_relocs == NULL)
01016        goto error_return;
01017 
01018       for (o = abfd->sections; o != NULL; o = o->next)
01019        {
01020          struct internal_reloc *irel;
01021          struct internal_reloc *irelend;
01022          struct coff_link_hash_entry **rel_hash;
01023          bfd_byte *erel;
01024 
01025          if (o->reloc_count == 0)
01026            continue;
01027 
01028          irel = finfo.section_info[o->target_index].relocs;
01029          irelend = irel + o->reloc_count;
01030          rel_hash = finfo.section_info[o->target_index].rel_hashes;
01031          erel = external_relocs;
01032          for (; irel < irelend; irel++, rel_hash++, erel += relsz)
01033            {
01034              if (*rel_hash != NULL)
01035               {
01036                 BFD_ASSERT ((*rel_hash)->indx >= 0);
01037                 irel->r_symndx = (*rel_hash)->indx;
01038               }
01039              bfd_coff_swap_reloc_out (abfd, irel, erel);
01040            }
01041 
01042          if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
01043            goto error_return;
01044          if (obj_pe (abfd) && o->reloc_count >= 0xffff)
01045            {
01046              /* In PE COFF, write the count of relocs as the first
01047                reloc.  The header overflow bit will be set
01048                elsewhere. */
01049              struct internal_reloc incount;
01050              bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
01051              
01052              memset (&incount, 0, sizeof (incount));
01053              incount.r_vaddr = o->reloc_count + 1;
01054              bfd_coff_swap_reloc_out (abfd, (PTR) &incount, (PTR) excount);
01055              if (bfd_bwrite (excount, relsz, abfd) != relsz)
01056               /* We'll leak, but it's an error anyway. */
01057               goto error_return;
01058              free (excount);
01059            }
01060          if (bfd_bwrite (external_relocs,
01061                        (bfd_size_type) relsz * o->reloc_count, abfd)
01062              != (bfd_size_type) relsz * o->reloc_count)
01063            goto error_return;
01064        }
01065 
01066       free (external_relocs);
01067       external_relocs = NULL;
01068     }
01069 
01070   /* Free up the section information.  */
01071   if (finfo.section_info != NULL)
01072     {
01073       unsigned int i;
01074 
01075       for (i = 0; i < abfd->section_count; i++)
01076        {
01077          if (finfo.section_info[i].relocs != NULL)
01078            free (finfo.section_info[i].relocs);
01079          if (finfo.section_info[i].rel_hashes != NULL)
01080            free (finfo.section_info[i].rel_hashes);
01081        }
01082       free (finfo.section_info);
01083       finfo.section_info = NULL;
01084     }
01085 
01086   /* If we have optimized stabs strings, output them.  */
01087   if (coff_hash_table (info)->stab_info.stabstr != NULL)
01088     {
01089       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
01090        return FALSE;
01091     }
01092 
01093   /* Write out the string table.  */
01094   if (obj_raw_syment_count (abfd) != 0 || long_section_names)
01095     {
01096       file_ptr pos;
01097 
01098       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
01099       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
01100        return FALSE;
01101 
01102 #if STRING_SIZE_SIZE == 4
01103       H_PUT_32 (abfd,
01104               _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
01105               strbuf);
01106 #else
01107  #error Change H_PUT_32 above
01108 #endif
01109 
01110       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
01111          != STRING_SIZE_SIZE)
01112        return FALSE;
01113 
01114       if (! _bfd_stringtab_emit (abfd, finfo.strtab))
01115        return FALSE;
01116 
01117       obj_coff_strings_written (abfd) = TRUE;
01118     }
01119 
01120   _bfd_stringtab_free (finfo.strtab);
01121 
01122   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
01123      not try to write out the symbols.  */
01124   bfd_get_symcount (abfd) = 0;
01125 
01126   return TRUE;
01127 
01128  error_return:
01129   if (debug_merge_allocated)
01130     coff_debug_merge_hash_table_free (&finfo.debug_merge);
01131   if (finfo.strtab != NULL)
01132     _bfd_stringtab_free (finfo.strtab);
01133   if (finfo.section_info != NULL)
01134     {
01135       unsigned int i;
01136 
01137       for (i = 0; i < abfd->section_count; i++)
01138        {
01139          if (finfo.section_info[i].relocs != NULL)
01140            free (finfo.section_info[i].relocs);
01141          if (finfo.section_info[i].rel_hashes != NULL)
01142            free (finfo.section_info[i].rel_hashes);
01143        }
01144       free (finfo.section_info);
01145     }
01146   if (finfo.internal_syms != NULL)
01147     free (finfo.internal_syms);
01148   if (finfo.sec_ptrs != NULL)
01149     free (finfo.sec_ptrs);
01150   if (finfo.sym_indices != NULL)
01151     free (finfo.sym_indices);
01152   if (finfo.outsyms != NULL)
01153     free (finfo.outsyms);
01154   if (finfo.linenos != NULL)
01155     free (finfo.linenos);
01156   if (finfo.contents != NULL)
01157     free (finfo.contents);
01158   if (finfo.external_relocs != NULL)
01159     free (finfo.external_relocs);
01160   if (finfo.internal_relocs != NULL)
01161     free (finfo.internal_relocs);
01162   if (external_relocs != NULL)
01163     free (external_relocs);
01164   return FALSE;
01165 }
01166 
01167 /* Parse out a -heap <reserved>,<commit> line.  */
01168 
01169 static char *
01170 dores_com (char *ptr, bfd *output_bfd, int heap)
01171 {
01172   if (coff_data(output_bfd)->pe)
01173     {
01174       int val = strtoul (ptr, &ptr, 0);
01175 
01176       if (heap)
01177        pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
01178       else
01179        pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
01180 
01181       if (ptr[0] == ',')
01182        {
01183          val = strtoul (ptr+1, &ptr, 0);
01184          if (heap)
01185            pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
01186          else
01187            pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
01188        }
01189     }
01190   return ptr;
01191 }
01192 
01193 static char *
01194 get_name (char *ptr, char **dst)
01195 {
01196   while (*ptr == ' ')
01197     ptr++;
01198   *dst = ptr;
01199   while (*ptr && *ptr != ' ')
01200     ptr++;
01201   *ptr = 0;
01202   return ptr+1;
01203 }
01204 
01205 /* Process any magic embedded commands in a section called .drectve.  */
01206 
01207 static int
01208 process_embedded_commands (bfd *output_bfd,
01209                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
01210                         bfd *abfd)
01211 {
01212   asection *sec = bfd_get_section_by_name (abfd, ".drectve");
01213   char *s;
01214   char *e;
01215   bfd_byte *copy;
01216 
01217   if (!sec)
01218     return 1;
01219 
01220   if (!bfd_malloc_and_get_section (abfd, sec, &copy))
01221     {
01222       if (copy != NULL)
01223        free (copy);
01224       return 0;
01225     }
01226   e = (char *) copy + sec->size;
01227 
01228   for (s = (char *) copy; s < e ; )
01229     {
01230       if (s[0] != '-')
01231        {
01232          s++;
01233          continue;
01234        }
01235       if (CONST_STRNEQ (s, "-attr"))
01236        {
01237          char *name;
01238          char *attribs;
01239          asection *asec;
01240          int loop = 1;
01241          int had_write = 0;
01242          int had_exec= 0;
01243 
01244          s += 5;
01245          s = get_name (s, &name);
01246          s = get_name (s, &attribs);
01247 
01248          while (loop)
01249            {
01250              switch (*attribs++)
01251               {
01252               case 'W':
01253                 had_write = 1;
01254                 break;
01255               case 'R':
01256                 break;
01257               case 'S':
01258                 break;
01259               case 'X':
01260                 had_exec = 1;
01261                 break;
01262               default:
01263                 loop = 0;
01264               }
01265            }
01266          asec = bfd_get_section_by_name (abfd, name);
01267          if (asec)
01268            {
01269              if (had_exec)
01270               asec->flags |= SEC_CODE;
01271              if (!had_write)
01272               asec->flags |= SEC_READONLY;
01273            }
01274        }
01275       else if (CONST_STRNEQ (s, "-heap"))
01276        s = dores_com (s + 5, output_bfd, 1);
01277 
01278       else if (CONST_STRNEQ (s, "-stack"))
01279        s = dores_com (s + 6, output_bfd, 0);
01280 
01281       else
01282        s++;
01283     }
01284   free (copy);
01285   return 1;
01286 }
01287 
01288 /* Place a marker against all symbols which are used by relocations.
01289    This marker can be picked up by the 'do we skip this symbol ?'
01290    loop in _bfd_coff_link_input_bfd() and used to prevent skipping
01291    that symbol.  */
01292 
01293 static void
01294 mark_relocs (struct coff_final_link_info *finfo, bfd *input_bfd)
01295 {
01296   asection * a;
01297 
01298   if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
01299     return;
01300 
01301   for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
01302     {
01303       struct internal_reloc *      internal_relocs;
01304       struct internal_reloc *      irel;
01305       struct internal_reloc *      irelend;
01306 
01307       if ((a->flags & SEC_RELOC) == 0 || a->reloc_count  < 1)
01308        continue;
01309       /* Don't mark relocs in excluded sections.  */
01310       if (a->output_section == bfd_abs_section_ptr)
01311        continue;
01312 
01313       /* Read in the relocs.  */
01314       internal_relocs = _bfd_coff_read_internal_relocs
01315        (input_bfd, a, FALSE,
01316         finfo->external_relocs,
01317         finfo->info->relocatable,
01318         (finfo->info->relocatable
01319          ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
01320          : finfo->internal_relocs)
01321        );
01322 
01323       if (internal_relocs == NULL)
01324        continue;
01325 
01326       irel     = internal_relocs;
01327       irelend  = irel + a->reloc_count;
01328 
01329       /* Place a mark in the sym_indices array (whose entries have
01330         been initialised to 0) for all of the symbols that are used
01331         in the relocation table.  This will then be picked up in the
01332         skip/don't-skip pass.  */
01333       for (; irel < irelend; irel++)
01334        finfo->sym_indices[ irel->r_symndx ] = -1;
01335     }
01336 }
01337 
01338 /* Link an input file into the linker output file.  This function
01339    handles all the sections and relocations of the input file at once.  */
01340 
01341 bfd_boolean
01342 _bfd_coff_link_input_bfd (struct coff_final_link_info *finfo, bfd *input_bfd)
01343 {
01344   unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
01345   unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
01346   bfd_boolean (*adjust_symndx)
01347     (bfd *, struct bfd_link_info *, bfd *, asection *,
01348      struct internal_reloc *, bfd_boolean *);
01349   bfd *output_bfd;
01350   const char *strings;
01351   bfd_size_type syment_base;
01352   bfd_boolean copy, hash;
01353   bfd_size_type isymesz;
01354   bfd_size_type osymesz;
01355   bfd_size_type linesz;
01356   bfd_byte *esym;
01357   bfd_byte *esym_end;
01358   struct internal_syment *isymp;
01359   asection **secpp;
01360   long *indexp;
01361   unsigned long output_index;
01362   bfd_byte *outsym;
01363   struct coff_link_hash_entry **sym_hash;
01364   asection *o;
01365 
01366   /* Move all the symbols to the output file.  */
01367 
01368   output_bfd = finfo->output_bfd;
01369   strings = NULL;
01370   syment_base = obj_raw_syment_count (output_bfd);
01371   isymesz = bfd_coff_symesz (input_bfd);
01372   osymesz = bfd_coff_symesz (output_bfd);
01373   linesz = bfd_coff_linesz (input_bfd);
01374   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
01375 
01376   copy = FALSE;
01377   if (! finfo->info->keep_memory)
01378     copy = TRUE;
01379   hash = TRUE;
01380   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
01381     hash = FALSE;
01382 
01383   if (! _bfd_coff_get_external_symbols (input_bfd))
01384     return FALSE;
01385 
01386   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
01387   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
01388   isymp = finfo->internal_syms;
01389   secpp = finfo->sec_ptrs;
01390   indexp = finfo->sym_indices;
01391   output_index = syment_base;
01392   outsym = finfo->outsyms;
01393 
01394   if (coff_data (output_bfd)->pe
01395       && ! process_embedded_commands (output_bfd, finfo->info, input_bfd))
01396     return FALSE;
01397 
01398   /* If we are going to perform relocations and also strip/discard some
01399      symbols then we must make sure that we do not strip/discard those
01400      symbols that are going to be involved in the relocations.  */
01401   if ((   finfo->info->strip   != strip_none
01402        || finfo->info->discard != discard_none)
01403       && finfo->info->relocatable)
01404     {
01405       /* Mark the symbol array as 'not-used'.  */
01406       memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
01407 
01408       mark_relocs (finfo, input_bfd);
01409     }
01410 
01411   while (esym < esym_end)
01412     {
01413       struct internal_syment isym;
01414       enum coff_symbol_classification classification;
01415       bfd_boolean skip;
01416       bfd_boolean global;
01417       bfd_boolean dont_skip_symbol;
01418       int add;
01419 
01420       bfd_coff_swap_sym_in (input_bfd, esym, isymp);
01421 
01422       /* Make a copy of *isymp so that the relocate_section function
01423         always sees the original values.  This is more reliable than
01424         always recomputing the symbol value even if we are stripping
01425         the symbol.  */
01426       isym = *isymp;
01427 
01428       classification = bfd_coff_classify_symbol (input_bfd, &isym);
01429       switch (classification)
01430        {
01431        default:
01432          abort ();
01433        case COFF_SYMBOL_GLOBAL:
01434        case COFF_SYMBOL_PE_SECTION:
01435        case COFF_SYMBOL_LOCAL:
01436          *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
01437          break;
01438        case COFF_SYMBOL_COMMON:
01439          *secpp = bfd_com_section_ptr;
01440          break;
01441        case COFF_SYMBOL_UNDEFINED:
01442          *secpp = bfd_und_section_ptr;
01443          break;
01444        }
01445 
01446       /* Extract the flag indicating if this symbol is used by a
01447          relocation.  */
01448       if ((finfo->info->strip != strip_none
01449           || finfo->info->discard != discard_none)
01450          && finfo->info->relocatable)
01451        dont_skip_symbol = *indexp;
01452       else
01453        dont_skip_symbol = FALSE;
01454 
01455       *indexp = -1;
01456 
01457       skip = FALSE;
01458       global = FALSE;
01459       add = 1 + isym.n_numaux;
01460 
01461       /* If we are stripping all symbols, we want to skip this one.  */
01462       if (finfo->info->strip == strip_all && ! dont_skip_symbol)
01463        skip = TRUE;
01464 
01465       if (! skip)
01466        {
01467          switch (classification)
01468            {
01469            default:
01470              abort ();
01471            case COFF_SYMBOL_GLOBAL:
01472            case COFF_SYMBOL_COMMON:
01473            case COFF_SYMBOL_PE_SECTION:
01474              /* This is a global symbol.  Global symbols come at the
01475                end of the symbol table, so skip them for now.
01476                Locally defined function symbols, however, are an
01477                exception, and are not moved to the end.  */
01478              global = TRUE;
01479              if (! ISFCN (isym.n_type))
01480               skip = TRUE;
01481              break;
01482 
01483            case COFF_SYMBOL_UNDEFINED:
01484              /* Undefined symbols are left for the end.  */
01485              global = TRUE;
01486              skip = TRUE;
01487              break;
01488 
01489            case COFF_SYMBOL_LOCAL:
01490              /* This is a local symbol.  Skip it if we are discarding
01491                  local symbols.  */
01492              if (finfo->info->discard == discard_all && ! dont_skip_symbol)
01493               skip = TRUE;
01494              break;
01495            }
01496        }
01497 
01498 #ifndef COFF_WITH_PE
01499       /* Skip section symbols for sections which are not going to be
01500         emitted.  */
01501       if (!skip
01502          && dont_skip_symbol == 0
01503          && isym.n_sclass == C_STAT
01504          && isym.n_type == T_NULL
01505           && isym.n_numaux > 0
01506          && (*secpp)->output_section == bfd_abs_section_ptr)
01507        skip = TRUE;
01508 #endif
01509 
01510       /* If we stripping debugging symbols, and this is a debugging
01511          symbol, then skip it.  FIXME: gas sets the section to N_ABS
01512          for some types of debugging symbols; I don't know if this is
01513          a bug or not.  In any case, we handle it here.  */
01514       if (! skip
01515          && finfo->info->strip == strip_debugger
01516          && ! dont_skip_symbol
01517          && (isym.n_scnum == N_DEBUG
01518              || (isym.n_scnum == N_ABS
01519                 && (isym.n_sclass == C_AUTO
01520                     || isym.n_sclass == C_REG
01521                     || isym.n_sclass == C_MOS
01522                     || isym.n_sclass == C_MOE
01523                     || isym.n_sclass == C_MOU
01524                     || isym.n_sclass == C_ARG
01525                     || isym.n_sclass == C_REGPARM
01526                     || isym.n_sclass == C_FIELD
01527                     || isym.n_sclass == C_EOS))))
01528        skip = TRUE;
01529 
01530       /* If some symbols are stripped based on the name, work out the
01531         name and decide whether to skip this symbol.  */
01532       if (! skip
01533          && (finfo->info->strip == strip_some
01534              || finfo->info->discard == discard_l))
01535        {
01536          const char *name;
01537          char buf[SYMNMLEN + 1];
01538 
01539          name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
01540          if (name == NULL)
01541            return FALSE;
01542 
01543          if (! dont_skip_symbol
01544              && ((finfo->info->strip == strip_some
01545                  && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
01546                                 FALSE) == NULL))
01547                  || (! global
01548                      && finfo->info->discard == discard_l
01549                      && bfd_is_local_label_name (input_bfd, name))))
01550            skip = TRUE;
01551        }
01552 
01553       /* If this is an enum, struct, or union tag, see if we have
01554          already output an identical type.  */
01555       if (! skip
01556          && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
01557          && (isym.n_sclass == C_ENTAG
01558              || isym.n_sclass == C_STRTAG
01559              || isym.n_sclass == C_UNTAG)
01560          && isym.n_numaux == 1)
01561        {
01562          const char *name;
01563          char buf[SYMNMLEN + 1];
01564          struct coff_debug_merge_hash_entry *mh;
01565          struct coff_debug_merge_type *mt;
01566          union internal_auxent aux;
01567          struct coff_debug_merge_element **epp;
01568          bfd_byte *esl, *eslend;
01569          struct internal_syment *islp;
01570          bfd_size_type amt;
01571 
01572          name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
01573          if (name == NULL)
01574            return FALSE;
01575 
01576          /* Ignore fake names invented by compiler; treat them all as
01577              the same name.  */
01578          if (*name == '~' || *name == '.' || *name == '$'
01579              || (*name == bfd_get_symbol_leading_char (input_bfd)
01580                 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
01581            name = "";
01582 
01583          mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
01584                                         TRUE, TRUE);
01585          if (mh == NULL)
01586            return FALSE;
01587 
01588          /* Allocate memory to hold type information.  If this turns
01589              out to be a duplicate, we pass this address to
01590              bfd_release.  */
01591          amt = sizeof (struct coff_debug_merge_type);
01592          mt = bfd_alloc (input_bfd, amt);
01593          if (mt == NULL)
01594            return FALSE;
01595          mt->class = isym.n_sclass;
01596 
01597          /* Pick up the aux entry, which points to the end of the tag
01598              entries.  */
01599          bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
01600                             isym.n_type, isym.n_sclass, 0, isym.n_numaux,
01601                             &aux);
01602 
01603          /* Gather the elements.  */
01604          epp = &mt->elements;
01605          mt->elements = NULL;
01606          islp = isymp + 2;
01607          esl = esym + 2 * isymesz;
01608          eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
01609                   + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
01610          while (esl < eslend)
01611            {
01612              const char *elename;
01613              char elebuf[SYMNMLEN + 1];
01614              char *name_copy;
01615 
01616              bfd_coff_swap_sym_in (input_bfd, esl, islp);
01617 
01618              amt = sizeof (struct coff_debug_merge_element);
01619              *epp = bfd_alloc (input_bfd, amt);
01620              if (*epp == NULL)
01621               return FALSE;
01622 
01623              elename = _bfd_coff_internal_syment_name (input_bfd, islp,
01624                                                  elebuf);
01625              if (elename == NULL)
01626               return FALSE;
01627 
01628              amt = strlen (elename) + 1;
01629              name_copy = bfd_alloc (input_bfd, amt);
01630              if (name_copy == NULL)
01631               return FALSE;
01632              strcpy (name_copy, elename);
01633 
01634              (*epp)->name = name_copy;
01635              (*epp)->type = islp->n_type;
01636              (*epp)->tagndx = 0;
01637              if (islp->n_numaux >= 1
01638                 && islp->n_type != T_NULL
01639                 && islp->n_sclass != C_EOS)
01640               {
01641                 union internal_auxent eleaux;
01642                 long indx;
01643 
01644                 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
01645                                    islp->n_type, islp->n_sclass, 0,
01646                                    islp->n_numaux, &eleaux);
01647                 indx = eleaux.x_sym.x_tagndx.l;
01648 
01649                 /* FIXME: If this tagndx entry refers to a symbol
01650                    defined later in this file, we just ignore it.
01651                    Handling this correctly would be tedious, and may
01652                    not be required.  */
01653                 if (indx > 0
01654                     && (indx
01655                        < ((esym -
01656                            (bfd_byte *) obj_coff_external_syms (input_bfd))
01657                           / (long) isymesz)))
01658                   {
01659                     (*epp)->tagndx = finfo->sym_indices[indx];
01660                     if ((*epp)->tagndx < 0)
01661                      (*epp)->tagndx = 0;
01662                   }
01663               }
01664              epp = &(*epp)->next;
01665              *epp = NULL;
01666 
01667              esl += (islp->n_numaux + 1) * isymesz;
01668              islp += islp->n_numaux + 1;
01669            }
01670 
01671          /* See if we already have a definition which matches this
01672              type.  We always output the type if it has no elements,
01673              for simplicity.  */
01674          if (mt->elements == NULL)
01675            bfd_release (input_bfd, mt);
01676          else
01677            {
01678              struct coff_debug_merge_type *mtl;
01679 
01680              for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
01681               {
01682                 struct coff_debug_merge_element *me, *mel;
01683 
01684                 if (mtl->class != mt->class)
01685                   continue;
01686 
01687                 for (me = mt->elements, mel = mtl->elements;
01688                      me != NULL && mel != NULL;
01689                      me = me->next, mel = mel->next)
01690                   {
01691                     if (strcmp (me->name, mel->name) != 0
01692                        || me->type != mel->type
01693                        || me->tagndx != mel->tagndx)
01694                      break;
01695                   }
01696 
01697                 if (me == NULL && mel == NULL)
01698                   break;
01699               }
01700 
01701              if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
01702               {
01703                 /* This is the first definition of this type.  */
01704                 mt->indx = output_index;
01705                 mt->next = mh->types;
01706                 mh->types = mt;
01707               }
01708              else
01709               {
01710                 /* This is a redefinition which can be merged.  */
01711                 bfd_release (input_bfd, mt);
01712                 *indexp = mtl->indx;
01713                 add = (eslend - esym) / isymesz;
01714                 skip = TRUE;
01715               }
01716            }
01717        }
01718 
01719       /* We now know whether we are to skip this symbol or not.  */
01720       if (! skip)
01721        {
01722          /* Adjust the symbol in order to output it.  */
01723 
01724          if (isym._n._n_n._n_zeroes == 0
01725              && isym._n._n_n._n_offset != 0)
01726            {
01727              const char *name;
01728              bfd_size_type indx;
01729 
01730              /* This symbol has a long name.  Enter it in the string
01731                table we are building.  Note that we do not check
01732                bfd_coff_symname_in_debug.  That is only true for
01733                XCOFF, and XCOFF requires different linking code
01734                anyhow.  */
01735              name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
01736              if (name == NULL)
01737               return FALSE;
01738              indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
01739              if (indx == (bfd_size_type) -1)
01740               return FALSE;
01741              isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
01742            }
01743 
01744          switch (isym.n_sclass)
01745            {
01746            case C_AUTO:
01747            case C_MOS:
01748            case C_EOS:
01749            case C_MOE:
01750            case C_MOU:
01751            case C_UNTAG:
01752            case C_STRTAG:
01753            case C_ENTAG:
01754            case C_TPDEF:
01755            case C_ARG:
01756            case C_USTATIC:
01757            case C_REG:
01758            case C_REGPARM:
01759            case C_FIELD:
01760              /* The symbol value should not be modified.  */
01761              break;
01762 
01763            case C_FCN:
01764              if (obj_pe (input_bfd)
01765                 && strcmp (isym.n_name, ".bf") != 0
01766                 && isym.n_scnum > 0)
01767               {
01768                 /* For PE, .lf and .ef get their value left alone,
01769                    while .bf gets relocated.  However, they all have
01770                    "real" section numbers, and need to be moved into
01771                    the new section.  */
01772                 isym.n_scnum = (*secpp)->output_section->target_index;
01773                 break;
01774               }
01775              /* Fall through.  */
01776            default:
01777            case C_LABEL:  /* Not completely sure about these 2 */
01778            case C_EXTDEF:
01779            case C_BLOCK:
01780            case C_EFCN:
01781            case C_NULL:
01782            case C_EXT:
01783            case C_STAT:
01784            case C_SECTION:
01785            case C_NT_WEAK:
01786              /* Compute new symbol location.  */
01787            if (isym.n_scnum > 0)
01788              {
01789               isym.n_scnum = (*secpp)->output_section->target_index;
01790               isym.n_value += (*secpp)->output_offset;
01791               if (! obj_pe (input_bfd))
01792                 isym.n_value -= (*secpp)->vma;
01793               if (! obj_pe (finfo->output_bfd))
01794                 isym.n_value += (*secpp)->output_section->vma;
01795              }
01796            break;
01797 
01798            case C_FILE:
01799              /* The value of a C_FILE symbol is the symbol index of
01800                the next C_FILE symbol.  The value of the last C_FILE
01801                symbol is the symbol index to the first external
01802                symbol (actually, coff_renumber_symbols does not get
01803                this right--it just sets the value of the last C_FILE
01804                symbol to zero--and nobody has ever complained about
01805                it).  We try to get this right, below, just before we
01806                write the symbols out, but in the general case we may
01807                have to write the symbol out twice.  */
01808              if (finfo->last_file_index != -1
01809                 && finfo->last_file.n_value != (bfd_vma) output_index)
01810               {
01811                 /* We must correct the value of the last C_FILE
01812                      entry.  */
01813                 finfo->last_file.n_value = output_index;
01814                 if ((bfd_size_type) finfo->last_file_index >= syment_base)
01815                   {
01816                     /* The last C_FILE symbol is in this input file.  */
01817                     bfd_coff_swap_sym_out (output_bfd,
01818                                         &finfo->last_file,
01819                                         (finfo->outsyms
01820                                          + ((finfo->last_file_index
01821                                             - syment_base)
01822                                            * osymesz)));
01823                   }
01824                 else
01825                   {
01826                     file_ptr pos;
01827 
01828                     /* We have already written out the last C_FILE
01829                       symbol.  We need to write it out again.  We
01830                       borrow *outsym temporarily.  */
01831                     bfd_coff_swap_sym_out (output_bfd,
01832                                         &finfo->last_file, outsym);
01833                     pos = obj_sym_filepos (output_bfd);
01834                     pos += finfo->last_file_index * osymesz;
01835                     if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
01836                        || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
01837                      return FALSE;
01838                   }
01839               }
01840 
01841              finfo->last_file_index = output_index;
01842              finfo->last_file = isym;
01843              break;
01844            }
01845 
01846          /* If doing task linking, convert normal global function symbols to
01847             static functions.  */
01848          if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
01849            isym.n_sclass = C_STAT;
01850 
01851          /* Output the symbol.  */
01852          bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
01853 
01854          *indexp = output_index;
01855 
01856          if (global)
01857            {
01858              long indx;
01859              struct coff_link_hash_entry *h;
01860 
01861              indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
01862                     / isymesz);
01863              h = obj_coff_sym_hashes (input_bfd)[indx];
01864              if (h == NULL)
01865               {
01866                 /* This can happen if there were errors earlier in
01867                      the link.  */
01868                 bfd_set_error (bfd_error_bad_value);
01869                 return FALSE;
01870               }
01871              h->indx = output_index;
01872            }
01873 
01874          output_index += add;
01875          outsym += add * osymesz;
01876        }
01877 
01878       esym += add * isymesz;
01879       isymp += add;
01880       ++secpp;
01881       ++indexp;
01882       for (--add; add > 0; --add)
01883        {
01884          *secpp++ = NULL;
01885          *indexp++ = -1;
01886        }
01887     }
01888 
01889   /* Fix up the aux entries.  This must be done in a separate pass,
01890      because we don't know the correct symbol indices until we have
01891      already decided which symbols we are going to keep.  */
01892   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
01893   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
01894   isymp = finfo->internal_syms;
01895   indexp = finfo->sym_indices;
01896   sym_hash = obj_coff_sym_hashes (input_bfd);
01897   outsym = finfo->outsyms;
01898 
01899   while (esym < esym_end)
01900     {
01901       int add;
01902 
01903       add = 1 + isymp->n_numaux;
01904 
01905       if ((*indexp < 0
01906           || (bfd_size_type) *indexp < syment_base)
01907          && (*sym_hash == NULL
01908              || (*sym_hash)->auxbfd != input_bfd))
01909        esym += add * isymesz;
01910       else
01911        {
01912          struct coff_link_hash_entry *h;
01913          int i;
01914 
01915          h = NULL;
01916          if (*indexp < 0)
01917            {
01918              h = *sym_hash;
01919 
01920              /* The m68k-motorola-sysv assembler will sometimes
01921                  generate two symbols with the same name, but only one
01922                  will have aux entries.  */
01923              BFD_ASSERT (isymp->n_numaux == 0
01924                        || h->numaux == 0
01925                        || h->numaux == isymp->n_numaux);
01926            }
01927 
01928          esym += isymesz;
01929 
01930          if (h == NULL)
01931            outsym += osymesz;
01932 
01933          /* Handle the aux entries.  This handling is based on
01934             coff_pointerize_aux.  I don't know if it always correct.  */
01935          for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
01936            {
01937              union internal_auxent aux;
01938              union internal_auxent *auxp;
01939 
01940              if (h != NULL && h->aux != NULL && (h->numaux > i))
01941               auxp = h->aux + i;
01942              else
01943               {
01944                 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
01945                                    isymp->n_sclass, i, isymp->n_numaux, &aux);
01946                 auxp = &aux;
01947               }
01948 
01949              if (isymp->n_sclass == C_FILE)
01950               {
01951                 /* If this is a long filename, we must put it in the
01952                    string table.  */
01953                 if (auxp->x_file.x_n.x_zeroes == 0
01954                     && auxp->x_file.x_n.x_offset != 0)
01955                   {
01956                     const char *filename;
01957                     bfd_size_type indx;
01958 
01959                     BFD_ASSERT (auxp->x_file.x_n.x_offset
01960                               >= STRING_SIZE_SIZE);
01961                     if (strings == NULL)
01962                      {
01963                        strings = _bfd_coff_read_string_table (input_bfd);
01964                        if (strings == NULL)
01965                          return FALSE;
01966                      }
01967                     filename = strings + auxp->x_file.x_n.x_offset;
01968                     indx = _bfd_stringtab_add (finfo->strtab, filename,
01969                                            hash, copy);
01970                     if (indx == (bfd_size_type) -1)
01971                      return FALSE;
01972                     auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
01973                   }
01974               }
01975              else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
01976                      && isymp->n_sclass != C_NT_WEAK)
01977               {
01978                 unsigned long indx;
01979 
01980                 if (ISFCN (isymp->n_type)
01981                     || ISTAG (isymp->n_sclass)
01982                     || isymp->n_sclass == C_BLOCK
01983                     || isymp->n_sclass == C_FCN)
01984                   {
01985                     indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
01986                     if (indx > 0
01987                        && indx < obj_raw_syment_count (input_bfd))
01988                      {
01989                        /* We look forward through the symbol for
01990                              the index of the next symbol we are going
01991                              to include.  I don't know if this is
01992                              entirely right.  */
01993                        while ((finfo->sym_indices[indx] < 0
01994                               || ((bfd_size_type) finfo->sym_indices[indx]
01995                                   < syment_base))
01996                              && indx < obj_raw_syment_count (input_bfd))
01997                          ++indx;
01998                        if (indx >= obj_raw_syment_count (input_bfd))
01999                          indx = output_index;
02000                        else
02001                          indx = finfo->sym_indices[indx];
02002                        auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
02003                      }
02004                   }
02005 
02006                 indx = auxp->x_sym.x_tagndx.l;
02007                 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
02008                   {
02009                     long symindx;
02010 
02011                     symindx = finfo->sym_indices[indx];
02012                     if (symindx < 0)
02013                      auxp->x_sym.x_tagndx.l = 0;
02014                     else
02015                      auxp->x_sym.x_tagndx.l = symindx;
02016                   }
02017 
02018                 /* The .bf symbols are supposed to be linked through
02019                    the endndx field.  We need to carry this list
02020                    across object files.  */
02021                 if (i == 0
02022                     && h == NULL
02023                     && isymp->n_sclass == C_FCN
02024                     && (isymp->_n._n_n._n_zeroes != 0
02025                        || isymp->_n._n_n._n_offset == 0)
02026                     && isymp->_n._n_name[0] == '.'
02027                     && isymp->_n._n_name[1] == 'b'
02028                     && isymp->_n._n_name[2] == 'f'
02029                     && isymp->_n._n_name[3] == '\0')
02030                   {
02031                     if (finfo->last_bf_index != -1)
02032                      {
02033                        finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
02034                          *indexp;
02035 
02036                        if ((bfd_size_type) finfo->last_bf_index
02037                            >= syment_base)
02038                          {
02039                            void *auxout;
02040 
02041                            /* The last .bf symbol is in this input
02042                              file.  This will only happen if the
02043                              assembler did not set up the .bf
02044                              endndx symbols correctly.  */
02045                            auxout = (finfo->outsyms
02046                                    + ((finfo->last_bf_index
02047                                        - syment_base)
02048                                       * osymesz));
02049 
02050                            bfd_coff_swap_aux_out (output_bfd,
02051                                                &finfo->last_bf,
02052                                                isymp->n_type,
02053                                                isymp->n_sclass,
02054                                                0, isymp->n_numaux,
02055                                                auxout);
02056                          }
02057                        else
02058                          {
02059                            file_ptr pos;
02060 
02061                            /* We have already written out the last
02062                                  .bf aux entry.  We need to write it
02063                                  out again.  We borrow *outsym
02064                                  temporarily.  FIXME: This case should
02065                                  be made faster.  */
02066                            bfd_coff_swap_aux_out (output_bfd,
02067                                                &finfo->last_bf,
02068                                                isymp->n_type,
02069                                                isymp->n_sclass,
02070                                                0, isymp->n_numaux,
02071                                                outsym);
02072                            pos = obj_sym_filepos (output_bfd);
02073                            pos += finfo->last_bf_index * osymesz;
02074                            if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
02075                               || (bfd_bwrite (outsym, osymesz, output_bfd)
02076                                   != osymesz))
02077                             return FALSE;
02078                          }
02079                      }
02080 
02081                     if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
02082                      finfo->last_bf_index = -1;
02083                     else
02084                      {
02085                        /* The endndx field of this aux entry must
02086                              be updated with the symbol number of the
02087                              next .bf symbol.  */
02088                        finfo->last_bf = *auxp;
02089                        finfo->last_bf_index = (((outsym - finfo->outsyms)
02090                                              / osymesz)
02091                                             + syment_base);
02092                      }
02093                   }
02094               }
02095 
02096              if (h == NULL)
02097               {
02098                 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
02099                                     isymp->n_sclass, i, isymp->n_numaux,
02100                                     outsym);
02101                 outsym += osymesz;
02102               }
02103 
02104              esym += isymesz;
02105            }
02106        }
02107 
02108       indexp += add;
02109       isymp += add;
02110       sym_hash += add;
02111     }
02112 
02113   /* Relocate the line numbers, unless we are stripping them.  */
02114   if (finfo->info->strip == strip_none
02115       || finfo->info->strip == strip_some)
02116     {
02117       for (o = input_bfd->sections; o != NULL; o = o->next)
02118        {
02119          bfd_vma offset;
02120          bfd_byte *eline;
02121          bfd_byte *elineend;
02122          bfd_byte *oeline;
02123          bfd_boolean skipping;
02124          file_ptr pos;
02125          bfd_size_type amt;
02126 
02127          /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
02128             build_link_order in ldwrite.c will not have created a
02129             link order, which means that we will not have seen this
02130             input section in _bfd_coff_final_link, which means that
02131             we will not have allocated space for the line numbers of
02132             this section.  I don't think line numbers can be
02133             meaningful for a section which does not have
02134             SEC_HAS_CONTENTS set, but, if they do, this must be
02135             changed.  */
02136          if (o->lineno_count == 0
02137              || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
02138            continue;
02139 
02140          if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
02141              || bfd_bread (finfo->linenos, linesz * o->lineno_count,
02142                         input_bfd) != linesz * o->lineno_count)
02143            return FALSE;
02144 
02145          offset = o->output_section->vma + o->output_offset - o->vma;
02146          eline = finfo->linenos;
02147          oeline = finfo->linenos;
02148          elineend = eline + linesz * o->lineno_count;
02149          skipping = FALSE;
02150          for (; eline < elineend; eline += linesz)
02151            {
02152              struct internal_lineno iline;
02153 
02154              bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
02155 
02156              if (iline.l_lnno != 0)
02157               iline.l_addr.l_paddr += offset;
02158              else if (iline.l_addr.l_symndx >= 0
02159                      && ((unsigned long) iline.l_addr.l_symndx
02160                         < obj_raw_syment_count (input_bfd)))
02161               {
02162                 long indx;
02163 
02164                 indx = finfo->sym_indices[iline.l_addr.l_symndx];
02165 
02166                 if (indx < 0)
02167                   {
02168                     /* These line numbers are attached to a symbol
02169                       which we are stripping.  We must discard the
02170                       line numbers because reading them back with
02171                       no associated symbol (or associating them all
02172                       with symbol #0) will fail.  We can't regain
02173                       the space in the output file, but at least
02174                       they're dense.  */
02175                     skipping = TRUE;
02176                   }
02177                 else
02178                   {
02179                     struct internal_syment is;
02180                     union internal_auxent ia;
02181 
02182                     /* Fix up the lnnoptr field in the aux entry of
02183                       the symbol.  It turns out that we can't do
02184                       this when we modify the symbol aux entries,
02185                       because gas sometimes screws up the lnnoptr
02186                       field and makes it an offset from the start
02187                       of the line numbers rather than an absolute
02188                       file index.  */
02189                     bfd_coff_swap_sym_in (output_bfd,
02190                                        (finfo->outsyms
02191                                         + ((indx - syment_base)
02192                                           * osymesz)), &is);
02193                     if ((ISFCN (is.n_type)
02194                         || is.n_sclass == C_BLOCK)
02195                        && is.n_numaux >= 1)
02196                      {
02197                        void *auxptr;
02198 
02199                        auxptr = (finfo->outsyms
02200                                 + ((indx - syment_base + 1)
02201                                    * osymesz));
02202                        bfd_coff_swap_aux_in (output_bfd, auxptr,
02203                                           is.n_type, is.n_sclass,
02204                                           0, is.n_numaux, &ia);
02205                        ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
02206                          (o->output_section->line_filepos
02207                           + o->output_section->lineno_count * linesz
02208                           + eline - finfo->linenos);
02209                        bfd_coff_swap_aux_out (output_bfd, &ia,
02210                                            is.n_type, is.n_sclass, 0,
02211                                            is.n_numaux, auxptr);
02212                      }
02213 
02214                     skipping = FALSE;
02215                   }
02216 
02217                 iline.l_addr.l_symndx = indx;
02218               }
02219 
02220              if (!skipping)
02221                {
02222                 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
02223                 oeline += linesz;
02224               }
02225            }
02226 
02227          pos = o->output_section->line_filepos;
02228          pos += o->output_section->lineno_count * linesz;
02229          amt = oeline - finfo->linenos;
02230          if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
02231              || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
02232            return FALSE;
02233 
02234          o->output_section->lineno_count += amt / linesz;
02235        }
02236     }
02237 
02238   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
02239      symbol will be the first symbol in the next input file.  In the
02240      normal case, this will save us from writing out the C_FILE symbol
02241      again.  */
02242   if (finfo->last_file_index != -1
02243       && (bfd_size_type) finfo->last_file_index >= syment_base)
02244     {
02245       finfo->last_file.n_value = output_index;
02246       bfd_coff_swap_sym_out (output_bfd, &finfo->last_file,
02247                           (finfo->outsyms
02248                            + ((finfo->last_file_index - syment_base)
02249                              * osymesz)));
02250     }
02251 
02252   /* Write the modified symbols to the output file.  */
02253   if (outsym > finfo->outsyms)
02254     {
02255       file_ptr pos;
02256       bfd_size_type amt;
02257 
02258       pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
02259       amt = outsym - finfo->outsyms;
02260       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
02261          || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
02262        return FALSE;
02263 
02264       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
02265                  + (outsym - finfo->outsyms) / osymesz)
02266                 == output_index);
02267 
02268       obj_raw_syment_count (output_bfd) = output_index;
02269     }
02270 
02271   /* Relocate the contents of each section.  */
02272   adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
02273   for (o = input_bfd->sections; o != NULL; o = o->next)
02274     {
02275       bfd_byte *contents;
02276       struct coff_section_tdata *secdata;
02277 
02278       if (! o->linker_mark)
02279        /* This section was omitted from the link.  */
02280        continue;
02281 
02282       if ((o->flags & SEC_LINKER_CREATED) != 0)
02283        continue;
02284 
02285       if ((o->flags & SEC_HAS_CONTENTS) == 0
02286          || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
02287        {
02288          if ((o->flags & SEC_RELOC) != 0
02289              && o->reloc_count != 0)
02290            {
02291              (*_bfd_error_handler)
02292               (_("%B: relocs in section `%A', but it has no contents"),
02293                input_bfd, o);
02294              bfd_set_error (bfd_error_no_contents);
02295              return FALSE;
02296            }
02297 
02298          continue;
02299        }
02300 
02301       secdata = coff_section_data (input_bfd, o);
02302       if (secdata != NULL && secdata->contents != NULL)
02303        contents = secdata->contents;
02304       else
02305        {
02306          bfd_size_type x = o->rawsize ? o->rawsize : o->size;
02307          if (! bfd_get_section_contents (input_bfd, o, finfo->contents, 0, x))
02308            return FALSE;
02309          contents = finfo->contents;
02310        }
02311 
02312       if ((o->flags & SEC_RELOC) != 0)
02313        {
02314          int target_index;
02315          struct internal_reloc *internal_relocs;
02316          struct internal_reloc *irel;
02317 
02318          /* Read in the relocs.  */
02319          target_index = o->output_section->target_index;
02320          internal_relocs = (_bfd_coff_read_internal_relocs
02321                           (input_bfd, o, FALSE, finfo->external_relocs,
02322                            finfo->info->relocatable,
02323                            (finfo->info->relocatable
02324                             ? (finfo->section_info[target_index].relocs
02325                               + o->output_section->reloc_count)
02326                             : finfo->internal_relocs)));
02327          if (internal_relocs == NULL)
02328            return FALSE;
02329 
02330          /* Call processor specific code to relocate the section
02331              contents.  */
02332          if (! bfd_coff_relocate_section (output_bfd, finfo->info,
02333                                       input_bfd, o,
02334                                       contents,
02335                                       internal_relocs,
02336                                       finfo->internal_syms,
02337                                       finfo->sec_ptrs))
02338            return FALSE;
02339 
02340          if (finfo->info->relocatable)
02341            {
02342              bfd_vma offset;
02343              struct internal_reloc *irelend;
02344              struct coff_link_hash_entry **rel_hash;
02345 
02346              offset = o->output_section->vma + o->output_offset - o->vma;
02347              irel = internal_relocs;
02348              irelend = irel + o->reloc_count;
02349              rel_hash = (finfo->section_info[target_index].rel_hashes
02350                        + o->output_section->reloc_count);
02351              for (; irel < irelend; irel++, rel_hash++)
02352               {
02353                 struct coff_link_hash_entry *h;
02354                 bfd_boolean adjusted;
02355 
02356                 *rel_hash = NULL;
02357 
02358                 /* Adjust the reloc address and symbol index.  */
02359                 irel->r_vaddr += offset;
02360 
02361                 if (irel->r_symndx == -1)
02362                   continue;
02363 
02364                 if (adjust_symndx)
02365                   {
02366                     if (! (*adjust_symndx) (output_bfd, finfo->info,
02367                                          input_bfd, o, irel,
02368                                          &adjusted))
02369                      return FALSE;
02370                     if (adjusted)
02371                      continue;
02372                   }
02373 
02374                 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
02375                 if (h != NULL)
02376                   {
02377                     /* This is a global symbol.  */
02378                     if (h->indx >= 0)
02379                      irel->r_symndx = h->indx;
02380                     else
02381                      {
02382                        /* This symbol is being written at the end
02383                           of the file, and we do not yet know the
02384                           symbol index.  We save the pointer to the
02385                           hash table entry in the rel_hash list.
02386                           We set the indx field to -2 to indicate
02387                           that this symbol must not be stripped.  */
02388                        *rel_hash = h;
02389                        h->indx = -2;
02390                      }
02391                   }
02392                 else
02393                   {
02394                     long indx;
02395 
02396                     indx = finfo->sym_indices[irel->r_symndx];
02397                     if (indx != -1)
02398                      irel->r_symndx = indx;
02399                     else
02400                      {
02401                        struct internal_syment *is;
02402                        const char *name;
02403                        char buf[SYMNMLEN + 1];
02404 
02405                        /* This reloc is against a symbol we are
02406                              stripping.  This should have been handled
02407                           by the 'dont_skip_symbol' code in the while
02408                           loop at the top of this function.  */
02409                        is = finfo->internal_syms + irel->r_symndx;
02410 
02411                        name = (_bfd_coff_internal_syment_name
02412                               (input_bfd, is, buf));
02413                        if (name == NULL)
02414                          return FALSE;
02415 
02416                        if (! ((*finfo->info->callbacks->unattached_reloc)
02417                              (finfo->info, name, input_bfd, o,
02418                               irel->r_vaddr)))
02419                          return FALSE;
02420                      }
02421                   }
02422               }
02423 
02424              o->output_section->reloc_count += o->reloc_count;
02425            }
02426        }
02427 
02428       /* Write out the modified section contents.  */
02429       if (secdata == NULL || secdata->stab_info == NULL)
02430        {
02431          file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
02432          if (! bfd_set_section_contents (output_bfd, o->output_section,
02433                                      contents, loc, o->size))
02434            return FALSE;
02435        }
02436       else
02437        {
02438          if (! (_bfd_write_section_stabs
02439                (output_bfd, &coff_hash_table (finfo->info)->stab_info,
02440                 o, &secdata->stab_info, contents)))
02441            return FALSE;
02442        }
02443     }
02444 
02445   if (! finfo->info->keep_memory
02446       && ! _bfd_coff_free_symbols (input_bfd))
02447     return FALSE;
02448 
02449   return TRUE;
02450 }
02451 
02452 /* Write out a global symbol.  Called via coff_link_hash_traverse.  */
02453 
02454 bfd_boolean
02455 _bfd_coff_write_global_sym (struct coff_link_hash_entry *h, void *data)
02456 {
02457   struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
02458   bfd *output_bfd;
02459   struct internal_syment isym;
02460   bfd_size_type symesz;
02461   unsigned int i;
02462   file_ptr pos;
02463 
02464   output_bfd = finfo->output_bfd;
02465 
02466   if (h->root.type == bfd_link_hash_warning)
02467     {
02468       h = (struct coff_link_hash_entry *) h->root.u.i.link;
02469       if (h->root.type == bfd_link_hash_new)
02470        return TRUE;
02471     }
02472 
02473   if (h->indx >= 0)
02474     return TRUE;
02475 
02476   if (h->indx != -2
02477       && (finfo->info->strip == strip_all
02478          || (finfo->info->strip == strip_some
02479              && (bfd_hash_lookup (finfo->info->keep_hash,
02480                                h->root.root.string, FALSE, FALSE)
02481                 == NULL))))
02482     return TRUE;
02483 
02484   switch (h->root.type)
02485     {
02486     default:
02487     case bfd_link_hash_new:
02488     case bfd_link_hash_warning:
02489       abort ();
02490       return FALSE;
02491 
02492     case bfd_link_hash_undefined:
02493     case bfd_link_hash_undefweak:
02494       isym.n_scnum = N_UNDEF;
02495       isym.n_value = 0;
02496       break;
02497 
02498     case bfd_link_hash_defined:
02499     case bfd_link_hash_defweak:
02500       {
02501        asection *sec;
02502 
02503        sec = h->root.u.def.section->output_section;
02504        if (bfd_is_abs_section (sec))
02505          isym.n_scnum = N_ABS;
02506        else
02507          isym.n_scnum = sec->target_index;
02508        isym.n_value = (h->root.u.def.value
02509                      + h->root.u.def.section->output_offset);
02510        if (! obj_pe (finfo->output_bfd))
02511          isym.n_value += sec->vma;
02512       }
02513       break;
02514 
02515     case bfd_link_hash_common:
02516       isym.n_scnum = N_UNDEF;
02517       isym.n_value = h->root.u.c.size;
02518       break;
02519 
02520     case bfd_link_hash_indirect:
02521       /* Just ignore these.  They can't be handled anyhow.  */
02522       return TRUE;
02523     }
02524 
02525   if (strlen (h->root.root.string) <= SYMNMLEN)
02526     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
02527   else
02528     {
02529       bfd_boolean hash;
02530       bfd_size_type indx;
02531 
02532       hash = TRUE;
02533       if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
02534        hash = FALSE;
02535       indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
02536                              FALSE);
02537       if (indx == (bfd_size_type) -1)
02538        {
02539          finfo->failed = TRUE;
02540          return FALSE;
02541        }
02542       isym._n._n_n._n_zeroes = 0;
02543       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
02544     }
02545 
02546   isym.n_sclass = h->class;
02547   isym.n_type = h->type;
02548 
02549   if (isym.n_sclass == C_NULL)
02550     isym.n_sclass = C_EXT;
02551 
02552   /* If doing task linking and this is the pass where we convert
02553      defined globals to statics, then do that conversion now.  If the
02554      symbol is not being converted, just ignore it and it will be
02555      output during a later pass.  */
02556   if (finfo->global_to_static)
02557     {
02558       if (! IS_EXTERNAL (output_bfd, isym))
02559        return TRUE;
02560 
02561       isym.n_sclass = C_STAT;
02562     }
02563 
02564   /* When a weak symbol is not overridden by a strong one,
02565      turn it into an external symbol when not building a
02566      shared or relocatable object.  */
02567   if (! finfo->info->shared
02568       && ! finfo->info->relocatable
02569       && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
02570     isym.n_sclass = C_EXT;
02571 
02572   isym.n_numaux = h->numaux;
02573 
02574   bfd_coff_swap_sym_out (output_bfd, &isym, finfo->outsyms);
02575 
02576   symesz = bfd_coff_symesz (output_bfd);
02577 
02578   pos = obj_sym_filepos (output_bfd);
02579   pos += obj_raw_syment_count (output_bfd) * symesz;
02580   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
02581       || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
02582     {
02583       finfo->failed = TRUE;
02584       return FALSE;
02585     }
02586 
02587   h->indx = obj_raw_syment_count (output_bfd);
02588 
02589   ++obj_raw_syment_count (output_bfd);
02590 
02591   /* Write out any associated aux entries.  Most of the aux entries
02592      will have been modified in _bfd_coff_link_input_bfd.  We have to
02593      handle section aux entries here, now that we have the final
02594      relocation and line number counts.  */
02595   for (i = 0; i < isym.n_numaux; i++)
02596     {
02597       union internal_auxent *auxp;
02598 
02599       auxp = h->aux + i;
02600 
02601       /* Look for a section aux entry here using the same tests that
02602          coff_swap_aux_out uses.  */
02603       if (i == 0
02604          && (isym.n_sclass == C_STAT
02605              || isym.n_sclass == C_HIDDEN)
02606          && isym.n_type == T_NULL
02607          && (h->root.type == bfd_link_hash_defined
02608              || h->root.type == bfd_link_hash_defweak))
02609        {
02610          asection *sec;
02611 
02612          sec = h->root.u.def.section->output_section;
02613          if (sec != NULL)
02614            {
02615              auxp->x_scn.x_scnlen = sec->size;
02616 
02617              /* For PE, an overflow on the final link reportedly does
02618                  not matter.  FIXME: Why not?  */
02619              if (sec->reloc_count > 0xffff
02620                 && (! obj_pe (output_bfd)
02621                     || finfo->info->relocatable))
02622               (*_bfd_error_handler)
02623                 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
02624                  bfd_get_filename (output_bfd),
02625                  bfd_get_section_name (output_bfd, sec),
02626                  sec->reloc_count);
02627 
02628              if (sec->lineno_count > 0xffff
02629                 && (! obj_pe (output_bfd)
02630                     || finfo->info->relocatable))
02631               (*_bfd_error_handler)
02632                 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
02633                  bfd_get_filename (output_bfd),
02634                  bfd_get_section_name (output_bfd, sec),
02635                  sec->lineno_count);
02636 
02637              auxp->x_scn.x_nreloc = sec->reloc_count;
02638              auxp->x_scn.x_nlinno = sec->lineno_count;
02639              auxp->x_scn.x_checksum = 0;
02640              auxp->x_scn.x_associated = 0;
02641              auxp->x_scn.x_comdat = 0;
02642            }
02643        }
02644 
02645       bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
02646                           isym.n_sclass, (int) i, isym.n_numaux,
02647                           finfo->outsyms);
02648       if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
02649        {
02650          finfo->failed = TRUE;
02651          return FALSE;
02652        }
02653       ++obj_raw_syment_count (output_bfd);
02654     }
02655 
02656   return TRUE;
02657 }
02658 
02659 /* Write out task global symbols, converting them to statics.  Called
02660    via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
02661    the dirty work, if the symbol we are processing needs conversion.  */
02662 
02663 bfd_boolean
02664 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
02665 {
02666   struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
02667   bfd_boolean rtnval = TRUE;
02668   bfd_boolean save_global_to_static;
02669 
02670   if (h->root.type == bfd_link_hash_warning)
02671     h = (struct coff_link_hash_entry *) h->root.u.i.link;
02672 
02673   if (h->indx < 0)
02674     {
02675       switch (h->root.type)
02676        {
02677        case bfd_link_hash_defined:
02678        case bfd_link_hash_defweak:
02679          save_global_to_static = finfo->global_to_static;
02680          finfo->global_to_static = TRUE;
02681          rtnval = _bfd_coff_write_global_sym (h, data);
02682          finfo->global_to_static = save_global_to_static;
02683          break;
02684        default:
02685          break;
02686        }
02687     }
02688   return (rtnval);
02689 }
02690 
02691 /* Handle a link order which is supposed to generate a reloc.  */
02692 
02693 bfd_boolean
02694 _bfd_coff_reloc_link_order (bfd *output_bfd,
02695                          struct coff_final_link_info *finfo,
02696                          asection *output_section,
02697                          struct bfd_link_order *link_order)
02698 {
02699   reloc_howto_type *howto;
02700   struct internal_reloc *irel;
02701   struct coff_link_hash_entry **rel_hash_ptr;
02702 
02703   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
02704   if (howto == NULL)
02705     {
02706       bfd_set_error (bfd_error_bad_value);
02707       return FALSE;
02708     }
02709 
02710   if (link_order->u.reloc.p->addend != 0)
02711     {
02712       bfd_size_type size;
02713       bfd_byte *buf;
02714       bfd_reloc_status_type rstat;
02715       bfd_boolean ok;
02716       file_ptr loc;
02717 
02718       size = bfd_get_reloc_size (howto);
02719       buf = bfd_zmalloc (size);
02720       if (buf == NULL)
02721        return FALSE;
02722 
02723       rstat = _bfd_relocate_contents (howto, output_bfd,
02724                                   (bfd_vma) link_order->u.reloc.p->addend,\
02725                                   buf);
02726       switch (rstat)
02727        {
02728        case bfd_reloc_ok:
02729          break;
02730        default:
02731        case bfd_reloc_outofrange:
02732          abort ();
02733        case bfd_reloc_overflow:
02734          if (! ((*finfo->info->callbacks->reloc_overflow)
02735                (finfo->info, NULL,
02736                 (link_order->type == bfd_section_reloc_link_order
02737                  ? bfd_section_name (output_bfd,
02738                                    link_order->u.reloc.p->u.section)
02739                  : link_order->u.reloc.p->u.name),
02740                 howto->name, link_order->u.reloc.p->addend,
02741                 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
02742            {
02743              free (buf);
02744              return FALSE;
02745            }
02746          break;
02747        }
02748       loc = link_order->offset * bfd_octets_per_byte (output_bfd);
02749       ok = bfd_set_section_contents (output_bfd, output_section, buf,
02750                                      loc, size);
02751       free (buf);
02752       if (! ok)
02753        return FALSE;
02754     }
02755 
02756   /* Store the reloc information in the right place.  It will get
02757      swapped and written out at the end of the final_link routine.  */
02758   irel = (finfo->section_info[output_section->target_index].relocs
02759          + output_section->reloc_count);
02760   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
02761                 + output_section->reloc_count);
02762 
02763   memset (irel, 0, sizeof (struct internal_reloc));
02764   *rel_hash_ptr = NULL;
02765 
02766   irel->r_vaddr = output_section->vma + link_order->offset;
02767 
02768   if (link_order->type == bfd_section_reloc_link_order)
02769     {
02770       /* We need to somehow locate a symbol in the right section.  The
02771          symbol must either have a value of zero, or we must adjust
02772          the addend by the value of the symbol.  FIXME: Write this
02773          when we need it.  The old linker couldn't handle this anyhow.  */
02774       abort ();
02775       *rel_hash_ptr = NULL;
02776       irel->r_symndx = 0;
02777     }
02778   else
02779     {
02780       struct coff_link_hash_entry *h;
02781 
02782       h = ((struct coff_link_hash_entry *)
02783           bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
02784                                     link_order->u.reloc.p->u.name,
02785                                     FALSE, FALSE, TRUE));
02786       if (h != NULL)
02787        {
02788          if (h->indx >= 0)
02789            irel->r_symndx = h->indx;
02790          else
02791            {
02792              /* Set the index to -2 to force this symbol to get
02793                written out.  */
02794              h->indx = -2;
02795              *rel_hash_ptr = h;
02796              irel->r_symndx = 0;
02797            }
02798        }
02799       else
02800        {
02801          if (! ((*finfo->info->callbacks->unattached_reloc)
02802                (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
02803                 (asection *) NULL, (bfd_vma) 0)))
02804            return FALSE;
02805          irel->r_symndx = 0;
02806        }
02807     }
02808 
02809   /* FIXME: Is this always right?  */
02810   irel->r_type = howto->type;
02811 
02812   /* r_size is only used on the RS/6000, which needs its own linker
02813      routines anyhow.  r_extern is only used for ECOFF.  */
02814 
02815   /* FIXME: What is the right value for r_offset?  Is zero OK?  */
02816   ++output_section->reloc_count;
02817 
02818   return TRUE;
02819 }
02820 
02821 /* A basic reloc handling routine which may be used by processors with
02822    simple relocs.  */
02823 
02824 bfd_boolean
02825 _bfd_coff_generic_relocate_section (bfd *output_bfd,
02826                                 struct bfd_link_info *info,
02827                                 bfd *input_bfd,
02828                                 asection *input_section,
02829                                 bfd_byte *contents,
02830                                 struct internal_reloc *relocs,
02831                                 struct internal_syment *syms,
02832                                 asection **sections)
02833 {
02834   struct internal_reloc *rel;
02835   struct internal_reloc *relend;
02836 
02837   rel = relocs;
02838   relend = rel + input_section->reloc_count;
02839   for (; rel < relend; rel++)
02840     {
02841       long symndx;
02842       struct coff_link_hash_entry *h;
02843       struct internal_syment *sym;
02844       bfd_vma addend;
02845       bfd_vma val;
02846       reloc_howto_type *howto;
02847       bfd_reloc_status_type rstat;
02848 
02849       symndx = rel->r_symndx;
02850 
02851       if (symndx == -1)
02852        {
02853          h = NULL;
02854          sym = NULL;
02855        }
02856       else if (symndx < 0
02857               || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
02858        {
02859          (*_bfd_error_handler)
02860            ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
02861          return FALSE;
02862        }
02863       else
02864        {
02865          h = obj_coff_sym_hashes (input_bfd)[symndx];
02866          sym = syms + symndx;
02867        }
02868 
02869       /* COFF treats common symbols in one of two ways.  Either the
02870          size of the symbol is included in the section contents, or it
02871          is not.  We assume that the size is not included, and force
02872          the rtype_to_howto function to adjust the addend as needed.  */
02873       if (sym != NULL && sym->n_scnum != 0)
02874        addend = - sym->n_value;
02875       else
02876        addend = 0;
02877 
02878       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
02879                                    sym, &addend);
02880       if (howto == NULL)
02881        return FALSE;
02882 
02883       /* If we are doing a relocatable link, then we can just ignore
02884          a PC relative reloc that is pcrel_offset.  It will already
02885          have the correct value.  If this is not a relocatable link,
02886          then we should ignore the symbol value.  */
02887       if (howto->pc_relative && howto->pcrel_offset)
02888        {
02889          if (info->relocatable)
02890            continue;
02891          if (sym != NULL && sym->n_scnum != 0)
02892            addend += sym->n_value;
02893        }
02894 
02895       val = 0;
02896 
02897       if (h == NULL)
02898        {
02899          asection *sec;
02900 
02901          if (symndx == -1)
02902            {
02903              sec = bfd_abs_section_ptr;
02904              val = 0;
02905            }
02906          else
02907            {
02908              sec = sections[symndx];
02909               val = (sec->output_section->vma
02910                    + sec->output_offset
02911                    + sym->n_value);
02912              if (! obj_pe (input_bfd))
02913               val -= sec->vma;
02914            }
02915        }
02916       else
02917        {
02918          if (h->root.type == bfd_link_hash_defined
02919              || h->root.type == bfd_link_hash_defweak)
02920            {
02921              /* Defined weak symbols are a GNU extension. */
02922              asection *sec;
02923 
02924              sec = h->root.u.def.section;
02925              val = (h->root.u.def.value
02926                    + sec->output_section->vma
02927                    + sec->output_offset);
02928            }
02929 
02930          else if (h->root.type == bfd_link_hash_undefweak)
02931            {
02932               if (h->class == C_NT_WEAK && h->numaux == 1)
02933               {
02934                 /* See _Microsoft Portable Executable and Common Object
02935                      File Format Specification_, section 5.5.3.
02936                    Note that weak symbols without aux records are a GNU
02937                    extension.
02938                    FIXME: All weak externals are treated as having
02939                    characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
02940                    These behave as per SVR4 ABI:  A library member
02941                    will resolve a weak external only if a normal
02942                    external causes the library member to be linked.
02943                    See also linker.c: generic_link_check_archive_element. */
02944                 asection *sec;
02945                 struct coff_link_hash_entry *h2 =
02946                   input_bfd->tdata.coff_obj_data->sym_hashes[
02947                   h->aux->x_sym.x_tagndx.l];
02948 
02949                 if (!h2 || h2->root.type == bfd_link_hash_undefined)
02950                   {
02951                     sec = bfd_abs_section_ptr;
02952                     val = 0;
02953                   }
02954                 else
02955                   {
02956                     sec = h2->root.u.def.section;
02957                     val = h2->root.u.def.value
02958                      + sec->output_section->vma + sec->output_offset;
02959                   }
02960               }
02961              else
02962                 /* This is a GNU extension.  */
02963               val = 0;
02964            }
02965 
02966          else if (! info->relocatable)
02967            {
02968              if (! ((*info->callbacks->undefined_symbol)
02969                    (info, h->root.root.string, input_bfd, input_section,
02970                     rel->r_vaddr - input_section->vma, TRUE)))
02971               return FALSE;
02972            }
02973        }
02974 
02975       if (info->base_file)
02976        {
02977          /* Emit a reloc if the backend thinks it needs it.  */
02978          if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
02979            {
02980              /* Relocation to a symbol in a section which isn't
02981                absolute.  We output the address here to a file.
02982                This file is then read by dlltool when generating the
02983                reloc section.  Note that the base file is not
02984                portable between systems.  We write out a long here,
02985                and dlltool reads in a long.  */
02986              long addr = (rel->r_vaddr
02987                         - input_section->vma
02988                         + input_section->output_offset
02989                         + input_section->output_section->vma);
02990              if (coff_data (output_bfd)->pe)
02991               addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
02992              if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
02993                 != sizeof (long))
02994               {
02995                 bfd_set_error (bfd_error_system_call);
02996                 return FALSE;
02997               }
02998            }
02999        }
03000 
03001       rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
03002                                    contents,
03003                                    rel->r_vaddr - input_section->vma,
03004                                    val, addend);
03005 
03006       switch (rstat)
03007        {
03008        default:
03009          abort ();
03010        case bfd_reloc_ok:
03011          break;
03012        case bfd_reloc_outofrange:
03013          (*_bfd_error_handler)
03014            (_("%B: bad reloc address 0x%lx in section `%A'"),
03015             input_bfd, input_section, (unsigned long) rel->r_vaddr);
03016          return FALSE;
03017        case bfd_reloc_overflow:
03018          {
03019            const char *name;
03020            char buf[SYMNMLEN + 1];
03021 
03022            if (symndx == -1)
03023              name = "*ABS*";
03024            else if (h != NULL)
03025              name = NULL;
03026            else
03027              {
03028               name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
03029               if (name == NULL)
03030                 return FALSE;
03031              }
03032 
03033            if (! ((*info->callbacks->reloc_overflow)
03034                  (info, (h ? &h->root : NULL), name, howto->name,
03035                   (bfd_vma) 0, input_bfd, input_section,
03036                   rel->r_vaddr - input_section->vma)))
03037              return FALSE;
03038          }
03039        }
03040     }
03041   return TRUE;
03042 }