Home | History | Annotate | Line # | Download | only in bfd
elflink.c revision 1.1.1.10
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2020 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 /* This struct is used to pass information to routines called via
     36    elf_link_hash_traverse which must return failure.  */
     37 
     38 struct elf_info_failed
     39 {
     40   struct bfd_link_info *info;
     41   bfd_boolean failed;
     42 };
     43 
     44 /* This structure is used to pass information to
     45    _bfd_elf_link_find_version_dependencies.  */
     46 
     47 struct elf_find_verdep_info
     48 {
     49   /* General link information.  */
     50   struct bfd_link_info *info;
     51   /* The number of dependencies.  */
     52   unsigned int vers;
     53   /* Whether we had a failure.  */
     54   bfd_boolean failed;
     55 };
     56 
     57 static bfd_boolean _bfd_elf_fix_symbol_flags
     58   (struct elf_link_hash_entry *, struct elf_info_failed *);
     59 
     60 asection *
     61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
     62 			     unsigned long r_symndx,
     63 			     bfd_boolean discard)
     64 {
     65   if (r_symndx >= cookie->locsymcount
     66       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
     67     {
     68       struct elf_link_hash_entry *h;
     69 
     70       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
     71 
     72       while (h->root.type == bfd_link_hash_indirect
     73 	     || h->root.type == bfd_link_hash_warning)
     74 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
     75 
     76       if ((h->root.type == bfd_link_hash_defined
     77 	   || h->root.type == bfd_link_hash_defweak)
     78 	   && discarded_section (h->root.u.def.section))
     79 	return h->root.u.def.section;
     80       else
     81 	return NULL;
     82     }
     83   else
     84     {
     85       /* It's not a relocation against a global symbol,
     86 	 but it could be a relocation against a local
     87 	 symbol for a discarded section.  */
     88       asection *isec;
     89       Elf_Internal_Sym *isym;
     90 
     91       /* Need to: get the symbol; get the section.  */
     92       isym = &cookie->locsyms[r_symndx];
     93       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
     94       if (isec != NULL
     95 	  && discard ? discarded_section (isec) : 1)
     96 	return isec;
     97      }
     98   return NULL;
     99 }
    100 
    101 /* Define a symbol in a dynamic linkage section.  */
    102 
    103 struct elf_link_hash_entry *
    104 _bfd_elf_define_linkage_sym (bfd *abfd,
    105 			     struct bfd_link_info *info,
    106 			     asection *sec,
    107 			     const char *name)
    108 {
    109   struct elf_link_hash_entry *h;
    110   struct bfd_link_hash_entry *bh;
    111   const struct elf_backend_data *bed;
    112 
    113   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
    114   if (h != NULL)
    115     {
    116       /* Zap symbol defined in an as-needed lib that wasn't linked.
    117 	 This is a symptom of a larger problem:  Absolute symbols
    118 	 defined in shared libraries can't be overridden, because we
    119 	 lose the link to the bfd which is via the symbol section.  */
    120       h->root.type = bfd_link_hash_new;
    121       bh = &h->root;
    122     }
    123   else
    124     bh = NULL;
    125 
    126   bed = get_elf_backend_data (abfd);
    127   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    128 					 sec, 0, NULL, FALSE, bed->collect,
    129 					 &bh))
    130     return NULL;
    131   h = (struct elf_link_hash_entry *) bh;
    132   BFD_ASSERT (h != NULL);
    133   h->def_regular = 1;
    134   h->non_elf = 0;
    135   h->root.linker_def = 1;
    136   h->type = STT_OBJECT;
    137   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    138     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    139 
    140   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    141   return h;
    142 }
    143 
    144 bfd_boolean
    145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    146 {
    147   flagword flags;
    148   asection *s;
    149   struct elf_link_hash_entry *h;
    150   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    151   struct elf_link_hash_table *htab = elf_hash_table (info);
    152 
    153   /* This function may be called more than once.  */
    154   if (htab->sgot != NULL)
    155     return TRUE;
    156 
    157   flags = bed->dynamic_sec_flags;
    158 
    159   s = bfd_make_section_anyway_with_flags (abfd,
    160 					  (bed->rela_plts_and_copies_p
    161 					   ? ".rela.got" : ".rel.got"),
    162 					  (bed->dynamic_sec_flags
    163 					   | SEC_READONLY));
    164   if (s == NULL
    165       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    166     return FALSE;
    167   htab->srelgot = s;
    168 
    169   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    170   if (s == NULL
    171       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    172     return FALSE;
    173   htab->sgot = s;
    174 
    175   if (bed->want_got_plt)
    176     {
    177       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    178       if (s == NULL
    179 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    180 	return FALSE;
    181       htab->sgotplt = s;
    182     }
    183 
    184   /* The first bit of the global offset table is the header.  */
    185   s->size += bed->got_header_size;
    186 
    187   if (bed->want_got_sym)
    188     {
    189       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    190 	 (or .got.plt) section.  We don't do this in the linker script
    191 	 because we don't want to define the symbol if we are not creating
    192 	 a global offset table.  */
    193       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    194 				       "_GLOBAL_OFFSET_TABLE_");
    195       elf_hash_table (info)->hgot = h;
    196       if (h == NULL)
    197 	return FALSE;
    198     }
    199 
    200   return TRUE;
    201 }
    202 
    203 /* Create a strtab to hold the dynamic symbol names.  */
    205 static bfd_boolean
    206 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    207 {
    208   struct elf_link_hash_table *hash_table;
    209 
    210   hash_table = elf_hash_table (info);
    211   if (hash_table->dynobj == NULL)
    212     {
    213       /* We may not set dynobj, an input file holding linker created
    214 	 dynamic sections to abfd, which may be a dynamic object with
    215 	 its own dynamic sections.  We need to find a normal input file
    216 	 to hold linker created sections if possible.  */
    217       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
    218 	{
    219 	  bfd *ibfd;
    220 	  asection *s;
    221 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
    222 	    if ((ibfd->flags
    223 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
    224 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
    225 		&& elf_object_id (ibfd) == elf_hash_table_id (hash_table)
    226 		&& !((s = ibfd->sections) != NULL
    227 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
    228 	      {
    229 		abfd = ibfd;
    230 		break;
    231 	      }
    232 	}
    233       hash_table->dynobj = abfd;
    234     }
    235 
    236   if (hash_table->dynstr == NULL)
    237     {
    238       hash_table->dynstr = _bfd_elf_strtab_init ();
    239       if (hash_table->dynstr == NULL)
    240 	return FALSE;
    241     }
    242   return TRUE;
    243 }
    244 
    245 /* Create some sections which will be filled in with dynamic linking
    246    information.  ABFD is an input file which requires dynamic sections
    247    to be created.  The dynamic sections take up virtual memory space
    248    when the final executable is run, so we need to create them before
    249    addresses are assigned to the output sections.  We work out the
    250    actual contents and size of these sections later.  */
    251 
    252 bfd_boolean
    253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    254 {
    255   flagword flags;
    256   asection *s;
    257   const struct elf_backend_data *bed;
    258   struct elf_link_hash_entry *h;
    259 
    260   if (! is_elf_hash_table (info->hash))
    261     return FALSE;
    262 
    263   if (elf_hash_table (info)->dynamic_sections_created)
    264     return TRUE;
    265 
    266   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    267     return FALSE;
    268 
    269   abfd = elf_hash_table (info)->dynobj;
    270   bed = get_elf_backend_data (abfd);
    271 
    272   flags = bed->dynamic_sec_flags;
    273 
    274   /* A dynamically linked executable has a .interp section, but a
    275      shared library does not.  */
    276   if (bfd_link_executable (info) && !info->nointerp)
    277     {
    278       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    279 					      flags | SEC_READONLY);
    280       if (s == NULL)
    281 	return FALSE;
    282     }
    283 
    284   /* Create sections to hold version informations.  These are removed
    285      if they are not needed.  */
    286   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    287 					  flags | SEC_READONLY);
    288   if (s == NULL
    289       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    290     return FALSE;
    291 
    292   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    293 					  flags | SEC_READONLY);
    294   if (s == NULL
    295       || !bfd_set_section_alignment (s, 1))
    296     return FALSE;
    297 
    298   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    299 					  flags | SEC_READONLY);
    300   if (s == NULL
    301       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    302     return FALSE;
    303 
    304   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    305 					  flags | SEC_READONLY);
    306   if (s == NULL
    307       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    308     return FALSE;
    309   elf_hash_table (info)->dynsym = s;
    310 
    311   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    312 					  flags | SEC_READONLY);
    313   if (s == NULL)
    314     return FALSE;
    315 
    316   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    317   if (s == NULL
    318       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    319     return FALSE;
    320 
    321   /* The special symbol _DYNAMIC is always set to the start of the
    322      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    323      only want to define it if we are, in fact, creating a .dynamic
    324      section.  We don't want to define it if there is no .dynamic
    325      section, since on some ELF platforms the start up code examines it
    326      to decide how to initialize the process.  */
    327   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    328   elf_hash_table (info)->hdynamic = h;
    329   if (h == NULL)
    330     return FALSE;
    331 
    332   if (info->emit_hash)
    333     {
    334       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    335 					      flags | SEC_READONLY);
    336       if (s == NULL
    337 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    338 	return FALSE;
    339       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    340     }
    341 
    342   if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
    343     {
    344       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    345 					      flags | SEC_READONLY);
    346       if (s == NULL
    347 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    348 	return FALSE;
    349       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    350 	 4 32-bit words followed by variable count of 64-bit words, then
    351 	 variable count of 32-bit words.  */
    352       if (bed->s->arch_size == 64)
    353 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    354       else
    355 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    356     }
    357 
    358   /* Let the backend create the rest of the sections.  This lets the
    359      backend set the right flags.  The backend will normally create
    360      the .got and .plt sections.  */
    361   if (bed->elf_backend_create_dynamic_sections == NULL
    362       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    363     return FALSE;
    364 
    365   elf_hash_table (info)->dynamic_sections_created = TRUE;
    366 
    367   return TRUE;
    368 }
    369 
    370 /* Create dynamic sections when linking against a dynamic object.  */
    371 
    372 bfd_boolean
    373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    374 {
    375   flagword flags, pltflags;
    376   struct elf_link_hash_entry *h;
    377   asection *s;
    378   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    379   struct elf_link_hash_table *htab = elf_hash_table (info);
    380 
    381   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    382      .rel[a].bss sections.  */
    383   flags = bed->dynamic_sec_flags;
    384 
    385   pltflags = flags;
    386   if (bed->plt_not_loaded)
    387     /* We do not clear SEC_ALLOC here because we still want the OS to
    388        allocate space for the section; it's just that there's nothing
    389        to read in from the object file.  */
    390     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    391   else
    392     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    393   if (bed->plt_readonly)
    394     pltflags |= SEC_READONLY;
    395 
    396   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    397   if (s == NULL
    398       || !bfd_set_section_alignment (s, bed->plt_alignment))
    399     return FALSE;
    400   htab->splt = s;
    401 
    402   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    403      .plt section.  */
    404   if (bed->want_plt_sym)
    405     {
    406       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    407 				       "_PROCEDURE_LINKAGE_TABLE_");
    408       elf_hash_table (info)->hplt = h;
    409       if (h == NULL)
    410 	return FALSE;
    411     }
    412 
    413   s = bfd_make_section_anyway_with_flags (abfd,
    414 					  (bed->rela_plts_and_copies_p
    415 					   ? ".rela.plt" : ".rel.plt"),
    416 					  flags | SEC_READONLY);
    417   if (s == NULL
    418       || !bfd_set_section_alignment (s, bed->s->log_file_align))
    419     return FALSE;
    420   htab->srelplt = s;
    421 
    422   if (! _bfd_elf_create_got_section (abfd, info))
    423     return FALSE;
    424 
    425   if (bed->want_dynbss)
    426     {
    427       /* The .dynbss section is a place to put symbols which are defined
    428 	 by dynamic objects, are referenced by regular objects, and are
    429 	 not functions.  We must allocate space for them in the process
    430 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    431 	 initialize them at run time.  The linker script puts the .dynbss
    432 	 section into the .bss section of the final image.  */
    433       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    434 					      SEC_ALLOC | SEC_LINKER_CREATED);
    435       if (s == NULL)
    436 	return FALSE;
    437       htab->sdynbss = s;
    438 
    439       if (bed->want_dynrelro)
    440 	{
    441 	  /* Similarly, but for symbols that were originally in read-only
    442 	     sections.  This section doesn't really need to have contents,
    443 	     but make it like other .data.rel.ro sections.  */
    444 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
    445 						  flags);
    446 	  if (s == NULL)
    447 	    return FALSE;
    448 	  htab->sdynrelro = s;
    449 	}
    450 
    451       /* The .rel[a].bss section holds copy relocs.  This section is not
    452 	 normally needed.  We need to create it here, though, so that the
    453 	 linker will map it to an output section.  We can't just create it
    454 	 only if we need it, because we will not know whether we need it
    455 	 until we have seen all the input files, and the first time the
    456 	 main linker code calls BFD after examining all the input files
    457 	 (size_dynamic_sections) the input sections have already been
    458 	 mapped to the output sections.  If the section turns out not to
    459 	 be needed, we can discard it later.  We will never need this
    460 	 section when generating a shared object, since they do not use
    461 	 copy relocs.  */
    462       if (bfd_link_executable (info))
    463 	{
    464 	  s = bfd_make_section_anyway_with_flags (abfd,
    465 						  (bed->rela_plts_and_copies_p
    466 						   ? ".rela.bss" : ".rel.bss"),
    467 						  flags | SEC_READONLY);
    468 	  if (s == NULL
    469 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
    470 	    return FALSE;
    471 	  htab->srelbss = s;
    472 
    473 	  if (bed->want_dynrelro)
    474 	    {
    475 	      s = (bfd_make_section_anyway_with_flags
    476 		   (abfd, (bed->rela_plts_and_copies_p
    477 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
    478 		    flags | SEC_READONLY));
    479 	      if (s == NULL
    480 		  || !bfd_set_section_alignment (s, bed->s->log_file_align))
    481 		return FALSE;
    482 	      htab->sreldynrelro = s;
    483 	    }
    484 	}
    485     }
    486 
    487   return TRUE;
    488 }
    489 
    490 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    492    read the input files, since we need to have a list of all of them
    493    before we can determine the final sizes of the output sections.
    494    Note that we may actually call this function even though we are not
    495    going to output any dynamic symbols; in some cases we know that a
    496    symbol should be in the dynamic symbol table, but only if there is
    497    one.  */
    498 
    499 bfd_boolean
    500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    501 				    struct elf_link_hash_entry *h)
    502 {
    503   if (h->dynindx == -1)
    504     {
    505       struct elf_strtab_hash *dynstr;
    506       char *p;
    507       const char *name;
    508       size_t indx;
    509 
    510       /* XXX: The ABI draft says the linker must turn hidden and
    511 	 internal symbols into STB_LOCAL symbols when producing the
    512 	 DSO. However, if ld.so honors st_other in the dynamic table,
    513 	 this would not be necessary.  */
    514       switch (ELF_ST_VISIBILITY (h->other))
    515 	{
    516 	case STV_INTERNAL:
    517 	case STV_HIDDEN:
    518 	  if (h->root.type != bfd_link_hash_undefined
    519 	      && h->root.type != bfd_link_hash_undefweak)
    520 	    {
    521 	      h->forced_local = 1;
    522 	      if (!elf_hash_table (info)->is_relocatable_executable)
    523 		return TRUE;
    524 	    }
    525 
    526 	default:
    527 	  break;
    528 	}
    529 
    530       h->dynindx = elf_hash_table (info)->dynsymcount;
    531       ++elf_hash_table (info)->dynsymcount;
    532 
    533       dynstr = elf_hash_table (info)->dynstr;
    534       if (dynstr == NULL)
    535 	{
    536 	  /* Create a strtab to hold the dynamic symbol names.  */
    537 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    538 	  if (dynstr == NULL)
    539 	    return FALSE;
    540 	}
    541 
    542       /* We don't put any version information in the dynamic string
    543 	 table.  */
    544       name = h->root.root.string;
    545       p = strchr (name, ELF_VER_CHR);
    546       if (p != NULL)
    547 	/* We know that the p points into writable memory.  In fact,
    548 	   there are only a few symbols that have read-only names, being
    549 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
    550 	   by the backends.  Most symbols will have names pointing into
    551 	   an ELF string table read from a file, or to objalloc memory.  */
    552 	*p = 0;
    553 
    554       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    555 
    556       if (p != NULL)
    557 	*p = ELF_VER_CHR;
    558 
    559       if (indx == (size_t) -1)
    560 	return FALSE;
    561       h->dynstr_index = indx;
    562     }
    563 
    564   return TRUE;
    565 }
    566 
    567 /* Mark a symbol dynamic.  */
    569 
    570 static void
    571 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    572 				  struct elf_link_hash_entry *h,
    573 				  Elf_Internal_Sym *sym)
    574 {
    575   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    576 
    577   /* It may be called more than once on the same H.  */
    578   if(h->dynamic || bfd_link_relocatable (info))
    579     return;
    580 
    581   if ((info->dynamic_data
    582        && (h->type == STT_OBJECT
    583 	   || h->type == STT_COMMON
    584 	   || (sym != NULL
    585 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
    586 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
    587       || (d != NULL
    588 	  && h->non_elf
    589 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    590     {
    591       h->dynamic = 1;
    592       /* NB: If a symbol is made dynamic by --dynamic-list, it has
    593 	 non-IR reference.  */
    594       h->root.non_ir_ref_dynamic = 1;
    595     }
    596 }
    597 
    598 /* Record an assignment to a symbol made by a linker script.  We need
    599    this in case some dynamic object refers to this symbol.  */
    600 
    601 bfd_boolean
    602 bfd_elf_record_link_assignment (bfd *output_bfd,
    603 				struct bfd_link_info *info,
    604 				const char *name,
    605 				bfd_boolean provide,
    606 				bfd_boolean hidden)
    607 {
    608   struct elf_link_hash_entry *h, *hv;
    609   struct elf_link_hash_table *htab;
    610   const struct elf_backend_data *bed;
    611 
    612   if (!is_elf_hash_table (info->hash))
    613     return TRUE;
    614 
    615   htab = elf_hash_table (info);
    616   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
    617   if (h == NULL)
    618     return provide;
    619 
    620   if (h->root.type == bfd_link_hash_warning)
    621     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    622 
    623   if (h->versioned == unknown)
    624     {
    625       /* Set versioned if symbol version is unknown.  */
    626       char *version = strrchr (name, ELF_VER_CHR);
    627       if (version)
    628 	{
    629 	  if (version > name && version[-1] != ELF_VER_CHR)
    630 	    h->versioned = versioned_hidden;
    631 	  else
    632 	    h->versioned = versioned;
    633 	}
    634     }
    635 
    636   /* Symbols defined in a linker script but not referenced anywhere
    637      else will have non_elf set.  */
    638   if (h->non_elf)
    639     {
    640       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    641       h->non_elf = 0;
    642     }
    643 
    644   switch (h->root.type)
    645     {
    646     case bfd_link_hash_defined:
    647     case bfd_link_hash_defweak:
    648     case bfd_link_hash_common:
    649       break;
    650     case bfd_link_hash_undefweak:
    651     case bfd_link_hash_undefined:
    652       /* Since we're defining the symbol, don't let it seem to have not
    653 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    654 	 may depend on this.  */
    655       h->root.type = bfd_link_hash_new;
    656       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    657 	bfd_link_repair_undef_list (&htab->root);
    658       break;
    659     case bfd_link_hash_new:
    660       break;
    661     case bfd_link_hash_indirect:
    662       /* We had a versioned symbol in a dynamic library.  We make the
    663 	 the versioned symbol point to this one.  */
    664       bed = get_elf_backend_data (output_bfd);
    665       hv = h;
    666       while (hv->root.type == bfd_link_hash_indirect
    667 	     || hv->root.type == bfd_link_hash_warning)
    668 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    669       /* We don't need to update h->root.u since linker will set them
    670 	 later.  */
    671       h->root.type = bfd_link_hash_undefined;
    672       hv->root.type = bfd_link_hash_indirect;
    673       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    674       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    675       break;
    676     default:
    677       BFD_FAIL ();
    678       return FALSE;
    679     }
    680 
    681   /* If this symbol is being provided by the linker script, and it is
    682      currently defined by a dynamic object, but not by a regular
    683      object, then mark it as undefined so that the generic linker will
    684      force the correct value.  */
    685   if (provide
    686       && h->def_dynamic
    687       && !h->def_regular)
    688     h->root.type = bfd_link_hash_undefined;
    689 
    690   /* If this symbol is currently defined by a dynamic object, but not
    691      by a regular object, then clear out any version information because
    692      the symbol will not be associated with the dynamic object any
    693      more.  */
    694   if (h->def_dynamic && !h->def_regular)
    695     h->verinfo.verdef = NULL;
    696 
    697   /* Make sure this symbol is not garbage collected.  */
    698   h->mark = 1;
    699 
    700   h->def_regular = 1;
    701 
    702   if (hidden)
    703     {
    704       bed = get_elf_backend_data (output_bfd);
    705       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    706 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    707       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    708     }
    709 
    710   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    711      and executables.  */
    712   if (!bfd_link_relocatable (info)
    713       && h->dynindx != -1
    714       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    715 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    716     h->forced_local = 1;
    717 
    718   if ((h->def_dynamic
    719        || h->ref_dynamic
    720        || bfd_link_dll (info)
    721        || elf_hash_table (info)->is_relocatable_executable)
    722       && !h->forced_local
    723       && h->dynindx == -1)
    724     {
    725       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    726 	return FALSE;
    727 
    728       /* If this is a weak defined symbol, and we know a corresponding
    729 	 real symbol from the same dynamic object, make sure the real
    730 	 symbol is also made into a dynamic symbol.  */
    731       if (h->is_weakalias)
    732 	{
    733 	  struct elf_link_hash_entry *def = weakdef (h);
    734 
    735 	  if (def->dynindx == -1
    736 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
    737 	    return FALSE;
    738 	}
    739     }
    740 
    741   return TRUE;
    742 }
    743 
    744 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    745    success, and 2 on a failure caused by attempting to record a symbol
    746    in a discarded section, eg. a discarded link-once section symbol.  */
    747 
    748 int
    749 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    750 					  bfd *input_bfd,
    751 					  long input_indx)
    752 {
    753   bfd_size_type amt;
    754   struct elf_link_local_dynamic_entry *entry;
    755   struct elf_link_hash_table *eht;
    756   struct elf_strtab_hash *dynstr;
    757   size_t dynstr_index;
    758   char *name;
    759   Elf_External_Sym_Shndx eshndx;
    760   char esym[sizeof (Elf64_External_Sym)];
    761 
    762   if (! is_elf_hash_table (info->hash))
    763     return 0;
    764 
    765   /* See if the entry exists already.  */
    766   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    767     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    768       return 1;
    769 
    770   amt = sizeof (*entry);
    771   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    772   if (entry == NULL)
    773     return 0;
    774 
    775   /* Go find the symbol, so that we can find it's name.  */
    776   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    777 			     1, input_indx, &entry->isym, esym, &eshndx))
    778     {
    779       bfd_release (input_bfd, entry);
    780       return 0;
    781     }
    782 
    783   if (entry->isym.st_shndx != SHN_UNDEF
    784       && entry->isym.st_shndx < SHN_LORESERVE)
    785     {
    786       asection *s;
    787 
    788       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    789       if (s == NULL || bfd_is_abs_section (s->output_section))
    790 	{
    791 	  /* We can still bfd_release here as nothing has done another
    792 	     bfd_alloc.  We can't do this later in this function.  */
    793 	  bfd_release (input_bfd, entry);
    794 	  return 2;
    795 	}
    796     }
    797 
    798   name = (bfd_elf_string_from_elf_section
    799 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    800 	   entry->isym.st_name));
    801 
    802   dynstr = elf_hash_table (info)->dynstr;
    803   if (dynstr == NULL)
    804     {
    805       /* Create a strtab to hold the dynamic symbol names.  */
    806       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    807       if (dynstr == NULL)
    808 	return 0;
    809     }
    810 
    811   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
    812   if (dynstr_index == (size_t) -1)
    813     return 0;
    814   entry->isym.st_name = dynstr_index;
    815 
    816   eht = elf_hash_table (info);
    817 
    818   entry->next = eht->dynlocal;
    819   eht->dynlocal = entry;
    820   entry->input_bfd = input_bfd;
    821   entry->input_indx = input_indx;
    822   eht->dynsymcount++;
    823 
    824   /* Whatever binding the symbol had before, it's now local.  */
    825   entry->isym.st_info
    826     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    827 
    828   /* The dynindx will be set at the end of size_dynamic_sections.  */
    829 
    830   return 1;
    831 }
    832 
    833 /* Return the dynindex of a local dynamic symbol.  */
    834 
    835 long
    836 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    837 				    bfd *input_bfd,
    838 				    long input_indx)
    839 {
    840   struct elf_link_local_dynamic_entry *e;
    841 
    842   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    843     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    844       return e->dynindx;
    845   return -1;
    846 }
    847 
    848 /* This function is used to renumber the dynamic symbols, if some of
    849    them are removed because they are marked as local.  This is called
    850    via elf_link_hash_traverse.  */
    851 
    852 static bfd_boolean
    853 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    854 				      void *data)
    855 {
    856   size_t *count = (size_t *) data;
    857 
    858   if (h->forced_local)
    859     return TRUE;
    860 
    861   if (h->dynindx != -1)
    862     h->dynindx = ++(*count);
    863 
    864   return TRUE;
    865 }
    866 
    867 
    868 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    869    STB_LOCAL binding.  */
    870 
    871 static bfd_boolean
    872 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    873 					    void *data)
    874 {
    875   size_t *count = (size_t *) data;
    876 
    877   if (!h->forced_local)
    878     return TRUE;
    879 
    880   if (h->dynindx != -1)
    881     h->dynindx = ++(*count);
    882 
    883   return TRUE;
    884 }
    885 
    886 /* Return true if the dynamic symbol for a given section should be
    887    omitted when creating a shared library.  */
    888 bfd_boolean
    889 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
    890 				      struct bfd_link_info *info,
    891 				      asection *p)
    892 {
    893   struct elf_link_hash_table *htab;
    894   asection *ip;
    895 
    896   switch (elf_section_data (p)->this_hdr.sh_type)
    897     {
    898     case SHT_PROGBITS:
    899     case SHT_NOBITS:
    900       /* If sh_type is yet undecided, assume it could be
    901 	 SHT_PROGBITS/SHT_NOBITS.  */
    902     case SHT_NULL:
    903       htab = elf_hash_table (info);
    904       if (htab->text_index_section != NULL)
    905 	return p != htab->text_index_section && p != htab->data_index_section;
    906 
    907       return (htab->dynobj != NULL
    908 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
    909 	      && ip->output_section == p);
    910 
    911       /* There shouldn't be section relative relocations
    912 	 against any other section.  */
    913     default:
    914       return TRUE;
    915     }
    916 }
    917 
    918 bfd_boolean
    919 _bfd_elf_omit_section_dynsym_all
    920     (bfd *output_bfd ATTRIBUTE_UNUSED,
    921      struct bfd_link_info *info ATTRIBUTE_UNUSED,
    922      asection *p ATTRIBUTE_UNUSED)
    923 {
    924   return TRUE;
    925 }
    926 
    927 /* Assign dynsym indices.  In a shared library we generate a section
    928    symbol for each output section, which come first.  Next come symbols
    929    which have been forced to local binding.  Then all of the back-end
    930    allocated local dynamic syms, followed by the rest of the global
    931    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
    932    (This prevents the early call before elf_backend_init_index_section
    933    and strip_excluded_output_sections setting dynindx for sections
    934    that are stripped.)  */
    935 
    936 static unsigned long
    937 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
    938 				struct bfd_link_info *info,
    939 				unsigned long *section_sym_count)
    940 {
    941   unsigned long dynsymcount = 0;
    942   bfd_boolean do_sec = section_sym_count != NULL;
    943 
    944   if (bfd_link_pic (info)
    945       || elf_hash_table (info)->is_relocatable_executable)
    946     {
    947       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
    948       asection *p;
    949       for (p = output_bfd->sections; p ; p = p->next)
    950 	if ((p->flags & SEC_EXCLUDE) == 0
    951 	    && (p->flags & SEC_ALLOC) != 0
    952 	    && elf_hash_table (info)->dynamic_relocs
    953 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
    954 	  {
    955 	    ++dynsymcount;
    956 	    if (do_sec)
    957 	      elf_section_data (p)->dynindx = dynsymcount;
    958 	  }
    959 	else if (do_sec)
    960 	  elf_section_data (p)->dynindx = 0;
    961     }
    962   if (do_sec)
    963     *section_sym_count = dynsymcount;
    964 
    965   elf_link_hash_traverse (elf_hash_table (info),
    966 			  elf_link_renumber_local_hash_table_dynsyms,
    967 			  &dynsymcount);
    968 
    969   if (elf_hash_table (info)->dynlocal)
    970     {
    971       struct elf_link_local_dynamic_entry *p;
    972       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
    973 	p->dynindx = ++dynsymcount;
    974     }
    975   elf_hash_table (info)->local_dynsymcount = dynsymcount;
    976 
    977   elf_link_hash_traverse (elf_hash_table (info),
    978 			  elf_link_renumber_hash_table_dynsyms,
    979 			  &dynsymcount);
    980 
    981   /* There is an unused NULL entry at the head of the table which we
    982      must account for in our count even if the table is empty since it
    983      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
    984      .dynamic section.  */
    985   dynsymcount++;
    986 
    987   elf_hash_table (info)->dynsymcount = dynsymcount;
    988   return dynsymcount;
    989 }
    990 
    991 /* Merge st_other field.  */
    992 
    993 static void
    994 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
    995 		    const Elf_Internal_Sym *isym, asection *sec,
    996 		    bfd_boolean definition, bfd_boolean dynamic)
    997 {
    998   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    999 
   1000   /* If st_other has a processor-specific meaning, specific
   1001      code might be needed here.  */
   1002   if (bed->elf_backend_merge_symbol_attribute)
   1003     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
   1004 						dynamic);
   1005 
   1006   if (!dynamic)
   1007     {
   1008       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
   1009       unsigned hvis = ELF_ST_VISIBILITY (h->other);
   1010 
   1011       /* Keep the most constraining visibility.  Leave the remainder
   1012 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
   1013       if (symvis - 1 < hvis - 1)
   1014 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
   1015     }
   1016   else if (definition
   1017 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
   1018 	   && (sec->flags & SEC_READONLY) == 0)
   1019     h->protected_def = 1;
   1020 }
   1021 
   1022 /* This function is called when we want to merge a new symbol with an
   1023    existing symbol.  It handles the various cases which arise when we
   1024    find a definition in a dynamic object, or when there is already a
   1025    definition in a dynamic object.  The new symbol is described by
   1026    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
   1027    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
   1028    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
   1029    of an old common symbol.  We set OVERRIDE if the old symbol is
   1030    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
   1031    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
   1032    to change.  By OK to change, we mean that we shouldn't warn if the
   1033    type or size does change.  */
   1034 
   1035 static bfd_boolean
   1036 _bfd_elf_merge_symbol (bfd *abfd,
   1037 		       struct bfd_link_info *info,
   1038 		       const char *name,
   1039 		       Elf_Internal_Sym *sym,
   1040 		       asection **psec,
   1041 		       bfd_vma *pvalue,
   1042 		       struct elf_link_hash_entry **sym_hash,
   1043 		       bfd **poldbfd,
   1044 		       bfd_boolean *pold_weak,
   1045 		       unsigned int *pold_alignment,
   1046 		       bfd_boolean *skip,
   1047 		       bfd_boolean *override,
   1048 		       bfd_boolean *type_change_ok,
   1049 		       bfd_boolean *size_change_ok,
   1050 		       bfd_boolean *matched)
   1051 {
   1052   asection *sec, *oldsec;
   1053   struct elf_link_hash_entry *h;
   1054   struct elf_link_hash_entry *hi;
   1055   struct elf_link_hash_entry *flip;
   1056   int bind;
   1057   bfd *oldbfd;
   1058   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   1059   bfd_boolean newweak, oldweak, newfunc, oldfunc;
   1060   const struct elf_backend_data *bed;
   1061   char *new_version;
   1062   bfd_boolean default_sym = *matched;
   1063 
   1064   *skip = FALSE;
   1065   *override = FALSE;
   1066 
   1067   sec = *psec;
   1068   bind = ELF_ST_BIND (sym->st_info);
   1069 
   1070   if (! bfd_is_und_section (sec))
   1071     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
   1072   else
   1073     h = ((struct elf_link_hash_entry *)
   1074 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
   1075   if (h == NULL)
   1076     return FALSE;
   1077   *sym_hash = h;
   1078 
   1079   bed = get_elf_backend_data (abfd);
   1080 
   1081   /* NEW_VERSION is the symbol version of the new symbol.  */
   1082   if (h->versioned != unversioned)
   1083     {
   1084       /* Symbol version is unknown or versioned.  */
   1085       new_version = strrchr (name, ELF_VER_CHR);
   1086       if (new_version)
   1087 	{
   1088 	  if (h->versioned == unknown)
   1089 	    {
   1090 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1091 		h->versioned = versioned_hidden;
   1092 	      else
   1093 		h->versioned = versioned;
   1094 	    }
   1095 	  new_version += 1;
   1096 	  if (new_version[0] == '\0')
   1097 	    new_version = NULL;
   1098 	}
   1099       else
   1100 	h->versioned = unversioned;
   1101     }
   1102   else
   1103     new_version = NULL;
   1104 
   1105   /* For merging, we only care about real symbols.  But we need to make
   1106      sure that indirect symbol dynamic flags are updated.  */
   1107   hi = h;
   1108   while (h->root.type == bfd_link_hash_indirect
   1109 	 || h->root.type == bfd_link_hash_warning)
   1110     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1111 
   1112   if (!*matched)
   1113     {
   1114       if (hi == h || h->root.type == bfd_link_hash_new)
   1115 	*matched = TRUE;
   1116       else
   1117 	{
   1118 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1119 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1120 	     true if the new symbol is only visible to the symbol with
   1121 	     the same symbol version.  */
   1122 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
   1123 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
   1124 	  if (!old_hidden && !new_hidden)
   1125 	    /* The new symbol matches the existing symbol if both
   1126 	       aren't hidden.  */
   1127 	    *matched = TRUE;
   1128 	  else
   1129 	    {
   1130 	      /* OLD_VERSION is the symbol version of the existing
   1131 		 symbol. */
   1132 	      char *old_version;
   1133 
   1134 	      if (h->versioned >= versioned)
   1135 		old_version = strrchr (h->root.root.string,
   1136 				       ELF_VER_CHR) + 1;
   1137 	      else
   1138 		 old_version = NULL;
   1139 
   1140 	      /* The new symbol matches the existing symbol if they
   1141 		 have the same symbol version.  */
   1142 	      *matched = (old_version == new_version
   1143 			  || (old_version != NULL
   1144 			      && new_version != NULL
   1145 			      && strcmp (old_version, new_version) == 0));
   1146 	    }
   1147 	}
   1148     }
   1149 
   1150   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1151      existing symbol.  */
   1152 
   1153   oldbfd = NULL;
   1154   oldsec = NULL;
   1155   switch (h->root.type)
   1156     {
   1157     default:
   1158       break;
   1159 
   1160     case bfd_link_hash_undefined:
   1161     case bfd_link_hash_undefweak:
   1162       oldbfd = h->root.u.undef.abfd;
   1163       break;
   1164 
   1165     case bfd_link_hash_defined:
   1166     case bfd_link_hash_defweak:
   1167       oldbfd = h->root.u.def.section->owner;
   1168       oldsec = h->root.u.def.section;
   1169       break;
   1170 
   1171     case bfd_link_hash_common:
   1172       oldbfd = h->root.u.c.p->section->owner;
   1173       oldsec = h->root.u.c.p->section;
   1174       if (pold_alignment)
   1175 	*pold_alignment = h->root.u.c.p->alignment_power;
   1176       break;
   1177     }
   1178   if (poldbfd && *poldbfd == NULL)
   1179     *poldbfd = oldbfd;
   1180 
   1181   /* Differentiate strong and weak symbols.  */
   1182   newweak = bind == STB_WEAK;
   1183   oldweak = (h->root.type == bfd_link_hash_defweak
   1184 	     || h->root.type == bfd_link_hash_undefweak);
   1185   if (pold_weak)
   1186     *pold_weak = oldweak;
   1187 
   1188   /* We have to check it for every instance since the first few may be
   1189      references and not all compilers emit symbol type for undefined
   1190      symbols.  */
   1191   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1192 
   1193   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1194      respectively, is from a dynamic object.  */
   1195 
   1196   newdyn = (abfd->flags & DYNAMIC) != 0;
   1197 
   1198   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1199      syms and defined syms in dynamic libraries respectively.
   1200      ref_dynamic on the other hand can be set for a symbol defined in
   1201      a dynamic library, and def_dynamic may not be set;  When the
   1202      definition in a dynamic lib is overridden by a definition in the
   1203      executable use of the symbol in the dynamic lib becomes a
   1204      reference to the executable symbol.  */
   1205   if (newdyn)
   1206     {
   1207       if (bfd_is_und_section (sec))
   1208 	{
   1209 	  if (bind != STB_WEAK)
   1210 	    {
   1211 	      h->ref_dynamic_nonweak = 1;
   1212 	      hi->ref_dynamic_nonweak = 1;
   1213 	    }
   1214 	}
   1215       else
   1216 	{
   1217 	  /* Update the existing symbol only if they match. */
   1218 	  if (*matched)
   1219 	    h->dynamic_def = 1;
   1220 	  hi->dynamic_def = 1;
   1221 	}
   1222     }
   1223 
   1224   /* If we just created the symbol, mark it as being an ELF symbol.
   1225      Other than that, there is nothing to do--there is no merge issue
   1226      with a newly defined symbol--so we just return.  */
   1227 
   1228   if (h->root.type == bfd_link_hash_new)
   1229     {
   1230       h->non_elf = 0;
   1231       return TRUE;
   1232     }
   1233 
   1234   /* In cases involving weak versioned symbols, we may wind up trying
   1235      to merge a symbol with itself.  Catch that here, to avoid the
   1236      confusion that results if we try to override a symbol with
   1237      itself.  The additional tests catch cases like
   1238      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1239      dynamic object, which we do want to handle here.  */
   1240   if (abfd == oldbfd
   1241       && (newweak || oldweak)
   1242       && ((abfd->flags & DYNAMIC) == 0
   1243 	  || !h->def_regular))
   1244     return TRUE;
   1245 
   1246   olddyn = FALSE;
   1247   if (oldbfd != NULL)
   1248     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1249   else if (oldsec != NULL)
   1250     {
   1251       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1252 	 indices used by MIPS ELF.  */
   1253       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1254     }
   1255 
   1256   /* Handle a case where plugin_notice won't be called and thus won't
   1257      set the non_ir_ref flags on the first pass over symbols.  */
   1258   if (oldbfd != NULL
   1259       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
   1260       && newdyn != olddyn)
   1261     {
   1262       h->root.non_ir_ref_dynamic = TRUE;
   1263       hi->root.non_ir_ref_dynamic = TRUE;
   1264     }
   1265 
   1266   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1267      respectively, appear to be a definition rather than reference.  */
   1268 
   1269   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1270 
   1271   olddef = (h->root.type != bfd_link_hash_undefined
   1272 	    && h->root.type != bfd_link_hash_undefweak
   1273 	    && h->root.type != bfd_link_hash_common);
   1274 
   1275   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1276      respectively, appear to be a function.  */
   1277 
   1278   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1279 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1280 
   1281   oldfunc = (h->type != STT_NOTYPE
   1282 	     && bed->is_function_type (h->type));
   1283 
   1284   if (!(newfunc && oldfunc)
   1285       && ELF_ST_TYPE (sym->st_info) != h->type
   1286       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1287       && h->type != STT_NOTYPE
   1288       && (newdef || bfd_is_com_section (sec))
   1289       && (olddef || h->root.type == bfd_link_hash_common))
   1290     {
   1291       /* If creating a default indirect symbol ("foo" or "foo@") from
   1292 	 a dynamic versioned definition ("foo@@") skip doing so if
   1293 	 there is an existing regular definition with a different
   1294 	 type.  We don't want, for example, a "time" variable in the
   1295 	 executable overriding a "time" function in a shared library.  */
   1296       if (newdyn
   1297 	  && !olddyn)
   1298 	{
   1299 	  *skip = TRUE;
   1300 	  return TRUE;
   1301 	}
   1302 
   1303       /* When adding a symbol from a regular object file after we have
   1304 	 created indirect symbols, undo the indirection and any
   1305 	 dynamic state.  */
   1306       if (hi != h
   1307 	  && !newdyn
   1308 	  && olddyn)
   1309 	{
   1310 	  h = hi;
   1311 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1312 	  h->forced_local = 0;
   1313 	  h->ref_dynamic = 0;
   1314 	  h->def_dynamic = 0;
   1315 	  h->dynamic_def = 0;
   1316 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1317 	    {
   1318 	      h->root.type = bfd_link_hash_undefined;
   1319 	      h->root.u.undef.abfd = abfd;
   1320 	    }
   1321 	  else
   1322 	    {
   1323 	      h->root.type = bfd_link_hash_new;
   1324 	      h->root.u.undef.abfd = NULL;
   1325 	    }
   1326 	  return TRUE;
   1327 	}
   1328     }
   1329 
   1330   /* Check TLS symbols.  We don't check undefined symbols introduced
   1331      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1332      check symbols from plugins because they also have no type.  */
   1333   if (oldbfd != NULL
   1334       && (oldbfd->flags & BFD_PLUGIN) == 0
   1335       && (abfd->flags & BFD_PLUGIN) == 0
   1336       && ELF_ST_TYPE (sym->st_info) != h->type
   1337       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1338     {
   1339       bfd *ntbfd, *tbfd;
   1340       bfd_boolean ntdef, tdef;
   1341       asection *ntsec, *tsec;
   1342 
   1343       if (h->type == STT_TLS)
   1344 	{
   1345 	  ntbfd = abfd;
   1346 	  ntsec = sec;
   1347 	  ntdef = newdef;
   1348 	  tbfd = oldbfd;
   1349 	  tsec = oldsec;
   1350 	  tdef = olddef;
   1351 	}
   1352       else
   1353 	{
   1354 	  ntbfd = oldbfd;
   1355 	  ntsec = oldsec;
   1356 	  ntdef = olddef;
   1357 	  tbfd = abfd;
   1358 	  tsec = sec;
   1359 	  tdef = newdef;
   1360 	}
   1361 
   1362       if (tdef && ntdef)
   1363 	_bfd_error_handler
   1364 	  /* xgettext:c-format */
   1365 	  (_("%s: TLS definition in %pB section %pA "
   1366 	     "mismatches non-TLS definition in %pB section %pA"),
   1367 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
   1368       else if (!tdef && !ntdef)
   1369 	_bfd_error_handler
   1370 	  /* xgettext:c-format */
   1371 	  (_("%s: TLS reference in %pB "
   1372 	     "mismatches non-TLS reference in %pB"),
   1373 	   h->root.root.string, tbfd, ntbfd);
   1374       else if (tdef)
   1375 	_bfd_error_handler
   1376 	  /* xgettext:c-format */
   1377 	  (_("%s: TLS definition in %pB section %pA "
   1378 	     "mismatches non-TLS reference in %pB"),
   1379 	   h->root.root.string, tbfd, tsec, ntbfd);
   1380       else
   1381 	_bfd_error_handler
   1382 	  /* xgettext:c-format */
   1383 	  (_("%s: TLS reference in %pB "
   1384 	     "mismatches non-TLS definition in %pB section %pA"),
   1385 	   h->root.root.string, tbfd, ntbfd, ntsec);
   1386 
   1387       bfd_set_error (bfd_error_bad_value);
   1388       return FALSE;
   1389     }
   1390 
   1391   /* If the old symbol has non-default visibility, we ignore the new
   1392      definition from a dynamic object.  */
   1393   if (newdyn
   1394       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1395       && !bfd_is_und_section (sec))
   1396     {
   1397       *skip = TRUE;
   1398       /* Make sure this symbol is dynamic.  */
   1399       h->ref_dynamic = 1;
   1400       hi->ref_dynamic = 1;
   1401       /* A protected symbol has external availability. Make sure it is
   1402 	 recorded as dynamic.
   1403 
   1404 	 FIXME: Should we check type and size for protected symbol?  */
   1405       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1406 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1407       else
   1408 	return TRUE;
   1409     }
   1410   else if (!newdyn
   1411 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1412 	   && h->def_dynamic)
   1413     {
   1414       /* If the new symbol with non-default visibility comes from a
   1415 	 relocatable file and the old definition comes from a dynamic
   1416 	 object, we remove the old definition.  */
   1417       if (hi->root.type == bfd_link_hash_indirect)
   1418 	{
   1419 	  /* Handle the case where the old dynamic definition is
   1420 	     default versioned.  We need to copy the symbol info from
   1421 	     the symbol with default version to the normal one if it
   1422 	     was referenced before.  */
   1423 	  if (h->ref_regular)
   1424 	    {
   1425 	      hi->root.type = h->root.type;
   1426 	      h->root.type = bfd_link_hash_indirect;
   1427 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1428 
   1429 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1430 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1431 		{
   1432 		  /* If the new symbol is hidden or internal, completely undo
   1433 		     any dynamic link state.  */
   1434 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1435 		  h->forced_local = 0;
   1436 		  h->ref_dynamic = 0;
   1437 		}
   1438 	      else
   1439 		h->ref_dynamic = 1;
   1440 
   1441 	      h->def_dynamic = 0;
   1442 	      /* FIXME: Should we check type and size for protected symbol?  */
   1443 	      h->size = 0;
   1444 	      h->type = 0;
   1445 
   1446 	      h = hi;
   1447 	    }
   1448 	  else
   1449 	    h = hi;
   1450 	}
   1451 
   1452       /* If the old symbol was undefined before, then it will still be
   1453 	 on the undefs list.  If the new symbol is undefined or
   1454 	 common, we can't make it bfd_link_hash_new here, because new
   1455 	 undefined or common symbols will be added to the undefs list
   1456 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1457 	 added twice to the undefs list.  Also, if the new symbol is
   1458 	 undefweak then we don't want to lose the strong undef.  */
   1459       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1460 	{
   1461 	  h->root.type = bfd_link_hash_undefined;
   1462 	  h->root.u.undef.abfd = abfd;
   1463 	}
   1464       else
   1465 	{
   1466 	  h->root.type = bfd_link_hash_new;
   1467 	  h->root.u.undef.abfd = NULL;
   1468 	}
   1469 
   1470       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1471 	{
   1472 	  /* If the new symbol is hidden or internal, completely undo
   1473 	     any dynamic link state.  */
   1474 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1475 	  h->forced_local = 0;
   1476 	  h->ref_dynamic = 0;
   1477 	}
   1478       else
   1479 	h->ref_dynamic = 1;
   1480       h->def_dynamic = 0;
   1481       /* FIXME: Should we check type and size for protected symbol?  */
   1482       h->size = 0;
   1483       h->type = 0;
   1484       return TRUE;
   1485     }
   1486 
   1487   /* If a new weak symbol definition comes from a regular file and the
   1488      old symbol comes from a dynamic library, we treat the new one as
   1489      strong.  Similarly, an old weak symbol definition from a regular
   1490      file is treated as strong when the new symbol comes from a dynamic
   1491      library.  Further, an old weak symbol from a dynamic library is
   1492      treated as strong if the new symbol is from a dynamic library.
   1493      This reflects the way glibc's ld.so works.
   1494 
   1495      Also allow a weak symbol to override a linker script symbol
   1496      defined by an early pass over the script.  This is done so the
   1497      linker knows the symbol is defined in an object file, for the
   1498      DEFINED script function.
   1499 
   1500      Do this before setting *type_change_ok or *size_change_ok so that
   1501      we warn properly when dynamic library symbols are overridden.  */
   1502 
   1503   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
   1504     newweak = FALSE;
   1505   if (olddef && newdyn)
   1506     oldweak = FALSE;
   1507 
   1508   /* Allow changes between different types of function symbol.  */
   1509   if (newfunc && oldfunc)
   1510     *type_change_ok = TRUE;
   1511 
   1512   /* It's OK to change the type if either the existing symbol or the
   1513      new symbol is weak.  A type change is also OK if the old symbol
   1514      is undefined and the new symbol is defined.  */
   1515 
   1516   if (oldweak
   1517       || newweak
   1518       || (newdef
   1519 	  && h->root.type == bfd_link_hash_undefined))
   1520     *type_change_ok = TRUE;
   1521 
   1522   /* It's OK to change the size if either the existing symbol or the
   1523      new symbol is weak, or if the old symbol is undefined.  */
   1524 
   1525   if (*type_change_ok
   1526       || h->root.type == bfd_link_hash_undefined)
   1527     *size_change_ok = TRUE;
   1528 
   1529   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1530      symbol, respectively, appears to be a common symbol in a dynamic
   1531      object.  If a symbol appears in an uninitialized section, and is
   1532      not weak, and is not a function, then it may be a common symbol
   1533      which was resolved when the dynamic object was created.  We want
   1534      to treat such symbols specially, because they raise special
   1535      considerations when setting the symbol size: if the symbol
   1536      appears as a common symbol in a regular object, and the size in
   1537      the regular object is larger, we must make sure that we use the
   1538      larger size.  This problematic case can always be avoided in C,
   1539      but it must be handled correctly when using Fortran shared
   1540      libraries.
   1541 
   1542      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1543      likewise for OLDDYNCOMMON and OLDDEF.
   1544 
   1545      Note that this test is just a heuristic, and that it is quite
   1546      possible to have an uninitialized symbol in a shared object which
   1547      is really a definition, rather than a common symbol.  This could
   1548      lead to some minor confusion when the symbol really is a common
   1549      symbol in some regular object.  However, I think it will be
   1550      harmless.  */
   1551 
   1552   if (newdyn
   1553       && newdef
   1554       && !newweak
   1555       && (sec->flags & SEC_ALLOC) != 0
   1556       && (sec->flags & SEC_LOAD) == 0
   1557       && sym->st_size > 0
   1558       && !newfunc)
   1559     newdyncommon = TRUE;
   1560   else
   1561     newdyncommon = FALSE;
   1562 
   1563   if (olddyn
   1564       && olddef
   1565       && h->root.type == bfd_link_hash_defined
   1566       && h->def_dynamic
   1567       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1568       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1569       && h->size > 0
   1570       && !oldfunc)
   1571     olddyncommon = TRUE;
   1572   else
   1573     olddyncommon = FALSE;
   1574 
   1575   /* We now know everything about the old and new symbols.  We ask the
   1576      backend to check if we can merge them.  */
   1577   if (bed->merge_symbol != NULL)
   1578     {
   1579       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1580 	return FALSE;
   1581       sec = *psec;
   1582     }
   1583 
   1584   /* There are multiple definitions of a normal symbol.  Skip the
   1585      default symbol as well as definition from an IR object.  */
   1586   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
   1587       && !default_sym && h->def_regular
   1588       && !(oldbfd != NULL
   1589 	   && (oldbfd->flags & BFD_PLUGIN) != 0
   1590 	   && (abfd->flags & BFD_PLUGIN) == 0))
   1591     {
   1592       /* Handle a multiple definition.  */
   1593       (*info->callbacks->multiple_definition) (info, &h->root,
   1594 					       abfd, sec, *pvalue);
   1595       *skip = TRUE;
   1596       return TRUE;
   1597     }
   1598 
   1599   /* If both the old and the new symbols look like common symbols in a
   1600      dynamic object, set the size of the symbol to the larger of the
   1601      two.  */
   1602 
   1603   if (olddyncommon
   1604       && newdyncommon
   1605       && sym->st_size != h->size)
   1606     {
   1607       /* Since we think we have two common symbols, issue a multiple
   1608 	 common warning if desired.  Note that we only warn if the
   1609 	 size is different.  If the size is the same, we simply let
   1610 	 the old symbol override the new one as normally happens with
   1611 	 symbols defined in dynamic objects.  */
   1612 
   1613       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1614 					   bfd_link_hash_common, sym->st_size);
   1615       if (sym->st_size > h->size)
   1616 	h->size = sym->st_size;
   1617 
   1618       *size_change_ok = TRUE;
   1619     }
   1620 
   1621   /* If we are looking at a dynamic object, and we have found a
   1622      definition, we need to see if the symbol was already defined by
   1623      some other object.  If so, we want to use the existing
   1624      definition, and we do not want to report a multiple symbol
   1625      definition error; we do this by clobbering *PSEC to be
   1626      bfd_und_section_ptr.
   1627 
   1628      We treat a common symbol as a definition if the symbol in the
   1629      shared library is a function, since common symbols always
   1630      represent variables; this can cause confusion in principle, but
   1631      any such confusion would seem to indicate an erroneous program or
   1632      shared library.  We also permit a common symbol in a regular
   1633      object to override a weak symbol in a shared object.  */
   1634 
   1635   if (newdyn
   1636       && newdef
   1637       && (olddef
   1638 	  || (h->root.type == bfd_link_hash_common
   1639 	      && (newweak || newfunc))))
   1640     {
   1641       *override = TRUE;
   1642       newdef = FALSE;
   1643       newdyncommon = FALSE;
   1644 
   1645       *psec = sec = bfd_und_section_ptr;
   1646       *size_change_ok = TRUE;
   1647 
   1648       /* If we get here when the old symbol is a common symbol, then
   1649 	 we are explicitly letting it override a weak symbol or
   1650 	 function in a dynamic object, and we don't want to warn about
   1651 	 a type change.  If the old symbol is a defined symbol, a type
   1652 	 change warning may still be appropriate.  */
   1653 
   1654       if (h->root.type == bfd_link_hash_common)
   1655 	*type_change_ok = TRUE;
   1656     }
   1657 
   1658   /* Handle the special case of an old common symbol merging with a
   1659      new symbol which looks like a common symbol in a shared object.
   1660      We change *PSEC and *PVALUE to make the new symbol look like a
   1661      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1662      right thing.  */
   1663 
   1664   if (newdyncommon
   1665       && h->root.type == bfd_link_hash_common)
   1666     {
   1667       *override = TRUE;
   1668       newdef = FALSE;
   1669       newdyncommon = FALSE;
   1670       *pvalue = sym->st_size;
   1671       *psec = sec = bed->common_section (oldsec);
   1672       *size_change_ok = TRUE;
   1673     }
   1674 
   1675   /* Skip weak definitions of symbols that are already defined.  */
   1676   if (newdef && olddef && newweak)
   1677     {
   1678       /* Don't skip new non-IR weak syms.  */
   1679       if (!(oldbfd != NULL
   1680 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1681 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1682 	{
   1683 	  newdef = FALSE;
   1684 	  *skip = TRUE;
   1685 	}
   1686 
   1687       /* Merge st_other.  If the symbol already has a dynamic index,
   1688 	 but visibility says it should not be visible, turn it into a
   1689 	 local symbol.  */
   1690       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
   1691       if (h->dynindx != -1)
   1692 	switch (ELF_ST_VISIBILITY (h->other))
   1693 	  {
   1694 	  case STV_INTERNAL:
   1695 	  case STV_HIDDEN:
   1696 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1697 	    break;
   1698 	  }
   1699     }
   1700 
   1701   /* If the old symbol is from a dynamic object, and the new symbol is
   1702      a definition which is not from a dynamic object, then the new
   1703      symbol overrides the old symbol.  Symbols from regular files
   1704      always take precedence over symbols from dynamic objects, even if
   1705      they are defined after the dynamic object in the link.
   1706 
   1707      As above, we again permit a common symbol in a regular object to
   1708      override a definition in a shared object if the shared object
   1709      symbol is a function or is weak.  */
   1710 
   1711   flip = NULL;
   1712   if (!newdyn
   1713       && (newdef
   1714 	  || (bfd_is_com_section (sec)
   1715 	      && (oldweak || oldfunc)))
   1716       && olddyn
   1717       && olddef
   1718       && h->def_dynamic)
   1719     {
   1720       /* Change the hash table entry to undefined, and let
   1721 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1722 	 new definition.  */
   1723 
   1724       h->root.type = bfd_link_hash_undefined;
   1725       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1726       *size_change_ok = TRUE;
   1727 
   1728       olddef = FALSE;
   1729       olddyncommon = FALSE;
   1730 
   1731       /* We again permit a type change when a common symbol may be
   1732 	 overriding a function.  */
   1733 
   1734       if (bfd_is_com_section (sec))
   1735 	{
   1736 	  if (oldfunc)
   1737 	    {
   1738 	      /* If a common symbol overrides a function, make sure
   1739 		 that it isn't defined dynamically nor has type
   1740 		 function.  */
   1741 	      h->def_dynamic = 0;
   1742 	      h->type = STT_NOTYPE;
   1743 	    }
   1744 	  *type_change_ok = TRUE;
   1745 	}
   1746 
   1747       if (hi->root.type == bfd_link_hash_indirect)
   1748 	flip = hi;
   1749       else
   1750 	/* This union may have been set to be non-NULL when this symbol
   1751 	   was seen in a dynamic object.  We must force the union to be
   1752 	   NULL, so that it is correct for a regular symbol.  */
   1753 	h->verinfo.vertree = NULL;
   1754     }
   1755 
   1756   /* Handle the special case of a new common symbol merging with an
   1757      old symbol that looks like it might be a common symbol defined in
   1758      a shared object.  Note that we have already handled the case in
   1759      which a new common symbol should simply override the definition
   1760      in the shared library.  */
   1761 
   1762   if (! newdyn
   1763       && bfd_is_com_section (sec)
   1764       && olddyncommon)
   1765     {
   1766       /* It would be best if we could set the hash table entry to a
   1767 	 common symbol, but we don't know what to use for the section
   1768 	 or the alignment.  */
   1769       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1770 					   bfd_link_hash_common, sym->st_size);
   1771 
   1772       /* If the presumed common symbol in the dynamic object is
   1773 	 larger, pretend that the new symbol has its size.  */
   1774 
   1775       if (h->size > *pvalue)
   1776 	*pvalue = h->size;
   1777 
   1778       /* We need to remember the alignment required by the symbol
   1779 	 in the dynamic object.  */
   1780       BFD_ASSERT (pold_alignment);
   1781       *pold_alignment = h->root.u.def.section->alignment_power;
   1782 
   1783       olddef = FALSE;
   1784       olddyncommon = FALSE;
   1785 
   1786       h->root.type = bfd_link_hash_undefined;
   1787       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1788 
   1789       *size_change_ok = TRUE;
   1790       *type_change_ok = TRUE;
   1791 
   1792       if (hi->root.type == bfd_link_hash_indirect)
   1793 	flip = hi;
   1794       else
   1795 	h->verinfo.vertree = NULL;
   1796     }
   1797 
   1798   if (flip != NULL)
   1799     {
   1800       /* Handle the case where we had a versioned symbol in a dynamic
   1801 	 library and now find a definition in a normal object.  In this
   1802 	 case, we make the versioned symbol point to the normal one.  */
   1803       flip->root.type = h->root.type;
   1804       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1805       h->root.type = bfd_link_hash_indirect;
   1806       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1807       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1808       if (h->def_dynamic)
   1809 	{
   1810 	  h->def_dynamic = 0;
   1811 	  flip->ref_dynamic = 1;
   1812 	}
   1813     }
   1814 
   1815   return TRUE;
   1816 }
   1817 
   1818 /* This function is called to create an indirect symbol from the
   1819    default for the symbol with the default version if needed. The
   1820    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1821    set DYNSYM if the new indirect symbol is dynamic.  */
   1822 
   1823 static bfd_boolean
   1824 _bfd_elf_add_default_symbol (bfd *abfd,
   1825 			     struct bfd_link_info *info,
   1826 			     struct elf_link_hash_entry *h,
   1827 			     const char *name,
   1828 			     Elf_Internal_Sym *sym,
   1829 			     asection *sec,
   1830 			     bfd_vma value,
   1831 			     bfd **poldbfd,
   1832 			     bfd_boolean *dynsym)
   1833 {
   1834   bfd_boolean type_change_ok;
   1835   bfd_boolean size_change_ok;
   1836   bfd_boolean skip;
   1837   char *shortname;
   1838   struct elf_link_hash_entry *hi;
   1839   struct bfd_link_hash_entry *bh;
   1840   const struct elf_backend_data *bed;
   1841   bfd_boolean collect;
   1842   bfd_boolean dynamic;
   1843   bfd_boolean override;
   1844   char *p;
   1845   size_t len, shortlen;
   1846   asection *tmp_sec;
   1847   bfd_boolean matched;
   1848 
   1849   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1850     return TRUE;
   1851 
   1852   /* If this symbol has a version, and it is the default version, we
   1853      create an indirect symbol from the default name to the fully
   1854      decorated name.  This will cause external references which do not
   1855      specify a version to be bound to this version of the symbol.  */
   1856   p = strchr (name, ELF_VER_CHR);
   1857   if (h->versioned == unknown)
   1858     {
   1859       if (p == NULL)
   1860 	{
   1861 	  h->versioned = unversioned;
   1862 	  return TRUE;
   1863 	}
   1864       else
   1865 	{
   1866 	  if (p[1] != ELF_VER_CHR)
   1867 	    {
   1868 	      h->versioned = versioned_hidden;
   1869 	      return TRUE;
   1870 	    }
   1871 	  else
   1872 	    h->versioned = versioned;
   1873 	}
   1874     }
   1875   else
   1876     {
   1877       /* PR ld/19073: We may see an unversioned definition after the
   1878 	 default version.  */
   1879       if (p == NULL)
   1880 	return TRUE;
   1881     }
   1882 
   1883   bed = get_elf_backend_data (abfd);
   1884   collect = bed->collect;
   1885   dynamic = (abfd->flags & DYNAMIC) != 0;
   1886 
   1887   shortlen = p - name;
   1888   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   1889   if (shortname == NULL)
   1890     return FALSE;
   1891   memcpy (shortname, name, shortlen);
   1892   shortname[shortlen] = '\0';
   1893 
   1894   /* We are going to create a new symbol.  Merge it with any existing
   1895      symbol with this name.  For the purposes of the merge, act as
   1896      though we were defining the symbol we just defined, although we
   1897      actually going to define an indirect symbol.  */
   1898   type_change_ok = FALSE;
   1899   size_change_ok = FALSE;
   1900   matched = TRUE;
   1901   tmp_sec = sec;
   1902   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1903 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1904 			      &type_change_ok, &size_change_ok, &matched))
   1905     return FALSE;
   1906 
   1907   if (skip)
   1908     goto nondefault;
   1909 
   1910   if (hi->def_regular || ELF_COMMON_DEF_P (hi))
   1911     {
   1912       /* If the undecorated symbol will have a version added by a
   1913 	 script different to H, then don't indirect to/from the
   1914 	 undecorated symbol.  This isn't ideal because we may not yet
   1915 	 have seen symbol versions, if given by a script on the
   1916 	 command line rather than via --version-script.  */
   1917       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   1918 	{
   1919 	  bfd_boolean hide;
   1920 
   1921 	  hi->verinfo.vertree
   1922 	    = bfd_find_version_for_sym (info->version_info,
   1923 					hi->root.root.string, &hide);
   1924 	  if (hi->verinfo.vertree != NULL && hide)
   1925 	    {
   1926 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   1927 	      goto nondefault;
   1928 	    }
   1929 	}
   1930       if (hi->verinfo.vertree != NULL
   1931 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   1932 	goto nondefault;
   1933     }
   1934 
   1935   if (! override)
   1936     {
   1937       /* Add the default symbol if not performing a relocatable link.  */
   1938       if (! bfd_link_relocatable (info))
   1939 	{
   1940 	  bh = &hi->root;
   1941 	  if (bh->type == bfd_link_hash_defined
   1942 	      && bh->u.def.section->owner != NULL
   1943 	      && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
   1944 	    {
   1945 	      /* Mark the previous definition from IR object as
   1946 		 undefined so that the generic linker will override
   1947 		 it.  */
   1948 	      bh->type = bfd_link_hash_undefined;
   1949 	      bh->u.undef.abfd = bh->u.def.section->owner;
   1950 	    }
   1951 	  if (! (_bfd_generic_link_add_one_symbol
   1952 		 (info, abfd, shortname, BSF_INDIRECT,
   1953 		  bfd_ind_section_ptr,
   1954 		  0, name, FALSE, collect, &bh)))
   1955 	    return FALSE;
   1956 	  hi = (struct elf_link_hash_entry *) bh;
   1957 	}
   1958     }
   1959   else
   1960     {
   1961       /* In this case the symbol named SHORTNAME is overriding the
   1962 	 indirect symbol we want to add.  We were planning on making
   1963 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   1964 	 is the name without a version.  NAME is the fully versioned
   1965 	 name, and it is the default version.
   1966 
   1967 	 Overriding means that we already saw a definition for the
   1968 	 symbol SHORTNAME in a regular object, and it is overriding
   1969 	 the symbol defined in the dynamic object.
   1970 
   1971 	 When this happens, we actually want to change NAME, the
   1972 	 symbol we just added, to refer to SHORTNAME.  This will cause
   1973 	 references to NAME in the shared object to become references
   1974 	 to SHORTNAME in the regular object.  This is what we expect
   1975 	 when we override a function in a shared object: that the
   1976 	 references in the shared object will be mapped to the
   1977 	 definition in the regular object.  */
   1978 
   1979       while (hi->root.type == bfd_link_hash_indirect
   1980 	     || hi->root.type == bfd_link_hash_warning)
   1981 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1982 
   1983       h->root.type = bfd_link_hash_indirect;
   1984       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1985       if (h->def_dynamic)
   1986 	{
   1987 	  h->def_dynamic = 0;
   1988 	  hi->ref_dynamic = 1;
   1989 	  if (hi->ref_regular
   1990 	      || hi->def_regular)
   1991 	    {
   1992 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   1993 		return FALSE;
   1994 	    }
   1995 	}
   1996 
   1997       /* Now set HI to H, so that the following code will set the
   1998 	 other fields correctly.  */
   1999       hi = h;
   2000     }
   2001 
   2002   /* Check if HI is a warning symbol.  */
   2003   if (hi->root.type == bfd_link_hash_warning)
   2004     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2005 
   2006   /* If there is a duplicate definition somewhere, then HI may not
   2007      point to an indirect symbol.  We will have reported an error to
   2008      the user in that case.  */
   2009 
   2010   if (hi->root.type == bfd_link_hash_indirect)
   2011     {
   2012       struct elf_link_hash_entry *ht;
   2013 
   2014       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   2015       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   2016 
   2017       /* A reference to the SHORTNAME symbol from a dynamic library
   2018 	 will be satisfied by the versioned symbol at runtime.  In
   2019 	 effect, we have a reference to the versioned symbol.  */
   2020       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2021       hi->dynamic_def |= ht->dynamic_def;
   2022 
   2023       /* See if the new flags lead us to realize that the symbol must
   2024 	 be dynamic.  */
   2025       if (! *dynsym)
   2026 	{
   2027 	  if (! dynamic)
   2028 	    {
   2029 	      if (! bfd_link_executable (info)
   2030 		  || hi->def_dynamic
   2031 		  || hi->ref_dynamic)
   2032 		*dynsym = TRUE;
   2033 	    }
   2034 	  else
   2035 	    {
   2036 	      if (hi->ref_regular)
   2037 		*dynsym = TRUE;
   2038 	    }
   2039 	}
   2040     }
   2041 
   2042   /* We also need to define an indirection from the nondefault version
   2043      of the symbol.  */
   2044 
   2045 nondefault:
   2046   len = strlen (name);
   2047   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   2048   if (shortname == NULL)
   2049     return FALSE;
   2050   memcpy (shortname, name, shortlen);
   2051   memcpy (shortname + shortlen, p + 1, len - shortlen);
   2052 
   2053   /* Once again, merge with any existing symbol.  */
   2054   type_change_ok = FALSE;
   2055   size_change_ok = FALSE;
   2056   tmp_sec = sec;
   2057   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2058 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2059 			      &type_change_ok, &size_change_ok, &matched))
   2060     return FALSE;
   2061 
   2062   if (skip)
   2063     return TRUE;
   2064 
   2065   if (override)
   2066     {
   2067       /* Here SHORTNAME is a versioned name, so we don't expect to see
   2068 	 the type of override we do in the case above unless it is
   2069 	 overridden by a versioned definition.  */
   2070       if (hi->root.type != bfd_link_hash_defined
   2071 	  && hi->root.type != bfd_link_hash_defweak)
   2072 	_bfd_error_handler
   2073 	  /* xgettext:c-format */
   2074 	  (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
   2075 	   abfd, shortname);
   2076     }
   2077   else
   2078     {
   2079       bh = &hi->root;
   2080       if (! (_bfd_generic_link_add_one_symbol
   2081 	     (info, abfd, shortname, BSF_INDIRECT,
   2082 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
   2083 	return FALSE;
   2084       hi = (struct elf_link_hash_entry *) bh;
   2085 
   2086       /* If there is a duplicate definition somewhere, then HI may not
   2087 	 point to an indirect symbol.  We will have reported an error
   2088 	 to the user in that case.  */
   2089 
   2090       if (hi->root.type == bfd_link_hash_indirect)
   2091 	{
   2092 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   2093 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2094 	  hi->dynamic_def |= h->dynamic_def;
   2095 
   2096 	  /* See if the new flags lead us to realize that the symbol
   2097 	     must be dynamic.  */
   2098 	  if (! *dynsym)
   2099 	    {
   2100 	      if (! dynamic)
   2101 		{
   2102 		  if (! bfd_link_executable (info)
   2103 		      || hi->ref_dynamic)
   2104 		    *dynsym = TRUE;
   2105 		}
   2106 	      else
   2107 		{
   2108 		  if (hi->ref_regular)
   2109 		    *dynsym = TRUE;
   2110 		}
   2111 	    }
   2112 	}
   2113     }
   2114 
   2115   return TRUE;
   2116 }
   2117 
   2118 /* This routine is used to export all defined symbols into the dynamic
   2120    symbol table.  It is called via elf_link_hash_traverse.  */
   2121 
   2122 static bfd_boolean
   2123 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   2124 {
   2125   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2126 
   2127   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2128   if (h->root.type == bfd_link_hash_indirect)
   2129     return TRUE;
   2130 
   2131   /* Ignore this if we won't export it.  */
   2132   if (!eif->info->export_dynamic && !h->dynamic)
   2133     return TRUE;
   2134 
   2135   if (h->dynindx == -1
   2136       && (h->def_regular || h->ref_regular)
   2137       && ! bfd_hide_sym_by_version (eif->info->version_info,
   2138 				    h->root.root.string))
   2139     {
   2140       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2141 	{
   2142 	  eif->failed = TRUE;
   2143 	  return FALSE;
   2144 	}
   2145     }
   2146 
   2147   return TRUE;
   2148 }
   2149 
   2150 /* Look through the symbols which are defined in other shared
   2152    libraries and referenced here.  Update the list of version
   2153    dependencies.  This will be put into the .gnu.version_r section.
   2154    This function is called via elf_link_hash_traverse.  */
   2155 
   2156 static bfd_boolean
   2157 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2158 					 void *data)
   2159 {
   2160   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2161   Elf_Internal_Verneed *t;
   2162   Elf_Internal_Vernaux *a;
   2163   bfd_size_type amt;
   2164 
   2165   /* We only care about symbols defined in shared objects with version
   2166      information.  */
   2167   if (!h->def_dynamic
   2168       || h->def_regular
   2169       || h->dynindx == -1
   2170       || h->verinfo.verdef == NULL
   2171       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2172 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2173     return TRUE;
   2174 
   2175   /* See if we already know about this version.  */
   2176   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2177        t != NULL;
   2178        t = t->vn_nextref)
   2179     {
   2180       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2181 	continue;
   2182 
   2183       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2184 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2185 	  return TRUE;
   2186 
   2187       break;
   2188     }
   2189 
   2190   /* This is a new version.  Add it to tree we are building.  */
   2191 
   2192   if (t == NULL)
   2193     {
   2194       amt = sizeof *t;
   2195       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2196       if (t == NULL)
   2197 	{
   2198 	  rinfo->failed = TRUE;
   2199 	  return FALSE;
   2200 	}
   2201 
   2202       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2203       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2204       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2205     }
   2206 
   2207   amt = sizeof *a;
   2208   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2209   if (a == NULL)
   2210     {
   2211       rinfo->failed = TRUE;
   2212       return FALSE;
   2213     }
   2214 
   2215   /* Note that we are copying a string pointer here, and testing it
   2216      above.  If bfd_elf_string_from_elf_section is ever changed to
   2217      discard the string data when low in memory, this will have to be
   2218      fixed.  */
   2219   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2220 
   2221   a->vna_flags = h->verinfo.verdef->vd_flags;
   2222   a->vna_nextptr = t->vn_auxptr;
   2223 
   2224   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2225   ++rinfo->vers;
   2226 
   2227   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2228 
   2229   t->vn_auxptr = a;
   2230 
   2231   return TRUE;
   2232 }
   2233 
   2234 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
   2235    hidden.  Set *T_P to NULL if there is no match.  */
   2236 
   2237 static bfd_boolean
   2238 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
   2239 				     struct elf_link_hash_entry *h,
   2240 				     const char *version_p,
   2241 				     struct bfd_elf_version_tree **t_p,
   2242 				     bfd_boolean *hide)
   2243 {
   2244   struct bfd_elf_version_tree *t;
   2245 
   2246   /* Look for the version.  If we find it, it is no longer weak.  */
   2247   for (t = info->version_info; t != NULL; t = t->next)
   2248     {
   2249       if (strcmp (t->name, version_p) == 0)
   2250 	{
   2251 	  size_t len;
   2252 	  char *alc;
   2253 	  struct bfd_elf_version_expr *d;
   2254 
   2255 	  len = version_p - h->root.root.string;
   2256 	  alc = (char *) bfd_malloc (len);
   2257 	  if (alc == NULL)
   2258 	    return FALSE;
   2259 	  memcpy (alc, h->root.root.string, len - 1);
   2260 	  alc[len - 1] = '\0';
   2261 	  if (alc[len - 2] == ELF_VER_CHR)
   2262 	    alc[len - 2] = '\0';
   2263 
   2264 	  h->verinfo.vertree = t;
   2265 	  t->used = TRUE;
   2266 	  d = NULL;
   2267 
   2268 	  if (t->globals.list != NULL)
   2269 	    d = (*t->match) (&t->globals, NULL, alc);
   2270 
   2271 	  /* See if there is anything to force this symbol to
   2272 	     local scope.  */
   2273 	  if (d == NULL && t->locals.list != NULL)
   2274 	    {
   2275 	      d = (*t->match) (&t->locals, NULL, alc);
   2276 	      if (d != NULL
   2277 		  && h->dynindx != -1
   2278 		  && ! info->export_dynamic)
   2279 		*hide = TRUE;
   2280 	    }
   2281 
   2282 	  free (alc);
   2283 	  break;
   2284 	}
   2285     }
   2286 
   2287   *t_p = t;
   2288 
   2289   return TRUE;
   2290 }
   2291 
   2292 /* Return TRUE if the symbol H is hidden by version script.  */
   2293 
   2294 bfd_boolean
   2295 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
   2296 				   struct elf_link_hash_entry *h)
   2297 {
   2298   const char *p;
   2299   bfd_boolean hide = FALSE;
   2300   const struct elf_backend_data *bed
   2301     = get_elf_backend_data (info->output_bfd);
   2302 
   2303   /* Version script only hides symbols defined in regular objects.  */
   2304   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2305     return TRUE;
   2306 
   2307   p = strchr (h->root.root.string, ELF_VER_CHR);
   2308   if (p != NULL && h->verinfo.vertree == NULL)
   2309     {
   2310       struct bfd_elf_version_tree *t;
   2311 
   2312       ++p;
   2313       if (*p == ELF_VER_CHR)
   2314 	++p;
   2315 
   2316       if (*p != '\0'
   2317 	  && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
   2318 	  && hide)
   2319 	{
   2320 	  if (hide)
   2321 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2322 	  return TRUE;
   2323 	}
   2324     }
   2325 
   2326   /* If we don't have a version for this symbol, see if we can find
   2327      something.  */
   2328   if (h->verinfo.vertree == NULL && info->version_info != NULL)
   2329     {
   2330       h->verinfo.vertree
   2331 	= bfd_find_version_for_sym (info->version_info,
   2332 				    h->root.root.string, &hide);
   2333       if (h->verinfo.vertree != NULL && hide)
   2334 	{
   2335 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2336 	  return TRUE;
   2337 	}
   2338     }
   2339 
   2340   return FALSE;
   2341 }
   2342 
   2343 /* Figure out appropriate versions for all the symbols.  We may not
   2344    have the version number script until we have read all of the input
   2345    files, so until that point we don't know which symbols should be
   2346    local.  This function is called via elf_link_hash_traverse.  */
   2347 
   2348 static bfd_boolean
   2349 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2350 {
   2351   struct elf_info_failed *sinfo;
   2352   struct bfd_link_info *info;
   2353   const struct elf_backend_data *bed;
   2354   struct elf_info_failed eif;
   2355   char *p;
   2356   bfd_boolean hide;
   2357 
   2358   sinfo = (struct elf_info_failed *) data;
   2359   info = sinfo->info;
   2360 
   2361   /* Fix the symbol flags.  */
   2362   eif.failed = FALSE;
   2363   eif.info = info;
   2364   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2365     {
   2366       if (eif.failed)
   2367 	sinfo->failed = TRUE;
   2368       return FALSE;
   2369     }
   2370 
   2371   bed = get_elf_backend_data (info->output_bfd);
   2372 
   2373   /* We only need version numbers for symbols defined in regular
   2374      objects.  */
   2375   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2376     {
   2377       /* Hide symbols defined in discarded input sections.  */
   2378       if ((h->root.type == bfd_link_hash_defined
   2379 	   || h->root.type == bfd_link_hash_defweak)
   2380 	  && discarded_section (h->root.u.def.section))
   2381 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2382       return TRUE;
   2383     }
   2384 
   2385   hide = FALSE;
   2386   p = strchr (h->root.root.string, ELF_VER_CHR);
   2387   if (p != NULL && h->verinfo.vertree == NULL)
   2388     {
   2389       struct bfd_elf_version_tree *t;
   2390 
   2391       ++p;
   2392       if (*p == ELF_VER_CHR)
   2393 	++p;
   2394 
   2395       /* If there is no version string, we can just return out.  */
   2396       if (*p == '\0')
   2397 	return TRUE;
   2398 
   2399       if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
   2400 	{
   2401 	  sinfo->failed = TRUE;
   2402 	  return FALSE;
   2403 	}
   2404 
   2405       if (hide)
   2406 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2407 
   2408       /* If we are building an application, we need to create a
   2409 	 version node for this version.  */
   2410       if (t == NULL && bfd_link_executable (info))
   2411 	{
   2412 	  struct bfd_elf_version_tree **pp;
   2413 	  int version_index;
   2414 
   2415 	  /* If we aren't going to export this symbol, we don't need
   2416 	     to worry about it.  */
   2417 	  if (h->dynindx == -1)
   2418 	    return TRUE;
   2419 
   2420 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
   2421 							  sizeof *t);
   2422 	  if (t == NULL)
   2423 	    {
   2424 	      sinfo->failed = TRUE;
   2425 	      return FALSE;
   2426 	    }
   2427 
   2428 	  t->name = p;
   2429 	  t->name_indx = (unsigned int) -1;
   2430 	  t->used = TRUE;
   2431 
   2432 	  version_index = 1;
   2433 	  /* Don't count anonymous version tag.  */
   2434 	  if (sinfo->info->version_info != NULL
   2435 	      && sinfo->info->version_info->vernum == 0)
   2436 	    version_index = 0;
   2437 	  for (pp = &sinfo->info->version_info;
   2438 	       *pp != NULL;
   2439 	       pp = &(*pp)->next)
   2440 	    ++version_index;
   2441 	  t->vernum = version_index;
   2442 
   2443 	  *pp = t;
   2444 
   2445 	  h->verinfo.vertree = t;
   2446 	}
   2447       else if (t == NULL)
   2448 	{
   2449 	  /* We could not find the version for a symbol when
   2450 	     generating a shared archive.  Return an error.  */
   2451 	  _bfd_error_handler
   2452 	    /* xgettext:c-format */
   2453 	    (_("%pB: version node not found for symbol %s"),
   2454 	     info->output_bfd, h->root.root.string);
   2455 	  bfd_set_error (bfd_error_bad_value);
   2456 	  sinfo->failed = TRUE;
   2457 	  return FALSE;
   2458 	}
   2459     }
   2460 
   2461   /* If we don't have a version for this symbol, see if we can find
   2462      something.  */
   2463   if (!hide
   2464       && h->verinfo.vertree == NULL
   2465       && sinfo->info->version_info != NULL)
   2466     {
   2467       h->verinfo.vertree
   2468 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2469 				    h->root.root.string, &hide);
   2470       if (h->verinfo.vertree != NULL && hide)
   2471 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2472     }
   2473 
   2474   return TRUE;
   2475 }
   2476 
   2477 /* Read and swap the relocs from the section indicated by SHDR.  This
   2479    may be either a REL or a RELA section.  The relocations are
   2480    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2481    which should have already been allocated to contain enough space.
   2482    The EXTERNAL_RELOCS are a buffer where the external form of the
   2483    relocations should be stored.
   2484 
   2485    Returns FALSE if something goes wrong.  */
   2486 
   2487 static bfd_boolean
   2488 elf_link_read_relocs_from_section (bfd *abfd,
   2489 				   asection *sec,
   2490 				   Elf_Internal_Shdr *shdr,
   2491 				   void *external_relocs,
   2492 				   Elf_Internal_Rela *internal_relocs)
   2493 {
   2494   const struct elf_backend_data *bed;
   2495   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2496   const bfd_byte *erela;
   2497   const bfd_byte *erelaend;
   2498   Elf_Internal_Rela *irela;
   2499   Elf_Internal_Shdr *symtab_hdr;
   2500   size_t nsyms;
   2501 
   2502   /* Position ourselves at the start of the section.  */
   2503   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2504     return FALSE;
   2505 
   2506   /* Read the relocations.  */
   2507   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
   2508     return FALSE;
   2509 
   2510   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2511   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2512 
   2513   bed = get_elf_backend_data (abfd);
   2514 
   2515   /* Convert the external relocations to the internal format.  */
   2516   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2517     swap_in = bed->s->swap_reloc_in;
   2518   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2519     swap_in = bed->s->swap_reloca_in;
   2520   else
   2521     {
   2522       bfd_set_error (bfd_error_wrong_format);
   2523       return FALSE;
   2524     }
   2525 
   2526   erela = (const bfd_byte *) external_relocs;
   2527   /* Setting erelaend like this and comparing with <= handles case of
   2528      a fuzzed object with sh_size not a multiple of sh_entsize.  */
   2529   erelaend = erela + shdr->sh_size - shdr->sh_entsize;
   2530   irela = internal_relocs;
   2531   while (erela <= erelaend)
   2532     {
   2533       bfd_vma r_symndx;
   2534 
   2535       (*swap_in) (abfd, erela, irela);
   2536       r_symndx = ELF32_R_SYM (irela->r_info);
   2537       if (bed->s->arch_size == 64)
   2538 	r_symndx >>= 24;
   2539       if (nsyms > 0)
   2540 	{
   2541 	  if ((size_t) r_symndx >= nsyms)
   2542 	    {
   2543 	      _bfd_error_handler
   2544 		/* xgettext:c-format */
   2545 		(_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
   2546 		   " for offset %#" PRIx64 " in section `%pA'"),
   2547 		 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
   2548 		 (uint64_t) irela->r_offset, sec);
   2549 	      bfd_set_error (bfd_error_bad_value);
   2550 	      return FALSE;
   2551 	    }
   2552 	}
   2553       else if (r_symndx != STN_UNDEF)
   2554 	{
   2555 	  _bfd_error_handler
   2556 	    /* xgettext:c-format */
   2557 	    (_("%pB: non-zero symbol index (%#" PRIx64 ")"
   2558 	       " for offset %#" PRIx64 " in section `%pA'"
   2559 	       " when the object file has no symbol table"),
   2560 	     abfd, (uint64_t) r_symndx,
   2561 	     (uint64_t) irela->r_offset, sec);
   2562 	  bfd_set_error (bfd_error_bad_value);
   2563 	  return FALSE;
   2564 	}
   2565       irela += bed->s->int_rels_per_ext_rel;
   2566       erela += shdr->sh_entsize;
   2567     }
   2568 
   2569   return TRUE;
   2570 }
   2571 
   2572 /* Read and swap the relocs for a section O.  They may have been
   2573    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2574    not NULL, they are used as buffers to read into.  They are known to
   2575    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2576    the return value is allocated using either malloc or bfd_alloc,
   2577    according to the KEEP_MEMORY argument.  If O has two relocation
   2578    sections (both REL and RELA relocations), then the REL_HDR
   2579    relocations will appear first in INTERNAL_RELOCS, followed by the
   2580    RELA_HDR relocations.  */
   2581 
   2582 Elf_Internal_Rela *
   2583 _bfd_elf_link_read_relocs (bfd *abfd,
   2584 			   asection *o,
   2585 			   void *external_relocs,
   2586 			   Elf_Internal_Rela *internal_relocs,
   2587 			   bfd_boolean keep_memory)
   2588 {
   2589   void *alloc1 = NULL;
   2590   Elf_Internal_Rela *alloc2 = NULL;
   2591   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2592   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2593   Elf_Internal_Rela *internal_rela_relocs;
   2594 
   2595   if (esdo->relocs != NULL)
   2596     return esdo->relocs;
   2597 
   2598   if (o->reloc_count == 0)
   2599     return NULL;
   2600 
   2601   if (internal_relocs == NULL)
   2602     {
   2603       bfd_size_type size;
   2604 
   2605       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
   2606       if (keep_memory)
   2607 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
   2608       else
   2609 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2610       if (internal_relocs == NULL)
   2611 	goto error_return;
   2612     }
   2613 
   2614   if (external_relocs == NULL)
   2615     {
   2616       bfd_size_type size = 0;
   2617 
   2618       if (esdo->rel.hdr)
   2619 	size += esdo->rel.hdr->sh_size;
   2620       if (esdo->rela.hdr)
   2621 	size += esdo->rela.hdr->sh_size;
   2622 
   2623       alloc1 = bfd_malloc (size);
   2624       if (alloc1 == NULL)
   2625 	goto error_return;
   2626       external_relocs = alloc1;
   2627     }
   2628 
   2629   internal_rela_relocs = internal_relocs;
   2630   if (esdo->rel.hdr)
   2631     {
   2632       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2633 					      external_relocs,
   2634 					      internal_relocs))
   2635 	goto error_return;
   2636       external_relocs = (((bfd_byte *) external_relocs)
   2637 			 + esdo->rel.hdr->sh_size);
   2638       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2639 			       * bed->s->int_rels_per_ext_rel);
   2640     }
   2641 
   2642   if (esdo->rela.hdr
   2643       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2644 					      external_relocs,
   2645 					      internal_rela_relocs)))
   2646     goto error_return;
   2647 
   2648   /* Cache the results for next time, if we can.  */
   2649   if (keep_memory)
   2650     esdo->relocs = internal_relocs;
   2651 
   2652   if (alloc1 != NULL)
   2653     free (alloc1);
   2654 
   2655   /* Don't free alloc2, since if it was allocated we are passing it
   2656      back (under the name of internal_relocs).  */
   2657 
   2658   return internal_relocs;
   2659 
   2660  error_return:
   2661   if (alloc1 != NULL)
   2662     free (alloc1);
   2663   if (alloc2 != NULL)
   2664     {
   2665       if (keep_memory)
   2666 	bfd_release (abfd, alloc2);
   2667       else
   2668 	free (alloc2);
   2669     }
   2670   return NULL;
   2671 }
   2672 
   2673 /* Compute the size of, and allocate space for, REL_HDR which is the
   2674    section header for a section containing relocations for O.  */
   2675 
   2676 static bfd_boolean
   2677 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2678 				  struct bfd_elf_section_reloc_data *reldata)
   2679 {
   2680   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2681 
   2682   /* That allows us to calculate the size of the section.  */
   2683   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2684 
   2685   /* The contents field must last into write_object_contents, so we
   2686      allocate it with bfd_alloc rather than malloc.  Also since we
   2687      cannot be sure that the contents will actually be filled in,
   2688      we zero the allocated space.  */
   2689   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2690   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2691     return FALSE;
   2692 
   2693   if (reldata->hashes == NULL && reldata->count)
   2694     {
   2695       struct elf_link_hash_entry **p;
   2696 
   2697       p = ((struct elf_link_hash_entry **)
   2698 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   2699       if (p == NULL)
   2700 	return FALSE;
   2701 
   2702       reldata->hashes = p;
   2703     }
   2704 
   2705   return TRUE;
   2706 }
   2707 
   2708 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2709    originated from the section given by INPUT_REL_HDR) to the
   2710    OUTPUT_BFD.  */
   2711 
   2712 bfd_boolean
   2713 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2714 			     asection *input_section,
   2715 			     Elf_Internal_Shdr *input_rel_hdr,
   2716 			     Elf_Internal_Rela *internal_relocs,
   2717 			     struct elf_link_hash_entry **rel_hash
   2718 			       ATTRIBUTE_UNUSED)
   2719 {
   2720   Elf_Internal_Rela *irela;
   2721   Elf_Internal_Rela *irelaend;
   2722   bfd_byte *erel;
   2723   struct bfd_elf_section_reloc_data *output_reldata;
   2724   asection *output_section;
   2725   const struct elf_backend_data *bed;
   2726   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   2727   struct bfd_elf_section_data *esdo;
   2728 
   2729   output_section = input_section->output_section;
   2730 
   2731   bed = get_elf_backend_data (output_bfd);
   2732   esdo = elf_section_data (output_section);
   2733   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2734     {
   2735       output_reldata = &esdo->rel;
   2736       swap_out = bed->s->swap_reloc_out;
   2737     }
   2738   else if (esdo->rela.hdr
   2739 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2740     {
   2741       output_reldata = &esdo->rela;
   2742       swap_out = bed->s->swap_reloca_out;
   2743     }
   2744   else
   2745     {
   2746       _bfd_error_handler
   2747 	/* xgettext:c-format */
   2748 	(_("%pB: relocation size mismatch in %pB section %pA"),
   2749 	 output_bfd, input_section->owner, input_section);
   2750       bfd_set_error (bfd_error_wrong_format);
   2751       return FALSE;
   2752     }
   2753 
   2754   erel = output_reldata->hdr->contents;
   2755   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   2756   irela = internal_relocs;
   2757   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   2758 		      * bed->s->int_rels_per_ext_rel);
   2759   while (irela < irelaend)
   2760     {
   2761       (*swap_out) (output_bfd, irela, erel);
   2762       irela += bed->s->int_rels_per_ext_rel;
   2763       erel += input_rel_hdr->sh_entsize;
   2764     }
   2765 
   2766   /* Bump the counter, so that we know where to add the next set of
   2767      relocations.  */
   2768   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   2769 
   2770   return TRUE;
   2771 }
   2772 
   2773 /* Make weak undefined symbols in PIE dynamic.  */
   2775 
   2776 bfd_boolean
   2777 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   2778 				 struct elf_link_hash_entry *h)
   2779 {
   2780   if (bfd_link_pie (info)
   2781       && h->dynindx == -1
   2782       && h->root.type == bfd_link_hash_undefweak)
   2783     return bfd_elf_link_record_dynamic_symbol (info, h);
   2784 
   2785   return TRUE;
   2786 }
   2787 
   2788 /* Fix up the flags for a symbol.  This handles various cases which
   2789    can only be fixed after all the input files are seen.  This is
   2790    currently called by both adjust_dynamic_symbol and
   2791    assign_sym_version, which is unnecessary but perhaps more robust in
   2792    the face of future changes.  */
   2793 
   2794 static bfd_boolean
   2795 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   2796 			   struct elf_info_failed *eif)
   2797 {
   2798   const struct elf_backend_data *bed;
   2799 
   2800   /* If this symbol was mentioned in a non-ELF file, try to set
   2801      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   2802      permit a non-ELF file to correctly refer to a symbol defined in
   2803      an ELF dynamic object.  */
   2804   if (h->non_elf)
   2805     {
   2806       while (h->root.type == bfd_link_hash_indirect)
   2807 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2808 
   2809       if (h->root.type != bfd_link_hash_defined
   2810 	  && h->root.type != bfd_link_hash_defweak)
   2811 	{
   2812 	  h->ref_regular = 1;
   2813 	  h->ref_regular_nonweak = 1;
   2814 	}
   2815       else
   2816 	{
   2817 	  if (h->root.u.def.section->owner != NULL
   2818 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   2819 		  == bfd_target_elf_flavour))
   2820 	    {
   2821 	      h->ref_regular = 1;
   2822 	      h->ref_regular_nonweak = 1;
   2823 	    }
   2824 	  else
   2825 	    h->def_regular = 1;
   2826 	}
   2827 
   2828       if (h->dynindx == -1
   2829 	  && (h->def_dynamic
   2830 	      || h->ref_dynamic))
   2831 	{
   2832 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2833 	    {
   2834 	      eif->failed = TRUE;
   2835 	      return FALSE;
   2836 	    }
   2837 	}
   2838     }
   2839   else
   2840     {
   2841       /* Unfortunately, NON_ELF is only correct if the symbol
   2842 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   2843 	 was first seen in an ELF file, we're probably OK unless the
   2844 	 symbol was defined in a non-ELF file.  Catch that case here.
   2845 	 FIXME: We're still in trouble if the symbol was first seen in
   2846 	 a dynamic object, and then later in a non-ELF regular object.  */
   2847       if ((h->root.type == bfd_link_hash_defined
   2848 	   || h->root.type == bfd_link_hash_defweak)
   2849 	  && !h->def_regular
   2850 	  && (h->root.u.def.section->owner != NULL
   2851 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   2852 		 != bfd_target_elf_flavour)
   2853 	      : (bfd_is_abs_section (h->root.u.def.section)
   2854 		 && !h->def_dynamic)))
   2855 	h->def_regular = 1;
   2856     }
   2857 
   2858   /* Backend specific symbol fixup.  */
   2859   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   2860   if (bed->elf_backend_fixup_symbol
   2861       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   2862     return FALSE;
   2863 
   2864   /* If this is a final link, and the symbol was defined as a common
   2865      symbol in a regular object file, and there was no definition in
   2866      any dynamic object, then the linker will have allocated space for
   2867      the symbol in a common section but the DEF_REGULAR
   2868      flag will not have been set.  */
   2869   if (h->root.type == bfd_link_hash_defined
   2870       && !h->def_regular
   2871       && h->ref_regular
   2872       && !h->def_dynamic
   2873       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   2874     h->def_regular = 1;
   2875 
   2876   /* Symbols defined in discarded sections shouldn't be dynamic.  */
   2877   if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
   2878     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2879 
   2880   /* If a weak undefined symbol has non-default visibility, we also
   2881      hide it from the dynamic linker.  */
   2882   else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2883 	   && h->root.type == bfd_link_hash_undefweak)
   2884     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2885 
   2886   /* A hidden versioned symbol in executable should be forced local if
   2887      it is is locally defined, not referenced by shared library and not
   2888      exported.  */
   2889   else if (bfd_link_executable (eif->info)
   2890 	   && h->versioned == versioned_hidden
   2891 	   && !eif->info->export_dynamic
   2892 	   && !h->dynamic
   2893 	   && !h->ref_dynamic
   2894 	   && h->def_regular)
   2895     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2896 
   2897   /* If -Bsymbolic was used (which means to bind references to global
   2898      symbols to the definition within the shared object), and this
   2899      symbol was defined in a regular object, then it actually doesn't
   2900      need a PLT entry.  Likewise, if the symbol has non-default
   2901      visibility.  If the symbol has hidden or internal visibility, we
   2902      will force it local.  */
   2903   else if (h->needs_plt
   2904 	   && bfd_link_pic (eif->info)
   2905 	   && is_elf_hash_table (eif->info->hash)
   2906 	   && (SYMBOLIC_BIND (eif->info, h)
   2907 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2908 	   && h->def_regular)
   2909     {
   2910       bfd_boolean force_local;
   2911 
   2912       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   2913 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   2914       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   2915     }
   2916 
   2917   /* If this is a weak defined symbol in a dynamic object, and we know
   2918      the real definition in the dynamic object, copy interesting flags
   2919      over to the real definition.  */
   2920   if (h->is_weakalias)
   2921     {
   2922       struct elf_link_hash_entry *def = weakdef (h);
   2923 
   2924       /* If the real definition is defined by a regular object file,
   2925 	 don't do anything special.  See the longer description in
   2926 	 _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
   2927 	 bfd_link_hash_defined as it was when put on the alias list
   2928 	 then it must have originally been a versioned symbol (for
   2929 	 which a non-versioned indirect symbol is created) and later
   2930 	 a definition for the non-versioned symbol is found.  In that
   2931 	 case the indirection is flipped with the versioned symbol
   2932 	 becoming an indirect pointing at the non-versioned symbol.
   2933 	 Thus, not an alias any more.  */
   2934       if (def->def_regular
   2935 	  || def->root.type != bfd_link_hash_defined)
   2936 	{
   2937 	  h = def;
   2938 	  while ((h = h->u.alias) != def)
   2939 	    h->is_weakalias = 0;
   2940 	}
   2941       else
   2942 	{
   2943 	  while (h->root.type == bfd_link_hash_indirect)
   2944 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2945 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   2946 		      || h->root.type == bfd_link_hash_defweak);
   2947 	  BFD_ASSERT (def->def_dynamic);
   2948 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
   2949 	}
   2950     }
   2951 
   2952   return TRUE;
   2953 }
   2954 
   2955 /* Make the backend pick a good value for a dynamic symbol.  This is
   2956    called via elf_link_hash_traverse, and also calls itself
   2957    recursively.  */
   2958 
   2959 static bfd_boolean
   2960 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   2961 {
   2962   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2963   struct elf_link_hash_table *htab;
   2964   const struct elf_backend_data *bed;
   2965 
   2966   if (! is_elf_hash_table (eif->info->hash))
   2967     return FALSE;
   2968 
   2969   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2970   if (h->root.type == bfd_link_hash_indirect)
   2971     return TRUE;
   2972 
   2973   /* Fix the symbol flags.  */
   2974   if (! _bfd_elf_fix_symbol_flags (h, eif))
   2975     return FALSE;
   2976 
   2977   htab = elf_hash_table (eif->info);
   2978   bed = get_elf_backend_data (htab->dynobj);
   2979 
   2980   if (h->root.type == bfd_link_hash_undefweak)
   2981     {
   2982       if (eif->info->dynamic_undefined_weak == 0)
   2983 	(*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2984       else if (eif->info->dynamic_undefined_weak > 0
   2985 	       && h->ref_regular
   2986 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2987 	       && !bfd_hide_sym_by_version (eif->info->version_info,
   2988 					    h->root.root.string))
   2989 	{
   2990 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2991 	    {
   2992 	      eif->failed = TRUE;
   2993 	      return FALSE;
   2994 	    }
   2995 	}
   2996     }
   2997 
   2998   /* If this symbol does not require a PLT entry, and it is not
   2999      defined by a dynamic object, or is not referenced by a regular
   3000      object, ignore it.  We do have to handle a weak defined symbol,
   3001      even if no regular object refers to it, if we decided to add it
   3002      to the dynamic symbol table.  FIXME: Do we normally need to worry
   3003      about symbols which are defined by one dynamic object and
   3004      referenced by another one?  */
   3005   if (!h->needs_plt
   3006       && h->type != STT_GNU_IFUNC
   3007       && (h->def_regular
   3008 	  || !h->def_dynamic
   3009 	  || (!h->ref_regular
   3010 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
   3011     {
   3012       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   3013       return TRUE;
   3014     }
   3015 
   3016   /* If we've already adjusted this symbol, don't do it again.  This
   3017      can happen via a recursive call.  */
   3018   if (h->dynamic_adjusted)
   3019     return TRUE;
   3020 
   3021   /* Don't look at this symbol again.  Note that we must set this
   3022      after checking the above conditions, because we may look at a
   3023      symbol once, decide not to do anything, and then get called
   3024      recursively later after REF_REGULAR is set below.  */
   3025   h->dynamic_adjusted = 1;
   3026 
   3027   /* If this is a weak definition, and we know a real definition, and
   3028      the real symbol is not itself defined by a regular object file,
   3029      then get a good value for the real definition.  We handle the
   3030      real symbol first, for the convenience of the backend routine.
   3031 
   3032      Note that there is a confusing case here.  If the real definition
   3033      is defined by a regular object file, we don't get the real symbol
   3034      from the dynamic object, but we do get the weak symbol.  If the
   3035      processor backend uses a COPY reloc, then if some routine in the
   3036      dynamic object changes the real symbol, we will not see that
   3037      change in the corresponding weak symbol.  This is the way other
   3038      ELF linkers work as well, and seems to be a result of the shared
   3039      library model.
   3040 
   3041      I will clarify this issue.  Most SVR4 shared libraries define the
   3042      variable _timezone and define timezone as a weak synonym.  The
   3043      tzset call changes _timezone.  If you write
   3044        extern int timezone;
   3045        int _timezone = 5;
   3046        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   3047      you might expect that, since timezone is a synonym for _timezone,
   3048      the same number will print both times.  However, if the processor
   3049      backend uses a COPY reloc, then actually timezone will be copied
   3050      into your process image, and, since you define _timezone
   3051      yourself, _timezone will not.  Thus timezone and _timezone will
   3052      wind up at different memory locations.  The tzset call will set
   3053      _timezone, leaving timezone unchanged.  */
   3054 
   3055   if (h->is_weakalias)
   3056     {
   3057       struct elf_link_hash_entry *def = weakdef (h);
   3058 
   3059       /* If we get to this point, there is an implicit reference to
   3060 	 the alias by a regular object file via the weak symbol H.  */
   3061       def->ref_regular = 1;
   3062 
   3063       /* Ensure that the backend adjust_dynamic_symbol function sees
   3064 	 the strong alias before H by recursively calling ourselves.  */
   3065       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
   3066 	return FALSE;
   3067     }
   3068 
   3069   /* If a symbol has no type and no size and does not require a PLT
   3070      entry, then we are probably about to do the wrong thing here: we
   3071      are probably going to create a COPY reloc for an empty object.
   3072      This case can arise when a shared object is built with assembly
   3073      code, and the assembly code fails to set the symbol type.  */
   3074   if (h->size == 0
   3075       && h->type == STT_NOTYPE
   3076       && !h->needs_plt)
   3077     _bfd_error_handler
   3078       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   3079        h->root.root.string);
   3080 
   3081   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   3082     {
   3083       eif->failed = TRUE;
   3084       return FALSE;
   3085     }
   3086 
   3087   return TRUE;
   3088 }
   3089 
   3090 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   3091    DYNBSS.  */
   3092 
   3093 bfd_boolean
   3094 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   3095 			      struct elf_link_hash_entry *h,
   3096 			      asection *dynbss)
   3097 {
   3098   unsigned int power_of_two;
   3099   bfd_vma mask;
   3100   asection *sec = h->root.u.def.section;
   3101 
   3102   /* The section alignment of the definition is the maximum alignment
   3103      requirement of symbols defined in the section.  Since we don't
   3104      know the symbol alignment requirement, we start with the
   3105      maximum alignment and check low bits of the symbol address
   3106      for the minimum alignment.  */
   3107   power_of_two = bfd_section_alignment (sec);
   3108   mask = ((bfd_vma) 1 << power_of_two) - 1;
   3109   while ((h->root.u.def.value & mask) != 0)
   3110     {
   3111        mask >>= 1;
   3112        --power_of_two;
   3113     }
   3114 
   3115   if (power_of_two > bfd_section_alignment (dynbss))
   3116     {
   3117       /* Adjust the section alignment if needed.  */
   3118       if (!bfd_set_section_alignment (dynbss, power_of_two))
   3119 	return FALSE;
   3120     }
   3121 
   3122   /* We make sure that the symbol will be aligned properly.  */
   3123   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   3124 
   3125   /* Define the symbol as being at this point in DYNBSS.  */
   3126   h->root.u.def.section = dynbss;
   3127   h->root.u.def.value = dynbss->size;
   3128 
   3129   /* Increment the size of DYNBSS to make room for the symbol.  */
   3130   dynbss->size += h->size;
   3131 
   3132   /* No error if extern_protected_data is true.  */
   3133   if (h->protected_def
   3134       && (!info->extern_protected_data
   3135 	  || (info->extern_protected_data < 0
   3136 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   3137     info->callbacks->einfo
   3138       (_("%P: copy reloc against protected `%pT' is dangerous\n"),
   3139        h->root.root.string);
   3140 
   3141   return TRUE;
   3142 }
   3143 
   3144 /* Adjust all external symbols pointing into SEC_MERGE sections
   3145    to reflect the object merging within the sections.  */
   3146 
   3147 static bfd_boolean
   3148 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   3149 {
   3150   asection *sec;
   3151 
   3152   if ((h->root.type == bfd_link_hash_defined
   3153        || h->root.type == bfd_link_hash_defweak)
   3154       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   3155       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3156     {
   3157       bfd *output_bfd = (bfd *) data;
   3158 
   3159       h->root.u.def.value =
   3160 	_bfd_merged_section_offset (output_bfd,
   3161 				    &h->root.u.def.section,
   3162 				    elf_section_data (sec)->sec_info,
   3163 				    h->root.u.def.value);
   3164     }
   3165 
   3166   return TRUE;
   3167 }
   3168 
   3169 /* Returns false if the symbol referred to by H should be considered
   3170    to resolve local to the current module, and true if it should be
   3171    considered to bind dynamically.  */
   3172 
   3173 bfd_boolean
   3174 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   3175 			   struct bfd_link_info *info,
   3176 			   bfd_boolean not_local_protected)
   3177 {
   3178   bfd_boolean binding_stays_local_p;
   3179   const struct elf_backend_data *bed;
   3180   struct elf_link_hash_table *hash_table;
   3181 
   3182   if (h == NULL)
   3183     return FALSE;
   3184 
   3185   while (h->root.type == bfd_link_hash_indirect
   3186 	 || h->root.type == bfd_link_hash_warning)
   3187     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3188 
   3189   /* If it was forced local, then clearly it's not dynamic.  */
   3190   if (h->dynindx == -1)
   3191     return FALSE;
   3192   if (h->forced_local)
   3193     return FALSE;
   3194 
   3195   /* Identify the cases where name binding rules say that a
   3196      visible symbol resolves locally.  */
   3197   binding_stays_local_p = (bfd_link_executable (info)
   3198 			   || SYMBOLIC_BIND (info, h));
   3199 
   3200   switch (ELF_ST_VISIBILITY (h->other))
   3201     {
   3202     case STV_INTERNAL:
   3203     case STV_HIDDEN:
   3204       return FALSE;
   3205 
   3206     case STV_PROTECTED:
   3207       hash_table = elf_hash_table (info);
   3208       if (!is_elf_hash_table (hash_table))
   3209 	return FALSE;
   3210 
   3211       bed = get_elf_backend_data (hash_table->dynobj);
   3212 
   3213       /* Proper resolution for function pointer equality may require
   3214 	 that these symbols perhaps be resolved dynamically, even though
   3215 	 we should be resolving them to the current module.  */
   3216       if (!not_local_protected || !bed->is_function_type (h->type))
   3217 	binding_stays_local_p = TRUE;
   3218       break;
   3219 
   3220     default:
   3221       break;
   3222     }
   3223 
   3224   /* If it isn't defined locally, then clearly it's dynamic.  */
   3225   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   3226     return TRUE;
   3227 
   3228   /* Otherwise, the symbol is dynamic if binding rules don't tell
   3229      us that it remains local.  */
   3230   return !binding_stays_local_p;
   3231 }
   3232 
   3233 /* Return true if the symbol referred to by H should be considered
   3234    to resolve local to the current module, and false otherwise.  Differs
   3235    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   3236    undefined symbols.  The two functions are virtually identical except
   3237    for the place where dynindx == -1 is tested.  If that test is true,
   3238    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
   3239    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
   3240    defined symbols.
   3241    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   3242    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   3243    treatment of undefined weak symbols.  For those that do not make
   3244    undefined weak symbols dynamic, both functions may return false.  */
   3245 
   3246 bfd_boolean
   3247 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   3248 			      struct bfd_link_info *info,
   3249 			      bfd_boolean local_protected)
   3250 {
   3251   const struct elf_backend_data *bed;
   3252   struct elf_link_hash_table *hash_table;
   3253 
   3254   /* If it's a local sym, of course we resolve locally.  */
   3255   if (h == NULL)
   3256     return TRUE;
   3257 
   3258   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   3259   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   3260       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   3261     return TRUE;
   3262 
   3263   /* Forced local symbols resolve locally.  */
   3264   if (h->forced_local)
   3265     return TRUE;
   3266 
   3267   /* Common symbols that become definitions don't get the DEF_REGULAR
   3268      flag set, so test it first, and don't bail out.  */
   3269   if (ELF_COMMON_DEF_P (h))
   3270     /* Do nothing.  */;
   3271   /* If we don't have a definition in a regular file, then we can't
   3272      resolve locally.  The sym is either undefined or dynamic.  */
   3273   else if (!h->def_regular)
   3274     return FALSE;
   3275 
   3276   /* Non-dynamic symbols resolve locally.  */
   3277   if (h->dynindx == -1)
   3278     return TRUE;
   3279 
   3280   /* At this point, we know the symbol is defined and dynamic.  In an
   3281      executable it must resolve locally, likewise when building symbolic
   3282      shared libraries.  */
   3283   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3284     return TRUE;
   3285 
   3286   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3287      with default visibility might not resolve locally.  */
   3288   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3289     return FALSE;
   3290 
   3291   hash_table = elf_hash_table (info);
   3292   if (!is_elf_hash_table (hash_table))
   3293     return TRUE;
   3294 
   3295   bed = get_elf_backend_data (hash_table->dynobj);
   3296 
   3297   /* If extern_protected_data is false, STV_PROTECTED non-function
   3298      symbols are local.  */
   3299   if ((!info->extern_protected_data
   3300        || (info->extern_protected_data < 0
   3301 	   && !bed->extern_protected_data))
   3302       && !bed->is_function_type (h->type))
   3303     return TRUE;
   3304 
   3305   /* Function pointer equality tests may require that STV_PROTECTED
   3306      symbols be treated as dynamic symbols.  If the address of a
   3307      function not defined in an executable is set to that function's
   3308      plt entry in the executable, then the address of the function in
   3309      a shared library must also be the plt entry in the executable.  */
   3310   return local_protected;
   3311 }
   3312 
   3313 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3314    aligned.  Returns the first TLS output section.  */
   3315 
   3316 struct bfd_section *
   3317 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3318 {
   3319   struct bfd_section *sec, *tls;
   3320   unsigned int align = 0;
   3321 
   3322   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3323     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3324       break;
   3325   tls = sec;
   3326 
   3327   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3328     if (sec->alignment_power > align)
   3329       align = sec->alignment_power;
   3330 
   3331   elf_hash_table (info)->tls_sec = tls;
   3332 
   3333   /* Ensure the alignment of the first section is the largest alignment,
   3334      so that the tls segment starts aligned.  */
   3335   if (tls != NULL)
   3336     tls->alignment_power = align;
   3337 
   3338   return tls;
   3339 }
   3340 
   3341 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3342 static bfd_boolean
   3343 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3344 				  Elf_Internal_Sym *sym)
   3345 {
   3346   const struct elf_backend_data *bed;
   3347 
   3348   /* Local symbols do not count, but target specific ones might.  */
   3349   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3350       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3351     return FALSE;
   3352 
   3353   bed = get_elf_backend_data (abfd);
   3354   /* Function symbols do not count.  */
   3355   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3356     return FALSE;
   3357 
   3358   /* If the section is undefined, then so is the symbol.  */
   3359   if (sym->st_shndx == SHN_UNDEF)
   3360     return FALSE;
   3361 
   3362   /* If the symbol is defined in the common section, then
   3363      it is a common definition and so does not count.  */
   3364   if (bed->common_definition (sym))
   3365     return FALSE;
   3366 
   3367   /* If the symbol is in a target specific section then we
   3368      must rely upon the backend to tell us what it is.  */
   3369   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3370     /* FIXME - this function is not coded yet:
   3371 
   3372        return _bfd_is_global_symbol_definition (abfd, sym);
   3373 
   3374        Instead for now assume that the definition is not global,
   3375        Even if this is wrong, at least the linker will behave
   3376        in the same way that it used to do.  */
   3377     return FALSE;
   3378 
   3379   return TRUE;
   3380 }
   3381 
   3382 /* Search the symbol table of the archive element of the archive ABFD
   3383    whose archive map contains a mention of SYMDEF, and determine if
   3384    the symbol is defined in this element.  */
   3385 static bfd_boolean
   3386 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3387 {
   3388   Elf_Internal_Shdr * hdr;
   3389   size_t symcount;
   3390   size_t extsymcount;
   3391   size_t extsymoff;
   3392   Elf_Internal_Sym *isymbuf;
   3393   Elf_Internal_Sym *isym;
   3394   Elf_Internal_Sym *isymend;
   3395   bfd_boolean result;
   3396 
   3397   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   3398   if (abfd == NULL)
   3399     return FALSE;
   3400 
   3401   if (! bfd_check_format (abfd, bfd_object))
   3402     return FALSE;
   3403 
   3404   /* Select the appropriate symbol table.  If we don't know if the
   3405      object file is an IR object, give linker LTO plugin a chance to
   3406      get the correct symbol table.  */
   3407   if (abfd->plugin_format == bfd_plugin_yes
   3408 #if BFD_SUPPORTS_PLUGINS
   3409       || (abfd->plugin_format == bfd_plugin_unknown
   3410 	  && bfd_link_plugin_object_p (abfd))
   3411 #endif
   3412       )
   3413     {
   3414       /* Use the IR symbol table if the object has been claimed by
   3415 	 plugin.  */
   3416       abfd = abfd->plugin_dummy_bfd;
   3417       hdr = &elf_tdata (abfd)->symtab_hdr;
   3418     }
   3419   else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3420     hdr = &elf_tdata (abfd)->symtab_hdr;
   3421   else
   3422     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3423 
   3424   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3425 
   3426   /* The sh_info field of the symtab header tells us where the
   3427      external symbols start.  We don't care about the local symbols.  */
   3428   if (elf_bad_symtab (abfd))
   3429     {
   3430       extsymcount = symcount;
   3431       extsymoff = 0;
   3432     }
   3433   else
   3434     {
   3435       extsymcount = symcount - hdr->sh_info;
   3436       extsymoff = hdr->sh_info;
   3437     }
   3438 
   3439   if (extsymcount == 0)
   3440     return FALSE;
   3441 
   3442   /* Read in the symbol table.  */
   3443   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3444 				  NULL, NULL, NULL);
   3445   if (isymbuf == NULL)
   3446     return FALSE;
   3447 
   3448   /* Scan the symbol table looking for SYMDEF.  */
   3449   result = FALSE;
   3450   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3451     {
   3452       const char *name;
   3453 
   3454       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3455 					      isym->st_name);
   3456       if (name == NULL)
   3457 	break;
   3458 
   3459       if (strcmp (name, symdef->name) == 0)
   3460 	{
   3461 	  result = is_global_data_symbol_definition (abfd, isym);
   3462 	  break;
   3463 	}
   3464     }
   3465 
   3466   free (isymbuf);
   3467 
   3468   return result;
   3469 }
   3470 
   3471 /* Add an entry to the .dynamic table.  */
   3473 
   3474 bfd_boolean
   3475 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3476 			    bfd_vma tag,
   3477 			    bfd_vma val)
   3478 {
   3479   struct elf_link_hash_table *hash_table;
   3480   const struct elf_backend_data *bed;
   3481   asection *s;
   3482   bfd_size_type newsize;
   3483   bfd_byte *newcontents;
   3484   Elf_Internal_Dyn dyn;
   3485 
   3486   hash_table = elf_hash_table (info);
   3487   if (! is_elf_hash_table (hash_table))
   3488     return FALSE;
   3489 
   3490   if (tag == DT_RELA || tag == DT_REL)
   3491     hash_table->dynamic_relocs = TRUE;
   3492 
   3493   bed = get_elf_backend_data (hash_table->dynobj);
   3494   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3495   BFD_ASSERT (s != NULL);
   3496 
   3497   newsize = s->size + bed->s->sizeof_dyn;
   3498   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3499   if (newcontents == NULL)
   3500     return FALSE;
   3501 
   3502   dyn.d_tag = tag;
   3503   dyn.d_un.d_val = val;
   3504   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3505 
   3506   s->size = newsize;
   3507   s->contents = newcontents;
   3508 
   3509   return TRUE;
   3510 }
   3511 
   3512 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
   3513    otherwise just check whether one already exists.  Returns -1 on error,
   3514    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3515 
   3516 static int
   3517 elf_add_dt_needed_tag (bfd *abfd,
   3518 		       struct bfd_link_info *info,
   3519 		       const char *soname,
   3520 		       bfd_boolean do_it)
   3521 {
   3522   struct elf_link_hash_table *hash_table;
   3523   size_t strindex;
   3524 
   3525   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3526     return -1;
   3527 
   3528   hash_table = elf_hash_table (info);
   3529   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
   3530   if (strindex == (size_t) -1)
   3531     return -1;
   3532 
   3533   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3534     {
   3535       asection *sdyn;
   3536       const struct elf_backend_data *bed;
   3537       bfd_byte *extdyn;
   3538 
   3539       bed = get_elf_backend_data (hash_table->dynobj);
   3540       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3541       if (sdyn != NULL)
   3542 	for (extdyn = sdyn->contents;
   3543 	     extdyn < sdyn->contents + sdyn->size;
   3544 	     extdyn += bed->s->sizeof_dyn)
   3545 	  {
   3546 	    Elf_Internal_Dyn dyn;
   3547 
   3548 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3549 	    if (dyn.d_tag == DT_NEEDED
   3550 		&& dyn.d_un.d_val == strindex)
   3551 	      {
   3552 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3553 		return 1;
   3554 	      }
   3555 	  }
   3556     }
   3557 
   3558   if (do_it)
   3559     {
   3560       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3561 	return -1;
   3562 
   3563       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3564 	return -1;
   3565     }
   3566   else
   3567     /* We were just checking for existence of the tag.  */
   3568     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3569 
   3570   return 0;
   3571 }
   3572 
   3573 /* Return true if SONAME is on the needed list between NEEDED and STOP
   3574    (or the end of list if STOP is NULL), and needed by a library that
   3575    will be loaded.  */
   3576 
   3577 static bfd_boolean
   3578 on_needed_list (const char *soname,
   3579 		struct bfd_link_needed_list *needed,
   3580 		struct bfd_link_needed_list *stop)
   3581 {
   3582   struct bfd_link_needed_list *look;
   3583   for (look = needed; look != stop; look = look->next)
   3584     if (strcmp (soname, look->name) == 0
   3585 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
   3586 	    /* If needed by a library that itself is not directly
   3587 	       needed, recursively check whether that library is
   3588 	       indirectly needed.  Since we add DT_NEEDED entries to
   3589 	       the end of the list, library dependencies appear after
   3590 	       the library.  Therefore search prior to the current
   3591 	       LOOK, preventing possible infinite recursion.  */
   3592 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
   3593       return TRUE;
   3594 
   3595   return FALSE;
   3596 }
   3597 
   3598 /* Sort symbol by value, section, size, and type.  */
   3599 static int
   3600 elf_sort_symbol (const void *arg1, const void *arg2)
   3601 {
   3602   const struct elf_link_hash_entry *h1;
   3603   const struct elf_link_hash_entry *h2;
   3604   bfd_signed_vma vdiff;
   3605   int sdiff;
   3606   const char *n1;
   3607   const char *n2;
   3608 
   3609   h1 = *(const struct elf_link_hash_entry **) arg1;
   3610   h2 = *(const struct elf_link_hash_entry **) arg2;
   3611   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3612   if (vdiff != 0)
   3613     return vdiff > 0 ? 1 : -1;
   3614 
   3615   sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3616   if (sdiff != 0)
   3617     return sdiff;
   3618 
   3619   /* Sort so that sized symbols are selected over zero size symbols.  */
   3620   vdiff = h1->size - h2->size;
   3621   if (vdiff != 0)
   3622     return vdiff > 0 ? 1 : -1;
   3623 
   3624   /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
   3625   if (h1->type != h2->type)
   3626     return h1->type - h2->type;
   3627 
   3628   /* If symbols are properly sized and typed, and multiple strong
   3629      aliases are not defined in a shared library by the user we
   3630      shouldn't get here.  Unfortunately linker script symbols like
   3631      __bss_start sometimes match a user symbol defined at the start of
   3632      .bss without proper size and type.  We'd like to preference the
   3633      user symbol over reserved system symbols.  Sort on leading
   3634      underscores.  */
   3635   n1 = h1->root.root.string;
   3636   n2 = h2->root.root.string;
   3637   while (*n1 == *n2)
   3638     {
   3639       if (*n1 == 0)
   3640 	break;
   3641       ++n1;
   3642       ++n2;
   3643     }
   3644   if (*n1 == '_')
   3645     return -1;
   3646   if (*n2 == '_')
   3647     return 1;
   3648 
   3649   /* Final sort on name selects user symbols like '_u' over reserved
   3650      system symbols like '_Z' and also will avoid qsort instability.  */
   3651   return *n1 - *n2;
   3652 }
   3653 
   3654 /* This function is used to adjust offsets into .dynstr for
   3655    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   3656 
   3657 static bfd_boolean
   3658 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   3659 {
   3660   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   3661 
   3662   if (h->dynindx != -1)
   3663     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   3664   return TRUE;
   3665 }
   3666 
   3667 /* Assign string offsets in .dynstr, update all structures referencing
   3668    them.  */
   3669 
   3670 static bfd_boolean
   3671 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   3672 {
   3673   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   3674   struct elf_link_local_dynamic_entry *entry;
   3675   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   3676   bfd *dynobj = hash_table->dynobj;
   3677   asection *sdyn;
   3678   bfd_size_type size;
   3679   const struct elf_backend_data *bed;
   3680   bfd_byte *extdyn;
   3681 
   3682   _bfd_elf_strtab_finalize (dynstr);
   3683   size = _bfd_elf_strtab_size (dynstr);
   3684 
   3685   bed = get_elf_backend_data (dynobj);
   3686   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3687   BFD_ASSERT (sdyn != NULL);
   3688 
   3689   /* Update all .dynamic entries referencing .dynstr strings.  */
   3690   for (extdyn = sdyn->contents;
   3691        extdyn < sdyn->contents + sdyn->size;
   3692        extdyn += bed->s->sizeof_dyn)
   3693     {
   3694       Elf_Internal_Dyn dyn;
   3695 
   3696       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   3697       switch (dyn.d_tag)
   3698 	{
   3699 	case DT_STRSZ:
   3700 	  dyn.d_un.d_val = size;
   3701 	  break;
   3702 	case DT_NEEDED:
   3703 	case DT_SONAME:
   3704 	case DT_RPATH:
   3705 	case DT_RUNPATH:
   3706 	case DT_FILTER:
   3707 	case DT_AUXILIARY:
   3708 	case DT_AUDIT:
   3709 	case DT_DEPAUDIT:
   3710 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   3711 	  break;
   3712 	default:
   3713 	  continue;
   3714 	}
   3715       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   3716     }
   3717 
   3718   /* Now update local dynamic symbols.  */
   3719   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   3720     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   3721 						  entry->isym.st_name);
   3722 
   3723   /* And the rest of dynamic symbols.  */
   3724   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   3725 
   3726   /* Adjust version definitions.  */
   3727   if (elf_tdata (output_bfd)->cverdefs)
   3728     {
   3729       asection *s;
   3730       bfd_byte *p;
   3731       size_t i;
   3732       Elf_Internal_Verdef def;
   3733       Elf_Internal_Verdaux defaux;
   3734 
   3735       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   3736       p = s->contents;
   3737       do
   3738 	{
   3739 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   3740 				   &def);
   3741 	  p += sizeof (Elf_External_Verdef);
   3742 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   3743 	    continue;
   3744 	  for (i = 0; i < def.vd_cnt; ++i)
   3745 	    {
   3746 	      _bfd_elf_swap_verdaux_in (output_bfd,
   3747 					(Elf_External_Verdaux *) p, &defaux);
   3748 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   3749 							defaux.vda_name);
   3750 	      _bfd_elf_swap_verdaux_out (output_bfd,
   3751 					 &defaux, (Elf_External_Verdaux *) p);
   3752 	      p += sizeof (Elf_External_Verdaux);
   3753 	    }
   3754 	}
   3755       while (def.vd_next);
   3756     }
   3757 
   3758   /* Adjust version references.  */
   3759   if (elf_tdata (output_bfd)->verref)
   3760     {
   3761       asection *s;
   3762       bfd_byte *p;
   3763       size_t i;
   3764       Elf_Internal_Verneed need;
   3765       Elf_Internal_Vernaux needaux;
   3766 
   3767       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   3768       p = s->contents;
   3769       do
   3770 	{
   3771 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   3772 				    &need);
   3773 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   3774 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   3775 				     (Elf_External_Verneed *) p);
   3776 	  p += sizeof (Elf_External_Verneed);
   3777 	  for (i = 0; i < need.vn_cnt; ++i)
   3778 	    {
   3779 	      _bfd_elf_swap_vernaux_in (output_bfd,
   3780 					(Elf_External_Vernaux *) p, &needaux);
   3781 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   3782 							 needaux.vna_name);
   3783 	      _bfd_elf_swap_vernaux_out (output_bfd,
   3784 					 &needaux,
   3785 					 (Elf_External_Vernaux *) p);
   3786 	      p += sizeof (Elf_External_Vernaux);
   3787 	    }
   3788 	}
   3789       while (need.vn_next);
   3790     }
   3791 
   3792   return TRUE;
   3793 }
   3794 
   3795 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3797    The default is to only match when the INPUT and OUTPUT are exactly
   3798    the same target.  */
   3799 
   3800 bfd_boolean
   3801 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   3802 				    const bfd_target *output)
   3803 {
   3804   return input == output;
   3805 }
   3806 
   3807 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3808    This version is used when different targets for the same architecture
   3809    are virtually identical.  */
   3810 
   3811 bfd_boolean
   3812 _bfd_elf_relocs_compatible (const bfd_target *input,
   3813 			    const bfd_target *output)
   3814 {
   3815   const struct elf_backend_data *obed, *ibed;
   3816 
   3817   if (input == output)
   3818     return TRUE;
   3819 
   3820   ibed = xvec_get_elf_backend_data (input);
   3821   obed = xvec_get_elf_backend_data (output);
   3822 
   3823   if (ibed->arch != obed->arch)
   3824     return FALSE;
   3825 
   3826   /* If both backends are using this function, deem them compatible.  */
   3827   return ibed->relocs_compatible == obed->relocs_compatible;
   3828 }
   3829 
   3830 /* Make a special call to the linker "notice" function to tell it that
   3831    we are about to handle an as-needed lib, or have finished
   3832    processing the lib.  */
   3833 
   3834 bfd_boolean
   3835 _bfd_elf_notice_as_needed (bfd *ibfd,
   3836 			   struct bfd_link_info *info,
   3837 			   enum notice_asneeded_action act)
   3838 {
   3839   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   3840 }
   3841 
   3842 /* Check relocations an ELF object file.  */
   3843 
   3844 bfd_boolean
   3845 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   3846 {
   3847   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3848   struct elf_link_hash_table *htab = elf_hash_table (info);
   3849 
   3850   /* If this object is the same format as the output object, and it is
   3851      not a shared library, then let the backend look through the
   3852      relocs.
   3853 
   3854      This is required to build global offset table entries and to
   3855      arrange for dynamic relocs.  It is not required for the
   3856      particular common case of linking non PIC code, even when linking
   3857      against shared libraries, but unfortunately there is no way of
   3858      knowing whether an object file has been compiled PIC or not.
   3859      Looking through the relocs is not particularly time consuming.
   3860      The problem is that we must either (1) keep the relocs in memory,
   3861      which causes the linker to require additional runtime memory or
   3862      (2) read the relocs twice from the input file, which wastes time.
   3863      This would be a good case for using mmap.
   3864 
   3865      I have no idea how to handle linking PIC code into a file of a
   3866      different format.  It probably can't be done.  */
   3867   if ((abfd->flags & DYNAMIC) == 0
   3868       && is_elf_hash_table (htab)
   3869       && bed->check_relocs != NULL
   3870       && elf_object_id (abfd) == elf_hash_table_id (htab)
   3871       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   3872     {
   3873       asection *o;
   3874 
   3875       for (o = abfd->sections; o != NULL; o = o->next)
   3876 	{
   3877 	  Elf_Internal_Rela *internal_relocs;
   3878 	  bfd_boolean ok;
   3879 
   3880 	  /* Don't check relocations in excluded sections.  */
   3881 	  if ((o->flags & SEC_RELOC) == 0
   3882 	      || (o->flags & SEC_EXCLUDE) != 0
   3883 	      || o->reloc_count == 0
   3884 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   3885 		  && (o->flags & SEC_DEBUGGING) != 0)
   3886 	      || bfd_is_abs_section (o->output_section))
   3887 	    continue;
   3888 
   3889 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   3890 						       info->keep_memory);
   3891 	  if (internal_relocs == NULL)
   3892 	    return FALSE;
   3893 
   3894 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
   3895 
   3896 	  if (elf_section_data (o)->relocs != internal_relocs)
   3897 	    free (internal_relocs);
   3898 
   3899 	  if (! ok)
   3900 	    return FALSE;
   3901 	}
   3902     }
   3903 
   3904   return TRUE;
   3905 }
   3906 
   3907 /* Add symbols from an ELF object file to the linker hash table.  */
   3908 
   3909 static bfd_boolean
   3910 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   3911 {
   3912   Elf_Internal_Ehdr *ehdr;
   3913   Elf_Internal_Shdr *hdr;
   3914   size_t symcount;
   3915   size_t extsymcount;
   3916   size_t extsymoff;
   3917   struct elf_link_hash_entry **sym_hash;
   3918   bfd_boolean dynamic;
   3919   Elf_External_Versym *extversym = NULL;
   3920   Elf_External_Versym *extversym_end = NULL;
   3921   Elf_External_Versym *ever;
   3922   struct elf_link_hash_entry *weaks;
   3923   struct elf_link_hash_entry **nondeflt_vers = NULL;
   3924   size_t nondeflt_vers_cnt = 0;
   3925   Elf_Internal_Sym *isymbuf = NULL;
   3926   Elf_Internal_Sym *isym;
   3927   Elf_Internal_Sym *isymend;
   3928   const struct elf_backend_data *bed;
   3929   bfd_boolean add_needed;
   3930   struct elf_link_hash_table *htab;
   3931   bfd_size_type amt;
   3932   void *alloc_mark = NULL;
   3933   struct bfd_hash_entry **old_table = NULL;
   3934   unsigned int old_size = 0;
   3935   unsigned int old_count = 0;
   3936   void *old_tab = NULL;
   3937   void *old_ent;
   3938   struct bfd_link_hash_entry *old_undefs = NULL;
   3939   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   3940   void *old_strtab = NULL;
   3941   size_t tabsize = 0;
   3942   asection *s;
   3943   bfd_boolean just_syms;
   3944 
   3945   htab = elf_hash_table (info);
   3946   bed = get_elf_backend_data (abfd);
   3947 
   3948   if ((abfd->flags & DYNAMIC) == 0)
   3949     dynamic = FALSE;
   3950   else
   3951     {
   3952       dynamic = TRUE;
   3953 
   3954       /* You can't use -r against a dynamic object.  Also, there's no
   3955 	 hope of using a dynamic object which does not exactly match
   3956 	 the format of the output file.  */
   3957       if (bfd_link_relocatable (info)
   3958 	  || !is_elf_hash_table (htab)
   3959 	  || info->output_bfd->xvec != abfd->xvec)
   3960 	{
   3961 	  if (bfd_link_relocatable (info))
   3962 	    bfd_set_error (bfd_error_invalid_operation);
   3963 	  else
   3964 	    bfd_set_error (bfd_error_wrong_format);
   3965 	  goto error_return;
   3966 	}
   3967     }
   3968 
   3969   ehdr = elf_elfheader (abfd);
   3970   if (info->warn_alternate_em
   3971       && bed->elf_machine_code != ehdr->e_machine
   3972       && ((bed->elf_machine_alt1 != 0
   3973 	   && ehdr->e_machine == bed->elf_machine_alt1)
   3974 	  || (bed->elf_machine_alt2 != 0
   3975 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   3976     _bfd_error_handler
   3977       /* xgettext:c-format */
   3978       (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
   3979        ehdr->e_machine, abfd, bed->elf_machine_code);
   3980 
   3981   /* As a GNU extension, any input sections which are named
   3982      .gnu.warning.SYMBOL are treated as warning symbols for the given
   3983      symbol.  This differs from .gnu.warning sections, which generate
   3984      warnings when they are included in an output file.  */
   3985   /* PR 12761: Also generate this warning when building shared libraries.  */
   3986   for (s = abfd->sections; s != NULL; s = s->next)
   3987     {
   3988       const char *name;
   3989 
   3990       name = bfd_section_name (s);
   3991       if (CONST_STRNEQ (name, ".gnu.warning."))
   3992 	{
   3993 	  char *msg;
   3994 	  bfd_size_type sz;
   3995 
   3996 	  name += sizeof ".gnu.warning." - 1;
   3997 
   3998 	  /* If this is a shared object, then look up the symbol
   3999 	     in the hash table.  If it is there, and it is already
   4000 	     been defined, then we will not be using the entry
   4001 	     from this shared object, so we don't need to warn.
   4002 	     FIXME: If we see the definition in a regular object
   4003 	     later on, we will warn, but we shouldn't.  The only
   4004 	     fix is to keep track of what warnings we are supposed
   4005 	     to emit, and then handle them all at the end of the
   4006 	     link.  */
   4007 	  if (dynamic)
   4008 	    {
   4009 	      struct elf_link_hash_entry *h;
   4010 
   4011 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   4012 
   4013 	      /* FIXME: What about bfd_link_hash_common?  */
   4014 	      if (h != NULL
   4015 		  && (h->root.type == bfd_link_hash_defined
   4016 		      || h->root.type == bfd_link_hash_defweak))
   4017 		continue;
   4018 	    }
   4019 
   4020 	  sz = s->size;
   4021 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   4022 	  if (msg == NULL)
   4023 	    goto error_return;
   4024 
   4025 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   4026 	    goto error_return;
   4027 
   4028 	  msg[sz] = '\0';
   4029 
   4030 	  if (! (_bfd_generic_link_add_one_symbol
   4031 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   4032 		  FALSE, bed->collect, NULL)))
   4033 	    goto error_return;
   4034 
   4035 	  if (bfd_link_executable (info))
   4036 	    {
   4037 	      /* Clobber the section size so that the warning does
   4038 		 not get copied into the output file.  */
   4039 	      s->size = 0;
   4040 
   4041 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   4042 		 the warning section don't get copied to the output.  */
   4043 	      s->flags |= SEC_EXCLUDE;
   4044 	    }
   4045 	}
   4046     }
   4047 
   4048   just_syms = ((s = abfd->sections) != NULL
   4049 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   4050 
   4051   add_needed = TRUE;
   4052   if (! dynamic)
   4053     {
   4054       /* If we are creating a shared library, create all the dynamic
   4055 	 sections immediately.  We need to attach them to something,
   4056 	 so we attach them to this BFD, provided it is the right
   4057 	 format and is not from ld --just-symbols.  Always create the
   4058 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
   4059 	 are no input BFD's of the same format as the output, we can't
   4060 	 make a shared library.  */
   4061       if (!just_syms
   4062 	  && (bfd_link_pic (info)
   4063 	      || (!bfd_link_relocatable (info)
   4064 		  && info->nointerp
   4065 		  && (info->export_dynamic || info->dynamic)))
   4066 	  && is_elf_hash_table (htab)
   4067 	  && info->output_bfd->xvec == abfd->xvec
   4068 	  && !htab->dynamic_sections_created)
   4069 	{
   4070 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   4071 	    goto error_return;
   4072 	}
   4073     }
   4074   else if (!is_elf_hash_table (htab))
   4075     goto error_return;
   4076   else
   4077     {
   4078       const char *soname = NULL;
   4079       char *audit = NULL;
   4080       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   4081       const Elf_Internal_Phdr *phdr;
   4082       int ret;
   4083 
   4084       /* ld --just-symbols and dynamic objects don't mix very well.
   4085 	 ld shouldn't allow it.  */
   4086       if (just_syms)
   4087 	abort ();
   4088 
   4089       /* If this dynamic lib was specified on the command line with
   4090 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   4091 	 tag unless the lib is actually used.  Similary for libs brought
   4092 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   4093 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   4094 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   4095 	 all.  */
   4096       add_needed = (elf_dyn_lib_class (abfd)
   4097 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   4098 		       | DYN_NO_NEEDED)) == 0;
   4099 
   4100       s = bfd_get_section_by_name (abfd, ".dynamic");
   4101       if (s != NULL)
   4102 	{
   4103 	  bfd_byte *dynbuf;
   4104 	  bfd_byte *extdyn;
   4105 	  unsigned int elfsec;
   4106 	  unsigned long shlink;
   4107 
   4108 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   4109 	    {
   4110 error_free_dyn:
   4111 	      free (dynbuf);
   4112 	      goto error_return;
   4113 	    }
   4114 
   4115 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   4116 	  if (elfsec == SHN_BAD)
   4117 	    goto error_free_dyn;
   4118 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   4119 
   4120 	  for (extdyn = dynbuf;
   4121 	       extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
   4122 	       extdyn += bed->s->sizeof_dyn)
   4123 	    {
   4124 	      Elf_Internal_Dyn dyn;
   4125 
   4126 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   4127 	      if (dyn.d_tag == DT_SONAME)
   4128 		{
   4129 		  unsigned int tagv = dyn.d_un.d_val;
   4130 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4131 		  if (soname == NULL)
   4132 		    goto error_free_dyn;
   4133 		}
   4134 	      if (dyn.d_tag == DT_NEEDED)
   4135 		{
   4136 		  struct bfd_link_needed_list *n, **pn;
   4137 		  char *fnm, *anm;
   4138 		  unsigned int tagv = dyn.d_un.d_val;
   4139 
   4140 		  amt = sizeof (struct bfd_link_needed_list);
   4141 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4142 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4143 		  if (n == NULL || fnm == NULL)
   4144 		    goto error_free_dyn;
   4145 		  amt = strlen (fnm) + 1;
   4146 		  anm = (char *) bfd_alloc (abfd, amt);
   4147 		  if (anm == NULL)
   4148 		    goto error_free_dyn;
   4149 		  memcpy (anm, fnm, amt);
   4150 		  n->name = anm;
   4151 		  n->by = abfd;
   4152 		  n->next = NULL;
   4153 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   4154 		    ;
   4155 		  *pn = n;
   4156 		}
   4157 	      if (dyn.d_tag == DT_RUNPATH)
   4158 		{
   4159 		  struct bfd_link_needed_list *n, **pn;
   4160 		  char *fnm, *anm;
   4161 		  unsigned int tagv = dyn.d_un.d_val;
   4162 
   4163 		  amt = sizeof (struct bfd_link_needed_list);
   4164 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4165 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4166 		  if (n == NULL || fnm == NULL)
   4167 		    goto error_free_dyn;
   4168 		  amt = strlen (fnm) + 1;
   4169 		  anm = (char *) bfd_alloc (abfd, amt);
   4170 		  if (anm == NULL)
   4171 		    goto error_free_dyn;
   4172 		  memcpy (anm, fnm, amt);
   4173 		  n->name = anm;
   4174 		  n->by = abfd;
   4175 		  n->next = NULL;
   4176 		  for (pn = & runpath;
   4177 		       *pn != NULL;
   4178 		       pn = &(*pn)->next)
   4179 		    ;
   4180 		  *pn = n;
   4181 		}
   4182 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   4183 	      if (!runpath && dyn.d_tag == DT_RPATH)
   4184 		{
   4185 		  struct bfd_link_needed_list *n, **pn;
   4186 		  char *fnm, *anm;
   4187 		  unsigned int tagv = dyn.d_un.d_val;
   4188 
   4189 		  amt = sizeof (struct bfd_link_needed_list);
   4190 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4191 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4192 		  if (n == NULL || fnm == NULL)
   4193 		    goto error_free_dyn;
   4194 		  amt = strlen (fnm) + 1;
   4195 		  anm = (char *) bfd_alloc (abfd, amt);
   4196 		  if (anm == NULL)
   4197 		    goto error_free_dyn;
   4198 		  memcpy (anm, fnm, amt);
   4199 		  n->name = anm;
   4200 		  n->by = abfd;
   4201 		  n->next = NULL;
   4202 		  for (pn = & rpath;
   4203 		       *pn != NULL;
   4204 		       pn = &(*pn)->next)
   4205 		    ;
   4206 		  *pn = n;
   4207 		}
   4208 	      if (dyn.d_tag == DT_AUDIT)
   4209 		{
   4210 		  unsigned int tagv = dyn.d_un.d_val;
   4211 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4212 		}
   4213 	    }
   4214 
   4215 	  free (dynbuf);
   4216 	}
   4217 
   4218       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   4219 	 frees all more recently bfd_alloc'd blocks as well.  */
   4220       if (runpath)
   4221 	rpath = runpath;
   4222 
   4223       if (rpath)
   4224 	{
   4225 	  struct bfd_link_needed_list **pn;
   4226 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   4227 	    ;
   4228 	  *pn = rpath;
   4229 	}
   4230 
   4231       /* If we have a PT_GNU_RELRO program header, mark as read-only
   4232 	 all sections contained fully therein.  This makes relro
   4233 	 shared library sections appear as they will at run-time.  */
   4234       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
   4235       while (phdr-- > elf_tdata (abfd)->phdr)
   4236 	if (phdr->p_type == PT_GNU_RELRO)
   4237 	  {
   4238 	    for (s = abfd->sections; s != NULL; s = s->next)
   4239 	      if ((s->flags & SEC_ALLOC) != 0
   4240 		  && s->vma >= phdr->p_vaddr
   4241 		  && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
   4242 		s->flags |= SEC_READONLY;
   4243 	    break;
   4244 	  }
   4245 
   4246       /* We do not want to include any of the sections in a dynamic
   4247 	 object in the output file.  We hack by simply clobbering the
   4248 	 list of sections in the BFD.  This could be handled more
   4249 	 cleanly by, say, a new section flag; the existing
   4250 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   4251 	 still implies that the section takes up space in the output
   4252 	 file.  */
   4253       bfd_section_list_clear (abfd);
   4254 
   4255       /* Find the name to use in a DT_NEEDED entry that refers to this
   4256 	 object.  If the object has a DT_SONAME entry, we use it.
   4257 	 Otherwise, if the generic linker stuck something in
   4258 	 elf_dt_name, we use that.  Otherwise, we just use the file
   4259 	 name.  */
   4260       if (soname == NULL || *soname == '\0')
   4261 	{
   4262 	  soname = elf_dt_name (abfd);
   4263 	  if (soname == NULL || *soname == '\0')
   4264 	    soname = bfd_get_filename (abfd);
   4265 	}
   4266 
   4267       /* Save the SONAME because sometimes the linker emulation code
   4268 	 will need to know it.  */
   4269       elf_dt_name (abfd) = soname;
   4270 
   4271       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   4272       if (ret < 0)
   4273 	goto error_return;
   4274 
   4275       /* If we have already included this dynamic object in the
   4276 	 link, just ignore it.  There is no reason to include a
   4277 	 particular dynamic object more than once.  */
   4278       if (ret > 0)
   4279 	return TRUE;
   4280 
   4281       /* Save the DT_AUDIT entry for the linker emulation code. */
   4282       elf_dt_audit (abfd) = audit;
   4283     }
   4284 
   4285   /* If this is a dynamic object, we always link against the .dynsym
   4286      symbol table, not the .symtab symbol table.  The dynamic linker
   4287      will only see the .dynsym symbol table, so there is no reason to
   4288      look at .symtab for a dynamic object.  */
   4289 
   4290   if (! dynamic || elf_dynsymtab (abfd) == 0)
   4291     hdr = &elf_tdata (abfd)->symtab_hdr;
   4292   else
   4293     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   4294 
   4295   symcount = hdr->sh_size / bed->s->sizeof_sym;
   4296 
   4297   /* The sh_info field of the symtab header tells us where the
   4298      external symbols start.  We don't care about the local symbols at
   4299      this point.  */
   4300   if (elf_bad_symtab (abfd))
   4301     {
   4302       extsymcount = symcount;
   4303       extsymoff = 0;
   4304     }
   4305   else
   4306     {
   4307       extsymcount = symcount - hdr->sh_info;
   4308       extsymoff = hdr->sh_info;
   4309     }
   4310 
   4311   sym_hash = elf_sym_hashes (abfd);
   4312   if (extsymcount != 0)
   4313     {
   4314       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   4315 				      NULL, NULL, NULL);
   4316       if (isymbuf == NULL)
   4317 	goto error_return;
   4318 
   4319       if (sym_hash == NULL)
   4320 	{
   4321 	  /* We store a pointer to the hash table entry for each
   4322 	     external symbol.  */
   4323 	  amt = extsymcount;
   4324 	  amt *= sizeof (struct elf_link_hash_entry *);
   4325 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   4326 	  if (sym_hash == NULL)
   4327 	    goto error_free_sym;
   4328 	  elf_sym_hashes (abfd) = sym_hash;
   4329 	}
   4330     }
   4331 
   4332   if (dynamic)
   4333     {
   4334       /* Read in any version definitions.  */
   4335       if (!_bfd_elf_slurp_version_tables (abfd,
   4336 					  info->default_imported_symver))
   4337 	goto error_free_sym;
   4338 
   4339       /* Read in the symbol versions, but don't bother to convert them
   4340 	 to internal format.  */
   4341       if (elf_dynversym (abfd) != 0)
   4342 	{
   4343 	  Elf_Internal_Shdr *versymhdr;
   4344 
   4345 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   4346 	  amt = versymhdr->sh_size;
   4347 	  extversym = (Elf_External_Versym *) bfd_malloc (amt);
   4348 	  if (extversym == NULL)
   4349 	    goto error_free_sym;
   4350 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
   4351 	      || bfd_bread (extversym, amt, abfd) != amt)
   4352 	    goto error_free_vers;
   4353 	  extversym_end = extversym + (amt / sizeof (* extversym));
   4354 	}
   4355     }
   4356 
   4357   /* If we are loading an as-needed shared lib, save the symbol table
   4358      state before we start adding symbols.  If the lib turns out
   4359      to be unneeded, restore the state.  */
   4360   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4361     {
   4362       unsigned int i;
   4363       size_t entsize;
   4364 
   4365       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   4366 	{
   4367 	  struct bfd_hash_entry *p;
   4368 	  struct elf_link_hash_entry *h;
   4369 
   4370 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4371 	    {
   4372 	      h = (struct elf_link_hash_entry *) p;
   4373 	      entsize += htab->root.table.entsize;
   4374 	      if (h->root.type == bfd_link_hash_warning)
   4375 		entsize += htab->root.table.entsize;
   4376 	    }
   4377 	}
   4378 
   4379       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   4380       old_tab = bfd_malloc (tabsize + entsize);
   4381       if (old_tab == NULL)
   4382 	goto error_free_vers;
   4383 
   4384       /* Remember the current objalloc pointer, so that all mem for
   4385 	 symbols added can later be reclaimed.  */
   4386       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   4387       if (alloc_mark == NULL)
   4388 	goto error_free_vers;
   4389 
   4390       /* Make a special call to the linker "notice" function to
   4391 	 tell it that we are about to handle an as-needed lib.  */
   4392       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   4393 	goto error_free_vers;
   4394 
   4395       /* Clone the symbol table.  Remember some pointers into the
   4396 	 symbol table, and dynamic symbol count.  */
   4397       old_ent = (char *) old_tab + tabsize;
   4398       memcpy (old_tab, htab->root.table.table, tabsize);
   4399       old_undefs = htab->root.undefs;
   4400       old_undefs_tail = htab->root.undefs_tail;
   4401       old_table = htab->root.table.table;
   4402       old_size = htab->root.table.size;
   4403       old_count = htab->root.table.count;
   4404       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   4405       if (old_strtab == NULL)
   4406 	goto error_free_vers;
   4407 
   4408       for (i = 0; i < htab->root.table.size; i++)
   4409 	{
   4410 	  struct bfd_hash_entry *p;
   4411 	  struct elf_link_hash_entry *h;
   4412 
   4413 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4414 	    {
   4415 	      memcpy (old_ent, p, htab->root.table.entsize);
   4416 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4417 	      h = (struct elf_link_hash_entry *) p;
   4418 	      if (h->root.type == bfd_link_hash_warning)
   4419 		{
   4420 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
   4421 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4422 		}
   4423 	    }
   4424 	}
   4425     }
   4426 
   4427   weaks = NULL;
   4428   if (extversym == NULL)
   4429     ever = NULL;
   4430   else if (extversym + extsymoff < extversym_end)
   4431     ever = extversym + extsymoff;
   4432   else
   4433     {
   4434       /* xgettext:c-format */
   4435       _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
   4436 			  abfd, (long) extsymoff,
   4437 			  (long) (extversym_end - extversym) / sizeof (* extversym));
   4438       bfd_set_error (bfd_error_bad_value);
   4439       goto error_free_vers;
   4440     }
   4441 
   4442   if (!bfd_link_relocatable (info)
   4443       && abfd->lto_slim_object)
   4444     {
   4445       _bfd_error_handler
   4446 	(_("%pB: plugin needed to handle lto object"), abfd);
   4447     }
   4448 
   4449   for (isym = isymbuf, isymend = isymbuf + extsymcount;
   4450        isym < isymend;
   4451        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   4452     {
   4453       int bind;
   4454       bfd_vma value;
   4455       asection *sec, *new_sec;
   4456       flagword flags;
   4457       const char *name;
   4458       struct elf_link_hash_entry *h;
   4459       struct elf_link_hash_entry *hi;
   4460       bfd_boolean definition;
   4461       bfd_boolean size_change_ok;
   4462       bfd_boolean type_change_ok;
   4463       bfd_boolean new_weak;
   4464       bfd_boolean old_weak;
   4465       bfd_boolean override;
   4466       bfd_boolean common;
   4467       bfd_boolean discarded;
   4468       unsigned int old_alignment;
   4469       unsigned int shindex;
   4470       bfd *old_bfd;
   4471       bfd_boolean matched;
   4472 
   4473       override = FALSE;
   4474 
   4475       flags = BSF_NO_FLAGS;
   4476       sec = NULL;
   4477       value = isym->st_value;
   4478       common = bed->common_definition (isym);
   4479       if (common && info->inhibit_common_definition)
   4480 	{
   4481 	  /* Treat common symbol as undefined for --no-define-common.  */
   4482 	  isym->st_shndx = SHN_UNDEF;
   4483 	  common = FALSE;
   4484 	}
   4485       discarded = FALSE;
   4486 
   4487       bind = ELF_ST_BIND (isym->st_info);
   4488       switch (bind)
   4489 	{
   4490 	case STB_LOCAL:
   4491 	  /* This should be impossible, since ELF requires that all
   4492 	     global symbols follow all local symbols, and that sh_info
   4493 	     point to the first global symbol.  Unfortunately, Irix 5
   4494 	     screws this up.  */
   4495 	  if (elf_bad_symtab (abfd))
   4496 	    continue;
   4497 
   4498 	  /* If we aren't prepared to handle locals within the globals
   4499 	     then we'll likely segfault on a NULL symbol hash if the
   4500 	     symbol is ever referenced in relocations.  */
   4501 	  shindex = elf_elfheader (abfd)->e_shstrndx;
   4502 	  name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
   4503 	  _bfd_error_handler (_("%pB: %s local symbol at index %lu"
   4504 				" (>= sh_info of %lu)"),
   4505 			      abfd, name, (long) (isym - isymbuf + extsymoff),
   4506 			      (long) extsymoff);
   4507 
   4508 	  /* Dynamic object relocations are not processed by ld, so
   4509 	     ld won't run into the problem mentioned above.  */
   4510 	  if (dynamic)
   4511 	    continue;
   4512 	  bfd_set_error (bfd_error_bad_value);
   4513 	  goto error_free_vers;
   4514 
   4515 	case STB_GLOBAL:
   4516 	  if (isym->st_shndx != SHN_UNDEF && !common)
   4517 	    flags = BSF_GLOBAL;
   4518 	  break;
   4519 
   4520 	case STB_WEAK:
   4521 	  flags = BSF_WEAK;
   4522 	  break;
   4523 
   4524 	case STB_GNU_UNIQUE:
   4525 	  flags = BSF_GNU_UNIQUE;
   4526 	  break;
   4527 
   4528 	default:
   4529 	  /* Leave it up to the processor backend.  */
   4530 	  break;
   4531 	}
   4532 
   4533       if (isym->st_shndx == SHN_UNDEF)
   4534 	sec = bfd_und_section_ptr;
   4535       else if (isym->st_shndx == SHN_ABS)
   4536 	sec = bfd_abs_section_ptr;
   4537       else if (isym->st_shndx == SHN_COMMON)
   4538 	{
   4539 	  sec = bfd_com_section_ptr;
   4540 	  /* What ELF calls the size we call the value.  What ELF
   4541 	     calls the value we call the alignment.  */
   4542 	  value = isym->st_size;
   4543 	}
   4544       else
   4545 	{
   4546 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4547 	  if (sec == NULL)
   4548 	    sec = bfd_abs_section_ptr;
   4549 	  else if (discarded_section (sec))
   4550 	    {
   4551 	      /* Symbols from discarded section are undefined.  We keep
   4552 		 its visibility.  */
   4553 	      sec = bfd_und_section_ptr;
   4554 	      discarded = TRUE;
   4555 	      isym->st_shndx = SHN_UNDEF;
   4556 	    }
   4557 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   4558 	    value -= sec->vma;
   4559 	}
   4560 
   4561       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   4562 					      isym->st_name);
   4563       if (name == NULL)
   4564 	goto error_free_vers;
   4565 
   4566       if (isym->st_shndx == SHN_COMMON
   4567 	  && (abfd->flags & BFD_PLUGIN) != 0)
   4568 	{
   4569 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   4570 
   4571 	  if (xc == NULL)
   4572 	    {
   4573 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   4574 				 | SEC_EXCLUDE);
   4575 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   4576 	      if (xc == NULL)
   4577 		goto error_free_vers;
   4578 	    }
   4579 	  sec = xc;
   4580 	}
   4581       else if (isym->st_shndx == SHN_COMMON
   4582 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   4583 	       && !bfd_link_relocatable (info))
   4584 	{
   4585 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   4586 
   4587 	  if (tcomm == NULL)
   4588 	    {
   4589 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   4590 				 | SEC_LINKER_CREATED);
   4591 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   4592 	      if (tcomm == NULL)
   4593 		goto error_free_vers;
   4594 	    }
   4595 	  sec = tcomm;
   4596 	}
   4597       else if (bed->elf_add_symbol_hook)
   4598 	{
   4599 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   4600 					     &sec, &value))
   4601 	    goto error_free_vers;
   4602 
   4603 	  /* The hook function sets the name to NULL if this symbol
   4604 	     should be skipped for some reason.  */
   4605 	  if (name == NULL)
   4606 	    continue;
   4607 	}
   4608 
   4609       /* Sanity check that all possibilities were handled.  */
   4610       if (sec == NULL)
   4611 	abort ();
   4612 
   4613       /* Silently discard TLS symbols from --just-syms.  There's
   4614 	 no way to combine a static TLS block with a new TLS block
   4615 	 for this executable.  */
   4616       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   4617 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   4618 	continue;
   4619 
   4620       if (bfd_is_und_section (sec)
   4621 	  || bfd_is_com_section (sec))
   4622 	definition = FALSE;
   4623       else
   4624 	definition = TRUE;
   4625 
   4626       size_change_ok = FALSE;
   4627       type_change_ok = bed->type_change_ok;
   4628       old_weak = FALSE;
   4629       matched = FALSE;
   4630       old_alignment = 0;
   4631       old_bfd = NULL;
   4632       new_sec = sec;
   4633 
   4634       if (is_elf_hash_table (htab))
   4635 	{
   4636 	  Elf_Internal_Versym iver;
   4637 	  unsigned int vernum = 0;
   4638 	  bfd_boolean skip;
   4639 
   4640 	  if (ever == NULL)
   4641 	    {
   4642 	      if (info->default_imported_symver)
   4643 		/* Use the default symbol version created earlier.  */
   4644 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   4645 	      else
   4646 		iver.vs_vers = 0;
   4647 	    }
   4648 	  else if (ever >= extversym_end)
   4649 	    {
   4650 	      /* xgettext:c-format */
   4651 	      _bfd_error_handler (_("%pB: not enough version information"),
   4652 				  abfd);
   4653 	      bfd_set_error (bfd_error_bad_value);
   4654 	      goto error_free_vers;
   4655 	    }
   4656 	  else
   4657 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   4658 
   4659 	  vernum = iver.vs_vers & VERSYM_VERSION;
   4660 
   4661 	  /* If this is a hidden symbol, or if it is not version
   4662 	     1, we append the version name to the symbol name.
   4663 	     However, we do not modify a non-hidden absolute symbol
   4664 	     if it is not a function, because it might be the version
   4665 	     symbol itself.  FIXME: What if it isn't?  */
   4666 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   4667 	      || (vernum > 1
   4668 		  && (!bfd_is_abs_section (sec)
   4669 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   4670 	    {
   4671 	      const char *verstr;
   4672 	      size_t namelen, verlen, newlen;
   4673 	      char *newname, *p;
   4674 
   4675 	      if (isym->st_shndx != SHN_UNDEF)
   4676 		{
   4677 		  if (vernum > elf_tdata (abfd)->cverdefs)
   4678 		    verstr = NULL;
   4679 		  else if (vernum > 1)
   4680 		    verstr =
   4681 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   4682 		  else
   4683 		    verstr = "";
   4684 
   4685 		  if (verstr == NULL)
   4686 		    {
   4687 		      _bfd_error_handler
   4688 			/* xgettext:c-format */
   4689 			(_("%pB: %s: invalid version %u (max %d)"),
   4690 			 abfd, name, vernum,
   4691 			 elf_tdata (abfd)->cverdefs);
   4692 		      bfd_set_error (bfd_error_bad_value);
   4693 		      goto error_free_vers;
   4694 		    }
   4695 		}
   4696 	      else
   4697 		{
   4698 		  /* We cannot simply test for the number of
   4699 		     entries in the VERNEED section since the
   4700 		     numbers for the needed versions do not start
   4701 		     at 0.  */
   4702 		  Elf_Internal_Verneed *t;
   4703 
   4704 		  verstr = NULL;
   4705 		  for (t = elf_tdata (abfd)->verref;
   4706 		       t != NULL;
   4707 		       t = t->vn_nextref)
   4708 		    {
   4709 		      Elf_Internal_Vernaux *a;
   4710 
   4711 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   4712 			{
   4713 			  if (a->vna_other == vernum)
   4714 			    {
   4715 			      verstr = a->vna_nodename;
   4716 			      break;
   4717 			    }
   4718 			}
   4719 		      if (a != NULL)
   4720 			break;
   4721 		    }
   4722 		  if (verstr == NULL)
   4723 		    {
   4724 		      _bfd_error_handler
   4725 			/* xgettext:c-format */
   4726 			(_("%pB: %s: invalid needed version %d"),
   4727 			 abfd, name, vernum);
   4728 		      bfd_set_error (bfd_error_bad_value);
   4729 		      goto error_free_vers;
   4730 		    }
   4731 		}
   4732 
   4733 	      namelen = strlen (name);
   4734 	      verlen = strlen (verstr);
   4735 	      newlen = namelen + verlen + 2;
   4736 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4737 		  && isym->st_shndx != SHN_UNDEF)
   4738 		++newlen;
   4739 
   4740 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   4741 	      if (newname == NULL)
   4742 		goto error_free_vers;
   4743 	      memcpy (newname, name, namelen);
   4744 	      p = newname + namelen;
   4745 	      *p++ = ELF_VER_CHR;
   4746 	      /* If this is a defined non-hidden version symbol,
   4747 		 we add another @ to the name.  This indicates the
   4748 		 default version of the symbol.  */
   4749 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4750 		  && isym->st_shndx != SHN_UNDEF)
   4751 		*p++ = ELF_VER_CHR;
   4752 	      memcpy (p, verstr, verlen + 1);
   4753 
   4754 	      name = newname;
   4755 	    }
   4756 
   4757 	  /* If this symbol has default visibility and the user has
   4758 	     requested we not re-export it, then mark it as hidden.  */
   4759 	  if (!bfd_is_und_section (sec)
   4760 	      && !dynamic
   4761 	      && abfd->no_export
   4762 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   4763 	    isym->st_other = (STV_HIDDEN
   4764 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   4765 
   4766 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   4767 				      sym_hash, &old_bfd, &old_weak,
   4768 				      &old_alignment, &skip, &override,
   4769 				      &type_change_ok, &size_change_ok,
   4770 				      &matched))
   4771 	    goto error_free_vers;
   4772 
   4773 	  if (skip)
   4774 	    continue;
   4775 
   4776 	  /* Override a definition only if the new symbol matches the
   4777 	     existing one.  */
   4778 	  if (override && matched)
   4779 	    definition = FALSE;
   4780 
   4781 	  h = *sym_hash;
   4782 	  while (h->root.type == bfd_link_hash_indirect
   4783 		 || h->root.type == bfd_link_hash_warning)
   4784 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4785 
   4786 	  if (elf_tdata (abfd)->verdef != NULL
   4787 	      && vernum > 1
   4788 	      && definition)
   4789 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   4790 	}
   4791 
   4792       if (! (_bfd_generic_link_add_one_symbol
   4793 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
   4794 	      (struct bfd_link_hash_entry **) sym_hash)))
   4795 	goto error_free_vers;
   4796 
   4797       h = *sym_hash;
   4798       /* We need to make sure that indirect symbol dynamic flags are
   4799 	 updated.  */
   4800       hi = h;
   4801       while (h->root.type == bfd_link_hash_indirect
   4802 	     || h->root.type == bfd_link_hash_warning)
   4803 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4804 
   4805       /* Setting the index to -3 tells elf_link_output_extsym that
   4806 	 this symbol is defined in a discarded section.  */
   4807       if (discarded)
   4808 	h->indx = -3;
   4809 
   4810       *sym_hash = h;
   4811 
   4812       new_weak = (flags & BSF_WEAK) != 0;
   4813       if (dynamic
   4814 	  && definition
   4815 	  && new_weak
   4816 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   4817 	  && is_elf_hash_table (htab)
   4818 	  && h->u.alias == NULL)
   4819 	{
   4820 	  /* Keep a list of all weak defined non function symbols from
   4821 	     a dynamic object, using the alias field.  Later in this
   4822 	     function we will set the alias field to the correct
   4823 	     value.  We only put non-function symbols from dynamic
   4824 	     objects on this list, because that happens to be the only
   4825 	     time we need to know the normal symbol corresponding to a
   4826 	     weak symbol, and the information is time consuming to
   4827 	     figure out.  If the alias field is not already NULL,
   4828 	     then this symbol was already defined by some previous
   4829 	     dynamic object, and we will be using that previous
   4830 	     definition anyhow.  */
   4831 
   4832 	  h->u.alias = weaks;
   4833 	  weaks = h;
   4834 	}
   4835 
   4836       /* Set the alignment of a common symbol.  */
   4837       if ((common || bfd_is_com_section (sec))
   4838 	  && h->root.type == bfd_link_hash_common)
   4839 	{
   4840 	  unsigned int align;
   4841 
   4842 	  if (common)
   4843 	    align = bfd_log2 (isym->st_value);
   4844 	  else
   4845 	    {
   4846 	      /* The new symbol is a common symbol in a shared object.
   4847 		 We need to get the alignment from the section.  */
   4848 	      align = new_sec->alignment_power;
   4849 	    }
   4850 	  if (align > old_alignment)
   4851 	    h->root.u.c.p->alignment_power = align;
   4852 	  else
   4853 	    h->root.u.c.p->alignment_power = old_alignment;
   4854 	}
   4855 
   4856       if (is_elf_hash_table (htab))
   4857 	{
   4858 	  /* Set a flag in the hash table entry indicating the type of
   4859 	     reference or definition we just found.  A dynamic symbol
   4860 	     is one which is referenced or defined by both a regular
   4861 	     object and a shared object.  */
   4862 	  bfd_boolean dynsym = FALSE;
   4863 
   4864 	  /* Plugin symbols aren't normal.  Don't set def_regular or
   4865 	     ref_regular for them, or make them dynamic.  */
   4866 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4867 	    ;
   4868 	  else if (! dynamic)
   4869 	    {
   4870 	      if (! definition)
   4871 		{
   4872 		  h->ref_regular = 1;
   4873 		  if (bind != STB_WEAK)
   4874 		    h->ref_regular_nonweak = 1;
   4875 		}
   4876 	      else
   4877 		{
   4878 		  h->def_regular = 1;
   4879 		  if (h->def_dynamic)
   4880 		    {
   4881 		      h->def_dynamic = 0;
   4882 		      h->ref_dynamic = 1;
   4883 		    }
   4884 		}
   4885 
   4886 	      /* If the indirect symbol has been forced local, don't
   4887 		 make the real symbol dynamic.  */
   4888 	      if ((h == hi || !hi->forced_local)
   4889 		  && (bfd_link_dll (info)
   4890 		      || h->def_dynamic
   4891 		      || h->ref_dynamic))
   4892 		dynsym = TRUE;
   4893 	    }
   4894 	  else
   4895 	    {
   4896 	      if (! definition)
   4897 		{
   4898 		  h->ref_dynamic = 1;
   4899 		  hi->ref_dynamic = 1;
   4900 		}
   4901 	      else
   4902 		{
   4903 		  h->def_dynamic = 1;
   4904 		  hi->def_dynamic = 1;
   4905 		}
   4906 
   4907 	      /* If the indirect symbol has been forced local, don't
   4908 		 make the real symbol dynamic.  */
   4909 	      if ((h == hi || !hi->forced_local)
   4910 		  && (h->def_regular
   4911 		      || h->ref_regular
   4912 		      || (h->is_weakalias
   4913 			  && weakdef (h)->dynindx != -1)))
   4914 		dynsym = TRUE;
   4915 	    }
   4916 
   4917 	  /* Check to see if we need to add an indirect symbol for
   4918 	     the default name.  */
   4919 	  if (definition
   4920 	      || (!override && h->root.type == bfd_link_hash_common))
   4921 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   4922 					      sec, value, &old_bfd, &dynsym))
   4923 	      goto error_free_vers;
   4924 
   4925 	  /* Check the alignment when a common symbol is involved. This
   4926 	     can change when a common symbol is overridden by a normal
   4927 	     definition or a common symbol is ignored due to the old
   4928 	     normal definition. We need to make sure the maximum
   4929 	     alignment is maintained.  */
   4930 	  if ((old_alignment || common)
   4931 	      && h->root.type != bfd_link_hash_common)
   4932 	    {
   4933 	      unsigned int common_align;
   4934 	      unsigned int normal_align;
   4935 	      unsigned int symbol_align;
   4936 	      bfd *normal_bfd;
   4937 	      bfd *common_bfd;
   4938 
   4939 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   4940 			  || h->root.type == bfd_link_hash_defweak);
   4941 
   4942 	      symbol_align = ffs (h->root.u.def.value) - 1;
   4943 	      if (h->root.u.def.section->owner != NULL
   4944 		  && (h->root.u.def.section->owner->flags
   4945 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
   4946 		{
   4947 		  normal_align = h->root.u.def.section->alignment_power;
   4948 		  if (normal_align > symbol_align)
   4949 		    normal_align = symbol_align;
   4950 		}
   4951 	      else
   4952 		normal_align = symbol_align;
   4953 
   4954 	      if (old_alignment)
   4955 		{
   4956 		  common_align = old_alignment;
   4957 		  common_bfd = old_bfd;
   4958 		  normal_bfd = abfd;
   4959 		}
   4960 	      else
   4961 		{
   4962 		  common_align = bfd_log2 (isym->st_value);
   4963 		  common_bfd = abfd;
   4964 		  normal_bfd = old_bfd;
   4965 		}
   4966 
   4967 	      if (normal_align < common_align)
   4968 		{
   4969 		  /* PR binutils/2735 */
   4970 		  if (normal_bfd == NULL)
   4971 		    _bfd_error_handler
   4972 		      /* xgettext:c-format */
   4973 		      (_("warning: alignment %u of common symbol `%s' in %pB is"
   4974 			 " greater than the alignment (%u) of its section %pA"),
   4975 		       1 << common_align, name, common_bfd,
   4976 		       1 << normal_align, h->root.u.def.section);
   4977 		  else
   4978 		    _bfd_error_handler
   4979 		      /* xgettext:c-format */
   4980 		      (_("warning: alignment %u of symbol `%s' in %pB"
   4981 			 " is smaller than %u in %pB"),
   4982 		       1 << normal_align, name, normal_bfd,
   4983 		       1 << common_align, common_bfd);
   4984 		}
   4985 	    }
   4986 
   4987 	  /* Remember the symbol size if it isn't undefined.  */
   4988 	  if (isym->st_size != 0
   4989 	      && isym->st_shndx != SHN_UNDEF
   4990 	      && (definition || h->size == 0))
   4991 	    {
   4992 	      if (h->size != 0
   4993 		  && h->size != isym->st_size
   4994 		  && ! size_change_ok)
   4995 		_bfd_error_handler
   4996 		  /* xgettext:c-format */
   4997 		  (_("warning: size of symbol `%s' changed"
   4998 		     " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
   4999 		   name, (uint64_t) h->size, old_bfd,
   5000 		   (uint64_t) isym->st_size, abfd);
   5001 
   5002 	      h->size = isym->st_size;
   5003 	    }
   5004 
   5005 	  /* If this is a common symbol, then we always want H->SIZE
   5006 	     to be the size of the common symbol.  The code just above
   5007 	     won't fix the size if a common symbol becomes larger.  We
   5008 	     don't warn about a size change here, because that is
   5009 	     covered by --warn-common.  Allow changes between different
   5010 	     function types.  */
   5011 	  if (h->root.type == bfd_link_hash_common)
   5012 	    h->size = h->root.u.c.size;
   5013 
   5014 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   5015 	      && ((definition && !new_weak)
   5016 		  || (old_weak && h->root.type == bfd_link_hash_common)
   5017 		  || h->type == STT_NOTYPE))
   5018 	    {
   5019 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   5020 
   5021 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   5022 		 symbol.  */
   5023 	      if (type == STT_GNU_IFUNC
   5024 		  && (abfd->flags & DYNAMIC) != 0)
   5025 		type = STT_FUNC;
   5026 
   5027 	      if (h->type != type)
   5028 		{
   5029 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   5030 		    /* xgettext:c-format */
   5031 		    _bfd_error_handler
   5032 		      (_("warning: type of symbol `%s' changed"
   5033 			 " from %d to %d in %pB"),
   5034 		       name, h->type, type, abfd);
   5035 
   5036 		  h->type = type;
   5037 		}
   5038 	    }
   5039 
   5040 	  /* Merge st_other field.  */
   5041 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
   5042 
   5043 	  /* We don't want to make debug symbol dynamic.  */
   5044 	  if (definition
   5045 	      && (sec->flags & SEC_DEBUGGING)
   5046 	      && !bfd_link_relocatable (info))
   5047 	    dynsym = FALSE;
   5048 
   5049 	  /* Nor should we make plugin symbols dynamic.  */
   5050 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   5051 	    dynsym = FALSE;
   5052 
   5053 	  if (definition)
   5054 	    {
   5055 	      h->target_internal = isym->st_target_internal;
   5056 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   5057 	    }
   5058 
   5059 	  if (definition && !dynamic)
   5060 	    {
   5061 	      char *p = strchr (name, ELF_VER_CHR);
   5062 	      if (p != NULL && p[1] != ELF_VER_CHR)
   5063 		{
   5064 		  /* Queue non-default versions so that .symver x, x@FOO
   5065 		     aliases can be checked.  */
   5066 		  if (!nondeflt_vers)
   5067 		    {
   5068 		      amt = ((isymend - isym + 1)
   5069 			     * sizeof (struct elf_link_hash_entry *));
   5070 		      nondeflt_vers
   5071 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   5072 		      if (!nondeflt_vers)
   5073 			goto error_free_vers;
   5074 		    }
   5075 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   5076 		}
   5077 	    }
   5078 
   5079 	  if (dynsym && h->dynindx == -1)
   5080 	    {
   5081 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5082 		goto error_free_vers;
   5083 	      if (h->is_weakalias
   5084 		  && weakdef (h)->dynindx == -1)
   5085 		{
   5086 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
   5087 		    goto error_free_vers;
   5088 		}
   5089 	    }
   5090 	  else if (h->dynindx != -1)
   5091 	    /* If the symbol already has a dynamic index, but
   5092 	       visibility says it should not be visible, turn it into
   5093 	       a local symbol.  */
   5094 	    switch (ELF_ST_VISIBILITY (h->other))
   5095 	      {
   5096 	      case STV_INTERNAL:
   5097 	      case STV_HIDDEN:
   5098 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   5099 		dynsym = FALSE;
   5100 		break;
   5101 	      }
   5102 
   5103 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
   5104 	     for unmatched symbol.  */
   5105 	  if (!add_needed
   5106 	      && matched
   5107 	      && definition
   5108 	      && ((dynsym
   5109 		   && h->ref_regular_nonweak
   5110 		   && (old_bfd == NULL
   5111 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
   5112 		  || (h->ref_dynamic_nonweak
   5113 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   5114 		      && !on_needed_list (elf_dt_name (abfd),
   5115 					  htab->needed, NULL))))
   5116 	    {
   5117 	      int ret;
   5118 	      const char *soname = elf_dt_name (abfd);
   5119 
   5120 	      info->callbacks->minfo ("%!", soname, old_bfd,
   5121 				      h->root.root.string);
   5122 
   5123 	      /* A symbol from a library loaded via DT_NEEDED of some
   5124 		 other library is referenced by a regular object.
   5125 		 Add a DT_NEEDED entry for it.  Issue an error if
   5126 		 --no-add-needed is used and the reference was not
   5127 		 a weak one.  */
   5128 	      if (old_bfd != NULL
   5129 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   5130 		{
   5131 		  _bfd_error_handler
   5132 		    /* xgettext:c-format */
   5133 		    (_("%pB: undefined reference to symbol '%s'"),
   5134 		     old_bfd, name);
   5135 		  bfd_set_error (bfd_error_missing_dso);
   5136 		  goto error_free_vers;
   5137 		}
   5138 
   5139 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   5140 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   5141 
   5142 	      add_needed = TRUE;
   5143 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   5144 	      if (ret < 0)
   5145 		goto error_free_vers;
   5146 
   5147 	      BFD_ASSERT (ret == 0);
   5148 	    }
   5149 	}
   5150     }
   5151 
   5152   if (info->lto_plugin_active
   5153       && !bfd_link_relocatable (info)
   5154       && (abfd->flags & BFD_PLUGIN) == 0
   5155       && !just_syms
   5156       && extsymcount)
   5157     {
   5158       int r_sym_shift;
   5159 
   5160       if (bed->s->arch_size == 32)
   5161 	r_sym_shift = 8;
   5162       else
   5163 	r_sym_shift = 32;
   5164 
   5165       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
   5166 	 referenced in regular objects so that linker plugin will get
   5167 	 the correct symbol resolution.  */
   5168 
   5169       sym_hash = elf_sym_hashes (abfd);
   5170       for (s = abfd->sections; s != NULL; s = s->next)
   5171 	{
   5172 	  Elf_Internal_Rela *internal_relocs;
   5173 	  Elf_Internal_Rela *rel, *relend;
   5174 
   5175 	  /* Don't check relocations in excluded sections.  */
   5176 	  if ((s->flags & SEC_RELOC) == 0
   5177 	      || s->reloc_count == 0
   5178 	      || (s->flags & SEC_EXCLUDE) != 0
   5179 	      || ((info->strip == strip_all
   5180 		   || info->strip == strip_debugger)
   5181 		  && (s->flags & SEC_DEBUGGING) != 0))
   5182 	    continue;
   5183 
   5184 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
   5185 						       NULL,
   5186 						       info->keep_memory);
   5187 	  if (internal_relocs == NULL)
   5188 	    goto error_free_vers;
   5189 
   5190 	  rel = internal_relocs;
   5191 	  relend = rel + s->reloc_count;
   5192 	  for ( ; rel < relend; rel++)
   5193 	    {
   5194 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   5195 	      struct elf_link_hash_entry *h;
   5196 
   5197 	      /* Skip local symbols.  */
   5198 	      if (r_symndx < extsymoff)
   5199 		continue;
   5200 
   5201 	      h = sym_hash[r_symndx - extsymoff];
   5202 	      if (h != NULL)
   5203 		h->root.non_ir_ref_regular = 1;
   5204 	    }
   5205 
   5206 	  if (elf_section_data (s)->relocs != internal_relocs)
   5207 	    free (internal_relocs);
   5208 	}
   5209     }
   5210 
   5211   if (extversym != NULL)
   5212     {
   5213       free (extversym);
   5214       extversym = NULL;
   5215     }
   5216 
   5217   if (isymbuf != NULL)
   5218     {
   5219       free (isymbuf);
   5220       isymbuf = NULL;
   5221     }
   5222 
   5223   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   5224     {
   5225       unsigned int i;
   5226 
   5227       /* Restore the symbol table.  */
   5228       old_ent = (char *) old_tab + tabsize;
   5229       memset (elf_sym_hashes (abfd), 0,
   5230 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   5231       htab->root.table.table = old_table;
   5232       htab->root.table.size = old_size;
   5233       htab->root.table.count = old_count;
   5234       memcpy (htab->root.table.table, old_tab, tabsize);
   5235       htab->root.undefs = old_undefs;
   5236       htab->root.undefs_tail = old_undefs_tail;
   5237       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   5238       free (old_strtab);
   5239       old_strtab = NULL;
   5240       for (i = 0; i < htab->root.table.size; i++)
   5241 	{
   5242 	  struct bfd_hash_entry *p;
   5243 	  struct elf_link_hash_entry *h;
   5244 	  bfd_size_type size;
   5245 	  unsigned int alignment_power;
   5246 	  unsigned int non_ir_ref_dynamic;
   5247 
   5248 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   5249 	    {
   5250 	      h = (struct elf_link_hash_entry *) p;
   5251 	      if (h->root.type == bfd_link_hash_warning)
   5252 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5253 
   5254 	      /* Preserve the maximum alignment and size for common
   5255 		 symbols even if this dynamic lib isn't on DT_NEEDED
   5256 		 since it can still be loaded at run time by another
   5257 		 dynamic lib.  */
   5258 	      if (h->root.type == bfd_link_hash_common)
   5259 		{
   5260 		  size = h->root.u.c.size;
   5261 		  alignment_power = h->root.u.c.p->alignment_power;
   5262 		}
   5263 	      else
   5264 		{
   5265 		  size = 0;
   5266 		  alignment_power = 0;
   5267 		}
   5268 	      /* Preserve non_ir_ref_dynamic so that this symbol
   5269 		 will be exported when the dynamic lib becomes needed
   5270 		 in the second pass.  */
   5271 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
   5272 	      memcpy (p, old_ent, htab->root.table.entsize);
   5273 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   5274 	      h = (struct elf_link_hash_entry *) p;
   5275 	      if (h->root.type == bfd_link_hash_warning)
   5276 		{
   5277 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
   5278 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   5279 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5280 		}
   5281 	      if (h->root.type == bfd_link_hash_common)
   5282 		{
   5283 		  if (size > h->root.u.c.size)
   5284 		    h->root.u.c.size = size;
   5285 		  if (alignment_power > h->root.u.c.p->alignment_power)
   5286 		    h->root.u.c.p->alignment_power = alignment_power;
   5287 		}
   5288 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
   5289 	    }
   5290 	}
   5291 
   5292       /* Make a special call to the linker "notice" function to
   5293 	 tell it that symbols added for crefs may need to be removed.  */
   5294       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   5295 	goto error_free_vers;
   5296 
   5297       free (old_tab);
   5298       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   5299 			   alloc_mark);
   5300       if (nondeflt_vers != NULL)
   5301 	free (nondeflt_vers);
   5302       return TRUE;
   5303     }
   5304 
   5305   if (old_tab != NULL)
   5306     {
   5307       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   5308 	goto error_free_vers;
   5309       free (old_tab);
   5310       old_tab = NULL;
   5311     }
   5312 
   5313   /* Now that all the symbols from this input file are created, if
   5314      not performing a relocatable link, handle .symver foo, foo@BAR
   5315      such that any relocs against foo become foo@BAR.  */
   5316   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   5317     {
   5318       size_t cnt, symidx;
   5319 
   5320       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   5321 	{
   5322 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   5323 	  char *shortname, *p;
   5324 
   5325 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   5326 	  if (p == NULL
   5327 	      || (h->root.type != bfd_link_hash_defined
   5328 		  && h->root.type != bfd_link_hash_defweak))
   5329 	    continue;
   5330 
   5331 	  amt = p - h->root.root.string;
   5332 	  shortname = (char *) bfd_malloc (amt + 1);
   5333 	  if (!shortname)
   5334 	    goto error_free_vers;
   5335 	  memcpy (shortname, h->root.root.string, amt);
   5336 	  shortname[amt] = '\0';
   5337 
   5338 	  hi = (struct elf_link_hash_entry *)
   5339 	       bfd_link_hash_lookup (&htab->root, shortname,
   5340 				     FALSE, FALSE, FALSE);
   5341 	  if (hi != NULL
   5342 	      && hi->root.type == h->root.type
   5343 	      && hi->root.u.def.value == h->root.u.def.value
   5344 	      && hi->root.u.def.section == h->root.u.def.section)
   5345 	    {
   5346 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   5347 	      hi->root.type = bfd_link_hash_indirect;
   5348 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   5349 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   5350 	      sym_hash = elf_sym_hashes (abfd);
   5351 	      if (sym_hash)
   5352 		for (symidx = 0; symidx < extsymcount; ++symidx)
   5353 		  if (sym_hash[symidx] == hi)
   5354 		    {
   5355 		      sym_hash[symidx] = h;
   5356 		      break;
   5357 		    }
   5358 	    }
   5359 	  free (shortname);
   5360 	}
   5361       free (nondeflt_vers);
   5362       nondeflt_vers = NULL;
   5363     }
   5364 
   5365   /* Now set the alias field correctly for all the weak defined
   5366      symbols we found.  The only way to do this is to search all the
   5367      symbols.  Since we only need the information for non functions in
   5368      dynamic objects, that's the only time we actually put anything on
   5369      the list WEAKS.  We need this information so that if a regular
   5370      object refers to a symbol defined weakly in a dynamic object, the
   5371      real symbol in the dynamic object is also put in the dynamic
   5372      symbols; we also must arrange for both symbols to point to the
   5373      same memory location.  We could handle the general case of symbol
   5374      aliasing, but a general symbol alias can only be generated in
   5375      assembler code, handling it correctly would be very time
   5376      consuming, and other ELF linkers don't handle general aliasing
   5377      either.  */
   5378   if (weaks != NULL)
   5379     {
   5380       struct elf_link_hash_entry **hpp;
   5381       struct elf_link_hash_entry **hppend;
   5382       struct elf_link_hash_entry **sorted_sym_hash;
   5383       struct elf_link_hash_entry *h;
   5384       size_t sym_count;
   5385 
   5386       /* Since we have to search the whole symbol list for each weak
   5387 	 defined symbol, search time for N weak defined symbols will be
   5388 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   5389       amt = extsymcount;
   5390       amt *= sizeof (*sorted_sym_hash);
   5391       sorted_sym_hash = bfd_malloc (amt);
   5392       if (sorted_sym_hash == NULL)
   5393 	goto error_return;
   5394       sym_hash = sorted_sym_hash;
   5395       hpp = elf_sym_hashes (abfd);
   5396       hppend = hpp + extsymcount;
   5397       sym_count = 0;
   5398       for (; hpp < hppend; hpp++)
   5399 	{
   5400 	  h = *hpp;
   5401 	  if (h != NULL
   5402 	      && h->root.type == bfd_link_hash_defined
   5403 	      && !bed->is_function_type (h->type))
   5404 	    {
   5405 	      *sym_hash = h;
   5406 	      sym_hash++;
   5407 	      sym_count++;
   5408 	    }
   5409 	}
   5410 
   5411       qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
   5412 	     elf_sort_symbol);
   5413 
   5414       while (weaks != NULL)
   5415 	{
   5416 	  struct elf_link_hash_entry *hlook;
   5417 	  asection *slook;
   5418 	  bfd_vma vlook;
   5419 	  size_t i, j, idx = 0;
   5420 
   5421 	  hlook = weaks;
   5422 	  weaks = hlook->u.alias;
   5423 	  hlook->u.alias = NULL;
   5424 
   5425 	  if (hlook->root.type != bfd_link_hash_defined
   5426 	      && hlook->root.type != bfd_link_hash_defweak)
   5427 	    continue;
   5428 
   5429 	  slook = hlook->root.u.def.section;
   5430 	  vlook = hlook->root.u.def.value;
   5431 
   5432 	  i = 0;
   5433 	  j = sym_count;
   5434 	  while (i != j)
   5435 	    {
   5436 	      bfd_signed_vma vdiff;
   5437 	      idx = (i + j) / 2;
   5438 	      h = sorted_sym_hash[idx];
   5439 	      vdiff = vlook - h->root.u.def.value;
   5440 	      if (vdiff < 0)
   5441 		j = idx;
   5442 	      else if (vdiff > 0)
   5443 		i = idx + 1;
   5444 	      else
   5445 		{
   5446 		  int sdiff = slook->id - h->root.u.def.section->id;
   5447 		  if (sdiff < 0)
   5448 		    j = idx;
   5449 		  else if (sdiff > 0)
   5450 		    i = idx + 1;
   5451 		  else
   5452 		    break;
   5453 		}
   5454 	    }
   5455 
   5456 	  /* We didn't find a value/section match.  */
   5457 	  if (i == j)
   5458 	    continue;
   5459 
   5460 	  /* With multiple aliases, or when the weak symbol is already
   5461 	     strongly defined, we have multiple matching symbols and
   5462 	     the binary search above may land on any of them.  Step
   5463 	     one past the matching symbol(s).  */
   5464 	  while (++idx != j)
   5465 	    {
   5466 	      h = sorted_sym_hash[idx];
   5467 	      if (h->root.u.def.section != slook
   5468 		  || h->root.u.def.value != vlook)
   5469 		break;
   5470 	    }
   5471 
   5472 	  /* Now look back over the aliases.  Since we sorted by size
   5473 	     as well as value and section, we'll choose the one with
   5474 	     the largest size.  */
   5475 	  while (idx-- != i)
   5476 	    {
   5477 	      h = sorted_sym_hash[idx];
   5478 
   5479 	      /* Stop if value or section doesn't match.  */
   5480 	      if (h->root.u.def.section != slook
   5481 		  || h->root.u.def.value != vlook)
   5482 		break;
   5483 	      else if (h != hlook)
   5484 		{
   5485 		  struct elf_link_hash_entry *t;
   5486 
   5487 		  hlook->u.alias = h;
   5488 		  hlook->is_weakalias = 1;
   5489 		  t = h;
   5490 		  if (t->u.alias != NULL)
   5491 		    while (t->u.alias != h)
   5492 		      t = t->u.alias;
   5493 		  t->u.alias = hlook;
   5494 
   5495 		  /* If the weak definition is in the list of dynamic
   5496 		     symbols, make sure the real definition is put
   5497 		     there as well.  */
   5498 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   5499 		    {
   5500 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5501 			{
   5502 			err_free_sym_hash:
   5503 			  free (sorted_sym_hash);
   5504 			  goto error_return;
   5505 			}
   5506 		    }
   5507 
   5508 		  /* If the real definition is in the list of dynamic
   5509 		     symbols, make sure the weak definition is put
   5510 		     there as well.  If we don't do this, then the
   5511 		     dynamic loader might not merge the entries for the
   5512 		     real definition and the weak definition.  */
   5513 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   5514 		    {
   5515 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   5516 			goto err_free_sym_hash;
   5517 		    }
   5518 		  break;
   5519 		}
   5520 	    }
   5521 	}
   5522 
   5523       free (sorted_sym_hash);
   5524     }
   5525 
   5526   if (bed->check_directives
   5527       && !(*bed->check_directives) (abfd, info))
   5528     return FALSE;
   5529 
   5530   /* If this is a non-traditional link, try to optimize the handling
   5531      of the .stab/.stabstr sections.  */
   5532   if (! dynamic
   5533       && ! info->traditional_format
   5534       && is_elf_hash_table (htab)
   5535       && (info->strip != strip_all && info->strip != strip_debugger))
   5536     {
   5537       asection *stabstr;
   5538 
   5539       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   5540       if (stabstr != NULL)
   5541 	{
   5542 	  bfd_size_type string_offset = 0;
   5543 	  asection *stab;
   5544 
   5545 	  for (stab = abfd->sections; stab; stab = stab->next)
   5546 	    if (CONST_STRNEQ (stab->name, ".stab")
   5547 		&& (!stab->name[5] ||
   5548 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   5549 		&& (stab->flags & SEC_MERGE) == 0
   5550 		&& !bfd_is_abs_section (stab->output_section))
   5551 	      {
   5552 		struct bfd_elf_section_data *secdata;
   5553 
   5554 		secdata = elf_section_data (stab);
   5555 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   5556 					       stabstr, &secdata->sec_info,
   5557 					       &string_offset))
   5558 		  goto error_return;
   5559 		if (secdata->sec_info)
   5560 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   5561 	    }
   5562 	}
   5563     }
   5564 
   5565   if (is_elf_hash_table (htab) && add_needed)
   5566     {
   5567       /* Add this bfd to the loaded list.  */
   5568       struct elf_link_loaded_list *n;
   5569 
   5570       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   5571       if (n == NULL)
   5572 	goto error_return;
   5573       n->abfd = abfd;
   5574       n->next = htab->loaded;
   5575       htab->loaded = n;
   5576     }
   5577 
   5578   return TRUE;
   5579 
   5580  error_free_vers:
   5581   if (old_tab != NULL)
   5582     free (old_tab);
   5583   if (old_strtab != NULL)
   5584     free (old_strtab);
   5585   if (nondeflt_vers != NULL)
   5586     free (nondeflt_vers);
   5587   if (extversym != NULL)
   5588     free (extversym);
   5589  error_free_sym:
   5590   if (isymbuf != NULL)
   5591     free (isymbuf);
   5592  error_return:
   5593   return FALSE;
   5594 }
   5595 
   5596 /* Return the linker hash table entry of a symbol that might be
   5597    satisfied by an archive symbol.  Return -1 on error.  */
   5598 
   5599 struct elf_link_hash_entry *
   5600 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   5601 				struct bfd_link_info *info,
   5602 				const char *name)
   5603 {
   5604   struct elf_link_hash_entry *h;
   5605   char *p, *copy;
   5606   size_t len, first;
   5607 
   5608   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
   5609   if (h != NULL)
   5610     return h;
   5611 
   5612   /* If this is a default version (the name contains @@), look up the
   5613      symbol again with only one `@' as well as without the version.
   5614      The effect is that references to the symbol with and without the
   5615      version will be matched by the default symbol in the archive.  */
   5616 
   5617   p = strchr (name, ELF_VER_CHR);
   5618   if (p == NULL || p[1] != ELF_VER_CHR)
   5619     return h;
   5620 
   5621   /* First check with only one `@'.  */
   5622   len = strlen (name);
   5623   copy = (char *) bfd_alloc (abfd, len);
   5624   if (copy == NULL)
   5625     return (struct elf_link_hash_entry *) -1;
   5626 
   5627   first = p - name + 1;
   5628   memcpy (copy, name, first);
   5629   memcpy (copy + first, name + first + 1, len - first);
   5630 
   5631   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
   5632   if (h == NULL)
   5633     {
   5634       /* We also need to check references to the symbol without the
   5635 	 version.  */
   5636       copy[first - 1] = '\0';
   5637       h = elf_link_hash_lookup (elf_hash_table (info), copy,
   5638 				FALSE, FALSE, TRUE);
   5639     }
   5640 
   5641   bfd_release (abfd, copy);
   5642   return h;
   5643 }
   5644 
   5645 /* Add symbols from an ELF archive file to the linker hash table.  We
   5646    don't use _bfd_generic_link_add_archive_symbols because we need to
   5647    handle versioned symbols.
   5648 
   5649    Fortunately, ELF archive handling is simpler than that done by
   5650    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   5651    oddities.  In ELF, if we find a symbol in the archive map, and the
   5652    symbol is currently undefined, we know that we must pull in that
   5653    object file.
   5654 
   5655    Unfortunately, we do have to make multiple passes over the symbol
   5656    table until nothing further is resolved.  */
   5657 
   5658 static bfd_boolean
   5659 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   5660 {
   5661   symindex c;
   5662   unsigned char *included = NULL;
   5663   carsym *symdefs;
   5664   bfd_boolean loop;
   5665   bfd_size_type amt;
   5666   const struct elf_backend_data *bed;
   5667   struct elf_link_hash_entry * (*archive_symbol_lookup)
   5668     (bfd *, struct bfd_link_info *, const char *);
   5669 
   5670   if (! bfd_has_map (abfd))
   5671     {
   5672       /* An empty archive is a special case.  */
   5673       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   5674 	return TRUE;
   5675       bfd_set_error (bfd_error_no_armap);
   5676       return FALSE;
   5677     }
   5678 
   5679   /* Keep track of all symbols we know to be already defined, and all
   5680      files we know to be already included.  This is to speed up the
   5681      second and subsequent passes.  */
   5682   c = bfd_ardata (abfd)->symdef_count;
   5683   if (c == 0)
   5684     return TRUE;
   5685   amt = c;
   5686   amt *= sizeof (*included);
   5687   included = (unsigned char *) bfd_zmalloc (amt);
   5688   if (included == NULL)
   5689     return FALSE;
   5690 
   5691   symdefs = bfd_ardata (abfd)->symdefs;
   5692   bed = get_elf_backend_data (abfd);
   5693   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   5694 
   5695   do
   5696     {
   5697       file_ptr last;
   5698       symindex i;
   5699       carsym *symdef;
   5700       carsym *symdefend;
   5701 
   5702       loop = FALSE;
   5703       last = -1;
   5704 
   5705       symdef = symdefs;
   5706       symdefend = symdef + c;
   5707       for (i = 0; symdef < symdefend; symdef++, i++)
   5708 	{
   5709 	  struct elf_link_hash_entry *h;
   5710 	  bfd *element;
   5711 	  struct bfd_link_hash_entry *undefs_tail;
   5712 	  symindex mark;
   5713 
   5714 	  if (included[i])
   5715 	    continue;
   5716 	  if (symdef->file_offset == last)
   5717 	    {
   5718 	      included[i] = TRUE;
   5719 	      continue;
   5720 	    }
   5721 
   5722 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   5723 	  if (h == (struct elf_link_hash_entry *) -1)
   5724 	    goto error_return;
   5725 
   5726 	  if (h == NULL)
   5727 	    continue;
   5728 
   5729 	  if (h->root.type == bfd_link_hash_common)
   5730 	    {
   5731 	      /* We currently have a common symbol.  The archive map contains
   5732 		 a reference to this symbol, so we may want to include it.  We
   5733 		 only want to include it however, if this archive element
   5734 		 contains a definition of the symbol, not just another common
   5735 		 declaration of it.
   5736 
   5737 		 Unfortunately some archivers (including GNU ar) will put
   5738 		 declarations of common symbols into their archive maps, as
   5739 		 well as real definitions, so we cannot just go by the archive
   5740 		 map alone.  Instead we must read in the element's symbol
   5741 		 table and check that to see what kind of symbol definition
   5742 		 this is.  */
   5743 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   5744 		continue;
   5745 	    }
   5746 	  else if (h->root.type != bfd_link_hash_undefined)
   5747 	    {
   5748 	      if (h->root.type != bfd_link_hash_undefweak)
   5749 		/* Symbol must be defined.  Don't check it again.  */
   5750 		included[i] = TRUE;
   5751 	      continue;
   5752 	    }
   5753 
   5754 	  /* We need to include this archive member.  */
   5755 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   5756 	  if (element == NULL)
   5757 	    goto error_return;
   5758 
   5759 	  if (! bfd_check_format (element, bfd_object))
   5760 	    goto error_return;
   5761 
   5762 	  undefs_tail = info->hash->undefs_tail;
   5763 
   5764 	  if (!(*info->callbacks
   5765 		->add_archive_element) (info, element, symdef->name, &element))
   5766 	    continue;
   5767 	  if (!bfd_link_add_symbols (element, info))
   5768 	    goto error_return;
   5769 
   5770 	  /* If there are any new undefined symbols, we need to make
   5771 	     another pass through the archive in order to see whether
   5772 	     they can be defined.  FIXME: This isn't perfect, because
   5773 	     common symbols wind up on undefs_tail and because an
   5774 	     undefined symbol which is defined later on in this pass
   5775 	     does not require another pass.  This isn't a bug, but it
   5776 	     does make the code less efficient than it could be.  */
   5777 	  if (undefs_tail != info->hash->undefs_tail)
   5778 	    loop = TRUE;
   5779 
   5780 	  /* Look backward to mark all symbols from this object file
   5781 	     which we have already seen in this pass.  */
   5782 	  mark = i;
   5783 	  do
   5784 	    {
   5785 	      included[mark] = TRUE;
   5786 	      if (mark == 0)
   5787 		break;
   5788 	      --mark;
   5789 	    }
   5790 	  while (symdefs[mark].file_offset == symdef->file_offset);
   5791 
   5792 	  /* We mark subsequent symbols from this object file as we go
   5793 	     on through the loop.  */
   5794 	  last = symdef->file_offset;
   5795 	}
   5796     }
   5797   while (loop);
   5798 
   5799   free (included);
   5800 
   5801   return TRUE;
   5802 
   5803  error_return:
   5804   if (included != NULL)
   5805     free (included);
   5806   return FALSE;
   5807 }
   5808 
   5809 /* Given an ELF BFD, add symbols to the global hash table as
   5810    appropriate.  */
   5811 
   5812 bfd_boolean
   5813 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   5814 {
   5815   switch (bfd_get_format (abfd))
   5816     {
   5817     case bfd_object:
   5818       return elf_link_add_object_symbols (abfd, info);
   5819     case bfd_archive:
   5820       return elf_link_add_archive_symbols (abfd, info);
   5821     default:
   5822       bfd_set_error (bfd_error_wrong_format);
   5823       return FALSE;
   5824     }
   5825 }
   5826 
   5827 struct hash_codes_info
   5829 {
   5830   unsigned long *hashcodes;
   5831   bfd_boolean error;
   5832 };
   5833 
   5834 /* This function will be called though elf_link_hash_traverse to store
   5835    all hash value of the exported symbols in an array.  */
   5836 
   5837 static bfd_boolean
   5838 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   5839 {
   5840   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   5841   const char *name;
   5842   unsigned long ha;
   5843   char *alc = NULL;
   5844 
   5845   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5846   if (h->dynindx == -1)
   5847     return TRUE;
   5848 
   5849   name = h->root.root.string;
   5850   if (h->versioned >= versioned)
   5851     {
   5852       char *p = strchr (name, ELF_VER_CHR);
   5853       if (p != NULL)
   5854 	{
   5855 	  alc = (char *) bfd_malloc (p - name + 1);
   5856 	  if (alc == NULL)
   5857 	    {
   5858 	      inf->error = TRUE;
   5859 	      return FALSE;
   5860 	    }
   5861 	  memcpy (alc, name, p - name);
   5862 	  alc[p - name] = '\0';
   5863 	  name = alc;
   5864 	}
   5865     }
   5866 
   5867   /* Compute the hash value.  */
   5868   ha = bfd_elf_hash (name);
   5869 
   5870   /* Store the found hash value in the array given as the argument.  */
   5871   *(inf->hashcodes)++ = ha;
   5872 
   5873   /* And store it in the struct so that we can put it in the hash table
   5874      later.  */
   5875   h->u.elf_hash_value = ha;
   5876 
   5877   if (alc != NULL)
   5878     free (alc);
   5879 
   5880   return TRUE;
   5881 }
   5882 
   5883 struct collect_gnu_hash_codes
   5884 {
   5885   bfd *output_bfd;
   5886   const struct elf_backend_data *bed;
   5887   unsigned long int nsyms;
   5888   unsigned long int maskbits;
   5889   unsigned long int *hashcodes;
   5890   unsigned long int *hashval;
   5891   unsigned long int *indx;
   5892   unsigned long int *counts;
   5893   bfd_vma *bitmask;
   5894   bfd_byte *contents;
   5895   bfd_size_type xlat;
   5896   long int min_dynindx;
   5897   unsigned long int bucketcount;
   5898   unsigned long int symindx;
   5899   long int local_indx;
   5900   long int shift1, shift2;
   5901   unsigned long int mask;
   5902   bfd_boolean error;
   5903 };
   5904 
   5905 /* This function will be called though elf_link_hash_traverse to store
   5906    all hash value of the exported symbols in an array.  */
   5907 
   5908 static bfd_boolean
   5909 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   5910 {
   5911   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5912   const char *name;
   5913   unsigned long ha;
   5914   char *alc = NULL;
   5915 
   5916   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5917   if (h->dynindx == -1)
   5918     return TRUE;
   5919 
   5920   /* Ignore also local symbols and undefined symbols.  */
   5921   if (! (*s->bed->elf_hash_symbol) (h))
   5922     return TRUE;
   5923 
   5924   name = h->root.root.string;
   5925   if (h->versioned >= versioned)
   5926     {
   5927       char *p = strchr (name, ELF_VER_CHR);
   5928       if (p != NULL)
   5929 	{
   5930 	  alc = (char *) bfd_malloc (p - name + 1);
   5931 	  if (alc == NULL)
   5932 	    {
   5933 	      s->error = TRUE;
   5934 	      return FALSE;
   5935 	    }
   5936 	  memcpy (alc, name, p - name);
   5937 	  alc[p - name] = '\0';
   5938 	  name = alc;
   5939 	}
   5940     }
   5941 
   5942   /* Compute the hash value.  */
   5943   ha = bfd_elf_gnu_hash (name);
   5944 
   5945   /* Store the found hash value in the array for compute_bucket_count,
   5946      and also for .dynsym reordering purposes.  */
   5947   s->hashcodes[s->nsyms] = ha;
   5948   s->hashval[h->dynindx] = ha;
   5949   ++s->nsyms;
   5950   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   5951     s->min_dynindx = h->dynindx;
   5952 
   5953   if (alc != NULL)
   5954     free (alc);
   5955 
   5956   return TRUE;
   5957 }
   5958 
   5959 /* This function will be called though elf_link_hash_traverse to do
   5960    final dynamic symbol renumbering in case of .gnu.hash.
   5961    If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
   5962    to the translation table.  */
   5963 
   5964 static bfd_boolean
   5965 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
   5966 {
   5967   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5968   unsigned long int bucket;
   5969   unsigned long int val;
   5970 
   5971   /* Ignore indirect symbols.  */
   5972   if (h->dynindx == -1)
   5973     return TRUE;
   5974 
   5975   /* Ignore also local symbols and undefined symbols.  */
   5976   if (! (*s->bed->elf_hash_symbol) (h))
   5977     {
   5978       if (h->dynindx >= s->min_dynindx)
   5979 	{
   5980 	  if (s->bed->record_xhash_symbol != NULL)
   5981 	    {
   5982 	      (*s->bed->record_xhash_symbol) (h, 0);
   5983 	      s->local_indx++;
   5984 	    }
   5985 	  else
   5986 	    h->dynindx = s->local_indx++;
   5987 	}
   5988       return TRUE;
   5989     }
   5990 
   5991   bucket = s->hashval[h->dynindx] % s->bucketcount;
   5992   val = (s->hashval[h->dynindx] >> s->shift1)
   5993 	& ((s->maskbits >> s->shift1) - 1);
   5994   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   5995   s->bitmask[val]
   5996     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   5997   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   5998   if (s->counts[bucket] == 1)
   5999     /* Last element terminates the chain.  */
   6000     val |= 1;
   6001   bfd_put_32 (s->output_bfd, val,
   6002 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   6003   --s->counts[bucket];
   6004   if (s->bed->record_xhash_symbol != NULL)
   6005     {
   6006       bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
   6007 
   6008       (*s->bed->record_xhash_symbol) (h, xlat_loc);
   6009     }
   6010   else
   6011     h->dynindx = s->indx[bucket]++;
   6012   return TRUE;
   6013 }
   6014 
   6015 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   6016 
   6017 bfd_boolean
   6018 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   6019 {
   6020   return !(h->forced_local
   6021 	   || h->root.type == bfd_link_hash_undefined
   6022 	   || h->root.type == bfd_link_hash_undefweak
   6023 	   || ((h->root.type == bfd_link_hash_defined
   6024 		|| h->root.type == bfd_link_hash_defweak)
   6025 	       && h->root.u.def.section->output_section == NULL));
   6026 }
   6027 
   6028 /* Array used to determine the number of hash table buckets to use
   6029    based on the number of symbols there are.  If there are fewer than
   6030    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   6031    fewer than 37 we use 17 buckets, and so forth.  We never use more
   6032    than 32771 buckets.  */
   6033 
   6034 static const size_t elf_buckets[] =
   6035 {
   6036   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   6037   16411, 32771, 0
   6038 };
   6039 
   6040 /* Compute bucket count for hashing table.  We do not use a static set
   6041    of possible tables sizes anymore.  Instead we determine for all
   6042    possible reasonable sizes of the table the outcome (i.e., the
   6043    number of collisions etc) and choose the best solution.  The
   6044    weighting functions are not too simple to allow the table to grow
   6045    without bounds.  Instead one of the weighting factors is the size.
   6046    Therefore the result is always a good payoff between few collisions
   6047    (= short chain lengths) and table size.  */
   6048 static size_t
   6049 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6050 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   6051 		      unsigned long int nsyms,
   6052 		      int gnu_hash)
   6053 {
   6054   size_t best_size = 0;
   6055   unsigned long int i;
   6056 
   6057   /* We have a problem here.  The following code to optimize the table
   6058      size requires an integer type with more the 32 bits.  If
   6059      BFD_HOST_U_64_BIT is set we know about such a type.  */
   6060 #ifdef BFD_HOST_U_64_BIT
   6061   if (info->optimize)
   6062     {
   6063       size_t minsize;
   6064       size_t maxsize;
   6065       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
   6066       bfd *dynobj = elf_hash_table (info)->dynobj;
   6067       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   6068       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   6069       unsigned long int *counts;
   6070       bfd_size_type amt;
   6071       unsigned int no_improvement_count = 0;
   6072 
   6073       /* Possible optimization parameters: if we have NSYMS symbols we say
   6074 	 that the hashing table must at least have NSYMS/4 and at most
   6075 	 2*NSYMS buckets.  */
   6076       minsize = nsyms / 4;
   6077       if (minsize == 0)
   6078 	minsize = 1;
   6079       best_size = maxsize = nsyms * 2;
   6080       if (gnu_hash)
   6081 	{
   6082 	  if (minsize < 2)
   6083 	    minsize = 2;
   6084 	  if ((best_size & 31) == 0)
   6085 	    ++best_size;
   6086 	}
   6087 
   6088       /* Create array where we count the collisions in.  We must use bfd_malloc
   6089 	 since the size could be large.  */
   6090       amt = maxsize;
   6091       amt *= sizeof (unsigned long int);
   6092       counts = (unsigned long int *) bfd_malloc (amt);
   6093       if (counts == NULL)
   6094 	return 0;
   6095 
   6096       /* Compute the "optimal" size for the hash table.  The criteria is a
   6097 	 minimal chain length.  The minor criteria is (of course) the size
   6098 	 of the table.  */
   6099       for (i = minsize; i < maxsize; ++i)
   6100 	{
   6101 	  /* Walk through the array of hashcodes and count the collisions.  */
   6102 	  BFD_HOST_U_64_BIT max;
   6103 	  unsigned long int j;
   6104 	  unsigned long int fact;
   6105 
   6106 	  if (gnu_hash && (i & 31) == 0)
   6107 	    continue;
   6108 
   6109 	  memset (counts, '\0', i * sizeof (unsigned long int));
   6110 
   6111 	  /* Determine how often each hash bucket is used.  */
   6112 	  for (j = 0; j < nsyms; ++j)
   6113 	    ++counts[hashcodes[j] % i];
   6114 
   6115 	  /* For the weight function we need some information about the
   6116 	     pagesize on the target.  This is information need not be 100%
   6117 	     accurate.  Since this information is not available (so far) we
   6118 	     define it here to a reasonable default value.  If it is crucial
   6119 	     to have a better value some day simply define this value.  */
   6120 # ifndef BFD_TARGET_PAGESIZE
   6121 #  define BFD_TARGET_PAGESIZE	(4096)
   6122 # endif
   6123 
   6124 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   6125 	     and the chains.  */
   6126 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   6127 
   6128 # if 1
   6129 	  /* Variant 1: optimize for short chains.  We add the squares
   6130 	     of all the chain lengths (which favors many small chain
   6131 	     over a few long chains).  */
   6132 	  for (j = 0; j < i; ++j)
   6133 	    max += counts[j] * counts[j];
   6134 
   6135 	  /* This adds penalties for the overall size of the table.  */
   6136 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6137 	  max *= fact * fact;
   6138 # else
   6139 	  /* Variant 2: Optimize a lot more for small table.  Here we
   6140 	     also add squares of the size but we also add penalties for
   6141 	     empty slots (the +1 term).  */
   6142 	  for (j = 0; j < i; ++j)
   6143 	    max += (1 + counts[j]) * (1 + counts[j]);
   6144 
   6145 	  /* The overall size of the table is considered, but not as
   6146 	     strong as in variant 1, where it is squared.  */
   6147 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   6148 	  max *= fact;
   6149 # endif
   6150 
   6151 	  /* Compare with current best results.  */
   6152 	  if (max < best_chlen)
   6153 	    {
   6154 	      best_chlen = max;
   6155 	      best_size = i;
   6156 	      no_improvement_count = 0;
   6157 	    }
   6158 	  /* PR 11843: Avoid futile long searches for the best bucket size
   6159 	     when there are a large number of symbols.  */
   6160 	  else if (++no_improvement_count == 100)
   6161 	    break;
   6162 	}
   6163 
   6164       free (counts);
   6165     }
   6166   else
   6167 #endif /* defined (BFD_HOST_U_64_BIT) */
   6168     {
   6169       /* This is the fallback solution if no 64bit type is available or if we
   6170 	 are not supposed to spend much time on optimizations.  We select the
   6171 	 bucket count using a fixed set of numbers.  */
   6172       for (i = 0; elf_buckets[i] != 0; i++)
   6173 	{
   6174 	  best_size = elf_buckets[i];
   6175 	  if (nsyms < elf_buckets[i + 1])
   6176 	    break;
   6177 	}
   6178       if (gnu_hash && best_size < 2)
   6179 	best_size = 2;
   6180     }
   6181 
   6182   return best_size;
   6183 }
   6184 
   6185 /* Size any SHT_GROUP section for ld -r.  */
   6186 
   6187 bfd_boolean
   6188 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   6189 {
   6190   bfd *ibfd;
   6191   asection *s;
   6192 
   6193   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6194     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   6195 	&& (s = ibfd->sections) != NULL
   6196 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
   6197 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   6198       return FALSE;
   6199   return TRUE;
   6200 }
   6201 
   6202 /* Set a default stack segment size.  The value in INFO wins.  If it
   6203    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   6204    undefined it is initialized.  */
   6205 
   6206 bfd_boolean
   6207 bfd_elf_stack_segment_size (bfd *output_bfd,
   6208 			    struct bfd_link_info *info,
   6209 			    const char *legacy_symbol,
   6210 			    bfd_vma default_size)
   6211 {
   6212   struct elf_link_hash_entry *h = NULL;
   6213 
   6214   /* Look for legacy symbol.  */
   6215   if (legacy_symbol)
   6216     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   6217 			      FALSE, FALSE, FALSE);
   6218   if (h && (h->root.type == bfd_link_hash_defined
   6219 	    || h->root.type == bfd_link_hash_defweak)
   6220       && h->def_regular
   6221       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   6222     {
   6223       /* The symbol has no type if specified on the command line.  */
   6224       h->type = STT_OBJECT;
   6225       if (info->stacksize)
   6226 	/* xgettext:c-format */
   6227 	_bfd_error_handler (_("%pB: stack size specified and %s set"),
   6228 			    output_bfd, legacy_symbol);
   6229       else if (h->root.u.def.section != bfd_abs_section_ptr)
   6230 	/* xgettext:c-format */
   6231 	_bfd_error_handler (_("%pB: %s not absolute"),
   6232 			    output_bfd, legacy_symbol);
   6233       else
   6234 	info->stacksize = h->root.u.def.value;
   6235     }
   6236 
   6237   if (!info->stacksize)
   6238     /* If the user didn't set a size, or explicitly inhibit the
   6239        size, set it now.  */
   6240     info->stacksize = default_size;
   6241 
   6242   /* Provide the legacy symbol, if it is referenced.  */
   6243   if (h && (h->root.type == bfd_link_hash_undefined
   6244 	    || h->root.type == bfd_link_hash_undefweak))
   6245     {
   6246       struct bfd_link_hash_entry *bh = NULL;
   6247 
   6248       if (!(_bfd_generic_link_add_one_symbol
   6249 	    (info, output_bfd, legacy_symbol,
   6250 	     BSF_GLOBAL, bfd_abs_section_ptr,
   6251 	     info->stacksize >= 0 ? info->stacksize : 0,
   6252 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
   6253 	return FALSE;
   6254 
   6255       h = (struct elf_link_hash_entry *) bh;
   6256       h->def_regular = 1;
   6257       h->type = STT_OBJECT;
   6258     }
   6259 
   6260   return TRUE;
   6261 }
   6262 
   6263 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   6264 
   6265 struct elf_gc_sweep_symbol_info
   6266 {
   6267   struct bfd_link_info *info;
   6268   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   6269 		       bfd_boolean);
   6270 };
   6271 
   6272 static bfd_boolean
   6273 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   6274 {
   6275   if (!h->mark
   6276       && (((h->root.type == bfd_link_hash_defined
   6277 	    || h->root.type == bfd_link_hash_defweak)
   6278 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   6279 		&& h->root.u.def.section->gc_mark))
   6280 	  || h->root.type == bfd_link_hash_undefined
   6281 	  || h->root.type == bfd_link_hash_undefweak))
   6282     {
   6283       struct elf_gc_sweep_symbol_info *inf;
   6284 
   6285       inf = (struct elf_gc_sweep_symbol_info *) data;
   6286       (*inf->hide_symbol) (inf->info, h, TRUE);
   6287       h->def_regular = 0;
   6288       h->ref_regular = 0;
   6289       h->ref_regular_nonweak = 0;
   6290     }
   6291 
   6292   return TRUE;
   6293 }
   6294 
   6295 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   6296    called by the ELF linker emulation before_allocation routine.  We
   6297    must set the sizes of the sections before the linker sets the
   6298    addresses of the various sections.  */
   6299 
   6300 bfd_boolean
   6301 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   6302 			       const char *soname,
   6303 			       const char *rpath,
   6304 			       const char *filter_shlib,
   6305 			       const char *audit,
   6306 			       const char *depaudit,
   6307 			       const char * const *auxiliary_filters,
   6308 			       struct bfd_link_info *info,
   6309 			       asection **sinterpptr)
   6310 {
   6311   bfd *dynobj;
   6312   const struct elf_backend_data *bed;
   6313 
   6314   *sinterpptr = NULL;
   6315 
   6316   if (!is_elf_hash_table (info->hash))
   6317     return TRUE;
   6318 
   6319   dynobj = elf_hash_table (info)->dynobj;
   6320 
   6321   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6322     {
   6323       struct bfd_elf_version_tree *verdefs;
   6324       struct elf_info_failed asvinfo;
   6325       struct bfd_elf_version_tree *t;
   6326       struct bfd_elf_version_expr *d;
   6327       asection *s;
   6328       size_t soname_indx;
   6329 
   6330       /* If we are supposed to export all symbols into the dynamic symbol
   6331 	 table (this is not the normal case), then do so.  */
   6332       if (info->export_dynamic
   6333 	  || (bfd_link_executable (info) && info->dynamic))
   6334 	{
   6335 	  struct elf_info_failed eif;
   6336 
   6337 	  eif.info = info;
   6338 	  eif.failed = FALSE;
   6339 	  elf_link_hash_traverse (elf_hash_table (info),
   6340 				  _bfd_elf_export_symbol,
   6341 				  &eif);
   6342 	  if (eif.failed)
   6343 	    return FALSE;
   6344 	}
   6345 
   6346       if (soname != NULL)
   6347 	{
   6348 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6349 					     soname, TRUE);
   6350 	  if (soname_indx == (size_t) -1
   6351 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   6352 	    return FALSE;
   6353 	}
   6354       else
   6355 	soname_indx = (size_t) -1;
   6356 
   6357       /* Make all global versions with definition.  */
   6358       for (t = info->version_info; t != NULL; t = t->next)
   6359 	for (d = t->globals.list; d != NULL; d = d->next)
   6360 	  if (!d->symver && d->literal)
   6361 	    {
   6362 	      const char *verstr, *name;
   6363 	      size_t namelen, verlen, newlen;
   6364 	      char *newname, *p, leading_char;
   6365 	      struct elf_link_hash_entry *newh;
   6366 
   6367 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   6368 	      name = d->pattern;
   6369 	      namelen = strlen (name) + (leading_char != '\0');
   6370 	      verstr = t->name;
   6371 	      verlen = strlen (verstr);
   6372 	      newlen = namelen + verlen + 3;
   6373 
   6374 	      newname = (char *) bfd_malloc (newlen);
   6375 	      if (newname == NULL)
   6376 		return FALSE;
   6377 	      newname[0] = leading_char;
   6378 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   6379 
   6380 	      /* Check the hidden versioned definition.  */
   6381 	      p = newname + namelen;
   6382 	      *p++ = ELF_VER_CHR;
   6383 	      memcpy (p, verstr, verlen + 1);
   6384 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   6385 					   newname, FALSE, FALSE,
   6386 					   FALSE);
   6387 	      if (newh == NULL
   6388 		  || (newh->root.type != bfd_link_hash_defined
   6389 		      && newh->root.type != bfd_link_hash_defweak))
   6390 		{
   6391 		  /* Check the default versioned definition.  */
   6392 		  *p++ = ELF_VER_CHR;
   6393 		  memcpy (p, verstr, verlen + 1);
   6394 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   6395 					       newname, FALSE, FALSE,
   6396 					       FALSE);
   6397 		}
   6398 	      free (newname);
   6399 
   6400 	      /* Mark this version if there is a definition and it is
   6401 		 not defined in a shared object.  */
   6402 	      if (newh != NULL
   6403 		  && !newh->def_dynamic
   6404 		  && (newh->root.type == bfd_link_hash_defined
   6405 		      || newh->root.type == bfd_link_hash_defweak))
   6406 		d->symver = 1;
   6407 	    }
   6408 
   6409       /* Attach all the symbols to their version information.  */
   6410       asvinfo.info = info;
   6411       asvinfo.failed = FALSE;
   6412 
   6413       elf_link_hash_traverse (elf_hash_table (info),
   6414 			      _bfd_elf_link_assign_sym_version,
   6415 			      &asvinfo);
   6416       if (asvinfo.failed)
   6417 	return FALSE;
   6418 
   6419       if (!info->allow_undefined_version)
   6420 	{
   6421 	  /* Check if all global versions have a definition.  */
   6422 	  bfd_boolean all_defined = TRUE;
   6423 	  for (t = info->version_info; t != NULL; t = t->next)
   6424 	    for (d = t->globals.list; d != NULL; d = d->next)
   6425 	      if (d->literal && !d->symver && !d->script)
   6426 		{
   6427 		  _bfd_error_handler
   6428 		    (_("%s: undefined version: %s"),
   6429 		     d->pattern, t->name);
   6430 		  all_defined = FALSE;
   6431 		}
   6432 
   6433 	  if (!all_defined)
   6434 	    {
   6435 	      bfd_set_error (bfd_error_bad_value);
   6436 	      return FALSE;
   6437 	    }
   6438 	}
   6439 
   6440       /* Set up the version definition section.  */
   6441       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   6442       BFD_ASSERT (s != NULL);
   6443 
   6444       /* We may have created additional version definitions if we are
   6445 	 just linking a regular application.  */
   6446       verdefs = info->version_info;
   6447 
   6448       /* Skip anonymous version tag.  */
   6449       if (verdefs != NULL && verdefs->vernum == 0)
   6450 	verdefs = verdefs->next;
   6451 
   6452       if (verdefs == NULL && !info->create_default_symver)
   6453 	s->flags |= SEC_EXCLUDE;
   6454       else
   6455 	{
   6456 	  unsigned int cdefs;
   6457 	  bfd_size_type size;
   6458 	  bfd_byte *p;
   6459 	  Elf_Internal_Verdef def;
   6460 	  Elf_Internal_Verdaux defaux;
   6461 	  struct bfd_link_hash_entry *bh;
   6462 	  struct elf_link_hash_entry *h;
   6463 	  const char *name;
   6464 
   6465 	  cdefs = 0;
   6466 	  size = 0;
   6467 
   6468 	  /* Make space for the base version.  */
   6469 	  size += sizeof (Elf_External_Verdef);
   6470 	  size += sizeof (Elf_External_Verdaux);
   6471 	  ++cdefs;
   6472 
   6473 	  /* Make space for the default version.  */
   6474 	  if (info->create_default_symver)
   6475 	    {
   6476 	      size += sizeof (Elf_External_Verdef);
   6477 	      ++cdefs;
   6478 	    }
   6479 
   6480 	  for (t = verdefs; t != NULL; t = t->next)
   6481 	    {
   6482 	      struct bfd_elf_version_deps *n;
   6483 
   6484 	      /* Don't emit base version twice.  */
   6485 	      if (t->vernum == 0)
   6486 		continue;
   6487 
   6488 	      size += sizeof (Elf_External_Verdef);
   6489 	      size += sizeof (Elf_External_Verdaux);
   6490 	      ++cdefs;
   6491 
   6492 	      for (n = t->deps; n != NULL; n = n->next)
   6493 		size += sizeof (Elf_External_Verdaux);
   6494 	    }
   6495 
   6496 	  s->size = size;
   6497 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6498 	  if (s->contents == NULL && s->size != 0)
   6499 	    return FALSE;
   6500 
   6501 	  /* Fill in the version definition section.  */
   6502 
   6503 	  p = s->contents;
   6504 
   6505 	  def.vd_version = VER_DEF_CURRENT;
   6506 	  def.vd_flags = VER_FLG_BASE;
   6507 	  def.vd_ndx = 1;
   6508 	  def.vd_cnt = 1;
   6509 	  if (info->create_default_symver)
   6510 	    {
   6511 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   6512 	      def.vd_next = sizeof (Elf_External_Verdef);
   6513 	    }
   6514 	  else
   6515 	    {
   6516 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6517 	      def.vd_next = (sizeof (Elf_External_Verdef)
   6518 			     + sizeof (Elf_External_Verdaux));
   6519 	    }
   6520 
   6521 	  if (soname_indx != (size_t) -1)
   6522 	    {
   6523 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6524 				      soname_indx);
   6525 	      def.vd_hash = bfd_elf_hash (soname);
   6526 	      defaux.vda_name = soname_indx;
   6527 	      name = soname;
   6528 	    }
   6529 	  else
   6530 	    {
   6531 	      size_t indx;
   6532 
   6533 	      name = lbasename (output_bfd->filename);
   6534 	      def.vd_hash = bfd_elf_hash (name);
   6535 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6536 					  name, FALSE);
   6537 	      if (indx == (size_t) -1)
   6538 		return FALSE;
   6539 	      defaux.vda_name = indx;
   6540 	    }
   6541 	  defaux.vda_next = 0;
   6542 
   6543 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   6544 				    (Elf_External_Verdef *) p);
   6545 	  p += sizeof (Elf_External_Verdef);
   6546 	  if (info->create_default_symver)
   6547 	    {
   6548 	      /* Add a symbol representing this version.  */
   6549 	      bh = NULL;
   6550 	      if (! (_bfd_generic_link_add_one_symbol
   6551 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   6552 		      0, NULL, FALSE,
   6553 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6554 		return FALSE;
   6555 	      h = (struct elf_link_hash_entry *) bh;
   6556 	      h->non_elf = 0;
   6557 	      h->def_regular = 1;
   6558 	      h->type = STT_OBJECT;
   6559 	      h->verinfo.vertree = NULL;
   6560 
   6561 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6562 		return FALSE;
   6563 
   6564 	      /* Create a duplicate of the base version with the same
   6565 		 aux block, but different flags.  */
   6566 	      def.vd_flags = 0;
   6567 	      def.vd_ndx = 2;
   6568 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6569 	      if (verdefs)
   6570 		def.vd_next = (sizeof (Elf_External_Verdef)
   6571 			       + sizeof (Elf_External_Verdaux));
   6572 	      else
   6573 		def.vd_next = 0;
   6574 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6575 					(Elf_External_Verdef *) p);
   6576 	      p += sizeof (Elf_External_Verdef);
   6577 	    }
   6578 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6579 				     (Elf_External_Verdaux *) p);
   6580 	  p += sizeof (Elf_External_Verdaux);
   6581 
   6582 	  for (t = verdefs; t != NULL; t = t->next)
   6583 	    {
   6584 	      unsigned int cdeps;
   6585 	      struct bfd_elf_version_deps *n;
   6586 
   6587 	      /* Don't emit the base version twice.  */
   6588 	      if (t->vernum == 0)
   6589 		continue;
   6590 
   6591 	      cdeps = 0;
   6592 	      for (n = t->deps; n != NULL; n = n->next)
   6593 		++cdeps;
   6594 
   6595 	      /* Add a symbol representing this version.  */
   6596 	      bh = NULL;
   6597 	      if (! (_bfd_generic_link_add_one_symbol
   6598 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   6599 		      0, NULL, FALSE,
   6600 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6601 		return FALSE;
   6602 	      h = (struct elf_link_hash_entry *) bh;
   6603 	      h->non_elf = 0;
   6604 	      h->def_regular = 1;
   6605 	      h->type = STT_OBJECT;
   6606 	      h->verinfo.vertree = t;
   6607 
   6608 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6609 		return FALSE;
   6610 
   6611 	      def.vd_version = VER_DEF_CURRENT;
   6612 	      def.vd_flags = 0;
   6613 	      if (t->globals.list == NULL
   6614 		  && t->locals.list == NULL
   6615 		  && ! t->used)
   6616 		def.vd_flags |= VER_FLG_WEAK;
   6617 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   6618 	      def.vd_cnt = cdeps + 1;
   6619 	      def.vd_hash = bfd_elf_hash (t->name);
   6620 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6621 	      def.vd_next = 0;
   6622 
   6623 	      /* If a basever node is next, it *must* be the last node in
   6624 		 the chain, otherwise Verdef construction breaks.  */
   6625 	      if (t->next != NULL && t->next->vernum == 0)
   6626 		BFD_ASSERT (t->next->next == NULL);
   6627 
   6628 	      if (t->next != NULL && t->next->vernum != 0)
   6629 		def.vd_next = (sizeof (Elf_External_Verdef)
   6630 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   6631 
   6632 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6633 					(Elf_External_Verdef *) p);
   6634 	      p += sizeof (Elf_External_Verdef);
   6635 
   6636 	      defaux.vda_name = h->dynstr_index;
   6637 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6638 				      h->dynstr_index);
   6639 	      defaux.vda_next = 0;
   6640 	      if (t->deps != NULL)
   6641 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   6642 	      t->name_indx = defaux.vda_name;
   6643 
   6644 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6645 					 (Elf_External_Verdaux *) p);
   6646 	      p += sizeof (Elf_External_Verdaux);
   6647 
   6648 	      for (n = t->deps; n != NULL; n = n->next)
   6649 		{
   6650 		  if (n->version_needed == NULL)
   6651 		    {
   6652 		      /* This can happen if there was an error in the
   6653 			 version script.  */
   6654 		      defaux.vda_name = 0;
   6655 		    }
   6656 		  else
   6657 		    {
   6658 		      defaux.vda_name = n->version_needed->name_indx;
   6659 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6660 					      defaux.vda_name);
   6661 		    }
   6662 		  if (n->next == NULL)
   6663 		    defaux.vda_next = 0;
   6664 		  else
   6665 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   6666 
   6667 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6668 					     (Elf_External_Verdaux *) p);
   6669 		  p += sizeof (Elf_External_Verdaux);
   6670 		}
   6671 	    }
   6672 
   6673 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   6674 	}
   6675     }
   6676 
   6677   bed = get_elf_backend_data (output_bfd);
   6678 
   6679   if (info->gc_sections && bed->can_gc_sections)
   6680     {
   6681       struct elf_gc_sweep_symbol_info sweep_info;
   6682 
   6683       /* Remove the symbols that were in the swept sections from the
   6684 	 dynamic symbol table.  */
   6685       sweep_info.info = info;
   6686       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   6687       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   6688 			      &sweep_info);
   6689     }
   6690 
   6691   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6692     {
   6693       asection *s;
   6694       struct elf_find_verdep_info sinfo;
   6695 
   6696       /* Work out the size of the version reference section.  */
   6697 
   6698       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   6699       BFD_ASSERT (s != NULL);
   6700 
   6701       sinfo.info = info;
   6702       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   6703       if (sinfo.vers == 0)
   6704 	sinfo.vers = 1;
   6705       sinfo.failed = FALSE;
   6706 
   6707       elf_link_hash_traverse (elf_hash_table (info),
   6708 			      _bfd_elf_link_find_version_dependencies,
   6709 			      &sinfo);
   6710       if (sinfo.failed)
   6711 	return FALSE;
   6712 
   6713       if (elf_tdata (output_bfd)->verref == NULL)
   6714 	s->flags |= SEC_EXCLUDE;
   6715       else
   6716 	{
   6717 	  Elf_Internal_Verneed *vn;
   6718 	  unsigned int size;
   6719 	  unsigned int crefs;
   6720 	  bfd_byte *p;
   6721 
   6722 	  /* Build the version dependency section.  */
   6723 	  size = 0;
   6724 	  crefs = 0;
   6725 	  for (vn = elf_tdata (output_bfd)->verref;
   6726 	       vn != NULL;
   6727 	       vn = vn->vn_nextref)
   6728 	    {
   6729 	      Elf_Internal_Vernaux *a;
   6730 
   6731 	      size += sizeof (Elf_External_Verneed);
   6732 	      ++crefs;
   6733 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6734 		size += sizeof (Elf_External_Vernaux);
   6735 	    }
   6736 
   6737 	  s->size = size;
   6738 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6739 	  if (s->contents == NULL)
   6740 	    return FALSE;
   6741 
   6742 	  p = s->contents;
   6743 	  for (vn = elf_tdata (output_bfd)->verref;
   6744 	       vn != NULL;
   6745 	       vn = vn->vn_nextref)
   6746 	    {
   6747 	      unsigned int caux;
   6748 	      Elf_Internal_Vernaux *a;
   6749 	      size_t indx;
   6750 
   6751 	      caux = 0;
   6752 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6753 		++caux;
   6754 
   6755 	      vn->vn_version = VER_NEED_CURRENT;
   6756 	      vn->vn_cnt = caux;
   6757 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6758 					  elf_dt_name (vn->vn_bfd) != NULL
   6759 					  ? elf_dt_name (vn->vn_bfd)
   6760 					  : lbasename (vn->vn_bfd->filename),
   6761 					  FALSE);
   6762 	      if (indx == (size_t) -1)
   6763 		return FALSE;
   6764 	      vn->vn_file = indx;
   6765 	      vn->vn_aux = sizeof (Elf_External_Verneed);
   6766 	      if (vn->vn_nextref == NULL)
   6767 		vn->vn_next = 0;
   6768 	      else
   6769 		vn->vn_next = (sizeof (Elf_External_Verneed)
   6770 			       + caux * sizeof (Elf_External_Vernaux));
   6771 
   6772 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
   6773 					 (Elf_External_Verneed *) p);
   6774 	      p += sizeof (Elf_External_Verneed);
   6775 
   6776 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6777 		{
   6778 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
   6779 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6780 					      a->vna_nodename, FALSE);
   6781 		  if (indx == (size_t) -1)
   6782 		    return FALSE;
   6783 		  a->vna_name = indx;
   6784 		  if (a->vna_nextptr == NULL)
   6785 		    a->vna_next = 0;
   6786 		  else
   6787 		    a->vna_next = sizeof (Elf_External_Vernaux);
   6788 
   6789 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
   6790 					     (Elf_External_Vernaux *) p);
   6791 		  p += sizeof (Elf_External_Vernaux);
   6792 		}
   6793 	    }
   6794 
   6795 	  elf_tdata (output_bfd)->cverrefs = crefs;
   6796 	}
   6797     }
   6798 
   6799   /* Any syms created from now on start with -1 in
   6800      got.refcount/offset and plt.refcount/offset.  */
   6801   elf_hash_table (info)->init_got_refcount
   6802     = elf_hash_table (info)->init_got_offset;
   6803   elf_hash_table (info)->init_plt_refcount
   6804     = elf_hash_table (info)->init_plt_offset;
   6805 
   6806   if (bfd_link_relocatable (info)
   6807       && !_bfd_elf_size_group_sections (info))
   6808     return FALSE;
   6809 
   6810   /* The backend may have to create some sections regardless of whether
   6811      we're dynamic or not.  */
   6812   if (bed->elf_backend_always_size_sections
   6813       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
   6814     return FALSE;
   6815 
   6816   /* Determine any GNU_STACK segment requirements, after the backend
   6817      has had a chance to set a default segment size.  */
   6818   if (info->execstack)
   6819     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   6820   else if (info->noexecstack)
   6821     elf_stack_flags (output_bfd) = PF_R | PF_W;
   6822   else
   6823     {
   6824       bfd *inputobj;
   6825       asection *notesec = NULL;
   6826       int exec = 0;
   6827 
   6828       for (inputobj = info->input_bfds;
   6829 	   inputobj;
   6830 	   inputobj = inputobj->link.next)
   6831 	{
   6832 	  asection *s;
   6833 
   6834 	  if (inputobj->flags
   6835 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   6836 	    continue;
   6837 	  s = inputobj->sections;
   6838 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   6839 	    continue;
   6840 
   6841 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   6842 	  if (s)
   6843 	    {
   6844 	      if (s->flags & SEC_CODE)
   6845 		exec = PF_X;
   6846 	      notesec = s;
   6847 	    }
   6848 	  else if (bed->default_execstack)
   6849 	    exec = PF_X;
   6850 	}
   6851       if (notesec || info->stacksize > 0)
   6852 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   6853       if (notesec && exec && bfd_link_relocatable (info)
   6854 	  && notesec->output_section != bfd_abs_section_ptr)
   6855 	notesec->output_section->flags |= SEC_CODE;
   6856     }
   6857 
   6858   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6859     {
   6860       struct elf_info_failed eif;
   6861       struct elf_link_hash_entry *h;
   6862       asection *dynstr;
   6863       asection *s;
   6864 
   6865       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   6866       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   6867 
   6868       if (info->symbolic)
   6869 	{
   6870 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   6871 	    return FALSE;
   6872 	  info->flags |= DF_SYMBOLIC;
   6873 	}
   6874 
   6875       if (rpath != NULL)
   6876 	{
   6877 	  size_t indx;
   6878 	  bfd_vma tag;
   6879 
   6880 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   6881 				      TRUE);
   6882 	  if (indx == (size_t) -1)
   6883 	    return FALSE;
   6884 
   6885 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   6886 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   6887 	    return FALSE;
   6888 	}
   6889 
   6890       if (filter_shlib != NULL)
   6891 	{
   6892 	  size_t indx;
   6893 
   6894 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6895 				      filter_shlib, TRUE);
   6896 	  if (indx == (size_t) -1
   6897 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   6898 	    return FALSE;
   6899 	}
   6900 
   6901       if (auxiliary_filters != NULL)
   6902 	{
   6903 	  const char * const *p;
   6904 
   6905 	  for (p = auxiliary_filters; *p != NULL; p++)
   6906 	    {
   6907 	      size_t indx;
   6908 
   6909 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6910 					  *p, TRUE);
   6911 	      if (indx == (size_t) -1
   6912 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   6913 		return FALSE;
   6914 	    }
   6915 	}
   6916 
   6917       if (audit != NULL)
   6918 	{
   6919 	  size_t indx;
   6920 
   6921 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   6922 				      TRUE);
   6923 	  if (indx == (size_t) -1
   6924 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   6925 	    return FALSE;
   6926 	}
   6927 
   6928       if (depaudit != NULL)
   6929 	{
   6930 	  size_t indx;
   6931 
   6932 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   6933 				      TRUE);
   6934 	  if (indx == (size_t) -1
   6935 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   6936 	    return FALSE;
   6937 	}
   6938 
   6939       eif.info = info;
   6940       eif.failed = FALSE;
   6941 
   6942       /* Find all symbols which were defined in a dynamic object and make
   6943 	 the backend pick a reasonable value for them.  */
   6944       elf_link_hash_traverse (elf_hash_table (info),
   6945 			      _bfd_elf_adjust_dynamic_symbol,
   6946 			      &eif);
   6947       if (eif.failed)
   6948 	return FALSE;
   6949 
   6950       /* Add some entries to the .dynamic section.  We fill in some of the
   6951 	 values later, in bfd_elf_final_link, but we must add the entries
   6952 	 now so that we know the final size of the .dynamic section.  */
   6953 
   6954       /* If there are initialization and/or finalization functions to
   6955 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   6956       h = (info->init_function
   6957 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6958 				   info->init_function, FALSE,
   6959 				   FALSE, FALSE)
   6960 	   : NULL);
   6961       if (h != NULL
   6962 	  && (h->ref_regular
   6963 	      || h->def_regular))
   6964 	{
   6965 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   6966 	    return FALSE;
   6967 	}
   6968       h = (info->fini_function
   6969 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6970 				   info->fini_function, FALSE,
   6971 				   FALSE, FALSE)
   6972 	   : NULL);
   6973       if (h != NULL
   6974 	  && (h->ref_regular
   6975 	      || h->def_regular))
   6976 	{
   6977 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   6978 	    return FALSE;
   6979 	}
   6980 
   6981       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   6982       if (s != NULL && s->linker_has_input)
   6983 	{
   6984 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   6985 	  if (! bfd_link_executable (info))
   6986 	    {
   6987 	      bfd *sub;
   6988 	      asection *o;
   6989 
   6990 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   6991 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   6992 		    && (o = sub->sections) != NULL
   6993 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
   6994 		  for (o = sub->sections; o != NULL; o = o->next)
   6995 		    if (elf_section_data (o)->this_hdr.sh_type
   6996 			== SHT_PREINIT_ARRAY)
   6997 		      {
   6998 			_bfd_error_handler
   6999 			  (_("%pB: .preinit_array section is not allowed in DSO"),
   7000 			   sub);
   7001 			break;
   7002 		      }
   7003 
   7004 	      bfd_set_error (bfd_error_nonrepresentable_section);
   7005 	      return FALSE;
   7006 	    }
   7007 
   7008 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   7009 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   7010 	    return FALSE;
   7011 	}
   7012       s = bfd_get_section_by_name (output_bfd, ".init_array");
   7013       if (s != NULL && s->linker_has_input)
   7014 	{
   7015 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   7016 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   7017 	    return FALSE;
   7018 	}
   7019       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   7020       if (s != NULL && s->linker_has_input)
   7021 	{
   7022 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   7023 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   7024 	    return FALSE;
   7025 	}
   7026 
   7027       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   7028       /* If .dynstr is excluded from the link, we don't want any of
   7029 	 these tags.  Strictly, we should be checking each section
   7030 	 individually;  This quick check covers for the case where
   7031 	 someone does a /DISCARD/ : { *(*) }.  */
   7032       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   7033 	{
   7034 	  bfd_size_type strsize;
   7035 
   7036 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7037 	  if ((info->emit_hash
   7038 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   7039 	      || (info->emit_gnu_hash
   7040 		  && (bed->record_xhash_symbol == NULL
   7041 		      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
   7042 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   7043 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   7044 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   7045 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   7046 					      bed->s->sizeof_sym))
   7047 	    return FALSE;
   7048 	}
   7049     }
   7050 
   7051   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   7052     return FALSE;
   7053 
   7054   /* The backend must work out the sizes of all the other dynamic
   7055      sections.  */
   7056   if (dynobj != NULL
   7057       && bed->elf_backend_size_dynamic_sections != NULL
   7058       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
   7059     return FALSE;
   7060 
   7061   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   7062     {
   7063       if (elf_tdata (output_bfd)->cverdefs)
   7064 	{
   7065 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
   7066 
   7067 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   7068 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
   7069 	    return FALSE;
   7070 	}
   7071 
   7072       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   7073 	{
   7074 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   7075 	    return FALSE;
   7076 	}
   7077       else if (info->flags & DF_BIND_NOW)
   7078 	{
   7079 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   7080 	    return FALSE;
   7081 	}
   7082 
   7083       if (info->flags_1)
   7084 	{
   7085 	  if (bfd_link_executable (info))
   7086 	    info->flags_1 &= ~ (DF_1_INITFIRST
   7087 				| DF_1_NODELETE
   7088 				| DF_1_NOOPEN);
   7089 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   7090 	    return FALSE;
   7091 	}
   7092 
   7093       if (elf_tdata (output_bfd)->cverrefs)
   7094 	{
   7095 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
   7096 
   7097 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   7098 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   7099 	    return FALSE;
   7100 	}
   7101 
   7102       if ((elf_tdata (output_bfd)->cverrefs == 0
   7103 	   && elf_tdata (output_bfd)->cverdefs == 0)
   7104 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
   7105 	{
   7106 	  asection *s;
   7107 
   7108 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   7109 	  s->flags |= SEC_EXCLUDE;
   7110 	}
   7111     }
   7112   return TRUE;
   7113 }
   7114 
   7115 /* Find the first non-excluded output section.  We'll use its
   7116    section symbol for some emitted relocs.  */
   7117 void
   7118 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   7119 {
   7120   asection *s;
   7121   asection *found = NULL;
   7122 
   7123   for (s = output_bfd->sections; s != NULL; s = s->next)
   7124     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7125 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7126       {
   7127 	found = s;
   7128 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7129 	  break;
   7130       }
   7131   elf_hash_table (info)->text_index_section = found;
   7132 }
   7133 
   7134 /* Find two non-excluded output sections, one for code, one for data.
   7135    We'll use their section symbols for some emitted relocs.  */
   7136 void
   7137 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   7138 {
   7139   asection *s;
   7140   asection *found = NULL;
   7141 
   7142   /* Data first, since setting text_index_section changes
   7143      _bfd_elf_omit_section_dynsym_default.  */
   7144   for (s = output_bfd->sections; s != NULL; s = s->next)
   7145     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7146 	&& !(s->flags & SEC_READONLY)
   7147 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7148       {
   7149 	found = s;
   7150 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
   7151 	  break;
   7152       }
   7153   elf_hash_table (info)->data_index_section = found;
   7154 
   7155   for (s = output_bfd->sections; s != NULL; s = s->next)
   7156     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   7157 	&& (s->flags & SEC_READONLY)
   7158 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
   7159       {
   7160 	found = s;
   7161 	break;
   7162       }
   7163   elf_hash_table (info)->text_index_section = found;
   7164 }
   7165 
   7166 #define GNU_HASH_SECTION_NAME(bed)			    \
   7167   (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
   7168 
   7169 bfd_boolean
   7170 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   7171 {
   7172   const struct elf_backend_data *bed;
   7173   unsigned long section_sym_count;
   7174   bfd_size_type dynsymcount = 0;
   7175 
   7176   if (!is_elf_hash_table (info->hash))
   7177     return TRUE;
   7178 
   7179   bed = get_elf_backend_data (output_bfd);
   7180   (*bed->elf_backend_init_index_section) (output_bfd, info);
   7181 
   7182   /* Assign dynsym indices.  In a shared library we generate a section
   7183      symbol for each output section, which come first.  Next come all
   7184      of the back-end allocated local dynamic syms, followed by the rest
   7185      of the global symbols.
   7186 
   7187      This is usually not needed for static binaries, however backends
   7188      can request to always do it, e.g. the MIPS backend uses dynamic
   7189      symbol counts to lay out GOT, which will be produced in the
   7190      presence of GOT relocations even in static binaries (holding fixed
   7191      data in that case, to satisfy those relocations).  */
   7192 
   7193   if (elf_hash_table (info)->dynamic_sections_created
   7194       || bed->always_renumber_dynsyms)
   7195     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   7196 						  &section_sym_count);
   7197 
   7198   if (elf_hash_table (info)->dynamic_sections_created)
   7199     {
   7200       bfd *dynobj;
   7201       asection *s;
   7202       unsigned int dtagcount;
   7203 
   7204       dynobj = elf_hash_table (info)->dynobj;
   7205 
   7206       /* Work out the size of the symbol version section.  */
   7207       s = bfd_get_linker_section (dynobj, ".gnu.version");
   7208       BFD_ASSERT (s != NULL);
   7209       if ((s->flags & SEC_EXCLUDE) == 0)
   7210 	{
   7211 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   7212 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7213 	  if (s->contents == NULL)
   7214 	    return FALSE;
   7215 
   7216 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   7217 	    return FALSE;
   7218 	}
   7219 
   7220       /* Set the size of the .dynsym and .hash sections.  We counted
   7221 	 the number of dynamic symbols in elf_link_add_object_symbols.
   7222 	 We will build the contents of .dynsym and .hash when we build
   7223 	 the final symbol table, because until then we do not know the
   7224 	 correct value to give the symbols.  We built the .dynstr
   7225 	 section as we went along in elf_link_add_object_symbols.  */
   7226       s = elf_hash_table (info)->dynsym;
   7227       BFD_ASSERT (s != NULL);
   7228       s->size = dynsymcount * bed->s->sizeof_sym;
   7229 
   7230       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7231       if (s->contents == NULL)
   7232 	return FALSE;
   7233 
   7234       /* The first entry in .dynsym is a dummy symbol.  Clear all the
   7235 	 section syms, in case we don't output them all.  */
   7236       ++section_sym_count;
   7237       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   7238 
   7239       elf_hash_table (info)->bucketcount = 0;
   7240 
   7241       /* Compute the size of the hashing table.  As a side effect this
   7242 	 computes the hash values for all the names we export.  */
   7243       if (info->emit_hash)
   7244 	{
   7245 	  unsigned long int *hashcodes;
   7246 	  struct hash_codes_info hashinf;
   7247 	  bfd_size_type amt;
   7248 	  unsigned long int nsyms;
   7249 	  size_t bucketcount;
   7250 	  size_t hash_entry_size;
   7251 
   7252 	  /* Compute the hash values for all exported symbols.  At the same
   7253 	     time store the values in an array so that we could use them for
   7254 	     optimizations.  */
   7255 	  amt = dynsymcount * sizeof (unsigned long int);
   7256 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   7257 	  if (hashcodes == NULL)
   7258 	    return FALSE;
   7259 	  hashinf.hashcodes = hashcodes;
   7260 	  hashinf.error = FALSE;
   7261 
   7262 	  /* Put all hash values in HASHCODES.  */
   7263 	  elf_link_hash_traverse (elf_hash_table (info),
   7264 				  elf_collect_hash_codes, &hashinf);
   7265 	  if (hashinf.error)
   7266 	    {
   7267 	      free (hashcodes);
   7268 	      return FALSE;
   7269 	    }
   7270 
   7271 	  nsyms = hashinf.hashcodes - hashcodes;
   7272 	  bucketcount
   7273 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   7274 	  free (hashcodes);
   7275 
   7276 	  if (bucketcount == 0 && nsyms > 0)
   7277 	    return FALSE;
   7278 
   7279 	  elf_hash_table (info)->bucketcount = bucketcount;
   7280 
   7281 	  s = bfd_get_linker_section (dynobj, ".hash");
   7282 	  BFD_ASSERT (s != NULL);
   7283 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   7284 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   7285 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7286 	  if (s->contents == NULL)
   7287 	    return FALSE;
   7288 
   7289 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   7290 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   7291 		   s->contents + hash_entry_size);
   7292 	}
   7293 
   7294       if (info->emit_gnu_hash)
   7295 	{
   7296 	  size_t i, cnt;
   7297 	  unsigned char *contents;
   7298 	  struct collect_gnu_hash_codes cinfo;
   7299 	  bfd_size_type amt;
   7300 	  size_t bucketcount;
   7301 
   7302 	  memset (&cinfo, 0, sizeof (cinfo));
   7303 
   7304 	  /* Compute the hash values for all exported symbols.  At the same
   7305 	     time store the values in an array so that we could use them for
   7306 	     optimizations.  */
   7307 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   7308 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   7309 	  if (cinfo.hashcodes == NULL)
   7310 	    return FALSE;
   7311 
   7312 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   7313 	  cinfo.min_dynindx = -1;
   7314 	  cinfo.output_bfd = output_bfd;
   7315 	  cinfo.bed = bed;
   7316 
   7317 	  /* Put all hash values in HASHCODES.  */
   7318 	  elf_link_hash_traverse (elf_hash_table (info),
   7319 				  elf_collect_gnu_hash_codes, &cinfo);
   7320 	  if (cinfo.error)
   7321 	    {
   7322 	      free (cinfo.hashcodes);
   7323 	      return FALSE;
   7324 	    }
   7325 
   7326 	  bucketcount
   7327 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   7328 
   7329 	  if (bucketcount == 0)
   7330 	    {
   7331 	      free (cinfo.hashcodes);
   7332 	      return FALSE;
   7333 	    }
   7334 
   7335 	  s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
   7336 	  BFD_ASSERT (s != NULL);
   7337 
   7338 	  if (cinfo.nsyms == 0)
   7339 	    {
   7340 	      /* Empty .gnu.hash or .MIPS.xhash section is special.  */
   7341 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   7342 	      free (cinfo.hashcodes);
   7343 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   7344 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7345 	      if (contents == NULL)
   7346 		return FALSE;
   7347 	      s->contents = contents;
   7348 	      /* 1 empty bucket.  */
   7349 	      bfd_put_32 (output_bfd, 1, contents);
   7350 	      /* SYMIDX above the special symbol 0.  */
   7351 	      bfd_put_32 (output_bfd, 1, contents + 4);
   7352 	      /* Just one word for bitmask.  */
   7353 	      bfd_put_32 (output_bfd, 1, contents + 8);
   7354 	      /* Only hash fn bloom filter.  */
   7355 	      bfd_put_32 (output_bfd, 0, contents + 12);
   7356 	      /* No hashes are valid - empty bitmask.  */
   7357 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   7358 	      /* No hashes in the only bucket.  */
   7359 	      bfd_put_32 (output_bfd, 0,
   7360 			  contents + 16 + bed->s->arch_size / 8);
   7361 	    }
   7362 	  else
   7363 	    {
   7364 	      unsigned long int maskwords, maskbitslog2, x;
   7365 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   7366 
   7367 	      x = cinfo.nsyms;
   7368 	      maskbitslog2 = 1;
   7369 	      while ((x >>= 1) != 0)
   7370 		++maskbitslog2;
   7371 	      if (maskbitslog2 < 3)
   7372 		maskbitslog2 = 5;
   7373 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   7374 		maskbitslog2 = maskbitslog2 + 3;
   7375 	      else
   7376 		maskbitslog2 = maskbitslog2 + 2;
   7377 	      if (bed->s->arch_size == 64)
   7378 		{
   7379 		  if (maskbitslog2 == 5)
   7380 		    maskbitslog2 = 6;
   7381 		  cinfo.shift1 = 6;
   7382 		}
   7383 	      else
   7384 		cinfo.shift1 = 5;
   7385 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   7386 	      cinfo.shift2 = maskbitslog2;
   7387 	      cinfo.maskbits = 1 << maskbitslog2;
   7388 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   7389 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   7390 	      amt += maskwords * sizeof (bfd_vma);
   7391 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   7392 	      if (cinfo.bitmask == NULL)
   7393 		{
   7394 		  free (cinfo.hashcodes);
   7395 		  return FALSE;
   7396 		}
   7397 
   7398 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   7399 	      cinfo.indx = cinfo.counts + bucketcount;
   7400 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   7401 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   7402 
   7403 	      /* Determine how often each hash bucket is used.  */
   7404 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   7405 	      for (i = 0; i < cinfo.nsyms; ++i)
   7406 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   7407 
   7408 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   7409 		if (cinfo.counts[i] != 0)
   7410 		  {
   7411 		    cinfo.indx[i] = cnt;
   7412 		    cnt += cinfo.counts[i];
   7413 		  }
   7414 	      BFD_ASSERT (cnt == dynsymcount);
   7415 	      cinfo.bucketcount = bucketcount;
   7416 	      cinfo.local_indx = cinfo.min_dynindx;
   7417 
   7418 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   7419 	      s->size += cinfo.maskbits / 8;
   7420 	      if (bed->record_xhash_symbol != NULL)
   7421 		s->size += cinfo.nsyms * 4;
   7422 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7423 	      if (contents == NULL)
   7424 		{
   7425 		  free (cinfo.bitmask);
   7426 		  free (cinfo.hashcodes);
   7427 		  return FALSE;
   7428 		}
   7429 
   7430 	      s->contents = contents;
   7431 	      bfd_put_32 (output_bfd, bucketcount, contents);
   7432 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   7433 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   7434 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   7435 	      contents += 16 + cinfo.maskbits / 8;
   7436 
   7437 	      for (i = 0; i < bucketcount; ++i)
   7438 		{
   7439 		  if (cinfo.counts[i] == 0)
   7440 		    bfd_put_32 (output_bfd, 0, contents);
   7441 		  else
   7442 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   7443 		  contents += 4;
   7444 		}
   7445 
   7446 	      cinfo.contents = contents;
   7447 
   7448 	      cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
   7449 	      /* Renumber dynamic symbols, if populating .gnu.hash section.
   7450 		 If using .MIPS.xhash, populate the translation table.  */
   7451 	      elf_link_hash_traverse (elf_hash_table (info),
   7452 				      elf_gnu_hash_process_symidx, &cinfo);
   7453 
   7454 	      contents = s->contents + 16;
   7455 	      for (i = 0; i < maskwords; ++i)
   7456 		{
   7457 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   7458 			   contents);
   7459 		  contents += bed->s->arch_size / 8;
   7460 		}
   7461 
   7462 	      free (cinfo.bitmask);
   7463 	      free (cinfo.hashcodes);
   7464 	    }
   7465 	}
   7466 
   7467       s = bfd_get_linker_section (dynobj, ".dynstr");
   7468       BFD_ASSERT (s != NULL);
   7469 
   7470       elf_finalize_dynstr (output_bfd, info);
   7471 
   7472       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7473 
   7474       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   7475 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   7476 	  return FALSE;
   7477     }
   7478 
   7479   return TRUE;
   7480 }
   7481 
   7482 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   7484 
   7485 static void
   7486 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   7487 			    asection *sec)
   7488 {
   7489   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   7490   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   7491 }
   7492 
   7493 /* Finish SHF_MERGE section merging.  */
   7494 
   7495 bfd_boolean
   7496 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
   7497 {
   7498   bfd *ibfd;
   7499   asection *sec;
   7500 
   7501   if (!is_elf_hash_table (info->hash))
   7502     return FALSE;
   7503 
   7504   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7505     if ((ibfd->flags & DYNAMIC) == 0
   7506 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   7507 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   7508 	    == get_elf_backend_data (obfd)->s->elfclass))
   7509       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7510 	if ((sec->flags & SEC_MERGE) != 0
   7511 	    && !bfd_is_abs_section (sec->output_section))
   7512 	  {
   7513 	    struct bfd_elf_section_data *secdata;
   7514 
   7515 	    secdata = elf_section_data (sec);
   7516 	    if (! _bfd_add_merge_section (obfd,
   7517 					  &elf_hash_table (info)->merge_info,
   7518 					  sec, &secdata->sec_info))
   7519 	      return FALSE;
   7520 	    else if (secdata->sec_info)
   7521 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   7522 	  }
   7523 
   7524   if (elf_hash_table (info)->merge_info != NULL)
   7525     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
   7526 			 merge_sections_remove_hook);
   7527   return TRUE;
   7528 }
   7529 
   7530 /* Create an entry in an ELF linker hash table.  */
   7531 
   7532 struct bfd_hash_entry *
   7533 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   7534 			    struct bfd_hash_table *table,
   7535 			    const char *string)
   7536 {
   7537   /* Allocate the structure if it has not already been allocated by a
   7538      subclass.  */
   7539   if (entry == NULL)
   7540     {
   7541       entry = (struct bfd_hash_entry *)
   7542 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   7543       if (entry == NULL)
   7544 	return entry;
   7545     }
   7546 
   7547   /* Call the allocation method of the superclass.  */
   7548   entry = _bfd_link_hash_newfunc (entry, table, string);
   7549   if (entry != NULL)
   7550     {
   7551       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   7552       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   7553 
   7554       /* Set local fields.  */
   7555       ret->indx = -1;
   7556       ret->dynindx = -1;
   7557       ret->got = htab->init_got_refcount;
   7558       ret->plt = htab->init_plt_refcount;
   7559       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   7560 			      - offsetof (struct elf_link_hash_entry, size)));
   7561       /* Assume that we have been called by a non-ELF symbol reader.
   7562 	 This flag is then reset by the code which reads an ELF input
   7563 	 file.  This ensures that a symbol created by a non-ELF symbol
   7564 	 reader will have the flag set correctly.  */
   7565       ret->non_elf = 1;
   7566     }
   7567 
   7568   return entry;
   7569 }
   7570 
   7571 /* Copy data from an indirect symbol to its direct symbol, hiding the
   7572    old indirect symbol.  Also used for copying flags to a weakdef.  */
   7573 
   7574 void
   7575 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   7576 				  struct elf_link_hash_entry *dir,
   7577 				  struct elf_link_hash_entry *ind)
   7578 {
   7579   struct elf_link_hash_table *htab;
   7580 
   7581   /* Copy down any references that we may have already seen to the
   7582      symbol which just became indirect.  */
   7583 
   7584   if (dir->versioned != versioned_hidden)
   7585     dir->ref_dynamic |= ind->ref_dynamic;
   7586   dir->ref_regular |= ind->ref_regular;
   7587   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   7588   dir->non_got_ref |= ind->non_got_ref;
   7589   dir->needs_plt |= ind->needs_plt;
   7590   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   7591 
   7592   if (ind->root.type != bfd_link_hash_indirect)
   7593     return;
   7594 
   7595   /* Copy over the global and procedure linkage table refcount entries.
   7596      These may have been already set up by a check_relocs routine.  */
   7597   htab = elf_hash_table (info);
   7598   if (ind->got.refcount > htab->init_got_refcount.refcount)
   7599     {
   7600       if (dir->got.refcount < 0)
   7601 	dir->got.refcount = 0;
   7602       dir->got.refcount += ind->got.refcount;
   7603       ind->got.refcount = htab->init_got_refcount.refcount;
   7604     }
   7605 
   7606   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   7607     {
   7608       if (dir->plt.refcount < 0)
   7609 	dir->plt.refcount = 0;
   7610       dir->plt.refcount += ind->plt.refcount;
   7611       ind->plt.refcount = htab->init_plt_refcount.refcount;
   7612     }
   7613 
   7614   if (ind->dynindx != -1)
   7615     {
   7616       if (dir->dynindx != -1)
   7617 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   7618       dir->dynindx = ind->dynindx;
   7619       dir->dynstr_index = ind->dynstr_index;
   7620       ind->dynindx = -1;
   7621       ind->dynstr_index = 0;
   7622     }
   7623 }
   7624 
   7625 void
   7626 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   7627 				struct elf_link_hash_entry *h,
   7628 				bfd_boolean force_local)
   7629 {
   7630   /* STT_GNU_IFUNC symbol must go through PLT.  */
   7631   if (h->type != STT_GNU_IFUNC)
   7632     {
   7633       h->plt = elf_hash_table (info)->init_plt_offset;
   7634       h->needs_plt = 0;
   7635     }
   7636   if (force_local)
   7637     {
   7638       h->forced_local = 1;
   7639       if (h->dynindx != -1)
   7640 	{
   7641 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7642 				  h->dynstr_index);
   7643 	  h->dynindx = -1;
   7644 	  h->dynstr_index = 0;
   7645 	}
   7646     }
   7647 }
   7648 
   7649 /* Hide a symbol. */
   7650 
   7651 void
   7652 _bfd_elf_link_hide_symbol (bfd *output_bfd,
   7653 			   struct bfd_link_info *info,
   7654 			   struct bfd_link_hash_entry *h)
   7655 {
   7656   if (is_elf_hash_table (info->hash))
   7657     {
   7658       const struct elf_backend_data *bed
   7659 	= get_elf_backend_data (output_bfd);
   7660       struct elf_link_hash_entry *eh
   7661 	= (struct elf_link_hash_entry *) h;
   7662       bed->elf_backend_hide_symbol (info, eh, TRUE);
   7663       eh->def_dynamic = 0;
   7664       eh->ref_dynamic = 0;
   7665       eh->dynamic_def = 0;
   7666     }
   7667 }
   7668 
   7669 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   7670    caller.  */
   7671 
   7672 bfd_boolean
   7673 _bfd_elf_link_hash_table_init
   7674   (struct elf_link_hash_table *table,
   7675    bfd *abfd,
   7676    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   7677 				      struct bfd_hash_table *,
   7678 				      const char *),
   7679    unsigned int entsize,
   7680    enum elf_target_id target_id)
   7681 {
   7682   bfd_boolean ret;
   7683   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
   7684 
   7685   table->init_got_refcount.refcount = can_refcount - 1;
   7686   table->init_plt_refcount.refcount = can_refcount - 1;
   7687   table->init_got_offset.offset = -(bfd_vma) 1;
   7688   table->init_plt_offset.offset = -(bfd_vma) 1;
   7689   /* The first dynamic symbol is a dummy.  */
   7690   table->dynsymcount = 1;
   7691 
   7692   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   7693 
   7694   table->root.type = bfd_link_elf_hash_table;
   7695   table->hash_table_id = target_id;
   7696 
   7697   return ret;
   7698 }
   7699 
   7700 /* Create an ELF linker hash table.  */
   7701 
   7702 struct bfd_link_hash_table *
   7703 _bfd_elf_link_hash_table_create (bfd *abfd)
   7704 {
   7705   struct elf_link_hash_table *ret;
   7706   bfd_size_type amt = sizeof (struct elf_link_hash_table);
   7707 
   7708   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   7709   if (ret == NULL)
   7710     return NULL;
   7711 
   7712   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   7713 				       sizeof (struct elf_link_hash_entry),
   7714 				       GENERIC_ELF_DATA))
   7715     {
   7716       free (ret);
   7717       return NULL;
   7718     }
   7719   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
   7720 
   7721   return &ret->root;
   7722 }
   7723 
   7724 /* Destroy an ELF linker hash table.  */
   7725 
   7726 void
   7727 _bfd_elf_link_hash_table_free (bfd *obfd)
   7728 {
   7729   struct elf_link_hash_table *htab;
   7730 
   7731   htab = (struct elf_link_hash_table *) obfd->link.hash;
   7732   if (htab->dynstr != NULL)
   7733     _bfd_elf_strtab_free (htab->dynstr);
   7734   _bfd_merge_sections_free (htab->merge_info);
   7735   _bfd_generic_link_hash_table_free (obfd);
   7736 }
   7737 
   7738 /* This is a hook for the ELF emulation code in the generic linker to
   7739    tell the backend linker what file name to use for the DT_NEEDED
   7740    entry for a dynamic object.  */
   7741 
   7742 void
   7743 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   7744 {
   7745   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7746       && bfd_get_format (abfd) == bfd_object)
   7747     elf_dt_name (abfd) = name;
   7748 }
   7749 
   7750 int
   7751 bfd_elf_get_dyn_lib_class (bfd *abfd)
   7752 {
   7753   int lib_class;
   7754   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7755       && bfd_get_format (abfd) == bfd_object)
   7756     lib_class = elf_dyn_lib_class (abfd);
   7757   else
   7758     lib_class = 0;
   7759   return lib_class;
   7760 }
   7761 
   7762 void
   7763 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   7764 {
   7765   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7766       && bfd_get_format (abfd) == bfd_object)
   7767     elf_dyn_lib_class (abfd) = lib_class;
   7768 }
   7769 
   7770 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   7771    the linker ELF emulation code.  */
   7772 
   7773 struct bfd_link_needed_list *
   7774 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   7775 			 struct bfd_link_info *info)
   7776 {
   7777   if (! is_elf_hash_table (info->hash))
   7778     return NULL;
   7779   return elf_hash_table (info)->needed;
   7780 }
   7781 
   7782 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   7783    hook for the linker ELF emulation code.  */
   7784 
   7785 struct bfd_link_needed_list *
   7786 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   7787 			  struct bfd_link_info *info)
   7788 {
   7789   if (! is_elf_hash_table (info->hash))
   7790     return NULL;
   7791   return elf_hash_table (info)->runpath;
   7792 }
   7793 
   7794 /* Get the name actually used for a dynamic object for a link.  This
   7795    is the SONAME entry if there is one.  Otherwise, it is the string
   7796    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   7797 
   7798 const char *
   7799 bfd_elf_get_dt_soname (bfd *abfd)
   7800 {
   7801   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7802       && bfd_get_format (abfd) == bfd_object)
   7803     return elf_dt_name (abfd);
   7804   return NULL;
   7805 }
   7806 
   7807 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   7808    the ELF linker emulation code.  */
   7809 
   7810 bfd_boolean
   7811 bfd_elf_get_bfd_needed_list (bfd *abfd,
   7812 			     struct bfd_link_needed_list **pneeded)
   7813 {
   7814   asection *s;
   7815   bfd_byte *dynbuf = NULL;
   7816   unsigned int elfsec;
   7817   unsigned long shlink;
   7818   bfd_byte *extdyn, *extdynend;
   7819   size_t extdynsize;
   7820   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   7821 
   7822   *pneeded = NULL;
   7823 
   7824   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   7825       || bfd_get_format (abfd) != bfd_object)
   7826     return TRUE;
   7827 
   7828   s = bfd_get_section_by_name (abfd, ".dynamic");
   7829   if (s == NULL || s->size == 0)
   7830     return TRUE;
   7831 
   7832   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   7833     goto error_return;
   7834 
   7835   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   7836   if (elfsec == SHN_BAD)
   7837     goto error_return;
   7838 
   7839   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   7840 
   7841   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   7842   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   7843 
   7844   extdyn = dynbuf;
   7845   extdynend = extdyn + s->size;
   7846   for (; extdyn < extdynend; extdyn += extdynsize)
   7847     {
   7848       Elf_Internal_Dyn dyn;
   7849 
   7850       (*swap_dyn_in) (abfd, extdyn, &dyn);
   7851 
   7852       if (dyn.d_tag == DT_NULL)
   7853 	break;
   7854 
   7855       if (dyn.d_tag == DT_NEEDED)
   7856 	{
   7857 	  const char *string;
   7858 	  struct bfd_link_needed_list *l;
   7859 	  unsigned int tagv = dyn.d_un.d_val;
   7860 	  bfd_size_type amt;
   7861 
   7862 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   7863 	  if (string == NULL)
   7864 	    goto error_return;
   7865 
   7866 	  amt = sizeof *l;
   7867 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   7868 	  if (l == NULL)
   7869 	    goto error_return;
   7870 
   7871 	  l->by = abfd;
   7872 	  l->name = string;
   7873 	  l->next = *pneeded;
   7874 	  *pneeded = l;
   7875 	}
   7876     }
   7877 
   7878   free (dynbuf);
   7879 
   7880   return TRUE;
   7881 
   7882  error_return:
   7883   if (dynbuf != NULL)
   7884     free (dynbuf);
   7885   return FALSE;
   7886 }
   7887 
   7888 struct elf_symbuf_symbol
   7889 {
   7890   unsigned long st_name;	/* Symbol name, index in string tbl */
   7891   unsigned char st_info;	/* Type and binding attributes */
   7892   unsigned char st_other;	/* Visibilty, and target specific */
   7893 };
   7894 
   7895 struct elf_symbuf_head
   7896 {
   7897   struct elf_symbuf_symbol *ssym;
   7898   size_t count;
   7899   unsigned int st_shndx;
   7900 };
   7901 
   7902 struct elf_symbol
   7903 {
   7904   union
   7905     {
   7906       Elf_Internal_Sym *isym;
   7907       struct elf_symbuf_symbol *ssym;
   7908       void *p;
   7909     } u;
   7910   const char *name;
   7911 };
   7912 
   7913 /* Sort references to symbols by ascending section number.  */
   7914 
   7915 static int
   7916 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   7917 {
   7918   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   7919   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   7920 
   7921   if (s1->st_shndx != s2->st_shndx)
   7922     return s1->st_shndx > s2->st_shndx ? 1 : -1;
   7923   /* Final sort by the address of the sym in the symbuf ensures
   7924      a stable sort.  */
   7925   if (s1 != s2)
   7926     return s1 > s2 ? 1 : -1;
   7927   return 0;
   7928 }
   7929 
   7930 static int
   7931 elf_sym_name_compare (const void *arg1, const void *arg2)
   7932 {
   7933   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   7934   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   7935   int ret = strcmp (s1->name, s2->name);
   7936   if (ret != 0)
   7937     return ret;
   7938   if (s1->u.p != s2->u.p)
   7939     return s1->u.p > s2->u.p ? 1 : -1;
   7940   return 0;
   7941 }
   7942 
   7943 static struct elf_symbuf_head *
   7944 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
   7945 {
   7946   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   7947   struct elf_symbuf_symbol *ssym;
   7948   struct elf_symbuf_head *ssymbuf, *ssymhead;
   7949   size_t i, shndx_count, total_size;
   7950 
   7951   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
   7952   if (indbuf == NULL)
   7953     return NULL;
   7954 
   7955   for (ind = indbuf, i = 0; i < symcount; i++)
   7956     if (isymbuf[i].st_shndx != SHN_UNDEF)
   7957       *ind++ = &isymbuf[i];
   7958   indbufend = ind;
   7959 
   7960   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   7961 	 elf_sort_elf_symbol);
   7962 
   7963   shndx_count = 0;
   7964   if (indbufend > indbuf)
   7965     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   7966       if (ind[0]->st_shndx != ind[1]->st_shndx)
   7967 	shndx_count++;
   7968 
   7969   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   7970 		+ (indbufend - indbuf) * sizeof (*ssym));
   7971   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   7972   if (ssymbuf == NULL)
   7973     {
   7974       free (indbuf);
   7975       return NULL;
   7976     }
   7977 
   7978   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   7979   ssymbuf->ssym = NULL;
   7980   ssymbuf->count = shndx_count;
   7981   ssymbuf->st_shndx = 0;
   7982   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   7983     {
   7984       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   7985 	{
   7986 	  ssymhead++;
   7987 	  ssymhead->ssym = ssym;
   7988 	  ssymhead->count = 0;
   7989 	  ssymhead->st_shndx = (*ind)->st_shndx;
   7990 	}
   7991       ssym->st_name = (*ind)->st_name;
   7992       ssym->st_info = (*ind)->st_info;
   7993       ssym->st_other = (*ind)->st_other;
   7994       ssymhead->count++;
   7995     }
   7996   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
   7997 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
   7998 		  == total_size));
   7999 
   8000   free (indbuf);
   8001   return ssymbuf;
   8002 }
   8003 
   8004 /* Check if 2 sections define the same set of local and global
   8005    symbols.  */
   8006 
   8007 static bfd_boolean
   8008 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   8009 				   struct bfd_link_info *info)
   8010 {
   8011   bfd *bfd1, *bfd2;
   8012   const struct elf_backend_data *bed1, *bed2;
   8013   Elf_Internal_Shdr *hdr1, *hdr2;
   8014   size_t symcount1, symcount2;
   8015   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   8016   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   8017   Elf_Internal_Sym *isym, *isymend;
   8018   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   8019   size_t count1, count2, i;
   8020   unsigned int shndx1, shndx2;
   8021   bfd_boolean result;
   8022 
   8023   bfd1 = sec1->owner;
   8024   bfd2 = sec2->owner;
   8025 
   8026   /* Both sections have to be in ELF.  */
   8027   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   8028       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   8029     return FALSE;
   8030 
   8031   if (elf_section_type (sec1) != elf_section_type (sec2))
   8032     return FALSE;
   8033 
   8034   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   8035   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   8036   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   8037     return FALSE;
   8038 
   8039   bed1 = get_elf_backend_data (bfd1);
   8040   bed2 = get_elf_backend_data (bfd2);
   8041   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   8042   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   8043   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   8044   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   8045 
   8046   if (symcount1 == 0 || symcount2 == 0)
   8047     return FALSE;
   8048 
   8049   result = FALSE;
   8050   isymbuf1 = NULL;
   8051   isymbuf2 = NULL;
   8052   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   8053   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   8054 
   8055   if (ssymbuf1 == NULL)
   8056     {
   8057       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   8058 				       NULL, NULL, NULL);
   8059       if (isymbuf1 == NULL)
   8060 	goto done;
   8061 
   8062       if (!info->reduce_memory_overheads)
   8063 	{
   8064 	  ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
   8065 	  elf_tdata (bfd1)->symbuf = ssymbuf1;
   8066 	}
   8067     }
   8068 
   8069   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   8070     {
   8071       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   8072 				       NULL, NULL, NULL);
   8073       if (isymbuf2 == NULL)
   8074 	goto done;
   8075 
   8076       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
   8077 	{
   8078 	  ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
   8079 	  elf_tdata (bfd2)->symbuf = ssymbuf2;
   8080 	}
   8081     }
   8082 
   8083   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   8084     {
   8085       /* Optimized faster version.  */
   8086       size_t lo, hi, mid;
   8087       struct elf_symbol *symp;
   8088       struct elf_symbuf_symbol *ssym, *ssymend;
   8089 
   8090       lo = 0;
   8091       hi = ssymbuf1->count;
   8092       ssymbuf1++;
   8093       count1 = 0;
   8094       while (lo < hi)
   8095 	{
   8096 	  mid = (lo + hi) / 2;
   8097 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   8098 	    hi = mid;
   8099 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   8100 	    lo = mid + 1;
   8101 	  else
   8102 	    {
   8103 	      count1 = ssymbuf1[mid].count;
   8104 	      ssymbuf1 += mid;
   8105 	      break;
   8106 	    }
   8107 	}
   8108 
   8109       lo = 0;
   8110       hi = ssymbuf2->count;
   8111       ssymbuf2++;
   8112       count2 = 0;
   8113       while (lo < hi)
   8114 	{
   8115 	  mid = (lo + hi) / 2;
   8116 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   8117 	    hi = mid;
   8118 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   8119 	    lo = mid + 1;
   8120 	  else
   8121 	    {
   8122 	      count2 = ssymbuf2[mid].count;
   8123 	      ssymbuf2 += mid;
   8124 	      break;
   8125 	    }
   8126 	}
   8127 
   8128       if (count1 == 0 || count2 == 0 || count1 != count2)
   8129 	goto done;
   8130 
   8131       symtable1
   8132 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   8133       symtable2
   8134 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   8135       if (symtable1 == NULL || symtable2 == NULL)
   8136 	goto done;
   8137 
   8138       symp = symtable1;
   8139       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
   8140 	   ssym < ssymend; ssym++, symp++)
   8141 	{
   8142 	  symp->u.ssym = ssym;
   8143 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
   8144 							hdr1->sh_link,
   8145 							ssym->st_name);
   8146 	}
   8147 
   8148       symp = symtable2;
   8149       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
   8150 	   ssym < ssymend; ssym++, symp++)
   8151 	{
   8152 	  symp->u.ssym = ssym;
   8153 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
   8154 							hdr2->sh_link,
   8155 							ssym->st_name);
   8156 	}
   8157 
   8158       /* Sort symbol by name.  */
   8159       qsort (symtable1, count1, sizeof (struct elf_symbol),
   8160 	     elf_sym_name_compare);
   8161       qsort (symtable2, count1, sizeof (struct elf_symbol),
   8162 	     elf_sym_name_compare);
   8163 
   8164       for (i = 0; i < count1; i++)
   8165 	/* Two symbols must have the same binding, type and name.  */
   8166 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   8167 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   8168 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8169 	  goto done;
   8170 
   8171       result = TRUE;
   8172       goto done;
   8173     }
   8174 
   8175   symtable1 = (struct elf_symbol *)
   8176       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   8177   symtable2 = (struct elf_symbol *)
   8178       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   8179   if (symtable1 == NULL || symtable2 == NULL)
   8180     goto done;
   8181 
   8182   /* Count definitions in the section.  */
   8183   count1 = 0;
   8184   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   8185     if (isym->st_shndx == shndx1)
   8186       symtable1[count1++].u.isym = isym;
   8187 
   8188   count2 = 0;
   8189   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   8190     if (isym->st_shndx == shndx2)
   8191       symtable2[count2++].u.isym = isym;
   8192 
   8193   if (count1 == 0 || count2 == 0 || count1 != count2)
   8194     goto done;
   8195 
   8196   for (i = 0; i < count1; i++)
   8197     symtable1[i].name
   8198       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   8199 					 symtable1[i].u.isym->st_name);
   8200 
   8201   for (i = 0; i < count2; i++)
   8202     symtable2[i].name
   8203       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   8204 					 symtable2[i].u.isym->st_name);
   8205 
   8206   /* Sort symbol by name.  */
   8207   qsort (symtable1, count1, sizeof (struct elf_symbol),
   8208 	 elf_sym_name_compare);
   8209   qsort (symtable2, count1, sizeof (struct elf_symbol),
   8210 	 elf_sym_name_compare);
   8211 
   8212   for (i = 0; i < count1; i++)
   8213     /* Two symbols must have the same binding, type and name.  */
   8214     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   8215 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   8216 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   8217       goto done;
   8218 
   8219   result = TRUE;
   8220 
   8221 done:
   8222   if (symtable1)
   8223     free (symtable1);
   8224   if (symtable2)
   8225     free (symtable2);
   8226   if (isymbuf1)
   8227     free (isymbuf1);
   8228   if (isymbuf2)
   8229     free (isymbuf2);
   8230 
   8231   return result;
   8232 }
   8233 
   8234 /* Return TRUE if 2 section types are compatible.  */
   8235 
   8236 bfd_boolean
   8237 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   8238 				 bfd *bbfd, const asection *bsec)
   8239 {
   8240   if (asec == NULL
   8241       || bsec == NULL
   8242       || abfd->xvec->flavour != bfd_target_elf_flavour
   8243       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   8244     return TRUE;
   8245 
   8246   return elf_section_type (asec) == elf_section_type (bsec);
   8247 }
   8248 
   8249 /* Final phase of ELF linker.  */
   8251 
   8252 /* A structure we use to avoid passing large numbers of arguments.  */
   8253 
   8254 struct elf_final_link_info
   8255 {
   8256   /* General link information.  */
   8257   struct bfd_link_info *info;
   8258   /* Output BFD.  */
   8259   bfd *output_bfd;
   8260   /* Symbol string table.  */
   8261   struct elf_strtab_hash *symstrtab;
   8262   /* .hash section.  */
   8263   asection *hash_sec;
   8264   /* symbol version section (.gnu.version).  */
   8265   asection *symver_sec;
   8266   /* Buffer large enough to hold contents of any section.  */
   8267   bfd_byte *contents;
   8268   /* Buffer large enough to hold external relocs of any section.  */
   8269   void *external_relocs;
   8270   /* Buffer large enough to hold internal relocs of any section.  */
   8271   Elf_Internal_Rela *internal_relocs;
   8272   /* Buffer large enough to hold external local symbols of any input
   8273      BFD.  */
   8274   bfd_byte *external_syms;
   8275   /* And a buffer for symbol section indices.  */
   8276   Elf_External_Sym_Shndx *locsym_shndx;
   8277   /* Buffer large enough to hold internal local symbols of any input
   8278      BFD.  */
   8279   Elf_Internal_Sym *internal_syms;
   8280   /* Array large enough to hold a symbol index for each local symbol
   8281      of any input BFD.  */
   8282   long *indices;
   8283   /* Array large enough to hold a section pointer for each local
   8284      symbol of any input BFD.  */
   8285   asection **sections;
   8286   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   8287   Elf_External_Sym_Shndx *symshndxbuf;
   8288   /* Number of STT_FILE syms seen.  */
   8289   size_t filesym_count;
   8290 };
   8291 
   8292 /* This struct is used to pass information to elf_link_output_extsym.  */
   8293 
   8294 struct elf_outext_info
   8295 {
   8296   bfd_boolean failed;
   8297   bfd_boolean localsyms;
   8298   bfd_boolean file_sym_done;
   8299   struct elf_final_link_info *flinfo;
   8300 };
   8301 
   8302 
   8303 /* Support for evaluating a complex relocation.
   8304 
   8305    Complex relocations are generalized, self-describing relocations.  The
   8306    implementation of them consists of two parts: complex symbols, and the
   8307    relocations themselves.
   8308 
   8309    The relocations are use a reserved elf-wide relocation type code (R_RELC
   8310    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   8311    information (start bit, end bit, word width, etc) into the addend.  This
   8312    information is extracted from CGEN-generated operand tables within gas.
   8313 
   8314    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
   8315    internal) representing prefix-notation expressions, including but not
   8316    limited to those sorts of expressions normally encoded as addends in the
   8317    addend field.  The symbol mangling format is:
   8318 
   8319    <node> := <literal>
   8320 	  |  <unary-operator> ':' <node>
   8321 	  |  <binary-operator> ':' <node> ':' <node>
   8322 	  ;
   8323 
   8324    <literal> := 's' <digits=N> ':' <N character symbol name>
   8325 	     |  'S' <digits=N> ':' <N character section name>
   8326 	     |  '#' <hexdigits>
   8327 	     ;
   8328 
   8329    <binary-operator> := as in C
   8330    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   8331 
   8332 static void
   8333 set_symbol_value (bfd *bfd_with_globals,
   8334 		  Elf_Internal_Sym *isymbuf,
   8335 		  size_t locsymcount,
   8336 		  size_t symidx,
   8337 		  bfd_vma val)
   8338 {
   8339   struct elf_link_hash_entry **sym_hashes;
   8340   struct elf_link_hash_entry *h;
   8341   size_t extsymoff = locsymcount;
   8342 
   8343   if (symidx < locsymcount)
   8344     {
   8345       Elf_Internal_Sym *sym;
   8346 
   8347       sym = isymbuf + symidx;
   8348       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   8349 	{
   8350 	  /* It is a local symbol: move it to the
   8351 	     "absolute" section and give it a value.  */
   8352 	  sym->st_shndx = SHN_ABS;
   8353 	  sym->st_value = val;
   8354 	  return;
   8355 	}
   8356       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   8357       extsymoff = 0;
   8358     }
   8359 
   8360   /* It is a global symbol: set its link type
   8361      to "defined" and give it a value.  */
   8362 
   8363   sym_hashes = elf_sym_hashes (bfd_with_globals);
   8364   h = sym_hashes [symidx - extsymoff];
   8365   while (h->root.type == bfd_link_hash_indirect
   8366 	 || h->root.type == bfd_link_hash_warning)
   8367     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8368   h->root.type = bfd_link_hash_defined;
   8369   h->root.u.def.value = val;
   8370   h->root.u.def.section = bfd_abs_section_ptr;
   8371 }
   8372 
   8373 static bfd_boolean
   8374 resolve_symbol (const char *name,
   8375 		bfd *input_bfd,
   8376 		struct elf_final_link_info *flinfo,
   8377 		bfd_vma *result,
   8378 		Elf_Internal_Sym *isymbuf,
   8379 		size_t locsymcount)
   8380 {
   8381   Elf_Internal_Sym *sym;
   8382   struct bfd_link_hash_entry *global_entry;
   8383   const char *candidate = NULL;
   8384   Elf_Internal_Shdr *symtab_hdr;
   8385   size_t i;
   8386 
   8387   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   8388 
   8389   for (i = 0; i < locsymcount; ++ i)
   8390     {
   8391       sym = isymbuf + i;
   8392 
   8393       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   8394 	continue;
   8395 
   8396       candidate = bfd_elf_string_from_elf_section (input_bfd,
   8397 						   symtab_hdr->sh_link,
   8398 						   sym->st_name);
   8399 #ifdef DEBUG
   8400       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   8401 	      name, candidate, (unsigned long) sym->st_value);
   8402 #endif
   8403       if (candidate && strcmp (candidate, name) == 0)
   8404 	{
   8405 	  asection *sec = flinfo->sections [i];
   8406 
   8407 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   8408 	  *result += sec->output_offset + sec->output_section->vma;
   8409 #ifdef DEBUG
   8410 	  printf ("Found symbol with value %8.8lx\n",
   8411 		  (unsigned long) *result);
   8412 #endif
   8413 	  return TRUE;
   8414 	}
   8415     }
   8416 
   8417   /* Hmm, haven't found it yet. perhaps it is a global.  */
   8418   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   8419 				       FALSE, FALSE, TRUE);
   8420   if (!global_entry)
   8421     return FALSE;
   8422 
   8423   if (global_entry->type == bfd_link_hash_defined
   8424       || global_entry->type == bfd_link_hash_defweak)
   8425     {
   8426       *result = (global_entry->u.def.value
   8427 		 + global_entry->u.def.section->output_section->vma
   8428 		 + global_entry->u.def.section->output_offset);
   8429 #ifdef DEBUG
   8430       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   8431 	      global_entry->root.string, (unsigned long) *result);
   8432 #endif
   8433       return TRUE;
   8434     }
   8435 
   8436   return FALSE;
   8437 }
   8438 
   8439 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
   8440    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
   8441    names like "foo.end" which is the end address of section "foo".  */
   8442 
   8443 static bfd_boolean
   8444 resolve_section (const char *name,
   8445 		 asection *sections,
   8446 		 bfd_vma *result,
   8447 		 bfd * abfd)
   8448 {
   8449   asection *curr;
   8450   unsigned int len;
   8451 
   8452   for (curr = sections; curr; curr = curr->next)
   8453     if (strcmp (curr->name, name) == 0)
   8454       {
   8455 	*result = curr->vma;
   8456 	return TRUE;
   8457       }
   8458 
   8459   /* Hmm. still haven't found it. try pseudo-section names.  */
   8460   /* FIXME: This could be coded more efficiently...  */
   8461   for (curr = sections; curr; curr = curr->next)
   8462     {
   8463       len = strlen (curr->name);
   8464       if (len > strlen (name))
   8465 	continue;
   8466 
   8467       if (strncmp (curr->name, name, len) == 0)
   8468 	{
   8469 	  if (strncmp (".end", name + len, 4) == 0)
   8470 	    {
   8471 	      *result = (curr->vma
   8472 			 + curr->size / bfd_octets_per_byte (abfd, curr));
   8473 	      return TRUE;
   8474 	    }
   8475 
   8476 	  /* Insert more pseudo-section names here, if you like.  */
   8477 	}
   8478     }
   8479 
   8480   return FALSE;
   8481 }
   8482 
   8483 static void
   8484 undefined_reference (const char *reftype, const char *name)
   8485 {
   8486   /* xgettext:c-format */
   8487   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   8488 		      reftype, name);
   8489 }
   8490 
   8491 static bfd_boolean
   8492 eval_symbol (bfd_vma *result,
   8493 	     const char **symp,
   8494 	     bfd *input_bfd,
   8495 	     struct elf_final_link_info *flinfo,
   8496 	     bfd_vma dot,
   8497 	     Elf_Internal_Sym *isymbuf,
   8498 	     size_t locsymcount,
   8499 	     int signed_p)
   8500 {
   8501   size_t len;
   8502   size_t symlen;
   8503   bfd_vma a;
   8504   bfd_vma b;
   8505   char symbuf[4096];
   8506   const char *sym = *symp;
   8507   const char *symend;
   8508   bfd_boolean symbol_is_section = FALSE;
   8509 
   8510   len = strlen (sym);
   8511   symend = sym + len;
   8512 
   8513   if (len < 1 || len > sizeof (symbuf))
   8514     {
   8515       bfd_set_error (bfd_error_invalid_operation);
   8516       return FALSE;
   8517     }
   8518 
   8519   switch (* sym)
   8520     {
   8521     case '.':
   8522       *result = dot;
   8523       *symp = sym + 1;
   8524       return TRUE;
   8525 
   8526     case '#':
   8527       ++sym;
   8528       *result = strtoul (sym, (char **) symp, 16);
   8529       return TRUE;
   8530 
   8531     case 'S':
   8532       symbol_is_section = TRUE;
   8533       /* Fall through.  */
   8534     case 's':
   8535       ++sym;
   8536       symlen = strtol (sym, (char **) symp, 10);
   8537       sym = *symp + 1; /* Skip the trailing ':'.  */
   8538 
   8539       if (symend < sym || symlen + 1 > sizeof (symbuf))
   8540 	{
   8541 	  bfd_set_error (bfd_error_invalid_operation);
   8542 	  return FALSE;
   8543 	}
   8544 
   8545       memcpy (symbuf, sym, symlen);
   8546       symbuf[symlen] = '\0';
   8547       *symp = sym + symlen;
   8548 
   8549       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   8550 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   8551 	 interpretation here; section means "try section first", not "must be a
   8552 	 section", and likewise with symbol.  */
   8553 
   8554       if (symbol_is_section)
   8555 	{
   8556 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
   8557 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   8558 				  isymbuf, locsymcount))
   8559 	    {
   8560 	      undefined_reference ("section", symbuf);
   8561 	      return FALSE;
   8562 	    }
   8563 	}
   8564       else
   8565 	{
   8566 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   8567 			       isymbuf, locsymcount)
   8568 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   8569 				   result, input_bfd))
   8570 	    {
   8571 	      undefined_reference ("symbol", symbuf);
   8572 	      return FALSE;
   8573 	    }
   8574 	}
   8575 
   8576       return TRUE;
   8577 
   8578       /* All that remains are operators.  */
   8579 
   8580 #define UNARY_OP(op)						\
   8581   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   8582     {								\
   8583       sym += strlen (#op);					\
   8584       if (*sym == ':')						\
   8585 	++sym;							\
   8586       *symp = sym;						\
   8587       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   8588 			isymbuf, locsymcount, signed_p))	\
   8589 	return FALSE;						\
   8590       if (signed_p)						\
   8591 	*result = op ((bfd_signed_vma) a);			\
   8592       else							\
   8593 	*result = op a;						\
   8594       return TRUE;						\
   8595     }
   8596 
   8597 #define BINARY_OP(op)						\
   8598   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   8599     {								\
   8600       sym += strlen (#op);					\
   8601       if (*sym == ':')						\
   8602 	++sym;							\
   8603       *symp = sym;						\
   8604       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   8605 			isymbuf, locsymcount, signed_p))	\
   8606 	return FALSE;						\
   8607       ++*symp;							\
   8608       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   8609 			isymbuf, locsymcount, signed_p))	\
   8610 	return FALSE;						\
   8611       if (signed_p)						\
   8612 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   8613       else							\
   8614 	*result = a op b;					\
   8615       return TRUE;						\
   8616     }
   8617 
   8618     default:
   8619       UNARY_OP  (0-);
   8620       BINARY_OP (<<);
   8621       BINARY_OP (>>);
   8622       BINARY_OP (==);
   8623       BINARY_OP (!=);
   8624       BINARY_OP (<=);
   8625       BINARY_OP (>=);
   8626       BINARY_OP (&&);
   8627       BINARY_OP (||);
   8628       UNARY_OP  (~);
   8629       UNARY_OP  (!);
   8630       BINARY_OP (*);
   8631       BINARY_OP (/);
   8632       BINARY_OP (%);
   8633       BINARY_OP (^);
   8634       BINARY_OP (|);
   8635       BINARY_OP (&);
   8636       BINARY_OP (+);
   8637       BINARY_OP (-);
   8638       BINARY_OP (<);
   8639       BINARY_OP (>);
   8640 #undef UNARY_OP
   8641 #undef BINARY_OP
   8642       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   8643       bfd_set_error (bfd_error_invalid_operation);
   8644       return FALSE;
   8645     }
   8646 }
   8647 
   8648 static void
   8649 put_value (bfd_vma size,
   8650 	   unsigned long chunksz,
   8651 	   bfd *input_bfd,
   8652 	   bfd_vma x,
   8653 	   bfd_byte *location)
   8654 {
   8655   location += (size - chunksz);
   8656 
   8657   for (; size; size -= chunksz, location -= chunksz)
   8658     {
   8659       switch (chunksz)
   8660 	{
   8661 	case 1:
   8662 	  bfd_put_8 (input_bfd, x, location);
   8663 	  x >>= 8;
   8664 	  break;
   8665 	case 2:
   8666 	  bfd_put_16 (input_bfd, x, location);
   8667 	  x >>= 16;
   8668 	  break;
   8669 	case 4:
   8670 	  bfd_put_32 (input_bfd, x, location);
   8671 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   8672 	  x >>= 16;
   8673 	  x >>= 16;
   8674 	  break;
   8675 #ifdef BFD64
   8676 	case 8:
   8677 	  bfd_put_64 (input_bfd, x, location);
   8678 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   8679 	  x >>= 32;
   8680 	  x >>= 32;
   8681 	  break;
   8682 #endif
   8683 	default:
   8684 	  abort ();
   8685 	  break;
   8686 	}
   8687     }
   8688 }
   8689 
   8690 static bfd_vma
   8691 get_value (bfd_vma size,
   8692 	   unsigned long chunksz,
   8693 	   bfd *input_bfd,
   8694 	   bfd_byte *location)
   8695 {
   8696   int shift;
   8697   bfd_vma x = 0;
   8698 
   8699   /* Sanity checks.  */
   8700   BFD_ASSERT (chunksz <= sizeof (x)
   8701 	      && size >= chunksz
   8702 	      && chunksz != 0
   8703 	      && (size % chunksz) == 0
   8704 	      && input_bfd != NULL
   8705 	      && location != NULL);
   8706 
   8707   if (chunksz == sizeof (x))
   8708     {
   8709       BFD_ASSERT (size == chunksz);
   8710 
   8711       /* Make sure that we do not perform an undefined shift operation.
   8712 	 We know that size == chunksz so there will only be one iteration
   8713 	 of the loop below.  */
   8714       shift = 0;
   8715     }
   8716   else
   8717     shift = 8 * chunksz;
   8718 
   8719   for (; size; size -= chunksz, location += chunksz)
   8720     {
   8721       switch (chunksz)
   8722 	{
   8723 	case 1:
   8724 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   8725 	  break;
   8726 	case 2:
   8727 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   8728 	  break;
   8729 	case 4:
   8730 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   8731 	  break;
   8732 #ifdef BFD64
   8733 	case 8:
   8734 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   8735 	  break;
   8736 #endif
   8737 	default:
   8738 	  abort ();
   8739 	}
   8740     }
   8741   return x;
   8742 }
   8743 
   8744 static void
   8745 decode_complex_addend (unsigned long *start,   /* in bits */
   8746 		       unsigned long *oplen,   /* in bits */
   8747 		       unsigned long *len,     /* in bits */
   8748 		       unsigned long *wordsz,  /* in bytes */
   8749 		       unsigned long *chunksz, /* in bytes */
   8750 		       unsigned long *lsb0_p,
   8751 		       unsigned long *signed_p,
   8752 		       unsigned long *trunc_p,
   8753 		       unsigned long encoded)
   8754 {
   8755   * start     =	 encoded	& 0x3F;
   8756   * len	      = (encoded >>  6) & 0x3F;
   8757   * oplen     = (encoded >> 12) & 0x3F;
   8758   * wordsz    = (encoded >> 18) & 0xF;
   8759   * chunksz   = (encoded >> 22) & 0xF;
   8760   * lsb0_p    = (encoded >> 27) & 1;
   8761   * signed_p  = (encoded >> 28) & 1;
   8762   * trunc_p   = (encoded >> 29) & 1;
   8763 }
   8764 
   8765 bfd_reloc_status_type
   8766 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   8767 				    asection *input_section,
   8768 				    bfd_byte *contents,
   8769 				    Elf_Internal_Rela *rel,
   8770 				    bfd_vma relocation)
   8771 {
   8772   bfd_vma shift, x, mask;
   8773   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   8774   bfd_reloc_status_type r;
   8775   bfd_size_type octets;
   8776 
   8777   /*  Perform this reloc, since it is complex.
   8778       (this is not to say that it necessarily refers to a complex
   8779       symbol; merely that it is a self-describing CGEN based reloc.
   8780       i.e. the addend has the complete reloc information (bit start, end,
   8781       word size, etc) encoded within it.).  */
   8782 
   8783   decode_complex_addend (&start, &oplen, &len, &wordsz,
   8784 			 &chunksz, &lsb0_p, &signed_p,
   8785 			 &trunc_p, rel->r_addend);
   8786 
   8787   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   8788 
   8789   if (lsb0_p)
   8790     shift = (start + 1) - len;
   8791   else
   8792     shift = (8 * wordsz) - (start + len);
   8793 
   8794   octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
   8795   x = get_value (wordsz, chunksz, input_bfd, contents + octets);
   8796 
   8797 #ifdef DEBUG
   8798   printf ("Doing complex reloc: "
   8799 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   8800 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   8801 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   8802 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   8803 	  oplen, (unsigned long) x, (unsigned long) mask,
   8804 	  (unsigned long) relocation);
   8805 #endif
   8806 
   8807   r = bfd_reloc_ok;
   8808   if (! trunc_p)
   8809     /* Now do an overflow check.  */
   8810     r = bfd_check_overflow ((signed_p
   8811 			     ? complain_overflow_signed
   8812 			     : complain_overflow_unsigned),
   8813 			    len, 0, (8 * wordsz),
   8814 			    relocation);
   8815 
   8816   /* Do the deed.  */
   8817   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   8818 
   8819 #ifdef DEBUG
   8820   printf ("           relocation: %8.8lx\n"
   8821 	  "         shifted mask: %8.8lx\n"
   8822 	  " shifted/masked reloc: %8.8lx\n"
   8823 	  "               result: %8.8lx\n",
   8824 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   8825 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   8826 #endif
   8827   put_value (wordsz, chunksz, input_bfd, x, contents + octets);
   8828   return r;
   8829 }
   8830 
   8831 /* Functions to read r_offset from external (target order) reloc
   8832    entry.  Faster than bfd_getl32 et al, because we let the compiler
   8833    know the value is aligned.  */
   8834 
   8835 static bfd_vma
   8836 ext32l_r_offset (const void *p)
   8837 {
   8838   union aligned32
   8839   {
   8840     uint32_t v;
   8841     unsigned char c[4];
   8842   };
   8843   const union aligned32 *a
   8844     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8845 
   8846   uint32_t aval = (  (uint32_t) a->c[0]
   8847 		   | (uint32_t) a->c[1] << 8
   8848 		   | (uint32_t) a->c[2] << 16
   8849 		   | (uint32_t) a->c[3] << 24);
   8850   return aval;
   8851 }
   8852 
   8853 static bfd_vma
   8854 ext32b_r_offset (const void *p)
   8855 {
   8856   union aligned32
   8857   {
   8858     uint32_t v;
   8859     unsigned char c[4];
   8860   };
   8861   const union aligned32 *a
   8862     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8863 
   8864   uint32_t aval = (  (uint32_t) a->c[0] << 24
   8865 		   | (uint32_t) a->c[1] << 16
   8866 		   | (uint32_t) a->c[2] << 8
   8867 		   | (uint32_t) a->c[3]);
   8868   return aval;
   8869 }
   8870 
   8871 #ifdef BFD_HOST_64_BIT
   8872 static bfd_vma
   8873 ext64l_r_offset (const void *p)
   8874 {
   8875   union aligned64
   8876   {
   8877     uint64_t v;
   8878     unsigned char c[8];
   8879   };
   8880   const union aligned64 *a
   8881     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8882 
   8883   uint64_t aval = (  (uint64_t) a->c[0]
   8884 		   | (uint64_t) a->c[1] << 8
   8885 		   | (uint64_t) a->c[2] << 16
   8886 		   | (uint64_t) a->c[3] << 24
   8887 		   | (uint64_t) a->c[4] << 32
   8888 		   | (uint64_t) a->c[5] << 40
   8889 		   | (uint64_t) a->c[6] << 48
   8890 		   | (uint64_t) a->c[7] << 56);
   8891   return aval;
   8892 }
   8893 
   8894 static bfd_vma
   8895 ext64b_r_offset (const void *p)
   8896 {
   8897   union aligned64
   8898   {
   8899     uint64_t v;
   8900     unsigned char c[8];
   8901   };
   8902   const union aligned64 *a
   8903     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8904 
   8905   uint64_t aval = (  (uint64_t) a->c[0] << 56
   8906 		   | (uint64_t) a->c[1] << 48
   8907 		   | (uint64_t) a->c[2] << 40
   8908 		   | (uint64_t) a->c[3] << 32
   8909 		   | (uint64_t) a->c[4] << 24
   8910 		   | (uint64_t) a->c[5] << 16
   8911 		   | (uint64_t) a->c[6] << 8
   8912 		   | (uint64_t) a->c[7]);
   8913   return aval;
   8914 }
   8915 #endif
   8916 
   8917 /* When performing a relocatable link, the input relocations are
   8918    preserved.  But, if they reference global symbols, the indices
   8919    referenced must be updated.  Update all the relocations found in
   8920    RELDATA.  */
   8921 
   8922 static bfd_boolean
   8923 elf_link_adjust_relocs (bfd *abfd,
   8924 			asection *sec,
   8925 			struct bfd_elf_section_reloc_data *reldata,
   8926 			bfd_boolean sort,
   8927 			struct bfd_link_info *info)
   8928 {
   8929   unsigned int i;
   8930   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8931   bfd_byte *erela;
   8932   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8933   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8934   bfd_vma r_type_mask;
   8935   int r_sym_shift;
   8936   unsigned int count = reldata->count;
   8937   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   8938 
   8939   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   8940     {
   8941       swap_in = bed->s->swap_reloc_in;
   8942       swap_out = bed->s->swap_reloc_out;
   8943     }
   8944   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   8945     {
   8946       swap_in = bed->s->swap_reloca_in;
   8947       swap_out = bed->s->swap_reloca_out;
   8948     }
   8949   else
   8950     abort ();
   8951 
   8952   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   8953     abort ();
   8954 
   8955   if (bed->s->arch_size == 32)
   8956     {
   8957       r_type_mask = 0xff;
   8958       r_sym_shift = 8;
   8959     }
   8960   else
   8961     {
   8962       r_type_mask = 0xffffffff;
   8963       r_sym_shift = 32;
   8964     }
   8965 
   8966   erela = reldata->hdr->contents;
   8967   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   8968     {
   8969       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   8970       unsigned int j;
   8971 
   8972       if (*rel_hash == NULL)
   8973 	continue;
   8974 
   8975       if ((*rel_hash)->indx == -2
   8976 	  && info->gc_sections
   8977 	  && ! info->gc_keep_exported)
   8978 	{
   8979 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
   8980 	  _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
   8981 			      abfd, sec,
   8982 			      (*rel_hash)->root.root.string);
   8983 	  _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
   8984 			      abfd, sec);
   8985 	  bfd_set_error (bfd_error_invalid_operation);
   8986 	  return FALSE;
   8987 	}
   8988       BFD_ASSERT ((*rel_hash)->indx >= 0);
   8989 
   8990       (*swap_in) (abfd, erela, irela);
   8991       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   8992 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   8993 			   | (irela[j].r_info & r_type_mask));
   8994       (*swap_out) (abfd, irela, erela);
   8995     }
   8996 
   8997   if (bed->elf_backend_update_relocs)
   8998     (*bed->elf_backend_update_relocs) (sec, reldata);
   8999 
   9000   if (sort && count != 0)
   9001     {
   9002       bfd_vma (*ext_r_off) (const void *);
   9003       bfd_vma r_off;
   9004       size_t elt_size;
   9005       bfd_byte *base, *end, *p, *loc;
   9006       bfd_byte *buf = NULL;
   9007 
   9008       if (bed->s->arch_size == 32)
   9009 	{
   9010 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9011 	    ext_r_off = ext32l_r_offset;
   9012 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9013 	    ext_r_off = ext32b_r_offset;
   9014 	  else
   9015 	    abort ();
   9016 	}
   9017       else
   9018 	{
   9019 #ifdef BFD_HOST_64_BIT
   9020 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   9021 	    ext_r_off = ext64l_r_offset;
   9022 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   9023 	    ext_r_off = ext64b_r_offset;
   9024 	  else
   9025 #endif
   9026 	    abort ();
   9027 	}
   9028 
   9029       /*  Must use a stable sort here.  A modified insertion sort,
   9030 	  since the relocs are mostly sorted already.  */
   9031       elt_size = reldata->hdr->sh_entsize;
   9032       base = reldata->hdr->contents;
   9033       end = base + count * elt_size;
   9034       if (elt_size > sizeof (Elf64_External_Rela))
   9035 	abort ();
   9036 
   9037       /* Ensure the first element is lowest.  This acts as a sentinel,
   9038 	 speeding the main loop below.  */
   9039       r_off = (*ext_r_off) (base);
   9040       for (p = loc = base; (p += elt_size) < end; )
   9041 	{
   9042 	  bfd_vma r_off2 = (*ext_r_off) (p);
   9043 	  if (r_off > r_off2)
   9044 	    {
   9045 	      r_off = r_off2;
   9046 	      loc = p;
   9047 	    }
   9048 	}
   9049       if (loc != base)
   9050 	{
   9051 	  /* Don't just swap *base and *loc as that changes the order
   9052 	     of the original base[0] and base[1] if they happen to
   9053 	     have the same r_offset.  */
   9054 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   9055 	  memcpy (onebuf, loc, elt_size);
   9056 	  memmove (base + elt_size, base, loc - base);
   9057 	  memcpy (base, onebuf, elt_size);
   9058 	}
   9059 
   9060       for (p = base + elt_size; (p += elt_size) < end; )
   9061 	{
   9062 	  /* base to p is sorted, *p is next to insert.  */
   9063 	  r_off = (*ext_r_off) (p);
   9064 	  /* Search the sorted region for location to insert.  */
   9065 	  loc = p - elt_size;
   9066 	  while (r_off < (*ext_r_off) (loc))
   9067 	    loc -= elt_size;
   9068 	  loc += elt_size;
   9069 	  if (loc != p)
   9070 	    {
   9071 	      /* Chances are there is a run of relocs to insert here,
   9072 		 from one of more input files.  Files are not always
   9073 		 linked in order due to the way elf_link_input_bfd is
   9074 		 called.  See pr17666.  */
   9075 	      size_t sortlen = p - loc;
   9076 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   9077 	      size_t runlen = elt_size;
   9078 	      size_t buf_size = 96 * 1024;
   9079 	      while (p + runlen < end
   9080 		     && (sortlen <= buf_size
   9081 			 || runlen + elt_size <= buf_size)
   9082 		     && r_off2 > (*ext_r_off) (p + runlen))
   9083 		runlen += elt_size;
   9084 	      if (buf == NULL)
   9085 		{
   9086 		  buf = bfd_malloc (buf_size);
   9087 		  if (buf == NULL)
   9088 		    return FALSE;
   9089 		}
   9090 	      if (runlen < sortlen)
   9091 		{
   9092 		  memcpy (buf, p, runlen);
   9093 		  memmove (loc + runlen, loc, sortlen);
   9094 		  memcpy (loc, buf, runlen);
   9095 		}
   9096 	      else
   9097 		{
   9098 		  memcpy (buf, loc, sortlen);
   9099 		  memmove (loc, p, runlen);
   9100 		  memcpy (loc + runlen, buf, sortlen);
   9101 		}
   9102 	      p += runlen - elt_size;
   9103 	    }
   9104 	}
   9105       /* Hashes are no longer valid.  */
   9106       free (reldata->hashes);
   9107       reldata->hashes = NULL;
   9108       free (buf);
   9109     }
   9110   return TRUE;
   9111 }
   9112 
   9113 struct elf_link_sort_rela
   9114 {
   9115   union {
   9116     bfd_vma offset;
   9117     bfd_vma sym_mask;
   9118   } u;
   9119   enum elf_reloc_type_class type;
   9120   /* We use this as an array of size int_rels_per_ext_rel.  */
   9121   Elf_Internal_Rela rela[1];
   9122 };
   9123 
   9124 /* qsort stability here and for cmp2 is only an issue if multiple
   9125    dynamic relocations are emitted at the same address.  But targets
   9126    that apply a series of dynamic relocations each operating on the
   9127    result of the prior relocation can't use -z combreloc as
   9128    implemented anyway.  Such schemes tend to be broken by sorting on
   9129    symbol index.  That leaves dynamic NONE relocs as the only other
   9130    case where ld might emit multiple relocs at the same address, and
   9131    those are only emitted due to target bugs.  */
   9132 
   9133 static int
   9134 elf_link_sort_cmp1 (const void *A, const void *B)
   9135 {
   9136   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9137   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9138   int relativea, relativeb;
   9139 
   9140   relativea = a->type == reloc_class_relative;
   9141   relativeb = b->type == reloc_class_relative;
   9142 
   9143   if (relativea < relativeb)
   9144     return 1;
   9145   if (relativea > relativeb)
   9146     return -1;
   9147   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   9148     return -1;
   9149   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   9150     return 1;
   9151   if (a->rela->r_offset < b->rela->r_offset)
   9152     return -1;
   9153   if (a->rela->r_offset > b->rela->r_offset)
   9154     return 1;
   9155   return 0;
   9156 }
   9157 
   9158 static int
   9159 elf_link_sort_cmp2 (const void *A, const void *B)
   9160 {
   9161   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   9162   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   9163 
   9164   if (a->type < b->type)
   9165     return -1;
   9166   if (a->type > b->type)
   9167     return 1;
   9168   if (a->u.offset < b->u.offset)
   9169     return -1;
   9170   if (a->u.offset > b->u.offset)
   9171     return 1;
   9172   if (a->rela->r_offset < b->rela->r_offset)
   9173     return -1;
   9174   if (a->rela->r_offset > b->rela->r_offset)
   9175     return 1;
   9176   return 0;
   9177 }
   9178 
   9179 static size_t
   9180 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   9181 {
   9182   asection *dynamic_relocs;
   9183   asection *rela_dyn;
   9184   asection *rel_dyn;
   9185   bfd_size_type count, size;
   9186   size_t i, ret, sort_elt, ext_size;
   9187   bfd_byte *sort, *s_non_relative, *p;
   9188   struct elf_link_sort_rela *sq;
   9189   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9190   int i2e = bed->s->int_rels_per_ext_rel;
   9191   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   9192   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   9193   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   9194   struct bfd_link_order *lo;
   9195   bfd_vma r_sym_mask;
   9196   bfd_boolean use_rela;
   9197 
   9198   /* Find a dynamic reloc section.  */
   9199   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   9200   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   9201   if (rela_dyn != NULL && rela_dyn->size > 0
   9202       && rel_dyn != NULL && rel_dyn->size > 0)
   9203     {
   9204       bfd_boolean use_rela_initialised = FALSE;
   9205 
   9206       /* This is just here to stop gcc from complaining.
   9207 	 Its initialization checking code is not perfect.  */
   9208       use_rela = TRUE;
   9209 
   9210       /* Both sections are present.  Examine the sizes
   9211 	 of the indirect sections to help us choose.  */
   9212       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   9213 	if (lo->type == bfd_indirect_link_order)
   9214 	  {
   9215 	    asection *o = lo->u.indirect.section;
   9216 
   9217 	    if ((o->size % bed->s->sizeof_rela) == 0)
   9218 	      {
   9219 		if ((o->size % bed->s->sizeof_rel) == 0)
   9220 		  /* Section size is divisible by both rel and rela sizes.
   9221 		     It is of no help to us.  */
   9222 		  ;
   9223 		else
   9224 		  {
   9225 		    /* Section size is only divisible by rela.  */
   9226 		    if (use_rela_initialised && !use_rela)
   9227 		      {
   9228 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   9229 					      "they are in more than one size"),
   9230 					    abfd);
   9231 			bfd_set_error (bfd_error_invalid_operation);
   9232 			return 0;
   9233 		      }
   9234 		    else
   9235 		      {
   9236 			use_rela = TRUE;
   9237 			use_rela_initialised = TRUE;
   9238 		      }
   9239 		  }
   9240 	      }
   9241 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   9242 	      {
   9243 		/* Section size is only divisible by rel.  */
   9244 		if (use_rela_initialised && use_rela)
   9245 		  {
   9246 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   9247 					  "they are in more than one size"),
   9248 					abfd);
   9249 		    bfd_set_error (bfd_error_invalid_operation);
   9250 		    return 0;
   9251 		  }
   9252 		else
   9253 		  {
   9254 		    use_rela = FALSE;
   9255 		    use_rela_initialised = TRUE;
   9256 		  }
   9257 	      }
   9258 	    else
   9259 	      {
   9260 		/* The section size is not divisible by either -
   9261 		   something is wrong.  */
   9262 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   9263 				      "they are of an unknown size"), abfd);
   9264 		bfd_set_error (bfd_error_invalid_operation);
   9265 		return 0;
   9266 	      }
   9267 	  }
   9268 
   9269       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   9270 	if (lo->type == bfd_indirect_link_order)
   9271 	  {
   9272 	    asection *o = lo->u.indirect.section;
   9273 
   9274 	    if ((o->size % bed->s->sizeof_rela) == 0)
   9275 	      {
   9276 		if ((o->size % bed->s->sizeof_rel) == 0)
   9277 		  /* Section size is divisible by both rel and rela sizes.
   9278 		     It is of no help to us.  */
   9279 		  ;
   9280 		else
   9281 		  {
   9282 		    /* Section size is only divisible by rela.  */
   9283 		    if (use_rela_initialised && !use_rela)
   9284 		      {
   9285 			_bfd_error_handler (_("%pB: unable to sort relocs - "
   9286 					      "they are in more than one size"),
   9287 					    abfd);
   9288 			bfd_set_error (bfd_error_invalid_operation);
   9289 			return 0;
   9290 		      }
   9291 		    else
   9292 		      {
   9293 			use_rela = TRUE;
   9294 			use_rela_initialised = TRUE;
   9295 		      }
   9296 		  }
   9297 	      }
   9298 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   9299 	      {
   9300 		/* Section size is only divisible by rel.  */
   9301 		if (use_rela_initialised && use_rela)
   9302 		  {
   9303 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
   9304 					  "they are in more than one size"),
   9305 					abfd);
   9306 		    bfd_set_error (bfd_error_invalid_operation);
   9307 		    return 0;
   9308 		  }
   9309 		else
   9310 		  {
   9311 		    use_rela = FALSE;
   9312 		    use_rela_initialised = TRUE;
   9313 		  }
   9314 	      }
   9315 	    else
   9316 	      {
   9317 		/* The section size is not divisible by either -
   9318 		   something is wrong.  */
   9319 		_bfd_error_handler (_("%pB: unable to sort relocs - "
   9320 				      "they are of an unknown size"), abfd);
   9321 		bfd_set_error (bfd_error_invalid_operation);
   9322 		return 0;
   9323 	      }
   9324 	  }
   9325 
   9326       if (! use_rela_initialised)
   9327 	/* Make a guess.  */
   9328 	use_rela = TRUE;
   9329     }
   9330   else if (rela_dyn != NULL && rela_dyn->size > 0)
   9331     use_rela = TRUE;
   9332   else if (rel_dyn != NULL && rel_dyn->size > 0)
   9333     use_rela = FALSE;
   9334   else
   9335     return 0;
   9336 
   9337   if (use_rela)
   9338     {
   9339       dynamic_relocs = rela_dyn;
   9340       ext_size = bed->s->sizeof_rela;
   9341       swap_in = bed->s->swap_reloca_in;
   9342       swap_out = bed->s->swap_reloca_out;
   9343     }
   9344   else
   9345     {
   9346       dynamic_relocs = rel_dyn;
   9347       ext_size = bed->s->sizeof_rel;
   9348       swap_in = bed->s->swap_reloc_in;
   9349       swap_out = bed->s->swap_reloc_out;
   9350     }
   9351 
   9352   size = 0;
   9353   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9354     if (lo->type == bfd_indirect_link_order)
   9355       size += lo->u.indirect.section->size;
   9356 
   9357   if (size != dynamic_relocs->size)
   9358     return 0;
   9359 
   9360   sort_elt = (sizeof (struct elf_link_sort_rela)
   9361 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   9362 
   9363   count = dynamic_relocs->size / ext_size;
   9364   if (count == 0)
   9365     return 0;
   9366   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   9367 
   9368   if (sort == NULL)
   9369     {
   9370       (*info->callbacks->warning)
   9371 	(info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
   9372       return 0;
   9373     }
   9374 
   9375   if (bed->s->arch_size == 32)
   9376     r_sym_mask = ~(bfd_vma) 0xff;
   9377   else
   9378     r_sym_mask = ~(bfd_vma) 0xffffffff;
   9379 
   9380   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9381     if (lo->type == bfd_indirect_link_order)
   9382       {
   9383 	bfd_byte *erel, *erelend;
   9384 	asection *o = lo->u.indirect.section;
   9385 
   9386 	if (o->contents == NULL && o->size != 0)
   9387 	  {
   9388 	    /* This is a reloc section that is being handled as a normal
   9389 	       section.  See bfd_section_from_shdr.  We can't combine
   9390 	       relocs in this case.  */
   9391 	    free (sort);
   9392 	    return 0;
   9393 	  }
   9394 	erel = o->contents;
   9395 	erelend = o->contents + o->size;
   9396 	p = sort + o->output_offset * opb / ext_size * sort_elt;
   9397 
   9398 	while (erel < erelend)
   9399 	  {
   9400 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9401 
   9402 	    (*swap_in) (abfd, erel, s->rela);
   9403 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   9404 	    s->u.sym_mask = r_sym_mask;
   9405 	    p += sort_elt;
   9406 	    erel += ext_size;
   9407 	  }
   9408       }
   9409 
   9410   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   9411 
   9412   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   9413     {
   9414       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9415       if (s->type != reloc_class_relative)
   9416 	break;
   9417     }
   9418   ret = i;
   9419   s_non_relative = p;
   9420 
   9421   sq = (struct elf_link_sort_rela *) s_non_relative;
   9422   for (; i < count; i++, p += sort_elt)
   9423     {
   9424       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   9425       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   9426 	sq = sp;
   9427       sp->u.offset = sq->rela->r_offset;
   9428     }
   9429 
   9430   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   9431 
   9432   struct elf_link_hash_table *htab = elf_hash_table (info);
   9433   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
   9434     {
   9435       /* We have plt relocs in .rela.dyn.  */
   9436       sq = (struct elf_link_sort_rela *) sort;
   9437       for (i = 0; i < count; i++)
   9438 	if (sq[count - i - 1].type != reloc_class_plt)
   9439 	  break;
   9440       if (i != 0 && htab->srelplt->size == i * ext_size)
   9441 	{
   9442 	  struct bfd_link_order **plo;
   9443 	  /* Put srelplt link_order last.  This is so the output_offset
   9444 	     set in the next loop is correct for DT_JMPREL.  */
   9445 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
   9446 	    if ((*plo)->type == bfd_indirect_link_order
   9447 		&& (*plo)->u.indirect.section == htab->srelplt)
   9448 	      {
   9449 		lo = *plo;
   9450 		*plo = lo->next;
   9451 	      }
   9452 	    else
   9453 	      plo = &(*plo)->next;
   9454 	  *plo = lo;
   9455 	  lo->next = NULL;
   9456 	  dynamic_relocs->map_tail.link_order = lo;
   9457 	}
   9458     }
   9459 
   9460   p = sort;
   9461   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9462     if (lo->type == bfd_indirect_link_order)
   9463       {
   9464 	bfd_byte *erel, *erelend;
   9465 	asection *o = lo->u.indirect.section;
   9466 
   9467 	erel = o->contents;
   9468 	erelend = o->contents + o->size;
   9469 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
   9470 	while (erel < erelend)
   9471 	  {
   9472 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9473 	    (*swap_out) (abfd, s->rela, erel);
   9474 	    p += sort_elt;
   9475 	    erel += ext_size;
   9476 	  }
   9477       }
   9478 
   9479   free (sort);
   9480   *psec = dynamic_relocs;
   9481   return ret;
   9482 }
   9483 
   9484 /* Add a symbol to the output symbol string table.  */
   9485 
   9486 static int
   9487 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
   9488 			   const char *name,
   9489 			   Elf_Internal_Sym *elfsym,
   9490 			   asection *input_sec,
   9491 			   struct elf_link_hash_entry *h)
   9492 {
   9493   int (*output_symbol_hook)
   9494     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   9495      struct elf_link_hash_entry *);
   9496   struct elf_link_hash_table *hash_table;
   9497   const struct elf_backend_data *bed;
   9498   bfd_size_type strtabsize;
   9499 
   9500   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   9501 
   9502   bed = get_elf_backend_data (flinfo->output_bfd);
   9503   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   9504   if (output_symbol_hook != NULL)
   9505     {
   9506       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   9507       if (ret != 1)
   9508 	return ret;
   9509     }
   9510 
   9511   if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
   9512     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
   9513   if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
   9514     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
   9515 
   9516   if (name == NULL
   9517       || *name == '\0'
   9518       || (input_sec->flags & SEC_EXCLUDE))
   9519     elfsym->st_name = (unsigned long) -1;
   9520   else
   9521     {
   9522       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   9523 	 to get the final offset for st_name.  */
   9524       elfsym->st_name
   9525 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   9526 					       name, FALSE);
   9527       if (elfsym->st_name == (unsigned long) -1)
   9528 	return 0;
   9529     }
   9530 
   9531   hash_table = elf_hash_table (flinfo->info);
   9532   strtabsize = hash_table->strtabsize;
   9533   if (strtabsize <= hash_table->strtabcount)
   9534     {
   9535       strtabsize += strtabsize;
   9536       hash_table->strtabsize = strtabsize;
   9537       strtabsize *= sizeof (*hash_table->strtab);
   9538       hash_table->strtab
   9539 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   9540 						 strtabsize);
   9541       if (hash_table->strtab == NULL)
   9542 	return 0;
   9543     }
   9544   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
   9545   hash_table->strtab[hash_table->strtabcount].dest_index
   9546     = hash_table->strtabcount;
   9547   hash_table->strtab[hash_table->strtabcount].destshndx_index
   9548     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
   9549 
   9550   flinfo->output_bfd->symcount += 1;
   9551   hash_table->strtabcount += 1;
   9552 
   9553   return 1;
   9554 }
   9555 
   9556 /* Swap symbols out to the symbol table and flush the output symbols to
   9557    the file.  */
   9558 
   9559 static bfd_boolean
   9560 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   9561 {
   9562   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   9563   bfd_size_type amt;
   9564   size_t i;
   9565   const struct elf_backend_data *bed;
   9566   bfd_byte *symbuf;
   9567   Elf_Internal_Shdr *hdr;
   9568   file_ptr pos;
   9569   bfd_boolean ret;
   9570 
   9571   if (!hash_table->strtabcount)
   9572     return TRUE;
   9573 
   9574   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   9575 
   9576   bed = get_elf_backend_data (flinfo->output_bfd);
   9577 
   9578   amt = bed->s->sizeof_sym * hash_table->strtabcount;
   9579   symbuf = (bfd_byte *) bfd_malloc (amt);
   9580   if (symbuf == NULL)
   9581     return FALSE;
   9582 
   9583   if (flinfo->symshndxbuf)
   9584     {
   9585       amt = sizeof (Elf_External_Sym_Shndx);
   9586       amt *= bfd_get_symcount (flinfo->output_bfd);
   9587       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   9588       if (flinfo->symshndxbuf == NULL)
   9589 	{
   9590 	  free (symbuf);
   9591 	  return FALSE;
   9592 	}
   9593     }
   9594 
   9595   for (i = 0; i < hash_table->strtabcount; i++)
   9596     {
   9597       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   9598       if (elfsym->sym.st_name == (unsigned long) -1)
   9599 	elfsym->sym.st_name = 0;
   9600       else
   9601 	elfsym->sym.st_name
   9602 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   9603 						    elfsym->sym.st_name);
   9604       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   9605 			       ((bfd_byte *) symbuf
   9606 				+ (elfsym->dest_index
   9607 				   * bed->s->sizeof_sym)),
   9608 			       (flinfo->symshndxbuf
   9609 				+ elfsym->destshndx_index));
   9610     }
   9611 
   9612   /* Allow the linker to examine the strtab and symtab now they are
   9613      populated.  */
   9614 
   9615   if (flinfo->info->callbacks->examine_strtab)
   9616     flinfo->info->callbacks->examine_strtab (hash_table->strtab,
   9617 					     hash_table->strtabcount,
   9618 					     flinfo->symstrtab);
   9619 
   9620   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   9621   pos = hdr->sh_offset + hdr->sh_size;
   9622   amt = hash_table->strtabcount * bed->s->sizeof_sym;
   9623   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   9624       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
   9625     {
   9626       hdr->sh_size += amt;
   9627       ret = TRUE;
   9628     }
   9629   else
   9630     ret = FALSE;
   9631 
   9632   free (symbuf);
   9633 
   9634   free (hash_table->strtab);
   9635   hash_table->strtab = NULL;
   9636 
   9637   return ret;
   9638 }
   9639 
   9640 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   9641 
   9642 static bfd_boolean
   9643 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   9644 {
   9645   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   9646       && sym->st_shndx < SHN_LORESERVE)
   9647     {
   9648       /* The gABI doesn't support dynamic symbols in output sections
   9649 	 beyond 64k.  */
   9650       _bfd_error_handler
   9651 	/* xgettext:c-format */
   9652 	(_("%pB: too many sections: %d (>= %d)"),
   9653 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   9654       bfd_set_error (bfd_error_nonrepresentable_section);
   9655       return FALSE;
   9656     }
   9657   return TRUE;
   9658 }
   9659 
   9660 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   9661    allowing an unsatisfied unversioned symbol in the DSO to match a
   9662    versioned symbol that would normally require an explicit version.
   9663    We also handle the case that a DSO references a hidden symbol
   9664    which may be satisfied by a versioned symbol in another DSO.  */
   9665 
   9666 static bfd_boolean
   9667 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   9668 				 const struct elf_backend_data *bed,
   9669 				 struct elf_link_hash_entry *h)
   9670 {
   9671   bfd *abfd;
   9672   struct elf_link_loaded_list *loaded;
   9673 
   9674   if (!is_elf_hash_table (info->hash))
   9675     return FALSE;
   9676 
   9677   /* Check indirect symbol.  */
   9678   while (h->root.type == bfd_link_hash_indirect)
   9679     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9680 
   9681   switch (h->root.type)
   9682     {
   9683     default:
   9684       abfd = NULL;
   9685       break;
   9686 
   9687     case bfd_link_hash_undefined:
   9688     case bfd_link_hash_undefweak:
   9689       abfd = h->root.u.undef.abfd;
   9690       if (abfd == NULL
   9691 	  || (abfd->flags & DYNAMIC) == 0
   9692 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   9693 	return FALSE;
   9694       break;
   9695 
   9696     case bfd_link_hash_defined:
   9697     case bfd_link_hash_defweak:
   9698       abfd = h->root.u.def.section->owner;
   9699       break;
   9700 
   9701     case bfd_link_hash_common:
   9702       abfd = h->root.u.c.p->section->owner;
   9703       break;
   9704     }
   9705   BFD_ASSERT (abfd != NULL);
   9706 
   9707   for (loaded = elf_hash_table (info)->loaded;
   9708        loaded != NULL;
   9709        loaded = loaded->next)
   9710     {
   9711       bfd *input;
   9712       Elf_Internal_Shdr *hdr;
   9713       size_t symcount;
   9714       size_t extsymcount;
   9715       size_t extsymoff;
   9716       Elf_Internal_Shdr *versymhdr;
   9717       Elf_Internal_Sym *isym;
   9718       Elf_Internal_Sym *isymend;
   9719       Elf_Internal_Sym *isymbuf;
   9720       Elf_External_Versym *ever;
   9721       Elf_External_Versym *extversym;
   9722 
   9723       input = loaded->abfd;
   9724 
   9725       /* We check each DSO for a possible hidden versioned definition.  */
   9726       if (input == abfd
   9727 	  || (input->flags & DYNAMIC) == 0
   9728 	  || elf_dynversym (input) == 0)
   9729 	continue;
   9730 
   9731       hdr = &elf_tdata (input)->dynsymtab_hdr;
   9732 
   9733       symcount = hdr->sh_size / bed->s->sizeof_sym;
   9734       if (elf_bad_symtab (input))
   9735 	{
   9736 	  extsymcount = symcount;
   9737 	  extsymoff = 0;
   9738 	}
   9739       else
   9740 	{
   9741 	  extsymcount = symcount - hdr->sh_info;
   9742 	  extsymoff = hdr->sh_info;
   9743 	}
   9744 
   9745       if (extsymcount == 0)
   9746 	continue;
   9747 
   9748       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   9749 				      NULL, NULL, NULL);
   9750       if (isymbuf == NULL)
   9751 	return FALSE;
   9752 
   9753       /* Read in any version definitions.  */
   9754       versymhdr = &elf_tdata (input)->dynversym_hdr;
   9755       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   9756       if (extversym == NULL)
   9757 	goto error_ret;
   9758 
   9759       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   9760 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
   9761 	      != versymhdr->sh_size))
   9762 	{
   9763 	  free (extversym);
   9764 	error_ret:
   9765 	  free (isymbuf);
   9766 	  return FALSE;
   9767 	}
   9768 
   9769       ever = extversym + extsymoff;
   9770       isymend = isymbuf + extsymcount;
   9771       for (isym = isymbuf; isym < isymend; isym++, ever++)
   9772 	{
   9773 	  const char *name;
   9774 	  Elf_Internal_Versym iver;
   9775 	  unsigned short version_index;
   9776 
   9777 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   9778 	      || isym->st_shndx == SHN_UNDEF)
   9779 	    continue;
   9780 
   9781 	  name = bfd_elf_string_from_elf_section (input,
   9782 						  hdr->sh_link,
   9783 						  isym->st_name);
   9784 	  if (strcmp (name, h->root.root.string) != 0)
   9785 	    continue;
   9786 
   9787 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   9788 
   9789 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   9790 	      && !(h->def_regular
   9791 		   && h->forced_local))
   9792 	    {
   9793 	      /* If we have a non-hidden versioned sym, then it should
   9794 		 have provided a definition for the undefined sym unless
   9795 		 it is defined in a non-shared object and forced local.
   9796 	       */
   9797 	      abort ();
   9798 	    }
   9799 
   9800 	  version_index = iver.vs_vers & VERSYM_VERSION;
   9801 	  if (version_index == 1 || version_index == 2)
   9802 	    {
   9803 	      /* This is the base or first version.  We can use it.  */
   9804 	      free (extversym);
   9805 	      free (isymbuf);
   9806 	      return TRUE;
   9807 	    }
   9808 	}
   9809 
   9810       free (extversym);
   9811       free (isymbuf);
   9812     }
   9813 
   9814   return FALSE;
   9815 }
   9816 
   9817 /* Convert ELF common symbol TYPE.  */
   9818 
   9819 static int
   9820 elf_link_convert_common_type (struct bfd_link_info *info, int type)
   9821 {
   9822   /* Commom symbol can only appear in relocatable link.  */
   9823   if (!bfd_link_relocatable (info))
   9824     abort ();
   9825   switch (info->elf_stt_common)
   9826     {
   9827     case unchanged:
   9828       break;
   9829     case elf_stt_common:
   9830       type = STT_COMMON;
   9831       break;
   9832     case no_elf_stt_common:
   9833       type = STT_OBJECT;
   9834       break;
   9835     }
   9836   return type;
   9837 }
   9838 
   9839 /* Add an external symbol to the symbol table.  This is called from
   9840    the hash table traversal routine.  When generating a shared object,
   9841    we go through the symbol table twice.  The first time we output
   9842    anything that might have been forced to local scope in a version
   9843    script.  The second time we output the symbols that are still
   9844    global symbols.  */
   9845 
   9846 static bfd_boolean
   9847 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   9848 {
   9849   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   9850   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   9851   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   9852   bfd_boolean strip;
   9853   Elf_Internal_Sym sym;
   9854   asection *input_sec;
   9855   const struct elf_backend_data *bed;
   9856   long indx;
   9857   int ret;
   9858   unsigned int type;
   9859 
   9860   if (h->root.type == bfd_link_hash_warning)
   9861     {
   9862       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9863       if (h->root.type == bfd_link_hash_new)
   9864 	return TRUE;
   9865     }
   9866 
   9867   /* Decide whether to output this symbol in this pass.  */
   9868   if (eoinfo->localsyms)
   9869     {
   9870       if (!h->forced_local)
   9871 	return TRUE;
   9872     }
   9873   else
   9874     {
   9875       if (h->forced_local)
   9876 	return TRUE;
   9877     }
   9878 
   9879   bed = get_elf_backend_data (flinfo->output_bfd);
   9880 
   9881   if (h->root.type == bfd_link_hash_undefined)
   9882     {
   9883       /* If we have an undefined symbol reference here then it must have
   9884 	 come from a shared library that is being linked in.  (Undefined
   9885 	 references in regular files have already been handled unless
   9886 	 they are in unreferenced sections which are removed by garbage
   9887 	 collection).  */
   9888       bfd_boolean ignore_undef = FALSE;
   9889 
   9890       /* Some symbols may be special in that the fact that they're
   9891 	 undefined can be safely ignored - let backend determine that.  */
   9892       if (bed->elf_backend_ignore_undef_symbol)
   9893 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   9894 
   9895       /* If we are reporting errors for this situation then do so now.  */
   9896       if (!ignore_undef
   9897 	  && h->ref_dynamic_nonweak
   9898 	  && (!h->ref_regular || flinfo->info->gc_sections)
   9899 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   9900 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   9901 	(*flinfo->info->callbacks->undefined_symbol)
   9902 	  (flinfo->info, h->root.root.string,
   9903 	   h->ref_regular ? NULL : h->root.u.undef.abfd,
   9904 	   NULL, 0,
   9905 	   flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
   9906 
   9907       /* Strip a global symbol defined in a discarded section.  */
   9908       if (h->indx == -3)
   9909 	return TRUE;
   9910     }
   9911 
   9912   /* We should also warn if a forced local symbol is referenced from
   9913      shared libraries.  */
   9914   if (bfd_link_executable (flinfo->info)
   9915       && h->forced_local
   9916       && h->ref_dynamic
   9917       && h->def_regular
   9918       && !h->dynamic_def
   9919       && h->ref_dynamic_nonweak
   9920       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   9921     {
   9922       bfd *def_bfd;
   9923       const char *msg;
   9924       struct elf_link_hash_entry *hi = h;
   9925 
   9926       /* Check indirect symbol.  */
   9927       while (hi->root.type == bfd_link_hash_indirect)
   9928 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   9929 
   9930       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   9931 	/* xgettext:c-format */
   9932 	msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
   9933       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   9934 	/* xgettext:c-format */
   9935 	msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
   9936       else
   9937 	/* xgettext:c-format */
   9938 	msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
   9939       def_bfd = flinfo->output_bfd;
   9940       if (hi->root.u.def.section != bfd_abs_section_ptr)
   9941 	def_bfd = hi->root.u.def.section->owner;
   9942       _bfd_error_handler (msg, flinfo->output_bfd,
   9943 			  h->root.root.string, def_bfd);
   9944       bfd_set_error (bfd_error_bad_value);
   9945       eoinfo->failed = TRUE;
   9946       return FALSE;
   9947     }
   9948 
   9949   /* We don't want to output symbols that have never been mentioned by
   9950      a regular file, or that we have been told to strip.  However, if
   9951      h->indx is set to -2, the symbol is used by a reloc and we must
   9952      output it.  */
   9953   strip = FALSE;
   9954   if (h->indx == -2)
   9955     ;
   9956   else if ((h->def_dynamic
   9957 	    || h->ref_dynamic
   9958 	    || h->root.type == bfd_link_hash_new)
   9959 	   && !h->def_regular
   9960 	   && !h->ref_regular)
   9961     strip = TRUE;
   9962   else if (flinfo->info->strip == strip_all)
   9963     strip = TRUE;
   9964   else if (flinfo->info->strip == strip_some
   9965 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   9966 			       h->root.root.string, FALSE, FALSE) == NULL)
   9967     strip = TRUE;
   9968   else if ((h->root.type == bfd_link_hash_defined
   9969 	    || h->root.type == bfd_link_hash_defweak)
   9970 	   && ((flinfo->info->strip_discarded
   9971 		&& discarded_section (h->root.u.def.section))
   9972 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   9973 		   && h->root.u.def.section->owner != NULL
   9974 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   9975     strip = TRUE;
   9976   else if ((h->root.type == bfd_link_hash_undefined
   9977 	    || h->root.type == bfd_link_hash_undefweak)
   9978 	   && h->root.u.undef.abfd != NULL
   9979 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   9980     strip = TRUE;
   9981 
   9982   type = h->type;
   9983 
   9984   /* If we're stripping it, and it's not a dynamic symbol, there's
   9985      nothing else to do.   However, if it is a forced local symbol or
   9986      an ifunc symbol we need to give the backend finish_dynamic_symbol
   9987      function a chance to make it dynamic.  */
   9988   if (strip
   9989       && h->dynindx == -1
   9990       && type != STT_GNU_IFUNC
   9991       && !h->forced_local)
   9992     return TRUE;
   9993 
   9994   sym.st_value = 0;
   9995   sym.st_size = h->size;
   9996   sym.st_other = h->other;
   9997   switch (h->root.type)
   9998     {
   9999     default:
   10000     case bfd_link_hash_new:
   10001     case bfd_link_hash_warning:
   10002       abort ();
   10003       return FALSE;
   10004 
   10005     case bfd_link_hash_undefined:
   10006     case bfd_link_hash_undefweak:
   10007       input_sec = bfd_und_section_ptr;
   10008       sym.st_shndx = SHN_UNDEF;
   10009       break;
   10010 
   10011     case bfd_link_hash_defined:
   10012     case bfd_link_hash_defweak:
   10013       {
   10014 	input_sec = h->root.u.def.section;
   10015 	if (input_sec->output_section != NULL)
   10016 	  {
   10017 	    sym.st_shndx =
   10018 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   10019 						 input_sec->output_section);
   10020 	    if (sym.st_shndx == SHN_BAD)
   10021 	      {
   10022 		_bfd_error_handler
   10023 		  /* xgettext:c-format */
   10024 		  (_("%pB: could not find output section %pA for input section %pA"),
   10025 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   10026 		bfd_set_error (bfd_error_nonrepresentable_section);
   10027 		eoinfo->failed = TRUE;
   10028 		return FALSE;
   10029 	      }
   10030 
   10031 	    /* ELF symbols in relocatable files are section relative,
   10032 	       but in nonrelocatable files they are virtual
   10033 	       addresses.  */
   10034 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   10035 	    if (!bfd_link_relocatable (flinfo->info))
   10036 	      {
   10037 		sym.st_value += input_sec->output_section->vma;
   10038 		if (h->type == STT_TLS)
   10039 		  {
   10040 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   10041 		    if (tls_sec != NULL)
   10042 		      sym.st_value -= tls_sec->vma;
   10043 		  }
   10044 	      }
   10045 	  }
   10046 	else
   10047 	  {
   10048 	    BFD_ASSERT (input_sec->owner == NULL
   10049 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   10050 	    sym.st_shndx = SHN_UNDEF;
   10051 	    input_sec = bfd_und_section_ptr;
   10052 	  }
   10053       }
   10054       break;
   10055 
   10056     case bfd_link_hash_common:
   10057       input_sec = h->root.u.c.p->section;
   10058       sym.st_shndx = bed->common_section_index (input_sec);
   10059       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   10060       break;
   10061 
   10062     case bfd_link_hash_indirect:
   10063       /* These symbols are created by symbol versioning.  They point
   10064 	 to the decorated version of the name.  For example, if the
   10065 	 symbol foo@@GNU_1.2 is the default, which should be used when
   10066 	 foo is used with no version, then we add an indirect symbol
   10067 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   10068 	 since the indirected symbol is already in the hash table.  */
   10069       return TRUE;
   10070     }
   10071 
   10072   if (type == STT_COMMON || type == STT_OBJECT)
   10073     switch (h->root.type)
   10074       {
   10075       case bfd_link_hash_common:
   10076 	type = elf_link_convert_common_type (flinfo->info, type);
   10077 	break;
   10078       case bfd_link_hash_defined:
   10079       case bfd_link_hash_defweak:
   10080 	if (bed->common_definition (&sym))
   10081 	  type = elf_link_convert_common_type (flinfo->info, type);
   10082 	else
   10083 	  type = STT_OBJECT;
   10084 	break;
   10085       case bfd_link_hash_undefined:
   10086       case bfd_link_hash_undefweak:
   10087 	break;
   10088       default:
   10089 	abort ();
   10090       }
   10091 
   10092   if (h->forced_local)
   10093     {
   10094       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
   10095       /* Turn off visibility on local symbol.  */
   10096       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   10097     }
   10098   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   10099   else if (h->unique_global && h->def_regular)
   10100     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
   10101   else if (h->root.type == bfd_link_hash_undefweak
   10102 	   || h->root.type == bfd_link_hash_defweak)
   10103     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
   10104   else
   10105     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   10106   sym.st_target_internal = h->target_internal;
   10107 
   10108   /* Give the processor backend a chance to tweak the symbol value,
   10109      and also to finish up anything that needs to be done for this
   10110      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   10111      forced local syms when non-shared is due to a historical quirk.
   10112      STT_GNU_IFUNC symbol must go through PLT.  */
   10113   if ((h->type == STT_GNU_IFUNC
   10114        && h->def_regular
   10115        && !bfd_link_relocatable (flinfo->info))
   10116       || ((h->dynindx != -1
   10117 	   || h->forced_local)
   10118 	  && ((bfd_link_pic (flinfo->info)
   10119 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   10120 		   || h->root.type != bfd_link_hash_undefweak))
   10121 	      || !h->forced_local)
   10122 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   10123     {
   10124       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   10125 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   10126 	{
   10127 	  eoinfo->failed = TRUE;
   10128 	  return FALSE;
   10129 	}
   10130     }
   10131 
   10132   /* If we are marking the symbol as undefined, and there are no
   10133      non-weak references to this symbol from a regular object, then
   10134      mark the symbol as weak undefined; if there are non-weak
   10135      references, mark the symbol as strong.  We can't do this earlier,
   10136      because it might not be marked as undefined until the
   10137      finish_dynamic_symbol routine gets through with it.  */
   10138   if (sym.st_shndx == SHN_UNDEF
   10139       && h->ref_regular
   10140       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   10141 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   10142     {
   10143       int bindtype;
   10144       type = ELF_ST_TYPE (sym.st_info);
   10145 
   10146       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   10147       if (type == STT_GNU_IFUNC)
   10148 	type = STT_FUNC;
   10149 
   10150       if (h->ref_regular_nonweak)
   10151 	bindtype = STB_GLOBAL;
   10152       else
   10153 	bindtype = STB_WEAK;
   10154       sym.st_info = ELF_ST_INFO (bindtype, type);
   10155     }
   10156 
   10157   /* If this is a symbol defined in a dynamic library, don't use the
   10158      symbol size from the dynamic library.  Relinking an executable
   10159      against a new library may introduce gratuitous changes in the
   10160      executable's symbols if we keep the size.  */
   10161   if (sym.st_shndx == SHN_UNDEF
   10162       && !h->def_regular
   10163       && h->def_dynamic)
   10164     sym.st_size = 0;
   10165 
   10166   /* If a non-weak symbol with non-default visibility is not defined
   10167      locally, it is a fatal error.  */
   10168   if (!bfd_link_relocatable (flinfo->info)
   10169       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   10170       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   10171       && h->root.type == bfd_link_hash_undefined
   10172       && !h->def_regular)
   10173     {
   10174       const char *msg;
   10175 
   10176       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   10177 	/* xgettext:c-format */
   10178 	msg = _("%pB: protected symbol `%s' isn't defined");
   10179       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   10180 	/* xgettext:c-format */
   10181 	msg = _("%pB: internal symbol `%s' isn't defined");
   10182       else
   10183 	/* xgettext:c-format */
   10184 	msg = _("%pB: hidden symbol `%s' isn't defined");
   10185       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
   10186       bfd_set_error (bfd_error_bad_value);
   10187       eoinfo->failed = TRUE;
   10188       return FALSE;
   10189     }
   10190 
   10191   /* If this symbol should be put in the .dynsym section, then put it
   10192      there now.  We already know the symbol index.  We also fill in
   10193      the entry in the .hash section.  */
   10194   if (h->dynindx != -1
   10195       && elf_hash_table (flinfo->info)->dynamic_sections_created
   10196       && elf_hash_table (flinfo->info)->dynsym != NULL
   10197       && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
   10198     {
   10199       bfd_byte *esym;
   10200 
   10201       /* Since there is no version information in the dynamic string,
   10202 	 if there is no version info in symbol version section, we will
   10203 	 have a run-time problem if not linking executable, referenced
   10204 	 by shared library, or not bound locally.  */
   10205       if (h->verinfo.verdef == NULL
   10206 	  && (!bfd_link_executable (flinfo->info)
   10207 	      || h->ref_dynamic
   10208 	      || !h->def_regular))
   10209 	{
   10210 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   10211 
   10212 	  if (p && p [1] != '\0')
   10213 	    {
   10214 	      _bfd_error_handler
   10215 		/* xgettext:c-format */
   10216 		(_("%pB: no symbol version section for versioned symbol `%s'"),
   10217 		 flinfo->output_bfd, h->root.root.string);
   10218 	      eoinfo->failed = TRUE;
   10219 	      return FALSE;
   10220 	    }
   10221 	}
   10222 
   10223       sym.st_name = h->dynstr_index;
   10224       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   10225 	      + h->dynindx * bed->s->sizeof_sym);
   10226       if (!check_dynsym (flinfo->output_bfd, &sym))
   10227 	{
   10228 	  eoinfo->failed = TRUE;
   10229 	  return FALSE;
   10230 	}
   10231       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   10232 
   10233       if (flinfo->hash_sec != NULL)
   10234 	{
   10235 	  size_t hash_entry_size;
   10236 	  bfd_byte *bucketpos;
   10237 	  bfd_vma chain;
   10238 	  size_t bucketcount;
   10239 	  size_t bucket;
   10240 
   10241 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   10242 	  bucket = h->u.elf_hash_value % bucketcount;
   10243 
   10244 	  hash_entry_size
   10245 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   10246 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   10247 		       + (bucket + 2) * hash_entry_size);
   10248 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   10249 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   10250 		   bucketpos);
   10251 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   10252 		   ((bfd_byte *) flinfo->hash_sec->contents
   10253 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   10254 	}
   10255 
   10256       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   10257 	{
   10258 	  Elf_Internal_Versym iversym;
   10259 	  Elf_External_Versym *eversym;
   10260 
   10261 	  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   10262 	    {
   10263 	      if (h->verinfo.verdef == NULL
   10264 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   10265 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   10266 		iversym.vs_vers = 0;
   10267 	      else
   10268 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   10269 	    }
   10270 	  else
   10271 	    {
   10272 	      if (h->verinfo.vertree == NULL)
   10273 		iversym.vs_vers = 1;
   10274 	      else
   10275 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   10276 	      if (flinfo->info->create_default_symver)
   10277 		iversym.vs_vers++;
   10278 	    }
   10279 
   10280 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
   10281 	     defined locally.  */
   10282 	  if (h->versioned == versioned_hidden && h->def_regular)
   10283 	    iversym.vs_vers |= VERSYM_HIDDEN;
   10284 
   10285 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   10286 	  eversym += h->dynindx;
   10287 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   10288 	}
   10289     }
   10290 
   10291   /* If the symbol is undefined, and we didn't output it to .dynsym,
   10292      strip it from .symtab too.  Obviously we can't do this for
   10293      relocatable output or when needed for --emit-relocs.  */
   10294   else if (input_sec == bfd_und_section_ptr
   10295 	   && h->indx != -2
   10296 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
   10297 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
   10298 	   && !bfd_link_relocatable (flinfo->info))
   10299     return TRUE;
   10300 
   10301   /* Also strip others that we couldn't earlier due to dynamic symbol
   10302      processing.  */
   10303   if (strip)
   10304     return TRUE;
   10305   if ((input_sec->flags & SEC_EXCLUDE) != 0)
   10306     return TRUE;
   10307 
   10308   /* Output a FILE symbol so that following locals are not associated
   10309      with the wrong input file.  We need one for forced local symbols
   10310      if we've seen more than one FILE symbol or when we have exactly
   10311      one FILE symbol but global symbols are present in a file other
   10312      than the one with the FILE symbol.  We also need one if linker
   10313      defined symbols are present.  In practice these conditions are
   10314      always met, so just emit the FILE symbol unconditionally.  */
   10315   if (eoinfo->localsyms
   10316       && !eoinfo->file_sym_done
   10317       && eoinfo->flinfo->filesym_count != 0)
   10318     {
   10319       Elf_Internal_Sym fsym;
   10320 
   10321       memset (&fsym, 0, sizeof (fsym));
   10322       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   10323       fsym.st_shndx = SHN_ABS;
   10324       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   10325 				      bfd_und_section_ptr, NULL))
   10326 	return FALSE;
   10327 
   10328       eoinfo->file_sym_done = TRUE;
   10329     }
   10330 
   10331   indx = bfd_get_symcount (flinfo->output_bfd);
   10332   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   10333 				   input_sec, h);
   10334   if (ret == 0)
   10335     {
   10336       eoinfo->failed = TRUE;
   10337       return FALSE;
   10338     }
   10339   else if (ret == 1)
   10340     h->indx = indx;
   10341   else if (h->indx == -2)
   10342     abort();
   10343 
   10344   return TRUE;
   10345 }
   10346 
   10347 /* Return TRUE if special handling is done for relocs in SEC against
   10348    symbols defined in discarded sections.  */
   10349 
   10350 static bfd_boolean
   10351 elf_section_ignore_discarded_relocs (asection *sec)
   10352 {
   10353   const struct elf_backend_data *bed;
   10354 
   10355   switch (sec->sec_info_type)
   10356     {
   10357     case SEC_INFO_TYPE_STABS:
   10358     case SEC_INFO_TYPE_EH_FRAME:
   10359     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   10360       return TRUE;
   10361     default:
   10362       break;
   10363     }
   10364 
   10365   bed = get_elf_backend_data (sec->owner);
   10366   if (bed->elf_backend_ignore_discarded_relocs != NULL
   10367       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   10368     return TRUE;
   10369 
   10370   return FALSE;
   10371 }
   10372 
   10373 /* Return a mask saying how ld should treat relocations in SEC against
   10374    symbols defined in discarded sections.  If this function returns
   10375    COMPLAIN set, ld will issue a warning message.  If this function
   10376    returns PRETEND set, and the discarded section was link-once and the
   10377    same size as the kept link-once section, ld will pretend that the
   10378    symbol was actually defined in the kept section.  Otherwise ld will
   10379    zero the reloc (at least that is the intent, but some cooperation by
   10380    the target dependent code is needed, particularly for REL targets).  */
   10381 
   10382 unsigned int
   10383 _bfd_elf_default_action_discarded (asection *sec)
   10384 {
   10385   if (sec->flags & SEC_DEBUGGING)
   10386     return PRETEND;
   10387 
   10388   if (strcmp (".eh_frame", sec->name) == 0)
   10389     return 0;
   10390 
   10391   if (strcmp (".gcc_except_table", sec->name) == 0)
   10392     return 0;
   10393 
   10394   return COMPLAIN | PRETEND;
   10395 }
   10396 
   10397 /* Find a match between a section and a member of a section group.  */
   10398 
   10399 static asection *
   10400 match_group_member (asection *sec, asection *group,
   10401 		    struct bfd_link_info *info)
   10402 {
   10403   asection *first = elf_next_in_group (group);
   10404   asection *s = first;
   10405 
   10406   while (s != NULL)
   10407     {
   10408       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   10409 	return s;
   10410 
   10411       s = elf_next_in_group (s);
   10412       if (s == first)
   10413 	break;
   10414     }
   10415 
   10416   return NULL;
   10417 }
   10418 
   10419 /* Check if the kept section of a discarded section SEC can be used
   10420    to replace it.  Return the replacement if it is OK.  Otherwise return
   10421    NULL.  */
   10422 
   10423 asection *
   10424 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   10425 {
   10426   asection *kept;
   10427 
   10428   kept = sec->kept_section;
   10429   if (kept != NULL)
   10430     {
   10431       if ((kept->flags & SEC_GROUP) != 0)
   10432 	kept = match_group_member (sec, kept, info);
   10433       if (kept != NULL
   10434 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   10435 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
   10436 	kept = NULL;
   10437       sec->kept_section = kept;
   10438     }
   10439   return kept;
   10440 }
   10441 
   10442 /* Link an input file into the linker output file.  This function
   10443    handles all the sections and relocations of the input file at once.
   10444    This is so that we only have to read the local symbols once, and
   10445    don't have to keep them in memory.  */
   10446 
   10447 static bfd_boolean
   10448 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   10449 {
   10450   int (*relocate_section)
   10451     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   10452      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   10453   bfd *output_bfd;
   10454   Elf_Internal_Shdr *symtab_hdr;
   10455   size_t locsymcount;
   10456   size_t extsymoff;
   10457   Elf_Internal_Sym *isymbuf;
   10458   Elf_Internal_Sym *isym;
   10459   Elf_Internal_Sym *isymend;
   10460   long *pindex;
   10461   asection **ppsection;
   10462   asection *o;
   10463   const struct elf_backend_data *bed;
   10464   struct elf_link_hash_entry **sym_hashes;
   10465   bfd_size_type address_size;
   10466   bfd_vma r_type_mask;
   10467   int r_sym_shift;
   10468   bfd_boolean have_file_sym = FALSE;
   10469 
   10470   output_bfd = flinfo->output_bfd;
   10471   bed = get_elf_backend_data (output_bfd);
   10472   relocate_section = bed->elf_backend_relocate_section;
   10473 
   10474   /* If this is a dynamic object, we don't want to do anything here:
   10475      we don't want the local symbols, and we don't want the section
   10476      contents.  */
   10477   if ((input_bfd->flags & DYNAMIC) != 0)
   10478     return TRUE;
   10479 
   10480   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10481   if (elf_bad_symtab (input_bfd))
   10482     {
   10483       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   10484       extsymoff = 0;
   10485     }
   10486   else
   10487     {
   10488       locsymcount = symtab_hdr->sh_info;
   10489       extsymoff = symtab_hdr->sh_info;
   10490     }
   10491 
   10492   /* Read the local symbols.  */
   10493   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   10494   if (isymbuf == NULL && locsymcount != 0)
   10495     {
   10496       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   10497 				      flinfo->internal_syms,
   10498 				      flinfo->external_syms,
   10499 				      flinfo->locsym_shndx);
   10500       if (isymbuf == NULL)
   10501 	return FALSE;
   10502     }
   10503 
   10504   /* Find local symbol sections and adjust values of symbols in
   10505      SEC_MERGE sections.  Write out those local symbols we know are
   10506      going into the output file.  */
   10507   isymend = isymbuf + locsymcount;
   10508   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   10509        isym < isymend;
   10510        isym++, pindex++, ppsection++)
   10511     {
   10512       asection *isec;
   10513       const char *name;
   10514       Elf_Internal_Sym osym;
   10515       long indx;
   10516       int ret;
   10517 
   10518       *pindex = -1;
   10519 
   10520       if (elf_bad_symtab (input_bfd))
   10521 	{
   10522 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   10523 	    {
   10524 	      *ppsection = NULL;
   10525 	      continue;
   10526 	    }
   10527 	}
   10528 
   10529       if (isym->st_shndx == SHN_UNDEF)
   10530 	isec = bfd_und_section_ptr;
   10531       else if (isym->st_shndx == SHN_ABS)
   10532 	isec = bfd_abs_section_ptr;
   10533       else if (isym->st_shndx == SHN_COMMON)
   10534 	isec = bfd_com_section_ptr;
   10535       else
   10536 	{
   10537 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   10538 	  if (isec == NULL)
   10539 	    {
   10540 	      /* Don't attempt to output symbols with st_shnx in the
   10541 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   10542 	      isec = bfd_und_section_ptr;
   10543 	    }
   10544 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   10545 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   10546 	    isym->st_value =
   10547 	      _bfd_merged_section_offset (output_bfd, &isec,
   10548 					  elf_section_data (isec)->sec_info,
   10549 					  isym->st_value);
   10550 	}
   10551 
   10552       *ppsection = isec;
   10553 
   10554       /* Don't output the first, undefined, symbol.  In fact, don't
   10555 	 output any undefined local symbol.  */
   10556       if (isec == bfd_und_section_ptr)
   10557 	continue;
   10558 
   10559       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   10560 	{
   10561 	  /* We never output section symbols.  Instead, we use the
   10562 	     section symbol of the corresponding section in the output
   10563 	     file.  */
   10564 	  continue;
   10565 	}
   10566 
   10567       /* If we are stripping all symbols, we don't want to output this
   10568 	 one.  */
   10569       if (flinfo->info->strip == strip_all)
   10570 	continue;
   10571 
   10572       /* If we are discarding all local symbols, we don't want to
   10573 	 output this one.  If we are generating a relocatable output
   10574 	 file, then some of the local symbols may be required by
   10575 	 relocs; we output them below as we discover that they are
   10576 	 needed.  */
   10577       if (flinfo->info->discard == discard_all)
   10578 	continue;
   10579 
   10580       /* If this symbol is defined in a section which we are
   10581 	 discarding, we don't need to keep it.  */
   10582       if (isym->st_shndx != SHN_UNDEF
   10583 	  && isym->st_shndx < SHN_LORESERVE
   10584 	  && bfd_section_removed_from_list (output_bfd,
   10585 					    isec->output_section))
   10586 	continue;
   10587 
   10588       /* Get the name of the symbol.  */
   10589       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   10590 					      isym->st_name);
   10591       if (name == NULL)
   10592 	return FALSE;
   10593 
   10594       /* See if we are discarding symbols with this name.  */
   10595       if ((flinfo->info->strip == strip_some
   10596 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
   10597 	       == NULL))
   10598 	  || (((flinfo->info->discard == discard_sec_merge
   10599 		&& (isec->flags & SEC_MERGE)
   10600 		&& !bfd_link_relocatable (flinfo->info))
   10601 	       || flinfo->info->discard == discard_l)
   10602 	      && bfd_is_local_label_name (input_bfd, name)))
   10603 	continue;
   10604 
   10605       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   10606 	{
   10607 	  if (input_bfd->lto_output)
   10608 	    /* -flto puts a temp file name here.  This means builds
   10609 	       are not reproducible.  Discard the symbol.  */
   10610 	    continue;
   10611 	  have_file_sym = TRUE;
   10612 	  flinfo->filesym_count += 1;
   10613 	}
   10614       if (!have_file_sym)
   10615 	{
   10616 	  /* In the absence of debug info, bfd_find_nearest_line uses
   10617 	     FILE symbols to determine the source file for local
   10618 	     function symbols.  Provide a FILE symbol here if input
   10619 	     files lack such, so that their symbols won't be
   10620 	     associated with a previous input file.  It's not the
   10621 	     source file, but the best we can do.  */
   10622 	  have_file_sym = TRUE;
   10623 	  flinfo->filesym_count += 1;
   10624 	  memset (&osym, 0, sizeof (osym));
   10625 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   10626 	  osym.st_shndx = SHN_ABS;
   10627 	  if (!elf_link_output_symstrtab (flinfo,
   10628 					  (input_bfd->lto_output ? NULL
   10629 					   : input_bfd->filename),
   10630 					  &osym, bfd_abs_section_ptr,
   10631 					  NULL))
   10632 	    return FALSE;
   10633 	}
   10634 
   10635       osym = *isym;
   10636 
   10637       /* Adjust the section index for the output file.  */
   10638       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   10639 							 isec->output_section);
   10640       if (osym.st_shndx == SHN_BAD)
   10641 	return FALSE;
   10642 
   10643       /* ELF symbols in relocatable files are section relative, but
   10644 	 in executable files they are virtual addresses.  Note that
   10645 	 this code assumes that all ELF sections have an associated
   10646 	 BFD section with a reasonable value for output_offset; below
   10647 	 we assume that they also have a reasonable value for
   10648 	 output_section.  Any special sections must be set up to meet
   10649 	 these requirements.  */
   10650       osym.st_value += isec->output_offset;
   10651       if (!bfd_link_relocatable (flinfo->info))
   10652 	{
   10653 	  osym.st_value += isec->output_section->vma;
   10654 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   10655 	    {
   10656 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   10657 	      if (elf_hash_table (flinfo->info)->tls_sec != NULL)
   10658 		osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   10659 	      else
   10660 		osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
   10661 					    STT_NOTYPE);
   10662 	    }
   10663 	}
   10664 
   10665       indx = bfd_get_symcount (output_bfd);
   10666       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   10667       if (ret == 0)
   10668 	return FALSE;
   10669       else if (ret == 1)
   10670 	*pindex = indx;
   10671     }
   10672 
   10673   if (bed->s->arch_size == 32)
   10674     {
   10675       r_type_mask = 0xff;
   10676       r_sym_shift = 8;
   10677       address_size = 4;
   10678     }
   10679   else
   10680     {
   10681       r_type_mask = 0xffffffff;
   10682       r_sym_shift = 32;
   10683       address_size = 8;
   10684     }
   10685 
   10686   /* Relocate the contents of each section.  */
   10687   sym_hashes = elf_sym_hashes (input_bfd);
   10688   for (o = input_bfd->sections; o != NULL; o = o->next)
   10689     {
   10690       bfd_byte *contents;
   10691 
   10692       if (! o->linker_mark)
   10693 	{
   10694 	  /* This section was omitted from the link.  */
   10695 	  continue;
   10696 	}
   10697 
   10698       if (!flinfo->info->resolve_section_groups
   10699 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   10700 	{
   10701 	  /* Deal with the group signature symbol.  */
   10702 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   10703 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   10704 	  asection *osec = o->output_section;
   10705 
   10706 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
   10707 	  if (symndx >= locsymcount
   10708 	      || (elf_bad_symtab (input_bfd)
   10709 		  && flinfo->sections[symndx] == NULL))
   10710 	    {
   10711 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
   10712 	      while (h->root.type == bfd_link_hash_indirect
   10713 		     || h->root.type == bfd_link_hash_warning)
   10714 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10715 	      /* Arrange for symbol to be output.  */
   10716 	      h->indx = -2;
   10717 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   10718 	    }
   10719 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   10720 	    {
   10721 	      /* We'll use the output section target_index.  */
   10722 	      asection *sec = flinfo->sections[symndx]->output_section;
   10723 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   10724 	    }
   10725 	  else
   10726 	    {
   10727 	      if (flinfo->indices[symndx] == -1)
   10728 		{
   10729 		  /* Otherwise output the local symbol now.  */
   10730 		  Elf_Internal_Sym sym = isymbuf[symndx];
   10731 		  asection *sec = flinfo->sections[symndx]->output_section;
   10732 		  const char *name;
   10733 		  long indx;
   10734 		  int ret;
   10735 
   10736 		  name = bfd_elf_string_from_elf_section (input_bfd,
   10737 							  symtab_hdr->sh_link,
   10738 							  sym.st_name);
   10739 		  if (name == NULL)
   10740 		    return FALSE;
   10741 
   10742 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   10743 								    sec);
   10744 		  if (sym.st_shndx == SHN_BAD)
   10745 		    return FALSE;
   10746 
   10747 		  sym.st_value += o->output_offset;
   10748 
   10749 		  indx = bfd_get_symcount (output_bfd);
   10750 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   10751 						   NULL);
   10752 		  if (ret == 0)
   10753 		    return FALSE;
   10754 		  else if (ret == 1)
   10755 		    flinfo->indices[symndx] = indx;
   10756 		  else
   10757 		    abort ();
   10758 		}
   10759 	      elf_section_data (osec)->this_hdr.sh_info
   10760 		= flinfo->indices[symndx];
   10761 	    }
   10762 	}
   10763 
   10764       if ((o->flags & SEC_HAS_CONTENTS) == 0
   10765 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   10766 	continue;
   10767 
   10768       if ((o->flags & SEC_LINKER_CREATED) != 0)
   10769 	{
   10770 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   10771 	     or somesuch.  */
   10772 	  continue;
   10773 	}
   10774 
   10775       /* Get the contents of the section.  They have been cached by a
   10776 	 relaxation routine.  Note that o is a section in an input
   10777 	 file, so the contents field will not have been set by any of
   10778 	 the routines which work on output files.  */
   10779       if (elf_section_data (o)->this_hdr.contents != NULL)
   10780 	{
   10781 	  contents = elf_section_data (o)->this_hdr.contents;
   10782 	  if (bed->caches_rawsize
   10783 	      && o->rawsize != 0
   10784 	      && o->rawsize < o->size)
   10785 	    {
   10786 	      memcpy (flinfo->contents, contents, o->rawsize);
   10787 	      contents = flinfo->contents;
   10788 	    }
   10789 	}
   10790       else
   10791 	{
   10792 	  contents = flinfo->contents;
   10793 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   10794 	    return FALSE;
   10795 	}
   10796 
   10797       if ((o->flags & SEC_RELOC) != 0)
   10798 	{
   10799 	  Elf_Internal_Rela *internal_relocs;
   10800 	  Elf_Internal_Rela *rel, *relend;
   10801 	  int action_discarded;
   10802 	  int ret;
   10803 
   10804 	  /* Get the swapped relocs.  */
   10805 	  internal_relocs
   10806 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
   10807 					 flinfo->internal_relocs, FALSE);
   10808 	  if (internal_relocs == NULL
   10809 	      && o->reloc_count > 0)
   10810 	    return FALSE;
   10811 
   10812 	  /* We need to reverse-copy input .ctors/.dtors sections if
   10813 	     they are placed in .init_array/.finit_array for output.  */
   10814 	  if (o->size > address_size
   10815 	      && ((strncmp (o->name, ".ctors", 6) == 0
   10816 		   && strcmp (o->output_section->name,
   10817 			      ".init_array") == 0)
   10818 		  || (strncmp (o->name, ".dtors", 6) == 0
   10819 		      && strcmp (o->output_section->name,
   10820 				 ".fini_array") == 0))
   10821 	      && (o->name[6] == 0 || o->name[6] == '.'))
   10822 	    {
   10823 	      if (o->size * bed->s->int_rels_per_ext_rel
   10824 		  != o->reloc_count * address_size)
   10825 		{
   10826 		  _bfd_error_handler
   10827 		    /* xgettext:c-format */
   10828 		    (_("error: %pB: size of section %pA is not "
   10829 		       "multiple of address size"),
   10830 		     input_bfd, o);
   10831 		  bfd_set_error (bfd_error_bad_value);
   10832 		  return FALSE;
   10833 		}
   10834 	      o->flags |= SEC_ELF_REVERSE_COPY;
   10835 	    }
   10836 
   10837 	  action_discarded = -1;
   10838 	  if (!elf_section_ignore_discarded_relocs (o))
   10839 	    action_discarded = (*bed->action_discarded) (o);
   10840 
   10841 	  /* Run through the relocs evaluating complex reloc symbols and
   10842 	     looking for relocs against symbols from discarded sections
   10843 	     or section symbols from removed link-once sections.
   10844 	     Complain about relocs against discarded sections.  Zero
   10845 	     relocs against removed link-once sections.  */
   10846 
   10847 	  rel = internal_relocs;
   10848 	  relend = rel + o->reloc_count;
   10849 	  for ( ; rel < relend; rel++)
   10850 	    {
   10851 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   10852 	      unsigned int s_type;
   10853 	      asection **ps, *sec;
   10854 	      struct elf_link_hash_entry *h = NULL;
   10855 	      const char *sym_name;
   10856 
   10857 	      if (r_symndx == STN_UNDEF)
   10858 		continue;
   10859 
   10860 	      if (r_symndx >= locsymcount
   10861 		  || (elf_bad_symtab (input_bfd)
   10862 		      && flinfo->sections[r_symndx] == NULL))
   10863 		{
   10864 		  h = sym_hashes[r_symndx - extsymoff];
   10865 
   10866 		  /* Badly formatted input files can contain relocs that
   10867 		     reference non-existant symbols.  Check here so that
   10868 		     we do not seg fault.  */
   10869 		  if (h == NULL)
   10870 		    {
   10871 		      _bfd_error_handler
   10872 			/* xgettext:c-format */
   10873 			(_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
   10874 			   "that references a non-existent global symbol"),
   10875 			 input_bfd, (uint64_t) rel->r_info, o);
   10876 		      bfd_set_error (bfd_error_bad_value);
   10877 		      return FALSE;
   10878 		    }
   10879 
   10880 		  while (h->root.type == bfd_link_hash_indirect
   10881 			 || h->root.type == bfd_link_hash_warning)
   10882 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10883 
   10884 		  s_type = h->type;
   10885 
   10886 		  /* If a plugin symbol is referenced from a non-IR file,
   10887 		     mark the symbol as undefined.  Note that the
   10888 		     linker may attach linker created dynamic sections
   10889 		     to the plugin bfd.  Symbols defined in linker
   10890 		     created sections are not plugin symbols.  */
   10891 		  if ((h->root.non_ir_ref_regular
   10892 		       || h->root.non_ir_ref_dynamic)
   10893 		      && (h->root.type == bfd_link_hash_defined
   10894 			  || h->root.type == bfd_link_hash_defweak)
   10895 		      && (h->root.u.def.section->flags
   10896 			  & SEC_LINKER_CREATED) == 0
   10897 		      && h->root.u.def.section->owner != NULL
   10898 		      && (h->root.u.def.section->owner->flags
   10899 			  & BFD_PLUGIN) != 0)
   10900 		    {
   10901 		      h->root.type = bfd_link_hash_undefined;
   10902 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   10903 		    }
   10904 
   10905 		  ps = NULL;
   10906 		  if (h->root.type == bfd_link_hash_defined
   10907 		      || h->root.type == bfd_link_hash_defweak)
   10908 		    ps = &h->root.u.def.section;
   10909 
   10910 		  sym_name = h->root.root.string;
   10911 		}
   10912 	      else
   10913 		{
   10914 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   10915 
   10916 		  s_type = ELF_ST_TYPE (sym->st_info);
   10917 		  ps = &flinfo->sections[r_symndx];
   10918 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10919 					       sym, *ps);
   10920 		}
   10921 
   10922 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   10923 		  && !bfd_link_relocatable (flinfo->info))
   10924 		{
   10925 		  bfd_vma val;
   10926 		  bfd_vma dot = (rel->r_offset
   10927 				 + o->output_offset + o->output_section->vma);
   10928 #ifdef DEBUG
   10929 		  printf ("Encountered a complex symbol!");
   10930 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   10931 			  input_bfd->filename, o->name,
   10932 			  (long) (rel - internal_relocs));
   10933 		  printf (" symbol: idx  %8.8lx, name %s\n",
   10934 			  r_symndx, sym_name);
   10935 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   10936 			  (unsigned long) rel->r_info,
   10937 			  (unsigned long) rel->r_offset);
   10938 #endif
   10939 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   10940 				    isymbuf, locsymcount, s_type == STT_SRELC))
   10941 		    return FALSE;
   10942 
   10943 		  /* Symbol evaluated OK.  Update to absolute value.  */
   10944 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   10945 				    r_symndx, val);
   10946 		  continue;
   10947 		}
   10948 
   10949 	      if (action_discarded != -1 && ps != NULL)
   10950 		{
   10951 		  /* Complain if the definition comes from a
   10952 		     discarded section.  */
   10953 		  if ((sec = *ps) != NULL && discarded_section (sec))
   10954 		    {
   10955 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   10956 		      if (action_discarded & COMPLAIN)
   10957 			(*flinfo->info->callbacks->einfo)
   10958 			  /* xgettext:c-format */
   10959 			  (_("%X`%s' referenced in section `%pA' of %pB: "
   10960 			     "defined in discarded section `%pA' of %pB\n"),
   10961 			   sym_name, o, input_bfd, sec, sec->owner);
   10962 
   10963 		      /* Try to do the best we can to support buggy old
   10964 			 versions of gcc.  Pretend that the symbol is
   10965 			 really defined in the kept linkonce section.
   10966 			 FIXME: This is quite broken.  Modifying the
   10967 			 symbol here means we will be changing all later
   10968 			 uses of the symbol, not just in this section.  */
   10969 		      if (action_discarded & PRETEND)
   10970 			{
   10971 			  asection *kept;
   10972 
   10973 			  kept = _bfd_elf_check_kept_section (sec,
   10974 							      flinfo->info);
   10975 			  if (kept != NULL)
   10976 			    {
   10977 			      *ps = kept;
   10978 			      continue;
   10979 			    }
   10980 			}
   10981 		    }
   10982 		}
   10983 	    }
   10984 
   10985 	  /* Relocate the section by invoking a back end routine.
   10986 
   10987 	     The back end routine is responsible for adjusting the
   10988 	     section contents as necessary, and (if using Rela relocs
   10989 	     and generating a relocatable output file) adjusting the
   10990 	     reloc addend as necessary.
   10991 
   10992 	     The back end routine does not have to worry about setting
   10993 	     the reloc address or the reloc symbol index.
   10994 
   10995 	     The back end routine is given a pointer to the swapped in
   10996 	     internal symbols, and can access the hash table entries
   10997 	     for the external symbols via elf_sym_hashes (input_bfd).
   10998 
   10999 	     When generating relocatable output, the back end routine
   11000 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   11001 	     output symbol is going to be a section symbol
   11002 	     corresponding to the output section, which will require
   11003 	     the addend to be adjusted.  */
   11004 
   11005 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   11006 				     input_bfd, o, contents,
   11007 				     internal_relocs,
   11008 				     isymbuf,
   11009 				     flinfo->sections);
   11010 	  if (!ret)
   11011 	    return FALSE;
   11012 
   11013 	  if (ret == 2
   11014 	      || bfd_link_relocatable (flinfo->info)
   11015 	      || flinfo->info->emitrelocations)
   11016 	    {
   11017 	      Elf_Internal_Rela *irela;
   11018 	      Elf_Internal_Rela *irelaend, *irelamid;
   11019 	      bfd_vma last_offset;
   11020 	      struct elf_link_hash_entry **rel_hash;
   11021 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   11022 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   11023 	      unsigned int next_erel;
   11024 	      bfd_boolean rela_normal;
   11025 	      struct bfd_elf_section_data *esdi, *esdo;
   11026 
   11027 	      esdi = elf_section_data (o);
   11028 	      esdo = elf_section_data (o->output_section);
   11029 	      rela_normal = FALSE;
   11030 
   11031 	      /* Adjust the reloc addresses and symbol indices.  */
   11032 
   11033 	      irela = internal_relocs;
   11034 	      irelaend = irela + o->reloc_count;
   11035 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
   11036 	      /* We start processing the REL relocs, if any.  When we reach
   11037 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   11038 	      irelamid = irela;
   11039 	      if (esdi->rel.hdr != NULL)
   11040 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   11041 			     * bed->s->int_rels_per_ext_rel);
   11042 	      rel_hash_list = rel_hash;
   11043 	      rela_hash_list = NULL;
   11044 	      last_offset = o->output_offset;
   11045 	      if (!bfd_link_relocatable (flinfo->info))
   11046 		last_offset += o->output_section->vma;
   11047 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   11048 		{
   11049 		  unsigned long r_symndx;
   11050 		  asection *sec;
   11051 		  Elf_Internal_Sym sym;
   11052 
   11053 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   11054 		    {
   11055 		      rel_hash++;
   11056 		      next_erel = 0;
   11057 		    }
   11058 
   11059 		  if (irela == irelamid)
   11060 		    {
   11061 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
   11062 		      rela_hash_list = rel_hash;
   11063 		      rela_normal = bed->rela_normal;
   11064 		    }
   11065 
   11066 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   11067 							     flinfo->info, o,
   11068 							     irela->r_offset);
   11069 		  if (irela->r_offset >= (bfd_vma) -2)
   11070 		    {
   11071 		      /* This is a reloc for a deleted entry or somesuch.
   11072 			 Turn it into an R_*_NONE reloc, at the same
   11073 			 offset as the last reloc.  elf_eh_frame.c and
   11074 			 bfd_elf_discard_info rely on reloc offsets
   11075 			 being ordered.  */
   11076 		      irela->r_offset = last_offset;
   11077 		      irela->r_info = 0;
   11078 		      irela->r_addend = 0;
   11079 		      continue;
   11080 		    }
   11081 
   11082 		  irela->r_offset += o->output_offset;
   11083 
   11084 		  /* Relocs in an executable have to be virtual addresses.  */
   11085 		  if (!bfd_link_relocatable (flinfo->info))
   11086 		    irela->r_offset += o->output_section->vma;
   11087 
   11088 		  last_offset = irela->r_offset;
   11089 
   11090 		  r_symndx = irela->r_info >> r_sym_shift;
   11091 		  if (r_symndx == STN_UNDEF)
   11092 		    continue;
   11093 
   11094 		  if (r_symndx >= locsymcount
   11095 		      || (elf_bad_symtab (input_bfd)
   11096 			  && flinfo->sections[r_symndx] == NULL))
   11097 		    {
   11098 		      struct elf_link_hash_entry *rh;
   11099 		      unsigned long indx;
   11100 
   11101 		      /* This is a reloc against a global symbol.  We
   11102 			 have not yet output all the local symbols, so
   11103 			 we do not know the symbol index of any global
   11104 			 symbol.  We set the rel_hash entry for this
   11105 			 reloc to point to the global hash table entry
   11106 			 for this symbol.  The symbol index is then
   11107 			 set at the end of bfd_elf_final_link.  */
   11108 		      indx = r_symndx - extsymoff;
   11109 		      rh = elf_sym_hashes (input_bfd)[indx];
   11110 		      while (rh->root.type == bfd_link_hash_indirect
   11111 			     || rh->root.type == bfd_link_hash_warning)
   11112 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
   11113 
   11114 		      /* Setting the index to -2 tells
   11115 			 elf_link_output_extsym that this symbol is
   11116 			 used by a reloc.  */
   11117 		      BFD_ASSERT (rh->indx < 0);
   11118 		      rh->indx = -2;
   11119 		      *rel_hash = rh;
   11120 
   11121 		      continue;
   11122 		    }
   11123 
   11124 		  /* This is a reloc against a local symbol.  */
   11125 
   11126 		  *rel_hash = NULL;
   11127 		  sym = isymbuf[r_symndx];
   11128 		  sec = flinfo->sections[r_symndx];
   11129 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   11130 		    {
   11131 		      /* I suppose the backend ought to fill in the
   11132 			 section of any STT_SECTION symbol against a
   11133 			 processor specific section.  */
   11134 		      r_symndx = STN_UNDEF;
   11135 		      if (bfd_is_abs_section (sec))
   11136 			;
   11137 		      else if (sec == NULL || sec->owner == NULL)
   11138 			{
   11139 			  bfd_set_error (bfd_error_bad_value);
   11140 			  return FALSE;
   11141 			}
   11142 		      else
   11143 			{
   11144 			  asection *osec = sec->output_section;
   11145 
   11146 			  /* If we have discarded a section, the output
   11147 			     section will be the absolute section.  In
   11148 			     case of discarded SEC_MERGE sections, use
   11149 			     the kept section.  relocate_section should
   11150 			     have already handled discarded linkonce
   11151 			     sections.  */
   11152 			  if (bfd_is_abs_section (osec)
   11153 			      && sec->kept_section != NULL
   11154 			      && sec->kept_section->output_section != NULL)
   11155 			    {
   11156 			      osec = sec->kept_section->output_section;
   11157 			      irela->r_addend -= osec->vma;
   11158 			    }
   11159 
   11160 			  if (!bfd_is_abs_section (osec))
   11161 			    {
   11162 			      r_symndx = osec->target_index;
   11163 			      if (r_symndx == STN_UNDEF)
   11164 				{
   11165 				  irela->r_addend += osec->vma;
   11166 				  osec = _bfd_nearby_section (output_bfd, osec,
   11167 							      osec->vma);
   11168 				  irela->r_addend -= osec->vma;
   11169 				  r_symndx = osec->target_index;
   11170 				}
   11171 			    }
   11172 			}
   11173 
   11174 		      /* Adjust the addend according to where the
   11175 			 section winds up in the output section.  */
   11176 		      if (rela_normal)
   11177 			irela->r_addend += sec->output_offset;
   11178 		    }
   11179 		  else
   11180 		    {
   11181 		      if (flinfo->indices[r_symndx] == -1)
   11182 			{
   11183 			  unsigned long shlink;
   11184 			  const char *name;
   11185 			  asection *osec;
   11186 			  long indx;
   11187 
   11188 			  if (flinfo->info->strip == strip_all)
   11189 			    {
   11190 			      /* You can't do ld -r -s.  */
   11191 			      bfd_set_error (bfd_error_invalid_operation);
   11192 			      return FALSE;
   11193 			    }
   11194 
   11195 			  /* This symbol was skipped earlier, but
   11196 			     since it is needed by a reloc, we
   11197 			     must output it now.  */
   11198 			  shlink = symtab_hdr->sh_link;
   11199 			  name = (bfd_elf_string_from_elf_section
   11200 				  (input_bfd, shlink, sym.st_name));
   11201 			  if (name == NULL)
   11202 			    return FALSE;
   11203 
   11204 			  osec = sec->output_section;
   11205 			  sym.st_shndx =
   11206 			    _bfd_elf_section_from_bfd_section (output_bfd,
   11207 							       osec);
   11208 			  if (sym.st_shndx == SHN_BAD)
   11209 			    return FALSE;
   11210 
   11211 			  sym.st_value += sec->output_offset;
   11212 			  if (!bfd_link_relocatable (flinfo->info))
   11213 			    {
   11214 			      sym.st_value += osec->vma;
   11215 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   11216 				{
   11217 				  struct elf_link_hash_table *htab
   11218 				    = elf_hash_table (flinfo->info);
   11219 
   11220 				  /* STT_TLS symbols are relative to PT_TLS
   11221 				     segment base.  */
   11222 				  if (htab->tls_sec != NULL)
   11223 				    sym.st_value -= htab->tls_sec->vma;
   11224 				  else
   11225 				    sym.st_info
   11226 				      = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
   11227 						     STT_NOTYPE);
   11228 				}
   11229 			    }
   11230 
   11231 			  indx = bfd_get_symcount (output_bfd);
   11232 			  ret = elf_link_output_symstrtab (flinfo, name,
   11233 							   &sym, sec,
   11234 							   NULL);
   11235 			  if (ret == 0)
   11236 			    return FALSE;
   11237 			  else if (ret == 1)
   11238 			    flinfo->indices[r_symndx] = indx;
   11239 			  else
   11240 			    abort ();
   11241 			}
   11242 
   11243 		      r_symndx = flinfo->indices[r_symndx];
   11244 		    }
   11245 
   11246 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   11247 				   | (irela->r_info & r_type_mask));
   11248 		}
   11249 
   11250 	      /* Swap out the relocs.  */
   11251 	      input_rel_hdr = esdi->rel.hdr;
   11252 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   11253 		{
   11254 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   11255 						     input_rel_hdr,
   11256 						     internal_relocs,
   11257 						     rel_hash_list))
   11258 		    return FALSE;
   11259 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   11260 				      * bed->s->int_rels_per_ext_rel);
   11261 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   11262 		}
   11263 
   11264 	      input_rela_hdr = esdi->rela.hdr;
   11265 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   11266 		{
   11267 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   11268 						     input_rela_hdr,
   11269 						     internal_relocs,
   11270 						     rela_hash_list))
   11271 		    return FALSE;
   11272 		}
   11273 	    }
   11274 	}
   11275 
   11276       /* Write out the modified section contents.  */
   11277       if (bed->elf_backend_write_section
   11278 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   11279 						contents))
   11280 	{
   11281 	  /* Section written out.  */
   11282 	}
   11283       else switch (o->sec_info_type)
   11284 	{
   11285 	case SEC_INFO_TYPE_STABS:
   11286 	  if (! (_bfd_write_section_stabs
   11287 		 (output_bfd,
   11288 		  &elf_hash_table (flinfo->info)->stab_info,
   11289 		  o, &elf_section_data (o)->sec_info, contents)))
   11290 	    return FALSE;
   11291 	  break;
   11292 	case SEC_INFO_TYPE_MERGE:
   11293 	  if (! _bfd_write_merged_section (output_bfd, o,
   11294 					   elf_section_data (o)->sec_info))
   11295 	    return FALSE;
   11296 	  break;
   11297 	case SEC_INFO_TYPE_EH_FRAME:
   11298 	  {
   11299 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   11300 						   o, contents))
   11301 	      return FALSE;
   11302 	  }
   11303 	  break;
   11304 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   11305 	  {
   11306 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   11307 							 flinfo->info,
   11308 							 o, contents))
   11309 	      return FALSE;
   11310 	  }
   11311 	  break;
   11312 	default:
   11313 	  {
   11314 	    if (! (o->flags & SEC_EXCLUDE))
   11315 	      {
   11316 		file_ptr offset = (file_ptr) o->output_offset;
   11317 		bfd_size_type todo = o->size;
   11318 
   11319 		offset *= bfd_octets_per_byte (output_bfd, o);
   11320 
   11321 		if ((o->flags & SEC_ELF_REVERSE_COPY))
   11322 		  {
   11323 		    /* Reverse-copy input section to output.  */
   11324 		    do
   11325 		      {
   11326 			todo -= address_size;
   11327 			if (! bfd_set_section_contents (output_bfd,
   11328 							o->output_section,
   11329 							contents + todo,
   11330 							offset,
   11331 							address_size))
   11332 			  return FALSE;
   11333 			if (todo == 0)
   11334 			  break;
   11335 			offset += address_size;
   11336 		      }
   11337 		    while (1);
   11338 		  }
   11339 		else if (! bfd_set_section_contents (output_bfd,
   11340 						     o->output_section,
   11341 						     contents,
   11342 						     offset, todo))
   11343 		  return FALSE;
   11344 	      }
   11345 	  }
   11346 	  break;
   11347 	}
   11348     }
   11349 
   11350   return TRUE;
   11351 }
   11352 
   11353 /* Generate a reloc when linking an ELF file.  This is a reloc
   11354    requested by the linker, and does not come from any input file.  This
   11355    is used to build constructor and destructor tables when linking
   11356    with -Ur.  */
   11357 
   11358 static bfd_boolean
   11359 elf_reloc_link_order (bfd *output_bfd,
   11360 		      struct bfd_link_info *info,
   11361 		      asection *output_section,
   11362 		      struct bfd_link_order *link_order)
   11363 {
   11364   reloc_howto_type *howto;
   11365   long indx;
   11366   bfd_vma offset;
   11367   bfd_vma addend;
   11368   struct bfd_elf_section_reloc_data *reldata;
   11369   struct elf_link_hash_entry **rel_hash_ptr;
   11370   Elf_Internal_Shdr *rel_hdr;
   11371   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   11372   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   11373   bfd_byte *erel;
   11374   unsigned int i;
   11375   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   11376 
   11377   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   11378   if (howto == NULL)
   11379     {
   11380       bfd_set_error (bfd_error_bad_value);
   11381       return FALSE;
   11382     }
   11383 
   11384   addend = link_order->u.reloc.p->addend;
   11385 
   11386   if (esdo->rel.hdr)
   11387     reldata = &esdo->rel;
   11388   else if (esdo->rela.hdr)
   11389     reldata = &esdo->rela;
   11390   else
   11391     {
   11392       reldata = NULL;
   11393       BFD_ASSERT (0);
   11394     }
   11395 
   11396   /* Figure out the symbol index.  */
   11397   rel_hash_ptr = reldata->hashes + reldata->count;
   11398   if (link_order->type == bfd_section_reloc_link_order)
   11399     {
   11400       indx = link_order->u.reloc.p->u.section->target_index;
   11401       BFD_ASSERT (indx != 0);
   11402       *rel_hash_ptr = NULL;
   11403     }
   11404   else
   11405     {
   11406       struct elf_link_hash_entry *h;
   11407 
   11408       /* Treat a reloc against a defined symbol as though it were
   11409 	 actually against the section.  */
   11410       h = ((struct elf_link_hash_entry *)
   11411 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   11412 					 link_order->u.reloc.p->u.name,
   11413 					 FALSE, FALSE, TRUE));
   11414       if (h != NULL
   11415 	  && (h->root.type == bfd_link_hash_defined
   11416 	      || h->root.type == bfd_link_hash_defweak))
   11417 	{
   11418 	  asection *section;
   11419 
   11420 	  section = h->root.u.def.section;
   11421 	  indx = section->output_section->target_index;
   11422 	  *rel_hash_ptr = NULL;
   11423 	  /* It seems that we ought to add the symbol value to the
   11424 	     addend here, but in practice it has already been added
   11425 	     because it was passed to constructor_callback.  */
   11426 	  addend += section->output_section->vma + section->output_offset;
   11427 	}
   11428       else if (h != NULL)
   11429 	{
   11430 	  /* Setting the index to -2 tells elf_link_output_extsym that
   11431 	     this symbol is used by a reloc.  */
   11432 	  h->indx = -2;
   11433 	  *rel_hash_ptr = h;
   11434 	  indx = 0;
   11435 	}
   11436       else
   11437 	{
   11438 	  (*info->callbacks->unattached_reloc)
   11439 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   11440 	  indx = 0;
   11441 	}
   11442     }
   11443 
   11444   /* If this is an inplace reloc, we must write the addend into the
   11445      object file.  */
   11446   if (howto->partial_inplace && addend != 0)
   11447     {
   11448       bfd_size_type size;
   11449       bfd_reloc_status_type rstat;
   11450       bfd_byte *buf;
   11451       bfd_boolean ok;
   11452       const char *sym_name;
   11453       bfd_size_type octets;
   11454 
   11455       size = (bfd_size_type) bfd_get_reloc_size (howto);
   11456       buf = (bfd_byte *) bfd_zmalloc (size);
   11457       if (buf == NULL && size != 0)
   11458 	return FALSE;
   11459       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   11460       switch (rstat)
   11461 	{
   11462 	case bfd_reloc_ok:
   11463 	  break;
   11464 
   11465 	default:
   11466 	case bfd_reloc_outofrange:
   11467 	  abort ();
   11468 
   11469 	case bfd_reloc_overflow:
   11470 	  if (link_order->type == bfd_section_reloc_link_order)
   11471 	    sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
   11472 	  else
   11473 	    sym_name = link_order->u.reloc.p->u.name;
   11474 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
   11475 					      howto->name, addend, NULL, NULL,
   11476 					      (bfd_vma) 0);
   11477 	  break;
   11478 	}
   11479 
   11480       octets = link_order->offset * bfd_octets_per_byte (output_bfd,
   11481 							 output_section);
   11482       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   11483 				     octets, size);
   11484       free (buf);
   11485       if (! ok)
   11486 	return FALSE;
   11487     }
   11488 
   11489   /* The address of a reloc is relative to the section in a
   11490      relocatable file, and is a virtual address in an executable
   11491      file.  */
   11492   offset = link_order->offset;
   11493   if (! bfd_link_relocatable (info))
   11494     offset += output_section->vma;
   11495 
   11496   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   11497     {
   11498       irel[i].r_offset = offset;
   11499       irel[i].r_info = 0;
   11500       irel[i].r_addend = 0;
   11501     }
   11502   if (bed->s->arch_size == 32)
   11503     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   11504   else
   11505     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
   11506 
   11507   rel_hdr = reldata->hdr;
   11508   erel = rel_hdr->contents;
   11509   if (rel_hdr->sh_type == SHT_REL)
   11510     {
   11511       erel += reldata->count * bed->s->sizeof_rel;
   11512       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   11513     }
   11514   else
   11515     {
   11516       irel[0].r_addend = addend;
   11517       erel += reldata->count * bed->s->sizeof_rela;
   11518       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   11519     }
   11520 
   11521   ++reldata->count;
   11522 
   11523   return TRUE;
   11524 }
   11525 
   11526 
   11527 /* Compare two sections based on the locations of the sections they are
   11528    linked to.  Used by elf_fixup_link_order.  */
   11529 
   11530 static int
   11531 compare_link_order (const void *a, const void *b)
   11532 {
   11533   const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
   11534   const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
   11535   asection *asec = elf_linked_to_section (alo->u.indirect.section);
   11536   asection *bsec = elf_linked_to_section (blo->u.indirect.section);
   11537   bfd_vma apos = asec->output_section->lma + asec->output_offset;
   11538   bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
   11539 
   11540   if (apos < bpos)
   11541     return -1;
   11542   if (apos > bpos)
   11543     return 1;
   11544 
   11545   /* The only way we should get matching LMAs is when the first of two
   11546      sections has zero size.  */
   11547   if (asec->size < bsec->size)
   11548     return -1;
   11549   if (asec->size > bsec->size)
   11550     return 1;
   11551 
   11552   /* If they are both zero size then they almost certainly have the same
   11553      VMA and thus are not ordered with respect to each other.  Test VMA
   11554      anyway, and fall back to id to make the result reproducible across
   11555      qsort implementations.  */
   11556   apos = asec->output_section->vma + asec->output_offset;
   11557   bpos = bsec->output_section->vma + bsec->output_offset;
   11558   if (apos < bpos)
   11559     return -1;
   11560   if (apos > bpos)
   11561     return 1;
   11562 
   11563   return asec->id - bsec->id;
   11564 }
   11565 
   11566 
   11567 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
   11568    order as their linked sections.  Returns false if this could not be done
   11569    because an output section includes both ordered and unordered
   11570    sections.  Ideally we'd do this in the linker proper.  */
   11571 
   11572 static bfd_boolean
   11573 elf_fixup_link_order (bfd *abfd, asection *o)
   11574 {
   11575   size_t seen_linkorder;
   11576   size_t seen_other;
   11577   size_t n;
   11578   struct bfd_link_order *p;
   11579   bfd *sub;
   11580   struct bfd_link_order **sections;
   11581   asection *s, *other_sec, *linkorder_sec;
   11582   bfd_vma offset;
   11583 
   11584   other_sec = NULL;
   11585   linkorder_sec = NULL;
   11586   seen_other = 0;
   11587   seen_linkorder = 0;
   11588   for (p = o->map_head.link_order; p != NULL; p = p->next)
   11589     {
   11590       if (p->type == bfd_indirect_link_order)
   11591 	{
   11592 	  s = p->u.indirect.section;
   11593 	  sub = s->owner;
   11594 	  if ((s->flags & SEC_LINKER_CREATED) == 0
   11595 	      && bfd_get_flavour (sub) == bfd_target_elf_flavour
   11596 	      && elf_section_data (s) != NULL
   11597 	      && elf_linked_to_section (s) != NULL)
   11598 	    {
   11599 	      seen_linkorder++;
   11600 	      linkorder_sec = s;
   11601 	    }
   11602 	  else
   11603 	    {
   11604 	      seen_other++;
   11605 	      other_sec = s;
   11606 	    }
   11607 	}
   11608       else
   11609 	seen_other++;
   11610 
   11611       if (seen_other && seen_linkorder)
   11612 	{
   11613 	  if (other_sec && linkorder_sec)
   11614 	    _bfd_error_handler
   11615 	      /* xgettext:c-format */
   11616 	      (_("%pA has both ordered [`%pA' in %pB] "
   11617 		 "and unordered [`%pA' in %pB] sections"),
   11618 	       o, linkorder_sec, linkorder_sec->owner,
   11619 	       other_sec, other_sec->owner);
   11620 	  else
   11621 	    _bfd_error_handler
   11622 	      (_("%pA has both ordered and unordered sections"), o);
   11623 	  bfd_set_error (bfd_error_bad_value);
   11624 	  return FALSE;
   11625 	}
   11626     }
   11627 
   11628   if (!seen_linkorder)
   11629     return TRUE;
   11630 
   11631   sections = bfd_malloc (seen_linkorder * sizeof (*sections));
   11632   if (sections == NULL)
   11633     return FALSE;
   11634 
   11635   seen_linkorder = 0;
   11636   for (p = o->map_head.link_order; p != NULL; p = p->next)
   11637     sections[seen_linkorder++] = p;
   11638 
   11639   /* Sort the input sections in the order of their linked section.  */
   11640   qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
   11641 
   11642   /* Change the offsets of the sections.  */
   11643   offset = 0;
   11644   for (n = 0; n < seen_linkorder; n++)
   11645     {
   11646       bfd_vma mask;
   11647       s = sections[n]->u.indirect.section;
   11648       mask = ~(bfd_vma) 0 << s->alignment_power;
   11649       offset = (offset + ~mask) & mask;
   11650       s->output_offset = offset / bfd_octets_per_byte (abfd, s);
   11651       sections[n]->offset = offset;
   11652       offset += sections[n]->size;
   11653     }
   11654 
   11655   free (sections);
   11656   return TRUE;
   11657 }
   11658 
   11659 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
   11660    Returns TRUE upon success, FALSE otherwise.  */
   11661 
   11662 static bfd_boolean
   11663 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
   11664 {
   11665   bfd_boolean ret = FALSE;
   11666   bfd *implib_bfd;
   11667   const struct elf_backend_data *bed;
   11668   flagword flags;
   11669   enum bfd_architecture arch;
   11670   unsigned int mach;
   11671   asymbol **sympp = NULL;
   11672   long symsize;
   11673   long symcount;
   11674   long src_count;
   11675   elf_symbol_type *osymbuf;
   11676 
   11677   implib_bfd = info->out_implib_bfd;
   11678   bed = get_elf_backend_data (abfd);
   11679 
   11680   if (!bfd_set_format (implib_bfd, bfd_object))
   11681     return FALSE;
   11682 
   11683   /* Use flag from executable but make it a relocatable object.  */
   11684   flags = bfd_get_file_flags (abfd);
   11685   flags &= ~HAS_RELOC;
   11686   if (!bfd_set_start_address (implib_bfd, 0)
   11687       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
   11688     return FALSE;
   11689 
   11690   /* Copy architecture of output file to import library file.  */
   11691   arch = bfd_get_arch (abfd);
   11692   mach = bfd_get_mach (abfd);
   11693   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
   11694       && (abfd->target_defaulted
   11695 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
   11696     return FALSE;
   11697 
   11698   /* Get symbol table size.  */
   11699   symsize = bfd_get_symtab_upper_bound (abfd);
   11700   if (symsize < 0)
   11701     return FALSE;
   11702 
   11703   /* Read in the symbol table.  */
   11704   sympp = (asymbol **) bfd_malloc (symsize);
   11705   if (sympp == NULL)
   11706     return FALSE;
   11707 
   11708   symcount = bfd_canonicalize_symtab (abfd, sympp);
   11709   if (symcount < 0)
   11710     goto free_sym_buf;
   11711 
   11712   /* Allow the BFD backend to copy any private header data it
   11713      understands from the output BFD to the import library BFD.  */
   11714   if (! bfd_copy_private_header_data (abfd, implib_bfd))
   11715     goto free_sym_buf;
   11716 
   11717   /* Filter symbols to appear in the import library.  */
   11718   if (bed->elf_backend_filter_implib_symbols)
   11719     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
   11720 						       symcount);
   11721   else
   11722     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
   11723   if (symcount == 0)
   11724     {
   11725       bfd_set_error (bfd_error_no_symbols);
   11726       _bfd_error_handler (_("%pB: no symbol found for import library"),
   11727 			  implib_bfd);
   11728       goto free_sym_buf;
   11729     }
   11730 
   11731 
   11732   /* Make symbols absolute.  */
   11733   osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
   11734 					    sizeof (*osymbuf));
   11735   if (osymbuf == NULL)
   11736     goto free_sym_buf;
   11737 
   11738   for (src_count = 0; src_count < symcount; src_count++)
   11739     {
   11740       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
   11741 	      sizeof (*osymbuf));
   11742       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
   11743       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
   11744       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
   11745       osymbuf[src_count].internal_elf_sym.st_value =
   11746 	osymbuf[src_count].symbol.value;
   11747       sympp[src_count] = &osymbuf[src_count].symbol;
   11748     }
   11749 
   11750   bfd_set_symtab (implib_bfd, sympp, symcount);
   11751 
   11752   /* Allow the BFD backend to copy any private data it understands
   11753      from the output BFD to the import library BFD.  This is done last
   11754      to permit the routine to look at the filtered symbol table.  */
   11755   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
   11756     goto free_sym_buf;
   11757 
   11758   if (!bfd_close (implib_bfd))
   11759     goto free_sym_buf;
   11760 
   11761   ret = TRUE;
   11762 
   11763 free_sym_buf:
   11764   free (sympp);
   11765   return ret;
   11766 }
   11767 
   11768 static void
   11769 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   11770 {
   11771   asection *o;
   11772 
   11773   if (flinfo->symstrtab != NULL)
   11774     _bfd_elf_strtab_free (flinfo->symstrtab);
   11775   if (flinfo->contents != NULL)
   11776     free (flinfo->contents);
   11777   if (flinfo->external_relocs != NULL)
   11778     free (flinfo->external_relocs);
   11779   if (flinfo->internal_relocs != NULL)
   11780     free (flinfo->internal_relocs);
   11781   if (flinfo->external_syms != NULL)
   11782     free (flinfo->external_syms);
   11783   if (flinfo->locsym_shndx != NULL)
   11784     free (flinfo->locsym_shndx);
   11785   if (flinfo->internal_syms != NULL)
   11786     free (flinfo->internal_syms);
   11787   if (flinfo->indices != NULL)
   11788     free (flinfo->indices);
   11789   if (flinfo->sections != NULL)
   11790     free (flinfo->sections);
   11791   if (flinfo->symshndxbuf != NULL
   11792       && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
   11793     free (flinfo->symshndxbuf);
   11794   for (o = obfd->sections; o != NULL; o = o->next)
   11795     {
   11796       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11797       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
   11798 	free (esdo->rel.hashes);
   11799       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
   11800 	free (esdo->rela.hashes);
   11801     }
   11802 }
   11803 
   11804 /* Do the final step of an ELF link.  */
   11805 
   11806 bfd_boolean
   11807 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   11808 {
   11809   bfd_boolean dynamic;
   11810   bfd_boolean emit_relocs;
   11811   bfd *dynobj;
   11812   struct elf_final_link_info flinfo;
   11813   asection *o;
   11814   struct bfd_link_order *p;
   11815   bfd *sub;
   11816   bfd_size_type max_contents_size;
   11817   bfd_size_type max_external_reloc_size;
   11818   bfd_size_type max_internal_reloc_count;
   11819   bfd_size_type max_sym_count;
   11820   bfd_size_type max_sym_shndx_count;
   11821   Elf_Internal_Sym elfsym;
   11822   unsigned int i;
   11823   Elf_Internal_Shdr *symtab_hdr;
   11824   Elf_Internal_Shdr *symtab_shndx_hdr;
   11825   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11826   struct elf_outext_info eoinfo;
   11827   bfd_boolean merged;
   11828   size_t relativecount = 0;
   11829   asection *reldyn = 0;
   11830   bfd_size_type amt;
   11831   asection *attr_section = NULL;
   11832   bfd_vma attr_size = 0;
   11833   const char *std_attrs_section;
   11834   struct elf_link_hash_table *htab = elf_hash_table (info);
   11835   bfd_boolean sections_removed;
   11836 
   11837   if (!is_elf_hash_table (htab))
   11838     return FALSE;
   11839 
   11840   if (bfd_link_pic (info))
   11841     abfd->flags |= DYNAMIC;
   11842 
   11843   dynamic = htab->dynamic_sections_created;
   11844   dynobj = htab->dynobj;
   11845 
   11846   emit_relocs = (bfd_link_relocatable (info)
   11847 		 || info->emitrelocations);
   11848 
   11849   flinfo.info = info;
   11850   flinfo.output_bfd = abfd;
   11851   flinfo.symstrtab = _bfd_elf_strtab_init ();
   11852   if (flinfo.symstrtab == NULL)
   11853     return FALSE;
   11854 
   11855   if (! dynamic)
   11856     {
   11857       flinfo.hash_sec = NULL;
   11858       flinfo.symver_sec = NULL;
   11859     }
   11860   else
   11861     {
   11862       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   11863       /* Note that dynsym_sec can be NULL (on VMS).  */
   11864       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   11865       /* Note that it is OK if symver_sec is NULL.  */
   11866     }
   11867 
   11868   flinfo.contents = NULL;
   11869   flinfo.external_relocs = NULL;
   11870   flinfo.internal_relocs = NULL;
   11871   flinfo.external_syms = NULL;
   11872   flinfo.locsym_shndx = NULL;
   11873   flinfo.internal_syms = NULL;
   11874   flinfo.indices = NULL;
   11875   flinfo.sections = NULL;
   11876   flinfo.symshndxbuf = NULL;
   11877   flinfo.filesym_count = 0;
   11878 
   11879   /* The object attributes have been merged.  Remove the input
   11880      sections from the link, and set the contents of the output
   11881      section.  */
   11882   sections_removed = FALSE;
   11883   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   11884   for (o = abfd->sections; o != NULL; o = o->next)
   11885     {
   11886       bfd_boolean remove_section = FALSE;
   11887 
   11888       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
   11889 	  || strcmp (o->name, ".gnu.attributes") == 0)
   11890 	{
   11891 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   11892 	    {
   11893 	      asection *input_section;
   11894 
   11895 	      if (p->type != bfd_indirect_link_order)
   11896 		continue;
   11897 	      input_section = p->u.indirect.section;
   11898 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   11899 		 elf_link_input_bfd ignores this section.  */
   11900 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   11901 	    }
   11902 
   11903 	  attr_size = bfd_elf_obj_attr_size (abfd);
   11904 	  bfd_set_section_size (o, attr_size);
   11905 	  /* Skip this section later on.  */
   11906 	  o->map_head.link_order = NULL;
   11907 	  if (attr_size)
   11908 	    attr_section = o;
   11909 	  else
   11910 	    remove_section = TRUE;
   11911 	}
   11912       else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
   11913 	{
   11914 	  /* Remove empty group section from linker output.  */
   11915 	  remove_section = TRUE;
   11916 	}
   11917       if (remove_section)
   11918 	{
   11919 	  o->flags |= SEC_EXCLUDE;
   11920 	  bfd_section_list_remove (abfd, o);
   11921 	  abfd->section_count--;
   11922 	  sections_removed = TRUE;
   11923 	}
   11924     }
   11925   if (sections_removed)
   11926     _bfd_fix_excluded_sec_syms (abfd, info);
   11927 
   11928   /* Count up the number of relocations we will output for each output
   11929      section, so that we know the sizes of the reloc sections.  We
   11930      also figure out some maximum sizes.  */
   11931   max_contents_size = 0;
   11932   max_external_reloc_size = 0;
   11933   max_internal_reloc_count = 0;
   11934   max_sym_count = 0;
   11935   max_sym_shndx_count = 0;
   11936   merged = FALSE;
   11937   for (o = abfd->sections; o != NULL; o = o->next)
   11938     {
   11939       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11940       o->reloc_count = 0;
   11941 
   11942       for (p = o->map_head.link_order; p != NULL; p = p->next)
   11943 	{
   11944 	  unsigned int reloc_count = 0;
   11945 	  unsigned int additional_reloc_count = 0;
   11946 	  struct bfd_elf_section_data *esdi = NULL;
   11947 
   11948 	  if (p->type == bfd_section_reloc_link_order
   11949 	      || p->type == bfd_symbol_reloc_link_order)
   11950 	    reloc_count = 1;
   11951 	  else if (p->type == bfd_indirect_link_order)
   11952 	    {
   11953 	      asection *sec;
   11954 
   11955 	      sec = p->u.indirect.section;
   11956 
   11957 	      /* Mark all sections which are to be included in the
   11958 		 link.  This will normally be every section.  We need
   11959 		 to do this so that we can identify any sections which
   11960 		 the linker has decided to not include.  */
   11961 	      sec->linker_mark = TRUE;
   11962 
   11963 	      if (sec->flags & SEC_MERGE)
   11964 		merged = TRUE;
   11965 
   11966 	      if (sec->rawsize > max_contents_size)
   11967 		max_contents_size = sec->rawsize;
   11968 	      if (sec->size > max_contents_size)
   11969 		max_contents_size = sec->size;
   11970 
   11971 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   11972 		  && (sec->owner->flags & DYNAMIC) == 0)
   11973 		{
   11974 		  size_t sym_count;
   11975 
   11976 		  /* We are interested in just local symbols, not all
   11977 		     symbols.  */
   11978 		  if (elf_bad_symtab (sec->owner))
   11979 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   11980 				 / bed->s->sizeof_sym);
   11981 		  else
   11982 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   11983 
   11984 		  if (sym_count > max_sym_count)
   11985 		    max_sym_count = sym_count;
   11986 
   11987 		  if (sym_count > max_sym_shndx_count
   11988 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   11989 		    max_sym_shndx_count = sym_count;
   11990 
   11991 		  if (esdo->this_hdr.sh_type == SHT_REL
   11992 		      || esdo->this_hdr.sh_type == SHT_RELA)
   11993 		    /* Some backends use reloc_count in relocation sections
   11994 		       to count particular types of relocs.  Of course,
   11995 		       reloc sections themselves can't have relocations.  */
   11996 		    ;
   11997 		  else if (emit_relocs)
   11998 		    {
   11999 		      reloc_count = sec->reloc_count;
   12000 		      if (bed->elf_backend_count_additional_relocs)
   12001 			{
   12002 			  int c;
   12003 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
   12004 			  additional_reloc_count += c;
   12005 			}
   12006 		    }
   12007 		  else if (bed->elf_backend_count_relocs)
   12008 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   12009 
   12010 		  esdi = elf_section_data (sec);
   12011 
   12012 		  if ((sec->flags & SEC_RELOC) != 0)
   12013 		    {
   12014 		      size_t ext_size = 0;
   12015 
   12016 		      if (esdi->rel.hdr != NULL)
   12017 			ext_size = esdi->rel.hdr->sh_size;
   12018 		      if (esdi->rela.hdr != NULL)
   12019 			ext_size += esdi->rela.hdr->sh_size;
   12020 
   12021 		      if (ext_size > max_external_reloc_size)
   12022 			max_external_reloc_size = ext_size;
   12023 		      if (sec->reloc_count > max_internal_reloc_count)
   12024 			max_internal_reloc_count = sec->reloc_count;
   12025 		    }
   12026 		}
   12027 	    }
   12028 
   12029 	  if (reloc_count == 0)
   12030 	    continue;
   12031 
   12032 	  reloc_count += additional_reloc_count;
   12033 	  o->reloc_count += reloc_count;
   12034 
   12035 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   12036 	    {
   12037 	      if (esdi->rel.hdr)
   12038 		{
   12039 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   12040 		  esdo->rel.count += additional_reloc_count;
   12041 		}
   12042 	      if (esdi->rela.hdr)
   12043 		{
   12044 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   12045 		  esdo->rela.count += additional_reloc_count;
   12046 		}
   12047 	    }
   12048 	  else
   12049 	    {
   12050 	      if (o->use_rela_p)
   12051 		esdo->rela.count += reloc_count;
   12052 	      else
   12053 		esdo->rel.count += reloc_count;
   12054 	    }
   12055 	}
   12056 
   12057       if (o->reloc_count > 0)
   12058 	o->flags |= SEC_RELOC;
   12059       else
   12060 	{
   12061 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   12062 	     set it (this is probably a bug) and if it is set
   12063 	     assign_section_numbers will create a reloc section.  */
   12064 	  o->flags &=~ SEC_RELOC;
   12065 	}
   12066 
   12067       /* If the SEC_ALLOC flag is not set, force the section VMA to
   12068 	 zero.  This is done in elf_fake_sections as well, but forcing
   12069 	 the VMA to 0 here will ensure that relocs against these
   12070 	 sections are handled correctly.  */
   12071       if ((o->flags & SEC_ALLOC) == 0
   12072 	  && ! o->user_set_vma)
   12073 	o->vma = 0;
   12074     }
   12075 
   12076   if (! bfd_link_relocatable (info) && merged)
   12077     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
   12078 
   12079   /* Figure out the file positions for everything but the symbol table
   12080      and the relocs.  We set symcount to force assign_section_numbers
   12081      to create a symbol table.  */
   12082   abfd->symcount = info->strip != strip_all || emit_relocs;
   12083   BFD_ASSERT (! abfd->output_has_begun);
   12084   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   12085     goto error_return;
   12086 
   12087   /* Set sizes, and assign file positions for reloc sections.  */
   12088   for (o = abfd->sections; o != NULL; o = o->next)
   12089     {
   12090       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12091       if ((o->flags & SEC_RELOC) != 0)
   12092 	{
   12093 	  if (esdo->rel.hdr
   12094 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   12095 	    goto error_return;
   12096 
   12097 	  if (esdo->rela.hdr
   12098 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   12099 	    goto error_return;
   12100 	}
   12101 
   12102       /* _bfd_elf_compute_section_file_positions makes temporary use
   12103 	 of target_index.  Reset it.  */
   12104       o->target_index = 0;
   12105 
   12106       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   12107 	 to count upwards while actually outputting the relocations.  */
   12108       esdo->rel.count = 0;
   12109       esdo->rela.count = 0;
   12110 
   12111       if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
   12112 	  && !bfd_section_is_ctf (o))
   12113 	{
   12114 	  /* Cache the section contents so that they can be compressed
   12115 	     later.  Use bfd_malloc since it will be freed by
   12116 	     bfd_compress_section_contents.  */
   12117 	  unsigned char *contents = esdo->this_hdr.contents;
   12118 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
   12119 	    abort ();
   12120 	  contents
   12121 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   12122 	  if (contents == NULL)
   12123 	    goto error_return;
   12124 	  esdo->this_hdr.contents = contents;
   12125 	}
   12126     }
   12127 
   12128   /* We have now assigned file positions for all the sections except .symtab,
   12129      .strtab, and non-loaded reloc and compressed debugging sections.  We start
   12130      the .symtab section at the current file position, and write directly to it.
   12131      We build the .strtab section in memory.  */
   12132   abfd->symcount = 0;
   12133   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12134   /* sh_name is set in prep_headers.  */
   12135   symtab_hdr->sh_type = SHT_SYMTAB;
   12136   /* sh_flags, sh_addr and sh_size all start off zero.  */
   12137   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   12138   /* sh_link is set in assign_section_numbers.  */
   12139   /* sh_info is set below.  */
   12140   /* sh_offset is set just below.  */
   12141   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   12142 
   12143   if (max_sym_count < 20)
   12144     max_sym_count = 20;
   12145   htab->strtabsize = max_sym_count;
   12146   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   12147   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
   12148   if (htab->strtab == NULL)
   12149     goto error_return;
   12150   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   12151   flinfo.symshndxbuf
   12152     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   12153        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   12154 
   12155   if (info->strip != strip_all || emit_relocs)
   12156     {
   12157       file_ptr off = elf_next_file_pos (abfd);
   12158 
   12159       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
   12160 
   12161       /* Note that at this point elf_next_file_pos (abfd) is
   12162 	 incorrect.  We do not yet know the size of the .symtab section.
   12163 	 We correct next_file_pos below, after we do know the size.  */
   12164 
   12165       /* Start writing out the symbol table.  The first symbol is always a
   12166 	 dummy symbol.  */
   12167       elfsym.st_value = 0;
   12168       elfsym.st_size = 0;
   12169       elfsym.st_info = 0;
   12170       elfsym.st_other = 0;
   12171       elfsym.st_shndx = SHN_UNDEF;
   12172       elfsym.st_target_internal = 0;
   12173       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   12174 				     bfd_und_section_ptr, NULL) != 1)
   12175 	goto error_return;
   12176 
   12177       /* Output a symbol for each section.  We output these even if we are
   12178 	 discarding local symbols, since they are used for relocs.  These
   12179 	 symbols have no names.  We store the index of each one in the
   12180 	 index field of the section, so that we can find it again when
   12181 	 outputting relocs.  */
   12182 
   12183       elfsym.st_size = 0;
   12184       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   12185       elfsym.st_other = 0;
   12186       elfsym.st_value = 0;
   12187       elfsym.st_target_internal = 0;
   12188       for (i = 1; i < elf_numsections (abfd); i++)
   12189 	{
   12190 	  o = bfd_section_from_elf_index (abfd, i);
   12191 	  if (o != NULL)
   12192 	    {
   12193 	      o->target_index = bfd_get_symcount (abfd);
   12194 	      elfsym.st_shndx = i;
   12195 	      if (!bfd_link_relocatable (info))
   12196 		elfsym.st_value = o->vma;
   12197 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
   12198 					     NULL) != 1)
   12199 		goto error_return;
   12200 	    }
   12201 	}
   12202     }
   12203 
   12204   /* Allocate some memory to hold information read in from the input
   12205      files.  */
   12206   if (max_contents_size != 0)
   12207     {
   12208       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   12209       if (flinfo.contents == NULL)
   12210 	goto error_return;
   12211     }
   12212 
   12213   if (max_external_reloc_size != 0)
   12214     {
   12215       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   12216       if (flinfo.external_relocs == NULL)
   12217 	goto error_return;
   12218     }
   12219 
   12220   if (max_internal_reloc_count != 0)
   12221     {
   12222       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
   12223       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   12224       if (flinfo.internal_relocs == NULL)
   12225 	goto error_return;
   12226     }
   12227 
   12228   if (max_sym_count != 0)
   12229     {
   12230       amt = max_sym_count * bed->s->sizeof_sym;
   12231       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   12232       if (flinfo.external_syms == NULL)
   12233 	goto error_return;
   12234 
   12235       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   12236       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   12237       if (flinfo.internal_syms == NULL)
   12238 	goto error_return;
   12239 
   12240       amt = max_sym_count * sizeof (long);
   12241       flinfo.indices = (long int *) bfd_malloc (amt);
   12242       if (flinfo.indices == NULL)
   12243 	goto error_return;
   12244 
   12245       amt = max_sym_count * sizeof (asection *);
   12246       flinfo.sections = (asection **) bfd_malloc (amt);
   12247       if (flinfo.sections == NULL)
   12248 	goto error_return;
   12249     }
   12250 
   12251   if (max_sym_shndx_count != 0)
   12252     {
   12253       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   12254       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   12255       if (flinfo.locsym_shndx == NULL)
   12256 	goto error_return;
   12257     }
   12258 
   12259   if (htab->tls_sec)
   12260     {
   12261       bfd_vma base, end = 0;
   12262       asection *sec;
   12263 
   12264       for (sec = htab->tls_sec;
   12265 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   12266 	   sec = sec->next)
   12267 	{
   12268 	  bfd_size_type size = sec->size;
   12269 
   12270 	  if (size == 0
   12271 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   12272 	    {
   12273 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   12274 
   12275 	      if (ord != NULL)
   12276 		size = ord->offset + ord->size;
   12277 	    }
   12278 	  end = sec->vma + size;
   12279 	}
   12280       base = htab->tls_sec->vma;
   12281       /* Only align end of TLS section if static TLS doesn't have special
   12282 	 alignment requirements.  */
   12283       if (bed->static_tls_alignment == 1)
   12284 	end = align_power (end, htab->tls_sec->alignment_power);
   12285       htab->tls_size = end - base;
   12286     }
   12287 
   12288   /* Reorder SHF_LINK_ORDER sections.  */
   12289   for (o = abfd->sections; o != NULL; o = o->next)
   12290     {
   12291       if (!elf_fixup_link_order (abfd, o))
   12292 	return FALSE;
   12293     }
   12294 
   12295   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   12296     return FALSE;
   12297 
   12298   /* Since ELF permits relocations to be against local symbols, we
   12299      must have the local symbols available when we do the relocations.
   12300      Since we would rather only read the local symbols once, and we
   12301      would rather not keep them in memory, we handle all the
   12302      relocations for a single input file at the same time.
   12303 
   12304      Unfortunately, there is no way to know the total number of local
   12305      symbols until we have seen all of them, and the local symbol
   12306      indices precede the global symbol indices.  This means that when
   12307      we are generating relocatable output, and we see a reloc against
   12308      a global symbol, we can not know the symbol index until we have
   12309      finished examining all the local symbols to see which ones we are
   12310      going to output.  To deal with this, we keep the relocations in
   12311      memory, and don't output them until the end of the link.  This is
   12312      an unfortunate waste of memory, but I don't see a good way around
   12313      it.  Fortunately, it only happens when performing a relocatable
   12314      link, which is not the common case.  FIXME: If keep_memory is set
   12315      we could write the relocs out and then read them again; I don't
   12316      know how bad the memory loss will be.  */
   12317 
   12318   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12319     sub->output_has_begun = FALSE;
   12320   for (o = abfd->sections; o != NULL; o = o->next)
   12321     {
   12322       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12323 	{
   12324 	  if (p->type == bfd_indirect_link_order
   12325 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   12326 		  == bfd_target_elf_flavour)
   12327 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   12328 	    {
   12329 	      if (! sub->output_has_begun)
   12330 		{
   12331 		  if (! elf_link_input_bfd (&flinfo, sub))
   12332 		    goto error_return;
   12333 		  sub->output_has_begun = TRUE;
   12334 		}
   12335 	    }
   12336 	  else if (p->type == bfd_section_reloc_link_order
   12337 		   || p->type == bfd_symbol_reloc_link_order)
   12338 	    {
   12339 	      if (! elf_reloc_link_order (abfd, info, o, p))
   12340 		goto error_return;
   12341 	    }
   12342 	  else
   12343 	    {
   12344 	      if (! _bfd_default_link_order (abfd, info, o, p))
   12345 		{
   12346 		  if (p->type == bfd_indirect_link_order
   12347 		      && (bfd_get_flavour (sub)
   12348 			  == bfd_target_elf_flavour)
   12349 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   12350 			  != bed->s->elfclass))
   12351 		    {
   12352 		      const char *iclass, *oclass;
   12353 
   12354 		      switch (bed->s->elfclass)
   12355 			{
   12356 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
   12357 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
   12358 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
   12359 			default: abort ();
   12360 			}
   12361 
   12362 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
   12363 			{
   12364 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
   12365 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
   12366 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
   12367 			default: abort ();
   12368 			}
   12369 
   12370 		      bfd_set_error (bfd_error_wrong_format);
   12371 		      _bfd_error_handler
   12372 			/* xgettext:c-format */
   12373 			(_("%pB: file class %s incompatible with %s"),
   12374 			 sub, iclass, oclass);
   12375 		    }
   12376 
   12377 		  goto error_return;
   12378 		}
   12379 	    }
   12380 	}
   12381     }
   12382 
   12383   /* Free symbol buffer if needed.  */
   12384   if (!info->reduce_memory_overheads)
   12385     {
   12386       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12387 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   12388 	    && elf_tdata (sub)->symbuf)
   12389 	  {
   12390 	    free (elf_tdata (sub)->symbuf);
   12391 	    elf_tdata (sub)->symbuf = NULL;
   12392 	  }
   12393     }
   12394 
   12395   /* Output any global symbols that got converted to local in a
   12396      version script or due to symbol visibility.  We do this in a
   12397      separate step since ELF requires all local symbols to appear
   12398      prior to any global symbols.  FIXME: We should only do this if
   12399      some global symbols were, in fact, converted to become local.
   12400      FIXME: Will this work correctly with the Irix 5 linker?  */
   12401   eoinfo.failed = FALSE;
   12402   eoinfo.flinfo = &flinfo;
   12403   eoinfo.localsyms = TRUE;
   12404   eoinfo.file_sym_done = FALSE;
   12405   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   12406   if (eoinfo.failed)
   12407     return FALSE;
   12408 
   12409   /* If backend needs to output some local symbols not present in the hash
   12410      table, do it now.  */
   12411   if (bed->elf_backend_output_arch_local_syms
   12412       && (info->strip != strip_all || emit_relocs))
   12413     {
   12414       typedef int (*out_sym_func)
   12415 	(void *, const char *, Elf_Internal_Sym *, asection *,
   12416 	 struct elf_link_hash_entry *);
   12417 
   12418       if (! ((*bed->elf_backend_output_arch_local_syms)
   12419 	     (abfd, info, &flinfo,
   12420 	      (out_sym_func) elf_link_output_symstrtab)))
   12421 	return FALSE;
   12422     }
   12423 
   12424   /* That wrote out all the local symbols.  Finish up the symbol table
   12425      with the global symbols. Even if we want to strip everything we
   12426      can, we still need to deal with those global symbols that got
   12427      converted to local in a version script.  */
   12428 
   12429   /* The sh_info field records the index of the first non local symbol.  */
   12430   symtab_hdr->sh_info = bfd_get_symcount (abfd);
   12431 
   12432   if (dynamic
   12433       && htab->dynsym != NULL
   12434       && htab->dynsym->output_section != bfd_abs_section_ptr)
   12435     {
   12436       Elf_Internal_Sym sym;
   12437       bfd_byte *dynsym = htab->dynsym->contents;
   12438 
   12439       o = htab->dynsym->output_section;
   12440       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
   12441 
   12442       /* Write out the section symbols for the output sections.  */
   12443       if (bfd_link_pic (info)
   12444 	  || htab->is_relocatable_executable)
   12445 	{
   12446 	  asection *s;
   12447 
   12448 	  sym.st_size = 0;
   12449 	  sym.st_name = 0;
   12450 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   12451 	  sym.st_other = 0;
   12452 	  sym.st_target_internal = 0;
   12453 
   12454 	  for (s = abfd->sections; s != NULL; s = s->next)
   12455 	    {
   12456 	      int indx;
   12457 	      bfd_byte *dest;
   12458 	      long dynindx;
   12459 
   12460 	      dynindx = elf_section_data (s)->dynindx;
   12461 	      if (dynindx <= 0)
   12462 		continue;
   12463 	      indx = elf_section_data (s)->this_idx;
   12464 	      BFD_ASSERT (indx > 0);
   12465 	      sym.st_shndx = indx;
   12466 	      if (! check_dynsym (abfd, &sym))
   12467 		return FALSE;
   12468 	      sym.st_value = s->vma;
   12469 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   12470 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   12471 	    }
   12472 	}
   12473 
   12474       /* Write out the local dynsyms.  */
   12475       if (htab->dynlocal)
   12476 	{
   12477 	  struct elf_link_local_dynamic_entry *e;
   12478 	  for (e = htab->dynlocal; e ; e = e->next)
   12479 	    {
   12480 	      asection *s;
   12481 	      bfd_byte *dest;
   12482 
   12483 	      /* Copy the internal symbol and turn off visibility.
   12484 		 Note that we saved a word of storage and overwrote
   12485 		 the original st_name with the dynstr_index.  */
   12486 	      sym = e->isym;
   12487 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   12488 
   12489 	      s = bfd_section_from_elf_index (e->input_bfd,
   12490 					      e->isym.st_shndx);
   12491 	      if (s != NULL)
   12492 		{
   12493 		  sym.st_shndx =
   12494 		    elf_section_data (s->output_section)->this_idx;
   12495 		  if (! check_dynsym (abfd, &sym))
   12496 		    return FALSE;
   12497 		  sym.st_value = (s->output_section->vma
   12498 				  + s->output_offset
   12499 				  + e->isym.st_value);
   12500 		}
   12501 
   12502 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   12503 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   12504 	    }
   12505 	}
   12506     }
   12507 
   12508   /* We get the global symbols from the hash table.  */
   12509   eoinfo.failed = FALSE;
   12510   eoinfo.localsyms = FALSE;
   12511   eoinfo.flinfo = &flinfo;
   12512   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   12513   if (eoinfo.failed)
   12514     return FALSE;
   12515 
   12516   /* If backend needs to output some symbols not present in the hash
   12517      table, do it now.  */
   12518   if (bed->elf_backend_output_arch_syms
   12519       && (info->strip != strip_all || emit_relocs))
   12520     {
   12521       typedef int (*out_sym_func)
   12522 	(void *, const char *, Elf_Internal_Sym *, asection *,
   12523 	 struct elf_link_hash_entry *);
   12524 
   12525       if (! ((*bed->elf_backend_output_arch_syms)
   12526 	     (abfd, info, &flinfo,
   12527 	      (out_sym_func) elf_link_output_symstrtab)))
   12528 	return FALSE;
   12529     }
   12530 
   12531   /* Finalize the .strtab section.  */
   12532   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   12533 
   12534   /* Swap out the .strtab section. */
   12535   if (!elf_link_swap_symbols_out (&flinfo))
   12536     return FALSE;
   12537 
   12538   /* Now we know the size of the symtab section.  */
   12539   if (bfd_get_symcount (abfd) > 0)
   12540     {
   12541       /* Finish up and write out the symbol string table (.strtab)
   12542 	 section.  */
   12543       Elf_Internal_Shdr *symstrtab_hdr = NULL;
   12544       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   12545 
   12546       if (elf_symtab_shndx_list (abfd))
   12547 	{
   12548 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   12549 
   12550 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   12551 	    {
   12552 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   12553 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   12554 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   12555 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   12556 	      symtab_shndx_hdr->sh_size = amt;
   12557 
   12558 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   12559 							       off, TRUE);
   12560 
   12561 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   12562 		  || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
   12563 		return FALSE;
   12564 	    }
   12565 	}
   12566 
   12567       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   12568       /* sh_name was set in prep_headers.  */
   12569       symstrtab_hdr->sh_type = SHT_STRTAB;
   12570       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   12571       symstrtab_hdr->sh_addr = 0;
   12572       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   12573       symstrtab_hdr->sh_entsize = 0;
   12574       symstrtab_hdr->sh_link = 0;
   12575       symstrtab_hdr->sh_info = 0;
   12576       /* sh_offset is set just below.  */
   12577       symstrtab_hdr->sh_addralign = 1;
   12578 
   12579       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   12580 						       off, TRUE);
   12581       elf_next_file_pos (abfd) = off;
   12582 
   12583       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   12584 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   12585 	return FALSE;
   12586     }
   12587 
   12588   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
   12589     {
   12590       _bfd_error_handler (_("%pB: failed to generate import library"),
   12591 			  info->out_implib_bfd);
   12592       return FALSE;
   12593     }
   12594 
   12595   /* Adjust the relocs to have the correct symbol indices.  */
   12596   for (o = abfd->sections; o != NULL; o = o->next)
   12597     {
   12598       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12599       bfd_boolean sort;
   12600 
   12601       if ((o->flags & SEC_RELOC) == 0)
   12602 	continue;
   12603 
   12604       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   12605       if (esdo->rel.hdr != NULL
   12606 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
   12607 	return FALSE;
   12608       if (esdo->rela.hdr != NULL
   12609 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
   12610 	return FALSE;
   12611 
   12612       /* Set the reloc_count field to 0 to prevent write_relocs from
   12613 	 trying to swap the relocs out itself.  */
   12614       o->reloc_count = 0;
   12615     }
   12616 
   12617   if (dynamic && info->combreloc && dynobj != NULL)
   12618     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   12619 
   12620   /* If we are linking against a dynamic object, or generating a
   12621      shared library, finish up the dynamic linking information.  */
   12622   if (dynamic)
   12623     {
   12624       bfd_byte *dyncon, *dynconend;
   12625 
   12626       /* Fix up .dynamic entries.  */
   12627       o = bfd_get_linker_section (dynobj, ".dynamic");
   12628       BFD_ASSERT (o != NULL);
   12629 
   12630       dyncon = o->contents;
   12631       dynconend = o->contents + o->size;
   12632       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   12633 	{
   12634 	  Elf_Internal_Dyn dyn;
   12635 	  const char *name;
   12636 	  unsigned int type;
   12637 	  bfd_size_type sh_size;
   12638 	  bfd_vma sh_addr;
   12639 
   12640 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   12641 
   12642 	  switch (dyn.d_tag)
   12643 	    {
   12644 	    default:
   12645 	      continue;
   12646 	    case DT_NULL:
   12647 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
   12648 		{
   12649 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   12650 		    {
   12651 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   12652 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   12653 		    default: continue;
   12654 		    }
   12655 		  dyn.d_un.d_val = relativecount;
   12656 		  relativecount = 0;
   12657 		  break;
   12658 		}
   12659 	      continue;
   12660 
   12661 	    case DT_INIT:
   12662 	      name = info->init_function;
   12663 	      goto get_sym;
   12664 	    case DT_FINI:
   12665 	      name = info->fini_function;
   12666 	    get_sym:
   12667 	      {
   12668 		struct elf_link_hash_entry *h;
   12669 
   12670 		h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   12671 		if (h != NULL
   12672 		    && (h->root.type == bfd_link_hash_defined
   12673 			|| h->root.type == bfd_link_hash_defweak))
   12674 		  {
   12675 		    dyn.d_un.d_ptr = h->root.u.def.value;
   12676 		    o = h->root.u.def.section;
   12677 		    if (o->output_section != NULL)
   12678 		      dyn.d_un.d_ptr += (o->output_section->vma
   12679 					 + o->output_offset);
   12680 		    else
   12681 		      {
   12682 			/* The symbol is imported from another shared
   12683 			   library and does not apply to this one.  */
   12684 			dyn.d_un.d_ptr = 0;
   12685 		      }
   12686 		    break;
   12687 		  }
   12688 	      }
   12689 	      continue;
   12690 
   12691 	    case DT_PREINIT_ARRAYSZ:
   12692 	      name = ".preinit_array";
   12693 	      goto get_out_size;
   12694 	    case DT_INIT_ARRAYSZ:
   12695 	      name = ".init_array";
   12696 	      goto get_out_size;
   12697 	    case DT_FINI_ARRAYSZ:
   12698 	      name = ".fini_array";
   12699 	    get_out_size:
   12700 	      o = bfd_get_section_by_name (abfd, name);
   12701 	      if (o == NULL)
   12702 		{
   12703 		  _bfd_error_handler
   12704 		    (_("could not find section %s"), name);
   12705 		  goto error_return;
   12706 		}
   12707 	      if (o->size == 0)
   12708 		_bfd_error_handler
   12709 		  (_("warning: %s section has zero size"), name);
   12710 	      dyn.d_un.d_val = o->size;
   12711 	      break;
   12712 
   12713 	    case DT_PREINIT_ARRAY:
   12714 	      name = ".preinit_array";
   12715 	      goto get_out_vma;
   12716 	    case DT_INIT_ARRAY:
   12717 	      name = ".init_array";
   12718 	      goto get_out_vma;
   12719 	    case DT_FINI_ARRAY:
   12720 	      name = ".fini_array";
   12721 	    get_out_vma:
   12722 	      o = bfd_get_section_by_name (abfd, name);
   12723 	      goto do_vma;
   12724 
   12725 	    case DT_HASH:
   12726 	      name = ".hash";
   12727 	      goto get_vma;
   12728 	    case DT_GNU_HASH:
   12729 	      name = ".gnu.hash";
   12730 	      goto get_vma;
   12731 	    case DT_STRTAB:
   12732 	      name = ".dynstr";
   12733 	      goto get_vma;
   12734 	    case DT_SYMTAB:
   12735 	      name = ".dynsym";
   12736 	      goto get_vma;
   12737 	    case DT_VERDEF:
   12738 	      name = ".gnu.version_d";
   12739 	      goto get_vma;
   12740 	    case DT_VERNEED:
   12741 	      name = ".gnu.version_r";
   12742 	      goto get_vma;
   12743 	    case DT_VERSYM:
   12744 	      name = ".gnu.version";
   12745 	    get_vma:
   12746 	      o = bfd_get_linker_section (dynobj, name);
   12747 	    do_vma:
   12748 	      if (o == NULL || bfd_is_abs_section (o->output_section))
   12749 		{
   12750 		  _bfd_error_handler
   12751 		    (_("could not find section %s"), name);
   12752 		  goto error_return;
   12753 		}
   12754 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   12755 		{
   12756 		  _bfd_error_handler
   12757 		    (_("warning: section '%s' is being made into a note"), name);
   12758 		  bfd_set_error (bfd_error_nonrepresentable_section);
   12759 		  goto error_return;
   12760 		}
   12761 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
   12762 	      break;
   12763 
   12764 	    case DT_REL:
   12765 	    case DT_RELA:
   12766 	    case DT_RELSZ:
   12767 	    case DT_RELASZ:
   12768 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   12769 		type = SHT_REL;
   12770 	      else
   12771 		type = SHT_RELA;
   12772 	      sh_size = 0;
   12773 	      sh_addr = 0;
   12774 	      for (i = 1; i < elf_numsections (abfd); i++)
   12775 		{
   12776 		  Elf_Internal_Shdr *hdr;
   12777 
   12778 		  hdr = elf_elfsections (abfd)[i];
   12779 		  if (hdr->sh_type == type
   12780 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   12781 		    {
   12782 		      sh_size += hdr->sh_size;
   12783 		      if (sh_addr == 0
   12784 			  || sh_addr > hdr->sh_addr)
   12785 			sh_addr = hdr->sh_addr;
   12786 		    }
   12787 		}
   12788 
   12789 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
   12790 		{
   12791 		  /* Don't count procedure linkage table relocs in the
   12792 		     overall reloc count.  */
   12793 		  sh_size -= htab->srelplt->size;
   12794 		  if (sh_size == 0)
   12795 		    /* If the size is zero, make the address zero too.
   12796 		       This is to avoid a glibc bug.  If the backend
   12797 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
   12798 		       zero, then we'll put DT_RELA at the end of
   12799 		       DT_JMPREL.  glibc will interpret the end of
   12800 		       DT_RELA matching the end of DT_JMPREL as the
   12801 		       case where DT_RELA includes DT_JMPREL, and for
   12802 		       LD_BIND_NOW will decide that processing DT_RELA
   12803 		       will process the PLT relocs too.  Net result:
   12804 		       No PLT relocs applied.  */
   12805 		    sh_addr = 0;
   12806 
   12807 		  /* If .rela.plt is the first .rela section, exclude
   12808 		     it from DT_RELA.  */
   12809 		  else if (sh_addr == (htab->srelplt->output_section->vma
   12810 				       + htab->srelplt->output_offset))
   12811 		    sh_addr += htab->srelplt->size;
   12812 		}
   12813 
   12814 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   12815 		dyn.d_un.d_val = sh_size;
   12816 	      else
   12817 		dyn.d_un.d_ptr = sh_addr;
   12818 	      break;
   12819 	    }
   12820 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   12821 	}
   12822     }
   12823 
   12824   /* If we have created any dynamic sections, then output them.  */
   12825   if (dynobj != NULL)
   12826     {
   12827       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   12828 	goto error_return;
   12829 
   12830       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   12831       if (((info->warn_shared_textrel && bfd_link_pic (info))
   12832 	   || info->error_textrel)
   12833 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
   12834 	{
   12835 	  bfd_byte *dyncon, *dynconend;
   12836 
   12837 	  dyncon = o->contents;
   12838 	  dynconend = o->contents + o->size;
   12839 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   12840 	    {
   12841 	      Elf_Internal_Dyn dyn;
   12842 
   12843 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   12844 
   12845 	      if (dyn.d_tag == DT_TEXTREL)
   12846 		{
   12847 		  if (info->error_textrel)
   12848 		    info->callbacks->einfo
   12849 		      (_("%P%X: read-only segment has dynamic relocations\n"));
   12850 		  else
   12851 		    info->callbacks->einfo
   12852 		      (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
   12853 		  break;
   12854 		}
   12855 	    }
   12856 	}
   12857 
   12858       for (o = dynobj->sections; o != NULL; o = o->next)
   12859 	{
   12860 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   12861 	      || o->size == 0
   12862 	      || o->output_section == bfd_abs_section_ptr)
   12863 	    continue;
   12864 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   12865 	    {
   12866 	      /* At this point, we are only interested in sections
   12867 		 created by _bfd_elf_link_create_dynamic_sections.  */
   12868 	      continue;
   12869 	    }
   12870 	  if (htab->stab_info.stabstr == o)
   12871 	    continue;
   12872 	  if (htab->eh_info.hdr_sec == o)
   12873 	    continue;
   12874 	  if (strcmp (o->name, ".dynstr") != 0)
   12875 	    {
   12876 	      bfd_size_type octets = ((file_ptr) o->output_offset
   12877 				      * bfd_octets_per_byte (abfd, o));
   12878 	      if (!bfd_set_section_contents (abfd, o->output_section,
   12879 					     o->contents, octets, o->size))
   12880 		goto error_return;
   12881 	    }
   12882 	  else
   12883 	    {
   12884 	      /* The contents of the .dynstr section are actually in a
   12885 		 stringtab.  */
   12886 	      file_ptr off;
   12887 
   12888 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   12889 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   12890 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
   12891 		goto error_return;
   12892 	    }
   12893 	}
   12894     }
   12895 
   12896   if (!info->resolve_section_groups)
   12897     {
   12898       bfd_boolean failed = FALSE;
   12899 
   12900       BFD_ASSERT (bfd_link_relocatable (info));
   12901       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   12902       if (failed)
   12903 	goto error_return;
   12904     }
   12905 
   12906   /* If we have optimized stabs strings, output them.  */
   12907   if (htab->stab_info.stabstr != NULL)
   12908     {
   12909       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
   12910 	goto error_return;
   12911     }
   12912 
   12913   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   12914     goto error_return;
   12915 
   12916   if (info->callbacks->emit_ctf)
   12917       info->callbacks->emit_ctf ();
   12918 
   12919   elf_final_link_free (abfd, &flinfo);
   12920 
   12921   if (attr_section)
   12922     {
   12923       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
   12924       if (contents == NULL)
   12925 	return FALSE;	/* Bail out and fail.  */
   12926       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
   12927       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
   12928       free (contents);
   12929     }
   12930 
   12931   return TRUE;
   12932 
   12933  error_return:
   12934   elf_final_link_free (abfd, &flinfo);
   12935   return FALSE;
   12936 }
   12937 
   12938 /* Initialize COOKIE for input bfd ABFD.  */
   12940 
   12941 static bfd_boolean
   12942 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   12943 		   struct bfd_link_info *info, bfd *abfd)
   12944 {
   12945   Elf_Internal_Shdr *symtab_hdr;
   12946   const struct elf_backend_data *bed;
   12947 
   12948   bed = get_elf_backend_data (abfd);
   12949   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12950 
   12951   cookie->abfd = abfd;
   12952   cookie->sym_hashes = elf_sym_hashes (abfd);
   12953   cookie->bad_symtab = elf_bad_symtab (abfd);
   12954   if (cookie->bad_symtab)
   12955     {
   12956       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   12957       cookie->extsymoff = 0;
   12958     }
   12959   else
   12960     {
   12961       cookie->locsymcount = symtab_hdr->sh_info;
   12962       cookie->extsymoff = symtab_hdr->sh_info;
   12963     }
   12964 
   12965   if (bed->s->arch_size == 32)
   12966     cookie->r_sym_shift = 8;
   12967   else
   12968     cookie->r_sym_shift = 32;
   12969 
   12970   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   12971   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   12972     {
   12973       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   12974 					      cookie->locsymcount, 0,
   12975 					      NULL, NULL, NULL);
   12976       if (cookie->locsyms == NULL)
   12977 	{
   12978 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   12979 	  return FALSE;
   12980 	}
   12981       if (info->keep_memory)
   12982 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   12983     }
   12984   return TRUE;
   12985 }
   12986 
   12987 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   12988 
   12989 static void
   12990 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   12991 {
   12992   Elf_Internal_Shdr *symtab_hdr;
   12993 
   12994   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12995   if (cookie->locsyms != NULL
   12996       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   12997     free (cookie->locsyms);
   12998 }
   12999 
   13000 /* Initialize the relocation information in COOKIE for input section SEC
   13001    of input bfd ABFD.  */
   13002 
   13003 static bfd_boolean
   13004 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13005 			struct bfd_link_info *info, bfd *abfd,
   13006 			asection *sec)
   13007 {
   13008   if (sec->reloc_count == 0)
   13009     {
   13010       cookie->rels = NULL;
   13011       cookie->relend = NULL;
   13012     }
   13013   else
   13014     {
   13015       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   13016 						info->keep_memory);
   13017       if (cookie->rels == NULL)
   13018 	return FALSE;
   13019       cookie->rel = cookie->rels;
   13020       cookie->relend = cookie->rels + sec->reloc_count;
   13021     }
   13022   cookie->rel = cookie->rels;
   13023   return TRUE;
   13024 }
   13025 
   13026 /* Free the memory allocated by init_reloc_cookie_rels,
   13027    if appropriate.  */
   13028 
   13029 static void
   13030 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   13031 			asection *sec)
   13032 {
   13033   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
   13034     free (cookie->rels);
   13035 }
   13036 
   13037 /* Initialize the whole of COOKIE for input section SEC.  */
   13038 
   13039 static bfd_boolean
   13040 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13041 			       struct bfd_link_info *info,
   13042 			       asection *sec)
   13043 {
   13044   if (!init_reloc_cookie (cookie, info, sec->owner))
   13045     goto error1;
   13046   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   13047     goto error2;
   13048   return TRUE;
   13049 
   13050  error2:
   13051   fini_reloc_cookie (cookie, sec->owner);
   13052  error1:
   13053   return FALSE;
   13054 }
   13055 
   13056 /* Free the memory allocated by init_reloc_cookie_for_section,
   13057    if appropriate.  */
   13058 
   13059 static void
   13060 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   13061 			       asection *sec)
   13062 {
   13063   fini_reloc_cookie_rels (cookie, sec);
   13064   fini_reloc_cookie (cookie, sec->owner);
   13065 }
   13066 
   13067 /* Garbage collect unused sections.  */
   13069 
   13070 /* Default gc_mark_hook.  */
   13071 
   13072 asection *
   13073 _bfd_elf_gc_mark_hook (asection *sec,
   13074 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13075 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   13076 		       struct elf_link_hash_entry *h,
   13077 		       Elf_Internal_Sym *sym)
   13078 {
   13079   if (h != NULL)
   13080     {
   13081       switch (h->root.type)
   13082 	{
   13083 	case bfd_link_hash_defined:
   13084 	case bfd_link_hash_defweak:
   13085 	  return h->root.u.def.section;
   13086 
   13087 	case bfd_link_hash_common:
   13088 	  return h->root.u.c.p->section;
   13089 
   13090 	default:
   13091 	  break;
   13092 	}
   13093     }
   13094   else
   13095     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   13096 
   13097   return NULL;
   13098 }
   13099 
   13100 /* Return the debug definition section.  */
   13101 
   13102 static asection *
   13103 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
   13104 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13105 			   Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   13106 			   struct elf_link_hash_entry *h,
   13107 			   Elf_Internal_Sym *sym)
   13108 {
   13109   if (h != NULL)
   13110     {
   13111       /* Return the global debug definition section.  */
   13112       if ((h->root.type == bfd_link_hash_defined
   13113 	   || h->root.type == bfd_link_hash_defweak)
   13114 	  && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
   13115 	return h->root.u.def.section;
   13116     }
   13117   else
   13118     {
   13119       /* Return the local debug definition section.  */
   13120       asection *isec = bfd_section_from_elf_index (sec->owner,
   13121 						   sym->st_shndx);
   13122       if ((isec->flags & SEC_DEBUGGING) != 0)
   13123 	return isec;
   13124     }
   13125 
   13126   return NULL;
   13127 }
   13128 
   13129 /* COOKIE->rel describes a relocation against section SEC, which is
   13130    a section we've decided to keep.  Return the section that contains
   13131    the relocation symbol, or NULL if no section contains it.  */
   13132 
   13133 asection *
   13134 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   13135 		       elf_gc_mark_hook_fn gc_mark_hook,
   13136 		       struct elf_reloc_cookie *cookie,
   13137 		       bfd_boolean *start_stop)
   13138 {
   13139   unsigned long r_symndx;
   13140   struct elf_link_hash_entry *h, *hw;
   13141 
   13142   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   13143   if (r_symndx == STN_UNDEF)
   13144     return NULL;
   13145 
   13146   if (r_symndx >= cookie->locsymcount
   13147       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   13148     {
   13149       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
   13150       if (h == NULL)
   13151 	{
   13152 	  info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
   13153 				  sec->owner);
   13154 	  return NULL;
   13155 	}
   13156       while (h->root.type == bfd_link_hash_indirect
   13157 	     || h->root.type == bfd_link_hash_warning)
   13158 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   13159       h->mark = 1;
   13160       /* Keep all aliases of the symbol too.  If an object symbol
   13161 	 needs to be copied into .dynbss then all of its aliases
   13162 	 should be present as dynamic symbols, not just the one used
   13163 	 on the copy relocation.  */
   13164       hw = h;
   13165       while (hw->is_weakalias)
   13166 	{
   13167 	  hw = hw->u.alias;
   13168 	  hw->mark = 1;
   13169 	}
   13170 
   13171       if (start_stop != NULL)
   13172 	{
   13173 	  /* To work around a glibc bug, mark XXX input sections
   13174 	     when there is a reference to __start_XXX or __stop_XXX
   13175 	     symbols.  */
   13176 	  if (h->start_stop)
   13177 	    {
   13178 	      asection *s = h->u2.start_stop_section;
   13179 	      *start_stop = !s->gc_mark;
   13180 	      return s;
   13181 	    }
   13182 	}
   13183 
   13184       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   13185     }
   13186 
   13187   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   13188 			  &cookie->locsyms[r_symndx]);
   13189 }
   13190 
   13191 /* COOKIE->rel describes a relocation against section SEC, which is
   13192    a section we've decided to keep.  Mark the section that contains
   13193    the relocation symbol.  */
   13194 
   13195 bfd_boolean
   13196 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   13197 			asection *sec,
   13198 			elf_gc_mark_hook_fn gc_mark_hook,
   13199 			struct elf_reloc_cookie *cookie)
   13200 {
   13201   asection *rsec;
   13202   bfd_boolean start_stop = FALSE;
   13203 
   13204   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   13205   while (rsec != NULL)
   13206     {
   13207       if (!rsec->gc_mark)
   13208 	{
   13209 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   13210 	      || (rsec->owner->flags & DYNAMIC) != 0)
   13211 	    rsec->gc_mark = 1;
   13212 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   13213 	    return FALSE;
   13214 	}
   13215       if (!start_stop)
   13216 	break;
   13217       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   13218     }
   13219   return TRUE;
   13220 }
   13221 
   13222 /* The mark phase of garbage collection.  For a given section, mark
   13223    it and any sections in this section's group, and all the sections
   13224    which define symbols to which it refers.  */
   13225 
   13226 bfd_boolean
   13227 _bfd_elf_gc_mark (struct bfd_link_info *info,
   13228 		  asection *sec,
   13229 		  elf_gc_mark_hook_fn gc_mark_hook)
   13230 {
   13231   bfd_boolean ret;
   13232   asection *group_sec, *eh_frame;
   13233 
   13234   sec->gc_mark = 1;
   13235 
   13236   /* Mark all the sections in the group.  */
   13237   group_sec = elf_section_data (sec)->next_in_group;
   13238   if (group_sec && !group_sec->gc_mark)
   13239     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   13240       return FALSE;
   13241 
   13242   /* Look through the section relocs.  */
   13243   ret = TRUE;
   13244   eh_frame = elf_eh_frame_section (sec->owner);
   13245   if ((sec->flags & SEC_RELOC) != 0
   13246       && sec->reloc_count > 0
   13247       && sec != eh_frame)
   13248     {
   13249       struct elf_reloc_cookie cookie;
   13250 
   13251       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   13252 	ret = FALSE;
   13253       else
   13254 	{
   13255 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   13256 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   13257 	      {
   13258 		ret = FALSE;
   13259 		break;
   13260 	      }
   13261 	  fini_reloc_cookie_for_section (&cookie, sec);
   13262 	}
   13263     }
   13264 
   13265   if (ret && eh_frame && elf_fde_list (sec))
   13266     {
   13267       struct elf_reloc_cookie cookie;
   13268 
   13269       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
   13270 	ret = FALSE;
   13271       else
   13272 	{
   13273 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   13274 				      gc_mark_hook, &cookie))
   13275 	    ret = FALSE;
   13276 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   13277 	}
   13278     }
   13279 
   13280   eh_frame = elf_section_eh_frame_entry (sec);
   13281   if (ret && eh_frame && !eh_frame->gc_mark)
   13282     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   13283       ret = FALSE;
   13284 
   13285   return ret;
   13286 }
   13287 
   13288 /* Scan and mark sections in a special or debug section group.  */
   13289 
   13290 static void
   13291 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   13292 {
   13293   /* Point to first section of section group.  */
   13294   asection *ssec;
   13295   /* Used to iterate the section group.  */
   13296   asection *msec;
   13297 
   13298   bfd_boolean is_special_grp = TRUE;
   13299   bfd_boolean is_debug_grp = TRUE;
   13300 
   13301   /* First scan to see if group contains any section other than debug
   13302      and special section.  */
   13303   ssec = msec = elf_next_in_group (grp);
   13304   do
   13305     {
   13306       if ((msec->flags & SEC_DEBUGGING) == 0)
   13307 	is_debug_grp = FALSE;
   13308 
   13309       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   13310 	is_special_grp = FALSE;
   13311 
   13312       msec = elf_next_in_group (msec);
   13313     }
   13314   while (msec != ssec);
   13315 
   13316   /* If this is a pure debug section group or pure special section group,
   13317      keep all sections in this group.  */
   13318   if (is_debug_grp || is_special_grp)
   13319     {
   13320       do
   13321 	{
   13322 	  msec->gc_mark = 1;
   13323 	  msec = elf_next_in_group (msec);
   13324 	}
   13325       while (msec != ssec);
   13326     }
   13327 }
   13328 
   13329 /* Keep debug and special sections.  */
   13330 
   13331 bfd_boolean
   13332 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   13333 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
   13334 {
   13335   bfd *ibfd;
   13336 
   13337   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   13338     {
   13339       asection *isec;
   13340       bfd_boolean some_kept;
   13341       bfd_boolean debug_frag_seen;
   13342       bfd_boolean has_kept_debug_info;
   13343 
   13344       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   13345 	continue;
   13346       isec = ibfd->sections;
   13347       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13348 	continue;
   13349 
   13350       /* Ensure all linker created sections are kept,
   13351 	 see if any other section is already marked,
   13352 	 and note if we have any fragmented debug sections.  */
   13353       debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
   13354       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13355 	{
   13356 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   13357 	    isec->gc_mark = 1;
   13358 	  else if (isec->gc_mark
   13359 		   && (isec->flags & SEC_ALLOC) != 0
   13360 		   && elf_section_type (isec) != SHT_NOTE)
   13361 	    some_kept = TRUE;
   13362 
   13363 	  if (!debug_frag_seen
   13364 	      && (isec->flags & SEC_DEBUGGING)
   13365 	      && CONST_STRNEQ (isec->name, ".debug_line."))
   13366 	    debug_frag_seen = TRUE;
   13367 	}
   13368 
   13369       /* If no non-note alloc section in this file will be kept, then
   13370 	 we can toss out the debug and special sections.  */
   13371       if (!some_kept)
   13372 	continue;
   13373 
   13374       /* Keep debug and special sections like .comment when they are
   13375 	 not part of a group.  Also keep section groups that contain
   13376 	 just debug sections or special sections.  */
   13377       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13378 	{
   13379 	  if ((isec->flags & SEC_GROUP) != 0)
   13380 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   13381 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   13382 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   13383 		   && elf_next_in_group (isec) == NULL)
   13384 	    isec->gc_mark = 1;
   13385 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
   13386 	    has_kept_debug_info = TRUE;
   13387 	}
   13388 
   13389       /* Look for CODE sections which are going to be discarded,
   13390 	 and find and discard any fragmented debug sections which
   13391 	 are associated with that code section.  */
   13392       if (debug_frag_seen)
   13393 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13394 	  if ((isec->flags & SEC_CODE) != 0
   13395 	      && isec->gc_mark == 0)
   13396 	    {
   13397 	      unsigned int ilen;
   13398 	      asection *dsec;
   13399 
   13400 	      ilen = strlen (isec->name);
   13401 
   13402 	      /* Association is determined by the name of the debug
   13403 		 section containing the name of the code section as
   13404 		 a suffix.  For example .debug_line.text.foo is a
   13405 		 debug section associated with .text.foo.  */
   13406 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   13407 		{
   13408 		  unsigned int dlen;
   13409 
   13410 		  if (dsec->gc_mark == 0
   13411 		      || (dsec->flags & SEC_DEBUGGING) == 0)
   13412 		    continue;
   13413 
   13414 		  dlen = strlen (dsec->name);
   13415 
   13416 		  if (dlen > ilen
   13417 		      && strncmp (dsec->name + (dlen - ilen),
   13418 				  isec->name, ilen) == 0)
   13419 		    dsec->gc_mark = 0;
   13420 		}
   13421 	  }
   13422 
   13423       /* Mark debug sections referenced by kept debug sections.  */
   13424       if (has_kept_debug_info)
   13425 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13426 	  if (isec->gc_mark
   13427 	      && (isec->flags & SEC_DEBUGGING) != 0)
   13428 	    if (!_bfd_elf_gc_mark (info, isec,
   13429 				   elf_gc_mark_debug_section))
   13430 	      return FALSE;
   13431     }
   13432   return TRUE;
   13433 }
   13434 
   13435 static bfd_boolean
   13436 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   13437 {
   13438   bfd *sub;
   13439   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13440 
   13441   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13442     {
   13443       asection *o;
   13444 
   13445       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   13446 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
   13447 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   13448 	continue;
   13449       o = sub->sections;
   13450       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13451 	continue;
   13452 
   13453       for (o = sub->sections; o != NULL; o = o->next)
   13454 	{
   13455 	  /* When any section in a section group is kept, we keep all
   13456 	     sections in the section group.  If the first member of
   13457 	     the section group is excluded, we will also exclude the
   13458 	     group section.  */
   13459 	  if (o->flags & SEC_GROUP)
   13460 	    {
   13461 	      asection *first = elf_next_in_group (o);
   13462 	      o->gc_mark = first->gc_mark;
   13463 	    }
   13464 
   13465 	  if (o->gc_mark)
   13466 	    continue;
   13467 
   13468 	  /* Skip sweeping sections already excluded.  */
   13469 	  if (o->flags & SEC_EXCLUDE)
   13470 	    continue;
   13471 
   13472 	  /* Since this is early in the link process, it is simple
   13473 	     to remove a section from the output.  */
   13474 	  o->flags |= SEC_EXCLUDE;
   13475 
   13476 	  if (info->print_gc_sections && o->size != 0)
   13477 	    /* xgettext:c-format */
   13478 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
   13479 				o, sub);
   13480 	}
   13481     }
   13482 
   13483   return TRUE;
   13484 }
   13485 
   13486 /* Propagate collected vtable information.  This is called through
   13487    elf_link_hash_traverse.  */
   13488 
   13489 static bfd_boolean
   13490 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   13491 {
   13492   /* Those that are not vtables.  */
   13493   if (h->start_stop
   13494       || h->u2.vtable == NULL
   13495       || h->u2.vtable->parent == NULL)
   13496     return TRUE;
   13497 
   13498   /* Those vtables that do not have parents, we cannot merge.  */
   13499   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
   13500     return TRUE;
   13501 
   13502   /* If we've already been done, exit.  */
   13503   if (h->u2.vtable->used && h->u2.vtable->used[-1])
   13504     return TRUE;
   13505 
   13506   /* Make sure the parent's table is up to date.  */
   13507   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
   13508 
   13509   if (h->u2.vtable->used == NULL)
   13510     {
   13511       /* None of this table's entries were referenced.  Re-use the
   13512 	 parent's table.  */
   13513       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
   13514       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
   13515     }
   13516   else
   13517     {
   13518       size_t n;
   13519       bfd_boolean *cu, *pu;
   13520 
   13521       /* Or the parent's entries into ours.  */
   13522       cu = h->u2.vtable->used;
   13523       cu[-1] = TRUE;
   13524       pu = h->u2.vtable->parent->u2.vtable->used;
   13525       if (pu != NULL)
   13526 	{
   13527 	  const struct elf_backend_data *bed;
   13528 	  unsigned int log_file_align;
   13529 
   13530 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   13531 	  log_file_align = bed->s->log_file_align;
   13532 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
   13533 	  while (n--)
   13534 	    {
   13535 	      if (*pu)
   13536 		*cu = TRUE;
   13537 	      pu++;
   13538 	      cu++;
   13539 	    }
   13540 	}
   13541     }
   13542 
   13543   return TRUE;
   13544 }
   13545 
   13546 static bfd_boolean
   13547 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
   13548 {
   13549   asection *sec;
   13550   bfd_vma hstart, hend;
   13551   Elf_Internal_Rela *relstart, *relend, *rel;
   13552   const struct elf_backend_data *bed;
   13553   unsigned int log_file_align;
   13554 
   13555   /* Take care of both those symbols that do not describe vtables as
   13556      well as those that are not loaded.  */
   13557   if (h->start_stop
   13558       || h->u2.vtable == NULL
   13559       || h->u2.vtable->parent == NULL)
   13560     return TRUE;
   13561 
   13562   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   13563 	      || h->root.type == bfd_link_hash_defweak);
   13564 
   13565   sec = h->root.u.def.section;
   13566   hstart = h->root.u.def.value;
   13567   hend = hstart + h->size;
   13568 
   13569   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
   13570   if (!relstart)
   13571     return *(bfd_boolean *) okp = FALSE;
   13572   bed = get_elf_backend_data (sec->owner);
   13573   log_file_align = bed->s->log_file_align;
   13574 
   13575   relend = relstart + sec->reloc_count;
   13576 
   13577   for (rel = relstart; rel < relend; ++rel)
   13578     if (rel->r_offset >= hstart && rel->r_offset < hend)
   13579       {
   13580 	/* If the entry is in use, do nothing.  */
   13581 	if (h->u2.vtable->used
   13582 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
   13583 	  {
   13584 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   13585 	    if (h->u2.vtable->used[entry])
   13586 	      continue;
   13587 	  }
   13588 	/* Otherwise, kill it.  */
   13589 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   13590       }
   13591 
   13592   return TRUE;
   13593 }
   13594 
   13595 /* Mark sections containing dynamically referenced symbols.  When
   13596    building shared libraries, we must assume that any visible symbol is
   13597    referenced.  */
   13598 
   13599 bfd_boolean
   13600 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   13601 {
   13602   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   13603   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   13604 
   13605   if ((h->root.type == bfd_link_hash_defined
   13606        || h->root.type == bfd_link_hash_defweak)
   13607       && ((h->ref_dynamic && !h->forced_local)
   13608 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   13609 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   13610 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   13611 	      && (!bfd_link_executable (info)
   13612 		  || info->gc_keep_exported
   13613 		  || info->export_dynamic
   13614 		  || (h->dynamic
   13615 		      && d != NULL
   13616 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   13617 	      && (h->versioned >= versioned
   13618 		  || !bfd_hide_sym_by_version (info->version_info,
   13619 					       h->root.root.string)))))
   13620     h->root.u.def.section->flags |= SEC_KEEP;
   13621 
   13622   return TRUE;
   13623 }
   13624 
   13625 /* Keep all sections containing symbols undefined on the command-line,
   13626    and the section containing the entry symbol.  */
   13627 
   13628 void
   13629 _bfd_elf_gc_keep (struct bfd_link_info *info)
   13630 {
   13631   struct bfd_sym_chain *sym;
   13632 
   13633   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   13634     {
   13635       struct elf_link_hash_entry *h;
   13636 
   13637       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   13638 				FALSE, FALSE, FALSE);
   13639 
   13640       if (h != NULL
   13641 	  && (h->root.type == bfd_link_hash_defined
   13642 	      || h->root.type == bfd_link_hash_defweak)
   13643 	  && !bfd_is_abs_section (h->root.u.def.section)
   13644 	  && !bfd_is_und_section (h->root.u.def.section))
   13645 	h->root.u.def.section->flags |= SEC_KEEP;
   13646     }
   13647 }
   13648 
   13649 bfd_boolean
   13650 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   13651 				struct bfd_link_info *info)
   13652 {
   13653   bfd *ibfd = info->input_bfds;
   13654 
   13655   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   13656     {
   13657       asection *sec;
   13658       struct elf_reloc_cookie cookie;
   13659 
   13660       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   13661 	continue;
   13662       sec = ibfd->sections;
   13663       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13664 	continue;
   13665 
   13666       if (!init_reloc_cookie (&cookie, info, ibfd))
   13667 	return FALSE;
   13668 
   13669       for (sec = ibfd->sections; sec; sec = sec->next)
   13670 	{
   13671 	  if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
   13672 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
   13673 	    {
   13674 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   13675 	      fini_reloc_cookie_rels (&cookie, sec);
   13676 	    }
   13677 	}
   13678     }
   13679   return TRUE;
   13680 }
   13681 
   13682 /* Do mark and sweep of unused sections.  */
   13683 
   13684 bfd_boolean
   13685 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   13686 {
   13687   bfd_boolean ok = TRUE;
   13688   bfd *sub;
   13689   elf_gc_mark_hook_fn gc_mark_hook;
   13690   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13691   struct elf_link_hash_table *htab;
   13692 
   13693   if (!bed->can_gc_sections
   13694       || !is_elf_hash_table (info->hash))
   13695     {
   13696       _bfd_error_handler(_("warning: gc-sections option ignored"));
   13697       return TRUE;
   13698     }
   13699 
   13700   bed->gc_keep (info);
   13701   htab = elf_hash_table (info);
   13702 
   13703   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   13704      at the .eh_frame section if we can mark the FDEs individually.  */
   13705   for (sub = info->input_bfds;
   13706        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   13707        sub = sub->link.next)
   13708     {
   13709       asection *sec;
   13710       struct elf_reloc_cookie cookie;
   13711 
   13712       sec = sub->sections;
   13713       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13714 	continue;
   13715       sec = bfd_get_section_by_name (sub, ".eh_frame");
   13716       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
   13717 	{
   13718 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   13719 	  if (elf_section_data (sec)->sec_info
   13720 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   13721 	    elf_eh_frame_section (sub) = sec;
   13722 	  fini_reloc_cookie_for_section (&cookie, sec);
   13723 	  sec = bfd_get_next_section_by_name (NULL, sec);
   13724 	}
   13725     }
   13726 
   13727   /* Apply transitive closure to the vtable entry usage info.  */
   13728   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   13729   if (!ok)
   13730     return FALSE;
   13731 
   13732   /* Kill the vtable relocations that were not used.  */
   13733   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
   13734   if (!ok)
   13735     return FALSE;
   13736 
   13737   /* Mark dynamically referenced symbols.  */
   13738   if (htab->dynamic_sections_created || info->gc_keep_exported)
   13739     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   13740 
   13741   /* Grovel through relocs to find out who stays ...  */
   13742   gc_mark_hook = bed->gc_mark_hook;
   13743   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13744     {
   13745       asection *o;
   13746 
   13747       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   13748 	  || elf_object_id (sub) != elf_hash_table_id (htab)
   13749 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   13750 	continue;
   13751 
   13752       o = sub->sections;
   13753       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13754 	continue;
   13755 
   13756       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   13757 	 Also treat note sections as a root, if the section is not part
   13758 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
   13759 	 well as FINI_ARRAY sections for ld -r.  */
   13760       for (o = sub->sections; o != NULL; o = o->next)
   13761 	if (!o->gc_mark
   13762 	    && (o->flags & SEC_EXCLUDE) == 0
   13763 	    && ((o->flags & SEC_KEEP) != 0
   13764 		|| (bfd_link_relocatable (info)
   13765 		    && ((elf_section_data (o)->this_hdr.sh_type
   13766 			 == SHT_PREINIT_ARRAY)
   13767 			|| (elf_section_data (o)->this_hdr.sh_type
   13768 			    == SHT_INIT_ARRAY)
   13769 			|| (elf_section_data (o)->this_hdr.sh_type
   13770 			    == SHT_FINI_ARRAY)))
   13771 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   13772 		    && elf_next_in_group (o) == NULL )))
   13773 	  {
   13774 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   13775 	      return FALSE;
   13776 	  }
   13777     }
   13778 
   13779   /* Allow the backend to mark additional target specific sections.  */
   13780   bed->gc_mark_extra_sections (info, gc_mark_hook);
   13781 
   13782   /* ... and mark SEC_EXCLUDE for those that go.  */
   13783   return elf_gc_sweep (abfd, info);
   13784 }
   13785 
   13786 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   13788 
   13789 bfd_boolean
   13790 bfd_elf_gc_record_vtinherit (bfd *abfd,
   13791 			     asection *sec,
   13792 			     struct elf_link_hash_entry *h,
   13793 			     bfd_vma offset)
   13794 {
   13795   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   13796   struct elf_link_hash_entry **search, *child;
   13797   size_t extsymcount;
   13798   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13799 
   13800   /* The sh_info field of the symtab header tells us where the
   13801      external symbols start.  We don't care about the local symbols at
   13802      this point.  */
   13803   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   13804   if (!elf_bad_symtab (abfd))
   13805     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   13806 
   13807   sym_hashes = elf_sym_hashes (abfd);
   13808   sym_hashes_end = sym_hashes + extsymcount;
   13809 
   13810   /* Hunt down the child symbol, which is in this section at the same
   13811      offset as the relocation.  */
   13812   for (search = sym_hashes; search != sym_hashes_end; ++search)
   13813     {
   13814       if ((child = *search) != NULL
   13815 	  && (child->root.type == bfd_link_hash_defined
   13816 	      || child->root.type == bfd_link_hash_defweak)
   13817 	  && child->root.u.def.section == sec
   13818 	  && child->root.u.def.value == offset)
   13819 	goto win;
   13820     }
   13821 
   13822   /* xgettext:c-format */
   13823   _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
   13824 		      abfd, sec, (uint64_t) offset);
   13825   bfd_set_error (bfd_error_invalid_operation);
   13826   return FALSE;
   13827 
   13828  win:
   13829   if (!child->u2.vtable)
   13830     {
   13831       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
   13832 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
   13833       if (!child->u2.vtable)
   13834 	return FALSE;
   13835     }
   13836   if (!h)
   13837     {
   13838       /* This *should* only be the absolute section.  It could potentially
   13839 	 be that someone has defined a non-global vtable though, which
   13840 	 would be bad.  It isn't worth paging in the local symbols to be
   13841 	 sure though; that case should simply be handled by the assembler.  */
   13842 
   13843       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
   13844     }
   13845   else
   13846     child->u2.vtable->parent = h;
   13847 
   13848   return TRUE;
   13849 }
   13850 
   13851 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   13852 
   13853 bfd_boolean
   13854 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
   13855 			   struct elf_link_hash_entry *h,
   13856 			   bfd_vma addend)
   13857 {
   13858   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13859   unsigned int log_file_align = bed->s->log_file_align;
   13860 
   13861   if (!h)
   13862     {
   13863       /* xgettext:c-format */
   13864       _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
   13865 			  abfd, sec);
   13866       bfd_set_error (bfd_error_bad_value);
   13867       return FALSE;
   13868     }
   13869 
   13870   if (!h->u2.vtable)
   13871     {
   13872       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
   13873 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
   13874       if (!h->u2.vtable)
   13875 	return FALSE;
   13876     }
   13877 
   13878   if (addend >= h->u2.vtable->size)
   13879     {
   13880       size_t size, bytes, file_align;
   13881       bfd_boolean *ptr = h->u2.vtable->used;
   13882 
   13883       /* While the symbol is undefined, we have to be prepared to handle
   13884 	 a zero size.  */
   13885       file_align = 1 << log_file_align;
   13886       if (h->root.type == bfd_link_hash_undefined)
   13887 	size = addend + file_align;
   13888       else
   13889 	{
   13890 	  size = h->size;
   13891 	  if (addend >= size)
   13892 	    {
   13893 	      /* Oops!  We've got a reference past the defined end of
   13894 		 the table.  This is probably a bug -- shall we warn?  */
   13895 	      size = addend + file_align;
   13896 	    }
   13897 	}
   13898       size = (size + file_align - 1) & -file_align;
   13899 
   13900       /* Allocate one extra entry for use as a "done" flag for the
   13901 	 consolidation pass.  */
   13902       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
   13903 
   13904       if (ptr)
   13905 	{
   13906 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
   13907 
   13908 	  if (ptr != NULL)
   13909 	    {
   13910 	      size_t oldbytes;
   13911 
   13912 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
   13913 			  * sizeof (bfd_boolean));
   13914 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   13915 	    }
   13916 	}
   13917       else
   13918 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
   13919 
   13920       if (ptr == NULL)
   13921 	return FALSE;
   13922 
   13923       /* And arrange for that done flag to be at index -1.  */
   13924       h->u2.vtable->used = ptr + 1;
   13925       h->u2.vtable->size = size;
   13926     }
   13927 
   13928   h->u2.vtable->used[addend >> log_file_align] = TRUE;
   13929 
   13930   return TRUE;
   13931 }
   13932 
   13933 /* Map an ELF section header flag to its corresponding string.  */
   13934 typedef struct
   13935 {
   13936   char *flag_name;
   13937   flagword flag_value;
   13938 } elf_flags_to_name_table;
   13939 
   13940 static elf_flags_to_name_table elf_flags_to_names [] =
   13941 {
   13942   { "SHF_WRITE", SHF_WRITE },
   13943   { "SHF_ALLOC", SHF_ALLOC },
   13944   { "SHF_EXECINSTR", SHF_EXECINSTR },
   13945   { "SHF_MERGE", SHF_MERGE },
   13946   { "SHF_STRINGS", SHF_STRINGS },
   13947   { "SHF_INFO_LINK", SHF_INFO_LINK},
   13948   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   13949   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   13950   { "SHF_GROUP", SHF_GROUP },
   13951   { "SHF_TLS", SHF_TLS },
   13952   { "SHF_MASKOS", SHF_MASKOS },
   13953   { "SHF_EXCLUDE", SHF_EXCLUDE },
   13954 };
   13955 
   13956 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   13957 bfd_boolean
   13958 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   13959 			      struct flag_info *flaginfo,
   13960 			      asection *section)
   13961 {
   13962   const bfd_vma sh_flags = elf_section_flags (section);
   13963 
   13964   if (!flaginfo->flags_initialized)
   13965     {
   13966       bfd *obfd = info->output_bfd;
   13967       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   13968       struct flag_info_list *tf = flaginfo->flag_list;
   13969       int with_hex = 0;
   13970       int without_hex = 0;
   13971 
   13972       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   13973 	{
   13974 	  unsigned i;
   13975 	  flagword (*lookup) (char *);
   13976 
   13977 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   13978 	  if (lookup != NULL)
   13979 	    {
   13980 	      flagword hexval = (*lookup) ((char *) tf->name);
   13981 
   13982 	      if (hexval != 0)
   13983 		{
   13984 		  if (tf->with == with_flags)
   13985 		    with_hex |= hexval;
   13986 		  else if (tf->with == without_flags)
   13987 		    without_hex |= hexval;
   13988 		  tf->valid = TRUE;
   13989 		  continue;
   13990 		}
   13991 	    }
   13992 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   13993 	    {
   13994 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   13995 		{
   13996 		  if (tf->with == with_flags)
   13997 		    with_hex |= elf_flags_to_names[i].flag_value;
   13998 		  else if (tf->with == without_flags)
   13999 		    without_hex |= elf_flags_to_names[i].flag_value;
   14000 		  tf->valid = TRUE;
   14001 		  break;
   14002 		}
   14003 	    }
   14004 	  if (!tf->valid)
   14005 	    {
   14006 	      info->callbacks->einfo
   14007 		(_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   14008 	      return FALSE;
   14009 	    }
   14010 	}
   14011       flaginfo->flags_initialized = TRUE;
   14012       flaginfo->only_with_flags |= with_hex;
   14013       flaginfo->not_with_flags |= without_hex;
   14014     }
   14015 
   14016   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   14017     return FALSE;
   14018 
   14019   if ((flaginfo->not_with_flags & sh_flags) != 0)
   14020     return FALSE;
   14021 
   14022   return TRUE;
   14023 }
   14024 
   14025 struct alloc_got_off_arg {
   14026   bfd_vma gotoff;
   14027   struct bfd_link_info *info;
   14028 };
   14029 
   14030 /* We need a special top-level link routine to convert got reference counts
   14031    to real got offsets.  */
   14032 
   14033 static bfd_boolean
   14034 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   14035 {
   14036   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   14037   bfd *obfd = gofarg->info->output_bfd;
   14038   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   14039 
   14040   if (h->got.refcount > 0)
   14041     {
   14042       h->got.offset = gofarg->gotoff;
   14043       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   14044     }
   14045   else
   14046     h->got.offset = (bfd_vma) -1;
   14047 
   14048   return TRUE;
   14049 }
   14050 
   14051 /* And an accompanying bit to work out final got entry offsets once
   14052    we're done.  Should be called from final_link.  */
   14053 
   14054 bfd_boolean
   14055 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   14056 					struct bfd_link_info *info)
   14057 {
   14058   bfd *i;
   14059   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14060   bfd_vma gotoff;
   14061   struct alloc_got_off_arg gofarg;
   14062 
   14063   BFD_ASSERT (abfd == info->output_bfd);
   14064 
   14065   if (! is_elf_hash_table (info->hash))
   14066     return FALSE;
   14067 
   14068   /* The GOT offset is relative to the .got section, but the GOT header is
   14069      put into the .got.plt section, if the backend uses it.  */
   14070   if (bed->want_got_plt)
   14071     gotoff = 0;
   14072   else
   14073     gotoff = bed->got_header_size;
   14074 
   14075   /* Do the local .got entries first.  */
   14076   for (i = info->input_bfds; i; i = i->link.next)
   14077     {
   14078       bfd_signed_vma *local_got;
   14079       size_t j, locsymcount;
   14080       Elf_Internal_Shdr *symtab_hdr;
   14081 
   14082       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   14083 	continue;
   14084 
   14085       local_got = elf_local_got_refcounts (i);
   14086       if (!local_got)
   14087 	continue;
   14088 
   14089       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   14090       if (elf_bad_symtab (i))
   14091 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   14092       else
   14093 	locsymcount = symtab_hdr->sh_info;
   14094 
   14095       for (j = 0; j < locsymcount; ++j)
   14096 	{
   14097 	  if (local_got[j] > 0)
   14098 	    {
   14099 	      local_got[j] = gotoff;
   14100 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   14101 	    }
   14102 	  else
   14103 	    local_got[j] = (bfd_vma) -1;
   14104 	}
   14105     }
   14106 
   14107   /* Then the global .got entries.  .plt refcounts are handled by
   14108      adjust_dynamic_symbol  */
   14109   gofarg.gotoff = gotoff;
   14110   gofarg.info = info;
   14111   elf_link_hash_traverse (elf_hash_table (info),
   14112 			  elf_gc_allocate_got_offsets,
   14113 			  &gofarg);
   14114   return TRUE;
   14115 }
   14116 
   14117 /* Many folk need no more in the way of final link than this, once
   14118    got entry reference counting is enabled.  */
   14119 
   14120 bfd_boolean
   14121 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   14122 {
   14123   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   14124     return FALSE;
   14125 
   14126   /* Invoke the regular ELF backend linker to do all the work.  */
   14127   return bfd_elf_final_link (abfd, info);
   14128 }
   14129 
   14130 bfd_boolean
   14131 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   14132 {
   14133   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   14134 
   14135   if (rcookie->bad_symtab)
   14136     rcookie->rel = rcookie->rels;
   14137 
   14138   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   14139     {
   14140       unsigned long r_symndx;
   14141 
   14142       if (! rcookie->bad_symtab)
   14143 	if (rcookie->rel->r_offset > offset)
   14144 	  return FALSE;
   14145       if (rcookie->rel->r_offset != offset)
   14146 	continue;
   14147 
   14148       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   14149       if (r_symndx == STN_UNDEF)
   14150 	return TRUE;
   14151 
   14152       if (r_symndx >= rcookie->locsymcount
   14153 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   14154 	{
   14155 	  struct elf_link_hash_entry *h;
   14156 
   14157 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
   14158 
   14159 	  while (h->root.type == bfd_link_hash_indirect
   14160 		 || h->root.type == bfd_link_hash_warning)
   14161 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   14162 
   14163 	  if ((h->root.type == bfd_link_hash_defined
   14164 	       || h->root.type == bfd_link_hash_defweak)
   14165 	      && (h->root.u.def.section->owner != rcookie->abfd
   14166 		  || h->root.u.def.section->kept_section != NULL
   14167 		  || discarded_section (h->root.u.def.section)))
   14168 	    return TRUE;
   14169 	}
   14170       else
   14171 	{
   14172 	  /* It's not a relocation against a global symbol,
   14173 	     but it could be a relocation against a local
   14174 	     symbol for a discarded section.  */
   14175 	  asection *isec;
   14176 	  Elf_Internal_Sym *isym;
   14177 
   14178 	  /* Need to: get the symbol; get the section.  */
   14179 	  isym = &rcookie->locsyms[r_symndx];
   14180 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   14181 	  if (isec != NULL
   14182 	      && (isec->kept_section != NULL
   14183 		  || discarded_section (isec)))
   14184 	    return TRUE;
   14185 	}
   14186       return FALSE;
   14187     }
   14188   return FALSE;
   14189 }
   14190 
   14191 /* Discard unneeded references to discarded sections.
   14192    Returns -1 on error, 1 if any section's size was changed, 0 if
   14193    nothing changed.  This function assumes that the relocations are in
   14194    sorted order, which is true for all known assemblers.  */
   14195 
   14196 int
   14197 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   14198 {
   14199   struct elf_reloc_cookie cookie;
   14200   asection *o;
   14201   bfd *abfd;
   14202   int changed = 0;
   14203 
   14204   if (info->traditional_format
   14205       || !is_elf_hash_table (info->hash))
   14206     return 0;
   14207 
   14208   o = bfd_get_section_by_name (output_bfd, ".stab");
   14209   if (o != NULL)
   14210     {
   14211       asection *i;
   14212 
   14213       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   14214 	{
   14215 	  if (i->size == 0
   14216 	      || i->reloc_count == 0
   14217 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   14218 	    continue;
   14219 
   14220 	  abfd = i->owner;
   14221 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   14222 	    continue;
   14223 
   14224 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   14225 	    return -1;
   14226 
   14227 	  if (_bfd_discard_section_stabs (abfd, i,
   14228 					  elf_section_data (i)->sec_info,
   14229 					  bfd_elf_reloc_symbol_deleted_p,
   14230 					  &cookie))
   14231 	    changed = 1;
   14232 
   14233 	  fini_reloc_cookie_for_section (&cookie, i);
   14234 	}
   14235     }
   14236 
   14237   o = NULL;
   14238   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   14239     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   14240   if (o != NULL)
   14241     {
   14242       asection *i;
   14243       int eh_changed = 0;
   14244       unsigned int eh_alignment;
   14245 
   14246       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   14247 	{
   14248 	  if (i->size == 0)
   14249 	    continue;
   14250 
   14251 	  abfd = i->owner;
   14252 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   14253 	    continue;
   14254 
   14255 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   14256 	    return -1;
   14257 
   14258 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   14259 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   14260 						 bfd_elf_reloc_symbol_deleted_p,
   14261 						 &cookie))
   14262 	    {
   14263 	      eh_changed = 1;
   14264 	      if (i->size != i->rawsize)
   14265 		changed = 1;
   14266 	    }
   14267 
   14268 	  fini_reloc_cookie_for_section (&cookie, i);
   14269 	}
   14270 
   14271       eh_alignment = 1 << o->alignment_power;
   14272       /* Skip over zero terminator, and prevent empty sections from
   14273 	 adding alignment padding at the end.  */
   14274       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
   14275 	if (i->size == 0)
   14276 	  i->flags |= SEC_EXCLUDE;
   14277 	else if (i->size > 4)
   14278 	  break;
   14279       /* The last non-empty eh_frame section doesn't need padding.  */
   14280       if (i != NULL)
   14281 	i = i->map_tail.s;
   14282       /* Any prior sections must pad the last FDE out to the output
   14283 	 section alignment.  Otherwise we might have zero padding
   14284 	 between sections, which would be seen as a terminator.  */
   14285       for (; i != NULL; i = i->map_tail.s)
   14286 	if (i->size == 4)
   14287 	  /* All but the last zero terminator should have been removed.  */
   14288 	  BFD_FAIL ();
   14289 	else
   14290 	  {
   14291 	    bfd_size_type size
   14292 	      = (i->size + eh_alignment - 1) & -eh_alignment;
   14293 	    if (i->size != size)
   14294 	      {
   14295 		i->size = size;
   14296 		changed = 1;
   14297 		eh_changed = 1;
   14298 	      }
   14299 	  }
   14300       if (eh_changed)
   14301 	elf_link_hash_traverse (elf_hash_table (info),
   14302 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
   14303     }
   14304 
   14305   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   14306     {
   14307       const struct elf_backend_data *bed;
   14308       asection *s;
   14309 
   14310       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   14311 	continue;
   14312       s = abfd->sections;
   14313       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14314 	continue;
   14315 
   14316       bed = get_elf_backend_data (abfd);
   14317 
   14318       if (bed->elf_backend_discard_info != NULL)
   14319 	{
   14320 	  if (!init_reloc_cookie (&cookie, info, abfd))
   14321 	    return -1;
   14322 
   14323 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   14324 	    changed = 1;
   14325 
   14326 	  fini_reloc_cookie (&cookie, abfd);
   14327 	}
   14328     }
   14329 
   14330   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   14331     _bfd_elf_end_eh_frame_parsing (info);
   14332 
   14333   if (info->eh_frame_hdr_type
   14334       && !bfd_link_relocatable (info)
   14335       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
   14336     changed = 1;
   14337 
   14338   return changed;
   14339 }
   14340 
   14341 bfd_boolean
   14342 _bfd_elf_section_already_linked (bfd *abfd,
   14343 				 asection *sec,
   14344 				 struct bfd_link_info *info)
   14345 {
   14346   flagword flags;
   14347   const char *name, *key;
   14348   struct bfd_section_already_linked *l;
   14349   struct bfd_section_already_linked_hash_entry *already_linked_list;
   14350 
   14351   if (sec->output_section == bfd_abs_section_ptr)
   14352     return FALSE;
   14353 
   14354   flags = sec->flags;
   14355 
   14356   /* Return if it isn't a linkonce section.  A comdat group section
   14357      also has SEC_LINK_ONCE set.  */
   14358   if ((flags & SEC_LINK_ONCE) == 0)
   14359     return FALSE;
   14360 
   14361   /* Don't put group member sections on our list of already linked
   14362      sections.  They are handled as a group via their group section.  */
   14363   if (elf_sec_group (sec) != NULL)
   14364     return FALSE;
   14365 
   14366   /* For a SHT_GROUP section, use the group signature as the key.  */
   14367   name = sec->name;
   14368   if ((flags & SEC_GROUP) != 0
   14369       && elf_next_in_group (sec) != NULL
   14370       && elf_group_name (elf_next_in_group (sec)) != NULL)
   14371     key = elf_group_name (elf_next_in_group (sec));
   14372   else
   14373     {
   14374       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   14375       if (CONST_STRNEQ (name, ".gnu.linkonce.")
   14376 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   14377 	key++;
   14378       else
   14379 	/* Must be a user linkonce section that doesn't follow gcc's
   14380 	   naming convention.  In this case we won't be matching
   14381 	   single member groups.  */
   14382 	key = name;
   14383     }
   14384 
   14385   already_linked_list = bfd_section_already_linked_table_lookup (key);
   14386 
   14387   for (l = already_linked_list->entry; l != NULL; l = l->next)
   14388     {
   14389       /* We may have 2 different types of sections on the list: group
   14390 	 sections with a signature of <key> (<key> is some string),
   14391 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   14392 	 Match like sections.  LTO plugin sections are an exception.
   14393 	 They are always named .gnu.linkonce.t.<key> and match either
   14394 	 type of section.  */
   14395       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   14396 	   && ((flags & SEC_GROUP) != 0
   14397 	       || strcmp (name, l->sec->name) == 0))
   14398 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
   14399 	{
   14400 	  /* The section has already been linked.  See if we should
   14401 	     issue a warning.  */
   14402 	  if (!_bfd_handle_already_linked (sec, l, info))
   14403 	    return FALSE;
   14404 
   14405 	  if (flags & SEC_GROUP)
   14406 	    {
   14407 	      asection *first = elf_next_in_group (sec);
   14408 	      asection *s = first;
   14409 
   14410 	      while (s != NULL)
   14411 		{
   14412 		  s->output_section = bfd_abs_section_ptr;
   14413 		  /* Record which group discards it.  */
   14414 		  s->kept_section = l->sec;
   14415 		  s = elf_next_in_group (s);
   14416 		  /* These lists are circular.  */
   14417 		  if (s == first)
   14418 		    break;
   14419 		}
   14420 	    }
   14421 
   14422 	  return TRUE;
   14423 	}
   14424     }
   14425 
   14426   /* A single member comdat group section may be discarded by a
   14427      linkonce section and vice versa.  */
   14428   if ((flags & SEC_GROUP) != 0)
   14429     {
   14430       asection *first = elf_next_in_group (sec);
   14431 
   14432       if (first != NULL && elf_next_in_group (first) == first)
   14433 	/* Check this single member group against linkonce sections.  */
   14434 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   14435 	  if ((l->sec->flags & SEC_GROUP) == 0
   14436 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   14437 	    {
   14438 	      first->output_section = bfd_abs_section_ptr;
   14439 	      first->kept_section = l->sec;
   14440 	      sec->output_section = bfd_abs_section_ptr;
   14441 	      break;
   14442 	    }
   14443     }
   14444   else
   14445     /* Check this linkonce section against single member groups.  */
   14446     for (l = already_linked_list->entry; l != NULL; l = l->next)
   14447       if (l->sec->flags & SEC_GROUP)
   14448 	{
   14449 	  asection *first = elf_next_in_group (l->sec);
   14450 
   14451 	  if (first != NULL
   14452 	      && elf_next_in_group (first) == first
   14453 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   14454 	    {
   14455 	      sec->output_section = bfd_abs_section_ptr;
   14456 	      sec->kept_section = first;
   14457 	      break;
   14458 	    }
   14459 	}
   14460 
   14461   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   14462      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   14463      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   14464      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   14465      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   14466      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   14467      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   14468      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   14469      The reverse order cannot happen as there is never a bfd with only the
   14470      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   14471      matter as here were are looking only for cross-bfd sections.  */
   14472 
   14473   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
   14474     for (l = already_linked_list->entry; l != NULL; l = l->next)
   14475       if ((l->sec->flags & SEC_GROUP) == 0
   14476 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
   14477 	{
   14478 	  if (abfd != l->sec->owner)
   14479 	    sec->output_section = bfd_abs_section_ptr;
   14480 	  break;
   14481 	}
   14482 
   14483   /* This is the first section with this name.  Record it.  */
   14484   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   14485     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   14486   return sec->output_section == bfd_abs_section_ptr;
   14487 }
   14488 
   14489 bfd_boolean
   14490 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   14491 {
   14492   return sym->st_shndx == SHN_COMMON;
   14493 }
   14494 
   14495 unsigned int
   14496 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   14497 {
   14498   return SHN_COMMON;
   14499 }
   14500 
   14501 asection *
   14502 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   14503 {
   14504   return bfd_com_section_ptr;
   14505 }
   14506 
   14507 bfd_vma
   14508 _bfd_elf_default_got_elt_size (bfd *abfd,
   14509 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14510 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   14511 			       bfd *ibfd ATTRIBUTE_UNUSED,
   14512 			       unsigned long symndx ATTRIBUTE_UNUSED)
   14513 {
   14514   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14515   return bed->s->arch_size / 8;
   14516 }
   14517 
   14518 /* Routines to support the creation of dynamic relocs.  */
   14519 
   14520 /* Returns the name of the dynamic reloc section associated with SEC.  */
   14521 
   14522 static const char *
   14523 get_dynamic_reloc_section_name (bfd *       abfd,
   14524 				asection *  sec,
   14525 				bfd_boolean is_rela)
   14526 {
   14527   char *name;
   14528   const char *old_name = bfd_section_name (sec);
   14529   const char *prefix = is_rela ? ".rela" : ".rel";
   14530 
   14531   if (old_name == NULL)
   14532     return NULL;
   14533 
   14534   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   14535   sprintf (name, "%s%s", prefix, old_name);
   14536 
   14537   return name;
   14538 }
   14539 
   14540 /* Returns the dynamic reloc section associated with SEC.
   14541    If necessary compute the name of the dynamic reloc section based
   14542    on SEC's name (looked up in ABFD's string table) and the setting
   14543    of IS_RELA.  */
   14544 
   14545 asection *
   14546 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
   14547 				    asection *  sec,
   14548 				    bfd_boolean is_rela)
   14549 {
   14550   asection * reloc_sec = elf_section_data (sec)->sreloc;
   14551 
   14552   if (reloc_sec == NULL)
   14553     {
   14554       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   14555 
   14556       if (name != NULL)
   14557 	{
   14558 	  reloc_sec = bfd_get_linker_section (abfd, name);
   14559 
   14560 	  if (reloc_sec != NULL)
   14561 	    elf_section_data (sec)->sreloc = reloc_sec;
   14562 	}
   14563     }
   14564 
   14565   return reloc_sec;
   14566 }
   14567 
   14568 /* Returns the dynamic reloc section associated with SEC.  If the
   14569    section does not exist it is created and attached to the DYNOBJ
   14570    bfd and stored in the SRELOC field of SEC's elf_section_data
   14571    structure.
   14572 
   14573    ALIGNMENT is the alignment for the newly created section and
   14574    IS_RELA defines whether the name should be .rela.<SEC's name>
   14575    or .rel.<SEC's name>.  The section name is looked up in the
   14576    string table associated with ABFD.  */
   14577 
   14578 asection *
   14579 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   14580 				     bfd *dynobj,
   14581 				     unsigned int alignment,
   14582 				     bfd *abfd,
   14583 				     bfd_boolean is_rela)
   14584 {
   14585   asection * reloc_sec = elf_section_data (sec)->sreloc;
   14586 
   14587   if (reloc_sec == NULL)
   14588     {
   14589       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   14590 
   14591       if (name == NULL)
   14592 	return NULL;
   14593 
   14594       reloc_sec = bfd_get_linker_section (dynobj, name);
   14595 
   14596       if (reloc_sec == NULL)
   14597 	{
   14598 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   14599 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   14600 	  if ((sec->flags & SEC_ALLOC) != 0)
   14601 	    flags |= SEC_ALLOC | SEC_LOAD;
   14602 
   14603 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   14604 	  if (reloc_sec != NULL)
   14605 	    {
   14606 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   14607 		 name.  Override as it may be wrong, eg. for a user
   14608 		 section named "auto" we'll get ".relauto" which is
   14609 		 seen to be a .rela section.  */
   14610 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   14611 	      if (!bfd_set_section_alignment (reloc_sec, alignment))
   14612 		reloc_sec = NULL;
   14613 	    }
   14614 	}
   14615 
   14616       elf_section_data (sec)->sreloc = reloc_sec;
   14617     }
   14618 
   14619   return reloc_sec;
   14620 }
   14621 
   14622 /* Copy the ELF symbol type and other attributes for a linker script
   14623    assignment from HSRC to HDEST.  Generally this should be treated as
   14624    if we found a strong non-dynamic definition for HDEST (except that
   14625    ld ignores multiple definition errors).  */
   14626 void
   14627 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   14628 				     struct bfd_link_hash_entry *hdest,
   14629 				     struct bfd_link_hash_entry *hsrc)
   14630 {
   14631   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   14632   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   14633   Elf_Internal_Sym isym;
   14634 
   14635   ehdest->type = ehsrc->type;
   14636   ehdest->target_internal = ehsrc->target_internal;
   14637 
   14638   isym.st_other = ehsrc->other;
   14639   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
   14640 }
   14641 
   14642 /* Append a RELA relocation REL to section S in BFD.  */
   14643 
   14644 void
   14645 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   14646 {
   14647   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14648   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   14649   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   14650   bed->s->swap_reloca_out (abfd, rel, loc);
   14651 }
   14652 
   14653 /* Append a REL relocation REL to section S in BFD.  */
   14654 
   14655 void
   14656 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   14657 {
   14658   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14659   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   14660   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   14661   bed->s->swap_reloc_out (abfd, rel, loc);
   14662 }
   14663 
   14664 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
   14665 
   14666 struct bfd_link_hash_entry *
   14667 bfd_elf_define_start_stop (struct bfd_link_info *info,
   14668 			   const char *symbol, asection *sec)
   14669 {
   14670   struct elf_link_hash_entry *h;
   14671 
   14672   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
   14673 			    FALSE, FALSE, TRUE);
   14674   if (h != NULL
   14675       && (h->root.type == bfd_link_hash_undefined
   14676 	  || h->root.type == bfd_link_hash_undefweak
   14677 	  || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
   14678     {
   14679       bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
   14680       h->root.type = bfd_link_hash_defined;
   14681       h->root.u.def.section = sec;
   14682       h->root.u.def.value = 0;
   14683       h->def_regular = 1;
   14684       h->def_dynamic = 0;
   14685       h->start_stop = 1;
   14686       h->u2.start_stop_section = sec;
   14687       if (symbol[0] == '.')
   14688 	{
   14689 	  /* .startof. and .sizeof. symbols are local.  */
   14690 	  const struct elf_backend_data *bed;
   14691 	  bed = get_elf_backend_data (info->output_bfd);
   14692 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   14693 	}
   14694       else
   14695 	{
   14696 	  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   14697 	    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
   14698 	  if (was_dynamic)
   14699 	    bfd_elf_link_record_dynamic_symbol (info, h);
   14700 	}
   14701       return &h->root;
   14702     }
   14703   return NULL;
   14704 }
   14705