Home | History | Annotate | Line # | Download | only in bfd
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2026 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "bfdlink.h"
     24 #include "libbfd.h"
     25 #define ARCH_SIZE 0
     26 #include "elf-bfd.h"
     27 #include "safe-ctype.h"
     28 #include "libiberty.h"
     29 #include "objalloc.h"
     30 #include "plugin.h"
     31 
     32 #include <limits.h>
     33 #ifndef CHAR_BIT
     34 #define CHAR_BIT 8
     35 #endif
     36 
     37 /* This struct is used to pass information to routines called via
     38    elf_link_hash_traverse which must return failure.  */
     39 
     40 struct elf_info_failed
     41 {
     42   struct bfd_link_info *info;
     43   bool failed;
     44 };
     45 
     46 static bool _bfd_elf_fix_symbol_flags
     47   (struct elf_link_hash_entry *, struct elf_info_failed *);
     48 
     49 /* Return false if linker should avoid caching relocation information
     50    and symbol tables of input files in memory.  */
     51 
     52 static bool
     53 _bfd_elf_link_keep_memory (struct bfd_link_info *info)
     54 {
     55 #ifdef USE_MMAP
     56   /* Don't cache symbol nor relocation tables if they are mapped in.
     57      NB: Since the --no-keep-memory linker option causes:
     58 
     59      https://sourceware.org/bugzilla/show_bug.cgi?id=31458
     60 
     61      this is opt-in by each backend.  */
     62   elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
     63   if (bed != NULL && bed->use_mmap)
     64     return false;
     65 #endif
     66   bfd *abfd;
     67   bfd_size_type size;
     68 
     69   if (!info->keep_memory)
     70     return false;
     71 
     72   if (info->max_cache_size == (bfd_size_type) -1)
     73     return true;
     74 
     75   abfd = info->input_bfds;
     76   size = info->cache_size;
     77   do
     78     {
     79       if (size >= info->max_cache_size)
     80 	{
     81 	  /* Over the limit.  Reduce the memory usage.  */
     82 	  info->keep_memory = false;
     83 	  return false;
     84 	}
     85       if (!abfd)
     86 	break;
     87       size += abfd->alloc_size;
     88       abfd = abfd->link.next;
     89     }
     90   while (1);
     91 
     92   return true;
     93 }
     94 
     95 struct elf_link_hash_entry *
     96 _bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **sym_hashes,
     97 			      unsigned int symndx,
     98 			      unsigned int ext_sym_start,
     99 			      unsigned int num_sym)
    100 {
    101   if (sym_hashes == NULL
    102       /* Guard against corrupt input.  See PR 32636 for an example.  */
    103       || symndx < ext_sym_start
    104       || symndx >= num_sym)
    105     return NULL;
    106 
    107   struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
    108 
    109   /* The hash might be empty when bad_symtab.  Also see PR32641.  */
    110   if (h == NULL)
    111     return NULL;
    112 
    113   while (h->root.type == bfd_link_hash_indirect
    114 	 || h->root.type == bfd_link_hash_warning)
    115     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    116 
    117   return h;
    118 }
    119 
    120 static struct elf_link_hash_entry *
    121 get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie,
    122 			      unsigned int symndx)
    123 {
    124   if (cookie == NULL)
    125     return NULL;
    126 
    127   return _bfd_elf_get_link_hash_entry (elf_sym_hashes (cookie->abfd), symndx,
    128 				       cookie->extsymoff, cookie->num_sym);
    129 }
    130 
    131 asection *
    132 _bfd_get_local_sym_section (struct elf_reloc_cookie *cookie,
    133 			    unsigned int symndx)
    134 {
    135   if (symndx >= cookie->locsymcount)
    136     return NULL;
    137 
    138   bfd *abfd = cookie->abfd;
    139   if (elf_loc_shndx (abfd) == NULL)
    140     {
    141       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    142       Elf_Internal_Sym *locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
    143 							cookie->locsymcount, 0,
    144 							NULL, NULL, NULL);
    145       if (locsyms == NULL)
    146 	return NULL;
    147       unsigned int *loc_shndx
    148 	= bfd_alloc (abfd, cookie->locsymcount * sizeof (*loc_shndx));
    149       if (loc_shndx == NULL)
    150 	return NULL;
    151       elf_loc_shndx (abfd) = loc_shndx;
    152       for (unsigned int i = 0; i < cookie->locsymcount; i++)
    153 	{
    154 	  loc_shndx[i] = locsyms[i].st_shndx;
    155 	  if (ELF_ST_BIND (locsyms[i].st_info) != STB_LOCAL)
    156 	    loc_shndx[i] = SHN_BAD;
    157 	}
    158       free (locsyms);
    159     }
    160 
    161   return bfd_section_from_elf_index (abfd, elf_loc_shndx (abfd)[symndx]);
    162 }
    163 
    164 asection *
    165 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
    166 			     unsigned long r_symndx)
    167 {
    168   struct elf_link_hash_entry *h;
    169 
    170   h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
    171 
    172   if (h != NULL)
    173     {
    174       if (h->root.type == bfd_link_hash_defined
    175 	  || h->root.type == bfd_link_hash_defweak)
    176 	return h->root.u.def.section;
    177       else
    178 	return NULL;
    179     }
    180 
    181   return _bfd_get_local_sym_section (cookie, r_symndx);
    182 }
    183 
    184 /* Define a symbol in a dynamic linkage section.  */
    185 
    186 struct elf_link_hash_entry *
    187 _bfd_elf_define_linkage_sym (bfd *abfd,
    188 			     struct bfd_link_info *info,
    189 			     asection *sec,
    190 			     const char *name)
    191 {
    192   struct elf_link_hash_entry *h;
    193   struct bfd_link_hash_entry *bh;
    194   elf_backend_data *bed;
    195 
    196   h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
    197   if (h != NULL)
    198     {
    199       /* Zap symbol defined in an as-needed lib that wasn't linked.
    200 	 This is a symptom of a larger problem:  Absolute symbols
    201 	 defined in shared libraries can't be overridden, because we
    202 	 lose the link to the bfd which is via the symbol section.  */
    203       h->root.type = bfd_link_hash_new;
    204       bh = &h->root;
    205     }
    206   else
    207     bh = NULL;
    208 
    209   bed = get_elf_backend_data (abfd);
    210   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    211 					 sec, 0, NULL, false, bed->collect,
    212 					 &bh))
    213     return NULL;
    214   h = (struct elf_link_hash_entry *) bh;
    215   BFD_ASSERT (h != NULL);
    216   h->def_regular = 1;
    217   h->non_elf = 0;
    218   h->root.linker_def = 1;
    219   h->type = STT_OBJECT;
    220   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    221     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    222 
    223   (*bed->elf_backend_hide_symbol) (info, h, true);
    224   return h;
    225 }
    226 
    227 bool
    228 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    229 {
    230   flagword flags;
    231   asection *s;
    232   struct elf_link_hash_entry *h;
    233   elf_backend_data *bed = get_elf_backend_data (abfd);
    234   struct elf_link_hash_table *htab = elf_hash_table (info);
    235 
    236   /* This function may be called more than once.  */
    237   if (htab->sgot != NULL)
    238     return true;
    239 
    240   flags = bed->dynamic_sec_flags;
    241 
    242   s = bfd_make_section_anyway_with_flags (abfd,
    243 					  (bed->rela_plts_and_copies_p
    244 					   ? ".rela.got" : ".rel.got"),
    245 					  flags | SEC_READONLY);
    246   if (s == NULL
    247       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    248     return false;
    249   htab->srelgot = s;
    250 
    251   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    252   if (s == NULL
    253       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    254     return false;
    255   htab->sgot = s;
    256 
    257   if (bed->want_got_plt)
    258     {
    259       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    260       if (s == NULL
    261 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    262 	return false;
    263       htab->sgotplt = s;
    264     }
    265 
    266   /* The first bit of the global offset table is the header.  */
    267   s->size += bed->got_header_size;
    268 
    269   if (bed->want_got_sym)
    270     {
    271       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    272 	 (or .got.plt) section.  We don't do this in the linker script
    273 	 because we don't want to define the symbol if we are not creating
    274 	 a global offset table.  */
    275       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    276 				       "_GLOBAL_OFFSET_TABLE_");
    277       elf_hash_table (info)->hgot = h;
    278       if (h == NULL)
    279 	return false;
    280     }
    281 
    282   return true;
    283 }
    284 
    285 /* Create a strtab to hold the dynamic symbol names.  */
    287 static bool
    288 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    289 {
    290   struct elf_link_hash_table *hash_table;
    291 
    292   hash_table = elf_hash_table (info);
    293   if (hash_table->dynobj == NULL)
    294     {
    295       /* We may not set dynobj, an input file holding linker created
    296 	 dynamic sections to abfd, which may be a dynamic object with
    297 	 its own dynamic sections.  We need to find a normal input file
    298 	 to hold linker created sections if possible.  */
    299       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
    300 	{
    301 	  bfd *ibfd;
    302 	  asection *s;
    303 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
    304 	    if ((ibfd->flags
    305 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
    306 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
    307 		&& elf_object_id (ibfd) == elf_hash_table_id (hash_table)
    308 		&& !((s = ibfd->sections) != NULL
    309 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
    310 	      {
    311 		abfd = ibfd;
    312 		break;
    313 	      }
    314 	}
    315       hash_table->dynobj = abfd;
    316     }
    317 
    318   if (hash_table->dynstr == NULL)
    319     {
    320       hash_table->dynstr = _bfd_elf_strtab_init ();
    321       if (hash_table->dynstr == NULL)
    322 	return false;
    323     }
    324   return true;
    325 }
    326 
    327 /* Create some sections which will be filled in with dynamic linking
    328    information.  ABFD is an input file which requires dynamic sections
    329    to be created.  The dynamic sections take up virtual memory space
    330    when the final executable is run, so we need to create them before
    331    addresses are assigned to the output sections.  We work out the
    332    actual contents and size of these sections later.  */
    333 
    334 bool
    335 bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    336 {
    337   flagword flags;
    338   asection *s;
    339   elf_backend_data *bed;
    340   struct elf_link_hash_entry *h;
    341 
    342   if (! is_elf_hash_table (info->hash))
    343     return false;
    344 
    345   if (elf_hash_table (info)->dynamic_sections_created)
    346     return true;
    347 
    348   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    349     return false;
    350 
    351   abfd = elf_hash_table (info)->dynobj;
    352   bed = get_elf_backend_data (abfd);
    353 
    354   flags = bed->dynamic_sec_flags;
    355 
    356   /* A dynamically linked executable has a .interp section, but a
    357      shared library does not.  */
    358   if (bfd_link_executable (info) && !info->nointerp)
    359     {
    360       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    361 					      flags | SEC_READONLY);
    362       if (s == NULL)
    363 	return false;
    364       elf_hash_table (info)->interp = s;
    365     }
    366 
    367   /* Create sections to hold version informations.  These are removed
    368      if they are not needed.  */
    369   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    370 					  flags | SEC_READONLY);
    371   if (s == NULL
    372       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    373     return false;
    374 
    375   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    376 					  flags | SEC_READONLY);
    377   if (s == NULL
    378       || !bfd_set_section_alignment (s, 1))
    379     return false;
    380 
    381   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    382 					  flags | SEC_READONLY);
    383   if (s == NULL
    384       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    385     return false;
    386 
    387   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    388 					  flags | SEC_READONLY);
    389   if (s == NULL
    390       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    391     return false;
    392   elf_hash_table (info)->dynsym = s;
    393 
    394   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    395 					  flags | SEC_READONLY);
    396   if (s == NULL)
    397     return false;
    398 
    399   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    400   if (s == NULL
    401       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    402     return false;
    403   elf_hash_table (info)->dynamic = s;
    404 
    405   /* The special symbol _DYNAMIC is always set to the start of the
    406      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    407      only want to define it if we are, in fact, creating a .dynamic
    408      section.  We don't want to define it if there is no .dynamic
    409      section, since on some ELF platforms the start up code examines it
    410      to decide how to initialize the process.  */
    411   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    412   elf_hash_table (info)->hdynamic = h;
    413   if (h == NULL)
    414     return false;
    415 
    416   if (info->emit_hash)
    417     {
    418       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    419 					      flags | SEC_READONLY);
    420       if (s == NULL
    421 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    422 	return false;
    423       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    424     }
    425 
    426   if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
    427     {
    428       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    429 					      flags | SEC_READONLY);
    430       if (s == NULL
    431 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    432 	return false;
    433       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    434 	 4 32-bit words followed by variable count of 64-bit words, then
    435 	 variable count of 32-bit words.  */
    436       if (bed->s->arch_size == 64)
    437 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    438       else
    439 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    440     }
    441 
    442   if (info->enable_dt_relr)
    443     {
    444       s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
    445 					      flags | SEC_READONLY);
    446       if (s == NULL
    447 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    448 	return false;
    449       elf_hash_table (info)->srelrdyn = s;
    450     }
    451 
    452   /* Let the backend create the rest of the sections.  This lets the
    453      backend set the right flags.  The backend will normally create
    454      the .got and .plt sections.  */
    455   if (bed->elf_backend_create_dynamic_sections == NULL
    456       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    457     return false;
    458 
    459   elf_hash_table (info)->dynamic_sections_created = true;
    460 
    461   return true;
    462 }
    463 
    464 /* Create dynamic sections when linking against a dynamic object.  */
    465 
    466 bool
    467 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    468 {
    469   flagword flags, pltflags;
    470   struct elf_link_hash_entry *h;
    471   asection *s;
    472   elf_backend_data *bed = get_elf_backend_data (abfd);
    473   struct elf_link_hash_table *htab = elf_hash_table (info);
    474 
    475   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    476      .rel[a].bss sections.  */
    477   flags = bed->dynamic_sec_flags;
    478 
    479   pltflags = flags;
    480   if (bed->plt_not_loaded)
    481     /* We do not clear SEC_ALLOC here because we still want the OS to
    482        allocate space for the section; it's just that there's nothing
    483        to read in from the object file.  */
    484     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    485   else
    486     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    487   if (bed->plt_readonly)
    488     pltflags |= SEC_READONLY;
    489 
    490   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    491   if (s == NULL
    492       || !bfd_set_section_alignment (s, bed->plt_alignment))
    493     return false;
    494   htab->splt = s;
    495 
    496   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    497      .plt section.  */
    498   if (bed->want_plt_sym)
    499     {
    500       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    501 				       "_PROCEDURE_LINKAGE_TABLE_");
    502       elf_hash_table (info)->hplt = h;
    503       if (h == NULL)
    504 	return false;
    505     }
    506 
    507   s = bfd_make_section_anyway_with_flags (abfd,
    508 					  (bed->rela_plts_and_copies_p
    509 					   ? ".rela.plt" : ".rel.plt"),
    510 					  flags | SEC_READONLY);
    511   if (s == NULL
    512       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    513     return false;
    514   htab->srelplt = s;
    515 
    516   if (! _bfd_elf_create_got_section (abfd, info))
    517     return false;
    518 
    519   if (bed->want_dynbss)
    520     {
    521       /* The .dynbss section is a place to put symbols which are defined
    522 	 by dynamic objects, are referenced by regular objects, and are
    523 	 not functions.  We must allocate space for them in the process
    524 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    525 	 initialize them at run time.  The linker script puts the .dynbss
    526 	 section into the .bss section of the final image.  */
    527       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    528 					      SEC_ALLOC | SEC_LINKER_CREATED);
    529       if (s == NULL)
    530 	return false;
    531       htab->sdynbss = s;
    532 
    533       if (bed->want_dynrelro)
    534 	{
    535 	  /* Similarly, but for symbols that were originally in read-only
    536 	     sections.  This section doesn't really need to have contents,
    537 	     but make it like other .data.rel.ro sections.  */
    538 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
    539 						  flags);
    540 	  if (s == NULL)
    541 	    return false;
    542 	  htab->sdynrelro = s;
    543 	}
    544 
    545       /* The .rel[a].bss section holds copy relocs.  This section is not
    546 	 normally needed.  We need to create it here, though, so that the
    547 	 linker will map it to an output section.  We can't just create it
    548 	 only if we need it, because we will not know whether we need it
    549 	 until we have seen all the input files, and the first time the
    550 	 main linker code calls BFD after examining all the input files
    551 	 (size_dynamic_sections) the input sections have already been
    552 	 mapped to the output sections.  If the section turns out not to
    553 	 be needed, we can discard it later.  We will never need this
    554 	 section when generating a shared object, since they do not use
    555 	 copy relocs.  */
    556       if (bfd_link_executable (info))
    557 	{
    558 	  s = bfd_make_section_anyway_with_flags (abfd,
    559 						  (bed->rela_plts_and_copies_p
    560 						   ? ".rela.bss" : ".rel.bss"),
    561 						  flags | SEC_READONLY);
    562 	  if (s == NULL
    563 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
    564 	    return false;
    565 	  htab->srelbss = s;
    566 
    567 	  if (bed->want_dynrelro)
    568 	    {
    569 	      s = (bfd_make_section_anyway_with_flags
    570 		   (abfd, (bed->rela_plts_and_copies_p
    571 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
    572 		    flags | SEC_READONLY));
    573 	      if (s == NULL
    574 		  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    575 		return false;
    576 	      htab->sreldynrelro = s;
    577 	    }
    578 	}
    579     }
    580 
    581   return true;
    582 }
    583 
    584 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    586    read the input files, since we need to have a list of all of them
    587    before we can determine the final sizes of the output sections.
    588    Note that we may actually call this function even though we are not
    589    going to output any dynamic symbols; in some cases we know that a
    590    symbol should be in the dynamic symbol table, but only if there is
    591    one.  */
    592 
    593 bool
    594 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    595 				    struct elf_link_hash_entry *h)
    596 {
    597   if (h->dynindx == -1)
    598     {
    599       struct elf_strtab_hash *dynstr;
    600       const char *p;
    601       const char *name;
    602       size_t indx;
    603 
    604       if (h->root.type == bfd_link_hash_defined
    605 	  || h->root.type == bfd_link_hash_defweak)
    606 	{
    607 	  /* An IR symbol should not be made dynamic.  */
    608 	  if (h->root.u.def.section != NULL
    609 	      && h->root.u.def.section->owner != NULL
    610 	      && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
    611 	    return true;
    612 	}
    613 
    614       /* XXX: The ABI draft says the linker must turn hidden and
    615 	 internal symbols into STB_LOCAL symbols when producing the
    616 	 DSO. However, if ld.so honors st_other in the dynamic table,
    617 	 this would not be necessary.  */
    618       switch (ELF_ST_VISIBILITY (h->other))
    619 	{
    620 	case STV_INTERNAL:
    621 	case STV_HIDDEN:
    622 	  if (h->root.type != bfd_link_hash_undefined
    623 	      && h->root.type != bfd_link_hash_undefweak)
    624 	    {
    625 	      h->forced_local = 1;
    626 	      return true;
    627 	    }
    628 
    629 	default:
    630 	  break;
    631 	}
    632 
    633       h->dynindx = elf_hash_table (info)->dynsymcount;
    634       if (h->forced_local)
    635 	elf_hash_table (info)->has_local_dynsyms = true;
    636       ++elf_hash_table (info)->dynsymcount;
    637 
    638       dynstr = elf_hash_table (info)->dynstr;
    639       if (dynstr == NULL)
    640 	{
    641 	  /* Create a strtab to hold the dynamic symbol names.  */
    642 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    643 	  if (dynstr == NULL)
    644 	    return false;
    645 	}
    646 
    647       char *unversioned_name = NULL;
    648 
    649       /* We don't put any version information in the dynamic string
    650 	 table.  */
    651       name = h->root.root.string;
    652       p = strchr (name, ELF_VER_CHR);
    653       if (p != NULL)
    654 	{
    655 	  unversioned_name = bfd_malloc (p - name + 1);
    656 	  memcpy (unversioned_name, name, p - name);
    657 	  unversioned_name[p - name] = 0;
    658 	  name = unversioned_name;
    659 	}
    660 
    661       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    662 
    663       if (p != NULL)
    664 	free (unversioned_name);
    665 
    666       if (indx == (size_t) -1)
    667 	return false;
    668       h->dynstr_index = indx;
    669     }
    670 
    671   return true;
    672 }
    673 
    674 /* Mark a symbol dynamic.  */
    676 
    677 static void
    678 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    679 				  struct elf_link_hash_entry *h,
    680 				  Elf_Internal_Sym *sym)
    681 {
    682   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    683 
    684   /* It may be called more than once on the same H.  */
    685   if(h->dynamic || bfd_link_relocatable (info))
    686     return;
    687 
    688   if ((info->dynamic_data
    689        && (h->type == STT_OBJECT
    690 	   || h->type == STT_COMMON
    691 	   || (sym != NULL
    692 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
    693 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
    694       || (d != NULL
    695 	  && h->non_elf
    696 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    697     {
    698       h->dynamic = 1;
    699       /* NB: If a symbol is made dynamic by --dynamic-list, it has
    700 	 non-IR reference.  */
    701       h->root.non_ir_ref_dynamic = 1;
    702     }
    703 }
    704 
    705 /* Record an assignment to a symbol made by a linker script.  We need
    706    this in case some dynamic object refers to this symbol.  */
    707 
    708 bool
    709 bfd_elf_record_link_assignment (bfd *output_bfd,
    710 				struct bfd_link_info *info,
    711 				const char *name,
    712 				bool provide,
    713 				bool hidden)
    714 {
    715   struct elf_link_hash_entry *h, *hv;
    716   struct elf_link_hash_table *htab;
    717   elf_backend_data *bed;
    718 
    719   if (!is_elf_hash_table (info->hash))
    720     return true;
    721 
    722   htab = elf_hash_table (info);
    723   h = elf_link_hash_lookup (htab, name, !provide, true, false);
    724   if (h == NULL)
    725     return provide;
    726 
    727   if (h->root.type == bfd_link_hash_warning)
    728     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    729 
    730   if (h->versioned == unknown)
    731     {
    732       /* Set versioned if symbol version is unknown.  */
    733       const char *version = strrchr (name, ELF_VER_CHR);
    734       if (version)
    735 	{
    736 	  if (version > name && version[-1] != ELF_VER_CHR)
    737 	    h->versioned = versioned_hidden;
    738 	  else
    739 	    h->versioned = versioned;
    740 	}
    741     }
    742 
    743   /* Symbols defined in a linker script but not referenced anywhere
    744      else will have non_elf set.  */
    745   if (h->non_elf)
    746     {
    747       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    748       h->non_elf = 0;
    749     }
    750 
    751   switch (h->root.type)
    752     {
    753     case bfd_link_hash_defined:
    754     case bfd_link_hash_defweak:
    755     case bfd_link_hash_common:
    756       break;
    757     case bfd_link_hash_undefweak:
    758     case bfd_link_hash_undefined:
    759       /* Since we're defining the symbol, don't let it seem to have not
    760 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    761 	 may depend on this.  */
    762       h->root.type = bfd_link_hash_new;
    763       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    764 	bfd_link_repair_undef_list (&htab->root);
    765       break;
    766     case bfd_link_hash_new:
    767       break;
    768     case bfd_link_hash_indirect:
    769       /* We had a versioned symbol in a dynamic library.  We make the
    770 	 the versioned symbol point to this one.  */
    771       bed = get_elf_backend_data (output_bfd);
    772       hv = h;
    773       while (hv->root.type == bfd_link_hash_indirect
    774 	     || hv->root.type == bfd_link_hash_warning)
    775 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    776       /* We don't need to update h->root.u since linker will set them
    777 	 later.  */
    778       h->root.type = bfd_link_hash_undefined;
    779       hv->root.type = bfd_link_hash_indirect;
    780       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    781       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    782       break;
    783     default:
    784       BFD_FAIL ();
    785       return false;
    786     }
    787 
    788   /* If this symbol is being provided by the linker script, and it is
    789      currently defined by a dynamic object, but not by a regular
    790      object, then mark it as undefined so that the generic linker will
    791      force the correct value.  */
    792   if (provide
    793       && h->def_dynamic
    794       && !h->def_regular)
    795     h->root.type = bfd_link_hash_undefined;
    796 
    797   /* If this symbol is currently defined by a dynamic object, but not
    798      by a regular object, then clear out any version information because
    799      the symbol will not be associated with the dynamic object any
    800      more.  */
    801   if (h->def_dynamic && !h->def_regular)
    802     h->verinfo.verdef = NULL;
    803 
    804   /* Make sure this symbol is not garbage collected.  */
    805   h->mark = 1;
    806 
    807   h->def_regular = 1;
    808 
    809   if (hidden)
    810     {
    811       bed = get_elf_backend_data (output_bfd);
    812       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    813 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    814       (*bed->elf_backend_hide_symbol) (info, h, true);
    815     }
    816 
    817   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    818      and executables.  */
    819   if (!bfd_link_relocatable (info)
    820       && h->dynindx != -1
    821       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    822 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    823     h->forced_local = 1;
    824 
    825   if ((h->def_dynamic
    826        || h->ref_dynamic
    827        || bfd_link_dll (info))
    828       && !h->forced_local
    829       && h->dynindx == -1)
    830     {
    831       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    832 	return false;
    833 
    834       /* If this is a weak defined symbol, and we know a corresponding
    835 	 real symbol from the same dynamic object, make sure the real
    836 	 symbol is also made into a dynamic symbol.  */
    837       if (h->is_weakalias)
    838 	{
    839 	  struct elf_link_hash_entry *def = weakdef (h);
    840 
    841 	  if (def->dynindx == -1
    842 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
    843 	    return false;
    844 	}
    845     }
    846 
    847   return true;
    848 }
    849 
    850 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    851    success, and 2 on a failure caused by attempting to record a symbol
    852    in a discarded section, eg. a discarded link-once section symbol.  */
    853 
    854 int
    855 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    856 					  bfd *input_bfd,
    857 					  long input_indx)
    858 {
    859   size_t amt;
    860   struct elf_link_local_dynamic_entry *entry;
    861   struct elf_link_hash_table *eht;
    862   struct elf_strtab_hash *dynstr;
    863   size_t dynstr_index;
    864   char *name;
    865   Elf_External_Sym_Shndx eshndx;
    866   char esym[sizeof (Elf64_External_Sym)];
    867 
    868   if (! is_elf_hash_table (info->hash))
    869     return 0;
    870 
    871   /* See if the entry exists already.  */
    872   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    873     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    874       return 1;
    875 
    876   amt = sizeof (*entry);
    877   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    878   if (entry == NULL)
    879     return 0;
    880 
    881   /* Go find the symbol, so that we can find it's name.  */
    882   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    883 			     1, input_indx, &entry->isym, esym, &eshndx))
    884     {
    885       bfd_release (input_bfd, entry);
    886       return 0;
    887     }
    888 
    889   if (entry->isym.st_shndx != SHN_UNDEF
    890       && entry->isym.st_shndx < SHN_LORESERVE)
    891     {
    892       asection *s;
    893 
    894       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    895       if (s == NULL || bfd_is_abs_section (s->output_section))
    896 	{
    897 	  /* We can still bfd_release here as nothing has done another
    898 	     bfd_alloc.  We can't do this later in this function.  */
    899 	  bfd_release (input_bfd, entry);
    900 	  return 2;
    901 	}
    902     }
    903 
    904   name = (bfd_elf_string_from_elf_section
    905 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    906 	   entry->isym.st_name));
    907 
    908   dynstr = elf_hash_table (info)->dynstr;
    909   if (dynstr == NULL)
    910     {
    911       /* Create a strtab to hold the dynamic symbol names.  */
    912       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    913       if (dynstr == NULL)
    914 	return 0;
    915     }
    916 
    917   dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
    918   if (dynstr_index == (size_t) -1)
    919     return 0;
    920   entry->isym.st_name = dynstr_index;
    921 
    922   eht = elf_hash_table (info);
    923 
    924   entry->next = eht->dynlocal;
    925   eht->dynlocal = entry;
    926   entry->input_bfd = input_bfd;
    927   entry->input_indx = input_indx;
    928   eht->dynsymcount++;
    929 
    930   /* Whatever binding the symbol had before, it's now local.  */
    931   entry->isym.st_info
    932     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    933 
    934   /* The dynindx will be set at the end of size_dynamic_sections.  */
    935 
    936   return 1;
    937 }
    938 
    939 /* Return the dynindex of a local dynamic symbol.  */
    940 
    941 long
    942 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    943 				    bfd *input_bfd,
    944 				    long input_indx)
    945 {
    946   struct elf_link_local_dynamic_entry *e;
    947 
    948   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    949     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    950       return e->dynindx;
    951   return -1;
    952 }
    953 
    954 /* This function is used to renumber the dynamic symbols, if some of
    955    them are removed because they are marked as local.  This is called
    956    via elf_link_hash_traverse.  */
    957 
    958 static bool
    959 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    960 				      void *data)
    961 {
    962   size_t *count = (size_t *) data;
    963 
    964   if (h->forced_local)
    965     return true;
    966 
    967   if (h->dynindx != -1)
    968     h->dynindx = ++(*count);
    969 
    970   return true;
    971 }
    972 
    973 
    974 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    975    STB_LOCAL binding.  */
    976 
    977 static bool
    978 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    979 					    void *data)
    980 {
    981   size_t *count = (size_t *) data;
    982 
    983   if (!h->forced_local)
    984     return true;
    985 
    986   if (h->dynindx != -1)
    987     h->dynindx = ++(*count);
    988 
    989   return true;
    990 }
    991 
    992 /* Return true if the dynamic symbol for a given section should be
    993    omitted when creating a shared library.  */
    994 bool
    995 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
    996 				      struct bfd_link_info *info,
    997 				      asection *p)
    998 {
    999   struct elf_link_hash_table *htab;
   1000   asection *ip;
   1001 
   1002   switch (elf_section_data (p)->this_hdr.sh_type)
   1003     {
   1004     case SHT_PROGBITS:
   1005     case SHT_NOBITS:
   1006       /* If sh_type is yet undecided, assume it could be
   1007 	 SHT_PROGBITS/SHT_NOBITS.  */
   1008     case SHT_NULL:
   1009       htab = elf_hash_table (info);
   1010       if (htab->text_index_section != NULL)
   1011 	return p != htab->text_index_section && p != htab->data_index_section;
   1012 
   1013       return (htab->dynobj != NULL
   1014 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
   1015 	      && ip->output_section == p);
   1016 
   1017       /* There shouldn't be section relative relocations
   1018 	 against any other section.  */
   1019     default:
   1020       return true;
   1021     }
   1022 }
   1023 
   1024 bool
   1025 _bfd_elf_omit_section_dynsym_all
   1026     (bfd *output_bfd ATTRIBUTE_UNUSED,
   1027      struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1028      asection *p ATTRIBUTE_UNUSED)
   1029 {
   1030   return true;
   1031 }
   1032 
   1033 /* Assign dynsym indices.  In a shared library we generate a section
   1034    symbol for each output section, which come first.  Next come symbols
   1035    which have been forced to local binding.  Then all of the back-end
   1036    allocated local dynamic syms, followed by the rest of the global
   1037    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
   1038    (This prevents the early call before elf_backend_init_index_section
   1039    and strip_excluded_output_sections setting dynindx for sections
   1040    that are stripped.)  */
   1041 
   1042 static unsigned long
   1043 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
   1044 				struct bfd_link_info *info,
   1045 				unsigned long *section_sym_count)
   1046 {
   1047   unsigned long dynsymcount = 0;
   1048   bool do_sec = section_sym_count != NULL;
   1049 
   1050   if (bfd_link_pic (info)
   1051       || elf_hash_table (info)->is_relocatable_executable)
   1052     {
   1053       elf_backend_data *bed = get_elf_backend_data (output_bfd);
   1054       asection *p;
   1055       for (p = output_bfd->sections; p ; p = p->next)
   1056 	if ((p->flags & SEC_EXCLUDE) == 0
   1057 	    && (p->flags & SEC_ALLOC) != 0
   1058 	    && elf_hash_table (info)->dynamic_relocs
   1059 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   1060 	  {
   1061 	    ++dynsymcount;
   1062 	    if (do_sec)
   1063 	      elf_section_data (p)->dynindx = dynsymcount;
   1064 	  }
   1065 	else if (do_sec)
   1066 	  elf_section_data (p)->dynindx = 0;
   1067     }
   1068   if (do_sec)
   1069     *section_sym_count = dynsymcount;
   1070 
   1071   if (elf_hash_table (info)->has_local_dynsyms)
   1072     elf_link_hash_traverse (elf_hash_table (info),
   1073 			    elf_link_renumber_local_hash_table_dynsyms,
   1074 			    &dynsymcount);
   1075 
   1076   if (elf_hash_table (info)->dynlocal)
   1077     {
   1078       struct elf_link_local_dynamic_entry *p;
   1079       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
   1080 	p->dynindx = ++dynsymcount;
   1081     }
   1082   elf_hash_table (info)->local_dynsymcount = dynsymcount;
   1083 
   1084   elf_link_hash_traverse (elf_hash_table (info),
   1085 			  elf_link_renumber_hash_table_dynsyms,
   1086 			  &dynsymcount);
   1087 
   1088   /* There is an unused NULL entry at the head of the table which we
   1089      must account for in our count even if the table is empty since it
   1090      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
   1091      .dynamic section.  */
   1092   dynsymcount++;
   1093 
   1094   elf_hash_table (info)->dynsymcount = dynsymcount;
   1095   return dynsymcount;
   1096 }
   1097 
   1098 /* Merge st_other field.  */
   1099 
   1100 static void
   1101 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
   1102 		    unsigned int st_other, asection *sec,
   1103 		    bool definition, bool dynamic)
   1104 {
   1105   elf_backend_data *bed = get_elf_backend_data (abfd);
   1106 
   1107   /* If st_other has a processor-specific meaning, specific
   1108      code might be needed here.  */
   1109   if (bed->elf_backend_merge_symbol_attribute)
   1110     (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
   1111 						dynamic);
   1112 
   1113   if (!dynamic)
   1114     {
   1115       unsigned symvis = ELF_ST_VISIBILITY (st_other);
   1116       unsigned hvis = ELF_ST_VISIBILITY (h->other);
   1117 
   1118       /* Keep the most constraining visibility.  Leave the remainder
   1119 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
   1120       if (symvis - 1 < hvis - 1)
   1121 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
   1122     }
   1123   else if (definition
   1124 	   && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
   1125 	   && (sec->flags & SEC_READONLY) == 0)
   1126     h->protected_def = 1;
   1127 }
   1128 
   1129 /* This function is called when we want to merge a new symbol with an
   1130    existing symbol.  It handles the various cases which arise when we
   1131    find a definition in a dynamic object, or when there is already a
   1132    definition in a dynamic object.  The new symbol is described by
   1133    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
   1134    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
   1135    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
   1136    of an old common symbol.  We set OVERRIDE if the old symbol is
   1137    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
   1138    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
   1139    to change.  By OK to change, we mean that we shouldn't warn if the
   1140    type or size does change.  */
   1141 
   1142 static bool
   1143 _bfd_elf_merge_symbol (bfd *abfd,
   1144 		       struct bfd_link_info *info,
   1145 		       const char *name,
   1146 		       Elf_Internal_Sym *sym,
   1147 		       asection **psec,
   1148 		       bfd_vma *pvalue,
   1149 		       struct elf_link_hash_entry **sym_hash,
   1150 		       bfd **poldbfd,
   1151 		       bool *pold_weak,
   1152 		       unsigned int *pold_alignment,
   1153 		       bool *skip,
   1154 		       bfd **override,
   1155 		       bool *type_change_ok,
   1156 		       bool *size_change_ok,
   1157 		       bool *matched)
   1158 {
   1159   asection *sec, *oldsec;
   1160   struct elf_link_hash_entry *h;
   1161   struct elf_link_hash_entry *hi;
   1162   struct elf_link_hash_entry *flip;
   1163   int bind;
   1164   bfd *oldbfd;
   1165   bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   1166   bool newweak, oldweak, newfunc, oldfunc;
   1167   elf_backend_data *bed;
   1168   const char *new_version;
   1169   bool default_sym = *matched;
   1170   struct elf_link_hash_table *htab;
   1171 
   1172   *skip = false;
   1173   *override = NULL;
   1174 
   1175   sec = *psec;
   1176   bind = ELF_ST_BIND (sym->st_info);
   1177 
   1178   if (! bfd_is_und_section (sec))
   1179     h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
   1180   else
   1181     h = ((struct elf_link_hash_entry *)
   1182 	 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
   1183   if (h == NULL)
   1184     return false;
   1185   *sym_hash = h;
   1186 
   1187   bed = get_elf_backend_data (abfd);
   1188 
   1189   htab = elf_hash_table (info);
   1190 
   1191   /* NEW_VERSION is the symbol version of the new symbol.  */
   1192   if (h->versioned != unversioned)
   1193     {
   1194       /* Symbol version is unknown or versioned.  */
   1195       new_version = strrchr (name, ELF_VER_CHR);
   1196       if (new_version)
   1197 	{
   1198 	  if (h->versioned == unknown)
   1199 	    {
   1200 	      /* The base symbol has an empty version.  */
   1201 	      if (new_version[1] == '\0')
   1202 		{
   1203 		  htab->has_base_symbols = true;
   1204 		  h->base_symbol = 1;
   1205 		}
   1206 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1207 		h->versioned = versioned_hidden;
   1208 	      else
   1209 		h->versioned = versioned;
   1210 	    }
   1211 	  new_version += 1;
   1212 	  if (new_version[0] == '\0')
   1213 	    new_version = NULL;
   1214 	}
   1215       else
   1216 	h->versioned = unversioned;
   1217     }
   1218   else
   1219     new_version = NULL;
   1220 
   1221   /* For merging, we only care about real symbols.  But we need to make
   1222      sure that indirect symbol dynamic flags are updated.  */
   1223   hi = h;
   1224   while (h->root.type == bfd_link_hash_indirect
   1225 	 || h->root.type == bfd_link_hash_warning)
   1226     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1227 
   1228   if (!*matched)
   1229     {
   1230       if (hi == h || h->root.type == bfd_link_hash_new)
   1231 	*matched = true;
   1232       else
   1233 	{
   1234 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1235 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1236 	     true if the new symbol is only visible to the symbol with
   1237 	     the same symbol version.  */
   1238 	  bool old_hidden = h->versioned == versioned_hidden;
   1239 	  bool new_hidden = hi->versioned == versioned_hidden;
   1240 	  if (!old_hidden && !new_hidden)
   1241 	    /* The new symbol matches the existing symbol if both
   1242 	       aren't hidden.  */
   1243 	    *matched = true;
   1244 	  else
   1245 	    {
   1246 	      /* OLD_VERSION is the symbol version of the existing
   1247 		 symbol. */
   1248 	      const char *old_version;
   1249 
   1250 	      if (h->versioned >= versioned)
   1251 		old_version = strrchr (h->root.root.string,
   1252 				       ELF_VER_CHR) + 1;
   1253 	      else
   1254 		 old_version = NULL;
   1255 
   1256 	      /* The new symbol matches the existing symbol if they
   1257 		 have the same symbol version.  */
   1258 	      *matched = (old_version == new_version
   1259 			  || (old_version != NULL
   1260 			      && new_version != NULL
   1261 			      && strcmp (old_version, new_version) == 0));
   1262 	    }
   1263 	}
   1264     }
   1265 
   1266   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1267      existing symbol.  */
   1268 
   1269   oldbfd = NULL;
   1270   oldsec = NULL;
   1271   switch (h->root.type)
   1272     {
   1273     default:
   1274       break;
   1275 
   1276     case bfd_link_hash_undefined:
   1277     case bfd_link_hash_undefweak:
   1278       oldbfd = h->root.u.undef.abfd;
   1279       break;
   1280 
   1281     case bfd_link_hash_defined:
   1282     case bfd_link_hash_defweak:
   1283       oldbfd = h->root.u.def.section->owner;
   1284       oldsec = h->root.u.def.section;
   1285       break;
   1286 
   1287     case bfd_link_hash_common:
   1288       oldbfd = h->root.u.c.p->section->owner;
   1289       oldsec = h->root.u.c.p->section;
   1290       if (pold_alignment)
   1291 	*pold_alignment = h->root.u.c.p->alignment_power;
   1292       break;
   1293     }
   1294   if (poldbfd && *poldbfd == NULL)
   1295     *poldbfd = oldbfd;
   1296 
   1297   /* Differentiate strong and weak symbols.  */
   1298   newweak = bind == STB_WEAK;
   1299   oldweak = (h->root.type == bfd_link_hash_defweak
   1300 	     || h->root.type == bfd_link_hash_undefweak);
   1301   if (pold_weak)
   1302     *pold_weak = oldweak;
   1303 
   1304   /* We have to check it for every instance since the first few may be
   1305      references and not all compilers emit symbol type for undefined
   1306      symbols.  */
   1307   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1308 
   1309   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1310      respectively, is from a dynamic object.  */
   1311 
   1312   newdyn = (abfd->flags & DYNAMIC) != 0;
   1313 
   1314   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1315      syms and defined syms in dynamic libraries respectively.
   1316      ref_dynamic on the other hand can be set for a symbol defined in
   1317      a dynamic library, and def_dynamic may not be set;  When the
   1318      definition in a dynamic lib is overridden by a definition in the
   1319      executable use of the symbol in the dynamic lib becomes a
   1320      reference to the executable symbol.  */
   1321   if (newdyn)
   1322     {
   1323       if (bfd_is_und_section (sec))
   1324 	{
   1325 	  if (bind != STB_WEAK)
   1326 	    {
   1327 	      h->ref_dynamic_nonweak = 1;
   1328 	      hi->ref_dynamic_nonweak = 1;
   1329 	    }
   1330 	}
   1331       else
   1332 	{
   1333 	  /* Update the existing symbol only if they match. */
   1334 	  if (*matched)
   1335 	    h->dynamic_def = 1;
   1336 	  hi->dynamic_def = 1;
   1337 	}
   1338     }
   1339 
   1340   /* If we just created the symbol, mark it as being an ELF symbol.
   1341      Other than that, there is nothing to do--there is no merge issue
   1342      with a newly defined symbol--so we just return.  */
   1343 
   1344   if (h->root.type == bfd_link_hash_new)
   1345     {
   1346       h->non_elf = 0;
   1347       return true;
   1348     }
   1349 
   1350   /* In cases involving weak versioned symbols, we may wind up trying
   1351      to merge a symbol with itself.  Catch that here, to avoid the
   1352      confusion that results if we try to override a symbol with
   1353      itself.  The additional tests catch cases like
   1354      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1355      dynamic object, which we do want to handle here.  */
   1356   if (abfd == oldbfd
   1357       && (newweak || oldweak)
   1358       && ((abfd->flags & DYNAMIC) == 0
   1359 	  || !h->def_regular))
   1360     return true;
   1361 
   1362   olddyn = false;
   1363   if (oldbfd != NULL)
   1364     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1365   else if (oldsec != NULL)
   1366     {
   1367       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1368 	 indices used by MIPS ELF.  */
   1369       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1370     }
   1371 
   1372   /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
   1373   if (!htab->handling_dt_needed
   1374       && oldbfd != NULL
   1375       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
   1376     {
   1377       if (newdyn != olddyn)
   1378 	{
   1379 	  /* Handle a case where plugin_notice won't be called and thus
   1380 	     won't set the non_ir_ref flags on the first pass over
   1381 	     symbols.  */
   1382 	  h->root.non_ir_ref_dynamic = true;
   1383 	  hi->root.non_ir_ref_dynamic = true;
   1384 	}
   1385       else if ((oldbfd->flags & BFD_PLUGIN) != 0
   1386 	       && hi->root.type == bfd_link_hash_indirect)
   1387 	{
   1388 	  /* Change indirect symbol from IR to undefined.  */
   1389 	  hi->root.type = bfd_link_hash_undefined;
   1390 	  hi->root.u.undef.abfd = oldbfd;
   1391 	}
   1392     }
   1393 
   1394   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1395      respectively, appear to be a definition rather than reference.  */
   1396 
   1397   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1398 
   1399   olddef = (h->root.type != bfd_link_hash_undefined
   1400 	    && h->root.type != bfd_link_hash_undefweak
   1401 	    && h->root.type != bfd_link_hash_common);
   1402 
   1403   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1404      respectively, appear to be a function.  */
   1405 
   1406   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1407 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1408 
   1409   oldfunc = (h->type != STT_NOTYPE
   1410 	     && bed->is_function_type (h->type));
   1411 
   1412   if (!(newfunc && oldfunc)
   1413       && ELF_ST_TYPE (sym->st_info) != h->type
   1414       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1415       && h->type != STT_NOTYPE
   1416       && (newdef || bfd_is_com_section (sec))
   1417       && (olddef || h->root.type == bfd_link_hash_common))
   1418     {
   1419       /* If creating a default indirect symbol ("foo" or "foo@") from
   1420 	 a dynamic versioned definition ("foo@@") skip doing so if
   1421 	 there is an existing regular definition with a different
   1422 	 type.  We don't want, for example, a "time" variable in the
   1423 	 executable overriding a "time" function in a shared library.  */
   1424       if (newdyn
   1425 	  && !olddyn)
   1426 	{
   1427 	  *skip = true;
   1428 	  return true;
   1429 	}
   1430 
   1431       /* When adding a symbol from a regular object file after we have
   1432 	 created indirect symbols, undo the indirection and any
   1433 	 dynamic state.  */
   1434       if (hi != h
   1435 	  && !newdyn
   1436 	  && olddyn)
   1437 	{
   1438 	  h = hi;
   1439 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1440 	  h->forced_local = 0;
   1441 	  h->ref_dynamic = 0;
   1442 	  h->def_dynamic = 0;
   1443 	  h->dynamic_def = 0;
   1444 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1445 	    {
   1446 	      h->root.type = bfd_link_hash_undefined;
   1447 	      h->root.u.undef.abfd = abfd;
   1448 	    }
   1449 	  else
   1450 	    {
   1451 	      h->root.type = bfd_link_hash_new;
   1452 	      h->root.u.undef.abfd = NULL;
   1453 	    }
   1454 	  return true;
   1455 	}
   1456     }
   1457 
   1458   /* Check TLS symbols.  We don't check undefined symbols introduced
   1459      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1460      check symbols from plugins because they also have no type.  */
   1461   if (oldbfd != NULL
   1462       && (oldbfd->flags & BFD_PLUGIN) == 0
   1463       && (abfd->flags & BFD_PLUGIN) == 0
   1464       && ELF_ST_TYPE (sym->st_info) != h->type
   1465       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1466     {
   1467       bfd *ntbfd, *tbfd;
   1468       bool ntdef, tdef;
   1469       asection *ntsec, *tsec;
   1470 
   1471       if (h->type == STT_TLS)
   1472 	{
   1473 	  ntbfd = abfd;
   1474 	  ntsec = sec;
   1475 	  ntdef = newdef;
   1476 	  tbfd = oldbfd;
   1477 	  tsec = oldsec;
   1478 	  tdef = olddef;
   1479 	}
   1480       else
   1481 	{
   1482 	  ntbfd = oldbfd;
   1483 	  ntsec = oldsec;
   1484 	  ntdef = olddef;
   1485 	  tbfd = abfd;
   1486 	  tsec = sec;
   1487 	  tdef = newdef;
   1488 	}
   1489 
   1490       if (tdef && ntdef)
   1491 	_bfd_error_handler
   1492 	  /* xgettext:c-format */
   1493 	  (_("%s: TLS definition in %pB section %pA "
   1494 	     "mismatches non-TLS definition in %pB section %pA"),
   1495 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
   1496       else if (!tdef && !ntdef)
   1497 	_bfd_error_handler
   1498 	  /* xgettext:c-format */
   1499 	  (_("%s: TLS reference in %pB "
   1500 	     "mismatches non-TLS reference in %pB"),
   1501 	   h->root.root.string, tbfd, ntbfd);
   1502       else if (tdef)
   1503 	_bfd_error_handler
   1504 	  /* xgettext:c-format */
   1505 	  (_("%s: TLS definition in %pB section %pA "
   1506 	     "mismatches non-TLS reference in %pB"),
   1507 	   h->root.root.string, tbfd, tsec, ntbfd);
   1508       else
   1509 	_bfd_error_handler
   1510 	  /* xgettext:c-format */
   1511 	  (_("%s: TLS reference in %pB "
   1512 	     "mismatches non-TLS definition in %pB section %pA"),
   1513 	   h->root.root.string, tbfd, ntbfd, ntsec);
   1514 
   1515       bfd_set_error (bfd_error_bad_value);
   1516       return false;
   1517     }
   1518 
   1519   /* If the old symbol has non-default visibility, we ignore the new
   1520      definition from a dynamic object.  */
   1521   if (newdyn
   1522       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1523       && !bfd_is_und_section (sec))
   1524     {
   1525       *skip = true;
   1526       /* Make sure this symbol is dynamic.  */
   1527       h->ref_dynamic = 1;
   1528       hi->ref_dynamic = 1;
   1529       /* A protected symbol has external availability. Make sure it is
   1530 	 recorded as dynamic.
   1531 
   1532 	 FIXME: Should we check type and size for protected symbol?  */
   1533       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1534 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1535       else
   1536 	return true;
   1537     }
   1538   else if (!newdyn
   1539 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1540 	   && h->def_dynamic)
   1541     {
   1542       /* If the new symbol with non-default visibility comes from a
   1543 	 relocatable file and the old definition comes from a dynamic
   1544 	 object, we remove the old definition.  */
   1545       if (hi->root.type == bfd_link_hash_indirect)
   1546 	{
   1547 	  /* Handle the case where the old dynamic definition is
   1548 	     default versioned.  We need to copy the symbol info from
   1549 	     the symbol with default version to the normal one if it
   1550 	     was referenced before.  */
   1551 	  if (h->ref_regular)
   1552 	    {
   1553 	      hi->root.type = h->root.type;
   1554 	      h->root.type = bfd_link_hash_indirect;
   1555 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1556 
   1557 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1558 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1559 		{
   1560 		  /* If the new symbol is hidden or internal, completely undo
   1561 		     any dynamic link state.  */
   1562 		  (*bed->elf_backend_hide_symbol) (info, h, true);
   1563 		  h->forced_local = 0;
   1564 		  h->ref_dynamic = 0;
   1565 		}
   1566 	      else
   1567 		h->ref_dynamic = 1;
   1568 
   1569 	      h->def_dynamic = 0;
   1570 	      /* FIXME: Should we check type and size for protected symbol?  */
   1571 	      h->size = 0;
   1572 	      h->type = 0;
   1573 
   1574 	      h = hi;
   1575 	    }
   1576 	  else
   1577 	    h = hi;
   1578 	}
   1579 
   1580       /* If the old symbol was undefined before, then it will still be
   1581 	 on the undefs list.  If the new symbol is undefined or
   1582 	 common, we can't make it bfd_link_hash_new here, because new
   1583 	 undefined or common symbols will be added to the undefs list
   1584 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1585 	 added twice to the undefs list.  Also, if the new symbol is
   1586 	 undefweak then we don't want to lose the strong undef.  */
   1587       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1588 	{
   1589 	  h->root.type = bfd_link_hash_undefined;
   1590 	  h->root.u.undef.abfd = abfd;
   1591 	}
   1592       else
   1593 	{
   1594 	  h->root.type = bfd_link_hash_new;
   1595 	  h->root.u.undef.abfd = NULL;
   1596 	}
   1597 
   1598       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1599 	{
   1600 	  /* If the new symbol is hidden or internal, completely undo
   1601 	     any dynamic link state.  */
   1602 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1603 	  h->forced_local = 0;
   1604 	  h->ref_dynamic = 0;
   1605 	}
   1606       else
   1607 	h->ref_dynamic = 1;
   1608       h->def_dynamic = 0;
   1609       /* FIXME: Should we check type and size for protected symbol?  */
   1610       h->size = 0;
   1611       h->type = 0;
   1612       return true;
   1613     }
   1614 
   1615   /* If a new weak symbol definition comes from a regular file and the
   1616      old symbol comes from a dynamic library, we treat the new one as
   1617      strong.  Similarly, an old weak symbol definition from a regular
   1618      file is treated as strong when the new symbol comes from a dynamic
   1619      library.  Further, an old weak symbol from a dynamic library is
   1620      treated as strong if the new symbol is from a dynamic library.
   1621      This reflects the way glibc's ld.so works.
   1622 
   1623      Also allow a weak symbol to override a linker script symbol
   1624      defined by an early pass over the script.  This is done so the
   1625      linker knows the symbol is defined in an object file, for the
   1626      DEFINED script function.
   1627 
   1628      Do this before setting *type_change_ok or *size_change_ok so that
   1629      we warn properly when dynamic library symbols are overridden.  */
   1630 
   1631   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
   1632     newweak = false;
   1633   if (olddef && newdyn)
   1634     oldweak = false;
   1635 
   1636   /* Allow changes between different types of function symbol.  */
   1637   if (newfunc && oldfunc)
   1638     *type_change_ok = true;
   1639 
   1640   /* It's OK to change the type if either the existing symbol or the
   1641      new symbol is weak.  A type change is also OK if the old symbol
   1642      is undefined and the new symbol is defined.  */
   1643 
   1644   if (oldweak
   1645       || newweak
   1646       || (newdef
   1647 	  && h->root.type == bfd_link_hash_undefined))
   1648     *type_change_ok = true;
   1649 
   1650   /* It's OK to change the size if either the existing symbol or the
   1651      new symbol is weak, or if the old symbol is undefined.  */
   1652 
   1653   if (*type_change_ok
   1654       || h->root.type == bfd_link_hash_undefined)
   1655     *size_change_ok = true;
   1656 
   1657   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1658      symbol, respectively, appears to be a common symbol in a dynamic
   1659      object.  If a symbol appears in an uninitialized section, and is
   1660      not weak, and is not a function, then it may be a common symbol
   1661      which was resolved when the dynamic object was created.  We want
   1662      to treat such symbols specially, because they raise special
   1663      considerations when setting the symbol size: if the symbol
   1664      appears as a common symbol in a regular object, and the size in
   1665      the regular object is larger, we must make sure that we use the
   1666      larger size.  This problematic case can always be avoided in C,
   1667      but it must be handled correctly when using Fortran shared
   1668      libraries.
   1669 
   1670      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1671      likewise for OLDDYNCOMMON and OLDDEF.
   1672 
   1673      Note that this test is just a heuristic, and that it is quite
   1674      possible to have an uninitialized symbol in a shared object which
   1675      is really a definition, rather than a common symbol.  This could
   1676      lead to some minor confusion when the symbol really is a common
   1677      symbol in some regular object.  However, I think it will be
   1678      harmless.  */
   1679 
   1680   if (newdyn
   1681       && newdef
   1682       && !newweak
   1683       && (sec->flags & SEC_ALLOC) != 0
   1684       && (sec->flags & SEC_LOAD) == 0
   1685       && sym->st_size > 0
   1686       && !newfunc)
   1687     newdyncommon = true;
   1688   else
   1689     newdyncommon = false;
   1690 
   1691   if (olddyn
   1692       && olddef
   1693       && h->root.type == bfd_link_hash_defined
   1694       && h->def_dynamic
   1695       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1696       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1697       && h->size > 0
   1698       && !oldfunc)
   1699     olddyncommon = true;
   1700   else
   1701     olddyncommon = false;
   1702 
   1703   /* We now know everything about the old and new symbols.  We ask the
   1704      backend to check if we can merge them.  */
   1705   if (bed->merge_symbol != NULL)
   1706     {
   1707       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1708 	return false;
   1709       sec = *psec;
   1710     }
   1711 
   1712   /* There are multiple definitions of a normal symbol.  Skip the
   1713      default symbol as well as definition from an IR object.  */
   1714   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
   1715       && !default_sym && h->def_regular
   1716       && !(oldbfd != NULL
   1717 	   && (oldbfd->flags & BFD_PLUGIN) != 0
   1718 	   && (abfd->flags & BFD_PLUGIN) == 0))
   1719     {
   1720       /* Handle a multiple definition.  */
   1721       (*info->callbacks->multiple_definition) (info, &h->root,
   1722 					       abfd, sec, *pvalue);
   1723       *skip = true;
   1724       return true;
   1725     }
   1726 
   1727   /* If both the old and the new symbols look like common symbols in a
   1728      dynamic object, set the size of the symbol to the larger of the
   1729      two.  */
   1730 
   1731   if (olddyncommon
   1732       && newdyncommon
   1733       && sym->st_size != h->size)
   1734     {
   1735       /* Since we think we have two common symbols, issue a multiple
   1736 	 common warning if desired.  Note that we only warn if the
   1737 	 size is different.  If the size is the same, we simply let
   1738 	 the old symbol override the new one as normally happens with
   1739 	 symbols defined in dynamic objects.  */
   1740 
   1741       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1742 					   bfd_link_hash_common, sym->st_size);
   1743       if (sym->st_size > h->size)
   1744 	h->size = sym->st_size;
   1745 
   1746       *size_change_ok = true;
   1747     }
   1748 
   1749   /* If we are looking at a dynamic object, and we have found a
   1750      definition, we need to see if the symbol was already defined by
   1751      some other object.  If so, we want to use the existing
   1752      definition, and we do not want to report a multiple symbol
   1753      definition error; we do this by clobbering *PSEC to be
   1754      bfd_und_section_ptr.
   1755 
   1756      We treat a common symbol as a definition if the symbol in the
   1757      shared library is a function, since common symbols always
   1758      represent variables; this can cause confusion in principle, but
   1759      any such confusion would seem to indicate an erroneous program or
   1760      shared library.  We also permit a common symbol in a regular
   1761      object to override a weak symbol in a shared object.  */
   1762 
   1763   if (newdyn
   1764       && newdef
   1765       && (olddef
   1766 	  || (h->root.type == bfd_link_hash_common
   1767 	      && (newweak || newfunc))))
   1768     {
   1769       *override = abfd;
   1770       newdef = false;
   1771       newdyncommon = false;
   1772 
   1773       *psec = sec = bfd_und_section_ptr;
   1774       *size_change_ok = true;
   1775 
   1776       /* If we get here when the old symbol is a common symbol, then
   1777 	 we are explicitly letting it override a weak symbol or
   1778 	 function in a dynamic object, and we don't want to warn about
   1779 	 a type change.  If the old symbol is a defined symbol, a type
   1780 	 change warning may still be appropriate.  */
   1781 
   1782       if (h->root.type == bfd_link_hash_common)
   1783 	*type_change_ok = true;
   1784     }
   1785 
   1786   /* Handle the special case of an old common symbol merging with a
   1787      new symbol which looks like a common symbol in a shared object.
   1788      We change *PSEC and *PVALUE to make the new symbol look like a
   1789      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1790      right thing.  */
   1791 
   1792   if (newdyncommon
   1793       && h->root.type == bfd_link_hash_common)
   1794     {
   1795       *override = oldbfd;
   1796       newdef = false;
   1797       newdyncommon = false;
   1798       *pvalue = sym->st_size;
   1799       *psec = sec = bed->common_section (oldsec);
   1800       *size_change_ok = true;
   1801     }
   1802 
   1803   /* Skip weak definitions of symbols that are already defined.  */
   1804   if (newdef && olddef && newweak)
   1805     {
   1806       /* Don't skip new non-IR weak syms.  */
   1807       if (!(oldbfd != NULL
   1808 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1809 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1810 	{
   1811 	  newdef = false;
   1812 	  *skip = true;
   1813 	}
   1814 
   1815       /* Merge st_other.  If the symbol already has a dynamic index,
   1816 	 but visibility says it should not be visible, turn it into a
   1817 	 local symbol.  */
   1818       elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
   1819       if (h->dynindx != -1)
   1820 	switch (ELF_ST_VISIBILITY (h->other))
   1821 	  {
   1822 	  case STV_INTERNAL:
   1823 	  case STV_HIDDEN:
   1824 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   1825 	    break;
   1826 	  }
   1827     }
   1828 
   1829   /* If the old symbol is from a dynamic object, and the new symbol is
   1830      a definition which is not from a dynamic object, then the new
   1831      symbol overrides the old symbol.  Symbols from regular files
   1832      always take precedence over symbols from dynamic objects, even if
   1833      they are defined after the dynamic object in the link.
   1834 
   1835      As above, we again permit a common symbol in a regular object to
   1836      override a definition in a shared object if the shared object
   1837      symbol is a function or is weak.  */
   1838 
   1839   flip = NULL;
   1840   if (!newdyn
   1841       && (newdef
   1842 	  || (bfd_is_com_section (sec)
   1843 	      && (oldweak || oldfunc)))
   1844       && olddyn
   1845       && olddef
   1846       && h->def_dynamic)
   1847     {
   1848       /* Change the hash table entry to undefined, and let
   1849 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1850 	 new definition.  */
   1851 
   1852       h->root.type = bfd_link_hash_undefined;
   1853       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1854       *size_change_ok = true;
   1855 
   1856       olddef = false;
   1857       olddyncommon = false;
   1858 
   1859       /* We again permit a type change when a common symbol may be
   1860 	 overriding a function.  */
   1861 
   1862       if (bfd_is_com_section (sec))
   1863 	{
   1864 	  if (oldfunc)
   1865 	    {
   1866 	      /* If a common symbol overrides a function, make sure
   1867 		 that it isn't defined dynamically nor has type
   1868 		 function.  */
   1869 	      h->def_dynamic = 0;
   1870 	      h->type = STT_NOTYPE;
   1871 	    }
   1872 	  *type_change_ok = true;
   1873 	}
   1874 
   1875       if (hi->root.type == bfd_link_hash_indirect)
   1876 	flip = hi;
   1877       else
   1878 	/* This union may have been set to be non-NULL when this symbol
   1879 	   was seen in a dynamic object.  We must force the union to be
   1880 	   NULL, so that it is correct for a regular symbol.  */
   1881 	h->verinfo.vertree = NULL;
   1882     }
   1883 
   1884   /* Handle the special case of a new common symbol merging with an
   1885      old symbol that looks like it might be a common symbol defined in
   1886      a shared object.  Note that we have already handled the case in
   1887      which a new common symbol should simply override the definition
   1888      in the shared library.  */
   1889 
   1890   if (! newdyn
   1891       && bfd_is_com_section (sec)
   1892       && olddyncommon)
   1893     {
   1894       /* It would be best if we could set the hash table entry to a
   1895 	 common symbol, but we don't know what to use for the section
   1896 	 or the alignment.  */
   1897       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1898 					   bfd_link_hash_common, sym->st_size);
   1899 
   1900       /* If the presumed common symbol in the dynamic object is
   1901 	 larger, pretend that the new symbol has its size.  */
   1902 
   1903       if (h->size > *pvalue)
   1904 	*pvalue = h->size;
   1905 
   1906       /* We need to remember the alignment required by the symbol
   1907 	 in the dynamic object.  */
   1908       BFD_ASSERT (pold_alignment);
   1909       *pold_alignment = h->root.u.def.section->alignment_power;
   1910 
   1911       olddef = false;
   1912       olddyncommon = false;
   1913 
   1914       h->root.type = bfd_link_hash_undefined;
   1915       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1916 
   1917       *size_change_ok = true;
   1918       *type_change_ok = true;
   1919 
   1920       if (hi->root.type == bfd_link_hash_indirect)
   1921 	flip = hi;
   1922       else
   1923 	h->verinfo.vertree = NULL;
   1924     }
   1925 
   1926   if (flip != NULL)
   1927     {
   1928       /* Handle the case where we had a versioned symbol in a dynamic
   1929 	 library and now find a definition in a normal object.  In this
   1930 	 case, we make the versioned symbol point to the normal one.  */
   1931       flip->root.type = h->root.type;
   1932       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1933       h->root.type = bfd_link_hash_indirect;
   1934       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1935       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1936       if (h->def_dynamic)
   1937 	{
   1938 	  h->def_dynamic = 0;
   1939 	  flip->ref_dynamic = 1;
   1940 	}
   1941     }
   1942 
   1943   return true;
   1944 }
   1945 
   1946 /* This function is called to create an indirect symbol from the
   1947    default for the symbol with the default version if needed. The
   1948    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1949    set DYNSYM if the new indirect symbol is dynamic.  */
   1950 
   1951 static bool
   1952 _bfd_elf_add_default_symbol (bfd *abfd,
   1953 			     struct bfd_link_info *info,
   1954 			     struct elf_link_hash_entry *h,
   1955 			     const char *name,
   1956 			     Elf_Internal_Sym *sym,
   1957 			     asection *sec,
   1958 			     bfd_vma value,
   1959 			     bfd **poldbfd,
   1960 			     bool *dynsym)
   1961 {
   1962   bool type_change_ok;
   1963   bool size_change_ok;
   1964   bool skip;
   1965   char *shortname;
   1966   struct elf_link_hash_entry *hi;
   1967   struct bfd_link_hash_entry *bh;
   1968   elf_backend_data *bed;
   1969   bool collect;
   1970   bool dynamic;
   1971   bfd *override;
   1972   const char *p;
   1973   size_t len, shortlen;
   1974   asection *tmp_sec;
   1975   bool matched;
   1976 
   1977   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1978     return true;
   1979 
   1980   /* If this symbol has a version, and it is the default version, we
   1981      create an indirect symbol from the default name to the fully
   1982      decorated name.  This will cause external references which do not
   1983      specify a version to be bound to this version of the symbol.  */
   1984   p = strchr (name, ELF_VER_CHR);
   1985   if (h->versioned == unknown)
   1986     {
   1987       if (p == NULL)
   1988 	{
   1989 	  h->versioned = unversioned;
   1990 	  return true;
   1991 	}
   1992       else
   1993 	{
   1994 	  if (p[1] != ELF_VER_CHR)
   1995 	    {
   1996 	      h->versioned = versioned_hidden;
   1997 	      return true;
   1998 	    }
   1999 	  else
   2000 	    h->versioned = versioned;
   2001 	}
   2002     }
   2003   else
   2004     {
   2005       /* PR ld/19073: We may see an unversioned definition after the
   2006 	 default version.  */
   2007       if (p == NULL)
   2008 	return true;
   2009     }
   2010 
   2011   bed = get_elf_backend_data (abfd);
   2012   collect = bed->collect;
   2013   dynamic = (abfd->flags & DYNAMIC) != 0;
   2014 
   2015   shortlen = p - name;
   2016   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   2017   if (shortname == NULL)
   2018     return false;
   2019   memcpy (shortname, name, shortlen);
   2020   shortname[shortlen] = '\0';
   2021 
   2022   /* We are going to create a new symbol.  Merge it with any existing
   2023      symbol with this name.  For the purposes of the merge, act as
   2024      though we were defining the symbol we just defined, although we
   2025      actually going to define an indirect symbol.  */
   2026   type_change_ok = false;
   2027   size_change_ok = false;
   2028   matched = true;
   2029   tmp_sec = sec;
   2030   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2031 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2032 			      &type_change_ok, &size_change_ok, &matched))
   2033     return false;
   2034 
   2035   if (skip)
   2036     goto nondefault;
   2037 
   2038   if (hi->def_regular || ELF_COMMON_DEF_P (hi))
   2039     {
   2040       /* If the undecorated symbol will have a version added by a
   2041 	 script different to H, then don't indirect to/from the
   2042 	 undecorated symbol.  This isn't ideal because we may not yet
   2043 	 have seen symbol versions, if given by a script on the
   2044 	 command line rather than via --version-script.  */
   2045       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   2046 	{
   2047 	  bool hide;
   2048 
   2049 	  hi->verinfo.vertree
   2050 	    = bfd_find_version_for_sym (info->version_info,
   2051 					hi->root.root.string, &hide);
   2052 	  if (hi->verinfo.vertree != NULL && hide)
   2053 	    {
   2054 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   2055 	      goto nondefault;
   2056 	    }
   2057 	}
   2058       if (hi->verinfo.vertree != NULL
   2059 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   2060 	goto nondefault;
   2061     }
   2062 
   2063   if (! override)
   2064     {
   2065       /* Add the default symbol if not performing a relocatable link.  */
   2066       if (! bfd_link_relocatable (info))
   2067 	{
   2068 	  bh = &hi->root;
   2069 	  if (bh->type == bfd_link_hash_defined
   2070 	      && bh->u.def.section->owner != NULL
   2071 	      && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
   2072 	    {
   2073 	      /* Mark the previous definition from IR object as
   2074 		 undefined so that the generic linker will override
   2075 		 it.  */
   2076 	      bh->type = bfd_link_hash_undefined;
   2077 	      bh->u.undef.abfd = bh->u.def.section->owner;
   2078 	    }
   2079 	  if (! (_bfd_generic_link_add_one_symbol
   2080 		 (info, abfd, shortname, BSF_INDIRECT,
   2081 		  bfd_ind_section_ptr,
   2082 		  0, name, false, collect, &bh)))
   2083 	    return false;
   2084 	  hi = (struct elf_link_hash_entry *) bh;
   2085 	}
   2086     }
   2087   else
   2088     {
   2089       /* In this case the symbol named SHORTNAME is overriding the
   2090 	 indirect symbol we want to add.  We were planning on making
   2091 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   2092 	 is the name without a version.  NAME is the fully versioned
   2093 	 name, and it is the default version.
   2094 
   2095 	 Overriding means that we already saw a definition for the
   2096 	 symbol SHORTNAME in a regular object, and it is overriding
   2097 	 the symbol defined in the dynamic object.
   2098 
   2099 	 When this happens, we actually want to change NAME, the
   2100 	 symbol we just added, to refer to SHORTNAME.  This will cause
   2101 	 references to NAME in the shared object to become references
   2102 	 to SHORTNAME in the regular object.  This is what we expect
   2103 	 when we override a function in a shared object: that the
   2104 	 references in the shared object will be mapped to the
   2105 	 definition in the regular object.  */
   2106 
   2107       while (hi->root.type == bfd_link_hash_indirect
   2108 	     || hi->root.type == bfd_link_hash_warning)
   2109 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2110 
   2111       h->root.type = bfd_link_hash_indirect;
   2112       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   2113       if (h->def_dynamic)
   2114 	{
   2115 	  h->def_dynamic = 0;
   2116 	  hi->ref_dynamic = 1;
   2117 	  if (hi->ref_regular
   2118 	      || hi->def_regular)
   2119 	    {
   2120 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   2121 		return false;
   2122 	    }
   2123 	}
   2124 
   2125       /* Now set HI to H, so that the following code will set the
   2126 	 other fields correctly.  */
   2127       hi = h;
   2128     }
   2129 
   2130   /* Check if HI is a warning symbol.  */
   2131   if (hi->root.type == bfd_link_hash_warning)
   2132     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2133 
   2134   /* If there is a duplicate definition somewhere, then HI may not
   2135      point to an indirect symbol.  We will have reported an error to
   2136      the user in that case.  */
   2137 
   2138   if (hi->root.type == bfd_link_hash_indirect)
   2139     {
   2140       struct elf_link_hash_entry *ht;
   2141 
   2142       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2143       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   2144 
   2145       /* If we first saw a reference to SHORTNAME with non-default
   2146 	 visibility, merge that visibility to the @@VER symbol.  */
   2147       elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
   2148 
   2149       /* A reference to the SHORTNAME symbol from a dynamic library
   2150 	 will be satisfied by the versioned symbol at runtime.  In
   2151 	 effect, we have a reference to the versioned symbol.  */
   2152       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2153       hi->dynamic_def |= ht->dynamic_def;
   2154 
   2155       /* See if the new flags lead us to realize that the symbol must
   2156 	 be dynamic.  */
   2157       if (! *dynsym)
   2158 	{
   2159 	  if (! dynamic)
   2160 	    {
   2161 	      if (! bfd_link_executable (info)
   2162 		  || hi->def_dynamic
   2163 		  || hi->ref_dynamic)
   2164 		*dynsym = true;
   2165 	    }
   2166 	  else
   2167 	    {
   2168 	      if (hi->ref_regular)
   2169 		*dynsym = true;
   2170 	    }
   2171 	}
   2172     }
   2173 
   2174   /* We also need to define an indirection from the nondefault version
   2175      of the symbol.  */
   2176 
   2177  nondefault:
   2178   len = strlen (name);
   2179   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   2180   if (shortname == NULL)
   2181     return false;
   2182   memcpy (shortname, name, shortlen);
   2183   memcpy (shortname + shortlen, p + 1, len - shortlen);
   2184 
   2185   /* Once again, merge with any existing symbol.  */
   2186   type_change_ok = false;
   2187   size_change_ok = false;
   2188   tmp_sec = sec;
   2189   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2190 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2191 			      &type_change_ok, &size_change_ok, &matched))
   2192     return false;
   2193 
   2194   if (skip)
   2195     {
   2196       if (!dynamic
   2197 	  && h->root.type == bfd_link_hash_defweak
   2198 	  && hi->root.type == bfd_link_hash_defined)
   2199 	{
   2200 	  /* We are handling a weak sym@@ver and attempting to define
   2201 	     a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
   2202 	     new weak sym@ver because there is already a strong sym@ver.
   2203 	     However, sym@ver and sym@@ver are really the same symbol.
   2204 	     The existing strong sym@ver ought to override sym@@ver.  */
   2205 	  h->root.type = bfd_link_hash_defined;
   2206 	  h->root.u.def.section = hi->root.u.def.section;
   2207 	  h->root.u.def.value = hi->root.u.def.value;
   2208 	  hi->root.type = bfd_link_hash_indirect;
   2209 	  hi->root.u.i.link = &h->root;
   2210 	}
   2211       else
   2212 	return true;
   2213     }
   2214   else if (override)
   2215     {
   2216       /* Here SHORTNAME is a versioned name, so we don't expect to see
   2217 	 the type of override we do in the case above unless it is
   2218 	 overridden by a versioned definition.  */
   2219       if (hi->root.type != bfd_link_hash_defined
   2220 	  && hi->root.type != bfd_link_hash_defweak)
   2221 	_bfd_error_handler
   2222 	  /* xgettext:c-format */
   2223 	  (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
   2224 	   abfd, shortname);
   2225       return true;
   2226     }
   2227   else
   2228     {
   2229       bh = &hi->root;
   2230       if (! (_bfd_generic_link_add_one_symbol
   2231 	     (info, abfd, shortname, BSF_INDIRECT,
   2232 	      bfd_ind_section_ptr, 0, name, false, collect, &bh)))
   2233 	return false;
   2234       hi = (struct elf_link_hash_entry *) bh;
   2235     }
   2236 
   2237   /* If there is a duplicate definition somewhere, then HI may not
   2238      point to an indirect symbol.  We will have reported an error
   2239      to the user in that case.  */
   2240   if (hi->root.type == bfd_link_hash_indirect)
   2241     {
   2242       (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   2243       h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2244       hi->dynamic_def |= h->dynamic_def;
   2245 
   2246       /* If we first saw a reference to @VER symbol with
   2247 	 non-default visibility, merge that visibility to the
   2248 	 @@VER symbol.  */
   2249       elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
   2250 
   2251       /* See if the new flags lead us to realize that the symbol
   2252 	 must be dynamic.  */
   2253       if (! *dynsym)
   2254 	{
   2255 	  if (! dynamic)
   2256 	    {
   2257 	      if (! bfd_link_executable (info)
   2258 		  || hi->ref_dynamic)
   2259 		*dynsym = true;
   2260 	    }
   2261 	  else
   2262 	    {
   2263 	      if (hi->ref_regular)
   2264 		*dynsym = true;
   2265 	    }
   2266 	}
   2267     }
   2268 
   2269   return true;
   2270 }
   2271 
   2272 /* This routine is used to export all defined symbols into the dynamic
   2274    symbol table.  It is called via elf_link_hash_traverse.  */
   2275 
   2276 static bool
   2277 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   2278 {
   2279   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2280 
   2281   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2282   if (h->root.type == bfd_link_hash_indirect)
   2283     return true;
   2284 
   2285   /* Ignore this if we won't export it.  */
   2286   if (!eif->info->export_dynamic && !h->dynamic)
   2287     return true;
   2288 
   2289   if (h->dynindx == -1
   2290       && (h->def_regular || h->ref_regular)
   2291       && ! bfd_hide_sym_by_version (eif->info->version_info,
   2292 				    h->root.root.string))
   2293     {
   2294       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2295 	{
   2296 	  eif->failed = true;
   2297 	  return false;
   2298 	}
   2299     }
   2300 
   2301   return true;
   2302 }
   2303 
   2304 /* Return true if linked against glibc.  Otherwise return false.  If
   2306    linked against glibc, add VERSION_DEP to the list of glibc version
   2307    dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
   2308    true, add VERSION_DEP to the version dependency list only if libc.so
   2309    defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
   2310    minor base version.  */
   2311 
   2312 static bool
   2313 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
   2314 			    const char *version_dep,
   2315 			    int *glibc_minor_base,
   2316 			    bool *auto_version)
   2317 {
   2318   Elf_Internal_Verneed *t;
   2319   Elf_Internal_Vernaux *a;
   2320   size_t amt;
   2321   int minor_version = -1;
   2322   bool added = false;
   2323   bool glibc = false;
   2324 
   2325   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2326        t != NULL;
   2327        t = t->vn_nextref)
   2328     {
   2329       const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
   2330       if (soname != NULL && startswith (soname, "libc.so."))
   2331 	break;
   2332     }
   2333 
   2334   /* Skip the shared library if it isn't libc.so.  */
   2335   if (t == NULL)
   2336     goto update_auto_version_and_return;
   2337 
   2338   for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2339     {
   2340       /* Return if VERSION_DEP dependency has been added.  */
   2341       if (a->vna_nodename == version_dep
   2342 	  || strcmp (a->vna_nodename, version_dep) == 0)
   2343 	{
   2344 	  glibc = true;
   2345 	  goto update_auto_version_and_return;
   2346 	}
   2347 
   2348       /* Check if libc.so provides GLIBC_2.XX version.  */
   2349       if (startswith (a->vna_nodename, "GLIBC_2."))
   2350 	{
   2351 	  minor_version = strtol (a->vna_nodename + 8, NULL, 10);
   2352 	  if (minor_version < *glibc_minor_base)
   2353 	    *glibc_minor_base = minor_version;
   2354 	}
   2355     }
   2356 
   2357   /* Skip if it isn't linked against glibc.  */
   2358   if (minor_version < 0)
   2359     goto update_auto_version_and_return;
   2360 
   2361   glibc = true;
   2362 
   2363   if (auto_version && *auto_version)
   2364     {
   2365       /* Add VERSION_DEP to the version dependency list only if
   2366 	 libc.so defines VERSION_DEP.  */
   2367 
   2368       bool defined = false;
   2369       Elf_Internal_Verdef *d;
   2370 
   2371       for (d = elf_tdata (t->vn_bfd)->verdef;
   2372 	   d != NULL;
   2373 	   d = d->vd_nextdef)
   2374 	if (strcmp (d->vd_nodename, version_dep) == 0)
   2375 	  {
   2376 	    defined = true;
   2377 	    break;
   2378 	  }
   2379 
   2380       /* Set *AUTO_VERSION to false and return true to indicate that
   2381 	 libc.so doesn't define VERSION_DEP.  */
   2382       if (!defined)
   2383 	goto update_auto_version_and_return;
   2384     }
   2385 
   2386   /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
   2387   if (startswith (version_dep, "GLIBC_2."))
   2388     {
   2389       minor_version = strtol (version_dep + 8, NULL, 10);
   2390       if (minor_version <= *glibc_minor_base)
   2391 	goto update_auto_version_and_return;
   2392     }
   2393 
   2394   amt = sizeof *a;
   2395   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2396   if (a == NULL)
   2397     {
   2398       rinfo->failed = true;
   2399       glibc = false;
   2400       goto update_auto_version_and_return;
   2401     }
   2402 
   2403   a->vna_nodename = version_dep;
   2404   a->vna_flags = 0;
   2405   a->vna_nextptr = t->vn_auxptr;
   2406   a->vna_other = rinfo->vers + 1;
   2407   ++rinfo->vers;
   2408 
   2409   t->vn_auxptr = a;
   2410 
   2411   added = true;
   2412 
   2413  update_auto_version_and_return:
   2414   if (auto_version)
   2415     *auto_version = added;
   2416 
   2417   return glibc;
   2418 }
   2419 
   2420 /* Add VERSION_DEP to the list of version dependencies when linked
   2421    against glibc.  */
   2422 
   2423 bool
   2424 _bfd_elf_link_add_glibc_version_dependency
   2425   (struct elf_find_verdep_info *rinfo,
   2426    const char *const version_dep[],
   2427    bool *auto_version)
   2428 {
   2429   int glibc_minor_base = INT_MAX;
   2430 
   2431   do
   2432     {
   2433       /* Return if not linked against glibc.  */
   2434       if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
   2435 				       &glibc_minor_base, auto_version))
   2436 	return false;
   2437       version_dep++;
   2438       auto_version++;
   2439     }
   2440   while (*version_dep != NULL);
   2441 
   2442   return true;
   2443 }
   2444 
   2445 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
   2446    linked against glibc.  */
   2447 
   2448 void
   2449 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
   2450 {
   2451   if (rinfo->info->enable_dt_relr)
   2452     {
   2453       static const char *const version[] =
   2454 	{
   2455 	  "GLIBC_ABI_DT_RELR",
   2456 	  NULL
   2457 	};
   2458       _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
   2459     }
   2460 }
   2461 
   2462 /* Look through the symbols which are defined in other shared
   2463    libraries and referenced here.  Update the list of version
   2464    dependencies.  This will be put into the .gnu.version_r section.
   2465    This function is called via elf_link_hash_traverse.  */
   2466 
   2467 static bool
   2468 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2469 					 void *data)
   2470 {
   2471   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2472   Elf_Internal_Verneed *t;
   2473   Elf_Internal_Vernaux *a;
   2474   size_t amt;
   2475 
   2476   /* We only care about symbols defined in shared objects with version
   2477      information.  */
   2478   if (!h->def_dynamic
   2479       || h->def_regular
   2480       || h->dynindx == -1
   2481       || h->verinfo.verdef == NULL
   2482       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2483 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2484     return true;
   2485 
   2486   /* See if we already know about this version.  */
   2487   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2488        t != NULL;
   2489        t = t->vn_nextref)
   2490     {
   2491       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2492 	continue;
   2493 
   2494       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2495 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2496 	  return true;
   2497 
   2498       break;
   2499     }
   2500 
   2501   /* This is a new version.  Add it to tree we are building.  */
   2502 
   2503   if (t == NULL)
   2504     {
   2505       amt = sizeof *t;
   2506       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2507       if (t == NULL)
   2508 	{
   2509 	  rinfo->failed = true;
   2510 	  return false;
   2511 	}
   2512 
   2513       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2514       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2515       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2516     }
   2517 
   2518   amt = sizeof *a;
   2519   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2520   if (a == NULL)
   2521     {
   2522       rinfo->failed = true;
   2523       return false;
   2524     }
   2525 
   2526   /* Note that we are copying a string pointer here, and testing it
   2527      above.  If bfd_elf_string_from_elf_section is ever changed to
   2528      discard the string data when low in memory, this will have to be
   2529      fixed.  */
   2530   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2531 
   2532   a->vna_flags = h->verinfo.verdef->vd_flags;
   2533   a->vna_nextptr = t->vn_auxptr;
   2534 
   2535   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2536   ++rinfo->vers;
   2537 
   2538   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2539 
   2540   t->vn_auxptr = a;
   2541 
   2542   return true;
   2543 }
   2544 
   2545 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
   2546    hidden.  Set *T_P to NULL if there is no match.  */
   2547 
   2548 static bool
   2549 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
   2550 				     struct elf_link_hash_entry *h,
   2551 				     const char *version_p,
   2552 				     struct bfd_elf_version_tree **t_p,
   2553 				     bool *hide)
   2554 {
   2555   struct bfd_elf_version_tree *t;
   2556 
   2557   /* Look for the version.  If we find it, it is no longer weak.  */
   2558   for (t = info->version_info; t != NULL; t = t->next)
   2559     {
   2560       if (strcmp (t->name, version_p) == 0)
   2561 	{
   2562 	  size_t len;
   2563 	  char *alc;
   2564 	  struct bfd_elf_version_expr *d;
   2565 
   2566 	  len = version_p - h->root.root.string;
   2567 	  alc = (char *) bfd_malloc (len);
   2568 	  if (alc == NULL)
   2569 	    return false;
   2570 	  memcpy (alc, h->root.root.string, len - 1);
   2571 	  alc[len - 1] = '\0';
   2572 	  if (alc[len - 2] == ELF_VER_CHR)
   2573 	    alc[len - 2] = '\0';
   2574 
   2575 	  h->verinfo.vertree = t;
   2576 	  t->used = true;
   2577 	  d = NULL;
   2578 
   2579 	  if (t->globals.list != NULL)
   2580 	    d = (*t->match) (&t->globals, NULL, alc);
   2581 
   2582 	  /* See if there is anything to force this symbol to
   2583 	     local scope.  */
   2584 	  if (d == NULL && t->locals.list != NULL)
   2585 	    {
   2586 	      d = (*t->match) (&t->locals, NULL, alc);
   2587 	      if (d != NULL
   2588 		  && h->dynindx != -1
   2589 		  && ! info->export_dynamic)
   2590 		*hide = true;
   2591 	    }
   2592 
   2593 	  free (alc);
   2594 	  break;
   2595 	}
   2596     }
   2597 
   2598   *t_p = t;
   2599 
   2600   return true;
   2601 }
   2602 
   2603 /* Return TRUE if the symbol H is hidden by version script.  */
   2604 
   2605 bool
   2606 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
   2607 				   struct elf_link_hash_entry *h)
   2608 {
   2609   const char *p;
   2610   bool hide = false;
   2611   elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
   2612 
   2613   /* Version script only hides symbols defined in regular objects.  */
   2614   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2615     return true;
   2616 
   2617   p = strchr (h->root.root.string, ELF_VER_CHR);
   2618   if (p != NULL && h->verinfo.vertree == NULL)
   2619     {
   2620       struct bfd_elf_version_tree *t;
   2621 
   2622       ++p;
   2623       if (*p == ELF_VER_CHR)
   2624 	++p;
   2625 
   2626       if (*p != '\0'
   2627 	  && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
   2628 	  && hide)
   2629 	{
   2630 	  if (hide)
   2631 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   2632 	  return true;
   2633 	}
   2634     }
   2635 
   2636   /* If we don't have a version for this symbol, see if we can find
   2637      something.  */
   2638   if (h->verinfo.vertree == NULL && info->version_info != NULL)
   2639     {
   2640       h->verinfo.vertree
   2641 	= bfd_find_version_for_sym (info->version_info,
   2642 				    h->root.root.string, &hide);
   2643       if (h->verinfo.vertree != NULL && hide)
   2644 	{
   2645 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   2646 	  return true;
   2647 	}
   2648     }
   2649 
   2650   return false;
   2651 }
   2652 
   2653 /* Figure out appropriate versions for all the symbols.  We may not
   2654    have the version number script until we have read all of the input
   2655    files, so until that point we don't know which symbols should be
   2656    local.  This function is called via elf_link_hash_traverse.  */
   2657 
   2658 static bool
   2659 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2660 {
   2661   struct elf_info_failed *sinfo;
   2662   struct bfd_link_info *info;
   2663   elf_backend_data *bed;
   2664   struct elf_info_failed eif;
   2665   const char *p;
   2666   bool hide;
   2667 
   2668   sinfo = (struct elf_info_failed *) data;
   2669   info = sinfo->info;
   2670 
   2671   /* Fix the symbol flags.  */
   2672   eif.failed = false;
   2673   eif.info = info;
   2674   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2675     {
   2676       if (eif.failed)
   2677 	sinfo->failed = true;
   2678       return false;
   2679     }
   2680 
   2681   bed = get_elf_backend_data (info->output_bfd);
   2682 
   2683   /* We only need version numbers for symbols defined in regular
   2684      objects.  */
   2685   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2686     {
   2687       /* Hide symbols defined in discarded input sections.  */
   2688       if ((h->root.type == bfd_link_hash_defined
   2689 	   || h->root.type == bfd_link_hash_defweak)
   2690 	  && discarded_section (h->root.u.def.section))
   2691 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2692       return true;
   2693     }
   2694 
   2695   hide = false;
   2696   p = strchr (h->root.root.string, ELF_VER_CHR);
   2697   if (p != NULL && h->verinfo.vertree == NULL)
   2698     {
   2699       struct bfd_elf_version_tree *t;
   2700 
   2701       ++p;
   2702       if (*p == ELF_VER_CHR)
   2703 	++p;
   2704 
   2705       /* If there is no version string, we can just return out.  */
   2706       if (*p == '\0')
   2707 	return true;
   2708 
   2709       if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
   2710 	{
   2711 	  sinfo->failed = true;
   2712 	  return false;
   2713 	}
   2714 
   2715       if (hide)
   2716 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2717 
   2718       /* If we are building an application, we need to create a
   2719 	 version node for this version.  */
   2720       if (t == NULL && bfd_link_executable (info))
   2721 	{
   2722 	  struct bfd_elf_version_tree **pp;
   2723 	  int version_index;
   2724 
   2725 	  /* If we aren't going to export this symbol, we don't need
   2726 	     to worry about it.  */
   2727 	  if (h->dynindx == -1)
   2728 	    return true;
   2729 
   2730 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
   2731 							  sizeof *t);
   2732 	  if (t == NULL)
   2733 	    {
   2734 	      sinfo->failed = true;
   2735 	      return false;
   2736 	    }
   2737 
   2738 	  t->name = p;
   2739 	  t->name_indx = (unsigned int) -1;
   2740 	  t->used = true;
   2741 
   2742 	  version_index = 1;
   2743 	  /* Don't count anonymous version tag.  */
   2744 	  if (sinfo->info->version_info != NULL
   2745 	      && sinfo->info->version_info->vernum == 0)
   2746 	    version_index = 0;
   2747 	  for (pp = &sinfo->info->version_info;
   2748 	       *pp != NULL;
   2749 	       pp = &(*pp)->next)
   2750 	    ++version_index;
   2751 	  t->vernum = version_index;
   2752 
   2753 	  *pp = t;
   2754 
   2755 	  h->verinfo.vertree = t;
   2756 	}
   2757       else if (t == NULL)
   2758 	{
   2759 	  /* We could not find the version for a symbol when
   2760 	     generating a shared archive.  Return an error.  */
   2761 	  _bfd_error_handler
   2762 	    /* xgettext:c-format */
   2763 	    (_("%pB: version node not found for symbol %s"),
   2764 	     info->output_bfd, h->root.root.string);
   2765 	  bfd_set_error (bfd_error_bad_value);
   2766 	  sinfo->failed = true;
   2767 	  return false;
   2768 	}
   2769     }
   2770 
   2771   /* If we don't have a version for this symbol, see if we can find
   2772      something.  */
   2773   if (!hide
   2774       && h->verinfo.vertree == NULL
   2775       && sinfo->info->version_info != NULL)
   2776     {
   2777       h->verinfo.vertree
   2778 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2779 				    h->root.root.string, &hide);
   2780       if (h->verinfo.vertree != NULL && hide)
   2781 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2782     }
   2783 
   2784   return true;
   2785 }
   2786 
   2787 /* Read and swap the relocs from the section indicated by SHDR.  This
   2789    may be either a REL or a RELA section.  The relocations are
   2790    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2791    which should have already been allocated to contain enough space.
   2792    The *EXTERNAL_RELOCS_P are a buffer where the external form of the
   2793    relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
   2794    *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
   2795    memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
   2796    unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
   2797 
   2798    Returns FALSE if something goes wrong.  */
   2799 
   2800 static bool
   2801 elf_link_read_relocs_from_section (bfd *abfd,
   2802 				   const asection *sec,
   2803 				   Elf_Internal_Shdr *shdr,
   2804 				   void **external_relocs_addr,
   2805 				   size_t *external_relocs_size,
   2806 				   Elf_Internal_Rela *internal_relocs)
   2807 {
   2808   elf_backend_data *bed;
   2809   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2810   const bfd_byte *erela;
   2811   const bfd_byte *erelaend;
   2812   Elf_Internal_Rela *irela;
   2813   Elf_Internal_Shdr *symtab_hdr;
   2814   size_t nsyms;
   2815   void *external_relocs = *external_relocs_addr;
   2816 
   2817   /* Position ourselves at the start of the section.  */
   2818   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2819     return false;
   2820 
   2821   /* Read the relocations.  */
   2822   *external_relocs_size = shdr->sh_size;
   2823   if (!_bfd_mmap_read_temporary (&external_relocs,
   2824 				 external_relocs_size,
   2825 				 external_relocs_addr, abfd, true))
   2826     return false;
   2827 
   2828   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2829   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2830 
   2831   bed = get_elf_backend_data (abfd);
   2832 
   2833   /* Convert the external relocations to the internal format.  */
   2834   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2835     swap_in = bed->s->swap_reloc_in;
   2836   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2837     swap_in = bed->s->swap_reloca_in;
   2838   else
   2839     {
   2840       bfd_set_error (bfd_error_wrong_format);
   2841       return false;
   2842     }
   2843 
   2844   erela = (const bfd_byte *) external_relocs;
   2845   /* Setting erelaend like this and comparing with <= handles case of
   2846      a fuzzed object with sh_size not a multiple of sh_entsize.  */
   2847   erelaend = erela + shdr->sh_size - shdr->sh_entsize;
   2848   irela = internal_relocs;
   2849   while (erela <= erelaend)
   2850     {
   2851       bfd_vma r_symndx;
   2852 
   2853       (*swap_in) (abfd, erela, irela);
   2854       r_symndx = ELF32_R_SYM (irela->r_info);
   2855       if (bed->s->arch_size == 64)
   2856 	r_symndx >>= 24;
   2857       if (nsyms > 0)
   2858 	{
   2859 	  if ((size_t) r_symndx >= nsyms)
   2860 	    {
   2861 	      _bfd_error_handler
   2862 		/* xgettext:c-format */
   2863 		(_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
   2864 		   " for offset %#" PRIx64 " in section `%pA'"),
   2865 		 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
   2866 		 (uint64_t) irela->r_offset, sec);
   2867 	      bfd_set_error (bfd_error_bad_value);
   2868 	      return false;
   2869 	    }
   2870 	}
   2871       else if (r_symndx != STN_UNDEF)
   2872 	{
   2873 	  _bfd_error_handler
   2874 	    /* xgettext:c-format */
   2875 	    (_("%pB: non-zero symbol index (%#" PRIx64 ")"
   2876 	       " for offset %#" PRIx64 " in section `%pA'"
   2877 	       " when the object file has no symbol table"),
   2878 	     abfd, (uint64_t) r_symndx,
   2879 	     (uint64_t) irela->r_offset, sec);
   2880 	  bfd_set_error (bfd_error_bad_value);
   2881 	  return false;
   2882 	}
   2883       irela += bed->s->int_rels_per_ext_rel;
   2884       erela += shdr->sh_entsize;
   2885     }
   2886 
   2887   return true;
   2888 }
   2889 
   2890 /* Read and swap the relocs for a section O.  They may have been
   2891    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2892    not NULL, they are used as buffers to read into.  They are known to
   2893    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2894    the return value is allocated using either malloc or bfd_alloc,
   2895    according to the KEEP_MEMORY argument.  If O has two relocation
   2896    sections (both REL and RELA relocations), then the REL_HDR
   2897    relocations will appear first in INTERNAL_RELOCS, followed by the
   2898    RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
   2899    update cache_size.  */
   2900 
   2901 Elf_Internal_Rela *
   2902 _bfd_elf_link_info_read_relocs (bfd *abfd,
   2903 				struct bfd_link_info *info,
   2904 				const asection *o,
   2905 				void *external_relocs,
   2906 				Elf_Internal_Rela *internal_relocs,
   2907 				bool keep_memory)
   2908 {
   2909   void *alloc1 = NULL;
   2910   size_t alloc1_size;
   2911   Elf_Internal_Rela *alloc2 = NULL;
   2912   elf_backend_data *bed = get_elf_backend_data (abfd);
   2913   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2914   Elf_Internal_Rela *internal_rela_relocs;
   2915 
   2916   if (esdo->relocs != NULL)
   2917     return esdo->relocs;
   2918 
   2919   if (o->reloc_count == 0)
   2920     return NULL;
   2921 
   2922   if (internal_relocs == NULL)
   2923     {
   2924       bfd_size_type size;
   2925 
   2926       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
   2927       if (keep_memory && info)
   2928 	info->cache_size += size;
   2929       internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2930       if (internal_relocs == NULL)
   2931 	return NULL;
   2932     }
   2933 
   2934   alloc1 = external_relocs;
   2935   internal_rela_relocs = internal_relocs;
   2936   if (esdo->rel.hdr)
   2937     {
   2938       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2939 					      &alloc1, &alloc1_size,
   2940 					      internal_relocs))
   2941 	goto error_return;
   2942       external_relocs = (((bfd_byte *) external_relocs)
   2943 			 + esdo->rel.hdr->sh_size);
   2944       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2945 			       * bed->s->int_rels_per_ext_rel);
   2946     }
   2947 
   2948   if (esdo->rela.hdr
   2949       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2950 					      &alloc1, &alloc1_size,
   2951 					      internal_rela_relocs)))
   2952     goto error_return;
   2953 
   2954   /* Cache the results for next time, if we can.  */
   2955   if (keep_memory)
   2956     esdo->relocs = internal_relocs;
   2957 
   2958   _bfd_munmap_temporary (alloc1, alloc1_size);
   2959 
   2960   /* Don't free alloc2, since if it was allocated we are passing it
   2961      back (under the name of internal_relocs).  */
   2962 
   2963   return internal_relocs;
   2964 
   2965  error_return:
   2966   _bfd_munmap_temporary (alloc1, alloc1_size);
   2967   free (alloc2);
   2968   return NULL;
   2969 }
   2970 
   2971 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
   2972    NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
   2973    struct bfd_link_info.  */
   2974 
   2975 Elf_Internal_Rela *
   2976 _bfd_elf_link_read_relocs (bfd *abfd,
   2977 			   const asection *o,
   2978 			   void *external_relocs,
   2979 			   Elf_Internal_Rela *internal_relocs,
   2980 			   bool keep_memory)
   2981 {
   2982   return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
   2983 					 internal_relocs, keep_memory);
   2984 
   2985 }
   2986 
   2987 /* Compute the size of, and allocate space for, REL_HDR which is the
   2988    section header for a section containing relocations for O.  */
   2989 
   2990 static bool
   2991 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2992 				  struct bfd_elf_section_reloc_data *reldata)
   2993 {
   2994   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2995 
   2996   /* That allows us to calculate the size of the section.  */
   2997   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2998 
   2999   /* The contents field must last into write_object_contents, so we
   3000      allocate it with bfd_alloc rather than malloc.  Also since we
   3001      cannot be sure that the contents will actually be filled in,
   3002      we zero the allocated space.  */
   3003   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   3004   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   3005     return false;
   3006 
   3007   if (reldata->hashes == NULL && reldata->count)
   3008     {
   3009       struct elf_link_hash_entry **p;
   3010 
   3011       p = ((struct elf_link_hash_entry **)
   3012 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   3013       if (p == NULL)
   3014 	return false;
   3015 
   3016       reldata->hashes = p;
   3017     }
   3018 
   3019   return true;
   3020 }
   3021 
   3022 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   3023    originated from the section given by INPUT_REL_HDR) to the
   3024    OUTPUT_BFD.  */
   3025 
   3026 bool
   3027 _bfd_elf_link_output_relocs (bfd *output_bfd,
   3028 			     asection *input_section,
   3029 			     Elf_Internal_Shdr *input_rel_hdr,
   3030 			     Elf_Internal_Rela *internal_relocs,
   3031 			     struct elf_link_hash_entry **rel_hash)
   3032 {
   3033   Elf_Internal_Rela *irela;
   3034   Elf_Internal_Rela *irelaend;
   3035   bfd_byte *erel;
   3036   struct bfd_elf_section_reloc_data *output_reldata;
   3037   asection *output_section;
   3038   elf_backend_data *bed;
   3039   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   3040   struct bfd_elf_section_data *esdo;
   3041 
   3042   output_section = input_section->output_section;
   3043 
   3044   bed = get_elf_backend_data (output_bfd);
   3045   esdo = elf_section_data (output_section);
   3046   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   3047     {
   3048       output_reldata = &esdo->rel;
   3049       swap_out = bed->s->swap_reloc_out;
   3050     }
   3051   else if (esdo->rela.hdr
   3052 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   3053     {
   3054       output_reldata = &esdo->rela;
   3055       swap_out = bed->s->swap_reloca_out;
   3056     }
   3057   else
   3058     {
   3059       _bfd_error_handler
   3060 	/* xgettext:c-format */
   3061 	(_("%pB: relocation size mismatch in %pB section %pA"),
   3062 	 output_bfd, input_section->owner, input_section);
   3063       bfd_set_error (bfd_error_wrong_format);
   3064       return false;
   3065     }
   3066 
   3067   erel = output_reldata->hdr->contents;
   3068   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   3069   irela = internal_relocs;
   3070   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   3071 		      * bed->s->int_rels_per_ext_rel);
   3072   while (irela < irelaend)
   3073     {
   3074       if (rel_hash && *rel_hash)
   3075 	(*rel_hash)->has_reloc = 1;
   3076       (*swap_out) (output_bfd, irela, erel);
   3077       irela += bed->s->int_rels_per_ext_rel;
   3078       erel += input_rel_hdr->sh_entsize;
   3079       if (rel_hash)
   3080 	rel_hash++;
   3081     }
   3082 
   3083   /* Bump the counter, so that we know where to add the next set of
   3084      relocations.  */
   3085   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   3086 
   3087   return true;
   3088 }
   3089 
   3090 /* Make weak undefined symbols in PIE dynamic.  */
   3092 
   3093 bool
   3094 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   3095 				 struct elf_link_hash_entry *h)
   3096 {
   3097   if (bfd_link_pie (info)
   3098       && h->dynindx == -1
   3099       && h->root.type == bfd_link_hash_undefweak)
   3100     return bfd_elf_link_record_dynamic_symbol (info, h);
   3101 
   3102   return true;
   3103 }
   3104 
   3105 /* Fix up the flags for a symbol.  This handles various cases which
   3106    can only be fixed after all the input files are seen.  This is
   3107    currently called by both adjust_dynamic_symbol and
   3108    assign_sym_version, which is unnecessary but perhaps more robust in
   3109    the face of future changes.  */
   3110 
   3111 static bool
   3112 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   3113 			   struct elf_info_failed *eif)
   3114 {
   3115   elf_backend_data *bed;
   3116 
   3117   /* If this symbol was mentioned in a non-ELF file, try to set
   3118      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   3119      permit a non-ELF file to correctly refer to a symbol defined in
   3120      an ELF dynamic object.  */
   3121   if (h->non_elf)
   3122     {
   3123       while (h->root.type == bfd_link_hash_indirect)
   3124 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3125 
   3126       if (h->root.type != bfd_link_hash_defined
   3127 	  && h->root.type != bfd_link_hash_defweak)
   3128 	{
   3129 	  h->ref_regular = 1;
   3130 	  h->ref_regular_nonweak = 1;
   3131 	}
   3132       else
   3133 	{
   3134 	  if (h->root.u.def.section->owner != NULL
   3135 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   3136 		  == bfd_target_elf_flavour))
   3137 	    {
   3138 	      h->ref_regular = 1;
   3139 	      h->ref_regular_nonweak = 1;
   3140 	    }
   3141 	  else
   3142 	    h->def_regular = 1;
   3143 	}
   3144 
   3145       if (h->dynindx == -1
   3146 	  && (h->def_dynamic
   3147 	      || h->ref_dynamic))
   3148 	{
   3149 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3150 	    {
   3151 	      eif->failed = true;
   3152 	      return false;
   3153 	    }
   3154 	}
   3155     }
   3156   else
   3157     {
   3158       /* Unfortunately, NON_ELF is only correct if the symbol
   3159 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   3160 	 was first seen in an ELF file, we're probably OK unless the
   3161 	 symbol was defined in a non-ELF file.  Catch that case here.
   3162 	 FIXME: We're still in trouble if the symbol was first seen in
   3163 	 a dynamic object, and then later in a non-ELF regular object.  */
   3164       if ((h->root.type == bfd_link_hash_defined
   3165 	   || h->root.type == bfd_link_hash_defweak)
   3166 	  && !h->def_regular
   3167 	  && (h->root.u.def.section->owner != NULL
   3168 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   3169 		 != bfd_target_elf_flavour)
   3170 	      : (bfd_is_abs_section (h->root.u.def.section)
   3171 		 && !h->def_dynamic)))
   3172 	h->def_regular = 1;
   3173     }
   3174 
   3175   /* Backend specific symbol fixup.  */
   3176   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   3177   if (bed->elf_backend_fixup_symbol
   3178       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   3179     return false;
   3180 
   3181   /* If this is a final link, and the symbol was defined as a common
   3182      symbol in a regular object file, and there was no definition in
   3183      any dynamic object, then the linker will have allocated space for
   3184      the symbol in a common section but the DEF_REGULAR
   3185      flag will not have been set.  */
   3186   if (h->root.type == bfd_link_hash_defined
   3187       && !h->def_regular
   3188       && h->ref_regular
   3189       && !h->def_dynamic
   3190       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   3191     h->def_regular = 1;
   3192 
   3193   /* Symbols defined in discarded sections shouldn't be dynamic.  */
   3194   if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
   3195     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3196 
   3197   /* If a weak undefined symbol has non-default visibility, we also
   3198      hide it from the dynamic linker.  */
   3199   else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   3200 	   && h->root.type == bfd_link_hash_undefweak)
   3201     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3202 
   3203   /* A hidden versioned symbol in executable should be forced local if
   3204      it is is locally defined, not referenced by shared library and not
   3205      exported.  */
   3206   else if (bfd_link_executable (eif->info)
   3207 	   && h->versioned == versioned_hidden
   3208 	   && !eif->info->export_dynamic
   3209 	   && !h->dynamic
   3210 	   && !h->ref_dynamic
   3211 	   && h->def_regular)
   3212     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3213 
   3214   /* If -Bsymbolic was used (which means to bind references to global
   3215      symbols to the definition within the shared object), and this
   3216      symbol was defined in a regular object, then it actually doesn't
   3217      need a PLT entry.  Likewise, if the symbol has non-default
   3218      visibility.  If the symbol has hidden or internal visibility, we
   3219      will force it local.  */
   3220   else if (h->needs_plt
   3221 	   && bfd_link_pic (eif->info)
   3222 	   && is_elf_hash_table (eif->info->hash)
   3223 	   && (SYMBOLIC_BIND (eif->info, h)
   3224 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   3225 	   && h->def_regular)
   3226     {
   3227       bool force_local;
   3228 
   3229       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   3230 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   3231       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   3232     }
   3233 
   3234   /* If this is a weak defined symbol in a dynamic object, and we know
   3235      the real definition in the dynamic object, copy interesting flags
   3236      over to the real definition.  */
   3237   if (h->is_weakalias)
   3238     {
   3239       struct elf_link_hash_entry *def = weakdef (h);
   3240       while (def->root.type == bfd_link_hash_indirect)
   3241         def = (struct elf_link_hash_entry *) def->root.u.i.link;
   3242 
   3243       /* If the real definition is defined by a regular object file,
   3244 	 don't do anything special.  See the longer description in
   3245 	 _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
   3246 	 bfd_link_hash_defined as it was when put on the alias list
   3247 	 then it must have originally been a versioned symbol (for
   3248 	 which a non-versioned indirect symbol is created) and later
   3249 	 a definition for the non-versioned symbol is found.  In that
   3250 	 case the indirection is flipped with the versioned symbol
   3251 	 becoming an indirect pointing at the non-versioned symbol.
   3252 	 Thus, not an alias any more.  */
   3253       if (def->def_regular
   3254 	  || def->root.type != bfd_link_hash_defined)
   3255 	{
   3256 	  h = def;
   3257 	  while ((h = h->u.alias) != def)
   3258 	    h->is_weakalias = 0;
   3259 	}
   3260       else
   3261 	{
   3262 	  while (h->root.type == bfd_link_hash_indirect)
   3263 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3264 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   3265 		      || h->root.type == bfd_link_hash_defweak);
   3266 	  BFD_ASSERT (def->def_dynamic);
   3267 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
   3268 	}
   3269     }
   3270 
   3271   return true;
   3272 }
   3273 
   3274 /* Make the backend pick a good value for a dynamic symbol.  This is
   3275    called via elf_link_hash_traverse, and also calls itself
   3276    recursively.  */
   3277 
   3278 static bool
   3279 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   3280 {
   3281   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   3282   struct elf_link_hash_table *htab;
   3283   elf_backend_data *bed;
   3284 
   3285   if (! is_elf_hash_table (eif->info->hash))
   3286     return false;
   3287 
   3288   htab = elf_hash_table (eif->info);
   3289   if (h->forced_local && h->dynindx != -1)
   3290     htab->has_local_dynsyms = true;
   3291 
   3292   /* Ignore indirect symbols.  These are added by the versioning code.  */
   3293   if (h->root.type == bfd_link_hash_indirect)
   3294     return true;
   3295 
   3296   /* Fix the symbol flags.  */
   3297   if (! _bfd_elf_fix_symbol_flags (h, eif))
   3298     return false;
   3299 
   3300   bed = get_elf_backend_data (htab->dynobj);
   3301 
   3302   if (h->root.type == bfd_link_hash_undefweak)
   3303     {
   3304       if (eif->info->dynamic_undefined_weak == 0)
   3305 	(*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3306       else if (eif->info->dynamic_undefined_weak > 0
   3307 	       && h->ref_regular
   3308 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3309 	       && !bfd_hide_sym_by_version (eif->info->version_info,
   3310 					    h->root.root.string))
   3311 	{
   3312 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3313 	    {
   3314 	      eif->failed = true;
   3315 	      return false;
   3316 	    }
   3317 	}
   3318     }
   3319 
   3320   /* If this symbol does not require a PLT entry, and it is not
   3321      defined by a dynamic object, or is not referenced by a regular
   3322      object, ignore it.  We do have to handle a weak defined symbol,
   3323      even if no regular object refers to it, if we decided to add it
   3324      to the dynamic symbol table.  FIXME: Do we normally need to worry
   3325      about symbols which are defined by one dynamic object and
   3326      referenced by another one?  */
   3327   if (!h->needs_plt
   3328       && h->type != STT_GNU_IFUNC
   3329       && (h->def_regular
   3330 	  || !h->def_dynamic
   3331 	  || (!h->ref_regular
   3332 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
   3333     {
   3334       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   3335       return true;
   3336     }
   3337 
   3338   /* If we've already adjusted this symbol, don't do it again.  This
   3339      can happen via a recursive call.  */
   3340   if (h->dynamic_adjusted)
   3341     return true;
   3342 
   3343   /* Don't look at this symbol again.  Note that we must set this
   3344      after checking the above conditions, because we may look at a
   3345      symbol once, decide not to do anything, and then get called
   3346      recursively later after REF_REGULAR is set below.  */
   3347   h->dynamic_adjusted = 1;
   3348 
   3349   /* If this is a weak definition, and we know a real definition, and
   3350      the real symbol is not itself defined by a regular object file,
   3351      then get a good value for the real definition.  We handle the
   3352      real symbol first, for the convenience of the backend routine.
   3353 
   3354      Note that there is a confusing case here.  If the real definition
   3355      is defined by a regular object file, we don't get the real symbol
   3356      from the dynamic object, but we do get the weak symbol.  If the
   3357      processor backend uses a COPY reloc, then if some routine in the
   3358      dynamic object changes the real symbol, we will not see that
   3359      change in the corresponding weak symbol.  This is the way other
   3360      ELF linkers work as well, and seems to be a result of the shared
   3361      library model.
   3362 
   3363      I will clarify this issue.  Most SVR4 shared libraries define the
   3364      variable _timezone and define timezone as a weak synonym.  The
   3365      tzset call changes _timezone.  If you write
   3366        extern int timezone;
   3367        int _timezone = 5;
   3368        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   3369      you might expect that, since timezone is a synonym for _timezone,
   3370      the same number will print both times.  However, if the processor
   3371      backend uses a COPY reloc, then actually timezone will be copied
   3372      into your process image, and, since you define _timezone
   3373      yourself, _timezone will not.  Thus timezone and _timezone will
   3374      wind up at different memory locations.  The tzset call will set
   3375      _timezone, leaving timezone unchanged.  */
   3376 
   3377   if (h->is_weakalias)
   3378     {
   3379       struct elf_link_hash_entry *def = weakdef (h);
   3380 
   3381       /* If we get to this point, there is an implicit reference to
   3382 	 the alias by a regular object file via the weak symbol H.  */
   3383       def->ref_regular = 1;
   3384 
   3385       /* Ensure that the backend adjust_dynamic_symbol function sees
   3386 	 the strong alias before H by recursively calling ourselves.  */
   3387       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
   3388 	return false;
   3389     }
   3390 
   3391   /* If a symbol has no type and no size and does not require a PLT
   3392      entry, then we are probably about to do the wrong thing here: we
   3393      are probably going to create a COPY reloc for an empty object.
   3394      This case can arise when a shared object is built with assembly
   3395      code, and the assembly code fails to set the symbol type.  */
   3396   if (h->size == 0
   3397       && h->type == STT_NOTYPE
   3398       && !h->needs_plt)
   3399     _bfd_error_handler
   3400       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   3401        h->root.root.string);
   3402 
   3403   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   3404     {
   3405       eif->failed = true;
   3406       return false;
   3407     }
   3408 
   3409   return true;
   3410 }
   3411 
   3412 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   3413    DYNBSS.  */
   3414 
   3415 bool
   3416 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   3417 			      struct elf_link_hash_entry *h,
   3418 			      asection *dynbss)
   3419 {
   3420   unsigned int power_of_two;
   3421   bfd_vma mask;
   3422   asection *sec = h->root.u.def.section;
   3423 
   3424   /* The section alignment of the definition is the maximum alignment
   3425      requirement of symbols defined in the section.  Since we don't
   3426      know the symbol alignment requirement, we start with the
   3427      maximum alignment and check low bits of the symbol address
   3428      for the minimum alignment.  */
   3429   power_of_two = bfd_section_alignment (sec);
   3430   mask = ((bfd_vma) 1 << power_of_two) - 1;
   3431   while ((h->root.u.def.value & mask) != 0)
   3432     {
   3433        mask >>= 1;
   3434        --power_of_two;
   3435     }
   3436 
   3437   /* Adjust the section alignment if needed.  */
   3438   if (!bfd_link_align_section (dynbss, power_of_two))
   3439     return false;
   3440 
   3441   /* We make sure that the symbol will be aligned properly.  */
   3442   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   3443 
   3444   /* Define the symbol as being at this point in DYNBSS.  */
   3445   h->root.u.def.section = dynbss;
   3446   h->root.u.def.value = dynbss->size;
   3447 
   3448   /* Increment the size of DYNBSS to make room for the symbol.  */
   3449   dynbss->size += h->size;
   3450 
   3451   /* No error if extern_protected_data is true.  */
   3452   if (h->protected_def
   3453       && (!info->extern_protected_data
   3454 	  || (info->extern_protected_data < 0
   3455 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   3456     info->callbacks->einfo
   3457       (_("%P: copy reloc against protected `%pT' is dangerous\n"),
   3458        h->root.root.string);
   3459 
   3460   return true;
   3461 }
   3462 
   3463 /* Adjust all external symbols pointing into SEC_MERGE sections
   3464    to reflect the object merging within the sections.  */
   3465 
   3466 static bool
   3467 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   3468 {
   3469   asection *sec;
   3470 
   3471   if ((h->root.type == bfd_link_hash_defined
   3472        || h->root.type == bfd_link_hash_defweak)
   3473       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   3474       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3475     {
   3476       bfd *output_bfd = (bfd *) data;
   3477 
   3478       h->root.u.def.value =
   3479 	_bfd_merged_section_offset (output_bfd,
   3480 				    &h->root.u.def.section,
   3481 				    h->root.u.def.value);
   3482     }
   3483 
   3484   return true;
   3485 }
   3486 
   3487 /* Returns false if the symbol referred to by H should be considered
   3488    to resolve local to the current module, and true if it should be
   3489    considered to bind dynamically.  */
   3490 
   3491 bool
   3492 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   3493 			   struct bfd_link_info *info,
   3494 			   bool not_local_protected)
   3495 {
   3496   bool binding_stays_local_p;
   3497   elf_backend_data *bed;
   3498   struct elf_link_hash_table *hash_table;
   3499 
   3500   if (h == NULL)
   3501     return false;
   3502 
   3503   while (h->root.type == bfd_link_hash_indirect
   3504 	 || h->root.type == bfd_link_hash_warning)
   3505     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3506 
   3507   /* If it was forced local, then clearly it's not dynamic.  */
   3508   if (h->dynindx == -1)
   3509     return false;
   3510   if (h->forced_local)
   3511     return false;
   3512 
   3513   /* Identify the cases where name binding rules say that a
   3514      visible symbol resolves locally.  */
   3515   binding_stays_local_p = (bfd_link_executable (info)
   3516 			   || SYMBOLIC_BIND (info, h));
   3517 
   3518   switch (ELF_ST_VISIBILITY (h->other))
   3519     {
   3520     case STV_INTERNAL:
   3521     case STV_HIDDEN:
   3522       return false;
   3523 
   3524     case STV_PROTECTED:
   3525       hash_table = elf_hash_table (info);
   3526       if (!is_elf_hash_table (&hash_table->root))
   3527 	return false;
   3528 
   3529       bed = get_elf_backend_data (hash_table->dynobj);
   3530 
   3531       /* Proper resolution for function pointer equality may require
   3532 	 that these symbols perhaps be resolved dynamically, even though
   3533 	 we should be resolving them to the current module.  */
   3534       if (!not_local_protected || !bed->is_function_type (h->type))
   3535 	binding_stays_local_p = true;
   3536       break;
   3537 
   3538     default:
   3539       break;
   3540     }
   3541 
   3542   /* If it isn't defined locally, then clearly it's dynamic.  */
   3543   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   3544     return true;
   3545 
   3546   /* Otherwise, the symbol is dynamic if binding rules don't tell
   3547      us that it remains local.  */
   3548   return !binding_stays_local_p;
   3549 }
   3550 
   3551 /* Return true if the symbol referred to by H should be considered
   3552    to resolve local to the current module, and false otherwise.  Differs
   3553    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   3554    undefined symbols.  The two functions are virtually identical except
   3555    for the place where dynindx == -1 is tested.  If that test is true,
   3556    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
   3557    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
   3558    defined symbols.
   3559    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   3560    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   3561    treatment of undefined weak symbols.  For those that do not make
   3562    undefined weak symbols dynamic, both functions may return false.  */
   3563 
   3564 bool
   3565 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   3566 			      struct bfd_link_info *info,
   3567 			      bool local_protected)
   3568 {
   3569   elf_backend_data *bed;
   3570   struct elf_link_hash_table *hash_table;
   3571 
   3572   /* If it's a local sym, of course we resolve locally.  */
   3573   if (h == NULL)
   3574     return true;
   3575 
   3576   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   3577   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   3578       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   3579     return true;
   3580 
   3581   /* Forced local symbols resolve locally.  */
   3582   if (h->forced_local)
   3583     return true;
   3584 
   3585   /* Common symbols that become definitions don't get the DEF_REGULAR
   3586      flag set, so test it first, and don't bail out.  */
   3587   if (ELF_COMMON_DEF_P (h))
   3588     /* Do nothing.  */;
   3589   /* If we don't have a definition in a regular file, then we can't
   3590      resolve locally.  The sym is either undefined or dynamic.  */
   3591   else if (!h->def_regular)
   3592     return false;
   3593 
   3594   /* Non-dynamic symbols resolve locally.  */
   3595   if (h->dynindx == -1)
   3596     return true;
   3597 
   3598   /* At this point, we know the symbol is defined and dynamic.  In an
   3599      executable it must resolve locally, likewise when building symbolic
   3600      shared libraries.  */
   3601   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3602     return true;
   3603 
   3604   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3605      with default visibility might not resolve locally.  */
   3606   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3607     return false;
   3608 
   3609   hash_table = elf_hash_table (info);
   3610   if (!is_elf_hash_table (&hash_table->root))
   3611     return true;
   3612 
   3613   /* STV_PROTECTED symbols with indirect external access are local. */
   3614   if (info->indirect_extern_access > 0)
   3615     return true;
   3616 
   3617   bed = get_elf_backend_data (hash_table->dynobj);
   3618 
   3619   /* If extern_protected_data is false, STV_PROTECTED non-function
   3620      symbols are local.  */
   3621   if ((!info->extern_protected_data
   3622        || (info->extern_protected_data < 0
   3623 	   && !bed->extern_protected_data))
   3624       && !bed->is_function_type (h->type))
   3625     return true;
   3626 
   3627   /* Function pointer equality tests may require that STV_PROTECTED
   3628      symbols be treated as dynamic symbols.  If the address of a
   3629      function not defined in an executable is set to that function's
   3630      plt entry in the executable, then the address of the function in
   3631      a shared library must also be the plt entry in the executable.  */
   3632   return local_protected;
   3633 }
   3634 
   3635 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3636    aligned.  Returns the first TLS output section.  */
   3637 
   3638 struct bfd_section *
   3639 bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3640 {
   3641   struct bfd_section *sec, *tls;
   3642   unsigned int align = 0;
   3643 
   3644   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3645     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3646       break;
   3647   tls = sec;
   3648 
   3649   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3650     if (sec->alignment_power > align)
   3651       align = sec->alignment_power;
   3652 
   3653   elf_hash_table (info)->tls_sec = tls;
   3654 
   3655   /* Ensure the alignment of the first section (usually .tdata) is the largest
   3656      alignment, so that the tls segment starts aligned.  */
   3657   if (tls != NULL)
   3658     (void) bfd_link_align_section (tls, align);
   3659 
   3660   return tls;
   3661 }
   3662 
   3663 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3664 static bool
   3665 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3666 				  Elf_Internal_Sym *sym)
   3667 {
   3668   elf_backend_data *bed;
   3669 
   3670   /* Local symbols do not count, but target specific ones might.  */
   3671   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3672       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3673     return false;
   3674 
   3675   bed = get_elf_backend_data (abfd);
   3676   /* Function symbols do not count.  */
   3677   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3678     return false;
   3679 
   3680   /* If the section is undefined, then so is the symbol.  */
   3681   if (sym->st_shndx == SHN_UNDEF)
   3682     return false;
   3683 
   3684   /* If the symbol is defined in the common section, then
   3685      it is a common definition and so does not count.  */
   3686   if (bed->common_definition (sym))
   3687     return false;
   3688 
   3689   /* If the symbol is in a target specific section then we
   3690      must rely upon the backend to tell us what it is.  */
   3691   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3692     /* FIXME - this function is not coded yet:
   3693 
   3694        return _bfd_is_global_symbol_definition (abfd, sym);
   3695 
   3696        Instead for now assume that the definition is not global,
   3697        Even if this is wrong, at least the linker will behave
   3698        in the same way that it used to do.  */
   3699     return false;
   3700 
   3701   return true;
   3702 }
   3703 
   3704 /* Search the symbol table of the archive element of the archive ABFD
   3705    whose archive map contains a mention of SYMDEF, and determine if
   3706    the symbol is defined in this element.  */
   3707 static bool
   3708 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3709 {
   3710   Elf_Internal_Shdr * hdr;
   3711   size_t symcount;
   3712   size_t extsymcount;
   3713   size_t extsymoff;
   3714   Elf_Internal_Sym *isymbuf;
   3715   Elf_Internal_Sym *isym;
   3716   Elf_Internal_Sym *isymend;
   3717   bool result;
   3718 
   3719   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
   3720   if (abfd == NULL)
   3721     return false;
   3722 
   3723   if (! bfd_check_format (abfd, bfd_object))
   3724     return false;
   3725 
   3726   /* Select the appropriate symbol table.  If we don't know if the
   3727      object file is an IR object, give linker LTO plugin a chance to
   3728      get the correct symbol table.  */
   3729   if (abfd->plugin_format == bfd_plugin_yes
   3730       || abfd->plugin_format == bfd_plugin_yes_unused
   3731       || (abfd->plugin_format == bfd_plugin_unknown
   3732 	  && bfd_link_plugin_object_p (abfd)))
   3733     {
   3734       /* Use the IR symbol table if the object has been claimed by
   3735 	 plugin.  */
   3736       abfd = abfd->plugin_dummy_bfd;
   3737       hdr = &elf_tdata (abfd)->symtab_hdr;
   3738     }
   3739   else
   3740     {
   3741       if (elf_use_dt_symtab_p (abfd))
   3742 	{
   3743 	  bfd_set_error (bfd_error_wrong_format);
   3744 	  return false;
   3745 	}
   3746 
   3747       if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3748 	hdr = &elf_tdata (abfd)->symtab_hdr;
   3749       else
   3750 	hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3751     }
   3752 
   3753   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3754 
   3755   /* The sh_info field of the symtab header tells us where the
   3756      external symbols start.  We don't care about the local symbols.  */
   3757   if (elf_bad_symtab (abfd))
   3758     {
   3759       extsymcount = symcount;
   3760       extsymoff = 0;
   3761     }
   3762   else
   3763     {
   3764       extsymcount = symcount - hdr->sh_info;
   3765       extsymoff = hdr->sh_info;
   3766     }
   3767 
   3768   if (extsymcount == 0)
   3769     return false;
   3770 
   3771   /* Read in the symbol table.  */
   3772   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3773 				  NULL, NULL, NULL);
   3774   if (isymbuf == NULL)
   3775     return false;
   3776 
   3777   /* Scan the symbol table looking for SYMDEF.  */
   3778   result = false;
   3779   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3780     {
   3781       const char *name;
   3782 
   3783       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3784 					      isym->st_name);
   3785       if (name == NULL)
   3786 	break;
   3787 
   3788       if (strcmp (name, symdef->name) == 0)
   3789 	{
   3790 	  result = is_global_data_symbol_definition (abfd, isym);
   3791 	  break;
   3792 	}
   3793     }
   3794 
   3795   free (isymbuf);
   3796 
   3797   return result;
   3798 }
   3799 
   3800 /* Add an entry to the .dynamic table.  */
   3802 
   3803 bool
   3804 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3805 			    bfd_vma tag,
   3806 			    bfd_vma val)
   3807 {
   3808   struct elf_link_hash_table *hash_table;
   3809   elf_backend_data *bed;
   3810   asection *s;
   3811   bfd_size_type newsize;
   3812   bfd_byte *newcontents;
   3813   Elf_Internal_Dyn dyn;
   3814 
   3815   hash_table = elf_hash_table (info);
   3816   if (! is_elf_hash_table (&hash_table->root))
   3817     return false;
   3818 
   3819   if (tag == DT_RELA || tag == DT_REL)
   3820     hash_table->dynamic_relocs = true;
   3821 
   3822   bed = get_elf_backend_data (hash_table->dynobj);
   3823   s = hash_table->dynamic;
   3824   BFD_ASSERT (s != NULL);
   3825 
   3826   newsize = s->size + bed->s->sizeof_dyn;
   3827   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3828   if (newcontents == NULL)
   3829     return false;
   3830 
   3831   dyn.d_tag = tag;
   3832   dyn.d_un.d_val = val;
   3833   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3834 
   3835   s->size = newsize;
   3836   s->contents = newcontents;
   3837 
   3838   return true;
   3839 }
   3840 
   3841 /* Strip zero-sized dynamic sections.  */
   3842 
   3843 bool
   3844 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
   3845 {
   3846   struct elf_link_hash_table *hash_table;
   3847   elf_backend_data *bed;
   3848   asection *s, *sdynamic, **pp;
   3849   asection *rela_dyn, *rel_dyn;
   3850   Elf_Internal_Dyn dyn;
   3851   bfd_byte *extdyn, *next;
   3852   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3853   bool strip_zero_sized;
   3854   bool strip_zero_sized_plt;
   3855 
   3856   if (bfd_link_relocatable (info))
   3857     return true;
   3858 
   3859   hash_table = elf_hash_table (info);
   3860   if (!is_elf_hash_table (&hash_table->root))
   3861     return false;
   3862 
   3863   if (!hash_table->dynobj)
   3864     return true;
   3865 
   3866   sdynamic= hash_table->dynamic;
   3867   if (!sdynamic)
   3868     return true;
   3869 
   3870   bed = get_elf_backend_data (hash_table->dynobj);
   3871   swap_dyn_in = bed->s->swap_dyn_in;
   3872 
   3873   strip_zero_sized = false;
   3874   strip_zero_sized_plt = false;
   3875 
   3876   /* Strip zero-sized dynamic sections.  */
   3877   rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
   3878   rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
   3879   for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
   3880     if (s->size == 0
   3881 	&& (s == rela_dyn
   3882 	    || s == rel_dyn
   3883 	    || s == hash_table->srelplt->output_section
   3884 	    || s == hash_table->splt->output_section))
   3885       {
   3886 	*pp = s->next;
   3887 	info->output_bfd->section_count--;
   3888 	strip_zero_sized = true;
   3889 	if (s == rela_dyn)
   3890 	  s = rela_dyn;
   3891 	if (s == rel_dyn)
   3892 	  s = rel_dyn;
   3893 	else if (s == hash_table->splt->output_section)
   3894 	  {
   3895 	    s = hash_table->splt;
   3896 	    strip_zero_sized_plt = true;
   3897 	  }
   3898 	else
   3899 	  s = hash_table->srelplt;
   3900 	s->flags |= SEC_EXCLUDE;
   3901 	s->output_section = bfd_abs_section_ptr;
   3902       }
   3903     else
   3904       pp = &s->next;
   3905 
   3906   if (strip_zero_sized_plt && sdynamic->size != 0)
   3907     for (extdyn = sdynamic->contents;
   3908 	 extdyn < sdynamic->contents + sdynamic->size;
   3909 	 extdyn = next)
   3910       {
   3911 	next = extdyn + bed->s->sizeof_dyn;
   3912 	swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3913 	switch (dyn.d_tag)
   3914 	  {
   3915 	  default:
   3916 	    break;
   3917 	  case DT_JMPREL:
   3918 	  case DT_PLTRELSZ:
   3919 	  case DT_PLTREL:
   3920 	    /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
   3921 	       the procedure linkage table (the .plt section) has been
   3922 	       removed.  */
   3923 	    memmove (extdyn, next,
   3924 		     sdynamic->size - (next - sdynamic->contents));
   3925 	    next = extdyn;
   3926 	  }
   3927       }
   3928 
   3929   if (strip_zero_sized)
   3930     {
   3931       /* Regenerate program headers.  */
   3932       elf_seg_map (info->output_bfd) = NULL;
   3933       return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
   3934     }
   3935 
   3936   return true;
   3937 }
   3938 
   3939 /* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
   3940    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3941 
   3942 int
   3943 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
   3944 {
   3945   struct elf_link_hash_table *hash_table;
   3946   size_t strindex;
   3947   const char *soname;
   3948 
   3949   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3950     return -1;
   3951 
   3952   hash_table = elf_hash_table (info);
   3953   soname = elf_dt_name (abfd);
   3954   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
   3955   if (strindex == (size_t) -1)
   3956     return -1;
   3957 
   3958   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3959     {
   3960       asection *sdyn;
   3961       elf_backend_data *bed;
   3962       bfd_byte *extdyn;
   3963 
   3964       bed = get_elf_backend_data (hash_table->dynobj);
   3965       sdyn = hash_table->dynamic;
   3966       if (sdyn != NULL && sdyn->size != 0)
   3967 	for (extdyn = sdyn->contents;
   3968 	     extdyn < sdyn->contents + sdyn->size;
   3969 	     extdyn += bed->s->sizeof_dyn)
   3970 	  {
   3971 	    Elf_Internal_Dyn dyn;
   3972 
   3973 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3974 	    if (dyn.d_tag == DT_NEEDED
   3975 		&& dyn.d_un.d_val == strindex)
   3976 	      {
   3977 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3978 		return 1;
   3979 	      }
   3980 	  }
   3981     }
   3982 
   3983   if (!bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3984     return -1;
   3985 
   3986   if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3987     return -1;
   3988 
   3989   return 0;
   3990 }
   3991 
   3992 /* Return true if SONAME is on the needed list between NEEDED and STOP
   3993    (or the end of list if STOP is NULL), and needed by a library that
   3994    will be loaded.  */
   3995 
   3996 static bool
   3997 on_needed_list (const char *soname,
   3998 		struct bfd_link_needed_list *needed,
   3999 		struct bfd_link_needed_list *stop)
   4000 {
   4001   struct bfd_link_needed_list *look;
   4002   for (look = needed; look != stop; look = look->next)
   4003     if (strcmp (soname, look->name) == 0
   4004 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
   4005 	    /* If needed by a library that itself is not directly
   4006 	       needed, recursively check whether that library is
   4007 	       indirectly needed.  Since we add DT_NEEDED entries to
   4008 	       the end of the list, library dependencies appear after
   4009 	       the library.  Therefore search prior to the current
   4010 	       LOOK, preventing possible infinite recursion.  */
   4011 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
   4012       return true;
   4013 
   4014   return false;
   4015 }
   4016 
   4017 /* Sort symbol by value, section, size, and type.  */
   4018 static int
   4019 elf_sort_symbol (const void *arg1, const void *arg2)
   4020 {
   4021   const struct elf_link_hash_entry *h1;
   4022   const struct elf_link_hash_entry *h2;
   4023   bfd_signed_vma vdiff;
   4024   int sdiff;
   4025   const char *n1;
   4026   const char *n2;
   4027 
   4028   h1 = *(const struct elf_link_hash_entry **) arg1;
   4029   h2 = *(const struct elf_link_hash_entry **) arg2;
   4030   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   4031   if (vdiff != 0)
   4032     return vdiff > 0 ? 1 : -1;
   4033 
   4034   sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   4035   if (sdiff != 0)
   4036     return sdiff;
   4037 
   4038   /* Sort so that sized symbols are selected over zero size symbols.  */
   4039   vdiff = h1->size - h2->size;
   4040   if (vdiff != 0)
   4041     return vdiff > 0 ? 1 : -1;
   4042 
   4043   /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
   4044   if (h1->type != h2->type)
   4045     return h1->type - h2->type;
   4046 
   4047   /* If symbols are properly sized and typed, and multiple strong
   4048      aliases are not defined in a shared library by the user we
   4049      shouldn't get here.  Unfortunately linker script symbols like
   4050      __bss_start sometimes match a user symbol defined at the start of
   4051      .bss without proper size and type.  We'd like to preference the
   4052      user symbol over reserved system symbols.  Sort on leading
   4053      underscores.  */
   4054   n1 = h1->root.root.string;
   4055   n2 = h2->root.root.string;
   4056   while (*n1 == *n2)
   4057     {
   4058       if (*n1 == 0)
   4059 	break;
   4060       ++n1;
   4061       ++n2;
   4062     }
   4063   if (*n1 == '_')
   4064     return -1;
   4065   if (*n2 == '_')
   4066     return 1;
   4067 
   4068   /* Final sort on name selects user symbols like '_u' over reserved
   4069      system symbols like '_Z' and also will avoid qsort instability.  */
   4070   return *n1 - *n2;
   4071 }
   4072 
   4073 /* This function is used to adjust offsets into .dynstr for
   4074    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   4075 
   4076 static bool
   4077 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   4078 {
   4079   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   4080 
   4081   if (h->dynindx != -1)
   4082     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   4083   return true;
   4084 }
   4085 
   4086 /* Assign string offsets in .dynstr, update all structures referencing
   4087    them.  */
   4088 
   4089 static bool
   4090 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   4091 {
   4092   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   4093   struct elf_link_local_dynamic_entry *entry;
   4094   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   4095   bfd *dynobj = hash_table->dynobj;
   4096   asection *sdyn;
   4097   bfd_size_type size;
   4098   elf_backend_data *bed;
   4099   bfd_byte *extdyn;
   4100 
   4101   _bfd_elf_strtab_finalize (dynstr);
   4102   size = _bfd_elf_strtab_size (dynstr);
   4103 
   4104   /* Allow the linker to examine the dynsymtab now it's fully populated.  */
   4105 
   4106   if (info->callbacks->examine_strtab)
   4107     info->callbacks->examine_strtab (dynstr);
   4108 
   4109   bed = get_elf_backend_data (dynobj);
   4110   sdyn = hash_table->dynamic;
   4111   BFD_ASSERT (sdyn != NULL);
   4112 
   4113   /* Update all .dynamic entries referencing .dynstr strings.  */
   4114   for (extdyn = sdyn->contents;
   4115        extdyn < PTR_ADD (sdyn->contents, sdyn->size);
   4116        extdyn += bed->s->sizeof_dyn)
   4117     {
   4118       Elf_Internal_Dyn dyn;
   4119 
   4120       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   4121       switch (dyn.d_tag)
   4122 	{
   4123 	case DT_STRSZ:
   4124 	  dyn.d_un.d_val = size;
   4125 	  break;
   4126 	case DT_NEEDED:
   4127 	case DT_SONAME:
   4128 	case DT_RPATH:
   4129 	case DT_RUNPATH:
   4130 	case DT_FILTER:
   4131 	case DT_AUXILIARY:
   4132 	case DT_AUDIT:
   4133 	case DT_DEPAUDIT:
   4134 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   4135 	  break;
   4136 	default:
   4137 	  continue;
   4138 	}
   4139       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   4140     }
   4141 
   4142   /* Now update local dynamic symbols.  */
   4143   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   4144     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   4145 						  entry->isym.st_name);
   4146 
   4147   /* And the rest of dynamic symbols.  */
   4148   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   4149 
   4150   /* Adjust version definitions.  */
   4151   if (elf_tdata (output_bfd)->cverdefs)
   4152     {
   4153       asection *s;
   4154       bfd_byte *p;
   4155       size_t i;
   4156       Elf_Internal_Verdef def;
   4157       Elf_Internal_Verdaux defaux;
   4158 
   4159       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   4160       p = s->contents;
   4161       do
   4162 	{
   4163 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   4164 				   &def);
   4165 	  p += sizeof (Elf_External_Verdef);
   4166 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   4167 	    continue;
   4168 	  for (i = 0; i < def.vd_cnt; ++i)
   4169 	    {
   4170 	      _bfd_elf_swap_verdaux_in (output_bfd,
   4171 					(Elf_External_Verdaux *) p, &defaux);
   4172 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   4173 							defaux.vda_name);
   4174 	      _bfd_elf_swap_verdaux_out (output_bfd,
   4175 					 &defaux, (Elf_External_Verdaux *) p);
   4176 	      p += sizeof (Elf_External_Verdaux);
   4177 	    }
   4178 	}
   4179       while (def.vd_next);
   4180     }
   4181 
   4182   /* Adjust version references.  */
   4183   if (elf_tdata (output_bfd)->verref)
   4184     {
   4185       asection *s;
   4186       bfd_byte *p;
   4187       size_t i;
   4188       Elf_Internal_Verneed need;
   4189       Elf_Internal_Vernaux needaux;
   4190 
   4191       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   4192       p = s->contents;
   4193       do
   4194 	{
   4195 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   4196 				    &need);
   4197 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   4198 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   4199 				     (Elf_External_Verneed *) p);
   4200 	  p += sizeof (Elf_External_Verneed);
   4201 	  for (i = 0; i < need.vn_cnt; ++i)
   4202 	    {
   4203 	      _bfd_elf_swap_vernaux_in (output_bfd,
   4204 					(Elf_External_Vernaux *) p, &needaux);
   4205 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   4206 							 needaux.vna_name);
   4207 	      _bfd_elf_swap_vernaux_out (output_bfd,
   4208 					 &needaux,
   4209 					 (Elf_External_Vernaux *) p);
   4210 	      p += sizeof (Elf_External_Vernaux);
   4211 	    }
   4212 	}
   4213       while (need.vn_next);
   4214     }
   4215 
   4216   return true;
   4217 }
   4218 
   4219 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4221    The default is to only match when the INPUT and OUTPUT are exactly
   4222    the same target.  */
   4223 
   4224 bool
   4225 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   4226 				    const bfd_target *output)
   4227 {
   4228   return input == output;
   4229 }
   4230 
   4231 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4232    This version is used when different targets for the same architecture
   4233    are virtually identical.  */
   4234 
   4235 bool
   4236 _bfd_elf_relocs_compatible (const bfd_target *input,
   4237 			    const bfd_target *output)
   4238 {
   4239   elf_backend_data *obed, *ibed;
   4240 
   4241   if (input == output)
   4242     return true;
   4243 
   4244   ibed = xvec_get_elf_backend_data (input);
   4245   obed = xvec_get_elf_backend_data (output);
   4246 
   4247   if (ibed->arch != obed->arch)
   4248     return false;
   4249 
   4250   /* If both backends are using this function, deem them compatible.  */
   4251   return ibed->relocs_compatible == obed->relocs_compatible;
   4252 }
   4253 
   4254 /* Make a special call to the linker "notice" function to tell it that
   4255    we are about to handle an as-needed lib, or have finished
   4256    processing the lib.  */
   4257 
   4258 bool
   4259 _bfd_elf_notice_as_needed (bfd *ibfd,
   4260 			   struct bfd_link_info *info,
   4261 			   enum notice_asneeded_action act)
   4262 {
   4263   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   4264 }
   4265 
   4266 /* Call ACTION on each relocation in an ELF object file.  */
   4267 
   4268 bool
   4269 _bfd_elf_link_iterate_on_relocs
   4270   (bfd *abfd, struct bfd_link_info *info,
   4271    bool (*action) (bfd *, struct bfd_link_info *, asection *,
   4272 		   const Elf_Internal_Rela *))
   4273 {
   4274   elf_backend_data *bed = get_elf_backend_data (abfd);
   4275   struct elf_link_hash_table *htab = elf_hash_table (info);
   4276 
   4277   /* If this object is the same format as the output object, and it is
   4278      not a shared library, then let the backend look through the
   4279      relocs.
   4280 
   4281      This is required to build global offset table entries and to
   4282      arrange for dynamic relocs.  It is not required for the
   4283      particular common case of linking non PIC code, even when linking
   4284      against shared libraries, but unfortunately there is no way of
   4285      knowing whether an object file has been compiled PIC or not.
   4286      Looking through the relocs is not particularly time consuming.
   4287      The problem is that we must either (1) keep the relocs in memory,
   4288      which causes the linker to require additional runtime memory or
   4289      (2) read the relocs twice from the input file, which wastes time.
   4290      This would be a good case for using mmap.
   4291 
   4292      I have no idea how to handle linking PIC code into a file of a
   4293      different format.  It probably can't be done.  */
   4294   if ((abfd->flags & DYNAMIC) == 0
   4295       && is_elf_hash_table (&htab->root)
   4296       && elf_object_id (abfd) == elf_hash_table_id (htab)
   4297       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   4298     {
   4299       asection *o;
   4300 
   4301       for (o = abfd->sections; o != NULL; o = o->next)
   4302 	{
   4303 	  Elf_Internal_Rela *internal_relocs;
   4304 	  bool ok;
   4305 
   4306 	  /* Don't check relocations in excluded sections.  Don't do
   4307 	     anything special with non-loaded, non-alloced sections.
   4308 	     In particular, any relocs in such sections should not
   4309 	     affect GOT and PLT reference counting (ie.  we don't
   4310 	     allow them to create GOT or PLT entries), there's no
   4311 	     possibility or desire to optimize TLS relocs, and
   4312 	     there's not much point in propagating relocs to shared
   4313 	     libs that the dynamic linker won't relocate.  */
   4314 	  if ((o->flags & SEC_ALLOC) == 0
   4315 	      || (o->flags & SEC_RELOC) == 0
   4316 	      || (o->flags & SEC_EXCLUDE) != 0
   4317 	      || o->reloc_count == 0
   4318 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   4319 		  && (o->flags & SEC_DEBUGGING) != 0)
   4320 	      || bfd_is_abs_section (o->output_section))
   4321 	    continue;
   4322 
   4323 	  internal_relocs = _bfd_elf_link_info_read_relocs
   4324 	    (abfd, info, o, NULL, NULL,
   4325 	     _bfd_elf_link_keep_memory (info));
   4326 	  if (internal_relocs == NULL)
   4327 	    return false;
   4328 
   4329 	  ok = action (abfd, info, o, internal_relocs);
   4330 
   4331 	  if (elf_section_data (o)->relocs != internal_relocs)
   4332 	    free (internal_relocs);
   4333 
   4334 	  if (! ok)
   4335 	    return false;
   4336 	}
   4337     }
   4338 
   4339   return true;
   4340 }
   4341 
   4342 /* Check relocations in an ELF object file.  This is called after
   4343    all input files have been opened.  */
   4344 
   4345 bool
   4346 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   4347 {
   4348   elf_backend_data *bed = get_elf_backend_data (abfd);
   4349   if (bed->check_relocs != NULL)
   4350     return _bfd_elf_link_iterate_on_relocs (abfd, info,
   4351 					    bed->check_relocs);
   4352   return true;
   4353 }
   4354 
   4355 /* An entry in the first definition hash table.  */
   4356 
   4357 struct elf_link_first_hash_entry
   4358 {
   4359   struct bfd_hash_entry root;
   4360   /* The object of the first definition.  */
   4361   bfd *abfd;
   4362 };
   4363 
   4364 /* The function to create a new entry in the first definition hash
   4365    table.  */
   4366 
   4367 static struct bfd_hash_entry *
   4368 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
   4369 			     struct bfd_hash_table *table,
   4370 			     const char *string)
   4371 {
   4372   struct elf_link_first_hash_entry *ret =
   4373     (struct elf_link_first_hash_entry *) entry;
   4374 
   4375   /* Allocate the structure if it has not already been allocated by a
   4376      subclass.  */
   4377   if (ret == NULL)
   4378     ret = (struct elf_link_first_hash_entry *)
   4379 	bfd_hash_allocate (table,
   4380 			   sizeof (struct elf_link_first_hash_entry));
   4381   if (ret == NULL)
   4382     return NULL;
   4383 
   4384   /* Call the allocation method of the superclass.  */
   4385   ret = ((struct elf_link_first_hash_entry *)
   4386 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
   4387 			   string));
   4388   if (ret != NULL)
   4389     ret->abfd = NULL;
   4390 
   4391   return (struct bfd_hash_entry *) ret;
   4392 }
   4393 
   4394 /* Add the symbol NAME from ABFD to first hash.  */
   4395 
   4396 static void
   4397 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
   4398 			    const char *name, bool copy)
   4399 {
   4400   struct elf_link_hash_table *htab = elf_hash_table (info);
   4401   /* Skip if there is no first hash.  */
   4402   if (htab->first_hash == NULL)
   4403     return;
   4404 
   4405   struct elf_link_first_hash_entry *e
   4406     = ((struct elf_link_first_hash_entry *)
   4407        bfd_hash_lookup (htab->first_hash, name, true, copy));
   4408   if (e == NULL)
   4409     info->callbacks->fatal
   4410       (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
   4411 
   4412   if (e->abfd == NULL)
   4413     /* Store ABFD in abfd.  */
   4414     e->abfd = abfd;
   4415 }
   4416 
   4417 /* Add symbols from an ELF object file to the linker hash table.  */
   4418 
   4419 static bool
   4420 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   4421 {
   4422   Elf_Internal_Ehdr *ehdr;
   4423   Elf_Internal_Shdr *hdr;
   4424   size_t symcount;
   4425   size_t extsymcount;
   4426   size_t extsymoff;
   4427   struct elf_link_hash_entry **sym_hash;
   4428   bool dynamic;
   4429   Elf_External_Versym *extversym = NULL;
   4430   Elf_External_Versym *extversym_end = NULL;
   4431   Elf_External_Versym *ever;
   4432   struct elf_link_hash_entry *weaks;
   4433   struct elf_link_hash_entry **nondeflt_vers = NULL;
   4434   size_t nondeflt_vers_cnt = 0;
   4435   Elf_Internal_Sym *isymbuf = NULL;
   4436   Elf_Internal_Sym *isym;
   4437   Elf_Internal_Sym *isymend;
   4438   elf_backend_data *bed;
   4439   bool add_needed;
   4440   struct elf_link_hash_table *htab;
   4441   void *alloc_mark = NULL;
   4442   struct bfd_hash_entry **old_table = NULL;
   4443   unsigned int old_size = 0;
   4444   unsigned int old_count = 0;
   4445   void *old_tab = NULL;
   4446   void *old_ent;
   4447   struct bfd_link_hash_entry *old_undefs = NULL;
   4448   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   4449   void *old_strtab = NULL;
   4450   size_t tabsize = 0;
   4451   asection *s;
   4452   bool just_syms;
   4453 
   4454   htab = elf_hash_table (info);
   4455   bed = get_elf_backend_data (abfd);
   4456 
   4457   if (elf_use_dt_symtab_p (abfd))
   4458     {
   4459       bfd_set_error (bfd_error_wrong_format);
   4460       return false;
   4461     }
   4462 
   4463   if ((abfd->flags & DYNAMIC) == 0)
   4464     {
   4465       dynamic = false;
   4466       if ((abfd->flags & BFD_PLUGIN) != 0
   4467 	  && is_elf_hash_table (&htab->root)
   4468 	  && htab->first_hash == NULL)
   4469 	{
   4470 	  /* Initialize first_hash for an IR input.  */
   4471 	  htab->first_hash = (struct bfd_hash_table *)
   4472 	    bfd_malloc (sizeof (struct bfd_hash_table));
   4473 	  if (htab->first_hash == NULL
   4474 	      || !bfd_hash_table_init
   4475 		   (htab->first_hash, elf_link_first_hash_newfunc,
   4476 		    sizeof (struct elf_link_first_hash_entry)))
   4477 	    info->callbacks->fatal
   4478 	      (_("%P: first_hash failed to create: %E\n"));
   4479 	}
   4480     }
   4481   else
   4482     {
   4483       dynamic = true;
   4484 
   4485       /* You can't use -r against a dynamic object.  Also, there's no
   4486 	 hope of using a dynamic object which does not exactly match
   4487 	 the format of the output file.  */
   4488       if (bfd_link_relocatable (info)
   4489 	  || !is_elf_hash_table (&htab->root)
   4490 	  || info->output_bfd->xvec != abfd->xvec)
   4491 	{
   4492 	  if (bfd_link_relocatable (info))
   4493 	    bfd_set_error (bfd_error_invalid_operation);
   4494 	  else
   4495 	    bfd_set_error (bfd_error_wrong_format);
   4496 	  goto error_return;
   4497 	}
   4498     }
   4499 
   4500   ehdr = elf_elfheader (abfd);
   4501   if (info->warn_alternate_em
   4502       && bed->elf_machine_code != ehdr->e_machine
   4503       && ((bed->elf_machine_alt1 != 0
   4504 	   && ehdr->e_machine == bed->elf_machine_alt1)
   4505 	  || (bed->elf_machine_alt2 != 0
   4506 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   4507     _bfd_error_handler
   4508       /* xgettext:c-format */
   4509       (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
   4510        ehdr->e_machine, abfd, bed->elf_machine_code);
   4511 
   4512   /* As a GNU extension, any input sections which are named
   4513      .gnu.warning.SYMBOL are treated as warning symbols for the given
   4514      symbol.  This differs from .gnu.warning sections, which generate
   4515      warnings when they are included in an output file.  */
   4516   /* PR 12761: Also generate this warning when building shared libraries.  */
   4517   for (s = abfd->sections; s != NULL; s = s->next)
   4518     {
   4519       const char *name;
   4520 
   4521       name = bfd_section_name (s);
   4522       if (startswith (name, ".gnu.warning."))
   4523 	{
   4524 	  char *msg;
   4525 	  bfd_size_type sz;
   4526 
   4527 	  name += sizeof ".gnu.warning." - 1;
   4528 
   4529 	  /* If this is a shared object, then look up the symbol
   4530 	     in the hash table.  If it is there, and it is already
   4531 	     been defined, then we will not be using the entry
   4532 	     from this shared object, so we don't need to warn.
   4533 	     FIXME: If we see the definition in a regular object
   4534 	     later on, we will warn, but we shouldn't.  The only
   4535 	     fix is to keep track of what warnings we are supposed
   4536 	     to emit, and then handle them all at the end of the
   4537 	     link.  */
   4538 	  if (dynamic)
   4539 	    {
   4540 	      struct elf_link_hash_entry *h;
   4541 
   4542 	      h = elf_link_hash_lookup (htab, name, false, false, true);
   4543 
   4544 	      /* FIXME: What about bfd_link_hash_common?  */
   4545 	      if (h != NULL
   4546 		  && (h->root.type == bfd_link_hash_defined
   4547 		      || h->root.type == bfd_link_hash_defweak))
   4548 		continue;
   4549 	    }
   4550 
   4551 	  sz = s->size;
   4552 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   4553 	  if (msg == NULL)
   4554 	    goto error_return;
   4555 
   4556 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   4557 	    goto error_return;
   4558 
   4559 	  msg[sz] = '\0';
   4560 
   4561 	  if (! (_bfd_generic_link_add_one_symbol
   4562 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   4563 		  false, bed->collect, NULL)))
   4564 	    goto error_return;
   4565 
   4566 	  if (bfd_link_executable (info))
   4567 	    {
   4568 	      /* Clobber the section size so that the warning does
   4569 		 not get copied into the output file.  */
   4570 	      s->size = 0;
   4571 
   4572 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   4573 		 the warning section don't get copied to the output.  */
   4574 	      s->flags |= SEC_EXCLUDE;
   4575 	    }
   4576 	}
   4577     }
   4578 
   4579   just_syms = ((s = abfd->sections) != NULL
   4580 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   4581 
   4582   add_needed = true;
   4583   if (! dynamic)
   4584     {
   4585       /* If we are creating a shared library, create all the dynamic
   4586 	 sections immediately.  We need to attach them to something,
   4587 	 so we attach them to this BFD, provided it is the right
   4588 	 format and is not from ld --just-symbols.  Always create the
   4589 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
   4590 	 are no input BFD's of the same format as the output, we can't
   4591 	 make a shared library.  */
   4592       if (!just_syms
   4593 	  && (bfd_link_pic (info)
   4594 	      || (!bfd_link_relocatable (info)
   4595 		  && info->nointerp
   4596 		  && (info->export_dynamic || info->dynamic)))
   4597 	  && is_elf_hash_table (&htab->root)
   4598 	  && info->output_bfd->xvec == abfd->xvec
   4599 	  && !htab->dynamic_sections_created)
   4600 	{
   4601 	  if (!bfd_elf_link_create_dynamic_sections (abfd, info))
   4602 	    goto error_return;
   4603 	}
   4604     }
   4605   else if (!is_elf_hash_table (&htab->root))
   4606     goto error_return;
   4607   else
   4608     {
   4609       const char *soname = NULL;
   4610       char *audit = NULL;
   4611       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   4612       const Elf_Internal_Phdr *phdr;
   4613       struct elf_link_loaded_list *loaded_lib;
   4614 
   4615       /* ld --just-symbols and dynamic objects don't mix very well.
   4616 	 ld shouldn't allow it.  */
   4617       if (just_syms)
   4618 	abort ();
   4619 
   4620       /* If this dynamic lib was specified on the command line with
   4621 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   4622 	 tag unless the lib is actually used.  Similary for libs brought
   4623 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   4624 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   4625 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   4626 	 all.  */
   4627       add_needed = (elf_dyn_lib_class (abfd)
   4628 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   4629 		       | DYN_NO_NEEDED)) == 0;
   4630 
   4631       s = bfd_get_section_by_name (abfd, ".dynamic");
   4632       if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
   4633 	{
   4634 	  bfd_byte *dynbuf;
   4635 	  bfd_byte *extdyn;
   4636 	  unsigned int elfsec;
   4637 	  unsigned long shlink;
   4638 
   4639 	  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   4640 	    {
   4641 	    error_free_dyn:
   4642 	      _bfd_elf_munmap_section_contents (s, dynbuf);
   4643 	      goto error_return;
   4644 	    }
   4645 
   4646 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   4647 	  if (elfsec == SHN_BAD)
   4648 	    goto error_free_dyn;
   4649 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   4650 
   4651 	  for (extdyn = dynbuf;
   4652 	       (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
   4653 	       extdyn += bed->s->sizeof_dyn)
   4654 	    {
   4655 	      Elf_Internal_Dyn dyn;
   4656 
   4657 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   4658 	      if (dyn.d_tag == DT_SONAME)
   4659 		{
   4660 		  unsigned int tagv = dyn.d_un.d_val;
   4661 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4662 		  if (soname == NULL)
   4663 		    goto error_free_dyn;
   4664 		}
   4665 	      if (dyn.d_tag == DT_NEEDED)
   4666 		{
   4667 		  struct bfd_link_needed_list *n, **pn;
   4668 		  char *fnm, *anm;
   4669 		  unsigned int tagv = dyn.d_un.d_val;
   4670 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4671 
   4672 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4673 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4674 		  if (n == NULL || fnm == NULL)
   4675 		    goto error_free_dyn;
   4676 		  amt = strlen (fnm) + 1;
   4677 		  anm = (char *) bfd_alloc (abfd, amt);
   4678 		  if (anm == NULL)
   4679 		    goto error_free_dyn;
   4680 		  memcpy (anm, fnm, amt);
   4681 		  n->name = anm;
   4682 		  n->by = abfd;
   4683 		  n->next = NULL;
   4684 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   4685 		    ;
   4686 		  *pn = n;
   4687 		}
   4688 	      if (dyn.d_tag == DT_RUNPATH)
   4689 		{
   4690 		  struct bfd_link_needed_list *n, **pn;
   4691 		  char *fnm, *anm;
   4692 		  unsigned int tagv = dyn.d_un.d_val;
   4693 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4694 
   4695 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4696 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4697 		  if (n == NULL || fnm == NULL)
   4698 		    goto error_free_dyn;
   4699 		  amt = strlen (fnm) + 1;
   4700 		  anm = (char *) bfd_alloc (abfd, amt);
   4701 		  if (anm == NULL)
   4702 		    goto error_free_dyn;
   4703 		  memcpy (anm, fnm, amt);
   4704 		  n->name = anm;
   4705 		  n->by = abfd;
   4706 		  n->next = NULL;
   4707 		  for (pn = & runpath;
   4708 		       *pn != NULL;
   4709 		       pn = &(*pn)->next)
   4710 		    ;
   4711 		  *pn = n;
   4712 		}
   4713 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   4714 	      if (!runpath && dyn.d_tag == DT_RPATH)
   4715 		{
   4716 		  struct bfd_link_needed_list *n, **pn;
   4717 		  char *fnm, *anm;
   4718 		  unsigned int tagv = dyn.d_un.d_val;
   4719 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4720 
   4721 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4722 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4723 		  if (n == NULL || fnm == NULL)
   4724 		    goto error_free_dyn;
   4725 		  amt = strlen (fnm) + 1;
   4726 		  anm = (char *) bfd_alloc (abfd, amt);
   4727 		  if (anm == NULL)
   4728 		    goto error_free_dyn;
   4729 		  memcpy (anm, fnm, amt);
   4730 		  n->name = anm;
   4731 		  n->by = abfd;
   4732 		  n->next = NULL;
   4733 		  for (pn = & rpath;
   4734 		       *pn != NULL;
   4735 		       pn = &(*pn)->next)
   4736 		    ;
   4737 		  *pn = n;
   4738 		}
   4739 	      if (dyn.d_tag == DT_AUDIT)
   4740 		{
   4741 		  unsigned int tagv = dyn.d_un.d_val;
   4742 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4743 		}
   4744 	      if (dyn.d_tag == DT_FLAGS_1)
   4745 		elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
   4746 	    }
   4747 
   4748 	  _bfd_elf_munmap_section_contents (s, dynbuf);
   4749 	}
   4750 
   4751       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   4752 	 frees all more recently bfd_alloc'd blocks as well.  */
   4753       if (runpath)
   4754 	rpath = runpath;
   4755 
   4756       if (rpath)
   4757 	{
   4758 	  struct bfd_link_needed_list **pn;
   4759 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   4760 	    ;
   4761 	  *pn = rpath;
   4762 	}
   4763 
   4764       /* If we have a PT_GNU_RELRO program header, mark as read-only
   4765 	 all sections contained fully therein.  This makes relro
   4766 	 shared library sections appear as they will at run-time.  */
   4767       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
   4768       while (phdr-- > elf_tdata (abfd)->phdr)
   4769 	if (phdr->p_type == PT_GNU_RELRO)
   4770 	  {
   4771 	    for (s = abfd->sections; s != NULL; s = s->next)
   4772 	      {
   4773 		unsigned int opb = bfd_octets_per_byte (abfd, s);
   4774 
   4775 		if ((s->flags & SEC_ALLOC) != 0
   4776 		    && s->vma * opb >= phdr->p_vaddr
   4777 		    && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
   4778 		  s->flags |= SEC_READONLY;
   4779 	      }
   4780 	    break;
   4781 	  }
   4782 
   4783       /* We do not want to include any of the sections in a dynamic
   4784 	 object in the output file.  We hack by simply clobbering the
   4785 	 list of sections in the BFD.  This could be handled more
   4786 	 cleanly by, say, a new section flag; the existing
   4787 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   4788 	 still implies that the section takes up space in the output
   4789 	 file.  */
   4790       bfd_section_list_clear (abfd);
   4791 
   4792       /* Find the name to use in a DT_NEEDED entry that refers to this
   4793 	 object.  If the object has a DT_SONAME entry, we use it.
   4794 	 Otherwise, if the generic linker stuck something in
   4795 	 elf_dt_name, we use that.  Otherwise, we just use the file
   4796 	 name.  */
   4797       if (soname == NULL || *soname == '\0')
   4798 	{
   4799 	  soname = elf_dt_name (abfd);
   4800 	  if (soname == NULL || *soname == '\0')
   4801 	    soname = bfd_get_filename (abfd);
   4802 	}
   4803 
   4804       /* Save the SONAME because sometimes the linker emulation code
   4805 	 will need to know it.  */
   4806       elf_dt_name (abfd) = soname;
   4807 
   4808       /* If we have already included this dynamic object in the
   4809 	 link, just ignore it.  There is no reason to include a
   4810 	 particular dynamic object more than once.  */
   4811       for (loaded_lib = htab->dyn_loaded;
   4812 	   loaded_lib != NULL;
   4813 	   loaded_lib = loaded_lib->next)
   4814 	{
   4815 	  if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
   4816 	    return true;
   4817 	}
   4818 
   4819       /* Create dynamic sections for backends that require that be done
   4820 	 before setup_gnu_properties.  */
   4821       if (add_needed
   4822 	  && !bfd_elf_link_create_dynamic_sections (abfd, info))
   4823 	return false;
   4824 
   4825       /* Save the DT_AUDIT entry for the linker emulation code. */
   4826       elf_dt_audit (abfd) = audit;
   4827     }
   4828 
   4829   /* If this is a dynamic object, we always link against the .dynsym
   4830      symbol table, not the .symtab symbol table.  The dynamic linker
   4831      will only see the .dynsym symbol table, so there is no reason to
   4832      look at .symtab for a dynamic object.  */
   4833 
   4834   if (! dynamic || elf_dynsymtab (abfd) == 0)
   4835     hdr = &elf_tdata (abfd)->symtab_hdr;
   4836   else
   4837     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   4838 
   4839   symcount = hdr->sh_size / bed->s->sizeof_sym;
   4840 
   4841   /* The sh_info field of the symtab header tells us where the
   4842      external symbols start.  We don't care about the local symbols at
   4843      this point.  */
   4844   if (elf_bad_symtab (abfd))
   4845     {
   4846       extsymcount = symcount;
   4847       extsymoff = 0;
   4848     }
   4849   else
   4850     {
   4851       extsymcount = symcount - hdr->sh_info;
   4852       extsymoff = hdr->sh_info;
   4853     }
   4854 
   4855   sym_hash = elf_sym_hashes (abfd);
   4856   if (extsymcount != 0)
   4857     {
   4858       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   4859 				      NULL, NULL, NULL);
   4860       if (isymbuf == NULL)
   4861 	goto error_return;
   4862 
   4863       if (sym_hash == NULL)
   4864 	{
   4865 	  /* We store a pointer to the hash table entry for each
   4866 	     external symbol.  */
   4867 	  sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
   4868 	  if (sym_hash == NULL)
   4869 	    goto error_free_sym;
   4870 	  elf_sym_hashes (abfd) = sym_hash;
   4871 	}
   4872     }
   4873 
   4874   if (dynamic)
   4875     {
   4876       /* Read in any version definitions.  */
   4877       if (!_bfd_elf_slurp_version_tables (abfd,
   4878 					  info->default_imported_symver))
   4879 	goto error_free_sym;
   4880 
   4881       /* Read in the symbol versions, but don't bother to convert them
   4882 	 to internal format.  */
   4883       if (elf_dynversym (abfd) != 0)
   4884 	{
   4885 	  Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   4886 	  bfd_size_type amt = versymhdr->sh_size;
   4887 
   4888 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
   4889 	    goto error_free_sym;
   4890 	  extversym = (Elf_External_Versym *)
   4891 	    _bfd_malloc_and_read (abfd, amt, amt);
   4892 	  if (extversym == NULL)
   4893 	    goto error_free_sym;
   4894 	  extversym_end = extversym + amt / sizeof (*extversym);
   4895 	}
   4896     }
   4897 
   4898   /* If we are loading an as-needed shared lib, save the symbol table
   4899      state before we start adding symbols.  If the lib turns out
   4900      to be unneeded, restore the state.  */
   4901   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4902     {
   4903       unsigned int i;
   4904       size_t entsize;
   4905 
   4906       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   4907 	{
   4908 	  struct bfd_hash_entry *p;
   4909 	  struct elf_link_hash_entry *h;
   4910 
   4911 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4912 	    {
   4913 	      h = (struct elf_link_hash_entry *) p;
   4914 	      entsize += htab->root.table.entsize;
   4915 	      if (h->root.type == bfd_link_hash_warning)
   4916 		{
   4917 		  entsize += htab->root.table.entsize;
   4918 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4919 		}
   4920 	      if (h->root.type == bfd_link_hash_common)
   4921 		entsize += sizeof (*h->root.u.c.p);
   4922 	    }
   4923 	}
   4924 
   4925       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   4926       old_tab = bfd_malloc (tabsize + entsize);
   4927       if (old_tab == NULL)
   4928 	goto error_free_vers;
   4929 
   4930       /* Remember the current objalloc pointer, so that all mem for
   4931 	 symbols added can later be reclaimed.  */
   4932       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   4933       if (alloc_mark == NULL)
   4934 	goto error_free_vers;
   4935 
   4936       /* Make a special call to the linker "notice" function to
   4937 	 tell it that we are about to handle an as-needed lib.  */
   4938       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   4939 	goto error_free_vers;
   4940 
   4941       /* Clone the symbol table.  Remember some pointers into the
   4942 	 symbol table, and dynamic symbol count.  */
   4943       old_ent = (char *) old_tab + tabsize;
   4944       memcpy (old_tab, htab->root.table.table, tabsize);
   4945       old_undefs = htab->root.undefs;
   4946       old_undefs_tail = htab->root.undefs_tail;
   4947       old_table = htab->root.table.table;
   4948       old_size = htab->root.table.size;
   4949       old_count = htab->root.table.count;
   4950       old_strtab = NULL;
   4951       if (htab->dynstr != NULL)
   4952 	{
   4953 	  old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   4954 	  if (old_strtab == NULL)
   4955 	    goto error_free_vers;
   4956 	}
   4957 
   4958       for (i = 0; i < htab->root.table.size; i++)
   4959 	{
   4960 	  struct bfd_hash_entry *p;
   4961 	  struct elf_link_hash_entry *h;
   4962 
   4963 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4964 	    {
   4965 	      h = (struct elf_link_hash_entry *) p;
   4966 	      memcpy (old_ent, h, htab->root.table.entsize);
   4967 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4968 	      if (h->root.type == bfd_link_hash_warning)
   4969 		{
   4970 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4971 		  memcpy (old_ent, h, htab->root.table.entsize);
   4972 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4973 		}
   4974 	      if (h->root.type == bfd_link_hash_common)
   4975 		{
   4976 		  memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
   4977 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   4978 		}
   4979 	    }
   4980 	}
   4981     }
   4982 
   4983   weaks = NULL;
   4984   if (extversym == NULL)
   4985     ever = NULL;
   4986   else if (extversym + extsymoff < extversym_end)
   4987     ever = extversym + extsymoff;
   4988   else
   4989     {
   4990       /* xgettext:c-format */
   4991       _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
   4992 			  abfd, (long) extsymoff,
   4993 			  (long) (extversym_end - extversym) / sizeof (* extversym));
   4994       bfd_set_error (bfd_error_bad_value);
   4995       goto error_free_vers;
   4996     }
   4997 
   4998   if (!bfd_link_relocatable (info)
   4999       && bfd_get_lto_type (abfd) == lto_slim_ir_object)
   5000     {
   5001       _bfd_error_handler
   5002 	(_("%pB: plugin needed to handle lto object"), abfd);
   5003     }
   5004 
   5005   for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
   5006        isym < isymend;
   5007        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   5008     {
   5009       int bind;
   5010       bfd_vma value;
   5011       asection *sec, *new_sec;
   5012       flagword flags;
   5013       const char *name;
   5014       const char *defvername;
   5015       bool must_copy_name = false;
   5016       struct elf_link_hash_entry *h;
   5017       struct elf_link_hash_entry *hi;
   5018       bool definition;
   5019       bool size_change_ok;
   5020       bool type_change_ok;
   5021       bool new_weak;
   5022       bool old_weak;
   5023       bfd *override;
   5024       bool common;
   5025       bool discarded;
   5026       unsigned int old_alignment;
   5027       unsigned int shindex;
   5028       bfd *old_bfd;
   5029       bool matched;
   5030 
   5031       override = NULL;
   5032 
   5033       flags = BSF_NO_FLAGS;
   5034       sec = NULL;
   5035       value = isym->st_value;
   5036       common = bed->common_definition (isym);
   5037       if (common && info->inhibit_common_definition)
   5038 	{
   5039 	  /* Treat common symbol as undefined for --no-define-common.  */
   5040 	  isym->st_shndx = SHN_UNDEF;
   5041 	  common = false;
   5042 	}
   5043       discarded = false;
   5044 
   5045       bind = ELF_ST_BIND (isym->st_info);
   5046       switch (bind)
   5047 	{
   5048 	case STB_LOCAL:
   5049 	  /* This should be impossible, since ELF requires that all
   5050 	     global symbols follow all local symbols, and that sh_info
   5051 	     point to the first global symbol.  Unfortunately, Irix 5
   5052 	     screws this up.  */
   5053 	  if (elf_bad_symtab (abfd))
   5054 	    continue;
   5055 
   5056 	  /* If we aren't prepared to handle locals within the globals
   5057 	     then we'll likely segfault on a NULL symbol hash if the
   5058 	     symbol is ever referenced in relocations.  */
   5059 	  shindex = elf_elfheader (abfd)->e_shstrndx;
   5060 	  name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
   5061 	  _bfd_error_handler (_("%pB: %s local symbol at index %lu"
   5062 				" (>= sh_info of %lu)"),
   5063 			      abfd, name, (long) (isym - isymbuf + extsymoff),
   5064 			      (long) extsymoff);
   5065 
   5066 	  /* Dynamic object relocations are not processed by ld, so
   5067 	     ld won't run into the problem mentioned above.  */
   5068 	  if (dynamic)
   5069 	    continue;
   5070 	  bfd_set_error (bfd_error_bad_value);
   5071 	  goto error_free_vers;
   5072 
   5073 	case STB_GLOBAL:
   5074 	  if (isym->st_shndx != SHN_UNDEF && !common)
   5075 	    flags = BSF_GLOBAL;
   5076 	  break;
   5077 
   5078 	case STB_WEAK:
   5079 	  flags = BSF_WEAK;
   5080 	  break;
   5081 
   5082 	case STB_GNU_UNIQUE:
   5083 	  flags = BSF_GNU_UNIQUE;
   5084 	  break;
   5085 
   5086 	default:
   5087 	  /* Leave it up to the processor backend.  */
   5088 	  break;
   5089 	}
   5090 
   5091       if (isym->st_shndx == SHN_UNDEF)
   5092 	sec = bfd_und_section_ptr;
   5093       else if (isym->st_shndx == SHN_ABS)
   5094 	sec = bfd_abs_section_ptr;
   5095       else if (isym->st_shndx == SHN_COMMON)
   5096 	{
   5097 	  sec = bfd_com_section_ptr;
   5098 	  /* What ELF calls the size we call the value.  What ELF
   5099 	     calls the value we call the alignment.  */
   5100 	  value = isym->st_size;
   5101 	}
   5102       else
   5103 	{
   5104 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5105 	  if (sec == NULL)
   5106 	    sec = bfd_abs_section_ptr;
   5107 	  else if (discarded_section (sec))
   5108 	    {
   5109 	      /* Symbols from discarded section are undefined.  We keep
   5110 		 its visibility.  */
   5111 	      sec = bfd_und_section_ptr;
   5112 	      discarded = true;
   5113 	      isym->st_shndx = SHN_UNDEF;
   5114 	    }
   5115 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   5116 	    value -= sec->vma;
   5117 	}
   5118 
   5119       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   5120 					      isym->st_name);
   5121       if (name == NULL)
   5122 	goto error_free_vers;
   5123 
   5124       if (isym->st_shndx == SHN_COMMON
   5125 	  && (abfd->flags & BFD_PLUGIN) != 0)
   5126 	{
   5127 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   5128 
   5129 	  if (xc == NULL)
   5130 	    {
   5131 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   5132 				 | SEC_EXCLUDE);
   5133 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   5134 	      if (xc == NULL)
   5135 		goto error_free_vers;
   5136 	    }
   5137 	  sec = xc;
   5138 	}
   5139       else if (isym->st_shndx == SHN_COMMON
   5140 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   5141 	       && !bfd_link_relocatable (info))
   5142 	{
   5143 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   5144 
   5145 	  if (tcomm == NULL)
   5146 	    {
   5147 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   5148 				 | SEC_LINKER_CREATED);
   5149 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   5150 	      if (tcomm == NULL)
   5151 		goto error_free_vers;
   5152 	    }
   5153 	  sec = tcomm;
   5154 	}
   5155       else if (bed->elf_add_symbol_hook)
   5156 	{
   5157 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   5158 					     &sec, &value))
   5159 	    goto error_free_vers;
   5160 
   5161 	  /* The hook function sets the name to NULL if this symbol
   5162 	     should be skipped for some reason.  */
   5163 	  if (name == NULL)
   5164 	    continue;
   5165 	}
   5166 
   5167       if (name[0] == '\0')
   5168 	{
   5169 	  _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
   5170 	  bfd_set_error (bfd_error_bad_value);
   5171 	  goto error_free_vers;
   5172 	}
   5173 
   5174       /* Sanity check that all possibilities were handled.  */
   5175       if (sec == NULL)
   5176 	abort ();
   5177 
   5178       /* Silently discard TLS symbols from --just-syms.  There's
   5179 	 no way to combine a static TLS block with a new TLS block
   5180 	 for this executable.  */
   5181       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   5182 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   5183 	continue;
   5184 
   5185       if (bfd_is_und_section (sec)
   5186 	  || bfd_is_com_section (sec))
   5187 	definition = false;
   5188       else
   5189 	definition = true;
   5190 
   5191       size_change_ok = false;
   5192       type_change_ok = bed->type_change_ok;
   5193       old_weak = false;
   5194       matched = false;
   5195       old_alignment = 0;
   5196       old_bfd = NULL;
   5197       new_sec = sec;
   5198       defvername = NULL;
   5199 
   5200       if (is_elf_hash_table (&htab->root))
   5201 	{
   5202 	  Elf_Internal_Versym iver;
   5203 	  unsigned int vernum = 0;
   5204 	  bool skip;
   5205 
   5206 	  if (ever == NULL)
   5207 	    {
   5208 	      if (info->default_imported_symver)
   5209 		/* Use the default symbol version created earlier.  */
   5210 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   5211 	      else
   5212 		iver.vs_vers = 0;
   5213 	    }
   5214 	  else if (ever >= extversym_end)
   5215 	    {
   5216 	      /* xgettext:c-format */
   5217 	      _bfd_error_handler (_("%pB: not enough version information"),
   5218 				  abfd);
   5219 	      bfd_set_error (bfd_error_bad_value);
   5220 	      goto error_free_vers;
   5221 	    }
   5222 	  else
   5223 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   5224 
   5225 	  vernum = iver.vs_vers & VERSYM_VERSION;
   5226 
   5227 	  /* If this is a hidden symbol, or if it is not version
   5228 	     1, we append the version name to the symbol name.
   5229 	     However, we do not modify a non-hidden absolute symbol
   5230 	     if it is not a function, because it might be the version
   5231 	     symbol itself.  FIXME: What if it isn't?  */
   5232 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   5233 	      || (vernum > 1
   5234 		  && (!bfd_is_abs_section (sec)
   5235 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   5236 	    {
   5237 	      const char *verstr;
   5238 	      size_t namelen, verlen, newlen;
   5239 	      char *newname, *p;
   5240 
   5241 	      if (isym->st_shndx != SHN_UNDEF)
   5242 		{
   5243 		  if (vernum > elf_tdata (abfd)->cverdefs)
   5244 		    verstr = NULL;
   5245 		  else if (vernum > 1)
   5246 		    verstr =
   5247 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   5248 		  else
   5249 		    verstr = "";
   5250 
   5251 		  if (verstr == NULL)
   5252 		    {
   5253 		      _bfd_error_handler
   5254 			/* xgettext:c-format */
   5255 			(_("%pB: %s: invalid version %u (max %d)"),
   5256 			 abfd, name, vernum,
   5257 			 elf_tdata (abfd)->cverdefs);
   5258 		      bfd_set_error (bfd_error_bad_value);
   5259 		      goto error_free_vers;
   5260 		    }
   5261 		}
   5262 	      else
   5263 		{
   5264 		  /* We cannot simply test for the number of
   5265 		     entries in the VERNEED section since the
   5266 		     numbers for the needed versions do not start
   5267 		     at 0.  */
   5268 		  Elf_Internal_Verneed *t;
   5269 
   5270 		  verstr = NULL;
   5271 		  for (t = elf_tdata (abfd)->verref;
   5272 		       t != NULL;
   5273 		       t = t->vn_nextref)
   5274 		    {
   5275 		      Elf_Internal_Vernaux *a;
   5276 
   5277 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   5278 			{
   5279 			  if (a->vna_other == vernum)
   5280 			    {
   5281 			      verstr = a->vna_nodename;
   5282 			      break;
   5283 			    }
   5284 			}
   5285 		      if (a != NULL)
   5286 			break;
   5287 		    }
   5288 		  if (verstr == NULL)
   5289 		    {
   5290 		      _bfd_error_handler
   5291 			/* xgettext:c-format */
   5292 			(_("%pB: %s: invalid needed version %d"),
   5293 			 abfd, name, vernum);
   5294 		      bfd_set_error (bfd_error_bad_value);
   5295 		      goto error_free_vers;
   5296 		    }
   5297 		}
   5298 
   5299 	      namelen = strlen (name);
   5300 	      verlen = strlen (verstr);
   5301 	      newlen = namelen + verlen + 2;
   5302 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5303 		  && isym->st_shndx != SHN_UNDEF)
   5304 		++newlen;
   5305 
   5306 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   5307 	      if (newname == NULL)
   5308 		goto error_free_vers;
   5309 	      memcpy (newname, name, namelen);
   5310 	      p = newname + namelen;
   5311 	      *p++ = ELF_VER_CHR;
   5312 	      /* If this is a defined non-hidden version symbol,
   5313 		 we add another @ to the name.  This indicates the
   5314 		 default version of the symbol.  */
   5315 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5316 		  && isym->st_shndx != SHN_UNDEF)
   5317 		*p++ = ELF_VER_CHR, defvername = name;
   5318 	      memcpy (p, verstr, verlen + 1);
   5319 
   5320 	      name = newname;
   5321 	      /* Since bfd_hash_alloc is used for "name", the string
   5322 		 must be copied if added to first_hash.  The string
   5323 		 memory can be freed when an --as-needed library is
   5324 		 not needed.  */
   5325 	      must_copy_name = true;
   5326 	    }
   5327 
   5328 	  /* If this symbol has default visibility and the user has
   5329 	     requested we not re-export it, then mark it as hidden.  */
   5330 	  if (!bfd_is_und_section (sec)
   5331 	      && !dynamic
   5332 	      && abfd->no_export
   5333 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   5334 	    isym->st_other = (STV_HIDDEN
   5335 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   5336 
   5337 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   5338 				      sym_hash, &old_bfd, &old_weak,
   5339 				      &old_alignment, &skip, &override,
   5340 				      &type_change_ok, &size_change_ok,
   5341 				      &matched))
   5342 	    goto error_free_vers;
   5343 
   5344 	  if (skip)
   5345 	    continue;
   5346 
   5347 	  h = *sym_hash;
   5348 	  while (h->root.type == bfd_link_hash_indirect
   5349 		 || h->root.type == bfd_link_hash_warning)
   5350 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5351 
   5352 	  /* Override a definition only if the new symbol matches the
   5353 	     existing one.  */
   5354 	  if (override && matched)
   5355 	    {
   5356 	      definition = false;
   5357 	      if (htab->first_hash != NULL
   5358 		  && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5359 		  && h->root.non_ir_ref_regular)
   5360 		{
   5361 		  /* When reloading --as-needed shared objects for new
   5362 		     symbols added from IR inputs, if this shared object
   5363 		     has the first definition, use it.  */
   5364 		  struct elf_link_first_hash_entry *e
   5365 		    = ((struct elf_link_first_hash_entry *)
   5366 		       bfd_hash_lookup (htab->first_hash, name, false,
   5367 					false));
   5368 		  if (e != NULL && e->abfd == abfd)
   5369 		    definition = true;
   5370 		}
   5371 	    }
   5372 
   5373 	  if (h->versioned != unversioned
   5374 	      && elf_tdata (abfd)->verdef != NULL
   5375 	      && vernum > 1
   5376 	      && definition)
   5377 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   5378 	}
   5379 
   5380       if (! (_bfd_generic_link_add_one_symbol
   5381 	     (info, override ? override : abfd, name, flags, sec, value,
   5382 	      NULL, false, bed->collect,
   5383 	      (struct bfd_link_hash_entry **) sym_hash)))
   5384 	goto error_free_vers;
   5385 
   5386       h = *sym_hash;
   5387       /* We need to make sure that indirect symbol dynamic flags are
   5388 	 updated.  */
   5389       hi = h;
   5390       while (h->root.type == bfd_link_hash_indirect
   5391 	     || h->root.type == bfd_link_hash_warning)
   5392 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5393 
   5394       *sym_hash = h;
   5395 
   5396       /* Setting the index to -3 tells elf_link_output_extsym that
   5397 	 this symbol is defined in a discarded section.  */
   5398       if (discarded && is_elf_hash_table (&htab->root))
   5399 	h->indx = -3;
   5400 
   5401       new_weak = (flags & BSF_WEAK) != 0;
   5402       if (dynamic
   5403 	  && definition
   5404 	  && new_weak
   5405 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   5406 	  && is_elf_hash_table (&htab->root)
   5407 	  && h->u.alias == NULL)
   5408 	{
   5409 	  /* Keep a list of all weak defined non function symbols from
   5410 	     a dynamic object, using the alias field.  Later in this
   5411 	     function we will set the alias field to the correct
   5412 	     value.  We only put non-function symbols from dynamic
   5413 	     objects on this list, because that happens to be the only
   5414 	     time we need to know the normal symbol corresponding to a
   5415 	     weak symbol, and the information is time consuming to
   5416 	     figure out.  If the alias field is not already NULL,
   5417 	     then this symbol was already defined by some previous
   5418 	     dynamic object, and we will be using that previous
   5419 	     definition anyhow.  */
   5420 
   5421 	  h->u.alias = weaks;
   5422 	  weaks = h;
   5423 	}
   5424 
   5425       /* Set the alignment of a common symbol.  */
   5426       if ((common || bfd_is_com_section (sec))
   5427 	  && h->root.type == bfd_link_hash_common)
   5428 	{
   5429 	  unsigned int align;
   5430 
   5431 	  if (common)
   5432 	    align = bfd_log2 (isym->st_value);
   5433 	  else
   5434 	    {
   5435 	      /* The new symbol is a common symbol in a shared object.
   5436 		 We need to get the alignment from the section.  */
   5437 	      align = new_sec->alignment_power;
   5438 	    }
   5439 	  if (align > old_alignment)
   5440 	    h->root.u.c.p->alignment_power = align;
   5441 	  else
   5442 	    h->root.u.c.p->alignment_power = old_alignment;
   5443 	}
   5444 
   5445       if (is_elf_hash_table (&htab->root))
   5446 	{
   5447 	  /* Set a flag in the hash table entry indicating the type of
   5448 	     reference or definition we just found.  A dynamic symbol
   5449 	     is one which is referenced or defined by both a regular
   5450 	     object and a shared object.  */
   5451 	  bool dynsym = false;
   5452 
   5453 	  /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
   5454 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5455 	    {
   5456 	      /* Except for this flag to track nonweak references.  */
   5457 	      if (!definition
   5458 		  && bind != STB_WEAK)
   5459 		h->ref_ir_nonweak = 1;
   5460 	    }
   5461 	  else if (!dynamic)
   5462 	    {
   5463 	      if (! definition)
   5464 		{
   5465 		  h->ref_regular = 1;
   5466 		  if (bind != STB_WEAK)
   5467 		    h->ref_regular_nonweak = 1;
   5468 		}
   5469 	      else
   5470 		{
   5471 		  h->def_regular = 1;
   5472 		  if (h->def_dynamic)
   5473 		    {
   5474 		      h->def_dynamic = 0;
   5475 		      h->ref_dynamic = 1;
   5476 		    }
   5477 		}
   5478 	    }
   5479 	  else
   5480 	    {
   5481 	      if (! definition)
   5482 		{
   5483 		  h->ref_dynamic = 1;
   5484 		  hi->ref_dynamic = 1;
   5485 		}
   5486 	      else
   5487 		{
   5488 		  h->def_dynamic = 1;
   5489 		  hi->def_dynamic = 1;
   5490 		}
   5491 	    }
   5492 
   5493 	  /* If an indirect symbol has been forced local, don't
   5494 	     make the real symbol dynamic.  */
   5495 	  if (h != hi && hi->forced_local)
   5496 	    ;
   5497 	  else if (!dynamic)
   5498 	    {
   5499 	      if (bfd_link_dll (info)
   5500 		  || h->def_dynamic
   5501 		  || h->ref_dynamic)
   5502 		dynsym = true;
   5503 	    }
   5504 	  else
   5505 	    {
   5506 	      if (h->def_regular
   5507 		  || h->ref_regular
   5508 		  || (h->is_weakalias
   5509 		      && weakdef (h)->dynindx != -1))
   5510 		dynsym = true;
   5511 	    }
   5512 
   5513 	  /* Check to see if we need to add an indirect symbol for
   5514 	     the default name.  */
   5515 	  if ((definition
   5516 	       || (!override && h->root.type == bfd_link_hash_common))
   5517 	      && !(hi != h
   5518 		   && hi->versioned == versioned_hidden))
   5519 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   5520 					      sec, value, &old_bfd, &dynsym))
   5521 	      goto error_free_vers;
   5522 
   5523 	  /* Check the alignment when a common symbol is involved. This
   5524 	     can change when a common symbol is overridden by a normal
   5525 	     definition or a common symbol is ignored due to the old
   5526 	     normal definition. We need to make sure the maximum
   5527 	     alignment is maintained.  */
   5528 	  if ((old_alignment || common)
   5529 	      && h->root.type != bfd_link_hash_common)
   5530 	    {
   5531 	      unsigned int common_align;
   5532 	      unsigned int normal_align;
   5533 	      unsigned int symbol_align;
   5534 	      bfd *normal_bfd;
   5535 	      bfd *common_bfd;
   5536 
   5537 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   5538 			  || h->root.type == bfd_link_hash_defweak);
   5539 
   5540 	      symbol_align = ffs (h->root.u.def.value) - 1;
   5541 	      if (h->root.u.def.section->owner != NULL
   5542 		  && (h->root.u.def.section->owner->flags
   5543 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
   5544 		{
   5545 		  normal_align = h->root.u.def.section->alignment_power;
   5546 		  if (normal_align > symbol_align)
   5547 		    normal_align = symbol_align;
   5548 		}
   5549 	      else
   5550 		normal_align = symbol_align;
   5551 
   5552 	      if (old_alignment)
   5553 		{
   5554 		  common_align = old_alignment;
   5555 		  common_bfd = old_bfd;
   5556 		  normal_bfd = abfd;
   5557 		}
   5558 	      else
   5559 		{
   5560 		  common_align = bfd_log2 (isym->st_value);
   5561 		  common_bfd = abfd;
   5562 		  normal_bfd = old_bfd;
   5563 		}
   5564 
   5565 	      if (normal_align < common_align)
   5566 		{
   5567 		  /* PR binutils/2735 */
   5568 		  uint64_t c_align = UINT64_C (1) << common_align;
   5569 		  uint64_t n_align = UINT64_C (1) << normal_align;
   5570 		  if (normal_bfd == NULL)
   5571 		    _bfd_error_handler
   5572 		      /* xgettext:c-format */
   5573 		      (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
   5574 			 " greater than the alignment (%" PRIu64 ") of its section %pA"),
   5575 		       c_align, name, common_bfd,
   5576 		       n_align, h->root.u.def.section);
   5577 		  else
   5578 		    _bfd_error_handler
   5579 		      /* xgettext:c-format */
   5580 		      (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
   5581 			 " is smaller than %" PRIu64 " used by the common definition in %pB"),
   5582 		       n_align, name, normal_bfd,
   5583 		       c_align, common_bfd);
   5584 
   5585 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5586 		  _bfd_error_handler
   5587 		    (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
   5588 		}
   5589 	    }
   5590 
   5591 	  /* Remember the symbol size if it isn't undefined.  */
   5592 	  if (isym->st_size != 0
   5593 	      && isym->st_shndx != SHN_UNDEF
   5594 	      && (definition || h->size == 0))
   5595 	    {
   5596 	      if (h->size != 0
   5597 		  && h->size != isym->st_size
   5598 		  && ! size_change_ok)
   5599 		{
   5600 		  _bfd_error_handler
   5601 		    /* xgettext:c-format */
   5602 		    (_("warning: size of symbol `%s' changed"
   5603 		       " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
   5604 		     name, (uint64_t) h->size, old_bfd,
   5605 		     (uint64_t) isym->st_size, abfd);
   5606 
   5607 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5608 		  _bfd_error_handler
   5609 		    (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
   5610 		}
   5611 
   5612 	      h->size = isym->st_size;
   5613 	    }
   5614 
   5615 	  /* If this is a common symbol, then we always want H->SIZE
   5616 	     to be the size of the common symbol.  The code just above
   5617 	     won't fix the size if a common symbol becomes larger.  We
   5618 	     don't warn about a size change here, because that is
   5619 	     covered by --warn-common.  Allow changes between different
   5620 	     function types.  */
   5621 	  if (h->root.type == bfd_link_hash_common)
   5622 	    h->size = h->root.u.c.size;
   5623 
   5624 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   5625 	      && ((definition && !new_weak)
   5626 		  || (old_weak && h->root.type == bfd_link_hash_common)
   5627 		  || h->type == STT_NOTYPE))
   5628 	    {
   5629 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   5630 
   5631 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   5632 		 symbol.  */
   5633 	      if (type == STT_GNU_IFUNC
   5634 		  && (abfd->flags & DYNAMIC) != 0)
   5635 		type = STT_FUNC;
   5636 
   5637 	      if (h->type != type)
   5638 		{
   5639 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   5640 		    /* xgettext:c-format */
   5641 		    _bfd_error_handler
   5642 		      (_("warning: type of symbol `%s' changed"
   5643 			 " from %d to %d in %pB"),
   5644 		       name, h->type, type, abfd);
   5645 
   5646 		  h->type = type;
   5647 		}
   5648 	    }
   5649 
   5650 	  /* Merge st_other field.  */
   5651 	  elf_merge_st_other (abfd, h, isym->st_other, sec,
   5652 			      definition, dynamic);
   5653 
   5654 	  /* We don't want to make debug symbol dynamic.  */
   5655 	  if (definition
   5656 	      && (sec->flags & SEC_DEBUGGING)
   5657 	      && !bfd_link_relocatable (info))
   5658 	    dynsym = false;
   5659 
   5660 	  /* Nor should we make plugin symbols dynamic.  */
   5661 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5662 	    dynsym = false;
   5663 
   5664 	  if (definition)
   5665 	    {
   5666 	      h->target_internal = isym->st_target_internal;
   5667 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   5668 	    }
   5669 
   5670 	  /* Don't add indirect symbols for .symver x, x@FOO aliases
   5671 	     in IR.  Since all data or text symbols in IR have the
   5672 	     same type, value and section, we can't tell if a symbol
   5673 	     is an alias of another symbol by their types, values and
   5674 	     sections.  */
   5675 	  if (definition
   5676 	      && !dynamic
   5677 	      && (abfd->flags & BFD_PLUGIN) == 0)
   5678 	    {
   5679 	      const char *p = strchr (name, ELF_VER_CHR);
   5680 	      if (p != NULL && p[1] != ELF_VER_CHR)
   5681 		{
   5682 		  /* Queue non-default versions so that .symver x, x@FOO
   5683 		     aliases can be checked.  */
   5684 		  if (!nondeflt_vers)
   5685 		    {
   5686 		      size_t amt = ((isymend - isym + 1)
   5687 				    * sizeof (struct elf_link_hash_entry *));
   5688 		      nondeflt_vers
   5689 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   5690 		      if (!nondeflt_vers)
   5691 			goto error_free_vers;
   5692 		    }
   5693 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   5694 		}
   5695 	    }
   5696 
   5697 	  if (dynsym && h->dynindx == -1)
   5698 	    {
   5699 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5700 		goto error_free_vers;
   5701 	      if (h->is_weakalias
   5702 		  && weakdef (h)->dynindx == -1)
   5703 		{
   5704 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
   5705 		    goto error_free_vers;
   5706 		}
   5707 	    }
   5708 	  else if (h->dynindx != -1)
   5709 	    /* If the symbol already has a dynamic index, but
   5710 	       visibility says it should not be visible, turn it into
   5711 	       a local symbol.  */
   5712 	    switch (ELF_ST_VISIBILITY (h->other))
   5713 	      {
   5714 	      case STV_INTERNAL:
   5715 	      case STV_HIDDEN:
   5716 		(*bed->elf_backend_hide_symbol) (info, h, true);
   5717 		dynsym = false;
   5718 		break;
   5719 	      }
   5720 
   5721 	  if (!add_needed
   5722 	      && matched
   5723 	      && definition
   5724 	      && h->root.type != bfd_link_hash_indirect)
   5725 	    {
   5726 	      if ((dynsym
   5727 		   && h->ref_regular_nonweak)
   5728 		  || (old_bfd != NULL
   5729 		      && (old_bfd->flags & BFD_PLUGIN) != 0
   5730 		      && h->ref_ir_nonweak
   5731 		      && !info->lto_all_symbols_read)
   5732 		  || (h->ref_dynamic_nonweak
   5733 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5734 		      && !on_needed_list (elf_dt_name (abfd),
   5735 					  htab->needed, NULL)))
   5736 		{
   5737 		  const char *soname = elf_dt_name (abfd);
   5738 
   5739 		  info->callbacks->minfo ("%!", soname, old_bfd,
   5740 					  h->root.root.string);
   5741 
   5742 		  /* A symbol from a library loaded via DT_NEEDED of some
   5743 		     other library is referenced by a regular object.
   5744 		     Add a DT_NEEDED entry for it.  Issue an error if
   5745 		     --no-add-needed is used and the reference was not
   5746 		     a weak one.  */
   5747 		  if (old_bfd != NULL
   5748 		      && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   5749 		    {
   5750 		      _bfd_error_handler
   5751 			/* xgettext:c-format */
   5752 			(_("%pB: undefined reference to symbol '%s'"),
   5753 			 old_bfd, name);
   5754 		      bfd_set_error (bfd_error_missing_dso);
   5755 		      goto error_free_vers;
   5756 		    }
   5757 
   5758 		  elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   5759 		    (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   5760 
   5761 		  /* Create dynamic sections for backends that require
   5762 		     that be done before setup_gnu_properties.  */
   5763 		  if (!bfd_elf_link_create_dynamic_sections (abfd, info))
   5764 		    goto error_free_vers;
   5765 		  add_needed = true;
   5766 		}
   5767 	      else if (dynamic
   5768 		       && h->root.u.def.section->owner == abfd)
   5769 		{
   5770 		  /* Add this symbol to first hash if this shared
   5771 		     object has the first definition.  */
   5772 		  elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
   5773 		  /* And if it was the default symbol version definition,
   5774 		     also add the short name.  */
   5775 		  if (defvername)
   5776 		    elf_link_add_to_first_hash (abfd, info, defvername, false);
   5777 		}
   5778 	    }
   5779 	}
   5780     }
   5781 
   5782   if (info->lto_plugin_active
   5783       && !bfd_link_relocatable (info)
   5784       && (abfd->flags & BFD_PLUGIN) == 0
   5785       && !just_syms
   5786       && extsymcount != 0
   5787       && is_elf_hash_table (&htab->root))
   5788     {
   5789       int r_sym_shift;
   5790 
   5791       if (bed->s->arch_size == 32)
   5792 	r_sym_shift = 8;
   5793       else
   5794 	r_sym_shift = 32;
   5795 
   5796       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
   5797 	 referenced in regular objects so that linker plugin will get
   5798 	 the correct symbol resolution.  */
   5799 
   5800       sym_hash = elf_sym_hashes (abfd);
   5801       for (s = abfd->sections; s != NULL; s = s->next)
   5802 	{
   5803 	  Elf_Internal_Rela *internal_relocs;
   5804 	  Elf_Internal_Rela *rel, *relend;
   5805 
   5806 	  /* Don't check relocations in excluded sections.  */
   5807 	  if ((s->flags & SEC_RELOC) == 0
   5808 	      || s->reloc_count == 0
   5809 	      || (s->flags & SEC_EXCLUDE) != 0
   5810 	      || (s->flags & SEC_DEBUGGING) != 0)
   5811 	    continue;
   5812 
   5813 	  internal_relocs = _bfd_elf_link_info_read_relocs
   5814 	    (abfd, info, s, NULL, NULL,
   5815 	     _bfd_elf_link_keep_memory (info));
   5816 	  if (internal_relocs == NULL)
   5817 	    goto error_free_vers;
   5818 
   5819 	  rel = internal_relocs;
   5820 	  relend = rel + s->reloc_count;
   5821 	  for ( ; rel < relend; rel++)
   5822 	    {
   5823 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   5824 	      struct elf_link_hash_entry *h;
   5825 
   5826 	      /* Skip local symbols.  */
   5827 	      if (r_symndx < extsymoff)
   5828 		continue;
   5829 
   5830 	      h = sym_hash[r_symndx - extsymoff];
   5831 	      if (h != NULL)
   5832 		h->root.non_ir_ref_regular = 1;
   5833 	    }
   5834 
   5835 	  if (elf_section_data (s)->relocs != internal_relocs)
   5836 	    free (internal_relocs);
   5837 	}
   5838     }
   5839 
   5840   free (extversym);
   5841   extversym = NULL;
   5842   free (isymbuf);
   5843   isymbuf = NULL;
   5844 
   5845   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   5846     {
   5847       unsigned int i;
   5848 
   5849       /* Restore the symbol table.  */
   5850       old_ent = (char *) old_tab + tabsize;
   5851       memset (elf_sym_hashes (abfd), 0,
   5852 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   5853       htab->root.table.table = old_table;
   5854       htab->root.table.size = old_size;
   5855       htab->root.table.count = old_count;
   5856       memcpy (htab->root.table.table, old_tab, tabsize);
   5857       htab->root.undefs = old_undefs;
   5858       htab->root.undefs_tail = old_undefs_tail;
   5859       if (htab->dynstr != NULL)
   5860 	_bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   5861       free (old_strtab);
   5862       old_strtab = NULL;
   5863       for (i = 0; i < htab->root.table.size; i++)
   5864 	{
   5865 	  struct bfd_hash_entry *p;
   5866 	  struct elf_link_hash_entry *h;
   5867 	  unsigned int non_ir_ref_dynamic;
   5868 
   5869 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   5870 	    {
   5871 	      /* Preserve non_ir_ref_dynamic so that this symbol
   5872 		 will be exported when the dynamic lib becomes needed
   5873 		 in the second pass.  */
   5874 	      h = (struct elf_link_hash_entry *) p;
   5875 	      if (h->root.type == bfd_link_hash_warning)
   5876 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5877 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
   5878 
   5879 	      h = (struct elf_link_hash_entry *) p;
   5880 	      memcpy (h, old_ent, htab->root.table.entsize);
   5881 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   5882 	      if (h->root.type == bfd_link_hash_warning)
   5883 		{
   5884 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5885 		  memcpy (h, old_ent, htab->root.table.entsize);
   5886 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   5887 		}
   5888 	      if (h->root.type == bfd_link_hash_common)
   5889 		{
   5890 		  memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
   5891 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   5892 		}
   5893 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
   5894 	    }
   5895 	}
   5896 
   5897       /* Make a special call to the linker "notice" function to
   5898 	 tell it that symbols added for crefs may need to be removed.  */
   5899       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   5900 	goto error_free_vers;
   5901 
   5902       free (old_tab);
   5903       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   5904 			   alloc_mark);
   5905       free (nondeflt_vers);
   5906       return true;
   5907     }
   5908 
   5909   free (old_strtab);
   5910   old_strtab = NULL;
   5911   if (old_tab != NULL)
   5912     {
   5913       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   5914 	goto error_free_vers;
   5915       free (old_tab);
   5916       old_tab = NULL;
   5917     }
   5918 
   5919   /* Now that all the symbols from this input file are created, if
   5920      not performing a relocatable link, handle .symver foo, foo@BAR
   5921      such that any relocs against foo become foo@BAR.  */
   5922   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   5923     {
   5924       size_t cnt, symidx;
   5925 
   5926       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   5927 	{
   5928 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   5929 	  char *shortname;
   5930 	  const char *p;
   5931 	  size_t amt;
   5932 
   5933 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   5934 	  if (p == NULL
   5935 	      || (h->root.type != bfd_link_hash_defined
   5936 		  && h->root.type != bfd_link_hash_defweak))
   5937 	    continue;
   5938 
   5939 	  amt = p - h->root.root.string;
   5940 	  shortname = (char *) bfd_malloc (amt + 1);
   5941 	  if (!shortname)
   5942 	    goto error_free_vers;
   5943 	  memcpy (shortname, h->root.root.string, amt);
   5944 	  shortname[amt] = '\0';
   5945 
   5946 	  hi = (struct elf_link_hash_entry *)
   5947 	       bfd_link_hash_lookup (&htab->root, shortname,
   5948 				     false, false, false);
   5949 	  if (hi != NULL
   5950 	      && hi->root.type == h->root.type
   5951 	      && hi->root.u.def.value == h->root.u.def.value
   5952 	      && hi->root.u.def.section == h->root.u.def.section)
   5953 	    {
   5954 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   5955 	      hi->root.type = bfd_link_hash_indirect;
   5956 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   5957 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   5958 	      sym_hash = elf_sym_hashes (abfd);
   5959 	      if (sym_hash)
   5960 		for (symidx = 0; symidx < extsymcount; ++symidx)
   5961 		  if (sym_hash[symidx] == hi)
   5962 		    {
   5963 		      sym_hash[symidx] = h;
   5964 		      break;
   5965 		    }
   5966 	    }
   5967 	  free (shortname);
   5968 	}
   5969     }
   5970   free (nondeflt_vers);
   5971   nondeflt_vers = NULL;
   5972 
   5973   /* Now set the alias field correctly for all the weak defined
   5974      symbols we found.  The only way to do this is to search all the
   5975      symbols.  Since we only need the information for non functions in
   5976      dynamic objects, that's the only time we actually put anything on
   5977      the list WEAKS.  We need this information so that if a regular
   5978      object refers to a symbol defined weakly in a dynamic object, the
   5979      real symbol in the dynamic object is also put in the dynamic
   5980      symbols; we also must arrange for both symbols to point to the
   5981      same memory location.  We could handle the general case of symbol
   5982      aliasing, but a general symbol alias can only be generated in
   5983      assembler code, handling it correctly would be very time
   5984      consuming, and other ELF linkers don't handle general aliasing
   5985      either.  */
   5986   if (weaks != NULL)
   5987     {
   5988       struct elf_link_hash_entry **hpp;
   5989       struct elf_link_hash_entry **hppend;
   5990       struct elf_link_hash_entry **sorted_sym_hash;
   5991       struct elf_link_hash_entry *h;
   5992       size_t sym_count, amt;
   5993 
   5994       /* Since we have to search the whole symbol list for each weak
   5995 	 defined symbol, search time for N weak defined symbols will be
   5996 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   5997       amt = extsymcount * sizeof (*sorted_sym_hash);
   5998       sorted_sym_hash = bfd_malloc (amt);
   5999       if (sorted_sym_hash == NULL)
   6000 	goto error_return;
   6001       sym_hash = sorted_sym_hash;
   6002       hpp = elf_sym_hashes (abfd);
   6003       hppend = hpp + extsymcount;
   6004       sym_count = 0;
   6005       for (; hpp < hppend; hpp++)
   6006 	{
   6007 	  h = *hpp;
   6008 	  if (h != NULL
   6009 	      && h->root.type == bfd_link_hash_defined
   6010 	      && !bed->is_function_type (h->type))
   6011 	    {
   6012 	      *sym_hash = h;
   6013 	      sym_hash++;
   6014 	      sym_count++;
   6015 	    }
   6016 	}
   6017 
   6018       qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
   6019 	     elf_sort_symbol);
   6020 
   6021       while (weaks != NULL)
   6022 	{
   6023 	  struct elf_link_hash_entry *hlook;
   6024 	  asection *slook;
   6025 	  bfd_vma vlook;
   6026 	  size_t i, j, idx = 0;
   6027 
   6028 	  hlook = weaks;
   6029 	  weaks = hlook->u.alias;
   6030 	  hlook->u.alias = NULL;
   6031 
   6032 	  if (hlook->root.type != bfd_link_hash_defined
   6033 	      && hlook->root.type != bfd_link_hash_defweak)
   6034 	    continue;
   6035 
   6036 	  slook = hlook->root.u.def.section;
   6037 	  vlook = hlook->root.u.def.value;
   6038 
   6039 	  i = 0;
   6040 	  j = sym_count;
   6041 	  while (i != j)
   6042 	    {
   6043 	      bfd_signed_vma vdiff;
   6044 	      idx = (i + j) / 2;
   6045 	      h = sorted_sym_hash[idx];
   6046 	      vdiff = vlook - h->root.u.def.value;
   6047 	      if (vdiff < 0)
   6048 		j = idx;
   6049 	      else if (vdiff > 0)
   6050 		i = idx + 1;
   6051 	      else
   6052 		{
   6053 		  int sdiff = slook->id - h->root.u.def.section->id;
   6054 		  if (sdiff < 0)
   6055 		    j = idx;
   6056 		  else if (sdiff > 0)
   6057 		    i = idx + 1;
   6058 		  else
   6059 		    break;
   6060 		}
   6061 	    }
   6062 
   6063 	  /* We didn't find a value/section match.  */
   6064 	  if (i == j)
   6065 	    continue;
   6066 
   6067 	  /* With multiple aliases, or when the weak symbol is already
   6068 	     strongly defined, we have multiple matching symbols and
   6069 	     the binary search above may land on any of them.  Step
   6070 	     one past the matching symbol(s).  */
   6071 	  while (++idx != j)
   6072 	    {
   6073 	      h = sorted_sym_hash[idx];
   6074 	      if (h->root.u.def.section != slook
   6075 		  || h->root.u.def.value != vlook)
   6076 		break;
   6077 	    }
   6078 
   6079 	  /* Now look back over the aliases.  Since we sorted by size
   6080 	     as well as value and section, we'll choose the one with
   6081 	     the largest size.  */
   6082 	  while (idx-- != i)
   6083 	    {
   6084 	      h = sorted_sym_hash[idx];
   6085 
   6086 	      /* Stop if value or section doesn't match.  */
   6087 	      if (h->root.u.def.section != slook
   6088 		  || h->root.u.def.value != vlook)
   6089 		break;
   6090 	      else if (h != hlook)
   6091 		{
   6092 		  struct elf_link_hash_entry *t;
   6093 
   6094 		  hlook->u.alias = h;
   6095 		  hlook->is_weakalias = 1;
   6096 		  t = h;
   6097 		  if (t->u.alias != NULL)
   6098 		    while (t->u.alias != h)
   6099 		      t = t->u.alias;
   6100 		  t->u.alias = hlook;
   6101 
   6102 		  /* If the weak definition is in the list of dynamic
   6103 		     symbols, make sure the real definition is put
   6104 		     there as well.  */
   6105 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   6106 		    {
   6107 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6108 			{
   6109 			err_free_sym_hash:
   6110 			  free (sorted_sym_hash);
   6111 			  goto error_return;
   6112 			}
   6113 		    }
   6114 
   6115 		  /* If the real definition is in the list of dynamic
   6116 		     symbols, make sure the weak definition is put
   6117 		     there as well.  If we don't do this, then the
   6118 		     dynamic loader might not merge the entries for the
   6119 		     real definition and the weak definition.  */
   6120 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   6121 		    {
   6122 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   6123 			goto err_free_sym_hash;
   6124 		    }
   6125 		  break;
   6126 		}
   6127 	    }
   6128 	}
   6129 
   6130       free (sorted_sym_hash);
   6131     }
   6132 
   6133   if (bed->check_directives
   6134       && !(*bed->check_directives) (abfd, info))
   6135     goto error_return;
   6136 
   6137   /* If this is a non-traditional link, try to optimize the handling
   6138      of the .stab/.stabstr sections.  */
   6139   if (! dynamic
   6140       && ! info->traditional_format
   6141       && is_elf_hash_table (&htab->root)
   6142       && (info->strip != strip_all && info->strip != strip_debugger))
   6143     {
   6144       asection *stabstr;
   6145 
   6146       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   6147       if (stabstr != NULL)
   6148 	{
   6149 	  bfd_size_type string_offset = 0;
   6150 	  asection *stab;
   6151 
   6152 	  for (stab = abfd->sections; stab; stab = stab->next)
   6153 	    if (startswith (stab->name, ".stab")
   6154 		&& (!stab->name[5] ||
   6155 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   6156 		&& (stab->flags & SEC_MERGE) == 0
   6157 		&& !bfd_is_abs_section (stab->output_section)
   6158 		&& !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   6159 					     stabstr, &string_offset))
   6160 	      goto error_return;
   6161 	}
   6162     }
   6163 
   6164   if (dynamic && add_needed)
   6165     {
   6166       /* Add this bfd to the loaded list.  */
   6167       struct elf_link_loaded_list *n;
   6168 
   6169       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   6170       if (n == NULL)
   6171 	goto error_return;
   6172       n->abfd = abfd;
   6173       n->next = htab->dyn_loaded;
   6174       htab->dyn_loaded = n;
   6175     }
   6176   if (dynamic && !add_needed
   6177       && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
   6178     elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
   6179 
   6180   return true;
   6181 
   6182  error_free_vers:
   6183   free (old_tab);
   6184   free (old_strtab);
   6185   free (nondeflt_vers);
   6186   free (extversym);
   6187  error_free_sym:
   6188   free (isymbuf);
   6189  error_return:
   6190   return false;
   6191 }
   6192 
   6193 /* Return the linker hash table entry of a symbol that might be
   6194    satisfied by an archive symbol.  Return -1 on error.  */
   6195 
   6196 struct bfd_link_hash_entry *
   6197 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   6198 				struct bfd_link_info *info,
   6199 				const char *name)
   6200 {
   6201   struct bfd_link_hash_entry *h;
   6202   const char *p;
   6203   char *copy;
   6204   size_t len, first;
   6205 
   6206   h = bfd_link_hash_lookup (info->hash, name, false, false, true);
   6207   if (h != NULL)
   6208     return h;
   6209 
   6210   /* If this is a default version (the name contains @@), look up the
   6211      symbol again with only one `@' as well as without the version.
   6212      The effect is that references to the symbol with and without the
   6213      version will be matched by the default symbol in the archive.  */
   6214 
   6215   p = strchr (name, ELF_VER_CHR);
   6216   if (p == NULL || p[1] != ELF_VER_CHR)
   6217     {
   6218       /* Add this symbol to first hash if this archive has the first
   6219 	 definition.  */
   6220       if (is_elf_hash_table (info->hash))
   6221 	elf_link_add_to_first_hash (abfd, info, name, false);
   6222       return h;
   6223     }
   6224 
   6225   /* First check with only one `@'.  */
   6226   len = strlen (name);
   6227   copy = (char *) bfd_alloc (abfd, len);
   6228   if (copy == NULL)
   6229     return (struct bfd_link_hash_entry *) -1;
   6230 
   6231   first = p - name + 1;
   6232   memcpy (copy, name, first);
   6233   memcpy (copy + first, name + first + 1, len - first);
   6234 
   6235   h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   6236   if (h == NULL)
   6237     {
   6238       /* We also need to check references to the symbol without the
   6239 	 version.  */
   6240       copy[first - 1] = '\0';
   6241       h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   6242     }
   6243 
   6244   bfd_release (abfd, copy);
   6245   return h;
   6246 }
   6247 
   6248 /* Add symbols from an ELF archive file to the linker hash table.  We
   6249    don't use _bfd_generic_link_add_archive_symbols because we need to
   6250    handle versioned symbols.
   6251 
   6252    Fortunately, ELF archive handling is simpler than that done by
   6253    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   6254    oddities.  In ELF, if we find a symbol in the archive map, and the
   6255    symbol is currently undefined, we know that we must pull in that
   6256    object file.
   6257 
   6258    Unfortunately, we do have to make multiple passes over the symbol
   6259    table until nothing further is resolved.  */
   6260 
   6261 static bool
   6262 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   6263 {
   6264   symindex c;
   6265   unsigned char *included = NULL;
   6266   carsym *symdefs;
   6267   bool loop;
   6268   size_t amt;
   6269   elf_backend_data *bed;
   6270   struct bfd_link_hash_entry * (*archive_symbol_lookup)
   6271     (bfd *, struct bfd_link_info *, const char *);
   6272 
   6273   if (! bfd_has_map (abfd))
   6274     {
   6275       /* An empty archive is a special case.  */
   6276       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   6277 	return true;
   6278       bfd_set_error (bfd_error_no_armap);
   6279       return false;
   6280     }
   6281 
   6282   /* Keep track of all symbols we know to be already defined, and all
   6283      files we know to be already included.  This is to speed up the
   6284      second and subsequent passes.  */
   6285   c = bfd_ardata (abfd)->symdef_count;
   6286   if (c == 0)
   6287     return true;
   6288   amt = c * sizeof (*included);
   6289   included = (unsigned char *) bfd_zmalloc (amt);
   6290   if (included == NULL)
   6291     return false;
   6292 
   6293   symdefs = bfd_ardata (abfd)->symdefs;
   6294   bed = get_elf_backend_data (abfd);
   6295   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   6296 
   6297   do
   6298     {
   6299       file_ptr last;
   6300       symindex i;
   6301       carsym *symdef;
   6302       carsym *symdefend;
   6303 
   6304       loop = false;
   6305       last = -1;
   6306 
   6307       symdef = symdefs;
   6308       symdefend = symdef + c;
   6309       for (i = 0; symdef < symdefend; symdef++, i++)
   6310 	{
   6311 	  struct bfd_link_hash_entry *h;
   6312 	  bfd *element;
   6313 	  struct bfd_link_hash_entry *undefs_tail;
   6314 	  symindex mark;
   6315 
   6316 	  if (included[i])
   6317 	    continue;
   6318 	  if (symdef->file_offset == last)
   6319 	    {
   6320 	      included[i] = true;
   6321 	      continue;
   6322 	    }
   6323 
   6324 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   6325 	  if (h == (struct bfd_link_hash_entry *) -1)
   6326 	    goto error_return;
   6327 
   6328 	  if (h == NULL)
   6329 	    continue;
   6330 
   6331 	  if (h->type == bfd_link_hash_undefined)
   6332 	    {
   6333 	      if (is_elf_hash_table (info->hash))
   6334 		{
   6335 		  /* If the archive element has already been loaded then one
   6336 		     of the symbols defined by that element might have been
   6337 		     made undefined due to being in a discarded section.  */
   6338 		  if (((struct elf_link_hash_entry *) h)->indx == -3)
   6339 		    continue;
   6340 
   6341 		  /* In the pre-LTO-plugin pass we must not mistakenly
   6342 		     include this archive member if an earlier shared
   6343 		     library defined this symbol.  */
   6344 		  struct elf_link_hash_table *htab = elf_hash_table (info);
   6345 		  if (htab->first_hash)
   6346 		    {
   6347 		      struct elf_link_first_hash_entry *e
   6348 			  = ((struct elf_link_first_hash_entry *)
   6349 			     bfd_hash_lookup (htab->first_hash, symdef->name,
   6350 					      false, false));
   6351 		      if (e
   6352 			  && (e->abfd->flags & DYNAMIC) != 0
   6353 			  && e->abfd != abfd)
   6354 			continue;
   6355 		    }
   6356 		}
   6357 	    }
   6358 	  else if (h->type == bfd_link_hash_common)
   6359 	    {
   6360 	      /* We currently have a common symbol.  The archive map contains
   6361 		 a reference to this symbol, so we may want to include it.  We
   6362 		 only want to include it however, if this archive element
   6363 		 contains a definition of the symbol, not just another common
   6364 		 declaration of it.
   6365 
   6366 		 Unfortunately some archivers (including GNU ar) will put
   6367 		 declarations of common symbols into their archive maps, as
   6368 		 well as real definitions, so we cannot just go by the archive
   6369 		 map alone.  Instead we must read in the element's symbol
   6370 		 table and check that to see what kind of symbol definition
   6371 		 this is.  */
   6372 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   6373 		continue;
   6374 	    }
   6375 	  else
   6376 	    {
   6377 	      if (h->type != bfd_link_hash_undefweak)
   6378 		/* Symbol must be defined.  Don't check it again.  */
   6379 		included[i] = true;
   6380 
   6381 	      if (!is_elf_hash_table (info->hash))
   6382 		continue;
   6383 	      struct elf_link_hash_entry *eh
   6384 		= (struct elf_link_hash_entry *) h;
   6385 	      /* Ignore the archive if the symbol isn't referenced by a
   6386 		 regular object or isn't defined in a shared object.  */
   6387 	      if (!eh->ref_regular || !eh->def_dynamic)
   6388 		continue;
   6389 	      /* Ignore the dynamic definition if symbol is first
   6390 		 defined in this archive.  */
   6391 	      struct elf_link_hash_table *htab = elf_hash_table (info);
   6392 	      if (htab->first_hash == NULL)
   6393 		continue;
   6394 	      struct elf_link_first_hash_entry *e
   6395 		= ((struct elf_link_first_hash_entry *)
   6396 		   bfd_hash_lookup (htab->first_hash, symdef->name,
   6397 				    false, false));
   6398 	      if (e == NULL || e->abfd != abfd)
   6399 		continue;
   6400 	    }
   6401 
   6402 	  /* We need to include this archive member.  */
   6403 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
   6404 					     info);
   6405 	  if (element == NULL)
   6406 	    goto error_return;
   6407 
   6408 	  if (! bfd_check_format (element, bfd_object))
   6409 	    goto error_return;
   6410 
   6411 	  undefs_tail = info->hash->undefs_tail;
   6412 
   6413 	  if (!(*info->callbacks
   6414 		->add_archive_element) (info, element, symdef->name, &element))
   6415 	    continue;
   6416 	  if (!bfd_link_add_symbols (element, info))
   6417 	    goto error_return;
   6418 
   6419 	  /* If there are any new undefined symbols, we need to make
   6420 	     another pass through the archive in order to see whether
   6421 	     they can be defined.  FIXME: This isn't perfect, because
   6422 	     common symbols wind up on undefs_tail and because an
   6423 	     undefined symbol which is defined later on in this pass
   6424 	     does not require another pass.  This isn't a bug, but it
   6425 	     does make the code less efficient than it could be.  */
   6426 	  if (undefs_tail != info->hash->undefs_tail)
   6427 	    loop = true;
   6428 
   6429 	  /* Look backward to mark all symbols from this object file
   6430 	     which we have already seen in this pass.  */
   6431 	  mark = i;
   6432 	  do
   6433 	    {
   6434 	      included[mark] = true;
   6435 	      if (mark == 0)
   6436 		break;
   6437 	      --mark;
   6438 	    }
   6439 	  while (symdefs[mark].file_offset == symdef->file_offset);
   6440 
   6441 	  /* We mark subsequent symbols from this object file as we go
   6442 	     on through the loop.  */
   6443 	  last = symdef->file_offset;
   6444 	}
   6445     }
   6446   while (loop);
   6447 
   6448   free (included);
   6449   return true;
   6450 
   6451  error_return:
   6452   free (included);
   6453   return false;
   6454 }
   6455 
   6456 /* Given an ELF BFD, add symbols to the global hash table as
   6457    appropriate.  */
   6458 
   6459 bool
   6460 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   6461 {
   6462   switch (bfd_get_format (abfd))
   6463     {
   6464     case bfd_object:
   6465       return elf_link_add_object_symbols (abfd, info);
   6466     case bfd_archive:
   6467       return elf_link_add_archive_symbols (abfd, info);
   6468     default:
   6469       bfd_set_error (bfd_error_wrong_format);
   6470       return false;
   6471     }
   6472 }
   6473 
   6474 struct hash_codes_info
   6476 {
   6477   unsigned long *hashcodes;
   6478   bool error;
   6479 };
   6480 
   6481 /* This function will be called though elf_link_hash_traverse to store
   6482    all hash value of the exported symbols in an array.  */
   6483 
   6484 static bool
   6485 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   6486 {
   6487   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   6488   const char *name;
   6489   unsigned long ha;
   6490   char *alc = NULL;
   6491 
   6492   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6493   if (h->dynindx == -1)
   6494     return true;
   6495 
   6496   name = h->root.root.string;
   6497   if (h->versioned >= versioned)
   6498     {
   6499       const char *p = strchr (name, ELF_VER_CHR);
   6500       if (p != NULL)
   6501 	{
   6502 	  alc = (char *) bfd_malloc (p - name + 1);
   6503 	  if (alc == NULL)
   6504 	    {
   6505 	      inf->error = true;
   6506 	      return false;
   6507 	    }
   6508 	  memcpy (alc, name, p - name);
   6509 	  alc[p - name] = '\0';
   6510 	  name = alc;
   6511 	}
   6512     }
   6513 
   6514   /* Compute the hash value.  */
   6515   ha = bfd_elf_hash (name);
   6516 
   6517   /* Store the found hash value in the array given as the argument.  */
   6518   *(inf->hashcodes)++ = ha;
   6519 
   6520   /* And store it in the struct so that we can put it in the hash table
   6521      later.  */
   6522   h->u.elf_hash_value = ha;
   6523 
   6524   free (alc);
   6525   return true;
   6526 }
   6527 
   6528 struct collect_gnu_hash_codes
   6529 {
   6530   bfd *output_bfd;
   6531   elf_backend_data *bed;
   6532   unsigned long int nsyms;
   6533   unsigned long int maskbits;
   6534   unsigned long int *hashcodes;
   6535   unsigned long int *hashval;
   6536   unsigned long int *indx;
   6537   unsigned long int *counts;
   6538   bfd_vma *bitmask;
   6539   bfd_byte *contents;
   6540   bfd_size_type xlat;
   6541   long int min_dynindx;
   6542   unsigned long int bucketcount;
   6543   unsigned long int symindx;
   6544   long int local_indx;
   6545   long int shift1, shift2;
   6546   unsigned long int mask;
   6547   bool error;
   6548   bool base_symbol;
   6549 };
   6550 
   6551 /* This function will be called though elf_link_hash_traverse to store
   6552    all hash value of the exported symbols in an array.  */
   6553 
   6554 static bool
   6555 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   6556 {
   6557   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6558   const char *name;
   6559   unsigned long ha;
   6560   char *alc = NULL;
   6561 
   6562   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6563   if (h->dynindx == -1)
   6564     return true;
   6565 
   6566   /* Ignore also local symbols and undefined symbols.  */
   6567   if (! (*s->bed->elf_hash_symbol) (h))
   6568     return true;
   6569 
   6570   name = h->root.root.string;
   6571   if (h->versioned >= versioned)
   6572     {
   6573       const char *p = strchr (name, ELF_VER_CHR);
   6574       if (p != NULL)
   6575 	{
   6576 	  alc = (char *) bfd_malloc (p - name + 1);
   6577 	  if (alc == NULL)
   6578 	    {
   6579 	      s->error = true;
   6580 	      return false;
   6581 	    }
   6582 	  memcpy (alc, name, p - name);
   6583 	  alc[p - name] = '\0';
   6584 	  name = alc;
   6585 	}
   6586     }
   6587 
   6588   /* Compute the hash value.  */
   6589   ha = bfd_elf_gnu_hash (name);
   6590 
   6591   /* Store the found hash value in the array for compute_bucket_count,
   6592      and also for .dynsym reordering purposes.  */
   6593   s->hashcodes[s->nsyms] = ha;
   6594   s->hashval[h->dynindx] = ha;
   6595   ++s->nsyms;
   6596   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   6597     s->min_dynindx = h->dynindx;
   6598 
   6599   free (alc);
   6600   return true;
   6601 }
   6602 
   6603 /* This function will be called though elf_link_hash_traverse to do
   6604    final dynamic symbol renumbering in case of .gnu.hash.
   6605    If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
   6606    to the translation table.  */
   6607 
   6608 static bool
   6609 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
   6610 {
   6611   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6612   unsigned long int bucket;
   6613   unsigned long int val;
   6614 
   6615   /* Ignore indirect symbols.  */
   6616   if (h->dynindx == -1)
   6617     return true;
   6618 
   6619   /* Skip if base symbol doesn't match.  */
   6620   if (s->base_symbol != !!h->base_symbol)
   6621     return true;
   6622 
   6623   /* Ignore also local symbols and undefined symbols.  */
   6624   if (! (*s->bed->elf_hash_symbol) (h))
   6625     {
   6626       if (h->dynindx >= s->min_dynindx)
   6627 	{
   6628 	  if (s->bed->record_xhash_symbol != NULL)
   6629 	    {
   6630 	      (*s->bed->record_xhash_symbol) (h, 0);
   6631 	      s->local_indx++;
   6632 	    }
   6633 	  else
   6634 	    h->dynindx = s->local_indx++;
   6635 	}
   6636       return true;
   6637     }
   6638 
   6639   bucket = s->hashval[h->dynindx] % s->bucketcount;
   6640   val = (s->hashval[h->dynindx] >> s->shift1)
   6641 	& ((s->maskbits >> s->shift1) - 1);
   6642   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   6643   s->bitmask[val]
   6644     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   6645   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   6646   if (s->counts[bucket] == 1)
   6647     /* Last element terminates the chain.  */
   6648     val |= 1;
   6649   bfd_put_32 (s->output_bfd, val,
   6650 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   6651   --s->counts[bucket];
   6652   if (s->bed->record_xhash_symbol != NULL)
   6653     {
   6654       bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
   6655 
   6656       (*s->bed->record_xhash_symbol) (h, xlat_loc);
   6657     }
   6658   else
   6659     h->dynindx = s->indx[bucket]++;
   6660   return true;
   6661 }
   6662 
   6663 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   6664 
   6665 bool
   6666 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   6667 {
   6668   return !(h->forced_local
   6669 	   || h->root.type == bfd_link_hash_undefined
   6670 	   || h->root.type == bfd_link_hash_undefweak
   6671 	   || ((h->root.type == bfd_link_hash_defined
   6672 		|| h->root.type == bfd_link_hash_defweak)
   6673 	       && h->root.u.def.section->output_section == NULL));
   6674 }
   6675 
   6676 /* Array used to determine the number of hash table buckets to use
   6677    based on the number of symbols there are.  If there are fewer than
   6678    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   6679    fewer than 37 we use 17 buckets, and so forth.  We never use more
   6680    than 32771 buckets.  */
   6681 
   6682 static const size_t elf_buckets[] =
   6683 {
   6684   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   6685   16411, 32771, 0
   6686 };
   6687 
   6688 /* Compute bucket count for hashing table.  We do not use a static set
   6689    of possible tables sizes anymore.  Instead we determine for all
   6690    possible reasonable sizes of the table the outcome (i.e., the
   6691    number of collisions etc) and choose the best solution.  The
   6692    weighting functions are not too simple to allow the table to grow
   6693    without bounds.  Instead one of the weighting factors is the size.
   6694    Therefore the result is always a good payoff between few collisions
   6695    (= short chain lengths) and table size.  */
   6696 static size_t
   6697 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6698 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   6699 		      unsigned long int nsyms,
   6700 		      int gnu_hash)
   6701 {
   6702   size_t best_size = 0;
   6703   unsigned long int i;
   6704 
   6705   if (info->optimize)
   6706     {
   6707       size_t minsize;
   6708       size_t maxsize;
   6709       uint64_t best_chlen = ~((uint64_t) 0);
   6710       bfd *dynobj = elf_hash_table (info)->dynobj;
   6711       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   6712       elf_backend_data *bed = get_elf_backend_data (dynobj);
   6713       unsigned long int *counts;
   6714       bfd_size_type amt;
   6715       unsigned int no_improvement_count = 0;
   6716 
   6717       /* Possible optimization parameters: if we have NSYMS symbols we say
   6718 	 that the hashing table must at least have NSYMS/4 and at most
   6719 	 2*NSYMS buckets.  */
   6720       minsize = nsyms / 4;
   6721       if (minsize == 0)
   6722 	minsize = 1;
   6723       best_size = maxsize = nsyms * 2;
   6724       if (gnu_hash)
   6725 	{
   6726 	  if (minsize < 2)
   6727 	    minsize = 2;
   6728 	  if ((best_size & 31) == 0)
   6729 	    ++best_size;
   6730 	}
   6731 
   6732       /* Create array where we count the collisions in.  We must use bfd_malloc
   6733 	 since the size could be large.  */
   6734       amt = maxsize;
   6735       amt *= sizeof (unsigned long int);
   6736       counts = (unsigned long int *) bfd_malloc (amt);
   6737       if (counts == NULL)
   6738 	return 0;
   6739 
   6740       /* Compute the "optimal" size for the hash table.  The criteria is a
   6741 	 minimal chain length.  The minor criteria is (of course) the size
   6742 	 of the table.  */
   6743       for (i = minsize; i < maxsize; ++i)
   6744 	{
   6745 	  /* Walk through the array of hashcodes and count the collisions.  */
   6746 	  uint64_t max;
   6747 	  unsigned long int j;
   6748 	  unsigned long int fact;
   6749 
   6750 	  if (gnu_hash && (i & 31) == 0)
   6751 	    continue;
   6752 
   6753 	  memset (counts, '\0', i * sizeof (unsigned long int));
   6754 
   6755 	  /* Determine how often each hash bucket is used.  */
   6756 	  for (j = 0; j < nsyms; ++j)
   6757 	    ++counts[hashcodes[j] % i];
   6758 
   6759 	  /* For the weight function we need some information about the
   6760 	     pagesize on the target.  This is information need not be 100%
   6761 	     accurate.  Since this information is not available (so far) we
   6762 	     define it here to a reasonable default value.  If it is crucial
   6763 	     to have a better value some day simply define this value.  */
   6764 # ifndef BFD_TARGET_PAGESIZE
   6765 #  define BFD_TARGET_PAGESIZE	(4096)
   6766 # endif
   6767 
   6768 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   6769 	     and the chains.  */
   6770 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   6771 
   6772 # if 1
   6773 	  /* Variant 1: optimize for short chains.  We add the squares
   6774 	     of all the chain lengths (which favors many small chain
   6775 	     over a few long chains).  */
   6776 	  for (j = 0; j < i; ++j)
   6777 	    max += counts[j] * counts[j];
   6778 
   6779 	  /* This adds penalties for the overall size of the table.  */
   6780 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6781 	  max *= fact * fact;
   6782 # else
   6783 	  /* Variant 2: Optimize a lot more for small table.  Here we
   6784 	     also add squares of the size but we also add penalties for
   6785 	     empty slots (the +1 term).  */
   6786 	  for (j = 0; j < i; ++j)
   6787 	    max += (1 + counts[j]) * (1 + counts[j]);
   6788 
   6789 	  /* The overall size of the table is considered, but not as
   6790 	     strong as in variant 1, where it is squared.  */
   6791 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6792 	  max *= fact;
   6793 # endif
   6794 
   6795 	  /* Compare with current best results.  */
   6796 	  if (max < best_chlen)
   6797 	    {
   6798 	      best_chlen = max;
   6799 	      best_size = i;
   6800 	      no_improvement_count = 0;
   6801 	    }
   6802 	  /* PR 11843: Avoid futile long searches for the best bucket size
   6803 	     when there are a large number of symbols.  */
   6804 	  else if (++no_improvement_count == 100)
   6805 	    break;
   6806 	}
   6807 
   6808       free (counts);
   6809     }
   6810   else
   6811     {
   6812       for (i = 0; elf_buckets[i] != 0; i++)
   6813 	{
   6814 	  best_size = elf_buckets[i];
   6815 	  if (nsyms < elf_buckets[i + 1])
   6816 	    break;
   6817 	}
   6818       if (gnu_hash && best_size < 2)
   6819 	best_size = 2;
   6820     }
   6821 
   6822   return best_size;
   6823 }
   6824 
   6825 /* Size any SHT_GROUP section for ld -r.  */
   6826 
   6827 bool
   6828 bfd_elf_size_group_sections (struct bfd_link_info *info)
   6829 {
   6830   bfd *ibfd;
   6831   asection *s;
   6832 
   6833   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6834     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   6835 	&& (s = ibfd->sections) != NULL
   6836 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
   6837 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   6838       return false;
   6839   return true;
   6840 }
   6841 
   6842 /* Set a default stack segment size.  The value in INFO wins.  If it
   6843    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   6844    undefined it is initialized.  */
   6845 
   6846 bool
   6847 bfd_elf_stack_segment_size (bfd *output_bfd,
   6848 			    struct bfd_link_info *info,
   6849 			    const char *legacy_symbol,
   6850 			    bfd_vma default_size)
   6851 {
   6852   struct elf_link_hash_entry *h = NULL;
   6853 
   6854   /* Look for legacy symbol.  */
   6855   if (legacy_symbol)
   6856     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   6857 			      false, false, false);
   6858   if (h && (h->root.type == bfd_link_hash_defined
   6859 	    || h->root.type == bfd_link_hash_defweak)
   6860       && h->def_regular
   6861       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   6862     {
   6863       /* The symbol has no type if specified on the command line.  */
   6864       h->type = STT_OBJECT;
   6865       if (info->stacksize)
   6866 	/* xgettext:c-format */
   6867 	_bfd_error_handler (_("%pB: stack size specified and %s set"),
   6868 			    output_bfd, legacy_symbol);
   6869       else if (h->root.u.def.section != bfd_abs_section_ptr)
   6870 	/* xgettext:c-format */
   6871 	_bfd_error_handler (_("%pB: %s not absolute"),
   6872 			    output_bfd, legacy_symbol);
   6873       else
   6874 	info->stacksize = h->root.u.def.value;
   6875     }
   6876 
   6877   if (!info->stacksize)
   6878     /* If the user didn't set a size, or explicitly inhibit the
   6879        size, set it now.  */
   6880     info->stacksize = default_size;
   6881 
   6882   /* Provide the legacy symbol, if it is referenced.  */
   6883   if (h && (h->root.type == bfd_link_hash_undefined
   6884 	    || h->root.type == bfd_link_hash_undefweak))
   6885     {
   6886       struct bfd_link_hash_entry *bh = NULL;
   6887 
   6888       if (!(_bfd_generic_link_add_one_symbol
   6889 	    (info, output_bfd, legacy_symbol,
   6890 	     BSF_GLOBAL, bfd_abs_section_ptr,
   6891 	     info->stacksize >= 0 ? info->stacksize : 0,
   6892 	     NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
   6893 	return false;
   6894 
   6895       h = (struct elf_link_hash_entry *) bh;
   6896       h->def_regular = 1;
   6897       h->type = STT_OBJECT;
   6898     }
   6899 
   6900   return true;
   6901 }
   6902 
   6903 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   6904 
   6905 struct elf_gc_sweep_symbol_info
   6906 {
   6907   struct bfd_link_info *info;
   6908   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   6909 		       bool);
   6910 };
   6911 
   6912 static bool
   6913 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   6914 {
   6915   if (!h->mark
   6916       && (((h->root.type == bfd_link_hash_defined
   6917 	    || h->root.type == bfd_link_hash_defweak)
   6918 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   6919 		&& h->root.u.def.section->gc_mark))
   6920 	  || h->root.type == bfd_link_hash_undefined
   6921 	  || h->root.type == bfd_link_hash_undefweak))
   6922     {
   6923       struct elf_gc_sweep_symbol_info *inf;
   6924 
   6925       inf = (struct elf_gc_sweep_symbol_info *) data;
   6926       (*inf->hide_symbol) (inf->info, h, true);
   6927       h->def_regular = 0;
   6928       h->ref_regular = 0;
   6929       h->ref_regular_nonweak = 0;
   6930     }
   6931 
   6932   return true;
   6933 }
   6934 
   6935 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   6936    called by the ELF linker emulation before_allocation routine.  We
   6937    must set the sizes of the sections before the linker sets the
   6938    addresses of the various sections.  */
   6939 
   6940 bool
   6941 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   6942 			       const char *soname,
   6943 			       const char *rpath,
   6944 			       const char *filter_shlib,
   6945 			       const char *audit,
   6946 			       const char *depaudit,
   6947 			       const char * const *auxiliary_filters,
   6948 			       struct bfd_link_info *info,
   6949 			       asection **sinterpptr)
   6950 {
   6951   bfd *dynobj;
   6952   elf_backend_data *bed;
   6953 
   6954   *sinterpptr = NULL;
   6955 
   6956   if (!is_elf_hash_table (info->hash))
   6957     return true;
   6958 
   6959   /* Any syms created from now on start with -1 in
   6960      got.refcount/offset and plt.refcount/offset.  */
   6961   elf_hash_table (info)->init_got_refcount
   6962     = elf_hash_table (info)->init_got_offset;
   6963   elf_hash_table (info)->init_plt_refcount
   6964     = elf_hash_table (info)->init_plt_offset;
   6965 
   6966   bed = get_elf_backend_data (output_bfd);
   6967 
   6968   /* The backend may have to create some sections regardless of whether
   6969      we're dynamic or not.  */
   6970   if (bed->elf_backend_early_size_sections
   6971       && !bed->elf_backend_early_size_sections (output_bfd, info))
   6972     return false;
   6973 
   6974   dynobj = elf_hash_table (info)->dynobj;
   6975 
   6976   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6977     {
   6978       struct bfd_elf_version_tree *verdefs;
   6979       struct elf_info_failed asvinfo;
   6980       struct bfd_elf_version_tree *t;
   6981       struct bfd_elf_version_expr *d;
   6982       asection *s;
   6983       size_t soname_indx;
   6984 
   6985       /* If we are supposed to export all symbols into the dynamic symbol
   6986 	 table (this is not the normal case), then do so.  */
   6987       if (info->export_dynamic
   6988 	  || (bfd_link_executable (info) && info->dynamic))
   6989 	{
   6990 	  struct elf_info_failed eif;
   6991 
   6992 	  eif.info = info;
   6993 	  eif.failed = false;
   6994 	  elf_link_hash_traverse (elf_hash_table (info),
   6995 				  _bfd_elf_export_symbol,
   6996 				  &eif);
   6997 	  if (eif.failed)
   6998 	    return false;
   6999 	}
   7000 
   7001       if (soname != NULL)
   7002 	{
   7003 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7004 					     soname, true);
   7005 	  if (soname_indx == (size_t) -1
   7006 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   7007 	    return false;
   7008 	}
   7009       else
   7010 	soname_indx = (size_t) -1;
   7011 
   7012       /* Make all global versions with definition.  */
   7013       for (t = info->version_info; t != NULL; t = t->next)
   7014 	for (d = t->globals.list; d != NULL; d = d->next)
   7015 	  if (!d->symver && d->literal)
   7016 	    {
   7017 	      const char *verstr, *name;
   7018 	      size_t namelen, verlen, newlen;
   7019 	      char *newname, *p, leading_char;
   7020 	      struct elf_link_hash_entry *newh;
   7021 
   7022 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   7023 	      name = d->pattern;
   7024 	      namelen = strlen (name) + (leading_char != '\0');
   7025 	      verstr = t->name;
   7026 	      verlen = strlen (verstr);
   7027 	      newlen = namelen + verlen + 3;
   7028 
   7029 	      newname = (char *) bfd_malloc (newlen);
   7030 	      if (newname == NULL)
   7031 		return false;
   7032 	      newname[0] = leading_char;
   7033 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   7034 
   7035 	      /* Check the hidden versioned definition.  */
   7036 	      p = newname + namelen;
   7037 	      *p++ = ELF_VER_CHR;
   7038 	      memcpy (p, verstr, verlen + 1);
   7039 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   7040 					   newname, false, false,
   7041 					   false);
   7042 	      if (newh == NULL
   7043 		  || (newh->root.type != bfd_link_hash_defined
   7044 		      && newh->root.type != bfd_link_hash_defweak))
   7045 		{
   7046 		  /* Check the default versioned definition.  */
   7047 		  *p++ = ELF_VER_CHR;
   7048 		  memcpy (p, verstr, verlen + 1);
   7049 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   7050 					       newname, false, false,
   7051 					       false);
   7052 		}
   7053 	      free (newname);
   7054 
   7055 	      /* Mark this version if there is a definition and it is
   7056 		 not defined in a shared object.  */
   7057 	      if (newh != NULL
   7058 		  && !newh->def_dynamic
   7059 		  && (newh->root.type == bfd_link_hash_defined
   7060 		      || newh->root.type == bfd_link_hash_defweak))
   7061 		d->symver = 1;
   7062 	    }
   7063 
   7064       /* Attach all the symbols to their version information.  */
   7065       asvinfo.info = info;
   7066       asvinfo.failed = false;
   7067 
   7068       elf_link_hash_traverse (elf_hash_table (info),
   7069 			      _bfd_elf_link_assign_sym_version,
   7070 			      &asvinfo);
   7071       if (asvinfo.failed)
   7072 	return false;
   7073 
   7074       if (!info->allow_undefined_version)
   7075 	{
   7076 	  /* Check if all global versions have a definition.  */
   7077 	  bool all_defined = true;
   7078 	  for (t = info->version_info; t != NULL; t = t->next)
   7079 	    for (d = t->globals.list; d != NULL; d = d->next)
   7080 	      if (d->literal && !d->symver && !d->script)
   7081 		{
   7082 		  _bfd_error_handler
   7083 		    (_("%s: undefined version: %s"),
   7084 		     d->pattern, t->name);
   7085 		  all_defined = false;
   7086 		}
   7087 
   7088 	  if (!all_defined)
   7089 	    {
   7090 	      bfd_set_error (bfd_error_bad_value);
   7091 	      return false;
   7092 	    }
   7093 	}
   7094 
   7095       /* Set up the version definition section.  */
   7096       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   7097       BFD_ASSERT (s != NULL);
   7098 
   7099       /* We may have created additional version definitions if we are
   7100 	 just linking a regular application.  */
   7101       verdefs = info->version_info;
   7102 
   7103       /* Skip anonymous version tag.  */
   7104       if (verdefs != NULL && verdefs->vernum == 0)
   7105 	verdefs = verdefs->next;
   7106 
   7107       if (verdefs == NULL && !info->create_default_symver)
   7108 	s->flags |= SEC_EXCLUDE;
   7109       else
   7110 	{
   7111 	  unsigned int cdefs;
   7112 	  bfd_size_type size;
   7113 	  bfd_byte *p;
   7114 	  Elf_Internal_Verdef def;
   7115 	  Elf_Internal_Verdaux defaux;
   7116 	  struct bfd_link_hash_entry *bh;
   7117 	  struct elf_link_hash_entry *h;
   7118 	  const char *name;
   7119 
   7120 	  cdefs = 0;
   7121 	  size = 0;
   7122 
   7123 	  /* Make space for the base version.  */
   7124 	  size += sizeof (Elf_External_Verdef);
   7125 	  size += sizeof (Elf_External_Verdaux);
   7126 	  ++cdefs;
   7127 
   7128 	  /* Make space for the default version.  */
   7129 	  if (info->create_default_symver)
   7130 	    {
   7131 	      size += sizeof (Elf_External_Verdef);
   7132 	      ++cdefs;
   7133 	    }
   7134 
   7135 	  for (t = verdefs; t != NULL; t = t->next)
   7136 	    {
   7137 	      struct bfd_elf_version_deps *n;
   7138 
   7139 	      /* Don't emit base version twice.  */
   7140 	      if (t->vernum == 0)
   7141 		continue;
   7142 
   7143 	      size += sizeof (Elf_External_Verdef);
   7144 	      size += sizeof (Elf_External_Verdaux);
   7145 	      ++cdefs;
   7146 
   7147 	      for (n = t->deps; n != NULL; n = n->next)
   7148 		size += sizeof (Elf_External_Verdaux);
   7149 	    }
   7150 
   7151 	  s->size = size;
   7152 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7153 	  if (s->contents == NULL && s->size != 0)
   7154 	    return false;
   7155 	  s->alloced = 1;
   7156 
   7157 	  /* Fill in the version definition section.  */
   7158 
   7159 	  p = s->contents;
   7160 
   7161 	  def.vd_version = VER_DEF_CURRENT;
   7162 	  def.vd_flags = VER_FLG_BASE;
   7163 	  def.vd_ndx = 1;
   7164 	  def.vd_cnt = 1;
   7165 	  if (info->create_default_symver)
   7166 	    {
   7167 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   7168 	      def.vd_next = sizeof (Elf_External_Verdef);
   7169 	    }
   7170 	  else
   7171 	    {
   7172 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7173 	      def.vd_next = (sizeof (Elf_External_Verdef)
   7174 			     + sizeof (Elf_External_Verdaux));
   7175 	    }
   7176 
   7177 	  if (soname_indx != (size_t) -1)
   7178 	    {
   7179 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7180 				      soname_indx);
   7181 	      def.vd_hash = bfd_elf_hash (soname);
   7182 	      defaux.vda_name = soname_indx;
   7183 	      name = soname;
   7184 	    }
   7185 	  else
   7186 	    {
   7187 	      size_t indx;
   7188 
   7189 	      name = lbasename (bfd_get_filename (output_bfd));
   7190 	      def.vd_hash = bfd_elf_hash (name);
   7191 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7192 					  name, false);
   7193 	      if (indx == (size_t) -1)
   7194 		return false;
   7195 	      defaux.vda_name = indx;
   7196 	    }
   7197 	  defaux.vda_next = 0;
   7198 
   7199 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   7200 				    (Elf_External_Verdef *) p);
   7201 	  p += sizeof (Elf_External_Verdef);
   7202 	  if (info->create_default_symver)
   7203 	    {
   7204 	      /* Add a symbol representing this version.  */
   7205 	      bh = NULL;
   7206 	      if (! (_bfd_generic_link_add_one_symbol
   7207 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   7208 		      0, NULL, false,
   7209 		      get_elf_backend_data (dynobj)->collect, &bh)))
   7210 		return false;
   7211 	      h = (struct elf_link_hash_entry *) bh;
   7212 	      h->non_elf = 0;
   7213 	      h->def_regular = 1;
   7214 	      h->type = STT_OBJECT;
   7215 	      h->verinfo.vertree = NULL;
   7216 
   7217 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7218 		return false;
   7219 
   7220 	      /* Create a duplicate of the base version with the same
   7221 		 aux block, but different flags.  */
   7222 	      def.vd_flags = 0;
   7223 	      def.vd_ndx = 2;
   7224 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7225 	      if (verdefs)
   7226 		def.vd_next = (sizeof (Elf_External_Verdef)
   7227 			       + sizeof (Elf_External_Verdaux));
   7228 	      else
   7229 		def.vd_next = 0;
   7230 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   7231 					(Elf_External_Verdef *) p);
   7232 	      p += sizeof (Elf_External_Verdef);
   7233 	    }
   7234 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7235 				     (Elf_External_Verdaux *) p);
   7236 	  p += sizeof (Elf_External_Verdaux);
   7237 
   7238 	  for (t = verdefs; t != NULL; t = t->next)
   7239 	    {
   7240 	      unsigned int cdeps;
   7241 	      struct bfd_elf_version_deps *n;
   7242 
   7243 	      /* Don't emit the base version twice.  */
   7244 	      if (t->vernum == 0)
   7245 		continue;
   7246 
   7247 	      cdeps = 0;
   7248 	      for (n = t->deps; n != NULL; n = n->next)
   7249 		++cdeps;
   7250 
   7251 	      /* Add a symbol representing this version.  */
   7252 	      bh = NULL;
   7253 	      if (! (_bfd_generic_link_add_one_symbol
   7254 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   7255 		      0, NULL, false,
   7256 		      get_elf_backend_data (dynobj)->collect, &bh)))
   7257 		return false;
   7258 	      h = (struct elf_link_hash_entry *) bh;
   7259 	      h->non_elf = 0;
   7260 	      h->def_regular = 1;
   7261 	      h->type = STT_OBJECT;
   7262 	      h->verinfo.vertree = t;
   7263 
   7264 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7265 		return false;
   7266 
   7267 	      def.vd_version = VER_DEF_CURRENT;
   7268 	      def.vd_flags = 0;
   7269 	      if (t->globals.list == NULL
   7270 		  && t->locals.list == NULL
   7271 		  && ! t->used)
   7272 		def.vd_flags |= VER_FLG_WEAK;
   7273 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   7274 	      def.vd_cnt = cdeps + 1;
   7275 	      def.vd_hash = bfd_elf_hash (t->name);
   7276 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7277 	      def.vd_next = 0;
   7278 
   7279 	      /* If a basever node is next, it *must* be the last node in
   7280 		 the chain, otherwise Verdef construction breaks.  */
   7281 	      if (t->next != NULL && t->next->vernum == 0)
   7282 		BFD_ASSERT (t->next->next == NULL);
   7283 
   7284 	      if (t->next != NULL && t->next->vernum != 0)
   7285 		def.vd_next = (sizeof (Elf_External_Verdef)
   7286 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   7287 
   7288 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   7289 					(Elf_External_Verdef *) p);
   7290 	      p += sizeof (Elf_External_Verdef);
   7291 
   7292 	      defaux.vda_name = h->dynstr_index;
   7293 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7294 				      h->dynstr_index);
   7295 	      defaux.vda_next = 0;
   7296 	      if (t->deps != NULL)
   7297 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   7298 	      t->name_indx = defaux.vda_name;
   7299 
   7300 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7301 					 (Elf_External_Verdaux *) p);
   7302 	      p += sizeof (Elf_External_Verdaux);
   7303 
   7304 	      for (n = t->deps; n != NULL; n = n->next)
   7305 		{
   7306 		  if (n->version_needed == NULL)
   7307 		    {
   7308 		      /* This can happen if there was an error in the
   7309 			 version script.  */
   7310 		      defaux.vda_name = 0;
   7311 		    }
   7312 		  else
   7313 		    {
   7314 		      defaux.vda_name = n->version_needed->name_indx;
   7315 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7316 					      defaux.vda_name);
   7317 		    }
   7318 		  if (n->next == NULL)
   7319 		    defaux.vda_next = 0;
   7320 		  else
   7321 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   7322 
   7323 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7324 					     (Elf_External_Verdaux *) p);
   7325 		  p += sizeof (Elf_External_Verdaux);
   7326 		}
   7327 	    }
   7328 
   7329 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   7330 	}
   7331     }
   7332 
   7333   if (info->gc_sections && bed->can_gc_sections)
   7334     {
   7335       struct elf_gc_sweep_symbol_info sweep_info;
   7336 
   7337       /* Remove the symbols that were in the swept sections from the
   7338 	 dynamic symbol table.  */
   7339       sweep_info.info = info;
   7340       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   7341       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   7342 			      &sweep_info);
   7343     }
   7344 
   7345   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7346     {
   7347       asection *s;
   7348       struct elf_find_verdep_info sinfo;
   7349 
   7350       /* Work out the size of the version reference section.  */
   7351 
   7352       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   7353       BFD_ASSERT (s != NULL);
   7354 
   7355       sinfo.info = info;
   7356       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   7357       if (sinfo.vers == 0)
   7358 	sinfo.vers = 1;
   7359       sinfo.failed = false;
   7360 
   7361       elf_link_hash_traverse (elf_hash_table (info),
   7362 			      _bfd_elf_link_find_version_dependencies,
   7363 			      &sinfo);
   7364       if (sinfo.failed)
   7365 	return false;
   7366 
   7367       bed->elf_backend_add_glibc_version_dependency (&sinfo);
   7368       if (sinfo.failed)
   7369 	return false;
   7370 
   7371       if (elf_tdata (output_bfd)->verref == NULL)
   7372 	s->flags |= SEC_EXCLUDE;
   7373       else
   7374 	{
   7375 	  Elf_Internal_Verneed *vn;
   7376 	  unsigned int size;
   7377 	  unsigned int crefs;
   7378 	  bfd_byte *p;
   7379 
   7380 	  /* Build the version dependency section.  */
   7381 	  size = 0;
   7382 	  crefs = 0;
   7383 	  for (vn = elf_tdata (output_bfd)->verref;
   7384 	       vn != NULL;
   7385 	       vn = vn->vn_nextref)
   7386 	    {
   7387 	      Elf_Internal_Vernaux *a;
   7388 
   7389 	      size += sizeof (Elf_External_Verneed);
   7390 	      ++crefs;
   7391 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7392 		size += sizeof (Elf_External_Vernaux);
   7393 	    }
   7394 
   7395 	  s->size = size;
   7396 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7397 	  if (s->contents == NULL)
   7398 	    return false;
   7399 	  s->alloced = 1;
   7400 
   7401 	  p = s->contents;
   7402 	  for (vn = elf_tdata (output_bfd)->verref;
   7403 	       vn != NULL;
   7404 	       vn = vn->vn_nextref)
   7405 	    {
   7406 	      unsigned int caux;
   7407 	      Elf_Internal_Vernaux *a;
   7408 	      size_t indx;
   7409 
   7410 	      caux = 0;
   7411 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7412 		++caux;
   7413 
   7414 	      vn->vn_version = VER_NEED_CURRENT;
   7415 	      vn->vn_cnt = caux;
   7416 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7417 					  elf_dt_name (vn->vn_bfd) != NULL
   7418 					  ? elf_dt_name (vn->vn_bfd)
   7419 					  : lbasename (bfd_get_filename
   7420 						       (vn->vn_bfd)),
   7421 					  false);
   7422 	      if (indx == (size_t) -1)
   7423 		return false;
   7424 	      vn->vn_file = indx;
   7425 	      vn->vn_aux = sizeof (Elf_External_Verneed);
   7426 	      if (vn->vn_nextref == NULL)
   7427 		vn->vn_next = 0;
   7428 	      else
   7429 		vn->vn_next = (sizeof (Elf_External_Verneed)
   7430 			       + caux * sizeof (Elf_External_Vernaux));
   7431 
   7432 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
   7433 					 (Elf_External_Verneed *) p);
   7434 	      p += sizeof (Elf_External_Verneed);
   7435 
   7436 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7437 		{
   7438 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
   7439 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7440 					      a->vna_nodename, false);
   7441 		  if (indx == (size_t) -1)
   7442 		    return false;
   7443 		  a->vna_name = indx;
   7444 		  if (a->vna_nextptr == NULL)
   7445 		    a->vna_next = 0;
   7446 		  else
   7447 		    a->vna_next = sizeof (Elf_External_Vernaux);
   7448 
   7449 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
   7450 					     (Elf_External_Vernaux *) p);
   7451 		  p += sizeof (Elf_External_Vernaux);
   7452 		}
   7453 	    }
   7454 
   7455 	  elf_tdata (output_bfd)->cverrefs = crefs;
   7456 	}
   7457     }
   7458 
   7459   if (bfd_link_relocatable (info)
   7460       && !bfd_elf_size_group_sections (info))
   7461     return false;
   7462 
   7463   /* Determine any GNU_STACK segment requirements, after the backend
   7464      has had a chance to set a default segment size.  */
   7465   if (info->execstack)
   7466     {
   7467       /* If the user has explicitly requested warnings, then generate one even
   7468 	 though the choice is the result of another command line option.  */
   7469       if (info->warn_execstack == 1)
   7470 	{
   7471 	  if (info->error_execstack)
   7472 	    {
   7473 	      _bfd_error_handler
   7474 		(_("\
   7475 error: creating an executable stack because of -z execstack command line option"));
   7476 	      return false;
   7477 	    }
   7478 
   7479 	  _bfd_error_handler
   7480 	    (_("\
   7481 warning: enabling an executable stack because of -z execstack command line option"));
   7482 	}
   7483 
   7484       elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   7485     }
   7486   else if (info->noexecstack)
   7487     elf_stack_flags (output_bfd) = PF_R | PF_W;
   7488   else
   7489     {
   7490       bfd *inputobj;
   7491       asection *notesec = NULL;
   7492       bfd *noteobj = NULL;
   7493       bfd *emptyobj = NULL;
   7494       int exec = 0;
   7495 
   7496       for (inputobj = info->input_bfds;
   7497 	   inputobj;
   7498 	   inputobj = inputobj->link.next)
   7499 	{
   7500 	  asection *s;
   7501 
   7502 	  if (inputobj->flags
   7503 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   7504 	    continue;
   7505 	  s = inputobj->sections;
   7506 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7507 	    continue;
   7508 
   7509 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   7510 	  if (s)
   7511 	    {
   7512 	      notesec = s;
   7513 	      if (s->flags & SEC_CODE)
   7514 		{
   7515 		  noteobj = inputobj;
   7516 		  exec = PF_X;
   7517 		  /* There is no point in scanning the remaining bfds.  */
   7518 		  break;
   7519 		}
   7520 	    }
   7521 	  else if (bed->default_execstack && info->default_execstack)
   7522 	    {
   7523 	      exec = PF_X;
   7524 	      emptyobj = inputobj;
   7525 	    }
   7526 	}
   7527 
   7528       if (notesec || info->stacksize > 0)
   7529 	{
   7530 	  if (exec)
   7531 	    {
   7532 	      if (info->warn_execstack != 0)
   7533 		{
   7534 		  /* PR 29072: Because an executable stack is a serious
   7535 		     security risk, make sure that the user knows that it is
   7536 		     being enabled despite the fact that it was not requested
   7537 		     on the command line.  */
   7538 		  if (noteobj)
   7539 		    {
   7540 		      if (info->error_execstack)
   7541 			{
   7542 			  _bfd_error_handler (_("\
   7543 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
   7544 					      bfd_get_filename (noteobj));
   7545 			  return false;
   7546 			}
   7547 
   7548 		      _bfd_error_handler (_("\
   7549 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
   7550 		       bfd_get_filename (noteobj));
   7551 		    }
   7552 		  else if (emptyobj)
   7553 		    {
   7554 		      if (info->error_execstack)
   7555 			{
   7556 			  _bfd_error_handler (_("\
   7557 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
   7558 					      bfd_get_filename (emptyobj));
   7559 			  return false;
   7560 			}
   7561 
   7562 		      _bfd_error_handler (_("\
   7563 warning: %s: missing .note.GNU-stack section implies executable stack"),
   7564 					  bfd_get_filename (emptyobj));
   7565 		      _bfd_error_handler (_("\
   7566 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
   7567 		    }
   7568 		}
   7569 	    }
   7570 	  elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   7571 	}
   7572 
   7573       if (notesec && exec && bfd_link_relocatable (info)
   7574 	  && notesec->output_section != bfd_abs_section_ptr)
   7575 	notesec->output_section->flags |= SEC_CODE;
   7576     }
   7577 
   7578   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7579     {
   7580       struct elf_info_failed eif;
   7581       struct elf_link_hash_entry *h;
   7582       asection *dynstr;
   7583       asection *s;
   7584 
   7585       *sinterpptr = elf_hash_table (info)->interp;
   7586       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   7587 
   7588       if (info->symbolic)
   7589 	{
   7590 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   7591 	    return false;
   7592 	  info->flags |= DF_SYMBOLIC;
   7593 	}
   7594 
   7595       if (rpath != NULL)
   7596 	{
   7597 	  size_t indx;
   7598 	  bfd_vma tag;
   7599 
   7600 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   7601 				      true);
   7602 	  if (indx == (size_t) -1)
   7603 	    return false;
   7604 
   7605 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   7606 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   7607 	    return false;
   7608 	}
   7609 
   7610       if (filter_shlib != NULL)
   7611 	{
   7612 	  size_t indx;
   7613 
   7614 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7615 				      filter_shlib, true);
   7616 	  if (indx == (size_t) -1
   7617 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   7618 	    return false;
   7619 	}
   7620 
   7621       if (auxiliary_filters != NULL)
   7622 	{
   7623 	  const char * const *p;
   7624 
   7625 	  for (p = auxiliary_filters; *p != NULL; p++)
   7626 	    {
   7627 	      size_t indx;
   7628 
   7629 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7630 					  *p, true);
   7631 	      if (indx == (size_t) -1
   7632 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   7633 		return false;
   7634 	    }
   7635 	}
   7636 
   7637       if (audit != NULL)
   7638 	{
   7639 	  size_t indx;
   7640 
   7641 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   7642 				      true);
   7643 	  if (indx == (size_t) -1
   7644 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   7645 	    return false;
   7646 	}
   7647 
   7648       if (depaudit != NULL)
   7649 	{
   7650 	  size_t indx;
   7651 
   7652 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   7653 				      true);
   7654 	  if (indx == (size_t) -1
   7655 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   7656 	    return false;
   7657 	}
   7658 
   7659       eif.info = info;
   7660       eif.failed = false;
   7661 
   7662       /* Find all symbols which were defined in a dynamic object and make
   7663 	 the backend pick a reasonable value for them.  */
   7664       elf_link_hash_traverse (elf_hash_table (info),
   7665 			      _bfd_elf_adjust_dynamic_symbol,
   7666 			      &eif);
   7667       if (eif.failed)
   7668 	return false;
   7669 
   7670       /* Add some entries to the .dynamic section.  We fill in some of the
   7671 	 values later, in bfd_elf_final_link, but we must add the entries
   7672 	 now so that we know the final size of the .dynamic section.  */
   7673 
   7674       /* If there are initialization and/or finalization functions to
   7675 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   7676       h = (info->init_function
   7677 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7678 				   info->init_function, false,
   7679 				   false, false)
   7680 	   : NULL);
   7681       if (h != NULL
   7682 	  && (h->ref_regular
   7683 	      || h->def_regular))
   7684 	{
   7685 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   7686 	    return false;
   7687 	}
   7688       h = (info->fini_function
   7689 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7690 				   info->fini_function, false,
   7691 				   false, false)
   7692 	   : NULL);
   7693       if (h != NULL
   7694 	  && (h->ref_regular
   7695 	      || h->def_regular))
   7696 	{
   7697 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   7698 	    return false;
   7699 	}
   7700 
   7701       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   7702       if (s != NULL && s->linker_has_input)
   7703 	{
   7704 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   7705 	  if (! bfd_link_executable (info))
   7706 	    {
   7707 	      bfd *sub;
   7708 	      asection *o;
   7709 
   7710 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   7711 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   7712 		    && (o = sub->sections) != NULL
   7713 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
   7714 		  for (o = sub->sections; o != NULL; o = o->next)
   7715 		    if (elf_section_data (o)->this_hdr.sh_type
   7716 			== SHT_PREINIT_ARRAY)
   7717 		      {
   7718 			_bfd_error_handler
   7719 			  (_("%pB: .preinit_array section is not allowed in DSO"),
   7720 			   sub);
   7721 			break;
   7722 		      }
   7723 
   7724 	      bfd_set_error (bfd_error_nonrepresentable_section);
   7725 	      return false;
   7726 	    }
   7727 
   7728 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   7729 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   7730 	    return false;
   7731 	}
   7732       s = bfd_get_section_by_name (output_bfd, ".init_array");
   7733       if (s != NULL && s->linker_has_input)
   7734 	{
   7735 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   7736 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   7737 	    return false;
   7738 	}
   7739       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   7740       if (s != NULL && s->linker_has_input)
   7741 	{
   7742 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   7743 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   7744 	    return false;
   7745 	}
   7746 
   7747       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   7748       /* If .dynstr is excluded from the link, we don't want any of
   7749 	 these tags.  Strictly, we should be checking each section
   7750 	 individually;  This quick check covers for the case where
   7751 	 someone does a /DISCARD/ : { *(*) }.  */
   7752       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   7753 	{
   7754 	  bfd_size_type strsize;
   7755 
   7756 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7757 	  if ((info->emit_hash
   7758 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   7759 	      || (info->emit_gnu_hash
   7760 		  && (bed->record_xhash_symbol == NULL
   7761 		      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
   7762 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   7763 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   7764 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   7765 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   7766 					      bed->s->sizeof_sym)
   7767 	      || (info->gnu_flags_1
   7768 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
   7769 						  info->gnu_flags_1)))
   7770 	    return false;
   7771 	}
   7772     }
   7773 
   7774   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   7775     return false;
   7776 
   7777   /* The backend must work out the sizes of all the other dynamic
   7778      sections.  */
   7779   if (bed->elf_backend_late_size_sections != NULL
   7780       && !bed->elf_backend_late_size_sections (output_bfd, info))
   7781     return false;
   7782 
   7783   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7784     {
   7785       if (elf_tdata (output_bfd)->cverdefs)
   7786 	{
   7787 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
   7788 
   7789 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   7790 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
   7791 	    return false;
   7792 	}
   7793 
   7794       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   7795 	{
   7796 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   7797 	    return false;
   7798 	}
   7799       else if (info->flags & DF_BIND_NOW)
   7800 	{
   7801 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   7802 	    return false;
   7803 	}
   7804 
   7805       if (info->flags_1)
   7806 	{
   7807 	  if (bfd_link_executable (info))
   7808 	    info->flags_1 &= ~ (DF_1_INITFIRST
   7809 				| DF_1_NODELETE
   7810 				| DF_1_NOOPEN);
   7811 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   7812 	    return false;
   7813 	}
   7814 
   7815       if (elf_tdata (output_bfd)->cverrefs)
   7816 	{
   7817 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
   7818 
   7819 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   7820 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   7821 	    return false;
   7822 	}
   7823 
   7824       if ((elf_tdata (output_bfd)->cverrefs == 0
   7825 	   && elf_tdata (output_bfd)->cverdefs == 0)
   7826 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
   7827 	{
   7828 	  asection *s;
   7829 
   7830 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   7831 	  s->flags |= SEC_EXCLUDE;
   7832 	}
   7833     }
   7834   return true;
   7835 }
   7836 
   7837 /* Find the first non-excluded output section.  We'll use its
   7838    section symbol for some emitted relocs.  */
   7839 void
   7840 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   7841 {
   7842   asection *s;
   7843   asection *found = NULL;
   7844 
   7845   for (s = output_bfd->sections; s != NULL; s = s->next)
   7846     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7847 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7848       {
   7849 	found = s;
   7850 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7851 	  break;
   7852       }
   7853   elf_hash_table (info)->text_index_section = found;
   7854 }
   7855 
   7856 /* Find two non-excluded output sections, one for code, one for data.
   7857    We'll use their section symbols for some emitted relocs.  */
   7858 void
   7859 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   7860 {
   7861   asection *s;
   7862   asection *found = NULL;
   7863 
   7864   /* Data first, since setting text_index_section changes
   7865      _bfd_elf_omit_section_dynsym_default.  */
   7866   for (s = output_bfd->sections; s != NULL; s = s->next)
   7867     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7868 	&& !(s->flags & SEC_READONLY)
   7869 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7870       {
   7871 	found = s;
   7872 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7873 	  break;
   7874       }
   7875   elf_hash_table (info)->data_index_section = found;
   7876 
   7877   for (s = output_bfd->sections; s != NULL; s = s->next)
   7878     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7879 	&& (s->flags & SEC_READONLY)
   7880 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7881       {
   7882 	found = s;
   7883 	break;
   7884       }
   7885   elf_hash_table (info)->text_index_section = found;
   7886 }
   7887 
   7888 #define GNU_HASH_SECTION_NAME(bed)			    \
   7889   (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
   7890 
   7891 bool
   7892 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   7893 {
   7894   elf_backend_data *bed;
   7895   unsigned long section_sym_count;
   7896   bfd_size_type dynsymcount = 0;
   7897 
   7898   if (!is_elf_hash_table (info->hash))
   7899     return true;
   7900 
   7901   bed = get_elf_backend_data (output_bfd);
   7902   (*bed->elf_backend_init_index_section) (output_bfd, info);
   7903 
   7904   /* Assign dynsym indices.  In a shared library we generate a section
   7905      symbol for each output section, which come first.  Next come all
   7906      of the back-end allocated local dynamic syms, followed by the rest
   7907      of the global symbols.
   7908 
   7909      This is usually not needed for static binaries, however backends
   7910      can request to always do it, e.g. the MIPS backend uses dynamic
   7911      symbol counts to lay out GOT, which will be produced in the
   7912      presence of GOT relocations even in static binaries (holding fixed
   7913      data in that case, to satisfy those relocations).  */
   7914 
   7915   if (elf_hash_table (info)->dynamic_sections_created
   7916       || bed->always_renumber_dynsyms)
   7917     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   7918 						  &section_sym_count);
   7919 
   7920   if (elf_hash_table (info)->dynamic_sections_created)
   7921     {
   7922       bfd *dynobj;
   7923       asection *s;
   7924       unsigned int dtagcount;
   7925 
   7926       dynobj = elf_hash_table (info)->dynobj;
   7927 
   7928       /* Work out the size of the symbol version section.  */
   7929       s = bfd_get_linker_section (dynobj, ".gnu.version");
   7930       BFD_ASSERT (s != NULL);
   7931       if ((s->flags & SEC_EXCLUDE) == 0)
   7932 	{
   7933 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   7934 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7935 	  if (s->contents == NULL)
   7936 	    return false;
   7937 	  s->alloced = 1;
   7938 
   7939 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   7940 	    return false;
   7941 	}
   7942 
   7943       /* Set the size of the .dynsym and .hash sections.  We counted
   7944 	 the number of dynamic symbols in elf_link_add_object_symbols.
   7945 	 We will build the contents of .dynsym and .hash when we build
   7946 	 the final symbol table, because until then we do not know the
   7947 	 correct value to give the symbols.  We built the .dynstr
   7948 	 section as we went along in elf_link_add_object_symbols.  */
   7949       s = elf_hash_table (info)->dynsym;
   7950       BFD_ASSERT (s != NULL);
   7951       s->size = dynsymcount * bed->s->sizeof_sym;
   7952 
   7953       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7954       if (s->contents == NULL)
   7955 	return false;
   7956       s->alloced = 1;
   7957 
   7958       /* The first entry in .dynsym is a dummy symbol.  Clear all the
   7959 	 section syms, in case we don't output them all.  */
   7960       ++section_sym_count;
   7961       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   7962 
   7963       elf_hash_table (info)->bucketcount = 0;
   7964 
   7965       /* Compute the size of the hashing table.  As a side effect this
   7966 	 computes the hash values for all the names we export.  */
   7967       if (info->emit_hash)
   7968 	{
   7969 	  unsigned long int *hashcodes;
   7970 	  struct hash_codes_info hashinf;
   7971 	  bfd_size_type amt;
   7972 	  unsigned long int nsyms;
   7973 	  size_t bucketcount;
   7974 	  size_t hash_entry_size;
   7975 
   7976 	  /* Compute the hash values for all exported symbols.  At the same
   7977 	     time store the values in an array so that we could use them for
   7978 	     optimizations.  */
   7979 	  amt = dynsymcount * sizeof (unsigned long int);
   7980 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   7981 	  if (hashcodes == NULL)
   7982 	    return false;
   7983 	  hashinf.hashcodes = hashcodes;
   7984 	  hashinf.error = false;
   7985 
   7986 	  /* Put all hash values in HASHCODES.  */
   7987 	  elf_link_hash_traverse (elf_hash_table (info),
   7988 				  elf_collect_hash_codes, &hashinf);
   7989 	  if (hashinf.error)
   7990 	    {
   7991 	      free (hashcodes);
   7992 	      return false;
   7993 	    }
   7994 
   7995 	  nsyms = hashinf.hashcodes - hashcodes;
   7996 	  bucketcount
   7997 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   7998 	  free (hashcodes);
   7999 
   8000 	  if (bucketcount == 0 && nsyms > 0)
   8001 	    return false;
   8002 
   8003 	  elf_hash_table (info)->bucketcount = bucketcount;
   8004 
   8005 	  s = bfd_get_linker_section (dynobj, ".hash");
   8006 	  BFD_ASSERT (s != NULL);
   8007 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   8008 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   8009 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   8010 	  if (s->contents == NULL)
   8011 	    return false;
   8012 	  s->alloced = 1;
   8013 
   8014 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   8015 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   8016 		   s->contents + hash_entry_size);
   8017 	}
   8018 
   8019       if (info->emit_gnu_hash)
   8020 	{
   8021 	  size_t i, cnt;
   8022 	  unsigned char *contents;
   8023 	  struct collect_gnu_hash_codes cinfo;
   8024 	  bfd_size_type amt;
   8025 	  size_t bucketcount;
   8026 
   8027 	  memset (&cinfo, 0, sizeof (cinfo));
   8028 
   8029 	  /* Compute the hash values for all exported symbols.  At the same
   8030 	     time store the values in an array so that we could use them for
   8031 	     optimizations.  */
   8032 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   8033 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   8034 	  if (cinfo.hashcodes == NULL)
   8035 	    return false;
   8036 
   8037 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   8038 	  cinfo.min_dynindx = -1;
   8039 	  cinfo.output_bfd = output_bfd;
   8040 	  cinfo.bed = bed;
   8041 
   8042 	  /* Put all hash values in HASHCODES.  */
   8043 	  elf_link_hash_traverse (elf_hash_table (info),
   8044 				  elf_collect_gnu_hash_codes, &cinfo);
   8045 	  if (cinfo.error)
   8046 	    {
   8047 	      free (cinfo.hashcodes);
   8048 	      return false;
   8049 	    }
   8050 
   8051 	  bucketcount
   8052 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   8053 
   8054 	  if (bucketcount == 0)
   8055 	    {
   8056 	      free (cinfo.hashcodes);
   8057 	      return false;
   8058 	    }
   8059 
   8060 	  s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
   8061 	  BFD_ASSERT (s != NULL);
   8062 
   8063 	  if (cinfo.nsyms == 0)
   8064 	    {
   8065 	      /* Empty .gnu.hash or .MIPS.xhash section is special.  */
   8066 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   8067 	      free (cinfo.hashcodes);
   8068 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   8069 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   8070 	      if (contents == NULL)
   8071 		return false;
   8072 	      s->contents = contents;
   8073 	      s->alloced = 1;
   8074 	      /* 1 empty bucket.  */
   8075 	      bfd_put_32 (output_bfd, 1, contents);
   8076 	      /* SYMIDX above the special symbol 0.  */
   8077 	      bfd_put_32 (output_bfd, 1, contents + 4);
   8078 	      /* Just one word for bitmask.  */
   8079 	      bfd_put_32 (output_bfd, 1, contents + 8);
   8080 	      /* Only hash fn bloom filter.  */
   8081 	      bfd_put_32 (output_bfd, 0, contents + 12);
   8082 	      /* No hashes are valid - empty bitmask.  */
   8083 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   8084 	      /* No hashes in the only bucket.  */
   8085 	      bfd_put_32 (output_bfd, 0,
   8086 			  contents + 16 + bed->s->arch_size / 8);
   8087 	    }
   8088 	  else
   8089 	    {
   8090 	      unsigned long int maskwords, maskbitslog2, x;
   8091 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   8092 
   8093 	      x = cinfo.nsyms;
   8094 	      maskbitslog2 = 1;
   8095 	      while ((x >>= 1) != 0)
   8096 		++maskbitslog2;
   8097 	      if (maskbitslog2 < 3)
   8098 		maskbitslog2 = 5;
   8099 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   8100 		maskbitslog2 = maskbitslog2 + 3;
   8101 	      else
   8102 		maskbitslog2 = maskbitslog2 + 2;
   8103 	      if (bed->s->arch_size == 64)
   8104 		{
   8105 		  if (maskbitslog2 == 5)
   8106 		    maskbitslog2 = 6;
   8107 		  cinfo.shift1 = 6;
   8108 		}
   8109 	      else
   8110 		cinfo.shift1 = 5;
   8111 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   8112 	      cinfo.shift2 = maskbitslog2;
   8113 	      cinfo.maskbits = 1 << maskbitslog2;
   8114 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   8115 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   8116 	      amt += maskwords * sizeof (bfd_vma);
   8117 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   8118 	      if (cinfo.bitmask == NULL)
   8119 		{
   8120 		  free (cinfo.hashcodes);
   8121 		  return false;
   8122 		}
   8123 
   8124 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   8125 	      cinfo.indx = cinfo.counts + bucketcount;
   8126 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   8127 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   8128 
   8129 	      /* Determine how often each hash bucket is used.  */
   8130 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   8131 	      for (i = 0; i < cinfo.nsyms; ++i)
   8132 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   8133 
   8134 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   8135 		if (cinfo.counts[i] != 0)
   8136 		  {
   8137 		    cinfo.indx[i] = cnt;
   8138 		    cnt += cinfo.counts[i];
   8139 		  }
   8140 	      BFD_ASSERT (cnt == dynsymcount);
   8141 	      cinfo.bucketcount = bucketcount;
   8142 	      cinfo.local_indx = cinfo.min_dynindx;
   8143 
   8144 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   8145 	      s->size += cinfo.maskbits / 8;
   8146 	      if (bed->record_xhash_symbol != NULL)
   8147 		s->size += cinfo.nsyms * 4;
   8148 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   8149 	      if (contents == NULL)
   8150 		{
   8151 		  free (cinfo.bitmask);
   8152 		  free (cinfo.hashcodes);
   8153 		  return false;
   8154 		}
   8155 
   8156 	      s->contents = contents;
   8157 	      s->alloced = 1;
   8158 	      bfd_put_32 (output_bfd, bucketcount, contents);
   8159 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   8160 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   8161 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   8162 	      contents += 16 + cinfo.maskbits / 8;
   8163 
   8164 	      for (i = 0; i < bucketcount; ++i)
   8165 		{
   8166 		  if (cinfo.counts[i] == 0)
   8167 		    bfd_put_32 (output_bfd, 0, contents);
   8168 		  else
   8169 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   8170 		  contents += 4;
   8171 		}
   8172 
   8173 	      cinfo.contents = contents;
   8174 
   8175 	      cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
   8176 
   8177 	      if (elf_hash_table (info)->has_base_symbols)
   8178 		{
   8179 		  /* Output base symbols first in DT_GNU_HASH so that
   8180 		     they will be picked before non-base symbols at
   8181 		     run-time.  */
   8182 		  cinfo.base_symbol = true;
   8183 
   8184 		  /* Renumber dynamic symbols, if populating .gnu.hash
   8185 		     section.  If using .MIPS.xhash, populate the
   8186 		     translation table.  */
   8187 		  elf_link_hash_traverse (elf_hash_table (info),
   8188 					  elf_gnu_hash_process_symidx, &cinfo);
   8189 		}
   8190 
   8191 	      /* Output non-base symbols last.  */
   8192 	      cinfo.base_symbol = false;
   8193 	      elf_link_hash_traverse (elf_hash_table (info),
   8194 				      elf_gnu_hash_process_symidx, &cinfo);
   8195 
   8196 	      contents = s->contents + 16;
   8197 	      for (i = 0; i < maskwords; ++i)
   8198 		{
   8199 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   8200 			   contents);
   8201 		  contents += bed->s->arch_size / 8;
   8202 		}
   8203 
   8204 	      free (cinfo.bitmask);
   8205 	      free (cinfo.hashcodes);
   8206 	    }
   8207 	}
   8208 
   8209       s = bfd_get_linker_section (dynobj, ".dynstr");
   8210       BFD_ASSERT (s != NULL);
   8211 
   8212       elf_finalize_dynstr (output_bfd, info);
   8213 
   8214       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   8215 
   8216       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   8217 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   8218 	  return false;
   8219     }
   8220 
   8221   return true;
   8222 }
   8223 
   8224 /* Create an entry in an ELF linker hash table.  */
   8226 
   8227 struct bfd_hash_entry *
   8228 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   8229 			    struct bfd_hash_table *table,
   8230 			    const char *string)
   8231 {
   8232   /* Allocate the structure if it has not already been allocated by a
   8233      subclass.  */
   8234   if (entry == NULL)
   8235     {
   8236       entry = (struct bfd_hash_entry *)
   8237 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   8238       if (entry == NULL)
   8239 	return entry;
   8240     }
   8241 
   8242   /* Call the allocation method of the superclass.  */
   8243   entry = _bfd_link_hash_newfunc (entry, table, string);
   8244   if (entry != NULL)
   8245     {
   8246       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   8247       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   8248 
   8249       /* Set local fields.  */
   8250       ret->indx = -1;
   8251       ret->dynindx = -1;
   8252       ret->got = htab->init_got_refcount;
   8253       ret->plt = htab->init_plt_refcount;
   8254       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   8255 			      - offsetof (struct elf_link_hash_entry, size)));
   8256       /* Assume that we have been called by a non-ELF symbol reader.
   8257 	 This flag is then reset by the code which reads an ELF input
   8258 	 file.  This ensures that a symbol created by a non-ELF symbol
   8259 	 reader will have the flag set correctly.  */
   8260       ret->non_elf = 1;
   8261     }
   8262 
   8263   return entry;
   8264 }
   8265 
   8266 /* Copy data from an indirect symbol to its direct symbol, hiding the
   8267    old indirect symbol.  Also used for copying flags to a weakdef.  */
   8268 
   8269 void
   8270 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   8271 				  struct elf_link_hash_entry *dir,
   8272 				  struct elf_link_hash_entry *ind)
   8273 {
   8274   struct elf_link_hash_table *htab;
   8275 
   8276   if (ind->dyn_relocs != NULL)
   8277     {
   8278       if (dir->dyn_relocs != NULL)
   8279 	{
   8280 	  struct elf_dyn_relocs **pp;
   8281 	  struct elf_dyn_relocs *p;
   8282 
   8283 	  /* Add reloc counts against the indirect sym to the direct sym
   8284 	     list.  Merge any entries against the same section.  */
   8285 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   8286 	    {
   8287 	      struct elf_dyn_relocs *q;
   8288 
   8289 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   8290 		if (q->sec == p->sec)
   8291 		  {
   8292 		    q->pc_count += p->pc_count;
   8293 		    q->count += p->count;
   8294 		    *pp = p->next;
   8295 		    break;
   8296 		  }
   8297 	      if (q == NULL)
   8298 		pp = &p->next;
   8299 	    }
   8300 	  *pp = dir->dyn_relocs;
   8301 	}
   8302 
   8303       dir->dyn_relocs = ind->dyn_relocs;
   8304       ind->dyn_relocs = NULL;
   8305     }
   8306 
   8307   /* Copy down any references that we may have already seen to the
   8308      symbol which just became indirect.  */
   8309 
   8310   if (dir->versioned != versioned_hidden)
   8311     dir->ref_dynamic |= ind->ref_dynamic;
   8312   dir->ref_regular |= ind->ref_regular;
   8313   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   8314   dir->non_got_ref |= ind->non_got_ref;
   8315   dir->needs_plt |= ind->needs_plt;
   8316   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   8317 
   8318   if (ind->root.type != bfd_link_hash_indirect)
   8319     return;
   8320 
   8321   /* Copy over the global and procedure linkage table refcount entries.
   8322      These may have been already set up by a check_relocs routine.  */
   8323   htab = elf_hash_table (info);
   8324   if (ind->got.refcount > htab->init_got_refcount.refcount)
   8325     {
   8326       if (dir->got.refcount < 0)
   8327 	dir->got.refcount = 0;
   8328       dir->got.refcount += ind->got.refcount;
   8329       ind->got.refcount = htab->init_got_refcount.refcount;
   8330     }
   8331 
   8332   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   8333     {
   8334       if (dir->plt.refcount < 0)
   8335 	dir->plt.refcount = 0;
   8336       dir->plt.refcount += ind->plt.refcount;
   8337       ind->plt.refcount = htab->init_plt_refcount.refcount;
   8338     }
   8339 
   8340   if (ind->dynindx != -1)
   8341     {
   8342       if (dir->dynindx != -1)
   8343 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   8344       dir->dynindx = ind->dynindx;
   8345       dir->dynstr_index = ind->dynstr_index;
   8346       ind->dynindx = -1;
   8347       ind->dynstr_index = 0;
   8348     }
   8349 }
   8350 
   8351 void
   8352 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   8353 				struct elf_link_hash_entry *h,
   8354 				bool force_local)
   8355 {
   8356   /* STT_GNU_IFUNC symbol must go through PLT.  */
   8357   if (h->type != STT_GNU_IFUNC)
   8358     {
   8359       h->plt = elf_hash_table (info)->init_plt_offset;
   8360       h->needs_plt = 0;
   8361     }
   8362   if (force_local)
   8363     {
   8364       h->forced_local = 1;
   8365       if (h->dynindx != -1)
   8366 	{
   8367 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8368 				  h->dynstr_index);
   8369 	  h->dynindx = -1;
   8370 	  h->dynstr_index = 0;
   8371 	}
   8372     }
   8373 }
   8374 
   8375 /* Hide a symbol. */
   8376 
   8377 void
   8378 _bfd_elf_link_hide_symbol (bfd *output_bfd,
   8379 			   struct bfd_link_info *info,
   8380 			   struct bfd_link_hash_entry *h)
   8381 {
   8382   if (is_elf_hash_table (info->hash))
   8383     {
   8384       elf_backend_data *bed = get_elf_backend_data (output_bfd);
   8385       struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
   8386       eh->def_dynamic = 0;
   8387       eh->ref_dynamic = 0;
   8388       eh->dynamic_def = 0;
   8389       bed->elf_backend_hide_symbol (info, eh, true);
   8390     }
   8391 }
   8392 
   8393 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   8394    caller.  */
   8395 
   8396 bool
   8397 _bfd_elf_link_hash_table_init
   8398   (struct elf_link_hash_table *table,
   8399    bfd *abfd,
   8400    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   8401 				      struct bfd_hash_table *,
   8402 				      const char *),
   8403    unsigned int entsize)
   8404 {
   8405   bool ret;
   8406   elf_backend_data *bed = get_elf_backend_data (abfd);
   8407   int can_refcount = bed->can_refcount;
   8408 
   8409   table->init_got_refcount.refcount = can_refcount - 1;
   8410   table->init_plt_refcount.refcount = can_refcount - 1;
   8411   table->init_got_offset.offset = -(bfd_vma) 1;
   8412   table->init_plt_offset.offset = -(bfd_vma) 1;
   8413   /* The first dynamic symbol is a dummy.  */
   8414   table->dynsymcount = 1;
   8415 
   8416   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   8417 
   8418   table->root.type = bfd_link_elf_hash_table;
   8419   table->hash_table_id = bed->target_id;
   8420   table->target_os = bed->target_os;
   8421   table->root.hash_table_free = _bfd_elf_link_hash_table_free;
   8422 
   8423   return ret;
   8424 }
   8425 
   8426 /* Create an ELF linker hash table.  */
   8427 
   8428 struct bfd_link_hash_table *
   8429 _bfd_elf_link_hash_table_create (bfd *abfd)
   8430 {
   8431   struct elf_link_hash_table *ret;
   8432   size_t amt = sizeof (struct elf_link_hash_table);
   8433 
   8434   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   8435   if (ret == NULL)
   8436     return NULL;
   8437 
   8438   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   8439 				       sizeof (struct elf_link_hash_entry)))
   8440     {
   8441       free (ret);
   8442       return NULL;
   8443     }
   8444 
   8445   return &ret->root;
   8446 }
   8447 
   8448 /* Destroy an ELF linker hash table.  */
   8449 
   8450 void
   8451 _bfd_elf_link_hash_table_free (bfd *obfd)
   8452 {
   8453   struct elf_link_hash_table *htab;
   8454 
   8455   htab = (struct elf_link_hash_table *) obfd->link.hash;
   8456   if (htab->dynstr != NULL)
   8457     _bfd_elf_strtab_free (htab->dynstr);
   8458   /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
   8459   if (htab->dynamic != NULL)
   8460     {
   8461       free (htab->dynamic->contents);
   8462       htab->dynamic->contents = NULL;
   8463     }
   8464   if (htab->first_hash != NULL)
   8465     {
   8466       bfd_hash_table_free (htab->first_hash);
   8467       free (htab->first_hash);
   8468     }
   8469   if (htab->eh_info.frame_hdr_is_compact)
   8470     free (htab->eh_info.u.compact.entries);
   8471   else
   8472     free (htab->eh_info.u.dwarf.array);
   8473   _bfd_generic_link_hash_table_free (obfd);
   8474 }
   8475 
   8476 /* This is a hook for the ELF emulation code in the generic linker to
   8477    tell the backend linker what file name to use for the DT_NEEDED
   8478    entry for a dynamic object.  */
   8479 
   8480 void
   8481 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   8482 {
   8483   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8484       && bfd_get_format (abfd) == bfd_object)
   8485     elf_dt_name (abfd) = name;
   8486 }
   8487 
   8488 int
   8489 bfd_elf_get_dyn_lib_class (bfd *abfd)
   8490 {
   8491   int lib_class;
   8492   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8493       && bfd_get_format (abfd) == bfd_object)
   8494     lib_class = elf_dyn_lib_class (abfd);
   8495   else
   8496     lib_class = 0;
   8497   return lib_class;
   8498 }
   8499 
   8500 void
   8501 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   8502 {
   8503   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8504       && bfd_get_format (abfd) == bfd_object)
   8505     elf_dyn_lib_class (abfd) = lib_class;
   8506 }
   8507 
   8508 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   8509    the linker ELF emulation code.  */
   8510 
   8511 struct bfd_link_needed_list *
   8512 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   8513 			 struct bfd_link_info *info)
   8514 {
   8515   if (! is_elf_hash_table (info->hash))
   8516     return NULL;
   8517   return elf_hash_table (info)->needed;
   8518 }
   8519 
   8520 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   8521    hook for the linker ELF emulation code.  */
   8522 
   8523 struct bfd_link_needed_list *
   8524 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   8525 			  struct bfd_link_info *info)
   8526 {
   8527   if (! is_elf_hash_table (info->hash))
   8528     return NULL;
   8529   return elf_hash_table (info)->runpath;
   8530 }
   8531 
   8532 /* Get the name actually used for a dynamic object for a link.  This
   8533    is the SONAME entry if there is one.  Otherwise, it is the string
   8534    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   8535 
   8536 const char *
   8537 bfd_elf_get_dt_soname (bfd *abfd)
   8538 {
   8539   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8540       && bfd_get_format (abfd) == bfd_object)
   8541     return elf_dt_name (abfd);
   8542   return NULL;
   8543 }
   8544 
   8545 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   8546    the ELF linker emulation code.  */
   8547 
   8548 bool
   8549 bfd_elf_get_bfd_needed_list (bfd *abfd,
   8550 			     struct bfd_link_needed_list **pneeded)
   8551 {
   8552   asection *s;
   8553   bfd_byte *dynbuf = NULL;
   8554   unsigned int elfsec;
   8555   unsigned long shlink;
   8556   bfd_byte *extdyn, *extdynend;
   8557   size_t extdynsize;
   8558   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   8559 
   8560   *pneeded = NULL;
   8561 
   8562   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   8563       || bfd_get_format (abfd) != bfd_object)
   8564     return true;
   8565 
   8566   s = bfd_get_section_by_name (abfd, ".dynamic");
   8567   if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
   8568     return true;
   8569 
   8570   if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   8571     goto error_return;
   8572 
   8573   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   8574   if (elfsec == SHN_BAD)
   8575     goto error_return;
   8576 
   8577   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   8578 
   8579   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   8580   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   8581 
   8582   for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   8583        (size_t) (extdynend - extdyn) >= extdynsize;
   8584        extdyn += extdynsize)
   8585     {
   8586       Elf_Internal_Dyn dyn;
   8587 
   8588       (*swap_dyn_in) (abfd, extdyn, &dyn);
   8589 
   8590       if (dyn.d_tag == DT_NULL)
   8591 	break;
   8592 
   8593       if (dyn.d_tag == DT_NEEDED)
   8594 	{
   8595 	  const char *string;
   8596 	  struct bfd_link_needed_list *l;
   8597 	  unsigned int tagv = dyn.d_un.d_val;
   8598 	  size_t amt;
   8599 
   8600 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   8601 	  if (string == NULL)
   8602 	    goto error_return;
   8603 
   8604 	  amt = sizeof *l;
   8605 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   8606 	  if (l == NULL)
   8607 	    goto error_return;
   8608 
   8609 	  l->by = abfd;
   8610 	  l->name = string;
   8611 	  l->next = *pneeded;
   8612 	  *pneeded = l;
   8613 	}
   8614     }
   8615 
   8616   _bfd_elf_munmap_section_contents (s, dynbuf);
   8617 
   8618   return true;
   8619 
   8620  error_return:
   8621   _bfd_elf_munmap_section_contents (s, dynbuf);
   8622   return false;
   8623 }
   8624 
   8625 struct elf_symbuf_symbol
   8626 {
   8627   unsigned long st_name;	/* Symbol name, index in string tbl */
   8628   unsigned char st_info;	/* Type and binding attributes */
   8629   unsigned char st_other;	/* Visibilty, and target specific */
   8630 };
   8631 
   8632 struct elf_symbuf_head
   8633 {
   8634   struct elf_symbuf_symbol *ssym;
   8635   size_t count;
   8636   unsigned int st_shndx;
   8637 };
   8638 
   8639 struct elf_symbol
   8640 {
   8641   union
   8642     {
   8643       Elf_Internal_Sym *isym;
   8644       struct elf_symbuf_symbol *ssym;
   8645       void *p;
   8646     } u;
   8647   const char *name;
   8648 };
   8649 
   8650 /* Sort references to symbols by ascending section number.  */
   8651 
   8652 static int
   8653 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   8654 {
   8655   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   8656   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   8657 
   8658   if (s1->st_shndx != s2->st_shndx)
   8659     return s1->st_shndx > s2->st_shndx ? 1 : -1;
   8660   /* Final sort by the address of the sym in the symbuf ensures
   8661      a stable sort.  */
   8662   if (s1 != s2)
   8663     return s1 > s2 ? 1 : -1;
   8664   return 0;
   8665 }
   8666 
   8667 static int
   8668 elf_sym_name_compare (const void *arg1, const void *arg2)
   8669 {
   8670   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   8671   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   8672   int ret = strcmp (s1->name, s2->name);
   8673   if (ret != 0)
   8674     return ret;
   8675   if (s1->u.p != s2->u.p)
   8676     return s1->u.p > s2->u.p ? 1 : -1;
   8677   return 0;
   8678 }
   8679 
   8680 static struct elf_symbuf_head *
   8681 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
   8682 {
   8683   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   8684   struct elf_symbuf_symbol *ssym;
   8685   struct elf_symbuf_head *ssymbuf, *ssymhead;
   8686   size_t i, shndx_count, total_size, amt;
   8687 
   8688   amt = symcount * sizeof (*indbuf);
   8689   indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
   8690   if (indbuf == NULL)
   8691     return NULL;
   8692 
   8693   for (ind = indbuf, i = 0; i < symcount; i++)
   8694     if (isymbuf[i].st_shndx != SHN_UNDEF)
   8695       *ind++ = &isymbuf[i];
   8696   indbufend = ind;
   8697 
   8698   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   8699 	 elf_sort_elf_symbol);
   8700 
   8701   shndx_count = 0;
   8702   if (indbufend > indbuf)
   8703     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   8704       if (ind[0]->st_shndx != ind[1]->st_shndx)
   8705 	shndx_count++;
   8706 
   8707   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   8708 		+ (indbufend - indbuf) * sizeof (*ssym));
   8709   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   8710   if (ssymbuf == NULL)
   8711     {
   8712       free (indbuf);
   8713       return NULL;
   8714     }
   8715 
   8716   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   8717   ssymbuf->ssym = NULL;
   8718   ssymbuf->count = shndx_count;
   8719   ssymbuf->st_shndx = 0;
   8720   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   8721     {
   8722       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   8723 	{
   8724 	  ssymhead++;
   8725 	  ssymhead->ssym = ssym;
   8726 	  ssymhead->count = 0;
   8727 	  ssymhead->st_shndx = (*ind)->st_shndx;
   8728 	}
   8729       ssym->st_name = (*ind)->st_name;
   8730       ssym->st_info = (*ind)->st_info;
   8731       ssym->st_other = (*ind)->st_other;
   8732       ssymhead->count++;
   8733     }
   8734   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
   8735 	      && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
   8736 
   8737   free (indbuf);
   8738   return ssymbuf;
   8739 }
   8740 
   8741 /* Check if 2 sections define the same set of local and global
   8742    symbols.  */
   8743 
   8744 static bool
   8745 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   8746 				   struct bfd_link_info *info)
   8747 {
   8748   bfd *bfd1, *bfd2;
   8749   elf_backend_data *bed1, *bed2;
   8750   Elf_Internal_Shdr *hdr1, *hdr2;
   8751   size_t symcount1, symcount2;
   8752   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   8753   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   8754   Elf_Internal_Sym *isym, *isymend;
   8755   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   8756   size_t count1, count2, sec_count1, sec_count2, i;
   8757   unsigned int shndx1, shndx2;
   8758   bool result;
   8759   bool ignore_section_symbol_p;
   8760 
   8761   bfd1 = sec1->owner;
   8762   bfd2 = sec2->owner;
   8763 
   8764   /* Both sections have to be in ELF.  */
   8765   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   8766       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   8767     return false;
   8768 
   8769   if (elf_section_type (sec1) != elf_section_type (sec2))
   8770     return false;
   8771 
   8772   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   8773   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   8774   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   8775     return false;
   8776 
   8777   bed1 = get_elf_backend_data (bfd1);
   8778   bed2 = get_elf_backend_data (bfd2);
   8779   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   8780   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   8781   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   8782   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   8783 
   8784   if (symcount1 == 0 || symcount2 == 0)
   8785     return false;
   8786 
   8787   result = false;
   8788   isymbuf1 = NULL;
   8789   isymbuf2 = NULL;
   8790   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   8791   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   8792 
   8793   /* Ignore section symbols only when matching non-debugging sections
   8794      or linkonce section with comdat section.  */
   8795   ignore_section_symbol_p
   8796     = ((sec1->flags & SEC_DEBUGGING) == 0
   8797        || ((elf_section_flags (sec1) & SHF_GROUP)
   8798 	   != (elf_section_flags (sec2) & SHF_GROUP)));
   8799 
   8800   if (ssymbuf1 == NULL)
   8801     {
   8802       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   8803 				       NULL, NULL, NULL);
   8804       if (isymbuf1 == NULL)
   8805 	goto done;
   8806 
   8807       if (info != NULL && !info->reduce_memory_overheads)
   8808 	{
   8809 	  ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
   8810 	  elf_tdata (bfd1)->symbuf = ssymbuf1;
   8811 	}
   8812     }
   8813 
   8814   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   8815     {
   8816       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   8817 				       NULL, NULL, NULL);
   8818       if (isymbuf2 == NULL)
   8819 	goto done;
   8820 
   8821       if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
   8822 	{
   8823 	  ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
   8824 	  elf_tdata (bfd2)->symbuf = ssymbuf2;
   8825 	}
   8826     }
   8827 
   8828   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   8829     {
   8830       /* Optimized faster version.  */
   8831       size_t lo, hi, mid;
   8832       struct elf_symbol *symp;
   8833       struct elf_symbuf_symbol *ssym, *ssymend;
   8834 
   8835       lo = 0;
   8836       hi = ssymbuf1->count;
   8837       ssymbuf1++;
   8838       count1 = 0;
   8839       sec_count1 = 0;
   8840       while (lo < hi)
   8841 	{
   8842 	  mid = (lo + hi) / 2;
   8843 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   8844 	    hi = mid;
   8845 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   8846 	    lo = mid + 1;
   8847 	  else
   8848 	    {
   8849 	      count1 = ssymbuf1[mid].count;
   8850 	      ssymbuf1 += mid;
   8851 	      break;
   8852 	    }
   8853 	}
   8854       if (ignore_section_symbol_p)
   8855 	{
   8856 	  for (i = 0; i < count1; i++)
   8857 	    if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
   8858 	      sec_count1++;
   8859 	  count1 -= sec_count1;
   8860 	}
   8861 
   8862       lo = 0;
   8863       hi = ssymbuf2->count;
   8864       ssymbuf2++;
   8865       count2 = 0;
   8866       sec_count2 = 0;
   8867       while (lo < hi)
   8868 	{
   8869 	  mid = (lo + hi) / 2;
   8870 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   8871 	    hi = mid;
   8872 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   8873 	    lo = mid + 1;
   8874 	  else
   8875 	    {
   8876 	      count2 = ssymbuf2[mid].count;
   8877 	      ssymbuf2 += mid;
   8878 	      break;
   8879 	    }
   8880 	}
   8881       if (ignore_section_symbol_p)
   8882 	{
   8883 	  for (i = 0; i < count2; i++)
   8884 	    if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
   8885 	      sec_count2++;
   8886 	  count2 -= sec_count2;
   8887 	}
   8888 
   8889       if (count1 == 0 || count2 == 0 || count1 != count2)
   8890 	goto done;
   8891 
   8892       symtable1
   8893 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   8894       symtable2
   8895 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   8896       if (symtable1 == NULL || symtable2 == NULL)
   8897 	goto done;
   8898 
   8899       symp = symtable1;
   8900       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
   8901 	   ssym < ssymend; ssym++)
   8902 	if (sec_count1 == 0
   8903 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8904 	  {
   8905 	    symp->u.ssym = ssym;
   8906 	    symp->name = bfd_elf_string_from_elf_section (bfd1,
   8907 							  hdr1->sh_link,
   8908 							  ssym->st_name);
   8909 	    if (symp->name == NULL)
   8910 	      goto done;
   8911 	    symp++;
   8912 	  }
   8913 
   8914       symp = symtable2;
   8915       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
   8916 	   ssym < ssymend; ssym++)
   8917 	if (sec_count2 == 0
   8918 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8919 	  {
   8920 	    symp->u.ssym = ssym;
   8921 	    symp->name = bfd_elf_string_from_elf_section (bfd2,
   8922 							  hdr2->sh_link,
   8923 							  ssym->st_name);
   8924 	    if (symp->name == NULL)
   8925 	      goto done;
   8926 	    symp++;
   8927 	  }
   8928 
   8929       /* Sort symbol by name.  */
   8930       qsort (symtable1, count1, sizeof (struct elf_symbol),
   8931 	     elf_sym_name_compare);
   8932       qsort (symtable2, count1, sizeof (struct elf_symbol),
   8933 	     elf_sym_name_compare);
   8934 
   8935       for (i = 0; i < count1; i++)
   8936 	/* Two symbols must have the same binding, type and name.  */
   8937 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   8938 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   8939 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8940 	  goto done;
   8941 
   8942       result = true;
   8943       goto done;
   8944     }
   8945 
   8946   symtable1 = (struct elf_symbol *)
   8947       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   8948   symtable2 = (struct elf_symbol *)
   8949       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   8950   if (symtable1 == NULL || symtable2 == NULL)
   8951     goto done;
   8952 
   8953   /* Count definitions in the section.  */
   8954   count1 = 0;
   8955   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   8956     if (isym->st_shndx == shndx1
   8957 	&& (!ignore_section_symbol_p
   8958 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8959       symtable1[count1++].u.isym = isym;
   8960 
   8961   count2 = 0;
   8962   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   8963     if (isym->st_shndx == shndx2
   8964 	&& (!ignore_section_symbol_p
   8965 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8966       symtable2[count2++].u.isym = isym;
   8967 
   8968   if (count1 == 0 || count2 == 0 || count1 != count2)
   8969     goto done;
   8970 
   8971   for (i = 0; i < count1; i++)
   8972     {
   8973       symtable1[i].name
   8974 	= bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   8975 					   symtable1[i].u.isym->st_name);
   8976       if (symtable1[i].name == NULL)
   8977 	goto done;
   8978     }
   8979 
   8980   for (i = 0; i < count2; i++)
   8981     {
   8982       symtable2[i].name
   8983 	= bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   8984 					   symtable2[i].u.isym->st_name);
   8985       if (symtable2[i].name == NULL)
   8986 	goto done;
   8987     }
   8988 
   8989   /* Sort symbol by name.  */
   8990   qsort (symtable1, count1, sizeof (struct elf_symbol),
   8991 	 elf_sym_name_compare);
   8992   qsort (symtable2, count1, sizeof (struct elf_symbol),
   8993 	 elf_sym_name_compare);
   8994 
   8995   for (i = 0; i < count1; i++)
   8996     /* Two symbols must have the same binding, type and name.  */
   8997     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   8998 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   8999 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   9000       goto done;
   9001 
   9002   result = true;
   9003 
   9004  done:
   9005   free (symtable1);
   9006   free (symtable2);
   9007   free (isymbuf1);
   9008   free (isymbuf2);
   9009 
   9010   return result;
   9011 }
   9012 
   9013 /* Return TRUE if 2 section types are compatible.  */
   9014 
   9015 bool
   9016 bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   9017 				bfd *bbfd, const asection *bsec)
   9018 {
   9019   if (asec == NULL
   9020       || bsec == NULL
   9021       || abfd->xvec->flavour != bfd_target_elf_flavour
   9022       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   9023     return true;
   9024 
   9025   return elf_section_type (asec) == elf_section_type (bsec);
   9026 }
   9027 
   9028 /* Final phase of ELF linker.  */
   9030 
   9031 /* A structure we use to avoid passing large numbers of arguments.  */
   9032 
   9033 struct elf_final_link_info
   9034 {
   9035   /* General link information.  */
   9036   struct bfd_link_info *info;
   9037   /* Output BFD.  */
   9038   bfd *output_bfd;
   9039   /* Symbol string table.  */
   9040   struct elf_strtab_hash *symstrtab;
   9041   /* .hash section.  */
   9042   asection *hash_sec;
   9043   /* symbol version section (.gnu.version).  */
   9044   asection *symver_sec;
   9045   /* Buffer large enough to hold contents of any section.  */
   9046   bfd_byte *contents;
   9047   /* Buffer large enough to hold external relocs of any section.  */
   9048   void *external_relocs;
   9049   /* Buffer large enough to hold internal relocs of any section.  */
   9050   Elf_Internal_Rela *internal_relocs;
   9051   /* Buffer large enough to hold external local symbols of any input
   9052      BFD.  */
   9053   bfd_byte *external_syms;
   9054   /* And a buffer for symbol section indices.  */
   9055   Elf_External_Sym_Shndx *locsym_shndx;
   9056   /* Buffer large enough to hold internal local symbols of any input
   9057      BFD.  */
   9058   Elf_Internal_Sym *internal_syms;
   9059   /* Array large enough to hold a symbol index for each local symbol
   9060      of any input BFD.  */
   9061   long *indices;
   9062   /* Array large enough to hold a section pointer for each local
   9063      symbol of any input BFD.  */
   9064   asection **sections;
   9065   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   9066   Elf_External_Sym_Shndx *symshndxbuf;
   9067   /* Number of STT_FILE syms seen.  */
   9068   size_t filesym_count;
   9069   /* Local symbol hash table.  */
   9070   struct bfd_hash_table local_hash_table;
   9071 };
   9072 
   9073 struct local_hash_entry
   9074 {
   9075   /* Base hash table entry structure.  */
   9076   struct bfd_hash_entry root;
   9077   /* Size of the local symbol name.  */
   9078   size_t size;
   9079   /* Number of the duplicated local symbol names.  */
   9080   long count;
   9081 };
   9082 
   9083 /* Create an entry in the local symbol hash table.  */
   9084 
   9085 static struct bfd_hash_entry *
   9086 local_hash_newfunc (struct bfd_hash_entry *entry,
   9087 		    struct bfd_hash_table *table,
   9088 		    const char *string)
   9089 {
   9090 
   9091   /* Allocate the structure if it has not already been allocated by a
   9092      subclass.  */
   9093   if (entry == NULL)
   9094     {
   9095       entry = bfd_hash_allocate (table,
   9096 				 sizeof (struct local_hash_entry));
   9097       if (entry == NULL)
   9098         return entry;
   9099     }
   9100 
   9101   /* Call the allocation method of the superclass.  */
   9102   entry = bfd_hash_newfunc (entry, table, string);
   9103   if (entry != NULL)
   9104     {
   9105       ((struct local_hash_entry *) entry)->count = 0;
   9106       ((struct local_hash_entry *) entry)->size = 0;
   9107     }
   9108 
   9109   return entry;
   9110 }
   9111 
   9112 /* This struct is used to pass information to elf_link_output_extsym.  */
   9113 
   9114 struct elf_outext_info
   9115 {
   9116   bool failed;
   9117   bool localsyms;
   9118   bool file_sym_done;
   9119   bool base_symbol;
   9120   struct elf_final_link_info *flinfo;
   9121 };
   9122 
   9123 
   9124 /* Support for evaluating a complex relocation.
   9125 
   9126    Complex relocations are generalized, self-describing relocations.  The
   9127    implementation of them consists of two parts: complex symbols, and the
   9128    relocations themselves.
   9129 
   9130    The relocations use a reserved elf-wide relocation type code (R_RELC
   9131    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   9132    information (start bit, end bit, word width, etc) into the addend.  This
   9133    information is extracted from CGEN-generated operand tables within gas.
   9134 
   9135    Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
   9136    internal) representing prefix-notation expressions, including but not
   9137    limited to those sorts of expressions normally encoded as addends in the
   9138    addend field.  The symbol mangling format is:
   9139 
   9140    <node> := <literal>
   9141 	  |  <unary-operator> ':' <node>
   9142 	  |  <binary-operator> ':' <node> ':' <node>
   9143 	  ;
   9144 
   9145    <literal> := 's' <digits=N> ':' <N character symbol name>
   9146 	     |  'S' <digits=N> ':' <N character section name>
   9147 	     |  '#' <hexdigits>
   9148 	     ;
   9149 
   9150    <binary-operator> := as in C
   9151    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   9152 
   9153 static bool
   9154 set_symbol_value (bfd *bfd_with_globals,
   9155 		  Elf_Internal_Sym *isymbuf,
   9156 		  size_t locsymcount,
   9157 		  size_t num_sym,
   9158 		  size_t symidx,
   9159 		  bfd_vma val)
   9160 {
   9161   struct elf_link_hash_entry *h;
   9162   size_t extsymoff = locsymcount;
   9163 
   9164   if (symidx < locsymcount)
   9165     {
   9166       Elf_Internal_Sym *sym;
   9167 
   9168       sym = isymbuf + symidx;
   9169       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   9170 	{
   9171 	  /* It is a local symbol: move it to the
   9172 	     "absolute" section and give it a value.  */
   9173 	  sym->st_shndx = SHN_ABS;
   9174 	  sym->st_value = val;
   9175 	  return true;
   9176 	}
   9177       if (!elf_bad_symtab (bfd_with_globals))
   9178 	{
   9179 	  _bfd_error_handler (_("%pB: corrupt symbol table"),
   9180 			      bfd_with_globals);
   9181 	  bfd_set_error (bfd_error_bad_value);
   9182 	  return false;
   9183 	}
   9184       extsymoff = 0;
   9185     }
   9186 
   9187   /* It is a global symbol: set its link type
   9188      to "defined" and give it a value.  */
   9189   h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
   9190 				    extsymoff, num_sym);
   9191   if (h == NULL)
   9192     return false;
   9193   h->root.type = bfd_link_hash_defined;
   9194   h->root.u.def.value = val;
   9195   h->root.u.def.section = bfd_abs_section_ptr;
   9196   return true;
   9197 }
   9198 
   9199 static bool
   9200 resolve_symbol (const char *name,
   9201 		bfd *input_bfd,
   9202 		struct elf_final_link_info *flinfo,
   9203 		bfd_vma *result,
   9204 		Elf_Internal_Sym *isymbuf,
   9205 		size_t locsymcount)
   9206 {
   9207   Elf_Internal_Sym *sym;
   9208   struct bfd_link_hash_entry *global_entry;
   9209   const char *candidate = NULL;
   9210   Elf_Internal_Shdr *symtab_hdr;
   9211   size_t i;
   9212 
   9213   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   9214 
   9215   for (i = 0; i < locsymcount; ++ i)
   9216     {
   9217       sym = isymbuf + i;
   9218 
   9219       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   9220 	continue;
   9221 
   9222       candidate = bfd_elf_string_from_elf_section (input_bfd,
   9223 						   symtab_hdr->sh_link,
   9224 						   sym->st_name);
   9225 #ifdef DEBUG
   9226       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   9227 	      name, candidate, (unsigned long) sym->st_value);
   9228 #endif
   9229       if (candidate && strcmp (candidate, name) == 0)
   9230 	{
   9231 	  asection *sec = flinfo->sections [i];
   9232 
   9233 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   9234 	  *result += sec->output_offset + sec->output_section->vma;
   9235 #ifdef DEBUG
   9236 	  printf ("Found symbol with value %8.8lx\n",
   9237 		  (unsigned long) *result);
   9238 #endif
   9239 	  return true;
   9240 	}
   9241     }
   9242 
   9243   /* Hmm, haven't found it yet. perhaps it is a global.  */
   9244   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   9245 				       false, false, true);
   9246   if (!global_entry)
   9247     return false;
   9248 
   9249   if (global_entry->type == bfd_link_hash_defined
   9250       || global_entry->type == bfd_link_hash_defweak)
   9251     {
   9252       *result = (global_entry->u.def.value
   9253 		 + global_entry->u.def.section->output_section->vma
   9254 		 + global_entry->u.def.section->output_offset);
   9255 #ifdef DEBUG
   9256       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   9257 	      global_entry->root.string, (unsigned long) *result);
   9258 #endif
   9259       return true;
   9260     }
   9261 
   9262   return false;
   9263 }
   9264 
   9265 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
   9266    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
   9267    names like "foo.end" which is the end address of section "foo".  */
   9268 
   9269 static bool
   9270 resolve_section (const char *name,
   9271 		 asection *sections,
   9272 		 bfd_vma *result,
   9273 		 bfd * abfd)
   9274 {
   9275   asection *curr;
   9276   unsigned int len;
   9277 
   9278   for (curr = sections; curr; curr = curr->next)
   9279     if (strcmp (curr->name, name) == 0)
   9280       {
   9281 	*result = curr->vma;
   9282 	return true;
   9283       }
   9284 
   9285   /* Hmm. still haven't found it. try pseudo-section names.  */
   9286   /* FIXME: This could be coded more efficiently...  */
   9287   for (curr = sections; curr; curr = curr->next)
   9288     {
   9289       len = strlen (curr->name);
   9290       if (len > strlen (name))
   9291 	continue;
   9292 
   9293       if (strncmp (curr->name, name, len) == 0)
   9294 	{
   9295 	  if (startswith (name + len, ".end"))
   9296 	    {
   9297 	      *result = (curr->vma
   9298 			 + curr->size / bfd_octets_per_byte (abfd, curr));
   9299 	      return true;
   9300 	    }
   9301 
   9302 	  /* Insert more pseudo-section names here, if you like.  */
   9303 	}
   9304     }
   9305 
   9306   return false;
   9307 }
   9308 
   9309 static void
   9310 undefined_reference (const char *reftype, const char *name)
   9311 {
   9312   /* xgettext:c-format */
   9313   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   9314 		      reftype, name);
   9315   bfd_set_error (bfd_error_bad_value);
   9316 }
   9317 
   9318 static bool
   9319 eval_symbol (bfd_vma *result,
   9320 	     const char **symp,
   9321 	     bfd *input_bfd,
   9322 	     struct elf_final_link_info *flinfo,
   9323 	     bfd_vma dot,
   9324 	     Elf_Internal_Sym *isymbuf,
   9325 	     size_t locsymcount,
   9326 	     int signed_p)
   9327 {
   9328   size_t len;
   9329   size_t symlen;
   9330   bfd_vma a;
   9331   bfd_vma b;
   9332   char symbuf[4096];
   9333   const char *sym = *symp;
   9334   const char *symend;
   9335   bool symbol_is_section = false;
   9336 
   9337   len = strlen (sym);
   9338   symend = sym + len;
   9339 
   9340   if (len < 1 || len > sizeof (symbuf))
   9341     {
   9342       bfd_set_error (bfd_error_invalid_operation);
   9343       return false;
   9344     }
   9345 
   9346   switch (* sym)
   9347     {
   9348     case '.':
   9349       *result = dot;
   9350       *symp = sym + 1;
   9351       return true;
   9352 
   9353     case '#':
   9354       ++sym;
   9355       *result = strtoul (sym, (char **) symp, 16);
   9356       return true;
   9357 
   9358     case 'S':
   9359       symbol_is_section = true;
   9360       /* Fall through.  */
   9361     case 's':
   9362       ++sym;
   9363       symlen = strtol (sym, (char **) symp, 10);
   9364       sym = *symp + 1; /* Skip the trailing ':'.  */
   9365 
   9366       if (symend < sym || symlen + 1 > sizeof (symbuf))
   9367 	{
   9368 	  bfd_set_error (bfd_error_invalid_operation);
   9369 	  return false;
   9370 	}
   9371 
   9372       memcpy (symbuf, sym, symlen);
   9373       symbuf[symlen] = '\0';
   9374       *symp = sym + symlen;
   9375 
   9376       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   9377 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   9378 	 interpretation here; section means "try section first", not "must be a
   9379 	 section", and likewise with symbol.  */
   9380 
   9381       if (symbol_is_section)
   9382 	{
   9383 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
   9384 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   9385 				  isymbuf, locsymcount))
   9386 	    {
   9387 	      undefined_reference ("section", symbuf);
   9388 	      return false;
   9389 	    }
   9390 	}
   9391       else
   9392 	{
   9393 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   9394 			       isymbuf, locsymcount)
   9395 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   9396 				   result, input_bfd))
   9397 	    {
   9398 	      undefined_reference ("symbol", symbuf);
   9399 	      return false;
   9400 	    }
   9401 	}
   9402 
   9403       return true;
   9404 
   9405       /* All that remains are operators.  */
   9406 
   9407 #define UNARY_OP(op)						\
   9408   if (startswith (sym, #op))					\
   9409     {								\
   9410       sym += strlen (#op);					\
   9411       if (*sym == ':')						\
   9412 	++sym;							\
   9413       *symp = sym;						\
   9414       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   9415 			isymbuf, locsymcount, signed_p))	\
   9416 	return false;						\
   9417       if (signed_p)						\
   9418 	*result = op ((bfd_signed_vma) a);			\
   9419       else							\
   9420 	*result = op a;						\
   9421       return true;						\
   9422     }
   9423 
   9424 #define BINARY_OP_HEAD(op)					\
   9425   if (startswith (sym, #op))					\
   9426     {								\
   9427       sym += strlen (#op);					\
   9428       if (*sym == ':')						\
   9429 	++sym;							\
   9430       *symp = sym;						\
   9431       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   9432 			isymbuf, locsymcount, signed_p))	\
   9433 	return false;						\
   9434       ++*symp;							\
   9435       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   9436 			isymbuf, locsymcount, signed_p))	\
   9437 	return false;
   9438 #define BINARY_OP_TAIL(op)					\
   9439       if (signed_p)						\
   9440 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   9441       else							\
   9442 	*result = a op b;					\
   9443       return true;						\
   9444     }
   9445 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
   9446 
   9447     default:
   9448       UNARY_OP  (0-);
   9449       BINARY_OP_HEAD (<<);
   9450       if (b >= sizeof (a) * CHAR_BIT)
   9451 	{
   9452 	  *result = 0;
   9453 	  return true;
   9454 	}
   9455       signed_p = 0;
   9456       BINARY_OP_TAIL (<<);
   9457       BINARY_OP_HEAD (>>);
   9458       if (b >= sizeof (a) * CHAR_BIT)
   9459 	{
   9460 	  *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
   9461 	  return true;
   9462 	}
   9463       BINARY_OP_TAIL (>>);
   9464       BINARY_OP (==);
   9465       BINARY_OP (!=);
   9466       BINARY_OP (<=);
   9467       BINARY_OP (>=);
   9468       BINARY_OP (&&);
   9469       BINARY_OP (||);
   9470       UNARY_OP  (~);
   9471       UNARY_OP  (!);
   9472       BINARY_OP (*);
   9473       BINARY_OP_HEAD (/);
   9474       if (b == 0)
   9475 	{
   9476 	  _bfd_error_handler (_("division by zero"));
   9477 	  bfd_set_error (bfd_error_bad_value);
   9478 	  return false;
   9479 	}
   9480       BINARY_OP_TAIL (/);
   9481       BINARY_OP_HEAD (%);
   9482       if (b == 0)
   9483 	{
   9484 	  _bfd_error_handler (_("division by zero"));
   9485 	  bfd_set_error (bfd_error_bad_value);
   9486 	  return false;
   9487 	}
   9488       BINARY_OP_TAIL (%);
   9489       BINARY_OP (^);
   9490       BINARY_OP (|);
   9491       BINARY_OP (&);
   9492       BINARY_OP (+);
   9493       BINARY_OP (-);
   9494       BINARY_OP (<);
   9495       BINARY_OP (>);
   9496 #undef UNARY_OP
   9497 #undef BINARY_OP
   9498       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   9499       bfd_set_error (bfd_error_invalid_operation);
   9500       return false;
   9501     }
   9502 }
   9503 
   9504 static void
   9505 put_value (bfd_vma size,
   9506 	   unsigned long chunksz,
   9507 	   bfd *input_bfd,
   9508 	   bfd_vma x,
   9509 	   bfd_byte *location)
   9510 {
   9511   location += (size - chunksz);
   9512 
   9513   for (; size; size -= chunksz, location -= chunksz)
   9514     {
   9515       switch (chunksz)
   9516 	{
   9517 	case 1:
   9518 	  bfd_put_8 (input_bfd, x, location);
   9519 	  x >>= 8;
   9520 	  break;
   9521 	case 2:
   9522 	  bfd_put_16 (input_bfd, x, location);
   9523 	  x >>= 16;
   9524 	  break;
   9525 	case 4:
   9526 	  bfd_put_32 (input_bfd, x, location);
   9527 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   9528 	  x >>= 16;
   9529 	  x >>= 16;
   9530 	  break;
   9531 #ifdef BFD64
   9532 	case 8:
   9533 	  bfd_put_64 (input_bfd, x, location);
   9534 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   9535 	  x >>= 32;
   9536 	  x >>= 32;
   9537 	  break;
   9538 #endif
   9539 	default:
   9540 	  abort ();
   9541 	  break;
   9542 	}
   9543     }
   9544 }
   9545 
   9546 static bfd_vma
   9547 get_value (bfd_vma size,
   9548 	   unsigned long chunksz,
   9549 	   bfd *input_bfd,
   9550 	   bfd_byte *location)
   9551 {
   9552   int shift;
   9553   bfd_vma x = 0;
   9554 
   9555   /* Sanity checks.  */
   9556   BFD_ASSERT (chunksz <= sizeof (x)
   9557 	      && size >= chunksz
   9558 	      && chunksz != 0
   9559 	      && (size % chunksz) == 0
   9560 	      && input_bfd != NULL
   9561 	      && location != NULL);
   9562 
   9563   if (chunksz == sizeof (x))
   9564     {
   9565       BFD_ASSERT (size == chunksz);
   9566 
   9567       /* Make sure that we do not perform an undefined shift operation.
   9568 	 We know that size == chunksz so there will only be one iteration
   9569 	 of the loop below.  */
   9570       shift = 0;
   9571     }
   9572   else
   9573     shift = 8 * chunksz;
   9574 
   9575   for (; size; size -= chunksz, location += chunksz)
   9576     {
   9577       switch (chunksz)
   9578 	{
   9579 	case 1:
   9580 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   9581 	  break;
   9582 	case 2:
   9583 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   9584 	  break;
   9585 	case 4:
   9586 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   9587 	  break;
   9588 #ifdef BFD64
   9589 	case 8:
   9590 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   9591 	  break;
   9592 #endif
   9593 	default:
   9594 	  abort ();
   9595 	}
   9596     }
   9597   return x;
   9598 }
   9599 
   9600 static void
   9601 decode_complex_addend (unsigned long *start,   /* in bits */
   9602 		       unsigned long *oplen,   /* in bits */
   9603 		       unsigned long *len,     /* in bits */
   9604 		       unsigned long *wordsz,  /* in bytes */
   9605 		       unsigned long *chunksz, /* in bytes */
   9606 		       unsigned long *lsb0_p,
   9607 		       unsigned long *signed_p,
   9608 		       unsigned long *trunc_p,
   9609 		       unsigned long encoded)
   9610 {
   9611   * start     =	 encoded	& 0x3F;
   9612   * len	      = (encoded >>  6) & 0x3F;
   9613   * oplen     = (encoded >> 12) & 0x3F;
   9614   * wordsz    = (encoded >> 18) & 0xF;
   9615   * chunksz   = (encoded >> 22) & 0xF;
   9616   * lsb0_p    = (encoded >> 27) & 1;
   9617   * signed_p  = (encoded >> 28) & 1;
   9618   * trunc_p   = (encoded >> 29) & 1;
   9619 }
   9620 
   9621 bfd_reloc_status_type
   9622 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   9623 				    asection *input_section,
   9624 				    bfd_byte *contents,
   9625 				    Elf_Internal_Rela *rel,
   9626 				    bfd_vma relocation)
   9627 {
   9628   bfd_vma shift, x, mask;
   9629   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   9630   bfd_reloc_status_type r;
   9631   bfd_size_type octets;
   9632 
   9633   /*  Perform this reloc, since it is complex.
   9634       (this is not to say that it necessarily refers to a complex
   9635       symbol; merely that it is a self-describing CGEN based reloc.
   9636       i.e. the addend has the complete reloc information (bit start, end,
   9637       word size, etc) encoded within it.).  */
   9638 
   9639   decode_complex_addend (&start, &oplen, &len, &wordsz,
   9640 			 &chunksz, &lsb0_p, &signed_p,
   9641 			 &trunc_p, rel->r_addend);
   9642 
   9643   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   9644 
   9645   if (lsb0_p)
   9646     shift = (start + 1) - len;
   9647   else
   9648     shift = (8 * wordsz) - (start + len);
   9649 
   9650   octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
   9651   x = get_value (wordsz, chunksz, input_bfd, contents + octets);
   9652 
   9653 #ifdef DEBUG
   9654   printf ("Doing complex reloc: "
   9655 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   9656 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   9657 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   9658 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   9659 	  oplen, (unsigned long) x, (unsigned long) mask,
   9660 	  (unsigned long) relocation);
   9661 #endif
   9662 
   9663   r = bfd_reloc_ok;
   9664   if (! trunc_p)
   9665     /* Now do an overflow check.  */
   9666     r = bfd_check_overflow ((signed_p
   9667 			     ? complain_overflow_signed
   9668 			     : complain_overflow_unsigned),
   9669 			    len, 0, (8 * wordsz),
   9670 			    relocation);
   9671 
   9672   /* Do the deed.  */
   9673   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   9674 
   9675 #ifdef DEBUG
   9676   printf ("           relocation: %8.8lx\n"
   9677 	  "         shifted mask: %8.8lx\n"
   9678 	  " shifted/masked reloc: %8.8lx\n"
   9679 	  "               result: %8.8lx\n",
   9680 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   9681 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   9682 #endif
   9683   put_value (wordsz, chunksz, input_bfd, x, contents + octets);
   9684   return r;
   9685 }
   9686 
   9687 /* Functions to read r_offset from external (target order) reloc
   9688    entry.  Faster than bfd_getl32 et al, because we let the compiler
   9689    know the value is aligned.  */
   9690 
   9691 static bfd_vma
   9692 ext32l_r_offset (const void *p)
   9693 {
   9694   union aligned32
   9695   {
   9696     uint32_t v;
   9697     unsigned char c[4];
   9698   };
   9699   const union aligned32 *a
   9700     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9701 
   9702   uint32_t aval = (  (uint32_t) a->c[0]
   9703 		   | (uint32_t) a->c[1] << 8
   9704 		   | (uint32_t) a->c[2] << 16
   9705 		   | (uint32_t) a->c[3] << 24);
   9706   return aval;
   9707 }
   9708 
   9709 static bfd_vma
   9710 ext32b_r_offset (const void *p)
   9711 {
   9712   union aligned32
   9713   {
   9714     uint32_t v;
   9715     unsigned char c[4];
   9716   };
   9717   const union aligned32 *a
   9718     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9719 
   9720   uint32_t aval = (  (uint32_t) a->c[0] << 24
   9721 		   | (uint32_t) a->c[1] << 16
   9722 		   | (uint32_t) a->c[2] << 8
   9723 		   | (uint32_t) a->c[3]);
   9724   return aval;
   9725 }
   9726 
   9727 static bfd_vma
   9728 ext64l_r_offset (const void *p)
   9729 {
   9730   union aligned64
   9731   {
   9732     uint64_t v;
   9733     unsigned char c[8];
   9734   };
   9735   const union aligned64 *a
   9736     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9737 
   9738   uint64_t aval = (  (uint64_t) a->c[0]
   9739 		   | (uint64_t) a->c[1] << 8
   9740 		   | (uint64_t) a->c[2] << 16
   9741 		   | (uint64_t) a->c[3] << 24
   9742 		   | (uint64_t) a->c[4] << 32
   9743 		   | (uint64_t) a->c[5] << 40
   9744 		   | (uint64_t) a->c[6] << 48
   9745 		   | (uint64_t) a->c[7] << 56);
   9746   return aval;
   9747 }
   9748 
   9749 static bfd_vma
   9750 ext64b_r_offset (const void *p)
   9751 {
   9752   union aligned64
   9753   {
   9754     uint64_t v;
   9755     unsigned char c[8];
   9756   };
   9757   const union aligned64 *a
   9758     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9759 
   9760   uint64_t aval = (  (uint64_t) a->c[0] << 56
   9761 		   | (uint64_t) a->c[1] << 48
   9762 		   | (uint64_t) a->c[2] << 40
   9763 		   | (uint64_t) a->c[3] << 32
   9764 		   | (uint64_t) a->c[4] << 24
   9765 		   | (uint64_t) a->c[5] << 16
   9766 		   | (uint64_t) a->c[6] << 8
   9767 		   | (uint64_t) a->c[7]);
   9768   return aval;
   9769 }
   9770 
   9771 /* When performing a relocatable link, the input relocations are
   9772    preserved.  But, if they reference global symbols, the indices
   9773    referenced must be updated.  Update all the relocations found in
   9774    RELDATA.  */
   9775 
   9776 static bool
   9777 elf_link_adjust_relocs (bfd *abfd,
   9778 			asection *sec,
   9779 			struct bfd_elf_section_reloc_data *reldata,
   9780 			bool sort,
   9781 			struct bfd_link_info *info)
   9782 {
   9783   unsigned int i;
   9784   elf_backend_data *bed = get_elf_backend_data (abfd);
   9785   bfd_byte *erela;
   9786   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   9787   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   9788   bfd_vma r_type_mask;
   9789   int r_sym_shift;
   9790   unsigned int count = reldata->count;
   9791   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   9792 
   9793   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   9794     {
   9795       swap_in = bed->s->swap_reloc_in;
   9796       swap_out = bed->s->swap_reloc_out;
   9797     }
   9798   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   9799     {
   9800       swap_in = bed->s->swap_reloca_in;
   9801       swap_out = bed->s->swap_reloca_out;
   9802     }
   9803   else
   9804     abort ();
   9805 
   9806   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   9807     abort ();
   9808 
   9809   if (bed->s->arch_size == 32)
   9810     {
   9811       r_type_mask = 0xff;
   9812       r_sym_shift = 8;
   9813     }
   9814   else
   9815     {
   9816       r_type_mask = 0xffffffff;
   9817       r_sym_shift = 32;
   9818     }
   9819 
   9820   erela = reldata->hdr->contents;
   9821   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   9822     {
   9823       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   9824       unsigned int j;
   9825 
   9826       if (*rel_hash == NULL)
   9827 	continue;
   9828 
   9829       if ((*rel_hash)->indx == -2
   9830 	  && info->gc_sections
   9831 	  && ! info->gc_keep_exported)
   9832 	{
   9833 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
   9834 	  _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
   9835 			      abfd, sec,
   9836 			      (*rel_hash)->root.root.string);
   9837 	  _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
   9838 			      abfd, sec);
   9839 	  bfd_set_error (bfd_error_invalid_operation);
   9840 	  return false;
   9841 	}
   9842       BFD_ASSERT ((*rel_hash)->indx >= 0);
   9843 
   9844       (*swap_in) (abfd, erela, irela);
   9845       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   9846 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   9847 			   | (irela[j].r_info & r_type_mask));
   9848       (*swap_out) (abfd, irela, erela);
   9849     }
   9850 
   9851   if (bed->elf_backend_update_relocs)
   9852     (*bed->elf_backend_update_relocs) (sec, reldata);
   9853 
   9854   if (sort && count != 0)
   9855     {
   9856       bfd_vma (*ext_r_off) (const void *);
   9857       bfd_vma r_off;
   9858       size_t elt_size;
   9859       bfd_byte *base, *end, *p, *loc;
   9860       bfd_byte *buf = NULL;
   9861 
   9862       if (bed->s->arch_size == 32)
   9863 	{
   9864 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9865 	    ext_r_off = ext32l_r_offset;
   9866 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9867 	    ext_r_off = ext32b_r_offset;
   9868 	  else
   9869 	    abort ();
   9870 	}
   9871       else
   9872 	{
   9873 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9874 	    ext_r_off = ext64l_r_offset;
   9875 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9876 	    ext_r_off = ext64b_r_offset;
   9877 	  else
   9878 	    abort ();
   9879 	}
   9880 
   9881       /*  Must use a stable sort here.  A modified insertion sort,
   9882 	  since the relocs are mostly sorted already.  */
   9883       elt_size = reldata->hdr->sh_entsize;
   9884       base = reldata->hdr->contents;
   9885       end = base + count * elt_size;
   9886       if (elt_size > sizeof (Elf64_External_Rela))
   9887 	abort ();
   9888 
   9889       /* Ensure the first element is lowest.  This acts as a sentinel,
   9890 	 speeding the main loop below.  */
   9891       r_off = (*ext_r_off) (base);
   9892       for (p = loc = base; (p += elt_size) < end; )
   9893 	{
   9894 	  bfd_vma r_off2 = (*ext_r_off) (p);
   9895 	  if (r_off > r_off2)
   9896 	    {
   9897 	      r_off = r_off2;
   9898 	      loc = p;
   9899 	    }
   9900 	}
   9901       if (loc != base)
   9902 	{
   9903 	  /* Don't just swap *base and *loc as that changes the order
   9904 	     of the original base[0] and base[1] if they happen to
   9905 	     have the same r_offset.  */
   9906 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   9907 	  memcpy (onebuf, loc, elt_size);
   9908 	  memmove (base + elt_size, base, loc - base);
   9909 	  memcpy (base, onebuf, elt_size);
   9910 	}
   9911 
   9912       for (p = base + elt_size; (p += elt_size) < end; )
   9913 	{
   9914 	  /* base to p is sorted, *p is next to insert.  */
   9915 	  r_off = (*ext_r_off) (p);
   9916 	  /* Search the sorted region for location to insert.  */
   9917 	  loc = p - elt_size;
   9918 	  while (r_off < (*ext_r_off) (loc))
   9919 	    loc -= elt_size;
   9920 	  loc += elt_size;
   9921 	  if (loc != p)
   9922 	    {
   9923 	      /* Chances are there is a run of relocs to insert here,
   9924 		 from one of more input files.  Files are not always
   9925 		 linked in order due to the way elf_link_input_bfd is
   9926 		 called.  See pr17666.  */
   9927 	      size_t sortlen = p - loc;
   9928 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   9929 	      size_t runlen = elt_size;
   9930 	      bfd_vma r_off_runend = r_off;
   9931 	      bfd_vma r_off_runend_next;
   9932 	      size_t buf_size = 96 * 1024;
   9933 	      while (p + runlen < end
   9934 		     && (sortlen <= buf_size
   9935 			 || runlen + elt_size <= buf_size)
   9936 		     /* run must not break the ordering of base..loc+1 */
   9937 		     && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
   9938 		     /* run must be already sorted */
   9939 		     && r_off_runend_next >= r_off_runend)
   9940 		{
   9941 		  runlen += elt_size;
   9942 		  r_off_runend = r_off_runend_next;
   9943 		}
   9944 	      if (buf == NULL)
   9945 		{
   9946 		  buf = bfd_malloc (buf_size);
   9947 		  if (buf == NULL)
   9948 		    return false;
   9949 		}
   9950 	      if (runlen < sortlen)
   9951 		{
   9952 		  memcpy (buf, p, runlen);
   9953 		  memmove (loc + runlen, loc, sortlen);
   9954 		  memcpy (loc, buf, runlen);
   9955 		}
   9956 	      else
   9957 		{
   9958 		  memcpy (buf, loc, sortlen);
   9959 		  memmove (loc, p, runlen);
   9960 		  memcpy (loc + runlen, buf, sortlen);
   9961 		}
   9962 	      p += runlen - elt_size;
   9963 	    }
   9964 	}
   9965       /* Hashes are no longer valid.  */
   9966       free (reldata->hashes);
   9967       reldata->hashes = NULL;
   9968       free (buf);
   9969     }
   9970   return true;
   9971 }
   9972 
   9973 struct elf_link_sort_rela
   9974 {
   9975   union {
   9976     bfd_vma offset;
   9977     bfd_vma sym_mask;
   9978   } u;
   9979   enum elf_reloc_type_class type;
   9980   /* We use this as an array of size int_rels_per_ext_rel.  */
   9981   Elf_Internal_Rela rela[1];
   9982 };
   9983 
   9984 /* qsort stability here and for cmp2 is only an issue if multiple
   9985    dynamic relocations are emitted at the same address.  But targets
   9986    that apply a series of dynamic relocations each operating on the
   9987    result of the prior relocation can't use -z combreloc as
   9988    implemented anyway.  Such schemes tend to be broken by sorting on
   9989    symbol index.  That leaves dynamic NONE relocs as the only other
   9990    case where ld might emit multiple relocs at the same address, and
   9991    those are only emitted due to target bugs.  */
   9992 
   9993 static int
   9994 elf_link_sort_cmp1 (const void *A, const void *B)
   9995 {
   9996   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9997   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9998   int relativea, relativeb;
   9999 
   10000   relativea = a->type == reloc_class_relative;
   10001   relativeb = b->type == reloc_class_relative;
   10002 
   10003   if (relativea < relativeb)
   10004     return 1;
   10005   if (relativea > relativeb)
   10006     return -1;
   10007   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   10008     return -1;
   10009   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   10010     return 1;
   10011   if (a->rela->r_offset < b->rela->r_offset)
   10012     return -1;
   10013   if (a->rela->r_offset > b->rela->r_offset)
   10014     return 1;
   10015   return 0;
   10016 }
   10017 
   10018 static int
   10019 elf_link_sort_cmp2 (const void *A, const void *B)
   10020 {
   10021   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   10022   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   10023 
   10024   if (a->type < b->type)
   10025     return -1;
   10026   if (a->type > b->type)
   10027     return 1;
   10028   if (a->u.offset < b->u.offset)
   10029     return -1;
   10030   if (a->u.offset > b->u.offset)
   10031     return 1;
   10032   if (a->rela->r_offset < b->rela->r_offset)
   10033     return -1;
   10034   if (a->rela->r_offset > b->rela->r_offset)
   10035     return 1;
   10036   return 0;
   10037 }
   10038 
   10039 static size_t
   10040 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   10041 {
   10042   asection *dynamic_relocs;
   10043   asection *rela_dyn;
   10044   asection *rel_dyn;
   10045   bfd_size_type count, size;
   10046   size_t i, ret, sort_elt, ext_size;
   10047   bfd_byte *sort, *s_non_relative, *p;
   10048   struct elf_link_sort_rela *sq;
   10049   elf_backend_data *bed = get_elf_backend_data (abfd);
   10050   int i2e = bed->s->int_rels_per_ext_rel;
   10051   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   10052   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   10053   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   10054   struct bfd_link_order *lo;
   10055   bfd_vma r_sym_mask;
   10056   bool use_rela;
   10057 
   10058   /* Find a dynamic reloc section.  */
   10059   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   10060   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   10061   if (rela_dyn != NULL && rela_dyn->size > 0
   10062       && rel_dyn != NULL && rel_dyn->size > 0)
   10063     {
   10064       bool use_rela_initialised = false;
   10065 
   10066       /* This is just here to stop gcc from complaining.
   10067 	 Its initialization checking code is not perfect.  */
   10068       use_rela = true;
   10069 
   10070       /* Both sections are present.  Examine the sizes
   10071 	 of the indirect sections to help us choose.  */
   10072       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   10073 	if (lo->type == bfd_indirect_link_order)
   10074 	  {
   10075 	    asection *o = lo->u.indirect.section;
   10076 
   10077 	    if ((o->size % bed->s->sizeof_rela) == 0)
   10078 	      {
   10079 		if ((o->size % bed->s->sizeof_rel) == 0)
   10080 		  /* Section size is divisible by both rel and rela sizes.
   10081 		     It is of no help to us.  */
   10082 		  ;
   10083 		else
   10084 		  {
   10085 		    /* Section size is only divisible by rela.  */
   10086 		    if (use_rela_initialised && !use_rela)
   10087 		      {
   10088 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   10089 					      "they are in more than one size"),
   10090 					    abfd);
   10091 			bfd_set_error (bfd_error_invalid_operation);
   10092 			return 0;
   10093 		      }
   10094 		    else
   10095 		      {
   10096 			use_rela = true;
   10097 			use_rela_initialised = true;
   10098 		      }
   10099 		  }
   10100 	      }
   10101 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   10102 	      {
   10103 		/* Section size is only divisible by rel.  */
   10104 		if (use_rela_initialised && use_rela)
   10105 		  {
   10106 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   10107 					  "they are in more than one size"),
   10108 					abfd);
   10109 		    bfd_set_error (bfd_error_invalid_operation);
   10110 		    return 0;
   10111 		  }
   10112 		else
   10113 		  {
   10114 		    use_rela = false;
   10115 		    use_rela_initialised = true;
   10116 		  }
   10117 	      }
   10118 	    else
   10119 	      {
   10120 		/* The section size is not divisible by either -
   10121 		   something is wrong.  */
   10122 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   10123 				      "they are of an unknown size"), abfd);
   10124 		bfd_set_error (bfd_error_invalid_operation);
   10125 		return 0;
   10126 	      }
   10127 	  }
   10128 
   10129       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   10130 	if (lo->type == bfd_indirect_link_order)
   10131 	  {
   10132 	    asection *o = lo->u.indirect.section;
   10133 
   10134 	    if ((o->size % bed->s->sizeof_rela) == 0)
   10135 	      {
   10136 		if ((o->size % bed->s->sizeof_rel) == 0)
   10137 		  /* Section size is divisible by both rel and rela sizes.
   10138 		     It is of no help to us.  */
   10139 		  ;
   10140 		else
   10141 		  {
   10142 		    /* Section size is only divisible by rela.  */
   10143 		    if (use_rela_initialised && !use_rela)
   10144 		      {
   10145 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   10146 					      "they are in more than one size"),
   10147 					    abfd);
   10148 			bfd_set_error (bfd_error_invalid_operation);
   10149 			return 0;
   10150 		      }
   10151 		    else
   10152 		      {
   10153 			use_rela = true;
   10154 			use_rela_initialised = true;
   10155 		      }
   10156 		  }
   10157 	      }
   10158 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   10159 	      {
   10160 		/* Section size is only divisible by rel.  */
   10161 		if (use_rela_initialised && use_rela)
   10162 		  {
   10163 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   10164 					  "they are in more than one size"),
   10165 					abfd);
   10166 		    bfd_set_error (bfd_error_invalid_operation);
   10167 		    return 0;
   10168 		  }
   10169 		else
   10170 		  {
   10171 		    use_rela = false;
   10172 		    use_rela_initialised = true;
   10173 		  }
   10174 	      }
   10175 	    else
   10176 	      {
   10177 		/* The section size is not divisible by either -
   10178 		   something is wrong.  */
   10179 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   10180 				      "they are of an unknown size"), abfd);
   10181 		bfd_set_error (bfd_error_invalid_operation);
   10182 		return 0;
   10183 	      }
   10184 	  }
   10185 
   10186       if (! use_rela_initialised)
   10187 	/* Make a guess.  */
   10188 	use_rela = true;
   10189     }
   10190   else if (rela_dyn != NULL && rela_dyn->size > 0)
   10191     use_rela = true;
   10192   else if (rel_dyn != NULL && rel_dyn->size > 0)
   10193     use_rela = false;
   10194   else
   10195     return 0;
   10196 
   10197   if (use_rela)
   10198     {
   10199       dynamic_relocs = rela_dyn;
   10200       ext_size = bed->s->sizeof_rela;
   10201       swap_in = bed->s->swap_reloca_in;
   10202       swap_out = bed->s->swap_reloca_out;
   10203     }
   10204   else
   10205     {
   10206       dynamic_relocs = rel_dyn;
   10207       ext_size = bed->s->sizeof_rel;
   10208       swap_in = bed->s->swap_reloc_in;
   10209       swap_out = bed->s->swap_reloc_out;
   10210     }
   10211 
   10212   size = 0;
   10213   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10214     if (lo->type == bfd_indirect_link_order)
   10215       size += lo->u.indirect.section->size;
   10216 
   10217   if (size != dynamic_relocs->size)
   10218     return 0;
   10219 
   10220   sort_elt = (sizeof (struct elf_link_sort_rela)
   10221 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   10222 
   10223   count = dynamic_relocs->size / ext_size;
   10224   if (count == 0)
   10225     return 0;
   10226   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   10227 
   10228   if (sort == NULL)
   10229     {
   10230       (*info->callbacks->warning)
   10231 	(info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
   10232       return 0;
   10233     }
   10234 
   10235   if (bed->s->arch_size == 32)
   10236     r_sym_mask = ~(bfd_vma) 0xff;
   10237   else
   10238     r_sym_mask = ~(bfd_vma) 0xffffffff;
   10239 
   10240   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10241     if (lo->type == bfd_indirect_link_order)
   10242       {
   10243 	bfd_byte *erel, *erelend;
   10244 	asection *o = lo->u.indirect.section;
   10245 
   10246 	if (o->contents == NULL && o->size != 0)
   10247 	  {
   10248 	    /* This is a reloc section that is being handled as a normal
   10249 	       section.  See bfd_section_from_shdr.  We can't combine
   10250 	       relocs in this case.  */
   10251 	    free (sort);
   10252 	    return 0;
   10253 	  }
   10254 	erel = o->contents;
   10255 	erelend = o->contents + o->size;
   10256 	p = sort + o->output_offset * opb / ext_size * sort_elt;
   10257 
   10258 	while (erel < erelend)
   10259 	  {
   10260 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10261 
   10262 	    (*swap_in) (abfd, erel, s->rela);
   10263 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   10264 	    s->u.sym_mask = r_sym_mask;
   10265 	    p += sort_elt;
   10266 	    erel += ext_size;
   10267 	  }
   10268       }
   10269 
   10270   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   10271 
   10272   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   10273     {
   10274       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10275       if (s->type != reloc_class_relative)
   10276 	break;
   10277     }
   10278   ret = i;
   10279   s_non_relative = p;
   10280 
   10281   sq = (struct elf_link_sort_rela *) s_non_relative;
   10282   for (; i < count; i++, p += sort_elt)
   10283     {
   10284       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   10285       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   10286 	sq = sp;
   10287       sp->u.offset = sq->rela->r_offset;
   10288     }
   10289 
   10290   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   10291 
   10292   struct elf_link_hash_table *htab = elf_hash_table (info);
   10293   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
   10294     {
   10295       /* We have plt relocs in .rela.dyn.  */
   10296       sq = (struct elf_link_sort_rela *) sort;
   10297       for (i = 0; i < count; i++)
   10298 	if (sq[count - i - 1].type != reloc_class_plt)
   10299 	  break;
   10300       if (i != 0 && htab->srelplt->size == i * ext_size)
   10301 	{
   10302 	  struct bfd_link_order **plo;
   10303 	  /* Put srelplt link_order last.  This is so the output_offset
   10304 	     set in the next loop is correct for DT_JMPREL.  */
   10305 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
   10306 	    if ((*plo)->type == bfd_indirect_link_order
   10307 		&& (*plo)->u.indirect.section == htab->srelplt)
   10308 	      {
   10309 		lo = *plo;
   10310 		*plo = lo->next;
   10311 	      }
   10312 	    else
   10313 	      plo = &(*plo)->next;
   10314 	  *plo = lo;
   10315 	  lo->next = NULL;
   10316 	  dynamic_relocs->map_tail.link_order = lo;
   10317 	}
   10318     }
   10319 
   10320   p = sort;
   10321   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10322     if (lo->type == bfd_indirect_link_order)
   10323       {
   10324 	bfd_byte *erel, *erelend;
   10325 	asection *o = lo->u.indirect.section;
   10326 
   10327 	erel = o->contents;
   10328 	erelend = o->contents + o->size;
   10329 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
   10330 	while (erel < erelend)
   10331 	  {
   10332 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10333 	    (*swap_out) (abfd, s->rela, erel);
   10334 	    p += sort_elt;
   10335 	    erel += ext_size;
   10336 	  }
   10337       }
   10338 
   10339   free (sort);
   10340   *psec = dynamic_relocs;
   10341   return ret;
   10342 }
   10343 
   10344 /* Add a symbol to the output symbol string table.  */
   10345 
   10346 static int
   10347 elf_link_output_symstrtab (void *finf,
   10348 			   const char *name,
   10349 			   Elf_Internal_Sym *elfsym,
   10350 			   asection *input_sec,
   10351 			   struct elf_link_hash_entry *h)
   10352 {
   10353   struct elf_final_link_info *flinfo = finf;
   10354   int (*output_symbol_hook)
   10355     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   10356      struct elf_link_hash_entry *);
   10357   struct elf_link_hash_table *hash_table;
   10358   elf_backend_data *bed;
   10359   bfd_size_type strtabsize;
   10360 
   10361   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10362 
   10363   bed = get_elf_backend_data (flinfo->output_bfd);
   10364   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   10365   if (output_symbol_hook != NULL)
   10366     {
   10367       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   10368       if (ret != 1)
   10369 	return ret;
   10370     }
   10371 
   10372   if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
   10373     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
   10374   if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
   10375     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
   10376 
   10377   if (name == NULL || *name == '\0')
   10378     elfsym->st_name = (unsigned long) -1;
   10379   else
   10380     {
   10381       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   10382 	 to get the final offset for st_name.  */
   10383       char *versioned_name = (char *) name;
   10384       if (h != NULL)
   10385 	{
   10386 	  if (h->versioned == versioned && h->def_dynamic)
   10387 	    {
   10388 	      /* Keep only one '@' for versioned symbols defined in
   10389 	         shared objects.  */
   10390 	      const char *version = strrchr (name, ELF_VER_CHR);
   10391 	      const char *base_end = strchr (name, ELF_VER_CHR);
   10392 	      if (version != base_end)
   10393 		{
   10394 		  size_t base_len;
   10395 		  size_t len = strlen (name);
   10396 		  versioned_name = bfd_alloc (flinfo->output_bfd, len);
   10397 		  if (versioned_name == NULL)
   10398 		    return 0;
   10399 		  base_len = base_end - name;
   10400 		  memcpy (versioned_name, name, base_len);
   10401 		  memcpy (versioned_name + base_len, version,
   10402 			  len - base_len);
   10403 		}
   10404 	    }
   10405 	}
   10406       else if (flinfo->info->unique_symbol
   10407 	       && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
   10408 	{
   10409 	  struct local_hash_entry *lh;
   10410 	  size_t count_len;
   10411 	  size_t base_len;
   10412 	  char buf[30];
   10413 	  switch (ELF_ST_TYPE (elfsym->st_info))
   10414 	    {
   10415 	    case STT_FILE:
   10416 	    case STT_SECTION:
   10417 	      break;
   10418 	    default:
   10419 	      lh = (struct local_hash_entry *) bfd_hash_lookup
   10420 		     (&flinfo->local_hash_table, name, true, false);
   10421 	      if (lh == NULL)
   10422 		return 0;
   10423 	      /* Always append ".COUNT" to local symbols to avoid
   10424 		 potential conflicts with local symbol "XXX.COUNT".  */
   10425 	      sprintf (buf, "%lx", lh->count);
   10426 	      base_len = lh->size;
   10427 	      if (!base_len)
   10428 		{
   10429 		  base_len = strlen (name);
   10430 		  lh->size = base_len;
   10431 		}
   10432 	      count_len = strlen (buf);
   10433 	      versioned_name = bfd_alloc (flinfo->output_bfd,
   10434 					  base_len + count_len + 2);
   10435 	      if (versioned_name == NULL)
   10436 		return 0;
   10437 	      memcpy (versioned_name, name, base_len);
   10438 	      versioned_name[base_len] = '.';
   10439 	      memcpy (versioned_name + base_len + 1, buf,
   10440 		      count_len + 1);
   10441 	      lh->count++;
   10442 	      break;
   10443 	    }
   10444 	}
   10445       elfsym->st_name
   10446 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   10447 					       versioned_name, false);
   10448       if (elfsym->st_name == (unsigned long) -1)
   10449 	return 0;
   10450     }
   10451 
   10452   hash_table = elf_hash_table (flinfo->info);
   10453   strtabsize = hash_table->strtabsize;
   10454   if (strtabsize <= flinfo->output_bfd->symcount)
   10455     {
   10456       strtabsize += strtabsize;
   10457       hash_table->strtabsize = strtabsize;
   10458       strtabsize *= sizeof (*hash_table->strtab);
   10459       hash_table->strtab
   10460 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   10461 						 strtabsize);
   10462       if (hash_table->strtab == NULL)
   10463 	return 0;
   10464     }
   10465   hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
   10466   hash_table->strtab[flinfo->output_bfd->symcount].dest_index
   10467     = flinfo->output_bfd->symcount;
   10468   flinfo->output_bfd->symcount += 1;
   10469 
   10470   return 1;
   10471 }
   10472 
   10473 /* Swap symbols out to the symbol table and flush the output symbols to
   10474    the file.  */
   10475 
   10476 static bool
   10477 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   10478 {
   10479   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   10480   size_t amt;
   10481   size_t i;
   10482   elf_backend_data *bed;
   10483   bfd_byte *symbuf;
   10484   Elf_Internal_Shdr *hdr;
   10485   file_ptr pos;
   10486   bool ret;
   10487 
   10488   if (flinfo->output_bfd->symcount == 0)
   10489     return true;
   10490 
   10491   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10492 
   10493   bed = get_elf_backend_data (flinfo->output_bfd);
   10494 
   10495   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10496   symbuf = (bfd_byte *) bfd_malloc (amt);
   10497   if (symbuf == NULL)
   10498     return false;
   10499 
   10500   if (flinfo->symshndxbuf)
   10501     {
   10502       amt = sizeof (Elf_External_Sym_Shndx);
   10503       amt *= bfd_get_symcount (flinfo->output_bfd);
   10504       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   10505       if (flinfo->symshndxbuf == NULL)
   10506 	{
   10507 	  free (symbuf);
   10508 	  return false;
   10509 	}
   10510     }
   10511 
   10512   /* Now swap out the symbols.  */
   10513   for (i = 0; i < flinfo->output_bfd->symcount; i++)
   10514     {
   10515       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   10516       if (elfsym->sym.st_name == (unsigned long) -1)
   10517 	elfsym->sym.st_name = 0;
   10518       else
   10519 	elfsym->sym.st_name
   10520 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   10521 						    elfsym->sym.st_name);
   10522 
   10523       /* Inform the linker of the addition of this symbol.  */
   10524 
   10525       if (flinfo->info->callbacks->ctf_new_symbol)
   10526 	flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
   10527 						 &elfsym->sym);
   10528 
   10529       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   10530 			       ((bfd_byte *) symbuf
   10531 				+ (elfsym->dest_index
   10532 				   * bed->s->sizeof_sym)),
   10533 			       NPTR_ADD (flinfo->symshndxbuf,
   10534 					 elfsym->dest_index));
   10535     }
   10536 
   10537   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   10538   pos = hdr->sh_offset + hdr->sh_size;
   10539   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10540   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   10541       && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
   10542     {
   10543       hdr->sh_size += amt;
   10544       ret = true;
   10545     }
   10546   else
   10547     ret = false;
   10548 
   10549   free (symbuf);
   10550   return ret;
   10551 }
   10552 
   10553 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   10554 
   10555 static bool
   10556 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   10557 {
   10558   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   10559       && sym->st_shndx < SHN_LORESERVE)
   10560     {
   10561       /* The gABI doesn't support dynamic symbols in output sections
   10562 	 beyond 64k.  */
   10563       _bfd_error_handler
   10564 	/* xgettext:c-format */
   10565 	(_("%pB: too many sections: %d (>= %d)"),
   10566 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   10567       bfd_set_error (bfd_error_nonrepresentable_section);
   10568       return false;
   10569     }
   10570   return true;
   10571 }
   10572 
   10573 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   10574    allowing an unsatisfied unversioned symbol in the DSO to match a
   10575    versioned symbol that would normally require an explicit version.
   10576    We also handle the case that a DSO references a hidden symbol
   10577    which may be satisfied by a versioned symbol in another DSO.  */
   10578 
   10579 static bool
   10580 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   10581 				 elf_backend_data *bed,
   10582 				 struct elf_link_hash_entry *h)
   10583 {
   10584   bfd *abfd;
   10585   struct elf_link_loaded_list *loaded;
   10586 
   10587   if (!is_elf_hash_table (info->hash))
   10588     return false;
   10589 
   10590   /* Check indirect symbol.  */
   10591   while (h->root.type == bfd_link_hash_indirect)
   10592     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10593 
   10594   switch (h->root.type)
   10595     {
   10596     default:
   10597       abfd = NULL;
   10598       break;
   10599 
   10600     case bfd_link_hash_undefined:
   10601     case bfd_link_hash_undefweak:
   10602       abfd = h->root.u.undef.abfd;
   10603       if (abfd == NULL
   10604 	  || (abfd->flags & DYNAMIC) == 0
   10605 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   10606 	return false;
   10607       break;
   10608 
   10609     case bfd_link_hash_defined:
   10610     case bfd_link_hash_defweak:
   10611       abfd = h->root.u.def.section->owner;
   10612       break;
   10613 
   10614     case bfd_link_hash_common:
   10615       abfd = h->root.u.c.p->section->owner;
   10616       break;
   10617     }
   10618   BFD_ASSERT (abfd != NULL);
   10619 
   10620   for (loaded = elf_hash_table (info)->dyn_loaded;
   10621        loaded != NULL;
   10622        loaded = loaded->next)
   10623     {
   10624       bfd *input;
   10625       Elf_Internal_Shdr *hdr;
   10626       size_t symcount;
   10627       size_t extsymcount;
   10628       size_t extsymoff;
   10629       Elf_Internal_Shdr *versymhdr;
   10630       Elf_Internal_Sym *isym;
   10631       Elf_Internal_Sym *isymend;
   10632       Elf_Internal_Sym *isymbuf;
   10633       Elf_External_Versym *ever;
   10634       Elf_External_Versym *extversym;
   10635 
   10636       input = loaded->abfd;
   10637 
   10638       /* We check each DSO for a possible hidden versioned definition.  */
   10639       if (input == abfd
   10640 	  || elf_dynversym (input) == 0)
   10641 	continue;
   10642 
   10643       hdr = &elf_tdata (input)->dynsymtab_hdr;
   10644 
   10645       symcount = hdr->sh_size / bed->s->sizeof_sym;
   10646       if (elf_bad_symtab (input))
   10647 	{
   10648 	  extsymcount = symcount;
   10649 	  extsymoff = 0;
   10650 	}
   10651       else
   10652 	{
   10653 	  extsymcount = symcount - hdr->sh_info;
   10654 	  extsymoff = hdr->sh_info;
   10655 	}
   10656 
   10657       if (extsymcount == 0)
   10658 	continue;
   10659 
   10660       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   10661 				      NULL, NULL, NULL);
   10662       if (isymbuf == NULL)
   10663 	return false;
   10664 
   10665       /* Read in any version definitions.  */
   10666       versymhdr = &elf_tdata (input)->dynversym_hdr;
   10667       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   10668 	  || (extversym = (Elf_External_Versym *)
   10669 	      _bfd_malloc_and_read (input, versymhdr->sh_size,
   10670 				    versymhdr->sh_size)) == NULL)
   10671 	{
   10672 	  free (isymbuf);
   10673 	  return false;
   10674 	}
   10675 
   10676       ever = extversym + extsymoff;
   10677       isymend = isymbuf + extsymcount;
   10678       for (isym = isymbuf; isym < isymend; isym++, ever++)
   10679 	{
   10680 	  const char *name;
   10681 	  Elf_Internal_Versym iver;
   10682 	  unsigned short version_index;
   10683 
   10684 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   10685 	      || isym->st_shndx == SHN_UNDEF)
   10686 	    continue;
   10687 
   10688 	  name = bfd_elf_string_from_elf_section (input,
   10689 						  hdr->sh_link,
   10690 						  isym->st_name);
   10691 	  if (strcmp (name, h->root.root.string) != 0)
   10692 	    continue;
   10693 
   10694 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   10695 
   10696 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   10697 	      && !(h->def_regular
   10698 		   && h->forced_local))
   10699 	    {
   10700 	      /* If we have a non-hidden versioned sym, then it should
   10701 		 have provided a definition for the undefined sym unless
   10702 		 it is defined in a non-shared object and forced local.
   10703 	       */
   10704 	      abort ();
   10705 	    }
   10706 
   10707 	  version_index = iver.vs_vers & VERSYM_VERSION;
   10708 	  if (version_index == 1 || version_index == 2)
   10709 	    {
   10710 	      /* This is the base or first version.  We can use it.  */
   10711 	      free (extversym);
   10712 	      free (isymbuf);
   10713 	      return true;
   10714 	    }
   10715 	}
   10716 
   10717       free (extversym);
   10718       free (isymbuf);
   10719     }
   10720 
   10721   return false;
   10722 }
   10723 
   10724 /* Convert ELF common symbol TYPE.  */
   10725 
   10726 static int
   10727 elf_link_convert_common_type (struct bfd_link_info *info, int type)
   10728 {
   10729   /* Commom symbol can only appear in relocatable link.  */
   10730   if (!bfd_link_relocatable (info))
   10731     abort ();
   10732   switch (info->elf_stt_common)
   10733     {
   10734     case unchanged:
   10735       break;
   10736     case elf_stt_common:
   10737       type = STT_COMMON;
   10738       break;
   10739     case no_elf_stt_common:
   10740       type = STT_OBJECT;
   10741       break;
   10742     }
   10743   return type;
   10744 }
   10745 
   10746 /* Add an external symbol to the symbol table.  This is called from
   10747    the hash table traversal routine.  When generating a shared object,
   10748    we go through the symbol table twice.  The first time we output
   10749    anything that might have been forced to local scope in a version
   10750    script.  The second time we output the symbols that are still
   10751    global symbols.  */
   10752 
   10753 static bool
   10754 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   10755 {
   10756   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   10757   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   10758   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   10759   bool strip;
   10760   Elf_Internal_Sym sym;
   10761   asection *input_sec;
   10762   elf_backend_data *bed;
   10763   long indx;
   10764   int ret;
   10765   unsigned int type;
   10766 
   10767   /* Skip if base symbol doesn't match.  */
   10768   if (eoinfo->base_symbol != !!h->base_symbol)
   10769     return true;
   10770 
   10771   if (h->root.type == bfd_link_hash_warning)
   10772     {
   10773       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10774       if (h->root.type == bfd_link_hash_new)
   10775 	return true;
   10776     }
   10777 
   10778   /* Decide whether to output this symbol in this pass.  */
   10779   if (eoinfo->localsyms)
   10780     {
   10781       if (!h->forced_local)
   10782 	return true;
   10783     }
   10784   else
   10785     {
   10786       if (h->forced_local)
   10787 	return true;
   10788     }
   10789 
   10790   bed = get_elf_backend_data (flinfo->output_bfd);
   10791 
   10792   if (h->root.type == bfd_link_hash_undefined)
   10793     {
   10794       /* If we have an undefined symbol reference here then it must have
   10795 	 come from a shared library that is being linked in.  (Undefined
   10796 	 references in regular files have already been handled unless
   10797 	 they are in unreferenced sections which are removed by garbage
   10798 	 collection).  */
   10799       bool ignore_undef = false;
   10800 
   10801       /* Some symbols may be special in that the fact that they're
   10802 	 undefined can be safely ignored - let backend determine that.  */
   10803       if (bed->elf_backend_ignore_undef_symbol)
   10804 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   10805 
   10806       /* If we are reporting errors for this situation then do so now.  */
   10807       if (!ignore_undef
   10808 	  && h->ref_dynamic_nonweak
   10809 	  && (!h->ref_regular || flinfo->info->gc_sections)
   10810 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   10811 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   10812 	{
   10813 	  flinfo->info->callbacks->undefined_symbol
   10814 	    (flinfo->info, h->root.root.string,
   10815 	     h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
   10816 	     flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
   10817 	     && !flinfo->info->warn_unresolved_syms);
   10818 	}
   10819 
   10820       /* Strip a global symbol defined in a discarded section.  */
   10821       if (h->indx == -3)
   10822 	return true;
   10823     }
   10824 
   10825   /* We should also warn if a forced local symbol is referenced from
   10826      shared libraries.  */
   10827   if (bfd_link_executable (flinfo->info)
   10828       && h->forced_local
   10829       && h->ref_dynamic
   10830       && h->def_regular
   10831       && !h->dynamic_def
   10832       && h->ref_dynamic_nonweak
   10833       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   10834     {
   10835       bfd *def_bfd;
   10836       const char *msg;
   10837       struct elf_link_hash_entry *hi = h;
   10838 
   10839       /* Check indirect symbol.  */
   10840       while (hi->root.type == bfd_link_hash_indirect)
   10841 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   10842 
   10843       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   10844 	/* xgettext:c-format */
   10845 	msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
   10846       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   10847 	/* xgettext:c-format */
   10848 	msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
   10849       else
   10850 	/* xgettext:c-format */
   10851 	msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
   10852       def_bfd = flinfo->output_bfd;
   10853       if (hi->root.u.def.section != bfd_abs_section_ptr)
   10854 	def_bfd = hi->root.u.def.section->owner;
   10855       _bfd_error_handler (msg, flinfo->output_bfd,
   10856 			  h->root.root.string, def_bfd);
   10857       bfd_set_error (bfd_error_bad_value);
   10858       eoinfo->failed = true;
   10859       return false;
   10860     }
   10861 
   10862   /* We don't want to output symbols that have never been mentioned by
   10863      a regular file, or that we have been told to strip.  However, if
   10864      h->indx is set to -2, the symbol is used by a reloc and we must
   10865      output it.  */
   10866   strip = false;
   10867   if (h->indx == -2)
   10868     ;
   10869   else if ((h->def_dynamic
   10870 	    || h->ref_dynamic
   10871 	    || h->root.type == bfd_link_hash_new)
   10872 	   && !h->def_regular
   10873 	   && !h->ref_regular)
   10874     strip = true;
   10875   else if (flinfo->info->strip == strip_all)
   10876     strip = true;
   10877   else if (flinfo->info->strip == strip_some
   10878 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   10879 			       h->root.root.string, false, false) == NULL)
   10880     strip = true;
   10881   else if ((h->root.type == bfd_link_hash_defined
   10882 	    || h->root.type == bfd_link_hash_defweak)
   10883 	   && ((flinfo->info->strip_discarded
   10884 		&& discarded_section (h->root.u.def.section))
   10885 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   10886 		   && h->root.u.def.section->owner != NULL
   10887 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   10888     strip = true;
   10889   else if ((h->root.type == bfd_link_hash_undefined
   10890 	    || h->root.type == bfd_link_hash_undefweak)
   10891 	   && h->root.u.undef.abfd != NULL
   10892 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   10893     strip = true;
   10894 
   10895   /* Remember if this symbol should be stripped.  */
   10896   bool should_strip = strip;
   10897 
   10898   /* Strip undefined weak symbols link if they don't have relocation.  */
   10899   if (!strip)
   10900     strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
   10901 
   10902   type = h->type;
   10903 
   10904   /* If we're stripping it, and it's not a dynamic symbol, there's
   10905      nothing else to do.   However, if it is a forced local symbol or
   10906      an ifunc symbol we need to give the backend finish_dynamic_symbol
   10907      function a chance to make it dynamic.  */
   10908   if (strip
   10909       && h->dynindx == -1
   10910       && type != STT_GNU_IFUNC
   10911       && !h->forced_local)
   10912     return true;
   10913 
   10914   sym.st_value = 0;
   10915   sym.st_size = h->size;
   10916   sym.st_other = h->other;
   10917   switch (h->root.type)
   10918     {
   10919     default:
   10920     case bfd_link_hash_new:
   10921     case bfd_link_hash_warning:
   10922       abort ();
   10923       return false;
   10924 
   10925     case bfd_link_hash_undefined:
   10926     case bfd_link_hash_undefweak:
   10927       input_sec = bfd_und_section_ptr;
   10928       sym.st_shndx = SHN_UNDEF;
   10929       break;
   10930 
   10931     case bfd_link_hash_defined:
   10932     case bfd_link_hash_defweak:
   10933       {
   10934 	input_sec = h->root.u.def.section;
   10935 	if (input_sec->output_section != NULL)
   10936 	  {
   10937 	    sym.st_shndx =
   10938 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   10939 						 input_sec->output_section);
   10940 	    if (sym.st_shndx == SHN_BAD)
   10941 	      {
   10942 		_bfd_error_handler
   10943 		  /* xgettext:c-format */
   10944 		  (_("%pB: could not find output section %pA for input section %pA"),
   10945 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   10946 		bfd_set_error (bfd_error_nonrepresentable_section);
   10947 		eoinfo->failed = true;
   10948 		return false;
   10949 	      }
   10950 
   10951 	    /* ELF symbols in relocatable files are section relative,
   10952 	       but in nonrelocatable files they are virtual
   10953 	       addresses.  */
   10954 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   10955 	    if (!bfd_link_relocatable (flinfo->info))
   10956 	      {
   10957 		sym.st_value += input_sec->output_section->vma;
   10958 		if (h->type == STT_TLS)
   10959 		  {
   10960 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   10961 		    if (tls_sec != NULL)
   10962 		      sym.st_value -= tls_sec->vma;
   10963 		  }
   10964 	      }
   10965 	  }
   10966 	else
   10967 	  {
   10968 	    BFD_ASSERT (input_sec->owner == NULL
   10969 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   10970 	    sym.st_shndx = SHN_UNDEF;
   10971 	    input_sec = bfd_und_section_ptr;
   10972 	  }
   10973       }
   10974       break;
   10975 
   10976     case bfd_link_hash_common:
   10977       input_sec = h->root.u.c.p->section;
   10978       sym.st_shndx = bed->common_section_index (input_sec);
   10979       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   10980       break;
   10981 
   10982     case bfd_link_hash_indirect:
   10983       /* These symbols are created by symbol versioning.  They point
   10984 	 to the decorated version of the name.  For example, if the
   10985 	 symbol foo@@GNU_1.2 is the default, which should be used when
   10986 	 foo is used with no version, then we add an indirect symbol
   10987 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   10988 	 since the indirected symbol is already in the hash table.  */
   10989       return true;
   10990     }
   10991 
   10992   if (type == STT_COMMON || type == STT_OBJECT)
   10993     switch (h->root.type)
   10994       {
   10995       case bfd_link_hash_common:
   10996 	type = elf_link_convert_common_type (flinfo->info, type);
   10997 	break;
   10998       case bfd_link_hash_defined:
   10999       case bfd_link_hash_defweak:
   11000 	if (bed->common_definition (&sym))
   11001 	  type = elf_link_convert_common_type (flinfo->info, type);
   11002 	else
   11003 	  type = STT_OBJECT;
   11004 	break;
   11005       case bfd_link_hash_undefined:
   11006       case bfd_link_hash_undefweak:
   11007 	break;
   11008       default:
   11009 	abort ();
   11010       }
   11011 
   11012   if (h->forced_local)
   11013     {
   11014       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
   11015       /* Turn off visibility on local symbol.  */
   11016       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   11017     }
   11018   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   11019   else if (h->unique_global && h->def_regular)
   11020     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
   11021   else if (h->root.type == bfd_link_hash_undefweak
   11022 	   || h->root.type == bfd_link_hash_defweak)
   11023     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
   11024   else
   11025     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   11026   sym.st_target_internal = h->target_internal;
   11027 
   11028   /* Give the processor backend a chance to tweak the symbol value,
   11029      and also to finish up anything that needs to be done for this
   11030      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   11031      forced local syms when non-shared is due to a historical quirk.
   11032      STT_GNU_IFUNC symbol must go through PLT.  */
   11033   if ((h->type == STT_GNU_IFUNC
   11034        && h->def_regular
   11035        && !bfd_link_relocatable (flinfo->info))
   11036       || ((h->dynindx != -1
   11037 	   || h->forced_local)
   11038 	  && ((bfd_link_pic (flinfo->info)
   11039 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   11040 		   || h->root.type != bfd_link_hash_undefweak))
   11041 	      || !h->forced_local)
   11042 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   11043     {
   11044       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   11045 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   11046 	{
   11047 	  eoinfo->failed = true;
   11048 	  return false;
   11049 	}
   11050       /* If a symbol is in the dynamic symbol table and isn't a
   11051 	 should-strip symbol, also keep it in the symbol table.  */
   11052       if (!should_strip)
   11053 	strip = false;
   11054     }
   11055 
   11056   /* If we are marking the symbol as undefined, and there are no
   11057      non-weak references to this symbol from a regular object, then
   11058      mark the symbol as weak undefined; if there are non-weak
   11059      references, mark the symbol as strong.  We can't do this earlier,
   11060      because it might not be marked as undefined until the
   11061      finish_dynamic_symbol routine gets through with it.  */
   11062   if (sym.st_shndx == SHN_UNDEF
   11063       && h->ref_regular
   11064       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   11065 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   11066     {
   11067       int bindtype;
   11068       type = ELF_ST_TYPE (sym.st_info);
   11069 
   11070       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   11071       if (type == STT_GNU_IFUNC)
   11072 	type = STT_FUNC;
   11073 
   11074       if (h->ref_regular_nonweak)
   11075 	bindtype = STB_GLOBAL;
   11076       else
   11077 	bindtype = STB_WEAK;
   11078       sym.st_info = ELF_ST_INFO (bindtype, type);
   11079     }
   11080 
   11081   /* If this is a symbol defined in a dynamic library, don't use the
   11082      symbol size from the dynamic library.  Relinking an executable
   11083      against a new library may introduce gratuitous changes in the
   11084      executable's symbols if we keep the size.  */
   11085   if (sym.st_shndx == SHN_UNDEF
   11086       && !h->def_regular
   11087       && h->def_dynamic)
   11088     sym.st_size = 0;
   11089 
   11090   /* If a non-weak symbol with non-default visibility is not defined
   11091      locally, it is a fatal error.  */
   11092   if (!bfd_link_relocatable (flinfo->info)
   11093       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   11094       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   11095       && h->root.type == bfd_link_hash_undefined
   11096       && !h->def_regular)
   11097     {
   11098       const char *msg;
   11099 
   11100       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   11101 	/* xgettext:c-format */
   11102 	msg = _("%pB: protected symbol `%s' isn't defined");
   11103       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   11104 	/* xgettext:c-format */
   11105 	msg = _("%pB: internal symbol `%s' isn't defined");
   11106       else
   11107 	/* xgettext:c-format */
   11108 	msg = _("%pB: hidden symbol `%s' isn't defined");
   11109       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
   11110       bfd_set_error (bfd_error_bad_value);
   11111       eoinfo->failed = true;
   11112       return false;
   11113     }
   11114 
   11115   /* If this symbol should be put in the .dynsym section, then put it
   11116      there now.  We already know the symbol index.  We also fill in
   11117      the entry in the .hash section.  */
   11118   if (h->dynindx != -1
   11119       && elf_hash_table (flinfo->info)->dynamic_sections_created
   11120       && elf_hash_table (flinfo->info)->dynsym != NULL
   11121       && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
   11122     {
   11123       bfd_byte *esym;
   11124 
   11125       /* Since there is no version information in the dynamic string,
   11126 	 if there is no version info in symbol version section, we will
   11127 	 have a run-time problem if not linking executable, referenced
   11128 	 by shared library, or not bound locally.  */
   11129       if (h->verinfo.verdef == NULL
   11130 	  && (!bfd_link_executable (flinfo->info)
   11131 	      || h->ref_dynamic
   11132 	      || !h->def_regular))
   11133 	{
   11134 	  const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   11135 
   11136 	  if (p && p [1] != '\0')
   11137 	    {
   11138 	      _bfd_error_handler
   11139 		/* xgettext:c-format */
   11140 		(_("%pB: no symbol version section for versioned symbol `%s'"),
   11141 		 flinfo->output_bfd, h->root.root.string);
   11142 	      eoinfo->failed = true;
   11143 	      return false;
   11144 	    }
   11145 	}
   11146 
   11147       sym.st_name = h->dynstr_index;
   11148       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   11149 	      + h->dynindx * bed->s->sizeof_sym);
   11150       if (!check_dynsym (flinfo->output_bfd, &sym))
   11151 	{
   11152 	  eoinfo->failed = true;
   11153 	  return false;
   11154 	}
   11155 
   11156       /* Inform the linker of the addition of this symbol.  */
   11157 
   11158       if (flinfo->info->callbacks->ctf_new_dynsym)
   11159 	flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
   11160 
   11161       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   11162 
   11163       if (flinfo->hash_sec != NULL)
   11164 	{
   11165 	  size_t hash_entry_size;
   11166 	  bfd_byte *bucketpos;
   11167 	  bfd_vma chain;
   11168 	  size_t bucketcount;
   11169 	  size_t bucket;
   11170 
   11171 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   11172 	  bucket = h->u.elf_hash_value % bucketcount;
   11173 
   11174 	  hash_entry_size
   11175 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   11176 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   11177 		       + (bucket + 2) * hash_entry_size);
   11178 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   11179 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   11180 		   bucketpos);
   11181 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   11182 		   ((bfd_byte *) flinfo->hash_sec->contents
   11183 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   11184 	}
   11185 
   11186       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   11187 	{
   11188 	  Elf_Internal_Versym iversym;
   11189 	  Elf_External_Versym *eversym;
   11190 
   11191 	  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   11192 	    {
   11193 	      if (h->verinfo.verdef == NULL
   11194 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   11195 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   11196 		{
   11197 		  iversym.vs_vers = 1;
   11198 		  if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
   11199 		    /* Referenced symbol without ELF_VER_CHR has no
   11200 		       version.  */
   11201 		    iversym.vs_vers = 0;
   11202 		}
   11203 	      else
   11204 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   11205 	    }
   11206 	  else
   11207 	    {
   11208 	      if (h->verinfo.vertree == NULL)
   11209 		iversym.vs_vers = 1;
   11210 	      else
   11211 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   11212 	      if (flinfo->info->create_default_symver)
   11213 		iversym.vs_vers++;
   11214 
   11215 	      /* Turn on VERSYM_HIDDEN only if the hidden versioned
   11216 		 symbol is defined locally.  */
   11217 	      if (h->versioned == versioned_hidden && h->def_regular)
   11218 		iversym.vs_vers |= VERSYM_HIDDEN;
   11219 	    }
   11220 
   11221 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   11222 	  eversym += h->dynindx;
   11223 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   11224 	}
   11225     }
   11226 
   11227   /* If the symbol is undefined, and we didn't output it to .dynsym,
   11228      strip it from .symtab too.  Obviously we can't do this for
   11229      relocatable output or when needed for --emit-relocs.  */
   11230   else if (input_sec == bfd_und_section_ptr
   11231 	   && h->indx != -2
   11232 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
   11233 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
   11234 	   && !bfd_link_relocatable (flinfo->info))
   11235     return true;
   11236 
   11237   /* Also strip others that we couldn't earlier due to dynamic symbol
   11238      processing.  */
   11239   if (strip)
   11240     return true;
   11241   if ((input_sec->flags & SEC_EXCLUDE) != 0)
   11242     return true;
   11243 
   11244   /* Output a FILE symbol so that following locals are not associated
   11245      with the wrong input file.  We need one for forced local symbols
   11246      if we've seen more than one FILE symbol or when we have exactly
   11247      one FILE symbol but global symbols are present in a file other
   11248      than the one with the FILE symbol.  We also need one if linker
   11249      defined symbols are present.  In practice these conditions are
   11250      always met, so just emit the FILE symbol unconditionally.  */
   11251   if (eoinfo->localsyms
   11252       && !eoinfo->file_sym_done
   11253       && eoinfo->flinfo->filesym_count != 0)
   11254     {
   11255       Elf_Internal_Sym fsym;
   11256 
   11257       memset (&fsym, 0, sizeof (fsym));
   11258       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   11259       fsym.st_shndx = SHN_ABS;
   11260       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   11261 				      bfd_und_section_ptr, NULL))
   11262 	return false;
   11263 
   11264       eoinfo->file_sym_done = true;
   11265     }
   11266 
   11267   indx = bfd_get_symcount (flinfo->output_bfd);
   11268   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   11269 				   input_sec, h);
   11270   if (ret == 0)
   11271     {
   11272       eoinfo->failed = true;
   11273       return false;
   11274     }
   11275   else if (ret == 1)
   11276     h->indx = indx;
   11277   else if (h->indx == -2)
   11278     abort();
   11279 
   11280   return true;
   11281 }
   11282 
   11283 /* Return TRUE if special handling is done for relocs in SEC against
   11284    symbols defined in discarded sections.  */
   11285 
   11286 static bool
   11287 elf_section_ignore_discarded_relocs (asection *sec)
   11288 {
   11289   elf_backend_data *bed;
   11290 
   11291   switch (sec->sec_info_type)
   11292     {
   11293     case SEC_INFO_TYPE_STABS:
   11294     case SEC_INFO_TYPE_EH_FRAME:
   11295     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   11296     case SEC_INFO_TYPE_SFRAME:
   11297       return true;
   11298     default:
   11299       break;
   11300     }
   11301 
   11302   bed = get_elf_backend_data (sec->owner);
   11303   if (bed->elf_backend_ignore_discarded_relocs != NULL
   11304       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   11305     return true;
   11306 
   11307   return false;
   11308 }
   11309 
   11310 /* Return a mask saying how ld should treat relocations in SEC against
   11311    symbols defined in discarded sections.  If this function returns
   11312    COMPLAIN set, ld will issue a warning message.  If this function
   11313    returns PRETEND set, and the discarded section was link-once and the
   11314    same size as the kept link-once section, ld will pretend that the
   11315    symbol was actually defined in the kept section.  Otherwise ld will
   11316    zero the reloc (at least that is the intent, but some cooperation by
   11317    the target dependent code is needed, particularly for REL targets).  */
   11318 
   11319 unsigned int
   11320 _bfd_elf_default_action_discarded (asection *sec)
   11321 {
   11322   if (sec->flags & SEC_DEBUGGING)
   11323     return PRETEND;
   11324 
   11325   if (elf_section_type (sec) == SHT_GNU_SFRAME)
   11326     return 0;
   11327 
   11328   if (strncmp (sec->name, ".eh_frame", 9) == 0
   11329       && (sec->name[9] == 0 || sec->name[9] == '.'))
   11330     return 0;
   11331 
   11332   if (strcmp (sec->name, ".gcc_except_table") == 0)
   11333     return 0;
   11334 
   11335   return COMPLAIN | PRETEND;
   11336 }
   11337 
   11338 /* Find a match between a section and a member of a section group.  */
   11339 
   11340 static asection *
   11341 match_group_member (asection *sec, asection *group,
   11342 		    struct bfd_link_info *info)
   11343 {
   11344   asection *first = elf_next_in_group (group);
   11345   asection *s = first;
   11346 
   11347   while (s != NULL)
   11348     {
   11349       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   11350 	return s;
   11351 
   11352       s = elf_next_in_group (s);
   11353       if (s == first)
   11354 	break;
   11355     }
   11356 
   11357   return NULL;
   11358 }
   11359 
   11360 /* Check if the kept section of a discarded section SEC can be used
   11361    to replace it.  Return the replacement if it is OK.  Otherwise return
   11362    NULL.  */
   11363 
   11364 asection *
   11365 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   11366 {
   11367   asection *kept;
   11368 
   11369   kept = sec->kept_section;
   11370   if (kept != NULL)
   11371     {
   11372       if ((kept->flags & SEC_GROUP) != 0)
   11373 	kept = match_group_member (sec, kept, info);
   11374       if (kept != NULL)
   11375 	{
   11376 	  if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   11377 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size))
   11378 	    kept = NULL;
   11379 	  else
   11380 	    {
   11381 	      /* Get the real kept section.  */
   11382 	      asection *next;
   11383 	      for (next = kept->kept_section;
   11384 		   next != NULL;
   11385 		   next = next->kept_section)
   11386 		kept = next;
   11387 	    }
   11388 	}
   11389       sec->kept_section = kept;
   11390     }
   11391   return kept;
   11392 }
   11393 
   11394 /* Link an input file into the linker output file.  This function
   11395    handles all the sections and relocations of the input file at once.
   11396    This is so that we only have to read the local symbols once, and
   11397    don't have to keep them in memory.  */
   11398 
   11399 static bool
   11400 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   11401 {
   11402   int (*relocate_section)
   11403     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   11404      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   11405   bfd *output_bfd;
   11406   Elf_Internal_Shdr *symtab_hdr;
   11407   size_t locsymcount;
   11408   size_t extsymoff;
   11409   size_t num_sym;
   11410   Elf_Internal_Sym *isymbuf;
   11411   Elf_Internal_Sym *isym;
   11412   Elf_Internal_Sym *isymend;
   11413   long *pindex;
   11414   asection **ppsection;
   11415   asection *o;
   11416   elf_backend_data *bed;
   11417   struct elf_link_hash_entry **sym_hashes;
   11418   bfd_size_type address_size;
   11419   bfd_vma r_type_mask;
   11420   int r_sym_shift;
   11421   bool have_file_sym = false;
   11422 
   11423   output_bfd = flinfo->output_bfd;
   11424   bed = get_elf_backend_data (output_bfd);
   11425   relocate_section = bed->elf_backend_relocate_section;
   11426 
   11427   /* If this is a dynamic object, we don't want to do anything here:
   11428      we don't want the local symbols, and we don't want the section
   11429      contents.  */
   11430   if ((input_bfd->flags & DYNAMIC) != 0)
   11431     return true;
   11432 
   11433   symtab_hdr = &elf_symtab_hdr (input_bfd);
   11434   num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
   11435   if (elf_bad_symtab (input_bfd))
   11436     {
   11437       locsymcount = num_sym;
   11438       extsymoff = 0;
   11439     }
   11440   else
   11441     {
   11442       locsymcount = symtab_hdr->sh_info;
   11443       extsymoff = symtab_hdr->sh_info;
   11444     }
   11445 
   11446   /* Enable GNU OSABI features in the output BFD that are used in the input
   11447      BFD.  */
   11448   if (bed->elf_osabi == ELFOSABI_NONE
   11449       || bed->elf_osabi == ELFOSABI_GNU
   11450       || bed->elf_osabi == ELFOSABI_FREEBSD)
   11451     elf_tdata (output_bfd)->has_gnu_osabi
   11452       |= (elf_tdata (input_bfd)->has_gnu_osabi
   11453 	  & (bfd_link_relocatable (flinfo->info)
   11454 	     ? -1 : ~elf_gnu_osabi_retain));
   11455 
   11456   /* Read the local symbols.  */
   11457   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   11458   if (isymbuf == NULL && locsymcount != 0)
   11459     {
   11460       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   11461 				      flinfo->internal_syms,
   11462 				      flinfo->external_syms,
   11463 				      flinfo->locsym_shndx);
   11464       if (isymbuf == NULL)
   11465 	return false;
   11466     }
   11467 
   11468   /* Find local symbol sections and adjust values of symbols in
   11469      SEC_MERGE sections.  Write out those local symbols we know are
   11470      going into the output file.  */
   11471   isymend = PTR_ADD (isymbuf, locsymcount);
   11472   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   11473        isym < isymend;
   11474        isym++, pindex++, ppsection++)
   11475     {
   11476       asection *isec;
   11477       const char *name;
   11478       Elf_Internal_Sym osym;
   11479       long indx;
   11480       int ret;
   11481 
   11482       *pindex = -1;
   11483 
   11484       if (elf_bad_symtab (input_bfd))
   11485 	{
   11486 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   11487 	    {
   11488 	      *ppsection = NULL;
   11489 	      continue;
   11490 	    }
   11491 	}
   11492 
   11493       if (isym->st_shndx == SHN_UNDEF)
   11494 	isec = bfd_und_section_ptr;
   11495       else if (isym->st_shndx == SHN_ABS)
   11496 	isec = bfd_abs_section_ptr;
   11497       else if (isym->st_shndx == SHN_COMMON)
   11498 	isec = bfd_com_section_ptr;
   11499       else
   11500 	{
   11501 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   11502 	  if (isec == NULL)
   11503 	    {
   11504 	      /* Don't attempt to output symbols with st_shnx in the
   11505 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   11506 	      isec = bfd_und_section_ptr;
   11507 	    }
   11508 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   11509 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   11510 	    isym->st_value =
   11511 	      _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
   11512 	}
   11513 
   11514       *ppsection = isec;
   11515 
   11516       /* Don't output the first, undefined, symbol.  In fact, don't
   11517 	 output any undefined local symbol.  */
   11518       if (isec == bfd_und_section_ptr)
   11519 	continue;
   11520 
   11521       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   11522 	{
   11523 	  /* We never output section symbols.  Instead, we use the
   11524 	     section symbol of the corresponding section in the output
   11525 	     file.  */
   11526 	  continue;
   11527 	}
   11528 
   11529       /* If we are stripping all symbols, we don't want to output this
   11530 	 one.  */
   11531       if (flinfo->info->strip == strip_all)
   11532 	continue;
   11533 
   11534       /* If we are discarding all local symbols, we don't want to
   11535 	 output this one.  If we are generating a relocatable output
   11536 	 file, then some of the local symbols may be required by
   11537 	 relocs; we output them below as we discover that they are
   11538 	 needed.  */
   11539       if (flinfo->info->discard == discard_all)
   11540 	continue;
   11541 
   11542       /* If this symbol is defined in a section which we are
   11543 	 discarding, we don't need to keep it.  */
   11544       if (isym->st_shndx < SHN_LORESERVE
   11545 	  && (isec->output_section == NULL
   11546 	      || bfd_section_removed_from_list (output_bfd,
   11547 						isec->output_section)))
   11548 	continue;
   11549 
   11550       /* Get the name of the symbol.  */
   11551       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   11552 					      isym->st_name);
   11553       if (name == NULL)
   11554 	return false;
   11555 
   11556       /* See if we are discarding symbols with this name.  */
   11557       if ((flinfo->info->strip == strip_some
   11558 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
   11559 	       == NULL))
   11560 	  || (((flinfo->info->discard == discard_sec_merge
   11561 		&& (isec->flags & SEC_MERGE)
   11562 		&& !bfd_link_relocatable (flinfo->info))
   11563 	       || flinfo->info->discard == discard_l)
   11564 	      && bfd_is_local_label_name (input_bfd, name)))
   11565 	continue;
   11566 
   11567       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   11568 	{
   11569 	  if (input_bfd->lto_output)
   11570 	    /* -flto puts a temp file name here.  This means builds
   11571 	       are not reproducible.  Discard the symbol.  */
   11572 	    continue;
   11573 	  have_file_sym = true;
   11574 	  flinfo->filesym_count += 1;
   11575 	}
   11576       if (!have_file_sym)
   11577 	{
   11578 	  /* In the absence of debug info, bfd_find_nearest_line uses
   11579 	     FILE symbols to determine the source file for local
   11580 	     function symbols.  Provide a FILE symbol here if input
   11581 	     files lack such, so that their symbols won't be
   11582 	     associated with a previous input file.  It's not the
   11583 	     source file, but the best we can do.  */
   11584 	  const char *filename;
   11585 	  have_file_sym = true;
   11586 	  flinfo->filesym_count += 1;
   11587 	  memset (&osym, 0, sizeof (osym));
   11588 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   11589 	  osym.st_shndx = SHN_ABS;
   11590 	  if (input_bfd->lto_output)
   11591 	    filename = NULL;
   11592 	  else
   11593 	    filename = lbasename (bfd_get_filename (input_bfd));
   11594 	  if (!elf_link_output_symstrtab (flinfo, filename, &osym,
   11595 					  bfd_abs_section_ptr, NULL))
   11596 	    return false;
   11597 	}
   11598 
   11599       osym = *isym;
   11600 
   11601       /* Adjust the section index for the output file.  */
   11602       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11603 							 isec->output_section);
   11604       if (osym.st_shndx == SHN_BAD)
   11605 	return false;
   11606 
   11607       /* ELF symbols in relocatable files are section relative, but
   11608 	 in executable files they are virtual addresses.  Note that
   11609 	 this code assumes that all ELF sections have an associated
   11610 	 BFD section with a reasonable value for output_offset; below
   11611 	 we assume that they also have a reasonable value for
   11612 	 output_section.  Any special sections must be set up to meet
   11613 	 these requirements.  */
   11614       osym.st_value += isec->output_offset;
   11615       if (!bfd_link_relocatable (flinfo->info))
   11616 	{
   11617 	  osym.st_value += isec->output_section->vma;
   11618 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   11619 	    {
   11620 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   11621 	      if (elf_hash_table (flinfo->info)->tls_sec != NULL)
   11622 		osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   11623 	      else
   11624 		osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
   11625 					    STT_NOTYPE);
   11626 	    }
   11627 	}
   11628 
   11629       indx = bfd_get_symcount (output_bfd);
   11630       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   11631       if (ret == 0)
   11632 	return false;
   11633       else if (ret == 1)
   11634 	*pindex = indx;
   11635     }
   11636 
   11637   if (bed->s->arch_size == 32)
   11638     {
   11639       r_type_mask = 0xff;
   11640       r_sym_shift = 8;
   11641       address_size = 4;
   11642     }
   11643   else
   11644     {
   11645       r_type_mask = 0xffffffff;
   11646       r_sym_shift = 32;
   11647       address_size = 8;
   11648     }
   11649 
   11650   /* Relocate the contents of each section.  */
   11651   sym_hashes = elf_sym_hashes (input_bfd);
   11652   for (o = input_bfd->sections; o != NULL; o = o->next)
   11653     {
   11654       bfd_byte *contents;
   11655 
   11656       if (! o->linker_mark)
   11657 	{
   11658 	  /* This section was omitted from the link.  */
   11659 	  continue;
   11660 	}
   11661 
   11662       if (!flinfo->info->resolve_section_groups
   11663 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   11664 	{
   11665 	  /* Deal with the group signature symbol.  */
   11666 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   11667 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   11668 	  asection *osec = o->output_section;
   11669 
   11670 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
   11671 	  if (symndx >= locsymcount
   11672 	      || (elf_bad_symtab (input_bfd)
   11673 		  && flinfo->sections[symndx] == NULL))
   11674 	    {
   11675 	      struct elf_link_hash_entry *h;
   11676 
   11677 	      h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
   11678 						extsymoff, num_sym);
   11679 	      if (h == NULL)
   11680 		{
   11681 		  _bfd_error_handler
   11682 		    /* xgettext:c-format */
   11683 		    (_("error: %pB: unable to create group section symbol"),
   11684 		     input_bfd);
   11685 		  bfd_set_error (bfd_error_bad_value);
   11686 		  return false;
   11687 		}
   11688 
   11689 	      /* Arrange for symbol to be output.  */
   11690 	      h->indx = -2;
   11691 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   11692 	    }
   11693 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   11694 	    {
   11695 	      /* We'll use the output section target_index.  */
   11696 	      asection *sec = flinfo->sections[symndx]->output_section;
   11697 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   11698 	    }
   11699 	  else
   11700 	    {
   11701 	      if (flinfo->indices[symndx] == -1)
   11702 		{
   11703 		  /* Otherwise output the local symbol now.  */
   11704 		  Elf_Internal_Sym sym = isymbuf[symndx];
   11705 		  asection *sec = flinfo->sections[symndx]->output_section;
   11706 		  const char *name;
   11707 		  long indx;
   11708 		  int ret;
   11709 
   11710 		  name = bfd_elf_string_from_elf_section (input_bfd,
   11711 							  symtab_hdr->sh_link,
   11712 							  sym.st_name);
   11713 		  if (name == NULL)
   11714 		    return false;
   11715 
   11716 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11717 								    sec);
   11718 		  if (sym.st_shndx == SHN_BAD)
   11719 		    return false;
   11720 
   11721 		  sym.st_value += o->output_offset;
   11722 
   11723 		  indx = bfd_get_symcount (output_bfd);
   11724 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   11725 						   NULL);
   11726 		  if (ret == 0)
   11727 		    return false;
   11728 		  else if (ret == 1)
   11729 		    flinfo->indices[symndx] = indx;
   11730 		  else
   11731 		    abort ();
   11732 		}
   11733 	      elf_section_data (osec)->this_hdr.sh_info
   11734 		= flinfo->indices[symndx];
   11735 	    }
   11736 	}
   11737 
   11738       if ((o->flags & SEC_HAS_CONTENTS) == 0
   11739 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   11740 	continue;
   11741 
   11742       if ((o->flags & SEC_LINKER_CREATED) != 0)
   11743 	{
   11744 	  /* Section was created by bfd_elf_link_create_dynamic_sections()
   11745 	     or somesuch.  */
   11746 	  continue;
   11747 	}
   11748 
   11749       /* Get the contents of the section.  They have been cached by a
   11750 	 relaxation routine.  Note that o is a section in an input
   11751 	 file, so the contents field will not have been set by any of
   11752 	 the routines which work on output files.  */
   11753       if (elf_section_data (o)->this_hdr.contents != NULL)
   11754 	{
   11755 	  contents = elf_section_data (o)->this_hdr.contents;
   11756 	  if (bed->caches_rawsize
   11757 	      && o->rawsize != 0
   11758 	      && o->rawsize < o->size)
   11759 	    {
   11760 	      memcpy (flinfo->contents, contents, o->rawsize);
   11761 	      contents = flinfo->contents;
   11762 	    }
   11763 	}
   11764       else if (!(o->flags & SEC_RELOC)
   11765 	       && !bed->elf_backend_write_section
   11766 	       && o->sec_info_type == SEC_INFO_TYPE_MERGE)
   11767 	/* A MERGE section that has no relocations doesn't need the
   11768 	   contents anymore, they have been recorded earlier.  Except
   11769 	   if the backend has special provisions for writing sections.  */
   11770 	contents = NULL;
   11771       else
   11772 	{
   11773 	  contents = flinfo->contents;
   11774 	  if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
   11775 						     &contents))
   11776 	    return false;
   11777 	}
   11778 
   11779       if ((o->flags & SEC_RELOC) != 0)
   11780 	{
   11781 	  Elf_Internal_Rela *internal_relocs;
   11782 	  Elf_Internal_Rela *rel, *relend;
   11783 	  int action_discarded;
   11784 	  int ret;
   11785 
   11786 	  /* Get the swapped relocs.  */
   11787 	  internal_relocs
   11788 	    = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
   11789 					      flinfo->external_relocs,
   11790 					      flinfo->internal_relocs,
   11791 					      false);
   11792 	  if (internal_relocs == NULL
   11793 	      && o->reloc_count > 0)
   11794 	    return false;
   11795 
   11796 	  action_discarded = -1;
   11797 	  if (!elf_section_ignore_discarded_relocs (o))
   11798 	    action_discarded = (*bed->action_discarded) (o);
   11799 
   11800 	  /* Run through the relocs evaluating complex reloc symbols and
   11801 	     looking for relocs against symbols from discarded sections
   11802 	     or section symbols from removed link-once sections.
   11803 	     Complain about relocs against discarded sections.  Zero
   11804 	     relocs against removed link-once sections.  */
   11805 
   11806 	  rel = internal_relocs;
   11807 	  relend = rel + o->reloc_count;
   11808 	  for ( ; rel < relend; rel++)
   11809 	    {
   11810 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   11811 	      unsigned int s_type;
   11812 	      asection **ps, *sec;
   11813 	      struct elf_link_hash_entry *h = NULL;
   11814 	      const char *sym_name;
   11815 
   11816 	      if (r_symndx == STN_UNDEF)
   11817 		continue;
   11818 
   11819 	      if (r_symndx >= locsymcount
   11820 		  || (elf_bad_symtab (input_bfd)
   11821 		      && flinfo->sections[r_symndx] == NULL))
   11822 		{
   11823 		  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
   11824 						    extsymoff, num_sym);
   11825 
   11826 		  /* Badly formatted input files can contain relocs that
   11827 		     reference non-existant symbols.  Check here so that
   11828 		     we do not seg fault.  */
   11829 		  if (h == NULL)
   11830 		    {
   11831 		      _bfd_error_handler
   11832 			/* xgettext:c-format */
   11833 			(_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
   11834 			   "that references a non-existent global symbol"),
   11835 			 input_bfd, (uint64_t) rel->r_info, o);
   11836 		      bfd_set_error (bfd_error_bad_value);
   11837 		      return false;
   11838 		    }
   11839 
   11840 		  s_type = h->type;
   11841 
   11842 		  /* If a plugin symbol is referenced from a non-IR file,
   11843 		     mark the symbol as undefined.  Note that the
   11844 		     linker may attach linker created dynamic sections
   11845 		     to the plugin bfd.  Symbols defined in linker
   11846 		     created sections are not plugin symbols.  */
   11847 		  if ((h->root.non_ir_ref_regular
   11848 		       || h->root.non_ir_ref_dynamic)
   11849 		      && (h->root.type == bfd_link_hash_defined
   11850 			  || h->root.type == bfd_link_hash_defweak)
   11851 		      && (h->root.u.def.section->flags
   11852 			  & SEC_LINKER_CREATED) == 0
   11853 		      && h->root.u.def.section->owner != NULL
   11854 		      && (h->root.u.def.section->owner->flags
   11855 			  & BFD_PLUGIN) != 0)
   11856 		    {
   11857 		      h->root.type = bfd_link_hash_undefined;
   11858 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   11859 		    }
   11860 
   11861 		  ps = NULL;
   11862 		  if (h->root.type == bfd_link_hash_defined
   11863 		      || h->root.type == bfd_link_hash_defweak)
   11864 		    ps = &h->root.u.def.section;
   11865 
   11866 		  sym_name = h->root.root.string;
   11867 		}
   11868 	      else
   11869 		{
   11870 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   11871 
   11872 		  s_type = ELF_ST_TYPE (sym->st_info);
   11873 		  ps = &flinfo->sections[r_symndx];
   11874 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   11875 					       sym, *ps);
   11876 		}
   11877 
   11878 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   11879 		  && !bfd_link_relocatable (flinfo->info))
   11880 		{
   11881 		  bfd_vma val;
   11882 		  bfd_vma dot = (rel->r_offset
   11883 				 + o->output_offset + o->output_section->vma);
   11884 #ifdef DEBUG
   11885 		  printf ("Encountered a complex symbol!");
   11886 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   11887 			  bfd_get_filename (input_bfd), o->name,
   11888 			  (long) (rel - internal_relocs));
   11889 		  printf (" symbol: idx  %8.8lx, name %s\n",
   11890 			  r_symndx, sym_name);
   11891 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   11892 			  (unsigned long) rel->r_info,
   11893 			  (unsigned long) rel->r_offset);
   11894 #endif
   11895 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   11896 				    isymbuf, locsymcount, s_type == STT_SRELC))
   11897 		    return false;
   11898 
   11899 		  /* Symbol evaluated OK.  Update to absolute value.  */
   11900 		  if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
   11901 					 num_sym, r_symndx, val))
   11902 		    return false;
   11903 
   11904 		  continue;
   11905 		}
   11906 
   11907 	      if (action_discarded != -1 && ps != NULL)
   11908 		{
   11909 		  /* Complain if the definition comes from a
   11910 		     discarded section.  */
   11911 		  if ((sec = *ps) != NULL && discarded_section (sec))
   11912 		    {
   11913 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   11914 		      if (action_discarded & COMPLAIN)
   11915 			(*flinfo->info->callbacks->einfo)
   11916 			  /* xgettext:c-format */
   11917 			  (_("%X`%s' referenced in section `%pA' of %pB: "
   11918 			     "defined in discarded section `%pA' of %pB\n"),
   11919 			   sym_name, o, input_bfd, sec, sec->owner);
   11920 
   11921 		      /* Try to do the best we can to support buggy old
   11922 			 versions of gcc.  Pretend that the symbol is
   11923 			 really defined in the kept linkonce section.
   11924 			 FIXME: This is quite broken.  Modifying the
   11925 			 symbol here means we will be changing all later
   11926 			 uses of the symbol, not just in this section.  */
   11927 		      if (action_discarded & PRETEND)
   11928 			{
   11929 			  asection *kept;
   11930 
   11931 			  kept = _bfd_elf_check_kept_section (sec,
   11932 							      flinfo->info);
   11933 			  if (kept != NULL)
   11934 			    {
   11935 			      *ps = kept;
   11936 			      continue;
   11937 			    }
   11938 			}
   11939 		    }
   11940 		}
   11941 	    }
   11942 
   11943 	  /* Relocate the section by invoking a back end routine.
   11944 
   11945 	     The back end routine is responsible for adjusting the
   11946 	     section contents as necessary, and (if using Rela relocs
   11947 	     and generating a relocatable output file) adjusting the
   11948 	     reloc addend as necessary.
   11949 
   11950 	     The back end routine does not have to worry about setting
   11951 	     the reloc address or the reloc symbol index.
   11952 
   11953 	     The back end routine is given a pointer to the swapped in
   11954 	     internal symbols, and can access the hash table entries
   11955 	     for the external symbols via elf_sym_hashes (input_bfd).
   11956 
   11957 	     When generating relocatable output, the back end routine
   11958 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   11959 	     output symbol is going to be a section symbol
   11960 	     corresponding to the output section, which will require
   11961 	     the addend to be adjusted.  */
   11962 
   11963 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   11964 				     input_bfd, o, contents,
   11965 				     internal_relocs,
   11966 				     isymbuf,
   11967 				     flinfo->sections);
   11968 	  if (!ret)
   11969 	    return false;
   11970 
   11971 	  if (ret == 2
   11972 	      || bfd_link_relocatable (flinfo->info)
   11973 	      || flinfo->info->emitrelocations)
   11974 	    {
   11975 	      Elf_Internal_Rela *irela;
   11976 	      Elf_Internal_Rela *irelaend, *irelamid;
   11977 	      bfd_vma last_offset;
   11978 	      struct elf_link_hash_entry **rel_hash;
   11979 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   11980 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   11981 	      unsigned int next_erel;
   11982 	      bool rela_normal;
   11983 	      struct bfd_elf_section_data *esdi, *esdo;
   11984 
   11985 	      esdi = elf_section_data (o);
   11986 	      esdo = elf_section_data (o->output_section);
   11987 	      rela_normal = false;
   11988 
   11989 	      /* Adjust the reloc addresses and symbol indices.  */
   11990 
   11991 	      irela = internal_relocs;
   11992 	      irelaend = irela + o->reloc_count;
   11993 	      rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
   11994 	      /* We start processing the REL relocs, if any.  When we reach
   11995 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   11996 	      irelamid = irela;
   11997 	      if (esdi->rel.hdr != NULL)
   11998 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   11999 			     * bed->s->int_rels_per_ext_rel);
   12000 	      rel_hash_list = rel_hash;
   12001 	      rela_hash_list = NULL;
   12002 	      last_offset = o->output_offset;
   12003 	      if (!bfd_link_relocatable (flinfo->info))
   12004 		last_offset += o->output_section->vma;
   12005 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   12006 		{
   12007 		  unsigned long r_symndx;
   12008 		  asection *sec;
   12009 		  Elf_Internal_Sym sym;
   12010 
   12011 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   12012 		    {
   12013 		      rel_hash++;
   12014 		      next_erel = 0;
   12015 		    }
   12016 
   12017 		  if (irela == irelamid)
   12018 		    {
   12019 		      rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
   12020 		      rela_hash_list = rel_hash;
   12021 		      rela_normal = bed->rela_normal;
   12022 		    }
   12023 
   12024 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   12025 							     flinfo->info, o,
   12026 							     irela->r_offset);
   12027 		  if (irela->r_offset >= (bfd_vma) -2)
   12028 		    {
   12029 		      /* This is a reloc for a deleted entry or somesuch.
   12030 			 Turn it into an R_*_NONE reloc, at the same
   12031 			 offset as the last reloc.  elf_eh_frame.c and
   12032 			 bfd_elf_discard_info rely on reloc offsets
   12033 			 being ordered.  */
   12034 		      irela->r_offset = last_offset;
   12035 		      irela->r_info = 0;
   12036 		      irela->r_addend = 0;
   12037 		      continue;
   12038 		    }
   12039 
   12040 		  irela->r_offset += o->output_offset;
   12041 
   12042 		  /* Relocs in an executable have to be virtual addresses.  */
   12043 		  if (!bfd_link_relocatable (flinfo->info))
   12044 		    irela->r_offset += o->output_section->vma;
   12045 
   12046 		  last_offset = irela->r_offset;
   12047 
   12048 		  r_symndx = irela->r_info >> r_sym_shift;
   12049 		  if (r_symndx == STN_UNDEF)
   12050 		    continue;
   12051 
   12052 		  if (r_symndx >= locsymcount
   12053 		      || (elf_bad_symtab (input_bfd)
   12054 			  && flinfo->sections[r_symndx] == NULL))
   12055 		    {
   12056 		      struct elf_link_hash_entry *rh;
   12057 
   12058 		      /* This is a reloc against a global symbol.  We
   12059 			 have not yet output all the local symbols, so
   12060 			 we do not know the symbol index of any global
   12061 			 symbol.  We set the rel_hash entry for this
   12062 			 reloc to point to the global hash table entry
   12063 			 for this symbol.  The symbol index is then
   12064 			 set at the end of bfd_elf_final_link.  */
   12065 		      rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
   12066 							 extsymoff, num_sym);
   12067 		      if (rh == NULL)
   12068 			{
   12069 			  /* FIXME: Generate an error ?  */
   12070 			  continue;
   12071 			}
   12072 
   12073 		      /* Setting the index to -2 tells elf_link_output_extsym
   12074 			 that this symbol is used by a reloc.  */
   12075 		      BFD_ASSERT (rh->indx < 0);
   12076 		      rh->indx = -2;
   12077 		      *rel_hash = rh;
   12078 
   12079 		      continue;
   12080 		    }
   12081 
   12082 		  /* This is a reloc against a local symbol.  */
   12083 
   12084 		  *rel_hash = NULL;
   12085 		  sym = isymbuf[r_symndx];
   12086 		  sec = flinfo->sections[r_symndx];
   12087 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   12088 		    {
   12089 		      /* I suppose the backend ought to fill in the
   12090 			 section of any STT_SECTION symbol against a
   12091 			 processor specific section.  */
   12092 		      r_symndx = STN_UNDEF;
   12093 		      if (bfd_is_abs_section (sec))
   12094 			;
   12095 		      else if (sec == NULL || sec->owner == NULL)
   12096 			{
   12097 			  bfd_set_error (bfd_error_bad_value);
   12098 			  return false;
   12099 			}
   12100 		      else
   12101 			{
   12102 			  asection *osec = sec->output_section;
   12103 
   12104 			  /* If we have discarded a section, the output
   12105 			     section will be the absolute section.  In
   12106 			     case of discarded SEC_MERGE sections, use
   12107 			     the kept section.  relocate_section should
   12108 			     have already handled discarded linkonce
   12109 			     sections.  */
   12110 			  if (bfd_is_abs_section (osec)
   12111 			      && sec->kept_section != NULL
   12112 			      && sec->kept_section->output_section != NULL)
   12113 			    {
   12114 			      osec = sec->kept_section->output_section;
   12115 			      irela->r_addend -= osec->vma;
   12116 			    }
   12117 
   12118 			  if (!bfd_is_abs_section (osec))
   12119 			    {
   12120 			      r_symndx = osec->target_index;
   12121 			      if (r_symndx == STN_UNDEF)
   12122 				{
   12123 				  irela->r_addend += osec->vma;
   12124 				  osec = flinfo->info->callbacks->nearby_section
   12125 				    (output_bfd, osec, osec->vma);
   12126 				  irela->r_addend -= osec->vma;
   12127 				  r_symndx = osec->target_index;
   12128 				}
   12129 			    }
   12130 			}
   12131 
   12132 		      /* Adjust the addend according to where the
   12133 			 section winds up in the output section.  */
   12134 		      if (rela_normal)
   12135 			irela->r_addend += sec->output_offset;
   12136 		    }
   12137 		  else
   12138 		    {
   12139 		      if (flinfo->indices[r_symndx] == -1)
   12140 			{
   12141 			  unsigned long shlink;
   12142 			  const char *name;
   12143 			  asection *osec;
   12144 			  long indx;
   12145 
   12146 			  if (flinfo->info->strip == strip_all)
   12147 			    {
   12148 			      /* You can't do ld -r -s.  */
   12149 			      bfd_set_error (bfd_error_invalid_operation);
   12150 			      return false;
   12151 			    }
   12152 
   12153 			  /* This symbol was skipped earlier, but
   12154 			     since it is needed by a reloc, we
   12155 			     must output it now.  */
   12156 			  shlink = symtab_hdr->sh_link;
   12157 			  name = (bfd_elf_string_from_elf_section
   12158 				  (input_bfd, shlink, sym.st_name));
   12159 			  if (name == NULL)
   12160 			    return false;
   12161 
   12162 			  osec = sec->output_section;
   12163 			  sym.st_shndx =
   12164 			    _bfd_elf_section_from_bfd_section (output_bfd,
   12165 							       osec);
   12166 			  if (sym.st_shndx == SHN_BAD)
   12167 			    return false;
   12168 
   12169 			  sym.st_value += sec->output_offset;
   12170 			  if (!bfd_link_relocatable (flinfo->info))
   12171 			    {
   12172 			      sym.st_value += osec->vma;
   12173 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   12174 				{
   12175 				  struct elf_link_hash_table *htab
   12176 				    = elf_hash_table (flinfo->info);
   12177 
   12178 				  /* STT_TLS symbols are relative to PT_TLS
   12179 				     segment base.  */
   12180 				  if (htab->tls_sec != NULL)
   12181 				    sym.st_value -= htab->tls_sec->vma;
   12182 				  else
   12183 				    sym.st_info
   12184 				      = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
   12185 						     STT_NOTYPE);
   12186 				}
   12187 			    }
   12188 
   12189 			  indx = bfd_get_symcount (output_bfd);
   12190 			  ret = elf_link_output_symstrtab (flinfo, name,
   12191 							   &sym, sec,
   12192 							   NULL);
   12193 			  if (ret == 0)
   12194 			    return false;
   12195 			  else if (ret == 1)
   12196 			    flinfo->indices[r_symndx] = indx;
   12197 			  else
   12198 			    abort ();
   12199 			}
   12200 
   12201 		      r_symndx = flinfo->indices[r_symndx];
   12202 		    }
   12203 
   12204 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   12205 				   | (irela->r_info & r_type_mask));
   12206 		}
   12207 
   12208 	      /* Swap out the relocs.  */
   12209 	      input_rel_hdr = esdi->rel.hdr;
   12210 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   12211 		{
   12212 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   12213 						     input_rel_hdr,
   12214 						     internal_relocs,
   12215 						     rel_hash_list))
   12216 		    return false;
   12217 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   12218 				      * bed->s->int_rels_per_ext_rel);
   12219 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   12220 		}
   12221 
   12222 	      input_rela_hdr = esdi->rela.hdr;
   12223 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   12224 		{
   12225 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   12226 						     input_rela_hdr,
   12227 						     internal_relocs,
   12228 						     rela_hash_list))
   12229 		    return false;
   12230 		}
   12231 	    }
   12232 	}
   12233 
   12234       /* Write out the modified section contents.  */
   12235       if (bed->elf_backend_write_section
   12236 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   12237 						contents))
   12238 	{
   12239 	  /* Section written out.  */
   12240 	}
   12241       else switch (o->sec_info_type)
   12242 	{
   12243 	case SEC_INFO_TYPE_STABS:
   12244 	  if (! (_bfd_write_section_stabs
   12245 		 (output_bfd,
   12246 		  &elf_hash_table (flinfo->info)->stab_info, o, contents)))
   12247 	    return false;
   12248 	  break;
   12249 	case SEC_INFO_TYPE_MERGE:
   12250 	  if (! _bfd_write_merged_section (output_bfd, o))
   12251 	    return false;
   12252 	  break;
   12253 	case SEC_INFO_TYPE_EH_FRAME:
   12254 	  {
   12255 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   12256 						   o, contents))
   12257 	      return false;
   12258 	  }
   12259 	  break;
   12260 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   12261 	  {
   12262 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   12263 							 flinfo->info,
   12264 							 o, contents))
   12265 	      return false;
   12266 	  }
   12267 	  break;
   12268 	case SEC_INFO_TYPE_SFRAME:
   12269 	    {
   12270 	      /* Merge SFrame section into the SFrame encoder context of the
   12271 		 output_bfd's section.  The final .sframe output section will
   12272 		 be written out later.  */
   12273 	      if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
   12274 						  o, contents))
   12275 		return false;
   12276 	    }
   12277 	    break;
   12278 	default:
   12279 	  {
   12280 	    if (! (o->flags & SEC_EXCLUDE))
   12281 	      {
   12282 		file_ptr offset = (file_ptr) o->output_offset;
   12283 		bfd_size_type todo = o->size;
   12284 
   12285 		offset *= bfd_octets_per_byte (output_bfd, o);
   12286 
   12287 		if ((o->flags & SEC_ELF_REVERSE_COPY)
   12288 		    && o->size > address_size)
   12289 		  {
   12290 		    /* Reverse-copy input section to output.  */
   12291 
   12292 		    if ((o->size & (address_size - 1)) != 0
   12293 			|| (o->reloc_count != 0
   12294 			    && (o->size * bed->s->int_rels_per_ext_rel
   12295 				!= o->reloc_count * address_size)))
   12296 		      {
   12297 			_bfd_error_handler
   12298 			  /* xgettext:c-format */
   12299 			  (_("error: %pB: size of section %pA is not "
   12300 			     "multiple of address size"),
   12301 			   input_bfd, o);
   12302 			bfd_set_error (bfd_error_bad_value);
   12303 			return false;
   12304 		      }
   12305 
   12306 		    do
   12307 		      {
   12308 			todo -= address_size;
   12309 			if (! bfd_set_section_contents (output_bfd,
   12310 							o->output_section,
   12311 							contents + todo,
   12312 							offset,
   12313 							address_size))
   12314 			  return false;
   12315 			if (todo == 0)
   12316 			  break;
   12317 			offset += address_size;
   12318 		      }
   12319 		    while (1);
   12320 		  }
   12321 		else if (! bfd_set_section_contents (output_bfd,
   12322 						     o->output_section,
   12323 						     contents,
   12324 						     offset, todo))
   12325 		  return false;
   12326 	      }
   12327 	  }
   12328 	  break;
   12329 	}
   12330 
   12331       /* Munmap the section contents for each input section.  */
   12332       _bfd_elf_link_munmap_section_contents (o);
   12333     }
   12334 
   12335   return true;
   12336 }
   12337 
   12338 /* Generate a reloc when linking an ELF file.  This is a reloc
   12339    requested by the linker, and does not come from any input file.  This
   12340    is used to build constructor and destructor tables when linking
   12341    with -Ur.  */
   12342 
   12343 static bool
   12344 elf_reloc_link_order (bfd *output_bfd,
   12345 		      struct bfd_link_info *info,
   12346 		      asection *output_section,
   12347 		      struct bfd_link_order *link_order)
   12348 {
   12349   reloc_howto_type *howto;
   12350   long indx;
   12351   bfd_vma offset;
   12352   bfd_vma addend;
   12353   struct bfd_elf_section_reloc_data *reldata;
   12354   struct elf_link_hash_entry **rel_hash_ptr;
   12355   Elf_Internal_Shdr *rel_hdr;
   12356   elf_backend_data *bed = get_elf_backend_data (output_bfd);
   12357   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   12358   bfd_byte *erel;
   12359   unsigned int i;
   12360   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   12361 
   12362   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   12363   if (howto == NULL)
   12364     {
   12365       bfd_set_error (bfd_error_bad_value);
   12366       return false;
   12367     }
   12368 
   12369   addend = link_order->u.reloc.p->addend;
   12370 
   12371   if (esdo->rel.hdr)
   12372     reldata = &esdo->rel;
   12373   else if (esdo->rela.hdr)
   12374     reldata = &esdo->rela;
   12375   else
   12376     {
   12377       reldata = NULL;
   12378       BFD_ASSERT (0);
   12379     }
   12380 
   12381   /* Figure out the symbol index.  */
   12382   rel_hash_ptr = reldata->hashes + reldata->count;
   12383   if (link_order->type == bfd_section_reloc_link_order)
   12384     {
   12385       indx = link_order->u.reloc.p->u.section->target_index;
   12386       BFD_ASSERT (indx != 0);
   12387       *rel_hash_ptr = NULL;
   12388     }
   12389   else
   12390     {
   12391       struct elf_link_hash_entry *h;
   12392 
   12393       /* Treat a reloc against a defined symbol as though it were
   12394 	 actually against the section.  */
   12395       h = ((struct elf_link_hash_entry *)
   12396 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   12397 					 link_order->u.reloc.p->u.name,
   12398 					 false, false, true));
   12399       if (h != NULL
   12400 	  && (h->root.type == bfd_link_hash_defined
   12401 	      || h->root.type == bfd_link_hash_defweak))
   12402 	{
   12403 	  asection *section;
   12404 
   12405 	  section = h->root.u.def.section;
   12406 	  indx = section->output_section->target_index;
   12407 	  *rel_hash_ptr = NULL;
   12408 	  /* It seems that we ought to add the symbol value to the
   12409 	     addend here, but in practice it has already been added
   12410 	     because it was passed to constructor_callback.  */
   12411 	  addend += section->output_section->vma + section->output_offset;
   12412 	}
   12413       else if (h != NULL)
   12414 	{
   12415 	  /* Setting the index to -2 tells elf_link_output_extsym that
   12416 	     this symbol is used by a reloc.  */
   12417 	  h->indx = -2;
   12418 	  *rel_hash_ptr = h;
   12419 	  indx = 0;
   12420 	}
   12421       else
   12422 	{
   12423 	  (*info->callbacks->unattached_reloc)
   12424 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   12425 	  indx = 0;
   12426 	}
   12427     }
   12428 
   12429   /* If this is an inplace reloc, we must write the addend into the
   12430      object file.  */
   12431   if (howto->partial_inplace && addend != 0)
   12432     {
   12433       bfd_size_type size;
   12434       bfd_reloc_status_type rstat;
   12435       bfd_byte *buf;
   12436       bool ok;
   12437       const char *sym_name;
   12438       bfd_size_type octets;
   12439 
   12440       size = (bfd_size_type) bfd_get_reloc_size (howto);
   12441       buf = (bfd_byte *) bfd_zmalloc (size);
   12442       if (buf == NULL && size != 0)
   12443 	return false;
   12444       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   12445       switch (rstat)
   12446 	{
   12447 	case bfd_reloc_ok:
   12448 	  break;
   12449 
   12450 	default:
   12451 	case bfd_reloc_outofrange:
   12452 	  abort ();
   12453 
   12454 	case bfd_reloc_overflow:
   12455 	  if (link_order->type == bfd_section_reloc_link_order)
   12456 	    sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
   12457 	  else
   12458 	    sym_name = link_order->u.reloc.p->u.name;
   12459 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
   12460 					      howto->name, addend, NULL, NULL,
   12461 					      (bfd_vma) 0);
   12462 	  break;
   12463 	}
   12464 
   12465       octets = link_order->offset * bfd_octets_per_byte (output_bfd,
   12466 							 output_section);
   12467       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   12468 				     octets, size);
   12469       free (buf);
   12470       if (! ok)
   12471 	return false;
   12472     }
   12473 
   12474   /* The address of a reloc is relative to the section in a
   12475      relocatable file, and is a virtual address in an executable
   12476      file.  */
   12477   offset = link_order->offset;
   12478   if (! bfd_link_relocatable (info))
   12479     offset += output_section->vma;
   12480 
   12481   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   12482     {
   12483       irel[i].r_offset = offset;
   12484       irel[i].r_info = 0;
   12485       irel[i].r_addend = 0;
   12486     }
   12487   if (bed->s->arch_size == 32)
   12488     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   12489   else
   12490 #ifdef BFD64
   12491     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
   12492 #else
   12493     BFD_FAIL();
   12494 #endif
   12495 
   12496   rel_hdr = reldata->hdr;
   12497   erel = rel_hdr->contents;
   12498   if (rel_hdr->sh_type == SHT_REL)
   12499     {
   12500       erel += reldata->count * bed->s->sizeof_rel;
   12501       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   12502     }
   12503   else
   12504     {
   12505       irel[0].r_addend = addend;
   12506       erel += reldata->count * bed->s->sizeof_rela;
   12507       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   12508     }
   12509 
   12510   ++reldata->count;
   12511 
   12512   return true;
   12513 }
   12514 
   12515 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
   12516    Returns TRUE upon success, FALSE otherwise.  */
   12517 
   12518 static bool
   12519 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
   12520 {
   12521   bool ret = false;
   12522   bfd *implib_bfd;
   12523   elf_backend_data *bed;
   12524   flagword flags;
   12525   enum bfd_architecture arch;
   12526   unsigned int mach;
   12527   asymbol **sympp = NULL;
   12528   long symsize;
   12529   long symcount;
   12530   long src_count;
   12531   elf_symbol_type *osymbuf;
   12532   size_t amt;
   12533 
   12534   implib_bfd = info->out_implib_bfd;
   12535   bed = get_elf_backend_data (abfd);
   12536 
   12537   if (!bfd_set_format (implib_bfd, bfd_object))
   12538     return false;
   12539 
   12540   /* Use flag from executable but make it a relocatable object.  */
   12541   flags = bfd_get_file_flags (abfd);
   12542   flags &= ~HAS_RELOC;
   12543   if (!bfd_set_start_address (implib_bfd, 0)
   12544       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
   12545     return false;
   12546 
   12547   /* Copy architecture of output file to import library file.  */
   12548   arch = bfd_get_arch (abfd);
   12549   mach = bfd_get_mach (abfd);
   12550   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
   12551       && (abfd->target_defaulted
   12552 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
   12553     return false;
   12554 
   12555   /* Get symbol table size.  */
   12556   symsize = bfd_get_symtab_upper_bound (abfd);
   12557   if (symsize < 0)
   12558     return false;
   12559 
   12560   /* Read in the symbol table.  */
   12561   sympp = (asymbol **) bfd_malloc (symsize);
   12562   if (sympp == NULL)
   12563     return false;
   12564 
   12565   symcount = bfd_canonicalize_symtab (abfd, sympp);
   12566   if (symcount < 0)
   12567     goto free_sym_buf;
   12568 
   12569   /* Allow the BFD backend to copy any private header data it
   12570      understands from the output BFD to the import library BFD.  */
   12571   if (! bfd_copy_private_header_data (abfd, implib_bfd))
   12572     goto free_sym_buf;
   12573 
   12574   /* Filter symbols to appear in the import library.  */
   12575   if (bed->elf_backend_filter_implib_symbols)
   12576     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
   12577 						       symcount);
   12578   else
   12579     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
   12580   if (symcount == 0)
   12581     {
   12582       bfd_set_error (bfd_error_no_symbols);
   12583       _bfd_error_handler (_("%pB: no symbol found for import library"),
   12584 			  implib_bfd);
   12585       goto free_sym_buf;
   12586     }
   12587 
   12588 
   12589   /* Make symbols absolute.  */
   12590   amt = symcount * sizeof (*osymbuf);
   12591   osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
   12592   if (osymbuf == NULL)
   12593     goto free_sym_buf;
   12594 
   12595   for (src_count = 0; src_count < symcount; src_count++)
   12596     {
   12597       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
   12598 	      sizeof (*osymbuf));
   12599       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
   12600       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
   12601       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
   12602       osymbuf[src_count].internal_elf_sym.st_value =
   12603 	osymbuf[src_count].symbol.value;
   12604       sympp[src_count] = &osymbuf[src_count].symbol;
   12605     }
   12606 
   12607   bfd_set_symtab (implib_bfd, sympp, symcount);
   12608 
   12609   /* Allow the BFD backend to copy any private data it understands
   12610      from the output BFD to the import library BFD.  This is done last
   12611      to permit the routine to look at the filtered symbol table.  */
   12612   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
   12613     goto free_sym_buf;
   12614 
   12615   if (!bfd_close (implib_bfd))
   12616     goto free_sym_buf;
   12617 
   12618   ret = true;
   12619 
   12620  free_sym_buf:
   12621   free (sympp);
   12622   return ret;
   12623 }
   12624 
   12625 static void
   12626 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   12627 {
   12628   asection *o;
   12629 
   12630   if (flinfo->symstrtab != NULL)
   12631     _bfd_elf_strtab_free (flinfo->symstrtab);
   12632   free (flinfo->contents);
   12633   free (flinfo->external_relocs);
   12634   free (flinfo->internal_relocs);
   12635   free (flinfo->external_syms);
   12636   free (flinfo->locsym_shndx);
   12637   free (flinfo->internal_syms);
   12638   free (flinfo->indices);
   12639   free (flinfo->sections);
   12640   if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
   12641     free (flinfo->symshndxbuf);
   12642   for (o = obfd->sections; o != NULL; o = o->next)
   12643     {
   12644       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12645       free (esdo->rel.hashes);
   12646       free (esdo->rela.hashes);
   12647     }
   12648 }
   12649 
   12650 /* Do the final step of an ELF link.  */
   12651 
   12652 bool
   12653 _bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   12654 {
   12655   bool dynamic;
   12656   bool emit_relocs;
   12657   bfd *dynobj;
   12658   struct elf_final_link_info flinfo;
   12659   asection *o;
   12660   struct bfd_link_order *p;
   12661   bfd *sub;
   12662   bfd_size_type max_contents_size;
   12663   bfd_size_type max_external_reloc_size;
   12664   bfd_size_type max_internal_reloc_count;
   12665   bfd_size_type max_sym_count;
   12666   bfd_size_type max_sym_shndx_count;
   12667   Elf_Internal_Sym elfsym;
   12668   unsigned int i;
   12669   Elf_Internal_Shdr *symtab_hdr;
   12670   Elf_Internal_Shdr *symtab_shndx_hdr;
   12671   elf_backend_data *bed = get_elf_backend_data (abfd);
   12672   struct elf_outext_info eoinfo;
   12673   bool merged;
   12674   size_t relativecount;
   12675   size_t relr_entsize;
   12676   asection *reldyn = 0;
   12677   bfd_size_type amt;
   12678   struct elf_link_hash_table *htab = elf_hash_table (info);
   12679   bool sections_removed;
   12680 
   12681   if (!is_elf_hash_table (&htab->root))
   12682     return false;
   12683 
   12684   if (bfd_link_pic (info))
   12685     abfd->flags |= DYNAMIC;
   12686 
   12687   dynamic = htab->dynamic_sections_created;
   12688   dynobj = htab->dynobj;
   12689 
   12690   emit_relocs = (bfd_link_relocatable (info)
   12691 		 || info->emitrelocations);
   12692 
   12693   memset (&flinfo, 0, sizeof (flinfo));
   12694   flinfo.info = info;
   12695   flinfo.output_bfd = abfd;
   12696   flinfo.symstrtab = _bfd_elf_strtab_init ();
   12697   if (flinfo.symstrtab == NULL)
   12698     return false;
   12699 
   12700   if (! dynamic)
   12701     {
   12702       flinfo.hash_sec = NULL;
   12703       flinfo.symver_sec = NULL;
   12704     }
   12705   else
   12706     {
   12707       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   12708       /* Note that dynsym_sec can be NULL (on VMS).  */
   12709       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   12710       /* Note that it is OK if symver_sec is NULL.  */
   12711     }
   12712 
   12713   if (info->unique_symbol
   12714       && !bfd_hash_table_init (&flinfo.local_hash_table,
   12715 			       local_hash_newfunc,
   12716 			       sizeof (struct local_hash_entry)))
   12717     return false;
   12718 
   12719   /* The object attributes have been merged.  Remove the input
   12720      sections from the link, and set the contents of the output
   12721      section.  */
   12722   sections_removed = false;
   12723   const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   12724   for (o = abfd->sections; o != NULL; o = o->next)
   12725     {
   12726       bool remove_section = false;
   12727 
   12728       if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
   12729 	  || strcmp (o->name, ".gnu.attributes") == 0)
   12730 	{
   12731 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   12732 	    {
   12733 	      asection *input_section;
   12734 
   12735 	      if (p->type != bfd_indirect_link_order)
   12736 		continue;
   12737 	      input_section = p->u.indirect.section;
   12738 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   12739 		 elf_link_input_bfd ignores this section.  */
   12740 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   12741 	    }
   12742 
   12743 	  /* Skip this section later on.  */
   12744 	  o->map_head.link_order = NULL;
   12745 
   12746 	  bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
   12747 	  /* Once ELF headers have been written, the size of a section is
   12748 	     frozen. We need to set the size of the attribute section before
   12749 	     _bfd_elf_compute_section_file_positions.  */
   12750 	  bfd_set_section_size (o, attr_size);
   12751 	  if (attr_size > 0)
   12752 	    elf_obj_object_attributes (abfd) = o;
   12753 	  else
   12754 	    remove_section = true;
   12755 	}
   12756       else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
   12757 	{
   12758 	  /* Remove empty group section from linker output.  */
   12759 	  remove_section = true;
   12760 	}
   12761       if (remove_section)
   12762 	{
   12763 	  o->flags |= SEC_EXCLUDE;
   12764 	  bfd_section_list_remove (abfd, o);
   12765 	  abfd->section_count--;
   12766 	  sections_removed = true;
   12767 	}
   12768     }
   12769   if (sections_removed)
   12770     bfd_fix_excluded_sec_syms (info);
   12771 
   12772   /* Count up the number of relocations we will output for each output
   12773      section, so that we know the sizes of the reloc sections.  We
   12774      also figure out some maximum sizes.  */
   12775 #ifdef USE_MMAP
   12776   if (bed->use_mmap)
   12777     {
   12778       /* Mmap is used only if section size >= the minimum mmap section
   12779 	 size.  The initial max_contents_size value covers all sections
   12780 	 smaller than the minimum mmap section size.  It may be increased
   12781 	 for compressed or linker created sections or sections whose
   12782 	 rawsize != size.  max_external_reloc_size covers all relocation
   12783 	 sections smaller than the minimum mmap section size.  */
   12784       max_contents_size = _bfd_minimum_mmap_size;
   12785       max_external_reloc_size = _bfd_minimum_mmap_size;
   12786     }
   12787   else
   12788 #endif
   12789     {
   12790       max_contents_size = 0;
   12791       max_external_reloc_size = 0;
   12792     }
   12793   max_internal_reloc_count = 0;
   12794   max_sym_count = 0;
   12795   max_sym_shndx_count = 0;
   12796   merged = false;
   12797   for (o = abfd->sections; o != NULL; o = o->next)
   12798     {
   12799       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12800       bfd_size_type size_align = 1;
   12801 
   12802       if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   12803 	{
   12804 	  /* eh_frame editing can extend last FDE to cover padding
   12805 	     between one section and the next.  */
   12806 	  size_align = (((bfd_size_type) 1 << o->alignment_power)
   12807 			* bfd_octets_per_byte (abfd, o));
   12808 	}
   12809 
   12810       o->reloc_count = 0;
   12811 
   12812       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12813 	{
   12814 	  unsigned int reloc_count = 0;
   12815 	  unsigned int additional_reloc_count = 0;
   12816 	  struct bfd_elf_section_data *esdi = NULL;
   12817 
   12818 	  if (p->type == bfd_section_reloc_link_order
   12819 	      || p->type == bfd_symbol_reloc_link_order)
   12820 	    reloc_count = 1;
   12821 	  else if (p->type == bfd_indirect_link_order)
   12822 	    {
   12823 	      asection *sec;
   12824 
   12825 	      sec = p->u.indirect.section;
   12826 
   12827 	      /* Mark all sections which are to be included in the
   12828 		 link.  This will normally be every section.  We need
   12829 		 to do this so that we can identify any sections which
   12830 		 the linker has decided to not include.  */
   12831 	      sec->linker_mark = true;
   12832 
   12833 	      if (sec->flags & SEC_MERGE)
   12834 		merged = true;
   12835 
   12836 #ifdef USE_MMAP
   12837 	      /* Mmap is used only on non-compressed, non-linker created
   12838 		 sections whose rawsize == size.  */
   12839 	      if (!bed->use_mmap
   12840 		  || sec->compress_status != COMPRESS_SECTION_NONE
   12841 		  || (sec->flags & SEC_LINKER_CREATED) != 0
   12842 		  || sec->rawsize != sec->size)
   12843 #endif
   12844 		{
   12845 		  bfd_size_type size = (sec->rawsize > sec->size
   12846 					? sec->rawsize : sec->size);
   12847 		  size = (size + size_align - 1) & -size_align;
   12848 		  if (max_contents_size < size)
   12849 		    max_contents_size = size;
   12850 		}
   12851 
   12852 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   12853 		  && (sec->owner->flags & DYNAMIC) == 0)
   12854 		{
   12855 		  size_t sym_count;
   12856 
   12857 		  /* We are interested in just local symbols, not all
   12858 		     symbols.  */
   12859 		  if (elf_bad_symtab (sec->owner))
   12860 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   12861 				 / bed->s->sizeof_sym);
   12862 		  else
   12863 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   12864 
   12865 		  if (sym_count > max_sym_count)
   12866 		    max_sym_count = sym_count;
   12867 
   12868 		  if (sym_count > max_sym_shndx_count
   12869 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   12870 		    max_sym_shndx_count = sym_count;
   12871 
   12872 		  esdi = elf_section_data (sec);
   12873 
   12874 		  if (esdi->this_hdr.sh_type == SHT_REL
   12875 		      || esdi->this_hdr.sh_type == SHT_RELA)
   12876 		    /* Some backends use reloc_count in relocation sections
   12877 		       to count particular types of relocs.  Of course,
   12878 		       reloc sections themselves can't have relocations.  */
   12879 		    ;
   12880 		  else if (emit_relocs)
   12881 		    {
   12882 		      reloc_count = sec->reloc_count;
   12883 		      if (bed->elf_backend_count_additional_relocs)
   12884 			{
   12885 			  int c;
   12886 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
   12887 			  additional_reloc_count += c;
   12888 			}
   12889 		    }
   12890 		  else if (bed->elf_backend_count_relocs)
   12891 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   12892 
   12893 		  if ((sec->flags & SEC_RELOC) != 0)
   12894 		    {
   12895 #ifdef USE_MMAP
   12896 		      if (!bed->use_mmap)
   12897 #endif
   12898 			{
   12899 			  size_t ext_size = 0;
   12900 
   12901 			  if (esdi->rel.hdr != NULL)
   12902 			    ext_size = esdi->rel.hdr->sh_size;
   12903 			  if (esdi->rela.hdr != NULL)
   12904 			    ext_size += esdi->rela.hdr->sh_size;
   12905 
   12906 			  if (ext_size > max_external_reloc_size)
   12907 			    max_external_reloc_size = ext_size;
   12908 			}
   12909 		      if (sec->reloc_count > max_internal_reloc_count)
   12910 			max_internal_reloc_count = sec->reloc_count;
   12911 		    }
   12912 		}
   12913 	    }
   12914 
   12915 	  if (reloc_count == 0)
   12916 	    continue;
   12917 
   12918 	  reloc_count += additional_reloc_count;
   12919 	  o->reloc_count += reloc_count;
   12920 
   12921 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   12922 	    {
   12923 	      if (esdi->rel.hdr)
   12924 		{
   12925 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   12926 		  esdo->rel.count += additional_reloc_count;
   12927 		}
   12928 	      if (esdi->rela.hdr)
   12929 		{
   12930 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   12931 		  esdo->rela.count += additional_reloc_count;
   12932 		}
   12933 	    }
   12934 	  else
   12935 	    {
   12936 	      if (o->use_rela_p)
   12937 		esdo->rela.count += reloc_count;
   12938 	      else
   12939 		esdo->rel.count += reloc_count;
   12940 	    }
   12941 	}
   12942 
   12943       if (o->reloc_count > 0)
   12944 	o->flags |= SEC_RELOC;
   12945       else
   12946 	{
   12947 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   12948 	     set it (this is probably a bug) and if it is set
   12949 	     assign_section_numbers will create a reloc section.  */
   12950 	  o->flags &=~ SEC_RELOC;
   12951 	}
   12952 
   12953       /* If the SEC_ALLOC flag is not set, force the section VMA to
   12954 	 zero.  This is done in elf_fake_sections as well, but forcing
   12955 	 the VMA to 0 here will ensure that relocs against these
   12956 	 sections are handled correctly.  */
   12957       if ((o->flags & SEC_ALLOC) == 0
   12958 	  && ! o->user_set_vma)
   12959 	o->vma = 0;
   12960     }
   12961 
   12962   if (! bfd_link_relocatable (info) && merged)
   12963     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
   12964 
   12965   /* Figure out the file positions for everything but the symbol table
   12966      and the relocs.  We set symcount to force assign_section_numbers
   12967      to create a symbol table.  */
   12968   abfd->symcount = info->strip != strip_all || emit_relocs;
   12969   BFD_ASSERT (! abfd->output_has_begun);
   12970   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   12971     goto error_return;
   12972 
   12973   /* Set sizes, and assign file positions for reloc sections.  */
   12974   for (o = abfd->sections; o != NULL; o = o->next)
   12975     {
   12976       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12977       if ((o->flags & SEC_RELOC) != 0)
   12978 	{
   12979 	  if (esdo->rel.hdr
   12980 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   12981 	    goto error_return;
   12982 
   12983 	  if (esdo->rela.hdr
   12984 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   12985 	    goto error_return;
   12986 	}
   12987 
   12988       /* _bfd_elf_compute_section_file_positions makes temporary use
   12989 	 of target_index.  Reset it.  */
   12990       o->target_index = 0;
   12991 
   12992       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   12993 	 to count upwards while actually outputting the relocations.  */
   12994       esdo->rel.count = 0;
   12995       esdo->rela.count = 0;
   12996 
   12997       if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
   12998 	  && !bfd_section_is_ctf (o))
   12999 	{
   13000 	  /* Cache the section contents so that they can be compressed
   13001 	     later.  Use bfd_malloc since it will be freed by
   13002 	     bfd_compress_section_contents.  */
   13003 	  unsigned char *contents = esdo->this_hdr.contents;
   13004 	  if (contents != NULL)
   13005 	    abort ();
   13006 	  contents
   13007 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   13008 	  if (contents == NULL)
   13009 	    goto error_return;
   13010 	  esdo->this_hdr.contents = contents;
   13011 	}
   13012     }
   13013 
   13014   /* We have now assigned file positions for all the sections except .symtab,
   13015      .strtab, and non-loaded reloc and compressed debugging sections.  We start
   13016      the .symtab section at the current file position, and write directly to it.
   13017      We build the .strtab section in memory.  */
   13018   abfd->symcount = 0;
   13019   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13020   /* sh_name is set in prep_headers.  */
   13021   symtab_hdr->sh_type = SHT_SYMTAB;
   13022   /* sh_flags, sh_addr and sh_size all start off zero.  */
   13023   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   13024   /* sh_link is set in assign_section_numbers.  */
   13025   /* sh_info is set below.  */
   13026   /* sh_offset is set just below.  */
   13027   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   13028 
   13029   if (max_sym_count < 20)
   13030     max_sym_count = 20;
   13031   htab->strtabsize = max_sym_count;
   13032   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   13033   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
   13034   if (htab->strtab == NULL)
   13035     goto error_return;
   13036   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   13037   flinfo.symshndxbuf
   13038     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   13039        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   13040 
   13041   if (info->strip != strip_all || emit_relocs)
   13042     {
   13043       file_ptr off = elf_next_file_pos (abfd);
   13044 
   13045       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
   13046 
   13047       /* Note that at this point elf_next_file_pos (abfd) is
   13048 	 incorrect.  We do not yet know the size of the .symtab section.
   13049 	 We correct next_file_pos below, after we do know the size.  */
   13050 
   13051       /* Start writing out the symbol table.  The first symbol is always a
   13052 	 dummy symbol.  */
   13053       elfsym.st_value = 0;
   13054       elfsym.st_size = 0;
   13055       elfsym.st_info = 0;
   13056       elfsym.st_other = 0;
   13057       elfsym.st_shndx = SHN_UNDEF;
   13058       elfsym.st_target_internal = 0;
   13059       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   13060 				     bfd_und_section_ptr, NULL) != 1)
   13061 	goto error_return;
   13062 
   13063       /* Output a symbol for each section if asked or they are used for
   13064 	 relocs.  These symbols usually have no names.  We store the
   13065 	 index of each one in the index field of the section, so that
   13066 	 we can find it again when outputting relocs.  */
   13067 
   13068       if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
   13069 	{
   13070 	  bool name_local_sections
   13071 	    = (bed->elf_backend_name_local_section_symbols
   13072 	       && bed->elf_backend_name_local_section_symbols (abfd));
   13073 	  const char *name = NULL;
   13074 
   13075 	  elfsym.st_size = 0;
   13076 	  elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   13077 	  elfsym.st_other = 0;
   13078 	  elfsym.st_value = 0;
   13079 	  elfsym.st_target_internal = 0;
   13080 	  for (i = 1; i < elf_numsections (abfd); i++)
   13081 	    {
   13082 	      o = bfd_section_from_elf_index (abfd, i);
   13083 	      if (o != NULL)
   13084 		{
   13085 		  o->target_index = bfd_get_symcount (abfd);
   13086 		  elfsym.st_shndx = i;
   13087 		  if (!bfd_link_relocatable (info))
   13088 		    elfsym.st_value = o->vma;
   13089 		  if (name_local_sections)
   13090 		    name = o->name;
   13091 		  if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
   13092 						 NULL) != 1)
   13093 		    goto error_return;
   13094 		}
   13095 	    }
   13096 	}
   13097     }
   13098 
   13099   /* On some targets like Irix 5 the symbol split between local and global
   13100      ones recorded in the sh_info field needs to be done between section
   13101      and all other symbols.  */
   13102   if (bed->elf_backend_elfsym_local_is_section
   13103       && bed->elf_backend_elfsym_local_is_section (abfd))
   13104     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   13105 
   13106   /* Allocate some memory to hold information read in from the input
   13107      files.  */
   13108   if (max_contents_size != 0)
   13109     {
   13110       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   13111       if (flinfo.contents == NULL)
   13112 	goto error_return;
   13113     }
   13114 
   13115   if (max_external_reloc_size != 0)
   13116     {
   13117       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   13118       if (flinfo.external_relocs == NULL)
   13119 	goto error_return;
   13120     }
   13121 
   13122   if (max_internal_reloc_count != 0)
   13123     {
   13124       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
   13125       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   13126       if (flinfo.internal_relocs == NULL)
   13127 	goto error_return;
   13128     }
   13129 
   13130   if (max_sym_count != 0)
   13131     {
   13132       amt = max_sym_count * bed->s->sizeof_sym;
   13133       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   13134       if (flinfo.external_syms == NULL)
   13135 	goto error_return;
   13136 
   13137       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   13138       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   13139       if (flinfo.internal_syms == NULL)
   13140 	goto error_return;
   13141 
   13142       amt = max_sym_count * sizeof (long);
   13143       flinfo.indices = (long int *) bfd_malloc (amt);
   13144       if (flinfo.indices == NULL)
   13145 	goto error_return;
   13146 
   13147       amt = max_sym_count * sizeof (asection *);
   13148       flinfo.sections = (asection **) bfd_malloc (amt);
   13149       if (flinfo.sections == NULL)
   13150 	goto error_return;
   13151     }
   13152 
   13153   if (max_sym_shndx_count != 0)
   13154     {
   13155       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   13156       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   13157       if (flinfo.locsym_shndx == NULL)
   13158 	goto error_return;
   13159     }
   13160 
   13161   if (htab->tls_sec)
   13162     {
   13163       bfd_vma base, end = 0;  /* Both bytes.  */
   13164       asection *sec;
   13165 
   13166       for (sec = htab->tls_sec;
   13167 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   13168 	   sec = sec->next)
   13169 	{
   13170 	  bfd_size_type size = sec->size;
   13171 	  unsigned int opb = bfd_octets_per_byte (abfd, sec);
   13172 
   13173 	  if (size == 0
   13174 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   13175 	    {
   13176 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   13177 
   13178 	      if (ord != NULL)
   13179 		size = ord->offset * opb + ord->size;
   13180 	    }
   13181 	  end = sec->vma + size / opb;
   13182 	}
   13183       base = htab->tls_sec->vma;
   13184       /* Only align end of TLS section if static TLS doesn't have special
   13185 	 alignment requirements.  */
   13186       if (bed->static_tls_alignment == 1)
   13187 	end = align_power (end, htab->tls_sec->alignment_power);
   13188       htab->tls_size = end - base;
   13189     }
   13190 
   13191   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   13192     return false;
   13193 
   13194   /* Finish relative relocations here after regular symbol processing
   13195      is finished if DT_RELR is enabled.  */
   13196   if (info->enable_dt_relr
   13197       && bed->finish_relative_relocs
   13198       && !bed->finish_relative_relocs (info))
   13199     info->callbacks->fatal
   13200       (_("%P: %pB: failed to finish relative relocations\n"), abfd);
   13201 
   13202   /* Since ELF permits relocations to be against local symbols, we
   13203      must have the local symbols available when we do the relocations.
   13204      Since we would rather only read the local symbols once, and we
   13205      would rather not keep them in memory, we handle all the
   13206      relocations for a single input file at the same time.
   13207 
   13208      Unfortunately, there is no way to know the total number of local
   13209      symbols until we have seen all of them, and the local symbol
   13210      indices precede the global symbol indices.  This means that when
   13211      we are generating relocatable output, and we see a reloc against
   13212      a global symbol, we can not know the symbol index until we have
   13213      finished examining all the local symbols to see which ones we are
   13214      going to output.  To deal with this, we keep the relocations in
   13215      memory, and don't output them until the end of the link.  This is
   13216      an unfortunate waste of memory, but I don't see a good way around
   13217      it.  Fortunately, it only happens when performing a relocatable
   13218      link, which is not the common case.  FIXME: If keep_memory is set
   13219      we could write the relocs out and then read them again; I don't
   13220      know how bad the memory loss will be.  */
   13221 
   13222   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13223     sub->output_has_begun = false;
   13224   for (o = abfd->sections; o != NULL; o = o->next)
   13225     {
   13226       for (p = o->map_head.link_order; p != NULL; p = p->next)
   13227 	{
   13228 	  if (p->type == bfd_indirect_link_order
   13229 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   13230 		  == bfd_target_elf_flavour)
   13231 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   13232 	    {
   13233 	      if (! sub->output_has_begun)
   13234 		{
   13235 		  if (! elf_link_input_bfd (&flinfo, sub))
   13236 		    goto error_return;
   13237 		  sub->output_has_begun = true;
   13238 		}
   13239 	    }
   13240 	  else if (p->type == bfd_section_reloc_link_order
   13241 		   || p->type == bfd_symbol_reloc_link_order)
   13242 	    {
   13243 	      if (! elf_reloc_link_order (abfd, info, o, p))
   13244 		goto error_return;
   13245 	    }
   13246 	  else
   13247 	    {
   13248 	      if (! _bfd_default_link_order (abfd, info, o, p))
   13249 		{
   13250 		  if (p->type == bfd_indirect_link_order
   13251 		      && (bfd_get_flavour (sub)
   13252 			  == bfd_target_elf_flavour)
   13253 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   13254 			  != bed->s->elfclass))
   13255 		    {
   13256 		      const char *iclass, *oclass;
   13257 
   13258 		      switch (bed->s->elfclass)
   13259 			{
   13260 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
   13261 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
   13262 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
   13263 			default: abort ();
   13264 			}
   13265 
   13266 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
   13267 			{
   13268 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
   13269 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
   13270 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
   13271 			default: abort ();
   13272 			}
   13273 
   13274 		      bfd_set_error (bfd_error_wrong_format);
   13275 		      _bfd_error_handler
   13276 			/* xgettext:c-format */
   13277 			(_("%pB: file class %s incompatible with %s"),
   13278 			 sub, iclass, oclass);
   13279 		    }
   13280 
   13281 		  goto error_return;
   13282 		}
   13283 	    }
   13284 	}
   13285     }
   13286 
   13287   /* Free symbol buffer if needed.  */
   13288   if (!info->reduce_memory_overheads)
   13289     {
   13290       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13291 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
   13292 	  {
   13293 	    free (elf_tdata (sub)->symbuf);
   13294 	    elf_tdata (sub)->symbuf = NULL;
   13295 	  }
   13296     }
   13297 
   13298   /* Output any global symbols that got converted to local in a
   13299      version script or due to symbol visibility.  We do this in a
   13300      separate step since ELF requires all local symbols to appear
   13301      prior to any global symbols.  FIXME: We should only do this if
   13302      some global symbols were, in fact, converted to become local.
   13303      FIXME: Will this work correctly with the Irix 5 linker?  */
   13304   eoinfo.failed = false;
   13305   eoinfo.flinfo = &flinfo;
   13306   eoinfo.localsyms = true;
   13307   eoinfo.file_sym_done = false;
   13308   /* Output non-base symbols first.  */
   13309   eoinfo.base_symbol = false;
   13310   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   13311   if (eoinfo.failed)
   13312     goto error_return;
   13313 
   13314   /* If backend needs to output some local symbols not present in the hash
   13315      table, do it now.  */
   13316   if (bed->elf_backend_output_arch_local_syms)
   13317     {
   13318       if (! ((*bed->elf_backend_output_arch_local_syms)
   13319 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   13320 	goto error_return;
   13321     }
   13322 
   13323   /* That wrote out all the local symbols.  Finish up the symbol table
   13324      with the global symbols. Even if we want to strip everything we
   13325      can, we still need to deal with those global symbols that got
   13326      converted to local in a version script.  */
   13327 
   13328   /* The sh_info field records the index of the first non local symbol.  */
   13329   if (!symtab_hdr->sh_info)
   13330     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   13331 
   13332   if (dynamic
   13333       && htab->dynsym != NULL
   13334       && htab->dynsym->output_section != bfd_abs_section_ptr)
   13335     {
   13336       Elf_Internal_Sym sym;
   13337       bfd_byte *dynsym = htab->dynsym->contents;
   13338 
   13339       o = htab->dynsym->output_section;
   13340       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
   13341 
   13342       /* Write out the section symbols for the output sections.  */
   13343       if (bfd_link_pic (info)
   13344 	  || htab->is_relocatable_executable)
   13345 	{
   13346 	  asection *s;
   13347 
   13348 	  sym.st_size = 0;
   13349 	  sym.st_name = 0;
   13350 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   13351 	  sym.st_other = 0;
   13352 	  sym.st_target_internal = 0;
   13353 
   13354 	  for (s = abfd->sections; s != NULL; s = s->next)
   13355 	    {
   13356 	      int indx;
   13357 	      bfd_byte *dest;
   13358 	      long dynindx;
   13359 
   13360 	      dynindx = elf_section_data (s)->dynindx;
   13361 	      if (dynindx <= 0)
   13362 		continue;
   13363 	      indx = elf_section_data (s)->this_idx;
   13364 	      BFD_ASSERT (indx > 0);
   13365 	      sym.st_shndx = indx;
   13366 	      if (! check_dynsym (abfd, &sym))
   13367 		goto error_return;
   13368 	      sym.st_value = s->vma;
   13369 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   13370 
   13371 	      /* Inform the linker of the addition of this symbol.  */
   13372 
   13373 	      if (info->callbacks->ctf_new_dynsym)
   13374 		info->callbacks->ctf_new_dynsym (dynindx, &sym);
   13375 
   13376 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13377 	    }
   13378 	}
   13379 
   13380       /* Write out the local dynsyms.  */
   13381       if (htab->dynlocal)
   13382 	{
   13383 	  struct elf_link_local_dynamic_entry *e;
   13384 	  for (e = htab->dynlocal; e ; e = e->next)
   13385 	    {
   13386 	      asection *s;
   13387 	      bfd_byte *dest;
   13388 
   13389 	      /* Copy the internal symbol and turn off visibility.
   13390 		 Note that we saved a word of storage and overwrote
   13391 		 the original st_name with the dynstr_index.  */
   13392 	      sym = e->isym;
   13393 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   13394 	      sym.st_shndx = SHN_UNDEF;
   13395 
   13396 	      s = bfd_section_from_elf_index (e->input_bfd,
   13397 					      e->isym.st_shndx);
   13398 	      if (s != NULL
   13399 		  && s->output_section != NULL
   13400 		  && elf_section_data (s->output_section) != NULL)
   13401 		{
   13402 		  sym.st_shndx =
   13403 		    elf_section_data (s->output_section)->this_idx;
   13404 		  if (! check_dynsym (abfd, &sym))
   13405 		    goto error_return;
   13406 		  sym.st_value = (s->output_section->vma
   13407 				  + s->output_offset
   13408 				  + e->isym.st_value);
   13409 		}
   13410 
   13411 	      /* Inform the linker of the addition of this symbol.  */
   13412 
   13413 	      if (info->callbacks->ctf_new_dynsym)
   13414 		info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
   13415 
   13416 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   13417 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13418 	    }
   13419 	}
   13420     }
   13421 
   13422   /* We get the global symbols from the hash table.  */
   13423   eoinfo.failed = false;
   13424   eoinfo.localsyms = false;
   13425   eoinfo.flinfo = &flinfo;
   13426   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   13427   if (eoinfo.failed)
   13428     goto error_return;
   13429 
   13430   if (htab->has_base_symbols)
   13431     {
   13432       /* Output base symbols last in DT_HASH so that they will be picked
   13433 	 before non-base symbols at run-time.  */
   13434       eoinfo.base_symbol = true;
   13435       bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
   13436 			 &eoinfo);
   13437       if (eoinfo.failed)
   13438 	goto error_return;
   13439     }
   13440 
   13441   /* If backend needs to output some symbols not present in the hash
   13442      table, do it now.  */
   13443   if (bed->elf_backend_output_arch_syms
   13444       && (info->strip != strip_all || emit_relocs))
   13445     {
   13446       if (! ((*bed->elf_backend_output_arch_syms)
   13447 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   13448 	goto error_return;
   13449     }
   13450 
   13451   /* Finalize the .strtab section.  */
   13452   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   13453 
   13454   /* Swap out the .strtab section. */
   13455   if (!elf_link_swap_symbols_out (&flinfo))
   13456     goto error_return;
   13457   free (htab->strtab);
   13458   htab->strtab = NULL;
   13459 
   13460   /* Now we know the size of the symtab section.  */
   13461   if (bfd_get_symcount (abfd) > 0)
   13462     {
   13463       /* Finish up and write out the symbol string table (.strtab)
   13464 	 section.  */
   13465       Elf_Internal_Shdr *symstrtab_hdr = NULL;
   13466       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   13467 
   13468       if (elf_symtab_shndx_list (abfd))
   13469 	{
   13470 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   13471 
   13472 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   13473 	    {
   13474 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   13475 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   13476 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   13477 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   13478 	      symtab_shndx_hdr->sh_size = amt;
   13479 
   13480 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   13481 							       off, true, 0);
   13482 
   13483 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   13484 		  || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
   13485 		goto error_return;
   13486 	    }
   13487 	}
   13488 
   13489       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   13490       symstrtab_hdr->sh_type = SHT_STRTAB;
   13491       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   13492       symstrtab_hdr->sh_addralign = 1;
   13493 
   13494       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   13495 						       off, true, 0);
   13496       elf_next_file_pos (abfd) = off;
   13497 
   13498       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   13499 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   13500 	goto error_return;
   13501     }
   13502 
   13503   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
   13504     {
   13505       _bfd_error_handler (_("%pB: failed to generate import library"),
   13506 			  info->out_implib_bfd);
   13507       goto error_return;
   13508     }
   13509 
   13510   /* Adjust the relocs to have the correct symbol indices.  */
   13511   for (o = abfd->sections; o != NULL; o = o->next)
   13512     {
   13513       struct bfd_elf_section_data *esdo = elf_section_data (o);
   13514       bool sort;
   13515 
   13516       if ((o->flags & SEC_RELOC) == 0)
   13517 	continue;
   13518 
   13519       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   13520       if (esdo->rel.hdr != NULL
   13521 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
   13522 	goto error_return;
   13523       if (esdo->rela.hdr != NULL
   13524 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
   13525 	goto error_return;
   13526 
   13527       /* Set the reloc_count field to 0 to prevent write_relocs from
   13528 	 trying to swap the relocs out itself.  */
   13529       o->reloc_count = 0;
   13530     }
   13531 
   13532   relativecount = 0;
   13533   if (dynamic && info->combreloc && dynobj != NULL)
   13534     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   13535 
   13536   relr_entsize = 0;
   13537   if (htab->srelrdyn != NULL
   13538       && htab->srelrdyn->output_section != NULL
   13539       && htab->srelrdyn->size != 0)
   13540     {
   13541       asection *s = htab->srelrdyn->output_section;
   13542       relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
   13543       if (relr_entsize == 0)
   13544 	{
   13545 	  relr_entsize = bed->s->arch_size / 8;
   13546 	  elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
   13547 	}
   13548     }
   13549 
   13550   /* If we are linking against a dynamic object, or generating a
   13551      shared library, finish up the dynamic linking information.  */
   13552   if (dynamic)
   13553     {
   13554       bfd_byte *dyncon, *dynconend;
   13555 
   13556       /* Fix up .dynamic entries.  */
   13557       o = htab->dynamic;
   13558       BFD_ASSERT (o != NULL);
   13559 
   13560       dyncon = o->contents;
   13561       dynconend = PTR_ADD (o->contents, o->size);
   13562       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13563 	{
   13564 	  Elf_Internal_Dyn dyn;
   13565 	  const char *name;
   13566 	  unsigned int type;
   13567 	  bfd_size_type sh_size;
   13568 	  bfd_vma sh_addr;
   13569 
   13570 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13571 
   13572 	  switch (dyn.d_tag)
   13573 	    {
   13574 	    default:
   13575 	      continue;
   13576 	    case DT_NULL:
   13577 	      if (relativecount != 0)
   13578 		{
   13579 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   13580 		    {
   13581 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   13582 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   13583 		    }
   13584 		  if (dyn.d_tag != DT_NULL
   13585 		      && dynconend - dyncon >= bed->s->sizeof_dyn)
   13586 		    {
   13587 		      dyn.d_un.d_val = relativecount;
   13588 		      relativecount = 0;
   13589 		      break;
   13590 		    }
   13591 		  relativecount = 0;
   13592 		}
   13593 	      if (relr_entsize != 0)
   13594 		{
   13595 		  if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
   13596 		    {
   13597 		      asection *s = htab->srelrdyn;
   13598 		      dyn.d_tag = DT_RELR;
   13599 		      dyn.d_un.d_ptr
   13600 			= s->output_section->vma + s->output_offset;
   13601 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13602 		      dyncon += bed->s->sizeof_dyn;
   13603 
   13604 		      dyn.d_tag = DT_RELRSZ;
   13605 		      dyn.d_un.d_val = s->size;
   13606 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13607 		      dyncon += bed->s->sizeof_dyn;
   13608 
   13609 		      dyn.d_tag = DT_RELRENT;
   13610 		      dyn.d_un.d_val = relr_entsize;
   13611 		      relr_entsize = 0;
   13612 		      break;
   13613 		    }
   13614 		  relr_entsize = 0;
   13615 		}
   13616 	      continue;
   13617 
   13618 	    case DT_INIT:
   13619 	      name = info->init_function;
   13620 	      goto get_sym;
   13621 	    case DT_FINI:
   13622 	      name = info->fini_function;
   13623 	    get_sym:
   13624 	      {
   13625 		struct elf_link_hash_entry *h;
   13626 
   13627 		h = elf_link_hash_lookup (htab, name, false, false, true);
   13628 		if (h != NULL
   13629 		    && (h->root.type == bfd_link_hash_defined
   13630 			|| h->root.type == bfd_link_hash_defweak))
   13631 		  {
   13632 		    dyn.d_un.d_ptr = h->root.u.def.value;
   13633 		    o = h->root.u.def.section;
   13634 		    if (o->output_section != NULL)
   13635 		      dyn.d_un.d_ptr += (o->output_section->vma
   13636 					 + o->output_offset);
   13637 		    else
   13638 		      {
   13639 			/* The symbol is imported from another shared
   13640 			   library and does not apply to this one.  */
   13641 			dyn.d_un.d_ptr = 0;
   13642 		      }
   13643 		    break;
   13644 		  }
   13645 	      }
   13646 	      continue;
   13647 
   13648 	    case DT_PREINIT_ARRAYSZ:
   13649 	      name = ".preinit_array";
   13650 	      goto get_out_size;
   13651 	    case DT_INIT_ARRAYSZ:
   13652 	      name = ".init_array";
   13653 	      goto get_out_size;
   13654 	    case DT_FINI_ARRAYSZ:
   13655 	      name = ".fini_array";
   13656 	    get_out_size:
   13657 	      o = bfd_get_section_by_name (abfd, name);
   13658 	      if (o == NULL)
   13659 		{
   13660 		  _bfd_error_handler
   13661 		    (_("could not find section %s"), name);
   13662 		  goto error_return;
   13663 		}
   13664 	      if (o->size == 0)
   13665 		_bfd_error_handler
   13666 		  (_("warning: %s section has zero size"), name);
   13667 	      dyn.d_un.d_val = o->size;
   13668 	      break;
   13669 
   13670 	    case DT_PREINIT_ARRAY:
   13671 	      name = ".preinit_array";
   13672 	      goto get_out_vma;
   13673 	    case DT_INIT_ARRAY:
   13674 	      name = ".init_array";
   13675 	      goto get_out_vma;
   13676 	    case DT_FINI_ARRAY:
   13677 	      name = ".fini_array";
   13678 	    get_out_vma:
   13679 	      o = bfd_get_section_by_name (abfd, name);
   13680 	      goto do_vma;
   13681 
   13682 	    case DT_HASH:
   13683 	      name = ".hash";
   13684 	      goto get_vma;
   13685 	    case DT_GNU_HASH:
   13686 	      name = ".gnu.hash";
   13687 	      goto get_vma;
   13688 	    case DT_STRTAB:
   13689 	      name = ".dynstr";
   13690 	      goto get_vma;
   13691 	    case DT_SYMTAB:
   13692 	      name = ".dynsym";
   13693 	      goto get_vma;
   13694 	    case DT_VERDEF:
   13695 	      name = ".gnu.version_d";
   13696 	      goto get_vma;
   13697 	    case DT_VERNEED:
   13698 	      name = ".gnu.version_r";
   13699 	      goto get_vma;
   13700 	    case DT_VERSYM:
   13701 	      name = ".gnu.version";
   13702 	    get_vma:
   13703 	      o = bfd_get_linker_section (dynobj, name);
   13704 	    do_vma:
   13705 	      if (o == NULL || bfd_is_abs_section (o->output_section))
   13706 		{
   13707 		  _bfd_error_handler
   13708 		    (_("could not find section %s"), name);
   13709 		  goto error_return;
   13710 		}
   13711 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   13712 		{
   13713 		  _bfd_error_handler
   13714 		    (_("warning: section '%s' is being made into a note"), name);
   13715 		  bfd_set_error (bfd_error_nonrepresentable_section);
   13716 		  goto error_return;
   13717 		}
   13718 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
   13719 	      break;
   13720 
   13721 	    case DT_REL:
   13722 	    case DT_RELA:
   13723 	    case DT_RELSZ:
   13724 	    case DT_RELASZ:
   13725 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   13726 		type = SHT_REL;
   13727 	      else
   13728 		type = SHT_RELA;
   13729 	      sh_size = 0;
   13730 	      sh_addr = 0;
   13731 	      for (i = 1; i < elf_numsections (abfd); i++)
   13732 		{
   13733 		  Elf_Internal_Shdr *hdr;
   13734 
   13735 		  hdr = elf_elfsections (abfd)[i];
   13736 		  if (hdr->sh_type == type
   13737 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   13738 		    {
   13739 		      sh_size += hdr->sh_size;
   13740 		      if (sh_addr == 0
   13741 			  || sh_addr > hdr->sh_addr)
   13742 			sh_addr = hdr->sh_addr;
   13743 		    }
   13744 		}
   13745 
   13746 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
   13747 		{
   13748 		  unsigned int opb = bfd_octets_per_byte (abfd, o);
   13749 
   13750 		  /* Don't count procedure linkage table relocs in the
   13751 		     overall reloc count.  */
   13752 		  sh_size -= htab->srelplt->size;
   13753 		  if (sh_size == 0)
   13754 		    /* If the size is zero, make the address zero too.
   13755 		       This is to avoid a glibc bug.  If the backend
   13756 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
   13757 		       zero, then we'll put DT_RELA at the end of
   13758 		       DT_JMPREL.  glibc will interpret the end of
   13759 		       DT_RELA matching the end of DT_JMPREL as the
   13760 		       case where DT_RELA includes DT_JMPREL, and for
   13761 		       LD_BIND_NOW will decide that processing DT_RELA
   13762 		       will process the PLT relocs too.  Net result:
   13763 		       No PLT relocs applied.  */
   13764 		    sh_addr = 0;
   13765 
   13766 		  /* If .rela.plt is the first .rela section, exclude
   13767 		     it from DT_RELA.  */
   13768 		  else if (sh_addr == (htab->srelplt->output_section->vma
   13769 				       + htab->srelplt->output_offset) * opb)
   13770 		    sh_addr += htab->srelplt->size;
   13771 		}
   13772 
   13773 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   13774 		dyn.d_un.d_val = sh_size;
   13775 	      else
   13776 		dyn.d_un.d_ptr = sh_addr;
   13777 	      break;
   13778 	    }
   13779 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13780 	}
   13781     }
   13782 
   13783   /* If we have created any dynamic sections, then output them.  */
   13784   if (dynobj != NULL)
   13785     {
   13786       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info,
   13787 							 flinfo.contents))
   13788 	goto error_return;
   13789 
   13790       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   13791       if (bfd_link_textrel_check (info)
   13792 	  && (o = htab->dynamic) != NULL
   13793 	  && o->size != 0)
   13794 	{
   13795 	  bfd_byte *dyncon, *dynconend;
   13796 
   13797 	  dyncon = o->contents;
   13798 	  dynconend = o->contents + o->size;
   13799 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13800 	    {
   13801 	      Elf_Internal_Dyn dyn;
   13802 
   13803 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13804 
   13805 	      if (dyn.d_tag == DT_TEXTREL)
   13806 		{
   13807 		  if (info->textrel_check == textrel_check_error)
   13808 		    info->callbacks->einfo
   13809 		      (_("%P%X: read-only segment has dynamic relocations\n"));
   13810 		  else if (bfd_link_dll (info))
   13811 		    info->callbacks->einfo
   13812 		      (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
   13813 		  else if (bfd_link_pde (info))
   13814 		    info->callbacks->einfo
   13815 		      (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
   13816 		  else
   13817 		    info->callbacks->einfo
   13818 		      (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
   13819 		  break;
   13820 		}
   13821 	    }
   13822 	}
   13823 
   13824       for (o = dynobj->sections; o != NULL; o = o->next)
   13825 	{
   13826 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   13827 	      || o->size == 0
   13828 	      || o->output_section == bfd_abs_section_ptr)
   13829 	    continue;
   13830 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   13831 	    {
   13832 	      /* At this point, we are only interested in sections
   13833 		 created by bfd_elf_link_create_dynamic_sections().  */
   13834 	      continue;
   13835 	    }
   13836 	  if (htab->stab_info.stabstr == o)
   13837 	    continue;
   13838 	  if (htab->eh_info.hdr_sec == o)
   13839 	    continue;
   13840 	  if (strcmp (o->name, ".dynstr") != 0)
   13841 	    {
   13842 	      bfd_size_type octets = ((file_ptr) o->output_offset
   13843 				      * bfd_octets_per_byte (abfd, o));
   13844 	      if (!bfd_set_section_contents (abfd, o->output_section,
   13845 					     o->contents, octets, o->size))
   13846 		goto error_return;
   13847 	    }
   13848 	  else
   13849 	    {
   13850 	      /* The contents of the .dynstr section are actually in a
   13851 		 stringtab.  */
   13852 	      file_ptr off;
   13853 
   13854 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   13855 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   13856 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
   13857 		goto error_return;
   13858 	    }
   13859 	}
   13860     }
   13861 
   13862   if (!info->resolve_section_groups)
   13863     {
   13864       bool failed = false;
   13865 
   13866       BFD_ASSERT (bfd_link_relocatable (info));
   13867       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   13868       if (failed)
   13869 	goto error_return;
   13870     }
   13871 
   13872   /* If we have optimized stabs strings, output them.  */
   13873   if (htab->stab_info.stabstr != NULL)
   13874     {
   13875       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
   13876 	goto error_return;
   13877     }
   13878 
   13879   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   13880     goto error_return;
   13881 
   13882   if (! _bfd_elf_write_section_sframe (abfd, info))
   13883     goto error_return;
   13884 
   13885   if (! _bfd_elf_write_section_object_attributes (abfd, info))
   13886     goto error_ret2;
   13887 
   13888   if (info->callbacks->emit_ctf)
   13889       info->callbacks->emit_ctf ();
   13890 
   13891   elf_final_link_free (abfd, &flinfo);
   13892 
   13893   if (info->unique_symbol)
   13894     bfd_hash_table_free (&flinfo.local_hash_table);
   13895   return true;
   13896 
   13897  error_return:
   13898   free (htab->strtab);
   13899   htab->strtab = NULL;
   13900   elf_final_link_free (abfd, &flinfo);
   13901  error_ret2:
   13902   if (info->unique_symbol)
   13903     bfd_hash_table_free (&flinfo.local_hash_table);
   13904   return false;
   13905 }
   13906 
   13907 /* Initialize COOKIE for input bfd ABFD.  */
   13909 
   13910 static bool
   13911 init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   13912 {
   13913   Elf_Internal_Shdr *symtab_hdr;
   13914   elf_backend_data *bed;
   13915 
   13916   bed = get_elf_backend_data (abfd);
   13917   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13918 
   13919   cookie->abfd = abfd;
   13920   cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
   13921   if (elf_bad_symtab (abfd))
   13922     {
   13923       cookie->locsymcount = cookie->num_sym;
   13924       cookie->extsymoff = 0;
   13925     }
   13926   else
   13927     {
   13928       cookie->locsymcount = symtab_hdr->sh_info;
   13929       cookie->extsymoff = symtab_hdr->sh_info;
   13930     }
   13931 
   13932   if (bed->s->arch_size == 32)
   13933     cookie->r_sym_shift = 8;
   13934   else
   13935     cookie->r_sym_shift = 32;
   13936 
   13937   return true;
   13938 }
   13939 
   13940 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   13941 
   13942 static void
   13943 fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
   13944 		   bfd *abfd ATTRIBUTE_UNUSED)
   13945 {
   13946 }
   13947 
   13948 /* Initialize the relocation information in COOKIE for input section SEC
   13949    of input bfd ABFD.  */
   13950 
   13951 static bool
   13952 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13953 			struct bfd_link_info *info, bfd *abfd,
   13954 			asection *sec, bool keep_memory)
   13955 {
   13956   if (sec->reloc_count == 0)
   13957     {
   13958       cookie->rels = NULL;
   13959       cookie->relend = NULL;
   13960     }
   13961   else
   13962     {
   13963       cookie->rels = _bfd_elf_link_info_read_relocs
   13964 	(abfd, info, sec, NULL, NULL,
   13965 	 keep_memory || _bfd_elf_link_keep_memory (info));
   13966       if (cookie->rels == NULL)
   13967 	return false;
   13968       cookie->rel = cookie->rels;
   13969       cookie->relend = cookie->rels + sec->reloc_count;
   13970     }
   13971   cookie->rel = cookie->rels;
   13972   return true;
   13973 }
   13974 
   13975 /* Free the memory allocated by init_reloc_cookie_rels,
   13976    if appropriate.  */
   13977 
   13978 static void
   13979 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13980 			asection *sec)
   13981 {
   13982   if (elf_section_data (sec)->relocs != cookie->rels)
   13983     free (cookie->rels);
   13984 }
   13985 
   13986 /* Initialize the whole of COOKIE for input section SEC.  */
   13987 
   13988 static bool
   13989 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13990 			       struct bfd_link_info *info,
   13991 			       asection *sec, bool keep_memory)
   13992 {
   13993   if (!init_reloc_cookie (cookie, sec->owner))
   13994     goto error1;
   13995   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
   13996 			       keep_memory))
   13997     goto error2;
   13998   return true;
   13999 
   14000  error2:
   14001   fini_reloc_cookie (cookie, sec->owner);
   14002  error1:
   14003   return false;
   14004 }
   14005 
   14006 /* Free the memory allocated by init_reloc_cookie_for_section,
   14007    if appropriate.  */
   14008 
   14009 static void
   14010 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   14011 			       asection *sec)
   14012 {
   14013   fini_reloc_cookie_rels (cookie, sec);
   14014   fini_reloc_cookie (cookie, sec->owner);
   14015 }
   14016 
   14017 /* Garbage collect unused sections.  */
   14019 
   14020 /* Default gc_mark_hook.  */
   14021 
   14022 asection *
   14023 _bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
   14024 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14025 		       struct elf_reloc_cookie *cookie,
   14026 		       struct elf_link_hash_entry *h,
   14027 		       unsigned int symndx)
   14028 {
   14029   if (h == NULL)
   14030     return _bfd_get_local_sym_section (cookie, symndx);
   14031 
   14032   switch (h->root.type)
   14033     {
   14034     case bfd_link_hash_defined:
   14035     case bfd_link_hash_defweak:
   14036       return h->root.u.def.section;
   14037 
   14038     case bfd_link_hash_common:
   14039       return h->root.u.c.p->section;
   14040 
   14041     default:
   14042       return NULL;
   14043     }
   14044 }
   14045 
   14046 /* Return the debug definition section.  */
   14047 
   14048 static asection *
   14049 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
   14050 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14051 			   struct elf_reloc_cookie *cookie,
   14052 			   struct elf_link_hash_entry *h,
   14053 			   unsigned int symndx)
   14054 {
   14055   if (h != NULL)
   14056     {
   14057       /* Return the global debug definition section.  */
   14058       if ((h->root.type == bfd_link_hash_defined
   14059 	   || h->root.type == bfd_link_hash_defweak)
   14060 	  && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
   14061 	return h->root.u.def.section;
   14062     }
   14063   else
   14064     {
   14065       /* Return the local debug definition section.  */
   14066       asection *isec = _bfd_get_local_sym_section (cookie, symndx);
   14067       if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
   14068 	return isec;
   14069     }
   14070 
   14071   return NULL;
   14072 }
   14073 
   14074 /* COOKIE->rel describes a relocation against section SEC, which is
   14075    a section we've decided to keep.  Return the section that contains
   14076    the relocation symbol, or NULL if no section contains it.  */
   14077 
   14078 asection *
   14079 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   14080 		       elf_gc_mark_hook_fn gc_mark_hook,
   14081 		       struct elf_reloc_cookie *cookie,
   14082 		       bool *start_stop)
   14083 {
   14084   unsigned long r_symndx;
   14085   struct elf_link_hash_entry *h, *hw;
   14086 
   14087   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   14088   if (r_symndx == STN_UNDEF)
   14089     return NULL;
   14090 
   14091   h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
   14092   if (h == NULL)
   14093     {
   14094       /* A corrupt input file can lead to a situation where the index
   14095 	 does not reference either a local or an external symbol.  */
   14096       if (r_symndx >= cookie->locsymcount)
   14097 	return NULL;
   14098 
   14099       return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
   14100     }
   14101 
   14102   bool was_marked = h->mark;
   14103 
   14104   h->mark = 1;
   14105   /* Keep all aliases of the symbol too.  If an object symbol
   14106      needs to be copied into .dynbss then all of its aliases
   14107      should be present as dynamic symbols, not just the one used
   14108      on the copy relocation.  */
   14109   hw = h;
   14110   while (hw->is_weakalias)
   14111     {
   14112       hw = hw->u.alias;
   14113       hw->mark = 1;
   14114     }
   14115 
   14116   if (!was_marked && h->start_stop && !h->root.ldscript_def)
   14117     {
   14118       if (info->start_stop_gc)
   14119 	return NULL;
   14120 
   14121       /* To work around a glibc bug, mark XXX input sections
   14122 	 when there is a reference to __start_XXX or __stop_XXX
   14123 	 symbols.  */
   14124       else if (start_stop != NULL)
   14125 	{
   14126 	  asection *s = h->u2.start_stop_section;
   14127 	  *start_stop = true;
   14128 	  return s;
   14129 	}
   14130     }
   14131 
   14132   return (*gc_mark_hook) (sec, info, cookie, h, 0);
   14133 }
   14134 
   14135 /* COOKIE->rel describes a relocation against section SEC, which is
   14136    a section we've decided to keep.  Mark the section that contains
   14137    the relocation symbol.  */
   14138 
   14139 bool
   14140 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   14141 			asection *sec,
   14142 			elf_gc_mark_hook_fn gc_mark_hook,
   14143 			struct elf_reloc_cookie *cookie)
   14144 {
   14145   asection *rsec;
   14146   bool start_stop = false;
   14147 
   14148   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   14149   while (rsec != NULL)
   14150     {
   14151       if (!rsec->gc_mark)
   14152 	{
   14153 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   14154 	      || (rsec->owner->flags & DYNAMIC) != 0)
   14155 	    rsec->gc_mark = 1;
   14156 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   14157 	    return false;
   14158 	}
   14159       if (!start_stop)
   14160 	break;
   14161       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   14162     }
   14163   return true;
   14164 }
   14165 
   14166 /* The mark phase of garbage collection.  For a given section, mark
   14167    it and any sections in this section's group, and all the sections
   14168    which define symbols to which it refers.  */
   14169 
   14170 bool
   14171 _bfd_elf_gc_mark (struct bfd_link_info *info,
   14172 		  asection *sec,
   14173 		  elf_gc_mark_hook_fn gc_mark_hook)
   14174 {
   14175   bool ret;
   14176   asection *group_sec, *eh_frame, *sframe;
   14177 
   14178   sec->gc_mark = 1;
   14179 
   14180   /* Mark all the sections in the group.  */
   14181   group_sec = elf_section_data (sec)->next_in_group;
   14182   if (group_sec && !group_sec->gc_mark)
   14183     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   14184       return false;
   14185 
   14186   /* Look through the section relocs.  */
   14187   ret = true;
   14188   eh_frame = elf_eh_frame_section (sec->owner);
   14189   sframe = elf_sframe_section (sec->owner);
   14190 
   14191   if ((sec->flags & SEC_RELOC) != 0
   14192       && sec->reloc_count > 0
   14193       && sec != eh_frame
   14194       && sec != sframe)
   14195     {
   14196       struct elf_reloc_cookie cookie;
   14197 
   14198       if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
   14199 	ret = false;
   14200       else
   14201 	{
   14202 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   14203 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   14204 	      {
   14205 		ret = false;
   14206 		break;
   14207 	      }
   14208 	  fini_reloc_cookie_for_section (&cookie, sec);
   14209 	}
   14210     }
   14211 
   14212   if (ret && eh_frame && elf_fde_list (sec))
   14213     {
   14214       struct elf_reloc_cookie cookie;
   14215 
   14216       /* NB: When --no-keep-memory is used, the symbol table and
   14217 	 relocation info for eh_frame are freed after they are retrieved
   14218 	 for each text section in the input object.  If an input object
   14219 	 has many text sections, the same data is retrieved and freed
   14220 	 many times which can take a very long time.  Always keep the
   14221 	 symbol table and relocation info for eh_frame to avoid it.  */
   14222       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
   14223 					  true))
   14224 	ret = false;
   14225       else
   14226 	{
   14227 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   14228 				      gc_mark_hook, &cookie))
   14229 	    ret = false;
   14230 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   14231 	}
   14232     }
   14233 
   14234   eh_frame = elf_section_eh_frame_entry (sec);
   14235   if (ret && eh_frame && !eh_frame->gc_mark)
   14236     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   14237       ret = false;
   14238 
   14239   return ret;
   14240 }
   14241 
   14242 /* Scan and mark sections in a special or debug section group.  */
   14243 
   14244 static void
   14245 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   14246 {
   14247   /* Point to first section of section group.  */
   14248   asection *ssec;
   14249   /* Used to iterate the section group.  */
   14250   asection *msec;
   14251 
   14252   bool is_special_grp = true;
   14253   bool is_debug_grp = true;
   14254 
   14255   /* First scan to see if group contains any section other than debug
   14256      and special section.  */
   14257   ssec = msec = elf_next_in_group (grp);
   14258   while (msec != NULL)
   14259     {
   14260       if ((msec->flags & SEC_DEBUGGING) == 0)
   14261 	is_debug_grp = false;
   14262 
   14263       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   14264 	is_special_grp = false;
   14265 
   14266       msec = elf_next_in_group (msec);
   14267       if (msec == ssec)
   14268 	break;
   14269     }
   14270 
   14271   /* If this is a pure debug section group or pure special section group,
   14272      keep all sections in this group.  */
   14273   if (is_debug_grp || is_special_grp)
   14274     {
   14275       msec = ssec;
   14276       while (msec != NULL)
   14277 	{
   14278 	  msec->gc_mark = 1;
   14279 	  msec = elf_next_in_group (msec);
   14280 	  if (msec == ssec)
   14281 	    break;
   14282 	}
   14283     }
   14284 }
   14285 
   14286 /* Keep debug and special sections.  */
   14287 
   14288 bool
   14289 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   14290 				 elf_gc_mark_hook_fn mark_hook)
   14291 {
   14292   bfd *ibfd;
   14293 
   14294   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14295     {
   14296       asection *isec;
   14297       bool some_kept;
   14298       bool debug_frag_seen;
   14299       bool has_kept_debug_info;
   14300 
   14301       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14302 	continue;
   14303       isec = ibfd->sections;
   14304       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14305 	continue;
   14306 
   14307       /* Ensure all linker created sections are kept,
   14308 	 see if any other section is already marked,
   14309 	 and note if we have any fragmented debug sections.  */
   14310       debug_frag_seen = some_kept = has_kept_debug_info = false;
   14311       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14312 	{
   14313 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   14314 	    isec->gc_mark = 1;
   14315 	  else if (isec->gc_mark
   14316 		   && (isec->flags & SEC_ALLOC) != 0
   14317 		   && elf_section_type (isec) != SHT_NOTE)
   14318 	    some_kept = true;
   14319 	  else
   14320 	    {
   14321 	      /* Since all sections, except for backend specific ones,
   14322 		 have been garbage collected, call mark_hook on this
   14323 		 section if any of its linked-to sections is marked.  */
   14324 	      asection *linked_to_sec;
   14325 	      for (linked_to_sec = elf_linked_to_section (isec);
   14326 		   linked_to_sec != NULL && !linked_to_sec->linker_mark;
   14327 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14328 		{
   14329 		  if (linked_to_sec->gc_mark)
   14330 		    {
   14331 		      if (!_bfd_elf_gc_mark (info, isec, mark_hook))
   14332 			return false;
   14333 		      break;
   14334 		    }
   14335 		  linked_to_sec->linker_mark = 1;
   14336 		}
   14337 	      for (linked_to_sec = elf_linked_to_section (isec);
   14338 		   linked_to_sec != NULL && linked_to_sec->linker_mark;
   14339 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14340 		linked_to_sec->linker_mark = 0;
   14341 	    }
   14342 
   14343 	  if (!debug_frag_seen
   14344 	      && (isec->flags & SEC_DEBUGGING)
   14345 	      && startswith (isec->name, ".debug_line."))
   14346 	    debug_frag_seen = true;
   14347 	  else if (strcmp (bfd_section_name (isec),
   14348 			   "__patchable_function_entries") == 0
   14349 		   && elf_linked_to_section (isec) == NULL)
   14350 	      info->callbacks->fatal (_("%P: %pB(%pA): error: "
   14351 					"need linked-to section "
   14352 					"for --gc-sections\n"),
   14353 				      isec->owner, isec);
   14354 	}
   14355 
   14356       /* If no non-note alloc section in this file will be kept, then
   14357 	 we can toss out the debug and special sections.  */
   14358       if (!some_kept)
   14359 	continue;
   14360 
   14361       /* Keep debug and special sections like .comment when they are
   14362 	 not part of a group.  Also keep section groups that contain
   14363 	 just debug sections or special sections.  NB: Sections with
   14364 	 linked-to section has been handled above.  */
   14365       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14366 	{
   14367 	  if ((isec->flags & SEC_GROUP) != 0)
   14368 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   14369 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   14370 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   14371 		   && elf_next_in_group (isec) == NULL
   14372 		   && elf_linked_to_section (isec) == NULL)
   14373 	    isec->gc_mark = 1;
   14374 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
   14375 	    has_kept_debug_info = true;
   14376 	}
   14377 
   14378       /* Look for CODE sections which are going to be discarded,
   14379 	 and find and discard any fragmented debug sections which
   14380 	 are associated with that code section.  */
   14381       if (debug_frag_seen)
   14382 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14383 	  if ((isec->flags & SEC_CODE) != 0
   14384 	      && isec->gc_mark == 0)
   14385 	    {
   14386 	      unsigned int ilen;
   14387 	      asection *dsec;
   14388 
   14389 	      ilen = strlen (isec->name);
   14390 
   14391 	      /* Association is determined by the name of the debug
   14392 		 section containing the name of the code section as
   14393 		 a suffix.  For example .debug_line.text.foo is a
   14394 		 debug section associated with .text.foo.  */
   14395 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   14396 		{
   14397 		  unsigned int dlen;
   14398 
   14399 		  if (dsec->gc_mark == 0
   14400 		      || (dsec->flags & SEC_DEBUGGING) == 0)
   14401 		    continue;
   14402 
   14403 		  dlen = strlen (dsec->name);
   14404 
   14405 		  if (dlen > ilen
   14406 		      && strncmp (dsec->name + (dlen - ilen),
   14407 				  isec->name, ilen) == 0)
   14408 		    dsec->gc_mark = 0;
   14409 		}
   14410 	  }
   14411 
   14412       /* Mark debug sections referenced by kept debug sections.  */
   14413       if (has_kept_debug_info)
   14414 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14415 	  if (isec->gc_mark
   14416 	      && (isec->flags & SEC_DEBUGGING) != 0)
   14417 	    if (!_bfd_elf_gc_mark (info, isec,
   14418 				   elf_gc_mark_debug_section))
   14419 	      return false;
   14420     }
   14421   return true;
   14422 }
   14423 
   14424 static bool
   14425 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   14426 {
   14427   bfd *sub;
   14428   elf_backend_data *bed = get_elf_backend_data (abfd);
   14429 
   14430   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14431     {
   14432       asection *o;
   14433 
   14434       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14435 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
   14436 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14437 	continue;
   14438       o = sub->sections;
   14439       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14440 	continue;
   14441 
   14442       for (o = sub->sections; o != NULL; o = o->next)
   14443 	{
   14444 	  /* When any section in a section group is kept, we keep all
   14445 	     sections in the section group.  If the first member of
   14446 	     the section group is excluded, we will also exclude the
   14447 	     group section.  */
   14448 	  if (o->flags & SEC_GROUP)
   14449 	    {
   14450 	      asection *first = elf_next_in_group (o);
   14451 	      if (first != NULL)
   14452 		o->gc_mark = first->gc_mark;
   14453 	    }
   14454 
   14455 	  if (o->gc_mark)
   14456 	    continue;
   14457 
   14458 	  /* Skip sweeping sections already excluded.  */
   14459 	  if (o->flags & SEC_EXCLUDE)
   14460 	    continue;
   14461 
   14462 	  /* Since this is early in the link process, it is simple
   14463 	     to remove a section from the output.  */
   14464 	  o->flags |= SEC_EXCLUDE;
   14465 
   14466 	  if (info->print_gc_sections && o->size != 0)
   14467 	    /* xgettext:c-format */
   14468 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
   14469 				o, sub);
   14470 	}
   14471     }
   14472 
   14473   return true;
   14474 }
   14475 
   14476 /* Propagate collected vtable information.  This is called through
   14477    elf_link_hash_traverse.  */
   14478 
   14479 static bool
   14480 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   14481 {
   14482   /* Those that are not vtables.  */
   14483   if (h->start_stop
   14484       || h->u2.vtable == NULL
   14485       || h->u2.vtable->parent == NULL)
   14486     return true;
   14487 
   14488   /* Those vtables that do not have parents, we cannot merge.  */
   14489   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
   14490     return true;
   14491 
   14492   /* If we've already been done, exit.  */
   14493   if (h->u2.vtable->used && h->u2.vtable->used[-1])
   14494     return true;
   14495 
   14496   /* Make sure the parent's table is up to date.  */
   14497   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
   14498 
   14499   if (h->u2.vtable->used == NULL)
   14500     {
   14501       /* None of this table's entries were referenced.  Re-use the
   14502 	 parent's table.  */
   14503       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
   14504       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
   14505     }
   14506   else
   14507     {
   14508       size_t n;
   14509       bool *cu, *pu;
   14510 
   14511       /* Or the parent's entries into ours.  */
   14512       cu = h->u2.vtable->used;
   14513       cu[-1] = true;
   14514       pu = h->u2.vtable->parent->u2.vtable->used;
   14515       if (pu != NULL)
   14516 	{
   14517 	  elf_backend_data *bed;
   14518 	  unsigned int log_file_align;
   14519 
   14520 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   14521 	  log_file_align = bed->s->log_file_align;
   14522 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
   14523 	  while (n--)
   14524 	    {
   14525 	      if (*pu)
   14526 		*cu = true;
   14527 	      pu++;
   14528 	      cu++;
   14529 	    }
   14530 	}
   14531     }
   14532 
   14533   return true;
   14534 }
   14535 
   14536 struct link_info_ok
   14537 {
   14538   struct bfd_link_info *info;
   14539   bool ok;
   14540 };
   14541 
   14542 static bool
   14543 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
   14544 				    void *ptr)
   14545 {
   14546   asection *sec;
   14547   bfd_vma hstart, hend;
   14548   Elf_Internal_Rela *relstart, *relend, *rel;
   14549   elf_backend_data *bed;
   14550   unsigned int log_file_align;
   14551   struct link_info_ok *info = (struct link_info_ok *) ptr;
   14552 
   14553   /* Take care of both those symbols that do not describe vtables as
   14554      well as those that are not loaded.  */
   14555   if (h->start_stop
   14556       || h->u2.vtable == NULL
   14557       || h->u2.vtable->parent == NULL)
   14558     return true;
   14559 
   14560   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   14561 	      || h->root.type == bfd_link_hash_defweak);
   14562 
   14563   sec = h->root.u.def.section;
   14564   hstart = h->root.u.def.value;
   14565   hend = hstart + h->size;
   14566 
   14567   relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
   14568 					     sec, NULL, NULL, true);
   14569   if (!relstart)
   14570     return info->ok = false;
   14571   bed = get_elf_backend_data (sec->owner);
   14572   log_file_align = bed->s->log_file_align;
   14573 
   14574   relend = relstart + sec->reloc_count;
   14575 
   14576   for (rel = relstart; rel < relend; ++rel)
   14577     if (rel->r_offset >= hstart && rel->r_offset < hend)
   14578       {
   14579 	/* If the entry is in use, do nothing.  */
   14580 	if (h->u2.vtable->used
   14581 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
   14582 	  {
   14583 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   14584 	    if (h->u2.vtable->used[entry])
   14585 	      continue;
   14586 	  }
   14587 	/* Otherwise, kill it.  */
   14588 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   14589       }
   14590 
   14591   return true;
   14592 }
   14593 
   14594 /* Mark sections containing dynamically referenced symbols.  When
   14595    building shared libraries, we must assume that any visible symbol is
   14596    referenced.  */
   14597 
   14598 bool
   14599 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   14600 {
   14601   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   14602   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   14603 
   14604   if ((h->root.type == bfd_link_hash_defined
   14605        || h->root.type == bfd_link_hash_defweak)
   14606       && (!h->start_stop
   14607 	  || h->root.ldscript_def
   14608 	  || !info->start_stop_gc)
   14609       && ((h->ref_dynamic && !h->forced_local)
   14610 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   14611 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   14612 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   14613 	      && (!bfd_link_executable (info)
   14614 		  || info->gc_keep_exported
   14615 		  || info->export_dynamic
   14616 		  || (h->dynamic
   14617 		      && d != NULL
   14618 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   14619 	      && (h->versioned >= versioned
   14620 		  || !bfd_hide_sym_by_version (info->version_info,
   14621 					       h->root.root.string)))))
   14622     h->root.u.def.section->flags |= SEC_KEEP;
   14623 
   14624   return true;
   14625 }
   14626 
   14627 /* Keep all sections containing symbols undefined on the command-line,
   14628    and the section containing the entry symbol.  */
   14629 
   14630 void
   14631 _bfd_elf_gc_keep (struct bfd_link_info *info)
   14632 {
   14633   struct bfd_sym_chain *sym;
   14634 
   14635   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   14636     {
   14637       struct elf_link_hash_entry *h;
   14638 
   14639       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   14640 				false, false, false);
   14641 
   14642       if (h != NULL
   14643 	  && (h->root.type == bfd_link_hash_defined
   14644 	      || h->root.type == bfd_link_hash_defweak)
   14645 	  && !bfd_is_const_section (h->root.u.def.section))
   14646 	h->root.u.def.section->flags |= SEC_KEEP;
   14647     }
   14648 }
   14649 
   14650 bool
   14651 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   14652 				struct bfd_link_info *info)
   14653 {
   14654   bfd *ibfd = info->input_bfds;
   14655 
   14656   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14657     {
   14658       asection *sec;
   14659       struct elf_reloc_cookie cookie;
   14660 
   14661       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14662 	continue;
   14663       sec = ibfd->sections;
   14664       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14665 	continue;
   14666 
   14667       if (!init_reloc_cookie (&cookie, ibfd))
   14668 	return false;
   14669 
   14670       for (sec = ibfd->sections; sec; sec = sec->next)
   14671 	{
   14672 	  if (startswith (bfd_section_name (sec), ".eh_frame_entry")
   14673 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
   14674 					 false))
   14675 	    {
   14676 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   14677 	      fini_reloc_cookie_rels (&cookie, sec);
   14678 	    }
   14679 	}
   14680     }
   14681   return true;
   14682 }
   14683 
   14684 /* Do mark and sweep of unused sections.  */
   14685 
   14686 bool
   14687 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   14688 {
   14689   bool ok = true;
   14690   bfd *sub;
   14691   elf_gc_mark_hook_fn gc_mark_hook;
   14692   elf_backend_data *bed = get_elf_backend_data (abfd);
   14693   struct elf_link_hash_table *htab;
   14694   struct link_info_ok info_ok;
   14695 
   14696   if (!bed->can_gc_sections
   14697       || !is_elf_hash_table (info->hash))
   14698     {
   14699       _bfd_error_handler(_("warning: gc-sections option ignored"));
   14700       return true;
   14701     }
   14702 
   14703   bed->gc_keep (info);
   14704   htab = elf_hash_table (info);
   14705 
   14706   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   14707      at the .eh_frame section if we can mark the FDEs individually.  */
   14708   for (sub = info->input_bfds;
   14709        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   14710        sub = sub->link.next)
   14711     {
   14712       asection *sec;
   14713       struct elf_reloc_cookie cookie;
   14714 
   14715       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
   14716 	continue;
   14717       sec = sub->sections;
   14718       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14719 	continue;
   14720       sec = bfd_get_section_by_name (sub, ".eh_frame");
   14721       while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
   14722 						   false))
   14723 	{
   14724 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   14725 	  if (sec->sec_info
   14726 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   14727 	    elf_eh_frame_section (sub) = sec;
   14728 	  fini_reloc_cookie_for_section (&cookie, sec);
   14729 	  sec = bfd_get_next_section_by_name (NULL, sec);
   14730 	}
   14731 
   14732       /* Handle .sframe section.  */
   14733       sec = bfd_get_section_by_name (sub, ".sframe");
   14734       while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
   14735 						   false))
   14736 	{
   14737 	  _bfd_elf_parse_sframe (sub, info, sec, &cookie);
   14738 
   14739 	  if (sec->sec_info
   14740 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   14741 	    elf_sframe_section (sub) = sec;
   14742 
   14743 	  fini_reloc_cookie_for_section (&cookie, sec);
   14744 	  sec = bfd_get_next_section_by_name (NULL, sec);
   14745 	}
   14746     }
   14747 
   14748   /* Apply transitive closure to the vtable entry usage info.  */
   14749   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   14750   if (!ok)
   14751     return false;
   14752 
   14753   /* Kill the vtable relocations that were not used.  */
   14754   info_ok.info = info;
   14755   info_ok.ok = true;
   14756   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
   14757   if (!info_ok.ok)
   14758     return false;
   14759 
   14760   /* Mark dynamically referenced symbols.  */
   14761   if (htab->dynamic_sections_created || info->gc_keep_exported)
   14762     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   14763 
   14764   /* Grovel through relocs to find out who stays ...  */
   14765   gc_mark_hook = bed->gc_mark_hook;
   14766   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14767     {
   14768       asection *o;
   14769 
   14770       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14771 	  || elf_object_id (sub) != elf_hash_table_id (htab)
   14772 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14773 	continue;
   14774 
   14775       o = sub->sections;
   14776       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14777 	continue;
   14778 
   14779       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   14780 	 Also treat note sections as a root, if the section is not part
   14781 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
   14782 	 well as FINI_ARRAY sections for ld -r.  */
   14783       for (o = sub->sections; o != NULL; o = o->next)
   14784 	if (!o->gc_mark
   14785 	    && (o->flags & SEC_EXCLUDE) == 0
   14786 	    && ((o->flags & SEC_KEEP) != 0
   14787 		|| (bfd_link_relocatable (info)
   14788 		    && ((elf_section_data (o)->this_hdr.sh_type
   14789 			 == SHT_PREINIT_ARRAY)
   14790 			|| (elf_section_data (o)->this_hdr.sh_type
   14791 			    == SHT_INIT_ARRAY)
   14792 			|| (elf_section_data (o)->this_hdr.sh_type
   14793 			    == SHT_FINI_ARRAY)))
   14794 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   14795 		    && elf_next_in_group (o) == NULL
   14796 		    && elf_linked_to_section (o) == NULL)
   14797 		|| ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
   14798 		    && (elf_section_flags (o) & SHF_GNU_RETAIN))))
   14799 	  {
   14800 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   14801 	      return false;
   14802 	  }
   14803     }
   14804 
   14805   /* Allow the backend to mark additional target specific sections.  */
   14806   bed->gc_mark_extra_sections (info, gc_mark_hook);
   14807 
   14808   /* ... and mark SEC_EXCLUDE for those that go.  */
   14809   return elf_gc_sweep (abfd, info);
   14810 }
   14811 
   14812 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   14814 
   14815 bool
   14816 bfd_elf_gc_record_vtinherit (bfd *abfd,
   14817 			     asection *sec,
   14818 			     struct elf_link_hash_entry *h,
   14819 			     bfd_vma offset)
   14820 {
   14821   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   14822   struct elf_link_hash_entry **search, *child;
   14823   size_t extsymcount;
   14824   elf_backend_data *bed = get_elf_backend_data (abfd);
   14825 
   14826   /* The sh_info field of the symtab header tells us where the
   14827      external symbols start.  We don't care about the local symbols at
   14828      this point.  */
   14829   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   14830   if (!elf_bad_symtab (abfd))
   14831     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   14832 
   14833   sym_hashes = elf_sym_hashes (abfd);
   14834   sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
   14835 
   14836   /* Hunt down the child symbol, which is in this section at the same
   14837      offset as the relocation.  */
   14838   for (search = sym_hashes; search != sym_hashes_end; ++search)
   14839     {
   14840       if ((child = *search) != NULL
   14841 	  && (child->root.type == bfd_link_hash_defined
   14842 	      || child->root.type == bfd_link_hash_defweak)
   14843 	  && child->root.u.def.section == sec
   14844 	  && child->root.u.def.value == offset)
   14845 	goto win;
   14846     }
   14847 
   14848   /* xgettext:c-format */
   14849   _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
   14850 		      abfd, sec, (uint64_t) offset);
   14851   bfd_set_error (bfd_error_invalid_operation);
   14852   return false;
   14853 
   14854  win:
   14855   if (!child->u2.vtable)
   14856     {
   14857       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14858 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
   14859       if (!child->u2.vtable)
   14860 	return false;
   14861     }
   14862   if (!h)
   14863     {
   14864       /* This *should* only be the absolute section.  It could potentially
   14865 	 be that someone has defined a non-global vtable though, which
   14866 	 would be bad.  It isn't worth paging in the local symbols to be
   14867 	 sure though; that case should simply be handled by the assembler.  */
   14868 
   14869       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
   14870     }
   14871   else
   14872     child->u2.vtable->parent = h;
   14873 
   14874   return true;
   14875 }
   14876 
   14877 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   14878 
   14879 bool
   14880 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
   14881 			   struct elf_link_hash_entry *h,
   14882 			   bfd_vma addend)
   14883 {
   14884   elf_backend_data *bed = get_elf_backend_data (abfd);
   14885   unsigned int log_file_align = bed->s->log_file_align;
   14886 
   14887   if (!h || addend > 1u << 28)
   14888     {
   14889       /* xgettext:c-format */
   14890       _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
   14891 			  abfd, sec);
   14892       bfd_set_error (bfd_error_bad_value);
   14893       return false;
   14894     }
   14895 
   14896   if (!h->u2.vtable)
   14897     {
   14898       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14899 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
   14900       if (!h->u2.vtable)
   14901 	return false;
   14902     }
   14903 
   14904   if (addend >= h->u2.vtable->size)
   14905     {
   14906       size_t size, bytes, file_align;
   14907       bool *ptr = h->u2.vtable->used;
   14908 
   14909       /* While the symbol is undefined, we have to be prepared to handle
   14910 	 a zero size.  */
   14911       file_align = 1 << log_file_align;
   14912       if (h->root.type == bfd_link_hash_undefined)
   14913 	size = addend + file_align;
   14914       else
   14915 	{
   14916 	  size = h->size;
   14917 	  if (addend >= size)
   14918 	    {
   14919 	      /* Oops!  We've got a reference past the defined end of
   14920 		 the table.  This is probably a bug -- shall we warn?  */
   14921 	      size = addend + file_align;
   14922 	    }
   14923 	}
   14924       size = (size + file_align - 1) & -file_align;
   14925 
   14926       /* Allocate one extra entry for use as a "done" flag for the
   14927 	 consolidation pass.  */
   14928       bytes = ((size >> log_file_align) + 1) * sizeof (bool);
   14929 
   14930       if (ptr)
   14931 	{
   14932 	  ptr = (bool *) bfd_realloc (ptr - 1, bytes);
   14933 
   14934 	  if (ptr != NULL)
   14935 	    {
   14936 	      size_t oldbytes;
   14937 
   14938 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
   14939 			  * sizeof (bool));
   14940 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   14941 	    }
   14942 	}
   14943       else
   14944 	ptr = (bool *) bfd_zmalloc (bytes);
   14945 
   14946       if (ptr == NULL)
   14947 	return false;
   14948 
   14949       /* And arrange for that done flag to be at index -1.  */
   14950       h->u2.vtable->used = ptr + 1;
   14951       h->u2.vtable->size = size;
   14952     }
   14953 
   14954   h->u2.vtable->used[addend >> log_file_align] = true;
   14955 
   14956   return true;
   14957 }
   14958 
   14959 /* Map an ELF section header flag to its corresponding string.  */
   14960 typedef struct
   14961 {
   14962   char *flag_name;
   14963   flagword flag_value;
   14964 } elf_flags_to_name_table;
   14965 
   14966 static const elf_flags_to_name_table elf_flags_to_names [] =
   14967 {
   14968   { "SHF_WRITE", SHF_WRITE },
   14969   { "SHF_ALLOC", SHF_ALLOC },
   14970   { "SHF_EXECINSTR", SHF_EXECINSTR },
   14971   { "SHF_MERGE", SHF_MERGE },
   14972   { "SHF_STRINGS", SHF_STRINGS },
   14973   { "SHF_INFO_LINK", SHF_INFO_LINK},
   14974   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   14975   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   14976   { "SHF_GROUP", SHF_GROUP },
   14977   { "SHF_TLS", SHF_TLS },
   14978   { "SHF_MASKOS", SHF_MASKOS },
   14979   { "SHF_EXCLUDE", SHF_EXCLUDE },
   14980 };
   14981 
   14982 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   14983 bool
   14984 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   14985 			      struct flag_info *flaginfo,
   14986 			      asection *section)
   14987 {
   14988   const bfd_vma sh_flags = elf_section_flags (section);
   14989 
   14990   if (!flaginfo->flags_initialized)
   14991     {
   14992       bfd *obfd = info->output_bfd;
   14993       elf_backend_data *bed = get_elf_backend_data (obfd);
   14994       struct flag_info_list *tf = flaginfo->flag_list;
   14995       int with_hex = 0;
   14996       int without_hex = 0;
   14997 
   14998       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   14999 	{
   15000 	  unsigned i;
   15001 	  flagword (*lookup) (char *);
   15002 
   15003 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   15004 	  if (lookup != NULL)
   15005 	    {
   15006 	      flagword hexval = (*lookup) ((char *) tf->name);
   15007 
   15008 	      if (hexval != 0)
   15009 		{
   15010 		  if (tf->with == with_flags)
   15011 		    with_hex |= hexval;
   15012 		  else if (tf->with == without_flags)
   15013 		    without_hex |= hexval;
   15014 		  tf->valid = true;
   15015 		  continue;
   15016 		}
   15017 	    }
   15018 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   15019 	    {
   15020 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   15021 		{
   15022 		  if (tf->with == with_flags)
   15023 		    with_hex |= elf_flags_to_names[i].flag_value;
   15024 		  else if (tf->with == without_flags)
   15025 		    without_hex |= elf_flags_to_names[i].flag_value;
   15026 		  tf->valid = true;
   15027 		  break;
   15028 		}
   15029 	    }
   15030 	  if (!tf->valid)
   15031 	    {
   15032 	      info->callbacks->einfo
   15033 		(_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   15034 	      return false;
   15035 	    }
   15036 	}
   15037       flaginfo->flags_initialized = true;
   15038       flaginfo->only_with_flags |= with_hex;
   15039       flaginfo->not_with_flags |= without_hex;
   15040     }
   15041 
   15042   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   15043     return false;
   15044 
   15045   if ((flaginfo->not_with_flags & sh_flags) != 0)
   15046     return false;
   15047 
   15048   return true;
   15049 }
   15050 
   15051 struct alloc_got_off_arg {
   15052   bfd_vma gotoff;
   15053   struct bfd_link_info *info;
   15054 };
   15055 
   15056 /* We need a special top-level link routine to convert got reference counts
   15057    to real got offsets.  */
   15058 
   15059 static bool
   15060 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   15061 {
   15062   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   15063   bfd *obfd = gofarg->info->output_bfd;
   15064   elf_backend_data *bed = get_elf_backend_data (obfd);
   15065 
   15066   if (h->got.refcount > 0)
   15067     {
   15068       h->got.offset = gofarg->gotoff;
   15069       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   15070     }
   15071   else
   15072     h->got.offset = (bfd_vma) -1;
   15073 
   15074   return true;
   15075 }
   15076 
   15077 /* And an accompanying bit to work out final got entry offsets once
   15078    we're done.  Should be called from final_link.  */
   15079 
   15080 bool
   15081 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   15082 					struct bfd_link_info *info)
   15083 {
   15084   bfd *i;
   15085   elf_backend_data *bed = get_elf_backend_data (abfd);
   15086   bfd_vma gotoff;
   15087   struct alloc_got_off_arg gofarg;
   15088 
   15089   BFD_ASSERT (abfd == info->output_bfd);
   15090 
   15091   if (! is_elf_hash_table (info->hash))
   15092     return false;
   15093 
   15094   /* The GOT offset is relative to the .got section, but the GOT header is
   15095      put into the .got.plt section, if the backend uses it.  */
   15096   if (bed->want_got_plt)
   15097     gotoff = 0;
   15098   else
   15099     gotoff = bed->got_header_size;
   15100 
   15101   /* Do the local .got entries first.  */
   15102   for (i = info->input_bfds; i; i = i->link.next)
   15103     {
   15104       bfd_signed_vma *local_got;
   15105       size_t j, locsymcount;
   15106       Elf_Internal_Shdr *symtab_hdr;
   15107 
   15108       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   15109 	continue;
   15110 
   15111       local_got = elf_local_got_refcounts (i);
   15112       if (!local_got)
   15113 	continue;
   15114 
   15115       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   15116       if (elf_bad_symtab (i))
   15117 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   15118       else
   15119 	locsymcount = symtab_hdr->sh_info;
   15120 
   15121       for (j = 0; j < locsymcount; ++j)
   15122 	{
   15123 	  if (local_got[j] > 0)
   15124 	    {
   15125 	      local_got[j] = gotoff;
   15126 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   15127 	    }
   15128 	  else
   15129 	    local_got[j] = (bfd_vma) -1;
   15130 	}
   15131     }
   15132 
   15133   /* Then the global .got entries.  .plt refcounts are handled by
   15134      adjust_dynamic_symbol  */
   15135   gofarg.gotoff = gotoff;
   15136   gofarg.info = info;
   15137   elf_link_hash_traverse (elf_hash_table (info),
   15138 			  elf_gc_allocate_got_offsets,
   15139 			  &gofarg);
   15140   return true;
   15141 }
   15142 
   15143 /* Many folk need no more in the way of final link than this, once
   15144    got entry reference counting is enabled.  */
   15145 
   15146 bool
   15147 _bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   15148 {
   15149   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   15150     return false;
   15151 
   15152   /* Invoke the regular ELF backend linker to do all the work.  */
   15153   return _bfd_elf_final_link (abfd, info);
   15154 }
   15155 
   15156 bool
   15157 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   15158 {
   15159   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   15160 
   15161   if (elf_bad_symtab (rcookie->abfd))
   15162     rcookie->rel = rcookie->rels;
   15163 
   15164   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   15165     {
   15166       unsigned long r_symndx;
   15167 
   15168       if (!elf_bad_symtab (rcookie->abfd)
   15169 	  && rcookie->rel->r_offset > offset)
   15170 	return false;
   15171       if (rcookie->rel->r_offset != offset)
   15172 	continue;
   15173 
   15174       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   15175       if (r_symndx == STN_UNDEF)
   15176 	return true;
   15177 
   15178       struct elf_link_hash_entry *h;
   15179 
   15180       h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
   15181 
   15182       if (h != NULL)
   15183 	{
   15184 	  if ((h->root.type == bfd_link_hash_defined
   15185 	       || h->root.type == bfd_link_hash_defweak)
   15186 	      && (h->root.u.def.section->owner != rcookie->abfd
   15187 		  || h->root.u.def.section->kept_section != NULL
   15188 		  || discarded_section (h->root.u.def.section)))
   15189 	    return true;
   15190 	}
   15191       else
   15192 	{
   15193 	  if (r_symndx >= rcookie->locsymcount)
   15194 	    /* This can happen with corrupt input.  */
   15195 	    return false;
   15196 
   15197 	  /* It's not a relocation against a global symbol,
   15198 	     but it could be a relocation against a local
   15199 	     symbol for a discarded section.  */
   15200 	  asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
   15201 	  if (isec != NULL
   15202 	      && (isec->kept_section != NULL
   15203 		  || discarded_section (isec)))
   15204 	    return true;
   15205 	}
   15206 
   15207       return false;
   15208     }
   15209   return false;
   15210 }
   15211 
   15212 /* Discard unneeded references to discarded sections.
   15213    Returns -1 on error, 1 if any section's size was changed, 0 if
   15214    nothing changed.  This function assumes that the relocations are in
   15215    sorted order, which is true for all known assemblers.  */
   15216 
   15217 int
   15218 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   15219 {
   15220   struct elf_reloc_cookie cookie;
   15221   asection *o;
   15222   bfd *abfd;
   15223   int changed = 0;
   15224 
   15225   if (info->traditional_format
   15226       || !is_elf_hash_table (info->hash))
   15227     return 0;
   15228 
   15229   o = bfd_get_section_by_name (output_bfd, ".stab");
   15230   if (o != NULL)
   15231     {
   15232       asection *i;
   15233 
   15234       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15235 	{
   15236 	  if (i->size == 0
   15237 	      || i->reloc_count == 0
   15238 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   15239 	    continue;
   15240 
   15241 	  abfd = i->owner;
   15242 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15243 	    continue;
   15244 
   15245 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15246 	    return -1;
   15247 
   15248 	  if (_bfd_discard_section_stabs (abfd, i,
   15249 					  bfd_elf_reloc_symbol_deleted_p,
   15250 					  &cookie))
   15251 	    changed = 1;
   15252 
   15253 	  fini_reloc_cookie_for_section (&cookie, i);
   15254 	}
   15255     }
   15256 
   15257   o = NULL;
   15258   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   15259     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   15260   if (o != NULL)
   15261     {
   15262       asection *i;
   15263       int eh_changed = 0;
   15264       unsigned int eh_alignment;  /* Octets.  */
   15265 
   15266       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15267 	{
   15268 	  int r;
   15269 
   15270 	  if (i->size == 0)
   15271 	    continue;
   15272 
   15273 	  abfd = i->owner;
   15274 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15275 	    continue;
   15276 
   15277 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15278 	    return -1;
   15279 
   15280 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   15281 	  r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
   15282 						 bfd_elf_reloc_symbol_deleted_p,
   15283 						 &cookie);
   15284 	  if (r)
   15285 	    {
   15286 	      eh_changed = 1;
   15287 	      if (r >= 2)
   15288 		changed = 1;
   15289 	    }
   15290 
   15291 	  fini_reloc_cookie_for_section (&cookie, i);
   15292 	}
   15293 
   15294       eh_alignment = ((1 << o->alignment_power)
   15295 		      * bfd_octets_per_byte (output_bfd, o));
   15296       /* Skip over zero terminator, and prevent empty sections from
   15297 	 adding alignment padding at the end.  */
   15298       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
   15299 	if (i->size == 0)
   15300 	  i->flags |= SEC_EXCLUDE;
   15301 	else if (i->size > 4)
   15302 	  break;
   15303       /* The last non-empty eh_frame section doesn't need padding.  */
   15304       if (i != NULL)
   15305 	i = i->map_tail.s;
   15306       /* Any prior sections must pad the last FDE out to the output
   15307 	 section alignment.  Otherwise we might have zero padding
   15308 	 between sections, which would be seen as a terminator.  */
   15309       for (; i != NULL; i = i->map_tail.s)
   15310 	if (i->size == 4)
   15311 	  /* All but the last zero terminator should have been removed.  */
   15312 	  BFD_FAIL ();
   15313 	else
   15314 	  {
   15315 	    bfd_size_type size
   15316 	      = (i->size + eh_alignment - 1) & -eh_alignment;
   15317 	    if (i->size != size)
   15318 	      {
   15319 		i->size = size;
   15320 		changed = 1;
   15321 		eh_changed = 1;
   15322 	      }
   15323 	  }
   15324       if (eh_changed)
   15325 	elf_link_hash_traverse (elf_hash_table (info),
   15326 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
   15327     }
   15328 
   15329   o = bfd_get_section_by_name (output_bfd, ".sframe");
   15330   if (o != NULL)
   15331     {
   15332       asection *i;
   15333 
   15334       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15335 	{
   15336 	  if (i->size == 0)
   15337 	    continue;
   15338 
   15339 	  abfd = i->owner;
   15340 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15341 	    continue;
   15342 
   15343 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15344 	    return -1;
   15345 
   15346 	  if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
   15347 	    {
   15348 	      if (_bfd_elf_discard_section_sframe (i,
   15349 						   bfd_elf_reloc_symbol_deleted_p,
   15350 						   &cookie))
   15351 		{
   15352 		  if (i->size != i->rawsize)
   15353 		    changed = 1;
   15354 		}
   15355 	    }
   15356 	  fini_reloc_cookie_for_section (&cookie, i);
   15357 	}
   15358       /* Update the reference to the output .sframe section.  Used to
   15359 	 determine later if PT_GNU_SFRAME segment is to be generated.  */
   15360       if (!_bfd_elf_set_section_sframe (output_bfd, info))
   15361 	return -1;
   15362     }
   15363 
   15364   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   15365     {
   15366       elf_backend_data *bed;
   15367       asection *s;
   15368 
   15369       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15370 	continue;
   15371       s = abfd->sections;
   15372       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   15373 	continue;
   15374 
   15375       bed = get_elf_backend_data (abfd);
   15376 
   15377       if (bed->elf_backend_discard_info != NULL)
   15378 	{
   15379 	  if (!init_reloc_cookie (&cookie, abfd))
   15380 	    return -1;
   15381 
   15382 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   15383 	    changed = 1;
   15384 
   15385 	  fini_reloc_cookie (&cookie, abfd);
   15386 	}
   15387     }
   15388 
   15389   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   15390     _bfd_elf_end_eh_frame_parsing (info);
   15391 
   15392   if (_bfd_elf_discard_section_eh_frame_hdr (info))
   15393     changed = 1;
   15394 
   15395   return changed;
   15396 }
   15397 
   15398 bool
   15399 _bfd_elf_section_already_linked (bfd *abfd,
   15400 				 asection *sec,
   15401 				 struct bfd_link_info *info)
   15402 {
   15403   flagword flags;
   15404   const char *name, *key;
   15405   struct bfd_section_already_linked *l;
   15406   struct bfd_section_already_linked_hash_entry *already_linked_list;
   15407 
   15408   if (sec->output_section == bfd_abs_section_ptr)
   15409     return false;
   15410 
   15411   flags = sec->flags;
   15412 
   15413   /* Return if it isn't a linkonce section.  A comdat group section
   15414      also has SEC_LINK_ONCE set.  */
   15415   if ((flags & SEC_LINK_ONCE) == 0)
   15416     return false;
   15417 
   15418   /* Don't put group member sections on our list of already linked
   15419      sections.  They are handled as a group via their group section.  */
   15420   if (elf_sec_group (sec) != NULL)
   15421     return false;
   15422 
   15423   /* For a SHT_GROUP section, use the group signature as the key.  */
   15424   name = sec->name;
   15425   if ((flags & SEC_GROUP) != 0
   15426       && elf_next_in_group (sec) != NULL
   15427       && elf_group_name (elf_next_in_group (sec)) != NULL)
   15428     key = elf_group_name (elf_next_in_group (sec));
   15429   else
   15430     {
   15431       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   15432       if (startswith (name, ".gnu.linkonce.")
   15433 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   15434 	key++;
   15435       else
   15436 	/* Must be a user linkonce section that doesn't follow gcc's
   15437 	   naming convention.  In this case we won't be matching
   15438 	   single member groups.  */
   15439 	key = name;
   15440     }
   15441 
   15442   already_linked_list = bfd_section_already_linked_table_lookup (key);
   15443 
   15444   for (l = already_linked_list->entry; l != NULL; l = l->next)
   15445     {
   15446       /* We may have 2 different types of sections on the list: group
   15447 	 sections with a signature of <key> (<key> is some string),
   15448 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   15449 	 Match like sections.  LTO plugin sections are an exception.
   15450 	 They are always named .gnu.linkonce.t.<key> and match either
   15451 	 type of section.  */
   15452       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   15453 	   && ((flags & SEC_GROUP) != 0
   15454 	       || strcmp (name, l->sec->name) == 0))
   15455 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0
   15456 	  || (sec->owner->flags & BFD_PLUGIN) != 0)
   15457 	{
   15458 	  /* The section has already been linked.  See if we should
   15459 	     issue a warning.  */
   15460 	  if (!_bfd_handle_already_linked (sec, l, info))
   15461 	    return false;
   15462 
   15463 	  if (flags & SEC_GROUP)
   15464 	    {
   15465 	      asection *first = elf_next_in_group (sec);
   15466 	      asection *s = first;
   15467 
   15468 	      while (s != NULL)
   15469 		{
   15470 		  s->output_section = bfd_abs_section_ptr;
   15471 		  /* Record which group discards it.  */
   15472 		  s->kept_section = l->sec;
   15473 		  s = elf_next_in_group (s);
   15474 		  /* These lists are circular.  */
   15475 		  if (s == first)
   15476 		    break;
   15477 		}
   15478 	    }
   15479 
   15480 	  return true;
   15481 	}
   15482     }
   15483 
   15484   /* A single member comdat group section may be discarded by a
   15485      linkonce section and vice versa.  */
   15486   if ((flags & SEC_GROUP) != 0)
   15487     {
   15488       asection *first = elf_next_in_group (sec);
   15489 
   15490       if (first != NULL && elf_next_in_group (first) == first)
   15491 	/* Check this single member group against linkonce sections.  */
   15492 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   15493 	  if ((l->sec->flags & SEC_GROUP) == 0
   15494 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   15495 	    {
   15496 	      first->output_section = bfd_abs_section_ptr;
   15497 	      first->kept_section = l->sec;
   15498 	      sec->output_section = bfd_abs_section_ptr;
   15499 	      break;
   15500 	    }
   15501     }
   15502   else
   15503     /* Check this linkonce section against single member groups.  */
   15504     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15505       if (l->sec->flags & SEC_GROUP)
   15506 	{
   15507 	  asection *first = elf_next_in_group (l->sec);
   15508 
   15509 	  if (first != NULL
   15510 	      && elf_next_in_group (first) == first
   15511 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   15512 	    {
   15513 	      sec->output_section = bfd_abs_section_ptr;
   15514 	      sec->kept_section = first;
   15515 	      break;
   15516 	    }
   15517 	}
   15518 
   15519   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   15520      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   15521      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   15522      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   15523      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   15524      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   15525      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   15526      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   15527      The reverse order cannot happen as there is never a bfd with only the
   15528      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   15529      matter as here were are looking only for cross-bfd sections.  */
   15530 
   15531   if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
   15532     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15533       if ((l->sec->flags & SEC_GROUP) == 0
   15534 	  && startswith (l->sec->name, ".gnu.linkonce.t."))
   15535 	{
   15536 	  if (abfd != l->sec->owner)
   15537 	    sec->output_section = bfd_abs_section_ptr;
   15538 	  break;
   15539 	}
   15540 
   15541   /* This is the first section with this name.  Record it.  */
   15542   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   15543     info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
   15544   return sec->output_section == bfd_abs_section_ptr;
   15545 }
   15546 
   15547 bool
   15548 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   15549 {
   15550   return sym->st_shndx == SHN_COMMON;
   15551 }
   15552 
   15553 unsigned int
   15554 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   15555 {
   15556   return SHN_COMMON;
   15557 }
   15558 
   15559 asection *
   15560 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   15561 {
   15562   return bfd_com_section_ptr;
   15563 }
   15564 
   15565 bfd_vma
   15566 _bfd_elf_default_got_elt_size (bfd *abfd,
   15567 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   15568 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   15569 			       bfd *ibfd ATTRIBUTE_UNUSED,
   15570 			       unsigned long symndx ATTRIBUTE_UNUSED)
   15571 {
   15572   elf_backend_data *bed = get_elf_backend_data (abfd);
   15573   return bed->s->arch_size / 8;
   15574 }
   15575 
   15576 /* Routines to support the creation of dynamic relocs.  */
   15577 
   15578 /* Returns the name of the dynamic reloc section associated with SEC.  */
   15579 
   15580 static const char *
   15581 get_dynamic_reloc_section_name (bfd *       abfd,
   15582 				asection *  sec,
   15583 				bool is_rela)
   15584 {
   15585   char *name;
   15586   const char *old_name = bfd_section_name (sec);
   15587   const char *prefix = is_rela ? ".rela" : ".rel";
   15588 
   15589   if (old_name == NULL)
   15590     return NULL;
   15591 
   15592   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   15593   sprintf (name, "%s%s", prefix, old_name);
   15594 
   15595   return name;
   15596 }
   15597 
   15598 /* Returns the dynamic reloc section associated with SEC.
   15599    If necessary compute the name of the dynamic reloc section based
   15600    on SEC's name (looked up in ABFD's string table) and the setting
   15601    of IS_RELA.  */
   15602 
   15603 asection *
   15604 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
   15605 				    asection *sec,
   15606 				    bool is_rela)
   15607 {
   15608   asection *reloc_sec = elf_section_data (sec)->sreloc;
   15609 
   15610   if (reloc_sec == NULL)
   15611     {
   15612       const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15613 
   15614       if (name != NULL)
   15615 	{
   15616 	  reloc_sec = bfd_get_linker_section (abfd, name);
   15617 
   15618 	  if (reloc_sec != NULL)
   15619 	    elf_section_data (sec)->sreloc = reloc_sec;
   15620 	}
   15621     }
   15622 
   15623   return reloc_sec;
   15624 }
   15625 
   15626 /* Returns the dynamic reloc section associated with SEC.  If the
   15627    section does not exist it is created and attached to the DYNOBJ
   15628    bfd and stored in the SRELOC field of SEC's elf_section_data
   15629    structure.
   15630 
   15631    ALIGNMENT is the alignment for the newly created section and
   15632    IS_RELA defines whether the name should be .rela.<SEC's name>
   15633    or .rel.<SEC's name>.  The section name is looked up in the
   15634    string table associated with ABFD.  */
   15635 
   15636 asection *
   15637 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   15638 				     bfd *dynobj,
   15639 				     unsigned int alignment,
   15640 				     bfd *abfd,
   15641 				     bool is_rela)
   15642 {
   15643   asection * reloc_sec = elf_section_data (sec)->sreloc;
   15644 
   15645   if (reloc_sec == NULL)
   15646     {
   15647       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15648 
   15649       if (name == NULL)
   15650 	return NULL;
   15651 
   15652       reloc_sec = bfd_get_linker_section (dynobj, name);
   15653 
   15654       if (reloc_sec == NULL)
   15655 	{
   15656 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   15657 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   15658 	  if ((sec->flags & SEC_ALLOC) != 0)
   15659 	    flags |= SEC_ALLOC | SEC_LOAD;
   15660 
   15661 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   15662 	  if (reloc_sec != NULL)
   15663 	    {
   15664 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   15665 		 name.  Override as it may be wrong, eg. for a user
   15666 		 section named "auto" we'll get ".relauto" which is
   15667 		 seen to be a .rela section.  */
   15668 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   15669 	      if (!bfd_set_section_alignment (reloc_sec, alignment))
   15670 		reloc_sec = NULL;
   15671 	    }
   15672 	}
   15673 
   15674       elf_section_data (sec)->sreloc = reloc_sec;
   15675     }
   15676 
   15677   return reloc_sec;
   15678 }
   15679 
   15680 /* Copy the ELF symbol type and other attributes for a linker script
   15681    assignment from HSRC to HDEST.  Generally this should be treated as
   15682    if we found a strong non-dynamic definition for HDEST (except that
   15683    ld ignores multiple definition errors).  */
   15684 void
   15685 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   15686 				     struct bfd_link_hash_entry *hdest,
   15687 				     struct bfd_link_hash_entry *hsrc)
   15688 {
   15689   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   15690   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   15691   Elf_Internal_Sym isym;
   15692 
   15693   ehdest->type = ehsrc->type;
   15694   ehdest->target_internal = ehsrc->target_internal;
   15695 
   15696   isym.st_other = ehsrc->other;
   15697   elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
   15698 }
   15699 
   15700 /* Append a RELA relocation REL to section S in BFD.  */
   15701 
   15702 void
   15703 _bfd_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15704 {
   15705   elf_backend_data *bed = get_elf_backend_data (abfd);
   15706   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   15707   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   15708   bed->s->swap_reloca_out (abfd, rel, loc);
   15709 }
   15710 
   15711 /* Append a REL relocation REL to section S in BFD.  */
   15712 
   15713 void
   15714 _bfd_elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15715 {
   15716   elf_backend_data *bed = get_elf_backend_data (abfd);
   15717   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   15718   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   15719   bed->s->swap_reloc_out (abfd, rel, loc);
   15720 }
   15721 
   15722 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
   15723 
   15724 struct bfd_link_hash_entry *
   15725 bfd_elf_define_start_stop (struct bfd_link_info *info,
   15726 			   const char *symbol, asection *sec)
   15727 {
   15728   struct elf_link_hash_entry *h;
   15729 
   15730   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
   15731 			    false, false, true);
   15732   /* NB: Common symbols will be turned into definition later.  */
   15733   if (h != NULL
   15734       && !h->root.ldscript_def
   15735       && (h->root.type == bfd_link_hash_undefined
   15736 	  || h->root.type == bfd_link_hash_undefweak
   15737 	  || ((h->ref_regular || h->def_dynamic)
   15738 	      && !h->def_regular
   15739 	      && h->root.type != bfd_link_hash_common)))
   15740     {
   15741       bool was_dynamic = h->ref_dynamic || h->def_dynamic;
   15742       h->verinfo.verdef = NULL;
   15743       h->root.type = bfd_link_hash_defined;
   15744       h->root.u.def.section = sec;
   15745       h->root.u.def.value = 0;
   15746       h->def_regular = 1;
   15747       h->def_dynamic = 0;
   15748       h->start_stop = 1;
   15749       h->u2.start_stop_section = sec;
   15750       if (symbol[0] == '.')
   15751 	{
   15752 	  /* .startof. and .sizeof. symbols are local.  */
   15753 	  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
   15754 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   15755 	}
   15756       else
   15757 	{
   15758 	  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   15759 	    h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
   15760 			| info->start_stop_visibility);
   15761 	  if (was_dynamic)
   15762 	    bfd_elf_link_record_dynamic_symbol (info, h);
   15763 	}
   15764       return &h->root;
   15765     }
   15766   return NULL;
   15767 }
   15768 
   15769 /* Find dynamic relocs for H that apply to read-only sections.  */
   15770 
   15771 asection *
   15772 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
   15773 {
   15774   struct elf_dyn_relocs *p;
   15775 
   15776   for (p = h->dyn_relocs; p != NULL; p = p->next)
   15777     {
   15778       asection *s = p->sec->output_section;
   15779 
   15780       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   15781 	return p->sec;
   15782     }
   15783   return NULL;
   15784 }
   15785 
   15786 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   15787    read-only sections.  */
   15788 
   15789 bool
   15790 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
   15791 {
   15792   asection *sec;
   15793 
   15794   if (h->root.type == bfd_link_hash_indirect)
   15795     return true;
   15796 
   15797   sec = _bfd_elf_readonly_dynrelocs (h);
   15798   if (sec != NULL)
   15799     {
   15800       struct bfd_link_info *info = (struct bfd_link_info *) inf;
   15801 
   15802       info->flags |= DF_TEXTREL;
   15803       /* xgettext:c-format */
   15804       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
   15805 				"in read-only section `%pA'\n"),
   15806 			      sec->owner, h->root.root.string, sec);
   15807 
   15808       if (bfd_link_textrel_check (info))
   15809 	/* xgettext:c-format */
   15810 	info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
   15811 				  "in read-only section `%pA'\n"),
   15812 				sec->owner, h->root.root.string, sec);
   15813 
   15814       /* Not an error, just cut short the traversal.  */
   15815       return false;
   15816     }
   15817   return true;
   15818 }
   15819 
   15820 /* Add dynamic tags.  */
   15821 
   15822 bool
   15823 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
   15824 			   bool need_dynamic_reloc)
   15825 {
   15826   struct elf_link_hash_table *htab = elf_hash_table (info);
   15827 
   15828   if (htab->dynamic_sections_created)
   15829     {
   15830       /* Add some entries to the .dynamic section.  We fill in the
   15831 	 values later, in finish_dynamic_sections, but we must add
   15832 	 the entries now so that we get the correct size for the
   15833 	 .dynamic section.  The DT_DEBUG entry is filled in by the
   15834 	 dynamic linker and used by the debugger.  */
   15835 #define add_dynamic_entry(TAG, VAL) \
   15836   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   15837 
   15838       elf_backend_data *bed = get_elf_backend_data (output_bfd);
   15839 
   15840       if (bfd_link_executable (info))
   15841 	{
   15842 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   15843 	    return false;
   15844 	}
   15845 
   15846       if (htab->dt_pltgot_required || htab->splt->size != 0)
   15847 	{
   15848 	  /* DT_PLTGOT is used by prelink even if there is no PLT
   15849 	     relocation.  */
   15850 	  if (!add_dynamic_entry (DT_PLTGOT, 0))
   15851 	    return false;
   15852 	}
   15853 
   15854       if (htab->dt_jmprel_required || htab->srelplt->size != 0)
   15855 	{
   15856 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
   15857 	      || !add_dynamic_entry (DT_PLTREL,
   15858 				     (bed->rela_plts_and_copies_p
   15859 				      ? DT_RELA : DT_REL))
   15860 	      || !add_dynamic_entry (DT_JMPREL, 0))
   15861 	    return false;
   15862 	}
   15863 
   15864       if (htab->tlsdesc_plt
   15865 	  && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
   15866 	      || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
   15867 	return false;
   15868 
   15869       if (need_dynamic_reloc)
   15870 	{
   15871 	  if (bed->rela_plts_and_copies_p)
   15872 	    {
   15873 	      if (!add_dynamic_entry (DT_RELA, 0)
   15874 		  || !add_dynamic_entry (DT_RELASZ, 0)
   15875 		  || !add_dynamic_entry (DT_RELAENT,
   15876 					 bed->s->sizeof_rela))
   15877 		return false;
   15878 	    }
   15879 	  else
   15880 	    {
   15881 	      if (!add_dynamic_entry (DT_REL, 0)
   15882 		  || !add_dynamic_entry (DT_RELSZ, 0)
   15883 		  || !add_dynamic_entry (DT_RELENT,
   15884 					 bed->s->sizeof_rel))
   15885 		return false;
   15886 	    }
   15887 
   15888 	  /* If any dynamic relocs apply to a read-only section,
   15889 	     then we need a DT_TEXTREL entry.  */
   15890 	  if ((info->flags & DF_TEXTREL) == 0)
   15891 	    elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
   15892 				    info);
   15893 
   15894 	  if ((info->flags & DF_TEXTREL) != 0)
   15895 	    {
   15896 	      if (htab->ifunc_resolvers)
   15897 		info->callbacks->einfo
   15898 		  (_("%P: warning: GNU indirect functions with DT_TEXTREL "
   15899 		     "may result in a segfault at runtime; recompile with %s\n"),
   15900 		   bfd_link_dll (info) ? "-fPIC" : "-fPIE");
   15901 
   15902 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   15903 		return false;
   15904 	    }
   15905 	}
   15906     }
   15907 #undef add_dynamic_entry
   15908 
   15909   return true;
   15910 }
   15911