Home | History | Annotate | Line # | Download | only in bfd
elflink.c revision 1.1.1.12
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2024 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 asection *
     53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
     54 			     unsigned long r_symndx,
     55 			     bool discard)
     56 {
     57   if (r_symndx >= cookie->locsymcount
     58       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
     59     {
     60       struct elf_link_hash_entry *h;
     61 
     62       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
     63 
     64       while (h->root.type == bfd_link_hash_indirect
     65 	     || h->root.type == bfd_link_hash_warning)
     66 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
     67 
     68       if ((h->root.type == bfd_link_hash_defined
     69 	   || h->root.type == bfd_link_hash_defweak)
     70 	   && discarded_section (h->root.u.def.section))
     71 	return h->root.u.def.section;
     72       else
     73 	return NULL;
     74     }
     75   else
     76     {
     77       /* It's not a relocation against a global symbol,
     78 	 but it could be a relocation against a local
     79 	 symbol for a discarded section.  */
     80       asection *isec;
     81       Elf_Internal_Sym *isym;
     82 
     83       /* Need to: get the symbol; get the section.  */
     84       isym = &cookie->locsyms[r_symndx];
     85       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
     86       if (isec != NULL
     87 	  && discard ? discarded_section (isec) : 1)
     88 	return isec;
     89      }
     90   return NULL;
     91 }
     92 
     93 /* Define a symbol in a dynamic linkage section.  */
     94 
     95 struct elf_link_hash_entry *
     96 _bfd_elf_define_linkage_sym (bfd *abfd,
     97 			     struct bfd_link_info *info,
     98 			     asection *sec,
     99 			     const char *name)
    100 {
    101   struct elf_link_hash_entry *h;
    102   struct bfd_link_hash_entry *bh;
    103   const struct elf_backend_data *bed;
    104 
    105   h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
    106   if (h != NULL)
    107     {
    108       /* Zap symbol defined in an as-needed lib that wasn't linked.
    109 	 This is a symptom of a larger problem:  Absolute symbols
    110 	 defined in shared libraries can't be overridden, because we
    111 	 lose the link to the bfd which is via the symbol section.  */
    112       h->root.type = bfd_link_hash_new;
    113       bh = &h->root;
    114     }
    115   else
    116     bh = NULL;
    117 
    118   bed = get_elf_backend_data (abfd);
    119   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    120 					 sec, 0, NULL, false, bed->collect,
    121 					 &bh))
    122     return NULL;
    123   h = (struct elf_link_hash_entry *) bh;
    124   BFD_ASSERT (h != NULL);
    125   h->def_regular = 1;
    126   h->non_elf = 0;
    127   h->root.linker_def = 1;
    128   h->type = STT_OBJECT;
    129   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    130     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    131 
    132   (*bed->elf_backend_hide_symbol) (info, h, true);
    133   return h;
    134 }
    135 
    136 bool
    137 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    138 {
    139   flagword flags;
    140   asection *s;
    141   struct elf_link_hash_entry *h;
    142   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    143   struct elf_link_hash_table *htab = elf_hash_table (info);
    144 
    145   /* This function may be called more than once.  */
    146   if (htab->sgot != NULL)
    147     return true;
    148 
    149   flags = bed->dynamic_sec_flags;
    150 
    151   s = bfd_make_section_anyway_with_flags (abfd,
    152 					  (bed->rela_plts_and_copies_p
    153 					   ? ".rela.got" : ".rel.got"),
    154 					  (bed->dynamic_sec_flags
    155 					   | SEC_READONLY));
    156   if (s == NULL
    157       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    158     return false;
    159   htab->srelgot = s;
    160 
    161   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    162   if (s == NULL
    163       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    164     return false;
    165   htab->sgot = s;
    166 
    167   if (bed->want_got_plt)
    168     {
    169       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    170       if (s == NULL
    171 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    172 	return false;
    173       htab->sgotplt = s;
    174     }
    175 
    176   /* The first bit of the global offset table is the header.  */
    177   s->size += bed->got_header_size;
    178 
    179   if (bed->want_got_sym)
    180     {
    181       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    182 	 (or .got.plt) section.  We don't do this in the linker script
    183 	 because we don't want to define the symbol if we are not creating
    184 	 a global offset table.  */
    185       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    186 				       "_GLOBAL_OFFSET_TABLE_");
    187       elf_hash_table (info)->hgot = h;
    188       if (h == NULL)
    189 	return false;
    190     }
    191 
    192   return true;
    193 }
    194 
    195 /* Create a strtab to hold the dynamic symbol names.  */
    197 static bool
    198 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    199 {
    200   struct elf_link_hash_table *hash_table;
    201 
    202   hash_table = elf_hash_table (info);
    203   if (hash_table->dynobj == NULL)
    204     {
    205       /* We may not set dynobj, an input file holding linker created
    206 	 dynamic sections to abfd, which may be a dynamic object with
    207 	 its own dynamic sections.  We need to find a normal input file
    208 	 to hold linker created sections if possible.  */
    209       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
    210 	{
    211 	  bfd *ibfd;
    212 	  asection *s;
    213 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
    214 	    if ((ibfd->flags
    215 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
    216 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
    217 		&& elf_object_id (ibfd) == elf_hash_table_id (hash_table)
    218 		&& !((s = ibfd->sections) != NULL
    219 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
    220 	      {
    221 		abfd = ibfd;
    222 		break;
    223 	      }
    224 	}
    225       hash_table->dynobj = abfd;
    226     }
    227 
    228   if (hash_table->dynstr == NULL)
    229     {
    230       hash_table->dynstr = _bfd_elf_strtab_init ();
    231       if (hash_table->dynstr == NULL)
    232 	return false;
    233     }
    234   return true;
    235 }
    236 
    237 /* Create some sections which will be filled in with dynamic linking
    238    information.  ABFD is an input file which requires dynamic sections
    239    to be created.  The dynamic sections take up virtual memory space
    240    when the final executable is run, so we need to create them before
    241    addresses are assigned to the output sections.  We work out the
    242    actual contents and size of these sections later.  */
    243 
    244 bool
    245 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    246 {
    247   flagword flags;
    248   asection *s;
    249   const struct elf_backend_data *bed;
    250   struct elf_link_hash_entry *h;
    251 
    252   if (! is_elf_hash_table (info->hash))
    253     return false;
    254 
    255   if (elf_hash_table (info)->dynamic_sections_created)
    256     return true;
    257 
    258   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    259     return false;
    260 
    261   abfd = elf_hash_table (info)->dynobj;
    262   bed = get_elf_backend_data (abfd);
    263 
    264   flags = bed->dynamic_sec_flags;
    265 
    266   /* A dynamically linked executable has a .interp section, but a
    267      shared library does not.  */
    268   if (bfd_link_executable (info) && !info->nointerp)
    269     {
    270       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    271 					      flags | SEC_READONLY);
    272       if (s == NULL)
    273 	return false;
    274     }
    275 
    276   /* Create sections to hold version informations.  These are removed
    277      if they are not needed.  */
    278   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    279 					  flags | SEC_READONLY);
    280   if (s == NULL
    281       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    282     return false;
    283 
    284   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    285 					  flags | SEC_READONLY);
    286   if (s == NULL
    287       || !bfd_set_section_alignment (s, 1))
    288     return false;
    289 
    290   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    291 					  flags | SEC_READONLY);
    292   if (s == NULL
    293       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    294     return false;
    295 
    296   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    297 					  flags | SEC_READONLY);
    298   if (s == NULL
    299       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    300     return false;
    301   elf_hash_table (info)->dynsym = s;
    302 
    303   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    304 					  flags | SEC_READONLY);
    305   if (s == NULL)
    306     return false;
    307 
    308   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    309   if (s == NULL
    310       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    311     return false;
    312 
    313   /* The special symbol _DYNAMIC is always set to the start of the
    314      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    315      only want to define it if we are, in fact, creating a .dynamic
    316      section.  We don't want to define it if there is no .dynamic
    317      section, since on some ELF platforms the start up code examines it
    318      to decide how to initialize the process.  */
    319   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    320   elf_hash_table (info)->hdynamic = h;
    321   if (h == NULL)
    322     return false;
    323 
    324   if (info->emit_hash)
    325     {
    326       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    327 					      flags | SEC_READONLY);
    328       if (s == NULL
    329 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    330 	return false;
    331       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    332     }
    333 
    334   if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
    335     {
    336       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    337 					      flags | SEC_READONLY);
    338       if (s == NULL
    339 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    340 	return false;
    341       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    342 	 4 32-bit words followed by variable count of 64-bit words, then
    343 	 variable count of 32-bit words.  */
    344       if (bed->s->arch_size == 64)
    345 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    346       else
    347 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    348     }
    349 
    350   if (info->enable_dt_relr)
    351     {
    352       s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
    353 					      (bed->dynamic_sec_flags
    354 					       | SEC_READONLY));
    355       if (s == NULL
    356 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    357 	return false;
    358       elf_hash_table (info)->srelrdyn = s;
    359     }
    360 
    361   /* Let the backend create the rest of the sections.  This lets the
    362      backend set the right flags.  The backend will normally create
    363      the .got and .plt sections.  */
    364   if (bed->elf_backend_create_dynamic_sections == NULL
    365       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    366     return false;
    367 
    368   elf_hash_table (info)->dynamic_sections_created = true;
    369 
    370   return true;
    371 }
    372 
    373 /* Create dynamic sections when linking against a dynamic object.  */
    374 
    375 bool
    376 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    377 {
    378   flagword flags, pltflags;
    379   struct elf_link_hash_entry *h;
    380   asection *s;
    381   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    382   struct elf_link_hash_table *htab = elf_hash_table (info);
    383 
    384   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    385      .rel[a].bss sections.  */
    386   flags = bed->dynamic_sec_flags;
    387 
    388   pltflags = flags;
    389   if (bed->plt_not_loaded)
    390     /* We do not clear SEC_ALLOC here because we still want the OS to
    391        allocate space for the section; it's just that there's nothing
    392        to read in from the object file.  */
    393     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    394   else
    395     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    396   if (bed->plt_readonly)
    397     pltflags |= SEC_READONLY;
    398 
    399   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    400   if (s == NULL
    401       || !bfd_set_section_alignment (s, bed->plt_alignment))
    402     return false;
    403   htab->splt = s;
    404 
    405   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    406      .plt section.  */
    407   if (bed->want_plt_sym)
    408     {
    409       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    410 				       "_PROCEDURE_LINKAGE_TABLE_");
    411       elf_hash_table (info)->hplt = h;
    412       if (h == NULL)
    413 	return false;
    414     }
    415 
    416   s = bfd_make_section_anyway_with_flags (abfd,
    417 					  (bed->rela_plts_and_copies_p
    418 					   ? ".rela.plt" : ".rel.plt"),
    419 					  flags | SEC_READONLY);
    420   if (s == NULL
    421       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    422     return false;
    423   htab->srelplt = s;
    424 
    425   if (! _bfd_elf_create_got_section (abfd, info))
    426     return false;
    427 
    428   if (bed->want_dynbss)
    429     {
    430       /* The .dynbss section is a place to put symbols which are defined
    431 	 by dynamic objects, are referenced by regular objects, and are
    432 	 not functions.  We must allocate space for them in the process
    433 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    434 	 initialize them at run time.  The linker script puts the .dynbss
    435 	 section into the .bss section of the final image.  */
    436       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    437 					      SEC_ALLOC | SEC_LINKER_CREATED);
    438       if (s == NULL)
    439 	return false;
    440       htab->sdynbss = s;
    441 
    442       if (bed->want_dynrelro)
    443 	{
    444 	  /* Similarly, but for symbols that were originally in read-only
    445 	     sections.  This section doesn't really need to have contents,
    446 	     but make it like other .data.rel.ro sections.  */
    447 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
    448 						  flags);
    449 	  if (s == NULL)
    450 	    return false;
    451 	  htab->sdynrelro = s;
    452 	}
    453 
    454       /* The .rel[a].bss section holds copy relocs.  This section is not
    455 	 normally needed.  We need to create it here, though, so that the
    456 	 linker will map it to an output section.  We can't just create it
    457 	 only if we need it, because we will not know whether we need it
    458 	 until we have seen all the input files, and the first time the
    459 	 main linker code calls BFD after examining all the input files
    460 	 (size_dynamic_sections) the input sections have already been
    461 	 mapped to the output sections.  If the section turns out not to
    462 	 be needed, we can discard it later.  We will never need this
    463 	 section when generating a shared object, since they do not use
    464 	 copy relocs.  */
    465       if (bfd_link_executable (info))
    466 	{
    467 	  s = bfd_make_section_anyway_with_flags (abfd,
    468 						  (bed->rela_plts_and_copies_p
    469 						   ? ".rela.bss" : ".rel.bss"),
    470 						  flags | SEC_READONLY);
    471 	  if (s == NULL
    472 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
    473 	    return false;
    474 	  htab->srelbss = s;
    475 
    476 	  if (bed->want_dynrelro)
    477 	    {
    478 	      s = (bfd_make_section_anyway_with_flags
    479 		   (abfd, (bed->rela_plts_and_copies_p
    480 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
    481 		    flags | SEC_READONLY));
    482 	      if (s == NULL
    483 		  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    484 		return false;
    485 	      htab->sreldynrelro = s;
    486 	    }
    487 	}
    488     }
    489 
    490   return true;
    491 }
    492 
    493 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    495    read the input files, since we need to have a list of all of them
    496    before we can determine the final sizes of the output sections.
    497    Note that we may actually call this function even though we are not
    498    going to output any dynamic symbols; in some cases we know that a
    499    symbol should be in the dynamic symbol table, but only if there is
    500    one.  */
    501 
    502 bool
    503 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    504 				    struct elf_link_hash_entry *h)
    505 {
    506   if (h->dynindx == -1)
    507     {
    508       struct elf_strtab_hash *dynstr;
    509       char *p;
    510       const char *name;
    511       size_t indx;
    512 
    513       if (h->root.type == bfd_link_hash_defined
    514 	  || h->root.type == bfd_link_hash_defweak)
    515 	{
    516 	  /* An IR symbol should not be made dynamic.  */
    517 	  if (h->root.u.def.section != NULL
    518 	      && h->root.u.def.section->owner != NULL
    519 	      && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
    520 	    return true;
    521 	}
    522 
    523       /* XXX: The ABI draft says the linker must turn hidden and
    524 	 internal symbols into STB_LOCAL symbols when producing the
    525 	 DSO. However, if ld.so honors st_other in the dynamic table,
    526 	 this would not be necessary.  */
    527       switch (ELF_ST_VISIBILITY (h->other))
    528 	{
    529 	case STV_INTERNAL:
    530 	case STV_HIDDEN:
    531 	  if (h->root.type != bfd_link_hash_undefined
    532 	      && h->root.type != bfd_link_hash_undefweak)
    533 	    {
    534 	      h->forced_local = 1;
    535 	      if (!elf_hash_table (info)->is_relocatable_executable
    536 		  || ((h->root.type == bfd_link_hash_defined
    537 		       || h->root.type == bfd_link_hash_defweak)
    538 		      && h->root.u.def.section->owner != NULL
    539 		      && h->root.u.def.section->owner->no_export)
    540 		  || (h->root.type == bfd_link_hash_common
    541 		      && h->root.u.c.p->section->owner != NULL
    542 		      && h->root.u.c.p->section->owner->no_export))
    543 		return true;
    544 	    }
    545 
    546 	default:
    547 	  break;
    548 	}
    549 
    550       h->dynindx = elf_hash_table (info)->dynsymcount;
    551       ++elf_hash_table (info)->dynsymcount;
    552 
    553       dynstr = elf_hash_table (info)->dynstr;
    554       if (dynstr == NULL)
    555 	{
    556 	  /* Create a strtab to hold the dynamic symbol names.  */
    557 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    558 	  if (dynstr == NULL)
    559 	    return false;
    560 	}
    561 
    562       /* We don't put any version information in the dynamic string
    563 	 table.  */
    564       name = h->root.root.string;
    565       p = strchr (name, ELF_VER_CHR);
    566       if (p != NULL)
    567 	/* We know that the p points into writable memory.  In fact,
    568 	   there are only a few symbols that have read-only names, being
    569 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
    570 	   by the backends.  Most symbols will have names pointing into
    571 	   an ELF string table read from a file, or to objalloc memory.  */
    572 	*p = 0;
    573 
    574       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    575 
    576       if (p != NULL)
    577 	*p = ELF_VER_CHR;
    578 
    579       if (indx == (size_t) -1)
    580 	return false;
    581       h->dynstr_index = indx;
    582     }
    583 
    584   return true;
    585 }
    586 
    587 /* Mark a symbol dynamic.  */
    589 
    590 static void
    591 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    592 				  struct elf_link_hash_entry *h,
    593 				  Elf_Internal_Sym *sym)
    594 {
    595   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    596 
    597   /* It may be called more than once on the same H.  */
    598   if(h->dynamic || bfd_link_relocatable (info))
    599     return;
    600 
    601   if ((info->dynamic_data
    602        && (h->type == STT_OBJECT
    603 	   || h->type == STT_COMMON
    604 	   || (sym != NULL
    605 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
    606 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
    607       || (d != NULL
    608 	  && h->non_elf
    609 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    610     {
    611       h->dynamic = 1;
    612       /* NB: If a symbol is made dynamic by --dynamic-list, it has
    613 	 non-IR reference.  */
    614       h->root.non_ir_ref_dynamic = 1;
    615     }
    616 }
    617 
    618 /* Record an assignment to a symbol made by a linker script.  We need
    619    this in case some dynamic object refers to this symbol.  */
    620 
    621 bool
    622 bfd_elf_record_link_assignment (bfd *output_bfd,
    623 				struct bfd_link_info *info,
    624 				const char *name,
    625 				bool provide,
    626 				bool hidden)
    627 {
    628   struct elf_link_hash_entry *h, *hv;
    629   struct elf_link_hash_table *htab;
    630   const struct elf_backend_data *bed;
    631 
    632   if (!is_elf_hash_table (info->hash))
    633     return true;
    634 
    635   htab = elf_hash_table (info);
    636   h = elf_link_hash_lookup (htab, name, !provide, true, false);
    637   if (h == NULL)
    638     return provide;
    639 
    640   if (h->root.type == bfd_link_hash_warning)
    641     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    642 
    643   if (h->versioned == unknown)
    644     {
    645       /* Set versioned if symbol version is unknown.  */
    646       char *version = strrchr (name, ELF_VER_CHR);
    647       if (version)
    648 	{
    649 	  if (version > name && version[-1] != ELF_VER_CHR)
    650 	    h->versioned = versioned_hidden;
    651 	  else
    652 	    h->versioned = versioned;
    653 	}
    654     }
    655 
    656   /* Symbols defined in a linker script but not referenced anywhere
    657      else will have non_elf set.  */
    658   if (h->non_elf)
    659     {
    660       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    661       h->non_elf = 0;
    662     }
    663 
    664   switch (h->root.type)
    665     {
    666     case bfd_link_hash_defined:
    667     case bfd_link_hash_defweak:
    668     case bfd_link_hash_common:
    669       break;
    670     case bfd_link_hash_undefweak:
    671     case bfd_link_hash_undefined:
    672       /* Since we're defining the symbol, don't let it seem to have not
    673 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    674 	 may depend on this.  */
    675       h->root.type = bfd_link_hash_new;
    676       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    677 	bfd_link_repair_undef_list (&htab->root);
    678       break;
    679     case bfd_link_hash_new:
    680       break;
    681     case bfd_link_hash_indirect:
    682       /* We had a versioned symbol in a dynamic library.  We make the
    683 	 the versioned symbol point to this one.  */
    684       bed = get_elf_backend_data (output_bfd);
    685       hv = h;
    686       while (hv->root.type == bfd_link_hash_indirect
    687 	     || hv->root.type == bfd_link_hash_warning)
    688 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    689       /* We don't need to update h->root.u since linker will set them
    690 	 later.  */
    691       h->root.type = bfd_link_hash_undefined;
    692       hv->root.type = bfd_link_hash_indirect;
    693       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    694       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    695       break;
    696     default:
    697       BFD_FAIL ();
    698       return false;
    699     }
    700 
    701   /* If this symbol is being provided by the linker script, and it is
    702      currently defined by a dynamic object, but not by a regular
    703      object, then mark it as undefined so that the generic linker will
    704      force the correct value.  */
    705   if (provide
    706       && h->def_dynamic
    707       && !h->def_regular)
    708     h->root.type = bfd_link_hash_undefined;
    709 
    710   /* If this symbol is currently defined by a dynamic object, but not
    711      by a regular object, then clear out any version information because
    712      the symbol will not be associated with the dynamic object any
    713      more.  */
    714   if (h->def_dynamic && !h->def_regular)
    715     h->verinfo.verdef = NULL;
    716 
    717   /* Make sure this symbol is not garbage collected.  */
    718   h->mark = 1;
    719 
    720   h->def_regular = 1;
    721 
    722   if (hidden)
    723     {
    724       bed = get_elf_backend_data (output_bfd);
    725       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    726 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    727       (*bed->elf_backend_hide_symbol) (info, h, true);
    728     }
    729 
    730   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    731      and executables.  */
    732   if (!bfd_link_relocatable (info)
    733       && h->dynindx != -1
    734       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    735 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    736     h->forced_local = 1;
    737 
    738   if ((h->def_dynamic
    739        || h->ref_dynamic
    740        || bfd_link_dll (info)
    741        || elf_hash_table (info)->is_relocatable_executable)
    742       && !h->forced_local
    743       && h->dynindx == -1)
    744     {
    745       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    746 	return false;
    747 
    748       /* If this is a weak defined symbol, and we know a corresponding
    749 	 real symbol from the same dynamic object, make sure the real
    750 	 symbol is also made into a dynamic symbol.  */
    751       if (h->is_weakalias)
    752 	{
    753 	  struct elf_link_hash_entry *def = weakdef (h);
    754 
    755 	  if (def->dynindx == -1
    756 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
    757 	    return false;
    758 	}
    759     }
    760 
    761   return true;
    762 }
    763 
    764 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    765    success, and 2 on a failure caused by attempting to record a symbol
    766    in a discarded section, eg. a discarded link-once section symbol.  */
    767 
    768 int
    769 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    770 					  bfd *input_bfd,
    771 					  long input_indx)
    772 {
    773   size_t amt;
    774   struct elf_link_local_dynamic_entry *entry;
    775   struct elf_link_hash_table *eht;
    776   struct elf_strtab_hash *dynstr;
    777   size_t dynstr_index;
    778   char *name;
    779   Elf_External_Sym_Shndx eshndx;
    780   char esym[sizeof (Elf64_External_Sym)];
    781 
    782   if (! is_elf_hash_table (info->hash))
    783     return 0;
    784 
    785   /* See if the entry exists already.  */
    786   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    787     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    788       return 1;
    789 
    790   amt = sizeof (*entry);
    791   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    792   if (entry == NULL)
    793     return 0;
    794 
    795   /* Go find the symbol, so that we can find it's name.  */
    796   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    797 			     1, input_indx, &entry->isym, esym, &eshndx))
    798     {
    799       bfd_release (input_bfd, entry);
    800       return 0;
    801     }
    802 
    803   if (entry->isym.st_shndx != SHN_UNDEF
    804       && entry->isym.st_shndx < SHN_LORESERVE)
    805     {
    806       asection *s;
    807 
    808       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    809       if (s == NULL || bfd_is_abs_section (s->output_section))
    810 	{
    811 	  /* We can still bfd_release here as nothing has done another
    812 	     bfd_alloc.  We can't do this later in this function.  */
    813 	  bfd_release (input_bfd, entry);
    814 	  return 2;
    815 	}
    816     }
    817 
    818   name = (bfd_elf_string_from_elf_section
    819 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    820 	   entry->isym.st_name));
    821 
    822   dynstr = elf_hash_table (info)->dynstr;
    823   if (dynstr == NULL)
    824     {
    825       /* Create a strtab to hold the dynamic symbol names.  */
    826       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    827       if (dynstr == NULL)
    828 	return 0;
    829     }
    830 
    831   dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
    832   if (dynstr_index == (size_t) -1)
    833     return 0;
    834   entry->isym.st_name = dynstr_index;
    835 
    836   eht = elf_hash_table (info);
    837 
    838   entry->next = eht->dynlocal;
    839   eht->dynlocal = entry;
    840   entry->input_bfd = input_bfd;
    841   entry->input_indx = input_indx;
    842   eht->dynsymcount++;
    843 
    844   /* Whatever binding the symbol had before, it's now local.  */
    845   entry->isym.st_info
    846     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    847 
    848   /* The dynindx will be set at the end of size_dynamic_sections.  */
    849 
    850   return 1;
    851 }
    852 
    853 /* Return the dynindex of a local dynamic symbol.  */
    854 
    855 long
    856 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    857 				    bfd *input_bfd,
    858 				    long input_indx)
    859 {
    860   struct elf_link_local_dynamic_entry *e;
    861 
    862   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    863     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    864       return e->dynindx;
    865   return -1;
    866 }
    867 
    868 /* This function is used to renumber the dynamic symbols, if some of
    869    them are removed because they are marked as local.  This is called
    870    via elf_link_hash_traverse.  */
    871 
    872 static bool
    873 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    874 				      void *data)
    875 {
    876   size_t *count = (size_t *) data;
    877 
    878   if (h->forced_local)
    879     return true;
    880 
    881   if (h->dynindx != -1)
    882     h->dynindx = ++(*count);
    883 
    884   return true;
    885 }
    886 
    887 
    888 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    889    STB_LOCAL binding.  */
    890 
    891 static bool
    892 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    893 					    void *data)
    894 {
    895   size_t *count = (size_t *) data;
    896 
    897   if (!h->forced_local)
    898     return true;
    899 
    900   if (h->dynindx != -1)
    901     h->dynindx = ++(*count);
    902 
    903   return true;
    904 }
    905 
    906 /* Return true if the dynamic symbol for a given section should be
    907    omitted when creating a shared library.  */
    908 bool
    909 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
    910 				      struct bfd_link_info *info,
    911 				      asection *p)
    912 {
    913   struct elf_link_hash_table *htab;
    914   asection *ip;
    915 
    916   switch (elf_section_data (p)->this_hdr.sh_type)
    917     {
    918     case SHT_PROGBITS:
    919     case SHT_NOBITS:
    920       /* If sh_type is yet undecided, assume it could be
    921 	 SHT_PROGBITS/SHT_NOBITS.  */
    922     case SHT_NULL:
    923       htab = elf_hash_table (info);
    924       if (htab->text_index_section != NULL)
    925 	return p != htab->text_index_section && p != htab->data_index_section;
    926 
    927       return (htab->dynobj != NULL
    928 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
    929 	      && ip->output_section == p);
    930 
    931       /* There shouldn't be section relative relocations
    932 	 against any other section.  */
    933     default:
    934       return true;
    935     }
    936 }
    937 
    938 bool
    939 _bfd_elf_omit_section_dynsym_all
    940     (bfd *output_bfd ATTRIBUTE_UNUSED,
    941      struct bfd_link_info *info ATTRIBUTE_UNUSED,
    942      asection *p ATTRIBUTE_UNUSED)
    943 {
    944   return true;
    945 }
    946 
    947 /* Assign dynsym indices.  In a shared library we generate a section
    948    symbol for each output section, which come first.  Next come symbols
    949    which have been forced to local binding.  Then all of the back-end
    950    allocated local dynamic syms, followed by the rest of the global
    951    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
    952    (This prevents the early call before elf_backend_init_index_section
    953    and strip_excluded_output_sections setting dynindx for sections
    954    that are stripped.)  */
    955 
    956 static unsigned long
    957 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
    958 				struct bfd_link_info *info,
    959 				unsigned long *section_sym_count)
    960 {
    961   unsigned long dynsymcount = 0;
    962   bool do_sec = section_sym_count != NULL;
    963 
    964   if (bfd_link_pic (info)
    965       || elf_hash_table (info)->is_relocatable_executable)
    966     {
    967       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
    968       asection *p;
    969       for (p = output_bfd->sections; p ; p = p->next)
    970 	if ((p->flags & SEC_EXCLUDE) == 0
    971 	    && (p->flags & SEC_ALLOC) != 0
    972 	    && elf_hash_table (info)->dynamic_relocs
    973 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
    974 	  {
    975 	    ++dynsymcount;
    976 	    if (do_sec)
    977 	      elf_section_data (p)->dynindx = dynsymcount;
    978 	  }
    979 	else if (do_sec)
    980 	  elf_section_data (p)->dynindx = 0;
    981     }
    982   if (do_sec)
    983     *section_sym_count = dynsymcount;
    984 
    985   elf_link_hash_traverse (elf_hash_table (info),
    986 			  elf_link_renumber_local_hash_table_dynsyms,
    987 			  &dynsymcount);
    988 
    989   if (elf_hash_table (info)->dynlocal)
    990     {
    991       struct elf_link_local_dynamic_entry *p;
    992       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
    993 	p->dynindx = ++dynsymcount;
    994     }
    995   elf_hash_table (info)->local_dynsymcount = dynsymcount;
    996 
    997   elf_link_hash_traverse (elf_hash_table (info),
    998 			  elf_link_renumber_hash_table_dynsyms,
    999 			  &dynsymcount);
   1000 
   1001   /* There is an unused NULL entry at the head of the table which we
   1002      must account for in our count even if the table is empty since it
   1003      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
   1004      .dynamic section.  */
   1005   dynsymcount++;
   1006 
   1007   elf_hash_table (info)->dynsymcount = dynsymcount;
   1008   return dynsymcount;
   1009 }
   1010 
   1011 /* Merge st_other field.  */
   1012 
   1013 static void
   1014 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
   1015 		    unsigned int st_other, asection *sec,
   1016 		    bool definition, bool dynamic)
   1017 {
   1018   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1019 
   1020   /* If st_other has a processor-specific meaning, specific
   1021      code might be needed here.  */
   1022   if (bed->elf_backend_merge_symbol_attribute)
   1023     (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
   1024 						dynamic);
   1025 
   1026   if (!dynamic)
   1027     {
   1028       unsigned symvis = ELF_ST_VISIBILITY (st_other);
   1029       unsigned hvis = ELF_ST_VISIBILITY (h->other);
   1030 
   1031       /* Keep the most constraining visibility.  Leave the remainder
   1032 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
   1033       if (symvis - 1 < hvis - 1)
   1034 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
   1035     }
   1036   else if (definition
   1037 	   && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
   1038 	   && (sec->flags & SEC_READONLY) == 0)
   1039     h->protected_def = 1;
   1040 }
   1041 
   1042 /* This function is called when we want to merge a new symbol with an
   1043    existing symbol.  It handles the various cases which arise when we
   1044    find a definition in a dynamic object, or when there is already a
   1045    definition in a dynamic object.  The new symbol is described by
   1046    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
   1047    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
   1048    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
   1049    of an old common symbol.  We set OVERRIDE if the old symbol is
   1050    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
   1051    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
   1052    to change.  By OK to change, we mean that we shouldn't warn if the
   1053    type or size does change.  */
   1054 
   1055 static bool
   1056 _bfd_elf_merge_symbol (bfd *abfd,
   1057 		       struct bfd_link_info *info,
   1058 		       const char *name,
   1059 		       Elf_Internal_Sym *sym,
   1060 		       asection **psec,
   1061 		       bfd_vma *pvalue,
   1062 		       struct elf_link_hash_entry **sym_hash,
   1063 		       bfd **poldbfd,
   1064 		       bool *pold_weak,
   1065 		       unsigned int *pold_alignment,
   1066 		       bool *skip,
   1067 		       bfd **override,
   1068 		       bool *type_change_ok,
   1069 		       bool *size_change_ok,
   1070 		       bool *matched)
   1071 {
   1072   asection *sec, *oldsec;
   1073   struct elf_link_hash_entry *h;
   1074   struct elf_link_hash_entry *hi;
   1075   struct elf_link_hash_entry *flip;
   1076   int bind;
   1077   bfd *oldbfd;
   1078   bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   1079   bool newweak, oldweak, newfunc, oldfunc;
   1080   const struct elf_backend_data *bed;
   1081   char *new_version;
   1082   bool default_sym = *matched;
   1083   struct elf_link_hash_table *htab;
   1084 
   1085   *skip = false;
   1086   *override = NULL;
   1087 
   1088   sec = *psec;
   1089   bind = ELF_ST_BIND (sym->st_info);
   1090 
   1091   if (! bfd_is_und_section (sec))
   1092     h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
   1093   else
   1094     h = ((struct elf_link_hash_entry *)
   1095 	 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
   1096   if (h == NULL)
   1097     return false;
   1098   *sym_hash = h;
   1099 
   1100   bed = get_elf_backend_data (abfd);
   1101 
   1102   /* NEW_VERSION is the symbol version of the new symbol.  */
   1103   if (h->versioned != unversioned)
   1104     {
   1105       /* Symbol version is unknown or versioned.  */
   1106       new_version = strrchr (name, ELF_VER_CHR);
   1107       if (new_version)
   1108 	{
   1109 	  if (h->versioned == unknown)
   1110 	    {
   1111 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1112 		h->versioned = versioned_hidden;
   1113 	      else
   1114 		h->versioned = versioned;
   1115 	    }
   1116 	  new_version += 1;
   1117 	  if (new_version[0] == '\0')
   1118 	    new_version = NULL;
   1119 	}
   1120       else
   1121 	h->versioned = unversioned;
   1122     }
   1123   else
   1124     new_version = NULL;
   1125 
   1126   /* For merging, we only care about real symbols.  But we need to make
   1127      sure that indirect symbol dynamic flags are updated.  */
   1128   hi = h;
   1129   while (h->root.type == bfd_link_hash_indirect
   1130 	 || h->root.type == bfd_link_hash_warning)
   1131     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1132 
   1133   if (!*matched)
   1134     {
   1135       if (hi == h || h->root.type == bfd_link_hash_new)
   1136 	*matched = true;
   1137       else
   1138 	{
   1139 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1140 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1141 	     true if the new symbol is only visible to the symbol with
   1142 	     the same symbol version.  */
   1143 	  bool old_hidden = h->versioned == versioned_hidden;
   1144 	  bool new_hidden = hi->versioned == versioned_hidden;
   1145 	  if (!old_hidden && !new_hidden)
   1146 	    /* The new symbol matches the existing symbol if both
   1147 	       aren't hidden.  */
   1148 	    *matched = true;
   1149 	  else
   1150 	    {
   1151 	      /* OLD_VERSION is the symbol version of the existing
   1152 		 symbol. */
   1153 	      char *old_version;
   1154 
   1155 	      if (h->versioned >= versioned)
   1156 		old_version = strrchr (h->root.root.string,
   1157 				       ELF_VER_CHR) + 1;
   1158 	      else
   1159 		 old_version = NULL;
   1160 
   1161 	      /* The new symbol matches the existing symbol if they
   1162 		 have the same symbol version.  */
   1163 	      *matched = (old_version == new_version
   1164 			  || (old_version != NULL
   1165 			      && new_version != NULL
   1166 			      && strcmp (old_version, new_version) == 0));
   1167 	    }
   1168 	}
   1169     }
   1170 
   1171   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1172      existing symbol.  */
   1173 
   1174   oldbfd = NULL;
   1175   oldsec = NULL;
   1176   switch (h->root.type)
   1177     {
   1178     default:
   1179       break;
   1180 
   1181     case bfd_link_hash_undefined:
   1182     case bfd_link_hash_undefweak:
   1183       oldbfd = h->root.u.undef.abfd;
   1184       break;
   1185 
   1186     case bfd_link_hash_defined:
   1187     case bfd_link_hash_defweak:
   1188       oldbfd = h->root.u.def.section->owner;
   1189       oldsec = h->root.u.def.section;
   1190       break;
   1191 
   1192     case bfd_link_hash_common:
   1193       oldbfd = h->root.u.c.p->section->owner;
   1194       oldsec = h->root.u.c.p->section;
   1195       if (pold_alignment)
   1196 	*pold_alignment = h->root.u.c.p->alignment_power;
   1197       break;
   1198     }
   1199   if (poldbfd && *poldbfd == NULL)
   1200     *poldbfd = oldbfd;
   1201 
   1202   /* Differentiate strong and weak symbols.  */
   1203   newweak = bind == STB_WEAK;
   1204   oldweak = (h->root.type == bfd_link_hash_defweak
   1205 	     || h->root.type == bfd_link_hash_undefweak);
   1206   if (pold_weak)
   1207     *pold_weak = oldweak;
   1208 
   1209   /* We have to check it for every instance since the first few may be
   1210      references and not all compilers emit symbol type for undefined
   1211      symbols.  */
   1212   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1213 
   1214   htab = elf_hash_table (info);
   1215 
   1216   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1217      respectively, is from a dynamic object.  */
   1218 
   1219   newdyn = (abfd->flags & DYNAMIC) != 0;
   1220 
   1221   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1222      syms and defined syms in dynamic libraries respectively.
   1223      ref_dynamic on the other hand can be set for a symbol defined in
   1224      a dynamic library, and def_dynamic may not be set;  When the
   1225      definition in a dynamic lib is overridden by a definition in the
   1226      executable use of the symbol in the dynamic lib becomes a
   1227      reference to the executable symbol.  */
   1228   if (newdyn)
   1229     {
   1230       if (bfd_is_und_section (sec))
   1231 	{
   1232 	  if (bind != STB_WEAK)
   1233 	    {
   1234 	      h->ref_dynamic_nonweak = 1;
   1235 	      hi->ref_dynamic_nonweak = 1;
   1236 	    }
   1237 	}
   1238       else
   1239 	{
   1240 	  /* Update the existing symbol only if they match. */
   1241 	  if (*matched)
   1242 	    h->dynamic_def = 1;
   1243 	  hi->dynamic_def = 1;
   1244 	}
   1245     }
   1246 
   1247   /* If we just created the symbol, mark it as being an ELF symbol.
   1248      Other than that, there is nothing to do--there is no merge issue
   1249      with a newly defined symbol--so we just return.  */
   1250 
   1251   if (h->root.type == bfd_link_hash_new)
   1252     {
   1253       h->non_elf = 0;
   1254       return true;
   1255     }
   1256 
   1257   /* In cases involving weak versioned symbols, we may wind up trying
   1258      to merge a symbol with itself.  Catch that here, to avoid the
   1259      confusion that results if we try to override a symbol with
   1260      itself.  The additional tests catch cases like
   1261      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1262      dynamic object, which we do want to handle here.  */
   1263   if (abfd == oldbfd
   1264       && (newweak || oldweak)
   1265       && ((abfd->flags & DYNAMIC) == 0
   1266 	  || !h->def_regular))
   1267     return true;
   1268 
   1269   olddyn = false;
   1270   if (oldbfd != NULL)
   1271     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1272   else if (oldsec != NULL)
   1273     {
   1274       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1275 	 indices used by MIPS ELF.  */
   1276       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1277     }
   1278 
   1279   /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
   1280   if (!htab->handling_dt_needed
   1281       && oldbfd != NULL
   1282       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
   1283     {
   1284       if (newdyn != olddyn)
   1285 	{
   1286 	  /* Handle a case where plugin_notice won't be called and thus
   1287 	     won't set the non_ir_ref flags on the first pass over
   1288 	     symbols.  */
   1289 	  h->root.non_ir_ref_dynamic = true;
   1290 	  hi->root.non_ir_ref_dynamic = true;
   1291 	}
   1292       else if ((oldbfd->flags & BFD_PLUGIN) != 0
   1293 	       && hi->root.type == bfd_link_hash_indirect)
   1294 	{
   1295 	  /* Change indirect symbol from IR to undefined.  */
   1296 	  hi->root.type = bfd_link_hash_undefined;
   1297 	  hi->root.u.undef.abfd = oldbfd;
   1298 	}
   1299     }
   1300 
   1301   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1302      respectively, appear to be a definition rather than reference.  */
   1303 
   1304   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1305 
   1306   olddef = (h->root.type != bfd_link_hash_undefined
   1307 	    && h->root.type != bfd_link_hash_undefweak
   1308 	    && h->root.type != bfd_link_hash_common);
   1309 
   1310   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1311      respectively, appear to be a function.  */
   1312 
   1313   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1314 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1315 
   1316   oldfunc = (h->type != STT_NOTYPE
   1317 	     && bed->is_function_type (h->type));
   1318 
   1319   if (!(newfunc && oldfunc)
   1320       && ELF_ST_TYPE (sym->st_info) != h->type
   1321       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1322       && h->type != STT_NOTYPE
   1323       && (newdef || bfd_is_com_section (sec))
   1324       && (olddef || h->root.type == bfd_link_hash_common))
   1325     {
   1326       /* If creating a default indirect symbol ("foo" or "foo@") from
   1327 	 a dynamic versioned definition ("foo@@") skip doing so if
   1328 	 there is an existing regular definition with a different
   1329 	 type.  We don't want, for example, a "time" variable in the
   1330 	 executable overriding a "time" function in a shared library.  */
   1331       if (newdyn
   1332 	  && !olddyn)
   1333 	{
   1334 	  *skip = true;
   1335 	  return true;
   1336 	}
   1337 
   1338       /* When adding a symbol from a regular object file after we have
   1339 	 created indirect symbols, undo the indirection and any
   1340 	 dynamic state.  */
   1341       if (hi != h
   1342 	  && !newdyn
   1343 	  && olddyn)
   1344 	{
   1345 	  h = hi;
   1346 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1347 	  h->forced_local = 0;
   1348 	  h->ref_dynamic = 0;
   1349 	  h->def_dynamic = 0;
   1350 	  h->dynamic_def = 0;
   1351 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1352 	    {
   1353 	      h->root.type = bfd_link_hash_undefined;
   1354 	      h->root.u.undef.abfd = abfd;
   1355 	    }
   1356 	  else
   1357 	    {
   1358 	      h->root.type = bfd_link_hash_new;
   1359 	      h->root.u.undef.abfd = NULL;
   1360 	    }
   1361 	  return true;
   1362 	}
   1363     }
   1364 
   1365   /* Check TLS symbols.  We don't check undefined symbols introduced
   1366      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1367      check symbols from plugins because they also have no type.  */
   1368   if (oldbfd != NULL
   1369       && (oldbfd->flags & BFD_PLUGIN) == 0
   1370       && (abfd->flags & BFD_PLUGIN) == 0
   1371       && ELF_ST_TYPE (sym->st_info) != h->type
   1372       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1373     {
   1374       bfd *ntbfd, *tbfd;
   1375       bool ntdef, tdef;
   1376       asection *ntsec, *tsec;
   1377 
   1378       if (h->type == STT_TLS)
   1379 	{
   1380 	  ntbfd = abfd;
   1381 	  ntsec = sec;
   1382 	  ntdef = newdef;
   1383 	  tbfd = oldbfd;
   1384 	  tsec = oldsec;
   1385 	  tdef = olddef;
   1386 	}
   1387       else
   1388 	{
   1389 	  ntbfd = oldbfd;
   1390 	  ntsec = oldsec;
   1391 	  ntdef = olddef;
   1392 	  tbfd = abfd;
   1393 	  tsec = sec;
   1394 	  tdef = newdef;
   1395 	}
   1396 
   1397       if (tdef && ntdef)
   1398 	_bfd_error_handler
   1399 	  /* xgettext:c-format */
   1400 	  (_("%s: TLS definition in %pB section %pA "
   1401 	     "mismatches non-TLS definition in %pB section %pA"),
   1402 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
   1403       else if (!tdef && !ntdef)
   1404 	_bfd_error_handler
   1405 	  /* xgettext:c-format */
   1406 	  (_("%s: TLS reference in %pB "
   1407 	     "mismatches non-TLS reference in %pB"),
   1408 	   h->root.root.string, tbfd, ntbfd);
   1409       else if (tdef)
   1410 	_bfd_error_handler
   1411 	  /* xgettext:c-format */
   1412 	  (_("%s: TLS definition in %pB section %pA "
   1413 	     "mismatches non-TLS reference in %pB"),
   1414 	   h->root.root.string, tbfd, tsec, ntbfd);
   1415       else
   1416 	_bfd_error_handler
   1417 	  /* xgettext:c-format */
   1418 	  (_("%s: TLS reference in %pB "
   1419 	     "mismatches non-TLS definition in %pB section %pA"),
   1420 	   h->root.root.string, tbfd, ntbfd, ntsec);
   1421 
   1422       bfd_set_error (bfd_error_bad_value);
   1423       return false;
   1424     }
   1425 
   1426   /* If the old symbol has non-default visibility, we ignore the new
   1427      definition from a dynamic object.  */
   1428   if (newdyn
   1429       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1430       && !bfd_is_und_section (sec))
   1431     {
   1432       *skip = true;
   1433       /* Make sure this symbol is dynamic.  */
   1434       h->ref_dynamic = 1;
   1435       hi->ref_dynamic = 1;
   1436       /* A protected symbol has external availability. Make sure it is
   1437 	 recorded as dynamic.
   1438 
   1439 	 FIXME: Should we check type and size for protected symbol?  */
   1440       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1441 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1442       else
   1443 	return true;
   1444     }
   1445   else if (!newdyn
   1446 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1447 	   && h->def_dynamic)
   1448     {
   1449       /* If the new symbol with non-default visibility comes from a
   1450 	 relocatable file and the old definition comes from a dynamic
   1451 	 object, we remove the old definition.  */
   1452       if (hi->root.type == bfd_link_hash_indirect)
   1453 	{
   1454 	  /* Handle the case where the old dynamic definition is
   1455 	     default versioned.  We need to copy the symbol info from
   1456 	     the symbol with default version to the normal one if it
   1457 	     was referenced before.  */
   1458 	  if (h->ref_regular)
   1459 	    {
   1460 	      hi->root.type = h->root.type;
   1461 	      h->root.type = bfd_link_hash_indirect;
   1462 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1463 
   1464 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1465 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1466 		{
   1467 		  /* If the new symbol is hidden or internal, completely undo
   1468 		     any dynamic link state.  */
   1469 		  (*bed->elf_backend_hide_symbol) (info, h, true);
   1470 		  h->forced_local = 0;
   1471 		  h->ref_dynamic = 0;
   1472 		}
   1473 	      else
   1474 		h->ref_dynamic = 1;
   1475 
   1476 	      h->def_dynamic = 0;
   1477 	      /* FIXME: Should we check type and size for protected symbol?  */
   1478 	      h->size = 0;
   1479 	      h->type = 0;
   1480 
   1481 	      h = hi;
   1482 	    }
   1483 	  else
   1484 	    h = hi;
   1485 	}
   1486 
   1487       /* If the old symbol was undefined before, then it will still be
   1488 	 on the undefs list.  If the new symbol is undefined or
   1489 	 common, we can't make it bfd_link_hash_new here, because new
   1490 	 undefined or common symbols will be added to the undefs list
   1491 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1492 	 added twice to the undefs list.  Also, if the new symbol is
   1493 	 undefweak then we don't want to lose the strong undef.  */
   1494       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1495 	{
   1496 	  h->root.type = bfd_link_hash_undefined;
   1497 	  h->root.u.undef.abfd = abfd;
   1498 	}
   1499       else
   1500 	{
   1501 	  h->root.type = bfd_link_hash_new;
   1502 	  h->root.u.undef.abfd = NULL;
   1503 	}
   1504 
   1505       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1506 	{
   1507 	  /* If the new symbol is hidden or internal, completely undo
   1508 	     any dynamic link state.  */
   1509 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   1510 	  h->forced_local = 0;
   1511 	  h->ref_dynamic = 0;
   1512 	}
   1513       else
   1514 	h->ref_dynamic = 1;
   1515       h->def_dynamic = 0;
   1516       /* FIXME: Should we check type and size for protected symbol?  */
   1517       h->size = 0;
   1518       h->type = 0;
   1519       return true;
   1520     }
   1521 
   1522   /* If a new weak symbol definition comes from a regular file and the
   1523      old symbol comes from a dynamic library, we treat the new one as
   1524      strong.  Similarly, an old weak symbol definition from a regular
   1525      file is treated as strong when the new symbol comes from a dynamic
   1526      library.  Further, an old weak symbol from a dynamic library is
   1527      treated as strong if the new symbol is from a dynamic library.
   1528      This reflects the way glibc's ld.so works.
   1529 
   1530      Also allow a weak symbol to override a linker script symbol
   1531      defined by an early pass over the script.  This is done so the
   1532      linker knows the symbol is defined in an object file, for the
   1533      DEFINED script function.
   1534 
   1535      Do this before setting *type_change_ok or *size_change_ok so that
   1536      we warn properly when dynamic library symbols are overridden.  */
   1537 
   1538   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
   1539     newweak = false;
   1540   if (olddef && newdyn)
   1541     oldweak = false;
   1542 
   1543   /* Allow changes between different types of function symbol.  */
   1544   if (newfunc && oldfunc)
   1545     *type_change_ok = true;
   1546 
   1547   /* It's OK to change the type if either the existing symbol or the
   1548      new symbol is weak.  A type change is also OK if the old symbol
   1549      is undefined and the new symbol is defined.  */
   1550 
   1551   if (oldweak
   1552       || newweak
   1553       || (newdef
   1554 	  && h->root.type == bfd_link_hash_undefined))
   1555     *type_change_ok = true;
   1556 
   1557   /* It's OK to change the size if either the existing symbol or the
   1558      new symbol is weak, or if the old symbol is undefined.  */
   1559 
   1560   if (*type_change_ok
   1561       || h->root.type == bfd_link_hash_undefined)
   1562     *size_change_ok = true;
   1563 
   1564   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1565      symbol, respectively, appears to be a common symbol in a dynamic
   1566      object.  If a symbol appears in an uninitialized section, and is
   1567      not weak, and is not a function, then it may be a common symbol
   1568      which was resolved when the dynamic object was created.  We want
   1569      to treat such symbols specially, because they raise special
   1570      considerations when setting the symbol size: if the symbol
   1571      appears as a common symbol in a regular object, and the size in
   1572      the regular object is larger, we must make sure that we use the
   1573      larger size.  This problematic case can always be avoided in C,
   1574      but it must be handled correctly when using Fortran shared
   1575      libraries.
   1576 
   1577      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1578      likewise for OLDDYNCOMMON and OLDDEF.
   1579 
   1580      Note that this test is just a heuristic, and that it is quite
   1581      possible to have an uninitialized symbol in a shared object which
   1582      is really a definition, rather than a common symbol.  This could
   1583      lead to some minor confusion when the symbol really is a common
   1584      symbol in some regular object.  However, I think it will be
   1585      harmless.  */
   1586 
   1587   if (newdyn
   1588       && newdef
   1589       && !newweak
   1590       && (sec->flags & SEC_ALLOC) != 0
   1591       && (sec->flags & SEC_LOAD) == 0
   1592       && sym->st_size > 0
   1593       && !newfunc)
   1594     newdyncommon = true;
   1595   else
   1596     newdyncommon = false;
   1597 
   1598   if (olddyn
   1599       && olddef
   1600       && h->root.type == bfd_link_hash_defined
   1601       && h->def_dynamic
   1602       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1603       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1604       && h->size > 0
   1605       && !oldfunc)
   1606     olddyncommon = true;
   1607   else
   1608     olddyncommon = false;
   1609 
   1610   /* We now know everything about the old and new symbols.  We ask the
   1611      backend to check if we can merge them.  */
   1612   if (bed->merge_symbol != NULL)
   1613     {
   1614       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1615 	return false;
   1616       sec = *psec;
   1617     }
   1618 
   1619   /* There are multiple definitions of a normal symbol.  Skip the
   1620      default symbol as well as definition from an IR object.  */
   1621   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
   1622       && !default_sym && h->def_regular
   1623       && !(oldbfd != NULL
   1624 	   && (oldbfd->flags & BFD_PLUGIN) != 0
   1625 	   && (abfd->flags & BFD_PLUGIN) == 0))
   1626     {
   1627       /* Handle a multiple definition.  */
   1628       (*info->callbacks->multiple_definition) (info, &h->root,
   1629 					       abfd, sec, *pvalue);
   1630       *skip = true;
   1631       return true;
   1632     }
   1633 
   1634   /* If both the old and the new symbols look like common symbols in a
   1635      dynamic object, set the size of the symbol to the larger of the
   1636      two.  */
   1637 
   1638   if (olddyncommon
   1639       && newdyncommon
   1640       && sym->st_size != h->size)
   1641     {
   1642       /* Since we think we have two common symbols, issue a multiple
   1643 	 common warning if desired.  Note that we only warn if the
   1644 	 size is different.  If the size is the same, we simply let
   1645 	 the old symbol override the new one as normally happens with
   1646 	 symbols defined in dynamic objects.  */
   1647 
   1648       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1649 					   bfd_link_hash_common, sym->st_size);
   1650       if (sym->st_size > h->size)
   1651 	h->size = sym->st_size;
   1652 
   1653       *size_change_ok = true;
   1654     }
   1655 
   1656   /* If we are looking at a dynamic object, and we have found a
   1657      definition, we need to see if the symbol was already defined by
   1658      some other object.  If so, we want to use the existing
   1659      definition, and we do not want to report a multiple symbol
   1660      definition error; we do this by clobbering *PSEC to be
   1661      bfd_und_section_ptr.
   1662 
   1663      We treat a common symbol as a definition if the symbol in the
   1664      shared library is a function, since common symbols always
   1665      represent variables; this can cause confusion in principle, but
   1666      any such confusion would seem to indicate an erroneous program or
   1667      shared library.  We also permit a common symbol in a regular
   1668      object to override a weak symbol in a shared object.  */
   1669 
   1670   if (newdyn
   1671       && newdef
   1672       && (olddef
   1673 	  || (h->root.type == bfd_link_hash_common
   1674 	      && (newweak || newfunc))))
   1675     {
   1676       *override = abfd;
   1677       newdef = false;
   1678       newdyncommon = false;
   1679 
   1680       *psec = sec = bfd_und_section_ptr;
   1681       *size_change_ok = true;
   1682 
   1683       /* If we get here when the old symbol is a common symbol, then
   1684 	 we are explicitly letting it override a weak symbol or
   1685 	 function in a dynamic object, and we don't want to warn about
   1686 	 a type change.  If the old symbol is a defined symbol, a type
   1687 	 change warning may still be appropriate.  */
   1688 
   1689       if (h->root.type == bfd_link_hash_common)
   1690 	*type_change_ok = true;
   1691     }
   1692 
   1693   /* Handle the special case of an old common symbol merging with a
   1694      new symbol which looks like a common symbol in a shared object.
   1695      We change *PSEC and *PVALUE to make the new symbol look like a
   1696      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1697      right thing.  */
   1698 
   1699   if (newdyncommon
   1700       && h->root.type == bfd_link_hash_common)
   1701     {
   1702       *override = oldbfd;
   1703       newdef = false;
   1704       newdyncommon = false;
   1705       *pvalue = sym->st_size;
   1706       *psec = sec = bed->common_section (oldsec);
   1707       *size_change_ok = true;
   1708     }
   1709 
   1710   /* Skip weak definitions of symbols that are already defined.  */
   1711   if (newdef && olddef && newweak)
   1712     {
   1713       /* Don't skip new non-IR weak syms.  */
   1714       if (!(oldbfd != NULL
   1715 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1716 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1717 	{
   1718 	  newdef = false;
   1719 	  *skip = true;
   1720 	}
   1721 
   1722       /* Merge st_other.  If the symbol already has a dynamic index,
   1723 	 but visibility says it should not be visible, turn it into a
   1724 	 local symbol.  */
   1725       elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
   1726       if (h->dynindx != -1)
   1727 	switch (ELF_ST_VISIBILITY (h->other))
   1728 	  {
   1729 	  case STV_INTERNAL:
   1730 	  case STV_HIDDEN:
   1731 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   1732 	    break;
   1733 	  }
   1734     }
   1735 
   1736   /* If the old symbol is from a dynamic object, and the new symbol is
   1737      a definition which is not from a dynamic object, then the new
   1738      symbol overrides the old symbol.  Symbols from regular files
   1739      always take precedence over symbols from dynamic objects, even if
   1740      they are defined after the dynamic object in the link.
   1741 
   1742      As above, we again permit a common symbol in a regular object to
   1743      override a definition in a shared object if the shared object
   1744      symbol is a function or is weak.  */
   1745 
   1746   flip = NULL;
   1747   if (!newdyn
   1748       && (newdef
   1749 	  || (bfd_is_com_section (sec)
   1750 	      && (oldweak || oldfunc)))
   1751       && olddyn
   1752       && olddef
   1753       && h->def_dynamic)
   1754     {
   1755       /* Change the hash table entry to undefined, and let
   1756 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1757 	 new definition.  */
   1758 
   1759       h->root.type = bfd_link_hash_undefined;
   1760       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1761       *size_change_ok = true;
   1762 
   1763       olddef = false;
   1764       olddyncommon = false;
   1765 
   1766       /* We again permit a type change when a common symbol may be
   1767 	 overriding a function.  */
   1768 
   1769       if (bfd_is_com_section (sec))
   1770 	{
   1771 	  if (oldfunc)
   1772 	    {
   1773 	      /* If a common symbol overrides a function, make sure
   1774 		 that it isn't defined dynamically nor has type
   1775 		 function.  */
   1776 	      h->def_dynamic = 0;
   1777 	      h->type = STT_NOTYPE;
   1778 	    }
   1779 	  *type_change_ok = true;
   1780 	}
   1781 
   1782       if (hi->root.type == bfd_link_hash_indirect)
   1783 	flip = hi;
   1784       else
   1785 	/* This union may have been set to be non-NULL when this symbol
   1786 	   was seen in a dynamic object.  We must force the union to be
   1787 	   NULL, so that it is correct for a regular symbol.  */
   1788 	h->verinfo.vertree = NULL;
   1789     }
   1790 
   1791   /* Handle the special case of a new common symbol merging with an
   1792      old symbol that looks like it might be a common symbol defined in
   1793      a shared object.  Note that we have already handled the case in
   1794      which a new common symbol should simply override the definition
   1795      in the shared library.  */
   1796 
   1797   if (! newdyn
   1798       && bfd_is_com_section (sec)
   1799       && olddyncommon)
   1800     {
   1801       /* It would be best if we could set the hash table entry to a
   1802 	 common symbol, but we don't know what to use for the section
   1803 	 or the alignment.  */
   1804       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1805 					   bfd_link_hash_common, sym->st_size);
   1806 
   1807       /* If the presumed common symbol in the dynamic object is
   1808 	 larger, pretend that the new symbol has its size.  */
   1809 
   1810       if (h->size > *pvalue)
   1811 	*pvalue = h->size;
   1812 
   1813       /* We need to remember the alignment required by the symbol
   1814 	 in the dynamic object.  */
   1815       BFD_ASSERT (pold_alignment);
   1816       *pold_alignment = h->root.u.def.section->alignment_power;
   1817 
   1818       olddef = false;
   1819       olddyncommon = false;
   1820 
   1821       h->root.type = bfd_link_hash_undefined;
   1822       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1823 
   1824       *size_change_ok = true;
   1825       *type_change_ok = true;
   1826 
   1827       if (hi->root.type == bfd_link_hash_indirect)
   1828 	flip = hi;
   1829       else
   1830 	h->verinfo.vertree = NULL;
   1831     }
   1832 
   1833   if (flip != NULL)
   1834     {
   1835       /* Handle the case where we had a versioned symbol in a dynamic
   1836 	 library and now find a definition in a normal object.  In this
   1837 	 case, we make the versioned symbol point to the normal one.  */
   1838       flip->root.type = h->root.type;
   1839       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1840       h->root.type = bfd_link_hash_indirect;
   1841       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1842       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1843       if (h->def_dynamic)
   1844 	{
   1845 	  h->def_dynamic = 0;
   1846 	  flip->ref_dynamic = 1;
   1847 	}
   1848     }
   1849 
   1850   return true;
   1851 }
   1852 
   1853 /* This function is called to create an indirect symbol from the
   1854    default for the symbol with the default version if needed. The
   1855    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1856    set DYNSYM if the new indirect symbol is dynamic.  */
   1857 
   1858 static bool
   1859 _bfd_elf_add_default_symbol (bfd *abfd,
   1860 			     struct bfd_link_info *info,
   1861 			     struct elf_link_hash_entry *h,
   1862 			     const char *name,
   1863 			     Elf_Internal_Sym *sym,
   1864 			     asection *sec,
   1865 			     bfd_vma value,
   1866 			     bfd **poldbfd,
   1867 			     bool *dynsym)
   1868 {
   1869   bool type_change_ok;
   1870   bool size_change_ok;
   1871   bool skip;
   1872   char *shortname;
   1873   struct elf_link_hash_entry *hi;
   1874   struct bfd_link_hash_entry *bh;
   1875   const struct elf_backend_data *bed;
   1876   bool collect;
   1877   bool dynamic;
   1878   bfd *override;
   1879   char *p;
   1880   size_t len, shortlen;
   1881   asection *tmp_sec;
   1882   bool matched;
   1883 
   1884   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1885     return true;
   1886 
   1887   /* If this symbol has a version, and it is the default version, we
   1888      create an indirect symbol from the default name to the fully
   1889      decorated name.  This will cause external references which do not
   1890      specify a version to be bound to this version of the symbol.  */
   1891   p = strchr (name, ELF_VER_CHR);
   1892   if (h->versioned == unknown)
   1893     {
   1894       if (p == NULL)
   1895 	{
   1896 	  h->versioned = unversioned;
   1897 	  return true;
   1898 	}
   1899       else
   1900 	{
   1901 	  if (p[1] != ELF_VER_CHR)
   1902 	    {
   1903 	      h->versioned = versioned_hidden;
   1904 	      return true;
   1905 	    }
   1906 	  else
   1907 	    h->versioned = versioned;
   1908 	}
   1909     }
   1910   else
   1911     {
   1912       /* PR ld/19073: We may see an unversioned definition after the
   1913 	 default version.  */
   1914       if (p == NULL)
   1915 	return true;
   1916     }
   1917 
   1918   bed = get_elf_backend_data (abfd);
   1919   collect = bed->collect;
   1920   dynamic = (abfd->flags & DYNAMIC) != 0;
   1921 
   1922   shortlen = p - name;
   1923   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   1924   if (shortname == NULL)
   1925     return false;
   1926   memcpy (shortname, name, shortlen);
   1927   shortname[shortlen] = '\0';
   1928 
   1929   /* We are going to create a new symbol.  Merge it with any existing
   1930      symbol with this name.  For the purposes of the merge, act as
   1931      though we were defining the symbol we just defined, although we
   1932      actually going to define an indirect symbol.  */
   1933   type_change_ok = false;
   1934   size_change_ok = false;
   1935   matched = true;
   1936   tmp_sec = sec;
   1937   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1938 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1939 			      &type_change_ok, &size_change_ok, &matched))
   1940     return false;
   1941 
   1942   if (skip)
   1943     goto nondefault;
   1944 
   1945   if (hi->def_regular || ELF_COMMON_DEF_P (hi))
   1946     {
   1947       /* If the undecorated symbol will have a version added by a
   1948 	 script different to H, then don't indirect to/from the
   1949 	 undecorated symbol.  This isn't ideal because we may not yet
   1950 	 have seen symbol versions, if given by a script on the
   1951 	 command line rather than via --version-script.  */
   1952       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   1953 	{
   1954 	  bool hide;
   1955 
   1956 	  hi->verinfo.vertree
   1957 	    = bfd_find_version_for_sym (info->version_info,
   1958 					hi->root.root.string, &hide);
   1959 	  if (hi->verinfo.vertree != NULL && hide)
   1960 	    {
   1961 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   1962 	      goto nondefault;
   1963 	    }
   1964 	}
   1965       if (hi->verinfo.vertree != NULL
   1966 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   1967 	goto nondefault;
   1968     }
   1969 
   1970   if (! override)
   1971     {
   1972       /* Add the default symbol if not performing a relocatable link.  */
   1973       if (! bfd_link_relocatable (info))
   1974 	{
   1975 	  bh = &hi->root;
   1976 	  if (bh->type == bfd_link_hash_defined
   1977 	      && bh->u.def.section->owner != NULL
   1978 	      && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
   1979 	    {
   1980 	      /* Mark the previous definition from IR object as
   1981 		 undefined so that the generic linker will override
   1982 		 it.  */
   1983 	      bh->type = bfd_link_hash_undefined;
   1984 	      bh->u.undef.abfd = bh->u.def.section->owner;
   1985 	    }
   1986 	  if (! (_bfd_generic_link_add_one_symbol
   1987 		 (info, abfd, shortname, BSF_INDIRECT,
   1988 		  bfd_ind_section_ptr,
   1989 		  0, name, false, collect, &bh)))
   1990 	    return false;
   1991 	  hi = (struct elf_link_hash_entry *) bh;
   1992 	}
   1993     }
   1994   else
   1995     {
   1996       /* In this case the symbol named SHORTNAME is overriding the
   1997 	 indirect symbol we want to add.  We were planning on making
   1998 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   1999 	 is the name without a version.  NAME is the fully versioned
   2000 	 name, and it is the default version.
   2001 
   2002 	 Overriding means that we already saw a definition for the
   2003 	 symbol SHORTNAME in a regular object, and it is overriding
   2004 	 the symbol defined in the dynamic object.
   2005 
   2006 	 When this happens, we actually want to change NAME, the
   2007 	 symbol we just added, to refer to SHORTNAME.  This will cause
   2008 	 references to NAME in the shared object to become references
   2009 	 to SHORTNAME in the regular object.  This is what we expect
   2010 	 when we override a function in a shared object: that the
   2011 	 references in the shared object will be mapped to the
   2012 	 definition in the regular object.  */
   2013 
   2014       while (hi->root.type == bfd_link_hash_indirect
   2015 	     || hi->root.type == bfd_link_hash_warning)
   2016 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2017 
   2018       h->root.type = bfd_link_hash_indirect;
   2019       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   2020       if (h->def_dynamic)
   2021 	{
   2022 	  h->def_dynamic = 0;
   2023 	  hi->ref_dynamic = 1;
   2024 	  if (hi->ref_regular
   2025 	      || hi->def_regular)
   2026 	    {
   2027 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   2028 		return false;
   2029 	    }
   2030 	}
   2031 
   2032       /* Now set HI to H, so that the following code will set the
   2033 	 other fields correctly.  */
   2034       hi = h;
   2035     }
   2036 
   2037   /* Check if HI is a warning symbol.  */
   2038   if (hi->root.type == bfd_link_hash_warning)
   2039     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2040 
   2041   /* If there is a duplicate definition somewhere, then HI may not
   2042      point to an indirect symbol.  We will have reported an error to
   2043      the user in that case.  */
   2044 
   2045   if (hi->root.type == bfd_link_hash_indirect)
   2046     {
   2047       struct elf_link_hash_entry *ht;
   2048 
   2049       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2050       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   2051 
   2052       /* If we first saw a reference to SHORTNAME with non-default
   2053 	 visibility, merge that visibility to the @@VER symbol.  */
   2054       elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
   2055 
   2056       /* A reference to the SHORTNAME symbol from a dynamic library
   2057 	 will be satisfied by the versioned symbol at runtime.  In
   2058 	 effect, we have a reference to the versioned symbol.  */
   2059       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2060       hi->dynamic_def |= ht->dynamic_def;
   2061 
   2062       /* See if the new flags lead us to realize that the symbol must
   2063 	 be dynamic.  */
   2064       if (! *dynsym)
   2065 	{
   2066 	  if (! dynamic)
   2067 	    {
   2068 	      if (! bfd_link_executable (info)
   2069 		  || hi->def_dynamic
   2070 		  || hi->ref_dynamic)
   2071 		*dynsym = true;
   2072 	    }
   2073 	  else
   2074 	    {
   2075 	      if (hi->ref_regular)
   2076 		*dynsym = true;
   2077 	    }
   2078 	}
   2079     }
   2080 
   2081   /* We also need to define an indirection from the nondefault version
   2082      of the symbol.  */
   2083 
   2084  nondefault:
   2085   len = strlen (name);
   2086   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   2087   if (shortname == NULL)
   2088     return false;
   2089   memcpy (shortname, name, shortlen);
   2090   memcpy (shortname + shortlen, p + 1, len - shortlen);
   2091 
   2092   /* Once again, merge with any existing symbol.  */
   2093   type_change_ok = false;
   2094   size_change_ok = false;
   2095   tmp_sec = sec;
   2096   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2097 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2098 			      &type_change_ok, &size_change_ok, &matched))
   2099     return false;
   2100 
   2101   if (skip)
   2102     {
   2103       if (!dynamic
   2104 	  && h->root.type == bfd_link_hash_defweak
   2105 	  && hi->root.type == bfd_link_hash_defined)
   2106 	{
   2107 	  /* We are handling a weak sym@@ver and attempting to define
   2108 	     a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
   2109 	     new weak sym@ver because there is already a strong sym@ver.
   2110 	     However, sym@ver and sym@@ver are really the same symbol.
   2111 	     The existing strong sym@ver ought to override sym@@ver.  */
   2112 	  h->root.type = bfd_link_hash_defined;
   2113 	  h->root.u.def.section = hi->root.u.def.section;
   2114 	  h->root.u.def.value = hi->root.u.def.value;
   2115 	  hi->root.type = bfd_link_hash_indirect;
   2116 	  hi->root.u.i.link = &h->root;
   2117 	}
   2118       else
   2119 	return true;
   2120     }
   2121   else if (override)
   2122     {
   2123       /* Here SHORTNAME is a versioned name, so we don't expect to see
   2124 	 the type of override we do in the case above unless it is
   2125 	 overridden by a versioned definition.  */
   2126       if (hi->root.type != bfd_link_hash_defined
   2127 	  && hi->root.type != bfd_link_hash_defweak)
   2128 	_bfd_error_handler
   2129 	  /* xgettext:c-format */
   2130 	  (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
   2131 	   abfd, shortname);
   2132       return true;
   2133     }
   2134   else
   2135     {
   2136       bh = &hi->root;
   2137       if (! (_bfd_generic_link_add_one_symbol
   2138 	     (info, abfd, shortname, BSF_INDIRECT,
   2139 	      bfd_ind_section_ptr, 0, name, false, collect, &bh)))
   2140 	return false;
   2141       hi = (struct elf_link_hash_entry *) bh;
   2142     }
   2143 
   2144   /* If there is a duplicate definition somewhere, then HI may not
   2145      point to an indirect symbol.  We will have reported an error
   2146      to the user in that case.  */
   2147   if (hi->root.type == bfd_link_hash_indirect)
   2148     {
   2149       (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   2150       h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2151       hi->dynamic_def |= h->dynamic_def;
   2152 
   2153       /* If we first saw a reference to @VER symbol with
   2154 	 non-default visibility, merge that visibility to the
   2155 	 @@VER symbol.  */
   2156       elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
   2157 
   2158       /* See if the new flags lead us to realize that the symbol
   2159 	 must be dynamic.  */
   2160       if (! *dynsym)
   2161 	{
   2162 	  if (! dynamic)
   2163 	    {
   2164 	      if (! bfd_link_executable (info)
   2165 		  || hi->ref_dynamic)
   2166 		*dynsym = true;
   2167 	    }
   2168 	  else
   2169 	    {
   2170 	      if (hi->ref_regular)
   2171 		*dynsym = true;
   2172 	    }
   2173 	}
   2174     }
   2175 
   2176   return true;
   2177 }
   2178 
   2179 /* This routine is used to export all defined symbols into the dynamic
   2181    symbol table.  It is called via elf_link_hash_traverse.  */
   2182 
   2183 static bool
   2184 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   2185 {
   2186   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2187 
   2188   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2189   if (h->root.type == bfd_link_hash_indirect)
   2190     return true;
   2191 
   2192   /* Ignore this if we won't export it.  */
   2193   if (!eif->info->export_dynamic && !h->dynamic)
   2194     return true;
   2195 
   2196   if (h->dynindx == -1
   2197       && (h->def_regular || h->ref_regular)
   2198       && ! bfd_hide_sym_by_version (eif->info->version_info,
   2199 				    h->root.root.string))
   2200     {
   2201       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2202 	{
   2203 	  eif->failed = true;
   2204 	  return false;
   2205 	}
   2206     }
   2207 
   2208   return true;
   2209 }
   2210 
   2211 /* Return the glibc version reference if VERSION_DEP is added to the
   2213    list of glibc version dependencies successfully.  VERSION_DEP will
   2214    be put into the .gnu.version_r section.  */
   2215 
   2216 static Elf_Internal_Verneed *
   2217 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
   2218 			    Elf_Internal_Verneed *glibc_verref,
   2219 			    const char *version_dep)
   2220 {
   2221   Elf_Internal_Verneed *t;
   2222   Elf_Internal_Vernaux *a;
   2223   size_t amt;
   2224 
   2225   if (glibc_verref != NULL)
   2226     {
   2227       t = glibc_verref;
   2228 
   2229       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2230 	{
   2231 	  /* Return if VERSION_DEP dependency has been added.  */
   2232 	  if (a->vna_nodename == version_dep
   2233 	      || strcmp (a->vna_nodename, version_dep) == 0)
   2234 	    return t;
   2235 	}
   2236     }
   2237   else
   2238     {
   2239       bool is_glibc;
   2240 
   2241       for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2242 	   t != NULL;
   2243 	   t = t->vn_nextref)
   2244 	{
   2245 	  const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
   2246 	  if (soname != NULL && startswith (soname, "libc.so."))
   2247 	    break;
   2248 	}
   2249 
   2250       /* Skip the shared library if it isn't libc.so.  */
   2251       if (t == NULL)
   2252 	return t;
   2253 
   2254       is_glibc = false;
   2255       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2256 	{
   2257 	  /* Return if VERSION_DEP dependency has been added.  */
   2258 	  if (a->vna_nodename == version_dep
   2259 	      || strcmp (a->vna_nodename, version_dep) == 0)
   2260 	    return t;
   2261 
   2262 	  /* Check if libc.so provides GLIBC_2.XX version.  */
   2263 	  if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
   2264 	    is_glibc = true;
   2265 	}
   2266 
   2267       /* Skip if it isn't linked against glibc.  */
   2268       if (!is_glibc)
   2269 	return NULL;
   2270     }
   2271 
   2272   amt = sizeof *a;
   2273   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2274   if (a == NULL)
   2275     {
   2276       rinfo->failed = true;
   2277       return NULL;
   2278     }
   2279 
   2280   a->vna_nodename = version_dep;
   2281   a->vna_flags = 0;
   2282   a->vna_nextptr = t->vn_auxptr;
   2283   a->vna_other = rinfo->vers + 1;
   2284   ++rinfo->vers;
   2285 
   2286   t->vn_auxptr = a;
   2287 
   2288   return t;
   2289 }
   2290 
   2291 /* Add VERSION_DEP to the list of version dependencies when linked
   2292    against glibc.  */
   2293 
   2294 void
   2295 _bfd_elf_link_add_glibc_version_dependency
   2296   (struct elf_find_verdep_info *rinfo,
   2297    const char *version_dep[])
   2298 {
   2299   Elf_Internal_Verneed *t = NULL;
   2300 
   2301   do
   2302     {
   2303       t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
   2304       /* Return if there is no glibc version reference.  */
   2305       if (t == NULL)
   2306 	return;
   2307       version_dep++;
   2308     }
   2309   while (*version_dep != NULL);
   2310 }
   2311 
   2312 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
   2313    linked against glibc.  */
   2314 
   2315 void
   2316 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
   2317 {
   2318   if (rinfo->info->enable_dt_relr)
   2319     {
   2320       const char *version[] =
   2321 	{
   2322 	  "GLIBC_ABI_DT_RELR",
   2323 	  NULL
   2324 	};
   2325       _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
   2326     }
   2327 }
   2328 
   2329 /* Look through the symbols which are defined in other shared
   2330    libraries and referenced here.  Update the list of version
   2331    dependencies.  This will be put into the .gnu.version_r section.
   2332    This function is called via elf_link_hash_traverse.  */
   2333 
   2334 static bool
   2335 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2336 					 void *data)
   2337 {
   2338   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2339   Elf_Internal_Verneed *t;
   2340   Elf_Internal_Vernaux *a;
   2341   size_t amt;
   2342 
   2343   /* We only care about symbols defined in shared objects with version
   2344      information.  */
   2345   if (!h->def_dynamic
   2346       || h->def_regular
   2347       || h->dynindx == -1
   2348       || h->verinfo.verdef == NULL
   2349       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2350 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2351     return true;
   2352 
   2353   /* See if we already know about this version.  */
   2354   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2355        t != NULL;
   2356        t = t->vn_nextref)
   2357     {
   2358       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2359 	continue;
   2360 
   2361       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2362 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2363 	  return true;
   2364 
   2365       break;
   2366     }
   2367 
   2368   /* This is a new version.  Add it to tree we are building.  */
   2369 
   2370   if (t == NULL)
   2371     {
   2372       amt = sizeof *t;
   2373       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2374       if (t == NULL)
   2375 	{
   2376 	  rinfo->failed = true;
   2377 	  return false;
   2378 	}
   2379 
   2380       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2381       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2382       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2383     }
   2384 
   2385   amt = sizeof *a;
   2386   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2387   if (a == NULL)
   2388     {
   2389       rinfo->failed = true;
   2390       return false;
   2391     }
   2392 
   2393   /* Note that we are copying a string pointer here, and testing it
   2394      above.  If bfd_elf_string_from_elf_section is ever changed to
   2395      discard the string data when low in memory, this will have to be
   2396      fixed.  */
   2397   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2398 
   2399   a->vna_flags = h->verinfo.verdef->vd_flags;
   2400   a->vna_nextptr = t->vn_auxptr;
   2401 
   2402   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2403   ++rinfo->vers;
   2404 
   2405   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2406 
   2407   t->vn_auxptr = a;
   2408 
   2409   return true;
   2410 }
   2411 
   2412 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
   2413    hidden.  Set *T_P to NULL if there is no match.  */
   2414 
   2415 static bool
   2416 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
   2417 				     struct elf_link_hash_entry *h,
   2418 				     const char *version_p,
   2419 				     struct bfd_elf_version_tree **t_p,
   2420 				     bool *hide)
   2421 {
   2422   struct bfd_elf_version_tree *t;
   2423 
   2424   /* Look for the version.  If we find it, it is no longer weak.  */
   2425   for (t = info->version_info; t != NULL; t = t->next)
   2426     {
   2427       if (strcmp (t->name, version_p) == 0)
   2428 	{
   2429 	  size_t len;
   2430 	  char *alc;
   2431 	  struct bfd_elf_version_expr *d;
   2432 
   2433 	  len = version_p - h->root.root.string;
   2434 	  alc = (char *) bfd_malloc (len);
   2435 	  if (alc == NULL)
   2436 	    return false;
   2437 	  memcpy (alc, h->root.root.string, len - 1);
   2438 	  alc[len - 1] = '\0';
   2439 	  if (alc[len - 2] == ELF_VER_CHR)
   2440 	    alc[len - 2] = '\0';
   2441 
   2442 	  h->verinfo.vertree = t;
   2443 	  t->used = true;
   2444 	  d = NULL;
   2445 
   2446 	  if (t->globals.list != NULL)
   2447 	    d = (*t->match) (&t->globals, NULL, alc);
   2448 
   2449 	  /* See if there is anything to force this symbol to
   2450 	     local scope.  */
   2451 	  if (d == NULL && t->locals.list != NULL)
   2452 	    {
   2453 	      d = (*t->match) (&t->locals, NULL, alc);
   2454 	      if (d != NULL
   2455 		  && h->dynindx != -1
   2456 		  && ! info->export_dynamic)
   2457 		*hide = true;
   2458 	    }
   2459 
   2460 	  free (alc);
   2461 	  break;
   2462 	}
   2463     }
   2464 
   2465   *t_p = t;
   2466 
   2467   return true;
   2468 }
   2469 
   2470 /* Return TRUE if the symbol H is hidden by version script.  */
   2471 
   2472 bool
   2473 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
   2474 				   struct elf_link_hash_entry *h)
   2475 {
   2476   const char *p;
   2477   bool hide = false;
   2478   const struct elf_backend_data *bed
   2479     = get_elf_backend_data (info->output_bfd);
   2480 
   2481   /* Version script only hides symbols defined in regular objects.  */
   2482   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2483     return true;
   2484 
   2485   p = strchr (h->root.root.string, ELF_VER_CHR);
   2486   if (p != NULL && h->verinfo.vertree == NULL)
   2487     {
   2488       struct bfd_elf_version_tree *t;
   2489 
   2490       ++p;
   2491       if (*p == ELF_VER_CHR)
   2492 	++p;
   2493 
   2494       if (*p != '\0'
   2495 	  && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
   2496 	  && hide)
   2497 	{
   2498 	  if (hide)
   2499 	    (*bed->elf_backend_hide_symbol) (info, h, true);
   2500 	  return true;
   2501 	}
   2502     }
   2503 
   2504   /* If we don't have a version for this symbol, see if we can find
   2505      something.  */
   2506   if (h->verinfo.vertree == NULL && info->version_info != NULL)
   2507     {
   2508       h->verinfo.vertree
   2509 	= bfd_find_version_for_sym (info->version_info,
   2510 				    h->root.root.string, &hide);
   2511       if (h->verinfo.vertree != NULL && hide)
   2512 	{
   2513 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   2514 	  return true;
   2515 	}
   2516     }
   2517 
   2518   return false;
   2519 }
   2520 
   2521 /* Figure out appropriate versions for all the symbols.  We may not
   2522    have the version number script until we have read all of the input
   2523    files, so until that point we don't know which symbols should be
   2524    local.  This function is called via elf_link_hash_traverse.  */
   2525 
   2526 static bool
   2527 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2528 {
   2529   struct elf_info_failed *sinfo;
   2530   struct bfd_link_info *info;
   2531   const struct elf_backend_data *bed;
   2532   struct elf_info_failed eif;
   2533   char *p;
   2534   bool hide;
   2535 
   2536   sinfo = (struct elf_info_failed *) data;
   2537   info = sinfo->info;
   2538 
   2539   /* Fix the symbol flags.  */
   2540   eif.failed = false;
   2541   eif.info = info;
   2542   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2543     {
   2544       if (eif.failed)
   2545 	sinfo->failed = true;
   2546       return false;
   2547     }
   2548 
   2549   bed = get_elf_backend_data (info->output_bfd);
   2550 
   2551   /* We only need version numbers for symbols defined in regular
   2552      objects.  */
   2553   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2554     {
   2555       /* Hide symbols defined in discarded input sections.  */
   2556       if ((h->root.type == bfd_link_hash_defined
   2557 	   || h->root.type == bfd_link_hash_defweak)
   2558 	  && discarded_section (h->root.u.def.section))
   2559 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2560       return true;
   2561     }
   2562 
   2563   hide = false;
   2564   p = strchr (h->root.root.string, ELF_VER_CHR);
   2565   if (p != NULL && h->verinfo.vertree == NULL)
   2566     {
   2567       struct bfd_elf_version_tree *t;
   2568 
   2569       ++p;
   2570       if (*p == ELF_VER_CHR)
   2571 	++p;
   2572 
   2573       /* If there is no version string, we can just return out.  */
   2574       if (*p == '\0')
   2575 	return true;
   2576 
   2577       if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
   2578 	{
   2579 	  sinfo->failed = true;
   2580 	  return false;
   2581 	}
   2582 
   2583       if (hide)
   2584 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2585 
   2586       /* If we are building an application, we need to create a
   2587 	 version node for this version.  */
   2588       if (t == NULL && bfd_link_executable (info))
   2589 	{
   2590 	  struct bfd_elf_version_tree **pp;
   2591 	  int version_index;
   2592 
   2593 	  /* If we aren't going to export this symbol, we don't need
   2594 	     to worry about it.  */
   2595 	  if (h->dynindx == -1)
   2596 	    return true;
   2597 
   2598 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
   2599 							  sizeof *t);
   2600 	  if (t == NULL)
   2601 	    {
   2602 	      sinfo->failed = true;
   2603 	      return false;
   2604 	    }
   2605 
   2606 	  t->name = p;
   2607 	  t->name_indx = (unsigned int) -1;
   2608 	  t->used = true;
   2609 
   2610 	  version_index = 1;
   2611 	  /* Don't count anonymous version tag.  */
   2612 	  if (sinfo->info->version_info != NULL
   2613 	      && sinfo->info->version_info->vernum == 0)
   2614 	    version_index = 0;
   2615 	  for (pp = &sinfo->info->version_info;
   2616 	       *pp != NULL;
   2617 	       pp = &(*pp)->next)
   2618 	    ++version_index;
   2619 	  t->vernum = version_index;
   2620 
   2621 	  *pp = t;
   2622 
   2623 	  h->verinfo.vertree = t;
   2624 	}
   2625       else if (t == NULL)
   2626 	{
   2627 	  /* We could not find the version for a symbol when
   2628 	     generating a shared archive.  Return an error.  */
   2629 	  _bfd_error_handler
   2630 	    /* xgettext:c-format */
   2631 	    (_("%pB: version node not found for symbol %s"),
   2632 	     info->output_bfd, h->root.root.string);
   2633 	  bfd_set_error (bfd_error_bad_value);
   2634 	  sinfo->failed = true;
   2635 	  return false;
   2636 	}
   2637     }
   2638 
   2639   /* If we don't have a version for this symbol, see if we can find
   2640      something.  */
   2641   if (!hide
   2642       && h->verinfo.vertree == NULL
   2643       && sinfo->info->version_info != NULL)
   2644     {
   2645       h->verinfo.vertree
   2646 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2647 				    h->root.root.string, &hide);
   2648       if (h->verinfo.vertree != NULL && hide)
   2649 	(*bed->elf_backend_hide_symbol) (info, h, true);
   2650     }
   2651 
   2652   return true;
   2653 }
   2654 
   2655 /* Read and swap the relocs from the section indicated by SHDR.  This
   2657    may be either a REL or a RELA section.  The relocations are
   2658    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2659    which should have already been allocated to contain enough space.
   2660    The EXTERNAL_RELOCS are a buffer where the external form of the
   2661    relocations should be stored.
   2662 
   2663    Returns FALSE if something goes wrong.  */
   2664 
   2665 static bool
   2666 elf_link_read_relocs_from_section (bfd *abfd,
   2667 				   asection *sec,
   2668 				   Elf_Internal_Shdr *shdr,
   2669 				   void *external_relocs,
   2670 				   Elf_Internal_Rela *internal_relocs)
   2671 {
   2672   const struct elf_backend_data *bed;
   2673   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2674   const bfd_byte *erela;
   2675   const bfd_byte *erelaend;
   2676   Elf_Internal_Rela *irela;
   2677   Elf_Internal_Shdr *symtab_hdr;
   2678   size_t nsyms;
   2679 
   2680   /* Position ourselves at the start of the section.  */
   2681   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2682     return false;
   2683 
   2684   /* Read the relocations.  */
   2685   if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
   2686     return false;
   2687 
   2688   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2689   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2690 
   2691   bed = get_elf_backend_data (abfd);
   2692 
   2693   /* Convert the external relocations to the internal format.  */
   2694   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2695     swap_in = bed->s->swap_reloc_in;
   2696   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2697     swap_in = bed->s->swap_reloca_in;
   2698   else
   2699     {
   2700       bfd_set_error (bfd_error_wrong_format);
   2701       return false;
   2702     }
   2703 
   2704   erela = (const bfd_byte *) external_relocs;
   2705   /* Setting erelaend like this and comparing with <= handles case of
   2706      a fuzzed object with sh_size not a multiple of sh_entsize.  */
   2707   erelaend = erela + shdr->sh_size - shdr->sh_entsize;
   2708   irela = internal_relocs;
   2709   while (erela <= erelaend)
   2710     {
   2711       bfd_vma r_symndx;
   2712 
   2713       (*swap_in) (abfd, erela, irela);
   2714       r_symndx = ELF32_R_SYM (irela->r_info);
   2715       if (bed->s->arch_size == 64)
   2716 	r_symndx >>= 24;
   2717       if (nsyms > 0)
   2718 	{
   2719 	  if ((size_t) r_symndx >= nsyms)
   2720 	    {
   2721 	      _bfd_error_handler
   2722 		/* xgettext:c-format */
   2723 		(_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
   2724 		   " for offset %#" PRIx64 " in section `%pA'"),
   2725 		 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
   2726 		 (uint64_t) irela->r_offset, sec);
   2727 	      bfd_set_error (bfd_error_bad_value);
   2728 	      return false;
   2729 	    }
   2730 	}
   2731       else if (r_symndx != STN_UNDEF)
   2732 	{
   2733 	  _bfd_error_handler
   2734 	    /* xgettext:c-format */
   2735 	    (_("%pB: non-zero symbol index (%#" PRIx64 ")"
   2736 	       " for offset %#" PRIx64 " in section `%pA'"
   2737 	       " when the object file has no symbol table"),
   2738 	     abfd, (uint64_t) r_symndx,
   2739 	     (uint64_t) irela->r_offset, sec);
   2740 	  bfd_set_error (bfd_error_bad_value);
   2741 	  return false;
   2742 	}
   2743       irela += bed->s->int_rels_per_ext_rel;
   2744       erela += shdr->sh_entsize;
   2745     }
   2746 
   2747   return true;
   2748 }
   2749 
   2750 /* Read and swap the relocs for a section O.  They may have been
   2751    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2752    not NULL, they are used as buffers to read into.  They are known to
   2753    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2754    the return value is allocated using either malloc or bfd_alloc,
   2755    according to the KEEP_MEMORY argument.  If O has two relocation
   2756    sections (both REL and RELA relocations), then the REL_HDR
   2757    relocations will appear first in INTERNAL_RELOCS, followed by the
   2758    RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
   2759    update cache_size.  */
   2760 
   2761 Elf_Internal_Rela *
   2762 _bfd_elf_link_info_read_relocs (bfd *abfd,
   2763 				struct bfd_link_info *info,
   2764 				asection *o,
   2765 				void *external_relocs,
   2766 				Elf_Internal_Rela *internal_relocs,
   2767 				bool keep_memory)
   2768 {
   2769   void *alloc1 = NULL;
   2770   Elf_Internal_Rela *alloc2 = NULL;
   2771   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2772   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2773   Elf_Internal_Rela *internal_rela_relocs;
   2774 
   2775   if (esdo->relocs != NULL)
   2776     return esdo->relocs;
   2777 
   2778   if (o->reloc_count == 0)
   2779     return NULL;
   2780 
   2781   if (internal_relocs == NULL)
   2782     {
   2783       bfd_size_type size;
   2784 
   2785       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
   2786       if (keep_memory)
   2787 	{
   2788 	  internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
   2789 	  if (info)
   2790 	    info->cache_size += size;
   2791 	}
   2792       else
   2793 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2794       if (internal_relocs == NULL)
   2795 	goto error_return;
   2796     }
   2797 
   2798   if (external_relocs == NULL)
   2799     {
   2800       bfd_size_type size = 0;
   2801 
   2802       if (esdo->rel.hdr)
   2803 	size += esdo->rel.hdr->sh_size;
   2804       if (esdo->rela.hdr)
   2805 	size += esdo->rela.hdr->sh_size;
   2806 
   2807       alloc1 = bfd_malloc (size);
   2808       if (alloc1 == NULL)
   2809 	goto error_return;
   2810       external_relocs = alloc1;
   2811     }
   2812 
   2813   internal_rela_relocs = internal_relocs;
   2814   if (esdo->rel.hdr)
   2815     {
   2816       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2817 					      external_relocs,
   2818 					      internal_relocs))
   2819 	goto error_return;
   2820       external_relocs = (((bfd_byte *) external_relocs)
   2821 			 + esdo->rel.hdr->sh_size);
   2822       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2823 			       * bed->s->int_rels_per_ext_rel);
   2824     }
   2825 
   2826   if (esdo->rela.hdr
   2827       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2828 					      external_relocs,
   2829 					      internal_rela_relocs)))
   2830     goto error_return;
   2831 
   2832   /* Cache the results for next time, if we can.  */
   2833   if (keep_memory)
   2834     esdo->relocs = internal_relocs;
   2835 
   2836   free (alloc1);
   2837 
   2838   /* Don't free alloc2, since if it was allocated we are passing it
   2839      back (under the name of internal_relocs).  */
   2840 
   2841   return internal_relocs;
   2842 
   2843  error_return:
   2844   free (alloc1);
   2845   if (alloc2 != NULL)
   2846     {
   2847       if (keep_memory)
   2848 	bfd_release (abfd, alloc2);
   2849       else
   2850 	free (alloc2);
   2851     }
   2852   return NULL;
   2853 }
   2854 
   2855 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
   2856    NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
   2857    struct bfd_link_info.  */
   2858 
   2859 Elf_Internal_Rela *
   2860 _bfd_elf_link_read_relocs (bfd *abfd,
   2861 			   asection *o,
   2862 			   void *external_relocs,
   2863 			   Elf_Internal_Rela *internal_relocs,
   2864 			   bool keep_memory)
   2865 {
   2866   return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
   2867 					 internal_relocs, keep_memory);
   2868 
   2869 }
   2870 
   2871 /* Compute the size of, and allocate space for, REL_HDR which is the
   2872    section header for a section containing relocations for O.  */
   2873 
   2874 static bool
   2875 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2876 				  struct bfd_elf_section_reloc_data *reldata)
   2877 {
   2878   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2879 
   2880   /* That allows us to calculate the size of the section.  */
   2881   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2882 
   2883   /* The contents field must last into write_object_contents, so we
   2884      allocate it with bfd_alloc rather than malloc.  Also since we
   2885      cannot be sure that the contents will actually be filled in,
   2886      we zero the allocated space.  */
   2887   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2888   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2889     return false;
   2890 
   2891   if (reldata->hashes == NULL && reldata->count)
   2892     {
   2893       struct elf_link_hash_entry **p;
   2894 
   2895       p = ((struct elf_link_hash_entry **)
   2896 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   2897       if (p == NULL)
   2898 	return false;
   2899 
   2900       reldata->hashes = p;
   2901     }
   2902 
   2903   return true;
   2904 }
   2905 
   2906 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2907    originated from the section given by INPUT_REL_HDR) to the
   2908    OUTPUT_BFD.  */
   2909 
   2910 bool
   2911 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2912 			     asection *input_section,
   2913 			     Elf_Internal_Shdr *input_rel_hdr,
   2914 			     Elf_Internal_Rela *internal_relocs,
   2915 			     struct elf_link_hash_entry **rel_hash
   2916 			       ATTRIBUTE_UNUSED)
   2917 {
   2918   Elf_Internal_Rela *irela;
   2919   Elf_Internal_Rela *irelaend;
   2920   bfd_byte *erel;
   2921   struct bfd_elf_section_reloc_data *output_reldata;
   2922   asection *output_section;
   2923   const struct elf_backend_data *bed;
   2924   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   2925   struct bfd_elf_section_data *esdo;
   2926 
   2927   output_section = input_section->output_section;
   2928 
   2929   bed = get_elf_backend_data (output_bfd);
   2930   esdo = elf_section_data (output_section);
   2931   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2932     {
   2933       output_reldata = &esdo->rel;
   2934       swap_out = bed->s->swap_reloc_out;
   2935     }
   2936   else if (esdo->rela.hdr
   2937 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2938     {
   2939       output_reldata = &esdo->rela;
   2940       swap_out = bed->s->swap_reloca_out;
   2941     }
   2942   else
   2943     {
   2944       _bfd_error_handler
   2945 	/* xgettext:c-format */
   2946 	(_("%pB: relocation size mismatch in %pB section %pA"),
   2947 	 output_bfd, input_section->owner, input_section);
   2948       bfd_set_error (bfd_error_wrong_format);
   2949       return false;
   2950     }
   2951 
   2952   erel = output_reldata->hdr->contents;
   2953   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   2954   irela = internal_relocs;
   2955   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   2956 		      * bed->s->int_rels_per_ext_rel);
   2957   while (irela < irelaend)
   2958     {
   2959       (*swap_out) (output_bfd, irela, erel);
   2960       irela += bed->s->int_rels_per_ext_rel;
   2961       erel += input_rel_hdr->sh_entsize;
   2962     }
   2963 
   2964   /* Bump the counter, so that we know where to add the next set of
   2965      relocations.  */
   2966   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   2967 
   2968   return true;
   2969 }
   2970 
   2971 /* Make weak undefined symbols in PIE dynamic.  */
   2973 
   2974 bool
   2975 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   2976 				 struct elf_link_hash_entry *h)
   2977 {
   2978   if (bfd_link_pie (info)
   2979       && h->dynindx == -1
   2980       && h->root.type == bfd_link_hash_undefweak)
   2981     return bfd_elf_link_record_dynamic_symbol (info, h);
   2982 
   2983   return true;
   2984 }
   2985 
   2986 /* Fix up the flags for a symbol.  This handles various cases which
   2987    can only be fixed after all the input files are seen.  This is
   2988    currently called by both adjust_dynamic_symbol and
   2989    assign_sym_version, which is unnecessary but perhaps more robust in
   2990    the face of future changes.  */
   2991 
   2992 static bool
   2993 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   2994 			   struct elf_info_failed *eif)
   2995 {
   2996   const struct elf_backend_data *bed;
   2997 
   2998   /* If this symbol was mentioned in a non-ELF file, try to set
   2999      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   3000      permit a non-ELF file to correctly refer to a symbol defined in
   3001      an ELF dynamic object.  */
   3002   if (h->non_elf)
   3003     {
   3004       while (h->root.type == bfd_link_hash_indirect)
   3005 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3006 
   3007       if (h->root.type != bfd_link_hash_defined
   3008 	  && h->root.type != bfd_link_hash_defweak)
   3009 	{
   3010 	  h->ref_regular = 1;
   3011 	  h->ref_regular_nonweak = 1;
   3012 	}
   3013       else
   3014 	{
   3015 	  if (h->root.u.def.section->owner != NULL
   3016 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   3017 		  == bfd_target_elf_flavour))
   3018 	    {
   3019 	      h->ref_regular = 1;
   3020 	      h->ref_regular_nonweak = 1;
   3021 	    }
   3022 	  else
   3023 	    h->def_regular = 1;
   3024 	}
   3025 
   3026       if (h->dynindx == -1
   3027 	  && (h->def_dynamic
   3028 	      || h->ref_dynamic))
   3029 	{
   3030 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3031 	    {
   3032 	      eif->failed = true;
   3033 	      return false;
   3034 	    }
   3035 	}
   3036     }
   3037   else
   3038     {
   3039       /* Unfortunately, NON_ELF is only correct if the symbol
   3040 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   3041 	 was first seen in an ELF file, we're probably OK unless the
   3042 	 symbol was defined in a non-ELF file.  Catch that case here.
   3043 	 FIXME: We're still in trouble if the symbol was first seen in
   3044 	 a dynamic object, and then later in a non-ELF regular object.  */
   3045       if ((h->root.type == bfd_link_hash_defined
   3046 	   || h->root.type == bfd_link_hash_defweak)
   3047 	  && !h->def_regular
   3048 	  && (h->root.u.def.section->owner != NULL
   3049 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   3050 		 != bfd_target_elf_flavour)
   3051 	      : (bfd_is_abs_section (h->root.u.def.section)
   3052 		 && !h->def_dynamic)))
   3053 	h->def_regular = 1;
   3054     }
   3055 
   3056   /* Backend specific symbol fixup.  */
   3057   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   3058   if (bed->elf_backend_fixup_symbol
   3059       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   3060     return false;
   3061 
   3062   /* If this is a final link, and the symbol was defined as a common
   3063      symbol in a regular object file, and there was no definition in
   3064      any dynamic object, then the linker will have allocated space for
   3065      the symbol in a common section but the DEF_REGULAR
   3066      flag will not have been set.  */
   3067   if (h->root.type == bfd_link_hash_defined
   3068       && !h->def_regular
   3069       && h->ref_regular
   3070       && !h->def_dynamic
   3071       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   3072     h->def_regular = 1;
   3073 
   3074   /* Symbols defined in discarded sections shouldn't be dynamic.  */
   3075   if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
   3076     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3077 
   3078   /* If a weak undefined symbol has non-default visibility, we also
   3079      hide it from the dynamic linker.  */
   3080   else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   3081 	   && h->root.type == bfd_link_hash_undefweak)
   3082     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3083 
   3084   /* A hidden versioned symbol in executable should be forced local if
   3085      it is is locally defined, not referenced by shared library and not
   3086      exported.  */
   3087   else if (bfd_link_executable (eif->info)
   3088 	   && h->versioned == versioned_hidden
   3089 	   && !eif->info->export_dynamic
   3090 	   && !h->dynamic
   3091 	   && !h->ref_dynamic
   3092 	   && h->def_regular)
   3093     (*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3094 
   3095   /* If -Bsymbolic was used (which means to bind references to global
   3096      symbols to the definition within the shared object), and this
   3097      symbol was defined in a regular object, then it actually doesn't
   3098      need a PLT entry.  Likewise, if the symbol has non-default
   3099      visibility.  If the symbol has hidden or internal visibility, we
   3100      will force it local.  */
   3101   else if (h->needs_plt
   3102 	   && bfd_link_pic (eif->info)
   3103 	   && is_elf_hash_table (eif->info->hash)
   3104 	   && (SYMBOLIC_BIND (eif->info, h)
   3105 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   3106 	   && h->def_regular)
   3107     {
   3108       bool force_local;
   3109 
   3110       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   3111 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   3112       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   3113     }
   3114 
   3115   /* If this is a weak defined symbol in a dynamic object, and we know
   3116      the real definition in the dynamic object, copy interesting flags
   3117      over to the real definition.  */
   3118   if (h->is_weakalias)
   3119     {
   3120       struct elf_link_hash_entry *def = weakdef (h);
   3121 
   3122       /* If the real definition is defined by a regular object file,
   3123 	 don't do anything special.  See the longer description in
   3124 	 _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
   3125 	 bfd_link_hash_defined as it was when put on the alias list
   3126 	 then it must have originally been a versioned symbol (for
   3127 	 which a non-versioned indirect symbol is created) and later
   3128 	 a definition for the non-versioned symbol is found.  In that
   3129 	 case the indirection is flipped with the versioned symbol
   3130 	 becoming an indirect pointing at the non-versioned symbol.
   3131 	 Thus, not an alias any more.  */
   3132       if (def->def_regular
   3133 	  || def->root.type != bfd_link_hash_defined)
   3134 	{
   3135 	  h = def;
   3136 	  while ((h = h->u.alias) != def)
   3137 	    h->is_weakalias = 0;
   3138 	}
   3139       else
   3140 	{
   3141 	  while (h->root.type == bfd_link_hash_indirect)
   3142 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3143 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   3144 		      || h->root.type == bfd_link_hash_defweak);
   3145 	  BFD_ASSERT (def->def_dynamic);
   3146 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
   3147 	}
   3148     }
   3149 
   3150   return true;
   3151 }
   3152 
   3153 /* Make the backend pick a good value for a dynamic symbol.  This is
   3154    called via elf_link_hash_traverse, and also calls itself
   3155    recursively.  */
   3156 
   3157 static bool
   3158 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   3159 {
   3160   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   3161   struct elf_link_hash_table *htab;
   3162   const struct elf_backend_data *bed;
   3163 
   3164   if (! is_elf_hash_table (eif->info->hash))
   3165     return false;
   3166 
   3167   /* Ignore indirect symbols.  These are added by the versioning code.  */
   3168   if (h->root.type == bfd_link_hash_indirect)
   3169     return true;
   3170 
   3171   /* Fix the symbol flags.  */
   3172   if (! _bfd_elf_fix_symbol_flags (h, eif))
   3173     return false;
   3174 
   3175   htab = elf_hash_table (eif->info);
   3176   bed = get_elf_backend_data (htab->dynobj);
   3177 
   3178   if (h->root.type == bfd_link_hash_undefweak)
   3179     {
   3180       if (eif->info->dynamic_undefined_weak == 0)
   3181 	(*bed->elf_backend_hide_symbol) (eif->info, h, true);
   3182       else if (eif->info->dynamic_undefined_weak > 0
   3183 	       && h->ref_regular
   3184 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3185 	       && !bfd_hide_sym_by_version (eif->info->version_info,
   3186 					    h->root.root.string))
   3187 	{
   3188 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
   3189 	    {
   3190 	      eif->failed = true;
   3191 	      return false;
   3192 	    }
   3193 	}
   3194     }
   3195 
   3196   /* If this symbol does not require a PLT entry, and it is not
   3197      defined by a dynamic object, or is not referenced by a regular
   3198      object, ignore it.  We do have to handle a weak defined symbol,
   3199      even if no regular object refers to it, if we decided to add it
   3200      to the dynamic symbol table.  FIXME: Do we normally need to worry
   3201      about symbols which are defined by one dynamic object and
   3202      referenced by another one?  */
   3203   if (!h->needs_plt
   3204       && h->type != STT_GNU_IFUNC
   3205       && (h->def_regular
   3206 	  || !h->def_dynamic
   3207 	  || (!h->ref_regular
   3208 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
   3209     {
   3210       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   3211       return true;
   3212     }
   3213 
   3214   /* If we've already adjusted this symbol, don't do it again.  This
   3215      can happen via a recursive call.  */
   3216   if (h->dynamic_adjusted)
   3217     return true;
   3218 
   3219   /* Don't look at this symbol again.  Note that we must set this
   3220      after checking the above conditions, because we may look at a
   3221      symbol once, decide not to do anything, and then get called
   3222      recursively later after REF_REGULAR is set below.  */
   3223   h->dynamic_adjusted = 1;
   3224 
   3225   /* If this is a weak definition, and we know a real definition, and
   3226      the real symbol is not itself defined by a regular object file,
   3227      then get a good value for the real definition.  We handle the
   3228      real symbol first, for the convenience of the backend routine.
   3229 
   3230      Note that there is a confusing case here.  If the real definition
   3231      is defined by a regular object file, we don't get the real symbol
   3232      from the dynamic object, but we do get the weak symbol.  If the
   3233      processor backend uses a COPY reloc, then if some routine in the
   3234      dynamic object changes the real symbol, we will not see that
   3235      change in the corresponding weak symbol.  This is the way other
   3236      ELF linkers work as well, and seems to be a result of the shared
   3237      library model.
   3238 
   3239      I will clarify this issue.  Most SVR4 shared libraries define the
   3240      variable _timezone and define timezone as a weak synonym.  The
   3241      tzset call changes _timezone.  If you write
   3242        extern int timezone;
   3243        int _timezone = 5;
   3244        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   3245      you might expect that, since timezone is a synonym for _timezone,
   3246      the same number will print both times.  However, if the processor
   3247      backend uses a COPY reloc, then actually timezone will be copied
   3248      into your process image, and, since you define _timezone
   3249      yourself, _timezone will not.  Thus timezone and _timezone will
   3250      wind up at different memory locations.  The tzset call will set
   3251      _timezone, leaving timezone unchanged.  */
   3252 
   3253   if (h->is_weakalias)
   3254     {
   3255       struct elf_link_hash_entry *def = weakdef (h);
   3256 
   3257       /* If we get to this point, there is an implicit reference to
   3258 	 the alias by a regular object file via the weak symbol H.  */
   3259       def->ref_regular = 1;
   3260 
   3261       /* Ensure that the backend adjust_dynamic_symbol function sees
   3262 	 the strong alias before H by recursively calling ourselves.  */
   3263       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
   3264 	return false;
   3265     }
   3266 
   3267   /* If a symbol has no type and no size and does not require a PLT
   3268      entry, then we are probably about to do the wrong thing here: we
   3269      are probably going to create a COPY reloc for an empty object.
   3270      This case can arise when a shared object is built with assembly
   3271      code, and the assembly code fails to set the symbol type.  */
   3272   if (h->size == 0
   3273       && h->type == STT_NOTYPE
   3274       && !h->needs_plt)
   3275     _bfd_error_handler
   3276       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   3277        h->root.root.string);
   3278 
   3279   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   3280     {
   3281       eif->failed = true;
   3282       return false;
   3283     }
   3284 
   3285   return true;
   3286 }
   3287 
   3288 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   3289    DYNBSS.  */
   3290 
   3291 bool
   3292 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   3293 			      struct elf_link_hash_entry *h,
   3294 			      asection *dynbss)
   3295 {
   3296   unsigned int power_of_two;
   3297   bfd_vma mask;
   3298   asection *sec = h->root.u.def.section;
   3299 
   3300   /* The section alignment of the definition is the maximum alignment
   3301      requirement of symbols defined in the section.  Since we don't
   3302      know the symbol alignment requirement, we start with the
   3303      maximum alignment and check low bits of the symbol address
   3304      for the minimum alignment.  */
   3305   power_of_two = bfd_section_alignment (sec);
   3306   mask = ((bfd_vma) 1 << power_of_two) - 1;
   3307   while ((h->root.u.def.value & mask) != 0)
   3308     {
   3309        mask >>= 1;
   3310        --power_of_two;
   3311     }
   3312 
   3313   if (power_of_two > bfd_section_alignment (dynbss))
   3314     {
   3315       /* Adjust the section alignment if needed.  */
   3316       if (!bfd_set_section_alignment (dynbss, power_of_two))
   3317 	return false;
   3318     }
   3319 
   3320   /* We make sure that the symbol will be aligned properly.  */
   3321   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   3322 
   3323   /* Define the symbol as being at this point in DYNBSS.  */
   3324   h->root.u.def.section = dynbss;
   3325   h->root.u.def.value = dynbss->size;
   3326 
   3327   /* Increment the size of DYNBSS to make room for the symbol.  */
   3328   dynbss->size += h->size;
   3329 
   3330   /* No error if extern_protected_data is true.  */
   3331   if (h->protected_def
   3332       && (!info->extern_protected_data
   3333 	  || (info->extern_protected_data < 0
   3334 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   3335     info->callbacks->einfo
   3336       (_("%P: copy reloc against protected `%pT' is dangerous\n"),
   3337        h->root.root.string);
   3338 
   3339   return true;
   3340 }
   3341 
   3342 /* Adjust all external symbols pointing into SEC_MERGE sections
   3343    to reflect the object merging within the sections.  */
   3344 
   3345 static bool
   3346 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   3347 {
   3348   asection *sec;
   3349 
   3350   if ((h->root.type == bfd_link_hash_defined
   3351        || h->root.type == bfd_link_hash_defweak)
   3352       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   3353       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3354     {
   3355       bfd *output_bfd = (bfd *) data;
   3356 
   3357       h->root.u.def.value =
   3358 	_bfd_merged_section_offset (output_bfd,
   3359 				    &h->root.u.def.section,
   3360 				    elf_section_data (sec)->sec_info,
   3361 				    h->root.u.def.value);
   3362     }
   3363 
   3364   return true;
   3365 }
   3366 
   3367 /* Returns false if the symbol referred to by H should be considered
   3368    to resolve local to the current module, and true if it should be
   3369    considered to bind dynamically.  */
   3370 
   3371 bool
   3372 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   3373 			   struct bfd_link_info *info,
   3374 			   bool not_local_protected)
   3375 {
   3376   bool binding_stays_local_p;
   3377   const struct elf_backend_data *bed;
   3378   struct elf_link_hash_table *hash_table;
   3379 
   3380   if (h == NULL)
   3381     return false;
   3382 
   3383   while (h->root.type == bfd_link_hash_indirect
   3384 	 || h->root.type == bfd_link_hash_warning)
   3385     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3386 
   3387   /* If it was forced local, then clearly it's not dynamic.  */
   3388   if (h->dynindx == -1)
   3389     return false;
   3390   if (h->forced_local)
   3391     return false;
   3392 
   3393   /* Identify the cases where name binding rules say that a
   3394      visible symbol resolves locally.  */
   3395   binding_stays_local_p = (bfd_link_executable (info)
   3396 			   || SYMBOLIC_BIND (info, h));
   3397 
   3398   switch (ELF_ST_VISIBILITY (h->other))
   3399     {
   3400     case STV_INTERNAL:
   3401     case STV_HIDDEN:
   3402       return false;
   3403 
   3404     case STV_PROTECTED:
   3405       hash_table = elf_hash_table (info);
   3406       if (!is_elf_hash_table (&hash_table->root))
   3407 	return false;
   3408 
   3409       bed = get_elf_backend_data (hash_table->dynobj);
   3410 
   3411       /* Proper resolution for function pointer equality may require
   3412 	 that these symbols perhaps be resolved dynamically, even though
   3413 	 we should be resolving them to the current module.  */
   3414       if (!not_local_protected || !bed->is_function_type (h->type))
   3415 	binding_stays_local_p = true;
   3416       break;
   3417 
   3418     default:
   3419       break;
   3420     }
   3421 
   3422   /* If it isn't defined locally, then clearly it's dynamic.  */
   3423   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   3424     return true;
   3425 
   3426   /* Otherwise, the symbol is dynamic if binding rules don't tell
   3427      us that it remains local.  */
   3428   return !binding_stays_local_p;
   3429 }
   3430 
   3431 /* Return true if the symbol referred to by H should be considered
   3432    to resolve local to the current module, and false otherwise.  Differs
   3433    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   3434    undefined symbols.  The two functions are virtually identical except
   3435    for the place where dynindx == -1 is tested.  If that test is true,
   3436    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
   3437    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
   3438    defined symbols.
   3439    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   3440    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   3441    treatment of undefined weak symbols.  For those that do not make
   3442    undefined weak symbols dynamic, both functions may return false.  */
   3443 
   3444 bool
   3445 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   3446 			      struct bfd_link_info *info,
   3447 			      bool local_protected)
   3448 {
   3449   const struct elf_backend_data *bed;
   3450   struct elf_link_hash_table *hash_table;
   3451 
   3452   /* If it's a local sym, of course we resolve locally.  */
   3453   if (h == NULL)
   3454     return true;
   3455 
   3456   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   3457   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   3458       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   3459     return true;
   3460 
   3461   /* Forced local symbols resolve locally.  */
   3462   if (h->forced_local)
   3463     return true;
   3464 
   3465   /* Common symbols that become definitions don't get the DEF_REGULAR
   3466      flag set, so test it first, and don't bail out.  */
   3467   if (ELF_COMMON_DEF_P (h))
   3468     /* Do nothing.  */;
   3469   /* If we don't have a definition in a regular file, then we can't
   3470      resolve locally.  The sym is either undefined or dynamic.  */
   3471   else if (!h->def_regular)
   3472     return false;
   3473 
   3474   /* Non-dynamic symbols resolve locally.  */
   3475   if (h->dynindx == -1)
   3476     return true;
   3477 
   3478   /* At this point, we know the symbol is defined and dynamic.  In an
   3479      executable it must resolve locally, likewise when building symbolic
   3480      shared libraries.  */
   3481   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3482     return true;
   3483 
   3484   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3485      with default visibility might not resolve locally.  */
   3486   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3487     return false;
   3488 
   3489   hash_table = elf_hash_table (info);
   3490   if (!is_elf_hash_table (&hash_table->root))
   3491     return true;
   3492 
   3493   /* STV_PROTECTED symbols with indirect external access are local. */
   3494   if (info->indirect_extern_access > 0)
   3495     return true;
   3496 
   3497   bed = get_elf_backend_data (hash_table->dynobj);
   3498 
   3499   /* If extern_protected_data is false, STV_PROTECTED non-function
   3500      symbols are local.  */
   3501   if ((!info->extern_protected_data
   3502        || (info->extern_protected_data < 0
   3503 	   && !bed->extern_protected_data))
   3504       && !bed->is_function_type (h->type))
   3505     return true;
   3506 
   3507   /* Function pointer equality tests may require that STV_PROTECTED
   3508      symbols be treated as dynamic symbols.  If the address of a
   3509      function not defined in an executable is set to that function's
   3510      plt entry in the executable, then the address of the function in
   3511      a shared library must also be the plt entry in the executable.  */
   3512   return local_protected;
   3513 }
   3514 
   3515 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3516    aligned.  Returns the first TLS output section.  */
   3517 
   3518 struct bfd_section *
   3519 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3520 {
   3521   struct bfd_section *sec, *tls;
   3522   unsigned int align = 0;
   3523 
   3524   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3525     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3526       break;
   3527   tls = sec;
   3528 
   3529   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3530     if (sec->alignment_power > align)
   3531       align = sec->alignment_power;
   3532 
   3533   elf_hash_table (info)->tls_sec = tls;
   3534 
   3535   /* Ensure the alignment of the first section (usually .tdata) is the largest
   3536      alignment, so that the tls segment starts aligned.  */
   3537   if (tls != NULL)
   3538     tls->alignment_power = align;
   3539 
   3540   return tls;
   3541 }
   3542 
   3543 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3544 static bool
   3545 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3546 				  Elf_Internal_Sym *sym)
   3547 {
   3548   const struct elf_backend_data *bed;
   3549 
   3550   /* Local symbols do not count, but target specific ones might.  */
   3551   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3552       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3553     return false;
   3554 
   3555   bed = get_elf_backend_data (abfd);
   3556   /* Function symbols do not count.  */
   3557   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3558     return false;
   3559 
   3560   /* If the section is undefined, then so is the symbol.  */
   3561   if (sym->st_shndx == SHN_UNDEF)
   3562     return false;
   3563 
   3564   /* If the symbol is defined in the common section, then
   3565      it is a common definition and so does not count.  */
   3566   if (bed->common_definition (sym))
   3567     return false;
   3568 
   3569   /* If the symbol is in a target specific section then we
   3570      must rely upon the backend to tell us what it is.  */
   3571   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3572     /* FIXME - this function is not coded yet:
   3573 
   3574        return _bfd_is_global_symbol_definition (abfd, sym);
   3575 
   3576        Instead for now assume that the definition is not global,
   3577        Even if this is wrong, at least the linker will behave
   3578        in the same way that it used to do.  */
   3579     return false;
   3580 
   3581   return true;
   3582 }
   3583 
   3584 /* Search the symbol table of the archive element of the archive ABFD
   3585    whose archive map contains a mention of SYMDEF, and determine if
   3586    the symbol is defined in this element.  */
   3587 static bool
   3588 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3589 {
   3590   Elf_Internal_Shdr * hdr;
   3591   size_t symcount;
   3592   size_t extsymcount;
   3593   size_t extsymoff;
   3594   Elf_Internal_Sym *isymbuf;
   3595   Elf_Internal_Sym *isym;
   3596   Elf_Internal_Sym *isymend;
   3597   bool result;
   3598 
   3599   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
   3600   if (abfd == NULL)
   3601     return false;
   3602 
   3603   if (! bfd_check_format (abfd, bfd_object))
   3604     return false;
   3605 
   3606   /* Select the appropriate symbol table.  If we don't know if the
   3607      object file is an IR object, give linker LTO plugin a chance to
   3608      get the correct symbol table.  */
   3609   if (abfd->plugin_format == bfd_plugin_yes
   3610 #if BFD_SUPPORTS_PLUGINS
   3611       || (abfd->plugin_format == bfd_plugin_unknown
   3612 	  && bfd_link_plugin_object_p (abfd))
   3613 #endif
   3614       )
   3615     {
   3616       /* Use the IR symbol table if the object has been claimed by
   3617 	 plugin.  */
   3618       abfd = abfd->plugin_dummy_bfd;
   3619       hdr = &elf_tdata (abfd)->symtab_hdr;
   3620     }
   3621   else
   3622     {
   3623       if (elf_use_dt_symtab_p (abfd))
   3624 	{
   3625 	  bfd_set_error (bfd_error_wrong_format);
   3626 	  return false;
   3627 	}
   3628 
   3629       if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3630 	hdr = &elf_tdata (abfd)->symtab_hdr;
   3631       else
   3632 	hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3633     }
   3634 
   3635   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3636 
   3637   /* The sh_info field of the symtab header tells us where the
   3638      external symbols start.  We don't care about the local symbols.  */
   3639   if (elf_bad_symtab (abfd))
   3640     {
   3641       extsymcount = symcount;
   3642       extsymoff = 0;
   3643     }
   3644   else
   3645     {
   3646       extsymcount = symcount - hdr->sh_info;
   3647       extsymoff = hdr->sh_info;
   3648     }
   3649 
   3650   if (extsymcount == 0)
   3651     return false;
   3652 
   3653   /* Read in the symbol table.  */
   3654   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3655 				  NULL, NULL, NULL);
   3656   if (isymbuf == NULL)
   3657     return false;
   3658 
   3659   /* Scan the symbol table looking for SYMDEF.  */
   3660   result = false;
   3661   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3662     {
   3663       const char *name;
   3664 
   3665       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3666 					      isym->st_name);
   3667       if (name == NULL)
   3668 	break;
   3669 
   3670       if (strcmp (name, symdef->name) == 0)
   3671 	{
   3672 	  result = is_global_data_symbol_definition (abfd, isym);
   3673 	  break;
   3674 	}
   3675     }
   3676 
   3677   free (isymbuf);
   3678 
   3679   return result;
   3680 }
   3681 
   3682 /* Add an entry to the .dynamic table.  */
   3684 
   3685 bool
   3686 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3687 			    bfd_vma tag,
   3688 			    bfd_vma val)
   3689 {
   3690   struct elf_link_hash_table *hash_table;
   3691   const struct elf_backend_data *bed;
   3692   asection *s;
   3693   bfd_size_type newsize;
   3694   bfd_byte *newcontents;
   3695   Elf_Internal_Dyn dyn;
   3696 
   3697   hash_table = elf_hash_table (info);
   3698   if (! is_elf_hash_table (&hash_table->root))
   3699     return false;
   3700 
   3701   if (tag == DT_RELA || tag == DT_REL)
   3702     hash_table->dynamic_relocs = true;
   3703 
   3704   bed = get_elf_backend_data (hash_table->dynobj);
   3705   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3706   BFD_ASSERT (s != NULL);
   3707 
   3708   newsize = s->size + bed->s->sizeof_dyn;
   3709   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3710   if (newcontents == NULL)
   3711     return false;
   3712 
   3713   dyn.d_tag = tag;
   3714   dyn.d_un.d_val = val;
   3715   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3716 
   3717   s->size = newsize;
   3718   s->contents = newcontents;
   3719 
   3720   return true;
   3721 }
   3722 
   3723 /* Strip zero-sized dynamic sections.  */
   3724 
   3725 bool
   3726 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
   3727 {
   3728   struct elf_link_hash_table *hash_table;
   3729   const struct elf_backend_data *bed;
   3730   asection *s, *sdynamic, **pp;
   3731   asection *rela_dyn, *rel_dyn;
   3732   Elf_Internal_Dyn dyn;
   3733   bfd_byte *extdyn, *next;
   3734   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3735   bool strip_zero_sized;
   3736   bool strip_zero_sized_plt;
   3737 
   3738   if (bfd_link_relocatable (info))
   3739     return true;
   3740 
   3741   hash_table = elf_hash_table (info);
   3742   if (!is_elf_hash_table (&hash_table->root))
   3743     return false;
   3744 
   3745   if (!hash_table->dynobj)
   3746     return true;
   3747 
   3748   sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3749   if (!sdynamic)
   3750     return true;
   3751 
   3752   bed = get_elf_backend_data (hash_table->dynobj);
   3753   swap_dyn_in = bed->s->swap_dyn_in;
   3754 
   3755   strip_zero_sized = false;
   3756   strip_zero_sized_plt = false;
   3757 
   3758   /* Strip zero-sized dynamic sections.  */
   3759   rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
   3760   rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
   3761   for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
   3762     if (s->size == 0
   3763 	&& (s == rela_dyn
   3764 	    || s == rel_dyn
   3765 	    || s == hash_table->srelplt->output_section
   3766 	    || s == hash_table->splt->output_section))
   3767       {
   3768 	*pp = s->next;
   3769 	info->output_bfd->section_count--;
   3770 	strip_zero_sized = true;
   3771 	if (s == rela_dyn)
   3772 	  s = rela_dyn;
   3773 	if (s == rel_dyn)
   3774 	  s = rel_dyn;
   3775 	else if (s == hash_table->splt->output_section)
   3776 	  {
   3777 	    s = hash_table->splt;
   3778 	    strip_zero_sized_plt = true;
   3779 	  }
   3780 	else
   3781 	  s = hash_table->srelplt;
   3782 	s->flags |= SEC_EXCLUDE;
   3783 	s->output_section = bfd_abs_section_ptr;
   3784       }
   3785     else
   3786       pp = &s->next;
   3787 
   3788   if (strip_zero_sized_plt && sdynamic->size != 0)
   3789     for (extdyn = sdynamic->contents;
   3790 	 extdyn < sdynamic->contents + sdynamic->size;
   3791 	 extdyn = next)
   3792       {
   3793 	next = extdyn + bed->s->sizeof_dyn;
   3794 	swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3795 	switch (dyn.d_tag)
   3796 	  {
   3797 	  default:
   3798 	    break;
   3799 	  case DT_JMPREL:
   3800 	  case DT_PLTRELSZ:
   3801 	  case DT_PLTREL:
   3802 	    /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
   3803 	       the procedure linkage table (the .plt section) has been
   3804 	       removed.  */
   3805 	    memmove (extdyn, next,
   3806 		     sdynamic->size - (next - sdynamic->contents));
   3807 	    next = extdyn;
   3808 	  }
   3809       }
   3810 
   3811   if (strip_zero_sized)
   3812     {
   3813       /* Regenerate program headers.  */
   3814       elf_seg_map (info->output_bfd) = NULL;
   3815       return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
   3816 						NULL);
   3817     }
   3818 
   3819   return true;
   3820 }
   3821 
   3822 /* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
   3823    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3824 
   3825 int
   3826 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
   3827 {
   3828   struct elf_link_hash_table *hash_table;
   3829   size_t strindex;
   3830   const char *soname;
   3831 
   3832   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3833     return -1;
   3834 
   3835   hash_table = elf_hash_table (info);
   3836   soname = elf_dt_name (abfd);
   3837   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
   3838   if (strindex == (size_t) -1)
   3839     return -1;
   3840 
   3841   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3842     {
   3843       asection *sdyn;
   3844       const struct elf_backend_data *bed;
   3845       bfd_byte *extdyn;
   3846 
   3847       bed = get_elf_backend_data (hash_table->dynobj);
   3848       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3849       if (sdyn != NULL && sdyn->size != 0)
   3850 	for (extdyn = sdyn->contents;
   3851 	     extdyn < sdyn->contents + sdyn->size;
   3852 	     extdyn += bed->s->sizeof_dyn)
   3853 	  {
   3854 	    Elf_Internal_Dyn dyn;
   3855 
   3856 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3857 	    if (dyn.d_tag == DT_NEEDED
   3858 		&& dyn.d_un.d_val == strindex)
   3859 	      {
   3860 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3861 		return 1;
   3862 	      }
   3863 	  }
   3864     }
   3865 
   3866   if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3867     return -1;
   3868 
   3869   if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3870     return -1;
   3871 
   3872   return 0;
   3873 }
   3874 
   3875 /* Return true if SONAME is on the needed list between NEEDED and STOP
   3876    (or the end of list if STOP is NULL), and needed by a library that
   3877    will be loaded.  */
   3878 
   3879 static bool
   3880 on_needed_list (const char *soname,
   3881 		struct bfd_link_needed_list *needed,
   3882 		struct bfd_link_needed_list *stop)
   3883 {
   3884   struct bfd_link_needed_list *look;
   3885   for (look = needed; look != stop; look = look->next)
   3886     if (strcmp (soname, look->name) == 0
   3887 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
   3888 	    /* If needed by a library that itself is not directly
   3889 	       needed, recursively check whether that library is
   3890 	       indirectly needed.  Since we add DT_NEEDED entries to
   3891 	       the end of the list, library dependencies appear after
   3892 	       the library.  Therefore search prior to the current
   3893 	       LOOK, preventing possible infinite recursion.  */
   3894 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
   3895       return true;
   3896 
   3897   return false;
   3898 }
   3899 
   3900 /* Sort symbol by value, section, size, and type.  */
   3901 static int
   3902 elf_sort_symbol (const void *arg1, const void *arg2)
   3903 {
   3904   const struct elf_link_hash_entry *h1;
   3905   const struct elf_link_hash_entry *h2;
   3906   bfd_signed_vma vdiff;
   3907   int sdiff;
   3908   const char *n1;
   3909   const char *n2;
   3910 
   3911   h1 = *(const struct elf_link_hash_entry **) arg1;
   3912   h2 = *(const struct elf_link_hash_entry **) arg2;
   3913   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3914   if (vdiff != 0)
   3915     return vdiff > 0 ? 1 : -1;
   3916 
   3917   sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3918   if (sdiff != 0)
   3919     return sdiff;
   3920 
   3921   /* Sort so that sized symbols are selected over zero size symbols.  */
   3922   vdiff = h1->size - h2->size;
   3923   if (vdiff != 0)
   3924     return vdiff > 0 ? 1 : -1;
   3925 
   3926   /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
   3927   if (h1->type != h2->type)
   3928     return h1->type - h2->type;
   3929 
   3930   /* If symbols are properly sized and typed, and multiple strong
   3931      aliases are not defined in a shared library by the user we
   3932      shouldn't get here.  Unfortunately linker script symbols like
   3933      __bss_start sometimes match a user symbol defined at the start of
   3934      .bss without proper size and type.  We'd like to preference the
   3935      user symbol over reserved system symbols.  Sort on leading
   3936      underscores.  */
   3937   n1 = h1->root.root.string;
   3938   n2 = h2->root.root.string;
   3939   while (*n1 == *n2)
   3940     {
   3941       if (*n1 == 0)
   3942 	break;
   3943       ++n1;
   3944       ++n2;
   3945     }
   3946   if (*n1 == '_')
   3947     return -1;
   3948   if (*n2 == '_')
   3949     return 1;
   3950 
   3951   /* Final sort on name selects user symbols like '_u' over reserved
   3952      system symbols like '_Z' and also will avoid qsort instability.  */
   3953   return *n1 - *n2;
   3954 }
   3955 
   3956 /* This function is used to adjust offsets into .dynstr for
   3957    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   3958 
   3959 static bool
   3960 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   3961 {
   3962   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   3963 
   3964   if (h->dynindx != -1)
   3965     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   3966   return true;
   3967 }
   3968 
   3969 /* Assign string offsets in .dynstr, update all structures referencing
   3970    them.  */
   3971 
   3972 static bool
   3973 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   3974 {
   3975   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   3976   struct elf_link_local_dynamic_entry *entry;
   3977   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   3978   bfd *dynobj = hash_table->dynobj;
   3979   asection *sdyn;
   3980   bfd_size_type size;
   3981   const struct elf_backend_data *bed;
   3982   bfd_byte *extdyn;
   3983 
   3984   _bfd_elf_strtab_finalize (dynstr);
   3985   size = _bfd_elf_strtab_size (dynstr);
   3986 
   3987   /* Allow the linker to examine the dynsymtab now it's fully populated.  */
   3988 
   3989   if (info->callbacks->examine_strtab)
   3990     info->callbacks->examine_strtab (dynstr);
   3991 
   3992   bed = get_elf_backend_data (dynobj);
   3993   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3994   BFD_ASSERT (sdyn != NULL);
   3995 
   3996   /* Update all .dynamic entries referencing .dynstr strings.  */
   3997   for (extdyn = sdyn->contents;
   3998        extdyn < PTR_ADD (sdyn->contents, sdyn->size);
   3999        extdyn += bed->s->sizeof_dyn)
   4000     {
   4001       Elf_Internal_Dyn dyn;
   4002 
   4003       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   4004       switch (dyn.d_tag)
   4005 	{
   4006 	case DT_STRSZ:
   4007 	  dyn.d_un.d_val = size;
   4008 	  break;
   4009 	case DT_NEEDED:
   4010 	case DT_SONAME:
   4011 	case DT_RPATH:
   4012 	case DT_RUNPATH:
   4013 	case DT_FILTER:
   4014 	case DT_AUXILIARY:
   4015 	case DT_AUDIT:
   4016 	case DT_DEPAUDIT:
   4017 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   4018 	  break;
   4019 	default:
   4020 	  continue;
   4021 	}
   4022       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   4023     }
   4024 
   4025   /* Now update local dynamic symbols.  */
   4026   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   4027     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   4028 						  entry->isym.st_name);
   4029 
   4030   /* And the rest of dynamic symbols.  */
   4031   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   4032 
   4033   /* Adjust version definitions.  */
   4034   if (elf_tdata (output_bfd)->cverdefs)
   4035     {
   4036       asection *s;
   4037       bfd_byte *p;
   4038       size_t i;
   4039       Elf_Internal_Verdef def;
   4040       Elf_Internal_Verdaux defaux;
   4041 
   4042       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   4043       p = s->contents;
   4044       do
   4045 	{
   4046 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   4047 				   &def);
   4048 	  p += sizeof (Elf_External_Verdef);
   4049 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   4050 	    continue;
   4051 	  for (i = 0; i < def.vd_cnt; ++i)
   4052 	    {
   4053 	      _bfd_elf_swap_verdaux_in (output_bfd,
   4054 					(Elf_External_Verdaux *) p, &defaux);
   4055 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   4056 							defaux.vda_name);
   4057 	      _bfd_elf_swap_verdaux_out (output_bfd,
   4058 					 &defaux, (Elf_External_Verdaux *) p);
   4059 	      p += sizeof (Elf_External_Verdaux);
   4060 	    }
   4061 	}
   4062       while (def.vd_next);
   4063     }
   4064 
   4065   /* Adjust version references.  */
   4066   if (elf_tdata (output_bfd)->verref)
   4067     {
   4068       asection *s;
   4069       bfd_byte *p;
   4070       size_t i;
   4071       Elf_Internal_Verneed need;
   4072       Elf_Internal_Vernaux needaux;
   4073 
   4074       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   4075       p = s->contents;
   4076       do
   4077 	{
   4078 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   4079 				    &need);
   4080 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   4081 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   4082 				     (Elf_External_Verneed *) p);
   4083 	  p += sizeof (Elf_External_Verneed);
   4084 	  for (i = 0; i < need.vn_cnt; ++i)
   4085 	    {
   4086 	      _bfd_elf_swap_vernaux_in (output_bfd,
   4087 					(Elf_External_Vernaux *) p, &needaux);
   4088 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   4089 							 needaux.vna_name);
   4090 	      _bfd_elf_swap_vernaux_out (output_bfd,
   4091 					 &needaux,
   4092 					 (Elf_External_Vernaux *) p);
   4093 	      p += sizeof (Elf_External_Vernaux);
   4094 	    }
   4095 	}
   4096       while (need.vn_next);
   4097     }
   4098 
   4099   return true;
   4100 }
   4101 
   4102 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4104    The default is to only match when the INPUT and OUTPUT are exactly
   4105    the same target.  */
   4106 
   4107 bool
   4108 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   4109 				    const bfd_target *output)
   4110 {
   4111   return input == output;
   4112 }
   4113 
   4114 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   4115    This version is used when different targets for the same architecture
   4116    are virtually identical.  */
   4117 
   4118 bool
   4119 _bfd_elf_relocs_compatible (const bfd_target *input,
   4120 			    const bfd_target *output)
   4121 {
   4122   const struct elf_backend_data *obed, *ibed;
   4123 
   4124   if (input == output)
   4125     return true;
   4126 
   4127   ibed = xvec_get_elf_backend_data (input);
   4128   obed = xvec_get_elf_backend_data (output);
   4129 
   4130   if (ibed->arch != obed->arch)
   4131     return false;
   4132 
   4133   /* If both backends are using this function, deem them compatible.  */
   4134   return ibed->relocs_compatible == obed->relocs_compatible;
   4135 }
   4136 
   4137 /* Make a special call to the linker "notice" function to tell it that
   4138    we are about to handle an as-needed lib, or have finished
   4139    processing the lib.  */
   4140 
   4141 bool
   4142 _bfd_elf_notice_as_needed (bfd *ibfd,
   4143 			   struct bfd_link_info *info,
   4144 			   enum notice_asneeded_action act)
   4145 {
   4146   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   4147 }
   4148 
   4149 /* Call ACTION on each relocation in an ELF object file.  */
   4150 
   4151 bool
   4152 _bfd_elf_link_iterate_on_relocs
   4153   (bfd *abfd, struct bfd_link_info *info,
   4154    bool (*action) (bfd *, struct bfd_link_info *, asection *,
   4155 		   const Elf_Internal_Rela *))
   4156 {
   4157   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4158   struct elf_link_hash_table *htab = elf_hash_table (info);
   4159 
   4160   /* If this object is the same format as the output object, and it is
   4161      not a shared library, then let the backend look through the
   4162      relocs.
   4163 
   4164      This is required to build global offset table entries and to
   4165      arrange for dynamic relocs.  It is not required for the
   4166      particular common case of linking non PIC code, even when linking
   4167      against shared libraries, but unfortunately there is no way of
   4168      knowing whether an object file has been compiled PIC or not.
   4169      Looking through the relocs is not particularly time consuming.
   4170      The problem is that we must either (1) keep the relocs in memory,
   4171      which causes the linker to require additional runtime memory or
   4172      (2) read the relocs twice from the input file, which wastes time.
   4173      This would be a good case for using mmap.
   4174 
   4175      I have no idea how to handle linking PIC code into a file of a
   4176      different format.  It probably can't be done.  */
   4177   if ((abfd->flags & DYNAMIC) == 0
   4178       && is_elf_hash_table (&htab->root)
   4179       && elf_object_id (abfd) == elf_hash_table_id (htab)
   4180       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   4181     {
   4182       asection *o;
   4183 
   4184       for (o = abfd->sections; o != NULL; o = o->next)
   4185 	{
   4186 	  Elf_Internal_Rela *internal_relocs;
   4187 	  bool ok;
   4188 
   4189 	  /* Don't check relocations in excluded sections.  Don't do
   4190 	     anything special with non-loaded, non-alloced sections.
   4191 	     In particular, any relocs in such sections should not
   4192 	     affect GOT and PLT reference counting (ie.  we don't
   4193 	     allow them to create GOT or PLT entries), there's no
   4194 	     possibility or desire to optimize TLS relocs, and
   4195 	     there's not much point in propagating relocs to shared
   4196 	     libs that the dynamic linker won't relocate.  */
   4197 	  if ((o->flags & SEC_ALLOC) == 0
   4198 	      || (o->flags & SEC_RELOC) == 0
   4199 	      || (o->flags & SEC_EXCLUDE) != 0
   4200 	      || o->reloc_count == 0
   4201 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   4202 		  && (o->flags & SEC_DEBUGGING) != 0)
   4203 	      || bfd_is_abs_section (o->output_section))
   4204 	    continue;
   4205 
   4206 	  internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
   4207 							    o, NULL,
   4208 							    NULL,
   4209 							    _bfd_link_keep_memory (info));
   4210 	  if (internal_relocs == NULL)
   4211 	    return false;
   4212 
   4213 	  ok = action (abfd, info, o, internal_relocs);
   4214 
   4215 	  if (elf_section_data (o)->relocs != internal_relocs)
   4216 	    free (internal_relocs);
   4217 
   4218 	  if (! ok)
   4219 	    return false;
   4220 	}
   4221     }
   4222 
   4223   return true;
   4224 }
   4225 
   4226 /* Check relocations in an ELF object file.  This is called after
   4227    all input files have been opened.  */
   4228 
   4229 bool
   4230 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   4231 {
   4232   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4233   if (bed->check_relocs != NULL)
   4234     return _bfd_elf_link_iterate_on_relocs (abfd, info,
   4235 					    bed->check_relocs);
   4236   return true;
   4237 }
   4238 
   4239 /* Add symbols from an ELF object file to the linker hash table.  */
   4240 
   4241 static bool
   4242 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   4243 {
   4244   Elf_Internal_Ehdr *ehdr;
   4245   Elf_Internal_Shdr *hdr;
   4246   size_t symcount;
   4247   size_t extsymcount;
   4248   size_t extsymoff;
   4249   struct elf_link_hash_entry **sym_hash;
   4250   bool dynamic;
   4251   Elf_External_Versym *extversym = NULL;
   4252   Elf_External_Versym *extversym_end = NULL;
   4253   Elf_External_Versym *ever;
   4254   struct elf_link_hash_entry *weaks;
   4255   struct elf_link_hash_entry **nondeflt_vers = NULL;
   4256   size_t nondeflt_vers_cnt = 0;
   4257   Elf_Internal_Sym *isymbuf = NULL;
   4258   Elf_Internal_Sym *isym;
   4259   Elf_Internal_Sym *isymend;
   4260   const struct elf_backend_data *bed;
   4261   bool add_needed;
   4262   struct elf_link_hash_table *htab;
   4263   void *alloc_mark = NULL;
   4264   struct bfd_hash_entry **old_table = NULL;
   4265   unsigned int old_size = 0;
   4266   unsigned int old_count = 0;
   4267   void *old_tab = NULL;
   4268   void *old_ent;
   4269   struct bfd_link_hash_entry *old_undefs = NULL;
   4270   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   4271   void *old_strtab = NULL;
   4272   size_t tabsize = 0;
   4273   asection *s;
   4274   bool just_syms;
   4275 
   4276   htab = elf_hash_table (info);
   4277   bed = get_elf_backend_data (abfd);
   4278 
   4279   if (elf_use_dt_symtab_p (abfd))
   4280     {
   4281       bfd_set_error (bfd_error_wrong_format);
   4282       return false;
   4283     }
   4284 
   4285   if ((abfd->flags & DYNAMIC) == 0)
   4286     dynamic = false;
   4287   else
   4288     {
   4289       dynamic = true;
   4290 
   4291       /* You can't use -r against a dynamic object.  Also, there's no
   4292 	 hope of using a dynamic object which does not exactly match
   4293 	 the format of the output file.  */
   4294       if (bfd_link_relocatable (info)
   4295 	  || !is_elf_hash_table (&htab->root)
   4296 	  || info->output_bfd->xvec != abfd->xvec)
   4297 	{
   4298 	  if (bfd_link_relocatable (info))
   4299 	    bfd_set_error (bfd_error_invalid_operation);
   4300 	  else
   4301 	    bfd_set_error (bfd_error_wrong_format);
   4302 	  goto error_return;
   4303 	}
   4304     }
   4305 
   4306   ehdr = elf_elfheader (abfd);
   4307   if (info->warn_alternate_em
   4308       && bed->elf_machine_code != ehdr->e_machine
   4309       && ((bed->elf_machine_alt1 != 0
   4310 	   && ehdr->e_machine == bed->elf_machine_alt1)
   4311 	  || (bed->elf_machine_alt2 != 0
   4312 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   4313     _bfd_error_handler
   4314       /* xgettext:c-format */
   4315       (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
   4316        ehdr->e_machine, abfd, bed->elf_machine_code);
   4317 
   4318   /* As a GNU extension, any input sections which are named
   4319      .gnu.warning.SYMBOL are treated as warning symbols for the given
   4320      symbol.  This differs from .gnu.warning sections, which generate
   4321      warnings when they are included in an output file.  */
   4322   /* PR 12761: Also generate this warning when building shared libraries.  */
   4323   for (s = abfd->sections; s != NULL; s = s->next)
   4324     {
   4325       const char *name;
   4326 
   4327       name = bfd_section_name (s);
   4328       if (startswith (name, ".gnu.warning."))
   4329 	{
   4330 	  char *msg;
   4331 	  bfd_size_type sz;
   4332 
   4333 	  name += sizeof ".gnu.warning." - 1;
   4334 
   4335 	  /* If this is a shared object, then look up the symbol
   4336 	     in the hash table.  If it is there, and it is already
   4337 	     been defined, then we will not be using the entry
   4338 	     from this shared object, so we don't need to warn.
   4339 	     FIXME: If we see the definition in a regular object
   4340 	     later on, we will warn, but we shouldn't.  The only
   4341 	     fix is to keep track of what warnings we are supposed
   4342 	     to emit, and then handle them all at the end of the
   4343 	     link.  */
   4344 	  if (dynamic)
   4345 	    {
   4346 	      struct elf_link_hash_entry *h;
   4347 
   4348 	      h = elf_link_hash_lookup (htab, name, false, false, true);
   4349 
   4350 	      /* FIXME: What about bfd_link_hash_common?  */
   4351 	      if (h != NULL
   4352 		  && (h->root.type == bfd_link_hash_defined
   4353 		      || h->root.type == bfd_link_hash_defweak))
   4354 		continue;
   4355 	    }
   4356 
   4357 	  sz = s->size;
   4358 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   4359 	  if (msg == NULL)
   4360 	    goto error_return;
   4361 
   4362 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   4363 	    goto error_return;
   4364 
   4365 	  msg[sz] = '\0';
   4366 
   4367 	  if (! (_bfd_generic_link_add_one_symbol
   4368 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   4369 		  false, bed->collect, NULL)))
   4370 	    goto error_return;
   4371 
   4372 	  if (bfd_link_executable (info))
   4373 	    {
   4374 	      /* Clobber the section size so that the warning does
   4375 		 not get copied into the output file.  */
   4376 	      s->size = 0;
   4377 
   4378 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   4379 		 the warning section don't get copied to the output.  */
   4380 	      s->flags |= SEC_EXCLUDE;
   4381 	    }
   4382 	}
   4383     }
   4384 
   4385   just_syms = ((s = abfd->sections) != NULL
   4386 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   4387 
   4388   add_needed = true;
   4389   if (! dynamic)
   4390     {
   4391       /* If we are creating a shared library, create all the dynamic
   4392 	 sections immediately.  We need to attach them to something,
   4393 	 so we attach them to this BFD, provided it is the right
   4394 	 format and is not from ld --just-symbols.  Always create the
   4395 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
   4396 	 are no input BFD's of the same format as the output, we can't
   4397 	 make a shared library.  */
   4398       if (!just_syms
   4399 	  && (bfd_link_pic (info)
   4400 	      || (!bfd_link_relocatable (info)
   4401 		  && info->nointerp
   4402 		  && (info->export_dynamic || info->dynamic)))
   4403 	  && is_elf_hash_table (&htab->root)
   4404 	  && info->output_bfd->xvec == abfd->xvec
   4405 	  && !htab->dynamic_sections_created)
   4406 	{
   4407 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   4408 	    goto error_return;
   4409 	}
   4410     }
   4411   else if (!is_elf_hash_table (&htab->root))
   4412     goto error_return;
   4413   else
   4414     {
   4415       const char *soname = NULL;
   4416       char *audit = NULL;
   4417       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   4418       const Elf_Internal_Phdr *phdr;
   4419       struct elf_link_loaded_list *loaded_lib;
   4420 
   4421       /* ld --just-symbols and dynamic objects don't mix very well.
   4422 	 ld shouldn't allow it.  */
   4423       if (just_syms)
   4424 	abort ();
   4425 
   4426       /* If this dynamic lib was specified on the command line with
   4427 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   4428 	 tag unless the lib is actually used.  Similary for libs brought
   4429 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   4430 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   4431 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   4432 	 all.  */
   4433       add_needed = (elf_dyn_lib_class (abfd)
   4434 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   4435 		       | DYN_NO_NEEDED)) == 0;
   4436 
   4437       s = bfd_get_section_by_name (abfd, ".dynamic");
   4438       if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
   4439 	{
   4440 	  bfd_byte *dynbuf;
   4441 	  bfd_byte *extdyn;
   4442 	  unsigned int elfsec;
   4443 	  unsigned long shlink;
   4444 
   4445 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   4446 	    {
   4447 	    error_free_dyn:
   4448 	      free (dynbuf);
   4449 	      goto error_return;
   4450 	    }
   4451 
   4452 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   4453 	  if (elfsec == SHN_BAD)
   4454 	    goto error_free_dyn;
   4455 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   4456 
   4457 	  for (extdyn = dynbuf;
   4458 	       (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
   4459 	       extdyn += bed->s->sizeof_dyn)
   4460 	    {
   4461 	      Elf_Internal_Dyn dyn;
   4462 
   4463 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   4464 	      if (dyn.d_tag == DT_SONAME)
   4465 		{
   4466 		  unsigned int tagv = dyn.d_un.d_val;
   4467 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4468 		  if (soname == NULL)
   4469 		    goto error_free_dyn;
   4470 		}
   4471 	      if (dyn.d_tag == DT_NEEDED)
   4472 		{
   4473 		  struct bfd_link_needed_list *n, **pn;
   4474 		  char *fnm, *anm;
   4475 		  unsigned int tagv = dyn.d_un.d_val;
   4476 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4477 
   4478 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4479 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4480 		  if (n == NULL || fnm == NULL)
   4481 		    goto error_free_dyn;
   4482 		  amt = strlen (fnm) + 1;
   4483 		  anm = (char *) bfd_alloc (abfd, amt);
   4484 		  if (anm == NULL)
   4485 		    goto error_free_dyn;
   4486 		  memcpy (anm, fnm, amt);
   4487 		  n->name = anm;
   4488 		  n->by = abfd;
   4489 		  n->next = NULL;
   4490 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   4491 		    ;
   4492 		  *pn = n;
   4493 		}
   4494 	      if (dyn.d_tag == DT_RUNPATH)
   4495 		{
   4496 		  struct bfd_link_needed_list *n, **pn;
   4497 		  char *fnm, *anm;
   4498 		  unsigned int tagv = dyn.d_un.d_val;
   4499 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4500 
   4501 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4502 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4503 		  if (n == NULL || fnm == NULL)
   4504 		    goto error_free_dyn;
   4505 		  amt = strlen (fnm) + 1;
   4506 		  anm = (char *) bfd_alloc (abfd, amt);
   4507 		  if (anm == NULL)
   4508 		    goto error_free_dyn;
   4509 		  memcpy (anm, fnm, amt);
   4510 		  n->name = anm;
   4511 		  n->by = abfd;
   4512 		  n->next = NULL;
   4513 		  for (pn = & runpath;
   4514 		       *pn != NULL;
   4515 		       pn = &(*pn)->next)
   4516 		    ;
   4517 		  *pn = n;
   4518 		}
   4519 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   4520 	      if (!runpath && dyn.d_tag == DT_RPATH)
   4521 		{
   4522 		  struct bfd_link_needed_list *n, **pn;
   4523 		  char *fnm, *anm;
   4524 		  unsigned int tagv = dyn.d_un.d_val;
   4525 		  size_t amt = sizeof (struct bfd_link_needed_list);
   4526 
   4527 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4528 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4529 		  if (n == NULL || fnm == NULL)
   4530 		    goto error_free_dyn;
   4531 		  amt = strlen (fnm) + 1;
   4532 		  anm = (char *) bfd_alloc (abfd, amt);
   4533 		  if (anm == NULL)
   4534 		    goto error_free_dyn;
   4535 		  memcpy (anm, fnm, amt);
   4536 		  n->name = anm;
   4537 		  n->by = abfd;
   4538 		  n->next = NULL;
   4539 		  for (pn = & rpath;
   4540 		       *pn != NULL;
   4541 		       pn = &(*pn)->next)
   4542 		    ;
   4543 		  *pn = n;
   4544 		}
   4545 	      if (dyn.d_tag == DT_AUDIT)
   4546 		{
   4547 		  unsigned int tagv = dyn.d_un.d_val;
   4548 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4549 		}
   4550 	      if (dyn.d_tag == DT_FLAGS_1)
   4551 		elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
   4552 	    }
   4553 
   4554 	  free (dynbuf);
   4555 	}
   4556 
   4557       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   4558 	 frees all more recently bfd_alloc'd blocks as well.  */
   4559       if (runpath)
   4560 	rpath = runpath;
   4561 
   4562       if (rpath)
   4563 	{
   4564 	  struct bfd_link_needed_list **pn;
   4565 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   4566 	    ;
   4567 	  *pn = rpath;
   4568 	}
   4569 
   4570       /* If we have a PT_GNU_RELRO program header, mark as read-only
   4571 	 all sections contained fully therein.  This makes relro
   4572 	 shared library sections appear as they will at run-time.  */
   4573       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
   4574       while (phdr-- > elf_tdata (abfd)->phdr)
   4575 	if (phdr->p_type == PT_GNU_RELRO)
   4576 	  {
   4577 	    for (s = abfd->sections; s != NULL; s = s->next)
   4578 	      {
   4579 		unsigned int opb = bfd_octets_per_byte (abfd, s);
   4580 
   4581 		if ((s->flags & SEC_ALLOC) != 0
   4582 		    && s->vma * opb >= phdr->p_vaddr
   4583 		    && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
   4584 		  s->flags |= SEC_READONLY;
   4585 	      }
   4586 	    break;
   4587 	  }
   4588 
   4589       /* We do not want to include any of the sections in a dynamic
   4590 	 object in the output file.  We hack by simply clobbering the
   4591 	 list of sections in the BFD.  This could be handled more
   4592 	 cleanly by, say, a new section flag; the existing
   4593 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   4594 	 still implies that the section takes up space in the output
   4595 	 file.  */
   4596       bfd_section_list_clear (abfd);
   4597 
   4598       /* Find the name to use in a DT_NEEDED entry that refers to this
   4599 	 object.  If the object has a DT_SONAME entry, we use it.
   4600 	 Otherwise, if the generic linker stuck something in
   4601 	 elf_dt_name, we use that.  Otherwise, we just use the file
   4602 	 name.  */
   4603       if (soname == NULL || *soname == '\0')
   4604 	{
   4605 	  soname = elf_dt_name (abfd);
   4606 	  if (soname == NULL || *soname == '\0')
   4607 	    soname = bfd_get_filename (abfd);
   4608 	}
   4609 
   4610       /* Save the SONAME because sometimes the linker emulation code
   4611 	 will need to know it.  */
   4612       elf_dt_name (abfd) = soname;
   4613 
   4614       /* If we have already included this dynamic object in the
   4615 	 link, just ignore it.  There is no reason to include a
   4616 	 particular dynamic object more than once.  */
   4617       for (loaded_lib = htab->dyn_loaded;
   4618 	   loaded_lib != NULL;
   4619 	   loaded_lib = loaded_lib->next)
   4620 	{
   4621 	  if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
   4622 	    return true;
   4623 	}
   4624 
   4625       /* Create dynamic sections for backends that require that be done
   4626 	 before setup_gnu_properties.  */
   4627       if (add_needed
   4628 	  && !_bfd_elf_link_create_dynamic_sections (abfd, info))
   4629 	return false;
   4630 
   4631       /* Save the DT_AUDIT entry for the linker emulation code. */
   4632       elf_dt_audit (abfd) = audit;
   4633     }
   4634 
   4635   /* If this is a dynamic object, we always link against the .dynsym
   4636      symbol table, not the .symtab symbol table.  The dynamic linker
   4637      will only see the .dynsym symbol table, so there is no reason to
   4638      look at .symtab for a dynamic object.  */
   4639 
   4640   if (! dynamic || elf_dynsymtab (abfd) == 0)
   4641     hdr = &elf_tdata (abfd)->symtab_hdr;
   4642   else
   4643     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   4644 
   4645   symcount = hdr->sh_size / bed->s->sizeof_sym;
   4646 
   4647   /* The sh_info field of the symtab header tells us where the
   4648      external symbols start.  We don't care about the local symbols at
   4649      this point.  */
   4650   if (elf_bad_symtab (abfd))
   4651     {
   4652       extsymcount = symcount;
   4653       extsymoff = 0;
   4654     }
   4655   else
   4656     {
   4657       extsymcount = symcount - hdr->sh_info;
   4658       extsymoff = hdr->sh_info;
   4659     }
   4660 
   4661   sym_hash = elf_sym_hashes (abfd);
   4662   if (extsymcount != 0)
   4663     {
   4664       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   4665 				      NULL, NULL, NULL);
   4666       if (isymbuf == NULL)
   4667 	goto error_return;
   4668 
   4669       if (sym_hash == NULL)
   4670 	{
   4671 	  /* We store a pointer to the hash table entry for each
   4672 	     external symbol.  */
   4673 	  size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   4674 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   4675 	  if (sym_hash == NULL)
   4676 	    goto error_free_sym;
   4677 	  elf_sym_hashes (abfd) = sym_hash;
   4678 	}
   4679     }
   4680 
   4681   if (dynamic)
   4682     {
   4683       /* Read in any version definitions.  */
   4684       if (!_bfd_elf_slurp_version_tables (abfd,
   4685 					  info->default_imported_symver))
   4686 	goto error_free_sym;
   4687 
   4688       /* Read in the symbol versions, but don't bother to convert them
   4689 	 to internal format.  */
   4690       if (elf_dynversym (abfd) != 0)
   4691 	{
   4692 	  Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   4693 	  bfd_size_type amt = versymhdr->sh_size;
   4694 
   4695 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
   4696 	    goto error_free_sym;
   4697 	  extversym = (Elf_External_Versym *)
   4698 	    _bfd_malloc_and_read (abfd, amt, amt);
   4699 	  if (extversym == NULL)
   4700 	    goto error_free_sym;
   4701 	  extversym_end = extversym + amt / sizeof (*extversym);
   4702 	}
   4703     }
   4704 
   4705   /* If we are loading an as-needed shared lib, save the symbol table
   4706      state before we start adding symbols.  If the lib turns out
   4707      to be unneeded, restore the state.  */
   4708   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4709     {
   4710       unsigned int i;
   4711       size_t entsize;
   4712 
   4713       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   4714 	{
   4715 	  struct bfd_hash_entry *p;
   4716 	  struct elf_link_hash_entry *h;
   4717 
   4718 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4719 	    {
   4720 	      h = (struct elf_link_hash_entry *) p;
   4721 	      entsize += htab->root.table.entsize;
   4722 	      if (h->root.type == bfd_link_hash_warning)
   4723 		{
   4724 		  entsize += htab->root.table.entsize;
   4725 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4726 		}
   4727 	      if (h->root.type == bfd_link_hash_common)
   4728 		entsize += sizeof (*h->root.u.c.p);
   4729 	    }
   4730 	}
   4731 
   4732       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   4733       old_tab = bfd_malloc (tabsize + entsize);
   4734       if (old_tab == NULL)
   4735 	goto error_free_vers;
   4736 
   4737       /* Remember the current objalloc pointer, so that all mem for
   4738 	 symbols added can later be reclaimed.  */
   4739       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   4740       if (alloc_mark == NULL)
   4741 	goto error_free_vers;
   4742 
   4743       /* Make a special call to the linker "notice" function to
   4744 	 tell it that we are about to handle an as-needed lib.  */
   4745       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   4746 	goto error_free_vers;
   4747 
   4748       /* Clone the symbol table.  Remember some pointers into the
   4749 	 symbol table, and dynamic symbol count.  */
   4750       old_ent = (char *) old_tab + tabsize;
   4751       memcpy (old_tab, htab->root.table.table, tabsize);
   4752       old_undefs = htab->root.undefs;
   4753       old_undefs_tail = htab->root.undefs_tail;
   4754       old_table = htab->root.table.table;
   4755       old_size = htab->root.table.size;
   4756       old_count = htab->root.table.count;
   4757       old_strtab = NULL;
   4758       if (htab->dynstr != NULL)
   4759 	{
   4760 	  old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   4761 	  if (old_strtab == NULL)
   4762 	    goto error_free_vers;
   4763 	}
   4764 
   4765       for (i = 0; i < htab->root.table.size; i++)
   4766 	{
   4767 	  struct bfd_hash_entry *p;
   4768 	  struct elf_link_hash_entry *h;
   4769 
   4770 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4771 	    {
   4772 	      h = (struct elf_link_hash_entry *) p;
   4773 	      memcpy (old_ent, h, htab->root.table.entsize);
   4774 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4775 	      if (h->root.type == bfd_link_hash_warning)
   4776 		{
   4777 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4778 		  memcpy (old_ent, h, htab->root.table.entsize);
   4779 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4780 		}
   4781 	      if (h->root.type == bfd_link_hash_common)
   4782 		{
   4783 		  memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
   4784 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   4785 		}
   4786 	    }
   4787 	}
   4788     }
   4789 
   4790   weaks = NULL;
   4791   if (extversym == NULL)
   4792     ever = NULL;
   4793   else if (extversym + extsymoff < extversym_end)
   4794     ever = extversym + extsymoff;
   4795   else
   4796     {
   4797       /* xgettext:c-format */
   4798       _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
   4799 			  abfd, (long) extsymoff,
   4800 			  (long) (extversym_end - extversym) / sizeof (* extversym));
   4801       bfd_set_error (bfd_error_bad_value);
   4802       goto error_free_vers;
   4803     }
   4804 
   4805   if (!bfd_link_relocatable (info)
   4806       && abfd->lto_slim_object)
   4807     {
   4808       _bfd_error_handler
   4809 	(_("%pB: plugin needed to handle lto object"), abfd);
   4810     }
   4811 
   4812   for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
   4813        isym < isymend;
   4814        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   4815     {
   4816       int bind;
   4817       bfd_vma value;
   4818       asection *sec, *new_sec;
   4819       flagword flags;
   4820       const char *name;
   4821       struct elf_link_hash_entry *h;
   4822       struct elf_link_hash_entry *hi;
   4823       bool definition;
   4824       bool size_change_ok;
   4825       bool type_change_ok;
   4826       bool new_weak;
   4827       bool old_weak;
   4828       bfd *override;
   4829       bool common;
   4830       bool discarded;
   4831       unsigned int old_alignment;
   4832       unsigned int shindex;
   4833       bfd *old_bfd;
   4834       bool matched;
   4835 
   4836       override = NULL;
   4837 
   4838       flags = BSF_NO_FLAGS;
   4839       sec = NULL;
   4840       value = isym->st_value;
   4841       common = bed->common_definition (isym);
   4842       if (common && info->inhibit_common_definition)
   4843 	{
   4844 	  /* Treat common symbol as undefined for --no-define-common.  */
   4845 	  isym->st_shndx = SHN_UNDEF;
   4846 	  common = false;
   4847 	}
   4848       discarded = false;
   4849 
   4850       bind = ELF_ST_BIND (isym->st_info);
   4851       switch (bind)
   4852 	{
   4853 	case STB_LOCAL:
   4854 	  /* This should be impossible, since ELF requires that all
   4855 	     global symbols follow all local symbols, and that sh_info
   4856 	     point to the first global symbol.  Unfortunately, Irix 5
   4857 	     screws this up.  */
   4858 	  if (elf_bad_symtab (abfd))
   4859 	    continue;
   4860 
   4861 	  /* If we aren't prepared to handle locals within the globals
   4862 	     then we'll likely segfault on a NULL symbol hash if the
   4863 	     symbol is ever referenced in relocations.  */
   4864 	  shindex = elf_elfheader (abfd)->e_shstrndx;
   4865 	  name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
   4866 	  _bfd_error_handler (_("%pB: %s local symbol at index %lu"
   4867 				" (>= sh_info of %lu)"),
   4868 			      abfd, name, (long) (isym - isymbuf + extsymoff),
   4869 			      (long) extsymoff);
   4870 
   4871 	  /* Dynamic object relocations are not processed by ld, so
   4872 	     ld won't run into the problem mentioned above.  */
   4873 	  if (dynamic)
   4874 	    continue;
   4875 	  bfd_set_error (bfd_error_bad_value);
   4876 	  goto error_free_vers;
   4877 
   4878 	case STB_GLOBAL:
   4879 	  if (isym->st_shndx != SHN_UNDEF && !common)
   4880 	    flags = BSF_GLOBAL;
   4881 	  break;
   4882 
   4883 	case STB_WEAK:
   4884 	  flags = BSF_WEAK;
   4885 	  break;
   4886 
   4887 	case STB_GNU_UNIQUE:
   4888 	  flags = BSF_GNU_UNIQUE;
   4889 	  break;
   4890 
   4891 	default:
   4892 	  /* Leave it up to the processor backend.  */
   4893 	  break;
   4894 	}
   4895 
   4896       if (isym->st_shndx == SHN_UNDEF)
   4897 	sec = bfd_und_section_ptr;
   4898       else if (isym->st_shndx == SHN_ABS)
   4899 	sec = bfd_abs_section_ptr;
   4900       else if (isym->st_shndx == SHN_COMMON)
   4901 	{
   4902 	  sec = bfd_com_section_ptr;
   4903 	  /* What ELF calls the size we call the value.  What ELF
   4904 	     calls the value we call the alignment.  */
   4905 	  value = isym->st_size;
   4906 	}
   4907       else
   4908 	{
   4909 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4910 	  if (sec == NULL)
   4911 	    sec = bfd_abs_section_ptr;
   4912 	  else if (discarded_section (sec))
   4913 	    {
   4914 	      /* Symbols from discarded section are undefined.  We keep
   4915 		 its visibility.  */
   4916 	      sec = bfd_und_section_ptr;
   4917 	      discarded = true;
   4918 	      isym->st_shndx = SHN_UNDEF;
   4919 	    }
   4920 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   4921 	    value -= sec->vma;
   4922 	}
   4923 
   4924       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   4925 					      isym->st_name);
   4926       if (name == NULL)
   4927 	goto error_free_vers;
   4928 
   4929       if (isym->st_shndx == SHN_COMMON
   4930 	  && (abfd->flags & BFD_PLUGIN) != 0)
   4931 	{
   4932 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   4933 
   4934 	  if (xc == NULL)
   4935 	    {
   4936 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   4937 				 | SEC_EXCLUDE);
   4938 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   4939 	      if (xc == NULL)
   4940 		goto error_free_vers;
   4941 	    }
   4942 	  sec = xc;
   4943 	}
   4944       else if (isym->st_shndx == SHN_COMMON
   4945 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   4946 	       && !bfd_link_relocatable (info))
   4947 	{
   4948 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   4949 
   4950 	  if (tcomm == NULL)
   4951 	    {
   4952 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   4953 				 | SEC_LINKER_CREATED);
   4954 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   4955 	      if (tcomm == NULL)
   4956 		goto error_free_vers;
   4957 	    }
   4958 	  sec = tcomm;
   4959 	}
   4960       else if (bed->elf_add_symbol_hook)
   4961 	{
   4962 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   4963 					     &sec, &value))
   4964 	    goto error_free_vers;
   4965 
   4966 	  /* The hook function sets the name to NULL if this symbol
   4967 	     should be skipped for some reason.  */
   4968 	  if (name == NULL)
   4969 	    continue;
   4970 	}
   4971 
   4972       /* Sanity check that all possibilities were handled.  */
   4973       if (sec == NULL)
   4974 	abort ();
   4975 
   4976       /* Silently discard TLS symbols from --just-syms.  There's
   4977 	 no way to combine a static TLS block with a new TLS block
   4978 	 for this executable.  */
   4979       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   4980 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   4981 	continue;
   4982 
   4983       if (bfd_is_und_section (sec)
   4984 	  || bfd_is_com_section (sec))
   4985 	definition = false;
   4986       else
   4987 	definition = true;
   4988 
   4989       size_change_ok = false;
   4990       type_change_ok = bed->type_change_ok;
   4991       old_weak = false;
   4992       matched = false;
   4993       old_alignment = 0;
   4994       old_bfd = NULL;
   4995       new_sec = sec;
   4996 
   4997       if (is_elf_hash_table (&htab->root))
   4998 	{
   4999 	  Elf_Internal_Versym iver;
   5000 	  unsigned int vernum = 0;
   5001 	  bool skip;
   5002 
   5003 	  if (ever == NULL)
   5004 	    {
   5005 	      if (info->default_imported_symver)
   5006 		/* Use the default symbol version created earlier.  */
   5007 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   5008 	      else
   5009 		iver.vs_vers = 0;
   5010 	    }
   5011 	  else if (ever >= extversym_end)
   5012 	    {
   5013 	      /* xgettext:c-format */
   5014 	      _bfd_error_handler (_("%pB: not enough version information"),
   5015 				  abfd);
   5016 	      bfd_set_error (bfd_error_bad_value);
   5017 	      goto error_free_vers;
   5018 	    }
   5019 	  else
   5020 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   5021 
   5022 	  vernum = iver.vs_vers & VERSYM_VERSION;
   5023 
   5024 	  /* If this is a hidden symbol, or if it is not version
   5025 	     1, we append the version name to the symbol name.
   5026 	     However, we do not modify a non-hidden absolute symbol
   5027 	     if it is not a function, because it might be the version
   5028 	     symbol itself.  FIXME: What if it isn't?  */
   5029 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   5030 	      || (vernum > 1
   5031 		  && (!bfd_is_abs_section (sec)
   5032 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   5033 	    {
   5034 	      const char *verstr;
   5035 	      size_t namelen, verlen, newlen;
   5036 	      char *newname, *p;
   5037 
   5038 	      if (isym->st_shndx != SHN_UNDEF)
   5039 		{
   5040 		  if (vernum > elf_tdata (abfd)->cverdefs)
   5041 		    verstr = NULL;
   5042 		  else if (vernum > 1)
   5043 		    verstr =
   5044 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   5045 		  else
   5046 		    verstr = "";
   5047 
   5048 		  if (verstr == NULL)
   5049 		    {
   5050 		      _bfd_error_handler
   5051 			/* xgettext:c-format */
   5052 			(_("%pB: %s: invalid version %u (max %d)"),
   5053 			 abfd, name, vernum,
   5054 			 elf_tdata (abfd)->cverdefs);
   5055 		      bfd_set_error (bfd_error_bad_value);
   5056 		      goto error_free_vers;
   5057 		    }
   5058 		}
   5059 	      else
   5060 		{
   5061 		  /* We cannot simply test for the number of
   5062 		     entries in the VERNEED section since the
   5063 		     numbers for the needed versions do not start
   5064 		     at 0.  */
   5065 		  Elf_Internal_Verneed *t;
   5066 
   5067 		  verstr = NULL;
   5068 		  for (t = elf_tdata (abfd)->verref;
   5069 		       t != NULL;
   5070 		       t = t->vn_nextref)
   5071 		    {
   5072 		      Elf_Internal_Vernaux *a;
   5073 
   5074 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   5075 			{
   5076 			  if (a->vna_other == vernum)
   5077 			    {
   5078 			      verstr = a->vna_nodename;
   5079 			      break;
   5080 			    }
   5081 			}
   5082 		      if (a != NULL)
   5083 			break;
   5084 		    }
   5085 		  if (verstr == NULL)
   5086 		    {
   5087 		      _bfd_error_handler
   5088 			/* xgettext:c-format */
   5089 			(_("%pB: %s: invalid needed version %d"),
   5090 			 abfd, name, vernum);
   5091 		      bfd_set_error (bfd_error_bad_value);
   5092 		      goto error_free_vers;
   5093 		    }
   5094 		}
   5095 
   5096 	      namelen = strlen (name);
   5097 	      verlen = strlen (verstr);
   5098 	      newlen = namelen + verlen + 2;
   5099 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5100 		  && isym->st_shndx != SHN_UNDEF)
   5101 		++newlen;
   5102 
   5103 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   5104 	      if (newname == NULL)
   5105 		goto error_free_vers;
   5106 	      memcpy (newname, name, namelen);
   5107 	      p = newname + namelen;
   5108 	      *p++ = ELF_VER_CHR;
   5109 	      /* If this is a defined non-hidden version symbol,
   5110 		 we add another @ to the name.  This indicates the
   5111 		 default version of the symbol.  */
   5112 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   5113 		  && isym->st_shndx != SHN_UNDEF)
   5114 		*p++ = ELF_VER_CHR;
   5115 	      memcpy (p, verstr, verlen + 1);
   5116 
   5117 	      name = newname;
   5118 	    }
   5119 
   5120 	  /* If this symbol has default visibility and the user has
   5121 	     requested we not re-export it, then mark it as hidden.  */
   5122 	  if (!bfd_is_und_section (sec)
   5123 	      && !dynamic
   5124 	      && abfd->no_export
   5125 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   5126 	    isym->st_other = (STV_HIDDEN
   5127 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   5128 
   5129 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   5130 				      sym_hash, &old_bfd, &old_weak,
   5131 				      &old_alignment, &skip, &override,
   5132 				      &type_change_ok, &size_change_ok,
   5133 				      &matched))
   5134 	    goto error_free_vers;
   5135 
   5136 	  if (skip)
   5137 	    continue;
   5138 
   5139 	  /* Override a definition only if the new symbol matches the
   5140 	     existing one.  */
   5141 	  if (override && matched)
   5142 	    definition = false;
   5143 
   5144 	  h = *sym_hash;
   5145 	  while (h->root.type == bfd_link_hash_indirect
   5146 		 || h->root.type == bfd_link_hash_warning)
   5147 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5148 
   5149 	  if (h->versioned != unversioned
   5150 	      && elf_tdata (abfd)->verdef != NULL
   5151 	      && vernum > 1
   5152 	      && definition)
   5153 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   5154 	}
   5155 
   5156       if (! (_bfd_generic_link_add_one_symbol
   5157 	     (info, override ? override : abfd, name, flags, sec, value,
   5158 	      NULL, false, bed->collect,
   5159 	      (struct bfd_link_hash_entry **) sym_hash)))
   5160 	goto error_free_vers;
   5161 
   5162       h = *sym_hash;
   5163       /* We need to make sure that indirect symbol dynamic flags are
   5164 	 updated.  */
   5165       hi = h;
   5166       while (h->root.type == bfd_link_hash_indirect
   5167 	     || h->root.type == bfd_link_hash_warning)
   5168 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5169 
   5170       *sym_hash = h;
   5171 
   5172       /* Setting the index to -3 tells elf_link_output_extsym that
   5173 	 this symbol is defined in a discarded section.  */
   5174       if (discarded && is_elf_hash_table (&htab->root))
   5175 	h->indx = -3;
   5176 
   5177       new_weak = (flags & BSF_WEAK) != 0;
   5178       if (dynamic
   5179 	  && definition
   5180 	  && new_weak
   5181 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   5182 	  && is_elf_hash_table (&htab->root)
   5183 	  && h->u.alias == NULL)
   5184 	{
   5185 	  /* Keep a list of all weak defined non function symbols from
   5186 	     a dynamic object, using the alias field.  Later in this
   5187 	     function we will set the alias field to the correct
   5188 	     value.  We only put non-function symbols from dynamic
   5189 	     objects on this list, because that happens to be the only
   5190 	     time we need to know the normal symbol corresponding to a
   5191 	     weak symbol, and the information is time consuming to
   5192 	     figure out.  If the alias field is not already NULL,
   5193 	     then this symbol was already defined by some previous
   5194 	     dynamic object, and we will be using that previous
   5195 	     definition anyhow.  */
   5196 
   5197 	  h->u.alias = weaks;
   5198 	  weaks = h;
   5199 	}
   5200 
   5201       /* Set the alignment of a common symbol.  */
   5202       if ((common || bfd_is_com_section (sec))
   5203 	  && h->root.type == bfd_link_hash_common)
   5204 	{
   5205 	  unsigned int align;
   5206 
   5207 	  if (common)
   5208 	    align = bfd_log2 (isym->st_value);
   5209 	  else
   5210 	    {
   5211 	      /* The new symbol is a common symbol in a shared object.
   5212 		 We need to get the alignment from the section.  */
   5213 	      align = new_sec->alignment_power;
   5214 	    }
   5215 	  if (align > old_alignment)
   5216 	    h->root.u.c.p->alignment_power = align;
   5217 	  else
   5218 	    h->root.u.c.p->alignment_power = old_alignment;
   5219 	}
   5220 
   5221       if (is_elf_hash_table (&htab->root))
   5222 	{
   5223 	  /* Set a flag in the hash table entry indicating the type of
   5224 	     reference or definition we just found.  A dynamic symbol
   5225 	     is one which is referenced or defined by both a regular
   5226 	     object and a shared object.  */
   5227 	  bool dynsym = false;
   5228 
   5229 	  /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
   5230 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5231 	    {
   5232 	      /* Except for this flag to track nonweak references.  */
   5233 	      if (!definition
   5234 		  && bind != STB_WEAK)
   5235 		h->ref_ir_nonweak = 1;
   5236 	    }
   5237 	  else if (!dynamic)
   5238 	    {
   5239 	      if (! definition)
   5240 		{
   5241 		  h->ref_regular = 1;
   5242 		  if (bind != STB_WEAK)
   5243 		    h->ref_regular_nonweak = 1;
   5244 		}
   5245 	      else
   5246 		{
   5247 		  h->def_regular = 1;
   5248 		  if (h->def_dynamic)
   5249 		    {
   5250 		      h->def_dynamic = 0;
   5251 		      h->ref_dynamic = 1;
   5252 		    }
   5253 		}
   5254 	    }
   5255 	  else
   5256 	    {
   5257 	      if (! definition)
   5258 		{
   5259 		  h->ref_dynamic = 1;
   5260 		  hi->ref_dynamic = 1;
   5261 		}
   5262 	      else
   5263 		{
   5264 		  h->def_dynamic = 1;
   5265 		  hi->def_dynamic = 1;
   5266 		}
   5267 	    }
   5268 
   5269 	  /* If an indirect symbol has been forced local, don't
   5270 	     make the real symbol dynamic.  */
   5271 	  if (h != hi && hi->forced_local)
   5272 	    ;
   5273 	  else if (!dynamic)
   5274 	    {
   5275 	      if (bfd_link_dll (info)
   5276 		  || h->def_dynamic
   5277 		  || h->ref_dynamic)
   5278 		dynsym = true;
   5279 	    }
   5280 	  else
   5281 	    {
   5282 	      if (h->def_regular
   5283 		  || h->ref_regular
   5284 		  || (h->is_weakalias
   5285 		      && weakdef (h)->dynindx != -1))
   5286 		dynsym = true;
   5287 	    }
   5288 
   5289 	  /* Check to see if we need to add an indirect symbol for
   5290 	     the default name.  */
   5291 	  if ((definition
   5292 	       || (!override && h->root.type == bfd_link_hash_common))
   5293 	      && !(hi != h
   5294 		   && hi->versioned == versioned_hidden))
   5295 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   5296 					      sec, value, &old_bfd, &dynsym))
   5297 	      goto error_free_vers;
   5298 
   5299 	  /* Check the alignment when a common symbol is involved. This
   5300 	     can change when a common symbol is overridden by a normal
   5301 	     definition or a common symbol is ignored due to the old
   5302 	     normal definition. We need to make sure the maximum
   5303 	     alignment is maintained.  */
   5304 	  if ((old_alignment || common)
   5305 	      && h->root.type != bfd_link_hash_common)
   5306 	    {
   5307 	      unsigned int common_align;
   5308 	      unsigned int normal_align;
   5309 	      unsigned int symbol_align;
   5310 	      bfd *normal_bfd;
   5311 	      bfd *common_bfd;
   5312 
   5313 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   5314 			  || h->root.type == bfd_link_hash_defweak);
   5315 
   5316 	      symbol_align = ffs (h->root.u.def.value) - 1;
   5317 	      if (h->root.u.def.section->owner != NULL
   5318 		  && (h->root.u.def.section->owner->flags
   5319 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
   5320 		{
   5321 		  normal_align = h->root.u.def.section->alignment_power;
   5322 		  if (normal_align > symbol_align)
   5323 		    normal_align = symbol_align;
   5324 		}
   5325 	      else
   5326 		normal_align = symbol_align;
   5327 
   5328 	      if (old_alignment)
   5329 		{
   5330 		  common_align = old_alignment;
   5331 		  common_bfd = old_bfd;
   5332 		  normal_bfd = abfd;
   5333 		}
   5334 	      else
   5335 		{
   5336 		  common_align = bfd_log2 (isym->st_value);
   5337 		  common_bfd = abfd;
   5338 		  normal_bfd = old_bfd;
   5339 		}
   5340 
   5341 	      if (normal_align < common_align)
   5342 		{
   5343 		  /* PR binutils/2735 */
   5344 		  if (normal_bfd == NULL)
   5345 		    _bfd_error_handler
   5346 		      /* xgettext:c-format */
   5347 		      (_("warning: alignment %u of common symbol `%s' in %pB is"
   5348 			 " greater than the alignment (%u) of its section %pA"),
   5349 		       1 << common_align, name, common_bfd,
   5350 		       1 << normal_align, h->root.u.def.section);
   5351 		  else
   5352 		    _bfd_error_handler
   5353 		      /* xgettext:c-format */
   5354 		      (_("warning: alignment %u of normal symbol `%s' in %pB"
   5355 			 " is smaller than %u used by the common definition in %pB"),
   5356 		       1 << normal_align, name, normal_bfd,
   5357 		       1 << common_align, common_bfd);
   5358 
   5359 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5360 		  _bfd_error_handler
   5361 		    (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
   5362 		}
   5363 	    }
   5364 
   5365 	  /* Remember the symbol size if it isn't undefined.  */
   5366 	  if (isym->st_size != 0
   5367 	      && isym->st_shndx != SHN_UNDEF
   5368 	      && (definition || h->size == 0))
   5369 	    {
   5370 	      if (h->size != 0
   5371 		  && h->size != isym->st_size
   5372 		  && ! size_change_ok)
   5373 		{
   5374 		  _bfd_error_handler
   5375 		    /* xgettext:c-format */
   5376 		    (_("warning: size of symbol `%s' changed"
   5377 		       " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
   5378 		     name, (uint64_t) h->size, old_bfd,
   5379 		     (uint64_t) isym->st_size, abfd);
   5380 
   5381 		  /* PR 30499: make sure that users understand that this warning is serious.  */
   5382 		  _bfd_error_handler
   5383 		    (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
   5384 		}
   5385 
   5386 	      h->size = isym->st_size;
   5387 	    }
   5388 
   5389 	  /* If this is a common symbol, then we always want H->SIZE
   5390 	     to be the size of the common symbol.  The code just above
   5391 	     won't fix the size if a common symbol becomes larger.  We
   5392 	     don't warn about a size change here, because that is
   5393 	     covered by --warn-common.  Allow changes between different
   5394 	     function types.  */
   5395 	  if (h->root.type == bfd_link_hash_common)
   5396 	    h->size = h->root.u.c.size;
   5397 
   5398 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   5399 	      && ((definition && !new_weak)
   5400 		  || (old_weak && h->root.type == bfd_link_hash_common)
   5401 		  || h->type == STT_NOTYPE))
   5402 	    {
   5403 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   5404 
   5405 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   5406 		 symbol.  */
   5407 	      if (type == STT_GNU_IFUNC
   5408 		  && (abfd->flags & DYNAMIC) != 0)
   5409 		type = STT_FUNC;
   5410 
   5411 	      if (h->type != type)
   5412 		{
   5413 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   5414 		    /* xgettext:c-format */
   5415 		    _bfd_error_handler
   5416 		      (_("warning: type of symbol `%s' changed"
   5417 			 " from %d to %d in %pB"),
   5418 		       name, h->type, type, abfd);
   5419 
   5420 		  h->type = type;
   5421 		}
   5422 	    }
   5423 
   5424 	  /* Merge st_other field.  */
   5425 	  elf_merge_st_other (abfd, h, isym->st_other, sec,
   5426 			      definition, dynamic);
   5427 
   5428 	  /* We don't want to make debug symbol dynamic.  */
   5429 	  if (definition
   5430 	      && (sec->flags & SEC_DEBUGGING)
   5431 	      && !bfd_link_relocatable (info))
   5432 	    dynsym = false;
   5433 
   5434 	  /* Nor should we make plugin symbols dynamic.  */
   5435 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5436 	    dynsym = false;
   5437 
   5438 	  if (definition)
   5439 	    {
   5440 	      h->target_internal = isym->st_target_internal;
   5441 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   5442 	    }
   5443 
   5444 	  /* Don't add indirect symbols for .symver x, x@FOO aliases
   5445 	     in IR.  Since all data or text symbols in IR have the
   5446 	     same type, value and section, we can't tell if a symbol
   5447 	     is an alias of another symbol by their types, values and
   5448 	     sections.  */
   5449 	  if (definition
   5450 	      && !dynamic
   5451 	      && (abfd->flags & BFD_PLUGIN) == 0)
   5452 	    {
   5453 	      char *p = strchr (name, ELF_VER_CHR);
   5454 	      if (p != NULL && p[1] != ELF_VER_CHR)
   5455 		{
   5456 		  /* Queue non-default versions so that .symver x, x@FOO
   5457 		     aliases can be checked.  */
   5458 		  if (!nondeflt_vers)
   5459 		    {
   5460 		      size_t amt = ((isymend - isym + 1)
   5461 				    * sizeof (struct elf_link_hash_entry *));
   5462 		      nondeflt_vers
   5463 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   5464 		      if (!nondeflt_vers)
   5465 			goto error_free_vers;
   5466 		    }
   5467 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   5468 		}
   5469 	    }
   5470 
   5471 	  if (dynsym && h->dynindx == -1)
   5472 	    {
   5473 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5474 		goto error_free_vers;
   5475 	      if (h->is_weakalias
   5476 		  && weakdef (h)->dynindx == -1)
   5477 		{
   5478 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
   5479 		    goto error_free_vers;
   5480 		}
   5481 	    }
   5482 	  else if (h->dynindx != -1)
   5483 	    /* If the symbol already has a dynamic index, but
   5484 	       visibility says it should not be visible, turn it into
   5485 	       a local symbol.  */
   5486 	    switch (ELF_ST_VISIBILITY (h->other))
   5487 	      {
   5488 	      case STV_INTERNAL:
   5489 	      case STV_HIDDEN:
   5490 		(*bed->elf_backend_hide_symbol) (info, h, true);
   5491 		dynsym = false;
   5492 		break;
   5493 	      }
   5494 
   5495 	  if (!add_needed
   5496 	      && matched
   5497 	      && definition
   5498 	      && h->root.type != bfd_link_hash_indirect
   5499 	      && ((dynsym
   5500 		   && h->ref_regular_nonweak)
   5501 		  || (old_bfd != NULL
   5502 		      && (old_bfd->flags & BFD_PLUGIN) != 0
   5503 		      && h->ref_ir_nonweak
   5504 		      && !info->lto_all_symbols_read)
   5505 		  || (h->ref_dynamic_nonweak
   5506 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5507 		      && !on_needed_list (elf_dt_name (abfd),
   5508 					  htab->needed, NULL))))
   5509 	    {
   5510 	      const char *soname = elf_dt_name (abfd);
   5511 
   5512 	      info->callbacks->minfo ("%!", soname, old_bfd,
   5513 				      h->root.root.string);
   5514 
   5515 	      /* A symbol from a library loaded via DT_NEEDED of some
   5516 		 other library is referenced by a regular object.
   5517 		 Add a DT_NEEDED entry for it.  Issue an error if
   5518 		 --no-add-needed is used and the reference was not
   5519 		 a weak one.  */
   5520 	      if (old_bfd != NULL
   5521 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   5522 		{
   5523 		  _bfd_error_handler
   5524 		    /* xgettext:c-format */
   5525 		    (_("%pB: undefined reference to symbol '%s'"),
   5526 		     old_bfd, name);
   5527 		  bfd_set_error (bfd_error_missing_dso);
   5528 		  goto error_free_vers;
   5529 		}
   5530 
   5531 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   5532 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   5533 
   5534 	      /* Create dynamic sections for backends that require
   5535 		 that be done before setup_gnu_properties.  */
   5536 	      if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
   5537 		return false;
   5538 	      add_needed = true;
   5539 	    }
   5540 	}
   5541     }
   5542 
   5543   if (info->lto_plugin_active
   5544       && !bfd_link_relocatable (info)
   5545       && (abfd->flags & BFD_PLUGIN) == 0
   5546       && !just_syms
   5547       && extsymcount)
   5548     {
   5549       int r_sym_shift;
   5550 
   5551       if (bed->s->arch_size == 32)
   5552 	r_sym_shift = 8;
   5553       else
   5554 	r_sym_shift = 32;
   5555 
   5556       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
   5557 	 referenced in regular objects so that linker plugin will get
   5558 	 the correct symbol resolution.  */
   5559 
   5560       sym_hash = elf_sym_hashes (abfd);
   5561       for (s = abfd->sections; s != NULL; s = s->next)
   5562 	{
   5563 	  Elf_Internal_Rela *internal_relocs;
   5564 	  Elf_Internal_Rela *rel, *relend;
   5565 
   5566 	  /* Don't check relocations in excluded sections.  */
   5567 	  if ((s->flags & SEC_RELOC) == 0
   5568 	      || s->reloc_count == 0
   5569 	      || (s->flags & SEC_EXCLUDE) != 0
   5570 	      || ((info->strip == strip_all
   5571 		   || info->strip == strip_debugger)
   5572 		  && (s->flags & SEC_DEBUGGING) != 0))
   5573 	    continue;
   5574 
   5575 	  internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
   5576 							    s, NULL,
   5577 							    NULL,
   5578 							    _bfd_link_keep_memory (info));
   5579 	  if (internal_relocs == NULL)
   5580 	    goto error_free_vers;
   5581 
   5582 	  rel = internal_relocs;
   5583 	  relend = rel + s->reloc_count;
   5584 	  for ( ; rel < relend; rel++)
   5585 	    {
   5586 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   5587 	      struct elf_link_hash_entry *h;
   5588 
   5589 	      /* Skip local symbols.  */
   5590 	      if (r_symndx < extsymoff)
   5591 		continue;
   5592 
   5593 	      h = sym_hash[r_symndx - extsymoff];
   5594 	      if (h != NULL)
   5595 		h->root.non_ir_ref_regular = 1;
   5596 	    }
   5597 
   5598 	  if (elf_section_data (s)->relocs != internal_relocs)
   5599 	    free (internal_relocs);
   5600 	}
   5601     }
   5602 
   5603   free (extversym);
   5604   extversym = NULL;
   5605   free (isymbuf);
   5606   isymbuf = NULL;
   5607 
   5608   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   5609     {
   5610       unsigned int i;
   5611 
   5612       /* Restore the symbol table.  */
   5613       old_ent = (char *) old_tab + tabsize;
   5614       memset (elf_sym_hashes (abfd), 0,
   5615 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   5616       htab->root.table.table = old_table;
   5617       htab->root.table.size = old_size;
   5618       htab->root.table.count = old_count;
   5619       memcpy (htab->root.table.table, old_tab, tabsize);
   5620       htab->root.undefs = old_undefs;
   5621       htab->root.undefs_tail = old_undefs_tail;
   5622       if (htab->dynstr != NULL)
   5623 	_bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   5624       free (old_strtab);
   5625       old_strtab = NULL;
   5626       for (i = 0; i < htab->root.table.size; i++)
   5627 	{
   5628 	  struct bfd_hash_entry *p;
   5629 	  struct elf_link_hash_entry *h;
   5630 	  unsigned int non_ir_ref_dynamic;
   5631 
   5632 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   5633 	    {
   5634 	      /* Preserve non_ir_ref_dynamic so that this symbol
   5635 		 will be exported when the dynamic lib becomes needed
   5636 		 in the second pass.  */
   5637 	      h = (struct elf_link_hash_entry *) p;
   5638 	      if (h->root.type == bfd_link_hash_warning)
   5639 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5640 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
   5641 
   5642 	      h = (struct elf_link_hash_entry *) p;
   5643 	      memcpy (h, old_ent, htab->root.table.entsize);
   5644 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   5645 	      if (h->root.type == bfd_link_hash_warning)
   5646 		{
   5647 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5648 		  memcpy (h, old_ent, htab->root.table.entsize);
   5649 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   5650 		}
   5651 	      if (h->root.type == bfd_link_hash_common)
   5652 		{
   5653 		  memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
   5654 		  old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
   5655 		}
   5656 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
   5657 	    }
   5658 	}
   5659 
   5660       /* Make a special call to the linker "notice" function to
   5661 	 tell it that symbols added for crefs may need to be removed.  */
   5662       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   5663 	goto error_free_vers;
   5664 
   5665       free (old_tab);
   5666       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   5667 			   alloc_mark);
   5668       free (nondeflt_vers);
   5669       return true;
   5670     }
   5671 
   5672   if (old_tab != NULL)
   5673     {
   5674       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   5675 	goto error_free_vers;
   5676       free (old_tab);
   5677       old_tab = NULL;
   5678     }
   5679 
   5680   /* Now that all the symbols from this input file are created, if
   5681      not performing a relocatable link, handle .symver foo, foo@BAR
   5682      such that any relocs against foo become foo@BAR.  */
   5683   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   5684     {
   5685       size_t cnt, symidx;
   5686 
   5687       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   5688 	{
   5689 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   5690 	  char *shortname, *p;
   5691 	  size_t amt;
   5692 
   5693 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   5694 	  if (p == NULL
   5695 	      || (h->root.type != bfd_link_hash_defined
   5696 		  && h->root.type != bfd_link_hash_defweak))
   5697 	    continue;
   5698 
   5699 	  amt = p - h->root.root.string;
   5700 	  shortname = (char *) bfd_malloc (amt + 1);
   5701 	  if (!shortname)
   5702 	    goto error_free_vers;
   5703 	  memcpy (shortname, h->root.root.string, amt);
   5704 	  shortname[amt] = '\0';
   5705 
   5706 	  hi = (struct elf_link_hash_entry *)
   5707 	       bfd_link_hash_lookup (&htab->root, shortname,
   5708 				     false, false, false);
   5709 	  if (hi != NULL
   5710 	      && hi->root.type == h->root.type
   5711 	      && hi->root.u.def.value == h->root.u.def.value
   5712 	      && hi->root.u.def.section == h->root.u.def.section)
   5713 	    {
   5714 	      (*bed->elf_backend_hide_symbol) (info, hi, true);
   5715 	      hi->root.type = bfd_link_hash_indirect;
   5716 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   5717 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   5718 	      sym_hash = elf_sym_hashes (abfd);
   5719 	      if (sym_hash)
   5720 		for (symidx = 0; symidx < extsymcount; ++symidx)
   5721 		  if (sym_hash[symidx] == hi)
   5722 		    {
   5723 		      sym_hash[symidx] = h;
   5724 		      break;
   5725 		    }
   5726 	    }
   5727 	  free (shortname);
   5728 	}
   5729       free (nondeflt_vers);
   5730       nondeflt_vers = NULL;
   5731     }
   5732 
   5733   /* Now set the alias field correctly for all the weak defined
   5734      symbols we found.  The only way to do this is to search all the
   5735      symbols.  Since we only need the information for non functions in
   5736      dynamic objects, that's the only time we actually put anything on
   5737      the list WEAKS.  We need this information so that if a regular
   5738      object refers to a symbol defined weakly in a dynamic object, the
   5739      real symbol in the dynamic object is also put in the dynamic
   5740      symbols; we also must arrange for both symbols to point to the
   5741      same memory location.  We could handle the general case of symbol
   5742      aliasing, but a general symbol alias can only be generated in
   5743      assembler code, handling it correctly would be very time
   5744      consuming, and other ELF linkers don't handle general aliasing
   5745      either.  */
   5746   if (weaks != NULL)
   5747     {
   5748       struct elf_link_hash_entry **hpp;
   5749       struct elf_link_hash_entry **hppend;
   5750       struct elf_link_hash_entry **sorted_sym_hash;
   5751       struct elf_link_hash_entry *h;
   5752       size_t sym_count, amt;
   5753 
   5754       /* Since we have to search the whole symbol list for each weak
   5755 	 defined symbol, search time for N weak defined symbols will be
   5756 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   5757       amt = extsymcount * sizeof (*sorted_sym_hash);
   5758       sorted_sym_hash = bfd_malloc (amt);
   5759       if (sorted_sym_hash == NULL)
   5760 	goto error_return;
   5761       sym_hash = sorted_sym_hash;
   5762       hpp = elf_sym_hashes (abfd);
   5763       hppend = hpp + extsymcount;
   5764       sym_count = 0;
   5765       for (; hpp < hppend; hpp++)
   5766 	{
   5767 	  h = *hpp;
   5768 	  if (h != NULL
   5769 	      && h->root.type == bfd_link_hash_defined
   5770 	      && !bed->is_function_type (h->type))
   5771 	    {
   5772 	      *sym_hash = h;
   5773 	      sym_hash++;
   5774 	      sym_count++;
   5775 	    }
   5776 	}
   5777 
   5778       qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
   5779 	     elf_sort_symbol);
   5780 
   5781       while (weaks != NULL)
   5782 	{
   5783 	  struct elf_link_hash_entry *hlook;
   5784 	  asection *slook;
   5785 	  bfd_vma vlook;
   5786 	  size_t i, j, idx = 0;
   5787 
   5788 	  hlook = weaks;
   5789 	  weaks = hlook->u.alias;
   5790 	  hlook->u.alias = NULL;
   5791 
   5792 	  if (hlook->root.type != bfd_link_hash_defined
   5793 	      && hlook->root.type != bfd_link_hash_defweak)
   5794 	    continue;
   5795 
   5796 	  slook = hlook->root.u.def.section;
   5797 	  vlook = hlook->root.u.def.value;
   5798 
   5799 	  i = 0;
   5800 	  j = sym_count;
   5801 	  while (i != j)
   5802 	    {
   5803 	      bfd_signed_vma vdiff;
   5804 	      idx = (i + j) / 2;
   5805 	      h = sorted_sym_hash[idx];
   5806 	      vdiff = vlook - h->root.u.def.value;
   5807 	      if (vdiff < 0)
   5808 		j = idx;
   5809 	      else if (vdiff > 0)
   5810 		i = idx + 1;
   5811 	      else
   5812 		{
   5813 		  int sdiff = slook->id - h->root.u.def.section->id;
   5814 		  if (sdiff < 0)
   5815 		    j = idx;
   5816 		  else if (sdiff > 0)
   5817 		    i = idx + 1;
   5818 		  else
   5819 		    break;
   5820 		}
   5821 	    }
   5822 
   5823 	  /* We didn't find a value/section match.  */
   5824 	  if (i == j)
   5825 	    continue;
   5826 
   5827 	  /* With multiple aliases, or when the weak symbol is already
   5828 	     strongly defined, we have multiple matching symbols and
   5829 	     the binary search above may land on any of them.  Step
   5830 	     one past the matching symbol(s).  */
   5831 	  while (++idx != j)
   5832 	    {
   5833 	      h = sorted_sym_hash[idx];
   5834 	      if (h->root.u.def.section != slook
   5835 		  || h->root.u.def.value != vlook)
   5836 		break;
   5837 	    }
   5838 
   5839 	  /* Now look back over the aliases.  Since we sorted by size
   5840 	     as well as value and section, we'll choose the one with
   5841 	     the largest size.  */
   5842 	  while (idx-- != i)
   5843 	    {
   5844 	      h = sorted_sym_hash[idx];
   5845 
   5846 	      /* Stop if value or section doesn't match.  */
   5847 	      if (h->root.u.def.section != slook
   5848 		  || h->root.u.def.value != vlook)
   5849 		break;
   5850 	      else if (h != hlook)
   5851 		{
   5852 		  struct elf_link_hash_entry *t;
   5853 
   5854 		  hlook->u.alias = h;
   5855 		  hlook->is_weakalias = 1;
   5856 		  t = h;
   5857 		  if (t->u.alias != NULL)
   5858 		    while (t->u.alias != h)
   5859 		      t = t->u.alias;
   5860 		  t->u.alias = hlook;
   5861 
   5862 		  /* If the weak definition is in the list of dynamic
   5863 		     symbols, make sure the real definition is put
   5864 		     there as well.  */
   5865 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   5866 		    {
   5867 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5868 			{
   5869 			err_free_sym_hash:
   5870 			  free (sorted_sym_hash);
   5871 			  goto error_return;
   5872 			}
   5873 		    }
   5874 
   5875 		  /* If the real definition is in the list of dynamic
   5876 		     symbols, make sure the weak definition is put
   5877 		     there as well.  If we don't do this, then the
   5878 		     dynamic loader might not merge the entries for the
   5879 		     real definition and the weak definition.  */
   5880 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   5881 		    {
   5882 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   5883 			goto err_free_sym_hash;
   5884 		    }
   5885 		  break;
   5886 		}
   5887 	    }
   5888 	}
   5889 
   5890       free (sorted_sym_hash);
   5891     }
   5892 
   5893   if (bed->check_directives
   5894       && !(*bed->check_directives) (abfd, info))
   5895     return false;
   5896 
   5897   /* If this is a non-traditional link, try to optimize the handling
   5898      of the .stab/.stabstr sections.  */
   5899   if (! dynamic
   5900       && ! info->traditional_format
   5901       && is_elf_hash_table (&htab->root)
   5902       && (info->strip != strip_all && info->strip != strip_debugger))
   5903     {
   5904       asection *stabstr;
   5905 
   5906       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   5907       if (stabstr != NULL)
   5908 	{
   5909 	  bfd_size_type string_offset = 0;
   5910 	  asection *stab;
   5911 
   5912 	  for (stab = abfd->sections; stab; stab = stab->next)
   5913 	    if (startswith (stab->name, ".stab")
   5914 		&& (!stab->name[5] ||
   5915 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   5916 		&& (stab->flags & SEC_MERGE) == 0
   5917 		&& !bfd_is_abs_section (stab->output_section))
   5918 	      {
   5919 		struct bfd_elf_section_data *secdata;
   5920 
   5921 		secdata = elf_section_data (stab);
   5922 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   5923 					       stabstr, &secdata->sec_info,
   5924 					       &string_offset))
   5925 		  goto error_return;
   5926 		if (secdata->sec_info)
   5927 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   5928 	    }
   5929 	}
   5930     }
   5931 
   5932   if (dynamic && add_needed)
   5933     {
   5934       /* Add this bfd to the loaded list.  */
   5935       struct elf_link_loaded_list *n;
   5936 
   5937       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   5938       if (n == NULL)
   5939 	goto error_return;
   5940       n->abfd = abfd;
   5941       n->next = htab->dyn_loaded;
   5942       htab->dyn_loaded = n;
   5943     }
   5944   if (dynamic && !add_needed
   5945       && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
   5946     elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
   5947 
   5948   return true;
   5949 
   5950  error_free_vers:
   5951   free (old_tab);
   5952   free (old_strtab);
   5953   free (nondeflt_vers);
   5954   free (extversym);
   5955  error_free_sym:
   5956   free (isymbuf);
   5957  error_return:
   5958   return false;
   5959 }
   5960 
   5961 /* Return the linker hash table entry of a symbol that might be
   5962    satisfied by an archive symbol.  Return -1 on error.  */
   5963 
   5964 struct bfd_link_hash_entry *
   5965 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   5966 				struct bfd_link_info *info,
   5967 				const char *name)
   5968 {
   5969   struct bfd_link_hash_entry *h;
   5970   char *p, *copy;
   5971   size_t len, first;
   5972 
   5973   h = bfd_link_hash_lookup (info->hash, name, false, false, true);
   5974   if (h != NULL)
   5975     return h;
   5976 
   5977   /* If this is a default version (the name contains @@), look up the
   5978      symbol again with only one `@' as well as without the version.
   5979      The effect is that references to the symbol with and without the
   5980      version will be matched by the default symbol in the archive.  */
   5981 
   5982   p = strchr (name, ELF_VER_CHR);
   5983   if (p == NULL || p[1] != ELF_VER_CHR)
   5984     return h;
   5985 
   5986   /* First check with only one `@'.  */
   5987   len = strlen (name);
   5988   copy = (char *) bfd_alloc (abfd, len);
   5989   if (copy == NULL)
   5990     return (struct bfd_link_hash_entry *) -1;
   5991 
   5992   first = p - name + 1;
   5993   memcpy (copy, name, first);
   5994   memcpy (copy + first, name + first + 1, len - first);
   5995 
   5996   h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   5997   if (h == NULL)
   5998     {
   5999       /* We also need to check references to the symbol without the
   6000 	 version.  */
   6001       copy[first - 1] = '\0';
   6002       h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
   6003     }
   6004 
   6005   bfd_release (abfd, copy);
   6006   return h;
   6007 }
   6008 
   6009 /* Add symbols from an ELF archive file to the linker hash table.  We
   6010    don't use _bfd_generic_link_add_archive_symbols because we need to
   6011    handle versioned symbols.
   6012 
   6013    Fortunately, ELF archive handling is simpler than that done by
   6014    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   6015    oddities.  In ELF, if we find a symbol in the archive map, and the
   6016    symbol is currently undefined, we know that we must pull in that
   6017    object file.
   6018 
   6019    Unfortunately, we do have to make multiple passes over the symbol
   6020    table until nothing further is resolved.  */
   6021 
   6022 static bool
   6023 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   6024 {
   6025   symindex c;
   6026   unsigned char *included = NULL;
   6027   carsym *symdefs;
   6028   bool loop;
   6029   size_t amt;
   6030   const struct elf_backend_data *bed;
   6031   struct bfd_link_hash_entry * (*archive_symbol_lookup)
   6032     (bfd *, struct bfd_link_info *, const char *);
   6033 
   6034   if (! bfd_has_map (abfd))
   6035     {
   6036       /* An empty archive is a special case.  */
   6037       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   6038 	return true;
   6039       bfd_set_error (bfd_error_no_armap);
   6040       return false;
   6041     }
   6042 
   6043   /* Keep track of all symbols we know to be already defined, and all
   6044      files we know to be already included.  This is to speed up the
   6045      second and subsequent passes.  */
   6046   c = bfd_ardata (abfd)->symdef_count;
   6047   if (c == 0)
   6048     return true;
   6049   amt = c * sizeof (*included);
   6050   included = (unsigned char *) bfd_zmalloc (amt);
   6051   if (included == NULL)
   6052     return false;
   6053 
   6054   symdefs = bfd_ardata (abfd)->symdefs;
   6055   bed = get_elf_backend_data (abfd);
   6056   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   6057 
   6058   do
   6059     {
   6060       file_ptr last;
   6061       symindex i;
   6062       carsym *symdef;
   6063       carsym *symdefend;
   6064 
   6065       loop = false;
   6066       last = -1;
   6067 
   6068       symdef = symdefs;
   6069       symdefend = symdef + c;
   6070       for (i = 0; symdef < symdefend; symdef++, i++)
   6071 	{
   6072 	  struct bfd_link_hash_entry *h;
   6073 	  bfd *element;
   6074 	  struct bfd_link_hash_entry *undefs_tail;
   6075 	  symindex mark;
   6076 
   6077 	  if (included[i])
   6078 	    continue;
   6079 	  if (symdef->file_offset == last)
   6080 	    {
   6081 	      included[i] = true;
   6082 	      continue;
   6083 	    }
   6084 
   6085 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   6086 	  if (h == (struct bfd_link_hash_entry *) -1)
   6087 	    goto error_return;
   6088 
   6089 	  if (h == NULL)
   6090 	    continue;
   6091 
   6092 	  if (h->type == bfd_link_hash_undefined)
   6093 	    {
   6094 	      /* If the archive element has already been loaded then one
   6095 		 of the symbols defined by that element might have been
   6096 		 made undefined due to being in a discarded section.  */
   6097 	      if (is_elf_hash_table (info->hash)
   6098 		  && ((struct elf_link_hash_entry *) h)->indx == -3)
   6099 		continue;
   6100 	    }
   6101 	  else if (h->type == bfd_link_hash_common)
   6102 	    {
   6103 	      /* We currently have a common symbol.  The archive map contains
   6104 		 a reference to this symbol, so we may want to include it.  We
   6105 		 only want to include it however, if this archive element
   6106 		 contains a definition of the symbol, not just another common
   6107 		 declaration of it.
   6108 
   6109 		 Unfortunately some archivers (including GNU ar) will put
   6110 		 declarations of common symbols into their archive maps, as
   6111 		 well as real definitions, so we cannot just go by the archive
   6112 		 map alone.  Instead we must read in the element's symbol
   6113 		 table and check that to see what kind of symbol definition
   6114 		 this is.  */
   6115 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   6116 		continue;
   6117 	    }
   6118 	  else
   6119 	    {
   6120 	      if (h->type != bfd_link_hash_undefweak)
   6121 		/* Symbol must be defined.  Don't check it again.  */
   6122 		included[i] = true;
   6123 	      continue;
   6124 	    }
   6125 
   6126 	  /* We need to include this archive member.  */
   6127 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
   6128 					     info);
   6129 	  if (element == NULL)
   6130 	    goto error_return;
   6131 
   6132 	  if (! bfd_check_format (element, bfd_object))
   6133 	    goto error_return;
   6134 
   6135 	  undefs_tail = info->hash->undefs_tail;
   6136 
   6137 	  if (!(*info->callbacks
   6138 		->add_archive_element) (info, element, symdef->name, &element))
   6139 	    continue;
   6140 	  if (!bfd_link_add_symbols (element, info))
   6141 	    goto error_return;
   6142 
   6143 	  /* If there are any new undefined symbols, we need to make
   6144 	     another pass through the archive in order to see whether
   6145 	     they can be defined.  FIXME: This isn't perfect, because
   6146 	     common symbols wind up on undefs_tail and because an
   6147 	     undefined symbol which is defined later on in this pass
   6148 	     does not require another pass.  This isn't a bug, but it
   6149 	     does make the code less efficient than it could be.  */
   6150 	  if (undefs_tail != info->hash->undefs_tail)
   6151 	    loop = true;
   6152 
   6153 	  /* Look backward to mark all symbols from this object file
   6154 	     which we have already seen in this pass.  */
   6155 	  mark = i;
   6156 	  do
   6157 	    {
   6158 	      included[mark] = true;
   6159 	      if (mark == 0)
   6160 		break;
   6161 	      --mark;
   6162 	    }
   6163 	  while (symdefs[mark].file_offset == symdef->file_offset);
   6164 
   6165 	  /* We mark subsequent symbols from this object file as we go
   6166 	     on through the loop.  */
   6167 	  last = symdef->file_offset;
   6168 	}
   6169     }
   6170   while (loop);
   6171 
   6172   free (included);
   6173   return true;
   6174 
   6175  error_return:
   6176   free (included);
   6177   return false;
   6178 }
   6179 
   6180 /* Given an ELF BFD, add symbols to the global hash table as
   6181    appropriate.  */
   6182 
   6183 bool
   6184 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   6185 {
   6186   switch (bfd_get_format (abfd))
   6187     {
   6188     case bfd_object:
   6189       return elf_link_add_object_symbols (abfd, info);
   6190     case bfd_archive:
   6191       return elf_link_add_archive_symbols (abfd, info);
   6192     default:
   6193       bfd_set_error (bfd_error_wrong_format);
   6194       return false;
   6195     }
   6196 }
   6197 
   6198 struct hash_codes_info
   6200 {
   6201   unsigned long *hashcodes;
   6202   bool error;
   6203 };
   6204 
   6205 /* This function will be called though elf_link_hash_traverse to store
   6206    all hash value of the exported symbols in an array.  */
   6207 
   6208 static bool
   6209 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   6210 {
   6211   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   6212   const char *name;
   6213   unsigned long ha;
   6214   char *alc = NULL;
   6215 
   6216   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6217   if (h->dynindx == -1)
   6218     return true;
   6219 
   6220   name = h->root.root.string;
   6221   if (h->versioned >= versioned)
   6222     {
   6223       char *p = strchr (name, ELF_VER_CHR);
   6224       if (p != NULL)
   6225 	{
   6226 	  alc = (char *) bfd_malloc (p - name + 1);
   6227 	  if (alc == NULL)
   6228 	    {
   6229 	      inf->error = true;
   6230 	      return false;
   6231 	    }
   6232 	  memcpy (alc, name, p - name);
   6233 	  alc[p - name] = '\0';
   6234 	  name = alc;
   6235 	}
   6236     }
   6237 
   6238   /* Compute the hash value.  */
   6239   ha = bfd_elf_hash (name);
   6240 
   6241   /* Store the found hash value in the array given as the argument.  */
   6242   *(inf->hashcodes)++ = ha;
   6243 
   6244   /* And store it in the struct so that we can put it in the hash table
   6245      later.  */
   6246   h->u.elf_hash_value = ha;
   6247 
   6248   free (alc);
   6249   return true;
   6250 }
   6251 
   6252 struct collect_gnu_hash_codes
   6253 {
   6254   bfd *output_bfd;
   6255   const struct elf_backend_data *bed;
   6256   unsigned long int nsyms;
   6257   unsigned long int maskbits;
   6258   unsigned long int *hashcodes;
   6259   unsigned long int *hashval;
   6260   unsigned long int *indx;
   6261   unsigned long int *counts;
   6262   bfd_vma *bitmask;
   6263   bfd_byte *contents;
   6264   bfd_size_type xlat;
   6265   long int min_dynindx;
   6266   unsigned long int bucketcount;
   6267   unsigned long int symindx;
   6268   long int local_indx;
   6269   long int shift1, shift2;
   6270   unsigned long int mask;
   6271   bool error;
   6272 };
   6273 
   6274 /* This function will be called though elf_link_hash_traverse to store
   6275    all hash value of the exported symbols in an array.  */
   6276 
   6277 static bool
   6278 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   6279 {
   6280   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6281   const char *name;
   6282   unsigned long ha;
   6283   char *alc = NULL;
   6284 
   6285   /* Ignore indirect symbols.  These are added by the versioning code.  */
   6286   if (h->dynindx == -1)
   6287     return true;
   6288 
   6289   /* Ignore also local symbols and undefined symbols.  */
   6290   if (! (*s->bed->elf_hash_symbol) (h))
   6291     return true;
   6292 
   6293   name = h->root.root.string;
   6294   if (h->versioned >= versioned)
   6295     {
   6296       char *p = strchr (name, ELF_VER_CHR);
   6297       if (p != NULL)
   6298 	{
   6299 	  alc = (char *) bfd_malloc (p - name + 1);
   6300 	  if (alc == NULL)
   6301 	    {
   6302 	      s->error = true;
   6303 	      return false;
   6304 	    }
   6305 	  memcpy (alc, name, p - name);
   6306 	  alc[p - name] = '\0';
   6307 	  name = alc;
   6308 	}
   6309     }
   6310 
   6311   /* Compute the hash value.  */
   6312   ha = bfd_elf_gnu_hash (name);
   6313 
   6314   /* Store the found hash value in the array for compute_bucket_count,
   6315      and also for .dynsym reordering purposes.  */
   6316   s->hashcodes[s->nsyms] = ha;
   6317   s->hashval[h->dynindx] = ha;
   6318   ++s->nsyms;
   6319   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   6320     s->min_dynindx = h->dynindx;
   6321 
   6322   free (alc);
   6323   return true;
   6324 }
   6325 
   6326 /* This function will be called though elf_link_hash_traverse to do
   6327    final dynamic symbol renumbering in case of .gnu.hash.
   6328    If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
   6329    to the translation table.  */
   6330 
   6331 static bool
   6332 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
   6333 {
   6334   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   6335   unsigned long int bucket;
   6336   unsigned long int val;
   6337 
   6338   /* Ignore indirect symbols.  */
   6339   if (h->dynindx == -1)
   6340     return true;
   6341 
   6342   /* Ignore also local symbols and undefined symbols.  */
   6343   if (! (*s->bed->elf_hash_symbol) (h))
   6344     {
   6345       if (h->dynindx >= s->min_dynindx)
   6346 	{
   6347 	  if (s->bed->record_xhash_symbol != NULL)
   6348 	    {
   6349 	      (*s->bed->record_xhash_symbol) (h, 0);
   6350 	      s->local_indx++;
   6351 	    }
   6352 	  else
   6353 	    h->dynindx = s->local_indx++;
   6354 	}
   6355       return true;
   6356     }
   6357 
   6358   bucket = s->hashval[h->dynindx] % s->bucketcount;
   6359   val = (s->hashval[h->dynindx] >> s->shift1)
   6360 	& ((s->maskbits >> s->shift1) - 1);
   6361   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   6362   s->bitmask[val]
   6363     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   6364   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   6365   if (s->counts[bucket] == 1)
   6366     /* Last element terminates the chain.  */
   6367     val |= 1;
   6368   bfd_put_32 (s->output_bfd, val,
   6369 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   6370   --s->counts[bucket];
   6371   if (s->bed->record_xhash_symbol != NULL)
   6372     {
   6373       bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
   6374 
   6375       (*s->bed->record_xhash_symbol) (h, xlat_loc);
   6376     }
   6377   else
   6378     h->dynindx = s->indx[bucket]++;
   6379   return true;
   6380 }
   6381 
   6382 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   6383 
   6384 bool
   6385 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   6386 {
   6387   return !(h->forced_local
   6388 	   || h->root.type == bfd_link_hash_undefined
   6389 	   || h->root.type == bfd_link_hash_undefweak
   6390 	   || ((h->root.type == bfd_link_hash_defined
   6391 		|| h->root.type == bfd_link_hash_defweak)
   6392 	       && h->root.u.def.section->output_section == NULL));
   6393 }
   6394 
   6395 /* Array used to determine the number of hash table buckets to use
   6396    based on the number of symbols there are.  If there are fewer than
   6397    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   6398    fewer than 37 we use 17 buckets, and so forth.  We never use more
   6399    than 32771 buckets.  */
   6400 
   6401 static const size_t elf_buckets[] =
   6402 {
   6403   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   6404   16411, 32771, 0
   6405 };
   6406 
   6407 /* Compute bucket count for hashing table.  We do not use a static set
   6408    of possible tables sizes anymore.  Instead we determine for all
   6409    possible reasonable sizes of the table the outcome (i.e., the
   6410    number of collisions etc) and choose the best solution.  The
   6411    weighting functions are not too simple to allow the table to grow
   6412    without bounds.  Instead one of the weighting factors is the size.
   6413    Therefore the result is always a good payoff between few collisions
   6414    (= short chain lengths) and table size.  */
   6415 static size_t
   6416 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6417 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   6418 		      unsigned long int nsyms,
   6419 		      int gnu_hash)
   6420 {
   6421   size_t best_size = 0;
   6422   unsigned long int i;
   6423 
   6424   if (info->optimize)
   6425     {
   6426       size_t minsize;
   6427       size_t maxsize;
   6428       uint64_t best_chlen = ~((uint64_t) 0);
   6429       bfd *dynobj = elf_hash_table (info)->dynobj;
   6430       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   6431       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   6432       unsigned long int *counts;
   6433       bfd_size_type amt;
   6434       unsigned int no_improvement_count = 0;
   6435 
   6436       /* Possible optimization parameters: if we have NSYMS symbols we say
   6437 	 that the hashing table must at least have NSYMS/4 and at most
   6438 	 2*NSYMS buckets.  */
   6439       minsize = nsyms / 4;
   6440       if (minsize == 0)
   6441 	minsize = 1;
   6442       best_size = maxsize = nsyms * 2;
   6443       if (gnu_hash)
   6444 	{
   6445 	  if (minsize < 2)
   6446 	    minsize = 2;
   6447 	  if ((best_size & 31) == 0)
   6448 	    ++best_size;
   6449 	}
   6450 
   6451       /* Create array where we count the collisions in.  We must use bfd_malloc
   6452 	 since the size could be large.  */
   6453       amt = maxsize;
   6454       amt *= sizeof (unsigned long int);
   6455       counts = (unsigned long int *) bfd_malloc (amt);
   6456       if (counts == NULL)
   6457 	return 0;
   6458 
   6459       /* Compute the "optimal" size for the hash table.  The criteria is a
   6460 	 minimal chain length.  The minor criteria is (of course) the size
   6461 	 of the table.  */
   6462       for (i = minsize; i < maxsize; ++i)
   6463 	{
   6464 	  /* Walk through the array of hashcodes and count the collisions.  */
   6465 	  uint64_t max;
   6466 	  unsigned long int j;
   6467 	  unsigned long int fact;
   6468 
   6469 	  if (gnu_hash && (i & 31) == 0)
   6470 	    continue;
   6471 
   6472 	  memset (counts, '\0', i * sizeof (unsigned long int));
   6473 
   6474 	  /* Determine how often each hash bucket is used.  */
   6475 	  for (j = 0; j < nsyms; ++j)
   6476 	    ++counts[hashcodes[j] % i];
   6477 
   6478 	  /* For the weight function we need some information about the
   6479 	     pagesize on the target.  This is information need not be 100%
   6480 	     accurate.  Since this information is not available (so far) we
   6481 	     define it here to a reasonable default value.  If it is crucial
   6482 	     to have a better value some day simply define this value.  */
   6483 # ifndef BFD_TARGET_PAGESIZE
   6484 #  define BFD_TARGET_PAGESIZE	(4096)
   6485 # endif
   6486 
   6487 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   6488 	     and the chains.  */
   6489 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   6490 
   6491 # if 1
   6492 	  /* Variant 1: optimize for short chains.  We add the squares
   6493 	     of all the chain lengths (which favors many small chain
   6494 	     over a few long chains).  */
   6495 	  for (j = 0; j < i; ++j)
   6496 	    max += counts[j] * counts[j];
   6497 
   6498 	  /* This adds penalties for the overall size of the table.  */
   6499 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6500 	  max *= fact * fact;
   6501 # else
   6502 	  /* Variant 2: Optimize a lot more for small table.  Here we
   6503 	     also add squares of the size but we also add penalties for
   6504 	     empty slots (the +1 term).  */
   6505 	  for (j = 0; j < i; ++j)
   6506 	    max += (1 + counts[j]) * (1 + counts[j]);
   6507 
   6508 	  /* The overall size of the table is considered, but not as
   6509 	     strong as in variant 1, where it is squared.  */
   6510 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6511 	  max *= fact;
   6512 # endif
   6513 
   6514 	  /* Compare with current best results.  */
   6515 	  if (max < best_chlen)
   6516 	    {
   6517 	      best_chlen = max;
   6518 	      best_size = i;
   6519 	      no_improvement_count = 0;
   6520 	    }
   6521 	  /* PR 11843: Avoid futile long searches for the best bucket size
   6522 	     when there are a large number of symbols.  */
   6523 	  else if (++no_improvement_count == 100)
   6524 	    break;
   6525 	}
   6526 
   6527       free (counts);
   6528     }
   6529   else
   6530     {
   6531       for (i = 0; elf_buckets[i] != 0; i++)
   6532 	{
   6533 	  best_size = elf_buckets[i];
   6534 	  if (nsyms < elf_buckets[i + 1])
   6535 	    break;
   6536 	}
   6537       if (gnu_hash && best_size < 2)
   6538 	best_size = 2;
   6539     }
   6540 
   6541   return best_size;
   6542 }
   6543 
   6544 /* Size any SHT_GROUP section for ld -r.  */
   6545 
   6546 bool
   6547 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   6548 {
   6549   bfd *ibfd;
   6550   asection *s;
   6551 
   6552   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6553     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   6554 	&& (s = ibfd->sections) != NULL
   6555 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
   6556 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   6557       return false;
   6558   return true;
   6559 }
   6560 
   6561 /* Set a default stack segment size.  The value in INFO wins.  If it
   6562    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   6563    undefined it is initialized.  */
   6564 
   6565 bool
   6566 bfd_elf_stack_segment_size (bfd *output_bfd,
   6567 			    struct bfd_link_info *info,
   6568 			    const char *legacy_symbol,
   6569 			    bfd_vma default_size)
   6570 {
   6571   struct elf_link_hash_entry *h = NULL;
   6572 
   6573   /* Look for legacy symbol.  */
   6574   if (legacy_symbol)
   6575     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   6576 			      false, false, false);
   6577   if (h && (h->root.type == bfd_link_hash_defined
   6578 	    || h->root.type == bfd_link_hash_defweak)
   6579       && h->def_regular
   6580       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   6581     {
   6582       /* The symbol has no type if specified on the command line.  */
   6583       h->type = STT_OBJECT;
   6584       if (info->stacksize)
   6585 	/* xgettext:c-format */
   6586 	_bfd_error_handler (_("%pB: stack size specified and %s set"),
   6587 			    output_bfd, legacy_symbol);
   6588       else if (h->root.u.def.section != bfd_abs_section_ptr)
   6589 	/* xgettext:c-format */
   6590 	_bfd_error_handler (_("%pB: %s not absolute"),
   6591 			    output_bfd, legacy_symbol);
   6592       else
   6593 	info->stacksize = h->root.u.def.value;
   6594     }
   6595 
   6596   if (!info->stacksize)
   6597     /* If the user didn't set a size, or explicitly inhibit the
   6598        size, set it now.  */
   6599     info->stacksize = default_size;
   6600 
   6601   /* Provide the legacy symbol, if it is referenced.  */
   6602   if (h && (h->root.type == bfd_link_hash_undefined
   6603 	    || h->root.type == bfd_link_hash_undefweak))
   6604     {
   6605       struct bfd_link_hash_entry *bh = NULL;
   6606 
   6607       if (!(_bfd_generic_link_add_one_symbol
   6608 	    (info, output_bfd, legacy_symbol,
   6609 	     BSF_GLOBAL, bfd_abs_section_ptr,
   6610 	     info->stacksize >= 0 ? info->stacksize : 0,
   6611 	     NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
   6612 	return false;
   6613 
   6614       h = (struct elf_link_hash_entry *) bh;
   6615       h->def_regular = 1;
   6616       h->type = STT_OBJECT;
   6617     }
   6618 
   6619   return true;
   6620 }
   6621 
   6622 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   6623 
   6624 struct elf_gc_sweep_symbol_info
   6625 {
   6626   struct bfd_link_info *info;
   6627   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   6628 		       bool);
   6629 };
   6630 
   6631 static bool
   6632 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   6633 {
   6634   if (!h->mark
   6635       && (((h->root.type == bfd_link_hash_defined
   6636 	    || h->root.type == bfd_link_hash_defweak)
   6637 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   6638 		&& h->root.u.def.section->gc_mark))
   6639 	  || h->root.type == bfd_link_hash_undefined
   6640 	  || h->root.type == bfd_link_hash_undefweak))
   6641     {
   6642       struct elf_gc_sweep_symbol_info *inf;
   6643 
   6644       inf = (struct elf_gc_sweep_symbol_info *) data;
   6645       (*inf->hide_symbol) (inf->info, h, true);
   6646       h->def_regular = 0;
   6647       h->ref_regular = 0;
   6648       h->ref_regular_nonweak = 0;
   6649     }
   6650 
   6651   return true;
   6652 }
   6653 
   6654 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   6655    called by the ELF linker emulation before_allocation routine.  We
   6656    must set the sizes of the sections before the linker sets the
   6657    addresses of the various sections.  */
   6658 
   6659 bool
   6660 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   6661 			       const char *soname,
   6662 			       const char *rpath,
   6663 			       const char *filter_shlib,
   6664 			       const char *audit,
   6665 			       const char *depaudit,
   6666 			       const char * const *auxiliary_filters,
   6667 			       struct bfd_link_info *info,
   6668 			       asection **sinterpptr)
   6669 {
   6670   bfd *dynobj;
   6671   const struct elf_backend_data *bed;
   6672 
   6673   *sinterpptr = NULL;
   6674 
   6675   if (!is_elf_hash_table (info->hash))
   6676     return true;
   6677 
   6678   /* Any syms created from now on start with -1 in
   6679      got.refcount/offset and plt.refcount/offset.  */
   6680   elf_hash_table (info)->init_got_refcount
   6681     = elf_hash_table (info)->init_got_offset;
   6682   elf_hash_table (info)->init_plt_refcount
   6683     = elf_hash_table (info)->init_plt_offset;
   6684 
   6685   bed = get_elf_backend_data (output_bfd);
   6686 
   6687   /* The backend may have to create some sections regardless of whether
   6688      we're dynamic or not.  */
   6689   if (bed->elf_backend_always_size_sections
   6690       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
   6691     return false;
   6692 
   6693   dynobj = elf_hash_table (info)->dynobj;
   6694 
   6695   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6696     {
   6697       struct bfd_elf_version_tree *verdefs;
   6698       struct elf_info_failed asvinfo;
   6699       struct bfd_elf_version_tree *t;
   6700       struct bfd_elf_version_expr *d;
   6701       asection *s;
   6702       size_t soname_indx;
   6703 
   6704       /* If we are supposed to export all symbols into the dynamic symbol
   6705 	 table (this is not the normal case), then do so.  */
   6706       if (info->export_dynamic
   6707 	  || (bfd_link_executable (info) && info->dynamic))
   6708 	{
   6709 	  struct elf_info_failed eif;
   6710 
   6711 	  eif.info = info;
   6712 	  eif.failed = false;
   6713 	  elf_link_hash_traverse (elf_hash_table (info),
   6714 				  _bfd_elf_export_symbol,
   6715 				  &eif);
   6716 	  if (eif.failed)
   6717 	    return false;
   6718 	}
   6719 
   6720       if (soname != NULL)
   6721 	{
   6722 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6723 					     soname, true);
   6724 	  if (soname_indx == (size_t) -1
   6725 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   6726 	    return false;
   6727 	}
   6728       else
   6729 	soname_indx = (size_t) -1;
   6730 
   6731       /* Make all global versions with definition.  */
   6732       for (t = info->version_info; t != NULL; t = t->next)
   6733 	for (d = t->globals.list; d != NULL; d = d->next)
   6734 	  if (!d->symver && d->literal)
   6735 	    {
   6736 	      const char *verstr, *name;
   6737 	      size_t namelen, verlen, newlen;
   6738 	      char *newname, *p, leading_char;
   6739 	      struct elf_link_hash_entry *newh;
   6740 
   6741 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   6742 	      name = d->pattern;
   6743 	      namelen = strlen (name) + (leading_char != '\0');
   6744 	      verstr = t->name;
   6745 	      verlen = strlen (verstr);
   6746 	      newlen = namelen + verlen + 3;
   6747 
   6748 	      newname = (char *) bfd_malloc (newlen);
   6749 	      if (newname == NULL)
   6750 		return false;
   6751 	      newname[0] = leading_char;
   6752 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   6753 
   6754 	      /* Check the hidden versioned definition.  */
   6755 	      p = newname + namelen;
   6756 	      *p++ = ELF_VER_CHR;
   6757 	      memcpy (p, verstr, verlen + 1);
   6758 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   6759 					   newname, false, false,
   6760 					   false);
   6761 	      if (newh == NULL
   6762 		  || (newh->root.type != bfd_link_hash_defined
   6763 		      && newh->root.type != bfd_link_hash_defweak))
   6764 		{
   6765 		  /* Check the default versioned definition.  */
   6766 		  *p++ = ELF_VER_CHR;
   6767 		  memcpy (p, verstr, verlen + 1);
   6768 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   6769 					       newname, false, false,
   6770 					       false);
   6771 		}
   6772 	      free (newname);
   6773 
   6774 	      /* Mark this version if there is a definition and it is
   6775 		 not defined in a shared object.  */
   6776 	      if (newh != NULL
   6777 		  && !newh->def_dynamic
   6778 		  && (newh->root.type == bfd_link_hash_defined
   6779 		      || newh->root.type == bfd_link_hash_defweak))
   6780 		d->symver = 1;
   6781 	    }
   6782 
   6783       /* Attach all the symbols to their version information.  */
   6784       asvinfo.info = info;
   6785       asvinfo.failed = false;
   6786 
   6787       elf_link_hash_traverse (elf_hash_table (info),
   6788 			      _bfd_elf_link_assign_sym_version,
   6789 			      &asvinfo);
   6790       if (asvinfo.failed)
   6791 	return false;
   6792 
   6793       if (!info->allow_undefined_version)
   6794 	{
   6795 	  /* Check if all global versions have a definition.  */
   6796 	  bool all_defined = true;
   6797 	  for (t = info->version_info; t != NULL; t = t->next)
   6798 	    for (d = t->globals.list; d != NULL; d = d->next)
   6799 	      if (d->literal && !d->symver && !d->script)
   6800 		{
   6801 		  _bfd_error_handler
   6802 		    (_("%s: undefined version: %s"),
   6803 		     d->pattern, t->name);
   6804 		  all_defined = false;
   6805 		}
   6806 
   6807 	  if (!all_defined)
   6808 	    {
   6809 	      bfd_set_error (bfd_error_bad_value);
   6810 	      return false;
   6811 	    }
   6812 	}
   6813 
   6814       /* Set up the version definition section.  */
   6815       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   6816       BFD_ASSERT (s != NULL);
   6817 
   6818       /* We may have created additional version definitions if we are
   6819 	 just linking a regular application.  */
   6820       verdefs = info->version_info;
   6821 
   6822       /* Skip anonymous version tag.  */
   6823       if (verdefs != NULL && verdefs->vernum == 0)
   6824 	verdefs = verdefs->next;
   6825 
   6826       if (verdefs == NULL && !info->create_default_symver)
   6827 	s->flags |= SEC_EXCLUDE;
   6828       else
   6829 	{
   6830 	  unsigned int cdefs;
   6831 	  bfd_size_type size;
   6832 	  bfd_byte *p;
   6833 	  Elf_Internal_Verdef def;
   6834 	  Elf_Internal_Verdaux defaux;
   6835 	  struct bfd_link_hash_entry *bh;
   6836 	  struct elf_link_hash_entry *h;
   6837 	  const char *name;
   6838 
   6839 	  cdefs = 0;
   6840 	  size = 0;
   6841 
   6842 	  /* Make space for the base version.  */
   6843 	  size += sizeof (Elf_External_Verdef);
   6844 	  size += sizeof (Elf_External_Verdaux);
   6845 	  ++cdefs;
   6846 
   6847 	  /* Make space for the default version.  */
   6848 	  if (info->create_default_symver)
   6849 	    {
   6850 	      size += sizeof (Elf_External_Verdef);
   6851 	      ++cdefs;
   6852 	    }
   6853 
   6854 	  for (t = verdefs; t != NULL; t = t->next)
   6855 	    {
   6856 	      struct bfd_elf_version_deps *n;
   6857 
   6858 	      /* Don't emit base version twice.  */
   6859 	      if (t->vernum == 0)
   6860 		continue;
   6861 
   6862 	      size += sizeof (Elf_External_Verdef);
   6863 	      size += sizeof (Elf_External_Verdaux);
   6864 	      ++cdefs;
   6865 
   6866 	      for (n = t->deps; n != NULL; n = n->next)
   6867 		size += sizeof (Elf_External_Verdaux);
   6868 	    }
   6869 
   6870 	  s->size = size;
   6871 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6872 	  if (s->contents == NULL && s->size != 0)
   6873 	    return false;
   6874 
   6875 	  /* Fill in the version definition section.  */
   6876 
   6877 	  p = s->contents;
   6878 
   6879 	  def.vd_version = VER_DEF_CURRENT;
   6880 	  def.vd_flags = VER_FLG_BASE;
   6881 	  def.vd_ndx = 1;
   6882 	  def.vd_cnt = 1;
   6883 	  if (info->create_default_symver)
   6884 	    {
   6885 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   6886 	      def.vd_next = sizeof (Elf_External_Verdef);
   6887 	    }
   6888 	  else
   6889 	    {
   6890 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6891 	      def.vd_next = (sizeof (Elf_External_Verdef)
   6892 			     + sizeof (Elf_External_Verdaux));
   6893 	    }
   6894 
   6895 	  if (soname_indx != (size_t) -1)
   6896 	    {
   6897 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6898 				      soname_indx);
   6899 	      def.vd_hash = bfd_elf_hash (soname);
   6900 	      defaux.vda_name = soname_indx;
   6901 	      name = soname;
   6902 	    }
   6903 	  else
   6904 	    {
   6905 	      size_t indx;
   6906 
   6907 	      name = lbasename (bfd_get_filename (output_bfd));
   6908 	      def.vd_hash = bfd_elf_hash (name);
   6909 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6910 					  name, false);
   6911 	      if (indx == (size_t) -1)
   6912 		return false;
   6913 	      defaux.vda_name = indx;
   6914 	    }
   6915 	  defaux.vda_next = 0;
   6916 
   6917 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   6918 				    (Elf_External_Verdef *) p);
   6919 	  p += sizeof (Elf_External_Verdef);
   6920 	  if (info->create_default_symver)
   6921 	    {
   6922 	      /* Add a symbol representing this version.  */
   6923 	      bh = NULL;
   6924 	      if (! (_bfd_generic_link_add_one_symbol
   6925 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   6926 		      0, NULL, false,
   6927 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6928 		return false;
   6929 	      h = (struct elf_link_hash_entry *) bh;
   6930 	      h->non_elf = 0;
   6931 	      h->def_regular = 1;
   6932 	      h->type = STT_OBJECT;
   6933 	      h->verinfo.vertree = NULL;
   6934 
   6935 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6936 		return false;
   6937 
   6938 	      /* Create a duplicate of the base version with the same
   6939 		 aux block, but different flags.  */
   6940 	      def.vd_flags = 0;
   6941 	      def.vd_ndx = 2;
   6942 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6943 	      if (verdefs)
   6944 		def.vd_next = (sizeof (Elf_External_Verdef)
   6945 			       + sizeof (Elf_External_Verdaux));
   6946 	      else
   6947 		def.vd_next = 0;
   6948 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6949 					(Elf_External_Verdef *) p);
   6950 	      p += sizeof (Elf_External_Verdef);
   6951 	    }
   6952 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6953 				     (Elf_External_Verdaux *) p);
   6954 	  p += sizeof (Elf_External_Verdaux);
   6955 
   6956 	  for (t = verdefs; t != NULL; t = t->next)
   6957 	    {
   6958 	      unsigned int cdeps;
   6959 	      struct bfd_elf_version_deps *n;
   6960 
   6961 	      /* Don't emit the base version twice.  */
   6962 	      if (t->vernum == 0)
   6963 		continue;
   6964 
   6965 	      cdeps = 0;
   6966 	      for (n = t->deps; n != NULL; n = n->next)
   6967 		++cdeps;
   6968 
   6969 	      /* Add a symbol representing this version.  */
   6970 	      bh = NULL;
   6971 	      if (! (_bfd_generic_link_add_one_symbol
   6972 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   6973 		      0, NULL, false,
   6974 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6975 		return false;
   6976 	      h = (struct elf_link_hash_entry *) bh;
   6977 	      h->non_elf = 0;
   6978 	      h->def_regular = 1;
   6979 	      h->type = STT_OBJECT;
   6980 	      h->verinfo.vertree = t;
   6981 
   6982 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6983 		return false;
   6984 
   6985 	      def.vd_version = VER_DEF_CURRENT;
   6986 	      def.vd_flags = 0;
   6987 	      if (t->globals.list == NULL
   6988 		  && t->locals.list == NULL
   6989 		  && ! t->used)
   6990 		def.vd_flags |= VER_FLG_WEAK;
   6991 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   6992 	      def.vd_cnt = cdeps + 1;
   6993 	      def.vd_hash = bfd_elf_hash (t->name);
   6994 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6995 	      def.vd_next = 0;
   6996 
   6997 	      /* If a basever node is next, it *must* be the last node in
   6998 		 the chain, otherwise Verdef construction breaks.  */
   6999 	      if (t->next != NULL && t->next->vernum == 0)
   7000 		BFD_ASSERT (t->next->next == NULL);
   7001 
   7002 	      if (t->next != NULL && t->next->vernum != 0)
   7003 		def.vd_next = (sizeof (Elf_External_Verdef)
   7004 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   7005 
   7006 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   7007 					(Elf_External_Verdef *) p);
   7008 	      p += sizeof (Elf_External_Verdef);
   7009 
   7010 	      defaux.vda_name = h->dynstr_index;
   7011 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7012 				      h->dynstr_index);
   7013 	      defaux.vda_next = 0;
   7014 	      if (t->deps != NULL)
   7015 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   7016 	      t->name_indx = defaux.vda_name;
   7017 
   7018 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7019 					 (Elf_External_Verdaux *) p);
   7020 	      p += sizeof (Elf_External_Verdaux);
   7021 
   7022 	      for (n = t->deps; n != NULL; n = n->next)
   7023 		{
   7024 		  if (n->version_needed == NULL)
   7025 		    {
   7026 		      /* This can happen if there was an error in the
   7027 			 version script.  */
   7028 		      defaux.vda_name = 0;
   7029 		    }
   7030 		  else
   7031 		    {
   7032 		      defaux.vda_name = n->version_needed->name_indx;
   7033 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   7034 					      defaux.vda_name);
   7035 		    }
   7036 		  if (n->next == NULL)
   7037 		    defaux.vda_next = 0;
   7038 		  else
   7039 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   7040 
   7041 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   7042 					     (Elf_External_Verdaux *) p);
   7043 		  p += sizeof (Elf_External_Verdaux);
   7044 		}
   7045 	    }
   7046 
   7047 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   7048 	}
   7049     }
   7050 
   7051   if (info->gc_sections && bed->can_gc_sections)
   7052     {
   7053       struct elf_gc_sweep_symbol_info sweep_info;
   7054 
   7055       /* Remove the symbols that were in the swept sections from the
   7056 	 dynamic symbol table.  */
   7057       sweep_info.info = info;
   7058       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   7059       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   7060 			      &sweep_info);
   7061     }
   7062 
   7063   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7064     {
   7065       asection *s;
   7066       struct elf_find_verdep_info sinfo;
   7067 
   7068       /* Work out the size of the version reference section.  */
   7069 
   7070       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   7071       BFD_ASSERT (s != NULL);
   7072 
   7073       sinfo.info = info;
   7074       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   7075       if (sinfo.vers == 0)
   7076 	sinfo.vers = 1;
   7077       sinfo.failed = false;
   7078 
   7079       elf_link_hash_traverse (elf_hash_table (info),
   7080 			      _bfd_elf_link_find_version_dependencies,
   7081 			      &sinfo);
   7082       if (sinfo.failed)
   7083 	return false;
   7084 
   7085       bed->elf_backend_add_glibc_version_dependency (&sinfo);
   7086       if (sinfo.failed)
   7087 	return false;
   7088 
   7089       if (elf_tdata (output_bfd)->verref == NULL)
   7090 	s->flags |= SEC_EXCLUDE;
   7091       else
   7092 	{
   7093 	  Elf_Internal_Verneed *vn;
   7094 	  unsigned int size;
   7095 	  unsigned int crefs;
   7096 	  bfd_byte *p;
   7097 
   7098 	  /* Build the version dependency section.  */
   7099 	  size = 0;
   7100 	  crefs = 0;
   7101 	  for (vn = elf_tdata (output_bfd)->verref;
   7102 	       vn != NULL;
   7103 	       vn = vn->vn_nextref)
   7104 	    {
   7105 	      Elf_Internal_Vernaux *a;
   7106 
   7107 	      size += sizeof (Elf_External_Verneed);
   7108 	      ++crefs;
   7109 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7110 		size += sizeof (Elf_External_Vernaux);
   7111 	    }
   7112 
   7113 	  s->size = size;
   7114 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7115 	  if (s->contents == NULL)
   7116 	    return false;
   7117 
   7118 	  p = s->contents;
   7119 	  for (vn = elf_tdata (output_bfd)->verref;
   7120 	       vn != NULL;
   7121 	       vn = vn->vn_nextref)
   7122 	    {
   7123 	      unsigned int caux;
   7124 	      Elf_Internal_Vernaux *a;
   7125 	      size_t indx;
   7126 
   7127 	      caux = 0;
   7128 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7129 		++caux;
   7130 
   7131 	      vn->vn_version = VER_NEED_CURRENT;
   7132 	      vn->vn_cnt = caux;
   7133 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7134 					  elf_dt_name (vn->vn_bfd) != NULL
   7135 					  ? elf_dt_name (vn->vn_bfd)
   7136 					  : lbasename (bfd_get_filename
   7137 						       (vn->vn_bfd)),
   7138 					  false);
   7139 	      if (indx == (size_t) -1)
   7140 		return false;
   7141 	      vn->vn_file = indx;
   7142 	      vn->vn_aux = sizeof (Elf_External_Verneed);
   7143 	      if (vn->vn_nextref == NULL)
   7144 		vn->vn_next = 0;
   7145 	      else
   7146 		vn->vn_next = (sizeof (Elf_External_Verneed)
   7147 			       + caux * sizeof (Elf_External_Vernaux));
   7148 
   7149 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
   7150 					 (Elf_External_Verneed *) p);
   7151 	      p += sizeof (Elf_External_Verneed);
   7152 
   7153 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   7154 		{
   7155 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
   7156 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7157 					      a->vna_nodename, false);
   7158 		  if (indx == (size_t) -1)
   7159 		    return false;
   7160 		  a->vna_name = indx;
   7161 		  if (a->vna_nextptr == NULL)
   7162 		    a->vna_next = 0;
   7163 		  else
   7164 		    a->vna_next = sizeof (Elf_External_Vernaux);
   7165 
   7166 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
   7167 					     (Elf_External_Vernaux *) p);
   7168 		  p += sizeof (Elf_External_Vernaux);
   7169 		}
   7170 	    }
   7171 
   7172 	  elf_tdata (output_bfd)->cverrefs = crefs;
   7173 	}
   7174     }
   7175 
   7176   if (bfd_link_relocatable (info)
   7177       && !_bfd_elf_size_group_sections (info))
   7178     return false;
   7179 
   7180   /* Determine any GNU_STACK segment requirements, after the backend
   7181      has had a chance to set a default segment size.  */
   7182   if (info->execstack)
   7183     {
   7184       /* If the user has explicitly requested warnings, then generate one even
   7185 	 though the choice is the result of another command line option.  */
   7186       if (info->warn_execstack == 1)
   7187 	{
   7188 	  if (info->error_execstack)
   7189 	    {
   7190 	      _bfd_error_handler
   7191 		(_("\
   7192 error: creating an executable stack because of -z execstack command line option"));
   7193 	      return false;
   7194 	    }
   7195 
   7196 	  _bfd_error_handler
   7197 	    (_("\
   7198 warning: enabling an executable stack because of -z execstack command line option"));
   7199 	}
   7200 
   7201       elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   7202     }
   7203   else if (info->noexecstack)
   7204     elf_stack_flags (output_bfd) = PF_R | PF_W;
   7205   else
   7206     {
   7207       bfd *inputobj;
   7208       asection *notesec = NULL;
   7209       bfd *noteobj = NULL;
   7210       bfd *emptyobj = NULL;
   7211       int exec = 0;
   7212 
   7213       for (inputobj = info->input_bfds;
   7214 	   inputobj;
   7215 	   inputobj = inputobj->link.next)
   7216 	{
   7217 	  asection *s;
   7218 
   7219 	  if (inputobj->flags
   7220 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   7221 	    continue;
   7222 	  s = inputobj->sections;
   7223 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7224 	    continue;
   7225 
   7226 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   7227 	  if (s)
   7228 	    {
   7229 	      notesec = s;
   7230 	      if (s->flags & SEC_CODE)
   7231 		{
   7232 		  noteobj = inputobj;
   7233 		  exec = PF_X;
   7234 		  /* There is no point in scanning the remaining bfds.  */
   7235 		  break;
   7236 		}
   7237 	    }
   7238 	  else if (bed->default_execstack && info->default_execstack)
   7239 	    {
   7240 	      exec = PF_X;
   7241 	      emptyobj = inputobj;
   7242 	    }
   7243 	}
   7244 
   7245       if (notesec || info->stacksize > 0)
   7246 	{
   7247 	  if (exec)
   7248 	    {
   7249 	      if (info->warn_execstack != 0)
   7250 		{
   7251 		  /* PR 29072: Because an executable stack is a serious
   7252 		     security risk, make sure that the user knows that it is
   7253 		     being enabled despite the fact that it was not requested
   7254 		     on the command line.  */
   7255 		  if (noteobj)
   7256 		    {
   7257 		      if (info->error_execstack)
   7258 			{
   7259 			  _bfd_error_handler (_("\
   7260 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
   7261 					      bfd_get_filename (noteobj));
   7262 			  return false;
   7263 			}
   7264 
   7265 		      _bfd_error_handler (_("\
   7266 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
   7267 		       bfd_get_filename (noteobj));
   7268 		    }
   7269 		  else if (emptyobj)
   7270 		    {
   7271 		      if (info->error_execstack)
   7272 			{
   7273 			  _bfd_error_handler (_("\
   7274 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
   7275 					      bfd_get_filename (emptyobj));
   7276 			  return false;
   7277 			}
   7278 
   7279 		      _bfd_error_handler (_("\
   7280 warning: %s: missing .note.GNU-stack section implies executable stack"),
   7281 					  bfd_get_filename (emptyobj));
   7282 		      _bfd_error_handler (_("\
   7283 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
   7284 		    }
   7285 		}
   7286 	    }
   7287 	  elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   7288 	}
   7289 
   7290       if (notesec && exec && bfd_link_relocatable (info)
   7291 	  && notesec->output_section != bfd_abs_section_ptr)
   7292 	notesec->output_section->flags |= SEC_CODE;
   7293     }
   7294 
   7295   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7296     {
   7297       struct elf_info_failed eif;
   7298       struct elf_link_hash_entry *h;
   7299       asection *dynstr;
   7300       asection *s;
   7301 
   7302       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   7303       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   7304 
   7305       if (info->symbolic)
   7306 	{
   7307 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   7308 	    return false;
   7309 	  info->flags |= DF_SYMBOLIC;
   7310 	}
   7311 
   7312       if (rpath != NULL)
   7313 	{
   7314 	  size_t indx;
   7315 	  bfd_vma tag;
   7316 
   7317 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   7318 				      true);
   7319 	  if (indx == (size_t) -1)
   7320 	    return false;
   7321 
   7322 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   7323 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   7324 	    return false;
   7325 	}
   7326 
   7327       if (filter_shlib != NULL)
   7328 	{
   7329 	  size_t indx;
   7330 
   7331 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7332 				      filter_shlib, true);
   7333 	  if (indx == (size_t) -1
   7334 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   7335 	    return false;
   7336 	}
   7337 
   7338       if (auxiliary_filters != NULL)
   7339 	{
   7340 	  const char * const *p;
   7341 
   7342 	  for (p = auxiliary_filters; *p != NULL; p++)
   7343 	    {
   7344 	      size_t indx;
   7345 
   7346 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   7347 					  *p, true);
   7348 	      if (indx == (size_t) -1
   7349 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   7350 		return false;
   7351 	    }
   7352 	}
   7353 
   7354       if (audit != NULL)
   7355 	{
   7356 	  size_t indx;
   7357 
   7358 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   7359 				      true);
   7360 	  if (indx == (size_t) -1
   7361 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   7362 	    return false;
   7363 	}
   7364 
   7365       if (depaudit != NULL)
   7366 	{
   7367 	  size_t indx;
   7368 
   7369 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   7370 				      true);
   7371 	  if (indx == (size_t) -1
   7372 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   7373 	    return false;
   7374 	}
   7375 
   7376       eif.info = info;
   7377       eif.failed = false;
   7378 
   7379       /* Find all symbols which were defined in a dynamic object and make
   7380 	 the backend pick a reasonable value for them.  */
   7381       elf_link_hash_traverse (elf_hash_table (info),
   7382 			      _bfd_elf_adjust_dynamic_symbol,
   7383 			      &eif);
   7384       if (eif.failed)
   7385 	return false;
   7386 
   7387       /* Add some entries to the .dynamic section.  We fill in some of the
   7388 	 values later, in bfd_elf_final_link, but we must add the entries
   7389 	 now so that we know the final size of the .dynamic section.  */
   7390 
   7391       /* If there are initialization and/or finalization functions to
   7392 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   7393       h = (info->init_function
   7394 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7395 				   info->init_function, false,
   7396 				   false, false)
   7397 	   : NULL);
   7398       if (h != NULL
   7399 	  && (h->ref_regular
   7400 	      || h->def_regular))
   7401 	{
   7402 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   7403 	    return false;
   7404 	}
   7405       h = (info->fini_function
   7406 	   ? elf_link_hash_lookup (elf_hash_table (info),
   7407 				   info->fini_function, false,
   7408 				   false, false)
   7409 	   : NULL);
   7410       if (h != NULL
   7411 	  && (h->ref_regular
   7412 	      || h->def_regular))
   7413 	{
   7414 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   7415 	    return false;
   7416 	}
   7417 
   7418       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   7419       if (s != NULL && s->linker_has_input)
   7420 	{
   7421 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   7422 	  if (! bfd_link_executable (info))
   7423 	    {
   7424 	      bfd *sub;
   7425 	      asection *o;
   7426 
   7427 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   7428 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   7429 		    && (o = sub->sections) != NULL
   7430 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
   7431 		  for (o = sub->sections; o != NULL; o = o->next)
   7432 		    if (elf_section_data (o)->this_hdr.sh_type
   7433 			== SHT_PREINIT_ARRAY)
   7434 		      {
   7435 			_bfd_error_handler
   7436 			  (_("%pB: .preinit_array section is not allowed in DSO"),
   7437 			   sub);
   7438 			break;
   7439 		      }
   7440 
   7441 	      bfd_set_error (bfd_error_nonrepresentable_section);
   7442 	      return false;
   7443 	    }
   7444 
   7445 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   7446 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   7447 	    return false;
   7448 	}
   7449       s = bfd_get_section_by_name (output_bfd, ".init_array");
   7450       if (s != NULL && s->linker_has_input)
   7451 	{
   7452 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   7453 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   7454 	    return false;
   7455 	}
   7456       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   7457       if (s != NULL && s->linker_has_input)
   7458 	{
   7459 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   7460 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   7461 	    return false;
   7462 	}
   7463 
   7464       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   7465       /* If .dynstr is excluded from the link, we don't want any of
   7466 	 these tags.  Strictly, we should be checking each section
   7467 	 individually;  This quick check covers for the case where
   7468 	 someone does a /DISCARD/ : { *(*) }.  */
   7469       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   7470 	{
   7471 	  bfd_size_type strsize;
   7472 
   7473 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7474 	  if ((info->emit_hash
   7475 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   7476 	      || (info->emit_gnu_hash
   7477 		  && (bed->record_xhash_symbol == NULL
   7478 		      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
   7479 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   7480 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   7481 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   7482 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   7483 					      bed->s->sizeof_sym)
   7484 	      || (info->gnu_flags_1
   7485 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
   7486 						  info->gnu_flags_1)))
   7487 	    return false;
   7488 	}
   7489     }
   7490 
   7491   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   7492     return false;
   7493 
   7494   /* The backend must work out the sizes of all the other dynamic
   7495      sections.  */
   7496   if (dynobj != NULL
   7497       && bed->elf_backend_size_dynamic_sections != NULL
   7498       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
   7499     return false;
   7500 
   7501   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7502     {
   7503       if (elf_tdata (output_bfd)->cverdefs)
   7504 	{
   7505 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
   7506 
   7507 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   7508 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
   7509 	    return false;
   7510 	}
   7511 
   7512       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   7513 	{
   7514 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   7515 	    return false;
   7516 	}
   7517       else if (info->flags & DF_BIND_NOW)
   7518 	{
   7519 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   7520 	    return false;
   7521 	}
   7522 
   7523       if (info->flags_1)
   7524 	{
   7525 	  if (bfd_link_executable (info))
   7526 	    info->flags_1 &= ~ (DF_1_INITFIRST
   7527 				| DF_1_NODELETE
   7528 				| DF_1_NOOPEN);
   7529 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   7530 	    return false;
   7531 	}
   7532 
   7533       if (elf_tdata (output_bfd)->cverrefs)
   7534 	{
   7535 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
   7536 
   7537 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   7538 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   7539 	    return false;
   7540 	}
   7541 
   7542       if ((elf_tdata (output_bfd)->cverrefs == 0
   7543 	   && elf_tdata (output_bfd)->cverdefs == 0)
   7544 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
   7545 	{
   7546 	  asection *s;
   7547 
   7548 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   7549 	  s->flags |= SEC_EXCLUDE;
   7550 	}
   7551     }
   7552   return true;
   7553 }
   7554 
   7555 /* Find the first non-excluded output section.  We'll use its
   7556    section symbol for some emitted relocs.  */
   7557 void
   7558 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   7559 {
   7560   asection *s;
   7561   asection *found = NULL;
   7562 
   7563   for (s = output_bfd->sections; s != NULL; s = s->next)
   7564     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7565 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7566       {
   7567 	found = s;
   7568 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7569 	  break;
   7570       }
   7571   elf_hash_table (info)->text_index_section = found;
   7572 }
   7573 
   7574 /* Find two non-excluded output sections, one for code, one for data.
   7575    We'll use their section symbols for some emitted relocs.  */
   7576 void
   7577 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   7578 {
   7579   asection *s;
   7580   asection *found = NULL;
   7581 
   7582   /* Data first, since setting text_index_section changes
   7583      _bfd_elf_omit_section_dynsym_default.  */
   7584   for (s = output_bfd->sections; s != NULL; s = s->next)
   7585     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7586 	&& !(s->flags & SEC_READONLY)
   7587 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7588       {
   7589 	found = s;
   7590 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7591 	  break;
   7592       }
   7593   elf_hash_table (info)->data_index_section = found;
   7594 
   7595   for (s = output_bfd->sections; s != NULL; s = s->next)
   7596     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7597 	&& (s->flags & SEC_READONLY)
   7598 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7599       {
   7600 	found = s;
   7601 	break;
   7602       }
   7603   elf_hash_table (info)->text_index_section = found;
   7604 }
   7605 
   7606 #define GNU_HASH_SECTION_NAME(bed)			    \
   7607   (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
   7608 
   7609 bool
   7610 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   7611 {
   7612   const struct elf_backend_data *bed;
   7613   unsigned long section_sym_count;
   7614   bfd_size_type dynsymcount = 0;
   7615 
   7616   if (!is_elf_hash_table (info->hash))
   7617     return true;
   7618 
   7619   bed = get_elf_backend_data (output_bfd);
   7620   (*bed->elf_backend_init_index_section) (output_bfd, info);
   7621 
   7622   /* Assign dynsym indices.  In a shared library we generate a section
   7623      symbol for each output section, which come first.  Next come all
   7624      of the back-end allocated local dynamic syms, followed by the rest
   7625      of the global symbols.
   7626 
   7627      This is usually not needed for static binaries, however backends
   7628      can request to always do it, e.g. the MIPS backend uses dynamic
   7629      symbol counts to lay out GOT, which will be produced in the
   7630      presence of GOT relocations even in static binaries (holding fixed
   7631      data in that case, to satisfy those relocations).  */
   7632 
   7633   if (elf_hash_table (info)->dynamic_sections_created
   7634       || bed->always_renumber_dynsyms)
   7635     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   7636 						  &section_sym_count);
   7637 
   7638   if (elf_hash_table (info)->dynamic_sections_created)
   7639     {
   7640       bfd *dynobj;
   7641       asection *s;
   7642       unsigned int dtagcount;
   7643 
   7644       dynobj = elf_hash_table (info)->dynobj;
   7645 
   7646       /* Work out the size of the symbol version section.  */
   7647       s = bfd_get_linker_section (dynobj, ".gnu.version");
   7648       BFD_ASSERT (s != NULL);
   7649       if ((s->flags & SEC_EXCLUDE) == 0)
   7650 	{
   7651 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   7652 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7653 	  if (s->contents == NULL)
   7654 	    return false;
   7655 
   7656 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   7657 	    return false;
   7658 	}
   7659 
   7660       /* Set the size of the .dynsym and .hash sections.  We counted
   7661 	 the number of dynamic symbols in elf_link_add_object_symbols.
   7662 	 We will build the contents of .dynsym and .hash when we build
   7663 	 the final symbol table, because until then we do not know the
   7664 	 correct value to give the symbols.  We built the .dynstr
   7665 	 section as we went along in elf_link_add_object_symbols.  */
   7666       s = elf_hash_table (info)->dynsym;
   7667       BFD_ASSERT (s != NULL);
   7668       s->size = dynsymcount * bed->s->sizeof_sym;
   7669 
   7670       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7671       if (s->contents == NULL)
   7672 	return false;
   7673 
   7674       /* The first entry in .dynsym is a dummy symbol.  Clear all the
   7675 	 section syms, in case we don't output them all.  */
   7676       ++section_sym_count;
   7677       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   7678 
   7679       elf_hash_table (info)->bucketcount = 0;
   7680 
   7681       /* Compute the size of the hashing table.  As a side effect this
   7682 	 computes the hash values for all the names we export.  */
   7683       if (info->emit_hash)
   7684 	{
   7685 	  unsigned long int *hashcodes;
   7686 	  struct hash_codes_info hashinf;
   7687 	  bfd_size_type amt;
   7688 	  unsigned long int nsyms;
   7689 	  size_t bucketcount;
   7690 	  size_t hash_entry_size;
   7691 
   7692 	  /* Compute the hash values for all exported symbols.  At the same
   7693 	     time store the values in an array so that we could use them for
   7694 	     optimizations.  */
   7695 	  amt = dynsymcount * sizeof (unsigned long int);
   7696 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   7697 	  if (hashcodes == NULL)
   7698 	    return false;
   7699 	  hashinf.hashcodes = hashcodes;
   7700 	  hashinf.error = false;
   7701 
   7702 	  /* Put all hash values in HASHCODES.  */
   7703 	  elf_link_hash_traverse (elf_hash_table (info),
   7704 				  elf_collect_hash_codes, &hashinf);
   7705 	  if (hashinf.error)
   7706 	    {
   7707 	      free (hashcodes);
   7708 	      return false;
   7709 	    }
   7710 
   7711 	  nsyms = hashinf.hashcodes - hashcodes;
   7712 	  bucketcount
   7713 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   7714 	  free (hashcodes);
   7715 
   7716 	  if (bucketcount == 0 && nsyms > 0)
   7717 	    return false;
   7718 
   7719 	  elf_hash_table (info)->bucketcount = bucketcount;
   7720 
   7721 	  s = bfd_get_linker_section (dynobj, ".hash");
   7722 	  BFD_ASSERT (s != NULL);
   7723 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   7724 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   7725 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7726 	  if (s->contents == NULL)
   7727 	    return false;
   7728 
   7729 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   7730 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   7731 		   s->contents + hash_entry_size);
   7732 	}
   7733 
   7734       if (info->emit_gnu_hash)
   7735 	{
   7736 	  size_t i, cnt;
   7737 	  unsigned char *contents;
   7738 	  struct collect_gnu_hash_codes cinfo;
   7739 	  bfd_size_type amt;
   7740 	  size_t bucketcount;
   7741 
   7742 	  memset (&cinfo, 0, sizeof (cinfo));
   7743 
   7744 	  /* Compute the hash values for all exported symbols.  At the same
   7745 	     time store the values in an array so that we could use them for
   7746 	     optimizations.  */
   7747 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   7748 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   7749 	  if (cinfo.hashcodes == NULL)
   7750 	    return false;
   7751 
   7752 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   7753 	  cinfo.min_dynindx = -1;
   7754 	  cinfo.output_bfd = output_bfd;
   7755 	  cinfo.bed = bed;
   7756 
   7757 	  /* Put all hash values in HASHCODES.  */
   7758 	  elf_link_hash_traverse (elf_hash_table (info),
   7759 				  elf_collect_gnu_hash_codes, &cinfo);
   7760 	  if (cinfo.error)
   7761 	    {
   7762 	      free (cinfo.hashcodes);
   7763 	      return false;
   7764 	    }
   7765 
   7766 	  bucketcount
   7767 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   7768 
   7769 	  if (bucketcount == 0)
   7770 	    {
   7771 	      free (cinfo.hashcodes);
   7772 	      return false;
   7773 	    }
   7774 
   7775 	  s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
   7776 	  BFD_ASSERT (s != NULL);
   7777 
   7778 	  if (cinfo.nsyms == 0)
   7779 	    {
   7780 	      /* Empty .gnu.hash or .MIPS.xhash section is special.  */
   7781 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   7782 	      free (cinfo.hashcodes);
   7783 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   7784 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7785 	      if (contents == NULL)
   7786 		return false;
   7787 	      s->contents = contents;
   7788 	      /* 1 empty bucket.  */
   7789 	      bfd_put_32 (output_bfd, 1, contents);
   7790 	      /* SYMIDX above the special symbol 0.  */
   7791 	      bfd_put_32 (output_bfd, 1, contents + 4);
   7792 	      /* Just one word for bitmask.  */
   7793 	      bfd_put_32 (output_bfd, 1, contents + 8);
   7794 	      /* Only hash fn bloom filter.  */
   7795 	      bfd_put_32 (output_bfd, 0, contents + 12);
   7796 	      /* No hashes are valid - empty bitmask.  */
   7797 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   7798 	      /* No hashes in the only bucket.  */
   7799 	      bfd_put_32 (output_bfd, 0,
   7800 			  contents + 16 + bed->s->arch_size / 8);
   7801 	    }
   7802 	  else
   7803 	    {
   7804 	      unsigned long int maskwords, maskbitslog2, x;
   7805 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   7806 
   7807 	      x = cinfo.nsyms;
   7808 	      maskbitslog2 = 1;
   7809 	      while ((x >>= 1) != 0)
   7810 		++maskbitslog2;
   7811 	      if (maskbitslog2 < 3)
   7812 		maskbitslog2 = 5;
   7813 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   7814 		maskbitslog2 = maskbitslog2 + 3;
   7815 	      else
   7816 		maskbitslog2 = maskbitslog2 + 2;
   7817 	      if (bed->s->arch_size == 64)
   7818 		{
   7819 		  if (maskbitslog2 == 5)
   7820 		    maskbitslog2 = 6;
   7821 		  cinfo.shift1 = 6;
   7822 		}
   7823 	      else
   7824 		cinfo.shift1 = 5;
   7825 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   7826 	      cinfo.shift2 = maskbitslog2;
   7827 	      cinfo.maskbits = 1 << maskbitslog2;
   7828 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   7829 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   7830 	      amt += maskwords * sizeof (bfd_vma);
   7831 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   7832 	      if (cinfo.bitmask == NULL)
   7833 		{
   7834 		  free (cinfo.hashcodes);
   7835 		  return false;
   7836 		}
   7837 
   7838 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   7839 	      cinfo.indx = cinfo.counts + bucketcount;
   7840 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   7841 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   7842 
   7843 	      /* Determine how often each hash bucket is used.  */
   7844 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   7845 	      for (i = 0; i < cinfo.nsyms; ++i)
   7846 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   7847 
   7848 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   7849 		if (cinfo.counts[i] != 0)
   7850 		  {
   7851 		    cinfo.indx[i] = cnt;
   7852 		    cnt += cinfo.counts[i];
   7853 		  }
   7854 	      BFD_ASSERT (cnt == dynsymcount);
   7855 	      cinfo.bucketcount = bucketcount;
   7856 	      cinfo.local_indx = cinfo.min_dynindx;
   7857 
   7858 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   7859 	      s->size += cinfo.maskbits / 8;
   7860 	      if (bed->record_xhash_symbol != NULL)
   7861 		s->size += cinfo.nsyms * 4;
   7862 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7863 	      if (contents == NULL)
   7864 		{
   7865 		  free (cinfo.bitmask);
   7866 		  free (cinfo.hashcodes);
   7867 		  return false;
   7868 		}
   7869 
   7870 	      s->contents = contents;
   7871 	      bfd_put_32 (output_bfd, bucketcount, contents);
   7872 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   7873 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   7874 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   7875 	      contents += 16 + cinfo.maskbits / 8;
   7876 
   7877 	      for (i = 0; i < bucketcount; ++i)
   7878 		{
   7879 		  if (cinfo.counts[i] == 0)
   7880 		    bfd_put_32 (output_bfd, 0, contents);
   7881 		  else
   7882 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   7883 		  contents += 4;
   7884 		}
   7885 
   7886 	      cinfo.contents = contents;
   7887 
   7888 	      cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
   7889 	      /* Renumber dynamic symbols, if populating .gnu.hash section.
   7890 		 If using .MIPS.xhash, populate the translation table.  */
   7891 	      elf_link_hash_traverse (elf_hash_table (info),
   7892 				      elf_gnu_hash_process_symidx, &cinfo);
   7893 
   7894 	      contents = s->contents + 16;
   7895 	      for (i = 0; i < maskwords; ++i)
   7896 		{
   7897 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   7898 			   contents);
   7899 		  contents += bed->s->arch_size / 8;
   7900 		}
   7901 
   7902 	      free (cinfo.bitmask);
   7903 	      free (cinfo.hashcodes);
   7904 	    }
   7905 	}
   7906 
   7907       s = bfd_get_linker_section (dynobj, ".dynstr");
   7908       BFD_ASSERT (s != NULL);
   7909 
   7910       elf_finalize_dynstr (output_bfd, info);
   7911 
   7912       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7913 
   7914       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   7915 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   7916 	  return false;
   7917     }
   7918 
   7919   return true;
   7920 }
   7921 
   7922 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   7924 
   7925 static void
   7926 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   7927 			    asection *sec)
   7928 {
   7929   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   7930   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   7931 }
   7932 
   7933 /* Finish SHF_MERGE section merging.  */
   7934 
   7935 bool
   7936 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
   7937 {
   7938   bfd *ibfd;
   7939   asection *sec;
   7940 
   7941   if (!is_elf_hash_table (info->hash))
   7942     return false;
   7943 
   7944   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7945     if ((ibfd->flags & DYNAMIC) == 0
   7946 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   7947 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   7948 	    == get_elf_backend_data (obfd)->s->elfclass))
   7949       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7950 	if ((sec->flags & SEC_MERGE) != 0
   7951 	    && !bfd_is_abs_section (sec->output_section))
   7952 	  {
   7953 	    struct bfd_elf_section_data *secdata;
   7954 
   7955 	    secdata = elf_section_data (sec);
   7956 	    if (! _bfd_add_merge_section (obfd,
   7957 					  &elf_hash_table (info)->merge_info,
   7958 					  sec, &secdata->sec_info))
   7959 	      return false;
   7960 	    else if (secdata->sec_info)
   7961 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   7962 	  }
   7963 
   7964   if (elf_hash_table (info)->merge_info != NULL)
   7965     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
   7966 			 merge_sections_remove_hook);
   7967   return true;
   7968 }
   7969 
   7970 /* Create an entry in an ELF linker hash table.  */
   7971 
   7972 struct bfd_hash_entry *
   7973 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   7974 			    struct bfd_hash_table *table,
   7975 			    const char *string)
   7976 {
   7977   /* Allocate the structure if it has not already been allocated by a
   7978      subclass.  */
   7979   if (entry == NULL)
   7980     {
   7981       entry = (struct bfd_hash_entry *)
   7982 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   7983       if (entry == NULL)
   7984 	return entry;
   7985     }
   7986 
   7987   /* Call the allocation method of the superclass.  */
   7988   entry = _bfd_link_hash_newfunc (entry, table, string);
   7989   if (entry != NULL)
   7990     {
   7991       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   7992       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   7993 
   7994       /* Set local fields.  */
   7995       ret->indx = -1;
   7996       ret->dynindx = -1;
   7997       ret->got = htab->init_got_refcount;
   7998       ret->plt = htab->init_plt_refcount;
   7999       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   8000 			      - offsetof (struct elf_link_hash_entry, size)));
   8001       /* Assume that we have been called by a non-ELF symbol reader.
   8002 	 This flag is then reset by the code which reads an ELF input
   8003 	 file.  This ensures that a symbol created by a non-ELF symbol
   8004 	 reader will have the flag set correctly.  */
   8005       ret->non_elf = 1;
   8006     }
   8007 
   8008   return entry;
   8009 }
   8010 
   8011 /* Copy data from an indirect symbol to its direct symbol, hiding the
   8012    old indirect symbol.  Also used for copying flags to a weakdef.  */
   8013 
   8014 void
   8015 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   8016 				  struct elf_link_hash_entry *dir,
   8017 				  struct elf_link_hash_entry *ind)
   8018 {
   8019   struct elf_link_hash_table *htab;
   8020 
   8021   if (ind->dyn_relocs != NULL)
   8022     {
   8023       if (dir->dyn_relocs != NULL)
   8024 	{
   8025 	  struct elf_dyn_relocs **pp;
   8026 	  struct elf_dyn_relocs *p;
   8027 
   8028 	  /* Add reloc counts against the indirect sym to the direct sym
   8029 	     list.  Merge any entries against the same section.  */
   8030 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   8031 	    {
   8032 	      struct elf_dyn_relocs *q;
   8033 
   8034 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   8035 		if (q->sec == p->sec)
   8036 		  {
   8037 		    q->pc_count += p->pc_count;
   8038 		    q->count += p->count;
   8039 		    *pp = p->next;
   8040 		    break;
   8041 		  }
   8042 	      if (q == NULL)
   8043 		pp = &p->next;
   8044 	    }
   8045 	  *pp = dir->dyn_relocs;
   8046 	}
   8047 
   8048       dir->dyn_relocs = ind->dyn_relocs;
   8049       ind->dyn_relocs = NULL;
   8050     }
   8051 
   8052   /* Copy down any references that we may have already seen to the
   8053      symbol which just became indirect.  */
   8054 
   8055   if (dir->versioned != versioned_hidden)
   8056     dir->ref_dynamic |= ind->ref_dynamic;
   8057   dir->ref_regular |= ind->ref_regular;
   8058   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   8059   dir->non_got_ref |= ind->non_got_ref;
   8060   dir->needs_plt |= ind->needs_plt;
   8061   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   8062 
   8063   if (ind->root.type != bfd_link_hash_indirect)
   8064     return;
   8065 
   8066   /* Copy over the global and procedure linkage table refcount entries.
   8067      These may have been already set up by a check_relocs routine.  */
   8068   htab = elf_hash_table (info);
   8069   if (ind->got.refcount > htab->init_got_refcount.refcount)
   8070     {
   8071       if (dir->got.refcount < 0)
   8072 	dir->got.refcount = 0;
   8073       dir->got.refcount += ind->got.refcount;
   8074       ind->got.refcount = htab->init_got_refcount.refcount;
   8075     }
   8076 
   8077   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   8078     {
   8079       if (dir->plt.refcount < 0)
   8080 	dir->plt.refcount = 0;
   8081       dir->plt.refcount += ind->plt.refcount;
   8082       ind->plt.refcount = htab->init_plt_refcount.refcount;
   8083     }
   8084 
   8085   if (ind->dynindx != -1)
   8086     {
   8087       if (dir->dynindx != -1)
   8088 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   8089       dir->dynindx = ind->dynindx;
   8090       dir->dynstr_index = ind->dynstr_index;
   8091       ind->dynindx = -1;
   8092       ind->dynstr_index = 0;
   8093     }
   8094 }
   8095 
   8096 void
   8097 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   8098 				struct elf_link_hash_entry *h,
   8099 				bool force_local)
   8100 {
   8101   /* STT_GNU_IFUNC symbol must go through PLT.  */
   8102   if (h->type != STT_GNU_IFUNC)
   8103     {
   8104       h->plt = elf_hash_table (info)->init_plt_offset;
   8105       h->needs_plt = 0;
   8106     }
   8107   if (force_local)
   8108     {
   8109       h->forced_local = 1;
   8110       if (h->dynindx != -1)
   8111 	{
   8112 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8113 				  h->dynstr_index);
   8114 	  h->dynindx = -1;
   8115 	  h->dynstr_index = 0;
   8116 	}
   8117     }
   8118 }
   8119 
   8120 /* Hide a symbol. */
   8121 
   8122 void
   8123 _bfd_elf_link_hide_symbol (bfd *output_bfd,
   8124 			   struct bfd_link_info *info,
   8125 			   struct bfd_link_hash_entry *h)
   8126 {
   8127   if (is_elf_hash_table (info->hash))
   8128     {
   8129       const struct elf_backend_data *bed
   8130 	= get_elf_backend_data (output_bfd);
   8131       struct elf_link_hash_entry *eh
   8132 	= (struct elf_link_hash_entry *) h;
   8133       bed->elf_backend_hide_symbol (info, eh, true);
   8134       eh->def_dynamic = 0;
   8135       eh->ref_dynamic = 0;
   8136       eh->dynamic_def = 0;
   8137     }
   8138 }
   8139 
   8140 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   8141    caller.  */
   8142 
   8143 bool
   8144 _bfd_elf_link_hash_table_init
   8145   (struct elf_link_hash_table *table,
   8146    bfd *abfd,
   8147    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   8148 				      struct bfd_hash_table *,
   8149 				      const char *),
   8150    unsigned int entsize,
   8151    enum elf_target_id target_id)
   8152 {
   8153   bool ret;
   8154   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
   8155 
   8156   table->init_got_refcount.refcount = can_refcount - 1;
   8157   table->init_plt_refcount.refcount = can_refcount - 1;
   8158   table->init_got_offset.offset = -(bfd_vma) 1;
   8159   table->init_plt_offset.offset = -(bfd_vma) 1;
   8160   /* The first dynamic symbol is a dummy.  */
   8161   table->dynsymcount = 1;
   8162 
   8163   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   8164 
   8165   table->root.type = bfd_link_elf_hash_table;
   8166   table->hash_table_id = target_id;
   8167   table->target_os = get_elf_backend_data (abfd)->target_os;
   8168 
   8169   return ret;
   8170 }
   8171 
   8172 /* Create an ELF linker hash table.  */
   8173 
   8174 struct bfd_link_hash_table *
   8175 _bfd_elf_link_hash_table_create (bfd *abfd)
   8176 {
   8177   struct elf_link_hash_table *ret;
   8178   size_t amt = sizeof (struct elf_link_hash_table);
   8179 
   8180   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   8181   if (ret == NULL)
   8182     return NULL;
   8183 
   8184   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   8185 				       sizeof (struct elf_link_hash_entry),
   8186 				       GENERIC_ELF_DATA))
   8187     {
   8188       free (ret);
   8189       return NULL;
   8190     }
   8191   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
   8192 
   8193   return &ret->root;
   8194 }
   8195 
   8196 /* Destroy an ELF linker hash table.  */
   8197 
   8198 void
   8199 _bfd_elf_link_hash_table_free (bfd *obfd)
   8200 {
   8201   struct elf_link_hash_table *htab;
   8202 
   8203   htab = (struct elf_link_hash_table *) obfd->link.hash;
   8204   if (htab->dynstr != NULL)
   8205     _bfd_elf_strtab_free (htab->dynstr);
   8206   _bfd_merge_sections_free (htab->merge_info);
   8207   _bfd_generic_link_hash_table_free (obfd);
   8208 }
   8209 
   8210 /* This is a hook for the ELF emulation code in the generic linker to
   8211    tell the backend linker what file name to use for the DT_NEEDED
   8212    entry for a dynamic object.  */
   8213 
   8214 void
   8215 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   8216 {
   8217   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8218       && bfd_get_format (abfd) == bfd_object)
   8219     elf_dt_name (abfd) = name;
   8220 }
   8221 
   8222 int
   8223 bfd_elf_get_dyn_lib_class (bfd *abfd)
   8224 {
   8225   int lib_class;
   8226   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8227       && bfd_get_format (abfd) == bfd_object)
   8228     lib_class = elf_dyn_lib_class (abfd);
   8229   else
   8230     lib_class = 0;
   8231   return lib_class;
   8232 }
   8233 
   8234 void
   8235 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   8236 {
   8237   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8238       && bfd_get_format (abfd) == bfd_object)
   8239     elf_dyn_lib_class (abfd) = lib_class;
   8240 }
   8241 
   8242 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   8243    the linker ELF emulation code.  */
   8244 
   8245 struct bfd_link_needed_list *
   8246 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   8247 			 struct bfd_link_info *info)
   8248 {
   8249   if (! is_elf_hash_table (info->hash))
   8250     return NULL;
   8251   return elf_hash_table (info)->needed;
   8252 }
   8253 
   8254 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   8255    hook for the linker ELF emulation code.  */
   8256 
   8257 struct bfd_link_needed_list *
   8258 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   8259 			  struct bfd_link_info *info)
   8260 {
   8261   if (! is_elf_hash_table (info->hash))
   8262     return NULL;
   8263   return elf_hash_table (info)->runpath;
   8264 }
   8265 
   8266 /* Get the name actually used for a dynamic object for a link.  This
   8267    is the SONAME entry if there is one.  Otherwise, it is the string
   8268    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   8269 
   8270 const char *
   8271 bfd_elf_get_dt_soname (bfd *abfd)
   8272 {
   8273   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   8274       && bfd_get_format (abfd) == bfd_object)
   8275     return elf_dt_name (abfd);
   8276   return NULL;
   8277 }
   8278 
   8279 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   8280    the ELF linker emulation code.  */
   8281 
   8282 bool
   8283 bfd_elf_get_bfd_needed_list (bfd *abfd,
   8284 			     struct bfd_link_needed_list **pneeded)
   8285 {
   8286   asection *s;
   8287   bfd_byte *dynbuf = NULL;
   8288   unsigned int elfsec;
   8289   unsigned long shlink;
   8290   bfd_byte *extdyn, *extdynend;
   8291   size_t extdynsize;
   8292   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   8293 
   8294   *pneeded = NULL;
   8295 
   8296   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   8297       || bfd_get_format (abfd) != bfd_object)
   8298     return true;
   8299 
   8300   s = bfd_get_section_by_name (abfd, ".dynamic");
   8301   if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
   8302     return true;
   8303 
   8304   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   8305     goto error_return;
   8306 
   8307   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   8308   if (elfsec == SHN_BAD)
   8309     goto error_return;
   8310 
   8311   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   8312 
   8313   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   8314   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   8315 
   8316   for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   8317        (size_t) (extdynend - extdyn) >= extdynsize;
   8318        extdyn += extdynsize)
   8319     {
   8320       Elf_Internal_Dyn dyn;
   8321 
   8322       (*swap_dyn_in) (abfd, extdyn, &dyn);
   8323 
   8324       if (dyn.d_tag == DT_NULL)
   8325 	break;
   8326 
   8327       if (dyn.d_tag == DT_NEEDED)
   8328 	{
   8329 	  const char *string;
   8330 	  struct bfd_link_needed_list *l;
   8331 	  unsigned int tagv = dyn.d_un.d_val;
   8332 	  size_t amt;
   8333 
   8334 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   8335 	  if (string == NULL)
   8336 	    goto error_return;
   8337 
   8338 	  amt = sizeof *l;
   8339 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   8340 	  if (l == NULL)
   8341 	    goto error_return;
   8342 
   8343 	  l->by = abfd;
   8344 	  l->name = string;
   8345 	  l->next = *pneeded;
   8346 	  *pneeded = l;
   8347 	}
   8348     }
   8349 
   8350   free (dynbuf);
   8351 
   8352   return true;
   8353 
   8354  error_return:
   8355   free (dynbuf);
   8356   return false;
   8357 }
   8358 
   8359 struct elf_symbuf_symbol
   8360 {
   8361   unsigned long st_name;	/* Symbol name, index in string tbl */
   8362   unsigned char st_info;	/* Type and binding attributes */
   8363   unsigned char st_other;	/* Visibilty, and target specific */
   8364 };
   8365 
   8366 struct elf_symbuf_head
   8367 {
   8368   struct elf_symbuf_symbol *ssym;
   8369   size_t count;
   8370   unsigned int st_shndx;
   8371 };
   8372 
   8373 struct elf_symbol
   8374 {
   8375   union
   8376     {
   8377       Elf_Internal_Sym *isym;
   8378       struct elf_symbuf_symbol *ssym;
   8379       void *p;
   8380     } u;
   8381   const char *name;
   8382 };
   8383 
   8384 /* Sort references to symbols by ascending section number.  */
   8385 
   8386 static int
   8387 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   8388 {
   8389   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   8390   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   8391 
   8392   if (s1->st_shndx != s2->st_shndx)
   8393     return s1->st_shndx > s2->st_shndx ? 1 : -1;
   8394   /* Final sort by the address of the sym in the symbuf ensures
   8395      a stable sort.  */
   8396   if (s1 != s2)
   8397     return s1 > s2 ? 1 : -1;
   8398   return 0;
   8399 }
   8400 
   8401 static int
   8402 elf_sym_name_compare (const void *arg1, const void *arg2)
   8403 {
   8404   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   8405   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   8406   int ret = strcmp (s1->name, s2->name);
   8407   if (ret != 0)
   8408     return ret;
   8409   if (s1->u.p != s2->u.p)
   8410     return s1->u.p > s2->u.p ? 1 : -1;
   8411   return 0;
   8412 }
   8413 
   8414 static struct elf_symbuf_head *
   8415 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
   8416 {
   8417   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   8418   struct elf_symbuf_symbol *ssym;
   8419   struct elf_symbuf_head *ssymbuf, *ssymhead;
   8420   size_t i, shndx_count, total_size, amt;
   8421 
   8422   amt = symcount * sizeof (*indbuf);
   8423   indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
   8424   if (indbuf == NULL)
   8425     return NULL;
   8426 
   8427   for (ind = indbuf, i = 0; i < symcount; i++)
   8428     if (isymbuf[i].st_shndx != SHN_UNDEF)
   8429       *ind++ = &isymbuf[i];
   8430   indbufend = ind;
   8431 
   8432   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   8433 	 elf_sort_elf_symbol);
   8434 
   8435   shndx_count = 0;
   8436   if (indbufend > indbuf)
   8437     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   8438       if (ind[0]->st_shndx != ind[1]->st_shndx)
   8439 	shndx_count++;
   8440 
   8441   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   8442 		+ (indbufend - indbuf) * sizeof (*ssym));
   8443   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   8444   if (ssymbuf == NULL)
   8445     {
   8446       free (indbuf);
   8447       return NULL;
   8448     }
   8449 
   8450   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   8451   ssymbuf->ssym = NULL;
   8452   ssymbuf->count = shndx_count;
   8453   ssymbuf->st_shndx = 0;
   8454   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   8455     {
   8456       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   8457 	{
   8458 	  ssymhead++;
   8459 	  ssymhead->ssym = ssym;
   8460 	  ssymhead->count = 0;
   8461 	  ssymhead->st_shndx = (*ind)->st_shndx;
   8462 	}
   8463       ssym->st_name = (*ind)->st_name;
   8464       ssym->st_info = (*ind)->st_info;
   8465       ssym->st_other = (*ind)->st_other;
   8466       ssymhead->count++;
   8467     }
   8468   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
   8469 	      && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
   8470 
   8471   free (indbuf);
   8472   return ssymbuf;
   8473 }
   8474 
   8475 /* Check if 2 sections define the same set of local and global
   8476    symbols.  */
   8477 
   8478 static bool
   8479 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   8480 				   struct bfd_link_info *info)
   8481 {
   8482   bfd *bfd1, *bfd2;
   8483   const struct elf_backend_data *bed1, *bed2;
   8484   Elf_Internal_Shdr *hdr1, *hdr2;
   8485   size_t symcount1, symcount2;
   8486   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   8487   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   8488   Elf_Internal_Sym *isym, *isymend;
   8489   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   8490   size_t count1, count2, sec_count1, sec_count2, i;
   8491   unsigned int shndx1, shndx2;
   8492   bool result;
   8493   bool ignore_section_symbol_p;
   8494 
   8495   bfd1 = sec1->owner;
   8496   bfd2 = sec2->owner;
   8497 
   8498   /* Both sections have to be in ELF.  */
   8499   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   8500       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   8501     return false;
   8502 
   8503   if (elf_section_type (sec1) != elf_section_type (sec2))
   8504     return false;
   8505 
   8506   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   8507   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   8508   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   8509     return false;
   8510 
   8511   bed1 = get_elf_backend_data (bfd1);
   8512   bed2 = get_elf_backend_data (bfd2);
   8513   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   8514   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   8515   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   8516   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   8517 
   8518   if (symcount1 == 0 || symcount2 == 0)
   8519     return false;
   8520 
   8521   result = false;
   8522   isymbuf1 = NULL;
   8523   isymbuf2 = NULL;
   8524   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   8525   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   8526 
   8527   /* Ignore section symbols only when matching non-debugging sections
   8528      or linkonce section with comdat section.  */
   8529   ignore_section_symbol_p
   8530     = ((sec1->flags & SEC_DEBUGGING) == 0
   8531        || ((elf_section_flags (sec1) & SHF_GROUP)
   8532 	   != (elf_section_flags (sec2) & SHF_GROUP)));
   8533 
   8534   if (ssymbuf1 == NULL)
   8535     {
   8536       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   8537 				       NULL, NULL, NULL);
   8538       if (isymbuf1 == NULL)
   8539 	goto done;
   8540 
   8541       if (info != NULL && !info->reduce_memory_overheads)
   8542 	{
   8543 	  ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
   8544 	  elf_tdata (bfd1)->symbuf = ssymbuf1;
   8545 	}
   8546     }
   8547 
   8548   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   8549     {
   8550       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   8551 				       NULL, NULL, NULL);
   8552       if (isymbuf2 == NULL)
   8553 	goto done;
   8554 
   8555       if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
   8556 	{
   8557 	  ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
   8558 	  elf_tdata (bfd2)->symbuf = ssymbuf2;
   8559 	}
   8560     }
   8561 
   8562   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   8563     {
   8564       /* Optimized faster version.  */
   8565       size_t lo, hi, mid;
   8566       struct elf_symbol *symp;
   8567       struct elf_symbuf_symbol *ssym, *ssymend;
   8568 
   8569       lo = 0;
   8570       hi = ssymbuf1->count;
   8571       ssymbuf1++;
   8572       count1 = 0;
   8573       sec_count1 = 0;
   8574       while (lo < hi)
   8575 	{
   8576 	  mid = (lo + hi) / 2;
   8577 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   8578 	    hi = mid;
   8579 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   8580 	    lo = mid + 1;
   8581 	  else
   8582 	    {
   8583 	      count1 = ssymbuf1[mid].count;
   8584 	      ssymbuf1 += mid;
   8585 	      break;
   8586 	    }
   8587 	}
   8588       if (ignore_section_symbol_p)
   8589 	{
   8590 	  for (i = 0; i < count1; i++)
   8591 	    if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
   8592 	      sec_count1++;
   8593 	  count1 -= sec_count1;
   8594 	}
   8595 
   8596       lo = 0;
   8597       hi = ssymbuf2->count;
   8598       ssymbuf2++;
   8599       count2 = 0;
   8600       sec_count2 = 0;
   8601       while (lo < hi)
   8602 	{
   8603 	  mid = (lo + hi) / 2;
   8604 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   8605 	    hi = mid;
   8606 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   8607 	    lo = mid + 1;
   8608 	  else
   8609 	    {
   8610 	      count2 = ssymbuf2[mid].count;
   8611 	      ssymbuf2 += mid;
   8612 	      break;
   8613 	    }
   8614 	}
   8615       if (ignore_section_symbol_p)
   8616 	{
   8617 	  for (i = 0; i < count2; i++)
   8618 	    if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
   8619 	      sec_count2++;
   8620 	  count2 -= sec_count2;
   8621 	}
   8622 
   8623       if (count1 == 0 || count2 == 0 || count1 != count2)
   8624 	goto done;
   8625 
   8626       symtable1
   8627 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   8628       symtable2
   8629 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   8630       if (symtable1 == NULL || symtable2 == NULL)
   8631 	goto done;
   8632 
   8633       symp = symtable1;
   8634       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
   8635 	   ssym < ssymend; ssym++)
   8636 	if (sec_count1 == 0
   8637 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8638 	  {
   8639 	    symp->u.ssym = ssym;
   8640 	    symp->name = bfd_elf_string_from_elf_section (bfd1,
   8641 							  hdr1->sh_link,
   8642 							  ssym->st_name);
   8643 	    symp++;
   8644 	  }
   8645 
   8646       symp = symtable2;
   8647       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
   8648 	   ssym < ssymend; ssym++)
   8649 	if (sec_count2 == 0
   8650 	    || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
   8651 	  {
   8652 	    symp->u.ssym = ssym;
   8653 	    symp->name = bfd_elf_string_from_elf_section (bfd2,
   8654 							  hdr2->sh_link,
   8655 							  ssym->st_name);
   8656 	    symp++;
   8657 	  }
   8658 
   8659       /* Sort symbol by name.  */
   8660       qsort (symtable1, count1, sizeof (struct elf_symbol),
   8661 	     elf_sym_name_compare);
   8662       qsort (symtable2, count1, sizeof (struct elf_symbol),
   8663 	     elf_sym_name_compare);
   8664 
   8665       for (i = 0; i < count1; i++)
   8666 	/* Two symbols must have the same binding, type and name.  */
   8667 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   8668 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   8669 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8670 	  goto done;
   8671 
   8672       result = true;
   8673       goto done;
   8674     }
   8675 
   8676   symtable1 = (struct elf_symbol *)
   8677       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   8678   symtable2 = (struct elf_symbol *)
   8679       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   8680   if (symtable1 == NULL || symtable2 == NULL)
   8681     goto done;
   8682 
   8683   /* Count definitions in the section.  */
   8684   count1 = 0;
   8685   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   8686     if (isym->st_shndx == shndx1
   8687 	&& (!ignore_section_symbol_p
   8688 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8689       symtable1[count1++].u.isym = isym;
   8690 
   8691   count2 = 0;
   8692   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   8693     if (isym->st_shndx == shndx2
   8694 	&& (!ignore_section_symbol_p
   8695 	    || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
   8696       symtable2[count2++].u.isym = isym;
   8697 
   8698   if (count1 == 0 || count2 == 0 || count1 != count2)
   8699     goto done;
   8700 
   8701   for (i = 0; i < count1; i++)
   8702     symtable1[i].name
   8703       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   8704 					 symtable1[i].u.isym->st_name);
   8705 
   8706   for (i = 0; i < count2; i++)
   8707     symtable2[i].name
   8708       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   8709 					 symtable2[i].u.isym->st_name);
   8710 
   8711   /* Sort symbol by name.  */
   8712   qsort (symtable1, count1, sizeof (struct elf_symbol),
   8713 	 elf_sym_name_compare);
   8714   qsort (symtable2, count1, sizeof (struct elf_symbol),
   8715 	 elf_sym_name_compare);
   8716 
   8717   for (i = 0; i < count1; i++)
   8718     /* Two symbols must have the same binding, type and name.  */
   8719     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   8720 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   8721 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8722       goto done;
   8723 
   8724   result = true;
   8725 
   8726  done:
   8727   free (symtable1);
   8728   free (symtable2);
   8729   free (isymbuf1);
   8730   free (isymbuf2);
   8731 
   8732   return result;
   8733 }
   8734 
   8735 /* Return TRUE if 2 section types are compatible.  */
   8736 
   8737 bool
   8738 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   8739 				 bfd *bbfd, const asection *bsec)
   8740 {
   8741   if (asec == NULL
   8742       || bsec == NULL
   8743       || abfd->xvec->flavour != bfd_target_elf_flavour
   8744       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   8745     return true;
   8746 
   8747   return elf_section_type (asec) == elf_section_type (bsec);
   8748 }
   8749 
   8750 /* Final phase of ELF linker.  */
   8752 
   8753 /* A structure we use to avoid passing large numbers of arguments.  */
   8754 
   8755 struct elf_final_link_info
   8756 {
   8757   /* General link information.  */
   8758   struct bfd_link_info *info;
   8759   /* Output BFD.  */
   8760   bfd *output_bfd;
   8761   /* Symbol string table.  */
   8762   struct elf_strtab_hash *symstrtab;
   8763   /* .hash section.  */
   8764   asection *hash_sec;
   8765   /* symbol version section (.gnu.version).  */
   8766   asection *symver_sec;
   8767   /* Buffer large enough to hold contents of any section.  */
   8768   bfd_byte *contents;
   8769   /* Buffer large enough to hold external relocs of any section.  */
   8770   void *external_relocs;
   8771   /* Buffer large enough to hold internal relocs of any section.  */
   8772   Elf_Internal_Rela *internal_relocs;
   8773   /* Buffer large enough to hold external local symbols of any input
   8774      BFD.  */
   8775   bfd_byte *external_syms;
   8776   /* And a buffer for symbol section indices.  */
   8777   Elf_External_Sym_Shndx *locsym_shndx;
   8778   /* Buffer large enough to hold internal local symbols of any input
   8779      BFD.  */
   8780   Elf_Internal_Sym *internal_syms;
   8781   /* Array large enough to hold a symbol index for each local symbol
   8782      of any input BFD.  */
   8783   long *indices;
   8784   /* Array large enough to hold a section pointer for each local
   8785      symbol of any input BFD.  */
   8786   asection **sections;
   8787   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   8788   Elf_External_Sym_Shndx *symshndxbuf;
   8789   /* Number of STT_FILE syms seen.  */
   8790   size_t filesym_count;
   8791   /* Local symbol hash table.  */
   8792   struct bfd_hash_table local_hash_table;
   8793 };
   8794 
   8795 struct local_hash_entry
   8796 {
   8797   /* Base hash table entry structure.  */
   8798   struct bfd_hash_entry root;
   8799   /* Size of the local symbol name.  */
   8800   size_t size;
   8801   /* Number of the duplicated local symbol names.  */
   8802   long count;
   8803 };
   8804 
   8805 /* Create an entry in the local symbol hash table.  */
   8806 
   8807 static struct bfd_hash_entry *
   8808 local_hash_newfunc (struct bfd_hash_entry *entry,
   8809 		    struct bfd_hash_table *table,
   8810 		    const char *string)
   8811 {
   8812 
   8813   /* Allocate the structure if it has not already been allocated by a
   8814      subclass.  */
   8815   if (entry == NULL)
   8816     {
   8817       entry = bfd_hash_allocate (table,
   8818 				 sizeof (struct local_hash_entry));
   8819       if (entry == NULL)
   8820         return entry;
   8821     }
   8822 
   8823   /* Call the allocation method of the superclass.  */
   8824   entry = bfd_hash_newfunc (entry, table, string);
   8825   if (entry != NULL)
   8826     {
   8827       ((struct local_hash_entry *) entry)->count = 0;
   8828       ((struct local_hash_entry *) entry)->size = 0;
   8829     }
   8830 
   8831   return entry;
   8832 }
   8833 
   8834 /* This struct is used to pass information to elf_link_output_extsym.  */
   8835 
   8836 struct elf_outext_info
   8837 {
   8838   bool failed;
   8839   bool localsyms;
   8840   bool file_sym_done;
   8841   struct elf_final_link_info *flinfo;
   8842 };
   8843 
   8844 
   8845 /* Support for evaluating a complex relocation.
   8846 
   8847    Complex relocations are generalized, self-describing relocations.  The
   8848    implementation of them consists of two parts: complex symbols, and the
   8849    relocations themselves.
   8850 
   8851    The relocations use a reserved elf-wide relocation type code (R_RELC
   8852    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   8853    information (start bit, end bit, word width, etc) into the addend.  This
   8854    information is extracted from CGEN-generated operand tables within gas.
   8855 
   8856    Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
   8857    internal) representing prefix-notation expressions, including but not
   8858    limited to those sorts of expressions normally encoded as addends in the
   8859    addend field.  The symbol mangling format is:
   8860 
   8861    <node> := <literal>
   8862 	  |  <unary-operator> ':' <node>
   8863 	  |  <binary-operator> ':' <node> ':' <node>
   8864 	  ;
   8865 
   8866    <literal> := 's' <digits=N> ':' <N character symbol name>
   8867 	     |  'S' <digits=N> ':' <N character section name>
   8868 	     |  '#' <hexdigits>
   8869 	     ;
   8870 
   8871    <binary-operator> := as in C
   8872    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   8873 
   8874 static void
   8875 set_symbol_value (bfd *bfd_with_globals,
   8876 		  Elf_Internal_Sym *isymbuf,
   8877 		  size_t locsymcount,
   8878 		  size_t symidx,
   8879 		  bfd_vma val)
   8880 {
   8881   struct elf_link_hash_entry **sym_hashes;
   8882   struct elf_link_hash_entry *h;
   8883   size_t extsymoff = locsymcount;
   8884 
   8885   if (symidx < locsymcount)
   8886     {
   8887       Elf_Internal_Sym *sym;
   8888 
   8889       sym = isymbuf + symidx;
   8890       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   8891 	{
   8892 	  /* It is a local symbol: move it to the
   8893 	     "absolute" section and give it a value.  */
   8894 	  sym->st_shndx = SHN_ABS;
   8895 	  sym->st_value = val;
   8896 	  return;
   8897 	}
   8898       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   8899       extsymoff = 0;
   8900     }
   8901 
   8902   /* It is a global symbol: set its link type
   8903      to "defined" and give it a value.  */
   8904 
   8905   sym_hashes = elf_sym_hashes (bfd_with_globals);
   8906   h = sym_hashes [symidx - extsymoff];
   8907   while (h->root.type == bfd_link_hash_indirect
   8908 	 || h->root.type == bfd_link_hash_warning)
   8909     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8910   h->root.type = bfd_link_hash_defined;
   8911   h->root.u.def.value = val;
   8912   h->root.u.def.section = bfd_abs_section_ptr;
   8913 }
   8914 
   8915 static bool
   8916 resolve_symbol (const char *name,
   8917 		bfd *input_bfd,
   8918 		struct elf_final_link_info *flinfo,
   8919 		bfd_vma *result,
   8920 		Elf_Internal_Sym *isymbuf,
   8921 		size_t locsymcount)
   8922 {
   8923   Elf_Internal_Sym *sym;
   8924   struct bfd_link_hash_entry *global_entry;
   8925   const char *candidate = NULL;
   8926   Elf_Internal_Shdr *symtab_hdr;
   8927   size_t i;
   8928 
   8929   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   8930 
   8931   for (i = 0; i < locsymcount; ++ i)
   8932     {
   8933       sym = isymbuf + i;
   8934 
   8935       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   8936 	continue;
   8937 
   8938       candidate = bfd_elf_string_from_elf_section (input_bfd,
   8939 						   symtab_hdr->sh_link,
   8940 						   sym->st_name);
   8941 #ifdef DEBUG
   8942       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   8943 	      name, candidate, (unsigned long) sym->st_value);
   8944 #endif
   8945       if (candidate && strcmp (candidate, name) == 0)
   8946 	{
   8947 	  asection *sec = flinfo->sections [i];
   8948 
   8949 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   8950 	  *result += sec->output_offset + sec->output_section->vma;
   8951 #ifdef DEBUG
   8952 	  printf ("Found symbol with value %8.8lx\n",
   8953 		  (unsigned long) *result);
   8954 #endif
   8955 	  return true;
   8956 	}
   8957     }
   8958 
   8959   /* Hmm, haven't found it yet. perhaps it is a global.  */
   8960   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   8961 				       false, false, true);
   8962   if (!global_entry)
   8963     return false;
   8964 
   8965   if (global_entry->type == bfd_link_hash_defined
   8966       || global_entry->type == bfd_link_hash_defweak)
   8967     {
   8968       *result = (global_entry->u.def.value
   8969 		 + global_entry->u.def.section->output_section->vma
   8970 		 + global_entry->u.def.section->output_offset);
   8971 #ifdef DEBUG
   8972       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   8973 	      global_entry->root.string, (unsigned long) *result);
   8974 #endif
   8975       return true;
   8976     }
   8977 
   8978   return false;
   8979 }
   8980 
   8981 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
   8982    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
   8983    names like "foo.end" which is the end address of section "foo".  */
   8984 
   8985 static bool
   8986 resolve_section (const char *name,
   8987 		 asection *sections,
   8988 		 bfd_vma *result,
   8989 		 bfd * abfd)
   8990 {
   8991   asection *curr;
   8992   unsigned int len;
   8993 
   8994   for (curr = sections; curr; curr = curr->next)
   8995     if (strcmp (curr->name, name) == 0)
   8996       {
   8997 	*result = curr->vma;
   8998 	return true;
   8999       }
   9000 
   9001   /* Hmm. still haven't found it. try pseudo-section names.  */
   9002   /* FIXME: This could be coded more efficiently...  */
   9003   for (curr = sections; curr; curr = curr->next)
   9004     {
   9005       len = strlen (curr->name);
   9006       if (len > strlen (name))
   9007 	continue;
   9008 
   9009       if (strncmp (curr->name, name, len) == 0)
   9010 	{
   9011 	  if (startswith (name + len, ".end"))
   9012 	    {
   9013 	      *result = (curr->vma
   9014 			 + curr->size / bfd_octets_per_byte (abfd, curr));
   9015 	      return true;
   9016 	    }
   9017 
   9018 	  /* Insert more pseudo-section names here, if you like.  */
   9019 	}
   9020     }
   9021 
   9022   return false;
   9023 }
   9024 
   9025 static void
   9026 undefined_reference (const char *reftype, const char *name)
   9027 {
   9028   /* xgettext:c-format */
   9029   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   9030 		      reftype, name);
   9031   bfd_set_error (bfd_error_bad_value);
   9032 }
   9033 
   9034 static bool
   9035 eval_symbol (bfd_vma *result,
   9036 	     const char **symp,
   9037 	     bfd *input_bfd,
   9038 	     struct elf_final_link_info *flinfo,
   9039 	     bfd_vma dot,
   9040 	     Elf_Internal_Sym *isymbuf,
   9041 	     size_t locsymcount,
   9042 	     int signed_p)
   9043 {
   9044   size_t len;
   9045   size_t symlen;
   9046   bfd_vma a;
   9047   bfd_vma b;
   9048   char symbuf[4096];
   9049   const char *sym = *symp;
   9050   const char *symend;
   9051   bool symbol_is_section = false;
   9052 
   9053   len = strlen (sym);
   9054   symend = sym + len;
   9055 
   9056   if (len < 1 || len > sizeof (symbuf))
   9057     {
   9058       bfd_set_error (bfd_error_invalid_operation);
   9059       return false;
   9060     }
   9061 
   9062   switch (* sym)
   9063     {
   9064     case '.':
   9065       *result = dot;
   9066       *symp = sym + 1;
   9067       return true;
   9068 
   9069     case '#':
   9070       ++sym;
   9071       *result = strtoul (sym, (char **) symp, 16);
   9072       return true;
   9073 
   9074     case 'S':
   9075       symbol_is_section = true;
   9076       /* Fall through.  */
   9077     case 's':
   9078       ++sym;
   9079       symlen = strtol (sym, (char **) symp, 10);
   9080       sym = *symp + 1; /* Skip the trailing ':'.  */
   9081 
   9082       if (symend < sym || symlen + 1 > sizeof (symbuf))
   9083 	{
   9084 	  bfd_set_error (bfd_error_invalid_operation);
   9085 	  return false;
   9086 	}
   9087 
   9088       memcpy (symbuf, sym, symlen);
   9089       symbuf[symlen] = '\0';
   9090       *symp = sym + symlen;
   9091 
   9092       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   9093 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   9094 	 interpretation here; section means "try section first", not "must be a
   9095 	 section", and likewise with symbol.  */
   9096 
   9097       if (symbol_is_section)
   9098 	{
   9099 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
   9100 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   9101 				  isymbuf, locsymcount))
   9102 	    {
   9103 	      undefined_reference ("section", symbuf);
   9104 	      return false;
   9105 	    }
   9106 	}
   9107       else
   9108 	{
   9109 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   9110 			       isymbuf, locsymcount)
   9111 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   9112 				   result, input_bfd))
   9113 	    {
   9114 	      undefined_reference ("symbol", symbuf);
   9115 	      return false;
   9116 	    }
   9117 	}
   9118 
   9119       return true;
   9120 
   9121       /* All that remains are operators.  */
   9122 
   9123 #define UNARY_OP(op)						\
   9124   if (startswith (sym, #op))					\
   9125     {								\
   9126       sym += strlen (#op);					\
   9127       if (*sym == ':')						\
   9128 	++sym;							\
   9129       *symp = sym;						\
   9130       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   9131 			isymbuf, locsymcount, signed_p))	\
   9132 	return false;						\
   9133       if (signed_p)						\
   9134 	*result = op ((bfd_signed_vma) a);			\
   9135       else							\
   9136 	*result = op a;						\
   9137       return true;						\
   9138     }
   9139 
   9140 #define BINARY_OP_HEAD(op)					\
   9141   if (startswith (sym, #op))					\
   9142     {								\
   9143       sym += strlen (#op);					\
   9144       if (*sym == ':')						\
   9145 	++sym;							\
   9146       *symp = sym;						\
   9147       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   9148 			isymbuf, locsymcount, signed_p))	\
   9149 	return false;						\
   9150       ++*symp;							\
   9151       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   9152 			isymbuf, locsymcount, signed_p))	\
   9153 	return false;
   9154 #define BINARY_OP_TAIL(op)					\
   9155       if (signed_p)						\
   9156 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   9157       else							\
   9158 	*result = a op b;					\
   9159       return true;						\
   9160     }
   9161 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
   9162 
   9163     default:
   9164       UNARY_OP  (0-);
   9165       BINARY_OP_HEAD (<<);
   9166       if (b >= sizeof (a) * CHAR_BIT)
   9167 	{
   9168 	  *result = 0;
   9169 	  return true;
   9170 	}
   9171       signed_p = 0;
   9172       BINARY_OP_TAIL (<<);
   9173       BINARY_OP_HEAD (>>);
   9174       if (b >= sizeof (a) * CHAR_BIT)
   9175 	{
   9176 	  *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
   9177 	  return true;
   9178 	}
   9179       BINARY_OP_TAIL (>>);
   9180       BINARY_OP (==);
   9181       BINARY_OP (!=);
   9182       BINARY_OP (<=);
   9183       BINARY_OP (>=);
   9184       BINARY_OP (&&);
   9185       BINARY_OP (||);
   9186       UNARY_OP  (~);
   9187       UNARY_OP  (!);
   9188       BINARY_OP (*);
   9189       BINARY_OP_HEAD (/);
   9190       if (b == 0)
   9191 	{
   9192 	  _bfd_error_handler (_("division by zero"));
   9193 	  bfd_set_error (bfd_error_bad_value);
   9194 	  return false;
   9195 	}
   9196       BINARY_OP_TAIL (/);
   9197       BINARY_OP_HEAD (%);
   9198       if (b == 0)
   9199 	{
   9200 	  _bfd_error_handler (_("division by zero"));
   9201 	  bfd_set_error (bfd_error_bad_value);
   9202 	  return false;
   9203 	}
   9204       BINARY_OP_TAIL (%);
   9205       BINARY_OP (^);
   9206       BINARY_OP (|);
   9207       BINARY_OP (&);
   9208       BINARY_OP (+);
   9209       BINARY_OP (-);
   9210       BINARY_OP (<);
   9211       BINARY_OP (>);
   9212 #undef UNARY_OP
   9213 #undef BINARY_OP
   9214       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   9215       bfd_set_error (bfd_error_invalid_operation);
   9216       return false;
   9217     }
   9218 }
   9219 
   9220 static void
   9221 put_value (bfd_vma size,
   9222 	   unsigned long chunksz,
   9223 	   bfd *input_bfd,
   9224 	   bfd_vma x,
   9225 	   bfd_byte *location)
   9226 {
   9227   location += (size - chunksz);
   9228 
   9229   for (; size; size -= chunksz, location -= chunksz)
   9230     {
   9231       switch (chunksz)
   9232 	{
   9233 	case 1:
   9234 	  bfd_put_8 (input_bfd, x, location);
   9235 	  x >>= 8;
   9236 	  break;
   9237 	case 2:
   9238 	  bfd_put_16 (input_bfd, x, location);
   9239 	  x >>= 16;
   9240 	  break;
   9241 	case 4:
   9242 	  bfd_put_32 (input_bfd, x, location);
   9243 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   9244 	  x >>= 16;
   9245 	  x >>= 16;
   9246 	  break;
   9247 #ifdef BFD64
   9248 	case 8:
   9249 	  bfd_put_64 (input_bfd, x, location);
   9250 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   9251 	  x >>= 32;
   9252 	  x >>= 32;
   9253 	  break;
   9254 #endif
   9255 	default:
   9256 	  abort ();
   9257 	  break;
   9258 	}
   9259     }
   9260 }
   9261 
   9262 static bfd_vma
   9263 get_value (bfd_vma size,
   9264 	   unsigned long chunksz,
   9265 	   bfd *input_bfd,
   9266 	   bfd_byte *location)
   9267 {
   9268   int shift;
   9269   bfd_vma x = 0;
   9270 
   9271   /* Sanity checks.  */
   9272   BFD_ASSERT (chunksz <= sizeof (x)
   9273 	      && size >= chunksz
   9274 	      && chunksz != 0
   9275 	      && (size % chunksz) == 0
   9276 	      && input_bfd != NULL
   9277 	      && location != NULL);
   9278 
   9279   if (chunksz == sizeof (x))
   9280     {
   9281       BFD_ASSERT (size == chunksz);
   9282 
   9283       /* Make sure that we do not perform an undefined shift operation.
   9284 	 We know that size == chunksz so there will only be one iteration
   9285 	 of the loop below.  */
   9286       shift = 0;
   9287     }
   9288   else
   9289     shift = 8 * chunksz;
   9290 
   9291   for (; size; size -= chunksz, location += chunksz)
   9292     {
   9293       switch (chunksz)
   9294 	{
   9295 	case 1:
   9296 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   9297 	  break;
   9298 	case 2:
   9299 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   9300 	  break;
   9301 	case 4:
   9302 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   9303 	  break;
   9304 #ifdef BFD64
   9305 	case 8:
   9306 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   9307 	  break;
   9308 #endif
   9309 	default:
   9310 	  abort ();
   9311 	}
   9312     }
   9313   return x;
   9314 }
   9315 
   9316 static void
   9317 decode_complex_addend (unsigned long *start,   /* in bits */
   9318 		       unsigned long *oplen,   /* in bits */
   9319 		       unsigned long *len,     /* in bits */
   9320 		       unsigned long *wordsz,  /* in bytes */
   9321 		       unsigned long *chunksz, /* in bytes */
   9322 		       unsigned long *lsb0_p,
   9323 		       unsigned long *signed_p,
   9324 		       unsigned long *trunc_p,
   9325 		       unsigned long encoded)
   9326 {
   9327   * start     =	 encoded	& 0x3F;
   9328   * len	      = (encoded >>  6) & 0x3F;
   9329   * oplen     = (encoded >> 12) & 0x3F;
   9330   * wordsz    = (encoded >> 18) & 0xF;
   9331   * chunksz   = (encoded >> 22) & 0xF;
   9332   * lsb0_p    = (encoded >> 27) & 1;
   9333   * signed_p  = (encoded >> 28) & 1;
   9334   * trunc_p   = (encoded >> 29) & 1;
   9335 }
   9336 
   9337 bfd_reloc_status_type
   9338 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   9339 				    asection *input_section,
   9340 				    bfd_byte *contents,
   9341 				    Elf_Internal_Rela *rel,
   9342 				    bfd_vma relocation)
   9343 {
   9344   bfd_vma shift, x, mask;
   9345   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   9346   bfd_reloc_status_type r;
   9347   bfd_size_type octets;
   9348 
   9349   /*  Perform this reloc, since it is complex.
   9350       (this is not to say that it necessarily refers to a complex
   9351       symbol; merely that it is a self-describing CGEN based reloc.
   9352       i.e. the addend has the complete reloc information (bit start, end,
   9353       word size, etc) encoded within it.).  */
   9354 
   9355   decode_complex_addend (&start, &oplen, &len, &wordsz,
   9356 			 &chunksz, &lsb0_p, &signed_p,
   9357 			 &trunc_p, rel->r_addend);
   9358 
   9359   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   9360 
   9361   if (lsb0_p)
   9362     shift = (start + 1) - len;
   9363   else
   9364     shift = (8 * wordsz) - (start + len);
   9365 
   9366   octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
   9367   x = get_value (wordsz, chunksz, input_bfd, contents + octets);
   9368 
   9369 #ifdef DEBUG
   9370   printf ("Doing complex reloc: "
   9371 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   9372 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   9373 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   9374 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   9375 	  oplen, (unsigned long) x, (unsigned long) mask,
   9376 	  (unsigned long) relocation);
   9377 #endif
   9378 
   9379   r = bfd_reloc_ok;
   9380   if (! trunc_p)
   9381     /* Now do an overflow check.  */
   9382     r = bfd_check_overflow ((signed_p
   9383 			     ? complain_overflow_signed
   9384 			     : complain_overflow_unsigned),
   9385 			    len, 0, (8 * wordsz),
   9386 			    relocation);
   9387 
   9388   /* Do the deed.  */
   9389   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   9390 
   9391 #ifdef DEBUG
   9392   printf ("           relocation: %8.8lx\n"
   9393 	  "         shifted mask: %8.8lx\n"
   9394 	  " shifted/masked reloc: %8.8lx\n"
   9395 	  "               result: %8.8lx\n",
   9396 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   9397 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   9398 #endif
   9399   put_value (wordsz, chunksz, input_bfd, x, contents + octets);
   9400   return r;
   9401 }
   9402 
   9403 /* Functions to read r_offset from external (target order) reloc
   9404    entry.  Faster than bfd_getl32 et al, because we let the compiler
   9405    know the value is aligned.  */
   9406 
   9407 static bfd_vma
   9408 ext32l_r_offset (const void *p)
   9409 {
   9410   union aligned32
   9411   {
   9412     uint32_t v;
   9413     unsigned char c[4];
   9414   };
   9415   const union aligned32 *a
   9416     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9417 
   9418   uint32_t aval = (  (uint32_t) a->c[0]
   9419 		   | (uint32_t) a->c[1] << 8
   9420 		   | (uint32_t) a->c[2] << 16
   9421 		   | (uint32_t) a->c[3] << 24);
   9422   return aval;
   9423 }
   9424 
   9425 static bfd_vma
   9426 ext32b_r_offset (const void *p)
   9427 {
   9428   union aligned32
   9429   {
   9430     uint32_t v;
   9431     unsigned char c[4];
   9432   };
   9433   const union aligned32 *a
   9434     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   9435 
   9436   uint32_t aval = (  (uint32_t) a->c[0] << 24
   9437 		   | (uint32_t) a->c[1] << 16
   9438 		   | (uint32_t) a->c[2] << 8
   9439 		   | (uint32_t) a->c[3]);
   9440   return aval;
   9441 }
   9442 
   9443 static bfd_vma
   9444 ext64l_r_offset (const void *p)
   9445 {
   9446   union aligned64
   9447   {
   9448     uint64_t v;
   9449     unsigned char c[8];
   9450   };
   9451   const union aligned64 *a
   9452     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9453 
   9454   uint64_t aval = (  (uint64_t) a->c[0]
   9455 		   | (uint64_t) a->c[1] << 8
   9456 		   | (uint64_t) a->c[2] << 16
   9457 		   | (uint64_t) a->c[3] << 24
   9458 		   | (uint64_t) a->c[4] << 32
   9459 		   | (uint64_t) a->c[5] << 40
   9460 		   | (uint64_t) a->c[6] << 48
   9461 		   | (uint64_t) a->c[7] << 56);
   9462   return aval;
   9463 }
   9464 
   9465 static bfd_vma
   9466 ext64b_r_offset (const void *p)
   9467 {
   9468   union aligned64
   9469   {
   9470     uint64_t v;
   9471     unsigned char c[8];
   9472   };
   9473   const union aligned64 *a
   9474     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   9475 
   9476   uint64_t aval = (  (uint64_t) a->c[0] << 56
   9477 		   | (uint64_t) a->c[1] << 48
   9478 		   | (uint64_t) a->c[2] << 40
   9479 		   | (uint64_t) a->c[3] << 32
   9480 		   | (uint64_t) a->c[4] << 24
   9481 		   | (uint64_t) a->c[5] << 16
   9482 		   | (uint64_t) a->c[6] << 8
   9483 		   | (uint64_t) a->c[7]);
   9484   return aval;
   9485 }
   9486 
   9487 /* When performing a relocatable link, the input relocations are
   9488    preserved.  But, if they reference global symbols, the indices
   9489    referenced must be updated.  Update all the relocations found in
   9490    RELDATA.  */
   9491 
   9492 static bool
   9493 elf_link_adjust_relocs (bfd *abfd,
   9494 			asection *sec,
   9495 			struct bfd_elf_section_reloc_data *reldata,
   9496 			bool sort,
   9497 			struct bfd_link_info *info)
   9498 {
   9499   unsigned int i;
   9500   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9501   bfd_byte *erela;
   9502   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   9503   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   9504   bfd_vma r_type_mask;
   9505   int r_sym_shift;
   9506   unsigned int count = reldata->count;
   9507   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   9508 
   9509   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   9510     {
   9511       swap_in = bed->s->swap_reloc_in;
   9512       swap_out = bed->s->swap_reloc_out;
   9513     }
   9514   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   9515     {
   9516       swap_in = bed->s->swap_reloca_in;
   9517       swap_out = bed->s->swap_reloca_out;
   9518     }
   9519   else
   9520     abort ();
   9521 
   9522   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   9523     abort ();
   9524 
   9525   if (bed->s->arch_size == 32)
   9526     {
   9527       r_type_mask = 0xff;
   9528       r_sym_shift = 8;
   9529     }
   9530   else
   9531     {
   9532       r_type_mask = 0xffffffff;
   9533       r_sym_shift = 32;
   9534     }
   9535 
   9536   erela = reldata->hdr->contents;
   9537   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   9538     {
   9539       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   9540       unsigned int j;
   9541 
   9542       if (*rel_hash == NULL)
   9543 	continue;
   9544 
   9545       if ((*rel_hash)->indx == -2
   9546 	  && info->gc_sections
   9547 	  && ! info->gc_keep_exported)
   9548 	{
   9549 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
   9550 	  _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
   9551 			      abfd, sec,
   9552 			      (*rel_hash)->root.root.string);
   9553 	  _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
   9554 			      abfd, sec);
   9555 	  bfd_set_error (bfd_error_invalid_operation);
   9556 	  return false;
   9557 	}
   9558       BFD_ASSERT ((*rel_hash)->indx >= 0);
   9559 
   9560       (*swap_in) (abfd, erela, irela);
   9561       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   9562 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   9563 			   | (irela[j].r_info & r_type_mask));
   9564       (*swap_out) (abfd, irela, erela);
   9565     }
   9566 
   9567   if (bed->elf_backend_update_relocs)
   9568     (*bed->elf_backend_update_relocs) (sec, reldata);
   9569 
   9570   if (sort && count != 0)
   9571     {
   9572       bfd_vma (*ext_r_off) (const void *);
   9573       bfd_vma r_off;
   9574       size_t elt_size;
   9575       bfd_byte *base, *end, *p, *loc;
   9576       bfd_byte *buf = NULL;
   9577 
   9578       if (bed->s->arch_size == 32)
   9579 	{
   9580 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9581 	    ext_r_off = ext32l_r_offset;
   9582 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9583 	    ext_r_off = ext32b_r_offset;
   9584 	  else
   9585 	    abort ();
   9586 	}
   9587       else
   9588 	{
   9589 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9590 	    ext_r_off = ext64l_r_offset;
   9591 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9592 	    ext_r_off = ext64b_r_offset;
   9593 	  else
   9594 	    abort ();
   9595 	}
   9596 
   9597       /*  Must use a stable sort here.  A modified insertion sort,
   9598 	  since the relocs are mostly sorted already.  */
   9599       elt_size = reldata->hdr->sh_entsize;
   9600       base = reldata->hdr->contents;
   9601       end = base + count * elt_size;
   9602       if (elt_size > sizeof (Elf64_External_Rela))
   9603 	abort ();
   9604 
   9605       /* Ensure the first element is lowest.  This acts as a sentinel,
   9606 	 speeding the main loop below.  */
   9607       r_off = (*ext_r_off) (base);
   9608       for (p = loc = base; (p += elt_size) < end; )
   9609 	{
   9610 	  bfd_vma r_off2 = (*ext_r_off) (p);
   9611 	  if (r_off > r_off2)
   9612 	    {
   9613 	      r_off = r_off2;
   9614 	      loc = p;
   9615 	    }
   9616 	}
   9617       if (loc != base)
   9618 	{
   9619 	  /* Don't just swap *base and *loc as that changes the order
   9620 	     of the original base[0] and base[1] if they happen to
   9621 	     have the same r_offset.  */
   9622 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   9623 	  memcpy (onebuf, loc, elt_size);
   9624 	  memmove (base + elt_size, base, loc - base);
   9625 	  memcpy (base, onebuf, elt_size);
   9626 	}
   9627 
   9628       for (p = base + elt_size; (p += elt_size) < end; )
   9629 	{
   9630 	  /* base to p is sorted, *p is next to insert.  */
   9631 	  r_off = (*ext_r_off) (p);
   9632 	  /* Search the sorted region for location to insert.  */
   9633 	  loc = p - elt_size;
   9634 	  while (r_off < (*ext_r_off) (loc))
   9635 	    loc -= elt_size;
   9636 	  loc += elt_size;
   9637 	  if (loc != p)
   9638 	    {
   9639 	      /* Chances are there is a run of relocs to insert here,
   9640 		 from one of more input files.  Files are not always
   9641 		 linked in order due to the way elf_link_input_bfd is
   9642 		 called.  See pr17666.  */
   9643 	      size_t sortlen = p - loc;
   9644 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   9645 	      size_t runlen = elt_size;
   9646 	      bfd_vma r_off_runend = r_off;
   9647 	      bfd_vma r_off_runend_next;
   9648 	      size_t buf_size = 96 * 1024;
   9649 	      while (p + runlen < end
   9650 		     && (sortlen <= buf_size
   9651 			 || runlen + elt_size <= buf_size)
   9652 		     /* run must not break the ordering of base..loc+1 */
   9653 		     && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
   9654 		     /* run must be already sorted */
   9655 		     && r_off_runend_next >= r_off_runend)
   9656 		{
   9657 		  runlen += elt_size;
   9658 		  r_off_runend = r_off_runend_next;
   9659 		}
   9660 	      if (buf == NULL)
   9661 		{
   9662 		  buf = bfd_malloc (buf_size);
   9663 		  if (buf == NULL)
   9664 		    return false;
   9665 		}
   9666 	      if (runlen < sortlen)
   9667 		{
   9668 		  memcpy (buf, p, runlen);
   9669 		  memmove (loc + runlen, loc, sortlen);
   9670 		  memcpy (loc, buf, runlen);
   9671 		}
   9672 	      else
   9673 		{
   9674 		  memcpy (buf, loc, sortlen);
   9675 		  memmove (loc, p, runlen);
   9676 		  memcpy (loc + runlen, buf, sortlen);
   9677 		}
   9678 	      p += runlen - elt_size;
   9679 	    }
   9680 	}
   9681       /* Hashes are no longer valid.  */
   9682       free (reldata->hashes);
   9683       reldata->hashes = NULL;
   9684       free (buf);
   9685     }
   9686   return true;
   9687 }
   9688 
   9689 struct elf_link_sort_rela
   9690 {
   9691   union {
   9692     bfd_vma offset;
   9693     bfd_vma sym_mask;
   9694   } u;
   9695   enum elf_reloc_type_class type;
   9696   /* We use this as an array of size int_rels_per_ext_rel.  */
   9697   Elf_Internal_Rela rela[1];
   9698 };
   9699 
   9700 /* qsort stability here and for cmp2 is only an issue if multiple
   9701    dynamic relocations are emitted at the same address.  But targets
   9702    that apply a series of dynamic relocations each operating on the
   9703    result of the prior relocation can't use -z combreloc as
   9704    implemented anyway.  Such schemes tend to be broken by sorting on
   9705    symbol index.  That leaves dynamic NONE relocs as the only other
   9706    case where ld might emit multiple relocs at the same address, and
   9707    those are only emitted due to target bugs.  */
   9708 
   9709 static int
   9710 elf_link_sort_cmp1 (const void *A, const void *B)
   9711 {
   9712   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9713   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9714   int relativea, relativeb;
   9715 
   9716   relativea = a->type == reloc_class_relative;
   9717   relativeb = b->type == reloc_class_relative;
   9718 
   9719   if (relativea < relativeb)
   9720     return 1;
   9721   if (relativea > relativeb)
   9722     return -1;
   9723   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   9724     return -1;
   9725   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   9726     return 1;
   9727   if (a->rela->r_offset < b->rela->r_offset)
   9728     return -1;
   9729   if (a->rela->r_offset > b->rela->r_offset)
   9730     return 1;
   9731   return 0;
   9732 }
   9733 
   9734 static int
   9735 elf_link_sort_cmp2 (const void *A, const void *B)
   9736 {
   9737   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9738   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9739 
   9740   if (a->type < b->type)
   9741     return -1;
   9742   if (a->type > b->type)
   9743     return 1;
   9744   if (a->u.offset < b->u.offset)
   9745     return -1;
   9746   if (a->u.offset > b->u.offset)
   9747     return 1;
   9748   if (a->rela->r_offset < b->rela->r_offset)
   9749     return -1;
   9750   if (a->rela->r_offset > b->rela->r_offset)
   9751     return 1;
   9752   return 0;
   9753 }
   9754 
   9755 static size_t
   9756 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   9757 {
   9758   asection *dynamic_relocs;
   9759   asection *rela_dyn;
   9760   asection *rel_dyn;
   9761   bfd_size_type count, size;
   9762   size_t i, ret, sort_elt, ext_size;
   9763   bfd_byte *sort, *s_non_relative, *p;
   9764   struct elf_link_sort_rela *sq;
   9765   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9766   int i2e = bed->s->int_rels_per_ext_rel;
   9767   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   9768   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   9769   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   9770   struct bfd_link_order *lo;
   9771   bfd_vma r_sym_mask;
   9772   bool use_rela;
   9773 
   9774   /* Find a dynamic reloc section.  */
   9775   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   9776   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   9777   if (rela_dyn != NULL && rela_dyn->size > 0
   9778       && rel_dyn != NULL && rel_dyn->size > 0)
   9779     {
   9780       bool use_rela_initialised = false;
   9781 
   9782       /* This is just here to stop gcc from complaining.
   9783 	 Its initialization checking code is not perfect.  */
   9784       use_rela = true;
   9785 
   9786       /* Both sections are present.  Examine the sizes
   9787 	 of the indirect sections to help us choose.  */
   9788       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   9789 	if (lo->type == bfd_indirect_link_order)
   9790 	  {
   9791 	    asection *o = lo->u.indirect.section;
   9792 
   9793 	    if ((o->size % bed->s->sizeof_rela) == 0)
   9794 	      {
   9795 		if ((o->size % bed->s->sizeof_rel) == 0)
   9796 		  /* Section size is divisible by both rel and rela sizes.
   9797 		     It is of no help to us.  */
   9798 		  ;
   9799 		else
   9800 		  {
   9801 		    /* Section size is only divisible by rela.  */
   9802 		    if (use_rela_initialised && !use_rela)
   9803 		      {
   9804 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   9805 					      "they are in more than one size"),
   9806 					    abfd);
   9807 			bfd_set_error (bfd_error_invalid_operation);
   9808 			return 0;
   9809 		      }
   9810 		    else
   9811 		      {
   9812 			use_rela = true;
   9813 			use_rela_initialised = true;
   9814 		      }
   9815 		  }
   9816 	      }
   9817 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   9818 	      {
   9819 		/* Section size is only divisible by rel.  */
   9820 		if (use_rela_initialised && use_rela)
   9821 		  {
   9822 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   9823 					  "they are in more than one size"),
   9824 					abfd);
   9825 		    bfd_set_error (bfd_error_invalid_operation);
   9826 		    return 0;
   9827 		  }
   9828 		else
   9829 		  {
   9830 		    use_rela = false;
   9831 		    use_rela_initialised = true;
   9832 		  }
   9833 	      }
   9834 	    else
   9835 	      {
   9836 		/* The section size is not divisible by either -
   9837 		   something is wrong.  */
   9838 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   9839 				      "they are of an unknown size"), abfd);
   9840 		bfd_set_error (bfd_error_invalid_operation);
   9841 		return 0;
   9842 	      }
   9843 	  }
   9844 
   9845       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   9846 	if (lo->type == bfd_indirect_link_order)
   9847 	  {
   9848 	    asection *o = lo->u.indirect.section;
   9849 
   9850 	    if ((o->size % bed->s->sizeof_rela) == 0)
   9851 	      {
   9852 		if ((o->size % bed->s->sizeof_rel) == 0)
   9853 		  /* Section size is divisible by both rel and rela sizes.
   9854 		     It is of no help to us.  */
   9855 		  ;
   9856 		else
   9857 		  {
   9858 		    /* Section size is only divisible by rela.  */
   9859 		    if (use_rela_initialised && !use_rela)
   9860 		      {
   9861 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   9862 					      "they are in more than one size"),
   9863 					    abfd);
   9864 			bfd_set_error (bfd_error_invalid_operation);
   9865 			return 0;
   9866 		      }
   9867 		    else
   9868 		      {
   9869 			use_rela = true;
   9870 			use_rela_initialised = true;
   9871 		      }
   9872 		  }
   9873 	      }
   9874 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   9875 	      {
   9876 		/* Section size is only divisible by rel.  */
   9877 		if (use_rela_initialised && use_rela)
   9878 		  {
   9879 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   9880 					  "they are in more than one size"),
   9881 					abfd);
   9882 		    bfd_set_error (bfd_error_invalid_operation);
   9883 		    return 0;
   9884 		  }
   9885 		else
   9886 		  {
   9887 		    use_rela = false;
   9888 		    use_rela_initialised = true;
   9889 		  }
   9890 	      }
   9891 	    else
   9892 	      {
   9893 		/* The section size is not divisible by either -
   9894 		   something is wrong.  */
   9895 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   9896 				      "they are of an unknown size"), abfd);
   9897 		bfd_set_error (bfd_error_invalid_operation);
   9898 		return 0;
   9899 	      }
   9900 	  }
   9901 
   9902       if (! use_rela_initialised)
   9903 	/* Make a guess.  */
   9904 	use_rela = true;
   9905     }
   9906   else if (rela_dyn != NULL && rela_dyn->size > 0)
   9907     use_rela = true;
   9908   else if (rel_dyn != NULL && rel_dyn->size > 0)
   9909     use_rela = false;
   9910   else
   9911     return 0;
   9912 
   9913   if (use_rela)
   9914     {
   9915       dynamic_relocs = rela_dyn;
   9916       ext_size = bed->s->sizeof_rela;
   9917       swap_in = bed->s->swap_reloca_in;
   9918       swap_out = bed->s->swap_reloca_out;
   9919     }
   9920   else
   9921     {
   9922       dynamic_relocs = rel_dyn;
   9923       ext_size = bed->s->sizeof_rel;
   9924       swap_in = bed->s->swap_reloc_in;
   9925       swap_out = bed->s->swap_reloc_out;
   9926     }
   9927 
   9928   size = 0;
   9929   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9930     if (lo->type == bfd_indirect_link_order)
   9931       size += lo->u.indirect.section->size;
   9932 
   9933   if (size != dynamic_relocs->size)
   9934     return 0;
   9935 
   9936   sort_elt = (sizeof (struct elf_link_sort_rela)
   9937 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   9938 
   9939   count = dynamic_relocs->size / ext_size;
   9940   if (count == 0)
   9941     return 0;
   9942   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   9943 
   9944   if (sort == NULL)
   9945     {
   9946       (*info->callbacks->warning)
   9947 	(info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
   9948       return 0;
   9949     }
   9950 
   9951   if (bed->s->arch_size == 32)
   9952     r_sym_mask = ~(bfd_vma) 0xff;
   9953   else
   9954     r_sym_mask = ~(bfd_vma) 0xffffffff;
   9955 
   9956   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9957     if (lo->type == bfd_indirect_link_order)
   9958       {
   9959 	bfd_byte *erel, *erelend;
   9960 	asection *o = lo->u.indirect.section;
   9961 
   9962 	if (o->contents == NULL && o->size != 0)
   9963 	  {
   9964 	    /* This is a reloc section that is being handled as a normal
   9965 	       section.  See bfd_section_from_shdr.  We can't combine
   9966 	       relocs in this case.  */
   9967 	    free (sort);
   9968 	    return 0;
   9969 	  }
   9970 	erel = o->contents;
   9971 	erelend = o->contents + o->size;
   9972 	p = sort + o->output_offset * opb / ext_size * sort_elt;
   9973 
   9974 	while (erel < erelend)
   9975 	  {
   9976 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9977 
   9978 	    (*swap_in) (abfd, erel, s->rela);
   9979 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   9980 	    s->u.sym_mask = r_sym_mask;
   9981 	    p += sort_elt;
   9982 	    erel += ext_size;
   9983 	  }
   9984       }
   9985 
   9986   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   9987 
   9988   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   9989     {
   9990       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9991       if (s->type != reloc_class_relative)
   9992 	break;
   9993     }
   9994   ret = i;
   9995   s_non_relative = p;
   9996 
   9997   sq = (struct elf_link_sort_rela *) s_non_relative;
   9998   for (; i < count; i++, p += sort_elt)
   9999     {
   10000       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   10001       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   10002 	sq = sp;
   10003       sp->u.offset = sq->rela->r_offset;
   10004     }
   10005 
   10006   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   10007 
   10008   struct elf_link_hash_table *htab = elf_hash_table (info);
   10009   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
   10010     {
   10011       /* We have plt relocs in .rela.dyn.  */
   10012       sq = (struct elf_link_sort_rela *) sort;
   10013       for (i = 0; i < count; i++)
   10014 	if (sq[count - i - 1].type != reloc_class_plt)
   10015 	  break;
   10016       if (i != 0 && htab->srelplt->size == i * ext_size)
   10017 	{
   10018 	  struct bfd_link_order **plo;
   10019 	  /* Put srelplt link_order last.  This is so the output_offset
   10020 	     set in the next loop is correct for DT_JMPREL.  */
   10021 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
   10022 	    if ((*plo)->type == bfd_indirect_link_order
   10023 		&& (*plo)->u.indirect.section == htab->srelplt)
   10024 	      {
   10025 		lo = *plo;
   10026 		*plo = lo->next;
   10027 	      }
   10028 	    else
   10029 	      plo = &(*plo)->next;
   10030 	  *plo = lo;
   10031 	  lo->next = NULL;
   10032 	  dynamic_relocs->map_tail.link_order = lo;
   10033 	}
   10034     }
   10035 
   10036   p = sort;
   10037   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   10038     if (lo->type == bfd_indirect_link_order)
   10039       {
   10040 	bfd_byte *erel, *erelend;
   10041 	asection *o = lo->u.indirect.section;
   10042 
   10043 	erel = o->contents;
   10044 	erelend = o->contents + o->size;
   10045 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
   10046 	while (erel < erelend)
   10047 	  {
   10048 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   10049 	    (*swap_out) (abfd, s->rela, erel);
   10050 	    p += sort_elt;
   10051 	    erel += ext_size;
   10052 	  }
   10053       }
   10054 
   10055   free (sort);
   10056   *psec = dynamic_relocs;
   10057   return ret;
   10058 }
   10059 
   10060 /* Add a symbol to the output symbol string table.  */
   10061 
   10062 static int
   10063 elf_link_output_symstrtab (void *finf,
   10064 			   const char *name,
   10065 			   Elf_Internal_Sym *elfsym,
   10066 			   asection *input_sec,
   10067 			   struct elf_link_hash_entry *h)
   10068 {
   10069   struct elf_final_link_info *flinfo = finf;
   10070   int (*output_symbol_hook)
   10071     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   10072      struct elf_link_hash_entry *);
   10073   struct elf_link_hash_table *hash_table;
   10074   const struct elf_backend_data *bed;
   10075   bfd_size_type strtabsize;
   10076 
   10077   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10078 
   10079   bed = get_elf_backend_data (flinfo->output_bfd);
   10080   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   10081   if (output_symbol_hook != NULL)
   10082     {
   10083       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   10084       if (ret != 1)
   10085 	return ret;
   10086     }
   10087 
   10088   if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
   10089     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
   10090   if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
   10091     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
   10092 
   10093   if (name == NULL || *name == '\0')
   10094     elfsym->st_name = (unsigned long) -1;
   10095   else
   10096     {
   10097       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   10098 	 to get the final offset for st_name.  */
   10099       char *versioned_name = (char *) name;
   10100       if (h != NULL)
   10101 	{
   10102 	  if (h->versioned == versioned && h->def_dynamic)
   10103 	    {
   10104 	      /* Keep only one '@' for versioned symbols defined in
   10105 	         shared objects.  */
   10106 	      char *version = strrchr (name, ELF_VER_CHR);
   10107 	      char *base_end = strchr (name, ELF_VER_CHR);
   10108 	      if (version != base_end)
   10109 		{
   10110 		  size_t base_len;
   10111 		  size_t len = strlen (name);
   10112 		  versioned_name = bfd_alloc (flinfo->output_bfd, len);
   10113 		  if (versioned_name == NULL)
   10114 		    return 0;
   10115 		  base_len = base_end - name;
   10116 		  memcpy (versioned_name, name, base_len);
   10117 		  memcpy (versioned_name + base_len, version,
   10118 			  len - base_len);
   10119 		}
   10120 	    }
   10121 	}
   10122       else if (flinfo->info->unique_symbol
   10123 	       && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
   10124 	{
   10125 	  struct local_hash_entry *lh;
   10126 	  size_t count_len;
   10127 	  size_t base_len;
   10128 	  char buf[30];
   10129 	  switch (ELF_ST_TYPE (elfsym->st_info))
   10130 	    {
   10131 	    case STT_FILE:
   10132 	    case STT_SECTION:
   10133 	      break;
   10134 	    default:
   10135 	      lh = (struct local_hash_entry *) bfd_hash_lookup
   10136 		     (&flinfo->local_hash_table, name, true, false);
   10137 	      if (lh == NULL)
   10138 		return 0;
   10139 	      /* Always append ".COUNT" to local symbols to avoid
   10140 		 potential conflicts with local symbol "XXX.COUNT".  */
   10141 	      sprintf (buf, "%lx", lh->count);
   10142 	      base_len = lh->size;
   10143 	      if (!base_len)
   10144 		{
   10145 		  base_len = strlen (name);
   10146 		  lh->size = base_len;
   10147 		}
   10148 	      count_len = strlen (buf);
   10149 	      versioned_name = bfd_alloc (flinfo->output_bfd,
   10150 					  base_len + count_len + 2);
   10151 	      if (versioned_name == NULL)
   10152 		return 0;
   10153 	      memcpy (versioned_name, name, base_len);
   10154 	      versioned_name[base_len] = '.';
   10155 	      memcpy (versioned_name + base_len + 1, buf,
   10156 		      count_len + 1);
   10157 	      lh->count++;
   10158 	      break;
   10159 	    }
   10160 	}
   10161       elfsym->st_name
   10162 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   10163 					       versioned_name, false);
   10164       if (elfsym->st_name == (unsigned long) -1)
   10165 	return 0;
   10166     }
   10167 
   10168   hash_table = elf_hash_table (flinfo->info);
   10169   strtabsize = hash_table->strtabsize;
   10170   if (strtabsize <= flinfo->output_bfd->symcount)
   10171     {
   10172       strtabsize += strtabsize;
   10173       hash_table->strtabsize = strtabsize;
   10174       strtabsize *= sizeof (*hash_table->strtab);
   10175       hash_table->strtab
   10176 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   10177 						 strtabsize);
   10178       if (hash_table->strtab == NULL)
   10179 	return 0;
   10180     }
   10181   hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
   10182   hash_table->strtab[flinfo->output_bfd->symcount].dest_index
   10183     = flinfo->output_bfd->symcount;
   10184   flinfo->output_bfd->symcount += 1;
   10185 
   10186   return 1;
   10187 }
   10188 
   10189 /* Swap symbols out to the symbol table and flush the output symbols to
   10190    the file.  */
   10191 
   10192 static bool
   10193 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   10194 {
   10195   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   10196   size_t amt;
   10197   size_t i;
   10198   const struct elf_backend_data *bed;
   10199   bfd_byte *symbuf;
   10200   Elf_Internal_Shdr *hdr;
   10201   file_ptr pos;
   10202   bool ret;
   10203 
   10204   if (flinfo->output_bfd->symcount == 0)
   10205     return true;
   10206 
   10207   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   10208 
   10209   bed = get_elf_backend_data (flinfo->output_bfd);
   10210 
   10211   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10212   symbuf = (bfd_byte *) bfd_malloc (amt);
   10213   if (symbuf == NULL)
   10214     return false;
   10215 
   10216   if (flinfo->symshndxbuf)
   10217     {
   10218       amt = sizeof (Elf_External_Sym_Shndx);
   10219       amt *= bfd_get_symcount (flinfo->output_bfd);
   10220       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   10221       if (flinfo->symshndxbuf == NULL)
   10222 	{
   10223 	  free (symbuf);
   10224 	  return false;
   10225 	}
   10226     }
   10227 
   10228   /* Now swap out the symbols.  */
   10229   for (i = 0; i < flinfo->output_bfd->symcount; i++)
   10230     {
   10231       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   10232       if (elfsym->sym.st_name == (unsigned long) -1)
   10233 	elfsym->sym.st_name = 0;
   10234       else
   10235 	elfsym->sym.st_name
   10236 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   10237 						    elfsym->sym.st_name);
   10238 
   10239       /* Inform the linker of the addition of this symbol.  */
   10240 
   10241       if (flinfo->info->callbacks->ctf_new_symbol)
   10242 	flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
   10243 						 &elfsym->sym);
   10244 
   10245       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   10246 			       ((bfd_byte *) symbuf
   10247 				+ (elfsym->dest_index
   10248 				   * bed->s->sizeof_sym)),
   10249 			       NPTR_ADD (flinfo->symshndxbuf,
   10250 					 elfsym->dest_index));
   10251     }
   10252 
   10253   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   10254   pos = hdr->sh_offset + hdr->sh_size;
   10255   amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
   10256   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   10257       && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
   10258     {
   10259       hdr->sh_size += amt;
   10260       ret = true;
   10261     }
   10262   else
   10263     ret = false;
   10264 
   10265   free (symbuf);
   10266 
   10267   free (hash_table->strtab);
   10268   hash_table->strtab = NULL;
   10269 
   10270   return ret;
   10271 }
   10272 
   10273 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   10274 
   10275 static bool
   10276 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   10277 {
   10278   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   10279       && sym->st_shndx < SHN_LORESERVE)
   10280     {
   10281       /* The gABI doesn't support dynamic symbols in output sections
   10282 	 beyond 64k.  */
   10283       _bfd_error_handler
   10284 	/* xgettext:c-format */
   10285 	(_("%pB: too many sections: %d (>= %d)"),
   10286 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   10287       bfd_set_error (bfd_error_nonrepresentable_section);
   10288       return false;
   10289     }
   10290   return true;
   10291 }
   10292 
   10293 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   10294    allowing an unsatisfied unversioned symbol in the DSO to match a
   10295    versioned symbol that would normally require an explicit version.
   10296    We also handle the case that a DSO references a hidden symbol
   10297    which may be satisfied by a versioned symbol in another DSO.  */
   10298 
   10299 static bool
   10300 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   10301 				 const struct elf_backend_data *bed,
   10302 				 struct elf_link_hash_entry *h)
   10303 {
   10304   bfd *abfd;
   10305   struct elf_link_loaded_list *loaded;
   10306 
   10307   if (!is_elf_hash_table (info->hash))
   10308     return false;
   10309 
   10310   /* Check indirect symbol.  */
   10311   while (h->root.type == bfd_link_hash_indirect)
   10312     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10313 
   10314   switch (h->root.type)
   10315     {
   10316     default:
   10317       abfd = NULL;
   10318       break;
   10319 
   10320     case bfd_link_hash_undefined:
   10321     case bfd_link_hash_undefweak:
   10322       abfd = h->root.u.undef.abfd;
   10323       if (abfd == NULL
   10324 	  || (abfd->flags & DYNAMIC) == 0
   10325 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   10326 	return false;
   10327       break;
   10328 
   10329     case bfd_link_hash_defined:
   10330     case bfd_link_hash_defweak:
   10331       abfd = h->root.u.def.section->owner;
   10332       break;
   10333 
   10334     case bfd_link_hash_common:
   10335       abfd = h->root.u.c.p->section->owner;
   10336       break;
   10337     }
   10338   BFD_ASSERT (abfd != NULL);
   10339 
   10340   for (loaded = elf_hash_table (info)->dyn_loaded;
   10341        loaded != NULL;
   10342        loaded = loaded->next)
   10343     {
   10344       bfd *input;
   10345       Elf_Internal_Shdr *hdr;
   10346       size_t symcount;
   10347       size_t extsymcount;
   10348       size_t extsymoff;
   10349       Elf_Internal_Shdr *versymhdr;
   10350       Elf_Internal_Sym *isym;
   10351       Elf_Internal_Sym *isymend;
   10352       Elf_Internal_Sym *isymbuf;
   10353       Elf_External_Versym *ever;
   10354       Elf_External_Versym *extversym;
   10355 
   10356       input = loaded->abfd;
   10357 
   10358       /* We check each DSO for a possible hidden versioned definition.  */
   10359       if (input == abfd
   10360 	  || elf_dynversym (input) == 0)
   10361 	continue;
   10362 
   10363       hdr = &elf_tdata (input)->dynsymtab_hdr;
   10364 
   10365       symcount = hdr->sh_size / bed->s->sizeof_sym;
   10366       if (elf_bad_symtab (input))
   10367 	{
   10368 	  extsymcount = symcount;
   10369 	  extsymoff = 0;
   10370 	}
   10371       else
   10372 	{
   10373 	  extsymcount = symcount - hdr->sh_info;
   10374 	  extsymoff = hdr->sh_info;
   10375 	}
   10376 
   10377       if (extsymcount == 0)
   10378 	continue;
   10379 
   10380       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   10381 				      NULL, NULL, NULL);
   10382       if (isymbuf == NULL)
   10383 	return false;
   10384 
   10385       /* Read in any version definitions.  */
   10386       versymhdr = &elf_tdata (input)->dynversym_hdr;
   10387       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   10388 	  || (extversym = (Elf_External_Versym *)
   10389 	      _bfd_malloc_and_read (input, versymhdr->sh_size,
   10390 				    versymhdr->sh_size)) == NULL)
   10391 	{
   10392 	  free (isymbuf);
   10393 	  return false;
   10394 	}
   10395 
   10396       ever = extversym + extsymoff;
   10397       isymend = isymbuf + extsymcount;
   10398       for (isym = isymbuf; isym < isymend; isym++, ever++)
   10399 	{
   10400 	  const char *name;
   10401 	  Elf_Internal_Versym iver;
   10402 	  unsigned short version_index;
   10403 
   10404 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   10405 	      || isym->st_shndx == SHN_UNDEF)
   10406 	    continue;
   10407 
   10408 	  name = bfd_elf_string_from_elf_section (input,
   10409 						  hdr->sh_link,
   10410 						  isym->st_name);
   10411 	  if (strcmp (name, h->root.root.string) != 0)
   10412 	    continue;
   10413 
   10414 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   10415 
   10416 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   10417 	      && !(h->def_regular
   10418 		   && h->forced_local))
   10419 	    {
   10420 	      /* If we have a non-hidden versioned sym, then it should
   10421 		 have provided a definition for the undefined sym unless
   10422 		 it is defined in a non-shared object and forced local.
   10423 	       */
   10424 	      abort ();
   10425 	    }
   10426 
   10427 	  version_index = iver.vs_vers & VERSYM_VERSION;
   10428 	  if (version_index == 1 || version_index == 2)
   10429 	    {
   10430 	      /* This is the base or first version.  We can use it.  */
   10431 	      free (extversym);
   10432 	      free (isymbuf);
   10433 	      return true;
   10434 	    }
   10435 	}
   10436 
   10437       free (extversym);
   10438       free (isymbuf);
   10439     }
   10440 
   10441   return false;
   10442 }
   10443 
   10444 /* Convert ELF common symbol TYPE.  */
   10445 
   10446 static int
   10447 elf_link_convert_common_type (struct bfd_link_info *info, int type)
   10448 {
   10449   /* Commom symbol can only appear in relocatable link.  */
   10450   if (!bfd_link_relocatable (info))
   10451     abort ();
   10452   switch (info->elf_stt_common)
   10453     {
   10454     case unchanged:
   10455       break;
   10456     case elf_stt_common:
   10457       type = STT_COMMON;
   10458       break;
   10459     case no_elf_stt_common:
   10460       type = STT_OBJECT;
   10461       break;
   10462     }
   10463   return type;
   10464 }
   10465 
   10466 /* Add an external symbol to the symbol table.  This is called from
   10467    the hash table traversal routine.  When generating a shared object,
   10468    we go through the symbol table twice.  The first time we output
   10469    anything that might have been forced to local scope in a version
   10470    script.  The second time we output the symbols that are still
   10471    global symbols.  */
   10472 
   10473 static bool
   10474 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   10475 {
   10476   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   10477   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   10478   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   10479   bool strip;
   10480   Elf_Internal_Sym sym;
   10481   asection *input_sec;
   10482   const struct elf_backend_data *bed;
   10483   long indx;
   10484   int ret;
   10485   unsigned int type;
   10486 
   10487   if (h->root.type == bfd_link_hash_warning)
   10488     {
   10489       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10490       if (h->root.type == bfd_link_hash_new)
   10491 	return true;
   10492     }
   10493 
   10494   /* Decide whether to output this symbol in this pass.  */
   10495   if (eoinfo->localsyms)
   10496     {
   10497       if (!h->forced_local)
   10498 	return true;
   10499     }
   10500   else
   10501     {
   10502       if (h->forced_local)
   10503 	return true;
   10504     }
   10505 
   10506   bed = get_elf_backend_data (flinfo->output_bfd);
   10507 
   10508   if (h->root.type == bfd_link_hash_undefined)
   10509     {
   10510       /* If we have an undefined symbol reference here then it must have
   10511 	 come from a shared library that is being linked in.  (Undefined
   10512 	 references in regular files have already been handled unless
   10513 	 they are in unreferenced sections which are removed by garbage
   10514 	 collection).  */
   10515       bool ignore_undef = false;
   10516 
   10517       /* Some symbols may be special in that the fact that they're
   10518 	 undefined can be safely ignored - let backend determine that.  */
   10519       if (bed->elf_backend_ignore_undef_symbol)
   10520 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   10521 
   10522       /* If we are reporting errors for this situation then do so now.  */
   10523       if (!ignore_undef
   10524 	  && h->ref_dynamic_nonweak
   10525 	  && (!h->ref_regular || flinfo->info->gc_sections)
   10526 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   10527 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   10528 	{
   10529 	  flinfo->info->callbacks->undefined_symbol
   10530 	    (flinfo->info, h->root.root.string,
   10531 	     h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
   10532 	     flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
   10533 	     && !flinfo->info->warn_unresolved_syms);
   10534 	}
   10535 
   10536       /* Strip a global symbol defined in a discarded section.  */
   10537       if (h->indx == -3)
   10538 	return true;
   10539     }
   10540 
   10541   /* We should also warn if a forced local symbol is referenced from
   10542      shared libraries.  */
   10543   if (bfd_link_executable (flinfo->info)
   10544       && h->forced_local
   10545       && h->ref_dynamic
   10546       && h->def_regular
   10547       && !h->dynamic_def
   10548       && h->ref_dynamic_nonweak
   10549       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   10550     {
   10551       bfd *def_bfd;
   10552       const char *msg;
   10553       struct elf_link_hash_entry *hi = h;
   10554 
   10555       /* Check indirect symbol.  */
   10556       while (hi->root.type == bfd_link_hash_indirect)
   10557 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   10558 
   10559       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   10560 	/* xgettext:c-format */
   10561 	msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
   10562       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   10563 	/* xgettext:c-format */
   10564 	msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
   10565       else
   10566 	/* xgettext:c-format */
   10567 	msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
   10568       def_bfd = flinfo->output_bfd;
   10569       if (hi->root.u.def.section != bfd_abs_section_ptr)
   10570 	def_bfd = hi->root.u.def.section->owner;
   10571       _bfd_error_handler (msg, flinfo->output_bfd,
   10572 			  h->root.root.string, def_bfd);
   10573       bfd_set_error (bfd_error_bad_value);
   10574       eoinfo->failed = true;
   10575       return false;
   10576     }
   10577 
   10578   /* We don't want to output symbols that have never been mentioned by
   10579      a regular file, or that we have been told to strip.  However, if
   10580      h->indx is set to -2, the symbol is used by a reloc and we must
   10581      output it.  */
   10582   strip = false;
   10583   if (h->indx == -2)
   10584     ;
   10585   else if ((h->def_dynamic
   10586 	    || h->ref_dynamic
   10587 	    || h->root.type == bfd_link_hash_new)
   10588 	   && !h->def_regular
   10589 	   && !h->ref_regular)
   10590     strip = true;
   10591   else if (flinfo->info->strip == strip_all)
   10592     strip = true;
   10593   else if (flinfo->info->strip == strip_some
   10594 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   10595 			       h->root.root.string, false, false) == NULL)
   10596     strip = true;
   10597   else if ((h->root.type == bfd_link_hash_defined
   10598 	    || h->root.type == bfd_link_hash_defweak)
   10599 	   && ((flinfo->info->strip_discarded
   10600 		&& discarded_section (h->root.u.def.section))
   10601 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   10602 		   && h->root.u.def.section->owner != NULL
   10603 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   10604     strip = true;
   10605   else if ((h->root.type == bfd_link_hash_undefined
   10606 	    || h->root.type == bfd_link_hash_undefweak)
   10607 	   && h->root.u.undef.abfd != NULL
   10608 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   10609     strip = true;
   10610 
   10611   type = h->type;
   10612 
   10613   /* If we're stripping it, and it's not a dynamic symbol, there's
   10614      nothing else to do.   However, if it is a forced local symbol or
   10615      an ifunc symbol we need to give the backend finish_dynamic_symbol
   10616      function a chance to make it dynamic.  */
   10617   if (strip
   10618       && h->dynindx == -1
   10619       && type != STT_GNU_IFUNC
   10620       && !h->forced_local)
   10621     return true;
   10622 
   10623   sym.st_value = 0;
   10624   sym.st_size = h->size;
   10625   sym.st_other = h->other;
   10626   switch (h->root.type)
   10627     {
   10628     default:
   10629     case bfd_link_hash_new:
   10630     case bfd_link_hash_warning:
   10631       abort ();
   10632       return false;
   10633 
   10634     case bfd_link_hash_undefined:
   10635     case bfd_link_hash_undefweak:
   10636       input_sec = bfd_und_section_ptr;
   10637       sym.st_shndx = SHN_UNDEF;
   10638       break;
   10639 
   10640     case bfd_link_hash_defined:
   10641     case bfd_link_hash_defweak:
   10642       {
   10643 	input_sec = h->root.u.def.section;
   10644 	if (input_sec->output_section != NULL)
   10645 	  {
   10646 	    sym.st_shndx =
   10647 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   10648 						 input_sec->output_section);
   10649 	    if (sym.st_shndx == SHN_BAD)
   10650 	      {
   10651 		_bfd_error_handler
   10652 		  /* xgettext:c-format */
   10653 		  (_("%pB: could not find output section %pA for input section %pA"),
   10654 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   10655 		bfd_set_error (bfd_error_nonrepresentable_section);
   10656 		eoinfo->failed = true;
   10657 		return false;
   10658 	      }
   10659 
   10660 	    /* ELF symbols in relocatable files are section relative,
   10661 	       but in nonrelocatable files they are virtual
   10662 	       addresses.  */
   10663 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   10664 	    if (!bfd_link_relocatable (flinfo->info))
   10665 	      {
   10666 		sym.st_value += input_sec->output_section->vma;
   10667 		if (h->type == STT_TLS)
   10668 		  {
   10669 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   10670 		    if (tls_sec != NULL)
   10671 		      sym.st_value -= tls_sec->vma;
   10672 		  }
   10673 	      }
   10674 	  }
   10675 	else
   10676 	  {
   10677 	    BFD_ASSERT (input_sec->owner == NULL
   10678 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   10679 	    sym.st_shndx = SHN_UNDEF;
   10680 	    input_sec = bfd_und_section_ptr;
   10681 	  }
   10682       }
   10683       break;
   10684 
   10685     case bfd_link_hash_common:
   10686       input_sec = h->root.u.c.p->section;
   10687       sym.st_shndx = bed->common_section_index (input_sec);
   10688       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   10689       break;
   10690 
   10691     case bfd_link_hash_indirect:
   10692       /* These symbols are created by symbol versioning.  They point
   10693 	 to the decorated version of the name.  For example, if the
   10694 	 symbol foo@@GNU_1.2 is the default, which should be used when
   10695 	 foo is used with no version, then we add an indirect symbol
   10696 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   10697 	 since the indirected symbol is already in the hash table.  */
   10698       return true;
   10699     }
   10700 
   10701   if (type == STT_COMMON || type == STT_OBJECT)
   10702     switch (h->root.type)
   10703       {
   10704       case bfd_link_hash_common:
   10705 	type = elf_link_convert_common_type (flinfo->info, type);
   10706 	break;
   10707       case bfd_link_hash_defined:
   10708       case bfd_link_hash_defweak:
   10709 	if (bed->common_definition (&sym))
   10710 	  type = elf_link_convert_common_type (flinfo->info, type);
   10711 	else
   10712 	  type = STT_OBJECT;
   10713 	break;
   10714       case bfd_link_hash_undefined:
   10715       case bfd_link_hash_undefweak:
   10716 	break;
   10717       default:
   10718 	abort ();
   10719       }
   10720 
   10721   if (h->forced_local)
   10722     {
   10723       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
   10724       /* Turn off visibility on local symbol.  */
   10725       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   10726     }
   10727   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   10728   else if (h->unique_global && h->def_regular)
   10729     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
   10730   else if (h->root.type == bfd_link_hash_undefweak
   10731 	   || h->root.type == bfd_link_hash_defweak)
   10732     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
   10733   else
   10734     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   10735   sym.st_target_internal = h->target_internal;
   10736 
   10737   /* Give the processor backend a chance to tweak the symbol value,
   10738      and also to finish up anything that needs to be done for this
   10739      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   10740      forced local syms when non-shared is due to a historical quirk.
   10741      STT_GNU_IFUNC symbol must go through PLT.  */
   10742   if ((h->type == STT_GNU_IFUNC
   10743        && h->def_regular
   10744        && !bfd_link_relocatable (flinfo->info))
   10745       || ((h->dynindx != -1
   10746 	   || h->forced_local)
   10747 	  && ((bfd_link_pic (flinfo->info)
   10748 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   10749 		   || h->root.type != bfd_link_hash_undefweak))
   10750 	      || !h->forced_local)
   10751 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   10752     {
   10753       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   10754 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   10755 	{
   10756 	  eoinfo->failed = true;
   10757 	  return false;
   10758 	}
   10759     }
   10760 
   10761   /* If we are marking the symbol as undefined, and there are no
   10762      non-weak references to this symbol from a regular object, then
   10763      mark the symbol as weak undefined; if there are non-weak
   10764      references, mark the symbol as strong.  We can't do this earlier,
   10765      because it might not be marked as undefined until the
   10766      finish_dynamic_symbol routine gets through with it.  */
   10767   if (sym.st_shndx == SHN_UNDEF
   10768       && h->ref_regular
   10769       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   10770 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   10771     {
   10772       int bindtype;
   10773       type = ELF_ST_TYPE (sym.st_info);
   10774 
   10775       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   10776       if (type == STT_GNU_IFUNC)
   10777 	type = STT_FUNC;
   10778 
   10779       if (h->ref_regular_nonweak)
   10780 	bindtype = STB_GLOBAL;
   10781       else
   10782 	bindtype = STB_WEAK;
   10783       sym.st_info = ELF_ST_INFO (bindtype, type);
   10784     }
   10785 
   10786   /* If this is a symbol defined in a dynamic library, don't use the
   10787      symbol size from the dynamic library.  Relinking an executable
   10788      against a new library may introduce gratuitous changes in the
   10789      executable's symbols if we keep the size.  */
   10790   if (sym.st_shndx == SHN_UNDEF
   10791       && !h->def_regular
   10792       && h->def_dynamic)
   10793     sym.st_size = 0;
   10794 
   10795   /* If a non-weak symbol with non-default visibility is not defined
   10796      locally, it is a fatal error.  */
   10797   if (!bfd_link_relocatable (flinfo->info)
   10798       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   10799       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   10800       && h->root.type == bfd_link_hash_undefined
   10801       && !h->def_regular)
   10802     {
   10803       const char *msg;
   10804 
   10805       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   10806 	/* xgettext:c-format */
   10807 	msg = _("%pB: protected symbol `%s' isn't defined");
   10808       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   10809 	/* xgettext:c-format */
   10810 	msg = _("%pB: internal symbol `%s' isn't defined");
   10811       else
   10812 	/* xgettext:c-format */
   10813 	msg = _("%pB: hidden symbol `%s' isn't defined");
   10814       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
   10815       bfd_set_error (bfd_error_bad_value);
   10816       eoinfo->failed = true;
   10817       return false;
   10818     }
   10819 
   10820   /* If this symbol should be put in the .dynsym section, then put it
   10821      there now.  We already know the symbol index.  We also fill in
   10822      the entry in the .hash section.  */
   10823   if (h->dynindx != -1
   10824       && elf_hash_table (flinfo->info)->dynamic_sections_created
   10825       && elf_hash_table (flinfo->info)->dynsym != NULL
   10826       && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
   10827     {
   10828       bfd_byte *esym;
   10829 
   10830       /* Since there is no version information in the dynamic string,
   10831 	 if there is no version info in symbol version section, we will
   10832 	 have a run-time problem if not linking executable, referenced
   10833 	 by shared library, or not bound locally.  */
   10834       if (h->verinfo.verdef == NULL
   10835 	  && (!bfd_link_executable (flinfo->info)
   10836 	      || h->ref_dynamic
   10837 	      || !h->def_regular))
   10838 	{
   10839 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   10840 
   10841 	  if (p && p [1] != '\0')
   10842 	    {
   10843 	      _bfd_error_handler
   10844 		/* xgettext:c-format */
   10845 		(_("%pB: no symbol version section for versioned symbol `%s'"),
   10846 		 flinfo->output_bfd, h->root.root.string);
   10847 	      eoinfo->failed = true;
   10848 	      return false;
   10849 	    }
   10850 	}
   10851 
   10852       sym.st_name = h->dynstr_index;
   10853       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   10854 	      + h->dynindx * bed->s->sizeof_sym);
   10855       if (!check_dynsym (flinfo->output_bfd, &sym))
   10856 	{
   10857 	  eoinfo->failed = true;
   10858 	  return false;
   10859 	}
   10860 
   10861       /* Inform the linker of the addition of this symbol.  */
   10862 
   10863       if (flinfo->info->callbacks->ctf_new_dynsym)
   10864 	flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
   10865 
   10866       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   10867 
   10868       if (flinfo->hash_sec != NULL)
   10869 	{
   10870 	  size_t hash_entry_size;
   10871 	  bfd_byte *bucketpos;
   10872 	  bfd_vma chain;
   10873 	  size_t bucketcount;
   10874 	  size_t bucket;
   10875 
   10876 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   10877 	  bucket = h->u.elf_hash_value % bucketcount;
   10878 
   10879 	  hash_entry_size
   10880 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   10881 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   10882 		       + (bucket + 2) * hash_entry_size);
   10883 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   10884 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   10885 		   bucketpos);
   10886 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   10887 		   ((bfd_byte *) flinfo->hash_sec->contents
   10888 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   10889 	}
   10890 
   10891       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   10892 	{
   10893 	  Elf_Internal_Versym iversym;
   10894 	  Elf_External_Versym *eversym;
   10895 
   10896 	  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   10897 	    {
   10898 	      if (h->verinfo.verdef == NULL
   10899 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   10900 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   10901 		iversym.vs_vers = 1;
   10902 	      else
   10903 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   10904 	    }
   10905 	  else
   10906 	    {
   10907 	      if (h->verinfo.vertree == NULL)
   10908 		iversym.vs_vers = 1;
   10909 	      else
   10910 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   10911 	      if (flinfo->info->create_default_symver)
   10912 		iversym.vs_vers++;
   10913 	    }
   10914 
   10915 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
   10916 	     defined locally.  */
   10917 	  if (h->versioned == versioned_hidden && h->def_regular)
   10918 	    iversym.vs_vers |= VERSYM_HIDDEN;
   10919 
   10920 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   10921 	  eversym += h->dynindx;
   10922 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   10923 	}
   10924     }
   10925 
   10926   /* If the symbol is undefined, and we didn't output it to .dynsym,
   10927      strip it from .symtab too.  Obviously we can't do this for
   10928      relocatable output or when needed for --emit-relocs.  */
   10929   else if (input_sec == bfd_und_section_ptr
   10930 	   && h->indx != -2
   10931 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
   10932 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
   10933 	   && !bfd_link_relocatable (flinfo->info))
   10934     return true;
   10935 
   10936   /* Also strip others that we couldn't earlier due to dynamic symbol
   10937      processing.  */
   10938   if (strip)
   10939     return true;
   10940   if ((input_sec->flags & SEC_EXCLUDE) != 0)
   10941     return true;
   10942 
   10943   /* Output a FILE symbol so that following locals are not associated
   10944      with the wrong input file.  We need one for forced local symbols
   10945      if we've seen more than one FILE symbol or when we have exactly
   10946      one FILE symbol but global symbols are present in a file other
   10947      than the one with the FILE symbol.  We also need one if linker
   10948      defined symbols are present.  In practice these conditions are
   10949      always met, so just emit the FILE symbol unconditionally.  */
   10950   if (eoinfo->localsyms
   10951       && !eoinfo->file_sym_done
   10952       && eoinfo->flinfo->filesym_count != 0)
   10953     {
   10954       Elf_Internal_Sym fsym;
   10955 
   10956       memset (&fsym, 0, sizeof (fsym));
   10957       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   10958       fsym.st_shndx = SHN_ABS;
   10959       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   10960 				      bfd_und_section_ptr, NULL))
   10961 	return false;
   10962 
   10963       eoinfo->file_sym_done = true;
   10964     }
   10965 
   10966   indx = bfd_get_symcount (flinfo->output_bfd);
   10967   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   10968 				   input_sec, h);
   10969   if (ret == 0)
   10970     {
   10971       eoinfo->failed = true;
   10972       return false;
   10973     }
   10974   else if (ret == 1)
   10975     h->indx = indx;
   10976   else if (h->indx == -2)
   10977     abort();
   10978 
   10979   return true;
   10980 }
   10981 
   10982 /* Return TRUE if special handling is done for relocs in SEC against
   10983    symbols defined in discarded sections.  */
   10984 
   10985 static bool
   10986 elf_section_ignore_discarded_relocs (asection *sec)
   10987 {
   10988   const struct elf_backend_data *bed;
   10989 
   10990   switch (sec->sec_info_type)
   10991     {
   10992     case SEC_INFO_TYPE_STABS:
   10993     case SEC_INFO_TYPE_EH_FRAME:
   10994     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   10995     case SEC_INFO_TYPE_SFRAME:
   10996       return true;
   10997     default:
   10998       break;
   10999     }
   11000 
   11001   bed = get_elf_backend_data (sec->owner);
   11002   if (bed->elf_backend_ignore_discarded_relocs != NULL
   11003       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   11004     return true;
   11005 
   11006   return false;
   11007 }
   11008 
   11009 /* Return a mask saying how ld should treat relocations in SEC against
   11010    symbols defined in discarded sections.  If this function returns
   11011    COMPLAIN set, ld will issue a warning message.  If this function
   11012    returns PRETEND set, and the discarded section was link-once and the
   11013    same size as the kept link-once section, ld will pretend that the
   11014    symbol was actually defined in the kept section.  Otherwise ld will
   11015    zero the reloc (at least that is the intent, but some cooperation by
   11016    the target dependent code is needed, particularly for REL targets).  */
   11017 
   11018 unsigned int
   11019 _bfd_elf_default_action_discarded (asection *sec)
   11020 {
   11021   const struct elf_backend_data *bed;
   11022   bed = get_elf_backend_data (sec->owner);
   11023 
   11024   if (sec->flags & SEC_DEBUGGING)
   11025     return PRETEND;
   11026 
   11027   if (strcmp (".eh_frame", sec->name) == 0)
   11028     return 0;
   11029 
   11030   if (bed->elf_backend_can_make_multiple_eh_frame
   11031       && strncmp (sec->name, ".eh_frame.", 10) == 0)
   11032     return 0;
   11033 
   11034   if (strcmp (".sframe", sec->name) == 0)
   11035     return 0;
   11036 
   11037   if (strcmp (".gcc_except_table", sec->name) == 0)
   11038     return 0;
   11039 
   11040   return COMPLAIN | PRETEND;
   11041 }
   11042 
   11043 /* Find a match between a section and a member of a section group.  */
   11044 
   11045 static asection *
   11046 match_group_member (asection *sec, asection *group,
   11047 		    struct bfd_link_info *info)
   11048 {
   11049   asection *first = elf_next_in_group (group);
   11050   asection *s = first;
   11051 
   11052   while (s != NULL)
   11053     {
   11054       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   11055 	return s;
   11056 
   11057       s = elf_next_in_group (s);
   11058       if (s == first)
   11059 	break;
   11060     }
   11061 
   11062   return NULL;
   11063 }
   11064 
   11065 /* Check if the kept section of a discarded section SEC can be used
   11066    to replace it.  Return the replacement if it is OK.  Otherwise return
   11067    NULL.  */
   11068 
   11069 asection *
   11070 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   11071 {
   11072   asection *kept;
   11073 
   11074   kept = sec->kept_section;
   11075   if (kept != NULL)
   11076     {
   11077       if ((kept->flags & SEC_GROUP) != 0)
   11078 	kept = match_group_member (sec, kept, info);
   11079       if (kept != NULL)
   11080 	{
   11081 	  if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   11082 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size))
   11083 	    kept = NULL;
   11084 	  else
   11085 	    {
   11086 	      /* Get the real kept section.  */
   11087 	      asection *next;
   11088 	      for (next = kept->kept_section;
   11089 		   next != NULL;
   11090 		   next = next->kept_section)
   11091 		kept = next;
   11092 	    }
   11093 	}
   11094       sec->kept_section = kept;
   11095     }
   11096   return kept;
   11097 }
   11098 
   11099 /* Link an input file into the linker output file.  This function
   11100    handles all the sections and relocations of the input file at once.
   11101    This is so that we only have to read the local symbols once, and
   11102    don't have to keep them in memory.  */
   11103 
   11104 static bool
   11105 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   11106 {
   11107   int (*relocate_section)
   11108     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   11109      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   11110   bfd *output_bfd;
   11111   Elf_Internal_Shdr *symtab_hdr;
   11112   size_t locsymcount;
   11113   size_t extsymoff;
   11114   Elf_Internal_Sym *isymbuf;
   11115   Elf_Internal_Sym *isym;
   11116   Elf_Internal_Sym *isymend;
   11117   long *pindex;
   11118   asection **ppsection;
   11119   asection *o;
   11120   const struct elf_backend_data *bed;
   11121   struct elf_link_hash_entry **sym_hashes;
   11122   bfd_size_type address_size;
   11123   bfd_vma r_type_mask;
   11124   int r_sym_shift;
   11125   bool have_file_sym = false;
   11126 
   11127   output_bfd = flinfo->output_bfd;
   11128   bed = get_elf_backend_data (output_bfd);
   11129   relocate_section = bed->elf_backend_relocate_section;
   11130 
   11131   /* If this is a dynamic object, we don't want to do anything here:
   11132      we don't want the local symbols, and we don't want the section
   11133      contents.  */
   11134   if ((input_bfd->flags & DYNAMIC) != 0)
   11135     return true;
   11136 
   11137   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   11138   if (elf_bad_symtab (input_bfd))
   11139     {
   11140       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   11141       extsymoff = 0;
   11142     }
   11143   else
   11144     {
   11145       locsymcount = symtab_hdr->sh_info;
   11146       extsymoff = symtab_hdr->sh_info;
   11147     }
   11148 
   11149   /* Enable GNU OSABI features in the output BFD that are used in the input
   11150      BFD.  */
   11151   if (bed->elf_osabi == ELFOSABI_NONE
   11152       || bed->elf_osabi == ELFOSABI_GNU
   11153       || bed->elf_osabi == ELFOSABI_FREEBSD)
   11154     elf_tdata (output_bfd)->has_gnu_osabi
   11155       |= (elf_tdata (input_bfd)->has_gnu_osabi
   11156 	  & (bfd_link_relocatable (flinfo->info)
   11157 	     ? -1 : ~elf_gnu_osabi_retain));
   11158 
   11159   /* Read the local symbols.  */
   11160   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   11161   if (isymbuf == NULL && locsymcount != 0)
   11162     {
   11163       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   11164 				      flinfo->internal_syms,
   11165 				      flinfo->external_syms,
   11166 				      flinfo->locsym_shndx);
   11167       if (isymbuf == NULL)
   11168 	return false;
   11169     }
   11170 
   11171   /* Find local symbol sections and adjust values of symbols in
   11172      SEC_MERGE sections.  Write out those local symbols we know are
   11173      going into the output file.  */
   11174   isymend = PTR_ADD (isymbuf, locsymcount);
   11175   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   11176        isym < isymend;
   11177        isym++, pindex++, ppsection++)
   11178     {
   11179       asection *isec;
   11180       const char *name;
   11181       Elf_Internal_Sym osym;
   11182       long indx;
   11183       int ret;
   11184 
   11185       *pindex = -1;
   11186 
   11187       if (elf_bad_symtab (input_bfd))
   11188 	{
   11189 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   11190 	    {
   11191 	      *ppsection = NULL;
   11192 	      continue;
   11193 	    }
   11194 	}
   11195 
   11196       if (isym->st_shndx == SHN_UNDEF)
   11197 	isec = bfd_und_section_ptr;
   11198       else if (isym->st_shndx == SHN_ABS)
   11199 	isec = bfd_abs_section_ptr;
   11200       else if (isym->st_shndx == SHN_COMMON)
   11201 	isec = bfd_com_section_ptr;
   11202       else
   11203 	{
   11204 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   11205 	  if (isec == NULL)
   11206 	    {
   11207 	      /* Don't attempt to output symbols with st_shnx in the
   11208 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   11209 	      isec = bfd_und_section_ptr;
   11210 	    }
   11211 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   11212 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   11213 	    isym->st_value =
   11214 	      _bfd_merged_section_offset (output_bfd, &isec,
   11215 					  elf_section_data (isec)->sec_info,
   11216 					  isym->st_value);
   11217 	}
   11218 
   11219       *ppsection = isec;
   11220 
   11221       /* Don't output the first, undefined, symbol.  In fact, don't
   11222 	 output any undefined local symbol.  */
   11223       if (isec == bfd_und_section_ptr)
   11224 	continue;
   11225 
   11226       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   11227 	{
   11228 	  /* We never output section symbols.  Instead, we use the
   11229 	     section symbol of the corresponding section in the output
   11230 	     file.  */
   11231 	  continue;
   11232 	}
   11233 
   11234       /* If we are stripping all symbols, we don't want to output this
   11235 	 one.  */
   11236       if (flinfo->info->strip == strip_all)
   11237 	continue;
   11238 
   11239       /* If we are discarding all local symbols, we don't want to
   11240 	 output this one.  If we are generating a relocatable output
   11241 	 file, then some of the local symbols may be required by
   11242 	 relocs; we output them below as we discover that they are
   11243 	 needed.  */
   11244       if (flinfo->info->discard == discard_all)
   11245 	continue;
   11246 
   11247       /* If this symbol is defined in a section which we are
   11248 	 discarding, we don't need to keep it.  */
   11249       if (isym->st_shndx < SHN_LORESERVE
   11250 	  && (isec->output_section == NULL
   11251 	      || bfd_section_removed_from_list (output_bfd,
   11252 						isec->output_section)))
   11253 	continue;
   11254 
   11255       /* Get the name of the symbol.  */
   11256       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   11257 					      isym->st_name);
   11258       if (name == NULL)
   11259 	return false;
   11260 
   11261       /* See if we are discarding symbols with this name.  */
   11262       if ((flinfo->info->strip == strip_some
   11263 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
   11264 	       == NULL))
   11265 	  || (((flinfo->info->discard == discard_sec_merge
   11266 		&& (isec->flags & SEC_MERGE)
   11267 		&& !bfd_link_relocatable (flinfo->info))
   11268 	       || flinfo->info->discard == discard_l)
   11269 	      && bfd_is_local_label_name (input_bfd, name)))
   11270 	continue;
   11271 
   11272       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   11273 	{
   11274 	  if (input_bfd->lto_output)
   11275 	    /* -flto puts a temp file name here.  This means builds
   11276 	       are not reproducible.  Discard the symbol.  */
   11277 	    continue;
   11278 	  have_file_sym = true;
   11279 	  flinfo->filesym_count += 1;
   11280 	}
   11281       if (!have_file_sym)
   11282 	{
   11283 	  /* In the absence of debug info, bfd_find_nearest_line uses
   11284 	     FILE symbols to determine the source file for local
   11285 	     function symbols.  Provide a FILE symbol here if input
   11286 	     files lack such, so that their symbols won't be
   11287 	     associated with a previous input file.  It's not the
   11288 	     source file, but the best we can do.  */
   11289 	  const char *filename;
   11290 	  have_file_sym = true;
   11291 	  flinfo->filesym_count += 1;
   11292 	  memset (&osym, 0, sizeof (osym));
   11293 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   11294 	  osym.st_shndx = SHN_ABS;
   11295 	  if (input_bfd->lto_output)
   11296 	    filename = NULL;
   11297 	  else
   11298 	    filename = lbasename (bfd_get_filename (input_bfd));
   11299 	  if (!elf_link_output_symstrtab (flinfo, filename, &osym,
   11300 					  bfd_abs_section_ptr, NULL))
   11301 	    return false;
   11302 	}
   11303 
   11304       osym = *isym;
   11305 
   11306       /* Adjust the section index for the output file.  */
   11307       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11308 							 isec->output_section);
   11309       if (osym.st_shndx == SHN_BAD)
   11310 	return false;
   11311 
   11312       /* ELF symbols in relocatable files are section relative, but
   11313 	 in executable files they are virtual addresses.  Note that
   11314 	 this code assumes that all ELF sections have an associated
   11315 	 BFD section with a reasonable value for output_offset; below
   11316 	 we assume that they also have a reasonable value for
   11317 	 output_section.  Any special sections must be set up to meet
   11318 	 these requirements.  */
   11319       osym.st_value += isec->output_offset;
   11320       if (!bfd_link_relocatable (flinfo->info))
   11321 	{
   11322 	  osym.st_value += isec->output_section->vma;
   11323 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   11324 	    {
   11325 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   11326 	      if (elf_hash_table (flinfo->info)->tls_sec != NULL)
   11327 		osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   11328 	      else
   11329 		osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
   11330 					    STT_NOTYPE);
   11331 	    }
   11332 	}
   11333 
   11334       indx = bfd_get_symcount (output_bfd);
   11335       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   11336       if (ret == 0)
   11337 	return false;
   11338       else if (ret == 1)
   11339 	*pindex = indx;
   11340     }
   11341 
   11342   if (bed->s->arch_size == 32)
   11343     {
   11344       r_type_mask = 0xff;
   11345       r_sym_shift = 8;
   11346       address_size = 4;
   11347     }
   11348   else
   11349     {
   11350       r_type_mask = 0xffffffff;
   11351       r_sym_shift = 32;
   11352       address_size = 8;
   11353     }
   11354 
   11355   /* Relocate the contents of each section.  */
   11356   sym_hashes = elf_sym_hashes (input_bfd);
   11357   for (o = input_bfd->sections; o != NULL; o = o->next)
   11358     {
   11359       bfd_byte *contents;
   11360 
   11361       if (! o->linker_mark)
   11362 	{
   11363 	  /* This section was omitted from the link.  */
   11364 	  continue;
   11365 	}
   11366 
   11367       if (!flinfo->info->resolve_section_groups
   11368 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   11369 	{
   11370 	  /* Deal with the group signature symbol.  */
   11371 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   11372 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   11373 	  asection *osec = o->output_section;
   11374 
   11375 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
   11376 	  if (symndx >= locsymcount
   11377 	      || (elf_bad_symtab (input_bfd)
   11378 		  && flinfo->sections[symndx] == NULL))
   11379 	    {
   11380 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
   11381 	      while (h->root.type == bfd_link_hash_indirect
   11382 		     || h->root.type == bfd_link_hash_warning)
   11383 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   11384 	      /* Arrange for symbol to be output.  */
   11385 	      h->indx = -2;
   11386 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   11387 	    }
   11388 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   11389 	    {
   11390 	      /* We'll use the output section target_index.  */
   11391 	      asection *sec = flinfo->sections[symndx]->output_section;
   11392 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   11393 	    }
   11394 	  else
   11395 	    {
   11396 	      if (flinfo->indices[symndx] == -1)
   11397 		{
   11398 		  /* Otherwise output the local symbol now.  */
   11399 		  Elf_Internal_Sym sym = isymbuf[symndx];
   11400 		  asection *sec = flinfo->sections[symndx]->output_section;
   11401 		  const char *name;
   11402 		  long indx;
   11403 		  int ret;
   11404 
   11405 		  name = bfd_elf_string_from_elf_section (input_bfd,
   11406 							  symtab_hdr->sh_link,
   11407 							  sym.st_name);
   11408 		  if (name == NULL)
   11409 		    return false;
   11410 
   11411 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   11412 								    sec);
   11413 		  if (sym.st_shndx == SHN_BAD)
   11414 		    return false;
   11415 
   11416 		  sym.st_value += o->output_offset;
   11417 
   11418 		  indx = bfd_get_symcount (output_bfd);
   11419 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   11420 						   NULL);
   11421 		  if (ret == 0)
   11422 		    return false;
   11423 		  else if (ret == 1)
   11424 		    flinfo->indices[symndx] = indx;
   11425 		  else
   11426 		    abort ();
   11427 		}
   11428 	      elf_section_data (osec)->this_hdr.sh_info
   11429 		= flinfo->indices[symndx];
   11430 	    }
   11431 	}
   11432 
   11433       if ((o->flags & SEC_HAS_CONTENTS) == 0
   11434 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   11435 	continue;
   11436 
   11437       if ((o->flags & SEC_LINKER_CREATED) != 0)
   11438 	{
   11439 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   11440 	     or somesuch.  */
   11441 	  continue;
   11442 	}
   11443 
   11444       /* Get the contents of the section.  They have been cached by a
   11445 	 relaxation routine.  Note that o is a section in an input
   11446 	 file, so the contents field will not have been set by any of
   11447 	 the routines which work on output files.  */
   11448       if (elf_section_data (o)->this_hdr.contents != NULL)
   11449 	{
   11450 	  contents = elf_section_data (o)->this_hdr.contents;
   11451 	  if (bed->caches_rawsize
   11452 	      && o->rawsize != 0
   11453 	      && o->rawsize < o->size)
   11454 	    {
   11455 	      memcpy (flinfo->contents, contents, o->rawsize);
   11456 	      contents = flinfo->contents;
   11457 	    }
   11458 	}
   11459       else if (!(o->flags & SEC_RELOC)
   11460 	       && !bed->elf_backend_write_section
   11461 	       && o->sec_info_type == SEC_INFO_TYPE_MERGE)
   11462 	/* A MERGE section that has no relocations doesn't need the
   11463 	   contents anymore, they have been recorded earlier.  Except
   11464 	   if the backend has special provisions for writing sections.  */
   11465 	contents = NULL;
   11466       else
   11467 	{
   11468 	  contents = flinfo->contents;
   11469 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   11470 	    return false;
   11471 	}
   11472 
   11473       if ((o->flags & SEC_RELOC) != 0)
   11474 	{
   11475 	  Elf_Internal_Rela *internal_relocs;
   11476 	  Elf_Internal_Rela *rel, *relend;
   11477 	  int action_discarded;
   11478 	  int ret;
   11479 
   11480 	  /* Get the swapped relocs.  */
   11481 	  internal_relocs
   11482 	    = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
   11483 					      flinfo->external_relocs,
   11484 					      flinfo->internal_relocs,
   11485 					      false);
   11486 	  if (internal_relocs == NULL
   11487 	      && o->reloc_count > 0)
   11488 	    return false;
   11489 
   11490 	  action_discarded = -1;
   11491 	  if (!elf_section_ignore_discarded_relocs (o))
   11492 	    action_discarded = (*bed->action_discarded) (o);
   11493 
   11494 	  /* Run through the relocs evaluating complex reloc symbols and
   11495 	     looking for relocs against symbols from discarded sections
   11496 	     or section symbols from removed link-once sections.
   11497 	     Complain about relocs against discarded sections.  Zero
   11498 	     relocs against removed link-once sections.  */
   11499 
   11500 	  rel = internal_relocs;
   11501 	  relend = rel + o->reloc_count;
   11502 	  for ( ; rel < relend; rel++)
   11503 	    {
   11504 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   11505 	      unsigned int s_type;
   11506 	      asection **ps, *sec;
   11507 	      struct elf_link_hash_entry *h = NULL;
   11508 	      const char *sym_name;
   11509 
   11510 	      if (r_symndx == STN_UNDEF)
   11511 		continue;
   11512 
   11513 	      if (r_symndx >= locsymcount
   11514 		  || (elf_bad_symtab (input_bfd)
   11515 		      && flinfo->sections[r_symndx] == NULL))
   11516 		{
   11517 		  h = sym_hashes[r_symndx - extsymoff];
   11518 
   11519 		  /* Badly formatted input files can contain relocs that
   11520 		     reference non-existant symbols.  Check here so that
   11521 		     we do not seg fault.  */
   11522 		  if (h == NULL)
   11523 		    {
   11524 		      _bfd_error_handler
   11525 			/* xgettext:c-format */
   11526 			(_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
   11527 			   "that references a non-existent global symbol"),
   11528 			 input_bfd, (uint64_t) rel->r_info, o);
   11529 		      bfd_set_error (bfd_error_bad_value);
   11530 		      return false;
   11531 		    }
   11532 
   11533 		  while (h->root.type == bfd_link_hash_indirect
   11534 			 || h->root.type == bfd_link_hash_warning)
   11535 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   11536 
   11537 		  s_type = h->type;
   11538 
   11539 		  /* If a plugin symbol is referenced from a non-IR file,
   11540 		     mark the symbol as undefined.  Note that the
   11541 		     linker may attach linker created dynamic sections
   11542 		     to the plugin bfd.  Symbols defined in linker
   11543 		     created sections are not plugin symbols.  */
   11544 		  if ((h->root.non_ir_ref_regular
   11545 		       || h->root.non_ir_ref_dynamic)
   11546 		      && (h->root.type == bfd_link_hash_defined
   11547 			  || h->root.type == bfd_link_hash_defweak)
   11548 		      && (h->root.u.def.section->flags
   11549 			  & SEC_LINKER_CREATED) == 0
   11550 		      && h->root.u.def.section->owner != NULL
   11551 		      && (h->root.u.def.section->owner->flags
   11552 			  & BFD_PLUGIN) != 0)
   11553 		    {
   11554 		      h->root.type = bfd_link_hash_undefined;
   11555 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   11556 		    }
   11557 
   11558 		  ps = NULL;
   11559 		  if (h->root.type == bfd_link_hash_defined
   11560 		      || h->root.type == bfd_link_hash_defweak)
   11561 		    ps = &h->root.u.def.section;
   11562 
   11563 		  sym_name = h->root.root.string;
   11564 		}
   11565 	      else
   11566 		{
   11567 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   11568 
   11569 		  s_type = ELF_ST_TYPE (sym->st_info);
   11570 		  ps = &flinfo->sections[r_symndx];
   11571 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   11572 					       sym, *ps);
   11573 		}
   11574 
   11575 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   11576 		  && !bfd_link_relocatable (flinfo->info))
   11577 		{
   11578 		  bfd_vma val;
   11579 		  bfd_vma dot = (rel->r_offset
   11580 				 + o->output_offset + o->output_section->vma);
   11581 #ifdef DEBUG
   11582 		  printf ("Encountered a complex symbol!");
   11583 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   11584 			  bfd_get_filename (input_bfd), o->name,
   11585 			  (long) (rel - internal_relocs));
   11586 		  printf (" symbol: idx  %8.8lx, name %s\n",
   11587 			  r_symndx, sym_name);
   11588 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   11589 			  (unsigned long) rel->r_info,
   11590 			  (unsigned long) rel->r_offset);
   11591 #endif
   11592 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   11593 				    isymbuf, locsymcount, s_type == STT_SRELC))
   11594 		    return false;
   11595 
   11596 		  /* Symbol evaluated OK.  Update to absolute value.  */
   11597 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   11598 				    r_symndx, val);
   11599 		  continue;
   11600 		}
   11601 
   11602 	      if (action_discarded != -1 && ps != NULL)
   11603 		{
   11604 		  /* Complain if the definition comes from a
   11605 		     discarded section.  */
   11606 		  if ((sec = *ps) != NULL && discarded_section (sec))
   11607 		    {
   11608 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   11609 		      if (action_discarded & COMPLAIN)
   11610 			(*flinfo->info->callbacks->einfo)
   11611 			  /* xgettext:c-format */
   11612 			  (_("%X`%s' referenced in section `%pA' of %pB: "
   11613 			     "defined in discarded section `%pA' of %pB\n"),
   11614 			   sym_name, o, input_bfd, sec, sec->owner);
   11615 
   11616 		      /* Try to do the best we can to support buggy old
   11617 			 versions of gcc.  Pretend that the symbol is
   11618 			 really defined in the kept linkonce section.
   11619 			 FIXME: This is quite broken.  Modifying the
   11620 			 symbol here means we will be changing all later
   11621 			 uses of the symbol, not just in this section.  */
   11622 		      if (action_discarded & PRETEND)
   11623 			{
   11624 			  asection *kept;
   11625 
   11626 			  kept = _bfd_elf_check_kept_section (sec,
   11627 							      flinfo->info);
   11628 			  if (kept != NULL)
   11629 			    {
   11630 			      *ps = kept;
   11631 			      continue;
   11632 			    }
   11633 			}
   11634 		    }
   11635 		}
   11636 	    }
   11637 
   11638 	  /* Relocate the section by invoking a back end routine.
   11639 
   11640 	     The back end routine is responsible for adjusting the
   11641 	     section contents as necessary, and (if using Rela relocs
   11642 	     and generating a relocatable output file) adjusting the
   11643 	     reloc addend as necessary.
   11644 
   11645 	     The back end routine does not have to worry about setting
   11646 	     the reloc address or the reloc symbol index.
   11647 
   11648 	     The back end routine is given a pointer to the swapped in
   11649 	     internal symbols, and can access the hash table entries
   11650 	     for the external symbols via elf_sym_hashes (input_bfd).
   11651 
   11652 	     When generating relocatable output, the back end routine
   11653 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   11654 	     output symbol is going to be a section symbol
   11655 	     corresponding to the output section, which will require
   11656 	     the addend to be adjusted.  */
   11657 
   11658 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   11659 				     input_bfd, o, contents,
   11660 				     internal_relocs,
   11661 				     isymbuf,
   11662 				     flinfo->sections);
   11663 	  if (!ret)
   11664 	    return false;
   11665 
   11666 	  if (ret == 2
   11667 	      || bfd_link_relocatable (flinfo->info)
   11668 	      || flinfo->info->emitrelocations)
   11669 	    {
   11670 	      Elf_Internal_Rela *irela;
   11671 	      Elf_Internal_Rela *irelaend, *irelamid;
   11672 	      bfd_vma last_offset;
   11673 	      struct elf_link_hash_entry **rel_hash;
   11674 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   11675 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   11676 	      unsigned int next_erel;
   11677 	      bool rela_normal;
   11678 	      struct bfd_elf_section_data *esdi, *esdo;
   11679 
   11680 	      esdi = elf_section_data (o);
   11681 	      esdo = elf_section_data (o->output_section);
   11682 	      rela_normal = false;
   11683 
   11684 	      /* Adjust the reloc addresses and symbol indices.  */
   11685 
   11686 	      irela = internal_relocs;
   11687 	      irelaend = irela + o->reloc_count;
   11688 	      rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
   11689 	      /* We start processing the REL relocs, if any.  When we reach
   11690 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   11691 	      irelamid = irela;
   11692 	      if (esdi->rel.hdr != NULL)
   11693 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   11694 			     * bed->s->int_rels_per_ext_rel);
   11695 	      rel_hash_list = rel_hash;
   11696 	      rela_hash_list = NULL;
   11697 	      last_offset = o->output_offset;
   11698 	      if (!bfd_link_relocatable (flinfo->info))
   11699 		last_offset += o->output_section->vma;
   11700 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   11701 		{
   11702 		  unsigned long r_symndx;
   11703 		  asection *sec;
   11704 		  Elf_Internal_Sym sym;
   11705 
   11706 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   11707 		    {
   11708 		      rel_hash++;
   11709 		      next_erel = 0;
   11710 		    }
   11711 
   11712 		  if (irela == irelamid)
   11713 		    {
   11714 		      rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
   11715 		      rela_hash_list = rel_hash;
   11716 		      rela_normal = bed->rela_normal;
   11717 		    }
   11718 
   11719 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   11720 							     flinfo->info, o,
   11721 							     irela->r_offset);
   11722 		  if (irela->r_offset >= (bfd_vma) -2)
   11723 		    {
   11724 		      /* This is a reloc for a deleted entry or somesuch.
   11725 			 Turn it into an R_*_NONE reloc, at the same
   11726 			 offset as the last reloc.  elf_eh_frame.c and
   11727 			 bfd_elf_discard_info rely on reloc offsets
   11728 			 being ordered.  */
   11729 		      irela->r_offset = last_offset;
   11730 		      irela->r_info = 0;
   11731 		      irela->r_addend = 0;
   11732 		      continue;
   11733 		    }
   11734 
   11735 		  irela->r_offset += o->output_offset;
   11736 
   11737 		  /* Relocs in an executable have to be virtual addresses.  */
   11738 		  if (!bfd_link_relocatable (flinfo->info))
   11739 		    irela->r_offset += o->output_section->vma;
   11740 
   11741 		  last_offset = irela->r_offset;
   11742 
   11743 		  r_symndx = irela->r_info >> r_sym_shift;
   11744 		  if (r_symndx == STN_UNDEF)
   11745 		    continue;
   11746 
   11747 		  if (r_symndx >= locsymcount
   11748 		      || (elf_bad_symtab (input_bfd)
   11749 			  && flinfo->sections[r_symndx] == NULL))
   11750 		    {
   11751 		      struct elf_link_hash_entry *rh;
   11752 		      unsigned long indx;
   11753 
   11754 		      /* This is a reloc against a global symbol.  We
   11755 			 have not yet output all the local symbols, so
   11756 			 we do not know the symbol index of any global
   11757 			 symbol.  We set the rel_hash entry for this
   11758 			 reloc to point to the global hash table entry
   11759 			 for this symbol.  The symbol index is then
   11760 			 set at the end of bfd_elf_final_link.  */
   11761 		      indx = r_symndx - extsymoff;
   11762 		      rh = elf_sym_hashes (input_bfd)[indx];
   11763 		      while (rh->root.type == bfd_link_hash_indirect
   11764 			     || rh->root.type == bfd_link_hash_warning)
   11765 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
   11766 
   11767 		      /* Setting the index to -2 tells
   11768 			 elf_link_output_extsym that this symbol is
   11769 			 used by a reloc.  */
   11770 		      BFD_ASSERT (rh->indx < 0);
   11771 		      rh->indx = -2;
   11772 		      *rel_hash = rh;
   11773 
   11774 		      continue;
   11775 		    }
   11776 
   11777 		  /* This is a reloc against a local symbol.  */
   11778 
   11779 		  *rel_hash = NULL;
   11780 		  sym = isymbuf[r_symndx];
   11781 		  sec = flinfo->sections[r_symndx];
   11782 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   11783 		    {
   11784 		      /* I suppose the backend ought to fill in the
   11785 			 section of any STT_SECTION symbol against a
   11786 			 processor specific section.  */
   11787 		      r_symndx = STN_UNDEF;
   11788 		      if (bfd_is_abs_section (sec))
   11789 			;
   11790 		      else if (sec == NULL || sec->owner == NULL)
   11791 			{
   11792 			  bfd_set_error (bfd_error_bad_value);
   11793 			  return false;
   11794 			}
   11795 		      else
   11796 			{
   11797 			  asection *osec = sec->output_section;
   11798 
   11799 			  /* If we have discarded a section, the output
   11800 			     section will be the absolute section.  In
   11801 			     case of discarded SEC_MERGE sections, use
   11802 			     the kept section.  relocate_section should
   11803 			     have already handled discarded linkonce
   11804 			     sections.  */
   11805 			  if (bfd_is_abs_section (osec)
   11806 			      && sec->kept_section != NULL
   11807 			      && sec->kept_section->output_section != NULL)
   11808 			    {
   11809 			      osec = sec->kept_section->output_section;
   11810 			      irela->r_addend -= osec->vma;
   11811 			    }
   11812 
   11813 			  if (!bfd_is_abs_section (osec))
   11814 			    {
   11815 			      r_symndx = osec->target_index;
   11816 			      if (r_symndx == STN_UNDEF)
   11817 				{
   11818 				  irela->r_addend += osec->vma;
   11819 				  osec = _bfd_nearby_section (output_bfd, osec,
   11820 							      osec->vma);
   11821 				  irela->r_addend -= osec->vma;
   11822 				  r_symndx = osec->target_index;
   11823 				}
   11824 			    }
   11825 			}
   11826 
   11827 		      /* Adjust the addend according to where the
   11828 			 section winds up in the output section.  */
   11829 		      if (rela_normal)
   11830 			irela->r_addend += sec->output_offset;
   11831 		    }
   11832 		  else
   11833 		    {
   11834 		      if (flinfo->indices[r_symndx] == -1)
   11835 			{
   11836 			  unsigned long shlink;
   11837 			  const char *name;
   11838 			  asection *osec;
   11839 			  long indx;
   11840 
   11841 			  if (flinfo->info->strip == strip_all)
   11842 			    {
   11843 			      /* You can't do ld -r -s.  */
   11844 			      bfd_set_error (bfd_error_invalid_operation);
   11845 			      return false;
   11846 			    }
   11847 
   11848 			  /* This symbol was skipped earlier, but
   11849 			     since it is needed by a reloc, we
   11850 			     must output it now.  */
   11851 			  shlink = symtab_hdr->sh_link;
   11852 			  name = (bfd_elf_string_from_elf_section
   11853 				  (input_bfd, shlink, sym.st_name));
   11854 			  if (name == NULL)
   11855 			    return false;
   11856 
   11857 			  osec = sec->output_section;
   11858 			  sym.st_shndx =
   11859 			    _bfd_elf_section_from_bfd_section (output_bfd,
   11860 							       osec);
   11861 			  if (sym.st_shndx == SHN_BAD)
   11862 			    return false;
   11863 
   11864 			  sym.st_value += sec->output_offset;
   11865 			  if (!bfd_link_relocatable (flinfo->info))
   11866 			    {
   11867 			      sym.st_value += osec->vma;
   11868 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   11869 				{
   11870 				  struct elf_link_hash_table *htab
   11871 				    = elf_hash_table (flinfo->info);
   11872 
   11873 				  /* STT_TLS symbols are relative to PT_TLS
   11874 				     segment base.  */
   11875 				  if (htab->tls_sec != NULL)
   11876 				    sym.st_value -= htab->tls_sec->vma;
   11877 				  else
   11878 				    sym.st_info
   11879 				      = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
   11880 						     STT_NOTYPE);
   11881 				}
   11882 			    }
   11883 
   11884 			  indx = bfd_get_symcount (output_bfd);
   11885 			  ret = elf_link_output_symstrtab (flinfo, name,
   11886 							   &sym, sec,
   11887 							   NULL);
   11888 			  if (ret == 0)
   11889 			    return false;
   11890 			  else if (ret == 1)
   11891 			    flinfo->indices[r_symndx] = indx;
   11892 			  else
   11893 			    abort ();
   11894 			}
   11895 
   11896 		      r_symndx = flinfo->indices[r_symndx];
   11897 		    }
   11898 
   11899 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   11900 				   | (irela->r_info & r_type_mask));
   11901 		}
   11902 
   11903 	      /* Swap out the relocs.  */
   11904 	      input_rel_hdr = esdi->rel.hdr;
   11905 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   11906 		{
   11907 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   11908 						     input_rel_hdr,
   11909 						     internal_relocs,
   11910 						     rel_hash_list))
   11911 		    return false;
   11912 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   11913 				      * bed->s->int_rels_per_ext_rel);
   11914 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   11915 		}
   11916 
   11917 	      input_rela_hdr = esdi->rela.hdr;
   11918 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   11919 		{
   11920 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   11921 						     input_rela_hdr,
   11922 						     internal_relocs,
   11923 						     rela_hash_list))
   11924 		    return false;
   11925 		}
   11926 	    }
   11927 	}
   11928 
   11929       /* Write out the modified section contents.  */
   11930       if (bed->elf_backend_write_section
   11931 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   11932 						contents))
   11933 	{
   11934 	  /* Section written out.  */
   11935 	}
   11936       else switch (o->sec_info_type)
   11937 	{
   11938 	case SEC_INFO_TYPE_STABS:
   11939 	  if (! (_bfd_write_section_stabs
   11940 		 (output_bfd,
   11941 		  &elf_hash_table (flinfo->info)->stab_info,
   11942 		  o, &elf_section_data (o)->sec_info, contents)))
   11943 	    return false;
   11944 	  break;
   11945 	case SEC_INFO_TYPE_MERGE:
   11946 	  if (! _bfd_write_merged_section (output_bfd, o,
   11947 					   elf_section_data (o)->sec_info))
   11948 	    return false;
   11949 	  break;
   11950 	case SEC_INFO_TYPE_EH_FRAME:
   11951 	  {
   11952 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   11953 						   o, contents))
   11954 	      return false;
   11955 	  }
   11956 	  break;
   11957 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   11958 	  {
   11959 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   11960 							 flinfo->info,
   11961 							 o, contents))
   11962 	      return false;
   11963 	  }
   11964 	  break;
   11965 	case SEC_INFO_TYPE_SFRAME:
   11966 	    {
   11967 	      /* Merge .sframe sections into the ctf frame encoder
   11968 		 context of the output_bfd's section.  The final .sframe
   11969 		 output section will be written out later.  */
   11970 	      if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
   11971 						  o, contents))
   11972 		return false;
   11973 	    }
   11974 	    break;
   11975 	default:
   11976 	  {
   11977 	    if (! (o->flags & SEC_EXCLUDE))
   11978 	      {
   11979 		file_ptr offset = (file_ptr) o->output_offset;
   11980 		bfd_size_type todo = o->size;
   11981 
   11982 		offset *= bfd_octets_per_byte (output_bfd, o);
   11983 
   11984 		if ((o->flags & SEC_ELF_REVERSE_COPY)
   11985 		    && o->size > address_size)
   11986 		  {
   11987 		    /* Reverse-copy input section to output.  */
   11988 
   11989 		    if ((o->size & (address_size - 1)) != 0
   11990 			|| (o->reloc_count != 0
   11991 			    && (o->size * bed->s->int_rels_per_ext_rel
   11992 				!= o->reloc_count * address_size)))
   11993 		      {
   11994 			_bfd_error_handler
   11995 			  /* xgettext:c-format */
   11996 			  (_("error: %pB: size of section %pA is not "
   11997 			     "multiple of address size"),
   11998 			   input_bfd, o);
   11999 			bfd_set_error (bfd_error_bad_value);
   12000 			return false;
   12001 		      }
   12002 
   12003 		    do
   12004 		      {
   12005 			todo -= address_size;
   12006 			if (! bfd_set_section_contents (output_bfd,
   12007 							o->output_section,
   12008 							contents + todo,
   12009 							offset,
   12010 							address_size))
   12011 			  return false;
   12012 			if (todo == 0)
   12013 			  break;
   12014 			offset += address_size;
   12015 		      }
   12016 		    while (1);
   12017 		  }
   12018 		else if (! bfd_set_section_contents (output_bfd,
   12019 						     o->output_section,
   12020 						     contents,
   12021 						     offset, todo))
   12022 		  return false;
   12023 	      }
   12024 	  }
   12025 	  break;
   12026 	}
   12027     }
   12028 
   12029   return true;
   12030 }
   12031 
   12032 /* Generate a reloc when linking an ELF file.  This is a reloc
   12033    requested by the linker, and does not come from any input file.  This
   12034    is used to build constructor and destructor tables when linking
   12035    with -Ur.  */
   12036 
   12037 static bool
   12038 elf_reloc_link_order (bfd *output_bfd,
   12039 		      struct bfd_link_info *info,
   12040 		      asection *output_section,
   12041 		      struct bfd_link_order *link_order)
   12042 {
   12043   reloc_howto_type *howto;
   12044   long indx;
   12045   bfd_vma offset;
   12046   bfd_vma addend;
   12047   struct bfd_elf_section_reloc_data *reldata;
   12048   struct elf_link_hash_entry **rel_hash_ptr;
   12049   Elf_Internal_Shdr *rel_hdr;
   12050   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   12051   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   12052   bfd_byte *erel;
   12053   unsigned int i;
   12054   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   12055 
   12056   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   12057   if (howto == NULL)
   12058     {
   12059       bfd_set_error (bfd_error_bad_value);
   12060       return false;
   12061     }
   12062 
   12063   addend = link_order->u.reloc.p->addend;
   12064 
   12065   if (esdo->rel.hdr)
   12066     reldata = &esdo->rel;
   12067   else if (esdo->rela.hdr)
   12068     reldata = &esdo->rela;
   12069   else
   12070     {
   12071       reldata = NULL;
   12072       BFD_ASSERT (0);
   12073     }
   12074 
   12075   /* Figure out the symbol index.  */
   12076   rel_hash_ptr = reldata->hashes + reldata->count;
   12077   if (link_order->type == bfd_section_reloc_link_order)
   12078     {
   12079       indx = link_order->u.reloc.p->u.section->target_index;
   12080       BFD_ASSERT (indx != 0);
   12081       *rel_hash_ptr = NULL;
   12082     }
   12083   else
   12084     {
   12085       struct elf_link_hash_entry *h;
   12086 
   12087       /* Treat a reloc against a defined symbol as though it were
   12088 	 actually against the section.  */
   12089       h = ((struct elf_link_hash_entry *)
   12090 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   12091 					 link_order->u.reloc.p->u.name,
   12092 					 false, false, true));
   12093       if (h != NULL
   12094 	  && (h->root.type == bfd_link_hash_defined
   12095 	      || h->root.type == bfd_link_hash_defweak))
   12096 	{
   12097 	  asection *section;
   12098 
   12099 	  section = h->root.u.def.section;
   12100 	  indx = section->output_section->target_index;
   12101 	  *rel_hash_ptr = NULL;
   12102 	  /* It seems that we ought to add the symbol value to the
   12103 	     addend here, but in practice it has already been added
   12104 	     because it was passed to constructor_callback.  */
   12105 	  addend += section->output_section->vma + section->output_offset;
   12106 	}
   12107       else if (h != NULL)
   12108 	{
   12109 	  /* Setting the index to -2 tells elf_link_output_extsym that
   12110 	     this symbol is used by a reloc.  */
   12111 	  h->indx = -2;
   12112 	  *rel_hash_ptr = h;
   12113 	  indx = 0;
   12114 	}
   12115       else
   12116 	{
   12117 	  (*info->callbacks->unattached_reloc)
   12118 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   12119 	  indx = 0;
   12120 	}
   12121     }
   12122 
   12123   /* If this is an inplace reloc, we must write the addend into the
   12124      object file.  */
   12125   if (howto->partial_inplace && addend != 0)
   12126     {
   12127       bfd_size_type size;
   12128       bfd_reloc_status_type rstat;
   12129       bfd_byte *buf;
   12130       bool ok;
   12131       const char *sym_name;
   12132       bfd_size_type octets;
   12133 
   12134       size = (bfd_size_type) bfd_get_reloc_size (howto);
   12135       buf = (bfd_byte *) bfd_zmalloc (size);
   12136       if (buf == NULL && size != 0)
   12137 	return false;
   12138       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   12139       switch (rstat)
   12140 	{
   12141 	case bfd_reloc_ok:
   12142 	  break;
   12143 
   12144 	default:
   12145 	case bfd_reloc_outofrange:
   12146 	  abort ();
   12147 
   12148 	case bfd_reloc_overflow:
   12149 	  if (link_order->type == bfd_section_reloc_link_order)
   12150 	    sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
   12151 	  else
   12152 	    sym_name = link_order->u.reloc.p->u.name;
   12153 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
   12154 					      howto->name, addend, NULL, NULL,
   12155 					      (bfd_vma) 0);
   12156 	  break;
   12157 	}
   12158 
   12159       octets = link_order->offset * bfd_octets_per_byte (output_bfd,
   12160 							 output_section);
   12161       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   12162 				     octets, size);
   12163       free (buf);
   12164       if (! ok)
   12165 	return false;
   12166     }
   12167 
   12168   /* The address of a reloc is relative to the section in a
   12169      relocatable file, and is a virtual address in an executable
   12170      file.  */
   12171   offset = link_order->offset;
   12172   if (! bfd_link_relocatable (info))
   12173     offset += output_section->vma;
   12174 
   12175   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   12176     {
   12177       irel[i].r_offset = offset;
   12178       irel[i].r_info = 0;
   12179       irel[i].r_addend = 0;
   12180     }
   12181   if (bed->s->arch_size == 32)
   12182     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   12183   else
   12184     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
   12185 
   12186   rel_hdr = reldata->hdr;
   12187   erel = rel_hdr->contents;
   12188   if (rel_hdr->sh_type == SHT_REL)
   12189     {
   12190       erel += reldata->count * bed->s->sizeof_rel;
   12191       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   12192     }
   12193   else
   12194     {
   12195       irel[0].r_addend = addend;
   12196       erel += reldata->count * bed->s->sizeof_rela;
   12197       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   12198     }
   12199 
   12200   ++reldata->count;
   12201 
   12202   return true;
   12203 }
   12204 
   12205 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
   12206    Returns TRUE upon success, FALSE otherwise.  */
   12207 
   12208 static bool
   12209 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
   12210 {
   12211   bool ret = false;
   12212   bfd *implib_bfd;
   12213   const struct elf_backend_data *bed;
   12214   flagword flags;
   12215   enum bfd_architecture arch;
   12216   unsigned int mach;
   12217   asymbol **sympp = NULL;
   12218   long symsize;
   12219   long symcount;
   12220   long src_count;
   12221   elf_symbol_type *osymbuf;
   12222   size_t amt;
   12223 
   12224   implib_bfd = info->out_implib_bfd;
   12225   bed = get_elf_backend_data (abfd);
   12226 
   12227   if (!bfd_set_format (implib_bfd, bfd_object))
   12228     return false;
   12229 
   12230   /* Use flag from executable but make it a relocatable object.  */
   12231   flags = bfd_get_file_flags (abfd);
   12232   flags &= ~HAS_RELOC;
   12233   if (!bfd_set_start_address (implib_bfd, 0)
   12234       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
   12235     return false;
   12236 
   12237   /* Copy architecture of output file to import library file.  */
   12238   arch = bfd_get_arch (abfd);
   12239   mach = bfd_get_mach (abfd);
   12240   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
   12241       && (abfd->target_defaulted
   12242 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
   12243     return false;
   12244 
   12245   /* Get symbol table size.  */
   12246   symsize = bfd_get_symtab_upper_bound (abfd);
   12247   if (symsize < 0)
   12248     return false;
   12249 
   12250   /* Read in the symbol table.  */
   12251   sympp = (asymbol **) bfd_malloc (symsize);
   12252   if (sympp == NULL)
   12253     return false;
   12254 
   12255   symcount = bfd_canonicalize_symtab (abfd, sympp);
   12256   if (symcount < 0)
   12257     goto free_sym_buf;
   12258 
   12259   /* Allow the BFD backend to copy any private header data it
   12260      understands from the output BFD to the import library BFD.  */
   12261   if (! bfd_copy_private_header_data (abfd, implib_bfd))
   12262     goto free_sym_buf;
   12263 
   12264   /* Filter symbols to appear in the import library.  */
   12265   if (bed->elf_backend_filter_implib_symbols)
   12266     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
   12267 						       symcount);
   12268   else
   12269     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
   12270   if (symcount == 0)
   12271     {
   12272       bfd_set_error (bfd_error_no_symbols);
   12273       _bfd_error_handler (_("%pB: no symbol found for import library"),
   12274 			  implib_bfd);
   12275       goto free_sym_buf;
   12276     }
   12277 
   12278 
   12279   /* Make symbols absolute.  */
   12280   amt = symcount * sizeof (*osymbuf);
   12281   osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
   12282   if (osymbuf == NULL)
   12283     goto free_sym_buf;
   12284 
   12285   for (src_count = 0; src_count < symcount; src_count++)
   12286     {
   12287       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
   12288 	      sizeof (*osymbuf));
   12289       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
   12290       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
   12291       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
   12292       osymbuf[src_count].internal_elf_sym.st_value =
   12293 	osymbuf[src_count].symbol.value;
   12294       sympp[src_count] = &osymbuf[src_count].symbol;
   12295     }
   12296 
   12297   bfd_set_symtab (implib_bfd, sympp, symcount);
   12298 
   12299   /* Allow the BFD backend to copy any private data it understands
   12300      from the output BFD to the import library BFD.  This is done last
   12301      to permit the routine to look at the filtered symbol table.  */
   12302   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
   12303     goto free_sym_buf;
   12304 
   12305   if (!bfd_close (implib_bfd))
   12306     goto free_sym_buf;
   12307 
   12308   ret = true;
   12309 
   12310  free_sym_buf:
   12311   free (sympp);
   12312   return ret;
   12313 }
   12314 
   12315 static void
   12316 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   12317 {
   12318   asection *o;
   12319 
   12320   if (flinfo->symstrtab != NULL)
   12321     _bfd_elf_strtab_free (flinfo->symstrtab);
   12322   free (flinfo->contents);
   12323   free (flinfo->external_relocs);
   12324   free (flinfo->internal_relocs);
   12325   free (flinfo->external_syms);
   12326   free (flinfo->locsym_shndx);
   12327   free (flinfo->internal_syms);
   12328   free (flinfo->indices);
   12329   free (flinfo->sections);
   12330   if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
   12331     free (flinfo->symshndxbuf);
   12332   for (o = obfd->sections; o != NULL; o = o->next)
   12333     {
   12334       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12335       free (esdo->rel.hashes);
   12336       free (esdo->rela.hashes);
   12337     }
   12338 }
   12339 
   12340 /* Do the final step of an ELF link.  */
   12341 
   12342 bool
   12343 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   12344 {
   12345   bool dynamic;
   12346   bool emit_relocs;
   12347   bfd *dynobj;
   12348   struct elf_final_link_info flinfo;
   12349   asection *o;
   12350   struct bfd_link_order *p;
   12351   bfd *sub;
   12352   bfd_size_type max_contents_size;
   12353   bfd_size_type max_external_reloc_size;
   12354   bfd_size_type max_internal_reloc_count;
   12355   bfd_size_type max_sym_count;
   12356   bfd_size_type max_sym_shndx_count;
   12357   Elf_Internal_Sym elfsym;
   12358   unsigned int i;
   12359   Elf_Internal_Shdr *symtab_hdr;
   12360   Elf_Internal_Shdr *symtab_shndx_hdr;
   12361   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12362   struct elf_outext_info eoinfo;
   12363   bool merged;
   12364   size_t relativecount;
   12365   size_t relr_entsize;
   12366   asection *reldyn = 0;
   12367   bfd_size_type amt;
   12368   asection *attr_section = NULL;
   12369   bfd_vma attr_size = 0;
   12370   const char *std_attrs_section;
   12371   struct elf_link_hash_table *htab = elf_hash_table (info);
   12372   bool sections_removed;
   12373   bool ret;
   12374 
   12375   if (!is_elf_hash_table (&htab->root))
   12376     return false;
   12377 
   12378   if (bfd_link_pic (info))
   12379     abfd->flags |= DYNAMIC;
   12380 
   12381   dynamic = htab->dynamic_sections_created;
   12382   dynobj = htab->dynobj;
   12383 
   12384   emit_relocs = (bfd_link_relocatable (info)
   12385 		 || info->emitrelocations);
   12386 
   12387   memset (&flinfo, 0, sizeof (flinfo));
   12388   flinfo.info = info;
   12389   flinfo.output_bfd = abfd;
   12390   flinfo.symstrtab = _bfd_elf_strtab_init ();
   12391   if (flinfo.symstrtab == NULL)
   12392     return false;
   12393 
   12394   if (! dynamic)
   12395     {
   12396       flinfo.hash_sec = NULL;
   12397       flinfo.symver_sec = NULL;
   12398     }
   12399   else
   12400     {
   12401       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   12402       /* Note that dynsym_sec can be NULL (on VMS).  */
   12403       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   12404       /* Note that it is OK if symver_sec is NULL.  */
   12405     }
   12406 
   12407   if (info->unique_symbol
   12408       && !bfd_hash_table_init (&flinfo.local_hash_table,
   12409 			       local_hash_newfunc,
   12410 			       sizeof (struct local_hash_entry)))
   12411     return false;
   12412 
   12413   /* The object attributes have been merged.  Remove the input
   12414      sections from the link, and set the contents of the output
   12415      section.  */
   12416   sections_removed = false;
   12417   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   12418   for (o = abfd->sections; o != NULL; o = o->next)
   12419     {
   12420       bool remove_section = false;
   12421 
   12422       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
   12423 	  || strcmp (o->name, ".gnu.attributes") == 0)
   12424 	{
   12425 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   12426 	    {
   12427 	      asection *input_section;
   12428 
   12429 	      if (p->type != bfd_indirect_link_order)
   12430 		continue;
   12431 	      input_section = p->u.indirect.section;
   12432 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   12433 		 elf_link_input_bfd ignores this section.  */
   12434 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   12435 	    }
   12436 
   12437 	  attr_size = bfd_elf_obj_attr_size (abfd);
   12438 	  bfd_set_section_size (o, attr_size);
   12439 	  /* Skip this section later on.  */
   12440 	  o->map_head.link_order = NULL;
   12441 	  if (attr_size)
   12442 	    attr_section = o;
   12443 	  else
   12444 	    remove_section = true;
   12445 	}
   12446       else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
   12447 	{
   12448 	  /* Remove empty group section from linker output.  */
   12449 	  remove_section = true;
   12450 	}
   12451       if (remove_section)
   12452 	{
   12453 	  o->flags |= SEC_EXCLUDE;
   12454 	  bfd_section_list_remove (abfd, o);
   12455 	  abfd->section_count--;
   12456 	  sections_removed = true;
   12457 	}
   12458     }
   12459   if (sections_removed)
   12460     _bfd_fix_excluded_sec_syms (abfd, info);
   12461 
   12462   /* Count up the number of relocations we will output for each output
   12463      section, so that we know the sizes of the reloc sections.  We
   12464      also figure out some maximum sizes.  */
   12465   max_contents_size = 0;
   12466   max_external_reloc_size = 0;
   12467   max_internal_reloc_count = 0;
   12468   max_sym_count = 0;
   12469   max_sym_shndx_count = 0;
   12470   merged = false;
   12471   for (o = abfd->sections; o != NULL; o = o->next)
   12472     {
   12473       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12474       o->reloc_count = 0;
   12475 
   12476       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12477 	{
   12478 	  unsigned int reloc_count = 0;
   12479 	  unsigned int additional_reloc_count = 0;
   12480 	  struct bfd_elf_section_data *esdi = NULL;
   12481 
   12482 	  if (p->type == bfd_section_reloc_link_order
   12483 	      || p->type == bfd_symbol_reloc_link_order)
   12484 	    reloc_count = 1;
   12485 	  else if (p->type == bfd_indirect_link_order)
   12486 	    {
   12487 	      asection *sec;
   12488 
   12489 	      sec = p->u.indirect.section;
   12490 
   12491 	      /* Mark all sections which are to be included in the
   12492 		 link.  This will normally be every section.  We need
   12493 		 to do this so that we can identify any sections which
   12494 		 the linker has decided to not include.  */
   12495 	      sec->linker_mark = true;
   12496 
   12497 	      if (sec->flags & SEC_MERGE)
   12498 		merged = true;
   12499 
   12500 	      if (sec->rawsize > max_contents_size)
   12501 		max_contents_size = sec->rawsize;
   12502 	      if (sec->size > max_contents_size)
   12503 		max_contents_size = sec->size;
   12504 
   12505 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   12506 		  && (sec->owner->flags & DYNAMIC) == 0)
   12507 		{
   12508 		  size_t sym_count;
   12509 
   12510 		  /* We are interested in just local symbols, not all
   12511 		     symbols.  */
   12512 		  if (elf_bad_symtab (sec->owner))
   12513 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   12514 				 / bed->s->sizeof_sym);
   12515 		  else
   12516 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   12517 
   12518 		  if (sym_count > max_sym_count)
   12519 		    max_sym_count = sym_count;
   12520 
   12521 		  if (sym_count > max_sym_shndx_count
   12522 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   12523 		    max_sym_shndx_count = sym_count;
   12524 
   12525 		  esdi = elf_section_data (sec);
   12526 
   12527 		  if (esdi->this_hdr.sh_type == SHT_REL
   12528 		      || esdi->this_hdr.sh_type == SHT_RELA)
   12529 		    /* Some backends use reloc_count in relocation sections
   12530 		       to count particular types of relocs.  Of course,
   12531 		       reloc sections themselves can't have relocations.  */
   12532 		    ;
   12533 		  else if (emit_relocs)
   12534 		    {
   12535 		      reloc_count = sec->reloc_count;
   12536 		      if (bed->elf_backend_count_additional_relocs)
   12537 			{
   12538 			  int c;
   12539 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
   12540 			  additional_reloc_count += c;
   12541 			}
   12542 		    }
   12543 		  else if (bed->elf_backend_count_relocs)
   12544 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   12545 
   12546 		  if ((sec->flags & SEC_RELOC) != 0)
   12547 		    {
   12548 		      size_t ext_size = 0;
   12549 
   12550 		      if (esdi->rel.hdr != NULL)
   12551 			ext_size = esdi->rel.hdr->sh_size;
   12552 		      if (esdi->rela.hdr != NULL)
   12553 			ext_size += esdi->rela.hdr->sh_size;
   12554 
   12555 		      if (ext_size > max_external_reloc_size)
   12556 			max_external_reloc_size = ext_size;
   12557 		      if (sec->reloc_count > max_internal_reloc_count)
   12558 			max_internal_reloc_count = sec->reloc_count;
   12559 		    }
   12560 		}
   12561 	    }
   12562 
   12563 	  if (reloc_count == 0)
   12564 	    continue;
   12565 
   12566 	  reloc_count += additional_reloc_count;
   12567 	  o->reloc_count += reloc_count;
   12568 
   12569 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   12570 	    {
   12571 	      if (esdi->rel.hdr)
   12572 		{
   12573 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   12574 		  esdo->rel.count += additional_reloc_count;
   12575 		}
   12576 	      if (esdi->rela.hdr)
   12577 		{
   12578 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   12579 		  esdo->rela.count += additional_reloc_count;
   12580 		}
   12581 	    }
   12582 	  else
   12583 	    {
   12584 	      if (o->use_rela_p)
   12585 		esdo->rela.count += reloc_count;
   12586 	      else
   12587 		esdo->rel.count += reloc_count;
   12588 	    }
   12589 	}
   12590 
   12591       if (o->reloc_count > 0)
   12592 	o->flags |= SEC_RELOC;
   12593       else
   12594 	{
   12595 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   12596 	     set it (this is probably a bug) and if it is set
   12597 	     assign_section_numbers will create a reloc section.  */
   12598 	  o->flags &=~ SEC_RELOC;
   12599 	}
   12600 
   12601       /* If the SEC_ALLOC flag is not set, force the section VMA to
   12602 	 zero.  This is done in elf_fake_sections as well, but forcing
   12603 	 the VMA to 0 here will ensure that relocs against these
   12604 	 sections are handled correctly.  */
   12605       if ((o->flags & SEC_ALLOC) == 0
   12606 	  && ! o->user_set_vma)
   12607 	o->vma = 0;
   12608     }
   12609 
   12610   if (! bfd_link_relocatable (info) && merged)
   12611     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
   12612 
   12613   /* Figure out the file positions for everything but the symbol table
   12614      and the relocs.  We set symcount to force assign_section_numbers
   12615      to create a symbol table.  */
   12616   abfd->symcount = info->strip != strip_all || emit_relocs;
   12617   BFD_ASSERT (! abfd->output_has_begun);
   12618   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   12619     goto error_return;
   12620 
   12621   /* Set sizes, and assign file positions for reloc sections.  */
   12622   for (o = abfd->sections; o != NULL; o = o->next)
   12623     {
   12624       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12625       if ((o->flags & SEC_RELOC) != 0)
   12626 	{
   12627 	  if (esdo->rel.hdr
   12628 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   12629 	    goto error_return;
   12630 
   12631 	  if (esdo->rela.hdr
   12632 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   12633 	    goto error_return;
   12634 	}
   12635 
   12636       /* _bfd_elf_compute_section_file_positions makes temporary use
   12637 	 of target_index.  Reset it.  */
   12638       o->target_index = 0;
   12639 
   12640       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   12641 	 to count upwards while actually outputting the relocations.  */
   12642       esdo->rel.count = 0;
   12643       esdo->rela.count = 0;
   12644 
   12645       if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
   12646 	  && !bfd_section_is_ctf (o))
   12647 	{
   12648 	  /* Cache the section contents so that they can be compressed
   12649 	     later.  Use bfd_malloc since it will be freed by
   12650 	     bfd_compress_section_contents.  */
   12651 	  unsigned char *contents = esdo->this_hdr.contents;
   12652 	  if (contents != NULL)
   12653 	    abort ();
   12654 	  contents
   12655 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   12656 	  if (contents == NULL)
   12657 	    goto error_return;
   12658 	  esdo->this_hdr.contents = contents;
   12659 	}
   12660     }
   12661 
   12662   /* We have now assigned file positions for all the sections except .symtab,
   12663      .strtab, and non-loaded reloc and compressed debugging sections.  We start
   12664      the .symtab section at the current file position, and write directly to it.
   12665      We build the .strtab section in memory.  */
   12666   abfd->symcount = 0;
   12667   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12668   /* sh_name is set in prep_headers.  */
   12669   symtab_hdr->sh_type = SHT_SYMTAB;
   12670   /* sh_flags, sh_addr and sh_size all start off zero.  */
   12671   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   12672   /* sh_link is set in assign_section_numbers.  */
   12673   /* sh_info is set below.  */
   12674   /* sh_offset is set just below.  */
   12675   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   12676 
   12677   if (max_sym_count < 20)
   12678     max_sym_count = 20;
   12679   htab->strtabsize = max_sym_count;
   12680   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   12681   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
   12682   if (htab->strtab == NULL)
   12683     goto error_return;
   12684   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   12685   flinfo.symshndxbuf
   12686     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   12687        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   12688 
   12689   if (info->strip != strip_all || emit_relocs)
   12690     {
   12691       file_ptr off = elf_next_file_pos (abfd);
   12692 
   12693       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
   12694 
   12695       /* Note that at this point elf_next_file_pos (abfd) is
   12696 	 incorrect.  We do not yet know the size of the .symtab section.
   12697 	 We correct next_file_pos below, after we do know the size.  */
   12698 
   12699       /* Start writing out the symbol table.  The first symbol is always a
   12700 	 dummy symbol.  */
   12701       elfsym.st_value = 0;
   12702       elfsym.st_size = 0;
   12703       elfsym.st_info = 0;
   12704       elfsym.st_other = 0;
   12705       elfsym.st_shndx = SHN_UNDEF;
   12706       elfsym.st_target_internal = 0;
   12707       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   12708 				     bfd_und_section_ptr, NULL) != 1)
   12709 	goto error_return;
   12710 
   12711       /* Output a symbol for each section if asked or they are used for
   12712 	 relocs.  These symbols usually have no names.  We store the
   12713 	 index of each one in the index field of the section, so that
   12714 	 we can find it again when outputting relocs.  */
   12715 
   12716       if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
   12717 	{
   12718 	  bool name_local_sections
   12719 	    = (bed->elf_backend_name_local_section_symbols
   12720 	       && bed->elf_backend_name_local_section_symbols (abfd));
   12721 	  const char *name = NULL;
   12722 
   12723 	  elfsym.st_size = 0;
   12724 	  elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   12725 	  elfsym.st_other = 0;
   12726 	  elfsym.st_value = 0;
   12727 	  elfsym.st_target_internal = 0;
   12728 	  for (i = 1; i < elf_numsections (abfd); i++)
   12729 	    {
   12730 	      o = bfd_section_from_elf_index (abfd, i);
   12731 	      if (o != NULL)
   12732 		{
   12733 		  o->target_index = bfd_get_symcount (abfd);
   12734 		  elfsym.st_shndx = i;
   12735 		  if (!bfd_link_relocatable (info))
   12736 		    elfsym.st_value = o->vma;
   12737 		  if (name_local_sections)
   12738 		    name = o->name;
   12739 		  if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
   12740 						 NULL) != 1)
   12741 		    goto error_return;
   12742 		}
   12743 	    }
   12744 	}
   12745     }
   12746 
   12747   /* On some targets like Irix 5 the symbol split between local and global
   12748      ones recorded in the sh_info field needs to be done between section
   12749      and all other symbols.  */
   12750   if (bed->elf_backend_elfsym_local_is_section
   12751       && bed->elf_backend_elfsym_local_is_section (abfd))
   12752     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   12753 
   12754   /* Allocate some memory to hold information read in from the input
   12755      files.  */
   12756   if (max_contents_size != 0)
   12757     {
   12758       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   12759       if (flinfo.contents == NULL)
   12760 	goto error_return;
   12761     }
   12762 
   12763   if (max_external_reloc_size != 0)
   12764     {
   12765       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   12766       if (flinfo.external_relocs == NULL)
   12767 	goto error_return;
   12768     }
   12769 
   12770   if (max_internal_reloc_count != 0)
   12771     {
   12772       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
   12773       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   12774       if (flinfo.internal_relocs == NULL)
   12775 	goto error_return;
   12776     }
   12777 
   12778   if (max_sym_count != 0)
   12779     {
   12780       amt = max_sym_count * bed->s->sizeof_sym;
   12781       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   12782       if (flinfo.external_syms == NULL)
   12783 	goto error_return;
   12784 
   12785       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   12786       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   12787       if (flinfo.internal_syms == NULL)
   12788 	goto error_return;
   12789 
   12790       amt = max_sym_count * sizeof (long);
   12791       flinfo.indices = (long int *) bfd_malloc (amt);
   12792       if (flinfo.indices == NULL)
   12793 	goto error_return;
   12794 
   12795       amt = max_sym_count * sizeof (asection *);
   12796       flinfo.sections = (asection **) bfd_malloc (amt);
   12797       if (flinfo.sections == NULL)
   12798 	goto error_return;
   12799     }
   12800 
   12801   if (max_sym_shndx_count != 0)
   12802     {
   12803       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   12804       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   12805       if (flinfo.locsym_shndx == NULL)
   12806 	goto error_return;
   12807     }
   12808 
   12809   if (htab->tls_sec)
   12810     {
   12811       bfd_vma base, end = 0;  /* Both bytes.  */
   12812       asection *sec;
   12813 
   12814       for (sec = htab->tls_sec;
   12815 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   12816 	   sec = sec->next)
   12817 	{
   12818 	  bfd_size_type size = sec->size;
   12819 	  unsigned int opb = bfd_octets_per_byte (abfd, sec);
   12820 
   12821 	  if (size == 0
   12822 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   12823 	    {
   12824 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   12825 
   12826 	      if (ord != NULL)
   12827 		size = ord->offset * opb + ord->size;
   12828 	    }
   12829 	  end = sec->vma + size / opb;
   12830 	}
   12831       base = htab->tls_sec->vma;
   12832       /* Only align end of TLS section if static TLS doesn't have special
   12833 	 alignment requirements.  */
   12834       if (bed->static_tls_alignment == 1)
   12835 	end = align_power (end, htab->tls_sec->alignment_power);
   12836       htab->tls_size = end - base;
   12837     }
   12838 
   12839   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   12840     return false;
   12841 
   12842   /* Finish relative relocations here after regular symbol processing
   12843      is finished if DT_RELR is enabled.  */
   12844   if (info->enable_dt_relr
   12845       && bed->finish_relative_relocs
   12846       && !bed->finish_relative_relocs (info))
   12847     info->callbacks->einfo
   12848       (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
   12849 
   12850   /* Since ELF permits relocations to be against local symbols, we
   12851      must have the local symbols available when we do the relocations.
   12852      Since we would rather only read the local symbols once, and we
   12853      would rather not keep them in memory, we handle all the
   12854      relocations for a single input file at the same time.
   12855 
   12856      Unfortunately, there is no way to know the total number of local
   12857      symbols until we have seen all of them, and the local symbol
   12858      indices precede the global symbol indices.  This means that when
   12859      we are generating relocatable output, and we see a reloc against
   12860      a global symbol, we can not know the symbol index until we have
   12861      finished examining all the local symbols to see which ones we are
   12862      going to output.  To deal with this, we keep the relocations in
   12863      memory, and don't output them until the end of the link.  This is
   12864      an unfortunate waste of memory, but I don't see a good way around
   12865      it.  Fortunately, it only happens when performing a relocatable
   12866      link, which is not the common case.  FIXME: If keep_memory is set
   12867      we could write the relocs out and then read them again; I don't
   12868      know how bad the memory loss will be.  */
   12869 
   12870   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12871     sub->output_has_begun = false;
   12872   for (o = abfd->sections; o != NULL; o = o->next)
   12873     {
   12874       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12875 	{
   12876 	  if (p->type == bfd_indirect_link_order
   12877 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   12878 		  == bfd_target_elf_flavour)
   12879 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   12880 	    {
   12881 	      if (! sub->output_has_begun)
   12882 		{
   12883 		  if (! elf_link_input_bfd (&flinfo, sub))
   12884 		    goto error_return;
   12885 		  sub->output_has_begun = true;
   12886 		}
   12887 	    }
   12888 	  else if (p->type == bfd_section_reloc_link_order
   12889 		   || p->type == bfd_symbol_reloc_link_order)
   12890 	    {
   12891 	      if (! elf_reloc_link_order (abfd, info, o, p))
   12892 		goto error_return;
   12893 	    }
   12894 	  else
   12895 	    {
   12896 	      if (! _bfd_default_link_order (abfd, info, o, p))
   12897 		{
   12898 		  if (p->type == bfd_indirect_link_order
   12899 		      && (bfd_get_flavour (sub)
   12900 			  == bfd_target_elf_flavour)
   12901 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   12902 			  != bed->s->elfclass))
   12903 		    {
   12904 		      const char *iclass, *oclass;
   12905 
   12906 		      switch (bed->s->elfclass)
   12907 			{
   12908 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
   12909 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
   12910 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
   12911 			default: abort ();
   12912 			}
   12913 
   12914 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
   12915 			{
   12916 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
   12917 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
   12918 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
   12919 			default: abort ();
   12920 			}
   12921 
   12922 		      bfd_set_error (bfd_error_wrong_format);
   12923 		      _bfd_error_handler
   12924 			/* xgettext:c-format */
   12925 			(_("%pB: file class %s incompatible with %s"),
   12926 			 sub, iclass, oclass);
   12927 		    }
   12928 
   12929 		  goto error_return;
   12930 		}
   12931 	    }
   12932 	}
   12933     }
   12934 
   12935   /* Free symbol buffer if needed.  */
   12936   if (!info->reduce_memory_overheads)
   12937     {
   12938       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12939 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
   12940 	  {
   12941 	    free (elf_tdata (sub)->symbuf);
   12942 	    elf_tdata (sub)->symbuf = NULL;
   12943 	  }
   12944     }
   12945 
   12946   ret = true;
   12947 
   12948   /* Output any global symbols that got converted to local in a
   12949      version script or due to symbol visibility.  We do this in a
   12950      separate step since ELF requires all local symbols to appear
   12951      prior to any global symbols.  FIXME: We should only do this if
   12952      some global symbols were, in fact, converted to become local.
   12953      FIXME: Will this work correctly with the Irix 5 linker?  */
   12954   eoinfo.failed = false;
   12955   eoinfo.flinfo = &flinfo;
   12956   eoinfo.localsyms = true;
   12957   eoinfo.file_sym_done = false;
   12958   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   12959   if (eoinfo.failed)
   12960     {
   12961       ret = false;
   12962       goto return_local_hash_table;
   12963     }
   12964 
   12965   /* If backend needs to output some local symbols not present in the hash
   12966      table, do it now.  */
   12967   if (bed->elf_backend_output_arch_local_syms)
   12968     {
   12969       if (! ((*bed->elf_backend_output_arch_local_syms)
   12970 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   12971 	{
   12972 	  ret = false;
   12973 	  goto return_local_hash_table;
   12974 	}
   12975     }
   12976 
   12977   /* That wrote out all the local symbols.  Finish up the symbol table
   12978      with the global symbols. Even if we want to strip everything we
   12979      can, we still need to deal with those global symbols that got
   12980      converted to local in a version script.  */
   12981 
   12982   /* The sh_info field records the index of the first non local symbol.  */
   12983   if (!symtab_hdr->sh_info)
   12984     symtab_hdr->sh_info = bfd_get_symcount (abfd);
   12985 
   12986   if (dynamic
   12987       && htab->dynsym != NULL
   12988       && htab->dynsym->output_section != bfd_abs_section_ptr)
   12989     {
   12990       Elf_Internal_Sym sym;
   12991       bfd_byte *dynsym = htab->dynsym->contents;
   12992 
   12993       o = htab->dynsym->output_section;
   12994       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
   12995 
   12996       /* Write out the section symbols for the output sections.  */
   12997       if (bfd_link_pic (info)
   12998 	  || htab->is_relocatable_executable)
   12999 	{
   13000 	  asection *s;
   13001 
   13002 	  sym.st_size = 0;
   13003 	  sym.st_name = 0;
   13004 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   13005 	  sym.st_other = 0;
   13006 	  sym.st_target_internal = 0;
   13007 
   13008 	  for (s = abfd->sections; s != NULL; s = s->next)
   13009 	    {
   13010 	      int indx;
   13011 	      bfd_byte *dest;
   13012 	      long dynindx;
   13013 
   13014 	      dynindx = elf_section_data (s)->dynindx;
   13015 	      if (dynindx <= 0)
   13016 		continue;
   13017 	      indx = elf_section_data (s)->this_idx;
   13018 	      BFD_ASSERT (indx > 0);
   13019 	      sym.st_shndx = indx;
   13020 	      if (! check_dynsym (abfd, &sym))
   13021 		{
   13022 		  ret = false;
   13023 		  goto return_local_hash_table;
   13024 		}
   13025 	      sym.st_value = s->vma;
   13026 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   13027 
   13028 	      /* Inform the linker of the addition of this symbol.  */
   13029 
   13030 	      if (info->callbacks->ctf_new_dynsym)
   13031 		info->callbacks->ctf_new_dynsym (dynindx, &sym);
   13032 
   13033 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13034 	    }
   13035 	}
   13036 
   13037       /* Write out the local dynsyms.  */
   13038       if (htab->dynlocal)
   13039 	{
   13040 	  struct elf_link_local_dynamic_entry *e;
   13041 	  for (e = htab->dynlocal; e ; e = e->next)
   13042 	    {
   13043 	      asection *s;
   13044 	      bfd_byte *dest;
   13045 
   13046 	      /* Copy the internal symbol and turn off visibility.
   13047 		 Note that we saved a word of storage and overwrote
   13048 		 the original st_name with the dynstr_index.  */
   13049 	      sym = e->isym;
   13050 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   13051 	      sym.st_shndx = SHN_UNDEF;
   13052 
   13053 	      s = bfd_section_from_elf_index (e->input_bfd,
   13054 					      e->isym.st_shndx);
   13055 	      if (s != NULL
   13056 		  && s->output_section != NULL
   13057 		  && elf_section_data (s->output_section) != NULL)
   13058 		{
   13059 		  sym.st_shndx =
   13060 		    elf_section_data (s->output_section)->this_idx;
   13061 		  if (! check_dynsym (abfd, &sym))
   13062 		    {
   13063 		      ret = false;
   13064 		      goto return_local_hash_table;
   13065 		    }
   13066 		  sym.st_value = (s->output_section->vma
   13067 				  + s->output_offset
   13068 				  + e->isym.st_value);
   13069 		}
   13070 
   13071 	      /* Inform the linker of the addition of this symbol.  */
   13072 
   13073 	      if (info->callbacks->ctf_new_dynsym)
   13074 		info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
   13075 
   13076 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   13077 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   13078 	    }
   13079 	}
   13080     }
   13081 
   13082   /* We get the global symbols from the hash table.  */
   13083   eoinfo.failed = false;
   13084   eoinfo.localsyms = false;
   13085   eoinfo.flinfo = &flinfo;
   13086   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   13087   if (eoinfo.failed)
   13088     {
   13089       ret = false;
   13090       goto return_local_hash_table;
   13091     }
   13092 
   13093   /* If backend needs to output some symbols not present in the hash
   13094      table, do it now.  */
   13095   if (bed->elf_backend_output_arch_syms
   13096       && (info->strip != strip_all || emit_relocs))
   13097     {
   13098       if (! ((*bed->elf_backend_output_arch_syms)
   13099 	     (abfd, info, &flinfo, elf_link_output_symstrtab)))
   13100 	{
   13101 	  ret = false;
   13102 	  goto return_local_hash_table;
   13103 	}
   13104     }
   13105 
   13106   /* Finalize the .strtab section.  */
   13107   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   13108 
   13109   /* Swap out the .strtab section. */
   13110   if (!elf_link_swap_symbols_out (&flinfo))
   13111     {
   13112       ret = false;
   13113       goto return_local_hash_table;
   13114     }
   13115 
   13116   /* Now we know the size of the symtab section.  */
   13117   if (bfd_get_symcount (abfd) > 0)
   13118     {
   13119       /* Finish up and write out the symbol string table (.strtab)
   13120 	 section.  */
   13121       Elf_Internal_Shdr *symstrtab_hdr = NULL;
   13122       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   13123 
   13124       if (elf_symtab_shndx_list (abfd))
   13125 	{
   13126 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   13127 
   13128 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   13129 	    {
   13130 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   13131 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   13132 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   13133 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   13134 	      symtab_shndx_hdr->sh_size = amt;
   13135 
   13136 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   13137 							       off, true);
   13138 
   13139 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   13140 		  || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
   13141 		{
   13142 		  ret = false;
   13143 		  goto return_local_hash_table;
   13144 		}
   13145 	    }
   13146 	}
   13147 
   13148       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   13149       /* sh_name was set in prep_headers.  */
   13150       symstrtab_hdr->sh_type = SHT_STRTAB;
   13151       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   13152       symstrtab_hdr->sh_addr = 0;
   13153       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   13154       symstrtab_hdr->sh_entsize = 0;
   13155       symstrtab_hdr->sh_link = 0;
   13156       symstrtab_hdr->sh_info = 0;
   13157       /* sh_offset is set just below.  */
   13158       symstrtab_hdr->sh_addralign = 1;
   13159 
   13160       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   13161 						       off, true);
   13162       elf_next_file_pos (abfd) = off;
   13163 
   13164       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   13165 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   13166 	{
   13167 	  ret = false;
   13168 	  goto return_local_hash_table;
   13169 	}
   13170     }
   13171 
   13172   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
   13173     {
   13174       _bfd_error_handler (_("%pB: failed to generate import library"),
   13175 			  info->out_implib_bfd);
   13176       ret = false;
   13177       goto return_local_hash_table;
   13178     }
   13179 
   13180   /* Adjust the relocs to have the correct symbol indices.  */
   13181   for (o = abfd->sections; o != NULL; o = o->next)
   13182     {
   13183       struct bfd_elf_section_data *esdo = elf_section_data (o);
   13184       bool sort;
   13185 
   13186       if ((o->flags & SEC_RELOC) == 0)
   13187 	continue;
   13188 
   13189       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   13190       if (esdo->rel.hdr != NULL
   13191 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
   13192 	{
   13193 	  ret = false;
   13194 	  goto return_local_hash_table;
   13195 	}
   13196       if (esdo->rela.hdr != NULL
   13197 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
   13198 	{
   13199 	  ret = false;
   13200 	  goto return_local_hash_table;
   13201 	}
   13202 
   13203       /* Set the reloc_count field to 0 to prevent write_relocs from
   13204 	 trying to swap the relocs out itself.  */
   13205       o->reloc_count = 0;
   13206     }
   13207 
   13208   relativecount = 0;
   13209   if (dynamic && info->combreloc && dynobj != NULL)
   13210     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   13211 
   13212   relr_entsize = 0;
   13213   if (htab->srelrdyn != NULL
   13214       && htab->srelrdyn->output_section != NULL
   13215       && htab->srelrdyn->size != 0)
   13216     {
   13217       asection *s = htab->srelrdyn->output_section;
   13218       relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
   13219       if (relr_entsize == 0)
   13220 	{
   13221 	  relr_entsize = bed->s->arch_size / 8;
   13222 	  elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
   13223 	}
   13224     }
   13225 
   13226   /* If we are linking against a dynamic object, or generating a
   13227      shared library, finish up the dynamic linking information.  */
   13228   if (dynamic)
   13229     {
   13230       bfd_byte *dyncon, *dynconend;
   13231 
   13232       /* Fix up .dynamic entries.  */
   13233       o = bfd_get_linker_section (dynobj, ".dynamic");
   13234       BFD_ASSERT (o != NULL);
   13235 
   13236       dyncon = o->contents;
   13237       dynconend = PTR_ADD (o->contents, o->size);
   13238       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13239 	{
   13240 	  Elf_Internal_Dyn dyn;
   13241 	  const char *name;
   13242 	  unsigned int type;
   13243 	  bfd_size_type sh_size;
   13244 	  bfd_vma sh_addr;
   13245 
   13246 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13247 
   13248 	  switch (dyn.d_tag)
   13249 	    {
   13250 	    default:
   13251 	      continue;
   13252 	    case DT_NULL:
   13253 	      if (relativecount != 0)
   13254 		{
   13255 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   13256 		    {
   13257 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   13258 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   13259 		    }
   13260 		  if (dyn.d_tag != DT_NULL
   13261 		      && dynconend - dyncon >= bed->s->sizeof_dyn)
   13262 		    {
   13263 		      dyn.d_un.d_val = relativecount;
   13264 		      relativecount = 0;
   13265 		      break;
   13266 		    }
   13267 		  relativecount = 0;
   13268 		}
   13269 	      if (relr_entsize != 0)
   13270 		{
   13271 		  if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
   13272 		    {
   13273 		      asection *s = htab->srelrdyn;
   13274 		      dyn.d_tag = DT_RELR;
   13275 		      dyn.d_un.d_ptr
   13276 			= s->output_section->vma + s->output_offset;
   13277 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13278 		      dyncon += bed->s->sizeof_dyn;
   13279 
   13280 		      dyn.d_tag = DT_RELRSZ;
   13281 		      dyn.d_un.d_val = s->size;
   13282 		      bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13283 		      dyncon += bed->s->sizeof_dyn;
   13284 
   13285 		      dyn.d_tag = DT_RELRENT;
   13286 		      dyn.d_un.d_val = relr_entsize;
   13287 		      relr_entsize = 0;
   13288 		      break;
   13289 		    }
   13290 		  relr_entsize = 0;
   13291 		}
   13292 	      continue;
   13293 
   13294 	    case DT_INIT:
   13295 	      name = info->init_function;
   13296 	      goto get_sym;
   13297 	    case DT_FINI:
   13298 	      name = info->fini_function;
   13299 	    get_sym:
   13300 	      {
   13301 		struct elf_link_hash_entry *h;
   13302 
   13303 		h = elf_link_hash_lookup (htab, name, false, false, true);
   13304 		if (h != NULL
   13305 		    && (h->root.type == bfd_link_hash_defined
   13306 			|| h->root.type == bfd_link_hash_defweak))
   13307 		  {
   13308 		    dyn.d_un.d_ptr = h->root.u.def.value;
   13309 		    o = h->root.u.def.section;
   13310 		    if (o->output_section != NULL)
   13311 		      dyn.d_un.d_ptr += (o->output_section->vma
   13312 					 + o->output_offset);
   13313 		    else
   13314 		      {
   13315 			/* The symbol is imported from another shared
   13316 			   library and does not apply to this one.  */
   13317 			dyn.d_un.d_ptr = 0;
   13318 		      }
   13319 		    break;
   13320 		  }
   13321 	      }
   13322 	      continue;
   13323 
   13324 	    case DT_PREINIT_ARRAYSZ:
   13325 	      name = ".preinit_array";
   13326 	      goto get_out_size;
   13327 	    case DT_INIT_ARRAYSZ:
   13328 	      name = ".init_array";
   13329 	      goto get_out_size;
   13330 	    case DT_FINI_ARRAYSZ:
   13331 	      name = ".fini_array";
   13332 	    get_out_size:
   13333 	      o = bfd_get_section_by_name (abfd, name);
   13334 	      if (o == NULL)
   13335 		{
   13336 		  _bfd_error_handler
   13337 		    (_("could not find section %s"), name);
   13338 		  goto error_return;
   13339 		}
   13340 	      if (o->size == 0)
   13341 		_bfd_error_handler
   13342 		  (_("warning: %s section has zero size"), name);
   13343 	      dyn.d_un.d_val = o->size;
   13344 	      break;
   13345 
   13346 	    case DT_PREINIT_ARRAY:
   13347 	      name = ".preinit_array";
   13348 	      goto get_out_vma;
   13349 	    case DT_INIT_ARRAY:
   13350 	      name = ".init_array";
   13351 	      goto get_out_vma;
   13352 	    case DT_FINI_ARRAY:
   13353 	      name = ".fini_array";
   13354 	    get_out_vma:
   13355 	      o = bfd_get_section_by_name (abfd, name);
   13356 	      goto do_vma;
   13357 
   13358 	    case DT_HASH:
   13359 	      name = ".hash";
   13360 	      goto get_vma;
   13361 	    case DT_GNU_HASH:
   13362 	      name = ".gnu.hash";
   13363 	      goto get_vma;
   13364 	    case DT_STRTAB:
   13365 	      name = ".dynstr";
   13366 	      goto get_vma;
   13367 	    case DT_SYMTAB:
   13368 	      name = ".dynsym";
   13369 	      goto get_vma;
   13370 	    case DT_VERDEF:
   13371 	      name = ".gnu.version_d";
   13372 	      goto get_vma;
   13373 	    case DT_VERNEED:
   13374 	      name = ".gnu.version_r";
   13375 	      goto get_vma;
   13376 	    case DT_VERSYM:
   13377 	      name = ".gnu.version";
   13378 	    get_vma:
   13379 	      o = bfd_get_linker_section (dynobj, name);
   13380 	    do_vma:
   13381 	      if (o == NULL || bfd_is_abs_section (o->output_section))
   13382 		{
   13383 		  _bfd_error_handler
   13384 		    (_("could not find section %s"), name);
   13385 		  goto error_return;
   13386 		}
   13387 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   13388 		{
   13389 		  _bfd_error_handler
   13390 		    (_("warning: section '%s' is being made into a note"), name);
   13391 		  bfd_set_error (bfd_error_nonrepresentable_section);
   13392 		  goto error_return;
   13393 		}
   13394 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
   13395 	      break;
   13396 
   13397 	    case DT_REL:
   13398 	    case DT_RELA:
   13399 	    case DT_RELSZ:
   13400 	    case DT_RELASZ:
   13401 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   13402 		type = SHT_REL;
   13403 	      else
   13404 		type = SHT_RELA;
   13405 	      sh_size = 0;
   13406 	      sh_addr = 0;
   13407 	      for (i = 1; i < elf_numsections (abfd); i++)
   13408 		{
   13409 		  Elf_Internal_Shdr *hdr;
   13410 
   13411 		  hdr = elf_elfsections (abfd)[i];
   13412 		  if (hdr->sh_type == type
   13413 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   13414 		    {
   13415 		      sh_size += hdr->sh_size;
   13416 		      if (sh_addr == 0
   13417 			  || sh_addr > hdr->sh_addr)
   13418 			sh_addr = hdr->sh_addr;
   13419 		    }
   13420 		}
   13421 
   13422 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
   13423 		{
   13424 		  unsigned int opb = bfd_octets_per_byte (abfd, o);
   13425 
   13426 		  /* Don't count procedure linkage table relocs in the
   13427 		     overall reloc count.  */
   13428 		  sh_size -= htab->srelplt->size;
   13429 		  if (sh_size == 0)
   13430 		    /* If the size is zero, make the address zero too.
   13431 		       This is to avoid a glibc bug.  If the backend
   13432 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
   13433 		       zero, then we'll put DT_RELA at the end of
   13434 		       DT_JMPREL.  glibc will interpret the end of
   13435 		       DT_RELA matching the end of DT_JMPREL as the
   13436 		       case where DT_RELA includes DT_JMPREL, and for
   13437 		       LD_BIND_NOW will decide that processing DT_RELA
   13438 		       will process the PLT relocs too.  Net result:
   13439 		       No PLT relocs applied.  */
   13440 		    sh_addr = 0;
   13441 
   13442 		  /* If .rela.plt is the first .rela section, exclude
   13443 		     it from DT_RELA.  */
   13444 		  else if (sh_addr == (htab->srelplt->output_section->vma
   13445 				       + htab->srelplt->output_offset) * opb)
   13446 		    sh_addr += htab->srelplt->size;
   13447 		}
   13448 
   13449 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   13450 		dyn.d_un.d_val = sh_size;
   13451 	      else
   13452 		dyn.d_un.d_ptr = sh_addr;
   13453 	      break;
   13454 	    }
   13455 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   13456 	}
   13457     }
   13458 
   13459   /* If we have created any dynamic sections, then output them.  */
   13460   if (dynobj != NULL)
   13461     {
   13462       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   13463 	goto error_return;
   13464 
   13465       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   13466       if (bfd_link_textrel_check (info)
   13467 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
   13468 	  && o->size != 0)
   13469 	{
   13470 	  bfd_byte *dyncon, *dynconend;
   13471 
   13472 	  dyncon = o->contents;
   13473 	  dynconend = o->contents + o->size;
   13474 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   13475 	    {
   13476 	      Elf_Internal_Dyn dyn;
   13477 
   13478 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   13479 
   13480 	      if (dyn.d_tag == DT_TEXTREL)
   13481 		{
   13482 		  if (info->textrel_check == textrel_check_error)
   13483 		    info->callbacks->einfo
   13484 		      (_("%P%X: read-only segment has dynamic relocations\n"));
   13485 		  else if (bfd_link_dll (info))
   13486 		    info->callbacks->einfo
   13487 		      (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
   13488 		  else if (bfd_link_pde (info))
   13489 		    info->callbacks->einfo
   13490 		      (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
   13491 		  else
   13492 		    info->callbacks->einfo
   13493 		      (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
   13494 		  break;
   13495 		}
   13496 	    }
   13497 	}
   13498 
   13499       for (o = dynobj->sections; o != NULL; o = o->next)
   13500 	{
   13501 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   13502 	      || o->size == 0
   13503 	      || o->output_section == bfd_abs_section_ptr)
   13504 	    continue;
   13505 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   13506 	    {
   13507 	      /* At this point, we are only interested in sections
   13508 		 created by _bfd_elf_link_create_dynamic_sections.  */
   13509 	      continue;
   13510 	    }
   13511 	  if (htab->stab_info.stabstr == o)
   13512 	    continue;
   13513 	  if (htab->eh_info.hdr_sec == o)
   13514 	    continue;
   13515 	  if (strcmp (o->name, ".dynstr") != 0)
   13516 	    {
   13517 	      bfd_size_type octets = ((file_ptr) o->output_offset
   13518 				      * bfd_octets_per_byte (abfd, o));
   13519 	      if (!bfd_set_section_contents (abfd, o->output_section,
   13520 					     o->contents, octets, o->size))
   13521 		goto error_return;
   13522 	    }
   13523 	  else
   13524 	    {
   13525 	      /* The contents of the .dynstr section are actually in a
   13526 		 stringtab.  */
   13527 	      file_ptr off;
   13528 
   13529 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   13530 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   13531 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
   13532 		goto error_return;
   13533 	    }
   13534 	}
   13535     }
   13536 
   13537   if (!info->resolve_section_groups)
   13538     {
   13539       bool failed = false;
   13540 
   13541       BFD_ASSERT (bfd_link_relocatable (info));
   13542       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   13543       if (failed)
   13544 	goto error_return;
   13545     }
   13546 
   13547   /* If we have optimized stabs strings, output them.  */
   13548   if (htab->stab_info.stabstr != NULL)
   13549     {
   13550       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
   13551 	goto error_return;
   13552     }
   13553 
   13554   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   13555     goto error_return;
   13556 
   13557   if (! _bfd_elf_write_section_sframe (abfd, info))
   13558     goto error_return;
   13559 
   13560   if (info->callbacks->emit_ctf)
   13561       info->callbacks->emit_ctf ();
   13562 
   13563   elf_final_link_free (abfd, &flinfo);
   13564 
   13565   if (attr_section)
   13566     {
   13567       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
   13568       if (contents == NULL)
   13569 	{
   13570 	  /* Bail out and fail.  */
   13571 	  ret = false;
   13572 	  goto return_local_hash_table;
   13573 	}
   13574       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
   13575       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
   13576       free (contents);
   13577     }
   13578 
   13579  return_local_hash_table:
   13580   if (info->unique_symbol)
   13581     bfd_hash_table_free (&flinfo.local_hash_table);
   13582   return ret;
   13583 
   13584  error_return:
   13585   elf_final_link_free (abfd, &flinfo);
   13586   ret = false;
   13587   goto return_local_hash_table;
   13588 }
   13589 
   13590 /* Initialize COOKIE for input bfd ABFD.  */
   13592 
   13593 static bool
   13594 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   13595 		   struct bfd_link_info *info, bfd *abfd)
   13596 {
   13597   Elf_Internal_Shdr *symtab_hdr;
   13598   const struct elf_backend_data *bed;
   13599 
   13600   bed = get_elf_backend_data (abfd);
   13601   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13602 
   13603   cookie->abfd = abfd;
   13604   cookie->sym_hashes = elf_sym_hashes (abfd);
   13605   cookie->bad_symtab = elf_bad_symtab (abfd);
   13606   if (cookie->bad_symtab)
   13607     {
   13608       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   13609       cookie->extsymoff = 0;
   13610     }
   13611   else
   13612     {
   13613       cookie->locsymcount = symtab_hdr->sh_info;
   13614       cookie->extsymoff = symtab_hdr->sh_info;
   13615     }
   13616 
   13617   if (bed->s->arch_size == 32)
   13618     cookie->r_sym_shift = 8;
   13619   else
   13620     cookie->r_sym_shift = 32;
   13621 
   13622   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   13623   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   13624     {
   13625       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   13626 					      cookie->locsymcount, 0,
   13627 					      NULL, NULL, NULL);
   13628       if (cookie->locsyms == NULL)
   13629 	{
   13630 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   13631 	  return false;
   13632 	}
   13633       if (_bfd_link_keep_memory (info) )
   13634 	{
   13635 	  symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   13636 	  info->cache_size += (cookie->locsymcount
   13637 			       * sizeof (Elf_External_Sym_Shndx));
   13638 	}
   13639     }
   13640   return true;
   13641 }
   13642 
   13643 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   13644 
   13645 static void
   13646 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   13647 {
   13648   Elf_Internal_Shdr *symtab_hdr;
   13649 
   13650   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13651   if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   13652     free (cookie->locsyms);
   13653 }
   13654 
   13655 /* Initialize the relocation information in COOKIE for input section SEC
   13656    of input bfd ABFD.  */
   13657 
   13658 static bool
   13659 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13660 			struct bfd_link_info *info, bfd *abfd,
   13661 			asection *sec)
   13662 {
   13663   if (sec->reloc_count == 0)
   13664     {
   13665       cookie->rels = NULL;
   13666       cookie->relend = NULL;
   13667     }
   13668   else
   13669     {
   13670       cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
   13671 						     NULL, NULL,
   13672 						     _bfd_link_keep_memory (info));
   13673       if (cookie->rels == NULL)
   13674 	return false;
   13675       cookie->rel = cookie->rels;
   13676       cookie->relend = cookie->rels + sec->reloc_count;
   13677     }
   13678   cookie->rel = cookie->rels;
   13679   return true;
   13680 }
   13681 
   13682 /* Free the memory allocated by init_reloc_cookie_rels,
   13683    if appropriate.  */
   13684 
   13685 static void
   13686 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13687 			asection *sec)
   13688 {
   13689   if (elf_section_data (sec)->relocs != cookie->rels)
   13690     free (cookie->rels);
   13691 }
   13692 
   13693 /* Initialize the whole of COOKIE for input section SEC.  */
   13694 
   13695 static bool
   13696 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13697 			       struct bfd_link_info *info,
   13698 			       asection *sec)
   13699 {
   13700   if (!init_reloc_cookie (cookie, info, sec->owner))
   13701     goto error1;
   13702   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   13703     goto error2;
   13704   return true;
   13705 
   13706  error2:
   13707   fini_reloc_cookie (cookie, sec->owner);
   13708  error1:
   13709   return false;
   13710 }
   13711 
   13712 /* Free the memory allocated by init_reloc_cookie_for_section,
   13713    if appropriate.  */
   13714 
   13715 static void
   13716 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13717 			       asection *sec)
   13718 {
   13719   fini_reloc_cookie_rels (cookie, sec);
   13720   fini_reloc_cookie (cookie, sec->owner);
   13721 }
   13722 
   13723 /* Garbage collect unused sections.  */
   13725 
   13726 /* Default gc_mark_hook.  */
   13727 
   13728 asection *
   13729 _bfd_elf_gc_mark_hook (asection *sec,
   13730 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13731 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   13732 		       struct elf_link_hash_entry *h,
   13733 		       Elf_Internal_Sym *sym)
   13734 {
   13735   if (h != NULL)
   13736     {
   13737       switch (h->root.type)
   13738 	{
   13739 	case bfd_link_hash_defined:
   13740 	case bfd_link_hash_defweak:
   13741 	  return h->root.u.def.section;
   13742 
   13743 	case bfd_link_hash_common:
   13744 	  return h->root.u.c.p->section;
   13745 
   13746 	default:
   13747 	  break;
   13748 	}
   13749     }
   13750   else
   13751     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   13752 
   13753   return NULL;
   13754 }
   13755 
   13756 /* Return the debug definition section.  */
   13757 
   13758 static asection *
   13759 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
   13760 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13761 			   Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   13762 			   struct elf_link_hash_entry *h,
   13763 			   Elf_Internal_Sym *sym)
   13764 {
   13765   if (h != NULL)
   13766     {
   13767       /* Return the global debug definition section.  */
   13768       if ((h->root.type == bfd_link_hash_defined
   13769 	   || h->root.type == bfd_link_hash_defweak)
   13770 	  && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
   13771 	return h->root.u.def.section;
   13772     }
   13773   else
   13774     {
   13775       /* Return the local debug definition section.  */
   13776       asection *isec = bfd_section_from_elf_index (sec->owner,
   13777 						   sym->st_shndx);
   13778       if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
   13779 	return isec;
   13780     }
   13781 
   13782   return NULL;
   13783 }
   13784 
   13785 /* COOKIE->rel describes a relocation against section SEC, which is
   13786    a section we've decided to keep.  Return the section that contains
   13787    the relocation symbol, or NULL if no section contains it.  */
   13788 
   13789 asection *
   13790 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   13791 		       elf_gc_mark_hook_fn gc_mark_hook,
   13792 		       struct elf_reloc_cookie *cookie,
   13793 		       bool *start_stop)
   13794 {
   13795   unsigned long r_symndx;
   13796   struct elf_link_hash_entry *h, *hw;
   13797 
   13798   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   13799   if (r_symndx == STN_UNDEF)
   13800     return NULL;
   13801 
   13802   if (r_symndx >= cookie->locsymcount
   13803       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   13804     {
   13805       bool was_marked;
   13806 
   13807       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
   13808       if (h == NULL)
   13809 	{
   13810 	  info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
   13811 				  sec->owner);
   13812 	  return NULL;
   13813 	}
   13814       while (h->root.type == bfd_link_hash_indirect
   13815 	     || h->root.type == bfd_link_hash_warning)
   13816 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   13817 
   13818       was_marked = h->mark;
   13819       h->mark = 1;
   13820       /* Keep all aliases of the symbol too.  If an object symbol
   13821 	 needs to be copied into .dynbss then all of its aliases
   13822 	 should be present as dynamic symbols, not just the one used
   13823 	 on the copy relocation.  */
   13824       hw = h;
   13825       while (hw->is_weakalias)
   13826 	{
   13827 	  hw = hw->u.alias;
   13828 	  hw->mark = 1;
   13829 	}
   13830 
   13831       if (!was_marked && h->start_stop && !h->root.ldscript_def)
   13832 	{
   13833 	  if (info->start_stop_gc)
   13834 	    return NULL;
   13835 
   13836 	  /* To work around a glibc bug, mark XXX input sections
   13837 	     when there is a reference to __start_XXX or __stop_XXX
   13838 	     symbols.  */
   13839 	  else if (start_stop != NULL)
   13840 	    {
   13841 	      asection *s = h->u2.start_stop_section;
   13842 	      *start_stop = true;
   13843 	      return s;
   13844 	    }
   13845 	}
   13846 
   13847       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   13848     }
   13849 
   13850   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   13851 			  &cookie->locsyms[r_symndx]);
   13852 }
   13853 
   13854 /* COOKIE->rel describes a relocation against section SEC, which is
   13855    a section we've decided to keep.  Mark the section that contains
   13856    the relocation symbol.  */
   13857 
   13858 bool
   13859 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   13860 			asection *sec,
   13861 			elf_gc_mark_hook_fn gc_mark_hook,
   13862 			struct elf_reloc_cookie *cookie)
   13863 {
   13864   asection *rsec;
   13865   bool start_stop = false;
   13866 
   13867   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   13868   while (rsec != NULL)
   13869     {
   13870       if (!rsec->gc_mark)
   13871 	{
   13872 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   13873 	      || (rsec->owner->flags & DYNAMIC) != 0)
   13874 	    rsec->gc_mark = 1;
   13875 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   13876 	    return false;
   13877 	}
   13878       if (!start_stop)
   13879 	break;
   13880       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   13881     }
   13882   return true;
   13883 }
   13884 
   13885 /* The mark phase of garbage collection.  For a given section, mark
   13886    it and any sections in this section's group, and all the sections
   13887    which define symbols to which it refers.  */
   13888 
   13889 bool
   13890 _bfd_elf_gc_mark (struct bfd_link_info *info,
   13891 		  asection *sec,
   13892 		  elf_gc_mark_hook_fn gc_mark_hook)
   13893 {
   13894   bool ret;
   13895   asection *group_sec, *eh_frame;
   13896 
   13897   sec->gc_mark = 1;
   13898 
   13899   /* Mark all the sections in the group.  */
   13900   group_sec = elf_section_data (sec)->next_in_group;
   13901   if (group_sec && !group_sec->gc_mark)
   13902     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   13903       return false;
   13904 
   13905   /* Look through the section relocs.  */
   13906   ret = true;
   13907   eh_frame = elf_eh_frame_section (sec->owner);
   13908   if ((sec->flags & SEC_RELOC) != 0
   13909       && sec->reloc_count > 0
   13910       && sec != eh_frame)
   13911     {
   13912       struct elf_reloc_cookie cookie;
   13913 
   13914       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   13915 	ret = false;
   13916       else
   13917 	{
   13918 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   13919 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   13920 	      {
   13921 		ret = false;
   13922 		break;
   13923 	      }
   13924 	  fini_reloc_cookie_for_section (&cookie, sec);
   13925 	}
   13926     }
   13927 
   13928   if (ret && eh_frame && elf_fde_list (sec))
   13929     {
   13930       struct elf_reloc_cookie cookie;
   13931 
   13932       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
   13933 	ret = false;
   13934       else
   13935 	{
   13936 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   13937 				      gc_mark_hook, &cookie))
   13938 	    ret = false;
   13939 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   13940 	}
   13941     }
   13942 
   13943   eh_frame = elf_section_eh_frame_entry (sec);
   13944   if (ret && eh_frame && !eh_frame->gc_mark)
   13945     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   13946       ret = false;
   13947 
   13948   return ret;
   13949 }
   13950 
   13951 /* Scan and mark sections in a special or debug section group.  */
   13952 
   13953 static void
   13954 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   13955 {
   13956   /* Point to first section of section group.  */
   13957   asection *ssec;
   13958   /* Used to iterate the section group.  */
   13959   asection *msec;
   13960 
   13961   bool is_special_grp = true;
   13962   bool is_debug_grp = true;
   13963 
   13964   /* First scan to see if group contains any section other than debug
   13965      and special section.  */
   13966   ssec = msec = elf_next_in_group (grp);
   13967   do
   13968     {
   13969       if ((msec->flags & SEC_DEBUGGING) == 0)
   13970 	is_debug_grp = false;
   13971 
   13972       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   13973 	is_special_grp = false;
   13974 
   13975       msec = elf_next_in_group (msec);
   13976     }
   13977   while (msec != ssec);
   13978 
   13979   /* If this is a pure debug section group or pure special section group,
   13980      keep all sections in this group.  */
   13981   if (is_debug_grp || is_special_grp)
   13982     {
   13983       do
   13984 	{
   13985 	  msec->gc_mark = 1;
   13986 	  msec = elf_next_in_group (msec);
   13987 	}
   13988       while (msec != ssec);
   13989     }
   13990 }
   13991 
   13992 /* Keep debug and special sections.  */
   13993 
   13994 bool
   13995 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   13996 				 elf_gc_mark_hook_fn mark_hook)
   13997 {
   13998   bfd *ibfd;
   13999 
   14000   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14001     {
   14002       asection *isec;
   14003       bool some_kept;
   14004       bool debug_frag_seen;
   14005       bool has_kept_debug_info;
   14006 
   14007       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14008 	continue;
   14009       isec = ibfd->sections;
   14010       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14011 	continue;
   14012 
   14013       /* Ensure all linker created sections are kept,
   14014 	 see if any other section is already marked,
   14015 	 and note if we have any fragmented debug sections.  */
   14016       debug_frag_seen = some_kept = has_kept_debug_info = false;
   14017       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14018 	{
   14019 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   14020 	    isec->gc_mark = 1;
   14021 	  else if (isec->gc_mark
   14022 		   && (isec->flags & SEC_ALLOC) != 0
   14023 		   && elf_section_type (isec) != SHT_NOTE)
   14024 	    some_kept = true;
   14025 	  else
   14026 	    {
   14027 	      /* Since all sections, except for backend specific ones,
   14028 		 have been garbage collected, call mark_hook on this
   14029 		 section if any of its linked-to sections is marked.  */
   14030 	      asection *linked_to_sec;
   14031 	      for (linked_to_sec = elf_linked_to_section (isec);
   14032 		   linked_to_sec != NULL && !linked_to_sec->linker_mark;
   14033 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14034 		{
   14035 		  if (linked_to_sec->gc_mark)
   14036 		    {
   14037 		      if (!_bfd_elf_gc_mark (info, isec, mark_hook))
   14038 			return false;
   14039 		      break;
   14040 		    }
   14041 		  linked_to_sec->linker_mark = 1;
   14042 		}
   14043 	      for (linked_to_sec = elf_linked_to_section (isec);
   14044 		   linked_to_sec != NULL && linked_to_sec->linker_mark;
   14045 		   linked_to_sec = elf_linked_to_section (linked_to_sec))
   14046 		linked_to_sec->linker_mark = 0;
   14047 	    }
   14048 
   14049 	  if (!debug_frag_seen
   14050 	      && (isec->flags & SEC_DEBUGGING)
   14051 	      && startswith (isec->name, ".debug_line."))
   14052 	    debug_frag_seen = true;
   14053 	  else if (strcmp (bfd_section_name (isec),
   14054 			   "__patchable_function_entries") == 0
   14055 		   && elf_linked_to_section (isec) == NULL)
   14056 	      info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
   14057 					"need linked-to section "
   14058 					"for --gc-sections\n"),
   14059 				      isec->owner, isec);
   14060 	}
   14061 
   14062       /* If no non-note alloc section in this file will be kept, then
   14063 	 we can toss out the debug and special sections.  */
   14064       if (!some_kept)
   14065 	continue;
   14066 
   14067       /* Keep debug and special sections like .comment when they are
   14068 	 not part of a group.  Also keep section groups that contain
   14069 	 just debug sections or special sections.  NB: Sections with
   14070 	 linked-to section has been handled above.  */
   14071       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14072 	{
   14073 	  if ((isec->flags & SEC_GROUP) != 0)
   14074 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   14075 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   14076 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   14077 		   && elf_next_in_group (isec) == NULL
   14078 		   && elf_linked_to_section (isec) == NULL)
   14079 	    isec->gc_mark = 1;
   14080 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
   14081 	    has_kept_debug_info = true;
   14082 	}
   14083 
   14084       /* Look for CODE sections which are going to be discarded,
   14085 	 and find and discard any fragmented debug sections which
   14086 	 are associated with that code section.  */
   14087       if (debug_frag_seen)
   14088 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14089 	  if ((isec->flags & SEC_CODE) != 0
   14090 	      && isec->gc_mark == 0)
   14091 	    {
   14092 	      unsigned int ilen;
   14093 	      asection *dsec;
   14094 
   14095 	      ilen = strlen (isec->name);
   14096 
   14097 	      /* Association is determined by the name of the debug
   14098 		 section containing the name of the code section as
   14099 		 a suffix.  For example .debug_line.text.foo is a
   14100 		 debug section associated with .text.foo.  */
   14101 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   14102 		{
   14103 		  unsigned int dlen;
   14104 
   14105 		  if (dsec->gc_mark == 0
   14106 		      || (dsec->flags & SEC_DEBUGGING) == 0)
   14107 		    continue;
   14108 
   14109 		  dlen = strlen (dsec->name);
   14110 
   14111 		  if (dlen > ilen
   14112 		      && strncmp (dsec->name + (dlen - ilen),
   14113 				  isec->name, ilen) == 0)
   14114 		    dsec->gc_mark = 0;
   14115 		}
   14116 	  }
   14117 
   14118       /* Mark debug sections referenced by kept debug sections.  */
   14119       if (has_kept_debug_info)
   14120 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   14121 	  if (isec->gc_mark
   14122 	      && (isec->flags & SEC_DEBUGGING) != 0)
   14123 	    if (!_bfd_elf_gc_mark (info, isec,
   14124 				   elf_gc_mark_debug_section))
   14125 	      return false;
   14126     }
   14127   return true;
   14128 }
   14129 
   14130 static bool
   14131 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   14132 {
   14133   bfd *sub;
   14134   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14135 
   14136   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14137     {
   14138       asection *o;
   14139 
   14140       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14141 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
   14142 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14143 	continue;
   14144       o = sub->sections;
   14145       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14146 	continue;
   14147 
   14148       for (o = sub->sections; o != NULL; o = o->next)
   14149 	{
   14150 	  /* When any section in a section group is kept, we keep all
   14151 	     sections in the section group.  If the first member of
   14152 	     the section group is excluded, we will also exclude the
   14153 	     group section.  */
   14154 	  if (o->flags & SEC_GROUP)
   14155 	    {
   14156 	      asection *first = elf_next_in_group (o);
   14157 	      o->gc_mark = first->gc_mark;
   14158 	    }
   14159 
   14160 	  if (o->gc_mark)
   14161 	    continue;
   14162 
   14163 	  /* Skip sweeping sections already excluded.  */
   14164 	  if (o->flags & SEC_EXCLUDE)
   14165 	    continue;
   14166 
   14167 	  /* Since this is early in the link process, it is simple
   14168 	     to remove a section from the output.  */
   14169 	  o->flags |= SEC_EXCLUDE;
   14170 
   14171 	  if (info->print_gc_sections && o->size != 0)
   14172 	    /* xgettext:c-format */
   14173 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
   14174 				o, sub);
   14175 	}
   14176     }
   14177 
   14178   return true;
   14179 }
   14180 
   14181 /* Propagate collected vtable information.  This is called through
   14182    elf_link_hash_traverse.  */
   14183 
   14184 static bool
   14185 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   14186 {
   14187   /* Those that are not vtables.  */
   14188   if (h->start_stop
   14189       || h->u2.vtable == NULL
   14190       || h->u2.vtable->parent == NULL)
   14191     return true;
   14192 
   14193   /* Those vtables that do not have parents, we cannot merge.  */
   14194   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
   14195     return true;
   14196 
   14197   /* If we've already been done, exit.  */
   14198   if (h->u2.vtable->used && h->u2.vtable->used[-1])
   14199     return true;
   14200 
   14201   /* Make sure the parent's table is up to date.  */
   14202   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
   14203 
   14204   if (h->u2.vtable->used == NULL)
   14205     {
   14206       /* None of this table's entries were referenced.  Re-use the
   14207 	 parent's table.  */
   14208       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
   14209       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
   14210     }
   14211   else
   14212     {
   14213       size_t n;
   14214       bool *cu, *pu;
   14215 
   14216       /* Or the parent's entries into ours.  */
   14217       cu = h->u2.vtable->used;
   14218       cu[-1] = true;
   14219       pu = h->u2.vtable->parent->u2.vtable->used;
   14220       if (pu != NULL)
   14221 	{
   14222 	  const struct elf_backend_data *bed;
   14223 	  unsigned int log_file_align;
   14224 
   14225 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   14226 	  log_file_align = bed->s->log_file_align;
   14227 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
   14228 	  while (n--)
   14229 	    {
   14230 	      if (*pu)
   14231 		*cu = true;
   14232 	      pu++;
   14233 	      cu++;
   14234 	    }
   14235 	}
   14236     }
   14237 
   14238   return true;
   14239 }
   14240 
   14241 struct link_info_ok
   14242 {
   14243   struct bfd_link_info *info;
   14244   bool ok;
   14245 };
   14246 
   14247 static bool
   14248 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
   14249 				    void *ptr)
   14250 {
   14251   asection *sec;
   14252   bfd_vma hstart, hend;
   14253   Elf_Internal_Rela *relstart, *relend, *rel;
   14254   const struct elf_backend_data *bed;
   14255   unsigned int log_file_align;
   14256   struct link_info_ok *info = (struct link_info_ok *) ptr;
   14257 
   14258   /* Take care of both those symbols that do not describe vtables as
   14259      well as those that are not loaded.  */
   14260   if (h->start_stop
   14261       || h->u2.vtable == NULL
   14262       || h->u2.vtable->parent == NULL)
   14263     return true;
   14264 
   14265   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   14266 	      || h->root.type == bfd_link_hash_defweak);
   14267 
   14268   sec = h->root.u.def.section;
   14269   hstart = h->root.u.def.value;
   14270   hend = hstart + h->size;
   14271 
   14272   relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
   14273 					     sec, NULL, NULL, true);
   14274   if (!relstart)
   14275     return info->ok = false;
   14276   bed = get_elf_backend_data (sec->owner);
   14277   log_file_align = bed->s->log_file_align;
   14278 
   14279   relend = relstart + sec->reloc_count;
   14280 
   14281   for (rel = relstart; rel < relend; ++rel)
   14282     if (rel->r_offset >= hstart && rel->r_offset < hend)
   14283       {
   14284 	/* If the entry is in use, do nothing.  */
   14285 	if (h->u2.vtable->used
   14286 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
   14287 	  {
   14288 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   14289 	    if (h->u2.vtable->used[entry])
   14290 	      continue;
   14291 	  }
   14292 	/* Otherwise, kill it.  */
   14293 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   14294       }
   14295 
   14296   return true;
   14297 }
   14298 
   14299 /* Mark sections containing dynamically referenced symbols.  When
   14300    building shared libraries, we must assume that any visible symbol is
   14301    referenced.  */
   14302 
   14303 bool
   14304 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   14305 {
   14306   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   14307   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   14308 
   14309   if ((h->root.type == bfd_link_hash_defined
   14310        || h->root.type == bfd_link_hash_defweak)
   14311       && (!h->start_stop
   14312 	  || h->root.ldscript_def
   14313 	  || !info->start_stop_gc)
   14314       && ((h->ref_dynamic && !h->forced_local)
   14315 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   14316 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   14317 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   14318 	      && (!bfd_link_executable (info)
   14319 		  || info->gc_keep_exported
   14320 		  || info->export_dynamic
   14321 		  || (h->dynamic
   14322 		      && d != NULL
   14323 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   14324 	      && (h->versioned >= versioned
   14325 		  || !bfd_hide_sym_by_version (info->version_info,
   14326 					       h->root.root.string)))))
   14327     h->root.u.def.section->flags |= SEC_KEEP;
   14328 
   14329   return true;
   14330 }
   14331 
   14332 /* Keep all sections containing symbols undefined on the command-line,
   14333    and the section containing the entry symbol.  */
   14334 
   14335 void
   14336 _bfd_elf_gc_keep (struct bfd_link_info *info)
   14337 {
   14338   struct bfd_sym_chain *sym;
   14339 
   14340   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   14341     {
   14342       struct elf_link_hash_entry *h;
   14343 
   14344       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   14345 				false, false, false);
   14346 
   14347       if (h != NULL
   14348 	  && (h->root.type == bfd_link_hash_defined
   14349 	      || h->root.type == bfd_link_hash_defweak)
   14350 	  && !bfd_is_const_section (h->root.u.def.section))
   14351 	h->root.u.def.section->flags |= SEC_KEEP;
   14352     }
   14353 }
   14354 
   14355 bool
   14356 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   14357 				struct bfd_link_info *info)
   14358 {
   14359   bfd *ibfd = info->input_bfds;
   14360 
   14361   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14362     {
   14363       asection *sec;
   14364       struct elf_reloc_cookie cookie;
   14365 
   14366       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   14367 	continue;
   14368       sec = ibfd->sections;
   14369       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14370 	continue;
   14371 
   14372       if (!init_reloc_cookie (&cookie, info, ibfd))
   14373 	return false;
   14374 
   14375       for (sec = ibfd->sections; sec; sec = sec->next)
   14376 	{
   14377 	  if (startswith (bfd_section_name (sec), ".eh_frame_entry")
   14378 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
   14379 	    {
   14380 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   14381 	      fini_reloc_cookie_rels (&cookie, sec);
   14382 	    }
   14383 	}
   14384     }
   14385   return true;
   14386 }
   14387 
   14388 /* Do mark and sweep of unused sections.  */
   14389 
   14390 bool
   14391 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   14392 {
   14393   bool ok = true;
   14394   bfd *sub;
   14395   elf_gc_mark_hook_fn gc_mark_hook;
   14396   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14397   struct elf_link_hash_table *htab;
   14398   struct link_info_ok info_ok;
   14399 
   14400   if (!bed->can_gc_sections
   14401       || !is_elf_hash_table (info->hash))
   14402     {
   14403       _bfd_error_handler(_("warning: gc-sections option ignored"));
   14404       return true;
   14405     }
   14406 
   14407   bed->gc_keep (info);
   14408   htab = elf_hash_table (info);
   14409 
   14410   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   14411      at the .eh_frame section if we can mark the FDEs individually.  */
   14412   for (sub = info->input_bfds;
   14413        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   14414        sub = sub->link.next)
   14415     {
   14416       asection *sec;
   14417       struct elf_reloc_cookie cookie;
   14418 
   14419       sec = sub->sections;
   14420       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14421 	continue;
   14422       sec = bfd_get_section_by_name (sub, ".eh_frame");
   14423       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
   14424 	{
   14425 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   14426 	  if (elf_section_data (sec)->sec_info
   14427 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   14428 	    elf_eh_frame_section (sub) = sec;
   14429 	  fini_reloc_cookie_for_section (&cookie, sec);
   14430 	  sec = bfd_get_next_section_by_name (NULL, sec);
   14431 	}
   14432     }
   14433 
   14434   /* Apply transitive closure to the vtable entry usage info.  */
   14435   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   14436   if (!ok)
   14437     return false;
   14438 
   14439   /* Kill the vtable relocations that were not used.  */
   14440   info_ok.info = info;
   14441   info_ok.ok = true;
   14442   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
   14443   if (!info_ok.ok)
   14444     return false;
   14445 
   14446   /* Mark dynamically referenced symbols.  */
   14447   if (htab->dynamic_sections_created || info->gc_keep_exported)
   14448     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   14449 
   14450   /* Grovel through relocs to find out who stays ...  */
   14451   gc_mark_hook = bed->gc_mark_hook;
   14452   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   14453     {
   14454       asection *o;
   14455 
   14456       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   14457 	  || elf_object_id (sub) != elf_hash_table_id (htab)
   14458 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   14459 	continue;
   14460 
   14461       o = sub->sections;
   14462       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14463 	continue;
   14464 
   14465       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   14466 	 Also treat note sections as a root, if the section is not part
   14467 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
   14468 	 well as FINI_ARRAY sections for ld -r.  */
   14469       for (o = sub->sections; o != NULL; o = o->next)
   14470 	if (!o->gc_mark
   14471 	    && (o->flags & SEC_EXCLUDE) == 0
   14472 	    && ((o->flags & SEC_KEEP) != 0
   14473 		|| (bfd_link_relocatable (info)
   14474 		    && ((elf_section_data (o)->this_hdr.sh_type
   14475 			 == SHT_PREINIT_ARRAY)
   14476 			|| (elf_section_data (o)->this_hdr.sh_type
   14477 			    == SHT_INIT_ARRAY)
   14478 			|| (elf_section_data (o)->this_hdr.sh_type
   14479 			    == SHT_FINI_ARRAY)))
   14480 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   14481 		    && elf_next_in_group (o) == NULL
   14482 		    && elf_linked_to_section (o) == NULL)
   14483 		|| ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
   14484 		    && (elf_section_flags (o) & SHF_GNU_RETAIN))))
   14485 	  {
   14486 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   14487 	      return false;
   14488 	  }
   14489     }
   14490 
   14491   /* Allow the backend to mark additional target specific sections.  */
   14492   bed->gc_mark_extra_sections (info, gc_mark_hook);
   14493 
   14494   /* ... and mark SEC_EXCLUDE for those that go.  */
   14495   return elf_gc_sweep (abfd, info);
   14496 }
   14497 
   14498 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   14500 
   14501 bool
   14502 bfd_elf_gc_record_vtinherit (bfd *abfd,
   14503 			     asection *sec,
   14504 			     struct elf_link_hash_entry *h,
   14505 			     bfd_vma offset)
   14506 {
   14507   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   14508   struct elf_link_hash_entry **search, *child;
   14509   size_t extsymcount;
   14510   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14511 
   14512   /* The sh_info field of the symtab header tells us where the
   14513      external symbols start.  We don't care about the local symbols at
   14514      this point.  */
   14515   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   14516   if (!elf_bad_symtab (abfd))
   14517     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   14518 
   14519   sym_hashes = elf_sym_hashes (abfd);
   14520   sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
   14521 
   14522   /* Hunt down the child symbol, which is in this section at the same
   14523      offset as the relocation.  */
   14524   for (search = sym_hashes; search != sym_hashes_end; ++search)
   14525     {
   14526       if ((child = *search) != NULL
   14527 	  && (child->root.type == bfd_link_hash_defined
   14528 	      || child->root.type == bfd_link_hash_defweak)
   14529 	  && child->root.u.def.section == sec
   14530 	  && child->root.u.def.value == offset)
   14531 	goto win;
   14532     }
   14533 
   14534   /* xgettext:c-format */
   14535   _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
   14536 		      abfd, sec, (uint64_t) offset);
   14537   bfd_set_error (bfd_error_invalid_operation);
   14538   return false;
   14539 
   14540  win:
   14541   if (!child->u2.vtable)
   14542     {
   14543       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14544 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
   14545       if (!child->u2.vtable)
   14546 	return false;
   14547     }
   14548   if (!h)
   14549     {
   14550       /* This *should* only be the absolute section.  It could potentially
   14551 	 be that someone has defined a non-global vtable though, which
   14552 	 would be bad.  It isn't worth paging in the local symbols to be
   14553 	 sure though; that case should simply be handled by the assembler.  */
   14554 
   14555       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
   14556     }
   14557   else
   14558     child->u2.vtable->parent = h;
   14559 
   14560   return true;
   14561 }
   14562 
   14563 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   14564 
   14565 bool
   14566 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
   14567 			   struct elf_link_hash_entry *h,
   14568 			   bfd_vma addend)
   14569 {
   14570   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14571   unsigned int log_file_align = bed->s->log_file_align;
   14572 
   14573   if (!h)
   14574     {
   14575       /* xgettext:c-format */
   14576       _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
   14577 			  abfd, sec);
   14578       bfd_set_error (bfd_error_bad_value);
   14579       return false;
   14580     }
   14581 
   14582   if (!h->u2.vtable)
   14583     {
   14584       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
   14585 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
   14586       if (!h->u2.vtable)
   14587 	return false;
   14588     }
   14589 
   14590   if (addend >= h->u2.vtable->size)
   14591     {
   14592       size_t size, bytes, file_align;
   14593       bool *ptr = h->u2.vtable->used;
   14594 
   14595       /* While the symbol is undefined, we have to be prepared to handle
   14596 	 a zero size.  */
   14597       file_align = 1 << log_file_align;
   14598       if (h->root.type == bfd_link_hash_undefined)
   14599 	size = addend + file_align;
   14600       else
   14601 	{
   14602 	  size = h->size;
   14603 	  if (addend >= size)
   14604 	    {
   14605 	      /* Oops!  We've got a reference past the defined end of
   14606 		 the table.  This is probably a bug -- shall we warn?  */
   14607 	      size = addend + file_align;
   14608 	    }
   14609 	}
   14610       size = (size + file_align - 1) & -file_align;
   14611 
   14612       /* Allocate one extra entry for use as a "done" flag for the
   14613 	 consolidation pass.  */
   14614       bytes = ((size >> log_file_align) + 1) * sizeof (bool);
   14615 
   14616       if (ptr)
   14617 	{
   14618 	  ptr = (bool *) bfd_realloc (ptr - 1, bytes);
   14619 
   14620 	  if (ptr != NULL)
   14621 	    {
   14622 	      size_t oldbytes;
   14623 
   14624 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
   14625 			  * sizeof (bool));
   14626 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   14627 	    }
   14628 	}
   14629       else
   14630 	ptr = (bool *) bfd_zmalloc (bytes);
   14631 
   14632       if (ptr == NULL)
   14633 	return false;
   14634 
   14635       /* And arrange for that done flag to be at index -1.  */
   14636       h->u2.vtable->used = ptr + 1;
   14637       h->u2.vtable->size = size;
   14638     }
   14639 
   14640   h->u2.vtable->used[addend >> log_file_align] = true;
   14641 
   14642   return true;
   14643 }
   14644 
   14645 /* Map an ELF section header flag to its corresponding string.  */
   14646 typedef struct
   14647 {
   14648   char *flag_name;
   14649   flagword flag_value;
   14650 } elf_flags_to_name_table;
   14651 
   14652 static const elf_flags_to_name_table elf_flags_to_names [] =
   14653 {
   14654   { "SHF_WRITE", SHF_WRITE },
   14655   { "SHF_ALLOC", SHF_ALLOC },
   14656   { "SHF_EXECINSTR", SHF_EXECINSTR },
   14657   { "SHF_MERGE", SHF_MERGE },
   14658   { "SHF_STRINGS", SHF_STRINGS },
   14659   { "SHF_INFO_LINK", SHF_INFO_LINK},
   14660   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   14661   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   14662   { "SHF_GROUP", SHF_GROUP },
   14663   { "SHF_TLS", SHF_TLS },
   14664   { "SHF_MASKOS", SHF_MASKOS },
   14665   { "SHF_EXCLUDE", SHF_EXCLUDE },
   14666 };
   14667 
   14668 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   14669 bool
   14670 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   14671 			      struct flag_info *flaginfo,
   14672 			      asection *section)
   14673 {
   14674   const bfd_vma sh_flags = elf_section_flags (section);
   14675 
   14676   if (!flaginfo->flags_initialized)
   14677     {
   14678       bfd *obfd = info->output_bfd;
   14679       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   14680       struct flag_info_list *tf = flaginfo->flag_list;
   14681       int with_hex = 0;
   14682       int without_hex = 0;
   14683 
   14684       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   14685 	{
   14686 	  unsigned i;
   14687 	  flagword (*lookup) (char *);
   14688 
   14689 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   14690 	  if (lookup != NULL)
   14691 	    {
   14692 	      flagword hexval = (*lookup) ((char *) tf->name);
   14693 
   14694 	      if (hexval != 0)
   14695 		{
   14696 		  if (tf->with == with_flags)
   14697 		    with_hex |= hexval;
   14698 		  else if (tf->with == without_flags)
   14699 		    without_hex |= hexval;
   14700 		  tf->valid = true;
   14701 		  continue;
   14702 		}
   14703 	    }
   14704 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   14705 	    {
   14706 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   14707 		{
   14708 		  if (tf->with == with_flags)
   14709 		    with_hex |= elf_flags_to_names[i].flag_value;
   14710 		  else if (tf->with == without_flags)
   14711 		    without_hex |= elf_flags_to_names[i].flag_value;
   14712 		  tf->valid = true;
   14713 		  break;
   14714 		}
   14715 	    }
   14716 	  if (!tf->valid)
   14717 	    {
   14718 	      info->callbacks->einfo
   14719 		(_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   14720 	      return false;
   14721 	    }
   14722 	}
   14723       flaginfo->flags_initialized = true;
   14724       flaginfo->only_with_flags |= with_hex;
   14725       flaginfo->not_with_flags |= without_hex;
   14726     }
   14727 
   14728   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   14729     return false;
   14730 
   14731   if ((flaginfo->not_with_flags & sh_flags) != 0)
   14732     return false;
   14733 
   14734   return true;
   14735 }
   14736 
   14737 struct alloc_got_off_arg {
   14738   bfd_vma gotoff;
   14739   struct bfd_link_info *info;
   14740 };
   14741 
   14742 /* We need a special top-level link routine to convert got reference counts
   14743    to real got offsets.  */
   14744 
   14745 static bool
   14746 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   14747 {
   14748   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   14749   bfd *obfd = gofarg->info->output_bfd;
   14750   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   14751 
   14752   if (h->got.refcount > 0)
   14753     {
   14754       h->got.offset = gofarg->gotoff;
   14755       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   14756     }
   14757   else
   14758     h->got.offset = (bfd_vma) -1;
   14759 
   14760   return true;
   14761 }
   14762 
   14763 /* And an accompanying bit to work out final got entry offsets once
   14764    we're done.  Should be called from final_link.  */
   14765 
   14766 bool
   14767 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   14768 					struct bfd_link_info *info)
   14769 {
   14770   bfd *i;
   14771   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14772   bfd_vma gotoff;
   14773   struct alloc_got_off_arg gofarg;
   14774 
   14775   BFD_ASSERT (abfd == info->output_bfd);
   14776 
   14777   if (! is_elf_hash_table (info->hash))
   14778     return false;
   14779 
   14780   /* The GOT offset is relative to the .got section, but the GOT header is
   14781      put into the .got.plt section, if the backend uses it.  */
   14782   if (bed->want_got_plt)
   14783     gotoff = 0;
   14784   else
   14785     gotoff = bed->got_header_size;
   14786 
   14787   /* Do the local .got entries first.  */
   14788   for (i = info->input_bfds; i; i = i->link.next)
   14789     {
   14790       bfd_signed_vma *local_got;
   14791       size_t j, locsymcount;
   14792       Elf_Internal_Shdr *symtab_hdr;
   14793 
   14794       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   14795 	continue;
   14796 
   14797       local_got = elf_local_got_refcounts (i);
   14798       if (!local_got)
   14799 	continue;
   14800 
   14801       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   14802       if (elf_bad_symtab (i))
   14803 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   14804       else
   14805 	locsymcount = symtab_hdr->sh_info;
   14806 
   14807       for (j = 0; j < locsymcount; ++j)
   14808 	{
   14809 	  if (local_got[j] > 0)
   14810 	    {
   14811 	      local_got[j] = gotoff;
   14812 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   14813 	    }
   14814 	  else
   14815 	    local_got[j] = (bfd_vma) -1;
   14816 	}
   14817     }
   14818 
   14819   /* Then the global .got entries.  .plt refcounts are handled by
   14820      adjust_dynamic_symbol  */
   14821   gofarg.gotoff = gotoff;
   14822   gofarg.info = info;
   14823   elf_link_hash_traverse (elf_hash_table (info),
   14824 			  elf_gc_allocate_got_offsets,
   14825 			  &gofarg);
   14826   return true;
   14827 }
   14828 
   14829 /* Many folk need no more in the way of final link than this, once
   14830    got entry reference counting is enabled.  */
   14831 
   14832 bool
   14833 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   14834 {
   14835   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   14836     return false;
   14837 
   14838   /* Invoke the regular ELF backend linker to do all the work.  */
   14839   return bfd_elf_final_link (abfd, info);
   14840 }
   14841 
   14842 bool
   14843 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   14844 {
   14845   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   14846 
   14847   if (rcookie->bad_symtab)
   14848     rcookie->rel = rcookie->rels;
   14849 
   14850   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   14851     {
   14852       unsigned long r_symndx;
   14853 
   14854       if (! rcookie->bad_symtab)
   14855 	if (rcookie->rel->r_offset > offset)
   14856 	  return false;
   14857       if (rcookie->rel->r_offset != offset)
   14858 	continue;
   14859 
   14860       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   14861       if (r_symndx == STN_UNDEF)
   14862 	return true;
   14863 
   14864       if (r_symndx >= rcookie->locsymcount
   14865 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   14866 	{
   14867 	  struct elf_link_hash_entry *h;
   14868 
   14869 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
   14870 
   14871 	  while (h->root.type == bfd_link_hash_indirect
   14872 		 || h->root.type == bfd_link_hash_warning)
   14873 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   14874 
   14875 	  if ((h->root.type == bfd_link_hash_defined
   14876 	       || h->root.type == bfd_link_hash_defweak)
   14877 	      && (h->root.u.def.section->owner != rcookie->abfd
   14878 		  || h->root.u.def.section->kept_section != NULL
   14879 		  || discarded_section (h->root.u.def.section)))
   14880 	    return true;
   14881 	}
   14882       else
   14883 	{
   14884 	  /* It's not a relocation against a global symbol,
   14885 	     but it could be a relocation against a local
   14886 	     symbol for a discarded section.  */
   14887 	  asection *isec;
   14888 	  Elf_Internal_Sym *isym;
   14889 
   14890 	  /* Need to: get the symbol; get the section.  */
   14891 	  isym = &rcookie->locsyms[r_symndx];
   14892 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   14893 	  if (isec != NULL
   14894 	      && (isec->kept_section != NULL
   14895 		  || discarded_section (isec)))
   14896 	    return true;
   14897 	}
   14898       return false;
   14899     }
   14900   return false;
   14901 }
   14902 
   14903 /* Discard unneeded references to discarded sections.
   14904    Returns -1 on error, 1 if any section's size was changed, 0 if
   14905    nothing changed.  This function assumes that the relocations are in
   14906    sorted order, which is true for all known assemblers.  */
   14907 
   14908 int
   14909 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   14910 {
   14911   struct elf_reloc_cookie cookie;
   14912   asection *o;
   14913   bfd *abfd;
   14914   int changed = 0;
   14915 
   14916   if (info->traditional_format
   14917       || !is_elf_hash_table (info->hash))
   14918     return 0;
   14919 
   14920   o = bfd_get_section_by_name (output_bfd, ".stab");
   14921   if (o != NULL)
   14922     {
   14923       asection *i;
   14924 
   14925       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   14926 	{
   14927 	  if (i->size == 0
   14928 	      || i->reloc_count == 0
   14929 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   14930 	    continue;
   14931 
   14932 	  abfd = i->owner;
   14933 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   14934 	    continue;
   14935 
   14936 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   14937 	    return -1;
   14938 
   14939 	  if (_bfd_discard_section_stabs (abfd, i,
   14940 					  elf_section_data (i)->sec_info,
   14941 					  bfd_elf_reloc_symbol_deleted_p,
   14942 					  &cookie))
   14943 	    changed = 1;
   14944 
   14945 	  fini_reloc_cookie_for_section (&cookie, i);
   14946 	}
   14947     }
   14948 
   14949   o = NULL;
   14950   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   14951     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   14952   if (o != NULL)
   14953     {
   14954       asection *i;
   14955       int eh_changed = 0;
   14956       unsigned int eh_alignment;  /* Octets.  */
   14957 
   14958       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   14959 	{
   14960 	  if (i->size == 0)
   14961 	    continue;
   14962 
   14963 	  abfd = i->owner;
   14964 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   14965 	    continue;
   14966 
   14967 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   14968 	    return -1;
   14969 
   14970 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   14971 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   14972 						 bfd_elf_reloc_symbol_deleted_p,
   14973 						 &cookie))
   14974 	    {
   14975 	      eh_changed = 1;
   14976 	      if (i->size != i->rawsize)
   14977 		changed = 1;
   14978 	    }
   14979 
   14980 	  fini_reloc_cookie_for_section (&cookie, i);
   14981 	}
   14982 
   14983       eh_alignment = ((1 << o->alignment_power)
   14984 		      * bfd_octets_per_byte (output_bfd, o));
   14985       /* Skip over zero terminator, and prevent empty sections from
   14986 	 adding alignment padding at the end.  */
   14987       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
   14988 	if (i->size == 0)
   14989 	  i->flags |= SEC_EXCLUDE;
   14990 	else if (i->size > 4)
   14991 	  break;
   14992       /* The last non-empty eh_frame section doesn't need padding.  */
   14993       if (i != NULL)
   14994 	i = i->map_tail.s;
   14995       /* Any prior sections must pad the last FDE out to the output
   14996 	 section alignment.  Otherwise we might have zero padding
   14997 	 between sections, which would be seen as a terminator.  */
   14998       for (; i != NULL; i = i->map_tail.s)
   14999 	if (i->size == 4)
   15000 	  /* All but the last zero terminator should have been removed.  */
   15001 	  BFD_FAIL ();
   15002 	else
   15003 	  {
   15004 	    bfd_size_type size
   15005 	      = (i->size + eh_alignment - 1) & -eh_alignment;
   15006 	    if (i->size != size)
   15007 	      {
   15008 		i->size = size;
   15009 		changed = 1;
   15010 		eh_changed = 1;
   15011 	      }
   15012 	  }
   15013       if (eh_changed)
   15014 	elf_link_hash_traverse (elf_hash_table (info),
   15015 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
   15016     }
   15017 
   15018   o = bfd_get_section_by_name (output_bfd, ".sframe");
   15019   if (o != NULL)
   15020     {
   15021       asection *i;
   15022 
   15023       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   15024 	{
   15025 	  if (i->size == 0)
   15026 	    continue;
   15027 
   15028 	  abfd = i->owner;
   15029 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15030 	    continue;
   15031 
   15032 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   15033 	    return -1;
   15034 
   15035 	  if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
   15036 	    {
   15037 	      if (_bfd_elf_discard_section_sframe (i,
   15038 						   bfd_elf_reloc_symbol_deleted_p,
   15039 						   &cookie))
   15040 		{
   15041 		  if (i->size != i->rawsize)
   15042 		    changed = 1;
   15043 		}
   15044 	    }
   15045 	  fini_reloc_cookie_for_section (&cookie, i);
   15046 	}
   15047       /* Update the reference to the output .sframe section.  Used to
   15048 	 determine later if PT_GNU_SFRAME segment is to be generated.  */
   15049       if (!_bfd_elf_set_section_sframe (output_bfd, info))
   15050 	return -1;
   15051     }
   15052 
   15053   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   15054     {
   15055       const struct elf_backend_data *bed;
   15056       asection *s;
   15057 
   15058       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   15059 	continue;
   15060       s = abfd->sections;
   15061       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   15062 	continue;
   15063 
   15064       bed = get_elf_backend_data (abfd);
   15065 
   15066       if (bed->elf_backend_discard_info != NULL)
   15067 	{
   15068 	  if (!init_reloc_cookie (&cookie, info, abfd))
   15069 	    return -1;
   15070 
   15071 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   15072 	    changed = 1;
   15073 
   15074 	  fini_reloc_cookie (&cookie, abfd);
   15075 	}
   15076     }
   15077 
   15078   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   15079     _bfd_elf_end_eh_frame_parsing (info);
   15080 
   15081   if (info->eh_frame_hdr_type
   15082       && !bfd_link_relocatable (info)
   15083       && _bfd_elf_discard_section_eh_frame_hdr (info))
   15084     changed = 1;
   15085 
   15086   return changed;
   15087 }
   15088 
   15089 bool
   15090 _bfd_elf_section_already_linked (bfd *abfd,
   15091 				 asection *sec,
   15092 				 struct bfd_link_info *info)
   15093 {
   15094   flagword flags;
   15095   const char *name, *key;
   15096   struct bfd_section_already_linked *l;
   15097   struct bfd_section_already_linked_hash_entry *already_linked_list;
   15098 
   15099   if (sec->output_section == bfd_abs_section_ptr)
   15100     return false;
   15101 
   15102   flags = sec->flags;
   15103 
   15104   /* Return if it isn't a linkonce section.  A comdat group section
   15105      also has SEC_LINK_ONCE set.  */
   15106   if ((flags & SEC_LINK_ONCE) == 0)
   15107     return false;
   15108 
   15109   /* Don't put group member sections on our list of already linked
   15110      sections.  They are handled as a group via their group section.  */
   15111   if (elf_sec_group (sec) != NULL)
   15112     return false;
   15113 
   15114   /* For a SHT_GROUP section, use the group signature as the key.  */
   15115   name = sec->name;
   15116   if ((flags & SEC_GROUP) != 0
   15117       && elf_next_in_group (sec) != NULL
   15118       && elf_group_name (elf_next_in_group (sec)) != NULL)
   15119     key = elf_group_name (elf_next_in_group (sec));
   15120   else
   15121     {
   15122       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   15123       if (startswith (name, ".gnu.linkonce.")
   15124 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   15125 	key++;
   15126       else
   15127 	/* Must be a user linkonce section that doesn't follow gcc's
   15128 	   naming convention.  In this case we won't be matching
   15129 	   single member groups.  */
   15130 	key = name;
   15131     }
   15132 
   15133   already_linked_list = bfd_section_already_linked_table_lookup (key);
   15134 
   15135   for (l = already_linked_list->entry; l != NULL; l = l->next)
   15136     {
   15137       /* We may have 2 different types of sections on the list: group
   15138 	 sections with a signature of <key> (<key> is some string),
   15139 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   15140 	 Match like sections.  LTO plugin sections are an exception.
   15141 	 They are always named .gnu.linkonce.t.<key> and match either
   15142 	 type of section.  */
   15143       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   15144 	   && ((flags & SEC_GROUP) != 0
   15145 	       || strcmp (name, l->sec->name) == 0))
   15146 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0
   15147 	  || (sec->owner->flags & BFD_PLUGIN) != 0)
   15148 	{
   15149 	  /* The section has already been linked.  See if we should
   15150 	     issue a warning.  */
   15151 	  if (!_bfd_handle_already_linked (sec, l, info))
   15152 	    return false;
   15153 
   15154 	  if (flags & SEC_GROUP)
   15155 	    {
   15156 	      asection *first = elf_next_in_group (sec);
   15157 	      asection *s = first;
   15158 
   15159 	      while (s != NULL)
   15160 		{
   15161 		  s->output_section = bfd_abs_section_ptr;
   15162 		  /* Record which group discards it.  */
   15163 		  s->kept_section = l->sec;
   15164 		  s = elf_next_in_group (s);
   15165 		  /* These lists are circular.  */
   15166 		  if (s == first)
   15167 		    break;
   15168 		}
   15169 	    }
   15170 
   15171 	  return true;
   15172 	}
   15173     }
   15174 
   15175   /* A single member comdat group section may be discarded by a
   15176      linkonce section and vice versa.  */
   15177   if ((flags & SEC_GROUP) != 0)
   15178     {
   15179       asection *first = elf_next_in_group (sec);
   15180 
   15181       if (first != NULL && elf_next_in_group (first) == first)
   15182 	/* Check this single member group against linkonce sections.  */
   15183 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   15184 	  if ((l->sec->flags & SEC_GROUP) == 0
   15185 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   15186 	    {
   15187 	      first->output_section = bfd_abs_section_ptr;
   15188 	      first->kept_section = l->sec;
   15189 	      sec->output_section = bfd_abs_section_ptr;
   15190 	      break;
   15191 	    }
   15192     }
   15193   else
   15194     /* Check this linkonce section against single member groups.  */
   15195     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15196       if (l->sec->flags & SEC_GROUP)
   15197 	{
   15198 	  asection *first = elf_next_in_group (l->sec);
   15199 
   15200 	  if (first != NULL
   15201 	      && elf_next_in_group (first) == first
   15202 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   15203 	    {
   15204 	      sec->output_section = bfd_abs_section_ptr;
   15205 	      sec->kept_section = first;
   15206 	      break;
   15207 	    }
   15208 	}
   15209 
   15210   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   15211      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   15212      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   15213      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   15214      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   15215      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   15216      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   15217      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   15218      The reverse order cannot happen as there is never a bfd with only the
   15219      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   15220      matter as here were are looking only for cross-bfd sections.  */
   15221 
   15222   if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
   15223     for (l = already_linked_list->entry; l != NULL; l = l->next)
   15224       if ((l->sec->flags & SEC_GROUP) == 0
   15225 	  && startswith (l->sec->name, ".gnu.linkonce.t."))
   15226 	{
   15227 	  if (abfd != l->sec->owner)
   15228 	    sec->output_section = bfd_abs_section_ptr;
   15229 	  break;
   15230 	}
   15231 
   15232   /* This is the first section with this name.  Record it.  */
   15233   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   15234     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   15235   return sec->output_section == bfd_abs_section_ptr;
   15236 }
   15237 
   15238 bool
   15239 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   15240 {
   15241   return sym->st_shndx == SHN_COMMON;
   15242 }
   15243 
   15244 unsigned int
   15245 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   15246 {
   15247   return SHN_COMMON;
   15248 }
   15249 
   15250 asection *
   15251 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   15252 {
   15253   return bfd_com_section_ptr;
   15254 }
   15255 
   15256 bfd_vma
   15257 _bfd_elf_default_got_elt_size (bfd *abfd,
   15258 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   15259 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   15260 			       bfd *ibfd ATTRIBUTE_UNUSED,
   15261 			       unsigned long symndx ATTRIBUTE_UNUSED)
   15262 {
   15263   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15264   return bed->s->arch_size / 8;
   15265 }
   15266 
   15267 /* Routines to support the creation of dynamic relocs.  */
   15268 
   15269 /* Returns the name of the dynamic reloc section associated with SEC.  */
   15270 
   15271 static const char *
   15272 get_dynamic_reloc_section_name (bfd *       abfd,
   15273 				asection *  sec,
   15274 				bool is_rela)
   15275 {
   15276   char *name;
   15277   const char *old_name = bfd_section_name (sec);
   15278   const char *prefix = is_rela ? ".rela" : ".rel";
   15279 
   15280   if (old_name == NULL)
   15281     return NULL;
   15282 
   15283   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   15284   sprintf (name, "%s%s", prefix, old_name);
   15285 
   15286   return name;
   15287 }
   15288 
   15289 /* Returns the dynamic reloc section associated with SEC.
   15290    If necessary compute the name of the dynamic reloc section based
   15291    on SEC's name (looked up in ABFD's string table) and the setting
   15292    of IS_RELA.  */
   15293 
   15294 asection *
   15295 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
   15296 				    asection *sec,
   15297 				    bool is_rela)
   15298 {
   15299   asection *reloc_sec = elf_section_data (sec)->sreloc;
   15300 
   15301   if (reloc_sec == NULL)
   15302     {
   15303       const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15304 
   15305       if (name != NULL)
   15306 	{
   15307 	  reloc_sec = bfd_get_linker_section (abfd, name);
   15308 
   15309 	  if (reloc_sec != NULL)
   15310 	    elf_section_data (sec)->sreloc = reloc_sec;
   15311 	}
   15312     }
   15313 
   15314   return reloc_sec;
   15315 }
   15316 
   15317 /* Returns the dynamic reloc section associated with SEC.  If the
   15318    section does not exist it is created and attached to the DYNOBJ
   15319    bfd and stored in the SRELOC field of SEC's elf_section_data
   15320    structure.
   15321 
   15322    ALIGNMENT is the alignment for the newly created section and
   15323    IS_RELA defines whether the name should be .rela.<SEC's name>
   15324    or .rel.<SEC's name>.  The section name is looked up in the
   15325    string table associated with ABFD.  */
   15326 
   15327 asection *
   15328 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   15329 				     bfd *dynobj,
   15330 				     unsigned int alignment,
   15331 				     bfd *abfd,
   15332 				     bool is_rela)
   15333 {
   15334   asection * reloc_sec = elf_section_data (sec)->sreloc;
   15335 
   15336   if (reloc_sec == NULL)
   15337     {
   15338       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   15339 
   15340       if (name == NULL)
   15341 	return NULL;
   15342 
   15343       reloc_sec = bfd_get_linker_section (dynobj, name);
   15344 
   15345       if (reloc_sec == NULL)
   15346 	{
   15347 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   15348 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   15349 	  if ((sec->flags & SEC_ALLOC) != 0)
   15350 	    flags |= SEC_ALLOC | SEC_LOAD;
   15351 
   15352 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   15353 	  if (reloc_sec != NULL)
   15354 	    {
   15355 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   15356 		 name.  Override as it may be wrong, eg. for a user
   15357 		 section named "auto" we'll get ".relauto" which is
   15358 		 seen to be a .rela section.  */
   15359 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   15360 	      if (!bfd_set_section_alignment (reloc_sec, alignment))
   15361 		reloc_sec = NULL;
   15362 	    }
   15363 	}
   15364 
   15365       elf_section_data (sec)->sreloc = reloc_sec;
   15366     }
   15367 
   15368   return reloc_sec;
   15369 }
   15370 
   15371 /* Copy the ELF symbol type and other attributes for a linker script
   15372    assignment from HSRC to HDEST.  Generally this should be treated as
   15373    if we found a strong non-dynamic definition for HDEST (except that
   15374    ld ignores multiple definition errors).  */
   15375 void
   15376 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   15377 				     struct bfd_link_hash_entry *hdest,
   15378 				     struct bfd_link_hash_entry *hsrc)
   15379 {
   15380   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   15381   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   15382   Elf_Internal_Sym isym;
   15383 
   15384   ehdest->type = ehsrc->type;
   15385   ehdest->target_internal = ehsrc->target_internal;
   15386 
   15387   isym.st_other = ehsrc->other;
   15388   elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
   15389 }
   15390 
   15391 /* Append a RELA relocation REL to section S in BFD.  */
   15392 
   15393 void
   15394 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15395 {
   15396   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15397   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   15398   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   15399   bed->s->swap_reloca_out (abfd, rel, loc);
   15400 }
   15401 
   15402 /* Append a REL relocation REL to section S in BFD.  */
   15403 
   15404 void
   15405 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   15406 {
   15407   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15408   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   15409   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   15410   bed->s->swap_reloc_out (abfd, rel, loc);
   15411 }
   15412 
   15413 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
   15414 
   15415 struct bfd_link_hash_entry *
   15416 bfd_elf_define_start_stop (struct bfd_link_info *info,
   15417 			   const char *symbol, asection *sec)
   15418 {
   15419   struct elf_link_hash_entry *h;
   15420 
   15421   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
   15422 			    false, false, true);
   15423   /* NB: Common symbols will be turned into definition later.  */
   15424   if (h != NULL
   15425       && !h->root.ldscript_def
   15426       && (h->root.type == bfd_link_hash_undefined
   15427 	  || h->root.type == bfd_link_hash_undefweak
   15428 	  || ((h->ref_regular || h->def_dynamic)
   15429 	      && !h->def_regular
   15430 	      && h->root.type != bfd_link_hash_common)))
   15431     {
   15432       bool was_dynamic = h->ref_dynamic || h->def_dynamic;
   15433       h->verinfo.verdef = NULL;
   15434       h->root.type = bfd_link_hash_defined;
   15435       h->root.u.def.section = sec;
   15436       h->root.u.def.value = 0;
   15437       h->def_regular = 1;
   15438       h->def_dynamic = 0;
   15439       h->start_stop = 1;
   15440       h->u2.start_stop_section = sec;
   15441       if (symbol[0] == '.')
   15442 	{
   15443 	  /* .startof. and .sizeof. symbols are local.  */
   15444 	  const struct elf_backend_data *bed;
   15445 	  bed = get_elf_backend_data (info->output_bfd);
   15446 	  (*bed->elf_backend_hide_symbol) (info, h, true);
   15447 	}
   15448       else
   15449 	{
   15450 	  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   15451 	    h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
   15452 			| info->start_stop_visibility);
   15453 	  if (was_dynamic)
   15454 	    bfd_elf_link_record_dynamic_symbol (info, h);
   15455 	}
   15456       return &h->root;
   15457     }
   15458   return NULL;
   15459 }
   15460 
   15461 /* Find dynamic relocs for H that apply to read-only sections.  */
   15462 
   15463 asection *
   15464 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
   15465 {
   15466   struct elf_dyn_relocs *p;
   15467 
   15468   for (p = h->dyn_relocs; p != NULL; p = p->next)
   15469     {
   15470       asection *s = p->sec->output_section;
   15471 
   15472       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   15473 	return p->sec;
   15474     }
   15475   return NULL;
   15476 }
   15477 
   15478 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   15479    read-only sections.  */
   15480 
   15481 bool
   15482 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
   15483 {
   15484   asection *sec;
   15485 
   15486   if (h->root.type == bfd_link_hash_indirect)
   15487     return true;
   15488 
   15489   sec = _bfd_elf_readonly_dynrelocs (h);
   15490   if (sec != NULL)
   15491     {
   15492       struct bfd_link_info *info = (struct bfd_link_info *) inf;
   15493 
   15494       info->flags |= DF_TEXTREL;
   15495       /* xgettext:c-format */
   15496       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
   15497 				"in read-only section `%pA'\n"),
   15498 			      sec->owner, h->root.root.string, sec);
   15499 
   15500       if (bfd_link_textrel_check (info))
   15501 	/* xgettext:c-format */
   15502 	info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
   15503 				  "in read-only section `%pA'\n"),
   15504 				sec->owner, h->root.root.string, sec);
   15505 
   15506       /* Not an error, just cut short the traversal.  */
   15507       return false;
   15508     }
   15509   return true;
   15510 }
   15511 
   15512 /* Add dynamic tags.  */
   15513 
   15514 bool
   15515 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
   15516 			   bool need_dynamic_reloc)
   15517 {
   15518   struct elf_link_hash_table *htab = elf_hash_table (info);
   15519 
   15520   if (htab->dynamic_sections_created)
   15521     {
   15522       /* Add some entries to the .dynamic section.  We fill in the
   15523 	 values later, in finish_dynamic_sections, but we must add
   15524 	 the entries now so that we get the correct size for the
   15525 	 .dynamic section.  The DT_DEBUG entry is filled in by the
   15526 	 dynamic linker and used by the debugger.  */
   15527 #define add_dynamic_entry(TAG, VAL) \
   15528   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   15529 
   15530       const struct elf_backend_data *bed
   15531 	= get_elf_backend_data (output_bfd);
   15532 
   15533       if (bfd_link_executable (info))
   15534 	{
   15535 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   15536 	    return false;
   15537 	}
   15538 
   15539       if (htab->dt_pltgot_required || htab->splt->size != 0)
   15540 	{
   15541 	  /* DT_PLTGOT is used by prelink even if there is no PLT
   15542 	     relocation.  */
   15543 	  if (!add_dynamic_entry (DT_PLTGOT, 0))
   15544 	    return false;
   15545 	}
   15546 
   15547       if (htab->dt_jmprel_required || htab->srelplt->size != 0)
   15548 	{
   15549 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
   15550 	      || !add_dynamic_entry (DT_PLTREL,
   15551 				     (bed->rela_plts_and_copies_p
   15552 				      ? DT_RELA : DT_REL))
   15553 	      || !add_dynamic_entry (DT_JMPREL, 0))
   15554 	    return false;
   15555 	}
   15556 
   15557       if (htab->tlsdesc_plt
   15558 	  && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
   15559 	      || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
   15560 	return false;
   15561 
   15562       if (need_dynamic_reloc)
   15563 	{
   15564 	  if (bed->rela_plts_and_copies_p)
   15565 	    {
   15566 	      if (!add_dynamic_entry (DT_RELA, 0)
   15567 		  || !add_dynamic_entry (DT_RELASZ, 0)
   15568 		  || !add_dynamic_entry (DT_RELAENT,
   15569 					 bed->s->sizeof_rela))
   15570 		return false;
   15571 	    }
   15572 	  else
   15573 	    {
   15574 	      if (!add_dynamic_entry (DT_REL, 0)
   15575 		  || !add_dynamic_entry (DT_RELSZ, 0)
   15576 		  || !add_dynamic_entry (DT_RELENT,
   15577 					 bed->s->sizeof_rel))
   15578 		return false;
   15579 	    }
   15580 
   15581 	  /* If any dynamic relocs apply to a read-only section,
   15582 	     then we need a DT_TEXTREL entry.  */
   15583 	  if ((info->flags & DF_TEXTREL) == 0)
   15584 	    elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
   15585 				    info);
   15586 
   15587 	  if ((info->flags & DF_TEXTREL) != 0)
   15588 	    {
   15589 	      if (htab->ifunc_resolvers)
   15590 		info->callbacks->einfo
   15591 		  (_("%P: warning: GNU indirect functions with DT_TEXTREL "
   15592 		     "may result in a segfault at runtime; recompile with %s\n"),
   15593 		   bfd_link_dll (info) ? "-fPIC" : "-fPIE");
   15594 
   15595 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   15596 		return false;
   15597 	    }
   15598 	}
   15599     }
   15600 #undef add_dynamic_entry
   15601 
   15602   return true;
   15603 }
   15604