Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* ELF linking support for BFD.
  2.    Copyright 1995-2013 Free Software Foundation, Inc.
  3.  
  4.    This file is part of BFD, the Binary File Descriptor library.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19.    MA 02110-1301, USA.  */
  20.  
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "bfdlink.h"
  24. #include "libbfd.h"
  25. #define ARCH_SIZE 0
  26. #include "elf-bfd.h"
  27. #include "safe-ctype.h"
  28. #include "libiberty.h"
  29. #include "objalloc.h"
  30.  
  31. /* This struct is used to pass information to routines called via
  32.    elf_link_hash_traverse which must return failure.  */
  33.  
  34. struct elf_info_failed
  35. {
  36.   struct bfd_link_info *info;
  37.   bfd_boolean failed;
  38. };
  39.  
  40. /* This structure is used to pass information to
  41.    _bfd_elf_link_find_version_dependencies.  */
  42.  
  43. struct elf_find_verdep_info
  44. {
  45.   /* General link information.  */
  46.   struct bfd_link_info *info;
  47.   /* The number of dependencies.  */
  48.   unsigned int vers;
  49.   /* Whether we had a failure.  */
  50.   bfd_boolean failed;
  51. };
  52.  
  53. static bfd_boolean _bfd_elf_fix_symbol_flags
  54.   (struct elf_link_hash_entry *, struct elf_info_failed *);
  55.  
  56. /* Define a symbol in a dynamic linkage section.  */
  57.  
  58. struct elf_link_hash_entry *
  59. _bfd_elf_define_linkage_sym (bfd *abfd,
  60.                              struct bfd_link_info *info,
  61.                              asection *sec,
  62.                              const char *name)
  63. {
  64.   struct elf_link_hash_entry *h;
  65.   struct bfd_link_hash_entry *bh;
  66.   const struct elf_backend_data *bed;
  67.  
  68.   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
  69.   if (h != NULL)
  70.     {
  71.       /* Zap symbol defined in an as-needed lib that wasn't linked.
  72.          This is a symptom of a larger problem:  Absolute symbols
  73.          defined in shared libraries can't be overridden, because we
  74.          lose the link to the bfd which is via the symbol section.  */
  75.       h->root.type = bfd_link_hash_new;
  76.     }
  77.  
  78.   bh = &h->root;
  79.   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
  80.                                          sec, 0, NULL, FALSE,
  81.                                          get_elf_backend_data (abfd)->collect,
  82.                                          &bh))
  83.     return NULL;
  84.   h = (struct elf_link_hash_entry *) bh;
  85.   h->def_regular = 1;
  86.   h->non_elf = 0;
  87.   h->type = STT_OBJECT;
  88.   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  89.     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  90.  
  91.   bed = get_elf_backend_data (abfd);
  92.   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  93.   return h;
  94. }
  95.  
  96. bfd_boolean
  97. _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  98. {
  99.   flagword flags;
  100.   asection *s;
  101.   struct elf_link_hash_entry *h;
  102.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  103.   struct elf_link_hash_table *htab = elf_hash_table (info);
  104.  
  105.   /* This function may be called more than once.  */
  106.   s = bfd_get_linker_section (abfd, ".got");
  107.   if (s != NULL)
  108.     return TRUE;
  109.  
  110.   flags = bed->dynamic_sec_flags;
  111.  
  112.   s = bfd_make_section_anyway_with_flags (abfd,
  113.                                           (bed->rela_plts_and_copies_p
  114.                                            ? ".rela.got" : ".rel.got"),
  115.                                           (bed->dynamic_sec_flags
  116.                                            | SEC_READONLY));
  117.   if (s == NULL
  118.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  119.     return FALSE;
  120.   htab->srelgot = s;
  121.  
  122.   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  123.   if (s == NULL
  124.       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  125.     return FALSE;
  126.   htab->sgot = s;
  127.  
  128.   if (bed->want_got_plt)
  129.     {
  130.       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  131.       if (s == NULL
  132.           || !bfd_set_section_alignment (abfd, s,
  133.                                          bed->s->log_file_align))
  134.         return FALSE;
  135.       htab->sgotplt = s;
  136.     }
  137.  
  138.   /* The first bit of the global offset table is the header.  */
  139.   s->size += bed->got_header_size;
  140.  
  141.   if (bed->want_got_sym)
  142.     {
  143.       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  144.          (or .got.plt) section.  We don't do this in the linker script
  145.          because we don't want to define the symbol if we are not creating
  146.          a global offset table.  */
  147.       h = _bfd_elf_define_linkage_sym (abfd, info, s,
  148.                                        "_GLOBAL_OFFSET_TABLE_");
  149.       elf_hash_table (info)->hgot = h;
  150.       if (h == NULL)
  151.         return FALSE;
  152.     }
  153.  
  154.   return TRUE;
  155. }
  156. /* Create a strtab to hold the dynamic symbol names.  */
  157. static bfd_boolean
  158. _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
  159. {
  160.   struct elf_link_hash_table *hash_table;
  161.  
  162.   hash_table = elf_hash_table (info);
  163.   if (hash_table->dynobj == NULL)
  164.     hash_table->dynobj = abfd;
  165.  
  166.   if (hash_table->dynstr == NULL)
  167.     {
  168.       hash_table->dynstr = _bfd_elf_strtab_init ();
  169.       if (hash_table->dynstr == NULL)
  170.         return FALSE;
  171.     }
  172.   return TRUE;
  173. }
  174.  
  175. /* Create some sections which will be filled in with dynamic linking
  176.    information.  ABFD is an input file which requires dynamic sections
  177.    to be created.  The dynamic sections take up virtual memory space
  178.    when the final executable is run, so we need to create them before
  179.    addresses are assigned to the output sections.  We work out the
  180.    actual contents and size of these sections later.  */
  181.  
  182. bfd_boolean
  183. _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  184. {
  185.   flagword flags;
  186.   asection *s;
  187.   const struct elf_backend_data *bed;
  188.   struct elf_link_hash_entry *h;
  189.  
  190.   if (! is_elf_hash_table (info->hash))
  191.     return FALSE;
  192.  
  193.   if (elf_hash_table (info)->dynamic_sections_created)
  194.     return TRUE;
  195.  
  196.   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  197.     return FALSE;
  198.  
  199.   abfd = elf_hash_table (info)->dynobj;
  200.   bed = get_elf_backend_data (abfd);
  201.  
  202.   flags = bed->dynamic_sec_flags;
  203.  
  204.   /* A dynamically linked executable has a .interp section, but a
  205.      shared library does not.  */
  206.   if (info->executable)
  207.     {
  208.       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
  209.                                               flags | SEC_READONLY);
  210.       if (s == NULL)
  211.         return FALSE;
  212.     }
  213.  
  214.   /* Create sections to hold version informations.  These are removed
  215.      if they are not needed.  */
  216.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
  217.                                           flags | SEC_READONLY);
  218.   if (s == NULL
  219.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  220.     return FALSE;
  221.  
  222.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
  223.                                           flags | SEC_READONLY);
  224.   if (s == NULL
  225.       || ! bfd_set_section_alignment (abfd, s, 1))
  226.     return FALSE;
  227.  
  228.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
  229.                                           flags | SEC_READONLY);
  230.   if (s == NULL
  231.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  232.     return FALSE;
  233.  
  234.   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
  235.                                           flags | SEC_READONLY);
  236.   if (s == NULL
  237.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  238.     return FALSE;
  239.  
  240.   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
  241.                                           flags | SEC_READONLY);
  242.   if (s == NULL)
  243.     return FALSE;
  244.  
  245.   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
  246.   if (s == NULL
  247.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  248.     return FALSE;
  249.  
  250.   /* The special symbol _DYNAMIC is always set to the start of the
  251.      .dynamic section.  We could set _DYNAMIC in a linker script, but we
  252.      only want to define it if we are, in fact, creating a .dynamic
  253.      section.  We don't want to define it if there is no .dynamic
  254.      section, since on some ELF platforms the start up code examines it
  255.      to decide how to initialize the process.  */
  256.   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
  257.   elf_hash_table (info)->hdynamic = h;
  258.   if (h == NULL)
  259.     return FALSE;
  260.  
  261.   if (info->emit_hash)
  262.     {
  263.       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
  264.                                               flags | SEC_READONLY);
  265.       if (s == NULL
  266.           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  267.         return FALSE;
  268.       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
  269.     }
  270.  
  271.   if (info->emit_gnu_hash)
  272.     {
  273.       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
  274.                                               flags | SEC_READONLY);
  275.       if (s == NULL
  276.           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  277.         return FALSE;
  278.       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
  279.          4 32-bit words followed by variable count of 64-bit words, then
  280.          variable count of 32-bit words.  */
  281.       if (bed->s->arch_size == 64)
  282.         elf_section_data (s)->this_hdr.sh_entsize = 0;
  283.       else
  284.         elf_section_data (s)->this_hdr.sh_entsize = 4;
  285.     }
  286.  
  287.   /* Let the backend create the rest of the sections.  This lets the
  288.      backend set the right flags.  The backend will normally create
  289.      the .got and .plt sections.  */
  290.   if (bed->elf_backend_create_dynamic_sections == NULL
  291.       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
  292.     return FALSE;
  293.  
  294.   elf_hash_table (info)->dynamic_sections_created = TRUE;
  295.  
  296.   return TRUE;
  297. }
  298.  
  299. /* Create dynamic sections when linking against a dynamic object.  */
  300.  
  301. bfd_boolean
  302. _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  303. {
  304.   flagword flags, pltflags;
  305.   struct elf_link_hash_entry *h;
  306.   asection *s;
  307.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  308.   struct elf_link_hash_table *htab = elf_hash_table (info);
  309.  
  310.   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  311.      .rel[a].bss sections.  */
  312.   flags = bed->dynamic_sec_flags;
  313.  
  314.   pltflags = flags;
  315.   if (bed->plt_not_loaded)
  316.     /* We do not clear SEC_ALLOC here because we still want the OS to
  317.        allocate space for the section; it's just that there's nothing
  318.        to read in from the object file.  */
  319.     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  320.   else
  321.     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
  322.   if (bed->plt_readonly)
  323.     pltflags |= SEC_READONLY;
  324.  
  325.   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  326.   if (s == NULL
  327.       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
  328.     return FALSE;
  329.   htab->splt = s;
  330.  
  331.   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  332.      .plt section.  */
  333.   if (bed->want_plt_sym)
  334.     {
  335.       h = _bfd_elf_define_linkage_sym (abfd, info, s,
  336.                                        "_PROCEDURE_LINKAGE_TABLE_");
  337.       elf_hash_table (info)->hplt = h;
  338.       if (h == NULL)
  339.         return FALSE;
  340.     }
  341.  
  342.   s = bfd_make_section_anyway_with_flags (abfd,
  343.                                           (bed->rela_plts_and_copies_p
  344.                                            ? ".rela.plt" : ".rel.plt"),
  345.                                           flags | SEC_READONLY);
  346.   if (s == NULL
  347.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  348.     return FALSE;
  349.   htab->srelplt = s;
  350.  
  351.   if (! _bfd_elf_create_got_section (abfd, info))
  352.     return FALSE;
  353.  
  354.   if (bed->want_dynbss)
  355.     {
  356.       /* The .dynbss section is a place to put symbols which are defined
  357.          by dynamic objects, are referenced by regular objects, and are
  358.          not functions.  We must allocate space for them in the process
  359.          image and use a R_*_COPY reloc to tell the dynamic linker to
  360.          initialize them at run time.  The linker script puts the .dynbss
  361.          section into the .bss section of the final image.  */
  362.       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  363.                                               (SEC_ALLOC | SEC_LINKER_CREATED));
  364.       if (s == NULL)
  365.         return FALSE;
  366.  
  367.       /* The .rel[a].bss section holds copy relocs.  This section is not
  368.          normally needed.  We need to create it here, though, so that the
  369.          linker will map it to an output section.  We can't just create it
  370.          only if we need it, because we will not know whether we need it
  371.          until we have seen all the input files, and the first time the
  372.          main linker code calls BFD after examining all the input files
  373.          (size_dynamic_sections) the input sections have already been
  374.          mapped to the output sections.  If the section turns out not to
  375.          be needed, we can discard it later.  We will never need this
  376.          section when generating a shared object, since they do not use
  377.          copy relocs.  */
  378.       if (! info->shared)
  379.         {
  380.           s = bfd_make_section_anyway_with_flags (abfd,
  381.                                                   (bed->rela_plts_and_copies_p
  382.                                                    ? ".rela.bss" : ".rel.bss"),
  383.                                                   flags | SEC_READONLY);
  384.           if (s == NULL
  385.               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  386.             return FALSE;
  387.         }
  388.     }
  389.  
  390.   return TRUE;
  391. }
  392. /* Record a new dynamic symbol.  We record the dynamic symbols as we
  393.    read the input files, since we need to have a list of all of them
  394.    before we can determine the final sizes of the output sections.
  395.    Note that we may actually call this function even though we are not
  396.    going to output any dynamic symbols; in some cases we know that a
  397.    symbol should be in the dynamic symbol table, but only if there is
  398.    one.  */
  399.  
  400. bfd_boolean
  401. bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
  402.                                     struct elf_link_hash_entry *h)
  403. {
  404.   if (h->dynindx == -1)
  405.     {
  406.       struct elf_strtab_hash *dynstr;
  407.       char *p;
  408.       const char *name;
  409.       bfd_size_type indx;
  410.  
  411.       /* XXX: The ABI draft says the linker must turn hidden and
  412.          internal symbols into STB_LOCAL symbols when producing the
  413.          DSO. However, if ld.so honors st_other in the dynamic table,
  414.          this would not be necessary.  */
  415.       switch (ELF_ST_VISIBILITY (h->other))
  416.         {
  417.         case STV_INTERNAL:
  418.         case STV_HIDDEN:
  419.           if (h->root.type != bfd_link_hash_undefined
  420.               && h->root.type != bfd_link_hash_undefweak)
  421.             {
  422.               h->forced_local = 1;
  423.               if (!elf_hash_table (info)->is_relocatable_executable)
  424.                 return TRUE;
  425.             }
  426.  
  427.         default:
  428.           break;
  429.         }
  430.  
  431.       h->dynindx = elf_hash_table (info)->dynsymcount;
  432.       ++elf_hash_table (info)->dynsymcount;
  433.  
  434.       dynstr = elf_hash_table (info)->dynstr;
  435.       if (dynstr == NULL)
  436.         {
  437.           /* Create a strtab to hold the dynamic symbol names.  */
  438.           elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  439.           if (dynstr == NULL)
  440.             return FALSE;
  441.         }
  442.  
  443.       /* We don't put any version information in the dynamic string
  444.          table.  */
  445.       name = h->root.root.string;
  446.       p = strchr (name, ELF_VER_CHR);
  447.       if (p != NULL)
  448.         /* We know that the p points into writable memory.  In fact,
  449.            there are only a few symbols that have read-only names, being
  450.            those like _GLOBAL_OFFSET_TABLE_ that are created specially
  451.            by the backends.  Most symbols will have names pointing into
  452.            an ELF string table read from a file, or to objalloc memory.  */
  453.         *p = 0;
  454.  
  455.       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
  456.  
  457.       if (p != NULL)
  458.         *p = ELF_VER_CHR;
  459.  
  460.       if (indx == (bfd_size_type) -1)
  461.         return FALSE;
  462.       h->dynstr_index = indx;
  463.     }
  464.  
  465.   return TRUE;
  466. }
  467. /* Mark a symbol dynamic.  */
  468.  
  469. static void
  470. bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
  471.                                   struct elf_link_hash_entry *h,
  472.                                   Elf_Internal_Sym *sym)
  473. {
  474.   struct bfd_elf_dynamic_list *d = info->dynamic_list;
  475.  
  476.   /* It may be called more than once on the same H.  */
  477.   if(h->dynamic || info->relocatable)
  478.     return;
  479.  
  480.   if ((info->dynamic_data
  481.        && (h->type == STT_OBJECT
  482.            || (sym != NULL
  483.                && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
  484.       || (d != NULL
  485.           && h->root.type == bfd_link_hash_new
  486.           && (*d->match) (&d->head, NULL, h->root.root.string)))
  487.     h->dynamic = 1;
  488. }
  489.  
  490. /* Record an assignment to a symbol made by a linker script.  We need
  491.    this in case some dynamic object refers to this symbol.  */
  492.  
  493. bfd_boolean
  494. bfd_elf_record_link_assignment (bfd *output_bfd,
  495.                                 struct bfd_link_info *info,
  496.                                 const char *name,
  497.                                 bfd_boolean provide,
  498.                                 bfd_boolean hidden)
  499. {
  500.   struct elf_link_hash_entry *h, *hv;
  501.   struct elf_link_hash_table *htab;
  502.   const struct elf_backend_data *bed;
  503.  
  504.   if (!is_elf_hash_table (info->hash))
  505.     return TRUE;
  506.  
  507.   htab = elf_hash_table (info);
  508.   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
  509.   if (h == NULL)
  510.     return provide;
  511.  
  512.   switch (h->root.type)
  513.     {
  514.     case bfd_link_hash_defined:
  515.     case bfd_link_hash_defweak:
  516.     case bfd_link_hash_common:
  517.       break;
  518.     case bfd_link_hash_undefweak:
  519.     case bfd_link_hash_undefined:
  520.       /* Since we're defining the symbol, don't let it seem to have not
  521.          been defined.  record_dynamic_symbol and size_dynamic_sections
  522.          may depend on this.  */
  523.       h->root.type = bfd_link_hash_new;
  524.       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
  525.         bfd_link_repair_undef_list (&htab->root);
  526.       break;
  527.     case bfd_link_hash_new:
  528.       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
  529.       h->non_elf = 0;
  530.       break;
  531.     case bfd_link_hash_indirect:
  532.       /* We had a versioned symbol in a dynamic library.  We make the
  533.          the versioned symbol point to this one.  */
  534.       bed = get_elf_backend_data (output_bfd);
  535.       hv = h;
  536.       while (hv->root.type == bfd_link_hash_indirect
  537.              || hv->root.type == bfd_link_hash_warning)
  538.         hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
  539.       /* We don't need to update h->root.u since linker will set them
  540.          later.  */
  541.       h->root.type = bfd_link_hash_undefined;
  542.       hv->root.type = bfd_link_hash_indirect;
  543.       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
  544.       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
  545.       break;
  546.     case bfd_link_hash_warning:
  547.       abort ();
  548.       break;
  549.     }
  550.  
  551.   /* If this symbol is being provided by the linker script, and it is
  552.      currently defined by a dynamic object, but not by a regular
  553.      object, then mark it as undefined so that the generic linker will
  554.      force the correct value.  */
  555.   if (provide
  556.       && h->def_dynamic
  557.       && !h->def_regular)
  558.     h->root.type = bfd_link_hash_undefined;
  559.  
  560.   /* If this symbol is not being provided by the linker script, and it is
  561.      currently defined by a dynamic object, but not by a regular object,
  562.      then clear out any version information because the symbol will not be
  563.      associated with the dynamic object any more.  */
  564.   if (!provide
  565.       && h->def_dynamic
  566.       && !h->def_regular)
  567.     h->verinfo.verdef = NULL;
  568.  
  569.   h->def_regular = 1;
  570.  
  571.   if (hidden)
  572.     {
  573.       bed = get_elf_backend_data (output_bfd);
  574.       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  575.         h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  576.       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  577.     }
  578.  
  579.   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
  580.      and executables.  */
  581.   if (!info->relocatable
  582.       && h->dynindx != -1
  583.       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  584.           || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
  585.     h->forced_local = 1;
  586.  
  587.   if ((h->def_dynamic
  588.        || h->ref_dynamic
  589.        || info->shared
  590.        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
  591.       && h->dynindx == -1)
  592.     {
  593.       if (! bfd_elf_link_record_dynamic_symbol (info, h))
  594.         return FALSE;
  595.  
  596.       /* If this is a weak defined symbol, and we know a corresponding
  597.          real symbol from the same dynamic object, make sure the real
  598.          symbol is also made into a dynamic symbol.  */
  599.       if (h->u.weakdef != NULL
  600.           && h->u.weakdef->dynindx == -1)
  601.         {
  602.           if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
  603.             return FALSE;
  604.         }
  605.     }
  606.  
  607.   return TRUE;
  608. }
  609.  
  610. /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
  611.    success, and 2 on a failure caused by attempting to record a symbol
  612.    in a discarded section, eg. a discarded link-once section symbol.  */
  613.  
  614. int
  615. bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
  616.                                           bfd *input_bfd,
  617.                                           long input_indx)
  618. {
  619.   bfd_size_type amt;
  620.   struct elf_link_local_dynamic_entry *entry;
  621.   struct elf_link_hash_table *eht;
  622.   struct elf_strtab_hash *dynstr;
  623.   unsigned long dynstr_index;
  624.   char *name;
  625.   Elf_External_Sym_Shndx eshndx;
  626.   char esym[sizeof (Elf64_External_Sym)];
  627.  
  628.   if (! is_elf_hash_table (info->hash))
  629.     return 0;
  630.  
  631.   /* See if the entry exists already.  */
  632.   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
  633.     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
  634.       return 1;
  635.  
  636.   amt = sizeof (*entry);
  637.   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
  638.   if (entry == NULL)
  639.     return 0;
  640.  
  641.   /* Go find the symbol, so that we can find it's name.  */
  642.   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
  643.                              1, input_indx, &entry->isym, esym, &eshndx))
  644.     {
  645.       bfd_release (input_bfd, entry);
  646.       return 0;
  647.     }
  648.  
  649.   if (entry->isym.st_shndx != SHN_UNDEF
  650.       && entry->isym.st_shndx < SHN_LORESERVE)
  651.     {
  652.       asection *s;
  653.  
  654.       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
  655.       if (s == NULL || bfd_is_abs_section (s->output_section))
  656.         {
  657.           /* We can still bfd_release here as nothing has done another
  658.              bfd_alloc.  We can't do this later in this function.  */
  659.           bfd_release (input_bfd, entry);
  660.           return 2;
  661.         }
  662.     }
  663.  
  664.   name = (bfd_elf_string_from_elf_section
  665.           (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
  666.            entry->isym.st_name));
  667.  
  668.   dynstr = elf_hash_table (info)->dynstr;
  669.   if (dynstr == NULL)
  670.     {
  671.       /* Create a strtab to hold the dynamic symbol names.  */
  672.       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  673.       if (dynstr == NULL)
  674.         return 0;
  675.     }
  676.  
  677.   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
  678.   if (dynstr_index == (unsigned long) -1)
  679.     return 0;
  680.   entry->isym.st_name = dynstr_index;
  681.  
  682.   eht = elf_hash_table (info);
  683.  
  684.   entry->next = eht->dynlocal;
  685.   eht->dynlocal = entry;
  686.   entry->input_bfd = input_bfd;
  687.   entry->input_indx = input_indx;
  688.   eht->dynsymcount++;
  689.  
  690.   /* Whatever binding the symbol had before, it's now local.  */
  691.   entry->isym.st_info
  692.     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
  693.  
  694.   /* The dynindx will be set at the end of size_dynamic_sections.  */
  695.  
  696.   return 1;
  697. }
  698.  
  699. /* Return the dynindex of a local dynamic symbol.  */
  700.  
  701. long
  702. _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
  703.                                     bfd *input_bfd,
  704.                                     long input_indx)
  705. {
  706.   struct elf_link_local_dynamic_entry *e;
  707.  
  708.   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  709.     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
  710.       return e->dynindx;
  711.   return -1;
  712. }
  713.  
  714. /* This function is used to renumber the dynamic symbols, if some of
  715.    them are removed because they are marked as local.  This is called
  716.    via elf_link_hash_traverse.  */
  717.  
  718. static bfd_boolean
  719. elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
  720.                                       void *data)
  721. {
  722.   size_t *count = (size_t *) data;
  723.  
  724.   if (h->forced_local)
  725.     return TRUE;
  726.  
  727.   if (h->dynindx != -1)
  728.     h->dynindx = ++(*count);
  729.  
  730.   return TRUE;
  731. }
  732.  
  733.  
  734. /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
  735.    STB_LOCAL binding.  */
  736.  
  737. static bfd_boolean
  738. elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
  739.                                             void *data)
  740. {
  741.   size_t *count = (size_t *) data;
  742.  
  743.   if (!h->forced_local)
  744.     return TRUE;
  745.  
  746.   if (h->dynindx != -1)
  747.     h->dynindx = ++(*count);
  748.  
  749.   return TRUE;
  750. }
  751.  
  752. /* Return true if the dynamic symbol for a given section should be
  753.    omitted when creating a shared library.  */
  754. bfd_boolean
  755. _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
  756.                                    struct bfd_link_info *info,
  757.                                    asection *p)
  758. {
  759.   struct elf_link_hash_table *htab;
  760.  
  761.   switch (elf_section_data (p)->this_hdr.sh_type)
  762.     {
  763.     case SHT_PROGBITS:
  764.     case SHT_NOBITS:
  765.       /* If sh_type is yet undecided, assume it could be
  766.          SHT_PROGBITS/SHT_NOBITS.  */
  767.     case SHT_NULL:
  768.       htab = elf_hash_table (info);
  769.       if (p == htab->tls_sec)
  770.         return FALSE;
  771.  
  772.       if (htab->text_index_section != NULL)
  773.         return p != htab->text_index_section && p != htab->data_index_section;
  774.  
  775.       if (strcmp (p->name, ".got") == 0
  776.           || strcmp (p->name, ".got.plt") == 0
  777.           || strcmp (p->name, ".plt") == 0)
  778.         {
  779.           asection *ip;
  780.  
  781.           if (htab->dynobj != NULL
  782.               && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
  783.               && ip->output_section == p)
  784.             return TRUE;
  785.         }
  786.       return FALSE;
  787.  
  788.       /* There shouldn't be section relative relocations
  789.          against any other section.  */
  790.     default:
  791.       return TRUE;
  792.     }
  793. }
  794.  
  795. /* Assign dynsym indices.  In a shared library we generate a section
  796.    symbol for each output section, which come first.  Next come symbols
  797.    which have been forced to local binding.  Then all of the back-end
  798.    allocated local dynamic syms, followed by the rest of the global
  799.    symbols.  */
  800.  
  801. static unsigned long
  802. _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
  803.                                 struct bfd_link_info *info,
  804.                                 unsigned long *section_sym_count)
  805. {
  806.   unsigned long dynsymcount = 0;
  807.  
  808.   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
  809.     {
  810.       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  811.       asection *p;
  812.       for (p = output_bfd->sections; p ; p = p->next)
  813.         if ((p->flags & SEC_EXCLUDE) == 0
  814.             && (p->flags & SEC_ALLOC) != 0
  815.             && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
  816.           elf_section_data (p)->dynindx = ++dynsymcount;
  817.         else
  818.           elf_section_data (p)->dynindx = 0;
  819.     }
  820.   *section_sym_count = dynsymcount;
  821.  
  822.   elf_link_hash_traverse (elf_hash_table (info),
  823.                           elf_link_renumber_local_hash_table_dynsyms,
  824.                           &dynsymcount);
  825.  
  826.   if (elf_hash_table (info)->dynlocal)
  827.     {
  828.       struct elf_link_local_dynamic_entry *p;
  829.       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
  830.         p->dynindx = ++dynsymcount;
  831.     }
  832.  
  833.   elf_link_hash_traverse (elf_hash_table (info),
  834.                           elf_link_renumber_hash_table_dynsyms,
  835.                           &dynsymcount);
  836.  
  837.   /* There is an unused NULL entry at the head of the table which
  838.      we must account for in our count.  Unless there weren't any
  839.      symbols, which means we'll have no table at all.  */
  840.   if (dynsymcount != 0)
  841.     ++dynsymcount;
  842.  
  843.   elf_hash_table (info)->dynsymcount = dynsymcount;
  844.   return dynsymcount;
  845. }
  846.  
  847. /* Merge st_other field.  */
  848.  
  849. static void
  850. elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
  851.                     Elf_Internal_Sym *isym, bfd_boolean definition,
  852.                     bfd_boolean dynamic)
  853. {
  854.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  855.  
  856.   /* If st_other has a processor-specific meaning, specific
  857.      code might be needed here. We never merge the visibility
  858.      attribute with the one from a dynamic object.  */
  859.   if (bed->elf_backend_merge_symbol_attribute)
  860.     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
  861.                                                 dynamic);
  862.  
  863.   /* If this symbol has default visibility and the user has requested
  864.      we not re-export it, then mark it as hidden.  */
  865.   if (definition
  866.       && !dynamic
  867.       && (abfd->no_export
  868.           || (abfd->my_archive && abfd->my_archive->no_export))
  869.       && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
  870.     isym->st_other = (STV_HIDDEN
  871.                       | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
  872.  
  873.   if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
  874.     {
  875.       unsigned char hvis, symvis, other, nvis;
  876.  
  877.       /* Only merge the visibility. Leave the remainder of the
  878.          st_other field to elf_backend_merge_symbol_attribute.  */
  879.       other = h->other & ~ELF_ST_VISIBILITY (-1);
  880.  
  881.       /* Combine visibilities, using the most constraining one.  */
  882.       hvis = ELF_ST_VISIBILITY (h->other);
  883.       symvis = ELF_ST_VISIBILITY (isym->st_other);
  884.       if (! hvis)
  885.         nvis = symvis;
  886.       else if (! symvis)
  887.         nvis = hvis;
  888.       else
  889.         nvis = hvis < symvis ? hvis : symvis;
  890.  
  891.       h->other = other | nvis;
  892.     }
  893. }
  894.  
  895. /* This function is called when we want to merge a new symbol with an
  896.    existing symbol.  It handles the various cases which arise when we
  897.    find a definition in a dynamic object, or when there is already a
  898.    definition in a dynamic object.  The new symbol is described by
  899.    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
  900.    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
  901.    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
  902.    of an old common symbol.  We set OVERRIDE if the old symbol is
  903.    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
  904.    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
  905.    to change.  By OK to change, we mean that we shouldn't warn if the
  906.    type or size does change.  */
  907.  
  908. static bfd_boolean
  909. _bfd_elf_merge_symbol (bfd *abfd,
  910.                        struct bfd_link_info *info,
  911.                        const char *name,
  912.                        Elf_Internal_Sym *sym,
  913.                        asection **psec,
  914.                        bfd_vma *pvalue,
  915.                        struct elf_link_hash_entry **sym_hash,
  916.                        bfd **poldbfd,
  917.                        bfd_boolean *pold_weak,
  918.                        unsigned int *pold_alignment,
  919.                        bfd_boolean *skip,
  920.                        bfd_boolean *override,
  921.                        bfd_boolean *type_change_ok,
  922.                        bfd_boolean *size_change_ok)
  923. {
  924.   asection *sec, *oldsec;
  925.   struct elf_link_hash_entry *h;
  926.   struct elf_link_hash_entry *hi;
  927.   struct elf_link_hash_entry *flip;
  928.   int bind;
  929.   bfd *oldbfd;
  930.   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
  931.   bfd_boolean newweak, oldweak, newfunc, oldfunc;
  932.   const struct elf_backend_data *bed;
  933.  
  934.   *skip = FALSE;
  935.   *override = FALSE;
  936.  
  937.   sec = *psec;
  938.   bind = ELF_ST_BIND (sym->st_info);
  939.  
  940.   if (! bfd_is_und_section (sec))
  941.     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
  942.   else
  943.     h = ((struct elf_link_hash_entry *)
  944.          bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
  945.   if (h == NULL)
  946.     return FALSE;
  947.   *sym_hash = h;
  948.  
  949.   bed = get_elf_backend_data (abfd);
  950.  
  951.   /* For merging, we only care about real symbols.  But we need to make
  952.      sure that indirect symbol dynamic flags are updated.  */
  953.   hi = h;
  954.   while (h->root.type == bfd_link_hash_indirect
  955.          || h->root.type == bfd_link_hash_warning)
  956.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  957.  
  958.   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
  959.      existing symbol.  */
  960.  
  961.   oldbfd = NULL;
  962.   oldsec = NULL;
  963.   switch (h->root.type)
  964.     {
  965.     default:
  966.       break;
  967.  
  968.     case bfd_link_hash_undefined:
  969.     case bfd_link_hash_undefweak:
  970.       oldbfd = h->root.u.undef.abfd;
  971.       break;
  972.  
  973.     case bfd_link_hash_defined:
  974.     case bfd_link_hash_defweak:
  975.       oldbfd = h->root.u.def.section->owner;
  976.       oldsec = h->root.u.def.section;
  977.       break;
  978.  
  979.     case bfd_link_hash_common:
  980.       oldbfd = h->root.u.c.p->section->owner;
  981.       oldsec = h->root.u.c.p->section;
  982.       if (pold_alignment)
  983.         *pold_alignment = h->root.u.c.p->alignment_power;
  984.       break;
  985.     }
  986.   if (poldbfd && *poldbfd == NULL)
  987.     *poldbfd = oldbfd;
  988.  
  989.   /* Differentiate strong and weak symbols.  */
  990.   newweak = bind == STB_WEAK;
  991.   oldweak = (h->root.type == bfd_link_hash_defweak
  992.              || h->root.type == bfd_link_hash_undefweak);
  993.   if (pold_weak)
  994.     *pold_weak = oldweak;
  995.  
  996.   /* This code is for coping with dynamic objects, and is only useful
  997.      if we are doing an ELF link.  */
  998.   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
  999.     return TRUE;
  1000.  
  1001.   /* We have to check it for every instance since the first few may be
  1002.      references and not all compilers emit symbol type for undefined
  1003.      symbols.  */
  1004.   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
  1005.  
  1006.   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
  1007.      respectively, is from a dynamic object.  */
  1008.  
  1009.   newdyn = (abfd->flags & DYNAMIC) != 0;
  1010.  
  1011.   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
  1012.      syms and defined syms in dynamic libraries respectively.
  1013.      ref_dynamic on the other hand can be set for a symbol defined in
  1014.      a dynamic library, and def_dynamic may not be set;  When the
  1015.      definition in a dynamic lib is overridden by a definition in the
  1016.      executable use of the symbol in the dynamic lib becomes a
  1017.      reference to the executable symbol.  */
  1018.   if (newdyn)
  1019.     {
  1020.       if (bfd_is_und_section (sec))
  1021.         {
  1022.           if (bind != STB_WEAK)
  1023.             {
  1024.               h->ref_dynamic_nonweak = 1;
  1025.               hi->ref_dynamic_nonweak = 1;
  1026.             }
  1027.         }
  1028.       else
  1029.         {
  1030.           h->dynamic_def = 1;
  1031.           hi->dynamic_def = 1;
  1032.         }
  1033.     }
  1034.  
  1035.   /* If we just created the symbol, mark it as being an ELF symbol.
  1036.      Other than that, there is nothing to do--there is no merge issue
  1037.      with a newly defined symbol--so we just return.  */
  1038.  
  1039.   if (h->root.type == bfd_link_hash_new)
  1040.     {
  1041.       h->non_elf = 0;
  1042.       return TRUE;
  1043.     }
  1044.  
  1045.   /* In cases involving weak versioned symbols, we may wind up trying
  1046.      to merge a symbol with itself.  Catch that here, to avoid the
  1047.      confusion that results if we try to override a symbol with
  1048.      itself.  The additional tests catch cases like
  1049.      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
  1050.      dynamic object, which we do want to handle here.  */
  1051.   if (abfd == oldbfd
  1052.       && (newweak || oldweak)
  1053.       && ((abfd->flags & DYNAMIC) == 0
  1054.           || !h->def_regular))
  1055.     return TRUE;
  1056.  
  1057.   olddyn = FALSE;
  1058.   if (oldbfd != NULL)
  1059.     olddyn = (oldbfd->flags & DYNAMIC) != 0;
  1060.   else if (oldsec != NULL)
  1061.     {
  1062.       /* This handles the special SHN_MIPS_{TEXT,DATA} section
  1063.          indices used by MIPS ELF.  */
  1064.       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
  1065.     }
  1066.  
  1067.   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
  1068.      respectively, appear to be a definition rather than reference.  */
  1069.  
  1070.   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
  1071.  
  1072.   olddef = (h->root.type != bfd_link_hash_undefined
  1073.             && h->root.type != bfd_link_hash_undefweak
  1074.             && h->root.type != bfd_link_hash_common);
  1075.  
  1076.   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
  1077.      respectively, appear to be a function.  */
  1078.  
  1079.   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1080.              && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
  1081.  
  1082.   oldfunc = (h->type != STT_NOTYPE
  1083.              && bed->is_function_type (h->type));
  1084.  
  1085.   /* When we try to create a default indirect symbol from the dynamic
  1086.      definition with the default version, we skip it if its type and
  1087.      the type of existing regular definition mismatch.  We only do it
  1088.      if the existing regular definition won't be dynamic.  */
  1089.   if (pold_alignment == NULL
  1090.       && !info->shared
  1091.       && !info->export_dynamic
  1092.       && !h->ref_dynamic
  1093.       && newdyn
  1094.       && newdef
  1095.       && !olddyn
  1096.       && (olddef || h->root.type == bfd_link_hash_common)
  1097.       && ELF_ST_TYPE (sym->st_info) != h->type
  1098.       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1099.       && h->type != STT_NOTYPE
  1100.       && !(newfunc && oldfunc))
  1101.     {
  1102.       *skip = TRUE;
  1103.       return TRUE;
  1104.     }
  1105.  
  1106.   /* Plugin symbol type isn't currently set.  Stop bogus errors.  */
  1107.   if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
  1108.     *type_change_ok = TRUE;
  1109.  
  1110.   /* Check TLS symbol.  We don't check undefined symbol introduced by
  1111.      "ld -u".  */
  1112.   else if (oldbfd != NULL
  1113.            && ELF_ST_TYPE (sym->st_info) != h->type
  1114.            && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
  1115.     {
  1116.       bfd *ntbfd, *tbfd;
  1117.       bfd_boolean ntdef, tdef;
  1118.       asection *ntsec, *tsec;
  1119.  
  1120.       if (h->type == STT_TLS)
  1121.         {
  1122.           ntbfd = abfd;
  1123.           ntsec = sec;
  1124.           ntdef = newdef;
  1125.           tbfd = oldbfd;
  1126.           tsec = oldsec;
  1127.           tdef = olddef;
  1128.         }
  1129.       else
  1130.         {
  1131.           ntbfd = oldbfd;
  1132.           ntsec = oldsec;
  1133.           ntdef = olddef;
  1134.           tbfd = abfd;
  1135.           tsec = sec;
  1136.           tdef = newdef;
  1137.         }
  1138.  
  1139.       if (tdef && ntdef)
  1140.         (*_bfd_error_handler)
  1141.           (_("%s: TLS definition in %B section %A "
  1142.              "mismatches non-TLS definition in %B section %A"),
  1143.            tbfd, tsec, ntbfd, ntsec, h->root.root.string);
  1144.       else if (!tdef && !ntdef)
  1145.         (*_bfd_error_handler)
  1146.           (_("%s: TLS reference in %B "
  1147.              "mismatches non-TLS reference in %B"),
  1148.            tbfd, ntbfd, h->root.root.string);
  1149.       else if (tdef)
  1150.         (*_bfd_error_handler)
  1151.           (_("%s: TLS definition in %B section %A "
  1152.              "mismatches non-TLS reference in %B"),
  1153.            tbfd, tsec, ntbfd, h->root.root.string);
  1154.       else
  1155.         (*_bfd_error_handler)
  1156.           (_("%s: TLS reference in %B "
  1157.              "mismatches non-TLS definition in %B section %A"),
  1158.            tbfd, ntbfd, ntsec, h->root.root.string);
  1159.  
  1160.       bfd_set_error (bfd_error_bad_value);
  1161.       return FALSE;
  1162.     }
  1163.  
  1164.   /* If the old symbol has non-default visibility, we ignore the new
  1165.      definition from a dynamic object.  */
  1166.   if (newdyn
  1167.       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1168.       && !bfd_is_und_section (sec))
  1169.     {
  1170.       *skip = TRUE;
  1171.       /* Make sure this symbol is dynamic.  */
  1172.       h->ref_dynamic = 1;
  1173.       hi->ref_dynamic = 1;
  1174.       /* A protected symbol has external availability. Make sure it is
  1175.          recorded as dynamic.
  1176.  
  1177.          FIXME: Should we check type and size for protected symbol?  */
  1178.       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
  1179.         return bfd_elf_link_record_dynamic_symbol (info, h);
  1180.       else
  1181.         return TRUE;
  1182.     }
  1183.   else if (!newdyn
  1184.            && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
  1185.            && h->def_dynamic)
  1186.     {
  1187.       /* If the new symbol with non-default visibility comes from a
  1188.          relocatable file and the old definition comes from a dynamic
  1189.          object, we remove the old definition.  */
  1190.       if (hi->root.type == bfd_link_hash_indirect)
  1191.         {
  1192.           /* Handle the case where the old dynamic definition is
  1193.              default versioned.  We need to copy the symbol info from
  1194.              the symbol with default version to the normal one if it
  1195.              was referenced before.  */
  1196.           if (h->ref_regular)
  1197.             {
  1198.               hi->root.type = h->root.type;
  1199.               h->root.type = bfd_link_hash_indirect;
  1200.               (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
  1201.  
  1202.               h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1203.               if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1204.                 {
  1205.                   /* If the new symbol is hidden or internal, completely undo
  1206.                      any dynamic link state.  */
  1207.                   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1208.                   h->forced_local = 0;
  1209.                   h->ref_dynamic = 0;
  1210.                 }
  1211.               else
  1212.                 h->ref_dynamic = 1;
  1213.  
  1214.               h->def_dynamic = 0;
  1215.               /* FIXME: Should we check type and size for protected symbol?  */
  1216.               h->size = 0;
  1217.               h->type = 0;
  1218.  
  1219.               h = hi;
  1220.             }
  1221.           else
  1222.             h = hi;
  1223.         }
  1224.  
  1225.       /* If the old symbol was undefined before, then it will still be
  1226.          on the undefs list.  If the new symbol is undefined or
  1227.          common, we can't make it bfd_link_hash_new here, because new
  1228.          undefined or common symbols will be added to the undefs list
  1229.          by _bfd_generic_link_add_one_symbol.  Symbols may not be
  1230.          added twice to the undefs list.  Also, if the new symbol is
  1231.          undefweak then we don't want to lose the strong undef.  */
  1232.       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
  1233.         {
  1234.           h->root.type = bfd_link_hash_undefined;
  1235.           h->root.u.undef.abfd = abfd;
  1236.         }
  1237.       else
  1238.         {
  1239.           h->root.type = bfd_link_hash_new;
  1240.           h->root.u.undef.abfd = NULL;
  1241.         }
  1242.  
  1243.       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1244.         {
  1245.           /* If the new symbol is hidden or internal, completely undo
  1246.              any dynamic link state.  */
  1247.           (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1248.           h->forced_local = 0;
  1249.           h->ref_dynamic = 0;
  1250.         }
  1251.       else
  1252.         h->ref_dynamic = 1;
  1253.       h->def_dynamic = 0;
  1254.       /* FIXME: Should we check type and size for protected symbol?  */
  1255.       h->size = 0;
  1256.       h->type = 0;
  1257.       return TRUE;
  1258.     }
  1259.  
  1260.   /* If a new weak symbol definition comes from a regular file and the
  1261.      old symbol comes from a dynamic library, we treat the new one as
  1262.      strong.  Similarly, an old weak symbol definition from a regular
  1263.      file is treated as strong when the new symbol comes from a dynamic
  1264.      library.  Further, an old weak symbol from a dynamic library is
  1265.      treated as strong if the new symbol is from a dynamic library.
  1266.      This reflects the way glibc's ld.so works.
  1267.  
  1268.      Do this before setting *type_change_ok or *size_change_ok so that
  1269.      we warn properly when dynamic library symbols are overridden.  */
  1270.  
  1271.   if (newdef && !newdyn && olddyn)
  1272.     newweak = FALSE;
  1273.   if (olddef && newdyn)
  1274.     oldweak = FALSE;
  1275.  
  1276.   /* Allow changes between different types of function symbol.  */
  1277.   if (newfunc && oldfunc)
  1278.     *type_change_ok = TRUE;
  1279.  
  1280.   /* It's OK to change the type if either the existing symbol or the
  1281.      new symbol is weak.  A type change is also OK if the old symbol
  1282.      is undefined and the new symbol is defined.  */
  1283.  
  1284.   if (oldweak
  1285.       || newweak
  1286.       || (newdef
  1287.           && h->root.type == bfd_link_hash_undefined))
  1288.     *type_change_ok = TRUE;
  1289.  
  1290.   /* It's OK to change the size if either the existing symbol or the
  1291.      new symbol is weak, or if the old symbol is undefined.  */
  1292.  
  1293.   if (*type_change_ok
  1294.       || h->root.type == bfd_link_hash_undefined)
  1295.     *size_change_ok = TRUE;
  1296.  
  1297.   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
  1298.      symbol, respectively, appears to be a common symbol in a dynamic
  1299.      object.  If a symbol appears in an uninitialized section, and is
  1300.      not weak, and is not a function, then it may be a common symbol
  1301.      which was resolved when the dynamic object was created.  We want
  1302.      to treat such symbols specially, because they raise special
  1303.      considerations when setting the symbol size: if the symbol
  1304.      appears as a common symbol in a regular object, and the size in
  1305.      the regular object is larger, we must make sure that we use the
  1306.      larger size.  This problematic case can always be avoided in C,
  1307.      but it must be handled correctly when using Fortran shared
  1308.      libraries.
  1309.  
  1310.      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
  1311.      likewise for OLDDYNCOMMON and OLDDEF.
  1312.  
  1313.      Note that this test is just a heuristic, and that it is quite
  1314.      possible to have an uninitialized symbol in a shared object which
  1315.      is really a definition, rather than a common symbol.  This could
  1316.      lead to some minor confusion when the symbol really is a common
  1317.      symbol in some regular object.  However, I think it will be
  1318.      harmless.  */
  1319.  
  1320.   if (newdyn
  1321.       && newdef
  1322.       && !newweak
  1323.       && (sec->flags & SEC_ALLOC) != 0
  1324.       && (sec->flags & SEC_LOAD) == 0
  1325.       && sym->st_size > 0
  1326.       && !newfunc)
  1327.     newdyncommon = TRUE;
  1328.   else
  1329.     newdyncommon = FALSE;
  1330.  
  1331.   if (olddyn
  1332.       && olddef
  1333.       && h->root.type == bfd_link_hash_defined
  1334.       && h->def_dynamic
  1335.       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
  1336.       && (h->root.u.def.section->flags & SEC_LOAD) == 0
  1337.       && h->size > 0
  1338.       && !oldfunc)
  1339.     olddyncommon = TRUE;
  1340.   else
  1341.     olddyncommon = FALSE;
  1342.  
  1343.   /* We now know everything about the old and new symbols.  We ask the
  1344.      backend to check if we can merge them.  */
  1345.   if (bed->merge_symbol != NULL)
  1346.     {
  1347.       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
  1348.         return FALSE;
  1349.       sec = *psec;
  1350.     }
  1351.  
  1352.   /* If both the old and the new symbols look like common symbols in a
  1353.      dynamic object, set the size of the symbol to the larger of the
  1354.      two.  */
  1355.  
  1356.   if (olddyncommon
  1357.       && newdyncommon
  1358.       && sym->st_size != h->size)
  1359.     {
  1360.       /* Since we think we have two common symbols, issue a multiple
  1361.          common warning if desired.  Note that we only warn if the
  1362.          size is different.  If the size is the same, we simply let
  1363.          the old symbol override the new one as normally happens with
  1364.          symbols defined in dynamic objects.  */
  1365.  
  1366.       if (! ((*info->callbacks->multiple_common)
  1367.              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
  1368.         return FALSE;
  1369.  
  1370.       if (sym->st_size > h->size)
  1371.         h->size = sym->st_size;
  1372.  
  1373.       *size_change_ok = TRUE;
  1374.     }
  1375.  
  1376.   /* If we are looking at a dynamic object, and we have found a
  1377.      definition, we need to see if the symbol was already defined by
  1378.      some other object.  If so, we want to use the existing
  1379.      definition, and we do not want to report a multiple symbol
  1380.      definition error; we do this by clobbering *PSEC to be
  1381.      bfd_und_section_ptr.
  1382.  
  1383.      We treat a common symbol as a definition if the symbol in the
  1384.      shared library is a function, since common symbols always
  1385.      represent variables; this can cause confusion in principle, but
  1386.      any such confusion would seem to indicate an erroneous program or
  1387.      shared library.  We also permit a common symbol in a regular
  1388.      object to override a weak symbol in a shared object.  */
  1389.  
  1390.   if (newdyn
  1391.       && newdef
  1392.       && (olddef
  1393.           || (h->root.type == bfd_link_hash_common
  1394.               && (newweak || newfunc))))
  1395.     {
  1396.       *override = TRUE;
  1397.       newdef = FALSE;
  1398.       newdyncommon = FALSE;
  1399.  
  1400.       *psec = sec = bfd_und_section_ptr;
  1401.       *size_change_ok = TRUE;
  1402.  
  1403.       /* If we get here when the old symbol is a common symbol, then
  1404.          we are explicitly letting it override a weak symbol or
  1405.          function in a dynamic object, and we don't want to warn about
  1406.          a type change.  If the old symbol is a defined symbol, a type
  1407.          change warning may still be appropriate.  */
  1408.  
  1409.       if (h->root.type == bfd_link_hash_common)
  1410.         *type_change_ok = TRUE;
  1411.     }
  1412.  
  1413.   /* Handle the special case of an old common symbol merging with a
  1414.      new symbol which looks like a common symbol in a shared object.
  1415.      We change *PSEC and *PVALUE to make the new symbol look like a
  1416.      common symbol, and let _bfd_generic_link_add_one_symbol do the
  1417.      right thing.  */
  1418.  
  1419.   if (newdyncommon
  1420.       && h->root.type == bfd_link_hash_common)
  1421.     {
  1422.       *override = TRUE;
  1423.       newdef = FALSE;
  1424.       newdyncommon = FALSE;
  1425.       *pvalue = sym->st_size;
  1426.       *psec = sec = bed->common_section (oldsec);
  1427.       *size_change_ok = TRUE;
  1428.     }
  1429.  
  1430.   /* Skip weak definitions of symbols that are already defined.  */
  1431.   if (newdef && olddef && newweak)
  1432.     {
  1433.       /* Don't skip new non-IR weak syms.  */
  1434.       if (!(oldbfd != NULL
  1435.             && (oldbfd->flags & BFD_PLUGIN) != 0
  1436.             && (abfd->flags & BFD_PLUGIN) == 0))
  1437.         *skip = TRUE;
  1438.  
  1439.       /* Merge st_other.  If the symbol already has a dynamic index,
  1440.          but visibility says it should not be visible, turn it into a
  1441.          local symbol.  */
  1442.       elf_merge_st_other (abfd, h, sym, newdef, newdyn);
  1443.       if (h->dynindx != -1)
  1444.         switch (ELF_ST_VISIBILITY (h->other))
  1445.           {
  1446.           case STV_INTERNAL:
  1447.           case STV_HIDDEN:
  1448.             (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1449.             break;
  1450.           }
  1451.     }
  1452.  
  1453.   /* If the old symbol is from a dynamic object, and the new symbol is
  1454.      a definition which is not from a dynamic object, then the new
  1455.      symbol overrides the old symbol.  Symbols from regular files
  1456.      always take precedence over symbols from dynamic objects, even if
  1457.      they are defined after the dynamic object in the link.
  1458.  
  1459.      As above, we again permit a common symbol in a regular object to
  1460.      override a definition in a shared object if the shared object
  1461.      symbol is a function or is weak.  */
  1462.  
  1463.   flip = NULL;
  1464.   if (!newdyn
  1465.       && (newdef
  1466.           || (bfd_is_com_section (sec)
  1467.               && (oldweak || oldfunc)))
  1468.       && olddyn
  1469.       && olddef
  1470.       && h->def_dynamic)
  1471.     {
  1472.       /* Change the hash table entry to undefined, and let
  1473.          _bfd_generic_link_add_one_symbol do the right thing with the
  1474.          new definition.  */
  1475.  
  1476.       h->root.type = bfd_link_hash_undefined;
  1477.       h->root.u.undef.abfd = h->root.u.def.section->owner;
  1478.       *size_change_ok = TRUE;
  1479.  
  1480.       olddef = FALSE;
  1481.       olddyncommon = FALSE;
  1482.  
  1483.       /* We again permit a type change when a common symbol may be
  1484.          overriding a function.  */
  1485.  
  1486.       if (bfd_is_com_section (sec))
  1487.         {
  1488.           if (oldfunc)
  1489.             {
  1490.               /* If a common symbol overrides a function, make sure
  1491.                  that it isn't defined dynamically nor has type
  1492.                  function.  */
  1493.               h->def_dynamic = 0;
  1494.               h->type = STT_NOTYPE;
  1495.             }
  1496.           *type_change_ok = TRUE;
  1497.         }
  1498.  
  1499.       if (hi->root.type == bfd_link_hash_indirect)
  1500.         flip = hi;
  1501.       else
  1502.         /* This union may have been set to be non-NULL when this symbol
  1503.            was seen in a dynamic object.  We must force the union to be
  1504.            NULL, so that it is correct for a regular symbol.  */
  1505.         h->verinfo.vertree = NULL;
  1506.     }
  1507.  
  1508.   /* Handle the special case of a new common symbol merging with an
  1509.      old symbol that looks like it might be a common symbol defined in
  1510.      a shared object.  Note that we have already handled the case in
  1511.      which a new common symbol should simply override the definition
  1512.      in the shared library.  */
  1513.  
  1514.   if (! newdyn
  1515.       && bfd_is_com_section (sec)
  1516.       && olddyncommon)
  1517.     {
  1518.       /* It would be best if we could set the hash table entry to a
  1519.          common symbol, but we don't know what to use for the section
  1520.          or the alignment.  */
  1521.       if (! ((*info->callbacks->multiple_common)
  1522.              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
  1523.         return FALSE;
  1524.  
  1525.       /* If the presumed common symbol in the dynamic object is
  1526.          larger, pretend that the new symbol has its size.  */
  1527.  
  1528.       if (h->size > *pvalue)
  1529.         *pvalue = h->size;
  1530.  
  1531.       /* We need to remember the alignment required by the symbol
  1532.          in the dynamic object.  */
  1533.       BFD_ASSERT (pold_alignment);
  1534.       *pold_alignment = h->root.u.def.section->alignment_power;
  1535.  
  1536.       olddef = FALSE;
  1537.       olddyncommon = FALSE;
  1538.  
  1539.       h->root.type = bfd_link_hash_undefined;
  1540.       h->root.u.undef.abfd = h->root.u.def.section->owner;
  1541.  
  1542.       *size_change_ok = TRUE;
  1543.       *type_change_ok = TRUE;
  1544.  
  1545.       if (hi->root.type == bfd_link_hash_indirect)
  1546.         flip = hi;
  1547.       else
  1548.         h->verinfo.vertree = NULL;
  1549.     }
  1550.  
  1551.   if (flip != NULL)
  1552.     {
  1553.       /* Handle the case where we had a versioned symbol in a dynamic
  1554.          library and now find a definition in a normal object.  In this
  1555.          case, we make the versioned symbol point to the normal one.  */
  1556.       flip->root.type = h->root.type;
  1557.       flip->root.u.undef.abfd = h->root.u.undef.abfd;
  1558.       h->root.type = bfd_link_hash_indirect;
  1559.       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
  1560.       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
  1561.       if (h->def_dynamic)
  1562.         {
  1563.           h->def_dynamic = 0;
  1564.           flip->ref_dynamic = 1;
  1565.         }
  1566.     }
  1567.  
  1568.   return TRUE;
  1569. }
  1570.  
  1571. /* This function is called to create an indirect symbol from the
  1572.    default for the symbol with the default version if needed. The
  1573.    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
  1574.    set DYNSYM if the new indirect symbol is dynamic.  */
  1575.  
  1576. static bfd_boolean
  1577. _bfd_elf_add_default_symbol (bfd *abfd,
  1578.                              struct bfd_link_info *info,
  1579.                              struct elf_link_hash_entry *h,
  1580.                              const char *name,
  1581.                              Elf_Internal_Sym *sym,
  1582.                              asection *sec,
  1583.                              bfd_vma value,
  1584.                              bfd **poldbfd,
  1585.                              bfd_boolean *dynsym)
  1586. {
  1587.   bfd_boolean type_change_ok;
  1588.   bfd_boolean size_change_ok;
  1589.   bfd_boolean skip;
  1590.   char *shortname;
  1591.   struct elf_link_hash_entry *hi;
  1592.   struct bfd_link_hash_entry *bh;
  1593.   const struct elf_backend_data *bed;
  1594.   bfd_boolean collect;
  1595.   bfd_boolean dynamic;
  1596.   bfd_boolean override;
  1597.   char *p;
  1598.   size_t len, shortlen;
  1599.   asection *tmp_sec;
  1600.  
  1601.   /* If this symbol has a version, and it is the default version, we
  1602.      create an indirect symbol from the default name to the fully
  1603.      decorated name.  This will cause external references which do not
  1604.      specify a version to be bound to this version of the symbol.  */
  1605.   p = strchr (name, ELF_VER_CHR);
  1606.   if (p == NULL || p[1] != ELF_VER_CHR)
  1607.     return TRUE;
  1608.  
  1609.   bed = get_elf_backend_data (abfd);
  1610.   collect = bed->collect;
  1611.   dynamic = (abfd->flags & DYNAMIC) != 0;
  1612.  
  1613.   shortlen = p - name;
  1614.   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
  1615.   if (shortname == NULL)
  1616.     return FALSE;
  1617.   memcpy (shortname, name, shortlen);
  1618.   shortname[shortlen] = '\0';
  1619.  
  1620.   /* We are going to create a new symbol.  Merge it with any existing
  1621.      symbol with this name.  For the purposes of the merge, act as
  1622.      though we were defining the symbol we just defined, although we
  1623.      actually going to define an indirect symbol.  */
  1624.   type_change_ok = FALSE;
  1625.   size_change_ok = FALSE;
  1626.   tmp_sec = sec;
  1627.   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1628.                               &hi, poldbfd, NULL, NULL, &skip, &override,
  1629.                               &type_change_ok, &size_change_ok))
  1630.     return FALSE;
  1631.  
  1632.   if (skip)
  1633.     goto nondefault;
  1634.  
  1635.   if (! override)
  1636.     {
  1637.       bh = &hi->root;
  1638.       if (! (_bfd_generic_link_add_one_symbol
  1639.              (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
  1640.               0, name, FALSE, collect, &bh)))
  1641.         return FALSE;
  1642.       hi = (struct elf_link_hash_entry *) bh;
  1643.     }
  1644.   else
  1645.     {
  1646.       /* In this case the symbol named SHORTNAME is overriding the
  1647.          indirect symbol we want to add.  We were planning on making
  1648.          SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
  1649.          is the name without a version.  NAME is the fully versioned
  1650.          name, and it is the default version.
  1651.  
  1652.          Overriding means that we already saw a definition for the
  1653.          symbol SHORTNAME in a regular object, and it is overriding
  1654.          the symbol defined in the dynamic object.
  1655.  
  1656.          When this happens, we actually want to change NAME, the
  1657.          symbol we just added, to refer to SHORTNAME.  This will cause
  1658.          references to NAME in the shared object to become references
  1659.          to SHORTNAME in the regular object.  This is what we expect
  1660.          when we override a function in a shared object: that the
  1661.          references in the shared object will be mapped to the
  1662.          definition in the regular object.  */
  1663.  
  1664.       while (hi->root.type == bfd_link_hash_indirect
  1665.              || hi->root.type == bfd_link_hash_warning)
  1666.         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1667.  
  1668.       h->root.type = bfd_link_hash_indirect;
  1669.       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1670.       if (h->def_dynamic)
  1671.         {
  1672.           h->def_dynamic = 0;
  1673.           hi->ref_dynamic = 1;
  1674.           if (hi->ref_regular
  1675.               || hi->def_regular)
  1676.             {
  1677.               if (! bfd_elf_link_record_dynamic_symbol (info, hi))
  1678.                 return FALSE;
  1679.             }
  1680.         }
  1681.  
  1682.       /* Now set HI to H, so that the following code will set the
  1683.          other fields correctly.  */
  1684.       hi = h;
  1685.     }
  1686.  
  1687.   /* Check if HI is a warning symbol.  */
  1688.   if (hi->root.type == bfd_link_hash_warning)
  1689.     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1690.  
  1691.   /* If there is a duplicate definition somewhere, then HI may not
  1692.      point to an indirect symbol.  We will have reported an error to
  1693.      the user in that case.  */
  1694.  
  1695.   if (hi->root.type == bfd_link_hash_indirect)
  1696.     {
  1697.       struct elf_link_hash_entry *ht;
  1698.  
  1699.       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1700.       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
  1701.  
  1702.       /* See if the new flags lead us to realize that the symbol must
  1703.          be dynamic.  */
  1704.       if (! *dynsym)
  1705.         {
  1706.           if (! dynamic)
  1707.             {
  1708.               if (! info->executable
  1709.                   || hi->def_dynamic
  1710.                   || hi->ref_dynamic)
  1711.                 *dynsym = TRUE;
  1712.             }
  1713.           else
  1714.             {
  1715.               if (hi->ref_regular)
  1716.                 *dynsym = TRUE;
  1717.             }
  1718.         }
  1719.     }
  1720.  
  1721.   /* We also need to define an indirection from the nondefault version
  1722.      of the symbol.  */
  1723.  
  1724. nondefault:
  1725.   len = strlen (name);
  1726.   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
  1727.   if (shortname == NULL)
  1728.     return FALSE;
  1729.   memcpy (shortname, name, shortlen);
  1730.   memcpy (shortname + shortlen, p + 1, len - shortlen);
  1731.  
  1732.   /* Once again, merge with any existing symbol.  */
  1733.   type_change_ok = FALSE;
  1734.   size_change_ok = FALSE;
  1735.   tmp_sec = sec;
  1736.   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1737.                               &hi, NULL, NULL, NULL, &skip, &override,
  1738.                               &type_change_ok, &size_change_ok))
  1739.     return FALSE;
  1740.  
  1741.   if (skip)
  1742.     return TRUE;
  1743.  
  1744.   if (override)
  1745.     {
  1746.       /* Here SHORTNAME is a versioned name, so we don't expect to see
  1747.          the type of override we do in the case above unless it is
  1748.          overridden by a versioned definition.  */
  1749.       if (hi->root.type != bfd_link_hash_defined
  1750.           && hi->root.type != bfd_link_hash_defweak)
  1751.         (*_bfd_error_handler)
  1752.           (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
  1753.            abfd, shortname);
  1754.     }
  1755.   else
  1756.     {
  1757.       bh = &hi->root;
  1758.       if (! (_bfd_generic_link_add_one_symbol
  1759.              (info, abfd, shortname, BSF_INDIRECT,
  1760.               bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
  1761.         return FALSE;
  1762.       hi = (struct elf_link_hash_entry *) bh;
  1763.  
  1764.       /* If there is a duplicate definition somewhere, then HI may not
  1765.          point to an indirect symbol.  We will have reported an error
  1766.          to the user in that case.  */
  1767.  
  1768.       if (hi->root.type == bfd_link_hash_indirect)
  1769.         {
  1770.           (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  1771.  
  1772.           /* See if the new flags lead us to realize that the symbol
  1773.              must be dynamic.  */
  1774.           if (! *dynsym)
  1775.             {
  1776.               if (! dynamic)
  1777.                 {
  1778.                   if (! info->executable
  1779.                       || hi->ref_dynamic)
  1780.                     *dynsym = TRUE;
  1781.                 }
  1782.               else
  1783.                 {
  1784.                   if (hi->ref_regular)
  1785.                     *dynsym = TRUE;
  1786.                 }
  1787.             }
  1788.         }
  1789.     }
  1790.  
  1791.   return TRUE;
  1792. }
  1793. /* This routine is used to export all defined symbols into the dynamic
  1794.    symbol table.  It is called via elf_link_hash_traverse.  */
  1795.  
  1796. static bfd_boolean
  1797. _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
  1798. {
  1799.   struct elf_info_failed *eif = (struct elf_info_failed *) data;
  1800.  
  1801.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  1802.   if (h->root.type == bfd_link_hash_indirect)
  1803.     return TRUE;
  1804.  
  1805.   /* Ignore this if we won't export it.  */
  1806.   if (!eif->info->export_dynamic && !h->dynamic)
  1807.     return TRUE;
  1808.  
  1809.   if (h->dynindx == -1
  1810.       && (h->def_regular || h->ref_regular)
  1811.       && ! bfd_hide_sym_by_version (eif->info->version_info,
  1812.                                     h->root.root.string))
  1813.     {
  1814.       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  1815.         {
  1816.           eif->failed = TRUE;
  1817.           return FALSE;
  1818.         }
  1819.     }
  1820.  
  1821.   return TRUE;
  1822. }
  1823. /* Look through the symbols which are defined in other shared
  1824.    libraries and referenced here.  Update the list of version
  1825.    dependencies.  This will be put into the .gnu.version_r section.
  1826.    This function is called via elf_link_hash_traverse.  */
  1827.  
  1828. static bfd_boolean
  1829. _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
  1830.                                          void *data)
  1831. {
  1832.   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
  1833.   Elf_Internal_Verneed *t;
  1834.   Elf_Internal_Vernaux *a;
  1835.   bfd_size_type amt;
  1836.  
  1837.   /* We only care about symbols defined in shared objects with version
  1838.      information.  */
  1839.   if (!h->def_dynamic
  1840.       || h->def_regular
  1841.       || h->dynindx == -1
  1842.       || h->verinfo.verdef == NULL)
  1843.     return TRUE;
  1844.  
  1845.   /* See if we already know about this version.  */
  1846.   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
  1847.        t != NULL;
  1848.        t = t->vn_nextref)
  1849.     {
  1850.       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
  1851.         continue;
  1852.  
  1853.       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  1854.         if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
  1855.           return TRUE;
  1856.  
  1857.       break;
  1858.     }
  1859.  
  1860.   /* This is a new version.  Add it to tree we are building.  */
  1861.  
  1862.   if (t == NULL)
  1863.     {
  1864.       amt = sizeof *t;
  1865.       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
  1866.       if (t == NULL)
  1867.         {
  1868.           rinfo->failed = TRUE;
  1869.           return FALSE;
  1870.         }
  1871.  
  1872.       t->vn_bfd = h->verinfo.verdef->vd_bfd;
  1873.       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
  1874.       elf_tdata (rinfo->info->output_bfd)->verref = t;
  1875.     }
  1876.  
  1877.   amt = sizeof *a;
  1878.   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
  1879.   if (a == NULL)
  1880.     {
  1881.       rinfo->failed = TRUE;
  1882.       return FALSE;
  1883.     }
  1884.  
  1885.   /* Note that we are copying a string pointer here, and testing it
  1886.      above.  If bfd_elf_string_from_elf_section is ever changed to
  1887.      discard the string data when low in memory, this will have to be
  1888.      fixed.  */
  1889.   a->vna_nodename = h->verinfo.verdef->vd_nodename;
  1890.  
  1891.   a->vna_flags = h->verinfo.verdef->vd_flags;
  1892.   a->vna_nextptr = t->vn_auxptr;
  1893.  
  1894.   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
  1895.   ++rinfo->vers;
  1896.  
  1897.   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
  1898.  
  1899.   t->vn_auxptr = a;
  1900.  
  1901.   return TRUE;
  1902. }
  1903.  
  1904. /* Figure out appropriate versions for all the symbols.  We may not
  1905.    have the version number script until we have read all of the input
  1906.    files, so until that point we don't know which symbols should be
  1907.    local.  This function is called via elf_link_hash_traverse.  */
  1908.  
  1909. static bfd_boolean
  1910. _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
  1911. {
  1912.   struct elf_info_failed *sinfo;
  1913.   struct bfd_link_info *info;
  1914.   const struct elf_backend_data *bed;
  1915.   struct elf_info_failed eif;
  1916.   char *p;
  1917.   bfd_size_type amt;
  1918.  
  1919.   sinfo = (struct elf_info_failed *) data;
  1920.   info = sinfo->info;
  1921.  
  1922.   /* Fix the symbol flags.  */
  1923.   eif.failed = FALSE;
  1924.   eif.info = info;
  1925.   if (! _bfd_elf_fix_symbol_flags (h, &eif))
  1926.     {
  1927.       if (eif.failed)
  1928.         sinfo->failed = TRUE;
  1929.       return FALSE;
  1930.     }
  1931.  
  1932.   /* We only need version numbers for symbols defined in regular
  1933.      objects.  */
  1934.   if (!h->def_regular)
  1935.     return TRUE;
  1936.  
  1937.   bed = get_elf_backend_data (info->output_bfd);
  1938.   p = strchr (h->root.root.string, ELF_VER_CHR);
  1939.   if (p != NULL && h->verinfo.vertree == NULL)
  1940.     {
  1941.       struct bfd_elf_version_tree *t;
  1942.       bfd_boolean hidden;
  1943.  
  1944.       hidden = TRUE;
  1945.  
  1946.       /* There are two consecutive ELF_VER_CHR characters if this is
  1947.          not a hidden symbol.  */
  1948.       ++p;
  1949.       if (*p == ELF_VER_CHR)
  1950.         {
  1951.           hidden = FALSE;
  1952.           ++p;
  1953.         }
  1954.  
  1955.       /* If there is no version string, we can just return out.  */
  1956.       if (*p == '\0')
  1957.         {
  1958.           if (hidden)
  1959.             h->hidden = 1;
  1960.           return TRUE;
  1961.         }
  1962.  
  1963.       /* Look for the version.  If we find it, it is no longer weak.  */
  1964.       for (t = sinfo->info->version_info; t != NULL; t = t->next)
  1965.         {
  1966.           if (strcmp (t->name, p) == 0)
  1967.             {
  1968.               size_t len;
  1969.               char *alc;
  1970.               struct bfd_elf_version_expr *d;
  1971.  
  1972.               len = p - h->root.root.string;
  1973.               alc = (char *) bfd_malloc (len);
  1974.               if (alc == NULL)
  1975.                 {
  1976.                   sinfo->failed = TRUE;
  1977.                   return FALSE;
  1978.                 }
  1979.               memcpy (alc, h->root.root.string, len - 1);
  1980.               alc[len - 1] = '\0';
  1981.               if (alc[len - 2] == ELF_VER_CHR)
  1982.                 alc[len - 2] = '\0';
  1983.  
  1984.               h->verinfo.vertree = t;
  1985.               t->used = TRUE;
  1986.               d = NULL;
  1987.  
  1988.               if (t->globals.list != NULL)
  1989.                 d = (*t->match) (&t->globals, NULL, alc);
  1990.  
  1991.               /* See if there is anything to force this symbol to
  1992.                  local scope.  */
  1993.               if (d == NULL && t->locals.list != NULL)
  1994.                 {
  1995.                   d = (*t->match) (&t->locals, NULL, alc);
  1996.                   if (d != NULL
  1997.                       && h->dynindx != -1
  1998.                       && ! info->export_dynamic)
  1999.                     (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  2000.                 }
  2001.  
  2002.               free (alc);
  2003.               break;
  2004.             }
  2005.         }
  2006.  
  2007.       /* If we are building an application, we need to create a
  2008.          version node for this version.  */
  2009.       if (t == NULL && info->executable)
  2010.         {
  2011.           struct bfd_elf_version_tree **pp;
  2012.           int version_index;
  2013.  
  2014.           /* If we aren't going to export this symbol, we don't need
  2015.              to worry about it.  */
  2016.           if (h->dynindx == -1)
  2017.             return TRUE;
  2018.  
  2019.           amt = sizeof *t;
  2020.           t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
  2021.           if (t == NULL)
  2022.             {
  2023.               sinfo->failed = TRUE;
  2024.               return FALSE;
  2025.             }
  2026.  
  2027.           t->name = p;
  2028.           t->name_indx = (unsigned int) -1;
  2029.           t->used = TRUE;
  2030.  
  2031.           version_index = 1;
  2032.           /* Don't count anonymous version tag.  */
  2033.           if (sinfo->info->version_info != NULL
  2034.               && sinfo->info->version_info->vernum == 0)
  2035.             version_index = 0;
  2036.           for (pp = &sinfo->info->version_info;
  2037.                *pp != NULL;
  2038.                pp = &(*pp)->next)
  2039.             ++version_index;
  2040.           t->vernum = version_index;
  2041.  
  2042.           *pp = t;
  2043.  
  2044.           h->verinfo.vertree = t;
  2045.         }
  2046.       else if (t == NULL)
  2047.         {
  2048.           /* We could not find the version for a symbol when
  2049.              generating a shared archive.  Return an error.  */
  2050.           (*_bfd_error_handler)
  2051.             (_("%B: version node not found for symbol %s"),
  2052.              info->output_bfd, h->root.root.string);
  2053.           bfd_set_error (bfd_error_bad_value);
  2054.           sinfo->failed = TRUE;
  2055.           return FALSE;
  2056.         }
  2057.  
  2058.       if (hidden)
  2059.         h->hidden = 1;
  2060.     }
  2061.  
  2062.   /* If we don't have a version for this symbol, see if we can find
  2063.      something.  */
  2064.   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
  2065.     {
  2066.       bfd_boolean hide;
  2067.  
  2068.       h->verinfo.vertree
  2069.         = bfd_find_version_for_sym (sinfo->info->version_info,
  2070.                                     h->root.root.string, &hide);
  2071.       if (h->verinfo.vertree != NULL && hide)
  2072.         (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  2073.     }
  2074.  
  2075.   return TRUE;
  2076. }
  2077. /* Read and swap the relocs from the section indicated by SHDR.  This
  2078.    may be either a REL or a RELA section.  The relocations are
  2079.    translated into RELA relocations and stored in INTERNAL_RELOCS,
  2080.    which should have already been allocated to contain enough space.
  2081.    The EXTERNAL_RELOCS are a buffer where the external form of the
  2082.    relocations should be stored.
  2083.  
  2084.    Returns FALSE if something goes wrong.  */
  2085.  
  2086. static bfd_boolean
  2087. elf_link_read_relocs_from_section (bfd *abfd,
  2088.                                    asection *sec,
  2089.                                    Elf_Internal_Shdr *shdr,
  2090.                                    void *external_relocs,
  2091.                                    Elf_Internal_Rela *internal_relocs)
  2092. {
  2093.   const struct elf_backend_data *bed;
  2094.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  2095.   const bfd_byte *erela;
  2096.   const bfd_byte *erelaend;
  2097.   Elf_Internal_Rela *irela;
  2098.   Elf_Internal_Shdr *symtab_hdr;
  2099.   size_t nsyms;
  2100.  
  2101.   /* Position ourselves at the start of the section.  */
  2102.   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
  2103.     return FALSE;
  2104.  
  2105.   /* Read the relocations.  */
  2106.   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
  2107.     return FALSE;
  2108.  
  2109.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2110.   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
  2111.  
  2112.   bed = get_elf_backend_data (abfd);
  2113.  
  2114.   /* Convert the external relocations to the internal format.  */
  2115.   if (shdr->sh_entsize == bed->s->sizeof_rel)
  2116.     swap_in = bed->s->swap_reloc_in;
  2117.   else if (shdr->sh_entsize == bed->s->sizeof_rela)
  2118.     swap_in = bed->s->swap_reloca_in;
  2119.   else
  2120.     {
  2121.       bfd_set_error (bfd_error_wrong_format);
  2122.       return FALSE;
  2123.     }
  2124.  
  2125.   erela = (const bfd_byte *) external_relocs;
  2126.   erelaend = erela + shdr->sh_size;
  2127.   irela = internal_relocs;
  2128.   while (erela < erelaend)
  2129.     {
  2130.       bfd_vma r_symndx;
  2131.  
  2132.       (*swap_in) (abfd, erela, irela);
  2133.       r_symndx = ELF32_R_SYM (irela->r_info);
  2134.       if (bed->s->arch_size == 64)
  2135.         r_symndx >>= 24;
  2136.       if (nsyms > 0)
  2137.         {
  2138.           if ((size_t) r_symndx >= nsyms)
  2139.             {
  2140.               (*_bfd_error_handler)
  2141.                 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
  2142.                    " for offset 0x%lx in section `%A'"),
  2143.                  abfd, sec,
  2144.                  (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
  2145.               bfd_set_error (bfd_error_bad_value);
  2146.               return FALSE;
  2147.             }
  2148.         }
  2149.       else if (r_symndx != STN_UNDEF)
  2150.         {
  2151.           (*_bfd_error_handler)
  2152.             (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
  2153.                " when the object file has no symbol table"),
  2154.              abfd, sec,
  2155.              (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
  2156.           bfd_set_error (bfd_error_bad_value);
  2157.           return FALSE;
  2158.         }
  2159.       irela += bed->s->int_rels_per_ext_rel;
  2160.       erela += shdr->sh_entsize;
  2161.     }
  2162.  
  2163.   return TRUE;
  2164. }
  2165.  
  2166. /* Read and swap the relocs for a section O.  They may have been
  2167.    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
  2168.    not NULL, they are used as buffers to read into.  They are known to
  2169.    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
  2170.    the return value is allocated using either malloc or bfd_alloc,
  2171.    according to the KEEP_MEMORY argument.  If O has two relocation
  2172.    sections (both REL and RELA relocations), then the REL_HDR
  2173.    relocations will appear first in INTERNAL_RELOCS, followed by the
  2174.    RELA_HDR relocations.  */
  2175.  
  2176. Elf_Internal_Rela *
  2177. _bfd_elf_link_read_relocs (bfd *abfd,
  2178.                            asection *o,
  2179.                            void *external_relocs,
  2180.                            Elf_Internal_Rela *internal_relocs,
  2181.                            bfd_boolean keep_memory)
  2182. {
  2183.   void *alloc1 = NULL;
  2184.   Elf_Internal_Rela *alloc2 = NULL;
  2185.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2186.   struct bfd_elf_section_data *esdo = elf_section_data (o);
  2187.   Elf_Internal_Rela *internal_rela_relocs;
  2188.  
  2189.   if (esdo->relocs != NULL)
  2190.     return esdo->relocs;
  2191.  
  2192.   if (o->reloc_count == 0)
  2193.     return NULL;
  2194.  
  2195.   if (internal_relocs == NULL)
  2196.     {
  2197.       bfd_size_type size;
  2198.  
  2199.       size = o->reloc_count;
  2200.       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
  2201.       if (keep_memory)
  2202.         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
  2203.       else
  2204.         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
  2205.       if (internal_relocs == NULL)
  2206.         goto error_return;
  2207.     }
  2208.  
  2209.   if (external_relocs == NULL)
  2210.     {
  2211.       bfd_size_type size = 0;
  2212.  
  2213.       if (esdo->rel.hdr)
  2214.         size += esdo->rel.hdr->sh_size;
  2215.       if (esdo->rela.hdr)
  2216.         size += esdo->rela.hdr->sh_size;
  2217.  
  2218.       alloc1 = bfd_malloc (size);
  2219.       if (alloc1 == NULL)
  2220.         goto error_return;
  2221.       external_relocs = alloc1;
  2222.     }
  2223.  
  2224.   internal_rela_relocs = internal_relocs;
  2225.   if (esdo->rel.hdr)
  2226.     {
  2227.       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
  2228.                                               external_relocs,
  2229.                                               internal_relocs))
  2230.         goto error_return;
  2231.       external_relocs = (((bfd_byte *) external_relocs)
  2232.                          + esdo->rel.hdr->sh_size);
  2233.       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
  2234.                                * bed->s->int_rels_per_ext_rel);
  2235.     }
  2236.  
  2237.   if (esdo->rela.hdr
  2238.       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
  2239.                                               external_relocs,
  2240.                                               internal_rela_relocs)))
  2241.     goto error_return;
  2242.  
  2243.   /* Cache the results for next time, if we can.  */
  2244.   if (keep_memory)
  2245.     esdo->relocs = internal_relocs;
  2246.  
  2247.   if (alloc1 != NULL)
  2248.     free (alloc1);
  2249.  
  2250.   /* Don't free alloc2, since if it was allocated we are passing it
  2251.      back (under the name of internal_relocs).  */
  2252.  
  2253.   return internal_relocs;
  2254.  
  2255.  error_return:
  2256.   if (alloc1 != NULL)
  2257.     free (alloc1);
  2258.   if (alloc2 != NULL)
  2259.     {
  2260.       if (keep_memory)
  2261.         bfd_release (abfd, alloc2);
  2262.       else
  2263.         free (alloc2);
  2264.     }
  2265.   return NULL;
  2266. }
  2267.  
  2268. /* Compute the size of, and allocate space for, REL_HDR which is the
  2269.    section header for a section containing relocations for O.  */
  2270.  
  2271. static bfd_boolean
  2272. _bfd_elf_link_size_reloc_section (bfd *abfd,
  2273.                                   struct bfd_elf_section_reloc_data *reldata)
  2274. {
  2275.   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
  2276.  
  2277.   /* That allows us to calculate the size of the section.  */
  2278.   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
  2279.  
  2280.   /* The contents field must last into write_object_contents, so we
  2281.      allocate it with bfd_alloc rather than malloc.  Also since we
  2282.      cannot be sure that the contents will actually be filled in,
  2283.      we zero the allocated space.  */
  2284.   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
  2285.   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
  2286.     return FALSE;
  2287.  
  2288.   if (reldata->hashes == NULL && reldata->count)
  2289.     {
  2290.       struct elf_link_hash_entry **p;
  2291.  
  2292.       p = (struct elf_link_hash_entry **)
  2293.           bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
  2294.       if (p == NULL)
  2295.         return FALSE;
  2296.  
  2297.       reldata->hashes = p;
  2298.     }
  2299.  
  2300.   return TRUE;
  2301. }
  2302.  
  2303. /* Copy the relocations indicated by the INTERNAL_RELOCS (which
  2304.    originated from the section given by INPUT_REL_HDR) to the
  2305.    OUTPUT_BFD.  */
  2306.  
  2307. bfd_boolean
  2308. _bfd_elf_link_output_relocs (bfd *output_bfd,
  2309.                              asection *input_section,
  2310.                              Elf_Internal_Shdr *input_rel_hdr,
  2311.                              Elf_Internal_Rela *internal_relocs,
  2312.                              struct elf_link_hash_entry **rel_hash
  2313.                                ATTRIBUTE_UNUSED)
  2314. {
  2315.   Elf_Internal_Rela *irela;
  2316.   Elf_Internal_Rela *irelaend;
  2317.   bfd_byte *erel;
  2318.   struct bfd_elf_section_reloc_data *output_reldata;
  2319.   asection *output_section;
  2320.   const struct elf_backend_data *bed;
  2321.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  2322.   struct bfd_elf_section_data *esdo;
  2323.  
  2324.   output_section = input_section->output_section;
  2325.  
  2326.   bed = get_elf_backend_data (output_bfd);
  2327.   esdo = elf_section_data (output_section);
  2328.   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2329.     {
  2330.       output_reldata = &esdo->rel;
  2331.       swap_out = bed->s->swap_reloc_out;
  2332.     }
  2333.   else if (esdo->rela.hdr
  2334.            && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2335.     {
  2336.       output_reldata = &esdo->rela;
  2337.       swap_out = bed->s->swap_reloca_out;
  2338.     }
  2339.   else
  2340.     {
  2341.       (*_bfd_error_handler)
  2342.         (_("%B: relocation size mismatch in %B section %A"),
  2343.          output_bfd, input_section->owner, input_section);
  2344.       bfd_set_error (bfd_error_wrong_format);
  2345.       return FALSE;
  2346.     }
  2347.  
  2348.   erel = output_reldata->hdr->contents;
  2349.   erel += output_reldata->count * input_rel_hdr->sh_entsize;
  2350.   irela = internal_relocs;
  2351.   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
  2352.                       * bed->s->int_rels_per_ext_rel);
  2353.   while (irela < irelaend)
  2354.     {
  2355.       (*swap_out) (output_bfd, irela, erel);
  2356.       irela += bed->s->int_rels_per_ext_rel;
  2357.       erel += input_rel_hdr->sh_entsize;
  2358.     }
  2359.  
  2360.   /* Bump the counter, so that we know where to add the next set of
  2361.      relocations.  */
  2362.   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
  2363.  
  2364.   return TRUE;
  2365. }
  2366. /* Make weak undefined symbols in PIE dynamic.  */
  2367.  
  2368. bfd_boolean
  2369. _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
  2370.                                  struct elf_link_hash_entry *h)
  2371. {
  2372.   if (info->pie
  2373.       && h->dynindx == -1
  2374.       && h->root.type == bfd_link_hash_undefweak)
  2375.     return bfd_elf_link_record_dynamic_symbol (info, h);
  2376.  
  2377.   return TRUE;
  2378. }
  2379.  
  2380. /* Fix up the flags for a symbol.  This handles various cases which
  2381.    can only be fixed after all the input files are seen.  This is
  2382.    currently called by both adjust_dynamic_symbol and
  2383.    assign_sym_version, which is unnecessary but perhaps more robust in
  2384.    the face of future changes.  */
  2385.  
  2386. static bfd_boolean
  2387. _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
  2388.                            struct elf_info_failed *eif)
  2389. {
  2390.   const struct elf_backend_data *bed;
  2391.  
  2392.   /* If this symbol was mentioned in a non-ELF file, try to set
  2393.      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
  2394.      permit a non-ELF file to correctly refer to a symbol defined in
  2395.      an ELF dynamic object.  */
  2396.   if (h->non_elf)
  2397.     {
  2398.       while (h->root.type == bfd_link_hash_indirect)
  2399.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2400.  
  2401.       if (h->root.type != bfd_link_hash_defined
  2402.           && h->root.type != bfd_link_hash_defweak)
  2403.         {
  2404.           h->ref_regular = 1;
  2405.           h->ref_regular_nonweak = 1;
  2406.         }
  2407.       else
  2408.         {
  2409.           if (h->root.u.def.section->owner != NULL
  2410.               && (bfd_get_flavour (h->root.u.def.section->owner)
  2411.                   == bfd_target_elf_flavour))
  2412.             {
  2413.               h->ref_regular = 1;
  2414.               h->ref_regular_nonweak = 1;
  2415.             }
  2416.           else
  2417.             h->def_regular = 1;
  2418.         }
  2419.  
  2420.       if (h->dynindx == -1
  2421.           && (h->def_dynamic
  2422.               || h->ref_dynamic))
  2423.         {
  2424.           if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  2425.             {
  2426.               eif->failed = TRUE;
  2427.               return FALSE;
  2428.             }
  2429.         }
  2430.     }
  2431.   else
  2432.     {
  2433.       /* Unfortunately, NON_ELF is only correct if the symbol
  2434.          was first seen in a non-ELF file.  Fortunately, if the symbol
  2435.          was first seen in an ELF file, we're probably OK unless the
  2436.          symbol was defined in a non-ELF file.  Catch that case here.
  2437.          FIXME: We're still in trouble if the symbol was first seen in
  2438.          a dynamic object, and then later in a non-ELF regular object.  */
  2439.       if ((h->root.type == bfd_link_hash_defined
  2440.            || h->root.type == bfd_link_hash_defweak)
  2441.           && !h->def_regular
  2442.           && (h->root.u.def.section->owner != NULL
  2443.               ? (bfd_get_flavour (h->root.u.def.section->owner)
  2444.                  != bfd_target_elf_flavour)
  2445.               : (bfd_is_abs_section (h->root.u.def.section)
  2446.                  && !h->def_dynamic)))
  2447.         h->def_regular = 1;
  2448.     }
  2449.  
  2450.   /* Backend specific symbol fixup.  */
  2451.   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
  2452.   if (bed->elf_backend_fixup_symbol
  2453.       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
  2454.     return FALSE;
  2455.  
  2456.   /* If this is a final link, and the symbol was defined as a common
  2457.      symbol in a regular object file, and there was no definition in
  2458.      any dynamic object, then the linker will have allocated space for
  2459.      the symbol in a common section but the DEF_REGULAR
  2460.      flag will not have been set.  */
  2461.   if (h->root.type == bfd_link_hash_defined
  2462.       && !h->def_regular
  2463.       && h->ref_regular
  2464.       && !h->def_dynamic
  2465.       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
  2466.     h->def_regular = 1;
  2467.  
  2468.   /* If -Bsymbolic was used (which means to bind references to global
  2469.      symbols to the definition within the shared object), and this
  2470.      symbol was defined in a regular object, then it actually doesn't
  2471.      need a PLT entry.  Likewise, if the symbol has non-default
  2472.      visibility.  If the symbol has hidden or internal visibility, we
  2473.      will force it local.  */
  2474.   if (h->needs_plt
  2475.       && eif->info->shared
  2476.       && is_elf_hash_table (eif->info->hash)
  2477.       && (SYMBOLIC_BIND (eif->info, h)
  2478.           || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2479.       && h->def_regular)
  2480.     {
  2481.       bfd_boolean force_local;
  2482.  
  2483.       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
  2484.                      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
  2485.       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
  2486.     }
  2487.  
  2488.   /* If a weak undefined symbol has non-default visibility, we also
  2489.      hide it from the dynamic linker.  */
  2490.   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2491.       && h->root.type == bfd_link_hash_undefweak)
  2492.     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
  2493.  
  2494.   /* If this is a weak defined symbol in a dynamic object, and we know
  2495.      the real definition in the dynamic object, copy interesting flags
  2496.      over to the real definition.  */
  2497.   if (h->u.weakdef != NULL)
  2498.     {
  2499.       /* If the real definition is defined by a regular object file,
  2500.          don't do anything special.  See the longer description in
  2501.          _bfd_elf_adjust_dynamic_symbol, below.  */
  2502.       if (h->u.weakdef->def_regular)
  2503.         h->u.weakdef = NULL;
  2504.       else
  2505.         {
  2506.           struct elf_link_hash_entry *weakdef = h->u.weakdef;
  2507.  
  2508.           while (h->root.type == bfd_link_hash_indirect)
  2509.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2510.  
  2511.           BFD_ASSERT (h->root.type == bfd_link_hash_defined
  2512.                       || h->root.type == bfd_link_hash_defweak);
  2513.           BFD_ASSERT (weakdef->def_dynamic);
  2514.           BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
  2515.                       || weakdef->root.type == bfd_link_hash_defweak);
  2516.           (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
  2517.         }
  2518.     }
  2519.  
  2520.   return TRUE;
  2521. }
  2522.  
  2523. /* Make the backend pick a good value for a dynamic symbol.  This is
  2524.    called via elf_link_hash_traverse, and also calls itself
  2525.    recursively.  */
  2526.  
  2527. static bfd_boolean
  2528. _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
  2529. {
  2530.   struct elf_info_failed *eif = (struct elf_info_failed *) data;
  2531.   bfd *dynobj;
  2532.   const struct elf_backend_data *bed;
  2533.  
  2534.   if (! is_elf_hash_table (eif->info->hash))
  2535.     return FALSE;
  2536.  
  2537.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  2538.   if (h->root.type == bfd_link_hash_indirect)
  2539.     return TRUE;
  2540.  
  2541.   /* Fix the symbol flags.  */
  2542.   if (! _bfd_elf_fix_symbol_flags (h, eif))
  2543.     return FALSE;
  2544.  
  2545.   /* If this symbol does not require a PLT entry, and it is not
  2546.      defined by a dynamic object, or is not referenced by a regular
  2547.      object, ignore it.  We do have to handle a weak defined symbol,
  2548.      even if no regular object refers to it, if we decided to add it
  2549.      to the dynamic symbol table.  FIXME: Do we normally need to worry
  2550.      about symbols which are defined by one dynamic object and
  2551.      referenced by another one?  */
  2552.   if (!h->needs_plt
  2553.       && h->type != STT_GNU_IFUNC
  2554.       && (h->def_regular
  2555.           || !h->def_dynamic
  2556.           || (!h->ref_regular
  2557.               && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
  2558.     {
  2559.       h->plt = elf_hash_table (eif->info)->init_plt_offset;
  2560.       return TRUE;
  2561.     }
  2562.  
  2563.   /* If we've already adjusted this symbol, don't do it again.  This
  2564.      can happen via a recursive call.  */
  2565.   if (h->dynamic_adjusted)
  2566.     return TRUE;
  2567.  
  2568.   /* Don't look at this symbol again.  Note that we must set this
  2569.      after checking the above conditions, because we may look at a
  2570.      symbol once, decide not to do anything, and then get called
  2571.      recursively later after REF_REGULAR is set below.  */
  2572.   h->dynamic_adjusted = 1;
  2573.  
  2574.   /* If this is a weak definition, and we know a real definition, and
  2575.      the real symbol is not itself defined by a regular object file,
  2576.      then get a good value for the real definition.  We handle the
  2577.      real symbol first, for the convenience of the backend routine.
  2578.  
  2579.      Note that there is a confusing case here.  If the real definition
  2580.      is defined by a regular object file, we don't get the real symbol
  2581.      from the dynamic object, but we do get the weak symbol.  If the
  2582.      processor backend uses a COPY reloc, then if some routine in the
  2583.      dynamic object changes the real symbol, we will not see that
  2584.      change in the corresponding weak symbol.  This is the way other
  2585.      ELF linkers work as well, and seems to be a result of the shared
  2586.      library model.
  2587.  
  2588.      I will clarify this issue.  Most SVR4 shared libraries define the
  2589.      variable _timezone and define timezone as a weak synonym.  The
  2590.      tzset call changes _timezone.  If you write
  2591.        extern int timezone;
  2592.        int _timezone = 5;
  2593.        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
  2594.      you might expect that, since timezone is a synonym for _timezone,
  2595.      the same number will print both times.  However, if the processor
  2596.      backend uses a COPY reloc, then actually timezone will be copied
  2597.      into your process image, and, since you define _timezone
  2598.      yourself, _timezone will not.  Thus timezone and _timezone will
  2599.      wind up at different memory locations.  The tzset call will set
  2600.      _timezone, leaving timezone unchanged.  */
  2601.  
  2602.   if (h->u.weakdef != NULL)
  2603.     {
  2604.       /* If we get to this point, there is an implicit reference to
  2605.          H->U.WEAKDEF by a regular object file via the weak symbol H.  */
  2606.       h->u.weakdef->ref_regular = 1;
  2607.  
  2608.       /* Ensure that the backend adjust_dynamic_symbol function sees
  2609.          H->U.WEAKDEF before H by recursively calling ourselves.  */
  2610.       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
  2611.         return FALSE;
  2612.     }
  2613.  
  2614.   /* If a symbol has no type and no size and does not require a PLT
  2615.      entry, then we are probably about to do the wrong thing here: we
  2616.      are probably going to create a COPY reloc for an empty object.
  2617.      This case can arise when a shared object is built with assembly
  2618.      code, and the assembly code fails to set the symbol type.  */
  2619.   if (h->size == 0
  2620.       && h->type == STT_NOTYPE
  2621.       && !h->needs_plt)
  2622.     (*_bfd_error_handler)
  2623.       (_("warning: type and size of dynamic symbol `%s' are not defined"),
  2624.        h->root.root.string);
  2625.  
  2626.   dynobj = elf_hash_table (eif->info)->dynobj;
  2627.   bed = get_elf_backend_data (dynobj);
  2628.  
  2629.   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
  2630.     {
  2631.       eif->failed = TRUE;
  2632.       return FALSE;
  2633.     }
  2634.  
  2635.   return TRUE;
  2636. }
  2637.  
  2638. /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
  2639.    DYNBSS.  */
  2640.  
  2641. bfd_boolean
  2642. _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
  2643.                               asection *dynbss)
  2644. {
  2645.   unsigned int power_of_two;
  2646.   bfd_vma mask;
  2647.   asection *sec = h->root.u.def.section;
  2648.  
  2649.   /* The section aligment of definition is the maximum alignment
  2650.      requirement of symbols defined in the section.  Since we don't
  2651.      know the symbol alignment requirement, we start with the
  2652.      maximum alignment and check low bits of the symbol address
  2653.      for the minimum alignment.  */
  2654.   power_of_two = bfd_get_section_alignment (sec->owner, sec);
  2655.   mask = ((bfd_vma) 1 << power_of_two) - 1;
  2656.   while ((h->root.u.def.value & mask) != 0)
  2657.     {
  2658.        mask >>= 1;
  2659.        --power_of_two;
  2660.     }
  2661.  
  2662.   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
  2663.                                                 dynbss))
  2664.     {
  2665.       /* Adjust the section alignment if needed.  */
  2666.       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
  2667.                                        power_of_two))
  2668.         return FALSE;
  2669.     }
  2670.  
  2671.   /* We make sure that the symbol will be aligned properly.  */
  2672.   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
  2673.  
  2674.   /* Define the symbol as being at this point in DYNBSS.  */
  2675.   h->root.u.def.section = dynbss;
  2676.   h->root.u.def.value = dynbss->size;
  2677.  
  2678.   /* Increment the size of DYNBSS to make room for the symbol.  */
  2679.   dynbss->size += h->size;
  2680.  
  2681.   return TRUE;
  2682. }
  2683.  
  2684. /* Adjust all external symbols pointing into SEC_MERGE sections
  2685.    to reflect the object merging within the sections.  */
  2686.  
  2687. static bfd_boolean
  2688. _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
  2689. {
  2690.   asection *sec;
  2691.  
  2692.   if ((h->root.type == bfd_link_hash_defined
  2693.        || h->root.type == bfd_link_hash_defweak)
  2694.       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
  2695.       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  2696.     {
  2697.       bfd *output_bfd = (bfd *) data;
  2698.  
  2699.       h->root.u.def.value =
  2700.         _bfd_merged_section_offset (output_bfd,
  2701.                                     &h->root.u.def.section,
  2702.                                     elf_section_data (sec)->sec_info,
  2703.                                     h->root.u.def.value);
  2704.     }
  2705.  
  2706.   return TRUE;
  2707. }
  2708.  
  2709. /* Returns false if the symbol referred to by H should be considered
  2710.    to resolve local to the current module, and true if it should be
  2711.    considered to bind dynamically.  */
  2712.  
  2713. bfd_boolean
  2714. _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
  2715.                            struct bfd_link_info *info,
  2716.                            bfd_boolean not_local_protected)
  2717. {
  2718.   bfd_boolean binding_stays_local_p;
  2719.   const struct elf_backend_data *bed;
  2720.   struct elf_link_hash_table *hash_table;
  2721.  
  2722.   if (h == NULL)
  2723.     return FALSE;
  2724.  
  2725.   while (h->root.type == bfd_link_hash_indirect
  2726.          || h->root.type == bfd_link_hash_warning)
  2727.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2728.  
  2729.   /* If it was forced local, then clearly it's not dynamic.  */
  2730.   if (h->dynindx == -1)
  2731.     return FALSE;
  2732.   if (h->forced_local)
  2733.     return FALSE;
  2734.  
  2735.   /* Identify the cases where name binding rules say that a
  2736.      visible symbol resolves locally.  */
  2737.   binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
  2738.  
  2739.   switch (ELF_ST_VISIBILITY (h->other))
  2740.     {
  2741.     case STV_INTERNAL:
  2742.     case STV_HIDDEN:
  2743.       return FALSE;
  2744.  
  2745.     case STV_PROTECTED:
  2746.       hash_table = elf_hash_table (info);
  2747.       if (!is_elf_hash_table (hash_table))
  2748.         return FALSE;
  2749.  
  2750.       bed = get_elf_backend_data (hash_table->dynobj);
  2751.  
  2752.       /* Proper resolution for function pointer equality may require
  2753.          that these symbols perhaps be resolved dynamically, even though
  2754.          we should be resolving them to the current module.  */
  2755.       if (!not_local_protected || !bed->is_function_type (h->type))
  2756.         binding_stays_local_p = TRUE;
  2757.       break;
  2758.  
  2759.     default:
  2760.       break;
  2761.     }
  2762.  
  2763.   /* If it isn't defined locally, then clearly it's dynamic.  */
  2764.   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  2765.     return TRUE;
  2766.  
  2767.   /* Otherwise, the symbol is dynamic if binding rules don't tell
  2768.      us that it remains local.  */
  2769.   return !binding_stays_local_p;
  2770. }
  2771.  
  2772. /* Return true if the symbol referred to by H should be considered
  2773.    to resolve local to the current module, and false otherwise.  Differs
  2774.    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
  2775.    undefined symbols.  The two functions are virtually identical except
  2776.    for the place where forced_local and dynindx == -1 are tested.  If
  2777.    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
  2778.    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
  2779.    the symbol is local only for defined symbols.
  2780.    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
  2781.    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
  2782.    treatment of undefined weak symbols.  For those that do not make
  2783.    undefined weak symbols dynamic, both functions may return false.  */
  2784.  
  2785. bfd_boolean
  2786. _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
  2787.                               struct bfd_link_info *info,
  2788.                               bfd_boolean local_protected)
  2789. {
  2790.   const struct elf_backend_data *bed;
  2791.   struct elf_link_hash_table *hash_table;
  2792.  
  2793.   /* If it's a local sym, of course we resolve locally.  */
  2794.   if (h == NULL)
  2795.     return TRUE;
  2796.  
  2797.   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
  2798.   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  2799.       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  2800.     return TRUE;
  2801.  
  2802.   /* Common symbols that become definitions don't get the DEF_REGULAR
  2803.      flag set, so test it first, and don't bail out.  */
  2804.   if (ELF_COMMON_DEF_P (h))
  2805.     /* Do nothing.  */;
  2806.   /* If we don't have a definition in a regular file, then we can't
  2807.      resolve locally.  The sym is either undefined or dynamic.  */
  2808.   else if (!h->def_regular)
  2809.     return FALSE;
  2810.  
  2811.   /* Forced local symbols resolve locally.  */
  2812.   if (h->forced_local)
  2813.     return TRUE;
  2814.  
  2815.   /* As do non-dynamic symbols.  */
  2816.   if (h->dynindx == -1)
  2817.     return TRUE;
  2818.  
  2819.   /* At this point, we know the symbol is defined and dynamic.  In an
  2820.      executable it must resolve locally, likewise when building symbolic
  2821.      shared libraries.  */
  2822.   if (info->executable || SYMBOLIC_BIND (info, h))
  2823.     return TRUE;
  2824.  
  2825.   /* Now deal with defined dynamic symbols in shared libraries.  Ones
  2826.      with default visibility might not resolve locally.  */
  2827.   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2828.     return FALSE;
  2829.  
  2830.   hash_table = elf_hash_table (info);
  2831.   if (!is_elf_hash_table (hash_table))
  2832.     return TRUE;
  2833.  
  2834.   bed = get_elf_backend_data (hash_table->dynobj);
  2835.  
  2836.   /* STV_PROTECTED non-function symbols are local.  */
  2837.   if (!bed->is_function_type (h->type))
  2838.     return TRUE;
  2839.  
  2840.   /* Function pointer equality tests may require that STV_PROTECTED
  2841.      symbols be treated as dynamic symbols.  If the address of a
  2842.      function not defined in an executable is set to that function's
  2843.      plt entry in the executable, then the address of the function in
  2844.      a shared library must also be the plt entry in the executable.  */
  2845.   return local_protected;
  2846. }
  2847.  
  2848. /* Caches some TLS segment info, and ensures that the TLS segment vma is
  2849.    aligned.  Returns the first TLS output section.  */
  2850.  
  2851. struct bfd_section *
  2852. _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
  2853. {
  2854.   struct bfd_section *sec, *tls;
  2855.   unsigned int align = 0;
  2856.  
  2857.   for (sec = obfd->sections; sec != NULL; sec = sec->next)
  2858.     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
  2859.       break;
  2860.   tls = sec;
  2861.  
  2862.   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
  2863.     if (sec->alignment_power > align)
  2864.       align = sec->alignment_power;
  2865.  
  2866.   elf_hash_table (info)->tls_sec = tls;
  2867.  
  2868.   /* Ensure the alignment of the first section is the largest alignment,
  2869.      so that the tls segment starts aligned.  */
  2870.   if (tls != NULL)
  2871.     tls->alignment_power = align;
  2872.  
  2873.   return tls;
  2874. }
  2875.  
  2876. /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
  2877. static bfd_boolean
  2878. is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
  2879.                                   Elf_Internal_Sym *sym)
  2880. {
  2881.   const struct elf_backend_data *bed;
  2882.  
  2883.   /* Local symbols do not count, but target specific ones might.  */
  2884.   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
  2885.       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
  2886.     return FALSE;
  2887.  
  2888.   bed = get_elf_backend_data (abfd);
  2889.   /* Function symbols do not count.  */
  2890.   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
  2891.     return FALSE;
  2892.  
  2893.   /* If the section is undefined, then so is the symbol.  */
  2894.   if (sym->st_shndx == SHN_UNDEF)
  2895.     return FALSE;
  2896.  
  2897.   /* If the symbol is defined in the common section, then
  2898.      it is a common definition and so does not count.  */
  2899.   if (bed->common_definition (sym))
  2900.     return FALSE;
  2901.  
  2902.   /* If the symbol is in a target specific section then we
  2903.      must rely upon the backend to tell us what it is.  */
  2904.   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
  2905.     /* FIXME - this function is not coded yet:
  2906.  
  2907.        return _bfd_is_global_symbol_definition (abfd, sym);
  2908.  
  2909.        Instead for now assume that the definition is not global,
  2910.        Even if this is wrong, at least the linker will behave
  2911.        in the same way that it used to do.  */
  2912.     return FALSE;
  2913.  
  2914.   return TRUE;
  2915. }
  2916.  
  2917. /* Search the symbol table of the archive element of the archive ABFD
  2918.    whose archive map contains a mention of SYMDEF, and determine if
  2919.    the symbol is defined in this element.  */
  2920. static bfd_boolean
  2921. elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
  2922. {
  2923.   Elf_Internal_Shdr * hdr;
  2924.   bfd_size_type symcount;
  2925.   bfd_size_type extsymcount;
  2926.   bfd_size_type extsymoff;
  2927.   Elf_Internal_Sym *isymbuf;
  2928.   Elf_Internal_Sym *isym;
  2929.   Elf_Internal_Sym *isymend;
  2930.   bfd_boolean result;
  2931.  
  2932.   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
  2933.   if (abfd == NULL)
  2934.     return FALSE;
  2935.  
  2936.   if (! bfd_check_format (abfd, bfd_object))
  2937.     return FALSE;
  2938.  
  2939.   /* If we have already included the element containing this symbol in the
  2940.      link then we do not need to include it again.  Just claim that any symbol
  2941.      it contains is not a definition, so that our caller will not decide to
  2942.      (re)include this element.  */
  2943.   if (abfd->archive_pass)
  2944.     return FALSE;
  2945.  
  2946.   /* Select the appropriate symbol table.  */
  2947.   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
  2948.     hdr = &elf_tdata (abfd)->symtab_hdr;
  2949.   else
  2950.     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  2951.  
  2952.   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
  2953.  
  2954.   /* The sh_info field of the symtab header tells us where the
  2955.      external symbols start.  We don't care about the local symbols.  */
  2956.   if (elf_bad_symtab (abfd))
  2957.     {
  2958.       extsymcount = symcount;
  2959.       extsymoff = 0;
  2960.     }
  2961.   else
  2962.     {
  2963.       extsymcount = symcount - hdr->sh_info;
  2964.       extsymoff = hdr->sh_info;
  2965.     }
  2966.  
  2967.   if (extsymcount == 0)
  2968.     return FALSE;
  2969.  
  2970.   /* Read in the symbol table.  */
  2971.   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  2972.                                   NULL, NULL, NULL);
  2973.   if (isymbuf == NULL)
  2974.     return FALSE;
  2975.  
  2976.   /* Scan the symbol table looking for SYMDEF.  */
  2977.   result = FALSE;
  2978.   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
  2979.     {
  2980.       const char *name;
  2981.  
  2982.       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  2983.                                               isym->st_name);
  2984.       if (name == NULL)
  2985.         break;
  2986.  
  2987.       if (strcmp (name, symdef->name) == 0)
  2988.         {
  2989.           result = is_global_data_symbol_definition (abfd, isym);
  2990.           break;
  2991.         }
  2992.     }
  2993.  
  2994.   free (isymbuf);
  2995.  
  2996.   return result;
  2997. }
  2998. /* Add an entry to the .dynamic table.  */
  2999.  
  3000. bfd_boolean
  3001. _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
  3002.                             bfd_vma tag,
  3003.                             bfd_vma val)
  3004. {
  3005.   struct elf_link_hash_table *hash_table;
  3006.   const struct elf_backend_data *bed;
  3007.   asection *s;
  3008.   bfd_size_type newsize;
  3009.   bfd_byte *newcontents;
  3010.   Elf_Internal_Dyn dyn;
  3011.  
  3012.   hash_table = elf_hash_table (info);
  3013.   if (! is_elf_hash_table (hash_table))
  3014.     return FALSE;
  3015.  
  3016.   bed = get_elf_backend_data (hash_table->dynobj);
  3017.   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3018.   BFD_ASSERT (s != NULL);
  3019.  
  3020.   newsize = s->size + bed->s->sizeof_dyn;
  3021.   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
  3022.   if (newcontents == NULL)
  3023.     return FALSE;
  3024.  
  3025.   dyn.d_tag = tag;
  3026.   dyn.d_un.d_val = val;
  3027.   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
  3028.  
  3029.   s->size = newsize;
  3030.   s->contents = newcontents;
  3031.  
  3032.   return TRUE;
  3033. }
  3034.  
  3035. /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
  3036.    otherwise just check whether one already exists.  Returns -1 on error,
  3037.    1 if a DT_NEEDED tag already exists, and 0 on success.  */
  3038.  
  3039. static int
  3040. elf_add_dt_needed_tag (bfd *abfd,
  3041.                        struct bfd_link_info *info,
  3042.                        const char *soname,
  3043.                        bfd_boolean do_it)
  3044. {
  3045.   struct elf_link_hash_table *hash_table;
  3046.   bfd_size_type strindex;
  3047.  
  3048.   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  3049.     return -1;
  3050.  
  3051.   hash_table = elf_hash_table (info);
  3052.   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
  3053.   if (strindex == (bfd_size_type) -1)
  3054.     return -1;
  3055.  
  3056.   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
  3057.     {
  3058.       asection *sdyn;
  3059.       const struct elf_backend_data *bed;
  3060.       bfd_byte *extdyn;
  3061.  
  3062.       bed = get_elf_backend_data (hash_table->dynobj);
  3063.       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3064.       if (sdyn != NULL)
  3065.         for (extdyn = sdyn->contents;
  3066.              extdyn < sdyn->contents + sdyn->size;
  3067.              extdyn += bed->s->sizeof_dyn)
  3068.           {
  3069.             Elf_Internal_Dyn dyn;
  3070.  
  3071.             bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
  3072.             if (dyn.d_tag == DT_NEEDED
  3073.                 && dyn.d_un.d_val == strindex)
  3074.               {
  3075.                 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
  3076.                 return 1;
  3077.               }
  3078.           }
  3079.     }
  3080.  
  3081.   if (do_it)
  3082.     {
  3083.       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
  3084.         return -1;
  3085.  
  3086.       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
  3087.         return -1;
  3088.     }
  3089.   else
  3090.     /* We were just checking for existence of the tag.  */
  3091.     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
  3092.  
  3093.   return 0;
  3094. }
  3095.  
  3096. static bfd_boolean
  3097. on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
  3098. {
  3099.   for (; needed != NULL; needed = needed->next)
  3100.     if (strcmp (soname, needed->name) == 0)
  3101.       return TRUE;
  3102.  
  3103.   return FALSE;
  3104. }
  3105.  
  3106. /* Sort symbol by value, section, and size.  */
  3107. static int
  3108. elf_sort_symbol (const void *arg1, const void *arg2)
  3109. {
  3110.   const struct elf_link_hash_entry *h1;
  3111.   const struct elf_link_hash_entry *h2;
  3112.   bfd_signed_vma vdiff;
  3113.  
  3114.   h1 = *(const struct elf_link_hash_entry **) arg1;
  3115.   h2 = *(const struct elf_link_hash_entry **) arg2;
  3116.   vdiff = h1->root.u.def.value - h2->root.u.def.value;
  3117.   if (vdiff != 0)
  3118.     return vdiff > 0 ? 1 : -1;
  3119.   else
  3120.     {
  3121.       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
  3122.       if (sdiff != 0)
  3123.         return sdiff > 0 ? 1 : -1;
  3124.     }
  3125.   vdiff = h1->size - h2->size;
  3126.   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
  3127. }
  3128.  
  3129. /* This function is used to adjust offsets into .dynstr for
  3130.    dynamic symbols.  This is called via elf_link_hash_traverse.  */
  3131.  
  3132. static bfd_boolean
  3133. elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
  3134. {
  3135.   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
  3136.  
  3137.   if (h->dynindx != -1)
  3138.     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
  3139.   return TRUE;
  3140. }
  3141.  
  3142. /* Assign string offsets in .dynstr, update all structures referencing
  3143.    them.  */
  3144.  
  3145. static bfd_boolean
  3146. elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  3147. {
  3148.   struct elf_link_hash_table *hash_table = elf_hash_table (info);
  3149.   struct elf_link_local_dynamic_entry *entry;
  3150.   struct elf_strtab_hash *dynstr = hash_table->dynstr;
  3151.   bfd *dynobj = hash_table->dynobj;
  3152.   asection *sdyn;
  3153.   bfd_size_type size;
  3154.   const struct elf_backend_data *bed;
  3155.   bfd_byte *extdyn;
  3156.  
  3157.   _bfd_elf_strtab_finalize (dynstr);
  3158.   size = _bfd_elf_strtab_size (dynstr);
  3159.  
  3160.   bed = get_elf_backend_data (dynobj);
  3161.   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3162.   BFD_ASSERT (sdyn != NULL);
  3163.  
  3164.   /* Update all .dynamic entries referencing .dynstr strings.  */
  3165.   for (extdyn = sdyn->contents;
  3166.        extdyn < sdyn->contents + sdyn->size;
  3167.        extdyn += bed->s->sizeof_dyn)
  3168.     {
  3169.       Elf_Internal_Dyn dyn;
  3170.  
  3171.       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
  3172.       switch (dyn.d_tag)
  3173.         {
  3174.         case DT_STRSZ:
  3175.           dyn.d_un.d_val = size;
  3176.           break;
  3177.         case DT_NEEDED:
  3178.         case DT_SONAME:
  3179.         case DT_RPATH:
  3180.         case DT_RUNPATH:
  3181.         case DT_FILTER:
  3182.         case DT_AUXILIARY:
  3183.         case DT_AUDIT:
  3184.         case DT_DEPAUDIT:
  3185.           dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
  3186.           break;
  3187.         default:
  3188.           continue;
  3189.         }
  3190.       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
  3191.     }
  3192.  
  3193.   /* Now update local dynamic symbols.  */
  3194.   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
  3195.     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
  3196.                                                   entry->isym.st_name);
  3197.  
  3198.   /* And the rest of dynamic symbols.  */
  3199.   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
  3200.  
  3201.   /* Adjust version definitions.  */
  3202.   if (elf_tdata (output_bfd)->cverdefs)
  3203.     {
  3204.       asection *s;
  3205.       bfd_byte *p;
  3206.       bfd_size_type i;
  3207.       Elf_Internal_Verdef def;
  3208.       Elf_Internal_Verdaux defaux;
  3209.  
  3210.       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  3211.       p = s->contents;
  3212.       do
  3213.         {
  3214.           _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
  3215.                                    &def);
  3216.           p += sizeof (Elf_External_Verdef);
  3217.           if (def.vd_aux != sizeof (Elf_External_Verdef))
  3218.             continue;
  3219.           for (i = 0; i < def.vd_cnt; ++i)
  3220.             {
  3221.               _bfd_elf_swap_verdaux_in (output_bfd,
  3222.                                         (Elf_External_Verdaux *) p, &defaux);
  3223.               defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
  3224.                                                         defaux.vda_name);
  3225.               _bfd_elf_swap_verdaux_out (output_bfd,
  3226.                                          &defaux, (Elf_External_Verdaux *) p);
  3227.               p += sizeof (Elf_External_Verdaux);
  3228.             }
  3229.         }
  3230.       while (def.vd_next);
  3231.     }
  3232.  
  3233.   /* Adjust version references.  */
  3234.   if (elf_tdata (output_bfd)->verref)
  3235.     {
  3236.       asection *s;
  3237.       bfd_byte *p;
  3238.       bfd_size_type i;
  3239.       Elf_Internal_Verneed need;
  3240.       Elf_Internal_Vernaux needaux;
  3241.  
  3242.       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  3243.       p = s->contents;
  3244.       do
  3245.         {
  3246.           _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
  3247.                                     &need);
  3248.           need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
  3249.           _bfd_elf_swap_verneed_out (output_bfd, &need,
  3250.                                      (Elf_External_Verneed *) p);
  3251.           p += sizeof (Elf_External_Verneed);
  3252.           for (i = 0; i < need.vn_cnt; ++i)
  3253.             {
  3254.               _bfd_elf_swap_vernaux_in (output_bfd,
  3255.                                         (Elf_External_Vernaux *) p, &needaux);
  3256.               needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
  3257.                                                          needaux.vna_name);
  3258.               _bfd_elf_swap_vernaux_out (output_bfd,
  3259.                                          &needaux,
  3260.                                          (Elf_External_Vernaux *) p);
  3261.               p += sizeof (Elf_External_Vernaux);
  3262.             }
  3263.         }
  3264.       while (need.vn_next);
  3265.     }
  3266.  
  3267.   return TRUE;
  3268. }
  3269. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3270.    The default is to only match when the INPUT and OUTPUT are exactly
  3271.    the same target.  */
  3272.  
  3273. bfd_boolean
  3274. _bfd_elf_default_relocs_compatible (const bfd_target *input,
  3275.                                     const bfd_target *output)
  3276. {
  3277.   return input == output;
  3278. }
  3279.  
  3280. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3281.    This version is used when different targets for the same architecture
  3282.    are virtually identical.  */
  3283.  
  3284. bfd_boolean
  3285. _bfd_elf_relocs_compatible (const bfd_target *input,
  3286.                             const bfd_target *output)
  3287. {
  3288.   const struct elf_backend_data *obed, *ibed;
  3289.  
  3290.   if (input == output)
  3291.     return TRUE;
  3292.  
  3293.   ibed = xvec_get_elf_backend_data (input);
  3294.   obed = xvec_get_elf_backend_data (output);
  3295.  
  3296.   if (ibed->arch != obed->arch)
  3297.     return FALSE;
  3298.  
  3299.   /* If both backends are using this function, deem them compatible.  */
  3300.   return ibed->relocs_compatible == obed->relocs_compatible;
  3301. }
  3302.  
  3303. /* Make a special call to the linker "notice" function to tell it that
  3304.    we are about to handle an as-needed lib, or have finished
  3305.    processing the lib.  */
  3306.  
  3307. bfd_boolean
  3308. _bfd_elf_notice_as_needed (bfd *ibfd,
  3309.                            struct bfd_link_info *info,
  3310.                            enum notice_asneeded_action act)
  3311. {
  3312.   return (*info->callbacks->notice) (info, NULL, ibfd, NULL, act, 0, NULL);
  3313. }
  3314.  
  3315. /* Add symbols from an ELF object file to the linker hash table.  */
  3316.  
  3317. static bfd_boolean
  3318. elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  3319. {
  3320.   Elf_Internal_Ehdr *ehdr;
  3321.   Elf_Internal_Shdr *hdr;
  3322.   bfd_size_type symcount;
  3323.   bfd_size_type extsymcount;
  3324.   bfd_size_type extsymoff;
  3325.   struct elf_link_hash_entry **sym_hash;
  3326.   bfd_boolean dynamic;
  3327.   Elf_External_Versym *extversym = NULL;
  3328.   Elf_External_Versym *ever;
  3329.   struct elf_link_hash_entry *weaks;
  3330.   struct elf_link_hash_entry **nondeflt_vers = NULL;
  3331.   bfd_size_type nondeflt_vers_cnt = 0;
  3332.   Elf_Internal_Sym *isymbuf = NULL;
  3333.   Elf_Internal_Sym *isym;
  3334.   Elf_Internal_Sym *isymend;
  3335.   const struct elf_backend_data *bed;
  3336.   bfd_boolean add_needed;
  3337.   struct elf_link_hash_table *htab;
  3338.   bfd_size_type amt;
  3339.   void *alloc_mark = NULL;
  3340.   struct bfd_hash_entry **old_table = NULL;
  3341.   unsigned int old_size = 0;
  3342.   unsigned int old_count = 0;
  3343.   void *old_tab = NULL;
  3344.   void *old_ent;
  3345.   struct bfd_link_hash_entry *old_undefs = NULL;
  3346.   struct bfd_link_hash_entry *old_undefs_tail = NULL;
  3347.   long old_dynsymcount = 0;
  3348.   bfd_size_type old_dynstr_size = 0;
  3349.   size_t tabsize = 0;
  3350.   asection *s;
  3351.  
  3352.   htab = elf_hash_table (info);
  3353.   bed = get_elf_backend_data (abfd);
  3354.  
  3355.   if ((abfd->flags & DYNAMIC) == 0)
  3356.     dynamic = FALSE;
  3357.   else
  3358.     {
  3359.       dynamic = TRUE;
  3360.  
  3361.       /* You can't use -r against a dynamic object.  Also, there's no
  3362.          hope of using a dynamic object which does not exactly match
  3363.          the format of the output file.  */
  3364.       if (info->relocatable
  3365.           || !is_elf_hash_table (htab)
  3366.           || info->output_bfd->xvec != abfd->xvec)
  3367.         {
  3368.           if (info->relocatable)
  3369.             bfd_set_error (bfd_error_invalid_operation);
  3370.           else
  3371.             bfd_set_error (bfd_error_wrong_format);
  3372.           goto error_return;
  3373.         }
  3374.     }
  3375.  
  3376.   ehdr = elf_elfheader (abfd);
  3377.   if (info->warn_alternate_em
  3378.       && bed->elf_machine_code != ehdr->e_machine
  3379.       && ((bed->elf_machine_alt1 != 0
  3380.            && ehdr->e_machine == bed->elf_machine_alt1)
  3381.           || (bed->elf_machine_alt2 != 0
  3382.               && ehdr->e_machine == bed->elf_machine_alt2)))
  3383.     info->callbacks->einfo
  3384.       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
  3385.        ehdr->e_machine, abfd, bed->elf_machine_code);
  3386.  
  3387.   /* As a GNU extension, any input sections which are named
  3388.      .gnu.warning.SYMBOL are treated as warning symbols for the given
  3389.      symbol.  This differs from .gnu.warning sections, which generate
  3390.      warnings when they are included in an output file.  */
  3391.   /* PR 12761: Also generate this warning when building shared libraries.  */
  3392.   for (s = abfd->sections; s != NULL; s = s->next)
  3393.     {
  3394.       const char *name;
  3395.  
  3396.       name = bfd_get_section_name (abfd, s);
  3397.       if (CONST_STRNEQ (name, ".gnu.warning."))
  3398.         {
  3399.           char *msg;
  3400.           bfd_size_type sz;
  3401.  
  3402.           name += sizeof ".gnu.warning." - 1;
  3403.  
  3404.           /* If this is a shared object, then look up the symbol
  3405.              in the hash table.  If it is there, and it is already
  3406.              been defined, then we will not be using the entry
  3407.              from this shared object, so we don't need to warn.
  3408.              FIXME: If we see the definition in a regular object
  3409.              later on, we will warn, but we shouldn't.  The only
  3410.              fix is to keep track of what warnings we are supposed
  3411.              to emit, and then handle them all at the end of the
  3412.              link.  */
  3413.           if (dynamic)
  3414.             {
  3415.               struct elf_link_hash_entry *h;
  3416.  
  3417.               h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
  3418.  
  3419.               /* FIXME: What about bfd_link_hash_common?  */
  3420.               if (h != NULL
  3421.                   && (h->root.type == bfd_link_hash_defined
  3422.                       || h->root.type == bfd_link_hash_defweak))
  3423.                 continue;
  3424.             }
  3425.  
  3426.           sz = s->size;
  3427.           msg = (char *) bfd_alloc (abfd, sz + 1);
  3428.           if (msg == NULL)
  3429.             goto error_return;
  3430.  
  3431.           if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
  3432.             goto error_return;
  3433.  
  3434.           msg[sz] = '\0';
  3435.  
  3436.           if (! (_bfd_generic_link_add_one_symbol
  3437.                  (info, abfd, name, BSF_WARNING, s, 0, msg,
  3438.                   FALSE, bed->collect, NULL)))
  3439.             goto error_return;
  3440.  
  3441.           if (!info->relocatable && info->executable)
  3442.             {
  3443.               /* Clobber the section size so that the warning does
  3444.                  not get copied into the output file.  */
  3445.               s->size = 0;
  3446.  
  3447.               /* Also set SEC_EXCLUDE, so that symbols defined in
  3448.                  the warning section don't get copied to the output.  */
  3449.               s->flags |= SEC_EXCLUDE;
  3450.             }
  3451.         }
  3452.     }
  3453.  
  3454.   add_needed = TRUE;
  3455.   if (! dynamic)
  3456.     {
  3457.       /* If we are creating a shared library, create all the dynamic
  3458.          sections immediately.  We need to attach them to something,
  3459.          so we attach them to this BFD, provided it is the right
  3460.          format.  FIXME: If there are no input BFD's of the same
  3461.          format as the output, we can't make a shared library.  */
  3462.       if (info->shared
  3463.           && is_elf_hash_table (htab)
  3464.           && info->output_bfd->xvec == abfd->xvec
  3465.           && !htab->dynamic_sections_created)
  3466.         {
  3467.           if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
  3468.             goto error_return;
  3469.         }
  3470.     }
  3471.   else if (!is_elf_hash_table (htab))
  3472.     goto error_return;
  3473.   else
  3474.     {
  3475.       const char *soname = NULL;
  3476.       char *audit = NULL;
  3477.       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
  3478.       int ret;
  3479.  
  3480.       /* ld --just-symbols and dynamic objects don't mix very well.
  3481.          ld shouldn't allow it.  */
  3482.       if ((s = abfd->sections) != NULL
  3483.           && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  3484.         abort ();
  3485.  
  3486.       /* If this dynamic lib was specified on the command line with
  3487.          --as-needed in effect, then we don't want to add a DT_NEEDED
  3488.          tag unless the lib is actually used.  Similary for libs brought
  3489.          in by another lib's DT_NEEDED.  When --no-add-needed is used
  3490.          on a dynamic lib, we don't want to add a DT_NEEDED entry for
  3491.          any dynamic library in DT_NEEDED tags in the dynamic lib at
  3492.          all.  */
  3493.       add_needed = (elf_dyn_lib_class (abfd)
  3494.                     & (DYN_AS_NEEDED | DYN_DT_NEEDED
  3495.                        | DYN_NO_NEEDED)) == 0;
  3496.  
  3497.       s = bfd_get_section_by_name (abfd, ".dynamic");
  3498.       if (s != NULL)
  3499.         {
  3500.           bfd_byte *dynbuf;
  3501.           bfd_byte *extdyn;
  3502.           unsigned int elfsec;
  3503.           unsigned long shlink;
  3504.  
  3505.           if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  3506.             {
  3507. error_free_dyn:
  3508.               free (dynbuf);
  3509.               goto error_return;
  3510.             }
  3511.  
  3512.           elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  3513.           if (elfsec == SHN_BAD)
  3514.             goto error_free_dyn;
  3515.           shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  3516.  
  3517.           for (extdyn = dynbuf;
  3518.                extdyn < dynbuf + s->size;
  3519.                extdyn += bed->s->sizeof_dyn)
  3520.             {
  3521.               Elf_Internal_Dyn dyn;
  3522.  
  3523.               bed->s->swap_dyn_in (abfd, extdyn, &dyn);
  3524.               if (dyn.d_tag == DT_SONAME)
  3525.                 {
  3526.                   unsigned int tagv = dyn.d_un.d_val;
  3527.                   soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3528.                   if (soname == NULL)
  3529.                     goto error_free_dyn;
  3530.                 }
  3531.               if (dyn.d_tag == DT_NEEDED)
  3532.                 {
  3533.                   struct bfd_link_needed_list *n, **pn;
  3534.                   char *fnm, *anm;
  3535.                   unsigned int tagv = dyn.d_un.d_val;
  3536.  
  3537.                   amt = sizeof (struct bfd_link_needed_list);
  3538.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3539.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3540.                   if (n == NULL || fnm == NULL)
  3541.                     goto error_free_dyn;
  3542.                   amt = strlen (fnm) + 1;
  3543.                   anm = (char *) bfd_alloc (abfd, amt);
  3544.                   if (anm == NULL)
  3545.                     goto error_free_dyn;
  3546.                   memcpy (anm, fnm, amt);
  3547.                   n->name = anm;
  3548.                   n->by = abfd;
  3549.                   n->next = NULL;
  3550.                   for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
  3551.                     ;
  3552.                   *pn = n;
  3553.                 }
  3554.               if (dyn.d_tag == DT_RUNPATH)
  3555.                 {
  3556.                   struct bfd_link_needed_list *n, **pn;
  3557.                   char *fnm, *anm;
  3558.                   unsigned int tagv = dyn.d_un.d_val;
  3559.  
  3560.                   amt = sizeof (struct bfd_link_needed_list);
  3561.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3562.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3563.                   if (n == NULL || fnm == NULL)
  3564.                     goto error_free_dyn;
  3565.                   amt = strlen (fnm) + 1;
  3566.                   anm = (char *) bfd_alloc (abfd, amt);
  3567.                   if (anm == NULL)
  3568.                     goto error_free_dyn;
  3569.                   memcpy (anm, fnm, amt);
  3570.                   n->name = anm;
  3571.                   n->by = abfd;
  3572.                   n->next = NULL;
  3573.                   for (pn = & runpath;
  3574.                        *pn != NULL;
  3575.                        pn = &(*pn)->next)
  3576.                     ;
  3577.                   *pn = n;
  3578.                 }
  3579.               /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
  3580.               if (!runpath && dyn.d_tag == DT_RPATH)
  3581.                 {
  3582.                   struct bfd_link_needed_list *n, **pn;
  3583.                   char *fnm, *anm;
  3584.                   unsigned int tagv = dyn.d_un.d_val;
  3585.  
  3586.                   amt = sizeof (struct bfd_link_needed_list);
  3587.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3588.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3589.                   if (n == NULL || fnm == NULL)
  3590.                     goto error_free_dyn;
  3591.                   amt = strlen (fnm) + 1;
  3592.                   anm = (char *) bfd_alloc (abfd, amt);
  3593.                   if (anm == NULL)
  3594.                     goto error_free_dyn;
  3595.                   memcpy (anm, fnm, amt);
  3596.                   n->name = anm;
  3597.                   n->by = abfd;
  3598.                   n->next = NULL;
  3599.                   for (pn = & rpath;
  3600.                        *pn != NULL;
  3601.                        pn = &(*pn)->next)
  3602.                     ;
  3603.                   *pn = n;
  3604.                 }
  3605.               if (dyn.d_tag == DT_AUDIT)
  3606.                 {
  3607.                   unsigned int tagv = dyn.d_un.d_val;
  3608.                   audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3609.                 }
  3610.             }
  3611.  
  3612.           free (dynbuf);
  3613.         }
  3614.  
  3615.       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
  3616.          frees all more recently bfd_alloc'd blocks as well.  */
  3617.       if (runpath)
  3618.         rpath = runpath;
  3619.  
  3620.       if (rpath)
  3621.         {
  3622.           struct bfd_link_needed_list **pn;
  3623.           for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
  3624.             ;
  3625.           *pn = rpath;
  3626.         }
  3627.  
  3628.       /* We do not want to include any of the sections in a dynamic
  3629.          object in the output file.  We hack by simply clobbering the
  3630.          list of sections in the BFD.  This could be handled more
  3631.          cleanly by, say, a new section flag; the existing
  3632.          SEC_NEVER_LOAD flag is not the one we want, because that one
  3633.          still implies that the section takes up space in the output
  3634.          file.  */
  3635.       bfd_section_list_clear (abfd);
  3636.  
  3637.       /* Find the name to use in a DT_NEEDED entry that refers to this
  3638.          object.  If the object has a DT_SONAME entry, we use it.
  3639.          Otherwise, if the generic linker stuck something in
  3640.          elf_dt_name, we use that.  Otherwise, we just use the file
  3641.          name.  */
  3642.       if (soname == NULL || *soname == '\0')
  3643.         {
  3644.           soname = elf_dt_name (abfd);
  3645.           if (soname == NULL || *soname == '\0')
  3646.             soname = bfd_get_filename (abfd);
  3647.         }
  3648.  
  3649.       /* Save the SONAME because sometimes the linker emulation code
  3650.          will need to know it.  */
  3651.       elf_dt_name (abfd) = soname;
  3652.  
  3653.       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
  3654.       if (ret < 0)
  3655.         goto error_return;
  3656.  
  3657.       /* If we have already included this dynamic object in the
  3658.          link, just ignore it.  There is no reason to include a
  3659.          particular dynamic object more than once.  */
  3660.       if (ret > 0)
  3661.         return TRUE;
  3662.  
  3663.       /* Save the DT_AUDIT entry for the linker emulation code. */
  3664.       elf_dt_audit (abfd) = audit;
  3665.     }
  3666.  
  3667.   /* If this is a dynamic object, we always link against the .dynsym
  3668.      symbol table, not the .symtab symbol table.  The dynamic linker
  3669.      will only see the .dynsym symbol table, so there is no reason to
  3670.      look at .symtab for a dynamic object.  */
  3671.  
  3672.   if (! dynamic || elf_dynsymtab (abfd) == 0)
  3673.     hdr = &elf_tdata (abfd)->symtab_hdr;
  3674.   else
  3675.     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  3676.  
  3677.   symcount = hdr->sh_size / bed->s->sizeof_sym;
  3678.  
  3679.   /* The sh_info field of the symtab header tells us where the
  3680.      external symbols start.  We don't care about the local symbols at
  3681.      this point.  */
  3682.   if (elf_bad_symtab (abfd))
  3683.     {
  3684.       extsymcount = symcount;
  3685.       extsymoff = 0;
  3686.     }
  3687.   else
  3688.     {
  3689.       extsymcount = symcount - hdr->sh_info;
  3690.       extsymoff = hdr->sh_info;
  3691.     }
  3692.  
  3693.   sym_hash = elf_sym_hashes (abfd);
  3694.   if (extsymcount != 0)
  3695.     {
  3696.       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  3697.                                       NULL, NULL, NULL);
  3698.       if (isymbuf == NULL)
  3699.         goto error_return;
  3700.  
  3701.       if (sym_hash == NULL)
  3702.         {
  3703.           /* We store a pointer to the hash table entry for each
  3704.              external symbol.  */
  3705.           amt = extsymcount * sizeof (struct elf_link_hash_entry *);
  3706.           sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
  3707.           if (sym_hash == NULL)
  3708.             goto error_free_sym;
  3709.           elf_sym_hashes (abfd) = sym_hash;
  3710.         }
  3711.     }
  3712.  
  3713.   if (dynamic)
  3714.     {
  3715.       /* Read in any version definitions.  */
  3716.       if (!_bfd_elf_slurp_version_tables (abfd,
  3717.                                           info->default_imported_symver))
  3718.         goto error_free_sym;
  3719.  
  3720.       /* Read in the symbol versions, but don't bother to convert them
  3721.          to internal format.  */
  3722.       if (elf_dynversym (abfd) != 0)
  3723.         {
  3724.           Elf_Internal_Shdr *versymhdr;
  3725.  
  3726.           versymhdr = &elf_tdata (abfd)->dynversym_hdr;
  3727.           extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
  3728.           if (extversym == NULL)
  3729.             goto error_free_sym;
  3730.           amt = versymhdr->sh_size;
  3731.           if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
  3732.               || bfd_bread (extversym, amt, abfd) != amt)
  3733.             goto error_free_vers;
  3734.         }
  3735.     }
  3736.  
  3737.   /* If we are loading an as-needed shared lib, save the symbol table
  3738.      state before we start adding symbols.  If the lib turns out
  3739.      to be unneeded, restore the state.  */
  3740.   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  3741.     {
  3742.       unsigned int i;
  3743.       size_t entsize;
  3744.  
  3745.       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
  3746.         {
  3747.           struct bfd_hash_entry *p;
  3748.           struct elf_link_hash_entry *h;
  3749.  
  3750.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  3751.             {
  3752.               h = (struct elf_link_hash_entry *) p;
  3753.               entsize += htab->root.table.entsize;
  3754.               if (h->root.type == bfd_link_hash_warning)
  3755.                 entsize += htab->root.table.entsize;
  3756.             }
  3757.         }
  3758.  
  3759.       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
  3760.       old_tab = bfd_malloc (tabsize + entsize);
  3761.       if (old_tab == NULL)
  3762.         goto error_free_vers;
  3763.  
  3764.       /* Remember the current objalloc pointer, so that all mem for
  3765.          symbols added can later be reclaimed.  */
  3766.       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
  3767.       if (alloc_mark == NULL)
  3768.         goto error_free_vers;
  3769.  
  3770.       /* Make a special call to the linker "notice" function to
  3771.          tell it that we are about to handle an as-needed lib.  */
  3772.       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
  3773.         goto error_free_vers;
  3774.  
  3775.       /* Clone the symbol table.  Remember some pointers into the
  3776.          symbol table, and dynamic symbol count.  */
  3777.       old_ent = (char *) old_tab + tabsize;
  3778.       memcpy (old_tab, htab->root.table.table, tabsize);
  3779.       old_undefs = htab->root.undefs;
  3780.       old_undefs_tail = htab->root.undefs_tail;
  3781.       old_table = htab->root.table.table;
  3782.       old_size = htab->root.table.size;
  3783.       old_count = htab->root.table.count;
  3784.       old_dynsymcount = htab->dynsymcount;
  3785.       old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
  3786.  
  3787.       for (i = 0; i < htab->root.table.size; i++)
  3788.         {
  3789.           struct bfd_hash_entry *p;
  3790.           struct elf_link_hash_entry *h;
  3791.  
  3792.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  3793.             {
  3794.               memcpy (old_ent, p, htab->root.table.entsize);
  3795.               old_ent = (char *) old_ent + htab->root.table.entsize;
  3796.               h = (struct elf_link_hash_entry *) p;
  3797.               if (h->root.type == bfd_link_hash_warning)
  3798.                 {
  3799.                   memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
  3800.                   old_ent = (char *) old_ent + htab->root.table.entsize;
  3801.                 }
  3802.             }
  3803.         }
  3804.     }
  3805.  
  3806.   weaks = NULL;
  3807.   ever = extversym != NULL ? extversym + extsymoff : NULL;
  3808.   for (isym = isymbuf, isymend = isymbuf + extsymcount;
  3809.        isym < isymend;
  3810.        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
  3811.     {
  3812.       int bind;
  3813.       bfd_vma value;
  3814.       asection *sec, *new_sec;
  3815.       flagword flags;
  3816.       const char *name;
  3817.       struct elf_link_hash_entry *h;
  3818.       struct elf_link_hash_entry *hi;
  3819.       bfd_boolean definition;
  3820.       bfd_boolean size_change_ok;
  3821.       bfd_boolean type_change_ok;
  3822.       bfd_boolean new_weakdef;
  3823.       bfd_boolean new_weak;
  3824.       bfd_boolean old_weak;
  3825.       bfd_boolean override;
  3826.       bfd_boolean common;
  3827.       unsigned int old_alignment;
  3828.       bfd *old_bfd;
  3829.  
  3830.       override = FALSE;
  3831.  
  3832.       flags = BSF_NO_FLAGS;
  3833.       sec = NULL;
  3834.       value = isym->st_value;
  3835.       common = bed->common_definition (isym);
  3836.  
  3837.       bind = ELF_ST_BIND (isym->st_info);
  3838.       switch (bind)
  3839.         {
  3840.         case STB_LOCAL:
  3841.           /* This should be impossible, since ELF requires that all
  3842.              global symbols follow all local symbols, and that sh_info
  3843.              point to the first global symbol.  Unfortunately, Irix 5
  3844.              screws this up.  */
  3845.           continue;
  3846.  
  3847.         case STB_GLOBAL:
  3848.           if (isym->st_shndx != SHN_UNDEF && !common)
  3849.             flags = BSF_GLOBAL;
  3850.           break;
  3851.  
  3852.         case STB_WEAK:
  3853.           flags = BSF_WEAK;
  3854.           break;
  3855.  
  3856.         case STB_GNU_UNIQUE:
  3857.           flags = BSF_GNU_UNIQUE;
  3858.           break;
  3859.  
  3860.         default:
  3861.           /* Leave it up to the processor backend.  */
  3862.           break;
  3863.         }
  3864.  
  3865.       if (isym->st_shndx == SHN_UNDEF)
  3866.         sec = bfd_und_section_ptr;
  3867.       else if (isym->st_shndx == SHN_ABS)
  3868.         sec = bfd_abs_section_ptr;
  3869.       else if (isym->st_shndx == SHN_COMMON)
  3870.         {
  3871.           sec = bfd_com_section_ptr;
  3872.           /* What ELF calls the size we call the value.  What ELF
  3873.              calls the value we call the alignment.  */
  3874.           value = isym->st_size;
  3875.         }
  3876.       else
  3877.         {
  3878.           sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  3879.           if (sec == NULL)
  3880.             sec = bfd_abs_section_ptr;
  3881.           else if (discarded_section (sec))
  3882.             {
  3883.               /* Symbols from discarded section are undefined.  We keep
  3884.                  its visibility.  */
  3885.               sec = bfd_und_section_ptr;
  3886.               isym->st_shndx = SHN_UNDEF;
  3887.             }
  3888.           else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  3889.             value -= sec->vma;
  3890.         }
  3891.  
  3892.       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  3893.                                               isym->st_name);
  3894.       if (name == NULL)
  3895.         goto error_free_vers;
  3896.  
  3897.       if (isym->st_shndx == SHN_COMMON
  3898.           && (abfd->flags & BFD_PLUGIN) != 0)
  3899.         {
  3900.           asection *xc = bfd_get_section_by_name (abfd, "COMMON");
  3901.  
  3902.           if (xc == NULL)
  3903.             {
  3904.               flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
  3905.                                  | SEC_EXCLUDE);
  3906.               xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
  3907.               if (xc == NULL)
  3908.                 goto error_free_vers;
  3909.             }
  3910.           sec = xc;
  3911.         }
  3912.       else if (isym->st_shndx == SHN_COMMON
  3913.                && ELF_ST_TYPE (isym->st_info) == STT_TLS
  3914.                && !info->relocatable)
  3915.         {
  3916.           asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
  3917.  
  3918.           if (tcomm == NULL)
  3919.             {
  3920.               flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
  3921.                                  | SEC_LINKER_CREATED);
  3922.               tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
  3923.               if (tcomm == NULL)
  3924.                 goto error_free_vers;
  3925.             }
  3926.           sec = tcomm;
  3927.         }
  3928.       else if (bed->elf_add_symbol_hook)
  3929.         {
  3930.           if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
  3931.                                              &sec, &value))
  3932.             goto error_free_vers;
  3933.  
  3934.           /* The hook function sets the name to NULL if this symbol
  3935.              should be skipped for some reason.  */
  3936.           if (name == NULL)
  3937.             continue;
  3938.         }
  3939.  
  3940.       /* Sanity check that all possibilities were handled.  */
  3941.       if (sec == NULL)
  3942.         {
  3943.           bfd_set_error (bfd_error_bad_value);
  3944.           goto error_free_vers;
  3945.         }
  3946.  
  3947.       /* Silently discard TLS symbols from --just-syms.  There's
  3948.          no way to combine a static TLS block with a new TLS block
  3949.          for this executable.  */
  3950.       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
  3951.           && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  3952.         continue;
  3953.  
  3954.       if (bfd_is_und_section (sec)
  3955.           || bfd_is_com_section (sec))
  3956.         definition = FALSE;
  3957.       else
  3958.         definition = TRUE;
  3959.  
  3960.       size_change_ok = FALSE;
  3961.       type_change_ok = bed->type_change_ok;
  3962.       old_weak = FALSE;
  3963.       old_alignment = 0;
  3964.       old_bfd = NULL;
  3965.       new_sec = sec;
  3966.  
  3967.       if (is_elf_hash_table (htab))
  3968.         {
  3969.           Elf_Internal_Versym iver;
  3970.           unsigned int vernum = 0;
  3971.           bfd_boolean skip;
  3972.  
  3973.           if (ever == NULL)
  3974.             {
  3975.               if (info->default_imported_symver)
  3976.                 /* Use the default symbol version created earlier.  */
  3977.                 iver.vs_vers = elf_tdata (abfd)->cverdefs;
  3978.               else
  3979.                 iver.vs_vers = 0;
  3980.             }
  3981.           else
  3982.             _bfd_elf_swap_versym_in (abfd, ever, &iver);
  3983.  
  3984.           vernum = iver.vs_vers & VERSYM_VERSION;
  3985.  
  3986.           /* If this is a hidden symbol, or if it is not version
  3987.              1, we append the version name to the symbol name.
  3988.              However, we do not modify a non-hidden absolute symbol
  3989.              if it is not a function, because it might be the version
  3990.              symbol itself.  FIXME: What if it isn't?  */
  3991.           if ((iver.vs_vers & VERSYM_HIDDEN) != 0
  3992.               || (vernum > 1
  3993.                   && (!bfd_is_abs_section (sec)
  3994.                       || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
  3995.             {
  3996.               const char *verstr;
  3997.               size_t namelen, verlen, newlen;
  3998.               char *newname, *p;
  3999.  
  4000.               if (isym->st_shndx != SHN_UNDEF)
  4001.                 {
  4002.                   if (vernum > elf_tdata (abfd)->cverdefs)
  4003.                     verstr = NULL;
  4004.                   else if (vernum > 1)
  4005.                     verstr =
  4006.                       elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
  4007.                   else
  4008.                     verstr = "";
  4009.  
  4010.                   if (verstr == NULL)
  4011.                     {
  4012.                       (*_bfd_error_handler)
  4013.                         (_("%B: %s: invalid version %u (max %d)"),
  4014.                          abfd, name, vernum,
  4015.                          elf_tdata (abfd)->cverdefs);
  4016.                       bfd_set_error (bfd_error_bad_value);
  4017.                       goto error_free_vers;
  4018.                     }
  4019.                 }
  4020.               else
  4021.                 {
  4022.                   /* We cannot simply test for the number of
  4023.                      entries in the VERNEED section since the
  4024.                      numbers for the needed versions do not start
  4025.                      at 0.  */
  4026.                   Elf_Internal_Verneed *t;
  4027.  
  4028.                   verstr = NULL;
  4029.                   for (t = elf_tdata (abfd)->verref;
  4030.                        t != NULL;
  4031.                        t = t->vn_nextref)
  4032.                     {
  4033.                       Elf_Internal_Vernaux *a;
  4034.  
  4035.                       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  4036.                         {
  4037.                           if (a->vna_other == vernum)
  4038.                             {
  4039.                               verstr = a->vna_nodename;
  4040.                               break;
  4041.                             }
  4042.                         }
  4043.                       if (a != NULL)
  4044.                         break;
  4045.                     }
  4046.                   if (verstr == NULL)
  4047.                     {
  4048.                       (*_bfd_error_handler)
  4049.                         (_("%B: %s: invalid needed version %d"),
  4050.                          abfd, name, vernum);
  4051.                       bfd_set_error (bfd_error_bad_value);
  4052.                       goto error_free_vers;
  4053.                     }
  4054.                 }
  4055.  
  4056.               namelen = strlen (name);
  4057.               verlen = strlen (verstr);
  4058.               newlen = namelen + verlen + 2;
  4059.               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4060.                   && isym->st_shndx != SHN_UNDEF)
  4061.                 ++newlen;
  4062.  
  4063.               newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
  4064.               if (newname == NULL)
  4065.                 goto error_free_vers;
  4066.               memcpy (newname, name, namelen);
  4067.               p = newname + namelen;
  4068.               *p++ = ELF_VER_CHR;
  4069.               /* If this is a defined non-hidden version symbol,
  4070.                  we add another @ to the name.  This indicates the
  4071.                  default version of the symbol.  */
  4072.               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4073.                   && isym->st_shndx != SHN_UNDEF)
  4074.                 *p++ = ELF_VER_CHR;
  4075.               memcpy (p, verstr, verlen + 1);
  4076.  
  4077.               name = newname;
  4078.             }
  4079.  
  4080.           if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
  4081.                                       sym_hash, &old_bfd, &old_weak,
  4082.                                       &old_alignment, &skip, &override,
  4083.                                       &type_change_ok, &size_change_ok))
  4084.             goto error_free_vers;
  4085.  
  4086.           if (skip)
  4087.             continue;
  4088.  
  4089.           if (override)
  4090.             definition = FALSE;
  4091.  
  4092.           h = *sym_hash;
  4093.           while (h->root.type == bfd_link_hash_indirect
  4094.                  || h->root.type == bfd_link_hash_warning)
  4095.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4096.  
  4097.           if (elf_tdata (abfd)->verdef != NULL
  4098.               && vernum > 1
  4099.               && definition)
  4100.             h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
  4101.         }
  4102.  
  4103.       if (! (_bfd_generic_link_add_one_symbol
  4104.              (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
  4105.               (struct bfd_link_hash_entry **) sym_hash)))
  4106.         goto error_free_vers;
  4107.  
  4108.       h = *sym_hash;
  4109.       /* We need to make sure that indirect symbol dynamic flags are
  4110.          updated.  */
  4111.       hi = h;
  4112.       while (h->root.type == bfd_link_hash_indirect
  4113.              || h->root.type == bfd_link_hash_warning)
  4114.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4115.  
  4116.       *sym_hash = h;
  4117.  
  4118.       new_weak = (flags & BSF_WEAK) != 0;
  4119.       new_weakdef = FALSE;
  4120.       if (dynamic
  4121.           && definition
  4122.           && new_weak
  4123.           && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
  4124.           && is_elf_hash_table (htab)
  4125.           && h->u.weakdef == NULL)
  4126.         {
  4127.           /* Keep a list of all weak defined non function symbols from
  4128.              a dynamic object, using the weakdef field.  Later in this
  4129.              function we will set the weakdef field to the correct
  4130.              value.  We only put non-function symbols from dynamic
  4131.              objects on this list, because that happens to be the only
  4132.              time we need to know the normal symbol corresponding to a
  4133.              weak symbol, and the information is time consuming to
  4134.              figure out.  If the weakdef field is not already NULL,
  4135.              then this symbol was already defined by some previous
  4136.              dynamic object, and we will be using that previous
  4137.              definition anyhow.  */
  4138.  
  4139.           h->u.weakdef = weaks;
  4140.           weaks = h;
  4141.           new_weakdef = TRUE;
  4142.         }
  4143.  
  4144.       /* Set the alignment of a common symbol.  */
  4145.       if ((common || bfd_is_com_section (sec))
  4146.           && h->root.type == bfd_link_hash_common)
  4147.         {
  4148.           unsigned int align;
  4149.  
  4150.           if (common)
  4151.             align = bfd_log2 (isym->st_value);
  4152.           else
  4153.             {
  4154.               /* The new symbol is a common symbol in a shared object.
  4155.                  We need to get the alignment from the section.  */
  4156.               align = new_sec->alignment_power;
  4157.             }
  4158.           if (align > old_alignment)
  4159.             h->root.u.c.p->alignment_power = align;
  4160.           else
  4161.             h->root.u.c.p->alignment_power = old_alignment;
  4162.         }
  4163.  
  4164.       if (is_elf_hash_table (htab))
  4165.         {
  4166.           /* Set a flag in the hash table entry indicating the type of
  4167.              reference or definition we just found.  A dynamic symbol
  4168.              is one which is referenced or defined by both a regular
  4169.              object and a shared object.  */
  4170.           bfd_boolean dynsym = FALSE;
  4171.  
  4172.           /* Plugin symbols aren't normal.  Don't set def_regular or
  4173.              ref_regular for them, or make them dynamic.  */
  4174.           if ((abfd->flags & BFD_PLUGIN) != 0)
  4175.             ;
  4176.           else if (! dynamic)
  4177.             {
  4178.               if (! definition)
  4179.                 {
  4180.                   h->ref_regular = 1;
  4181.                   if (bind != STB_WEAK)
  4182.                     h->ref_regular_nonweak = 1;
  4183.                 }
  4184.               else
  4185.                 {
  4186.                   h->def_regular = 1;
  4187.                   if (h->def_dynamic)
  4188.                     {
  4189.                       h->def_dynamic = 0;
  4190.                       h->ref_dynamic = 1;
  4191.                     }
  4192.                 }
  4193.  
  4194.               /* If the indirect symbol has been forced local, don't
  4195.                  make the real symbol dynamic.  */
  4196.               if ((h == hi || !hi->forced_local)
  4197.                   && (! info->executable
  4198.                       || h->def_dynamic
  4199.                       || h->ref_dynamic))
  4200.                 dynsym = TRUE;
  4201.             }
  4202.           else
  4203.             {
  4204.               if (! definition)
  4205.                 {
  4206.                   h->ref_dynamic = 1;
  4207.                   hi->ref_dynamic = 1;
  4208.                 }
  4209.               else
  4210.                 {
  4211.                   h->def_dynamic = 1;
  4212.                   hi->def_dynamic = 1;
  4213.                 }
  4214.  
  4215.               /* If the indirect symbol has been forced local, don't
  4216.                  make the real symbol dynamic.  */
  4217.               if ((h == hi || !hi->forced_local)
  4218.                   && (h->def_regular
  4219.                       || h->ref_regular
  4220.                       || (h->u.weakdef != NULL
  4221.                           && ! new_weakdef
  4222.                           && h->u.weakdef->dynindx != -1)))
  4223.                 dynsym = TRUE;
  4224.             }
  4225.  
  4226.           /* Check to see if we need to add an indirect symbol for
  4227.              the default name.  */
  4228.           if (definition
  4229.               || (!override && h->root.type == bfd_link_hash_common))
  4230.             if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
  4231.                                               sec, value, &old_bfd, &dynsym))
  4232.               goto error_free_vers;
  4233.  
  4234.           /* Check the alignment when a common symbol is involved. This
  4235.              can change when a common symbol is overridden by a normal
  4236.              definition or a common symbol is ignored due to the old
  4237.              normal definition. We need to make sure the maximum
  4238.              alignment is maintained.  */
  4239.           if ((old_alignment || common)
  4240.               && h->root.type != bfd_link_hash_common)
  4241.             {
  4242.               unsigned int common_align;
  4243.               unsigned int normal_align;
  4244.               unsigned int symbol_align;
  4245.               bfd *normal_bfd;
  4246.               bfd *common_bfd;
  4247.  
  4248.               BFD_ASSERT (h->root.type == bfd_link_hash_defined
  4249.                           || h->root.type == bfd_link_hash_defweak);
  4250.  
  4251.               symbol_align = ffs (h->root.u.def.value) - 1;
  4252.               if (h->root.u.def.section->owner != NULL
  4253.                   && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
  4254.                 {
  4255.                   normal_align = h->root.u.def.section->alignment_power;
  4256.                   if (normal_align > symbol_align)
  4257.                     normal_align = symbol_align;
  4258.                 }
  4259.               else
  4260.                 normal_align = symbol_align;
  4261.  
  4262.               if (old_alignment)
  4263.                 {
  4264.                   common_align = old_alignment;
  4265.                   common_bfd = old_bfd;
  4266.                   normal_bfd = abfd;
  4267.                 }
  4268.               else
  4269.                 {
  4270.                   common_align = bfd_log2 (isym->st_value);
  4271.                   common_bfd = abfd;
  4272.                   normal_bfd = old_bfd;
  4273.                 }
  4274.  
  4275.               if (normal_align < common_align)
  4276.                 {
  4277.                   /* PR binutils/2735 */
  4278.                   if (normal_bfd == NULL)
  4279.                     (*_bfd_error_handler)
  4280.                       (_("Warning: alignment %u of common symbol `%s' in %B is"
  4281.                          " greater than the alignment (%u) of its section %A"),
  4282.                        common_bfd, h->root.u.def.section,
  4283.                        1 << common_align, name, 1 << normal_align);
  4284.                   else
  4285.                     (*_bfd_error_handler)
  4286.                       (_("Warning: alignment %u of symbol `%s' in %B"
  4287.                          " is smaller than %u in %B"),
  4288.                        normal_bfd, common_bfd,
  4289.                        1 << normal_align, name, 1 << common_align);
  4290.                 }
  4291.             }
  4292.  
  4293.           /* Remember the symbol size if it isn't undefined.  */
  4294.           if (isym->st_size != 0
  4295.               && isym->st_shndx != SHN_UNDEF
  4296.               && (definition || h->size == 0))
  4297.             {
  4298.               if (h->size != 0
  4299.                   && h->size != isym->st_size
  4300.                   && ! size_change_ok)
  4301.                 (*_bfd_error_handler)
  4302.                   (_("Warning: size of symbol `%s' changed"
  4303.                      " from %lu in %B to %lu in %B"),
  4304.                    old_bfd, abfd,
  4305.                    name, (unsigned long) h->size,
  4306.                    (unsigned long) isym->st_size);
  4307.  
  4308.               h->size = isym->st_size;
  4309.             }
  4310.  
  4311.           /* If this is a common symbol, then we always want H->SIZE
  4312.              to be the size of the common symbol.  The code just above
  4313.              won't fix the size if a common symbol becomes larger.  We
  4314.              don't warn about a size change here, because that is
  4315.              covered by --warn-common.  Allow changes between different
  4316.              function types.  */
  4317.           if (h->root.type == bfd_link_hash_common)
  4318.             h->size = h->root.u.c.size;
  4319.  
  4320.           if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
  4321.               && ((definition && !new_weak)
  4322.                   || (old_weak && h->root.type == bfd_link_hash_common)
  4323.                   || h->type == STT_NOTYPE))
  4324.             {
  4325.               unsigned int type = ELF_ST_TYPE (isym->st_info);
  4326.  
  4327.               /* Turn an IFUNC symbol from a DSO into a normal FUNC
  4328.                  symbol.  */
  4329.               if (type == STT_GNU_IFUNC
  4330.                   && (abfd->flags & DYNAMIC) != 0)
  4331.                 type = STT_FUNC;
  4332.  
  4333.               if (h->type != type)
  4334.                 {
  4335.                   if (h->type != STT_NOTYPE && ! type_change_ok)
  4336.                     (*_bfd_error_handler)
  4337.                       (_("Warning: type of symbol `%s' changed"
  4338.                          " from %d to %d in %B"),
  4339.                        abfd, name, h->type, type);
  4340.  
  4341.                   h->type = type;
  4342.                 }
  4343.             }
  4344.  
  4345.           /* Merge st_other field.  */
  4346.           elf_merge_st_other (abfd, h, isym, definition, dynamic);
  4347.  
  4348.           /* We don't want to make debug symbol dynamic.  */
  4349.           if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
  4350.             dynsym = FALSE;
  4351.  
  4352.           /* Nor should we make plugin symbols dynamic.  */
  4353.           if ((abfd->flags & BFD_PLUGIN) != 0)
  4354.             dynsym = FALSE;
  4355.  
  4356.           if (definition)
  4357.             {
  4358.               h->target_internal = isym->st_target_internal;
  4359.               h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
  4360.             }
  4361.  
  4362.           if (definition && !dynamic)
  4363.             {
  4364.               char *p = strchr (name, ELF_VER_CHR);
  4365.               if (p != NULL && p[1] != ELF_VER_CHR)
  4366.                 {
  4367.                   /* Queue non-default versions so that .symver x, x@FOO
  4368.                      aliases can be checked.  */
  4369.                   if (!nondeflt_vers)
  4370.                     {
  4371.                       amt = ((isymend - isym + 1)
  4372.                              * sizeof (struct elf_link_hash_entry *));
  4373.                       nondeflt_vers =
  4374.                           (struct elf_link_hash_entry **) bfd_malloc (amt);
  4375.                       if (!nondeflt_vers)
  4376.                         goto error_free_vers;
  4377.                     }
  4378.                   nondeflt_vers[nondeflt_vers_cnt++] = h;
  4379.                 }
  4380.             }
  4381.  
  4382.           if (dynsym && h->dynindx == -1)
  4383.             {
  4384.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4385.                 goto error_free_vers;
  4386.               if (h->u.weakdef != NULL
  4387.                   && ! new_weakdef
  4388.                   && h->u.weakdef->dynindx == -1)
  4389.                 {
  4390.                   if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
  4391.                     goto error_free_vers;
  4392.                 }
  4393.             }
  4394.           else if (dynsym && h->dynindx != -1)
  4395.             /* If the symbol already has a dynamic index, but
  4396.                visibility says it should not be visible, turn it into
  4397.                a local symbol.  */
  4398.             switch (ELF_ST_VISIBILITY (h->other))
  4399.               {
  4400.               case STV_INTERNAL:
  4401.               case STV_HIDDEN:
  4402.                 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  4403.                 dynsym = FALSE;
  4404.                 break;
  4405.               }
  4406.  
  4407.           /* Don't add DT_NEEDED for references from the dummy bfd.  */
  4408.           if (!add_needed
  4409.               && definition
  4410.               && ((dynsym
  4411.                    && h->ref_regular_nonweak
  4412.                    && (old_bfd == NULL
  4413.                        || (old_bfd->flags & BFD_PLUGIN) == 0))
  4414.                   || (h->ref_dynamic_nonweak
  4415.                       && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
  4416.                       && !on_needed_list (elf_dt_name (abfd), htab->needed))))
  4417.             {
  4418.               int ret;
  4419.               const char *soname = elf_dt_name (abfd);
  4420.  
  4421.               /* A symbol from a library loaded via DT_NEEDED of some
  4422.                  other library is referenced by a regular object.
  4423.                  Add a DT_NEEDED entry for it.  Issue an error if
  4424.                  --no-add-needed is used and the reference was not
  4425.                  a weak one.  */
  4426.               if (old_bfd != NULL
  4427.                   && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
  4428.                 {
  4429.                   (*_bfd_error_handler)
  4430.                     (_("%B: undefined reference to symbol '%s'"),
  4431.                      old_bfd, name);
  4432.                   bfd_set_error (bfd_error_missing_dso);
  4433.                   goto error_free_vers;
  4434.                 }
  4435.  
  4436.               elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
  4437.                   (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
  4438.  
  4439.               add_needed = TRUE;
  4440.               ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
  4441.               if (ret < 0)
  4442.                 goto error_free_vers;
  4443.  
  4444.               BFD_ASSERT (ret == 0);
  4445.             }
  4446.         }
  4447.     }
  4448.  
  4449.   if (extversym != NULL)
  4450.     {
  4451.       free (extversym);
  4452.       extversym = NULL;
  4453.     }
  4454.  
  4455.   if (isymbuf != NULL)
  4456.     {
  4457.       free (isymbuf);
  4458.       isymbuf = NULL;
  4459.     }
  4460.  
  4461.   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  4462.     {
  4463.       unsigned int i;
  4464.  
  4465.       /* Restore the symbol table.  */
  4466.       old_ent = (char *) old_tab + tabsize;
  4467.       memset (elf_sym_hashes (abfd), 0,
  4468.               extsymcount * sizeof (struct elf_link_hash_entry *));
  4469.       htab->root.table.table = old_table;
  4470.       htab->root.table.size = old_size;
  4471.       htab->root.table.count = old_count;
  4472.       memcpy (htab->root.table.table, old_tab, tabsize);
  4473.       htab->root.undefs = old_undefs;
  4474.       htab->root.undefs_tail = old_undefs_tail;
  4475.       _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
  4476.       for (i = 0; i < htab->root.table.size; i++)
  4477.         {
  4478.           struct bfd_hash_entry *p;
  4479.           struct elf_link_hash_entry *h;
  4480.           bfd_size_type size;
  4481.           unsigned int alignment_power;
  4482.  
  4483.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  4484.             {
  4485.               h = (struct elf_link_hash_entry *) p;
  4486.               if (h->root.type == bfd_link_hash_warning)
  4487.                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4488.               if (h->dynindx >= old_dynsymcount
  4489.                   && h->dynstr_index < old_dynstr_size)
  4490.                 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
  4491.  
  4492.               /* Preserve the maximum alignment and size for common
  4493.                  symbols even if this dynamic lib isn't on DT_NEEDED
  4494.                  since it can still be loaded at run time by another
  4495.                  dynamic lib.  */
  4496.               if (h->root.type == bfd_link_hash_common)
  4497.                 {
  4498.                   size = h->root.u.c.size;
  4499.                   alignment_power = h->root.u.c.p->alignment_power;
  4500.                 }
  4501.               else
  4502.                 {
  4503.                   size = 0;
  4504.                   alignment_power = 0;
  4505.                 }
  4506.               memcpy (p, old_ent, htab->root.table.entsize);
  4507.               old_ent = (char *) old_ent + htab->root.table.entsize;
  4508.               h = (struct elf_link_hash_entry *) p;
  4509.               if (h->root.type == bfd_link_hash_warning)
  4510.                 {
  4511.                   memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
  4512.                   old_ent = (char *) old_ent + htab->root.table.entsize;
  4513.                   h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4514.                 }
  4515.               if (h->root.type == bfd_link_hash_common)
  4516.                 {
  4517.                   if (size > h->root.u.c.size)
  4518.                     h->root.u.c.size = size;
  4519.                   if (alignment_power > h->root.u.c.p->alignment_power)
  4520.                     h->root.u.c.p->alignment_power = alignment_power;
  4521.                 }
  4522.             }
  4523.         }
  4524.  
  4525.       /* Make a special call to the linker "notice" function to
  4526.          tell it that symbols added for crefs may need to be removed.  */
  4527.       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
  4528.         goto error_free_vers;
  4529.  
  4530.       free (old_tab);
  4531.       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
  4532.                            alloc_mark);
  4533.       if (nondeflt_vers != NULL)
  4534.         free (nondeflt_vers);
  4535.       return TRUE;
  4536.     }
  4537.  
  4538.   if (old_tab != NULL)
  4539.     {
  4540.       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
  4541.         goto error_free_vers;
  4542.       free (old_tab);
  4543.       old_tab = NULL;
  4544.     }
  4545.  
  4546.   /* Now that all the symbols from this input file are created, handle
  4547.      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
  4548.   if (nondeflt_vers != NULL)
  4549.     {
  4550.       bfd_size_type cnt, symidx;
  4551.  
  4552.       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
  4553.         {
  4554.           struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
  4555.           char *shortname, *p;
  4556.  
  4557.           p = strchr (h->root.root.string, ELF_VER_CHR);
  4558.           if (p == NULL
  4559.               || (h->root.type != bfd_link_hash_defined
  4560.                   && h->root.type != bfd_link_hash_defweak))
  4561.             continue;
  4562.  
  4563.           amt = p - h->root.root.string;
  4564.           shortname = (char *) bfd_malloc (amt + 1);
  4565.           if (!shortname)
  4566.             goto error_free_vers;
  4567.           memcpy (shortname, h->root.root.string, amt);
  4568.           shortname[amt] = '\0';
  4569.  
  4570.           hi = (struct elf_link_hash_entry *)
  4571.                bfd_link_hash_lookup (&htab->root, shortname,
  4572.                                      FALSE, FALSE, FALSE);
  4573.           if (hi != NULL
  4574.               && hi->root.type == h->root.type
  4575.               && hi->root.u.def.value == h->root.u.def.value
  4576.               && hi->root.u.def.section == h->root.u.def.section)
  4577.             {
  4578.               (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
  4579.               hi->root.type = bfd_link_hash_indirect;
  4580.               hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
  4581.               (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  4582.               sym_hash = elf_sym_hashes (abfd);
  4583.               if (sym_hash)
  4584.                 for (symidx = 0; symidx < extsymcount; ++symidx)
  4585.                   if (sym_hash[symidx] == hi)
  4586.                     {
  4587.                       sym_hash[symidx] = h;
  4588.                       break;
  4589.                     }
  4590.             }
  4591.           free (shortname);
  4592.         }
  4593.       free (nondeflt_vers);
  4594.       nondeflt_vers = NULL;
  4595.     }
  4596.  
  4597.   /* Now set the weakdefs field correctly for all the weak defined
  4598.      symbols we found.  The only way to do this is to search all the
  4599.      symbols.  Since we only need the information for non functions in
  4600.      dynamic objects, that's the only time we actually put anything on
  4601.      the list WEAKS.  We need this information so that if a regular
  4602.      object refers to a symbol defined weakly in a dynamic object, the
  4603.      real symbol in the dynamic object is also put in the dynamic
  4604.      symbols; we also must arrange for both symbols to point to the
  4605.      same memory location.  We could handle the general case of symbol
  4606.      aliasing, but a general symbol alias can only be generated in
  4607.      assembler code, handling it correctly would be very time
  4608.      consuming, and other ELF linkers don't handle general aliasing
  4609.      either.  */
  4610.   if (weaks != NULL)
  4611.     {
  4612.       struct elf_link_hash_entry **hpp;
  4613.       struct elf_link_hash_entry **hppend;
  4614.       struct elf_link_hash_entry **sorted_sym_hash;
  4615.       struct elf_link_hash_entry *h;
  4616.       size_t sym_count;
  4617.  
  4618.       /* Since we have to search the whole symbol list for each weak
  4619.          defined symbol, search time for N weak defined symbols will be
  4620.          O(N^2). Binary search will cut it down to O(NlogN).  */
  4621.       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
  4622.       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
  4623.       if (sorted_sym_hash == NULL)
  4624.         goto error_return;
  4625.       sym_hash = sorted_sym_hash;
  4626.       hpp = elf_sym_hashes (abfd);
  4627.       hppend = hpp + extsymcount;
  4628.       sym_count = 0;
  4629.       for (; hpp < hppend; hpp++)
  4630.         {
  4631.           h = *hpp;
  4632.           if (h != NULL
  4633.               && h->root.type == bfd_link_hash_defined
  4634.               && !bed->is_function_type (h->type))
  4635.             {
  4636.               *sym_hash = h;
  4637.               sym_hash++;
  4638.               sym_count++;
  4639.             }
  4640.         }
  4641.  
  4642.       qsort (sorted_sym_hash, sym_count,
  4643.              sizeof (struct elf_link_hash_entry *),
  4644.              elf_sort_symbol);
  4645.  
  4646.       while (weaks != NULL)
  4647.         {
  4648.           struct elf_link_hash_entry *hlook;
  4649.           asection *slook;
  4650.           bfd_vma vlook;
  4651.           size_t i, j, idx = 0;
  4652.  
  4653.           hlook = weaks;
  4654.           weaks = hlook->u.weakdef;
  4655.           hlook->u.weakdef = NULL;
  4656.  
  4657.           BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
  4658.                       || hlook->root.type == bfd_link_hash_defweak
  4659.                       || hlook->root.type == bfd_link_hash_common
  4660.                       || hlook->root.type == bfd_link_hash_indirect);
  4661.           slook = hlook->root.u.def.section;
  4662.           vlook = hlook->root.u.def.value;
  4663.  
  4664.           i = 0;
  4665.           j = sym_count;
  4666.           while (i != j)
  4667.             {
  4668.               bfd_signed_vma vdiff;
  4669.               idx = (i + j) / 2;
  4670.               h = sorted_sym_hash[idx];
  4671.               vdiff = vlook - h->root.u.def.value;
  4672.               if (vdiff < 0)
  4673.                 j = idx;
  4674.               else if (vdiff > 0)
  4675.                 i = idx + 1;
  4676.               else
  4677.                 {
  4678.                   long sdiff = slook->id - h->root.u.def.section->id;
  4679.                   if (sdiff < 0)
  4680.                     j = idx;
  4681.                   else if (sdiff > 0)
  4682.                     i = idx + 1;
  4683.                   else
  4684.                     break;
  4685.                 }
  4686.             }
  4687.  
  4688.           /* We didn't find a value/section match.  */
  4689.           if (i == j)
  4690.             continue;
  4691.  
  4692.           /* With multiple aliases, or when the weak symbol is already
  4693.              strongly defined, we have multiple matching symbols and
  4694.              the binary search above may land on any of them.  Step
  4695.              one past the matching symbol(s).  */
  4696.           while (++idx != j)
  4697.             {
  4698.               h = sorted_sym_hash[idx];
  4699.               if (h->root.u.def.section != slook
  4700.                   || h->root.u.def.value != vlook)
  4701.                 break;
  4702.             }
  4703.  
  4704.           /* Now look back over the aliases.  Since we sorted by size
  4705.              as well as value and section, we'll choose the one with
  4706.              the largest size.  */
  4707.           while (idx-- != i)
  4708.             {
  4709.               h = sorted_sym_hash[idx];
  4710.  
  4711.               /* Stop if value or section doesn't match.  */
  4712.               if (h->root.u.def.section != slook
  4713.                   || h->root.u.def.value != vlook)
  4714.                 break;
  4715.               else if (h != hlook)
  4716.                 {
  4717.                   hlook->u.weakdef = h;
  4718.  
  4719.                   /* If the weak definition is in the list of dynamic
  4720.                      symbols, make sure the real definition is put
  4721.                      there as well.  */
  4722.                   if (hlook->dynindx != -1 && h->dynindx == -1)
  4723.                     {
  4724.                       if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4725.                         {
  4726.                         err_free_sym_hash:
  4727.                           free (sorted_sym_hash);
  4728.                           goto error_return;
  4729.                         }
  4730.                     }
  4731.  
  4732.                   /* If the real definition is in the list of dynamic
  4733.                      symbols, make sure the weak definition is put
  4734.                      there as well.  If we don't do this, then the
  4735.                      dynamic loader might not merge the entries for the
  4736.                      real definition and the weak definition.  */
  4737.                   if (h->dynindx != -1 && hlook->dynindx == -1)
  4738.                     {
  4739.                       if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
  4740.                         goto err_free_sym_hash;
  4741.                     }
  4742.                   break;
  4743.                 }
  4744.             }
  4745.         }
  4746.  
  4747.       free (sorted_sym_hash);
  4748.     }
  4749.  
  4750.   if (bed->check_directives
  4751.       && !(*bed->check_directives) (abfd, info))
  4752.     return FALSE;
  4753.  
  4754.   /* If this object is the same format as the output object, and it is
  4755.      not a shared library, then let the backend look through the
  4756.      relocs.
  4757.  
  4758.      This is required to build global offset table entries and to
  4759.      arrange for dynamic relocs.  It is not required for the
  4760.      particular common case of linking non PIC code, even when linking
  4761.      against shared libraries, but unfortunately there is no way of
  4762.      knowing whether an object file has been compiled PIC or not.
  4763.      Looking through the relocs is not particularly time consuming.
  4764.      The problem is that we must either (1) keep the relocs in memory,
  4765.      which causes the linker to require additional runtime memory or
  4766.      (2) read the relocs twice from the input file, which wastes time.
  4767.      This would be a good case for using mmap.
  4768.  
  4769.      I have no idea how to handle linking PIC code into a file of a
  4770.      different format.  It probably can't be done.  */
  4771.   if (! dynamic
  4772.       && is_elf_hash_table (htab)
  4773.       && bed->check_relocs != NULL
  4774.       && elf_object_id (abfd) == elf_hash_table_id (htab)
  4775.       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
  4776.     {
  4777.       asection *o;
  4778.  
  4779.       for (o = abfd->sections; o != NULL; o = o->next)
  4780.         {
  4781.           Elf_Internal_Rela *internal_relocs;
  4782.           bfd_boolean ok;
  4783.  
  4784.           if ((o->flags & SEC_RELOC) == 0
  4785.               || o->reloc_count == 0
  4786.               || ((info->strip == strip_all || info->strip == strip_debugger)
  4787.                   && (o->flags & SEC_DEBUGGING) != 0)
  4788.               || bfd_is_abs_section (o->output_section))
  4789.             continue;
  4790.  
  4791.           internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
  4792.                                                        info->keep_memory);
  4793.           if (internal_relocs == NULL)
  4794.             goto error_return;
  4795.  
  4796.           ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
  4797.  
  4798.           if (elf_section_data (o)->relocs != internal_relocs)
  4799.             free (internal_relocs);
  4800.  
  4801.           if (! ok)
  4802.             goto error_return;
  4803.         }
  4804.     }
  4805.  
  4806.   /* If this is a non-traditional link, try to optimize the handling
  4807.      of the .stab/.stabstr sections.  */
  4808.   if (! dynamic
  4809.       && ! info->traditional_format
  4810.       && is_elf_hash_table (htab)
  4811.       && (info->strip != strip_all && info->strip != strip_debugger))
  4812.     {
  4813.       asection *stabstr;
  4814.  
  4815.       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
  4816.       if (stabstr != NULL)
  4817.         {
  4818.           bfd_size_type string_offset = 0;
  4819.           asection *stab;
  4820.  
  4821.           for (stab = abfd->sections; stab; stab = stab->next)
  4822.             if (CONST_STRNEQ (stab->name, ".stab")
  4823.                 && (!stab->name[5] ||
  4824.                     (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
  4825.                 && (stab->flags & SEC_MERGE) == 0
  4826.                 && !bfd_is_abs_section (stab->output_section))
  4827.               {
  4828.                 struct bfd_elf_section_data *secdata;
  4829.  
  4830.                 secdata = elf_section_data (stab);
  4831.                 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
  4832.                                                stabstr, &secdata->sec_info,
  4833.                                                &string_offset))
  4834.                   goto error_return;
  4835.                 if (secdata->sec_info)
  4836.                   stab->sec_info_type = SEC_INFO_TYPE_STABS;
  4837.             }
  4838.         }
  4839.     }
  4840.  
  4841.   if (is_elf_hash_table (htab) && add_needed)
  4842.     {
  4843.       /* Add this bfd to the loaded list.  */
  4844.       struct elf_link_loaded_list *n;
  4845.  
  4846.       n = (struct elf_link_loaded_list *)
  4847.           bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
  4848.       if (n == NULL)
  4849.         goto error_return;
  4850.       n->abfd = abfd;
  4851.       n->next = htab->loaded;
  4852.       htab->loaded = n;
  4853.     }
  4854.  
  4855.   return TRUE;
  4856.  
  4857.  error_free_vers:
  4858.   if (old_tab != NULL)
  4859.     free (old_tab);
  4860.   if (nondeflt_vers != NULL)
  4861.     free (nondeflt_vers);
  4862.   if (extversym != NULL)
  4863.     free (extversym);
  4864.  error_free_sym:
  4865.   if (isymbuf != NULL)
  4866.     free (isymbuf);
  4867.  error_return:
  4868.   return FALSE;
  4869. }
  4870.  
  4871. /* Return the linker hash table entry of a symbol that might be
  4872.    satisfied by an archive symbol.  Return -1 on error.  */
  4873.  
  4874. struct elf_link_hash_entry *
  4875. _bfd_elf_archive_symbol_lookup (bfd *abfd,
  4876.                                 struct bfd_link_info *info,
  4877.                                 const char *name)
  4878. {
  4879.   struct elf_link_hash_entry *h;
  4880.   char *p, *copy;
  4881.   size_t len, first;
  4882.  
  4883.   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
  4884.   if (h != NULL)
  4885.     return h;
  4886.  
  4887.   /* If this is a default version (the name contains @@), look up the
  4888.      symbol again with only one `@' as well as without the version.
  4889.      The effect is that references to the symbol with and without the
  4890.      version will be matched by the default symbol in the archive.  */
  4891.  
  4892.   p = strchr (name, ELF_VER_CHR);
  4893.   if (p == NULL || p[1] != ELF_VER_CHR)
  4894.     return h;
  4895.  
  4896.   /* First check with only one `@'.  */
  4897.   len = strlen (name);
  4898.   copy = (char *) bfd_alloc (abfd, len);
  4899.   if (copy == NULL)
  4900.     return (struct elf_link_hash_entry *) 0 - 1;
  4901.  
  4902.   first = p - name + 1;
  4903.   memcpy (copy, name, first);
  4904.   memcpy (copy + first, name + first + 1, len - first);
  4905.  
  4906.   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
  4907.   if (h == NULL)
  4908.     {
  4909.       /* We also need to check references to the symbol without the
  4910.          version.  */
  4911.       copy[first - 1] = '\0';
  4912.       h = elf_link_hash_lookup (elf_hash_table (info), copy,
  4913.                                 FALSE, FALSE, TRUE);
  4914.     }
  4915.  
  4916.   bfd_release (abfd, copy);
  4917.   return h;
  4918. }
  4919.  
  4920. /* Add symbols from an ELF archive file to the linker hash table.  We
  4921.    don't use _bfd_generic_link_add_archive_symbols because of a
  4922.    problem which arises on UnixWare.  The UnixWare libc.so is an
  4923.    archive which includes an entry libc.so.1 which defines a bunch of
  4924.    symbols.  The libc.so archive also includes a number of other
  4925.    object files, which also define symbols, some of which are the same
  4926.    as those defined in libc.so.1.  Correct linking requires that we
  4927.    consider each object file in turn, and include it if it defines any
  4928.    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
  4929.    this; it looks through the list of undefined symbols, and includes
  4930.    any object file which defines them.  When this algorithm is used on
  4931.    UnixWare, it winds up pulling in libc.so.1 early and defining a
  4932.    bunch of symbols.  This means that some of the other objects in the
  4933.    archive are not included in the link, which is incorrect since they
  4934.    precede libc.so.1 in the archive.
  4935.  
  4936.    Fortunately, ELF archive handling is simpler than that done by
  4937.    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
  4938.    oddities.  In ELF, if we find a symbol in the archive map, and the
  4939.    symbol is currently undefined, we know that we must pull in that
  4940.    object file.
  4941.  
  4942.    Unfortunately, we do have to make multiple passes over the symbol
  4943.    table until nothing further is resolved.  */
  4944.  
  4945. static bfd_boolean
  4946. elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
  4947. {
  4948.   symindex c;
  4949.   bfd_boolean *defined = NULL;
  4950.   bfd_boolean *included = NULL;
  4951.   carsym *symdefs;
  4952.   bfd_boolean loop;
  4953.   bfd_size_type amt;
  4954.   const struct elf_backend_data *bed;
  4955.   struct elf_link_hash_entry * (*archive_symbol_lookup)
  4956.     (bfd *, struct bfd_link_info *, const char *);
  4957.  
  4958.   if (! bfd_has_map (abfd))
  4959.     {
  4960.       /* An empty archive is a special case.  */
  4961.       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  4962.         return TRUE;
  4963.       bfd_set_error (bfd_error_no_armap);
  4964.       return FALSE;
  4965.     }
  4966.  
  4967.   /* Keep track of all symbols we know to be already defined, and all
  4968.      files we know to be already included.  This is to speed up the
  4969.      second and subsequent passes.  */
  4970.   c = bfd_ardata (abfd)->symdef_count;
  4971.   if (c == 0)
  4972.     return TRUE;
  4973.   amt = c;
  4974.   amt *= sizeof (bfd_boolean);
  4975.   defined = (bfd_boolean *) bfd_zmalloc (amt);
  4976.   included = (bfd_boolean *) bfd_zmalloc (amt);
  4977.   if (defined == NULL || included == NULL)
  4978.     goto error_return;
  4979.  
  4980.   symdefs = bfd_ardata (abfd)->symdefs;
  4981.   bed = get_elf_backend_data (abfd);
  4982.   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
  4983.  
  4984.   do
  4985.     {
  4986.       file_ptr last;
  4987.       symindex i;
  4988.       carsym *symdef;
  4989.       carsym *symdefend;
  4990.  
  4991.       loop = FALSE;
  4992.       last = -1;
  4993.  
  4994.       symdef = symdefs;
  4995.       symdefend = symdef + c;
  4996.       for (i = 0; symdef < symdefend; symdef++, i++)
  4997.         {
  4998.           struct elf_link_hash_entry *h;
  4999.           bfd *element;
  5000.           struct bfd_link_hash_entry *undefs_tail;
  5001.           symindex mark;
  5002.  
  5003.           if (defined[i] || included[i])
  5004.             continue;
  5005.           if (symdef->file_offset == last)
  5006.             {
  5007.               included[i] = TRUE;
  5008.               continue;
  5009.             }
  5010.  
  5011.           h = archive_symbol_lookup (abfd, info, symdef->name);
  5012.           if (h == (struct elf_link_hash_entry *) 0 - 1)
  5013.             goto error_return;
  5014.  
  5015.           if (h == NULL)
  5016.             continue;
  5017.  
  5018.           if (h->root.type == bfd_link_hash_common)
  5019.             {
  5020.               /* We currently have a common symbol.  The archive map contains
  5021.                  a reference to this symbol, so we may want to include it.  We
  5022.                  only want to include it however, if this archive element
  5023.                  contains a definition of the symbol, not just another common
  5024.                  declaration of it.
  5025.  
  5026.                  Unfortunately some archivers (including GNU ar) will put
  5027.                  declarations of common symbols into their archive maps, as
  5028.                  well as real definitions, so we cannot just go by the archive
  5029.                  map alone.  Instead we must read in the element's symbol
  5030.                  table and check that to see what kind of symbol definition
  5031.                  this is.  */
  5032.               if (! elf_link_is_defined_archive_symbol (abfd, symdef))
  5033.                 continue;
  5034.             }
  5035.           else if (h->root.type != bfd_link_hash_undefined)
  5036.             {
  5037.               if (h->root.type != bfd_link_hash_undefweak)
  5038.                 defined[i] = TRUE;
  5039.               continue;
  5040.             }
  5041.  
  5042.           /* We need to include this archive member.  */
  5043.           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
  5044.           if (element == NULL)
  5045.             goto error_return;
  5046.  
  5047.           if (! bfd_check_format (element, bfd_object))
  5048.             goto error_return;
  5049.  
  5050.           /* Doublecheck that we have not included this object
  5051.              already--it should be impossible, but there may be
  5052.              something wrong with the archive.  */
  5053.           if (element->archive_pass != 0)
  5054.             {
  5055.               bfd_set_error (bfd_error_bad_value);
  5056.               goto error_return;
  5057.             }
  5058.           element->archive_pass = 1;
  5059.  
  5060.           undefs_tail = info->hash->undefs_tail;
  5061.  
  5062.           if (!(*info->callbacks
  5063.                 ->add_archive_element) (info, element, symdef->name, &element))
  5064.             goto error_return;
  5065.           if (!bfd_link_add_symbols (element, info))
  5066.             goto error_return;
  5067.  
  5068.           /* If there are any new undefined symbols, we need to make
  5069.              another pass through the archive in order to see whether
  5070.              they can be defined.  FIXME: This isn't perfect, because
  5071.              common symbols wind up on undefs_tail and because an
  5072.              undefined symbol which is defined later on in this pass
  5073.              does not require another pass.  This isn't a bug, but it
  5074.              does make the code less efficient than it could be.  */
  5075.           if (undefs_tail != info->hash->undefs_tail)
  5076.             loop = TRUE;
  5077.  
  5078.           /* Look backward to mark all symbols from this object file
  5079.              which we have already seen in this pass.  */
  5080.           mark = i;
  5081.           do
  5082.             {
  5083.               included[mark] = TRUE;
  5084.               if (mark == 0)
  5085.                 break;
  5086.               --mark;
  5087.             }
  5088.           while (symdefs[mark].file_offset == symdef->file_offset);
  5089.  
  5090.           /* We mark subsequent symbols from this object file as we go
  5091.              on through the loop.  */
  5092.           last = symdef->file_offset;
  5093.         }
  5094.     }
  5095.   while (loop);
  5096.  
  5097.   free (defined);
  5098.   free (included);
  5099.  
  5100.   return TRUE;
  5101.  
  5102.  error_return:
  5103.   if (defined != NULL)
  5104.     free (defined);
  5105.   if (included != NULL)
  5106.     free (included);
  5107.   return FALSE;
  5108. }
  5109.  
  5110. /* Given an ELF BFD, add symbols to the global hash table as
  5111.    appropriate.  */
  5112.  
  5113. bfd_boolean
  5114. bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  5115. {
  5116.   switch (bfd_get_format (abfd))
  5117.     {
  5118.     case bfd_object:
  5119.       return elf_link_add_object_symbols (abfd, info);
  5120.     case bfd_archive:
  5121.       return elf_link_add_archive_symbols (abfd, info);
  5122.     default:
  5123.       bfd_set_error (bfd_error_wrong_format);
  5124.       return FALSE;
  5125.     }
  5126. }
  5127. struct hash_codes_info
  5128. {
  5129.   unsigned long *hashcodes;
  5130.   bfd_boolean error;
  5131. };
  5132.  
  5133. /* This function will be called though elf_link_hash_traverse to store
  5134.    all hash value of the exported symbols in an array.  */
  5135.  
  5136. static bfd_boolean
  5137. elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
  5138. {
  5139.   struct hash_codes_info *inf = (struct hash_codes_info *) data;
  5140.   const char *name;
  5141.   char *p;
  5142.   unsigned long ha;
  5143.   char *alc = NULL;
  5144.  
  5145.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  5146.   if (h->dynindx == -1)
  5147.     return TRUE;
  5148.  
  5149.   name = h->root.root.string;
  5150.   p = strchr (name, ELF_VER_CHR);
  5151.   if (p != NULL)
  5152.     {
  5153.       alc = (char *) bfd_malloc (p - name + 1);
  5154.       if (alc == NULL)
  5155.         {
  5156.           inf->error = TRUE;
  5157.           return FALSE;
  5158.         }
  5159.       memcpy (alc, name, p - name);
  5160.       alc[p - name] = '\0';
  5161.       name = alc;
  5162.     }
  5163.  
  5164.   /* Compute the hash value.  */
  5165.   ha = bfd_elf_hash (name);
  5166.  
  5167.   /* Store the found hash value in the array given as the argument.  */
  5168.   *(inf->hashcodes)++ = ha;
  5169.  
  5170.   /* And store it in the struct so that we can put it in the hash table
  5171.      later.  */
  5172.   h->u.elf_hash_value = ha;
  5173.  
  5174.   if (alc != NULL)
  5175.     free (alc);
  5176.  
  5177.   return TRUE;
  5178. }
  5179.  
  5180. struct collect_gnu_hash_codes
  5181. {
  5182.   bfd *output_bfd;
  5183.   const struct elf_backend_data *bed;
  5184.   unsigned long int nsyms;
  5185.   unsigned long int maskbits;
  5186.   unsigned long int *hashcodes;
  5187.   unsigned long int *hashval;
  5188.   unsigned long int *indx;
  5189.   unsigned long int *counts;
  5190.   bfd_vma *bitmask;
  5191.   bfd_byte *contents;
  5192.   long int min_dynindx;
  5193.   unsigned long int bucketcount;
  5194.   unsigned long int symindx;
  5195.   long int local_indx;
  5196.   long int shift1, shift2;
  5197.   unsigned long int mask;
  5198.   bfd_boolean error;
  5199. };
  5200.  
  5201. /* This function will be called though elf_link_hash_traverse to store
  5202.    all hash value of the exported symbols in an array.  */
  5203.  
  5204. static bfd_boolean
  5205. elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
  5206. {
  5207.   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5208.   const char *name;
  5209.   char *p;
  5210.   unsigned long ha;
  5211.   char *alc = NULL;
  5212.  
  5213.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  5214.   if (h->dynindx == -1)
  5215.     return TRUE;
  5216.  
  5217.   /* Ignore also local symbols and undefined symbols.  */
  5218.   if (! (*s->bed->elf_hash_symbol) (h))
  5219.     return TRUE;
  5220.  
  5221.   name = h->root.root.string;
  5222.   p = strchr (name, ELF_VER_CHR);
  5223.   if (p != NULL)
  5224.     {
  5225.       alc = (char *) bfd_malloc (p - name + 1);
  5226.       if (alc == NULL)
  5227.         {
  5228.           s->error = TRUE;
  5229.           return FALSE;
  5230.         }
  5231.       memcpy (alc, name, p - name);
  5232.       alc[p - name] = '\0';
  5233.       name = alc;
  5234.     }
  5235.  
  5236.   /* Compute the hash value.  */
  5237.   ha = bfd_elf_gnu_hash (name);
  5238.  
  5239.   /* Store the found hash value in the array for compute_bucket_count,
  5240.      and also for .dynsym reordering purposes.  */
  5241.   s->hashcodes[s->nsyms] = ha;
  5242.   s->hashval[h->dynindx] = ha;
  5243.   ++s->nsyms;
  5244.   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
  5245.     s->min_dynindx = h->dynindx;
  5246.  
  5247.   if (alc != NULL)
  5248.     free (alc);
  5249.  
  5250.   return TRUE;
  5251. }
  5252.  
  5253. /* This function will be called though elf_link_hash_traverse to do
  5254.    final dynaminc symbol renumbering.  */
  5255.  
  5256. static bfd_boolean
  5257. elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
  5258. {
  5259.   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5260.   unsigned long int bucket;
  5261.   unsigned long int val;
  5262.  
  5263.   /* Ignore indirect symbols.  */
  5264.   if (h->dynindx == -1)
  5265.     return TRUE;
  5266.  
  5267.   /* Ignore also local symbols and undefined symbols.  */
  5268.   if (! (*s->bed->elf_hash_symbol) (h))
  5269.     {
  5270.       if (h->dynindx >= s->min_dynindx)
  5271.         h->dynindx = s->local_indx++;
  5272.       return TRUE;
  5273.     }
  5274.  
  5275.   bucket = s->hashval[h->dynindx] % s->bucketcount;
  5276.   val = (s->hashval[h->dynindx] >> s->shift1)
  5277.         & ((s->maskbits >> s->shift1) - 1);
  5278.   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
  5279.   s->bitmask[val]
  5280.     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
  5281.   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
  5282.   if (s->counts[bucket] == 1)
  5283.     /* Last element terminates the chain.  */
  5284.     val |= 1;
  5285.   bfd_put_32 (s->output_bfd, val,
  5286.               s->contents + (s->indx[bucket] - s->symindx) * 4);
  5287.   --s->counts[bucket];
  5288.   h->dynindx = s->indx[bucket]++;
  5289.   return TRUE;
  5290. }
  5291.  
  5292. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
  5293.  
  5294. bfd_boolean
  5295. _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
  5296. {
  5297.   return !(h->forced_local
  5298.            || h->root.type == bfd_link_hash_undefined
  5299.            || h->root.type == bfd_link_hash_undefweak
  5300.            || ((h->root.type == bfd_link_hash_defined
  5301.                 || h->root.type == bfd_link_hash_defweak)
  5302.                && h->root.u.def.section->output_section == NULL));
  5303. }
  5304.  
  5305. /* Array used to determine the number of hash table buckets to use
  5306.    based on the number of symbols there are.  If there are fewer than
  5307.    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
  5308.    fewer than 37 we use 17 buckets, and so forth.  We never use more
  5309.    than 32771 buckets.  */
  5310.  
  5311. static const size_t elf_buckets[] =
  5312. {
  5313.   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  5314.   16411, 32771, 0
  5315. };
  5316.  
  5317. /* Compute bucket count for hashing table.  We do not use a static set
  5318.    of possible tables sizes anymore.  Instead we determine for all
  5319.    possible reasonable sizes of the table the outcome (i.e., the
  5320.    number of collisions etc) and choose the best solution.  The
  5321.    weighting functions are not too simple to allow the table to grow
  5322.    without bounds.  Instead one of the weighting factors is the size.
  5323.    Therefore the result is always a good payoff between few collisions
  5324.    (= short chain lengths) and table size.  */
  5325. static size_t
  5326. compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5327.                       unsigned long int *hashcodes ATTRIBUTE_UNUSED,
  5328.                       unsigned long int nsyms,
  5329.                       int gnu_hash)
  5330. {
  5331.   size_t best_size = 0;
  5332.   unsigned long int i;
  5333.  
  5334.   /* We have a problem here.  The following code to optimize the table
  5335.      size requires an integer type with more the 32 bits.  If
  5336.      BFD_HOST_U_64_BIT is set we know about such a type.  */
  5337. #ifdef BFD_HOST_U_64_BIT
  5338.   if (info->optimize)
  5339.     {
  5340.       size_t minsize;
  5341.       size_t maxsize;
  5342.       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
  5343.       bfd *dynobj = elf_hash_table (info)->dynobj;
  5344.       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
  5345.       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
  5346.       unsigned long int *counts;
  5347.       bfd_size_type amt;
  5348.       unsigned int no_improvement_count = 0;
  5349.  
  5350.       /* Possible optimization parameters: if we have NSYMS symbols we say
  5351.          that the hashing table must at least have NSYMS/4 and at most
  5352.          2*NSYMS buckets.  */
  5353.       minsize = nsyms / 4;
  5354.       if (minsize == 0)
  5355.         minsize = 1;
  5356.       best_size = maxsize = nsyms * 2;
  5357.       if (gnu_hash)
  5358.         {
  5359.           if (minsize < 2)
  5360.             minsize = 2;
  5361.           if ((best_size & 31) == 0)
  5362.             ++best_size;
  5363.         }
  5364.  
  5365.       /* Create array where we count the collisions in.  We must use bfd_malloc
  5366.          since the size could be large.  */
  5367.       amt = maxsize;
  5368.       amt *= sizeof (unsigned long int);
  5369.       counts = (unsigned long int *) bfd_malloc (amt);
  5370.       if (counts == NULL)
  5371.         return 0;
  5372.  
  5373.       /* Compute the "optimal" size for the hash table.  The criteria is a
  5374.          minimal chain length.  The minor criteria is (of course) the size
  5375.          of the table.  */
  5376.       for (i = minsize; i < maxsize; ++i)
  5377.         {
  5378.           /* Walk through the array of hashcodes and count the collisions.  */
  5379.           BFD_HOST_U_64_BIT max;
  5380.           unsigned long int j;
  5381.           unsigned long int fact;
  5382.  
  5383.           if (gnu_hash && (i & 31) == 0)
  5384.             continue;
  5385.  
  5386.           memset (counts, '\0', i * sizeof (unsigned long int));
  5387.  
  5388.           /* Determine how often each hash bucket is used.  */
  5389.           for (j = 0; j < nsyms; ++j)
  5390.             ++counts[hashcodes[j] % i];
  5391.  
  5392.           /* For the weight function we need some information about the
  5393.              pagesize on the target.  This is information need not be 100%
  5394.              accurate.  Since this information is not available (so far) we
  5395.              define it here to a reasonable default value.  If it is crucial
  5396.              to have a better value some day simply define this value.  */
  5397. # ifndef BFD_TARGET_PAGESIZE
  5398. #  define BFD_TARGET_PAGESIZE   (4096)
  5399. # endif
  5400.  
  5401.           /* We in any case need 2 + DYNSYMCOUNT entries for the size values
  5402.              and the chains.  */
  5403.           max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
  5404.  
  5405. # if 1
  5406.           /* Variant 1: optimize for short chains.  We add the squares
  5407.              of all the chain lengths (which favors many small chain
  5408.              over a few long chains).  */
  5409.           for (j = 0; j < i; ++j)
  5410.             max += counts[j] * counts[j];
  5411.  
  5412.           /* This adds penalties for the overall size of the table.  */
  5413.           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5414.           max *= fact * fact;
  5415. # else
  5416.           /* Variant 2: Optimize a lot more for small table.  Here we
  5417.              also add squares of the size but we also add penalties for
  5418.              empty slots (the +1 term).  */
  5419.           for (j = 0; j < i; ++j)
  5420.             max += (1 + counts[j]) * (1 + counts[j]);
  5421.  
  5422.           /* The overall size of the table is considered, but not as
  5423.              strong as in variant 1, where it is squared.  */
  5424.           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5425.           max *= fact;
  5426. # endif
  5427.  
  5428.           /* Compare with current best results.  */
  5429.           if (max < best_chlen)
  5430.             {
  5431.               best_chlen = max;
  5432.               best_size = i;
  5433.               no_improvement_count = 0;
  5434.             }
  5435.           /* PR 11843: Avoid futile long searches for the best bucket size
  5436.              when there are a large number of symbols.  */
  5437.           else if (++no_improvement_count == 100)
  5438.             break;
  5439.         }
  5440.  
  5441.       free (counts);
  5442.     }
  5443.   else
  5444. #endif /* defined (BFD_HOST_U_64_BIT) */
  5445.     {
  5446.       /* This is the fallback solution if no 64bit type is available or if we
  5447.          are not supposed to spend much time on optimizations.  We select the
  5448.          bucket count using a fixed set of numbers.  */
  5449.       for (i = 0; elf_buckets[i] != 0; i++)
  5450.         {
  5451.           best_size = elf_buckets[i];
  5452.           if (nsyms < elf_buckets[i + 1])
  5453.             break;
  5454.         }
  5455.       if (gnu_hash && best_size < 2)
  5456.         best_size = 2;
  5457.     }
  5458.  
  5459.   return best_size;
  5460. }
  5461.  
  5462. /* Size any SHT_GROUP section for ld -r.  */
  5463.  
  5464. bfd_boolean
  5465. _bfd_elf_size_group_sections (struct bfd_link_info *info)
  5466. {
  5467.   bfd *ibfd;
  5468.  
  5469.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
  5470.     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  5471.         && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
  5472.       return FALSE;
  5473.   return TRUE;
  5474. }
  5475.  
  5476. /* Set a default stack segment size.  The value in INFO wins.  If it
  5477.    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
  5478.    undefined it is initialized.  */
  5479.  
  5480. bfd_boolean
  5481. bfd_elf_stack_segment_size (bfd *output_bfd,
  5482.                             struct bfd_link_info *info,
  5483.                             const char *legacy_symbol,
  5484.                             bfd_vma default_size)
  5485. {
  5486.   struct elf_link_hash_entry *h = NULL;
  5487.  
  5488.   /* Look for legacy symbol.  */
  5489.   if (legacy_symbol)
  5490.     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
  5491.                               FALSE, FALSE, FALSE);
  5492.   if (h && (h->root.type == bfd_link_hash_defined
  5493.             || h->root.type == bfd_link_hash_defweak)
  5494.       && h->def_regular
  5495.       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
  5496.     {
  5497.       /* The symbol has no type if specified on the command line.  */
  5498.       h->type = STT_OBJECT;
  5499.       if (info->stacksize)
  5500.         (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
  5501.                                output_bfd, legacy_symbol);
  5502.       else if (h->root.u.def.section != bfd_abs_section_ptr)
  5503.         (*_bfd_error_handler) (_("%B: %s not absolute"),
  5504.                                output_bfd, legacy_symbol);
  5505.       else
  5506.         info->stacksize = h->root.u.def.value;
  5507.     }
  5508.  
  5509.   if (!info->stacksize)
  5510.     /* If the user didn't set a size, or explicitly inhibit the
  5511.        size, set it now.  */
  5512.     info->stacksize = default_size;
  5513.  
  5514.   /* Provide the legacy symbol, if it is referenced.  */
  5515.   if (h && (h->root.type == bfd_link_hash_undefined
  5516.             || h->root.type == bfd_link_hash_undefweak))
  5517.     {
  5518.       struct bfd_link_hash_entry *bh = NULL;
  5519.  
  5520.       if (!(_bfd_generic_link_add_one_symbol
  5521.             (info, output_bfd, legacy_symbol,
  5522.              BSF_GLOBAL, bfd_abs_section_ptr,
  5523.              info->stacksize >= 0 ? info->stacksize : 0,
  5524.              NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
  5525.         return FALSE;
  5526.  
  5527.       h = (struct elf_link_hash_entry *) bh;
  5528.       h->def_regular = 1;
  5529.       h->type = STT_OBJECT;
  5530.     }
  5531.  
  5532.   return TRUE;
  5533. }
  5534.  
  5535. /* Set up the sizes and contents of the ELF dynamic sections.  This is
  5536.    called by the ELF linker emulation before_allocation routine.  We
  5537.    must set the sizes of the sections before the linker sets the
  5538.    addresses of the various sections.  */
  5539.  
  5540. bfd_boolean
  5541. bfd_elf_size_dynamic_sections (bfd *output_bfd,
  5542.                                const char *soname,
  5543.                                const char *rpath,
  5544.                                const char *filter_shlib,
  5545.                                const char *audit,
  5546.                                const char *depaudit,
  5547.                                const char * const *auxiliary_filters,
  5548.                                struct bfd_link_info *info,
  5549.                                asection **sinterpptr)
  5550. {
  5551.   bfd_size_type soname_indx;
  5552.   bfd *dynobj;
  5553.   const struct elf_backend_data *bed;
  5554.   struct elf_info_failed asvinfo;
  5555.  
  5556.   *sinterpptr = NULL;
  5557.  
  5558.   soname_indx = (bfd_size_type) -1;
  5559.  
  5560.   if (!is_elf_hash_table (info->hash))
  5561.     return TRUE;
  5562.  
  5563.   bed = get_elf_backend_data (output_bfd);
  5564.  
  5565.   /* Any syms created from now on start with -1 in
  5566.      got.refcount/offset and plt.refcount/offset.  */
  5567.   elf_hash_table (info)->init_got_refcount
  5568.     = elf_hash_table (info)->init_got_offset;
  5569.   elf_hash_table (info)->init_plt_refcount
  5570.     = elf_hash_table (info)->init_plt_offset;
  5571.  
  5572.   if (info->relocatable
  5573.       && !_bfd_elf_size_group_sections (info))
  5574.     return FALSE;
  5575.  
  5576.   /* The backend may have to create some sections regardless of whether
  5577.      we're dynamic or not.  */
  5578.   if (bed->elf_backend_always_size_sections
  5579.       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
  5580.     return FALSE;
  5581.  
  5582.   /* Determine any GNU_STACK segment requirements, after the backend
  5583.      has had a chance to set a default segment size.  */
  5584.   if (info->execstack)
  5585.     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
  5586.   else if (info->noexecstack)
  5587.     elf_stack_flags (output_bfd) = PF_R | PF_W;
  5588.   else
  5589.     {
  5590.       bfd *inputobj;
  5591.       asection *notesec = NULL;
  5592.       int exec = 0;
  5593.  
  5594.       for (inputobj = info->input_bfds;
  5595.            inputobj;
  5596.            inputobj = inputobj->link_next)
  5597.         {
  5598.           asection *s;
  5599.  
  5600.           if (inputobj->flags
  5601.               & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
  5602.             continue;
  5603.           s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
  5604.           if (s)
  5605.             {
  5606.               if (s->flags & SEC_CODE)
  5607.                 exec = PF_X;
  5608.               notesec = s;
  5609.             }
  5610.           else if (bed->default_execstack)
  5611.             exec = PF_X;
  5612.         }
  5613.       if (notesec || info->stacksize > 0)
  5614.         elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
  5615.       if (notesec && exec && info->relocatable
  5616.           && notesec->output_section != bfd_abs_section_ptr)
  5617.         notesec->output_section->flags |= SEC_CODE;
  5618.     }
  5619.  
  5620.   dynobj = elf_hash_table (info)->dynobj;
  5621.  
  5622.   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  5623.     {
  5624.       struct elf_info_failed eif;
  5625.       struct elf_link_hash_entry *h;
  5626.       asection *dynstr;
  5627.       struct bfd_elf_version_tree *t;
  5628.       struct bfd_elf_version_expr *d;
  5629.       asection *s;
  5630.       bfd_boolean all_defined;
  5631.  
  5632.       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
  5633.       BFD_ASSERT (*sinterpptr != NULL || !info->executable);
  5634.  
  5635.       if (soname != NULL)
  5636.         {
  5637.           soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5638.                                              soname, TRUE);
  5639.           if (soname_indx == (bfd_size_type) -1
  5640.               || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
  5641.             return FALSE;
  5642.         }
  5643.  
  5644.       if (info->symbolic)
  5645.         {
  5646.           if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
  5647.             return FALSE;
  5648.           info->flags |= DF_SYMBOLIC;
  5649.         }
  5650.  
  5651.       if (rpath != NULL)
  5652.         {
  5653.           bfd_size_type indx;
  5654.           bfd_vma tag;
  5655.  
  5656.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
  5657.                                       TRUE);
  5658.           if (indx == (bfd_size_type) -1)
  5659.             return FALSE;
  5660.  
  5661.           tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
  5662.           if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
  5663.             return FALSE;
  5664.         }
  5665.  
  5666.       if (filter_shlib != NULL)
  5667.         {
  5668.           bfd_size_type indx;
  5669.  
  5670.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5671.                                       filter_shlib, TRUE);
  5672.           if (indx == (bfd_size_type) -1
  5673.               || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
  5674.             return FALSE;
  5675.         }
  5676.  
  5677.       if (auxiliary_filters != NULL)
  5678.         {
  5679.           const char * const *p;
  5680.  
  5681.           for (p = auxiliary_filters; *p != NULL; p++)
  5682.             {
  5683.               bfd_size_type indx;
  5684.  
  5685.               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5686.                                           *p, TRUE);
  5687.               if (indx == (bfd_size_type) -1
  5688.                   || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
  5689.                 return FALSE;
  5690.             }
  5691.         }
  5692.  
  5693.       if (audit != NULL)
  5694.         {
  5695.           bfd_size_type indx;
  5696.  
  5697.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
  5698.                                       TRUE);
  5699.           if (indx == (bfd_size_type) -1
  5700.               || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
  5701.             return FALSE;
  5702.         }
  5703.  
  5704.       if (depaudit != NULL)
  5705.         {
  5706.           bfd_size_type indx;
  5707.  
  5708.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
  5709.                                       TRUE);
  5710.           if (indx == (bfd_size_type) -1
  5711.               || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
  5712.             return FALSE;
  5713.         }
  5714.  
  5715.       eif.info = info;
  5716.       eif.failed = FALSE;
  5717.  
  5718.       /* If we are supposed to export all symbols into the dynamic symbol
  5719.          table (this is not the normal case), then do so.  */
  5720.       if (info->export_dynamic
  5721.           || (info->executable && info->dynamic))
  5722.         {
  5723.           elf_link_hash_traverse (elf_hash_table (info),
  5724.                                   _bfd_elf_export_symbol,
  5725.                                   &eif);
  5726.           if (eif.failed)
  5727.             return FALSE;
  5728.         }
  5729.  
  5730.       /* Make all global versions with definition.  */
  5731.       for (t = info->version_info; t != NULL; t = t->next)
  5732.         for (d = t->globals.list; d != NULL; d = d->next)
  5733.           if (!d->symver && d->literal)
  5734.             {
  5735.               const char *verstr, *name;
  5736.               size_t namelen, verlen, newlen;
  5737.               char *newname, *p, leading_char;
  5738.               struct elf_link_hash_entry *newh;
  5739.  
  5740.               leading_char = bfd_get_symbol_leading_char (output_bfd);
  5741.               name = d->pattern;
  5742.               namelen = strlen (name) + (leading_char != '\0');
  5743.               verstr = t->name;
  5744.               verlen = strlen (verstr);
  5745.               newlen = namelen + verlen + 3;
  5746.  
  5747.               newname = (char *) bfd_malloc (newlen);
  5748.               if (newname == NULL)
  5749.                 return FALSE;
  5750.               newname[0] = leading_char;
  5751.               memcpy (newname + (leading_char != '\0'), name, namelen);
  5752.  
  5753.               /* Check the hidden versioned definition.  */
  5754.               p = newname + namelen;
  5755.               *p++ = ELF_VER_CHR;
  5756.               memcpy (p, verstr, verlen + 1);
  5757.               newh = elf_link_hash_lookup (elf_hash_table (info),
  5758.                                            newname, FALSE, FALSE,
  5759.                                            FALSE);
  5760.               if (newh == NULL
  5761.                   || (newh->root.type != bfd_link_hash_defined
  5762.                       && newh->root.type != bfd_link_hash_defweak))
  5763.                 {
  5764.                   /* Check the default versioned definition.  */
  5765.                   *p++ = ELF_VER_CHR;
  5766.                   memcpy (p, verstr, verlen + 1);
  5767.                   newh = elf_link_hash_lookup (elf_hash_table (info),
  5768.                                                newname, FALSE, FALSE,
  5769.                                                FALSE);
  5770.                 }
  5771.               free (newname);
  5772.  
  5773.               /* Mark this version if there is a definition and it is
  5774.                  not defined in a shared object.  */
  5775.               if (newh != NULL
  5776.                   && !newh->def_dynamic
  5777.                   && (newh->root.type == bfd_link_hash_defined
  5778.                       || newh->root.type == bfd_link_hash_defweak))
  5779.                 d->symver = 1;
  5780.             }
  5781.  
  5782.       /* Attach all the symbols to their version information.  */
  5783.       asvinfo.info = info;
  5784.       asvinfo.failed = FALSE;
  5785.  
  5786.       elf_link_hash_traverse (elf_hash_table (info),
  5787.                               _bfd_elf_link_assign_sym_version,
  5788.                               &asvinfo);
  5789.       if (asvinfo.failed)
  5790.         return FALSE;
  5791.  
  5792.       if (!info->allow_undefined_version)
  5793.         {
  5794.           /* Check if all global versions have a definition.  */
  5795.           all_defined = TRUE;
  5796.           for (t = info->version_info; t != NULL; t = t->next)
  5797.             for (d = t->globals.list; d != NULL; d = d->next)
  5798.               if (d->literal && !d->symver && !d->script)
  5799.                 {
  5800.                   (*_bfd_error_handler)
  5801.                     (_("%s: undefined version: %s"),
  5802.                      d->pattern, t->name);
  5803.                   all_defined = FALSE;
  5804.                 }
  5805.  
  5806.           if (!all_defined)
  5807.             {
  5808.               bfd_set_error (bfd_error_bad_value);
  5809.               return FALSE;
  5810.             }
  5811.         }
  5812.  
  5813.       /* Find all symbols which were defined in a dynamic object and make
  5814.          the backend pick a reasonable value for them.  */
  5815.       elf_link_hash_traverse (elf_hash_table (info),
  5816.                               _bfd_elf_adjust_dynamic_symbol,
  5817.                               &eif);
  5818.       if (eif.failed)
  5819.         return FALSE;
  5820.  
  5821.       /* Add some entries to the .dynamic section.  We fill in some of the
  5822.          values later, in bfd_elf_final_link, but we must add the entries
  5823.          now so that we know the final size of the .dynamic section.  */
  5824.  
  5825.       /* If there are initialization and/or finalization functions to
  5826.          call then add the corresponding DT_INIT/DT_FINI entries.  */
  5827.       h = (info->init_function
  5828.            ? elf_link_hash_lookup (elf_hash_table (info),
  5829.                                    info->init_function, FALSE,
  5830.                                    FALSE, FALSE)
  5831.            : NULL);
  5832.       if (h != NULL
  5833.           && (h->ref_regular
  5834.               || h->def_regular))
  5835.         {
  5836.           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
  5837.             return FALSE;
  5838.         }
  5839.       h = (info->fini_function
  5840.            ? elf_link_hash_lookup (elf_hash_table (info),
  5841.                                    info->fini_function, FALSE,
  5842.                                    FALSE, FALSE)
  5843.            : NULL);
  5844.       if (h != NULL
  5845.           && (h->ref_regular
  5846.               || h->def_regular))
  5847.         {
  5848.           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
  5849.             return FALSE;
  5850.         }
  5851.  
  5852.       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
  5853.       if (s != NULL && s->linker_has_input)
  5854.         {
  5855.           /* DT_PREINIT_ARRAY is not allowed in shared library.  */
  5856.           if (! info->executable)
  5857.             {
  5858.               bfd *sub;
  5859.               asection *o;
  5860.  
  5861.               for (sub = info->input_bfds; sub != NULL;
  5862.                    sub = sub->link_next)
  5863.                 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
  5864.                   for (o = sub->sections; o != NULL; o = o->next)
  5865.                     if (elf_section_data (o)->this_hdr.sh_type
  5866.                         == SHT_PREINIT_ARRAY)
  5867.                       {
  5868.                         (*_bfd_error_handler)
  5869.                           (_("%B: .preinit_array section is not allowed in DSO"),
  5870.                            sub);
  5871.                         break;
  5872.                       }
  5873.  
  5874.               bfd_set_error (bfd_error_nonrepresentable_section);
  5875.               return FALSE;
  5876.             }
  5877.  
  5878.           if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
  5879.               || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
  5880.             return FALSE;
  5881.         }
  5882.       s = bfd_get_section_by_name (output_bfd, ".init_array");
  5883.       if (s != NULL && s->linker_has_input)
  5884.         {
  5885.           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
  5886.               || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
  5887.             return FALSE;
  5888.         }
  5889.       s = bfd_get_section_by_name (output_bfd, ".fini_array");
  5890.       if (s != NULL && s->linker_has_input)
  5891.         {
  5892.           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
  5893.               || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
  5894.             return FALSE;
  5895.         }
  5896.  
  5897.       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
  5898.       /* If .dynstr is excluded from the link, we don't want any of
  5899.          these tags.  Strictly, we should be checking each section
  5900.          individually;  This quick check covers for the case where
  5901.          someone does a /DISCARD/ : { *(*) }.  */
  5902.       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
  5903.         {
  5904.           bfd_size_type strsize;
  5905.  
  5906.           strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  5907.           if ((info->emit_hash
  5908.                && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
  5909.               || (info->emit_gnu_hash
  5910.                   && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
  5911.               || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
  5912.               || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
  5913.               || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
  5914.               || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
  5915.                                               bed->s->sizeof_sym))
  5916.             return FALSE;
  5917.         }
  5918.     }
  5919.  
  5920.   /* The backend must work out the sizes of all the other dynamic
  5921.      sections.  */
  5922.   if (dynobj != NULL
  5923.       && bed->elf_backend_size_dynamic_sections != NULL
  5924.       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
  5925.     return FALSE;
  5926.  
  5927.   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
  5928.     return FALSE;
  5929.  
  5930.   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  5931.     {
  5932.       unsigned long section_sym_count;
  5933.       struct bfd_elf_version_tree *verdefs;
  5934.       asection *s;
  5935.  
  5936.       /* Set up the version definition section.  */
  5937.       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  5938.       BFD_ASSERT (s != NULL);
  5939.  
  5940.       /* We may have created additional version definitions if we are
  5941.          just linking a regular application.  */
  5942.       verdefs = info->version_info;
  5943.  
  5944.       /* Skip anonymous version tag.  */
  5945.       if (verdefs != NULL && verdefs->vernum == 0)
  5946.         verdefs = verdefs->next;
  5947.  
  5948.       if (verdefs == NULL && !info->create_default_symver)
  5949.         s->flags |= SEC_EXCLUDE;
  5950.       else
  5951.         {
  5952.           unsigned int cdefs;
  5953.           bfd_size_type size;
  5954.           struct bfd_elf_version_tree *t;
  5955.           bfd_byte *p;
  5956.           Elf_Internal_Verdef def;
  5957.           Elf_Internal_Verdaux defaux;
  5958.           struct bfd_link_hash_entry *bh;
  5959.           struct elf_link_hash_entry *h;
  5960.           const char *name;
  5961.  
  5962.           cdefs = 0;
  5963.           size = 0;
  5964.  
  5965.           /* Make space for the base version.  */
  5966.           size += sizeof (Elf_External_Verdef);
  5967.           size += sizeof (Elf_External_Verdaux);
  5968.           ++cdefs;
  5969.  
  5970.           /* Make space for the default version.  */
  5971.           if (info->create_default_symver)
  5972.             {
  5973.               size += sizeof (Elf_External_Verdef);
  5974.               ++cdefs;
  5975.             }
  5976.  
  5977.           for (t = verdefs; t != NULL; t = t->next)
  5978.             {
  5979.               struct bfd_elf_version_deps *n;
  5980.  
  5981.               /* Don't emit base version twice.  */
  5982.               if (t->vernum == 0)
  5983.                 continue;
  5984.  
  5985.               size += sizeof (Elf_External_Verdef);
  5986.               size += sizeof (Elf_External_Verdaux);
  5987.               ++cdefs;
  5988.  
  5989.               for (n = t->deps; n != NULL; n = n->next)
  5990.                 size += sizeof (Elf_External_Verdaux);
  5991.             }
  5992.  
  5993.           s->size = size;
  5994.           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  5995.           if (s->contents == NULL && s->size != 0)
  5996.             return FALSE;
  5997.  
  5998.           /* Fill in the version definition section.  */
  5999.  
  6000.           p = s->contents;
  6001.  
  6002.           def.vd_version = VER_DEF_CURRENT;
  6003.           def.vd_flags = VER_FLG_BASE;
  6004.           def.vd_ndx = 1;
  6005.           def.vd_cnt = 1;
  6006.           if (info->create_default_symver)
  6007.             {
  6008.               def.vd_aux = 2 * sizeof (Elf_External_Verdef);
  6009.               def.vd_next = sizeof (Elf_External_Verdef);
  6010.             }
  6011.           else
  6012.             {
  6013.               def.vd_aux = sizeof (Elf_External_Verdef);
  6014.               def.vd_next = (sizeof (Elf_External_Verdef)
  6015.                              + sizeof (Elf_External_Verdaux));
  6016.             }
  6017.  
  6018.           if (soname_indx != (bfd_size_type) -1)
  6019.             {
  6020.               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6021.                                       soname_indx);
  6022.               def.vd_hash = bfd_elf_hash (soname);
  6023.               defaux.vda_name = soname_indx;
  6024.               name = soname;
  6025.             }
  6026.           else
  6027.             {
  6028.               bfd_size_type indx;
  6029.  
  6030.               name = lbasename (output_bfd->filename);
  6031.               def.vd_hash = bfd_elf_hash (name);
  6032.               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6033.                                           name, FALSE);
  6034.               if (indx == (bfd_size_type) -1)
  6035.                 return FALSE;
  6036.               defaux.vda_name = indx;
  6037.             }
  6038.           defaux.vda_next = 0;
  6039.  
  6040.           _bfd_elf_swap_verdef_out (output_bfd, &def,
  6041.                                     (Elf_External_Verdef *) p);
  6042.           p += sizeof (Elf_External_Verdef);
  6043.           if (info->create_default_symver)
  6044.             {
  6045.               /* Add a symbol representing this version.  */
  6046.               bh = NULL;
  6047.               if (! (_bfd_generic_link_add_one_symbol
  6048.                      (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
  6049.                       0, NULL, FALSE,
  6050.                       get_elf_backend_data (dynobj)->collect, &bh)))
  6051.                 return FALSE;
  6052.               h = (struct elf_link_hash_entry *) bh;
  6053.               h->non_elf = 0;
  6054.               h->def_regular = 1;
  6055.               h->type = STT_OBJECT;
  6056.               h->verinfo.vertree = NULL;
  6057.  
  6058.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6059.                 return FALSE;
  6060.  
  6061.               /* Create a duplicate of the base version with the same
  6062.                  aux block, but different flags.  */
  6063.               def.vd_flags = 0;
  6064.               def.vd_ndx = 2;
  6065.               def.vd_aux = sizeof (Elf_External_Verdef);
  6066.               if (verdefs)
  6067.                 def.vd_next = (sizeof (Elf_External_Verdef)
  6068.                                + sizeof (Elf_External_Verdaux));
  6069.               else
  6070.                 def.vd_next = 0;
  6071.               _bfd_elf_swap_verdef_out (output_bfd, &def,
  6072.                                         (Elf_External_Verdef *) p);
  6073.               p += sizeof (Elf_External_Verdef);
  6074.             }
  6075.           _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6076.                                      (Elf_External_Verdaux *) p);
  6077.           p += sizeof (Elf_External_Verdaux);
  6078.  
  6079.           for (t = verdefs; t != NULL; t = t->next)
  6080.             {
  6081.               unsigned int cdeps;
  6082.               struct bfd_elf_version_deps *n;
  6083.  
  6084.               /* Don't emit the base version twice.  */
  6085.               if (t->vernum == 0)
  6086.                 continue;
  6087.  
  6088.               cdeps = 0;
  6089.               for (n = t->deps; n != NULL; n = n->next)
  6090.                 ++cdeps;
  6091.  
  6092.               /* Add a symbol representing this version.  */
  6093.               bh = NULL;
  6094.               if (! (_bfd_generic_link_add_one_symbol
  6095.                      (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
  6096.                       0, NULL, FALSE,
  6097.                       get_elf_backend_data (dynobj)->collect, &bh)))
  6098.                 return FALSE;
  6099.               h = (struct elf_link_hash_entry *) bh;
  6100.               h->non_elf = 0;
  6101.               h->def_regular = 1;
  6102.               h->type = STT_OBJECT;
  6103.               h->verinfo.vertree = t;
  6104.  
  6105.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6106.                 return FALSE;
  6107.  
  6108.               def.vd_version = VER_DEF_CURRENT;
  6109.               def.vd_flags = 0;
  6110.               if (t->globals.list == NULL
  6111.                   && t->locals.list == NULL
  6112.                   && ! t->used)
  6113.                 def.vd_flags |= VER_FLG_WEAK;
  6114.               def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
  6115.               def.vd_cnt = cdeps + 1;
  6116.               def.vd_hash = bfd_elf_hash (t->name);
  6117.               def.vd_aux = sizeof (Elf_External_Verdef);
  6118.               def.vd_next = 0;
  6119.  
  6120.               /* If a basever node is next, it *must* be the last node in
  6121.                  the chain, otherwise Verdef construction breaks.  */
  6122.               if (t->next != NULL && t->next->vernum == 0)
  6123.                 BFD_ASSERT (t->next->next == NULL);
  6124.  
  6125.               if (t->next != NULL && t->next->vernum != 0)
  6126.                 def.vd_next = (sizeof (Elf_External_Verdef)
  6127.                                + (cdeps + 1) * sizeof (Elf_External_Verdaux));
  6128.  
  6129.               _bfd_elf_swap_verdef_out (output_bfd, &def,
  6130.                                         (Elf_External_Verdef *) p);
  6131.               p += sizeof (Elf_External_Verdef);
  6132.  
  6133.               defaux.vda_name = h->dynstr_index;
  6134.               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6135.                                       h->dynstr_index);
  6136.               defaux.vda_next = 0;
  6137.               if (t->deps != NULL)
  6138.                 defaux.vda_next = sizeof (Elf_External_Verdaux);
  6139.               t->name_indx = defaux.vda_name;
  6140.  
  6141.               _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6142.                                          (Elf_External_Verdaux *) p);
  6143.               p += sizeof (Elf_External_Verdaux);
  6144.  
  6145.               for (n = t->deps; n != NULL; n = n->next)
  6146.                 {
  6147.                   if (n->version_needed == NULL)
  6148.                     {
  6149.                       /* This can happen if there was an error in the
  6150.                          version script.  */
  6151.                       defaux.vda_name = 0;
  6152.                     }
  6153.                   else
  6154.                     {
  6155.                       defaux.vda_name = n->version_needed->name_indx;
  6156.                       _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6157.                                               defaux.vda_name);
  6158.                     }
  6159.                   if (n->next == NULL)
  6160.                     defaux.vda_next = 0;
  6161.                   else
  6162.                     defaux.vda_next = sizeof (Elf_External_Verdaux);
  6163.  
  6164.                   _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6165.                                              (Elf_External_Verdaux *) p);
  6166.                   p += sizeof (Elf_External_Verdaux);
  6167.                 }
  6168.             }
  6169.  
  6170.           if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
  6171.               || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
  6172.             return FALSE;
  6173.  
  6174.           elf_tdata (output_bfd)->cverdefs = cdefs;
  6175.         }
  6176.  
  6177.       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
  6178.         {
  6179.           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
  6180.             return FALSE;
  6181.         }
  6182.       else if (info->flags & DF_BIND_NOW)
  6183.         {
  6184.           if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
  6185.             return FALSE;
  6186.         }
  6187.  
  6188.       if (info->flags_1)
  6189.         {
  6190.           if (info->executable)
  6191.             info->flags_1 &= ~ (DF_1_INITFIRST
  6192.                                 | DF_1_NODELETE
  6193.                                 | DF_1_NOOPEN);
  6194.           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
  6195.             return FALSE;
  6196.         }
  6197.  
  6198.       /* Work out the size of the version reference section.  */
  6199.  
  6200.       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  6201.       BFD_ASSERT (s != NULL);
  6202.       {
  6203.         struct elf_find_verdep_info sinfo;
  6204.  
  6205.         sinfo.info = info;
  6206.         sinfo.vers = elf_tdata (output_bfd)->cverdefs;
  6207.         if (sinfo.vers == 0)
  6208.           sinfo.vers = 1;
  6209.         sinfo.failed = FALSE;
  6210.  
  6211.         elf_link_hash_traverse (elf_hash_table (info),
  6212.                                 _bfd_elf_link_find_version_dependencies,
  6213.                                 &sinfo);
  6214.         if (sinfo.failed)
  6215.           return FALSE;
  6216.  
  6217.         if (elf_tdata (output_bfd)->verref == NULL)
  6218.           s->flags |= SEC_EXCLUDE;
  6219.         else
  6220.           {
  6221.             Elf_Internal_Verneed *t;
  6222.             unsigned int size;
  6223.             unsigned int crefs;
  6224.             bfd_byte *p;
  6225.  
  6226.             /* Build the version dependency section.  */
  6227.             size = 0;
  6228.             crefs = 0;
  6229.             for (t = elf_tdata (output_bfd)->verref;
  6230.                  t != NULL;
  6231.                  t = t->vn_nextref)
  6232.               {
  6233.                 Elf_Internal_Vernaux *a;
  6234.  
  6235.                 size += sizeof (Elf_External_Verneed);
  6236.                 ++crefs;
  6237.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6238.                   size += sizeof (Elf_External_Vernaux);
  6239.               }
  6240.  
  6241.             s->size = size;
  6242.             s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6243.             if (s->contents == NULL)
  6244.               return FALSE;
  6245.  
  6246.             p = s->contents;
  6247.             for (t = elf_tdata (output_bfd)->verref;
  6248.                  t != NULL;
  6249.                  t = t->vn_nextref)
  6250.               {
  6251.                 unsigned int caux;
  6252.                 Elf_Internal_Vernaux *a;
  6253.                 bfd_size_type indx;
  6254.  
  6255.                 caux = 0;
  6256.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6257.                   ++caux;
  6258.  
  6259.                 t->vn_version = VER_NEED_CURRENT;
  6260.                 t->vn_cnt = caux;
  6261.                 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6262.                                             elf_dt_name (t->vn_bfd) != NULL
  6263.                                             ? elf_dt_name (t->vn_bfd)
  6264.                                             : lbasename (t->vn_bfd->filename),
  6265.                                             FALSE);
  6266.                 if (indx == (bfd_size_type) -1)
  6267.                   return FALSE;
  6268.                 t->vn_file = indx;
  6269.                 t->vn_aux = sizeof (Elf_External_Verneed);
  6270.                 if (t->vn_nextref == NULL)
  6271.                   t->vn_next = 0;
  6272.                 else
  6273.                   t->vn_next = (sizeof (Elf_External_Verneed)
  6274.                                 + caux * sizeof (Elf_External_Vernaux));
  6275.  
  6276.                 _bfd_elf_swap_verneed_out (output_bfd, t,
  6277.                                            (Elf_External_Verneed *) p);
  6278.                 p += sizeof (Elf_External_Verneed);
  6279.  
  6280.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6281.                   {
  6282.                     a->vna_hash = bfd_elf_hash (a->vna_nodename);
  6283.                     indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6284.                                                 a->vna_nodename, FALSE);
  6285.                     if (indx == (bfd_size_type) -1)
  6286.                       return FALSE;
  6287.                     a->vna_name = indx;
  6288.                     if (a->vna_nextptr == NULL)
  6289.                       a->vna_next = 0;
  6290.                     else
  6291.                       a->vna_next = sizeof (Elf_External_Vernaux);
  6292.  
  6293.                     _bfd_elf_swap_vernaux_out (output_bfd, a,
  6294.                                                (Elf_External_Vernaux *) p);
  6295.                     p += sizeof (Elf_External_Vernaux);
  6296.                   }
  6297.               }
  6298.  
  6299.             if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
  6300.                 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
  6301.               return FALSE;
  6302.  
  6303.             elf_tdata (output_bfd)->cverrefs = crefs;
  6304.           }
  6305.       }
  6306.  
  6307.       if ((elf_tdata (output_bfd)->cverrefs == 0
  6308.            && elf_tdata (output_bfd)->cverdefs == 0)
  6309.           || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
  6310.                                              &section_sym_count) == 0)
  6311.         {
  6312.           s = bfd_get_linker_section (dynobj, ".gnu.version");
  6313.           s->flags |= SEC_EXCLUDE;
  6314.         }
  6315.     }
  6316.   return TRUE;
  6317. }
  6318.  
  6319. /* Find the first non-excluded output section.  We'll use its
  6320.    section symbol for some emitted relocs.  */
  6321. void
  6322. _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
  6323. {
  6324.   asection *s;
  6325.  
  6326.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6327.     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
  6328.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6329.       {
  6330.         elf_hash_table (info)->text_index_section = s;
  6331.         break;
  6332.       }
  6333. }
  6334.  
  6335. /* Find two non-excluded output sections, one for code, one for data.
  6336.    We'll use their section symbols for some emitted relocs.  */
  6337. void
  6338. _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
  6339. {
  6340.   asection *s;
  6341.  
  6342.   /* Data first, since setting text_index_section changes
  6343.      _bfd_elf_link_omit_section_dynsym.  */
  6344.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6345.     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
  6346.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6347.       {
  6348.         elf_hash_table (info)->data_index_section = s;
  6349.         break;
  6350.       }
  6351.  
  6352.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6353.     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
  6354.          == (SEC_ALLOC | SEC_READONLY))
  6355.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6356.       {
  6357.         elf_hash_table (info)->text_index_section = s;
  6358.         break;
  6359.       }
  6360.  
  6361.   if (elf_hash_table (info)->text_index_section == NULL)
  6362.     elf_hash_table (info)->text_index_section
  6363.       = elf_hash_table (info)->data_index_section;
  6364. }
  6365.  
  6366. bfd_boolean
  6367. bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  6368. {
  6369.   const struct elf_backend_data *bed;
  6370.  
  6371.   if (!is_elf_hash_table (info->hash))
  6372.     return TRUE;
  6373.  
  6374.   bed = get_elf_backend_data (output_bfd);
  6375.   (*bed->elf_backend_init_index_section) (output_bfd, info);
  6376.  
  6377.   if (elf_hash_table (info)->dynamic_sections_created)
  6378.     {
  6379.       bfd *dynobj;
  6380.       asection *s;
  6381.       bfd_size_type dynsymcount;
  6382.       unsigned long section_sym_count;
  6383.       unsigned int dtagcount;
  6384.  
  6385.       dynobj = elf_hash_table (info)->dynobj;
  6386.  
  6387.       /* Assign dynsym indicies.  In a shared library we generate a
  6388.          section symbol for each output section, which come first.
  6389.          Next come all of the back-end allocated local dynamic syms,
  6390.          followed by the rest of the global symbols.  */
  6391.  
  6392.       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
  6393.                                                     &section_sym_count);
  6394.  
  6395.       /* Work out the size of the symbol version section.  */
  6396.       s = bfd_get_linker_section (dynobj, ".gnu.version");
  6397.       BFD_ASSERT (s != NULL);
  6398.       if (dynsymcount != 0
  6399.           && (s->flags & SEC_EXCLUDE) == 0)
  6400.         {
  6401.           s->size = dynsymcount * sizeof (Elf_External_Versym);
  6402.           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6403.           if (s->contents == NULL)
  6404.             return FALSE;
  6405.  
  6406.           if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
  6407.             return FALSE;
  6408.         }
  6409.  
  6410.       /* Set the size of the .dynsym and .hash sections.  We counted
  6411.          the number of dynamic symbols in elf_link_add_object_symbols.
  6412.          We will build the contents of .dynsym and .hash when we build
  6413.          the final symbol table, because until then we do not know the
  6414.          correct value to give the symbols.  We built the .dynstr
  6415.          section as we went along in elf_link_add_object_symbols.  */
  6416.       s = bfd_get_linker_section (dynobj, ".dynsym");
  6417.       BFD_ASSERT (s != NULL);
  6418.       s->size = dynsymcount * bed->s->sizeof_sym;
  6419.  
  6420.       if (dynsymcount != 0)
  6421.         {
  6422.           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6423.           if (s->contents == NULL)
  6424.             return FALSE;
  6425.  
  6426.           /* The first entry in .dynsym is a dummy symbol.
  6427.              Clear all the section syms, in case we don't output them all.  */
  6428.           ++section_sym_count;
  6429.           memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
  6430.         }
  6431.  
  6432.       elf_hash_table (info)->bucketcount = 0;
  6433.  
  6434.       /* Compute the size of the hashing table.  As a side effect this
  6435.          computes the hash values for all the names we export.  */
  6436.       if (info->emit_hash)
  6437.         {
  6438.           unsigned long int *hashcodes;
  6439.           struct hash_codes_info hashinf;
  6440.           bfd_size_type amt;
  6441.           unsigned long int nsyms;
  6442.           size_t bucketcount;
  6443.           size_t hash_entry_size;
  6444.  
  6445.           /* Compute the hash values for all exported symbols.  At the same
  6446.              time store the values in an array so that we could use them for
  6447.              optimizations.  */
  6448.           amt = dynsymcount * sizeof (unsigned long int);
  6449.           hashcodes = (unsigned long int *) bfd_malloc (amt);
  6450.           if (hashcodes == NULL)
  6451.             return FALSE;
  6452.           hashinf.hashcodes = hashcodes;
  6453.           hashinf.error = FALSE;
  6454.  
  6455.           /* Put all hash values in HASHCODES.  */
  6456.           elf_link_hash_traverse (elf_hash_table (info),
  6457.                                   elf_collect_hash_codes, &hashinf);
  6458.           if (hashinf.error)
  6459.             {
  6460.               free (hashcodes);
  6461.               return FALSE;
  6462.             }
  6463.  
  6464.           nsyms = hashinf.hashcodes - hashcodes;
  6465.           bucketcount
  6466.             = compute_bucket_count (info, hashcodes, nsyms, 0);
  6467.           free (hashcodes);
  6468.  
  6469.           if (bucketcount == 0)
  6470.             return FALSE;
  6471.  
  6472.           elf_hash_table (info)->bucketcount = bucketcount;
  6473.  
  6474.           s = bfd_get_linker_section (dynobj, ".hash");
  6475.           BFD_ASSERT (s != NULL);
  6476.           hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
  6477.           s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
  6478.           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6479.           if (s->contents == NULL)
  6480.             return FALSE;
  6481.  
  6482.           bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
  6483.           bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
  6484.                    s->contents + hash_entry_size);
  6485.         }
  6486.  
  6487.       if (info->emit_gnu_hash)
  6488.         {
  6489.           size_t i, cnt;
  6490.           unsigned char *contents;
  6491.           struct collect_gnu_hash_codes cinfo;
  6492.           bfd_size_type amt;
  6493.           size_t bucketcount;
  6494.  
  6495.           memset (&cinfo, 0, sizeof (cinfo));
  6496.  
  6497.           /* Compute the hash values for all exported symbols.  At the same
  6498.              time store the values in an array so that we could use them for
  6499.              optimizations.  */
  6500.           amt = dynsymcount * 2 * sizeof (unsigned long int);
  6501.           cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
  6502.           if (cinfo.hashcodes == NULL)
  6503.             return FALSE;
  6504.  
  6505.           cinfo.hashval = cinfo.hashcodes + dynsymcount;
  6506.           cinfo.min_dynindx = -1;
  6507.           cinfo.output_bfd = output_bfd;
  6508.           cinfo.bed = bed;
  6509.  
  6510.           /* Put all hash values in HASHCODES.  */
  6511.           elf_link_hash_traverse (elf_hash_table (info),
  6512.                                   elf_collect_gnu_hash_codes, &cinfo);
  6513.           if (cinfo.error)
  6514.             {
  6515.               free (cinfo.hashcodes);
  6516.               return FALSE;
  6517.             }
  6518.  
  6519.           bucketcount
  6520.             = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
  6521.  
  6522.           if (bucketcount == 0)
  6523.             {
  6524.               free (cinfo.hashcodes);
  6525.               return FALSE;
  6526.             }
  6527.  
  6528.           s = bfd_get_linker_section (dynobj, ".gnu.hash");
  6529.           BFD_ASSERT (s != NULL);
  6530.  
  6531.           if (cinfo.nsyms == 0)
  6532.             {
  6533.               /* Empty .gnu.hash section is special.  */
  6534.               BFD_ASSERT (cinfo.min_dynindx == -1);
  6535.               free (cinfo.hashcodes);
  6536.               s->size = 5 * 4 + bed->s->arch_size / 8;
  6537.               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6538.               if (contents == NULL)
  6539.                 return FALSE;
  6540.               s->contents = contents;
  6541.               /* 1 empty bucket.  */
  6542.               bfd_put_32 (output_bfd, 1, contents);
  6543.               /* SYMIDX above the special symbol 0.  */
  6544.               bfd_put_32 (output_bfd, 1, contents + 4);
  6545.               /* Just one word for bitmask.  */
  6546.               bfd_put_32 (output_bfd, 1, contents + 8);
  6547.               /* Only hash fn bloom filter.  */
  6548.               bfd_put_32 (output_bfd, 0, contents + 12);
  6549.               /* No hashes are valid - empty bitmask.  */
  6550.               bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
  6551.               /* No hashes in the only bucket.  */
  6552.               bfd_put_32 (output_bfd, 0,
  6553.                           contents + 16 + bed->s->arch_size / 8);
  6554.             }
  6555.           else
  6556.             {
  6557.               unsigned long int maskwords, maskbitslog2, x;
  6558.               BFD_ASSERT (cinfo.min_dynindx != -1);
  6559.  
  6560.               x = cinfo.nsyms;
  6561.               maskbitslog2 = 1;
  6562.               while ((x >>= 1) != 0)
  6563.                 ++maskbitslog2;
  6564.               if (maskbitslog2 < 3)
  6565.                 maskbitslog2 = 5;
  6566.               else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
  6567.                 maskbitslog2 = maskbitslog2 + 3;
  6568.               else
  6569.                 maskbitslog2 = maskbitslog2 + 2;
  6570.               if (bed->s->arch_size == 64)
  6571.                 {
  6572.                   if (maskbitslog2 == 5)
  6573.                     maskbitslog2 = 6;
  6574.                   cinfo.shift1 = 6;
  6575.                 }
  6576.               else
  6577.                 cinfo.shift1 = 5;
  6578.               cinfo.mask = (1 << cinfo.shift1) - 1;
  6579.               cinfo.shift2 = maskbitslog2;
  6580.               cinfo.maskbits = 1 << maskbitslog2;
  6581.               maskwords = 1 << (maskbitslog2 - cinfo.shift1);
  6582.               amt = bucketcount * sizeof (unsigned long int) * 2;
  6583.               amt += maskwords * sizeof (bfd_vma);
  6584.               cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
  6585.               if (cinfo.bitmask == NULL)
  6586.                 {
  6587.                   free (cinfo.hashcodes);
  6588.                   return FALSE;
  6589.                 }
  6590.  
  6591.               cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
  6592.               cinfo.indx = cinfo.counts + bucketcount;
  6593.               cinfo.symindx = dynsymcount - cinfo.nsyms;
  6594.               memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
  6595.  
  6596.               /* Determine how often each hash bucket is used.  */
  6597.               memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
  6598.               for (i = 0; i < cinfo.nsyms; ++i)
  6599.                 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
  6600.  
  6601.               for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
  6602.                 if (cinfo.counts[i] != 0)
  6603.                   {
  6604.                     cinfo.indx[i] = cnt;
  6605.                     cnt += cinfo.counts[i];
  6606.                   }
  6607.               BFD_ASSERT (cnt == dynsymcount);
  6608.               cinfo.bucketcount = bucketcount;
  6609.               cinfo.local_indx = cinfo.min_dynindx;
  6610.  
  6611.               s->size = (4 + bucketcount + cinfo.nsyms) * 4;
  6612.               s->size += cinfo.maskbits / 8;
  6613.               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6614.               if (contents == NULL)
  6615.                 {
  6616.                   free (cinfo.bitmask);
  6617.                   free (cinfo.hashcodes);
  6618.                   return FALSE;
  6619.                 }
  6620.  
  6621.               s->contents = contents;
  6622.               bfd_put_32 (output_bfd, bucketcount, contents);
  6623.               bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
  6624.               bfd_put_32 (output_bfd, maskwords, contents + 8);
  6625.               bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
  6626.               contents += 16 + cinfo.maskbits / 8;
  6627.  
  6628.               for (i = 0; i < bucketcount; ++i)
  6629.                 {
  6630.                   if (cinfo.counts[i] == 0)
  6631.                     bfd_put_32 (output_bfd, 0, contents);
  6632.                   else
  6633.                     bfd_put_32 (output_bfd, cinfo.indx[i], contents);
  6634.                   contents += 4;
  6635.                 }
  6636.  
  6637.               cinfo.contents = contents;
  6638.  
  6639.               /* Renumber dynamic symbols, populate .gnu.hash section.  */
  6640.               elf_link_hash_traverse (elf_hash_table (info),
  6641.                                       elf_renumber_gnu_hash_syms, &cinfo);
  6642.  
  6643.               contents = s->contents + 16;
  6644.               for (i = 0; i < maskwords; ++i)
  6645.                 {
  6646.                   bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
  6647.                            contents);
  6648.                   contents += bed->s->arch_size / 8;
  6649.                 }
  6650.  
  6651.               free (cinfo.bitmask);
  6652.               free (cinfo.hashcodes);
  6653.             }
  6654.         }
  6655.  
  6656.       s = bfd_get_linker_section (dynobj, ".dynstr");
  6657.       BFD_ASSERT (s != NULL);
  6658.  
  6659.       elf_finalize_dynstr (output_bfd, info);
  6660.  
  6661.       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  6662.  
  6663.       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
  6664.         if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
  6665.           return FALSE;
  6666.     }
  6667.  
  6668.   return TRUE;
  6669. }
  6670. /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
  6671.  
  6672. static void
  6673. merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
  6674.                             asection *sec)
  6675. {
  6676.   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
  6677.   sec->sec_info_type = SEC_INFO_TYPE_NONE;
  6678. }
  6679.  
  6680. /* Finish SHF_MERGE section merging.  */
  6681.  
  6682. bfd_boolean
  6683. _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
  6684. {
  6685.   bfd *ibfd;
  6686.   asection *sec;
  6687.  
  6688.   if (!is_elf_hash_table (info->hash))
  6689.     return FALSE;
  6690.  
  6691.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
  6692.     if ((ibfd->flags & DYNAMIC) == 0)
  6693.       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  6694.         if ((sec->flags & SEC_MERGE) != 0
  6695.             && !bfd_is_abs_section (sec->output_section))
  6696.           {
  6697.             struct bfd_elf_section_data *secdata;
  6698.  
  6699.             secdata = elf_section_data (sec);
  6700.             if (! _bfd_add_merge_section (abfd,
  6701.                                           &elf_hash_table (info)->merge_info,
  6702.                                           sec, &secdata->sec_info))
  6703.               return FALSE;
  6704.             else if (secdata->sec_info)
  6705.               sec->sec_info_type = SEC_INFO_TYPE_MERGE;
  6706.           }
  6707.  
  6708.   if (elf_hash_table (info)->merge_info != NULL)
  6709.     _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
  6710.                          merge_sections_remove_hook);
  6711.   return TRUE;
  6712. }
  6713.  
  6714. /* Create an entry in an ELF linker hash table.  */
  6715.  
  6716. struct bfd_hash_entry *
  6717. _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  6718.                             struct bfd_hash_table *table,
  6719.                             const char *string)
  6720. {
  6721.   /* Allocate the structure if it has not already been allocated by a
  6722.      subclass.  */
  6723.   if (entry == NULL)
  6724.     {
  6725.       entry = (struct bfd_hash_entry *)
  6726.           bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
  6727.       if (entry == NULL)
  6728.         return entry;
  6729.     }
  6730.  
  6731.   /* Call the allocation method of the superclass.  */
  6732.   entry = _bfd_link_hash_newfunc (entry, table, string);
  6733.   if (entry != NULL)
  6734.     {
  6735.       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
  6736.       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
  6737.  
  6738.       /* Set local fields.  */
  6739.       ret->indx = -1;
  6740.       ret->dynindx = -1;
  6741.       ret->got = htab->init_got_refcount;
  6742.       ret->plt = htab->init_plt_refcount;
  6743.       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
  6744.                               - offsetof (struct elf_link_hash_entry, size)));
  6745.       /* Assume that we have been called by a non-ELF symbol reader.
  6746.          This flag is then reset by the code which reads an ELF input
  6747.          file.  This ensures that a symbol created by a non-ELF symbol
  6748.          reader will have the flag set correctly.  */
  6749.       ret->non_elf = 1;
  6750.     }
  6751.  
  6752.   return entry;
  6753. }
  6754.  
  6755. /* Copy data from an indirect symbol to its direct symbol, hiding the
  6756.    old indirect symbol.  Also used for copying flags to a weakdef.  */
  6757.  
  6758. void
  6759. _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
  6760.                                   struct elf_link_hash_entry *dir,
  6761.                                   struct elf_link_hash_entry *ind)
  6762. {
  6763.   struct elf_link_hash_table *htab;
  6764.  
  6765.   /* Copy down any references that we may have already seen to the
  6766.      symbol which just became indirect.  */
  6767.  
  6768.   dir->ref_dynamic |= ind->ref_dynamic;
  6769.   dir->ref_regular |= ind->ref_regular;
  6770.   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  6771.   dir->non_got_ref |= ind->non_got_ref;
  6772.   dir->needs_plt |= ind->needs_plt;
  6773.   dir->pointer_equality_needed |= ind->pointer_equality_needed;
  6774.  
  6775.   if (ind->root.type != bfd_link_hash_indirect)
  6776.     return;
  6777.  
  6778.   /* Copy over the global and procedure linkage table refcount entries.
  6779.      These may have been already set up by a check_relocs routine.  */
  6780.   htab = elf_hash_table (info);
  6781.   if (ind->got.refcount > htab->init_got_refcount.refcount)
  6782.     {
  6783.       if (dir->got.refcount < 0)
  6784.         dir->got.refcount = 0;
  6785.       dir->got.refcount += ind->got.refcount;
  6786.       ind->got.refcount = htab->init_got_refcount.refcount;
  6787.     }
  6788.  
  6789.   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
  6790.     {
  6791.       if (dir->plt.refcount < 0)
  6792.         dir->plt.refcount = 0;
  6793.       dir->plt.refcount += ind->plt.refcount;
  6794.       ind->plt.refcount = htab->init_plt_refcount.refcount;
  6795.     }
  6796.  
  6797.   if (ind->dynindx != -1)
  6798.     {
  6799.       if (dir->dynindx != -1)
  6800.         _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
  6801.       dir->dynindx = ind->dynindx;
  6802.       dir->dynstr_index = ind->dynstr_index;
  6803.       ind->dynindx = -1;
  6804.       ind->dynstr_index = 0;
  6805.     }
  6806. }
  6807.  
  6808. void
  6809. _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
  6810.                                 struct elf_link_hash_entry *h,
  6811.                                 bfd_boolean force_local)
  6812. {
  6813.   /* STT_GNU_IFUNC symbol must go through PLT.  */
  6814.   if (h->type != STT_GNU_IFUNC)
  6815.     {
  6816.       h->plt = elf_hash_table (info)->init_plt_offset;
  6817.       h->needs_plt = 0;
  6818.     }
  6819.   if (force_local)
  6820.     {
  6821.       h->forced_local = 1;
  6822.       if (h->dynindx != -1)
  6823.         {
  6824.           h->dynindx = -1;
  6825.           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  6826.                                   h->dynstr_index);
  6827.         }
  6828.     }
  6829. }
  6830.  
  6831. /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
  6832.    caller.  */
  6833.  
  6834. bfd_boolean
  6835. _bfd_elf_link_hash_table_init
  6836.   (struct elf_link_hash_table *table,
  6837.    bfd *abfd,
  6838.    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  6839.                                       struct bfd_hash_table *,
  6840.                                       const char *),
  6841.    unsigned int entsize,
  6842.    enum elf_target_id target_id)
  6843. {
  6844.   bfd_boolean ret;
  6845.   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
  6846.  
  6847.   table->init_got_refcount.refcount = can_refcount - 1;
  6848.   table->init_plt_refcount.refcount = can_refcount - 1;
  6849.   table->init_got_offset.offset = -(bfd_vma) 1;
  6850.   table->init_plt_offset.offset = -(bfd_vma) 1;
  6851.   /* The first dynamic symbol is a dummy.  */
  6852.   table->dynsymcount = 1;
  6853.  
  6854.   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  6855.  
  6856.   table->root.type = bfd_link_elf_hash_table;
  6857.   table->hash_table_id = target_id;
  6858.  
  6859.   return ret;
  6860. }
  6861.  
  6862. /* Create an ELF linker hash table.  */
  6863.  
  6864. struct bfd_link_hash_table *
  6865. _bfd_elf_link_hash_table_create (bfd *abfd)
  6866. {
  6867.   struct elf_link_hash_table *ret;
  6868.   bfd_size_type amt = sizeof (struct elf_link_hash_table);
  6869.  
  6870.   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
  6871.   if (ret == NULL)
  6872.     return NULL;
  6873.  
  6874.   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
  6875.                                        sizeof (struct elf_link_hash_entry),
  6876.                                        GENERIC_ELF_DATA))
  6877.     {
  6878.       free (ret);
  6879.       return NULL;
  6880.     }
  6881.  
  6882.   return &ret->root;
  6883. }
  6884.  
  6885. /* Destroy an ELF linker hash table.  */
  6886.  
  6887. void
  6888. _bfd_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
  6889. {
  6890.   struct elf_link_hash_table *htab = (struct elf_link_hash_table *) hash;
  6891.   if (htab->dynstr != NULL)
  6892.     _bfd_elf_strtab_free (htab->dynstr);
  6893.   _bfd_merge_sections_free (htab->merge_info);
  6894.   _bfd_generic_link_hash_table_free (hash);
  6895. }
  6896.  
  6897. /* This is a hook for the ELF emulation code in the generic linker to
  6898.    tell the backend linker what file name to use for the DT_NEEDED
  6899.    entry for a dynamic object.  */
  6900.  
  6901. void
  6902. bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
  6903. {
  6904.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  6905.       && bfd_get_format (abfd) == bfd_object)
  6906.     elf_dt_name (abfd) = name;
  6907. }
  6908.  
  6909. int
  6910. bfd_elf_get_dyn_lib_class (bfd *abfd)
  6911. {
  6912.   int lib_class;
  6913.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  6914.       && bfd_get_format (abfd) == bfd_object)
  6915.     lib_class = elf_dyn_lib_class (abfd);
  6916.   else
  6917.     lib_class = 0;
  6918.   return lib_class;
  6919. }
  6920.  
  6921. void
  6922. bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
  6923. {
  6924.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  6925.       && bfd_get_format (abfd) == bfd_object)
  6926.     elf_dyn_lib_class (abfd) = lib_class;
  6927. }
  6928.  
  6929. /* Get the list of DT_NEEDED entries for a link.  This is a hook for
  6930.    the linker ELF emulation code.  */
  6931.  
  6932. struct bfd_link_needed_list *
  6933. bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
  6934.                          struct bfd_link_info *info)
  6935. {
  6936.   if (! is_elf_hash_table (info->hash))
  6937.     return NULL;
  6938.   return elf_hash_table (info)->needed;
  6939. }
  6940.  
  6941. /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
  6942.    hook for the linker ELF emulation code.  */
  6943.  
  6944. struct bfd_link_needed_list *
  6945. bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
  6946.                           struct bfd_link_info *info)
  6947. {
  6948.   if (! is_elf_hash_table (info->hash))
  6949.     return NULL;
  6950.   return elf_hash_table (info)->runpath;
  6951. }
  6952.  
  6953. /* Get the name actually used for a dynamic object for a link.  This
  6954.    is the SONAME entry if there is one.  Otherwise, it is the string
  6955.    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
  6956.  
  6957. const char *
  6958. bfd_elf_get_dt_soname (bfd *abfd)
  6959. {
  6960.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  6961.       && bfd_get_format (abfd) == bfd_object)
  6962.     return elf_dt_name (abfd);
  6963.   return NULL;
  6964. }
  6965.  
  6966. /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
  6967.    the ELF linker emulation code.  */
  6968.  
  6969. bfd_boolean
  6970. bfd_elf_get_bfd_needed_list (bfd *abfd,
  6971.                              struct bfd_link_needed_list **pneeded)
  6972. {
  6973.   asection *s;
  6974.   bfd_byte *dynbuf = NULL;
  6975.   unsigned int elfsec;
  6976.   unsigned long shlink;
  6977.   bfd_byte *extdyn, *extdynend;
  6978.   size_t extdynsize;
  6979.   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  6980.  
  6981.   *pneeded = NULL;
  6982.  
  6983.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
  6984.       || bfd_get_format (abfd) != bfd_object)
  6985.     return TRUE;
  6986.  
  6987.   s = bfd_get_section_by_name (abfd, ".dynamic");
  6988.   if (s == NULL || s->size == 0)
  6989.     return TRUE;
  6990.  
  6991.   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  6992.     goto error_return;
  6993.  
  6994.   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  6995.   if (elfsec == SHN_BAD)
  6996.     goto error_return;
  6997.  
  6998.   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  6999.  
  7000.   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  7001.   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  7002.  
  7003.   extdyn = dynbuf;
  7004.   extdynend = extdyn + s->size;
  7005.   for (; extdyn < extdynend; extdyn += extdynsize)
  7006.     {
  7007.       Elf_Internal_Dyn dyn;
  7008.  
  7009.       (*swap_dyn_in) (abfd, extdyn, &dyn);
  7010.  
  7011.       if (dyn.d_tag == DT_NULL)
  7012.         break;
  7013.  
  7014.       if (dyn.d_tag == DT_NEEDED)
  7015.         {
  7016.           const char *string;
  7017.           struct bfd_link_needed_list *l;
  7018.           unsigned int tagv = dyn.d_un.d_val;
  7019.           bfd_size_type amt;
  7020.  
  7021.           string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  7022.           if (string == NULL)
  7023.             goto error_return;
  7024.  
  7025.           amt = sizeof *l;
  7026.           l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  7027.           if (l == NULL)
  7028.             goto error_return;
  7029.  
  7030.           l->by = abfd;
  7031.           l->name = string;
  7032.           l->next = *pneeded;
  7033.           *pneeded = l;
  7034.         }
  7035.     }
  7036.  
  7037.   free (dynbuf);
  7038.  
  7039.   return TRUE;
  7040.  
  7041.  error_return:
  7042.   if (dynbuf != NULL)
  7043.     free (dynbuf);
  7044.   return FALSE;
  7045. }
  7046.  
  7047. struct elf_symbuf_symbol
  7048. {
  7049.   unsigned long st_name;        /* Symbol name, index in string tbl */
  7050.   unsigned char st_info;        /* Type and binding attributes */
  7051.   unsigned char st_other;       /* Visibilty, and target specific */
  7052. };
  7053.  
  7054. struct elf_symbuf_head
  7055. {
  7056.   struct elf_symbuf_symbol *ssym;
  7057.   bfd_size_type count;
  7058.   unsigned int st_shndx;
  7059. };
  7060.  
  7061. struct elf_symbol
  7062. {
  7063.   union
  7064.     {
  7065.       Elf_Internal_Sym *isym;
  7066.       struct elf_symbuf_symbol *ssym;
  7067.     } u;
  7068.   const char *name;
  7069. };
  7070.  
  7071. /* Sort references to symbols by ascending section number.  */
  7072.  
  7073. static int
  7074. elf_sort_elf_symbol (const void *arg1, const void *arg2)
  7075. {
  7076.   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
  7077.   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
  7078.  
  7079.   return s1->st_shndx - s2->st_shndx;
  7080. }
  7081.  
  7082. static int
  7083. elf_sym_name_compare (const void *arg1, const void *arg2)
  7084. {
  7085.   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
  7086.   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
  7087.   return strcmp (s1->name, s2->name);
  7088. }
  7089.  
  7090. static struct elf_symbuf_head *
  7091. elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
  7092. {
  7093.   Elf_Internal_Sym **ind, **indbufend, **indbuf;
  7094.   struct elf_symbuf_symbol *ssym;
  7095.   struct elf_symbuf_head *ssymbuf, *ssymhead;
  7096.   bfd_size_type i, shndx_count, total_size;
  7097.  
  7098.   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
  7099.   if (indbuf == NULL)
  7100.     return NULL;
  7101.  
  7102.   for (ind = indbuf, i = 0; i < symcount; i++)
  7103.     if (isymbuf[i].st_shndx != SHN_UNDEF)
  7104.       *ind++ = &isymbuf[i];
  7105.   indbufend = ind;
  7106.  
  7107.   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
  7108.          elf_sort_elf_symbol);
  7109.  
  7110.   shndx_count = 0;
  7111.   if (indbufend > indbuf)
  7112.     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
  7113.       if (ind[0]->st_shndx != ind[1]->st_shndx)
  7114.         shndx_count++;
  7115.  
  7116.   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
  7117.                 + (indbufend - indbuf) * sizeof (*ssym));
  7118.   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
  7119.   if (ssymbuf == NULL)
  7120.     {
  7121.       free (indbuf);
  7122.       return NULL;
  7123.     }
  7124.  
  7125.   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
  7126.   ssymbuf->ssym = NULL;
  7127.   ssymbuf->count = shndx_count;
  7128.   ssymbuf->st_shndx = 0;
  7129.   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
  7130.     {
  7131.       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
  7132.         {
  7133.           ssymhead++;
  7134.           ssymhead->ssym = ssym;
  7135.           ssymhead->count = 0;
  7136.           ssymhead->st_shndx = (*ind)->st_shndx;
  7137.         }
  7138.       ssym->st_name = (*ind)->st_name;
  7139.       ssym->st_info = (*ind)->st_info;
  7140.       ssym->st_other = (*ind)->st_other;
  7141.       ssymhead->count++;
  7142.     }
  7143.   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
  7144.               && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
  7145.                   == total_size));
  7146.  
  7147.   free (indbuf);
  7148.   return ssymbuf;
  7149. }
  7150.  
  7151. /* Check if 2 sections define the same set of local and global
  7152.    symbols.  */
  7153.  
  7154. static bfd_boolean
  7155. bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
  7156.                                    struct bfd_link_info *info)
  7157. {
  7158.   bfd *bfd1, *bfd2;
  7159.   const struct elf_backend_data *bed1, *bed2;
  7160.   Elf_Internal_Shdr *hdr1, *hdr2;
  7161.   bfd_size_type symcount1, symcount2;
  7162.   Elf_Internal_Sym *isymbuf1, *isymbuf2;
  7163.   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
  7164.   Elf_Internal_Sym *isym, *isymend;
  7165.   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
  7166.   bfd_size_type count1, count2, i;
  7167.   unsigned int shndx1, shndx2;
  7168.   bfd_boolean result;
  7169.  
  7170.   bfd1 = sec1->owner;
  7171.   bfd2 = sec2->owner;
  7172.  
  7173.   /* Both sections have to be in ELF.  */
  7174.   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
  7175.       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
  7176.     return FALSE;
  7177.  
  7178.   if (elf_section_type (sec1) != elf_section_type (sec2))
  7179.     return FALSE;
  7180.  
  7181.   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
  7182.   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
  7183.   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
  7184.     return FALSE;
  7185.  
  7186.   bed1 = get_elf_backend_data (bfd1);
  7187.   bed2 = get_elf_backend_data (bfd2);
  7188.   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
  7189.   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
  7190.   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
  7191.   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
  7192.  
  7193.   if (symcount1 == 0 || symcount2 == 0)
  7194.     return FALSE;
  7195.  
  7196.   result = FALSE;
  7197.   isymbuf1 = NULL;
  7198.   isymbuf2 = NULL;
  7199.   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
  7200.   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
  7201.  
  7202.   if (ssymbuf1 == NULL)
  7203.     {
  7204.       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
  7205.                                        NULL, NULL, NULL);
  7206.       if (isymbuf1 == NULL)
  7207.         goto done;
  7208.  
  7209.       if (!info->reduce_memory_overheads)
  7210.         elf_tdata (bfd1)->symbuf = ssymbuf1
  7211.           = elf_create_symbuf (symcount1, isymbuf1);
  7212.     }
  7213.  
  7214.   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
  7215.     {
  7216.       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
  7217.                                        NULL, NULL, NULL);
  7218.       if (isymbuf2 == NULL)
  7219.         goto done;
  7220.  
  7221.       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
  7222.         elf_tdata (bfd2)->symbuf = ssymbuf2
  7223.           = elf_create_symbuf (symcount2, isymbuf2);
  7224.     }
  7225.  
  7226.   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
  7227.     {
  7228.       /* Optimized faster version.  */
  7229.       bfd_size_type lo, hi, mid;
  7230.       struct elf_symbol *symp;
  7231.       struct elf_symbuf_symbol *ssym, *ssymend;
  7232.  
  7233.       lo = 0;
  7234.       hi = ssymbuf1->count;
  7235.       ssymbuf1++;
  7236.       count1 = 0;
  7237.       while (lo < hi)
  7238.         {
  7239.           mid = (lo + hi) / 2;
  7240.           if (shndx1 < ssymbuf1[mid].st_shndx)
  7241.             hi = mid;
  7242.           else if (shndx1 > ssymbuf1[mid].st_shndx)
  7243.             lo = mid + 1;
  7244.           else
  7245.             {
  7246.               count1 = ssymbuf1[mid].count;
  7247.               ssymbuf1 += mid;
  7248.               break;
  7249.             }
  7250.         }
  7251.  
  7252.       lo = 0;
  7253.       hi = ssymbuf2->count;
  7254.       ssymbuf2++;
  7255.       count2 = 0;
  7256.       while (lo < hi)
  7257.         {
  7258.           mid = (lo + hi) / 2;
  7259.           if (shndx2 < ssymbuf2[mid].st_shndx)
  7260.             hi = mid;
  7261.           else if (shndx2 > ssymbuf2[mid].st_shndx)
  7262.             lo = mid + 1;
  7263.           else
  7264.             {
  7265.               count2 = ssymbuf2[mid].count;
  7266.               ssymbuf2 += mid;
  7267.               break;
  7268.             }
  7269.         }
  7270.  
  7271.       if (count1 == 0 || count2 == 0 || count1 != count2)
  7272.         goto done;
  7273.  
  7274.       symtable1 = (struct elf_symbol *)
  7275.           bfd_malloc (count1 * sizeof (struct elf_symbol));
  7276.       symtable2 = (struct elf_symbol *)
  7277.           bfd_malloc (count2 * sizeof (struct elf_symbol));
  7278.       if (symtable1 == NULL || symtable2 == NULL)
  7279.         goto done;
  7280.  
  7281.       symp = symtable1;
  7282.       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
  7283.            ssym < ssymend; ssym++, symp++)
  7284.         {
  7285.           symp->u.ssym = ssym;
  7286.           symp->name = bfd_elf_string_from_elf_section (bfd1,
  7287.                                                         hdr1->sh_link,
  7288.                                                         ssym->st_name);
  7289.         }
  7290.  
  7291.       symp = symtable2;
  7292.       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
  7293.            ssym < ssymend; ssym++, symp++)
  7294.         {
  7295.           symp->u.ssym = ssym;
  7296.           symp->name = bfd_elf_string_from_elf_section (bfd2,
  7297.                                                         hdr2->sh_link,
  7298.                                                         ssym->st_name);
  7299.         }
  7300.  
  7301.       /* Sort symbol by name.  */
  7302.       qsort (symtable1, count1, sizeof (struct elf_symbol),
  7303.              elf_sym_name_compare);
  7304.       qsort (symtable2, count1, sizeof (struct elf_symbol),
  7305.              elf_sym_name_compare);
  7306.  
  7307.       for (i = 0; i < count1; i++)
  7308.         /* Two symbols must have the same binding, type and name.  */
  7309.         if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
  7310.             || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
  7311.             || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7312.           goto done;
  7313.  
  7314.       result = TRUE;
  7315.       goto done;
  7316.     }
  7317.  
  7318.   symtable1 = (struct elf_symbol *)
  7319.       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
  7320.   symtable2 = (struct elf_symbol *)
  7321.       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
  7322.   if (symtable1 == NULL || symtable2 == NULL)
  7323.     goto done;
  7324.  
  7325.   /* Count definitions in the section.  */
  7326.   count1 = 0;
  7327.   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
  7328.     if (isym->st_shndx == shndx1)
  7329.       symtable1[count1++].u.isym = isym;
  7330.  
  7331.   count2 = 0;
  7332.   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
  7333.     if (isym->st_shndx == shndx2)
  7334.       symtable2[count2++].u.isym = isym;
  7335.  
  7336.   if (count1 == 0 || count2 == 0 || count1 != count2)
  7337.     goto done;
  7338.  
  7339.   for (i = 0; i < count1; i++)
  7340.     symtable1[i].name
  7341.       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
  7342.                                          symtable1[i].u.isym->st_name);
  7343.  
  7344.   for (i = 0; i < count2; i++)
  7345.     symtable2[i].name
  7346.       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
  7347.                                          symtable2[i].u.isym->st_name);
  7348.  
  7349.   /* Sort symbol by name.  */
  7350.   qsort (symtable1, count1, sizeof (struct elf_symbol),
  7351.          elf_sym_name_compare);
  7352.   qsort (symtable2, count1, sizeof (struct elf_symbol),
  7353.          elf_sym_name_compare);
  7354.  
  7355.   for (i = 0; i < count1; i++)
  7356.     /* Two symbols must have the same binding, type and name.  */
  7357.     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
  7358.         || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
  7359.         || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7360.       goto done;
  7361.  
  7362.   result = TRUE;
  7363.  
  7364. done:
  7365.   if (symtable1)
  7366.     free (symtable1);
  7367.   if (symtable2)
  7368.     free (symtable2);
  7369.   if (isymbuf1)
  7370.     free (isymbuf1);
  7371.   if (isymbuf2)
  7372.     free (isymbuf2);
  7373.  
  7374.   return result;
  7375. }
  7376.  
  7377. /* Return TRUE if 2 section types are compatible.  */
  7378.  
  7379. bfd_boolean
  7380. _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
  7381.                                  bfd *bbfd, const asection *bsec)
  7382. {
  7383.   if (asec == NULL
  7384.       || bsec == NULL
  7385.       || abfd->xvec->flavour != bfd_target_elf_flavour
  7386.       || bbfd->xvec->flavour != bfd_target_elf_flavour)
  7387.     return TRUE;
  7388.  
  7389.   return elf_section_type (asec) == elf_section_type (bsec);
  7390. }
  7391. /* Final phase of ELF linker.  */
  7392.  
  7393. /* A structure we use to avoid passing large numbers of arguments.  */
  7394.  
  7395. struct elf_final_link_info
  7396. {
  7397.   /* General link information.  */
  7398.   struct bfd_link_info *info;
  7399.   /* Output BFD.  */
  7400.   bfd *output_bfd;
  7401.   /* Symbol string table.  */
  7402.   struct bfd_strtab_hash *symstrtab;
  7403.   /* .dynsym section.  */
  7404.   asection *dynsym_sec;
  7405.   /* .hash section.  */
  7406.   asection *hash_sec;
  7407.   /* symbol version section (.gnu.version).  */
  7408.   asection *symver_sec;
  7409.   /* Buffer large enough to hold contents of any section.  */
  7410.   bfd_byte *contents;
  7411.   /* Buffer large enough to hold external relocs of any section.  */
  7412.   void *external_relocs;
  7413.   /* Buffer large enough to hold internal relocs of any section.  */
  7414.   Elf_Internal_Rela *internal_relocs;
  7415.   /* Buffer large enough to hold external local symbols of any input
  7416.      BFD.  */
  7417.   bfd_byte *external_syms;
  7418.   /* And a buffer for symbol section indices.  */
  7419.   Elf_External_Sym_Shndx *locsym_shndx;
  7420.   /* Buffer large enough to hold internal local symbols of any input
  7421.      BFD.  */
  7422.   Elf_Internal_Sym *internal_syms;
  7423.   /* Array large enough to hold a symbol index for each local symbol
  7424.      of any input BFD.  */
  7425.   long *indices;
  7426.   /* Array large enough to hold a section pointer for each local
  7427.      symbol of any input BFD.  */
  7428.   asection **sections;
  7429.   /* Buffer to hold swapped out symbols.  */
  7430.   bfd_byte *symbuf;
  7431.   /* And one for symbol section indices.  */
  7432.   Elf_External_Sym_Shndx *symshndxbuf;
  7433.   /* Number of swapped out symbols in buffer.  */
  7434.   size_t symbuf_count;
  7435.   /* Number of symbols which fit in symbuf.  */
  7436.   size_t symbuf_size;
  7437.   /* And same for symshndxbuf.  */
  7438.   size_t shndxbuf_size;
  7439.   /* Number of STT_FILE syms seen.  */
  7440.   size_t filesym_count;
  7441. };
  7442.  
  7443. /* This struct is used to pass information to elf_link_output_extsym.  */
  7444.  
  7445. struct elf_outext_info
  7446. {
  7447.   bfd_boolean failed;
  7448.   bfd_boolean localsyms;
  7449.   bfd_boolean need_second_pass;
  7450.   bfd_boolean second_pass;
  7451.   struct elf_final_link_info *flinfo;
  7452. };
  7453.  
  7454.  
  7455. /* Support for evaluating a complex relocation.
  7456.  
  7457.    Complex relocations are generalized, self-describing relocations.  The
  7458.    implementation of them consists of two parts: complex symbols, and the
  7459.    relocations themselves.
  7460.  
  7461.    The relocations are use a reserved elf-wide relocation type code (R_RELC
  7462.    external / BFD_RELOC_RELC internal) and an encoding of relocation field
  7463.    information (start bit, end bit, word width, etc) into the addend.  This
  7464.    information is extracted from CGEN-generated operand tables within gas.
  7465.  
  7466.    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
  7467.    internal) representing prefix-notation expressions, including but not
  7468.    limited to those sorts of expressions normally encoded as addends in the
  7469.    addend field.  The symbol mangling format is:
  7470.  
  7471.    <node> := <literal>
  7472.           |  <unary-operator> ':' <node>
  7473.           |  <binary-operator> ':' <node> ':' <node>
  7474.           ;
  7475.  
  7476.    <literal> := 's' <digits=N> ':' <N character symbol name>
  7477.              |  'S' <digits=N> ':' <N character section name>
  7478.              |  '#' <hexdigits>
  7479.              ;
  7480.  
  7481.    <binary-operator> := as in C
  7482.    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
  7483.  
  7484. static void
  7485. set_symbol_value (bfd *bfd_with_globals,
  7486.                   Elf_Internal_Sym *isymbuf,
  7487.                   size_t locsymcount,
  7488.                   size_t symidx,
  7489.                   bfd_vma val)
  7490. {
  7491.   struct elf_link_hash_entry **sym_hashes;
  7492.   struct elf_link_hash_entry *h;
  7493.   size_t extsymoff = locsymcount;
  7494.  
  7495.   if (symidx < locsymcount)
  7496.     {
  7497.       Elf_Internal_Sym *sym;
  7498.  
  7499.       sym = isymbuf + symidx;
  7500.       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  7501.         {
  7502.           /* It is a local symbol: move it to the
  7503.              "absolute" section and give it a value.  */
  7504.           sym->st_shndx = SHN_ABS;
  7505.           sym->st_value = val;
  7506.           return;
  7507.         }
  7508.       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
  7509.       extsymoff = 0;
  7510.     }
  7511.  
  7512.   /* It is a global symbol: set its link type
  7513.      to "defined" and give it a value.  */
  7514.  
  7515.   sym_hashes = elf_sym_hashes (bfd_with_globals);
  7516.   h = sym_hashes [symidx - extsymoff];
  7517.   while (h->root.type == bfd_link_hash_indirect
  7518.          || h->root.type == bfd_link_hash_warning)
  7519.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  7520.   h->root.type = bfd_link_hash_defined;
  7521.   h->root.u.def.value = val;
  7522.   h->root.u.def.section = bfd_abs_section_ptr;
  7523. }
  7524.  
  7525. static bfd_boolean
  7526. resolve_symbol (const char *name,
  7527.                 bfd *input_bfd,
  7528.                 struct elf_final_link_info *flinfo,
  7529.                 bfd_vma *result,
  7530.                 Elf_Internal_Sym *isymbuf,
  7531.                 size_t locsymcount)
  7532. {
  7533.   Elf_Internal_Sym *sym;
  7534.   struct bfd_link_hash_entry *global_entry;
  7535.   const char *candidate = NULL;
  7536.   Elf_Internal_Shdr *symtab_hdr;
  7537.   size_t i;
  7538.  
  7539.   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  7540.  
  7541.   for (i = 0; i < locsymcount; ++ i)
  7542.     {
  7543.       sym = isymbuf + i;
  7544.  
  7545.       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
  7546.         continue;
  7547.  
  7548.       candidate = bfd_elf_string_from_elf_section (input_bfd,
  7549.                                                    symtab_hdr->sh_link,
  7550.                                                    sym->st_name);
  7551. #ifdef DEBUG
  7552.       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
  7553.               name, candidate, (unsigned long) sym->st_value);
  7554. #endif
  7555.       if (candidate && strcmp (candidate, name) == 0)
  7556.         {
  7557.           asection *sec = flinfo->sections [i];
  7558.  
  7559.           *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
  7560.           *result += sec->output_offset + sec->output_section->vma;
  7561. #ifdef DEBUG
  7562.           printf ("Found symbol with value %8.8lx\n",
  7563.                   (unsigned long) *result);
  7564. #endif
  7565.           return TRUE;
  7566.         }
  7567.     }
  7568.  
  7569.   /* Hmm, haven't found it yet. perhaps it is a global.  */
  7570.   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
  7571.                                        FALSE, FALSE, TRUE);
  7572.   if (!global_entry)
  7573.     return FALSE;
  7574.  
  7575.   if (global_entry->type == bfd_link_hash_defined
  7576.       || global_entry->type == bfd_link_hash_defweak)
  7577.     {
  7578.       *result = (global_entry->u.def.value
  7579.                  + global_entry->u.def.section->output_section->vma
  7580.                  + global_entry->u.def.section->output_offset);
  7581. #ifdef DEBUG
  7582.       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
  7583.               global_entry->root.string, (unsigned long) *result);
  7584. #endif
  7585.       return TRUE;
  7586.     }
  7587.  
  7588.   return FALSE;
  7589. }
  7590.  
  7591. static bfd_boolean
  7592. resolve_section (const char *name,
  7593.                  asection *sections,
  7594.                  bfd_vma *result)
  7595. {
  7596.   asection *curr;
  7597.   unsigned int len;
  7598.  
  7599.   for (curr = sections; curr; curr = curr->next)
  7600.     if (strcmp (curr->name, name) == 0)
  7601.       {
  7602.         *result = curr->vma;
  7603.         return TRUE;
  7604.       }
  7605.  
  7606.   /* Hmm. still haven't found it. try pseudo-section names.  */
  7607.   for (curr = sections; curr; curr = curr->next)
  7608.     {
  7609.       len = strlen (curr->name);
  7610.       if (len > strlen (name))
  7611.         continue;
  7612.  
  7613.       if (strncmp (curr->name, name, len) == 0)
  7614.         {
  7615.           if (strncmp (".end", name + len, 4) == 0)
  7616.             {
  7617.               *result = curr->vma + curr->size;
  7618.               return TRUE;
  7619.             }
  7620.  
  7621.           /* Insert more pseudo-section names here, if you like.  */
  7622.         }
  7623.     }
  7624.  
  7625.   return FALSE;
  7626. }
  7627.  
  7628. static void
  7629. undefined_reference (const char *reftype, const char *name)
  7630. {
  7631.   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
  7632.                       reftype, name);
  7633. }
  7634.  
  7635. static bfd_boolean
  7636. eval_symbol (bfd_vma *result,
  7637.              const char **symp,
  7638.              bfd *input_bfd,
  7639.              struct elf_final_link_info *flinfo,
  7640.              bfd_vma dot,
  7641.              Elf_Internal_Sym *isymbuf,
  7642.              size_t locsymcount,
  7643.              int signed_p)
  7644. {
  7645.   size_t len;
  7646.   size_t symlen;
  7647.   bfd_vma a;
  7648.   bfd_vma b;
  7649.   char symbuf[4096];
  7650.   const char *sym = *symp;
  7651.   const char *symend;
  7652.   bfd_boolean symbol_is_section = FALSE;
  7653.  
  7654.   len = strlen (sym);
  7655.   symend = sym + len;
  7656.  
  7657.   if (len < 1 || len > sizeof (symbuf))
  7658.     {
  7659.       bfd_set_error (bfd_error_invalid_operation);
  7660.       return FALSE;
  7661.     }
  7662.  
  7663.   switch (* sym)
  7664.     {
  7665.     case '.':
  7666.       *result = dot;
  7667.       *symp = sym + 1;
  7668.       return TRUE;
  7669.  
  7670.     case '#':
  7671.       ++sym;
  7672.       *result = strtoul (sym, (char **) symp, 16);
  7673.       return TRUE;
  7674.  
  7675.     case 'S':
  7676.       symbol_is_section = TRUE;
  7677.     case 's':
  7678.       ++sym;
  7679.       symlen = strtol (sym, (char **) symp, 10);
  7680.       sym = *symp + 1; /* Skip the trailing ':'.  */
  7681.  
  7682.       if (symend < sym || symlen + 1 > sizeof (symbuf))
  7683.         {
  7684.           bfd_set_error (bfd_error_invalid_operation);
  7685.           return FALSE;
  7686.         }
  7687.  
  7688.       memcpy (symbuf, sym, symlen);
  7689.       symbuf[symlen] = '\0';
  7690.       *symp = sym + symlen;
  7691.  
  7692.       /* Is it always possible, with complex symbols, that gas "mis-guessed"
  7693.          the symbol as a section, or vice-versa. so we're pretty liberal in our
  7694.          interpretation here; section means "try section first", not "must be a
  7695.          section", and likewise with symbol.  */
  7696.  
  7697.       if (symbol_is_section)
  7698.         {
  7699.           if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
  7700.               && !resolve_symbol (symbuf, input_bfd, flinfo, result,
  7701.                                   isymbuf, locsymcount))
  7702.             {
  7703.               undefined_reference ("section", symbuf);
  7704.               return FALSE;
  7705.             }
  7706.         }
  7707.       else
  7708.         {
  7709.           if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
  7710.                                isymbuf, locsymcount)
  7711.               && !resolve_section (symbuf, flinfo->output_bfd->sections,
  7712.                                    result))
  7713.             {
  7714.               undefined_reference ("symbol", symbuf);
  7715.               return FALSE;
  7716.             }
  7717.         }
  7718.  
  7719.       return TRUE;
  7720.  
  7721.       /* All that remains are operators.  */
  7722.  
  7723. #define UNARY_OP(op)                                            \
  7724.   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
  7725.     {                                                           \
  7726.       sym += strlen (#op);                                      \
  7727.       if (*sym == ':')                                          \
  7728.         ++sym;                                                  \
  7729.       *symp = sym;                                              \
  7730.       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,       \
  7731.                         isymbuf, locsymcount, signed_p))        \
  7732.         return FALSE;                                           \
  7733.       if (signed_p)                                             \
  7734.         *result = op ((bfd_signed_vma) a);                      \
  7735.       else                                                      \
  7736.         *result = op a;                                         \
  7737.       return TRUE;                                              \
  7738.     }
  7739.  
  7740. #define BINARY_OP(op)                                           \
  7741.   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
  7742.     {                                                           \
  7743.       sym += strlen (#op);                                      \
  7744.       if (*sym == ':')                                          \
  7745.         ++sym;                                                  \
  7746.       *symp = sym;                                              \
  7747.       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,       \
  7748.                         isymbuf, locsymcount, signed_p))        \
  7749.         return FALSE;                                           \
  7750.       ++*symp;                                                  \
  7751.       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,       \
  7752.                         isymbuf, locsymcount, signed_p))        \
  7753.         return FALSE;                                           \
  7754.       if (signed_p)                                             \
  7755.         *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
  7756.       else                                                      \
  7757.         *result = a op b;                                       \
  7758.       return TRUE;                                              \
  7759.     }
  7760.  
  7761.     default:
  7762.       UNARY_OP  (0-);
  7763.       BINARY_OP (<<);
  7764.       BINARY_OP (>>);
  7765.       BINARY_OP (==);
  7766.       BINARY_OP (!=);
  7767.       BINARY_OP (<=);
  7768.       BINARY_OP (>=);
  7769.       BINARY_OP (&&);
  7770.       BINARY_OP (||);
  7771.       UNARY_OP  (~);
  7772.       UNARY_OP  (!);
  7773.       BINARY_OP (*);
  7774.       BINARY_OP (/);
  7775.       BINARY_OP (%);
  7776.       BINARY_OP (^);
  7777.       BINARY_OP (|);
  7778.       BINARY_OP (&);
  7779.       BINARY_OP (+);
  7780.       BINARY_OP (-);
  7781.       BINARY_OP (<);
  7782.       BINARY_OP (>);
  7783. #undef UNARY_OP
  7784. #undef BINARY_OP
  7785.       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
  7786.       bfd_set_error (bfd_error_invalid_operation);
  7787.       return FALSE;
  7788.     }
  7789. }
  7790.  
  7791. static void
  7792. put_value (bfd_vma size,
  7793.            unsigned long chunksz,
  7794.            bfd *input_bfd,
  7795.            bfd_vma x,
  7796.            bfd_byte *location)
  7797. {
  7798.   location += (size - chunksz);
  7799.  
  7800.   for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
  7801.     {
  7802.       switch (chunksz)
  7803.         {
  7804.         default:
  7805.         case 0:
  7806.           abort ();
  7807.         case 1:
  7808.           bfd_put_8 (input_bfd, x, location);
  7809.           break;
  7810.         case 2:
  7811.           bfd_put_16 (input_bfd, x, location);
  7812.           break;
  7813.         case 4:
  7814.           bfd_put_32 (input_bfd, x, location);
  7815.           break;
  7816.         case 8:
  7817. #ifdef BFD64
  7818.           bfd_put_64 (input_bfd, x, location);
  7819. #else
  7820.           abort ();
  7821. #endif
  7822.           break;
  7823.         }
  7824.     }
  7825. }
  7826.  
  7827. static bfd_vma
  7828. get_value (bfd_vma size,
  7829.            unsigned long chunksz,
  7830.            bfd *input_bfd,
  7831.            bfd_byte *location)
  7832. {
  7833.   int shift;
  7834.   bfd_vma x = 0;
  7835.  
  7836.   /* Sanity checks.  */
  7837.   BFD_ASSERT (chunksz <= sizeof (x)
  7838.               && size >= chunksz
  7839.               && chunksz != 0
  7840.               && (size % chunksz) == 0
  7841.               && input_bfd != NULL
  7842.               && location != NULL);
  7843.  
  7844.   if (chunksz == sizeof (x))
  7845.     {
  7846.       BFD_ASSERT (size == chunksz);
  7847.  
  7848.       /* Make sure that we do not perform an undefined shift operation.
  7849.          We know that size == chunksz so there will only be one iteration
  7850.          of the loop below.  */
  7851.       shift = 0;
  7852.     }
  7853.   else
  7854.     shift = 8 * chunksz;
  7855.  
  7856.   for (; size; size -= chunksz, location += chunksz)
  7857.     {
  7858.       switch (chunksz)
  7859.         {
  7860.         case 1:
  7861.           x = (x << shift) | bfd_get_8 (input_bfd, location);
  7862.           break;
  7863.         case 2:
  7864.           x = (x << shift) | bfd_get_16 (input_bfd, location);
  7865.           break;
  7866.         case 4:
  7867.           x = (x << shift) | bfd_get_32 (input_bfd, location);
  7868.           break;
  7869. #ifdef BFD64
  7870.         case 8:
  7871.           x = (x << shift) | bfd_get_64 (input_bfd, location);
  7872.           break;
  7873. #endif
  7874.         default:
  7875.           abort ();
  7876.         }
  7877.     }
  7878.   return x;
  7879. }
  7880.  
  7881. static void
  7882. decode_complex_addend (unsigned long *start,   /* in bits */
  7883.                        unsigned long *oplen,   /* in bits */
  7884.                        unsigned long *len,     /* in bits */
  7885.                        unsigned long *wordsz,  /* in bytes */
  7886.                        unsigned long *chunksz, /* in bytes */
  7887.                        unsigned long *lsb0_p,
  7888.                        unsigned long *signed_p,
  7889.                        unsigned long *trunc_p,
  7890.                        unsigned long encoded)
  7891. {
  7892.   * start     =  encoded        & 0x3F;
  7893.   * len       = (encoded >>  6) & 0x3F;
  7894.   * oplen     = (encoded >> 12) & 0x3F;
  7895.   * wordsz    = (encoded >> 18) & 0xF;
  7896.   * chunksz   = (encoded >> 22) & 0xF;
  7897.   * lsb0_p    = (encoded >> 27) & 1;
  7898.   * signed_p  = (encoded >> 28) & 1;
  7899.   * trunc_p   = (encoded >> 29) & 1;
  7900. }
  7901.  
  7902. bfd_reloc_status_type
  7903. bfd_elf_perform_complex_relocation (bfd *input_bfd,
  7904.                                     asection *input_section ATTRIBUTE_UNUSED,
  7905.                                     bfd_byte *contents,
  7906.                                     Elf_Internal_Rela *rel,
  7907.                                     bfd_vma relocation)
  7908. {
  7909.   bfd_vma shift, x, mask;
  7910.   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
  7911.   bfd_reloc_status_type r;
  7912.  
  7913.   /*  Perform this reloc, since it is complex.
  7914.       (this is not to say that it necessarily refers to a complex
  7915.       symbol; merely that it is a self-describing CGEN based reloc.
  7916.       i.e. the addend has the complete reloc information (bit start, end,
  7917.       word size, etc) encoded within it.).  */
  7918.  
  7919.   decode_complex_addend (&start, &oplen, &len, &wordsz,
  7920.                          &chunksz, &lsb0_p, &signed_p,
  7921.                          &trunc_p, rel->r_addend);
  7922.  
  7923.   mask = (((1L << (len - 1)) - 1) << 1) | 1;
  7924.  
  7925.   if (lsb0_p)
  7926.     shift = (start + 1) - len;
  7927.   else
  7928.     shift = (8 * wordsz) - (start + len);
  7929.  
  7930.   /* FIXME: octets_per_byte.  */
  7931.   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
  7932.  
  7933. #ifdef DEBUG
  7934.   printf ("Doing complex reloc: "
  7935.           "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
  7936.           "chunksz %ld, start %ld, len %ld, oplen %ld\n"
  7937.           "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
  7938.           lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
  7939.           oplen, (unsigned long) x, (unsigned long) mask,
  7940.           (unsigned long) relocation);
  7941. #endif
  7942.  
  7943.   r = bfd_reloc_ok;
  7944.   if (! trunc_p)
  7945.     /* Now do an overflow check.  */
  7946.     r = bfd_check_overflow ((signed_p
  7947.                              ? complain_overflow_signed
  7948.                              : complain_overflow_unsigned),
  7949.                             len, 0, (8 * wordsz),
  7950.                             relocation);
  7951.  
  7952.   /* Do the deed.  */
  7953.   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
  7954.  
  7955. #ifdef DEBUG
  7956.   printf ("           relocation: %8.8lx\n"
  7957.           "         shifted mask: %8.8lx\n"
  7958.           " shifted/masked reloc: %8.8lx\n"
  7959.           "               result: %8.8lx\n",
  7960.           (unsigned long) relocation, (unsigned long) (mask << shift),
  7961.           (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
  7962. #endif
  7963.   /* FIXME: octets_per_byte.  */
  7964.   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
  7965.   return r;
  7966. }
  7967.  
  7968. /* When performing a relocatable link, the input relocations are
  7969.    preserved.  But, if they reference global symbols, the indices
  7970.    referenced must be updated.  Update all the relocations found in
  7971.    RELDATA.  */
  7972.  
  7973. static void
  7974. elf_link_adjust_relocs (bfd *abfd,
  7975.                         struct bfd_elf_section_reloc_data *reldata)
  7976. {
  7977.   unsigned int i;
  7978.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  7979.   bfd_byte *erela;
  7980.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  7981.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  7982.   bfd_vma r_type_mask;
  7983.   int r_sym_shift;
  7984.   unsigned int count = reldata->count;
  7985.   struct elf_link_hash_entry **rel_hash = reldata->hashes;
  7986.  
  7987.   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
  7988.     {
  7989.       swap_in = bed->s->swap_reloc_in;
  7990.       swap_out = bed->s->swap_reloc_out;
  7991.     }
  7992.   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
  7993.     {
  7994.       swap_in = bed->s->swap_reloca_in;
  7995.       swap_out = bed->s->swap_reloca_out;
  7996.     }
  7997.   else
  7998.     abort ();
  7999.  
  8000.   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
  8001.     abort ();
  8002.  
  8003.   if (bed->s->arch_size == 32)
  8004.     {
  8005.       r_type_mask = 0xff;
  8006.       r_sym_shift = 8;
  8007.     }
  8008.   else
  8009.     {
  8010.       r_type_mask = 0xffffffff;
  8011.       r_sym_shift = 32;
  8012.     }
  8013.  
  8014.   erela = reldata->hdr->contents;
  8015.   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
  8016.     {
  8017.       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
  8018.       unsigned int j;
  8019.  
  8020.       if (*rel_hash == NULL)
  8021.         continue;
  8022.  
  8023.       BFD_ASSERT ((*rel_hash)->indx >= 0);
  8024.  
  8025.       (*swap_in) (abfd, erela, irela);
  8026.       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
  8027.         irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
  8028.                            | (irela[j].r_info & r_type_mask));
  8029.       (*swap_out) (abfd, irela, erela);
  8030.     }
  8031. }
  8032.  
  8033. struct elf_link_sort_rela
  8034. {
  8035.   union {
  8036.     bfd_vma offset;
  8037.     bfd_vma sym_mask;
  8038.   } u;
  8039.   enum elf_reloc_type_class type;
  8040.   /* We use this as an array of size int_rels_per_ext_rel.  */
  8041.   Elf_Internal_Rela rela[1];
  8042. };
  8043.  
  8044. static int
  8045. elf_link_sort_cmp1 (const void *A, const void *B)
  8046. {
  8047.   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8048.   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8049.   int relativea, relativeb;
  8050.  
  8051.   relativea = a->type == reloc_class_relative;
  8052.   relativeb = b->type == reloc_class_relative;
  8053.  
  8054.   if (relativea < relativeb)
  8055.     return 1;
  8056.   if (relativea > relativeb)
  8057.     return -1;
  8058.   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
  8059.     return -1;
  8060.   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
  8061.     return 1;
  8062.   if (a->rela->r_offset < b->rela->r_offset)
  8063.     return -1;
  8064.   if (a->rela->r_offset > b->rela->r_offset)
  8065.     return 1;
  8066.   return 0;
  8067. }
  8068.  
  8069. static int
  8070. elf_link_sort_cmp2 (const void *A, const void *B)
  8071. {
  8072.   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8073.   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8074.  
  8075.   if (a->type < b->type)
  8076.     return -1;
  8077.   if (a->type > b->type)
  8078.     return 1;
  8079.   if (a->u.offset < b->u.offset)
  8080.     return -1;
  8081.   if (a->u.offset > b->u.offset)
  8082.     return 1;
  8083.   if (a->rela->r_offset < b->rela->r_offset)
  8084.     return -1;
  8085.   if (a->rela->r_offset > b->rela->r_offset)
  8086.     return 1;
  8087.   return 0;
  8088. }
  8089.  
  8090. static size_t
  8091. elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
  8092. {
  8093.   asection *dynamic_relocs;
  8094.   asection *rela_dyn;
  8095.   asection *rel_dyn;
  8096.   bfd_size_type count, size;
  8097.   size_t i, ret, sort_elt, ext_size;
  8098.   bfd_byte *sort, *s_non_relative, *p;
  8099.   struct elf_link_sort_rela *sq;
  8100.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8101.   int i2e = bed->s->int_rels_per_ext_rel;
  8102.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  8103.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  8104.   struct bfd_link_order *lo;
  8105.   bfd_vma r_sym_mask;
  8106.   bfd_boolean use_rela;
  8107.  
  8108.   /* Find a dynamic reloc section.  */
  8109.   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
  8110.   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
  8111.   if (rela_dyn != NULL && rela_dyn->size > 0
  8112.       && rel_dyn != NULL && rel_dyn->size > 0)
  8113.     {
  8114.       bfd_boolean use_rela_initialised = FALSE;
  8115.  
  8116.       /* This is just here to stop gcc from complaining.
  8117.          It's initialization checking code is not perfect.  */
  8118.       use_rela = TRUE;
  8119.  
  8120.       /* Both sections are present.  Examine the sizes
  8121.          of the indirect sections to help us choose.  */
  8122.       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8123.         if (lo->type == bfd_indirect_link_order)
  8124.           {
  8125.             asection *o = lo->u.indirect.section;
  8126.  
  8127.             if ((o->size % bed->s->sizeof_rela) == 0)
  8128.               {
  8129.                 if ((o->size % bed->s->sizeof_rel) == 0)
  8130.                   /* Section size is divisible by both rel and rela sizes.
  8131.                      It is of no help to us.  */
  8132.                   ;
  8133.                 else
  8134.                   {
  8135.                     /* Section size is only divisible by rela.  */
  8136.                     if (use_rela_initialised && (use_rela == FALSE))
  8137.                       {
  8138.                         _bfd_error_handler
  8139.                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8140.                         bfd_set_error (bfd_error_invalid_operation);
  8141.                         return 0;
  8142.                       }
  8143.                     else
  8144.                       {
  8145.                         use_rela = TRUE;
  8146.                         use_rela_initialised = TRUE;
  8147.                       }
  8148.                   }
  8149.               }
  8150.             else if ((o->size % bed->s->sizeof_rel) == 0)
  8151.               {
  8152.                 /* Section size is only divisible by rel.  */
  8153.                 if (use_rela_initialised && (use_rela == TRUE))
  8154.                   {
  8155.                     _bfd_error_handler
  8156.                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8157.                     bfd_set_error (bfd_error_invalid_operation);
  8158.                     return 0;
  8159.                   }
  8160.                 else
  8161.                   {
  8162.                     use_rela = FALSE;
  8163.                     use_rela_initialised = TRUE;
  8164.                   }
  8165.               }
  8166.             else
  8167.               {
  8168.                 /* The section size is not divisible by either - something is wrong.  */
  8169.                 _bfd_error_handler
  8170.                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
  8171.                 bfd_set_error (bfd_error_invalid_operation);
  8172.                 return 0;
  8173.               }
  8174.           }
  8175.  
  8176.       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8177.         if (lo->type == bfd_indirect_link_order)
  8178.           {
  8179.             asection *o = lo->u.indirect.section;
  8180.  
  8181.             if ((o->size % bed->s->sizeof_rela) == 0)
  8182.               {
  8183.                 if ((o->size % bed->s->sizeof_rel) == 0)
  8184.                   /* Section size is divisible by both rel and rela sizes.
  8185.                      It is of no help to us.  */
  8186.                   ;
  8187.                 else
  8188.                   {
  8189.                     /* Section size is only divisible by rela.  */
  8190.                     if (use_rela_initialised && (use_rela == FALSE))
  8191.                       {
  8192.                         _bfd_error_handler
  8193.                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8194.                         bfd_set_error (bfd_error_invalid_operation);
  8195.                         return 0;
  8196.                       }
  8197.                     else
  8198.                       {
  8199.                         use_rela = TRUE;
  8200.                         use_rela_initialised = TRUE;
  8201.                       }
  8202.                   }
  8203.               }
  8204.             else if ((o->size % bed->s->sizeof_rel) == 0)
  8205.               {
  8206.                 /* Section size is only divisible by rel.  */
  8207.                 if (use_rela_initialised && (use_rela == TRUE))
  8208.                   {
  8209.                     _bfd_error_handler
  8210.                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8211.                     bfd_set_error (bfd_error_invalid_operation);
  8212.                     return 0;
  8213.                   }
  8214.                 else
  8215.                   {
  8216.                     use_rela = FALSE;
  8217.                     use_rela_initialised = TRUE;
  8218.                   }
  8219.               }
  8220.             else
  8221.               {
  8222.                 /* The section size is not divisible by either - something is wrong.  */
  8223.                 _bfd_error_handler
  8224.                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
  8225.                 bfd_set_error (bfd_error_invalid_operation);
  8226.                 return 0;
  8227.               }
  8228.           }
  8229.  
  8230.       if (! use_rela_initialised)
  8231.         /* Make a guess.  */
  8232.         use_rela = TRUE;
  8233.     }
  8234.   else if (rela_dyn != NULL && rela_dyn->size > 0)
  8235.     use_rela = TRUE;
  8236.   else if (rel_dyn != NULL && rel_dyn->size > 0)
  8237.     use_rela = FALSE;
  8238.   else
  8239.     return 0;
  8240.  
  8241.   if (use_rela)
  8242.     {
  8243.       dynamic_relocs = rela_dyn;
  8244.       ext_size = bed->s->sizeof_rela;
  8245.       swap_in = bed->s->swap_reloca_in;
  8246.       swap_out = bed->s->swap_reloca_out;
  8247.     }
  8248.   else
  8249.     {
  8250.       dynamic_relocs = rel_dyn;
  8251.       ext_size = bed->s->sizeof_rel;
  8252.       swap_in = bed->s->swap_reloc_in;
  8253.       swap_out = bed->s->swap_reloc_out;
  8254.     }
  8255.  
  8256.   size = 0;
  8257.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8258.     if (lo->type == bfd_indirect_link_order)
  8259.       size += lo->u.indirect.section->size;
  8260.  
  8261.   if (size != dynamic_relocs->size)
  8262.     return 0;
  8263.  
  8264.   sort_elt = (sizeof (struct elf_link_sort_rela)
  8265.               + (i2e - 1) * sizeof (Elf_Internal_Rela));
  8266.  
  8267.   count = dynamic_relocs->size / ext_size;
  8268.   if (count == 0)
  8269.     return 0;
  8270.   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
  8271.  
  8272.   if (sort == NULL)
  8273.     {
  8274.       (*info->callbacks->warning)
  8275.         (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
  8276.       return 0;
  8277.     }
  8278.  
  8279.   if (bed->s->arch_size == 32)
  8280.     r_sym_mask = ~(bfd_vma) 0xff;
  8281.   else
  8282.     r_sym_mask = ~(bfd_vma) 0xffffffff;
  8283.  
  8284.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8285.     if (lo->type == bfd_indirect_link_order)
  8286.       {
  8287.         bfd_byte *erel, *erelend;
  8288.         asection *o = lo->u.indirect.section;
  8289.  
  8290.         if (o->contents == NULL && o->size != 0)
  8291.           {
  8292.             /* This is a reloc section that is being handled as a normal
  8293.                section.  See bfd_section_from_shdr.  We can't combine
  8294.                relocs in this case.  */
  8295.             free (sort);
  8296.             return 0;
  8297.           }
  8298.         erel = o->contents;
  8299.         erelend = o->contents + o->size;
  8300.         /* FIXME: octets_per_byte.  */
  8301.         p = sort + o->output_offset / ext_size * sort_elt;
  8302.  
  8303.         while (erel < erelend)
  8304.           {
  8305.             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8306.  
  8307.             (*swap_in) (abfd, erel, s->rela);
  8308.             s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
  8309.             s->u.sym_mask = r_sym_mask;
  8310.             p += sort_elt;
  8311.             erel += ext_size;
  8312.           }
  8313.       }
  8314.  
  8315.   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
  8316.  
  8317.   for (i = 0, p = sort; i < count; i++, p += sort_elt)
  8318.     {
  8319.       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8320.       if (s->type != reloc_class_relative)
  8321.         break;
  8322.     }
  8323.   ret = i;
  8324.   s_non_relative = p;
  8325.  
  8326.   sq = (struct elf_link_sort_rela *) s_non_relative;
  8327.   for (; i < count; i++, p += sort_elt)
  8328.     {
  8329.       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
  8330.       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
  8331.         sq = sp;
  8332.       sp->u.offset = sq->rela->r_offset;
  8333.     }
  8334.  
  8335.   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
  8336.  
  8337.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8338.     if (lo->type == bfd_indirect_link_order)
  8339.       {
  8340.         bfd_byte *erel, *erelend;
  8341.         asection *o = lo->u.indirect.section;
  8342.  
  8343.         erel = o->contents;
  8344.         erelend = o->contents + o->size;
  8345.         /* FIXME: octets_per_byte.  */
  8346.         p = sort + o->output_offset / ext_size * sort_elt;
  8347.         while (erel < erelend)
  8348.           {
  8349.             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8350.             (*swap_out) (abfd, s->rela, erel);
  8351.             p += sort_elt;
  8352.             erel += ext_size;
  8353.           }
  8354.       }
  8355.  
  8356.   free (sort);
  8357.   *psec = dynamic_relocs;
  8358.   return ret;
  8359. }
  8360.  
  8361. /* Flush the output symbols to the file.  */
  8362.  
  8363. static bfd_boolean
  8364. elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
  8365.                             const struct elf_backend_data *bed)
  8366. {
  8367.   if (flinfo->symbuf_count > 0)
  8368.     {
  8369.       Elf_Internal_Shdr *hdr;
  8370.       file_ptr pos;
  8371.       bfd_size_type amt;
  8372.  
  8373.       hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
  8374.       pos = hdr->sh_offset + hdr->sh_size;
  8375.       amt = flinfo->symbuf_count * bed->s->sizeof_sym;
  8376.       if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
  8377.           || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
  8378.         return FALSE;
  8379.  
  8380.       hdr->sh_size += amt;
  8381.       flinfo->symbuf_count = 0;
  8382.     }
  8383.  
  8384.   return TRUE;
  8385. }
  8386.  
  8387. /* Add a symbol to the output symbol table.  */
  8388.  
  8389. static int
  8390. elf_link_output_sym (struct elf_final_link_info *flinfo,
  8391.                      const char *name,
  8392.                      Elf_Internal_Sym *elfsym,
  8393.                      asection *input_sec,
  8394.                      struct elf_link_hash_entry *h)
  8395. {
  8396.   bfd_byte *dest;
  8397.   Elf_External_Sym_Shndx *destshndx;
  8398.   int (*output_symbol_hook)
  8399.     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
  8400.      struct elf_link_hash_entry *);
  8401.   const struct elf_backend_data *bed;
  8402.  
  8403.   bed = get_elf_backend_data (flinfo->output_bfd);
  8404.   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
  8405.   if (output_symbol_hook != NULL)
  8406.     {
  8407.       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
  8408.       if (ret != 1)
  8409.         return ret;
  8410.     }
  8411.  
  8412.   if (name == NULL || *name == '\0')
  8413.     elfsym->st_name = 0;
  8414.   else if (input_sec->flags & SEC_EXCLUDE)
  8415.     elfsym->st_name = 0;
  8416.   else
  8417.     {
  8418.       elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
  8419.                                                             name, TRUE, FALSE);
  8420.       if (elfsym->st_name == (unsigned long) -1)
  8421.         return 0;
  8422.     }
  8423.  
  8424.   if (flinfo->symbuf_count >= flinfo->symbuf_size)
  8425.     {
  8426.       if (! elf_link_flush_output_syms (flinfo, bed))
  8427.         return 0;
  8428.     }
  8429.  
  8430.   dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
  8431.   destshndx = flinfo->symshndxbuf;
  8432.   if (destshndx != NULL)
  8433.     {
  8434.       if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
  8435.         {
  8436.           bfd_size_type amt;
  8437.  
  8438.           amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
  8439.           destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
  8440.                                                               amt * 2);
  8441.           if (destshndx == NULL)
  8442.             return 0;
  8443.           flinfo->symshndxbuf = destshndx;
  8444.           memset ((char *) destshndx + amt, 0, amt);
  8445.           flinfo->shndxbuf_size *= 2;
  8446.         }
  8447.       destshndx += bfd_get_symcount (flinfo->output_bfd);
  8448.     }
  8449.  
  8450.   bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
  8451.   flinfo->symbuf_count += 1;
  8452.   bfd_get_symcount (flinfo->output_bfd) += 1;
  8453.  
  8454.   return 1;
  8455. }
  8456.  
  8457. /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
  8458.  
  8459. static bfd_boolean
  8460. check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
  8461. {
  8462.   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
  8463.       && sym->st_shndx < SHN_LORESERVE)
  8464.     {
  8465.       /* The gABI doesn't support dynamic symbols in output sections
  8466.          beyond 64k.  */
  8467.       (*_bfd_error_handler)
  8468.         (_("%B: Too many sections: %d (>= %d)"),
  8469.          abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
  8470.       bfd_set_error (bfd_error_nonrepresentable_section);
  8471.       return FALSE;
  8472.     }
  8473.   return TRUE;
  8474. }
  8475.  
  8476. /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
  8477.    allowing an unsatisfied unversioned symbol in the DSO to match a
  8478.    versioned symbol that would normally require an explicit version.
  8479.    We also handle the case that a DSO references a hidden symbol
  8480.    which may be satisfied by a versioned symbol in another DSO.  */
  8481.  
  8482. static bfd_boolean
  8483. elf_link_check_versioned_symbol (struct bfd_link_info *info,
  8484.                                  const struct elf_backend_data *bed,
  8485.                                  struct elf_link_hash_entry *h)
  8486. {
  8487.   bfd *abfd;
  8488.   struct elf_link_loaded_list *loaded;
  8489.  
  8490.   if (!is_elf_hash_table (info->hash))
  8491.     return FALSE;
  8492.  
  8493.   /* Check indirect symbol.  */
  8494.   while (h->root.type == bfd_link_hash_indirect)
  8495.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  8496.  
  8497.   switch (h->root.type)
  8498.     {
  8499.     default:
  8500.       abfd = NULL;
  8501.       break;
  8502.  
  8503.     case bfd_link_hash_undefined:
  8504.     case bfd_link_hash_undefweak:
  8505.       abfd = h->root.u.undef.abfd;
  8506.       if ((abfd->flags & DYNAMIC) == 0
  8507.           || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
  8508.         return FALSE;
  8509.       break;
  8510.  
  8511.     case bfd_link_hash_defined:
  8512.     case bfd_link_hash_defweak:
  8513.       abfd = h->root.u.def.section->owner;
  8514.       break;
  8515.  
  8516.     case bfd_link_hash_common:
  8517.       abfd = h->root.u.c.p->section->owner;
  8518.       break;
  8519.     }
  8520.   BFD_ASSERT (abfd != NULL);
  8521.  
  8522.   for (loaded = elf_hash_table (info)->loaded;
  8523.        loaded != NULL;
  8524.        loaded = loaded->next)
  8525.     {
  8526.       bfd *input;
  8527.       Elf_Internal_Shdr *hdr;
  8528.       bfd_size_type symcount;
  8529.       bfd_size_type extsymcount;
  8530.       bfd_size_type extsymoff;
  8531.       Elf_Internal_Shdr *versymhdr;
  8532.       Elf_Internal_Sym *isym;
  8533.       Elf_Internal_Sym *isymend;
  8534.       Elf_Internal_Sym *isymbuf;
  8535.       Elf_External_Versym *ever;
  8536.       Elf_External_Versym *extversym;
  8537.  
  8538.       input = loaded->abfd;
  8539.  
  8540.       /* We check each DSO for a possible hidden versioned definition.  */
  8541.       if (input == abfd
  8542.           || (input->flags & DYNAMIC) == 0
  8543.           || elf_dynversym (input) == 0)
  8544.         continue;
  8545.  
  8546.       hdr = &elf_tdata (input)->dynsymtab_hdr;
  8547.  
  8548.       symcount = hdr->sh_size / bed->s->sizeof_sym;
  8549.       if (elf_bad_symtab (input))
  8550.         {
  8551.           extsymcount = symcount;
  8552.           extsymoff = 0;
  8553.         }
  8554.       else
  8555.         {
  8556.           extsymcount = symcount - hdr->sh_info;
  8557.           extsymoff = hdr->sh_info;
  8558.         }
  8559.  
  8560.       if (extsymcount == 0)
  8561.         continue;
  8562.  
  8563.       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
  8564.                                       NULL, NULL, NULL);
  8565.       if (isymbuf == NULL)
  8566.         return FALSE;
  8567.  
  8568.       /* Read in any version definitions.  */
  8569.       versymhdr = &elf_tdata (input)->dynversym_hdr;
  8570.       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
  8571.       if (extversym == NULL)
  8572.         goto error_ret;
  8573.  
  8574.       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
  8575.           || (bfd_bread (extversym, versymhdr->sh_size, input)
  8576.               != versymhdr->sh_size))
  8577.         {
  8578.           free (extversym);
  8579.         error_ret:
  8580.           free (isymbuf);
  8581.           return FALSE;
  8582.         }
  8583.  
  8584.       ever = extversym + extsymoff;
  8585.       isymend = isymbuf + extsymcount;
  8586.       for (isym = isymbuf; isym < isymend; isym++, ever++)
  8587.         {
  8588.           const char *name;
  8589.           Elf_Internal_Versym iver;
  8590.           unsigned short version_index;
  8591.  
  8592.           if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
  8593.               || isym->st_shndx == SHN_UNDEF)
  8594.             continue;
  8595.  
  8596.           name = bfd_elf_string_from_elf_section (input,
  8597.                                                   hdr->sh_link,
  8598.                                                   isym->st_name);
  8599.           if (strcmp (name, h->root.root.string) != 0)
  8600.             continue;
  8601.  
  8602.           _bfd_elf_swap_versym_in (input, ever, &iver);
  8603.  
  8604.           if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  8605.               && !(h->def_regular
  8606.                    && h->forced_local))
  8607.             {
  8608.               /* If we have a non-hidden versioned sym, then it should
  8609.                  have provided a definition for the undefined sym unless
  8610.                  it is defined in a non-shared object and forced local.
  8611.                */
  8612.               abort ();
  8613.             }
  8614.  
  8615.           version_index = iver.vs_vers & VERSYM_VERSION;
  8616.           if (version_index == 1 || version_index == 2)
  8617.             {
  8618.               /* This is the base or first version.  We can use it.  */
  8619.               free (extversym);
  8620.               free (isymbuf);
  8621.               return TRUE;
  8622.             }
  8623.         }
  8624.  
  8625.       free (extversym);
  8626.       free (isymbuf);
  8627.     }
  8628.  
  8629.   return FALSE;
  8630. }
  8631.  
  8632. /* Add an external symbol to the symbol table.  This is called from
  8633.    the hash table traversal routine.  When generating a shared object,
  8634.    we go through the symbol table twice.  The first time we output
  8635.    anything that might have been forced to local scope in a version
  8636.    script.  The second time we output the symbols that are still
  8637.    global symbols.  */
  8638.  
  8639. static bfd_boolean
  8640. elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
  8641. {
  8642.   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
  8643.   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
  8644.   struct elf_final_link_info *flinfo = eoinfo->flinfo;
  8645.   bfd_boolean strip;
  8646.   Elf_Internal_Sym sym;
  8647.   asection *input_sec;
  8648.   const struct elf_backend_data *bed;
  8649.   long indx;
  8650.   int ret;
  8651.  
  8652.   if (h->root.type == bfd_link_hash_warning)
  8653.     {
  8654.       h = (struct elf_link_hash_entry *) h->root.u.i.link;
  8655.       if (h->root.type == bfd_link_hash_new)
  8656.         return TRUE;
  8657.     }
  8658.  
  8659.   /* Decide whether to output this symbol in this pass.  */
  8660.   if (eoinfo->localsyms)
  8661.     {
  8662.       if (!h->forced_local)
  8663.         return TRUE;
  8664.       if (eoinfo->second_pass
  8665.           && !((h->root.type == bfd_link_hash_defined
  8666.                 || h->root.type == bfd_link_hash_defweak)
  8667.                && h->root.u.def.section->output_section != NULL))
  8668.         return TRUE;
  8669.     }
  8670.   else
  8671.     {
  8672.       if (h->forced_local)
  8673.         return TRUE;
  8674.     }
  8675.  
  8676.   bed = get_elf_backend_data (flinfo->output_bfd);
  8677.  
  8678.   if (h->root.type == bfd_link_hash_undefined)
  8679.     {
  8680.       /* If we have an undefined symbol reference here then it must have
  8681.          come from a shared library that is being linked in.  (Undefined
  8682.          references in regular files have already been handled unless
  8683.          they are in unreferenced sections which are removed by garbage
  8684.          collection).  */
  8685.       bfd_boolean ignore_undef = FALSE;
  8686.  
  8687.       /* Some symbols may be special in that the fact that they're
  8688.          undefined can be safely ignored - let backend determine that.  */
  8689.       if (bed->elf_backend_ignore_undef_symbol)
  8690.         ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
  8691.  
  8692.       /* If we are reporting errors for this situation then do so now.  */
  8693.       if (!ignore_undef
  8694.           && h->ref_dynamic
  8695.           && (!h->ref_regular || flinfo->info->gc_sections)
  8696.           && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
  8697.           && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
  8698.         {
  8699.           if (!(flinfo->info->callbacks->undefined_symbol
  8700.                 (flinfo->info, h->root.root.string,
  8701.                  h->ref_regular ? NULL : h->root.u.undef.abfd,
  8702.                  NULL, 0,
  8703.                  (flinfo->info->unresolved_syms_in_shared_libs
  8704.                   == RM_GENERATE_ERROR))))
  8705.             {
  8706.               bfd_set_error (bfd_error_bad_value);
  8707.               eoinfo->failed = TRUE;
  8708.               return FALSE;
  8709.             }
  8710.         }
  8711.     }
  8712.  
  8713.   /* We should also warn if a forced local symbol is referenced from
  8714.      shared libraries.  */
  8715.   if (!flinfo->info->relocatable
  8716.       && flinfo->info->executable
  8717.       && h->forced_local
  8718.       && h->ref_dynamic
  8719.       && h->def_regular
  8720.       && !h->dynamic_def
  8721.       && h->ref_dynamic_nonweak
  8722.       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
  8723.     {
  8724.       bfd *def_bfd;
  8725.       const char *msg;
  8726.       struct elf_link_hash_entry *hi = h;
  8727.  
  8728.       /* Check indirect symbol.  */
  8729.       while (hi->root.type == bfd_link_hash_indirect)
  8730.         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  8731.  
  8732.       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  8733.         msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
  8734.       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
  8735.         msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
  8736.       else
  8737.         msg = _("%B: local symbol `%s' in %B is referenced by DSO");
  8738.       def_bfd = flinfo->output_bfd;
  8739.       if (hi->root.u.def.section != bfd_abs_section_ptr)
  8740.         def_bfd = hi->root.u.def.section->owner;
  8741.       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
  8742.                              h->root.root.string);
  8743.       bfd_set_error (bfd_error_bad_value);
  8744.       eoinfo->failed = TRUE;
  8745.       return FALSE;
  8746.     }
  8747.  
  8748.   /* We don't want to output symbols that have never been mentioned by
  8749.      a regular file, or that we have been told to strip.  However, if
  8750.      h->indx is set to -2, the symbol is used by a reloc and we must
  8751.      output it.  */
  8752.   if (h->indx == -2)
  8753.     strip = FALSE;
  8754.   else if ((h->def_dynamic
  8755.             || h->ref_dynamic
  8756.             || h->root.type == bfd_link_hash_new)
  8757.            && !h->def_regular
  8758.            && !h->ref_regular)
  8759.     strip = TRUE;
  8760.   else if (flinfo->info->strip == strip_all)
  8761.     strip = TRUE;
  8762.   else if (flinfo->info->strip == strip_some
  8763.            && bfd_hash_lookup (flinfo->info->keep_hash,
  8764.                                h->root.root.string, FALSE, FALSE) == NULL)
  8765.     strip = TRUE;
  8766.   else if ((h->root.type == bfd_link_hash_defined
  8767.             || h->root.type == bfd_link_hash_defweak)
  8768.            && ((flinfo->info->strip_discarded
  8769.                 && discarded_section (h->root.u.def.section))
  8770.                || (h->root.u.def.section->owner != NULL
  8771.                    && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
  8772.     strip = TRUE;
  8773.   else if ((h->root.type == bfd_link_hash_undefined
  8774.             || h->root.type == bfd_link_hash_undefweak)
  8775.            && h->root.u.undef.abfd != NULL
  8776.            && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
  8777.     strip = TRUE;
  8778.   else
  8779.     strip = FALSE;
  8780.  
  8781.   /* If we're stripping it, and it's not a dynamic symbol, there's
  8782.      nothing else to do unless it is a forced local symbol or a
  8783.      STT_GNU_IFUNC symbol.  */
  8784.   if (strip
  8785.       && h->dynindx == -1
  8786.       && h->type != STT_GNU_IFUNC
  8787.       && !h->forced_local)
  8788.     return TRUE;
  8789.  
  8790.   sym.st_value = 0;
  8791.   sym.st_size = h->size;
  8792.   sym.st_other = h->other;
  8793.   if (h->forced_local)
  8794.     {
  8795.       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
  8796.       /* Turn off visibility on local symbol.  */
  8797.       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  8798.     }
  8799.   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
  8800.   else if (h->unique_global && h->def_regular)
  8801.     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
  8802.   else if (h->root.type == bfd_link_hash_undefweak
  8803.            || h->root.type == bfd_link_hash_defweak)
  8804.     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
  8805.   else
  8806.     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
  8807.   sym.st_target_internal = h->target_internal;
  8808.  
  8809.   switch (h->root.type)
  8810.     {
  8811.     default:
  8812.     case bfd_link_hash_new:
  8813.     case bfd_link_hash_warning:
  8814.       abort ();
  8815.       return FALSE;
  8816.  
  8817.     case bfd_link_hash_undefined:
  8818.     case bfd_link_hash_undefweak:
  8819.       input_sec = bfd_und_section_ptr;
  8820.       sym.st_shndx = SHN_UNDEF;
  8821.       break;
  8822.  
  8823.     case bfd_link_hash_defined:
  8824.     case bfd_link_hash_defweak:
  8825.       {
  8826.         input_sec = h->root.u.def.section;
  8827.         if (input_sec->output_section != NULL)
  8828.           {
  8829.             if (eoinfo->localsyms && flinfo->filesym_count == 1)
  8830.               {
  8831.                 bfd_boolean second_pass_sym
  8832.                   = (input_sec->owner == flinfo->output_bfd
  8833.                      || input_sec->owner == NULL
  8834.                      || (input_sec->flags & SEC_LINKER_CREATED) != 0
  8835.                      || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
  8836.  
  8837.                 eoinfo->need_second_pass |= second_pass_sym;
  8838.                 if (eoinfo->second_pass != second_pass_sym)
  8839.                   return TRUE;
  8840.               }
  8841.  
  8842.             sym.st_shndx =
  8843.               _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
  8844.                                                  input_sec->output_section);
  8845.             if (sym.st_shndx == SHN_BAD)
  8846.               {
  8847.                 (*_bfd_error_handler)
  8848.                   (_("%B: could not find output section %A for input section %A"),
  8849.                    flinfo->output_bfd, input_sec->output_section, input_sec);
  8850.                 bfd_set_error (bfd_error_nonrepresentable_section);
  8851.                 eoinfo->failed = TRUE;
  8852.                 return FALSE;
  8853.               }
  8854.  
  8855.             /* ELF symbols in relocatable files are section relative,
  8856.                but in nonrelocatable files they are virtual
  8857.                addresses.  */
  8858.             sym.st_value = h->root.u.def.value + input_sec->output_offset;
  8859.             if (!flinfo->info->relocatable)
  8860.               {
  8861.                 sym.st_value += input_sec->output_section->vma;
  8862.                 if (h->type == STT_TLS)
  8863.                   {
  8864.                     asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
  8865.                     if (tls_sec != NULL)
  8866.                       sym.st_value -= tls_sec->vma;
  8867.                     else
  8868.                       {
  8869.                         /* The TLS section may have been garbage collected.  */
  8870.                         BFD_ASSERT (flinfo->info->gc_sections
  8871.                                     && !input_sec->gc_mark);
  8872.                       }
  8873.                   }
  8874.               }
  8875.           }
  8876.         else
  8877.           {
  8878.             BFD_ASSERT (input_sec->owner == NULL
  8879.                         || (input_sec->owner->flags & DYNAMIC) != 0);
  8880.             sym.st_shndx = SHN_UNDEF;
  8881.             input_sec = bfd_und_section_ptr;
  8882.           }
  8883.       }
  8884.       break;
  8885.  
  8886.     case bfd_link_hash_common:
  8887.       input_sec = h->root.u.c.p->section;
  8888.       sym.st_shndx = bed->common_section_index (input_sec);
  8889.       sym.st_value = 1 << h->root.u.c.p->alignment_power;
  8890.       break;
  8891.  
  8892.     case bfd_link_hash_indirect:
  8893.       /* These symbols are created by symbol versioning.  They point
  8894.          to the decorated version of the name.  For example, if the
  8895.          symbol foo@@GNU_1.2 is the default, which should be used when
  8896.          foo is used with no version, then we add an indirect symbol
  8897.          foo which points to foo@@GNU_1.2.  We ignore these symbols,
  8898.          since the indirected symbol is already in the hash table.  */
  8899.       return TRUE;
  8900.     }
  8901.  
  8902.   /* Give the processor backend a chance to tweak the symbol value,
  8903.      and also to finish up anything that needs to be done for this
  8904.      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
  8905.      forced local syms when non-shared is due to a historical quirk.
  8906.      STT_GNU_IFUNC symbol must go through PLT.  */
  8907.   if ((h->type == STT_GNU_IFUNC
  8908.        && h->def_regular
  8909.        && !flinfo->info->relocatable)
  8910.       || ((h->dynindx != -1
  8911.            || h->forced_local)
  8912.           && ((flinfo->info->shared
  8913.                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  8914.                    || h->root.type != bfd_link_hash_undefweak))
  8915.               || !h->forced_local)
  8916.           && elf_hash_table (flinfo->info)->dynamic_sections_created))
  8917.     {
  8918.       if (! ((*bed->elf_backend_finish_dynamic_symbol)
  8919.              (flinfo->output_bfd, flinfo->info, h, &sym)))
  8920.         {
  8921.           eoinfo->failed = TRUE;
  8922.           return FALSE;
  8923.         }
  8924.     }
  8925.  
  8926.   /* If we are marking the symbol as undefined, and there are no
  8927.      non-weak references to this symbol from a regular object, then
  8928.      mark the symbol as weak undefined; if there are non-weak
  8929.      references, mark the symbol as strong.  We can't do this earlier,
  8930.      because it might not be marked as undefined until the
  8931.      finish_dynamic_symbol routine gets through with it.  */
  8932.   if (sym.st_shndx == SHN_UNDEF
  8933.       && h->ref_regular
  8934.       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
  8935.           || ELF_ST_BIND (sym.st_info) == STB_WEAK))
  8936.     {
  8937.       int bindtype;
  8938.       unsigned int type = ELF_ST_TYPE (sym.st_info);
  8939.  
  8940.       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
  8941.       if (type == STT_GNU_IFUNC)
  8942.         type = STT_FUNC;
  8943.  
  8944.       if (h->ref_regular_nonweak)
  8945.         bindtype = STB_GLOBAL;
  8946.       else
  8947.         bindtype = STB_WEAK;
  8948.       sym.st_info = ELF_ST_INFO (bindtype, type);
  8949.     }
  8950.  
  8951.   /* If this is a symbol defined in a dynamic library, don't use the
  8952.      symbol size from the dynamic library.  Relinking an executable
  8953.      against a new library may introduce gratuitous changes in the
  8954.      executable's symbols if we keep the size.  */
  8955.   if (sym.st_shndx == SHN_UNDEF
  8956.       && !h->def_regular
  8957.       && h->def_dynamic)
  8958.     sym.st_size = 0;
  8959.  
  8960.   /* If a non-weak symbol with non-default visibility is not defined
  8961.      locally, it is a fatal error.  */
  8962.   if (!flinfo->info->relocatable
  8963.       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
  8964.       && ELF_ST_BIND (sym.st_info) != STB_WEAK
  8965.       && h->root.type == bfd_link_hash_undefined
  8966.       && !h->def_regular)
  8967.     {
  8968.       const char *msg;
  8969.  
  8970.       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
  8971.         msg = _("%B: protected symbol `%s' isn't defined");
  8972.       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
  8973.         msg = _("%B: internal symbol `%s' isn't defined");
  8974.       else
  8975.         msg = _("%B: hidden symbol `%s' isn't defined");
  8976.       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
  8977.       bfd_set_error (bfd_error_bad_value);
  8978.       eoinfo->failed = TRUE;
  8979.       return FALSE;
  8980.     }
  8981.  
  8982.   /* If this symbol should be put in the .dynsym section, then put it
  8983.      there now.  We already know the symbol index.  We also fill in
  8984.      the entry in the .hash section.  */
  8985.   if (flinfo->dynsym_sec != NULL
  8986.       && h->dynindx != -1
  8987.       && elf_hash_table (flinfo->info)->dynamic_sections_created)
  8988.     {
  8989.       bfd_byte *esym;
  8990.  
  8991.       /* Since there is no version information in the dynamic string,
  8992.          if there is no version info in symbol version section, we will
  8993.          have a run-time problem.  */
  8994.       if (h->verinfo.verdef == NULL)
  8995.         {
  8996.           char *p = strrchr (h->root.root.string, ELF_VER_CHR);
  8997.  
  8998.           if (p && p [1] != '\0')
  8999.             {
  9000.               (*_bfd_error_handler)
  9001.                 (_("%B: No symbol version section for versioned symbol `%s'"),
  9002.                  flinfo->output_bfd, h->root.root.string);
  9003.               eoinfo->failed = TRUE;
  9004.               return FALSE;
  9005.             }
  9006.         }
  9007.  
  9008.       sym.st_name = h->dynstr_index;
  9009.       esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
  9010.       if (!check_dynsym (flinfo->output_bfd, &sym))
  9011.         {
  9012.           eoinfo->failed = TRUE;
  9013.           return FALSE;
  9014.         }
  9015.       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
  9016.  
  9017.       if (flinfo->hash_sec != NULL)
  9018.         {
  9019.           size_t hash_entry_size;
  9020.           bfd_byte *bucketpos;
  9021.           bfd_vma chain;
  9022.           size_t bucketcount;
  9023.           size_t bucket;
  9024.  
  9025.           bucketcount = elf_hash_table (flinfo->info)->bucketcount;
  9026.           bucket = h->u.elf_hash_value % bucketcount;
  9027.  
  9028.           hash_entry_size
  9029.             = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
  9030.           bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
  9031.                        + (bucket + 2) * hash_entry_size);
  9032.           chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
  9033.           bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
  9034.                    bucketpos);
  9035.           bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
  9036.                    ((bfd_byte *) flinfo->hash_sec->contents
  9037.                     + (bucketcount + 2 + h->dynindx) * hash_entry_size));
  9038.         }
  9039.  
  9040.       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
  9041.         {
  9042.           Elf_Internal_Versym iversym;
  9043.           Elf_External_Versym *eversym;
  9044.  
  9045.           if (!h->def_regular)
  9046.             {
  9047.               if (h->verinfo.verdef == NULL)
  9048.                 iversym.vs_vers = 0;
  9049.               else
  9050.                 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
  9051.             }
  9052.           else
  9053.             {
  9054.               if (h->verinfo.vertree == NULL)
  9055.                 iversym.vs_vers = 1;
  9056.               else
  9057.                 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
  9058.               if (flinfo->info->create_default_symver)
  9059.                 iversym.vs_vers++;
  9060.             }
  9061.  
  9062.           if (h->hidden)
  9063.             iversym.vs_vers |= VERSYM_HIDDEN;
  9064.  
  9065.           eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
  9066.           eversym += h->dynindx;
  9067.           _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
  9068.         }
  9069.     }
  9070.  
  9071.   /* If we're stripping it, then it was just a dynamic symbol, and
  9072.      there's nothing else to do.  */
  9073.   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
  9074.     return TRUE;
  9075.  
  9076.   indx = bfd_get_symcount (flinfo->output_bfd);
  9077.   ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
  9078.   if (ret == 0)
  9079.     {
  9080.       eoinfo->failed = TRUE;
  9081.       return FALSE;
  9082.     }
  9083.   else if (ret == 1)
  9084.     h->indx = indx;
  9085.   else if (h->indx == -2)
  9086.     abort();
  9087.  
  9088.   return TRUE;
  9089. }
  9090.  
  9091. /* Return TRUE if special handling is done for relocs in SEC against
  9092.    symbols defined in discarded sections.  */
  9093.  
  9094. static bfd_boolean
  9095. elf_section_ignore_discarded_relocs (asection *sec)
  9096. {
  9097.   const struct elf_backend_data *bed;
  9098.  
  9099.   switch (sec->sec_info_type)
  9100.     {
  9101.     case SEC_INFO_TYPE_STABS:
  9102.     case SEC_INFO_TYPE_EH_FRAME:
  9103.       return TRUE;
  9104.     default:
  9105.       break;
  9106.     }
  9107.  
  9108.   bed = get_elf_backend_data (sec->owner);
  9109.   if (bed->elf_backend_ignore_discarded_relocs != NULL
  9110.       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
  9111.     return TRUE;
  9112.  
  9113.   return FALSE;
  9114. }
  9115.  
  9116. /* Return a mask saying how ld should treat relocations in SEC against
  9117.    symbols defined in discarded sections.  If this function returns
  9118.    COMPLAIN set, ld will issue a warning message.  If this function
  9119.    returns PRETEND set, and the discarded section was link-once and the
  9120.    same size as the kept link-once section, ld will pretend that the
  9121.    symbol was actually defined in the kept section.  Otherwise ld will
  9122.    zero the reloc (at least that is the intent, but some cooperation by
  9123.    the target dependent code is needed, particularly for REL targets).  */
  9124.  
  9125. unsigned int
  9126. _bfd_elf_default_action_discarded (asection *sec)
  9127. {
  9128.   if (sec->flags & SEC_DEBUGGING)
  9129.     return PRETEND;
  9130.  
  9131.   if (strcmp (".eh_frame", sec->name) == 0)
  9132.     return 0;
  9133.  
  9134.   if (strcmp (".gcc_except_table", sec->name) == 0)
  9135.     return 0;
  9136.  
  9137.   return COMPLAIN | PRETEND;
  9138. }
  9139.  
  9140. /* Find a match between a section and a member of a section group.  */
  9141.  
  9142. static asection *
  9143. match_group_member (asection *sec, asection *group,
  9144.                     struct bfd_link_info *info)
  9145. {
  9146.   asection *first = elf_next_in_group (group);
  9147.   asection *s = first;
  9148.  
  9149.   while (s != NULL)
  9150.     {
  9151.       if (bfd_elf_match_symbols_in_sections (s, sec, info))
  9152.         return s;
  9153.  
  9154.       s = elf_next_in_group (s);
  9155.       if (s == first)
  9156.         break;
  9157.     }
  9158.  
  9159.   return NULL;
  9160. }
  9161.  
  9162. /* Check if the kept section of a discarded section SEC can be used
  9163.    to replace it.  Return the replacement if it is OK.  Otherwise return
  9164.    NULL.  */
  9165.  
  9166. asection *
  9167. _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
  9168. {
  9169.   asection *kept;
  9170.  
  9171.   kept = sec->kept_section;
  9172.   if (kept != NULL)
  9173.     {
  9174.       if ((kept->flags & SEC_GROUP) != 0)
  9175.         kept = match_group_member (sec, kept, info);
  9176.       if (kept != NULL
  9177.           && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
  9178.               != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
  9179.         kept = NULL;
  9180.       sec->kept_section = kept;
  9181.     }
  9182.   return kept;
  9183. }
  9184.  
  9185. /* Link an input file into the linker output file.  This function
  9186.    handles all the sections and relocations of the input file at once.
  9187.    This is so that we only have to read the local symbols once, and
  9188.    don't have to keep them in memory.  */
  9189.  
  9190. static bfd_boolean
  9191. elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
  9192. {
  9193.   int (*relocate_section)
  9194.     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  9195.      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  9196.   bfd *output_bfd;
  9197.   Elf_Internal_Shdr *symtab_hdr;
  9198.   size_t locsymcount;
  9199.   size_t extsymoff;
  9200.   Elf_Internal_Sym *isymbuf;
  9201.   Elf_Internal_Sym *isym;
  9202.   Elf_Internal_Sym *isymend;
  9203.   long *pindex;
  9204.   asection **ppsection;
  9205.   asection *o;
  9206.   const struct elf_backend_data *bed;
  9207.   struct elf_link_hash_entry **sym_hashes;
  9208.   bfd_size_type address_size;
  9209.   bfd_vma r_type_mask;
  9210.   int r_sym_shift;
  9211.   bfd_boolean have_file_sym = FALSE;
  9212.  
  9213.   output_bfd = flinfo->output_bfd;
  9214.   bed = get_elf_backend_data (output_bfd);
  9215.   relocate_section = bed->elf_backend_relocate_section;
  9216.  
  9217.   /* If this is a dynamic object, we don't want to do anything here:
  9218.      we don't want the local symbols, and we don't want the section
  9219.      contents.  */
  9220.   if ((input_bfd->flags & DYNAMIC) != 0)
  9221.     return TRUE;
  9222.  
  9223.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  9224.   if (elf_bad_symtab (input_bfd))
  9225.     {
  9226.       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  9227.       extsymoff = 0;
  9228.     }
  9229.   else
  9230.     {
  9231.       locsymcount = symtab_hdr->sh_info;
  9232.       extsymoff = symtab_hdr->sh_info;
  9233.     }
  9234.  
  9235.   /* Read the local symbols.  */
  9236.   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  9237.   if (isymbuf == NULL && locsymcount != 0)
  9238.     {
  9239.       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
  9240.                                       flinfo->internal_syms,
  9241.                                       flinfo->external_syms,
  9242.                                       flinfo->locsym_shndx);
  9243.       if (isymbuf == NULL)
  9244.         return FALSE;
  9245.     }
  9246.  
  9247.   /* Find local symbol sections and adjust values of symbols in
  9248.      SEC_MERGE sections.  Write out those local symbols we know are
  9249.      going into the output file.  */
  9250.   isymend = isymbuf + locsymcount;
  9251.   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
  9252.        isym < isymend;
  9253.        isym++, pindex++, ppsection++)
  9254.     {
  9255.       asection *isec;
  9256.       const char *name;
  9257.       Elf_Internal_Sym osym;
  9258.       long indx;
  9259.       int ret;
  9260.  
  9261.       *pindex = -1;
  9262.  
  9263.       if (elf_bad_symtab (input_bfd))
  9264.         {
  9265.           if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
  9266.             {
  9267.               *ppsection = NULL;
  9268.               continue;
  9269.             }
  9270.         }
  9271.  
  9272.       if (isym->st_shndx == SHN_UNDEF)
  9273.         isec = bfd_und_section_ptr;
  9274.       else if (isym->st_shndx == SHN_ABS)
  9275.         isec = bfd_abs_section_ptr;
  9276.       else if (isym->st_shndx == SHN_COMMON)
  9277.         isec = bfd_com_section_ptr;
  9278.       else
  9279.         {
  9280.           isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  9281.           if (isec == NULL)
  9282.             {
  9283.               /* Don't attempt to output symbols with st_shnx in the
  9284.                  reserved range other than SHN_ABS and SHN_COMMON.  */
  9285.               *ppsection = NULL;
  9286.               continue;
  9287.             }
  9288.           else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
  9289.                    && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
  9290.             isym->st_value =
  9291.               _bfd_merged_section_offset (output_bfd, &isec,
  9292.                                           elf_section_data (isec)->sec_info,
  9293.                                           isym->st_value);
  9294.         }
  9295.  
  9296.       *ppsection = isec;
  9297.  
  9298.       /* Don't output the first, undefined, symbol.  */
  9299.       if (ppsection == flinfo->sections)
  9300.         continue;
  9301.  
  9302.       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  9303.         {
  9304.           /* We never output section symbols.  Instead, we use the
  9305.              section symbol of the corresponding section in the output
  9306.              file.  */
  9307.           continue;
  9308.         }
  9309.  
  9310.       /* If we are stripping all symbols, we don't want to output this
  9311.          one.  */
  9312.       if (flinfo->info->strip == strip_all)
  9313.         continue;
  9314.  
  9315.       /* If we are discarding all local symbols, we don't want to
  9316.          output this one.  If we are generating a relocatable output
  9317.          file, then some of the local symbols may be required by
  9318.          relocs; we output them below as we discover that they are
  9319.          needed.  */
  9320.       if (flinfo->info->discard == discard_all)
  9321.         continue;
  9322.  
  9323.       /* If this symbol is defined in a section which we are
  9324.          discarding, we don't need to keep it.  */
  9325.       if (isym->st_shndx != SHN_UNDEF
  9326.           && isym->st_shndx < SHN_LORESERVE
  9327.           && bfd_section_removed_from_list (output_bfd,
  9328.                                             isec->output_section))
  9329.         continue;
  9330.  
  9331.       /* Get the name of the symbol.  */
  9332.       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
  9333.                                               isym->st_name);
  9334.       if (name == NULL)
  9335.         return FALSE;
  9336.  
  9337.       /* See if we are discarding symbols with this name.  */
  9338.       if ((flinfo->info->strip == strip_some
  9339.            && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
  9340.                == NULL))
  9341.           || (((flinfo->info->discard == discard_sec_merge
  9342.                 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
  9343.                || flinfo->info->discard == discard_l)
  9344.               && bfd_is_local_label_name (input_bfd, name)))
  9345.         continue;
  9346.  
  9347.       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
  9348.         {
  9349.           have_file_sym = TRUE;
  9350.           flinfo->filesym_count += 1;
  9351.         }
  9352.       if (!have_file_sym)
  9353.         {
  9354.           /* In the absence of debug info, bfd_find_nearest_line uses
  9355.              FILE symbols to determine the source file for local
  9356.              function symbols.  Provide a FILE symbol here if input
  9357.              files lack such, so that their symbols won't be
  9358.              associated with a previous input file.  It's not the
  9359.              source file, but the best we can do.  */
  9360.           have_file_sym = TRUE;
  9361.           flinfo->filesym_count += 1;
  9362.           memset (&osym, 0, sizeof (osym));
  9363.           osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  9364.           osym.st_shndx = SHN_ABS;
  9365.           if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
  9366.                                     bfd_abs_section_ptr, NULL))
  9367.             return FALSE;
  9368.         }
  9369.  
  9370.       osym = *isym;
  9371.  
  9372.       /* Adjust the section index for the output file.  */
  9373.       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9374.                                                          isec->output_section);
  9375.       if (osym.st_shndx == SHN_BAD)
  9376.         return FALSE;
  9377.  
  9378.       /* ELF symbols in relocatable files are section relative, but
  9379.          in executable files they are virtual addresses.  Note that
  9380.          this code assumes that all ELF sections have an associated
  9381.          BFD section with a reasonable value for output_offset; below
  9382.          we assume that they also have a reasonable value for
  9383.          output_section.  Any special sections must be set up to meet
  9384.          these requirements.  */
  9385.       osym.st_value += isec->output_offset;
  9386.       if (!flinfo->info->relocatable)
  9387.         {
  9388.           osym.st_value += isec->output_section->vma;
  9389.           if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
  9390.             {
  9391.               /* STT_TLS symbols are relative to PT_TLS segment base.  */
  9392.               BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
  9393.               osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
  9394.             }
  9395.         }
  9396.  
  9397.       indx = bfd_get_symcount (output_bfd);
  9398.       ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
  9399.       if (ret == 0)
  9400.         return FALSE;
  9401.       else if (ret == 1)
  9402.         *pindex = indx;
  9403.     }
  9404.  
  9405.   if (bed->s->arch_size == 32)
  9406.     {
  9407.       r_type_mask = 0xff;
  9408.       r_sym_shift = 8;
  9409.       address_size = 4;
  9410.     }
  9411.   else
  9412.     {
  9413.       r_type_mask = 0xffffffff;
  9414.       r_sym_shift = 32;
  9415.       address_size = 8;
  9416.     }
  9417.  
  9418.   /* Relocate the contents of each section.  */
  9419.   sym_hashes = elf_sym_hashes (input_bfd);
  9420.   for (o = input_bfd->sections; o != NULL; o = o->next)
  9421.     {
  9422.       bfd_byte *contents;
  9423.  
  9424.       if (! o->linker_mark)
  9425.         {
  9426.           /* This section was omitted from the link.  */
  9427.           continue;
  9428.         }
  9429.  
  9430.       if (flinfo->info->relocatable
  9431.           && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
  9432.         {
  9433.           /* Deal with the group signature symbol.  */
  9434.           struct bfd_elf_section_data *sec_data = elf_section_data (o);
  9435.           unsigned long symndx = sec_data->this_hdr.sh_info;
  9436.           asection *osec = o->output_section;
  9437.  
  9438.           if (symndx >= locsymcount
  9439.               || (elf_bad_symtab (input_bfd)
  9440.                   && flinfo->sections[symndx] == NULL))
  9441.             {
  9442.               struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
  9443.               while (h->root.type == bfd_link_hash_indirect
  9444.                      || h->root.type == bfd_link_hash_warning)
  9445.                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9446.               /* Arrange for symbol to be output.  */
  9447.               h->indx = -2;
  9448.               elf_section_data (osec)->this_hdr.sh_info = -2;
  9449.             }
  9450.           else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
  9451.             {
  9452.               /* We'll use the output section target_index.  */
  9453.               asection *sec = flinfo->sections[symndx]->output_section;
  9454.               elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
  9455.             }
  9456.           else
  9457.             {
  9458.               if (flinfo->indices[symndx] == -1)
  9459.                 {
  9460.                   /* Otherwise output the local symbol now.  */
  9461.                   Elf_Internal_Sym sym = isymbuf[symndx];
  9462.                   asection *sec = flinfo->sections[symndx]->output_section;
  9463.                   const char *name;
  9464.                   long indx;
  9465.                   int ret;
  9466.  
  9467.                   name = bfd_elf_string_from_elf_section (input_bfd,
  9468.                                                           symtab_hdr->sh_link,
  9469.                                                           sym.st_name);
  9470.                   if (name == NULL)
  9471.                     return FALSE;
  9472.  
  9473.                   sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9474.                                                                     sec);
  9475.                   if (sym.st_shndx == SHN_BAD)
  9476.                     return FALSE;
  9477.  
  9478.                   sym.st_value += o->output_offset;
  9479.  
  9480.                   indx = bfd_get_symcount (output_bfd);
  9481.                   ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
  9482.                   if (ret == 0)
  9483.                     return FALSE;
  9484.                   else if (ret == 1)
  9485.                     flinfo->indices[symndx] = indx;
  9486.                   else
  9487.                     abort ();
  9488.                 }
  9489.               elf_section_data (osec)->this_hdr.sh_info
  9490.                 = flinfo->indices[symndx];
  9491.             }
  9492.         }
  9493.  
  9494.       if ((o->flags & SEC_HAS_CONTENTS) == 0
  9495.           || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
  9496.         continue;
  9497.  
  9498.       if ((o->flags & SEC_LINKER_CREATED) != 0)
  9499.         {
  9500.           /* Section was created by _bfd_elf_link_create_dynamic_sections
  9501.              or somesuch.  */
  9502.           continue;
  9503.         }
  9504.  
  9505.       /* Get the contents of the section.  They have been cached by a
  9506.          relaxation routine.  Note that o is a section in an input
  9507.          file, so the contents field will not have been set by any of
  9508.          the routines which work on output files.  */
  9509.       if (elf_section_data (o)->this_hdr.contents != NULL)
  9510.         contents = elf_section_data (o)->this_hdr.contents;
  9511.       else
  9512.         {
  9513.           contents = flinfo->contents;
  9514.           if (! bfd_get_full_section_contents (input_bfd, o, &contents))
  9515.             return FALSE;
  9516.         }
  9517.  
  9518.       if ((o->flags & SEC_RELOC) != 0)
  9519.         {
  9520.           Elf_Internal_Rela *internal_relocs;
  9521.           Elf_Internal_Rela *rel, *relend;
  9522.           int action_discarded;
  9523.           int ret;
  9524.  
  9525.           /* Get the swapped relocs.  */
  9526.           internal_relocs
  9527.             = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
  9528.                                          flinfo->internal_relocs, FALSE);
  9529.           if (internal_relocs == NULL
  9530.               && o->reloc_count > 0)
  9531.             return FALSE;
  9532.  
  9533.           /* We need to reverse-copy input .ctors/.dtors sections if
  9534.              they are placed in .init_array/.finit_array for output.  */
  9535.           if (o->size > address_size
  9536.               && ((strncmp (o->name, ".ctors", 6) == 0
  9537.                    && strcmp (o->output_section->name,
  9538.                               ".init_array") == 0)
  9539.                   || (strncmp (o->name, ".dtors", 6) == 0
  9540.                       && strcmp (o->output_section->name,
  9541.                                  ".fini_array") == 0))
  9542.               && (o->name[6] == 0 || o->name[6] == '.'))
  9543.             {
  9544.               if (o->size != o->reloc_count * address_size)
  9545.                 {
  9546.                   (*_bfd_error_handler)
  9547.                     (_("error: %B: size of section %A is not "
  9548.                        "multiple of address size"),
  9549.                      input_bfd, o);
  9550.                   bfd_set_error (bfd_error_on_input);
  9551.                   return FALSE;
  9552.                 }
  9553.               o->flags |= SEC_ELF_REVERSE_COPY;
  9554.             }
  9555.  
  9556.           action_discarded = -1;
  9557.           if (!elf_section_ignore_discarded_relocs (o))
  9558.             action_discarded = (*bed->action_discarded) (o);
  9559.  
  9560.           /* Run through the relocs evaluating complex reloc symbols and
  9561.              looking for relocs against symbols from discarded sections
  9562.              or section symbols from removed link-once sections.
  9563.              Complain about relocs against discarded sections.  Zero
  9564.              relocs against removed link-once sections.  */
  9565.  
  9566.           rel = internal_relocs;
  9567.           relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
  9568.           for ( ; rel < relend; rel++)
  9569.             {
  9570.               unsigned long r_symndx = rel->r_info >> r_sym_shift;
  9571.               unsigned int s_type;
  9572.               asection **ps, *sec;
  9573.               struct elf_link_hash_entry *h = NULL;
  9574.               const char *sym_name;
  9575.  
  9576.               if (r_symndx == STN_UNDEF)
  9577.                 continue;
  9578.  
  9579.               if (r_symndx >= locsymcount
  9580.                   || (elf_bad_symtab (input_bfd)
  9581.                       && flinfo->sections[r_symndx] == NULL))
  9582.                 {
  9583.                   h = sym_hashes[r_symndx - extsymoff];
  9584.  
  9585.                   /* Badly formatted input files can contain relocs that
  9586.                      reference non-existant symbols.  Check here so that
  9587.                      we do not seg fault.  */
  9588.                   if (h == NULL)
  9589.                     {
  9590.                       char buffer [32];
  9591.  
  9592.                       sprintf_vma (buffer, rel->r_info);
  9593.                       (*_bfd_error_handler)
  9594.                         (_("error: %B contains a reloc (0x%s) for section %A "
  9595.                            "that references a non-existent global symbol"),
  9596.                          input_bfd, o, buffer);
  9597.                       bfd_set_error (bfd_error_bad_value);
  9598.                       return FALSE;
  9599.                     }
  9600.  
  9601.                   while (h->root.type == bfd_link_hash_indirect
  9602.                          || h->root.type == bfd_link_hash_warning)
  9603.                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9604.  
  9605.                   s_type = h->type;
  9606.  
  9607.                   ps = NULL;
  9608.                   if (h->root.type == bfd_link_hash_defined
  9609.                       || h->root.type == bfd_link_hash_defweak)
  9610.                     ps = &h->root.u.def.section;
  9611.  
  9612.                   sym_name = h->root.root.string;
  9613.                 }
  9614.               else
  9615.                 {
  9616.                   Elf_Internal_Sym *sym = isymbuf + r_symndx;
  9617.  
  9618.                   s_type = ELF_ST_TYPE (sym->st_info);
  9619.                   ps = &flinfo->sections[r_symndx];
  9620.                   sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
  9621.                                                sym, *ps);
  9622.                 }
  9623.  
  9624.               if ((s_type == STT_RELC || s_type == STT_SRELC)
  9625.                   && !flinfo->info->relocatable)
  9626.                 {
  9627.                   bfd_vma val;
  9628.                   bfd_vma dot = (rel->r_offset
  9629.                                  + o->output_offset + o->output_section->vma);
  9630. #ifdef DEBUG
  9631.                   printf ("Encountered a complex symbol!");
  9632.                   printf (" (input_bfd %s, section %s, reloc %ld\n",
  9633.                           input_bfd->filename, o->name,
  9634.                           (long) (rel - internal_relocs));
  9635.                   printf (" symbol: idx  %8.8lx, name %s\n",
  9636.                           r_symndx, sym_name);
  9637.                   printf (" reloc : info %8.8lx, addr %8.8lx\n",
  9638.                           (unsigned long) rel->r_info,
  9639.                           (unsigned long) rel->r_offset);
  9640. #endif
  9641.                   if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
  9642.                                     isymbuf, locsymcount, s_type == STT_SRELC))
  9643.                     return FALSE;
  9644.  
  9645.                   /* Symbol evaluated OK.  Update to absolute value.  */
  9646.                   set_symbol_value (input_bfd, isymbuf, locsymcount,
  9647.                                     r_symndx, val);
  9648.                   continue;
  9649.                 }
  9650.  
  9651.               if (action_discarded != -1 && ps != NULL)
  9652.                 {
  9653.                   /* Complain if the definition comes from a
  9654.                      discarded section.  */
  9655.                   if ((sec = *ps) != NULL && discarded_section (sec))
  9656.                     {
  9657.                       BFD_ASSERT (r_symndx != STN_UNDEF);
  9658.                       if (action_discarded & COMPLAIN)
  9659.                         (*flinfo->info->callbacks->einfo)
  9660.                           (_("%X`%s' referenced in section `%A' of %B: "
  9661.                              "defined in discarded section `%A' of %B\n"),
  9662.                            sym_name, o, input_bfd, sec, sec->owner);
  9663.  
  9664.                       /* Try to do the best we can to support buggy old
  9665.                          versions of gcc.  Pretend that the symbol is
  9666.                          really defined in the kept linkonce section.
  9667.                          FIXME: This is quite broken.  Modifying the
  9668.                          symbol here means we will be changing all later
  9669.                          uses of the symbol, not just in this section.  */
  9670.                       if (action_discarded & PRETEND)
  9671.                         {
  9672.                           asection *kept;
  9673.  
  9674.                           kept = _bfd_elf_check_kept_section (sec,
  9675.                                                               flinfo->info);
  9676.                           if (kept != NULL)
  9677.                             {
  9678.                               *ps = kept;
  9679.                               continue;
  9680.                             }
  9681.                         }
  9682.                     }
  9683.                 }
  9684.             }
  9685.  
  9686.           /* Relocate the section by invoking a back end routine.
  9687.  
  9688.              The back end routine is responsible for adjusting the
  9689.              section contents as necessary, and (if using Rela relocs
  9690.              and generating a relocatable output file) adjusting the
  9691.              reloc addend as necessary.
  9692.  
  9693.              The back end routine does not have to worry about setting
  9694.              the reloc address or the reloc symbol index.
  9695.  
  9696.              The back end routine is given a pointer to the swapped in
  9697.              internal symbols, and can access the hash table entries
  9698.              for the external symbols via elf_sym_hashes (input_bfd).
  9699.  
  9700.              When generating relocatable output, the back end routine
  9701.              must handle STB_LOCAL/STT_SECTION symbols specially.  The
  9702.              output symbol is going to be a section symbol
  9703.              corresponding to the output section, which will require
  9704.              the addend to be adjusted.  */
  9705.  
  9706.           ret = (*relocate_section) (output_bfd, flinfo->info,
  9707.                                      input_bfd, o, contents,
  9708.                                      internal_relocs,
  9709.                                      isymbuf,
  9710.                                      flinfo->sections);
  9711.           if (!ret)
  9712.             return FALSE;
  9713.  
  9714.           if (ret == 2
  9715.               || flinfo->info->relocatable
  9716.               || flinfo->info->emitrelocations)
  9717.             {
  9718.               Elf_Internal_Rela *irela;
  9719.               Elf_Internal_Rela *irelaend, *irelamid;
  9720.               bfd_vma last_offset;
  9721.               struct elf_link_hash_entry **rel_hash;
  9722.               struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
  9723.               Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
  9724.               unsigned int next_erel;
  9725.               bfd_boolean rela_normal;
  9726.               struct bfd_elf_section_data *esdi, *esdo;
  9727.  
  9728.               esdi = elf_section_data (o);
  9729.               esdo = elf_section_data (o->output_section);
  9730.               rela_normal = FALSE;
  9731.  
  9732.               /* Adjust the reloc addresses and symbol indices.  */
  9733.  
  9734.               irela = internal_relocs;
  9735.               irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
  9736.               rel_hash = esdo->rel.hashes + esdo->rel.count;
  9737.               /* We start processing the REL relocs, if any.  When we reach
  9738.                  IRELAMID in the loop, we switch to the RELA relocs.  */
  9739.               irelamid = irela;
  9740.               if (esdi->rel.hdr != NULL)
  9741.                 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
  9742.                              * bed->s->int_rels_per_ext_rel);
  9743.               rel_hash_list = rel_hash;
  9744.               rela_hash_list = NULL;
  9745.               last_offset = o->output_offset;
  9746.               if (!flinfo->info->relocatable)
  9747.                 last_offset += o->output_section->vma;
  9748.               for (next_erel = 0; irela < irelaend; irela++, next_erel++)
  9749.                 {
  9750.                   unsigned long r_symndx;
  9751.                   asection *sec;
  9752.                   Elf_Internal_Sym sym;
  9753.  
  9754.                   if (next_erel == bed->s->int_rels_per_ext_rel)
  9755.                     {
  9756.                       rel_hash++;
  9757.                       next_erel = 0;
  9758.                     }
  9759.  
  9760.                   if (irela == irelamid)
  9761.                     {
  9762.                       rel_hash = esdo->rela.hashes + esdo->rela.count;
  9763.                       rela_hash_list = rel_hash;
  9764.                       rela_normal = bed->rela_normal;
  9765.                     }
  9766.  
  9767.                   irela->r_offset = _bfd_elf_section_offset (output_bfd,
  9768.                                                              flinfo->info, o,
  9769.                                                              irela->r_offset);
  9770.                   if (irela->r_offset >= (bfd_vma) -2)
  9771.                     {
  9772.                       /* This is a reloc for a deleted entry or somesuch.
  9773.                          Turn it into an R_*_NONE reloc, at the same
  9774.                          offset as the last reloc.  elf_eh_frame.c and
  9775.                          bfd_elf_discard_info rely on reloc offsets
  9776.                          being ordered.  */
  9777.                       irela->r_offset = last_offset;
  9778.                       irela->r_info = 0;
  9779.                       irela->r_addend = 0;
  9780.                       continue;
  9781.                     }
  9782.  
  9783.                   irela->r_offset += o->output_offset;
  9784.  
  9785.                   /* Relocs in an executable have to be virtual addresses.  */
  9786.                   if (!flinfo->info->relocatable)
  9787.                     irela->r_offset += o->output_section->vma;
  9788.  
  9789.                   last_offset = irela->r_offset;
  9790.  
  9791.                   r_symndx = irela->r_info >> r_sym_shift;
  9792.                   if (r_symndx == STN_UNDEF)
  9793.                     continue;
  9794.  
  9795.                   if (r_symndx >= locsymcount
  9796.                       || (elf_bad_symtab (input_bfd)
  9797.                           && flinfo->sections[r_symndx] == NULL))
  9798.                     {
  9799.                       struct elf_link_hash_entry *rh;
  9800.                       unsigned long indx;
  9801.  
  9802.                       /* This is a reloc against a global symbol.  We
  9803.                          have not yet output all the local symbols, so
  9804.                          we do not know the symbol index of any global
  9805.                          symbol.  We set the rel_hash entry for this
  9806.                          reloc to point to the global hash table entry
  9807.                          for this symbol.  The symbol index is then
  9808.                          set at the end of bfd_elf_final_link.  */
  9809.                       indx = r_symndx - extsymoff;
  9810.                       rh = elf_sym_hashes (input_bfd)[indx];
  9811.                       while (rh->root.type == bfd_link_hash_indirect
  9812.                              || rh->root.type == bfd_link_hash_warning)
  9813.                         rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
  9814.  
  9815.                       /* Setting the index to -2 tells
  9816.                          elf_link_output_extsym that this symbol is
  9817.                          used by a reloc.  */
  9818.                       BFD_ASSERT (rh->indx < 0);
  9819.                       rh->indx = -2;
  9820.  
  9821.                       *rel_hash = rh;
  9822.  
  9823.                       continue;
  9824.                     }
  9825.  
  9826.                   /* This is a reloc against a local symbol.  */
  9827.  
  9828.                   *rel_hash = NULL;
  9829.                   sym = isymbuf[r_symndx];
  9830.                   sec = flinfo->sections[r_symndx];
  9831.                   if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
  9832.                     {
  9833.                       /* I suppose the backend ought to fill in the
  9834.                          section of any STT_SECTION symbol against a
  9835.                          processor specific section.  */
  9836.                       r_symndx = STN_UNDEF;
  9837.                       if (bfd_is_abs_section (sec))
  9838.                         ;
  9839.                       else if (sec == NULL || sec->owner == NULL)
  9840.                         {
  9841.                           bfd_set_error (bfd_error_bad_value);
  9842.                           return FALSE;
  9843.                         }
  9844.                       else
  9845.                         {
  9846.                           asection *osec = sec->output_section;
  9847.  
  9848.                           /* If we have discarded a section, the output
  9849.                              section will be the absolute section.  In
  9850.                              case of discarded SEC_MERGE sections, use
  9851.                              the kept section.  relocate_section should
  9852.                              have already handled discarded linkonce
  9853.                              sections.  */
  9854.                           if (bfd_is_abs_section (osec)
  9855.                               && sec->kept_section != NULL
  9856.                               && sec->kept_section->output_section != NULL)
  9857.                             {
  9858.                               osec = sec->kept_section->output_section;
  9859.                               irela->r_addend -= osec->vma;
  9860.                             }
  9861.  
  9862.                           if (!bfd_is_abs_section (osec))
  9863.                             {
  9864.                               r_symndx = osec->target_index;
  9865.                               if (r_symndx == STN_UNDEF)
  9866.                                 {
  9867.                                   irela->r_addend += osec->vma;
  9868.                                   osec = _bfd_nearby_section (output_bfd, osec,
  9869.                                                               osec->vma);
  9870.                                   irela->r_addend -= osec->vma;
  9871.                                   r_symndx = osec->target_index;
  9872.                                 }
  9873.                             }
  9874.                         }
  9875.  
  9876.                       /* Adjust the addend according to where the
  9877.                          section winds up in the output section.  */
  9878.                       if (rela_normal)
  9879.                         irela->r_addend += sec->output_offset;
  9880.                     }
  9881.                   else
  9882.                     {
  9883.                       if (flinfo->indices[r_symndx] == -1)
  9884.                         {
  9885.                           unsigned long shlink;
  9886.                           const char *name;
  9887.                           asection *osec;
  9888.                           long indx;
  9889.  
  9890.                           if (flinfo->info->strip == strip_all)
  9891.                             {
  9892.                               /* You can't do ld -r -s.  */
  9893.                               bfd_set_error (bfd_error_invalid_operation);
  9894.                               return FALSE;
  9895.                             }
  9896.  
  9897.                           /* This symbol was skipped earlier, but
  9898.                              since it is needed by a reloc, we
  9899.                              must output it now.  */
  9900.                           shlink = symtab_hdr->sh_link;
  9901.                           name = (bfd_elf_string_from_elf_section
  9902.                                   (input_bfd, shlink, sym.st_name));
  9903.                           if (name == NULL)
  9904.                             return FALSE;
  9905.  
  9906.                           osec = sec->output_section;
  9907.                           sym.st_shndx =
  9908.                             _bfd_elf_section_from_bfd_section (output_bfd,
  9909.                                                                osec);
  9910.                           if (sym.st_shndx == SHN_BAD)
  9911.                             return FALSE;
  9912.  
  9913.                           sym.st_value += sec->output_offset;
  9914.                           if (!flinfo->info->relocatable)
  9915.                             {
  9916.                               sym.st_value += osec->vma;
  9917.                               if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
  9918.                                 {
  9919.                                   /* STT_TLS symbols are relative to PT_TLS
  9920.                                      segment base.  */
  9921.                                   BFD_ASSERT (elf_hash_table (flinfo->info)
  9922.                                               ->tls_sec != NULL);
  9923.                                   sym.st_value -= (elf_hash_table (flinfo->info)
  9924.                                                    ->tls_sec->vma);
  9925.                                 }
  9926.                             }
  9927.  
  9928.                           indx = bfd_get_symcount (output_bfd);
  9929.                           ret = elf_link_output_sym (flinfo, name, &sym, sec,
  9930.                                                      NULL);
  9931.                           if (ret == 0)
  9932.                             return FALSE;
  9933.                           else if (ret == 1)
  9934.                             flinfo->indices[r_symndx] = indx;
  9935.                           else
  9936.                             abort ();
  9937.                         }
  9938.  
  9939.                       r_symndx = flinfo->indices[r_symndx];
  9940.                     }
  9941.  
  9942.                   irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
  9943.                                    | (irela->r_info & r_type_mask));
  9944.                 }
  9945.  
  9946.               /* Swap out the relocs.  */
  9947.               input_rel_hdr = esdi->rel.hdr;
  9948.               if (input_rel_hdr && input_rel_hdr->sh_size != 0)
  9949.                 {
  9950.                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
  9951.                                                      input_rel_hdr,
  9952.                                                      internal_relocs,
  9953.                                                      rel_hash_list))
  9954.                     return FALSE;
  9955.                   internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
  9956.                                       * bed->s->int_rels_per_ext_rel);
  9957.                   rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
  9958.                 }
  9959.  
  9960.               input_rela_hdr = esdi->rela.hdr;
  9961.               if (input_rela_hdr && input_rela_hdr->sh_size != 0)
  9962.                 {
  9963.                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
  9964.                                                      input_rela_hdr,
  9965.                                                      internal_relocs,
  9966.                                                      rela_hash_list))
  9967.                     return FALSE;
  9968.                 }
  9969.             }
  9970.         }
  9971.  
  9972.       /* Write out the modified section contents.  */
  9973.       if (bed->elf_backend_write_section
  9974.           && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
  9975.                                                 contents))
  9976.         {
  9977.           /* Section written out.  */
  9978.         }
  9979.       else switch (o->sec_info_type)
  9980.         {
  9981.         case SEC_INFO_TYPE_STABS:
  9982.           if (! (_bfd_write_section_stabs
  9983.                  (output_bfd,
  9984.                   &elf_hash_table (flinfo->info)->stab_info,
  9985.                   o, &elf_section_data (o)->sec_info, contents)))
  9986.             return FALSE;
  9987.           break;
  9988.         case SEC_INFO_TYPE_MERGE:
  9989.           if (! _bfd_write_merged_section (output_bfd, o,
  9990.                                            elf_section_data (o)->sec_info))
  9991.             return FALSE;
  9992.           break;
  9993.         case SEC_INFO_TYPE_EH_FRAME:
  9994.           {
  9995.             if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
  9996.                                                    o, contents))
  9997.               return FALSE;
  9998.           }
  9999.           break;
  10000.         default:
  10001.           {
  10002.             /* FIXME: octets_per_byte.  */
  10003.             if (! (o->flags & SEC_EXCLUDE))
  10004.               {
  10005.                 file_ptr offset = (file_ptr) o->output_offset;
  10006.                 bfd_size_type todo = o->size;
  10007.                 if ((o->flags & SEC_ELF_REVERSE_COPY))
  10008.                   {
  10009.                     /* Reverse-copy input section to output.  */
  10010.                     do
  10011.                       {
  10012.                         todo -= address_size;
  10013.                         if (! bfd_set_section_contents (output_bfd,
  10014.                                                         o->output_section,
  10015.                                                         contents + todo,
  10016.                                                         offset,
  10017.                                                         address_size))
  10018.                           return FALSE;
  10019.                         if (todo == 0)
  10020.                           break;
  10021.                         offset += address_size;
  10022.                       }
  10023.                     while (1);
  10024.                   }
  10025.                 else if (! bfd_set_section_contents (output_bfd,
  10026.                                                      o->output_section,
  10027.                                                      contents,
  10028.                                                      offset, todo))
  10029.                   return FALSE;
  10030.               }
  10031.           }
  10032.           break;
  10033.         }
  10034.     }
  10035.  
  10036.   return TRUE;
  10037. }
  10038.  
  10039. /* Generate a reloc when linking an ELF file.  This is a reloc
  10040.    requested by the linker, and does not come from any input file.  This
  10041.    is used to build constructor and destructor tables when linking
  10042.    with -Ur.  */
  10043.  
  10044. static bfd_boolean
  10045. elf_reloc_link_order (bfd *output_bfd,
  10046.                       struct bfd_link_info *info,
  10047.                       asection *output_section,
  10048.                       struct bfd_link_order *link_order)
  10049. {
  10050.   reloc_howto_type *howto;
  10051.   long indx;
  10052.   bfd_vma offset;
  10053.   bfd_vma addend;
  10054.   struct bfd_elf_section_reloc_data *reldata;
  10055.   struct elf_link_hash_entry **rel_hash_ptr;
  10056.   Elf_Internal_Shdr *rel_hdr;
  10057.   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  10058.   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
  10059.   bfd_byte *erel;
  10060.   unsigned int i;
  10061.   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
  10062.  
  10063.   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  10064.   if (howto == NULL)
  10065.     {
  10066.       bfd_set_error (bfd_error_bad_value);
  10067.       return FALSE;
  10068.     }
  10069.  
  10070.   addend = link_order->u.reloc.p->addend;
  10071.  
  10072.   if (esdo->rel.hdr)
  10073.     reldata = &esdo->rel;
  10074.   else if (esdo->rela.hdr)
  10075.     reldata = &esdo->rela;
  10076.   else
  10077.     {
  10078.       reldata = NULL;
  10079.       BFD_ASSERT (0);
  10080.     }
  10081.  
  10082.   /* Figure out the symbol index.  */
  10083.   rel_hash_ptr = reldata->hashes + reldata->count;
  10084.   if (link_order->type == bfd_section_reloc_link_order)
  10085.     {
  10086.       indx = link_order->u.reloc.p->u.section->target_index;
  10087.       BFD_ASSERT (indx != 0);
  10088.       *rel_hash_ptr = NULL;
  10089.     }
  10090.   else
  10091.     {
  10092.       struct elf_link_hash_entry *h;
  10093.  
  10094.       /* Treat a reloc against a defined symbol as though it were
  10095.          actually against the section.  */
  10096.       h = ((struct elf_link_hash_entry *)
  10097.            bfd_wrapped_link_hash_lookup (output_bfd, info,
  10098.                                          link_order->u.reloc.p->u.name,
  10099.                                          FALSE, FALSE, TRUE));
  10100.       if (h != NULL
  10101.           && (h->root.type == bfd_link_hash_defined
  10102.               || h->root.type == bfd_link_hash_defweak))
  10103.         {
  10104.           asection *section;
  10105.  
  10106.           section = h->root.u.def.section;
  10107.           indx = section->output_section->target_index;
  10108.           *rel_hash_ptr = NULL;
  10109.           /* It seems that we ought to add the symbol value to the
  10110.              addend here, but in practice it has already been added
  10111.              because it was passed to constructor_callback.  */
  10112.           addend += section->output_section->vma + section->output_offset;
  10113.         }
  10114.       else if (h != NULL)
  10115.         {
  10116.           /* Setting the index to -2 tells elf_link_output_extsym that
  10117.              this symbol is used by a reloc.  */
  10118.           h->indx = -2;
  10119.           *rel_hash_ptr = h;
  10120.           indx = 0;
  10121.         }
  10122.       else
  10123.         {
  10124.           if (! ((*info->callbacks->unattached_reloc)
  10125.                  (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
  10126.             return FALSE;
  10127.           indx = 0;
  10128.         }
  10129.     }
  10130.  
  10131.   /* If this is an inplace reloc, we must write the addend into the
  10132.      object file.  */
  10133.   if (howto->partial_inplace && addend != 0)
  10134.     {
  10135.       bfd_size_type size;
  10136.       bfd_reloc_status_type rstat;
  10137.       bfd_byte *buf;
  10138.       bfd_boolean ok;
  10139.       const char *sym_name;
  10140.  
  10141.       size = (bfd_size_type) bfd_get_reloc_size (howto);
  10142.       buf = (bfd_byte *) bfd_zmalloc (size);
  10143.       if (buf == NULL)
  10144.         return FALSE;
  10145.       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
  10146.       switch (rstat)
  10147.         {
  10148.         case bfd_reloc_ok:
  10149.           break;
  10150.  
  10151.         default:
  10152.         case bfd_reloc_outofrange:
  10153.           abort ();
  10154.  
  10155.         case bfd_reloc_overflow:
  10156.           if (link_order->type == bfd_section_reloc_link_order)
  10157.             sym_name = bfd_section_name (output_bfd,
  10158.                                          link_order->u.reloc.p->u.section);
  10159.           else
  10160.             sym_name = link_order->u.reloc.p->u.name;
  10161.           if (! ((*info->callbacks->reloc_overflow)
  10162.                  (info, NULL, sym_name, howto->name, addend, NULL,
  10163.                   NULL, (bfd_vma) 0)))
  10164.             {
  10165.               free (buf);
  10166.               return FALSE;
  10167.             }
  10168.           break;
  10169.         }
  10170.       ok = bfd_set_section_contents (output_bfd, output_section, buf,
  10171.                                      link_order->offset, size);
  10172.       free (buf);
  10173.       if (! ok)
  10174.         return FALSE;
  10175.     }
  10176.  
  10177.   /* The address of a reloc is relative to the section in a
  10178.      relocatable file, and is a virtual address in an executable
  10179.      file.  */
  10180.   offset = link_order->offset;
  10181.   if (! info->relocatable)
  10182.     offset += output_section->vma;
  10183.  
  10184.   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
  10185.     {
  10186.       irel[i].r_offset = offset;
  10187.       irel[i].r_info = 0;
  10188.       irel[i].r_addend = 0;
  10189.     }
  10190.   if (bed->s->arch_size == 32)
  10191.     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
  10192.   else
  10193.     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
  10194.  
  10195.   rel_hdr = reldata->hdr;
  10196.   erel = rel_hdr->contents;
  10197.   if (rel_hdr->sh_type == SHT_REL)
  10198.     {
  10199.       erel += reldata->count * bed->s->sizeof_rel;
  10200.       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
  10201.     }
  10202.   else
  10203.     {
  10204.       irel[0].r_addend = addend;
  10205.       erel += reldata->count * bed->s->sizeof_rela;
  10206.       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
  10207.     }
  10208.  
  10209.   ++reldata->count;
  10210.  
  10211.   return TRUE;
  10212. }
  10213.  
  10214.  
  10215. /* Get the output vma of the section pointed to by the sh_link field.  */
  10216.  
  10217. static bfd_vma
  10218. elf_get_linked_section_vma (struct bfd_link_order *p)
  10219. {
  10220.   Elf_Internal_Shdr **elf_shdrp;
  10221.   asection *s;
  10222.   int elfsec;
  10223.  
  10224.   s = p->u.indirect.section;
  10225.   elf_shdrp = elf_elfsections (s->owner);
  10226.   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
  10227.   elfsec = elf_shdrp[elfsec]->sh_link;
  10228.   /* PR 290:
  10229.      The Intel C compiler generates SHT_IA_64_UNWIND with
  10230.      SHF_LINK_ORDER.  But it doesn't set the sh_link or
  10231.      sh_info fields.  Hence we could get the situation
  10232.      where elfsec is 0.  */
  10233.   if (elfsec == 0)
  10234.     {
  10235.       const struct elf_backend_data *bed
  10236.         = get_elf_backend_data (s->owner);
  10237.       if (bed->link_order_error_handler)
  10238.         bed->link_order_error_handler
  10239.           (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
  10240.       return 0;
  10241.     }
  10242.   else
  10243.     {
  10244.       s = elf_shdrp[elfsec]->bfd_section;
  10245.       return s->output_section->vma + s->output_offset;
  10246.     }
  10247. }
  10248.  
  10249.  
  10250. /* Compare two sections based on the locations of the sections they are
  10251.    linked to.  Used by elf_fixup_link_order.  */
  10252.  
  10253. static int
  10254. compare_link_order (const void * a, const void * b)
  10255. {
  10256.   bfd_vma apos;
  10257.   bfd_vma bpos;
  10258.  
  10259.   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
  10260.   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
  10261.   if (apos < bpos)
  10262.     return -1;
  10263.   return apos > bpos;
  10264. }
  10265.  
  10266.  
  10267. /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
  10268.    order as their linked sections.  Returns false if this could not be done
  10269.    because an output section includes both ordered and unordered
  10270.    sections.  Ideally we'd do this in the linker proper.  */
  10271.  
  10272. static bfd_boolean
  10273. elf_fixup_link_order (bfd *abfd, asection *o)
  10274. {
  10275.   int seen_linkorder;
  10276.   int seen_other;
  10277.   int n;
  10278.   struct bfd_link_order *p;
  10279.   bfd *sub;
  10280.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10281.   unsigned elfsec;
  10282.   struct bfd_link_order **sections;
  10283.   asection *s, *other_sec, *linkorder_sec;
  10284.   bfd_vma offset;
  10285.  
  10286.   other_sec = NULL;
  10287.   linkorder_sec = NULL;
  10288.   seen_other = 0;
  10289.   seen_linkorder = 0;
  10290.   for (p = o->map_head.link_order; p != NULL; p = p->next)
  10291.     {
  10292.       if (p->type == bfd_indirect_link_order)
  10293.         {
  10294.           s = p->u.indirect.section;
  10295.           sub = s->owner;
  10296.           if (bfd_get_flavour (sub) == bfd_target_elf_flavour
  10297.               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
  10298.               && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
  10299.               && elfsec < elf_numsections (sub)
  10300.               && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
  10301.               && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
  10302.             {
  10303.               seen_linkorder++;
  10304.               linkorder_sec = s;
  10305.             }
  10306.           else
  10307.             {
  10308.               seen_other++;
  10309.               other_sec = s;
  10310.             }
  10311.         }
  10312.       else
  10313.         seen_other++;
  10314.  
  10315.       if (seen_other && seen_linkorder)
  10316.         {
  10317.           if (other_sec && linkorder_sec)
  10318.             (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
  10319.                                    o, linkorder_sec,
  10320.                                    linkorder_sec->owner, other_sec,
  10321.                                    other_sec->owner);
  10322.           else
  10323.             (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
  10324.                                    o);
  10325.           bfd_set_error (bfd_error_bad_value);
  10326.           return FALSE;
  10327.         }
  10328.     }
  10329.  
  10330.   if (!seen_linkorder)
  10331.     return TRUE;
  10332.  
  10333.   sections = (struct bfd_link_order **)
  10334.     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
  10335.   if (sections == NULL)
  10336.     return FALSE;
  10337.   seen_linkorder = 0;
  10338.  
  10339.   for (p = o->map_head.link_order; p != NULL; p = p->next)
  10340.     {
  10341.       sections[seen_linkorder++] = p;
  10342.     }
  10343.   /* Sort the input sections in the order of their linked section.  */
  10344.   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
  10345.          compare_link_order);
  10346.  
  10347.   /* Change the offsets of the sections.  */
  10348.   offset = 0;
  10349.   for (n = 0; n < seen_linkorder; n++)
  10350.     {
  10351.       s = sections[n]->u.indirect.section;
  10352.       offset &= ~(bfd_vma) 0 << s->alignment_power;
  10353.       s->output_offset = offset;
  10354.       sections[n]->offset = offset;
  10355.       /* FIXME: octets_per_byte.  */
  10356.       offset += sections[n]->size;
  10357.     }
  10358.  
  10359.   free (sections);
  10360.   return TRUE;
  10361. }
  10362.  
  10363. static void
  10364. elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
  10365. {
  10366.   asection *o;
  10367.  
  10368.   if (flinfo->symstrtab != NULL)
  10369.     _bfd_stringtab_free (flinfo->symstrtab);
  10370.   if (flinfo->contents != NULL)
  10371.     free (flinfo->contents);
  10372.   if (flinfo->external_relocs != NULL)
  10373.     free (flinfo->external_relocs);
  10374.   if (flinfo->internal_relocs != NULL)
  10375.     free (flinfo->internal_relocs);
  10376.   if (flinfo->external_syms != NULL)
  10377.     free (flinfo->external_syms);
  10378.   if (flinfo->locsym_shndx != NULL)
  10379.     free (flinfo->locsym_shndx);
  10380.   if (flinfo->internal_syms != NULL)
  10381.     free (flinfo->internal_syms);
  10382.   if (flinfo->indices != NULL)
  10383.     free (flinfo->indices);
  10384.   if (flinfo->sections != NULL)
  10385.     free (flinfo->sections);
  10386.   if (flinfo->symbuf != NULL)
  10387.     free (flinfo->symbuf);
  10388.   if (flinfo->symshndxbuf != NULL)
  10389.     free (flinfo->symshndxbuf);
  10390.   for (o = obfd->sections; o != NULL; o = o->next)
  10391.     {
  10392.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  10393.       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
  10394.         free (esdo->rel.hashes);
  10395.       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
  10396.         free (esdo->rela.hashes);
  10397.     }
  10398. }
  10399.  
  10400. /* Do the final step of an ELF link.  */
  10401.  
  10402. bfd_boolean
  10403. bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
  10404. {
  10405.   bfd_boolean dynamic;
  10406.   bfd_boolean emit_relocs;
  10407.   bfd *dynobj;
  10408.   struct elf_final_link_info flinfo;
  10409.   asection *o;
  10410.   struct bfd_link_order *p;
  10411.   bfd *sub;
  10412.   bfd_size_type max_contents_size;
  10413.   bfd_size_type max_external_reloc_size;
  10414.   bfd_size_type max_internal_reloc_count;
  10415.   bfd_size_type max_sym_count;
  10416.   bfd_size_type max_sym_shndx_count;
  10417.   file_ptr off;
  10418.   Elf_Internal_Sym elfsym;
  10419.   unsigned int i;
  10420.   Elf_Internal_Shdr *symtab_hdr;
  10421.   Elf_Internal_Shdr *symtab_shndx_hdr;
  10422.   Elf_Internal_Shdr *symstrtab_hdr;
  10423.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10424.   struct elf_outext_info eoinfo;
  10425.   bfd_boolean merged;
  10426.   size_t relativecount = 0;
  10427.   asection *reldyn = 0;
  10428.   bfd_size_type amt;
  10429.   asection *attr_section = NULL;
  10430.   bfd_vma attr_size = 0;
  10431.   const char *std_attrs_section;
  10432.  
  10433.   if (! is_elf_hash_table (info->hash))
  10434.     return FALSE;
  10435.  
  10436.   if (info->shared)
  10437.     abfd->flags |= DYNAMIC;
  10438.  
  10439.   dynamic = elf_hash_table (info)->dynamic_sections_created;
  10440.   dynobj = elf_hash_table (info)->dynobj;
  10441.  
  10442.   emit_relocs = (info->relocatable
  10443.                  || info->emitrelocations);
  10444.  
  10445.   flinfo.info = info;
  10446.   flinfo.output_bfd = abfd;
  10447.   flinfo.symstrtab = _bfd_elf_stringtab_init ();
  10448.   if (flinfo.symstrtab == NULL)
  10449.     return FALSE;
  10450.  
  10451.   if (! dynamic)
  10452.     {
  10453.       flinfo.dynsym_sec = NULL;
  10454.       flinfo.hash_sec = NULL;
  10455.       flinfo.symver_sec = NULL;
  10456.     }
  10457.   else
  10458.     {
  10459.       flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
  10460.       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
  10461.       /* Note that dynsym_sec can be NULL (on VMS).  */
  10462.       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
  10463.       /* Note that it is OK if symver_sec is NULL.  */
  10464.     }
  10465.  
  10466.   flinfo.contents = NULL;
  10467.   flinfo.external_relocs = NULL;
  10468.   flinfo.internal_relocs = NULL;
  10469.   flinfo.external_syms = NULL;
  10470.   flinfo.locsym_shndx = NULL;
  10471.   flinfo.internal_syms = NULL;
  10472.   flinfo.indices = NULL;
  10473.   flinfo.sections = NULL;
  10474.   flinfo.symbuf = NULL;
  10475.   flinfo.symshndxbuf = NULL;
  10476.   flinfo.symbuf_count = 0;
  10477.   flinfo.shndxbuf_size = 0;
  10478.   flinfo.filesym_count = 0;
  10479.  
  10480.   /* The object attributes have been merged.  Remove the input
  10481.      sections from the link, and set the contents of the output
  10482.      secton.  */
  10483.   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
  10484.   for (o = abfd->sections; o != NULL; o = o->next)
  10485.     {
  10486.       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
  10487.           || strcmp (o->name, ".gnu.attributes") == 0)
  10488.         {
  10489.           for (p = o->map_head.link_order; p != NULL; p = p->next)
  10490.             {
  10491.               asection *input_section;
  10492.  
  10493.               if (p->type != bfd_indirect_link_order)
  10494.                 continue;
  10495.               input_section = p->u.indirect.section;
  10496.               /* Hack: reset the SEC_HAS_CONTENTS flag so that
  10497.                  elf_link_input_bfd ignores this section.  */
  10498.               input_section->flags &= ~SEC_HAS_CONTENTS;
  10499.             }
  10500.  
  10501.           attr_size = bfd_elf_obj_attr_size (abfd);
  10502.           if (attr_size)
  10503.             {
  10504.               bfd_set_section_size (abfd, o, attr_size);
  10505.               attr_section = o;
  10506.               /* Skip this section later on.  */
  10507.               o->map_head.link_order = NULL;
  10508.             }
  10509.           else
  10510.             o->flags |= SEC_EXCLUDE;
  10511.         }
  10512.     }
  10513.  
  10514.   /* Count up the number of relocations we will output for each output
  10515.      section, so that we know the sizes of the reloc sections.  We
  10516.      also figure out some maximum sizes.  */
  10517.   max_contents_size = 0;
  10518.   max_external_reloc_size = 0;
  10519.   max_internal_reloc_count = 0;
  10520.   max_sym_count = 0;
  10521.   max_sym_shndx_count = 0;
  10522.   merged = FALSE;
  10523.   for (o = abfd->sections; o != NULL; o = o->next)
  10524.     {
  10525.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  10526.       o->reloc_count = 0;
  10527.  
  10528.       for (p = o->map_head.link_order; p != NULL; p = p->next)
  10529.         {
  10530.           unsigned int reloc_count = 0;
  10531.           struct bfd_elf_section_data *esdi = NULL;
  10532.  
  10533.           if (p->type == bfd_section_reloc_link_order
  10534.               || p->type == bfd_symbol_reloc_link_order)
  10535.             reloc_count = 1;
  10536.           else if (p->type == bfd_indirect_link_order)
  10537.             {
  10538.               asection *sec;
  10539.  
  10540.               sec = p->u.indirect.section;
  10541.               esdi = elf_section_data (sec);
  10542.  
  10543.               /* Mark all sections which are to be included in the
  10544.                  link.  This will normally be every section.  We need
  10545.                  to do this so that we can identify any sections which
  10546.                  the linker has decided to not include.  */
  10547.               sec->linker_mark = TRUE;
  10548.  
  10549.               if (sec->flags & SEC_MERGE)
  10550.                 merged = TRUE;
  10551.  
  10552.               if (esdo->this_hdr.sh_type == SHT_REL
  10553.                   || esdo->this_hdr.sh_type == SHT_RELA)
  10554.                 /* Some backends use reloc_count in relocation sections
  10555.                    to count particular types of relocs.  Of course,
  10556.                    reloc sections themselves can't have relocations.  */
  10557.                 reloc_count = 0;
  10558.               else if (info->relocatable || info->emitrelocations)
  10559.                 reloc_count = sec->reloc_count;
  10560.               else if (bed->elf_backend_count_relocs)
  10561.                 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
  10562.  
  10563.               if (sec->rawsize > max_contents_size)
  10564.                 max_contents_size = sec->rawsize;
  10565.               if (sec->size > max_contents_size)
  10566.                 max_contents_size = sec->size;
  10567.  
  10568.               /* We are interested in just local symbols, not all
  10569.                  symbols.  */
  10570.               if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
  10571.                   && (sec->owner->flags & DYNAMIC) == 0)
  10572.                 {
  10573.                   size_t sym_count;
  10574.  
  10575.                   if (elf_bad_symtab (sec->owner))
  10576.                     sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
  10577.                                  / bed->s->sizeof_sym);
  10578.                   else
  10579.                     sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
  10580.  
  10581.                   if (sym_count > max_sym_count)
  10582.                     max_sym_count = sym_count;
  10583.  
  10584.                   if (sym_count > max_sym_shndx_count
  10585.                       && elf_symtab_shndx (sec->owner) != 0)
  10586.                     max_sym_shndx_count = sym_count;
  10587.  
  10588.                   if ((sec->flags & SEC_RELOC) != 0)
  10589.                     {
  10590.                       size_t ext_size = 0;
  10591.  
  10592.                       if (esdi->rel.hdr != NULL)
  10593.                         ext_size = esdi->rel.hdr->sh_size;
  10594.                       if (esdi->rela.hdr != NULL)
  10595.                         ext_size += esdi->rela.hdr->sh_size;
  10596.  
  10597.                       if (ext_size > max_external_reloc_size)
  10598.                         max_external_reloc_size = ext_size;
  10599.                       if (sec->reloc_count > max_internal_reloc_count)
  10600.                         max_internal_reloc_count = sec->reloc_count;
  10601.                     }
  10602.                 }
  10603.             }
  10604.  
  10605.           if (reloc_count == 0)
  10606.             continue;
  10607.  
  10608.           o->reloc_count += reloc_count;
  10609.  
  10610.           if (p->type == bfd_indirect_link_order
  10611.               && (info->relocatable || info->emitrelocations))
  10612.             {
  10613.               if (esdi->rel.hdr)
  10614.                 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
  10615.               if (esdi->rela.hdr)
  10616.                 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
  10617.             }
  10618.           else
  10619.             {
  10620.               if (o->use_rela_p)
  10621.                 esdo->rela.count += reloc_count;
  10622.               else
  10623.                 esdo->rel.count += reloc_count;
  10624.             }
  10625.         }
  10626.  
  10627.       if (o->reloc_count > 0)
  10628.         o->flags |= SEC_RELOC;
  10629.       else
  10630.         {
  10631.           /* Explicitly clear the SEC_RELOC flag.  The linker tends to
  10632.              set it (this is probably a bug) and if it is set
  10633.              assign_section_numbers will create a reloc section.  */
  10634.           o->flags &=~ SEC_RELOC;
  10635.         }
  10636.  
  10637.       /* If the SEC_ALLOC flag is not set, force the section VMA to
  10638.          zero.  This is done in elf_fake_sections as well, but forcing
  10639.          the VMA to 0 here will ensure that relocs against these
  10640.          sections are handled correctly.  */
  10641.       if ((o->flags & SEC_ALLOC) == 0
  10642.           && ! o->user_set_vma)
  10643.         o->vma = 0;
  10644.     }
  10645.  
  10646.   if (! info->relocatable && merged)
  10647.     elf_link_hash_traverse (elf_hash_table (info),
  10648.                             _bfd_elf_link_sec_merge_syms, abfd);
  10649.  
  10650.   /* Figure out the file positions for everything but the symbol table
  10651.      and the relocs.  We set symcount to force assign_section_numbers
  10652.      to create a symbol table.  */
  10653.   bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
  10654.   BFD_ASSERT (! abfd->output_has_begun);
  10655.   if (! _bfd_elf_compute_section_file_positions (abfd, info))
  10656.     goto error_return;
  10657.  
  10658.   /* Set sizes, and assign file positions for reloc sections.  */
  10659.   for (o = abfd->sections; o != NULL; o = o->next)
  10660.     {
  10661.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  10662.       if ((o->flags & SEC_RELOC) != 0)
  10663.         {
  10664.           if (esdo->rel.hdr
  10665.               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
  10666.             goto error_return;
  10667.  
  10668.           if (esdo->rela.hdr
  10669.               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
  10670.             goto error_return;
  10671.         }
  10672.  
  10673.       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
  10674.          to count upwards while actually outputting the relocations.  */
  10675.       esdo->rel.count = 0;
  10676.       esdo->rela.count = 0;
  10677.     }
  10678.  
  10679.   _bfd_elf_assign_file_positions_for_relocs (abfd);
  10680.  
  10681.   /* We have now assigned file positions for all the sections except
  10682.      .symtab and .strtab.  We start the .symtab section at the current
  10683.      file position, and write directly to it.  We build the .strtab
  10684.      section in memory.  */
  10685.   bfd_get_symcount (abfd) = 0;
  10686.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  10687.   /* sh_name is set in prep_headers.  */
  10688.   symtab_hdr->sh_type = SHT_SYMTAB;
  10689.   /* sh_flags, sh_addr and sh_size all start off zero.  */
  10690.   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
  10691.   /* sh_link is set in assign_section_numbers.  */
  10692.   /* sh_info is set below.  */
  10693.   /* sh_offset is set just below.  */
  10694.   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
  10695.  
  10696.   off = elf_next_file_pos (abfd);
  10697.   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
  10698.  
  10699.   /* Note that at this point elf_next_file_pos (abfd) is
  10700.      incorrect.  We do not yet know the size of the .symtab section.
  10701.      We correct next_file_pos below, after we do know the size.  */
  10702.  
  10703.   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
  10704.      continuously seeking to the right position in the file.  */
  10705.   if (! info->keep_memory || max_sym_count < 20)
  10706.     flinfo.symbuf_size = 20;
  10707.   else
  10708.     flinfo.symbuf_size = max_sym_count;
  10709.   amt = flinfo.symbuf_size;
  10710.   amt *= bed->s->sizeof_sym;
  10711.   flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
  10712.   if (flinfo.symbuf == NULL)
  10713.     goto error_return;
  10714.   if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
  10715.     {
  10716.       /* Wild guess at number of output symbols.  realloc'd as needed.  */
  10717.       amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
  10718.       flinfo.shndxbuf_size = amt;
  10719.       amt *= sizeof (Elf_External_Sym_Shndx);
  10720.       flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
  10721.       if (flinfo.symshndxbuf == NULL)
  10722.         goto error_return;
  10723.     }
  10724.  
  10725.   /* Start writing out the symbol table.  The first symbol is always a
  10726.      dummy symbol.  */
  10727.   if (info->strip != strip_all
  10728.       || emit_relocs)
  10729.     {
  10730.       elfsym.st_value = 0;
  10731.       elfsym.st_size = 0;
  10732.       elfsym.st_info = 0;
  10733.       elfsym.st_other = 0;
  10734.       elfsym.st_shndx = SHN_UNDEF;
  10735.       elfsym.st_target_internal = 0;
  10736.       if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
  10737.                                NULL) != 1)
  10738.         goto error_return;
  10739.     }
  10740.  
  10741.   /* Output a symbol for each section.  We output these even if we are
  10742.      discarding local symbols, since they are used for relocs.  These
  10743.      symbols have no names.  We store the index of each one in the
  10744.      index field of the section, so that we can find it again when
  10745.      outputting relocs.  */
  10746.   if (info->strip != strip_all
  10747.       || emit_relocs)
  10748.     {
  10749.       elfsym.st_size = 0;
  10750.       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  10751.       elfsym.st_other = 0;
  10752.       elfsym.st_value = 0;
  10753.       elfsym.st_target_internal = 0;
  10754.       for (i = 1; i < elf_numsections (abfd); i++)
  10755.         {
  10756.           o = bfd_section_from_elf_index (abfd, i);
  10757.           if (o != NULL)
  10758.             {
  10759.               o->target_index = bfd_get_symcount (abfd);
  10760.               elfsym.st_shndx = i;
  10761.               if (!info->relocatable)
  10762.                 elfsym.st_value = o->vma;
  10763.               if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
  10764.                 goto error_return;
  10765.             }
  10766.         }
  10767.     }
  10768.  
  10769.   /* Allocate some memory to hold information read in from the input
  10770.      files.  */
  10771.   if (max_contents_size != 0)
  10772.     {
  10773.       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  10774.       if (flinfo.contents == NULL)
  10775.         goto error_return;
  10776.     }
  10777.  
  10778.   if (max_external_reloc_size != 0)
  10779.     {
  10780.       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
  10781.       if (flinfo.external_relocs == NULL)
  10782.         goto error_return;
  10783.     }
  10784.  
  10785.   if (max_internal_reloc_count != 0)
  10786.     {
  10787.       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
  10788.       amt *= sizeof (Elf_Internal_Rela);
  10789.       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
  10790.       if (flinfo.internal_relocs == NULL)
  10791.         goto error_return;
  10792.     }
  10793.  
  10794.   if (max_sym_count != 0)
  10795.     {
  10796.       amt = max_sym_count * bed->s->sizeof_sym;
  10797.       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
  10798.       if (flinfo.external_syms == NULL)
  10799.         goto error_return;
  10800.  
  10801.       amt = max_sym_count * sizeof (Elf_Internal_Sym);
  10802.       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
  10803.       if (flinfo.internal_syms == NULL)
  10804.         goto error_return;
  10805.  
  10806.       amt = max_sym_count * sizeof (long);
  10807.       flinfo.indices = (long int *) bfd_malloc (amt);
  10808.       if (flinfo.indices == NULL)
  10809.         goto error_return;
  10810.  
  10811.       amt = max_sym_count * sizeof (asection *);
  10812.       flinfo.sections = (asection **) bfd_malloc (amt);
  10813.       if (flinfo.sections == NULL)
  10814.         goto error_return;
  10815.     }
  10816.  
  10817.   if (max_sym_shndx_count != 0)
  10818.     {
  10819.       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
  10820.       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
  10821.       if (flinfo.locsym_shndx == NULL)
  10822.         goto error_return;
  10823.     }
  10824.  
  10825.   if (elf_hash_table (info)->tls_sec)
  10826.     {
  10827.       bfd_vma base, end = 0;
  10828.       asection *sec;
  10829.  
  10830.       for (sec = elf_hash_table (info)->tls_sec;
  10831.            sec && (sec->flags & SEC_THREAD_LOCAL);
  10832.            sec = sec->next)
  10833.         {
  10834.           bfd_size_type size = sec->size;
  10835.  
  10836.           if (size == 0
  10837.               && (sec->flags & SEC_HAS_CONTENTS) == 0)
  10838.             {
  10839.               struct bfd_link_order *ord = sec->map_tail.link_order;
  10840.  
  10841.               if (ord != NULL)
  10842.                 size = ord->offset + ord->size;
  10843.             }
  10844.           end = sec->vma + size;
  10845.         }
  10846.       base = elf_hash_table (info)->tls_sec->vma;
  10847.       /* Only align end of TLS section if static TLS doesn't have special
  10848.          alignment requirements.  */
  10849.       if (bed->static_tls_alignment == 1)
  10850.         end = align_power (end,
  10851.                            elf_hash_table (info)->tls_sec->alignment_power);
  10852.       elf_hash_table (info)->tls_size = end - base;
  10853.     }
  10854.  
  10855.   /* Reorder SHF_LINK_ORDER sections.  */
  10856.   for (o = abfd->sections; o != NULL; o = o->next)
  10857.     {
  10858.       if (!elf_fixup_link_order (abfd, o))
  10859.         return FALSE;
  10860.     }
  10861.  
  10862.   /* Since ELF permits relocations to be against local symbols, we
  10863.      must have the local symbols available when we do the relocations.
  10864.      Since we would rather only read the local symbols once, and we
  10865.      would rather not keep them in memory, we handle all the
  10866.      relocations for a single input file at the same time.
  10867.  
  10868.      Unfortunately, there is no way to know the total number of local
  10869.      symbols until we have seen all of them, and the local symbol
  10870.      indices precede the global symbol indices.  This means that when
  10871.      we are generating relocatable output, and we see a reloc against
  10872.      a global symbol, we can not know the symbol index until we have
  10873.      finished examining all the local symbols to see which ones we are
  10874.      going to output.  To deal with this, we keep the relocations in
  10875.      memory, and don't output them until the end of the link.  This is
  10876.      an unfortunate waste of memory, but I don't see a good way around
  10877.      it.  Fortunately, it only happens when performing a relocatable
  10878.      link, which is not the common case.  FIXME: If keep_memory is set
  10879.      we could write the relocs out and then read them again; I don't
  10880.      know how bad the memory loss will be.  */
  10881.  
  10882.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  10883.     sub->output_has_begun = FALSE;
  10884.   for (o = abfd->sections; o != NULL; o = o->next)
  10885.     {
  10886.       for (p = o->map_head.link_order; p != NULL; p = p->next)
  10887.         {
  10888.           if (p->type == bfd_indirect_link_order
  10889.               && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
  10890.                   == bfd_target_elf_flavour)
  10891.               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
  10892.             {
  10893.               if (! sub->output_has_begun)
  10894.                 {
  10895.                   if (! elf_link_input_bfd (&flinfo, sub))
  10896.                     goto error_return;
  10897.                   sub->output_has_begun = TRUE;
  10898.                 }
  10899.             }
  10900.           else if (p->type == bfd_section_reloc_link_order
  10901.                    || p->type == bfd_symbol_reloc_link_order)
  10902.             {
  10903.               if (! elf_reloc_link_order (abfd, info, o, p))
  10904.                 goto error_return;
  10905.             }
  10906.           else
  10907.             {
  10908.               if (! _bfd_default_link_order (abfd, info, o, p))
  10909.                 {
  10910.                   if (p->type == bfd_indirect_link_order
  10911.                       && (bfd_get_flavour (sub)
  10912.                           == bfd_target_elf_flavour)
  10913.                       && (elf_elfheader (sub)->e_ident[EI_CLASS]
  10914.                           != bed->s->elfclass))
  10915.                     {
  10916.                       const char *iclass, *oclass;
  10917.  
  10918.                       if (bed->s->elfclass == ELFCLASS64)
  10919.                         {
  10920.                           iclass = "ELFCLASS32";
  10921.                           oclass = "ELFCLASS64";
  10922.                         }
  10923.                       else
  10924.                         {
  10925.                           iclass = "ELFCLASS64";
  10926.                           oclass = "ELFCLASS32";
  10927.                         }
  10928.  
  10929.                       bfd_set_error (bfd_error_wrong_format);
  10930.                       (*_bfd_error_handler)
  10931.                         (_("%B: file class %s incompatible with %s"),
  10932.                          sub, iclass, oclass);
  10933.                     }
  10934.  
  10935.                   goto error_return;
  10936.                 }
  10937.             }
  10938.         }
  10939.     }
  10940.  
  10941.   /* Free symbol buffer if needed.  */
  10942.   if (!info->reduce_memory_overheads)
  10943.     {
  10944.       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  10945.         if (bfd_get_flavour (sub) == bfd_target_elf_flavour
  10946.             && elf_tdata (sub)->symbuf)
  10947.           {
  10948.             free (elf_tdata (sub)->symbuf);
  10949.             elf_tdata (sub)->symbuf = NULL;
  10950.           }
  10951.     }
  10952.  
  10953.   /* Output a FILE symbol so that following locals are not associated
  10954.      with the wrong input file.  */
  10955.   memset (&elfsym, 0, sizeof (elfsym));
  10956.   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  10957.   elfsym.st_shndx = SHN_ABS;
  10958.  
  10959.   if (flinfo.filesym_count > 1
  10960.       && !elf_link_output_sym (&flinfo, NULL, &elfsym,
  10961.                                bfd_und_section_ptr, NULL))
  10962.     return FALSE;
  10963.  
  10964.   /* Output any global symbols that got converted to local in a
  10965.      version script or due to symbol visibility.  We do this in a
  10966.      separate step since ELF requires all local symbols to appear
  10967.      prior to any global symbols.  FIXME: We should only do this if
  10968.      some global symbols were, in fact, converted to become local.
  10969.      FIXME: Will this work correctly with the Irix 5 linker?  */
  10970.   eoinfo.failed = FALSE;
  10971.   eoinfo.flinfo = &flinfo;
  10972.   eoinfo.localsyms = TRUE;
  10973.   eoinfo.need_second_pass = FALSE;
  10974.   eoinfo.second_pass = FALSE;
  10975.   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  10976.   if (eoinfo.failed)
  10977.     return FALSE;
  10978.  
  10979.   if (flinfo.filesym_count == 1
  10980.       && !elf_link_output_sym (&flinfo, NULL, &elfsym,
  10981.                                bfd_und_section_ptr, NULL))
  10982.     return FALSE;
  10983.  
  10984.   if (eoinfo.need_second_pass)
  10985.     {
  10986.       eoinfo.second_pass = TRUE;
  10987.       bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  10988.       if (eoinfo.failed)
  10989.         return FALSE;
  10990.     }
  10991.  
  10992.   /* If backend needs to output some local symbols not present in the hash
  10993.      table, do it now.  */
  10994.   if (bed->elf_backend_output_arch_local_syms)
  10995.     {
  10996.       typedef int (*out_sym_func)
  10997.         (void *, const char *, Elf_Internal_Sym *, asection *,
  10998.          struct elf_link_hash_entry *);
  10999.  
  11000.       if (! ((*bed->elf_backend_output_arch_local_syms)
  11001.              (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
  11002.         return FALSE;
  11003.     }
  11004.  
  11005.   /* That wrote out all the local symbols.  Finish up the symbol table
  11006.      with the global symbols. Even if we want to strip everything we
  11007.      can, we still need to deal with those global symbols that got
  11008.      converted to local in a version script.  */
  11009.  
  11010.   /* The sh_info field records the index of the first non local symbol.  */
  11011.   symtab_hdr->sh_info = bfd_get_symcount (abfd);
  11012.  
  11013.   if (dynamic
  11014.       && flinfo.dynsym_sec != NULL
  11015.       && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
  11016.     {
  11017.       Elf_Internal_Sym sym;
  11018.       bfd_byte *dynsym = flinfo.dynsym_sec->contents;
  11019.       long last_local = 0;
  11020.  
  11021.       /* Write out the section symbols for the output sections.  */
  11022.       if (info->shared || elf_hash_table (info)->is_relocatable_executable)
  11023.         {
  11024.           asection *s;
  11025.  
  11026.           sym.st_size = 0;
  11027.           sym.st_name = 0;
  11028.           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  11029.           sym.st_other = 0;
  11030.           sym.st_target_internal = 0;
  11031.  
  11032.           for (s = abfd->sections; s != NULL; s = s->next)
  11033.             {
  11034.               int indx;
  11035.               bfd_byte *dest;
  11036.               long dynindx;
  11037.  
  11038.               dynindx = elf_section_data (s)->dynindx;
  11039.               if (dynindx <= 0)
  11040.                 continue;
  11041.               indx = elf_section_data (s)->this_idx;
  11042.               BFD_ASSERT (indx > 0);
  11043.               sym.st_shndx = indx;
  11044.               if (! check_dynsym (abfd, &sym))
  11045.                 return FALSE;
  11046.               sym.st_value = s->vma;
  11047.               dest = dynsym + dynindx * bed->s->sizeof_sym;
  11048.               if (last_local < dynindx)
  11049.                 last_local = dynindx;
  11050.               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11051.             }
  11052.         }
  11053.  
  11054.       /* Write out the local dynsyms.  */
  11055.       if (elf_hash_table (info)->dynlocal)
  11056.         {
  11057.           struct elf_link_local_dynamic_entry *e;
  11058.           for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  11059.             {
  11060.               asection *s;
  11061.               bfd_byte *dest;
  11062.  
  11063.               /* Copy the internal symbol and turn off visibility.
  11064.                  Note that we saved a word of storage and overwrote
  11065.                  the original st_name with the dynstr_index.  */
  11066.               sym = e->isym;
  11067.               sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  11068.  
  11069.               s = bfd_section_from_elf_index (e->input_bfd,
  11070.                                               e->isym.st_shndx);
  11071.               if (s != NULL)
  11072.                 {
  11073.                   sym.st_shndx =
  11074.                     elf_section_data (s->output_section)->this_idx;
  11075.                   if (! check_dynsym (abfd, &sym))
  11076.                     return FALSE;
  11077.                   sym.st_value = (s->output_section->vma
  11078.                                   + s->output_offset
  11079.                                   + e->isym.st_value);
  11080.                 }
  11081.  
  11082.               if (last_local < e->dynindx)
  11083.                 last_local = e->dynindx;
  11084.  
  11085.               dest = dynsym + e->dynindx * bed->s->sizeof_sym;
  11086.               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11087.             }
  11088.         }
  11089.  
  11090.       elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
  11091.         last_local + 1;
  11092.     }
  11093.  
  11094.   /* We get the global symbols from the hash table.  */
  11095.   eoinfo.failed = FALSE;
  11096.   eoinfo.localsyms = FALSE;
  11097.   eoinfo.flinfo = &flinfo;
  11098.   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  11099.   if (eoinfo.failed)
  11100.     return FALSE;
  11101.  
  11102.   /* If backend needs to output some symbols not present in the hash
  11103.      table, do it now.  */
  11104.   if (bed->elf_backend_output_arch_syms)
  11105.     {
  11106.       typedef int (*out_sym_func)
  11107.         (void *, const char *, Elf_Internal_Sym *, asection *,
  11108.          struct elf_link_hash_entry *);
  11109.  
  11110.       if (! ((*bed->elf_backend_output_arch_syms)
  11111.              (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
  11112.         return FALSE;
  11113.     }
  11114.  
  11115.   /* Flush all symbols to the file.  */
  11116.   if (! elf_link_flush_output_syms (&flinfo, bed))
  11117.     return FALSE;
  11118.  
  11119.   /* Now we know the size of the symtab section.  */
  11120.   off += symtab_hdr->sh_size;
  11121.  
  11122.   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
  11123.   if (symtab_shndx_hdr->sh_name != 0)
  11124.     {
  11125.       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
  11126.       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
  11127.       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
  11128.       amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
  11129.       symtab_shndx_hdr->sh_size = amt;
  11130.  
  11131.       off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
  11132.                                                        off, TRUE);
  11133.  
  11134.       if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
  11135.           || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
  11136.         return FALSE;
  11137.     }
  11138.  
  11139.  
  11140.   /* Finish up and write out the symbol string table (.strtab)
  11141.      section.  */
  11142.   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  11143.   /* sh_name was set in prep_headers.  */
  11144.   symstrtab_hdr->sh_type = SHT_STRTAB;
  11145.   symstrtab_hdr->sh_flags = 0;
  11146.   symstrtab_hdr->sh_addr = 0;
  11147.   symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
  11148.   symstrtab_hdr->sh_entsize = 0;
  11149.   symstrtab_hdr->sh_link = 0;
  11150.   symstrtab_hdr->sh_info = 0;
  11151.   /* sh_offset is set just below.  */
  11152.   symstrtab_hdr->sh_addralign = 1;
  11153.  
  11154.   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
  11155.   elf_next_file_pos (abfd) = off;
  11156.  
  11157.   if (bfd_get_symcount (abfd) > 0)
  11158.     {
  11159.       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
  11160.           || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
  11161.         return FALSE;
  11162.     }
  11163.  
  11164.   /* Adjust the relocs to have the correct symbol indices.  */
  11165.   for (o = abfd->sections; o != NULL; o = o->next)
  11166.     {
  11167.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  11168.       if ((o->flags & SEC_RELOC) == 0)
  11169.         continue;
  11170.  
  11171.       if (esdo->rel.hdr != NULL)
  11172.         elf_link_adjust_relocs (abfd, &esdo->rel);
  11173.       if (esdo->rela.hdr != NULL)
  11174.         elf_link_adjust_relocs (abfd, &esdo->rela);
  11175.  
  11176.       /* Set the reloc_count field to 0 to prevent write_relocs from
  11177.          trying to swap the relocs out itself.  */
  11178.       o->reloc_count = 0;
  11179.     }
  11180.  
  11181.   if (dynamic && info->combreloc && dynobj != NULL)
  11182.     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
  11183.  
  11184.   /* If we are linking against a dynamic object, or generating a
  11185.      shared library, finish up the dynamic linking information.  */
  11186.   if (dynamic)
  11187.     {
  11188.       bfd_byte *dyncon, *dynconend;
  11189.  
  11190.       /* Fix up .dynamic entries.  */
  11191.       o = bfd_get_linker_section (dynobj, ".dynamic");
  11192.       BFD_ASSERT (o != NULL);
  11193.  
  11194.       dyncon = o->contents;
  11195.       dynconend = o->contents + o->size;
  11196.       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11197.         {
  11198.           Elf_Internal_Dyn dyn;
  11199.           const char *name;
  11200.           unsigned int type;
  11201.  
  11202.           bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11203.  
  11204.           switch (dyn.d_tag)
  11205.             {
  11206.             default:
  11207.               continue;
  11208.             case DT_NULL:
  11209.               if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
  11210.                 {
  11211.                   switch (elf_section_data (reldyn)->this_hdr.sh_type)
  11212.                     {
  11213.                     case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
  11214.                     case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
  11215.                     default: continue;
  11216.                     }
  11217.                   dyn.d_un.d_val = relativecount;
  11218.                   relativecount = 0;
  11219.                   break;
  11220.                 }
  11221.               continue;
  11222.  
  11223.             case DT_INIT:
  11224.               name = info->init_function;
  11225.               goto get_sym;
  11226.             case DT_FINI:
  11227.               name = info->fini_function;
  11228.             get_sym:
  11229.               {
  11230.                 struct elf_link_hash_entry *h;
  11231.  
  11232.                 h = elf_link_hash_lookup (elf_hash_table (info), name,
  11233.                                           FALSE, FALSE, TRUE);
  11234.                 if (h != NULL
  11235.                     && (h->root.type == bfd_link_hash_defined
  11236.                         || h->root.type == bfd_link_hash_defweak))
  11237.                   {
  11238.                     dyn.d_un.d_ptr = h->root.u.def.value;
  11239.                     o = h->root.u.def.section;
  11240.                     if (o->output_section != NULL)
  11241.                       dyn.d_un.d_ptr += (o->output_section->vma
  11242.                                          + o->output_offset);
  11243.                     else
  11244.                       {
  11245.                         /* The symbol is imported from another shared
  11246.                            library and does not apply to this one.  */
  11247.                         dyn.d_un.d_ptr = 0;
  11248.                       }
  11249.                     break;
  11250.                   }
  11251.               }
  11252.               continue;
  11253.  
  11254.             case DT_PREINIT_ARRAYSZ:
  11255.               name = ".preinit_array";
  11256.               goto get_size;
  11257.             case DT_INIT_ARRAYSZ:
  11258.               name = ".init_array";
  11259.               goto get_size;
  11260.             case DT_FINI_ARRAYSZ:
  11261.               name = ".fini_array";
  11262.             get_size:
  11263.               o = bfd_get_section_by_name (abfd, name);
  11264.               if (o == NULL)
  11265.                 {
  11266.                   (*_bfd_error_handler)
  11267.                     (_("%B: could not find output section %s"), abfd, name);
  11268.                   goto error_return;
  11269.                 }
  11270.               if (o->size == 0)
  11271.                 (*_bfd_error_handler)
  11272.                   (_("warning: %s section has zero size"), name);
  11273.               dyn.d_un.d_val = o->size;
  11274.               break;
  11275.  
  11276.             case DT_PREINIT_ARRAY:
  11277.               name = ".preinit_array";
  11278.               goto get_vma;
  11279.             case DT_INIT_ARRAY:
  11280.               name = ".init_array";
  11281.               goto get_vma;
  11282.             case DT_FINI_ARRAY:
  11283.               name = ".fini_array";
  11284.               goto get_vma;
  11285.  
  11286.             case DT_HASH:
  11287.               name = ".hash";
  11288.               goto get_vma;
  11289.             case DT_GNU_HASH:
  11290.               name = ".gnu.hash";
  11291.               goto get_vma;
  11292.             case DT_STRTAB:
  11293.               name = ".dynstr";
  11294.               goto get_vma;
  11295.             case DT_SYMTAB:
  11296.               name = ".dynsym";
  11297.               goto get_vma;
  11298.             case DT_VERDEF:
  11299.               name = ".gnu.version_d";
  11300.               goto get_vma;
  11301.             case DT_VERNEED:
  11302.               name = ".gnu.version_r";
  11303.               goto get_vma;
  11304.             case DT_VERSYM:
  11305.               name = ".gnu.version";
  11306.             get_vma:
  11307.               o = bfd_get_section_by_name (abfd, name);
  11308.               if (o == NULL)
  11309.                 {
  11310.                   (*_bfd_error_handler)
  11311.                     (_("%B: could not find output section %s"), abfd, name);
  11312.                   goto error_return;
  11313.                 }
  11314.               if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
  11315.                 {
  11316.                   (*_bfd_error_handler)
  11317.                     (_("warning: section '%s' is being made into a note"), name);
  11318.                   bfd_set_error (bfd_error_nonrepresentable_section);
  11319.                   goto error_return;
  11320.                 }
  11321.               dyn.d_un.d_ptr = o->vma;
  11322.               break;
  11323.  
  11324.             case DT_REL:
  11325.             case DT_RELA:
  11326.             case DT_RELSZ:
  11327.             case DT_RELASZ:
  11328.               if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
  11329.                 type = SHT_REL;
  11330.               else
  11331.                 type = SHT_RELA;
  11332.               dyn.d_un.d_val = 0;
  11333.               dyn.d_un.d_ptr = 0;
  11334.               for (i = 1; i < elf_numsections (abfd); i++)
  11335.                 {
  11336.                   Elf_Internal_Shdr *hdr;
  11337.  
  11338.                   hdr = elf_elfsections (abfd)[i];
  11339.                   if (hdr->sh_type == type
  11340.                       && (hdr->sh_flags & SHF_ALLOC) != 0)
  11341.                     {
  11342.                       if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
  11343.                         dyn.d_un.d_val += hdr->sh_size;
  11344.                       else
  11345.                         {
  11346.                           if (dyn.d_un.d_ptr == 0
  11347.                               || hdr->sh_addr < dyn.d_un.d_ptr)
  11348.                             dyn.d_un.d_ptr = hdr->sh_addr;
  11349.                         }
  11350.                     }
  11351.                 }
  11352.               break;
  11353.             }
  11354.           bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
  11355.         }
  11356.     }
  11357.  
  11358.   /* If we have created any dynamic sections, then output them.  */
  11359.   if (dynobj != NULL)
  11360.     {
  11361.       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
  11362.         goto error_return;
  11363.  
  11364.       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
  11365.       if (((info->warn_shared_textrel && info->shared)
  11366.            || info->error_textrel)
  11367.           && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
  11368.         {
  11369.           bfd_byte *dyncon, *dynconend;
  11370.  
  11371.           dyncon = o->contents;
  11372.           dynconend = o->contents + o->size;
  11373.           for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11374.             {
  11375.               Elf_Internal_Dyn dyn;
  11376.  
  11377.               bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11378.  
  11379.               if (dyn.d_tag == DT_TEXTREL)
  11380.                 {
  11381.                   if (info->error_textrel)
  11382.                     info->callbacks->einfo
  11383.                       (_("%P%X: read-only segment has dynamic relocations.\n"));
  11384.                   else
  11385.                     info->callbacks->einfo
  11386.                       (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
  11387.                   break;
  11388.                 }
  11389.             }
  11390.         }
  11391.  
  11392.       for (o = dynobj->sections; o != NULL; o = o->next)
  11393.         {
  11394.           if ((o->flags & SEC_HAS_CONTENTS) == 0
  11395.               || o->size == 0
  11396.               || o->output_section == bfd_abs_section_ptr)
  11397.             continue;
  11398.           if ((o->flags & SEC_LINKER_CREATED) == 0)
  11399.             {
  11400.               /* At this point, we are only interested in sections
  11401.                  created by _bfd_elf_link_create_dynamic_sections.  */
  11402.               continue;
  11403.             }
  11404.           if (elf_hash_table (info)->stab_info.stabstr == o)
  11405.             continue;
  11406.           if (elf_hash_table (info)->eh_info.hdr_sec == o)
  11407.             continue;
  11408.           if (strcmp (o->name, ".dynstr") != 0)
  11409.             {
  11410.               /* FIXME: octets_per_byte.  */
  11411.               if (! bfd_set_section_contents (abfd, o->output_section,
  11412.                                               o->contents,
  11413.                                               (file_ptr) o->output_offset,
  11414.                                               o->size))
  11415.                 goto error_return;
  11416.             }
  11417.           else
  11418.             {
  11419.               /* The contents of the .dynstr section are actually in a
  11420.                  stringtab.  */
  11421.               off = elf_section_data (o->output_section)->this_hdr.sh_offset;
  11422.               if (bfd_seek (abfd, off, SEEK_SET) != 0
  11423.                   || ! _bfd_elf_strtab_emit (abfd,
  11424.                                              elf_hash_table (info)->dynstr))
  11425.                 goto error_return;
  11426.             }
  11427.         }
  11428.     }
  11429.  
  11430.   if (info->relocatable)
  11431.     {
  11432.       bfd_boolean failed = FALSE;
  11433.  
  11434.       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
  11435.       if (failed)
  11436.         goto error_return;
  11437.     }
  11438.  
  11439.   /* If we have optimized stabs strings, output them.  */
  11440.   if (elf_hash_table (info)->stab_info.stabstr != NULL)
  11441.     {
  11442.       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
  11443.         goto error_return;
  11444.     }
  11445.  
  11446.   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
  11447.     goto error_return;
  11448.  
  11449.   elf_final_link_free (abfd, &flinfo);
  11450.  
  11451.   elf_linker (abfd) = TRUE;
  11452.  
  11453.   if (attr_section)
  11454.     {
  11455.       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
  11456.       if (contents == NULL)
  11457.         return FALSE;   /* Bail out and fail.  */
  11458.       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
  11459.       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
  11460.       free (contents);
  11461.     }
  11462.  
  11463.   return TRUE;
  11464.  
  11465.  error_return:
  11466.   elf_final_link_free (abfd, &flinfo);
  11467.   return FALSE;
  11468. }
  11469. /* Initialize COOKIE for input bfd ABFD.  */
  11470.  
  11471. static bfd_boolean
  11472. init_reloc_cookie (struct elf_reloc_cookie *cookie,
  11473.                    struct bfd_link_info *info, bfd *abfd)
  11474. {
  11475.   Elf_Internal_Shdr *symtab_hdr;
  11476.   const struct elf_backend_data *bed;
  11477.  
  11478.   bed = get_elf_backend_data (abfd);
  11479.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11480.  
  11481.   cookie->abfd = abfd;
  11482.   cookie->sym_hashes = elf_sym_hashes (abfd);
  11483.   cookie->bad_symtab = elf_bad_symtab (abfd);
  11484.   if (cookie->bad_symtab)
  11485.     {
  11486.       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  11487.       cookie->extsymoff = 0;
  11488.     }
  11489.   else
  11490.     {
  11491.       cookie->locsymcount = symtab_hdr->sh_info;
  11492.       cookie->extsymoff = symtab_hdr->sh_info;
  11493.     }
  11494.  
  11495.   if (bed->s->arch_size == 32)
  11496.     cookie->r_sym_shift = 8;
  11497.   else
  11498.     cookie->r_sym_shift = 32;
  11499.  
  11500.   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  11501.   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
  11502.     {
  11503.       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  11504.                                               cookie->locsymcount, 0,
  11505.                                               NULL, NULL, NULL);
  11506.       if (cookie->locsyms == NULL)
  11507.         {
  11508.           info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
  11509.           return FALSE;
  11510.         }
  11511.       if (info->keep_memory)
  11512.         symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
  11513.     }
  11514.   return TRUE;
  11515. }
  11516.  
  11517. /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
  11518.  
  11519. static void
  11520. fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
  11521. {
  11522.   Elf_Internal_Shdr *symtab_hdr;
  11523.  
  11524.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11525.   if (cookie->locsyms != NULL
  11526.       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
  11527.     free (cookie->locsyms);
  11528. }
  11529.  
  11530. /* Initialize the relocation information in COOKIE for input section SEC
  11531.    of input bfd ABFD.  */
  11532.  
  11533. static bfd_boolean
  11534. init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11535.                         struct bfd_link_info *info, bfd *abfd,
  11536.                         asection *sec)
  11537. {
  11538.   const struct elf_backend_data *bed;
  11539.  
  11540.   if (sec->reloc_count == 0)
  11541.     {
  11542.       cookie->rels = NULL;
  11543.       cookie->relend = NULL;
  11544.     }
  11545.   else
  11546.     {
  11547.       bed = get_elf_backend_data (abfd);
  11548.  
  11549.       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  11550.                                                 info->keep_memory);
  11551.       if (cookie->rels == NULL)
  11552.         return FALSE;
  11553.       cookie->rel = cookie->rels;
  11554.       cookie->relend = (cookie->rels
  11555.                         + sec->reloc_count * bed->s->int_rels_per_ext_rel);
  11556.     }
  11557.   cookie->rel = cookie->rels;
  11558.   return TRUE;
  11559. }
  11560.  
  11561. /* Free the memory allocated by init_reloc_cookie_rels,
  11562.    if appropriate.  */
  11563.  
  11564. static void
  11565. fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11566.                         asection *sec)
  11567. {
  11568.   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
  11569.     free (cookie->rels);
  11570. }
  11571.  
  11572. /* Initialize the whole of COOKIE for input section SEC.  */
  11573.  
  11574. static bfd_boolean
  11575. init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  11576.                                struct bfd_link_info *info,
  11577.                                asection *sec)
  11578. {
  11579.   if (!init_reloc_cookie (cookie, info, sec->owner))
  11580.     goto error1;
  11581.   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
  11582.     goto error2;
  11583.   return TRUE;
  11584.  
  11585.  error2:
  11586.   fini_reloc_cookie (cookie, sec->owner);
  11587.  error1:
  11588.   return FALSE;
  11589. }
  11590.  
  11591. /* Free the memory allocated by init_reloc_cookie_for_section,
  11592.    if appropriate.  */
  11593.  
  11594. static void
  11595. fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  11596.                                asection *sec)
  11597. {
  11598.   fini_reloc_cookie_rels (cookie, sec);
  11599.   fini_reloc_cookie (cookie, sec->owner);
  11600. }
  11601. /* Garbage collect unused sections.  */
  11602.  
  11603. /* Default gc_mark_hook.  */
  11604.  
  11605. asection *
  11606. _bfd_elf_gc_mark_hook (asection *sec,
  11607.                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
  11608.                        Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
  11609.                        struct elf_link_hash_entry *h,
  11610.                        Elf_Internal_Sym *sym)
  11611. {
  11612.   const char *sec_name;
  11613.  
  11614.   if (h != NULL)
  11615.     {
  11616.       switch (h->root.type)
  11617.         {
  11618.         case bfd_link_hash_defined:
  11619.         case bfd_link_hash_defweak:
  11620.           return h->root.u.def.section;
  11621.  
  11622.         case bfd_link_hash_common:
  11623.           return h->root.u.c.p->section;
  11624.  
  11625.         case bfd_link_hash_undefined:
  11626.         case bfd_link_hash_undefweak:
  11627.           /* To work around a glibc bug, keep all XXX input sections
  11628.              when there is an as yet undefined reference to __start_XXX
  11629.              or __stop_XXX symbols.  The linker will later define such
  11630.              symbols for orphan input sections that have a name
  11631.              representable as a C identifier.  */
  11632.           if (strncmp (h->root.root.string, "__start_", 8) == 0)
  11633.             sec_name = h->root.root.string + 8;
  11634.           else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
  11635.             sec_name = h->root.root.string + 7;
  11636.           else
  11637.             sec_name = NULL;
  11638.  
  11639.           if (sec_name && *sec_name != '\0')
  11640.             {
  11641.               bfd *i;
  11642.  
  11643.               for (i = info->input_bfds; i; i = i->link_next)
  11644.                 {
  11645.                   sec = bfd_get_section_by_name (i, sec_name);
  11646.                   if (sec)
  11647.                     sec->flags |= SEC_KEEP;
  11648.                 }
  11649.             }
  11650.           break;
  11651.  
  11652.         default:
  11653.           break;
  11654.         }
  11655.     }
  11656.   else
  11657.     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
  11658.  
  11659.   return NULL;
  11660. }
  11661.  
  11662. /* COOKIE->rel describes a relocation against section SEC, which is
  11663.    a section we've decided to keep.  Return the section that contains
  11664.    the relocation symbol, or NULL if no section contains it.  */
  11665.  
  11666. asection *
  11667. _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
  11668.                        elf_gc_mark_hook_fn gc_mark_hook,
  11669.                        struct elf_reloc_cookie *cookie)
  11670. {
  11671.   unsigned long r_symndx;
  11672.   struct elf_link_hash_entry *h;
  11673.  
  11674.   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
  11675.   if (r_symndx == STN_UNDEF)
  11676.     return NULL;
  11677.  
  11678.   if (r_symndx >= cookie->locsymcount
  11679.       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  11680.     {
  11681.       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
  11682.       while (h->root.type == bfd_link_hash_indirect
  11683.              || h->root.type == bfd_link_hash_warning)
  11684.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  11685.       h->mark = 1;
  11686.       /* If this symbol is weak and there is a non-weak definition, we
  11687.          keep the non-weak definition because many backends put
  11688.          dynamic reloc info on the non-weak definition for code
  11689.          handling copy relocs.  */
  11690.       if (h->u.weakdef != NULL)
  11691.         h->u.weakdef->mark = 1;
  11692.       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
  11693.     }
  11694.  
  11695.   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
  11696.                           &cookie->locsyms[r_symndx]);
  11697. }
  11698.  
  11699. /* COOKIE->rel describes a relocation against section SEC, which is
  11700.    a section we've decided to keep.  Mark the section that contains
  11701.    the relocation symbol.  */
  11702.  
  11703. bfd_boolean
  11704. _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
  11705.                         asection *sec,
  11706.                         elf_gc_mark_hook_fn gc_mark_hook,
  11707.                         struct elf_reloc_cookie *cookie)
  11708. {
  11709.   asection *rsec;
  11710.  
  11711.   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
  11712.   if (rsec && !rsec->gc_mark)
  11713.     {
  11714.       if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
  11715.           || (rsec->owner->flags & DYNAMIC) != 0)
  11716.         rsec->gc_mark = 1;
  11717.       else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
  11718.         return FALSE;
  11719.     }
  11720.   return TRUE;
  11721. }
  11722.  
  11723. /* The mark phase of garbage collection.  For a given section, mark
  11724.    it and any sections in this section's group, and all the sections
  11725.    which define symbols to which it refers.  */
  11726.  
  11727. bfd_boolean
  11728. _bfd_elf_gc_mark (struct bfd_link_info *info,
  11729.                   asection *sec,
  11730.                   elf_gc_mark_hook_fn gc_mark_hook)
  11731. {
  11732.   bfd_boolean ret;
  11733.   asection *group_sec, *eh_frame;
  11734.  
  11735.   sec->gc_mark = 1;
  11736.  
  11737.   /* Mark all the sections in the group.  */
  11738.   group_sec = elf_section_data (sec)->next_in_group;
  11739.   if (group_sec && !group_sec->gc_mark)
  11740.     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
  11741.       return FALSE;
  11742.  
  11743.   /* Look through the section relocs.  */
  11744.   ret = TRUE;
  11745.   eh_frame = elf_eh_frame_section (sec->owner);
  11746.   if ((sec->flags & SEC_RELOC) != 0
  11747.       && sec->reloc_count > 0
  11748.       && sec != eh_frame)
  11749.     {
  11750.       struct elf_reloc_cookie cookie;
  11751.  
  11752.       if (!init_reloc_cookie_for_section (&cookie, info, sec))
  11753.         ret = FALSE;
  11754.       else
  11755.         {
  11756.           for (; cookie.rel < cookie.relend; cookie.rel++)
  11757.             if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
  11758.               {
  11759.                 ret = FALSE;
  11760.                 break;
  11761.               }
  11762.           fini_reloc_cookie_for_section (&cookie, sec);
  11763.         }
  11764.     }
  11765.  
  11766.   if (ret && eh_frame && elf_fde_list (sec))
  11767.     {
  11768.       struct elf_reloc_cookie cookie;
  11769.  
  11770.       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
  11771.         ret = FALSE;
  11772.       else
  11773.         {
  11774.           if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
  11775.                                       gc_mark_hook, &cookie))
  11776.             ret = FALSE;
  11777.           fini_reloc_cookie_for_section (&cookie, eh_frame);
  11778.         }
  11779.     }
  11780.  
  11781.   return ret;
  11782. }
  11783.  
  11784. /* Keep debug and special sections.  */
  11785.  
  11786. bfd_boolean
  11787. _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
  11788.                                  elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
  11789. {
  11790.   bfd *ibfd;
  11791.  
  11792.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
  11793.     {
  11794.       asection *isec;
  11795.       bfd_boolean some_kept;
  11796.       bfd_boolean debug_frag_seen;
  11797.  
  11798.       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  11799.         continue;
  11800.  
  11801.       /* Ensure all linker created sections are kept,
  11802.          see if any other section is already marked,
  11803.          and note if we have any fragmented debug sections.  */
  11804.       debug_frag_seen = some_kept = FALSE;
  11805.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  11806.         {
  11807.           if ((isec->flags & SEC_LINKER_CREATED) != 0)
  11808.             isec->gc_mark = 1;
  11809.           else if (isec->gc_mark)
  11810.             some_kept = TRUE;
  11811.  
  11812.           if (debug_frag_seen == FALSE
  11813.               && (isec->flags & SEC_DEBUGGING)
  11814.               && CONST_STRNEQ (isec->name, ".debug_line."))
  11815.             debug_frag_seen = TRUE;
  11816.         }
  11817.  
  11818.       /* If no section in this file will be kept, then we can
  11819.          toss out the debug and special sections.  */
  11820.       if (!some_kept)
  11821.         continue;
  11822.  
  11823.       /* Keep debug and special sections like .comment when they are
  11824.          not part of a group, or when we have single-member groups.  */
  11825.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  11826.         if ((elf_next_in_group (isec) == NULL
  11827.              || elf_next_in_group (isec) == isec)
  11828.             && ((isec->flags & SEC_DEBUGGING) != 0
  11829.                 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
  11830.           isec->gc_mark = 1;
  11831.  
  11832.       if (! debug_frag_seen)
  11833.         continue;
  11834.  
  11835.       /* Look for CODE sections which are going to be discarded,
  11836.          and find and discard any fragmented debug sections which
  11837.          are associated with that code section.  */
  11838.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  11839.         if ((isec->flags & SEC_CODE) != 0
  11840.             && isec->gc_mark == 0)
  11841.           {
  11842.             unsigned int ilen;
  11843.             asection *dsec;
  11844.  
  11845.             ilen = strlen (isec->name);
  11846.  
  11847.             /* Association is determined by the name of the debug section
  11848.                containing the name of the code section as a suffix.  For
  11849.                example .debug_line.text.foo is a debug section associated
  11850.                with .text.foo.  */
  11851.             for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
  11852.               {
  11853.                 unsigned int dlen;
  11854.  
  11855.                 if (dsec->gc_mark == 0
  11856.                     || (dsec->flags & SEC_DEBUGGING) == 0)
  11857.                   continue;
  11858.  
  11859.                 dlen = strlen (dsec->name);
  11860.  
  11861.                 if (dlen > ilen
  11862.                     && strncmp (dsec->name + (dlen - ilen),
  11863.                                 isec->name, ilen) == 0)
  11864.                   {
  11865.                     dsec->gc_mark = 0;
  11866.                     break;
  11867.                   }
  11868.               }
  11869.           }
  11870.     }
  11871.   return TRUE;
  11872. }
  11873.  
  11874. /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
  11875.  
  11876. struct elf_gc_sweep_symbol_info
  11877. {
  11878.   struct bfd_link_info *info;
  11879.   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
  11880.                        bfd_boolean);
  11881. };
  11882.  
  11883. static bfd_boolean
  11884. elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
  11885. {
  11886.   if (!h->mark
  11887.       && (((h->root.type == bfd_link_hash_defined
  11888.             || h->root.type == bfd_link_hash_defweak)
  11889.            && !(h->def_regular
  11890.                 && h->root.u.def.section->gc_mark))
  11891.           || h->root.type == bfd_link_hash_undefined
  11892.           || h->root.type == bfd_link_hash_undefweak))
  11893.     {
  11894.       struct elf_gc_sweep_symbol_info *inf;
  11895.  
  11896.       inf = (struct elf_gc_sweep_symbol_info *) data;
  11897.       (*inf->hide_symbol) (inf->info, h, TRUE);
  11898.       h->def_regular = 0;
  11899.       h->ref_regular = 0;
  11900.       h->ref_regular_nonweak = 0;
  11901.     }
  11902.  
  11903.   return TRUE;
  11904. }
  11905.  
  11906. /* The sweep phase of garbage collection.  Remove all garbage sections.  */
  11907.  
  11908. typedef bfd_boolean (*gc_sweep_hook_fn)
  11909.   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
  11910.  
  11911. static bfd_boolean
  11912. elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
  11913. {
  11914.   bfd *sub;
  11915.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  11916.   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
  11917.   unsigned long section_sym_count;
  11918.   struct elf_gc_sweep_symbol_info sweep_info;
  11919.  
  11920.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  11921.     {
  11922.       asection *o;
  11923.  
  11924.       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
  11925.         continue;
  11926.  
  11927.       for (o = sub->sections; o != NULL; o = o->next)
  11928.         {
  11929.           /* When any section in a section group is kept, we keep all
  11930.              sections in the section group.  If the first member of
  11931.              the section group is excluded, we will also exclude the
  11932.              group section.  */
  11933.           if (o->flags & SEC_GROUP)
  11934.             {
  11935.               asection *first = elf_next_in_group (o);
  11936.               o->gc_mark = first->gc_mark;
  11937.             }
  11938.  
  11939.           if (o->gc_mark)
  11940.             continue;
  11941.  
  11942.           /* Skip sweeping sections already excluded.  */
  11943.           if (o->flags & SEC_EXCLUDE)
  11944.             continue;
  11945.  
  11946.           /* Since this is early in the link process, it is simple
  11947.              to remove a section from the output.  */
  11948.           o->flags |= SEC_EXCLUDE;
  11949.  
  11950.           if (info->print_gc_sections && o->size != 0)
  11951.             _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
  11952.  
  11953.           /* But we also have to update some of the relocation
  11954.              info we collected before.  */
  11955.           if (gc_sweep_hook
  11956.               && (o->flags & SEC_RELOC) != 0
  11957.               && o->reloc_count > 0
  11958.               && !bfd_is_abs_section (o->output_section))
  11959.             {
  11960.               Elf_Internal_Rela *internal_relocs;
  11961.               bfd_boolean r;
  11962.  
  11963.               internal_relocs
  11964.                 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
  11965.                                              info->keep_memory);
  11966.               if (internal_relocs == NULL)
  11967.                 return FALSE;
  11968.  
  11969.               r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
  11970.  
  11971.               if (elf_section_data (o)->relocs != internal_relocs)
  11972.                 free (internal_relocs);
  11973.  
  11974.               if (!r)
  11975.                 return FALSE;
  11976.             }
  11977.         }
  11978.     }
  11979.  
  11980.   /* Remove the symbols that were in the swept sections from the dynamic
  11981.      symbol table.  GCFIXME: Anyone know how to get them out of the
  11982.      static symbol table as well?  */
  11983.   sweep_info.info = info;
  11984.   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
  11985.   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
  11986.                           &sweep_info);
  11987.  
  11988.   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
  11989.   return TRUE;
  11990. }
  11991.  
  11992. /* Propagate collected vtable information.  This is called through
  11993.    elf_link_hash_traverse.  */
  11994.  
  11995. static bfd_boolean
  11996. elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
  11997. {
  11998.   /* Those that are not vtables.  */
  11999.   if (h->vtable == NULL || h->vtable->parent == NULL)
  12000.     return TRUE;
  12001.  
  12002.   /* Those vtables that do not have parents, we cannot merge.  */
  12003.   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
  12004.     return TRUE;
  12005.  
  12006.   /* If we've already been done, exit.  */
  12007.   if (h->vtable->used && h->vtable->used[-1])
  12008.     return TRUE;
  12009.  
  12010.   /* Make sure the parent's table is up to date.  */
  12011.   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
  12012.  
  12013.   if (h->vtable->used == NULL)
  12014.     {
  12015.       /* None of this table's entries were referenced.  Re-use the
  12016.          parent's table.  */
  12017.       h->vtable->used = h->vtable->parent->vtable->used;
  12018.       h->vtable->size = h->vtable->parent->vtable->size;
  12019.     }
  12020.   else
  12021.     {
  12022.       size_t n;
  12023.       bfd_boolean *cu, *pu;
  12024.  
  12025.       /* Or the parent's entries into ours.  */
  12026.       cu = h->vtable->used;
  12027.       cu[-1] = TRUE;
  12028.       pu = h->vtable->parent->vtable->used;
  12029.       if (pu != NULL)
  12030.         {
  12031.           const struct elf_backend_data *bed;
  12032.           unsigned int log_file_align;
  12033.  
  12034.           bed = get_elf_backend_data (h->root.u.def.section->owner);
  12035.           log_file_align = bed->s->log_file_align;
  12036.           n = h->vtable->parent->vtable->size >> log_file_align;
  12037.           while (n--)
  12038.             {
  12039.               if (*pu)
  12040.                 *cu = TRUE;
  12041.               pu++;
  12042.               cu++;
  12043.             }
  12044.         }
  12045.     }
  12046.  
  12047.   return TRUE;
  12048. }
  12049.  
  12050. static bfd_boolean
  12051. elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
  12052. {
  12053.   asection *sec;
  12054.   bfd_vma hstart, hend;
  12055.   Elf_Internal_Rela *relstart, *relend, *rel;
  12056.   const struct elf_backend_data *bed;
  12057.   unsigned int log_file_align;
  12058.  
  12059.   /* Take care of both those symbols that do not describe vtables as
  12060.      well as those that are not loaded.  */
  12061.   if (h->vtable == NULL || h->vtable->parent == NULL)
  12062.     return TRUE;
  12063.  
  12064.   BFD_ASSERT (h->root.type == bfd_link_hash_defined
  12065.               || h->root.type == bfd_link_hash_defweak);
  12066.  
  12067.   sec = h->root.u.def.section;
  12068.   hstart = h->root.u.def.value;
  12069.   hend = hstart + h->size;
  12070.  
  12071.   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
  12072.   if (!relstart)
  12073.     return *(bfd_boolean *) okp = FALSE;
  12074.   bed = get_elf_backend_data (sec->owner);
  12075.   log_file_align = bed->s->log_file_align;
  12076.  
  12077.   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
  12078.  
  12079.   for (rel = relstart; rel < relend; ++rel)
  12080.     if (rel->r_offset >= hstart && rel->r_offset < hend)
  12081.       {
  12082.         /* If the entry is in use, do nothing.  */
  12083.         if (h->vtable->used
  12084.             && (rel->r_offset - hstart) < h->vtable->size)
  12085.           {
  12086.             bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
  12087.             if (h->vtable->used[entry])
  12088.               continue;
  12089.           }
  12090.         /* Otherwise, kill it.  */
  12091.         rel->r_offset = rel->r_info = rel->r_addend = 0;
  12092.       }
  12093.  
  12094.   return TRUE;
  12095. }
  12096.  
  12097. /* Mark sections containing dynamically referenced symbols.  When
  12098.    building shared libraries, we must assume that any visible symbol is
  12099.    referenced.  */
  12100.  
  12101. bfd_boolean
  12102. bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
  12103. {
  12104.   struct bfd_link_info *info = (struct bfd_link_info *) inf;
  12105.  
  12106.   if ((h->root.type == bfd_link_hash_defined
  12107.        || h->root.type == bfd_link_hash_defweak)
  12108.       && (h->ref_dynamic
  12109.           || ((!info->executable || info->export_dynamic)
  12110.               && h->def_regular
  12111.               && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  12112.               && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
  12113.               && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
  12114.                   || !bfd_hide_sym_by_version (info->version_info,
  12115.                                                h->root.root.string)))))
  12116.     h->root.u.def.section->flags |= SEC_KEEP;
  12117.  
  12118.   return TRUE;
  12119. }
  12120.  
  12121. /* Keep all sections containing symbols undefined on the command-line,
  12122.    and the section containing the entry symbol.  */
  12123.  
  12124. void
  12125. _bfd_elf_gc_keep (struct bfd_link_info *info)
  12126. {
  12127.   struct bfd_sym_chain *sym;
  12128.  
  12129.   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
  12130.     {
  12131.       struct elf_link_hash_entry *h;
  12132.  
  12133.       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
  12134.                                 FALSE, FALSE, FALSE);
  12135.  
  12136.       if (h != NULL
  12137.           && (h->root.type == bfd_link_hash_defined
  12138.               || h->root.type == bfd_link_hash_defweak)
  12139.           && !bfd_is_abs_section (h->root.u.def.section))
  12140.         h->root.u.def.section->flags |= SEC_KEEP;
  12141.     }
  12142. }
  12143.  
  12144. /* Do mark and sweep of unused sections.  */
  12145.  
  12146. bfd_boolean
  12147. bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
  12148. {
  12149.   bfd_boolean ok = TRUE;
  12150.   bfd *sub;
  12151.   elf_gc_mark_hook_fn gc_mark_hook;
  12152.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12153.  
  12154.   if (!bed->can_gc_sections
  12155.       || !is_elf_hash_table (info->hash))
  12156.     {
  12157.       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
  12158.       return TRUE;
  12159.     }
  12160.  
  12161.   bed->gc_keep (info);
  12162.  
  12163.   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
  12164.      at the .eh_frame section if we can mark the FDEs individually.  */
  12165.   _bfd_elf_begin_eh_frame_parsing (info);
  12166.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  12167.     {
  12168.       asection *sec;
  12169.       struct elf_reloc_cookie cookie;
  12170.  
  12171.       sec = bfd_get_section_by_name (sub, ".eh_frame");
  12172.       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
  12173.         {
  12174.           _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
  12175.           if (elf_section_data (sec)->sec_info
  12176.               && (sec->flags & SEC_LINKER_CREATED) == 0)
  12177.             elf_eh_frame_section (sub) = sec;
  12178.           fini_reloc_cookie_for_section (&cookie, sec);
  12179.           sec = bfd_get_next_section_by_name (sec);
  12180.         }
  12181.     }
  12182.   _bfd_elf_end_eh_frame_parsing (info);
  12183.  
  12184.   /* Apply transitive closure to the vtable entry usage info.  */
  12185.   elf_link_hash_traverse (elf_hash_table (info),
  12186.                           elf_gc_propagate_vtable_entries_used,
  12187.                           &ok);
  12188.   if (!ok)
  12189.     return FALSE;
  12190.  
  12191.   /* Kill the vtable relocations that were not used.  */
  12192.   elf_link_hash_traverse (elf_hash_table (info),
  12193.                           elf_gc_smash_unused_vtentry_relocs,
  12194.                           &ok);
  12195.   if (!ok)
  12196.     return FALSE;
  12197.  
  12198.   /* Mark dynamically referenced symbols.  */
  12199.   if (elf_hash_table (info)->dynamic_sections_created)
  12200.     elf_link_hash_traverse (elf_hash_table (info),
  12201.                             bed->gc_mark_dynamic_ref,
  12202.                             info);
  12203.  
  12204.   /* Grovel through relocs to find out who stays ...  */
  12205.   gc_mark_hook = bed->gc_mark_hook;
  12206.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  12207.     {
  12208.       asection *o;
  12209.  
  12210.       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
  12211.         continue;
  12212.  
  12213.       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
  12214.          Also treat note sections as a root, if the section is not part
  12215.          of a group.  */
  12216.       for (o = sub->sections; o != NULL; o = o->next)
  12217.         if (!o->gc_mark
  12218.             && (o->flags & SEC_EXCLUDE) == 0
  12219.             && ((o->flags & SEC_KEEP) != 0
  12220.                 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
  12221.                     && elf_next_in_group (o) == NULL )))
  12222.           {
  12223.             if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
  12224.               return FALSE;
  12225.           }
  12226.     }
  12227.  
  12228.   /* Allow the backend to mark additional target specific sections.  */
  12229.   bed->gc_mark_extra_sections (info, gc_mark_hook);
  12230.  
  12231.   /* ... and mark SEC_EXCLUDE for those that go.  */
  12232.   return elf_gc_sweep (abfd, info);
  12233. }
  12234. /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
  12235.  
  12236. bfd_boolean
  12237. bfd_elf_gc_record_vtinherit (bfd *abfd,
  12238.                              asection *sec,
  12239.                              struct elf_link_hash_entry *h,
  12240.                              bfd_vma offset)
  12241. {
  12242.   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  12243.   struct elf_link_hash_entry **search, *child;
  12244.   bfd_size_type extsymcount;
  12245.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12246.  
  12247.   /* The sh_info field of the symtab header tells us where the
  12248.      external symbols start.  We don't care about the local symbols at
  12249.      this point.  */
  12250.   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
  12251.   if (!elf_bad_symtab (abfd))
  12252.     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
  12253.  
  12254.   sym_hashes = elf_sym_hashes (abfd);
  12255.   sym_hashes_end = sym_hashes + extsymcount;
  12256.  
  12257.   /* Hunt down the child symbol, which is in this section at the same
  12258.      offset as the relocation.  */
  12259.   for (search = sym_hashes; search != sym_hashes_end; ++search)
  12260.     {
  12261.       if ((child = *search) != NULL
  12262.           && (child->root.type == bfd_link_hash_defined
  12263.               || child->root.type == bfd_link_hash_defweak)
  12264.           && child->root.u.def.section == sec
  12265.           && child->root.u.def.value == offset)
  12266.         goto win;
  12267.     }
  12268.  
  12269.   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
  12270.                          abfd, sec, (unsigned long) offset);
  12271.   bfd_set_error (bfd_error_invalid_operation);
  12272.   return FALSE;
  12273.  
  12274.  win:
  12275.   if (!child->vtable)
  12276.     {
  12277.       child->vtable = (struct elf_link_virtual_table_entry *)
  12278.           bfd_zalloc (abfd, sizeof (*child->vtable));
  12279.       if (!child->vtable)
  12280.         return FALSE;
  12281.     }
  12282.   if (!h)
  12283.     {
  12284.       /* This *should* only be the absolute section.  It could potentially
  12285.          be that someone has defined a non-global vtable though, which
  12286.          would be bad.  It isn't worth paging in the local symbols to be
  12287.          sure though; that case should simply be handled by the assembler.  */
  12288.  
  12289.       child->vtable->parent = (struct elf_link_hash_entry *) -1;
  12290.     }
  12291.   else
  12292.     child->vtable->parent = h;
  12293.  
  12294.   return TRUE;
  12295. }
  12296.  
  12297. /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
  12298.  
  12299. bfd_boolean
  12300. bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
  12301.                            asection *sec ATTRIBUTE_UNUSED,
  12302.                            struct elf_link_hash_entry *h,
  12303.                            bfd_vma addend)
  12304. {
  12305.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12306.   unsigned int log_file_align = bed->s->log_file_align;
  12307.  
  12308.   if (!h->vtable)
  12309.     {
  12310.       h->vtable = (struct elf_link_virtual_table_entry *)
  12311.           bfd_zalloc (abfd, sizeof (*h->vtable));
  12312.       if (!h->vtable)
  12313.         return FALSE;
  12314.     }
  12315.  
  12316.   if (addend >= h->vtable->size)
  12317.     {
  12318.       size_t size, bytes, file_align;
  12319.       bfd_boolean *ptr = h->vtable->used;
  12320.  
  12321.       /* While the symbol is undefined, we have to be prepared to handle
  12322.          a zero size.  */
  12323.       file_align = 1 << log_file_align;
  12324.       if (h->root.type == bfd_link_hash_undefined)
  12325.         size = addend + file_align;
  12326.       else
  12327.         {
  12328.           size = h->size;
  12329.           if (addend >= size)
  12330.             {
  12331.               /* Oops!  We've got a reference past the defined end of
  12332.                  the table.  This is probably a bug -- shall we warn?  */
  12333.               size = addend + file_align;
  12334.             }
  12335.         }
  12336.       size = (size + file_align - 1) & -file_align;
  12337.  
  12338.       /* Allocate one extra entry for use as a "done" flag for the
  12339.          consolidation pass.  */
  12340.       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
  12341.  
  12342.       if (ptr)
  12343.         {
  12344.           ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
  12345.  
  12346.           if (ptr != NULL)
  12347.             {
  12348.               size_t oldbytes;
  12349.  
  12350.               oldbytes = (((h->vtable->size >> log_file_align) + 1)
  12351.                           * sizeof (bfd_boolean));
  12352.               memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
  12353.             }
  12354.         }
  12355.       else
  12356.         ptr = (bfd_boolean *) bfd_zmalloc (bytes);
  12357.  
  12358.       if (ptr == NULL)
  12359.         return FALSE;
  12360.  
  12361.       /* And arrange for that done flag to be at index -1.  */
  12362.       h->vtable->used = ptr + 1;
  12363.       h->vtable->size = size;
  12364.     }
  12365.  
  12366.   h->vtable->used[addend >> log_file_align] = TRUE;
  12367.  
  12368.   return TRUE;
  12369. }
  12370.  
  12371. /* Map an ELF section header flag to its corresponding string.  */
  12372. typedef struct
  12373. {
  12374.   char *flag_name;
  12375.   flagword flag_value;
  12376. } elf_flags_to_name_table;
  12377.  
  12378. static elf_flags_to_name_table elf_flags_to_names [] =
  12379. {
  12380.   { "SHF_WRITE", SHF_WRITE },
  12381.   { "SHF_ALLOC", SHF_ALLOC },
  12382.   { "SHF_EXECINSTR", SHF_EXECINSTR },
  12383.   { "SHF_MERGE", SHF_MERGE },
  12384.   { "SHF_STRINGS", SHF_STRINGS },
  12385.   { "SHF_INFO_LINK", SHF_INFO_LINK},
  12386.   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
  12387.   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
  12388.   { "SHF_GROUP", SHF_GROUP },
  12389.   { "SHF_TLS", SHF_TLS },
  12390.   { "SHF_MASKOS", SHF_MASKOS },
  12391.   { "SHF_EXCLUDE", SHF_EXCLUDE },
  12392. };
  12393.  
  12394. /* Returns TRUE if the section is to be included, otherwise FALSE.  */
  12395. bfd_boolean
  12396. bfd_elf_lookup_section_flags (struct bfd_link_info *info,
  12397.                               struct flag_info *flaginfo,
  12398.                               asection *section)
  12399. {
  12400.   const bfd_vma sh_flags = elf_section_flags (section);
  12401.  
  12402.   if (!flaginfo->flags_initialized)
  12403.     {
  12404.       bfd *obfd = info->output_bfd;
  12405.       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  12406.       struct flag_info_list *tf = flaginfo->flag_list;
  12407.       int with_hex = 0;
  12408.       int without_hex = 0;
  12409.  
  12410.       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
  12411.         {
  12412.           unsigned i;
  12413.           flagword (*lookup) (char *);
  12414.  
  12415.           lookup = bed->elf_backend_lookup_section_flags_hook;
  12416.           if (lookup != NULL)
  12417.             {
  12418.               flagword hexval = (*lookup) ((char *) tf->name);
  12419.  
  12420.               if (hexval != 0)
  12421.                 {
  12422.                   if (tf->with == with_flags)
  12423.                     with_hex |= hexval;
  12424.                   else if (tf->with == without_flags)
  12425.                     without_hex |= hexval;
  12426.                   tf->valid = TRUE;
  12427.                   continue;
  12428.                 }
  12429.             }
  12430.           for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
  12431.             {
  12432.               if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
  12433.                 {
  12434.                   if (tf->with == with_flags)
  12435.                     with_hex |= elf_flags_to_names[i].flag_value;
  12436.                   else if (tf->with == without_flags)
  12437.                     without_hex |= elf_flags_to_names[i].flag_value;
  12438.                   tf->valid = TRUE;
  12439.                   break;
  12440.                 }
  12441.             }
  12442.           if (!tf->valid)
  12443.             {
  12444.               info->callbacks->einfo
  12445.                 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
  12446.               return FALSE;
  12447.             }
  12448.         }
  12449.       flaginfo->flags_initialized = TRUE;
  12450.       flaginfo->only_with_flags |= with_hex;
  12451.       flaginfo->not_with_flags |= without_hex;
  12452.     }
  12453.  
  12454.   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
  12455.     return FALSE;
  12456.  
  12457.   if ((flaginfo->not_with_flags & sh_flags) != 0)
  12458.     return FALSE;
  12459.  
  12460.   return TRUE;
  12461. }
  12462.  
  12463. struct alloc_got_off_arg {
  12464.   bfd_vma gotoff;
  12465.   struct bfd_link_info *info;
  12466. };
  12467.  
  12468. /* We need a special top-level link routine to convert got reference counts
  12469.    to real got offsets.  */
  12470.  
  12471. static bfd_boolean
  12472. elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
  12473. {
  12474.   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
  12475.   bfd *obfd = gofarg->info->output_bfd;
  12476.   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  12477.  
  12478.   if (h->got.refcount > 0)
  12479.     {
  12480.       h->got.offset = gofarg->gotoff;
  12481.       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
  12482.     }
  12483.   else
  12484.     h->got.offset = (bfd_vma) -1;
  12485.  
  12486.   return TRUE;
  12487. }
  12488.  
  12489. /* And an accompanying bit to work out final got entry offsets once
  12490.    we're done.  Should be called from final_link.  */
  12491.  
  12492. bfd_boolean
  12493. bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
  12494.                                         struct bfd_link_info *info)
  12495. {
  12496.   bfd *i;
  12497.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12498.   bfd_vma gotoff;
  12499.   struct alloc_got_off_arg gofarg;
  12500.  
  12501.   BFD_ASSERT (abfd == info->output_bfd);
  12502.  
  12503.   if (! is_elf_hash_table (info->hash))
  12504.     return FALSE;
  12505.  
  12506.   /* The GOT offset is relative to the .got section, but the GOT header is
  12507.      put into the .got.plt section, if the backend uses it.  */
  12508.   if (bed->want_got_plt)
  12509.     gotoff = 0;
  12510.   else
  12511.     gotoff = bed->got_header_size;
  12512.  
  12513.   /* Do the local .got entries first.  */
  12514.   for (i = info->input_bfds; i; i = i->link_next)
  12515.     {
  12516.       bfd_signed_vma *local_got;
  12517.       bfd_size_type j, locsymcount;
  12518.       Elf_Internal_Shdr *symtab_hdr;
  12519.  
  12520.       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
  12521.         continue;
  12522.  
  12523.       local_got = elf_local_got_refcounts (i);
  12524.       if (!local_got)
  12525.         continue;
  12526.  
  12527.       symtab_hdr = &elf_tdata (i)->symtab_hdr;
  12528.       if (elf_bad_symtab (i))
  12529.         locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  12530.       else
  12531.         locsymcount = symtab_hdr->sh_info;
  12532.  
  12533.       for (j = 0; j < locsymcount; ++j)
  12534.         {
  12535.           if (local_got[j] > 0)
  12536.             {
  12537.               local_got[j] = gotoff;
  12538.               gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
  12539.             }
  12540.           else
  12541.             local_got[j] = (bfd_vma) -1;
  12542.         }
  12543.     }
  12544.  
  12545.   /* Then the global .got entries.  .plt refcounts are handled by
  12546.      adjust_dynamic_symbol  */
  12547.   gofarg.gotoff = gotoff;
  12548.   gofarg.info = info;
  12549.   elf_link_hash_traverse (elf_hash_table (info),
  12550.                           elf_gc_allocate_got_offsets,
  12551.                           &gofarg);
  12552.   return TRUE;
  12553. }
  12554.  
  12555. /* Many folk need no more in the way of final link than this, once
  12556.    got entry reference counting is enabled.  */
  12557.  
  12558. bfd_boolean
  12559. bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
  12560. {
  12561.   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
  12562.     return FALSE;
  12563.  
  12564.   /* Invoke the regular ELF backend linker to do all the work.  */
  12565.   return bfd_elf_final_link (abfd, info);
  12566. }
  12567.  
  12568. bfd_boolean
  12569. bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
  12570. {
  12571.   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
  12572.  
  12573.   if (rcookie->bad_symtab)
  12574.     rcookie->rel = rcookie->rels;
  12575.  
  12576.   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
  12577.     {
  12578.       unsigned long r_symndx;
  12579.  
  12580.       if (! rcookie->bad_symtab)
  12581.         if (rcookie->rel->r_offset > offset)
  12582.           return FALSE;
  12583.       if (rcookie->rel->r_offset != offset)
  12584.         continue;
  12585.  
  12586.       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
  12587.       if (r_symndx == STN_UNDEF)
  12588.         return TRUE;
  12589.  
  12590.       if (r_symndx >= rcookie->locsymcount
  12591.           || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  12592.         {
  12593.           struct elf_link_hash_entry *h;
  12594.  
  12595.           h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
  12596.  
  12597.           while (h->root.type == bfd_link_hash_indirect
  12598.                  || h->root.type == bfd_link_hash_warning)
  12599.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  12600.  
  12601.           if ((h->root.type == bfd_link_hash_defined
  12602.                || h->root.type == bfd_link_hash_defweak)
  12603.               && discarded_section (h->root.u.def.section))
  12604.             return TRUE;
  12605.           else
  12606.             return FALSE;
  12607.         }
  12608.       else
  12609.         {
  12610.           /* It's not a relocation against a global symbol,
  12611.              but it could be a relocation against a local
  12612.              symbol for a discarded section.  */
  12613.           asection *isec;
  12614.           Elf_Internal_Sym *isym;
  12615.  
  12616.           /* Need to: get the symbol; get the section.  */
  12617.           isym = &rcookie->locsyms[r_symndx];
  12618.           isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
  12619.           if (isec != NULL && discarded_section (isec))
  12620.             return TRUE;
  12621.         }
  12622.       return FALSE;
  12623.     }
  12624.   return FALSE;
  12625. }
  12626.  
  12627. /* Discard unneeded references to discarded sections.
  12628.    Returns TRUE if any section's size was changed.  */
  12629. /* This function assumes that the relocations are in sorted order,
  12630.    which is true for all known assemblers.  */
  12631.  
  12632. bfd_boolean
  12633. bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
  12634. {
  12635.   struct elf_reloc_cookie cookie;
  12636.   asection *stab, *eh;
  12637.   const struct elf_backend_data *bed;
  12638.   bfd *abfd;
  12639.   bfd_boolean ret = FALSE;
  12640.  
  12641.   if (info->traditional_format
  12642.       || !is_elf_hash_table (info->hash))
  12643.     return FALSE;
  12644.  
  12645.   _bfd_elf_begin_eh_frame_parsing (info);
  12646.   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
  12647.     {
  12648.       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  12649.         continue;
  12650.  
  12651.       bed = get_elf_backend_data (abfd);
  12652.  
  12653.       eh = NULL;
  12654.       if (!info->relocatable)
  12655.         {
  12656.           eh = bfd_get_section_by_name (abfd, ".eh_frame");
  12657.           while (eh != NULL
  12658.                  && (eh->size == 0
  12659.                      || bfd_is_abs_section (eh->output_section)))
  12660.             eh = bfd_get_next_section_by_name (eh);
  12661.         }
  12662.  
  12663.       stab = bfd_get_section_by_name (abfd, ".stab");
  12664.       if (stab != NULL
  12665.           && (stab->size == 0
  12666.               || bfd_is_abs_section (stab->output_section)
  12667.               || stab->sec_info_type != SEC_INFO_TYPE_STABS))
  12668.         stab = NULL;
  12669.  
  12670.       if (stab == NULL
  12671.           && eh == NULL
  12672.           && bed->elf_backend_discard_info == NULL)
  12673.         continue;
  12674.  
  12675.       if (!init_reloc_cookie (&cookie, info, abfd))
  12676.         return FALSE;
  12677.  
  12678.       if (stab != NULL
  12679.           && stab->reloc_count > 0
  12680.           && init_reloc_cookie_rels (&cookie, info, abfd, stab))
  12681.         {
  12682.           if (_bfd_discard_section_stabs (abfd, stab,
  12683.                                           elf_section_data (stab)->sec_info,
  12684.                                           bfd_elf_reloc_symbol_deleted_p,
  12685.                                           &cookie))
  12686.             ret = TRUE;
  12687.           fini_reloc_cookie_rels (&cookie, stab);
  12688.         }
  12689.  
  12690.       while (eh != NULL
  12691.              && init_reloc_cookie_rels (&cookie, info, abfd, eh))
  12692.         {
  12693.           _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
  12694.           if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
  12695.                                                  bfd_elf_reloc_symbol_deleted_p,
  12696.                                                  &cookie))
  12697.             ret = TRUE;
  12698.           fini_reloc_cookie_rels (&cookie, eh);
  12699.           eh = bfd_get_next_section_by_name (eh);
  12700.         }
  12701.  
  12702.       if (bed->elf_backend_discard_info != NULL
  12703.           && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
  12704.         ret = TRUE;
  12705.  
  12706.       fini_reloc_cookie (&cookie, abfd);
  12707.     }
  12708.   _bfd_elf_end_eh_frame_parsing (info);
  12709.  
  12710.   if (info->eh_frame_hdr
  12711.       && !info->relocatable
  12712.       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
  12713.     ret = TRUE;
  12714.  
  12715.   return ret;
  12716. }
  12717.  
  12718. bfd_boolean
  12719. _bfd_elf_section_already_linked (bfd *abfd,
  12720.                                  asection *sec,
  12721.                                  struct bfd_link_info *info)
  12722. {
  12723.   flagword flags;
  12724.   const char *name, *key;
  12725.   struct bfd_section_already_linked *l;
  12726.   struct bfd_section_already_linked_hash_entry *already_linked_list;
  12727.  
  12728.   if (sec->output_section == bfd_abs_section_ptr)
  12729.     return FALSE;
  12730.  
  12731.   flags = sec->flags;
  12732.  
  12733.   /* Return if it isn't a linkonce section.  A comdat group section
  12734.      also has SEC_LINK_ONCE set.  */
  12735.   if ((flags & SEC_LINK_ONCE) == 0)
  12736.     return FALSE;
  12737.  
  12738.   /* Don't put group member sections on our list of already linked
  12739.      sections.  They are handled as a group via their group section.  */
  12740.   if (elf_sec_group (sec) != NULL)
  12741.     return FALSE;
  12742.  
  12743.   /* For a SHT_GROUP section, use the group signature as the key.  */
  12744.   name = sec->name;
  12745.   if ((flags & SEC_GROUP) != 0
  12746.       && elf_next_in_group (sec) != NULL
  12747.       && elf_group_name (elf_next_in_group (sec)) != NULL)
  12748.     key = elf_group_name (elf_next_in_group (sec));
  12749.   else
  12750.     {
  12751.       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
  12752.       if (CONST_STRNEQ (name, ".gnu.linkonce.")
  12753.           && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
  12754.         key++;
  12755.       else
  12756.         /* Must be a user linkonce section that doesn't follow gcc's
  12757.            naming convention.  In this case we won't be matching
  12758.            single member groups.  */
  12759.         key = name;
  12760.     }
  12761.  
  12762.   already_linked_list = bfd_section_already_linked_table_lookup (key);
  12763.  
  12764.   for (l = already_linked_list->entry; l != NULL; l = l->next)
  12765.     {
  12766.       /* We may have 2 different types of sections on the list: group
  12767.          sections with a signature of <key> (<key> is some string),
  12768.          and linkonce sections named .gnu.linkonce.<type>.<key>.
  12769.          Match like sections.  LTO plugin sections are an exception.
  12770.          They are always named .gnu.linkonce.t.<key> and match either
  12771.          type of section.  */
  12772.       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
  12773.            && ((flags & SEC_GROUP) != 0
  12774.                || strcmp (name, l->sec->name) == 0))
  12775.           || (l->sec->owner->flags & BFD_PLUGIN) != 0)
  12776.         {
  12777.           /* The section has already been linked.  See if we should
  12778.              issue a warning.  */
  12779.           if (!_bfd_handle_already_linked (sec, l, info))
  12780.             return FALSE;
  12781.  
  12782.           if (flags & SEC_GROUP)
  12783.             {
  12784.               asection *first = elf_next_in_group (sec);
  12785.               asection *s = first;
  12786.  
  12787.               while (s != NULL)
  12788.                 {
  12789.                   s->output_section = bfd_abs_section_ptr;
  12790.                   /* Record which group discards it.  */
  12791.                   s->kept_section = l->sec;
  12792.                   s = elf_next_in_group (s);
  12793.                   /* These lists are circular.  */
  12794.                   if (s == first)
  12795.                     break;
  12796.                 }
  12797.             }
  12798.  
  12799.           return TRUE;
  12800.         }
  12801.     }
  12802.  
  12803.   /* A single member comdat group section may be discarded by a
  12804.      linkonce section and vice versa.  */
  12805.   if ((flags & SEC_GROUP) != 0)
  12806.     {
  12807.       asection *first = elf_next_in_group (sec);
  12808.  
  12809.       if (first != NULL && elf_next_in_group (first) == first)
  12810.         /* Check this single member group against linkonce sections.  */
  12811.         for (l = already_linked_list->entry; l != NULL; l = l->next)
  12812.           if ((l->sec->flags & SEC_GROUP) == 0
  12813.               && bfd_elf_match_symbols_in_sections (l->sec, first, info))
  12814.             {
  12815.               first->output_section = bfd_abs_section_ptr;
  12816.               first->kept_section = l->sec;
  12817.               sec->output_section = bfd_abs_section_ptr;
  12818.               break;
  12819.             }
  12820.     }
  12821.   else
  12822.     /* Check this linkonce section against single member groups.  */
  12823.     for (l = already_linked_list->entry; l != NULL; l = l->next)
  12824.       if (l->sec->flags & SEC_GROUP)
  12825.         {
  12826.           asection *first = elf_next_in_group (l->sec);
  12827.  
  12828.           if (first != NULL
  12829.               && elf_next_in_group (first) == first
  12830.               && bfd_elf_match_symbols_in_sections (first, sec, info))
  12831.             {
  12832.               sec->output_section = bfd_abs_section_ptr;
  12833.               sec->kept_section = first;
  12834.               break;
  12835.             }
  12836.         }
  12837.  
  12838.   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
  12839.      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
  12840.      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
  12841.      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
  12842.      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
  12843.      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
  12844.      `.gnu.linkonce.t.F' section from a different bfd not requiring any
  12845.      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
  12846.      The reverse order cannot happen as there is never a bfd with only the
  12847.      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
  12848.      matter as here were are looking only for cross-bfd sections.  */
  12849.  
  12850.   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
  12851.     for (l = already_linked_list->entry; l != NULL; l = l->next)
  12852.       if ((l->sec->flags & SEC_GROUP) == 0
  12853.           && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
  12854.         {
  12855.           if (abfd != l->sec->owner)
  12856.             sec->output_section = bfd_abs_section_ptr;
  12857.           break;
  12858.         }
  12859.  
  12860.   /* This is the first section with this name.  Record it.  */
  12861.   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  12862.     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  12863.   return sec->output_section == bfd_abs_section_ptr;
  12864. }
  12865.  
  12866. bfd_boolean
  12867. _bfd_elf_common_definition (Elf_Internal_Sym *sym)
  12868. {
  12869.   return sym->st_shndx == SHN_COMMON;
  12870. }
  12871.  
  12872. unsigned int
  12873. _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
  12874. {
  12875.   return SHN_COMMON;
  12876. }
  12877.  
  12878. asection *
  12879. _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
  12880. {
  12881.   return bfd_com_section_ptr;
  12882. }
  12883.  
  12884. bfd_vma
  12885. _bfd_elf_default_got_elt_size (bfd *abfd,
  12886.                                struct bfd_link_info *info ATTRIBUTE_UNUSED,
  12887.                                struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
  12888.                                bfd *ibfd ATTRIBUTE_UNUSED,
  12889.                                unsigned long symndx ATTRIBUTE_UNUSED)
  12890. {
  12891.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12892.   return bed->s->arch_size / 8;
  12893. }
  12894.  
  12895. /* Routines to support the creation of dynamic relocs.  */
  12896.  
  12897. /* Returns the name of the dynamic reloc section associated with SEC.  */
  12898.  
  12899. static const char *
  12900. get_dynamic_reloc_section_name (bfd *       abfd,
  12901.                                 asection *  sec,
  12902.                                 bfd_boolean is_rela)
  12903. {
  12904.   char *name;
  12905.   const char *old_name = bfd_get_section_name (NULL, sec);
  12906.   const char *prefix = is_rela ? ".rela" : ".rel";
  12907.  
  12908.   if (old_name == NULL)
  12909.     return NULL;
  12910.  
  12911.   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
  12912.   sprintf (name, "%s%s", prefix, old_name);
  12913.  
  12914.   return name;
  12915. }
  12916.  
  12917. /* Returns the dynamic reloc section associated with SEC.
  12918.    If necessary compute the name of the dynamic reloc section based
  12919.    on SEC's name (looked up in ABFD's string table) and the setting
  12920.    of IS_RELA.  */
  12921.  
  12922. asection *
  12923. _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
  12924.                                     asection *  sec,
  12925.                                     bfd_boolean is_rela)
  12926. {
  12927.   asection * reloc_sec = elf_section_data (sec)->sreloc;
  12928.  
  12929.   if (reloc_sec == NULL)
  12930.     {
  12931.       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  12932.  
  12933.       if (name != NULL)
  12934.         {
  12935.           reloc_sec = bfd_get_linker_section (abfd, name);
  12936.  
  12937.           if (reloc_sec != NULL)
  12938.             elf_section_data (sec)->sreloc = reloc_sec;
  12939.         }
  12940.     }
  12941.  
  12942.   return reloc_sec;
  12943. }
  12944.  
  12945. /* Returns the dynamic reloc section associated with SEC.  If the
  12946.    section does not exist it is created and attached to the DYNOBJ
  12947.    bfd and stored in the SRELOC field of SEC's elf_section_data
  12948.    structure.
  12949.  
  12950.    ALIGNMENT is the alignment for the newly created section and
  12951.    IS_RELA defines whether the name should be .rela.<SEC's name>
  12952.    or .rel.<SEC's name>.  The section name is looked up in the
  12953.    string table associated with ABFD.  */
  12954.  
  12955. asection *
  12956. _bfd_elf_make_dynamic_reloc_section (asection *         sec,
  12957.                                      bfd *              dynobj,
  12958.                                      unsigned int       alignment,
  12959.                                      bfd *              abfd,
  12960.                                      bfd_boolean        is_rela)
  12961. {
  12962.   asection * reloc_sec = elf_section_data (sec)->sreloc;
  12963.  
  12964.   if (reloc_sec == NULL)
  12965.     {
  12966.       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  12967.  
  12968.       if (name == NULL)
  12969.         return NULL;
  12970.  
  12971.       reloc_sec = bfd_get_linker_section (dynobj, name);
  12972.  
  12973.       if (reloc_sec == NULL)
  12974.         {
  12975.           flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
  12976.                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  12977.           if ((sec->flags & SEC_ALLOC) != 0)
  12978.             flags |= SEC_ALLOC | SEC_LOAD;
  12979.  
  12980.           reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
  12981.           if (reloc_sec != NULL)
  12982.             {
  12983.               /* _bfd_elf_get_sec_type_attr chooses a section type by
  12984.                  name.  Override as it may be wrong, eg. for a user
  12985.                  section named "auto" we'll get ".relauto" which is
  12986.                  seen to be a .rela section.  */
  12987.               elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
  12988.               if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
  12989.                 reloc_sec = NULL;
  12990.             }
  12991.         }
  12992.  
  12993.       elf_section_data (sec)->sreloc = reloc_sec;
  12994.     }
  12995.  
  12996.   return reloc_sec;
  12997. }
  12998.  
  12999. /* Copy the ELF symbol type associated with a linker hash entry.  */
  13000. void
  13001. _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
  13002.     struct bfd_link_hash_entry * hdest,
  13003.     struct bfd_link_hash_entry * hsrc)
  13004. {
  13005.   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
  13006.   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
  13007.  
  13008.   ehdest->type = ehsrc->type;
  13009.   ehdest->target_internal = ehsrc->target_internal;
  13010. }
  13011.  
  13012. /* Append a RELA relocation REL to section S in BFD.  */
  13013.  
  13014. void
  13015. elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13016. {
  13017.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13018.   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  13019.   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
  13020.   bed->s->swap_reloca_out (abfd, rel, loc);
  13021. }
  13022.  
  13023. /* Append a REL relocation REL to section S in BFD.  */
  13024.  
  13025. void
  13026. elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13027. {
  13028.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13029.   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
  13030.   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
  13031.   bed->s->swap_reloc_out (abfd, rel, loc);
  13032. }
  13033.