Subversion Repositories Kolibri OS

Rev

Rev 5197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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