Subversion Repositories Kolibri OS

Rev

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

  1. /* ELF linking support for BFD.
  2.    Copyright (C) 1995-2015 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 "bfd_stdint.h"
  24. #include "bfdlink.h"
  25. #include "libbfd.h"
  26. #define ARCH_SIZE 0
  27. #include "elf-bfd.h"
  28. #include "safe-ctype.h"
  29. #include "libiberty.h"
  30. #include "objalloc.h"
  31.  
  32. /* This struct is used to pass information to routines called via
  33.    elf_link_hash_traverse which must return failure.  */
  34.  
  35. struct elf_info_failed
  36. {
  37.   struct bfd_link_info *info;
  38.   bfd_boolean failed;
  39. };
  40.  
  41. /* This structure is used to pass information to
  42.    _bfd_elf_link_find_version_dependencies.  */
  43.  
  44. struct elf_find_verdep_info
  45. {
  46.   /* General link information.  */
  47.   struct bfd_link_info *info;
  48.   /* The number of dependencies.  */
  49.   unsigned int vers;
  50.   /* Whether we had a failure.  */
  51.   bfd_boolean failed;
  52. };
  53.  
  54. static bfd_boolean _bfd_elf_fix_symbol_flags
  55.   (struct elf_link_hash_entry *, struct elf_info_failed *);
  56.  
  57. asection *
  58. _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
  59.                              unsigned long r_symndx,
  60.                              bfd_boolean discard)
  61. {
  62.   if (r_symndx >= cookie->locsymcount
  63.       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  64.     {
  65.       struct elf_link_hash_entry *h;
  66.  
  67.       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
  68.  
  69.       while (h->root.type == bfd_link_hash_indirect
  70.              || h->root.type == bfd_link_hash_warning)
  71.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  72.  
  73.       if ((h->root.type == bfd_link_hash_defined
  74.            || h->root.type == bfd_link_hash_defweak)
  75.            && discarded_section (h->root.u.def.section))
  76.         return h->root.u.def.section;
  77.       else
  78.         return NULL;
  79.     }
  80.   else
  81.     {
  82.       /* It's not a relocation against a global symbol,
  83.          but it could be a relocation against a local
  84.          symbol for a discarded section.  */
  85.       asection *isec;
  86.       Elf_Internal_Sym *isym;
  87.  
  88.       /* Need to: get the symbol; get the section.  */
  89.       isym = &cookie->locsyms[r_symndx];
  90.       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
  91.       if (isec != NULL
  92.           && discard ? discarded_section (isec) : 1)
  93.         return isec;
  94.      }
  95.   return NULL;
  96. }
  97.  
  98. /* Define a symbol in a dynamic linkage section.  */
  99.  
  100. struct elf_link_hash_entry *
  101. _bfd_elf_define_linkage_sym (bfd *abfd,
  102.                              struct bfd_link_info *info,
  103.                              asection *sec,
  104.                              const char *name)
  105. {
  106.   struct elf_link_hash_entry *h;
  107.   struct bfd_link_hash_entry *bh;
  108.   const struct elf_backend_data *bed;
  109.  
  110.   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
  111.   if (h != NULL)
  112.     {
  113.       /* Zap symbol defined in an as-needed lib that wasn't linked.
  114.          This is a symptom of a larger problem:  Absolute symbols
  115.          defined in shared libraries can't be overridden, because we
  116.          lose the link to the bfd which is via the symbol section.  */
  117.       h->root.type = bfd_link_hash_new;
  118.     }
  119.  
  120.   bh = &h->root;
  121.   bed = get_elf_backend_data (abfd);
  122.   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
  123.                                          sec, 0, NULL, FALSE, bed->collect,
  124.                                          &bh))
  125.     return NULL;
  126.   h = (struct elf_link_hash_entry *) bh;
  127.   h->def_regular = 1;
  128.   h->non_elf = 0;
  129.   h->root.linker_def = 1;
  130.   h->type = STT_OBJECT;
  131.   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  132.     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  133.  
  134.   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  135.   return h;
  136. }
  137.  
  138. bfd_boolean
  139. _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  140. {
  141.   flagword flags;
  142.   asection *s;
  143.   struct elf_link_hash_entry *h;
  144.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  145.   struct elf_link_hash_table *htab = elf_hash_table (info);
  146.  
  147.   /* This function may be called more than once.  */
  148.   s = bfd_get_linker_section (abfd, ".got");
  149.   if (s != NULL)
  150.     return TRUE;
  151.  
  152.   flags = bed->dynamic_sec_flags;
  153.  
  154.   s = bfd_make_section_anyway_with_flags (abfd,
  155.                                           (bed->rela_plts_and_copies_p
  156.                                            ? ".rela.got" : ".rel.got"),
  157.                                           (bed->dynamic_sec_flags
  158.                                            | SEC_READONLY));
  159.   if (s == NULL
  160.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  161.     return FALSE;
  162.   htab->srelgot = s;
  163.  
  164.   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  165.   if (s == NULL
  166.       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  167.     return FALSE;
  168.   htab->sgot = s;
  169.  
  170.   if (bed->want_got_plt)
  171.     {
  172.       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  173.       if (s == NULL
  174.           || !bfd_set_section_alignment (abfd, s,
  175.                                          bed->s->log_file_align))
  176.         return FALSE;
  177.       htab->sgotplt = s;
  178.     }
  179.  
  180.   /* The first bit of the global offset table is the header.  */
  181.   s->size += bed->got_header_size;
  182.  
  183.   if (bed->want_got_sym)
  184.     {
  185.       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  186.          (or .got.plt) section.  We don't do this in the linker script
  187.          because we don't want to define the symbol if we are not creating
  188.          a global offset table.  */
  189.       h = _bfd_elf_define_linkage_sym (abfd, info, s,
  190.                                        "_GLOBAL_OFFSET_TABLE_");
  191.       elf_hash_table (info)->hgot = h;
  192.       if (h == NULL)
  193.         return FALSE;
  194.     }
  195.  
  196.   return TRUE;
  197. }
  198. /* Create a strtab to hold the dynamic symbol names.  */
  199. static bfd_boolean
  200. _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
  201. {
  202.   struct elf_link_hash_table *hash_table;
  203.  
  204.   hash_table = elf_hash_table (info);
  205.   if (hash_table->dynobj == NULL)
  206.     hash_table->dynobj = abfd;
  207.  
  208.   if (hash_table->dynstr == NULL)
  209.     {
  210.       hash_table->dynstr = _bfd_elf_strtab_init ();
  211.       if (hash_table->dynstr == NULL)
  212.         return FALSE;
  213.     }
  214.   return TRUE;
  215. }
  216.  
  217. /* Create some sections which will be filled in with dynamic linking
  218.    information.  ABFD is an input file which requires dynamic sections
  219.    to be created.  The dynamic sections take up virtual memory space
  220.    when the final executable is run, so we need to create them before
  221.    addresses are assigned to the output sections.  We work out the
  222.    actual contents and size of these sections later.  */
  223.  
  224. bfd_boolean
  225. _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  226. {
  227.   flagword flags;
  228.   asection *s;
  229.   const struct elf_backend_data *bed;
  230.   struct elf_link_hash_entry *h;
  231.  
  232.   if (! is_elf_hash_table (info->hash))
  233.     return FALSE;
  234.  
  235.   if (elf_hash_table (info)->dynamic_sections_created)
  236.     return TRUE;
  237.  
  238.   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  239.     return FALSE;
  240.  
  241.   abfd = elf_hash_table (info)->dynobj;
  242.   bed = get_elf_backend_data (abfd);
  243.  
  244.   flags = bed->dynamic_sec_flags;
  245.  
  246.   /* A dynamically linked executable has a .interp section, but a
  247.      shared library does not.  */
  248.   if (bfd_link_executable (info) && !info->nointerp)
  249.     {
  250.       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
  251.                                               flags | SEC_READONLY);
  252.       if (s == NULL)
  253.         return FALSE;
  254.     }
  255.  
  256.   /* Create sections to hold version informations.  These are removed
  257.      if they are not needed.  */
  258.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
  259.                                           flags | SEC_READONLY);
  260.   if (s == NULL
  261.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  262.     return FALSE;
  263.  
  264.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
  265.                                           flags | SEC_READONLY);
  266.   if (s == NULL
  267.       || ! bfd_set_section_alignment (abfd, s, 1))
  268.     return FALSE;
  269.  
  270.   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
  271.                                           flags | SEC_READONLY);
  272.   if (s == NULL
  273.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  274.     return FALSE;
  275.  
  276.   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
  277.                                           flags | SEC_READONLY);
  278.   if (s == NULL
  279.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  280.     return FALSE;
  281.   elf_hash_table (info)->dynsym = s;
  282.  
  283.   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
  284.                                           flags | SEC_READONLY);
  285.   if (s == NULL)
  286.     return FALSE;
  287.  
  288.   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
  289.   if (s == NULL
  290.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  291.     return FALSE;
  292.  
  293.   /* The special symbol _DYNAMIC is always set to the start of the
  294.      .dynamic section.  We could set _DYNAMIC in a linker script, but we
  295.      only want to define it if we are, in fact, creating a .dynamic
  296.      section.  We don't want to define it if there is no .dynamic
  297.      section, since on some ELF platforms the start up code examines it
  298.      to decide how to initialize the process.  */
  299.   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
  300.   elf_hash_table (info)->hdynamic = h;
  301.   if (h == NULL)
  302.     return FALSE;
  303.  
  304.   if (info->emit_hash)
  305.     {
  306.       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
  307.                                               flags | SEC_READONLY);
  308.       if (s == NULL
  309.           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  310.         return FALSE;
  311.       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
  312.     }
  313.  
  314.   if (info->emit_gnu_hash)
  315.     {
  316.       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
  317.                                               flags | SEC_READONLY);
  318.       if (s == NULL
  319.           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  320.         return FALSE;
  321.       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
  322.          4 32-bit words followed by variable count of 64-bit words, then
  323.          variable count of 32-bit words.  */
  324.       if (bed->s->arch_size == 64)
  325.         elf_section_data (s)->this_hdr.sh_entsize = 0;
  326.       else
  327.         elf_section_data (s)->this_hdr.sh_entsize = 4;
  328.     }
  329.  
  330.   /* Let the backend create the rest of the sections.  This lets the
  331.      backend set the right flags.  The backend will normally create
  332.      the .got and .plt sections.  */
  333.   if (bed->elf_backend_create_dynamic_sections == NULL
  334.       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
  335.     return FALSE;
  336.  
  337.   elf_hash_table (info)->dynamic_sections_created = TRUE;
  338.  
  339.   return TRUE;
  340. }
  341.  
  342. /* Create dynamic sections when linking against a dynamic object.  */
  343.  
  344. bfd_boolean
  345. _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  346. {
  347.   flagword flags, pltflags;
  348.   struct elf_link_hash_entry *h;
  349.   asection *s;
  350.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  351.   struct elf_link_hash_table *htab = elf_hash_table (info);
  352.  
  353.   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  354.      .rel[a].bss sections.  */
  355.   flags = bed->dynamic_sec_flags;
  356.  
  357.   pltflags = flags;
  358.   if (bed->plt_not_loaded)
  359.     /* We do not clear SEC_ALLOC here because we still want the OS to
  360.        allocate space for the section; it's just that there's nothing
  361.        to read in from the object file.  */
  362.     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  363.   else
  364.     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
  365.   if (bed->plt_readonly)
  366.     pltflags |= SEC_READONLY;
  367.  
  368.   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  369.   if (s == NULL
  370.       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
  371.     return FALSE;
  372.   htab->splt = s;
  373.  
  374.   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  375.      .plt section.  */
  376.   if (bed->want_plt_sym)
  377.     {
  378.       h = _bfd_elf_define_linkage_sym (abfd, info, s,
  379.                                        "_PROCEDURE_LINKAGE_TABLE_");
  380.       elf_hash_table (info)->hplt = h;
  381.       if (h == NULL)
  382.         return FALSE;
  383.     }
  384.  
  385.   s = bfd_make_section_anyway_with_flags (abfd,
  386.                                           (bed->rela_plts_and_copies_p
  387.                                            ? ".rela.plt" : ".rel.plt"),
  388.                                           flags | SEC_READONLY);
  389.   if (s == NULL
  390.       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  391.     return FALSE;
  392.   htab->srelplt = s;
  393.  
  394.   if (! _bfd_elf_create_got_section (abfd, info))
  395.     return FALSE;
  396.  
  397.   if (bed->want_dynbss)
  398.     {
  399.       /* The .dynbss section is a place to put symbols which are defined
  400.          by dynamic objects, are referenced by regular objects, and are
  401.          not functions.  We must allocate space for them in the process
  402.          image and use a R_*_COPY reloc to tell the dynamic linker to
  403.          initialize them at run time.  The linker script puts the .dynbss
  404.          section into the .bss section of the final image.  */
  405.       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  406.                                               (SEC_ALLOC | SEC_LINKER_CREATED));
  407.       if (s == NULL)
  408.         return FALSE;
  409.  
  410.       /* The .rel[a].bss section holds copy relocs.  This section is not
  411.          normally needed.  We need to create it here, though, so that the
  412.          linker will map it to an output section.  We can't just create it
  413.          only if we need it, because we will not know whether we need it
  414.          until we have seen all the input files, and the first time the
  415.          main linker code calls BFD after examining all the input files
  416.          (size_dynamic_sections) the input sections have already been
  417.          mapped to the output sections.  If the section turns out not to
  418.          be needed, we can discard it later.  We will never need this
  419.          section when generating a shared object, since they do not use
  420.          copy relocs.  */
  421.       if (! bfd_link_pic (info))
  422.         {
  423.           s = bfd_make_section_anyway_with_flags (abfd,
  424.                                                   (bed->rela_plts_and_copies_p
  425.                                                    ? ".rela.bss" : ".rel.bss"),
  426.                                                   flags | SEC_READONLY);
  427.           if (s == NULL
  428.               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  429.             return FALSE;
  430.         }
  431.     }
  432.  
  433.   return TRUE;
  434. }
  435. /* Record a new dynamic symbol.  We record the dynamic symbols as we
  436.    read the input files, since we need to have a list of all of them
  437.    before we can determine the final sizes of the output sections.
  438.    Note that we may actually call this function even though we are not
  439.    going to output any dynamic symbols; in some cases we know that a
  440.    symbol should be in the dynamic symbol table, but only if there is
  441.    one.  */
  442.  
  443. bfd_boolean
  444. bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
  445.                                     struct elf_link_hash_entry *h)
  446. {
  447.   if (h->dynindx == -1)
  448.     {
  449.       struct elf_strtab_hash *dynstr;
  450.       char *p;
  451.       const char *name;
  452.       bfd_size_type indx;
  453.  
  454.       /* XXX: The ABI draft says the linker must turn hidden and
  455.          internal symbols into STB_LOCAL symbols when producing the
  456.          DSO. However, if ld.so honors st_other in the dynamic table,
  457.          this would not be necessary.  */
  458.       switch (ELF_ST_VISIBILITY (h->other))
  459.         {
  460.         case STV_INTERNAL:
  461.         case STV_HIDDEN:
  462.           if (h->root.type != bfd_link_hash_undefined
  463.               && h->root.type != bfd_link_hash_undefweak)
  464.             {
  465.               h->forced_local = 1;
  466.               if (!elf_hash_table (info)->is_relocatable_executable)
  467.                 return TRUE;
  468.             }
  469.  
  470.         default:
  471.           break;
  472.         }
  473.  
  474.       h->dynindx = elf_hash_table (info)->dynsymcount;
  475.       ++elf_hash_table (info)->dynsymcount;
  476.  
  477.       dynstr = elf_hash_table (info)->dynstr;
  478.       if (dynstr == NULL)
  479.         {
  480.           /* Create a strtab to hold the dynamic symbol names.  */
  481.           elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  482.           if (dynstr == NULL)
  483.             return FALSE;
  484.         }
  485.  
  486.       /* We don't put any version information in the dynamic string
  487.          table.  */
  488.       name = h->root.root.string;
  489.       p = strchr (name, ELF_VER_CHR);
  490.       if (p != NULL)
  491.         /* We know that the p points into writable memory.  In fact,
  492.            there are only a few symbols that have read-only names, being
  493.            those like _GLOBAL_OFFSET_TABLE_ that are created specially
  494.            by the backends.  Most symbols will have names pointing into
  495.            an ELF string table read from a file, or to objalloc memory.  */
  496.         *p = 0;
  497.  
  498.       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
  499.  
  500.       if (p != NULL)
  501.         *p = ELF_VER_CHR;
  502.  
  503.       if (indx == (bfd_size_type) -1)
  504.         return FALSE;
  505.       h->dynstr_index = indx;
  506.     }
  507.  
  508.   return TRUE;
  509. }
  510. /* Mark a symbol dynamic.  */
  511.  
  512. static void
  513. bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
  514.                                   struct elf_link_hash_entry *h,
  515.                                   Elf_Internal_Sym *sym)
  516. {
  517.   struct bfd_elf_dynamic_list *d = info->dynamic_list;
  518.  
  519.   /* It may be called more than once on the same H.  */
  520.   if(h->dynamic || bfd_link_relocatable (info))
  521.     return;
  522.  
  523.   if ((info->dynamic_data
  524.        && (h->type == STT_OBJECT
  525.            || (sym != NULL
  526.                && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
  527.       || (d != NULL
  528.           && h->root.type == bfd_link_hash_new
  529.           && (*d->match) (&d->head, NULL, h->root.root.string)))
  530.     h->dynamic = 1;
  531. }
  532.  
  533. /* Record an assignment to a symbol made by a linker script.  We need
  534.    this in case some dynamic object refers to this symbol.  */
  535.  
  536. bfd_boolean
  537. bfd_elf_record_link_assignment (bfd *output_bfd,
  538.                                 struct bfd_link_info *info,
  539.                                 const char *name,
  540.                                 bfd_boolean provide,
  541.                                 bfd_boolean hidden)
  542. {
  543.   struct elf_link_hash_entry *h, *hv;
  544.   struct elf_link_hash_table *htab;
  545.   const struct elf_backend_data *bed;
  546.  
  547.   if (!is_elf_hash_table (info->hash))
  548.     return TRUE;
  549.  
  550.   htab = elf_hash_table (info);
  551.   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
  552.   if (h == NULL)
  553.     return provide;
  554.  
  555.   switch (h->root.type)
  556.     {
  557.     case bfd_link_hash_defined:
  558.     case bfd_link_hash_defweak:
  559.     case bfd_link_hash_common:
  560.       break;
  561.     case bfd_link_hash_undefweak:
  562.     case bfd_link_hash_undefined:
  563.       /* Since we're defining the symbol, don't let it seem to have not
  564.          been defined.  record_dynamic_symbol and size_dynamic_sections
  565.          may depend on this.  */
  566.       h->root.type = bfd_link_hash_new;
  567.       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
  568.         bfd_link_repair_undef_list (&htab->root);
  569.       break;
  570.     case bfd_link_hash_new:
  571.       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
  572.       h->non_elf = 0;
  573.       break;
  574.     case bfd_link_hash_indirect:
  575.       /* We had a versioned symbol in a dynamic library.  We make the
  576.          the versioned symbol point to this one.  */
  577.       bed = get_elf_backend_data (output_bfd);
  578.       hv = h;
  579.       while (hv->root.type == bfd_link_hash_indirect
  580.              || hv->root.type == bfd_link_hash_warning)
  581.         hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
  582.       /* We don't need to update h->root.u since linker will set them
  583.          later.  */
  584.       h->root.type = bfd_link_hash_undefined;
  585.       hv->root.type = bfd_link_hash_indirect;
  586.       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
  587.       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
  588.       break;
  589.     case bfd_link_hash_warning:
  590.       abort ();
  591.       break;
  592.     }
  593.  
  594.   /* If this symbol is being provided by the linker script, and it is
  595.      currently defined by a dynamic object, but not by a regular
  596.      object, then mark it as undefined so that the generic linker will
  597.      force the correct value.  */
  598.   if (provide
  599.       && h->def_dynamic
  600.       && !h->def_regular)
  601.     h->root.type = bfd_link_hash_undefined;
  602.  
  603.   /* If this symbol is not being provided by the linker script, and it is
  604.      currently defined by a dynamic object, but not by a regular object,
  605.      then clear out any version information because the symbol will not be
  606.      associated with the dynamic object any more.  */
  607.   if (!provide
  608.       && h->def_dynamic
  609.       && !h->def_regular)
  610.     h->verinfo.verdef = NULL;
  611.  
  612.   h->def_regular = 1;
  613.  
  614.   if (hidden)
  615.     {
  616.       bed = get_elf_backend_data (output_bfd);
  617.       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  618.         h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  619.       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  620.     }
  621.  
  622.   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
  623.      and executables.  */
  624.   if (!bfd_link_relocatable (info)
  625.       && h->dynindx != -1
  626.       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  627.           || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
  628.     h->forced_local = 1;
  629.  
  630.   if ((h->def_dynamic
  631.        || h->ref_dynamic
  632.        || bfd_link_pic (info)
  633.        || (bfd_link_pde (info)
  634.            && elf_hash_table (info)->is_relocatable_executable))
  635.       && h->dynindx == -1)
  636.     {
  637.       if (! bfd_elf_link_record_dynamic_symbol (info, h))
  638.         return FALSE;
  639.  
  640.       /* If this is a weak defined symbol, and we know a corresponding
  641.          real symbol from the same dynamic object, make sure the real
  642.          symbol is also made into a dynamic symbol.  */
  643.       if (h->u.weakdef != NULL
  644.           && h->u.weakdef->dynindx == -1)
  645.         {
  646.           if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
  647.             return FALSE;
  648.         }
  649.     }
  650.  
  651.   return TRUE;
  652. }
  653.  
  654. /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
  655.    success, and 2 on a failure caused by attempting to record a symbol
  656.    in a discarded section, eg. a discarded link-once section symbol.  */
  657.  
  658. int
  659. bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
  660.                                           bfd *input_bfd,
  661.                                           long input_indx)
  662. {
  663.   bfd_size_type amt;
  664.   struct elf_link_local_dynamic_entry *entry;
  665.   struct elf_link_hash_table *eht;
  666.   struct elf_strtab_hash *dynstr;
  667.   unsigned long dynstr_index;
  668.   char *name;
  669.   Elf_External_Sym_Shndx eshndx;
  670.   char esym[sizeof (Elf64_External_Sym)];
  671.  
  672.   if (! is_elf_hash_table (info->hash))
  673.     return 0;
  674.  
  675.   /* See if the entry exists already.  */
  676.   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
  677.     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
  678.       return 1;
  679.  
  680.   amt = sizeof (*entry);
  681.   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
  682.   if (entry == NULL)
  683.     return 0;
  684.  
  685.   /* Go find the symbol, so that we can find it's name.  */
  686.   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
  687.                              1, input_indx, &entry->isym, esym, &eshndx))
  688.     {
  689.       bfd_release (input_bfd, entry);
  690.       return 0;
  691.     }
  692.  
  693.   if (entry->isym.st_shndx != SHN_UNDEF
  694.       && entry->isym.st_shndx < SHN_LORESERVE)
  695.     {
  696.       asection *s;
  697.  
  698.       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
  699.       if (s == NULL || bfd_is_abs_section (s->output_section))
  700.         {
  701.           /* We can still bfd_release here as nothing has done another
  702.              bfd_alloc.  We can't do this later in this function.  */
  703.           bfd_release (input_bfd, entry);
  704.           return 2;
  705.         }
  706.     }
  707.  
  708.   name = (bfd_elf_string_from_elf_section
  709.           (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
  710.            entry->isym.st_name));
  711.  
  712.   dynstr = elf_hash_table (info)->dynstr;
  713.   if (dynstr == NULL)
  714.     {
  715.       /* Create a strtab to hold the dynamic symbol names.  */
  716.       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  717.       if (dynstr == NULL)
  718.         return 0;
  719.     }
  720.  
  721.   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
  722.   if (dynstr_index == (unsigned long) -1)
  723.     return 0;
  724.   entry->isym.st_name = dynstr_index;
  725.  
  726.   eht = elf_hash_table (info);
  727.  
  728.   entry->next = eht->dynlocal;
  729.   eht->dynlocal = entry;
  730.   entry->input_bfd = input_bfd;
  731.   entry->input_indx = input_indx;
  732.   eht->dynsymcount++;
  733.  
  734.   /* Whatever binding the symbol had before, it's now local.  */
  735.   entry->isym.st_info
  736.     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
  737.  
  738.   /* The dynindx will be set at the end of size_dynamic_sections.  */
  739.  
  740.   return 1;
  741. }
  742.  
  743. /* Return the dynindex of a local dynamic symbol.  */
  744.  
  745. long
  746. _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
  747.                                     bfd *input_bfd,
  748.                                     long input_indx)
  749. {
  750.   struct elf_link_local_dynamic_entry *e;
  751.  
  752.   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  753.     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
  754.       return e->dynindx;
  755.   return -1;
  756. }
  757.  
  758. /* This function is used to renumber the dynamic symbols, if some of
  759.    them are removed because they are marked as local.  This is called
  760.    via elf_link_hash_traverse.  */
  761.  
  762. static bfd_boolean
  763. elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
  764.                                       void *data)
  765. {
  766.   size_t *count = (size_t *) data;
  767.  
  768.   if (h->forced_local)
  769.     return TRUE;
  770.  
  771.   if (h->dynindx != -1)
  772.     h->dynindx = ++(*count);
  773.  
  774.   return TRUE;
  775. }
  776.  
  777.  
  778. /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
  779.    STB_LOCAL binding.  */
  780.  
  781. static bfd_boolean
  782. elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
  783.                                             void *data)
  784. {
  785.   size_t *count = (size_t *) data;
  786.  
  787.   if (!h->forced_local)
  788.     return TRUE;
  789.  
  790.   if (h->dynindx != -1)
  791.     h->dynindx = ++(*count);
  792.  
  793.   return TRUE;
  794. }
  795.  
  796. /* Return true if the dynamic symbol for a given section should be
  797.    omitted when creating a shared library.  */
  798. bfd_boolean
  799. _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
  800.                                    struct bfd_link_info *info,
  801.                                    asection *p)
  802. {
  803.   struct elf_link_hash_table *htab;
  804.   asection *ip;
  805.  
  806.   switch (elf_section_data (p)->this_hdr.sh_type)
  807.     {
  808.     case SHT_PROGBITS:
  809.     case SHT_NOBITS:
  810.       /* If sh_type is yet undecided, assume it could be
  811.          SHT_PROGBITS/SHT_NOBITS.  */
  812.     case SHT_NULL:
  813.       htab = elf_hash_table (info);
  814.       if (p == htab->tls_sec)
  815.         return FALSE;
  816.  
  817.       if (htab->text_index_section != NULL)
  818.         return p != htab->text_index_section && p != htab->data_index_section;
  819.  
  820.       return (htab->dynobj != NULL
  821.               && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
  822.               && ip->output_section == p);
  823.  
  824.       /* There shouldn't be section relative relocations
  825.          against any other section.  */
  826.     default:
  827.       return TRUE;
  828.     }
  829. }
  830.  
  831. /* Assign dynsym indices.  In a shared library we generate a section
  832.    symbol for each output section, which come first.  Next come symbols
  833.    which have been forced to local binding.  Then all of the back-end
  834.    allocated local dynamic syms, followed by the rest of the global
  835.    symbols.  */
  836.  
  837. static unsigned long
  838. _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
  839.                                 struct bfd_link_info *info,
  840.                                 unsigned long *section_sym_count)
  841. {
  842.   unsigned long dynsymcount = 0;
  843.  
  844.   if (bfd_link_pic (info)
  845.       || elf_hash_table (info)->is_relocatable_executable)
  846.     {
  847.       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  848.       asection *p;
  849.       for (p = output_bfd->sections; p ; p = p->next)
  850.         if ((p->flags & SEC_EXCLUDE) == 0
  851.             && (p->flags & SEC_ALLOC) != 0
  852.             && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
  853.           elf_section_data (p)->dynindx = ++dynsymcount;
  854.         else
  855.           elf_section_data (p)->dynindx = 0;
  856.     }
  857.   *section_sym_count = dynsymcount;
  858.  
  859.   elf_link_hash_traverse (elf_hash_table (info),
  860.                           elf_link_renumber_local_hash_table_dynsyms,
  861.                           &dynsymcount);
  862.  
  863.   if (elf_hash_table (info)->dynlocal)
  864.     {
  865.       struct elf_link_local_dynamic_entry *p;
  866.       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
  867.         p->dynindx = ++dynsymcount;
  868.     }
  869.  
  870.   elf_link_hash_traverse (elf_hash_table (info),
  871.                           elf_link_renumber_hash_table_dynsyms,
  872.                           &dynsymcount);
  873.  
  874.   /* There is an unused NULL entry at the head of the table which
  875.      we must account for in our count.  Unless there weren't any
  876.      symbols, which means we'll have no table at all.  */
  877.   if (dynsymcount != 0)
  878.     ++dynsymcount;
  879.  
  880.   elf_hash_table (info)->dynsymcount = dynsymcount;
  881.   return dynsymcount;
  882. }
  883.  
  884. /* Merge st_other field.  */
  885.  
  886. static void
  887. elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
  888.                     const Elf_Internal_Sym *isym, asection *sec,
  889.                     bfd_boolean definition, bfd_boolean dynamic)
  890. {
  891.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  892.  
  893.   /* If st_other has a processor-specific meaning, specific
  894.      code might be needed here.  */
  895.   if (bed->elf_backend_merge_symbol_attribute)
  896.     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
  897.                                                 dynamic);
  898.  
  899.   if (!dynamic)
  900.     {
  901.       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
  902.       unsigned hvis = ELF_ST_VISIBILITY (h->other);
  903.  
  904.       /* Keep the most constraining visibility.  Leave the remainder
  905.          of the st_other field to elf_backend_merge_symbol_attribute.  */
  906.       if (symvis - 1 < hvis - 1)
  907.         h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
  908.     }
  909.   else if (definition
  910.            && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
  911.            && (sec->flags & SEC_READONLY) == 0)
  912.     h->protected_def = 1;
  913. }
  914.  
  915. /* This function is called when we want to merge a new symbol with an
  916.    existing symbol.  It handles the various cases which arise when we
  917.    find a definition in a dynamic object, or when there is already a
  918.    definition in a dynamic object.  The new symbol is described by
  919.    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
  920.    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
  921.    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
  922.    of an old common symbol.  We set OVERRIDE if the old symbol is
  923.    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
  924.    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
  925.    to change.  By OK to change, we mean that we shouldn't warn if the
  926.    type or size does change.  */
  927.  
  928. static bfd_boolean
  929. _bfd_elf_merge_symbol (bfd *abfd,
  930.                        struct bfd_link_info *info,
  931.                        const char *name,
  932.                        Elf_Internal_Sym *sym,
  933.                        asection **psec,
  934.                        bfd_vma *pvalue,
  935.                        struct elf_link_hash_entry **sym_hash,
  936.                        bfd **poldbfd,
  937.                        bfd_boolean *pold_weak,
  938.                        unsigned int *pold_alignment,
  939.                        bfd_boolean *skip,
  940.                        bfd_boolean *override,
  941.                        bfd_boolean *type_change_ok,
  942.                        bfd_boolean *size_change_ok,
  943.                        bfd_boolean *matched)
  944. {
  945.   asection *sec, *oldsec;
  946.   struct elf_link_hash_entry *h;
  947.   struct elf_link_hash_entry *hi;
  948.   struct elf_link_hash_entry *flip;
  949.   int bind;
  950.   bfd *oldbfd;
  951.   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
  952.   bfd_boolean newweak, oldweak, newfunc, oldfunc;
  953.   const struct elf_backend_data *bed;
  954.   char *new_version;
  955.  
  956.   *skip = FALSE;
  957.   *override = FALSE;
  958.  
  959.   sec = *psec;
  960.   bind = ELF_ST_BIND (sym->st_info);
  961.  
  962.   if (! bfd_is_und_section (sec))
  963.     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
  964.   else
  965.     h = ((struct elf_link_hash_entry *)
  966.          bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
  967.   if (h == NULL)
  968.     return FALSE;
  969.   *sym_hash = h;
  970.  
  971.   bed = get_elf_backend_data (abfd);
  972.  
  973.   /* NEW_VERSION is the symbol version of the new symbol.  */
  974.   if (h->versioned != unversioned)
  975.     {
  976.       /* Symbol version is unknown or versioned.  */
  977.       new_version = strrchr (name, ELF_VER_CHR);
  978.       if (new_version)
  979.         {
  980.           if (h->versioned == unknown)
  981.             {
  982.               if (new_version > name && new_version[-1] != ELF_VER_CHR)
  983.                 h->versioned = versioned_hidden;
  984.               else
  985.                 h->versioned = versioned;
  986.             }
  987.           new_version += 1;
  988.           if (new_version[0] == '\0')
  989.             new_version = NULL;
  990.         }
  991.       else
  992.         h->versioned = unversioned;
  993.     }
  994.   else
  995.     new_version = NULL;
  996.  
  997.   /* For merging, we only care about real symbols.  But we need to make
  998.      sure that indirect symbol dynamic flags are updated.  */
  999.   hi = h;
  1000.   while (h->root.type == bfd_link_hash_indirect
  1001.          || h->root.type == bfd_link_hash_warning)
  1002.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1003.  
  1004.   if (!*matched)
  1005.     {
  1006.       if (hi == h || h->root.type == bfd_link_hash_new)
  1007.         *matched = TRUE;
  1008.       else
  1009.         {
  1010.           /* OLD_HIDDEN is true if the existing symbol is only visible
  1011.              to the symbol with the same symbol version.  NEW_HIDDEN is
  1012.              true if the new symbol is only visible to the symbol with
  1013.              the same symbol version.  */
  1014.           bfd_boolean old_hidden = h->versioned == versioned_hidden;
  1015.           bfd_boolean new_hidden = hi->versioned == versioned_hidden;
  1016.           if (!old_hidden && !new_hidden)
  1017.             /* The new symbol matches the existing symbol if both
  1018.                aren't hidden.  */
  1019.             *matched = TRUE;
  1020.           else
  1021.             {
  1022.               /* OLD_VERSION is the symbol version of the existing
  1023.                  symbol. */
  1024.               char *old_version;
  1025.  
  1026.               if (h->versioned >= versioned)
  1027.                 old_version = strrchr (h->root.root.string,
  1028.                                        ELF_VER_CHR) + 1;
  1029.               else
  1030.                  old_version = NULL;
  1031.  
  1032.               /* The new symbol matches the existing symbol if they
  1033.                  have the same symbol version.  */
  1034.               *matched = (old_version == new_version
  1035.                           || (old_version != NULL
  1036.                               && new_version != NULL
  1037.                               && strcmp (old_version, new_version) == 0));
  1038.             }
  1039.         }
  1040.     }
  1041.  
  1042.   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
  1043.      existing symbol.  */
  1044.  
  1045.   oldbfd = NULL;
  1046.   oldsec = NULL;
  1047.   switch (h->root.type)
  1048.     {
  1049.     default:
  1050.       break;
  1051.  
  1052.     case bfd_link_hash_undefined:
  1053.     case bfd_link_hash_undefweak:
  1054.       oldbfd = h->root.u.undef.abfd;
  1055.       break;
  1056.  
  1057.     case bfd_link_hash_defined:
  1058.     case bfd_link_hash_defweak:
  1059.       oldbfd = h->root.u.def.section->owner;
  1060.       oldsec = h->root.u.def.section;
  1061.       break;
  1062.  
  1063.     case bfd_link_hash_common:
  1064.       oldbfd = h->root.u.c.p->section->owner;
  1065.       oldsec = h->root.u.c.p->section;
  1066.       if (pold_alignment)
  1067.         *pold_alignment = h->root.u.c.p->alignment_power;
  1068.       break;
  1069.     }
  1070.   if (poldbfd && *poldbfd == NULL)
  1071.     *poldbfd = oldbfd;
  1072.  
  1073.   /* Differentiate strong and weak symbols.  */
  1074.   newweak = bind == STB_WEAK;
  1075.   oldweak = (h->root.type == bfd_link_hash_defweak
  1076.              || h->root.type == bfd_link_hash_undefweak);
  1077.   if (pold_weak)
  1078.     *pold_weak = oldweak;
  1079.  
  1080.   /* This code is for coping with dynamic objects, and is only useful
  1081.      if we are doing an ELF link.  */
  1082.   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
  1083.     return TRUE;
  1084.  
  1085.   /* We have to check it for every instance since the first few may be
  1086.      references and not all compilers emit symbol type for undefined
  1087.      symbols.  */
  1088.   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
  1089.  
  1090.   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
  1091.      respectively, is from a dynamic object.  */
  1092.  
  1093.   newdyn = (abfd->flags & DYNAMIC) != 0;
  1094.  
  1095.   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
  1096.      syms and defined syms in dynamic libraries respectively.
  1097.      ref_dynamic on the other hand can be set for a symbol defined in
  1098.      a dynamic library, and def_dynamic may not be set;  When the
  1099.      definition in a dynamic lib is overridden by a definition in the
  1100.      executable use of the symbol in the dynamic lib becomes a
  1101.      reference to the executable symbol.  */
  1102.   if (newdyn)
  1103.     {
  1104.       if (bfd_is_und_section (sec))
  1105.         {
  1106.           if (bind != STB_WEAK)
  1107.             {
  1108.               h->ref_dynamic_nonweak = 1;
  1109.               hi->ref_dynamic_nonweak = 1;
  1110.             }
  1111.         }
  1112.       else
  1113.         {
  1114.           /* Update the existing symbol only if they match. */
  1115.           if (*matched)
  1116.             h->dynamic_def = 1;
  1117.           hi->dynamic_def = 1;
  1118.         }
  1119.     }
  1120.  
  1121.   /* If we just created the symbol, mark it as being an ELF symbol.
  1122.      Other than that, there is nothing to do--there is no merge issue
  1123.      with a newly defined symbol--so we just return.  */
  1124.  
  1125.   if (h->root.type == bfd_link_hash_new)
  1126.     {
  1127.       h->non_elf = 0;
  1128.       return TRUE;
  1129.     }
  1130.  
  1131.   /* In cases involving weak versioned symbols, we may wind up trying
  1132.      to merge a symbol with itself.  Catch that here, to avoid the
  1133.      confusion that results if we try to override a symbol with
  1134.      itself.  The additional tests catch cases like
  1135.      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
  1136.      dynamic object, which we do want to handle here.  */
  1137.   if (abfd == oldbfd
  1138.       && (newweak || oldweak)
  1139.       && ((abfd->flags & DYNAMIC) == 0
  1140.           || !h->def_regular))
  1141.     return TRUE;
  1142.  
  1143.   olddyn = FALSE;
  1144.   if (oldbfd != NULL)
  1145.     olddyn = (oldbfd->flags & DYNAMIC) != 0;
  1146.   else if (oldsec != NULL)
  1147.     {
  1148.       /* This handles the special SHN_MIPS_{TEXT,DATA} section
  1149.          indices used by MIPS ELF.  */
  1150.       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
  1151.     }
  1152.  
  1153.   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
  1154.      respectively, appear to be a definition rather than reference.  */
  1155.  
  1156.   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
  1157.  
  1158.   olddef = (h->root.type != bfd_link_hash_undefined
  1159.             && h->root.type != bfd_link_hash_undefweak
  1160.             && h->root.type != bfd_link_hash_common);
  1161.  
  1162.   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
  1163.      respectively, appear to be a function.  */
  1164.  
  1165.   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1166.              && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
  1167.  
  1168.   oldfunc = (h->type != STT_NOTYPE
  1169.              && bed->is_function_type (h->type));
  1170.  
  1171.   /* When we try to create a default indirect symbol from the dynamic
  1172.      definition with the default version, we skip it if its type and
  1173.      the type of existing regular definition mismatch.  */
  1174.   if (pold_alignment == NULL
  1175.       && newdyn
  1176.       && newdef
  1177.       && !olddyn
  1178.       && (((olddef || h->root.type == bfd_link_hash_common)
  1179.            && ELF_ST_TYPE (sym->st_info) != h->type
  1180.            && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1181.            && h->type != STT_NOTYPE
  1182.            && !(newfunc && oldfunc))
  1183.           || (olddef
  1184.               && ((h->type == STT_GNU_IFUNC)
  1185.                   != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
  1186.     {
  1187.       *skip = TRUE;
  1188.       return TRUE;
  1189.     }
  1190.  
  1191.   /* Check TLS symbols.  We don't check undefined symbols introduced
  1192.      by "ld -u" which have no type (and oldbfd NULL), and we don't
  1193.      check symbols from plugins because they also have no type.  */
  1194.   if (oldbfd != NULL
  1195.       && (oldbfd->flags & BFD_PLUGIN) == 0
  1196.       && (abfd->flags & BFD_PLUGIN) == 0
  1197.       && ELF_ST_TYPE (sym->st_info) != h->type
  1198.       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
  1199.     {
  1200.       bfd *ntbfd, *tbfd;
  1201.       bfd_boolean ntdef, tdef;
  1202.       asection *ntsec, *tsec;
  1203.  
  1204.       if (h->type == STT_TLS)
  1205.         {
  1206.           ntbfd = abfd;
  1207.           ntsec = sec;
  1208.           ntdef = newdef;
  1209.           tbfd = oldbfd;
  1210.           tsec = oldsec;
  1211.           tdef = olddef;
  1212.         }
  1213.       else
  1214.         {
  1215.           ntbfd = oldbfd;
  1216.           ntsec = oldsec;
  1217.           ntdef = olddef;
  1218.           tbfd = abfd;
  1219.           tsec = sec;
  1220.           tdef = newdef;
  1221.         }
  1222.  
  1223.       if (tdef && ntdef)
  1224.         (*_bfd_error_handler)
  1225.           (_("%s: TLS definition in %B section %A "
  1226.              "mismatches non-TLS definition in %B section %A"),
  1227.            tbfd, tsec, ntbfd, ntsec, h->root.root.string);
  1228.       else if (!tdef && !ntdef)
  1229.         (*_bfd_error_handler)
  1230.           (_("%s: TLS reference in %B "
  1231.              "mismatches non-TLS reference in %B"),
  1232.            tbfd, ntbfd, h->root.root.string);
  1233.       else if (tdef)
  1234.         (*_bfd_error_handler)
  1235.           (_("%s: TLS definition in %B section %A "
  1236.              "mismatches non-TLS reference in %B"),
  1237.            tbfd, tsec, ntbfd, h->root.root.string);
  1238.       else
  1239.         (*_bfd_error_handler)
  1240.           (_("%s: TLS reference in %B "
  1241.              "mismatches non-TLS definition in %B section %A"),
  1242.            tbfd, ntbfd, ntsec, h->root.root.string);
  1243.  
  1244.       bfd_set_error (bfd_error_bad_value);
  1245.       return FALSE;
  1246.     }
  1247.  
  1248.   /* If the old symbol has non-default visibility, we ignore the new
  1249.      definition from a dynamic object.  */
  1250.   if (newdyn
  1251.       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1252.       && !bfd_is_und_section (sec))
  1253.     {
  1254.       *skip = TRUE;
  1255.       /* Make sure this symbol is dynamic.  */
  1256.       h->ref_dynamic = 1;
  1257.       hi->ref_dynamic = 1;
  1258.       /* A protected symbol has external availability. Make sure it is
  1259.          recorded as dynamic.
  1260.  
  1261.          FIXME: Should we check type and size for protected symbol?  */
  1262.       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
  1263.         return bfd_elf_link_record_dynamic_symbol (info, h);
  1264.       else
  1265.         return TRUE;
  1266.     }
  1267.   else if (!newdyn
  1268.            && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
  1269.            && h->def_dynamic)
  1270.     {
  1271.       /* If the new symbol with non-default visibility comes from a
  1272.          relocatable file and the old definition comes from a dynamic
  1273.          object, we remove the old definition.  */
  1274.       if (hi->root.type == bfd_link_hash_indirect)
  1275.         {
  1276.           /* Handle the case where the old dynamic definition is
  1277.              default versioned.  We need to copy the symbol info from
  1278.              the symbol with default version to the normal one if it
  1279.              was referenced before.  */
  1280.           if (h->ref_regular)
  1281.             {
  1282.               hi->root.type = h->root.type;
  1283.               h->root.type = bfd_link_hash_indirect;
  1284.               (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
  1285.  
  1286.               h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1287.               if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1288.                 {
  1289.                   /* If the new symbol is hidden or internal, completely undo
  1290.                      any dynamic link state.  */
  1291.                   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1292.                   h->forced_local = 0;
  1293.                   h->ref_dynamic = 0;
  1294.                 }
  1295.               else
  1296.                 h->ref_dynamic = 1;
  1297.  
  1298.               h->def_dynamic = 0;
  1299.               /* FIXME: Should we check type and size for protected symbol?  */
  1300.               h->size = 0;
  1301.               h->type = 0;
  1302.  
  1303.               h = hi;
  1304.             }
  1305.           else
  1306.             h = hi;
  1307.         }
  1308.  
  1309.       /* If the old symbol was undefined before, then it will still be
  1310.          on the undefs list.  If the new symbol is undefined or
  1311.          common, we can't make it bfd_link_hash_new here, because new
  1312.          undefined or common symbols will be added to the undefs list
  1313.          by _bfd_generic_link_add_one_symbol.  Symbols may not be
  1314.          added twice to the undefs list.  Also, if the new symbol is
  1315.          undefweak then we don't want to lose the strong undef.  */
  1316.       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
  1317.         {
  1318.           h->root.type = bfd_link_hash_undefined;
  1319.           h->root.u.undef.abfd = abfd;
  1320.         }
  1321.       else
  1322.         {
  1323.           h->root.type = bfd_link_hash_new;
  1324.           h->root.u.undef.abfd = NULL;
  1325.         }
  1326.  
  1327.       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1328.         {
  1329.           /* If the new symbol is hidden or internal, completely undo
  1330.              any dynamic link state.  */
  1331.           (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1332.           h->forced_local = 0;
  1333.           h->ref_dynamic = 0;
  1334.         }
  1335.       else
  1336.         h->ref_dynamic = 1;
  1337.       h->def_dynamic = 0;
  1338.       /* FIXME: Should we check type and size for protected symbol?  */
  1339.       h->size = 0;
  1340.       h->type = 0;
  1341.       return TRUE;
  1342.     }
  1343.  
  1344.   /* If a new weak symbol definition comes from a regular file and the
  1345.      old symbol comes from a dynamic library, we treat the new one as
  1346.      strong.  Similarly, an old weak symbol definition from a regular
  1347.      file is treated as strong when the new symbol comes from a dynamic
  1348.      library.  Further, an old weak symbol from a dynamic library is
  1349.      treated as strong if the new symbol is from a dynamic library.
  1350.      This reflects the way glibc's ld.so works.
  1351.  
  1352.      Do this before setting *type_change_ok or *size_change_ok so that
  1353.      we warn properly when dynamic library symbols are overridden.  */
  1354.  
  1355.   if (newdef && !newdyn && olddyn)
  1356.     newweak = FALSE;
  1357.   if (olddef && newdyn)
  1358.     oldweak = FALSE;
  1359.  
  1360.   /* Allow changes between different types of function symbol.  */
  1361.   if (newfunc && oldfunc)
  1362.     *type_change_ok = TRUE;
  1363.  
  1364.   /* It's OK to change the type if either the existing symbol or the
  1365.      new symbol is weak.  A type change is also OK if the old symbol
  1366.      is undefined and the new symbol is defined.  */
  1367.  
  1368.   if (oldweak
  1369.       || newweak
  1370.       || (newdef
  1371.           && h->root.type == bfd_link_hash_undefined))
  1372.     *type_change_ok = TRUE;
  1373.  
  1374.   /* It's OK to change the size if either the existing symbol or the
  1375.      new symbol is weak, or if the old symbol is undefined.  */
  1376.  
  1377.   if (*type_change_ok
  1378.       || h->root.type == bfd_link_hash_undefined)
  1379.     *size_change_ok = TRUE;
  1380.  
  1381.   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
  1382.      symbol, respectively, appears to be a common symbol in a dynamic
  1383.      object.  If a symbol appears in an uninitialized section, and is
  1384.      not weak, and is not a function, then it may be a common symbol
  1385.      which was resolved when the dynamic object was created.  We want
  1386.      to treat such symbols specially, because they raise special
  1387.      considerations when setting the symbol size: if the symbol
  1388.      appears as a common symbol in a regular object, and the size in
  1389.      the regular object is larger, we must make sure that we use the
  1390.      larger size.  This problematic case can always be avoided in C,
  1391.      but it must be handled correctly when using Fortran shared
  1392.      libraries.
  1393.  
  1394.      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
  1395.      likewise for OLDDYNCOMMON and OLDDEF.
  1396.  
  1397.      Note that this test is just a heuristic, and that it is quite
  1398.      possible to have an uninitialized symbol in a shared object which
  1399.      is really a definition, rather than a common symbol.  This could
  1400.      lead to some minor confusion when the symbol really is a common
  1401.      symbol in some regular object.  However, I think it will be
  1402.      harmless.  */
  1403.  
  1404.   if (newdyn
  1405.       && newdef
  1406.       && !newweak
  1407.       && (sec->flags & SEC_ALLOC) != 0
  1408.       && (sec->flags & SEC_LOAD) == 0
  1409.       && sym->st_size > 0
  1410.       && !newfunc)
  1411.     newdyncommon = TRUE;
  1412.   else
  1413.     newdyncommon = FALSE;
  1414.  
  1415.   if (olddyn
  1416.       && olddef
  1417.       && h->root.type == bfd_link_hash_defined
  1418.       && h->def_dynamic
  1419.       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
  1420.       && (h->root.u.def.section->flags & SEC_LOAD) == 0
  1421.       && h->size > 0
  1422.       && !oldfunc)
  1423.     olddyncommon = TRUE;
  1424.   else
  1425.     olddyncommon = FALSE;
  1426.  
  1427.   /* We now know everything about the old and new symbols.  We ask the
  1428.      backend to check if we can merge them.  */
  1429.   if (bed->merge_symbol != NULL)
  1430.     {
  1431.       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
  1432.         return FALSE;
  1433.       sec = *psec;
  1434.     }
  1435.  
  1436.   /* If both the old and the new symbols look like common symbols in a
  1437.      dynamic object, set the size of the symbol to the larger of the
  1438.      two.  */
  1439.  
  1440.   if (olddyncommon
  1441.       && newdyncommon
  1442.       && sym->st_size != h->size)
  1443.     {
  1444.       /* Since we think we have two common symbols, issue a multiple
  1445.          common warning if desired.  Note that we only warn if the
  1446.          size is different.  If the size is the same, we simply let
  1447.          the old symbol override the new one as normally happens with
  1448.          symbols defined in dynamic objects.  */
  1449.  
  1450.       if (! ((*info->callbacks->multiple_common)
  1451.              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
  1452.         return FALSE;
  1453.  
  1454.       if (sym->st_size > h->size)
  1455.         h->size = sym->st_size;
  1456.  
  1457.       *size_change_ok = TRUE;
  1458.     }
  1459.  
  1460.   /* If we are looking at a dynamic object, and we have found a
  1461.      definition, we need to see if the symbol was already defined by
  1462.      some other object.  If so, we want to use the existing
  1463.      definition, and we do not want to report a multiple symbol
  1464.      definition error; we do this by clobbering *PSEC to be
  1465.      bfd_und_section_ptr.
  1466.  
  1467.      We treat a common symbol as a definition if the symbol in the
  1468.      shared library is a function, since common symbols always
  1469.      represent variables; this can cause confusion in principle, but
  1470.      any such confusion would seem to indicate an erroneous program or
  1471.      shared library.  We also permit a common symbol in a regular
  1472.      object to override a weak symbol in a shared object.  */
  1473.  
  1474.   if (newdyn
  1475.       && newdef
  1476.       && (olddef
  1477.           || (h->root.type == bfd_link_hash_common
  1478.               && (newweak || newfunc))))
  1479.     {
  1480.       *override = TRUE;
  1481.       newdef = FALSE;
  1482.       newdyncommon = FALSE;
  1483.  
  1484.       *psec = sec = bfd_und_section_ptr;
  1485.       *size_change_ok = TRUE;
  1486.  
  1487.       /* If we get here when the old symbol is a common symbol, then
  1488.          we are explicitly letting it override a weak symbol or
  1489.          function in a dynamic object, and we don't want to warn about
  1490.          a type change.  If the old symbol is a defined symbol, a type
  1491.          change warning may still be appropriate.  */
  1492.  
  1493.       if (h->root.type == bfd_link_hash_common)
  1494.         *type_change_ok = TRUE;
  1495.     }
  1496.  
  1497.   /* Handle the special case of an old common symbol merging with a
  1498.      new symbol which looks like a common symbol in a shared object.
  1499.      We change *PSEC and *PVALUE to make the new symbol look like a
  1500.      common symbol, and let _bfd_generic_link_add_one_symbol do the
  1501.      right thing.  */
  1502.  
  1503.   if (newdyncommon
  1504.       && h->root.type == bfd_link_hash_common)
  1505.     {
  1506.       *override = TRUE;
  1507.       newdef = FALSE;
  1508.       newdyncommon = FALSE;
  1509.       *pvalue = sym->st_size;
  1510.       *psec = sec = bed->common_section (oldsec);
  1511.       *size_change_ok = TRUE;
  1512.     }
  1513.  
  1514.   /* Skip weak definitions of symbols that are already defined.  */
  1515.   if (newdef && olddef && newweak)
  1516.     {
  1517.       /* Don't skip new non-IR weak syms.  */
  1518.       if (!(oldbfd != NULL
  1519.             && (oldbfd->flags & BFD_PLUGIN) != 0
  1520.             && (abfd->flags & BFD_PLUGIN) == 0))
  1521.         {
  1522.           newdef = FALSE;
  1523.           *skip = TRUE;
  1524.         }
  1525.  
  1526.       /* Merge st_other.  If the symbol already has a dynamic index,
  1527.          but visibility says it should not be visible, turn it into a
  1528.          local symbol.  */
  1529.       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
  1530.       if (h->dynindx != -1)
  1531.         switch (ELF_ST_VISIBILITY (h->other))
  1532.           {
  1533.           case STV_INTERNAL:
  1534.           case STV_HIDDEN:
  1535.             (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  1536.             break;
  1537.           }
  1538.     }
  1539.  
  1540.   /* If the old symbol is from a dynamic object, and the new symbol is
  1541.      a definition which is not from a dynamic object, then the new
  1542.      symbol overrides the old symbol.  Symbols from regular files
  1543.      always take precedence over symbols from dynamic objects, even if
  1544.      they are defined after the dynamic object in the link.
  1545.  
  1546.      As above, we again permit a common symbol in a regular object to
  1547.      override a definition in a shared object if the shared object
  1548.      symbol is a function or is weak.  */
  1549.  
  1550.   flip = NULL;
  1551.   if (!newdyn
  1552.       && (newdef
  1553.           || (bfd_is_com_section (sec)
  1554.               && (oldweak || oldfunc)))
  1555.       && olddyn
  1556.       && olddef
  1557.       && h->def_dynamic)
  1558.     {
  1559.       /* Change the hash table entry to undefined, and let
  1560.          _bfd_generic_link_add_one_symbol do the right thing with the
  1561.          new definition.  */
  1562.  
  1563.       h->root.type = bfd_link_hash_undefined;
  1564.       h->root.u.undef.abfd = h->root.u.def.section->owner;
  1565.       *size_change_ok = TRUE;
  1566.  
  1567.       olddef = FALSE;
  1568.       olddyncommon = FALSE;
  1569.  
  1570.       /* We again permit a type change when a common symbol may be
  1571.          overriding a function.  */
  1572.  
  1573.       if (bfd_is_com_section (sec))
  1574.         {
  1575.           if (oldfunc)
  1576.             {
  1577.               /* If a common symbol overrides a function, make sure
  1578.                  that it isn't defined dynamically nor has type
  1579.                  function.  */
  1580.               h->def_dynamic = 0;
  1581.               h->type = STT_NOTYPE;
  1582.             }
  1583.           *type_change_ok = TRUE;
  1584.         }
  1585.  
  1586.       if (hi->root.type == bfd_link_hash_indirect)
  1587.         flip = hi;
  1588.       else
  1589.         /* This union may have been set to be non-NULL when this symbol
  1590.            was seen in a dynamic object.  We must force the union to be
  1591.            NULL, so that it is correct for a regular symbol.  */
  1592.         h->verinfo.vertree = NULL;
  1593.     }
  1594.  
  1595.   /* Handle the special case of a new common symbol merging with an
  1596.      old symbol that looks like it might be a common symbol defined in
  1597.      a shared object.  Note that we have already handled the case in
  1598.      which a new common symbol should simply override the definition
  1599.      in the shared library.  */
  1600.  
  1601.   if (! newdyn
  1602.       && bfd_is_com_section (sec)
  1603.       && olddyncommon)
  1604.     {
  1605.       /* It would be best if we could set the hash table entry to a
  1606.          common symbol, but we don't know what to use for the section
  1607.          or the alignment.  */
  1608.       if (! ((*info->callbacks->multiple_common)
  1609.              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
  1610.         return FALSE;
  1611.  
  1612.       /* If the presumed common symbol in the dynamic object is
  1613.          larger, pretend that the new symbol has its size.  */
  1614.  
  1615.       if (h->size > *pvalue)
  1616.         *pvalue = h->size;
  1617.  
  1618.       /* We need to remember the alignment required by the symbol
  1619.          in the dynamic object.  */
  1620.       BFD_ASSERT (pold_alignment);
  1621.       *pold_alignment = h->root.u.def.section->alignment_power;
  1622.  
  1623.       olddef = FALSE;
  1624.       olddyncommon = FALSE;
  1625.  
  1626.       h->root.type = bfd_link_hash_undefined;
  1627.       h->root.u.undef.abfd = h->root.u.def.section->owner;
  1628.  
  1629.       *size_change_ok = TRUE;
  1630.       *type_change_ok = TRUE;
  1631.  
  1632.       if (hi->root.type == bfd_link_hash_indirect)
  1633.         flip = hi;
  1634.       else
  1635.         h->verinfo.vertree = NULL;
  1636.     }
  1637.  
  1638.   if (flip != NULL)
  1639.     {
  1640.       /* Handle the case where we had a versioned symbol in a dynamic
  1641.          library and now find a definition in a normal object.  In this
  1642.          case, we make the versioned symbol point to the normal one.  */
  1643.       flip->root.type = h->root.type;
  1644.       flip->root.u.undef.abfd = h->root.u.undef.abfd;
  1645.       h->root.type = bfd_link_hash_indirect;
  1646.       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
  1647.       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
  1648.       if (h->def_dynamic)
  1649.         {
  1650.           h->def_dynamic = 0;
  1651.           flip->ref_dynamic = 1;
  1652.         }
  1653.     }
  1654.  
  1655.   return TRUE;
  1656. }
  1657.  
  1658. /* This function is called to create an indirect symbol from the
  1659.    default for the symbol with the default version if needed. The
  1660.    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
  1661.    set DYNSYM if the new indirect symbol is dynamic.  */
  1662.  
  1663. static bfd_boolean
  1664. _bfd_elf_add_default_symbol (bfd *abfd,
  1665.                              struct bfd_link_info *info,
  1666.                              struct elf_link_hash_entry *h,
  1667.                              const char *name,
  1668.                              Elf_Internal_Sym *sym,
  1669.                              asection *sec,
  1670.                              bfd_vma value,
  1671.                              bfd **poldbfd,
  1672.                              bfd_boolean *dynsym)
  1673. {
  1674.   bfd_boolean type_change_ok;
  1675.   bfd_boolean size_change_ok;
  1676.   bfd_boolean skip;
  1677.   char *shortname;
  1678.   struct elf_link_hash_entry *hi;
  1679.   struct bfd_link_hash_entry *bh;
  1680.   const struct elf_backend_data *bed;
  1681.   bfd_boolean collect;
  1682.   bfd_boolean dynamic;
  1683.   bfd_boolean override;
  1684.   char *p;
  1685.   size_t len, shortlen;
  1686.   asection *tmp_sec;
  1687.   bfd_boolean matched;
  1688.  
  1689.   if (h->versioned == unversioned || h->versioned == versioned_hidden)
  1690.     return TRUE;
  1691.  
  1692.   /* If this symbol has a version, and it is the default version, we
  1693.      create an indirect symbol from the default name to the fully
  1694.      decorated name.  This will cause external references which do not
  1695.      specify a version to be bound to this version of the symbol.  */
  1696.   p = strchr (name, ELF_VER_CHR);
  1697.   if (h->versioned == unknown)
  1698.     {
  1699.       if (p == NULL)
  1700.         {
  1701.           h->versioned = unversioned;
  1702.           return TRUE;
  1703.         }
  1704.       else
  1705.         {
  1706.           if (p[1] != ELF_VER_CHR)
  1707.             {
  1708.               h->versioned = versioned_hidden;
  1709.               return TRUE;
  1710.             }
  1711.           else
  1712.             h->versioned = versioned;
  1713.         }
  1714.     }
  1715.   else
  1716.     {
  1717.       /* PR ld/19073: We may see an unversioned definition after the
  1718.          default version.  */
  1719.       if (p == NULL)
  1720.         return TRUE;
  1721.     }
  1722.  
  1723.   bed = get_elf_backend_data (abfd);
  1724.   collect = bed->collect;
  1725.   dynamic = (abfd->flags & DYNAMIC) != 0;
  1726.  
  1727.   shortlen = p - name;
  1728.   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
  1729.   if (shortname == NULL)
  1730.     return FALSE;
  1731.   memcpy (shortname, name, shortlen);
  1732.   shortname[shortlen] = '\0';
  1733.  
  1734.   /* We are going to create a new symbol.  Merge it with any existing
  1735.      symbol with this name.  For the purposes of the merge, act as
  1736.      though we were defining the symbol we just defined, although we
  1737.      actually going to define an indirect symbol.  */
  1738.   type_change_ok = FALSE;
  1739.   size_change_ok = FALSE;
  1740.   matched = TRUE;
  1741.   tmp_sec = sec;
  1742.   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1743.                               &hi, poldbfd, NULL, NULL, &skip, &override,
  1744.                               &type_change_ok, &size_change_ok, &matched))
  1745.     return FALSE;
  1746.  
  1747.   if (skip)
  1748.     goto nondefault;
  1749.  
  1750.   if (! override)
  1751.     {
  1752.       /* Add the default symbol if not performing a relocatable link.  */
  1753.       if (! bfd_link_relocatable (info))
  1754.         {
  1755.           bh = &hi->root;
  1756.           if (! (_bfd_generic_link_add_one_symbol
  1757.                  (info, abfd, shortname, BSF_INDIRECT,
  1758.                   bfd_ind_section_ptr,
  1759.                   0, name, FALSE, collect, &bh)))
  1760.             return FALSE;
  1761.           hi = (struct elf_link_hash_entry *) bh;
  1762.         }
  1763.     }
  1764.   else
  1765.     {
  1766.       /* In this case the symbol named SHORTNAME is overriding the
  1767.          indirect symbol we want to add.  We were planning on making
  1768.          SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
  1769.          is the name without a version.  NAME is the fully versioned
  1770.          name, and it is the default version.
  1771.  
  1772.          Overriding means that we already saw a definition for the
  1773.          symbol SHORTNAME in a regular object, and it is overriding
  1774.          the symbol defined in the dynamic object.
  1775.  
  1776.          When this happens, we actually want to change NAME, the
  1777.          symbol we just added, to refer to SHORTNAME.  This will cause
  1778.          references to NAME in the shared object to become references
  1779.          to SHORTNAME in the regular object.  This is what we expect
  1780.          when we override a function in a shared object: that the
  1781.          references in the shared object will be mapped to the
  1782.          definition in the regular object.  */
  1783.  
  1784.       while (hi->root.type == bfd_link_hash_indirect
  1785.              || hi->root.type == bfd_link_hash_warning)
  1786.         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1787.  
  1788.       h->root.type = bfd_link_hash_indirect;
  1789.       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1790.       if (h->def_dynamic)
  1791.         {
  1792.           h->def_dynamic = 0;
  1793.           hi->ref_dynamic = 1;
  1794.           if (hi->ref_regular
  1795.               || hi->def_regular)
  1796.             {
  1797.               if (! bfd_elf_link_record_dynamic_symbol (info, hi))
  1798.                 return FALSE;
  1799.             }
  1800.         }
  1801.  
  1802.       /* Now set HI to H, so that the following code will set the
  1803.          other fields correctly.  */
  1804.       hi = h;
  1805.     }
  1806.  
  1807.   /* Check if HI is a warning symbol.  */
  1808.   if (hi->root.type == bfd_link_hash_warning)
  1809.     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1810.  
  1811.   /* If there is a duplicate definition somewhere, then HI may not
  1812.      point to an indirect symbol.  We will have reported an error to
  1813.      the user in that case.  */
  1814.  
  1815.   if (hi->root.type == bfd_link_hash_indirect)
  1816.     {
  1817.       struct elf_link_hash_entry *ht;
  1818.  
  1819.       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1820.       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
  1821.  
  1822.       /* A reference to the SHORTNAME symbol from a dynamic library
  1823.          will be satisfied by the versioned symbol at runtime.  In
  1824.          effect, we have a reference to the versioned symbol.  */
  1825.       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
  1826.       hi->dynamic_def |= ht->dynamic_def;
  1827.  
  1828.       /* See if the new flags lead us to realize that the symbol must
  1829.          be dynamic.  */
  1830.       if (! *dynsym)
  1831.         {
  1832.           if (! dynamic)
  1833.             {
  1834.               if (! bfd_link_executable (info)
  1835.                   || hi->def_dynamic
  1836.                   || hi->ref_dynamic)
  1837.                 *dynsym = TRUE;
  1838.             }
  1839.           else
  1840.             {
  1841.               if (hi->ref_regular)
  1842.                 *dynsym = TRUE;
  1843.             }
  1844.         }
  1845.     }
  1846.  
  1847.   /* We also need to define an indirection from the nondefault version
  1848.      of the symbol.  */
  1849.  
  1850. nondefault:
  1851.   len = strlen (name);
  1852.   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
  1853.   if (shortname == NULL)
  1854.     return FALSE;
  1855.   memcpy (shortname, name, shortlen);
  1856.   memcpy (shortname + shortlen, p + 1, len - shortlen);
  1857.  
  1858.   /* Once again, merge with any existing symbol.  */
  1859.   type_change_ok = FALSE;
  1860.   size_change_ok = FALSE;
  1861.   tmp_sec = sec;
  1862.   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1863.                               &hi, poldbfd, NULL, NULL, &skip, &override,
  1864.                               &type_change_ok, &size_change_ok, &matched))
  1865.     return FALSE;
  1866.  
  1867.   if (skip)
  1868.     return TRUE;
  1869.  
  1870.   if (override)
  1871.     {
  1872.       /* Here SHORTNAME is a versioned name, so we don't expect to see
  1873.          the type of override we do in the case above unless it is
  1874.          overridden by a versioned definition.  */
  1875.       if (hi->root.type != bfd_link_hash_defined
  1876.           && hi->root.type != bfd_link_hash_defweak)
  1877.         (*_bfd_error_handler)
  1878.           (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
  1879.            abfd, shortname);
  1880.     }
  1881.   else
  1882.     {
  1883.       bh = &hi->root;
  1884.       if (! (_bfd_generic_link_add_one_symbol
  1885.              (info, abfd, shortname, BSF_INDIRECT,
  1886.               bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
  1887.         return FALSE;
  1888.       hi = (struct elf_link_hash_entry *) bh;
  1889.  
  1890.       /* If there is a duplicate definition somewhere, then HI may not
  1891.          point to an indirect symbol.  We will have reported an error
  1892.          to the user in that case.  */
  1893.  
  1894.       if (hi->root.type == bfd_link_hash_indirect)
  1895.         {
  1896.           (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  1897.           h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
  1898.           hi->dynamic_def |= h->dynamic_def;
  1899.  
  1900.           /* See if the new flags lead us to realize that the symbol
  1901.              must be dynamic.  */
  1902.           if (! *dynsym)
  1903.             {
  1904.               if (! dynamic)
  1905.                 {
  1906.                   if (! bfd_link_executable (info)
  1907.                       || hi->ref_dynamic)
  1908.                     *dynsym = TRUE;
  1909.                 }
  1910.               else
  1911.                 {
  1912.                   if (hi->ref_regular)
  1913.                     *dynsym = TRUE;
  1914.                 }
  1915.             }
  1916.         }
  1917.     }
  1918.  
  1919.   return TRUE;
  1920. }
  1921. /* This routine is used to export all defined symbols into the dynamic
  1922.    symbol table.  It is called via elf_link_hash_traverse.  */
  1923.  
  1924. static bfd_boolean
  1925. _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
  1926. {
  1927.   struct elf_info_failed *eif = (struct elf_info_failed *) data;
  1928.  
  1929.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  1930.   if (h->root.type == bfd_link_hash_indirect)
  1931.     return TRUE;
  1932.  
  1933.   /* Ignore this if we won't export it.  */
  1934.   if (!eif->info->export_dynamic && !h->dynamic)
  1935.     return TRUE;
  1936.  
  1937.   if (h->dynindx == -1
  1938.       && (h->def_regular || h->ref_regular)
  1939.       && ! bfd_hide_sym_by_version (eif->info->version_info,
  1940.                                     h->root.root.string))
  1941.     {
  1942.       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  1943.         {
  1944.           eif->failed = TRUE;
  1945.           return FALSE;
  1946.         }
  1947.     }
  1948.  
  1949.   return TRUE;
  1950. }
  1951. /* Look through the symbols which are defined in other shared
  1952.    libraries and referenced here.  Update the list of version
  1953.    dependencies.  This will be put into the .gnu.version_r section.
  1954.    This function is called via elf_link_hash_traverse.  */
  1955.  
  1956. static bfd_boolean
  1957. _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
  1958.                                          void *data)
  1959. {
  1960.   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
  1961.   Elf_Internal_Verneed *t;
  1962.   Elf_Internal_Vernaux *a;
  1963.   bfd_size_type amt;
  1964.  
  1965.   /* We only care about symbols defined in shared objects with version
  1966.      information.  */
  1967.   if (!h->def_dynamic
  1968.       || h->def_regular
  1969.       || h->dynindx == -1
  1970.       || h->verinfo.verdef == NULL
  1971.       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
  1972.           & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
  1973.     return TRUE;
  1974.  
  1975.   /* See if we already know about this version.  */
  1976.   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
  1977.        t != NULL;
  1978.        t = t->vn_nextref)
  1979.     {
  1980.       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
  1981.         continue;
  1982.  
  1983.       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  1984.         if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
  1985.           return TRUE;
  1986.  
  1987.       break;
  1988.     }
  1989.  
  1990.   /* This is a new version.  Add it to tree we are building.  */
  1991.  
  1992.   if (t == NULL)
  1993.     {
  1994.       amt = sizeof *t;
  1995.       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
  1996.       if (t == NULL)
  1997.         {
  1998.           rinfo->failed = TRUE;
  1999.           return FALSE;
  2000.         }
  2001.  
  2002.       t->vn_bfd = h->verinfo.verdef->vd_bfd;
  2003.       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
  2004.       elf_tdata (rinfo->info->output_bfd)->verref = t;
  2005.     }
  2006.  
  2007.   amt = sizeof *a;
  2008.   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
  2009.   if (a == NULL)
  2010.     {
  2011.       rinfo->failed = TRUE;
  2012.       return FALSE;
  2013.     }
  2014.  
  2015.   /* Note that we are copying a string pointer here, and testing it
  2016.      above.  If bfd_elf_string_from_elf_section is ever changed to
  2017.      discard the string data when low in memory, this will have to be
  2018.      fixed.  */
  2019.   a->vna_nodename = h->verinfo.verdef->vd_nodename;
  2020.  
  2021.   a->vna_flags = h->verinfo.verdef->vd_flags;
  2022.   a->vna_nextptr = t->vn_auxptr;
  2023.  
  2024.   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
  2025.   ++rinfo->vers;
  2026.  
  2027.   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
  2028.  
  2029.   t->vn_auxptr = a;
  2030.  
  2031.   return TRUE;
  2032. }
  2033.  
  2034. /* Figure out appropriate versions for all the symbols.  We may not
  2035.    have the version number script until we have read all of the input
  2036.    files, so until that point we don't know which symbols should be
  2037.    local.  This function is called via elf_link_hash_traverse.  */
  2038.  
  2039. static bfd_boolean
  2040. _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
  2041. {
  2042.   struct elf_info_failed *sinfo;
  2043.   struct bfd_link_info *info;
  2044.   const struct elf_backend_data *bed;
  2045.   struct elf_info_failed eif;
  2046.   char *p;
  2047.   bfd_size_type amt;
  2048.  
  2049.   sinfo = (struct elf_info_failed *) data;
  2050.   info = sinfo->info;
  2051.  
  2052.   /* Fix the symbol flags.  */
  2053.   eif.failed = FALSE;
  2054.   eif.info = info;
  2055.   if (! _bfd_elf_fix_symbol_flags (h, &eif))
  2056.     {
  2057.       if (eif.failed)
  2058.         sinfo->failed = TRUE;
  2059.       return FALSE;
  2060.     }
  2061.  
  2062.   /* We only need version numbers for symbols defined in regular
  2063.      objects.  */
  2064.   if (!h->def_regular)
  2065.     return TRUE;
  2066.  
  2067.   bed = get_elf_backend_data (info->output_bfd);
  2068.   p = strchr (h->root.root.string, ELF_VER_CHR);
  2069.   if (p != NULL && h->verinfo.vertree == NULL)
  2070.     {
  2071.       struct bfd_elf_version_tree *t;
  2072.  
  2073.       ++p;
  2074.       if (*p == ELF_VER_CHR)
  2075.         ++p;
  2076.  
  2077.       /* If there is no version string, we can just return out.  */
  2078.       if (*p == '\0')
  2079.         return TRUE;
  2080.  
  2081.       /* Look for the version.  If we find it, it is no longer weak.  */
  2082.       for (t = sinfo->info->version_info; t != NULL; t = t->next)
  2083.         {
  2084.           if (strcmp (t->name, p) == 0)
  2085.             {
  2086.               size_t len;
  2087.               char *alc;
  2088.               struct bfd_elf_version_expr *d;
  2089.  
  2090.               len = p - h->root.root.string;
  2091.               alc = (char *) bfd_malloc (len);
  2092.               if (alc == NULL)
  2093.                 {
  2094.                   sinfo->failed = TRUE;
  2095.                   return FALSE;
  2096.                 }
  2097.               memcpy (alc, h->root.root.string, len - 1);
  2098.               alc[len - 1] = '\0';
  2099.               if (alc[len - 2] == ELF_VER_CHR)
  2100.                 alc[len - 2] = '\0';
  2101.  
  2102.               h->verinfo.vertree = t;
  2103.               t->used = TRUE;
  2104.               d = NULL;
  2105.  
  2106.               if (t->globals.list != NULL)
  2107.                 d = (*t->match) (&t->globals, NULL, alc);
  2108.  
  2109.               /* See if there is anything to force this symbol to
  2110.                  local scope.  */
  2111.               if (d == NULL && t->locals.list != NULL)
  2112.                 {
  2113.                   d = (*t->match) (&t->locals, NULL, alc);
  2114.                   if (d != NULL
  2115.                       && h->dynindx != -1
  2116.                       && ! info->export_dynamic)
  2117.                     (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  2118.                 }
  2119.  
  2120.               free (alc);
  2121.               break;
  2122.             }
  2123.         }
  2124.  
  2125.       /* If we are building an application, we need to create a
  2126.          version node for this version.  */
  2127.       if (t == NULL && bfd_link_executable (info))
  2128.         {
  2129.           struct bfd_elf_version_tree **pp;
  2130.           int version_index;
  2131.  
  2132.           /* If we aren't going to export this symbol, we don't need
  2133.              to worry about it.  */
  2134.           if (h->dynindx == -1)
  2135.             return TRUE;
  2136.  
  2137.           amt = sizeof *t;
  2138.           t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
  2139.           if (t == NULL)
  2140.             {
  2141.               sinfo->failed = TRUE;
  2142.               return FALSE;
  2143.             }
  2144.  
  2145.           t->name = p;
  2146.           t->name_indx = (unsigned int) -1;
  2147.           t->used = TRUE;
  2148.  
  2149.           version_index = 1;
  2150.           /* Don't count anonymous version tag.  */
  2151.           if (sinfo->info->version_info != NULL
  2152.               && sinfo->info->version_info->vernum == 0)
  2153.             version_index = 0;
  2154.           for (pp = &sinfo->info->version_info;
  2155.                *pp != NULL;
  2156.                pp = &(*pp)->next)
  2157.             ++version_index;
  2158.           t->vernum = version_index;
  2159.  
  2160.           *pp = t;
  2161.  
  2162.           h->verinfo.vertree = t;
  2163.         }
  2164.       else if (t == NULL)
  2165.         {
  2166.           /* We could not find the version for a symbol when
  2167.              generating a shared archive.  Return an error.  */
  2168.           (*_bfd_error_handler)
  2169.             (_("%B: version node not found for symbol %s"),
  2170.              info->output_bfd, h->root.root.string);
  2171.           bfd_set_error (bfd_error_bad_value);
  2172.           sinfo->failed = TRUE;
  2173.           return FALSE;
  2174.         }
  2175.     }
  2176.  
  2177.   /* If we don't have a version for this symbol, see if we can find
  2178.      something.  */
  2179.   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
  2180.     {
  2181.       bfd_boolean hide;
  2182.  
  2183.       h->verinfo.vertree
  2184.         = bfd_find_version_for_sym (sinfo->info->version_info,
  2185.                                     h->root.root.string, &hide);
  2186.       if (h->verinfo.vertree != NULL && hide)
  2187.         (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  2188.     }
  2189.  
  2190.   return TRUE;
  2191. }
  2192. /* Read and swap the relocs from the section indicated by SHDR.  This
  2193.    may be either a REL or a RELA section.  The relocations are
  2194.    translated into RELA relocations and stored in INTERNAL_RELOCS,
  2195.    which should have already been allocated to contain enough space.
  2196.    The EXTERNAL_RELOCS are a buffer where the external form of the
  2197.    relocations should be stored.
  2198.  
  2199.    Returns FALSE if something goes wrong.  */
  2200.  
  2201. static bfd_boolean
  2202. elf_link_read_relocs_from_section (bfd *abfd,
  2203.                                    asection *sec,
  2204.                                    Elf_Internal_Shdr *shdr,
  2205.                                    void *external_relocs,
  2206.                                    Elf_Internal_Rela *internal_relocs)
  2207. {
  2208.   const struct elf_backend_data *bed;
  2209.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  2210.   const bfd_byte *erela;
  2211.   const bfd_byte *erelaend;
  2212.   Elf_Internal_Rela *irela;
  2213.   Elf_Internal_Shdr *symtab_hdr;
  2214.   size_t nsyms;
  2215.  
  2216.   /* Position ourselves at the start of the section.  */
  2217.   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
  2218.     return FALSE;
  2219.  
  2220.   /* Read the relocations.  */
  2221.   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
  2222.     return FALSE;
  2223.  
  2224.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2225.   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
  2226.  
  2227.   bed = get_elf_backend_data (abfd);
  2228.  
  2229.   /* Convert the external relocations to the internal format.  */
  2230.   if (shdr->sh_entsize == bed->s->sizeof_rel)
  2231.     swap_in = bed->s->swap_reloc_in;
  2232.   else if (shdr->sh_entsize == bed->s->sizeof_rela)
  2233.     swap_in = bed->s->swap_reloca_in;
  2234.   else
  2235.     {
  2236.       bfd_set_error (bfd_error_wrong_format);
  2237.       return FALSE;
  2238.     }
  2239.  
  2240.   erela = (const bfd_byte *) external_relocs;
  2241.   erelaend = erela + shdr->sh_size;
  2242.   irela = internal_relocs;
  2243.   while (erela < erelaend)
  2244.     {
  2245.       bfd_vma r_symndx;
  2246.  
  2247.       (*swap_in) (abfd, erela, irela);
  2248.       r_symndx = ELF32_R_SYM (irela->r_info);
  2249.       if (bed->s->arch_size == 64)
  2250.         r_symndx >>= 24;
  2251.       if (nsyms > 0)
  2252.         {
  2253.           if ((size_t) r_symndx >= nsyms)
  2254.             {
  2255.               (*_bfd_error_handler)
  2256.                 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
  2257.                    " for offset 0x%lx in section `%A'"),
  2258.                  abfd, sec,
  2259.                  (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
  2260.               bfd_set_error (bfd_error_bad_value);
  2261.               return FALSE;
  2262.             }
  2263.         }
  2264.       else if (r_symndx != STN_UNDEF)
  2265.         {
  2266.           (*_bfd_error_handler)
  2267.             (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
  2268.                " when the object file has no symbol table"),
  2269.              abfd, sec,
  2270.              (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
  2271.           bfd_set_error (bfd_error_bad_value);
  2272.           return FALSE;
  2273.         }
  2274.       irela += bed->s->int_rels_per_ext_rel;
  2275.       erela += shdr->sh_entsize;
  2276.     }
  2277.  
  2278.   return TRUE;
  2279. }
  2280.  
  2281. /* Read and swap the relocs for a section O.  They may have been
  2282.    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
  2283.    not NULL, they are used as buffers to read into.  They are known to
  2284.    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
  2285.    the return value is allocated using either malloc or bfd_alloc,
  2286.    according to the KEEP_MEMORY argument.  If O has two relocation
  2287.    sections (both REL and RELA relocations), then the REL_HDR
  2288.    relocations will appear first in INTERNAL_RELOCS, followed by the
  2289.    RELA_HDR relocations.  */
  2290.  
  2291. Elf_Internal_Rela *
  2292. _bfd_elf_link_read_relocs (bfd *abfd,
  2293.                            asection *o,
  2294.                            void *external_relocs,
  2295.                            Elf_Internal_Rela *internal_relocs,
  2296.                            bfd_boolean keep_memory)
  2297. {
  2298.   void *alloc1 = NULL;
  2299.   Elf_Internal_Rela *alloc2 = NULL;
  2300.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2301.   struct bfd_elf_section_data *esdo = elf_section_data (o);
  2302.   Elf_Internal_Rela *internal_rela_relocs;
  2303.  
  2304.   if (esdo->relocs != NULL)
  2305.     return esdo->relocs;
  2306.  
  2307.   if (o->reloc_count == 0)
  2308.     return NULL;
  2309.  
  2310.   if (internal_relocs == NULL)
  2311.     {
  2312.       bfd_size_type size;
  2313.  
  2314.       size = o->reloc_count;
  2315.       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
  2316.       if (keep_memory)
  2317.         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
  2318.       else
  2319.         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
  2320.       if (internal_relocs == NULL)
  2321.         goto error_return;
  2322.     }
  2323.  
  2324.   if (external_relocs == NULL)
  2325.     {
  2326.       bfd_size_type size = 0;
  2327.  
  2328.       if (esdo->rel.hdr)
  2329.         size += esdo->rel.hdr->sh_size;
  2330.       if (esdo->rela.hdr)
  2331.         size += esdo->rela.hdr->sh_size;
  2332.  
  2333.       alloc1 = bfd_malloc (size);
  2334.       if (alloc1 == NULL)
  2335.         goto error_return;
  2336.       external_relocs = alloc1;
  2337.     }
  2338.  
  2339.   internal_rela_relocs = internal_relocs;
  2340.   if (esdo->rel.hdr)
  2341.     {
  2342.       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
  2343.                                               external_relocs,
  2344.                                               internal_relocs))
  2345.         goto error_return;
  2346.       external_relocs = (((bfd_byte *) external_relocs)
  2347.                          + esdo->rel.hdr->sh_size);
  2348.       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
  2349.                                * bed->s->int_rels_per_ext_rel);
  2350.     }
  2351.  
  2352.   if (esdo->rela.hdr
  2353.       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
  2354.                                               external_relocs,
  2355.                                               internal_rela_relocs)))
  2356.     goto error_return;
  2357.  
  2358.   /* Cache the results for next time, if we can.  */
  2359.   if (keep_memory)
  2360.     esdo->relocs = internal_relocs;
  2361.  
  2362.   if (alloc1 != NULL)
  2363.     free (alloc1);
  2364.  
  2365.   /* Don't free alloc2, since if it was allocated we are passing it
  2366.      back (under the name of internal_relocs).  */
  2367.  
  2368.   return internal_relocs;
  2369.  
  2370.  error_return:
  2371.   if (alloc1 != NULL)
  2372.     free (alloc1);
  2373.   if (alloc2 != NULL)
  2374.     {
  2375.       if (keep_memory)
  2376.         bfd_release (abfd, alloc2);
  2377.       else
  2378.         free (alloc2);
  2379.     }
  2380.   return NULL;
  2381. }
  2382.  
  2383. /* Compute the size of, and allocate space for, REL_HDR which is the
  2384.    section header for a section containing relocations for O.  */
  2385.  
  2386. static bfd_boolean
  2387. _bfd_elf_link_size_reloc_section (bfd *abfd,
  2388.                                   struct bfd_elf_section_reloc_data *reldata)
  2389. {
  2390.   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
  2391.  
  2392.   /* That allows us to calculate the size of the section.  */
  2393.   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
  2394.  
  2395.   /* The contents field must last into write_object_contents, so we
  2396.      allocate it with bfd_alloc rather than malloc.  Also since we
  2397.      cannot be sure that the contents will actually be filled in,
  2398.      we zero the allocated space.  */
  2399.   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
  2400.   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
  2401.     return FALSE;
  2402.  
  2403.   if (reldata->hashes == NULL && reldata->count)
  2404.     {
  2405.       struct elf_link_hash_entry **p;
  2406.  
  2407.       p = ((struct elf_link_hash_entry **)
  2408.            bfd_zmalloc (reldata->count * sizeof (*p)));
  2409.       if (p == NULL)
  2410.         return FALSE;
  2411.  
  2412.       reldata->hashes = p;
  2413.     }
  2414.  
  2415.   return TRUE;
  2416. }
  2417.  
  2418. /* Copy the relocations indicated by the INTERNAL_RELOCS (which
  2419.    originated from the section given by INPUT_REL_HDR) to the
  2420.    OUTPUT_BFD.  */
  2421.  
  2422. bfd_boolean
  2423. _bfd_elf_link_output_relocs (bfd *output_bfd,
  2424.                              asection *input_section,
  2425.                              Elf_Internal_Shdr *input_rel_hdr,
  2426.                              Elf_Internal_Rela *internal_relocs,
  2427.                              struct elf_link_hash_entry **rel_hash
  2428.                                ATTRIBUTE_UNUSED)
  2429. {
  2430.   Elf_Internal_Rela *irela;
  2431.   Elf_Internal_Rela *irelaend;
  2432.   bfd_byte *erel;
  2433.   struct bfd_elf_section_reloc_data *output_reldata;
  2434.   asection *output_section;
  2435.   const struct elf_backend_data *bed;
  2436.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  2437.   struct bfd_elf_section_data *esdo;
  2438.  
  2439.   output_section = input_section->output_section;
  2440.  
  2441.   bed = get_elf_backend_data (output_bfd);
  2442.   esdo = elf_section_data (output_section);
  2443.   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2444.     {
  2445.       output_reldata = &esdo->rel;
  2446.       swap_out = bed->s->swap_reloc_out;
  2447.     }
  2448.   else if (esdo->rela.hdr
  2449.            && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2450.     {
  2451.       output_reldata = &esdo->rela;
  2452.       swap_out = bed->s->swap_reloca_out;
  2453.     }
  2454.   else
  2455.     {
  2456.       (*_bfd_error_handler)
  2457.         (_("%B: relocation size mismatch in %B section %A"),
  2458.          output_bfd, input_section->owner, input_section);
  2459.       bfd_set_error (bfd_error_wrong_format);
  2460.       return FALSE;
  2461.     }
  2462.  
  2463.   erel = output_reldata->hdr->contents;
  2464.   erel += output_reldata->count * input_rel_hdr->sh_entsize;
  2465.   irela = internal_relocs;
  2466.   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
  2467.                       * bed->s->int_rels_per_ext_rel);
  2468.   while (irela < irelaend)
  2469.     {
  2470.       (*swap_out) (output_bfd, irela, erel);
  2471.       irela += bed->s->int_rels_per_ext_rel;
  2472.       erel += input_rel_hdr->sh_entsize;
  2473.     }
  2474.  
  2475.   /* Bump the counter, so that we know where to add the next set of
  2476.      relocations.  */
  2477.   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
  2478.  
  2479.   return TRUE;
  2480. }
  2481. /* Make weak undefined symbols in PIE dynamic.  */
  2482.  
  2483. bfd_boolean
  2484. _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
  2485.                                  struct elf_link_hash_entry *h)
  2486. {
  2487.   if (bfd_link_pie (info)
  2488.       && h->dynindx == -1
  2489.       && h->root.type == bfd_link_hash_undefweak)
  2490.     return bfd_elf_link_record_dynamic_symbol (info, h);
  2491.  
  2492.   return TRUE;
  2493. }
  2494.  
  2495. /* Fix up the flags for a symbol.  This handles various cases which
  2496.    can only be fixed after all the input files are seen.  This is
  2497.    currently called by both adjust_dynamic_symbol and
  2498.    assign_sym_version, which is unnecessary but perhaps more robust in
  2499.    the face of future changes.  */
  2500.  
  2501. static bfd_boolean
  2502. _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
  2503.                            struct elf_info_failed *eif)
  2504. {
  2505.   const struct elf_backend_data *bed;
  2506.  
  2507.   /* If this symbol was mentioned in a non-ELF file, try to set
  2508.      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
  2509.      permit a non-ELF file to correctly refer to a symbol defined in
  2510.      an ELF dynamic object.  */
  2511.   if (h->non_elf)
  2512.     {
  2513.       while (h->root.type == bfd_link_hash_indirect)
  2514.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2515.  
  2516.       if (h->root.type != bfd_link_hash_defined
  2517.           && h->root.type != bfd_link_hash_defweak)
  2518.         {
  2519.           h->ref_regular = 1;
  2520.           h->ref_regular_nonweak = 1;
  2521.         }
  2522.       else
  2523.         {
  2524.           if (h->root.u.def.section->owner != NULL
  2525.               && (bfd_get_flavour (h->root.u.def.section->owner)
  2526.                   == bfd_target_elf_flavour))
  2527.             {
  2528.               h->ref_regular = 1;
  2529.               h->ref_regular_nonweak = 1;
  2530.             }
  2531.           else
  2532.             h->def_regular = 1;
  2533.         }
  2534.  
  2535.       if (h->dynindx == -1
  2536.           && (h->def_dynamic
  2537.               || h->ref_dynamic))
  2538.         {
  2539.           if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  2540.             {
  2541.               eif->failed = TRUE;
  2542.               return FALSE;
  2543.             }
  2544.         }
  2545.     }
  2546.   else
  2547.     {
  2548.       /* Unfortunately, NON_ELF is only correct if the symbol
  2549.          was first seen in a non-ELF file.  Fortunately, if the symbol
  2550.          was first seen in an ELF file, we're probably OK unless the
  2551.          symbol was defined in a non-ELF file.  Catch that case here.
  2552.          FIXME: We're still in trouble if the symbol was first seen in
  2553.          a dynamic object, and then later in a non-ELF regular object.  */
  2554.       if ((h->root.type == bfd_link_hash_defined
  2555.            || h->root.type == bfd_link_hash_defweak)
  2556.           && !h->def_regular
  2557.           && (h->root.u.def.section->owner != NULL
  2558.               ? (bfd_get_flavour (h->root.u.def.section->owner)
  2559.                  != bfd_target_elf_flavour)
  2560.               : (bfd_is_abs_section (h->root.u.def.section)
  2561.                  && !h->def_dynamic)))
  2562.         h->def_regular = 1;
  2563.     }
  2564.  
  2565.   /* Backend specific symbol fixup.  */
  2566.   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
  2567.   if (bed->elf_backend_fixup_symbol
  2568.       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
  2569.     return FALSE;
  2570.  
  2571.   /* If this is a final link, and the symbol was defined as a common
  2572.      symbol in a regular object file, and there was no definition in
  2573.      any dynamic object, then the linker will have allocated space for
  2574.      the symbol in a common section but the DEF_REGULAR
  2575.      flag will not have been set.  */
  2576.   if (h->root.type == bfd_link_hash_defined
  2577.       && !h->def_regular
  2578.       && h->ref_regular
  2579.       && !h->def_dynamic
  2580.       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
  2581.     h->def_regular = 1;
  2582.  
  2583.   /* If -Bsymbolic was used (which means to bind references to global
  2584.      symbols to the definition within the shared object), and this
  2585.      symbol was defined in a regular object, then it actually doesn't
  2586.      need a PLT entry.  Likewise, if the symbol has non-default
  2587.      visibility.  If the symbol has hidden or internal visibility, we
  2588.      will force it local.  */
  2589.   if (h->needs_plt
  2590.       && bfd_link_pic (eif->info)
  2591.       && is_elf_hash_table (eif->info->hash)
  2592.       && (SYMBOLIC_BIND (eif->info, h)
  2593.           || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2594.       && h->def_regular)
  2595.     {
  2596.       bfd_boolean force_local;
  2597.  
  2598.       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
  2599.                      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
  2600.       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
  2601.     }
  2602.  
  2603.   /* If a weak undefined symbol has non-default visibility, we also
  2604.      hide it from the dynamic linker.  */
  2605.   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2606.       && h->root.type == bfd_link_hash_undefweak)
  2607.     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
  2608.  
  2609.   /* If this is a weak defined symbol in a dynamic object, and we know
  2610.      the real definition in the dynamic object, copy interesting flags
  2611.      over to the real definition.  */
  2612.   if (h->u.weakdef != NULL)
  2613.     {
  2614.       /* If the real definition is defined by a regular object file,
  2615.          don't do anything special.  See the longer description in
  2616.          _bfd_elf_adjust_dynamic_symbol, below.  */
  2617.       if (h->u.weakdef->def_regular)
  2618.         h->u.weakdef = NULL;
  2619.       else
  2620.         {
  2621.           struct elf_link_hash_entry *weakdef = h->u.weakdef;
  2622.  
  2623.           while (h->root.type == bfd_link_hash_indirect)
  2624.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2625.  
  2626.           BFD_ASSERT (h->root.type == bfd_link_hash_defined
  2627.                       || h->root.type == bfd_link_hash_defweak);
  2628.           BFD_ASSERT (weakdef->def_dynamic);
  2629.           BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
  2630.                       || weakdef->root.type == bfd_link_hash_defweak);
  2631.           (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
  2632.         }
  2633.     }
  2634.  
  2635.   return TRUE;
  2636. }
  2637.  
  2638. /* Make the backend pick a good value for a dynamic symbol.  This is
  2639.    called via elf_link_hash_traverse, and also calls itself
  2640.    recursively.  */
  2641.  
  2642. static bfd_boolean
  2643. _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
  2644. {
  2645.   struct elf_info_failed *eif = (struct elf_info_failed *) data;
  2646.   bfd *dynobj;
  2647.   const struct elf_backend_data *bed;
  2648.  
  2649.   if (! is_elf_hash_table (eif->info->hash))
  2650.     return FALSE;
  2651.  
  2652.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  2653.   if (h->root.type == bfd_link_hash_indirect)
  2654.     return TRUE;
  2655.  
  2656.   /* Fix the symbol flags.  */
  2657.   if (! _bfd_elf_fix_symbol_flags (h, eif))
  2658.     return FALSE;
  2659.  
  2660.   /* If this symbol does not require a PLT entry, and it is not
  2661.      defined by a dynamic object, or is not referenced by a regular
  2662.      object, ignore it.  We do have to handle a weak defined symbol,
  2663.      even if no regular object refers to it, if we decided to add it
  2664.      to the dynamic symbol table.  FIXME: Do we normally need to worry
  2665.      about symbols which are defined by one dynamic object and
  2666.      referenced by another one?  */
  2667.   if (!h->needs_plt
  2668.       && h->type != STT_GNU_IFUNC
  2669.       && (h->def_regular
  2670.           || !h->def_dynamic
  2671.           || (!h->ref_regular
  2672.               && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
  2673.     {
  2674.       h->plt = elf_hash_table (eif->info)->init_plt_offset;
  2675.       return TRUE;
  2676.     }
  2677.  
  2678.   /* If we've already adjusted this symbol, don't do it again.  This
  2679.      can happen via a recursive call.  */
  2680.   if (h->dynamic_adjusted)
  2681.     return TRUE;
  2682.  
  2683.   /* Don't look at this symbol again.  Note that we must set this
  2684.      after checking the above conditions, because we may look at a
  2685.      symbol once, decide not to do anything, and then get called
  2686.      recursively later after REF_REGULAR is set below.  */
  2687.   h->dynamic_adjusted = 1;
  2688.  
  2689.   /* If this is a weak definition, and we know a real definition, and
  2690.      the real symbol is not itself defined by a regular object file,
  2691.      then get a good value for the real definition.  We handle the
  2692.      real symbol first, for the convenience of the backend routine.
  2693.  
  2694.      Note that there is a confusing case here.  If the real definition
  2695.      is defined by a regular object file, we don't get the real symbol
  2696.      from the dynamic object, but we do get the weak symbol.  If the
  2697.      processor backend uses a COPY reloc, then if some routine in the
  2698.      dynamic object changes the real symbol, we will not see that
  2699.      change in the corresponding weak symbol.  This is the way other
  2700.      ELF linkers work as well, and seems to be a result of the shared
  2701.      library model.
  2702.  
  2703.      I will clarify this issue.  Most SVR4 shared libraries define the
  2704.      variable _timezone and define timezone as a weak synonym.  The
  2705.      tzset call changes _timezone.  If you write
  2706.        extern int timezone;
  2707.        int _timezone = 5;
  2708.        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
  2709.      you might expect that, since timezone is a synonym for _timezone,
  2710.      the same number will print both times.  However, if the processor
  2711.      backend uses a COPY reloc, then actually timezone will be copied
  2712.      into your process image, and, since you define _timezone
  2713.      yourself, _timezone will not.  Thus timezone and _timezone will
  2714.      wind up at different memory locations.  The tzset call will set
  2715.      _timezone, leaving timezone unchanged.  */
  2716.  
  2717.   if (h->u.weakdef != NULL)
  2718.     {
  2719.       /* If we get to this point, there is an implicit reference to
  2720.          H->U.WEAKDEF by a regular object file via the weak symbol H.  */
  2721.       h->u.weakdef->ref_regular = 1;
  2722.  
  2723.       /* Ensure that the backend adjust_dynamic_symbol function sees
  2724.          H->U.WEAKDEF before H by recursively calling ourselves.  */
  2725.       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
  2726.         return FALSE;
  2727.     }
  2728.  
  2729.   /* If a symbol has no type and no size and does not require a PLT
  2730.      entry, then we are probably about to do the wrong thing here: we
  2731.      are probably going to create a COPY reloc for an empty object.
  2732.      This case can arise when a shared object is built with assembly
  2733.      code, and the assembly code fails to set the symbol type.  */
  2734.   if (h->size == 0
  2735.       && h->type == STT_NOTYPE
  2736.       && !h->needs_plt)
  2737.     (*_bfd_error_handler)
  2738.       (_("warning: type and size of dynamic symbol `%s' are not defined"),
  2739.        h->root.root.string);
  2740.  
  2741.   dynobj = elf_hash_table (eif->info)->dynobj;
  2742.   bed = get_elf_backend_data (dynobj);
  2743.  
  2744.   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
  2745.     {
  2746.       eif->failed = TRUE;
  2747.       return FALSE;
  2748.     }
  2749.  
  2750.   return TRUE;
  2751. }
  2752.  
  2753. /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
  2754.    DYNBSS.  */
  2755.  
  2756. bfd_boolean
  2757. _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
  2758.                               struct elf_link_hash_entry *h,
  2759.                               asection *dynbss)
  2760. {
  2761.   unsigned int power_of_two;
  2762.   bfd_vma mask;
  2763.   asection *sec = h->root.u.def.section;
  2764.  
  2765.   /* The section aligment of definition is the maximum alignment
  2766.      requirement of symbols defined in the section.  Since we don't
  2767.      know the symbol alignment requirement, we start with the
  2768.      maximum alignment and check low bits of the symbol address
  2769.      for the minimum alignment.  */
  2770.   power_of_two = bfd_get_section_alignment (sec->owner, sec);
  2771.   mask = ((bfd_vma) 1 << power_of_two) - 1;
  2772.   while ((h->root.u.def.value & mask) != 0)
  2773.     {
  2774.        mask >>= 1;
  2775.        --power_of_two;
  2776.     }
  2777.  
  2778.   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
  2779.                                                 dynbss))
  2780.     {
  2781.       /* Adjust the section alignment if needed.  */
  2782.       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
  2783.                                        power_of_two))
  2784.         return FALSE;
  2785.     }
  2786.  
  2787.   /* We make sure that the symbol will be aligned properly.  */
  2788.   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
  2789.  
  2790.   /* Define the symbol as being at this point in DYNBSS.  */
  2791.   h->root.u.def.section = dynbss;
  2792.   h->root.u.def.value = dynbss->size;
  2793.  
  2794.   /* Increment the size of DYNBSS to make room for the symbol.  */
  2795.   dynbss->size += h->size;
  2796.  
  2797.   /* No error if extern_protected_data is true.  */
  2798.   if (h->protected_def
  2799.       && (!info->extern_protected_data
  2800.           || (info->extern_protected_data < 0
  2801.               && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
  2802.     info->callbacks->einfo
  2803.       (_("%P: copy reloc against protected `%T' is dangerous\n"),
  2804.        h->root.root.string);
  2805.  
  2806.   return TRUE;
  2807. }
  2808.  
  2809. /* Adjust all external symbols pointing into SEC_MERGE sections
  2810.    to reflect the object merging within the sections.  */
  2811.  
  2812. static bfd_boolean
  2813. _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
  2814. {
  2815.   asection *sec;
  2816.  
  2817.   if ((h->root.type == bfd_link_hash_defined
  2818.        || h->root.type == bfd_link_hash_defweak)
  2819.       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
  2820.       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  2821.     {
  2822.       bfd *output_bfd = (bfd *) data;
  2823.  
  2824.       h->root.u.def.value =
  2825.         _bfd_merged_section_offset (output_bfd,
  2826.                                     &h->root.u.def.section,
  2827.                                     elf_section_data (sec)->sec_info,
  2828.                                     h->root.u.def.value);
  2829.     }
  2830.  
  2831.   return TRUE;
  2832. }
  2833.  
  2834. /* Returns false if the symbol referred to by H should be considered
  2835.    to resolve local to the current module, and true if it should be
  2836.    considered to bind dynamically.  */
  2837.  
  2838. bfd_boolean
  2839. _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
  2840.                            struct bfd_link_info *info,
  2841.                            bfd_boolean not_local_protected)
  2842. {
  2843.   bfd_boolean binding_stays_local_p;
  2844.   const struct elf_backend_data *bed;
  2845.   struct elf_link_hash_table *hash_table;
  2846.  
  2847.   if (h == NULL)
  2848.     return FALSE;
  2849.  
  2850.   while (h->root.type == bfd_link_hash_indirect
  2851.          || h->root.type == bfd_link_hash_warning)
  2852.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2853.  
  2854.   /* If it was forced local, then clearly it's not dynamic.  */
  2855.   if (h->dynindx == -1)
  2856.     return FALSE;
  2857.   if (h->forced_local)
  2858.     return FALSE;
  2859.  
  2860.   /* Identify the cases where name binding rules say that a
  2861.      visible symbol resolves locally.  */
  2862.   binding_stays_local_p = (bfd_link_executable (info)
  2863.                            || SYMBOLIC_BIND (info, h));
  2864.  
  2865.   switch (ELF_ST_VISIBILITY (h->other))
  2866.     {
  2867.     case STV_INTERNAL:
  2868.     case STV_HIDDEN:
  2869.       return FALSE;
  2870.  
  2871.     case STV_PROTECTED:
  2872.       hash_table = elf_hash_table (info);
  2873.       if (!is_elf_hash_table (hash_table))
  2874.         return FALSE;
  2875.  
  2876.       bed = get_elf_backend_data (hash_table->dynobj);
  2877.  
  2878.       /* Proper resolution for function pointer equality may require
  2879.          that these symbols perhaps be resolved dynamically, even though
  2880.          we should be resolving them to the current module.  */
  2881.       if (!not_local_protected || !bed->is_function_type (h->type))
  2882.         binding_stays_local_p = TRUE;
  2883.       break;
  2884.  
  2885.     default:
  2886.       break;
  2887.     }
  2888.  
  2889.   /* If it isn't defined locally, then clearly it's dynamic.  */
  2890.   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  2891.     return TRUE;
  2892.  
  2893.   /* Otherwise, the symbol is dynamic if binding rules don't tell
  2894.      us that it remains local.  */
  2895.   return !binding_stays_local_p;
  2896. }
  2897.  
  2898. /* Return true if the symbol referred to by H should be considered
  2899.    to resolve local to the current module, and false otherwise.  Differs
  2900.    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
  2901.    undefined symbols.  The two functions are virtually identical except
  2902.    for the place where forced_local and dynindx == -1 are tested.  If
  2903.    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
  2904.    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
  2905.    the symbol is local only for defined symbols.
  2906.    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
  2907.    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
  2908.    treatment of undefined weak symbols.  For those that do not make
  2909.    undefined weak symbols dynamic, both functions may return false.  */
  2910.  
  2911. bfd_boolean
  2912. _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
  2913.                               struct bfd_link_info *info,
  2914.                               bfd_boolean local_protected)
  2915. {
  2916.   const struct elf_backend_data *bed;
  2917.   struct elf_link_hash_table *hash_table;
  2918.  
  2919.   /* If it's a local sym, of course we resolve locally.  */
  2920.   if (h == NULL)
  2921.     return TRUE;
  2922.  
  2923.   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
  2924.   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  2925.       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  2926.     return TRUE;
  2927.  
  2928.   /* Common symbols that become definitions don't get the DEF_REGULAR
  2929.      flag set, so test it first, and don't bail out.  */
  2930.   if (ELF_COMMON_DEF_P (h))
  2931.     /* Do nothing.  */;
  2932.   /* If we don't have a definition in a regular file, then we can't
  2933.      resolve locally.  The sym is either undefined or dynamic.  */
  2934.   else if (!h->def_regular)
  2935.     return FALSE;
  2936.  
  2937.   /* Forced local symbols resolve locally.  */
  2938.   if (h->forced_local)
  2939.     return TRUE;
  2940.  
  2941.   /* As do non-dynamic symbols.  */
  2942.   if (h->dynindx == -1)
  2943.     return TRUE;
  2944.  
  2945.   /* At this point, we know the symbol is defined and dynamic.  In an
  2946.      executable it must resolve locally, likewise when building symbolic
  2947.      shared libraries.  */
  2948.   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
  2949.     return TRUE;
  2950.  
  2951.   /* Now deal with defined dynamic symbols in shared libraries.  Ones
  2952.      with default visibility might not resolve locally.  */
  2953.   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2954.     return FALSE;
  2955.  
  2956.   hash_table = elf_hash_table (info);
  2957.   if (!is_elf_hash_table (hash_table))
  2958.     return TRUE;
  2959.  
  2960.   bed = get_elf_backend_data (hash_table->dynobj);
  2961.  
  2962.   /* If extern_protected_data is false, STV_PROTECTED non-function
  2963.      symbols are local.  */
  2964.   if ((!info->extern_protected_data
  2965.        || (info->extern_protected_data < 0
  2966.            && !bed->extern_protected_data))
  2967.       && !bed->is_function_type (h->type))
  2968.     return TRUE;
  2969.  
  2970.   /* Function pointer equality tests may require that STV_PROTECTED
  2971.      symbols be treated as dynamic symbols.  If the address of a
  2972.      function not defined in an executable is set to that function's
  2973.      plt entry in the executable, then the address of the function in
  2974.      a shared library must also be the plt entry in the executable.  */
  2975.   return local_protected;
  2976. }
  2977.  
  2978. /* Caches some TLS segment info, and ensures that the TLS segment vma is
  2979.    aligned.  Returns the first TLS output section.  */
  2980.  
  2981. struct bfd_section *
  2982. _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
  2983. {
  2984.   struct bfd_section *sec, *tls;
  2985.   unsigned int align = 0;
  2986.  
  2987.   for (sec = obfd->sections; sec != NULL; sec = sec->next)
  2988.     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
  2989.       break;
  2990.   tls = sec;
  2991.  
  2992.   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
  2993.     if (sec->alignment_power > align)
  2994.       align = sec->alignment_power;
  2995.  
  2996.   elf_hash_table (info)->tls_sec = tls;
  2997.  
  2998.   /* Ensure the alignment of the first section is the largest alignment,
  2999.      so that the tls segment starts aligned.  */
  3000.   if (tls != NULL)
  3001.     tls->alignment_power = align;
  3002.  
  3003.   return tls;
  3004. }
  3005.  
  3006. /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
  3007. static bfd_boolean
  3008. is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
  3009.                                   Elf_Internal_Sym *sym)
  3010. {
  3011.   const struct elf_backend_data *bed;
  3012.  
  3013.   /* Local symbols do not count, but target specific ones might.  */
  3014.   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
  3015.       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
  3016.     return FALSE;
  3017.  
  3018.   bed = get_elf_backend_data (abfd);
  3019.   /* Function symbols do not count.  */
  3020.   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
  3021.     return FALSE;
  3022.  
  3023.   /* If the section is undefined, then so is the symbol.  */
  3024.   if (sym->st_shndx == SHN_UNDEF)
  3025.     return FALSE;
  3026.  
  3027.   /* If the symbol is defined in the common section, then
  3028.      it is a common definition and so does not count.  */
  3029.   if (bed->common_definition (sym))
  3030.     return FALSE;
  3031.  
  3032.   /* If the symbol is in a target specific section then we
  3033.      must rely upon the backend to tell us what it is.  */
  3034.   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
  3035.     /* FIXME - this function is not coded yet:
  3036.  
  3037.        return _bfd_is_global_symbol_definition (abfd, sym);
  3038.  
  3039.        Instead for now assume that the definition is not global,
  3040.        Even if this is wrong, at least the linker will behave
  3041.        in the same way that it used to do.  */
  3042.     return FALSE;
  3043.  
  3044.   return TRUE;
  3045. }
  3046.  
  3047. /* Search the symbol table of the archive element of the archive ABFD
  3048.    whose archive map contains a mention of SYMDEF, and determine if
  3049.    the symbol is defined in this element.  */
  3050. static bfd_boolean
  3051. elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
  3052. {
  3053.   Elf_Internal_Shdr * hdr;
  3054.   bfd_size_type symcount;
  3055.   bfd_size_type extsymcount;
  3056.   bfd_size_type extsymoff;
  3057.   Elf_Internal_Sym *isymbuf;
  3058.   Elf_Internal_Sym *isym;
  3059.   Elf_Internal_Sym *isymend;
  3060.   bfd_boolean result;
  3061.  
  3062.   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
  3063.   if (abfd == NULL)
  3064.     return FALSE;
  3065.  
  3066.   /* Return FALSE if the object has been claimed by plugin.  */
  3067.   if (abfd->plugin_format == bfd_plugin_yes)
  3068.     return FALSE;
  3069.  
  3070.   if (! bfd_check_format (abfd, bfd_object))
  3071.     return FALSE;
  3072.  
  3073.   /* Select the appropriate symbol table.  */
  3074.   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
  3075.     hdr = &elf_tdata (abfd)->symtab_hdr;
  3076.   else
  3077.     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  3078.  
  3079.   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
  3080.  
  3081.   /* The sh_info field of the symtab header tells us where the
  3082.      external symbols start.  We don't care about the local symbols.  */
  3083.   if (elf_bad_symtab (abfd))
  3084.     {
  3085.       extsymcount = symcount;
  3086.       extsymoff = 0;
  3087.     }
  3088.   else
  3089.     {
  3090.       extsymcount = symcount - hdr->sh_info;
  3091.       extsymoff = hdr->sh_info;
  3092.     }
  3093.  
  3094.   if (extsymcount == 0)
  3095.     return FALSE;
  3096.  
  3097.   /* Read in the symbol table.  */
  3098.   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  3099.                                   NULL, NULL, NULL);
  3100.   if (isymbuf == NULL)
  3101.     return FALSE;
  3102.  
  3103.   /* Scan the symbol table looking for SYMDEF.  */
  3104.   result = FALSE;
  3105.   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
  3106.     {
  3107.       const char *name;
  3108.  
  3109.       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  3110.                                               isym->st_name);
  3111.       if (name == NULL)
  3112.         break;
  3113.  
  3114.       if (strcmp (name, symdef->name) == 0)
  3115.         {
  3116.           result = is_global_data_symbol_definition (abfd, isym);
  3117.           break;
  3118.         }
  3119.     }
  3120.  
  3121.   free (isymbuf);
  3122.  
  3123.   return result;
  3124. }
  3125. /* Add an entry to the .dynamic table.  */
  3126.  
  3127. bfd_boolean
  3128. _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
  3129.                             bfd_vma tag,
  3130.                             bfd_vma val)
  3131. {
  3132.   struct elf_link_hash_table *hash_table;
  3133.   const struct elf_backend_data *bed;
  3134.   asection *s;
  3135.   bfd_size_type newsize;
  3136.   bfd_byte *newcontents;
  3137.   Elf_Internal_Dyn dyn;
  3138.  
  3139.   hash_table = elf_hash_table (info);
  3140.   if (! is_elf_hash_table (hash_table))
  3141.     return FALSE;
  3142.  
  3143.   bed = get_elf_backend_data (hash_table->dynobj);
  3144.   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3145.   BFD_ASSERT (s != NULL);
  3146.  
  3147.   newsize = s->size + bed->s->sizeof_dyn;
  3148.   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
  3149.   if (newcontents == NULL)
  3150.     return FALSE;
  3151.  
  3152.   dyn.d_tag = tag;
  3153.   dyn.d_un.d_val = val;
  3154.   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
  3155.  
  3156.   s->size = newsize;
  3157.   s->contents = newcontents;
  3158.  
  3159.   return TRUE;
  3160. }
  3161.  
  3162. /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
  3163.    otherwise just check whether one already exists.  Returns -1 on error,
  3164.    1 if a DT_NEEDED tag already exists, and 0 on success.  */
  3165.  
  3166. static int
  3167. elf_add_dt_needed_tag (bfd *abfd,
  3168.                        struct bfd_link_info *info,
  3169.                        const char *soname,
  3170.                        bfd_boolean do_it)
  3171. {
  3172.   struct elf_link_hash_table *hash_table;
  3173.   bfd_size_type strindex;
  3174.  
  3175.   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  3176.     return -1;
  3177.  
  3178.   hash_table = elf_hash_table (info);
  3179.   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
  3180.   if (strindex == (bfd_size_type) -1)
  3181.     return -1;
  3182.  
  3183.   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
  3184.     {
  3185.       asection *sdyn;
  3186.       const struct elf_backend_data *bed;
  3187.       bfd_byte *extdyn;
  3188.  
  3189.       bed = get_elf_backend_data (hash_table->dynobj);
  3190.       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3191.       if (sdyn != NULL)
  3192.         for (extdyn = sdyn->contents;
  3193.              extdyn < sdyn->contents + sdyn->size;
  3194.              extdyn += bed->s->sizeof_dyn)
  3195.           {
  3196.             Elf_Internal_Dyn dyn;
  3197.  
  3198.             bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
  3199.             if (dyn.d_tag == DT_NEEDED
  3200.                 && dyn.d_un.d_val == strindex)
  3201.               {
  3202.                 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
  3203.                 return 1;
  3204.               }
  3205.           }
  3206.     }
  3207.  
  3208.   if (do_it)
  3209.     {
  3210.       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
  3211.         return -1;
  3212.  
  3213.       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
  3214.         return -1;
  3215.     }
  3216.   else
  3217.     /* We were just checking for existence of the tag.  */
  3218.     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
  3219.  
  3220.   return 0;
  3221. }
  3222.  
  3223. static bfd_boolean
  3224. on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
  3225. {
  3226.   for (; needed != NULL; needed = needed->next)
  3227.     if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
  3228.         && strcmp (soname, needed->name) == 0)
  3229.       return TRUE;
  3230.  
  3231.   return FALSE;
  3232. }
  3233.  
  3234. /* Sort symbol by value, section, and size.  */
  3235. static int
  3236. elf_sort_symbol (const void *arg1, const void *arg2)
  3237. {
  3238.   const struct elf_link_hash_entry *h1;
  3239.   const struct elf_link_hash_entry *h2;
  3240.   bfd_signed_vma vdiff;
  3241.  
  3242.   h1 = *(const struct elf_link_hash_entry **) arg1;
  3243.   h2 = *(const struct elf_link_hash_entry **) arg2;
  3244.   vdiff = h1->root.u.def.value - h2->root.u.def.value;
  3245.   if (vdiff != 0)
  3246.     return vdiff > 0 ? 1 : -1;
  3247.   else
  3248.     {
  3249.       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
  3250.       if (sdiff != 0)
  3251.         return sdiff > 0 ? 1 : -1;
  3252.     }
  3253.   vdiff = h1->size - h2->size;
  3254.   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
  3255. }
  3256.  
  3257. /* This function is used to adjust offsets into .dynstr for
  3258.    dynamic symbols.  This is called via elf_link_hash_traverse.  */
  3259.  
  3260. static bfd_boolean
  3261. elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
  3262. {
  3263.   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
  3264.  
  3265.   if (h->dynindx != -1)
  3266.     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
  3267.   return TRUE;
  3268. }
  3269.  
  3270. /* Assign string offsets in .dynstr, update all structures referencing
  3271.    them.  */
  3272.  
  3273. static bfd_boolean
  3274. elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  3275. {
  3276.   struct elf_link_hash_table *hash_table = elf_hash_table (info);
  3277.   struct elf_link_local_dynamic_entry *entry;
  3278.   struct elf_strtab_hash *dynstr = hash_table->dynstr;
  3279.   bfd *dynobj = hash_table->dynobj;
  3280.   asection *sdyn;
  3281.   bfd_size_type size;
  3282.   const struct elf_backend_data *bed;
  3283.   bfd_byte *extdyn;
  3284.  
  3285.   _bfd_elf_strtab_finalize (dynstr);
  3286.   size = _bfd_elf_strtab_size (dynstr);
  3287.  
  3288.   bed = get_elf_backend_data (dynobj);
  3289.   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3290.   BFD_ASSERT (sdyn != NULL);
  3291.  
  3292.   /* Update all .dynamic entries referencing .dynstr strings.  */
  3293.   for (extdyn = sdyn->contents;
  3294.        extdyn < sdyn->contents + sdyn->size;
  3295.        extdyn += bed->s->sizeof_dyn)
  3296.     {
  3297.       Elf_Internal_Dyn dyn;
  3298.  
  3299.       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
  3300.       switch (dyn.d_tag)
  3301.         {
  3302.         case DT_STRSZ:
  3303.           dyn.d_un.d_val = size;
  3304.           break;
  3305.         case DT_NEEDED:
  3306.         case DT_SONAME:
  3307.         case DT_RPATH:
  3308.         case DT_RUNPATH:
  3309.         case DT_FILTER:
  3310.         case DT_AUXILIARY:
  3311.         case DT_AUDIT:
  3312.         case DT_DEPAUDIT:
  3313.           dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
  3314.           break;
  3315.         default:
  3316.           continue;
  3317.         }
  3318.       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
  3319.     }
  3320.  
  3321.   /* Now update local dynamic symbols.  */
  3322.   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
  3323.     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
  3324.                                                   entry->isym.st_name);
  3325.  
  3326.   /* And the rest of dynamic symbols.  */
  3327.   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
  3328.  
  3329.   /* Adjust version definitions.  */
  3330.   if (elf_tdata (output_bfd)->cverdefs)
  3331.     {
  3332.       asection *s;
  3333.       bfd_byte *p;
  3334.       bfd_size_type i;
  3335.       Elf_Internal_Verdef def;
  3336.       Elf_Internal_Verdaux defaux;
  3337.  
  3338.       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  3339.       p = s->contents;
  3340.       do
  3341.         {
  3342.           _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
  3343.                                    &def);
  3344.           p += sizeof (Elf_External_Verdef);
  3345.           if (def.vd_aux != sizeof (Elf_External_Verdef))
  3346.             continue;
  3347.           for (i = 0; i < def.vd_cnt; ++i)
  3348.             {
  3349.               _bfd_elf_swap_verdaux_in (output_bfd,
  3350.                                         (Elf_External_Verdaux *) p, &defaux);
  3351.               defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
  3352.                                                         defaux.vda_name);
  3353.               _bfd_elf_swap_verdaux_out (output_bfd,
  3354.                                          &defaux, (Elf_External_Verdaux *) p);
  3355.               p += sizeof (Elf_External_Verdaux);
  3356.             }
  3357.         }
  3358.       while (def.vd_next);
  3359.     }
  3360.  
  3361.   /* Adjust version references.  */
  3362.   if (elf_tdata (output_bfd)->verref)
  3363.     {
  3364.       asection *s;
  3365.       bfd_byte *p;
  3366.       bfd_size_type i;
  3367.       Elf_Internal_Verneed need;
  3368.       Elf_Internal_Vernaux needaux;
  3369.  
  3370.       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  3371.       p = s->contents;
  3372.       do
  3373.         {
  3374.           _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
  3375.                                     &need);
  3376.           need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
  3377.           _bfd_elf_swap_verneed_out (output_bfd, &need,
  3378.                                      (Elf_External_Verneed *) p);
  3379.           p += sizeof (Elf_External_Verneed);
  3380.           for (i = 0; i < need.vn_cnt; ++i)
  3381.             {
  3382.               _bfd_elf_swap_vernaux_in (output_bfd,
  3383.                                         (Elf_External_Vernaux *) p, &needaux);
  3384.               needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
  3385.                                                          needaux.vna_name);
  3386.               _bfd_elf_swap_vernaux_out (output_bfd,
  3387.                                          &needaux,
  3388.                                          (Elf_External_Vernaux *) p);
  3389.               p += sizeof (Elf_External_Vernaux);
  3390.             }
  3391.         }
  3392.       while (need.vn_next);
  3393.     }
  3394.  
  3395.   return TRUE;
  3396. }
  3397. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3398.    The default is to only match when the INPUT and OUTPUT are exactly
  3399.    the same target.  */
  3400.  
  3401. bfd_boolean
  3402. _bfd_elf_default_relocs_compatible (const bfd_target *input,
  3403.                                     const bfd_target *output)
  3404. {
  3405.   return input == output;
  3406. }
  3407.  
  3408. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3409.    This version is used when different targets for the same architecture
  3410.    are virtually identical.  */
  3411.  
  3412. bfd_boolean
  3413. _bfd_elf_relocs_compatible (const bfd_target *input,
  3414.                             const bfd_target *output)
  3415. {
  3416.   const struct elf_backend_data *obed, *ibed;
  3417.  
  3418.   if (input == output)
  3419.     return TRUE;
  3420.  
  3421.   ibed = xvec_get_elf_backend_data (input);
  3422.   obed = xvec_get_elf_backend_data (output);
  3423.  
  3424.   if (ibed->arch != obed->arch)
  3425.     return FALSE;
  3426.  
  3427.   /* If both backends are using this function, deem them compatible.  */
  3428.   return ibed->relocs_compatible == obed->relocs_compatible;
  3429. }
  3430.  
  3431. /* Make a special call to the linker "notice" function to tell it that
  3432.    we are about to handle an as-needed lib, or have finished
  3433.    processing the lib.  */
  3434.  
  3435. bfd_boolean
  3436. _bfd_elf_notice_as_needed (bfd *ibfd,
  3437.                            struct bfd_link_info *info,
  3438.                            enum notice_asneeded_action act)
  3439. {
  3440.   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
  3441. }
  3442.  
  3443. /* Add symbols from an ELF object file to the linker hash table.  */
  3444.  
  3445. static bfd_boolean
  3446. elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  3447. {
  3448.   Elf_Internal_Ehdr *ehdr;
  3449.   Elf_Internal_Shdr *hdr;
  3450.   bfd_size_type symcount;
  3451.   bfd_size_type extsymcount;
  3452.   bfd_size_type extsymoff;
  3453.   struct elf_link_hash_entry **sym_hash;
  3454.   bfd_boolean dynamic;
  3455.   Elf_External_Versym *extversym = NULL;
  3456.   Elf_External_Versym *ever;
  3457.   struct elf_link_hash_entry *weaks;
  3458.   struct elf_link_hash_entry **nondeflt_vers = NULL;
  3459.   bfd_size_type nondeflt_vers_cnt = 0;
  3460.   Elf_Internal_Sym *isymbuf = NULL;
  3461.   Elf_Internal_Sym *isym;
  3462.   Elf_Internal_Sym *isymend;
  3463.   const struct elf_backend_data *bed;
  3464.   bfd_boolean add_needed;
  3465.   struct elf_link_hash_table *htab;
  3466.   bfd_size_type amt;
  3467.   void *alloc_mark = NULL;
  3468.   struct bfd_hash_entry **old_table = NULL;
  3469.   unsigned int old_size = 0;
  3470.   unsigned int old_count = 0;
  3471.   void *old_tab = NULL;
  3472.   void *old_ent;
  3473.   struct bfd_link_hash_entry *old_undefs = NULL;
  3474.   struct bfd_link_hash_entry *old_undefs_tail = NULL;
  3475.   long old_dynsymcount = 0;
  3476.   bfd_size_type old_dynstr_size = 0;
  3477.   size_t tabsize = 0;
  3478.   asection *s;
  3479.   bfd_boolean just_syms;
  3480.  
  3481.   htab = elf_hash_table (info);
  3482.   bed = get_elf_backend_data (abfd);
  3483.  
  3484.   if ((abfd->flags & DYNAMIC) == 0)
  3485.     dynamic = FALSE;
  3486.   else
  3487.     {
  3488.       dynamic = TRUE;
  3489.  
  3490.       /* You can't use -r against a dynamic object.  Also, there's no
  3491.          hope of using a dynamic object which does not exactly match
  3492.          the format of the output file.  */
  3493.       if (bfd_link_relocatable (info)
  3494.           || !is_elf_hash_table (htab)
  3495.           || info->output_bfd->xvec != abfd->xvec)
  3496.         {
  3497.           if (bfd_link_relocatable (info))
  3498.             bfd_set_error (bfd_error_invalid_operation);
  3499.           else
  3500.             bfd_set_error (bfd_error_wrong_format);
  3501.           goto error_return;
  3502.         }
  3503.     }
  3504.  
  3505.   ehdr = elf_elfheader (abfd);
  3506.   if (info->warn_alternate_em
  3507.       && bed->elf_machine_code != ehdr->e_machine
  3508.       && ((bed->elf_machine_alt1 != 0
  3509.            && ehdr->e_machine == bed->elf_machine_alt1)
  3510.           || (bed->elf_machine_alt2 != 0
  3511.               && ehdr->e_machine == bed->elf_machine_alt2)))
  3512.     info->callbacks->einfo
  3513.       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
  3514.        ehdr->e_machine, abfd, bed->elf_machine_code);
  3515.  
  3516.   /* As a GNU extension, any input sections which are named
  3517.      .gnu.warning.SYMBOL are treated as warning symbols for the given
  3518.      symbol.  This differs from .gnu.warning sections, which generate
  3519.      warnings when they are included in an output file.  */
  3520.   /* PR 12761: Also generate this warning when building shared libraries.  */
  3521.   for (s = abfd->sections; s != NULL; s = s->next)
  3522.     {
  3523.       const char *name;
  3524.  
  3525.       name = bfd_get_section_name (abfd, s);
  3526.       if (CONST_STRNEQ (name, ".gnu.warning."))
  3527.         {
  3528.           char *msg;
  3529.           bfd_size_type sz;
  3530.  
  3531.           name += sizeof ".gnu.warning." - 1;
  3532.  
  3533.           /* If this is a shared object, then look up the symbol
  3534.              in the hash table.  If it is there, and it is already
  3535.              been defined, then we will not be using the entry
  3536.              from this shared object, so we don't need to warn.
  3537.              FIXME: If we see the definition in a regular object
  3538.              later on, we will warn, but we shouldn't.  The only
  3539.              fix is to keep track of what warnings we are supposed
  3540.              to emit, and then handle them all at the end of the
  3541.              link.  */
  3542.           if (dynamic)
  3543.             {
  3544.               struct elf_link_hash_entry *h;
  3545.  
  3546.               h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
  3547.  
  3548.               /* FIXME: What about bfd_link_hash_common?  */
  3549.               if (h != NULL
  3550.                   && (h->root.type == bfd_link_hash_defined
  3551.                       || h->root.type == bfd_link_hash_defweak))
  3552.                 continue;
  3553.             }
  3554.  
  3555.           sz = s->size;
  3556.           msg = (char *) bfd_alloc (abfd, sz + 1);
  3557.           if (msg == NULL)
  3558.             goto error_return;
  3559.  
  3560.           if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
  3561.             goto error_return;
  3562.  
  3563.           msg[sz] = '\0';
  3564.  
  3565.           if (! (_bfd_generic_link_add_one_symbol
  3566.                  (info, abfd, name, BSF_WARNING, s, 0, msg,
  3567.                   FALSE, bed->collect, NULL)))
  3568.             goto error_return;
  3569.  
  3570.           if (bfd_link_executable (info))
  3571.             {
  3572.               /* Clobber the section size so that the warning does
  3573.                  not get copied into the output file.  */
  3574.               s->size = 0;
  3575.  
  3576.               /* Also set SEC_EXCLUDE, so that symbols defined in
  3577.                  the warning section don't get copied to the output.  */
  3578.               s->flags |= SEC_EXCLUDE;
  3579.             }
  3580.         }
  3581.     }
  3582.  
  3583.   just_syms = ((s = abfd->sections) != NULL
  3584.                && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
  3585.  
  3586.   add_needed = TRUE;
  3587.   if (! dynamic)
  3588.     {
  3589.       /* If we are creating a shared library, create all the dynamic
  3590.          sections immediately.  We need to attach them to something,
  3591.          so we attach them to this BFD, provided it is the right
  3592.          format and is not from ld --just-symbols.  FIXME: If there
  3593.          are no input BFD's of the same format as the output, we can't
  3594.          make a shared library.  */
  3595.       if (!just_syms
  3596.           && bfd_link_pic (info)
  3597.           && is_elf_hash_table (htab)
  3598.           && info->output_bfd->xvec == abfd->xvec
  3599.           && !htab->dynamic_sections_created)
  3600.         {
  3601.           if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
  3602.             goto error_return;
  3603.         }
  3604.     }
  3605.   else if (!is_elf_hash_table (htab))
  3606.     goto error_return;
  3607.   else
  3608.     {
  3609.       const char *soname = NULL;
  3610.       char *audit = NULL;
  3611.       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
  3612.       int ret;
  3613.  
  3614.       /* ld --just-symbols and dynamic objects don't mix very well.
  3615.          ld shouldn't allow it.  */
  3616.       if (just_syms)
  3617.         abort ();
  3618.  
  3619.       /* If this dynamic lib was specified on the command line with
  3620.          --as-needed in effect, then we don't want to add a DT_NEEDED
  3621.          tag unless the lib is actually used.  Similary for libs brought
  3622.          in by another lib's DT_NEEDED.  When --no-add-needed is used
  3623.          on a dynamic lib, we don't want to add a DT_NEEDED entry for
  3624.          any dynamic library in DT_NEEDED tags in the dynamic lib at
  3625.          all.  */
  3626.       add_needed = (elf_dyn_lib_class (abfd)
  3627.                     & (DYN_AS_NEEDED | DYN_DT_NEEDED
  3628.                        | DYN_NO_NEEDED)) == 0;
  3629.  
  3630.       s = bfd_get_section_by_name (abfd, ".dynamic");
  3631.       if (s != NULL)
  3632.         {
  3633.           bfd_byte *dynbuf;
  3634.           bfd_byte *extdyn;
  3635.           unsigned int elfsec;
  3636.           unsigned long shlink;
  3637.  
  3638.           if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  3639.             {
  3640. error_free_dyn:
  3641.               free (dynbuf);
  3642.               goto error_return;
  3643.             }
  3644.  
  3645.           elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  3646.           if (elfsec == SHN_BAD)
  3647.             goto error_free_dyn;
  3648.           shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  3649.  
  3650.           for (extdyn = dynbuf;
  3651.                extdyn < dynbuf + s->size;
  3652.                extdyn += bed->s->sizeof_dyn)
  3653.             {
  3654.               Elf_Internal_Dyn dyn;
  3655.  
  3656.               bed->s->swap_dyn_in (abfd, extdyn, &dyn);
  3657.               if (dyn.d_tag == DT_SONAME)
  3658.                 {
  3659.                   unsigned int tagv = dyn.d_un.d_val;
  3660.                   soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3661.                   if (soname == NULL)
  3662.                     goto error_free_dyn;
  3663.                 }
  3664.               if (dyn.d_tag == DT_NEEDED)
  3665.                 {
  3666.                   struct bfd_link_needed_list *n, **pn;
  3667.                   char *fnm, *anm;
  3668.                   unsigned int tagv = dyn.d_un.d_val;
  3669.  
  3670.                   amt = sizeof (struct bfd_link_needed_list);
  3671.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3672.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3673.                   if (n == NULL || fnm == NULL)
  3674.                     goto error_free_dyn;
  3675.                   amt = strlen (fnm) + 1;
  3676.                   anm = (char *) bfd_alloc (abfd, amt);
  3677.                   if (anm == NULL)
  3678.                     goto error_free_dyn;
  3679.                   memcpy (anm, fnm, amt);
  3680.                   n->name = anm;
  3681.                   n->by = abfd;
  3682.                   n->next = NULL;
  3683.                   for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
  3684.                     ;
  3685.                   *pn = n;
  3686.                 }
  3687.               if (dyn.d_tag == DT_RUNPATH)
  3688.                 {
  3689.                   struct bfd_link_needed_list *n, **pn;
  3690.                   char *fnm, *anm;
  3691.                   unsigned int tagv = dyn.d_un.d_val;
  3692.  
  3693.                   amt = sizeof (struct bfd_link_needed_list);
  3694.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3695.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3696.                   if (n == NULL || fnm == NULL)
  3697.                     goto error_free_dyn;
  3698.                   amt = strlen (fnm) + 1;
  3699.                   anm = (char *) bfd_alloc (abfd, amt);
  3700.                   if (anm == NULL)
  3701.                     goto error_free_dyn;
  3702.                   memcpy (anm, fnm, amt);
  3703.                   n->name = anm;
  3704.                   n->by = abfd;
  3705.                   n->next = NULL;
  3706.                   for (pn = & runpath;
  3707.                        *pn != NULL;
  3708.                        pn = &(*pn)->next)
  3709.                     ;
  3710.                   *pn = n;
  3711.                 }
  3712.               /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
  3713.               if (!runpath && dyn.d_tag == DT_RPATH)
  3714.                 {
  3715.                   struct bfd_link_needed_list *n, **pn;
  3716.                   char *fnm, *anm;
  3717.                   unsigned int tagv = dyn.d_un.d_val;
  3718.  
  3719.                   amt = sizeof (struct bfd_link_needed_list);
  3720.                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3721.                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3722.                   if (n == NULL || fnm == NULL)
  3723.                     goto error_free_dyn;
  3724.                   amt = strlen (fnm) + 1;
  3725.                   anm = (char *) bfd_alloc (abfd, amt);
  3726.                   if (anm == NULL)
  3727.                     goto error_free_dyn;
  3728.                   memcpy (anm, fnm, amt);
  3729.                   n->name = anm;
  3730.                   n->by = abfd;
  3731.                   n->next = NULL;
  3732.                   for (pn = & rpath;
  3733.                        *pn != NULL;
  3734.                        pn = &(*pn)->next)
  3735.                     ;
  3736.                   *pn = n;
  3737.                 }
  3738.               if (dyn.d_tag == DT_AUDIT)
  3739.                 {
  3740.                   unsigned int tagv = dyn.d_un.d_val;
  3741.                   audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3742.                 }
  3743.             }
  3744.  
  3745.           free (dynbuf);
  3746.         }
  3747.  
  3748.       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
  3749.          frees all more recently bfd_alloc'd blocks as well.  */
  3750.       if (runpath)
  3751.         rpath = runpath;
  3752.  
  3753.       if (rpath)
  3754.         {
  3755.           struct bfd_link_needed_list **pn;
  3756.           for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
  3757.             ;
  3758.           *pn = rpath;
  3759.         }
  3760.  
  3761.       /* We do not want to include any of the sections in a dynamic
  3762.          object in the output file.  We hack by simply clobbering the
  3763.          list of sections in the BFD.  This could be handled more
  3764.          cleanly by, say, a new section flag; the existing
  3765.          SEC_NEVER_LOAD flag is not the one we want, because that one
  3766.          still implies that the section takes up space in the output
  3767.          file.  */
  3768.       bfd_section_list_clear (abfd);
  3769.  
  3770.       /* Find the name to use in a DT_NEEDED entry that refers to this
  3771.          object.  If the object has a DT_SONAME entry, we use it.
  3772.          Otherwise, if the generic linker stuck something in
  3773.          elf_dt_name, we use that.  Otherwise, we just use the file
  3774.          name.  */
  3775.       if (soname == NULL || *soname == '\0')
  3776.         {
  3777.           soname = elf_dt_name (abfd);
  3778.           if (soname == NULL || *soname == '\0')
  3779.             soname = bfd_get_filename (abfd);
  3780.         }
  3781.  
  3782.       /* Save the SONAME because sometimes the linker emulation code
  3783.          will need to know it.  */
  3784.       elf_dt_name (abfd) = soname;
  3785.  
  3786.       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
  3787.       if (ret < 0)
  3788.         goto error_return;
  3789.  
  3790.       /* If we have already included this dynamic object in the
  3791.          link, just ignore it.  There is no reason to include a
  3792.          particular dynamic object more than once.  */
  3793.       if (ret > 0)
  3794.         return TRUE;
  3795.  
  3796.       /* Save the DT_AUDIT entry for the linker emulation code. */
  3797.       elf_dt_audit (abfd) = audit;
  3798.     }
  3799.  
  3800.   /* If this is a dynamic object, we always link against the .dynsym
  3801.      symbol table, not the .symtab symbol table.  The dynamic linker
  3802.      will only see the .dynsym symbol table, so there is no reason to
  3803.      look at .symtab for a dynamic object.  */
  3804.  
  3805.   if (! dynamic || elf_dynsymtab (abfd) == 0)
  3806.     hdr = &elf_tdata (abfd)->symtab_hdr;
  3807.   else
  3808.     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  3809.  
  3810.   symcount = hdr->sh_size / bed->s->sizeof_sym;
  3811.  
  3812.   /* The sh_info field of the symtab header tells us where the
  3813.      external symbols start.  We don't care about the local symbols at
  3814.      this point.  */
  3815.   if (elf_bad_symtab (abfd))
  3816.     {
  3817.       extsymcount = symcount;
  3818.       extsymoff = 0;
  3819.     }
  3820.   else
  3821.     {
  3822.       extsymcount = symcount - hdr->sh_info;
  3823.       extsymoff = hdr->sh_info;
  3824.     }
  3825.  
  3826.   sym_hash = elf_sym_hashes (abfd);
  3827.   if (extsymcount != 0)
  3828.     {
  3829.       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  3830.                                       NULL, NULL, NULL);
  3831.       if (isymbuf == NULL)
  3832.         goto error_return;
  3833.  
  3834.       if (sym_hash == NULL)
  3835.         {
  3836.           /* We store a pointer to the hash table entry for each
  3837.              external symbol.  */
  3838.           amt = extsymcount * sizeof (struct elf_link_hash_entry *);
  3839.           sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
  3840.           if (sym_hash == NULL)
  3841.             goto error_free_sym;
  3842.           elf_sym_hashes (abfd) = sym_hash;
  3843.         }
  3844.     }
  3845.  
  3846.   if (dynamic)
  3847.     {
  3848.       /* Read in any version definitions.  */
  3849.       if (!_bfd_elf_slurp_version_tables (abfd,
  3850.                                           info->default_imported_symver))
  3851.         goto error_free_sym;
  3852.  
  3853.       /* Read in the symbol versions, but don't bother to convert them
  3854.          to internal format.  */
  3855.       if (elf_dynversym (abfd) != 0)
  3856.         {
  3857.           Elf_Internal_Shdr *versymhdr;
  3858.  
  3859.           versymhdr = &elf_tdata (abfd)->dynversym_hdr;
  3860.           extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
  3861.           if (extversym == NULL)
  3862.             goto error_free_sym;
  3863.           amt = versymhdr->sh_size;
  3864.           if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
  3865.               || bfd_bread (extversym, amt, abfd) != amt)
  3866.             goto error_free_vers;
  3867.         }
  3868.     }
  3869.  
  3870.   /* If we are loading an as-needed shared lib, save the symbol table
  3871.      state before we start adding symbols.  If the lib turns out
  3872.      to be unneeded, restore the state.  */
  3873.   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  3874.     {
  3875.       unsigned int i;
  3876.       size_t entsize;
  3877.  
  3878.       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
  3879.         {
  3880.           struct bfd_hash_entry *p;
  3881.           struct elf_link_hash_entry *h;
  3882.  
  3883.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  3884.             {
  3885.               h = (struct elf_link_hash_entry *) p;
  3886.               entsize += htab->root.table.entsize;
  3887.               if (h->root.type == bfd_link_hash_warning)
  3888.                 entsize += htab->root.table.entsize;
  3889.             }
  3890.         }
  3891.  
  3892.       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
  3893.       old_tab = bfd_malloc (tabsize + entsize);
  3894.       if (old_tab == NULL)
  3895.         goto error_free_vers;
  3896.  
  3897.       /* Remember the current objalloc pointer, so that all mem for
  3898.          symbols added can later be reclaimed.  */
  3899.       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
  3900.       if (alloc_mark == NULL)
  3901.         goto error_free_vers;
  3902.  
  3903.       /* Make a special call to the linker "notice" function to
  3904.          tell it that we are about to handle an as-needed lib.  */
  3905.       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
  3906.         goto error_free_vers;
  3907.  
  3908.       /* Clone the symbol table.  Remember some pointers into the
  3909.          symbol table, and dynamic symbol count.  */
  3910.       old_ent = (char *) old_tab + tabsize;
  3911.       memcpy (old_tab, htab->root.table.table, tabsize);
  3912.       old_undefs = htab->root.undefs;
  3913.       old_undefs_tail = htab->root.undefs_tail;
  3914.       old_table = htab->root.table.table;
  3915.       old_size = htab->root.table.size;
  3916.       old_count = htab->root.table.count;
  3917.       old_dynsymcount = htab->dynsymcount;
  3918.       old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
  3919.  
  3920.       for (i = 0; i < htab->root.table.size; i++)
  3921.         {
  3922.           struct bfd_hash_entry *p;
  3923.           struct elf_link_hash_entry *h;
  3924.  
  3925.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  3926.             {
  3927.               memcpy (old_ent, p, htab->root.table.entsize);
  3928.               old_ent = (char *) old_ent + htab->root.table.entsize;
  3929.               h = (struct elf_link_hash_entry *) p;
  3930.               if (h->root.type == bfd_link_hash_warning)
  3931.                 {
  3932.                   memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
  3933.                   old_ent = (char *) old_ent + htab->root.table.entsize;
  3934.                 }
  3935.             }
  3936.         }
  3937.     }
  3938.  
  3939.   weaks = NULL;
  3940.   ever = extversym != NULL ? extversym + extsymoff : NULL;
  3941.   for (isym = isymbuf, isymend = isymbuf + extsymcount;
  3942.        isym < isymend;
  3943.        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
  3944.     {
  3945.       int bind;
  3946.       bfd_vma value;
  3947.       asection *sec, *new_sec;
  3948.       flagword flags;
  3949.       const char *name;
  3950.       struct elf_link_hash_entry *h;
  3951.       struct elf_link_hash_entry *hi;
  3952.       bfd_boolean definition;
  3953.       bfd_boolean size_change_ok;
  3954.       bfd_boolean type_change_ok;
  3955.       bfd_boolean new_weakdef;
  3956.       bfd_boolean new_weak;
  3957.       bfd_boolean old_weak;
  3958.       bfd_boolean override;
  3959.       bfd_boolean common;
  3960.       unsigned int old_alignment;
  3961.       bfd *old_bfd;
  3962.       bfd_boolean matched;
  3963.  
  3964.       override = FALSE;
  3965.  
  3966.       flags = BSF_NO_FLAGS;
  3967.       sec = NULL;
  3968.       value = isym->st_value;
  3969.       common = bed->common_definition (isym);
  3970.  
  3971.       bind = ELF_ST_BIND (isym->st_info);
  3972.       switch (bind)
  3973.         {
  3974.         case STB_LOCAL:
  3975.           /* This should be impossible, since ELF requires that all
  3976.              global symbols follow all local symbols, and that sh_info
  3977.              point to the first global symbol.  Unfortunately, Irix 5
  3978.              screws this up.  */
  3979.           continue;
  3980.  
  3981.         case STB_GLOBAL:
  3982.           if (isym->st_shndx != SHN_UNDEF && !common)
  3983.             flags = BSF_GLOBAL;
  3984.           break;
  3985.  
  3986.         case STB_WEAK:
  3987.           flags = BSF_WEAK;
  3988.           break;
  3989.  
  3990.         case STB_GNU_UNIQUE:
  3991.           flags = BSF_GNU_UNIQUE;
  3992.           break;
  3993.  
  3994.         default:
  3995.           /* Leave it up to the processor backend.  */
  3996.           break;
  3997.         }
  3998.  
  3999.       if (isym->st_shndx == SHN_UNDEF)
  4000.         sec = bfd_und_section_ptr;
  4001.       else if (isym->st_shndx == SHN_ABS)
  4002.         sec = bfd_abs_section_ptr;
  4003.       else if (isym->st_shndx == SHN_COMMON)
  4004.         {
  4005.           sec = bfd_com_section_ptr;
  4006.           /* What ELF calls the size we call the value.  What ELF
  4007.              calls the value we call the alignment.  */
  4008.           value = isym->st_size;
  4009.         }
  4010.       else
  4011.         {
  4012.           sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4013.           if (sec == NULL)
  4014.             sec = bfd_abs_section_ptr;
  4015.           else if (discarded_section (sec))
  4016.             {
  4017.               /* Symbols from discarded section are undefined.  We keep
  4018.                  its visibility.  */
  4019.               sec = bfd_und_section_ptr;
  4020.               isym->st_shndx = SHN_UNDEF;
  4021.             }
  4022.           else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  4023.             value -= sec->vma;
  4024.         }
  4025.  
  4026.       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  4027.                                               isym->st_name);
  4028.       if (name == NULL)
  4029.         goto error_free_vers;
  4030.  
  4031.       if (isym->st_shndx == SHN_COMMON
  4032.           && (abfd->flags & BFD_PLUGIN) != 0)
  4033.         {
  4034.           asection *xc = bfd_get_section_by_name (abfd, "COMMON");
  4035.  
  4036.           if (xc == NULL)
  4037.             {
  4038.               flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
  4039.                                  | SEC_EXCLUDE);
  4040.               xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
  4041.               if (xc == NULL)
  4042.                 goto error_free_vers;
  4043.             }
  4044.           sec = xc;
  4045.         }
  4046.       else if (isym->st_shndx == SHN_COMMON
  4047.                && ELF_ST_TYPE (isym->st_info) == STT_TLS
  4048.                && !bfd_link_relocatable (info))
  4049.         {
  4050.           asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
  4051.  
  4052.           if (tcomm == NULL)
  4053.             {
  4054.               flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
  4055.                                  | SEC_LINKER_CREATED);
  4056.               tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
  4057.               if (tcomm == NULL)
  4058.                 goto error_free_vers;
  4059.             }
  4060.           sec = tcomm;
  4061.         }
  4062.       else if (bed->elf_add_symbol_hook)
  4063.         {
  4064.           if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
  4065.                                              &sec, &value))
  4066.             goto error_free_vers;
  4067.  
  4068.           /* The hook function sets the name to NULL if this symbol
  4069.              should be skipped for some reason.  */
  4070.           if (name == NULL)
  4071.             continue;
  4072.         }
  4073.  
  4074.       /* Sanity check that all possibilities were handled.  */
  4075.       if (sec == NULL)
  4076.         {
  4077.           bfd_set_error (bfd_error_bad_value);
  4078.           goto error_free_vers;
  4079.         }
  4080.  
  4081.       /* Silently discard TLS symbols from --just-syms.  There's
  4082.          no way to combine a static TLS block with a new TLS block
  4083.          for this executable.  */
  4084.       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
  4085.           && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  4086.         continue;
  4087.  
  4088.       if (bfd_is_und_section (sec)
  4089.           || bfd_is_com_section (sec))
  4090.         definition = FALSE;
  4091.       else
  4092.         definition = TRUE;
  4093.  
  4094.       size_change_ok = FALSE;
  4095.       type_change_ok = bed->type_change_ok;
  4096.       old_weak = FALSE;
  4097.       matched = FALSE;
  4098.       old_alignment = 0;
  4099.       old_bfd = NULL;
  4100.       new_sec = sec;
  4101.  
  4102.       if (is_elf_hash_table (htab))
  4103.         {
  4104.           Elf_Internal_Versym iver;
  4105.           unsigned int vernum = 0;
  4106.           bfd_boolean skip;
  4107.  
  4108.           if (ever == NULL)
  4109.             {
  4110.               if (info->default_imported_symver)
  4111.                 /* Use the default symbol version created earlier.  */
  4112.                 iver.vs_vers = elf_tdata (abfd)->cverdefs;
  4113.               else
  4114.                 iver.vs_vers = 0;
  4115.             }
  4116.           else
  4117.             _bfd_elf_swap_versym_in (abfd, ever, &iver);
  4118.  
  4119.           vernum = iver.vs_vers & VERSYM_VERSION;
  4120.  
  4121.           /* If this is a hidden symbol, or if it is not version
  4122.              1, we append the version name to the symbol name.
  4123.              However, we do not modify a non-hidden absolute symbol
  4124.              if it is not a function, because it might be the version
  4125.              symbol itself.  FIXME: What if it isn't?  */
  4126.           if ((iver.vs_vers & VERSYM_HIDDEN) != 0
  4127.               || (vernum > 1
  4128.                   && (!bfd_is_abs_section (sec)
  4129.                       || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
  4130.             {
  4131.               const char *verstr;
  4132.               size_t namelen, verlen, newlen;
  4133.               char *newname, *p;
  4134.  
  4135.               if (isym->st_shndx != SHN_UNDEF)
  4136.                 {
  4137.                   if (vernum > elf_tdata (abfd)->cverdefs)
  4138.                     verstr = NULL;
  4139.                   else if (vernum > 1)
  4140.                     verstr =
  4141.                       elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
  4142.                   else
  4143.                     verstr = "";
  4144.  
  4145.                   if (verstr == NULL)
  4146.                     {
  4147.                       (*_bfd_error_handler)
  4148.                         (_("%B: %s: invalid version %u (max %d)"),
  4149.                          abfd, name, vernum,
  4150.                          elf_tdata (abfd)->cverdefs);
  4151.                       bfd_set_error (bfd_error_bad_value);
  4152.                       goto error_free_vers;
  4153.                     }
  4154.                 }
  4155.               else
  4156.                 {
  4157.                   /* We cannot simply test for the number of
  4158.                      entries in the VERNEED section since the
  4159.                      numbers for the needed versions do not start
  4160.                      at 0.  */
  4161.                   Elf_Internal_Verneed *t;
  4162.  
  4163.                   verstr = NULL;
  4164.                   for (t = elf_tdata (abfd)->verref;
  4165.                        t != NULL;
  4166.                        t = t->vn_nextref)
  4167.                     {
  4168.                       Elf_Internal_Vernaux *a;
  4169.  
  4170.                       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  4171.                         {
  4172.                           if (a->vna_other == vernum)
  4173.                             {
  4174.                               verstr = a->vna_nodename;
  4175.                               break;
  4176.                             }
  4177.                         }
  4178.                       if (a != NULL)
  4179.                         break;
  4180.                     }
  4181.                   if (verstr == NULL)
  4182.                     {
  4183.                       (*_bfd_error_handler)
  4184.                         (_("%B: %s: invalid needed version %d"),
  4185.                          abfd, name, vernum);
  4186.                       bfd_set_error (bfd_error_bad_value);
  4187.                       goto error_free_vers;
  4188.                     }
  4189.                 }
  4190.  
  4191.               namelen = strlen (name);
  4192.               verlen = strlen (verstr);
  4193.               newlen = namelen + verlen + 2;
  4194.               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4195.                   && isym->st_shndx != SHN_UNDEF)
  4196.                 ++newlen;
  4197.  
  4198.               newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
  4199.               if (newname == NULL)
  4200.                 goto error_free_vers;
  4201.               memcpy (newname, name, namelen);
  4202.               p = newname + namelen;
  4203.               *p++ = ELF_VER_CHR;
  4204.               /* If this is a defined non-hidden version symbol,
  4205.                  we add another @ to the name.  This indicates the
  4206.                  default version of the symbol.  */
  4207.               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4208.                   && isym->st_shndx != SHN_UNDEF)
  4209.                 *p++ = ELF_VER_CHR;
  4210.               memcpy (p, verstr, verlen + 1);
  4211.  
  4212.               name = newname;
  4213.             }
  4214.  
  4215.           /* If this symbol has default visibility and the user has
  4216.              requested we not re-export it, then mark it as hidden.  */
  4217.           if (!bfd_is_und_section (sec)
  4218.               && !dynamic
  4219.               && abfd->no_export
  4220.               && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
  4221.             isym->st_other = (STV_HIDDEN
  4222.                               | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
  4223.  
  4224.           if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
  4225.                                       sym_hash, &old_bfd, &old_weak,
  4226.                                       &old_alignment, &skip, &override,
  4227.                                       &type_change_ok, &size_change_ok,
  4228.                                       &matched))
  4229.             goto error_free_vers;
  4230.  
  4231.           if (skip)
  4232.             continue;
  4233.  
  4234.           /* Override a definition only if the new symbol matches the
  4235.              existing one.  */
  4236.           if (override && matched)
  4237.             definition = FALSE;
  4238.  
  4239.           h = *sym_hash;
  4240.           while (h->root.type == bfd_link_hash_indirect
  4241.                  || h->root.type == bfd_link_hash_warning)
  4242.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4243.  
  4244.           if (elf_tdata (abfd)->verdef != NULL
  4245.               && vernum > 1
  4246.               && definition)
  4247.             h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
  4248.         }
  4249.  
  4250.       if (! (_bfd_generic_link_add_one_symbol
  4251.              (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
  4252.               (struct bfd_link_hash_entry **) sym_hash)))
  4253.         goto error_free_vers;
  4254.  
  4255.       h = *sym_hash;
  4256.       /* We need to make sure that indirect symbol dynamic flags are
  4257.          updated.  */
  4258.       hi = h;
  4259.       while (h->root.type == bfd_link_hash_indirect
  4260.              || h->root.type == bfd_link_hash_warning)
  4261.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4262.  
  4263.       *sym_hash = h;
  4264.  
  4265.       new_weak = (flags & BSF_WEAK) != 0;
  4266.       new_weakdef = FALSE;
  4267.       if (dynamic
  4268.           && definition
  4269.           && new_weak
  4270.           && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
  4271.           && is_elf_hash_table (htab)
  4272.           && h->u.weakdef == NULL)
  4273.         {
  4274.           /* Keep a list of all weak defined non function symbols from
  4275.              a dynamic object, using the weakdef field.  Later in this
  4276.              function we will set the weakdef field to the correct
  4277.              value.  We only put non-function symbols from dynamic
  4278.              objects on this list, because that happens to be the only
  4279.              time we need to know the normal symbol corresponding to a
  4280.              weak symbol, and the information is time consuming to
  4281.              figure out.  If the weakdef field is not already NULL,
  4282.              then this symbol was already defined by some previous
  4283.              dynamic object, and we will be using that previous
  4284.              definition anyhow.  */
  4285.  
  4286.           h->u.weakdef = weaks;
  4287.           weaks = h;
  4288.           new_weakdef = TRUE;
  4289.         }
  4290.  
  4291.       /* Set the alignment of a common symbol.  */
  4292.       if ((common || bfd_is_com_section (sec))
  4293.           && h->root.type == bfd_link_hash_common)
  4294.         {
  4295.           unsigned int align;
  4296.  
  4297.           if (common)
  4298.             align = bfd_log2 (isym->st_value);
  4299.           else
  4300.             {
  4301.               /* The new symbol is a common symbol in a shared object.
  4302.                  We need to get the alignment from the section.  */
  4303.               align = new_sec->alignment_power;
  4304.             }
  4305.           if (align > old_alignment)
  4306.             h->root.u.c.p->alignment_power = align;
  4307.           else
  4308.             h->root.u.c.p->alignment_power = old_alignment;
  4309.         }
  4310.  
  4311.       if (is_elf_hash_table (htab))
  4312.         {
  4313.           /* Set a flag in the hash table entry indicating the type of
  4314.              reference or definition we just found.  A dynamic symbol
  4315.              is one which is referenced or defined by both a regular
  4316.              object and a shared object.  */
  4317.           bfd_boolean dynsym = FALSE;
  4318.  
  4319.           /* Plugin symbols aren't normal.  Don't set def_regular or
  4320.              ref_regular for them, or make them dynamic.  */
  4321.           if ((abfd->flags & BFD_PLUGIN) != 0)
  4322.             ;
  4323.           else if (! dynamic)
  4324.             {
  4325.               if (! definition)
  4326.                 {
  4327.                   h->ref_regular = 1;
  4328.                   if (bind != STB_WEAK)
  4329.                     h->ref_regular_nonweak = 1;
  4330.                 }
  4331.               else
  4332.                 {
  4333.                   h->def_regular = 1;
  4334.                   if (h->def_dynamic)
  4335.                     {
  4336.                       h->def_dynamic = 0;
  4337.                       h->ref_dynamic = 1;
  4338.                     }
  4339.                 }
  4340.  
  4341.               /* If the indirect symbol has been forced local, don't
  4342.                  make the real symbol dynamic.  */
  4343.               if ((h == hi || !hi->forced_local)
  4344.                   && (bfd_link_dll (info)
  4345.                       || h->def_dynamic
  4346.                       || h->ref_dynamic))
  4347.                 dynsym = TRUE;
  4348.             }
  4349.           else
  4350.             {
  4351.               if (! definition)
  4352.                 {
  4353.                   h->ref_dynamic = 1;
  4354.                   hi->ref_dynamic = 1;
  4355.                 }
  4356.               else
  4357.                 {
  4358.                   h->def_dynamic = 1;
  4359.                   hi->def_dynamic = 1;
  4360.                 }
  4361.  
  4362.               /* If the indirect symbol has been forced local, don't
  4363.                  make the real symbol dynamic.  */
  4364.               if ((h == hi || !hi->forced_local)
  4365.                   && (h->def_regular
  4366.                       || h->ref_regular
  4367.                       || (h->u.weakdef != NULL
  4368.                           && ! new_weakdef
  4369.                           && h->u.weakdef->dynindx != -1)))
  4370.                 dynsym = TRUE;
  4371.             }
  4372.  
  4373.           /* Check to see if we need to add an indirect symbol for
  4374.              the default name.  */
  4375.           if (definition
  4376.               || (!override && h->root.type == bfd_link_hash_common))
  4377.             if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
  4378.                                               sec, value, &old_bfd, &dynsym))
  4379.               goto error_free_vers;
  4380.  
  4381.           /* Check the alignment when a common symbol is involved. This
  4382.              can change when a common symbol is overridden by a normal
  4383.              definition or a common symbol is ignored due to the old
  4384.              normal definition. We need to make sure the maximum
  4385.              alignment is maintained.  */
  4386.           if ((old_alignment || common)
  4387.               && h->root.type != bfd_link_hash_common)
  4388.             {
  4389.               unsigned int common_align;
  4390.               unsigned int normal_align;
  4391.               unsigned int symbol_align;
  4392.               bfd *normal_bfd;
  4393.               bfd *common_bfd;
  4394.  
  4395.               BFD_ASSERT (h->root.type == bfd_link_hash_defined
  4396.                           || h->root.type == bfd_link_hash_defweak);
  4397.  
  4398.               symbol_align = ffs (h->root.u.def.value) - 1;
  4399.               if (h->root.u.def.section->owner != NULL
  4400.                   && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
  4401.                 {
  4402.                   normal_align = h->root.u.def.section->alignment_power;
  4403.                   if (normal_align > symbol_align)
  4404.                     normal_align = symbol_align;
  4405.                 }
  4406.               else
  4407.                 normal_align = symbol_align;
  4408.  
  4409.               if (old_alignment)
  4410.                 {
  4411.                   common_align = old_alignment;
  4412.                   common_bfd = old_bfd;
  4413.                   normal_bfd = abfd;
  4414.                 }
  4415.               else
  4416.                 {
  4417.                   common_align = bfd_log2 (isym->st_value);
  4418.                   common_bfd = abfd;
  4419.                   normal_bfd = old_bfd;
  4420.                 }
  4421.  
  4422.               if (normal_align < common_align)
  4423.                 {
  4424.                   /* PR binutils/2735 */
  4425.                   if (normal_bfd == NULL)
  4426.                     (*_bfd_error_handler)
  4427.                       (_("Warning: alignment %u of common symbol `%s' in %B is"
  4428.                          " greater than the alignment (%u) of its section %A"),
  4429.                        common_bfd, h->root.u.def.section,
  4430.                        1 << common_align, name, 1 << normal_align);
  4431.                   else
  4432.                     (*_bfd_error_handler)
  4433.                       (_("Warning: alignment %u of symbol `%s' in %B"
  4434.                          " is smaller than %u in %B"),
  4435.                        normal_bfd, common_bfd,
  4436.                        1 << normal_align, name, 1 << common_align);
  4437.                 }
  4438.             }
  4439.  
  4440.           /* Remember the symbol size if it isn't undefined.  */
  4441.           if (isym->st_size != 0
  4442.               && isym->st_shndx != SHN_UNDEF
  4443.               && (definition || h->size == 0))
  4444.             {
  4445.               if (h->size != 0
  4446.                   && h->size != isym->st_size
  4447.                   && ! size_change_ok)
  4448.                 (*_bfd_error_handler)
  4449.                   (_("Warning: size of symbol `%s' changed"
  4450.                      " from %lu in %B to %lu in %B"),
  4451.                    old_bfd, abfd,
  4452.                    name, (unsigned long) h->size,
  4453.                    (unsigned long) isym->st_size);
  4454.  
  4455.               h->size = isym->st_size;
  4456.             }
  4457.  
  4458.           /* If this is a common symbol, then we always want H->SIZE
  4459.              to be the size of the common symbol.  The code just above
  4460.              won't fix the size if a common symbol becomes larger.  We
  4461.              don't warn about a size change here, because that is
  4462.              covered by --warn-common.  Allow changes between different
  4463.              function types.  */
  4464.           if (h->root.type == bfd_link_hash_common)
  4465.             h->size = h->root.u.c.size;
  4466.  
  4467.           if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
  4468.               && ((definition && !new_weak)
  4469.                   || (old_weak && h->root.type == bfd_link_hash_common)
  4470.                   || h->type == STT_NOTYPE))
  4471.             {
  4472.               unsigned int type = ELF_ST_TYPE (isym->st_info);
  4473.  
  4474.               /* Turn an IFUNC symbol from a DSO into a normal FUNC
  4475.                  symbol.  */
  4476.               if (type == STT_GNU_IFUNC
  4477.                   && (abfd->flags & DYNAMIC) != 0)
  4478.                 type = STT_FUNC;
  4479.  
  4480.               if (h->type != type)
  4481.                 {
  4482.                   if (h->type != STT_NOTYPE && ! type_change_ok)
  4483.                     (*_bfd_error_handler)
  4484.                       (_("Warning: type of symbol `%s' changed"
  4485.                          " from %d to %d in %B"),
  4486.                        abfd, name, h->type, type);
  4487.  
  4488.                   h->type = type;
  4489.                 }
  4490.             }
  4491.  
  4492.           /* Merge st_other field.  */
  4493.           elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
  4494.  
  4495.           /* We don't want to make debug symbol dynamic.  */
  4496.           if (definition
  4497.               && (sec->flags & SEC_DEBUGGING)
  4498.               && !bfd_link_relocatable (info))
  4499.             dynsym = FALSE;
  4500.  
  4501.           /* Nor should we make plugin symbols dynamic.  */
  4502.           if ((abfd->flags & BFD_PLUGIN) != 0)
  4503.             dynsym = FALSE;
  4504.  
  4505.           if (definition)
  4506.             {
  4507.               h->target_internal = isym->st_target_internal;
  4508.               h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
  4509.             }
  4510.  
  4511.           if (definition && !dynamic)
  4512.             {
  4513.               char *p = strchr (name, ELF_VER_CHR);
  4514.               if (p != NULL && p[1] != ELF_VER_CHR)
  4515.                 {
  4516.                   /* Queue non-default versions so that .symver x, x@FOO
  4517.                      aliases can be checked.  */
  4518.                   if (!nondeflt_vers)
  4519.                     {
  4520.                       amt = ((isymend - isym + 1)
  4521.                              * sizeof (struct elf_link_hash_entry *));
  4522.                       nondeflt_vers
  4523.                         = (struct elf_link_hash_entry **) bfd_malloc (amt);
  4524.                       if (!nondeflt_vers)
  4525.                         goto error_free_vers;
  4526.                     }
  4527.                   nondeflt_vers[nondeflt_vers_cnt++] = h;
  4528.                 }
  4529.             }
  4530.  
  4531.           if (dynsym && h->dynindx == -1)
  4532.             {
  4533.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4534.                 goto error_free_vers;
  4535.               if (h->u.weakdef != NULL
  4536.                   && ! new_weakdef
  4537.                   && h->u.weakdef->dynindx == -1)
  4538.                 {
  4539.                   if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
  4540.                     goto error_free_vers;
  4541.                 }
  4542.             }
  4543.           else if (dynsym && h->dynindx != -1)
  4544.             /* If the symbol already has a dynamic index, but
  4545.                visibility says it should not be visible, turn it into
  4546.                a local symbol.  */
  4547.             switch (ELF_ST_VISIBILITY (h->other))
  4548.               {
  4549.               case STV_INTERNAL:
  4550.               case STV_HIDDEN:
  4551.                 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
  4552.                 dynsym = FALSE;
  4553.                 break;
  4554.               }
  4555.  
  4556.           /* Don't add DT_NEEDED for references from the dummy bfd.  */
  4557.           if (!add_needed
  4558.               && definition
  4559.               && ((dynsym
  4560.                    && h->ref_regular_nonweak
  4561.                    && (old_bfd == NULL
  4562.                        || (old_bfd->flags & BFD_PLUGIN) == 0))
  4563.                   || (h->ref_dynamic_nonweak
  4564.                       && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
  4565.                       && !on_needed_list (elf_dt_name (abfd), htab->needed))))
  4566.             {
  4567.               int ret;
  4568.               const char *soname = elf_dt_name (abfd);
  4569.  
  4570.               info->callbacks->minfo ("%!", soname, old_bfd,
  4571.                                       h->root.root.string);
  4572.  
  4573.               /* A symbol from a library loaded via DT_NEEDED of some
  4574.                  other library is referenced by a regular object.
  4575.                  Add a DT_NEEDED entry for it.  Issue an error if
  4576.                  --no-add-needed is used and the reference was not
  4577.                  a weak one.  */
  4578.               if (old_bfd != NULL
  4579.                   && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
  4580.                 {
  4581.                   (*_bfd_error_handler)
  4582.                     (_("%B: undefined reference to symbol '%s'"),
  4583.                      old_bfd, name);
  4584.                   bfd_set_error (bfd_error_missing_dso);
  4585.                   goto error_free_vers;
  4586.                 }
  4587.  
  4588.               elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
  4589.                 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
  4590.  
  4591.               add_needed = TRUE;
  4592.               ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
  4593.               if (ret < 0)
  4594.                 goto error_free_vers;
  4595.  
  4596.               BFD_ASSERT (ret == 0);
  4597.             }
  4598.         }
  4599.     }
  4600.  
  4601.   if (extversym != NULL)
  4602.     {
  4603.       free (extversym);
  4604.       extversym = NULL;
  4605.     }
  4606.  
  4607.   if (isymbuf != NULL)
  4608.     {
  4609.       free (isymbuf);
  4610.       isymbuf = NULL;
  4611.     }
  4612.  
  4613.   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  4614.     {
  4615.       unsigned int i;
  4616.  
  4617.       /* Restore the symbol table.  */
  4618.       old_ent = (char *) old_tab + tabsize;
  4619.       memset (elf_sym_hashes (abfd), 0,
  4620.               extsymcount * sizeof (struct elf_link_hash_entry *));
  4621.       htab->root.table.table = old_table;
  4622.       htab->root.table.size = old_size;
  4623.       htab->root.table.count = old_count;
  4624.       memcpy (htab->root.table.table, old_tab, tabsize);
  4625.       htab->root.undefs = old_undefs;
  4626.       htab->root.undefs_tail = old_undefs_tail;
  4627.       _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
  4628.       for (i = 0; i < htab->root.table.size; i++)
  4629.         {
  4630.           struct bfd_hash_entry *p;
  4631.           struct elf_link_hash_entry *h;
  4632.           bfd_size_type size;
  4633.           unsigned int alignment_power;
  4634.  
  4635.           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  4636.             {
  4637.               h = (struct elf_link_hash_entry *) p;
  4638.               if (h->root.type == bfd_link_hash_warning)
  4639.                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4640.               if (h->dynindx >= old_dynsymcount
  4641.                   && h->dynstr_index < old_dynstr_size)
  4642.                 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
  4643.  
  4644.               /* Preserve the maximum alignment and size for common
  4645.                  symbols even if this dynamic lib isn't on DT_NEEDED
  4646.                  since it can still be loaded at run time by another
  4647.                  dynamic lib.  */
  4648.               if (h->root.type == bfd_link_hash_common)
  4649.                 {
  4650.                   size = h->root.u.c.size;
  4651.                   alignment_power = h->root.u.c.p->alignment_power;
  4652.                 }
  4653.               else
  4654.                 {
  4655.                   size = 0;
  4656.                   alignment_power = 0;
  4657.                 }
  4658.               memcpy (p, old_ent, htab->root.table.entsize);
  4659.               old_ent = (char *) old_ent + htab->root.table.entsize;
  4660.               h = (struct elf_link_hash_entry *) p;
  4661.               if (h->root.type == bfd_link_hash_warning)
  4662.                 {
  4663.                   memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
  4664.                   old_ent = (char *) old_ent + htab->root.table.entsize;
  4665.                   h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4666.                 }
  4667.               if (h->root.type == bfd_link_hash_common)
  4668.                 {
  4669.                   if (size > h->root.u.c.size)
  4670.                     h->root.u.c.size = size;
  4671.                   if (alignment_power > h->root.u.c.p->alignment_power)
  4672.                     h->root.u.c.p->alignment_power = alignment_power;
  4673.                 }
  4674.             }
  4675.         }
  4676.  
  4677.       /* Make a special call to the linker "notice" function to
  4678.          tell it that symbols added for crefs may need to be removed.  */
  4679.       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
  4680.         goto error_free_vers;
  4681.  
  4682.       free (old_tab);
  4683.       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
  4684.                            alloc_mark);
  4685.       if (nondeflt_vers != NULL)
  4686.         free (nondeflt_vers);
  4687.       return TRUE;
  4688.     }
  4689.  
  4690.   if (old_tab != NULL)
  4691.     {
  4692.       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
  4693.         goto error_free_vers;
  4694.       free (old_tab);
  4695.       old_tab = NULL;
  4696.     }
  4697.  
  4698.   /* Now that all the symbols from this input file are created, if
  4699.      not performing a relocatable link, handle .symver foo, foo@BAR
  4700.      such that any relocs against foo become foo@BAR.  */
  4701.   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
  4702.     {
  4703.       bfd_size_type cnt, symidx;
  4704.  
  4705.       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
  4706.         {
  4707.           struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
  4708.           char *shortname, *p;
  4709.  
  4710.           p = strchr (h->root.root.string, ELF_VER_CHR);
  4711.           if (p == NULL
  4712.               || (h->root.type != bfd_link_hash_defined
  4713.                   && h->root.type != bfd_link_hash_defweak))
  4714.             continue;
  4715.  
  4716.           amt = p - h->root.root.string;
  4717.           shortname = (char *) bfd_malloc (amt + 1);
  4718.           if (!shortname)
  4719.             goto error_free_vers;
  4720.           memcpy (shortname, h->root.root.string, amt);
  4721.           shortname[amt] = '\0';
  4722.  
  4723.           hi = (struct elf_link_hash_entry *)
  4724.                bfd_link_hash_lookup (&htab->root, shortname,
  4725.                                      FALSE, FALSE, FALSE);
  4726.           if (hi != NULL
  4727.               && hi->root.type == h->root.type
  4728.               && hi->root.u.def.value == h->root.u.def.value
  4729.               && hi->root.u.def.section == h->root.u.def.section)
  4730.             {
  4731.               (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
  4732.               hi->root.type = bfd_link_hash_indirect;
  4733.               hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
  4734.               (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  4735.               sym_hash = elf_sym_hashes (abfd);
  4736.               if (sym_hash)
  4737.                 for (symidx = 0; symidx < extsymcount; ++symidx)
  4738.                   if (sym_hash[symidx] == hi)
  4739.                     {
  4740.                       sym_hash[symidx] = h;
  4741.                       break;
  4742.                     }
  4743.             }
  4744.           free (shortname);
  4745.         }
  4746.       free (nondeflt_vers);
  4747.       nondeflt_vers = NULL;
  4748.     }
  4749.  
  4750.   /* Now set the weakdefs field correctly for all the weak defined
  4751.      symbols we found.  The only way to do this is to search all the
  4752.      symbols.  Since we only need the information for non functions in
  4753.      dynamic objects, that's the only time we actually put anything on
  4754.      the list WEAKS.  We need this information so that if a regular
  4755.      object refers to a symbol defined weakly in a dynamic object, the
  4756.      real symbol in the dynamic object is also put in the dynamic
  4757.      symbols; we also must arrange for both symbols to point to the
  4758.      same memory location.  We could handle the general case of symbol
  4759.      aliasing, but a general symbol alias can only be generated in
  4760.      assembler code, handling it correctly would be very time
  4761.      consuming, and other ELF linkers don't handle general aliasing
  4762.      either.  */
  4763.   if (weaks != NULL)
  4764.     {
  4765.       struct elf_link_hash_entry **hpp;
  4766.       struct elf_link_hash_entry **hppend;
  4767.       struct elf_link_hash_entry **sorted_sym_hash;
  4768.       struct elf_link_hash_entry *h;
  4769.       size_t sym_count;
  4770.  
  4771.       /* Since we have to search the whole symbol list for each weak
  4772.          defined symbol, search time for N weak defined symbols will be
  4773.          O(N^2). Binary search will cut it down to O(NlogN).  */
  4774.       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
  4775.       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
  4776.       if (sorted_sym_hash == NULL)
  4777.         goto error_return;
  4778.       sym_hash = sorted_sym_hash;
  4779.       hpp = elf_sym_hashes (abfd);
  4780.       hppend = hpp + extsymcount;
  4781.       sym_count = 0;
  4782.       for (; hpp < hppend; hpp++)
  4783.         {
  4784.           h = *hpp;
  4785.           if (h != NULL
  4786.               && h->root.type == bfd_link_hash_defined
  4787.               && !bed->is_function_type (h->type))
  4788.             {
  4789.               *sym_hash = h;
  4790.               sym_hash++;
  4791.               sym_count++;
  4792.             }
  4793.         }
  4794.  
  4795.       qsort (sorted_sym_hash, sym_count,
  4796.              sizeof (struct elf_link_hash_entry *),
  4797.              elf_sort_symbol);
  4798.  
  4799.       while (weaks != NULL)
  4800.         {
  4801.           struct elf_link_hash_entry *hlook;
  4802.           asection *slook;
  4803.           bfd_vma vlook;
  4804.           size_t i, j, idx = 0;
  4805.  
  4806.           hlook = weaks;
  4807.           weaks = hlook->u.weakdef;
  4808.           hlook->u.weakdef = NULL;
  4809.  
  4810.           BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
  4811.                       || hlook->root.type == bfd_link_hash_defweak
  4812.                       || hlook->root.type == bfd_link_hash_common
  4813.                       || hlook->root.type == bfd_link_hash_indirect);
  4814.           slook = hlook->root.u.def.section;
  4815.           vlook = hlook->root.u.def.value;
  4816.  
  4817.           i = 0;
  4818.           j = sym_count;
  4819.           while (i != j)
  4820.             {
  4821.               bfd_signed_vma vdiff;
  4822.               idx = (i + j) / 2;
  4823.               h = sorted_sym_hash[idx];
  4824.               vdiff = vlook - h->root.u.def.value;
  4825.               if (vdiff < 0)
  4826.                 j = idx;
  4827.               else if (vdiff > 0)
  4828.                 i = idx + 1;
  4829.               else
  4830.                 {
  4831.                   int sdiff = slook->id - h->root.u.def.section->id;
  4832.                   if (sdiff < 0)
  4833.                     j = idx;
  4834.                   else if (sdiff > 0)
  4835.                     i = idx + 1;
  4836.                   else
  4837.                     break;
  4838.                 }
  4839.             }
  4840.  
  4841.           /* We didn't find a value/section match.  */
  4842.           if (i == j)
  4843.             continue;
  4844.  
  4845.           /* With multiple aliases, or when the weak symbol is already
  4846.              strongly defined, we have multiple matching symbols and
  4847.              the binary search above may land on any of them.  Step
  4848.              one past the matching symbol(s).  */
  4849.           while (++idx != j)
  4850.             {
  4851.               h = sorted_sym_hash[idx];
  4852.               if (h->root.u.def.section != slook
  4853.                   || h->root.u.def.value != vlook)
  4854.                 break;
  4855.             }
  4856.  
  4857.           /* Now look back over the aliases.  Since we sorted by size
  4858.              as well as value and section, we'll choose the one with
  4859.              the largest size.  */
  4860.           while (idx-- != i)
  4861.             {
  4862.               h = sorted_sym_hash[idx];
  4863.  
  4864.               /* Stop if value or section doesn't match.  */
  4865.               if (h->root.u.def.section != slook
  4866.                   || h->root.u.def.value != vlook)
  4867.                 break;
  4868.               else if (h != hlook)
  4869.                 {
  4870.                   hlook->u.weakdef = h;
  4871.  
  4872.                   /* If the weak definition is in the list of dynamic
  4873.                      symbols, make sure the real definition is put
  4874.                      there as well.  */
  4875.                   if (hlook->dynindx != -1 && h->dynindx == -1)
  4876.                     {
  4877.                       if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4878.                         {
  4879.                         err_free_sym_hash:
  4880.                           free (sorted_sym_hash);
  4881.                           goto error_return;
  4882.                         }
  4883.                     }
  4884.  
  4885.                   /* If the real definition is in the list of dynamic
  4886.                      symbols, make sure the weak definition is put
  4887.                      there as well.  If we don't do this, then the
  4888.                      dynamic loader might not merge the entries for the
  4889.                      real definition and the weak definition.  */
  4890.                   if (h->dynindx != -1 && hlook->dynindx == -1)
  4891.                     {
  4892.                       if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
  4893.                         goto err_free_sym_hash;
  4894.                     }
  4895.                   break;
  4896.                 }
  4897.             }
  4898.         }
  4899.  
  4900.       free (sorted_sym_hash);
  4901.     }
  4902.  
  4903.   if (bed->check_directives
  4904.       && !(*bed->check_directives) (abfd, info))
  4905.     return FALSE;
  4906.  
  4907.   /* If this object is the same format as the output object, and it is
  4908.      not a shared library, then let the backend look through the
  4909.      relocs.
  4910.  
  4911.      This is required to build global offset table entries and to
  4912.      arrange for dynamic relocs.  It is not required for the
  4913.      particular common case of linking non PIC code, even when linking
  4914.      against shared libraries, but unfortunately there is no way of
  4915.      knowing whether an object file has been compiled PIC or not.
  4916.      Looking through the relocs is not particularly time consuming.
  4917.      The problem is that we must either (1) keep the relocs in memory,
  4918.      which causes the linker to require additional runtime memory or
  4919.      (2) read the relocs twice from the input file, which wastes time.
  4920.      This would be a good case for using mmap.
  4921.  
  4922.      I have no idea how to handle linking PIC code into a file of a
  4923.      different format.  It probably can't be done.  */
  4924.   if (! dynamic
  4925.       && is_elf_hash_table (htab)
  4926.       && bed->check_relocs != NULL
  4927.       && elf_object_id (abfd) == elf_hash_table_id (htab)
  4928.       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
  4929.     {
  4930.       asection *o;
  4931.  
  4932.       for (o = abfd->sections; o != NULL; o = o->next)
  4933.         {
  4934.           Elf_Internal_Rela *internal_relocs;
  4935.           bfd_boolean ok;
  4936.  
  4937.           if ((o->flags & SEC_RELOC) == 0
  4938.               || o->reloc_count == 0
  4939.               || ((info->strip == strip_all || info->strip == strip_debugger)
  4940.                   && (o->flags & SEC_DEBUGGING) != 0)
  4941.               || bfd_is_abs_section (o->output_section))
  4942.             continue;
  4943.  
  4944.           internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
  4945.                                                        info->keep_memory);
  4946.           if (internal_relocs == NULL)
  4947.             goto error_return;
  4948.  
  4949.           ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
  4950.  
  4951.           if (elf_section_data (o)->relocs != internal_relocs)
  4952.             free (internal_relocs);
  4953.  
  4954.           if (! ok)
  4955.             goto error_return;
  4956.         }
  4957.     }
  4958.  
  4959.   /* If this is a non-traditional link, try to optimize the handling
  4960.      of the .stab/.stabstr sections.  */
  4961.   if (! dynamic
  4962.       && ! info->traditional_format
  4963.       && is_elf_hash_table (htab)
  4964.       && (info->strip != strip_all && info->strip != strip_debugger))
  4965.     {
  4966.       asection *stabstr;
  4967.  
  4968.       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
  4969.       if (stabstr != NULL)
  4970.         {
  4971.           bfd_size_type string_offset = 0;
  4972.           asection *stab;
  4973.  
  4974.           for (stab = abfd->sections; stab; stab = stab->next)
  4975.             if (CONST_STRNEQ (stab->name, ".stab")
  4976.                 && (!stab->name[5] ||
  4977.                     (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
  4978.                 && (stab->flags & SEC_MERGE) == 0
  4979.                 && !bfd_is_abs_section (stab->output_section))
  4980.               {
  4981.                 struct bfd_elf_section_data *secdata;
  4982.  
  4983.                 secdata = elf_section_data (stab);
  4984.                 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
  4985.                                                stabstr, &secdata->sec_info,
  4986.                                                &string_offset))
  4987.                   goto error_return;
  4988.                 if (secdata->sec_info)
  4989.                   stab->sec_info_type = SEC_INFO_TYPE_STABS;
  4990.             }
  4991.         }
  4992.     }
  4993.  
  4994.   if (is_elf_hash_table (htab) && add_needed)
  4995.     {
  4996.       /* Add this bfd to the loaded list.  */
  4997.       struct elf_link_loaded_list *n;
  4998.  
  4999.       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
  5000.       if (n == NULL)
  5001.         goto error_return;
  5002.       n->abfd = abfd;
  5003.       n->next = htab->loaded;
  5004.       htab->loaded = n;
  5005.     }
  5006.  
  5007.   return TRUE;
  5008.  
  5009.  error_free_vers:
  5010.   if (old_tab != NULL)
  5011.     free (old_tab);
  5012.   if (nondeflt_vers != NULL)
  5013.     free (nondeflt_vers);
  5014.   if (extversym != NULL)
  5015.     free (extversym);
  5016.  error_free_sym:
  5017.   if (isymbuf != NULL)
  5018.     free (isymbuf);
  5019.  error_return:
  5020.   return FALSE;
  5021. }
  5022.  
  5023. /* Return the linker hash table entry of a symbol that might be
  5024.    satisfied by an archive symbol.  Return -1 on error.  */
  5025.  
  5026. struct elf_link_hash_entry *
  5027. _bfd_elf_archive_symbol_lookup (bfd *abfd,
  5028.                                 struct bfd_link_info *info,
  5029.                                 const char *name)
  5030. {
  5031.   struct elf_link_hash_entry *h;
  5032.   char *p, *copy;
  5033.   size_t len, first;
  5034.  
  5035.   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
  5036.   if (h != NULL)
  5037.     return h;
  5038.  
  5039.   /* If this is a default version (the name contains @@), look up the
  5040.      symbol again with only one `@' as well as without the version.
  5041.      The effect is that references to the symbol with and without the
  5042.      version will be matched by the default symbol in the archive.  */
  5043.  
  5044.   p = strchr (name, ELF_VER_CHR);
  5045.   if (p == NULL || p[1] != ELF_VER_CHR)
  5046.     return h;
  5047.  
  5048.   /* First check with only one `@'.  */
  5049.   len = strlen (name);
  5050.   copy = (char *) bfd_alloc (abfd, len);
  5051.   if (copy == NULL)
  5052.     return (struct elf_link_hash_entry *) 0 - 1;
  5053.  
  5054.   first = p - name + 1;
  5055.   memcpy (copy, name, first);
  5056.   memcpy (copy + first, name + first + 1, len - first);
  5057.  
  5058.   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
  5059.   if (h == NULL)
  5060.     {
  5061.       /* We also need to check references to the symbol without the
  5062.          version.  */
  5063.       copy[first - 1] = '\0';
  5064.       h = elf_link_hash_lookup (elf_hash_table (info), copy,
  5065.                                 FALSE, FALSE, TRUE);
  5066.     }
  5067.  
  5068.   bfd_release (abfd, copy);
  5069.   return h;
  5070. }
  5071.  
  5072. /* Add symbols from an ELF archive file to the linker hash table.  We
  5073.    don't use _bfd_generic_link_add_archive_symbols because we need to
  5074.    handle versioned symbols.
  5075.  
  5076.    Fortunately, ELF archive handling is simpler than that done by
  5077.    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
  5078.    oddities.  In ELF, if we find a symbol in the archive map, and the
  5079.    symbol is currently undefined, we know that we must pull in that
  5080.    object file.
  5081.  
  5082.    Unfortunately, we do have to make multiple passes over the symbol
  5083.    table until nothing further is resolved.  */
  5084.  
  5085. static bfd_boolean
  5086. elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
  5087. {
  5088.   symindex c;
  5089.   unsigned char *included = NULL;
  5090.   carsym *symdefs;
  5091.   bfd_boolean loop;
  5092.   bfd_size_type amt;
  5093.   const struct elf_backend_data *bed;
  5094.   struct elf_link_hash_entry * (*archive_symbol_lookup)
  5095.     (bfd *, struct bfd_link_info *, const char *);
  5096.  
  5097.   if (! bfd_has_map (abfd))
  5098.     {
  5099.       /* An empty archive is a special case.  */
  5100.       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  5101.         return TRUE;
  5102.       bfd_set_error (bfd_error_no_armap);
  5103.       return FALSE;
  5104.     }
  5105.  
  5106.   /* Keep track of all symbols we know to be already defined, and all
  5107.      files we know to be already included.  This is to speed up the
  5108.      second and subsequent passes.  */
  5109.   c = bfd_ardata (abfd)->symdef_count;
  5110.   if (c == 0)
  5111.     return TRUE;
  5112.   amt = c;
  5113.   amt *= sizeof (*included);
  5114.   included = (unsigned char *) bfd_zmalloc (amt);
  5115.   if (included == NULL)
  5116.     return FALSE;
  5117.  
  5118.   symdefs = bfd_ardata (abfd)->symdefs;
  5119.   bed = get_elf_backend_data (abfd);
  5120.   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
  5121.  
  5122.   do
  5123.     {
  5124.       file_ptr last;
  5125.       symindex i;
  5126.       carsym *symdef;
  5127.       carsym *symdefend;
  5128.  
  5129.       loop = FALSE;
  5130.       last = -1;
  5131.  
  5132.       symdef = symdefs;
  5133.       symdefend = symdef + c;
  5134.       for (i = 0; symdef < symdefend; symdef++, i++)
  5135.         {
  5136.           struct elf_link_hash_entry *h;
  5137.           bfd *element;
  5138.           struct bfd_link_hash_entry *undefs_tail;
  5139.           symindex mark;
  5140.  
  5141.           if (included[i])
  5142.             continue;
  5143.           if (symdef->file_offset == last)
  5144.             {
  5145.               included[i] = TRUE;
  5146.               continue;
  5147.             }
  5148.  
  5149.           h = archive_symbol_lookup (abfd, info, symdef->name);
  5150.           if (h == (struct elf_link_hash_entry *) 0 - 1)
  5151.             goto error_return;
  5152.  
  5153.           if (h == NULL)
  5154.             continue;
  5155.  
  5156.           if (h->root.type == bfd_link_hash_common)
  5157.             {
  5158.               /* We currently have a common symbol.  The archive map contains
  5159.                  a reference to this symbol, so we may want to include it.  We
  5160.                  only want to include it however, if this archive element
  5161.                  contains a definition of the symbol, not just another common
  5162.                  declaration of it.
  5163.  
  5164.                  Unfortunately some archivers (including GNU ar) will put
  5165.                  declarations of common symbols into their archive maps, as
  5166.                  well as real definitions, so we cannot just go by the archive
  5167.                  map alone.  Instead we must read in the element's symbol
  5168.                  table and check that to see what kind of symbol definition
  5169.                  this is.  */
  5170.               if (! elf_link_is_defined_archive_symbol (abfd, symdef))
  5171.                 continue;
  5172.             }
  5173.           else if (h->root.type != bfd_link_hash_undefined)
  5174.             {
  5175.               if (h->root.type != bfd_link_hash_undefweak)
  5176.                 /* Symbol must be defined.  Don't check it again.  */
  5177.                 included[i] = TRUE;
  5178.               continue;
  5179.             }
  5180.  
  5181.           /* We need to include this archive member.  */
  5182.           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
  5183.           if (element == NULL)
  5184.             goto error_return;
  5185.  
  5186.           if (! bfd_check_format (element, bfd_object))
  5187.             goto error_return;
  5188.  
  5189.           undefs_tail = info->hash->undefs_tail;
  5190.  
  5191.           if (!(*info->callbacks
  5192.                 ->add_archive_element) (info, element, symdef->name, &element))
  5193.             goto error_return;
  5194.           if (!bfd_link_add_symbols (element, info))
  5195.             goto error_return;
  5196.  
  5197.           /* If there are any new undefined symbols, we need to make
  5198.              another pass through the archive in order to see whether
  5199.              they can be defined.  FIXME: This isn't perfect, because
  5200.              common symbols wind up on undefs_tail and because an
  5201.              undefined symbol which is defined later on in this pass
  5202.              does not require another pass.  This isn't a bug, but it
  5203.              does make the code less efficient than it could be.  */
  5204.           if (undefs_tail != info->hash->undefs_tail)
  5205.             loop = TRUE;
  5206.  
  5207.           /* Look backward to mark all symbols from this object file
  5208.              which we have already seen in this pass.  */
  5209.           mark = i;
  5210.           do
  5211.             {
  5212.               included[mark] = TRUE;
  5213.               if (mark == 0)
  5214.                 break;
  5215.               --mark;
  5216.             }
  5217.           while (symdefs[mark].file_offset == symdef->file_offset);
  5218.  
  5219.           /* We mark subsequent symbols from this object file as we go
  5220.              on through the loop.  */
  5221.           last = symdef->file_offset;
  5222.         }
  5223.     }
  5224.   while (loop);
  5225.  
  5226.   free (included);
  5227.  
  5228.   return TRUE;
  5229.  
  5230.  error_return:
  5231.   if (included != NULL)
  5232.     free (included);
  5233.   return FALSE;
  5234. }
  5235.  
  5236. /* Given an ELF BFD, add symbols to the global hash table as
  5237.    appropriate.  */
  5238.  
  5239. bfd_boolean
  5240. bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  5241. {
  5242.   switch (bfd_get_format (abfd))
  5243.     {
  5244.     case bfd_object:
  5245.       return elf_link_add_object_symbols (abfd, info);
  5246.     case bfd_archive:
  5247.       return elf_link_add_archive_symbols (abfd, info);
  5248.     default:
  5249.       bfd_set_error (bfd_error_wrong_format);
  5250.       return FALSE;
  5251.     }
  5252. }
  5253. struct hash_codes_info
  5254. {
  5255.   unsigned long *hashcodes;
  5256.   bfd_boolean error;
  5257. };
  5258.  
  5259. /* This function will be called though elf_link_hash_traverse to store
  5260.    all hash value of the exported symbols in an array.  */
  5261.  
  5262. static bfd_boolean
  5263. elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
  5264. {
  5265.   struct hash_codes_info *inf = (struct hash_codes_info *) data;
  5266.   const char *name;
  5267.   unsigned long ha;
  5268.   char *alc = NULL;
  5269.  
  5270.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  5271.   if (h->dynindx == -1)
  5272.     return TRUE;
  5273.  
  5274.   name = h->root.root.string;
  5275.   if (h->versioned >= versioned)
  5276.     {
  5277.       char *p = strchr (name, ELF_VER_CHR);
  5278.       if (p != NULL)
  5279.         {
  5280.           alc = (char *) bfd_malloc (p - name + 1);
  5281.           if (alc == NULL)
  5282.             {
  5283.               inf->error = TRUE;
  5284.               return FALSE;
  5285.             }
  5286.           memcpy (alc, name, p - name);
  5287.           alc[p - name] = '\0';
  5288.           name = alc;
  5289.         }
  5290.     }
  5291.  
  5292.   /* Compute the hash value.  */
  5293.   ha = bfd_elf_hash (name);
  5294.  
  5295.   /* Store the found hash value in the array given as the argument.  */
  5296.   *(inf->hashcodes)++ = ha;
  5297.  
  5298.   /* And store it in the struct so that we can put it in the hash table
  5299.      later.  */
  5300.   h->u.elf_hash_value = ha;
  5301.  
  5302.   if (alc != NULL)
  5303.     free (alc);
  5304.  
  5305.   return TRUE;
  5306. }
  5307.  
  5308. struct collect_gnu_hash_codes
  5309. {
  5310.   bfd *output_bfd;
  5311.   const struct elf_backend_data *bed;
  5312.   unsigned long int nsyms;
  5313.   unsigned long int maskbits;
  5314.   unsigned long int *hashcodes;
  5315.   unsigned long int *hashval;
  5316.   unsigned long int *indx;
  5317.   unsigned long int *counts;
  5318.   bfd_vma *bitmask;
  5319.   bfd_byte *contents;
  5320.   long int min_dynindx;
  5321.   unsigned long int bucketcount;
  5322.   unsigned long int symindx;
  5323.   long int local_indx;
  5324.   long int shift1, shift2;
  5325.   unsigned long int mask;
  5326.   bfd_boolean error;
  5327. };
  5328.  
  5329. /* This function will be called though elf_link_hash_traverse to store
  5330.    all hash value of the exported symbols in an array.  */
  5331.  
  5332. static bfd_boolean
  5333. elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
  5334. {
  5335.   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5336.   const char *name;
  5337.   unsigned long ha;
  5338.   char *alc = NULL;
  5339.  
  5340.   /* Ignore indirect symbols.  These are added by the versioning code.  */
  5341.   if (h->dynindx == -1)
  5342.     return TRUE;
  5343.  
  5344.   /* Ignore also local symbols and undefined symbols.  */
  5345.   if (! (*s->bed->elf_hash_symbol) (h))
  5346.     return TRUE;
  5347.  
  5348.   name = h->root.root.string;
  5349.   if (h->versioned >= versioned)
  5350.     {
  5351.       char *p = strchr (name, ELF_VER_CHR);
  5352.       if (p != NULL)
  5353.         {
  5354.           alc = (char *) bfd_malloc (p - name + 1);
  5355.           if (alc == NULL)
  5356.             {
  5357.               s->error = TRUE;
  5358.               return FALSE;
  5359.             }
  5360.           memcpy (alc, name, p - name);
  5361.           alc[p - name] = '\0';
  5362.           name = alc;
  5363.         }
  5364.     }
  5365.  
  5366.   /* Compute the hash value.  */
  5367.   ha = bfd_elf_gnu_hash (name);
  5368.  
  5369.   /* Store the found hash value in the array for compute_bucket_count,
  5370.      and also for .dynsym reordering purposes.  */
  5371.   s->hashcodes[s->nsyms] = ha;
  5372.   s->hashval[h->dynindx] = ha;
  5373.   ++s->nsyms;
  5374.   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
  5375.     s->min_dynindx = h->dynindx;
  5376.  
  5377.   if (alc != NULL)
  5378.     free (alc);
  5379.  
  5380.   return TRUE;
  5381. }
  5382.  
  5383. /* This function will be called though elf_link_hash_traverse to do
  5384.    final dynaminc symbol renumbering.  */
  5385.  
  5386. static bfd_boolean
  5387. elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
  5388. {
  5389.   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5390.   unsigned long int bucket;
  5391.   unsigned long int val;
  5392.  
  5393.   /* Ignore indirect symbols.  */
  5394.   if (h->dynindx == -1)
  5395.     return TRUE;
  5396.  
  5397.   /* Ignore also local symbols and undefined symbols.  */
  5398.   if (! (*s->bed->elf_hash_symbol) (h))
  5399.     {
  5400.       if (h->dynindx >= s->min_dynindx)
  5401.         h->dynindx = s->local_indx++;
  5402.       return TRUE;
  5403.     }
  5404.  
  5405.   bucket = s->hashval[h->dynindx] % s->bucketcount;
  5406.   val = (s->hashval[h->dynindx] >> s->shift1)
  5407.         & ((s->maskbits >> s->shift1) - 1);
  5408.   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
  5409.   s->bitmask[val]
  5410.     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
  5411.   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
  5412.   if (s->counts[bucket] == 1)
  5413.     /* Last element terminates the chain.  */
  5414.     val |= 1;
  5415.   bfd_put_32 (s->output_bfd, val,
  5416.               s->contents + (s->indx[bucket] - s->symindx) * 4);
  5417.   --s->counts[bucket];
  5418.   h->dynindx = s->indx[bucket]++;
  5419.   return TRUE;
  5420. }
  5421.  
  5422. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
  5423.  
  5424. bfd_boolean
  5425. _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
  5426. {
  5427.   return !(h->forced_local
  5428.            || h->root.type == bfd_link_hash_undefined
  5429.            || h->root.type == bfd_link_hash_undefweak
  5430.            || ((h->root.type == bfd_link_hash_defined
  5431.                 || h->root.type == bfd_link_hash_defweak)
  5432.                && h->root.u.def.section->output_section == NULL));
  5433. }
  5434.  
  5435. /* Array used to determine the number of hash table buckets to use
  5436.    based on the number of symbols there are.  If there are fewer than
  5437.    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
  5438.    fewer than 37 we use 17 buckets, and so forth.  We never use more
  5439.    than 32771 buckets.  */
  5440.  
  5441. static const size_t elf_buckets[] =
  5442. {
  5443.   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  5444.   16411, 32771, 0
  5445. };
  5446.  
  5447. /* Compute bucket count for hashing table.  We do not use a static set
  5448.    of possible tables sizes anymore.  Instead we determine for all
  5449.    possible reasonable sizes of the table the outcome (i.e., the
  5450.    number of collisions etc) and choose the best solution.  The
  5451.    weighting functions are not too simple to allow the table to grow
  5452.    without bounds.  Instead one of the weighting factors is the size.
  5453.    Therefore the result is always a good payoff between few collisions
  5454.    (= short chain lengths) and table size.  */
  5455. static size_t
  5456. compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5457.                       unsigned long int *hashcodes ATTRIBUTE_UNUSED,
  5458.                       unsigned long int nsyms,
  5459.                       int gnu_hash)
  5460. {
  5461.   size_t best_size = 0;
  5462.   unsigned long int i;
  5463.  
  5464.   /* We have a problem here.  The following code to optimize the table
  5465.      size requires an integer type with more the 32 bits.  If
  5466.      BFD_HOST_U_64_BIT is set we know about such a type.  */
  5467. #ifdef BFD_HOST_U_64_BIT
  5468.   if (info->optimize)
  5469.     {
  5470.       size_t minsize;
  5471.       size_t maxsize;
  5472.       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
  5473.       bfd *dynobj = elf_hash_table (info)->dynobj;
  5474.       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
  5475.       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
  5476.       unsigned long int *counts;
  5477.       bfd_size_type amt;
  5478.       unsigned int no_improvement_count = 0;
  5479.  
  5480.       /* Possible optimization parameters: if we have NSYMS symbols we say
  5481.          that the hashing table must at least have NSYMS/4 and at most
  5482.          2*NSYMS buckets.  */
  5483.       minsize = nsyms / 4;
  5484.       if (minsize == 0)
  5485.         minsize = 1;
  5486.       best_size = maxsize = nsyms * 2;
  5487.       if (gnu_hash)
  5488.         {
  5489.           if (minsize < 2)
  5490.             minsize = 2;
  5491.           if ((best_size & 31) == 0)
  5492.             ++best_size;
  5493.         }
  5494.  
  5495.       /* Create array where we count the collisions in.  We must use bfd_malloc
  5496.          since the size could be large.  */
  5497.       amt = maxsize;
  5498.       amt *= sizeof (unsigned long int);
  5499.       counts = (unsigned long int *) bfd_malloc (amt);
  5500.       if (counts == NULL)
  5501.         return 0;
  5502.  
  5503.       /* Compute the "optimal" size for the hash table.  The criteria is a
  5504.          minimal chain length.  The minor criteria is (of course) the size
  5505.          of the table.  */
  5506.       for (i = minsize; i < maxsize; ++i)
  5507.         {
  5508.           /* Walk through the array of hashcodes and count the collisions.  */
  5509.           BFD_HOST_U_64_BIT max;
  5510.           unsigned long int j;
  5511.           unsigned long int fact;
  5512.  
  5513.           if (gnu_hash && (i & 31) == 0)
  5514.             continue;
  5515.  
  5516.           memset (counts, '\0', i * sizeof (unsigned long int));
  5517.  
  5518.           /* Determine how often each hash bucket is used.  */
  5519.           for (j = 0; j < nsyms; ++j)
  5520.             ++counts[hashcodes[j] % i];
  5521.  
  5522.           /* For the weight function we need some information about the
  5523.              pagesize on the target.  This is information need not be 100%
  5524.              accurate.  Since this information is not available (so far) we
  5525.              define it here to a reasonable default value.  If it is crucial
  5526.              to have a better value some day simply define this value.  */
  5527. # ifndef BFD_TARGET_PAGESIZE
  5528. #  define BFD_TARGET_PAGESIZE   (4096)
  5529. # endif
  5530.  
  5531.           /* We in any case need 2 + DYNSYMCOUNT entries for the size values
  5532.              and the chains.  */
  5533.           max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
  5534.  
  5535. # if 1
  5536.           /* Variant 1: optimize for short chains.  We add the squares
  5537.              of all the chain lengths (which favors many small chain
  5538.              over a few long chains).  */
  5539.           for (j = 0; j < i; ++j)
  5540.             max += counts[j] * counts[j];
  5541.  
  5542.           /* This adds penalties for the overall size of the table.  */
  5543.           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5544.           max *= fact * fact;
  5545. # else
  5546.           /* Variant 2: Optimize a lot more for small table.  Here we
  5547.              also add squares of the size but we also add penalties for
  5548.              empty slots (the +1 term).  */
  5549.           for (j = 0; j < i; ++j)
  5550.             max += (1 + counts[j]) * (1 + counts[j]);
  5551.  
  5552.           /* The overall size of the table is considered, but not as
  5553.              strong as in variant 1, where it is squared.  */
  5554.           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5555.           max *= fact;
  5556. # endif
  5557.  
  5558.           /* Compare with current best results.  */
  5559.           if (max < best_chlen)
  5560.             {
  5561.               best_chlen = max;
  5562.               best_size = i;
  5563.               no_improvement_count = 0;
  5564.             }
  5565.           /* PR 11843: Avoid futile long searches for the best bucket size
  5566.              when there are a large number of symbols.  */
  5567.           else if (++no_improvement_count == 100)
  5568.             break;
  5569.         }
  5570.  
  5571.       free (counts);
  5572.     }
  5573.   else
  5574. #endif /* defined (BFD_HOST_U_64_BIT) */
  5575.     {
  5576.       /* This is the fallback solution if no 64bit type is available or if we
  5577.          are not supposed to spend much time on optimizations.  We select the
  5578.          bucket count using a fixed set of numbers.  */
  5579.       for (i = 0; elf_buckets[i] != 0; i++)
  5580.         {
  5581.           best_size = elf_buckets[i];
  5582.           if (nsyms < elf_buckets[i + 1])
  5583.             break;
  5584.         }
  5585.       if (gnu_hash && best_size < 2)
  5586.         best_size = 2;
  5587.     }
  5588.  
  5589.   return best_size;
  5590. }
  5591.  
  5592. /* Size any SHT_GROUP section for ld -r.  */
  5593.  
  5594. bfd_boolean
  5595. _bfd_elf_size_group_sections (struct bfd_link_info *info)
  5596. {
  5597.   bfd *ibfd;
  5598.  
  5599.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  5600.     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  5601.         && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
  5602.       return FALSE;
  5603.   return TRUE;
  5604. }
  5605.  
  5606. /* Set a default stack segment size.  The value in INFO wins.  If it
  5607.    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
  5608.    undefined it is initialized.  */
  5609.  
  5610. bfd_boolean
  5611. bfd_elf_stack_segment_size (bfd *output_bfd,
  5612.                             struct bfd_link_info *info,
  5613.                             const char *legacy_symbol,
  5614.                             bfd_vma default_size)
  5615. {
  5616.   struct elf_link_hash_entry *h = NULL;
  5617.  
  5618.   /* Look for legacy symbol.  */
  5619.   if (legacy_symbol)
  5620.     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
  5621.                               FALSE, FALSE, FALSE);
  5622.   if (h && (h->root.type == bfd_link_hash_defined
  5623.             || h->root.type == bfd_link_hash_defweak)
  5624.       && h->def_regular
  5625.       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
  5626.     {
  5627.       /* The symbol has no type if specified on the command line.  */
  5628.       h->type = STT_OBJECT;
  5629.       if (info->stacksize)
  5630.         (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
  5631.                                output_bfd, legacy_symbol);
  5632.       else if (h->root.u.def.section != bfd_abs_section_ptr)
  5633.         (*_bfd_error_handler) (_("%B: %s not absolute"),
  5634.                                output_bfd, legacy_symbol);
  5635.       else
  5636.         info->stacksize = h->root.u.def.value;
  5637.     }
  5638.  
  5639.   if (!info->stacksize)
  5640.     /* If the user didn't set a size, or explicitly inhibit the
  5641.        size, set it now.  */
  5642.     info->stacksize = default_size;
  5643.  
  5644.   /* Provide the legacy symbol, if it is referenced.  */
  5645.   if (h && (h->root.type == bfd_link_hash_undefined
  5646.             || h->root.type == bfd_link_hash_undefweak))
  5647.     {
  5648.       struct bfd_link_hash_entry *bh = NULL;
  5649.  
  5650.       if (!(_bfd_generic_link_add_one_symbol
  5651.             (info, output_bfd, legacy_symbol,
  5652.              BSF_GLOBAL, bfd_abs_section_ptr,
  5653.              info->stacksize >= 0 ? info->stacksize : 0,
  5654.              NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
  5655.         return FALSE;
  5656.  
  5657.       h = (struct elf_link_hash_entry *) bh;
  5658.       h->def_regular = 1;
  5659.       h->type = STT_OBJECT;
  5660.     }
  5661.  
  5662.   return TRUE;
  5663. }
  5664.  
  5665. /* Set up the sizes and contents of the ELF dynamic sections.  This is
  5666.    called by the ELF linker emulation before_allocation routine.  We
  5667.    must set the sizes of the sections before the linker sets the
  5668.    addresses of the various sections.  */
  5669.  
  5670. bfd_boolean
  5671. bfd_elf_size_dynamic_sections (bfd *output_bfd,
  5672.                                const char *soname,
  5673.                                const char *rpath,
  5674.                                const char *filter_shlib,
  5675.                                const char *audit,
  5676.                                const char *depaudit,
  5677.                                const char * const *auxiliary_filters,
  5678.                                struct bfd_link_info *info,
  5679.                                asection **sinterpptr)
  5680. {
  5681.   bfd_size_type soname_indx;
  5682.   bfd *dynobj;
  5683.   const struct elf_backend_data *bed;
  5684.   struct elf_info_failed asvinfo;
  5685.  
  5686.   *sinterpptr = NULL;
  5687.  
  5688.   soname_indx = (bfd_size_type) -1;
  5689.  
  5690.   if (!is_elf_hash_table (info->hash))
  5691.     return TRUE;
  5692.  
  5693.   bed = get_elf_backend_data (output_bfd);
  5694.  
  5695.   /* Any syms created from now on start with -1 in
  5696.      got.refcount/offset and plt.refcount/offset.  */
  5697.   elf_hash_table (info)->init_got_refcount
  5698.     = elf_hash_table (info)->init_got_offset;
  5699.   elf_hash_table (info)->init_plt_refcount
  5700.     = elf_hash_table (info)->init_plt_offset;
  5701.  
  5702.   if (bfd_link_relocatable (info)
  5703.       && !_bfd_elf_size_group_sections (info))
  5704.     return FALSE;
  5705.  
  5706.   /* The backend may have to create some sections regardless of whether
  5707.      we're dynamic or not.  */
  5708.   if (bed->elf_backend_always_size_sections
  5709.       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
  5710.     return FALSE;
  5711.  
  5712.   /* Determine any GNU_STACK segment requirements, after the backend
  5713.      has had a chance to set a default segment size.  */
  5714.   if (info->execstack)
  5715.     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
  5716.   else if (info->noexecstack)
  5717.     elf_stack_flags (output_bfd) = PF_R | PF_W;
  5718.   else
  5719.     {
  5720.       bfd *inputobj;
  5721.       asection *notesec = NULL;
  5722.       int exec = 0;
  5723.  
  5724.       for (inputobj = info->input_bfds;
  5725.            inputobj;
  5726.            inputobj = inputobj->link.next)
  5727.         {
  5728.           asection *s;
  5729.  
  5730.           if (inputobj->flags
  5731.               & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
  5732.             continue;
  5733.           s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
  5734.           if (s)
  5735.             {
  5736.               if (s->flags & SEC_CODE)
  5737.                 exec = PF_X;
  5738.               notesec = s;
  5739.             }
  5740.           else if (bed->default_execstack)
  5741.             exec = PF_X;
  5742.         }
  5743.       if (notesec || info->stacksize > 0)
  5744.         elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
  5745.       if (notesec && exec && bfd_link_relocatable (info)
  5746.           && notesec->output_section != bfd_abs_section_ptr)
  5747.         notesec->output_section->flags |= SEC_CODE;
  5748.     }
  5749.  
  5750.   dynobj = elf_hash_table (info)->dynobj;
  5751.  
  5752.   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  5753.     {
  5754.       struct elf_info_failed eif;
  5755.       struct elf_link_hash_entry *h;
  5756.       asection *dynstr;
  5757.       struct bfd_elf_version_tree *t;
  5758.       struct bfd_elf_version_expr *d;
  5759.       asection *s;
  5760.       bfd_boolean all_defined;
  5761.  
  5762.       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
  5763.       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
  5764.  
  5765.       if (soname != NULL)
  5766.         {
  5767.           soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5768.                                              soname, TRUE);
  5769.           if (soname_indx == (bfd_size_type) -1
  5770.               || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
  5771.             return FALSE;
  5772.         }
  5773.  
  5774.       if (info->symbolic)
  5775.         {
  5776.           if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
  5777.             return FALSE;
  5778.           info->flags |= DF_SYMBOLIC;
  5779.         }
  5780.  
  5781.       if (rpath != NULL)
  5782.         {
  5783.           bfd_size_type indx;
  5784.           bfd_vma tag;
  5785.  
  5786.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
  5787.                                       TRUE);
  5788.           if (indx == (bfd_size_type) -1)
  5789.             return FALSE;
  5790.  
  5791.           tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
  5792.           if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
  5793.             return FALSE;
  5794.         }
  5795.  
  5796.       if (filter_shlib != NULL)
  5797.         {
  5798.           bfd_size_type indx;
  5799.  
  5800.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5801.                                       filter_shlib, TRUE);
  5802.           if (indx == (bfd_size_type) -1
  5803.               || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
  5804.             return FALSE;
  5805.         }
  5806.  
  5807.       if (auxiliary_filters != NULL)
  5808.         {
  5809.           const char * const *p;
  5810.  
  5811.           for (p = auxiliary_filters; *p != NULL; p++)
  5812.             {
  5813.               bfd_size_type indx;
  5814.  
  5815.               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5816.                                           *p, TRUE);
  5817.               if (indx == (bfd_size_type) -1
  5818.                   || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
  5819.                 return FALSE;
  5820.             }
  5821.         }
  5822.  
  5823.       if (audit != NULL)
  5824.         {
  5825.           bfd_size_type indx;
  5826.  
  5827.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
  5828.                                       TRUE);
  5829.           if (indx == (bfd_size_type) -1
  5830.               || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
  5831.             return FALSE;
  5832.         }
  5833.  
  5834.       if (depaudit != NULL)
  5835.         {
  5836.           bfd_size_type indx;
  5837.  
  5838.           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
  5839.                                       TRUE);
  5840.           if (indx == (bfd_size_type) -1
  5841.               || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
  5842.             return FALSE;
  5843.         }
  5844.  
  5845.       eif.info = info;
  5846.       eif.failed = FALSE;
  5847.  
  5848.       /* If we are supposed to export all symbols into the dynamic symbol
  5849.          table (this is not the normal case), then do so.  */
  5850.       if (info->export_dynamic
  5851.           || (bfd_link_executable (info) && info->dynamic))
  5852.         {
  5853.           elf_link_hash_traverse (elf_hash_table (info),
  5854.                                   _bfd_elf_export_symbol,
  5855.                                   &eif);
  5856.           if (eif.failed)
  5857.             return FALSE;
  5858.         }
  5859.  
  5860.       /* Make all global versions with definition.  */
  5861.       for (t = info->version_info; t != NULL; t = t->next)
  5862.         for (d = t->globals.list; d != NULL; d = d->next)
  5863.           if (!d->symver && d->literal)
  5864.             {
  5865.               const char *verstr, *name;
  5866.               size_t namelen, verlen, newlen;
  5867.               char *newname, *p, leading_char;
  5868.               struct elf_link_hash_entry *newh;
  5869.  
  5870.               leading_char = bfd_get_symbol_leading_char (output_bfd);
  5871.               name = d->pattern;
  5872.               namelen = strlen (name) + (leading_char != '\0');
  5873.               verstr = t->name;
  5874.               verlen = strlen (verstr);
  5875.               newlen = namelen + verlen + 3;
  5876.  
  5877.               newname = (char *) bfd_malloc (newlen);
  5878.               if (newname == NULL)
  5879.                 return FALSE;
  5880.               newname[0] = leading_char;
  5881.               memcpy (newname + (leading_char != '\0'), name, namelen);
  5882.  
  5883.               /* Check the hidden versioned definition.  */
  5884.               p = newname + namelen;
  5885.               *p++ = ELF_VER_CHR;
  5886.               memcpy (p, verstr, verlen + 1);
  5887.               newh = elf_link_hash_lookup (elf_hash_table (info),
  5888.                                            newname, FALSE, FALSE,
  5889.                                            FALSE);
  5890.               if (newh == NULL
  5891.                   || (newh->root.type != bfd_link_hash_defined
  5892.                       && newh->root.type != bfd_link_hash_defweak))
  5893.                 {
  5894.                   /* Check the default versioned definition.  */
  5895.                   *p++ = ELF_VER_CHR;
  5896.                   memcpy (p, verstr, verlen + 1);
  5897.                   newh = elf_link_hash_lookup (elf_hash_table (info),
  5898.                                                newname, FALSE, FALSE,
  5899.                                                FALSE);
  5900.                 }
  5901.               free (newname);
  5902.  
  5903.               /* Mark this version if there is a definition and it is
  5904.                  not defined in a shared object.  */
  5905.               if (newh != NULL
  5906.                   && !newh->def_dynamic
  5907.                   && (newh->root.type == bfd_link_hash_defined
  5908.                       || newh->root.type == bfd_link_hash_defweak))
  5909.                 d->symver = 1;
  5910.             }
  5911.  
  5912.       /* Attach all the symbols to their version information.  */
  5913.       asvinfo.info = info;
  5914.       asvinfo.failed = FALSE;
  5915.  
  5916.       elf_link_hash_traverse (elf_hash_table (info),
  5917.                               _bfd_elf_link_assign_sym_version,
  5918.                               &asvinfo);
  5919.       if (asvinfo.failed)
  5920.         return FALSE;
  5921.  
  5922.       if (!info->allow_undefined_version)
  5923.         {
  5924.           /* Check if all global versions have a definition.  */
  5925.           all_defined = TRUE;
  5926.           for (t = info->version_info; t != NULL; t = t->next)
  5927.             for (d = t->globals.list; d != NULL; d = d->next)
  5928.               if (d->literal && !d->symver && !d->script)
  5929.                 {
  5930.                   (*_bfd_error_handler)
  5931.                     (_("%s: undefined version: %s"),
  5932.                      d->pattern, t->name);
  5933.                   all_defined = FALSE;
  5934.                 }
  5935.  
  5936.           if (!all_defined)
  5937.             {
  5938.               bfd_set_error (bfd_error_bad_value);
  5939.               return FALSE;
  5940.             }
  5941.         }
  5942.  
  5943.       /* Find all symbols which were defined in a dynamic object and make
  5944.          the backend pick a reasonable value for them.  */
  5945.       elf_link_hash_traverse (elf_hash_table (info),
  5946.                               _bfd_elf_adjust_dynamic_symbol,
  5947.                               &eif);
  5948.       if (eif.failed)
  5949.         return FALSE;
  5950.  
  5951.       /* Add some entries to the .dynamic section.  We fill in some of the
  5952.          values later, in bfd_elf_final_link, but we must add the entries
  5953.          now so that we know the final size of the .dynamic section.  */
  5954.  
  5955.       /* If there are initialization and/or finalization functions to
  5956.          call then add the corresponding DT_INIT/DT_FINI entries.  */
  5957.       h = (info->init_function
  5958.            ? elf_link_hash_lookup (elf_hash_table (info),
  5959.                                    info->init_function, FALSE,
  5960.                                    FALSE, FALSE)
  5961.            : NULL);
  5962.       if (h != NULL
  5963.           && (h->ref_regular
  5964.               || h->def_regular))
  5965.         {
  5966.           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
  5967.             return FALSE;
  5968.         }
  5969.       h = (info->fini_function
  5970.            ? elf_link_hash_lookup (elf_hash_table (info),
  5971.                                    info->fini_function, FALSE,
  5972.                                    FALSE, FALSE)
  5973.            : NULL);
  5974.       if (h != NULL
  5975.           && (h->ref_regular
  5976.               || h->def_regular))
  5977.         {
  5978.           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
  5979.             return FALSE;
  5980.         }
  5981.  
  5982.       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
  5983.       if (s != NULL && s->linker_has_input)
  5984.         {
  5985.           /* DT_PREINIT_ARRAY is not allowed in shared library.  */
  5986.           if (! bfd_link_executable (info))
  5987.             {
  5988.               bfd *sub;
  5989.               asection *o;
  5990.  
  5991.               for (sub = info->input_bfds; sub != NULL;
  5992.                    sub = sub->link.next)
  5993.                 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
  5994.                   for (o = sub->sections; o != NULL; o = o->next)
  5995.                     if (elf_section_data (o)->this_hdr.sh_type
  5996.                         == SHT_PREINIT_ARRAY)
  5997.                       {
  5998.                         (*_bfd_error_handler)
  5999.                           (_("%B: .preinit_array section is not allowed in DSO"),
  6000.                            sub);
  6001.                         break;
  6002.                       }
  6003.  
  6004.               bfd_set_error (bfd_error_nonrepresentable_section);
  6005.               return FALSE;
  6006.             }
  6007.  
  6008.           if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
  6009.               || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
  6010.             return FALSE;
  6011.         }
  6012.       s = bfd_get_section_by_name (output_bfd, ".init_array");
  6013.       if (s != NULL && s->linker_has_input)
  6014.         {
  6015.           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
  6016.               || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
  6017.             return FALSE;
  6018.         }
  6019.       s = bfd_get_section_by_name (output_bfd, ".fini_array");
  6020.       if (s != NULL && s->linker_has_input)
  6021.         {
  6022.           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
  6023.               || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
  6024.             return FALSE;
  6025.         }
  6026.  
  6027.       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
  6028.       /* If .dynstr is excluded from the link, we don't want any of
  6029.          these tags.  Strictly, we should be checking each section
  6030.          individually;  This quick check covers for the case where
  6031.          someone does a /DISCARD/ : { *(*) }.  */
  6032.       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
  6033.         {
  6034.           bfd_size_type strsize;
  6035.  
  6036.           strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  6037.           if ((info->emit_hash
  6038.                && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
  6039.               || (info->emit_gnu_hash
  6040.                   && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
  6041.               || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
  6042.               || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
  6043.               || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
  6044.               || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
  6045.                                               bed->s->sizeof_sym))
  6046.             return FALSE;
  6047.         }
  6048.     }
  6049.  
  6050.   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
  6051.     return FALSE;
  6052.  
  6053.   /* The backend must work out the sizes of all the other dynamic
  6054.      sections.  */
  6055.   if (dynobj != NULL
  6056.       && bed->elf_backend_size_dynamic_sections != NULL
  6057.       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
  6058.     return FALSE;
  6059.  
  6060.   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  6061.     {
  6062.       unsigned long section_sym_count;
  6063.       struct bfd_elf_version_tree *verdefs;
  6064.       asection *s;
  6065.  
  6066.       /* Set up the version definition section.  */
  6067.       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  6068.       BFD_ASSERT (s != NULL);
  6069.  
  6070.       /* We may have created additional version definitions if we are
  6071.          just linking a regular application.  */
  6072.       verdefs = info->version_info;
  6073.  
  6074.       /* Skip anonymous version tag.  */
  6075.       if (verdefs != NULL && verdefs->vernum == 0)
  6076.         verdefs = verdefs->next;
  6077.  
  6078.       if (verdefs == NULL && !info->create_default_symver)
  6079.         s->flags |= SEC_EXCLUDE;
  6080.       else
  6081.         {
  6082.           unsigned int cdefs;
  6083.           bfd_size_type size;
  6084.           struct bfd_elf_version_tree *t;
  6085.           bfd_byte *p;
  6086.           Elf_Internal_Verdef def;
  6087.           Elf_Internal_Verdaux defaux;
  6088.           struct bfd_link_hash_entry *bh;
  6089.           struct elf_link_hash_entry *h;
  6090.           const char *name;
  6091.  
  6092.           cdefs = 0;
  6093.           size = 0;
  6094.  
  6095.           /* Make space for the base version.  */
  6096.           size += sizeof (Elf_External_Verdef);
  6097.           size += sizeof (Elf_External_Verdaux);
  6098.           ++cdefs;
  6099.  
  6100.           /* Make space for the default version.  */
  6101.           if (info->create_default_symver)
  6102.             {
  6103.               size += sizeof (Elf_External_Verdef);
  6104.               ++cdefs;
  6105.             }
  6106.  
  6107.           for (t = verdefs; t != NULL; t = t->next)
  6108.             {
  6109.               struct bfd_elf_version_deps *n;
  6110.  
  6111.               /* Don't emit base version twice.  */
  6112.               if (t->vernum == 0)
  6113.                 continue;
  6114.  
  6115.               size += sizeof (Elf_External_Verdef);
  6116.               size += sizeof (Elf_External_Verdaux);
  6117.               ++cdefs;
  6118.  
  6119.               for (n = t->deps; n != NULL; n = n->next)
  6120.                 size += sizeof (Elf_External_Verdaux);
  6121.             }
  6122.  
  6123.           s->size = size;
  6124.           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6125.           if (s->contents == NULL && s->size != 0)
  6126.             return FALSE;
  6127.  
  6128.           /* Fill in the version definition section.  */
  6129.  
  6130.           p = s->contents;
  6131.  
  6132.           def.vd_version = VER_DEF_CURRENT;
  6133.           def.vd_flags = VER_FLG_BASE;
  6134.           def.vd_ndx = 1;
  6135.           def.vd_cnt = 1;
  6136.           if (info->create_default_symver)
  6137.             {
  6138.               def.vd_aux = 2 * sizeof (Elf_External_Verdef);
  6139.               def.vd_next = sizeof (Elf_External_Verdef);
  6140.             }
  6141.           else
  6142.             {
  6143.               def.vd_aux = sizeof (Elf_External_Verdef);
  6144.               def.vd_next = (sizeof (Elf_External_Verdef)
  6145.                              + sizeof (Elf_External_Verdaux));
  6146.             }
  6147.  
  6148.           if (soname_indx != (bfd_size_type) -1)
  6149.             {
  6150.               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6151.                                       soname_indx);
  6152.               def.vd_hash = bfd_elf_hash (soname);
  6153.               defaux.vda_name = soname_indx;
  6154.               name = soname;
  6155.             }
  6156.           else
  6157.             {
  6158.               bfd_size_type indx;
  6159.  
  6160.               name = lbasename (output_bfd->filename);
  6161.               def.vd_hash = bfd_elf_hash (name);
  6162.               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6163.                                           name, FALSE);
  6164.               if (indx == (bfd_size_type) -1)
  6165.                 return FALSE;
  6166.               defaux.vda_name = indx;
  6167.             }
  6168.           defaux.vda_next = 0;
  6169.  
  6170.           _bfd_elf_swap_verdef_out (output_bfd, &def,
  6171.                                     (Elf_External_Verdef *) p);
  6172.           p += sizeof (Elf_External_Verdef);
  6173.           if (info->create_default_symver)
  6174.             {
  6175.               /* Add a symbol representing this version.  */
  6176.               bh = NULL;
  6177.               if (! (_bfd_generic_link_add_one_symbol
  6178.                      (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
  6179.                       0, NULL, FALSE,
  6180.                       get_elf_backend_data (dynobj)->collect, &bh)))
  6181.                 return FALSE;
  6182.               h = (struct elf_link_hash_entry *) bh;
  6183.               h->non_elf = 0;
  6184.               h->def_regular = 1;
  6185.               h->type = STT_OBJECT;
  6186.               h->verinfo.vertree = NULL;
  6187.  
  6188.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6189.                 return FALSE;
  6190.  
  6191.               /* Create a duplicate of the base version with the same
  6192.                  aux block, but different flags.  */
  6193.               def.vd_flags = 0;
  6194.               def.vd_ndx = 2;
  6195.               def.vd_aux = sizeof (Elf_External_Verdef);
  6196.               if (verdefs)
  6197.                 def.vd_next = (sizeof (Elf_External_Verdef)
  6198.                                + sizeof (Elf_External_Verdaux));
  6199.               else
  6200.                 def.vd_next = 0;
  6201.               _bfd_elf_swap_verdef_out (output_bfd, &def,
  6202.                                         (Elf_External_Verdef *) p);
  6203.               p += sizeof (Elf_External_Verdef);
  6204.             }
  6205.           _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6206.                                      (Elf_External_Verdaux *) p);
  6207.           p += sizeof (Elf_External_Verdaux);
  6208.  
  6209.           for (t = verdefs; t != NULL; t = t->next)
  6210.             {
  6211.               unsigned int cdeps;
  6212.               struct bfd_elf_version_deps *n;
  6213.  
  6214.               /* Don't emit the base version twice.  */
  6215.               if (t->vernum == 0)
  6216.                 continue;
  6217.  
  6218.               cdeps = 0;
  6219.               for (n = t->deps; n != NULL; n = n->next)
  6220.                 ++cdeps;
  6221.  
  6222.               /* Add a symbol representing this version.  */
  6223.               bh = NULL;
  6224.               if (! (_bfd_generic_link_add_one_symbol
  6225.                      (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
  6226.                       0, NULL, FALSE,
  6227.                       get_elf_backend_data (dynobj)->collect, &bh)))
  6228.                 return FALSE;
  6229.               h = (struct elf_link_hash_entry *) bh;
  6230.               h->non_elf = 0;
  6231.               h->def_regular = 1;
  6232.               h->type = STT_OBJECT;
  6233.               h->verinfo.vertree = t;
  6234.  
  6235.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6236.                 return FALSE;
  6237.  
  6238.               def.vd_version = VER_DEF_CURRENT;
  6239.               def.vd_flags = 0;
  6240.               if (t->globals.list == NULL
  6241.                   && t->locals.list == NULL
  6242.                   && ! t->used)
  6243.                 def.vd_flags |= VER_FLG_WEAK;
  6244.               def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
  6245.               def.vd_cnt = cdeps + 1;
  6246.               def.vd_hash = bfd_elf_hash (t->name);
  6247.               def.vd_aux = sizeof (Elf_External_Verdef);
  6248.               def.vd_next = 0;
  6249.  
  6250.               /* If a basever node is next, it *must* be the last node in
  6251.                  the chain, otherwise Verdef construction breaks.  */
  6252.               if (t->next != NULL && t->next->vernum == 0)
  6253.                 BFD_ASSERT (t->next->next == NULL);
  6254.  
  6255.               if (t->next != NULL && t->next->vernum != 0)
  6256.                 def.vd_next = (sizeof (Elf_External_Verdef)
  6257.                                + (cdeps + 1) * sizeof (Elf_External_Verdaux));
  6258.  
  6259.               _bfd_elf_swap_verdef_out (output_bfd, &def,
  6260.                                         (Elf_External_Verdef *) p);
  6261.               p += sizeof (Elf_External_Verdef);
  6262.  
  6263.               defaux.vda_name = h->dynstr_index;
  6264.               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6265.                                       h->dynstr_index);
  6266.               defaux.vda_next = 0;
  6267.               if (t->deps != NULL)
  6268.                 defaux.vda_next = sizeof (Elf_External_Verdaux);
  6269.               t->name_indx = defaux.vda_name;
  6270.  
  6271.               _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6272.                                          (Elf_External_Verdaux *) p);
  6273.               p += sizeof (Elf_External_Verdaux);
  6274.  
  6275.               for (n = t->deps; n != NULL; n = n->next)
  6276.                 {
  6277.                   if (n->version_needed == NULL)
  6278.                     {
  6279.                       /* This can happen if there was an error in the
  6280.                          version script.  */
  6281.                       defaux.vda_name = 0;
  6282.                     }
  6283.                   else
  6284.                     {
  6285.                       defaux.vda_name = n->version_needed->name_indx;
  6286.                       _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6287.                                               defaux.vda_name);
  6288.                     }
  6289.                   if (n->next == NULL)
  6290.                     defaux.vda_next = 0;
  6291.                   else
  6292.                     defaux.vda_next = sizeof (Elf_External_Verdaux);
  6293.  
  6294.                   _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6295.                                              (Elf_External_Verdaux *) p);
  6296.                   p += sizeof (Elf_External_Verdaux);
  6297.                 }
  6298.             }
  6299.  
  6300.           if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
  6301.               || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
  6302.             return FALSE;
  6303.  
  6304.           elf_tdata (output_bfd)->cverdefs = cdefs;
  6305.         }
  6306.  
  6307.       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
  6308.         {
  6309.           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
  6310.             return FALSE;
  6311.         }
  6312.       else if (info->flags & DF_BIND_NOW)
  6313.         {
  6314.           if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
  6315.             return FALSE;
  6316.         }
  6317.  
  6318.       if (info->flags_1)
  6319.         {
  6320.           if (bfd_link_executable (info))
  6321.             info->flags_1 &= ~ (DF_1_INITFIRST
  6322.                                 | DF_1_NODELETE
  6323.                                 | DF_1_NOOPEN);
  6324.           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
  6325.             return FALSE;
  6326.         }
  6327.  
  6328.       /* Work out the size of the version reference section.  */
  6329.  
  6330.       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  6331.       BFD_ASSERT (s != NULL);
  6332.       {
  6333.         struct elf_find_verdep_info sinfo;
  6334.  
  6335.         sinfo.info = info;
  6336.         sinfo.vers = elf_tdata (output_bfd)->cverdefs;
  6337.         if (sinfo.vers == 0)
  6338.           sinfo.vers = 1;
  6339.         sinfo.failed = FALSE;
  6340.  
  6341.         elf_link_hash_traverse (elf_hash_table (info),
  6342.                                 _bfd_elf_link_find_version_dependencies,
  6343.                                 &sinfo);
  6344.         if (sinfo.failed)
  6345.           return FALSE;
  6346.  
  6347.         if (elf_tdata (output_bfd)->verref == NULL)
  6348.           s->flags |= SEC_EXCLUDE;
  6349.         else
  6350.           {
  6351.             Elf_Internal_Verneed *t;
  6352.             unsigned int size;
  6353.             unsigned int crefs;
  6354.             bfd_byte *p;
  6355.  
  6356.             /* Build the version dependency section.  */
  6357.             size = 0;
  6358.             crefs = 0;
  6359.             for (t = elf_tdata (output_bfd)->verref;
  6360.                  t != NULL;
  6361.                  t = t->vn_nextref)
  6362.               {
  6363.                 Elf_Internal_Vernaux *a;
  6364.  
  6365.                 size += sizeof (Elf_External_Verneed);
  6366.                 ++crefs;
  6367.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6368.                   size += sizeof (Elf_External_Vernaux);
  6369.               }
  6370.  
  6371.             s->size = size;
  6372.             s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6373.             if (s->contents == NULL)
  6374.               return FALSE;
  6375.  
  6376.             p = s->contents;
  6377.             for (t = elf_tdata (output_bfd)->verref;
  6378.                  t != NULL;
  6379.                  t = t->vn_nextref)
  6380.               {
  6381.                 unsigned int caux;
  6382.                 Elf_Internal_Vernaux *a;
  6383.                 bfd_size_type indx;
  6384.  
  6385.                 caux = 0;
  6386.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6387.                   ++caux;
  6388.  
  6389.                 t->vn_version = VER_NEED_CURRENT;
  6390.                 t->vn_cnt = caux;
  6391.                 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6392.                                             elf_dt_name (t->vn_bfd) != NULL
  6393.                                             ? elf_dt_name (t->vn_bfd)
  6394.                                             : lbasename (t->vn_bfd->filename),
  6395.                                             FALSE);
  6396.                 if (indx == (bfd_size_type) -1)
  6397.                   return FALSE;
  6398.                 t->vn_file = indx;
  6399.                 t->vn_aux = sizeof (Elf_External_Verneed);
  6400.                 if (t->vn_nextref == NULL)
  6401.                   t->vn_next = 0;
  6402.                 else
  6403.                   t->vn_next = (sizeof (Elf_External_Verneed)
  6404.                                 + caux * sizeof (Elf_External_Vernaux));
  6405.  
  6406.                 _bfd_elf_swap_verneed_out (output_bfd, t,
  6407.                                            (Elf_External_Verneed *) p);
  6408.                 p += sizeof (Elf_External_Verneed);
  6409.  
  6410.                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6411.                   {
  6412.                     a->vna_hash = bfd_elf_hash (a->vna_nodename);
  6413.                     indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6414.                                                 a->vna_nodename, FALSE);
  6415.                     if (indx == (bfd_size_type) -1)
  6416.                       return FALSE;
  6417.                     a->vna_name = indx;
  6418.                     if (a->vna_nextptr == NULL)
  6419.                       a->vna_next = 0;
  6420.                     else
  6421.                       a->vna_next = sizeof (Elf_External_Vernaux);
  6422.  
  6423.                     _bfd_elf_swap_vernaux_out (output_bfd, a,
  6424.                                                (Elf_External_Vernaux *) p);
  6425.                     p += sizeof (Elf_External_Vernaux);
  6426.                   }
  6427.               }
  6428.  
  6429.             if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
  6430.                 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
  6431.               return FALSE;
  6432.  
  6433.             elf_tdata (output_bfd)->cverrefs = crefs;
  6434.           }
  6435.       }
  6436.  
  6437.       if ((elf_tdata (output_bfd)->cverrefs == 0
  6438.            && elf_tdata (output_bfd)->cverdefs == 0)
  6439.           || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
  6440.                                              &section_sym_count) == 0)
  6441.         {
  6442.           s = bfd_get_linker_section (dynobj, ".gnu.version");
  6443.           s->flags |= SEC_EXCLUDE;
  6444.         }
  6445.     }
  6446.   return TRUE;
  6447. }
  6448.  
  6449. /* Find the first non-excluded output section.  We'll use its
  6450.    section symbol for some emitted relocs.  */
  6451. void
  6452. _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
  6453. {
  6454.   asection *s;
  6455.  
  6456.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6457.     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
  6458.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6459.       {
  6460.         elf_hash_table (info)->text_index_section = s;
  6461.         break;
  6462.       }
  6463. }
  6464.  
  6465. /* Find two non-excluded output sections, one for code, one for data.
  6466.    We'll use their section symbols for some emitted relocs.  */
  6467. void
  6468. _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
  6469. {
  6470.   asection *s;
  6471.  
  6472.   /* Data first, since setting text_index_section changes
  6473.      _bfd_elf_link_omit_section_dynsym.  */
  6474.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6475.     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
  6476.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6477.       {
  6478.         elf_hash_table (info)->data_index_section = s;
  6479.         break;
  6480.       }
  6481.  
  6482.   for (s = output_bfd->sections; s != NULL; s = s->next)
  6483.     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
  6484.          == (SEC_ALLOC | SEC_READONLY))
  6485.         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
  6486.       {
  6487.         elf_hash_table (info)->text_index_section = s;
  6488.         break;
  6489.       }
  6490.  
  6491.   if (elf_hash_table (info)->text_index_section == NULL)
  6492.     elf_hash_table (info)->text_index_section
  6493.       = elf_hash_table (info)->data_index_section;
  6494. }
  6495.  
  6496. bfd_boolean
  6497. bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  6498. {
  6499.   const struct elf_backend_data *bed;
  6500.  
  6501.   if (!is_elf_hash_table (info->hash))
  6502.     return TRUE;
  6503.  
  6504.   bed = get_elf_backend_data (output_bfd);
  6505.   (*bed->elf_backend_init_index_section) (output_bfd, info);
  6506.  
  6507.   if (elf_hash_table (info)->dynamic_sections_created)
  6508.     {
  6509.       bfd *dynobj;
  6510.       asection *s;
  6511.       bfd_size_type dynsymcount;
  6512.       unsigned long section_sym_count;
  6513.       unsigned int dtagcount;
  6514.  
  6515.       dynobj = elf_hash_table (info)->dynobj;
  6516.  
  6517.       /* Assign dynsym indicies.  In a shared library we generate a
  6518.          section symbol for each output section, which come first.
  6519.          Next come all of the back-end allocated local dynamic syms,
  6520.          followed by the rest of the global symbols.  */
  6521.  
  6522.       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
  6523.                                                     &section_sym_count);
  6524.  
  6525.       /* Work out the size of the symbol version section.  */
  6526.       s = bfd_get_linker_section (dynobj, ".gnu.version");
  6527.       BFD_ASSERT (s != NULL);
  6528.       if (dynsymcount != 0
  6529.           && (s->flags & SEC_EXCLUDE) == 0)
  6530.         {
  6531.           s->size = dynsymcount * sizeof (Elf_External_Versym);
  6532.           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6533.           if (s->contents == NULL)
  6534.             return FALSE;
  6535.  
  6536.           if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
  6537.             return FALSE;
  6538.         }
  6539.  
  6540.       /* Set the size of the .dynsym and .hash sections.  We counted
  6541.          the number of dynamic symbols in elf_link_add_object_symbols.
  6542.          We will build the contents of .dynsym and .hash when we build
  6543.          the final symbol table, because until then we do not know the
  6544.          correct value to give the symbols.  We built the .dynstr
  6545.          section as we went along in elf_link_add_object_symbols.  */
  6546.       s = elf_hash_table (info)->dynsym;
  6547.       BFD_ASSERT (s != NULL);
  6548.       s->size = dynsymcount * bed->s->sizeof_sym;
  6549.  
  6550.       if (dynsymcount != 0)
  6551.         {
  6552.           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6553.           if (s->contents == NULL)
  6554.             return FALSE;
  6555.  
  6556.           /* The first entry in .dynsym is a dummy symbol.
  6557.              Clear all the section syms, in case we don't output them all.  */
  6558.           ++section_sym_count;
  6559.           memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
  6560.         }
  6561.  
  6562.       elf_hash_table (info)->bucketcount = 0;
  6563.  
  6564.       /* Compute the size of the hashing table.  As a side effect this
  6565.          computes the hash values for all the names we export.  */
  6566.       if (info->emit_hash)
  6567.         {
  6568.           unsigned long int *hashcodes;
  6569.           struct hash_codes_info hashinf;
  6570.           bfd_size_type amt;
  6571.           unsigned long int nsyms;
  6572.           size_t bucketcount;
  6573.           size_t hash_entry_size;
  6574.  
  6575.           /* Compute the hash values for all exported symbols.  At the same
  6576.              time store the values in an array so that we could use them for
  6577.              optimizations.  */
  6578.           amt = dynsymcount * sizeof (unsigned long int);
  6579.           hashcodes = (unsigned long int *) bfd_malloc (amt);
  6580.           if (hashcodes == NULL)
  6581.             return FALSE;
  6582.           hashinf.hashcodes = hashcodes;
  6583.           hashinf.error = FALSE;
  6584.  
  6585.           /* Put all hash values in HASHCODES.  */
  6586.           elf_link_hash_traverse (elf_hash_table (info),
  6587.                                   elf_collect_hash_codes, &hashinf);
  6588.           if (hashinf.error)
  6589.             {
  6590.               free (hashcodes);
  6591.               return FALSE;
  6592.             }
  6593.  
  6594.           nsyms = hashinf.hashcodes - hashcodes;
  6595.           bucketcount
  6596.             = compute_bucket_count (info, hashcodes, nsyms, 0);
  6597.           free (hashcodes);
  6598.  
  6599.           if (bucketcount == 0)
  6600.             return FALSE;
  6601.  
  6602.           elf_hash_table (info)->bucketcount = bucketcount;
  6603.  
  6604.           s = bfd_get_linker_section (dynobj, ".hash");
  6605.           BFD_ASSERT (s != NULL);
  6606.           hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
  6607.           s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
  6608.           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6609.           if (s->contents == NULL)
  6610.             return FALSE;
  6611.  
  6612.           bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
  6613.           bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
  6614.                    s->contents + hash_entry_size);
  6615.         }
  6616.  
  6617.       if (info->emit_gnu_hash)
  6618.         {
  6619.           size_t i, cnt;
  6620.           unsigned char *contents;
  6621.           struct collect_gnu_hash_codes cinfo;
  6622.           bfd_size_type amt;
  6623.           size_t bucketcount;
  6624.  
  6625.           memset (&cinfo, 0, sizeof (cinfo));
  6626.  
  6627.           /* Compute the hash values for all exported symbols.  At the same
  6628.              time store the values in an array so that we could use them for
  6629.              optimizations.  */
  6630.           amt = dynsymcount * 2 * sizeof (unsigned long int);
  6631.           cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
  6632.           if (cinfo.hashcodes == NULL)
  6633.             return FALSE;
  6634.  
  6635.           cinfo.hashval = cinfo.hashcodes + dynsymcount;
  6636.           cinfo.min_dynindx = -1;
  6637.           cinfo.output_bfd = output_bfd;
  6638.           cinfo.bed = bed;
  6639.  
  6640.           /* Put all hash values in HASHCODES.  */
  6641.           elf_link_hash_traverse (elf_hash_table (info),
  6642.                                   elf_collect_gnu_hash_codes, &cinfo);
  6643.           if (cinfo.error)
  6644.             {
  6645.               free (cinfo.hashcodes);
  6646.               return FALSE;
  6647.             }
  6648.  
  6649.           bucketcount
  6650.             = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
  6651.  
  6652.           if (bucketcount == 0)
  6653.             {
  6654.               free (cinfo.hashcodes);
  6655.               return FALSE;
  6656.             }
  6657.  
  6658.           s = bfd_get_linker_section (dynobj, ".gnu.hash");
  6659.           BFD_ASSERT (s != NULL);
  6660.  
  6661.           if (cinfo.nsyms == 0)
  6662.             {
  6663.               /* Empty .gnu.hash section is special.  */
  6664.               BFD_ASSERT (cinfo.min_dynindx == -1);
  6665.               free (cinfo.hashcodes);
  6666.               s->size = 5 * 4 + bed->s->arch_size / 8;
  6667.               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6668.               if (contents == NULL)
  6669.                 return FALSE;
  6670.               s->contents = contents;
  6671.               /* 1 empty bucket.  */
  6672.               bfd_put_32 (output_bfd, 1, contents);
  6673.               /* SYMIDX above the special symbol 0.  */
  6674.               bfd_put_32 (output_bfd, 1, contents + 4);
  6675.               /* Just one word for bitmask.  */
  6676.               bfd_put_32 (output_bfd, 1, contents + 8);
  6677.               /* Only hash fn bloom filter.  */
  6678.               bfd_put_32 (output_bfd, 0, contents + 12);
  6679.               /* No hashes are valid - empty bitmask.  */
  6680.               bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
  6681.               /* No hashes in the only bucket.  */
  6682.               bfd_put_32 (output_bfd, 0,
  6683.                           contents + 16 + bed->s->arch_size / 8);
  6684.             }
  6685.           else
  6686.             {
  6687.               unsigned long int maskwords, maskbitslog2, x;
  6688.               BFD_ASSERT (cinfo.min_dynindx != -1);
  6689.  
  6690.               x = cinfo.nsyms;
  6691.               maskbitslog2 = 1;
  6692.               while ((x >>= 1) != 0)
  6693.                 ++maskbitslog2;
  6694.               if (maskbitslog2 < 3)
  6695.                 maskbitslog2 = 5;
  6696.               else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
  6697.                 maskbitslog2 = maskbitslog2 + 3;
  6698.               else
  6699.                 maskbitslog2 = maskbitslog2 + 2;
  6700.               if (bed->s->arch_size == 64)
  6701.                 {
  6702.                   if (maskbitslog2 == 5)
  6703.                     maskbitslog2 = 6;
  6704.                   cinfo.shift1 = 6;
  6705.                 }
  6706.               else
  6707.                 cinfo.shift1 = 5;
  6708.               cinfo.mask = (1 << cinfo.shift1) - 1;
  6709.               cinfo.shift2 = maskbitslog2;
  6710.               cinfo.maskbits = 1 << maskbitslog2;
  6711.               maskwords = 1 << (maskbitslog2 - cinfo.shift1);
  6712.               amt = bucketcount * sizeof (unsigned long int) * 2;
  6713.               amt += maskwords * sizeof (bfd_vma);
  6714.               cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
  6715.               if (cinfo.bitmask == NULL)
  6716.                 {
  6717.                   free (cinfo.hashcodes);
  6718.                   return FALSE;
  6719.                 }
  6720.  
  6721.               cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
  6722.               cinfo.indx = cinfo.counts + bucketcount;
  6723.               cinfo.symindx = dynsymcount - cinfo.nsyms;
  6724.               memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
  6725.  
  6726.               /* Determine how often each hash bucket is used.  */
  6727.               memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
  6728.               for (i = 0; i < cinfo.nsyms; ++i)
  6729.                 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
  6730.  
  6731.               for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
  6732.                 if (cinfo.counts[i] != 0)
  6733.                   {
  6734.                     cinfo.indx[i] = cnt;
  6735.                     cnt += cinfo.counts[i];
  6736.                   }
  6737.               BFD_ASSERT (cnt == dynsymcount);
  6738.               cinfo.bucketcount = bucketcount;
  6739.               cinfo.local_indx = cinfo.min_dynindx;
  6740.  
  6741.               s->size = (4 + bucketcount + cinfo.nsyms) * 4;
  6742.               s->size += cinfo.maskbits / 8;
  6743.               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6744.               if (contents == NULL)
  6745.                 {
  6746.                   free (cinfo.bitmask);
  6747.                   free (cinfo.hashcodes);
  6748.                   return FALSE;
  6749.                 }
  6750.  
  6751.               s->contents = contents;
  6752.               bfd_put_32 (output_bfd, bucketcount, contents);
  6753.               bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
  6754.               bfd_put_32 (output_bfd, maskwords, contents + 8);
  6755.               bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
  6756.               contents += 16 + cinfo.maskbits / 8;
  6757.  
  6758.               for (i = 0; i < bucketcount; ++i)
  6759.                 {
  6760.                   if (cinfo.counts[i] == 0)
  6761.                     bfd_put_32 (output_bfd, 0, contents);
  6762.                   else
  6763.                     bfd_put_32 (output_bfd, cinfo.indx[i], contents);
  6764.                   contents += 4;
  6765.                 }
  6766.  
  6767.               cinfo.contents = contents;
  6768.  
  6769.               /* Renumber dynamic symbols, populate .gnu.hash section.  */
  6770.               elf_link_hash_traverse (elf_hash_table (info),
  6771.                                       elf_renumber_gnu_hash_syms, &cinfo);
  6772.  
  6773.               contents = s->contents + 16;
  6774.               for (i = 0; i < maskwords; ++i)
  6775.                 {
  6776.                   bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
  6777.                            contents);
  6778.                   contents += bed->s->arch_size / 8;
  6779.                 }
  6780.  
  6781.               free (cinfo.bitmask);
  6782.               free (cinfo.hashcodes);
  6783.             }
  6784.         }
  6785.  
  6786.       s = bfd_get_linker_section (dynobj, ".dynstr");
  6787.       BFD_ASSERT (s != NULL);
  6788.  
  6789.       elf_finalize_dynstr (output_bfd, info);
  6790.  
  6791.       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  6792.  
  6793.       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
  6794.         if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
  6795.           return FALSE;
  6796.     }
  6797.  
  6798.   return TRUE;
  6799. }
  6800. /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
  6801.  
  6802. static void
  6803. merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
  6804.                             asection *sec)
  6805. {
  6806.   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
  6807.   sec->sec_info_type = SEC_INFO_TYPE_NONE;
  6808. }
  6809.  
  6810. /* Finish SHF_MERGE section merging.  */
  6811.  
  6812. bfd_boolean
  6813. _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
  6814. {
  6815.   bfd *ibfd;
  6816.   asection *sec;
  6817.  
  6818.   if (!is_elf_hash_table (info->hash))
  6819.     return FALSE;
  6820.  
  6821.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  6822.     if ((ibfd->flags & DYNAMIC) == 0
  6823.         && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  6824.         && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
  6825.             == get_elf_backend_data (obfd)->s->elfclass))
  6826.       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  6827.         if ((sec->flags & SEC_MERGE) != 0
  6828.             && !bfd_is_abs_section (sec->output_section))
  6829.           {
  6830.             struct bfd_elf_section_data *secdata;
  6831.  
  6832.             secdata = elf_section_data (sec);
  6833.             if (! _bfd_add_merge_section (obfd,
  6834.                                           &elf_hash_table (info)->merge_info,
  6835.                                           sec, &secdata->sec_info))
  6836.               return FALSE;
  6837.             else if (secdata->sec_info)
  6838.               sec->sec_info_type = SEC_INFO_TYPE_MERGE;
  6839.           }
  6840.  
  6841.   if (elf_hash_table (info)->merge_info != NULL)
  6842.     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
  6843.                          merge_sections_remove_hook);
  6844.   return TRUE;
  6845. }
  6846.  
  6847. /* Create an entry in an ELF linker hash table.  */
  6848.  
  6849. struct bfd_hash_entry *
  6850. _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  6851.                             struct bfd_hash_table *table,
  6852.                             const char *string)
  6853. {
  6854.   /* Allocate the structure if it has not already been allocated by a
  6855.      subclass.  */
  6856.   if (entry == NULL)
  6857.     {
  6858.       entry = (struct bfd_hash_entry *)
  6859.         bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
  6860.       if (entry == NULL)
  6861.         return entry;
  6862.     }
  6863.  
  6864.   /* Call the allocation method of the superclass.  */
  6865.   entry = _bfd_link_hash_newfunc (entry, table, string);
  6866.   if (entry != NULL)
  6867.     {
  6868.       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
  6869.       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
  6870.  
  6871.       /* Set local fields.  */
  6872.       ret->indx = -1;
  6873.       ret->dynindx = -1;
  6874.       ret->got = htab->init_got_refcount;
  6875.       ret->plt = htab->init_plt_refcount;
  6876.       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
  6877.                               - offsetof (struct elf_link_hash_entry, size)));
  6878.       /* Assume that we have been called by a non-ELF symbol reader.
  6879.          This flag is then reset by the code which reads an ELF input
  6880.          file.  This ensures that a symbol created by a non-ELF symbol
  6881.          reader will have the flag set correctly.  */
  6882.       ret->non_elf = 1;
  6883.     }
  6884.  
  6885.   return entry;
  6886. }
  6887.  
  6888. /* Copy data from an indirect symbol to its direct symbol, hiding the
  6889.    old indirect symbol.  Also used for copying flags to a weakdef.  */
  6890.  
  6891. void
  6892. _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
  6893.                                   struct elf_link_hash_entry *dir,
  6894.                                   struct elf_link_hash_entry *ind)
  6895. {
  6896.   struct elf_link_hash_table *htab;
  6897.  
  6898.   /* Copy down any references that we may have already seen to the
  6899.      symbol which just became indirect if DIR isn't a hidden versioned
  6900.      symbol.  */
  6901.  
  6902.   if (dir->versioned != versioned_hidden)
  6903.     {
  6904.       dir->ref_dynamic |= ind->ref_dynamic;
  6905.       dir->ref_regular |= ind->ref_regular;
  6906.       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  6907.       dir->non_got_ref |= ind->non_got_ref;
  6908.       dir->needs_plt |= ind->needs_plt;
  6909.       dir->pointer_equality_needed |= ind->pointer_equality_needed;
  6910.     }
  6911.  
  6912.   if (ind->root.type != bfd_link_hash_indirect)
  6913.     return;
  6914.  
  6915.   /* Copy over the global and procedure linkage table refcount entries.
  6916.      These may have been already set up by a check_relocs routine.  */
  6917.   htab = elf_hash_table (info);
  6918.   if (ind->got.refcount > htab->init_got_refcount.refcount)
  6919.     {
  6920.       if (dir->got.refcount < 0)
  6921.         dir->got.refcount = 0;
  6922.       dir->got.refcount += ind->got.refcount;
  6923.       ind->got.refcount = htab->init_got_refcount.refcount;
  6924.     }
  6925.  
  6926.   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
  6927.     {
  6928.       if (dir->plt.refcount < 0)
  6929.         dir->plt.refcount = 0;
  6930.       dir->plt.refcount += ind->plt.refcount;
  6931.       ind->plt.refcount = htab->init_plt_refcount.refcount;
  6932.     }
  6933.  
  6934.   if (ind->dynindx != -1)
  6935.     {
  6936.       if (dir->dynindx != -1)
  6937.         _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
  6938.       dir->dynindx = ind->dynindx;
  6939.       dir->dynstr_index = ind->dynstr_index;
  6940.       ind->dynindx = -1;
  6941.       ind->dynstr_index = 0;
  6942.     }
  6943. }
  6944.  
  6945. void
  6946. _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
  6947.                                 struct elf_link_hash_entry *h,
  6948.                                 bfd_boolean force_local)
  6949. {
  6950.   /* STT_GNU_IFUNC symbol must go through PLT.  */
  6951.   if (h->type != STT_GNU_IFUNC)
  6952.     {
  6953.       h->plt = elf_hash_table (info)->init_plt_offset;
  6954.       h->needs_plt = 0;
  6955.     }
  6956.   if (force_local)
  6957.     {
  6958.       h->forced_local = 1;
  6959.       if (h->dynindx != -1)
  6960.         {
  6961.           h->dynindx = -1;
  6962.           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  6963.                                   h->dynstr_index);
  6964.         }
  6965.     }
  6966. }
  6967.  
  6968. /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
  6969.    caller.  */
  6970.  
  6971. bfd_boolean
  6972. _bfd_elf_link_hash_table_init
  6973.   (struct elf_link_hash_table *table,
  6974.    bfd *abfd,
  6975.    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  6976.                                       struct bfd_hash_table *,
  6977.                                       const char *),
  6978.    unsigned int entsize,
  6979.    enum elf_target_id target_id)
  6980. {
  6981.   bfd_boolean ret;
  6982.   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
  6983.  
  6984.   table->init_got_refcount.refcount = can_refcount - 1;
  6985.   table->init_plt_refcount.refcount = can_refcount - 1;
  6986.   table->init_got_offset.offset = -(bfd_vma) 1;
  6987.   table->init_plt_offset.offset = -(bfd_vma) 1;
  6988.   /* The first dynamic symbol is a dummy.  */
  6989.   table->dynsymcount = 1;
  6990.  
  6991.   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  6992.  
  6993.   table->root.type = bfd_link_elf_hash_table;
  6994.   table->hash_table_id = target_id;
  6995.  
  6996.   return ret;
  6997. }
  6998.  
  6999. /* Create an ELF linker hash table.  */
  7000.  
  7001. struct bfd_link_hash_table *
  7002. _bfd_elf_link_hash_table_create (bfd *abfd)
  7003. {
  7004.   struct elf_link_hash_table *ret;
  7005.   bfd_size_type amt = sizeof (struct elf_link_hash_table);
  7006.  
  7007.   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
  7008.   if (ret == NULL)
  7009.     return NULL;
  7010.  
  7011.   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
  7012.                                        sizeof (struct elf_link_hash_entry),
  7013.                                        GENERIC_ELF_DATA))
  7014.     {
  7015.       free (ret);
  7016.       return NULL;
  7017.     }
  7018.   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
  7019.  
  7020.   return &ret->root;
  7021. }
  7022.  
  7023. /* Destroy an ELF linker hash table.  */
  7024.  
  7025. void
  7026. _bfd_elf_link_hash_table_free (bfd *obfd)
  7027. {
  7028.   struct elf_link_hash_table *htab;
  7029.  
  7030.   htab = (struct elf_link_hash_table *) obfd->link.hash;
  7031.   if (htab->dynstr != NULL)
  7032.     _bfd_elf_strtab_free (htab->dynstr);
  7033.   _bfd_merge_sections_free (htab->merge_info);
  7034.   _bfd_generic_link_hash_table_free (obfd);
  7035. }
  7036.  
  7037. /* This is a hook for the ELF emulation code in the generic linker to
  7038.    tell the backend linker what file name to use for the DT_NEEDED
  7039.    entry for a dynamic object.  */
  7040.  
  7041. void
  7042. bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
  7043. {
  7044.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7045.       && bfd_get_format (abfd) == bfd_object)
  7046.     elf_dt_name (abfd) = name;
  7047. }
  7048.  
  7049. int
  7050. bfd_elf_get_dyn_lib_class (bfd *abfd)
  7051. {
  7052.   int lib_class;
  7053.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7054.       && bfd_get_format (abfd) == bfd_object)
  7055.     lib_class = elf_dyn_lib_class (abfd);
  7056.   else
  7057.     lib_class = 0;
  7058.   return lib_class;
  7059. }
  7060.  
  7061. void
  7062. bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
  7063. {
  7064.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7065.       && bfd_get_format (abfd) == bfd_object)
  7066.     elf_dyn_lib_class (abfd) = lib_class;
  7067. }
  7068.  
  7069. /* Get the list of DT_NEEDED entries for a link.  This is a hook for
  7070.    the linker ELF emulation code.  */
  7071.  
  7072. struct bfd_link_needed_list *
  7073. bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
  7074.                          struct bfd_link_info *info)
  7075. {
  7076.   if (! is_elf_hash_table (info->hash))
  7077.     return NULL;
  7078.   return elf_hash_table (info)->needed;
  7079. }
  7080.  
  7081. /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
  7082.    hook for the linker ELF emulation code.  */
  7083.  
  7084. struct bfd_link_needed_list *
  7085. bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
  7086.                           struct bfd_link_info *info)
  7087. {
  7088.   if (! is_elf_hash_table (info->hash))
  7089.     return NULL;
  7090.   return elf_hash_table (info)->runpath;
  7091. }
  7092.  
  7093. /* Get the name actually used for a dynamic object for a link.  This
  7094.    is the SONAME entry if there is one.  Otherwise, it is the string
  7095.    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
  7096.  
  7097. const char *
  7098. bfd_elf_get_dt_soname (bfd *abfd)
  7099. {
  7100.   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7101.       && bfd_get_format (abfd) == bfd_object)
  7102.     return elf_dt_name (abfd);
  7103.   return NULL;
  7104. }
  7105.  
  7106. /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
  7107.    the ELF linker emulation code.  */
  7108.  
  7109. bfd_boolean
  7110. bfd_elf_get_bfd_needed_list (bfd *abfd,
  7111.                              struct bfd_link_needed_list **pneeded)
  7112. {
  7113.   asection *s;
  7114.   bfd_byte *dynbuf = NULL;
  7115.   unsigned int elfsec;
  7116.   unsigned long shlink;
  7117.   bfd_byte *extdyn, *extdynend;
  7118.   size_t extdynsize;
  7119.   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  7120.  
  7121.   *pneeded = NULL;
  7122.  
  7123.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
  7124.       || bfd_get_format (abfd) != bfd_object)
  7125.     return TRUE;
  7126.  
  7127.   s = bfd_get_section_by_name (abfd, ".dynamic");
  7128.   if (s == NULL || s->size == 0)
  7129.     return TRUE;
  7130.  
  7131.   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  7132.     goto error_return;
  7133.  
  7134.   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  7135.   if (elfsec == SHN_BAD)
  7136.     goto error_return;
  7137.  
  7138.   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  7139.  
  7140.   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  7141.   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  7142.  
  7143.   extdyn = dynbuf;
  7144.   extdynend = extdyn + s->size;
  7145.   for (; extdyn < extdynend; extdyn += extdynsize)
  7146.     {
  7147.       Elf_Internal_Dyn dyn;
  7148.  
  7149.       (*swap_dyn_in) (abfd, extdyn, &dyn);
  7150.  
  7151.       if (dyn.d_tag == DT_NULL)
  7152.         break;
  7153.  
  7154.       if (dyn.d_tag == DT_NEEDED)
  7155.         {
  7156.           const char *string;
  7157.           struct bfd_link_needed_list *l;
  7158.           unsigned int tagv = dyn.d_un.d_val;
  7159.           bfd_size_type amt;
  7160.  
  7161.           string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  7162.           if (string == NULL)
  7163.             goto error_return;
  7164.  
  7165.           amt = sizeof *l;
  7166.           l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  7167.           if (l == NULL)
  7168.             goto error_return;
  7169.  
  7170.           l->by = abfd;
  7171.           l->name = string;
  7172.           l->next = *pneeded;
  7173.           *pneeded = l;
  7174.         }
  7175.     }
  7176.  
  7177.   free (dynbuf);
  7178.  
  7179.   return TRUE;
  7180.  
  7181.  error_return:
  7182.   if (dynbuf != NULL)
  7183.     free (dynbuf);
  7184.   return FALSE;
  7185. }
  7186.  
  7187. struct elf_symbuf_symbol
  7188. {
  7189.   unsigned long st_name;        /* Symbol name, index in string tbl */
  7190.   unsigned char st_info;        /* Type and binding attributes */
  7191.   unsigned char st_other;       /* Visibilty, and target specific */
  7192. };
  7193.  
  7194. struct elf_symbuf_head
  7195. {
  7196.   struct elf_symbuf_symbol *ssym;
  7197.   bfd_size_type count;
  7198.   unsigned int st_shndx;
  7199. };
  7200.  
  7201. struct elf_symbol
  7202. {
  7203.   union
  7204.     {
  7205.       Elf_Internal_Sym *isym;
  7206.       struct elf_symbuf_symbol *ssym;
  7207.     } u;
  7208.   const char *name;
  7209. };
  7210.  
  7211. /* Sort references to symbols by ascending section number.  */
  7212.  
  7213. static int
  7214. elf_sort_elf_symbol (const void *arg1, const void *arg2)
  7215. {
  7216.   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
  7217.   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
  7218.  
  7219.   return s1->st_shndx - s2->st_shndx;
  7220. }
  7221.  
  7222. static int
  7223. elf_sym_name_compare (const void *arg1, const void *arg2)
  7224. {
  7225.   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
  7226.   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
  7227.   return strcmp (s1->name, s2->name);
  7228. }
  7229.  
  7230. static struct elf_symbuf_head *
  7231. elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
  7232. {
  7233.   Elf_Internal_Sym **ind, **indbufend, **indbuf;
  7234.   struct elf_symbuf_symbol *ssym;
  7235.   struct elf_symbuf_head *ssymbuf, *ssymhead;
  7236.   bfd_size_type i, shndx_count, total_size;
  7237.  
  7238.   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
  7239.   if (indbuf == NULL)
  7240.     return NULL;
  7241.  
  7242.   for (ind = indbuf, i = 0; i < symcount; i++)
  7243.     if (isymbuf[i].st_shndx != SHN_UNDEF)
  7244.       *ind++ = &isymbuf[i];
  7245.   indbufend = ind;
  7246.  
  7247.   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
  7248.          elf_sort_elf_symbol);
  7249.  
  7250.   shndx_count = 0;
  7251.   if (indbufend > indbuf)
  7252.     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
  7253.       if (ind[0]->st_shndx != ind[1]->st_shndx)
  7254.         shndx_count++;
  7255.  
  7256.   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
  7257.                 + (indbufend - indbuf) * sizeof (*ssym));
  7258.   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
  7259.   if (ssymbuf == NULL)
  7260.     {
  7261.       free (indbuf);
  7262.       return NULL;
  7263.     }
  7264.  
  7265.   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
  7266.   ssymbuf->ssym = NULL;
  7267.   ssymbuf->count = shndx_count;
  7268.   ssymbuf->st_shndx = 0;
  7269.   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
  7270.     {
  7271.       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
  7272.         {
  7273.           ssymhead++;
  7274.           ssymhead->ssym = ssym;
  7275.           ssymhead->count = 0;
  7276.           ssymhead->st_shndx = (*ind)->st_shndx;
  7277.         }
  7278.       ssym->st_name = (*ind)->st_name;
  7279.       ssym->st_info = (*ind)->st_info;
  7280.       ssym->st_other = (*ind)->st_other;
  7281.       ssymhead->count++;
  7282.     }
  7283.   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
  7284.               && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
  7285.                   == total_size));
  7286.  
  7287.   free (indbuf);
  7288.   return ssymbuf;
  7289. }
  7290.  
  7291. /* Check if 2 sections define the same set of local and global
  7292.    symbols.  */
  7293.  
  7294. static bfd_boolean
  7295. bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
  7296.                                    struct bfd_link_info *info)
  7297. {
  7298.   bfd *bfd1, *bfd2;
  7299.   const struct elf_backend_data *bed1, *bed2;
  7300.   Elf_Internal_Shdr *hdr1, *hdr2;
  7301.   bfd_size_type symcount1, symcount2;
  7302.   Elf_Internal_Sym *isymbuf1, *isymbuf2;
  7303.   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
  7304.   Elf_Internal_Sym *isym, *isymend;
  7305.   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
  7306.   bfd_size_type count1, count2, i;
  7307.   unsigned int shndx1, shndx2;
  7308.   bfd_boolean result;
  7309.  
  7310.   bfd1 = sec1->owner;
  7311.   bfd2 = sec2->owner;
  7312.  
  7313.   /* Both sections have to be in ELF.  */
  7314.   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
  7315.       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
  7316.     return FALSE;
  7317.  
  7318.   if (elf_section_type (sec1) != elf_section_type (sec2))
  7319.     return FALSE;
  7320.  
  7321.   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
  7322.   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
  7323.   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
  7324.     return FALSE;
  7325.  
  7326.   bed1 = get_elf_backend_data (bfd1);
  7327.   bed2 = get_elf_backend_data (bfd2);
  7328.   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
  7329.   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
  7330.   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
  7331.   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
  7332.  
  7333.   if (symcount1 == 0 || symcount2 == 0)
  7334.     return FALSE;
  7335.  
  7336.   result = FALSE;
  7337.   isymbuf1 = NULL;
  7338.   isymbuf2 = NULL;
  7339.   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
  7340.   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
  7341.  
  7342.   if (ssymbuf1 == NULL)
  7343.     {
  7344.       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
  7345.                                        NULL, NULL, NULL);
  7346.       if (isymbuf1 == NULL)
  7347.         goto done;
  7348.  
  7349.       if (!info->reduce_memory_overheads)
  7350.         elf_tdata (bfd1)->symbuf = ssymbuf1
  7351.           = elf_create_symbuf (symcount1, isymbuf1);
  7352.     }
  7353.  
  7354.   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
  7355.     {
  7356.       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
  7357.                                        NULL, NULL, NULL);
  7358.       if (isymbuf2 == NULL)
  7359.         goto done;
  7360.  
  7361.       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
  7362.         elf_tdata (bfd2)->symbuf = ssymbuf2
  7363.           = elf_create_symbuf (symcount2, isymbuf2);
  7364.     }
  7365.  
  7366.   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
  7367.     {
  7368.       /* Optimized faster version.  */
  7369.       bfd_size_type lo, hi, mid;
  7370.       struct elf_symbol *symp;
  7371.       struct elf_symbuf_symbol *ssym, *ssymend;
  7372.  
  7373.       lo = 0;
  7374.       hi = ssymbuf1->count;
  7375.       ssymbuf1++;
  7376.       count1 = 0;
  7377.       while (lo < hi)
  7378.         {
  7379.           mid = (lo + hi) / 2;
  7380.           if (shndx1 < ssymbuf1[mid].st_shndx)
  7381.             hi = mid;
  7382.           else if (shndx1 > ssymbuf1[mid].st_shndx)
  7383.             lo = mid + 1;
  7384.           else
  7385.             {
  7386.               count1 = ssymbuf1[mid].count;
  7387.               ssymbuf1 += mid;
  7388.               break;
  7389.             }
  7390.         }
  7391.  
  7392.       lo = 0;
  7393.       hi = ssymbuf2->count;
  7394.       ssymbuf2++;
  7395.       count2 = 0;
  7396.       while (lo < hi)
  7397.         {
  7398.           mid = (lo + hi) / 2;
  7399.           if (shndx2 < ssymbuf2[mid].st_shndx)
  7400.             hi = mid;
  7401.           else if (shndx2 > ssymbuf2[mid].st_shndx)
  7402.             lo = mid + 1;
  7403.           else
  7404.             {
  7405.               count2 = ssymbuf2[mid].count;
  7406.               ssymbuf2 += mid;
  7407.               break;
  7408.             }
  7409.         }
  7410.  
  7411.       if (count1 == 0 || count2 == 0 || count1 != count2)
  7412.         goto done;
  7413.  
  7414.       symtable1
  7415.         = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
  7416.       symtable2
  7417.         = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
  7418.       if (symtable1 == NULL || symtable2 == NULL)
  7419.         goto done;
  7420.  
  7421.       symp = symtable1;
  7422.       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
  7423.            ssym < ssymend; ssym++, symp++)
  7424.         {
  7425.           symp->u.ssym = ssym;
  7426.           symp->name = bfd_elf_string_from_elf_section (bfd1,
  7427.                                                         hdr1->sh_link,
  7428.                                                         ssym->st_name);
  7429.         }
  7430.  
  7431.       symp = symtable2;
  7432.       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
  7433.            ssym < ssymend; ssym++, symp++)
  7434.         {
  7435.           symp->u.ssym = ssym;
  7436.           symp->name = bfd_elf_string_from_elf_section (bfd2,
  7437.                                                         hdr2->sh_link,
  7438.                                                         ssym->st_name);
  7439.         }
  7440.  
  7441.       /* Sort symbol by name.  */
  7442.       qsort (symtable1, count1, sizeof (struct elf_symbol),
  7443.              elf_sym_name_compare);
  7444.       qsort (symtable2, count1, sizeof (struct elf_symbol),
  7445.              elf_sym_name_compare);
  7446.  
  7447.       for (i = 0; i < count1; i++)
  7448.         /* Two symbols must have the same binding, type and name.  */
  7449.         if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
  7450.             || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
  7451.             || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7452.           goto done;
  7453.  
  7454.       result = TRUE;
  7455.       goto done;
  7456.     }
  7457.  
  7458.   symtable1 = (struct elf_symbol *)
  7459.       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
  7460.   symtable2 = (struct elf_symbol *)
  7461.       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
  7462.   if (symtable1 == NULL || symtable2 == NULL)
  7463.     goto done;
  7464.  
  7465.   /* Count definitions in the section.  */
  7466.   count1 = 0;
  7467.   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
  7468.     if (isym->st_shndx == shndx1)
  7469.       symtable1[count1++].u.isym = isym;
  7470.  
  7471.   count2 = 0;
  7472.   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
  7473.     if (isym->st_shndx == shndx2)
  7474.       symtable2[count2++].u.isym = isym;
  7475.  
  7476.   if (count1 == 0 || count2 == 0 || count1 != count2)
  7477.     goto done;
  7478.  
  7479.   for (i = 0; i < count1; i++)
  7480.     symtable1[i].name
  7481.       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
  7482.                                          symtable1[i].u.isym->st_name);
  7483.  
  7484.   for (i = 0; i < count2; i++)
  7485.     symtable2[i].name
  7486.       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
  7487.                                          symtable2[i].u.isym->st_name);
  7488.  
  7489.   /* Sort symbol by name.  */
  7490.   qsort (symtable1, count1, sizeof (struct elf_symbol),
  7491.          elf_sym_name_compare);
  7492.   qsort (symtable2, count1, sizeof (struct elf_symbol),
  7493.          elf_sym_name_compare);
  7494.  
  7495.   for (i = 0; i < count1; i++)
  7496.     /* Two symbols must have the same binding, type and name.  */
  7497.     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
  7498.         || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
  7499.         || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7500.       goto done;
  7501.  
  7502.   result = TRUE;
  7503.  
  7504. done:
  7505.   if (symtable1)
  7506.     free (symtable1);
  7507.   if (symtable2)
  7508.     free (symtable2);
  7509.   if (isymbuf1)
  7510.     free (isymbuf1);
  7511.   if (isymbuf2)
  7512.     free (isymbuf2);
  7513.  
  7514.   return result;
  7515. }
  7516.  
  7517. /* Return TRUE if 2 section types are compatible.  */
  7518.  
  7519. bfd_boolean
  7520. _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
  7521.                                  bfd *bbfd, const asection *bsec)
  7522. {
  7523.   if (asec == NULL
  7524.       || bsec == NULL
  7525.       || abfd->xvec->flavour != bfd_target_elf_flavour
  7526.       || bbfd->xvec->flavour != bfd_target_elf_flavour)
  7527.     return TRUE;
  7528.  
  7529.   return elf_section_type (asec) == elf_section_type (bsec);
  7530. }
  7531. /* Final phase of ELF linker.  */
  7532.  
  7533. /* A structure we use to avoid passing large numbers of arguments.  */
  7534.  
  7535. struct elf_final_link_info
  7536. {
  7537.   /* General link information.  */
  7538.   struct bfd_link_info *info;
  7539.   /* Output BFD.  */
  7540.   bfd *output_bfd;
  7541.   /* Symbol string table.  */
  7542.   struct elf_strtab_hash *symstrtab;
  7543.   /* .hash section.  */
  7544.   asection *hash_sec;
  7545.   /* symbol version section (.gnu.version).  */
  7546.   asection *symver_sec;
  7547.   /* Buffer large enough to hold contents of any section.  */
  7548.   bfd_byte *contents;
  7549.   /* Buffer large enough to hold external relocs of any section.  */
  7550.   void *external_relocs;
  7551.   /* Buffer large enough to hold internal relocs of any section.  */
  7552.   Elf_Internal_Rela *internal_relocs;
  7553.   /* Buffer large enough to hold external local symbols of any input
  7554.      BFD.  */
  7555.   bfd_byte *external_syms;
  7556.   /* And a buffer for symbol section indices.  */
  7557.   Elf_External_Sym_Shndx *locsym_shndx;
  7558.   /* Buffer large enough to hold internal local symbols of any input
  7559.      BFD.  */
  7560.   Elf_Internal_Sym *internal_syms;
  7561.   /* Array large enough to hold a symbol index for each local symbol
  7562.      of any input BFD.  */
  7563.   long *indices;
  7564.   /* Array large enough to hold a section pointer for each local
  7565.      symbol of any input BFD.  */
  7566.   asection **sections;
  7567.   /* Buffer for SHT_SYMTAB_SHNDX section.  */
  7568.   Elf_External_Sym_Shndx *symshndxbuf;
  7569.   /* Number of STT_FILE syms seen.  */
  7570.   size_t filesym_count;
  7571. };
  7572.  
  7573. /* This struct is used to pass information to elf_link_output_extsym.  */
  7574.  
  7575. struct elf_outext_info
  7576. {
  7577.   bfd_boolean failed;
  7578.   bfd_boolean localsyms;
  7579.   bfd_boolean file_sym_done;
  7580.   struct elf_final_link_info *flinfo;
  7581. };
  7582.  
  7583.  
  7584. /* Support for evaluating a complex relocation.
  7585.  
  7586.    Complex relocations are generalized, self-describing relocations.  The
  7587.    implementation of them consists of two parts: complex symbols, and the
  7588.    relocations themselves.
  7589.  
  7590.    The relocations are use a reserved elf-wide relocation type code (R_RELC
  7591.    external / BFD_RELOC_RELC internal) and an encoding of relocation field
  7592.    information (start bit, end bit, word width, etc) into the addend.  This
  7593.    information is extracted from CGEN-generated operand tables within gas.
  7594.  
  7595.    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
  7596.    internal) representing prefix-notation expressions, including but not
  7597.    limited to those sorts of expressions normally encoded as addends in the
  7598.    addend field.  The symbol mangling format is:
  7599.  
  7600.    <node> := <literal>
  7601.           |  <unary-operator> ':' <node>
  7602.           |  <binary-operator> ':' <node> ':' <node>
  7603.           ;
  7604.  
  7605.    <literal> := 's' <digits=N> ':' <N character symbol name>
  7606.              |  'S' <digits=N> ':' <N character section name>
  7607.              |  '#' <hexdigits>
  7608.              ;
  7609.  
  7610.    <binary-operator> := as in C
  7611.    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
  7612.  
  7613. static void
  7614. set_symbol_value (bfd *bfd_with_globals,
  7615.                   Elf_Internal_Sym *isymbuf,
  7616.                   size_t locsymcount,
  7617.                   size_t symidx,
  7618.                   bfd_vma val)
  7619. {
  7620.   struct elf_link_hash_entry **sym_hashes;
  7621.   struct elf_link_hash_entry *h;
  7622.   size_t extsymoff = locsymcount;
  7623.  
  7624.   if (symidx < locsymcount)
  7625.     {
  7626.       Elf_Internal_Sym *sym;
  7627.  
  7628.       sym = isymbuf + symidx;
  7629.       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  7630.         {
  7631.           /* It is a local symbol: move it to the
  7632.              "absolute" section and give it a value.  */
  7633.           sym->st_shndx = SHN_ABS;
  7634.           sym->st_value = val;
  7635.           return;
  7636.         }
  7637.       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
  7638.       extsymoff = 0;
  7639.     }
  7640.  
  7641.   /* It is a global symbol: set its link type
  7642.      to "defined" and give it a value.  */
  7643.  
  7644.   sym_hashes = elf_sym_hashes (bfd_with_globals);
  7645.   h = sym_hashes [symidx - extsymoff];
  7646.   while (h->root.type == bfd_link_hash_indirect
  7647.          || h->root.type == bfd_link_hash_warning)
  7648.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  7649.   h->root.type = bfd_link_hash_defined;
  7650.   h->root.u.def.value = val;
  7651.   h->root.u.def.section = bfd_abs_section_ptr;
  7652. }
  7653.  
  7654. static bfd_boolean
  7655. resolve_symbol (const char *name,
  7656.                 bfd *input_bfd,
  7657.                 struct elf_final_link_info *flinfo,
  7658.                 bfd_vma *result,
  7659.                 Elf_Internal_Sym *isymbuf,
  7660.                 size_t locsymcount)
  7661. {
  7662.   Elf_Internal_Sym *sym;
  7663.   struct bfd_link_hash_entry *global_entry;
  7664.   const char *candidate = NULL;
  7665.   Elf_Internal_Shdr *symtab_hdr;
  7666.   size_t i;
  7667.  
  7668.   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  7669.  
  7670.   for (i = 0; i < locsymcount; ++ i)
  7671.     {
  7672.       sym = isymbuf + i;
  7673.  
  7674.       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
  7675.         continue;
  7676.  
  7677.       candidate = bfd_elf_string_from_elf_section (input_bfd,
  7678.                                                    symtab_hdr->sh_link,
  7679.                                                    sym->st_name);
  7680. #ifdef DEBUG
  7681.       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
  7682.               name, candidate, (unsigned long) sym->st_value);
  7683. #endif
  7684.       if (candidate && strcmp (candidate, name) == 0)
  7685.         {
  7686.           asection *sec = flinfo->sections [i];
  7687.  
  7688.           *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
  7689.           *result += sec->output_offset + sec->output_section->vma;
  7690. #ifdef DEBUG
  7691.           printf ("Found symbol with value %8.8lx\n",
  7692.                   (unsigned long) *result);
  7693. #endif
  7694.           return TRUE;
  7695.         }
  7696.     }
  7697.  
  7698.   /* Hmm, haven't found it yet. perhaps it is a global.  */
  7699.   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
  7700.                                        FALSE, FALSE, TRUE);
  7701.   if (!global_entry)
  7702.     return FALSE;
  7703.  
  7704.   if (global_entry->type == bfd_link_hash_defined
  7705.       || global_entry->type == bfd_link_hash_defweak)
  7706.     {
  7707.       *result = (global_entry->u.def.value
  7708.                  + global_entry->u.def.section->output_section->vma
  7709.                  + global_entry->u.def.section->output_offset);
  7710. #ifdef DEBUG
  7711.       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
  7712.               global_entry->root.string, (unsigned long) *result);
  7713. #endif
  7714.       return TRUE;
  7715.     }
  7716.  
  7717.   return FALSE;
  7718. }
  7719.  
  7720. static bfd_boolean
  7721. resolve_section (const char *name,
  7722.                  asection *sections,
  7723.                  bfd_vma *result)
  7724. {
  7725.   asection *curr;
  7726.   unsigned int len;
  7727.  
  7728.   for (curr = sections; curr; curr = curr->next)
  7729.     if (strcmp (curr->name, name) == 0)
  7730.       {
  7731.         *result = curr->vma;
  7732.         return TRUE;
  7733.       }
  7734.  
  7735.   /* Hmm. still haven't found it. try pseudo-section names.  */
  7736.   for (curr = sections; curr; curr = curr->next)
  7737.     {
  7738.       len = strlen (curr->name);
  7739.       if (len > strlen (name))
  7740.         continue;
  7741.  
  7742.       if (strncmp (curr->name, name, len) == 0)
  7743.         {
  7744.           if (strncmp (".end", name + len, 4) == 0)
  7745.             {
  7746.               *result = curr->vma + curr->size;
  7747.               return TRUE;
  7748.             }
  7749.  
  7750.           /* Insert more pseudo-section names here, if you like.  */
  7751.         }
  7752.     }
  7753.  
  7754.   return FALSE;
  7755. }
  7756.  
  7757. static void
  7758. undefined_reference (const char *reftype, const char *name)
  7759. {
  7760.   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
  7761.                       reftype, name);
  7762. }
  7763.  
  7764. static bfd_boolean
  7765. eval_symbol (bfd_vma *result,
  7766.              const char **symp,
  7767.              bfd *input_bfd,
  7768.              struct elf_final_link_info *flinfo,
  7769.              bfd_vma dot,
  7770.              Elf_Internal_Sym *isymbuf,
  7771.              size_t locsymcount,
  7772.              int signed_p)
  7773. {
  7774.   size_t len;
  7775.   size_t symlen;
  7776.   bfd_vma a;
  7777.   bfd_vma b;
  7778.   char symbuf[4096];
  7779.   const char *sym = *symp;
  7780.   const char *symend;
  7781.   bfd_boolean symbol_is_section = FALSE;
  7782.  
  7783.   len = strlen (sym);
  7784.   symend = sym + len;
  7785.  
  7786.   if (len < 1 || len > sizeof (symbuf))
  7787.     {
  7788.       bfd_set_error (bfd_error_invalid_operation);
  7789.       return FALSE;
  7790.     }
  7791.  
  7792.   switch (* sym)
  7793.     {
  7794.     case '.':
  7795.       *result = dot;
  7796.       *symp = sym + 1;
  7797.       return TRUE;
  7798.  
  7799.     case '#':
  7800.       ++sym;
  7801.       *result = strtoul (sym, (char **) symp, 16);
  7802.       return TRUE;
  7803.  
  7804.     case 'S':
  7805.       symbol_is_section = TRUE;
  7806.     case 's':
  7807.       ++sym;
  7808.       symlen = strtol (sym, (char **) symp, 10);
  7809.       sym = *symp + 1; /* Skip the trailing ':'.  */
  7810.  
  7811.       if (symend < sym || symlen + 1 > sizeof (symbuf))
  7812.         {
  7813.           bfd_set_error (bfd_error_invalid_operation);
  7814.           return FALSE;
  7815.         }
  7816.  
  7817.       memcpy (symbuf, sym, symlen);
  7818.       symbuf[symlen] = '\0';
  7819.       *symp = sym + symlen;
  7820.  
  7821.       /* Is it always possible, with complex symbols, that gas "mis-guessed"
  7822.          the symbol as a section, or vice-versa. so we're pretty liberal in our
  7823.          interpretation here; section means "try section first", not "must be a
  7824.          section", and likewise with symbol.  */
  7825.  
  7826.       if (symbol_is_section)
  7827.         {
  7828.           if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
  7829.               && !resolve_symbol (symbuf, input_bfd, flinfo, result,
  7830.                                   isymbuf, locsymcount))
  7831.             {
  7832.               undefined_reference ("section", symbuf);
  7833.               return FALSE;
  7834.             }
  7835.         }
  7836.       else
  7837.         {
  7838.           if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
  7839.                                isymbuf, locsymcount)
  7840.               && !resolve_section (symbuf, flinfo->output_bfd->sections,
  7841.                                    result))
  7842.             {
  7843.               undefined_reference ("symbol", symbuf);
  7844.               return FALSE;
  7845.             }
  7846.         }
  7847.  
  7848.       return TRUE;
  7849.  
  7850.       /* All that remains are operators.  */
  7851.  
  7852. #define UNARY_OP(op)                                            \
  7853.   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
  7854.     {                                                           \
  7855.       sym += strlen (#op);                                      \
  7856.       if (*sym == ':')                                          \
  7857.         ++sym;                                                  \
  7858.       *symp = sym;                                              \
  7859.       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,       \
  7860.                         isymbuf, locsymcount, signed_p))        \
  7861.         return FALSE;                                           \
  7862.       if (signed_p)                                             \
  7863.         *result = op ((bfd_signed_vma) a);                      \
  7864.       else                                                      \
  7865.         *result = op a;                                         \
  7866.       return TRUE;                                              \
  7867.     }
  7868.  
  7869. #define BINARY_OP(op)                                           \
  7870.   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
  7871.     {                                                           \
  7872.       sym += strlen (#op);                                      \
  7873.       if (*sym == ':')                                          \
  7874.         ++sym;                                                  \
  7875.       *symp = sym;                                              \
  7876.       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,       \
  7877.                         isymbuf, locsymcount, signed_p))        \
  7878.         return FALSE;                                           \
  7879.       ++*symp;                                                  \
  7880.       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,       \
  7881.                         isymbuf, locsymcount, signed_p))        \
  7882.         return FALSE;                                           \
  7883.       if (signed_p)                                             \
  7884.         *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
  7885.       else                                                      \
  7886.         *result = a op b;                                       \
  7887.       return TRUE;                                              \
  7888.     }
  7889.  
  7890.     default:
  7891.       UNARY_OP  (0-);
  7892.       BINARY_OP (<<);
  7893.       BINARY_OP (>>);
  7894.       BINARY_OP (==);
  7895.       BINARY_OP (!=);
  7896.       BINARY_OP (<=);
  7897.       BINARY_OP (>=);
  7898.       BINARY_OP (&&);
  7899.       BINARY_OP (||);
  7900.       UNARY_OP  (~);
  7901.       UNARY_OP  (!);
  7902.       BINARY_OP (*);
  7903.       BINARY_OP (/);
  7904.       BINARY_OP (%);
  7905.       BINARY_OP (^);
  7906.       BINARY_OP (|);
  7907.       BINARY_OP (&);
  7908.       BINARY_OP (+);
  7909.       BINARY_OP (-);
  7910.       BINARY_OP (<);
  7911.       BINARY_OP (>);
  7912. #undef UNARY_OP
  7913. #undef BINARY_OP
  7914.       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
  7915.       bfd_set_error (bfd_error_invalid_operation);
  7916.       return FALSE;
  7917.     }
  7918. }
  7919.  
  7920. static void
  7921. put_value (bfd_vma size,
  7922.            unsigned long chunksz,
  7923.            bfd *input_bfd,
  7924.            bfd_vma x,
  7925.            bfd_byte *location)
  7926. {
  7927.   location += (size - chunksz);
  7928.  
  7929.   for (; size; size -= chunksz, location -= chunksz)
  7930.     {
  7931.       switch (chunksz)
  7932.         {
  7933.         case 1:
  7934.           bfd_put_8 (input_bfd, x, location);
  7935.           x >>= 8;
  7936.           break;
  7937.         case 2:
  7938.           bfd_put_16 (input_bfd, x, location);
  7939.           x >>= 16;
  7940.           break;
  7941.         case 4:
  7942.           bfd_put_32 (input_bfd, x, location);
  7943.           /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
  7944.           x >>= 16;
  7945.           x >>= 16;
  7946.           break;
  7947. #ifdef BFD64
  7948.         case 8:
  7949.           bfd_put_64 (input_bfd, x, location);
  7950.           /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
  7951.           x >>= 32;
  7952.           x >>= 32;
  7953.           break;
  7954. #endif
  7955.         default:
  7956.           abort ();
  7957.           break;
  7958.         }
  7959.     }
  7960. }
  7961.  
  7962. static bfd_vma
  7963. get_value (bfd_vma size,
  7964.            unsigned long chunksz,
  7965.            bfd *input_bfd,
  7966.            bfd_byte *location)
  7967. {
  7968.   int shift;
  7969.   bfd_vma x = 0;
  7970.  
  7971.   /* Sanity checks.  */
  7972.   BFD_ASSERT (chunksz <= sizeof (x)
  7973.               && size >= chunksz
  7974.               && chunksz != 0
  7975.               && (size % chunksz) == 0
  7976.               && input_bfd != NULL
  7977.               && location != NULL);
  7978.  
  7979.   if (chunksz == sizeof (x))
  7980.     {
  7981.       BFD_ASSERT (size == chunksz);
  7982.  
  7983.       /* Make sure that we do not perform an undefined shift operation.
  7984.          We know that size == chunksz so there will only be one iteration
  7985.          of the loop below.  */
  7986.       shift = 0;
  7987.     }
  7988.   else
  7989.     shift = 8 * chunksz;
  7990.  
  7991.   for (; size; size -= chunksz, location += chunksz)
  7992.     {
  7993.       switch (chunksz)
  7994.         {
  7995.         case 1:
  7996.           x = (x << shift) | bfd_get_8 (input_bfd, location);
  7997.           break;
  7998.         case 2:
  7999.           x = (x << shift) | bfd_get_16 (input_bfd, location);
  8000.           break;
  8001.         case 4:
  8002.           x = (x << shift) | bfd_get_32 (input_bfd, location);
  8003.           break;
  8004. #ifdef BFD64
  8005.         case 8:
  8006.           x = (x << shift) | bfd_get_64 (input_bfd, location);
  8007.           break;
  8008. #endif
  8009.         default:
  8010.           abort ();
  8011.         }
  8012.     }
  8013.   return x;
  8014. }
  8015.  
  8016. static void
  8017. decode_complex_addend (unsigned long *start,   /* in bits */
  8018.                        unsigned long *oplen,   /* in bits */
  8019.                        unsigned long *len,     /* in bits */
  8020.                        unsigned long *wordsz,  /* in bytes */
  8021.                        unsigned long *chunksz, /* in bytes */
  8022.                        unsigned long *lsb0_p,
  8023.                        unsigned long *signed_p,
  8024.                        unsigned long *trunc_p,
  8025.                        unsigned long encoded)
  8026. {
  8027.   * start     =  encoded        & 0x3F;
  8028.   * len       = (encoded >>  6) & 0x3F;
  8029.   * oplen     = (encoded >> 12) & 0x3F;
  8030.   * wordsz    = (encoded >> 18) & 0xF;
  8031.   * chunksz   = (encoded >> 22) & 0xF;
  8032.   * lsb0_p    = (encoded >> 27) & 1;
  8033.   * signed_p  = (encoded >> 28) & 1;
  8034.   * trunc_p   = (encoded >> 29) & 1;
  8035. }
  8036.  
  8037. bfd_reloc_status_type
  8038. bfd_elf_perform_complex_relocation (bfd *input_bfd,
  8039.                                     asection *input_section ATTRIBUTE_UNUSED,
  8040.                                     bfd_byte *contents,
  8041.                                     Elf_Internal_Rela *rel,
  8042.                                     bfd_vma relocation)
  8043. {
  8044.   bfd_vma shift, x, mask;
  8045.   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
  8046.   bfd_reloc_status_type r;
  8047.  
  8048.   /*  Perform this reloc, since it is complex.
  8049.       (this is not to say that it necessarily refers to a complex
  8050.       symbol; merely that it is a self-describing CGEN based reloc.
  8051.       i.e. the addend has the complete reloc information (bit start, end,
  8052.       word size, etc) encoded within it.).  */
  8053.  
  8054.   decode_complex_addend (&start, &oplen, &len, &wordsz,
  8055.                          &chunksz, &lsb0_p, &signed_p,
  8056.                          &trunc_p, rel->r_addend);
  8057.  
  8058.   mask = (((1L << (len - 1)) - 1) << 1) | 1;
  8059.  
  8060.   if (lsb0_p)
  8061.     shift = (start + 1) - len;
  8062.   else
  8063.     shift = (8 * wordsz) - (start + len);
  8064.  
  8065.   /* FIXME: octets_per_byte.  */
  8066.   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
  8067.  
  8068. #ifdef DEBUG
  8069.   printf ("Doing complex reloc: "
  8070.           "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
  8071.           "chunksz %ld, start %ld, len %ld, oplen %ld\n"
  8072.           "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
  8073.           lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
  8074.           oplen, (unsigned long) x, (unsigned long) mask,
  8075.           (unsigned long) relocation);
  8076. #endif
  8077.  
  8078.   r = bfd_reloc_ok;
  8079.   if (! trunc_p)
  8080.     /* Now do an overflow check.  */
  8081.     r = bfd_check_overflow ((signed_p
  8082.                              ? complain_overflow_signed
  8083.                              : complain_overflow_unsigned),
  8084.                             len, 0, (8 * wordsz),
  8085.                             relocation);
  8086.  
  8087.   /* Do the deed.  */
  8088.   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
  8089.  
  8090. #ifdef DEBUG
  8091.   printf ("           relocation: %8.8lx\n"
  8092.           "         shifted mask: %8.8lx\n"
  8093.           " shifted/masked reloc: %8.8lx\n"
  8094.           "               result: %8.8lx\n",
  8095.           (unsigned long) relocation, (unsigned long) (mask << shift),
  8096.           (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
  8097. #endif
  8098.   /* FIXME: octets_per_byte.  */
  8099.   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
  8100.   return r;
  8101. }
  8102.  
  8103. /* Functions to read r_offset from external (target order) reloc
  8104.    entry.  Faster than bfd_getl32 et al, because we let the compiler
  8105.    know the value is aligned.  */
  8106.  
  8107. static bfd_vma
  8108. ext32l_r_offset (const void *p)
  8109. {
  8110.   union aligned32
  8111.   {
  8112.     uint32_t v;
  8113.     unsigned char c[4];
  8114.   };
  8115.   const union aligned32 *a
  8116.     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
  8117.  
  8118.   uint32_t aval = (  (uint32_t) a->c[0]
  8119.                    | (uint32_t) a->c[1] << 8
  8120.                    | (uint32_t) a->c[2] << 16
  8121.                    | (uint32_t) a->c[3] << 24);
  8122.   return aval;
  8123. }
  8124.  
  8125. static bfd_vma
  8126. ext32b_r_offset (const void *p)
  8127. {
  8128.   union aligned32
  8129.   {
  8130.     uint32_t v;
  8131.     unsigned char c[4];
  8132.   };
  8133.   const union aligned32 *a
  8134.     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
  8135.  
  8136.   uint32_t aval = (  (uint32_t) a->c[0] << 24
  8137.                    | (uint32_t) a->c[1] << 16
  8138.                    | (uint32_t) a->c[2] << 8
  8139.                    | (uint32_t) a->c[3]);
  8140.   return aval;
  8141. }
  8142.  
  8143. #ifdef BFD_HOST_64_BIT
  8144. static bfd_vma
  8145. ext64l_r_offset (const void *p)
  8146. {
  8147.   union aligned64
  8148.   {
  8149.     uint64_t v;
  8150.     unsigned char c[8];
  8151.   };
  8152.   const union aligned64 *a
  8153.     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
  8154.  
  8155.   uint64_t aval = (  (uint64_t) a->c[0]
  8156.                    | (uint64_t) a->c[1] << 8
  8157.                    | (uint64_t) a->c[2] << 16
  8158.                    | (uint64_t) a->c[3] << 24
  8159.                    | (uint64_t) a->c[4] << 32
  8160.                    | (uint64_t) a->c[5] << 40
  8161.                    | (uint64_t) a->c[6] << 48
  8162.                    | (uint64_t) a->c[7] << 56);
  8163.   return aval;
  8164. }
  8165.  
  8166. static bfd_vma
  8167. ext64b_r_offset (const void *p)
  8168. {
  8169.   union aligned64
  8170.   {
  8171.     uint64_t v;
  8172.     unsigned char c[8];
  8173.   };
  8174.   const union aligned64 *a
  8175.     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
  8176.  
  8177.   uint64_t aval = (  (uint64_t) a->c[0] << 56
  8178.                    | (uint64_t) a->c[1] << 48
  8179.                    | (uint64_t) a->c[2] << 40
  8180.                    | (uint64_t) a->c[3] << 32
  8181.                    | (uint64_t) a->c[4] << 24
  8182.                    | (uint64_t) a->c[5] << 16
  8183.                    | (uint64_t) a->c[6] << 8
  8184.                    | (uint64_t) a->c[7]);
  8185.   return aval;
  8186. }
  8187. #endif
  8188.  
  8189. /* When performing a relocatable link, the input relocations are
  8190.    preserved.  But, if they reference global symbols, the indices
  8191.    referenced must be updated.  Update all the relocations found in
  8192.    RELDATA.  */
  8193.  
  8194. static bfd_boolean
  8195. elf_link_adjust_relocs (bfd *abfd,
  8196.                         struct bfd_elf_section_reloc_data *reldata,
  8197.                         bfd_boolean sort)
  8198. {
  8199.   unsigned int i;
  8200.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8201.   bfd_byte *erela;
  8202.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  8203.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  8204.   bfd_vma r_type_mask;
  8205.   int r_sym_shift;
  8206.   unsigned int count = reldata->count;
  8207.   struct elf_link_hash_entry **rel_hash = reldata->hashes;
  8208.  
  8209.   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
  8210.     {
  8211.       swap_in = bed->s->swap_reloc_in;
  8212.       swap_out = bed->s->swap_reloc_out;
  8213.     }
  8214.   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
  8215.     {
  8216.       swap_in = bed->s->swap_reloca_in;
  8217.       swap_out = bed->s->swap_reloca_out;
  8218.     }
  8219.   else
  8220.     abort ();
  8221.  
  8222.   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
  8223.     abort ();
  8224.  
  8225.   if (bed->s->arch_size == 32)
  8226.     {
  8227.       r_type_mask = 0xff;
  8228.       r_sym_shift = 8;
  8229.     }
  8230.   else
  8231.     {
  8232.       r_type_mask = 0xffffffff;
  8233.       r_sym_shift = 32;
  8234.     }
  8235.  
  8236.   erela = reldata->hdr->contents;
  8237.   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
  8238.     {
  8239.       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
  8240.       unsigned int j;
  8241.  
  8242.       if (*rel_hash == NULL)
  8243.         continue;
  8244.  
  8245.       BFD_ASSERT ((*rel_hash)->indx >= 0);
  8246.  
  8247.       (*swap_in) (abfd, erela, irela);
  8248.       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
  8249.         irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
  8250.                            | (irela[j].r_info & r_type_mask));
  8251.       (*swap_out) (abfd, irela, erela);
  8252.     }
  8253.  
  8254.   if (sort && count != 0)
  8255.     {
  8256.       bfd_vma (*ext_r_off) (const void *);
  8257.       bfd_vma r_off;
  8258.       size_t elt_size;
  8259.       bfd_byte *base, *end, *p, *loc;
  8260.       bfd_byte *buf = NULL;
  8261.  
  8262.       if (bed->s->arch_size == 32)
  8263.         {
  8264.           if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
  8265.             ext_r_off = ext32l_r_offset;
  8266.           else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
  8267.             ext_r_off = ext32b_r_offset;
  8268.           else
  8269.             abort ();
  8270.         }
  8271.       else
  8272.         {
  8273. #ifdef BFD_HOST_64_BIT
  8274.           if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
  8275.             ext_r_off = ext64l_r_offset;
  8276.           else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
  8277.             ext_r_off = ext64b_r_offset;
  8278.           else
  8279. #endif
  8280.             abort ();
  8281.         }
  8282.  
  8283.       /*  Must use a stable sort here.  A modified insertion sort,
  8284.           since the relocs are mostly sorted already.  */
  8285.       elt_size = reldata->hdr->sh_entsize;
  8286.       base = reldata->hdr->contents;
  8287.       end = base + count * elt_size;
  8288.       if (elt_size > sizeof (Elf64_External_Rela))
  8289.         abort ();
  8290.  
  8291.       /* Ensure the first element is lowest.  This acts as a sentinel,
  8292.          speeding the main loop below.  */
  8293.       r_off = (*ext_r_off) (base);
  8294.       for (p = loc = base; (p += elt_size) < end; )
  8295.         {
  8296.           bfd_vma r_off2 = (*ext_r_off) (p);
  8297.           if (r_off > r_off2)
  8298.             {
  8299.               r_off = r_off2;
  8300.               loc = p;
  8301.             }
  8302.         }
  8303.       if (loc != base)
  8304.         {
  8305.           /* Don't just swap *base and *loc as that changes the order
  8306.              of the original base[0] and base[1] if they happen to
  8307.              have the same r_offset.  */
  8308.           bfd_byte onebuf[sizeof (Elf64_External_Rela)];
  8309.           memcpy (onebuf, loc, elt_size);
  8310.           memmove (base + elt_size, base, loc - base);
  8311.           memcpy (base, onebuf, elt_size);
  8312.         }
  8313.  
  8314.       for (p = base + elt_size; (p += elt_size) < end; )
  8315.         {
  8316.           /* base to p is sorted, *p is next to insert.  */
  8317.           r_off = (*ext_r_off) (p);
  8318.           /* Search the sorted region for location to insert.  */
  8319.           loc = p - elt_size;
  8320.           while (r_off < (*ext_r_off) (loc))
  8321.             loc -= elt_size;
  8322.           loc += elt_size;
  8323.           if (loc != p)
  8324.             {
  8325.               /* Chances are there is a run of relocs to insert here,
  8326.                  from one of more input files.  Files are not always
  8327.                  linked in order due to the way elf_link_input_bfd is
  8328.                  called.  See pr17666.  */
  8329.               size_t sortlen = p - loc;
  8330.               bfd_vma r_off2 = (*ext_r_off) (loc);
  8331.               size_t runlen = elt_size;
  8332.               size_t buf_size = 96 * 1024;
  8333.               while (p + runlen < end
  8334.                      && (sortlen <= buf_size
  8335.                          || runlen + elt_size <= buf_size)
  8336.                      && r_off2 > (*ext_r_off) (p + runlen))
  8337.                 runlen += elt_size;
  8338.               if (buf == NULL)
  8339.                 {
  8340.                   buf = bfd_malloc (buf_size);
  8341.                   if (buf == NULL)
  8342.                     return FALSE;
  8343.                 }
  8344.               if (runlen < sortlen)
  8345.                 {
  8346.                   memcpy (buf, p, runlen);
  8347.                   memmove (loc + runlen, loc, sortlen);
  8348.                   memcpy (loc, buf, runlen);
  8349.                 }
  8350.               else
  8351.                 {
  8352.                   memcpy (buf, loc, sortlen);
  8353.                   memmove (loc, p, runlen);
  8354.                   memcpy (loc + runlen, buf, sortlen);
  8355.                 }
  8356.               p += runlen - elt_size;
  8357.             }
  8358.         }
  8359.       /* Hashes are no longer valid.  */
  8360.       free (reldata->hashes);
  8361.       reldata->hashes = NULL;
  8362.       free (buf);
  8363.     }
  8364.   return TRUE;
  8365. }
  8366.  
  8367. struct elf_link_sort_rela
  8368. {
  8369.   union {
  8370.     bfd_vma offset;
  8371.     bfd_vma sym_mask;
  8372.   } u;
  8373.   enum elf_reloc_type_class type;
  8374.   /* We use this as an array of size int_rels_per_ext_rel.  */
  8375.   Elf_Internal_Rela rela[1];
  8376. };
  8377.  
  8378. static int
  8379. elf_link_sort_cmp1 (const void *A, const void *B)
  8380. {
  8381.   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8382.   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8383.   int relativea, relativeb;
  8384.  
  8385.   relativea = a->type == reloc_class_relative;
  8386.   relativeb = b->type == reloc_class_relative;
  8387.  
  8388.   if (relativea < relativeb)
  8389.     return 1;
  8390.   if (relativea > relativeb)
  8391.     return -1;
  8392.   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
  8393.     return -1;
  8394.   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
  8395.     return 1;
  8396.   if (a->rela->r_offset < b->rela->r_offset)
  8397.     return -1;
  8398.   if (a->rela->r_offset > b->rela->r_offset)
  8399.     return 1;
  8400.   return 0;
  8401. }
  8402.  
  8403. static int
  8404. elf_link_sort_cmp2 (const void *A, const void *B)
  8405. {
  8406.   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8407.   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8408.  
  8409.   if (a->type < b->type)
  8410.     return -1;
  8411.   if (a->type > b->type)
  8412.     return 1;
  8413.   if (a->u.offset < b->u.offset)
  8414.     return -1;
  8415.   if (a->u.offset > b->u.offset)
  8416.     return 1;
  8417.   if (a->rela->r_offset < b->rela->r_offset)
  8418.     return -1;
  8419.   if (a->rela->r_offset > b->rela->r_offset)
  8420.     return 1;
  8421.   return 0;
  8422. }
  8423.  
  8424. static size_t
  8425. elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
  8426. {
  8427.   asection *dynamic_relocs;
  8428.   asection *rela_dyn;
  8429.   asection *rel_dyn;
  8430.   bfd_size_type count, size;
  8431.   size_t i, ret, sort_elt, ext_size;
  8432.   bfd_byte *sort, *s_non_relative, *p;
  8433.   struct elf_link_sort_rela *sq;
  8434.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8435.   int i2e = bed->s->int_rels_per_ext_rel;
  8436.   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  8437.   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  8438.   struct bfd_link_order *lo;
  8439.   bfd_vma r_sym_mask;
  8440.   bfd_boolean use_rela;
  8441.  
  8442.   /* Find a dynamic reloc section.  */
  8443.   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
  8444.   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
  8445.   if (rela_dyn != NULL && rela_dyn->size > 0
  8446.       && rel_dyn != NULL && rel_dyn->size > 0)
  8447.     {
  8448.       bfd_boolean use_rela_initialised = FALSE;
  8449.  
  8450.       /* This is just here to stop gcc from complaining.
  8451.          It's initialization checking code is not perfect.  */
  8452.       use_rela = TRUE;
  8453.  
  8454.       /* Both sections are present.  Examine the sizes
  8455.          of the indirect sections to help us choose.  */
  8456.       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8457.         if (lo->type == bfd_indirect_link_order)
  8458.           {
  8459.             asection *o = lo->u.indirect.section;
  8460.  
  8461.             if ((o->size % bed->s->sizeof_rela) == 0)
  8462.               {
  8463.                 if ((o->size % bed->s->sizeof_rel) == 0)
  8464.                   /* Section size is divisible by both rel and rela sizes.
  8465.                      It is of no help to us.  */
  8466.                   ;
  8467.                 else
  8468.                   {
  8469.                     /* Section size is only divisible by rela.  */
  8470.                     if (use_rela_initialised && (use_rela == FALSE))
  8471.                       {
  8472.                         _bfd_error_handler
  8473.                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8474.                         bfd_set_error (bfd_error_invalid_operation);
  8475.                         return 0;
  8476.                       }
  8477.                     else
  8478.                       {
  8479.                         use_rela = TRUE;
  8480.                         use_rela_initialised = TRUE;
  8481.                       }
  8482.                   }
  8483.               }
  8484.             else if ((o->size % bed->s->sizeof_rel) == 0)
  8485.               {
  8486.                 /* Section size is only divisible by rel.  */
  8487.                 if (use_rela_initialised && (use_rela == TRUE))
  8488.                   {
  8489.                     _bfd_error_handler
  8490.                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8491.                     bfd_set_error (bfd_error_invalid_operation);
  8492.                     return 0;
  8493.                   }
  8494.                 else
  8495.                   {
  8496.                     use_rela = FALSE;
  8497.                     use_rela_initialised = TRUE;
  8498.                   }
  8499.               }
  8500.             else
  8501.               {
  8502.                 /* The section size is not divisible by either - something is wrong.  */
  8503.                 _bfd_error_handler
  8504.                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
  8505.                 bfd_set_error (bfd_error_invalid_operation);
  8506.                 return 0;
  8507.               }
  8508.           }
  8509.  
  8510.       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8511.         if (lo->type == bfd_indirect_link_order)
  8512.           {
  8513.             asection *o = lo->u.indirect.section;
  8514.  
  8515.             if ((o->size % bed->s->sizeof_rela) == 0)
  8516.               {
  8517.                 if ((o->size % bed->s->sizeof_rel) == 0)
  8518.                   /* Section size is divisible by both rel and rela sizes.
  8519.                      It is of no help to us.  */
  8520.                   ;
  8521.                 else
  8522.                   {
  8523.                     /* Section size is only divisible by rela.  */
  8524.                     if (use_rela_initialised && (use_rela == FALSE))
  8525.                       {
  8526.                         _bfd_error_handler
  8527.                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8528.                         bfd_set_error (bfd_error_invalid_operation);
  8529.                         return 0;
  8530.                       }
  8531.                     else
  8532.                       {
  8533.                         use_rela = TRUE;
  8534.                         use_rela_initialised = TRUE;
  8535.                       }
  8536.                   }
  8537.               }
  8538.             else if ((o->size % bed->s->sizeof_rel) == 0)
  8539.               {
  8540.                 /* Section size is only divisible by rel.  */
  8541.                 if (use_rela_initialised && (use_rela == TRUE))
  8542.                   {
  8543.                     _bfd_error_handler
  8544.                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
  8545.                     bfd_set_error (bfd_error_invalid_operation);
  8546.                     return 0;
  8547.                   }
  8548.                 else
  8549.                   {
  8550.                     use_rela = FALSE;
  8551.                     use_rela_initialised = TRUE;
  8552.                   }
  8553.               }
  8554.             else
  8555.               {
  8556.                 /* The section size is not divisible by either - something is wrong.  */
  8557.                 _bfd_error_handler
  8558.                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
  8559.                 bfd_set_error (bfd_error_invalid_operation);
  8560.                 return 0;
  8561.               }
  8562.           }
  8563.  
  8564.       if (! use_rela_initialised)
  8565.         /* Make a guess.  */
  8566.         use_rela = TRUE;
  8567.     }
  8568.   else if (rela_dyn != NULL && rela_dyn->size > 0)
  8569.     use_rela = TRUE;
  8570.   else if (rel_dyn != NULL && rel_dyn->size > 0)
  8571.     use_rela = FALSE;
  8572.   else
  8573.     return 0;
  8574.  
  8575.   if (use_rela)
  8576.     {
  8577.       dynamic_relocs = rela_dyn;
  8578.       ext_size = bed->s->sizeof_rela;
  8579.       swap_in = bed->s->swap_reloca_in;
  8580.       swap_out = bed->s->swap_reloca_out;
  8581.     }
  8582.   else
  8583.     {
  8584.       dynamic_relocs = rel_dyn;
  8585.       ext_size = bed->s->sizeof_rel;
  8586.       swap_in = bed->s->swap_reloc_in;
  8587.       swap_out = bed->s->swap_reloc_out;
  8588.     }
  8589.  
  8590.   size = 0;
  8591.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8592.     if (lo->type == bfd_indirect_link_order)
  8593.       size += lo->u.indirect.section->size;
  8594.  
  8595.   if (size != dynamic_relocs->size)
  8596.     return 0;
  8597.  
  8598.   sort_elt = (sizeof (struct elf_link_sort_rela)
  8599.               + (i2e - 1) * sizeof (Elf_Internal_Rela));
  8600.  
  8601.   count = dynamic_relocs->size / ext_size;
  8602.   if (count == 0)
  8603.     return 0;
  8604.   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
  8605.  
  8606.   if (sort == NULL)
  8607.     {
  8608.       (*info->callbacks->warning)
  8609.         (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
  8610.       return 0;
  8611.     }
  8612.  
  8613.   if (bed->s->arch_size == 32)
  8614.     r_sym_mask = ~(bfd_vma) 0xff;
  8615.   else
  8616.     r_sym_mask = ~(bfd_vma) 0xffffffff;
  8617.  
  8618.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8619.     if (lo->type == bfd_indirect_link_order)
  8620.       {
  8621.         bfd_byte *erel, *erelend;
  8622.         asection *o = lo->u.indirect.section;
  8623.  
  8624.         if (o->contents == NULL && o->size != 0)
  8625.           {
  8626.             /* This is a reloc section that is being handled as a normal
  8627.                section.  See bfd_section_from_shdr.  We can't combine
  8628.                relocs in this case.  */
  8629.             free (sort);
  8630.             return 0;
  8631.           }
  8632.         erel = o->contents;
  8633.         erelend = o->contents + o->size;
  8634.         /* FIXME: octets_per_byte.  */
  8635.         p = sort + o->output_offset / ext_size * sort_elt;
  8636.  
  8637.         while (erel < erelend)
  8638.           {
  8639.             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8640.  
  8641.             (*swap_in) (abfd, erel, s->rela);
  8642.             s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
  8643.             s->u.sym_mask = r_sym_mask;
  8644.             p += sort_elt;
  8645.             erel += ext_size;
  8646.           }
  8647.       }
  8648.  
  8649.   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
  8650.  
  8651.   for (i = 0, p = sort; i < count; i++, p += sort_elt)
  8652.     {
  8653.       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8654.       if (s->type != reloc_class_relative)
  8655.         break;
  8656.     }
  8657.   ret = i;
  8658.   s_non_relative = p;
  8659.  
  8660.   sq = (struct elf_link_sort_rela *) s_non_relative;
  8661.   for (; i < count; i++, p += sort_elt)
  8662.     {
  8663.       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
  8664.       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
  8665.         sq = sp;
  8666.       sp->u.offset = sq->rela->r_offset;
  8667.     }
  8668.  
  8669.   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
  8670.  
  8671.   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8672.     if (lo->type == bfd_indirect_link_order)
  8673.       {
  8674.         bfd_byte *erel, *erelend;
  8675.         asection *o = lo->u.indirect.section;
  8676.  
  8677.         erel = o->contents;
  8678.         erelend = o->contents + o->size;
  8679.         /* FIXME: octets_per_byte.  */
  8680.         p = sort + o->output_offset / ext_size * sort_elt;
  8681.         while (erel < erelend)
  8682.           {
  8683.             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8684.             (*swap_out) (abfd, s->rela, erel);
  8685.             p += sort_elt;
  8686.             erel += ext_size;
  8687.           }
  8688.       }
  8689.  
  8690.   free (sort);
  8691.   *psec = dynamic_relocs;
  8692.   return ret;
  8693. }
  8694.  
  8695. /* Add a symbol to the output symbol string table.  */
  8696.  
  8697. static int
  8698. elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
  8699.                            const char *name,
  8700.                            Elf_Internal_Sym *elfsym,
  8701.                            asection *input_sec,
  8702.                            struct elf_link_hash_entry *h)
  8703. {
  8704.   int (*output_symbol_hook)
  8705.     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
  8706.      struct elf_link_hash_entry *);
  8707.   struct elf_link_hash_table *hash_table;
  8708.   const struct elf_backend_data *bed;
  8709.   bfd_size_type strtabsize;
  8710.  
  8711.   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
  8712.  
  8713.   bed = get_elf_backend_data (flinfo->output_bfd);
  8714.   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
  8715.   if (output_symbol_hook != NULL)
  8716.     {
  8717.       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
  8718.       if (ret != 1)
  8719.         return ret;
  8720.     }
  8721.  
  8722.   if (name == NULL
  8723.       || *name == '\0'
  8724.       || (input_sec->flags & SEC_EXCLUDE))
  8725.     elfsym->st_name = (unsigned long) -1;
  8726.   else
  8727.     {
  8728.       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
  8729.          to get the final offset for st_name.  */
  8730.       elfsym->st_name
  8731.         = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
  8732.                                                name, FALSE);
  8733.       if (elfsym->st_name == (unsigned long) -1)
  8734.         return 0;
  8735.     }
  8736.  
  8737.   hash_table = elf_hash_table (flinfo->info);
  8738.   strtabsize = hash_table->strtabsize;
  8739.   if (strtabsize <= hash_table->strtabcount)
  8740.     {
  8741.       strtabsize += strtabsize;
  8742.       hash_table->strtabsize = strtabsize;
  8743.       strtabsize *= sizeof (*hash_table->strtab);
  8744.       hash_table->strtab
  8745.         = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
  8746.                                                  strtabsize);
  8747.       if (hash_table->strtab == NULL)
  8748.         return 0;
  8749.     }
  8750.   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
  8751.   hash_table->strtab[hash_table->strtabcount].dest_index
  8752.     = hash_table->strtabcount;
  8753.   hash_table->strtab[hash_table->strtabcount].destshndx_index
  8754.     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
  8755.  
  8756.   bfd_get_symcount (flinfo->output_bfd) += 1;
  8757.   hash_table->strtabcount += 1;
  8758.  
  8759.   return 1;
  8760. }
  8761.  
  8762. /* Swap symbols out to the symbol table and flush the output symbols to
  8763.    the file.  */
  8764.  
  8765. static bfd_boolean
  8766. elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
  8767. {
  8768.   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
  8769.   bfd_size_type amt, i;
  8770.   const struct elf_backend_data *bed;
  8771.   bfd_byte *symbuf;
  8772.   Elf_Internal_Shdr *hdr;
  8773.   file_ptr pos;
  8774.   bfd_boolean ret;
  8775.  
  8776.   if (!hash_table->strtabcount)
  8777.     return TRUE;
  8778.  
  8779.   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
  8780.  
  8781.   bed = get_elf_backend_data (flinfo->output_bfd);
  8782.  
  8783.   amt = bed->s->sizeof_sym * hash_table->strtabcount;
  8784.   symbuf = (bfd_byte *) bfd_malloc (amt);
  8785.   if (symbuf == NULL)
  8786.     return FALSE;
  8787.  
  8788.   if (flinfo->symshndxbuf)
  8789.     {
  8790.       amt = (sizeof (Elf_External_Sym_Shndx)
  8791.              * (bfd_get_symcount (flinfo->output_bfd)));
  8792.       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
  8793.       if (flinfo->symshndxbuf == NULL)
  8794.         {
  8795.           free (symbuf);
  8796.           return FALSE;
  8797.         }
  8798.     }
  8799.  
  8800.   for (i = 0; i < hash_table->strtabcount; i++)
  8801.     {
  8802.       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
  8803.       if (elfsym->sym.st_name == (unsigned long) -1)
  8804.         elfsym->sym.st_name = 0;
  8805.       else
  8806.         elfsym->sym.st_name
  8807.           = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
  8808.                                                     elfsym->sym.st_name);
  8809.       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
  8810.                                ((bfd_byte *) symbuf
  8811.                                 + (elfsym->dest_index
  8812.                                    * bed->s->sizeof_sym)),
  8813.                                (flinfo->symshndxbuf
  8814.                                 + elfsym->destshndx_index));
  8815.     }
  8816.  
  8817.   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
  8818.   pos = hdr->sh_offset + hdr->sh_size;
  8819.   amt = hash_table->strtabcount * bed->s->sizeof_sym;
  8820.   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
  8821.       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
  8822.     {
  8823.       hdr->sh_size += amt;
  8824.       ret = TRUE;
  8825.     }
  8826.   else
  8827.     ret = FALSE;
  8828.  
  8829.   free (symbuf);
  8830.  
  8831.   free (hash_table->strtab);
  8832.   hash_table->strtab = NULL;
  8833.  
  8834.   return ret;
  8835. }
  8836.  
  8837. /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
  8838.  
  8839. static bfd_boolean
  8840. check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
  8841. {
  8842.   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
  8843.       && sym->st_shndx < SHN_LORESERVE)
  8844.     {
  8845.       /* The gABI doesn't support dynamic symbols in output sections
  8846.          beyond 64k.  */
  8847.       (*_bfd_error_handler)
  8848.         (_("%B: Too many sections: %d (>= %d)"),
  8849.          abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
  8850.       bfd_set_error (bfd_error_nonrepresentable_section);
  8851.       return FALSE;
  8852.     }
  8853.   return TRUE;
  8854. }
  8855.  
  8856. /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
  8857.    allowing an unsatisfied unversioned symbol in the DSO to match a
  8858.    versioned symbol that would normally require an explicit version.
  8859.    We also handle the case that a DSO references a hidden symbol
  8860.    which may be satisfied by a versioned symbol in another DSO.  */
  8861.  
  8862. static bfd_boolean
  8863. elf_link_check_versioned_symbol (struct bfd_link_info *info,
  8864.                                  const struct elf_backend_data *bed,
  8865.                                  struct elf_link_hash_entry *h)
  8866. {
  8867.   bfd *abfd;
  8868.   struct elf_link_loaded_list *loaded;
  8869.  
  8870.   if (!is_elf_hash_table (info->hash))
  8871.     return FALSE;
  8872.  
  8873.   /* Check indirect symbol.  */
  8874.   while (h->root.type == bfd_link_hash_indirect)
  8875.     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  8876.  
  8877.   switch (h->root.type)
  8878.     {
  8879.     default:
  8880.       abfd = NULL;
  8881.       break;
  8882.  
  8883.     case bfd_link_hash_undefined:
  8884.     case bfd_link_hash_undefweak:
  8885.       abfd = h->root.u.undef.abfd;
  8886.       if ((abfd->flags & DYNAMIC) == 0
  8887.           || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
  8888.         return FALSE;
  8889.       break;
  8890.  
  8891.     case bfd_link_hash_defined:
  8892.     case bfd_link_hash_defweak:
  8893.       abfd = h->root.u.def.section->owner;
  8894.       break;
  8895.  
  8896.     case bfd_link_hash_common:
  8897.       abfd = h->root.u.c.p->section->owner;
  8898.       break;
  8899.     }
  8900.   BFD_ASSERT (abfd != NULL);
  8901.  
  8902.   for (loaded = elf_hash_table (info)->loaded;
  8903.        loaded != NULL;
  8904.        loaded = loaded->next)
  8905.     {
  8906.       bfd *input;
  8907.       Elf_Internal_Shdr *hdr;
  8908.       bfd_size_type symcount;
  8909.       bfd_size_type extsymcount;
  8910.       bfd_size_type extsymoff;
  8911.       Elf_Internal_Shdr *versymhdr;
  8912.       Elf_Internal_Sym *isym;
  8913.       Elf_Internal_Sym *isymend;
  8914.       Elf_Internal_Sym *isymbuf;
  8915.       Elf_External_Versym *ever;
  8916.       Elf_External_Versym *extversym;
  8917.  
  8918.       input = loaded->abfd;
  8919.  
  8920.       /* We check each DSO for a possible hidden versioned definition.  */
  8921.       if (input == abfd
  8922.           || (input->flags & DYNAMIC) == 0
  8923.           || elf_dynversym (input) == 0)
  8924.         continue;
  8925.  
  8926.       hdr = &elf_tdata (input)->dynsymtab_hdr;
  8927.  
  8928.       symcount = hdr->sh_size / bed->s->sizeof_sym;
  8929.       if (elf_bad_symtab (input))
  8930.         {
  8931.           extsymcount = symcount;
  8932.           extsymoff = 0;
  8933.         }
  8934.       else
  8935.         {
  8936.           extsymcount = symcount - hdr->sh_info;
  8937.           extsymoff = hdr->sh_info;
  8938.         }
  8939.  
  8940.       if (extsymcount == 0)
  8941.         continue;
  8942.  
  8943.       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
  8944.                                       NULL, NULL, NULL);
  8945.       if (isymbuf == NULL)
  8946.         return FALSE;
  8947.  
  8948.       /* Read in any version definitions.  */
  8949.       versymhdr = &elf_tdata (input)->dynversym_hdr;
  8950.       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
  8951.       if (extversym == NULL)
  8952.         goto error_ret;
  8953.  
  8954.       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
  8955.           || (bfd_bread (extversym, versymhdr->sh_size, input)
  8956.               != versymhdr->sh_size))
  8957.         {
  8958.           free (extversym);
  8959.         error_ret:
  8960.           free (isymbuf);
  8961.           return FALSE;
  8962.         }
  8963.  
  8964.       ever = extversym + extsymoff;
  8965.       isymend = isymbuf + extsymcount;
  8966.       for (isym = isymbuf; isym < isymend; isym++, ever++)
  8967.         {
  8968.           const char *name;
  8969.           Elf_Internal_Versym iver;
  8970.           unsigned short version_index;
  8971.  
  8972.           if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
  8973.               || isym->st_shndx == SHN_UNDEF)
  8974.             continue;
  8975.  
  8976.           name = bfd_elf_string_from_elf_section (input,
  8977.                                                   hdr->sh_link,
  8978.                                                   isym->st_name);
  8979.           if (strcmp (name, h->root.root.string) != 0)
  8980.             continue;
  8981.  
  8982.           _bfd_elf_swap_versym_in (input, ever, &iver);
  8983.  
  8984.           if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  8985.               && !(h->def_regular
  8986.                    && h->forced_local))
  8987.             {
  8988.               /* If we have a non-hidden versioned sym, then it should
  8989.                  have provided a definition for the undefined sym unless
  8990.                  it is defined in a non-shared object and forced local.
  8991.                */
  8992.               abort ();
  8993.             }
  8994.  
  8995.           version_index = iver.vs_vers & VERSYM_VERSION;
  8996.           if (version_index == 1 || version_index == 2)
  8997.             {
  8998.               /* This is the base or first version.  We can use it.  */
  8999.               free (extversym);
  9000.               free (isymbuf);
  9001.               return TRUE;
  9002.             }
  9003.         }
  9004.  
  9005.       free (extversym);
  9006.       free (isymbuf);
  9007.     }
  9008.  
  9009.   return FALSE;
  9010. }
  9011.  
  9012. /* Add an external symbol to the symbol table.  This is called from
  9013.    the hash table traversal routine.  When generating a shared object,
  9014.    we go through the symbol table twice.  The first time we output
  9015.    anything that might have been forced to local scope in a version
  9016.    script.  The second time we output the symbols that are still
  9017.    global symbols.  */
  9018.  
  9019. static bfd_boolean
  9020. elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
  9021. {
  9022.   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
  9023.   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
  9024.   struct elf_final_link_info *flinfo = eoinfo->flinfo;
  9025.   bfd_boolean strip;
  9026.   Elf_Internal_Sym sym;
  9027.   asection *input_sec;
  9028.   const struct elf_backend_data *bed;
  9029.   long indx;
  9030.   int ret;
  9031.   /* A symbol is bound locally if it is forced local or it is locally
  9032.      defined, hidden versioned, not referenced by shared library and
  9033.      not exported when linking executable.  */
  9034.   bfd_boolean local_bind = (h->forced_local
  9035.                             || (bfd_link_executable (flinfo->info)
  9036.                                 && !flinfo->info->export_dynamic
  9037.                                 && !h->dynamic
  9038.                                 && !h->ref_dynamic
  9039.                                 && h->def_regular
  9040.                                 && h->versioned == versioned_hidden));
  9041.  
  9042.   if (h->root.type == bfd_link_hash_warning)
  9043.     {
  9044.       h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9045.       if (h->root.type == bfd_link_hash_new)
  9046.         return TRUE;
  9047.     }
  9048.  
  9049.   /* Decide whether to output this symbol in this pass.  */
  9050.   if (eoinfo->localsyms)
  9051.     {
  9052.       if (!local_bind)
  9053.         return TRUE;
  9054.     }
  9055.   else
  9056.     {
  9057.       if (local_bind)
  9058.         return TRUE;
  9059.     }
  9060.  
  9061.   bed = get_elf_backend_data (flinfo->output_bfd);
  9062.  
  9063.   if (h->root.type == bfd_link_hash_undefined)
  9064.     {
  9065.       /* If we have an undefined symbol reference here then it must have
  9066.          come from a shared library that is being linked in.  (Undefined
  9067.          references in regular files have already been handled unless
  9068.          they are in unreferenced sections which are removed by garbage
  9069.          collection).  */
  9070.       bfd_boolean ignore_undef = FALSE;
  9071.  
  9072.       /* Some symbols may be special in that the fact that they're
  9073.          undefined can be safely ignored - let backend determine that.  */
  9074.       if (bed->elf_backend_ignore_undef_symbol)
  9075.         ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
  9076.  
  9077.       /* If we are reporting errors for this situation then do so now.  */
  9078.       if (!ignore_undef
  9079.           && h->ref_dynamic
  9080.           && (!h->ref_regular || flinfo->info->gc_sections)
  9081.           && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
  9082.           && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
  9083.         {
  9084.           if (!(flinfo->info->callbacks->undefined_symbol
  9085.                 (flinfo->info, h->root.root.string,
  9086.                  h->ref_regular ? NULL : h->root.u.undef.abfd,
  9087.                  NULL, 0,
  9088.                  (flinfo->info->unresolved_syms_in_shared_libs
  9089.                   == RM_GENERATE_ERROR))))
  9090.             {
  9091.               bfd_set_error (bfd_error_bad_value);
  9092.               eoinfo->failed = TRUE;
  9093.               return FALSE;
  9094.             }
  9095.         }
  9096.     }
  9097.  
  9098.   /* We should also warn if a forced local symbol is referenced from
  9099.      shared libraries.  */
  9100.   if (bfd_link_executable (flinfo->info)
  9101.       && h->forced_local
  9102.       && h->ref_dynamic
  9103.       && h->def_regular
  9104.       && !h->dynamic_def
  9105.       && h->ref_dynamic_nonweak
  9106.       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
  9107.     {
  9108.       bfd *def_bfd;
  9109.       const char *msg;
  9110.       struct elf_link_hash_entry *hi = h;
  9111.  
  9112.       /* Check indirect symbol.  */
  9113.       while (hi->root.type == bfd_link_hash_indirect)
  9114.         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  9115.  
  9116.       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  9117.         msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
  9118.       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
  9119.         msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
  9120.       else
  9121.         msg = _("%B: local symbol `%s' in %B is referenced by DSO");
  9122.       def_bfd = flinfo->output_bfd;
  9123.       if (hi->root.u.def.section != bfd_abs_section_ptr)
  9124.         def_bfd = hi->root.u.def.section->owner;
  9125.       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
  9126.                              h->root.root.string);
  9127.       bfd_set_error (bfd_error_bad_value);
  9128.       eoinfo->failed = TRUE;
  9129.       return FALSE;
  9130.     }
  9131.  
  9132.   /* We don't want to output symbols that have never been mentioned by
  9133.      a regular file, or that we have been told to strip.  However, if
  9134.      h->indx is set to -2, the symbol is used by a reloc and we must
  9135.      output it.  */
  9136.   if (h->indx == -2)
  9137.     strip = FALSE;
  9138.   else if ((h->def_dynamic
  9139.             || h->ref_dynamic
  9140.             || h->root.type == bfd_link_hash_new)
  9141.            && !h->def_regular
  9142.            && !h->ref_regular)
  9143.     strip = TRUE;
  9144.   else if (flinfo->info->strip == strip_all)
  9145.     strip = TRUE;
  9146.   else if (flinfo->info->strip == strip_some
  9147.            && bfd_hash_lookup (flinfo->info->keep_hash,
  9148.                                h->root.root.string, FALSE, FALSE) == NULL)
  9149.     strip = TRUE;
  9150.   else if ((h->root.type == bfd_link_hash_defined
  9151.             || h->root.type == bfd_link_hash_defweak)
  9152.            && ((flinfo->info->strip_discarded
  9153.                 && discarded_section (h->root.u.def.section))
  9154.                || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
  9155.                    && h->root.u.def.section->owner != NULL
  9156.                    && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
  9157.     strip = TRUE;
  9158.   else if ((h->root.type == bfd_link_hash_undefined
  9159.             || h->root.type == bfd_link_hash_undefweak)
  9160.            && h->root.u.undef.abfd != NULL
  9161.            && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
  9162.     strip = TRUE;
  9163.   else
  9164.     strip = FALSE;
  9165.  
  9166.   /* If we're stripping it, and it's not a dynamic symbol, there's
  9167.      nothing else to do unless it is a forced local symbol or a
  9168.      STT_GNU_IFUNC symbol.  */
  9169.   if (strip
  9170.       && h->dynindx == -1
  9171.       && h->type != STT_GNU_IFUNC
  9172.       && !h->forced_local)
  9173.     return TRUE;
  9174.  
  9175.   sym.st_value = 0;
  9176.   sym.st_size = h->size;
  9177.   sym.st_other = h->other;
  9178.   if (local_bind)
  9179.     {
  9180.       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
  9181.       /* Turn off visibility on local symbol.  */
  9182.       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  9183.     }
  9184.   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
  9185.   else if (h->unique_global && h->def_regular)
  9186.     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
  9187.   else if (h->root.type == bfd_link_hash_undefweak
  9188.            || h->root.type == bfd_link_hash_defweak)
  9189.     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
  9190.   else
  9191.     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
  9192.   sym.st_target_internal = h->target_internal;
  9193.  
  9194.   switch (h->root.type)
  9195.     {
  9196.     default:
  9197.     case bfd_link_hash_new:
  9198.     case bfd_link_hash_warning:
  9199.       abort ();
  9200.       return FALSE;
  9201.  
  9202.     case bfd_link_hash_undefined:
  9203.     case bfd_link_hash_undefweak:
  9204.       input_sec = bfd_und_section_ptr;
  9205.       sym.st_shndx = SHN_UNDEF;
  9206.       break;
  9207.  
  9208.     case bfd_link_hash_defined:
  9209.     case bfd_link_hash_defweak:
  9210.       {
  9211.         input_sec = h->root.u.def.section;
  9212.         if (input_sec->output_section != NULL)
  9213.           {
  9214.             sym.st_shndx =
  9215.               _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
  9216.                                                  input_sec->output_section);
  9217.             if (sym.st_shndx == SHN_BAD)
  9218.               {
  9219.                 (*_bfd_error_handler)
  9220.                   (_("%B: could not find output section %A for input section %A"),
  9221.                    flinfo->output_bfd, input_sec->output_section, input_sec);
  9222.                 bfd_set_error (bfd_error_nonrepresentable_section);
  9223.                 eoinfo->failed = TRUE;
  9224.                 return FALSE;
  9225.               }
  9226.  
  9227.             /* ELF symbols in relocatable files are section relative,
  9228.                but in nonrelocatable files they are virtual
  9229.                addresses.  */
  9230.             sym.st_value = h->root.u.def.value + input_sec->output_offset;
  9231.             if (!bfd_link_relocatable (flinfo->info))
  9232.               {
  9233.                 sym.st_value += input_sec->output_section->vma;
  9234.                 if (h->type == STT_TLS)
  9235.                   {
  9236.                     asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
  9237.                     if (tls_sec != NULL)
  9238.                       sym.st_value -= tls_sec->vma;
  9239.                   }
  9240.               }
  9241.           }
  9242.         else
  9243.           {
  9244.             BFD_ASSERT (input_sec->owner == NULL
  9245.                         || (input_sec->owner->flags & DYNAMIC) != 0);
  9246.             sym.st_shndx = SHN_UNDEF;
  9247.             input_sec = bfd_und_section_ptr;
  9248.           }
  9249.       }
  9250.       break;
  9251.  
  9252.     case bfd_link_hash_common:
  9253.       input_sec = h->root.u.c.p->section;
  9254.       sym.st_shndx = bed->common_section_index (input_sec);
  9255.       sym.st_value = 1 << h->root.u.c.p->alignment_power;
  9256.       break;
  9257.  
  9258.     case bfd_link_hash_indirect:
  9259.       /* These symbols are created by symbol versioning.  They point
  9260.          to the decorated version of the name.  For example, if the
  9261.          symbol foo@@GNU_1.2 is the default, which should be used when
  9262.          foo is used with no version, then we add an indirect symbol
  9263.          foo which points to foo@@GNU_1.2.  We ignore these symbols,
  9264.          since the indirected symbol is already in the hash table.  */
  9265.       return TRUE;
  9266.     }
  9267.  
  9268.   /* Give the processor backend a chance to tweak the symbol value,
  9269.      and also to finish up anything that needs to be done for this
  9270.      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
  9271.      forced local syms when non-shared is due to a historical quirk.
  9272.      STT_GNU_IFUNC symbol must go through PLT.  */
  9273.   if ((h->type == STT_GNU_IFUNC
  9274.        && h->def_regular
  9275.        && !bfd_link_relocatable (flinfo->info))
  9276.       || ((h->dynindx != -1
  9277.            || h->forced_local)
  9278.           && ((bfd_link_pic (flinfo->info)
  9279.                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  9280.                    || h->root.type != bfd_link_hash_undefweak))
  9281.               || !h->forced_local)
  9282.           && elf_hash_table (flinfo->info)->dynamic_sections_created))
  9283.     {
  9284.       if (! ((*bed->elf_backend_finish_dynamic_symbol)
  9285.              (flinfo->output_bfd, flinfo->info, h, &sym)))
  9286.         {
  9287.           eoinfo->failed = TRUE;
  9288.           return FALSE;
  9289.         }
  9290.     }
  9291.  
  9292.   /* If we are marking the symbol as undefined, and there are no
  9293.      non-weak references to this symbol from a regular object, then
  9294.      mark the symbol as weak undefined; if there are non-weak
  9295.      references, mark the symbol as strong.  We can't do this earlier,
  9296.      because it might not be marked as undefined until the
  9297.      finish_dynamic_symbol routine gets through with it.  */
  9298.   if (sym.st_shndx == SHN_UNDEF
  9299.       && h->ref_regular
  9300.       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
  9301.           || ELF_ST_BIND (sym.st_info) == STB_WEAK))
  9302.     {
  9303.       int bindtype;
  9304.       unsigned int type = ELF_ST_TYPE (sym.st_info);
  9305.  
  9306.       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
  9307.       if (type == STT_GNU_IFUNC)
  9308.         type = STT_FUNC;
  9309.  
  9310.       if (h->ref_regular_nonweak)
  9311.         bindtype = STB_GLOBAL;
  9312.       else
  9313.         bindtype = STB_WEAK;
  9314.       sym.st_info = ELF_ST_INFO (bindtype, type);
  9315.     }
  9316.  
  9317.   /* If this is a symbol defined in a dynamic library, don't use the
  9318.      symbol size from the dynamic library.  Relinking an executable
  9319.      against a new library may introduce gratuitous changes in the
  9320.      executable's symbols if we keep the size.  */
  9321.   if (sym.st_shndx == SHN_UNDEF
  9322.       && !h->def_regular
  9323.       && h->def_dynamic)
  9324.     sym.st_size = 0;
  9325.  
  9326.   /* If a non-weak symbol with non-default visibility is not defined
  9327.      locally, it is a fatal error.  */
  9328.   if (!bfd_link_relocatable (flinfo->info)
  9329.       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
  9330.       && ELF_ST_BIND (sym.st_info) != STB_WEAK
  9331.       && h->root.type == bfd_link_hash_undefined
  9332.       && !h->def_regular)
  9333.     {
  9334.       const char *msg;
  9335.  
  9336.       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
  9337.         msg = _("%B: protected symbol `%s' isn't defined");
  9338.       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
  9339.         msg = _("%B: internal symbol `%s' isn't defined");
  9340.       else
  9341.         msg = _("%B: hidden symbol `%s' isn't defined");
  9342.       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
  9343.       bfd_set_error (bfd_error_bad_value);
  9344.       eoinfo->failed = TRUE;
  9345.       return FALSE;
  9346.     }
  9347.  
  9348.   /* If this symbol should be put in the .dynsym section, then put it
  9349.      there now.  We already know the symbol index.  We also fill in
  9350.      the entry in the .hash section.  */
  9351.   if (elf_hash_table (flinfo->info)->dynsym != NULL
  9352.       && h->dynindx != -1
  9353.       && elf_hash_table (flinfo->info)->dynamic_sections_created)
  9354.     {
  9355.       bfd_byte *esym;
  9356.  
  9357.       /* Since there is no version information in the dynamic string,
  9358.          if there is no version info in symbol version section, we will
  9359.          have a run-time problem if not linking executable, referenced
  9360.          by shared library, not locally defined, or not bound locally.
  9361.       */
  9362.       if (h->verinfo.verdef == NULL
  9363.           && !local_bind
  9364.           && (!bfd_link_executable (flinfo->info)
  9365.               || h->ref_dynamic
  9366.               || !h->def_regular))
  9367.         {
  9368.           char *p = strrchr (h->root.root.string, ELF_VER_CHR);
  9369.  
  9370.           if (p && p [1] != '\0')
  9371.             {
  9372.               (*_bfd_error_handler)
  9373.                 (_("%B: No symbol version section for versioned symbol `%s'"),
  9374.                  flinfo->output_bfd, h->root.root.string);
  9375.               eoinfo->failed = TRUE;
  9376.               return FALSE;
  9377.             }
  9378.         }
  9379.  
  9380.       sym.st_name = h->dynstr_index;
  9381.       esym = (elf_hash_table (flinfo->info)->dynsym->contents
  9382.               + h->dynindx * bed->s->sizeof_sym);
  9383.       if (!check_dynsym (flinfo->output_bfd, &sym))
  9384.         {
  9385.           eoinfo->failed = TRUE;
  9386.           return FALSE;
  9387.         }
  9388.       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
  9389.  
  9390.       if (flinfo->hash_sec != NULL)
  9391.         {
  9392.           size_t hash_entry_size;
  9393.           bfd_byte *bucketpos;
  9394.           bfd_vma chain;
  9395.           size_t bucketcount;
  9396.           size_t bucket;
  9397.  
  9398.           bucketcount = elf_hash_table (flinfo->info)->bucketcount;
  9399.           bucket = h->u.elf_hash_value % bucketcount;
  9400.  
  9401.           hash_entry_size
  9402.             = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
  9403.           bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
  9404.                        + (bucket + 2) * hash_entry_size);
  9405.           chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
  9406.           bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
  9407.                    bucketpos);
  9408.           bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
  9409.                    ((bfd_byte *) flinfo->hash_sec->contents
  9410.                     + (bucketcount + 2 + h->dynindx) * hash_entry_size));
  9411.         }
  9412.  
  9413.       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
  9414.         {
  9415.           Elf_Internal_Versym iversym;
  9416.           Elf_External_Versym *eversym;
  9417.  
  9418.           if (!h->def_regular)
  9419.             {
  9420.               if (h->verinfo.verdef == NULL
  9421.                   || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
  9422.                       & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
  9423.                 iversym.vs_vers = 0;
  9424.               else
  9425.                 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
  9426.             }
  9427.           else
  9428.             {
  9429.               if (h->verinfo.vertree == NULL)
  9430.                 iversym.vs_vers = 1;
  9431.               else
  9432.                 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
  9433.               if (flinfo->info->create_default_symver)
  9434.                 iversym.vs_vers++;
  9435.             }
  9436.  
  9437.           /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
  9438.              defined locally.  */
  9439.           if (h->versioned == versioned_hidden && h->def_regular)
  9440.             iversym.vs_vers |= VERSYM_HIDDEN;
  9441.  
  9442.           eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
  9443.           eversym += h->dynindx;
  9444.           _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
  9445.         }
  9446.     }
  9447.  
  9448.   /* If we're stripping it, then it was just a dynamic symbol, and
  9449.      there's nothing else to do.  */
  9450.   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
  9451.     return TRUE;
  9452.  
  9453.   /* Output a FILE symbol so that following locals are not associated
  9454.      with the wrong input file.  We need one for forced local symbols
  9455.      if we've seen more than one FILE symbol or when we have exactly
  9456.      one FILE symbol but global symbols are present in a file other
  9457.      than the one with the FILE symbol.  We also need one if linker
  9458.      defined symbols are present.  In practice these conditions are
  9459.      always met, so just emit the FILE symbol unconditionally.  */
  9460.   if (eoinfo->localsyms
  9461.       && !eoinfo->file_sym_done
  9462.       && eoinfo->flinfo->filesym_count != 0)
  9463.     {
  9464.       Elf_Internal_Sym fsym;
  9465.  
  9466.       memset (&fsym, 0, sizeof (fsym));
  9467.       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  9468.       fsym.st_shndx = SHN_ABS;
  9469.       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
  9470.                                       bfd_und_section_ptr, NULL))
  9471.         return FALSE;
  9472.  
  9473.       eoinfo->file_sym_done = TRUE;
  9474.     }
  9475.  
  9476.   indx = bfd_get_symcount (flinfo->output_bfd);
  9477.   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
  9478.                                    input_sec, h);
  9479.   if (ret == 0)
  9480.     {
  9481.       eoinfo->failed = TRUE;
  9482.       return FALSE;
  9483.     }
  9484.   else if (ret == 1)
  9485.     h->indx = indx;
  9486.   else if (h->indx == -2)
  9487.     abort();
  9488.  
  9489.   return TRUE;
  9490. }
  9491.  
  9492. /* Return TRUE if special handling is done for relocs in SEC against
  9493.    symbols defined in discarded sections.  */
  9494.  
  9495. static bfd_boolean
  9496. elf_section_ignore_discarded_relocs (asection *sec)
  9497. {
  9498.   const struct elf_backend_data *bed;
  9499.  
  9500.   switch (sec->sec_info_type)
  9501.     {
  9502.     case SEC_INFO_TYPE_STABS:
  9503.     case SEC_INFO_TYPE_EH_FRAME:
  9504.     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
  9505.       return TRUE;
  9506.     default:
  9507.       break;
  9508.     }
  9509.  
  9510.   bed = get_elf_backend_data (sec->owner);
  9511.   if (bed->elf_backend_ignore_discarded_relocs != NULL
  9512.       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
  9513.     return TRUE;
  9514.  
  9515.   return FALSE;
  9516. }
  9517.  
  9518. /* Return a mask saying how ld should treat relocations in SEC against
  9519.    symbols defined in discarded sections.  If this function returns
  9520.    COMPLAIN set, ld will issue a warning message.  If this function
  9521.    returns PRETEND set, and the discarded section was link-once and the
  9522.    same size as the kept link-once section, ld will pretend that the
  9523.    symbol was actually defined in the kept section.  Otherwise ld will
  9524.    zero the reloc (at least that is the intent, but some cooperation by
  9525.    the target dependent code is needed, particularly for REL targets).  */
  9526.  
  9527. unsigned int
  9528. _bfd_elf_default_action_discarded (asection *sec)
  9529. {
  9530.   if (sec->flags & SEC_DEBUGGING)
  9531.     return PRETEND;
  9532.  
  9533.   if (strcmp (".eh_frame", sec->name) == 0)
  9534.     return 0;
  9535.  
  9536.   if (strcmp (".gcc_except_table", sec->name) == 0)
  9537.     return 0;
  9538.  
  9539.   return COMPLAIN | PRETEND;
  9540. }
  9541.  
  9542. /* Find a match between a section and a member of a section group.  */
  9543.  
  9544. static asection *
  9545. match_group_member (asection *sec, asection *group,
  9546.                     struct bfd_link_info *info)
  9547. {
  9548.   asection *first = elf_next_in_group (group);
  9549.   asection *s = first;
  9550.  
  9551.   while (s != NULL)
  9552.     {
  9553.       if (bfd_elf_match_symbols_in_sections (s, sec, info))
  9554.         return s;
  9555.  
  9556.       s = elf_next_in_group (s);
  9557.       if (s == first)
  9558.         break;
  9559.     }
  9560.  
  9561.   return NULL;
  9562. }
  9563.  
  9564. /* Check if the kept section of a discarded section SEC can be used
  9565.    to replace it.  Return the replacement if it is OK.  Otherwise return
  9566.    NULL.  */
  9567.  
  9568. asection *
  9569. _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
  9570. {
  9571.   asection *kept;
  9572.  
  9573.   kept = sec->kept_section;
  9574.   if (kept != NULL)
  9575.     {
  9576.       if ((kept->flags & SEC_GROUP) != 0)
  9577.         kept = match_group_member (sec, kept, info);
  9578.       if (kept != NULL
  9579.           && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
  9580.               != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
  9581.         kept = NULL;
  9582.       sec->kept_section = kept;
  9583.     }
  9584.   return kept;
  9585. }
  9586.  
  9587. /* Link an input file into the linker output file.  This function
  9588.    handles all the sections and relocations of the input file at once.
  9589.    This is so that we only have to read the local symbols once, and
  9590.    don't have to keep them in memory.  */
  9591.  
  9592. static bfd_boolean
  9593. elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
  9594. {
  9595.   int (*relocate_section)
  9596.     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  9597.      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  9598.   bfd *output_bfd;
  9599.   Elf_Internal_Shdr *symtab_hdr;
  9600.   size_t locsymcount;
  9601.   size_t extsymoff;
  9602.   Elf_Internal_Sym *isymbuf;
  9603.   Elf_Internal_Sym *isym;
  9604.   Elf_Internal_Sym *isymend;
  9605.   long *pindex;
  9606.   asection **ppsection;
  9607.   asection *o;
  9608.   const struct elf_backend_data *bed;
  9609.   struct elf_link_hash_entry **sym_hashes;
  9610.   bfd_size_type address_size;
  9611.   bfd_vma r_type_mask;
  9612.   int r_sym_shift;
  9613.   bfd_boolean have_file_sym = FALSE;
  9614.  
  9615.   output_bfd = flinfo->output_bfd;
  9616.   bed = get_elf_backend_data (output_bfd);
  9617.   relocate_section = bed->elf_backend_relocate_section;
  9618.  
  9619.   /* If this is a dynamic object, we don't want to do anything here:
  9620.      we don't want the local symbols, and we don't want the section
  9621.      contents.  */
  9622.   if ((input_bfd->flags & DYNAMIC) != 0)
  9623.     return TRUE;
  9624.  
  9625.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  9626.   if (elf_bad_symtab (input_bfd))
  9627.     {
  9628.       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  9629.       extsymoff = 0;
  9630.     }
  9631.   else
  9632.     {
  9633.       locsymcount = symtab_hdr->sh_info;
  9634.       extsymoff = symtab_hdr->sh_info;
  9635.     }
  9636.  
  9637.   /* Read the local symbols.  */
  9638.   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  9639.   if (isymbuf == NULL && locsymcount != 0)
  9640.     {
  9641.       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
  9642.                                       flinfo->internal_syms,
  9643.                                       flinfo->external_syms,
  9644.                                       flinfo->locsym_shndx);
  9645.       if (isymbuf == NULL)
  9646.         return FALSE;
  9647.     }
  9648.  
  9649.   /* Find local symbol sections and adjust values of symbols in
  9650.      SEC_MERGE sections.  Write out those local symbols we know are
  9651.      going into the output file.  */
  9652.   isymend = isymbuf + locsymcount;
  9653.   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
  9654.        isym < isymend;
  9655.        isym++, pindex++, ppsection++)
  9656.     {
  9657.       asection *isec;
  9658.       const char *name;
  9659.       Elf_Internal_Sym osym;
  9660.       long indx;
  9661.       int ret;
  9662.  
  9663.       *pindex = -1;
  9664.  
  9665.       if (elf_bad_symtab (input_bfd))
  9666.         {
  9667.           if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
  9668.             {
  9669.               *ppsection = NULL;
  9670.               continue;
  9671.             }
  9672.         }
  9673.  
  9674.       if (isym->st_shndx == SHN_UNDEF)
  9675.         isec = bfd_und_section_ptr;
  9676.       else if (isym->st_shndx == SHN_ABS)
  9677.         isec = bfd_abs_section_ptr;
  9678.       else if (isym->st_shndx == SHN_COMMON)
  9679.         isec = bfd_com_section_ptr;
  9680.       else
  9681.         {
  9682.           isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  9683.           if (isec == NULL)
  9684.             {
  9685.               /* Don't attempt to output symbols with st_shnx in the
  9686.                  reserved range other than SHN_ABS and SHN_COMMON.  */
  9687.               *ppsection = NULL;
  9688.               continue;
  9689.             }
  9690.           else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
  9691.                    && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
  9692.             isym->st_value =
  9693.               _bfd_merged_section_offset (output_bfd, &isec,
  9694.                                           elf_section_data (isec)->sec_info,
  9695.                                           isym->st_value);
  9696.         }
  9697.  
  9698.       *ppsection = isec;
  9699.  
  9700.       /* Don't output the first, undefined, symbol.  */
  9701.       if (ppsection == flinfo->sections)
  9702.         continue;
  9703.  
  9704.       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  9705.         {
  9706.           /* We never output section symbols.  Instead, we use the
  9707.              section symbol of the corresponding section in the output
  9708.              file.  */
  9709.           continue;
  9710.         }
  9711.  
  9712.       /* If we are stripping all symbols, we don't want to output this
  9713.          one.  */
  9714.       if (flinfo->info->strip == strip_all)
  9715.         continue;
  9716.  
  9717.       /* If we are discarding all local symbols, we don't want to
  9718.          output this one.  If we are generating a relocatable output
  9719.          file, then some of the local symbols may be required by
  9720.          relocs; we output them below as we discover that they are
  9721.          needed.  */
  9722.       if (flinfo->info->discard == discard_all)
  9723.         continue;
  9724.  
  9725.       /* If this symbol is defined in a section which we are
  9726.          discarding, we don't need to keep it.  */
  9727.       if (isym->st_shndx != SHN_UNDEF
  9728.           && isym->st_shndx < SHN_LORESERVE
  9729.           && bfd_section_removed_from_list (output_bfd,
  9730.                                             isec->output_section))
  9731.         continue;
  9732.  
  9733.       /* Get the name of the symbol.  */
  9734.       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
  9735.                                               isym->st_name);
  9736.       if (name == NULL)
  9737.         return FALSE;
  9738.  
  9739.       /* See if we are discarding symbols with this name.  */
  9740.       if ((flinfo->info->strip == strip_some
  9741.            && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
  9742.                == NULL))
  9743.           || (((flinfo->info->discard == discard_sec_merge
  9744.                 && (isec->flags & SEC_MERGE)
  9745.                 && !bfd_link_relocatable (flinfo->info))
  9746.                || flinfo->info->discard == discard_l)
  9747.               && bfd_is_local_label_name (input_bfd, name)))
  9748.         continue;
  9749.  
  9750.       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
  9751.         {
  9752.           if (input_bfd->lto_output)
  9753.             /* -flto puts a temp file name here.  This means builds
  9754.                are not reproducible.  Discard the symbol.  */
  9755.             continue;
  9756.           have_file_sym = TRUE;
  9757.           flinfo->filesym_count += 1;
  9758.         }
  9759.       if (!have_file_sym)
  9760.         {
  9761.           /* In the absence of debug info, bfd_find_nearest_line uses
  9762.              FILE symbols to determine the source file for local
  9763.              function symbols.  Provide a FILE symbol here if input
  9764.              files lack such, so that their symbols won't be
  9765.              associated with a previous input file.  It's not the
  9766.              source file, but the best we can do.  */
  9767.           have_file_sym = TRUE;
  9768.           flinfo->filesym_count += 1;
  9769.           memset (&osym, 0, sizeof (osym));
  9770.           osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  9771.           osym.st_shndx = SHN_ABS;
  9772.           if (!elf_link_output_symstrtab (flinfo,
  9773.                                           (input_bfd->lto_output ? NULL
  9774.                                            : input_bfd->filename),
  9775.                                           &osym, bfd_abs_section_ptr,
  9776.                                           NULL))
  9777.             return FALSE;
  9778.         }
  9779.  
  9780.       osym = *isym;
  9781.  
  9782.       /* Adjust the section index for the output file.  */
  9783.       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9784.                                                          isec->output_section);
  9785.       if (osym.st_shndx == SHN_BAD)
  9786.         return FALSE;
  9787.  
  9788.       /* ELF symbols in relocatable files are section relative, but
  9789.          in executable files they are virtual addresses.  Note that
  9790.          this code assumes that all ELF sections have an associated
  9791.          BFD section with a reasonable value for output_offset; below
  9792.          we assume that they also have a reasonable value for
  9793.          output_section.  Any special sections must be set up to meet
  9794.          these requirements.  */
  9795.       osym.st_value += isec->output_offset;
  9796.       if (!bfd_link_relocatable (flinfo->info))
  9797.         {
  9798.           osym.st_value += isec->output_section->vma;
  9799.           if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
  9800.             {
  9801.               /* STT_TLS symbols are relative to PT_TLS segment base.  */
  9802.               BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
  9803.               osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
  9804.             }
  9805.         }
  9806.  
  9807.       indx = bfd_get_symcount (output_bfd);
  9808.       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
  9809.       if (ret == 0)
  9810.         return FALSE;
  9811.       else if (ret == 1)
  9812.         *pindex = indx;
  9813.     }
  9814.  
  9815.   if (bed->s->arch_size == 32)
  9816.     {
  9817.       r_type_mask = 0xff;
  9818.       r_sym_shift = 8;
  9819.       address_size = 4;
  9820.     }
  9821.   else
  9822.     {
  9823.       r_type_mask = 0xffffffff;
  9824.       r_sym_shift = 32;
  9825.       address_size = 8;
  9826.     }
  9827.  
  9828.   /* Relocate the contents of each section.  */
  9829.   sym_hashes = elf_sym_hashes (input_bfd);
  9830.   for (o = input_bfd->sections; o != NULL; o = o->next)
  9831.     {
  9832.       bfd_byte *contents;
  9833.  
  9834.       if (! o->linker_mark)
  9835.         {
  9836.           /* This section was omitted from the link.  */
  9837.           continue;
  9838.         }
  9839.  
  9840.       if (bfd_link_relocatable (flinfo->info)
  9841.           && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
  9842.         {
  9843.           /* Deal with the group signature symbol.  */
  9844.           struct bfd_elf_section_data *sec_data = elf_section_data (o);
  9845.           unsigned long symndx = sec_data->this_hdr.sh_info;
  9846.           asection *osec = o->output_section;
  9847.  
  9848.           if (symndx >= locsymcount
  9849.               || (elf_bad_symtab (input_bfd)
  9850.                   && flinfo->sections[symndx] == NULL))
  9851.             {
  9852.               struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
  9853.               while (h->root.type == bfd_link_hash_indirect
  9854.                      || h->root.type == bfd_link_hash_warning)
  9855.                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9856.               /* Arrange for symbol to be output.  */
  9857.               h->indx = -2;
  9858.               elf_section_data (osec)->this_hdr.sh_info = -2;
  9859.             }
  9860.           else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
  9861.             {
  9862.               /* We'll use the output section target_index.  */
  9863.               asection *sec = flinfo->sections[symndx]->output_section;
  9864.               elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
  9865.             }
  9866.           else
  9867.             {
  9868.               if (flinfo->indices[symndx] == -1)
  9869.                 {
  9870.                   /* Otherwise output the local symbol now.  */
  9871.                   Elf_Internal_Sym sym = isymbuf[symndx];
  9872.                   asection *sec = flinfo->sections[symndx]->output_section;
  9873.                   const char *name;
  9874.                   long indx;
  9875.                   int ret;
  9876.  
  9877.                   name = bfd_elf_string_from_elf_section (input_bfd,
  9878.                                                           symtab_hdr->sh_link,
  9879.                                                           sym.st_name);
  9880.                   if (name == NULL)
  9881.                     return FALSE;
  9882.  
  9883.                   sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9884.                                                                     sec);
  9885.                   if (sym.st_shndx == SHN_BAD)
  9886.                     return FALSE;
  9887.  
  9888.                   sym.st_value += o->output_offset;
  9889.  
  9890.                   indx = bfd_get_symcount (output_bfd);
  9891.                   ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
  9892.                                                    NULL);
  9893.                   if (ret == 0)
  9894.                     return FALSE;
  9895.                   else if (ret == 1)
  9896.                     flinfo->indices[symndx] = indx;
  9897.                   else
  9898.                     abort ();
  9899.                 }
  9900.               elf_section_data (osec)->this_hdr.sh_info
  9901.                 = flinfo->indices[symndx];
  9902.             }
  9903.         }
  9904.  
  9905.       if ((o->flags & SEC_HAS_CONTENTS) == 0
  9906.           || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
  9907.         continue;
  9908.  
  9909.       if ((o->flags & SEC_LINKER_CREATED) != 0)
  9910.         {
  9911.           /* Section was created by _bfd_elf_link_create_dynamic_sections
  9912.              or somesuch.  */
  9913.           continue;
  9914.         }
  9915.  
  9916.       /* Get the contents of the section.  They have been cached by a
  9917.          relaxation routine.  Note that o is a section in an input
  9918.          file, so the contents field will not have been set by any of
  9919.          the routines which work on output files.  */
  9920.       if (elf_section_data (o)->this_hdr.contents != NULL)
  9921.         {
  9922.           contents = elf_section_data (o)->this_hdr.contents;
  9923.           if (bed->caches_rawsize
  9924.               && o->rawsize != 0
  9925.               && o->rawsize < o->size)
  9926.             {
  9927.               memcpy (flinfo->contents, contents, o->rawsize);
  9928.               contents = flinfo->contents;
  9929.             }
  9930.         }
  9931.       else
  9932.         {
  9933.           contents = flinfo->contents;
  9934.           if (! bfd_get_full_section_contents (input_bfd, o, &contents))
  9935.             return FALSE;
  9936.         }
  9937.  
  9938.       if ((o->flags & SEC_RELOC) != 0)
  9939.         {
  9940.           Elf_Internal_Rela *internal_relocs;
  9941.           Elf_Internal_Rela *rel, *relend;
  9942.           int action_discarded;
  9943.           int ret;
  9944.  
  9945.           /* Get the swapped relocs.  */
  9946.           internal_relocs
  9947.             = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
  9948.                                          flinfo->internal_relocs, FALSE);
  9949.           if (internal_relocs == NULL
  9950.               && o->reloc_count > 0)
  9951.             return FALSE;
  9952.  
  9953.           /* We need to reverse-copy input .ctors/.dtors sections if
  9954.              they are placed in .init_array/.finit_array for output.  */
  9955.           if (o->size > address_size
  9956.               && ((strncmp (o->name, ".ctors", 6) == 0
  9957.                    && strcmp (o->output_section->name,
  9958.                               ".init_array") == 0)
  9959.                   || (strncmp (o->name, ".dtors", 6) == 0
  9960.                       && strcmp (o->output_section->name,
  9961.                                  ".fini_array") == 0))
  9962.               && (o->name[6] == 0 || o->name[6] == '.'))
  9963.             {
  9964.               if (o->size != o->reloc_count * address_size)
  9965.                 {
  9966.                   (*_bfd_error_handler)
  9967.                     (_("error: %B: size of section %A is not "
  9968.                        "multiple of address size"),
  9969.                      input_bfd, o);
  9970.                   bfd_set_error (bfd_error_on_input);
  9971.                   return FALSE;
  9972.                 }
  9973.               o->flags |= SEC_ELF_REVERSE_COPY;
  9974.             }
  9975.  
  9976.           action_discarded = -1;
  9977.           if (!elf_section_ignore_discarded_relocs (o))
  9978.             action_discarded = (*bed->action_discarded) (o);
  9979.  
  9980.           /* Run through the relocs evaluating complex reloc symbols and
  9981.              looking for relocs against symbols from discarded sections
  9982.              or section symbols from removed link-once sections.
  9983.              Complain about relocs against discarded sections.  Zero
  9984.              relocs against removed link-once sections.  */
  9985.  
  9986.           rel = internal_relocs;
  9987.           relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
  9988.           for ( ; rel < relend; rel++)
  9989.             {
  9990.               unsigned long r_symndx = rel->r_info >> r_sym_shift;
  9991.               unsigned int s_type;
  9992.               asection **ps, *sec;
  9993.               struct elf_link_hash_entry *h = NULL;
  9994.               const char *sym_name;
  9995.  
  9996.               if (r_symndx == STN_UNDEF)
  9997.                 continue;
  9998.  
  9999.               if (r_symndx >= locsymcount
  10000.                   || (elf_bad_symtab (input_bfd)
  10001.                       && flinfo->sections[r_symndx] == NULL))
  10002.                 {
  10003.                   h = sym_hashes[r_symndx - extsymoff];
  10004.  
  10005.                   /* Badly formatted input files can contain relocs that
  10006.                      reference non-existant symbols.  Check here so that
  10007.                      we do not seg fault.  */
  10008.                   if (h == NULL)
  10009.                     {
  10010.                       char buffer [32];
  10011.  
  10012.                       sprintf_vma (buffer, rel->r_info);
  10013.                       (*_bfd_error_handler)
  10014.                         (_("error: %B contains a reloc (0x%s) for section %A "
  10015.                            "that references a non-existent global symbol"),
  10016.                          input_bfd, o, buffer);
  10017.                       bfd_set_error (bfd_error_bad_value);
  10018.                       return FALSE;
  10019.                     }
  10020.  
  10021.                   while (h->root.type == bfd_link_hash_indirect
  10022.                          || h->root.type == bfd_link_hash_warning)
  10023.                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
  10024.  
  10025.                   s_type = h->type;
  10026.  
  10027.                   /* If a plugin symbol is referenced from a non-IR file,
  10028.                      mark the symbol as undefined.  Note that the
  10029.                      linker may attach linker created dynamic sections
  10030.                      to the plugin bfd.  Symbols defined in linker
  10031.                      created sections are not plugin symbols.  */
  10032.                   if (h->root.non_ir_ref
  10033.                       && (h->root.type == bfd_link_hash_defined
  10034.                           || h->root.type == bfd_link_hash_defweak)
  10035.                       && (h->root.u.def.section->flags
  10036.                           & SEC_LINKER_CREATED) == 0
  10037.                       && h->root.u.def.section->owner != NULL
  10038.                       && (h->root.u.def.section->owner->flags
  10039.                           & BFD_PLUGIN) != 0)
  10040.                     {
  10041.                       h->root.type = bfd_link_hash_undefined;
  10042.                       h->root.u.undef.abfd = h->root.u.def.section->owner;
  10043.                     }
  10044.  
  10045.                   ps = NULL;
  10046.                   if (h->root.type == bfd_link_hash_defined
  10047.                       || h->root.type == bfd_link_hash_defweak)
  10048.                     ps = &h->root.u.def.section;
  10049.  
  10050.                   sym_name = h->root.root.string;
  10051.                 }
  10052.               else
  10053.                 {
  10054.                   Elf_Internal_Sym *sym = isymbuf + r_symndx;
  10055.  
  10056.                   s_type = ELF_ST_TYPE (sym->st_info);
  10057.                   ps = &flinfo->sections[r_symndx];
  10058.                   sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
  10059.                                                sym, *ps);
  10060.                 }
  10061.  
  10062.               if ((s_type == STT_RELC || s_type == STT_SRELC)
  10063.                   && !bfd_link_relocatable (flinfo->info))
  10064.                 {
  10065.                   bfd_vma val;
  10066.                   bfd_vma dot = (rel->r_offset
  10067.                                  + o->output_offset + o->output_section->vma);
  10068. #ifdef DEBUG
  10069.                   printf ("Encountered a complex symbol!");
  10070.                   printf (" (input_bfd %s, section %s, reloc %ld\n",
  10071.                           input_bfd->filename, o->name,
  10072.                           (long) (rel - internal_relocs));
  10073.                   printf (" symbol: idx  %8.8lx, name %s\n",
  10074.                           r_symndx, sym_name);
  10075.                   printf (" reloc : info %8.8lx, addr %8.8lx\n",
  10076.                           (unsigned long) rel->r_info,
  10077.                           (unsigned long) rel->r_offset);
  10078. #endif
  10079.                   if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
  10080.                                     isymbuf, locsymcount, s_type == STT_SRELC))
  10081.                     return FALSE;
  10082.  
  10083.                   /* Symbol evaluated OK.  Update to absolute value.  */
  10084.                   set_symbol_value (input_bfd, isymbuf, locsymcount,
  10085.                                     r_symndx, val);
  10086.                   continue;
  10087.                 }
  10088.  
  10089.               if (action_discarded != -1 && ps != NULL)
  10090.                 {
  10091.                   /* Complain if the definition comes from a
  10092.                      discarded section.  */
  10093.                   if ((sec = *ps) != NULL && discarded_section (sec))
  10094.                     {
  10095.                       BFD_ASSERT (r_symndx != STN_UNDEF);
  10096.                       if (action_discarded & COMPLAIN)
  10097.                         (*flinfo->info->callbacks->einfo)
  10098.                           (_("%X`%s' referenced in section `%A' of %B: "
  10099.                              "defined in discarded section `%A' of %B\n"),
  10100.                            sym_name, o, input_bfd, sec, sec->owner);
  10101.  
  10102.                       /* Try to do the best we can to support buggy old
  10103.                          versions of gcc.  Pretend that the symbol is
  10104.                          really defined in the kept linkonce section.
  10105.                          FIXME: This is quite broken.  Modifying the
  10106.                          symbol here means we will be changing all later
  10107.                          uses of the symbol, not just in this section.  */
  10108.                       if (action_discarded & PRETEND)
  10109.                         {
  10110.                           asection *kept;
  10111.  
  10112.                           kept = _bfd_elf_check_kept_section (sec,
  10113.                                                               flinfo->info);
  10114.                           if (kept != NULL)
  10115.                             {
  10116.                               *ps = kept;
  10117.                               continue;
  10118.                             }
  10119.                         }
  10120.                     }
  10121.                 }
  10122.             }
  10123.  
  10124.           /* Relocate the section by invoking a back end routine.
  10125.  
  10126.              The back end routine is responsible for adjusting the
  10127.              section contents as necessary, and (if using Rela relocs
  10128.              and generating a relocatable output file) adjusting the
  10129.              reloc addend as necessary.
  10130.  
  10131.              The back end routine does not have to worry about setting
  10132.              the reloc address or the reloc symbol index.
  10133.  
  10134.              The back end routine is given a pointer to the swapped in
  10135.              internal symbols, and can access the hash table entries
  10136.              for the external symbols via elf_sym_hashes (input_bfd).
  10137.  
  10138.              When generating relocatable output, the back end routine
  10139.              must handle STB_LOCAL/STT_SECTION symbols specially.  The
  10140.              output symbol is going to be a section symbol
  10141.              corresponding to the output section, which will require
  10142.              the addend to be adjusted.  */
  10143.  
  10144.           ret = (*relocate_section) (output_bfd, flinfo->info,
  10145.                                      input_bfd, o, contents,
  10146.                                      internal_relocs,
  10147.                                      isymbuf,
  10148.                                      flinfo->sections);
  10149.           if (!ret)
  10150.             return FALSE;
  10151.  
  10152.           if (ret == 2
  10153.               || bfd_link_relocatable (flinfo->info)
  10154.               || flinfo->info->emitrelocations)
  10155.             {
  10156.               Elf_Internal_Rela *irela;
  10157.               Elf_Internal_Rela *irelaend, *irelamid;
  10158.               bfd_vma last_offset;
  10159.               struct elf_link_hash_entry **rel_hash;
  10160.               struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
  10161.               Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
  10162.               unsigned int next_erel;
  10163.               bfd_boolean rela_normal;
  10164.               struct bfd_elf_section_data *esdi, *esdo;
  10165.  
  10166.               esdi = elf_section_data (o);
  10167.               esdo = elf_section_data (o->output_section);
  10168.               rela_normal = FALSE;
  10169.  
  10170.               /* Adjust the reloc addresses and symbol indices.  */
  10171.  
  10172.               irela = internal_relocs;
  10173.               irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
  10174.               rel_hash = esdo->rel.hashes + esdo->rel.count;
  10175.               /* We start processing the REL relocs, if any.  When we reach
  10176.                  IRELAMID in the loop, we switch to the RELA relocs.  */
  10177.               irelamid = irela;
  10178.               if (esdi->rel.hdr != NULL)
  10179.                 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
  10180.                              * bed->s->int_rels_per_ext_rel);
  10181.               rel_hash_list = rel_hash;
  10182.               rela_hash_list = NULL;
  10183.               last_offset = o->output_offset;
  10184.               if (!bfd_link_relocatable (flinfo->info))
  10185.                 last_offset += o->output_section->vma;
  10186.               for (next_erel = 0; irela < irelaend; irela++, next_erel++)
  10187.                 {
  10188.                   unsigned long r_symndx;
  10189.                   asection *sec;
  10190.                   Elf_Internal_Sym sym;
  10191.  
  10192.                   if (next_erel == bed->s->int_rels_per_ext_rel)
  10193.                     {
  10194.                       rel_hash++;
  10195.                       next_erel = 0;
  10196.                     }
  10197.  
  10198.                   if (irela == irelamid)
  10199.                     {
  10200.                       rel_hash = esdo->rela.hashes + esdo->rela.count;
  10201.                       rela_hash_list = rel_hash;
  10202.                       rela_normal = bed->rela_normal;
  10203.                     }
  10204.  
  10205.                   irela->r_offset = _bfd_elf_section_offset (output_bfd,
  10206.                                                              flinfo->info, o,
  10207.                                                              irela->r_offset);
  10208.                   if (irela->r_offset >= (bfd_vma) -2)
  10209.                     {
  10210.                       /* This is a reloc for a deleted entry or somesuch.
  10211.                          Turn it into an R_*_NONE reloc, at the same
  10212.                          offset as the last reloc.  elf_eh_frame.c and
  10213.                          bfd_elf_discard_info rely on reloc offsets
  10214.                          being ordered.  */
  10215.                       irela->r_offset = last_offset;
  10216.                       irela->r_info = 0;
  10217.                       irela->r_addend = 0;
  10218.                       continue;
  10219.                     }
  10220.  
  10221.                   irela->r_offset += o->output_offset;
  10222.  
  10223.                   /* Relocs in an executable have to be virtual addresses.  */
  10224.                   if (!bfd_link_relocatable (flinfo->info))
  10225.                     irela->r_offset += o->output_section->vma;
  10226.  
  10227.                   last_offset = irela->r_offset;
  10228.  
  10229.                   r_symndx = irela->r_info >> r_sym_shift;
  10230.                   if (r_symndx == STN_UNDEF)
  10231.                     continue;
  10232.  
  10233.                   if (r_symndx >= locsymcount
  10234.                       || (elf_bad_symtab (input_bfd)
  10235.                           && flinfo->sections[r_symndx] == NULL))
  10236.                     {
  10237.                       struct elf_link_hash_entry *rh;
  10238.                       unsigned long indx;
  10239.  
  10240.                       /* This is a reloc against a global symbol.  We
  10241.                          have not yet output all the local symbols, so
  10242.                          we do not know the symbol index of any global
  10243.                          symbol.  We set the rel_hash entry for this
  10244.                          reloc to point to the global hash table entry
  10245.                          for this symbol.  The symbol index is then
  10246.                          set at the end of bfd_elf_final_link.  */
  10247.                       indx = r_symndx - extsymoff;
  10248.                       rh = elf_sym_hashes (input_bfd)[indx];
  10249.                       while (rh->root.type == bfd_link_hash_indirect
  10250.                              || rh->root.type == bfd_link_hash_warning)
  10251.                         rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
  10252.  
  10253.                       /* Setting the index to -2 tells
  10254.                          elf_link_output_extsym that this symbol is
  10255.                          used by a reloc.  */
  10256.                       BFD_ASSERT (rh->indx < 0);
  10257.                       rh->indx = -2;
  10258.  
  10259.                       *rel_hash = rh;
  10260.  
  10261.                       continue;
  10262.                     }
  10263.  
  10264.                   /* This is a reloc against a local symbol.  */
  10265.  
  10266.                   *rel_hash = NULL;
  10267.                   sym = isymbuf[r_symndx];
  10268.                   sec = flinfo->sections[r_symndx];
  10269.                   if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
  10270.                     {
  10271.                       /* I suppose the backend ought to fill in the
  10272.                          section of any STT_SECTION symbol against a
  10273.                          processor specific section.  */
  10274.                       r_symndx = STN_UNDEF;
  10275.                       if (bfd_is_abs_section (sec))
  10276.                         ;
  10277.                       else if (sec == NULL || sec->owner == NULL)
  10278.                         {
  10279.                           bfd_set_error (bfd_error_bad_value);
  10280.                           return FALSE;
  10281.                         }
  10282.                       else
  10283.                         {
  10284.                           asection *osec = sec->output_section;
  10285.  
  10286.                           /* If we have discarded a section, the output
  10287.                              section will be the absolute section.  In
  10288.                              case of discarded SEC_MERGE sections, use
  10289.                              the kept section.  relocate_section should
  10290.                              have already handled discarded linkonce
  10291.                              sections.  */
  10292.                           if (bfd_is_abs_section (osec)
  10293.                               && sec->kept_section != NULL
  10294.                               && sec->kept_section->output_section != NULL)
  10295.                             {
  10296.                               osec = sec->kept_section->output_section;
  10297.                               irela->r_addend -= osec->vma;
  10298.                             }
  10299.  
  10300.                           if (!bfd_is_abs_section (osec))
  10301.                             {
  10302.                               r_symndx = osec->target_index;
  10303.                               if (r_symndx == STN_UNDEF)
  10304.                                 {
  10305.                                   irela->r_addend += osec->vma;
  10306.                                   osec = _bfd_nearby_section (output_bfd, osec,
  10307.                                                               osec->vma);
  10308.                                   irela->r_addend -= osec->vma;
  10309.                                   r_symndx = osec->target_index;
  10310.                                 }
  10311.                             }
  10312.                         }
  10313.  
  10314.                       /* Adjust the addend according to where the
  10315.                          section winds up in the output section.  */
  10316.                       if (rela_normal)
  10317.                         irela->r_addend += sec->output_offset;
  10318.                     }
  10319.                   else
  10320.                     {
  10321.                       if (flinfo->indices[r_symndx] == -1)
  10322.                         {
  10323.                           unsigned long shlink;
  10324.                           const char *name;
  10325.                           asection *osec;
  10326.                           long indx;
  10327.  
  10328.                           if (flinfo->info->strip == strip_all)
  10329.                             {
  10330.                               /* You can't do ld -r -s.  */
  10331.                               bfd_set_error (bfd_error_invalid_operation);
  10332.                               return FALSE;
  10333.                             }
  10334.  
  10335.                           /* This symbol was skipped earlier, but
  10336.                              since it is needed by a reloc, we
  10337.                              must output it now.  */
  10338.                           shlink = symtab_hdr->sh_link;
  10339.                           name = (bfd_elf_string_from_elf_section
  10340.                                   (input_bfd, shlink, sym.st_name));
  10341.                           if (name == NULL)
  10342.                             return FALSE;
  10343.  
  10344.                           osec = sec->output_section;
  10345.                           sym.st_shndx =
  10346.                             _bfd_elf_section_from_bfd_section (output_bfd,
  10347.                                                                osec);
  10348.                           if (sym.st_shndx == SHN_BAD)
  10349.                             return FALSE;
  10350.  
  10351.                           sym.st_value += sec->output_offset;
  10352.                           if (!bfd_link_relocatable (flinfo->info))
  10353.                             {
  10354.                               sym.st_value += osec->vma;
  10355.                               if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
  10356.                                 {
  10357.                                   /* STT_TLS symbols are relative to PT_TLS
  10358.                                      segment base.  */
  10359.                                   BFD_ASSERT (elf_hash_table (flinfo->info)
  10360.                                               ->tls_sec != NULL);
  10361.                                   sym.st_value -= (elf_hash_table (flinfo->info)
  10362.                                                    ->tls_sec->vma);
  10363.                                 }
  10364.                             }
  10365.  
  10366.                           indx = bfd_get_symcount (output_bfd);
  10367.                           ret = elf_link_output_symstrtab (flinfo, name,
  10368.                                                            &sym, sec,
  10369.                                                            NULL);
  10370.                           if (ret == 0)
  10371.                             return FALSE;
  10372.                           else if (ret == 1)
  10373.                             flinfo->indices[r_symndx] = indx;
  10374.                           else
  10375.                             abort ();
  10376.                         }
  10377.  
  10378.                       r_symndx = flinfo->indices[r_symndx];
  10379.                     }
  10380.  
  10381.                   irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
  10382.                                    | (irela->r_info & r_type_mask));
  10383.                 }
  10384.  
  10385.               /* Swap out the relocs.  */
  10386.               input_rel_hdr = esdi->rel.hdr;
  10387.               if (input_rel_hdr && input_rel_hdr->sh_size != 0)
  10388.                 {
  10389.                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
  10390.                                                      input_rel_hdr,
  10391.                                                      internal_relocs,
  10392.                                                      rel_hash_list))
  10393.                     return FALSE;
  10394.                   internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
  10395.                                       * bed->s->int_rels_per_ext_rel);
  10396.                   rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
  10397.                 }
  10398.  
  10399.               input_rela_hdr = esdi->rela.hdr;
  10400.               if (input_rela_hdr && input_rela_hdr->sh_size != 0)
  10401.                 {
  10402.                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
  10403.                                                      input_rela_hdr,
  10404.                                                      internal_relocs,
  10405.                                                      rela_hash_list))
  10406.                     return FALSE;
  10407.                 }
  10408.             }
  10409.         }
  10410.  
  10411.       /* Write out the modified section contents.  */
  10412.       if (bed->elf_backend_write_section
  10413.           && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
  10414.                                                 contents))
  10415.         {
  10416.           /* Section written out.  */
  10417.         }
  10418.       else switch (o->sec_info_type)
  10419.         {
  10420.         case SEC_INFO_TYPE_STABS:
  10421.           if (! (_bfd_write_section_stabs
  10422.                  (output_bfd,
  10423.                   &elf_hash_table (flinfo->info)->stab_info,
  10424.                   o, &elf_section_data (o)->sec_info, contents)))
  10425.             return FALSE;
  10426.           break;
  10427.         case SEC_INFO_TYPE_MERGE:
  10428.           if (! _bfd_write_merged_section (output_bfd, o,
  10429.                                            elf_section_data (o)->sec_info))
  10430.             return FALSE;
  10431.           break;
  10432.         case SEC_INFO_TYPE_EH_FRAME:
  10433.           {
  10434.             if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
  10435.                                                    o, contents))
  10436.               return FALSE;
  10437.           }
  10438.           break;
  10439.         case SEC_INFO_TYPE_EH_FRAME_ENTRY:
  10440.           {
  10441.             if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
  10442.                                                          flinfo->info,
  10443.                                                          o, contents))
  10444.               return FALSE;
  10445.           }
  10446.           break;
  10447.         default:
  10448.           {
  10449.             /* FIXME: octets_per_byte.  */
  10450.             if (! (o->flags & SEC_EXCLUDE))
  10451.               {
  10452.                 file_ptr offset = (file_ptr) o->output_offset;
  10453.                 bfd_size_type todo = o->size;
  10454.                 if ((o->flags & SEC_ELF_REVERSE_COPY))
  10455.                   {
  10456.                     /* Reverse-copy input section to output.  */
  10457.                     do
  10458.                       {
  10459.                         todo -= address_size;
  10460.                         if (! bfd_set_section_contents (output_bfd,
  10461.                                                         o->output_section,
  10462.                                                         contents + todo,
  10463.                                                         offset,
  10464.                                                         address_size))
  10465.                           return FALSE;
  10466.                         if (todo == 0)
  10467.                           break;
  10468.                         offset += address_size;
  10469.                       }
  10470.                     while (1);
  10471.                   }
  10472.                 else if (! bfd_set_section_contents (output_bfd,
  10473.                                                      o->output_section,
  10474.                                                      contents,
  10475.                                                      offset, todo))
  10476.                   return FALSE;
  10477.               }
  10478.           }
  10479.           break;
  10480.         }
  10481.     }
  10482.  
  10483.   return TRUE;
  10484. }
  10485.  
  10486. /* Generate a reloc when linking an ELF file.  This is a reloc
  10487.    requested by the linker, and does not come from any input file.  This
  10488.    is used to build constructor and destructor tables when linking
  10489.    with -Ur.  */
  10490.  
  10491. static bfd_boolean
  10492. elf_reloc_link_order (bfd *output_bfd,
  10493.                       struct bfd_link_info *info,
  10494.                       asection *output_section,
  10495.                       struct bfd_link_order *link_order)
  10496. {
  10497.   reloc_howto_type *howto;
  10498.   long indx;
  10499.   bfd_vma offset;
  10500.   bfd_vma addend;
  10501.   struct bfd_elf_section_reloc_data *reldata;
  10502.   struct elf_link_hash_entry **rel_hash_ptr;
  10503.   Elf_Internal_Shdr *rel_hdr;
  10504.   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  10505.   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
  10506.   bfd_byte *erel;
  10507.   unsigned int i;
  10508.   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
  10509.  
  10510.   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  10511.   if (howto == NULL)
  10512.     {
  10513.       bfd_set_error (bfd_error_bad_value);
  10514.       return FALSE;
  10515.     }
  10516.  
  10517.   addend = link_order->u.reloc.p->addend;
  10518.  
  10519.   if (esdo->rel.hdr)
  10520.     reldata = &esdo->rel;
  10521.   else if (esdo->rela.hdr)
  10522.     reldata = &esdo->rela;
  10523.   else
  10524.     {
  10525.       reldata = NULL;
  10526.       BFD_ASSERT (0);
  10527.     }
  10528.  
  10529.   /* Figure out the symbol index.  */
  10530.   rel_hash_ptr = reldata->hashes + reldata->count;
  10531.   if (link_order->type == bfd_section_reloc_link_order)
  10532.     {
  10533.       indx = link_order->u.reloc.p->u.section->target_index;
  10534.       BFD_ASSERT (indx != 0);
  10535.       *rel_hash_ptr = NULL;
  10536.     }
  10537.   else
  10538.     {
  10539.       struct elf_link_hash_entry *h;
  10540.  
  10541.       /* Treat a reloc against a defined symbol as though it were
  10542.          actually against the section.  */
  10543.       h = ((struct elf_link_hash_entry *)
  10544.            bfd_wrapped_link_hash_lookup (output_bfd, info,
  10545.                                          link_order->u.reloc.p->u.name,
  10546.                                          FALSE, FALSE, TRUE));
  10547.       if (h != NULL
  10548.           && (h->root.type == bfd_link_hash_defined
  10549.               || h->root.type == bfd_link_hash_defweak))
  10550.         {
  10551.           asection *section;
  10552.  
  10553.           section = h->root.u.def.section;
  10554.           indx = section->output_section->target_index;
  10555.           *rel_hash_ptr = NULL;
  10556.           /* It seems that we ought to add the symbol value to the
  10557.              addend here, but in practice it has already been added
  10558.              because it was passed to constructor_callback.  */
  10559.           addend += section->output_section->vma + section->output_offset;
  10560.         }
  10561.       else if (h != NULL)
  10562.         {
  10563.           /* Setting the index to -2 tells elf_link_output_extsym that
  10564.              this symbol is used by a reloc.  */
  10565.           h->indx = -2;
  10566.           *rel_hash_ptr = h;
  10567.           indx = 0;
  10568.         }
  10569.       else
  10570.         {
  10571.           if (! ((*info->callbacks->unattached_reloc)
  10572.                  (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
  10573.             return FALSE;
  10574.           indx = 0;
  10575.         }
  10576.     }
  10577.  
  10578.   /* If this is an inplace reloc, we must write the addend into the
  10579.      object file.  */
  10580.   if (howto->partial_inplace && addend != 0)
  10581.     {
  10582.       bfd_size_type size;
  10583.       bfd_reloc_status_type rstat;
  10584.       bfd_byte *buf;
  10585.       bfd_boolean ok;
  10586.       const char *sym_name;
  10587.  
  10588.       size = (bfd_size_type) bfd_get_reloc_size (howto);
  10589.       buf = (bfd_byte *) bfd_zmalloc (size);
  10590.       if (buf == NULL && size != 0)
  10591.         return FALSE;
  10592.       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
  10593.       switch (rstat)
  10594.         {
  10595.         case bfd_reloc_ok:
  10596.           break;
  10597.  
  10598.         default:
  10599.         case bfd_reloc_outofrange:
  10600.           abort ();
  10601.  
  10602.         case bfd_reloc_overflow:
  10603.           if (link_order->type == bfd_section_reloc_link_order)
  10604.             sym_name = bfd_section_name (output_bfd,
  10605.                                          link_order->u.reloc.p->u.section);
  10606.           else
  10607.             sym_name = link_order->u.reloc.p->u.name;
  10608.           if (! ((*info->callbacks->reloc_overflow)
  10609.                  (info, NULL, sym_name, howto->name, addend, NULL,
  10610.                   NULL, (bfd_vma) 0)))
  10611.             {
  10612.               free (buf);
  10613.               return FALSE;
  10614.             }
  10615.           break;
  10616.         }
  10617.       ok = bfd_set_section_contents (output_bfd, output_section, buf,
  10618.                                      link_order->offset, size);
  10619.       free (buf);
  10620.       if (! ok)
  10621.         return FALSE;
  10622.     }
  10623.  
  10624.   /* The address of a reloc is relative to the section in a
  10625.      relocatable file, and is a virtual address in an executable
  10626.      file.  */
  10627.   offset = link_order->offset;
  10628.   if (! bfd_link_relocatable (info))
  10629.     offset += output_section->vma;
  10630.  
  10631.   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
  10632.     {
  10633.       irel[i].r_offset = offset;
  10634.       irel[i].r_info = 0;
  10635.       irel[i].r_addend = 0;
  10636.     }
  10637.   if (bed->s->arch_size == 32)
  10638.     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
  10639.   else
  10640.     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
  10641.  
  10642.   rel_hdr = reldata->hdr;
  10643.   erel = rel_hdr->contents;
  10644.   if (rel_hdr->sh_type == SHT_REL)
  10645.     {
  10646.       erel += reldata->count * bed->s->sizeof_rel;
  10647.       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
  10648.     }
  10649.   else
  10650.     {
  10651.       irel[0].r_addend = addend;
  10652.       erel += reldata->count * bed->s->sizeof_rela;
  10653.       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
  10654.     }
  10655.  
  10656.   ++reldata->count;
  10657.  
  10658.   return TRUE;
  10659. }
  10660.  
  10661.  
  10662. /* Get the output vma of the section pointed to by the sh_link field.  */
  10663.  
  10664. static bfd_vma
  10665. elf_get_linked_section_vma (struct bfd_link_order *p)
  10666. {
  10667.   Elf_Internal_Shdr **elf_shdrp;
  10668.   asection *s;
  10669.   int elfsec;
  10670.  
  10671.   s = p->u.indirect.section;
  10672.   elf_shdrp = elf_elfsections (s->owner);
  10673.   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
  10674.   elfsec = elf_shdrp[elfsec]->sh_link;
  10675.   /* PR 290:
  10676.      The Intel C compiler generates SHT_IA_64_UNWIND with
  10677.      SHF_LINK_ORDER.  But it doesn't set the sh_link or
  10678.      sh_info fields.  Hence we could get the situation
  10679.      where elfsec is 0.  */
  10680.   if (elfsec == 0)
  10681.     {
  10682.       const struct elf_backend_data *bed
  10683.         = get_elf_backend_data (s->owner);
  10684.       if (bed->link_order_error_handler)
  10685.         bed->link_order_error_handler
  10686.           (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
  10687.       return 0;
  10688.     }
  10689.   else
  10690.     {
  10691.       s = elf_shdrp[elfsec]->bfd_section;
  10692.       return s->output_section->vma + s->output_offset;
  10693.     }
  10694. }
  10695.  
  10696.  
  10697. /* Compare two sections based on the locations of the sections they are
  10698.    linked to.  Used by elf_fixup_link_order.  */
  10699.  
  10700. static int
  10701. compare_link_order (const void * a, const void * b)
  10702. {
  10703.   bfd_vma apos;
  10704.   bfd_vma bpos;
  10705.  
  10706.   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
  10707.   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
  10708.   if (apos < bpos)
  10709.     return -1;
  10710.   return apos > bpos;
  10711. }
  10712.  
  10713.  
  10714. /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
  10715.    order as their linked sections.  Returns false if this could not be done
  10716.    because an output section includes both ordered and unordered
  10717.    sections.  Ideally we'd do this in the linker proper.  */
  10718.  
  10719. static bfd_boolean
  10720. elf_fixup_link_order (bfd *abfd, asection *o)
  10721. {
  10722.   int seen_linkorder;
  10723.   int seen_other;
  10724.   int n;
  10725.   struct bfd_link_order *p;
  10726.   bfd *sub;
  10727.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10728.   unsigned elfsec;
  10729.   struct bfd_link_order **sections;
  10730.   asection *s, *other_sec, *linkorder_sec;
  10731.   bfd_vma offset;
  10732.  
  10733.   other_sec = NULL;
  10734.   linkorder_sec = NULL;
  10735.   seen_other = 0;
  10736.   seen_linkorder = 0;
  10737.   for (p = o->map_head.link_order; p != NULL; p = p->next)
  10738.     {
  10739.       if (p->type == bfd_indirect_link_order)
  10740.         {
  10741.           s = p->u.indirect.section;
  10742.           sub = s->owner;
  10743.           if (bfd_get_flavour (sub) == bfd_target_elf_flavour
  10744.               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
  10745.               && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
  10746.               && elfsec < elf_numsections (sub)
  10747.               && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
  10748.               && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
  10749.             {
  10750.               seen_linkorder++;
  10751.               linkorder_sec = s;
  10752.             }
  10753.           else
  10754.             {
  10755.               seen_other++;
  10756.               other_sec = s;
  10757.             }
  10758.         }
  10759.       else
  10760.         seen_other++;
  10761.  
  10762.       if (seen_other && seen_linkorder)
  10763.         {
  10764.           if (other_sec && linkorder_sec)
  10765.             (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
  10766.                                    o, linkorder_sec,
  10767.                                    linkorder_sec->owner, other_sec,
  10768.                                    other_sec->owner);
  10769.           else
  10770.             (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
  10771.                                    o);
  10772.           bfd_set_error (bfd_error_bad_value);
  10773.           return FALSE;
  10774.         }
  10775.     }
  10776.  
  10777.   if (!seen_linkorder)
  10778.     return TRUE;
  10779.  
  10780.   sections = (struct bfd_link_order **)
  10781.     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
  10782.   if (sections == NULL)
  10783.     return FALSE;
  10784.   seen_linkorder = 0;
  10785.  
  10786.   for (p = o->map_head.link_order; p != NULL; p = p->next)
  10787.     {
  10788.       sections[seen_linkorder++] = p;
  10789.     }
  10790.   /* Sort the input sections in the order of their linked section.  */
  10791.   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
  10792.          compare_link_order);
  10793.  
  10794.   /* Change the offsets of the sections.  */
  10795.   offset = 0;
  10796.   for (n = 0; n < seen_linkorder; n++)
  10797.     {
  10798.       s = sections[n]->u.indirect.section;
  10799.       offset &= ~(bfd_vma) 0 << s->alignment_power;
  10800.       s->output_offset = offset;
  10801.       sections[n]->offset = offset;
  10802.       /* FIXME: octets_per_byte.  */
  10803.       offset += sections[n]->size;
  10804.     }
  10805.  
  10806.   free (sections);
  10807.   return TRUE;
  10808. }
  10809.  
  10810. static void
  10811. elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
  10812. {
  10813.   asection *o;
  10814.  
  10815.   if (flinfo->symstrtab != NULL)
  10816.     _bfd_elf_strtab_free (flinfo->symstrtab);
  10817.   if (flinfo->contents != NULL)
  10818.     free (flinfo->contents);
  10819.   if (flinfo->external_relocs != NULL)
  10820.     free (flinfo->external_relocs);
  10821.   if (flinfo->internal_relocs != NULL)
  10822.     free (flinfo->internal_relocs);
  10823.   if (flinfo->external_syms != NULL)
  10824.     free (flinfo->external_syms);
  10825.   if (flinfo->locsym_shndx != NULL)
  10826.     free (flinfo->locsym_shndx);
  10827.   if (flinfo->internal_syms != NULL)
  10828.     free (flinfo->internal_syms);
  10829.   if (flinfo->indices != NULL)
  10830.     free (flinfo->indices);
  10831.   if (flinfo->sections != NULL)
  10832.     free (flinfo->sections);
  10833.   if (flinfo->symshndxbuf != NULL)
  10834.     free (flinfo->symshndxbuf);
  10835.   for (o = obfd->sections; o != NULL; o = o->next)
  10836.     {
  10837.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  10838.       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
  10839.         free (esdo->rel.hashes);
  10840.       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
  10841.         free (esdo->rela.hashes);
  10842.     }
  10843. }
  10844.  
  10845. /* Do the final step of an ELF link.  */
  10846.  
  10847. bfd_boolean
  10848. bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
  10849. {
  10850.   bfd_boolean dynamic;
  10851.   bfd_boolean emit_relocs;
  10852.   bfd *dynobj;
  10853.   struct elf_final_link_info flinfo;
  10854.   asection *o;
  10855.   struct bfd_link_order *p;
  10856.   bfd *sub;
  10857.   bfd_size_type max_contents_size;
  10858.   bfd_size_type max_external_reloc_size;
  10859.   bfd_size_type max_internal_reloc_count;
  10860.   bfd_size_type max_sym_count;
  10861.   bfd_size_type max_sym_shndx_count;
  10862.   Elf_Internal_Sym elfsym;
  10863.   unsigned int i;
  10864.   Elf_Internal_Shdr *symtab_hdr;
  10865.   Elf_Internal_Shdr *symtab_shndx_hdr;
  10866.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10867.   struct elf_outext_info eoinfo;
  10868.   bfd_boolean merged;
  10869.   size_t relativecount = 0;
  10870.   asection *reldyn = 0;
  10871.   bfd_size_type amt;
  10872.   asection *attr_section = NULL;
  10873.   bfd_vma attr_size = 0;
  10874.   const char *std_attrs_section;
  10875.  
  10876.   if (! is_elf_hash_table (info->hash))
  10877.     return FALSE;
  10878.  
  10879.   if (bfd_link_pic (info))
  10880.     abfd->flags |= DYNAMIC;
  10881.  
  10882.   dynamic = elf_hash_table (info)->dynamic_sections_created;
  10883.   dynobj = elf_hash_table (info)->dynobj;
  10884.  
  10885.   emit_relocs = (bfd_link_relocatable (info)
  10886.                  || info->emitrelocations);
  10887.  
  10888.   flinfo.info = info;
  10889.   flinfo.output_bfd = abfd;
  10890.   flinfo.symstrtab = _bfd_elf_strtab_init ();
  10891.   if (flinfo.symstrtab == NULL)
  10892.     return FALSE;
  10893.  
  10894.   if (! dynamic)
  10895.     {
  10896.       flinfo.hash_sec = NULL;
  10897.       flinfo.symver_sec = NULL;
  10898.     }
  10899.   else
  10900.     {
  10901.       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
  10902.       /* Note that dynsym_sec can be NULL (on VMS).  */
  10903.       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
  10904.       /* Note that it is OK if symver_sec is NULL.  */
  10905.     }
  10906.  
  10907.   flinfo.contents = NULL;
  10908.   flinfo.external_relocs = NULL;
  10909.   flinfo.internal_relocs = NULL;
  10910.   flinfo.external_syms = NULL;
  10911.   flinfo.locsym_shndx = NULL;
  10912.   flinfo.internal_syms = NULL;
  10913.   flinfo.indices = NULL;
  10914.   flinfo.sections = NULL;
  10915.   flinfo.symshndxbuf = NULL;
  10916.   flinfo.filesym_count = 0;
  10917.  
  10918.   /* The object attributes have been merged.  Remove the input
  10919.      sections from the link, and set the contents of the output
  10920.      secton.  */
  10921.   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
  10922.   for (o = abfd->sections; o != NULL; o = o->next)
  10923.     {
  10924.       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
  10925.           || strcmp (o->name, ".gnu.attributes") == 0)
  10926.         {
  10927.           for (p = o->map_head.link_order; p != NULL; p = p->next)
  10928.             {
  10929.               asection *input_section;
  10930.  
  10931.               if (p->type != bfd_indirect_link_order)
  10932.                 continue;
  10933.               input_section = p->u.indirect.section;
  10934.               /* Hack: reset the SEC_HAS_CONTENTS flag so that
  10935.                  elf_link_input_bfd ignores this section.  */
  10936.               input_section->flags &= ~SEC_HAS_CONTENTS;
  10937.             }
  10938.  
  10939.           attr_size = bfd_elf_obj_attr_size (abfd);
  10940.           if (attr_size)
  10941.             {
  10942.               bfd_set_section_size (abfd, o, attr_size);
  10943.               attr_section = o;
  10944.               /* Skip this section later on.  */
  10945.               o->map_head.link_order = NULL;
  10946.             }
  10947.           else
  10948.             o->flags |= SEC_EXCLUDE;
  10949.         }
  10950.     }
  10951.  
  10952.   /* Count up the number of relocations we will output for each output
  10953.      section, so that we know the sizes of the reloc sections.  We
  10954.      also figure out some maximum sizes.  */
  10955.   max_contents_size = 0;
  10956.   max_external_reloc_size = 0;
  10957.   max_internal_reloc_count = 0;
  10958.   max_sym_count = 0;
  10959.   max_sym_shndx_count = 0;
  10960.   merged = FALSE;
  10961.   for (o = abfd->sections; o != NULL; o = o->next)
  10962.     {
  10963.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  10964.       o->reloc_count = 0;
  10965.  
  10966.       for (p = o->map_head.link_order; p != NULL; p = p->next)
  10967.         {
  10968.           unsigned int reloc_count = 0;
  10969.           struct bfd_elf_section_data *esdi = NULL;
  10970.  
  10971.           if (p->type == bfd_section_reloc_link_order
  10972.               || p->type == bfd_symbol_reloc_link_order)
  10973.             reloc_count = 1;
  10974.           else if (p->type == bfd_indirect_link_order)
  10975.             {
  10976.               asection *sec;
  10977.  
  10978.               sec = p->u.indirect.section;
  10979.               esdi = elf_section_data (sec);
  10980.  
  10981.               /* Mark all sections which are to be included in the
  10982.                  link.  This will normally be every section.  We need
  10983.                  to do this so that we can identify any sections which
  10984.                  the linker has decided to not include.  */
  10985.               sec->linker_mark = TRUE;
  10986.  
  10987.               if (sec->flags & SEC_MERGE)
  10988.                 merged = TRUE;
  10989.  
  10990.               if (esdo->this_hdr.sh_type == SHT_REL
  10991.                   || esdo->this_hdr.sh_type == SHT_RELA)
  10992.                 /* Some backends use reloc_count in relocation sections
  10993.                    to count particular types of relocs.  Of course,
  10994.                    reloc sections themselves can't have relocations.  */
  10995.                 reloc_count = 0;
  10996.               else if (emit_relocs)
  10997.                 reloc_count = sec->reloc_count;
  10998.               else if (bed->elf_backend_count_relocs)
  10999.                 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
  11000.  
  11001.               if (sec->rawsize > max_contents_size)
  11002.                 max_contents_size = sec->rawsize;
  11003.               if (sec->size > max_contents_size)
  11004.                 max_contents_size = sec->size;
  11005.  
  11006.               /* We are interested in just local symbols, not all
  11007.                  symbols.  */
  11008.               if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
  11009.                   && (sec->owner->flags & DYNAMIC) == 0)
  11010.                 {
  11011.                   size_t sym_count;
  11012.  
  11013.                   if (elf_bad_symtab (sec->owner))
  11014.                     sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
  11015.                                  / bed->s->sizeof_sym);
  11016.                   else
  11017.                     sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
  11018.  
  11019.                   if (sym_count > max_sym_count)
  11020.                     max_sym_count = sym_count;
  11021.  
  11022.                   if (sym_count > max_sym_shndx_count
  11023.                       && elf_symtab_shndx_list (sec->owner) != NULL)
  11024.                     max_sym_shndx_count = sym_count;
  11025.  
  11026.                   if ((sec->flags & SEC_RELOC) != 0)
  11027.                     {
  11028.                       size_t ext_size = 0;
  11029.  
  11030.                       if (esdi->rel.hdr != NULL)
  11031.                         ext_size = esdi->rel.hdr->sh_size;
  11032.                       if (esdi->rela.hdr != NULL)
  11033.                         ext_size += esdi->rela.hdr->sh_size;
  11034.  
  11035.                       if (ext_size > max_external_reloc_size)
  11036.                         max_external_reloc_size = ext_size;
  11037.                       if (sec->reloc_count > max_internal_reloc_count)
  11038.                         max_internal_reloc_count = sec->reloc_count;
  11039.                     }
  11040.                 }
  11041.             }
  11042.  
  11043.           if (reloc_count == 0)
  11044.             continue;
  11045.  
  11046.           o->reloc_count += reloc_count;
  11047.  
  11048.           if (p->type == bfd_indirect_link_order && emit_relocs)
  11049.             {
  11050.               if (esdi->rel.hdr)
  11051.                 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
  11052.               if (esdi->rela.hdr)
  11053.                 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
  11054.             }
  11055.           else
  11056.             {
  11057.               if (o->use_rela_p)
  11058.                 esdo->rela.count += reloc_count;
  11059.               else
  11060.                 esdo->rel.count += reloc_count;
  11061.             }
  11062.         }
  11063.  
  11064.       if (o->reloc_count > 0)
  11065.         o->flags |= SEC_RELOC;
  11066.       else
  11067.         {
  11068.           /* Explicitly clear the SEC_RELOC flag.  The linker tends to
  11069.              set it (this is probably a bug) and if it is set
  11070.              assign_section_numbers will create a reloc section.  */
  11071.           o->flags &=~ SEC_RELOC;
  11072.         }
  11073.  
  11074.       /* If the SEC_ALLOC flag is not set, force the section VMA to
  11075.          zero.  This is done in elf_fake_sections as well, but forcing
  11076.          the VMA to 0 here will ensure that relocs against these
  11077.          sections are handled correctly.  */
  11078.       if ((o->flags & SEC_ALLOC) == 0
  11079.           && ! o->user_set_vma)
  11080.         o->vma = 0;
  11081.     }
  11082.  
  11083.   if (! bfd_link_relocatable (info) && merged)
  11084.     elf_link_hash_traverse (elf_hash_table (info),
  11085.                             _bfd_elf_link_sec_merge_syms, abfd);
  11086.  
  11087.   /* Figure out the file positions for everything but the symbol table
  11088.      and the relocs.  We set symcount to force assign_section_numbers
  11089.      to create a symbol table.  */
  11090.   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
  11091.   BFD_ASSERT (! abfd->output_has_begun);
  11092.   if (! _bfd_elf_compute_section_file_positions (abfd, info))
  11093.     goto error_return;
  11094.  
  11095.   /* Set sizes, and assign file positions for reloc sections.  */
  11096.   for (o = abfd->sections; o != NULL; o = o->next)
  11097.     {
  11098.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  11099.       if ((o->flags & SEC_RELOC) != 0)
  11100.         {
  11101.           if (esdo->rel.hdr
  11102.               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
  11103.             goto error_return;
  11104.  
  11105.           if (esdo->rela.hdr
  11106.               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
  11107.             goto error_return;
  11108.         }
  11109.  
  11110.       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
  11111.          to count upwards while actually outputting the relocations.  */
  11112.       esdo->rel.count = 0;
  11113.       esdo->rela.count = 0;
  11114.  
  11115.       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
  11116.         {
  11117.           /* Cache the section contents so that they can be compressed
  11118.              later.  Use bfd_malloc since it will be freed by
  11119.              bfd_compress_section_contents.  */
  11120.           unsigned char *contents = esdo->this_hdr.contents;
  11121.           if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
  11122.             abort ();
  11123.           contents
  11124.             = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
  11125.           if (contents == NULL)
  11126.             goto error_return;
  11127.           esdo->this_hdr.contents = contents;
  11128.         }
  11129.     }
  11130.  
  11131.   /* We have now assigned file positions for all the sections except
  11132.      .symtab, .strtab, and non-loaded reloc sections.  We start the
  11133.      .symtab section at the current file position, and write directly
  11134.      to it.  We build the .strtab section in memory.  */
  11135.   bfd_get_symcount (abfd) = 0;
  11136.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11137.   /* sh_name is set in prep_headers.  */
  11138.   symtab_hdr->sh_type = SHT_SYMTAB;
  11139.   /* sh_flags, sh_addr and sh_size all start off zero.  */
  11140.   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
  11141.   /* sh_link is set in assign_section_numbers.  */
  11142.   /* sh_info is set below.  */
  11143.   /* sh_offset is set just below.  */
  11144.   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
  11145.  
  11146.   if (max_sym_count < 20)
  11147.     max_sym_count = 20;
  11148.   elf_hash_table (info)->strtabsize = max_sym_count;
  11149.   amt = max_sym_count * sizeof (struct elf_sym_strtab);
  11150.   elf_hash_table (info)->strtab
  11151.     = (struct elf_sym_strtab *) bfd_malloc (amt);
  11152.   if (elf_hash_table (info)->strtab == NULL)
  11153.     goto error_return;
  11154.   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
  11155.   flinfo.symshndxbuf
  11156.     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
  11157.        ? (Elf_External_Sym_Shndx *) -1 : NULL);
  11158.  
  11159.   if (info->strip != strip_all || emit_relocs)
  11160.     {
  11161.       file_ptr off = elf_next_file_pos (abfd);
  11162.  
  11163.       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
  11164.  
  11165.       /* Note that at this point elf_next_file_pos (abfd) is
  11166.          incorrect.  We do not yet know the size of the .symtab section.
  11167.          We correct next_file_pos below, after we do know the size.  */
  11168.  
  11169.       /* Start writing out the symbol table.  The first symbol is always a
  11170.          dummy symbol.  */
  11171.       elfsym.st_value = 0;
  11172.       elfsym.st_size = 0;
  11173.       elfsym.st_info = 0;
  11174.       elfsym.st_other = 0;
  11175.       elfsym.st_shndx = SHN_UNDEF;
  11176.       elfsym.st_target_internal = 0;
  11177.       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
  11178.                                      bfd_und_section_ptr, NULL) != 1)
  11179.         goto error_return;
  11180.  
  11181.       /* Output a symbol for each section.  We output these even if we are
  11182.          discarding local symbols, since they are used for relocs.  These
  11183.          symbols have no names.  We store the index of each one in the
  11184.          index field of the section, so that we can find it again when
  11185.          outputting relocs.  */
  11186.  
  11187.       elfsym.st_size = 0;
  11188.       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  11189.       elfsym.st_other = 0;
  11190.       elfsym.st_value = 0;
  11191.       elfsym.st_target_internal = 0;
  11192.       for (i = 1; i < elf_numsections (abfd); i++)
  11193.         {
  11194.           o = bfd_section_from_elf_index (abfd, i);
  11195.           if (o != NULL)
  11196.             {
  11197.               o->target_index = bfd_get_symcount (abfd);
  11198.               elfsym.st_shndx = i;
  11199.               if (!bfd_link_relocatable (info))
  11200.                 elfsym.st_value = o->vma;
  11201.               if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
  11202.                                              NULL) != 1)
  11203.                 goto error_return;
  11204.             }
  11205.         }
  11206.     }
  11207.  
  11208.   /* Allocate some memory to hold information read in from the input
  11209.      files.  */
  11210.   if (max_contents_size != 0)
  11211.     {
  11212.       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  11213.       if (flinfo.contents == NULL)
  11214.         goto error_return;
  11215.     }
  11216.  
  11217.   if (max_external_reloc_size != 0)
  11218.     {
  11219.       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
  11220.       if (flinfo.external_relocs == NULL)
  11221.         goto error_return;
  11222.     }
  11223.  
  11224.   if (max_internal_reloc_count != 0)
  11225.     {
  11226.       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
  11227.       amt *= sizeof (Elf_Internal_Rela);
  11228.       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
  11229.       if (flinfo.internal_relocs == NULL)
  11230.         goto error_return;
  11231.     }
  11232.  
  11233.   if (max_sym_count != 0)
  11234.     {
  11235.       amt = max_sym_count * bed->s->sizeof_sym;
  11236.       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
  11237.       if (flinfo.external_syms == NULL)
  11238.         goto error_return;
  11239.  
  11240.       amt = max_sym_count * sizeof (Elf_Internal_Sym);
  11241.       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
  11242.       if (flinfo.internal_syms == NULL)
  11243.         goto error_return;
  11244.  
  11245.       amt = max_sym_count * sizeof (long);
  11246.       flinfo.indices = (long int *) bfd_malloc (amt);
  11247.       if (flinfo.indices == NULL)
  11248.         goto error_return;
  11249.  
  11250.       amt = max_sym_count * sizeof (asection *);
  11251.       flinfo.sections = (asection **) bfd_malloc (amt);
  11252.       if (flinfo.sections == NULL)
  11253.         goto error_return;
  11254.     }
  11255.  
  11256.   if (max_sym_shndx_count != 0)
  11257.     {
  11258.       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
  11259.       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
  11260.       if (flinfo.locsym_shndx == NULL)
  11261.         goto error_return;
  11262.     }
  11263.  
  11264.   if (elf_hash_table (info)->tls_sec)
  11265.     {
  11266.       bfd_vma base, end = 0;
  11267.       asection *sec;
  11268.  
  11269.       for (sec = elf_hash_table (info)->tls_sec;
  11270.            sec && (sec->flags & SEC_THREAD_LOCAL);
  11271.            sec = sec->next)
  11272.         {
  11273.           bfd_size_type size = sec->size;
  11274.  
  11275.           if (size == 0
  11276.               && (sec->flags & SEC_HAS_CONTENTS) == 0)
  11277.             {
  11278.               struct bfd_link_order *ord = sec->map_tail.link_order;
  11279.  
  11280.               if (ord != NULL)
  11281.                 size = ord->offset + ord->size;
  11282.             }
  11283.           end = sec->vma + size;
  11284.         }
  11285.       base = elf_hash_table (info)->tls_sec->vma;
  11286.       /* Only align end of TLS section if static TLS doesn't have special
  11287.          alignment requirements.  */
  11288.       if (bed->static_tls_alignment == 1)
  11289.         end = align_power (end,
  11290.                            elf_hash_table (info)->tls_sec->alignment_power);
  11291.       elf_hash_table (info)->tls_size = end - base;
  11292.     }
  11293.  
  11294.   /* Reorder SHF_LINK_ORDER sections.  */
  11295.   for (o = abfd->sections; o != NULL; o = o->next)
  11296.     {
  11297.       if (!elf_fixup_link_order (abfd, o))
  11298.         return FALSE;
  11299.     }
  11300.  
  11301.   if (!_bfd_elf_fixup_eh_frame_hdr (info))
  11302.     return FALSE;
  11303.  
  11304.   /* Since ELF permits relocations to be against local symbols, we
  11305.      must have the local symbols available when we do the relocations.
  11306.      Since we would rather only read the local symbols once, and we
  11307.      would rather not keep them in memory, we handle all the
  11308.      relocations for a single input file at the same time.
  11309.  
  11310.      Unfortunately, there is no way to know the total number of local
  11311.      symbols until we have seen all of them, and the local symbol
  11312.      indices precede the global symbol indices.  This means that when
  11313.      we are generating relocatable output, and we see a reloc against
  11314.      a global symbol, we can not know the symbol index until we have
  11315.      finished examining all the local symbols to see which ones we are
  11316.      going to output.  To deal with this, we keep the relocations in
  11317.      memory, and don't output them until the end of the link.  This is
  11318.      an unfortunate waste of memory, but I don't see a good way around
  11319.      it.  Fortunately, it only happens when performing a relocatable
  11320.      link, which is not the common case.  FIXME: If keep_memory is set
  11321.      we could write the relocs out and then read them again; I don't
  11322.      know how bad the memory loss will be.  */
  11323.  
  11324.   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  11325.     sub->output_has_begun = FALSE;
  11326.   for (o = abfd->sections; o != NULL; o = o->next)
  11327.     {
  11328.       for (p = o->map_head.link_order; p != NULL; p = p->next)
  11329.         {
  11330.           if (p->type == bfd_indirect_link_order
  11331.               && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
  11332.                   == bfd_target_elf_flavour)
  11333.               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
  11334.             {
  11335.               if (! sub->output_has_begun)
  11336.                 {
  11337.                   if (! elf_link_input_bfd (&flinfo, sub))
  11338.                     goto error_return;
  11339.                   sub->output_has_begun = TRUE;
  11340.                 }
  11341.             }
  11342.           else if (p->type == bfd_section_reloc_link_order
  11343.                    || p->type == bfd_symbol_reloc_link_order)
  11344.             {
  11345.               if (! elf_reloc_link_order (abfd, info, o, p))
  11346.                 goto error_return;
  11347.             }
  11348.           else
  11349.             {
  11350.               if (! _bfd_default_link_order (abfd, info, o, p))
  11351.                 {
  11352.                   if (p->type == bfd_indirect_link_order
  11353.                       && (bfd_get_flavour (sub)
  11354.                           == bfd_target_elf_flavour)
  11355.                       && (elf_elfheader (sub)->e_ident[EI_CLASS]
  11356.                           != bed->s->elfclass))
  11357.                     {
  11358.                       const char *iclass, *oclass;
  11359.  
  11360.                       if (bed->s->elfclass == ELFCLASS64)
  11361.                         {
  11362.                           iclass = "ELFCLASS32";
  11363.                           oclass = "ELFCLASS64";
  11364.                         }
  11365.                       else
  11366.                         {
  11367.                           iclass = "ELFCLASS64";
  11368.                           oclass = "ELFCLASS32";
  11369.                         }
  11370.  
  11371.                       bfd_set_error (bfd_error_wrong_format);
  11372.                       (*_bfd_error_handler)
  11373.                         (_("%B: file class %s incompatible with %s"),
  11374.                          sub, iclass, oclass);
  11375.                     }
  11376.  
  11377.                   goto error_return;
  11378.                 }
  11379.             }
  11380.         }
  11381.     }
  11382.  
  11383.   /* Free symbol buffer if needed.  */
  11384.   if (!info->reduce_memory_overheads)
  11385.     {
  11386.       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  11387.         if (bfd_get_flavour (sub) == bfd_target_elf_flavour
  11388.             && elf_tdata (sub)->symbuf)
  11389.           {
  11390.             free (elf_tdata (sub)->symbuf);
  11391.             elf_tdata (sub)->symbuf = NULL;
  11392.           }
  11393.     }
  11394.  
  11395.   /* Output any global symbols that got converted to local in a
  11396.      version script or due to symbol visibility.  We do this in a
  11397.      separate step since ELF requires all local symbols to appear
  11398.      prior to any global symbols.  FIXME: We should only do this if
  11399.      some global symbols were, in fact, converted to become local.
  11400.      FIXME: Will this work correctly with the Irix 5 linker?  */
  11401.   eoinfo.failed = FALSE;
  11402.   eoinfo.flinfo = &flinfo;
  11403.   eoinfo.localsyms = TRUE;
  11404.   eoinfo.file_sym_done = FALSE;
  11405.   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  11406.   if (eoinfo.failed)
  11407.     return FALSE;
  11408.  
  11409.   /* If backend needs to output some local symbols not present in the hash
  11410.      table, do it now.  */
  11411.   if (bed->elf_backend_output_arch_local_syms
  11412.       && (info->strip != strip_all || emit_relocs))
  11413.     {
  11414.       typedef int (*out_sym_func)
  11415.         (void *, const char *, Elf_Internal_Sym *, asection *,
  11416.          struct elf_link_hash_entry *);
  11417.  
  11418.       if (! ((*bed->elf_backend_output_arch_local_syms)
  11419.              (abfd, info, &flinfo,
  11420.               (out_sym_func) elf_link_output_symstrtab)))
  11421.         return FALSE;
  11422.     }
  11423.  
  11424.   /* That wrote out all the local symbols.  Finish up the symbol table
  11425.      with the global symbols. Even if we want to strip everything we
  11426.      can, we still need to deal with those global symbols that got
  11427.      converted to local in a version script.  */
  11428.  
  11429.   /* The sh_info field records the index of the first non local symbol.  */
  11430.   symtab_hdr->sh_info = bfd_get_symcount (abfd);
  11431.  
  11432.   if (dynamic
  11433.       && elf_hash_table (info)->dynsym != NULL
  11434.       && (elf_hash_table (info)->dynsym->output_section
  11435.           != bfd_abs_section_ptr))
  11436.     {
  11437.       Elf_Internal_Sym sym;
  11438.       bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
  11439.       long last_local = 0;
  11440.  
  11441.       /* Write out the section symbols for the output sections.  */
  11442.       if (bfd_link_pic (info)
  11443.           || elf_hash_table (info)->is_relocatable_executable)
  11444.         {
  11445.           asection *s;
  11446.  
  11447.           sym.st_size = 0;
  11448.           sym.st_name = 0;
  11449.           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  11450.           sym.st_other = 0;
  11451.           sym.st_target_internal = 0;
  11452.  
  11453.           for (s = abfd->sections; s != NULL; s = s->next)
  11454.             {
  11455.               int indx;
  11456.               bfd_byte *dest;
  11457.               long dynindx;
  11458.  
  11459.               dynindx = elf_section_data (s)->dynindx;
  11460.               if (dynindx <= 0)
  11461.                 continue;
  11462.               indx = elf_section_data (s)->this_idx;
  11463.               BFD_ASSERT (indx > 0);
  11464.               sym.st_shndx = indx;
  11465.               if (! check_dynsym (abfd, &sym))
  11466.                 return FALSE;
  11467.               sym.st_value = s->vma;
  11468.               dest = dynsym + dynindx * bed->s->sizeof_sym;
  11469.               if (last_local < dynindx)
  11470.                 last_local = dynindx;
  11471.               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11472.             }
  11473.         }
  11474.  
  11475.       /* Write out the local dynsyms.  */
  11476.       if (elf_hash_table (info)->dynlocal)
  11477.         {
  11478.           struct elf_link_local_dynamic_entry *e;
  11479.           for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  11480.             {
  11481.               asection *s;
  11482.               bfd_byte *dest;
  11483.  
  11484.               /* Copy the internal symbol and turn off visibility.
  11485.                  Note that we saved a word of storage and overwrote
  11486.                  the original st_name with the dynstr_index.  */
  11487.               sym = e->isym;
  11488.               sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  11489.  
  11490.               s = bfd_section_from_elf_index (e->input_bfd,
  11491.                                               e->isym.st_shndx);
  11492.               if (s != NULL)
  11493.                 {
  11494.                   sym.st_shndx =
  11495.                     elf_section_data (s->output_section)->this_idx;
  11496.                   if (! check_dynsym (abfd, &sym))
  11497.                     return FALSE;
  11498.                   sym.st_value = (s->output_section->vma
  11499.                                   + s->output_offset
  11500.                                   + e->isym.st_value);
  11501.                 }
  11502.  
  11503.               if (last_local < e->dynindx)
  11504.                 last_local = e->dynindx;
  11505.  
  11506.               dest = dynsym + e->dynindx * bed->s->sizeof_sym;
  11507.               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11508.             }
  11509.         }
  11510.  
  11511.       elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
  11512.         last_local + 1;
  11513.     }
  11514.  
  11515.   /* We get the global symbols from the hash table.  */
  11516.   eoinfo.failed = FALSE;
  11517.   eoinfo.localsyms = FALSE;
  11518.   eoinfo.flinfo = &flinfo;
  11519.   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  11520.   if (eoinfo.failed)
  11521.     return FALSE;
  11522.  
  11523.   /* If backend needs to output some symbols not present in the hash
  11524.      table, do it now.  */
  11525.   if (bed->elf_backend_output_arch_syms
  11526.       && (info->strip != strip_all || emit_relocs))
  11527.     {
  11528.       typedef int (*out_sym_func)
  11529.         (void *, const char *, Elf_Internal_Sym *, asection *,
  11530.          struct elf_link_hash_entry *);
  11531.  
  11532.       if (! ((*bed->elf_backend_output_arch_syms)
  11533.              (abfd, info, &flinfo,
  11534.               (out_sym_func) elf_link_output_symstrtab)))
  11535.         return FALSE;
  11536.     }
  11537.  
  11538.   /* Finalize the .strtab section.  */
  11539.   _bfd_elf_strtab_finalize (flinfo.symstrtab);
  11540.  
  11541.   /* Swap out the .strtab section. */
  11542.   if (!elf_link_swap_symbols_out (&flinfo))
  11543.     return FALSE;
  11544.  
  11545.   /* Now we know the size of the symtab section.  */
  11546.   if (bfd_get_symcount (abfd) > 0)
  11547.     {
  11548.       /* Finish up and write out the symbol string table (.strtab)
  11549.          section.  */
  11550.       Elf_Internal_Shdr *symstrtab_hdr;
  11551.       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
  11552.  
  11553.       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  11554.       if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
  11555.         {
  11556.           symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
  11557.           symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
  11558.           symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
  11559.           amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
  11560.           symtab_shndx_hdr->sh_size = amt;
  11561.  
  11562.           off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
  11563.                                                            off, TRUE);
  11564.  
  11565.           if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
  11566.               || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
  11567.             return FALSE;
  11568.         }
  11569.  
  11570.       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  11571.       /* sh_name was set in prep_headers.  */
  11572.       symstrtab_hdr->sh_type = SHT_STRTAB;
  11573.       symstrtab_hdr->sh_flags = 0;
  11574.       symstrtab_hdr->sh_addr = 0;
  11575.       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
  11576.       symstrtab_hdr->sh_entsize = 0;
  11577.       symstrtab_hdr->sh_link = 0;
  11578.       symstrtab_hdr->sh_info = 0;
  11579.       /* sh_offset is set just below.  */
  11580.       symstrtab_hdr->sh_addralign = 1;
  11581.  
  11582.       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
  11583.                                                        off, TRUE);
  11584.       elf_next_file_pos (abfd) = off;
  11585.  
  11586.       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
  11587.           || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
  11588.         return FALSE;
  11589.     }
  11590.  
  11591.   /* Adjust the relocs to have the correct symbol indices.  */
  11592.   for (o = abfd->sections; o != NULL; o = o->next)
  11593.     {
  11594.       struct bfd_elf_section_data *esdo = elf_section_data (o);
  11595.       bfd_boolean sort;
  11596.       if ((o->flags & SEC_RELOC) == 0)
  11597.         continue;
  11598.  
  11599.       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
  11600.       if (esdo->rel.hdr != NULL
  11601.           && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
  11602.         return FALSE;
  11603.       if (esdo->rela.hdr != NULL
  11604.           && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
  11605.         return FALSE;
  11606.  
  11607.       /* Set the reloc_count field to 0 to prevent write_relocs from
  11608.          trying to swap the relocs out itself.  */
  11609.       o->reloc_count = 0;
  11610.     }
  11611.  
  11612.   if (dynamic && info->combreloc && dynobj != NULL)
  11613.     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
  11614.  
  11615.   /* If we are linking against a dynamic object, or generating a
  11616.      shared library, finish up the dynamic linking information.  */
  11617.   if (dynamic)
  11618.     {
  11619.       bfd_byte *dyncon, *dynconend;
  11620.  
  11621.       /* Fix up .dynamic entries.  */
  11622.       o = bfd_get_linker_section (dynobj, ".dynamic");
  11623.       BFD_ASSERT (o != NULL);
  11624.  
  11625.       dyncon = o->contents;
  11626.       dynconend = o->contents + o->size;
  11627.       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11628.         {
  11629.           Elf_Internal_Dyn dyn;
  11630.           const char *name;
  11631.           unsigned int type;
  11632.  
  11633.           bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11634.  
  11635.           switch (dyn.d_tag)
  11636.             {
  11637.             default:
  11638.               continue;
  11639.             case DT_NULL:
  11640.               if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
  11641.                 {
  11642.                   switch (elf_section_data (reldyn)->this_hdr.sh_type)
  11643.                     {
  11644.                     case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
  11645.                     case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
  11646.                     default: continue;
  11647.                     }
  11648.                   dyn.d_un.d_val = relativecount;
  11649.                   relativecount = 0;
  11650.                   break;
  11651.                 }
  11652.               continue;
  11653.  
  11654.             case DT_INIT:
  11655.               name = info->init_function;
  11656.               goto get_sym;
  11657.             case DT_FINI:
  11658.               name = info->fini_function;
  11659.             get_sym:
  11660.               {
  11661.                 struct elf_link_hash_entry *h;
  11662.  
  11663.                 h = elf_link_hash_lookup (elf_hash_table (info), name,
  11664.                                           FALSE, FALSE, TRUE);
  11665.                 if (h != NULL
  11666.                     && (h->root.type == bfd_link_hash_defined
  11667.                         || h->root.type == bfd_link_hash_defweak))
  11668.                   {
  11669.                     dyn.d_un.d_ptr = h->root.u.def.value;
  11670.                     o = h->root.u.def.section;
  11671.                     if (o->output_section != NULL)
  11672.                       dyn.d_un.d_ptr += (o->output_section->vma
  11673.                                          + o->output_offset);
  11674.                     else
  11675.                       {
  11676.                         /* The symbol is imported from another shared
  11677.                            library and does not apply to this one.  */
  11678.                         dyn.d_un.d_ptr = 0;
  11679.                       }
  11680.                     break;
  11681.                   }
  11682.               }
  11683.               continue;
  11684.  
  11685.             case DT_PREINIT_ARRAYSZ:
  11686.               name = ".preinit_array";
  11687.               goto get_size;
  11688.             case DT_INIT_ARRAYSZ:
  11689.               name = ".init_array";
  11690.               goto get_size;
  11691.             case DT_FINI_ARRAYSZ:
  11692.               name = ".fini_array";
  11693.             get_size:
  11694.               o = bfd_get_section_by_name (abfd, name);
  11695.               if (o == NULL)
  11696.                 {
  11697.                   (*_bfd_error_handler)
  11698.                     (_("%B: could not find output section %s"), abfd, name);
  11699.                   goto error_return;
  11700.                 }
  11701.               if (o->size == 0)
  11702.                 (*_bfd_error_handler)
  11703.                   (_("warning: %s section has zero size"), name);
  11704.               dyn.d_un.d_val = o->size;
  11705.               break;
  11706.  
  11707.             case DT_PREINIT_ARRAY:
  11708.               name = ".preinit_array";
  11709.               goto get_vma;
  11710.             case DT_INIT_ARRAY:
  11711.               name = ".init_array";
  11712.               goto get_vma;
  11713.             case DT_FINI_ARRAY:
  11714.               name = ".fini_array";
  11715.               goto get_vma;
  11716.  
  11717.             case DT_HASH:
  11718.               name = ".hash";
  11719.               goto get_vma;
  11720.             case DT_GNU_HASH:
  11721.               name = ".gnu.hash";
  11722.               goto get_vma;
  11723.             case DT_STRTAB:
  11724.               name = ".dynstr";
  11725.               goto get_vma;
  11726.             case DT_SYMTAB:
  11727.               name = ".dynsym";
  11728.               goto get_vma;
  11729.             case DT_VERDEF:
  11730.               name = ".gnu.version_d";
  11731.               goto get_vma;
  11732.             case DT_VERNEED:
  11733.               name = ".gnu.version_r";
  11734.               goto get_vma;
  11735.             case DT_VERSYM:
  11736.               name = ".gnu.version";
  11737.             get_vma:
  11738.               o = bfd_get_section_by_name (abfd, name);
  11739.               if (o == NULL)
  11740.                 {
  11741.                   (*_bfd_error_handler)
  11742.                     (_("%B: could not find output section %s"), abfd, name);
  11743.                   goto error_return;
  11744.                 }
  11745.               if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
  11746.                 {
  11747.                   (*_bfd_error_handler)
  11748.                     (_("warning: section '%s' is being made into a note"), name);
  11749.                   bfd_set_error (bfd_error_nonrepresentable_section);
  11750.                   goto error_return;
  11751.                 }
  11752.               dyn.d_un.d_ptr = o->vma;
  11753.               break;
  11754.  
  11755.             case DT_REL:
  11756.             case DT_RELA:
  11757.             case DT_RELSZ:
  11758.             case DT_RELASZ:
  11759.               if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
  11760.                 type = SHT_REL;
  11761.               else
  11762.                 type = SHT_RELA;
  11763.               dyn.d_un.d_val = 0;
  11764.               dyn.d_un.d_ptr = 0;
  11765.               for (i = 1; i < elf_numsections (abfd); i++)
  11766.                 {
  11767.                   Elf_Internal_Shdr *hdr;
  11768.  
  11769.                   hdr = elf_elfsections (abfd)[i];
  11770.                   if (hdr->sh_type == type
  11771.                       && (hdr->sh_flags & SHF_ALLOC) != 0)
  11772.                     {
  11773.                       if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
  11774.                         dyn.d_un.d_val += hdr->sh_size;
  11775.                       else
  11776.                         {
  11777.                           if (dyn.d_un.d_ptr == 0
  11778.                               || hdr->sh_addr < dyn.d_un.d_ptr)
  11779.                             dyn.d_un.d_ptr = hdr->sh_addr;
  11780.                         }
  11781.                     }
  11782.                 }
  11783.               break;
  11784.             }
  11785.           bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
  11786.         }
  11787.     }
  11788.  
  11789.   /* If we have created any dynamic sections, then output them.  */
  11790.   if (dynobj != NULL)
  11791.     {
  11792.       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
  11793.         goto error_return;
  11794.  
  11795.       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
  11796.       if (((info->warn_shared_textrel && bfd_link_pic (info))
  11797.            || info->error_textrel)
  11798.           && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
  11799.         {
  11800.           bfd_byte *dyncon, *dynconend;
  11801.  
  11802.           dyncon = o->contents;
  11803.           dynconend = o->contents + o->size;
  11804.           for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11805.             {
  11806.               Elf_Internal_Dyn dyn;
  11807.  
  11808.               bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11809.  
  11810.               if (dyn.d_tag == DT_TEXTREL)
  11811.                 {
  11812.                   if (info->error_textrel)
  11813.                     info->callbacks->einfo
  11814.                       (_("%P%X: read-only segment has dynamic relocations.\n"));
  11815.                   else
  11816.                     info->callbacks->einfo
  11817.                       (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
  11818.                   break;
  11819.                 }
  11820.             }
  11821.         }
  11822.  
  11823.       for (o = dynobj->sections; o != NULL; o = o->next)
  11824.         {
  11825.           if ((o->flags & SEC_HAS_CONTENTS) == 0
  11826.               || o->size == 0
  11827.               || o->output_section == bfd_abs_section_ptr)
  11828.             continue;
  11829.           if ((o->flags & SEC_LINKER_CREATED) == 0)
  11830.             {
  11831.               /* At this point, we are only interested in sections
  11832.                  created by _bfd_elf_link_create_dynamic_sections.  */
  11833.               continue;
  11834.             }
  11835.           if (elf_hash_table (info)->stab_info.stabstr == o)
  11836.             continue;
  11837.           if (elf_hash_table (info)->eh_info.hdr_sec == o)
  11838.             continue;
  11839.           if (strcmp (o->name, ".dynstr") != 0)
  11840.             {
  11841.               /* FIXME: octets_per_byte.  */
  11842.               if (! bfd_set_section_contents (abfd, o->output_section,
  11843.                                               o->contents,
  11844.                                               (file_ptr) o->output_offset,
  11845.                                               o->size))
  11846.                 goto error_return;
  11847.             }
  11848.           else
  11849.             {
  11850.               /* The contents of the .dynstr section are actually in a
  11851.                  stringtab.  */
  11852.               file_ptr off;
  11853.  
  11854.               off = elf_section_data (o->output_section)->this_hdr.sh_offset;
  11855.               if (bfd_seek (abfd, off, SEEK_SET) != 0
  11856.                   || ! _bfd_elf_strtab_emit (abfd,
  11857.                                              elf_hash_table (info)->dynstr))
  11858.                 goto error_return;
  11859.             }
  11860.         }
  11861.     }
  11862.  
  11863.   if (bfd_link_relocatable (info))
  11864.     {
  11865.       bfd_boolean failed = FALSE;
  11866.  
  11867.       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
  11868.       if (failed)
  11869.         goto error_return;
  11870.     }
  11871.  
  11872.   /* If we have optimized stabs strings, output them.  */
  11873.   if (elf_hash_table (info)->stab_info.stabstr != NULL)
  11874.     {
  11875.       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
  11876.         goto error_return;
  11877.     }
  11878.  
  11879.   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
  11880.     goto error_return;
  11881.  
  11882.   elf_final_link_free (abfd, &flinfo);
  11883.  
  11884.   elf_linker (abfd) = TRUE;
  11885.  
  11886.   if (attr_section)
  11887.     {
  11888.       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
  11889.       if (contents == NULL)
  11890.         return FALSE;   /* Bail out and fail.  */
  11891.       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
  11892.       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
  11893.       free (contents);
  11894.     }
  11895.  
  11896.   return TRUE;
  11897.  
  11898.  error_return:
  11899.   elf_final_link_free (abfd, &flinfo);
  11900.   return FALSE;
  11901. }
  11902. /* Initialize COOKIE for input bfd ABFD.  */
  11903.  
  11904. static bfd_boolean
  11905. init_reloc_cookie (struct elf_reloc_cookie *cookie,
  11906.                    struct bfd_link_info *info, bfd *abfd)
  11907. {
  11908.   Elf_Internal_Shdr *symtab_hdr;
  11909.   const struct elf_backend_data *bed;
  11910.  
  11911.   bed = get_elf_backend_data (abfd);
  11912.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11913.  
  11914.   cookie->abfd = abfd;
  11915.   cookie->sym_hashes = elf_sym_hashes (abfd);
  11916.   cookie->bad_symtab = elf_bad_symtab (abfd);
  11917.   if (cookie->bad_symtab)
  11918.     {
  11919.       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  11920.       cookie->extsymoff = 0;
  11921.     }
  11922.   else
  11923.     {
  11924.       cookie->locsymcount = symtab_hdr->sh_info;
  11925.       cookie->extsymoff = symtab_hdr->sh_info;
  11926.     }
  11927.  
  11928.   if (bed->s->arch_size == 32)
  11929.     cookie->r_sym_shift = 8;
  11930.   else
  11931.     cookie->r_sym_shift = 32;
  11932.  
  11933.   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  11934.   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
  11935.     {
  11936.       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  11937.                                               cookie->locsymcount, 0,
  11938.                                               NULL, NULL, NULL);
  11939.       if (cookie->locsyms == NULL)
  11940.         {
  11941.           info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
  11942.           return FALSE;
  11943.         }
  11944.       if (info->keep_memory)
  11945.         symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
  11946.     }
  11947.   return TRUE;
  11948. }
  11949.  
  11950. /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
  11951.  
  11952. static void
  11953. fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
  11954. {
  11955.   Elf_Internal_Shdr *symtab_hdr;
  11956.  
  11957.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11958.   if (cookie->locsyms != NULL
  11959.       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
  11960.     free (cookie->locsyms);
  11961. }
  11962.  
  11963. /* Initialize the relocation information in COOKIE for input section SEC
  11964.    of input bfd ABFD.  */
  11965.  
  11966. static bfd_boolean
  11967. init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11968.                         struct bfd_link_info *info, bfd *abfd,
  11969.                         asection *sec)
  11970. {
  11971.   const struct elf_backend_data *bed;
  11972.  
  11973.   if (sec->reloc_count == 0)
  11974.     {
  11975.       cookie->rels = NULL;
  11976.       cookie->relend = NULL;
  11977.     }
  11978.   else
  11979.     {
  11980.       bed = get_elf_backend_data (abfd);
  11981.  
  11982.       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  11983.                                                 info->keep_memory);
  11984.       if (cookie->rels == NULL)
  11985.         return FALSE;
  11986.       cookie->rel = cookie->rels;
  11987.       cookie->relend = (cookie->rels
  11988.                         + sec->reloc_count * bed->s->int_rels_per_ext_rel);
  11989.     }
  11990.   cookie->rel = cookie->rels;
  11991.   return TRUE;
  11992. }
  11993.  
  11994. /* Free the memory allocated by init_reloc_cookie_rels,
  11995.    if appropriate.  */
  11996.  
  11997. static void
  11998. fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11999.                         asection *sec)
  12000. {
  12001.   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
  12002.     free (cookie->rels);
  12003. }
  12004.  
  12005. /* Initialize the whole of COOKIE for input section SEC.  */
  12006.  
  12007. static bfd_boolean
  12008. init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  12009.                                struct bfd_link_info *info,
  12010.                                asection *sec)
  12011. {
  12012.   if (!init_reloc_cookie (cookie, info, sec->owner))
  12013.     goto error1;
  12014.   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
  12015.     goto error2;
  12016.   return TRUE;
  12017.  
  12018.  error2:
  12019.   fini_reloc_cookie (cookie, sec->owner);
  12020.  error1:
  12021.   return FALSE;
  12022. }
  12023.  
  12024. /* Free the memory allocated by init_reloc_cookie_for_section,
  12025.    if appropriate.  */
  12026.  
  12027. static void
  12028. fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  12029.                                asection *sec)
  12030. {
  12031.   fini_reloc_cookie_rels (cookie, sec);
  12032.   fini_reloc_cookie (cookie, sec->owner);
  12033. }
  12034. /* Garbage collect unused sections.  */
  12035.  
  12036. /* Default gc_mark_hook.  */
  12037.  
  12038. asection *
  12039. _bfd_elf_gc_mark_hook (asection *sec,
  12040.                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
  12041.                        Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
  12042.                        struct elf_link_hash_entry *h,
  12043.                        Elf_Internal_Sym *sym)
  12044. {
  12045.   if (h != NULL)
  12046.     {
  12047.       switch (h->root.type)
  12048.         {
  12049.         case bfd_link_hash_defined:
  12050.         case bfd_link_hash_defweak:
  12051.           return h->root.u.def.section;
  12052.  
  12053.         case bfd_link_hash_common:
  12054.           return h->root.u.c.p->section;
  12055.  
  12056.         default:
  12057.           break;
  12058.         }
  12059.     }
  12060.   else
  12061.     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
  12062.  
  12063.   return NULL;
  12064. }
  12065.  
  12066. /* COOKIE->rel describes a relocation against section SEC, which is
  12067.    a section we've decided to keep.  Return the section that contains
  12068.    the relocation symbol, or NULL if no section contains it.  */
  12069.  
  12070. asection *
  12071. _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
  12072.                        elf_gc_mark_hook_fn gc_mark_hook,
  12073.                        struct elf_reloc_cookie *cookie,
  12074.                        bfd_boolean *start_stop)
  12075. {
  12076.   unsigned long r_symndx;
  12077.   struct elf_link_hash_entry *h;
  12078.  
  12079.   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
  12080.   if (r_symndx == STN_UNDEF)
  12081.     return NULL;
  12082.  
  12083.   if (r_symndx >= cookie->locsymcount
  12084.       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  12085.     {
  12086.       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
  12087.       if (h == NULL)
  12088.         {
  12089.           info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
  12090.                                   sec->owner);
  12091.           return NULL;
  12092.         }
  12093.       while (h->root.type == bfd_link_hash_indirect
  12094.              || h->root.type == bfd_link_hash_warning)
  12095.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  12096.       h->mark = 1;
  12097.       /* If this symbol is weak and there is a non-weak definition, we
  12098.          keep the non-weak definition because many backends put
  12099.          dynamic reloc info on the non-weak definition for code
  12100.          handling copy relocs.  */
  12101.       if (h->u.weakdef != NULL)
  12102.         h->u.weakdef->mark = 1;
  12103.  
  12104.       if (start_stop != NULL
  12105.           && (h->root.type == bfd_link_hash_undefined
  12106.               || h->root.type == bfd_link_hash_undefweak))
  12107.         {
  12108.           /* To work around a glibc bug, mark all XXX input sections
  12109.              when there is an as yet undefined reference to __start_XXX
  12110.              or __stop_XXX symbols.  The linker will later define such
  12111.              symbols for orphan input sections that have a name
  12112.              representable as a C identifier.  */
  12113.           const char *sec_name = NULL;
  12114.           if (strncmp (h->root.root.string, "__start_", 8) == 0)
  12115.             sec_name = h->root.root.string + 8;
  12116.           else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
  12117.             sec_name = h->root.root.string + 7;
  12118.  
  12119.           if (sec_name != NULL && *sec_name != '\0')
  12120.             {
  12121.               bfd *i;
  12122.  
  12123.               for (i = info->input_bfds; i != NULL; i = i->link.next)
  12124.                 {
  12125.                   asection *s = bfd_get_section_by_name (i, sec_name);
  12126.                   if (s != NULL && !s->gc_mark)
  12127.                     {
  12128.                       *start_stop = TRUE;
  12129.                       return s;
  12130.                     }
  12131.                 }
  12132.             }
  12133.         }
  12134.  
  12135.       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
  12136.     }
  12137.  
  12138.   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
  12139.                           &cookie->locsyms[r_symndx]);
  12140. }
  12141.  
  12142. /* COOKIE->rel describes a relocation against section SEC, which is
  12143.    a section we've decided to keep.  Mark the section that contains
  12144.    the relocation symbol.  */
  12145.  
  12146. bfd_boolean
  12147. _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
  12148.                         asection *sec,
  12149.                         elf_gc_mark_hook_fn gc_mark_hook,
  12150.                         struct elf_reloc_cookie *cookie)
  12151. {
  12152.   asection *rsec;
  12153.   bfd_boolean start_stop = FALSE;
  12154.  
  12155.   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
  12156.   while (rsec != NULL)
  12157.     {
  12158.       if (!rsec->gc_mark)
  12159.         {
  12160.           if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
  12161.               || (rsec->owner->flags & DYNAMIC) != 0)
  12162.             rsec->gc_mark = 1;
  12163.           else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
  12164.             return FALSE;
  12165.         }
  12166.       if (!start_stop)
  12167.         break;
  12168.       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
  12169.     }
  12170.   return TRUE;
  12171. }
  12172.  
  12173. /* The mark phase of garbage collection.  For a given section, mark
  12174.    it and any sections in this section's group, and all the sections
  12175.    which define symbols to which it refers.  */
  12176.  
  12177. bfd_boolean
  12178. _bfd_elf_gc_mark (struct bfd_link_info *info,
  12179.                   asection *sec,
  12180.                   elf_gc_mark_hook_fn gc_mark_hook)
  12181. {
  12182.   bfd_boolean ret;
  12183.   asection *group_sec, *eh_frame;
  12184.  
  12185.   sec->gc_mark = 1;
  12186.  
  12187.   /* Mark all the sections in the group.  */
  12188.   group_sec = elf_section_data (sec)->next_in_group;
  12189.   if (group_sec && !group_sec->gc_mark)
  12190.     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
  12191.       return FALSE;
  12192.  
  12193.   /* Look through the section relocs.  */
  12194.   ret = TRUE;
  12195.   eh_frame = elf_eh_frame_section (sec->owner);
  12196.   if ((sec->flags & SEC_RELOC) != 0
  12197.       && sec->reloc_count > 0
  12198.       && sec != eh_frame)
  12199.     {
  12200.       struct elf_reloc_cookie cookie;
  12201.  
  12202.       if (!init_reloc_cookie_for_section (&cookie, info, sec))
  12203.         ret = FALSE;
  12204.       else
  12205.         {
  12206.           for (; cookie.rel < cookie.relend; cookie.rel++)
  12207.             if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
  12208.               {
  12209.                 ret = FALSE;
  12210.                 break;
  12211.               }
  12212.           fini_reloc_cookie_for_section (&cookie, sec);
  12213.         }
  12214.     }
  12215.  
  12216.   if (ret && eh_frame && elf_fde_list (sec))
  12217.     {
  12218.       struct elf_reloc_cookie cookie;
  12219.  
  12220.       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
  12221.         ret = FALSE;
  12222.       else
  12223.         {
  12224.           if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
  12225.                                       gc_mark_hook, &cookie))
  12226.             ret = FALSE;
  12227.           fini_reloc_cookie_for_section (&cookie, eh_frame);
  12228.         }
  12229.     }
  12230.  
  12231.   eh_frame = elf_section_eh_frame_entry (sec);
  12232.   if (ret && eh_frame && !eh_frame->gc_mark)
  12233.     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
  12234.       ret = FALSE;
  12235.  
  12236.   return ret;
  12237. }
  12238.  
  12239. /* Scan and mark sections in a special or debug section group.  */
  12240.  
  12241. static void
  12242. _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
  12243. {
  12244.   /* Point to first section of section group.  */
  12245.   asection *ssec;
  12246.   /* Used to iterate the section group.  */
  12247.   asection *msec;
  12248.  
  12249.   bfd_boolean is_special_grp = TRUE;
  12250.   bfd_boolean is_debug_grp = TRUE;
  12251.  
  12252.   /* First scan to see if group contains any section other than debug
  12253.      and special section.  */
  12254.   ssec = msec = elf_next_in_group (grp);
  12255.   do
  12256.     {
  12257.       if ((msec->flags & SEC_DEBUGGING) == 0)
  12258.         is_debug_grp = FALSE;
  12259.  
  12260.       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
  12261.         is_special_grp = FALSE;
  12262.  
  12263.       msec = elf_next_in_group (msec);
  12264.     }
  12265.   while (msec != ssec);
  12266.  
  12267.   /* If this is a pure debug section group or pure special section group,
  12268.      keep all sections in this group.  */
  12269.   if (is_debug_grp || is_special_grp)
  12270.     {
  12271.       do
  12272.         {
  12273.           msec->gc_mark = 1;
  12274.           msec = elf_next_in_group (msec);
  12275.         }
  12276.       while (msec != ssec);
  12277.     }
  12278. }
  12279.  
  12280. /* Keep debug and special sections.  */
  12281.  
  12282. bfd_boolean
  12283. _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
  12284.                                  elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
  12285. {
  12286.   bfd *ibfd;
  12287.  
  12288.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  12289.     {
  12290.       asection *isec;
  12291.       bfd_boolean some_kept;
  12292.       bfd_boolean debug_frag_seen;
  12293.  
  12294.       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  12295.         continue;
  12296.  
  12297.       /* Ensure all linker created sections are kept,
  12298.          see if any other section is already marked,
  12299.          and note if we have any fragmented debug sections.  */
  12300.       debug_frag_seen = some_kept = FALSE;
  12301.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12302.         {
  12303.           if ((isec->flags & SEC_LINKER_CREATED) != 0)
  12304.             isec->gc_mark = 1;
  12305.           else if (isec->gc_mark)
  12306.             some_kept = TRUE;
  12307.  
  12308.           if (debug_frag_seen == FALSE
  12309.               && (isec->flags & SEC_DEBUGGING)
  12310.               && CONST_STRNEQ (isec->name, ".debug_line."))
  12311.             debug_frag_seen = TRUE;
  12312.         }
  12313.  
  12314.       /* If no section in this file will be kept, then we can
  12315.          toss out the debug and special sections.  */
  12316.       if (!some_kept)
  12317.         continue;
  12318.  
  12319.       /* Keep debug and special sections like .comment when they are
  12320.          not part of a group.  Also keep section groups that contain
  12321.          just debug sections or special sections.  */
  12322.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12323.         {
  12324.           if ((isec->flags & SEC_GROUP) != 0)
  12325.             _bfd_elf_gc_mark_debug_special_section_group (isec);
  12326.           else if (((isec->flags & SEC_DEBUGGING) != 0
  12327.                     || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
  12328.                    && elf_next_in_group (isec) == NULL)
  12329.             isec->gc_mark = 1;
  12330.         }
  12331.  
  12332.       if (! debug_frag_seen)
  12333.         continue;
  12334.  
  12335.       /* Look for CODE sections which are going to be discarded,
  12336.          and find and discard any fragmented debug sections which
  12337.          are associated with that code section.  */
  12338.       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12339.         if ((isec->flags & SEC_CODE) != 0
  12340.             && isec->gc_mark == 0)
  12341.           {
  12342.             unsigned int ilen;
  12343.             asection *dsec;
  12344.  
  12345.             ilen = strlen (isec->name);
  12346.  
  12347.             /* Association is determined by the name of the debug section
  12348.                containing the name of the code section as a suffix.  For
  12349.                example .debug_line.text.foo is a debug section associated
  12350.                with .text.foo.  */
  12351.             for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
  12352.               {
  12353.                 unsigned int dlen;
  12354.  
  12355.                 if (dsec->gc_mark == 0
  12356.                     || (dsec->flags & SEC_DEBUGGING) == 0)
  12357.                   continue;
  12358.  
  12359.                 dlen = strlen (dsec->name);
  12360.  
  12361.                 if (dlen > ilen
  12362.                     && strncmp (dsec->name + (dlen - ilen),
  12363.                                 isec->name, ilen) == 0)
  12364.                   {
  12365.                     dsec->gc_mark = 0;
  12366.                   }
  12367.               }
  12368.           }
  12369.     }
  12370.   return TRUE;
  12371. }
  12372.  
  12373. /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
  12374.  
  12375. struct elf_gc_sweep_symbol_info
  12376. {
  12377.   struct bfd_link_info *info;
  12378.   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
  12379.                        bfd_boolean);
  12380. };
  12381.  
  12382. static bfd_boolean
  12383. elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
  12384. {
  12385.   if (!h->mark
  12386.       && (((h->root.type == bfd_link_hash_defined
  12387.             || h->root.type == bfd_link_hash_defweak)
  12388.            && !((h->def_regular || ELF_COMMON_DEF_P (h))
  12389.                 && h->root.u.def.section->gc_mark))
  12390.           || h->root.type == bfd_link_hash_undefined
  12391.           || h->root.type == bfd_link_hash_undefweak))
  12392.     {
  12393.       struct elf_gc_sweep_symbol_info *inf;
  12394.  
  12395.       inf = (struct elf_gc_sweep_symbol_info *) data;
  12396.       (*inf->hide_symbol) (inf->info, h, TRUE);
  12397.       h->def_regular = 0;
  12398.       h->ref_regular = 0;
  12399.       h->ref_regular_nonweak = 0;
  12400.     }
  12401.  
  12402.   return TRUE;
  12403. }
  12404.  
  12405. /* The sweep phase of garbage collection.  Remove all garbage sections.  */
  12406.  
  12407. typedef bfd_boolean (*gc_sweep_hook_fn)
  12408.   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
  12409.  
  12410. static bfd_boolean
  12411. elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
  12412. {
  12413.   bfd *sub;
  12414.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12415.   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
  12416.   unsigned long section_sym_count;
  12417.   struct elf_gc_sweep_symbol_info sweep_info;
  12418.  
  12419.   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  12420.     {
  12421.       asection *o;
  12422.  
  12423.       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
  12424.           || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
  12425.         continue;
  12426.  
  12427.       for (o = sub->sections; o != NULL; o = o->next)
  12428.         {
  12429.           /* When any section in a section group is kept, we keep all
  12430.              sections in the section group.  If the first member of
  12431.              the section group is excluded, we will also exclude the
  12432.              group section.  */
  12433.           if (o->flags & SEC_GROUP)
  12434.             {
  12435.               asection *first = elf_next_in_group (o);
  12436.               o->gc_mark = first->gc_mark;
  12437.             }
  12438.  
  12439.           if (o->gc_mark)
  12440.             continue;
  12441.  
  12442.           /* Skip sweeping sections already excluded.  */
  12443.           if (o->flags & SEC_EXCLUDE)
  12444.             continue;
  12445.  
  12446.           /* Since this is early in the link process, it is simple
  12447.              to remove a section from the output.  */
  12448.           o->flags |= SEC_EXCLUDE;
  12449.  
  12450.           if (info->print_gc_sections && o->size != 0)
  12451.             _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
  12452.  
  12453.           /* But we also have to update some of the relocation
  12454.              info we collected before.  */
  12455.           if (gc_sweep_hook
  12456.               && (o->flags & SEC_RELOC) != 0
  12457.               && o->reloc_count != 0
  12458.               && !((info->strip == strip_all || info->strip == strip_debugger)
  12459.                    && (o->flags & SEC_DEBUGGING) != 0)
  12460.               && !bfd_is_abs_section (o->output_section))
  12461.             {
  12462.               Elf_Internal_Rela *internal_relocs;
  12463.               bfd_boolean r;
  12464.  
  12465.               internal_relocs
  12466.                 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
  12467.                                              info->keep_memory);
  12468.               if (internal_relocs == NULL)
  12469.                 return FALSE;
  12470.  
  12471.               r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
  12472.  
  12473.               if (elf_section_data (o)->relocs != internal_relocs)
  12474.                 free (internal_relocs);
  12475.  
  12476.               if (!r)
  12477.                 return FALSE;
  12478.             }
  12479.         }
  12480.     }
  12481.  
  12482.   /* Remove the symbols that were in the swept sections from the dynamic
  12483.      symbol table.  GCFIXME: Anyone know how to get them out of the
  12484.      static symbol table as well?  */
  12485.   sweep_info.info = info;
  12486.   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
  12487.   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
  12488.                           &sweep_info);
  12489.  
  12490.   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
  12491.   return TRUE;
  12492. }
  12493.  
  12494. /* Propagate collected vtable information.  This is called through
  12495.    elf_link_hash_traverse.  */
  12496.  
  12497. static bfd_boolean
  12498. elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
  12499. {
  12500.   /* Those that are not vtables.  */
  12501.   if (h->vtable == NULL || h->vtable->parent == NULL)
  12502.     return TRUE;
  12503.  
  12504.   /* Those vtables that do not have parents, we cannot merge.  */
  12505.   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
  12506.     return TRUE;
  12507.  
  12508.   /* If we've already been done, exit.  */
  12509.   if (h->vtable->used && h->vtable->used[-1])
  12510.     return TRUE;
  12511.  
  12512.   /* Make sure the parent's table is up to date.  */
  12513.   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
  12514.  
  12515.   if (h->vtable->used == NULL)
  12516.     {
  12517.       /* None of this table's entries were referenced.  Re-use the
  12518.          parent's table.  */
  12519.       h->vtable->used = h->vtable->parent->vtable->used;
  12520.       h->vtable->size = h->vtable->parent->vtable->size;
  12521.     }
  12522.   else
  12523.     {
  12524.       size_t n;
  12525.       bfd_boolean *cu, *pu;
  12526.  
  12527.       /* Or the parent's entries into ours.  */
  12528.       cu = h->vtable->used;
  12529.       cu[-1] = TRUE;
  12530.       pu = h->vtable->parent->vtable->used;
  12531.       if (pu != NULL)
  12532.         {
  12533.           const struct elf_backend_data *bed;
  12534.           unsigned int log_file_align;
  12535.  
  12536.           bed = get_elf_backend_data (h->root.u.def.section->owner);
  12537.           log_file_align = bed->s->log_file_align;
  12538.           n = h->vtable->parent->vtable->size >> log_file_align;
  12539.           while (n--)
  12540.             {
  12541.               if (*pu)
  12542.                 *cu = TRUE;
  12543.               pu++;
  12544.               cu++;
  12545.             }
  12546.         }
  12547.     }
  12548.  
  12549.   return TRUE;
  12550. }
  12551.  
  12552. static bfd_boolean
  12553. elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
  12554. {
  12555.   asection *sec;
  12556.   bfd_vma hstart, hend;
  12557.   Elf_Internal_Rela *relstart, *relend, *rel;
  12558.   const struct elf_backend_data *bed;
  12559.   unsigned int log_file_align;
  12560.  
  12561.   /* Take care of both those symbols that do not describe vtables as
  12562.      well as those that are not loaded.  */
  12563.   if (h->vtable == NULL || h->vtable->parent == NULL)
  12564.     return TRUE;
  12565.  
  12566.   BFD_ASSERT (h->root.type == bfd_link_hash_defined
  12567.               || h->root.type == bfd_link_hash_defweak);
  12568.  
  12569.   sec = h->root.u.def.section;
  12570.   hstart = h->root.u.def.value;
  12571.   hend = hstart + h->size;
  12572.  
  12573.   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
  12574.   if (!relstart)
  12575.     return *(bfd_boolean *) okp = FALSE;
  12576.   bed = get_elf_backend_data (sec->owner);
  12577.   log_file_align = bed->s->log_file_align;
  12578.  
  12579.   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
  12580.  
  12581.   for (rel = relstart; rel < relend; ++rel)
  12582.     if (rel->r_offset >= hstart && rel->r_offset < hend)
  12583.       {
  12584.         /* If the entry is in use, do nothing.  */
  12585.         if (h->vtable->used
  12586.             && (rel->r_offset - hstart) < h->vtable->size)
  12587.           {
  12588.             bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
  12589.             if (h->vtable->used[entry])
  12590.               continue;
  12591.           }
  12592.         /* Otherwise, kill it.  */
  12593.         rel->r_offset = rel->r_info = rel->r_addend = 0;
  12594.       }
  12595.  
  12596.   return TRUE;
  12597. }
  12598.  
  12599. /* Mark sections containing dynamically referenced symbols.  When
  12600.    building shared libraries, we must assume that any visible symbol is
  12601.    referenced.  */
  12602.  
  12603. bfd_boolean
  12604. bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
  12605. {
  12606.   struct bfd_link_info *info = (struct bfd_link_info *) inf;
  12607.   struct bfd_elf_dynamic_list *d = info->dynamic_list;
  12608.  
  12609.   if ((h->root.type == bfd_link_hash_defined
  12610.        || h->root.type == bfd_link_hash_defweak)
  12611.       && (h->ref_dynamic
  12612.           || ((h->def_regular || ELF_COMMON_DEF_P (h))
  12613.               && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  12614.               && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
  12615.               && (!bfd_link_executable (info)
  12616.                   || info->export_dynamic
  12617.                   || (h->dynamic
  12618.                       && d != NULL
  12619.                       && (*d->match) (&d->head, NULL, h->root.root.string)))
  12620.               && (h->versioned >= versioned
  12621.                   || !bfd_hide_sym_by_version (info->version_info,
  12622.                                                h->root.root.string)))))
  12623.     h->root.u.def.section->flags |= SEC_KEEP;
  12624.  
  12625.   return TRUE;
  12626. }
  12627.  
  12628. /* Keep all sections containing symbols undefined on the command-line,
  12629.    and the section containing the entry symbol.  */
  12630.  
  12631. void
  12632. _bfd_elf_gc_keep (struct bfd_link_info *info)
  12633. {
  12634.   struct bfd_sym_chain *sym;
  12635.  
  12636.   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
  12637.     {
  12638.       struct elf_link_hash_entry *h;
  12639.  
  12640.       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
  12641.                                 FALSE, FALSE, FALSE);
  12642.  
  12643.       if (h != NULL
  12644.           && (h->root.type == bfd_link_hash_defined
  12645.               || h->root.type == bfd_link_hash_defweak)
  12646.           && !bfd_is_abs_section (h->root.u.def.section))
  12647.         h->root.u.def.section->flags |= SEC_KEEP;
  12648.     }
  12649. }
  12650.  
  12651. bfd_boolean
  12652. bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
  12653.                                 struct bfd_link_info *info)
  12654. {
  12655.   bfd *ibfd = info->input_bfds;
  12656.  
  12657.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  12658.     {
  12659.       asection *sec;
  12660.       struct elf_reloc_cookie cookie;
  12661.  
  12662.       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  12663.         continue;
  12664.  
  12665.       if (!init_reloc_cookie (&cookie, info, ibfd))
  12666.         return FALSE;
  12667.  
  12668.       for (sec = ibfd->sections; sec; sec = sec->next)
  12669.         {
  12670.           if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
  12671.               && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
  12672.             {
  12673.               _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
  12674.               fini_reloc_cookie_rels (&cookie, sec);
  12675.             }
  12676.         }
  12677.     }
  12678.   return TRUE;
  12679. }
  12680.  
  12681. /* Do mark and sweep of unused sections.  */
  12682.  
  12683. bfd_boolean
  12684. bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
  12685. {
  12686.   bfd_boolean ok = TRUE;
  12687.   bfd *sub;
  12688.   elf_gc_mark_hook_fn gc_mark_hook;
  12689.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12690.   struct elf_link_hash_table *htab;
  12691.  
  12692.   if (!bed->can_gc_sections
  12693.       || !is_elf_hash_table (info->hash))
  12694.     {
  12695.       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
  12696.       return TRUE;
  12697.     }
  12698.  
  12699.   bed->gc_keep (info);
  12700.   htab = elf_hash_table (info);
  12701.  
  12702.   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
  12703.      at the .eh_frame section if we can mark the FDEs individually.  */
  12704.   for (sub = info->input_bfds;
  12705.        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
  12706.        sub = sub->link.next)
  12707.     {
  12708.       asection *sec;
  12709.       struct elf_reloc_cookie cookie;
  12710.  
  12711.       sec = bfd_get_section_by_name (sub, ".eh_frame");
  12712.       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
  12713.         {
  12714.           _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
  12715.           if (elf_section_data (sec)->sec_info
  12716.               && (sec->flags & SEC_LINKER_CREATED) == 0)
  12717.             elf_eh_frame_section (sub) = sec;
  12718.           fini_reloc_cookie_for_section (&cookie, sec);
  12719.           sec = bfd_get_next_section_by_name (NULL, sec);
  12720.         }
  12721.     }
  12722.  
  12723.   /* Apply transitive closure to the vtable entry usage info.  */
  12724.   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
  12725.   if (!ok)
  12726.     return FALSE;
  12727.  
  12728.   /* Kill the vtable relocations that were not used.  */
  12729.   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
  12730.   if (!ok)
  12731.     return FALSE;
  12732.  
  12733.   /* Mark dynamically referenced symbols.  */
  12734.   if (htab->dynamic_sections_created)
  12735.     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
  12736.  
  12737.   /* Grovel through relocs to find out who stays ...  */
  12738.   gc_mark_hook = bed->gc_mark_hook;
  12739.   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  12740.     {
  12741.       asection *o;
  12742.  
  12743.       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
  12744.           || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
  12745.         continue;
  12746.  
  12747.       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
  12748.          Also treat note sections as a root, if the section is not part
  12749.          of a group.  */
  12750.       for (o = sub->sections; o != NULL; o = o->next)
  12751.         if (!o->gc_mark
  12752.             && (o->flags & SEC_EXCLUDE) == 0
  12753.             && ((o->flags & SEC_KEEP) != 0
  12754.                 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
  12755.                     && elf_next_in_group (o) == NULL )))
  12756.           {
  12757.             if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
  12758.               return FALSE;
  12759.           }
  12760.     }
  12761.  
  12762.   /* Allow the backend to mark additional target specific sections.  */
  12763.   bed->gc_mark_extra_sections (info, gc_mark_hook);
  12764.  
  12765.   /* ... and mark SEC_EXCLUDE for those that go.  */
  12766.   return elf_gc_sweep (abfd, info);
  12767. }
  12768. /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
  12769.  
  12770. bfd_boolean
  12771. bfd_elf_gc_record_vtinherit (bfd *abfd,
  12772.                              asection *sec,
  12773.                              struct elf_link_hash_entry *h,
  12774.                              bfd_vma offset)
  12775. {
  12776.   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  12777.   struct elf_link_hash_entry **search, *child;
  12778.   bfd_size_type extsymcount;
  12779.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12780.  
  12781.   /* The sh_info field of the symtab header tells us where the
  12782.      external symbols start.  We don't care about the local symbols at
  12783.      this point.  */
  12784.   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
  12785.   if (!elf_bad_symtab (abfd))
  12786.     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
  12787.  
  12788.   sym_hashes = elf_sym_hashes (abfd);
  12789.   sym_hashes_end = sym_hashes + extsymcount;
  12790.  
  12791.   /* Hunt down the child symbol, which is in this section at the same
  12792.      offset as the relocation.  */
  12793.   for (search = sym_hashes; search != sym_hashes_end; ++search)
  12794.     {
  12795.       if ((child = *search) != NULL
  12796.           && (child->root.type == bfd_link_hash_defined
  12797.               || child->root.type == bfd_link_hash_defweak)
  12798.           && child->root.u.def.section == sec
  12799.           && child->root.u.def.value == offset)
  12800.         goto win;
  12801.     }
  12802.  
  12803.   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
  12804.                          abfd, sec, (unsigned long) offset);
  12805.   bfd_set_error (bfd_error_invalid_operation);
  12806.   return FALSE;
  12807.  
  12808.  win:
  12809.   if (!child->vtable)
  12810.     {
  12811.       child->vtable = ((struct elf_link_virtual_table_entry *)
  12812.                        bfd_zalloc (abfd, sizeof (*child->vtable)));
  12813.       if (!child->vtable)
  12814.         return FALSE;
  12815.     }
  12816.   if (!h)
  12817.     {
  12818.       /* This *should* only be the absolute section.  It could potentially
  12819.          be that someone has defined a non-global vtable though, which
  12820.          would be bad.  It isn't worth paging in the local symbols to be
  12821.          sure though; that case should simply be handled by the assembler.  */
  12822.  
  12823.       child->vtable->parent = (struct elf_link_hash_entry *) -1;
  12824.     }
  12825.   else
  12826.     child->vtable->parent = h;
  12827.  
  12828.   return TRUE;
  12829. }
  12830.  
  12831. /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
  12832.  
  12833. bfd_boolean
  12834. bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
  12835.                            asection *sec ATTRIBUTE_UNUSED,
  12836.                            struct elf_link_hash_entry *h,
  12837.                            bfd_vma addend)
  12838. {
  12839.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12840.   unsigned int log_file_align = bed->s->log_file_align;
  12841.  
  12842.   if (!h->vtable)
  12843.     {
  12844.       h->vtable = ((struct elf_link_virtual_table_entry *)
  12845.                    bfd_zalloc (abfd, sizeof (*h->vtable)));
  12846.       if (!h->vtable)
  12847.         return FALSE;
  12848.     }
  12849.  
  12850.   if (addend >= h->vtable->size)
  12851.     {
  12852.       size_t size, bytes, file_align;
  12853.       bfd_boolean *ptr = h->vtable->used;
  12854.  
  12855.       /* While the symbol is undefined, we have to be prepared to handle
  12856.          a zero size.  */
  12857.       file_align = 1 << log_file_align;
  12858.       if (h->root.type == bfd_link_hash_undefined)
  12859.         size = addend + file_align;
  12860.       else
  12861.         {
  12862.           size = h->size;
  12863.           if (addend >= size)
  12864.             {
  12865.               /* Oops!  We've got a reference past the defined end of
  12866.                  the table.  This is probably a bug -- shall we warn?  */
  12867.               size = addend + file_align;
  12868.             }
  12869.         }
  12870.       size = (size + file_align - 1) & -file_align;
  12871.  
  12872.       /* Allocate one extra entry for use as a "done" flag for the
  12873.          consolidation pass.  */
  12874.       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
  12875.  
  12876.       if (ptr)
  12877.         {
  12878.           ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
  12879.  
  12880.           if (ptr != NULL)
  12881.             {
  12882.               size_t oldbytes;
  12883.  
  12884.               oldbytes = (((h->vtable->size >> log_file_align) + 1)
  12885.                           * sizeof (bfd_boolean));
  12886.               memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
  12887.             }
  12888.         }
  12889.       else
  12890.         ptr = (bfd_boolean *) bfd_zmalloc (bytes);
  12891.  
  12892.       if (ptr == NULL)
  12893.         return FALSE;
  12894.  
  12895.       /* And arrange for that done flag to be at index -1.  */
  12896.       h->vtable->used = ptr + 1;
  12897.       h->vtable->size = size;
  12898.     }
  12899.  
  12900.   h->vtable->used[addend >> log_file_align] = TRUE;
  12901.  
  12902.   return TRUE;
  12903. }
  12904.  
  12905. /* Map an ELF section header flag to its corresponding string.  */
  12906. typedef struct
  12907. {
  12908.   char *flag_name;
  12909.   flagword flag_value;
  12910. } elf_flags_to_name_table;
  12911.  
  12912. static elf_flags_to_name_table elf_flags_to_names [] =
  12913. {
  12914.   { "SHF_WRITE", SHF_WRITE },
  12915.   { "SHF_ALLOC", SHF_ALLOC },
  12916.   { "SHF_EXECINSTR", SHF_EXECINSTR },
  12917.   { "SHF_MERGE", SHF_MERGE },
  12918.   { "SHF_STRINGS", SHF_STRINGS },
  12919.   { "SHF_INFO_LINK", SHF_INFO_LINK},
  12920.   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
  12921.   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
  12922.   { "SHF_GROUP", SHF_GROUP },
  12923.   { "SHF_TLS", SHF_TLS },
  12924.   { "SHF_MASKOS", SHF_MASKOS },
  12925.   { "SHF_EXCLUDE", SHF_EXCLUDE },
  12926. };
  12927.  
  12928. /* Returns TRUE if the section is to be included, otherwise FALSE.  */
  12929. bfd_boolean
  12930. bfd_elf_lookup_section_flags (struct bfd_link_info *info,
  12931.                               struct flag_info *flaginfo,
  12932.                               asection *section)
  12933. {
  12934.   const bfd_vma sh_flags = elf_section_flags (section);
  12935.  
  12936.   if (!flaginfo->flags_initialized)
  12937.     {
  12938.       bfd *obfd = info->output_bfd;
  12939.       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  12940.       struct flag_info_list *tf = flaginfo->flag_list;
  12941.       int with_hex = 0;
  12942.       int without_hex = 0;
  12943.  
  12944.       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
  12945.         {
  12946.           unsigned i;
  12947.           flagword (*lookup) (char *);
  12948.  
  12949.           lookup = bed->elf_backend_lookup_section_flags_hook;
  12950.           if (lookup != NULL)
  12951.             {
  12952.               flagword hexval = (*lookup) ((char *) tf->name);
  12953.  
  12954.               if (hexval != 0)
  12955.                 {
  12956.                   if (tf->with == with_flags)
  12957.                     with_hex |= hexval;
  12958.                   else if (tf->with == without_flags)
  12959.                     without_hex |= hexval;
  12960.                   tf->valid = TRUE;
  12961.                   continue;
  12962.                 }
  12963.             }
  12964.           for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
  12965.             {
  12966.               if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
  12967.                 {
  12968.                   if (tf->with == with_flags)
  12969.                     with_hex |= elf_flags_to_names[i].flag_value;
  12970.                   else if (tf->with == without_flags)
  12971.                     without_hex |= elf_flags_to_names[i].flag_value;
  12972.                   tf->valid = TRUE;
  12973.                   break;
  12974.                 }
  12975.             }
  12976.           if (!tf->valid)
  12977.             {
  12978.               info->callbacks->einfo
  12979.                 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
  12980.               return FALSE;
  12981.             }
  12982.         }
  12983.       flaginfo->flags_initialized = TRUE;
  12984.       flaginfo->only_with_flags |= with_hex;
  12985.       flaginfo->not_with_flags |= without_hex;
  12986.     }
  12987.  
  12988.   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
  12989.     return FALSE;
  12990.  
  12991.   if ((flaginfo->not_with_flags & sh_flags) != 0)
  12992.     return FALSE;
  12993.  
  12994.   return TRUE;
  12995. }
  12996.  
  12997. struct alloc_got_off_arg {
  12998.   bfd_vma gotoff;
  12999.   struct bfd_link_info *info;
  13000. };
  13001.  
  13002. /* We need a special top-level link routine to convert got reference counts
  13003.    to real got offsets.  */
  13004.  
  13005. static bfd_boolean
  13006. elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
  13007. {
  13008.   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
  13009.   bfd *obfd = gofarg->info->output_bfd;
  13010.   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  13011.  
  13012.   if (h->got.refcount > 0)
  13013.     {
  13014.       h->got.offset = gofarg->gotoff;
  13015.       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
  13016.     }
  13017.   else
  13018.     h->got.offset = (bfd_vma) -1;
  13019.  
  13020.   return TRUE;
  13021. }
  13022.  
  13023. /* And an accompanying bit to work out final got entry offsets once
  13024.    we're done.  Should be called from final_link.  */
  13025.  
  13026. bfd_boolean
  13027. bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
  13028.                                         struct bfd_link_info *info)
  13029. {
  13030.   bfd *i;
  13031.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13032.   bfd_vma gotoff;
  13033.   struct alloc_got_off_arg gofarg;
  13034.  
  13035.   BFD_ASSERT (abfd == info->output_bfd);
  13036.  
  13037.   if (! is_elf_hash_table (info->hash))
  13038.     return FALSE;
  13039.  
  13040.   /* The GOT offset is relative to the .got section, but the GOT header is
  13041.      put into the .got.plt section, if the backend uses it.  */
  13042.   if (bed->want_got_plt)
  13043.     gotoff = 0;
  13044.   else
  13045.     gotoff = bed->got_header_size;
  13046.  
  13047.   /* Do the local .got entries first.  */
  13048.   for (i = info->input_bfds; i; i = i->link.next)
  13049.     {
  13050.       bfd_signed_vma *local_got;
  13051.       bfd_size_type j, locsymcount;
  13052.       Elf_Internal_Shdr *symtab_hdr;
  13053.  
  13054.       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
  13055.         continue;
  13056.  
  13057.       local_got = elf_local_got_refcounts (i);
  13058.       if (!local_got)
  13059.         continue;
  13060.  
  13061.       symtab_hdr = &elf_tdata (i)->symtab_hdr;
  13062.       if (elf_bad_symtab (i))
  13063.         locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  13064.       else
  13065.         locsymcount = symtab_hdr->sh_info;
  13066.  
  13067.       for (j = 0; j < locsymcount; ++j)
  13068.         {
  13069.           if (local_got[j] > 0)
  13070.             {
  13071.               local_got[j] = gotoff;
  13072.               gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
  13073.             }
  13074.           else
  13075.             local_got[j] = (bfd_vma) -1;
  13076.         }
  13077.     }
  13078.  
  13079.   /* Then the global .got entries.  .plt refcounts are handled by
  13080.      adjust_dynamic_symbol  */
  13081.   gofarg.gotoff = gotoff;
  13082.   gofarg.info = info;
  13083.   elf_link_hash_traverse (elf_hash_table (info),
  13084.                           elf_gc_allocate_got_offsets,
  13085.                           &gofarg);
  13086.   return TRUE;
  13087. }
  13088.  
  13089. /* Many folk need no more in the way of final link than this, once
  13090.    got entry reference counting is enabled.  */
  13091.  
  13092. bfd_boolean
  13093. bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
  13094. {
  13095.   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
  13096.     return FALSE;
  13097.  
  13098.   /* Invoke the regular ELF backend linker to do all the work.  */
  13099.   return bfd_elf_final_link (abfd, info);
  13100. }
  13101.  
  13102. bfd_boolean
  13103. bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
  13104. {
  13105.   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
  13106.  
  13107.   if (rcookie->bad_symtab)
  13108.     rcookie->rel = rcookie->rels;
  13109.  
  13110.   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
  13111.     {
  13112.       unsigned long r_symndx;
  13113.  
  13114.       if (! rcookie->bad_symtab)
  13115.         if (rcookie->rel->r_offset > offset)
  13116.           return FALSE;
  13117.       if (rcookie->rel->r_offset != offset)
  13118.         continue;
  13119.  
  13120.       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
  13121.       if (r_symndx == STN_UNDEF)
  13122.         return TRUE;
  13123.  
  13124.       if (r_symndx >= rcookie->locsymcount
  13125.           || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  13126.         {
  13127.           struct elf_link_hash_entry *h;
  13128.  
  13129.           h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
  13130.  
  13131.           while (h->root.type == bfd_link_hash_indirect
  13132.                  || h->root.type == bfd_link_hash_warning)
  13133.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  13134.  
  13135.           if ((h->root.type == bfd_link_hash_defined
  13136.                || h->root.type == bfd_link_hash_defweak)
  13137.               && (h->root.u.def.section->owner != rcookie->abfd
  13138.                   || h->root.u.def.section->kept_section != NULL
  13139.                   || discarded_section (h->root.u.def.section)))
  13140.             return TRUE;
  13141.         }
  13142.       else
  13143.         {
  13144.           /* It's not a relocation against a global symbol,
  13145.              but it could be a relocation against a local
  13146.              symbol for a discarded section.  */
  13147.           asection *isec;
  13148.           Elf_Internal_Sym *isym;
  13149.  
  13150.           /* Need to: get the symbol; get the section.  */
  13151.           isym = &rcookie->locsyms[r_symndx];
  13152.           isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
  13153.           if (isec != NULL
  13154.               && (isec->kept_section != NULL
  13155.                   || discarded_section (isec)))
  13156.             return TRUE;
  13157.         }
  13158.       return FALSE;
  13159.     }
  13160.   return FALSE;
  13161. }
  13162.  
  13163. /* Discard unneeded references to discarded sections.
  13164.    Returns -1 on error, 1 if any section's size was changed, 0 if
  13165.    nothing changed.  This function assumes that the relocations are in
  13166.    sorted order, which is true for all known assemblers.  */
  13167.  
  13168. int
  13169. bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
  13170. {
  13171.   struct elf_reloc_cookie cookie;
  13172.   asection *o;
  13173.   bfd *abfd;
  13174.   int changed = 0;
  13175.  
  13176.   if (info->traditional_format
  13177.       || !is_elf_hash_table (info->hash))
  13178.     return 0;
  13179.  
  13180.   o = bfd_get_section_by_name (output_bfd, ".stab");
  13181.   if (o != NULL)
  13182.     {
  13183.       asection *i;
  13184.  
  13185.       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  13186.         {
  13187.           if (i->size == 0
  13188.               || i->reloc_count == 0
  13189.               || i->sec_info_type != SEC_INFO_TYPE_STABS)
  13190.             continue;
  13191.  
  13192.           abfd = i->owner;
  13193.           if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  13194.             continue;
  13195.  
  13196.           if (!init_reloc_cookie_for_section (&cookie, info, i))
  13197.             return -1;
  13198.  
  13199.           if (_bfd_discard_section_stabs (abfd, i,
  13200.                                           elf_section_data (i)->sec_info,
  13201.                                           bfd_elf_reloc_symbol_deleted_p,
  13202.                                           &cookie))
  13203.             changed = 1;
  13204.  
  13205.           fini_reloc_cookie_for_section (&cookie, i);
  13206.         }
  13207.     }
  13208.  
  13209.   o = NULL;
  13210.   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
  13211.     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
  13212.   if (o != NULL)
  13213.     {
  13214.       asection *i;
  13215.  
  13216.       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  13217.         {
  13218.           if (i->size == 0)
  13219.             continue;
  13220.  
  13221.           abfd = i->owner;
  13222.           if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  13223.             continue;
  13224.  
  13225.           if (!init_reloc_cookie_for_section (&cookie, info, i))
  13226.             return -1;
  13227.  
  13228.           _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
  13229.           if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
  13230.                                                  bfd_elf_reloc_symbol_deleted_p,
  13231.                                                  &cookie))
  13232.             changed = 1;
  13233.  
  13234.           fini_reloc_cookie_for_section (&cookie, i);
  13235.         }
  13236.     }
  13237.  
  13238.   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  13239.     {
  13240.       const struct elf_backend_data *bed;
  13241.  
  13242.       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  13243.         continue;
  13244.  
  13245.       bed = get_elf_backend_data (abfd);
  13246.  
  13247.       if (bed->elf_backend_discard_info != NULL)
  13248.         {
  13249.           if (!init_reloc_cookie (&cookie, info, abfd))
  13250.             return -1;
  13251.  
  13252.           if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
  13253.             changed = 1;
  13254.  
  13255.           fini_reloc_cookie (&cookie, abfd);
  13256.         }
  13257.     }
  13258.  
  13259.   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
  13260.     _bfd_elf_end_eh_frame_parsing (info);
  13261.  
  13262.   if (info->eh_frame_hdr_type
  13263.       && !bfd_link_relocatable (info)
  13264.       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
  13265.     changed = 1;
  13266.  
  13267.   return changed;
  13268. }
  13269.  
  13270. bfd_boolean
  13271. _bfd_elf_section_already_linked (bfd *abfd,
  13272.                                  asection *sec,
  13273.                                  struct bfd_link_info *info)
  13274. {
  13275.   flagword flags;
  13276.   const char *name, *key;
  13277.   struct bfd_section_already_linked *l;
  13278.   struct bfd_section_already_linked_hash_entry *already_linked_list;
  13279.  
  13280.   if (sec->output_section == bfd_abs_section_ptr)
  13281.     return FALSE;
  13282.  
  13283.   flags = sec->flags;
  13284.  
  13285.   /* Return if it isn't a linkonce section.  A comdat group section
  13286.      also has SEC_LINK_ONCE set.  */
  13287.   if ((flags & SEC_LINK_ONCE) == 0)
  13288.     return FALSE;
  13289.  
  13290.   /* Don't put group member sections on our list of already linked
  13291.      sections.  They are handled as a group via their group section.  */
  13292.   if (elf_sec_group (sec) != NULL)
  13293.     return FALSE;
  13294.  
  13295.   /* For a SHT_GROUP section, use the group signature as the key.  */
  13296.   name = sec->name;
  13297.   if ((flags & SEC_GROUP) != 0
  13298.       && elf_next_in_group (sec) != NULL
  13299.       && elf_group_name (elf_next_in_group (sec)) != NULL)
  13300.     key = elf_group_name (elf_next_in_group (sec));
  13301.   else
  13302.     {
  13303.       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
  13304.       if (CONST_STRNEQ (name, ".gnu.linkonce.")
  13305.           && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
  13306.         key++;
  13307.       else
  13308.         /* Must be a user linkonce section that doesn't follow gcc's
  13309.            naming convention.  In this case we won't be matching
  13310.            single member groups.  */
  13311.         key = name;
  13312.     }
  13313.  
  13314.   already_linked_list = bfd_section_already_linked_table_lookup (key);
  13315.  
  13316.   for (l = already_linked_list->entry; l != NULL; l = l->next)
  13317.     {
  13318.       /* We may have 2 different types of sections on the list: group
  13319.          sections with a signature of <key> (<key> is some string),
  13320.          and linkonce sections named .gnu.linkonce.<type>.<key>.
  13321.          Match like sections.  LTO plugin sections are an exception.
  13322.          They are always named .gnu.linkonce.t.<key> and match either
  13323.          type of section.  */
  13324.       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
  13325.            && ((flags & SEC_GROUP) != 0
  13326.                || strcmp (name, l->sec->name) == 0))
  13327.           || (l->sec->owner->flags & BFD_PLUGIN) != 0)
  13328.         {
  13329.           /* The section has already been linked.  See if we should
  13330.              issue a warning.  */
  13331.           if (!_bfd_handle_already_linked (sec, l, info))
  13332.             return FALSE;
  13333.  
  13334.           if (flags & SEC_GROUP)
  13335.             {
  13336.               asection *first = elf_next_in_group (sec);
  13337.               asection *s = first;
  13338.  
  13339.               while (s != NULL)
  13340.                 {
  13341.                   s->output_section = bfd_abs_section_ptr;
  13342.                   /* Record which group discards it.  */
  13343.                   s->kept_section = l->sec;
  13344.                   s = elf_next_in_group (s);
  13345.                   /* These lists are circular.  */
  13346.                   if (s == first)
  13347.                     break;
  13348.                 }
  13349.             }
  13350.  
  13351.           return TRUE;
  13352.         }
  13353.     }
  13354.  
  13355.   /* A single member comdat group section may be discarded by a
  13356.      linkonce section and vice versa.  */
  13357.   if ((flags & SEC_GROUP) != 0)
  13358.     {
  13359.       asection *first = elf_next_in_group (sec);
  13360.  
  13361.       if (first != NULL && elf_next_in_group (first) == first)
  13362.         /* Check this single member group against linkonce sections.  */
  13363.         for (l = already_linked_list->entry; l != NULL; l = l->next)
  13364.           if ((l->sec->flags & SEC_GROUP) == 0
  13365.               && bfd_elf_match_symbols_in_sections (l->sec, first, info))
  13366.             {
  13367.               first->output_section = bfd_abs_section_ptr;
  13368.               first->kept_section = l->sec;
  13369.               sec->output_section = bfd_abs_section_ptr;
  13370.               break;
  13371.             }
  13372.     }
  13373.   else
  13374.     /* Check this linkonce section against single member groups.  */
  13375.     for (l = already_linked_list->entry; l != NULL; l = l->next)
  13376.       if (l->sec->flags & SEC_GROUP)
  13377.         {
  13378.           asection *first = elf_next_in_group (l->sec);
  13379.  
  13380.           if (first != NULL
  13381.               && elf_next_in_group (first) == first
  13382.               && bfd_elf_match_symbols_in_sections (first, sec, info))
  13383.             {
  13384.               sec->output_section = bfd_abs_section_ptr;
  13385.               sec->kept_section = first;
  13386.               break;
  13387.             }
  13388.         }
  13389.  
  13390.   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
  13391.      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
  13392.      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
  13393.      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
  13394.      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
  13395.      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
  13396.      `.gnu.linkonce.t.F' section from a different bfd not requiring any
  13397.      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
  13398.      The reverse order cannot happen as there is never a bfd with only the
  13399.      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
  13400.      matter as here were are looking only for cross-bfd sections.  */
  13401.  
  13402.   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
  13403.     for (l = already_linked_list->entry; l != NULL; l = l->next)
  13404.       if ((l->sec->flags & SEC_GROUP) == 0
  13405.           && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
  13406.         {
  13407.           if (abfd != l->sec->owner)
  13408.             sec->output_section = bfd_abs_section_ptr;
  13409.           break;
  13410.         }
  13411.  
  13412.   /* This is the first section with this name.  Record it.  */
  13413.   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  13414.     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  13415.   return sec->output_section == bfd_abs_section_ptr;
  13416. }
  13417.  
  13418. bfd_boolean
  13419. _bfd_elf_common_definition (Elf_Internal_Sym *sym)
  13420. {
  13421.   return sym->st_shndx == SHN_COMMON;
  13422. }
  13423.  
  13424. unsigned int
  13425. _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
  13426. {
  13427.   return SHN_COMMON;
  13428. }
  13429.  
  13430. asection *
  13431. _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
  13432. {
  13433.   return bfd_com_section_ptr;
  13434. }
  13435.  
  13436. bfd_vma
  13437. _bfd_elf_default_got_elt_size (bfd *abfd,
  13438.                                struct bfd_link_info *info ATTRIBUTE_UNUSED,
  13439.                                struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
  13440.                                bfd *ibfd ATTRIBUTE_UNUSED,
  13441.                                unsigned long symndx ATTRIBUTE_UNUSED)
  13442. {
  13443.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13444.   return bed->s->arch_size / 8;
  13445. }
  13446.  
  13447. /* Routines to support the creation of dynamic relocs.  */
  13448.  
  13449. /* Returns the name of the dynamic reloc section associated with SEC.  */
  13450.  
  13451. static const char *
  13452. get_dynamic_reloc_section_name (bfd *       abfd,
  13453.                                 asection *  sec,
  13454.                                 bfd_boolean is_rela)
  13455. {
  13456.   char *name;
  13457.   const char *old_name = bfd_get_section_name (NULL, sec);
  13458.   const char *prefix = is_rela ? ".rela" : ".rel";
  13459.  
  13460.   if (old_name == NULL)
  13461.     return NULL;
  13462.  
  13463.   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
  13464.   sprintf (name, "%s%s", prefix, old_name);
  13465.  
  13466.   return name;
  13467. }
  13468.  
  13469. /* Returns the dynamic reloc section associated with SEC.
  13470.    If necessary compute the name of the dynamic reloc section based
  13471.    on SEC's name (looked up in ABFD's string table) and the setting
  13472.    of IS_RELA.  */
  13473.  
  13474. asection *
  13475. _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
  13476.                                     asection *  sec,
  13477.                                     bfd_boolean is_rela)
  13478. {
  13479.   asection * reloc_sec = elf_section_data (sec)->sreloc;
  13480.  
  13481.   if (reloc_sec == NULL)
  13482.     {
  13483.       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  13484.  
  13485.       if (name != NULL)
  13486.         {
  13487.           reloc_sec = bfd_get_linker_section (abfd, name);
  13488.  
  13489.           if (reloc_sec != NULL)
  13490.             elf_section_data (sec)->sreloc = reloc_sec;
  13491.         }
  13492.     }
  13493.  
  13494.   return reloc_sec;
  13495. }
  13496.  
  13497. /* Returns the dynamic reloc section associated with SEC.  If the
  13498.    section does not exist it is created and attached to the DYNOBJ
  13499.    bfd and stored in the SRELOC field of SEC's elf_section_data
  13500.    structure.
  13501.  
  13502.    ALIGNMENT is the alignment for the newly created section and
  13503.    IS_RELA defines whether the name should be .rela.<SEC's name>
  13504.    or .rel.<SEC's name>.  The section name is looked up in the
  13505.    string table associated with ABFD.  */
  13506.  
  13507. asection *
  13508. _bfd_elf_make_dynamic_reloc_section (asection *sec,
  13509.                                      bfd *dynobj,
  13510.                                      unsigned int alignment,
  13511.                                      bfd *abfd,
  13512.                                      bfd_boolean is_rela)
  13513. {
  13514.   asection * reloc_sec = elf_section_data (sec)->sreloc;
  13515.  
  13516.   if (reloc_sec == NULL)
  13517.     {
  13518.       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  13519.  
  13520.       if (name == NULL)
  13521.         return NULL;
  13522.  
  13523.       reloc_sec = bfd_get_linker_section (dynobj, name);
  13524.  
  13525.       if (reloc_sec == NULL)
  13526.         {
  13527.           flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
  13528.                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  13529.           if ((sec->flags & SEC_ALLOC) != 0)
  13530.             flags |= SEC_ALLOC | SEC_LOAD;
  13531.  
  13532.           reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
  13533.           if (reloc_sec != NULL)
  13534.             {
  13535.               /* _bfd_elf_get_sec_type_attr chooses a section type by
  13536.                  name.  Override as it may be wrong, eg. for a user
  13537.                  section named "auto" we'll get ".relauto" which is
  13538.                  seen to be a .rela section.  */
  13539.               elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
  13540.               if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
  13541.                 reloc_sec = NULL;
  13542.             }
  13543.         }
  13544.  
  13545.       elf_section_data (sec)->sreloc = reloc_sec;
  13546.     }
  13547.  
  13548.   return reloc_sec;
  13549. }
  13550.  
  13551. /* Copy the ELF symbol type and other attributes for a linker script
  13552.    assignment from HSRC to HDEST.  Generally this should be treated as
  13553.    if we found a strong non-dynamic definition for HDEST (except that
  13554.    ld ignores multiple definition errors).  */
  13555. void
  13556. _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
  13557.                                      struct bfd_link_hash_entry *hdest,
  13558.                                      struct bfd_link_hash_entry *hsrc)
  13559. {
  13560.   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
  13561.   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
  13562.   Elf_Internal_Sym isym;
  13563.  
  13564.   ehdest->type = ehsrc->type;
  13565.   ehdest->target_internal = ehsrc->target_internal;
  13566.  
  13567.   isym.st_other = ehsrc->other;
  13568.   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
  13569. }
  13570.  
  13571. /* Append a RELA relocation REL to section S in BFD.  */
  13572.  
  13573. void
  13574. elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13575. {
  13576.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13577.   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  13578.   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
  13579.   bed->s->swap_reloca_out (abfd, rel, loc);
  13580. }
  13581.  
  13582. /* Append a REL relocation REL to section S in BFD.  */
  13583.  
  13584. void
  13585. elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13586. {
  13587.   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13588.   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
  13589.   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
  13590.   bed->s->swap_reloc_out (abfd, rel, loc);
  13591. }
  13592.