home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / cofflink.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  69KB  |  2,492 lines

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