Home | History | Annotate | Line # | Download | only in bfd
elflink.c revision 1.1.1.13
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2025 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 #if BFD_SUPPORTS_PLUGINS
     31 #include "plugin-api.h"
     32 #include "plugin.h"
     33 #endif
     34 
     35 #include <limits.h>
     36 #ifndef CHAR_BIT
     37 #define CHAR_BIT 8
     38 #endif
     39 
     40 /* This struct is used to pass information to routines called via
     41    elf_link_hash_traverse which must return failure.  */
     42 
     43 struct elf_info_failed
     44 {
     45   struct bfd_link_info *info;
     46   bool failed;
     47 };
     48 
     49 static bool _bfd_elf_fix_symbol_flags
     50   (struct elf_link_hash_entry *, struct elf_info_failed *);
     51 
     52 /* Return false if linker should avoid caching relocation information
     53    and symbol tables of input files in memory.  */
     54 
     55 static bool
     56 _bfd_elf_link_keep_memory (struct bfd_link_info *info)
     57 {
     58 #ifdef USE_MMAP
     59   /* Don't cache symbol nor relocation tables if they are mapped in.
     60      NB: Since the --no-keep-memory linker option causes:
     61 
     62      https://sourceware.org/bugzilla/show_bug.cgi?id=31458
     63 
     64      this is opt-in by each backend.  */
     65   const struct elf_backend_data *bed
     66     = get_elf_backend_data (info->output_bfd);
     67   if (bed != NULL && bed->use_mmap)
     68     return false;
     69 #endif
     70   bfd *abfd;
     71   bfd_size_type size;
     72 
     73   if (!info->keep_memory)
     74     return false;
     75 
     76   if (info->max_cache_size == (bfd_size_type) -1)
     77     return true;
     78 
     79   abfd = info->input_bfds;
     80   size = info->cache_size;
     81   do
     82     {
     83       if (size >= info->max_cache_size)
     84 	{
     85 	  /* Over the limit.  Reduce the memory usage.  */
     86 	  info->keep_memory = false;
     87 	  return false;
     88 	}
     89       if (!abfd)
     90 	break;
     91       size += abfd->alloc_size;
     92       abfd = abfd->link.next;
     93     }
     94   while (1);
     95 
     96   return true;
     97 }
     98 
     99 static struct elf_link_hash_entry *
    100 get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
    101 		     unsigned int                   symndx,
    102 		     unsigned int                   ext_sym_start)
    103 {
    104   if (sym_hashes == NULL
    105       /* Guard against corrupt input.  See PR 32636 for an example.  */
    106       || symndx < ext_sym_start)
    107     return NULL;
    108 
    109   struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
    110 
    111   /* The hash might be empty.  See PR 32641 for an example of this.  */
    112   if (h == NULL)
    113     return NULL;
    114 
    115   while (h->root.type == bfd_link_hash_indirect
    116 	 || h->root.type == bfd_link_hash_warning)
    117     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    118 
    119   return h;
    120 }
    121 
    122 struct elf_link_hash_entry *
    123 _bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
    124 			      unsigned int                   symndx,
    125 			      Elf_Internal_Shdr *            symtab_hdr)
    126 {
    127   if (symtab_hdr == NULL)
    128     return NULL;
    129 
    130   return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info);
    131 }
    132 
    133 static struct elf_link_hash_entry *
    134 get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx)
    135 {
    136   if (cookie == NULL || cookie->sym_hashes == NULL)
    137     return NULL;
    138 
    139   if (r_symndx >= cookie->locsymcount
    140       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
    141     return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff);
    142 
    143   return NULL;
    144 }
    145 
    146 asection *
    147 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
    148 			     unsigned long r_symndx,
    149 			     bool discard)
    150 {
    151   struct elf_link_hash_entry *h;
    152 
    153   h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
    154 
    155   if (h != NULL)
    156     {
    157       if ((h->root.type == bfd_link_hash_defined
    158 	   || h->root.type == bfd_link_hash_defweak)
    159 	   && discarded_section (h->root.u.def.section))
    160 	return h->root.u.def.section;
    161       else
    162 	return NULL;
    163     }
    164 
    165   /* It's not a relocation against a global symbol,
    166      but it could be a relocation against a local
    167      symbol for a discarded section.  */
    168   asection *isec;
    169   Elf_Internal_Sym *isym;
    170 
    171   /* Need to: get the symbol; get the section.  */
    172   isym = &cookie->locsyms[r_symndx];
    173   isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
    174   if (isec != NULL
    175       && discard ? discarded_section (isec) : 1)
    176     return isec;
    177 
    178   return NULL;
    179 }
    180 
    181 /* Define a symbol in a dynamic linkage section.  */
    182 
    183 struct elf_link_hash_entry *
    184 _bfd_elf_define_linkage_sym (bfd *abfd,
    185 			     struct bfd_link_info *info,
    186 			     asection *sec,
    187 			     const char *name)
    188 {
    189   struct elf_link_hash_entry *h;
    190   struct bfd_link_hash_entry *bh;
    191   const struct elf_backend_data *bed;
    192 
    193   h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
    194   if (h != NULL)
    195     {
    196       /* Zap symbol defined in an as-needed lib that wasn't linked.
    197 	 This is a symptom of a larger problem:  Absolute symbols
    198 	 defined in shared libraries can't be overridden, because we
    199 	 lose the link to the bfd which is via the symbol section.  */
    200       h->root.type = bfd_link_hash_new;
    201       bh = &h->root;
    202     }
    203   else
    204     bh = NULL;
    205 
    206   bed = get_elf_backend_data (abfd);
    207   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    208 					 sec, 0, NULL, false, bed->collect,
    209 					 &bh))
    210     return NULL;
    211   h = (struct elf_link_hash_entry *) bh;
    212   BFD_ASSERT (h != NULL);
    213   h->def_regular = 1;
    214   h->non_elf = 0;
    215   h->root.linker_def = 1;
    216   h->type = STT_OBJECT;
    217   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    218     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    219 
    220   (*bed->elf_backend_hide_symbol) (info, h, true);
    221   return h;
    222 }
    223 
    224 bool
    225 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    226 {
    227   flagword flags;
    228   asection *s;
    229   struct elf_link_hash_entry *h;
    230   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    231   struct elf_link_hash_table *htab = elf_hash_table (info);
    232 
    233   /* This function may be called more than once.  */
    234   if (htab->sgot != NULL)
    235     return true;
    236 
    237   flags = bed->dynamic_sec_flags;
    238 
    239   s = bfd_make_section_anyway_with_flags (abfd,
    240 					  (bed->rela_plts_and_copies_p
    241 					   ? ".rela.got" : ".rel.got"),
    242 					  flags | SEC_READONLY);
    243   if (s == NULL
    244       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    245     return false;
    246   htab->srelgot = s;
    247 
    248   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    249   if (s == NULL
    250       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    251     return false;
    252   htab->sgot = s;
    253 
    254   if (bed->want_got_plt)
    255     {
    256       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    257       if (s == NULL
    258 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    259 	return false;
    260       htab->sgotplt = s;
    261     }
    262 
    263   /* The first bit of the global offset table is the header.  */
    264   s->size += bed->got_header_size;
    265 
    266   if (bed->want_got_sym)
    267     {
    268       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    269 	 (or .got.plt) section.  We don't do this in the linker script
    270 	 because we don't want to define the symbol if we are not creating
    271 	 a global offset table.  */
    272       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    273 				       "_GLOBAL_OFFSET_TABLE_");
    274       elf_hash_table (info)->hgot = h;
    275       if (h == NULL)
    276 	return false;
    277     }
    278 
    279   return true;
    280 }
    281 
    282 /* Create a strtab to hold the dynamic symbol names.  */
    284 static bool
    285 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    286 {
    287   struct elf_link_hash_table *hash_table;
    288 
    289   hash_table = elf_hash_table (info);
    290   if (hash_table->dynobj == NULL)
    291     {
    292       /* We may not set dynobj, an input file holding linker created
    293 	 dynamic sections to abfd, which may be a dynamic object with
    294 	 its own dynamic sections.  We need to find a normal input file
    295 	 to hold linker created sections if possible.  */
    296       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
    297 	{
    298 	  bfd *ibfd;
    299 	  asection *s;
    300 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
    301 	    if ((ibfd->flags
    302 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
    303 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
    304 		&& elf_object_id (ibfd) == elf_hash_table_id (hash_table)
    305 		&& !((s = ibfd->sections) != NULL
    306 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
    307 	      {
    308 		abfd = ibfd;
    309 		break;
    310 	      }
    311 	}
    312       hash_table->dynobj = abfd;
    313     }
    314 
    315   if (hash_table->dynstr == NULL)
    316     {
    317       hash_table->dynstr = _bfd_elf_strtab_init ();
    318       if (hash_table->dynstr == NULL)
    319 	return false;
    320     }
    321   return true;
    322 }
    323 
    324 /* Create some sections which will be filled in with dynamic linking
    325    information.  ABFD is an input file which requires dynamic sections
    326    to be created.  The dynamic sections take up virtual memory space
    327    when the final executable is run, so we need to create them before
    328    addresses are assigned to the output sections.  We work out the
    329    actual contents and size of these sections later.  */
    330 
    331 bool
    332 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    333 {
    334   flagword flags;
    335   asection *s;
    336   const struct elf_backend_data *bed;
    337   struct elf_link_hash_entry *h;
    338 
    339   if (! is_elf_hash_table (info->hash))
    340     return false;
    341 
    342   if (elf_hash_table (info)->dynamic_sections_created)
    343     return true;
    344 
    345   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    346     return false;
    347 
    348   abfd = elf_hash_table (info)->dynobj;
    349   bed = get_elf_backend_data (abfd);
    350 
    351   flags = bed->dynamic_sec_flags;
    352 
    353   /* A dynamically linked executable has a .interp section, but a
    354      shared library does not.  */
    355   if (bfd_link_executable (info) && !info->nointerp)
    356     {
    357       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    358 					      flags | SEC_READONLY);
    359       if (s == NULL)
    360 	return false;
    361     }
    362 
    363   /* Create sections to hold version informations.  These are removed
    364      if they are not needed.  */
    365   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    366 					  flags | SEC_READONLY);
    367   if (s == NULL
    368       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    369     return false;
    370 
    371   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    372 					  flags | SEC_READONLY);
    373   if (s == NULL
    374       || !bfd_set_section_alignment (s, 1))
    375     return false;
    376 
    377   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    378 					  flags | SEC_READONLY);
    379   if (s == NULL
    380       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    381     return false;
    382 
    383   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    384 					  flags | SEC_READONLY);
    385   if (s == NULL
    386       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    387     return false;
    388   elf_hash_table (info)->dynsym = s;
    389 
    390   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    391 					  flags | SEC_READONLY);
    392   if (s == NULL)
    393     return false;
    394 
    395   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    396   if (s == NULL
    397       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    398     return false;
    399   elf_hash_table (info)->dynamic = s;
    400 
    401   /* The special symbol _DYNAMIC is always set to the start of the
    402      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    403      only want to define it if we are, in fact, creating a .dynamic
    404      section.  We don't want to define it if there is no .dynamic
    405      section, since on some ELF platforms the start up code examines it
    406      to decide how to initialize the process.  */
    407   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    408   elf_hash_table (info)->hdynamic = h;
    409   if (h == NULL)
    410     return false;
    411 
    412   if (info->emit_hash)
    413     {
    414       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    415 					      flags | SEC_READONLY);
    416       if (s == NULL
    417 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    418 	return false;
    419       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    420     }
    421 
    422   if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
    423     {
    424       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    425 					      flags | SEC_READONLY);
    426       if (s == NULL
    427 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    428 	return false;
    429       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    430 	 4 32-bit words followed by variable count of 64-bit words, then
    431 	 variable count of 32-bit words.  */
    432       if (bed->s->arch_size == 64)
    433 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    434       else
    435 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    436     }
    437 
    438   if (info->enable_dt_relr)
    439     {
    440       s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
    441 					      flags | SEC_READONLY);
    442       if (s == NULL
    443 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    444 	return false;
    445       elf_hash_table (info)->srelrdyn = s;
    446     }
    447 
    448   /* Let the backend create the rest of the sections.  This lets the
    449      backend set the right flags.  The backend will normally create
    450      the .got and .plt sections.  */
    451   if (bed->elf_backend_create_dynamic_sections == NULL
    452       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    453     return false;
    454 
    455   elf_hash_table (info)->dynamic_sections_created = true;
    456 
    457   return true;
    458 }
    459 
    460 /* Create dynamic sections when linking against a dynamic object.  */
    461 
    462 bool
    463 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    464 {
    465   flagword flags, pltflags;
    466   struct elf_link_hash_entry *h;
    467   asection *s;
    468   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    469   struct elf_link_hash_table *htab = elf_hash_table (info);
    470 
    471   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    472      .rel[a].bss sections.  */
    473   flags = bed->dynamic_sec_flags;
    474 
    475   pltflags = flags;
    476   if (bed->plt_not_loaded)
    477     /* We do not clear SEC_ALLOC here because we still want the OS to
    478        allocate space for the section; it's just that there's nothing
    479        to read in from the object file.  */
    480     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    481   else
    482     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    483   if (bed->plt_readonly)
    484     pltflags |= SEC_READONLY;
    485 
    486   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    487   if (s == NULL
    488       || !bfd_set_section_alignment (s, bed->plt_alignment))
    489     return false;
    490   htab->splt = s;
    491 
    492   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    493      .plt section.  */
    494   if (bed->want_plt_sym)
    495     {
    496       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    497 				       "_PROCEDURE_LINKAGE_TABLE_");
    498       elf_hash_table (info)->hplt = h;
    499       if (h == NULL)
    500 	return false;
    501     }
    502 
    503   s = bfd_make_section_anyway_with_flags (abfd,
    504 					  (bed->rela_plts_and_copies_p
    505 					   ? ".rela.plt" : ".rel.plt"),
    506 					  flags | SEC_READONLY);
    507   if (s == NULL
    508       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    509     return false;
    510   htab->srelplt = s;
    511 
    512   if (! _bfd_elf_create_got_section (abfd, info))
    513     return false;
    514 
    515   if (bed->want_dynbss)
    516     {
    517       /* The .dynbss section is a place to put symbols which are defined
    518 	 by dynamic objects, are referenced by regular objects, and are
    519 	 not functions.  We must allocate space for them in the process
    520 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    521 	 initialize them at run time.  The linker script puts the .dynbss
    522 	 section into the .bss section of the final image.  */
    523       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    524 					      SEC_ALLOC | SEC_LINKER_CREATED);
    525       if (s == NULL)
    526 	return false;
    527       htab->sdynbss = s;
    528 
    529       if (bed->want_dynrelro)
    530 	{
    531 	  /* Similarly, but for symbols that were originally in read-only
    532 	     sections.  This section doesn't really need to have contents,
    533 	     but make it like other .data.rel.ro sections.  */
    534 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
    535 						  flags);
    536 	  if (s == NULL)
    537 	    return false;
    538 	  htab->sdynrelro = s;
    539 	}
    540 
    541       /* The .rel[a].bss section holds copy relocs.  This section is not
    542 	 normally needed.  We need to create it here, though, so that the
    543 	 linker will map it to an output section.  We can't just create it
    544 	 only if we need it, because we will not know whether we need it
    545 	 until we have seen all the input files, and the first time the
    546 	 main linker code calls BFD after examining all the input files
    547 	 (size_dynamic_sections) the input sections have already been
    548 	 mapped to the output sections.  If the section turns out not to
    549 	 be needed, we can discard it later.  We will never need this
    550 	 section when generating a shared object, since they do not use
    551 	 copy relocs.  */
    552       if (bfd_link_executable (info))
    553 	{
    554 	  s = bfd_make_section_anyway_with_flags (abfd,
    555 						  (bed->rela_plts_and_copies_p
    556 						   ? ".rela.bss" : ".rel.bss"),
    557 						  flags | SEC_READONLY);
    558 	  if (s == NULL
    559 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
    560 	    return false;
    561 	  htab->srelbss = s;
    562 
    563 	  if (bed->want_dynrelro)
    564 	    {
    565 	      s = (bfd_make_section_anyway_with_flags
    566 		   (abfd, (bed->rela_plts_and_copies_p
    567 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
    568 		    flags | SEC_READONLY));
    569 	      if (s == NULL
    570 		  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    571 		return false;
    572 	      htab->sreldynrelro = s;
    573 	    }
    574 	}
    575     }
    576 
    577   return true;
    578 }
    579 
    580 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    582    read the input files, since we need to have a list of all of them
    583    before we can determine the final sizes of the output sections.
    584    Note that we may actually call this function even though we are not
    585    going to output any dynamic symbols; in some cases we know that a
    586    symbol should be in the dynamic symbol table, but only if there is
    587    one.  */
    588 
    589 bool
    590 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    591 				    struct elf_link_hash_entry *h)
    592 {
    593   if (h->dynindx == -1)
    594     {
    595       struct elf_strtab_hash *dynstr;
    596       char *p;
    597       const char *name;
    598       size_t indx;
    599 
    600       if (h->root.type == bfd_link_hash_defined
    601 	  || h->root.type == bfd_link_hash_defweak)
    602 	{
    603 	  /* An IR symbol should not be made dynamic.  */
    604 	  if (h->root.u.def.section != NULL
    605 	      && h->root.u.def.section->owner != NULL
    606 	      && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
    607 	    return true;
    608 	}
    609 
    610       /* XXX: The ABI draft says the linker must turn hidden and
    611 	 internal symbols into STB_LOCAL symbols when producing the
    612 	 DSO. However, if ld.so honors st_other in the dynamic table,
    613 	 this would not be necessary.  */
    614       switch (ELF_ST_VISIBILITY (h->other))
    615 	{
    616 	case STV_INTERNAL:
    617 	case STV_HIDDEN:
    618 	  if (h->root.type != bfd_link_hash_undefined
    619 	      && h->root.type != bfd_link_hash_undefweak)
    620 	    {
    621 	      h->forced_local = 1;
    622 	      return true;
    623 	    }
    624 
    625 	default:
    626 	  break;
    627 	}
    628 
    629       h->dynindx = elf_hash_table (info)->dynsymcount;
    630       ++elf_hash_table (info)->dynsymcount;
    631 
    632       dynstr = elf_hash_table (info)->dynstr;
    633       if (dynstr == NULL)
    634 	{
    635 	  /* Create a strtab to hold the dynamic symbol names.  */
    636 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    637 	  if (dynstr == NULL)
    638 	    return false;
    639 	}
    640 
    641       char *unversioned_name = NULL;
    642 
    643       /* We don't put any version information in the dynamic string
    644 	 table.  */
    645       name = h->root.root.string;
    646       p = strchr (name, ELF_VER_CHR);
    647       if (p != NULL)
    648 	{
    649 	  unversioned_name = bfd_malloc (p - name + 1);
    650 	  memcpy (unversioned_name, name, p - name);
    651 	  unversioned_name[p - name] = 0;
    652 	  name = unversioned_name;
    653 	}
    654 
    655       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    656 
    657       if (p != NULL)
    658 	free (unversioned_name);
    659 
    660       if (indx == (size_t) -1)
    661 	return false;
    662       h->dynstr_index = indx;
    663     }
    664 
    665   return true;
    666 }
    667 
    668 /* Mark a symbol dynamic.  */
    670 
    671 static void
    672 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    673 				  struct elf_link_hash_entry *h,
    674 				  Elf_Internal_Sym *sym)
    675 {
    676   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    677 
    678   /* It may be called more than once on the same H.  */
    679   if(h->dynamic || bfd_link_relocatable (info))
    680     return;
    681 
    682   if ((info->dynamic_data
    683        && (h->type == STT_OBJECT
    684 	   || h->type == STT_COMMON
    685 	   || (sym != NULL
    686 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
    687 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
    688       || (d != NULL
    689 	  && h->non_elf
    690 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    691     {
    692       h->dynamic = 1;
    693       /* NB: If a symbol is made dynamic by --dynamic-list, it has
    694 	 non-IR reference.  */
    695       h->root.non_ir_ref_dynamic = 1;
    696     }
    697 }
    698 
    699 /* Record an assignment to a symbol made by a linker script.  We need
    700    this in case some dynamic object refers to this symbol.  */
    701 
    702 bool
    703 bfd_elf_record_link_assignment (bfd *output_bfd,
    704 				struct bfd_link_info *info,
    705 				const char *name,
    706 				bool provide,
    707 				bool hidden)
    708 {
    709   struct elf_link_hash_entry *h, *hv;
    710   struct elf_link_hash_table *htab;
    711   const struct elf_backend_data *bed;
    712 
    713   if (!is_elf_hash_table (info->hash))
    714     return true;
    715 
    716   htab = elf_hash_table (info);
    717   h = elf_link_hash_lookup (htab, name, !provide, true, false);
    718   if (h == NULL)
    719     return provide;
    720 
    721   if (h->root.type == bfd_link_hash_warning)
    722     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    723 
    724   if (h->versioned == unknown)
    725     {
    726       /* Set versioned if symbol version is unknown.  */
    727       char *version = strrchr (name, ELF_VER_CHR);
    728       if (version)
    729 	{
    730 	  if (version > name && version[-1] != ELF_VER_CHR)
    731 	    h->versioned = versioned_hidden;
    732 	  else
    733 	    h->versioned = versioned;
    734 	}
    735     }
    736 
    737   /* Symbols defined in a linker script but not referenced anywhere
    738      else will have non_elf set.  */
    739   if (h->non_elf)
    740     {
    741       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    742       h->non_elf = 0;
    743     }
    744 
    745   switch (h->root.type)
    746     {
    747     case bfd_link_hash_defined:
    748     case bfd_link_hash_defweak:
    749     case bfd_link_hash_common:
    750       break;
    751     case bfd_link_hash_undefweak:
    752     case bfd_link_hash_undefined:
    753       /* Since we're defining the symbol, don't let it seem to have not
    754 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    755 	 may depend on this.  */
    756       h->root.type = bfd_link_hash_new;
    757       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    758 	bfd_link_repair_undef_list (&htab->root);
    759       break;
    760     case bfd_link_hash_new:
    761       break;
    762     case bfd_link_hash_indirect:
    763       /* We had a versioned symbol in a dynamic library.  We make the
    764 	 the versioned symbol point to this one.  */
    765       bed = get_elf_backend_data (output_bfd);
    766       hv = h;
    767       while (hv->root.type == bfd_link_hash_indirect
    768 	     || hv->root.type == bfd_link_hash_warning)
    769 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    770       /* We don't need to update h->root.u since linker will set them
    771 	 later.  */
    772       h->root.type = bfd_link_hash_undefined;
    773       hv->root.type = bfd_link_hash_indirect;
    774       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    775       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    776       break;
    777     default:
    778       BFD_FAIL ();
    779       return false;
    780     }
    781 
    782   /* If this symbol is being provided by the linker script, and it is
    783      currently defined by a dynamic object, but not by a regular
    784      object, then mark it as undefined so that the generic linker will
    785      force the correct value.  */
    786   if (provide
    787       && h->def_dynamic
    788       && !h->def_regular)
    789     h->root.type = bfd_link_hash_undefined;
    790 
    791   /* If this symbol is currently defined by a dynamic object, but not
    792      by a regular object, then clear out any version information because
    793      the symbol will not be associated with the dynamic object any
    794      more.  */
    795   if (h->def_dynamic && !h->def_regular)
    796     h->verinfo.verdef = NULL;
    797 
    798   /* Make sure this symbol is not garbage collected.  */
    799   h->mark = 1;
    800 
    801   h->def_regular = 1;
    802 
    803   if (hidden)
    804     {
    805       bed = get_elf_backend_data (output_bfd);
    806       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    807 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    808       (*bed->elf_backend_hide_symbol) (info, h, true);
    809     }
    810 
    811   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    812      and executables.  */
    813   if (!bfd_link_relocatable (info)
    814       && h->dynindx != -1
    815       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    816 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    817     h->forced_local = 1;
    818 
    819   if ((h->def_dynamic
    820        || h->ref_dynamic
    821        || bfd_link_dll (info))
    822       && !h->forced_local
    823       && h->dynindx == -1)
    824     {
    825       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    826 	return false;
    827 
    828       /* If this is a weak defined symbol, and we know a corresponding
    829 	 real symbol from the same dynamic object, make sure the real
    830 	 symbol is also made into a dynamic symbol.  */
    831       if (h->is_weakalias)
    832 	{
    833 	  struct elf_link_hash_entry *def = weakdef (h);
    834 
    835 	  if (def->dynindx == -1
    836 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
    837 	    return false;
    838 	}
    839     }
    840 
    841   return true;
    842 }
    843 
    844 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    845    success, and 2 on a failure caused by attempting to record a symbol
    846    in a discarded section, eg. a discarded link-once section symbol.  */
    847 
    848 int
    849 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    850 					  bfd *input_bfd,
    851 					  long input_indx)
    852 {
    853   size_t amt;
    854   struct elf_link_local_dynamic_entry *entry;
    855   struct elf_link_hash_table *eht;
    856   struct elf_strtab_hash *dynstr;
    857   size_t dynstr_index;
    858   char *name;
    859   Elf_External_Sym_Shndx eshndx;
    860   char esym[sizeof (Elf64_External_Sym)];
    861 
    862   if (! is_elf_hash_table (info->hash))
    863     return 0;
    864 
    865   /* See if the entry exists already.  */
    866   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    867     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    868       return 1;
    869 
    870   amt = sizeof (*entry);
    871   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    872   if (entry == NULL)
    873     return 0;
    874 
    875   /* Go find the symbol, so that we can find it's name.  */
    876   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    877 			     1, input_indx, &entry->isym, esym, &eshndx))
    878     {
    879       bfd_release (input_bfd, entry);
    880       return 0;
    881     }
    882 
    883   if (entry->isym.st_shndx != SHN_UNDEF
    884       && entry->isym.st_shndx < SHN_LORESERVE)
    885     {
    886       asection *s;
    887 
    888       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    889       if (s == NULL || bfd_is_abs_section (s->output_section))
    890 	{
    891 	  /* We can still bfd_release here as nothing has done another
    892 	     bfd_alloc.  We can't do this later in this function.  */
    893 	  bfd_release (input_bfd, entry);
    894 	  return 2;
    895 	}
    896     }
    897 
    898   name = (bfd_elf_string_from_elf_section
    899 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    900 	   entry->isym.st_name));
    901 
    902   dynstr = elf_hash_table (info)->dynstr;
    903   if (dynstr == NULL)
    904     {
    905       /* Create a strtab to hold the dynamic symbol names.  */
    906       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    907       if (dynstr == NULL)
    908 	return 0;
    909     }
    910 
    911   dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
    912   if (dynstr_index == (size_t) -1)
    913     return 0;
    914   entry->isym.st_name = dynstr_index;
    915 
    916   eht = elf_hash_table (info);
    917 
    918   entry->next = eht->dynlocal;
    919   eht->dynlocal = entry;
    920   entry->input_bfd = input_bfd;
    921   entry->input_indx = input_indx;
    922   eht->dynsymcount++;
    923 
    924   /* Whatever binding the symbol had before, it's now local.  */
    925   entry->isym.st_info
    926     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    927 
    928   /* The dynindx will be set at the end of size_dynamic_sections.  */
    929 
    930   return 1;
    931 }
    932 
    933 /* Return the dynindex of a local dynamic symbol.  */
    934 
    935 long
    936 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    937 				    bfd *input_bfd,
    938 				    long input_indx)
    939 {
    940   struct elf_link_local_dynamic_entry *e;
    941 
    942   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    943     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    944       return e->dynindx;
    945   return -1;
    946 }
    947 
    948 /* This function is used to renumber the dynamic symbols, if some of
    949    them are removed because they are marked as local.  This is called
    950    via elf_link_hash_traverse.  */
    951 
    952 static bool
    953 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    954 				      void *data)
    955 {
    956   size_t *count = (size_t *) data;
    957 
    958   if (h->forced_local)
    959     return true;
    960 
    961   if (h->dynindx != -1)
    962     h->dynindx = ++(*count);
    963 
    964   return true;
    965 }
    966 
    967 
    968 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    969    STB_LOCAL binding.  */
    970 
    971 static bool
    972 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    973 					    void *data)
    974 {
    975   size_t *count = (size_t *) data;
    976 
    977   if (!h->forced_local)
    978     return true;
    979 
    980   if (h->dynindx != -1)
    981     h->dynindx = ++(*count);
    982 
    983   return true;
    984 }
    985 
    986 /* Return true if the dynamic symbol for a given section should be
    987    omitted when creating a shared library.  */
    988 bool
    989 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
    990 				      struct bfd_link_info *info,
    991 				      asection *p)
    992 {
    993   struct elf_link_hash_table *htab;
    994   asection *ip;
    995 
    996   switch (elf_section_data (p)->this_hdr.sh_type)
    997     {
    998     case SHT_PROGBITS:
    999     case SHT_NOBITS:
   1000       /* If sh_type is yet undecided, assume it could be
   1001 	 SHT_PROGBITS/SHT_NOBITS.  */
   1002     case SHT_NULL:
   1003       htab = elf_hash_table (info);
   1004       if (htab->text_index_section != NULL)
   1005 	return p != htab->text_index_section && p != htab->data_index_section;
   1006 
   1007       return (htab->dynobj != NULL
   1008 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
   1009 	      && ip->output_section == p);
   1010 
   1011       /* There shouldn't be section relative relocations
   1012 	 against any other section.  */
   1013     default:
   1014       return true;
   1015     }
   1016 }
   1017 
   1018 bool
   1019 _bfd_elf_omit_section_dynsym_all
   1020     (bfd *output_bfd ATTRIBUTE_UNUSED,
   1021      struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1022      asection *p ATTRIBUTE_UNUSED)
   1023 {
   1024   return true;
   1025 }
   1026 
   1027 /* Assign dynsym indices.  In a shared library we generate a section
   1028    symbol for each output section, which come first.  Next come symbols
   1029    which have been forced to local binding.  Then all of the back-end
   1030    allocated local dynamic syms, followed by the rest of the global
   1031    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
   1032    (This prevents the early call before elf_backend_init_index_section
   1033    and strip_excluded_output_sections setting dynindx for sections
   1034    that are stripped.)  */
   1035 
   1036 static unsigned long
   1037 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
   1038 				struct bfd_link_info *info,
   1039 				unsigned long *section_sym_count)
   1040 {
   1041   unsigned long dynsymcount = 0;
   1042   bool do_sec = section_sym_count != NULL;
   1043 
   1044   if (bfd_link_pic (info)
   1045       || elf_hash_table (info)->is_relocatable_executable)
   1046     {
   1047       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   1048       asection *p;
   1049       for (p = output_bfd->sections; p ; p = p->next)
   1050 	if ((p->flags & SEC_EXCLUDE) == 0
   1051 	    && (p->flags & SEC_ALLOC) != 0
   1052 	    && elf_hash_table (info)->dynamic_relocs
   1053 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   1054 	  {
   1055 	    ++dynsymcount;
   1056 	    if (do_sec)
   1057 	      elf_section_data (p)->dynindx = dynsymcount;
   1058 	  }
   1059 	else if (do_sec)
   1060 	  elf_section_data (p)->dynindx = 0;
   1061     }
   1062   if (do_sec)
   1063     *section_sym_count = dynsymcount;
   1064 
   1065   elf_link_hash_traverse (elf_hash_table (info),
   1066 			  elf_link_renumber_local_hash_table_dynsyms,
   1067 			  &dynsymcount);
   1068 
   1069   if (elf_hash_table (info)->dynlocal)
   1070     {
   1071       struct elf_link_local_dynamic_entry *p;
   1072       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
   1073 	p->dynindx = ++dynsymcount;
   1074     }
   1075   elf_hash_table (info)->local_dynsymcount = dynsymcount;
   1076 
   1077   elf_link_hash_traverse (elf_hash_table (info),
   1078 			  elf_link_renumber_hash_table_dynsyms,
   1079 			  &dynsymcount);
   1080 
   1081   /* There is an unused NULL entry at the head of the table which we
   1082      must account for in our count even if the table is empty since it
   1083      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
   1084      .dynamic section.  */
   1085   dynsymcount++;
   1086 
   1087   elf_hash_table (info)->dynsymcount = dynsymcount;
   1088   return dynsymcount;
   1089 }
   1090 
   1091 /* Merge st_other field.  */
   1092 
   1093 static void
   1094 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
   1095 		    unsigned int st_other, asection *sec,
   1096 		    bool definition, bool dynamic)
   1097 {
   1098   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1099 
   1100   /* If st_other has a processor-specific meaning, specific
   1101      code might be needed here.  */
   1102   if (bed->elf_backend_merge_symbol_attribute)
   1103     (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
   1104 						dynamic);
   1105 
   1106   if (!dynamic)
   1107     {
   1108       unsigned symvis = ELF_ST_VISIBILITY (st_other);
   1109       unsigned hvis = ELF_ST_VISIBILITY (h->other);
   1110 
   1111       /* Keep the most constraining visibility.  Leave the remainder
   1112 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
   1113       if (symvis - 1 < hvis - 1)
   1114 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
   1115     }
   1116   else if (definition
   1117 	   && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
   1118 	   && (sec->flags & SEC_READONLY) == 0)
   1119     h->protected_def = 1;
   1120 }
   1121 
   1122 /* This function is called when we want to merge a new symbol with an
   1123    existing symbol.  It handles the various cases which arise when we
   1124    find a definition in a dynamic object, or when there is already a
   1125    definition in a dynamic object.  The new symbol is described by
   1126    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
   1127    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
   1128    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
   1129    of an old common symbol.  We set OVERRIDE if the old symbol is
   1130    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
   1131    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
   1132    to change.  By OK to change, we mean that we shouldn't warn if the
   1133    type or size does change.  */
   1134 
   1135 static bool
   1136 _bfd_elf_merge_symbol (bfd *abfd,
   1137 		       struct bfd_link_info *info,
   1138 		       const char *name,
   1139 		       Elf_Internal_Sym *sym,
   1140 		       asection **psec,
   1141 		       bfd_vma *pvalue,
   1142 		       struct elf_link_hash_entry **sym_hash,
   1143 		       bfd **poldbfd,
   1144 		       bool *pold_weak,
   1145 		       unsigned int *pold_alignment,
   1146 		       bool *skip,
   1147 		       bfd **override,
   1148 		       bool *type_change_ok,
   1149 		       bool *size_change_ok,
   1150 		       bool *matched)
   1151 {
   1152   asection *sec, *oldsec;
   1153   struct elf_link_hash_entry *h;
   1154   struct elf_link_hash_entry *hi;
   1155   struct elf_link_hash_entry *flip;
   1156   int bind;
   1157   bfd *oldbfd;
   1158   bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   1159   bool newweak, oldweak, newfunc, oldfunc;
   1160   const struct elf_backend_data *bed;
   1161   char *new_version;
   1162   bool default_sym = *matched;
   1163   struct elf_link_hash_table *htab;
   1164 
   1165   *skip = false;
   1166   *override = NULL;
   1167 
   1168   sec = *psec;
   1169   bind = ELF_ST_BIND (sym->st_info);
   1170 
   1171   if (! bfd_is_und_section (sec))
   1172     h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
   1173   else
   1174     h = ((struct elf_link_hash_entry *)
   1175 	 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
   1176   if (h == NULL)
   1177     return false;
   1178   *sym_hash = h;
   1179 
   1180   bed = get_elf_backend_data (abfd);
   1181 
   1182   /* NEW_VERSION is the symbol version of the new symbol.  */
   1183   if (h->versioned != unversioned)
   1184     {
   1185       /* Symbol version is unknown or versioned.  */
   1186       new_version = strrchr (name, ELF_VER_CHR);
   1187       if (new_version)
   1188 	{
   1189 	  if (h->versioned == unknown)
   1190 	    {
   1191 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1192 		h->versioned = versioned_hidden;
   1193 	      else
   1194 		h->versioned = versioned;
   1195 	    }
   1196 	  new_version += 1;
   1197 	  if (new_version[0] == '\0')
   1198 	    new_version = NULL;
   1199 	}
   1200       else
   1201 	h->versioned = unversioned;
   1202     }
   1203   else
   1204     new_version = NULL;
   1205 
   1206   /* For merging, we only care about real symbols.  But we need to make
   1207      sure that indirect symbol dynamic flags are updated.  */
   1208   hi = h;
   1209   while (h->root.type == bfd_link_hash_indirect
   1210 	 || h->root.type == bfd_link_hash_warning)
   1211     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1212 
   1213   if (!*matched)
   1214     {
   1215       if (hi == h || h->root.type == bfd_link_hash_new)
   1216 	*matched = true;
   1217       else
   1218 	{
   1219 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1220 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1221 	     true if the new symbol is only visible to the symbol with
   1222 	     the same symbol version.  */
   1223 	  bool old_hidden = h->versioned == versioned_hidden;
   1224 	  bool new_hidden = hi->versioned == versioned_hidden;
   1225 	  if (!old_hidden && !new_hidden)
   1226 	    /* The new symbol matches the existing symbol if both
   1227 	       aren't hidden.  */
   1228 	    *matched = true;
   1229 	  else
   1230 	    {
   1231 	      /* OLD_VERSION is the symbol version of the existing
   1232 		 symbol. */
   1233 	      char *old_version;
   1234 
   1235 	      if (h->versioned >= versioned)
   1236 		old_version = strrchr (h->root.root.string,
   1237 				       ELF_VER_CHR) + 1;
   1238 	      else
   1239 		 old_version = NULL;
   1240 
   1241 	      /* The new symbol matches the existing symbol if they
   1242 		 have the same symbol version.  */
   1243 	      *matched = (old_version == new_version
   1244 			  || (old_version != NULL
   1245 			      && new_version != NULL
   1246 			      && strcmp (old_version, new_version) == 0));
   1247 	    }
   1248 	}
   1249     }
   1250 
   1251   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1252      existing symbol.  */
   1253 
   1254   oldbfd = NULL;
   1255   oldsec = NULL;
   1256   switch (h->root.type)
   1257     {
   1258     default:
   1259       break;
   1260 
   1261     case bfd_link_hash_undefined:
   1262     case bfd_link_hash_undefweak:
   1263       oldbfd = h->root.u.undef.abfd;
   1264       break;
   1265 
   1266     case bfd_link_hash_defined:
   1267     case bfd_link_hash_defweak:
   1268       oldbfd = h->root.u.def.section->owner;
   1269       oldsec = h->root.u.def.section;
   1270       break;
   1271 
   1272     case bfd_link_hash_common:
   1273       oldbfd = h->root.u.c.p->section->owner;
   1274       oldsec = h->root.u.c.p->section;
   1275       if (pold_alignment)
   1276 	*pold_alignment = h->root.u.c.p->alignment_power;
   1277       break;
   1278     }
   1279   if (poldbfd && *poldbfd == NULL)
   1280     *poldbfd = oldbfd;
   1281 
   1282   /* Differentiate strong and weak symbols.  */
   1283   newweak = bind == STB_WEAK;
   1284   oldweak = (h->root.type == bfd_link_hash_defweak
   1285 	     || h->root.type == bfd_link_hash_undefweak);
   1286   if (pold_weak)
   1287     *pold_weak = oldweak;
   1288 
   1289   /* We have to check it for every instance since the first few may be
   1290      references and not all compilers emit symbol type for undefined
   1291      symbols.  */
   1292   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1293 
   1294   htab = elf_hash_table (info);
   1295 
   1296   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1297      respectively, is from a dynamic object.  */
   1298 
   1299   newdyn = (abfd->flags & DYNAMIC) != 0;
   1300 
   1301   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1302      syms and defined syms in dynamic libraries respectively.
   1303      ref_dynamic on the other hand can be set for a symbol defined in
   1304      a dynamic library, and def_dynamic may not be set;  When the
   1305      definition in a dynamic lib is overridden by a definition in the
   1306      executable use of the symbol in the dynamic lib becomes a
   1307      reference to the executable symbol.  */
   1308   if (newdyn)
   1309     {
   1310       if (bfd_is_und_section (sec))
   1311 	{
   1312 	  if (bind != STB_WEAK)
   1313 	    {
   1314 	      h->ref_dynamic_nonweak = 1;
   1315 	      hi->ref_dynamic_nonweak = 1;
   1316 	    }
   1317 	}
   1318       else
   1319 	{
   1320 	  /* Update the existing symbol only if they match. */
   1321 	  if (*matched)
   1322 	    h->dynamic_def = 1;
   1323 	  hi->dynamic_def = 1;
   1324 	}
   1325     }
   1326 
   1327   /* If we just created the symbol, mark it as being an ELF symbol.
   1328      Other than that, there is nothing to do--there is no merge issue
   1329      with a newly defined symbol--so we just return.  */
   1330 
   1331   if (h->root.type == bfd_link_hash_new)
   1332     {
   1333       h->non_elf = 0;
   1334       return true;
   1335     }
   1336 
   1337   /* In cases involving weak versioned symbols, we may wind up trying
   1338      to merge a symbol with itself.  Catch that here, to avoid the
   1339      confusion that results if we try to override a symbol with
   1340      itself.  The additional tests catch cases like
   1341      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1342      dynamic object, which we do want to handle here.  */
   1343   if (abfd == oldbfd
   1344       && (newweak || oldweak)
   1345       && ((abfd->flags & DYNAMIC) == 0
   1346 	  || !h->def_regular))
   1347     return true;
   1348 
   1349   olddyn = false;
   1350   if (oldbfd != NULL)
   1351     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1352   else if (oldsec != NULL)
   1353     {
   1354       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1355 	 indices used by MIPS ELF.  */
   1356       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1357     }
   1358 
   1359   /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
   1360   if (!htab->handling_dt_needed
   1361       && oldbfd != NULL
   1362       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
   1363     {
   1364       if (newdyn != olddyn)
   1365 	{
   1366 	  /* Handle a case where plugin_notice won't be called and thus
   1367 	     won't set the non_ir_ref flags on the first pass over
   1368 	     symbols.  */
   1369 	  h->root.non_ir_ref_dynamic = true;
   1370 	  hi->root.non_ir_ref_dynamic = true;
   1371 	}
   1372       else if ((oldbfd->flags & BFD_PLUGIN) != 0
   1373 	       && hi->root.type == bfd_link_hash_indirect)
   1374 	{
   1375 	  /* Change indirect symbol from IR to undefined.  */
   1376 	  hi->root.type = bfd_link_hash_undefined;
   1377 	  hi->root.u.undef.abfd = oldbfd;
   1378 	}
   1379     }
   1380 
   1381   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1382      respectively, appear to be a definition rather than reference.  */
   1383 
   1384   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1385 
   1386   olddef = (h->root.type != bfd_link_hash_undefined
   1387 	    && h->root.type != bfd_link_hash_undefweak
   1388 	    && h->root.type != bfd_link_hash_common);
   1389 
   1390   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1391      respectively, appear to be a function.  */
   1392 
   1393   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1394 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1395 
   1396   oldfunc = (h->type != STT_NOTYPE
   1397 	     && bed->is_function_type (h->type));
   1398 
   1399   if (!(newfunc && oldfunc)
   1400       && ELF_ST_TYPE (sym->st_info) != h->type
   1401       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1402       && h->type != STT_NOTYPE
   1403       && (newdef || bfd_is_com_section (sec))
   1404       && (olddef || h->root.type == bfd_link_hash_common))
   1405     {
   1406       /* If creating a default indirect symbol ("foo" or "foo@") from
   1407 	 a dynamic versioned definition ("foo@@") skip doing so if
   1408 	 there is an existing regular definition with a different
   1409 	 type.  We don't want, for example, a "time" variable in the
   1410 	 executable overriding a "time" function in a shared library.  */
   1411       if (newdyn
   1412 	  && !olddyn)
   1413 	{
   1414 	  *skip = true;
   1415 	  return true;
   1416 	}
   1417 
   1418       /* When adding a symbol from a regular object file after we have
   1419 	 created indirect symbols, undo the indirection and any
   1420 	 dynamic state.  */
   1421       if (hi != h
   1422 	  && !newdyn
   1423 	  && olddyn)
   1424 	{
   1425 	  h = hi;
   1426 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1427 	  h->forced_local = 0;
   1428 	  h->ref_dynamic = 0;
   1429 	  h->def_dynamic = 0;
   1430 	  h->dynamic_def = 0;
   1431 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1432 	    {
   1433 	      h->root.type = bfd_link_hash_undefined;
   1434 	      h->root.u.undef.abfd = abfd;
   1435 	    }
   1436 	  else
   1437 	    {
   1438 	      h->root.type = bfd_link_hash_new;
   1439 	      h->root.u.undef.abfd = NULL;
   1440 	    }
   1441 	  return true;
   1442 	}
   1443     }
   1444 
   1445   /* Check TLS symbols.  We don't check undefined symbols introduced
   1446      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1447      check symbols from plugins because they also have no type.  */
   1448   if (oldbfd != NULL
   1449       && (oldbfd->flags & BFD_PLUGIN) == 0
   1450       && (abfd->flags & BFD_PLUGIN) == 0
   1451       && ELF_ST_TYPE (sym->st_info) != h->type
   1452       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1453     {
   1454       bfd *ntbfd, *tbfd;
   1455       bool ntdef, tdef;
   1456       asection *ntsec, *tsec;
   1457 
   1458       if (h->type == STT_TLS)
   1459 	{
   1460 	  ntbfd = abfd;
   1461 	  ntsec = sec;
   1462 	  ntdef = newdef;
   1463 	  tbfd = oldbfd;
   1464 	  tsec = oldsec;
   1465 	  tdef = olddef;
   1466 	}
   1467       else
   1468 	{
   1469 	  ntbfd = oldbfd;
   1470 	  ntsec = oldsec;
   1471 	  ntdef = olddef;
   1472 	  tbfd = abfd;
   1473 	  tsec = sec;
   1474 	  tdef = newdef;
   1475 	}
   1476 
   1477       if (tdef && ntdef)
   1478 	_bfd_error_handler
   1479 	  /* xgettext:c-format */
   1480 	  (_("%s: TLS definition in %pB section %pA "
   1481 	     "mismatches non-TLS definition in %pB section %pA"),
   1482 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
   1483       else if (!tdef && !ntdef)
   1484 	_bfd_error_handler
   1485 	  /* xgettext:c-format */
   1486 	  (_("%s: TLS reference in %pB "
   1487 	     "mismatches non-TLS reference in %pB"),
   1488 	   h->root.root.string, tbfd, ntbfd);
   1489       else if (tdef)
   1490 	_bfd_error_handler
   1491 	  /* xgettext:c-format */
   1492 	  (_("%s: TLS definition in %pB section %pA "
   1493 	     "mismatches non-TLS reference in %pB"),
   1494 	   h->root.root.string, tbfd, tsec, ntbfd);
   1495       else
   1496 	_bfd_error_handler
   1497 	  /* xgettext:c-format */
   1498 	  (_("%s: TLS reference in %pB "
   1499 	     "mismatches non-TLS definition in %pB section %pA"),
   1500 	   h->root.root.string, tbfd, ntbfd, ntsec);
   1501 
   1502       bfd_set_error (bfd_error_bad_value);
   1503       return false;
   1504     }
   1505 
   1506   /* If the old symbol has non-default visibility, we ignore the new
   1507      definition from a dynamic object.  */
   1508   if (newdyn
   1509       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1510       && !bfd_is_und_section (sec))
   1511     {
   1512       *skip = true;
   1513       /* Make sure this symbol is dynamic.  */
   1514       h->ref_dynamic = 1;
   1515       hi->ref_dynamic = 1;
   1516       /* A protected symbol has external availability. Make sure it is
   1517 	 recorded as dynamic.
   1518 
   1519 	 FIXME: Should we check type and size for protected symbol?  */
   1520       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1521 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1522       else
   1523 	return true;
   1524     }
   1525   else if (!newdyn
   1526 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1527 	   && h->def_dynamic)
   1528     {
   1529       /* If the new symbol with non-default visibility comes from a
   1530 	 relocatable file and the old definition comes from a dynamic
   1531 	 object, we remove the old definition.  */
   1532       if (hi->root.type == bfd_link_hash_indirect)
   1533 	{
   1534 	  /* Handle the case where the old dynamic definition is
   1535 	     default versioned.  We need to copy the symbol info from
   1536 	     the symbol with default version to the normal one if it
   1537 	     was referenced before.  */
   1538 	  if (h->ref_regular)
   1539 	    {
   1540 	      hi->root.type = h->root.type;
   1541 	      h->root.type = bfd_link_hash_indirect;
   1542 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1543 
   1544 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1545 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1546 		{
   1547 		  /* If the new symbol is hidden or internal, completely undo
   1548 		     any dynamic link state.  */
   1549 		  (*bed->elf_backend_hide_symbol) (info, h, true);
   1550 		  h->forced_local = 0;
   1551 		  h->ref_dynamic = 0;
   1552 		}
   1553 	      else
   1554 		h->ref_dynamic = 1;
   1555 
   1556 	      h->def_dynamic = 0;
   1557 	      /* FIXME: Should we check type and size for protected symbol?  */
   1558 	      h->size = 0;
   1559 	      h->type = 0;
   1560 
   1561 	      h = hi;
   1562 	    }
   1563 	  else
   1564 	    h = hi;
   1565 	}
   1566 
   1567       /* If the old symbol was undefined before, then it will still be
   1568 	 on the undefs list.  If the new symbol is undefined or
   1569 	 common, we can't make it bfd_link_hash_new here, because new
   1570 	 undefined or common symbols will be added to the undefs list
   1571 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1572 	 added twice to the undefs list.  Also, if the new symbol is
   1573 	 undefweak then we don't want to lose the strong undef.  */
   1574       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1575 	{
   1576 	  h->root.type = bfd_link_hash_undefined;
   1577 	  h->root.u.undef.abfd = abfd;
   1578 	}
   1579       else
   1580 	{
   1581 	  h->root.type = bfd_link_hash_new;
   1582 	  h->root.u.undef.abfd = NULL;
   1583 	}
   1584 
   1585       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1586 	{
   1587 	  /* If the new symbol is hidden or internal, completely undo
   1588 	     any dynamic link state.  */
   1589 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1590 	  h->forced_local = 0;
   1591 	  h->ref_dynamic = 0;
   1592 	}
   1593       else
   1594 	h->ref_dynamic = 1;
   1595       h->def_dynamic = 0;
   1596       /* FIXME: Should we check type and size for protected symbol?  */
   1597       h->size = 0;
   1598       h->type = 0;
   1599       return true;
   1600     }
   1601 
   1602   /* If a new weak symbol definition comes from a regular file and the
   1603      old symbol comes from a dynamic library, we treat the new one as
   1604      strong.  Similarly, an old weak symbol definition from a regular
   1605      file is treated as strong when the new symbol comes from a dynamic
   1606      library.  Further, an old weak symbol from a dynamic library is
   1607      treated as strong if the new symbol is from a dynamic library.
   1608      This reflects the way glibc's ld.so works.
   1609 
   1610      Also allow a weak symbol to override a linker script symbol
   1611      defined by an early pass over the script.  This is done so the
   1612      linker knows the symbol is defined in an object file, for the
   1613      DEFINED script function.
   1614 
   1615      Do this before setting *type_change_ok or *size_change_ok so that
   1616      we warn properly when dynamic library symbols are overridden.  */
   1617 
   1618   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
   1619     newweak = false;
   1620   if (olddef && newdyn)
   1621     oldweak = false;
   1622 
   1623   /* Allow changes between different types of function symbol.  */
   1624   if (newfunc && oldfunc)
   1625     *type_change_ok = true;
   1626 
   1627   /* It's OK to change the type if either the existing symbol or the
   1628      new symbol is weak.  A type change is also OK if the old symbol
   1629      is undefined and the new symbol is defined.  */
   1630 
   1631   if (oldweak
   1632       || newweak
   1633       || (newdef
   1634 	  && h->root.type == bfd_link_hash_undefined))
   1635     *type_change_ok = true;
   1636 
   1637   /* It's OK to change the size if either the existing symbol or the
   1638      new symbol is weak, or if the old symbol is undefined.  */
   1639 
   1640   if (*type_change_ok
   1641       || h->root.type == bfd_link_hash_undefined)
   1642     *size_change_ok = true;
   1643 
   1644   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1645      symbol, respectively, appears to be a common symbol in a dynamic
   1646      object.  If a symbol appears in an uninitialized section, and is
   1647      not weak, and is not a function, then it may be a common symbol
   1648      which was resolved when the dynamic object was created.  We want
   1649      to treat such symbols specially, because they raise special
   1650      considerations when setting the symbol size: if the symbol
   1651      appears as a common symbol in a regular object, and the size in
   1652      the regular object is larger, we must make sure that we use the
   1653      larger size.  This problematic case can always be avoided in C,
   1654      but it must be handled correctly when using Fortran shared
   1655      libraries.
   1656 
   1657      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1658      likewise for OLDDYNCOMMON and OLDDEF.
   1659 
   1660      Note that this test is just a heuristic, and that it is quite
   1661      possible to have an uninitialized symbol in a shared object which
   1662      is really a definition, rather than a common symbol.  This could
   1663      lead to some minor confusion when the symbol really is a common
   1664      symbol in some regular object.  However, I think it will be
   1665      harmless.  */
   1666 
   1667   if (newdyn
   1668       && newdef
   1669       && !newweak
   1670       && (sec->flags & SEC_ALLOC) != 0
   1671       && (sec->flags & SEC_LOAD) == 0
   1672       && sym->st_size > 0
   1673       && !newfunc)
   1674     newdyncommon = true;
   1675   else
   1676     newdyncommon = false;
   1677 
   1678   if (olddyn
   1679       && olddef
   1680       && h->root.type == bfd_link_hash_defined
   1681       && h->def_dynamic
   1682       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1683       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1684       && h->size > 0
   1685       && !oldfunc)
   1686     olddyncommon = true;
   1687   else
   1688     olddyncommon = false;
   1689 
   1690   /* We now know everything about the old and new symbols.  We ask the
   1691      backend to check if we can merge them.  */
   1692   if (bed->merge_symbol != NULL)
   1693     {
   1694       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1695 	return false;
   1696       sec = *psec;
   1697     }
   1698 
   1699   /* There are multiple definitions of a normal symbol.  Skip the
   1700      default symbol as well as definition from an IR object.  */
   1701   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
   1702       && !default_sym && h->def_regular
   1703       && !(oldbfd != NULL
   1704 	   && (oldbfd->flags & BFD_PLUGIN) != 0
   1705 	   && (abfd->flags & BFD_PLUGIN) == 0))
   1706     {
   1707       /* Handle a multiple definition.  */
   1708       (*info->callbacks->multiple_definition) (info, &h->root,
   1709 					       abfd, sec, *pvalue);
   1710       *skip = true;
   1711       return true;
   1712     }
   1713 
   1714   /* If both the old and the new symbols look like common symbols in a
   1715      dynamic object, set the size of the symbol to the larger of the
   1716      two.  */
   1717 
   1718   if (olddyncommon
   1719       && newdyncommon
   1720       && sym->st_size != h->size)
   1721     {
   1722       /* Since we think we have two common symbols, issue a multiple
   1723 	 common warning if desired.  Note that we only warn if the
   1724 	 size is different.  If the size is the same, we simply let
   1725 	 the old symbol override the new one as normally happens with
   1726 	 symbols defined in dynamic objects.  */
   1727 
   1728       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1729 					   bfd_link_hash_common, sym->st_size);
   1730       if (sym->st_size > h->size)
   1731 	h->size = sym->st_size;
   1732 
   1733       *size_change_ok = true;
   1734     }
   1735 
   1736   /* If we are looking at a dynamic object, and we have found a
   1737      definition, we need to see if the symbol was already defined by
   1738      some other object.  If so, we want to use the existing
   1739      definition, and we do not want to report a multiple symbol
   1740      definition error; we do this by clobbering *PSEC to be
   1741      bfd_und_section_ptr.
   1742 
   1743      We treat a common symbol as a definition if the symbol in the
   1744      shared library is a function, since common symbols always
   1745      represent variables; this can cause confusion in principle, but
   1746      any such confusion would seem to indicate an erroneous program or
   1747      shared library.  We also permit a common symbol in a regular
   1748      object to override a weak symbol in a shared object.  */
   1749 
   1750   if (newdyn
   1751       && newdef
   1752       && (olddef
   1753 	  || (h->root.type == bfd_link_hash_common
   1754 	      && (newweak || newfunc))))
   1755     {
   1756       *override = abfd;
   1757       newdef = false;
   1758       newdyncommon = false;
   1759 
   1760       *psec = sec = bfd_und_section_ptr;
   1761       *size_change_ok = true;
   1762 
   1763       /* If we get here when the old symbol is a common symbol, then
   1764 	 we are explicitly letting it override a weak symbol or
   1765 	 function in a dynamic object, and we don't want to warn about
   1766 	 a type change.  If the old symbol is a defined symbol, a type
   1767 	 change warning may still be appropriate.  */
   1768 
   1769       if (h->root.type == bfd_link_hash_common)
   1770 	*type_change_ok = true;
   1771     }
   1772 
   1773   /* Handle the special case of an old common symbol merging with a
   1774      new symbol which looks like a common symbol in a shared object.
   1775      We change *PSEC and *PVALUE to make the new symbol look like a
   1776      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1777      right thing.  */
   1778 
   1779   if (newdyncommon
   1780       && h->root.type == bfd_link_hash_common)
   1781     {
   1782       *override = oldbfd;
   1783       newdef = false;
   1784       newdyncommon = false;
   1785       *pvalue = sym->st_size;
   1786       *psec = sec = bed->common_section (oldsec);
   1787       *size_change_ok = true;
   1788     }
   1789 
   1790   /* Skip weak definitions of symbols that are already defined.  */
   1791   if (newdef && olddef && newweak)
   1792     {
   1793       /* Don't skip new non-IR weak syms.  */
   1794       if (!(oldbfd != NULL
   1795 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1796 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1797 	{
   1798 	  newdef = false;
   1799 	  *skip = true;
   1800 	}
   1801 
   1802       /* Merge st_other.  If the symbol already has a dynamic index,
   1803 	 but visibility says it should not be visible, turn it into a
   1804 	 local symbol.  */
   1805       elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
   1806       if (h->dynindx != -1)
   1807 	switch (ELF_ST_VISIBILITY (h->other))
   1808 	  {
   1809 	  case STV_INTERNAL:
   1810 	  case STV_HIDDEN:
   1811 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   1812 	    break;
   1813 	  }
   1814     }
   1815 
   1816   /* If the old symbol is from a dynamic object, and the new symbol is
   1817      a definition which is not from a dynamic object, then the new
   1818      symbol overrides the old symbol.  Symbols from regular files
   1819      always take precedence over symbols from dynamic objects, even if
   1820      they are defined after the dynamic object in the link.
   1821 
   1822      As above, we again permit a common symbol in a regular object to
   1823      override a definition in a shared object if the shared object
   1824      symbol is a function or is weak.  */
   1825 
   1826   flip = NULL;
   1827   if (!newdyn
   1828       && (newdef
   1829 	  || (bfd_is_com_section (sec)
   1830 	      && (oldweak || oldfunc)))
   1831       && olddyn
   1832       && olddef
   1833       && h->def_dynamic)
   1834     {
   1835       /* Change the hash table entry to undefined, and let
   1836 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1837 	 new definition.  */
   1838 
   1839       h->root.type = bfd_link_hash_undefined;
   1840       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1841       *size_change_ok = true;
   1842 
   1843       olddef = false;
   1844       olddyncommon = false;
   1845 
   1846       /* We again permit a type change when a common symbol may be
   1847 	 overriding a function.  */
   1848 
   1849       if (bfd_is_com_section (sec))
   1850 	{
   1851 	  if (oldfunc)
   1852 	    {
   1853 	      /* If a common symbol overrides a function, make sure
   1854 		 that it isn't defined dynamically nor has type
   1855 		 function.  */
   1856 	      h->def_dynamic = 0;
   1857 	      h->type = STT_NOTYPE;
   1858 	    }
   1859 	  *type_change_ok = true;
   1860 	}
   1861 
   1862       if (hi->root.type == bfd_link_hash_indirect)
   1863 	flip = hi;
   1864       else
   1865 	/* This union may have been set to be non-NULL when this symbol
   1866 	   was seen in a dynamic object.  We must force the union to be
   1867 	   NULL, so that it is correct for a regular symbol.  */
   1868 	h->verinfo.vertree = NULL;
   1869     }
   1870 
   1871   /* Handle the special case of a new common symbol merging with an
   1872      old symbol that looks like it might be a common symbol defined in
   1873      a shared object.  Note that we have already handled the case in
   1874      which a new common symbol should simply override the definition
   1875      in the shared library.  */
   1876 
   1877   if (! newdyn
   1878       && bfd_is_com_section (sec)
   1879       && olddyncommon)
   1880     {
   1881       /* It would be best if we could set the hash table entry to a
   1882 	 common symbol, but we don't know what to use for the section
   1883 	 or the alignment.  */
   1884       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1885 					   bfd_link_hash_common, sym->st_size);
   1886 
   1887       /* If the presumed common symbol in the dynamic object is
   1888 	 larger, pretend that the new symbol has its size.  */
   1889 
   1890       if (h->size > *pvalue)
   1891 	*pvalue = h->size;
   1892 
   1893       /* We need to remember the alignment required by the symbol
   1894 	 in the dynamic object.  */
   1895       BFD_ASSERT (pold_alignment);
   1896       *pold_alignment = h->root.u.def.section->alignment_power;
   1897 
   1898       olddef = false;
   1899       olddyncommon = false;
   1900 
   1901       h->root.type = bfd_link_hash_undefined;
   1902       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1903 
   1904       *size_change_ok = true;
   1905       *type_change_ok = true;
   1906 
   1907       if (hi->root.type == bfd_link_hash_indirect)
   1908 	flip = hi;
   1909       else
   1910 	h->verinfo.vertree = NULL;
   1911     }
   1912 
   1913   if (flip != NULL)
   1914     {
   1915       /* Handle the case where we had a versioned symbol in a dynamic
   1916 	 library and now find a definition in a normal object.  In this
   1917 	 case, we make the versioned symbol point to the normal one.  */
   1918       flip->root.type = h->root.type;
   1919       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1920       h->root.type = bfd_link_hash_indirect;
   1921       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1922       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1923       if (h->def_dynamic)
   1924 	{
   1925 	  h->def_dynamic = 0;
   1926 	  flip->ref_dynamic = 1;
   1927 	}
   1928     }
   1929 
   1930   return true;
   1931 }
   1932 
   1933 /* This function is called to create an indirect symbol from the
   1934    default for the symbol with the default version if needed. The
   1935    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1936    set DYNSYM if the new indirect symbol is dynamic.  */
   1937 
   1938 static bool
   1939 _bfd_elf_add_default_symbol (bfd *abfd,
   1940 			     struct bfd_link_info *info,
   1941 			     struct elf_link_hash_entry *h,
   1942 			     const char *name,
   1943 			     Elf_Internal_Sym *sym,
   1944 			     asection *sec,
   1945 			     bfd_vma value,
   1946 			     bfd **poldbfd,
   1947 			     bool *dynsym)
   1948 {
   1949   bool type_change_ok;
   1950   bool size_change_ok;
   1951   bool skip;
   1952   char *shortname;
   1953   struct elf_link_hash_entry *hi;
   1954   struct bfd_link_hash_entry *bh;
   1955   const struct elf_backend_data *bed;
   1956   bool collect;
   1957   bool dynamic;
   1958   bfd *override;
   1959   char *p;
   1960   size_t len, shortlen;
   1961   asection *tmp_sec;
   1962   bool matched;
   1963 
   1964   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1965     return true;
   1966 
   1967   /* If this symbol has a version, and it is the default version, we
   1968      create an indirect symbol from the default name to the fully
   1969      decorated name.  This will cause external references which do not
   1970      specify a version to be bound to this version of the symbol.  */
   1971   p = strchr (name, ELF_VER_CHR);
   1972   if (h->versioned == unknown)
   1973     {
   1974       if (p == NULL)
   1975 	{
   1976 	  h->versioned = unversioned;
   1977 	  return true;
   1978 	}
   1979       else
   1980 	{
   1981 	  if (p[1] != ELF_VER_CHR)
   1982 	    {
   1983 	      h->versioned = versioned_hidden;
   1984 	      return true;
   1985 	    }
   1986 	  else
   1987 	    h->versioned = versioned;
   1988 	}
   1989     }
   1990   else
   1991     {
   1992       /* PR ld/19073: We may see an unversioned definition after the
   1993 	 default version.  */
   1994       if (p == NULL)
   1995 	return true;
   1996     }
   1997 
   1998   bed = get_elf_backend_data (abfd);
   1999   collect = bed->collect;
   2000   dynamic = (abfd->flags & DYNAMIC) != 0;
   2001 
   2002   shortlen = p - name;
   2003   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   2004   if (shortname == NULL)
   2005     return false;
   2006   memcpy (shortname, name, shortlen);
   2007   shortname[shortlen] = '\0';
   2008 
   2009   /* We are going to create a new symbol.  Merge it with any existing
   2010      symbol with this name.  For the purposes of the merge, act as
   2011      though we were defining the symbol we just defined, although we
   2012      actually going to define an indirect symbol.  */
   2013   type_change_ok = false;
   2014   size_change_ok = false;
   2015   matched = true;
   2016   tmp_sec = sec;
   2017   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2018 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2019 			      &type_change_ok, &size_change_ok, &matched))
   2020     return false;
   2021 
   2022   if (skip)
   2023     goto nondefault;
   2024 
   2025   if (hi->def_regular || ELF_COMMON_DEF_P (hi))
   2026     {
   2027       /* If the undecorated symbol will have a version added by a
   2028 	 script different to H, then don't indirect to/from the
   2029 	 undecorated symbol.  This isn't ideal because we may not yet
   2030 	 have seen symbol versions, if given by a script on the
   2031 	 command line rather than via --version-script.  */
   2032       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   2033 	{
   2034 	  bool hide;
   2035 
   2036 	  hi->verinfo.vertree
   2037 	    = bfd_find_version_for_sym (info->version_info,
   2038 					hi->root.root.string, &hide);
   2039 	  if (hi->verinfo.vertree != NULL && hide)
   2040 	    {
   2041 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   2042 	      goto nondefault;
   2043 	    }
   2044 	}
   2045       if (hi->verinfo.vertree != NULL
   2046 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   2047 	goto nondefault;
   2048     }
   2049 
   2050   if (! override)
   2051     {
   2052       /* Add the default symbol if not performing a relocatable link.  */
   2053       if (! bfd_link_relocatable (info))
   2054 	{
   2055 	  bh = &hi->root;
   2056 	  if (bh->type == bfd_link_hash_defined
   2057 	      && bh->u.def.section->owner != NULL
   2058 	      && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
   2059 	    {
   2060 	      /* Mark the previous definition from IR object as
   2061 		 undefined so that the generic linker will override
   2062 		 it.  */
   2063 	      bh->type = bfd_link_hash_undefined;
   2064 	      bh->u.undef.abfd = bh->u.def.section->owner;
   2065 	    }
   2066 	  if (! (_bfd_generic_link_add_one_symbol
   2067 		 (info, abfd, shortname, BSF_INDIRECT,
   2068 		  bfd_ind_section_ptr,
   2069 		  0, name, false, collect, &bh)))
   2070 	    return false;
   2071 	  hi = (struct elf_link_hash_entry *) bh;
   2072 	}
   2073     }
   2074   else
   2075     {
   2076       /* In this case the symbol named SHORTNAME is overriding the
   2077 	 indirect symbol we want to add.  We were planning on making
   2078 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   2079 	 is the name without a version.  NAME is the fully versioned
   2080 	 name, and it is the default version.
   2081 
   2082 	 Overriding means that we already saw a definition for the
   2083 	 symbol SHORTNAME in a regular object, and it is overriding
   2084 	 the symbol defined in the dynamic object.
   2085 
   2086 	 When this happens, we actually want to change NAME, the
   2087 	 symbol we just added, to refer to SHORTNAME.  This will cause
   2088 	 references to NAME in the shared object to become references
   2089 	 to SHORTNAME in the regular object.  This is what we expect
   2090 	 when we override a function in a shared object: that the
   2091 	 references in the shared object will be mapped to the
   2092 	 definition in the regular object.  */
   2093 
   2094       while (hi->root.type == bfd_link_hash_indirect
   2095 	     || hi->root.type == bfd_link_hash_warning)
   2096 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2097 
   2098       h->root.type = bfd_link_hash_indirect;
   2099       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   2100       if (h->def_dynamic)
   2101 	{
   2102 	  h->def_dynamic = 0;
   2103 	  hi->ref_dynamic = 1;
   2104 	  if (hi->ref_regular
   2105 	      || hi->def_regular)
   2106 	    {
   2107 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   2108 		return false;
   2109 	    }
   2110 	}
   2111 
   2112       /* Now set HI to H, so that the following code will set the
   2113 	 other fields correctly.  */
   2114       hi = h;
   2115     }
   2116 
   2117   /* Check if HI is a warning symbol.  */
   2118   if (hi->root.type == bfd_link_hash_warning)
   2119     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2120 
   2121   /* If there is a duplicate definition somewhere, then HI may not
   2122      point to an indirect symbol.  We will have reported an error to
   2123      the user in that case.  */
   2124 
   2125   if (hi->root.type == bfd_link_hash_indirect)
   2126     {
   2127       struct elf_link_hash_entry *ht;
   2128 
   2129       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2130       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   2131 
   2132       /* If we first saw a reference to SHORTNAME with non-default
   2133 	 visibility, merge that visibility to the @@VER symbol.  */
   2134       elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
   2135 
   2136       /* A reference to the SHORTNAME symbol from a dynamic library
   2137 	 will be satisfied by the versioned symbol at runtime.  In
   2138 	 effect, we have a reference to the versioned symbol.  */
   2139       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2140       hi->dynamic_def |= ht->dynamic_def;
   2141 
   2142       /* See if the new flags lead us to realize that the symbol must
   2143 	 be dynamic.  */
   2144       if (! *dynsym)
   2145 	{
   2146 	  if (! dynamic)
   2147 	    {
   2148 	      if (! bfd_link_executable (info)
   2149 		  || hi->def_dynamic
   2150 		  || hi->ref_dynamic)
   2151 		*dynsym = true;
   2152 	    }
   2153 	  else
   2154 	    {
   2155 	      if (hi->ref_regular)
   2156 		*dynsym = true;
   2157 	    }
   2158 	}
   2159     }
   2160 
   2161   /* We also need to define an indirection from the nondefault version
   2162      of the symbol.  */
   2163 
   2164  nondefault:
   2165   len = strlen (name);
   2166   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   2167   if (shortname == NULL)
   2168     return false;
   2169   memcpy (shortname, name, shortlen);
   2170   memcpy (shortname + shortlen, p + 1, len - shortlen);
   2171 
   2172   /* Once again, merge with any existing symbol.  */
   2173   type_change_ok = false;
   2174   size_change_ok = false;
   2175   tmp_sec = sec;
   2176   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2177 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2178 			      &type_change_ok, &size_change_ok, &matched))
   2179     return false;
   2180 
   2181   if (skip)
   2182     {
   2183       if (!dynamic
   2184 	  && h->root.type == bfd_link_hash_defweak
   2185 	  && hi->root.type == bfd_link_hash_defined)
   2186 	{
   2187 	  /* We are handling a weak sym@@ver and attempting to define
   2188 	     a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
   2189 	     new weak sym@ver because there is already a strong sym@ver.
   2190 	     However, sym@ver and sym@@ver are really the same symbol.
   2191 	     The existing strong sym@ver ought to override sym@@ver.  */
   2192 	  h->root.type = bfd_link_hash_defined;
   2193 	  h->root.u.def.section = hi->root.u.def.section;
   2194 	  h->root.u.def.value = hi->root.u.def.value;
   2195 	  hi->root.type = bfd_link_hash_indirect;
   2196 	  hi->root.u.i.link = &h->root;
   2197 	}
   2198       else
   2199 	return true;
   2200     }
   2201   else if (override)
   2202     {
   2203       /* Here SHORTNAME is a versioned name, so we don't expect to see
   2204 	 the type of override we do in the case above unless it is
   2205 	 overridden by a versioned definition.  */
   2206       if (hi->root.type != bfd_link_hash_defined
   2207 	  && hi->root.type != bfd_link_hash_defweak)
   2208 	_bfd_error_handler
   2209 	  /* xgettext:c-format */
   2210 	  (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
   2211 	   abfd, shortname);
   2212       return true;
   2213     }
   2214   else
   2215     {
   2216       bh = &hi->root;
   2217       if (! (_bfd_generic_link_add_one_symbol
   2218 	     (info, abfd, shortname, BSF_INDIRECT,
   2219 	      bfd_ind_section_ptr, 0, name, false, collect, &bh)))
   2220 	return false;
   2221       hi = (struct elf_link_hash_entry *) bh;
   2222     }
   2223 
   2224   /* If there is a duplicate definition somewhere, then HI may not
   2225      point to an indirect symbol.  We will have reported an error
   2226      to the user in that case.  */
   2227   if (hi->root.type == bfd_link_hash_indirect)
   2228     {
   2229       (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   2230       h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2231       hi->dynamic_def |= h->dynamic_def;
   2232 
   2233       /* If we first saw a reference to @VER symbol with
   2234 	 non-default visibility, merge that visibility to the
   2235 	 @@VER symbol.  */
   2236       elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
   2237 
   2238       /* See if the new flags lead us to realize that the symbol
   2239 	 must be dynamic.  */
   2240       if (! *dynsym)
   2241 	{
   2242 	  if (! dynamic)
   2243 	    {
   2244 	      if (! bfd_link_executable (info)
   2245 		  || hi->ref_dynamic)
   2246 		*dynsym = true;
   2247 	    }
   2248 	  else
   2249 	    {
   2250 	      if (hi->ref_regular)
   2251 		*dynsym = true;
   2252 	    }
   2253 	}
   2254     }
   2255 
   2256   return true;
   2257 }
   2258 
   2259 /* This routine is used to export all defined symbols into the dynamic
   2261    symbol table.  It is called via elf_link_hash_traverse.  */
   2262 
   2263 static bool
   2264 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   2265 {
   2266   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2267 
   2268   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2269   if (h->root.type == bfd_link_hash_indirect)
   2270     return true;
   2271 
   2272   /* Ignore this if we won't export it.  */
   2273   if (!eif->info->export_dynamic && !h->dynamic)
   2274     return true;
   2275 
   2276   if (h->dynindx == -1
   2277       && (h->def_regular || h->ref_regular)
   2278       && ! bfd_hide_sym_by_version (eif->info->version_info,
   2279 				    h->root.root.string))
   2280     {
   2281       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2282 	{
   2283 	  eif->failed = true;
   2284 	  return false;
   2285 	}
   2286     }
   2287 
   2288   return true;
   2289 }
   2290 
   2291 /* Return the glibc version reference if VERSION_DEP is added to the
   2293    list of glibc version dependencies successfully.  VERSION_DEP will
   2294    be put into the .gnu.version_r section.  GLIBC_MINOR_BASE is the
   2295    pointer to the glibc minor base version.  */
   2296 
   2297 static Elf_Internal_Verneed *
   2298 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
   2299 			    Elf_Internal_Verneed *glibc_verref,
   2300 			    const char *version_dep,
   2301 			    int *glibc_minor_base)
   2302 {
   2303   Elf_Internal_Verneed *t;
   2304   Elf_Internal_Vernaux *a;
   2305   size_t amt;
   2306   int minor_version = -1;
   2307 
   2308   if (glibc_verref != NULL)
   2309     {
   2310       t = glibc_verref;
   2311 
   2312       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2313 	{
   2314 	  /* Return if VERSION_DEP dependency has been added.  */
   2315 	  if (a->vna_nodename == version_dep
   2316 	      || strcmp (a->vna_nodename, version_dep) == 0)
   2317 	    return t;
   2318 	}
   2319     }
   2320   else
   2321     {
   2322       for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2323 	   t != NULL;
   2324 	   t = t->vn_nextref)
   2325 	{
   2326 	  const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
   2327 	  if (soname != NULL && startswith (soname, "libc.so."))
   2328 	    break;
   2329 	}
   2330 
   2331       /* Skip the shared library if it isn't libc.so.  */
   2332       if (t == NULL)
   2333 	return t;
   2334 
   2335       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2336 	{
   2337 	  /* Return if VERSION_DEP dependency has been added.  */
   2338 	  if (a->vna_nodename == version_dep
   2339 	      || strcmp (a->vna_nodename, version_dep) == 0)
   2340 	    return t;
   2341 
   2342 	  /* Check if libc.so provides GLIBC_2.XX version.  */
   2343 	  if (startswith (a->vna_nodename, "GLIBC_2."))
   2344 	    {
   2345 	      minor_version = strtol (a->vna_nodename + 8, NULL, 10);
   2346 	      if (minor_version < *glibc_minor_base)
   2347 		*glibc_minor_base = minor_version;
   2348 	    }
   2349 	}
   2350 
   2351       /* Skip if it isn't linked against glibc.  */
   2352       if (minor_version < 0)
   2353 	return NULL;
   2354     }
   2355 
   2356   /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
   2357   if (startswith (version_dep, "GLIBC_2."))
   2358     {
   2359       minor_version = strtol (version_dep + 8, NULL, 10);
   2360       if (minor_version <= *glibc_minor_base)
   2361 	return NULL;
   2362     }
   2363 
   2364   amt = sizeof *a;
   2365   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2366   if (a == NULL)
   2367     {
   2368       rinfo->failed = true;
   2369       return NULL;
   2370     }
   2371 
   2372   a->vna_nodename = version_dep;
   2373   a->vna_flags = 0;
   2374   a->vna_nextptr = t->vn_auxptr;
   2375   a->vna_other = rinfo->vers + 1;
   2376   ++rinfo->vers;
   2377 
   2378   t->vn_auxptr = a;
   2379 
   2380   return t;
   2381 }
   2382 
   2383 /* Add VERSION_DEP to the list of version dependencies when linked
   2384    against glibc.  */
   2385 
   2386 void
   2387 _bfd_elf_link_add_glibc_version_dependency
   2388   (struct elf_find_verdep_info *rinfo,
   2389    const char *version_dep[])
   2390 {
   2391   Elf_Internal_Verneed *t = NULL;
   2392   int glibc_minor_base = INT_MAX;
   2393 
   2394   do
   2395     {
   2396       t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
   2397 				      &glibc_minor_base);
   2398       /* Return if there is no glibc version reference.  */
   2399       if (t == NULL)
   2400 	return;
   2401       version_dep++;
   2402     }
   2403   while (*version_dep != NULL);
   2404 }
   2405 
   2406 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
   2407    linked against glibc.  */
   2408 
   2409 void
   2410 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
   2411 {
   2412   if (rinfo->info->enable_dt_relr)
   2413     {
   2414       const char *version[] =
   2415 	{
   2416 	  "GLIBC_ABI_DT_RELR",
   2417 	  NULL
   2418 	};
   2419       _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
   2420     }
   2421 }
   2422 
   2423 /* Look through the symbols which are defined in other shared
   2424    libraries and referenced here.  Update the list of version
   2425    dependencies.  This will be put into the .gnu.version_r section.
   2426    This function is called via elf_link_hash_traverse.  */
   2427 
   2428 static bool
   2429 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2430 					 void *data)
   2431 {
   2432   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2433   Elf_Internal_Verneed *t;
   2434   Elf_Internal_Vernaux *a;
   2435   size_t amt;
   2436 
   2437   /* We only care about symbols defined in shared objects with version
   2438      information.  */
   2439   if (!h->def_dynamic
   2440       || h->def_regular
   2441       || h->dynindx == -1
   2442       || h->verinfo.verdef == NULL
   2443       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2444 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2445     return true;
   2446 
   2447   /* See if we already know about this version.  */
   2448   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2449        t != NULL;
   2450        t = t->vn_nextref)
   2451     {
   2452       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2453 	continue;
   2454 
   2455       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2456 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2457 	  return true;
   2458 
   2459       break;
   2460     }
   2461 
   2462   /* This is a new version.  Add it to tree we are building.  */
   2463 
   2464   if (t == NULL)
   2465     {
   2466       amt = sizeof *t;
   2467       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2468       if (t == NULL)
   2469 	{
   2470 	  rinfo->failed = true;
   2471 	  return false;
   2472 	}
   2473 
   2474       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2475       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2476       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2477     }
   2478 
   2479   amt = sizeof *a;
   2480   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2481   if (a == NULL)
   2482     {
   2483       rinfo->failed = true;
   2484       return false;
   2485     }
   2486 
   2487   /* Note that we are copying a string pointer here, and testing it
   2488      above.  If bfd_elf_string_from_elf_section is ever changed to
   2489      discard the string data when low in memory, this will have to be
   2490      fixed.  */
   2491   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2492 
   2493   a->vna_flags = h->verinfo.verdef->vd_flags;
   2494   a->vna_nextptr = t->vn_auxptr;
   2495 
   2496   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2497   ++rinfo->vers;
   2498 
   2499   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2500 
   2501   t->vn_auxptr = a;
   2502 
   2503   return true;
   2504 }
   2505 
   2506 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
   2507    hidden.  Set *T_P to NULL if there is no match.  */
   2508 
   2509 static bool
   2510 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
   2511 				     struct elf_link_hash_entry *h,
   2512 				     const char *version_p,
   2513 				     struct bfd_elf_version_tree **t_p,
   2514 				     bool *hide)
   2515 {
   2516   struct bfd_elf_version_tree *t;
   2517 
   2518   /* Look for the version.  If we find it, it is no longer weak.  */
   2519   for (t = info->version_info; t != NULL; t = t->next)
   2520     {
   2521       if (strcmp (t->name, version_p) == 0)
   2522 	{
   2523 	  size_t len;
   2524 	  char *alc;
   2525 	  struct bfd_elf_version_expr *d;
   2526 
   2527 	  len = version_p - h->root.root.string;
   2528 	  alc = (char *) bfd_malloc (len);
   2529 	  if (alc == NULL)
   2530 	    return false;
   2531 	  memcpy (alc, h->root.root.string, len - 1);
   2532 	  alc[len - 1] = '\0';
   2533 	  if (alc[len - 2] == ELF_VER_CHR)
   2534 	    alc[len - 2] = '\0';
   2535 
   2536 	  h->verinfo.vertree = t;
   2537 	  t->used = true;
   2538 	  d = NULL;
   2539 
   2540 	  if (t->globals.list != NULL)
   2541 	    d = (*t->match) (&t->globals, NULL, alc);
   2542 
   2543 	  /* See if there is anything to force this symbol to
   2544 	     local scope.  */
   2545 	  if (d == NULL && t->locals.list != NULL)
   2546 	    {
   2547 	      d = (*t->match) (&t->locals, NULL, alc);
   2548 	      if (d != NULL
   2549 		  && h->dynindx != -1
   2550 		  && ! info->export_dynamic)
   2551 		*hide = true;
   2552 	    }
   2553 
   2554 	  free (alc);
   2555 	  break;
   2556 	}
   2557     }
   2558 
   2559   *t_p = t;
   2560 
   2561   return true;
   2562 }
   2563 
   2564 /* Return TRUE if the symbol H is hidden by version script.  */
   2565 
   2566 bool
   2567 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
   2568 				   struct elf_link_hash_entry *h)
   2569 {
   2570   const char *p;
   2571   bool hide = false;
   2572   const struct elf_backend_data *bed
   2573     = get_elf_backend_data (info->output_bfd);
   2574 
   2575   /* Version script only hides symbols defined in regular objects.  */
   2576   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2577     return true;
   2578 
   2579   p = strchr (h->root.root.string, ELF_VER_CHR);
   2580   if (p != NULL && h->verinfo.vertree == NULL)
   2581     {
   2582       struct bfd_elf_version_tree *t;
   2583 
   2584       ++p;
   2585       if (*p == ELF_VER_CHR)
   2586 	++p;
   2587 
   2588       if (*p != '\0'
   2589 	  && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
   2590 	  && hide)
   2591 	{
   2592 	  if (hide)
   2593 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   2594 	  return true;
   2595 	}
   2596     }
   2597 
   2598   /* If we don't have a version for this symbol, see if we can find
   2599      something.  */
   2600   if (h->verinfo.vertree == NULL && info->version_info != NULL)
   2601     {
   2602       h->verinfo.vertree
   2603 	= bfd_find_version_for_sym (info->version_info,
   2604 				    h->root.root.string, &hide);
   2605       if (h->verinfo.vertree != NULL && hide)
   2606 	{
   2607 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   2608 	  return true;
   2609 	}
   2610     }
   2611 
   2612   return false;
   2613 }
   2614 
   2615 /* Figure out appropriate versions for all the symbols.  We may not
   2616    have the version number script until we have read all of the input
   2617    files, so until that point we don't know which symbols should be
   2618    local.  This function is called via elf_link_hash_traverse.  */
   2619 
   2620 static bool
   2621 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2622 {
   2623   struct elf_info_failed *sinfo;
   2624   struct bfd_link_info *info;
   2625   const struct elf_backend_data *bed;
   2626   struct elf_info_failed eif;
   2627   char *p;
   2628   bool hide;
   2629 
   2630   sinfo = (struct elf_info_failed *) data;
   2631   info = sinfo->info;
   2632 
   2633   /* Fix the symbol flags.  */
   2634   eif.failed = false;
   2635   eif.info = info;
   2636   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2637     {
   2638       if (eif.failed)
   2639 	sinfo->failed = true;
   2640       return false;
   2641     }
   2642 
   2643   bed = get_elf_backend_data (info->output_bfd);
   2644 
   2645   /* We only need version numbers for symbols defined in regular
   2646      objects.  */
   2647   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2648     {
   2649       /* Hide symbols defined in discarded input sections.  */
   2650       if ((h->root.type == bfd_link_hash_defined
   2651 	   || h->root.type == bfd_link_hash_defweak)
   2652 	  && discarded_section (h->root.u.def.section))
   2653 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2654       return true;
   2655     }
   2656 
   2657   hide = false;
   2658   p = strchr (h->root.root.string, ELF_VER_CHR);
   2659   if (p != NULL && h->verinfo.vertree == NULL)
   2660     {
   2661       struct bfd_elf_version_tree *t;
   2662 
   2663       ++p;
   2664       if (*p == ELF_VER_CHR)
   2665 	++p;
   2666 
   2667       /* If there is no version string, we can just return out.  */
   2668       if (*p == '\0')
   2669 	return true;
   2670 
   2671       if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
   2672 	{
   2673 	  sinfo->failed = true;
   2674 	  return false;
   2675 	}
   2676 
   2677       if (hide)
   2678 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2679 
   2680       /* If we are building an application, we need to create a
   2681 	 version node for this version.  */
   2682       if (t == NULL && bfd_link_executable (info))
   2683 	{
   2684 	  struct bfd_elf_version_tree **pp;
   2685 	  int version_index;
   2686 
   2687 	  /* If we aren't going to export this symbol, we don't need
   2688 	     to worry about it.  */
   2689 	  if (h->dynindx == -1)
   2690 	    return true;
   2691 
   2692 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
   2693 							  sizeof *t);
   2694 	  if (t == NULL)
   2695 	    {
   2696 	      sinfo->failed = true;
   2697 	      return false;
   2698 	    }
   2699 
   2700 	  t->name = p;
   2701 	  t->name_indx = (unsigned int) -1;
   2702 	  t->used = true;
   2703 
   2704 	  version_index = 1;
   2705 	  /* Don't count anonymous version tag.  */
   2706 	  if (sinfo->info->version_info != NULL
   2707 	      && sinfo->info->version_info->vernum == 0)
   2708 	    version_index = 0;
   2709 	  for (pp = &sinfo->info->version_info;
   2710 	       *pp != NULL;
   2711 	       pp = &(*pp)->next)
   2712 	    ++version_index;
   2713 	  t->vernum = version_index;
   2714 
   2715 	  *pp = t;
   2716 
   2717 	  h->verinfo.vertree = t;
   2718 	}
   2719       else if (t == NULL)
   2720 	{
   2721 	  /* We could not find the version for a symbol when
   2722 	     generating a shared archive.  Return an error.  */
   2723 	  _bfd_error_handler
   2724 	    /* xgettext:c-format */
   2725 	    (_("%pB: version node not found for symbol %s"),
   2726 	     info->output_bfd, h->root.root.string);
   2727 	  bfd_set_error (bfd_error_bad_value);
   2728 	  sinfo->failed = true;
   2729 	  return false;
   2730 	}
   2731     }
   2732 
   2733   /* If we don't have a version for this symbol, see if we can find
   2734      something.  */
   2735   if (!hide
   2736       && h->verinfo.vertree == NULL
   2737       && sinfo->info->version_info != NULL)
   2738     {
   2739       h->verinfo.vertree
   2740 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2741 				    h->root.root.string, &hide);
   2742       if (h->verinfo.vertree != NULL && hide)
   2743 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2744     }
   2745 
   2746   return true;
   2747 }
   2748 
   2749 /* Read and swap the relocs from the section indicated by SHDR.  This
   2751    may be either a REL or a RELA section.  The relocations are
   2752    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2753    which should have already been allocated to contain enough space.
   2754    The *EXTERNAL_RELOCS_P are a buffer where the external form of the
   2755    relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
   2756    *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
   2757    memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
   2758    unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
   2759 
   2760    Returns FALSE if something goes wrong.  */
   2761 
   2762 static bool
   2763 elf_link_read_relocs_from_section (bfd *abfd,
   2764 				   const asection *sec,
   2765 				   Elf_Internal_Shdr *shdr,
   2766 				   void **external_relocs_addr,
   2767 				   size_t *external_relocs_size,
   2768 				   Elf_Internal_Rela *internal_relocs)
   2769 {
   2770   const struct elf_backend_data *bed;
   2771   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2772   const bfd_byte *erela;
   2773   const bfd_byte *erelaend;
   2774   Elf_Internal_Rela *irela;
   2775   Elf_Internal_Shdr *symtab_hdr;
   2776   size_t nsyms;
   2777   void *external_relocs = *external_relocs_addr;
   2778 
   2779   /* Position ourselves at the start of the section.  */
   2780   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2781     return false;
   2782 
   2783   /* Read the relocations.  */
   2784   *external_relocs_size = shdr->sh_size;
   2785   if (!_bfd_mmap_read_temporary (&external_relocs,
   2786 				 external_relocs_size,
   2787 				 external_relocs_addr, abfd, true))
   2788     return false;
   2789 
   2790   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2791   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2792 
   2793   bed = get_elf_backend_data (abfd);
   2794 
   2795   /* Convert the external relocations to the internal format.  */
   2796   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2797     swap_in = bed->s->swap_reloc_in;
   2798   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2799     swap_in = bed->s->swap_reloca_in;
   2800   else
   2801     {
   2802       bfd_set_error (bfd_error_wrong_format);
   2803       return false;
   2804     }
   2805 
   2806   erela = (const bfd_byte *) external_relocs;
   2807   /* Setting erelaend like this and comparing with <= handles case of
   2808      a fuzzed object with sh_size not a multiple of sh_entsize.  */
   2809   erelaend = erela + shdr->sh_size - shdr->sh_entsize;
   2810   irela = internal_relocs;
   2811   while (erela <= erelaend)
   2812     {
   2813       bfd_vma r_symndx;
   2814 
   2815       (*swap_in) (abfd, erela, irela);
   2816       r_symndx = ELF32_R_SYM (irela->r_info);
   2817       if (bed->s->arch_size == 64)
   2818 	r_symndx >>= 24;
   2819       if (nsyms > 0)
   2820 	{
   2821 	  if ((size_t) r_symndx >= nsyms)
   2822 	    {
   2823 	      _bfd_error_handler
   2824 		/* xgettext:c-format */
   2825 		(_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
   2826 		   " for offset %#" PRIx64 " in section `%pA'"),
   2827 		 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
   2828 		 (uint64_t) irela->r_offset, sec);
   2829 	      bfd_set_error (bfd_error_bad_value);
   2830 	      return false;
   2831 	    }
   2832 	}
   2833       else if (r_symndx != STN_UNDEF)
   2834 	{
   2835 	  _bfd_error_handler
   2836 	    /* xgettext:c-format */
   2837 	    (_("%pB: non-zero symbol index (%#" PRIx64 ")"
   2838 	       " for offset %#" PRIx64 " in section `%pA'"
   2839 	       " when the object file has no symbol table"),
   2840 	     abfd, (uint64_t) r_symndx,
   2841 	     (uint64_t) irela->r_offset, sec);
   2842 	  bfd_set_error (bfd_error_bad_value);
   2843 	  return false;
   2844 	}
   2845       irela += bed->s->int_rels_per_ext_rel;
   2846       erela += shdr->sh_entsize;
   2847     }
   2848 
   2849   return true;
   2850 }
   2851 
   2852 /* Read and swap the relocs for a section O.  They may have been
   2853    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2854    not NULL, they are used as buffers to read into.  They are known to
   2855    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2856    the return value is allocated using either malloc or bfd_alloc,
   2857    according to the KEEP_MEMORY argument.  If O has two relocation
   2858    sections (both REL and RELA relocations), then the REL_HDR
   2859    relocations will appear first in INTERNAL_RELOCS, followed by the
   2860    RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
   2861    update cache_size.  */
   2862 
   2863 Elf_Internal_Rela *
   2864 _bfd_elf_link_info_read_relocs (bfd *abfd,
   2865 				struct bfd_link_info *info,
   2866 				const asection *o,
   2867 				void *external_relocs,
   2868 				Elf_Internal_Rela *internal_relocs,
   2869 				bool keep_memory)
   2870 {
   2871   void *alloc1 = NULL;
   2872   size_t alloc1_size;
   2873   Elf_Internal_Rela *alloc2 = NULL;
   2874   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2875   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2876   Elf_Internal_Rela *internal_rela_relocs;
   2877 
   2878   if (esdo->relocs != NULL)
   2879     return esdo->relocs;
   2880 
   2881   if (o->reloc_count == 0)
   2882     return NULL;
   2883 
   2884   if (internal_relocs == NULL)
   2885     {
   2886       bfd_size_type size;
   2887 
   2888       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
   2889       if (keep_memory && info)
   2890 	info->cache_size += size;
   2891       internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2892       if (internal_relocs == NULL)
   2893 	return NULL;
   2894     }
   2895 
   2896   alloc1 = external_relocs;
   2897   internal_rela_relocs = internal_relocs;
   2898   if (esdo->rel.hdr)
   2899     {
   2900       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2901 					      &alloc1, &alloc1_size,
   2902 					      internal_relocs))
   2903 	goto error_return;
   2904       external_relocs = (((bfd_byte *) external_relocs)
   2905 			 + esdo->rel.hdr->sh_size);
   2906       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2907 			       * bed->s->int_rels_per_ext_rel);
   2908     }
   2909 
   2910   if (esdo->rela.hdr
   2911       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2912 					      &alloc1, &alloc1_size,
   2913 					      internal_rela_relocs)))
   2914     goto error_return;
   2915 
   2916   /* Cache the results for next time, if we can.  */
   2917   if (keep_memory)
   2918     esdo->relocs = internal_relocs;
   2919 
   2920   _bfd_munmap_temporary (alloc1, alloc1_size);
   2921 
   2922   /* Don't free alloc2, since if it was allocated we are passing it
   2923      back (under the name of internal_relocs).  */
   2924 
   2925   return internal_relocs;
   2926 
   2927  error_return:
   2928   _bfd_munmap_temporary (alloc1, alloc1_size);
   2929   free (alloc2);
   2930   return NULL;
   2931 }
   2932 
   2933 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
   2934    NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
   2935    struct bfd_link_info.  */
   2936 
   2937 Elf_Internal_Rela *
   2938 _bfd_elf_link_read_relocs (bfd *abfd,
   2939 			   const asection *o,
   2940 			   void *external_relocs,
   2941 			   Elf_Internal_Rela *internal_relocs,
   2942 			   bool keep_memory)
   2943 {
   2944   return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
   2945 					 internal_relocs, keep_memory);
   2946 
   2947 }
   2948 
   2949 /* Compute the size of, and allocate space for, REL_HDR which is the
   2950    section header for a section containing relocations for O.  */
   2951 
   2952 static bool
   2953 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2954 				  struct bfd_elf_section_reloc_data *reldata)
   2955 {
   2956   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2957 
   2958   /* That allows us to calculate the size of the section.  */
   2959   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2960 
   2961   /* The contents field must last into write_object_contents, so we
   2962      allocate it with bfd_alloc rather than malloc.  Also since we
   2963      cannot be sure that the contents will actually be filled in,
   2964      we zero the allocated space.  */
   2965   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2966   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2967     return false;
   2968 
   2969   if (reldata->hashes == NULL && reldata->count)
   2970     {
   2971       struct elf_link_hash_entry **p;
   2972 
   2973       p = ((struct elf_link_hash_entry **)
   2974 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   2975       if (p == NULL)
   2976 	return false;
   2977 
   2978       reldata->hashes = p;
   2979     }
   2980 
   2981   return true;
   2982 }
   2983 
   2984 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2985    originated from the section given by INPUT_REL_HDR) to the
   2986    OUTPUT_BFD.  */
   2987 
   2988 bool
   2989 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2990 			     asection *input_section,
   2991 			     Elf_Internal_Shdr *input_rel_hdr,
   2992 			     Elf_Internal_Rela *internal_relocs,
   2993 			     struct elf_link_hash_entry **rel_hash)
   2994 {
   2995   Elf_Internal_Rela *irela;
   2996   Elf_Internal_Rela *irelaend;
   2997   bfd_byte *erel;
   2998   struct bfd_elf_section_reloc_data *output_reldata;
   2999   asection *output_section;
   3000   const struct elf_backend_data *bed;
   3001   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   3002   struct bfd_elf_section_data *esdo;
   3003 
   3004   output_section = input_section->output_section;
   3005 
   3006   bed = get_elf_backend_data (output_bfd);
   3007   esdo = elf_section_data (output_section);
   3008   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   3009     {
   3010       output_reldata = &esdo->rel;
   3011       swap_out = bed->s->swap_reloc_out;
   3012     }
   3013   else if (esdo->rela.hdr
   3014 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   3015     {
   3016       output_reldata = &esdo->rela;
   3017       swap_out = bed->s->swap_reloca_out;
   3018     }
   3019   else
   3020     {
   3021       _bfd_error_handler
   3022 	/* xgettext:c-format */
   3023 	(_("%pB: relocation size mismatch in %pB section %pA"),
   3024 	 output_bfd, input_section->owner, input_section);
   3025       bfd_set_error (bfd_error_wrong_format);
   3026       return false;
   3027     }
   3028 
   3029   erel = output_reldata->hdr->contents;
   3030   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   3031   irela = internal_relocs;
   3032   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   3033 		      * bed->s->int_rels_per_ext_rel);
   3034   while (irela < irelaend)
   3035     {
   3036       if (rel_hash && *rel_hash)
   3037 	(*rel_hash)->has_reloc = 1;
   3038       (*swap_out) (output_bfd, irela, erel);
   3039       irela += bed->s->int_rels_per_ext_rel;
   3040       erel += input_rel_hdr->sh_entsize;
   3041       if (rel_hash)
   3042 	rel_hash++;
   3043     }
   3044 
   3045   /* Bump the counter, so that we know where to add the next set of
   3046      relocations.  */
   3047   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   3048 
   3049   return true;
   3050 }
   3051 
   3052 /* Make weak undefined symbols in PIE dynamic.  */
   3054 
   3055 bool
   3056 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   3057 				 struct elf_link_hash_entry *h)
   3058 {
   3059   if (bfd_link_pie (info)
   3060       && h->dynindx == -1
   3061       && h->root.type == bfd_link_hash_undefweak)
   3062     return bfd_elf_link_record_dynamic_symbol (info, h);
   3063 
   3064   return true;
   3065 }
   3066 
   3067 /* Fix up the flags for a symbol.  This handles various cases which
   3068    can only be fixed after all the input files are seen.  This is
   3069    currently called by both adjust_dynamic_symbol and
   3070    assign_sym_version, which is unnecessary but perhaps more robust in
   3071    the face of future changes.  */
   3072 
   3073 static bool
   3074 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   3075 			   struct elf_info_failed *eif)
   3076 {
   3077   const struct elf_backend_data *bed;
   3078 
   3079   /* If this symbol was mentioned in a non-ELF file, try to set
   3080      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   3081      permit a non-ELF file to correctly refer to a symbol defined in
   3082      an ELF dynamic object.  */
   3083   if (h->non_elf)
   3084     {
   3085       while (h->root.type == bfd_link_hash_indirect)
   3086 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3087 
   3088       if (h->root.type != bfd_link_hash_defined
   3089 	  && h->root.type != bfd_link_hash_defweak)
   3090 	{
   3091 	  h->ref_regular = 1;
   3092 	  h->ref_regular_nonweak = 1;
   3093 	}
   3094       else
   3095 	{
   3096 	  if (h->root.u.def.section->owner != NULL
   3097 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   3098 		  == bfd_target_elf_flavour))
   3099 	    {
   3100 	      h->ref_regular = 1;
   3101 	      h->ref_regular_nonweak = 1;
   3102 	    }
   3103 	  else
   3104 	    h->def_regular = 1;
   3105 	}
   3106 
   3107       if (h->dynindx == -1
   3108 	  && (h->def_dynamic
   3109 	      || h->ref_dynamic))
   3110 	{
   3111 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3112 	    {
   3113 	      eif->failed = true;
   3114 	      return false;
   3115 	    }
   3116 	}
   3117     }
   3118   else
   3119     {
   3120       /* Unfortunately, NON_ELF is only correct if the symbol
   3121 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   3122 	 was first seen in an ELF file, we're probably OK unless the
   3123 	 symbol was defined in a non-ELF file.  Catch that case here.
   3124 	 FIXME: We're still in trouble if the symbol was first seen in
   3125 	 a dynamic object, and then later in a non-ELF regular object.  */
   3126       if ((h->root.type == bfd_link_hash_defined
   3127 	   || h->root.type == bfd_link_hash_defweak)
   3128 	  && !h->def_regular
   3129 	  && (h->root.u.def.section->owner != NULL
   3130 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   3131 		 != bfd_target_elf_flavour)
   3132 	      : (bfd_is_abs_section (h->root.u.def.section)
   3133 		 && !h->def_dynamic)))
   3134 	h->def_regular = 1;
   3135     }
   3136 
   3137   /* Backend specific symbol fixup.  */
   3138   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   3139   if (bed->elf_backend_fixup_symbol
   3140       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   3141     return false;
   3142 
   3143   /* If this is a final link, and the symbol was defined as a common
   3144      symbol in a regular object file, and there was no definition in
   3145      any dynamic object, then the linker will have allocated space for
   3146      the symbol in a common section but the DEF_REGULAR
   3147      flag will not have been set.  */
   3148   if (h->root.type == bfd_link_hash_defined
   3149       && !h->def_regular
   3150       && h->ref_regular
   3151       && !h->def_dynamic
   3152       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   3153     h->def_regular = 1;
   3154 
   3155   /* Symbols defined in discarded sections shouldn't be dynamic.  */
   3156   if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
   3157     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3158 
   3159   /* If a weak undefined symbol has non-default visibility, we also
   3160      hide it from the dynamic linker.  */
   3161   else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   3162 	   && h->root.type == bfd_link_hash_undefweak)
   3163     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3164 
   3165   /* A hidden versioned symbol in executable should be forced local if
   3166      it is is locally defined, not referenced by shared library and not
   3167      exported.  */
   3168   else if (bfd_link_executable (eif->info)
   3169 	   && h->versioned == versioned_hidden
   3170 	   && !eif->info->export_dynamic
   3171 	   && !h->dynamic
   3172 	   && !h->ref_dynamic
   3173 	   && h->def_regular)
   3174     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3175 
   3176   /* If -Bsymbolic was used (which means to bind references to global
   3177      symbols to the definition within the shared object), and this
   3178      symbol was defined in a regular object, then it actually doesn't
   3179      need a PLT entry.  Likewise, if the symbol has non-default
   3180      visibility.  If the symbol has hidden or internal visibility, we
   3181      will force it local.  */
   3182   else if (h->needs_plt
   3183 	   && bfd_link_pic (eif->info)
   3184 	   && is_elf_hash_table (eif->info->hash)
   3185 	   && (SYMBOLIC_BIND (eif->info, h)
   3186 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   3187 	   && h->def_regular)
   3188     {
   3189       bool force_local;
   3190 
   3191       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   3192 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   3193       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   3194     }
   3195 
   3196   /* If this is a weak defined symbol in a dynamic object, and we know
   3197      the real definition in the dynamic object, copy interesting flags
   3198      over to the real definition.  */
   3199   if (h->is_weakalias)
   3200     {
   3201       struct elf_link_hash_entry *def = weakdef (h);
   3202 
   3203       /* If the real definition is defined by a regular object file,
   3204 	 don't do anything special.  See the longer description in
   3205 	 _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
   3206 	 bfd_link_hash_defined as it was when put on the alias list
   3207 	 then it must have originally been a versioned symbol (for
   3208 	 which a non-versioned indirect symbol is created) and later
   3209 	 a definition for the non-versioned symbol is found.  In that
   3210 	 case the indirection is flipped with the versioned symbol
   3211 	 becoming an indirect pointing at the non-versioned symbol.
   3212 	 Thus, not an alias any more.  */
   3213       if (def->def_regular
   3214 	  || def->root.type != bfd_link_hash_defined)
   3215 	{
   3216 	  h = def;
   3217 	  while ((h = h->u.alias) != def)
   3218 	    h->is_weakalias = 0;
   3219 	}
   3220       else
   3221 	{
   3222 	  while (h->root.type == bfd_link_hash_indirect)
   3223 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3224 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   3225 		      || h->root.type == bfd_link_hash_defweak);
   3226 	  BFD_ASSERT (def->def_dynamic);
   3227 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
   3228 	}
   3229     }
   3230 
   3231   return true;
   3232 }
   3233 
   3234 /* Make the backend pick a good value for a dynamic symbol.  This is
   3235    called via elf_link_hash_traverse, and also calls itself
   3236    recursively.  */
   3237 
   3238 static bool
   3239 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   3240 {
   3241   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   3242   struct elf_link_hash_table *htab;
   3243   const struct elf_backend_data *bed;
   3244 
   3245   if (! is_elf_hash_table (eif->info->hash))
   3246     return false;
   3247 
   3248   /* Ignore indirect symbols.  These are added by the versioning code.  */
   3249   if (h->root.type == bfd_link_hash_indirect)
   3250     return true;
   3251 
   3252   /* Fix the symbol flags.  */
   3253   if (! _bfd_elf_fix_symbol_flags (h, eif))
   3254     return false;
   3255 
   3256   htab = elf_hash_table (eif->info);
   3257   bed = get_elf_backend_data (htab->dynobj);
   3258 
   3259   if (h->root.type == bfd_link_hash_undefweak)
   3260     {
   3261       if (eif->info->dynamic_undefined_weak == 0)
   3262 	(*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3263       else if (eif->info->dynamic_undefined_weak > 0
   3264 	       && h->ref_regular
   3265 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3266 	       && !bfd_hide_sym_by_version (eif->info->version_info,
   3267 					    h->root.root.string))
   3268 	{
   3269 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3270 	    {
   3271 	      eif->failed = true;
   3272 	      return false;
   3273 	    }
   3274 	}
   3275     }
   3276 
   3277   /* If this symbol does not require a PLT entry, and it is not
   3278      defined by a dynamic object, or is not referenced by a regular
   3279      object, ignore it.  We do have to handle a weak defined symbol,
   3280      even if no regular object refers to it, if we decided to add it
   3281      to the dynamic symbol table.  FIXME: Do we normally need to worry
   3282      about symbols which are defined by one dynamic object and
   3283      referenced by another one?  */
   3284   if (!h->needs_plt
   3285       && h->type != STT_GNU_IFUNC
   3286       && (h->def_regular
   3287 	  || !h->def_dynamic
   3288 	  || (!h->ref_regular
   3289 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
   3290     {
   3291       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   3292       return true;
   3293     }
   3294 
   3295   /* If we've already adjusted this symbol, don't do it again.  This
   3296      can happen via a recursive call.  */
   3297   if (h->dynamic_adjusted)
   3298     return true;
   3299 
   3300   /* Don't look at this symbol again.  Note that we must set this
   3301      after checking the above conditions, because we may look at a
   3302      symbol once, decide not to do anything, and then get called
   3303      recursively later after REF_REGULAR is set below.  */
   3304   h->dynamic_adjusted = 1;
   3305 
   3306   /* If this is a weak definition, and we know a real definition, and
   3307      the real symbol is not itself defined by a regular object file,
   3308      then get a good value for the real definition.  We handle the
   3309      real symbol first, for the convenience of the backend routine.
   3310 
   3311      Note that there is a confusing case here.  If the real definition
   3312      is defined by a regular object file, we don't get the real symbol
   3313      from the dynamic object, but we do get the weak symbol.  If the
   3314      processor backend uses a COPY reloc, then if some routine in the
   3315      dynamic object changes the real symbol, we will not see that
   3316      change in the corresponding weak symbol.  This is the way other
   3317      ELF linkers work as well, and seems to be a result of the shared
   3318      library model.
   3319 
   3320      I will clarify this issue.  Most SVR4 shared libraries define the
   3321      variable _timezone and define timezone as a weak synonym.  The
   3322      tzset call changes _timezone.  If you write
   3323        extern int timezone;
   3324        int _timezone = 5;
   3325        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   3326      you might expect that, since timezone is a synonym for _timezone,
   3327      the same number will print both times.  However, if the processor
   3328      backend uses a COPY reloc, then actually timezone will be copied
   3329      into your process image, and, since you define _timezone
   3330      yourself, _timezone will not.  Thus timezone and _timezone will
   3331      wind up at different memory locations.  The tzset call will set
   3332      _timezone, leaving timezone unchanged.  */
   3333 
   3334   if (h->is_weakalias)
   3335     {
   3336       struct elf_link_hash_entry *def = weakdef (h);
   3337 
   3338       /* If we get to this point, there is an implicit reference to
   3339 	 the alias by a regular object file via the weak symbol H.  */
   3340       def->ref_regular = 1;
   3341 
   3342       /* Ensure that the backend adjust_dynamic_symbol function sees
   3343 	 the strong alias before H by recursively calling ourselves.  */
   3344       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
   3345 	return false;
   3346     }
   3347 
   3348   /* If a symbol has no type and no size and does not require a PLT
   3349      entry, then we are probably about to do the wrong thing here: we
   3350      are probably going to create a COPY reloc for an empty object.
   3351      This case can arise when a shared object is built with assembly
   3352      code, and the assembly code fails to set the symbol type.  */
   3353   if (h->size == 0
   3354       && h->type == STT_NOTYPE
   3355       && !h->needs_plt)
   3356     _bfd_error_handler
   3357       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   3358        h->root.root.string);
   3359 
   3360   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   3361     {
   3362       eif->failed = true;
   3363       return false;
   3364     }
   3365 
   3366   return true;
   3367 }
   3368 
   3369 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   3370    DYNBSS.  */
   3371 
   3372 bool
   3373 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   3374 			      struct elf_link_hash_entry *h,
   3375 			      asection *dynbss)
   3376 {
   3377   unsigned int power_of_two;
   3378   bfd_vma mask;
   3379   asection *sec = h->root.u.def.section;
   3380 
   3381   /* The section alignment of the definition is the maximum alignment
   3382      requirement of symbols defined in the section.  Since we don't
   3383      know the symbol alignment requirement, we start with the
   3384      maximum alignment and check low bits of the symbol address
   3385      for the minimum alignment.  */
   3386   power_of_two = bfd_section_alignment (sec);
   3387   mask = ((bfd_vma) 1 << power_of_two) - 1;
   3388   while ((h->root.u.def.value & mask) != 0)
   3389     {
   3390        mask >>= 1;
   3391        --power_of_two;
   3392     }
   3393 
   3394   /* Adjust the section alignment if needed.  */
   3395   if (!bfd_link_align_section (dynbss, power_of_two))
   3396     return false;
   3397 
   3398   /* We make sure that the symbol will be aligned properly.  */
   3399   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   3400 
   3401   /* Define the symbol as being at this point in DYNBSS.  */
   3402   h->root.u.def.section = dynbss;
   3403   h->root.u.def.value = dynbss->size;
   3404 
   3405   /* Increment the size of DYNBSS to make room for the symbol.  */
   3406   dynbss->size += h->size;
   3407 
   3408   /* No error if extern_protected_data is true.  */
   3409   if (h->protected_def
   3410       && (!info->extern_protected_data
   3411 	  || (info->extern_protected_data < 0
   3412 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   3413     info->callbacks->einfo
   3414       (_("%P: copy reloc against protected `%pT' is dangerous\n"),
   3415        h->root.root.string);
   3416 
   3417   return true;
   3418 }
   3419 
   3420 /* Adjust all external symbols pointing into SEC_MERGE sections
   3421    to reflect the object merging within the sections.  */
   3422 
   3423 static bool
   3424 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   3425 {
   3426   asection *sec;
   3427 
   3428   if ((h->root.type == bfd_link_hash_defined
   3429        || h->root.type == bfd_link_hash_defweak)
   3430       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   3431       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3432     {
   3433       bfd *output_bfd = (bfd *) data;
   3434 
   3435       h->root.u.def.value =
   3436 	_bfd_merged_section_offset (output_bfd,
   3437 				    &h->root.u.def.section,
   3438 				    elf_section_data (sec)->sec_info,
   3439 				    h->root.u.def.value);
   3440     }
   3441 
   3442   return true;
   3443 }
   3444 
   3445 /* Returns false if the symbol referred to by H should be considered
   3446    to resolve local to the current module, and true if it should be
   3447    considered to bind dynamically.  */
   3448 
   3449 bool
   3450 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   3451 			   struct bfd_link_info *info,
   3452 			   bool not_local_protected)
   3453 {
   3454   bool binding_stays_local_p;
   3455   const struct elf_backend_data *bed;
   3456   struct elf_link_hash_table *hash_table;
   3457 
   3458   if (h == NULL)
   3459     return false;
   3460 
   3461   while (h->root.type == bfd_link_hash_indirect
   3462 	 || h->root.type == bfd_link_hash_warning)
   3463     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3464 
   3465   /* If it was forced local, then clearly it's not dynamic.  */
   3466   if (h->dynindx == -1)
   3467     return false;
   3468   if (h->forced_local)
   3469     return false;
   3470 
   3471   /* Identify the cases where name binding rules say that a
   3472      visible symbol resolves locally.  */
   3473   binding_stays_local_p = (bfd_link_executable (info)
   3474 			   || SYMBOLIC_BIND (info, h));
   3475 
   3476   switch (ELF_ST_VISIBILITY (h->other))
   3477     {
   3478     case STV_INTERNAL:
   3479     case STV_HIDDEN:
   3480       return false;
   3481 
   3482     case STV_PROTECTED:
   3483       hash_table = elf_hash_table (info);
   3484       if (!is_elf_hash_table (&hash_table->root))
   3485 	return false;
   3486 
   3487       bed = get_elf_backend_data (hash_table->dynobj);
   3488 
   3489       /* Proper resolution for function pointer equality may require
   3490 	 that these symbols perhaps be resolved dynamically, even though
   3491 	 we should be resolving them to the current module.  */
   3492       if (!not_local_protected || !bed->is_function_type (h->type))
   3493 	binding_stays_local_p = true;
   3494       break;
   3495 
   3496     default:
   3497       break;
   3498     }
   3499 
   3500   /* If it isn't defined locally, then clearly it's dynamic.  */
   3501   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   3502     return true;
   3503 
   3504   /* Otherwise, the symbol is dynamic if binding rules don't tell
   3505      us that it remains local.  */
   3506   return !binding_stays_local_p;
   3507 }
   3508 
   3509 /* Return true if the symbol referred to by H should be considered
   3510    to resolve local to the current module, and false otherwise.  Differs
   3511    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   3512    undefined symbols.  The two functions are virtually identical except
   3513    for the place where dynindx == -1 is tested.  If that test is true,
   3514    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
   3515    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
   3516    defined symbols.
   3517    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   3518    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   3519    treatment of undefined weak symbols.  For those that do not make
   3520    undefined weak symbols dynamic, both functions may return false.  */
   3521 
   3522 bool
   3523 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   3524 			      struct bfd_link_info *info,
   3525 			      bool local_protected)
   3526 {
   3527   const struct elf_backend_data *bed;
   3528   struct elf_link_hash_table *hash_table;
   3529 
   3530   /* If it's a local sym, of course we resolve locally.  */
   3531   if (h == NULL)
   3532     return true;
   3533 
   3534   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   3535   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   3536       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   3537     return true;
   3538 
   3539   /* Forced local symbols resolve locally.  */
   3540   if (h->forced_local)
   3541     return true;
   3542 
   3543   /* Common symbols that become definitions don't get the DEF_REGULAR
   3544      flag set, so test it first, and don't bail out.  */
   3545   if (ELF_COMMON_DEF_P (h))
   3546     /* Do nothing.  */;
   3547   /* If we don't have a definition in a regular file, then we can't
   3548      resolve locally.  The sym is either undefined or dynamic.  */
   3549   else if (!h->def_regular)
   3550     return false;
   3551 
   3552   /* Non-dynamic symbols resolve locally.  */
   3553   if (h->dynindx == -1)
   3554     return true;
   3555 
   3556   /* At this point, we know the symbol is defined and dynamic.  In an
   3557      executable it must resolve locally, likewise when building symbolic
   3558      shared libraries.  */
   3559   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3560     return true;
   3561 
   3562   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3563      with default visibility might not resolve locally.  */
   3564   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3565     return false;
   3566 
   3567   hash_table = elf_hash_table (info);
   3568   if (!is_elf_hash_table (&hash_table->root))
   3569     return true;
   3570 
   3571   /* STV_PROTECTED symbols with indirect external access are local. */
   3572   if (info->indirect_extern_access > 0)
   3573     return true;
   3574 
   3575   bed = get_elf_backend_data (hash_table->dynobj);
   3576 
   3577   /* If extern_protected_data is false, STV_PROTECTED non-function
   3578      symbols are local.  */
   3579   if ((!info->extern_protected_data
   3580        || (info->extern_protected_data < 0
   3581 	   && !bed->extern_protected_data))
   3582       && !bed->is_function_type (h->type))
   3583     return true;
   3584 
   3585   /* Function pointer equality tests may require that STV_PROTECTED
   3586      symbols be treated as dynamic symbols.  If the address of a
   3587      function not defined in an executable is set to that function's
   3588      plt entry in the executable, then the address of the function in
   3589      a shared library must also be the plt entry in the executable.  */
   3590   return local_protected;
   3591 }
   3592 
   3593 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3594    aligned.  Returns the first TLS output section.  */
   3595 
   3596 struct bfd_section *
   3597 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3598 {
   3599   struct bfd_section *sec, *tls;
   3600   unsigned int align = 0;
   3601 
   3602   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3603     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3604       break;
   3605   tls = sec;
   3606 
   3607   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3608     if (sec->alignment_power > align)
   3609       align = sec->alignment_power;
   3610 
   3611   elf_hash_table (info)->tls_sec = tls;
   3612 
   3613   /* Ensure the alignment of the first section (usually .tdata) is the largest
   3614      alignment, so that the tls segment starts aligned.  */
   3615   if (tls != NULL)
   3616     (void) bfd_link_align_section (tls, align);
   3617 
   3618   return tls;
   3619 }
   3620 
   3621 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3622 static bool
   3623 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3624 				  Elf_Internal_Sym *sym)
   3625 {
   3626   const struct elf_backend_data *bed;
   3627 
   3628   /* Local symbols do not count, but target specific ones might.  */
   3629   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3630       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3631     return false;
   3632 
   3633   bed = get_elf_backend_data (abfd);
   3634   /* Function symbols do not count.  */
   3635   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3636     return false;
   3637 
   3638   /* If the section is undefined, then so is the symbol.  */
   3639   if (sym->st_shndx == SHN_UNDEF)
   3640     return false;
   3641 
   3642   /* If the symbol is defined in the common section, then
   3643      it is a common definition and so does not count.  */
   3644   if (bed->common_definition (sym))
   3645     return false;
   3646 
   3647   /* If the symbol is in a target specific section then we
   3648      must rely upon the backend to tell us what it is.  */
   3649   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3650     /* FIXME - this function is not coded yet:
   3651 
   3652        return _bfd_is_global_symbol_definition (abfd, sym);
   3653 
   3654        Instead for now assume that the definition is not global,
   3655        Even if this is wrong, at least the linker will behave
   3656        in the same way that it used to do.  */
   3657     return false;
   3658 
   3659   return true;
   3660 }
   3661 
   3662 /* Search the symbol table of the archive element of the archive ABFD
   3663    whose archive map contains a mention of SYMDEF, and determine if
   3664    the symbol is defined in this element.  */
   3665 static bool
   3666 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3667 {
   3668   Elf_Internal_Shdr * hdr;
   3669   size_t symcount;
   3670   size_t extsymcount;
   3671   size_t extsymoff;
   3672   Elf_Internal_Sym *isymbuf;
   3673   Elf_Internal_Sym *isym;
   3674   Elf_Internal_Sym *isymend;
   3675   bool result;
   3676 
   3677   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
   3678   if (abfd == NULL)
   3679     return false;
   3680 
   3681   if (! bfd_check_format (abfd, bfd_object))
   3682     return false;
   3683 
   3684   /* Select the appropriate symbol table.  If we don't know if the
   3685      object file is an IR object, give linker LTO plugin a chance to
   3686      get the correct symbol table.  */
   3687   if (abfd->plugin_format == bfd_plugin_yes
   3688       || abfd->plugin_format == bfd_plugin_yes_unused
   3689 #if BFD_SUPPORTS_PLUGINS
   3690       || (abfd->plugin_format == bfd_plugin_unknown
   3691 	  && bfd_link_plugin_object_p (abfd))
   3692 #endif
   3693       )
   3694     {
   3695       /* Use the IR symbol table if the object has been claimed by
   3696 	 plugin.  */
   3697       abfd = abfd->plugin_dummy_bfd;
   3698       hdr = &elf_tdata (abfd)->symtab_hdr;
   3699     }
   3700   else
   3701     {
   3702       if (elf_use_dt_symtab_p (abfd))
   3703 	{
   3704 	  bfd_set_error (bfd_error_wrong_format);
   3705 	  return false;
   3706 	}
   3707 
   3708       if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3709 	hdr = &elf_tdata (abfd)->symtab_hdr;
   3710       else
   3711 	hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3712     }
   3713 
   3714   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3715 
   3716   /* The sh_info field of the symtab header tells us where the
   3717      external symbols start.  We don't care about the local symbols.  */
   3718   if (elf_bad_symtab (abfd))
   3719     {
   3720       extsymcount = symcount;
   3721       extsymoff = 0;
   3722     }
   3723   else
   3724     {
   3725       extsymcount = symcount - hdr->sh_info;
   3726       extsymoff = hdr->sh_info;
   3727     }
   3728 
   3729   if (extsymcount == 0)
   3730     return false;
   3731 
   3732   /* Read in the symbol table.  */
   3733   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3734 				  NULL, NULL, NULL);
   3735   if (isymbuf == NULL)
   3736     return false;
   3737 
   3738   /* Scan the symbol table looking for SYMDEF.  */
   3739   result = false;
   3740   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3741     {
   3742       const char *name;
   3743 
   3744       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3745 					      isym->st_name);
   3746       if (name == NULL)
   3747 	break;
   3748 
   3749       if (strcmp (name, symdef->name) == 0)
   3750 	{
   3751 	  result = is_global_data_symbol_definition (abfd, isym);
   3752 	  break;
   3753 	}
   3754     }
   3755 
   3756   free (isymbuf);
   3757 
   3758   return result;
   3759 }
   3760 
   3761 /* Add an entry to the .dynamic table.  */
   3763 
   3764 bool
   3765 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3766 			    bfd_vma tag,
   3767 			    bfd_vma val)
   3768 {
   3769   struct elf_link_hash_table *hash_table;
   3770   const struct elf_backend_data *bed;
   3771   asection *s;
   3772   bfd_size_type newsize;
   3773   bfd_byte *newcontents;
   3774   Elf_Internal_Dyn dyn;
   3775 
   3776   hash_table = elf_hash_table (info);
   3777   if (! is_elf_hash_table (&hash_table->root))
   3778     return false;
   3779 
   3780   if (tag == DT_RELA || tag == DT_REL)
   3781     hash_table->dynamic_relocs = true;
   3782 
   3783   bed = get_elf_backend_data (hash_table->dynobj);
   3784   s = hash_table->dynamic;
   3785   BFD_ASSERT (s != NULL);
   3786 
   3787   newsize = s->size + bed->s->sizeof_dyn;
   3788   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3789   if (newcontents == NULL)
   3790     return false;
   3791 
   3792   dyn.d_tag = tag;
   3793   dyn.d_un.d_val = val;
   3794   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3795 
   3796   s->size = newsize;
   3797   s->contents = newcontents;
   3798 
   3799   return true;
   3800 }
   3801 
   3802 /* Strip zero-sized dynamic sections.  */
   3803 
   3804 bool
   3805 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
   3806 {
   3807   struct elf_link_hash_table *hash_table;
   3808   const struct elf_backend_data *bed;
   3809   asection *s, *sdynamic, **pp;
   3810   asection *rela_dyn, *rel_dyn;
   3811   Elf_Internal_Dyn dyn;
   3812   bfd_byte *extdyn, *next;
   3813   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3814   bool strip_zero_sized;
   3815   bool strip_zero_sized_plt;
   3816 
   3817   if (bfd_link_relocatable (info))
   3818     return true;
   3819 
   3820   hash_table = elf_hash_table (info);
   3821   if (!is_elf_hash_table (&hash_table->root))
   3822     return false;
   3823 
   3824   if (!hash_table->dynobj)
   3825     return true;
   3826 
   3827   sdynamic= hash_table->dynamic;
   3828   if (!sdynamic)
   3829     return true;
   3830 
   3831   bed = get_elf_backend_data (hash_table->dynobj);
   3832   swap_dyn_in = bed->s->swap_dyn_in;
   3833 
   3834   strip_zero_sized = false;
   3835   strip_zero_sized_plt = false;
   3836 
   3837   /* Strip zero-sized dynamic sections.  */
   3838   rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
   3839   rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
   3840   for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
   3841     if (s->size == 0
   3842 	&& (s == rela_dyn
   3843 	    || s == rel_dyn
   3844 	    || s == hash_table->srelplt->output_section
   3845 	    || s == hash_table->splt->output_section))
   3846       {
   3847 	*pp = s->next;
   3848 	info->output_bfd->section_count--;
   3849 	strip_zero_sized = true;
   3850 	if (s == rela_dyn)
   3851 	  s = rela_dyn;
   3852 	if (s == rel_dyn)
   3853 	  s = rel_dyn;
   3854 	else if (s == hash_table->splt->output_section)
   3855 	  {
   3856 	    s = hash_table->splt;
   3857 	    strip_zero_sized_plt = true;
   3858 	  }
   3859 	else
   3860 	  s = hash_table->srelplt;
   3861 	s->flags |= SEC_EXCLUDE;
   3862 	s->output_section = bfd_abs_section_ptr;
   3863       }
   3864     else
   3865       pp = &s->next;
   3866 
   3867   if (strip_zero_sized_plt && sdynamic->size != 0)
   3868     for (extdyn = sdynamic->contents;
   3869 	 extdyn < sdynamic->contents + sdynamic->size;
   3870 	 extdyn = next)
   3871       {
   3872 	next = extdyn + bed->s->sizeof_dyn;
   3873 	swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3874 	switch (dyn.d_tag)
   3875 	  {
   3876 	  default:
   3877 	    break;
   3878 	  case DT_JMPREL:
   3879 	  case DT_PLTRELSZ:
   3880 	  case DT_PLTREL:
   3881 	    /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
   3882 	       the procedure linkage table (the .plt section) has been
   3883 	       removed.  */
   3884 	    memmove (extdyn, next,
   3885 		     sdynamic->size - (next - sdynamic->contents));
   3886 	    next = extdyn;
   3887 	  }
   3888       }
   3889 
   3890   if (strip_zero_sized)
   3891     {
   3892       /* Regenerate program headers.  */
   3893       elf_seg_map (info->output_bfd) = NULL;
   3894       return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
   3895 						NULL);
   3896     }
   3897 
   3898   return true;
   3899 }
   3900 
   3901 /* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
   3902    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3903 
   3904 int
   3905 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
   3906 {
   3907   struct elf_link_hash_table *hash_table;
   3908   size_t strindex;
   3909   const char *soname;
   3910 
   3911   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3912     return -1;
   3913 
   3914   hash_table = elf_hash_table (info);
   3915   soname = elf_dt_name (abfd);
   3916   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
   3917   if (strindex == (size_t) -1)
   3918     return -1;
   3919 
   3920   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3921     {
   3922       asection *sdyn;
   3923       const struct elf_backend_data *bed;
   3924       bfd_byte *extdyn;
   3925 
   3926       bed = get_elf_backend_data (hash_table->dynobj);
   3927       sdyn = hash_table->dynamic;
   3928       if (sdyn != NULL && sdyn->size != 0)
   3929 	for (extdyn = sdyn->contents;
   3930 	     extdyn < sdyn->contents + sdyn->size;
   3931 	     extdyn += bed->s->sizeof_dyn)
   3932 	  {
   3933 	    Elf_Internal_Dyn dyn;
   3934 
   3935 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3936 	    if (dyn.d_tag == DT_NEEDED
   3937 		&& dyn.d_un.d_val == strindex)
   3938 	      {
   3939 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3940 		return 1;
   3941 	      }
   3942 	  }
   3943     }
   3944 
   3945   if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3946     return -1;
   3947 
   3948   if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3949     return -1;
   3950 
   3951   return 0;
   3952 }
   3953 
   3954 /* Return true if SONAME is on the needed list between NEEDED and STOP
   3955    (or the end of list if STOP is NULL), and needed by a library that
   3956    will be loaded.  */
   3957 
   3958 static bool
   3959 on_needed_list (const char *soname,
   3960 		struct bfd_link_needed_list *needed,
   3961 		struct bfd_link_needed_list *stop)
   3962 {
   3963   struct bfd_link_needed_list *look;
   3964   for (look = needed; look != stop; look = look->next)
   3965     if (strcmp (soname, look->name) == 0
   3966 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
   3967 	    /* If needed by a library that itself is not directly
   3968 	       needed, recursively check whether that library is
   3969 	       indirectly needed.  Since we add DT_NEEDED entries to
   3970 	       the end of the list, library dependencies appear after
   3971 	       the library.  Therefore search prior to the current
   3972 	       LOOK, preventing possible infinite recursion.  */
   3973 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
   3974       return true;
   3975 
   3976   return false;
   3977 }
   3978 
   3979 /* Sort symbol by value, section, size, and type.  */
   3980 static int
   3981 elf_sort_symbol (const void *arg1, const void *arg2)
   3982 {
   3983   const struct elf_link_hash_entry *h1;
   3984   const struct elf_link_hash_entry *h2;
   3985   bfd_signed_vma vdiff;
   3986   int sdiff;
   3987   const char *n1;
   3988   const char *n2;
   3989 
   3990   h1 = *(const struct elf_link_hash_entry **) arg1;
   3991   h2 = *(const struct elf_link_hash_entry **) arg2;
   3992   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3993   if (vdiff != 0)
   3994     return vdiff > 0 ? 1 : -1;
   3995 
   3996   sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3997   if (sdiff != 0)
   3998     return sdiff;
   3999 
   4000   /* Sort so that sized symbols are selected over zero size symbols.  */
   4001   vdiff = h1->size - h2->size;
   4002   if (vdiff != 0)
   4003     return vdiff > 0 ? 1 : -1;
   4004 
   4005   /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
   4006   if (h1->type != h2->type)
   4007     return h1->type - h2->type;
   4008 
   4009   /* If symbols are properly sized and typed, and multiple strong
   4010      aliases are not defined in a shared library by the user we
   4011      shouldn't get here.  Unfortunately linker script symbols like
   4012      __bss_start sometimes match a user symbol defined at the start of
   4013      .bss without proper size and type.  We'd like to preference the
   4014      user symbol over reserved system symbols.  Sort on leading
   4015      underscores.  */
   4016   n1 = h1->root.root.string;
   4017   n2 = h2->root.root.string;
   4018   while (*n1 == *n2)
   4019     {
   4020       if (*n1 == 0)
   4021 	break;
   4022       ++n1;
   4023       ++n2;
   4024     }
   4025   if (*n1 == '_')
   4026     return -1;
   4027   if (*n2 == '_')
   4028     return 1;
   4029 
   4030   /* Final sort on name selects user symbols like '_u' over reserved
   4031      system symbols like '_Z' and also will avoid qsort instability.  */
   4032   return *n1 - *n2;
   4033 }
   4034 
   4035 /* This function is used to adjust offsets into .dynstr for
   4036    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   4037 
   4038 static bool
   4039 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   4040 {
   4041   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   4042 
   4043   if (h->dynindx != -1)
   4044     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   4045   return true;
   4046 }
   4047 
   4048 /* Assign string offsets in .dynstr, update all structures referencing
   4049    them.  */
   4050 
   4051 static bool
   4052 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   4053 {
   4054   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   4055   struct elf_link_local_dynamic_entry *entry;
   4056   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   4057   bfd *dynobj = hash_table->dynobj;
   4058   asection *sdyn;
   4059   bfd_size_type size;
   4060   const struct elf_backend_data *bed;
   4061   bfd_byte *extdyn;
   4062 
   4063   _bfd_elf_strtab_finalize (dynstr);
   4064   size = _bfd_elf_strtab_size (dynstr);
   4065 
   4066   /* Allow the linker to examine the dynsymtab now it's fully populated.  */
   4067 
   4068   if (info->callbacks->examine_strtab)
   4069     info->callbacks->examine_strtab (dynstr);
   4070 
   4071   bed = get_elf_backend_data (dynobj);
   4072   sdyn = hash_table->dynamic;
   4073   BFD_ASSERT (sdyn != NULL);
   4074 
   4075   /* Update all .dynamic entries referencing .dynstr strings.  */
   4076   for (extdyn = sdyn->contents;
   4077        extdyn < PTR_ADD (sdyn->contents, sdyn->size);
   4078        extdyn += bed->s->sizeof_dyn)
   4079     {
   4080       Elf_Internal_Dyn dyn;
   4081 
   4082       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   4083       switch (dyn.d_tag)
   4084 	{
   4085 	case DT_STRSZ:
   4086 	  dyn.d_un.d_val = size;
   4087 	  break;
   4088 	case DT_NEEDED:
   4089 	case DT_SONAME:
   4090 	case DT_RPATH:
   4091 	case DT_RUNPATH:
   4092 	case DT_FILTER:
   4093 	case DT_AUXILIARY:
   4094 	case DT_AUDIT:
   4095 	case DT_DEPAUDIT:
   4096 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   4097 	  break;
   4098 	default:
   4099 	  continue;
   4100 	}
   4101       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   4102     }
   4103 
   4104   /* Now update local dynamic symbols.  */
   4105   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   4106     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   4107 						  entry->isym.st_name);
   4108 
   4109   /* And the rest of dynamic symbols.  */
   4110   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   4111 
   4112   /* Adjust version definitions.  */
   4113   if (elf_tdata (output_bfd)->cverdefs)
   4114     {
   4115       asection *s;
   4116       bfd_byte *p;
   4117       size_t i;
   4118       Elf_Internal_Verdef def;
   4119       Elf_Internal_Verdaux defaux;
   4120 
   4121       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   4122       p = s->contents;
   4123       do
   4124 	{
   4125 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   4126 				   &def);
   4127 	  p += sizeof (Elf_External_Verdef);
   4128 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   4129 	    continue;
   4130 	  for (i = 0; i < def.vd_cnt; ++i)
   4131 	    {
   4132 	      _bfd_elf_swap_verdaux_in (output_bfd,
   4133 					(Elf_External_Verdaux *) p, &defaux);
   4134 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   4135 							defaux.vda_name);
   4136 	      _bfd_elf_swap_verdaux_out (output_bfd,
   4137 					 &defaux, (Elf_External_Verdaux *) p);
   4138 	      p += sizeof (Elf_External_Verdaux);
   4139 	    }
   4140 	}
   4141       while (def.vd_next);
   4142     }
   4143 
   4144   /* Adjust version references.  */
   4145   if (elf_tdata (output_bfd)->verref)
   4146     {
   4147       asection *s;
   4148       bfd_byte *p;
   4149       size_t i;
   4150       Elf_Internal_Verneed need;
   4151       Elf_Internal_Vernaux needaux;
   4152 
   4153       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   4154       p = s->contents;
   4155       do
   4156 	{
   4157 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   4158 				    &need);
   4159 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   4160 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   4161 				     (Elf_External_Verneed *) p);
   4162 	  p += sizeof (Elf_External_Verneed);
   4163 	  for (i = 0; i < need.vn_cnt; ++i)
   4164 	    {
   4165 	      _bfd_elf_swap_vernaux_in (output_bfd,
   4166 					(Elf_External_Vernaux *) p, &needaux);
   4167 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   4168 							 needaux.vna_name);
   4169 	      _bfd_elf_swap_vernaux_out (output_bfd,
   4170 					 &needaux,
   4171 					 (Elf_External_Vernaux *) p);
   4172 	      p += sizeof (Elf_External_Vernaux);
   4173 	    }
   4174 	}
   4175       while (need.vn_next);
   4176     }
   4177 
   4178   return true;
   4179 }
   4180 
   4181 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4183    The default is to only match when the INPUT and OUTPUT are exactly
   4184    the same target.  */
   4185 
   4186 bool
   4187 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   4188 				    const bfd_target *output)
   4189 {
   4190   return input == output;
   4191 }
   4192 
   4193 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4194    This version is used when different targets for the same architecture
   4195    are virtually identical.  */
   4196 
   4197 bool
   4198 _bfd_elf_relocs_compatible (const bfd_target *input,
   4199 			    const bfd_target *output)
   4200 {
   4201   const struct elf_backend_data *obed, *ibed;
   4202 
   4203   if (input == output)
   4204     return true;
   4205 
   4206   ibed = xvec_get_elf_backend_data (input);
   4207   obed = xvec_get_elf_backend_data (output);
   4208 
   4209   if (ibed->arch != obed->arch)
   4210     return false;
   4211 
   4212   /* If both backends are using this function, deem them compatible.  */
   4213   return ibed->relocs_compatible == obed->relocs_compatible;
   4214 }
   4215 
   4216 /* Make a special call to the linker "notice" function to tell it that
   4217    we are about to handle an as-needed lib, or have finished
   4218    processing the lib.  */
   4219 
   4220 bool
   4221 _bfd_elf_notice_as_needed (bfd *ibfd,
   4222 			   struct bfd_link_info *info,
   4223 			   enum notice_asneeded_action act)
   4224 {
   4225   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   4226 }
   4227 
   4228 /* Call ACTION on each relocation in an ELF object file.  */
   4229 
   4230 bool
   4231 _bfd_elf_link_iterate_on_relocs
   4232   (bfd *abfd, struct bfd_link_info *info,
   4233    bool (*action) (bfd *, struct bfd_link_info *, asection *,
   4234 		   const Elf_Internal_Rela *))
   4235 {
   4236   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4237   struct elf_link_hash_table *htab = elf_hash_table (info);
   4238 
   4239   /* If this object is the same format as the output object, and it is
   4240      not a shared library, then let the backend look through the
   4241      relocs.
   4242 
   4243      This is required to build global offset table entries and to
   4244      arrange for dynamic relocs.  It is not required for the
   4245      particular common case of linking non PIC code, even when linking
   4246      against shared libraries, but unfortunately there is no way of
   4247      knowing whether an object file has been compiled PIC or not.
   4248      Looking through the relocs is not particularly time consuming.
   4249      The problem is that we must either (1) keep the relocs in memory,
   4250      which causes the linker to require additional runtime memory or
   4251      (2) read the relocs twice from the input file, which wastes time.
   4252      This would be a good case for using mmap.
   4253 
   4254      I have no idea how to handle linking PIC code into a file of a
   4255      different format.  It probably can't be done.  */
   4256   if ((abfd->flags & DYNAMIC) == 0
   4257       && is_elf_hash_table (&htab->root)
   4258       && elf_object_id (abfd) == elf_hash_table_id (htab)
   4259       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   4260     {
   4261       asection *o;
   4262 
   4263       for (o = abfd->sections; o != NULL; o = o->next)
   4264 	{
   4265 	  Elf_Internal_Rela *internal_relocs;
   4266 	  bool ok;
   4267 
   4268 	  /* Don't check relocations in excluded sections.  Don't do
   4269 	     anything special with non-loaded, non-alloced sections.
   4270 	     In particular, any relocs in such sections should not
   4271 	     affect GOT and PLT reference counting (ie.  we don't
   4272 	     allow them to create GOT or PLT entries), there's no
   4273 	     possibility or desire to optimize TLS relocs, and
   4274 	     there's not much point in propagating relocs to shared
   4275 	     libs that the dynamic linker won't relocate.  */
   4276 	  if ((o->flags & SEC_ALLOC) == 0
   4277 	      || (o->flags & SEC_RELOC) == 0
   4278 	      || (o->flags & SEC_EXCLUDE) != 0
   4279 	      || o->reloc_count == 0
   4280 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   4281 		  && (o->flags & SEC_DEBUGGING) != 0)
   4282 	      || bfd_is_abs_section (o->output_section))
   4283 	    continue;
   4284 
   4285 	  internal_relocs = _bfd_elf_link_info_read_relocs
   4286 	    (abfd, info, o, NULL, NULL,
   4287 	     _bfd_elf_link_keep_memory (info));
   4288 	  if (internal_relocs == NULL)
   4289 	    return false;
   4290 
   4291 	  ok = action (abfd, info, o, internal_relocs);
   4292 
   4293 	  if (elf_section_data (o)->relocs != internal_relocs)
   4294 	    free (internal_relocs);
   4295 
   4296 	  if (! ok)
   4297 	    return false;
   4298 	}
   4299     }
   4300 
   4301   return true;
   4302 }
   4303 
   4304 /* Check relocations in an ELF object file.  This is called after
   4305    all input files have been opened.  */
   4306 
   4307 bool
   4308 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   4309 {
   4310   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4311   if (bed->check_relocs != NULL)
   4312     return _bfd_elf_link_iterate_on_relocs (abfd, info,
   4313 					    bed->check_relocs);
   4314   return true;
   4315 }
   4316 
   4317 /* An entry in the first definition hash table.  */
   4318 
   4319 struct elf_link_first_hash_entry
   4320 {
   4321   struct bfd_hash_entry root;
   4322   /* The object of the first definition.  */
   4323   bfd *abfd;
   4324 };
   4325 
   4326 /* The function to create a new entry in the first definition hash
   4327    table.  */
   4328 
   4329 static struct bfd_hash_entry *
   4330 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
   4331 			     struct bfd_hash_table *table,
   4332 			     const char *string)
   4333 {
   4334   struct elf_link_first_hash_entry *ret =
   4335     (struct elf_link_first_hash_entry *) entry;
   4336 
   4337   /* Allocate the structure if it has not already been allocated by a
   4338      subclass.  */
   4339   if (ret == NULL)
   4340     ret = (struct elf_link_first_hash_entry *)
   4341 	bfd_hash_allocate (table,
   4342 			   sizeof (struct elf_link_first_hash_entry));
   4343   if (ret == NULL)
   4344     return NULL;
   4345 
   4346   /* Call the allocation method of the superclass.  */
   4347   ret = ((struct elf_link_first_hash_entry *)
   4348 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
   4349 			   string));
   4350   if (ret != NULL)
   4351     ret->abfd = NULL;
   4352 
   4353   return (struct bfd_hash_entry *) ret;
   4354 }
   4355 
   4356 /* Add the symbol NAME from ABFD to first hash.  */
   4357 
   4358 static void
   4359 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
   4360 			    const char *name, bool copy)
   4361 {
   4362   struct elf_link_hash_table *htab = elf_hash_table (info);
   4363   /* Skip if there is no first hash.  */
   4364   if (htab->first_hash == NULL)
   4365     return;
   4366 
   4367   struct elf_link_first_hash_entry *e
   4368     = ((struct elf_link_first_hash_entry *)
   4369        bfd_hash_lookup (htab->first_hash, name, true, copy));
   4370   if (e == NULL)
   4371     info->callbacks->fatal
   4372       (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
   4373 
   4374   if (e->abfd == NULL)
   4375     /* Store ABFD in abfd.  */
   4376     e->abfd = abfd;
   4377 }
   4378 
   4379 /* Add symbols from an ELF object file to the linker hash table.  */
   4380 
   4381 static bool
   4382 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   4383 {
   4384   Elf_Internal_Ehdr *ehdr;
   4385   Elf_Internal_Shdr *hdr;
   4386   size_t symcount;
   4387   size_t extsymcount;
   4388   size_t extsymoff;
   4389   struct elf_link_hash_entry **sym_hash;
   4390   bool dynamic;
   4391   Elf_External_Versym *extversym = NULL;
   4392   Elf_External_Versym *extversym_end = NULL;
   4393   Elf_External_Versym *ever;
   4394   struct elf_link_hash_entry *weaks;
   4395   struct elf_link_hash_entry **nondeflt_vers = NULL;
   4396   size_t nondeflt_vers_cnt = 0;
   4397   Elf_Internal_Sym *isymbuf = NULL;
   4398   Elf_Internal_Sym *isym;
   4399   Elf_Internal_Sym *isymend;
   4400   const struct elf_backend_data *bed;
   4401   bool add_needed;
   4402   struct elf_link_hash_table *htab;
   4403   void *alloc_mark = NULL;
   4404   struct bfd_hash_entry **old_table = NULL;
   4405   unsigned int old_size = 0;
   4406   unsigned int old_count = 0;
   4407   void *old_tab = NULL;
   4408   void *old_ent;
   4409   struct bfd_link_hash_entry *old_undefs = NULL;
   4410   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   4411   void *old_strtab = NULL;
   4412   size_t tabsize = 0;
   4413   asection *s;
   4414   bool just_syms;
   4415 
   4416   htab = elf_hash_table (info);
   4417   bed = get_elf_backend_data (abfd);
   4418 
   4419   if (elf_use_dt_symtab_p (abfd))
   4420     {
   4421       bfd_set_error (bfd_error_wrong_format);
   4422       return false;
   4423     }
   4424 
   4425   if ((abfd->flags & DYNAMIC) == 0)
   4426     {
   4427       dynamic = false;
   4428       if ((abfd->flags & BFD_PLUGIN) != 0
   4429 	  && is_elf_hash_table (&htab->root)
   4430 	  && htab->first_hash == NULL)
   4431 	{
   4432 	  /* Initialize first_hash for an IR input.  */
   4433 	  htab->first_hash = (struct bfd_hash_table *)
   4434 	    bfd_malloc (sizeof (struct bfd_hash_table));
   4435 	  if (htab->first_hash == NULL
   4436 	      || !bfd_hash_table_init
   4437 		   (htab->first_hash, elf_link_first_hash_newfunc,
   4438 		    sizeof (struct elf_link_first_hash_entry)))
   4439 	    info->callbacks->fatal
   4440 	      (_("%P: first_hash failed to create: %E\n"));
   4441 	}
   4442     }
   4443   else
   4444     {
   4445       dynamic = true;
   4446 
   4447       /* You can't use -r against a dynamic object.  Also, there's no
   4448 	 hope of using a dynamic object which does not exactly match
   4449 	 the format of the output file.  */
   4450       if (bfd_link_relocatable (info)
   4451 	  || !is_elf_hash_table (&htab->root)
   4452 	  || info->output_bfd->xvec != abfd->xvec)
   4453 	{
   4454 	  if (bfd_link_relocatable (info))
   4455 	    bfd_set_error (bfd_error_invalid_operation);
   4456 	  else
   4457 	    bfd_set_error (bfd_error_wrong_format);
   4458 	  goto error_return;
   4459 	}
   4460     }
   4461 
   4462   ehdr = elf_elfheader (abfd);
   4463   if (info->warn_alternate_em
   4464       && bed->elf_machine_code != ehdr->e_machine
   4465       && ((bed->elf_machine_alt1 != 0
   4466 	   && ehdr->e_machine == bed->elf_machine_alt1)
   4467 	  || (bed->elf_machine_alt2 != 0
   4468 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   4469     _bfd_error_handler
   4470       /* xgettext:c-format */
   4471       (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
   4472        ehdr->e_machine, abfd, bed->elf_machine_code);
   4473 
   4474   /* As a GNU extension, any input sections which are named
   4475      .gnu.warning.SYMBOL are treated as warning symbols for the given
   4476      symbol.  This differs from .gnu.warning sections, which generate
   4477      warnings when they are included in an output file.  */
   4478   /* PR 12761: Also generate this warning when building shared libraries.  */
   4479   for (s = abfd->sections; s != NULL; s = s->next)
   4480     {
   4481       const char *name;
   4482 
   4483       name = bfd_section_name (s);
   4484       if (startswith (name, ".gnu.warning."))
   4485 	{
   4486 	  char *msg;
   4487 	  bfd_size_type sz;
   4488 
   4489 	  name += sizeof ".gnu.warning." - 1;
   4490 
   4491 	  /* If this is a shared object, then look up the symbol
   4492 	     in the hash table.  If it is there, and it is already
   4493 	     been defined, then we will not be using the entry
   4494 	     from this shared object, so we don't need to warn.
   4495 	     FIXME: If we see the definition in a regular object
   4496 	     later on, we will warn, but we shouldn't.  The only
   4497 	     fix is to keep track of what warnings we are supposed
   4498 	     to emit, and then handle them all at the end of the
   4499 	     link.  */
   4500 	  if (dynamic)
   4501 	    {
   4502 	      struct elf_link_hash_entry *h;
   4503 
   4504 	      h = elf_link_hash_lookup (htab, name, false, false, true);
   4505 
   4506 	      /* FIXME: What about bfd_link_hash_common?  */
   4507 	      if (h != NULL
   4508 		  && (h->root.type == bfd_link_hash_defined
   4509 		      || h->root.type == bfd_link_hash_defweak))
   4510 		continue;
   4511 	    }
   4512 
   4513 	  sz = s->size;
   4514 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   4515 	  if (msg == NULL)
   4516 	    goto error_return;
   4517 
   4518 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   4519 	    goto error_return;
   4520 
   4521 	  msg[sz] = '\0';
   4522 
   4523 	  if (! (_bfd_generic_link_add_one_symbol
   4524 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   4525 		  false, bed->collect, NULL)))
   4526 	    goto error_return;
   4527 
   4528 	  if (bfd_link_executable (info))
   4529 	    {
   4530 	      /* Clobber the section size so that the warning does
   4531 		 not get copied into the output file.  */
   4532 	      s->size = 0;
   4533 
   4534 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   4535 		 the warning section don't get copied to the output.  */
   4536 	      s->flags |= SEC_EXCLUDE;
   4537 	    }
   4538 	}
   4539     }
   4540 
   4541   just_syms = ((s = abfd->sections) != NULL
   4542 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   4543 
   4544   add_needed = true;
   4545   if (! dynamic)
   4546     {
   4547       /* If we are creating a shared library, create all the dynamic
   4548 	 sections immediately.  We need to attach them to something,
   4549 	 so we attach them to this BFD, provided it is the right
   4550 	 format and is not from ld --just-symbols.  Always create the
   4551 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
   4552 	 are no input BFD's of the same format as the output, we can't
   4553 	 make a shared library.  */
   4554       if (!just_syms
   4555 	  && (bfd_link_pic (info)
   4556 	      || (!bfd_link_relocatable (info)
   4557 		  && info->nointerp
   4558 		  && (info->export_dynamic || info->dynamic)))
   4559 	  && is_elf_hash_table (&htab->root)
   4560 	  && info->output_bfd->xvec == abfd->xvec
   4561 	  && !htab->dynamic_sections_created)
   4562 	{
   4563 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   4564 	    goto error_return;
   4565 	}
   4566     }
   4567   else if (!is_elf_hash_table (&htab->root))
   4568     goto error_return;
   4569   else
   4570     {
   4571       const char *soname = NULL;
   4572       char *audit = NULL;
   4573       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   4574       const Elf_Internal_Phdr *phdr;
   4575       struct elf_link_loaded_list *loaded_lib;
   4576 
   4577       /* ld --just-symbols and dynamic objects don't mix very well.
   4578 	 ld shouldn't allow it.  */
   4579       if (just_syms)
   4580 	abort ();
   4581 
   4582       /* If this dynamic lib was specified on the command line with
   4583 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   4584 	 tag unless the lib is actually used.  Similary for libs brought
   4585 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   4586 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   4587 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   4588 	 all.  */
   4589       add_needed = (elf_dyn_lib_class (abfd)
   4590 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   4591 		       | DYN_NO_NEEDED)) == 0;
   4592 
   4593       s = bfd_get_section_by_name (abfd, ".dynamic");
   4594       if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
   4595 	{
   4596 	  bfd_byte *dynbuf;
   4597 	  bfd_byte *extdyn;
   4598 	  unsigned int elfsec;
   4599 	  unsigned long shlink;
   4600 
   4601 	  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   4602 	    {
   4603 	    error_free_dyn:
   4604 	      _bfd_elf_munmap_section_contents (s, dynbuf);
   4605 	      goto error_return;
   4606 	    }
   4607 
   4608 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   4609 	  if (elfsec == SHN_BAD)
   4610 	    goto error_free_dyn;
   4611 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   4612 
   4613 	  for (extdyn = dynbuf;
   4614 	       (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
   4615 	       extdyn += bed->s->sizeof_dyn)
   4616 	    {
   4617 	      Elf_Internal_Dyn dyn;
   4618 
   4619 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   4620 	      if (dyn.d_tag == DT_SONAME)
   4621 		{
   4622 		  unsigned int tagv = dyn.d_un.d_val;
   4623 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4624 		  if (soname == NULL)
   4625 		    goto error_free_dyn;
   4626 		}
   4627 	      if (dyn.d_tag == DT_NEEDED)
   4628 		{
   4629 		  struct bfd_link_needed_list *n, **pn;
   4630 		  char *fnm, *anm;
   4631 		  unsigned int tagv = dyn.d_un.d_val;
   4632 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4633 
   4634 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4635 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4636 		  if (n == NULL || fnm == NULL)
   4637 		    goto error_free_dyn;
   4638 		  amt = strlen (fnm) + 1;
   4639 		  anm = (char *) bfd_alloc (abfd, amt);
   4640 		  if (anm == NULL)
   4641 		    goto error_free_dyn;
   4642 		  memcpy (anm, fnm, amt);
   4643 		  n->name = anm;
   4644 		  n->by = abfd;
   4645 		  n->next = NULL;
   4646 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   4647 		    ;
   4648 		  *pn = n;
   4649 		}
   4650 	      if (dyn.d_tag == DT_RUNPATH)
   4651 		{
   4652 		  struct bfd_link_needed_list *n, **pn;
   4653 		  char *fnm, *anm;
   4654 		  unsigned int tagv = dyn.d_un.d_val;
   4655 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4656 
   4657 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4658 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4659 		  if (n == NULL || fnm == NULL)
   4660 		    goto error_free_dyn;
   4661 		  amt = strlen (fnm) + 1;
   4662 		  anm = (char *) bfd_alloc (abfd, amt);
   4663 		  if (anm == NULL)
   4664 		    goto error_free_dyn;
   4665 		  memcpy (anm, fnm, amt);
   4666 		  n->name = anm;
   4667 		  n->by = abfd;
   4668 		  n->next = NULL;
   4669 		  for (pn = & runpath;
   4670 		       *pn != NULL;
   4671 		       pn = &(*pn)->next)
   4672 		    ;
   4673 		  *pn = n;
   4674 		}
   4675 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   4676 	      if (!runpath && dyn.d_tag == DT_RPATH)
   4677 		{
   4678 		  struct bfd_link_needed_list *n, **pn;
   4679 		  char *fnm, *anm;
   4680 		  unsigned int tagv = dyn.d_un.d_val;
   4681 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4682 
   4683 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4684 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4685 		  if (n == NULL || fnm == NULL)
   4686 		    goto error_free_dyn;
   4687 		  amt = strlen (fnm) + 1;
   4688 		  anm = (char *) bfd_alloc (abfd, amt);
   4689 		  if (anm == NULL)
   4690 		    goto error_free_dyn;
   4691 		  memcpy (anm, fnm, amt);
   4692 		  n->name = anm;
   4693 		  n->by = abfd;
   4694 		  n->next = NULL;
   4695 		  for (pn = & rpath;
   4696 		       *pn != NULL;
   4697 		       pn = &(*pn)->next)
   4698 		    ;
   4699 		  *pn = n;
   4700 		}
   4701 	      if (dyn.d_tag == DT_AUDIT)
   4702 		{
   4703 		  unsigned int tagv = dyn.d_un.d_val;
   4704 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4705 		}
   4706 	      if (dyn.d_tag == DT_FLAGS_1)
   4707 		elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
   4708 	    }
   4709 
   4710 	  _bfd_elf_munmap_section_contents (s, dynbuf);
   4711 	}
   4712 
   4713       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   4714 	 frees all more recently bfd_alloc'd blocks as well.  */
   4715       if (runpath)
   4716 	rpath = runpath;
   4717 
   4718       if (rpath)
   4719 	{
   4720 	  struct bfd_link_needed_list **pn;
   4721 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   4722 	    ;
   4723 	  *pn = rpath;
   4724 	}
   4725 
   4726       /* If we have a PT_GNU_RELRO program header, mark as read-only
   4727 	 all sections contained fully therein.  This makes relro
   4728 	 shared library sections appear as they will at run-time.  */
   4729       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
   4730       while (phdr-- > elf_tdata (abfd)->phdr)
   4731 	if (phdr->p_type == PT_GNU_RELRO)
   4732 	  {
   4733 	    for (s = abfd->sections; s != NULL; s = s->next)
   4734 	      {
   4735 		unsigned int opb = bfd_octets_per_byte (abfd, s);
   4736 
   4737 		if ((s->flags & SEC_ALLOC) != 0
   4738 		    && s->vma * opb >= phdr->p_vaddr
   4739 		    && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
   4740 		  s->flags |= SEC_READONLY;
   4741 	      }
   4742 	    break;
   4743 	  }
   4744 
   4745       /* We do not want to include any of the sections in a dynamic
   4746 	 object in the output file.  We hack by simply clobbering the
   4747 	 list of sections in the BFD.  This could be handled more
   4748 	 cleanly by, say, a new section flag; the existing
   4749 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   4750 	 still implies that the section takes up space in the output
   4751 	 file.  */
   4752       bfd_section_list_clear (abfd);
   4753 
   4754       /* Find the name to use in a DT_NEEDED entry that refers to this
   4755 	 object.  If the object has a DT_SONAME entry, we use it.
   4756 	 Otherwise, if the generic linker stuck something in
   4757 	 elf_dt_name, we use that.  Otherwise, we just use the file
   4758 	 name.  */
   4759       if (soname == NULL || *soname == '\0')
   4760 	{
   4761 	  soname = elf_dt_name (abfd);
   4762 	  if (soname == NULL || *soname == '\0')
   4763 	    soname = bfd_get_filename (abfd);
   4764 	}
   4765 
   4766       /* Save the SONAME because sometimes the linker emulation code
   4767 	 will need to know it.  */
   4768       elf_dt_name (abfd) = soname;
   4769 
   4770       /* If we have already included this dynamic object in the
   4771 	 link, just ignore it.  There is no reason to include a
   4772 	 particular dynamic object more than once.  */
   4773       for (loaded_lib = htab->dyn_loaded;
   4774 	   loaded_lib != NULL;
   4775 	   loaded_lib = loaded_lib->next)
   4776 	{
   4777 	  if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
   4778 	    return true;
   4779 	}
   4780 
   4781       /* Create dynamic sections for backends that require that be done
   4782 	 before setup_gnu_properties.  */
   4783       if (add_needed
   4784 	  && !_bfd_elf_link_create_dynamic_sections (abfd, info))
   4785 	return false;
   4786 
   4787       /* Save the DT_AUDIT entry for the linker emulation code. */
   4788       elf_dt_audit (abfd) = audit;
   4789     }
   4790 
   4791   /* If this is a dynamic object, we always link against the .dynsym
   4792      symbol table, not the .symtab symbol table.  The dynamic linker
   4793      will only see the .dynsym symbol table, so there is no reason to
   4794      look at .symtab for a dynamic object.  */
   4795 
   4796   if (! dynamic || elf_dynsymtab (abfd) == 0)
   4797     hdr = &elf_tdata (abfd)->symtab_hdr;
   4798   else
   4799     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   4800 
   4801   symcount = hdr->sh_size / bed->s->sizeof_sym;
   4802 
   4803   /* The sh_info field of the symtab header tells us where the
   4804      external symbols start.  We don't care about the local symbols at
   4805      this point.  */
   4806   if (elf_bad_symtab (abfd))
   4807     {
   4808       extsymcount = symcount;
   4809       extsymoff = 0;
   4810     }
   4811   else
   4812     {
   4813       extsymcount = symcount - hdr->sh_info;
   4814       extsymoff = hdr->sh_info;
   4815     }
   4816 
   4817   sym_hash = elf_sym_hashes (abfd);
   4818   if (extsymcount != 0)
   4819     {
   4820       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   4821 				      NULL, NULL, NULL);
   4822       if (isymbuf == NULL)
   4823 	goto error_return;
   4824 
   4825       if (sym_hash == NULL)
   4826 	{
   4827 	  /* We store a pointer to the hash table entry for each
   4828 	     external symbol.  */
   4829 	  size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   4830 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   4831 	  if (sym_hash == NULL)
   4832 	    goto error_free_sym;
   4833 	  elf_sym_hashes (abfd) = sym_hash;
   4834 	}
   4835     }
   4836 
   4837   if (dynamic)
   4838     {
   4839       /* Read in any version definitions.  */
   4840       if (!_bfd_elf_slurp_version_tables (abfd,
   4841 					  info->default_imported_symver))
   4842 	goto error_free_sym;
   4843 
   4844       /* Read in the symbol versions, but don't bother to convert them
   4845 	 to internal format.  */
   4846       if (elf_dynversym (abfd) != 0)
   4847 	{
   4848 	  Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   4849 	  bfd_size_type amt = versymhdr->sh_size;
   4850 
   4851 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
   4852 	    goto error_free_sym;
   4853 	  extversym = (Elf_External_Versym *)
   4854 	    _bfd_malloc_and_read (abfd, amt, amt);
   4855 	  if (extversym == NULL)
   4856 	    goto error_free_sym;
   4857 	  extversym_end = extversym + amt / sizeof (*extversym);
   4858 	}
   4859     }
   4860 
   4861   /* If we are loading an as-needed shared lib, save the symbol table
   4862      state before we start adding symbols.  If the lib turns out
   4863      to be unneeded, restore the state.  */
   4864   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4865     {
   4866       unsigned int i;
   4867       size_t entsize;
   4868 
   4869       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   4870 	{
   4871 	  struct bfd_hash_entry *p;
   4872 	  struct elf_link_hash_entry *h;
   4873 
   4874 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4875 	    {
   4876 	      h = (struct elf_link_hash_entry *) p;
   4877 	      entsize += htab->root.table.entsize;
   4878 	      if (h->root.type == bfd_link_hash_warning)
   4879 		{
   4880 		  entsize += htab->root.table.entsize;
   4881 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4882 		}
   4883 	      if (h->root.type == bfd_link_hash_common)
   4884 		entsize += sizeof (*h->root.u.c.p);
   4885 	    }
   4886 	}
   4887 
   4888       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   4889       old_tab = bfd_malloc (tabsize + entsize);
   4890       if (old_tab == NULL)
   4891 	goto error_free_vers;
   4892 
   4893       /* Remember the current objalloc pointer, so that all mem for
   4894 	 symbols added can later be reclaimed.  */
   4895       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   4896       if (alloc_mark == NULL)
   4897 	goto error_free_vers;
   4898 
   4899       /* Make a special call to the linker "notice" function to
   4900 	 tell it that we are about to handle an as-needed lib.  */
   4901       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   4902 	goto error_free_vers;
   4903 
   4904       /* Clone the symbol table.  Remember some pointers into the
   4905 	 symbol table, and dynamic symbol count.  */
   4906       old_ent = (char *) old_tab + tabsize;
   4907       memcpy (old_tab, htab->root.table.table, tabsize);
   4908       old_undefs = htab->root.undefs;
   4909       old_undefs_tail = htab->root.undefs_tail;
   4910       old_table = htab->root.table.table;
   4911       old_size = htab->root.table.size;
   4912       old_count = htab->root.table.count;
   4913       old_strtab = NULL;
   4914       if (htab->dynstr != NULL)
   4915 	{
   4916 	  old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   4917 	  if (old_strtab == NULL)
   4918 	    goto error_free_vers;
   4919 	}
   4920 
   4921       for (i = 0; i < htab->root.table.size; i++)
   4922 	{
   4923 	  struct bfd_hash_entry *p;
   4924 	  struct elf_link_hash_entry *h;
   4925 
   4926 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4927 	    {
   4928 	      h = (struct elf_link_hash_entry *) p;
   4929 	      memcpy (old_ent, h, htab->root.table.entsize);
   4930 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4931 	      if (h->root.type == bfd_link_hash_warning)
   4932 		{
   4933 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4934 		  memcpy (old_ent, h, htab->root.table.entsize);
   4935 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4936 		}
   4937 	      if (h->root.type == bfd_link_hash_common)
   4938 		{
   4939 		  memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
   4940 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   4941 		}
   4942 	    }
   4943 	}
   4944     }
   4945 
   4946   weaks = NULL;
   4947   if (extversym == NULL)
   4948     ever = NULL;
   4949   else if (extversym + extsymoff < extversym_end)
   4950     ever = extversym + extsymoff;
   4951   else
   4952     {
   4953       /* xgettext:c-format */
   4954       _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
   4955 			  abfd, (long) extsymoff,
   4956 			  (long) (extversym_end - extversym) / sizeof (* extversym));
   4957       bfd_set_error (bfd_error_bad_value);
   4958       goto error_free_vers;
   4959     }
   4960 
   4961   if (!bfd_link_relocatable (info)
   4962       && bfd_get_lto_type (abfd) == lto_slim_ir_object)
   4963     {
   4964       _bfd_error_handler
   4965 	(_("%pB: plugin needed to handle lto object"), abfd);
   4966     }
   4967 
   4968   for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
   4969        isym < isymend;
   4970        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   4971     {
   4972       int bind;
   4973       bfd_vma value;
   4974       asection *sec, *new_sec;
   4975       flagword flags;
   4976       const char *name;
   4977       const char *defvername;
   4978       bool must_copy_name = false;
   4979       struct elf_link_hash_entry *h;
   4980       struct elf_link_hash_entry *hi;
   4981       bool definition;
   4982       bool size_change_ok;
   4983       bool type_change_ok;
   4984       bool new_weak;
   4985       bool old_weak;
   4986       bfd *override;
   4987       bool common;
   4988       bool discarded;
   4989       unsigned int old_alignment;
   4990       unsigned int shindex;
   4991       bfd *old_bfd;
   4992       bool matched;
   4993 
   4994       override = NULL;
   4995 
   4996       flags = BSF_NO_FLAGS;
   4997       sec = NULL;
   4998       value = isym->st_value;
   4999       common = bed->common_definition (isym);
   5000       if (common && info->inhibit_common_definition)
   5001 	{
   5002 	  /* Treat common symbol as undefined for --no-define-common.  */
   5003 	  isym->st_shndx = SHN_UNDEF;
   5004 	  common = false;
   5005 	}
   5006       discarded = false;
   5007 
   5008       bind = ELF_ST_BIND (isym->st_info);
   5009       switch (bind)
   5010 	{
   5011 	case STB_LOCAL:
   5012 	  /* This should be impossible, since ELF requires that all
   5013 	     global symbols follow all local symbols, and that sh_info
   5014 	     point to the first global symbol.  Unfortunately, Irix 5
   5015 	     screws this up.  */
   5016 	  if (elf_bad_symtab (abfd))
   5017 	    continue;
   5018 
   5019 	  /* If we aren't prepared to handle locals within the globals
   5020 	     then we'll likely segfault on a NULL symbol hash if the
   5021 	     symbol is ever referenced in relocations.  */
   5022 	  shindex = elf_elfheader (abfd)->e_shstrndx;
   5023 	  name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
   5024 	  _bfd_error_handler (_("%pB: %s local symbol at index %lu"
   5025 				" (>= sh_info of %lu)"),
   5026 			      abfd, name, (long) (isym - isymbuf + extsymoff),
   5027 			      (long) extsymoff);
   5028 
   5029 	  /* Dynamic object relocations are not processed by ld, so
   5030 	     ld won't run into the problem mentioned above.  */
   5031 	  if (dynamic)
   5032 	    continue;
   5033 	  bfd_set_error (bfd_error_bad_value);
   5034 	  goto error_free_vers;
   5035 
   5036 	case STB_GLOBAL:
   5037 	  if (isym->st_shndx != SHN_UNDEF && !common)
   5038 	    flags = BSF_GLOBAL;
   5039 	  break;
   5040 
   5041 	case STB_WEAK:
   5042 	  flags = BSF_WEAK;
   5043 	  break;
   5044 
   5045 	case STB_GNU_UNIQUE:
   5046 	  flags = BSF_GNU_UNIQUE;
   5047 	  break;
   5048 
   5049 	default:
   5050 	  /* Leave it up to the processor backend.  */
   5051 	  break;
   5052 	}
   5053 
   5054       if (isym->st_shndx == SHN_UNDEF)
   5055 	sec = bfd_und_section_ptr;
   5056       else if (isym->st_shndx == SHN_ABS)
   5057 	sec = bfd_abs_section_ptr;
   5058       else if (isym->st_shndx == SHN_COMMON)
   5059 	{
   5060 	  sec = bfd_com_section_ptr;
   5061 	  /* What ELF calls the size we call the value.  What ELF
   5062 	     calls the value we call the alignment.  */
   5063 	  value = isym->st_size;
   5064 	}
   5065       else
   5066 	{
   5067 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5068 	  if (sec == NULL)
   5069 	    sec = bfd_abs_section_ptr;
   5070 	  else if (discarded_section (sec))
   5071 	    {
   5072 	      /* Symbols from discarded section are undefined.  We keep
   5073 		 its visibility.  */
   5074 	      sec = bfd_und_section_ptr;
   5075 	      discarded = true;
   5076 	      isym->st_shndx = SHN_UNDEF;
   5077 	    }
   5078 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   5079 	    value -= sec->vma;
   5080 	}
   5081 
   5082       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   5083 					      isym->st_name);
   5084       if (name == NULL)
   5085 	goto error_free_vers;
   5086 
   5087       if (isym->st_shndx == SHN_COMMON
   5088 	  && (abfd->flags & BFD_PLUGIN) != 0)
   5089 	{
   5090 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   5091 
   5092 	  if (xc == NULL)
   5093 	    {
   5094 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   5095 				 | SEC_EXCLUDE);
   5096 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   5097 	      if (xc == NULL)
   5098 		goto error_free_vers;
   5099 	    }
   5100 	  sec = xc;
   5101 	}
   5102       else if (isym->st_shndx == SHN_COMMON
   5103 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   5104 	       && !bfd_link_relocatable (info))
   5105 	{
   5106 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   5107 
   5108 	  if (tcomm == NULL)
   5109 	    {
   5110 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   5111 				 | SEC_LINKER_CREATED);
   5112 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   5113 	      if (tcomm == NULL)
   5114 		goto error_free_vers;
   5115 	    }
   5116 	  sec = tcomm;
   5117 	}
   5118       else if (bed->elf_add_symbol_hook)
   5119 	{
   5120 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   5121 					     &sec, &value))
   5122 	    goto error_free_vers;
   5123 
   5124 	  /* The hook function sets the name to NULL if this symbol
   5125 	     should be skipped for some reason.  */
   5126 	  if (name == NULL)
   5127 	    continue;
   5128 	}
   5129 
   5130       /* Sanity check that all possibilities were handled.  */
   5131       if (sec == NULL)
   5132 	abort ();
   5133 
   5134       /* Silently discard TLS symbols from --just-syms.  There's
   5135 	 no way to combine a static TLS block with a new TLS block
   5136 	 for this executable.  */
   5137       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   5138 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   5139 	continue;
   5140 
   5141       if (bfd_is_und_section (sec)
   5142 	  || bfd_is_com_section (sec))
   5143 	definition = false;
   5144       else
   5145 	definition = true;
   5146 
   5147       size_change_ok = false;
   5148       type_change_ok = bed->type_change_ok;
   5149       old_weak = false;
   5150       matched = false;
   5151       old_alignment = 0;
   5152       old_bfd = NULL;
   5153       new_sec = sec;
   5154       defvername = NULL;
   5155 
   5156       if (is_elf_hash_table (&htab->root))
   5157 	{
   5158 	  Elf_Internal_Versym iver;
   5159 	  unsigned int vernum = 0;
   5160 	  bool skip;
   5161 
   5162 	  if (ever == NULL)
   5163 	    {
   5164 	      if (info->default_imported_symver)
   5165 		/* Use the default symbol version created earlier.  */
   5166 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   5167 	      else
   5168 		iver.vs_vers = 0;
   5169 	    }
   5170 	  else if (ever >= extversym_end)
   5171 	    {
   5172 	      /* xgettext:c-format */
   5173 	      _bfd_error_handler (_("%pB: not enough version information"),
   5174 				  abfd);
   5175 	      bfd_set_error (bfd_error_bad_value);
   5176 	      goto error_free_vers;
   5177 	    }
   5178 	  else
   5179 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   5180 
   5181 	  vernum = iver.vs_vers & VERSYM_VERSION;
   5182 
   5183 	  /* If this is a hidden symbol, or if it is not version
   5184 	     1, we append the version name to the symbol name.
   5185 	     However, we do not modify a non-hidden absolute symbol
   5186 	     if it is not a function, because it might be the version
   5187 	     symbol itself.  FIXME: What if it isn't?  */
   5188 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   5189 	      || (vernum > 1
   5190 		  && (!bfd_is_abs_section (sec)
   5191 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   5192 	    {
   5193 	      const char *verstr;
   5194 	      size_t namelen, verlen, newlen;
   5195 	      char *newname, *p;
   5196 
   5197 	      if (isym->st_shndx != SHN_UNDEF)
   5198 		{
   5199 		  if (vernum > elf_tdata (abfd)->cverdefs)
   5200 		    verstr = NULL;
   5201 		  else if (vernum > 1)
   5202 		    verstr =
   5203 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   5204 		  else
   5205 		    verstr = "";
   5206 
   5207 		  if (verstr == NULL)
   5208 		    {
   5209 		      _bfd_error_handler
   5210 			/* xgettext:c-format */
   5211 			(_("%pB: %s: invalid version %u (max %d)"),
   5212 			 abfd, name, vernum,
   5213 			 elf_tdata (abfd)->cverdefs);
   5214 		      bfd_set_error (bfd_error_bad_value);
   5215 		      goto error_free_vers;
   5216 		    }
   5217 		}
   5218 	      else
   5219 		{
   5220 		  /* We cannot simply test for the number of
   5221 		     entries in the VERNEED section since the
   5222 		     numbers for the needed versions do not start
   5223 		     at 0.  */
   5224 		  Elf_Internal_Verneed *t;
   5225 
   5226 		  verstr = NULL;
   5227 		  for (t = elf_tdata (abfd)->verref;
   5228 		       t != NULL;
   5229 		       t = t->vn_nextref)
   5230 		    {
   5231 		      Elf_Internal_Vernaux *a;
   5232 
   5233 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   5234 			{
   5235 			  if (a->vna_other == vernum)
   5236 			    {
   5237 			      verstr = a->vna_nodename;
   5238 			      break;
   5239 			    }
   5240 			}
   5241 		      if (a != NULL)
   5242 			break;
   5243 		    }
   5244 		  if (verstr == NULL)
   5245 		    {
   5246 		      _bfd_error_handler
   5247 			/* xgettext:c-format */
   5248 			(_("%pB: %s: invalid needed version %d"),
   5249 			 abfd, name, vernum);
   5250 		      bfd_set_error (bfd_error_bad_value);
   5251 		      goto error_free_vers;
   5252 		    }
   5253 		}
   5254 
   5255 	      namelen = strlen (name);
   5256 	      verlen = strlen (verstr);
   5257 	      newlen = namelen + verlen + 2;
   5258 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5259 		  && isym->st_shndx != SHN_UNDEF)
   5260 		++newlen;
   5261 
   5262 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   5263 	      if (newname == NULL)
   5264 		goto error_free_vers;
   5265 	      memcpy (newname, name, namelen);
   5266 	      p = newname + namelen;
   5267 	      *p++ = ELF_VER_CHR;
   5268 	      /* If this is a defined non-hidden version symbol,
   5269 		 we add another @ to the name.  This indicates the
   5270 		 default version of the symbol.  */
   5271 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5272 		  && isym->st_shndx != SHN_UNDEF)
   5273 		*p++ = ELF_VER_CHR, defvername = name;
   5274 	      memcpy (p, verstr, verlen + 1);
   5275 
   5276 	      name = newname;
   5277 	      /* Since bfd_hash_alloc is used for "name", the string
   5278 		 must be copied if added to first_hash.  The string
   5279 		 memory can be freed when an --as-needed library is
   5280 		 not needed.  */
   5281 	      must_copy_name = true;
   5282 	    }
   5283 
   5284 	  /* If this symbol has default visibility and the user has
   5285 	     requested we not re-export it, then mark it as hidden.  */
   5286 	  if (!bfd_is_und_section (sec)
   5287 	      && !dynamic
   5288 	      && abfd->no_export
   5289 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   5290 	    isym->st_other = (STV_HIDDEN
   5291 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   5292 
   5293 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   5294 				      sym_hash, &old_bfd, &old_weak,
   5295 				      &old_alignment, &skip, &override,
   5296 				      &type_change_ok, &size_change_ok,
   5297 				      &matched))
   5298 	    goto error_free_vers;
   5299 
   5300 	  if (skip)
   5301 	    continue;
   5302 
   5303 	  h = *sym_hash;
   5304 	  while (h->root.type == bfd_link_hash_indirect
   5305 		 || h->root.type == bfd_link_hash_warning)
   5306 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5307 
   5308 	  /* Override a definition only if the new symbol matches the
   5309 	     existing one.  */
   5310 	  if (override && matched)
   5311 	    {
   5312 	      definition = false;
   5313 	      if (htab->first_hash != NULL
   5314 		  && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5315 		  && h->root.non_ir_ref_regular)
   5316 		{
   5317 		  /* When reloading --as-needed shared objects for new
   5318 		     symbols added from IR inputs, if this shared object
   5319 		     has the first definition, use it.  */
   5320 		  struct elf_link_first_hash_entry *e
   5321 		    = ((struct elf_link_first_hash_entry *)
   5322 		       bfd_hash_lookup (htab->first_hash, name, false,
   5323 					false));
   5324 		  if (e != NULL && e->abfd == abfd)
   5325 		    definition = true;
   5326 		}
   5327 	    }
   5328 
   5329 	  if (h->versioned != unversioned
   5330 	      && elf_tdata (abfd)->verdef != NULL
   5331 	      && vernum > 1
   5332 	      && definition)
   5333 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   5334 	}
   5335 
   5336       if (! (_bfd_generic_link_add_one_symbol
   5337 	     (info, override ? override : abfd, name, flags, sec, value,
   5338 	      NULL, false, bed->collect,
   5339 	      (struct bfd_link_hash_entry **) sym_hash)))
   5340 	goto error_free_vers;
   5341 
   5342       h = *sym_hash;
   5343       /* We need to make sure that indirect symbol dynamic flags are
   5344 	 updated.  */
   5345       hi = h;
   5346       while (h->root.type == bfd_link_hash_indirect
   5347 	     || h->root.type == bfd_link_hash_warning)
   5348 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5349 
   5350       *sym_hash = h;
   5351 
   5352       /* Setting the index to -3 tells elf_link_output_extsym that
   5353 	 this symbol is defined in a discarded section.  */
   5354       if (discarded && is_elf_hash_table (&htab->root))
   5355 	h->indx = -3;
   5356 
   5357       new_weak = (flags & BSF_WEAK) != 0;
   5358       if (dynamic
   5359 	  && definition
   5360 	  && new_weak
   5361 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   5362 	  && is_elf_hash_table (&htab->root)
   5363 	  && h->u.alias == NULL)
   5364 	{
   5365 	  /* Keep a list of all weak defined non function symbols from
   5366 	     a dynamic object, using the alias field.  Later in this
   5367 	     function we will set the alias field to the correct
   5368 	     value.  We only put non-function symbols from dynamic
   5369 	     objects on this list, because that happens to be the only
   5370 	     time we need to know the normal symbol corresponding to a
   5371 	     weak symbol, and the information is time consuming to
   5372 	     figure out.  If the alias field is not already NULL,
   5373 	     then this symbol was already defined by some previous
   5374 	     dynamic object, and we will be using that previous
   5375 	     definition anyhow.  */
   5376 
   5377 	  h->u.alias = weaks;
   5378 	  weaks = h;
   5379 	}
   5380 
   5381       /* Set the alignment of a common symbol.  */
   5382       if ((common || bfd_is_com_section (sec))
   5383 	  && h->root.type == bfd_link_hash_common)
   5384 	{
   5385 	  unsigned int align;
   5386 
   5387 	  if (common)
   5388 	    align = bfd_log2 (isym->st_value);
   5389 	  else
   5390 	    {
   5391 	      /* The new symbol is a common symbol in a shared object.
   5392 		 We need to get the alignment from the section.  */
   5393 	      align = new_sec->alignment_power;
   5394 	    }
   5395 	  if (align > old_alignment)
   5396 	    h->root.u.c.p->alignment_power = align;
   5397 	  else
   5398 	    h->root.u.c.p->alignment_power = old_alignment;
   5399 	}
   5400 
   5401       if (is_elf_hash_table (&htab->root))
   5402 	{
   5403 	  /* Set a flag in the hash table entry indicating the type of
   5404 	     reference or definition we just found.  A dynamic symbol
   5405 	     is one which is referenced or defined by both a regular
   5406 	     object and a shared object.  */
   5407 	  bool dynsym = false;
   5408 
   5409 	  /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
   5410 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5411 	    {
   5412 	      /* Except for this flag to track nonweak references.  */
   5413 	      if (!definition
   5414 		  && bind != STB_WEAK)
   5415 		h->ref_ir_nonweak = 1;
   5416 	    }
   5417 	  else if (!dynamic)
   5418 	    {
   5419 	      if (! definition)
   5420 		{
   5421 		  h->ref_regular = 1;
   5422 		  if (bind != STB_WEAK)
   5423 		    h->ref_regular_nonweak = 1;
   5424 		}
   5425 	      else
   5426 		{
   5427 		  h->def_regular = 1;
   5428 		  if (h->def_dynamic)
   5429 		    {
   5430 		      h->def_dynamic = 0;
   5431 		      h->ref_dynamic = 1;
   5432 		    }
   5433 		}
   5434 	    }
   5435 	  else
   5436 	    {
   5437 	      if (! definition)
   5438 		{
   5439 		  h->ref_dynamic = 1;
   5440 		  hi->ref_dynamic = 1;
   5441 		}
   5442 	      else
   5443 		{
   5444 		  h->def_dynamic = 1;
   5445 		  hi->def_dynamic = 1;
   5446 		}
   5447 	    }
   5448 
   5449 	  /* If an indirect symbol has been forced local, don't
   5450 	     make the real symbol dynamic.  */
   5451 	  if (h != hi && hi->forced_local)
   5452 	    ;
   5453 	  else if (!dynamic)
   5454 	    {
   5455 	      if (bfd_link_dll (info)
   5456 		  || h->def_dynamic
   5457 		  || h->ref_dynamic)
   5458 		dynsym = true;
   5459 	    }
   5460 	  else
   5461 	    {
   5462 	      if (h->def_regular
   5463 		  || h->ref_regular
   5464 		  || (h->is_weakalias
   5465 		      && weakdef (h)->dynindx != -1))
   5466 		dynsym = true;
   5467 	    }
   5468 
   5469 	  /* Check to see if we need to add an indirect symbol for
   5470 	     the default name.  */
   5471 	  if ((definition
   5472 	       || (!override && h->root.type == bfd_link_hash_common))
   5473 	      && !(hi != h
   5474 		   && hi->versioned == versioned_hidden))
   5475 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   5476 					      sec, value, &old_bfd, &dynsym))
   5477 	      goto error_free_vers;
   5478 
   5479 	  /* Check the alignment when a common symbol is involved. This
   5480 	     can change when a common symbol is overridden by a normal
   5481 	     definition or a common symbol is ignored due to the old
   5482 	     normal definition. We need to make sure the maximum
   5483 	     alignment is maintained.  */
   5484 	  if ((old_alignment || common)
   5485 	      && h->root.type != bfd_link_hash_common)
   5486 	    {
   5487 	      unsigned int common_align;
   5488 	      unsigned int normal_align;
   5489 	      unsigned int symbol_align;
   5490 	      bfd *normal_bfd;
   5491 	      bfd *common_bfd;
   5492 
   5493 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   5494 			  || h->root.type == bfd_link_hash_defweak);
   5495 
   5496 	      symbol_align = ffs (h->root.u.def.value) - 1;
   5497 	      if (h->root.u.def.section->owner != NULL
   5498 		  && (h->root.u.def.section->owner->flags
   5499 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
   5500 		{
   5501 		  normal_align = h->root.u.def.section->alignment_power;
   5502 		  if (normal_align > symbol_align)
   5503 		    normal_align = symbol_align;
   5504 		}
   5505 	      else
   5506 		normal_align = symbol_align;
   5507 
   5508 	      if (old_alignment)
   5509 		{
   5510 		  common_align = old_alignment;
   5511 		  common_bfd = old_bfd;
   5512 		  normal_bfd = abfd;
   5513 		}
   5514 	      else
   5515 		{
   5516 		  common_align = bfd_log2 (isym->st_value);
   5517 		  common_bfd = abfd;
   5518 		  normal_bfd = old_bfd;
   5519 		}
   5520 
   5521 	      if (normal_align < common_align)
   5522 		{
   5523 		  /* PR binutils/2735 */
   5524 		  if (normal_bfd == NULL)
   5525 		    _bfd_error_handler
   5526 		      /* xgettext:c-format */
   5527 		      (_("warning: alignment %u of common symbol `%s' in %pB is"
   5528 			 " greater than the alignment (%u) of its section %pA"),
   5529 		       1 << common_align, name, common_bfd,
   5530 		       1 << normal_align, h->root.u.def.section);
   5531 		  else
   5532 		    _bfd_error_handler
   5533 		      /* xgettext:c-format */
   5534 		      (_("warning: alignment %u of normal symbol `%s' in %pB"
   5535 			 " is smaller than %u used by the common definition in %pB"),
   5536 		       1 << normal_align, name, normal_bfd,
   5537 		       1 << common_align, common_bfd);
   5538 
   5539 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5540 		  _bfd_error_handler
   5541 		    (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
   5542 		}
   5543 	    }
   5544 
   5545 	  /* Remember the symbol size if it isn't undefined.  */
   5546 	  if (isym->st_size != 0
   5547 	      && isym->st_shndx != SHN_UNDEF
   5548 	      && (definition || h->size == 0))
   5549 	    {
   5550 	      if (h->size != 0
   5551 		  && h->size != isym->st_size
   5552 		  && ! size_change_ok)
   5553 		{
   5554 		  _bfd_error_handler
   5555 		    /* xgettext:c-format */
   5556 		    (_("warning: size of symbol `%s' changed"
   5557 		       " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
   5558 		     name, (uint64_t) h->size, old_bfd,
   5559 		     (uint64_t) isym->st_size, abfd);
   5560 
   5561 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5562 		  _bfd_error_handler
   5563 		    (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
   5564 		}
   5565 
   5566 	      h->size = isym->st_size;
   5567 	    }
   5568 
   5569 	  /* If this is a common symbol, then we always want H->SIZE
   5570 	     to be the size of the common symbol.  The code just above
   5571 	     won't fix the size if a common symbol becomes larger.  We
   5572 	     don't warn about a size change here, because that is
   5573 	     covered by --warn-common.  Allow changes between different
   5574 	     function types.  */
   5575 	  if (h->root.type == bfd_link_hash_common)
   5576 	    h->size = h->root.u.c.size;
   5577 
   5578 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   5579 	      && ((definition && !new_weak)
   5580 		  || (old_weak && h->root.type == bfd_link_hash_common)
   5581 		  || h->type == STT_NOTYPE))
   5582 	    {
   5583 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   5584 
   5585 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   5586 		 symbol.  */
   5587 	      if (type == STT_GNU_IFUNC
   5588 		  && (abfd->flags & DYNAMIC) != 0)
   5589 		type = STT_FUNC;
   5590 
   5591 	      if (h->type != type)
   5592 		{
   5593 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   5594 		    /* xgettext:c-format */
   5595 		    _bfd_error_handler
   5596 		      (_("warning: type of symbol `%s' changed"
   5597 			 " from %d to %d in %pB"),
   5598 		       name, h->type, type, abfd);
   5599 
   5600 		  h->type = type;
   5601 		}
   5602 	    }
   5603 
   5604 	  /* Merge st_other field.  */
   5605 	  elf_merge_st_other (abfd, h, isym->st_other, sec,
   5606 			      definition, dynamic);
   5607 
   5608 	  /* We don't want to make debug symbol dynamic.  */
   5609 	  if (definition
   5610 	      && (sec->flags & SEC_DEBUGGING)
   5611 	      && !bfd_link_relocatable (info))
   5612 	    dynsym = false;
   5613 
   5614 	  /* Nor should we make plugin symbols dynamic.  */
   5615 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5616 	    dynsym = false;
   5617 
   5618 	  if (definition)
   5619 	    {
   5620 	      h->target_internal = isym->st_target_internal;
   5621 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   5622 	    }
   5623 
   5624 	  /* Don't add indirect symbols for .symver x, x@FOO aliases
   5625 	     in IR.  Since all data or text symbols in IR have the
   5626 	     same type, value and section, we can't tell if a symbol
   5627 	     is an alias of another symbol by their types, values and
   5628 	     sections.  */
   5629 	  if (definition
   5630 	      && !dynamic
   5631 	      && (abfd->flags & BFD_PLUGIN) == 0)
   5632 	    {
   5633 	      char *p = strchr (name, ELF_VER_CHR);
   5634 	      if (p != NULL && p[1] != ELF_VER_CHR)
   5635 		{
   5636 		  /* Queue non-default versions so that .symver x, x@FOO
   5637 		     aliases can be checked.  */
   5638 		  if (!nondeflt_vers)
   5639 		    {
   5640 		      size_t amt = ((isymend - isym + 1)
   5641 				    * sizeof (struct elf_link_hash_entry *));
   5642 		      nondeflt_vers
   5643 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   5644 		      if (!nondeflt_vers)
   5645 			goto error_free_vers;
   5646 		    }
   5647 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   5648 		}
   5649 	    }
   5650 
   5651 	  if (dynsym && h->dynindx == -1)
   5652 	    {
   5653 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5654 		goto error_free_vers;
   5655 	      if (h->is_weakalias
   5656 		  && weakdef (h)->dynindx == -1)
   5657 		{
   5658 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
   5659 		    goto error_free_vers;
   5660 		}
   5661 	    }
   5662 	  else if (h->dynindx != -1)
   5663 	    /* If the symbol already has a dynamic index, but
   5664 	       visibility says it should not be visible, turn it into
   5665 	       a local symbol.  */
   5666 	    switch (ELF_ST_VISIBILITY (h->other))
   5667 	      {
   5668 	      case STV_INTERNAL:
   5669 	      case STV_HIDDEN:
   5670 		(*bed->elf_backend_hide_symbol) (info, h, true);
   5671 		dynsym = false;
   5672 		break;
   5673 	      }
   5674 
   5675 	  if (!add_needed
   5676 	      && matched
   5677 	      && definition
   5678 	      && h->root.type != bfd_link_hash_indirect)
   5679 	    {
   5680 	      if ((dynsym
   5681 		   && h->ref_regular_nonweak)
   5682 		  || (old_bfd != NULL
   5683 		      && (old_bfd->flags & BFD_PLUGIN) != 0
   5684 		      && h->ref_ir_nonweak
   5685 		      && !info->lto_all_symbols_read)
   5686 		  || (h->ref_dynamic_nonweak
   5687 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5688 		      && !on_needed_list (elf_dt_name (abfd),
   5689 					  htab->needed, NULL)))
   5690 		{
   5691 		  const char *soname = elf_dt_name (abfd);
   5692 
   5693 		  info->callbacks->minfo ("%!", soname, old_bfd,
   5694 					  h->root.root.string);
   5695 
   5696 		  /* A symbol from a library loaded via DT_NEEDED of some
   5697 		     other library is referenced by a regular object.
   5698 		     Add a DT_NEEDED entry for it.  Issue an error if
   5699 		     --no-add-needed is used and the reference was not
   5700 		     a weak one.  */
   5701 		  if (old_bfd != NULL
   5702 		      && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   5703 		    {
   5704 		      _bfd_error_handler
   5705 			/* xgettext:c-format */
   5706 			(_("%pB: undefined reference to symbol '%s'"),
   5707 			 old_bfd, name);
   5708 		      bfd_set_error (bfd_error_missing_dso);
   5709 		      goto error_free_vers;
   5710 		    }
   5711 
   5712 		  elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   5713 		    (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   5714 
   5715 		  /* Create dynamic sections for backends that require
   5716 		     that be done before setup_gnu_properties.  */
   5717 		  if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
   5718 		    goto error_free_vers;
   5719 		  add_needed = true;
   5720 		}
   5721 	      else if (dynamic
   5722 		       && h->root.u.def.section->owner == abfd)
   5723 		{
   5724 		  /* Add this symbol to first hash if this shared
   5725 		     object has the first definition.  */
   5726 		  elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
   5727 		  /* And if it was the default symbol version definition,
   5728 		     also add the short name.  */
   5729 		  if (defvername)
   5730 		    elf_link_add_to_first_hash (abfd, info, defvername, false);
   5731 		}
   5732 	    }
   5733 	}
   5734     }
   5735 
   5736   if (info->lto_plugin_active
   5737       && !bfd_link_relocatable (info)
   5738       && (abfd->flags & BFD_PLUGIN) == 0
   5739       && !just_syms
   5740       && extsymcount != 0
   5741       && is_elf_hash_table (&htab->root))
   5742     {
   5743       int r_sym_shift;
   5744 
   5745       if (bed->s->arch_size == 32)
   5746 	r_sym_shift = 8;
   5747       else
   5748 	r_sym_shift = 32;
   5749 
   5750       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
   5751 	 referenced in regular objects so that linker plugin will get
   5752 	 the correct symbol resolution.  */
   5753 
   5754       sym_hash = elf_sym_hashes (abfd);
   5755       for (s = abfd->sections; s != NULL; s = s->next)
   5756 	{
   5757 	  Elf_Internal_Rela *internal_relocs;
   5758 	  Elf_Internal_Rela *rel, *relend;
   5759 
   5760 	  /* Don't check relocations in excluded sections.  */
   5761 	  if ((s->flags & SEC_RELOC) == 0
   5762 	      || s->reloc_count == 0
   5763 	      || (s->flags & SEC_EXCLUDE) != 0
   5764 	      || (s->flags & SEC_DEBUGGING) != 0)
   5765 	    continue;
   5766 
   5767 	  internal_relocs = _bfd_elf_link_info_read_relocs
   5768 	    (abfd, info, s, NULL, NULL,
   5769 	     _bfd_elf_link_keep_memory (info));
   5770 	  if (internal_relocs == NULL)
   5771 	    goto error_free_vers;
   5772 
   5773 	  rel = internal_relocs;
   5774 	  relend = rel + s->reloc_count;
   5775 	  for ( ; rel < relend; rel++)
   5776 	    {
   5777 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   5778 	      struct elf_link_hash_entry *h;
   5779 
   5780 	      /* Skip local symbols.  */
   5781 	      if (r_symndx < extsymoff)
   5782 		continue;
   5783 
   5784 	      h = sym_hash[r_symndx - extsymoff];
   5785 	      if (h != NULL)
   5786 		h->root.non_ir_ref_regular = 1;
   5787 	    }
   5788 
   5789 	  if (elf_section_data (s)->relocs != internal_relocs)
   5790 	    free (internal_relocs);
   5791 	}
   5792     }
   5793 
   5794   free (extversym);
   5795   extversym = NULL;
   5796   free (isymbuf);
   5797   isymbuf = NULL;
   5798 
   5799   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   5800     {
   5801       unsigned int i;
   5802 
   5803       /* Restore the symbol table.  */
   5804       old_ent = (char *) old_tab + tabsize;
   5805       memset (elf_sym_hashes (abfd), 0,
   5806 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   5807       htab->root.table.table = old_table;
   5808       htab->root.table.size = old_size;
   5809       htab->root.table.count = old_count;
   5810       memcpy (htab->root.table.table, old_tab, tabsize);
   5811       htab->root.undefs = old_undefs;
   5812       htab->root.undefs_tail = old_undefs_tail;
   5813       if (htab->dynstr != NULL)
   5814 	_bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   5815       free (old_strtab);
   5816       old_strtab = NULL;
   5817       for (i = 0; i < htab->root.table.size; i++)
   5818 	{
   5819 	  struct bfd_hash_entry *p;
   5820 	  struct elf_link_hash_entry *h;
   5821 	  unsigned int non_ir_ref_dynamic;
   5822 
   5823 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   5824 	    {
   5825 	      /* Preserve non_ir_ref_dynamic so that this symbol
   5826 		 will be exported when the dynamic lib becomes needed
   5827 		 in the second pass.  */
   5828 	      h = (struct elf_link_hash_entry *) p;
   5829 	      if (h->root.type == bfd_link_hash_warning)
   5830 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5831 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
   5832 
   5833 	      h = (struct elf_link_hash_entry *) p;
   5834 	      memcpy (h, old_ent, htab->root.table.entsize);
   5835 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   5836 	      if (h->root.type == bfd_link_hash_warning)
   5837 		{
   5838 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5839 		  memcpy (h, old_ent, htab->root.table.entsize);
   5840 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   5841 		}
   5842 	      if (h->root.type == bfd_link_hash_common)
   5843 		{
   5844 		  memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
   5845 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   5846 		}
   5847 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
   5848 	    }
   5849 	}
   5850 
   5851       /* Make a special call to the linker "notice" function to
   5852 	 tell it that symbols added for crefs may need to be removed.  */
   5853       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   5854 	goto error_free_vers;
   5855 
   5856       free (old_tab);
   5857       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   5858 			   alloc_mark);
   5859       free (nondeflt_vers);
   5860       return true;
   5861     }
   5862 
   5863   free (old_strtab);
   5864   old_strtab = NULL;
   5865   if (old_tab != NULL)
   5866     {
   5867       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   5868 	goto error_free_vers;
   5869       free (old_tab);
   5870       old_tab = NULL;
   5871     }
   5872 
   5873   /* Now that all the symbols from this input file are created, if
   5874      not performing a relocatable link, handle .symver foo, foo@BAR
   5875      such that any relocs against foo become foo@BAR.  */
   5876   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   5877     {
   5878       size_t cnt, symidx;
   5879 
   5880       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   5881 	{
   5882 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   5883 	  char *shortname, *p;
   5884 	  size_t amt;
   5885 
   5886 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   5887 	  if (p == NULL
   5888 	      || (h->root.type != bfd_link_hash_defined
   5889 		  && h->root.type != bfd_link_hash_defweak))
   5890 	    continue;
   5891 
   5892 	  amt = p - h->root.root.string;
   5893 	  shortname = (char *) bfd_malloc (amt + 1);
   5894 	  if (!shortname)
   5895 	    goto error_free_vers;
   5896 	  memcpy (shortname, h->root.root.string, amt);
   5897 	  shortname[amt] = '\0';
   5898 
   5899 	  hi = (struct elf_link_hash_entry *)
   5900 	       bfd_link_hash_lookup (&htab->root, shortname,
   5901 				     false, false, false);
   5902 	  if (hi != NULL
   5903 	      && hi->root.type == h->root.type
   5904 	      && hi->root.u.def.value == h->root.u.def.value
   5905 	      && hi->root.u.def.section == h->root.u.def.section)
   5906 	    {
   5907 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   5908 	      hi->root.type = bfd_link_hash_indirect;
   5909 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   5910 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   5911 	      sym_hash = elf_sym_hashes (abfd);
   5912 	      if (sym_hash)
   5913 		for (symidx = 0; symidx < extsymcount; ++symidx)
   5914 		  if (sym_hash[symidx] == hi)
   5915 		    {
   5916 		      sym_hash[symidx] = h;
   5917 		      break;
   5918 		    }
   5919 	    }
   5920 	  free (shortname);
   5921 	}
   5922     }
   5923   free (nondeflt_vers);
   5924   nondeflt_vers = NULL;
   5925 
   5926   /* Now set the alias field correctly for all the weak defined
   5927      symbols we found.  The only way to do this is to search all the
   5928      symbols.  Since we only need the information for non functions in
   5929      dynamic objects, that's the only time we actually put anything on
   5930      the list WEAKS.  We need this information so that if a regular
   5931      object refers to a symbol defined weakly in a dynamic object, the
   5932      real symbol in the dynamic object is also put in the dynamic
   5933      symbols; we also must arrange for both symbols to point to the
   5934      same memory location.  We could handle the general case of symbol
   5935      aliasing, but a general symbol alias can only be generated in
   5936      assembler code, handling it correctly would be very time
   5937      consuming, and other ELF linkers don't handle general aliasing
   5938      either.  */
   5939   if (weaks != NULL)
   5940     {
   5941       struct elf_link_hash_entry **hpp;
   5942       struct elf_link_hash_entry **hppend;
   5943       struct elf_link_hash_entry **sorted_sym_hash;
   5944       struct elf_link_hash_entry *h;
   5945       size_t sym_count, amt;
   5946 
   5947       /* Since we have to search the whole symbol list for each weak
   5948 	 defined symbol, search time for N weak defined symbols will be
   5949 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   5950       amt = extsymcount * sizeof (*sorted_sym_hash);
   5951       sorted_sym_hash = bfd_malloc (amt);
   5952       if (sorted_sym_hash == NULL)
   5953 	goto error_return;
   5954       sym_hash = sorted_sym_hash;
   5955       hpp = elf_sym_hashes (abfd);
   5956       hppend = hpp + extsymcount;
   5957       sym_count = 0;
   5958       for (; hpp < hppend; hpp++)
   5959 	{
   5960 	  h = *hpp;
   5961 	  if (h != NULL
   5962 	      && h->root.type == bfd_link_hash_defined
   5963 	      && !bed->is_function_type (h->type))
   5964 	    {
   5965 	      *sym_hash = h;
   5966 	      sym_hash++;
   5967 	      sym_count++;
   5968 	    }
   5969 	}
   5970 
   5971       qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
   5972 	     elf_sort_symbol);
   5973 
   5974       while (weaks != NULL)
   5975 	{
   5976 	  struct elf_link_hash_entry *hlook;
   5977 	  asection *slook;
   5978 	  bfd_vma vlook;
   5979 	  size_t i, j, idx = 0;
   5980 
   5981 	  hlook = weaks;
   5982 	  weaks = hlook->u.alias;
   5983 	  hlook->u.alias = NULL;
   5984 
   5985 	  if (hlook->root.type != bfd_link_hash_defined
   5986 	      && hlook->root.type != bfd_link_hash_defweak)
   5987 	    continue;
   5988 
   5989 	  slook = hlook->root.u.def.section;
   5990 	  vlook = hlook->root.u.def.value;
   5991 
   5992 	  i = 0;
   5993 	  j = sym_count;
   5994 	  while (i != j)
   5995 	    {
   5996 	      bfd_signed_vma vdiff;
   5997 	      idx = (i + j) / 2;
   5998 	      h = sorted_sym_hash[idx];
   5999 	      vdiff = vlook - h->root.u.def.value;
   6000 	      if (vdiff < 0)
   6001 		j = idx;
   6002 	      else if (vdiff > 0)
   6003 		i = idx + 1;
   6004 	      else
   6005 		{
   6006 		  int sdiff = slook->id - h->root.u.def.section->id;
   6007 		  if (sdiff < 0)
   6008 		    j = idx;
   6009 		  else if (sdiff > 0)
   6010 		    i = idx + 1;
   6011 		  else
   6012 		    break;
   6013 		}
   6014 	    }
   6015 
   6016 	  /* We didn't find a value/section match.  */
   6017 	  if (i == j)
   6018 	    continue;
   6019 
   6020 	  /* With multiple aliases, or when the weak symbol is already
   6021 	     strongly defined, we have multiple matching symbols and
   6022 	     the binary search above may land on any of them.  Step
   6023 	     one past the matching symbol(s).  */
   6024 	  while (++idx != j)
   6025 	    {
   6026 	      h = sorted_sym_hash[idx];
   6027 	      if (h->root.u.def.section != slook
   6028 		  || h->root.u.def.value != vlook)
   6029 		break;
   6030 	    }
   6031 
   6032 	  /* Now look back over the aliases.  Since we sorted by size
   6033 	     as well as value and section, we'll choose the one with
   6034 	     the largest size.  */
   6035 	  while (idx-- != i)
   6036 	    {
   6037 	      h = sorted_sym_hash[idx];
   6038 
   6039 	      /* Stop if value or section doesn't match.  */
   6040 	      if (h->root.u.def.section != slook
   6041 		  || h->root.u.def.value != vlook)
   6042 		break;
   6043 	      else if (h != hlook)
   6044 		{
   6045 		  struct elf_link_hash_entry *t;
   6046 
   6047 		  hlook->u.alias = h;
   6048 		  hlook->is_weakalias = 1;
   6049 		  t = h;
   6050 		  if (t->u.alias != NULL)
   6051 		    while (t->u.alias != h)
   6052 		      t = t->u.alias;
   6053 		  t->u.alias = hlook;
   6054 
   6055 		  /* If the weak definition is in the list of dynamic
   6056 		     symbols, make sure the real definition is put
   6057 		     there as well.  */
   6058 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   6059 		    {
   6060 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6061 			{
   6062 			err_free_sym_hash:
   6063 			  free (sorted_sym_hash);
   6064 			  goto error_return;
   6065 			}
   6066 		    }
   6067 
   6068 		  /* If the real definition is in the list of dynamic
   6069 		     symbols, make sure the weak definition is put
   6070 		     there as well.  If we don't do this, then the
   6071 		     dynamic loader might not merge the entries for the
   6072 		     real definition and the weak definition.  */
   6073 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   6074 		    {
   6075 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   6076 			goto err_free_sym_hash;
   6077 		    }
   6078 		  break;
   6079 		}
   6080 	    }
   6081 	}
   6082 
   6083       free (sorted_sym_hash);
   6084     }
   6085 
   6086   if (bed->check_directives
   6087       && !(*bed->check_directives) (abfd, info))
   6088     goto error_return;
   6089 
   6090   /* If this is a non-traditional link, try to optimize the handling
   6091      of the .stab/.stabstr sections.  */
   6092   if (! dynamic
   6093       && ! info->traditional_format
   6094       && is_elf_hash_table (&htab->root)
   6095       && (info->strip != strip_all && info->strip != strip_debugger))
   6096     {
   6097       asection *stabstr;
   6098 
   6099       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   6100       if (stabstr != NULL)
   6101 	{
   6102 	  bfd_size_type string_offset = 0;
   6103 	  asection *stab;
   6104 
   6105 	  for (stab = abfd->sections; stab; stab = stab->next)
   6106 	    if (startswith (stab->name, ".stab")
   6107 		&& (!stab->name[5] ||
   6108 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   6109 		&& (stab->flags & SEC_MERGE) == 0
   6110 		&& !bfd_is_abs_section (stab->output_section))
   6111 	      {
   6112 		struct bfd_elf_section_data *secdata;
   6113 
   6114 		secdata = elf_section_data (stab);
   6115 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   6116 					       stabstr, &secdata->sec_info,
   6117 					       &string_offset))
   6118 		  goto error_return;
   6119 		if (secdata->sec_info)
   6120 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   6121 	    }
   6122 	}
   6123     }
   6124 
   6125   if (dynamic && add_needed)
   6126     {
   6127       /* Add this bfd to the loaded list.  */
   6128       struct elf_link_loaded_list *n;
   6129 
   6130       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   6131       if (n == NULL)
   6132 	goto error_return;
   6133       n->abfd = abfd;
   6134       n->next = htab->dyn_loaded;
   6135       htab->dyn_loaded = n;
   6136     }
   6137   if (dynamic && !add_needed
   6138       && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
   6139     elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
   6140 
   6141   return true;
   6142 
   6143  error_free_vers:
   6144   free (old_tab);
   6145   free (old_strtab);
   6146   free (nondeflt_vers);
   6147   free (extversym);
   6148  error_free_sym:
   6149   free (isymbuf);
   6150  error_return:
   6151   return false;
   6152 }
   6153 
   6154 /* Return the linker hash table entry of a symbol that might be
   6155    satisfied by an archive symbol.  Return -1 on error.  */
   6156 
   6157 struct bfd_link_hash_entry *
   6158 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   6159 				struct bfd_link_info *info,
   6160 				const char *name)
   6161 {
   6162   struct bfd_link_hash_entry *h;
   6163   char *p, *copy;
   6164   size_t len, first;
   6165 
   6166   h = bfd_link_hash_lookup (info->hash, name, false, false, true);
   6167   if (h != NULL)
   6168     return h;
   6169 
   6170   /* If this is a default version (the name contains @@), look up the
   6171      symbol again with only one `@' as well as without the version.
   6172      The effect is that references to the symbol with and without the
   6173      version will be matched by the default symbol in the archive.  */
   6174 
   6175   p = strchr (name, ELF_VER_CHR);
   6176   if (p == NULL || p[1] != ELF_VER_CHR)
   6177     {
   6178       /* Add this symbol to first hash if this archive has the first
   6179 	 definition.  */
   6180       if (is_elf_hash_table (info->hash))
   6181 	elf_link_add_to_first_hash (abfd, info, name, false);
   6182       return h;
   6183     }
   6184 
   6185   /* First check with only one `@'.  */
   6186   len = strlen (name);
   6187   copy = (char *) bfd_alloc (abfd, len);
   6188   if (copy == NULL)
   6189     return (struct bfd_link_hash_entry *) -1;
   6190 
   6191   first = p - name + 1;
   6192   memcpy (copy, name, first);
   6193   memcpy (copy + first, name + first + 1, len - first);
   6194 
   6195   h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   6196   if (h == NULL)
   6197     {
   6198       /* We also need to check references to the symbol without the
   6199 	 version.  */
   6200       copy[first - 1] = '\0';
   6201       h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   6202     }
   6203 
   6204   bfd_release (abfd, copy);
   6205   return h;
   6206 }
   6207 
   6208 /* Add symbols from an ELF archive file to the linker hash table.  We
   6209    don't use _bfd_generic_link_add_archive_symbols because we need to
   6210    handle versioned symbols.
   6211 
   6212    Fortunately, ELF archive handling is simpler than that done by
   6213    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   6214    oddities.  In ELF, if we find a symbol in the archive map, and the
   6215    symbol is currently undefined, we know that we must pull in that
   6216    object file.
   6217 
   6218    Unfortunately, we do have to make multiple passes over the symbol
   6219    table until nothing further is resolved.  */
   6220 
   6221 static bool
   6222 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   6223 {
   6224   symindex c;
   6225   unsigned char *included = NULL;
   6226   carsym *symdefs;
   6227   bool loop;
   6228   size_t amt;
   6229   const struct elf_backend_data *bed;
   6230   struct bfd_link_hash_entry * (*archive_symbol_lookup)
   6231     (bfd *, struct bfd_link_info *, const char *);
   6232 
   6233   if (! bfd_has_map (abfd))
   6234     {
   6235       /* An empty archive is a special case.  */
   6236       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   6237 	return true;
   6238       bfd_set_error (bfd_error_no_armap);
   6239       return false;
   6240     }
   6241 
   6242   /* Keep track of all symbols we know to be already defined, and all
   6243      files we know to be already included.  This is to speed up the
   6244      second and subsequent passes.  */
   6245   c = bfd_ardata (abfd)->symdef_count;
   6246   if (c == 0)
   6247     return true;
   6248   amt = c * sizeof (*included);
   6249   included = (unsigned char *) bfd_zmalloc (amt);
   6250   if (included == NULL)
   6251     return false;
   6252 
   6253   symdefs = bfd_ardata (abfd)->symdefs;
   6254   bed = get_elf_backend_data (abfd);
   6255   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   6256 
   6257   do
   6258     {
   6259       file_ptr last;
   6260       symindex i;
   6261       carsym *symdef;
   6262       carsym *symdefend;
   6263 
   6264       loop = false;
   6265       last = -1;
   6266 
   6267       symdef = symdefs;
   6268       symdefend = symdef + c;
   6269       for (i = 0; symdef < symdefend; symdef++, i++)
   6270 	{
   6271 	  struct bfd_link_hash_entry *h;
   6272 	  bfd *element;
   6273 	  struct bfd_link_hash_entry *undefs_tail;
   6274 	  symindex mark;
   6275 
   6276 	  if (included[i])
   6277 	    continue;
   6278 	  if (symdef->file_offset == last)
   6279 	    {
   6280 	      included[i] = true;
   6281 	      continue;
   6282 	    }
   6283 
   6284 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   6285 	  if (h == (struct bfd_link_hash_entry *) -1)
   6286 	    goto error_return;
   6287 
   6288 	  if (h == NULL)
   6289 	    continue;
   6290 
   6291 	  if (h->type == bfd_link_hash_undefined)
   6292 	    {
   6293 	      if (is_elf_hash_table (info->hash))
   6294 		{
   6295 		  /* If the archive element has already been loaded then one
   6296 		     of the symbols defined by that element might have been
   6297 		     made undefined due to being in a discarded section.  */
   6298 		  if (((struct elf_link_hash_entry *) h)->indx == -3)
   6299 		    continue;
   6300 
   6301 		  /* In the pre-LTO-plugin pass we must not mistakenly
   6302 		     include this archive member if an earlier shared
   6303 		     library defined this symbol.  */
   6304 		  struct elf_link_hash_table *htab = elf_hash_table (info);
   6305 		  if (htab->first_hash)
   6306 		    {
   6307 		      struct elf_link_first_hash_entry *e
   6308 			  = ((struct elf_link_first_hash_entry *)
   6309 			     bfd_hash_lookup (htab->first_hash, symdef->name,
   6310 					      false, false));
   6311 		      if (e
   6312 			  && (e->abfd->flags & DYNAMIC) != 0
   6313 			  && e->abfd != abfd)
   6314 			continue;
   6315 		    }
   6316 		}
   6317 	    }
   6318 	  else if (h->type == bfd_link_hash_common)
   6319 	    {
   6320 	      /* We currently have a common symbol.  The archive map contains
   6321 		 a reference to this symbol, so we may want to include it.  We
   6322 		 only want to include it however, if this archive element
   6323 		 contains a definition of the symbol, not just another common
   6324 		 declaration of it.
   6325 
   6326 		 Unfortunately some archivers (including GNU ar) will put
   6327 		 declarations of common symbols into their archive maps, as
   6328 		 well as real definitions, so we cannot just go by the archive
   6329 		 map alone.  Instead we must read in the element's symbol
   6330 		 table and check that to see what kind of symbol definition
   6331 		 this is.  */
   6332 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   6333 		continue;
   6334 	    }
   6335 	  else
   6336 	    {
   6337 	      if (h->type != bfd_link_hash_undefweak)
   6338 		/* Symbol must be defined.  Don't check it again.  */
   6339 		included[i] = true;
   6340 
   6341 	      if (!is_elf_hash_table (info->hash))
   6342 		continue;
   6343 	      struct elf_link_hash_entry *eh
   6344 		= (struct elf_link_hash_entry *) h;
   6345 	      /* Ignore the archive if the symbol isn't referenced by a
   6346 		 regular object or isn't defined in a shared object.  */
   6347 	      if (!eh->ref_regular || !eh->def_dynamic)
   6348 		continue;
   6349 	      /* Ignore the dynamic definition if symbol is first
   6350 		 defined in this archive.  */
   6351 	      struct elf_link_hash_table *htab = elf_hash_table (info);
   6352 	      if (htab->first_hash == NULL)
   6353 		continue;
   6354 	      struct elf_link_first_hash_entry *e
   6355 		= ((struct elf_link_first_hash_entry *)
   6356 		   bfd_hash_lookup (htab->first_hash, symdef->name,
   6357 				    false, false));
   6358 	      if (e == NULL || e->abfd != abfd)
   6359 		continue;
   6360 	    }
   6361 
   6362 	  /* We need to include this archive member.  */
   6363 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
   6364 					     info);
   6365 	  if (element == NULL)
   6366 	    goto error_return;
   6367 
   6368 	  if (! bfd_check_format (element, bfd_object))
   6369 	    goto error_return;
   6370 
   6371 	  undefs_tail = info->hash->undefs_tail;
   6372 
   6373 	  if (!(*info->callbacks
   6374 		->add_archive_element) (info, element, symdef->name, &element))
   6375 	    continue;
   6376 	  if (!bfd_link_add_symbols (element, info))
   6377 	    goto error_return;
   6378 
   6379 	  /* If there are any new undefined symbols, we need to make
   6380 	     another pass through the archive in order to see whether
   6381 	     they can be defined.  FIXME: This isn't perfect, because
   6382 	     common symbols wind up on undefs_tail and because an
   6383 	     undefined symbol which is defined later on in this pass
   6384 	     does not require another pass.  This isn't a bug, but it
   6385 	     does make the code less efficient than it could be.  */
   6386 	  if (undefs_tail != info->hash->undefs_tail)
   6387 	    loop = true;
   6388 
   6389 	  /* Look backward to mark all symbols from this object file
   6390 	     which we have already seen in this pass.  */
   6391 	  mark = i;
   6392 	  do
   6393 	    {
   6394 	      included[mark] = true;
   6395 	      if (mark == 0)
   6396 		break;
   6397 	      --mark;
   6398 	    }
   6399 	  while (symdefs[mark].file_offset == symdef->file_offset);
   6400 
   6401 	  /* We mark subsequent symbols from this object file as we go
   6402 	     on through the loop.  */
   6403 	  last = symdef->file_offset;
   6404 	}
   6405     }
   6406   while (loop);
   6407 
   6408   free (included);
   6409   return true;
   6410 
   6411  error_return:
   6412   free (included);
   6413   return false;
   6414 }
   6415 
   6416 /* Given an ELF BFD, add symbols to the global hash table as
   6417    appropriate.  */
   6418 
   6419 bool
   6420 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   6421 {
   6422   switch (bfd_get_format (abfd))
   6423     {
   6424     case bfd_object:
   6425       return elf_link_add_object_symbols (abfd, info);
   6426     case bfd_archive:
   6427       return elf_link_add_archive_symbols (abfd, info);
   6428     default:
   6429       bfd_set_error (bfd_error_wrong_format);
   6430       return false;
   6431     }
   6432 }
   6433 
   6434 struct hash_codes_info
   6436 {
   6437   unsigned long *hashcodes;
   6438   bool error;
   6439 };
   6440 
   6441 /* This function will be called though elf_link_hash_traverse to store
   6442    all hash value of the exported symbols in an array.  */
   6443 
   6444 static bool
   6445 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   6446 {
   6447   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   6448   const char *name;
   6449   unsigned long ha;
   6450   char *alc = NULL;
   6451 
   6452   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6453   if (h->dynindx == -1)
   6454     return true;
   6455 
   6456   name = h->root.root.string;
   6457   if (h->versioned >= versioned)
   6458     {
   6459       char *p = strchr (name, ELF_VER_CHR);
   6460       if (p != NULL)
   6461 	{
   6462 	  alc = (char *) bfd_malloc (p - name + 1);
   6463 	  if (alc == NULL)
   6464 	    {
   6465 	      inf->error = true;
   6466 	      return false;
   6467 	    }
   6468 	  memcpy (alc, name, p - name);
   6469 	  alc[p - name] = '\0';
   6470 	  name = alc;
   6471 	}
   6472     }
   6473 
   6474   /* Compute the hash value.  */
   6475   ha = bfd_elf_hash (name);
   6476 
   6477   /* Store the found hash value in the array given as the argument.  */
   6478   *(inf->hashcodes)++ = ha;
   6479 
   6480   /* And store it in the struct so that we can put it in the hash table
   6481      later.  */
   6482   h->u.elf_hash_value = ha;
   6483 
   6484   free (alc);
   6485   return true;
   6486 }
   6487 
   6488 struct collect_gnu_hash_codes
   6489 {
   6490   bfd *output_bfd;
   6491   const struct elf_backend_data *bed;
   6492   unsigned long int nsyms;
   6493   unsigned long int maskbits;
   6494   unsigned long int *hashcodes;
   6495   unsigned long int *hashval;
   6496   unsigned long int *indx;
   6497   unsigned long int *counts;
   6498   bfd_vma *bitmask;
   6499   bfd_byte *contents;
   6500   bfd_size_type xlat;
   6501   long int min_dynindx;
   6502   unsigned long int bucketcount;
   6503   unsigned long int symindx;
   6504   long int local_indx;
   6505   long int shift1, shift2;
   6506   unsigned long int mask;
   6507   bool error;
   6508 };
   6509 
   6510 /* This function will be called though elf_link_hash_traverse to store
   6511    all hash value of the exported symbols in an array.  */
   6512 
   6513 static bool
   6514 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   6515 {
   6516   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6517   const char *name;
   6518   unsigned long ha;
   6519   char *alc = NULL;
   6520 
   6521   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6522   if (h->dynindx == -1)
   6523     return true;
   6524 
   6525   /* Ignore also local symbols and undefined symbols.  */
   6526   if (! (*s->bed->elf_hash_symbol) (h))
   6527     return true;
   6528 
   6529   name = h->root.root.string;
   6530   if (h->versioned >= versioned)
   6531     {
   6532       char *p = strchr (name, ELF_VER_CHR);
   6533       if (p != NULL)
   6534 	{
   6535 	  alc = (char *) bfd_malloc (p - name + 1);
   6536 	  if (alc == NULL)
   6537 	    {
   6538 	      s->error = true;
   6539 	      return false;
   6540 	    }
   6541 	  memcpy (alc, name, p - name);
   6542 	  alc[p - name] = '\0';
   6543 	  name = alc;
   6544 	}
   6545     }
   6546 
   6547   /* Compute the hash value.  */
   6548   ha = bfd_elf_gnu_hash (name);
   6549 
   6550   /* Store the found hash value in the array for compute_bucket_count,
   6551      and also for .dynsym reordering purposes.  */
   6552   s->hashcodes[s->nsyms] = ha;
   6553   s->hashval[h->dynindx] = ha;
   6554   ++s->nsyms;
   6555   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   6556     s->min_dynindx = h->dynindx;
   6557 
   6558   free (alc);
   6559   return true;
   6560 }
   6561 
   6562 /* This function will be called though elf_link_hash_traverse to do
   6563    final dynamic symbol renumbering in case of .gnu.hash.
   6564    If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
   6565    to the translation table.  */
   6566 
   6567 static bool
   6568 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
   6569 {
   6570   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6571   unsigned long int bucket;
   6572   unsigned long int val;
   6573 
   6574   /* Ignore indirect symbols.  */
   6575   if (h->dynindx == -1)
   6576     return true;
   6577 
   6578   /* Ignore also local symbols and undefined symbols.  */
   6579   if (! (*s->bed->elf_hash_symbol) (h))
   6580     {
   6581       if (h->dynindx >= s->min_dynindx)
   6582 	{
   6583 	  if (s->bed->record_xhash_symbol != NULL)
   6584 	    {
   6585 	      (*s->bed->record_xhash_symbol) (h, 0);
   6586 	      s->local_indx++;
   6587 	    }
   6588 	  else
   6589 	    h->dynindx = s->local_indx++;
   6590 	}
   6591       return true;
   6592     }
   6593 
   6594   bucket = s->hashval[h->dynindx] % s->bucketcount;
   6595   val = (s->hashval[h->dynindx] >> s->shift1)
   6596 	& ((s->maskbits >> s->shift1) - 1);
   6597   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   6598   s->bitmask[val]
   6599     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   6600   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   6601   if (s->counts[bucket] == 1)
   6602     /* Last element terminates the chain.  */
   6603     val |= 1;
   6604   bfd_put_32 (s->output_bfd, val,
   6605 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   6606   --s->counts[bucket];
   6607   if (s->bed->record_xhash_symbol != NULL)
   6608     {
   6609       bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
   6610 
   6611       (*s->bed->record_xhash_symbol) (h, xlat_loc);
   6612     }
   6613   else
   6614     h->dynindx = s->indx[bucket]++;
   6615   return true;
   6616 }
   6617 
   6618 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   6619 
   6620 bool
   6621 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   6622 {
   6623   return !(h->forced_local
   6624 	   || h->root.type == bfd_link_hash_undefined
   6625 	   || h->root.type == bfd_link_hash_undefweak
   6626 	   || ((h->root.type == bfd_link_hash_defined
   6627 		|| h->root.type == bfd_link_hash_defweak)
   6628 	       && h->root.u.def.section->output_section == NULL));
   6629 }
   6630 
   6631 /* Array used to determine the number of hash table buckets to use
   6632    based on the number of symbols there are.  If there are fewer than
   6633    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   6634    fewer than 37 we use 17 buckets, and so forth.  We never use more
   6635    than 32771 buckets.  */
   6636 
   6637 static const size_t elf_buckets[] =
   6638 {
   6639   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   6640   16411, 32771, 0
   6641 };
   6642 
   6643 /* Compute bucket count for hashing table.  We do not use a static set
   6644    of possible tables sizes anymore.  Instead we determine for all
   6645    possible reasonable sizes of the table the outcome (i.e., the
   6646    number of collisions etc) and choose the best solution.  The
   6647    weighting functions are not too simple to allow the table to grow
   6648    without bounds.  Instead one of the weighting factors is the size.
   6649    Therefore the result is always a good payoff between few collisions
   6650    (= short chain lengths) and table size.  */
   6651 static size_t
   6652 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6653 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   6654 		      unsigned long int nsyms,
   6655 		      int gnu_hash)
   6656 {
   6657   size_t best_size = 0;
   6658   unsigned long int i;
   6659 
   6660   if (info->optimize)
   6661     {
   6662       size_t minsize;
   6663       size_t maxsize;
   6664       uint64_t best_chlen = ~((uint64_t) 0);
   6665       bfd *dynobj = elf_hash_table (info)->dynobj;
   6666       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   6667       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   6668       unsigned long int *counts;
   6669       bfd_size_type amt;
   6670       unsigned int no_improvement_count = 0;
   6671 
   6672       /* Possible optimization parameters: if we have NSYMS symbols we say
   6673 	 that the hashing table must at least have NSYMS/4 and at most
   6674 	 2*NSYMS buckets.  */
   6675       minsize = nsyms / 4;
   6676       if (minsize == 0)
   6677 	minsize = 1;
   6678       best_size = maxsize = nsyms * 2;
   6679       if (gnu_hash)
   6680 	{
   6681 	  if (minsize < 2)
   6682 	    minsize = 2;
   6683 	  if ((best_size & 31) == 0)
   6684 	    ++best_size;
   6685 	}
   6686 
   6687       /* Create array where we count the collisions in.  We must use bfd_malloc
   6688 	 since the size could be large.  */
   6689       amt = maxsize;
   6690       amt *= sizeof (unsigned long int);
   6691       counts = (unsigned long int *) bfd_malloc (amt);
   6692       if (counts == NULL)
   6693 	return 0;
   6694 
   6695       /* Compute the "optimal" size for the hash table.  The criteria is a
   6696 	 minimal chain length.  The minor criteria is (of course) the size
   6697 	 of the table.  */
   6698       for (i = minsize; i < maxsize; ++i)
   6699 	{
   6700 	  /* Walk through the array of hashcodes and count the collisions.  */
   6701 	  uint64_t max;
   6702 	  unsigned long int j;
   6703 	  unsigned long int fact;
   6704 
   6705 	  if (gnu_hash && (i & 31) == 0)
   6706 	    continue;
   6707 
   6708 	  memset (counts, '\0', i * sizeof (unsigned long int));
   6709 
   6710 	  /* Determine how often each hash bucket is used.  */
   6711 	  for (j = 0; j < nsyms; ++j)
   6712 	    ++counts[hashcodes[j] % i];
   6713 
   6714 	  /* For the weight function we need some information about the
   6715 	     pagesize on the target.  This is information need not be 100%
   6716 	     accurate.  Since this information is not available (so far) we
   6717 	     define it here to a reasonable default value.  If it is crucial
   6718 	     to have a better value some day simply define this value.  */
   6719 # ifndef BFD_TARGET_PAGESIZE
   6720 #  define BFD_TARGET_PAGESIZE	(4096)
   6721 # endif
   6722 
   6723 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   6724 	     and the chains.  */
   6725 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   6726 
   6727 # if 1
   6728 	  /* Variant 1: optimize for short chains.  We add the squares
   6729 	     of all the chain lengths (which favors many small chain
   6730 	     over a few long chains).  */
   6731 	  for (j = 0; j < i; ++j)
   6732 	    max += counts[j] * counts[j];
   6733 
   6734 	  /* This adds penalties for the overall size of the table.  */
   6735 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6736 	  max *= fact * fact;
   6737 # else
   6738 	  /* Variant 2: Optimize a lot more for small table.  Here we
   6739 	     also add squares of the size but we also add penalties for
   6740 	     empty slots (the +1 term).  */
   6741 	  for (j = 0; j < i; ++j)
   6742 	    max += (1 + counts[j]) * (1 + counts[j]);
   6743 
   6744 	  /* The overall size of the table is considered, but not as
   6745 	     strong as in variant 1, where it is squared.  */
   6746 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6747 	  max *= fact;
   6748 # endif
   6749 
   6750 	  /* Compare with current best results.  */
   6751 	  if (max < best_chlen)
   6752 	    {
   6753 	      best_chlen = max;
   6754 	      best_size = i;
   6755 	      no_improvement_count = 0;
   6756 	    }
   6757 	  /* PR 11843: Avoid futile long searches for the best bucket size
   6758 	     when there are a large number of symbols.  */
   6759 	  else if (++no_improvement_count == 100)
   6760 	    break;
   6761 	}
   6762 
   6763       free (counts);
   6764     }
   6765   else
   6766     {
   6767       for (i = 0; elf_buckets[i] != 0; i++)
   6768 	{
   6769 	  best_size = elf_buckets[i];
   6770 	  if (nsyms < elf_buckets[i + 1])
   6771 	    break;
   6772 	}
   6773       if (gnu_hash && best_size < 2)
   6774 	best_size = 2;
   6775     }
   6776 
   6777   return best_size;
   6778 }
   6779 
   6780 /* Size any SHT_GROUP section for ld -r.  */
   6781 
   6782 bool
   6783 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   6784 {
   6785   bfd *ibfd;
   6786   asection *s;
   6787 
   6788   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6789     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   6790 	&& (s = ibfd->sections) != NULL
   6791 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
   6792 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   6793       return false;
   6794   return true;
   6795 }
   6796 
   6797 /* Set a default stack segment size.  The value in INFO wins.  If it
   6798    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   6799    undefined it is initialized.  */
   6800 
   6801 bool
   6802 bfd_elf_stack_segment_size (bfd *output_bfd,
   6803 			    struct bfd_link_info *info,
   6804 			    const char *legacy_symbol,
   6805 			    bfd_vma default_size)
   6806 {
   6807   struct elf_link_hash_entry *h = NULL;
   6808 
   6809   /* Look for legacy symbol.  */
   6810   if (legacy_symbol)
   6811     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   6812 			      false, false, false);
   6813   if (h && (h->root.type == bfd_link_hash_defined
   6814 	    || h->root.type == bfd_link_hash_defweak)
   6815       && h->def_regular
   6816       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   6817     {
   6818       /* The symbol has no type if specified on the command line.  */
   6819       h->type = STT_OBJECT;
   6820       if (info->stacksize)
   6821 	/* xgettext:c-format */
   6822 	_bfd_error_handler (_("%pB: stack size specified and %s set"),
   6823 			    output_bfd, legacy_symbol);
   6824       else if (h->root.u.def.section != bfd_abs_section_ptr)
   6825 	/* xgettext:c-format */
   6826 	_bfd_error_handler (_("%pB: %s not absolute"),
   6827 			    output_bfd, legacy_symbol);
   6828       else
   6829 	info->stacksize = h->root.u.def.value;
   6830     }
   6831 
   6832   if (!info->stacksize)
   6833     /* If the user didn't set a size, or explicitly inhibit the
   6834        size, set it now.  */
   6835     info->stacksize = default_size;
   6836 
   6837   /* Provide the legacy symbol, if it is referenced.  */
   6838   if (h && (h->root.type == bfd_link_hash_undefined
   6839 	    || h->root.type == bfd_link_hash_undefweak))
   6840     {
   6841       struct bfd_link_hash_entry *bh = NULL;
   6842 
   6843       if (!(_bfd_generic_link_add_one_symbol
   6844 	    (info, output_bfd, legacy_symbol,
   6845 	     BSF_GLOBAL, bfd_abs_section_ptr,
   6846 	     info->stacksize >= 0 ? info->stacksize : 0,
   6847 	     NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
   6848 	return false;
   6849 
   6850       h = (struct elf_link_hash_entry *) bh;
   6851       h->def_regular = 1;
   6852       h->type = STT_OBJECT;
   6853     }
   6854 
   6855   return true;
   6856 }
   6857 
   6858 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   6859 
   6860 struct elf_gc_sweep_symbol_info
   6861 {
   6862   struct bfd_link_info *info;
   6863   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   6864 		       bool);
   6865 };
   6866 
   6867 static bool
   6868 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   6869 {
   6870   if (!h->mark
   6871       && (((h->root.type == bfd_link_hash_defined
   6872 	    || h->root.type == bfd_link_hash_defweak)
   6873 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   6874 		&& h->root.u.def.section->gc_mark))
   6875 	  || h->root.type == bfd_link_hash_undefined
   6876 	  || h->root.type == bfd_link_hash_undefweak))
   6877     {
   6878       struct elf_gc_sweep_symbol_info *inf;
   6879 
   6880       inf = (struct elf_gc_sweep_symbol_info *) data;
   6881       (*inf->hide_symbol) (inf->info, h, true);
   6882       h->def_regular = 0;
   6883       h->ref_regular = 0;
   6884       h->ref_regular_nonweak = 0;
   6885     }
   6886 
   6887   return true;
   6888 }
   6889 
   6890 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   6891    called by the ELF linker emulation before_allocation routine.  We
   6892    must set the sizes of the sections before the linker sets the
   6893    addresses of the various sections.  */
   6894 
   6895 bool
   6896 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   6897 			       const char *soname,
   6898 			       const char *rpath,
   6899 			       const char *filter_shlib,
   6900 			       const char *audit,
   6901 			       const char *depaudit,
   6902 			       const char * const *auxiliary_filters,
   6903 			       struct bfd_link_info *info,
   6904 			       asection **sinterpptr)
   6905 {
   6906   bfd *dynobj;
   6907   const struct elf_backend_data *bed;
   6908 
   6909   *sinterpptr = NULL;
   6910 
   6911   if (!is_elf_hash_table (info->hash))
   6912     return true;
   6913 
   6914   /* Any syms created from now on start with -1 in
   6915      got.refcount/offset and plt.refcount/offset.  */
   6916   elf_hash_table (info)->init_got_refcount
   6917     = elf_hash_table (info)->init_got_offset;
   6918   elf_hash_table (info)->init_plt_refcount
   6919     = elf_hash_table (info)->init_plt_offset;
   6920 
   6921   bed = get_elf_backend_data (output_bfd);
   6922 
   6923   /* The backend may have to create some sections regardless of whether
   6924      we're dynamic or not.  */
   6925   if (bed->elf_backend_early_size_sections
   6926       && !bed->elf_backend_early_size_sections (output_bfd, info))
   6927     return false;
   6928 
   6929   dynobj = elf_hash_table (info)->dynobj;
   6930 
   6931   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6932     {
   6933       struct bfd_elf_version_tree *verdefs;
   6934       struct elf_info_failed asvinfo;
   6935       struct bfd_elf_version_tree *t;
   6936       struct bfd_elf_version_expr *d;
   6937       asection *s;
   6938       size_t soname_indx;
   6939 
   6940       /* If we are supposed to export all symbols into the dynamic symbol
   6941 	 table (this is not the normal case), then do so.  */
   6942       if (info->export_dynamic
   6943 	  || (bfd_link_executable (info) && info->dynamic))
   6944 	{
   6945 	  struct elf_info_failed eif;
   6946 
   6947 	  eif.info = info;
   6948 	  eif.failed = false;
   6949 	  elf_link_hash_traverse (elf_hash_table (info),
   6950 				  _bfd_elf_export_symbol,
   6951 				  &eif);
   6952 	  if (eif.failed)
   6953 	    return false;
   6954 	}
   6955 
   6956       if (soname != NULL)
   6957 	{
   6958 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6959 					     soname, true);
   6960 	  if (soname_indx == (size_t) -1
   6961 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   6962 	    return false;
   6963 	}
   6964       else
   6965 	soname_indx = (size_t) -1;
   6966 
   6967       /* Make all global versions with definition.  */
   6968       for (t = info->version_info; t != NULL; t = t->next)
   6969 	for (d = t->globals.list; d != NULL; d = d->next)
   6970 	  if (!d->symver && d->literal)
   6971 	    {
   6972 	      const char *verstr, *name;
   6973 	      size_t namelen, verlen, newlen;
   6974 	      char *newname, *p, leading_char;
   6975 	      struct elf_link_hash_entry *newh;
   6976 
   6977 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   6978 	      name = d->pattern;
   6979 	      namelen = strlen (name) + (leading_char != '\0');
   6980 	      verstr = t->name;
   6981 	      verlen = strlen (verstr);
   6982 	      newlen = namelen + verlen + 3;
   6983 
   6984 	      newname = (char *) bfd_malloc (newlen);
   6985 	      if (newname == NULL)
   6986 		return false;
   6987 	      newname[0] = leading_char;
   6988 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   6989 
   6990 	      /* Check the hidden versioned definition.  */
   6991 	      p = newname + namelen;
   6992 	      *p++ = ELF_VER_CHR;
   6993 	      memcpy (p, verstr, verlen + 1);
   6994 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   6995 					   newname, false, false,
   6996 					   false);
   6997 	      if (newh == NULL
   6998 		  || (newh->root.type != bfd_link_hash_defined
   6999 		      && newh->root.type != bfd_link_hash_defweak))
   7000 		{
   7001 		  /* Check the default versioned definition.  */
   7002 		  *p++ = ELF_VER_CHR;
   7003 		  memcpy (p, verstr, verlen + 1);
   7004 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   7005 					       newname, false, false,
   7006 					       false);
   7007 		}
   7008 	      free (newname);
   7009 
   7010 	      /* Mark this version if there is a definition and it is
   7011 		 not defined in a shared object.  */
   7012 	      if (newh != NULL
   7013 		  && !newh->def_dynamic
   7014 		  && (newh->root.type == bfd_link_hash_defined
   7015 		      || newh->root.type == bfd_link_hash_defweak))
   7016 		d->symver = 1;
   7017 	    }
   7018 
   7019       /* Attach all the symbols to their version information.  */
   7020       asvinfo.info = info;
   7021       asvinfo.failed = false;
   7022 
   7023       elf_link_hash_traverse (elf_hash_table (info),
   7024 			      _bfd_elf_link_assign_sym_version,
   7025 			      &asvinfo);
   7026       if (asvinfo.failed)
   7027 	return false;
   7028 
   7029       if (!info->allow_undefined_version)
   7030 	{
   7031 	  /* Check if all global versions have a definition.  */
   7032 	  bool all_defined = true;
   7033 	  for (t = info->version_info; t != NULL; t = t->next)
   7034 	    for (d = t->globals.list; d != NULL; d = d->next)
   7035 	      if (d->literal && !d->symver && !d->script)
   7036 		{
   7037 		  _bfd_error_handler
   7038 		    (_("%s: undefined version: %s"),
   7039 		     d->pattern, t->name);
   7040 		  all_defined = false;
   7041 		}
   7042 
   7043 	  if (!all_defined)
   7044 	    {
   7045 	      bfd_set_error (bfd_error_bad_value);
   7046 	      return false;
   7047 	    }
   7048 	}
   7049 
   7050       /* Set up the version definition section.  */
   7051       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   7052       BFD_ASSERT (s != NULL);
   7053 
   7054       /* We may have created additional version definitions if we are
   7055 	 just linking a regular application.  */
   7056       verdefs = info->version_info;
   7057 
   7058       /* Skip anonymous version tag.  */
   7059       if (verdefs != NULL && verdefs->vernum == 0)
   7060 	verdefs = verdefs->next;
   7061 
   7062       if (verdefs == NULL && !info->create_default_symver)
   7063 	s->flags |= SEC_EXCLUDE;
   7064       else
   7065 	{
   7066 	  unsigned int cdefs;
   7067 	  bfd_size_type size;
   7068 	  bfd_byte *p;
   7069 	  Elf_Internal_Verdef def;
   7070 	  Elf_Internal_Verdaux defaux;
   7071 	  struct bfd_link_hash_entry *bh;
   7072 	  struct elf_link_hash_entry *h;
   7073 	  const char *name;
   7074 
   7075 	  cdefs = 0;
   7076 	  size = 0;
   7077 
   7078 	  /* Make space for the base version.  */
   7079 	  size += sizeof (Elf_External_Verdef);
   7080 	  size += sizeof (Elf_External_Verdaux);
   7081 	  ++cdefs;
   7082 
   7083 	  /* Make space for the default version.  */
   7084 	  if (info->create_default_symver)
   7085 	    {
   7086 	      size += sizeof (Elf_External_Verdef);
   7087 	      ++cdefs;
   7088 	    }
   7089 
   7090 	  for (t = verdefs; t != NULL; t = t->next)
   7091 	    {
   7092 	      struct bfd_elf_version_deps *n;
   7093 
   7094 	      /* Don't emit base version twice.  */
   7095 	      if (t->vernum == 0)
   7096 		continue;
   7097 
   7098 	      size += sizeof (Elf_External_Verdef);
   7099 	      size += sizeof (Elf_External_Verdaux);
   7100 	      ++cdefs;
   7101 
   7102 	      for (n = t->deps; n != NULL; n = n->next)
   7103 		size += sizeof (Elf_External_Verdaux);
   7104 	    }
   7105 
   7106 	  s->size = size;
   7107 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7108 	  if (s->contents == NULL && s->size != 0)
   7109 	    return false;
   7110 	  s->alloced = 1;
   7111 
   7112 	  /* Fill in the version definition section.  */
   7113 
   7114 	  p = s->contents;
   7115 
   7116 	  def.vd_version = VER_DEF_CURRENT;
   7117 	  def.vd_flags = VER_FLG_BASE;
   7118 	  def.vd_ndx = 1;
   7119 	  def.vd_cnt = 1;
   7120 	  if (info->create_default_symver)
   7121 	    {
   7122 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   7123 	      def.vd_next = sizeof (Elf_External_Verdef);
   7124 	    }
   7125 	  else
   7126 	    {
   7127 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7128 	      def.vd_next = (sizeof (Elf_External_Verdef)
   7129 			     + sizeof (Elf_External_Verdaux));
   7130 	    }
   7131 
   7132 	  if (soname_indx != (size_t) -1)
   7133 	    {
   7134 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7135 				      soname_indx);
   7136 	      def.vd_hash = bfd_elf_hash (soname);
   7137 	      defaux.vda_name = soname_indx;
   7138 	      name = soname;
   7139 	    }
   7140 	  else
   7141 	    {
   7142 	      size_t indx;
   7143 
   7144 	      name = lbasename (bfd_get_filename (output_bfd));
   7145 	      def.vd_hash = bfd_elf_hash (name);
   7146 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7147 					  name, false);
   7148 	      if (indx == (size_t) -1)
   7149 		return false;
   7150 	      defaux.vda_name = indx;
   7151 	    }
   7152 	  defaux.vda_next = 0;
   7153 
   7154 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   7155 				    (Elf_External_Verdef *) p);
   7156 	  p += sizeof (Elf_External_Verdef);
   7157 	  if (info->create_default_symver)
   7158 	    {
   7159 	      /* Add a symbol representing this version.  */
   7160 	      bh = NULL;
   7161 	      if (! (_bfd_generic_link_add_one_symbol
   7162 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   7163 		      0, NULL, false,
   7164 		      get_elf_backend_data (dynobj)->collect, &bh)))
   7165 		return false;
   7166 	      h = (struct elf_link_hash_entry *) bh;
   7167 	      h->non_elf = 0;
   7168 	      h->def_regular = 1;
   7169 	      h->type = STT_OBJECT;
   7170 	      h->verinfo.vertree = NULL;
   7171 
   7172 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7173 		return false;
   7174 
   7175 	      /* Create a duplicate of the base version with the same
   7176 		 aux block, but different flags.  */
   7177 	      def.vd_flags = 0;
   7178 	      def.vd_ndx = 2;
   7179 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7180 	      if (verdefs)
   7181 		def.vd_next = (sizeof (Elf_External_Verdef)
   7182 			       + sizeof (Elf_External_Verdaux));
   7183 	      else
   7184 		def.vd_next = 0;
   7185 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   7186 					(Elf_External_Verdef *) p);
   7187 	      p += sizeof (Elf_External_Verdef);
   7188 	    }
   7189 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7190 				     (Elf_External_Verdaux *) p);
   7191 	  p += sizeof (Elf_External_Verdaux);
   7192 
   7193 	  for (t = verdefs; t != NULL; t = t->next)
   7194 	    {
   7195 	      unsigned int cdeps;
   7196 	      struct bfd_elf_version_deps *n;
   7197 
   7198 	      /* Don't emit the base version twice.  */
   7199 	      if (t->vernum == 0)
   7200 		continue;
   7201 
   7202 	      cdeps = 0;
   7203 	      for (n = t->deps; n != NULL; n = n->next)
   7204 		++cdeps;
   7205 
   7206 	      /* Add a symbol representing this version.  */
   7207 	      bh = NULL;
   7208 	      if (! (_bfd_generic_link_add_one_symbol
   7209 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   7210 		      0, NULL, false,
   7211 		      get_elf_backend_data (dynobj)->collect, &bh)))
   7212 		return false;
   7213 	      h = (struct elf_link_hash_entry *) bh;
   7214 	      h->non_elf = 0;
   7215 	      h->def_regular = 1;
   7216 	      h->type = STT_OBJECT;
   7217 	      h->verinfo.vertree = t;
   7218 
   7219 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7220 		return false;
   7221 
   7222 	      def.vd_version = VER_DEF_CURRENT;
   7223 	      def.vd_flags = 0;
   7224 	      if (t->globals.list == NULL
   7225 		  && t->locals.list == NULL
   7226 		  && ! t->used)
   7227 		def.vd_flags |= VER_FLG_WEAK;
   7228 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   7229 	      def.vd_cnt = cdeps + 1;
   7230 	      def.vd_hash = bfd_elf_hash (t->name);
   7231 	      def.vd_aux = sizeof (Elf_External_Verdef);
   7232 	      def.vd_next = 0;
   7233 
   7234 	      /* If a basever node is next, it *must* be the last node in
   7235 		 the chain, otherwise Verdef construction breaks.  */
   7236 	      if (t->next != NULL && t->next->vernum == 0)
   7237 		BFD_ASSERT (t->next->next == NULL);
   7238 
   7239 	      if (t->next != NULL && t->next->vernum != 0)
   7240 		def.vd_next = (sizeof (Elf_External_Verdef)
   7241 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   7242 
   7243 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   7244 					(Elf_External_Verdef *) p);
   7245 	      p += sizeof (Elf_External_Verdef);
   7246 
   7247 	      defaux.vda_name = h->dynstr_index;
   7248 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7249 				      h->dynstr_index);
   7250 	      defaux.vda_next = 0;
   7251 	      if (t->deps != NULL)
   7252 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   7253 	      t->name_indx = defaux.vda_name;
   7254 
   7255 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7256 					 (Elf_External_Verdaux *) p);
   7257 	      p += sizeof (Elf_External_Verdaux);
   7258 
   7259 	      for (n = t->deps; n != NULL; n = n->next)
   7260 		{
   7261 		  if (n->version_needed == NULL)
   7262 		    {
   7263 		      /* This can happen if there was an error in the
   7264 			 version script.  */
   7265 		      defaux.vda_name = 0;
   7266 		    }
   7267 		  else
   7268 		    {
   7269 		      defaux.vda_name = n->version_needed->name_indx;
   7270 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7271 					      defaux.vda_name);
   7272 		    }
   7273 		  if (n->next == NULL)
   7274 		    defaux.vda_next = 0;
   7275 		  else
   7276 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   7277 
   7278 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7279 					     (Elf_External_Verdaux *) p);
   7280 		  p += sizeof (Elf_External_Verdaux);
   7281 		}
   7282 	    }
   7283 
   7284 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   7285 	}
   7286     }
   7287 
   7288   if (info->gc_sections && bed->can_gc_sections)
   7289     {
   7290       struct elf_gc_sweep_symbol_info sweep_info;
   7291 
   7292       /* Remove the symbols that were in the swept sections from the
   7293 	 dynamic symbol table.  */
   7294       sweep_info.info = info;
   7295       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   7296       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   7297 			      &sweep_info);
   7298     }
   7299 
   7300   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7301     {
   7302       asection *s;
   7303       struct elf_find_verdep_info sinfo;
   7304 
   7305       /* Work out the size of the version reference section.  */
   7306 
   7307       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   7308       BFD_ASSERT (s != NULL);
   7309 
   7310       sinfo.info = info;
   7311       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   7312       if (sinfo.vers == 0)
   7313 	sinfo.vers = 1;
   7314       sinfo.failed = false;
   7315 
   7316       elf_link_hash_traverse (elf_hash_table (info),
   7317 			      _bfd_elf_link_find_version_dependencies,
   7318 			      &sinfo);
   7319       if (sinfo.failed)
   7320 	return false;
   7321 
   7322       bed->elf_backend_add_glibc_version_dependency (&sinfo);
   7323       if (sinfo.failed)
   7324 	return false;
   7325 
   7326       if (elf_tdata (output_bfd)->verref == NULL)
   7327 	s->flags |= SEC_EXCLUDE;
   7328       else
   7329 	{
   7330 	  Elf_Internal_Verneed *vn;
   7331 	  unsigned int size;
   7332 	  unsigned int crefs;
   7333 	  bfd_byte *p;
   7334 
   7335 	  /* Build the version dependency section.  */
   7336 	  size = 0;
   7337 	  crefs = 0;
   7338 	  for (vn = elf_tdata (output_bfd)->verref;
   7339 	       vn != NULL;
   7340 	       vn = vn->vn_nextref)
   7341 	    {
   7342 	      Elf_Internal_Vernaux *a;
   7343 
   7344 	      size += sizeof (Elf_External_Verneed);
   7345 	      ++crefs;
   7346 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7347 		size += sizeof (Elf_External_Vernaux);
   7348 	    }
   7349 
   7350 	  s->size = size;
   7351 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7352 	  if (s->contents == NULL)
   7353 	    return false;
   7354 	  s->alloced = 1;
   7355 
   7356 	  p = s->contents;
   7357 	  for (vn = elf_tdata (output_bfd)->verref;
   7358 	       vn != NULL;
   7359 	       vn = vn->vn_nextref)
   7360 	    {
   7361 	      unsigned int caux;
   7362 	      Elf_Internal_Vernaux *a;
   7363 	      size_t indx;
   7364 
   7365 	      caux = 0;
   7366 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7367 		++caux;
   7368 
   7369 	      vn->vn_version = VER_NEED_CURRENT;
   7370 	      vn->vn_cnt = caux;
   7371 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7372 					  elf_dt_name (vn->vn_bfd) != NULL
   7373 					  ? elf_dt_name (vn->vn_bfd)
   7374 					  : lbasename (bfd_get_filename
   7375 						       (vn->vn_bfd)),
   7376 					  false);
   7377 	      if (indx == (size_t) -1)
   7378 		return false;
   7379 	      vn->vn_file = indx;
   7380 	      vn->vn_aux = sizeof (Elf_External_Verneed);
   7381 	      if (vn->vn_nextref == NULL)
   7382 		vn->vn_next = 0;
   7383 	      else
   7384 		vn->vn_next = (sizeof (Elf_External_Verneed)
   7385 			       + caux * sizeof (Elf_External_Vernaux));
   7386 
   7387 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
   7388 					 (Elf_External_Verneed *) p);
   7389 	      p += sizeof (Elf_External_Verneed);
   7390 
   7391 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7392 		{
   7393 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
   7394 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7395 					      a->vna_nodename, false);
   7396 		  if (indx == (size_t) -1)
   7397 		    return false;
   7398 		  a->vna_name = indx;
   7399 		  if (a->vna_nextptr == NULL)
   7400 		    a->vna_next = 0;
   7401 		  else
   7402 		    a->vna_next = sizeof (Elf_External_Vernaux);
   7403 
   7404 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
   7405 					     (Elf_External_Vernaux *) p);
   7406 		  p += sizeof (Elf_External_Vernaux);
   7407 		}
   7408 	    }
   7409 
   7410 	  elf_tdata (output_bfd)->cverrefs = crefs;
   7411 	}
   7412     }
   7413 
   7414   if (bfd_link_relocatable (info)
   7415       && !_bfd_elf_size_group_sections (info))
   7416     return false;
   7417 
   7418   /* Determine any GNU_STACK segment requirements, after the backend
   7419      has had a chance to set a default segment size.  */
   7420   if (info->execstack)
   7421     {
   7422       /* If the user has explicitly requested warnings, then generate one even
   7423 	 though the choice is the result of another command line option.  */
   7424       if (info->warn_execstack == 1)
   7425 	{
   7426 	  if (info->error_execstack)
   7427 	    {
   7428 	      _bfd_error_handler
   7429 		(_("\
   7430 error: creating an executable stack because of -z execstack command line option"));
   7431 	      return false;
   7432 	    }
   7433 
   7434 	  _bfd_error_handler
   7435 	    (_("\
   7436 warning: enabling an executable stack because of -z execstack command line option"));
   7437 	}
   7438 
   7439       elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   7440     }
   7441   else if (info->noexecstack)
   7442     elf_stack_flags (output_bfd) = PF_R | PF_W;
   7443   else
   7444     {
   7445       bfd *inputobj;
   7446       asection *notesec = NULL;
   7447       bfd *noteobj = NULL;
   7448       bfd *emptyobj = NULL;
   7449       int exec = 0;
   7450 
   7451       for (inputobj = info->input_bfds;
   7452 	   inputobj;
   7453 	   inputobj = inputobj->link.next)
   7454 	{
   7455 	  asection *s;
   7456 
   7457 	  if (inputobj->flags
   7458 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   7459 	    continue;
   7460 	  s = inputobj->sections;
   7461 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7462 	    continue;
   7463 
   7464 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   7465 	  if (s)
   7466 	    {
   7467 	      notesec = s;
   7468 	      if (s->flags & SEC_CODE)
   7469 		{
   7470 		  noteobj = inputobj;
   7471 		  exec = PF_X;
   7472 		  /* There is no point in scanning the remaining bfds.  */
   7473 		  break;
   7474 		}
   7475 	    }
   7476 	  else if (bed->default_execstack && info->default_execstack)
   7477 	    {
   7478 	      exec = PF_X;
   7479 	      emptyobj = inputobj;
   7480 	    }
   7481 	}
   7482 
   7483       if (notesec || info->stacksize > 0)
   7484 	{
   7485 	  if (exec)
   7486 	    {
   7487 	      if (info->warn_execstack != 0)
   7488 		{
   7489 		  /* PR 29072: Because an executable stack is a serious
   7490 		     security risk, make sure that the user knows that it is
   7491 		     being enabled despite the fact that it was not requested
   7492 		     on the command line.  */
   7493 		  if (noteobj)
   7494 		    {
   7495 		      if (info->error_execstack)
   7496 			{
   7497 			  _bfd_error_handler (_("\
   7498 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
   7499 					      bfd_get_filename (noteobj));
   7500 			  return false;
   7501 			}
   7502 
   7503 		      _bfd_error_handler (_("\
   7504 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
   7505 		       bfd_get_filename (noteobj));
   7506 		    }
   7507 		  else if (emptyobj)
   7508 		    {
   7509 		      if (info->error_execstack)
   7510 			{
   7511 			  _bfd_error_handler (_("\
   7512 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
   7513 					      bfd_get_filename (emptyobj));
   7514 			  return false;
   7515 			}
   7516 
   7517 		      _bfd_error_handler (_("\
   7518 warning: %s: missing .note.GNU-stack section implies executable stack"),
   7519 					  bfd_get_filename (emptyobj));
   7520 		      _bfd_error_handler (_("\
   7521 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
   7522 		    }
   7523 		}
   7524 	    }
   7525 	  elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   7526 	}
   7527 
   7528       if (notesec && exec && bfd_link_relocatable (info)
   7529 	  && notesec->output_section != bfd_abs_section_ptr)
   7530 	notesec->output_section->flags |= SEC_CODE;
   7531     }
   7532 
   7533   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7534     {
   7535       struct elf_info_failed eif;
   7536       struct elf_link_hash_entry *h;
   7537       asection *dynstr;
   7538       asection *s;
   7539 
   7540       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   7541       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   7542 
   7543       if (info->symbolic)
   7544 	{
   7545 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   7546 	    return false;
   7547 	  info->flags |= DF_SYMBOLIC;
   7548 	}
   7549 
   7550       if (rpath != NULL)
   7551 	{
   7552 	  size_t indx;
   7553 	  bfd_vma tag;
   7554 
   7555 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   7556 				      true);
   7557 	  if (indx == (size_t) -1)
   7558 	    return false;
   7559 
   7560 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   7561 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   7562 	    return false;
   7563 	}
   7564 
   7565       if (filter_shlib != NULL)
   7566 	{
   7567 	  size_t indx;
   7568 
   7569 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7570 				      filter_shlib, true);
   7571 	  if (indx == (size_t) -1
   7572 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   7573 	    return false;
   7574 	}
   7575 
   7576       if (auxiliary_filters != NULL)
   7577 	{
   7578 	  const char * const *p;
   7579 
   7580 	  for (p = auxiliary_filters; *p != NULL; p++)
   7581 	    {
   7582 	      size_t indx;
   7583 
   7584 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7585 					  *p, true);
   7586 	      if (indx == (size_t) -1
   7587 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   7588 		return false;
   7589 	    }
   7590 	}
   7591 
   7592       if (audit != NULL)
   7593 	{
   7594 	  size_t indx;
   7595 
   7596 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   7597 				      true);
   7598 	  if (indx == (size_t) -1
   7599 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   7600 	    return false;
   7601 	}
   7602 
   7603       if (depaudit != NULL)
   7604 	{
   7605 	  size_t indx;
   7606 
   7607 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   7608 				      true);
   7609 	  if (indx == (size_t) -1
   7610 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   7611 	    return false;
   7612 	}
   7613 
   7614       eif.info = info;
   7615       eif.failed = false;
   7616 
   7617       /* Find all symbols which were defined in a dynamic object and make
   7618 	 the backend pick a reasonable value for them.  */
   7619       elf_link_hash_traverse (elf_hash_table (info),
   7620 			      _bfd_elf_adjust_dynamic_symbol,
   7621 			      &eif);
   7622       if (eif.failed)
   7623 	return false;
   7624 
   7625       /* Add some entries to the .dynamic section.  We fill in some of the
   7626 	 values later, in bfd_elf_final_link, but we must add the entries
   7627 	 now so that we know the final size of the .dynamic section.  */
   7628 
   7629       /* If there are initialization and/or finalization functions to
   7630 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   7631       h = (info->init_function
   7632 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7633 				   info->init_function, false,
   7634 				   false, false)
   7635 	   : NULL);
   7636       if (h != NULL
   7637 	  && (h->ref_regular
   7638 	      || h->def_regular))
   7639 	{
   7640 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   7641 	    return false;
   7642 	}
   7643       h = (info->fini_function
   7644 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7645 				   info->fini_function, false,
   7646 				   false, false)
   7647 	   : NULL);
   7648       if (h != NULL
   7649 	  && (h->ref_regular
   7650 	      || h->def_regular))
   7651 	{
   7652 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   7653 	    return false;
   7654 	}
   7655 
   7656       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   7657       if (s != NULL && s->linker_has_input)
   7658 	{
   7659 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   7660 	  if (! bfd_link_executable (info))
   7661 	    {
   7662 	      bfd *sub;
   7663 	      asection *o;
   7664 
   7665 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   7666 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   7667 		    && (o = sub->sections) != NULL
   7668 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
   7669 		  for (o = sub->sections; o != NULL; o = o->next)
   7670 		    if (elf_section_data (o)->this_hdr.sh_type
   7671 			== SHT_PREINIT_ARRAY)
   7672 		      {
   7673 			_bfd_error_handler
   7674 			  (_("%pB: .preinit_array section is not allowed in DSO"),
   7675 			   sub);
   7676 			break;
   7677 		      }
   7678 
   7679 	      bfd_set_error (bfd_error_nonrepresentable_section);
   7680 	      return false;
   7681 	    }
   7682 
   7683 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   7684 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   7685 	    return false;
   7686 	}
   7687       s = bfd_get_section_by_name (output_bfd, ".init_array");
   7688       if (s != NULL && s->linker_has_input)
   7689 	{
   7690 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   7691 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   7692 	    return false;
   7693 	}
   7694       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   7695       if (s != NULL && s->linker_has_input)
   7696 	{
   7697 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   7698 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   7699 	    return false;
   7700 	}
   7701 
   7702       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   7703       /* If .dynstr is excluded from the link, we don't want any of
   7704 	 these tags.  Strictly, we should be checking each section
   7705 	 individually;  This quick check covers for the case where
   7706 	 someone does a /DISCARD/ : { *(*) }.  */
   7707       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   7708 	{
   7709 	  bfd_size_type strsize;
   7710 
   7711 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7712 	  if ((info->emit_hash
   7713 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   7714 	      || (info->emit_gnu_hash
   7715 		  && (bed->record_xhash_symbol == NULL
   7716 		      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
   7717 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   7718 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   7719 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   7720 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   7721 					      bed->s->sizeof_sym)
   7722 	      || (info->gnu_flags_1
   7723 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
   7724 						  info->gnu_flags_1)))
   7725 	    return false;
   7726 	}
   7727     }
   7728 
   7729   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   7730     return false;
   7731 
   7732   /* The backend must work out the sizes of all the other dynamic
   7733      sections.  */
   7734   if (bed->elf_backend_late_size_sections != NULL
   7735       && !bed->elf_backend_late_size_sections (output_bfd, info))
   7736     return false;
   7737 
   7738   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7739     {
   7740       if (elf_tdata (output_bfd)->cverdefs)
   7741 	{
   7742 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
   7743 
   7744 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   7745 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
   7746 	    return false;
   7747 	}
   7748 
   7749       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   7750 	{
   7751 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   7752 	    return false;
   7753 	}
   7754       else if (info->flags & DF_BIND_NOW)
   7755 	{
   7756 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   7757 	    return false;
   7758 	}
   7759 
   7760       if (info->flags_1)
   7761 	{
   7762 	  if (bfd_link_executable (info))
   7763 	    info->flags_1 &= ~ (DF_1_INITFIRST
   7764 				| DF_1_NODELETE
   7765 				| DF_1_NOOPEN);
   7766 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   7767 	    return false;
   7768 	}
   7769 
   7770       if (elf_tdata (output_bfd)->cverrefs)
   7771 	{
   7772 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
   7773 
   7774 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   7775 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   7776 	    return false;
   7777 	}
   7778 
   7779       if ((elf_tdata (output_bfd)->cverrefs == 0
   7780 	   && elf_tdata (output_bfd)->cverdefs == 0)
   7781 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
   7782 	{
   7783 	  asection *s;
   7784 
   7785 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   7786 	  s->flags |= SEC_EXCLUDE;
   7787 	}
   7788     }
   7789   return true;
   7790 }
   7791 
   7792 /* Find the first non-excluded output section.  We'll use its
   7793    section symbol for some emitted relocs.  */
   7794 void
   7795 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   7796 {
   7797   asection *s;
   7798   asection *found = NULL;
   7799 
   7800   for (s = output_bfd->sections; s != NULL; s = s->next)
   7801     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7802 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7803       {
   7804 	found = s;
   7805 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7806 	  break;
   7807       }
   7808   elf_hash_table (info)->text_index_section = found;
   7809 }
   7810 
   7811 /* Find two non-excluded output sections, one for code, one for data.
   7812    We'll use their section symbols for some emitted relocs.  */
   7813 void
   7814 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   7815 {
   7816   asection *s;
   7817   asection *found = NULL;
   7818 
   7819   /* Data first, since setting text_index_section changes
   7820      _bfd_elf_omit_section_dynsym_default.  */
   7821   for (s = output_bfd->sections; s != NULL; s = s->next)
   7822     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7823 	&& !(s->flags & SEC_READONLY)
   7824 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7825       {
   7826 	found = s;
   7827 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7828 	  break;
   7829       }
   7830   elf_hash_table (info)->data_index_section = found;
   7831 
   7832   for (s = output_bfd->sections; s != NULL; s = s->next)
   7833     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7834 	&& (s->flags & SEC_READONLY)
   7835 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7836       {
   7837 	found = s;
   7838 	break;
   7839       }
   7840   elf_hash_table (info)->text_index_section = found;
   7841 }
   7842 
   7843 #define GNU_HASH_SECTION_NAME(bed)			    \
   7844   (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
   7845 
   7846 bool
   7847 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   7848 {
   7849   const struct elf_backend_data *bed;
   7850   unsigned long section_sym_count;
   7851   bfd_size_type dynsymcount = 0;
   7852 
   7853   if (!is_elf_hash_table (info->hash))
   7854     return true;
   7855 
   7856   bed = get_elf_backend_data (output_bfd);
   7857   (*bed->elf_backend_init_index_section) (output_bfd, info);
   7858 
   7859   /* Assign dynsym indices.  In a shared library we generate a section
   7860      symbol for each output section, which come first.  Next come all
   7861      of the back-end allocated local dynamic syms, followed by the rest
   7862      of the global symbols.
   7863 
   7864      This is usually not needed for static binaries, however backends
   7865      can request to always do it, e.g. the MIPS backend uses dynamic
   7866      symbol counts to lay out GOT, which will be produced in the
   7867      presence of GOT relocations even in static binaries (holding fixed
   7868      data in that case, to satisfy those relocations).  */
   7869 
   7870   if (elf_hash_table (info)->dynamic_sections_created
   7871       || bed->always_renumber_dynsyms)
   7872     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   7873 						  &section_sym_count);
   7874 
   7875   if (elf_hash_table (info)->dynamic_sections_created)
   7876     {
   7877       bfd *dynobj;
   7878       asection *s;
   7879       unsigned int dtagcount;
   7880 
   7881       dynobj = elf_hash_table (info)->dynobj;
   7882 
   7883       /* Work out the size of the symbol version section.  */
   7884       s = bfd_get_linker_section (dynobj, ".gnu.version");
   7885       BFD_ASSERT (s != NULL);
   7886       if ((s->flags & SEC_EXCLUDE) == 0)
   7887 	{
   7888 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   7889 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7890 	  if (s->contents == NULL)
   7891 	    return false;
   7892 	  s->alloced = 1;
   7893 
   7894 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   7895 	    return false;
   7896 	}
   7897 
   7898       /* Set the size of the .dynsym and .hash sections.  We counted
   7899 	 the number of dynamic symbols in elf_link_add_object_symbols.
   7900 	 We will build the contents of .dynsym and .hash when we build
   7901 	 the final symbol table, because until then we do not know the
   7902 	 correct value to give the symbols.  We built the .dynstr
   7903 	 section as we went along in elf_link_add_object_symbols.  */
   7904       s = elf_hash_table (info)->dynsym;
   7905       BFD_ASSERT (s != NULL);
   7906       s->size = dynsymcount * bed->s->sizeof_sym;
   7907 
   7908       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7909       if (s->contents == NULL)
   7910 	return false;
   7911       s->alloced = 1;
   7912 
   7913       /* The first entry in .dynsym is a dummy symbol.  Clear all the
   7914 	 section syms, in case we don't output them all.  */
   7915       ++section_sym_count;
   7916       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   7917 
   7918       elf_hash_table (info)->bucketcount = 0;
   7919 
   7920       /* Compute the size of the hashing table.  As a side effect this
   7921 	 computes the hash values for all the names we export.  */
   7922       if (info->emit_hash)
   7923 	{
   7924 	  unsigned long int *hashcodes;
   7925 	  struct hash_codes_info hashinf;
   7926 	  bfd_size_type amt;
   7927 	  unsigned long int nsyms;
   7928 	  size_t bucketcount;
   7929 	  size_t hash_entry_size;
   7930 
   7931 	  /* Compute the hash values for all exported symbols.  At the same
   7932 	     time store the values in an array so that we could use them for
   7933 	     optimizations.  */
   7934 	  amt = dynsymcount * sizeof (unsigned long int);
   7935 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   7936 	  if (hashcodes == NULL)
   7937 	    return false;
   7938 	  hashinf.hashcodes = hashcodes;
   7939 	  hashinf.error = false;
   7940 
   7941 	  /* Put all hash values in HASHCODES.  */
   7942 	  elf_link_hash_traverse (elf_hash_table (info),
   7943 				  elf_collect_hash_codes, &hashinf);
   7944 	  if (hashinf.error)
   7945 	    {
   7946 	      free (hashcodes);
   7947 	      return false;
   7948 	    }
   7949 
   7950 	  nsyms = hashinf.hashcodes - hashcodes;
   7951 	  bucketcount
   7952 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   7953 	  free (hashcodes);
   7954 
   7955 	  if (bucketcount == 0 && nsyms > 0)
   7956 	    return false;
   7957 
   7958 	  elf_hash_table (info)->bucketcount = bucketcount;
   7959 
   7960 	  s = bfd_get_linker_section (dynobj, ".hash");
   7961 	  BFD_ASSERT (s != NULL);
   7962 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   7963 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   7964 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7965 	  if (s->contents == NULL)
   7966 	    return false;
   7967 	  s->alloced = 1;
   7968 
   7969 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   7970 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   7971 		   s->contents + hash_entry_size);
   7972 	}
   7973 
   7974       if (info->emit_gnu_hash)
   7975 	{
   7976 	  size_t i, cnt;
   7977 	  unsigned char *contents;
   7978 	  struct collect_gnu_hash_codes cinfo;
   7979 	  bfd_size_type amt;
   7980 	  size_t bucketcount;
   7981 
   7982 	  memset (&cinfo, 0, sizeof (cinfo));
   7983 
   7984 	  /* Compute the hash values for all exported symbols.  At the same
   7985 	     time store the values in an array so that we could use them for
   7986 	     optimizations.  */
   7987 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   7988 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   7989 	  if (cinfo.hashcodes == NULL)
   7990 	    return false;
   7991 
   7992 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   7993 	  cinfo.min_dynindx = -1;
   7994 	  cinfo.output_bfd = output_bfd;
   7995 	  cinfo.bed = bed;
   7996 
   7997 	  /* Put all hash values in HASHCODES.  */
   7998 	  elf_link_hash_traverse (elf_hash_table (info),
   7999 				  elf_collect_gnu_hash_codes, &cinfo);
   8000 	  if (cinfo.error)
   8001 	    {
   8002 	      free (cinfo.hashcodes);
   8003 	      return false;
   8004 	    }
   8005 
   8006 	  bucketcount
   8007 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   8008 
   8009 	  if (bucketcount == 0)
   8010 	    {
   8011 	      free (cinfo.hashcodes);
   8012 	      return false;
   8013 	    }
   8014 
   8015 	  s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
   8016 	  BFD_ASSERT (s != NULL);
   8017 
   8018 	  if (cinfo.nsyms == 0)
   8019 	    {
   8020 	      /* Empty .gnu.hash or .MIPS.xhash section is special.  */
   8021 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   8022 	      free (cinfo.hashcodes);
   8023 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   8024 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   8025 	      if (contents == NULL)
   8026 		return false;
   8027 	      s->contents = contents;
   8028 	      s->alloced = 1;
   8029 	      /* 1 empty bucket.  */
   8030 	      bfd_put_32 (output_bfd, 1, contents);
   8031 	      /* SYMIDX above the special symbol 0.  */
   8032 	      bfd_put_32 (output_bfd, 1, contents + 4);
   8033 	      /* Just one word for bitmask.  */
   8034 	      bfd_put_32 (output_bfd, 1, contents + 8);
   8035 	      /* Only hash fn bloom filter.  */
   8036 	      bfd_put_32 (output_bfd, 0, contents + 12);
   8037 	      /* No hashes are valid - empty bitmask.  */
   8038 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   8039 	      /* No hashes in the only bucket.  */
   8040 	      bfd_put_32 (output_bfd, 0,
   8041 			  contents + 16 + bed->s->arch_size / 8);
   8042 	    }
   8043 	  else
   8044 	    {
   8045 	      unsigned long int maskwords, maskbitslog2, x;
   8046 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   8047 
   8048 	      x = cinfo.nsyms;
   8049 	      maskbitslog2 = 1;
   8050 	      while ((x >>= 1) != 0)
   8051 		++maskbitslog2;
   8052 	      if (maskbitslog2 < 3)
   8053 		maskbitslog2 = 5;
   8054 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   8055 		maskbitslog2 = maskbitslog2 + 3;
   8056 	      else
   8057 		maskbitslog2 = maskbitslog2 + 2;
   8058 	      if (bed->s->arch_size == 64)
   8059 		{
   8060 		  if (maskbitslog2 == 5)
   8061 		    maskbitslog2 = 6;
   8062 		  cinfo.shift1 = 6;
   8063 		}
   8064 	      else
   8065 		cinfo.shift1 = 5;
   8066 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   8067 	      cinfo.shift2 = maskbitslog2;
   8068 	      cinfo.maskbits = 1 << maskbitslog2;
   8069 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   8070 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   8071 	      amt += maskwords * sizeof (bfd_vma);
   8072 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   8073 	      if (cinfo.bitmask == NULL)
   8074 		{
   8075 		  free (cinfo.hashcodes);
   8076 		  return false;
   8077 		}
   8078 
   8079 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   8080 	      cinfo.indx = cinfo.counts + bucketcount;
   8081 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   8082 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   8083 
   8084 	      /* Determine how often each hash bucket is used.  */
   8085 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   8086 	      for (i = 0; i < cinfo.nsyms; ++i)
   8087 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   8088 
   8089 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   8090 		if (cinfo.counts[i] != 0)
   8091 		  {
   8092 		    cinfo.indx[i] = cnt;
   8093 		    cnt += cinfo.counts[i];
   8094 		  }
   8095 	      BFD_ASSERT (cnt == dynsymcount);
   8096 	      cinfo.bucketcount = bucketcount;
   8097 	      cinfo.local_indx = cinfo.min_dynindx;
   8098 
   8099 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   8100 	      s->size += cinfo.maskbits / 8;
   8101 	      if (bed->record_xhash_symbol != NULL)
   8102 		s->size += cinfo.nsyms * 4;
   8103 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   8104 	      if (contents == NULL)
   8105 		{
   8106 		  free (cinfo.bitmask);
   8107 		  free (cinfo.hashcodes);
   8108 		  return false;
   8109 		}
   8110 
   8111 	      s->contents = contents;
   8112 	      s->alloced = 1;
   8113 	      bfd_put_32 (output_bfd, bucketcount, contents);
   8114 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   8115 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   8116 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   8117 	      contents += 16 + cinfo.maskbits / 8;
   8118 
   8119 	      for (i = 0; i < bucketcount; ++i)
   8120 		{
   8121 		  if (cinfo.counts[i] == 0)
   8122 		    bfd_put_32 (output_bfd, 0, contents);
   8123 		  else
   8124 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   8125 		  contents += 4;
   8126 		}
   8127 
   8128 	      cinfo.contents = contents;
   8129 
   8130 	      cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
   8131 	      /* Renumber dynamic symbols, if populating .gnu.hash section.
   8132 		 If using .MIPS.xhash, populate the translation table.  */
   8133 	      elf_link_hash_traverse (elf_hash_table (info),
   8134 				      elf_gnu_hash_process_symidx, &cinfo);
   8135 
   8136 	      contents = s->contents + 16;
   8137 	      for (i = 0; i < maskwords; ++i)
   8138 		{
   8139 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   8140 			   contents);
   8141 		  contents += bed->s->arch_size / 8;
   8142 		}
   8143 
   8144 	      free (cinfo.bitmask);
   8145 	      free (cinfo.hashcodes);
   8146 	    }
   8147 	}
   8148 
   8149       s = bfd_get_linker_section (dynobj, ".dynstr");
   8150       BFD_ASSERT (s != NULL);
   8151 
   8152       elf_finalize_dynstr (output_bfd, info);
   8153 
   8154       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   8155 
   8156       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   8157 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   8158 	  return false;
   8159     }
   8160 
   8161   return true;
   8162 }
   8163 
   8164 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   8166 
   8167 static void
   8168 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   8169 			    asection *sec)
   8170 {
   8171   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   8172   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   8173 }
   8174 
   8175 /* Finish SHF_MERGE section merging.  */
   8176 
   8177 bool
   8178 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
   8179 {
   8180   bfd *ibfd;
   8181   asection *sec;
   8182 
   8183   if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
   8184     abort ();
   8185 
   8186   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8187     if ((ibfd->flags & DYNAMIC) == 0
   8188 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   8189 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   8190 	    == get_elf_backend_data (obfd)->s->elfclass))
   8191       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8192 	if ((sec->flags & SEC_MERGE) != 0
   8193 	    && !bfd_is_abs_section (sec->output_section))
   8194 	  {
   8195 	    struct bfd_elf_section_data *secdata;
   8196 
   8197 	    secdata = elf_section_data (sec);
   8198 	    if (! _bfd_add_merge_section (obfd,
   8199 					  &elf_hash_table (info)->merge_info,
   8200 					  sec, &secdata->sec_info))
   8201 	      return false;
   8202 	    else if (secdata->sec_info)
   8203 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   8204 	  }
   8205 
   8206   if (elf_hash_table (info)->merge_info != NULL)
   8207     return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
   8208 				merge_sections_remove_hook);
   8209   return true;
   8210 }
   8211 
   8212 /* Create an entry in an ELF linker hash table.  */
   8213 
   8214 struct bfd_hash_entry *
   8215 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   8216 			    struct bfd_hash_table *table,
   8217 			    const char *string)
   8218 {
   8219   /* Allocate the structure if it has not already been allocated by a
   8220      subclass.  */
   8221   if (entry == NULL)
   8222     {
   8223       entry = (struct bfd_hash_entry *)
   8224 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   8225       if (entry == NULL)
   8226 	return entry;
   8227     }
   8228 
   8229   /* Call the allocation method of the superclass.  */
   8230   entry = _bfd_link_hash_newfunc (entry, table, string);
   8231   if (entry != NULL)
   8232     {
   8233       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   8234       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   8235 
   8236       /* Set local fields.  */
   8237       ret->indx = -1;
   8238       ret->dynindx = -1;
   8239       ret->got = htab->init_got_refcount;
   8240       ret->plt = htab->init_plt_refcount;
   8241       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   8242 			      - offsetof (struct elf_link_hash_entry, size)));
   8243       /* Assume that we have been called by a non-ELF symbol reader.
   8244 	 This flag is then reset by the code which reads an ELF input
   8245 	 file.  This ensures that a symbol created by a non-ELF symbol
   8246 	 reader will have the flag set correctly.  */
   8247       ret->non_elf = 1;
   8248     }
   8249 
   8250   return entry;
   8251 }
   8252 
   8253 /* Copy data from an indirect symbol to its direct symbol, hiding the
   8254    old indirect symbol.  Also used for copying flags to a weakdef.  */
   8255 
   8256 void
   8257 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   8258 				  struct elf_link_hash_entry *dir,
   8259 				  struct elf_link_hash_entry *ind)
   8260 {
   8261   struct elf_link_hash_table *htab;
   8262 
   8263   if (ind->dyn_relocs != NULL)
   8264     {
   8265       if (dir->dyn_relocs != NULL)
   8266 	{
   8267 	  struct elf_dyn_relocs **pp;
   8268 	  struct elf_dyn_relocs *p;
   8269 
   8270 	  /* Add reloc counts against the indirect sym to the direct sym
   8271 	     list.  Merge any entries against the same section.  */
   8272 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   8273 	    {
   8274 	      struct elf_dyn_relocs *q;
   8275 
   8276 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   8277 		if (q->sec == p->sec)
   8278 		  {
   8279 		    q->pc_count += p->pc_count;
   8280 		    q->count += p->count;
   8281 		    *pp = p->next;
   8282 		    break;
   8283 		  }
   8284 	      if (q == NULL)
   8285 		pp = &p->next;
   8286 	    }
   8287 	  *pp = dir->dyn_relocs;
   8288 	}
   8289 
   8290       dir->dyn_relocs = ind->dyn_relocs;
   8291       ind->dyn_relocs = NULL;
   8292     }
   8293 
   8294   /* Copy down any references that we may have already seen to the
   8295      symbol which just became indirect.  */
   8296 
   8297   if (dir->versioned != versioned_hidden)
   8298     dir->ref_dynamic |= ind->ref_dynamic;
   8299   dir->ref_regular |= ind->ref_regular;
   8300   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   8301   dir->non_got_ref |= ind->non_got_ref;
   8302   dir->needs_plt |= ind->needs_plt;
   8303   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   8304 
   8305   if (ind->root.type != bfd_link_hash_indirect)
   8306     return;
   8307 
   8308   /* Copy over the global and procedure linkage table refcount entries.
   8309      These may have been already set up by a check_relocs routine.  */
   8310   htab = elf_hash_table (info);
   8311   if (ind->got.refcount > htab->init_got_refcount.refcount)
   8312     {
   8313       if (dir->got.refcount < 0)
   8314 	dir->got.refcount = 0;
   8315       dir->got.refcount += ind->got.refcount;
   8316       ind->got.refcount = htab->init_got_refcount.refcount;
   8317     }
   8318 
   8319   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   8320     {
   8321       if (dir->plt.refcount < 0)
   8322 	dir->plt.refcount = 0;
   8323       dir->plt.refcount += ind->plt.refcount;
   8324       ind->plt.refcount = htab->init_plt_refcount.refcount;
   8325     }
   8326 
   8327   if (ind->dynindx != -1)
   8328     {
   8329       if (dir->dynindx != -1)
   8330 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   8331       dir->dynindx = ind->dynindx;
   8332       dir->dynstr_index = ind->dynstr_index;
   8333       ind->dynindx = -1;
   8334       ind->dynstr_index = 0;
   8335     }
   8336 }
   8337 
   8338 void
   8339 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   8340 				struct elf_link_hash_entry *h,
   8341 				bool force_local)
   8342 {
   8343   /* STT_GNU_IFUNC symbol must go through PLT.  */
   8344   if (h->type != STT_GNU_IFUNC)
   8345     {
   8346       h->plt = elf_hash_table (info)->init_plt_offset;
   8347       h->needs_plt = 0;
   8348     }
   8349   if (force_local)
   8350     {
   8351       h->forced_local = 1;
   8352       if (h->dynindx != -1)
   8353 	{
   8354 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8355 				  h->dynstr_index);
   8356 	  h->dynindx = -1;
   8357 	  h->dynstr_index = 0;
   8358 	}
   8359     }
   8360 }
   8361 
   8362 /* Hide a symbol. */
   8363 
   8364 void
   8365 _bfd_elf_link_hide_symbol (bfd *output_bfd,
   8366 			   struct bfd_link_info *info,
   8367 			   struct bfd_link_hash_entry *h)
   8368 {
   8369   if (is_elf_hash_table (info->hash))
   8370     {
   8371       const struct elf_backend_data *bed
   8372 	= get_elf_backend_data (output_bfd);
   8373       struct elf_link_hash_entry *eh
   8374 	= (struct elf_link_hash_entry *) h;
   8375       bed->elf_backend_hide_symbol (info, eh, true);
   8376       eh->def_dynamic = 0;
   8377       eh->ref_dynamic = 0;
   8378       eh->dynamic_def = 0;
   8379     }
   8380 }
   8381 
   8382 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   8383    caller.  */
   8384 
   8385 bool
   8386 _bfd_elf_link_hash_table_init
   8387   (struct elf_link_hash_table *table,
   8388    bfd *abfd,
   8389    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   8390 				      struct bfd_hash_table *,
   8391 				      const char *),
   8392    unsigned int entsize)
   8393 {
   8394   bool ret;
   8395   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8396   int can_refcount = bed->can_refcount;
   8397 
   8398   table->init_got_refcount.refcount = can_refcount - 1;
   8399   table->init_plt_refcount.refcount = can_refcount - 1;
   8400   table->init_got_offset.offset = -(bfd_vma) 1;
   8401   table->init_plt_offset.offset = -(bfd_vma) 1;
   8402   /* The first dynamic symbol is a dummy.  */
   8403   table->dynsymcount = 1;
   8404 
   8405   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   8406 
   8407   table->root.type = bfd_link_elf_hash_table;
   8408   table->hash_table_id = bed->target_id;
   8409   table->target_os = bed->target_os;
   8410   table->root.hash_table_free = _bfd_elf_link_hash_table_free;
   8411 
   8412   return ret;
   8413 }
   8414 
   8415 /* Create an ELF linker hash table.  */
   8416 
   8417 struct bfd_link_hash_table *
   8418 _bfd_elf_link_hash_table_create (bfd *abfd)
   8419 {
   8420   struct elf_link_hash_table *ret;
   8421   size_t amt = sizeof (struct elf_link_hash_table);
   8422 
   8423   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   8424   if (ret == NULL)
   8425     return NULL;
   8426 
   8427   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   8428 				       sizeof (struct elf_link_hash_entry)))
   8429     {
   8430       free (ret);
   8431       return NULL;
   8432     }
   8433 
   8434   return &ret->root;
   8435 }
   8436 
   8437 /* Destroy an ELF linker hash table.  */
   8438 
   8439 void
   8440 _bfd_elf_link_hash_table_free (bfd *obfd)
   8441 {
   8442   struct elf_link_hash_table *htab;
   8443 
   8444   htab = (struct elf_link_hash_table *) obfd->link.hash;
   8445   if (htab->dynstr != NULL)
   8446     _bfd_elf_strtab_free (htab->dynstr);
   8447   _bfd_merge_sections_free (htab->merge_info);
   8448   /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
   8449   if (htab->dynamic != NULL)
   8450     {
   8451       free (htab->dynamic->contents);
   8452       htab->dynamic->contents = NULL;
   8453     }
   8454   if (htab->first_hash != NULL)
   8455     {
   8456       bfd_hash_table_free (htab->first_hash);
   8457       free (htab->first_hash);
   8458     }
   8459   if (htab->eh_info.frame_hdr_is_compact)
   8460     free (htab->eh_info.u.compact.entries);
   8461   else
   8462     free (htab->eh_info.u.dwarf.array);
   8463   _bfd_generic_link_hash_table_free (obfd);
   8464 }
   8465 
   8466 /* This is a hook for the ELF emulation code in the generic linker to
   8467    tell the backend linker what file name to use for the DT_NEEDED
   8468    entry for a dynamic object.  */
   8469 
   8470 void
   8471 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   8472 {
   8473   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8474       && bfd_get_format (abfd) == bfd_object)
   8475     elf_dt_name (abfd) = name;
   8476 }
   8477 
   8478 int
   8479 bfd_elf_get_dyn_lib_class (bfd *abfd)
   8480 {
   8481   int lib_class;
   8482   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8483       && bfd_get_format (abfd) == bfd_object)
   8484     lib_class = elf_dyn_lib_class (abfd);
   8485   else
   8486     lib_class = 0;
   8487   return lib_class;
   8488 }
   8489 
   8490 void
   8491 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   8492 {
   8493   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8494       && bfd_get_format (abfd) == bfd_object)
   8495     elf_dyn_lib_class (abfd) = lib_class;
   8496 }
   8497 
   8498 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   8499    the linker ELF emulation code.  */
   8500 
   8501 struct bfd_link_needed_list *
   8502 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   8503 			 struct bfd_link_info *info)
   8504 {
   8505   if (! is_elf_hash_table (info->hash))
   8506     return NULL;
   8507   return elf_hash_table (info)->needed;
   8508 }
   8509 
   8510 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   8511    hook for the linker ELF emulation code.  */
   8512 
   8513 struct bfd_link_needed_list *
   8514 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   8515 			  struct bfd_link_info *info)
   8516 {
   8517   if (! is_elf_hash_table (info->hash))
   8518     return NULL;
   8519   return elf_hash_table (info)->runpath;
   8520 }
   8521 
   8522 /* Get the name actually used for a dynamic object for a link.  This
   8523    is the SONAME entry if there is one.  Otherwise, it is the string
   8524    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   8525 
   8526 const char *
   8527 bfd_elf_get_dt_soname (bfd *abfd)
   8528 {
   8529   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8530       && bfd_get_format (abfd) == bfd_object)
   8531     return elf_dt_name (abfd);
   8532   return NULL;
   8533 }
   8534 
   8535 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   8536    the ELF linker emulation code.  */
   8537 
   8538 bool
   8539 bfd_elf_get_bfd_needed_list (bfd *abfd,
   8540 			     struct bfd_link_needed_list **pneeded)
   8541 {
   8542   asection *s;
   8543   bfd_byte *dynbuf = NULL;
   8544   unsigned int elfsec;
   8545   unsigned long shlink;
   8546   bfd_byte *extdyn, *extdynend;
   8547   size_t extdynsize;
   8548   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   8549 
   8550   *pneeded = NULL;
   8551 
   8552   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   8553       || bfd_get_format (abfd) != bfd_object)
   8554     return true;
   8555 
   8556   s = bfd_get_section_by_name (abfd, ".dynamic");
   8557   if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
   8558     return true;
   8559 
   8560   if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   8561     goto error_return;
   8562 
   8563   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   8564   if (elfsec == SHN_BAD)
   8565     goto error_return;
   8566 
   8567   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   8568 
   8569   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   8570   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   8571 
   8572   for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   8573        (size_t) (extdynend - extdyn) >= extdynsize;
   8574        extdyn += extdynsize)
   8575     {
   8576       Elf_Internal_Dyn dyn;
   8577 
   8578       (*swap_dyn_in) (abfd, extdyn, &dyn);
   8579 
   8580       if (dyn.d_tag == DT_NULL)
   8581 	break;
   8582 
   8583       if (dyn.d_tag == DT_NEEDED)
   8584 	{
   8585 	  const char *string;
   8586 	  struct bfd_link_needed_list *l;
   8587 	  unsigned int tagv = dyn.d_un.d_val;
   8588 	  size_t amt;
   8589 
   8590 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   8591 	  if (string == NULL)
   8592 	    goto error_return;
   8593 
   8594 	  amt = sizeof *l;
   8595 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   8596 	  if (l == NULL)
   8597 	    goto error_return;
   8598 
   8599 	  l->by = abfd;
   8600 	  l->name = string;
   8601 	  l->next = *pneeded;
   8602 	  *pneeded = l;
   8603 	}
   8604     }
   8605 
   8606   _bfd_elf_munmap_section_contents (s, dynbuf);
   8607 
   8608   return true;
   8609 
   8610  error_return:
   8611   _bfd_elf_munmap_section_contents (s, dynbuf);
   8612   return false;
   8613 }
   8614 
   8615 struct elf_symbuf_symbol
   8616 {
   8617   unsigned long st_name;	/* Symbol name, index in string tbl */
   8618   unsigned char st_info;	/* Type and binding attributes */
   8619   unsigned char st_other;	/* Visibilty, and target specific */
   8620 };
   8621 
   8622 struct elf_symbuf_head
   8623 {
   8624   struct elf_symbuf_symbol *ssym;
   8625   size_t count;
   8626   unsigned int st_shndx;
   8627 };
   8628 
   8629 struct elf_symbol
   8630 {
   8631   union
   8632     {
   8633       Elf_Internal_Sym *isym;
   8634       struct elf_symbuf_symbol *ssym;
   8635       void *p;
   8636     } u;
   8637   const char *name;
   8638 };
   8639 
   8640 /* Sort references to symbols by ascending section number.  */
   8641 
   8642 static int
   8643 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   8644 {
   8645   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   8646   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   8647 
   8648   if (s1->st_shndx != s2->st_shndx)
   8649     return s1->st_shndx > s2->st_shndx ? 1 : -1;
   8650   /* Final sort by the address of the sym in the symbuf ensures
   8651      a stable sort.  */
   8652   if (s1 != s2)
   8653     return s1 > s2 ? 1 : -1;
   8654   return 0;
   8655 }
   8656 
   8657 static int
   8658 elf_sym_name_compare (const void *arg1, const void *arg2)
   8659 {
   8660   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   8661   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   8662   int ret = strcmp (s1->name, s2->name);
   8663   if (ret != 0)
   8664     return ret;
   8665   if (s1->u.p != s2->u.p)
   8666     return s1->u.p > s2->u.p ? 1 : -1;
   8667   return 0;
   8668 }
   8669 
   8670 static struct elf_symbuf_head *
   8671 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
   8672 {
   8673   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   8674   struct elf_symbuf_symbol *ssym;
   8675   struct elf_symbuf_head *ssymbuf, *ssymhead;
   8676   size_t i, shndx_count, total_size, amt;
   8677 
   8678   amt = symcount * sizeof (*indbuf);
   8679   indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
   8680   if (indbuf == NULL)
   8681     return NULL;
   8682 
   8683   for (ind = indbuf, i = 0; i < symcount; i++)
   8684     if (isymbuf[i].st_shndx != SHN_UNDEF)
   8685       *ind++ = &isymbuf[i];
   8686   indbufend = ind;
   8687 
   8688   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   8689 	 elf_sort_elf_symbol);
   8690 
   8691   shndx_count = 0;
   8692   if (indbufend > indbuf)
   8693     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   8694       if (ind[0]->st_shndx != ind[1]->st_shndx)
   8695 	shndx_count++;
   8696 
   8697   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   8698 		+ (indbufend - indbuf) * sizeof (*ssym));
   8699   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   8700   if (ssymbuf == NULL)
   8701     {
   8702       free (indbuf);
   8703       return NULL;
   8704     }
   8705 
   8706   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   8707   ssymbuf->ssym = NULL;
   8708   ssymbuf->count = shndx_count;
   8709   ssymbuf->st_shndx = 0;
   8710   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   8711     {
   8712       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   8713 	{
   8714 	  ssymhead++;
   8715 	  ssymhead->ssym = ssym;
   8716 	  ssymhead->count = 0;
   8717 	  ssymhead->st_shndx = (*ind)->st_shndx;
   8718 	}
   8719       ssym->st_name = (*ind)->st_name;
   8720       ssym->st_info = (*ind)->st_info;
   8721       ssym->st_other = (*ind)->st_other;
   8722       ssymhead->count++;
   8723     }
   8724   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
   8725 	      && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
   8726 
   8727   free (indbuf);
   8728   return ssymbuf;
   8729 }
   8730 
   8731 /* Check if 2 sections define the same set of local and global
   8732    symbols.  */
   8733 
   8734 static bool
   8735 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   8736 				   struct bfd_link_info *info)
   8737 {
   8738   bfd *bfd1, *bfd2;
   8739   const struct elf_backend_data *bed1, *bed2;
   8740   Elf_Internal_Shdr *hdr1, *hdr2;
   8741   size_t symcount1, symcount2;
   8742   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   8743   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   8744   Elf_Internal_Sym *isym, *isymend;
   8745   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   8746   size_t count1, count2, sec_count1, sec_count2, i;
   8747   unsigned int shndx1, shndx2;
   8748   bool result;
   8749   bool ignore_section_symbol_p;
   8750 
   8751   bfd1 = sec1->owner;
   8752   bfd2 = sec2->owner;
   8753 
   8754   /* Both sections have to be in ELF.  */
   8755   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   8756       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   8757     return false;
   8758 
   8759   if (elf_section_type (sec1) != elf_section_type (sec2))
   8760     return false;
   8761 
   8762   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   8763   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   8764   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   8765     return false;
   8766 
   8767   bed1 = get_elf_backend_data (bfd1);
   8768   bed2 = get_elf_backend_data (bfd2);
   8769   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   8770   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   8771   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   8772   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   8773 
   8774   if (symcount1 == 0 || symcount2 == 0)
   8775     return false;
   8776 
   8777   result = false;
   8778   isymbuf1 = NULL;
   8779   isymbuf2 = NULL;
   8780   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   8781   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   8782 
   8783   /* Ignore section symbols only when matching non-debugging sections
   8784      or linkonce section with comdat section.  */
   8785   ignore_section_symbol_p
   8786     = ((sec1->flags & SEC_DEBUGGING) == 0
   8787        || ((elf_section_flags (sec1) & SHF_GROUP)
   8788 	   != (elf_section_flags (sec2) & SHF_GROUP)));
   8789 
   8790   if (ssymbuf1 == NULL)
   8791     {
   8792       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   8793 				       NULL, NULL, NULL);
   8794       if (isymbuf1 == NULL)
   8795 	goto done;
   8796 
   8797       if (info != NULL && !info->reduce_memory_overheads)
   8798 	{
   8799 	  ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
   8800 	  elf_tdata (bfd1)->symbuf = ssymbuf1;
   8801 	}
   8802     }
   8803 
   8804   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   8805     {
   8806       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   8807 				       NULL, NULL, NULL);
   8808       if (isymbuf2 == NULL)
   8809 	goto done;
   8810 
   8811       if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
   8812 	{
   8813 	  ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
   8814 	  elf_tdata (bfd2)->symbuf = ssymbuf2;
   8815 	}
   8816     }
   8817 
   8818   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   8819     {
   8820       /* Optimized faster version.  */
   8821       size_t lo, hi, mid;
   8822       struct elf_symbol *symp;
   8823       struct elf_symbuf_symbol *ssym, *ssymend;
   8824 
   8825       lo = 0;
   8826       hi = ssymbuf1->count;
   8827       ssymbuf1++;
   8828       count1 = 0;
   8829       sec_count1 = 0;
   8830       while (lo < hi)
   8831 	{
   8832 	  mid = (lo + hi) / 2;
   8833 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   8834 	    hi = mid;
   8835 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   8836 	    lo = mid + 1;
   8837 	  else
   8838 	    {
   8839 	      count1 = ssymbuf1[mid].count;
   8840 	      ssymbuf1 += mid;
   8841 	      break;
   8842 	    }
   8843 	}
   8844       if (ignore_section_symbol_p)
   8845 	{
   8846 	  for (i = 0; i < count1; i++)
   8847 	    if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
   8848 	      sec_count1++;
   8849 	  count1 -= sec_count1;
   8850 	}
   8851 
   8852       lo = 0;
   8853       hi = ssymbuf2->count;
   8854       ssymbuf2++;
   8855       count2 = 0;
   8856       sec_count2 = 0;
   8857       while (lo < hi)
   8858 	{
   8859 	  mid = (lo + hi) / 2;
   8860 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   8861 	    hi = mid;
   8862 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   8863 	    lo = mid + 1;
   8864 	  else
   8865 	    {
   8866 	      count2 = ssymbuf2[mid].count;
   8867 	      ssymbuf2 += mid;
   8868 	      break;
   8869 	    }
   8870 	}
   8871       if (ignore_section_symbol_p)
   8872 	{
   8873 	  for (i = 0; i < count2; i++)
   8874 	    if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
   8875 	      sec_count2++;
   8876 	  count2 -= sec_count2;
   8877 	}
   8878 
   8879       if (count1 == 0 || count2 == 0 || count1 != count2)
   8880 	goto done;
   8881 
   8882       symtable1
   8883 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   8884       symtable2
   8885 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   8886       if (symtable1 == NULL || symtable2 == NULL)
   8887 	goto done;
   8888 
   8889       symp = symtable1;
   8890       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
   8891 	   ssym < ssymend; ssym++)
   8892 	if (sec_count1 == 0
   8893 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8894 	  {
   8895 	    symp->u.ssym = ssym;
   8896 	    symp->name = bfd_elf_string_from_elf_section (bfd1,
   8897 							  hdr1->sh_link,
   8898 							  ssym->st_name);
   8899 	    if (symp->name == NULL)
   8900 	      goto done;
   8901 	    symp++;
   8902 	  }
   8903 
   8904       symp = symtable2;
   8905       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
   8906 	   ssym < ssymend; ssym++)
   8907 	if (sec_count2 == 0
   8908 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8909 	  {
   8910 	    symp->u.ssym = ssym;
   8911 	    symp->name = bfd_elf_string_from_elf_section (bfd2,
   8912 							  hdr2->sh_link,
   8913 							  ssym->st_name);
   8914 	    if (symp->name == NULL)
   8915 	      goto done;
   8916 	    symp++;
   8917 	  }
   8918 
   8919       /* Sort symbol by name.  */
   8920       qsort (symtable1, count1, sizeof (struct elf_symbol),
   8921 	     elf_sym_name_compare);
   8922       qsort (symtable2, count1, sizeof (struct elf_symbol),
   8923 	     elf_sym_name_compare);
   8924 
   8925       for (i = 0; i < count1; i++)
   8926 	/* Two symbols must have the same binding, type and name.  */
   8927 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   8928 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   8929 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8930 	  goto done;
   8931 
   8932       result = true;
   8933       goto done;
   8934     }
   8935 
   8936   symtable1 = (struct elf_symbol *)
   8937       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   8938   symtable2 = (struct elf_symbol *)
   8939       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   8940   if (symtable1 == NULL || symtable2 == NULL)
   8941     goto done;
   8942 
   8943   /* Count definitions in the section.  */
   8944   count1 = 0;
   8945   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   8946     if (isym->st_shndx == shndx1
   8947 	&& (!ignore_section_symbol_p
   8948 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8949       symtable1[count1++].u.isym = isym;
   8950 
   8951   count2 = 0;
   8952   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   8953     if (isym->st_shndx == shndx2
   8954 	&& (!ignore_section_symbol_p
   8955 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8956       symtable2[count2++].u.isym = isym;
   8957 
   8958   if (count1 == 0 || count2 == 0 || count1 != count2)
   8959     goto done;
   8960 
   8961   for (i = 0; i < count1; i++)
   8962     {
   8963       symtable1[i].name
   8964 	= bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   8965 					   symtable1[i].u.isym->st_name);
   8966       if (symtable1[i].name == NULL)
   8967 	goto done;
   8968     }
   8969 
   8970   for (i = 0; i < count2; i++)
   8971     {
   8972       symtable2[i].name
   8973 	= bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   8974 					   symtable2[i].u.isym->st_name);
   8975       if (symtable2[i].name == NULL)
   8976 	goto done;
   8977     }
   8978 
   8979   /* Sort symbol by name.  */
   8980   qsort (symtable1, count1, sizeof (struct elf_symbol),
   8981 	 elf_sym_name_compare);
   8982   qsort (symtable2, count1, sizeof (struct elf_symbol),
   8983 	 elf_sym_name_compare);
   8984 
   8985   for (i = 0; i < count1; i++)
   8986     /* Two symbols must have the same binding, type and name.  */
   8987     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   8988 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   8989 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8990       goto done;
   8991 
   8992   result = true;
   8993 
   8994  done:
   8995   free (symtable1);
   8996   free (symtable2);
   8997   free (isymbuf1);
   8998   free (isymbuf2);
   8999 
   9000   return result;
   9001 }
   9002 
   9003 /* Return TRUE if 2 section types are compatible.  */
   9004 
   9005 bool
   9006 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   9007 				 bfd *bbfd, const asection *bsec)
   9008 {
   9009   if (asec == NULL
   9010       || bsec == NULL
   9011       || abfd->xvec->flavour != bfd_target_elf_flavour
   9012       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   9013     return true;
   9014 
   9015   return elf_section_type (asec) == elf_section_type (bsec);
   9016 }
   9017 
   9018 /* Final phase of ELF linker.  */
   9020 
   9021 /* A structure we use to avoid passing large numbers of arguments.  */
   9022 
   9023 struct elf_final_link_info
   9024 {
   9025   /* General link information.  */
   9026   struct bfd_link_info *info;
   9027   /* Output BFD.  */
   9028   bfd *output_bfd;
   9029   /* Symbol string table.  */
   9030   struct elf_strtab_hash *symstrtab;
   9031   /* .hash section.  */
   9032   asection *hash_sec;
   9033   /* symbol version section (.gnu.version).  */
   9034   asection *symver_sec;
   9035   /* Buffer large enough to hold contents of any section.  */
   9036   bfd_byte *contents;
   9037   /* Buffer large enough to hold external relocs of any section.  */
   9038   void *external_relocs;
   9039   /* Buffer large enough to hold internal relocs of any section.  */
   9040   Elf_Internal_Rela *internal_relocs;
   9041   /* Buffer large enough to hold external local symbols of any input
   9042      BFD.  */
   9043   bfd_byte *external_syms;
   9044   /* And a buffer for symbol section indices.  */
   9045   Elf_External_Sym_Shndx *locsym_shndx;
   9046   /* Buffer large enough to hold internal local symbols of any input
   9047      BFD.  */
   9048   Elf_Internal_Sym *internal_syms;
   9049   /* Array large enough to hold a symbol index for each local symbol
   9050      of any input BFD.  */
   9051   long *indices;
   9052   /* Array large enough to hold a section pointer for each local
   9053      symbol of any input BFD.  */
   9054   asection **sections;
   9055   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   9056   Elf_External_Sym_Shndx *symshndxbuf;
   9057   /* Number of STT_FILE syms seen.  */
   9058   size_t filesym_count;
   9059   /* Local symbol hash table.  */
   9060   struct bfd_hash_table local_hash_table;
   9061 };
   9062 
   9063 struct local_hash_entry
   9064 {
   9065   /* Base hash table entry structure.  */
   9066   struct bfd_hash_entry root;
   9067   /* Size of the local symbol name.  */
   9068   size_t size;
   9069   /* Number of the duplicated local symbol names.  */
   9070   long count;
   9071 };
   9072 
   9073 /* Create an entry in the local symbol hash table.  */
   9074 
   9075 static struct bfd_hash_entry *
   9076 local_hash_newfunc (struct bfd_hash_entry *entry,
   9077 		    struct bfd_hash_table *table,
   9078 		    const char *string)
   9079 {
   9080 
   9081   /* Allocate the structure if it has not already been allocated by a
   9082      subclass.  */
   9083   if (entry == NULL)
   9084     {
   9085       entry = bfd_hash_allocate (table,
   9086 				 sizeof (struct local_hash_entry));
   9087       if (entry == NULL)
   9088         return entry;
   9089     }
   9090 
   9091   /* Call the allocation method of the superclass.  */
   9092   entry = bfd_hash_newfunc (entry, table, string);
   9093   if (entry != NULL)
   9094     {
   9095       ((struct local_hash_entry *) entry)->count = 0;
   9096       ((struct local_hash_entry *) entry)->size = 0;
   9097     }
   9098 
   9099   return entry;
   9100 }
   9101 
   9102 /* This struct is used to pass information to elf_link_output_extsym.  */
   9103 
   9104 struct elf_outext_info
   9105 {
   9106   bool failed;
   9107   bool localsyms;
   9108   bool file_sym_done;
   9109   struct elf_final_link_info *flinfo;
   9110 };
   9111 
   9112 
   9113 /* Support for evaluating a complex relocation.
   9114 
   9115    Complex relocations are generalized, self-describing relocations.  The
   9116    implementation of them consists of two parts: complex symbols, and the
   9117    relocations themselves.
   9118 
   9119    The relocations use a reserved elf-wide relocation type code (R_RELC
   9120    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   9121    information (start bit, end bit, word width, etc) into the addend.  This
   9122    information is extracted from CGEN-generated operand tables within gas.
   9123 
   9124    Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
   9125    internal) representing prefix-notation expressions, including but not
   9126    limited to those sorts of expressions normally encoded as addends in the
   9127    addend field.  The symbol mangling format is:
   9128 
   9129    <node> := <literal>
   9130 	  |  <unary-operator> ':' <node>
   9131 	  |  <binary-operator> ':' <node> ':' <node>
   9132 	  ;
   9133 
   9134    <literal> := 's' <digits=N> ':' <N character symbol name>
   9135 	     |  'S' <digits=N> ':' <N character section name>
   9136 	     |  '#' <hexdigits>
   9137 	     ;
   9138 
   9139    <binary-operator> := as in C
   9140    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   9141 
   9142 static void
   9143 set_symbol_value (bfd *bfd_with_globals,
   9144 		  Elf_Internal_Sym *isymbuf,
   9145 		  size_t locsymcount,
   9146 		  size_t symidx,
   9147 		  bfd_vma val)
   9148 {
   9149   struct elf_link_hash_entry *h;
   9150   size_t extsymoff = locsymcount;
   9151 
   9152   if (symidx < locsymcount)
   9153     {
   9154       Elf_Internal_Sym *sym;
   9155 
   9156       sym = isymbuf + symidx;
   9157       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   9158 	{
   9159 	  /* It is a local symbol: move it to the
   9160 	     "absolute" section and give it a value.  */
   9161 	  sym->st_shndx = SHN_ABS;
   9162 	  sym->st_value = val;
   9163 	  return;
   9164 	}
   9165       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   9166       extsymoff = 0;
   9167     }
   9168 
   9169   /* It is a global symbol: set its link type
   9170      to "defined" and give it a value.  */
   9171   h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff);
   9172   if (h == NULL)
   9173     {
   9174       /* FIXMEL What should we do ?  */
   9175       return;
   9176     }
   9177   h->root.type = bfd_link_hash_defined;
   9178   h->root.u.def.value = val;
   9179   h->root.u.def.section = bfd_abs_section_ptr;
   9180 }
   9181 
   9182 static bool
   9183 resolve_symbol (const char *name,
   9184 		bfd *input_bfd,
   9185 		struct elf_final_link_info *flinfo,
   9186 		bfd_vma *result,
   9187 		Elf_Internal_Sym *isymbuf,
   9188 		size_t locsymcount)
   9189 {
   9190   Elf_Internal_Sym *sym;
   9191   struct bfd_link_hash_entry *global_entry;
   9192   const char *candidate = NULL;
   9193   Elf_Internal_Shdr *symtab_hdr;
   9194   size_t i;
   9195 
   9196   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   9197 
   9198   for (i = 0; i < locsymcount; ++ i)
   9199     {
   9200       sym = isymbuf + i;
   9201 
   9202       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   9203 	continue;
   9204 
   9205       candidate = bfd_elf_string_from_elf_section (input_bfd,
   9206 						   symtab_hdr->sh_link,
   9207 						   sym->st_name);
   9208 #ifdef DEBUG
   9209       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   9210 	      name, candidate, (unsigned long) sym->st_value);
   9211 #endif
   9212       if (candidate && strcmp (candidate, name) == 0)
   9213 	{
   9214 	  asection *sec = flinfo->sections [i];
   9215 
   9216 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   9217 	  *result += sec->output_offset + sec->output_section->vma;
   9218 #ifdef DEBUG
   9219 	  printf ("Found symbol with value %8.8lx\n",
   9220 		  (unsigned long) *result);
   9221 #endif
   9222 	  return true;
   9223 	}
   9224     }
   9225 
   9226   /* Hmm, haven't found it yet. perhaps it is a global.  */
   9227   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   9228 				       false, false, true);
   9229   if (!global_entry)
   9230     return false;
   9231 
   9232   if (global_entry->type == bfd_link_hash_defined
   9233       || global_entry->type == bfd_link_hash_defweak)
   9234     {
   9235       *result = (global_entry->u.def.value
   9236 		 + global_entry->u.def.section->output_section->vma
   9237 		 + global_entry->u.def.section->output_offset);
   9238 #ifdef DEBUG
   9239       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   9240 	      global_entry->root.string, (unsigned long) *result);
   9241 #endif
   9242       return true;
   9243     }
   9244 
   9245   return false;
   9246 }
   9247 
   9248 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
   9249    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
   9250    names like "foo.end" which is the end address of section "foo".  */
   9251 
   9252 static bool
   9253 resolve_section (const char *name,
   9254 		 asection *sections,
   9255 		 bfd_vma *result,
   9256 		 bfd * abfd)
   9257 {
   9258   asection *curr;
   9259   unsigned int len;
   9260 
   9261   for (curr = sections; curr; curr = curr->next)
   9262     if (strcmp (curr->name, name) == 0)
   9263       {
   9264 	*result = curr->vma;
   9265 	return true;
   9266       }
   9267 
   9268   /* Hmm. still haven't found it. try pseudo-section names.  */
   9269   /* FIXME: This could be coded more efficiently...  */
   9270   for (curr = sections; curr; curr = curr->next)
   9271     {
   9272       len = strlen (curr->name);
   9273       if (len > strlen (name))
   9274 	continue;
   9275 
   9276       if (strncmp (curr->name, name, len) == 0)
   9277 	{
   9278 	  if (startswith (name + len, ".end"))
   9279 	    {
   9280 	      *result = (curr->vma
   9281 			 + curr->size / bfd_octets_per_byte (abfd, curr));
   9282 	      return true;
   9283 	    }
   9284 
   9285 	  /* Insert more pseudo-section names here, if you like.  */
   9286 	}
   9287     }
   9288 
   9289   return false;
   9290 }
   9291 
   9292 static void
   9293 undefined_reference (const char *reftype, const char *name)
   9294 {
   9295   /* xgettext:c-format */
   9296   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   9297 		      reftype, name);
   9298   bfd_set_error (bfd_error_bad_value);
   9299 }
   9300 
   9301 static bool
   9302 eval_symbol (bfd_vma *result,
   9303 	     const char **symp,
   9304 	     bfd *input_bfd,
   9305 	     struct elf_final_link_info *flinfo,
   9306 	     bfd_vma dot,
   9307 	     Elf_Internal_Sym *isymbuf,
   9308 	     size_t locsymcount,
   9309 	     int signed_p)
   9310 {
   9311   size_t len;
   9312   size_t symlen;
   9313   bfd_vma a;
   9314   bfd_vma b;
   9315   char symbuf[4096];
   9316   const char *sym = *symp;
   9317   const char *symend;
   9318   bool symbol_is_section = false;
   9319 
   9320   len = strlen (sym);
   9321   symend = sym + len;
   9322 
   9323   if (len < 1 || len > sizeof (symbuf))
   9324     {
   9325       bfd_set_error (bfd_error_invalid_operation);
   9326       return false;
   9327     }
   9328 
   9329   switch (* sym)
   9330     {
   9331     case '.':
   9332       *result = dot;
   9333       *symp = sym + 1;
   9334       return true;
   9335 
   9336     case '#':
   9337       ++sym;
   9338       *result = strtoul (sym, (char **) symp, 16);
   9339       return true;
   9340 
   9341     case 'S':
   9342       symbol_is_section = true;
   9343       /* Fall through.  */
   9344     case 's':
   9345       ++sym;
   9346       symlen = strtol (sym, (char **) symp, 10);
   9347       sym = *symp + 1; /* Skip the trailing ':'.  */
   9348 
   9349       if (symend < sym || symlen + 1 > sizeof (symbuf))
   9350 	{
   9351 	  bfd_set_error (bfd_error_invalid_operation);
   9352 	  return false;
   9353 	}
   9354 
   9355       memcpy (symbuf, sym, symlen);
   9356       symbuf[symlen] = '\0';
   9357       *symp = sym + symlen;
   9358 
   9359       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   9360 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   9361 	 interpretation here; section means "try section first", not "must be a
   9362 	 section", and likewise with symbol.  */
   9363 
   9364       if (symbol_is_section)
   9365 	{
   9366 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
   9367 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   9368 				  isymbuf, locsymcount))
   9369 	    {
   9370 	      undefined_reference ("section", symbuf);
   9371 	      return false;
   9372 	    }
   9373 	}
   9374       else
   9375 	{
   9376 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   9377 			       isymbuf, locsymcount)
   9378 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   9379 				   result, input_bfd))
   9380 	    {
   9381 	      undefined_reference ("symbol", symbuf);
   9382 	      return false;
   9383 	    }
   9384 	}
   9385 
   9386       return true;
   9387 
   9388       /* All that remains are operators.  */
   9389 
   9390 #define UNARY_OP(op)						\
   9391   if (startswith (sym, #op))					\
   9392     {								\
   9393       sym += strlen (#op);					\
   9394       if (*sym == ':')						\
   9395 	++sym;							\
   9396       *symp = sym;						\
   9397       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   9398 			isymbuf, locsymcount, signed_p))	\
   9399 	return false;						\
   9400       if (signed_p)						\
   9401 	*result = op ((bfd_signed_vma) a);			\
   9402       else							\
   9403 	*result = op a;						\
   9404       return true;						\
   9405     }
   9406 
   9407 #define BINARY_OP_HEAD(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       ++*symp;							\
   9418       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   9419 			isymbuf, locsymcount, signed_p))	\
   9420 	return false;
   9421 #define BINARY_OP_TAIL(op)					\
   9422       if (signed_p)						\
   9423 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   9424       else							\
   9425 	*result = a op b;					\
   9426       return true;						\
   9427     }
   9428 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
   9429 
   9430     default:
   9431       UNARY_OP  (0-);
   9432       BINARY_OP_HEAD (<<);
   9433       if (b >= sizeof (a) * CHAR_BIT)
   9434 	{
   9435 	  *result = 0;
   9436 	  return true;
   9437 	}
   9438       signed_p = 0;
   9439       BINARY_OP_TAIL (<<);
   9440       BINARY_OP_HEAD (>>);
   9441       if (b >= sizeof (a) * CHAR_BIT)
   9442 	{
   9443 	  *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
   9444 	  return true;
   9445 	}
   9446       BINARY_OP_TAIL (>>);
   9447       BINARY_OP (==);
   9448       BINARY_OP (!=);
   9449       BINARY_OP (<=);
   9450       BINARY_OP (>=);
   9451       BINARY_OP (&&);
   9452       BINARY_OP (||);
   9453       UNARY_OP  (~);
   9454       UNARY_OP  (!);
   9455       BINARY_OP (*);
   9456       BINARY_OP_HEAD (/);
   9457       if (b == 0)
   9458 	{
   9459 	  _bfd_error_handler (_("division by zero"));
   9460 	  bfd_set_error (bfd_error_bad_value);
   9461 	  return false;
   9462 	}
   9463       BINARY_OP_TAIL (/);
   9464       BINARY_OP_HEAD (%);
   9465       if (b == 0)
   9466 	{
   9467 	  _bfd_error_handler (_("division by zero"));
   9468 	  bfd_set_error (bfd_error_bad_value);
   9469 	  return false;
   9470 	}
   9471       BINARY_OP_TAIL (%);
   9472       BINARY_OP (^);
   9473       BINARY_OP (|);
   9474       BINARY_OP (&);
   9475       BINARY_OP (+);
   9476       BINARY_OP (-);
   9477       BINARY_OP (<);
   9478       BINARY_OP (>);
   9479 #undef UNARY_OP
   9480 #undef BINARY_OP
   9481       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   9482       bfd_set_error (bfd_error_invalid_operation);
   9483       return false;
   9484     }
   9485 }
   9486 
   9487 static void
   9488 put_value (bfd_vma size,
   9489 	   unsigned long chunksz,
   9490 	   bfd *input_bfd,
   9491 	   bfd_vma x,
   9492 	   bfd_byte *location)
   9493 {
   9494   location += (size - chunksz);
   9495 
   9496   for (; size; size -= chunksz, location -= chunksz)
   9497     {
   9498       switch (chunksz)
   9499 	{
   9500 	case 1:
   9501 	  bfd_put_8 (input_bfd, x, location);
   9502 	  x >>= 8;
   9503 	  break;
   9504 	case 2:
   9505 	  bfd_put_16 (input_bfd, x, location);
   9506 	  x >>= 16;
   9507 	  break;
   9508 	case 4:
   9509 	  bfd_put_32 (input_bfd, x, location);
   9510 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   9511 	  x >>= 16;
   9512 	  x >>= 16;
   9513 	  break;
   9514 #ifdef BFD64
   9515 	case 8:
   9516 	  bfd_put_64 (input_bfd, x, location);
   9517 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   9518 	  x >>= 32;
   9519 	  x >>= 32;
   9520 	  break;
   9521 #endif
   9522 	default:
   9523 	  abort ();
   9524 	  break;
   9525 	}
   9526     }
   9527 }
   9528 
   9529 static bfd_vma
   9530 get_value (bfd_vma size,
   9531 	   unsigned long chunksz,
   9532 	   bfd *input_bfd,
   9533 	   bfd_byte *location)
   9534 {
   9535   int shift;
   9536   bfd_vma x = 0;
   9537 
   9538   /* Sanity checks.  */
   9539   BFD_ASSERT (chunksz <= sizeof (x)
   9540 	      && size >= chunksz
   9541 	      && chunksz != 0
   9542 	      && (size % chunksz) == 0
   9543 	      && input_bfd != NULL
   9544 	      && location != NULL);
   9545 
   9546   if (chunksz == sizeof (x))
   9547     {
   9548       BFD_ASSERT (size == chunksz);
   9549 
   9550       /* Make sure that we do not perform an undefined shift operation.
   9551 	 We know that size == chunksz so there will only be one iteration
   9552 	 of the loop below.  */
   9553       shift = 0;
   9554     }
   9555   else
   9556     shift = 8 * chunksz;
   9557 
   9558   for (; size; size -= chunksz, location += chunksz)
   9559     {
   9560       switch (chunksz)
   9561 	{
   9562 	case 1:
   9563 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   9564 	  break;
   9565 	case 2:
   9566 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   9567 	  break;
   9568 	case 4:
   9569 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   9570 	  break;
   9571 #ifdef BFD64
   9572 	case 8:
   9573 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   9574 	  break;
   9575 #endif
   9576 	default:
   9577 	  abort ();
   9578 	}
   9579     }
   9580   return x;
   9581 }
   9582 
   9583 static void
   9584 decode_complex_addend (unsigned long *start,   /* in bits */
   9585 		       unsigned long *oplen,   /* in bits */
   9586 		       unsigned long *len,     /* in bits */
   9587 		       unsigned long *wordsz,  /* in bytes */
   9588 		       unsigned long *chunksz, /* in bytes */
   9589 		       unsigned long *lsb0_p,
   9590 		       unsigned long *signed_p,
   9591 		       unsigned long *trunc_p,
   9592 		       unsigned long encoded)
   9593 {
   9594   * start     =	 encoded	& 0x3F;
   9595   * len	      = (encoded >>  6) & 0x3F;
   9596   * oplen     = (encoded >> 12) & 0x3F;
   9597   * wordsz    = (encoded >> 18) & 0xF;
   9598   * chunksz   = (encoded >> 22) & 0xF;
   9599   * lsb0_p    = (encoded >> 27) & 1;
   9600   * signed_p  = (encoded >> 28) & 1;
   9601   * trunc_p   = (encoded >> 29) & 1;
   9602 }
   9603 
   9604 bfd_reloc_status_type
   9605 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   9606 				    asection *input_section,
   9607 				    bfd_byte *contents,
   9608 				    Elf_Internal_Rela *rel,
   9609 				    bfd_vma relocation)
   9610 {
   9611   bfd_vma shift, x, mask;
   9612   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   9613   bfd_reloc_status_type r;
   9614   bfd_size_type octets;
   9615 
   9616   /*  Perform this reloc, since it is complex.
   9617       (this is not to say that it necessarily refers to a complex
   9618       symbol; merely that it is a self-describing CGEN based reloc.
   9619       i.e. the addend has the complete reloc information (bit start, end,
   9620       word size, etc) encoded within it.).  */
   9621 
   9622   decode_complex_addend (&start, &oplen, &len, &wordsz,
   9623 			 &chunksz, &lsb0_p, &signed_p,
   9624 			 &trunc_p, rel->r_addend);
   9625 
   9626   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   9627 
   9628   if (lsb0_p)
   9629     shift = (start + 1) - len;
   9630   else
   9631     shift = (8 * wordsz) - (start + len);
   9632 
   9633   octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
   9634   x = get_value (wordsz, chunksz, input_bfd, contents + octets);
   9635 
   9636 #ifdef DEBUG
   9637   printf ("Doing complex reloc: "
   9638 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   9639 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   9640 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   9641 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   9642 	  oplen, (unsigned long) x, (unsigned long) mask,
   9643 	  (unsigned long) relocation);
   9644 #endif
   9645 
   9646   r = bfd_reloc_ok;
   9647   if (! trunc_p)
   9648     /* Now do an overflow check.  */
   9649     r = bfd_check_overflow ((signed_p
   9650 			     ? complain_overflow_signed
   9651 			     : complain_overflow_unsigned),
   9652 			    len, 0, (8 * wordsz),
   9653 			    relocation);
   9654 
   9655   /* Do the deed.  */
   9656   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   9657 
   9658 #ifdef DEBUG
   9659   printf ("           relocation: %8.8lx\n"
   9660 	  "         shifted mask: %8.8lx\n"
   9661 	  " shifted/masked reloc: %8.8lx\n"
   9662 	  "               result: %8.8lx\n",
   9663 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   9664 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   9665 #endif
   9666   put_value (wordsz, chunksz, input_bfd, x, contents + octets);
   9667   return r;
   9668 }
   9669 
   9670 /* Functions to read r_offset from external (target order) reloc
   9671    entry.  Faster than bfd_getl32 et al, because we let the compiler
   9672    know the value is aligned.  */
   9673 
   9674 static bfd_vma
   9675 ext32l_r_offset (const void *p)
   9676 {
   9677   union aligned32
   9678   {
   9679     uint32_t v;
   9680     unsigned char c[4];
   9681   };
   9682   const union aligned32 *a
   9683     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9684 
   9685   uint32_t aval = (  (uint32_t) a->c[0]
   9686 		   | (uint32_t) a->c[1] << 8
   9687 		   | (uint32_t) a->c[2] << 16
   9688 		   | (uint32_t) a->c[3] << 24);
   9689   return aval;
   9690 }
   9691 
   9692 static bfd_vma
   9693 ext32b_r_offset (const void *p)
   9694 {
   9695   union aligned32
   9696   {
   9697     uint32_t v;
   9698     unsigned char c[4];
   9699   };
   9700   const union aligned32 *a
   9701     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9702 
   9703   uint32_t aval = (  (uint32_t) a->c[0] << 24
   9704 		   | (uint32_t) a->c[1] << 16
   9705 		   | (uint32_t) a->c[2] << 8
   9706 		   | (uint32_t) a->c[3]);
   9707   return aval;
   9708 }
   9709 
   9710 static bfd_vma
   9711 ext64l_r_offset (const void *p)
   9712 {
   9713   union aligned64
   9714   {
   9715     uint64_t v;
   9716     unsigned char c[8];
   9717   };
   9718   const union aligned64 *a
   9719     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9720 
   9721   uint64_t aval = (  (uint64_t) a->c[0]
   9722 		   | (uint64_t) a->c[1] << 8
   9723 		   | (uint64_t) a->c[2] << 16
   9724 		   | (uint64_t) a->c[3] << 24
   9725 		   | (uint64_t) a->c[4] << 32
   9726 		   | (uint64_t) a->c[5] << 40
   9727 		   | (uint64_t) a->c[6] << 48
   9728 		   | (uint64_t) a->c[7] << 56);
   9729   return aval;
   9730 }
   9731 
   9732 static bfd_vma
   9733 ext64b_r_offset (const void *p)
   9734 {
   9735   union aligned64
   9736   {
   9737     uint64_t v;
   9738     unsigned char c[8];
   9739   };
   9740   const union aligned64 *a
   9741     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9742 
   9743   uint64_t aval = (  (uint64_t) a->c[0] << 56
   9744 		   | (uint64_t) a->c[1] << 48
   9745 		   | (uint64_t) a->c[2] << 40
   9746 		   | (uint64_t) a->c[3] << 32
   9747 		   | (uint64_t) a->c[4] << 24
   9748 		   | (uint64_t) a->c[5] << 16
   9749 		   | (uint64_t) a->c[6] << 8
   9750 		   | (uint64_t) a->c[7]);
   9751   return aval;
   9752 }
   9753 
   9754 /* When performing a relocatable link, the input relocations are
   9755    preserved.  But, if they reference global symbols, the indices
   9756    referenced must be updated.  Update all the relocations found in
   9757    RELDATA.  */
   9758 
   9759 static bool
   9760 elf_link_adjust_relocs (bfd *abfd,
   9761 			asection *sec,
   9762 			struct bfd_elf_section_reloc_data *reldata,
   9763 			bool sort,
   9764 			struct bfd_link_info *info)
   9765 {
   9766   unsigned int i;
   9767   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9768   bfd_byte *erela;
   9769   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   9770   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   9771   bfd_vma r_type_mask;
   9772   int r_sym_shift;
   9773   unsigned int count = reldata->count;
   9774   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   9775 
   9776   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   9777     {
   9778       swap_in = bed->s->swap_reloc_in;
   9779       swap_out = bed->s->swap_reloc_out;
   9780     }
   9781   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   9782     {
   9783       swap_in = bed->s->swap_reloca_in;
   9784       swap_out = bed->s->swap_reloca_out;
   9785     }
   9786   else
   9787     abort ();
   9788 
   9789   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   9790     abort ();
   9791 
   9792   if (bed->s->arch_size == 32)
   9793     {
   9794       r_type_mask = 0xff;
   9795       r_sym_shift = 8;
   9796     }
   9797   else
   9798     {
   9799       r_type_mask = 0xffffffff;
   9800       r_sym_shift = 32;
   9801     }
   9802 
   9803   erela = reldata->hdr->contents;
   9804   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   9805     {
   9806       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   9807       unsigned int j;
   9808 
   9809       if (*rel_hash == NULL)
   9810 	continue;
   9811 
   9812       if ((*rel_hash)->indx == -2
   9813 	  && info->gc_sections
   9814 	  && ! info->gc_keep_exported)
   9815 	{
   9816 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
   9817 	  _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
   9818 			      abfd, sec,
   9819 			      (*rel_hash)->root.root.string);
   9820 	  _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
   9821 			      abfd, sec);
   9822 	  bfd_set_error (bfd_error_invalid_operation);
   9823 	  return false;
   9824 	}
   9825       BFD_ASSERT ((*rel_hash)->indx >= 0);
   9826 
   9827       (*swap_in) (abfd, erela, irela);
   9828       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   9829 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   9830 			   | (irela[j].r_info & r_type_mask));
   9831       (*swap_out) (abfd, irela, erela);
   9832     }
   9833 
   9834   if (bed->elf_backend_update_relocs)
   9835     (*bed->elf_backend_update_relocs) (sec, reldata);
   9836 
   9837   if (sort && count != 0)
   9838     {
   9839       bfd_vma (*ext_r_off) (const void *);
   9840       bfd_vma r_off;
   9841       size_t elt_size;
   9842       bfd_byte *base, *end, *p, *loc;
   9843       bfd_byte *buf = NULL;
   9844 
   9845       if (bed->s->arch_size == 32)
   9846 	{
   9847 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9848 	    ext_r_off = ext32l_r_offset;
   9849 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9850 	    ext_r_off = ext32b_r_offset;
   9851 	  else
   9852 	    abort ();
   9853 	}
   9854       else
   9855 	{
   9856 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9857 	    ext_r_off = ext64l_r_offset;
   9858 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9859 	    ext_r_off = ext64b_r_offset;
   9860 	  else
   9861 	    abort ();
   9862 	}
   9863 
   9864       /*  Must use a stable sort here.  A modified insertion sort,
   9865 	  since the relocs are mostly sorted already.  */
   9866       elt_size = reldata->hdr->sh_entsize;
   9867       base = reldata->hdr->contents;
   9868       end = base + count * elt_size;
   9869       if (elt_size > sizeof (Elf64_External_Rela))
   9870 	abort ();
   9871 
   9872       /* Ensure the first element is lowest.  This acts as a sentinel,
   9873 	 speeding the main loop below.  */
   9874       r_off = (*ext_r_off) (base);
   9875       for (p = loc = base; (p += elt_size) < end; )
   9876 	{
   9877 	  bfd_vma r_off2 = (*ext_r_off) (p);
   9878 	  if (r_off > r_off2)
   9879 	    {
   9880 	      r_off = r_off2;
   9881 	      loc = p;
   9882 	    }
   9883 	}
   9884       if (loc != base)
   9885 	{
   9886 	  /* Don't just swap *base and *loc as that changes the order
   9887 	     of the original base[0] and base[1] if they happen to
   9888 	     have the same r_offset.  */
   9889 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   9890 	  memcpy (onebuf, loc, elt_size);
   9891 	  memmove (base + elt_size, base, loc - base);
   9892 	  memcpy (base, onebuf, elt_size);
   9893 	}
   9894 
   9895       for (p = base + elt_size; (p += elt_size) < end; )
   9896 	{
   9897 	  /* base to p is sorted, *p is next to insert.  */
   9898 	  r_off = (*ext_r_off) (p);
   9899 	  /* Search the sorted region for location to insert.  */
   9900 	  loc = p - elt_size;
   9901 	  while (r_off < (*ext_r_off) (loc))
   9902 	    loc -= elt_size;
   9903 	  loc += elt_size;
   9904 	  if (loc != p)
   9905 	    {
   9906 	      /* Chances are there is a run of relocs to insert here,
   9907 		 from one of more input files.  Files are not always
   9908 		 linked in order due to the way elf_link_input_bfd is
   9909 		 called.  See pr17666.  */
   9910 	      size_t sortlen = p - loc;
   9911 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   9912 	      size_t runlen = elt_size;
   9913 	      bfd_vma r_off_runend = r_off;
   9914 	      bfd_vma r_off_runend_next;
   9915 	      size_t buf_size = 96 * 1024;
   9916 	      while (p + runlen < end
   9917 		     && (sortlen <= buf_size
   9918 			 || runlen + elt_size <= buf_size)
   9919 		     /* run must not break the ordering of base..loc+1 */
   9920 		     && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
   9921 		     /* run must be already sorted */
   9922 		     && r_off_runend_next >= r_off_runend)
   9923 		{
   9924 		  runlen += elt_size;
   9925 		  r_off_runend = r_off_runend_next;
   9926 		}
   9927 	      if (buf == NULL)
   9928 		{
   9929 		  buf = bfd_malloc (buf_size);
   9930 		  if (buf == NULL)
   9931 		    return false;
   9932 		}
   9933 	      if (runlen < sortlen)
   9934 		{
   9935 		  memcpy (buf, p, runlen);
   9936 		  memmove (loc + runlen, loc, sortlen);
   9937 		  memcpy (loc, buf, runlen);
   9938 		}
   9939 	      else
   9940 		{
   9941 		  memcpy (buf, loc, sortlen);
   9942 		  memmove (loc, p, runlen);
   9943 		  memcpy (loc + runlen, buf, sortlen);
   9944 		}
   9945 	      p += runlen - elt_size;
   9946 	    }
   9947 	}
   9948       /* Hashes are no longer valid.  */
   9949       free (reldata->hashes);
   9950       reldata->hashes = NULL;
   9951       free (buf);
   9952     }
   9953   return true;
   9954 }
   9955 
   9956 struct elf_link_sort_rela
   9957 {
   9958   union {
   9959     bfd_vma offset;
   9960     bfd_vma sym_mask;
   9961   } u;
   9962   enum elf_reloc_type_class type;
   9963   /* We use this as an array of size int_rels_per_ext_rel.  */
   9964   Elf_Internal_Rela rela[1];
   9965 };
   9966 
   9967 /* qsort stability here and for cmp2 is only an issue if multiple
   9968    dynamic relocations are emitted at the same address.  But targets
   9969    that apply a series of dynamic relocations each operating on the
   9970    result of the prior relocation can't use -z combreloc as
   9971    implemented anyway.  Such schemes tend to be broken by sorting on
   9972    symbol index.  That leaves dynamic NONE relocs as the only other
   9973    case where ld might emit multiple relocs at the same address, and
   9974    those are only emitted due to target bugs.  */
   9975 
   9976 static int
   9977 elf_link_sort_cmp1 (const void *A, const void *B)
   9978 {
   9979   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9980   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9981   int relativea, relativeb;
   9982 
   9983   relativea = a->type == reloc_class_relative;
   9984   relativeb = b->type == reloc_class_relative;
   9985 
   9986   if (relativea < relativeb)
   9987     return 1;
   9988   if (relativea > relativeb)
   9989     return -1;
   9990   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   9991     return -1;
   9992   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   9993     return 1;
   9994   if (a->rela->r_offset < b->rela->r_offset)
   9995     return -1;
   9996   if (a->rela->r_offset > b->rela->r_offset)
   9997     return 1;
   9998   return 0;
   9999 }
   10000 
   10001 static int
   10002 elf_link_sort_cmp2 (const void *A, const void *B)
   10003 {
   10004   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   10005   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   10006 
   10007   if (a->type < b->type)
   10008     return -1;
   10009   if (a->type > b->type)
   10010     return 1;
   10011   if (a->u.offset < b->u.offset)
   10012     return -1;
   10013   if (a->u.offset > b->u.offset)
   10014     return 1;
   10015   if (a->rela->r_offset < b->rela->r_offset)
   10016     return -1;
   10017   if (a->rela->r_offset > b->rela->r_offset)
   10018     return 1;
   10019   return 0;
   10020 }
   10021 
   10022 static size_t
   10023 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   10024 {
   10025   asection *dynamic_relocs;
   10026   asection *rela_dyn;
   10027   asection *rel_dyn;
   10028   bfd_size_type count, size;
   10029   size_t i, ret, sort_elt, ext_size;
   10030   bfd_byte *sort, *s_non_relative, *p;
   10031   struct elf_link_sort_rela *sq;
   10032   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10033   int i2e = bed->s->int_rels_per_ext_rel;
   10034   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   10035   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   10036   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   10037   struct bfd_link_order *lo;
   10038   bfd_vma r_sym_mask;
   10039   bool use_rela;
   10040 
   10041   /* Find a dynamic reloc section.  */
   10042   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   10043   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   10044   if (rela_dyn != NULL && rela_dyn->size > 0
   10045       && rel_dyn != NULL && rel_dyn->size > 0)
   10046     {
   10047       bool use_rela_initialised = false;
   10048 
   10049       /* This is just here to stop gcc from complaining.
   10050 	 Its initialization checking code is not perfect.  */
   10051       use_rela = true;
   10052 
   10053       /* Both sections are present.  Examine the sizes
   10054 	 of the indirect sections to help us choose.  */
   10055       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   10056 	if (lo->type == bfd_indirect_link_order)
   10057 	  {
   10058 	    asection *o = lo->u.indirect.section;
   10059 
   10060 	    if ((o->size % bed->s->sizeof_rela) == 0)
   10061 	      {
   10062 		if ((o->size % bed->s->sizeof_rel) == 0)
   10063 		  /* Section size is divisible by both rel and rela sizes.
   10064 		     It is of no help to us.  */
   10065 		  ;
   10066 		else
   10067 		  {
   10068 		    /* Section size is only divisible by rela.  */
   10069 		    if (use_rela_initialised && !use_rela)
   10070 		      {
   10071 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   10072 					      "they are in more than one size"),
   10073 					    abfd);
   10074 			bfd_set_error (bfd_error_invalid_operation);
   10075 			return 0;
   10076 		      }
   10077 		    else
   10078 		      {
   10079 			use_rela = true;
   10080 			use_rela_initialised = true;
   10081 		      }
   10082 		  }
   10083 	      }
   10084 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   10085 	      {
   10086 		/* Section size is only divisible by rel.  */
   10087 		if (use_rela_initialised && use_rela)
   10088 		  {
   10089 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   10090 					  "they are in more than one size"),
   10091 					abfd);
   10092 		    bfd_set_error (bfd_error_invalid_operation);
   10093 		    return 0;
   10094 		  }
   10095 		else
   10096 		  {
   10097 		    use_rela = false;
   10098 		    use_rela_initialised = true;
   10099 		  }
   10100 	      }
   10101 	    else
   10102 	      {
   10103 		/* The section size is not divisible by either -
   10104 		   something is wrong.  */
   10105 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   10106 				      "they are of an unknown size"), abfd);
   10107 		bfd_set_error (bfd_error_invalid_operation);
   10108 		return 0;
   10109 	      }
   10110 	  }
   10111 
   10112       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   10113 	if (lo->type == bfd_indirect_link_order)
   10114 	  {
   10115 	    asection *o = lo->u.indirect.section;
   10116 
   10117 	    if ((o->size % bed->s->sizeof_rela) == 0)
   10118 	      {
   10119 		if ((o->size % bed->s->sizeof_rel) == 0)
   10120 		  /* Section size is divisible by both rel and rela sizes.
   10121 		     It is of no help to us.  */
   10122 		  ;
   10123 		else
   10124 		  {
   10125 		    /* Section size is only divisible by rela.  */
   10126 		    if (use_rela_initialised && !use_rela)
   10127 		      {
   10128 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   10129 					      "they are in more than one size"),
   10130 					    abfd);
   10131 			bfd_set_error (bfd_error_invalid_operation);
   10132 			return 0;
   10133 		      }
   10134 		    else
   10135 		      {
   10136 			use_rela = true;
   10137 			use_rela_initialised = true;
   10138 		      }
   10139 		  }
   10140 	      }
   10141 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   10142 	      {
   10143 		/* Section size is only divisible by rel.  */
   10144 		if (use_rela_initialised && use_rela)
   10145 		  {
   10146 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   10147 					  "they are in more than one size"),
   10148 					abfd);
   10149 		    bfd_set_error (bfd_error_invalid_operation);
   10150 		    return 0;
   10151 		  }
   10152 		else
   10153 		  {
   10154 		    use_rela = false;
   10155 		    use_rela_initialised = true;
   10156 		  }
   10157 	      }
   10158 	    else
   10159 	      {
   10160 		/* The section size is not divisible by either -
   10161 		   something is wrong.  */
   10162 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   10163 				      "they are of an unknown size"), abfd);
   10164 		bfd_set_error (bfd_error_invalid_operation);
   10165 		return 0;
   10166 	      }
   10167 	  }
   10168 
   10169       if (! use_rela_initialised)
   10170 	/* Make a guess.  */
   10171 	use_rela = true;
   10172     }
   10173   else if (rela_dyn != NULL && rela_dyn->size > 0)
   10174     use_rela = true;
   10175   else if (rel_dyn != NULL && rel_dyn->size > 0)
   10176     use_rela = false;
   10177   else
   10178     return 0;
   10179 
   10180   if (use_rela)
   10181     {
   10182       dynamic_relocs = rela_dyn;
   10183       ext_size = bed->s->sizeof_rela;
   10184       swap_in = bed->s->swap_reloca_in;
   10185       swap_out = bed->s->swap_reloca_out;
   10186     }
   10187   else
   10188     {
   10189       dynamic_relocs = rel_dyn;
   10190       ext_size = bed->s->sizeof_rel;
   10191       swap_in = bed->s->swap_reloc_in;
   10192       swap_out = bed->s->swap_reloc_out;
   10193     }
   10194 
   10195   size = 0;
   10196   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10197     if (lo->type == bfd_indirect_link_order)
   10198       size += lo->u.indirect.section->size;
   10199 
   10200   if (size != dynamic_relocs->size)
   10201     return 0;
   10202 
   10203   sort_elt = (sizeof (struct elf_link_sort_rela)
   10204 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   10205 
   10206   count = dynamic_relocs->size / ext_size;
   10207   if (count == 0)
   10208     return 0;
   10209   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   10210 
   10211   if (sort == NULL)
   10212     {
   10213       (*info->callbacks->warning)
   10214 	(info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
   10215       return 0;
   10216     }
   10217 
   10218   if (bed->s->arch_size == 32)
   10219     r_sym_mask = ~(bfd_vma) 0xff;
   10220   else
   10221     r_sym_mask = ~(bfd_vma) 0xffffffff;
   10222 
   10223   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10224     if (lo->type == bfd_indirect_link_order)
   10225       {
   10226 	bfd_byte *erel, *erelend;
   10227 	asection *o = lo->u.indirect.section;
   10228 
   10229 	if (o->contents == NULL && o->size != 0)
   10230 	  {
   10231 	    /* This is a reloc section that is being handled as a normal
   10232 	       section.  See bfd_section_from_shdr.  We can't combine
   10233 	       relocs in this case.  */
   10234 	    free (sort);
   10235 	    return 0;
   10236 	  }
   10237 	erel = o->contents;
   10238 	erelend = o->contents + o->size;
   10239 	p = sort + o->output_offset * opb / ext_size * sort_elt;
   10240 
   10241 	while (erel < erelend)
   10242 	  {
   10243 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10244 
   10245 	    (*swap_in) (abfd, erel, s->rela);
   10246 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   10247 	    s->u.sym_mask = r_sym_mask;
   10248 	    p += sort_elt;
   10249 	    erel += ext_size;
   10250 	  }
   10251       }
   10252 
   10253   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   10254 
   10255   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   10256     {
   10257       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10258       if (s->type != reloc_class_relative)
   10259 	break;
   10260     }
   10261   ret = i;
   10262   s_non_relative = p;
   10263 
   10264   sq = (struct elf_link_sort_rela *) s_non_relative;
   10265   for (; i < count; i++, p += sort_elt)
   10266     {
   10267       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   10268       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   10269 	sq = sp;
   10270       sp->u.offset = sq->rela->r_offset;
   10271     }
   10272 
   10273   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   10274 
   10275   struct elf_link_hash_table *htab = elf_hash_table (info);
   10276   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
   10277     {
   10278       /* We have plt relocs in .rela.dyn.  */
   10279       sq = (struct elf_link_sort_rela *) sort;
   10280       for (i = 0; i < count; i++)
   10281 	if (sq[count - i - 1].type != reloc_class_plt)
   10282 	  break;
   10283       if (i != 0 && htab->srelplt->size == i * ext_size)
   10284 	{
   10285 	  struct bfd_link_order **plo;
   10286 	  /* Put srelplt link_order last.  This is so the output_offset
   10287 	     set in the next loop is correct for DT_JMPREL.  */
   10288 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
   10289 	    if ((*plo)->type == bfd_indirect_link_order
   10290 		&& (*plo)->u.indirect.section == htab->srelplt)
   10291 	      {
   10292 		lo = *plo;
   10293 		*plo = lo->next;
   10294 	      }
   10295 	    else
   10296 	      plo = &(*plo)->next;
   10297 	  *plo = lo;
   10298 	  lo->next = NULL;
   10299 	  dynamic_relocs->map_tail.link_order = lo;
   10300 	}
   10301     }
   10302 
   10303   p = sort;
   10304   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10305     if (lo->type == bfd_indirect_link_order)
   10306       {
   10307 	bfd_byte *erel, *erelend;
   10308 	asection *o = lo->u.indirect.section;
   10309 
   10310 	erel = o->contents;
   10311 	erelend = o->contents + o->size;
   10312 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
   10313 	while (erel < erelend)
   10314 	  {
   10315 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10316 	    (*swap_out) (abfd, s->rela, erel);
   10317 	    p += sort_elt;
   10318 	    erel += ext_size;
   10319 	  }
   10320       }
   10321 
   10322   free (sort);
   10323   *psec = dynamic_relocs;
   10324   return ret;
   10325 }
   10326 
   10327 /* Add a symbol to the output symbol string table.  */
   10328 
   10329 static int
   10330 elf_link_output_symstrtab (void *finf,
   10331 			   const char *name,
   10332 			   Elf_Internal_Sym *elfsym,
   10333 			   asection *input_sec,
   10334 			   struct elf_link_hash_entry *h)
   10335 {
   10336   struct elf_final_link_info *flinfo = finf;
   10337   int (*output_symbol_hook)
   10338     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   10339      struct elf_link_hash_entry *);
   10340   struct elf_link_hash_table *hash_table;
   10341   const struct elf_backend_data *bed;
   10342   bfd_size_type strtabsize;
   10343 
   10344   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10345 
   10346   bed = get_elf_backend_data (flinfo->output_bfd);
   10347   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   10348   if (output_symbol_hook != NULL)
   10349     {
   10350       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   10351       if (ret != 1)
   10352 	return ret;
   10353     }
   10354 
   10355   if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
   10356     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
   10357   if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
   10358     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
   10359 
   10360   if (name == NULL || *name == '\0')
   10361     elfsym->st_name = (unsigned long) -1;
   10362   else
   10363     {
   10364       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   10365 	 to get the final offset for st_name.  */
   10366       char *versioned_name = (char *) name;
   10367       if (h != NULL)
   10368 	{
   10369 	  if (h->versioned == versioned && h->def_dynamic)
   10370 	    {
   10371 	      /* Keep only one '@' for versioned symbols defined in
   10372 	         shared objects.  */
   10373 	      char *version = strrchr (name, ELF_VER_CHR);
   10374 	      char *base_end = strchr (name, ELF_VER_CHR);
   10375 	      if (version != base_end)
   10376 		{
   10377 		  size_t base_len;
   10378 		  size_t len = strlen (name);
   10379 		  versioned_name = bfd_alloc (flinfo->output_bfd, len);
   10380 		  if (versioned_name == NULL)
   10381 		    return 0;
   10382 		  base_len = base_end - name;
   10383 		  memcpy (versioned_name, name, base_len);
   10384 		  memcpy (versioned_name + base_len, version,
   10385 			  len - base_len);
   10386 		}
   10387 	    }
   10388 	}
   10389       else if (flinfo->info->unique_symbol
   10390 	       && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
   10391 	{
   10392 	  struct local_hash_entry *lh;
   10393 	  size_t count_len;
   10394 	  size_t base_len;
   10395 	  char buf[30];
   10396 	  switch (ELF_ST_TYPE (elfsym->st_info))
   10397 	    {
   10398 	    case STT_FILE:
   10399 	    case STT_SECTION:
   10400 	      break;
   10401 	    default:
   10402 	      lh = (struct local_hash_entry *) bfd_hash_lookup
   10403 		     (&flinfo->local_hash_table, name, true, false);
   10404 	      if (lh == NULL)
   10405 		return 0;
   10406 	      /* Always append ".COUNT" to local symbols to avoid
   10407 		 potential conflicts with local symbol "XXX.COUNT".  */
   10408 	      sprintf (buf, "%lx", lh->count);
   10409 	      base_len = lh->size;
   10410 	      if (!base_len)
   10411 		{
   10412 		  base_len = strlen (name);
   10413 		  lh->size = base_len;
   10414 		}
   10415 	      count_len = strlen (buf);
   10416 	      versioned_name = bfd_alloc (flinfo->output_bfd,
   10417 					  base_len + count_len + 2);
   10418 	      if (versioned_name == NULL)
   10419 		return 0;
   10420 	      memcpy (versioned_name, name, base_len);
   10421 	      versioned_name[base_len] = '.';
   10422 	      memcpy (versioned_name + base_len + 1, buf,
   10423 		      count_len + 1);
   10424 	      lh->count++;
   10425 	      break;
   10426 	    }
   10427 	}
   10428       elfsym->st_name
   10429 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   10430 					       versioned_name, false);
   10431       if (elfsym->st_name == (unsigned long) -1)
   10432 	return 0;
   10433     }
   10434 
   10435   hash_table = elf_hash_table (flinfo->info);
   10436   strtabsize = hash_table->strtabsize;
   10437   if (strtabsize <= flinfo->output_bfd->symcount)
   10438     {
   10439       strtabsize += strtabsize;
   10440       hash_table->strtabsize = strtabsize;
   10441       strtabsize *= sizeof (*hash_table->strtab);
   10442       hash_table->strtab
   10443 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   10444 						 strtabsize);
   10445       if (hash_table->strtab == NULL)
   10446 	return 0;
   10447     }
   10448   hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
   10449   hash_table->strtab[flinfo->output_bfd->symcount].dest_index
   10450     = flinfo->output_bfd->symcount;
   10451   flinfo->output_bfd->symcount += 1;
   10452 
   10453   return 1;
   10454 }
   10455 
   10456 /* Swap symbols out to the symbol table and flush the output symbols to
   10457    the file.  */
   10458 
   10459 static bool
   10460 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   10461 {
   10462   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   10463   size_t amt;
   10464   size_t i;
   10465   const struct elf_backend_data *bed;
   10466   bfd_byte *symbuf;
   10467   Elf_Internal_Shdr *hdr;
   10468   file_ptr pos;
   10469   bool ret;
   10470 
   10471   if (flinfo->output_bfd->symcount == 0)
   10472     return true;
   10473 
   10474   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10475 
   10476   bed = get_elf_backend_data (flinfo->output_bfd);
   10477 
   10478   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10479   symbuf = (bfd_byte *) bfd_malloc (amt);
   10480   if (symbuf == NULL)
   10481     return false;
   10482 
   10483   if (flinfo->symshndxbuf)
   10484     {
   10485       amt = sizeof (Elf_External_Sym_Shndx);
   10486       amt *= bfd_get_symcount (flinfo->output_bfd);
   10487       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   10488       if (flinfo->symshndxbuf == NULL)
   10489 	{
   10490 	  free (symbuf);
   10491 	  return false;
   10492 	}
   10493     }
   10494 
   10495   /* Now swap out the symbols.  */
   10496   for (i = 0; i < flinfo->output_bfd->symcount; i++)
   10497     {
   10498       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   10499       if (elfsym->sym.st_name == (unsigned long) -1)
   10500 	elfsym->sym.st_name = 0;
   10501       else
   10502 	elfsym->sym.st_name
   10503 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   10504 						    elfsym->sym.st_name);
   10505 
   10506       /* Inform the linker of the addition of this symbol.  */
   10507 
   10508       if (flinfo->info->callbacks->ctf_new_symbol)
   10509 	flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
   10510 						 &elfsym->sym);
   10511 
   10512       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   10513 			       ((bfd_byte *) symbuf
   10514 				+ (elfsym->dest_index
   10515 				   * bed->s->sizeof_sym)),
   10516 			       NPTR_ADD (flinfo->symshndxbuf,
   10517 					 elfsym->dest_index));
   10518     }
   10519 
   10520   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   10521   pos = hdr->sh_offset + hdr->sh_size;
   10522   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10523   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   10524       && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
   10525     {
   10526       hdr->sh_size += amt;
   10527       ret = true;
   10528     }
   10529   else
   10530     ret = false;
   10531 
   10532   free (symbuf);
   10533   return ret;
   10534 }
   10535 
   10536 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   10537 
   10538 static bool
   10539 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   10540 {
   10541   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   10542       && sym->st_shndx < SHN_LORESERVE)
   10543     {
   10544       /* The gABI doesn't support dynamic symbols in output sections
   10545 	 beyond 64k.  */
   10546       _bfd_error_handler
   10547 	/* xgettext:c-format */
   10548 	(_("%pB: too many sections: %d (>= %d)"),
   10549 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   10550       bfd_set_error (bfd_error_nonrepresentable_section);
   10551       return false;
   10552     }
   10553   return true;
   10554 }
   10555 
   10556 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   10557    allowing an unsatisfied unversioned symbol in the DSO to match a
   10558    versioned symbol that would normally require an explicit version.
   10559    We also handle the case that a DSO references a hidden symbol
   10560    which may be satisfied by a versioned symbol in another DSO.  */
   10561 
   10562 static bool
   10563 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   10564 				 const struct elf_backend_data *bed,
   10565 				 struct elf_link_hash_entry *h)
   10566 {
   10567   bfd *abfd;
   10568   struct elf_link_loaded_list *loaded;
   10569 
   10570   if (!is_elf_hash_table (info->hash))
   10571     return false;
   10572 
   10573   /* Check indirect symbol.  */
   10574   while (h->root.type == bfd_link_hash_indirect)
   10575     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10576 
   10577   switch (h->root.type)
   10578     {
   10579     default:
   10580       abfd = NULL;
   10581       break;
   10582 
   10583     case bfd_link_hash_undefined:
   10584     case bfd_link_hash_undefweak:
   10585       abfd = h->root.u.undef.abfd;
   10586       if (abfd == NULL
   10587 	  || (abfd->flags & DYNAMIC) == 0
   10588 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   10589 	return false;
   10590       break;
   10591 
   10592     case bfd_link_hash_defined:
   10593     case bfd_link_hash_defweak:
   10594       abfd = h->root.u.def.section->owner;
   10595       break;
   10596 
   10597     case bfd_link_hash_common:
   10598       abfd = h->root.u.c.p->section->owner;
   10599       break;
   10600     }
   10601   BFD_ASSERT (abfd != NULL);
   10602 
   10603   for (loaded = elf_hash_table (info)->dyn_loaded;
   10604        loaded != NULL;
   10605        loaded = loaded->next)
   10606     {
   10607       bfd *input;
   10608       Elf_Internal_Shdr *hdr;
   10609       size_t symcount;
   10610       size_t extsymcount;
   10611       size_t extsymoff;
   10612       Elf_Internal_Shdr *versymhdr;
   10613       Elf_Internal_Sym *isym;
   10614       Elf_Internal_Sym *isymend;
   10615       Elf_Internal_Sym *isymbuf;
   10616       Elf_External_Versym *ever;
   10617       Elf_External_Versym *extversym;
   10618 
   10619       input = loaded->abfd;
   10620 
   10621       /* We check each DSO for a possible hidden versioned definition.  */
   10622       if (input == abfd
   10623 	  || elf_dynversym (input) == 0)
   10624 	continue;
   10625 
   10626       hdr = &elf_tdata (input)->dynsymtab_hdr;
   10627 
   10628       symcount = hdr->sh_size / bed->s->sizeof_sym;
   10629       if (elf_bad_symtab (input))
   10630 	{
   10631 	  extsymcount = symcount;
   10632 	  extsymoff = 0;
   10633 	}
   10634       else
   10635 	{
   10636 	  extsymcount = symcount - hdr->sh_info;
   10637 	  extsymoff = hdr->sh_info;
   10638 	}
   10639 
   10640       if (extsymcount == 0)
   10641 	continue;
   10642 
   10643       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   10644 				      NULL, NULL, NULL);
   10645       if (isymbuf == NULL)
   10646 	return false;
   10647 
   10648       /* Read in any version definitions.  */
   10649       versymhdr = &elf_tdata (input)->dynversym_hdr;
   10650       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   10651 	  || (extversym = (Elf_External_Versym *)
   10652 	      _bfd_malloc_and_read (input, versymhdr->sh_size,
   10653 				    versymhdr->sh_size)) == NULL)
   10654 	{
   10655 	  free (isymbuf);
   10656 	  return false;
   10657 	}
   10658 
   10659       ever = extversym + extsymoff;
   10660       isymend = isymbuf + extsymcount;
   10661       for (isym = isymbuf; isym < isymend; isym++, ever++)
   10662 	{
   10663 	  const char *name;
   10664 	  Elf_Internal_Versym iver;
   10665 	  unsigned short version_index;
   10666 
   10667 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   10668 	      || isym->st_shndx == SHN_UNDEF)
   10669 	    continue;
   10670 
   10671 	  name = bfd_elf_string_from_elf_section (input,
   10672 						  hdr->sh_link,
   10673 						  isym->st_name);
   10674 	  if (strcmp (name, h->root.root.string) != 0)
   10675 	    continue;
   10676 
   10677 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   10678 
   10679 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   10680 	      && !(h->def_regular
   10681 		   && h->forced_local))
   10682 	    {
   10683 	      /* If we have a non-hidden versioned sym, then it should
   10684 		 have provided a definition for the undefined sym unless
   10685 		 it is defined in a non-shared object and forced local.
   10686 	       */
   10687 	      abort ();
   10688 	    }
   10689 
   10690 	  version_index = iver.vs_vers & VERSYM_VERSION;
   10691 	  if (version_index == 1 || version_index == 2)
   10692 	    {
   10693 	      /* This is the base or first version.  We can use it.  */
   10694 	      free (extversym);
   10695 	      free (isymbuf);
   10696 	      return true;
   10697 	    }
   10698 	}
   10699 
   10700       free (extversym);
   10701       free (isymbuf);
   10702     }
   10703 
   10704   return false;
   10705 }
   10706 
   10707 /* Convert ELF common symbol TYPE.  */
   10708 
   10709 static int
   10710 elf_link_convert_common_type (struct bfd_link_info *info, int type)
   10711 {
   10712   /* Commom symbol can only appear in relocatable link.  */
   10713   if (!bfd_link_relocatable (info))
   10714     abort ();
   10715   switch (info->elf_stt_common)
   10716     {
   10717     case unchanged:
   10718       break;
   10719     case elf_stt_common:
   10720       type = STT_COMMON;
   10721       break;
   10722     case no_elf_stt_common:
   10723       type = STT_OBJECT;
   10724       break;
   10725     }
   10726   return type;
   10727 }
   10728 
   10729 /* Add an external symbol to the symbol table.  This is called from
   10730    the hash table traversal routine.  When generating a shared object,
   10731    we go through the symbol table twice.  The first time we output
   10732    anything that might have been forced to local scope in a version
   10733    script.  The second time we output the symbols that are still
   10734    global symbols.  */
   10735 
   10736 static bool
   10737 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   10738 {
   10739   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   10740   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   10741   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   10742   bool strip;
   10743   Elf_Internal_Sym sym;
   10744   asection *input_sec;
   10745   const struct elf_backend_data *bed;
   10746   long indx;
   10747   int ret;
   10748   unsigned int type;
   10749 
   10750   if (h->root.type == bfd_link_hash_warning)
   10751     {
   10752       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10753       if (h->root.type == bfd_link_hash_new)
   10754 	return true;
   10755     }
   10756 
   10757   /* Decide whether to output this symbol in this pass.  */
   10758   if (eoinfo->localsyms)
   10759     {
   10760       if (!h->forced_local)
   10761 	return true;
   10762     }
   10763   else
   10764     {
   10765       if (h->forced_local)
   10766 	return true;
   10767     }
   10768 
   10769   bed = get_elf_backend_data (flinfo->output_bfd);
   10770 
   10771   if (h->root.type == bfd_link_hash_undefined)
   10772     {
   10773       /* If we have an undefined symbol reference here then it must have
   10774 	 come from a shared library that is being linked in.  (Undefined
   10775 	 references in regular files have already been handled unless
   10776 	 they are in unreferenced sections which are removed by garbage
   10777 	 collection).  */
   10778       bool ignore_undef = false;
   10779 
   10780       /* Some symbols may be special in that the fact that they're
   10781 	 undefined can be safely ignored - let backend determine that.  */
   10782       if (bed->elf_backend_ignore_undef_symbol)
   10783 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   10784 
   10785       /* If we are reporting errors for this situation then do so now.  */
   10786       if (!ignore_undef
   10787 	  && h->ref_dynamic_nonweak
   10788 	  && (!h->ref_regular || flinfo->info->gc_sections)
   10789 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   10790 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   10791 	{
   10792 	  flinfo->info->callbacks->undefined_symbol
   10793 	    (flinfo->info, h->root.root.string,
   10794 	     h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
   10795 	     flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
   10796 	     && !flinfo->info->warn_unresolved_syms);
   10797 	}
   10798 
   10799       /* Strip a global symbol defined in a discarded section.  */
   10800       if (h->indx == -3)
   10801 	return true;
   10802     }
   10803 
   10804   /* We should also warn if a forced local symbol is referenced from
   10805      shared libraries.  */
   10806   if (bfd_link_executable (flinfo->info)
   10807       && h->forced_local
   10808       && h->ref_dynamic
   10809       && h->def_regular
   10810       && !h->dynamic_def
   10811       && h->ref_dynamic_nonweak
   10812       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   10813     {
   10814       bfd *def_bfd;
   10815       const char *msg;
   10816       struct elf_link_hash_entry *hi = h;
   10817 
   10818       /* Check indirect symbol.  */
   10819       while (hi->root.type == bfd_link_hash_indirect)
   10820 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   10821 
   10822       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   10823 	/* xgettext:c-format */
   10824 	msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
   10825       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   10826 	/* xgettext:c-format */
   10827 	msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
   10828       else
   10829 	/* xgettext:c-format */
   10830 	msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
   10831       def_bfd = flinfo->output_bfd;
   10832       if (hi->root.u.def.section != bfd_abs_section_ptr)
   10833 	def_bfd = hi->root.u.def.section->owner;
   10834       _bfd_error_handler (msg, flinfo->output_bfd,
   10835 			  h->root.root.string, def_bfd);
   10836       bfd_set_error (bfd_error_bad_value);
   10837       eoinfo->failed = true;
   10838       return false;
   10839     }
   10840 
   10841   /* We don't want to output symbols that have never been mentioned by
   10842      a regular file, or that we have been told to strip.  However, if
   10843      h->indx is set to -2, the symbol is used by a reloc and we must
   10844      output it.  */
   10845   strip = false;
   10846   if (h->indx == -2)
   10847     ;
   10848   else if ((h->def_dynamic
   10849 	    || h->ref_dynamic
   10850 	    || h->root.type == bfd_link_hash_new)
   10851 	   && !h->def_regular
   10852 	   && !h->ref_regular)
   10853     strip = true;
   10854   else if (flinfo->info->strip == strip_all)
   10855     strip = true;
   10856   else if (flinfo->info->strip == strip_some
   10857 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   10858 			       h->root.root.string, false, false) == NULL)
   10859     strip = true;
   10860   else if ((h->root.type == bfd_link_hash_defined
   10861 	    || h->root.type == bfd_link_hash_defweak)
   10862 	   && ((flinfo->info->strip_discarded
   10863 		&& discarded_section (h->root.u.def.section))
   10864 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   10865 		   && h->root.u.def.section->owner != NULL
   10866 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   10867     strip = true;
   10868   else if ((h->root.type == bfd_link_hash_undefined
   10869 	    || h->root.type == bfd_link_hash_undefweak)
   10870 	   && h->root.u.undef.abfd != NULL
   10871 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   10872     strip = true;
   10873 
   10874   /* Remember if this symbol should be stripped.  */
   10875   bool should_strip = strip;
   10876 
   10877   /* Strip undefined weak symbols link if they don't have relocation.  */
   10878   if (!strip)
   10879     strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
   10880 
   10881   type = h->type;
   10882 
   10883   /* If we're stripping it, and it's not a dynamic symbol, there's
   10884      nothing else to do.   However, if it is a forced local symbol or
   10885      an ifunc symbol we need to give the backend finish_dynamic_symbol
   10886      function a chance to make it dynamic.  */
   10887   if (strip
   10888       && h->dynindx == -1
   10889       && type != STT_GNU_IFUNC
   10890       && !h->forced_local)
   10891     return true;
   10892 
   10893   sym.st_value = 0;
   10894   sym.st_size = h->size;
   10895   sym.st_other = h->other;
   10896   switch (h->root.type)
   10897     {
   10898     default:
   10899     case bfd_link_hash_new:
   10900     case bfd_link_hash_warning:
   10901       abort ();
   10902       return false;
   10903 
   10904     case bfd_link_hash_undefined:
   10905     case bfd_link_hash_undefweak:
   10906       input_sec = bfd_und_section_ptr;
   10907       sym.st_shndx = SHN_UNDEF;
   10908       break;
   10909 
   10910     case bfd_link_hash_defined:
   10911     case bfd_link_hash_defweak:
   10912       {
   10913 	input_sec = h->root.u.def.section;
   10914 	if (input_sec->output_section != NULL)
   10915 	  {
   10916 	    sym.st_shndx =
   10917 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   10918 						 input_sec->output_section);
   10919 	    if (sym.st_shndx == SHN_BAD)
   10920 	      {
   10921 		_bfd_error_handler
   10922 		  /* xgettext:c-format */
   10923 		  (_("%pB: could not find output section %pA for input section %pA"),
   10924 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   10925 		bfd_set_error (bfd_error_nonrepresentable_section);
   10926 		eoinfo->failed = true;
   10927 		return false;
   10928 	      }
   10929 
   10930 	    /* ELF symbols in relocatable files are section relative,
   10931 	       but in nonrelocatable files they are virtual
   10932 	       addresses.  */
   10933 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   10934 	    if (!bfd_link_relocatable (flinfo->info))
   10935 	      {
   10936 		sym.st_value += input_sec->output_section->vma;
   10937 		if (h->type == STT_TLS)
   10938 		  {
   10939 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   10940 		    if (tls_sec != NULL)
   10941 		      sym.st_value -= tls_sec->vma;
   10942 		  }
   10943 	      }
   10944 	  }
   10945 	else
   10946 	  {
   10947 	    BFD_ASSERT (input_sec->owner == NULL
   10948 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   10949 	    sym.st_shndx = SHN_UNDEF;
   10950 	    input_sec = bfd_und_section_ptr;
   10951 	  }
   10952       }
   10953       break;
   10954 
   10955     case bfd_link_hash_common:
   10956       input_sec = h->root.u.c.p->section;
   10957       sym.st_shndx = bed->common_section_index (input_sec);
   10958       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   10959       break;
   10960 
   10961     case bfd_link_hash_indirect:
   10962       /* These symbols are created by symbol versioning.  They point
   10963 	 to the decorated version of the name.  For example, if the
   10964 	 symbol foo@@GNU_1.2 is the default, which should be used when
   10965 	 foo is used with no version, then we add an indirect symbol
   10966 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   10967 	 since the indirected symbol is already in the hash table.  */
   10968       return true;
   10969     }
   10970 
   10971   if (type == STT_COMMON || type == STT_OBJECT)
   10972     switch (h->root.type)
   10973       {
   10974       case bfd_link_hash_common:
   10975 	type = elf_link_convert_common_type (flinfo->info, type);
   10976 	break;
   10977       case bfd_link_hash_defined:
   10978       case bfd_link_hash_defweak:
   10979 	if (bed->common_definition (&sym))
   10980 	  type = elf_link_convert_common_type (flinfo->info, type);
   10981 	else
   10982 	  type = STT_OBJECT;
   10983 	break;
   10984       case bfd_link_hash_undefined:
   10985       case bfd_link_hash_undefweak:
   10986 	break;
   10987       default:
   10988 	abort ();
   10989       }
   10990 
   10991   if (h->forced_local)
   10992     {
   10993       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
   10994       /* Turn off visibility on local symbol.  */
   10995       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   10996     }
   10997   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   10998   else if (h->unique_global && h->def_regular)
   10999     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
   11000   else if (h->root.type == bfd_link_hash_undefweak
   11001 	   || h->root.type == bfd_link_hash_defweak)
   11002     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
   11003   else
   11004     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   11005   sym.st_target_internal = h->target_internal;
   11006 
   11007   /* Give the processor backend a chance to tweak the symbol value,
   11008      and also to finish up anything that needs to be done for this
   11009      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   11010      forced local syms when non-shared is due to a historical quirk.
   11011      STT_GNU_IFUNC symbol must go through PLT.  */
   11012   if ((h->type == STT_GNU_IFUNC
   11013        && h->def_regular
   11014        && !bfd_link_relocatable (flinfo->info))
   11015       || ((h->dynindx != -1
   11016 	   || h->forced_local)
   11017 	  && ((bfd_link_pic (flinfo->info)
   11018 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   11019 		   || h->root.type != bfd_link_hash_undefweak))
   11020 	      || !h->forced_local)
   11021 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   11022     {
   11023       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   11024 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   11025 	{
   11026 	  eoinfo->failed = true;
   11027 	  return false;
   11028 	}
   11029       /* If a symbol is in the dynamic symbol table and isn't a
   11030 	 should-strip symbol, also keep it in the symbol table.  */
   11031       if (!should_strip)
   11032 	strip = false;
   11033     }
   11034 
   11035   /* If we are marking the symbol as undefined, and there are no
   11036      non-weak references to this symbol from a regular object, then
   11037      mark the symbol as weak undefined; if there are non-weak
   11038      references, mark the symbol as strong.  We can't do this earlier,
   11039      because it might not be marked as undefined until the
   11040      finish_dynamic_symbol routine gets through with it.  */
   11041   if (sym.st_shndx == SHN_UNDEF
   11042       && h->ref_regular
   11043       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   11044 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   11045     {
   11046       int bindtype;
   11047       type = ELF_ST_TYPE (sym.st_info);
   11048 
   11049       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   11050       if (type == STT_GNU_IFUNC)
   11051 	type = STT_FUNC;
   11052 
   11053       if (h->ref_regular_nonweak)
   11054 	bindtype = STB_GLOBAL;
   11055       else
   11056 	bindtype = STB_WEAK;
   11057       sym.st_info = ELF_ST_INFO (bindtype, type);
   11058     }
   11059 
   11060   /* If this is a symbol defined in a dynamic library, don't use the
   11061      symbol size from the dynamic library.  Relinking an executable
   11062      against a new library may introduce gratuitous changes in the
   11063      executable's symbols if we keep the size.  */
   11064   if (sym.st_shndx == SHN_UNDEF
   11065       && !h->def_regular
   11066       && h->def_dynamic)
   11067     sym.st_size = 0;
   11068 
   11069   /* If a non-weak symbol with non-default visibility is not defined
   11070      locally, it is a fatal error.  */
   11071   if (!bfd_link_relocatable (flinfo->info)
   11072       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   11073       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   11074       && h->root.type == bfd_link_hash_undefined
   11075       && !h->def_regular)
   11076     {
   11077       const char *msg;
   11078 
   11079       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   11080 	/* xgettext:c-format */
   11081 	msg = _("%pB: protected symbol `%s' isn't defined");
   11082       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   11083 	/* xgettext:c-format */
   11084 	msg = _("%pB: internal symbol `%s' isn't defined");
   11085       else
   11086 	/* xgettext:c-format */
   11087 	msg = _("%pB: hidden symbol `%s' isn't defined");
   11088       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
   11089       bfd_set_error (bfd_error_bad_value);
   11090       eoinfo->failed = true;
   11091       return false;
   11092     }
   11093 
   11094   /* If this symbol should be put in the .dynsym section, then put it
   11095      there now.  We already know the symbol index.  We also fill in
   11096      the entry in the .hash section.  */
   11097   if (h->dynindx != -1
   11098       && elf_hash_table (flinfo->info)->dynamic_sections_created
   11099       && elf_hash_table (flinfo->info)->dynsym != NULL
   11100       && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
   11101     {
   11102       bfd_byte *esym;
   11103 
   11104       /* Since there is no version information in the dynamic string,
   11105 	 if there is no version info in symbol version section, we will
   11106 	 have a run-time problem if not linking executable, referenced
   11107 	 by shared library, or not bound locally.  */
   11108       if (h->verinfo.verdef == NULL
   11109 	  && (!bfd_link_executable (flinfo->info)
   11110 	      || h->ref_dynamic
   11111 	      || !h->def_regular))
   11112 	{
   11113 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   11114 
   11115 	  if (p && p [1] != '\0')
   11116 	    {
   11117 	      _bfd_error_handler
   11118 		/* xgettext:c-format */
   11119 		(_("%pB: no symbol version section for versioned symbol `%s'"),
   11120 		 flinfo->output_bfd, h->root.root.string);
   11121 	      eoinfo->failed = true;
   11122 	      return false;
   11123 	    }
   11124 	}
   11125 
   11126       sym.st_name = h->dynstr_index;
   11127       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   11128 	      + h->dynindx * bed->s->sizeof_sym);
   11129       if (!check_dynsym (flinfo->output_bfd, &sym))
   11130 	{
   11131 	  eoinfo->failed = true;
   11132 	  return false;
   11133 	}
   11134 
   11135       /* Inform the linker of the addition of this symbol.  */
   11136 
   11137       if (flinfo->info->callbacks->ctf_new_dynsym)
   11138 	flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
   11139 
   11140       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   11141 
   11142       if (flinfo->hash_sec != NULL)
   11143 	{
   11144 	  size_t hash_entry_size;
   11145 	  bfd_byte *bucketpos;
   11146 	  bfd_vma chain;
   11147 	  size_t bucketcount;
   11148 	  size_t bucket;
   11149 
   11150 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   11151 	  bucket = h->u.elf_hash_value % bucketcount;
   11152 
   11153 	  hash_entry_size
   11154 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   11155 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   11156 		       + (bucket + 2) * hash_entry_size);
   11157 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   11158 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   11159 		   bucketpos);
   11160 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   11161 		   ((bfd_byte *) flinfo->hash_sec->contents
   11162 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   11163 	}
   11164 
   11165       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   11166 	{
   11167 	  Elf_Internal_Versym iversym;
   11168 	  Elf_External_Versym *eversym;
   11169 
   11170 	  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   11171 	    {
   11172 	      if (h->verinfo.verdef == NULL
   11173 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   11174 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   11175 		iversym.vs_vers = 1;
   11176 	      else
   11177 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   11178 	    }
   11179 	  else
   11180 	    {
   11181 	      if (h->verinfo.vertree == NULL)
   11182 		iversym.vs_vers = 1;
   11183 	      else
   11184 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   11185 	      if (flinfo->info->create_default_symver)
   11186 		iversym.vs_vers++;
   11187 	    }
   11188 
   11189 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
   11190 	     defined locally.  */
   11191 	  if (h->versioned == versioned_hidden && h->def_regular)
   11192 	    iversym.vs_vers |= VERSYM_HIDDEN;
   11193 
   11194 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   11195 	  eversym += h->dynindx;
   11196 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   11197 	}
   11198     }
   11199 
   11200   /* If the symbol is undefined, and we didn't output it to .dynsym,
   11201      strip it from .symtab too.  Obviously we can't do this for
   11202      relocatable output or when needed for --emit-relocs.  */
   11203   else if (input_sec == bfd_und_section_ptr
   11204 	   && h->indx != -2
   11205 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
   11206 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
   11207 	   && !bfd_link_relocatable (flinfo->info))
   11208     return true;
   11209 
   11210   /* Also strip others that we couldn't earlier due to dynamic symbol
   11211      processing.  */
   11212   if (strip)
   11213     return true;
   11214   if ((input_sec->flags & SEC_EXCLUDE) != 0)
   11215     return true;
   11216 
   11217   /* Output a FILE symbol so that following locals are not associated
   11218      with the wrong input file.  We need one for forced local symbols
   11219      if we've seen more than one FILE symbol or when we have exactly
   11220      one FILE symbol but global symbols are present in a file other
   11221      than the one with the FILE symbol.  We also need one if linker
   11222      defined symbols are present.  In practice these conditions are
   11223      always met, so just emit the FILE symbol unconditionally.  */
   11224   if (eoinfo->localsyms
   11225       && !eoinfo->file_sym_done
   11226       && eoinfo->flinfo->filesym_count != 0)
   11227     {
   11228       Elf_Internal_Sym fsym;
   11229 
   11230       memset (&fsym, 0, sizeof (fsym));
   11231       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   11232       fsym.st_shndx = SHN_ABS;
   11233       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   11234 				      bfd_und_section_ptr, NULL))
   11235 	return false;
   11236 
   11237       eoinfo->file_sym_done = true;
   11238     }
   11239 
   11240   indx = bfd_get_symcount (flinfo->output_bfd);
   11241   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   11242 				   input_sec, h);
   11243   if (ret == 0)
   11244     {
   11245       eoinfo->failed = true;
   11246       return false;
   11247     }
   11248   else if (ret == 1)
   11249     h->indx = indx;
   11250   else if (h->indx == -2)
   11251     abort();
   11252 
   11253   return true;
   11254 }
   11255 
   11256 /* Return TRUE if special handling is done for relocs in SEC against
   11257    symbols defined in discarded sections.  */
   11258 
   11259 static bool
   11260 elf_section_ignore_discarded_relocs (asection *sec)
   11261 {
   11262   const struct elf_backend_data *bed;
   11263 
   11264   switch (sec->sec_info_type)
   11265     {
   11266     case SEC_INFO_TYPE_STABS:
   11267     case SEC_INFO_TYPE_EH_FRAME:
   11268     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   11269     case SEC_INFO_TYPE_SFRAME:
   11270       return true;
   11271     default:
   11272       break;
   11273     }
   11274 
   11275   bed = get_elf_backend_data (sec->owner);
   11276   if (bed->elf_backend_ignore_discarded_relocs != NULL
   11277       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   11278     return true;
   11279 
   11280   return false;
   11281 }
   11282 
   11283 /* Return a mask saying how ld should treat relocations in SEC against
   11284    symbols defined in discarded sections.  If this function returns
   11285    COMPLAIN set, ld will issue a warning message.  If this function
   11286    returns PRETEND set, and the discarded section was link-once and the
   11287    same size as the kept link-once section, ld will pretend that the
   11288    symbol was actually defined in the kept section.  Otherwise ld will
   11289    zero the reloc (at least that is the intent, but some cooperation by
   11290    the target dependent code is needed, particularly for REL targets).  */
   11291 
   11292 unsigned int
   11293 _bfd_elf_default_action_discarded (asection *sec)
   11294 {
   11295   const struct elf_backend_data *bed;
   11296   bed = get_elf_backend_data (sec->owner);
   11297 
   11298   if (sec->flags & SEC_DEBUGGING)
   11299     return PRETEND;
   11300 
   11301   if (strcmp (".eh_frame", sec->name) == 0)
   11302     return 0;
   11303 
   11304   if (bed->elf_backend_can_make_multiple_eh_frame
   11305       && strncmp (sec->name, ".eh_frame.", 10) == 0)
   11306     return 0;
   11307 
   11308   if (elf_section_type (sec) == SHT_GNU_SFRAME)
   11309     return 0;
   11310 
   11311   if (strcmp (".gcc_except_table", sec->name) == 0)
   11312     return 0;
   11313 
   11314   return COMPLAIN | PRETEND;
   11315 }
   11316 
   11317 /* Find a match between a section and a member of a section group.  */
   11318 
   11319 static asection *
   11320 match_group_member (asection *sec, asection *group,
   11321 		    struct bfd_link_info *info)
   11322 {
   11323   asection *first = elf_next_in_group (group);
   11324   asection *s = first;
   11325 
   11326   while (s != NULL)
   11327     {
   11328       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   11329 	return s;
   11330 
   11331       s = elf_next_in_group (s);
   11332       if (s == first)
   11333 	break;
   11334     }
   11335 
   11336   return NULL;
   11337 }
   11338 
   11339 /* Check if the kept section of a discarded section SEC can be used
   11340    to replace it.  Return the replacement if it is OK.  Otherwise return
   11341    NULL.  */
   11342 
   11343 asection *
   11344 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   11345 {
   11346   asection *kept;
   11347 
   11348   kept = sec->kept_section;
   11349   if (kept != NULL)
   11350     {
   11351       if ((kept->flags & SEC_GROUP) != 0)
   11352 	kept = match_group_member (sec, kept, info);
   11353       if (kept != NULL)
   11354 	{
   11355 	  if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   11356 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size))
   11357 	    kept = NULL;
   11358 	  else
   11359 	    {
   11360 	      /* Get the real kept section.  */
   11361 	      asection *next;
   11362 	      for (next = kept->kept_section;
   11363 		   next != NULL;
   11364 		   next = next->kept_section)
   11365 		kept = next;
   11366 	    }
   11367 	}
   11368       sec->kept_section = kept;
   11369     }
   11370   return kept;
   11371 }
   11372 
   11373 /* Link an input file into the linker output file.  This function
   11374    handles all the sections and relocations of the input file at once.
   11375    This is so that we only have to read the local symbols once, and
   11376    don't have to keep them in memory.  */
   11377 
   11378 static bool
   11379 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   11380 {
   11381   int (*relocate_section)
   11382     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   11383      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   11384   bfd *output_bfd;
   11385   Elf_Internal_Shdr *symtab_hdr;
   11386   size_t locsymcount;
   11387   size_t extsymoff;
   11388   Elf_Internal_Sym *isymbuf;
   11389   Elf_Internal_Sym *isym;
   11390   Elf_Internal_Sym *isymend;
   11391   long *pindex;
   11392   asection **ppsection;
   11393   asection *o;
   11394   const struct elf_backend_data *bed;
   11395   struct elf_link_hash_entry **sym_hashes;
   11396   bfd_size_type address_size;
   11397   bfd_vma r_type_mask;
   11398   int r_sym_shift;
   11399   bool have_file_sym = false;
   11400 
   11401   output_bfd = flinfo->output_bfd;
   11402   bed = get_elf_backend_data (output_bfd);
   11403   relocate_section = bed->elf_backend_relocate_section;
   11404 
   11405   /* If this is a dynamic object, we don't want to do anything here:
   11406      we don't want the local symbols, and we don't want the section
   11407      contents.  */
   11408   if ((input_bfd->flags & DYNAMIC) != 0)
   11409     return true;
   11410 
   11411   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   11412   if (elf_bad_symtab (input_bfd))
   11413     {
   11414       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   11415       extsymoff = 0;
   11416     }
   11417   else
   11418     {
   11419       locsymcount = symtab_hdr->sh_info;
   11420       extsymoff = symtab_hdr->sh_info;
   11421     }
   11422 
   11423   /* Enable GNU OSABI features in the output BFD that are used in the input
   11424      BFD.  */
   11425   if (bed->elf_osabi == ELFOSABI_NONE
   11426       || bed->elf_osabi == ELFOSABI_GNU
   11427       || bed->elf_osabi == ELFOSABI_FREEBSD)
   11428     elf_tdata (output_bfd)->has_gnu_osabi
   11429       |= (elf_tdata (input_bfd)->has_gnu_osabi
   11430 	  & (bfd_link_relocatable (flinfo->info)
   11431 	     ? -1 : ~elf_gnu_osabi_retain));
   11432 
   11433   /* Read the local symbols.  */
   11434   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   11435   if (isymbuf == NULL && locsymcount != 0)
   11436     {
   11437       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   11438 				      flinfo->internal_syms,
   11439 				      flinfo->external_syms,
   11440 				      flinfo->locsym_shndx);
   11441       if (isymbuf == NULL)
   11442 	return false;
   11443     }
   11444 
   11445   /* Find local symbol sections and adjust values of symbols in
   11446      SEC_MERGE sections.  Write out those local symbols we know are
   11447      going into the output file.  */
   11448   isymend = PTR_ADD (isymbuf, locsymcount);
   11449   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   11450        isym < isymend;
   11451        isym++, pindex++, ppsection++)
   11452     {
   11453       asection *isec;
   11454       const char *name;
   11455       Elf_Internal_Sym osym;
   11456       long indx;
   11457       int ret;
   11458 
   11459       *pindex = -1;
   11460 
   11461       if (elf_bad_symtab (input_bfd))
   11462 	{
   11463 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   11464 	    {
   11465 	      *ppsection = NULL;
   11466 	      continue;
   11467 	    }
   11468 	}
   11469 
   11470       if (isym->st_shndx == SHN_UNDEF)
   11471 	isec = bfd_und_section_ptr;
   11472       else if (isym->st_shndx == SHN_ABS)
   11473 	isec = bfd_abs_section_ptr;
   11474       else if (isym->st_shndx == SHN_COMMON)
   11475 	isec = bfd_com_section_ptr;
   11476       else
   11477 	{
   11478 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   11479 	  if (isec == NULL)
   11480 	    {
   11481 	      /* Don't attempt to output symbols with st_shnx in the
   11482 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   11483 	      isec = bfd_und_section_ptr;
   11484 	    }
   11485 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   11486 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   11487 	    isym->st_value =
   11488 	      _bfd_merged_section_offset (output_bfd, &isec,
   11489 					  elf_section_data (isec)->sec_info,
   11490 					  isym->st_value);
   11491 	}
   11492 
   11493       *ppsection = isec;
   11494 
   11495       /* Don't output the first, undefined, symbol.  In fact, don't
   11496 	 output any undefined local symbol.  */
   11497       if (isec == bfd_und_section_ptr)
   11498 	continue;
   11499 
   11500       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   11501 	{
   11502 	  /* We never output section symbols.  Instead, we use the
   11503 	     section symbol of the corresponding section in the output
   11504 	     file.  */
   11505 	  continue;
   11506 	}
   11507 
   11508       /* If we are stripping all symbols, we don't want to output this
   11509 	 one.  */
   11510       if (flinfo->info->strip == strip_all)
   11511 	continue;
   11512 
   11513       /* If we are discarding all local symbols, we don't want to
   11514 	 output this one.  If we are generating a relocatable output
   11515 	 file, then some of the local symbols may be required by
   11516 	 relocs; we output them below as we discover that they are
   11517 	 needed.  */
   11518       if (flinfo->info->discard == discard_all)
   11519 	continue;
   11520 
   11521       /* If this symbol is defined in a section which we are
   11522 	 discarding, we don't need to keep it.  */
   11523       if (isym->st_shndx < SHN_LORESERVE
   11524 	  && (isec->output_section == NULL
   11525 	      || bfd_section_removed_from_list (output_bfd,
   11526 						isec->output_section)))
   11527 	continue;
   11528 
   11529       /* Get the name of the symbol.  */
   11530       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   11531 					      isym->st_name);
   11532       if (name == NULL)
   11533 	return false;
   11534 
   11535       /* See if we are discarding symbols with this name.  */
   11536       if ((flinfo->info->strip == strip_some
   11537 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
   11538 	       == NULL))
   11539 	  || (((flinfo->info->discard == discard_sec_merge
   11540 		&& (isec->flags & SEC_MERGE)
   11541 		&& !bfd_link_relocatable (flinfo->info))
   11542 	       || flinfo->info->discard == discard_l)
   11543 	      && bfd_is_local_label_name (input_bfd, name)))
   11544 	continue;
   11545 
   11546       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   11547 	{
   11548 	  if (input_bfd->lto_output)
   11549 	    /* -flto puts a temp file name here.  This means builds
   11550 	       are not reproducible.  Discard the symbol.  */
   11551 	    continue;
   11552 	  have_file_sym = true;
   11553 	  flinfo->filesym_count += 1;
   11554 	}
   11555       if (!have_file_sym)
   11556 	{
   11557 	  /* In the absence of debug info, bfd_find_nearest_line uses
   11558 	     FILE symbols to determine the source file for local
   11559 	     function symbols.  Provide a FILE symbol here if input
   11560 	     files lack such, so that their symbols won't be
   11561 	     associated with a previous input file.  It's not the
   11562 	     source file, but the best we can do.  */
   11563 	  const char *filename;
   11564 	  have_file_sym = true;
   11565 	  flinfo->filesym_count += 1;
   11566 	  memset (&osym, 0, sizeof (osym));
   11567 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   11568 	  osym.st_shndx = SHN_ABS;
   11569 	  if (input_bfd->lto_output)
   11570 	    filename = NULL;
   11571 	  else
   11572 	    filename = lbasename (bfd_get_filename (input_bfd));
   11573 	  if (!elf_link_output_symstrtab (flinfo, filename, &osym,
   11574 					  bfd_abs_section_ptr, NULL))
   11575 	    return false;
   11576 	}
   11577 
   11578       osym = *isym;
   11579 
   11580       /* Adjust the section index for the output file.  */
   11581       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11582 							 isec->output_section);
   11583       if (osym.st_shndx == SHN_BAD)
   11584 	return false;
   11585 
   11586       /* ELF symbols in relocatable files are section relative, but
   11587 	 in executable files they are virtual addresses.  Note that
   11588 	 this code assumes that all ELF sections have an associated
   11589 	 BFD section with a reasonable value for output_offset; below
   11590 	 we assume that they also have a reasonable value for
   11591 	 output_section.  Any special sections must be set up to meet
   11592 	 these requirements.  */
   11593       osym.st_value += isec->output_offset;
   11594       if (!bfd_link_relocatable (flinfo->info))
   11595 	{
   11596 	  osym.st_value += isec->output_section->vma;
   11597 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   11598 	    {
   11599 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   11600 	      if (elf_hash_table (flinfo->info)->tls_sec != NULL)
   11601 		osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   11602 	      else
   11603 		osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
   11604 					    STT_NOTYPE);
   11605 	    }
   11606 	}
   11607 
   11608       indx = bfd_get_symcount (output_bfd);
   11609       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   11610       if (ret == 0)
   11611 	return false;
   11612       else if (ret == 1)
   11613 	*pindex = indx;
   11614     }
   11615 
   11616   if (bed->s->arch_size == 32)
   11617     {
   11618       r_type_mask = 0xff;
   11619       r_sym_shift = 8;
   11620       address_size = 4;
   11621     }
   11622   else
   11623     {
   11624       r_type_mask = 0xffffffff;
   11625       r_sym_shift = 32;
   11626       address_size = 8;
   11627     }
   11628 
   11629   /* Relocate the contents of each section.  */
   11630   sym_hashes = elf_sym_hashes (input_bfd);
   11631   for (o = input_bfd->sections; o != NULL; o = o->next)
   11632     {
   11633       bfd_byte *contents;
   11634 
   11635       if (! o->linker_mark)
   11636 	{
   11637 	  /* This section was omitted from the link.  */
   11638 	  continue;
   11639 	}
   11640 
   11641       if (!flinfo->info->resolve_section_groups
   11642 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   11643 	{
   11644 	  /* Deal with the group signature symbol.  */
   11645 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   11646 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   11647 	  asection *osec = o->output_section;
   11648 
   11649 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
   11650 	  if (symndx >= locsymcount
   11651 	      || (elf_bad_symtab (input_bfd)
   11652 		  && flinfo->sections[symndx] == NULL))
   11653 	    {
   11654 	      struct elf_link_hash_entry *h;
   11655 
   11656 	      h = get_link_hash_entry (sym_hashes, symndx, extsymoff);
   11657 	      if (h == NULL)
   11658 		{
   11659 		  _bfd_error_handler
   11660 		    /* xgettext:c-format */
   11661 		    (_("error: %pB: unable to create group section symbol"),
   11662 		     input_bfd);
   11663 		  bfd_set_error (bfd_error_bad_value);
   11664 		  return false;
   11665 		}
   11666 
   11667 	      /* Arrange for symbol to be output.  */
   11668 	      h->indx = -2;
   11669 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   11670 	    }
   11671 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   11672 	    {
   11673 	      /* We'll use the output section target_index.  */
   11674 	      asection *sec = flinfo->sections[symndx]->output_section;
   11675 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   11676 	    }
   11677 	  else
   11678 	    {
   11679 	      if (flinfo->indices[symndx] == -1)
   11680 		{
   11681 		  /* Otherwise output the local symbol now.  */
   11682 		  Elf_Internal_Sym sym = isymbuf[symndx];
   11683 		  asection *sec = flinfo->sections[symndx]->output_section;
   11684 		  const char *name;
   11685 		  long indx;
   11686 		  int ret;
   11687 
   11688 		  name = bfd_elf_string_from_elf_section (input_bfd,
   11689 							  symtab_hdr->sh_link,
   11690 							  sym.st_name);
   11691 		  if (name == NULL)
   11692 		    return false;
   11693 
   11694 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11695 								    sec);
   11696 		  if (sym.st_shndx == SHN_BAD)
   11697 		    return false;
   11698 
   11699 		  sym.st_value += o->output_offset;
   11700 
   11701 		  indx = bfd_get_symcount (output_bfd);
   11702 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   11703 						   NULL);
   11704 		  if (ret == 0)
   11705 		    return false;
   11706 		  else if (ret == 1)
   11707 		    flinfo->indices[symndx] = indx;
   11708 		  else
   11709 		    abort ();
   11710 		}
   11711 	      elf_section_data (osec)->this_hdr.sh_info
   11712 		= flinfo->indices[symndx];
   11713 	    }
   11714 	}
   11715 
   11716       if ((o->flags & SEC_HAS_CONTENTS) == 0
   11717 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   11718 	continue;
   11719 
   11720       if ((o->flags & SEC_LINKER_CREATED) != 0)
   11721 	{
   11722 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   11723 	     or somesuch.  */
   11724 	  continue;
   11725 	}
   11726 
   11727       /* Get the contents of the section.  They have been cached by a
   11728 	 relaxation routine.  Note that o is a section in an input
   11729 	 file, so the contents field will not have been set by any of
   11730 	 the routines which work on output files.  */
   11731       if (elf_section_data (o)->this_hdr.contents != NULL)
   11732 	{
   11733 	  contents = elf_section_data (o)->this_hdr.contents;
   11734 	  if (bed->caches_rawsize
   11735 	      && o->rawsize != 0
   11736 	      && o->rawsize < o->size)
   11737 	    {
   11738 	      memcpy (flinfo->contents, contents, o->rawsize);
   11739 	      contents = flinfo->contents;
   11740 	    }
   11741 	}
   11742       else if (!(o->flags & SEC_RELOC)
   11743 	       && !bed->elf_backend_write_section
   11744 	       && o->sec_info_type == SEC_INFO_TYPE_MERGE)
   11745 	/* A MERGE section that has no relocations doesn't need the
   11746 	   contents anymore, they have been recorded earlier.  Except
   11747 	   if the backend has special provisions for writing sections.  */
   11748 	contents = NULL;
   11749       else
   11750 	{
   11751 	  contents = flinfo->contents;
   11752 	  if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
   11753 						     &contents))
   11754 	    return false;
   11755 	}
   11756 
   11757       if ((o->flags & SEC_RELOC) != 0)
   11758 	{
   11759 	  Elf_Internal_Rela *internal_relocs;
   11760 	  Elf_Internal_Rela *rel, *relend;
   11761 	  int action_discarded;
   11762 	  int ret;
   11763 
   11764 	  /* Get the swapped relocs.  */
   11765 	  internal_relocs
   11766 	    = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
   11767 					      flinfo->external_relocs,
   11768 					      flinfo->internal_relocs,
   11769 					      false);
   11770 	  if (internal_relocs == NULL
   11771 	      && o->reloc_count > 0)
   11772 	    return false;
   11773 
   11774 	  action_discarded = -1;
   11775 	  if (!elf_section_ignore_discarded_relocs (o))
   11776 	    action_discarded = (*bed->action_discarded) (o);
   11777 
   11778 	  /* Run through the relocs evaluating complex reloc symbols and
   11779 	     looking for relocs against symbols from discarded sections
   11780 	     or section symbols from removed link-once sections.
   11781 	     Complain about relocs against discarded sections.  Zero
   11782 	     relocs against removed link-once sections.  */
   11783 
   11784 	  rel = internal_relocs;
   11785 	  relend = rel + o->reloc_count;
   11786 	  for ( ; rel < relend; rel++)
   11787 	    {
   11788 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   11789 	      unsigned int s_type;
   11790 	      asection **ps, *sec;
   11791 	      struct elf_link_hash_entry *h = NULL;
   11792 	      const char *sym_name;
   11793 
   11794 	      if (r_symndx == STN_UNDEF)
   11795 		continue;
   11796 
   11797 	      if (r_symndx >= locsymcount
   11798 		  || (elf_bad_symtab (input_bfd)
   11799 		      && flinfo->sections[r_symndx] == NULL))
   11800 		{
   11801 		  h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff);
   11802 
   11803 		  /* Badly formatted input files can contain relocs that
   11804 		     reference non-existant symbols.  Check here so that
   11805 		     we do not seg fault.  */
   11806 		  if (h == NULL)
   11807 		    {
   11808 		      _bfd_error_handler
   11809 			/* xgettext:c-format */
   11810 			(_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
   11811 			   "that references a non-existent global symbol"),
   11812 			 input_bfd, (uint64_t) rel->r_info, o);
   11813 		      bfd_set_error (bfd_error_bad_value);
   11814 		      return false;
   11815 		    }
   11816 
   11817 		  s_type = h->type;
   11818 
   11819 		  /* If a plugin symbol is referenced from a non-IR file,
   11820 		     mark the symbol as undefined.  Note that the
   11821 		     linker may attach linker created dynamic sections
   11822 		     to the plugin bfd.  Symbols defined in linker
   11823 		     created sections are not plugin symbols.  */
   11824 		  if ((h->root.non_ir_ref_regular
   11825 		       || h->root.non_ir_ref_dynamic)
   11826 		      && (h->root.type == bfd_link_hash_defined
   11827 			  || h->root.type == bfd_link_hash_defweak)
   11828 		      && (h->root.u.def.section->flags
   11829 			  & SEC_LINKER_CREATED) == 0
   11830 		      && h->root.u.def.section->owner != NULL
   11831 		      && (h->root.u.def.section->owner->flags
   11832 			  & BFD_PLUGIN) != 0)
   11833 		    {
   11834 		      h->root.type = bfd_link_hash_undefined;
   11835 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   11836 		    }
   11837 
   11838 		  ps = NULL;
   11839 		  if (h->root.type == bfd_link_hash_defined
   11840 		      || h->root.type == bfd_link_hash_defweak)
   11841 		    ps = &h->root.u.def.section;
   11842 
   11843 		  sym_name = h->root.root.string;
   11844 		}
   11845 	      else
   11846 		{
   11847 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   11848 
   11849 		  s_type = ELF_ST_TYPE (sym->st_info);
   11850 		  ps = &flinfo->sections[r_symndx];
   11851 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   11852 					       sym, *ps);
   11853 		}
   11854 
   11855 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   11856 		  && !bfd_link_relocatable (flinfo->info))
   11857 		{
   11858 		  bfd_vma val;
   11859 		  bfd_vma dot = (rel->r_offset
   11860 				 + o->output_offset + o->output_section->vma);
   11861 #ifdef DEBUG
   11862 		  printf ("Encountered a complex symbol!");
   11863 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   11864 			  bfd_get_filename (input_bfd), o->name,
   11865 			  (long) (rel - internal_relocs));
   11866 		  printf (" symbol: idx  %8.8lx, name %s\n",
   11867 			  r_symndx, sym_name);
   11868 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   11869 			  (unsigned long) rel->r_info,
   11870 			  (unsigned long) rel->r_offset);
   11871 #endif
   11872 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   11873 				    isymbuf, locsymcount, s_type == STT_SRELC))
   11874 		    return false;
   11875 
   11876 		  /* Symbol evaluated OK.  Update to absolute value.  */
   11877 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   11878 				    r_symndx, val);
   11879 		  continue;
   11880 		}
   11881 
   11882 	      if (action_discarded != -1 && ps != NULL)
   11883 		{
   11884 		  /* Complain if the definition comes from a
   11885 		     discarded section.  */
   11886 		  if ((sec = *ps) != NULL && discarded_section (sec))
   11887 		    {
   11888 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   11889 		      if (action_discarded & COMPLAIN)
   11890 			(*flinfo->info->callbacks->einfo)
   11891 			  /* xgettext:c-format */
   11892 			  (_("%X`%s' referenced in section `%pA' of %pB: "
   11893 			     "defined in discarded section `%pA' of %pB\n"),
   11894 			   sym_name, o, input_bfd, sec, sec->owner);
   11895 
   11896 		      /* Try to do the best we can to support buggy old
   11897 			 versions of gcc.  Pretend that the symbol is
   11898 			 really defined in the kept linkonce section.
   11899 			 FIXME: This is quite broken.  Modifying the
   11900 			 symbol here means we will be changing all later
   11901 			 uses of the symbol, not just in this section.  */
   11902 		      if (action_discarded & PRETEND)
   11903 			{
   11904 			  asection *kept;
   11905 
   11906 			  kept = _bfd_elf_check_kept_section (sec,
   11907 							      flinfo->info);
   11908 			  if (kept != NULL)
   11909 			    {
   11910 			      *ps = kept;
   11911 			      continue;
   11912 			    }
   11913 			}
   11914 		    }
   11915 		}
   11916 	    }
   11917 
   11918 	  /* Relocate the section by invoking a back end routine.
   11919 
   11920 	     The back end routine is responsible for adjusting the
   11921 	     section contents as necessary, and (if using Rela relocs
   11922 	     and generating a relocatable output file) adjusting the
   11923 	     reloc addend as necessary.
   11924 
   11925 	     The back end routine does not have to worry about setting
   11926 	     the reloc address or the reloc symbol index.
   11927 
   11928 	     The back end routine is given a pointer to the swapped in
   11929 	     internal symbols, and can access the hash table entries
   11930 	     for the external symbols via elf_sym_hashes (input_bfd).
   11931 
   11932 	     When generating relocatable output, the back end routine
   11933 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   11934 	     output symbol is going to be a section symbol
   11935 	     corresponding to the output section, which will require
   11936 	     the addend to be adjusted.  */
   11937 
   11938 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   11939 				     input_bfd, o, contents,
   11940 				     internal_relocs,
   11941 				     isymbuf,
   11942 				     flinfo->sections);
   11943 	  if (!ret)
   11944 	    return false;
   11945 
   11946 	  if (ret == 2
   11947 	      || bfd_link_relocatable (flinfo->info)
   11948 	      || flinfo->info->emitrelocations)
   11949 	    {
   11950 	      Elf_Internal_Rela *irela;
   11951 	      Elf_Internal_Rela *irelaend, *irelamid;
   11952 	      bfd_vma last_offset;
   11953 	      struct elf_link_hash_entry **rel_hash;
   11954 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   11955 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   11956 	      unsigned int next_erel;
   11957 	      bool rela_normal;
   11958 	      struct bfd_elf_section_data *esdi, *esdo;
   11959 
   11960 	      esdi = elf_section_data (o);
   11961 	      esdo = elf_section_data (o->output_section);
   11962 	      rela_normal = false;
   11963 
   11964 	      /* Adjust the reloc addresses and symbol indices.  */
   11965 
   11966 	      irela = internal_relocs;
   11967 	      irelaend = irela + o->reloc_count;
   11968 	      rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
   11969 	      /* We start processing the REL relocs, if any.  When we reach
   11970 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   11971 	      irelamid = irela;
   11972 	      if (esdi->rel.hdr != NULL)
   11973 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   11974 			     * bed->s->int_rels_per_ext_rel);
   11975 	      rel_hash_list = rel_hash;
   11976 	      rela_hash_list = NULL;
   11977 	      last_offset = o->output_offset;
   11978 	      if (!bfd_link_relocatable (flinfo->info))
   11979 		last_offset += o->output_section->vma;
   11980 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   11981 		{
   11982 		  unsigned long r_symndx;
   11983 		  asection *sec;
   11984 		  Elf_Internal_Sym sym;
   11985 
   11986 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   11987 		    {
   11988 		      rel_hash++;
   11989 		      next_erel = 0;
   11990 		    }
   11991 
   11992 		  if (irela == irelamid)
   11993 		    {
   11994 		      rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
   11995 		      rela_hash_list = rel_hash;
   11996 		      rela_normal = bed->rela_normal;
   11997 		    }
   11998 
   11999 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   12000 							     flinfo->info, o,
   12001 							     irela->r_offset);
   12002 		  if (irela->r_offset >= (bfd_vma) -2)
   12003 		    {
   12004 		      /* This is a reloc for a deleted entry or somesuch.
   12005 			 Turn it into an R_*_NONE reloc, at the same
   12006 			 offset as the last reloc.  elf_eh_frame.c and
   12007 			 bfd_elf_discard_info rely on reloc offsets
   12008 			 being ordered.  */
   12009 		      irela->r_offset = last_offset;
   12010 		      irela->r_info = 0;
   12011 		      irela->r_addend = 0;
   12012 		      continue;
   12013 		    }
   12014 
   12015 		  irela->r_offset += o->output_offset;
   12016 
   12017 		  /* Relocs in an executable have to be virtual addresses.  */
   12018 		  if (!bfd_link_relocatable (flinfo->info))
   12019 		    irela->r_offset += o->output_section->vma;
   12020 
   12021 		  last_offset = irela->r_offset;
   12022 
   12023 		  r_symndx = irela->r_info >> r_sym_shift;
   12024 		  if (r_symndx == STN_UNDEF)
   12025 		    continue;
   12026 
   12027 		  if (r_symndx >= locsymcount
   12028 		      || (elf_bad_symtab (input_bfd)
   12029 			  && flinfo->sections[r_symndx] == NULL))
   12030 		    {
   12031 		      struct elf_link_hash_entry *rh;
   12032 
   12033 		      /* This is a reloc against a global symbol.  We
   12034 			 have not yet output all the local symbols, so
   12035 			 we do not know the symbol index of any global
   12036 			 symbol.  We set the rel_hash entry for this
   12037 			 reloc to point to the global hash table entry
   12038 			 for this symbol.  The symbol index is then
   12039 			 set at the end of bfd_elf_final_link.  */
   12040 		      rh = get_link_hash_entry (elf_sym_hashes (input_bfd),
   12041 						r_symndx, extsymoff);
   12042 		      if (rh == NULL)
   12043 			{
   12044 			  /* FIXME: Generate an error ?  */
   12045 			  continue;
   12046 			}
   12047 
   12048 		      /* Setting the index to -2 tells elf_link_output_extsym
   12049 			 that this symbol is used by a reloc.  */
   12050 		      BFD_ASSERT (rh->indx < 0);
   12051 		      rh->indx = -2;
   12052 		      *rel_hash = rh;
   12053 
   12054 		      continue;
   12055 		    }
   12056 
   12057 		  /* This is a reloc against a local symbol.  */
   12058 
   12059 		  *rel_hash = NULL;
   12060 		  sym = isymbuf[r_symndx];
   12061 		  sec = flinfo->sections[r_symndx];
   12062 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   12063 		    {
   12064 		      /* I suppose the backend ought to fill in the
   12065 			 section of any STT_SECTION symbol against a
   12066 			 processor specific section.  */
   12067 		      r_symndx = STN_UNDEF;
   12068 		      if (bfd_is_abs_section (sec))
   12069 			;
   12070 		      else if (sec == NULL || sec->owner == NULL)
   12071 			{
   12072 			  bfd_set_error (bfd_error_bad_value);
   12073 			  return false;
   12074 			}
   12075 		      else
   12076 			{
   12077 			  asection *osec = sec->output_section;
   12078 
   12079 			  /* If we have discarded a section, the output
   12080 			     section will be the absolute section.  In
   12081 			     case of discarded SEC_MERGE sections, use
   12082 			     the kept section.  relocate_section should
   12083 			     have already handled discarded linkonce
   12084 			     sections.  */
   12085 			  if (bfd_is_abs_section (osec)
   12086 			      && sec->kept_section != NULL
   12087 			      && sec->kept_section->output_section != NULL)
   12088 			    {
   12089 			      osec = sec->kept_section->output_section;
   12090 			      irela->r_addend -= osec->vma;
   12091 			    }
   12092 
   12093 			  if (!bfd_is_abs_section (osec))
   12094 			    {
   12095 			      r_symndx = osec->target_index;
   12096 			      if (r_symndx == STN_UNDEF)
   12097 				{
   12098 				  irela->r_addend += osec->vma;
   12099 				  osec = _bfd_nearby_section (output_bfd, osec,
   12100 							      osec->vma);
   12101 				  irela->r_addend -= osec->vma;
   12102 				  r_symndx = osec->target_index;
   12103 				}
   12104 			    }
   12105 			}
   12106 
   12107 		      /* Adjust the addend according to where the
   12108 			 section winds up in the output section.  */
   12109 		      if (rela_normal)
   12110 			irela->r_addend += sec->output_offset;
   12111 		    }
   12112 		  else
   12113 		    {
   12114 		      if (flinfo->indices[r_symndx] == -1)
   12115 			{
   12116 			  unsigned long shlink;
   12117 			  const char *name;
   12118 			  asection *osec;
   12119 			  long indx;
   12120 
   12121 			  if (flinfo->info->strip == strip_all)
   12122 			    {
   12123 			      /* You can't do ld -r -s.  */
   12124 			      bfd_set_error (bfd_error_invalid_operation);
   12125 			      return false;
   12126 			    }
   12127 
   12128 			  /* This symbol was skipped earlier, but
   12129 			     since it is needed by a reloc, we
   12130 			     must output it now.  */
   12131 			  shlink = symtab_hdr->sh_link;
   12132 			  name = (bfd_elf_string_from_elf_section
   12133 				  (input_bfd, shlink, sym.st_name));
   12134 			  if (name == NULL)
   12135 			    return false;
   12136 
   12137 			  osec = sec->output_section;
   12138 			  sym.st_shndx =
   12139 			    _bfd_elf_section_from_bfd_section (output_bfd,
   12140 							       osec);
   12141 			  if (sym.st_shndx == SHN_BAD)
   12142 			    return false;
   12143 
   12144 			  sym.st_value += sec->output_offset;
   12145 			  if (!bfd_link_relocatable (flinfo->info))
   12146 			    {
   12147 			      sym.st_value += osec->vma;
   12148 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   12149 				{
   12150 				  struct elf_link_hash_table *htab
   12151 				    = elf_hash_table (flinfo->info);
   12152 
   12153 				  /* STT_TLS symbols are relative to PT_TLS
   12154 				     segment base.  */
   12155 				  if (htab->tls_sec != NULL)
   12156 				    sym.st_value -= htab->tls_sec->vma;
   12157 				  else
   12158 				    sym.st_info
   12159 				      = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
   12160 						     STT_NOTYPE);
   12161 				}
   12162 			    }
   12163 
   12164 			  indx = bfd_get_symcount (output_bfd);
   12165 			  ret = elf_link_output_symstrtab (flinfo, name,
   12166 							   &sym, sec,
   12167 							   NULL);
   12168 			  if (ret == 0)
   12169 			    return false;
   12170 			  else if (ret == 1)
   12171 			    flinfo->indices[r_symndx] = indx;
   12172 			  else
   12173 			    abort ();
   12174 			}
   12175 
   12176 		      r_symndx = flinfo->indices[r_symndx];
   12177 		    }
   12178 
   12179 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   12180 				   | (irela->r_info & r_type_mask));
   12181 		}
   12182 
   12183 	      /* Swap out the relocs.  */
   12184 	      input_rel_hdr = esdi->rel.hdr;
   12185 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   12186 		{
   12187 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   12188 						     input_rel_hdr,
   12189 						     internal_relocs,
   12190 						     rel_hash_list))
   12191 		    return false;
   12192 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   12193 				      * bed->s->int_rels_per_ext_rel);
   12194 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   12195 		}
   12196 
   12197 	      input_rela_hdr = esdi->rela.hdr;
   12198 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   12199 		{
   12200 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   12201 						     input_rela_hdr,
   12202 						     internal_relocs,
   12203 						     rela_hash_list))
   12204 		    return false;
   12205 		}
   12206 	    }
   12207 	}
   12208 
   12209       /* Write out the modified section contents.  */
   12210       if (bed->elf_backend_write_section
   12211 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   12212 						contents))
   12213 	{
   12214 	  /* Section written out.  */
   12215 	}
   12216       else switch (o->sec_info_type)
   12217 	{
   12218 	case SEC_INFO_TYPE_STABS:
   12219 	  if (! (_bfd_write_section_stabs
   12220 		 (output_bfd,
   12221 		  &elf_hash_table (flinfo->info)->stab_info,
   12222 		  o, &elf_section_data (o)->sec_info, contents)))
   12223 	    return false;
   12224 	  break;
   12225 	case SEC_INFO_TYPE_MERGE:
   12226 	  if (! _bfd_write_merged_section (output_bfd, o,
   12227 					   elf_section_data (o)->sec_info))
   12228 	    return false;
   12229 	  break;
   12230 	case SEC_INFO_TYPE_EH_FRAME:
   12231 	  {
   12232 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   12233 						   o, contents))
   12234 	      return false;
   12235 	  }
   12236 	  break;
   12237 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   12238 	  {
   12239 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   12240 							 flinfo->info,
   12241 							 o, contents))
   12242 	      return false;
   12243 	  }
   12244 	  break;
   12245 	case SEC_INFO_TYPE_SFRAME:
   12246 	    {
   12247 	      /* Merge SFrame section into the SFrame encoder context of the
   12248 		 output_bfd's section.  The final .sframe output section will
   12249 		 be written out later.  */
   12250 	      if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
   12251 						  o, contents))
   12252 		return false;
   12253 	    }
   12254 	    break;
   12255 	default:
   12256 	  {
   12257 	    if (! (o->flags & SEC_EXCLUDE))
   12258 	      {
   12259 		file_ptr offset = (file_ptr) o->output_offset;
   12260 		bfd_size_type todo = o->size;
   12261 
   12262 		offset *= bfd_octets_per_byte (output_bfd, o);
   12263 
   12264 		if ((o->flags & SEC_ELF_REVERSE_COPY)
   12265 		    && o->size > address_size)
   12266 		  {
   12267 		    /* Reverse-copy input section to output.  */
   12268 
   12269 		    if ((o->size & (address_size - 1)) != 0
   12270 			|| (o->reloc_count != 0
   12271 			    && (o->size * bed->s->int_rels_per_ext_rel
   12272 				!= o->reloc_count * address_size)))
   12273 		      {
   12274 			_bfd_error_handler
   12275 			  /* xgettext:c-format */
   12276 			  (_("error: %pB: size of section %pA is not "
   12277 			     "multiple of address size"),
   12278 			   input_bfd, o);
   12279 			bfd_set_error (bfd_error_bad_value);
   12280 			return false;
   12281 		      }
   12282 
   12283 		    do
   12284 		      {
   12285 			todo -= address_size;
   12286 			if (! bfd_set_section_contents (output_bfd,
   12287 							o->output_section,
   12288 							contents + todo,
   12289 							offset,
   12290 							address_size))
   12291 			  return false;
   12292 			if (todo == 0)
   12293 			  break;
   12294 			offset += address_size;
   12295 		      }
   12296 		    while (1);
   12297 		  }
   12298 		else if (! bfd_set_section_contents (output_bfd,
   12299 						     o->output_section,
   12300 						     contents,
   12301 						     offset, todo))
   12302 		  return false;
   12303 	      }
   12304 	  }
   12305 	  break;
   12306 	}
   12307 
   12308       /* Munmap the section contents for each input section.  */
   12309       _bfd_elf_link_munmap_section_contents (o);
   12310     }
   12311 
   12312   return true;
   12313 }
   12314 
   12315 /* Generate a reloc when linking an ELF file.  This is a reloc
   12316    requested by the linker, and does not come from any input file.  This
   12317    is used to build constructor and destructor tables when linking
   12318    with -Ur.  */
   12319 
   12320 static bool
   12321 elf_reloc_link_order (bfd *output_bfd,
   12322 		      struct bfd_link_info *info,
   12323 		      asection *output_section,
   12324 		      struct bfd_link_order *link_order)
   12325 {
   12326   reloc_howto_type *howto;
   12327   long indx;
   12328   bfd_vma offset;
   12329   bfd_vma addend;
   12330   struct bfd_elf_section_reloc_data *reldata;
   12331   struct elf_link_hash_entry **rel_hash_ptr;
   12332   Elf_Internal_Shdr *rel_hdr;
   12333   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   12334   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   12335   bfd_byte *erel;
   12336   unsigned int i;
   12337   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   12338 
   12339   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   12340   if (howto == NULL)
   12341     {
   12342       bfd_set_error (bfd_error_bad_value);
   12343       return false;
   12344     }
   12345 
   12346   addend = link_order->u.reloc.p->addend;
   12347 
   12348   if (esdo->rel.hdr)
   12349     reldata = &esdo->rel;
   12350   else if (esdo->rela.hdr)
   12351     reldata = &esdo->rela;
   12352   else
   12353     {
   12354       reldata = NULL;
   12355       BFD_ASSERT (0);
   12356     }
   12357 
   12358   /* Figure out the symbol index.  */
   12359   rel_hash_ptr = reldata->hashes + reldata->count;
   12360   if (link_order->type == bfd_section_reloc_link_order)
   12361     {
   12362       indx = link_order->u.reloc.p->u.section->target_index;
   12363       BFD_ASSERT (indx != 0);
   12364       *rel_hash_ptr = NULL;
   12365     }
   12366   else
   12367     {
   12368       struct elf_link_hash_entry *h;
   12369 
   12370       /* Treat a reloc against a defined symbol as though it were
   12371 	 actually against the section.  */
   12372       h = ((struct elf_link_hash_entry *)
   12373 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   12374 					 link_order->u.reloc.p->u.name,
   12375 					 false, false, true));
   12376       if (h != NULL
   12377 	  && (h->root.type == bfd_link_hash_defined
   12378 	      || h->root.type == bfd_link_hash_defweak))
   12379 	{
   12380 	  asection *section;
   12381 
   12382 	  section = h->root.u.def.section;
   12383 	  indx = section->output_section->target_index;
   12384 	  *rel_hash_ptr = NULL;
   12385 	  /* It seems that we ought to add the symbol value to the
   12386 	     addend here, but in practice it has already been added
   12387 	     because it was passed to constructor_callback.  */
   12388 	  addend += section->output_section->vma + section->output_offset;
   12389 	}
   12390       else if (h != NULL)
   12391 	{
   12392 	  /* Setting the index to -2 tells elf_link_output_extsym that
   12393 	     this symbol is used by a reloc.  */
   12394 	  h->indx = -2;
   12395 	  *rel_hash_ptr = h;
   12396 	  indx = 0;
   12397 	}
   12398       else
   12399 	{
   12400 	  (*info->callbacks->unattached_reloc)
   12401 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   12402 	  indx = 0;
   12403 	}
   12404     }
   12405 
   12406   /* If this is an inplace reloc, we must write the addend into the
   12407      object file.  */
   12408   if (howto->partial_inplace && addend != 0)
   12409     {
   12410       bfd_size_type size;
   12411       bfd_reloc_status_type rstat;
   12412       bfd_byte *buf;
   12413       bool ok;
   12414       const char *sym_name;
   12415       bfd_size_type octets;
   12416 
   12417       size = (bfd_size_type) bfd_get_reloc_size (howto);
   12418       buf = (bfd_byte *) bfd_zmalloc (size);
   12419       if (buf == NULL && size != 0)
   12420 	return false;
   12421       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   12422       switch (rstat)
   12423 	{
   12424 	case bfd_reloc_ok:
   12425 	  break;
   12426 
   12427 	default:
   12428 	case bfd_reloc_outofrange:
   12429 	  abort ();
   12430 
   12431 	case bfd_reloc_overflow:
   12432 	  if (link_order->type == bfd_section_reloc_link_order)
   12433 	    sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
   12434 	  else
   12435 	    sym_name = link_order->u.reloc.p->u.name;
   12436 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
   12437 					      howto->name, addend, NULL, NULL,
   12438 					      (bfd_vma) 0);
   12439 	  break;
   12440 	}
   12441 
   12442       octets = link_order->offset * bfd_octets_per_byte (output_bfd,
   12443 							 output_section);
   12444       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   12445 				     octets, size);
   12446       free (buf);
   12447       if (! ok)
   12448 	return false;
   12449     }
   12450 
   12451   /* The address of a reloc is relative to the section in a
   12452      relocatable file, and is a virtual address in an executable
   12453      file.  */
   12454   offset = link_order->offset;
   12455   if (! bfd_link_relocatable (info))
   12456     offset += output_section->vma;
   12457 
   12458   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   12459     {
   12460       irel[i].r_offset = offset;
   12461       irel[i].r_info = 0;
   12462       irel[i].r_addend = 0;
   12463     }
   12464   if (bed->s->arch_size == 32)
   12465     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   12466   else
   12467     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
   12468 
   12469   rel_hdr = reldata->hdr;
   12470   erel = rel_hdr->contents;
   12471   if (rel_hdr->sh_type == SHT_REL)
   12472     {
   12473       erel += reldata->count * bed->s->sizeof_rel;
   12474       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   12475     }
   12476   else
   12477     {
   12478       irel[0].r_addend = addend;
   12479       erel += reldata->count * bed->s->sizeof_rela;
   12480       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   12481     }
   12482 
   12483   ++reldata->count;
   12484 
   12485   return true;
   12486 }
   12487 
   12488 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
   12489    Returns TRUE upon success, FALSE otherwise.  */
   12490 
   12491 static bool
   12492 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
   12493 {
   12494   bool ret = false;
   12495   bfd *implib_bfd;
   12496   const struct elf_backend_data *bed;
   12497   flagword flags;
   12498   enum bfd_architecture arch;
   12499   unsigned int mach;
   12500   asymbol **sympp = NULL;
   12501   long symsize;
   12502   long symcount;
   12503   long src_count;
   12504   elf_symbol_type *osymbuf;
   12505   size_t amt;
   12506 
   12507   implib_bfd = info->out_implib_bfd;
   12508   bed = get_elf_backend_data (abfd);
   12509 
   12510   if (!bfd_set_format (implib_bfd, bfd_object))
   12511     return false;
   12512 
   12513   /* Use flag from executable but make it a relocatable object.  */
   12514   flags = bfd_get_file_flags (abfd);
   12515   flags &= ~HAS_RELOC;
   12516   if (!bfd_set_start_address (implib_bfd, 0)
   12517       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
   12518     return false;
   12519 
   12520   /* Copy architecture of output file to import library file.  */
   12521   arch = bfd_get_arch (abfd);
   12522   mach = bfd_get_mach (abfd);
   12523   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
   12524       && (abfd->target_defaulted
   12525 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
   12526     return false;
   12527 
   12528   /* Get symbol table size.  */
   12529   symsize = bfd_get_symtab_upper_bound (abfd);
   12530   if (symsize < 0)
   12531     return false;
   12532 
   12533   /* Read in the symbol table.  */
   12534   sympp = (asymbol **) bfd_malloc (symsize);
   12535   if (sympp == NULL)
   12536     return false;
   12537 
   12538   symcount = bfd_canonicalize_symtab (abfd, sympp);
   12539   if (symcount < 0)
   12540     goto free_sym_buf;
   12541 
   12542   /* Allow the BFD backend to copy any private header data it
   12543      understands from the output BFD to the import library BFD.  */
   12544   if (! bfd_copy_private_header_data (abfd, implib_bfd))
   12545     goto free_sym_buf;
   12546 
   12547   /* Filter symbols to appear in the import library.  */
   12548   if (bed->elf_backend_filter_implib_symbols)
   12549     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
   12550 						       symcount);
   12551   else
   12552     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
   12553   if (symcount == 0)
   12554     {
   12555       bfd_set_error (bfd_error_no_symbols);
   12556       _bfd_error_handler (_("%pB: no symbol found for import library"),
   12557 			  implib_bfd);
   12558       goto free_sym_buf;
   12559     }
   12560 
   12561 
   12562   /* Make symbols absolute.  */
   12563   amt = symcount * sizeof (*osymbuf);
   12564   osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
   12565   if (osymbuf == NULL)
   12566     goto free_sym_buf;
   12567 
   12568   for (src_count = 0; src_count < symcount; src_count++)
   12569     {
   12570       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
   12571 	      sizeof (*osymbuf));
   12572       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
   12573       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
   12574       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
   12575       osymbuf[src_count].internal_elf_sym.st_value =
   12576 	osymbuf[src_count].symbol.value;
   12577       sympp[src_count] = &osymbuf[src_count].symbol;
   12578     }
   12579 
   12580   bfd_set_symtab (implib_bfd, sympp, symcount);
   12581 
   12582   /* Allow the BFD backend to copy any private data it understands
   12583      from the output BFD to the import library BFD.  This is done last
   12584      to permit the routine to look at the filtered symbol table.  */
   12585   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
   12586     goto free_sym_buf;
   12587 
   12588   if (!bfd_close (implib_bfd))
   12589     goto free_sym_buf;
   12590 
   12591   ret = true;
   12592 
   12593  free_sym_buf:
   12594   free (sympp);
   12595   return ret;
   12596 }
   12597 
   12598 static void
   12599 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   12600 {
   12601   asection *o;
   12602 
   12603   if (flinfo->symstrtab != NULL)
   12604     _bfd_elf_strtab_free (flinfo->symstrtab);
   12605   free (flinfo->contents);
   12606   free (flinfo->external_relocs);
   12607   free (flinfo->internal_relocs);
   12608   free (flinfo->external_syms);
   12609   free (flinfo->locsym_shndx);
   12610   free (flinfo->internal_syms);
   12611   free (flinfo->indices);
   12612   free (flinfo->sections);
   12613   if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
   12614     free (flinfo->symshndxbuf);
   12615   for (o = obfd->sections; o != NULL; o = o->next)
   12616     {
   12617       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12618       free (esdo->rel.hashes);
   12619       free (esdo->rela.hashes);
   12620     }
   12621 }
   12622 
   12623 /* Do the final step of an ELF link.  */
   12624 
   12625 bool
   12626 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   12627 {
   12628   bool dynamic;
   12629   bool emit_relocs;
   12630   bfd *dynobj;
   12631   struct elf_final_link_info flinfo;
   12632   asection *o;
   12633   struct bfd_link_order *p;
   12634   bfd *sub;
   12635   bfd_size_type max_contents_size;
   12636   bfd_size_type max_external_reloc_size;
   12637   bfd_size_type max_internal_reloc_count;
   12638   bfd_size_type max_sym_count;
   12639   bfd_size_type max_sym_shndx_count;
   12640   Elf_Internal_Sym elfsym;
   12641   unsigned int i;
   12642   Elf_Internal_Shdr *symtab_hdr;
   12643   Elf_Internal_Shdr *symtab_shndx_hdr;
   12644   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12645   struct elf_outext_info eoinfo;
   12646   bool merged;
   12647   size_t relativecount;
   12648   size_t relr_entsize;
   12649   asection *reldyn = 0;
   12650   bfd_size_type amt;
   12651   struct elf_link_hash_table *htab = elf_hash_table (info);
   12652   bool sections_removed;
   12653 
   12654   if (!is_elf_hash_table (&htab->root))
   12655     return false;
   12656 
   12657   if (bfd_link_pic (info))
   12658     abfd->flags |= DYNAMIC;
   12659 
   12660   dynamic = htab->dynamic_sections_created;
   12661   dynobj = htab->dynobj;
   12662 
   12663   emit_relocs = (bfd_link_relocatable (info)
   12664 		 || info->emitrelocations);
   12665 
   12666   memset (&flinfo, 0, sizeof (flinfo));
   12667   flinfo.info = info;
   12668   flinfo.output_bfd = abfd;
   12669   flinfo.symstrtab = _bfd_elf_strtab_init ();
   12670   if (flinfo.symstrtab == NULL)
   12671     return false;
   12672 
   12673   if (! dynamic)
   12674     {
   12675       flinfo.hash_sec = NULL;
   12676       flinfo.symver_sec = NULL;
   12677     }
   12678   else
   12679     {
   12680       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   12681       /* Note that dynsym_sec can be NULL (on VMS).  */
   12682       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   12683       /* Note that it is OK if symver_sec is NULL.  */
   12684     }
   12685 
   12686   if (info->unique_symbol
   12687       && !bfd_hash_table_init (&flinfo.local_hash_table,
   12688 			       local_hash_newfunc,
   12689 			       sizeof (struct local_hash_entry)))
   12690     return false;
   12691 
   12692   /* The object attributes have been merged.  Remove the input
   12693      sections from the link, and set the contents of the output
   12694      section.  */
   12695   sections_removed = false;
   12696   const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   12697   for (o = abfd->sections; o != NULL; o = o->next)
   12698     {
   12699       bool remove_section = false;
   12700 
   12701       if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
   12702 	  || strcmp (o->name, ".gnu.attributes") == 0)
   12703 	{
   12704 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   12705 	    {
   12706 	      asection *input_section;
   12707 
   12708 	      if (p->type != bfd_indirect_link_order)
   12709 		continue;
   12710 	      input_section = p->u.indirect.section;
   12711 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   12712 		 elf_link_input_bfd ignores this section.  */
   12713 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   12714 	    }
   12715 
   12716 	  /* Skip this section later on.  */
   12717 	  o->map_head.link_order = NULL;
   12718 
   12719 	  bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
   12720 	  /* Once ELF headers have been written, the size of a section is
   12721 	     frozen. We need to set the size of the attribute section before
   12722 	     _bfd_elf_compute_section_file_positions.  */
   12723 	  bfd_set_section_size (o, attr_size);
   12724 	  if (attr_size > 0)
   12725 	    elf_obj_build_attributes (abfd) = o;
   12726 	  else
   12727 	    remove_section = true;
   12728 	}
   12729       else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
   12730 	{
   12731 	  /* Remove empty group section from linker output.  */
   12732 	  remove_section = true;
   12733 	}
   12734       if (remove_section)
   12735 	{
   12736 	  o->flags |= SEC_EXCLUDE;
   12737 	  bfd_section_list_remove (abfd, o);
   12738 	  abfd->section_count--;
   12739 	  sections_removed = true;
   12740 	}
   12741     }
   12742   if (sections_removed)
   12743     _bfd_fix_excluded_sec_syms (abfd, info);
   12744 
   12745   /* Count up the number of relocations we will output for each output
   12746      section, so that we know the sizes of the reloc sections.  We
   12747      also figure out some maximum sizes.  */
   12748 #ifdef USE_MMAP
   12749   if (bed->use_mmap)
   12750     {
   12751       /* Mmap is used only if section size >= the minimum mmap section
   12752 	 size.  The initial max_contents_size value covers all sections
   12753 	 smaller than the minimum mmap section size.  It may be increased
   12754 	 for compressed or linker created sections or sections whose
   12755 	 rawsize != size.  max_external_reloc_size covers all relocation
   12756 	 sections smaller than the minimum mmap section size.  */
   12757       max_contents_size = _bfd_minimum_mmap_size;
   12758       max_external_reloc_size = _bfd_minimum_mmap_size;
   12759     }
   12760   else
   12761 #endif
   12762     {
   12763       max_contents_size = 0;
   12764       max_external_reloc_size = 0;
   12765     }
   12766   max_internal_reloc_count = 0;
   12767   max_sym_count = 0;
   12768   max_sym_shndx_count = 0;
   12769   merged = false;
   12770   for (o = abfd->sections; o != NULL; o = o->next)
   12771     {
   12772       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12773       o->reloc_count = 0;
   12774 
   12775       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12776 	{
   12777 	  unsigned int reloc_count = 0;
   12778 	  unsigned int additional_reloc_count = 0;
   12779 	  struct bfd_elf_section_data *esdi = NULL;
   12780 
   12781 	  if (p->type == bfd_section_reloc_link_order
   12782 	      || p->type == bfd_symbol_reloc_link_order)
   12783 	    reloc_count = 1;
   12784 	  else if (p->type == bfd_indirect_link_order)
   12785 	    {
   12786 	      asection *sec;
   12787 
   12788 	      sec = p->u.indirect.section;
   12789 
   12790 	      /* Mark all sections which are to be included in the
   12791 		 link.  This will normally be every section.  We need
   12792 		 to do this so that we can identify any sections which
   12793 		 the linker has decided to not include.  */
   12794 	      sec->linker_mark = true;
   12795 
   12796 	      if (sec->flags & SEC_MERGE)
   12797 		merged = true;
   12798 
   12799 #ifdef USE_MMAP
   12800 	      /* Mmap is used only on non-compressed, non-linker created
   12801 		 sections whose rawsize == size.  */
   12802 	      if (!bed->use_mmap
   12803 		  || sec->compress_status != COMPRESS_SECTION_NONE
   12804 		  || (sec->flags & SEC_LINKER_CREATED) != 0
   12805 		  || sec->rawsize != sec->size)
   12806 #endif
   12807 		{
   12808 		  if (sec->rawsize > max_contents_size)
   12809 		    max_contents_size = sec->rawsize;
   12810 		  if (sec->size > max_contents_size)
   12811 		    max_contents_size = sec->size;
   12812 		}
   12813 
   12814 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   12815 		  && (sec->owner->flags & DYNAMIC) == 0)
   12816 		{
   12817 		  size_t sym_count;
   12818 
   12819 		  /* We are interested in just local symbols, not all
   12820 		     symbols.  */
   12821 		  if (elf_bad_symtab (sec->owner))
   12822 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   12823 				 / bed->s->sizeof_sym);
   12824 		  else
   12825 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   12826 
   12827 		  if (sym_count > max_sym_count)
   12828 		    max_sym_count = sym_count;
   12829 
   12830 		  if (sym_count > max_sym_shndx_count
   12831 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   12832 		    max_sym_shndx_count = sym_count;
   12833 
   12834 		  esdi = elf_section_data (sec);
   12835 
   12836 		  if (esdi->this_hdr.sh_type == SHT_REL
   12837 		      || esdi->this_hdr.sh_type == SHT_RELA)
   12838 		    /* Some backends use reloc_count in relocation sections
   12839 		       to count particular types of relocs.  Of course,
   12840 		       reloc sections themselves can't have relocations.  */
   12841 		    ;
   12842 		  else if (emit_relocs)
   12843 		    {
   12844 		      reloc_count = sec->reloc_count;
   12845 		      if (bed->elf_backend_count_additional_relocs)
   12846 			{
   12847 			  int c;
   12848 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
   12849 			  additional_reloc_count += c;
   12850 			}
   12851 		    }
   12852 		  else if (bed->elf_backend_count_relocs)
   12853 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   12854 
   12855 		  if ((sec->flags & SEC_RELOC) != 0)
   12856 		    {
   12857 #ifdef USE_MMAP
   12858 		      if (!bed->use_mmap)
   12859 #endif
   12860 			{
   12861 			  size_t ext_size = 0;
   12862 
   12863 			  if (esdi->rel.hdr != NULL)
   12864 			    ext_size = esdi->rel.hdr->sh_size;
   12865 			  if (esdi->rela.hdr != NULL)
   12866 			    ext_size += esdi->rela.hdr->sh_size;
   12867 
   12868 			  if (ext_size > max_external_reloc_size)
   12869 			    max_external_reloc_size = ext_size;
   12870 			}
   12871 		      if (sec->reloc_count > max_internal_reloc_count)
   12872 			max_internal_reloc_count = sec->reloc_count;
   12873 		    }
   12874 		}
   12875 	    }
   12876 
   12877 	  if (reloc_count == 0)
   12878 	    continue;
   12879 
   12880 	  reloc_count += additional_reloc_count;
   12881 	  o->reloc_count += reloc_count;
   12882 
   12883 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   12884 	    {
   12885 	      if (esdi->rel.hdr)
   12886 		{
   12887 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   12888 		  esdo->rel.count += additional_reloc_count;
   12889 		}
   12890 	      if (esdi->rela.hdr)
   12891 		{
   12892 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   12893 		  esdo->rela.count += additional_reloc_count;
   12894 		}
   12895 	    }
   12896 	  else
   12897 	    {
   12898 	      if (o->use_rela_p)
   12899 		esdo->rela.count += reloc_count;
   12900 	      else
   12901 		esdo->rel.count += reloc_count;
   12902 	    }
   12903 	}
   12904 
   12905       if (o->reloc_count > 0)
   12906 	o->flags |= SEC_RELOC;
   12907       else
   12908 	{
   12909 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   12910 	     set it (this is probably a bug) and if it is set
   12911 	     assign_section_numbers will create a reloc section.  */
   12912 	  o->flags &=~ SEC_RELOC;
   12913 	}
   12914 
   12915       /* If the SEC_ALLOC flag is not set, force the section VMA to
   12916 	 zero.  This is done in elf_fake_sections as well, but forcing
   12917 	 the VMA to 0 here will ensure that relocs against these
   12918 	 sections are handled correctly.  */
   12919       if ((o->flags & SEC_ALLOC) == 0
   12920 	  && ! o->user_set_vma)
   12921 	o->vma = 0;
   12922     }
   12923 
   12924   if (! bfd_link_relocatable (info) && merged)
   12925     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
   12926 
   12927   /* Figure out the file positions for everything but the symbol table
   12928      and the relocs.  We set symcount to force assign_section_numbers
   12929      to create a symbol table.  */
   12930   abfd->symcount = info->strip != strip_all || emit_relocs;
   12931   BFD_ASSERT (! abfd->output_has_begun);
   12932   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   12933     goto error_return;
   12934 
   12935   /* Set sizes, and assign file positions for reloc sections.  */
   12936   for (o = abfd->sections; o != NULL; o = o->next)
   12937     {
   12938       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12939       if ((o->flags & SEC_RELOC) != 0)
   12940 	{
   12941 	  if (esdo->rel.hdr
   12942 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   12943 	    goto error_return;
   12944 
   12945 	  if (esdo->rela.hdr
   12946 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   12947 	    goto error_return;
   12948 	}
   12949 
   12950       /* _bfd_elf_compute_section_file_positions makes temporary use
   12951 	 of target_index.  Reset it.  */
   12952       o->target_index = 0;
   12953 
   12954       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   12955 	 to count upwards while actually outputting the relocations.  */
   12956       esdo->rel.count = 0;
   12957       esdo->rela.count = 0;
   12958 
   12959       if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
   12960 	  && !bfd_section_is_ctf (o))
   12961 	{
   12962 	  /* Cache the section contents so that they can be compressed
   12963 	     later.  Use bfd_malloc since it will be freed by
   12964 	     bfd_compress_section_contents.  */
   12965 	  unsigned char *contents = esdo->this_hdr.contents;
   12966 	  if (contents != NULL)
   12967 	    abort ();
   12968 	  contents
   12969 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   12970 	  if (contents == NULL)
   12971 	    goto error_return;
   12972 	  esdo->this_hdr.contents = contents;
   12973 	}
   12974     }
   12975 
   12976   /* We have now assigned file positions for all the sections except .symtab,
   12977      .strtab, and non-loaded reloc and compressed debugging sections.  We start
   12978      the .symtab section at the current file position, and write directly to it.
   12979      We build the .strtab section in memory.  */
   12980   abfd->symcount = 0;
   12981   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12982   /* sh_name is set in prep_headers.  */
   12983   symtab_hdr->sh_type = SHT_SYMTAB;
   12984   /* sh_flags, sh_addr and sh_size all start off zero.  */
   12985   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   12986   /* sh_link is set in assign_section_numbers.  */
   12987   /* sh_info is set below.  */
   12988   /* sh_offset is set just below.  */
   12989   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   12990 
   12991   if (max_sym_count < 20)
   12992     max_sym_count = 20;
   12993   htab->strtabsize = max_sym_count;
   12994   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   12995   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
   12996   if (htab->strtab == NULL)
   12997     goto error_return;
   12998   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   12999   flinfo.symshndxbuf
   13000     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   13001        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   13002 
   13003   if (info->strip != strip_all || emit_relocs)
   13004     {
   13005       file_ptr off = elf_next_file_pos (abfd);
   13006 
   13007       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
   13008 
   13009       /* Note that at this point elf_next_file_pos (abfd) is
   13010 	 incorrect.  We do not yet know the size of the .symtab section.
   13011 	 We correct next_file_pos below, after we do know the size.  */
   13012 
   13013       /* Start writing out the symbol table.  The first symbol is always a
   13014 	 dummy symbol.  */
   13015       elfsym.st_value = 0;
   13016       elfsym.st_size = 0;
   13017       elfsym.st_info = 0;
   13018       elfsym.st_other = 0;
   13019       elfsym.st_shndx = SHN_UNDEF;
   13020       elfsym.st_target_internal = 0;
   13021       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   13022 				     bfd_und_section_ptr, NULL) != 1)
   13023 	goto error_return;
   13024 
   13025       /* Output a symbol for each section if asked or they are used for
   13026 	 relocs.  These symbols usually have no names.  We store the
   13027 	 index of each one in the index field of the section, so that
   13028 	 we can find it again when outputting relocs.  */
   13029 
   13030       if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
   13031 	{
   13032 	  bool name_local_sections
   13033 	    = (bed->elf_backend_name_local_section_symbols
   13034 	       && bed->elf_backend_name_local_section_symbols (abfd));
   13035 	  const char *name = NULL;
   13036 
   13037 	  elfsym.st_size = 0;
   13038 	  elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   13039 	  elfsym.st_other = 0;
   13040 	  elfsym.st_value = 0;
   13041 	  elfsym.st_target_internal = 0;
   13042 	  for (i = 1; i < elf_numsections (abfd); i++)
   13043 	    {
   13044 	      o = bfd_section_from_elf_index (abfd, i);
   13045 	      if (o != NULL)
   13046 		{
   13047 		  o->target_index = bfd_get_symcount (abfd);
   13048 		  elfsym.st_shndx = i;
   13049 		  if (!bfd_link_relocatable (info))
   13050 		    elfsym.st_value = o->vma;
   13051 		  if (name_local_sections)
   13052 		    name = o->name;
   13053 		  if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
   13054 						 NULL) != 1)
   13055 		    goto error_return;
   13056 		}
   13057 	    }
   13058 	}
   13059     }
   13060 
   13061   /* On some targets like Irix 5 the symbol split between local and global
   13062      ones recorded in the sh_info field needs to be done between section
   13063      and all other symbols.  */
   13064   if (bed->elf_backend_elfsym_local_is_section
   13065       && bed->elf_backend_elfsym_local_is_section (abfd))
   13066     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   13067 
   13068   /* Allocate some memory to hold information read in from the input
   13069      files.  */
   13070   if (max_contents_size != 0)
   13071     {
   13072       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   13073       if (flinfo.contents == NULL)
   13074 	goto error_return;
   13075     }
   13076 
   13077   if (max_external_reloc_size != 0)
   13078     {
   13079       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   13080       if (flinfo.external_relocs == NULL)
   13081 	goto error_return;
   13082     }
   13083 
   13084   if (max_internal_reloc_count != 0)
   13085     {
   13086       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
   13087       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   13088       if (flinfo.internal_relocs == NULL)
   13089 	goto error_return;
   13090     }
   13091 
   13092   if (max_sym_count != 0)
   13093     {
   13094       amt = max_sym_count * bed->s->sizeof_sym;
   13095       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   13096       if (flinfo.external_syms == NULL)
   13097 	goto error_return;
   13098 
   13099       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   13100       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   13101       if (flinfo.internal_syms == NULL)
   13102 	goto error_return;
   13103 
   13104       amt = max_sym_count * sizeof (long);
   13105       flinfo.indices = (long int *) bfd_malloc (amt);
   13106       if (flinfo.indices == NULL)
   13107 	goto error_return;
   13108 
   13109       amt = max_sym_count * sizeof (asection *);
   13110       flinfo.sections = (asection **) bfd_malloc (amt);
   13111       if (flinfo.sections == NULL)
   13112 	goto error_return;
   13113     }
   13114 
   13115   if (max_sym_shndx_count != 0)
   13116     {
   13117       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   13118       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   13119       if (flinfo.locsym_shndx == NULL)
   13120 	goto error_return;
   13121     }
   13122 
   13123   if (htab->tls_sec)
   13124     {
   13125       bfd_vma base, end = 0;  /* Both bytes.  */
   13126       asection *sec;
   13127 
   13128       for (sec = htab->tls_sec;
   13129 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   13130 	   sec = sec->next)
   13131 	{
   13132 	  bfd_size_type size = sec->size;
   13133 	  unsigned int opb = bfd_octets_per_byte (abfd, sec);
   13134 
   13135 	  if (size == 0
   13136 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   13137 	    {
   13138 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   13139 
   13140 	      if (ord != NULL)
   13141 		size = ord->offset * opb + ord->size;
   13142 	    }
   13143 	  end = sec->vma + size / opb;
   13144 	}
   13145       base = htab->tls_sec->vma;
   13146       /* Only align end of TLS section if static TLS doesn't have special
   13147 	 alignment requirements.  */
   13148       if (bed->static_tls_alignment == 1)
   13149 	end = align_power (end, htab->tls_sec->alignment_power);
   13150       htab->tls_size = end - base;
   13151     }
   13152 
   13153   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   13154     return false;
   13155 
   13156   /* Finish relative relocations here after regular symbol processing
   13157      is finished if DT_RELR is enabled.  */
   13158   if (info->enable_dt_relr
   13159       && bed->finish_relative_relocs
   13160       && !bed->finish_relative_relocs (info))
   13161     info->callbacks->fatal
   13162       (_("%P: %pB: failed to finish relative relocations\n"), abfd);
   13163 
   13164   /* Since ELF permits relocations to be against local symbols, we
   13165      must have the local symbols available when we do the relocations.
   13166      Since we would rather only read the local symbols once, and we
   13167      would rather not keep them in memory, we handle all the
   13168      relocations for a single input file at the same time.
   13169 
   13170      Unfortunately, there is no way to know the total number of local
   13171      symbols until we have seen all of them, and the local symbol
   13172      indices precede the global symbol indices.  This means that when
   13173      we are generating relocatable output, and we see a reloc against
   13174      a global symbol, we can not know the symbol index until we have
   13175      finished examining all the local symbols to see which ones we are
   13176      going to output.  To deal with this, we keep the relocations in
   13177      memory, and don't output them until the end of the link.  This is
   13178      an unfortunate waste of memory, but I don't see a good way around
   13179      it.  Fortunately, it only happens when performing a relocatable
   13180      link, which is not the common case.  FIXME: If keep_memory is set
   13181      we could write the relocs out and then read them again; I don't
   13182      know how bad the memory loss will be.  */
   13183 
   13184   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13185     sub->output_has_begun = false;
   13186   for (o = abfd->sections; o != NULL; o = o->next)
   13187     {
   13188       for (p = o->map_head.link_order; p != NULL; p = p->next)
   13189 	{
   13190 	  if (p->type == bfd_indirect_link_order
   13191 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   13192 		  == bfd_target_elf_flavour)
   13193 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   13194 	    {
   13195 	      if (! sub->output_has_begun)
   13196 		{
   13197 		  if (! elf_link_input_bfd (&flinfo, sub))
   13198 		    goto error_return;
   13199 		  sub->output_has_begun = true;
   13200 		}
   13201 	    }
   13202 	  else if (p->type == bfd_section_reloc_link_order
   13203 		   || p->type == bfd_symbol_reloc_link_order)
   13204 	    {
   13205 	      if (! elf_reloc_link_order (abfd, info, o, p))
   13206 		goto error_return;
   13207 	    }
   13208 	  else
   13209 	    {
   13210 	      if (! _bfd_default_link_order (abfd, info, o, p))
   13211 		{
   13212 		  if (p->type == bfd_indirect_link_order
   13213 		      && (bfd_get_flavour (sub)
   13214 			  == bfd_target_elf_flavour)
   13215 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   13216 			  != bed->s->elfclass))
   13217 		    {
   13218 		      const char *iclass, *oclass;
   13219 
   13220 		      switch (bed->s->elfclass)
   13221 			{
   13222 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
   13223 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
   13224 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
   13225 			default: abort ();
   13226 			}
   13227 
   13228 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
   13229 			{
   13230 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
   13231 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
   13232 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
   13233 			default: abort ();
   13234 			}
   13235 
   13236 		      bfd_set_error (bfd_error_wrong_format);
   13237 		      _bfd_error_handler
   13238 			/* xgettext:c-format */
   13239 			(_("%pB: file class %s incompatible with %s"),
   13240 			 sub, iclass, oclass);
   13241 		    }
   13242 
   13243 		  goto error_return;
   13244 		}
   13245 	    }
   13246 	}
   13247     }
   13248 
   13249   /* Free symbol buffer if needed.  */
   13250   if (!info->reduce_memory_overheads)
   13251     {
   13252       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13253 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
   13254 	  {
   13255 	    free (elf_tdata (sub)->symbuf);
   13256 	    elf_tdata (sub)->symbuf = NULL;
   13257 	  }
   13258     }
   13259 
   13260   /* Output any global symbols that got converted to local in a
   13261      version script or due to symbol visibility.  We do this in a
   13262      separate step since ELF requires all local symbols to appear
   13263      prior to any global symbols.  FIXME: We should only do this if
   13264      some global symbols were, in fact, converted to become local.
   13265      FIXME: Will this work correctly with the Irix 5 linker?  */
   13266   eoinfo.failed = false;
   13267   eoinfo.flinfo = &flinfo;
   13268   eoinfo.localsyms = true;
   13269   eoinfo.file_sym_done = false;
   13270   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   13271   if (eoinfo.failed)
   13272     goto error_return;
   13273 
   13274   /* If backend needs to output some local symbols not present in the hash
   13275      table, do it now.  */
   13276   if (bed->elf_backend_output_arch_local_syms)
   13277     {
   13278       if (! ((*bed->elf_backend_output_arch_local_syms)
   13279 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   13280 	goto error_return;
   13281     }
   13282 
   13283   /* That wrote out all the local symbols.  Finish up the symbol table
   13284      with the global symbols. Even if we want to strip everything we
   13285      can, we still need to deal with those global symbols that got
   13286      converted to local in a version script.  */
   13287 
   13288   /* The sh_info field records the index of the first non local symbol.  */
   13289   if (!symtab_hdr->sh_info)
   13290     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   13291 
   13292   if (dynamic
   13293       && htab->dynsym != NULL
   13294       && htab->dynsym->output_section != bfd_abs_section_ptr)
   13295     {
   13296       Elf_Internal_Sym sym;
   13297       bfd_byte *dynsym = htab->dynsym->contents;
   13298 
   13299       o = htab->dynsym->output_section;
   13300       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
   13301 
   13302       /* Write out the section symbols for the output sections.  */
   13303       if (bfd_link_pic (info)
   13304 	  || htab->is_relocatable_executable)
   13305 	{
   13306 	  asection *s;
   13307 
   13308 	  sym.st_size = 0;
   13309 	  sym.st_name = 0;
   13310 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   13311 	  sym.st_other = 0;
   13312 	  sym.st_target_internal = 0;
   13313 
   13314 	  for (s = abfd->sections; s != NULL; s = s->next)
   13315 	    {
   13316 	      int indx;
   13317 	      bfd_byte *dest;
   13318 	      long dynindx;
   13319 
   13320 	      dynindx = elf_section_data (s)->dynindx;
   13321 	      if (dynindx <= 0)
   13322 		continue;
   13323 	      indx = elf_section_data (s)->this_idx;
   13324 	      BFD_ASSERT (indx > 0);
   13325 	      sym.st_shndx = indx;
   13326 	      if (! check_dynsym (abfd, &sym))
   13327 		goto error_return;
   13328 	      sym.st_value = s->vma;
   13329 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   13330 
   13331 	      /* Inform the linker of the addition of this symbol.  */
   13332 
   13333 	      if (info->callbacks->ctf_new_dynsym)
   13334 		info->callbacks->ctf_new_dynsym (dynindx, &sym);
   13335 
   13336 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13337 	    }
   13338 	}
   13339 
   13340       /* Write out the local dynsyms.  */
   13341       if (htab->dynlocal)
   13342 	{
   13343 	  struct elf_link_local_dynamic_entry *e;
   13344 	  for (e = htab->dynlocal; e ; e = e->next)
   13345 	    {
   13346 	      asection *s;
   13347 	      bfd_byte *dest;
   13348 
   13349 	      /* Copy the internal symbol and turn off visibility.
   13350 		 Note that we saved a word of storage and overwrote
   13351 		 the original st_name with the dynstr_index.  */
   13352 	      sym = e->isym;
   13353 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   13354 	      sym.st_shndx = SHN_UNDEF;
   13355 
   13356 	      s = bfd_section_from_elf_index (e->input_bfd,
   13357 					      e->isym.st_shndx);
   13358 	      if (s != NULL
   13359 		  && s->output_section != NULL
   13360 		  && elf_section_data (s->output_section) != NULL)
   13361 		{
   13362 		  sym.st_shndx =
   13363 		    elf_section_data (s->output_section)->this_idx;
   13364 		  if (! check_dynsym (abfd, &sym))
   13365 		    goto error_return;
   13366 		  sym.st_value = (s->output_section->vma
   13367 				  + s->output_offset
   13368 				  + e->isym.st_value);
   13369 		}
   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 (e->dynindx, &sym);
   13375 
   13376 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   13377 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13378 	    }
   13379 	}
   13380     }
   13381 
   13382   /* We get the global symbols from the hash table.  */
   13383   eoinfo.failed = false;
   13384   eoinfo.localsyms = false;
   13385   eoinfo.flinfo = &flinfo;
   13386   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   13387   if (eoinfo.failed)
   13388     goto error_return;
   13389 
   13390   /* If backend needs to output some symbols not present in the hash
   13391      table, do it now.  */
   13392   if (bed->elf_backend_output_arch_syms
   13393       && (info->strip != strip_all || emit_relocs))
   13394     {
   13395       if (! ((*bed->elf_backend_output_arch_syms)
   13396 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   13397 	goto error_return;
   13398     }
   13399 
   13400   /* Finalize the .strtab section.  */
   13401   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   13402 
   13403   /* Swap out the .strtab section. */
   13404   if (!elf_link_swap_symbols_out (&flinfo))
   13405     goto error_return;
   13406   free (htab->strtab);
   13407   htab->strtab = NULL;
   13408 
   13409   /* Now we know the size of the symtab section.  */
   13410   if (bfd_get_symcount (abfd) > 0)
   13411     {
   13412       /* Finish up and write out the symbol string table (.strtab)
   13413 	 section.  */
   13414       Elf_Internal_Shdr *symstrtab_hdr = NULL;
   13415       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   13416 
   13417       if (elf_symtab_shndx_list (abfd))
   13418 	{
   13419 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   13420 
   13421 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   13422 	    {
   13423 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   13424 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   13425 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   13426 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   13427 	      symtab_shndx_hdr->sh_size = amt;
   13428 
   13429 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   13430 							       off, true, 0);
   13431 
   13432 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   13433 		  || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
   13434 		goto error_return;
   13435 	    }
   13436 	}
   13437 
   13438       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   13439       /* sh_name was set in prep_headers.  */
   13440       symstrtab_hdr->sh_type = SHT_STRTAB;
   13441       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   13442       symstrtab_hdr->sh_addr = 0;
   13443       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   13444       symstrtab_hdr->sh_entsize = 0;
   13445       symstrtab_hdr->sh_link = 0;
   13446       symstrtab_hdr->sh_info = 0;
   13447       /* sh_offset is set just below.  */
   13448       symstrtab_hdr->sh_addralign = 1;
   13449 
   13450       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   13451 						       off, true, 0);
   13452       elf_next_file_pos (abfd) = off;
   13453 
   13454       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   13455 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   13456 	goto error_return;
   13457     }
   13458 
   13459   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
   13460     {
   13461       _bfd_error_handler (_("%pB: failed to generate import library"),
   13462 			  info->out_implib_bfd);
   13463       goto error_return;
   13464     }
   13465 
   13466   /* Adjust the relocs to have the correct symbol indices.  */
   13467   for (o = abfd->sections; o != NULL; o = o->next)
   13468     {
   13469       struct bfd_elf_section_data *esdo = elf_section_data (o);
   13470       bool sort;
   13471 
   13472       if ((o->flags & SEC_RELOC) == 0)
   13473 	continue;
   13474 
   13475       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   13476       if (esdo->rel.hdr != NULL
   13477 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
   13478 	goto error_return;
   13479       if (esdo->rela.hdr != NULL
   13480 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
   13481 	goto error_return;
   13482 
   13483       /* Set the reloc_count field to 0 to prevent write_relocs from
   13484 	 trying to swap the relocs out itself.  */
   13485       o->reloc_count = 0;
   13486     }
   13487 
   13488   relativecount = 0;
   13489   if (dynamic && info->combreloc && dynobj != NULL)
   13490     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   13491 
   13492   relr_entsize = 0;
   13493   if (htab->srelrdyn != NULL
   13494       && htab->srelrdyn->output_section != NULL
   13495       && htab->srelrdyn->size != 0)
   13496     {
   13497       asection *s = htab->srelrdyn->output_section;
   13498       relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
   13499       if (relr_entsize == 0)
   13500 	{
   13501 	  relr_entsize = bed->s->arch_size / 8;
   13502 	  elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
   13503 	}
   13504     }
   13505 
   13506   /* If we are linking against a dynamic object, or generating a
   13507      shared library, finish up the dynamic linking information.  */
   13508   if (dynamic)
   13509     {
   13510       bfd_byte *dyncon, *dynconend;
   13511 
   13512       /* Fix up .dynamic entries.  */
   13513       o = htab->dynamic;
   13514       BFD_ASSERT (o != NULL);
   13515 
   13516       dyncon = o->contents;
   13517       dynconend = PTR_ADD (o->contents, o->size);
   13518       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13519 	{
   13520 	  Elf_Internal_Dyn dyn;
   13521 	  const char *name;
   13522 	  unsigned int type;
   13523 	  bfd_size_type sh_size;
   13524 	  bfd_vma sh_addr;
   13525 
   13526 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13527 
   13528 	  switch (dyn.d_tag)
   13529 	    {
   13530 	    default:
   13531 	      continue;
   13532 	    case DT_NULL:
   13533 	      if (relativecount != 0)
   13534 		{
   13535 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   13536 		    {
   13537 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   13538 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   13539 		    }
   13540 		  if (dyn.d_tag != DT_NULL
   13541 		      && dynconend - dyncon >= bed->s->sizeof_dyn)
   13542 		    {
   13543 		      dyn.d_un.d_val = relativecount;
   13544 		      relativecount = 0;
   13545 		      break;
   13546 		    }
   13547 		  relativecount = 0;
   13548 		}
   13549 	      if (relr_entsize != 0)
   13550 		{
   13551 		  if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
   13552 		    {
   13553 		      asection *s = htab->srelrdyn;
   13554 		      dyn.d_tag = DT_RELR;
   13555 		      dyn.d_un.d_ptr
   13556 			= s->output_section->vma + s->output_offset;
   13557 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13558 		      dyncon += bed->s->sizeof_dyn;
   13559 
   13560 		      dyn.d_tag = DT_RELRSZ;
   13561 		      dyn.d_un.d_val = s->size;
   13562 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13563 		      dyncon += bed->s->sizeof_dyn;
   13564 
   13565 		      dyn.d_tag = DT_RELRENT;
   13566 		      dyn.d_un.d_val = relr_entsize;
   13567 		      relr_entsize = 0;
   13568 		      break;
   13569 		    }
   13570 		  relr_entsize = 0;
   13571 		}
   13572 	      continue;
   13573 
   13574 	    case DT_INIT:
   13575 	      name = info->init_function;
   13576 	      goto get_sym;
   13577 	    case DT_FINI:
   13578 	      name = info->fini_function;
   13579 	    get_sym:
   13580 	      {
   13581 		struct elf_link_hash_entry *h;
   13582 
   13583 		h = elf_link_hash_lookup (htab, name, false, false, true);
   13584 		if (h != NULL
   13585 		    && (h->root.type == bfd_link_hash_defined
   13586 			|| h->root.type == bfd_link_hash_defweak))
   13587 		  {
   13588 		    dyn.d_un.d_ptr = h->root.u.def.value;
   13589 		    o = h->root.u.def.section;
   13590 		    if (o->output_section != NULL)
   13591 		      dyn.d_un.d_ptr += (o->output_section->vma
   13592 					 + o->output_offset);
   13593 		    else
   13594 		      {
   13595 			/* The symbol is imported from another shared
   13596 			   library and does not apply to this one.  */
   13597 			dyn.d_un.d_ptr = 0;
   13598 		      }
   13599 		    break;
   13600 		  }
   13601 	      }
   13602 	      continue;
   13603 
   13604 	    case DT_PREINIT_ARRAYSZ:
   13605 	      name = ".preinit_array";
   13606 	      goto get_out_size;
   13607 	    case DT_INIT_ARRAYSZ:
   13608 	      name = ".init_array";
   13609 	      goto get_out_size;
   13610 	    case DT_FINI_ARRAYSZ:
   13611 	      name = ".fini_array";
   13612 	    get_out_size:
   13613 	      o = bfd_get_section_by_name (abfd, name);
   13614 	      if (o == NULL)
   13615 		{
   13616 		  _bfd_error_handler
   13617 		    (_("could not find section %s"), name);
   13618 		  goto error_return;
   13619 		}
   13620 	      if (o->size == 0)
   13621 		_bfd_error_handler
   13622 		  (_("warning: %s section has zero size"), name);
   13623 	      dyn.d_un.d_val = o->size;
   13624 	      break;
   13625 
   13626 	    case DT_PREINIT_ARRAY:
   13627 	      name = ".preinit_array";
   13628 	      goto get_out_vma;
   13629 	    case DT_INIT_ARRAY:
   13630 	      name = ".init_array";
   13631 	      goto get_out_vma;
   13632 	    case DT_FINI_ARRAY:
   13633 	      name = ".fini_array";
   13634 	    get_out_vma:
   13635 	      o = bfd_get_section_by_name (abfd, name);
   13636 	      goto do_vma;
   13637 
   13638 	    case DT_HASH:
   13639 	      name = ".hash";
   13640 	      goto get_vma;
   13641 	    case DT_GNU_HASH:
   13642 	      name = ".gnu.hash";
   13643 	      goto get_vma;
   13644 	    case DT_STRTAB:
   13645 	      name = ".dynstr";
   13646 	      goto get_vma;
   13647 	    case DT_SYMTAB:
   13648 	      name = ".dynsym";
   13649 	      goto get_vma;
   13650 	    case DT_VERDEF:
   13651 	      name = ".gnu.version_d";
   13652 	      goto get_vma;
   13653 	    case DT_VERNEED:
   13654 	      name = ".gnu.version_r";
   13655 	      goto get_vma;
   13656 	    case DT_VERSYM:
   13657 	      name = ".gnu.version";
   13658 	    get_vma:
   13659 	      o = bfd_get_linker_section (dynobj, name);
   13660 	    do_vma:
   13661 	      if (o == NULL || bfd_is_abs_section (o->output_section))
   13662 		{
   13663 		  _bfd_error_handler
   13664 		    (_("could not find section %s"), name);
   13665 		  goto error_return;
   13666 		}
   13667 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   13668 		{
   13669 		  _bfd_error_handler
   13670 		    (_("warning: section '%s' is being made into a note"), name);
   13671 		  bfd_set_error (bfd_error_nonrepresentable_section);
   13672 		  goto error_return;
   13673 		}
   13674 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
   13675 	      break;
   13676 
   13677 	    case DT_REL:
   13678 	    case DT_RELA:
   13679 	    case DT_RELSZ:
   13680 	    case DT_RELASZ:
   13681 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   13682 		type = SHT_REL;
   13683 	      else
   13684 		type = SHT_RELA;
   13685 	      sh_size = 0;
   13686 	      sh_addr = 0;
   13687 	      for (i = 1; i < elf_numsections (abfd); i++)
   13688 		{
   13689 		  Elf_Internal_Shdr *hdr;
   13690 
   13691 		  hdr = elf_elfsections (abfd)[i];
   13692 		  if (hdr->sh_type == type
   13693 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   13694 		    {
   13695 		      sh_size += hdr->sh_size;
   13696 		      if (sh_addr == 0
   13697 			  || sh_addr > hdr->sh_addr)
   13698 			sh_addr = hdr->sh_addr;
   13699 		    }
   13700 		}
   13701 
   13702 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
   13703 		{
   13704 		  unsigned int opb = bfd_octets_per_byte (abfd, o);
   13705 
   13706 		  /* Don't count procedure linkage table relocs in the
   13707 		     overall reloc count.  */
   13708 		  sh_size -= htab->srelplt->size;
   13709 		  if (sh_size == 0)
   13710 		    /* If the size is zero, make the address zero too.
   13711 		       This is to avoid a glibc bug.  If the backend
   13712 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
   13713 		       zero, then we'll put DT_RELA at the end of
   13714 		       DT_JMPREL.  glibc will interpret the end of
   13715 		       DT_RELA matching the end of DT_JMPREL as the
   13716 		       case where DT_RELA includes DT_JMPREL, and for
   13717 		       LD_BIND_NOW will decide that processing DT_RELA
   13718 		       will process the PLT relocs too.  Net result:
   13719 		       No PLT relocs applied.  */
   13720 		    sh_addr = 0;
   13721 
   13722 		  /* If .rela.plt is the first .rela section, exclude
   13723 		     it from DT_RELA.  */
   13724 		  else if (sh_addr == (htab->srelplt->output_section->vma
   13725 				       + htab->srelplt->output_offset) * opb)
   13726 		    sh_addr += htab->srelplt->size;
   13727 		}
   13728 
   13729 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   13730 		dyn.d_un.d_val = sh_size;
   13731 	      else
   13732 		dyn.d_un.d_ptr = sh_addr;
   13733 	      break;
   13734 	    }
   13735 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13736 	}
   13737     }
   13738 
   13739   /* If we have created any dynamic sections, then output them.  */
   13740   if (dynobj != NULL)
   13741     {
   13742       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   13743 	goto error_return;
   13744 
   13745       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   13746       if (bfd_link_textrel_check (info)
   13747 	  && (o = htab->dynamic) != NULL
   13748 	  && o->size != 0)
   13749 	{
   13750 	  bfd_byte *dyncon, *dynconend;
   13751 
   13752 	  dyncon = o->contents;
   13753 	  dynconend = o->contents + o->size;
   13754 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13755 	    {
   13756 	      Elf_Internal_Dyn dyn;
   13757 
   13758 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13759 
   13760 	      if (dyn.d_tag == DT_TEXTREL)
   13761 		{
   13762 		  if (info->textrel_check == textrel_check_error)
   13763 		    info->callbacks->einfo
   13764 		      (_("%P%X: read-only segment has dynamic relocations\n"));
   13765 		  else if (bfd_link_dll (info))
   13766 		    info->callbacks->einfo
   13767 		      (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
   13768 		  else if (bfd_link_pde (info))
   13769 		    info->callbacks->einfo
   13770 		      (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
   13771 		  else
   13772 		    info->callbacks->einfo
   13773 		      (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
   13774 		  break;
   13775 		}
   13776 	    }
   13777 	}
   13778 
   13779       for (o = dynobj->sections; o != NULL; o = o->next)
   13780 	{
   13781 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   13782 	      || o->size == 0
   13783 	      || o->output_section == bfd_abs_section_ptr)
   13784 	    continue;
   13785 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   13786 	    {
   13787 	      /* At this point, we are only interested in sections
   13788 		 created by _bfd_elf_link_create_dynamic_sections.  */
   13789 	      continue;
   13790 	    }
   13791 	  if (htab->stab_info.stabstr == o)
   13792 	    continue;
   13793 	  if (htab->eh_info.hdr_sec == o)
   13794 	    continue;
   13795 	  if (strcmp (o->name, ".dynstr") != 0)
   13796 	    {
   13797 	      bfd_size_type octets = ((file_ptr) o->output_offset
   13798 				      * bfd_octets_per_byte (abfd, o));
   13799 	      if (!bfd_set_section_contents (abfd, o->output_section,
   13800 					     o->contents, octets, o->size))
   13801 		goto error_return;
   13802 	    }
   13803 	  else
   13804 	    {
   13805 	      /* The contents of the .dynstr section are actually in a
   13806 		 stringtab.  */
   13807 	      file_ptr off;
   13808 
   13809 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   13810 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   13811 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
   13812 		goto error_return;
   13813 	    }
   13814 	}
   13815     }
   13816 
   13817   if (!info->resolve_section_groups)
   13818     {
   13819       bool failed = false;
   13820 
   13821       BFD_ASSERT (bfd_link_relocatable (info));
   13822       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   13823       if (failed)
   13824 	goto error_return;
   13825     }
   13826 
   13827   /* If we have optimized stabs strings, output them.  */
   13828   if (htab->stab_info.stabstr != NULL)
   13829     {
   13830       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
   13831 	goto error_return;
   13832     }
   13833 
   13834   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   13835     goto error_return;
   13836 
   13837   if (! _bfd_elf_write_section_sframe (abfd, info))
   13838     goto error_return;
   13839 
   13840   if (! _bfd_elf_write_section_build_attributes (abfd, info))
   13841     goto error_ret2;
   13842 
   13843   if (info->callbacks->emit_ctf)
   13844       info->callbacks->emit_ctf ();
   13845 
   13846   elf_final_link_free (abfd, &flinfo);
   13847 
   13848   if (info->unique_symbol)
   13849     bfd_hash_table_free (&flinfo.local_hash_table);
   13850   return true;
   13851 
   13852  error_return:
   13853   free (htab->strtab);
   13854   htab->strtab = NULL;
   13855   elf_final_link_free (abfd, &flinfo);
   13856  error_ret2:
   13857   if (info->unique_symbol)
   13858     bfd_hash_table_free (&flinfo.local_hash_table);
   13859   return false;
   13860 }
   13861 
   13862 /* Initialize COOKIE for input bfd ABFD.  */
   13864 
   13865 static bool
   13866 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   13867 		   struct bfd_link_info *info, bfd *abfd,
   13868 		   bool keep_memory)
   13869 {
   13870   Elf_Internal_Shdr *symtab_hdr;
   13871   const struct elf_backend_data *bed;
   13872 
   13873   bed = get_elf_backend_data (abfd);
   13874   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13875 
   13876   cookie->abfd = abfd;
   13877   cookie->sym_hashes = elf_sym_hashes (abfd);
   13878   cookie->bad_symtab = elf_bad_symtab (abfd);
   13879   if (cookie->bad_symtab)
   13880     {
   13881       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   13882       cookie->extsymoff = 0;
   13883     }
   13884   else
   13885     {
   13886       cookie->locsymcount = symtab_hdr->sh_info;
   13887       cookie->extsymoff = symtab_hdr->sh_info;
   13888     }
   13889 
   13890   if (bed->s->arch_size == 32)
   13891     cookie->r_sym_shift = 8;
   13892   else
   13893     cookie->r_sym_shift = 32;
   13894 
   13895   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   13896   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   13897     {
   13898       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   13899 					      cookie->locsymcount, 0,
   13900 					      NULL, NULL, NULL);
   13901       if (cookie->locsyms == NULL)
   13902 	{
   13903 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   13904 	  return false;
   13905 	}
   13906       if (keep_memory || _bfd_elf_link_keep_memory (info))
   13907 	{
   13908 	  symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   13909 	  info->cache_size += (cookie->locsymcount
   13910 			       * sizeof (Elf_Internal_Sym));
   13911 	}
   13912     }
   13913   return true;
   13914 }
   13915 
   13916 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   13917 
   13918 static void
   13919 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   13920 {
   13921   Elf_Internal_Shdr *symtab_hdr;
   13922 
   13923   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13924   if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   13925     free (cookie->locsyms);
   13926 }
   13927 
   13928 /* Initialize the relocation information in COOKIE for input section SEC
   13929    of input bfd ABFD.  */
   13930 
   13931 static bool
   13932 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13933 			struct bfd_link_info *info, bfd *abfd,
   13934 			asection *sec, bool keep_memory)
   13935 {
   13936   if (sec->reloc_count == 0)
   13937     {
   13938       cookie->rels = NULL;
   13939       cookie->relend = NULL;
   13940     }
   13941   else
   13942     {
   13943       cookie->rels = _bfd_elf_link_info_read_relocs
   13944 	(abfd, info, sec, NULL, NULL,
   13945 	 keep_memory || _bfd_elf_link_keep_memory (info));
   13946       if (cookie->rels == NULL)
   13947 	return false;
   13948       cookie->rel = cookie->rels;
   13949       cookie->relend = cookie->rels + sec->reloc_count;
   13950     }
   13951   cookie->rel = cookie->rels;
   13952   return true;
   13953 }
   13954 
   13955 /* Free the memory allocated by init_reloc_cookie_rels,
   13956    if appropriate.  */
   13957 
   13958 static void
   13959 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13960 			asection *sec)
   13961 {
   13962   if (elf_section_data (sec)->relocs != cookie->rels)
   13963     free (cookie->rels);
   13964 }
   13965 
   13966 /* Initialize the whole of COOKIE for input section SEC.  */
   13967 
   13968 static bool
   13969 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13970 			       struct bfd_link_info *info,
   13971 			       asection *sec, bool keep_memory)
   13972 {
   13973   if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
   13974     goto error1;
   13975   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
   13976 			       keep_memory))
   13977     goto error2;
   13978   return true;
   13979 
   13980  error2:
   13981   fini_reloc_cookie (cookie, sec->owner);
   13982  error1:
   13983   return false;
   13984 }
   13985 
   13986 /* Free the memory allocated by init_reloc_cookie_for_section,
   13987    if appropriate.  */
   13988 
   13989 static void
   13990 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13991 			       asection *sec)
   13992 {
   13993   fini_reloc_cookie_rels (cookie, sec);
   13994   fini_reloc_cookie (cookie, sec->owner);
   13995 }
   13996 
   13997 /* Garbage collect unused sections.  */
   13999 
   14000 /* Default gc_mark_hook.  */
   14001 
   14002 asection *
   14003 _bfd_elf_gc_mark_hook (asection *sec,
   14004 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14005 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   14006 		       struct elf_link_hash_entry *h,
   14007 		       Elf_Internal_Sym *sym)
   14008 {
   14009   if (h == NULL)
   14010     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   14011 
   14012   switch (h->root.type)
   14013     {
   14014     case bfd_link_hash_defined:
   14015     case bfd_link_hash_defweak:
   14016       return h->root.u.def.section;
   14017 
   14018     case bfd_link_hash_common:
   14019       return h->root.u.c.p->section;
   14020 
   14021     default:
   14022       return NULL;
   14023     }
   14024 }
   14025 
   14026 /* Return the debug definition section.  */
   14027 
   14028 static asection *
   14029 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
   14030 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14031 			   Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   14032 			   struct elf_link_hash_entry *h,
   14033 			   Elf_Internal_Sym *sym)
   14034 {
   14035   if (h != NULL)
   14036     {
   14037       /* Return the global debug definition section.  */
   14038       if ((h->root.type == bfd_link_hash_defined
   14039 	   || h->root.type == bfd_link_hash_defweak)
   14040 	  && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
   14041 	return h->root.u.def.section;
   14042     }
   14043   else
   14044     {
   14045       /* Return the local debug definition section.  */
   14046       asection *isec = bfd_section_from_elf_index (sec->owner,
   14047 						   sym->st_shndx);
   14048       if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
   14049 	return isec;
   14050     }
   14051 
   14052   return NULL;
   14053 }
   14054 
   14055 /* COOKIE->rel describes a relocation against section SEC, which is
   14056    a section we've decided to keep.  Return the section that contains
   14057    the relocation symbol, or NULL if no section contains it.  */
   14058 
   14059 asection *
   14060 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   14061 		       elf_gc_mark_hook_fn gc_mark_hook,
   14062 		       struct elf_reloc_cookie *cookie,
   14063 		       bool *start_stop)
   14064 {
   14065   unsigned long r_symndx;
   14066   struct elf_link_hash_entry *h, *hw;
   14067 
   14068   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   14069   if (r_symndx == STN_UNDEF)
   14070     return NULL;
   14071 
   14072   h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
   14073   if (h == NULL)
   14074     {
   14075       /* A corrupt input file can lead to a situation where the index
   14076 	 does not reference either a local or an external symbol.  */
   14077       if (r_symndx >= cookie->locsymcount)
   14078 	return NULL;
   14079 
   14080       return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   14081 			      &cookie->locsyms[r_symndx]);
   14082     }
   14083 
   14084   bool was_marked = h->mark;
   14085 
   14086   h->mark = 1;
   14087   /* Keep all aliases of the symbol too.  If an object symbol
   14088      needs to be copied into .dynbss then all of its aliases
   14089      should be present as dynamic symbols, not just the one used
   14090      on the copy relocation.  */
   14091   hw = h;
   14092   while (hw->is_weakalias)
   14093     {
   14094       hw = hw->u.alias;
   14095       hw->mark = 1;
   14096     }
   14097 
   14098   if (!was_marked && h->start_stop && !h->root.ldscript_def)
   14099     {
   14100       if (info->start_stop_gc)
   14101 	return NULL;
   14102 
   14103       /* To work around a glibc bug, mark XXX input sections
   14104 	 when there is a reference to __start_XXX or __stop_XXX
   14105 	 symbols.  */
   14106       else if (start_stop != NULL)
   14107 	{
   14108 	  asection *s = h->u2.start_stop_section;
   14109 	  *start_stop = true;
   14110 	  return s;
   14111 	}
   14112     }
   14113 
   14114   return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   14115 }
   14116 
   14117 /* COOKIE->rel describes a relocation against section SEC, which is
   14118    a section we've decided to keep.  Mark the section that contains
   14119    the relocation symbol.  */
   14120 
   14121 bool
   14122 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   14123 			asection *sec,
   14124 			elf_gc_mark_hook_fn gc_mark_hook,
   14125 			struct elf_reloc_cookie *cookie)
   14126 {
   14127   asection *rsec;
   14128   bool start_stop = false;
   14129 
   14130   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   14131   while (rsec != NULL)
   14132     {
   14133       if (!rsec->gc_mark)
   14134 	{
   14135 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   14136 	      || (rsec->owner->flags & DYNAMIC) != 0)
   14137 	    rsec->gc_mark = 1;
   14138 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   14139 	    return false;
   14140 	}
   14141       if (!start_stop)
   14142 	break;
   14143       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   14144     }
   14145   return true;
   14146 }
   14147 
   14148 /* The mark phase of garbage collection.  For a given section, mark
   14149    it and any sections in this section's group, and all the sections
   14150    which define symbols to which it refers.  */
   14151 
   14152 bool
   14153 _bfd_elf_gc_mark (struct bfd_link_info *info,
   14154 		  asection *sec,
   14155 		  elf_gc_mark_hook_fn gc_mark_hook)
   14156 {
   14157   bool ret;
   14158   asection *group_sec, *eh_frame;
   14159 
   14160   sec->gc_mark = 1;
   14161 
   14162   /* Mark all the sections in the group.  */
   14163   group_sec = elf_section_data (sec)->next_in_group;
   14164   if (group_sec && !group_sec->gc_mark)
   14165     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   14166       return false;
   14167 
   14168   /* Look through the section relocs.  */
   14169   ret = true;
   14170   eh_frame = elf_eh_frame_section (sec->owner);
   14171   if ((sec->flags & SEC_RELOC) != 0
   14172       && sec->reloc_count > 0
   14173       && sec != eh_frame)
   14174     {
   14175       struct elf_reloc_cookie cookie;
   14176 
   14177       if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
   14178 	ret = false;
   14179       else
   14180 	{
   14181 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   14182 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   14183 	      {
   14184 		ret = false;
   14185 		break;
   14186 	      }
   14187 	  fini_reloc_cookie_for_section (&cookie, sec);
   14188 	}
   14189     }
   14190 
   14191   if (ret && eh_frame && elf_fde_list (sec))
   14192     {
   14193       struct elf_reloc_cookie cookie;
   14194 
   14195       /* NB: When --no-keep-memory is used, the symbol table and
   14196 	 relocation info for eh_frame are freed after they are retrieved
   14197 	 for each text section in the input object.  If an input object
   14198 	 has many text sections, the same data is retrieved and freed
   14199 	 many times which can take a very long time.  Always keep the
   14200 	 symbol table and relocation info for eh_frame to avoid it.  */
   14201       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
   14202 					  true))
   14203 	ret = false;
   14204       else
   14205 	{
   14206 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   14207 				      gc_mark_hook, &cookie))
   14208 	    ret = false;
   14209 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   14210 	}
   14211     }
   14212 
   14213   eh_frame = elf_section_eh_frame_entry (sec);
   14214   if (ret && eh_frame && !eh_frame->gc_mark)
   14215     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   14216       ret = false;
   14217 
   14218   return ret;
   14219 }
   14220 
   14221 /* Scan and mark sections in a special or debug section group.  */
   14222 
   14223 static void
   14224 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   14225 {
   14226   /* Point to first section of section group.  */
   14227   asection *ssec;
   14228   /* Used to iterate the section group.  */
   14229   asection *msec;
   14230 
   14231   bool is_special_grp = true;
   14232   bool is_debug_grp = true;
   14233 
   14234   /* First scan to see if group contains any section other than debug
   14235      and special section.  */
   14236   ssec = msec = elf_next_in_group (grp);
   14237   do
   14238     {
   14239       if ((msec->flags & SEC_DEBUGGING) == 0)
   14240 	is_debug_grp = false;
   14241 
   14242       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   14243 	is_special_grp = false;
   14244 
   14245       msec = elf_next_in_group (msec);
   14246     }
   14247   while (msec != ssec);
   14248 
   14249   /* If this is a pure debug section group or pure special section group,
   14250      keep all sections in this group.  */
   14251   if (is_debug_grp || is_special_grp)
   14252     {
   14253       do
   14254 	{
   14255 	  msec->gc_mark = 1;
   14256 	  msec = elf_next_in_group (msec);
   14257 	}
   14258       while (msec != ssec);
   14259     }
   14260 }
   14261 
   14262 /* Keep debug and special sections.  */
   14263 
   14264 bool
   14265 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   14266 				 elf_gc_mark_hook_fn mark_hook)
   14267 {
   14268   bfd *ibfd;
   14269 
   14270   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14271     {
   14272       asection *isec;
   14273       bool some_kept;
   14274       bool debug_frag_seen;
   14275       bool has_kept_debug_info;
   14276 
   14277       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14278 	continue;
   14279       isec = ibfd->sections;
   14280       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14281 	continue;
   14282 
   14283       /* Ensure all linker created sections are kept,
   14284 	 see if any other section is already marked,
   14285 	 and note if we have any fragmented debug sections.  */
   14286       debug_frag_seen = some_kept = has_kept_debug_info = false;
   14287       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14288 	{
   14289 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   14290 	    isec->gc_mark = 1;
   14291 	  else if (isec->gc_mark
   14292 		   && (isec->flags & SEC_ALLOC) != 0
   14293 		   && elf_section_type (isec) != SHT_NOTE)
   14294 	    some_kept = true;
   14295 	  else
   14296 	    {
   14297 	      /* Since all sections, except for backend specific ones,
   14298 		 have been garbage collected, call mark_hook on this
   14299 		 section if any of its linked-to sections is marked.  */
   14300 	      asection *linked_to_sec;
   14301 	      for (linked_to_sec = elf_linked_to_section (isec);
   14302 		   linked_to_sec != NULL && !linked_to_sec->linker_mark;
   14303 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14304 		{
   14305 		  if (linked_to_sec->gc_mark)
   14306 		    {
   14307 		      if (!_bfd_elf_gc_mark (info, isec, mark_hook))
   14308 			return false;
   14309 		      break;
   14310 		    }
   14311 		  linked_to_sec->linker_mark = 1;
   14312 		}
   14313 	      for (linked_to_sec = elf_linked_to_section (isec);
   14314 		   linked_to_sec != NULL && linked_to_sec->linker_mark;
   14315 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14316 		linked_to_sec->linker_mark = 0;
   14317 	    }
   14318 
   14319 	  if (!debug_frag_seen
   14320 	      && (isec->flags & SEC_DEBUGGING)
   14321 	      && startswith (isec->name, ".debug_line."))
   14322 	    debug_frag_seen = true;
   14323 	  else if (strcmp (bfd_section_name (isec),
   14324 			   "__patchable_function_entries") == 0
   14325 		   && elf_linked_to_section (isec) == NULL)
   14326 	      info->callbacks->fatal (_("%P: %pB(%pA): error: "
   14327 					"need linked-to section "
   14328 					"for --gc-sections\n"),
   14329 				      isec->owner, isec);
   14330 	}
   14331 
   14332       /* If no non-note alloc section in this file will be kept, then
   14333 	 we can toss out the debug and special sections.  */
   14334       if (!some_kept)
   14335 	continue;
   14336 
   14337       /* Keep debug and special sections like .comment when they are
   14338 	 not part of a group.  Also keep section groups that contain
   14339 	 just debug sections or special sections.  NB: Sections with
   14340 	 linked-to section has been handled above.  */
   14341       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14342 	{
   14343 	  if ((isec->flags & SEC_GROUP) != 0)
   14344 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   14345 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   14346 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   14347 		   && elf_next_in_group (isec) == NULL
   14348 		   && elf_linked_to_section (isec) == NULL)
   14349 	    isec->gc_mark = 1;
   14350 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
   14351 	    has_kept_debug_info = true;
   14352 	}
   14353 
   14354       /* Look for CODE sections which are going to be discarded,
   14355 	 and find and discard any fragmented debug sections which
   14356 	 are associated with that code section.  */
   14357       if (debug_frag_seen)
   14358 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14359 	  if ((isec->flags & SEC_CODE) != 0
   14360 	      && isec->gc_mark == 0)
   14361 	    {
   14362 	      unsigned int ilen;
   14363 	      asection *dsec;
   14364 
   14365 	      ilen = strlen (isec->name);
   14366 
   14367 	      /* Association is determined by the name of the debug
   14368 		 section containing the name of the code section as
   14369 		 a suffix.  For example .debug_line.text.foo is a
   14370 		 debug section associated with .text.foo.  */
   14371 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   14372 		{
   14373 		  unsigned int dlen;
   14374 
   14375 		  if (dsec->gc_mark == 0
   14376 		      || (dsec->flags & SEC_DEBUGGING) == 0)
   14377 		    continue;
   14378 
   14379 		  dlen = strlen (dsec->name);
   14380 
   14381 		  if (dlen > ilen
   14382 		      && strncmp (dsec->name + (dlen - ilen),
   14383 				  isec->name, ilen) == 0)
   14384 		    dsec->gc_mark = 0;
   14385 		}
   14386 	  }
   14387 
   14388       /* Mark debug sections referenced by kept debug sections.  */
   14389       if (has_kept_debug_info)
   14390 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14391 	  if (isec->gc_mark
   14392 	      && (isec->flags & SEC_DEBUGGING) != 0)
   14393 	    if (!_bfd_elf_gc_mark (info, isec,
   14394 				   elf_gc_mark_debug_section))
   14395 	      return false;
   14396     }
   14397   return true;
   14398 }
   14399 
   14400 static bool
   14401 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   14402 {
   14403   bfd *sub;
   14404   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14405 
   14406   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14407     {
   14408       asection *o;
   14409 
   14410       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14411 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
   14412 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14413 	continue;
   14414       o = sub->sections;
   14415       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14416 	continue;
   14417 
   14418       for (o = sub->sections; o != NULL; o = o->next)
   14419 	{
   14420 	  /* When any section in a section group is kept, we keep all
   14421 	     sections in the section group.  If the first member of
   14422 	     the section group is excluded, we will also exclude the
   14423 	     group section.  */
   14424 	  if (o->flags & SEC_GROUP)
   14425 	    {
   14426 	      asection *first = elf_next_in_group (o);
   14427 	      if (first != NULL)
   14428 		o->gc_mark = first->gc_mark;
   14429 	    }
   14430 
   14431 	  if (o->gc_mark)
   14432 	    continue;
   14433 
   14434 	  /* Skip sweeping sections already excluded.  */
   14435 	  if (o->flags & SEC_EXCLUDE)
   14436 	    continue;
   14437 
   14438 	  /* Since this is early in the link process, it is simple
   14439 	     to remove a section from the output.  */
   14440 	  o->flags |= SEC_EXCLUDE;
   14441 
   14442 	  if (info->print_gc_sections && o->size != 0)
   14443 	    /* xgettext:c-format */
   14444 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
   14445 				o, sub);
   14446 	}
   14447     }
   14448 
   14449   return true;
   14450 }
   14451 
   14452 /* Propagate collected vtable information.  This is called through
   14453    elf_link_hash_traverse.  */
   14454 
   14455 static bool
   14456 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   14457 {
   14458   /* Those that are not vtables.  */
   14459   if (h->start_stop
   14460       || h->u2.vtable == NULL
   14461       || h->u2.vtable->parent == NULL)
   14462     return true;
   14463 
   14464   /* Those vtables that do not have parents, we cannot merge.  */
   14465   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
   14466     return true;
   14467 
   14468   /* If we've already been done, exit.  */
   14469   if (h->u2.vtable->used && h->u2.vtable->used[-1])
   14470     return true;
   14471 
   14472   /* Make sure the parent's table is up to date.  */
   14473   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
   14474 
   14475   if (h->u2.vtable->used == NULL)
   14476     {
   14477       /* None of this table's entries were referenced.  Re-use the
   14478 	 parent's table.  */
   14479       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
   14480       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
   14481     }
   14482   else
   14483     {
   14484       size_t n;
   14485       bool *cu, *pu;
   14486 
   14487       /* Or the parent's entries into ours.  */
   14488       cu = h->u2.vtable->used;
   14489       cu[-1] = true;
   14490       pu = h->u2.vtable->parent->u2.vtable->used;
   14491       if (pu != NULL)
   14492 	{
   14493 	  const struct elf_backend_data *bed;
   14494 	  unsigned int log_file_align;
   14495 
   14496 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   14497 	  log_file_align = bed->s->log_file_align;
   14498 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
   14499 	  while (n--)
   14500 	    {
   14501 	      if (*pu)
   14502 		*cu = true;
   14503 	      pu++;
   14504 	      cu++;
   14505 	    }
   14506 	}
   14507     }
   14508 
   14509   return true;
   14510 }
   14511 
   14512 struct link_info_ok
   14513 {
   14514   struct bfd_link_info *info;
   14515   bool ok;
   14516 };
   14517 
   14518 static bool
   14519 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
   14520 				    void *ptr)
   14521 {
   14522   asection *sec;
   14523   bfd_vma hstart, hend;
   14524   Elf_Internal_Rela *relstart, *relend, *rel;
   14525   const struct elf_backend_data *bed;
   14526   unsigned int log_file_align;
   14527   struct link_info_ok *info = (struct link_info_ok *) ptr;
   14528 
   14529   /* Take care of both those symbols that do not describe vtables as
   14530      well as those that are not loaded.  */
   14531   if (h->start_stop
   14532       || h->u2.vtable == NULL
   14533       || h->u2.vtable->parent == NULL)
   14534     return true;
   14535 
   14536   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   14537 	      || h->root.type == bfd_link_hash_defweak);
   14538 
   14539   sec = h->root.u.def.section;
   14540   hstart = h->root.u.def.value;
   14541   hend = hstart + h->size;
   14542 
   14543   relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
   14544 					     sec, NULL, NULL, true);
   14545   if (!relstart)
   14546     return info->ok = false;
   14547   bed = get_elf_backend_data (sec->owner);
   14548   log_file_align = bed->s->log_file_align;
   14549 
   14550   relend = relstart + sec->reloc_count;
   14551 
   14552   for (rel = relstart; rel < relend; ++rel)
   14553     if (rel->r_offset >= hstart && rel->r_offset < hend)
   14554       {
   14555 	/* If the entry is in use, do nothing.  */
   14556 	if (h->u2.vtable->used
   14557 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
   14558 	  {
   14559 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   14560 	    if (h->u2.vtable->used[entry])
   14561 	      continue;
   14562 	  }
   14563 	/* Otherwise, kill it.  */
   14564 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   14565       }
   14566 
   14567   return true;
   14568 }
   14569 
   14570 /* Mark sections containing dynamically referenced symbols.  When
   14571    building shared libraries, we must assume that any visible symbol is
   14572    referenced.  */
   14573 
   14574 bool
   14575 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   14576 {
   14577   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   14578   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   14579 
   14580   if ((h->root.type == bfd_link_hash_defined
   14581        || h->root.type == bfd_link_hash_defweak)
   14582       && (!h->start_stop
   14583 	  || h->root.ldscript_def
   14584 	  || !info->start_stop_gc)
   14585       && ((h->ref_dynamic && !h->forced_local)
   14586 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   14587 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   14588 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   14589 	      && (!bfd_link_executable (info)
   14590 		  || info->gc_keep_exported
   14591 		  || info->export_dynamic
   14592 		  || (h->dynamic
   14593 		      && d != NULL
   14594 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   14595 	      && (h->versioned >= versioned
   14596 		  || !bfd_hide_sym_by_version (info->version_info,
   14597 					       h->root.root.string)))))
   14598     h->root.u.def.section->flags |= SEC_KEEP;
   14599 
   14600   return true;
   14601 }
   14602 
   14603 /* Keep all sections containing symbols undefined on the command-line,
   14604    and the section containing the entry symbol.  */
   14605 
   14606 void
   14607 _bfd_elf_gc_keep (struct bfd_link_info *info)
   14608 {
   14609   struct bfd_sym_chain *sym;
   14610 
   14611   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   14612     {
   14613       struct elf_link_hash_entry *h;
   14614 
   14615       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   14616 				false, false, false);
   14617 
   14618       if (h != NULL
   14619 	  && (h->root.type == bfd_link_hash_defined
   14620 	      || h->root.type == bfd_link_hash_defweak)
   14621 	  && !bfd_is_const_section (h->root.u.def.section))
   14622 	h->root.u.def.section->flags |= SEC_KEEP;
   14623     }
   14624 }
   14625 
   14626 bool
   14627 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   14628 				struct bfd_link_info *info)
   14629 {
   14630   bfd *ibfd = info->input_bfds;
   14631 
   14632   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14633     {
   14634       asection *sec;
   14635       struct elf_reloc_cookie cookie;
   14636 
   14637       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14638 	continue;
   14639       sec = ibfd->sections;
   14640       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14641 	continue;
   14642 
   14643       if (!init_reloc_cookie (&cookie, info, ibfd, false))
   14644 	return false;
   14645 
   14646       for (sec = ibfd->sections; sec; sec = sec->next)
   14647 	{
   14648 	  if (startswith (bfd_section_name (sec), ".eh_frame_entry")
   14649 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
   14650 					 false))
   14651 	    {
   14652 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   14653 	      fini_reloc_cookie_rels (&cookie, sec);
   14654 	    }
   14655 	}
   14656     }
   14657   return true;
   14658 }
   14659 
   14660 /* Do mark and sweep of unused sections.  */
   14661 
   14662 bool
   14663 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   14664 {
   14665   bool ok = true;
   14666   bfd *sub;
   14667   elf_gc_mark_hook_fn gc_mark_hook;
   14668   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14669   struct elf_link_hash_table *htab;
   14670   struct link_info_ok info_ok;
   14671 
   14672   if (!bed->can_gc_sections
   14673       || !is_elf_hash_table (info->hash))
   14674     {
   14675       _bfd_error_handler(_("warning: gc-sections option ignored"));
   14676       return true;
   14677     }
   14678 
   14679   bed->gc_keep (info);
   14680   htab = elf_hash_table (info);
   14681 
   14682   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   14683      at the .eh_frame section if we can mark the FDEs individually.  */
   14684   for (sub = info->input_bfds;
   14685        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   14686        sub = sub->link.next)
   14687     {
   14688       asection *sec;
   14689       struct elf_reloc_cookie cookie;
   14690 
   14691       sec = sub->sections;
   14692       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14693 	continue;
   14694       sec = bfd_get_section_by_name (sub, ".eh_frame");
   14695       while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
   14696 						   false))
   14697 	{
   14698 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   14699 	  if (elf_section_data (sec)->sec_info
   14700 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   14701 	    elf_eh_frame_section (sub) = sec;
   14702 	  fini_reloc_cookie_for_section (&cookie, sec);
   14703 	  sec = bfd_get_next_section_by_name (NULL, sec);
   14704 	}
   14705     }
   14706 
   14707   /* Apply transitive closure to the vtable entry usage info.  */
   14708   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   14709   if (!ok)
   14710     return false;
   14711 
   14712   /* Kill the vtable relocations that were not used.  */
   14713   info_ok.info = info;
   14714   info_ok.ok = true;
   14715   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
   14716   if (!info_ok.ok)
   14717     return false;
   14718 
   14719   /* Mark dynamically referenced symbols.  */
   14720   if (htab->dynamic_sections_created || info->gc_keep_exported)
   14721     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   14722 
   14723   /* Grovel through relocs to find out who stays ...  */
   14724   gc_mark_hook = bed->gc_mark_hook;
   14725   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14726     {
   14727       asection *o;
   14728 
   14729       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14730 	  || elf_object_id (sub) != elf_hash_table_id (htab)
   14731 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14732 	continue;
   14733 
   14734       o = sub->sections;
   14735       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14736 	continue;
   14737 
   14738       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   14739 	 Also treat note sections as a root, if the section is not part
   14740 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
   14741 	 well as FINI_ARRAY sections for ld -r.  */
   14742       for (o = sub->sections; o != NULL; o = o->next)
   14743 	if (!o->gc_mark
   14744 	    && (o->flags & SEC_EXCLUDE) == 0
   14745 	    && ((o->flags & SEC_KEEP) != 0
   14746 		|| (bfd_link_relocatable (info)
   14747 		    && ((elf_section_data (o)->this_hdr.sh_type
   14748 			 == SHT_PREINIT_ARRAY)
   14749 			|| (elf_section_data (o)->this_hdr.sh_type
   14750 			    == SHT_INIT_ARRAY)
   14751 			|| (elf_section_data (o)->this_hdr.sh_type
   14752 			    == SHT_FINI_ARRAY)))
   14753 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   14754 		    && elf_next_in_group (o) == NULL
   14755 		    && elf_linked_to_section (o) == NULL)
   14756 		|| ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
   14757 		    && (elf_section_flags (o) & SHF_GNU_RETAIN))))
   14758 	  {
   14759 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   14760 	      return false;
   14761 	  }
   14762     }
   14763 
   14764   /* Allow the backend to mark additional target specific sections.  */
   14765   bed->gc_mark_extra_sections (info, gc_mark_hook);
   14766 
   14767   /* ... and mark SEC_EXCLUDE for those that go.  */
   14768   return elf_gc_sweep (abfd, info);
   14769 }
   14770 
   14771 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   14773 
   14774 bool
   14775 bfd_elf_gc_record_vtinherit (bfd *abfd,
   14776 			     asection *sec,
   14777 			     struct elf_link_hash_entry *h,
   14778 			     bfd_vma offset)
   14779 {
   14780   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   14781   struct elf_link_hash_entry **search, *child;
   14782   size_t extsymcount;
   14783   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14784 
   14785   /* The sh_info field of the symtab header tells us where the
   14786      external symbols start.  We don't care about the local symbols at
   14787      this point.  */
   14788   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   14789   if (!elf_bad_symtab (abfd))
   14790     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   14791 
   14792   sym_hashes = elf_sym_hashes (abfd);
   14793   sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
   14794 
   14795   /* Hunt down the child symbol, which is in this section at the same
   14796      offset as the relocation.  */
   14797   for (search = sym_hashes; search != sym_hashes_end; ++search)
   14798     {
   14799       if ((child = *search) != NULL
   14800 	  && (child->root.type == bfd_link_hash_defined
   14801 	      || child->root.type == bfd_link_hash_defweak)
   14802 	  && child->root.u.def.section == sec
   14803 	  && child->root.u.def.value == offset)
   14804 	goto win;
   14805     }
   14806 
   14807   /* xgettext:c-format */
   14808   _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
   14809 		      abfd, sec, (uint64_t) offset);
   14810   bfd_set_error (bfd_error_invalid_operation);
   14811   return false;
   14812 
   14813  win:
   14814   if (!child->u2.vtable)
   14815     {
   14816       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14817 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
   14818       if (!child->u2.vtable)
   14819 	return false;
   14820     }
   14821   if (!h)
   14822     {
   14823       /* This *should* only be the absolute section.  It could potentially
   14824 	 be that someone has defined a non-global vtable though, which
   14825 	 would be bad.  It isn't worth paging in the local symbols to be
   14826 	 sure though; that case should simply be handled by the assembler.  */
   14827 
   14828       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
   14829     }
   14830   else
   14831     child->u2.vtable->parent = h;
   14832 
   14833   return true;
   14834 }
   14835 
   14836 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   14837 
   14838 bool
   14839 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
   14840 			   struct elf_link_hash_entry *h,
   14841 			   bfd_vma addend)
   14842 {
   14843   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14844   unsigned int log_file_align = bed->s->log_file_align;
   14845 
   14846   if (!h)
   14847     {
   14848       /* xgettext:c-format */
   14849       _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
   14850 			  abfd, sec);
   14851       bfd_set_error (bfd_error_bad_value);
   14852       return false;
   14853     }
   14854 
   14855   if (!h->u2.vtable)
   14856     {
   14857       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14858 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
   14859       if (!h->u2.vtable)
   14860 	return false;
   14861     }
   14862 
   14863   if (addend >= h->u2.vtable->size)
   14864     {
   14865       size_t size, bytes, file_align;
   14866       bool *ptr = h->u2.vtable->used;
   14867 
   14868       /* While the symbol is undefined, we have to be prepared to handle
   14869 	 a zero size.  */
   14870       file_align = 1 << log_file_align;
   14871       if (h->root.type == bfd_link_hash_undefined)
   14872 	size = addend + file_align;
   14873       else
   14874 	{
   14875 	  size = h->size;
   14876 	  if (addend >= size)
   14877 	    {
   14878 	      /* Oops!  We've got a reference past the defined end of
   14879 		 the table.  This is probably a bug -- shall we warn?  */
   14880 	      size = addend + file_align;
   14881 	    }
   14882 	}
   14883       size = (size + file_align - 1) & -file_align;
   14884 
   14885       /* Allocate one extra entry for use as a "done" flag for the
   14886 	 consolidation pass.  */
   14887       bytes = ((size >> log_file_align) + 1) * sizeof (bool);
   14888 
   14889       if (ptr)
   14890 	{
   14891 	  ptr = (bool *) bfd_realloc (ptr - 1, bytes);
   14892 
   14893 	  if (ptr != NULL)
   14894 	    {
   14895 	      size_t oldbytes;
   14896 
   14897 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
   14898 			  * sizeof (bool));
   14899 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   14900 	    }
   14901 	}
   14902       else
   14903 	ptr = (bool *) bfd_zmalloc (bytes);
   14904 
   14905       if (ptr == NULL)
   14906 	return false;
   14907 
   14908       /* And arrange for that done flag to be at index -1.  */
   14909       h->u2.vtable->used = ptr + 1;
   14910       h->u2.vtable->size = size;
   14911     }
   14912 
   14913   h->u2.vtable->used[addend >> log_file_align] = true;
   14914 
   14915   return true;
   14916 }
   14917 
   14918 /* Map an ELF section header flag to its corresponding string.  */
   14919 typedef struct
   14920 {
   14921   char *flag_name;
   14922   flagword flag_value;
   14923 } elf_flags_to_name_table;
   14924 
   14925 static const elf_flags_to_name_table elf_flags_to_names [] =
   14926 {
   14927   { "SHF_WRITE", SHF_WRITE },
   14928   { "SHF_ALLOC", SHF_ALLOC },
   14929   { "SHF_EXECINSTR", SHF_EXECINSTR },
   14930   { "SHF_MERGE", SHF_MERGE },
   14931   { "SHF_STRINGS", SHF_STRINGS },
   14932   { "SHF_INFO_LINK", SHF_INFO_LINK},
   14933   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   14934   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   14935   { "SHF_GROUP", SHF_GROUP },
   14936   { "SHF_TLS", SHF_TLS },
   14937   { "SHF_MASKOS", SHF_MASKOS },
   14938   { "SHF_EXCLUDE", SHF_EXCLUDE },
   14939 };
   14940 
   14941 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   14942 bool
   14943 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   14944 			      struct flag_info *flaginfo,
   14945 			      asection *section)
   14946 {
   14947   const bfd_vma sh_flags = elf_section_flags (section);
   14948 
   14949   if (!flaginfo->flags_initialized)
   14950     {
   14951       bfd *obfd = info->output_bfd;
   14952       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   14953       struct flag_info_list *tf = flaginfo->flag_list;
   14954       int with_hex = 0;
   14955       int without_hex = 0;
   14956 
   14957       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   14958 	{
   14959 	  unsigned i;
   14960 	  flagword (*lookup) (char *);
   14961 
   14962 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   14963 	  if (lookup != NULL)
   14964 	    {
   14965 	      flagword hexval = (*lookup) ((char *) tf->name);
   14966 
   14967 	      if (hexval != 0)
   14968 		{
   14969 		  if (tf->with == with_flags)
   14970 		    with_hex |= hexval;
   14971 		  else if (tf->with == without_flags)
   14972 		    without_hex |= hexval;
   14973 		  tf->valid = true;
   14974 		  continue;
   14975 		}
   14976 	    }
   14977 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   14978 	    {
   14979 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   14980 		{
   14981 		  if (tf->with == with_flags)
   14982 		    with_hex |= elf_flags_to_names[i].flag_value;
   14983 		  else if (tf->with == without_flags)
   14984 		    without_hex |= elf_flags_to_names[i].flag_value;
   14985 		  tf->valid = true;
   14986 		  break;
   14987 		}
   14988 	    }
   14989 	  if (!tf->valid)
   14990 	    {
   14991 	      info->callbacks->einfo
   14992 		(_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   14993 	      return false;
   14994 	    }
   14995 	}
   14996       flaginfo->flags_initialized = true;
   14997       flaginfo->only_with_flags |= with_hex;
   14998       flaginfo->not_with_flags |= without_hex;
   14999     }
   15000 
   15001   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   15002     return false;
   15003 
   15004   if ((flaginfo->not_with_flags & sh_flags) != 0)
   15005     return false;
   15006 
   15007   return true;
   15008 }
   15009 
   15010 struct alloc_got_off_arg {
   15011   bfd_vma gotoff;
   15012   struct bfd_link_info *info;
   15013 };
   15014 
   15015 /* We need a special top-level link routine to convert got reference counts
   15016    to real got offsets.  */
   15017 
   15018 static bool
   15019 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   15020 {
   15021   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   15022   bfd *obfd = gofarg->info->output_bfd;
   15023   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   15024 
   15025   if (h->got.refcount > 0)
   15026     {
   15027       h->got.offset = gofarg->gotoff;
   15028       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   15029     }
   15030   else
   15031     h->got.offset = (bfd_vma) -1;
   15032 
   15033   return true;
   15034 }
   15035 
   15036 /* And an accompanying bit to work out final got entry offsets once
   15037    we're done.  Should be called from final_link.  */
   15038 
   15039 bool
   15040 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   15041 					struct bfd_link_info *info)
   15042 {
   15043   bfd *i;
   15044   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15045   bfd_vma gotoff;
   15046   struct alloc_got_off_arg gofarg;
   15047 
   15048   BFD_ASSERT (abfd == info->output_bfd);
   15049 
   15050   if (! is_elf_hash_table (info->hash))
   15051     return false;
   15052 
   15053   /* The GOT offset is relative to the .got section, but the GOT header is
   15054      put into the .got.plt section, if the backend uses it.  */
   15055   if (bed->want_got_plt)
   15056     gotoff = 0;
   15057   else
   15058     gotoff = bed->got_header_size;
   15059 
   15060   /* Do the local .got entries first.  */
   15061   for (i = info->input_bfds; i; i = i->link.next)
   15062     {
   15063       bfd_signed_vma *local_got;
   15064       size_t j, locsymcount;
   15065       Elf_Internal_Shdr *symtab_hdr;
   15066 
   15067       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   15068 	continue;
   15069 
   15070       local_got = elf_local_got_refcounts (i);
   15071       if (!local_got)
   15072 	continue;
   15073 
   15074       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   15075       if (elf_bad_symtab (i))
   15076 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   15077       else
   15078 	locsymcount = symtab_hdr->sh_info;
   15079 
   15080       for (j = 0; j < locsymcount; ++j)
   15081 	{
   15082 	  if (local_got[j] > 0)
   15083 	    {
   15084 	      local_got[j] = gotoff;
   15085 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   15086 	    }
   15087 	  else
   15088 	    local_got[j] = (bfd_vma) -1;
   15089 	}
   15090     }
   15091 
   15092   /* Then the global .got entries.  .plt refcounts are handled by
   15093      adjust_dynamic_symbol  */
   15094   gofarg.gotoff = gotoff;
   15095   gofarg.info = info;
   15096   elf_link_hash_traverse (elf_hash_table (info),
   15097 			  elf_gc_allocate_got_offsets,
   15098 			  &gofarg);
   15099   return true;
   15100 }
   15101 
   15102 /* Many folk need no more in the way of final link than this, once
   15103    got entry reference counting is enabled.  */
   15104 
   15105 bool
   15106 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   15107 {
   15108   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   15109     return false;
   15110 
   15111   /* Invoke the regular ELF backend linker to do all the work.  */
   15112   return bfd_elf_final_link (abfd, info);
   15113 }
   15114 
   15115 bool
   15116 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   15117 {
   15118   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   15119 
   15120   if (rcookie->bad_symtab)
   15121     rcookie->rel = rcookie->rels;
   15122 
   15123   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   15124     {
   15125       unsigned long r_symndx;
   15126 
   15127       if (! rcookie->bad_symtab)
   15128 	if (rcookie->rel->r_offset > offset)
   15129 	  return false;
   15130       if (rcookie->rel->r_offset != offset)
   15131 	continue;
   15132 
   15133       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   15134       if (r_symndx == STN_UNDEF)
   15135 	return true;
   15136 
   15137       struct elf_link_hash_entry *h;
   15138 
   15139       h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
   15140 
   15141       if (h != NULL)
   15142 	{
   15143 	  if ((h->root.type == bfd_link_hash_defined
   15144 	       || h->root.type == bfd_link_hash_defweak)
   15145 	      && (h->root.u.def.section->owner != rcookie->abfd
   15146 		  || h->root.u.def.section->kept_section != NULL
   15147 		  || discarded_section (h->root.u.def.section)))
   15148 	    return true;
   15149 	}
   15150       else
   15151 	{
   15152 	  if (r_symndx >= rcookie->locsymcount)
   15153 	    /* This can happen with corrupt input.  */
   15154 	    return false;
   15155 
   15156 	  /* It's not a relocation against a global symbol,
   15157 	     but it could be a relocation against a local
   15158 	     symbol for a discarded section.  */
   15159 	  asection *isec;
   15160 	  Elf_Internal_Sym *isym;
   15161 
   15162 	  /* Need to: get the symbol; get the section.  */
   15163 	  isym = &rcookie->locsyms[r_symndx];
   15164 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   15165 	  if (isec != NULL
   15166 	      && (isec->kept_section != NULL
   15167 		  || discarded_section (isec)))
   15168 	    return true;
   15169 	}
   15170 
   15171       return false;
   15172     }
   15173   return false;
   15174 }
   15175 
   15176 /* Discard unneeded references to discarded sections.
   15177    Returns -1 on error, 1 if any section's size was changed, 0 if
   15178    nothing changed.  This function assumes that the relocations are in
   15179    sorted order, which is true for all known assemblers.  */
   15180 
   15181 int
   15182 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   15183 {
   15184   struct elf_reloc_cookie cookie;
   15185   asection *o;
   15186   bfd *abfd;
   15187   int changed = 0;
   15188 
   15189   if (info->traditional_format
   15190       || !is_elf_hash_table (info->hash))
   15191     return 0;
   15192 
   15193   o = bfd_get_section_by_name (output_bfd, ".stab");
   15194   if (o != NULL)
   15195     {
   15196       asection *i;
   15197 
   15198       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15199 	{
   15200 	  if (i->size == 0
   15201 	      || i->reloc_count == 0
   15202 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   15203 	    continue;
   15204 
   15205 	  abfd = i->owner;
   15206 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15207 	    continue;
   15208 
   15209 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15210 	    return -1;
   15211 
   15212 	  if (_bfd_discard_section_stabs (abfd, i,
   15213 					  elf_section_data (i)->sec_info,
   15214 					  bfd_elf_reloc_symbol_deleted_p,
   15215 					  &cookie))
   15216 	    changed = 1;
   15217 
   15218 	  fini_reloc_cookie_for_section (&cookie, i);
   15219 	}
   15220     }
   15221 
   15222   o = NULL;
   15223   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   15224     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   15225   if (o != NULL)
   15226     {
   15227       asection *i;
   15228       int eh_changed = 0;
   15229       unsigned int eh_alignment;  /* Octets.  */
   15230 
   15231       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15232 	{
   15233 	  if (i->size == 0)
   15234 	    continue;
   15235 
   15236 	  abfd = i->owner;
   15237 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15238 	    continue;
   15239 
   15240 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15241 	    return -1;
   15242 
   15243 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   15244 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   15245 						 bfd_elf_reloc_symbol_deleted_p,
   15246 						 &cookie))
   15247 	    {
   15248 	      eh_changed = 1;
   15249 	      if (i->size != i->rawsize)
   15250 		changed = 1;
   15251 	    }
   15252 
   15253 	  fini_reloc_cookie_for_section (&cookie, i);
   15254 	}
   15255 
   15256       eh_alignment = ((1 << o->alignment_power)
   15257 		      * bfd_octets_per_byte (output_bfd, o));
   15258       /* Skip over zero terminator, and prevent empty sections from
   15259 	 adding alignment padding at the end.  */
   15260       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
   15261 	if (i->size == 0)
   15262 	  i->flags |= SEC_EXCLUDE;
   15263 	else if (i->size > 4)
   15264 	  break;
   15265       /* The last non-empty eh_frame section doesn't need padding.  */
   15266       if (i != NULL)
   15267 	i = i->map_tail.s;
   15268       /* Any prior sections must pad the last FDE out to the output
   15269 	 section alignment.  Otherwise we might have zero padding
   15270 	 between sections, which would be seen as a terminator.  */
   15271       for (; i != NULL; i = i->map_tail.s)
   15272 	if (i->size == 4)
   15273 	  /* All but the last zero terminator should have been removed.  */
   15274 	  BFD_FAIL ();
   15275 	else
   15276 	  {
   15277 	    bfd_size_type size
   15278 	      = (i->size + eh_alignment - 1) & -eh_alignment;
   15279 	    if (i->size != size)
   15280 	      {
   15281 		i->size = size;
   15282 		changed = 1;
   15283 		eh_changed = 1;
   15284 	      }
   15285 	  }
   15286       if (eh_changed)
   15287 	elf_link_hash_traverse (elf_hash_table (info),
   15288 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
   15289     }
   15290 
   15291   o = bfd_get_section_by_name (output_bfd, ".sframe");
   15292   if (o != NULL)
   15293     {
   15294       asection *i;
   15295 
   15296       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15297 	{
   15298 	  if (i->size == 0)
   15299 	    continue;
   15300 
   15301 	  abfd = i->owner;
   15302 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15303 	    continue;
   15304 
   15305 	  if (!init_reloc_cookie_for_section (&cookie, info, i, false))
   15306 	    return -1;
   15307 
   15308 	  if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
   15309 	    {
   15310 	      if (_bfd_elf_discard_section_sframe (i,
   15311 						   bfd_elf_reloc_symbol_deleted_p,
   15312 						   &cookie))
   15313 		{
   15314 		  if (i->size != i->rawsize)
   15315 		    changed = 1;
   15316 		}
   15317 	    }
   15318 	  fini_reloc_cookie_for_section (&cookie, i);
   15319 	}
   15320       /* Update the reference to the output .sframe section.  Used to
   15321 	 determine later if PT_GNU_SFRAME segment is to be generated.  */
   15322       if (!_bfd_elf_set_section_sframe (output_bfd, info))
   15323 	return -1;
   15324     }
   15325 
   15326   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   15327     {
   15328       const struct elf_backend_data *bed;
   15329       asection *s;
   15330 
   15331       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15332 	continue;
   15333       s = abfd->sections;
   15334       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   15335 	continue;
   15336 
   15337       bed = get_elf_backend_data (abfd);
   15338 
   15339       if (bed->elf_backend_discard_info != NULL)
   15340 	{
   15341 	  if (!init_reloc_cookie (&cookie, info, abfd, false))
   15342 	    return -1;
   15343 
   15344 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   15345 	    changed = 1;
   15346 
   15347 	  fini_reloc_cookie (&cookie, abfd);
   15348 	}
   15349     }
   15350 
   15351   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   15352     _bfd_elf_end_eh_frame_parsing (info);
   15353 
   15354   if (_bfd_elf_discard_section_eh_frame_hdr (info))
   15355     changed = 1;
   15356 
   15357   return changed;
   15358 }
   15359 
   15360 bool
   15361 _bfd_elf_section_already_linked (bfd *abfd,
   15362 				 asection *sec,
   15363 				 struct bfd_link_info *info)
   15364 {
   15365   flagword flags;
   15366   const char *name, *key;
   15367   struct bfd_section_already_linked *l;
   15368   struct bfd_section_already_linked_hash_entry *already_linked_list;
   15369 
   15370   if (sec->output_section == bfd_abs_section_ptr)
   15371     return false;
   15372 
   15373   flags = sec->flags;
   15374 
   15375   /* Return if it isn't a linkonce section.  A comdat group section
   15376      also has SEC_LINK_ONCE set.  */
   15377   if ((flags & SEC_LINK_ONCE) == 0)
   15378     return false;
   15379 
   15380   /* Don't put group member sections on our list of already linked
   15381      sections.  They are handled as a group via their group section.  */
   15382   if (elf_sec_group (sec) != NULL)
   15383     return false;
   15384 
   15385   /* For a SHT_GROUP section, use the group signature as the key.  */
   15386   name = sec->name;
   15387   if ((flags & SEC_GROUP) != 0
   15388       && elf_next_in_group (sec) != NULL
   15389       && elf_group_name (elf_next_in_group (sec)) != NULL)
   15390     key = elf_group_name (elf_next_in_group (sec));
   15391   else
   15392     {
   15393       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   15394       if (startswith (name, ".gnu.linkonce.")
   15395 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   15396 	key++;
   15397       else
   15398 	/* Must be a user linkonce section that doesn't follow gcc's
   15399 	   naming convention.  In this case we won't be matching
   15400 	   single member groups.  */
   15401 	key = name;
   15402     }
   15403 
   15404   already_linked_list = bfd_section_already_linked_table_lookup (key);
   15405 
   15406   for (l = already_linked_list->entry; l != NULL; l = l->next)
   15407     {
   15408       /* We may have 2 different types of sections on the list: group
   15409 	 sections with a signature of <key> (<key> is some string),
   15410 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   15411 	 Match like sections.  LTO plugin sections are an exception.
   15412 	 They are always named .gnu.linkonce.t.<key> and match either
   15413 	 type of section.  */
   15414       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   15415 	   && ((flags & SEC_GROUP) != 0
   15416 	       || strcmp (name, l->sec->name) == 0))
   15417 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0
   15418 	  || (sec->owner->flags & BFD_PLUGIN) != 0)
   15419 	{
   15420 	  /* The section has already been linked.  See if we should
   15421 	     issue a warning.  */
   15422 	  if (!_bfd_handle_already_linked (sec, l, info))
   15423 	    return false;
   15424 
   15425 	  if (flags & SEC_GROUP)
   15426 	    {
   15427 	      asection *first = elf_next_in_group (sec);
   15428 	      asection *s = first;
   15429 
   15430 	      while (s != NULL)
   15431 		{
   15432 		  s->output_section = bfd_abs_section_ptr;
   15433 		  /* Record which group discards it.  */
   15434 		  s->kept_section = l->sec;
   15435 		  s = elf_next_in_group (s);
   15436 		  /* These lists are circular.  */
   15437 		  if (s == first)
   15438 		    break;
   15439 		}
   15440 	    }
   15441 
   15442 	  return true;
   15443 	}
   15444     }
   15445 
   15446   /* A single member comdat group section may be discarded by a
   15447      linkonce section and vice versa.  */
   15448   if ((flags & SEC_GROUP) != 0)
   15449     {
   15450       asection *first = elf_next_in_group (sec);
   15451 
   15452       if (first != NULL && elf_next_in_group (first) == first)
   15453 	/* Check this single member group against linkonce sections.  */
   15454 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   15455 	  if ((l->sec->flags & SEC_GROUP) == 0
   15456 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   15457 	    {
   15458 	      first->output_section = bfd_abs_section_ptr;
   15459 	      first->kept_section = l->sec;
   15460 	      sec->output_section = bfd_abs_section_ptr;
   15461 	      break;
   15462 	    }
   15463     }
   15464   else
   15465     /* Check this linkonce section against single member groups.  */
   15466     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15467       if (l->sec->flags & SEC_GROUP)
   15468 	{
   15469 	  asection *first = elf_next_in_group (l->sec);
   15470 
   15471 	  if (first != NULL
   15472 	      && elf_next_in_group (first) == first
   15473 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   15474 	    {
   15475 	      sec->output_section = bfd_abs_section_ptr;
   15476 	      sec->kept_section = first;
   15477 	      break;
   15478 	    }
   15479 	}
   15480 
   15481   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   15482      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   15483      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   15484      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   15485      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   15486      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   15487      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   15488      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   15489      The reverse order cannot happen as there is never a bfd with only the
   15490      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   15491      matter as here were are looking only for cross-bfd sections.  */
   15492 
   15493   if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
   15494     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15495       if ((l->sec->flags & SEC_GROUP) == 0
   15496 	  && startswith (l->sec->name, ".gnu.linkonce.t."))
   15497 	{
   15498 	  if (abfd != l->sec->owner)
   15499 	    sec->output_section = bfd_abs_section_ptr;
   15500 	  break;
   15501 	}
   15502 
   15503   /* This is the first section with this name.  Record it.  */
   15504   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   15505     info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
   15506   return sec->output_section == bfd_abs_section_ptr;
   15507 }
   15508 
   15509 bool
   15510 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   15511 {
   15512   return sym->st_shndx == SHN_COMMON;
   15513 }
   15514 
   15515 unsigned int
   15516 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   15517 {
   15518   return SHN_COMMON;
   15519 }
   15520 
   15521 asection *
   15522 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   15523 {
   15524   return bfd_com_section_ptr;
   15525 }
   15526 
   15527 bfd_vma
   15528 _bfd_elf_default_got_elt_size (bfd *abfd,
   15529 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   15530 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   15531 			       bfd *ibfd ATTRIBUTE_UNUSED,
   15532 			       unsigned long symndx ATTRIBUTE_UNUSED)
   15533 {
   15534   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15535   return bed->s->arch_size / 8;
   15536 }
   15537 
   15538 /* Routines to support the creation of dynamic relocs.  */
   15539 
   15540 /* Returns the name of the dynamic reloc section associated with SEC.  */
   15541 
   15542 static const char *
   15543 get_dynamic_reloc_section_name (bfd *       abfd,
   15544 				asection *  sec,
   15545 				bool is_rela)
   15546 {
   15547   char *name;
   15548   const char *old_name = bfd_section_name (sec);
   15549   const char *prefix = is_rela ? ".rela" : ".rel";
   15550 
   15551   if (old_name == NULL)
   15552     return NULL;
   15553 
   15554   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   15555   sprintf (name, "%s%s", prefix, old_name);
   15556 
   15557   return name;
   15558 }
   15559 
   15560 /* Returns the dynamic reloc section associated with SEC.
   15561    If necessary compute the name of the dynamic reloc section based
   15562    on SEC's name (looked up in ABFD's string table) and the setting
   15563    of IS_RELA.  */
   15564 
   15565 asection *
   15566 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
   15567 				    asection *sec,
   15568 				    bool is_rela)
   15569 {
   15570   asection *reloc_sec = elf_section_data (sec)->sreloc;
   15571 
   15572   if (reloc_sec == NULL)
   15573     {
   15574       const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15575 
   15576       if (name != NULL)
   15577 	{
   15578 	  reloc_sec = bfd_get_linker_section (abfd, name);
   15579 
   15580 	  if (reloc_sec != NULL)
   15581 	    elf_section_data (sec)->sreloc = reloc_sec;
   15582 	}
   15583     }
   15584 
   15585   return reloc_sec;
   15586 }
   15587 
   15588 /* Returns the dynamic reloc section associated with SEC.  If the
   15589    section does not exist it is created and attached to the DYNOBJ
   15590    bfd and stored in the SRELOC field of SEC's elf_section_data
   15591    structure.
   15592 
   15593    ALIGNMENT is the alignment for the newly created section and
   15594    IS_RELA defines whether the name should be .rela.<SEC's name>
   15595    or .rel.<SEC's name>.  The section name is looked up in the
   15596    string table associated with ABFD.  */
   15597 
   15598 asection *
   15599 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   15600 				     bfd *dynobj,
   15601 				     unsigned int alignment,
   15602 				     bfd *abfd,
   15603 				     bool is_rela)
   15604 {
   15605   asection * reloc_sec = elf_section_data (sec)->sreloc;
   15606 
   15607   if (reloc_sec == NULL)
   15608     {
   15609       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15610 
   15611       if (name == NULL)
   15612 	return NULL;
   15613 
   15614       reloc_sec = bfd_get_linker_section (dynobj, name);
   15615 
   15616       if (reloc_sec == NULL)
   15617 	{
   15618 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   15619 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   15620 	  if ((sec->flags & SEC_ALLOC) != 0)
   15621 	    flags |= SEC_ALLOC | SEC_LOAD;
   15622 
   15623 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   15624 	  if (reloc_sec != NULL)
   15625 	    {
   15626 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   15627 		 name.  Override as it may be wrong, eg. for a user
   15628 		 section named "auto" we'll get ".relauto" which is
   15629 		 seen to be a .rela section.  */
   15630 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   15631 	      if (!bfd_set_section_alignment (reloc_sec, alignment))
   15632 		reloc_sec = NULL;
   15633 	    }
   15634 	}
   15635 
   15636       elf_section_data (sec)->sreloc = reloc_sec;
   15637     }
   15638 
   15639   return reloc_sec;
   15640 }
   15641 
   15642 /* Copy the ELF symbol type and other attributes for a linker script
   15643    assignment from HSRC to HDEST.  Generally this should be treated as
   15644    if we found a strong non-dynamic definition for HDEST (except that
   15645    ld ignores multiple definition errors).  */
   15646 void
   15647 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   15648 				     struct bfd_link_hash_entry *hdest,
   15649 				     struct bfd_link_hash_entry *hsrc)
   15650 {
   15651   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   15652   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   15653   Elf_Internal_Sym isym;
   15654 
   15655   ehdest->type = ehsrc->type;
   15656   ehdest->target_internal = ehsrc->target_internal;
   15657 
   15658   isym.st_other = ehsrc->other;
   15659   elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
   15660 }
   15661 
   15662 /* Append a RELA relocation REL to section S in BFD.  */
   15663 
   15664 void
   15665 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15666 {
   15667   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15668   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   15669   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   15670   bed->s->swap_reloca_out (abfd, rel, loc);
   15671 }
   15672 
   15673 /* Append a REL relocation REL to section S in BFD.  */
   15674 
   15675 void
   15676 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15677 {
   15678   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15679   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   15680   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   15681   bed->s->swap_reloc_out (abfd, rel, loc);
   15682 }
   15683 
   15684 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
   15685 
   15686 struct bfd_link_hash_entry *
   15687 bfd_elf_define_start_stop (struct bfd_link_info *info,
   15688 			   const char *symbol, asection *sec)
   15689 {
   15690   struct elf_link_hash_entry *h;
   15691 
   15692   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
   15693 			    false, false, true);
   15694   /* NB: Common symbols will be turned into definition later.  */
   15695   if (h != NULL
   15696       && !h->root.ldscript_def
   15697       && (h->root.type == bfd_link_hash_undefined
   15698 	  || h->root.type == bfd_link_hash_undefweak
   15699 	  || ((h->ref_regular || h->def_dynamic)
   15700 	      && !h->def_regular
   15701 	      && h->root.type != bfd_link_hash_common)))
   15702     {
   15703       bool was_dynamic = h->ref_dynamic || h->def_dynamic;
   15704       h->verinfo.verdef = NULL;
   15705       h->root.type = bfd_link_hash_defined;
   15706       h->root.u.def.section = sec;
   15707       h->root.u.def.value = 0;
   15708       h->def_regular = 1;
   15709       h->def_dynamic = 0;
   15710       h->start_stop = 1;
   15711       h->u2.start_stop_section = sec;
   15712       if (symbol[0] == '.')
   15713 	{
   15714 	  /* .startof. and .sizeof. symbols are local.  */
   15715 	  const struct elf_backend_data *bed;
   15716 	  bed = get_elf_backend_data (info->output_bfd);
   15717 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   15718 	}
   15719       else
   15720 	{
   15721 	  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   15722 	    h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
   15723 			| info->start_stop_visibility);
   15724 	  if (was_dynamic)
   15725 	    bfd_elf_link_record_dynamic_symbol (info, h);
   15726 	}
   15727       return &h->root;
   15728     }
   15729   return NULL;
   15730 }
   15731 
   15732 /* Find dynamic relocs for H that apply to read-only sections.  */
   15733 
   15734 asection *
   15735 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
   15736 {
   15737   struct elf_dyn_relocs *p;
   15738 
   15739   for (p = h->dyn_relocs; p != NULL; p = p->next)
   15740     {
   15741       asection *s = p->sec->output_section;
   15742 
   15743       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   15744 	return p->sec;
   15745     }
   15746   return NULL;
   15747 }
   15748 
   15749 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   15750    read-only sections.  */
   15751 
   15752 bool
   15753 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
   15754 {
   15755   asection *sec;
   15756 
   15757   if (h->root.type == bfd_link_hash_indirect)
   15758     return true;
   15759 
   15760   sec = _bfd_elf_readonly_dynrelocs (h);
   15761   if (sec != NULL)
   15762     {
   15763       struct bfd_link_info *info = (struct bfd_link_info *) inf;
   15764 
   15765       info->flags |= DF_TEXTREL;
   15766       /* xgettext:c-format */
   15767       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
   15768 				"in read-only section `%pA'\n"),
   15769 			      sec->owner, h->root.root.string, sec);
   15770 
   15771       if (bfd_link_textrel_check (info))
   15772 	/* xgettext:c-format */
   15773 	info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
   15774 				  "in read-only section `%pA'\n"),
   15775 				sec->owner, h->root.root.string, sec);
   15776 
   15777       /* Not an error, just cut short the traversal.  */
   15778       return false;
   15779     }
   15780   return true;
   15781 }
   15782 
   15783 /* Add dynamic tags.  */
   15784 
   15785 bool
   15786 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
   15787 			   bool need_dynamic_reloc)
   15788 {
   15789   struct elf_link_hash_table *htab = elf_hash_table (info);
   15790 
   15791   if (htab->dynamic_sections_created)
   15792     {
   15793       /* Add some entries to the .dynamic section.  We fill in the
   15794 	 values later, in finish_dynamic_sections, but we must add
   15795 	 the entries now so that we get the correct size for the
   15796 	 .dynamic section.  The DT_DEBUG entry is filled in by the
   15797 	 dynamic linker and used by the debugger.  */
   15798 #define add_dynamic_entry(TAG, VAL) \
   15799   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   15800 
   15801       const struct elf_backend_data *bed
   15802 	= get_elf_backend_data (output_bfd);
   15803 
   15804       if (bfd_link_executable (info))
   15805 	{
   15806 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   15807 	    return false;
   15808 	}
   15809 
   15810       if (htab->dt_pltgot_required || htab->splt->size != 0)
   15811 	{
   15812 	  /* DT_PLTGOT is used by prelink even if there is no PLT
   15813 	     relocation.  */
   15814 	  if (!add_dynamic_entry (DT_PLTGOT, 0))
   15815 	    return false;
   15816 	}
   15817 
   15818       if (htab->dt_jmprel_required || htab->srelplt->size != 0)
   15819 	{
   15820 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
   15821 	      || !add_dynamic_entry (DT_PLTREL,
   15822 				     (bed->rela_plts_and_copies_p
   15823 				      ? DT_RELA : DT_REL))
   15824 	      || !add_dynamic_entry (DT_JMPREL, 0))
   15825 	    return false;
   15826 	}
   15827 
   15828       if (htab->tlsdesc_plt
   15829 	  && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
   15830 	      || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
   15831 	return false;
   15832 
   15833       if (need_dynamic_reloc)
   15834 	{
   15835 	  if (bed->rela_plts_and_copies_p)
   15836 	    {
   15837 	      if (!add_dynamic_entry (DT_RELA, 0)
   15838 		  || !add_dynamic_entry (DT_RELASZ, 0)
   15839 		  || !add_dynamic_entry (DT_RELAENT,
   15840 					 bed->s->sizeof_rela))
   15841 		return false;
   15842 	    }
   15843 	  else
   15844 	    {
   15845 	      if (!add_dynamic_entry (DT_REL, 0)
   15846 		  || !add_dynamic_entry (DT_RELSZ, 0)
   15847 		  || !add_dynamic_entry (DT_RELENT,
   15848 					 bed->s->sizeof_rel))
   15849 		return false;
   15850 	    }
   15851 
   15852 	  /* If any dynamic relocs apply to a read-only section,
   15853 	     then we need a DT_TEXTREL entry.  */
   15854 	  if ((info->flags & DF_TEXTREL) == 0)
   15855 	    elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
   15856 				    info);
   15857 
   15858 	  if ((info->flags & DF_TEXTREL) != 0)
   15859 	    {
   15860 	      if (htab->ifunc_resolvers)
   15861 		info->callbacks->einfo
   15862 		  (_("%P: warning: GNU indirect functions with DT_TEXTREL "
   15863 		     "may result in a segfault at runtime; recompile with %s\n"),
   15864 		   bfd_link_dll (info) ? "-fPIC" : "-fPIE");
   15865 
   15866 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   15867 		return false;
   15868 	    }
   15869 	}
   15870     }
   15871 #undef add_dynamic_entry
   15872 
   15873   return true;
   15874 }
   15875