Home | History | Annotate | Line # | Download | only in bfd
elflink.c revision 1.15
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2018 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "bfd_stdint.h"
     24 #include "bfdlink.h"
     25 #include "libbfd.h"
     26 #define ARCH_SIZE 0
     27 #include "elf-bfd.h"
     28 #include "safe-ctype.h"
     29 #include "libiberty.h"
     30 #include "objalloc.h"
     31 #if BFD_SUPPORTS_PLUGINS
     32 #include "plugin-api.h"
     33 #include "plugin.h"
     34 #endif
     35 
     36 /* This struct is used to pass information to routines called via
     37    elf_link_hash_traverse which must return failure.  */
     38 
     39 struct elf_info_failed
     40 {
     41   struct bfd_link_info *info;
     42   bfd_boolean failed;
     43 };
     44 
     45 /* This structure is used to pass information to
     46    _bfd_elf_link_find_version_dependencies.  */
     47 
     48 struct elf_find_verdep_info
     49 {
     50   /* General link information.  */
     51   struct bfd_link_info *info;
     52   /* The number of dependencies.  */
     53   unsigned int vers;
     54   /* Whether we had a failure.  */
     55   bfd_boolean failed;
     56 };
     57 
     58 static bfd_boolean _bfd_elf_fix_symbol_flags
     59   (struct elf_link_hash_entry *, struct elf_info_failed *);
     60 
     61 asection *
     62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
     63 			     unsigned long r_symndx,
     64 			     bfd_boolean discard)
     65 {
     66   if (r_symndx >= cookie->locsymcount
     67       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
     68     {
     69       struct elf_link_hash_entry *h;
     70 
     71       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
     72 
     73       while (h->root.type == bfd_link_hash_indirect
     74 	     || h->root.type == bfd_link_hash_warning)
     75 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
     76 
     77       if ((h->root.type == bfd_link_hash_defined
     78 	   || h->root.type == bfd_link_hash_defweak)
     79 	   && discarded_section (h->root.u.def.section))
     80 	return h->root.u.def.section;
     81       else
     82 	return NULL;
     83     }
     84   else
     85     {
     86       /* It's not a relocation against a global symbol,
     87 	 but it could be a relocation against a local
     88 	 symbol for a discarded section.  */
     89       asection *isec;
     90       Elf_Internal_Sym *isym;
     91 
     92       /* Need to: get the symbol; get the section.  */
     93       isym = &cookie->locsyms[r_symndx];
     94       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
     95       if (isec != NULL
     96 	  && discard ? discarded_section (isec) : 1)
     97 	return isec;
     98      }
     99   return NULL;
    100 }
    101 
    102 /* Define a symbol in a dynamic linkage section.  */
    103 
    104 struct elf_link_hash_entry *
    105 _bfd_elf_define_linkage_sym (bfd *abfd,
    106 			     struct bfd_link_info *info,
    107 			     asection *sec,
    108 			     const char *name)
    109 {
    110   struct elf_link_hash_entry *h;
    111   struct bfd_link_hash_entry *bh;
    112   const struct elf_backend_data *bed;
    113 
    114   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
    115   if (h != NULL)
    116     {
    117       /* Zap symbol defined in an as-needed lib that wasn't linked.
    118 	 This is a symptom of a larger problem:  Absolute symbols
    119 	 defined in shared libraries can't be overridden, because we
    120 	 lose the link to the bfd which is via the symbol section.  */
    121       h->root.type = bfd_link_hash_new;
    122       bh = &h->root;
    123     }
    124   else
    125     bh = NULL;
    126 
    127   bed = get_elf_backend_data (abfd);
    128   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    129 					 sec, 0, NULL, FALSE, bed->collect,
    130 					 &bh))
    131     return NULL;
    132   h = (struct elf_link_hash_entry *) bh;
    133   BFD_ASSERT (h != NULL);
    134   h->def_regular = 1;
    135   h->non_elf = 0;
    136   h->root.linker_def = 1;
    137   h->type = STT_OBJECT;
    138   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    139     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    140 
    141   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    142   return h;
    143 }
    144 
    145 bfd_boolean
    146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    147 {
    148   flagword flags;
    149   asection *s;
    150   struct elf_link_hash_entry *h;
    151   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    152   struct elf_link_hash_table *htab = elf_hash_table (info);
    153 
    154   /* This function may be called more than once.  */
    155   if (htab->sgot != NULL)
    156     return TRUE;
    157 
    158   flags = bed->dynamic_sec_flags;
    159 
    160   s = bfd_make_section_anyway_with_flags (abfd,
    161 					  (bed->rela_plts_and_copies_p
    162 					   ? ".rela.got" : ".rel.got"),
    163 					  (bed->dynamic_sec_flags
    164 					   | SEC_READONLY));
    165   if (s == NULL
    166       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    167     return FALSE;
    168   htab->srelgot = s;
    169 
    170   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    171   if (s == NULL
    172       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    173     return FALSE;
    174   htab->sgot = s;
    175 
    176   if (bed->want_got_plt)
    177     {
    178       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    179       if (s == NULL
    180 	  || !bfd_set_section_alignment (abfd, s,
    181 					 bed->s->log_file_align))
    182 	return FALSE;
    183       htab->sgotplt = s;
    184     }
    185 
    186   /* The first bit of the global offset table is the header.  */
    187   s->size += bed->got_header_size;
    188 
    189   if (bed->want_got_sym)
    190     {
    191       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    192 	 (or .got.plt) section.  We don't do this in the linker script
    193 	 because we don't want to define the symbol if we are not creating
    194 	 a global offset table.  */
    195       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    196 				       "_GLOBAL_OFFSET_TABLE_");
    197       elf_hash_table (info)->hgot = h;
    198       if (h == NULL)
    199 	return FALSE;
    200     }
    201 
    202   return TRUE;
    203 }
    204 
    205 /* Create a strtab to hold the dynamic symbol names.  */
    207 static bfd_boolean
    208 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    209 {
    210   struct elf_link_hash_table *hash_table;
    211 
    212   hash_table = elf_hash_table (info);
    213   if (hash_table->dynobj == NULL)
    214     {
    215       /* We may not set dynobj, an input file holding linker created
    216 	 dynamic sections to abfd, which may be a dynamic object with
    217 	 its own dynamic sections.  We need to find a normal input file
    218 	 to hold linker created sections if possible.  */
    219       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
    220 	{
    221 	  bfd *ibfd;
    222 	  asection *s;
    223 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
    224 	    if ((ibfd->flags
    225 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
    226 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
    227 		&& !((s = ibfd->sections) != NULL
    228 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
    229 	      {
    230 		abfd = ibfd;
    231 		break;
    232 	      }
    233 	}
    234       hash_table->dynobj = abfd;
    235     }
    236 
    237   if (hash_table->dynstr == NULL)
    238     {
    239       hash_table->dynstr = _bfd_elf_strtab_init ();
    240       if (hash_table->dynstr == NULL)
    241 	return FALSE;
    242     }
    243   return TRUE;
    244 }
    245 
    246 /* Create some sections which will be filled in with dynamic linking
    247    information.  ABFD is an input file which requires dynamic sections
    248    to be created.  The dynamic sections take up virtual memory space
    249    when the final executable is run, so we need to create them before
    250    addresses are assigned to the output sections.  We work out the
    251    actual contents and size of these sections later.  */
    252 
    253 bfd_boolean
    254 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    255 {
    256   flagword flags;
    257   asection *s;
    258   const struct elf_backend_data *bed;
    259   struct elf_link_hash_entry *h;
    260 
    261   if (! is_elf_hash_table (info->hash))
    262     return FALSE;
    263 
    264   if (elf_hash_table (info)->dynamic_sections_created)
    265     return TRUE;
    266 
    267   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    268     return FALSE;
    269 
    270   abfd = elf_hash_table (info)->dynobj;
    271   bed = get_elf_backend_data (abfd);
    272 
    273   flags = bed->dynamic_sec_flags;
    274 
    275   /* A dynamically linked executable has a .interp section, but a
    276      shared library does not.  */
    277   if (bfd_link_executable (info) && !info->nointerp)
    278     {
    279       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    280 					      flags | SEC_READONLY);
    281       if (s == NULL)
    282 	return FALSE;
    283     }
    284 
    285   /* Create sections to hold version informations.  These are removed
    286      if they are not needed.  */
    287   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    288 					  flags | SEC_READONLY);
    289   if (s == NULL
    290       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    291     return FALSE;
    292 
    293   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    294 					  flags | SEC_READONLY);
    295   if (s == NULL
    296       || ! bfd_set_section_alignment (abfd, s, 1))
    297     return FALSE;
    298 
    299   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    300 					  flags | SEC_READONLY);
    301   if (s == NULL
    302       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    303     return FALSE;
    304 
    305   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    306 					  flags | SEC_READONLY);
    307   if (s == NULL
    308       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    309     return FALSE;
    310   elf_hash_table (info)->dynsym = s;
    311 
    312   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    313 					  flags | SEC_READONLY);
    314   if (s == NULL)
    315     return FALSE;
    316 
    317   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    318   if (s == NULL
    319       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    320     return FALSE;
    321 
    322   /* The special symbol _DYNAMIC is always set to the start of the
    323      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    324      only want to define it if we are, in fact, creating a .dynamic
    325      section.  We don't want to define it if there is no .dynamic
    326      section, since on some ELF platforms the start up code examines it
    327      to decide how to initialize the process.  */
    328   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    329   elf_hash_table (info)->hdynamic = h;
    330   if (h == NULL)
    331     return FALSE;
    332 
    333   if (info->emit_hash)
    334     {
    335       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    336 					      flags | SEC_READONLY);
    337       if (s == NULL
    338 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    339 	return FALSE;
    340       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    341     }
    342 
    343   if (info->emit_gnu_hash)
    344     {
    345       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    346 					      flags | SEC_READONLY);
    347       if (s == NULL
    348 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    349 	return FALSE;
    350       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    351 	 4 32-bit words followed by variable count of 64-bit words, then
    352 	 variable count of 32-bit words.  */
    353       if (bed->s->arch_size == 64)
    354 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    355       else
    356 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    357     }
    358 
    359   /* Let the backend create the rest of the sections.  This lets the
    360      backend set the right flags.  The backend will normally create
    361      the .got and .plt sections.  */
    362   if (bed->elf_backend_create_dynamic_sections == NULL
    363       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    364     return FALSE;
    365 
    366   elf_hash_table (info)->dynamic_sections_created = TRUE;
    367 
    368   return TRUE;
    369 }
    370 
    371 /* Create dynamic sections when linking against a dynamic object.  */
    372 
    373 bfd_boolean
    374 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    375 {
    376   flagword flags, pltflags;
    377   struct elf_link_hash_entry *h;
    378   asection *s;
    379   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    380   struct elf_link_hash_table *htab = elf_hash_table (info);
    381 
    382   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    383      .rel[a].bss sections.  */
    384   flags = bed->dynamic_sec_flags;
    385 
    386   pltflags = flags;
    387   if (bed->plt_not_loaded)
    388     /* We do not clear SEC_ALLOC here because we still want the OS to
    389        allocate space for the section; it's just that there's nothing
    390        to read in from the object file.  */
    391     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    392   else
    393     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    394   if (bed->plt_readonly)
    395     pltflags |= SEC_READONLY;
    396 
    397   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    398   if (s == NULL
    399       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    400     return FALSE;
    401   htab->splt = s;
    402 
    403   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    404      .plt section.  */
    405   if (bed->want_plt_sym)
    406     {
    407       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    408 				       "_PROCEDURE_LINKAGE_TABLE_");
    409       elf_hash_table (info)->hplt = h;
    410       if (h == NULL)
    411 	return FALSE;
    412     }
    413 
    414   s = bfd_make_section_anyway_with_flags (abfd,
    415 					  (bed->rela_plts_and_copies_p
    416 					   ? ".rela.plt" : ".rel.plt"),
    417 					  flags | SEC_READONLY);
    418   if (s == NULL
    419       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    420     return FALSE;
    421   htab->srelplt = s;
    422 
    423   if (! _bfd_elf_create_got_section (abfd, info))
    424     return FALSE;
    425 
    426   if (bed->want_dynbss)
    427     {
    428       /* The .dynbss section is a place to put symbols which are defined
    429 	 by dynamic objects, are referenced by regular objects, and are
    430 	 not functions.  We must allocate space for them in the process
    431 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    432 	 initialize them at run time.  The linker script puts the .dynbss
    433 	 section into the .bss section of the final image.  */
    434       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    435 					      SEC_ALLOC | SEC_LINKER_CREATED);
    436       if (s == NULL)
    437 	return FALSE;
    438       htab->sdynbss = s;
    439 
    440       if (bed->want_dynrelro)
    441 	{
    442 	  /* Similarly, but for symbols that were originally in read-only
    443 	     sections.  This section doesn't really need to have contents,
    444 	     but make it like other .data.rel.ro sections.  */
    445 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
    446 						  flags);
    447 	  if (s == NULL)
    448 	    return FALSE;
    449 	  htab->sdynrelro = s;
    450 	}
    451 
    452       /* The .rel[a].bss section holds copy relocs.  This section is not
    453 	 normally needed.  We need to create it here, though, so that the
    454 	 linker will map it to an output section.  We can't just create it
    455 	 only if we need it, because we will not know whether we need it
    456 	 until we have seen all the input files, and the first time the
    457 	 main linker code calls BFD after examining all the input files
    458 	 (size_dynamic_sections) the input sections have already been
    459 	 mapped to the output sections.  If the section turns out not to
    460 	 be needed, we can discard it later.  We will never need this
    461 	 section when generating a shared object, since they do not use
    462 	 copy relocs.  */
    463       if (bfd_link_executable (info))
    464 	{
    465 	  s = bfd_make_section_anyway_with_flags (abfd,
    466 						  (bed->rela_plts_and_copies_p
    467 						   ? ".rela.bss" : ".rel.bss"),
    468 						  flags | SEC_READONLY);
    469 	  if (s == NULL
    470 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    471 	    return FALSE;
    472 	  htab->srelbss = s;
    473 
    474 	  if (bed->want_dynrelro)
    475 	    {
    476 	      s = (bfd_make_section_anyway_with_flags
    477 		   (abfd, (bed->rela_plts_and_copies_p
    478 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
    479 		    flags | SEC_READONLY));
    480 	      if (s == NULL
    481 		  || ! bfd_set_section_alignment (abfd, s,
    482 						  bed->s->log_file_align))
    483 		return FALSE;
    484 	      htab->sreldynrelro = s;
    485 	    }
    486 	}
    487     }
    488 
    489   return TRUE;
    490 }
    491 
    492 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    494    read the input files, since we need to have a list of all of them
    495    before we can determine the final sizes of the output sections.
    496    Note that we may actually call this function even though we are not
    497    going to output any dynamic symbols; in some cases we know that a
    498    symbol should be in the dynamic symbol table, but only if there is
    499    one.  */
    500 
    501 bfd_boolean
    502 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    503 				    struct elf_link_hash_entry *h)
    504 {
    505   if (h->dynindx == -1)
    506     {
    507       struct elf_strtab_hash *dynstr;
    508       char *p;
    509       const char *name;
    510       size_t indx;
    511 
    512       /* XXX: The ABI draft says the linker must turn hidden and
    513 	 internal symbols into STB_LOCAL symbols when producing the
    514 	 DSO. However, if ld.so honors st_other in the dynamic table,
    515 	 this would not be necessary.  */
    516       switch (ELF_ST_VISIBILITY (h->other))
    517 	{
    518 	case STV_INTERNAL:
    519 	case STV_HIDDEN:
    520 	  if (h->root.type != bfd_link_hash_undefined
    521 	      && h->root.type != bfd_link_hash_undefweak)
    522 	    {
    523 	      h->forced_local = 1;
    524 	      if (!elf_hash_table (info)->is_relocatable_executable)
    525 		return TRUE;
    526 	    }
    527 
    528 	default:
    529 	  break;
    530 	}
    531 
    532       h->dynindx = elf_hash_table (info)->dynsymcount;
    533       ++elf_hash_table (info)->dynsymcount;
    534 
    535       dynstr = elf_hash_table (info)->dynstr;
    536       if (dynstr == NULL)
    537 	{
    538 	  /* Create a strtab to hold the dynamic symbol names.  */
    539 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    540 	  if (dynstr == NULL)
    541 	    return FALSE;
    542 	}
    543 
    544       /* We don't put any version information in the dynamic string
    545 	 table.  */
    546       name = h->root.root.string;
    547       p = strchr (name, ELF_VER_CHR);
    548       if (p != NULL)
    549 	/* We know that the p points into writable memory.  In fact,
    550 	   there are only a few symbols that have read-only names, being
    551 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
    552 	   by the backends.  Most symbols will have names pointing into
    553 	   an ELF string table read from a file, or to objalloc memory.  */
    554 	*p = 0;
    555 
    556       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    557 
    558       if (p != NULL)
    559 	*p = ELF_VER_CHR;
    560 
    561       if (indx == (size_t) -1)
    562 	return FALSE;
    563       h->dynstr_index = indx;
    564     }
    565 
    566   return TRUE;
    567 }
    568 
    569 /* Mark a symbol dynamic.  */
    571 
    572 static void
    573 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    574 				  struct elf_link_hash_entry *h,
    575 				  Elf_Internal_Sym *sym)
    576 {
    577   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    578 
    579   /* It may be called more than once on the same H.  */
    580   if(h->dynamic || bfd_link_relocatable (info))
    581     return;
    582 
    583   if ((info->dynamic_data
    584        && (h->type == STT_OBJECT
    585 	   || h->type == STT_COMMON
    586 	   || (sym != NULL
    587 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
    588 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
    589       || (d != NULL
    590 	  && h->non_elf
    591 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    592     h->dynamic = 1;
    593 }
    594 
    595 /* Record an assignment to a symbol made by a linker script.  We need
    596    this in case some dynamic object refers to this symbol.  */
    597 
    598 bfd_boolean
    599 bfd_elf_record_link_assignment (bfd *output_bfd,
    600 				struct bfd_link_info *info,
    601 				const char *name,
    602 				bfd_boolean provide,
    603 				bfd_boolean hidden)
    604 {
    605   struct elf_link_hash_entry *h, *hv;
    606   struct elf_link_hash_table *htab;
    607   const struct elf_backend_data *bed;
    608 
    609   if (!is_elf_hash_table (info->hash))
    610     return TRUE;
    611 
    612   htab = elf_hash_table (info);
    613   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
    614   if (h == NULL)
    615     return provide;
    616 
    617   if (h->root.type == bfd_link_hash_warning)
    618     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    619 
    620   if (h->versioned == unknown)
    621     {
    622       /* Set versioned if symbol version is unknown.  */
    623       char *version = strrchr (name, ELF_VER_CHR);
    624       if (version)
    625 	{
    626 	  if (version > name && version[-1] != ELF_VER_CHR)
    627 	    h->versioned = versioned_hidden;
    628 	  else
    629 	    h->versioned = versioned;
    630 	}
    631     }
    632 
    633   /* Symbols defined in a linker script but not referenced anywhere
    634      else will have non_elf set.  */
    635   if (h->non_elf)
    636     {
    637       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    638       h->non_elf = 0;
    639     }
    640 
    641   switch (h->root.type)
    642     {
    643     case bfd_link_hash_defined:
    644     case bfd_link_hash_defweak:
    645     case bfd_link_hash_common:
    646       break;
    647     case bfd_link_hash_undefweak:
    648     case bfd_link_hash_undefined:
    649       /* Since we're defining the symbol, don't let it seem to have not
    650 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    651 	 may depend on this.  */
    652       h->root.type = bfd_link_hash_new;
    653       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    654 	bfd_link_repair_undef_list (&htab->root);
    655       break;
    656     case bfd_link_hash_new:
    657       break;
    658     case bfd_link_hash_indirect:
    659       /* We had a versioned symbol in a dynamic library.  We make the
    660 	 the versioned symbol point to this one.  */
    661       bed = get_elf_backend_data (output_bfd);
    662       hv = h;
    663       while (hv->root.type == bfd_link_hash_indirect
    664 	     || hv->root.type == bfd_link_hash_warning)
    665 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    666       /* We don't need to update h->root.u since linker will set them
    667 	 later.  */
    668       h->root.type = bfd_link_hash_undefined;
    669       hv->root.type = bfd_link_hash_indirect;
    670       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    671       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    672       break;
    673     default:
    674       BFD_FAIL ();
    675       return FALSE;
    676     }
    677 
    678   /* If this symbol is being provided by the linker script, and it is
    679      currently defined by a dynamic object, but not by a regular
    680      object, then mark it as undefined so that the generic linker will
    681      force the correct value.  */
    682   if (provide
    683       && h->def_dynamic
    684       && !h->def_regular)
    685     h->root.type = bfd_link_hash_undefined;
    686 
    687   /* If this symbol is not being provided by the linker script, and it is
    688      currently defined by a dynamic object, but not by a regular object,
    689      then clear out any version information because the symbol will not be
    690      associated with the dynamic object any more.  */
    691   if (!provide
    692       && h->def_dynamic
    693       && !h->def_regular)
    694     h->verinfo.verdef = NULL;
    695 
    696   /* Make sure this symbol is not garbage collected.  */
    697   h->mark = 1;
    698 
    699   h->def_regular = 1;
    700 
    701   if (hidden)
    702     {
    703       bed = get_elf_backend_data (output_bfd);
    704       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    705 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    706       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    707     }
    708 
    709   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    710      and executables.  */
    711   if (!bfd_link_relocatable (info)
    712       && h->dynindx != -1
    713       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    714 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    715     h->forced_local = 1;
    716 
    717   if ((h->def_dynamic
    718        || h->ref_dynamic
    719        || bfd_link_dll (info)
    720        || elf_hash_table (info)->is_relocatable_executable)
    721       && h->dynindx == -1)
    722     {
    723       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    724 	return FALSE;
    725 
    726       /* If this is a weak defined symbol, and we know a corresponding
    727 	 real symbol from the same dynamic object, make sure the real
    728 	 symbol is also made into a dynamic symbol.  */
    729       if (h->is_weakalias)
    730 	{
    731 	  struct elf_link_hash_entry *def = weakdef (h);
    732 
    733 	  if (def->dynindx == -1
    734 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
    735 	    return FALSE;
    736 	}
    737     }
    738 
    739   return TRUE;
    740 }
    741 
    742 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    743    success, and 2 on a failure caused by attempting to record a symbol
    744    in a discarded section, eg. a discarded link-once section symbol.  */
    745 
    746 int
    747 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    748 					  bfd *input_bfd,
    749 					  long input_indx)
    750 {
    751   bfd_size_type amt;
    752   struct elf_link_local_dynamic_entry *entry;
    753   struct elf_link_hash_table *eht;
    754   struct elf_strtab_hash *dynstr;
    755   size_t dynstr_index;
    756   char *name;
    757   Elf_External_Sym_Shndx eshndx;
    758   char esym[sizeof (Elf64_External_Sym)];
    759 
    760   if (! is_elf_hash_table (info->hash))
    761     return 0;
    762 
    763   /* See if the entry exists already.  */
    764   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    765     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    766       return 1;
    767 
    768   amt = sizeof (*entry);
    769   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    770   if (entry == NULL)
    771     return 0;
    772 
    773   /* Go find the symbol, so that we can find it's name.  */
    774   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    775 			     1, input_indx, &entry->isym, esym, &eshndx))
    776     {
    777       bfd_release (input_bfd, entry);
    778       return 0;
    779     }
    780 
    781   if (entry->isym.st_shndx != SHN_UNDEF
    782       && entry->isym.st_shndx < SHN_LORESERVE)
    783     {
    784       asection *s;
    785 
    786       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    787       if (s == NULL || bfd_is_abs_section (s->output_section))
    788 	{
    789 	  /* We can still bfd_release here as nothing has done another
    790 	     bfd_alloc.  We can't do this later in this function.  */
    791 	  bfd_release (input_bfd, entry);
    792 	  return 2;
    793 	}
    794     }
    795 
    796   name = (bfd_elf_string_from_elf_section
    797 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    798 	   entry->isym.st_name));
    799 
    800   dynstr = elf_hash_table (info)->dynstr;
    801   if (dynstr == NULL)
    802     {
    803       /* Create a strtab to hold the dynamic symbol names.  */
    804       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    805       if (dynstr == NULL)
    806 	return 0;
    807     }
    808 
    809   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
    810   if (dynstr_index == (size_t) -1)
    811     return 0;
    812   entry->isym.st_name = dynstr_index;
    813 
    814   eht = elf_hash_table (info);
    815 
    816   entry->next = eht->dynlocal;
    817   eht->dynlocal = entry;
    818   entry->input_bfd = input_bfd;
    819   entry->input_indx = input_indx;
    820   eht->dynsymcount++;
    821 
    822   /* Whatever binding the symbol had before, it's now local.  */
    823   entry->isym.st_info
    824     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    825 
    826   /* The dynindx will be set at the end of size_dynamic_sections.  */
    827 
    828   return 1;
    829 }
    830 
    831 /* Return the dynindex of a local dynamic symbol.  */
    832 
    833 long
    834 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    835 				    bfd *input_bfd,
    836 				    long input_indx)
    837 {
    838   struct elf_link_local_dynamic_entry *e;
    839 
    840   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    841     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    842       return e->dynindx;
    843   return -1;
    844 }
    845 
    846 /* This function is used to renumber the dynamic symbols, if some of
    847    them are removed because they are marked as local.  This is called
    848    via elf_link_hash_traverse.  */
    849 
    850 static bfd_boolean
    851 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    852 				      void *data)
    853 {
    854   size_t *count = (size_t *) data;
    855 
    856   if (h->forced_local)
    857     return TRUE;
    858 
    859   if (h->dynindx != -1)
    860     h->dynindx = ++(*count);
    861 
    862   return TRUE;
    863 }
    864 
    865 
    866 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    867    STB_LOCAL binding.  */
    868 
    869 static bfd_boolean
    870 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    871 					    void *data)
    872 {
    873   size_t *count = (size_t *) data;
    874 
    875   if (!h->forced_local)
    876     return TRUE;
    877 
    878   if (h->dynindx != -1)
    879     h->dynindx = ++(*count);
    880 
    881   return TRUE;
    882 }
    883 
    884 /* Return true if the dynamic symbol for a given section should be
    885    omitted when creating a shared library.  */
    886 bfd_boolean
    887 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
    888 				   struct bfd_link_info *info,
    889 				   asection *p)
    890 {
    891   struct elf_link_hash_table *htab;
    892   asection *ip;
    893 
    894   switch (elf_section_data (p)->this_hdr.sh_type)
    895     {
    896     case SHT_PROGBITS:
    897     case SHT_NOBITS:
    898       /* If sh_type is yet undecided, assume it could be
    899 	 SHT_PROGBITS/SHT_NOBITS.  */
    900     case SHT_NULL:
    901       htab = elf_hash_table (info);
    902       if (p == htab->tls_sec)
    903 	return FALSE;
    904 
    905       if (htab->text_index_section != NULL)
    906 	return p != htab->text_index_section && p != htab->data_index_section;
    907 
    908       return (htab->dynobj != NULL
    909 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
    910 	      && ip->output_section == p);
    911 
    912       /* There shouldn't be section relative relocations
    913 	 against any other section.  */
    914     default:
    915       return TRUE;
    916     }
    917 }
    918 
    919 /* Assign dynsym indices.  In a shared library we generate a section
    920    symbol for each output section, which come first.  Next come symbols
    921    which have been forced to local binding.  Then all of the back-end
    922    allocated local dynamic syms, followed by the rest of the global
    923    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
    924    (This prevents the early call before elf_backend_init_index_section
    925    and strip_excluded_output_sections setting dynindx for sections
    926    that are stripped.)  */
    927 
    928 static unsigned long
    929 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
    930 				struct bfd_link_info *info,
    931 				unsigned long *section_sym_count)
    932 {
    933   unsigned long dynsymcount = 0;
    934   bfd_boolean do_sec = section_sym_count != NULL;
    935 
    936   if (bfd_link_pic (info)
    937       || elf_hash_table (info)->is_relocatable_executable)
    938     {
    939       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
    940       asection *p;
    941       for (p = output_bfd->sections; p ; p = p->next)
    942 	if ((p->flags & SEC_EXCLUDE) == 0
    943 	    && (p->flags & SEC_ALLOC) != 0
    944 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
    945 	  {
    946 	    ++dynsymcount;
    947 	    if (do_sec)
    948 	      elf_section_data (p)->dynindx = dynsymcount;
    949 	  }
    950 	else if (do_sec)
    951 	  elf_section_data (p)->dynindx = 0;
    952     }
    953   if (do_sec)
    954     *section_sym_count = dynsymcount;
    955 
    956   elf_link_hash_traverse (elf_hash_table (info),
    957 			  elf_link_renumber_local_hash_table_dynsyms,
    958 			  &dynsymcount);
    959 
    960   if (elf_hash_table (info)->dynlocal)
    961     {
    962       struct elf_link_local_dynamic_entry *p;
    963       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
    964 	p->dynindx = ++dynsymcount;
    965     }
    966   elf_hash_table (info)->local_dynsymcount = dynsymcount;
    967 
    968   elf_link_hash_traverse (elf_hash_table (info),
    969 			  elf_link_renumber_hash_table_dynsyms,
    970 			  &dynsymcount);
    971 
    972   /* There is an unused NULL entry at the head of the table which we
    973      must account for in our count even if the table is empty since it
    974      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
    975      .dynamic section.  */
    976   dynsymcount++;
    977 
    978   elf_hash_table (info)->dynsymcount = dynsymcount;
    979   return dynsymcount;
    980 }
    981 
    982 /* Merge st_other field.  */
    983 
    984 static void
    985 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
    986 		    const Elf_Internal_Sym *isym, asection *sec,
    987 		    bfd_boolean definition, bfd_boolean dynamic)
    988 {
    989   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    990 
    991   /* If st_other has a processor-specific meaning, specific
    992      code might be needed here.  */
    993   if (bed->elf_backend_merge_symbol_attribute)
    994     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
    995 						dynamic);
    996 
    997   if (!dynamic)
    998     {
    999       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
   1000       unsigned hvis = ELF_ST_VISIBILITY (h->other);
   1001 
   1002       /* Keep the most constraining visibility.  Leave the remainder
   1003 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
   1004       if (symvis - 1 < hvis - 1)
   1005 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
   1006     }
   1007   else if (definition
   1008 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
   1009 	   && (sec->flags & SEC_READONLY) == 0)
   1010     h->protected_def = 1;
   1011 }
   1012 
   1013 /* This function is called when we want to merge a new symbol with an
   1014    existing symbol.  It handles the various cases which arise when we
   1015    find a definition in a dynamic object, or when there is already a
   1016    definition in a dynamic object.  The new symbol is described by
   1017    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
   1018    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
   1019    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
   1020    of an old common symbol.  We set OVERRIDE if the old symbol is
   1021    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
   1022    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
   1023    to change.  By OK to change, we mean that we shouldn't warn if the
   1024    type or size does change.  */
   1025 
   1026 static bfd_boolean
   1027 _bfd_elf_merge_symbol (bfd *abfd,
   1028 		       struct bfd_link_info *info,
   1029 		       const char *name,
   1030 		       Elf_Internal_Sym *sym,
   1031 		       asection **psec,
   1032 		       bfd_vma *pvalue,
   1033 		       struct elf_link_hash_entry **sym_hash,
   1034 		       bfd **poldbfd,
   1035 		       bfd_boolean *pold_weak,
   1036 		       unsigned int *pold_alignment,
   1037 		       bfd_boolean *skip,
   1038 		       bfd_boolean *override,
   1039 		       bfd_boolean *type_change_ok,
   1040 		       bfd_boolean *size_change_ok,
   1041 		       bfd_boolean *matched)
   1042 {
   1043   asection *sec, *oldsec;
   1044   struct elf_link_hash_entry *h;
   1045   struct elf_link_hash_entry *hi;
   1046   struct elf_link_hash_entry *flip;
   1047   int bind;
   1048   bfd *oldbfd;
   1049   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   1050   bfd_boolean newweak, oldweak, newfunc, oldfunc;
   1051   const struct elf_backend_data *bed;
   1052   char *new_version;
   1053   bfd_boolean default_sym = *matched;
   1054 
   1055   *skip = FALSE;
   1056   *override = FALSE;
   1057 
   1058   sec = *psec;
   1059   bind = ELF_ST_BIND (sym->st_info);
   1060 
   1061   if (! bfd_is_und_section (sec))
   1062     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
   1063   else
   1064     h = ((struct elf_link_hash_entry *)
   1065 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
   1066   if (h == NULL)
   1067     return FALSE;
   1068   *sym_hash = h;
   1069 
   1070   bed = get_elf_backend_data (abfd);
   1071 
   1072   /* NEW_VERSION is the symbol version of the new symbol.  */
   1073   if (h->versioned != unversioned)
   1074     {
   1075       /* Symbol version is unknown or versioned.  */
   1076       new_version = strrchr (name, ELF_VER_CHR);
   1077       if (new_version)
   1078 	{
   1079 	  if (h->versioned == unknown)
   1080 	    {
   1081 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1082 		h->versioned = versioned_hidden;
   1083 	      else
   1084 		h->versioned = versioned;
   1085 	    }
   1086 	  new_version += 1;
   1087 	  if (new_version[0] == '\0')
   1088 	    new_version = NULL;
   1089 	}
   1090       else
   1091 	h->versioned = unversioned;
   1092     }
   1093   else
   1094     new_version = NULL;
   1095 
   1096   /* For merging, we only care about real symbols.  But we need to make
   1097      sure that indirect symbol dynamic flags are updated.  */
   1098   hi = h;
   1099   while (h->root.type == bfd_link_hash_indirect
   1100 	 || h->root.type == bfd_link_hash_warning)
   1101     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1102 
   1103   if (!*matched)
   1104     {
   1105       if (hi == h || h->root.type == bfd_link_hash_new)
   1106 	*matched = TRUE;
   1107       else
   1108 	{
   1109 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1110 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1111 	     true if the new symbol is only visible to the symbol with
   1112 	     the same symbol version.  */
   1113 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
   1114 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
   1115 	  if (!old_hidden && !new_hidden)
   1116 	    /* The new symbol matches the existing symbol if both
   1117 	       aren't hidden.  */
   1118 	    *matched = TRUE;
   1119 	  else
   1120 	    {
   1121 	      /* OLD_VERSION is the symbol version of the existing
   1122 		 symbol. */
   1123 	      char *old_version;
   1124 
   1125 	      if (h->versioned >= versioned)
   1126 		old_version = strrchr (h->root.root.string,
   1127 				       ELF_VER_CHR) + 1;
   1128 	      else
   1129 		 old_version = NULL;
   1130 
   1131 	      /* The new symbol matches the existing symbol if they
   1132 		 have the same symbol version.  */
   1133 	      *matched = (old_version == new_version
   1134 			  || (old_version != NULL
   1135 			      && new_version != NULL
   1136 			      && strcmp (old_version, new_version) == 0));
   1137 	    }
   1138 	}
   1139     }
   1140 
   1141   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1142      existing symbol.  */
   1143 
   1144   oldbfd = NULL;
   1145   oldsec = NULL;
   1146   switch (h->root.type)
   1147     {
   1148     default:
   1149       break;
   1150 
   1151     case bfd_link_hash_undefined:
   1152     case bfd_link_hash_undefweak:
   1153       oldbfd = h->root.u.undef.abfd;
   1154       break;
   1155 
   1156     case bfd_link_hash_defined:
   1157     case bfd_link_hash_defweak:
   1158       oldbfd = h->root.u.def.section->owner;
   1159       oldsec = h->root.u.def.section;
   1160       break;
   1161 
   1162     case bfd_link_hash_common:
   1163       oldbfd = h->root.u.c.p->section->owner;
   1164       oldsec = h->root.u.c.p->section;
   1165       if (pold_alignment)
   1166 	*pold_alignment = h->root.u.c.p->alignment_power;
   1167       break;
   1168     }
   1169   if (poldbfd && *poldbfd == NULL)
   1170     *poldbfd = oldbfd;
   1171 
   1172   /* Differentiate strong and weak symbols.  */
   1173   newweak = bind == STB_WEAK;
   1174   oldweak = (h->root.type == bfd_link_hash_defweak
   1175 	     || h->root.type == bfd_link_hash_undefweak);
   1176   if (pold_weak)
   1177     *pold_weak = oldweak;
   1178 
   1179   /* We have to check it for every instance since the first few may be
   1180      references and not all compilers emit symbol type for undefined
   1181      symbols.  */
   1182   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1183 
   1184   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1185      respectively, is from a dynamic object.  */
   1186 
   1187   newdyn = (abfd->flags & DYNAMIC) != 0;
   1188 
   1189   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1190      syms and defined syms in dynamic libraries respectively.
   1191      ref_dynamic on the other hand can be set for a symbol defined in
   1192      a dynamic library, and def_dynamic may not be set;  When the
   1193      definition in a dynamic lib is overridden by a definition in the
   1194      executable use of the symbol in the dynamic lib becomes a
   1195      reference to the executable symbol.  */
   1196   if (newdyn)
   1197     {
   1198       if (bfd_is_und_section (sec))
   1199 	{
   1200 	  if (bind != STB_WEAK)
   1201 	    {
   1202 	      h->ref_dynamic_nonweak = 1;
   1203 	      hi->ref_dynamic_nonweak = 1;
   1204 	    }
   1205 	}
   1206       else
   1207 	{
   1208 	  /* Update the existing symbol only if they match. */
   1209 	  if (*matched)
   1210 	    h->dynamic_def = 1;
   1211 	  hi->dynamic_def = 1;
   1212 	}
   1213     }
   1214 
   1215   /* If we just created the symbol, mark it as being an ELF symbol.
   1216      Other than that, there is nothing to do--there is no merge issue
   1217      with a newly defined symbol--so we just return.  */
   1218 
   1219   if (h->root.type == bfd_link_hash_new)
   1220     {
   1221       h->non_elf = 0;
   1222       return TRUE;
   1223     }
   1224 
   1225   /* In cases involving weak versioned symbols, we may wind up trying
   1226      to merge a symbol with itself.  Catch that here, to avoid the
   1227      confusion that results if we try to override a symbol with
   1228      itself.  The additional tests catch cases like
   1229      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1230      dynamic object, which we do want to handle here.  */
   1231   if (abfd == oldbfd
   1232       && (newweak || oldweak)
   1233       && ((abfd->flags & DYNAMIC) == 0
   1234 	  || !h->def_regular))
   1235     return TRUE;
   1236 
   1237   olddyn = FALSE;
   1238   if (oldbfd != NULL)
   1239     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1240   else if (oldsec != NULL)
   1241     {
   1242       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1243 	 indices used by MIPS ELF.  */
   1244       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1245     }
   1246 
   1247   /* Handle a case where plugin_notice won't be called and thus won't
   1248      set the non_ir_ref flags on the first pass over symbols.  */
   1249   if (oldbfd != NULL
   1250       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
   1251       && newdyn != olddyn)
   1252     {
   1253       h->root.non_ir_ref_dynamic = TRUE;
   1254       hi->root.non_ir_ref_dynamic = TRUE;
   1255     }
   1256 
   1257   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1258      respectively, appear to be a definition rather than reference.  */
   1259 
   1260   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1261 
   1262   olddef = (h->root.type != bfd_link_hash_undefined
   1263 	    && h->root.type != bfd_link_hash_undefweak
   1264 	    && h->root.type != bfd_link_hash_common);
   1265 
   1266   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1267      respectively, appear to be a function.  */
   1268 
   1269   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1270 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1271 
   1272   oldfunc = (h->type != STT_NOTYPE
   1273 	     && bed->is_function_type (h->type));
   1274 
   1275   if (!(newfunc && oldfunc)
   1276       && ELF_ST_TYPE (sym->st_info) != h->type
   1277       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1278       && h->type != STT_NOTYPE
   1279       && (newdef || bfd_is_com_section (sec))
   1280       && (olddef || h->root.type == bfd_link_hash_common))
   1281     {
   1282       /* If creating a default indirect symbol ("foo" or "foo@") from
   1283 	 a dynamic versioned definition ("foo@@") skip doing so if
   1284 	 there is an existing regular definition with a different
   1285 	 type.  We don't want, for example, a "time" variable in the
   1286 	 executable overriding a "time" function in a shared library.  */
   1287       if (newdyn
   1288 	  && !olddyn)
   1289 	{
   1290 	  *skip = TRUE;
   1291 	  return TRUE;
   1292 	}
   1293 
   1294       /* When adding a symbol from a regular object file after we have
   1295 	 created indirect symbols, undo the indirection and any
   1296 	 dynamic state.  */
   1297       if (hi != h
   1298 	  && !newdyn
   1299 	  && olddyn)
   1300 	{
   1301 	  h = hi;
   1302 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1303 	  h->forced_local = 0;
   1304 	  h->ref_dynamic = 0;
   1305 	  h->def_dynamic = 0;
   1306 	  h->dynamic_def = 0;
   1307 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1308 	    {
   1309 	      h->root.type = bfd_link_hash_undefined;
   1310 	      h->root.u.undef.abfd = abfd;
   1311 	    }
   1312 	  else
   1313 	    {
   1314 	      h->root.type = bfd_link_hash_new;
   1315 	      h->root.u.undef.abfd = NULL;
   1316 	    }
   1317 	  return TRUE;
   1318 	}
   1319     }
   1320 
   1321   /* Check TLS symbols.  We don't check undefined symbols introduced
   1322      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1323      check symbols from plugins because they also have no type.  */
   1324   if (oldbfd != NULL
   1325       && (oldbfd->flags & BFD_PLUGIN) == 0
   1326       && (abfd->flags & BFD_PLUGIN) == 0
   1327       && ELF_ST_TYPE (sym->st_info) != h->type
   1328       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1329     {
   1330       bfd *ntbfd, *tbfd;
   1331       bfd_boolean ntdef, tdef;
   1332       asection *ntsec, *tsec;
   1333 
   1334       if (h->type == STT_TLS)
   1335 	{
   1336 	  ntbfd = abfd;
   1337 	  ntsec = sec;
   1338 	  ntdef = newdef;
   1339 	  tbfd = oldbfd;
   1340 	  tsec = oldsec;
   1341 	  tdef = olddef;
   1342 	}
   1343       else
   1344 	{
   1345 	  ntbfd = oldbfd;
   1346 	  ntsec = oldsec;
   1347 	  ntdef = olddef;
   1348 	  tbfd = abfd;
   1349 	  tsec = sec;
   1350 	  tdef = newdef;
   1351 	}
   1352 
   1353       if (tdef && ntdef)
   1354 	_bfd_error_handler
   1355 	  /* xgettext:c-format */
   1356 	  (_("%s: TLS definition in %B section %A "
   1357 	     "mismatches non-TLS definition in %B section %A"),
   1358 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
   1359       else if (!tdef && !ntdef)
   1360 	_bfd_error_handler
   1361 	  /* xgettext:c-format */
   1362 	  (_("%s: TLS reference in %B "
   1363 	     "mismatches non-TLS reference in %B"),
   1364 	   h->root.root.string, tbfd, ntbfd);
   1365       else if (tdef)
   1366 	_bfd_error_handler
   1367 	  /* xgettext:c-format */
   1368 	  (_("%s: TLS definition in %B section %A "
   1369 	     "mismatches non-TLS reference in %B"),
   1370 	   h->root.root.string, tbfd, tsec, ntbfd);
   1371       else
   1372 	_bfd_error_handler
   1373 	  /* xgettext:c-format */
   1374 	  (_("%s: TLS reference in %B "
   1375 	     "mismatches non-TLS definition in %B section %A"),
   1376 	   h->root.root.string, tbfd, ntbfd, ntsec);
   1377 
   1378       bfd_set_error (bfd_error_bad_value);
   1379       return FALSE;
   1380     }
   1381 
   1382   /* If the old symbol has non-default visibility, we ignore the new
   1383      definition from a dynamic object.  */
   1384   if (newdyn
   1385       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1386       && !bfd_is_und_section (sec))
   1387     {
   1388       *skip = TRUE;
   1389       /* Make sure this symbol is dynamic.  */
   1390       h->ref_dynamic = 1;
   1391       hi->ref_dynamic = 1;
   1392       /* A protected symbol has external availability. Make sure it is
   1393 	 recorded as dynamic.
   1394 
   1395 	 FIXME: Should we check type and size for protected symbol?  */
   1396       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1397 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1398       else
   1399 	return TRUE;
   1400     }
   1401   else if (!newdyn
   1402 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1403 	   && h->def_dynamic)
   1404     {
   1405       /* If the new symbol with non-default visibility comes from a
   1406 	 relocatable file and the old definition comes from a dynamic
   1407 	 object, we remove the old definition.  */
   1408       if (hi->root.type == bfd_link_hash_indirect)
   1409 	{
   1410 	  /* Handle the case where the old dynamic definition is
   1411 	     default versioned.  We need to copy the symbol info from
   1412 	     the symbol with default version to the normal one if it
   1413 	     was referenced before.  */
   1414 	  if (h->ref_regular)
   1415 	    {
   1416 	      hi->root.type = h->root.type;
   1417 	      h->root.type = bfd_link_hash_indirect;
   1418 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1419 
   1420 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1421 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1422 		{
   1423 		  /* If the new symbol is hidden or internal, completely undo
   1424 		     any dynamic link state.  */
   1425 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1426 		  h->forced_local = 0;
   1427 		  h->ref_dynamic = 0;
   1428 		}
   1429 	      else
   1430 		h->ref_dynamic = 1;
   1431 
   1432 	      h->def_dynamic = 0;
   1433 	      /* FIXME: Should we check type and size for protected symbol?  */
   1434 	      h->size = 0;
   1435 	      h->type = 0;
   1436 
   1437 	      h = hi;
   1438 	    }
   1439 	  else
   1440 	    h = hi;
   1441 	}
   1442 
   1443       /* If the old symbol was undefined before, then it will still be
   1444 	 on the undefs list.  If the new symbol is undefined or
   1445 	 common, we can't make it bfd_link_hash_new here, because new
   1446 	 undefined or common symbols will be added to the undefs list
   1447 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1448 	 added twice to the undefs list.  Also, if the new symbol is
   1449 	 undefweak then we don't want to lose the strong undef.  */
   1450       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1451 	{
   1452 	  h->root.type = bfd_link_hash_undefined;
   1453 	  h->root.u.undef.abfd = abfd;
   1454 	}
   1455       else
   1456 	{
   1457 	  h->root.type = bfd_link_hash_new;
   1458 	  h->root.u.undef.abfd = NULL;
   1459 	}
   1460 
   1461       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1462 	{
   1463 	  /* If the new symbol is hidden or internal, completely undo
   1464 	     any dynamic link state.  */
   1465 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1466 	  h->forced_local = 0;
   1467 	  h->ref_dynamic = 0;
   1468 	}
   1469       else
   1470 	h->ref_dynamic = 1;
   1471       h->def_dynamic = 0;
   1472       /* FIXME: Should we check type and size for protected symbol?  */
   1473       h->size = 0;
   1474       h->type = 0;
   1475       return TRUE;
   1476     }
   1477 
   1478   /* If a new weak symbol definition comes from a regular file and the
   1479      old symbol comes from a dynamic library, we treat the new one as
   1480      strong.  Similarly, an old weak symbol definition from a regular
   1481      file is treated as strong when the new symbol comes from a dynamic
   1482      library.  Further, an old weak symbol from a dynamic library is
   1483      treated as strong if the new symbol is from a dynamic library.
   1484      This reflects the way glibc's ld.so works.
   1485 
   1486      Also allow a weak symbol to override a linker script symbol
   1487      defined by an early pass over the script.  This is done so the
   1488      linker knows the symbol is defined in an object file, for the
   1489      DEFINED script function.
   1490 
   1491      Do this before setting *type_change_ok or *size_change_ok so that
   1492      we warn properly when dynamic library symbols are overridden.  */
   1493 
   1494   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
   1495     newweak = FALSE;
   1496   if (olddef && newdyn)
   1497     oldweak = FALSE;
   1498 
   1499   /* Allow changes between different types of function symbol.  */
   1500   if (newfunc && oldfunc)
   1501     *type_change_ok = TRUE;
   1502 
   1503   /* It's OK to change the type if either the existing symbol or the
   1504      new symbol is weak.  A type change is also OK if the old symbol
   1505      is undefined and the new symbol is defined.  */
   1506 
   1507   if (oldweak
   1508       || newweak
   1509       || (newdef
   1510 	  && h->root.type == bfd_link_hash_undefined))
   1511     *type_change_ok = TRUE;
   1512 
   1513   /* It's OK to change the size if either the existing symbol or the
   1514      new symbol is weak, or if the old symbol is undefined.  */
   1515 
   1516   if (*type_change_ok
   1517       || h->root.type == bfd_link_hash_undefined)
   1518     *size_change_ok = TRUE;
   1519 
   1520   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1521      symbol, respectively, appears to be a common symbol in a dynamic
   1522      object.  If a symbol appears in an uninitialized section, and is
   1523      not weak, and is not a function, then it may be a common symbol
   1524      which was resolved when the dynamic object was created.  We want
   1525      to treat such symbols specially, because they raise special
   1526      considerations when setting the symbol size: if the symbol
   1527      appears as a common symbol in a regular object, and the size in
   1528      the regular object is larger, we must make sure that we use the
   1529      larger size.  This problematic case can always be avoided in C,
   1530      but it must be handled correctly when using Fortran shared
   1531      libraries.
   1532 
   1533      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1534      likewise for OLDDYNCOMMON and OLDDEF.
   1535 
   1536      Note that this test is just a heuristic, and that it is quite
   1537      possible to have an uninitialized symbol in a shared object which
   1538      is really a definition, rather than a common symbol.  This could
   1539      lead to some minor confusion when the symbol really is a common
   1540      symbol in some regular object.  However, I think it will be
   1541      harmless.  */
   1542 
   1543   if (newdyn
   1544       && newdef
   1545       && !newweak
   1546       && (sec->flags & SEC_ALLOC) != 0
   1547       && (sec->flags & SEC_LOAD) == 0
   1548       && sym->st_size > 0
   1549       && !newfunc)
   1550     newdyncommon = TRUE;
   1551   else
   1552     newdyncommon = FALSE;
   1553 
   1554   if (olddyn
   1555       && olddef
   1556       && h->root.type == bfd_link_hash_defined
   1557       && h->def_dynamic
   1558       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1559       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1560       && h->size > 0
   1561       && !oldfunc)
   1562     olddyncommon = TRUE;
   1563   else
   1564     olddyncommon = FALSE;
   1565 
   1566   /* We now know everything about the old and new symbols.  We ask the
   1567      backend to check if we can merge them.  */
   1568   if (bed->merge_symbol != NULL)
   1569     {
   1570       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1571 	return FALSE;
   1572       sec = *psec;
   1573     }
   1574 
   1575   /* There are multiple definitions of a normal symbol.  Skip the
   1576      default symbol as well as definition from an IR object.  */
   1577   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
   1578       && !default_sym && h->def_regular
   1579       && !(oldbfd != NULL
   1580 	   && (oldbfd->flags & BFD_PLUGIN) != 0
   1581 	   && (abfd->flags & BFD_PLUGIN) == 0))
   1582     {
   1583       /* Handle a multiple definition.  */
   1584       (*info->callbacks->multiple_definition) (info, &h->root,
   1585 					       abfd, sec, *pvalue);
   1586       *skip = TRUE;
   1587       return TRUE;
   1588     }
   1589 
   1590   /* If both the old and the new symbols look like common symbols in a
   1591      dynamic object, set the size of the symbol to the larger of the
   1592      two.  */
   1593 
   1594   if (olddyncommon
   1595       && newdyncommon
   1596       && sym->st_size != h->size)
   1597     {
   1598       /* Since we think we have two common symbols, issue a multiple
   1599 	 common warning if desired.  Note that we only warn if the
   1600 	 size is different.  If the size is the same, we simply let
   1601 	 the old symbol override the new one as normally happens with
   1602 	 symbols defined in dynamic objects.  */
   1603 
   1604       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1605 					   bfd_link_hash_common, sym->st_size);
   1606       if (sym->st_size > h->size)
   1607 	h->size = sym->st_size;
   1608 
   1609       *size_change_ok = TRUE;
   1610     }
   1611 
   1612   /* If we are looking at a dynamic object, and we have found a
   1613      definition, we need to see if the symbol was already defined by
   1614      some other object.  If so, we want to use the existing
   1615      definition, and we do not want to report a multiple symbol
   1616      definition error; we do this by clobbering *PSEC to be
   1617      bfd_und_section_ptr.
   1618 
   1619      We treat a common symbol as a definition if the symbol in the
   1620      shared library is a function, since common symbols always
   1621      represent variables; this can cause confusion in principle, but
   1622      any such confusion would seem to indicate an erroneous program or
   1623      shared library.  We also permit a common symbol in a regular
   1624      object to override a weak symbol in a shared object.  */
   1625 
   1626   if (newdyn
   1627       && newdef
   1628       && (olddef
   1629 	  || (h->root.type == bfd_link_hash_common
   1630 	      && (newweak || newfunc))))
   1631     {
   1632       *override = TRUE;
   1633       newdef = FALSE;
   1634       newdyncommon = FALSE;
   1635 
   1636       *psec = sec = bfd_und_section_ptr;
   1637       *size_change_ok = TRUE;
   1638 
   1639       /* If we get here when the old symbol is a common symbol, then
   1640 	 we are explicitly letting it override a weak symbol or
   1641 	 function in a dynamic object, and we don't want to warn about
   1642 	 a type change.  If the old symbol is a defined symbol, a type
   1643 	 change warning may still be appropriate.  */
   1644 
   1645       if (h->root.type == bfd_link_hash_common)
   1646 	*type_change_ok = TRUE;
   1647     }
   1648 
   1649   /* Handle the special case of an old common symbol merging with a
   1650      new symbol which looks like a common symbol in a shared object.
   1651      We change *PSEC and *PVALUE to make the new symbol look like a
   1652      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1653      right thing.  */
   1654 
   1655   if (newdyncommon
   1656       && h->root.type == bfd_link_hash_common)
   1657     {
   1658       *override = TRUE;
   1659       newdef = FALSE;
   1660       newdyncommon = FALSE;
   1661       *pvalue = sym->st_size;
   1662       *psec = sec = bed->common_section (oldsec);
   1663       *size_change_ok = TRUE;
   1664     }
   1665 
   1666   /* Skip weak definitions of symbols that are already defined.  */
   1667   if (newdef && olddef && newweak)
   1668     {
   1669       /* Don't skip new non-IR weak syms.  */
   1670       if (!(oldbfd != NULL
   1671 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1672 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1673 	{
   1674 	  newdef = FALSE;
   1675 	  *skip = TRUE;
   1676 	}
   1677 
   1678       /* Merge st_other.  If the symbol already has a dynamic index,
   1679 	 but visibility says it should not be visible, turn it into a
   1680 	 local symbol.  */
   1681       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
   1682       if (h->dynindx != -1)
   1683 	switch (ELF_ST_VISIBILITY (h->other))
   1684 	  {
   1685 	  case STV_INTERNAL:
   1686 	  case STV_HIDDEN:
   1687 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1688 	    break;
   1689 	  }
   1690     }
   1691 
   1692   /* If the old symbol is from a dynamic object, and the new symbol is
   1693      a definition which is not from a dynamic object, then the new
   1694      symbol overrides the old symbol.  Symbols from regular files
   1695      always take precedence over symbols from dynamic objects, even if
   1696      they are defined after the dynamic object in the link.
   1697 
   1698      As above, we again permit a common symbol in a regular object to
   1699      override a definition in a shared object if the shared object
   1700      symbol is a function or is weak.  */
   1701 
   1702   flip = NULL;
   1703   if (!newdyn
   1704       && (newdef
   1705 	  || (bfd_is_com_section (sec)
   1706 	      && (oldweak || oldfunc)))
   1707       && olddyn
   1708       && olddef
   1709       && h->def_dynamic)
   1710     {
   1711       /* Change the hash table entry to undefined, and let
   1712 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1713 	 new definition.  */
   1714 
   1715       h->root.type = bfd_link_hash_undefined;
   1716       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1717       *size_change_ok = TRUE;
   1718 
   1719       olddef = FALSE;
   1720       olddyncommon = FALSE;
   1721 
   1722       /* We again permit a type change when a common symbol may be
   1723 	 overriding a function.  */
   1724 
   1725       if (bfd_is_com_section (sec))
   1726 	{
   1727 	  if (oldfunc)
   1728 	    {
   1729 	      /* If a common symbol overrides a function, make sure
   1730 		 that it isn't defined dynamically nor has type
   1731 		 function.  */
   1732 	      h->def_dynamic = 0;
   1733 	      h->type = STT_NOTYPE;
   1734 	    }
   1735 	  *type_change_ok = TRUE;
   1736 	}
   1737 
   1738       if (hi->root.type == bfd_link_hash_indirect)
   1739 	flip = hi;
   1740       else
   1741 	/* This union may have been set to be non-NULL when this symbol
   1742 	   was seen in a dynamic object.  We must force the union to be
   1743 	   NULL, so that it is correct for a regular symbol.  */
   1744 	h->verinfo.vertree = NULL;
   1745     }
   1746 
   1747   /* Handle the special case of a new common symbol merging with an
   1748      old symbol that looks like it might be a common symbol defined in
   1749      a shared object.  Note that we have already handled the case in
   1750      which a new common symbol should simply override the definition
   1751      in the shared library.  */
   1752 
   1753   if (! newdyn
   1754       && bfd_is_com_section (sec)
   1755       && olddyncommon)
   1756     {
   1757       /* It would be best if we could set the hash table entry to a
   1758 	 common symbol, but we don't know what to use for the section
   1759 	 or the alignment.  */
   1760       (*info->callbacks->multiple_common) (info, &h->root, abfd,
   1761 					   bfd_link_hash_common, sym->st_size);
   1762 
   1763       /* If the presumed common symbol in the dynamic object is
   1764 	 larger, pretend that the new symbol has its size.  */
   1765 
   1766       if (h->size > *pvalue)
   1767 	*pvalue = h->size;
   1768 
   1769       /* We need to remember the alignment required by the symbol
   1770 	 in the dynamic object.  */
   1771       BFD_ASSERT (pold_alignment);
   1772       *pold_alignment = h->root.u.def.section->alignment_power;
   1773 
   1774       olddef = FALSE;
   1775       olddyncommon = FALSE;
   1776 
   1777       h->root.type = bfd_link_hash_undefined;
   1778       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1779 
   1780       *size_change_ok = TRUE;
   1781       *type_change_ok = TRUE;
   1782 
   1783       if (hi->root.type == bfd_link_hash_indirect)
   1784 	flip = hi;
   1785       else
   1786 	h->verinfo.vertree = NULL;
   1787     }
   1788 
   1789   if (flip != NULL)
   1790     {
   1791       /* Handle the case where we had a versioned symbol in a dynamic
   1792 	 library and now find a definition in a normal object.  In this
   1793 	 case, we make the versioned symbol point to the normal one.  */
   1794       flip->root.type = h->root.type;
   1795       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1796       h->root.type = bfd_link_hash_indirect;
   1797       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1798       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1799       if (h->def_dynamic)
   1800 	{
   1801 	  h->def_dynamic = 0;
   1802 	  flip->ref_dynamic = 1;
   1803 	}
   1804     }
   1805 
   1806   return TRUE;
   1807 }
   1808 
   1809 /* This function is called to create an indirect symbol from the
   1810    default for the symbol with the default version if needed. The
   1811    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1812    set DYNSYM if the new indirect symbol is dynamic.  */
   1813 
   1814 static bfd_boolean
   1815 _bfd_elf_add_default_symbol (bfd *abfd,
   1816 			     struct bfd_link_info *info,
   1817 			     struct elf_link_hash_entry *h,
   1818 			     const char *name,
   1819 			     Elf_Internal_Sym *sym,
   1820 			     asection *sec,
   1821 			     bfd_vma value,
   1822 			     bfd **poldbfd,
   1823 			     bfd_boolean *dynsym)
   1824 {
   1825   bfd_boolean type_change_ok;
   1826   bfd_boolean size_change_ok;
   1827   bfd_boolean skip;
   1828   char *shortname;
   1829   struct elf_link_hash_entry *hi;
   1830   struct bfd_link_hash_entry *bh;
   1831   const struct elf_backend_data *bed;
   1832   bfd_boolean collect;
   1833   bfd_boolean dynamic;
   1834   bfd_boolean override;
   1835   char *p;
   1836   size_t len, shortlen;
   1837   asection *tmp_sec;
   1838   bfd_boolean matched;
   1839 
   1840   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1841     return TRUE;
   1842 
   1843   /* If this symbol has a version, and it is the default version, we
   1844      create an indirect symbol from the default name to the fully
   1845      decorated name.  This will cause external references which do not
   1846      specify a version to be bound to this version of the symbol.  */
   1847   p = strchr (name, ELF_VER_CHR);
   1848   if (h->versioned == unknown)
   1849     {
   1850       if (p == NULL)
   1851 	{
   1852 	  h->versioned = unversioned;
   1853 	  return TRUE;
   1854 	}
   1855       else
   1856 	{
   1857 	  if (p[1] != ELF_VER_CHR)
   1858 	    {
   1859 	      h->versioned = versioned_hidden;
   1860 	      return TRUE;
   1861 	    }
   1862 	  else
   1863 	    h->versioned = versioned;
   1864 	}
   1865     }
   1866   else
   1867     {
   1868       /* PR ld/19073: We may see an unversioned definition after the
   1869 	 default version.  */
   1870       if (p == NULL)
   1871 	return TRUE;
   1872     }
   1873 
   1874   bed = get_elf_backend_data (abfd);
   1875   collect = bed->collect;
   1876   dynamic = (abfd->flags & DYNAMIC) != 0;
   1877 
   1878   shortlen = p - name;
   1879   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   1880   if (shortname == NULL)
   1881     return FALSE;
   1882   memcpy (shortname, name, shortlen);
   1883   shortname[shortlen] = '\0';
   1884 
   1885   /* We are going to create a new symbol.  Merge it with any existing
   1886      symbol with this name.  For the purposes of the merge, act as
   1887      though we were defining the symbol we just defined, although we
   1888      actually going to define an indirect symbol.  */
   1889   type_change_ok = FALSE;
   1890   size_change_ok = FALSE;
   1891   matched = TRUE;
   1892   tmp_sec = sec;
   1893   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1894 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1895 			      &type_change_ok, &size_change_ok, &matched))
   1896     return FALSE;
   1897 
   1898   if (skip)
   1899     goto nondefault;
   1900 
   1901   if (hi->def_regular)
   1902     {
   1903       /* If the undecorated symbol will have a version added by a
   1904 	 script different to H, then don't indirect to/from the
   1905 	 undecorated symbol.  This isn't ideal because we may not yet
   1906 	 have seen symbol versions, if given by a script on the
   1907 	 command line rather than via --version-script.  */
   1908       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   1909 	{
   1910 	  bfd_boolean hide;
   1911 
   1912 	  hi->verinfo.vertree
   1913 	    = bfd_find_version_for_sym (info->version_info,
   1914 					hi->root.root.string, &hide);
   1915 	  if (hi->verinfo.vertree != NULL && hide)
   1916 	    {
   1917 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   1918 	      goto nondefault;
   1919 	    }
   1920 	}
   1921       if (hi->verinfo.vertree != NULL
   1922 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   1923 	goto nondefault;
   1924     }
   1925 
   1926   if (! override)
   1927     {
   1928       /* Add the default symbol if not performing a relocatable link.  */
   1929       if (! bfd_link_relocatable (info))
   1930 	{
   1931 	  bh = &hi->root;
   1932 	  if (! (_bfd_generic_link_add_one_symbol
   1933 		 (info, abfd, shortname, BSF_INDIRECT,
   1934 		  bfd_ind_section_ptr,
   1935 		  0, name, FALSE, collect, &bh)))
   1936 	    return FALSE;
   1937 	  hi = (struct elf_link_hash_entry *) bh;
   1938 	}
   1939     }
   1940   else
   1941     {
   1942       /* In this case the symbol named SHORTNAME is overriding the
   1943 	 indirect symbol we want to add.  We were planning on making
   1944 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   1945 	 is the name without a version.  NAME is the fully versioned
   1946 	 name, and it is the default version.
   1947 
   1948 	 Overriding means that we already saw a definition for the
   1949 	 symbol SHORTNAME in a regular object, and it is overriding
   1950 	 the symbol defined in the dynamic object.
   1951 
   1952 	 When this happens, we actually want to change NAME, the
   1953 	 symbol we just added, to refer to SHORTNAME.  This will cause
   1954 	 references to NAME in the shared object to become references
   1955 	 to SHORTNAME in the regular object.  This is what we expect
   1956 	 when we override a function in a shared object: that the
   1957 	 references in the shared object will be mapped to the
   1958 	 definition in the regular object.  */
   1959 
   1960       while (hi->root.type == bfd_link_hash_indirect
   1961 	     || hi->root.type == bfd_link_hash_warning)
   1962 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1963 
   1964       h->root.type = bfd_link_hash_indirect;
   1965       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1966       if (h->def_dynamic)
   1967 	{
   1968 	  h->def_dynamic = 0;
   1969 	  hi->ref_dynamic = 1;
   1970 	  if (hi->ref_regular
   1971 	      || hi->def_regular)
   1972 	    {
   1973 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   1974 		return FALSE;
   1975 	    }
   1976 	}
   1977 
   1978       /* Now set HI to H, so that the following code will set the
   1979 	 other fields correctly.  */
   1980       hi = h;
   1981     }
   1982 
   1983   /* Check if HI is a warning symbol.  */
   1984   if (hi->root.type == bfd_link_hash_warning)
   1985     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1986 
   1987   /* If there is a duplicate definition somewhere, then HI may not
   1988      point to an indirect symbol.  We will have reported an error to
   1989      the user in that case.  */
   1990 
   1991   if (hi->root.type == bfd_link_hash_indirect)
   1992     {
   1993       struct elf_link_hash_entry *ht;
   1994 
   1995       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1996       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   1997 
   1998       /* A reference to the SHORTNAME symbol from a dynamic library
   1999 	 will be satisfied by the versioned symbol at runtime.  In
   2000 	 effect, we have a reference to the versioned symbol.  */
   2001       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2002       hi->dynamic_def |= ht->dynamic_def;
   2003 
   2004       /* See if the new flags lead us to realize that the symbol must
   2005 	 be dynamic.  */
   2006       if (! *dynsym)
   2007 	{
   2008 	  if (! dynamic)
   2009 	    {
   2010 	      if (! bfd_link_executable (info)
   2011 		  || hi->def_dynamic
   2012 		  || hi->ref_dynamic)
   2013 		*dynsym = TRUE;
   2014 	    }
   2015 	  else
   2016 	    {
   2017 	      if (hi->ref_regular)
   2018 		*dynsym = TRUE;
   2019 	    }
   2020 	}
   2021     }
   2022 
   2023   /* We also need to define an indirection from the nondefault version
   2024      of the symbol.  */
   2025 
   2026 nondefault:
   2027   len = strlen (name);
   2028   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   2029   if (shortname == NULL)
   2030     return FALSE;
   2031   memcpy (shortname, name, shortlen);
   2032   memcpy (shortname + shortlen, p + 1, len - shortlen);
   2033 
   2034   /* Once again, merge with any existing symbol.  */
   2035   type_change_ok = FALSE;
   2036   size_change_ok = FALSE;
   2037   tmp_sec = sec;
   2038   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   2039 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   2040 			      &type_change_ok, &size_change_ok, &matched))
   2041     return FALSE;
   2042 
   2043   if (skip)
   2044     return TRUE;
   2045 
   2046   if (override)
   2047     {
   2048       /* Here SHORTNAME is a versioned name, so we don't expect to see
   2049 	 the type of override we do in the case above unless it is
   2050 	 overridden by a versioned definition.  */
   2051       if (hi->root.type != bfd_link_hash_defined
   2052 	  && hi->root.type != bfd_link_hash_defweak)
   2053 	_bfd_error_handler
   2054 	  /* xgettext:c-format */
   2055 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
   2056 	   abfd, shortname);
   2057     }
   2058   else
   2059     {
   2060       bh = &hi->root;
   2061       if (! (_bfd_generic_link_add_one_symbol
   2062 	     (info, abfd, shortname, BSF_INDIRECT,
   2063 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
   2064 	return FALSE;
   2065       hi = (struct elf_link_hash_entry *) bh;
   2066 
   2067       /* If there is a duplicate definition somewhere, then HI may not
   2068 	 point to an indirect symbol.  We will have reported an error
   2069 	 to the user in that case.  */
   2070 
   2071       if (hi->root.type == bfd_link_hash_indirect)
   2072 	{
   2073 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   2074 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   2075 	  hi->dynamic_def |= h->dynamic_def;
   2076 
   2077 	  /* See if the new flags lead us to realize that the symbol
   2078 	     must be dynamic.  */
   2079 	  if (! *dynsym)
   2080 	    {
   2081 	      if (! dynamic)
   2082 		{
   2083 		  if (! bfd_link_executable (info)
   2084 		      || hi->ref_dynamic)
   2085 		    *dynsym = TRUE;
   2086 		}
   2087 	      else
   2088 		{
   2089 		  if (hi->ref_regular)
   2090 		    *dynsym = TRUE;
   2091 		}
   2092 	    }
   2093 	}
   2094     }
   2095 
   2096   return TRUE;
   2097 }
   2098 
   2099 /* This routine is used to export all defined symbols into the dynamic
   2101    symbol table.  It is called via elf_link_hash_traverse.  */
   2102 
   2103 static bfd_boolean
   2104 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   2105 {
   2106   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2107 
   2108   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2109   if (h->root.type == bfd_link_hash_indirect)
   2110     return TRUE;
   2111 
   2112   /* Ignore this if we won't export it.  */
   2113   if (!eif->info->export_dynamic && !h->dynamic)
   2114     return TRUE;
   2115 
   2116   if (h->dynindx == -1
   2117       && (h->def_regular || h->ref_regular)
   2118       && ! bfd_hide_sym_by_version (eif->info->version_info,
   2119 				    h->root.root.string))
   2120     {
   2121       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2122 	{
   2123 	  eif->failed = TRUE;
   2124 	  return FALSE;
   2125 	}
   2126     }
   2127 
   2128   return TRUE;
   2129 }
   2130 
   2131 /* Look through the symbols which are defined in other shared
   2133    libraries and referenced here.  Update the list of version
   2134    dependencies.  This will be put into the .gnu.version_r section.
   2135    This function is called via elf_link_hash_traverse.  */
   2136 
   2137 static bfd_boolean
   2138 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2139 					 void *data)
   2140 {
   2141   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2142   Elf_Internal_Verneed *t;
   2143   Elf_Internal_Vernaux *a;
   2144   bfd_size_type amt;
   2145 
   2146   /* We only care about symbols defined in shared objects with version
   2147      information.  */
   2148   if (!h->def_dynamic
   2149       || h->def_regular
   2150       || h->dynindx == -1
   2151       || h->verinfo.verdef == NULL
   2152       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2153 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2154     return TRUE;
   2155 
   2156   /* See if we already know about this version.  */
   2157   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2158        t != NULL;
   2159        t = t->vn_nextref)
   2160     {
   2161       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2162 	continue;
   2163 
   2164       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2165 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2166 	  return TRUE;
   2167 
   2168       break;
   2169     }
   2170 
   2171   /* This is a new version.  Add it to tree we are building.  */
   2172 
   2173   if (t == NULL)
   2174     {
   2175       amt = sizeof *t;
   2176       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2177       if (t == NULL)
   2178 	{
   2179 	  rinfo->failed = TRUE;
   2180 	  return FALSE;
   2181 	}
   2182 
   2183       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2184       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2185       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2186     }
   2187 
   2188   amt = sizeof *a;
   2189   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2190   if (a == NULL)
   2191     {
   2192       rinfo->failed = TRUE;
   2193       return FALSE;
   2194     }
   2195 
   2196   /* Note that we are copying a string pointer here, and testing it
   2197      above.  If bfd_elf_string_from_elf_section is ever changed to
   2198      discard the string data when low in memory, this will have to be
   2199      fixed.  */
   2200   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2201 
   2202   a->vna_flags = h->verinfo.verdef->vd_flags;
   2203   a->vna_nextptr = t->vn_auxptr;
   2204 
   2205   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2206   ++rinfo->vers;
   2207 
   2208   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2209 
   2210   t->vn_auxptr = a;
   2211 
   2212   return TRUE;
   2213 }
   2214 
   2215 /* Figure out appropriate versions for all the symbols.  We may not
   2216    have the version number script until we have read all of the input
   2217    files, so until that point we don't know which symbols should be
   2218    local.  This function is called via elf_link_hash_traverse.  */
   2219 
   2220 static bfd_boolean
   2221 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2222 {
   2223   struct elf_info_failed *sinfo;
   2224   struct bfd_link_info *info;
   2225   const struct elf_backend_data *bed;
   2226   struct elf_info_failed eif;
   2227   char *p;
   2228 
   2229   sinfo = (struct elf_info_failed *) data;
   2230   info = sinfo->info;
   2231 
   2232   /* Fix the symbol flags.  */
   2233   eif.failed = FALSE;
   2234   eif.info = info;
   2235   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2236     {
   2237       if (eif.failed)
   2238 	sinfo->failed = TRUE;
   2239       return FALSE;
   2240     }
   2241 
   2242   /* We only need version numbers for symbols defined in regular
   2243      objects.  */
   2244   if (!h->def_regular)
   2245     return TRUE;
   2246 
   2247   bed = get_elf_backend_data (info->output_bfd);
   2248   p = strchr (h->root.root.string, ELF_VER_CHR);
   2249   if (p != NULL && h->verinfo.vertree == NULL)
   2250     {
   2251       struct bfd_elf_version_tree *t;
   2252 
   2253       ++p;
   2254       if (*p == ELF_VER_CHR)
   2255 	++p;
   2256 
   2257       /* If there is no version string, we can just return out.  */
   2258       if (*p == '\0')
   2259 	return TRUE;
   2260 
   2261       /* Look for the version.  If we find it, it is no longer weak.  */
   2262       for (t = sinfo->info->version_info; t != NULL; t = t->next)
   2263 	{
   2264 	  if (strcmp (t->name, p) == 0)
   2265 	    {
   2266 	      size_t len;
   2267 	      char *alc;
   2268 	      struct bfd_elf_version_expr *d;
   2269 
   2270 	      len = p - h->root.root.string;
   2271 	      alc = (char *) bfd_malloc (len);
   2272 	      if (alc == NULL)
   2273 		{
   2274 		  sinfo->failed = TRUE;
   2275 		  return FALSE;
   2276 		}
   2277 	      memcpy (alc, h->root.root.string, len - 1);
   2278 	      alc[len - 1] = '\0';
   2279 	      if (alc[len - 2] == ELF_VER_CHR)
   2280 		alc[len - 2] = '\0';
   2281 
   2282 	      h->verinfo.vertree = t;
   2283 	      t->used = TRUE;
   2284 	      d = NULL;
   2285 
   2286 	      if (t->globals.list != NULL)
   2287 		d = (*t->match) (&t->globals, NULL, alc);
   2288 
   2289 	      /* See if there is anything to force this symbol to
   2290 		 local scope.  */
   2291 	      if (d == NULL && t->locals.list != NULL)
   2292 		{
   2293 		  d = (*t->match) (&t->locals, NULL, alc);
   2294 		  if (d != NULL
   2295 		      && h->dynindx != -1
   2296 		      && ! info->export_dynamic)
   2297 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2298 		}
   2299 
   2300 	      free (alc);
   2301 	      break;
   2302 	    }
   2303 	}
   2304 
   2305       /* If we are building an application, we need to create a
   2306 	 version node for this version.  */
   2307       if (t == NULL && bfd_link_executable (info))
   2308 	{
   2309 	  struct bfd_elf_version_tree **pp;
   2310 	  int version_index;
   2311 
   2312 	  /* If we aren't going to export this symbol, we don't need
   2313 	     to worry about it.  */
   2314 	  if (h->dynindx == -1)
   2315 	    return TRUE;
   2316 
   2317 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
   2318 							  sizeof *t);
   2319 	  if (t == NULL)
   2320 	    {
   2321 	      sinfo->failed = TRUE;
   2322 	      return FALSE;
   2323 	    }
   2324 
   2325 	  t->name = p;
   2326 	  t->name_indx = (unsigned int) -1;
   2327 	  t->used = TRUE;
   2328 
   2329 	  version_index = 1;
   2330 	  /* Don't count anonymous version tag.  */
   2331 	  if (sinfo->info->version_info != NULL
   2332 	      && sinfo->info->version_info->vernum == 0)
   2333 	    version_index = 0;
   2334 	  for (pp = &sinfo->info->version_info;
   2335 	       *pp != NULL;
   2336 	       pp = &(*pp)->next)
   2337 	    ++version_index;
   2338 	  t->vernum = version_index;
   2339 
   2340 	  *pp = t;
   2341 
   2342 	  h->verinfo.vertree = t;
   2343 	}
   2344       else if (t == NULL)
   2345 	{
   2346 	  /* We could not find the version for a symbol when
   2347 	     generating a shared archive.  Return an error.  */
   2348 	  _bfd_error_handler
   2349 	    /* xgettext:c-format */
   2350 	    (_("%B: version node not found for symbol %s"),
   2351 	     info->output_bfd, h->root.root.string);
   2352 	  bfd_set_error (bfd_error_bad_value);
   2353 	  sinfo->failed = TRUE;
   2354 	  return FALSE;
   2355 	}
   2356     }
   2357 
   2358   /* If we don't have a version for this symbol, see if we can find
   2359      something.  */
   2360   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
   2361     {
   2362       bfd_boolean hide;
   2363 
   2364       h->verinfo.vertree
   2365 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2366 				    h->root.root.string, &hide);
   2367       if (h->verinfo.vertree != NULL && hide)
   2368 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2369     }
   2370 
   2371   return TRUE;
   2372 }
   2373 
   2374 /* Read and swap the relocs from the section indicated by SHDR.  This
   2376    may be either a REL or a RELA section.  The relocations are
   2377    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2378    which should have already been allocated to contain enough space.
   2379    The EXTERNAL_RELOCS are a buffer where the external form of the
   2380    relocations should be stored.
   2381 
   2382    Returns FALSE if something goes wrong.  */
   2383 
   2384 static bfd_boolean
   2385 elf_link_read_relocs_from_section (bfd *abfd,
   2386 				   asection *sec,
   2387 				   Elf_Internal_Shdr *shdr,
   2388 				   void *external_relocs,
   2389 				   Elf_Internal_Rela *internal_relocs)
   2390 {
   2391   const struct elf_backend_data *bed;
   2392   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2393   const bfd_byte *erela;
   2394   const bfd_byte *erelaend;
   2395   Elf_Internal_Rela *irela;
   2396   Elf_Internal_Shdr *symtab_hdr;
   2397   size_t nsyms;
   2398 
   2399   /* Position ourselves at the start of the section.  */
   2400   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2401     return FALSE;
   2402 
   2403   /* Read the relocations.  */
   2404   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
   2405     return FALSE;
   2406 
   2407   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2408   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2409 
   2410   bed = get_elf_backend_data (abfd);
   2411 
   2412   /* Convert the external relocations to the internal format.  */
   2413   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2414     swap_in = bed->s->swap_reloc_in;
   2415   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2416     swap_in = bed->s->swap_reloca_in;
   2417   else
   2418     {
   2419       bfd_set_error (bfd_error_wrong_format);
   2420       return FALSE;
   2421     }
   2422 
   2423   erela = (const bfd_byte *) external_relocs;
   2424   erelaend = erela + shdr->sh_size;
   2425   irela = internal_relocs;
   2426   while (erela < erelaend)
   2427     {
   2428       bfd_vma r_symndx;
   2429 
   2430       (*swap_in) (abfd, erela, irela);
   2431       r_symndx = ELF32_R_SYM (irela->r_info);
   2432       if (bed->s->arch_size == 64)
   2433 	r_symndx >>= 24;
   2434       if (nsyms > 0)
   2435 	{
   2436 	  if ((size_t) r_symndx >= nsyms)
   2437 	    {
   2438 	      _bfd_error_handler
   2439 		/* xgettext:c-format */
   2440 		(_("%B: bad reloc symbol index (%#Lx >= %#lx)"
   2441 		   " for offset %#Lx in section `%A'"),
   2442 		 abfd, r_symndx, (unsigned long) nsyms,
   2443 		 irela->r_offset, sec);
   2444 	      bfd_set_error (bfd_error_bad_value);
   2445 	      return FALSE;
   2446 	    }
   2447 	}
   2448       else if (r_symndx != STN_UNDEF)
   2449 	{
   2450 	  _bfd_error_handler
   2451 	    /* xgettext:c-format */
   2452 	    (_("%B: non-zero symbol index (%#Lx)"
   2453 	       " for offset %#Lx in section `%A'"
   2454 	       " when the object file has no symbol table"),
   2455 	     abfd, r_symndx,
   2456 	     irela->r_offset, sec);
   2457 	  bfd_set_error (bfd_error_bad_value);
   2458 	  return FALSE;
   2459 	}
   2460       irela += bed->s->int_rels_per_ext_rel;
   2461       erela += shdr->sh_entsize;
   2462     }
   2463 
   2464   return TRUE;
   2465 }
   2466 
   2467 /* Read and swap the relocs for a section O.  They may have been
   2468    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2469    not NULL, they are used as buffers to read into.  They are known to
   2470    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2471    the return value is allocated using either malloc or bfd_alloc,
   2472    according to the KEEP_MEMORY argument.  If O has two relocation
   2473    sections (both REL and RELA relocations), then the REL_HDR
   2474    relocations will appear first in INTERNAL_RELOCS, followed by the
   2475    RELA_HDR relocations.  */
   2476 
   2477 Elf_Internal_Rela *
   2478 _bfd_elf_link_read_relocs (bfd *abfd,
   2479 			   asection *o,
   2480 			   void *external_relocs,
   2481 			   Elf_Internal_Rela *internal_relocs,
   2482 			   bfd_boolean keep_memory)
   2483 {
   2484   void *alloc1 = NULL;
   2485   Elf_Internal_Rela *alloc2 = NULL;
   2486   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2487   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2488   Elf_Internal_Rela *internal_rela_relocs;
   2489 
   2490   if (esdo->relocs != NULL)
   2491     return esdo->relocs;
   2492 
   2493   if (o->reloc_count == 0)
   2494     return NULL;
   2495 
   2496   if (internal_relocs == NULL)
   2497     {
   2498       bfd_size_type size;
   2499 
   2500       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
   2501       if (keep_memory)
   2502 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
   2503       else
   2504 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2505       if (internal_relocs == NULL)
   2506 	goto error_return;
   2507     }
   2508 
   2509   if (external_relocs == NULL)
   2510     {
   2511       bfd_size_type size = 0;
   2512 
   2513       if (esdo->rel.hdr)
   2514 	size += esdo->rel.hdr->sh_size;
   2515       if (esdo->rela.hdr)
   2516 	size += esdo->rela.hdr->sh_size;
   2517 
   2518       alloc1 = bfd_malloc (size);
   2519       if (alloc1 == NULL)
   2520 	goto error_return;
   2521       external_relocs = alloc1;
   2522     }
   2523 
   2524   internal_rela_relocs = internal_relocs;
   2525   if (esdo->rel.hdr)
   2526     {
   2527       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2528 					      external_relocs,
   2529 					      internal_relocs))
   2530 	goto error_return;
   2531       external_relocs = (((bfd_byte *) external_relocs)
   2532 			 + esdo->rel.hdr->sh_size);
   2533       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2534 			       * bed->s->int_rels_per_ext_rel);
   2535     }
   2536 
   2537   if (esdo->rela.hdr
   2538       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2539 					      external_relocs,
   2540 					      internal_rela_relocs)))
   2541     goto error_return;
   2542 
   2543   /* Cache the results for next time, if we can.  */
   2544   if (keep_memory)
   2545     esdo->relocs = internal_relocs;
   2546 
   2547   if (alloc1 != NULL)
   2548     free (alloc1);
   2549 
   2550   /* Don't free alloc2, since if it was allocated we are passing it
   2551      back (under the name of internal_relocs).  */
   2552 
   2553   return internal_relocs;
   2554 
   2555  error_return:
   2556   if (alloc1 != NULL)
   2557     free (alloc1);
   2558   if (alloc2 != NULL)
   2559     {
   2560       if (keep_memory)
   2561 	bfd_release (abfd, alloc2);
   2562       else
   2563 	free (alloc2);
   2564     }
   2565   return NULL;
   2566 }
   2567 
   2568 /* Compute the size of, and allocate space for, REL_HDR which is the
   2569    section header for a section containing relocations for O.  */
   2570 
   2571 static bfd_boolean
   2572 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2573 				  struct bfd_elf_section_reloc_data *reldata)
   2574 {
   2575   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2576 
   2577   /* That allows us to calculate the size of the section.  */
   2578   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2579 
   2580   /* The contents field must last into write_object_contents, so we
   2581      allocate it with bfd_alloc rather than malloc.  Also since we
   2582      cannot be sure that the contents will actually be filled in,
   2583      we zero the allocated space.  */
   2584   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2585   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2586     return FALSE;
   2587 
   2588   if (reldata->hashes == NULL && reldata->count)
   2589     {
   2590       struct elf_link_hash_entry **p;
   2591 
   2592       p = ((struct elf_link_hash_entry **)
   2593 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   2594       if (p == NULL)
   2595 	return FALSE;
   2596 
   2597       reldata->hashes = p;
   2598     }
   2599 
   2600   return TRUE;
   2601 }
   2602 
   2603 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2604    originated from the section given by INPUT_REL_HDR) to the
   2605    OUTPUT_BFD.  */
   2606 
   2607 bfd_boolean
   2608 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2609 			     asection *input_section,
   2610 			     Elf_Internal_Shdr *input_rel_hdr,
   2611 			     Elf_Internal_Rela *internal_relocs,
   2612 			     struct elf_link_hash_entry **rel_hash
   2613 			       ATTRIBUTE_UNUSED)
   2614 {
   2615   Elf_Internal_Rela *irela;
   2616   Elf_Internal_Rela *irelaend;
   2617   bfd_byte *erel;
   2618   struct bfd_elf_section_reloc_data *output_reldata;
   2619   asection *output_section;
   2620   const struct elf_backend_data *bed;
   2621   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   2622   struct bfd_elf_section_data *esdo;
   2623 
   2624   output_section = input_section->output_section;
   2625 
   2626   bed = get_elf_backend_data (output_bfd);
   2627   esdo = elf_section_data (output_section);
   2628   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2629     {
   2630       output_reldata = &esdo->rel;
   2631       swap_out = bed->s->swap_reloc_out;
   2632     }
   2633   else if (esdo->rela.hdr
   2634 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2635     {
   2636       output_reldata = &esdo->rela;
   2637       swap_out = bed->s->swap_reloca_out;
   2638     }
   2639   else
   2640     {
   2641       _bfd_error_handler
   2642 	/* xgettext:c-format */
   2643 	(_("%B: relocation size mismatch in %B section %A"),
   2644 	 output_bfd, input_section->owner, input_section);
   2645       bfd_set_error (bfd_error_wrong_format);
   2646       return FALSE;
   2647     }
   2648 
   2649   erel = output_reldata->hdr->contents;
   2650   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   2651   irela = internal_relocs;
   2652   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   2653 		      * bed->s->int_rels_per_ext_rel);
   2654   while (irela < irelaend)
   2655     {
   2656       (*swap_out) (output_bfd, irela, erel);
   2657       irela += bed->s->int_rels_per_ext_rel;
   2658       erel += input_rel_hdr->sh_entsize;
   2659     }
   2660 
   2661   /* Bump the counter, so that we know where to add the next set of
   2662      relocations.  */
   2663   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   2664 
   2665   return TRUE;
   2666 }
   2667 
   2668 /* Make weak undefined symbols in PIE dynamic.  */
   2670 
   2671 bfd_boolean
   2672 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   2673 				 struct elf_link_hash_entry *h)
   2674 {
   2675   if (bfd_link_pie (info)
   2676       && h->dynindx == -1
   2677       && h->root.type == bfd_link_hash_undefweak)
   2678     return bfd_elf_link_record_dynamic_symbol (info, h);
   2679 
   2680   return TRUE;
   2681 }
   2682 
   2683 /* Fix up the flags for a symbol.  This handles various cases which
   2684    can only be fixed after all the input files are seen.  This is
   2685    currently called by both adjust_dynamic_symbol and
   2686    assign_sym_version, which is unnecessary but perhaps more robust in
   2687    the face of future changes.  */
   2688 
   2689 static bfd_boolean
   2690 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   2691 			   struct elf_info_failed *eif)
   2692 {
   2693   const struct elf_backend_data *bed;
   2694 
   2695   /* If this symbol was mentioned in a non-ELF file, try to set
   2696      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   2697      permit a non-ELF file to correctly refer to a symbol defined in
   2698      an ELF dynamic object.  */
   2699   if (h->non_elf)
   2700     {
   2701       while (h->root.type == bfd_link_hash_indirect)
   2702 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2703 
   2704       if (h->root.type != bfd_link_hash_defined
   2705 	  && h->root.type != bfd_link_hash_defweak)
   2706 	{
   2707 	  h->ref_regular = 1;
   2708 	  h->ref_regular_nonweak = 1;
   2709 	}
   2710       else
   2711 	{
   2712 	  if (h->root.u.def.section->owner != NULL
   2713 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   2714 		  == bfd_target_elf_flavour))
   2715 	    {
   2716 	      h->ref_regular = 1;
   2717 	      h->ref_regular_nonweak = 1;
   2718 	    }
   2719 	  else
   2720 	    h->def_regular = 1;
   2721 	}
   2722 
   2723       if (h->dynindx == -1
   2724 	  && (h->def_dynamic
   2725 	      || h->ref_dynamic))
   2726 	{
   2727 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2728 	    {
   2729 	      eif->failed = TRUE;
   2730 	      return FALSE;
   2731 	    }
   2732 	}
   2733     }
   2734   else
   2735     {
   2736       /* Unfortunately, NON_ELF is only correct if the symbol
   2737 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   2738 	 was first seen in an ELF file, we're probably OK unless the
   2739 	 symbol was defined in a non-ELF file.  Catch that case here.
   2740 	 FIXME: We're still in trouble if the symbol was first seen in
   2741 	 a dynamic object, and then later in a non-ELF regular object.  */
   2742       if ((h->root.type == bfd_link_hash_defined
   2743 	   || h->root.type == bfd_link_hash_defweak)
   2744 	  && !h->def_regular
   2745 	  && (h->root.u.def.section->owner != NULL
   2746 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   2747 		 != bfd_target_elf_flavour)
   2748 	      : (bfd_is_abs_section (h->root.u.def.section)
   2749 		 && !h->def_dynamic)))
   2750 	h->def_regular = 1;
   2751     }
   2752 
   2753   /* Backend specific symbol fixup.  */
   2754   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   2755   if (bed->elf_backend_fixup_symbol
   2756       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   2757     return FALSE;
   2758 
   2759   /* If this is a final link, and the symbol was defined as a common
   2760      symbol in a regular object file, and there was no definition in
   2761      any dynamic object, then the linker will have allocated space for
   2762      the symbol in a common section but the DEF_REGULAR
   2763      flag will not have been set.  */
   2764   if (h->root.type == bfd_link_hash_defined
   2765       && !h->def_regular
   2766       && h->ref_regular
   2767       && !h->def_dynamic
   2768       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   2769     h->def_regular = 1;
   2770 
   2771   /* If a weak undefined symbol has non-default visibility, we also
   2772      hide it from the dynamic linker.  */
   2773   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2774       && h->root.type == bfd_link_hash_undefweak)
   2775     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2776 
   2777   /* A hidden versioned symbol in executable should be forced local if
   2778      it is is locally defined, not referenced by shared library and not
   2779      exported.  */
   2780   else if (bfd_link_executable (eif->info)
   2781 	   && h->versioned == versioned_hidden
   2782 	   && !eif->info->export_dynamic
   2783 	   && !h->dynamic
   2784 	   && !h->ref_dynamic
   2785 	   && h->def_regular)
   2786     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2787 
   2788   /* If -Bsymbolic was used (which means to bind references to global
   2789      symbols to the definition within the shared object), and this
   2790      symbol was defined in a regular object, then it actually doesn't
   2791      need a PLT entry.  Likewise, if the symbol has non-default
   2792      visibility.  If the symbol has hidden or internal visibility, we
   2793      will force it local.  */
   2794   else if (h->needs_plt
   2795 	   && bfd_link_pic (eif->info)
   2796 	   && is_elf_hash_table (eif->info->hash)
   2797 	   && (SYMBOLIC_BIND (eif->info, h)
   2798 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2799 	   && h->def_regular)
   2800     {
   2801       bfd_boolean force_local;
   2802 
   2803       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   2804 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   2805       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   2806     }
   2807 
   2808   /* If this is a weak defined symbol in a dynamic object, and we know
   2809      the real definition in the dynamic object, copy interesting flags
   2810      over to the real definition.  */
   2811   if (h->is_weakalias)
   2812     {
   2813       struct elf_link_hash_entry *def = weakdef (h);
   2814 
   2815       /* If the real definition is defined by a regular object file,
   2816 	 don't do anything special.  See the longer description in
   2817 	 _bfd_elf_adjust_dynamic_symbol, below.  */
   2818       if (def->def_regular)
   2819 	{
   2820 	  h = def;
   2821 	  while ((h = h->u.alias) != def)
   2822 	    h->is_weakalias = 0;
   2823 	}
   2824       else
   2825 	{
   2826 	  while (h->root.type == bfd_link_hash_indirect)
   2827 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2828 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   2829 		      || h->root.type == bfd_link_hash_defweak);
   2830 	  BFD_ASSERT (def->def_dynamic);
   2831 	  BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   2832 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
   2833 	}
   2834     }
   2835 
   2836   return TRUE;
   2837 }
   2838 
   2839 /* Make the backend pick a good value for a dynamic symbol.  This is
   2840    called via elf_link_hash_traverse, and also calls itself
   2841    recursively.  */
   2842 
   2843 static bfd_boolean
   2844 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   2845 {
   2846   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2847   struct elf_link_hash_table *htab;
   2848   const struct elf_backend_data *bed;
   2849 
   2850   if (! is_elf_hash_table (eif->info->hash))
   2851     return FALSE;
   2852 
   2853   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2854   if (h->root.type == bfd_link_hash_indirect)
   2855     return TRUE;
   2856 
   2857   /* Fix the symbol flags.  */
   2858   if (! _bfd_elf_fix_symbol_flags (h, eif))
   2859     return FALSE;
   2860 
   2861   htab = elf_hash_table (eif->info);
   2862   bed = get_elf_backend_data (htab->dynobj);
   2863 
   2864   if (h->root.type == bfd_link_hash_undefweak)
   2865     {
   2866       if (eif->info->dynamic_undefined_weak == 0)
   2867 	(*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2868       else if (eif->info->dynamic_undefined_weak > 0
   2869 	       && h->ref_regular
   2870 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2871 	       && !bfd_hide_sym_by_version (eif->info->version_info,
   2872 					    h->root.root.string))
   2873 	{
   2874 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2875 	    {
   2876 	      eif->failed = TRUE;
   2877 	      return FALSE;
   2878 	    }
   2879 	}
   2880     }
   2881 
   2882   /* If this symbol does not require a PLT entry, and it is not
   2883      defined by a dynamic object, or is not referenced by a regular
   2884      object, ignore it.  We do have to handle a weak defined symbol,
   2885      even if no regular object refers to it, if we decided to add it
   2886      to the dynamic symbol table.  FIXME: Do we normally need to worry
   2887      about symbols which are defined by one dynamic object and
   2888      referenced by another one?  */
   2889   if (!h->needs_plt
   2890       && h->type != STT_GNU_IFUNC
   2891       && (h->def_regular
   2892 	  || !h->def_dynamic
   2893 	  || (!h->ref_regular
   2894 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
   2895     {
   2896       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   2897       return TRUE;
   2898     }
   2899 
   2900   /* If we've already adjusted this symbol, don't do it again.  This
   2901      can happen via a recursive call.  */
   2902   if (h->dynamic_adjusted)
   2903     return TRUE;
   2904 
   2905   /* Don't look at this symbol again.  Note that we must set this
   2906      after checking the above conditions, because we may look at a
   2907      symbol once, decide not to do anything, and then get called
   2908      recursively later after REF_REGULAR is set below.  */
   2909   h->dynamic_adjusted = 1;
   2910 
   2911   /* If this is a weak definition, and we know a real definition, and
   2912      the real symbol is not itself defined by a regular object file,
   2913      then get a good value for the real definition.  We handle the
   2914      real symbol first, for the convenience of the backend routine.
   2915 
   2916      Note that there is a confusing case here.  If the real definition
   2917      is defined by a regular object file, we don't get the real symbol
   2918      from the dynamic object, but we do get the weak symbol.  If the
   2919      processor backend uses a COPY reloc, then if some routine in the
   2920      dynamic object changes the real symbol, we will not see that
   2921      change in the corresponding weak symbol.  This is the way other
   2922      ELF linkers work as well, and seems to be a result of the shared
   2923      library model.
   2924 
   2925      I will clarify this issue.  Most SVR4 shared libraries define the
   2926      variable _timezone and define timezone as a weak synonym.  The
   2927      tzset call changes _timezone.  If you write
   2928        extern int timezone;
   2929        int _timezone = 5;
   2930        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   2931      you might expect that, since timezone is a synonym for _timezone,
   2932      the same number will print both times.  However, if the processor
   2933      backend uses a COPY reloc, then actually timezone will be copied
   2934      into your process image, and, since you define _timezone
   2935      yourself, _timezone will not.  Thus timezone and _timezone will
   2936      wind up at different memory locations.  The tzset call will set
   2937      _timezone, leaving timezone unchanged.  */
   2938 
   2939   if (h->is_weakalias)
   2940     {
   2941       struct elf_link_hash_entry *def = weakdef (h);
   2942 
   2943       /* If we get to this point, there is an implicit reference to
   2944 	 the alias by a regular object file via the weak symbol H.  */
   2945       def->ref_regular = 1;
   2946 
   2947       /* Ensure that the backend adjust_dynamic_symbol function sees
   2948 	 the strong alias before H by recursively calling ourselves.  */
   2949       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
   2950 	return FALSE;
   2951     }
   2952 
   2953   /* If a symbol has no type and no size and does not require a PLT
   2954      entry, then we are probably about to do the wrong thing here: we
   2955      are probably going to create a COPY reloc for an empty object.
   2956      This case can arise when a shared object is built with assembly
   2957      code, and the assembly code fails to set the symbol type.  */
   2958   if (h->size == 0
   2959       && h->type == STT_NOTYPE
   2960       && !h->needs_plt)
   2961     _bfd_error_handler
   2962       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   2963        h->root.root.string);
   2964 
   2965   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   2966     {
   2967       eif->failed = TRUE;
   2968       return FALSE;
   2969     }
   2970 
   2971   return TRUE;
   2972 }
   2973 
   2974 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   2975    DYNBSS.  */
   2976 
   2977 bfd_boolean
   2978 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   2979 			      struct elf_link_hash_entry *h,
   2980 			      asection *dynbss)
   2981 {
   2982   unsigned int power_of_two;
   2983   bfd_vma mask;
   2984   asection *sec = h->root.u.def.section;
   2985 
   2986   /* The section alignment of the definition is the maximum alignment
   2987      requirement of symbols defined in the section.  Since we don't
   2988      know the symbol alignment requirement, we start with the
   2989      maximum alignment and check low bits of the symbol address
   2990      for the minimum alignment.  */
   2991   power_of_two = bfd_get_section_alignment (sec->owner, sec);
   2992   mask = ((bfd_vma) 1 << power_of_two) - 1;
   2993   while ((h->root.u.def.value & mask) != 0)
   2994     {
   2995        mask >>= 1;
   2996        --power_of_two;
   2997     }
   2998 
   2999   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
   3000 						dynbss))
   3001     {
   3002       /* Adjust the section alignment if needed.  */
   3003       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
   3004 				       power_of_two))
   3005 	return FALSE;
   3006     }
   3007 
   3008   /* We make sure that the symbol will be aligned properly.  */
   3009   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   3010 
   3011   /* Define the symbol as being at this point in DYNBSS.  */
   3012   h->root.u.def.section = dynbss;
   3013   h->root.u.def.value = dynbss->size;
   3014 
   3015   /* Increment the size of DYNBSS to make room for the symbol.  */
   3016   dynbss->size += h->size;
   3017 
   3018   /* No error if extern_protected_data is true.  */
   3019   if (h->protected_def
   3020       && (!info->extern_protected_data
   3021 	  || (info->extern_protected_data < 0
   3022 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   3023     info->callbacks->einfo
   3024       (_("%P: copy reloc against protected `%T' is dangerous\n"),
   3025        h->root.root.string);
   3026 
   3027   return TRUE;
   3028 }
   3029 
   3030 /* Adjust all external symbols pointing into SEC_MERGE sections
   3031    to reflect the object merging within the sections.  */
   3032 
   3033 static bfd_boolean
   3034 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   3035 {
   3036   asection *sec;
   3037 
   3038   if ((h->root.type == bfd_link_hash_defined
   3039        || h->root.type == bfd_link_hash_defweak)
   3040       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   3041       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3042     {
   3043       bfd *output_bfd = (bfd *) data;
   3044 
   3045       h->root.u.def.value =
   3046 	_bfd_merged_section_offset (output_bfd,
   3047 				    &h->root.u.def.section,
   3048 				    elf_section_data (sec)->sec_info,
   3049 				    h->root.u.def.value);
   3050     }
   3051 
   3052   return TRUE;
   3053 }
   3054 
   3055 /* Returns false if the symbol referred to by H should be considered
   3056    to resolve local to the current module, and true if it should be
   3057    considered to bind dynamically.  */
   3058 
   3059 bfd_boolean
   3060 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   3061 			   struct bfd_link_info *info,
   3062 			   bfd_boolean not_local_protected)
   3063 {
   3064   bfd_boolean binding_stays_local_p;
   3065   const struct elf_backend_data *bed;
   3066   struct elf_link_hash_table *hash_table;
   3067 
   3068   if (h == NULL)
   3069     return FALSE;
   3070 
   3071   while (h->root.type == bfd_link_hash_indirect
   3072 	 || h->root.type == bfd_link_hash_warning)
   3073     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3074 
   3075   /* If it was forced local, then clearly it's not dynamic.  */
   3076   if (h->dynindx == -1)
   3077     return FALSE;
   3078   if (h->forced_local)
   3079     return FALSE;
   3080 
   3081   /* Identify the cases where name binding rules say that a
   3082      visible symbol resolves locally.  */
   3083   binding_stays_local_p = (bfd_link_executable (info)
   3084 			   || SYMBOLIC_BIND (info, h));
   3085 
   3086   switch (ELF_ST_VISIBILITY (h->other))
   3087     {
   3088     case STV_INTERNAL:
   3089     case STV_HIDDEN:
   3090       return FALSE;
   3091 
   3092     case STV_PROTECTED:
   3093       hash_table = elf_hash_table (info);
   3094       if (!is_elf_hash_table (hash_table))
   3095 	return FALSE;
   3096 
   3097       bed = get_elf_backend_data (hash_table->dynobj);
   3098 
   3099       /* Proper resolution for function pointer equality may require
   3100 	 that these symbols perhaps be resolved dynamically, even though
   3101 	 we should be resolving them to the current module.  */
   3102       if (!not_local_protected || !bed->is_function_type (h->type))
   3103 	binding_stays_local_p = TRUE;
   3104       break;
   3105 
   3106     default:
   3107       break;
   3108     }
   3109 
   3110   /* If it isn't defined locally, then clearly it's dynamic.  */
   3111   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   3112     return TRUE;
   3113 
   3114   /* Otherwise, the symbol is dynamic if binding rules don't tell
   3115      us that it remains local.  */
   3116   return !binding_stays_local_p;
   3117 }
   3118 
   3119 /* Return true if the symbol referred to by H should be considered
   3120    to resolve local to the current module, and false otherwise.  Differs
   3121    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   3122    undefined symbols.  The two functions are virtually identical except
   3123    for the place where dynindx == -1 is tested.  If that test is true,
   3124    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
   3125    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
   3126    defined symbols.
   3127    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   3128    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   3129    treatment of undefined weak symbols.  For those that do not make
   3130    undefined weak symbols dynamic, both functions may return false.  */
   3131 
   3132 bfd_boolean
   3133 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   3134 			      struct bfd_link_info *info,
   3135 			      bfd_boolean local_protected)
   3136 {
   3137   const struct elf_backend_data *bed;
   3138   struct elf_link_hash_table *hash_table;
   3139 
   3140   /* If it's a local sym, of course we resolve locally.  */
   3141   if (h == NULL)
   3142     return TRUE;
   3143 
   3144   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   3145   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   3146       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   3147     return TRUE;
   3148 
   3149   /* Forced local symbols resolve locally.  */
   3150   if (h->forced_local)
   3151     return TRUE;
   3152 
   3153   /* Common symbols that become definitions don't get the DEF_REGULAR
   3154      flag set, so test it first, and don't bail out.  */
   3155   if (ELF_COMMON_DEF_P (h))
   3156     /* Do nothing.  */;
   3157   /* If we don't have a definition in a regular file, then we can't
   3158      resolve locally.  The sym is either undefined or dynamic.  */
   3159   else if (!h->def_regular)
   3160     return FALSE;
   3161 
   3162   /* Non-dynamic symbols resolve locally.  */
   3163   if (h->dynindx == -1)
   3164     return TRUE;
   3165 
   3166   /* At this point, we know the symbol is defined and dynamic.  In an
   3167      executable it must resolve locally, likewise when building symbolic
   3168      shared libraries.  */
   3169   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3170     return TRUE;
   3171 
   3172   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3173      with default visibility might not resolve locally.  */
   3174   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3175     return FALSE;
   3176 
   3177   hash_table = elf_hash_table (info);
   3178   if (!is_elf_hash_table (hash_table))
   3179     return TRUE;
   3180 
   3181   bed = get_elf_backend_data (hash_table->dynobj);
   3182 
   3183   /* If extern_protected_data is false, STV_PROTECTED non-function
   3184      symbols are local.  */
   3185   if ((!info->extern_protected_data
   3186        || (info->extern_protected_data < 0
   3187 	   && !bed->extern_protected_data))
   3188       && !bed->is_function_type (h->type))
   3189     return TRUE;
   3190 
   3191   /* Function pointer equality tests may require that STV_PROTECTED
   3192      symbols be treated as dynamic symbols.  If the address of a
   3193      function not defined in an executable is set to that function's
   3194      plt entry in the executable, then the address of the function in
   3195      a shared library must also be the plt entry in the executable.  */
   3196   return local_protected;
   3197 }
   3198 
   3199 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3200    aligned.  Returns the first TLS output section.  */
   3201 
   3202 struct bfd_section *
   3203 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3204 {
   3205   struct bfd_section *sec, *tls;
   3206   unsigned int align = 0;
   3207 
   3208   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3209     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3210       break;
   3211   tls = sec;
   3212 
   3213   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3214     if (sec->alignment_power > align)
   3215       align = sec->alignment_power;
   3216 
   3217   elf_hash_table (info)->tls_sec = tls;
   3218 
   3219   /* Ensure the alignment of the first section is the largest alignment,
   3220      so that the tls segment starts aligned.  */
   3221   if (tls != NULL)
   3222     tls->alignment_power = align;
   3223 
   3224   return tls;
   3225 }
   3226 
   3227 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3228 static bfd_boolean
   3229 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3230 				  Elf_Internal_Sym *sym)
   3231 {
   3232   const struct elf_backend_data *bed;
   3233 
   3234   /* Local symbols do not count, but target specific ones might.  */
   3235   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3236       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3237     return FALSE;
   3238 
   3239   bed = get_elf_backend_data (abfd);
   3240   /* Function symbols do not count.  */
   3241   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3242     return FALSE;
   3243 
   3244   /* If the section is undefined, then so is the symbol.  */
   3245   if (sym->st_shndx == SHN_UNDEF)
   3246     return FALSE;
   3247 
   3248   /* If the symbol is defined in the common section, then
   3249      it is a common definition and so does not count.  */
   3250   if (bed->common_definition (sym))
   3251     return FALSE;
   3252 
   3253   /* If the symbol is in a target specific section then we
   3254      must rely upon the backend to tell us what it is.  */
   3255   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3256     /* FIXME - this function is not coded yet:
   3257 
   3258        return _bfd_is_global_symbol_definition (abfd, sym);
   3259 
   3260        Instead for now assume that the definition is not global,
   3261        Even if this is wrong, at least the linker will behave
   3262        in the same way that it used to do.  */
   3263     return FALSE;
   3264 
   3265   return TRUE;
   3266 }
   3267 
   3268 /* Search the symbol table of the archive element of the archive ABFD
   3269    whose archive map contains a mention of SYMDEF, and determine if
   3270    the symbol is defined in this element.  */
   3271 static bfd_boolean
   3272 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3273 {
   3274   Elf_Internal_Shdr * hdr;
   3275   size_t symcount;
   3276   size_t extsymcount;
   3277   size_t extsymoff;
   3278   Elf_Internal_Sym *isymbuf;
   3279   Elf_Internal_Sym *isym;
   3280   Elf_Internal_Sym *isymend;
   3281   bfd_boolean result;
   3282 
   3283   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   3284   if (abfd == NULL)
   3285     return FALSE;
   3286 
   3287   if (! bfd_check_format (abfd, bfd_object))
   3288     return FALSE;
   3289 
   3290   /* Select the appropriate symbol table.  If we don't know if the
   3291      object file is an IR object, give linker LTO plugin a chance to
   3292      get the correct symbol table.  */
   3293   if (abfd->plugin_format == bfd_plugin_yes
   3294 #if BFD_SUPPORTS_PLUGINS
   3295       || (abfd->plugin_format == bfd_plugin_unknown
   3296 	  && bfd_link_plugin_object_p (abfd))
   3297 #endif
   3298       )
   3299     {
   3300       /* Use the IR symbol table if the object has been claimed by
   3301 	 plugin.  */
   3302       abfd = abfd->plugin_dummy_bfd;
   3303       hdr = &elf_tdata (abfd)->symtab_hdr;
   3304     }
   3305   else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3306     hdr = &elf_tdata (abfd)->symtab_hdr;
   3307   else
   3308     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3309 
   3310   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3311 
   3312   /* The sh_info field of the symtab header tells us where the
   3313      external symbols start.  We don't care about the local symbols.  */
   3314   if (elf_bad_symtab (abfd))
   3315     {
   3316       extsymcount = symcount;
   3317       extsymoff = 0;
   3318     }
   3319   else
   3320     {
   3321       extsymcount = symcount - hdr->sh_info;
   3322       extsymoff = hdr->sh_info;
   3323     }
   3324 
   3325   if (extsymcount == 0)
   3326     return FALSE;
   3327 
   3328   /* Read in the symbol table.  */
   3329   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3330 				  NULL, NULL, NULL);
   3331   if (isymbuf == NULL)
   3332     return FALSE;
   3333 
   3334   /* Scan the symbol table looking for SYMDEF.  */
   3335   result = FALSE;
   3336   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3337     {
   3338       const char *name;
   3339 
   3340       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3341 					      isym->st_name);
   3342       if (name == NULL)
   3343 	break;
   3344 
   3345       if (strcmp (name, symdef->name) == 0)
   3346 	{
   3347 	  result = is_global_data_symbol_definition (abfd, isym);
   3348 	  break;
   3349 	}
   3350     }
   3351 
   3352   free (isymbuf);
   3353 
   3354   return result;
   3355 }
   3356 
   3357 /* Add an entry to the .dynamic table.  */
   3359 
   3360 bfd_boolean
   3361 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3362 			    bfd_vma tag,
   3363 			    bfd_vma val)
   3364 {
   3365   struct elf_link_hash_table *hash_table;
   3366   const struct elf_backend_data *bed;
   3367   asection *s;
   3368   bfd_size_type newsize;
   3369   bfd_byte *newcontents;
   3370   Elf_Internal_Dyn dyn;
   3371 
   3372   hash_table = elf_hash_table (info);
   3373   if (! is_elf_hash_table (hash_table))
   3374     return FALSE;
   3375 
   3376   bed = get_elf_backend_data (hash_table->dynobj);
   3377   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3378   BFD_ASSERT (s != NULL);
   3379 
   3380   newsize = s->size + bed->s->sizeof_dyn;
   3381   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3382   if (newcontents == NULL)
   3383     return FALSE;
   3384 
   3385   dyn.d_tag = tag;
   3386   dyn.d_un.d_val = val;
   3387   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3388 
   3389   s->size = newsize;
   3390   s->contents = newcontents;
   3391 
   3392   return TRUE;
   3393 }
   3394 
   3395 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
   3396    otherwise just check whether one already exists.  Returns -1 on error,
   3397    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3398 
   3399 static int
   3400 elf_add_dt_needed_tag (bfd *abfd,
   3401 		       struct bfd_link_info *info,
   3402 		       const char *soname,
   3403 		       bfd_boolean do_it)
   3404 {
   3405   struct elf_link_hash_table *hash_table;
   3406   size_t strindex;
   3407 
   3408   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3409     return -1;
   3410 
   3411   hash_table = elf_hash_table (info);
   3412   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
   3413   if (strindex == (size_t) -1)
   3414     return -1;
   3415 
   3416   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3417     {
   3418       asection *sdyn;
   3419       const struct elf_backend_data *bed;
   3420       bfd_byte *extdyn;
   3421 
   3422       bed = get_elf_backend_data (hash_table->dynobj);
   3423       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3424       if (sdyn != NULL)
   3425 	for (extdyn = sdyn->contents;
   3426 	     extdyn < sdyn->contents + sdyn->size;
   3427 	     extdyn += bed->s->sizeof_dyn)
   3428 	  {
   3429 	    Elf_Internal_Dyn dyn;
   3430 
   3431 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3432 	    if (dyn.d_tag == DT_NEEDED
   3433 		&& dyn.d_un.d_val == strindex)
   3434 	      {
   3435 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3436 		return 1;
   3437 	      }
   3438 	  }
   3439     }
   3440 
   3441   if (do_it)
   3442     {
   3443       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3444 	return -1;
   3445 
   3446       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3447 	return -1;
   3448     }
   3449   else
   3450     /* We were just checking for existence of the tag.  */
   3451     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3452 
   3453   return 0;
   3454 }
   3455 
   3456 /* Return true if SONAME is on the needed list between NEEDED and STOP
   3457    (or the end of list if STOP is NULL), and needed by a library that
   3458    will be loaded.  */
   3459 
   3460 static bfd_boolean
   3461 on_needed_list (const char *soname,
   3462 		struct bfd_link_needed_list *needed,
   3463 		struct bfd_link_needed_list *stop)
   3464 {
   3465   struct bfd_link_needed_list *look;
   3466   for (look = needed; look != stop; look = look->next)
   3467     if (strcmp (soname, look->name) == 0
   3468 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
   3469 	    /* If needed by a library that itself is not directly
   3470 	       needed, recursively check whether that library is
   3471 	       indirectly needed.  Since we add DT_NEEDED entries to
   3472 	       the end of the list, library dependencies appear after
   3473 	       the library.  Therefore search prior to the current
   3474 	       LOOK, preventing possible infinite recursion.  */
   3475 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
   3476       return TRUE;
   3477 
   3478   return FALSE;
   3479 }
   3480 
   3481 /* Sort symbol by value, section, and size.  */
   3482 static int
   3483 elf_sort_symbol (const void *arg1, const void *arg2)
   3484 {
   3485   const struct elf_link_hash_entry *h1;
   3486   const struct elf_link_hash_entry *h2;
   3487   bfd_signed_vma vdiff;
   3488 
   3489   h1 = *(const struct elf_link_hash_entry **) arg1;
   3490   h2 = *(const struct elf_link_hash_entry **) arg2;
   3491   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3492   if (vdiff != 0)
   3493     return vdiff > 0 ? 1 : -1;
   3494   else
   3495     {
   3496       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3497       if (sdiff != 0)
   3498 	return sdiff > 0 ? 1 : -1;
   3499     }
   3500   vdiff = h1->size - h2->size;
   3501   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
   3502 }
   3503 
   3504 /* This function is used to adjust offsets into .dynstr for
   3505    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   3506 
   3507 static bfd_boolean
   3508 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   3509 {
   3510   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   3511 
   3512   if (h->dynindx != -1)
   3513     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   3514   return TRUE;
   3515 }
   3516 
   3517 /* Assign string offsets in .dynstr, update all structures referencing
   3518    them.  */
   3519 
   3520 static bfd_boolean
   3521 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   3522 {
   3523   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   3524   struct elf_link_local_dynamic_entry *entry;
   3525   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   3526   bfd *dynobj = hash_table->dynobj;
   3527   asection *sdyn;
   3528   bfd_size_type size;
   3529   const struct elf_backend_data *bed;
   3530   bfd_byte *extdyn;
   3531 
   3532   _bfd_elf_strtab_finalize (dynstr);
   3533   size = _bfd_elf_strtab_size (dynstr);
   3534 
   3535   bed = get_elf_backend_data (dynobj);
   3536   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3537   BFD_ASSERT (sdyn != NULL);
   3538 
   3539   /* Update all .dynamic entries referencing .dynstr strings.  */
   3540   for (extdyn = sdyn->contents;
   3541        extdyn < sdyn->contents + sdyn->size;
   3542        extdyn += bed->s->sizeof_dyn)
   3543     {
   3544       Elf_Internal_Dyn dyn;
   3545 
   3546       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   3547       switch (dyn.d_tag)
   3548 	{
   3549 	case DT_STRSZ:
   3550 	  dyn.d_un.d_val = size;
   3551 	  break;
   3552 	case DT_NEEDED:
   3553 	case DT_SONAME:
   3554 	case DT_RPATH:
   3555 	case DT_RUNPATH:
   3556 	case DT_FILTER:
   3557 	case DT_AUXILIARY:
   3558 	case DT_AUDIT:
   3559 	case DT_DEPAUDIT:
   3560 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   3561 	  break;
   3562 	default:
   3563 	  continue;
   3564 	}
   3565       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   3566     }
   3567 
   3568   /* Now update local dynamic symbols.  */
   3569   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   3570     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   3571 						  entry->isym.st_name);
   3572 
   3573   /* And the rest of dynamic symbols.  */
   3574   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   3575 
   3576   /* Adjust version definitions.  */
   3577   if (elf_tdata (output_bfd)->cverdefs)
   3578     {
   3579       asection *s;
   3580       bfd_byte *p;
   3581       size_t i;
   3582       Elf_Internal_Verdef def;
   3583       Elf_Internal_Verdaux defaux;
   3584 
   3585       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   3586       p = s->contents;
   3587       do
   3588 	{
   3589 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   3590 				   &def);
   3591 	  p += sizeof (Elf_External_Verdef);
   3592 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   3593 	    continue;
   3594 	  for (i = 0; i < def.vd_cnt; ++i)
   3595 	    {
   3596 	      _bfd_elf_swap_verdaux_in (output_bfd,
   3597 					(Elf_External_Verdaux *) p, &defaux);
   3598 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   3599 							defaux.vda_name);
   3600 	      _bfd_elf_swap_verdaux_out (output_bfd,
   3601 					 &defaux, (Elf_External_Verdaux *) p);
   3602 	      p += sizeof (Elf_External_Verdaux);
   3603 	    }
   3604 	}
   3605       while (def.vd_next);
   3606     }
   3607 
   3608   /* Adjust version references.  */
   3609   if (elf_tdata (output_bfd)->verref)
   3610     {
   3611       asection *s;
   3612       bfd_byte *p;
   3613       size_t i;
   3614       Elf_Internal_Verneed need;
   3615       Elf_Internal_Vernaux needaux;
   3616 
   3617       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   3618       p = s->contents;
   3619       do
   3620 	{
   3621 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   3622 				    &need);
   3623 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   3624 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   3625 				     (Elf_External_Verneed *) p);
   3626 	  p += sizeof (Elf_External_Verneed);
   3627 	  for (i = 0; i < need.vn_cnt; ++i)
   3628 	    {
   3629 	      _bfd_elf_swap_vernaux_in (output_bfd,
   3630 					(Elf_External_Vernaux *) p, &needaux);
   3631 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   3632 							 needaux.vna_name);
   3633 	      _bfd_elf_swap_vernaux_out (output_bfd,
   3634 					 &needaux,
   3635 					 (Elf_External_Vernaux *) p);
   3636 	      p += sizeof (Elf_External_Vernaux);
   3637 	    }
   3638 	}
   3639       while (need.vn_next);
   3640     }
   3641 
   3642   return TRUE;
   3643 }
   3644 
   3645 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3647    The default is to only match when the INPUT and OUTPUT are exactly
   3648    the same target.  */
   3649 
   3650 bfd_boolean
   3651 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   3652 				    const bfd_target *output)
   3653 {
   3654   return input == output;
   3655 }
   3656 
   3657 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3658    This version is used when different targets for the same architecture
   3659    are virtually identical.  */
   3660 
   3661 bfd_boolean
   3662 _bfd_elf_relocs_compatible (const bfd_target *input,
   3663 			    const bfd_target *output)
   3664 {
   3665   const struct elf_backend_data *obed, *ibed;
   3666 
   3667   if (input == output)
   3668     return TRUE;
   3669 
   3670   ibed = xvec_get_elf_backend_data (input);
   3671   obed = xvec_get_elf_backend_data (output);
   3672 
   3673   if (ibed->arch != obed->arch)
   3674     return FALSE;
   3675 
   3676   /* If both backends are using this function, deem them compatible.  */
   3677   return ibed->relocs_compatible == obed->relocs_compatible;
   3678 }
   3679 
   3680 /* Make a special call to the linker "notice" function to tell it that
   3681    we are about to handle an as-needed lib, or have finished
   3682    processing the lib.  */
   3683 
   3684 bfd_boolean
   3685 _bfd_elf_notice_as_needed (bfd *ibfd,
   3686 			   struct bfd_link_info *info,
   3687 			   enum notice_asneeded_action act)
   3688 {
   3689   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   3690 }
   3691 
   3692 /* Check relocations an ELF object file.  */
   3693 
   3694 bfd_boolean
   3695 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   3696 {
   3697   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3698   struct elf_link_hash_table *htab = elf_hash_table (info);
   3699 
   3700   /* If this object is the same format as the output object, and it is
   3701      not a shared library, then let the backend look through the
   3702      relocs.
   3703 
   3704      This is required to build global offset table entries and to
   3705      arrange for dynamic relocs.  It is not required for the
   3706      particular common case of linking non PIC code, even when linking
   3707      against shared libraries, but unfortunately there is no way of
   3708      knowing whether an object file has been compiled PIC or not.
   3709      Looking through the relocs is not particularly time consuming.
   3710      The problem is that we must either (1) keep the relocs in memory,
   3711      which causes the linker to require additional runtime memory or
   3712      (2) read the relocs twice from the input file, which wastes time.
   3713      This would be a good case for using mmap.
   3714 
   3715      I have no idea how to handle linking PIC code into a file of a
   3716      different format.  It probably can't be done.  */
   3717   if ((abfd->flags & DYNAMIC) == 0
   3718       && is_elf_hash_table (htab)
   3719       && bed->check_relocs != NULL
   3720       && elf_object_id (abfd) == elf_hash_table_id (htab)
   3721       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   3722     {
   3723       asection *o;
   3724 
   3725       for (o = abfd->sections; o != NULL; o = o->next)
   3726 	{
   3727 	  Elf_Internal_Rela *internal_relocs;
   3728 	  bfd_boolean ok;
   3729 
   3730 	  /* Don't check relocations in excluded sections.  */
   3731 	  if ((o->flags & SEC_RELOC) == 0
   3732 	      || (o->flags & SEC_EXCLUDE) != 0
   3733 	      || o->reloc_count == 0
   3734 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   3735 		  && (o->flags & SEC_DEBUGGING) != 0)
   3736 	      || bfd_is_abs_section (o->output_section))
   3737 	    continue;
   3738 
   3739 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   3740 						       info->keep_memory);
   3741 	  if (internal_relocs == NULL)
   3742 	    return FALSE;
   3743 
   3744 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
   3745 
   3746 	  if (elf_section_data (o)->relocs != internal_relocs)
   3747 	    free (internal_relocs);
   3748 
   3749 	  if (! ok)
   3750 	    return FALSE;
   3751 	}
   3752     }
   3753 
   3754   return TRUE;
   3755 }
   3756 
   3757 /* Add symbols from an ELF object file to the linker hash table.  */
   3758 
   3759 static bfd_boolean
   3760 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   3761 {
   3762   Elf_Internal_Ehdr *ehdr;
   3763   Elf_Internal_Shdr *hdr;
   3764   size_t symcount;
   3765   size_t extsymcount;
   3766   size_t extsymoff;
   3767   struct elf_link_hash_entry **sym_hash;
   3768   bfd_boolean dynamic;
   3769   Elf_External_Versym *extversym = NULL;
   3770   Elf_External_Versym *ever;
   3771   struct elf_link_hash_entry *weaks;
   3772   struct elf_link_hash_entry **nondeflt_vers = NULL;
   3773   size_t nondeflt_vers_cnt = 0;
   3774   Elf_Internal_Sym *isymbuf = NULL;
   3775   Elf_Internal_Sym *isym;
   3776   Elf_Internal_Sym *isymend;
   3777   const struct elf_backend_data *bed;
   3778   bfd_boolean add_needed;
   3779   struct elf_link_hash_table *htab;
   3780   bfd_size_type amt;
   3781   void *alloc_mark = NULL;
   3782   struct bfd_hash_entry **old_table = NULL;
   3783   unsigned int old_size = 0;
   3784   unsigned int old_count = 0;
   3785   void *old_tab = NULL;
   3786   void *old_ent;
   3787   struct bfd_link_hash_entry *old_undefs = NULL;
   3788   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   3789   void *old_strtab = NULL;
   3790   size_t tabsize = 0;
   3791   asection *s;
   3792   bfd_boolean just_syms;
   3793 
   3794   htab = elf_hash_table (info);
   3795   bed = get_elf_backend_data (abfd);
   3796 
   3797   if ((abfd->flags & DYNAMIC) == 0)
   3798     dynamic = FALSE;
   3799   else
   3800     {
   3801       dynamic = TRUE;
   3802 
   3803       /* You can't use -r against a dynamic object.  Also, there's no
   3804 	 hope of using a dynamic object which does not exactly match
   3805 	 the format of the output file.  */
   3806       if (bfd_link_relocatable (info)
   3807 	  || !is_elf_hash_table (htab)
   3808 	  || info->output_bfd->xvec != abfd->xvec)
   3809 	{
   3810 	  if (bfd_link_relocatable (info))
   3811 	    bfd_set_error (bfd_error_invalid_operation);
   3812 	  else
   3813 	    bfd_set_error (bfd_error_wrong_format);
   3814 	  goto error_return;
   3815 	}
   3816     }
   3817 
   3818   ehdr = elf_elfheader (abfd);
   3819   if (info->warn_alternate_em
   3820       && bed->elf_machine_code != ehdr->e_machine
   3821       && ((bed->elf_machine_alt1 != 0
   3822 	   && ehdr->e_machine == bed->elf_machine_alt1)
   3823 	  || (bed->elf_machine_alt2 != 0
   3824 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   3825     info->callbacks->einfo
   3826       /* xgettext:c-format */
   3827       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
   3828        ehdr->e_machine, abfd, bed->elf_machine_code);
   3829 
   3830   /* As a GNU extension, any input sections which are named
   3831      .gnu.warning.SYMBOL are treated as warning symbols for the given
   3832      symbol.  This differs from .gnu.warning sections, which generate
   3833      warnings when they are included in an output file.  */
   3834   /* PR 12761: Also generate this warning when building shared libraries.  */
   3835   for (s = abfd->sections; s != NULL; s = s->next)
   3836     {
   3837       const char *name;
   3838 
   3839       name = bfd_get_section_name (abfd, s);
   3840       if (CONST_STRNEQ (name, ".gnu.warning."))
   3841 	{
   3842 	  char *msg;
   3843 	  bfd_size_type sz;
   3844 
   3845 	  name += sizeof ".gnu.warning." - 1;
   3846 
   3847 	  /* If this is a shared object, then look up the symbol
   3848 	     in the hash table.  If it is there, and it is already
   3849 	     been defined, then we will not be using the entry
   3850 	     from this shared object, so we don't need to warn.
   3851 	     FIXME: If we see the definition in a regular object
   3852 	     later on, we will warn, but we shouldn't.  The only
   3853 	     fix is to keep track of what warnings we are supposed
   3854 	     to emit, and then handle them all at the end of the
   3855 	     link.  */
   3856 	  if (dynamic)
   3857 	    {
   3858 	      struct elf_link_hash_entry *h;
   3859 
   3860 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   3861 
   3862 	      /* FIXME: What about bfd_link_hash_common?  */
   3863 	      if (h != NULL
   3864 		  && (h->root.type == bfd_link_hash_defined
   3865 		      || h->root.type == bfd_link_hash_defweak))
   3866 		continue;
   3867 	    }
   3868 
   3869 	  sz = s->size;
   3870 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   3871 	  if (msg == NULL)
   3872 	    goto error_return;
   3873 
   3874 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   3875 	    goto error_return;
   3876 
   3877 	  msg[sz] = '\0';
   3878 
   3879 	  if (! (_bfd_generic_link_add_one_symbol
   3880 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   3881 		  FALSE, bed->collect, NULL)))
   3882 	    goto error_return;
   3883 
   3884 	  if (bfd_link_executable (info))
   3885 	    {
   3886 	      /* Clobber the section size so that the warning does
   3887 		 not get copied into the output file.  */
   3888 	      s->size = 0;
   3889 
   3890 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   3891 		 the warning section don't get copied to the output.  */
   3892 	      s->flags |= SEC_EXCLUDE;
   3893 	    }
   3894 	}
   3895     }
   3896 
   3897   just_syms = ((s = abfd->sections) != NULL
   3898 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   3899 
   3900   add_needed = TRUE;
   3901   if (! dynamic)
   3902     {
   3903       /* If we are creating a shared library, create all the dynamic
   3904 	 sections immediately.  We need to attach them to something,
   3905 	 so we attach them to this BFD, provided it is the right
   3906 	 format and is not from ld --just-symbols.  Always create the
   3907 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
   3908 	 are no input BFD's of the same format as the output, we can't
   3909 	 make a shared library.  */
   3910       if (!just_syms
   3911 	  && (bfd_link_pic (info)
   3912 	      || (!bfd_link_relocatable (info)
   3913 		  && info->nointerp
   3914 		  && (info->export_dynamic || info->dynamic)))
   3915 	  && is_elf_hash_table (htab)
   3916 	  && info->output_bfd->xvec == abfd->xvec
   3917 	  && !htab->dynamic_sections_created)
   3918 	{
   3919 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   3920 	    goto error_return;
   3921 	}
   3922     }
   3923   else if (!is_elf_hash_table (htab))
   3924     goto error_return;
   3925   else
   3926     {
   3927       const char *soname = NULL;
   3928       char *audit = NULL;
   3929       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   3930       const Elf_Internal_Phdr *phdr;
   3931       int ret;
   3932 
   3933       /* ld --just-symbols and dynamic objects don't mix very well.
   3934 	 ld shouldn't allow it.  */
   3935       if (just_syms)
   3936 	abort ();
   3937 
   3938       /* If this dynamic lib was specified on the command line with
   3939 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   3940 	 tag unless the lib is actually used.  Similary for libs brought
   3941 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   3942 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   3943 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   3944 	 all.  */
   3945       add_needed = (elf_dyn_lib_class (abfd)
   3946 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   3947 		       | DYN_NO_NEEDED)) == 0;
   3948 
   3949       s = bfd_get_section_by_name (abfd, ".dynamic");
   3950       if (s != NULL)
   3951 	{
   3952 	  bfd_byte *dynbuf;
   3953 	  bfd_byte *extdyn;
   3954 	  unsigned int elfsec;
   3955 	  unsigned long shlink;
   3956 
   3957 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   3958 	    {
   3959 error_free_dyn:
   3960 	      free (dynbuf);
   3961 	      goto error_return;
   3962 	    }
   3963 
   3964 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   3965 	  if (elfsec == SHN_BAD)
   3966 	    goto error_free_dyn;
   3967 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   3968 
   3969 	  for (extdyn = dynbuf;
   3970 	       extdyn < dynbuf + s->size;
   3971 	       extdyn += bed->s->sizeof_dyn)
   3972 	    {
   3973 	      Elf_Internal_Dyn dyn;
   3974 
   3975 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   3976 	      if (dyn.d_tag == DT_SONAME)
   3977 		{
   3978 		  unsigned int tagv = dyn.d_un.d_val;
   3979 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3980 		  if (soname == NULL)
   3981 		    goto error_free_dyn;
   3982 		}
   3983 	      if (dyn.d_tag == DT_NEEDED)
   3984 		{
   3985 		  struct bfd_link_needed_list *n, **pn;
   3986 		  char *fnm, *anm;
   3987 		  unsigned int tagv = dyn.d_un.d_val;
   3988 
   3989 		  amt = sizeof (struct bfd_link_needed_list);
   3990 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3991 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3992 		  if (n == NULL || fnm == NULL)
   3993 		    goto error_free_dyn;
   3994 		  amt = strlen (fnm) + 1;
   3995 		  anm = (char *) bfd_alloc (abfd, amt);
   3996 		  if (anm == NULL)
   3997 		    goto error_free_dyn;
   3998 		  memcpy (anm, fnm, amt);
   3999 		  n->name = anm;
   4000 		  n->by = abfd;
   4001 		  n->next = NULL;
   4002 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   4003 		    ;
   4004 		  *pn = n;
   4005 		}
   4006 	      if (dyn.d_tag == DT_RUNPATH)
   4007 		{
   4008 		  struct bfd_link_needed_list *n, **pn;
   4009 		  char *fnm, *anm;
   4010 		  unsigned int tagv = dyn.d_un.d_val;
   4011 
   4012 		  amt = sizeof (struct bfd_link_needed_list);
   4013 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4014 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4015 		  if (n == NULL || fnm == NULL)
   4016 		    goto error_free_dyn;
   4017 		  amt = strlen (fnm) + 1;
   4018 		  anm = (char *) bfd_alloc (abfd, amt);
   4019 		  if (anm == NULL)
   4020 		    goto error_free_dyn;
   4021 		  memcpy (anm, fnm, amt);
   4022 		  n->name = anm;
   4023 		  n->by = abfd;
   4024 		  n->next = NULL;
   4025 		  for (pn = & runpath;
   4026 		       *pn != NULL;
   4027 		       pn = &(*pn)->next)
   4028 		    ;
   4029 		  *pn = n;
   4030 		}
   4031 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   4032 	      if (!runpath && dyn.d_tag == DT_RPATH)
   4033 		{
   4034 		  struct bfd_link_needed_list *n, **pn;
   4035 		  char *fnm, *anm;
   4036 		  unsigned int tagv = dyn.d_un.d_val;
   4037 
   4038 		  amt = sizeof (struct bfd_link_needed_list);
   4039 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   4040 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4041 		  if (n == NULL || fnm == NULL)
   4042 		    goto error_free_dyn;
   4043 		  amt = strlen (fnm) + 1;
   4044 		  anm = (char *) bfd_alloc (abfd, amt);
   4045 		  if (anm == NULL)
   4046 		    goto error_free_dyn;
   4047 		  memcpy (anm, fnm, amt);
   4048 		  n->name = anm;
   4049 		  n->by = abfd;
   4050 		  n->next = NULL;
   4051 		  for (pn = & rpath;
   4052 		       *pn != NULL;
   4053 		       pn = &(*pn)->next)
   4054 		    ;
   4055 		  *pn = n;
   4056 		}
   4057 	      if (dyn.d_tag == DT_AUDIT)
   4058 		{
   4059 		  unsigned int tagv = dyn.d_un.d_val;
   4060 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   4061 		}
   4062 	    }
   4063 
   4064 	  free (dynbuf);
   4065 	}
   4066 
   4067       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   4068 	 frees all more recently bfd_alloc'd blocks as well.  */
   4069       if (runpath)
   4070 	rpath = runpath;
   4071 
   4072       if (rpath)
   4073 	{
   4074 	  struct bfd_link_needed_list **pn;
   4075 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   4076 	    ;
   4077 	  *pn = rpath;
   4078 	}
   4079 
   4080       /* If we have a PT_GNU_RELRO program header, mark as read-only
   4081 	 all sections contained fully therein.  This makes relro
   4082 	 shared library sections appear as they will at run-time.  */
   4083       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
   4084       while (--phdr >= elf_tdata (abfd)->phdr)
   4085 	if (phdr->p_type == PT_GNU_RELRO)
   4086 	  {
   4087 	    for (s = abfd->sections; s != NULL; s = s->next)
   4088 	      if ((s->flags & SEC_ALLOC) != 0
   4089 		  && s->vma >= phdr->p_vaddr
   4090 		  && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
   4091 		s->flags |= SEC_READONLY;
   4092 	    break;
   4093 	  }
   4094 
   4095       /* We do not want to include any of the sections in a dynamic
   4096 	 object in the output file.  We hack by simply clobbering the
   4097 	 list of sections in the BFD.  This could be handled more
   4098 	 cleanly by, say, a new section flag; the existing
   4099 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   4100 	 still implies that the section takes up space in the output
   4101 	 file.  */
   4102       bfd_section_list_clear (abfd);
   4103 
   4104       /* Find the name to use in a DT_NEEDED entry that refers to this
   4105 	 object.  If the object has a DT_SONAME entry, we use it.
   4106 	 Otherwise, if the generic linker stuck something in
   4107 	 elf_dt_name, we use that.  Otherwise, we just use the file
   4108 	 name.  */
   4109       if (soname == NULL || *soname == '\0')
   4110 	{
   4111 	  soname = elf_dt_name (abfd);
   4112 	  if (soname == NULL || *soname == '\0')
   4113 	    soname = bfd_get_filename (abfd);
   4114 	}
   4115 
   4116       /* Save the SONAME because sometimes the linker emulation code
   4117 	 will need to know it.  */
   4118       elf_dt_name (abfd) = soname;
   4119 
   4120       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   4121       if (ret < 0)
   4122 	goto error_return;
   4123 
   4124       /* If we have already included this dynamic object in the
   4125 	 link, just ignore it.  There is no reason to include a
   4126 	 particular dynamic object more than once.  */
   4127       if (ret > 0)
   4128 	return TRUE;
   4129 
   4130       /* Save the DT_AUDIT entry for the linker emulation code. */
   4131       elf_dt_audit (abfd) = audit;
   4132     }
   4133 
   4134   /* If this is a dynamic object, we always link against the .dynsym
   4135      symbol table, not the .symtab symbol table.  The dynamic linker
   4136      will only see the .dynsym symbol table, so there is no reason to
   4137      look at .symtab for a dynamic object.  */
   4138 
   4139   if (! dynamic || elf_dynsymtab (abfd) == 0)
   4140     hdr = &elf_tdata (abfd)->symtab_hdr;
   4141   else
   4142     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   4143 
   4144   symcount = hdr->sh_size / bed->s->sizeof_sym;
   4145 
   4146   /* The sh_info field of the symtab header tells us where the
   4147      external symbols start.  We don't care about the local symbols at
   4148      this point.  */
   4149   if (elf_bad_symtab (abfd))
   4150     {
   4151       extsymcount = symcount;
   4152       extsymoff = 0;
   4153     }
   4154   else
   4155     {
   4156       extsymcount = symcount - hdr->sh_info;
   4157       extsymoff = hdr->sh_info;
   4158     }
   4159 
   4160   sym_hash = elf_sym_hashes (abfd);
   4161   if (extsymcount != 0)
   4162     {
   4163       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   4164 				      NULL, NULL, NULL);
   4165       if (isymbuf == NULL)
   4166 	goto error_return;
   4167 
   4168       if (sym_hash == NULL)
   4169 	{
   4170 	  /* We store a pointer to the hash table entry for each
   4171 	     external symbol.  */
   4172 	  amt = extsymcount;
   4173 	  amt *= sizeof (struct elf_link_hash_entry *);
   4174 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   4175 	  if (sym_hash == NULL)
   4176 	    goto error_free_sym;
   4177 	  elf_sym_hashes (abfd) = sym_hash;
   4178 	}
   4179     }
   4180 
   4181   if (dynamic)
   4182     {
   4183       /* Read in any version definitions.  */
   4184       if (!_bfd_elf_slurp_version_tables (abfd,
   4185 					  info->default_imported_symver))
   4186 	goto error_free_sym;
   4187 
   4188       /* Read in the symbol versions, but don't bother to convert them
   4189 	 to internal format.  */
   4190       if (elf_dynversym (abfd) != 0)
   4191 	{
   4192 	  Elf_Internal_Shdr *versymhdr;
   4193 
   4194 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   4195 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   4196 	  if (extversym == NULL)
   4197 	    goto error_free_sym;
   4198 	  amt = versymhdr->sh_size;
   4199 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
   4200 	      || bfd_bread (extversym, amt, abfd) != amt)
   4201 	    goto error_free_vers;
   4202 	}
   4203     }
   4204 
   4205   /* If we are loading an as-needed shared lib, save the symbol table
   4206      state before we start adding symbols.  If the lib turns out
   4207      to be unneeded, restore the state.  */
   4208   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4209     {
   4210       unsigned int i;
   4211       size_t entsize;
   4212 
   4213       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   4214 	{
   4215 	  struct bfd_hash_entry *p;
   4216 	  struct elf_link_hash_entry *h;
   4217 
   4218 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4219 	    {
   4220 	      h = (struct elf_link_hash_entry *) p;
   4221 	      entsize += htab->root.table.entsize;
   4222 	      if (h->root.type == bfd_link_hash_warning)
   4223 		entsize += htab->root.table.entsize;
   4224 	    }
   4225 	}
   4226 
   4227       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   4228       old_tab = bfd_malloc (tabsize + entsize);
   4229       if (old_tab == NULL)
   4230 	goto error_free_vers;
   4231 
   4232       /* Remember the current objalloc pointer, so that all mem for
   4233 	 symbols added can later be reclaimed.  */
   4234       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   4235       if (alloc_mark == NULL)
   4236 	goto error_free_vers;
   4237 
   4238       /* Make a special call to the linker "notice" function to
   4239 	 tell it that we are about to handle an as-needed lib.  */
   4240       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   4241 	goto error_free_vers;
   4242 
   4243       /* Clone the symbol table.  Remember some pointers into the
   4244 	 symbol table, and dynamic symbol count.  */
   4245       old_ent = (char *) old_tab + tabsize;
   4246       memcpy (old_tab, htab->root.table.table, tabsize);
   4247       old_undefs = htab->root.undefs;
   4248       old_undefs_tail = htab->root.undefs_tail;
   4249       old_table = htab->root.table.table;
   4250       old_size = htab->root.table.size;
   4251       old_count = htab->root.table.count;
   4252       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   4253       if (old_strtab == NULL)
   4254 	goto error_free_vers;
   4255 
   4256       for (i = 0; i < htab->root.table.size; i++)
   4257 	{
   4258 	  struct bfd_hash_entry *p;
   4259 	  struct elf_link_hash_entry *h;
   4260 
   4261 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4262 	    {
   4263 	      memcpy (old_ent, p, htab->root.table.entsize);
   4264 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4265 	      h = (struct elf_link_hash_entry *) p;
   4266 	      if (h->root.type == bfd_link_hash_warning)
   4267 		{
   4268 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
   4269 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4270 		}
   4271 	    }
   4272 	}
   4273     }
   4274 
   4275   weaks = NULL;
   4276   ever = extversym != NULL ? extversym + extsymoff : NULL;
   4277   for (isym = isymbuf, isymend = isymbuf + extsymcount;
   4278        isym < isymend;
   4279        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   4280     {
   4281       int bind;
   4282       bfd_vma value;
   4283       asection *sec, *new_sec;
   4284       flagword flags;
   4285       const char *name;
   4286       struct elf_link_hash_entry *h;
   4287       struct elf_link_hash_entry *hi;
   4288       bfd_boolean definition;
   4289       bfd_boolean size_change_ok;
   4290       bfd_boolean type_change_ok;
   4291       bfd_boolean new_weak;
   4292       bfd_boolean old_weak;
   4293       bfd_boolean override;
   4294       bfd_boolean common;
   4295       bfd_boolean discarded;
   4296       unsigned int old_alignment;
   4297       bfd *old_bfd;
   4298       bfd_boolean matched;
   4299 
   4300       override = FALSE;
   4301 
   4302       flags = BSF_NO_FLAGS;
   4303       sec = NULL;
   4304       value = isym->st_value;
   4305       common = bed->common_definition (isym);
   4306       if (common && info->inhibit_common_definition)
   4307 	{
   4308 	  /* Treat common symbol as undefined for --no-define-common.  */
   4309 	  isym->st_shndx = SHN_UNDEF;
   4310 	  common = FALSE;
   4311 	}
   4312       discarded = FALSE;
   4313 
   4314       bind = ELF_ST_BIND (isym->st_info);
   4315       switch (bind)
   4316 	{
   4317 	case STB_LOCAL:
   4318 	  /* This should be impossible, since ELF requires that all
   4319 	     global symbols follow all local symbols, and that sh_info
   4320 	     point to the first global symbol.  Unfortunately, Irix 5
   4321 	     screws this up.  */
   4322 	  continue;
   4323 
   4324 	case STB_GLOBAL:
   4325 	  if (isym->st_shndx != SHN_UNDEF && !common)
   4326 	    flags = BSF_GLOBAL;
   4327 	  break;
   4328 
   4329 	case STB_WEAK:
   4330 	  flags = BSF_WEAK;
   4331 	  break;
   4332 
   4333 	case STB_GNU_UNIQUE:
   4334 	  flags = BSF_GNU_UNIQUE;
   4335 	  break;
   4336 
   4337 	default:
   4338 	  /* Leave it up to the processor backend.  */
   4339 	  break;
   4340 	}
   4341 
   4342       if (isym->st_shndx == SHN_UNDEF)
   4343 	sec = bfd_und_section_ptr;
   4344       else if (isym->st_shndx == SHN_ABS)
   4345 	sec = bfd_abs_section_ptr;
   4346       else if (isym->st_shndx == SHN_COMMON)
   4347 	{
   4348 	  sec = bfd_com_section_ptr;
   4349 	  /* What ELF calls the size we call the value.  What ELF
   4350 	     calls the value we call the alignment.  */
   4351 	  value = isym->st_size;
   4352 	}
   4353       else
   4354 	{
   4355 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4356 	  if (sec == NULL)
   4357 	    sec = bfd_abs_section_ptr;
   4358 	  else if (discarded_section (sec))
   4359 	    {
   4360 	      /* Symbols from discarded section are undefined.  We keep
   4361 		 its visibility.  */
   4362 	      sec = bfd_und_section_ptr;
   4363 	      discarded = TRUE;
   4364 	      isym->st_shndx = SHN_UNDEF;
   4365 	    }
   4366 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   4367 	    value -= sec->vma;
   4368 	}
   4369 
   4370       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   4371 					      isym->st_name);
   4372       if (name == NULL)
   4373 	goto error_free_vers;
   4374 
   4375       if (isym->st_shndx == SHN_COMMON
   4376 	  && (abfd->flags & BFD_PLUGIN) != 0)
   4377 	{
   4378 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   4379 
   4380 	  if (xc == NULL)
   4381 	    {
   4382 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   4383 				 | SEC_EXCLUDE);
   4384 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   4385 	      if (xc == NULL)
   4386 		goto error_free_vers;
   4387 	    }
   4388 	  sec = xc;
   4389 	}
   4390       else if (isym->st_shndx == SHN_COMMON
   4391 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   4392 	       && !bfd_link_relocatable (info))
   4393 	{
   4394 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   4395 
   4396 	  if (tcomm == NULL)
   4397 	    {
   4398 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   4399 				 | SEC_LINKER_CREATED);
   4400 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   4401 	      if (tcomm == NULL)
   4402 		goto error_free_vers;
   4403 	    }
   4404 	  sec = tcomm;
   4405 	}
   4406       else if (bed->elf_add_symbol_hook)
   4407 	{
   4408 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   4409 					     &sec, &value))
   4410 	    goto error_free_vers;
   4411 
   4412 	  /* The hook function sets the name to NULL if this symbol
   4413 	     should be skipped for some reason.  */
   4414 	  if (name == NULL)
   4415 	    continue;
   4416 	}
   4417 
   4418       /* Sanity check that all possibilities were handled.  */
   4419       if (sec == NULL)
   4420 	{
   4421 	  bfd_set_error (bfd_error_bad_value);
   4422 	  goto error_free_vers;
   4423 	}
   4424 
   4425       /* Silently discard TLS symbols from --just-syms.  There's
   4426 	 no way to combine a static TLS block with a new TLS block
   4427 	 for this executable.  */
   4428       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   4429 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   4430 	continue;
   4431 
   4432       if (bfd_is_und_section (sec)
   4433 	  || bfd_is_com_section (sec))
   4434 	definition = FALSE;
   4435       else
   4436 	definition = TRUE;
   4437 
   4438       size_change_ok = FALSE;
   4439       type_change_ok = bed->type_change_ok;
   4440       old_weak = FALSE;
   4441       matched = FALSE;
   4442       old_alignment = 0;
   4443       old_bfd = NULL;
   4444       new_sec = sec;
   4445 
   4446       if (is_elf_hash_table (htab))
   4447 	{
   4448 	  Elf_Internal_Versym iver;
   4449 	  unsigned int vernum = 0;
   4450 	  bfd_boolean skip;
   4451 
   4452 	  if (ever == NULL)
   4453 	    {
   4454 	      if (info->default_imported_symver)
   4455 		/* Use the default symbol version created earlier.  */
   4456 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   4457 	      else
   4458 		iver.vs_vers = 0;
   4459 	    }
   4460 	  else
   4461 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   4462 
   4463 	  vernum = iver.vs_vers & VERSYM_VERSION;
   4464 
   4465 	  /* If this is a hidden symbol, or if it is not version
   4466 	     1, we append the version name to the symbol name.
   4467 	     However, we do not modify a non-hidden absolute symbol
   4468 	     if it is not a function, because it might be the version
   4469 	     symbol itself.  FIXME: What if it isn't?  */
   4470 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   4471 	      || (vernum > 1
   4472 		  && (!bfd_is_abs_section (sec)
   4473 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   4474 	    {
   4475 	      const char *verstr;
   4476 	      size_t namelen, verlen, newlen;
   4477 	      char *newname, *p;
   4478 
   4479 	      if (isym->st_shndx != SHN_UNDEF)
   4480 		{
   4481 		  if (vernum > elf_tdata (abfd)->cverdefs)
   4482 		    verstr = NULL;
   4483 		  else if (vernum > 1)
   4484 		    verstr =
   4485 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   4486 		  else
   4487 		    verstr = "";
   4488 
   4489 		  if (verstr == NULL)
   4490 		    {
   4491 		      _bfd_error_handler
   4492 			/* xgettext:c-format */
   4493 			(_("%B: %s: invalid version %u (max %d)"),
   4494 			 abfd, name, vernum,
   4495 			 elf_tdata (abfd)->cverdefs);
   4496 		      bfd_set_error (bfd_error_bad_value);
   4497 		      goto error_free_vers;
   4498 		    }
   4499 		}
   4500 	      else
   4501 		{
   4502 		  /* We cannot simply test for the number of
   4503 		     entries in the VERNEED section since the
   4504 		     numbers for the needed versions do not start
   4505 		     at 0.  */
   4506 		  Elf_Internal_Verneed *t;
   4507 
   4508 		  verstr = NULL;
   4509 		  for (t = elf_tdata (abfd)->verref;
   4510 		       t != NULL;
   4511 		       t = t->vn_nextref)
   4512 		    {
   4513 		      Elf_Internal_Vernaux *a;
   4514 
   4515 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   4516 			{
   4517 			  if (a->vna_other == vernum)
   4518 			    {
   4519 			      verstr = a->vna_nodename;
   4520 			      break;
   4521 			    }
   4522 			}
   4523 		      if (a != NULL)
   4524 			break;
   4525 		    }
   4526 		  if (verstr == NULL)
   4527 		    {
   4528 		      _bfd_error_handler
   4529 			/* xgettext:c-format */
   4530 			(_("%B: %s: invalid needed version %d"),
   4531 			 abfd, name, vernum);
   4532 		      bfd_set_error (bfd_error_bad_value);
   4533 		      goto error_free_vers;
   4534 		    }
   4535 		}
   4536 
   4537 	      namelen = strlen (name);
   4538 	      verlen = strlen (verstr);
   4539 	      newlen = namelen + verlen + 2;
   4540 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4541 		  && isym->st_shndx != SHN_UNDEF)
   4542 		++newlen;
   4543 
   4544 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   4545 	      if (newname == NULL)
   4546 		goto error_free_vers;
   4547 	      memcpy (newname, name, namelen);
   4548 	      p = newname + namelen;
   4549 	      *p++ = ELF_VER_CHR;
   4550 	      /* If this is a defined non-hidden version symbol,
   4551 		 we add another @ to the name.  This indicates the
   4552 		 default version of the symbol.  */
   4553 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4554 		  && isym->st_shndx != SHN_UNDEF)
   4555 		*p++ = ELF_VER_CHR;
   4556 	      memcpy (p, verstr, verlen + 1);
   4557 
   4558 	      name = newname;
   4559 	    }
   4560 
   4561 	  /* If this symbol has default visibility and the user has
   4562 	     requested we not re-export it, then mark it as hidden.  */
   4563 	  if (!bfd_is_und_section (sec)
   4564 	      && !dynamic
   4565 	      && abfd->no_export
   4566 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   4567 	    isym->st_other = (STV_HIDDEN
   4568 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   4569 
   4570 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   4571 				      sym_hash, &old_bfd, &old_weak,
   4572 				      &old_alignment, &skip, &override,
   4573 				      &type_change_ok, &size_change_ok,
   4574 				      &matched))
   4575 	    goto error_free_vers;
   4576 
   4577 	  if (skip)
   4578 	    continue;
   4579 
   4580 	  /* Override a definition only if the new symbol matches the
   4581 	     existing one.  */
   4582 	  if (override && matched)
   4583 	    definition = FALSE;
   4584 
   4585 	  h = *sym_hash;
   4586 	  while (h->root.type == bfd_link_hash_indirect
   4587 		 || h->root.type == bfd_link_hash_warning)
   4588 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4589 
   4590 	  if (elf_tdata (abfd)->verdef != NULL
   4591 	      && vernum > 1
   4592 	      && definition)
   4593 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   4594 	}
   4595 
   4596       if (! (_bfd_generic_link_add_one_symbol
   4597 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
   4598 	      (struct bfd_link_hash_entry **) sym_hash)))
   4599 	goto error_free_vers;
   4600 
   4601       if ((flags & BSF_GNU_UNIQUE)
   4602 	  && (abfd->flags & DYNAMIC) == 0
   4603 	  && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
   4604 	elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
   4605 
   4606       h = *sym_hash;
   4607       /* We need to make sure that indirect symbol dynamic flags are
   4608 	 updated.  */
   4609       hi = h;
   4610       while (h->root.type == bfd_link_hash_indirect
   4611 	     || h->root.type == bfd_link_hash_warning)
   4612 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4613 
   4614       /* Setting the index to -3 tells elf_link_output_extsym that
   4615 	 this symbol is defined in a discarded section.  */
   4616       if (discarded)
   4617 	h->indx = -3;
   4618 
   4619       *sym_hash = h;
   4620 
   4621       new_weak = (flags & BSF_WEAK) != 0;
   4622       if (dynamic
   4623 	  && definition
   4624 	  && new_weak
   4625 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   4626 	  && is_elf_hash_table (htab)
   4627 	  && h->u.alias == NULL)
   4628 	{
   4629 	  /* Keep a list of all weak defined non function symbols from
   4630 	     a dynamic object, using the alias field.  Later in this
   4631 	     function we will set the alias field to the correct
   4632 	     value.  We only put non-function symbols from dynamic
   4633 	     objects on this list, because that happens to be the only
   4634 	     time we need to know the normal symbol corresponding to a
   4635 	     weak symbol, and the information is time consuming to
   4636 	     figure out.  If the alias field is not already NULL,
   4637 	     then this symbol was already defined by some previous
   4638 	     dynamic object, and we will be using that previous
   4639 	     definition anyhow.  */
   4640 
   4641 	  h->u.alias = weaks;
   4642 	  weaks = h;
   4643 	}
   4644 
   4645       /* Set the alignment of a common symbol.  */
   4646       if ((common || bfd_is_com_section (sec))
   4647 	  && h->root.type == bfd_link_hash_common)
   4648 	{
   4649 	  unsigned int align;
   4650 
   4651 	  if (common)
   4652 	    align = bfd_log2 (isym->st_value);
   4653 	  else
   4654 	    {
   4655 	      /* The new symbol is a common symbol in a shared object.
   4656 		 We need to get the alignment from the section.  */
   4657 	      align = new_sec->alignment_power;
   4658 	    }
   4659 	  if (align > old_alignment)
   4660 	    h->root.u.c.p->alignment_power = align;
   4661 	  else
   4662 	    h->root.u.c.p->alignment_power = old_alignment;
   4663 	}
   4664 
   4665       if (is_elf_hash_table (htab))
   4666 	{
   4667 	  /* Set a flag in the hash table entry indicating the type of
   4668 	     reference or definition we just found.  A dynamic symbol
   4669 	     is one which is referenced or defined by both a regular
   4670 	     object and a shared object.  */
   4671 	  bfd_boolean dynsym = FALSE;
   4672 
   4673 	  /* Plugin symbols aren't normal.  Don't set def_regular or
   4674 	     ref_regular for them, or make them dynamic.  */
   4675 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4676 	    ;
   4677 	  else if (! dynamic)
   4678 	    {
   4679 	      if (! definition)
   4680 		{
   4681 		  h->ref_regular = 1;
   4682 		  if (bind != STB_WEAK)
   4683 		    h->ref_regular_nonweak = 1;
   4684 		}
   4685 	      else
   4686 		{
   4687 		  h->def_regular = 1;
   4688 		  if (h->def_dynamic)
   4689 		    {
   4690 		      h->def_dynamic = 0;
   4691 		      h->ref_dynamic = 1;
   4692 		    }
   4693 		}
   4694 
   4695 	      /* If the indirect symbol has been forced local, don't
   4696 		 make the real symbol dynamic.  */
   4697 	      if ((h == hi || !hi->forced_local)
   4698 		  && (bfd_link_dll (info)
   4699 		      || h->def_dynamic
   4700 		      || h->ref_dynamic))
   4701 		dynsym = TRUE;
   4702 	    }
   4703 	  else
   4704 	    {
   4705 	      if (! definition)
   4706 		{
   4707 		  h->ref_dynamic = 1;
   4708 		  hi->ref_dynamic = 1;
   4709 		}
   4710 	      else
   4711 		{
   4712 		  h->def_dynamic = 1;
   4713 		  hi->def_dynamic = 1;
   4714 		}
   4715 
   4716 	      /* If the indirect symbol has been forced local, don't
   4717 		 make the real symbol dynamic.  */
   4718 	      if ((h == hi || !hi->forced_local)
   4719 		  && (h->def_regular
   4720 		      || h->ref_regular
   4721 		      || (h->is_weakalias
   4722 			  && weakdef (h)->dynindx != -1)))
   4723 		dynsym = TRUE;
   4724 	    }
   4725 
   4726 	  /* Check to see if we need to add an indirect symbol for
   4727 	     the default name.  */
   4728 	  if (definition
   4729 	      || (!override && h->root.type == bfd_link_hash_common))
   4730 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   4731 					      sec, value, &old_bfd, &dynsym))
   4732 	      goto error_free_vers;
   4733 
   4734 	  /* Check the alignment when a common symbol is involved. This
   4735 	     can change when a common symbol is overridden by a normal
   4736 	     definition or a common symbol is ignored due to the old
   4737 	     normal definition. We need to make sure the maximum
   4738 	     alignment is maintained.  */
   4739 	  if ((old_alignment || common)
   4740 	      && h->root.type != bfd_link_hash_common)
   4741 	    {
   4742 	      unsigned int common_align;
   4743 	      unsigned int normal_align;
   4744 	      unsigned int symbol_align;
   4745 	      bfd *normal_bfd;
   4746 	      bfd *common_bfd;
   4747 
   4748 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   4749 			  || h->root.type == bfd_link_hash_defweak);
   4750 
   4751 	      symbol_align = ffs (h->root.u.def.value) - 1;
   4752 	      if (h->root.u.def.section->owner != NULL
   4753 		  && (h->root.u.def.section->owner->flags
   4754 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
   4755 		{
   4756 		  normal_align = h->root.u.def.section->alignment_power;
   4757 		  if (normal_align > symbol_align)
   4758 		    normal_align = symbol_align;
   4759 		}
   4760 	      else
   4761 		normal_align = symbol_align;
   4762 
   4763 	      if (old_alignment)
   4764 		{
   4765 		  common_align = old_alignment;
   4766 		  common_bfd = old_bfd;
   4767 		  normal_bfd = abfd;
   4768 		}
   4769 	      else
   4770 		{
   4771 		  common_align = bfd_log2 (isym->st_value);
   4772 		  common_bfd = abfd;
   4773 		  normal_bfd = old_bfd;
   4774 		}
   4775 
   4776 	      if (normal_align < common_align)
   4777 		{
   4778 		  /* PR binutils/2735 */
   4779 		  if (normal_bfd == NULL)
   4780 		    _bfd_error_handler
   4781 		      /* xgettext:c-format */
   4782 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
   4783 			 " greater than the alignment (%u) of its section %A"),
   4784 		       1 << common_align, name, common_bfd,
   4785 		       1 << normal_align, h->root.u.def.section);
   4786 		  else
   4787 		    _bfd_error_handler
   4788 		      /* xgettext:c-format */
   4789 		      (_("Warning: alignment %u of symbol `%s' in %B"
   4790 			 " is smaller than %u in %B"),
   4791 		       1 << normal_align, name, normal_bfd,
   4792 		       1 << common_align, common_bfd);
   4793 		}
   4794 	    }
   4795 
   4796 	  /* Remember the symbol size if it isn't undefined.  */
   4797 	  if (isym->st_size != 0
   4798 	      && isym->st_shndx != SHN_UNDEF
   4799 	      && (definition || h->size == 0))
   4800 	    {
   4801 	      if (h->size != 0
   4802 		  && h->size != isym->st_size
   4803 		  && ! size_change_ok)
   4804 		_bfd_error_handler
   4805 		  /* xgettext:c-format */
   4806 		  (_("Warning: size of symbol `%s' changed"
   4807 		     " from %Lu in %B to %Lu in %B"),
   4808 		   name, h->size, old_bfd, isym->st_size, abfd);
   4809 
   4810 	      h->size = isym->st_size;
   4811 	    }
   4812 
   4813 	  /* If this is a common symbol, then we always want H->SIZE
   4814 	     to be the size of the common symbol.  The code just above
   4815 	     won't fix the size if a common symbol becomes larger.  We
   4816 	     don't warn about a size change here, because that is
   4817 	     covered by --warn-common.  Allow changes between different
   4818 	     function types.  */
   4819 	  if (h->root.type == bfd_link_hash_common)
   4820 	    h->size = h->root.u.c.size;
   4821 
   4822 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   4823 	      && ((definition && !new_weak)
   4824 		  || (old_weak && h->root.type == bfd_link_hash_common)
   4825 		  || h->type == STT_NOTYPE))
   4826 	    {
   4827 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   4828 
   4829 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   4830 		 symbol.  */
   4831 	      if (type == STT_GNU_IFUNC
   4832 		  && (abfd->flags & DYNAMIC) != 0)
   4833 		type = STT_FUNC;
   4834 
   4835 	      if (h->type != type)
   4836 		{
   4837 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   4838 		    /* xgettext:c-format */
   4839 		    _bfd_error_handler
   4840 		      (_("Warning: type of symbol `%s' changed"
   4841 			 " from %d to %d in %B"),
   4842 		       name, h->type, type, abfd);
   4843 
   4844 		  h->type = type;
   4845 		}
   4846 	    }
   4847 
   4848 	  /* Merge st_other field.  */
   4849 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
   4850 
   4851 	  /* We don't want to make debug symbol dynamic.  */
   4852 	  if (definition
   4853 	      && (sec->flags & SEC_DEBUGGING)
   4854 	      && !bfd_link_relocatable (info))
   4855 	    dynsym = FALSE;
   4856 
   4857 	  /* Nor should we make plugin symbols dynamic.  */
   4858 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4859 	    dynsym = FALSE;
   4860 
   4861 	  if (definition)
   4862 	    {
   4863 	      h->target_internal = isym->st_target_internal;
   4864 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   4865 	    }
   4866 
   4867 	  if (definition && !dynamic)
   4868 	    {
   4869 	      char *p = strchr (name, ELF_VER_CHR);
   4870 	      if (p != NULL && p[1] != ELF_VER_CHR)
   4871 		{
   4872 		  /* Queue non-default versions so that .symver x, x@FOO
   4873 		     aliases can be checked.  */
   4874 		  if (!nondeflt_vers)
   4875 		    {
   4876 		      amt = ((isymend - isym + 1)
   4877 			     * sizeof (struct elf_link_hash_entry *));
   4878 		      nondeflt_vers
   4879 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   4880 		      if (!nondeflt_vers)
   4881 			goto error_free_vers;
   4882 		    }
   4883 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   4884 		}
   4885 	    }
   4886 
   4887 	  if (dynsym && h->dynindx == -1)
   4888 	    {
   4889 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4890 		goto error_free_vers;
   4891 	      if (h->is_weakalias
   4892 		  && weakdef (h)->dynindx == -1)
   4893 		{
   4894 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
   4895 		    goto error_free_vers;
   4896 		}
   4897 	    }
   4898 	  else if (h->dynindx != -1)
   4899 	    /* If the symbol already has a dynamic index, but
   4900 	       visibility says it should not be visible, turn it into
   4901 	       a local symbol.  */
   4902 	    switch (ELF_ST_VISIBILITY (h->other))
   4903 	      {
   4904 	      case STV_INTERNAL:
   4905 	      case STV_HIDDEN:
   4906 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   4907 		dynsym = FALSE;
   4908 		break;
   4909 	      }
   4910 
   4911 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
   4912 	     for unmatched symbol.  */
   4913 	  if (!add_needed
   4914 	      && matched
   4915 	      && definition
   4916 	      && ((dynsym
   4917 		   && h->ref_regular_nonweak
   4918 		   && (old_bfd == NULL
   4919 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
   4920 		  || (h->ref_dynamic_nonweak
   4921 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   4922 		      && !on_needed_list (elf_dt_name (abfd),
   4923 					  htab->needed, NULL))))
   4924 	    {
   4925 	      int ret;
   4926 	      const char *soname = elf_dt_name (abfd);
   4927 
   4928 	      info->callbacks->minfo ("%!", soname, old_bfd,
   4929 				      h->root.root.string);
   4930 
   4931 	      /* A symbol from a library loaded via DT_NEEDED of some
   4932 		 other library is referenced by a regular object.
   4933 		 Add a DT_NEEDED entry for it.  Issue an error if
   4934 		 --no-add-needed is used and the reference was not
   4935 		 a weak one.  */
   4936 	      if (old_bfd != NULL
   4937 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   4938 		{
   4939 		  _bfd_error_handler
   4940 		    /* xgettext:c-format */
   4941 		    (_("%B: undefined reference to symbol '%s'"),
   4942 		     old_bfd, name);
   4943 		  bfd_set_error (bfd_error_missing_dso);
   4944 		  goto error_free_vers;
   4945 		}
   4946 
   4947 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   4948 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   4949 
   4950 	      add_needed = TRUE;
   4951 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   4952 	      if (ret < 0)
   4953 		goto error_free_vers;
   4954 
   4955 	      BFD_ASSERT (ret == 0);
   4956 	    }
   4957 	}
   4958     }
   4959 
   4960   if (info->lto_plugin_active
   4961       && !bfd_link_relocatable (info)
   4962       && (abfd->flags & BFD_PLUGIN) == 0
   4963       && !just_syms
   4964       && extsymcount)
   4965     {
   4966       int r_sym_shift;
   4967 
   4968       if (bed->s->arch_size == 32)
   4969 	r_sym_shift = 8;
   4970       else
   4971 	r_sym_shift = 32;
   4972 
   4973       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
   4974 	 referenced in regular objects so that linker plugin will get
   4975 	 the correct symbol resolution.  */
   4976 
   4977       sym_hash = elf_sym_hashes (abfd);
   4978       for (s = abfd->sections; s != NULL; s = s->next)
   4979 	{
   4980 	  Elf_Internal_Rela *internal_relocs;
   4981 	  Elf_Internal_Rela *rel, *relend;
   4982 
   4983 	  /* Don't check relocations in excluded sections.  */
   4984 	  if ((s->flags & SEC_RELOC) == 0
   4985 	      || s->reloc_count == 0
   4986 	      || (s->flags & SEC_EXCLUDE) != 0
   4987 	      || ((info->strip == strip_all
   4988 		   || info->strip == strip_debugger)
   4989 		  && (s->flags & SEC_DEBUGGING) != 0))
   4990 	    continue;
   4991 
   4992 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
   4993 						       NULL,
   4994 						       info->keep_memory);
   4995 	  if (internal_relocs == NULL)
   4996 	    goto error_free_vers;
   4997 
   4998 	  rel = internal_relocs;
   4999 	  relend = rel + s->reloc_count;
   5000 	  for ( ; rel < relend; rel++)
   5001 	    {
   5002 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   5003 	      struct elf_link_hash_entry *h;
   5004 
   5005 	      /* Skip local symbols.  */
   5006 	      if (r_symndx < extsymoff)
   5007 		continue;
   5008 
   5009 	      h = sym_hash[r_symndx - extsymoff];
   5010 	      if (h != NULL)
   5011 		h->root.non_ir_ref_regular = 1;
   5012 	    }
   5013 
   5014 	  if (elf_section_data (s)->relocs != internal_relocs)
   5015 	    free (internal_relocs);
   5016 	}
   5017     }
   5018 
   5019   if (extversym != NULL)
   5020     {
   5021       free (extversym);
   5022       extversym = NULL;
   5023     }
   5024 
   5025   if (isymbuf != NULL)
   5026     {
   5027       free (isymbuf);
   5028       isymbuf = NULL;
   5029     }
   5030 
   5031   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   5032     {
   5033       unsigned int i;
   5034 
   5035       /* Restore the symbol table.  */
   5036       old_ent = (char *) old_tab + tabsize;
   5037       memset (elf_sym_hashes (abfd), 0,
   5038 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   5039       htab->root.table.table = old_table;
   5040       htab->root.table.size = old_size;
   5041       htab->root.table.count = old_count;
   5042       memcpy (htab->root.table.table, old_tab, tabsize);
   5043       htab->root.undefs = old_undefs;
   5044       htab->root.undefs_tail = old_undefs_tail;
   5045       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   5046       free (old_strtab);
   5047       old_strtab = NULL;
   5048       for (i = 0; i < htab->root.table.size; i++)
   5049 	{
   5050 	  struct bfd_hash_entry *p;
   5051 	  struct elf_link_hash_entry *h;
   5052 	  bfd_size_type size;
   5053 	  unsigned int alignment_power;
   5054 	  unsigned int non_ir_ref_dynamic;
   5055 
   5056 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   5057 	    {
   5058 	      h = (struct elf_link_hash_entry *) p;
   5059 	      if (h->root.type == bfd_link_hash_warning)
   5060 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5061 
   5062 	      /* Preserve the maximum alignment and size for common
   5063 		 symbols even if this dynamic lib isn't on DT_NEEDED
   5064 		 since it can still be loaded at run time by another
   5065 		 dynamic lib.  */
   5066 	      if (h->root.type == bfd_link_hash_common)
   5067 		{
   5068 		  size = h->root.u.c.size;
   5069 		  alignment_power = h->root.u.c.p->alignment_power;
   5070 		}
   5071 	      else
   5072 		{
   5073 		  size = 0;
   5074 		  alignment_power = 0;
   5075 		}
   5076 	      /* Preserve non_ir_ref_dynamic so that this symbol
   5077 		 will be exported when the dynamic lib becomes needed
   5078 		 in the second pass.  */
   5079 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
   5080 	      memcpy (p, old_ent, htab->root.table.entsize);
   5081 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   5082 	      h = (struct elf_link_hash_entry *) p;
   5083 	      if (h->root.type == bfd_link_hash_warning)
   5084 		{
   5085 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
   5086 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   5087 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5088 		}
   5089 	      if (h->root.type == bfd_link_hash_common)
   5090 		{
   5091 		  if (size > h->root.u.c.size)
   5092 		    h->root.u.c.size = size;
   5093 		  if (alignment_power > h->root.u.c.p->alignment_power)
   5094 		    h->root.u.c.p->alignment_power = alignment_power;
   5095 		}
   5096 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
   5097 	    }
   5098 	}
   5099 
   5100       /* Make a special call to the linker "notice" function to
   5101 	 tell it that symbols added for crefs may need to be removed.  */
   5102       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   5103 	goto error_free_vers;
   5104 
   5105       free (old_tab);
   5106       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   5107 			   alloc_mark);
   5108       if (nondeflt_vers != NULL)
   5109 	free (nondeflt_vers);
   5110       return TRUE;
   5111     }
   5112 
   5113   if (old_tab != NULL)
   5114     {
   5115       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   5116 	goto error_free_vers;
   5117       free (old_tab);
   5118       old_tab = NULL;
   5119     }
   5120 
   5121   /* Now that all the symbols from this input file are created, if
   5122      not performing a relocatable link, handle .symver foo, foo@BAR
   5123      such that any relocs against foo become foo@BAR.  */
   5124   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   5125     {
   5126       size_t cnt, symidx;
   5127 
   5128       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   5129 	{
   5130 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   5131 	  char *shortname, *p;
   5132 
   5133 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   5134 	  if (p == NULL
   5135 	      || (h->root.type != bfd_link_hash_defined
   5136 		  && h->root.type != bfd_link_hash_defweak))
   5137 	    continue;
   5138 
   5139 	  amt = p - h->root.root.string;
   5140 	  shortname = (char *) bfd_malloc (amt + 1);
   5141 	  if (!shortname)
   5142 	    goto error_free_vers;
   5143 	  memcpy (shortname, h->root.root.string, amt);
   5144 	  shortname[amt] = '\0';
   5145 
   5146 	  hi = (struct elf_link_hash_entry *)
   5147 	       bfd_link_hash_lookup (&htab->root, shortname,
   5148 				     FALSE, FALSE, FALSE);
   5149 	  if (hi != NULL
   5150 	      && hi->root.type == h->root.type
   5151 	      && hi->root.u.def.value == h->root.u.def.value
   5152 	      && hi->root.u.def.section == h->root.u.def.section)
   5153 	    {
   5154 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   5155 	      hi->root.type = bfd_link_hash_indirect;
   5156 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   5157 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   5158 	      sym_hash = elf_sym_hashes (abfd);
   5159 	      if (sym_hash)
   5160 		for (symidx = 0; symidx < extsymcount; ++symidx)
   5161 		  if (sym_hash[symidx] == hi)
   5162 		    {
   5163 		      sym_hash[symidx] = h;
   5164 		      break;
   5165 		    }
   5166 	    }
   5167 	  free (shortname);
   5168 	}
   5169       free (nondeflt_vers);
   5170       nondeflt_vers = NULL;
   5171     }
   5172 
   5173   /* Now set the alias field correctly for all the weak defined
   5174      symbols we found.  The only way to do this is to search all the
   5175      symbols.  Since we only need the information for non functions in
   5176      dynamic objects, that's the only time we actually put anything on
   5177      the list WEAKS.  We need this information so that if a regular
   5178      object refers to a symbol defined weakly in a dynamic object, the
   5179      real symbol in the dynamic object is also put in the dynamic
   5180      symbols; we also must arrange for both symbols to point to the
   5181      same memory location.  We could handle the general case of symbol
   5182      aliasing, but a general symbol alias can only be generated in
   5183      assembler code, handling it correctly would be very time
   5184      consuming, and other ELF linkers don't handle general aliasing
   5185      either.  */
   5186   if (weaks != NULL)
   5187     {
   5188       struct elf_link_hash_entry **hpp;
   5189       struct elf_link_hash_entry **hppend;
   5190       struct elf_link_hash_entry **sorted_sym_hash;
   5191       struct elf_link_hash_entry *h;
   5192       size_t sym_count;
   5193 
   5194       /* Since we have to search the whole symbol list for each weak
   5195 	 defined symbol, search time for N weak defined symbols will be
   5196 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   5197       amt = extsymcount;
   5198       amt *= sizeof (struct elf_link_hash_entry *);
   5199       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
   5200       if (sorted_sym_hash == NULL)
   5201 	goto error_return;
   5202       sym_hash = sorted_sym_hash;
   5203       hpp = elf_sym_hashes (abfd);
   5204       hppend = hpp + extsymcount;
   5205       sym_count = 0;
   5206       for (; hpp < hppend; hpp++)
   5207 	{
   5208 	  h = *hpp;
   5209 	  if (h != NULL
   5210 	      && h->root.type == bfd_link_hash_defined
   5211 	      && !bed->is_function_type (h->type))
   5212 	    {
   5213 	      *sym_hash = h;
   5214 	      sym_hash++;
   5215 	      sym_count++;
   5216 	    }
   5217 	}
   5218 
   5219       qsort (sorted_sym_hash, sym_count,
   5220 	     sizeof (struct elf_link_hash_entry *),
   5221 	     elf_sort_symbol);
   5222 
   5223       while (weaks != NULL)
   5224 	{
   5225 	  struct elf_link_hash_entry *hlook;
   5226 	  asection *slook;
   5227 	  bfd_vma vlook;
   5228 	  size_t i, j, idx = 0;
   5229 
   5230 	  hlook = weaks;
   5231 	  weaks = hlook->u.alias;
   5232 	  hlook->u.alias = NULL;
   5233 
   5234 	  if (hlook->root.type != bfd_link_hash_defined
   5235 	      && hlook->root.type != bfd_link_hash_defweak)
   5236 	    continue;
   5237 
   5238 	  slook = hlook->root.u.def.section;
   5239 	  vlook = hlook->root.u.def.value;
   5240 
   5241 	  i = 0;
   5242 	  j = sym_count;
   5243 	  while (i != j)
   5244 	    {
   5245 	      bfd_signed_vma vdiff;
   5246 	      idx = (i + j) / 2;
   5247 	      h = sorted_sym_hash[idx];
   5248 	      vdiff = vlook - h->root.u.def.value;
   5249 	      if (vdiff < 0)
   5250 		j = idx;
   5251 	      else if (vdiff > 0)
   5252 		i = idx + 1;
   5253 	      else
   5254 		{
   5255 		  int sdiff = slook->id - h->root.u.def.section->id;
   5256 		  if (sdiff < 0)
   5257 		    j = idx;
   5258 		  else if (sdiff > 0)
   5259 		    i = idx + 1;
   5260 		  else
   5261 		    break;
   5262 		}
   5263 	    }
   5264 
   5265 	  /* We didn't find a value/section match.  */
   5266 	  if (i == j)
   5267 	    continue;
   5268 
   5269 	  /* With multiple aliases, or when the weak symbol is already
   5270 	     strongly defined, we have multiple matching symbols and
   5271 	     the binary search above may land on any of them.  Step
   5272 	     one past the matching symbol(s).  */
   5273 	  while (++idx != j)
   5274 	    {
   5275 	      h = sorted_sym_hash[idx];
   5276 	      if (h->root.u.def.section != slook
   5277 		  || h->root.u.def.value != vlook)
   5278 		break;
   5279 	    }
   5280 
   5281 	  /* Now look back over the aliases.  Since we sorted by size
   5282 	     as well as value and section, we'll choose the one with
   5283 	     the largest size.  */
   5284 	  while (idx-- != i)
   5285 	    {
   5286 	      h = sorted_sym_hash[idx];
   5287 
   5288 	      /* Stop if value or section doesn't match.  */
   5289 	      if (h->root.u.def.section != slook
   5290 		  || h->root.u.def.value != vlook)
   5291 		break;
   5292 	      else if (h != hlook)
   5293 		{
   5294 		  struct elf_link_hash_entry *t;
   5295 
   5296 		  hlook->u.alias = h;
   5297 		  hlook->is_weakalias = 1;
   5298 		  t = h;
   5299 		  if (t->u.alias != NULL)
   5300 		    while (t->u.alias != h)
   5301 		      t = t->u.alias;
   5302 		  t->u.alias = hlook;
   5303 
   5304 		  /* If the weak definition is in the list of dynamic
   5305 		     symbols, make sure the real definition is put
   5306 		     there as well.  */
   5307 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   5308 		    {
   5309 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   5310 			{
   5311 			err_free_sym_hash:
   5312 			  free (sorted_sym_hash);
   5313 			  goto error_return;
   5314 			}
   5315 		    }
   5316 
   5317 		  /* If the real definition is in the list of dynamic
   5318 		     symbols, make sure the weak definition is put
   5319 		     there as well.  If we don't do this, then the
   5320 		     dynamic loader might not merge the entries for the
   5321 		     real definition and the weak definition.  */
   5322 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   5323 		    {
   5324 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   5325 			goto err_free_sym_hash;
   5326 		    }
   5327 		  break;
   5328 		}
   5329 	    }
   5330 	}
   5331 
   5332       free (sorted_sym_hash);
   5333     }
   5334 
   5335   if (bed->check_directives
   5336       && !(*bed->check_directives) (abfd, info))
   5337     return FALSE;
   5338 
   5339   /* If this is a non-traditional link, try to optimize the handling
   5340      of the .stab/.stabstr sections.  */
   5341   if (! dynamic
   5342       && ! info->traditional_format
   5343       && is_elf_hash_table (htab)
   5344       && (info->strip != strip_all && info->strip != strip_debugger))
   5345     {
   5346       asection *stabstr;
   5347 
   5348       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   5349       if (stabstr != NULL)
   5350 	{
   5351 	  bfd_size_type string_offset = 0;
   5352 	  asection *stab;
   5353 
   5354 	  for (stab = abfd->sections; stab; stab = stab->next)
   5355 	    if (CONST_STRNEQ (stab->name, ".stab")
   5356 		&& (!stab->name[5] ||
   5357 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   5358 		&& (stab->flags & SEC_MERGE) == 0
   5359 		&& !bfd_is_abs_section (stab->output_section))
   5360 	      {
   5361 		struct bfd_elf_section_data *secdata;
   5362 
   5363 		secdata = elf_section_data (stab);
   5364 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   5365 					       stabstr, &secdata->sec_info,
   5366 					       &string_offset))
   5367 		  goto error_return;
   5368 		if (secdata->sec_info)
   5369 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   5370 	    }
   5371 	}
   5372     }
   5373 
   5374   if (is_elf_hash_table (htab) && add_needed)
   5375     {
   5376       /* Add this bfd to the loaded list.  */
   5377       struct elf_link_loaded_list *n;
   5378 
   5379       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   5380       if (n == NULL)
   5381 	goto error_return;
   5382       n->abfd = abfd;
   5383       n->next = htab->loaded;
   5384       htab->loaded = n;
   5385     }
   5386 
   5387   return TRUE;
   5388 
   5389  error_free_vers:
   5390   if (old_tab != NULL)
   5391     free (old_tab);
   5392   if (old_strtab != NULL)
   5393     free (old_strtab);
   5394   if (nondeflt_vers != NULL)
   5395     free (nondeflt_vers);
   5396   if (extversym != NULL)
   5397     free (extversym);
   5398  error_free_sym:
   5399   if (isymbuf != NULL)
   5400     free (isymbuf);
   5401  error_return:
   5402   return FALSE;
   5403 }
   5404 
   5405 /* Return the linker hash table entry of a symbol that might be
   5406    satisfied by an archive symbol.  Return -1 on error.  */
   5407 
   5408 struct elf_link_hash_entry *
   5409 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   5410 				struct bfd_link_info *info,
   5411 				const char *name)
   5412 {
   5413   struct elf_link_hash_entry *h;
   5414   char *p, *copy;
   5415   size_t len, first;
   5416 
   5417   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
   5418   if (h != NULL)
   5419     return h;
   5420 
   5421   /* If this is a default version (the name contains @@), look up the
   5422      symbol again with only one `@' as well as without the version.
   5423      The effect is that references to the symbol with and without the
   5424      version will be matched by the default symbol in the archive.  */
   5425 
   5426   p = strchr (name, ELF_VER_CHR);
   5427   if (p == NULL || p[1] != ELF_VER_CHR)
   5428     return h;
   5429 
   5430   /* First check with only one `@'.  */
   5431   len = strlen (name);
   5432   copy = (char *) bfd_alloc (abfd, len);
   5433   if (copy == NULL)
   5434     return (struct elf_link_hash_entry *) 0 - 1;
   5435 
   5436   first = p - name + 1;
   5437   memcpy (copy, name, first);
   5438   memcpy (copy + first, name + first + 1, len - first);
   5439 
   5440   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
   5441   if (h == NULL)
   5442     {
   5443       /* We also need to check references to the symbol without the
   5444 	 version.  */
   5445       copy[first - 1] = '\0';
   5446       h = elf_link_hash_lookup (elf_hash_table (info), copy,
   5447 				FALSE, FALSE, TRUE);
   5448     }
   5449 
   5450   bfd_release (abfd, copy);
   5451   return h;
   5452 }
   5453 
   5454 /* Add symbols from an ELF archive file to the linker hash table.  We
   5455    don't use _bfd_generic_link_add_archive_symbols because we need to
   5456    handle versioned symbols.
   5457 
   5458    Fortunately, ELF archive handling is simpler than that done by
   5459    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   5460    oddities.  In ELF, if we find a symbol in the archive map, and the
   5461    symbol is currently undefined, we know that we must pull in that
   5462    object file.
   5463 
   5464    Unfortunately, we do have to make multiple passes over the symbol
   5465    table until nothing further is resolved.  */
   5466 
   5467 static bfd_boolean
   5468 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   5469 {
   5470   symindex c;
   5471   unsigned char *included = NULL;
   5472   carsym *symdefs;
   5473   bfd_boolean loop;
   5474   bfd_size_type amt;
   5475   const struct elf_backend_data *bed;
   5476   struct elf_link_hash_entry * (*archive_symbol_lookup)
   5477     (bfd *, struct bfd_link_info *, const char *);
   5478 
   5479   if (! bfd_has_map (abfd))
   5480     {
   5481       /* An empty archive is a special case.  */
   5482       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   5483 	return TRUE;
   5484       bfd_set_error (bfd_error_no_armap);
   5485       return FALSE;
   5486     }
   5487 
   5488   /* Keep track of all symbols we know to be already defined, and all
   5489      files we know to be already included.  This is to speed up the
   5490      second and subsequent passes.  */
   5491   c = bfd_ardata (abfd)->symdef_count;
   5492   if (c == 0)
   5493     return TRUE;
   5494   amt = c;
   5495   amt *= sizeof (*included);
   5496   included = (unsigned char *) bfd_zmalloc (amt);
   5497   if (included == NULL)
   5498     return FALSE;
   5499 
   5500   symdefs = bfd_ardata (abfd)->symdefs;
   5501   bed = get_elf_backend_data (abfd);
   5502   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   5503 
   5504   do
   5505     {
   5506       file_ptr last;
   5507       symindex i;
   5508       carsym *symdef;
   5509       carsym *symdefend;
   5510 
   5511       loop = FALSE;
   5512       last = -1;
   5513 
   5514       symdef = symdefs;
   5515       symdefend = symdef + c;
   5516       for (i = 0; symdef < symdefend; symdef++, i++)
   5517 	{
   5518 	  struct elf_link_hash_entry *h;
   5519 	  bfd *element;
   5520 	  struct bfd_link_hash_entry *undefs_tail;
   5521 	  symindex mark;
   5522 
   5523 	  if (included[i])
   5524 	    continue;
   5525 	  if (symdef->file_offset == last)
   5526 	    {
   5527 	      included[i] = TRUE;
   5528 	      continue;
   5529 	    }
   5530 
   5531 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   5532 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
   5533 	    goto error_return;
   5534 
   5535 	  if (h == NULL)
   5536 	    continue;
   5537 
   5538 	  if (h->root.type == bfd_link_hash_common)
   5539 	    {
   5540 	      /* We currently have a common symbol.  The archive map contains
   5541 		 a reference to this symbol, so we may want to include it.  We
   5542 		 only want to include it however, if this archive element
   5543 		 contains a definition of the symbol, not just another common
   5544 		 declaration of it.
   5545 
   5546 		 Unfortunately some archivers (including GNU ar) will put
   5547 		 declarations of common symbols into their archive maps, as
   5548 		 well as real definitions, so we cannot just go by the archive
   5549 		 map alone.  Instead we must read in the element's symbol
   5550 		 table and check that to see what kind of symbol definition
   5551 		 this is.  */
   5552 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   5553 		continue;
   5554 	    }
   5555 	  else if (h->root.type != bfd_link_hash_undefined)
   5556 	    {
   5557 	      if (h->root.type != bfd_link_hash_undefweak)
   5558 		/* Symbol must be defined.  Don't check it again.  */
   5559 		included[i] = TRUE;
   5560 	      continue;
   5561 	    }
   5562 
   5563 	  /* We need to include this archive member.  */
   5564 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   5565 	  if (element == NULL)
   5566 	    goto error_return;
   5567 
   5568 	  if (! bfd_check_format (element, bfd_object))
   5569 	    goto error_return;
   5570 
   5571 	  undefs_tail = info->hash->undefs_tail;
   5572 
   5573 	  if (!(*info->callbacks
   5574 		->add_archive_element) (info, element, symdef->name, &element))
   5575 	    continue;
   5576 	  if (!bfd_link_add_symbols (element, info))
   5577 	    goto error_return;
   5578 
   5579 	  /* If there are any new undefined symbols, we need to make
   5580 	     another pass through the archive in order to see whether
   5581 	     they can be defined.  FIXME: This isn't perfect, because
   5582 	     common symbols wind up on undefs_tail and because an
   5583 	     undefined symbol which is defined later on in this pass
   5584 	     does not require another pass.  This isn't a bug, but it
   5585 	     does make the code less efficient than it could be.  */
   5586 	  if (undefs_tail != info->hash->undefs_tail)
   5587 	    loop = TRUE;
   5588 
   5589 	  /* Look backward to mark all symbols from this object file
   5590 	     which we have already seen in this pass.  */
   5591 	  mark = i;
   5592 	  do
   5593 	    {
   5594 	      included[mark] = TRUE;
   5595 	      if (mark == 0)
   5596 		break;
   5597 	      --mark;
   5598 	    }
   5599 	  while (symdefs[mark].file_offset == symdef->file_offset);
   5600 
   5601 	  /* We mark subsequent symbols from this object file as we go
   5602 	     on through the loop.  */
   5603 	  last = symdef->file_offset;
   5604 	}
   5605     }
   5606   while (loop);
   5607 
   5608   free (included);
   5609 
   5610   return TRUE;
   5611 
   5612  error_return:
   5613   if (included != NULL)
   5614     free (included);
   5615   return FALSE;
   5616 }
   5617 
   5618 /* Given an ELF BFD, add symbols to the global hash table as
   5619    appropriate.  */
   5620 
   5621 bfd_boolean
   5622 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   5623 {
   5624   switch (bfd_get_format (abfd))
   5625     {
   5626     case bfd_object:
   5627       return elf_link_add_object_symbols (abfd, info);
   5628     case bfd_archive:
   5629       return elf_link_add_archive_symbols (abfd, info);
   5630     default:
   5631       bfd_set_error (bfd_error_wrong_format);
   5632       return FALSE;
   5633     }
   5634 }
   5635 
   5636 struct hash_codes_info
   5638 {
   5639   unsigned long *hashcodes;
   5640   bfd_boolean error;
   5641 };
   5642 
   5643 /* This function will be called though elf_link_hash_traverse to store
   5644    all hash value of the exported symbols in an array.  */
   5645 
   5646 static bfd_boolean
   5647 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   5648 {
   5649   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   5650   const char *name;
   5651   unsigned long ha;
   5652   char *alc = NULL;
   5653 
   5654   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5655   if (h->dynindx == -1)
   5656     return TRUE;
   5657 
   5658   name = h->root.root.string;
   5659   if (h->versioned >= versioned)
   5660     {
   5661       char *p = strchr (name, ELF_VER_CHR);
   5662       if (p != NULL)
   5663 	{
   5664 	  alc = (char *) bfd_malloc (p - name + 1);
   5665 	  if (alc == NULL)
   5666 	    {
   5667 	      inf->error = TRUE;
   5668 	      return FALSE;
   5669 	    }
   5670 	  memcpy (alc, name, p - name);
   5671 	  alc[p - name] = '\0';
   5672 	  name = alc;
   5673 	}
   5674     }
   5675 
   5676   /* Compute the hash value.  */
   5677   ha = bfd_elf_hash (name);
   5678 
   5679   /* Store the found hash value in the array given as the argument.  */
   5680   *(inf->hashcodes)++ = ha;
   5681 
   5682   /* And store it in the struct so that we can put it in the hash table
   5683      later.  */
   5684   h->u.elf_hash_value = ha;
   5685 
   5686   if (alc != NULL)
   5687     free (alc);
   5688 
   5689   return TRUE;
   5690 }
   5691 
   5692 struct collect_gnu_hash_codes
   5693 {
   5694   bfd *output_bfd;
   5695   const struct elf_backend_data *bed;
   5696   unsigned long int nsyms;
   5697   unsigned long int maskbits;
   5698   unsigned long int *hashcodes;
   5699   unsigned long int *hashval;
   5700   unsigned long int *indx;
   5701   unsigned long int *counts;
   5702   bfd_vma *bitmask;
   5703   bfd_byte *contents;
   5704   long int min_dynindx;
   5705   unsigned long int bucketcount;
   5706   unsigned long int symindx;
   5707   long int local_indx;
   5708   long int shift1, shift2;
   5709   unsigned long int mask;
   5710   bfd_boolean error;
   5711 };
   5712 
   5713 /* This function will be called though elf_link_hash_traverse to store
   5714    all hash value of the exported symbols in an array.  */
   5715 
   5716 static bfd_boolean
   5717 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   5718 {
   5719   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5720   const char *name;
   5721   unsigned long ha;
   5722   char *alc = NULL;
   5723 
   5724   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5725   if (h->dynindx == -1)
   5726     return TRUE;
   5727 
   5728   /* Ignore also local symbols and undefined symbols.  */
   5729   if (! (*s->bed->elf_hash_symbol) (h))
   5730     return TRUE;
   5731 
   5732   name = h->root.root.string;
   5733   if (h->versioned >= versioned)
   5734     {
   5735       char *p = strchr (name, ELF_VER_CHR);
   5736       if (p != NULL)
   5737 	{
   5738 	  alc = (char *) bfd_malloc (p - name + 1);
   5739 	  if (alc == NULL)
   5740 	    {
   5741 	      s->error = TRUE;
   5742 	      return FALSE;
   5743 	    }
   5744 	  memcpy (alc, name, p - name);
   5745 	  alc[p - name] = '\0';
   5746 	  name = alc;
   5747 	}
   5748     }
   5749 
   5750   /* Compute the hash value.  */
   5751   ha = bfd_elf_gnu_hash (name);
   5752 
   5753   /* Store the found hash value in the array for compute_bucket_count,
   5754      and also for .dynsym reordering purposes.  */
   5755   s->hashcodes[s->nsyms] = ha;
   5756   s->hashval[h->dynindx] = ha;
   5757   ++s->nsyms;
   5758   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   5759     s->min_dynindx = h->dynindx;
   5760 
   5761   if (alc != NULL)
   5762     free (alc);
   5763 
   5764   return TRUE;
   5765 }
   5766 
   5767 /* This function will be called though elf_link_hash_traverse to do
   5768    final dynaminc symbol renumbering.  */
   5769 
   5770 static bfd_boolean
   5771 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
   5772 {
   5773   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5774   unsigned long int bucket;
   5775   unsigned long int val;
   5776 
   5777   /* Ignore indirect symbols.  */
   5778   if (h->dynindx == -1)
   5779     return TRUE;
   5780 
   5781   /* Ignore also local symbols and undefined symbols.  */
   5782   if (! (*s->bed->elf_hash_symbol) (h))
   5783     {
   5784       if (h->dynindx >= s->min_dynindx)
   5785 	h->dynindx = s->local_indx++;
   5786       return TRUE;
   5787     }
   5788 
   5789   bucket = s->hashval[h->dynindx] % s->bucketcount;
   5790   val = (s->hashval[h->dynindx] >> s->shift1)
   5791 	& ((s->maskbits >> s->shift1) - 1);
   5792   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   5793   s->bitmask[val]
   5794     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   5795   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   5796   if (s->counts[bucket] == 1)
   5797     /* Last element terminates the chain.  */
   5798     val |= 1;
   5799   bfd_put_32 (s->output_bfd, val,
   5800 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   5801   --s->counts[bucket];
   5802   h->dynindx = s->indx[bucket]++;
   5803   return TRUE;
   5804 }
   5805 
   5806 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   5807 
   5808 bfd_boolean
   5809 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   5810 {
   5811   return !(h->forced_local
   5812 	   || h->root.type == bfd_link_hash_undefined
   5813 	   || h->root.type == bfd_link_hash_undefweak
   5814 	   || ((h->root.type == bfd_link_hash_defined
   5815 		|| h->root.type == bfd_link_hash_defweak)
   5816 	       && h->root.u.def.section->output_section == NULL));
   5817 }
   5818 
   5819 /* Array used to determine the number of hash table buckets to use
   5820    based on the number of symbols there are.  If there are fewer than
   5821    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   5822    fewer than 37 we use 17 buckets, and so forth.  We never use more
   5823    than 32771 buckets.  */
   5824 
   5825 static const size_t elf_buckets[] =
   5826 {
   5827   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   5828   16411, 32771, 0
   5829 };
   5830 
   5831 /* Compute bucket count for hashing table.  We do not use a static set
   5832    of possible tables sizes anymore.  Instead we determine for all
   5833    possible reasonable sizes of the table the outcome (i.e., the
   5834    number of collisions etc) and choose the best solution.  The
   5835    weighting functions are not too simple to allow the table to grow
   5836    without bounds.  Instead one of the weighting factors is the size.
   5837    Therefore the result is always a good payoff between few collisions
   5838    (= short chain lengths) and table size.  */
   5839 static size_t
   5840 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5841 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   5842 		      unsigned long int nsyms,
   5843 		      int gnu_hash)
   5844 {
   5845   size_t best_size = 0;
   5846   unsigned long int i;
   5847 
   5848   /* We have a problem here.  The following code to optimize the table
   5849      size requires an integer type with more the 32 bits.  If
   5850      BFD_HOST_U_64_BIT is set we know about such a type.  */
   5851 #ifdef BFD_HOST_U_64_BIT
   5852   if (info->optimize)
   5853     {
   5854       size_t minsize;
   5855       size_t maxsize;
   5856       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
   5857       bfd *dynobj = elf_hash_table (info)->dynobj;
   5858       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   5859       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   5860       unsigned long int *counts;
   5861       bfd_size_type amt;
   5862       unsigned int no_improvement_count = 0;
   5863 
   5864       /* Possible optimization parameters: if we have NSYMS symbols we say
   5865 	 that the hashing table must at least have NSYMS/4 and at most
   5866 	 2*NSYMS buckets.  */
   5867       minsize = nsyms / 4;
   5868       if (minsize == 0)
   5869 	minsize = 1;
   5870       best_size = maxsize = nsyms * 2;
   5871       if (gnu_hash)
   5872 	{
   5873 	  if (minsize < 2)
   5874 	    minsize = 2;
   5875 	  if ((best_size & 31) == 0)
   5876 	    ++best_size;
   5877 	}
   5878 
   5879       /* Create array where we count the collisions in.  We must use bfd_malloc
   5880 	 since the size could be large.  */
   5881       amt = maxsize;
   5882       amt *= sizeof (unsigned long int);
   5883       counts = (unsigned long int *) bfd_malloc (amt);
   5884       if (counts == NULL)
   5885 	return 0;
   5886 
   5887       /* Compute the "optimal" size for the hash table.  The criteria is a
   5888 	 minimal chain length.  The minor criteria is (of course) the size
   5889 	 of the table.  */
   5890       for (i = minsize; i < maxsize; ++i)
   5891 	{
   5892 	  /* Walk through the array of hashcodes and count the collisions.  */
   5893 	  BFD_HOST_U_64_BIT max;
   5894 	  unsigned long int j;
   5895 	  unsigned long int fact;
   5896 
   5897 	  if (gnu_hash && (i & 31) == 0)
   5898 	    continue;
   5899 
   5900 	  memset (counts, '\0', i * sizeof (unsigned long int));
   5901 
   5902 	  /* Determine how often each hash bucket is used.  */
   5903 	  for (j = 0; j < nsyms; ++j)
   5904 	    ++counts[hashcodes[j] % i];
   5905 
   5906 	  /* For the weight function we need some information about the
   5907 	     pagesize on the target.  This is information need not be 100%
   5908 	     accurate.  Since this information is not available (so far) we
   5909 	     define it here to a reasonable default value.  If it is crucial
   5910 	     to have a better value some day simply define this value.  */
   5911 # ifndef BFD_TARGET_PAGESIZE
   5912 #  define BFD_TARGET_PAGESIZE	(4096)
   5913 # endif
   5914 
   5915 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   5916 	     and the chains.  */
   5917 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   5918 
   5919 # if 1
   5920 	  /* Variant 1: optimize for short chains.  We add the squares
   5921 	     of all the chain lengths (which favors many small chain
   5922 	     over a few long chains).  */
   5923 	  for (j = 0; j < i; ++j)
   5924 	    max += counts[j] * counts[j];
   5925 
   5926 	  /* This adds penalties for the overall size of the table.  */
   5927 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5928 	  max *= fact * fact;
   5929 # else
   5930 	  /* Variant 2: Optimize a lot more for small table.  Here we
   5931 	     also add squares of the size but we also add penalties for
   5932 	     empty slots (the +1 term).  */
   5933 	  for (j = 0; j < i; ++j)
   5934 	    max += (1 + counts[j]) * (1 + counts[j]);
   5935 
   5936 	  /* The overall size of the table is considered, but not as
   5937 	     strong as in variant 1, where it is squared.  */
   5938 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5939 	  max *= fact;
   5940 # endif
   5941 
   5942 	  /* Compare with current best results.  */
   5943 	  if (max < best_chlen)
   5944 	    {
   5945 	      best_chlen = max;
   5946 	      best_size = i;
   5947 	      no_improvement_count = 0;
   5948 	    }
   5949 	  /* PR 11843: Avoid futile long searches for the best bucket size
   5950 	     when there are a large number of symbols.  */
   5951 	  else if (++no_improvement_count == 100)
   5952 	    break;
   5953 	}
   5954 
   5955       free (counts);
   5956     }
   5957   else
   5958 #endif /* defined (BFD_HOST_U_64_BIT) */
   5959     {
   5960       /* This is the fallback solution if no 64bit type is available or if we
   5961 	 are not supposed to spend much time on optimizations.  We select the
   5962 	 bucket count using a fixed set of numbers.  */
   5963       for (i = 0; elf_buckets[i] != 0; i++)
   5964 	{
   5965 	  best_size = elf_buckets[i];
   5966 	  if (nsyms < elf_buckets[i + 1])
   5967 	    break;
   5968 	}
   5969       if (gnu_hash && best_size < 2)
   5970 	best_size = 2;
   5971     }
   5972 
   5973   return best_size;
   5974 }
   5975 
   5976 /* Size any SHT_GROUP section for ld -r.  */
   5977 
   5978 bfd_boolean
   5979 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   5980 {
   5981   bfd *ibfd;
   5982   asection *s;
   5983 
   5984   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   5985     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   5986 	&& (s = ibfd->sections) != NULL
   5987 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
   5988 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   5989       return FALSE;
   5990   return TRUE;
   5991 }
   5992 
   5993 /* Set a default stack segment size.  The value in INFO wins.  If it
   5994    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   5995    undefined it is initialized.  */
   5996 
   5997 bfd_boolean
   5998 bfd_elf_stack_segment_size (bfd *output_bfd,
   5999 			    struct bfd_link_info *info,
   6000 			    const char *legacy_symbol,
   6001 			    bfd_vma default_size)
   6002 {
   6003   struct elf_link_hash_entry *h = NULL;
   6004 
   6005   /* Look for legacy symbol.  */
   6006   if (legacy_symbol)
   6007     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   6008 			      FALSE, FALSE, FALSE);
   6009   if (h && (h->root.type == bfd_link_hash_defined
   6010 	    || h->root.type == bfd_link_hash_defweak)
   6011       && h->def_regular
   6012       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   6013     {
   6014       /* The symbol has no type if specified on the command line.  */
   6015       h->type = STT_OBJECT;
   6016       if (info->stacksize)
   6017 	/* xgettext:c-format */
   6018 	_bfd_error_handler (_("%B: stack size specified and %s set"),
   6019 			    output_bfd, legacy_symbol);
   6020       else if (h->root.u.def.section != bfd_abs_section_ptr)
   6021 	/* xgettext:c-format */
   6022 	_bfd_error_handler (_("%B: %s not absolute"),
   6023 			    output_bfd, legacy_symbol);
   6024       else
   6025 	info->stacksize = h->root.u.def.value;
   6026     }
   6027 
   6028   if (!info->stacksize)
   6029     /* If the user didn't set a size, or explicitly inhibit the
   6030        size, set it now.  */
   6031     info->stacksize = default_size;
   6032 
   6033   /* Provide the legacy symbol, if it is referenced.  */
   6034   if (h && (h->root.type == bfd_link_hash_undefined
   6035 	    || h->root.type == bfd_link_hash_undefweak))
   6036     {
   6037       struct bfd_link_hash_entry *bh = NULL;
   6038 
   6039       if (!(_bfd_generic_link_add_one_symbol
   6040 	    (info, output_bfd, legacy_symbol,
   6041 	     BSF_GLOBAL, bfd_abs_section_ptr,
   6042 	     info->stacksize >= 0 ? info->stacksize : 0,
   6043 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
   6044 	return FALSE;
   6045 
   6046       h = (struct elf_link_hash_entry *) bh;
   6047       h->def_regular = 1;
   6048       h->type = STT_OBJECT;
   6049     }
   6050 
   6051   return TRUE;
   6052 }
   6053 
   6054 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   6055 
   6056 struct elf_gc_sweep_symbol_info
   6057 {
   6058   struct bfd_link_info *info;
   6059   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   6060 		       bfd_boolean);
   6061 };
   6062 
   6063 static bfd_boolean
   6064 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   6065 {
   6066   if (!h->mark
   6067       && (((h->root.type == bfd_link_hash_defined
   6068 	    || h->root.type == bfd_link_hash_defweak)
   6069 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   6070 		&& h->root.u.def.section->gc_mark))
   6071 	  || h->root.type == bfd_link_hash_undefined
   6072 	  || h->root.type == bfd_link_hash_undefweak))
   6073     {
   6074       struct elf_gc_sweep_symbol_info *inf;
   6075 
   6076       inf = (struct elf_gc_sweep_symbol_info *) data;
   6077       (*inf->hide_symbol) (inf->info, h, TRUE);
   6078       h->def_regular = 0;
   6079       h->ref_regular = 0;
   6080       h->ref_regular_nonweak = 0;
   6081     }
   6082 
   6083   return TRUE;
   6084 }
   6085 
   6086 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   6087    called by the ELF linker emulation before_allocation routine.  We
   6088    must set the sizes of the sections before the linker sets the
   6089    addresses of the various sections.  */
   6090 
   6091 bfd_boolean
   6092 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   6093 			       const char *soname,
   6094 			       const char *rpath,
   6095 			       const char *filter_shlib,
   6096 			       const char *audit,
   6097 			       const char *depaudit,
   6098 			       const char * const *auxiliary_filters,
   6099 			       struct bfd_link_info *info,
   6100 			       asection **sinterpptr)
   6101 {
   6102   bfd *dynobj;
   6103   const struct elf_backend_data *bed;
   6104 
   6105   *sinterpptr = NULL;
   6106 
   6107   if (!is_elf_hash_table (info->hash))
   6108     return TRUE;
   6109 
   6110   dynobj = elf_hash_table (info)->dynobj;
   6111 
   6112   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6113     {
   6114       struct bfd_elf_version_tree *verdefs;
   6115       struct elf_info_failed asvinfo;
   6116       struct bfd_elf_version_tree *t;
   6117       struct bfd_elf_version_expr *d;
   6118       asection *s;
   6119       size_t soname_indx;
   6120 
   6121       /* If we are supposed to export all symbols into the dynamic symbol
   6122 	 table (this is not the normal case), then do so.  */
   6123       if (info->export_dynamic
   6124 	  || (bfd_link_executable (info) && info->dynamic))
   6125 	{
   6126 	  struct elf_info_failed eif;
   6127 
   6128 	  eif.info = info;
   6129 	  eif.failed = FALSE;
   6130 	  elf_link_hash_traverse (elf_hash_table (info),
   6131 				  _bfd_elf_export_symbol,
   6132 				  &eif);
   6133 	  if (eif.failed)
   6134 	    return FALSE;
   6135 	}
   6136 
   6137       if (soname != NULL)
   6138 	{
   6139 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6140 					     soname, TRUE);
   6141 	  if (soname_indx == (size_t) -1
   6142 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   6143 	    return FALSE;
   6144 	}
   6145       else
   6146 	soname_indx = (size_t) -1;
   6147 
   6148       /* Make all global versions with definition.  */
   6149       for (t = info->version_info; t != NULL; t = t->next)
   6150 	for (d = t->globals.list; d != NULL; d = d->next)
   6151 	  if (!d->symver && d->literal)
   6152 	    {
   6153 	      const char *verstr, *name;
   6154 	      size_t namelen, verlen, newlen;
   6155 	      char *newname, *p, leading_char;
   6156 	      struct elf_link_hash_entry *newh;
   6157 
   6158 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   6159 	      name = d->pattern;
   6160 	      namelen = strlen (name) + (leading_char != '\0');
   6161 	      verstr = t->name;
   6162 	      verlen = strlen (verstr);
   6163 	      newlen = namelen + verlen + 3;
   6164 
   6165 	      newname = (char *) bfd_malloc (newlen);
   6166 	      if (newname == NULL)
   6167 		return FALSE;
   6168 	      newname[0] = leading_char;
   6169 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   6170 
   6171 	      /* Check the hidden versioned definition.  */
   6172 	      p = newname + namelen;
   6173 	      *p++ = ELF_VER_CHR;
   6174 	      memcpy (p, verstr, verlen + 1);
   6175 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   6176 					   newname, FALSE, FALSE,
   6177 					   FALSE);
   6178 	      if (newh == NULL
   6179 		  || (newh->root.type != bfd_link_hash_defined
   6180 		      && newh->root.type != bfd_link_hash_defweak))
   6181 		{
   6182 		  /* Check the default versioned definition.  */
   6183 		  *p++ = ELF_VER_CHR;
   6184 		  memcpy (p, verstr, verlen + 1);
   6185 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   6186 					       newname, FALSE, FALSE,
   6187 					       FALSE);
   6188 		}
   6189 	      free (newname);
   6190 
   6191 	      /* Mark this version if there is a definition and it is
   6192 		 not defined in a shared object.  */
   6193 	      if (newh != NULL
   6194 		  && !newh->def_dynamic
   6195 		  && (newh->root.type == bfd_link_hash_defined
   6196 		      || newh->root.type == bfd_link_hash_defweak))
   6197 		d->symver = 1;
   6198 	    }
   6199 
   6200       /* Attach all the symbols to their version information.  */
   6201       asvinfo.info = info;
   6202       asvinfo.failed = FALSE;
   6203 
   6204       elf_link_hash_traverse (elf_hash_table (info),
   6205 			      _bfd_elf_link_assign_sym_version,
   6206 			      &asvinfo);
   6207       if (asvinfo.failed)
   6208 	return FALSE;
   6209 
   6210       if (!info->allow_undefined_version)
   6211 	{
   6212 	  /* Check if all global versions have a definition.  */
   6213 	  bfd_boolean all_defined = TRUE;
   6214 	  for (t = info->version_info; t != NULL; t = t->next)
   6215 	    for (d = t->globals.list; d != NULL; d = d->next)
   6216 	      if (d->literal && !d->symver && !d->script)
   6217 		{
   6218 		  _bfd_error_handler
   6219 		    (_("%s: undefined version: %s"),
   6220 		     d->pattern, t->name);
   6221 		  all_defined = FALSE;
   6222 		}
   6223 
   6224 	  if (!all_defined)
   6225 	    {
   6226 	      bfd_set_error (bfd_error_bad_value);
   6227 	      return FALSE;
   6228 	    }
   6229 	}
   6230 
   6231       /* Set up the version definition section.  */
   6232       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   6233       BFD_ASSERT (s != NULL);
   6234 
   6235       /* We may have created additional version definitions if we are
   6236 	 just linking a regular application.  */
   6237       verdefs = info->version_info;
   6238 
   6239       /* Skip anonymous version tag.  */
   6240       if (verdefs != NULL && verdefs->vernum == 0)
   6241 	verdefs = verdefs->next;
   6242 
   6243       if (verdefs == NULL && !info->create_default_symver)
   6244 	s->flags |= SEC_EXCLUDE;
   6245       else
   6246 	{
   6247 	  unsigned int cdefs;
   6248 	  bfd_size_type size;
   6249 	  bfd_byte *p;
   6250 	  Elf_Internal_Verdef def;
   6251 	  Elf_Internal_Verdaux defaux;
   6252 	  struct bfd_link_hash_entry *bh;
   6253 	  struct elf_link_hash_entry *h;
   6254 	  const char *name;
   6255 
   6256 	  cdefs = 0;
   6257 	  size = 0;
   6258 
   6259 	  /* Make space for the base version.  */
   6260 	  size += sizeof (Elf_External_Verdef);
   6261 	  size += sizeof (Elf_External_Verdaux);
   6262 	  ++cdefs;
   6263 
   6264 	  /* Make space for the default version.  */
   6265 	  if (info->create_default_symver)
   6266 	    {
   6267 	      size += sizeof (Elf_External_Verdef);
   6268 	      ++cdefs;
   6269 	    }
   6270 
   6271 	  for (t = verdefs; t != NULL; t = t->next)
   6272 	    {
   6273 	      struct bfd_elf_version_deps *n;
   6274 
   6275 	      /* Don't emit base version twice.  */
   6276 	      if (t->vernum == 0)
   6277 		continue;
   6278 
   6279 	      size += sizeof (Elf_External_Verdef);
   6280 	      size += sizeof (Elf_External_Verdaux);
   6281 	      ++cdefs;
   6282 
   6283 	      for (n = t->deps; n != NULL; n = n->next)
   6284 		size += sizeof (Elf_External_Verdaux);
   6285 	    }
   6286 
   6287 	  s->size = size;
   6288 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6289 	  if (s->contents == NULL && s->size != 0)
   6290 	    return FALSE;
   6291 
   6292 	  /* Fill in the version definition section.  */
   6293 
   6294 	  p = s->contents;
   6295 
   6296 	  def.vd_version = VER_DEF_CURRENT;
   6297 	  def.vd_flags = VER_FLG_BASE;
   6298 	  def.vd_ndx = 1;
   6299 	  def.vd_cnt = 1;
   6300 	  if (info->create_default_symver)
   6301 	    {
   6302 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   6303 	      def.vd_next = sizeof (Elf_External_Verdef);
   6304 	    }
   6305 	  else
   6306 	    {
   6307 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6308 	      def.vd_next = (sizeof (Elf_External_Verdef)
   6309 			     + sizeof (Elf_External_Verdaux));
   6310 	    }
   6311 
   6312 	  if (soname_indx != (size_t) -1)
   6313 	    {
   6314 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6315 				      soname_indx);
   6316 	      def.vd_hash = bfd_elf_hash (soname);
   6317 	      defaux.vda_name = soname_indx;
   6318 	      name = soname;
   6319 	    }
   6320 	  else
   6321 	    {
   6322 	      size_t indx;
   6323 
   6324 	      name = lbasename (output_bfd->filename);
   6325 	      def.vd_hash = bfd_elf_hash (name);
   6326 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6327 					  name, FALSE);
   6328 	      if (indx == (size_t) -1)
   6329 		return FALSE;
   6330 	      defaux.vda_name = indx;
   6331 	    }
   6332 	  defaux.vda_next = 0;
   6333 
   6334 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   6335 				    (Elf_External_Verdef *) p);
   6336 	  p += sizeof (Elf_External_Verdef);
   6337 	  if (info->create_default_symver)
   6338 	    {
   6339 	      /* Add a symbol representing this version.  */
   6340 	      bh = NULL;
   6341 	      if (! (_bfd_generic_link_add_one_symbol
   6342 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   6343 		      0, NULL, FALSE,
   6344 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6345 		return FALSE;
   6346 	      h = (struct elf_link_hash_entry *) bh;
   6347 	      h->non_elf = 0;
   6348 	      h->def_regular = 1;
   6349 	      h->type = STT_OBJECT;
   6350 	      h->verinfo.vertree = NULL;
   6351 
   6352 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6353 		return FALSE;
   6354 
   6355 	      /* Create a duplicate of the base version with the same
   6356 		 aux block, but different flags.  */
   6357 	      def.vd_flags = 0;
   6358 	      def.vd_ndx = 2;
   6359 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6360 	      if (verdefs)
   6361 		def.vd_next = (sizeof (Elf_External_Verdef)
   6362 			       + sizeof (Elf_External_Verdaux));
   6363 	      else
   6364 		def.vd_next = 0;
   6365 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6366 					(Elf_External_Verdef *) p);
   6367 	      p += sizeof (Elf_External_Verdef);
   6368 	    }
   6369 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6370 				     (Elf_External_Verdaux *) p);
   6371 	  p += sizeof (Elf_External_Verdaux);
   6372 
   6373 	  for (t = verdefs; t != NULL; t = t->next)
   6374 	    {
   6375 	      unsigned int cdeps;
   6376 	      struct bfd_elf_version_deps *n;
   6377 
   6378 	      /* Don't emit the base version twice.  */
   6379 	      if (t->vernum == 0)
   6380 		continue;
   6381 
   6382 	      cdeps = 0;
   6383 	      for (n = t->deps; n != NULL; n = n->next)
   6384 		++cdeps;
   6385 
   6386 	      /* Add a symbol representing this version.  */
   6387 	      bh = NULL;
   6388 	      if (! (_bfd_generic_link_add_one_symbol
   6389 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   6390 		      0, NULL, FALSE,
   6391 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6392 		return FALSE;
   6393 	      h = (struct elf_link_hash_entry *) bh;
   6394 	      h->non_elf = 0;
   6395 	      h->def_regular = 1;
   6396 	      h->type = STT_OBJECT;
   6397 	      h->verinfo.vertree = t;
   6398 
   6399 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6400 		return FALSE;
   6401 
   6402 	      def.vd_version = VER_DEF_CURRENT;
   6403 	      def.vd_flags = 0;
   6404 	      if (t->globals.list == NULL
   6405 		  && t->locals.list == NULL
   6406 		  && ! t->used)
   6407 		def.vd_flags |= VER_FLG_WEAK;
   6408 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   6409 	      def.vd_cnt = cdeps + 1;
   6410 	      def.vd_hash = bfd_elf_hash (t->name);
   6411 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6412 	      def.vd_next = 0;
   6413 
   6414 	      /* If a basever node is next, it *must* be the last node in
   6415 		 the chain, otherwise Verdef construction breaks.  */
   6416 	      if (t->next != NULL && t->next->vernum == 0)
   6417 		BFD_ASSERT (t->next->next == NULL);
   6418 
   6419 	      if (t->next != NULL && t->next->vernum != 0)
   6420 		def.vd_next = (sizeof (Elf_External_Verdef)
   6421 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   6422 
   6423 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6424 					(Elf_External_Verdef *) p);
   6425 	      p += sizeof (Elf_External_Verdef);
   6426 
   6427 	      defaux.vda_name = h->dynstr_index;
   6428 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6429 				      h->dynstr_index);
   6430 	      defaux.vda_next = 0;
   6431 	      if (t->deps != NULL)
   6432 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   6433 	      t->name_indx = defaux.vda_name;
   6434 
   6435 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6436 					 (Elf_External_Verdaux *) p);
   6437 	      p += sizeof (Elf_External_Verdaux);
   6438 
   6439 	      for (n = t->deps; n != NULL; n = n->next)
   6440 		{
   6441 		  if (n->version_needed == NULL)
   6442 		    {
   6443 		      /* This can happen if there was an error in the
   6444 			 version script.  */
   6445 		      defaux.vda_name = 0;
   6446 		    }
   6447 		  else
   6448 		    {
   6449 		      defaux.vda_name = n->version_needed->name_indx;
   6450 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6451 					      defaux.vda_name);
   6452 		    }
   6453 		  if (n->next == NULL)
   6454 		    defaux.vda_next = 0;
   6455 		  else
   6456 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   6457 
   6458 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6459 					     (Elf_External_Verdaux *) p);
   6460 		  p += sizeof (Elf_External_Verdaux);
   6461 		}
   6462 	    }
   6463 
   6464 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   6465 	}
   6466     }
   6467 
   6468   bed = get_elf_backend_data (output_bfd);
   6469 
   6470   if (info->gc_sections && bed->can_gc_sections)
   6471     {
   6472       struct elf_gc_sweep_symbol_info sweep_info;
   6473 
   6474       /* Remove the symbols that were in the swept sections from the
   6475 	 dynamic symbol table.  */
   6476       sweep_info.info = info;
   6477       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   6478       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   6479 			      &sweep_info);
   6480     }
   6481 
   6482   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6483     {
   6484       asection *s;
   6485       struct elf_find_verdep_info sinfo;
   6486 
   6487       /* Work out the size of the version reference section.  */
   6488 
   6489       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   6490       BFD_ASSERT (s != NULL);
   6491 
   6492       sinfo.info = info;
   6493       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   6494       if (sinfo.vers == 0)
   6495 	sinfo.vers = 1;
   6496       sinfo.failed = FALSE;
   6497 
   6498       elf_link_hash_traverse (elf_hash_table (info),
   6499 			      _bfd_elf_link_find_version_dependencies,
   6500 			      &sinfo);
   6501       if (sinfo.failed)
   6502 	return FALSE;
   6503 
   6504       if (elf_tdata (output_bfd)->verref == NULL)
   6505 	s->flags |= SEC_EXCLUDE;
   6506       else
   6507 	{
   6508 	  Elf_Internal_Verneed *vn;
   6509 	  unsigned int size;
   6510 	  unsigned int crefs;
   6511 	  bfd_byte *p;
   6512 
   6513 	  /* Build the version dependency section.  */
   6514 	  size = 0;
   6515 	  crefs = 0;
   6516 	  for (vn = elf_tdata (output_bfd)->verref;
   6517 	       vn != NULL;
   6518 	       vn = vn->vn_nextref)
   6519 	    {
   6520 	      Elf_Internal_Vernaux *a;
   6521 
   6522 	      size += sizeof (Elf_External_Verneed);
   6523 	      ++crefs;
   6524 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6525 		size += sizeof (Elf_External_Vernaux);
   6526 	    }
   6527 
   6528 	  s->size = size;
   6529 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6530 	  if (s->contents == NULL)
   6531 	    return FALSE;
   6532 
   6533 	  p = s->contents;
   6534 	  for (vn = elf_tdata (output_bfd)->verref;
   6535 	       vn != NULL;
   6536 	       vn = vn->vn_nextref)
   6537 	    {
   6538 	      unsigned int caux;
   6539 	      Elf_Internal_Vernaux *a;
   6540 	      size_t indx;
   6541 
   6542 	      caux = 0;
   6543 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6544 		++caux;
   6545 
   6546 	      vn->vn_version = VER_NEED_CURRENT;
   6547 	      vn->vn_cnt = caux;
   6548 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6549 					  elf_dt_name (vn->vn_bfd) != NULL
   6550 					  ? elf_dt_name (vn->vn_bfd)
   6551 					  : lbasename (vn->vn_bfd->filename),
   6552 					  FALSE);
   6553 	      if (indx == (size_t) -1)
   6554 		return FALSE;
   6555 	      vn->vn_file = indx;
   6556 	      vn->vn_aux = sizeof (Elf_External_Verneed);
   6557 	      if (vn->vn_nextref == NULL)
   6558 		vn->vn_next = 0;
   6559 	      else
   6560 		vn->vn_next = (sizeof (Elf_External_Verneed)
   6561 			       + caux * sizeof (Elf_External_Vernaux));
   6562 
   6563 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
   6564 					 (Elf_External_Verneed *) p);
   6565 	      p += sizeof (Elf_External_Verneed);
   6566 
   6567 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6568 		{
   6569 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
   6570 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6571 					      a->vna_nodename, FALSE);
   6572 		  if (indx == (size_t) -1)
   6573 		    return FALSE;
   6574 		  a->vna_name = indx;
   6575 		  if (a->vna_nextptr == NULL)
   6576 		    a->vna_next = 0;
   6577 		  else
   6578 		    a->vna_next = sizeof (Elf_External_Vernaux);
   6579 
   6580 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
   6581 					     (Elf_External_Vernaux *) p);
   6582 		  p += sizeof (Elf_External_Vernaux);
   6583 		}
   6584 	    }
   6585 
   6586 	  elf_tdata (output_bfd)->cverrefs = crefs;
   6587 	}
   6588     }
   6589 
   6590   /* Any syms created from now on start with -1 in
   6591      got.refcount/offset and plt.refcount/offset.  */
   6592   elf_hash_table (info)->init_got_refcount
   6593     = elf_hash_table (info)->init_got_offset;
   6594   elf_hash_table (info)->init_plt_refcount
   6595     = elf_hash_table (info)->init_plt_offset;
   6596 
   6597   if (bfd_link_relocatable (info)
   6598       && !_bfd_elf_size_group_sections (info))
   6599     return FALSE;
   6600 
   6601   /* The backend may have to create some sections regardless of whether
   6602      we're dynamic or not.  */
   6603   if (bed->elf_backend_always_size_sections
   6604       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
   6605     return FALSE;
   6606 
   6607   /* Determine any GNU_STACK segment requirements, after the backend
   6608      has had a chance to set a default segment size.  */
   6609   if (info->execstack)
   6610     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   6611   else if (info->noexecstack)
   6612     elf_stack_flags (output_bfd) = PF_R | PF_W;
   6613   else
   6614     {
   6615       bfd *inputobj;
   6616       asection *notesec = NULL;
   6617       int exec = 0;
   6618 
   6619       for (inputobj = info->input_bfds;
   6620 	   inputobj;
   6621 	   inputobj = inputobj->link.next)
   6622 	{
   6623 	  asection *s;
   6624 
   6625 	  if (inputobj->flags
   6626 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   6627 	    continue;
   6628 	  s = inputobj->sections;
   6629 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   6630 	    continue;
   6631 
   6632 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   6633 	  if (s)
   6634 	    {
   6635 	      if (s->flags & SEC_CODE)
   6636 		exec = PF_X;
   6637 	      notesec = s;
   6638 	    }
   6639 	  else if (bed->default_execstack)
   6640 	    exec = PF_X;
   6641 	}
   6642       if (notesec || info->stacksize > 0)
   6643 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   6644       if (notesec && exec && bfd_link_relocatable (info)
   6645 	  && notesec->output_section != bfd_abs_section_ptr)
   6646 	notesec->output_section->flags |= SEC_CODE;
   6647     }
   6648 
   6649   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6650     {
   6651       struct elf_info_failed eif;
   6652       struct elf_link_hash_entry *h;
   6653       asection *dynstr;
   6654       asection *s;
   6655 
   6656       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   6657       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   6658 
   6659       if (info->symbolic)
   6660 	{
   6661 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   6662 	    return FALSE;
   6663 	  info->flags |= DF_SYMBOLIC;
   6664 	}
   6665 
   6666       if (rpath != NULL)
   6667 	{
   6668 	  size_t indx;
   6669 	  bfd_vma tag;
   6670 
   6671 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   6672 				      TRUE);
   6673 	  if (indx == (size_t) -1)
   6674 	    return FALSE;
   6675 
   6676 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   6677 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   6678 	    return FALSE;
   6679 	}
   6680 
   6681       if (filter_shlib != NULL)
   6682 	{
   6683 	  size_t indx;
   6684 
   6685 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6686 				      filter_shlib, TRUE);
   6687 	  if (indx == (size_t) -1
   6688 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   6689 	    return FALSE;
   6690 	}
   6691 
   6692       if (auxiliary_filters != NULL)
   6693 	{
   6694 	  const char * const *p;
   6695 
   6696 	  for (p = auxiliary_filters; *p != NULL; p++)
   6697 	    {
   6698 	      size_t indx;
   6699 
   6700 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6701 					  *p, TRUE);
   6702 	      if (indx == (size_t) -1
   6703 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   6704 		return FALSE;
   6705 	    }
   6706 	}
   6707 
   6708       if (audit != NULL)
   6709 	{
   6710 	  size_t indx;
   6711 
   6712 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   6713 				      TRUE);
   6714 	  if (indx == (size_t) -1
   6715 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   6716 	    return FALSE;
   6717 	}
   6718 
   6719       if (depaudit != NULL)
   6720 	{
   6721 	  size_t indx;
   6722 
   6723 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   6724 				      TRUE);
   6725 	  if (indx == (size_t) -1
   6726 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   6727 	    return FALSE;
   6728 	}
   6729 
   6730       eif.info = info;
   6731       eif.failed = FALSE;
   6732 
   6733       /* Find all symbols which were defined in a dynamic object and make
   6734 	 the backend pick a reasonable value for them.  */
   6735       elf_link_hash_traverse (elf_hash_table (info),
   6736 			      _bfd_elf_adjust_dynamic_symbol,
   6737 			      &eif);
   6738       if (eif.failed)
   6739 	return FALSE;
   6740 
   6741       /* Add some entries to the .dynamic section.  We fill in some of the
   6742 	 values later, in bfd_elf_final_link, but we must add the entries
   6743 	 now so that we know the final size of the .dynamic section.  */
   6744 
   6745       /* If there are initialization and/or finalization functions to
   6746 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   6747       h = (info->init_function
   6748 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6749 				   info->init_function, FALSE,
   6750 				   FALSE, FALSE)
   6751 	   : NULL);
   6752       if (h != NULL
   6753 	  && (h->ref_regular
   6754 	      || h->def_regular))
   6755 	{
   6756 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   6757 	    return FALSE;
   6758 	}
   6759       h = (info->fini_function
   6760 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6761 				   info->fini_function, FALSE,
   6762 				   FALSE, FALSE)
   6763 	   : NULL);
   6764       if (h != NULL
   6765 	  && (h->ref_regular
   6766 	      || h->def_regular))
   6767 	{
   6768 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   6769 	    return FALSE;
   6770 	}
   6771 
   6772       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   6773       if (s != NULL && s->linker_has_input)
   6774 	{
   6775 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   6776 	  if (! bfd_link_executable (info))
   6777 	    {
   6778 	      bfd *sub;
   6779 	      asection *o;
   6780 
   6781 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   6782 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   6783 		    && (o = sub->sections) != NULL
   6784 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
   6785 		  for (o = sub->sections; o != NULL; o = o->next)
   6786 		    if (elf_section_data (o)->this_hdr.sh_type
   6787 			== SHT_PREINIT_ARRAY)
   6788 		      {
   6789 			_bfd_error_handler
   6790 			  (_("%B: .preinit_array section is not allowed in DSO"),
   6791 			   sub);
   6792 			break;
   6793 		      }
   6794 
   6795 	      bfd_set_error (bfd_error_nonrepresentable_section);
   6796 	      return FALSE;
   6797 	    }
   6798 
   6799 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   6800 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   6801 	    return FALSE;
   6802 	}
   6803       s = bfd_get_section_by_name (output_bfd, ".init_array");
   6804       if (s != NULL && s->linker_has_input)
   6805 	{
   6806 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   6807 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   6808 	    return FALSE;
   6809 	}
   6810       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   6811       if (s != NULL && s->linker_has_input)
   6812 	{
   6813 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   6814 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   6815 	    return FALSE;
   6816 	}
   6817 
   6818       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   6819       /* If .dynstr is excluded from the link, we don't want any of
   6820 	 these tags.  Strictly, we should be checking each section
   6821 	 individually;  This quick check covers for the case where
   6822 	 someone does a /DISCARD/ : { *(*) }.  */
   6823       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   6824 	{
   6825 	  bfd_size_type strsize;
   6826 
   6827 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   6828 	  if ((info->emit_hash
   6829 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   6830 	      || (info->emit_gnu_hash
   6831 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
   6832 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   6833 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   6834 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   6835 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   6836 					      bed->s->sizeof_sym))
   6837 	    return FALSE;
   6838 	}
   6839     }
   6840 
   6841   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   6842     return FALSE;
   6843 
   6844   /* The backend must work out the sizes of all the other dynamic
   6845      sections.  */
   6846   if (dynobj != NULL
   6847       && bed->elf_backend_size_dynamic_sections != NULL
   6848       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
   6849     return FALSE;
   6850 
   6851   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6852     {
   6853       if (elf_tdata (output_bfd)->cverdefs)
   6854 	{
   6855 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
   6856 
   6857 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   6858 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
   6859 	    return FALSE;
   6860 	}
   6861 
   6862       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   6863 	{
   6864 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   6865 	    return FALSE;
   6866 	}
   6867       else if (info->flags & DF_BIND_NOW)
   6868 	{
   6869 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   6870 	    return FALSE;
   6871 	}
   6872 
   6873       if (info->flags_1)
   6874 	{
   6875 	  if (bfd_link_executable (info))
   6876 	    info->flags_1 &= ~ (DF_1_INITFIRST
   6877 				| DF_1_NODELETE
   6878 				| DF_1_NOOPEN);
   6879 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   6880 	    return FALSE;
   6881 	}
   6882 
   6883       if (elf_tdata (output_bfd)->cverrefs)
   6884 	{
   6885 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
   6886 
   6887 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   6888 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   6889 	    return FALSE;
   6890 	}
   6891 
   6892       if ((elf_tdata (output_bfd)->cverrefs == 0
   6893 	   && elf_tdata (output_bfd)->cverdefs == 0)
   6894 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
   6895 	{
   6896 	  asection *s;
   6897 
   6898 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   6899 	  s->flags |= SEC_EXCLUDE;
   6900 	}
   6901     }
   6902   return TRUE;
   6903 }
   6904 
   6905 /* Find the first non-excluded output section.  We'll use its
   6906    section symbol for some emitted relocs.  */
   6907 void
   6908 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   6909 {
   6910   asection *s;
   6911 
   6912   for (s = output_bfd->sections; s != NULL; s = s->next)
   6913     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   6914 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6915       {
   6916 	elf_hash_table (info)->text_index_section = s;
   6917 	break;
   6918       }
   6919 }
   6920 
   6921 /* Find two non-excluded output sections, one for code, one for data.
   6922    We'll use their section symbols for some emitted relocs.  */
   6923 void
   6924 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   6925 {
   6926   asection *s;
   6927 
   6928   /* Data first, since setting text_index_section changes
   6929      _bfd_elf_link_omit_section_dynsym.  */
   6930   for (s = output_bfd->sections; s != NULL; s = s->next)
   6931     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
   6932 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6933       {
   6934 	elf_hash_table (info)->data_index_section = s;
   6935 	break;
   6936       }
   6937 
   6938   for (s = output_bfd->sections; s != NULL; s = s->next)
   6939     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
   6940 	 == (SEC_ALLOC | SEC_READONLY))
   6941 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6942       {
   6943 	elf_hash_table (info)->text_index_section = s;
   6944 	break;
   6945       }
   6946 
   6947   if (elf_hash_table (info)->text_index_section == NULL)
   6948     elf_hash_table (info)->text_index_section
   6949       = elf_hash_table (info)->data_index_section;
   6950 }
   6951 
   6952 bfd_boolean
   6953 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   6954 {
   6955   const struct elf_backend_data *bed;
   6956   unsigned long section_sym_count;
   6957   bfd_size_type dynsymcount = 0;
   6958 
   6959   if (!is_elf_hash_table (info->hash))
   6960     return TRUE;
   6961 
   6962   bed = get_elf_backend_data (output_bfd);
   6963   (*bed->elf_backend_init_index_section) (output_bfd, info);
   6964 
   6965   /* Assign dynsym indices.  In a shared library we generate a section
   6966      symbol for each output section, which come first.  Next come all
   6967      of the back-end allocated local dynamic syms, followed by the rest
   6968      of the global symbols.
   6969 
   6970      This is usually not needed for static binaries, however backends
   6971      can request to always do it, e.g. the MIPS backend uses dynamic
   6972      symbol counts to lay out GOT, which will be produced in the
   6973      presence of GOT relocations even in static binaries (holding fixed
   6974      data in that case, to satisfy those relocations).  */
   6975 
   6976   if (elf_hash_table (info)->dynamic_sections_created
   6977       || bed->always_renumber_dynsyms)
   6978     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   6979 						  &section_sym_count);
   6980 
   6981   if (elf_hash_table (info)->dynamic_sections_created)
   6982     {
   6983       bfd *dynobj;
   6984       asection *s;
   6985       unsigned int dtagcount;
   6986 
   6987       dynobj = elf_hash_table (info)->dynobj;
   6988 
   6989       /* Work out the size of the symbol version section.  */
   6990       s = bfd_get_linker_section (dynobj, ".gnu.version");
   6991       BFD_ASSERT (s != NULL);
   6992       if ((s->flags & SEC_EXCLUDE) == 0)
   6993 	{
   6994 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   6995 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6996 	  if (s->contents == NULL)
   6997 	    return FALSE;
   6998 
   6999 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   7000 	    return FALSE;
   7001 	}
   7002 
   7003       /* Set the size of the .dynsym and .hash sections.  We counted
   7004 	 the number of dynamic symbols in elf_link_add_object_symbols.
   7005 	 We will build the contents of .dynsym and .hash when we build
   7006 	 the final symbol table, because until then we do not know the
   7007 	 correct value to give the symbols.  We built the .dynstr
   7008 	 section as we went along in elf_link_add_object_symbols.  */
   7009       s = elf_hash_table (info)->dynsym;
   7010       BFD_ASSERT (s != NULL);
   7011       s->size = dynsymcount * bed->s->sizeof_sym;
   7012 
   7013       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   7014       if (s->contents == NULL)
   7015 	return FALSE;
   7016 
   7017       /* The first entry in .dynsym is a dummy symbol.  Clear all the
   7018 	 section syms, in case we don't output them all.  */
   7019       ++section_sym_count;
   7020       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   7021 
   7022       elf_hash_table (info)->bucketcount = 0;
   7023 
   7024       /* Compute the size of the hashing table.  As a side effect this
   7025 	 computes the hash values for all the names we export.  */
   7026       if (info->emit_hash)
   7027 	{
   7028 	  unsigned long int *hashcodes;
   7029 	  struct hash_codes_info hashinf;
   7030 	  bfd_size_type amt;
   7031 	  unsigned long int nsyms;
   7032 	  size_t bucketcount;
   7033 	  size_t hash_entry_size;
   7034 
   7035 	  /* Compute the hash values for all exported symbols.  At the same
   7036 	     time store the values in an array so that we could use them for
   7037 	     optimizations.  */
   7038 	  amt = dynsymcount * sizeof (unsigned long int);
   7039 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   7040 	  if (hashcodes == NULL)
   7041 	    return FALSE;
   7042 	  hashinf.hashcodes = hashcodes;
   7043 	  hashinf.error = FALSE;
   7044 
   7045 	  /* Put all hash values in HASHCODES.  */
   7046 	  elf_link_hash_traverse (elf_hash_table (info),
   7047 				  elf_collect_hash_codes, &hashinf);
   7048 	  if (hashinf.error)
   7049 	    {
   7050 	      free (hashcodes);
   7051 	      return FALSE;
   7052 	    }
   7053 
   7054 	  nsyms = hashinf.hashcodes - hashcodes;
   7055 	  bucketcount
   7056 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   7057 	  free (hashcodes);
   7058 
   7059 	  if (bucketcount == 0 && nsyms > 0)
   7060 	    return FALSE;
   7061 
   7062 	  elf_hash_table (info)->bucketcount = bucketcount;
   7063 
   7064 	  s = bfd_get_linker_section (dynobj, ".hash");
   7065 	  BFD_ASSERT (s != NULL);
   7066 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   7067 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   7068 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7069 	  if (s->contents == NULL)
   7070 	    return FALSE;
   7071 
   7072 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   7073 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   7074 		   s->contents + hash_entry_size);
   7075 	}
   7076 
   7077       if (info->emit_gnu_hash)
   7078 	{
   7079 	  size_t i, cnt;
   7080 	  unsigned char *contents;
   7081 	  struct collect_gnu_hash_codes cinfo;
   7082 	  bfd_size_type amt;
   7083 	  size_t bucketcount;
   7084 
   7085 	  memset (&cinfo, 0, sizeof (cinfo));
   7086 
   7087 	  /* Compute the hash values for all exported symbols.  At the same
   7088 	     time store the values in an array so that we could use them for
   7089 	     optimizations.  */
   7090 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   7091 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   7092 	  if (cinfo.hashcodes == NULL)
   7093 	    return FALSE;
   7094 
   7095 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   7096 	  cinfo.min_dynindx = -1;
   7097 	  cinfo.output_bfd = output_bfd;
   7098 	  cinfo.bed = bed;
   7099 
   7100 	  /* Put all hash values in HASHCODES.  */
   7101 	  elf_link_hash_traverse (elf_hash_table (info),
   7102 				  elf_collect_gnu_hash_codes, &cinfo);
   7103 	  if (cinfo.error)
   7104 	    {
   7105 	      free (cinfo.hashcodes);
   7106 	      return FALSE;
   7107 	    }
   7108 
   7109 	  bucketcount
   7110 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   7111 
   7112 	  if (bucketcount == 0)
   7113 	    {
   7114 	      free (cinfo.hashcodes);
   7115 	      return FALSE;
   7116 	    }
   7117 
   7118 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
   7119 	  BFD_ASSERT (s != NULL);
   7120 
   7121 	  if (cinfo.nsyms == 0)
   7122 	    {
   7123 	      /* Empty .gnu.hash section is special.  */
   7124 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   7125 	      free (cinfo.hashcodes);
   7126 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   7127 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7128 	      if (contents == NULL)
   7129 		return FALSE;
   7130 	      s->contents = contents;
   7131 	      /* 1 empty bucket.  */
   7132 	      bfd_put_32 (output_bfd, 1, contents);
   7133 	      /* SYMIDX above the special symbol 0.  */
   7134 	      bfd_put_32 (output_bfd, 1, contents + 4);
   7135 	      /* Just one word for bitmask.  */
   7136 	      bfd_put_32 (output_bfd, 1, contents + 8);
   7137 	      /* Only hash fn bloom filter.  */
   7138 	      bfd_put_32 (output_bfd, 0, contents + 12);
   7139 	      /* No hashes are valid - empty bitmask.  */
   7140 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   7141 	      /* No hashes in the only bucket.  */
   7142 	      bfd_put_32 (output_bfd, 0,
   7143 			  contents + 16 + bed->s->arch_size / 8);
   7144 	    }
   7145 	  else
   7146 	    {
   7147 	      unsigned long int maskwords, maskbitslog2, x;
   7148 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   7149 
   7150 	      x = cinfo.nsyms;
   7151 	      maskbitslog2 = 1;
   7152 	      while ((x >>= 1) != 0)
   7153 		++maskbitslog2;
   7154 	      if (maskbitslog2 < 3)
   7155 		maskbitslog2 = 5;
   7156 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   7157 		maskbitslog2 = maskbitslog2 + 3;
   7158 	      else
   7159 		maskbitslog2 = maskbitslog2 + 2;
   7160 	      if (bed->s->arch_size == 64)
   7161 		{
   7162 		  if (maskbitslog2 == 5)
   7163 		    maskbitslog2 = 6;
   7164 		  cinfo.shift1 = 6;
   7165 		}
   7166 	      else
   7167 		cinfo.shift1 = 5;
   7168 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   7169 	      cinfo.shift2 = maskbitslog2;
   7170 	      cinfo.maskbits = 1 << maskbitslog2;
   7171 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   7172 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   7173 	      amt += maskwords * sizeof (bfd_vma);
   7174 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   7175 	      if (cinfo.bitmask == NULL)
   7176 		{
   7177 		  free (cinfo.hashcodes);
   7178 		  return FALSE;
   7179 		}
   7180 
   7181 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   7182 	      cinfo.indx = cinfo.counts + bucketcount;
   7183 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   7184 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   7185 
   7186 	      /* Determine how often each hash bucket is used.  */
   7187 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   7188 	      for (i = 0; i < cinfo.nsyms; ++i)
   7189 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   7190 
   7191 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   7192 		if (cinfo.counts[i] != 0)
   7193 		  {
   7194 		    cinfo.indx[i] = cnt;
   7195 		    cnt += cinfo.counts[i];
   7196 		  }
   7197 	      BFD_ASSERT (cnt == dynsymcount);
   7198 	      cinfo.bucketcount = bucketcount;
   7199 	      cinfo.local_indx = cinfo.min_dynindx;
   7200 
   7201 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   7202 	      s->size += cinfo.maskbits / 8;
   7203 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   7204 	      if (contents == NULL)
   7205 		{
   7206 		  free (cinfo.bitmask);
   7207 		  free (cinfo.hashcodes);
   7208 		  return FALSE;
   7209 		}
   7210 
   7211 	      s->contents = contents;
   7212 	      bfd_put_32 (output_bfd, bucketcount, contents);
   7213 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   7214 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   7215 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   7216 	      contents += 16 + cinfo.maskbits / 8;
   7217 
   7218 	      for (i = 0; i < bucketcount; ++i)
   7219 		{
   7220 		  if (cinfo.counts[i] == 0)
   7221 		    bfd_put_32 (output_bfd, 0, contents);
   7222 		  else
   7223 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   7224 		  contents += 4;
   7225 		}
   7226 
   7227 	      cinfo.contents = contents;
   7228 
   7229 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
   7230 	      elf_link_hash_traverse (elf_hash_table (info),
   7231 				      elf_renumber_gnu_hash_syms, &cinfo);
   7232 
   7233 	      contents = s->contents + 16;
   7234 	      for (i = 0; i < maskwords; ++i)
   7235 		{
   7236 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   7237 			   contents);
   7238 		  contents += bed->s->arch_size / 8;
   7239 		}
   7240 
   7241 	      free (cinfo.bitmask);
   7242 	      free (cinfo.hashcodes);
   7243 	    }
   7244 	}
   7245 
   7246       s = bfd_get_linker_section (dynobj, ".dynstr");
   7247       BFD_ASSERT (s != NULL);
   7248 
   7249       elf_finalize_dynstr (output_bfd, info);
   7250 
   7251       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   7252 
   7253       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   7254 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   7255 	  return FALSE;
   7256     }
   7257 
   7258   return TRUE;
   7259 }
   7260 
   7261 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   7263 
   7264 static void
   7265 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   7266 			    asection *sec)
   7267 {
   7268   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   7269   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   7270 }
   7271 
   7272 /* Finish SHF_MERGE section merging.  */
   7273 
   7274 bfd_boolean
   7275 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
   7276 {
   7277   bfd *ibfd;
   7278   asection *sec;
   7279 
   7280   if (!is_elf_hash_table (info->hash))
   7281     return FALSE;
   7282 
   7283   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7284     if ((ibfd->flags & DYNAMIC) == 0
   7285 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   7286 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   7287 	    == get_elf_backend_data (obfd)->s->elfclass))
   7288       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7289 	if ((sec->flags & SEC_MERGE) != 0
   7290 	    && !bfd_is_abs_section (sec->output_section))
   7291 	  {
   7292 	    struct bfd_elf_section_data *secdata;
   7293 
   7294 	    secdata = elf_section_data (sec);
   7295 	    if (! _bfd_add_merge_section (obfd,
   7296 					  &elf_hash_table (info)->merge_info,
   7297 					  sec, &secdata->sec_info))
   7298 	      return FALSE;
   7299 	    else if (secdata->sec_info)
   7300 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   7301 	  }
   7302 
   7303   if (elf_hash_table (info)->merge_info != NULL)
   7304     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
   7305 			 merge_sections_remove_hook);
   7306   return TRUE;
   7307 }
   7308 
   7309 /* Create an entry in an ELF linker hash table.  */
   7310 
   7311 struct bfd_hash_entry *
   7312 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   7313 			    struct bfd_hash_table *table,
   7314 			    const char *string)
   7315 {
   7316   /* Allocate the structure if it has not already been allocated by a
   7317      subclass.  */
   7318   if (entry == NULL)
   7319     {
   7320       entry = (struct bfd_hash_entry *)
   7321 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   7322       if (entry == NULL)
   7323 	return entry;
   7324     }
   7325 
   7326   /* Call the allocation method of the superclass.  */
   7327   entry = _bfd_link_hash_newfunc (entry, table, string);
   7328   if (entry != NULL)
   7329     {
   7330       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   7331       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   7332 
   7333       /* Set local fields.  */
   7334       ret->indx = -1;
   7335       ret->dynindx = -1;
   7336       ret->got = htab->init_got_refcount;
   7337       ret->plt = htab->init_plt_refcount;
   7338       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   7339 			      - offsetof (struct elf_link_hash_entry, size)));
   7340       /* Assume that we have been called by a non-ELF symbol reader.
   7341 	 This flag is then reset by the code which reads an ELF input
   7342 	 file.  This ensures that a symbol created by a non-ELF symbol
   7343 	 reader will have the flag set correctly.  */
   7344       ret->non_elf = 1;
   7345     }
   7346 
   7347   return entry;
   7348 }
   7349 
   7350 /* Copy data from an indirect symbol to its direct symbol, hiding the
   7351    old indirect symbol.  Also used for copying flags to a weakdef.  */
   7352 
   7353 void
   7354 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   7355 				  struct elf_link_hash_entry *dir,
   7356 				  struct elf_link_hash_entry *ind)
   7357 {
   7358   struct elf_link_hash_table *htab;
   7359 
   7360   /* Copy down any references that we may have already seen to the
   7361      symbol which just became indirect.  */
   7362 
   7363   if (dir->versioned != versioned_hidden)
   7364     dir->ref_dynamic |= ind->ref_dynamic;
   7365   dir->ref_regular |= ind->ref_regular;
   7366   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   7367   dir->non_got_ref |= ind->non_got_ref;
   7368   dir->needs_plt |= ind->needs_plt;
   7369   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   7370 
   7371   if (ind->root.type != bfd_link_hash_indirect)
   7372     return;
   7373 
   7374   /* Copy over the global and procedure linkage table refcount entries.
   7375      These may have been already set up by a check_relocs routine.  */
   7376   htab = elf_hash_table (info);
   7377   if (ind->got.refcount > htab->init_got_refcount.refcount)
   7378     {
   7379       if (dir->got.refcount < 0)
   7380 	dir->got.refcount = 0;
   7381       dir->got.refcount += ind->got.refcount;
   7382       ind->got.refcount = htab->init_got_refcount.refcount;
   7383     }
   7384 
   7385   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   7386     {
   7387       if (dir->plt.refcount < 0)
   7388 	dir->plt.refcount = 0;
   7389       dir->plt.refcount += ind->plt.refcount;
   7390       ind->plt.refcount = htab->init_plt_refcount.refcount;
   7391     }
   7392 
   7393   if (ind->dynindx != -1)
   7394     {
   7395       if (dir->dynindx != -1)
   7396 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   7397       dir->dynindx = ind->dynindx;
   7398       dir->dynstr_index = ind->dynstr_index;
   7399       ind->dynindx = -1;
   7400       ind->dynstr_index = 0;
   7401     }
   7402 }
   7403 
   7404 void
   7405 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   7406 				struct elf_link_hash_entry *h,
   7407 				bfd_boolean force_local)
   7408 {
   7409   /* STT_GNU_IFUNC symbol must go through PLT.  */
   7410   if (h->type != STT_GNU_IFUNC)
   7411     {
   7412       h->plt = elf_hash_table (info)->init_plt_offset;
   7413       h->needs_plt = 0;
   7414     }
   7415   if (force_local)
   7416     {
   7417       h->forced_local = 1;
   7418       if (h->dynindx != -1)
   7419 	{
   7420 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7421 				  h->dynstr_index);
   7422 	  h->dynindx = -1;
   7423 	  h->dynstr_index = 0;
   7424 	}
   7425     }
   7426 }
   7427 
   7428 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   7429    caller.  */
   7430 
   7431 bfd_boolean
   7432 _bfd_elf_link_hash_table_init
   7433   (struct elf_link_hash_table *table,
   7434    bfd *abfd,
   7435    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   7436 				      struct bfd_hash_table *,
   7437 				      const char *),
   7438    unsigned int entsize,
   7439    enum elf_target_id target_id)
   7440 {
   7441   bfd_boolean ret;
   7442   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
   7443 
   7444   table->init_got_refcount.refcount = can_refcount - 1;
   7445   table->init_plt_refcount.refcount = can_refcount - 1;
   7446   table->init_got_offset.offset = -(bfd_vma) 1;
   7447   table->init_plt_offset.offset = -(bfd_vma) 1;
   7448   /* The first dynamic symbol is a dummy.  */
   7449   table->dynsymcount = 1;
   7450 
   7451   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   7452 
   7453   table->root.type = bfd_link_elf_hash_table;
   7454   table->hash_table_id = target_id;
   7455 
   7456   return ret;
   7457 }
   7458 
   7459 /* Create an ELF linker hash table.  */
   7460 
   7461 struct bfd_link_hash_table *
   7462 _bfd_elf_link_hash_table_create (bfd *abfd)
   7463 {
   7464   struct elf_link_hash_table *ret;
   7465   bfd_size_type amt = sizeof (struct elf_link_hash_table);
   7466 
   7467   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   7468   if (ret == NULL)
   7469     return NULL;
   7470 
   7471   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   7472 				       sizeof (struct elf_link_hash_entry),
   7473 				       GENERIC_ELF_DATA))
   7474     {
   7475       free (ret);
   7476       return NULL;
   7477     }
   7478   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
   7479 
   7480   return &ret->root;
   7481 }
   7482 
   7483 /* Destroy an ELF linker hash table.  */
   7484 
   7485 void
   7486 _bfd_elf_link_hash_table_free (bfd *obfd)
   7487 {
   7488   struct elf_link_hash_table *htab;
   7489 
   7490   htab = (struct elf_link_hash_table *) obfd->link.hash;
   7491   if (htab->dynstr != NULL)
   7492     _bfd_elf_strtab_free (htab->dynstr);
   7493   _bfd_merge_sections_free (htab->merge_info);
   7494   _bfd_generic_link_hash_table_free (obfd);
   7495 }
   7496 
   7497 /* This is a hook for the ELF emulation code in the generic linker to
   7498    tell the backend linker what file name to use for the DT_NEEDED
   7499    entry for a dynamic object.  */
   7500 
   7501 void
   7502 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   7503 {
   7504   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7505       && bfd_get_format (abfd) == bfd_object)
   7506     elf_dt_name (abfd) = name;
   7507 }
   7508 
   7509 int
   7510 bfd_elf_get_dyn_lib_class (bfd *abfd)
   7511 {
   7512   int lib_class;
   7513   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7514       && bfd_get_format (abfd) == bfd_object)
   7515     lib_class = elf_dyn_lib_class (abfd);
   7516   else
   7517     lib_class = 0;
   7518   return lib_class;
   7519 }
   7520 
   7521 void
   7522 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   7523 {
   7524   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7525       && bfd_get_format (abfd) == bfd_object)
   7526     elf_dyn_lib_class (abfd) = lib_class;
   7527 }
   7528 
   7529 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   7530    the linker ELF emulation code.  */
   7531 
   7532 struct bfd_link_needed_list *
   7533 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   7534 			 struct bfd_link_info *info)
   7535 {
   7536   if (! is_elf_hash_table (info->hash))
   7537     return NULL;
   7538   return elf_hash_table (info)->needed;
   7539 }
   7540 
   7541 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   7542    hook for the linker ELF emulation code.  */
   7543 
   7544 struct bfd_link_needed_list *
   7545 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   7546 			  struct bfd_link_info *info)
   7547 {
   7548   if (! is_elf_hash_table (info->hash))
   7549     return NULL;
   7550   return elf_hash_table (info)->runpath;
   7551 }
   7552 
   7553 /* Get the name actually used for a dynamic object for a link.  This
   7554    is the SONAME entry if there is one.  Otherwise, it is the string
   7555    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   7556 
   7557 const char *
   7558 bfd_elf_get_dt_soname (bfd *abfd)
   7559 {
   7560   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7561       && bfd_get_format (abfd) == bfd_object)
   7562     return elf_dt_name (abfd);
   7563   return NULL;
   7564 }
   7565 
   7566 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   7567    the ELF linker emulation code.  */
   7568 
   7569 bfd_boolean
   7570 bfd_elf_get_bfd_needed_list (bfd *abfd,
   7571 			     struct bfd_link_needed_list **pneeded)
   7572 {
   7573   asection *s;
   7574   bfd_byte *dynbuf = NULL;
   7575   unsigned int elfsec;
   7576   unsigned long shlink;
   7577   bfd_byte *extdyn, *extdynend;
   7578   size_t extdynsize;
   7579   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   7580 
   7581   *pneeded = NULL;
   7582 
   7583   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   7584       || bfd_get_format (abfd) != bfd_object)
   7585     return TRUE;
   7586 
   7587   s = bfd_get_section_by_name (abfd, ".dynamic");
   7588   if (s == NULL || s->size == 0)
   7589     return TRUE;
   7590 
   7591   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   7592     goto error_return;
   7593 
   7594   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   7595   if (elfsec == SHN_BAD)
   7596     goto error_return;
   7597 
   7598   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   7599 
   7600   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   7601   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   7602 
   7603   extdyn = dynbuf;
   7604   extdynend = extdyn + s->size;
   7605   for (; extdyn < extdynend; extdyn += extdynsize)
   7606     {
   7607       Elf_Internal_Dyn dyn;
   7608 
   7609       (*swap_dyn_in) (abfd, extdyn, &dyn);
   7610 
   7611       if (dyn.d_tag == DT_NULL)
   7612 	break;
   7613 
   7614       if (dyn.d_tag == DT_NEEDED)
   7615 	{
   7616 	  const char *string;
   7617 	  struct bfd_link_needed_list *l;
   7618 	  unsigned int tagv = dyn.d_un.d_val;
   7619 	  bfd_size_type amt;
   7620 
   7621 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   7622 	  if (string == NULL)
   7623 	    goto error_return;
   7624 
   7625 	  amt = sizeof *l;
   7626 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   7627 	  if (l == NULL)
   7628 	    goto error_return;
   7629 
   7630 	  l->by = abfd;
   7631 	  l->name = string;
   7632 	  l->next = *pneeded;
   7633 	  *pneeded = l;
   7634 	}
   7635     }
   7636 
   7637   free (dynbuf);
   7638 
   7639   return TRUE;
   7640 
   7641  error_return:
   7642   if (dynbuf != NULL)
   7643     free (dynbuf);
   7644   return FALSE;
   7645 }
   7646 
   7647 struct elf_symbuf_symbol
   7648 {
   7649   unsigned long st_name;	/* Symbol name, index in string tbl */
   7650   unsigned char st_info;	/* Type and binding attributes */
   7651   unsigned char st_other;	/* Visibilty, and target specific */
   7652 };
   7653 
   7654 struct elf_symbuf_head
   7655 {
   7656   struct elf_symbuf_symbol *ssym;
   7657   size_t count;
   7658   unsigned int st_shndx;
   7659 };
   7660 
   7661 struct elf_symbol
   7662 {
   7663   union
   7664     {
   7665       Elf_Internal_Sym *isym;
   7666       struct elf_symbuf_symbol *ssym;
   7667     } u;
   7668   const char *name;
   7669 };
   7670 
   7671 /* Sort references to symbols by ascending section number.  */
   7672 
   7673 static int
   7674 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   7675 {
   7676   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   7677   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   7678 
   7679   return s1->st_shndx - s2->st_shndx;
   7680 }
   7681 
   7682 static int
   7683 elf_sym_name_compare (const void *arg1, const void *arg2)
   7684 {
   7685   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   7686   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   7687   return strcmp (s1->name, s2->name);
   7688 }
   7689 
   7690 static struct elf_symbuf_head *
   7691 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
   7692 {
   7693   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   7694   struct elf_symbuf_symbol *ssym;
   7695   struct elf_symbuf_head *ssymbuf, *ssymhead;
   7696   size_t i, shndx_count, total_size;
   7697 
   7698   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
   7699   if (indbuf == NULL)
   7700     return NULL;
   7701 
   7702   for (ind = indbuf, i = 0; i < symcount; i++)
   7703     if (isymbuf[i].st_shndx != SHN_UNDEF)
   7704       *ind++ = &isymbuf[i];
   7705   indbufend = ind;
   7706 
   7707   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   7708 	 elf_sort_elf_symbol);
   7709 
   7710   shndx_count = 0;
   7711   if (indbufend > indbuf)
   7712     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   7713       if (ind[0]->st_shndx != ind[1]->st_shndx)
   7714 	shndx_count++;
   7715 
   7716   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   7717 		+ (indbufend - indbuf) * sizeof (*ssym));
   7718   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   7719   if (ssymbuf == NULL)
   7720     {
   7721       free (indbuf);
   7722       return NULL;
   7723     }
   7724 
   7725   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   7726   ssymbuf->ssym = NULL;
   7727   ssymbuf->count = shndx_count;
   7728   ssymbuf->st_shndx = 0;
   7729   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   7730     {
   7731       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   7732 	{
   7733 	  ssymhead++;
   7734 	  ssymhead->ssym = ssym;
   7735 	  ssymhead->count = 0;
   7736 	  ssymhead->st_shndx = (*ind)->st_shndx;
   7737 	}
   7738       ssym->st_name = (*ind)->st_name;
   7739       ssym->st_info = (*ind)->st_info;
   7740       ssym->st_other = (*ind)->st_other;
   7741       ssymhead->count++;
   7742     }
   7743   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
   7744 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
   7745 		  == total_size));
   7746 
   7747   free (indbuf);
   7748   return ssymbuf;
   7749 }
   7750 
   7751 /* Check if 2 sections define the same set of local and global
   7752    symbols.  */
   7753 
   7754 static bfd_boolean
   7755 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   7756 				   struct bfd_link_info *info)
   7757 {
   7758   bfd *bfd1, *bfd2;
   7759   const struct elf_backend_data *bed1, *bed2;
   7760   Elf_Internal_Shdr *hdr1, *hdr2;
   7761   size_t symcount1, symcount2;
   7762   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   7763   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   7764   Elf_Internal_Sym *isym, *isymend;
   7765   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   7766   size_t count1, count2, i;
   7767   unsigned int shndx1, shndx2;
   7768   bfd_boolean result;
   7769 
   7770   bfd1 = sec1->owner;
   7771   bfd2 = sec2->owner;
   7772 
   7773   /* Both sections have to be in ELF.  */
   7774   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   7775       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   7776     return FALSE;
   7777 
   7778   if (elf_section_type (sec1) != elf_section_type (sec2))
   7779     return FALSE;
   7780 
   7781   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   7782   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   7783   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   7784     return FALSE;
   7785 
   7786   bed1 = get_elf_backend_data (bfd1);
   7787   bed2 = get_elf_backend_data (bfd2);
   7788   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   7789   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   7790   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   7791   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   7792 
   7793   if (symcount1 == 0 || symcount2 == 0)
   7794     return FALSE;
   7795 
   7796   result = FALSE;
   7797   isymbuf1 = NULL;
   7798   isymbuf2 = NULL;
   7799   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   7800   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   7801 
   7802   if (ssymbuf1 == NULL)
   7803     {
   7804       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   7805 				       NULL, NULL, NULL);
   7806       if (isymbuf1 == NULL)
   7807 	goto done;
   7808 
   7809       if (!info->reduce_memory_overheads)
   7810 	elf_tdata (bfd1)->symbuf = ssymbuf1
   7811 	  = elf_create_symbuf (symcount1, isymbuf1);
   7812     }
   7813 
   7814   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   7815     {
   7816       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   7817 				       NULL, NULL, NULL);
   7818       if (isymbuf2 == NULL)
   7819 	goto done;
   7820 
   7821       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
   7822 	elf_tdata (bfd2)->symbuf = ssymbuf2
   7823 	  = elf_create_symbuf (symcount2, isymbuf2);
   7824     }
   7825 
   7826   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   7827     {
   7828       /* Optimized faster version.  */
   7829       size_t lo, hi, mid;
   7830       struct elf_symbol *symp;
   7831       struct elf_symbuf_symbol *ssym, *ssymend;
   7832 
   7833       lo = 0;
   7834       hi = ssymbuf1->count;
   7835       ssymbuf1++;
   7836       count1 = 0;
   7837       while (lo < hi)
   7838 	{
   7839 	  mid = (lo + hi) / 2;
   7840 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   7841 	    hi = mid;
   7842 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   7843 	    lo = mid + 1;
   7844 	  else
   7845 	    {
   7846 	      count1 = ssymbuf1[mid].count;
   7847 	      ssymbuf1 += mid;
   7848 	      break;
   7849 	    }
   7850 	}
   7851 
   7852       lo = 0;
   7853       hi = ssymbuf2->count;
   7854       ssymbuf2++;
   7855       count2 = 0;
   7856       while (lo < hi)
   7857 	{
   7858 	  mid = (lo + hi) / 2;
   7859 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   7860 	    hi = mid;
   7861 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   7862 	    lo = mid + 1;
   7863 	  else
   7864 	    {
   7865 	      count2 = ssymbuf2[mid].count;
   7866 	      ssymbuf2 += mid;
   7867 	      break;
   7868 	    }
   7869 	}
   7870 
   7871       if (count1 == 0 || count2 == 0 || count1 != count2)
   7872 	goto done;
   7873 
   7874       symtable1
   7875 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   7876       symtable2
   7877 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   7878       if (symtable1 == NULL || symtable2 == NULL)
   7879 	goto done;
   7880 
   7881       symp = symtable1;
   7882       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
   7883 	   ssym < ssymend; ssym++, symp++)
   7884 	{
   7885 	  symp->u.ssym = ssym;
   7886 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
   7887 							hdr1->sh_link,
   7888 							ssym->st_name);
   7889 	}
   7890 
   7891       symp = symtable2;
   7892       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
   7893 	   ssym < ssymend; ssym++, symp++)
   7894 	{
   7895 	  symp->u.ssym = ssym;
   7896 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
   7897 							hdr2->sh_link,
   7898 							ssym->st_name);
   7899 	}
   7900 
   7901       /* Sort symbol by name.  */
   7902       qsort (symtable1, count1, sizeof (struct elf_symbol),
   7903 	     elf_sym_name_compare);
   7904       qsort (symtable2, count1, sizeof (struct elf_symbol),
   7905 	     elf_sym_name_compare);
   7906 
   7907       for (i = 0; i < count1; i++)
   7908 	/* Two symbols must have the same binding, type and name.  */
   7909 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   7910 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   7911 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7912 	  goto done;
   7913 
   7914       result = TRUE;
   7915       goto done;
   7916     }
   7917 
   7918   symtable1 = (struct elf_symbol *)
   7919       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   7920   symtable2 = (struct elf_symbol *)
   7921       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   7922   if (symtable1 == NULL || symtable2 == NULL)
   7923     goto done;
   7924 
   7925   /* Count definitions in the section.  */
   7926   count1 = 0;
   7927   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   7928     if (isym->st_shndx == shndx1)
   7929       symtable1[count1++].u.isym = isym;
   7930 
   7931   count2 = 0;
   7932   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   7933     if (isym->st_shndx == shndx2)
   7934       symtable2[count2++].u.isym = isym;
   7935 
   7936   if (count1 == 0 || count2 == 0 || count1 != count2)
   7937     goto done;
   7938 
   7939   for (i = 0; i < count1; i++)
   7940     symtable1[i].name
   7941       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   7942 					 symtable1[i].u.isym->st_name);
   7943 
   7944   for (i = 0; i < count2; i++)
   7945     symtable2[i].name
   7946       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   7947 					 symtable2[i].u.isym->st_name);
   7948 
   7949   /* Sort symbol by name.  */
   7950   qsort (symtable1, count1, sizeof (struct elf_symbol),
   7951 	 elf_sym_name_compare);
   7952   qsort (symtable2, count1, sizeof (struct elf_symbol),
   7953 	 elf_sym_name_compare);
   7954 
   7955   for (i = 0; i < count1; i++)
   7956     /* Two symbols must have the same binding, type and name.  */
   7957     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   7958 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   7959 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7960       goto done;
   7961 
   7962   result = TRUE;
   7963 
   7964 done:
   7965   if (symtable1)
   7966     free (symtable1);
   7967   if (symtable2)
   7968     free (symtable2);
   7969   if (isymbuf1)
   7970     free (isymbuf1);
   7971   if (isymbuf2)
   7972     free (isymbuf2);
   7973 
   7974   return result;
   7975 }
   7976 
   7977 /* Return TRUE if 2 section types are compatible.  */
   7978 
   7979 bfd_boolean
   7980 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   7981 				 bfd *bbfd, const asection *bsec)
   7982 {
   7983   if (asec == NULL
   7984       || bsec == NULL
   7985       || abfd->xvec->flavour != bfd_target_elf_flavour
   7986       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   7987     return TRUE;
   7988 
   7989   return elf_section_type (asec) == elf_section_type (bsec);
   7990 }
   7991 
   7992 /* Final phase of ELF linker.  */
   7994 
   7995 /* A structure we use to avoid passing large numbers of arguments.  */
   7996 
   7997 struct elf_final_link_info
   7998 {
   7999   /* General link information.  */
   8000   struct bfd_link_info *info;
   8001   /* Output BFD.  */
   8002   bfd *output_bfd;
   8003   /* Symbol string table.  */
   8004   struct elf_strtab_hash *symstrtab;
   8005   /* .hash section.  */
   8006   asection *hash_sec;
   8007   /* symbol version section (.gnu.version).  */
   8008   asection *symver_sec;
   8009   /* Buffer large enough to hold contents of any section.  */
   8010   bfd_byte *contents;
   8011   /* Buffer large enough to hold external relocs of any section.  */
   8012   void *external_relocs;
   8013   /* Buffer large enough to hold internal relocs of any section.  */
   8014   Elf_Internal_Rela *internal_relocs;
   8015   /* Buffer large enough to hold external local symbols of any input
   8016      BFD.  */
   8017   bfd_byte *external_syms;
   8018   /* And a buffer for symbol section indices.  */
   8019   Elf_External_Sym_Shndx *locsym_shndx;
   8020   /* Buffer large enough to hold internal local symbols of any input
   8021      BFD.  */
   8022   Elf_Internal_Sym *internal_syms;
   8023   /* Array large enough to hold a symbol index for each local symbol
   8024      of any input BFD.  */
   8025   long *indices;
   8026   /* Array large enough to hold a section pointer for each local
   8027      symbol of any input BFD.  */
   8028   asection **sections;
   8029   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   8030   Elf_External_Sym_Shndx *symshndxbuf;
   8031   /* Number of STT_FILE syms seen.  */
   8032   size_t filesym_count;
   8033 };
   8034 
   8035 /* This struct is used to pass information to elf_link_output_extsym.  */
   8036 
   8037 struct elf_outext_info
   8038 {
   8039   bfd_boolean failed;
   8040   bfd_boolean localsyms;
   8041   bfd_boolean file_sym_done;
   8042   struct elf_final_link_info *flinfo;
   8043 };
   8044 
   8045 
   8046 /* Support for evaluating a complex relocation.
   8047 
   8048    Complex relocations are generalized, self-describing relocations.  The
   8049    implementation of them consists of two parts: complex symbols, and the
   8050    relocations themselves.
   8051 
   8052    The relocations are use a reserved elf-wide relocation type code (R_RELC
   8053    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   8054    information (start bit, end bit, word width, etc) into the addend.  This
   8055    information is extracted from CGEN-generated operand tables within gas.
   8056 
   8057    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
   8058    internal) representing prefix-notation expressions, including but not
   8059    limited to those sorts of expressions normally encoded as addends in the
   8060    addend field.  The symbol mangling format is:
   8061 
   8062    <node> := <literal>
   8063 	  |  <unary-operator> ':' <node>
   8064 	  |  <binary-operator> ':' <node> ':' <node>
   8065 	  ;
   8066 
   8067    <literal> := 's' <digits=N> ':' <N character symbol name>
   8068 	     |  'S' <digits=N> ':' <N character section name>
   8069 	     |  '#' <hexdigits>
   8070 	     ;
   8071 
   8072    <binary-operator> := as in C
   8073    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   8074 
   8075 static void
   8076 set_symbol_value (bfd *bfd_with_globals,
   8077 		  Elf_Internal_Sym *isymbuf,
   8078 		  size_t locsymcount,
   8079 		  size_t symidx,
   8080 		  bfd_vma val)
   8081 {
   8082   struct elf_link_hash_entry **sym_hashes;
   8083   struct elf_link_hash_entry *h;
   8084   size_t extsymoff = locsymcount;
   8085 
   8086   if (symidx < locsymcount)
   8087     {
   8088       Elf_Internal_Sym *sym;
   8089 
   8090       sym = isymbuf + symidx;
   8091       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   8092 	{
   8093 	  /* It is a local symbol: move it to the
   8094 	     "absolute" section and give it a value.  */
   8095 	  sym->st_shndx = SHN_ABS;
   8096 	  sym->st_value = val;
   8097 	  return;
   8098 	}
   8099       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   8100       extsymoff = 0;
   8101     }
   8102 
   8103   /* It is a global symbol: set its link type
   8104      to "defined" and give it a value.  */
   8105 
   8106   sym_hashes = elf_sym_hashes (bfd_with_globals);
   8107   h = sym_hashes [symidx - extsymoff];
   8108   while (h->root.type == bfd_link_hash_indirect
   8109 	 || h->root.type == bfd_link_hash_warning)
   8110     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8111   h->root.type = bfd_link_hash_defined;
   8112   h->root.u.def.value = val;
   8113   h->root.u.def.section = bfd_abs_section_ptr;
   8114 }
   8115 
   8116 static bfd_boolean
   8117 resolve_symbol (const char *name,
   8118 		bfd *input_bfd,
   8119 		struct elf_final_link_info *flinfo,
   8120 		bfd_vma *result,
   8121 		Elf_Internal_Sym *isymbuf,
   8122 		size_t locsymcount)
   8123 {
   8124   Elf_Internal_Sym *sym;
   8125   struct bfd_link_hash_entry *global_entry;
   8126   const char *candidate = NULL;
   8127   Elf_Internal_Shdr *symtab_hdr;
   8128   size_t i;
   8129 
   8130   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   8131 
   8132   for (i = 0; i < locsymcount; ++ i)
   8133     {
   8134       sym = isymbuf + i;
   8135 
   8136       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   8137 	continue;
   8138 
   8139       candidate = bfd_elf_string_from_elf_section (input_bfd,
   8140 						   symtab_hdr->sh_link,
   8141 						   sym->st_name);
   8142 #ifdef DEBUG
   8143       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   8144 	      name, candidate, (unsigned long) sym->st_value);
   8145 #endif
   8146       if (candidate && strcmp (candidate, name) == 0)
   8147 	{
   8148 	  asection *sec = flinfo->sections [i];
   8149 
   8150 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   8151 	  *result += sec->output_offset + sec->output_section->vma;
   8152 #ifdef DEBUG
   8153 	  printf ("Found symbol with value %8.8lx\n",
   8154 		  (unsigned long) *result);
   8155 #endif
   8156 	  return TRUE;
   8157 	}
   8158     }
   8159 
   8160   /* Hmm, haven't found it yet. perhaps it is a global.  */
   8161   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   8162 				       FALSE, FALSE, TRUE);
   8163   if (!global_entry)
   8164     return FALSE;
   8165 
   8166   if (global_entry->type == bfd_link_hash_defined
   8167       || global_entry->type == bfd_link_hash_defweak)
   8168     {
   8169       *result = (global_entry->u.def.value
   8170 		 + global_entry->u.def.section->output_section->vma
   8171 		 + global_entry->u.def.section->output_offset);
   8172 #ifdef DEBUG
   8173       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   8174 	      global_entry->root.string, (unsigned long) *result);
   8175 #endif
   8176       return TRUE;
   8177     }
   8178 
   8179   return FALSE;
   8180 }
   8181 
   8182 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
   8183    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
   8184    names like "foo.end" which is the end address of section "foo".  */
   8185 
   8186 static bfd_boolean
   8187 resolve_section (const char *name,
   8188 		 asection *sections,
   8189 		 bfd_vma *result,
   8190 		 bfd * abfd)
   8191 {
   8192   asection *curr;
   8193   unsigned int len;
   8194 
   8195   for (curr = sections; curr; curr = curr->next)
   8196     if (strcmp (curr->name, name) == 0)
   8197       {
   8198 	*result = curr->vma;
   8199 	return TRUE;
   8200       }
   8201 
   8202   /* Hmm. still haven't found it. try pseudo-section names.  */
   8203   /* FIXME: This could be coded more efficiently...  */
   8204   for (curr = sections; curr; curr = curr->next)
   8205     {
   8206       len = strlen (curr->name);
   8207       if (len > strlen (name))
   8208 	continue;
   8209 
   8210       if (strncmp (curr->name, name, len) == 0)
   8211 	{
   8212 	  if (strncmp (".end", name + len, 4) == 0)
   8213 	    {
   8214 	      *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
   8215 	      return TRUE;
   8216 	    }
   8217 
   8218 	  /* Insert more pseudo-section names here, if you like.  */
   8219 	}
   8220     }
   8221 
   8222   return FALSE;
   8223 }
   8224 
   8225 static void
   8226 undefined_reference (const char *reftype, const char *name)
   8227 {
   8228   /* xgettext:c-format */
   8229   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   8230 		      reftype, name);
   8231 }
   8232 
   8233 static bfd_boolean
   8234 eval_symbol (bfd_vma *result,
   8235 	     const char **symp,
   8236 	     bfd *input_bfd,
   8237 	     struct elf_final_link_info *flinfo,
   8238 	     bfd_vma dot,
   8239 	     Elf_Internal_Sym *isymbuf,
   8240 	     size_t locsymcount,
   8241 	     int signed_p)
   8242 {
   8243   size_t len;
   8244   size_t symlen;
   8245   bfd_vma a;
   8246   bfd_vma b;
   8247   char symbuf[4096];
   8248   const char *sym = *symp;
   8249   const char *symend;
   8250   bfd_boolean symbol_is_section = FALSE;
   8251 
   8252   len = strlen (sym);
   8253   symend = sym + len;
   8254 
   8255   if (len < 1 || len > sizeof (symbuf))
   8256     {
   8257       bfd_set_error (bfd_error_invalid_operation);
   8258       return FALSE;
   8259     }
   8260 
   8261   switch (* sym)
   8262     {
   8263     case '.':
   8264       *result = dot;
   8265       *symp = sym + 1;
   8266       return TRUE;
   8267 
   8268     case '#':
   8269       ++sym;
   8270       *result = strtoul (sym, (char **) symp, 16);
   8271       return TRUE;
   8272 
   8273     case 'S':
   8274       symbol_is_section = TRUE;
   8275       /* Fall through.  */
   8276     case 's':
   8277       ++sym;
   8278       symlen = strtol (sym, (char **) symp, 10);
   8279       sym = *symp + 1; /* Skip the trailing ':'.  */
   8280 
   8281       if (symend < sym || symlen + 1 > sizeof (symbuf))
   8282 	{
   8283 	  bfd_set_error (bfd_error_invalid_operation);
   8284 	  return FALSE;
   8285 	}
   8286 
   8287       memcpy (symbuf, sym, symlen);
   8288       symbuf[symlen] = '\0';
   8289       *symp = sym + symlen;
   8290 
   8291       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   8292 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   8293 	 interpretation here; section means "try section first", not "must be a
   8294 	 section", and likewise with symbol.  */
   8295 
   8296       if (symbol_is_section)
   8297 	{
   8298 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
   8299 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   8300 				  isymbuf, locsymcount))
   8301 	    {
   8302 	      undefined_reference ("section", symbuf);
   8303 	      return FALSE;
   8304 	    }
   8305 	}
   8306       else
   8307 	{
   8308 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   8309 			       isymbuf, locsymcount)
   8310 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   8311 				   result, input_bfd))
   8312 	    {
   8313 	      undefined_reference ("symbol", symbuf);
   8314 	      return FALSE;
   8315 	    }
   8316 	}
   8317 
   8318       return TRUE;
   8319 
   8320       /* All that remains are operators.  */
   8321 
   8322 #define UNARY_OP(op)						\
   8323   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   8324     {								\
   8325       sym += strlen (#op);					\
   8326       if (*sym == ':')						\
   8327 	++sym;							\
   8328       *symp = sym;						\
   8329       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   8330 			isymbuf, locsymcount, signed_p))	\
   8331 	return FALSE;						\
   8332       if (signed_p)						\
   8333 	*result = op ((bfd_signed_vma) a);			\
   8334       else							\
   8335 	*result = op a;						\
   8336       return TRUE;						\
   8337     }
   8338 
   8339 #define BINARY_OP(op)						\
   8340   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   8341     {								\
   8342       sym += strlen (#op);					\
   8343       if (*sym == ':')						\
   8344 	++sym;							\
   8345       *symp = sym;						\
   8346       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   8347 			isymbuf, locsymcount, signed_p))	\
   8348 	return FALSE;						\
   8349       ++*symp;							\
   8350       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   8351 			isymbuf, locsymcount, signed_p))	\
   8352 	return FALSE;						\
   8353       if (signed_p)						\
   8354 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   8355       else							\
   8356 	*result = a op b;					\
   8357       return TRUE;						\
   8358     }
   8359 
   8360     default:
   8361       UNARY_OP  (0-);
   8362       BINARY_OP (<<);
   8363       BINARY_OP (>>);
   8364       BINARY_OP (==);
   8365       BINARY_OP (!=);
   8366       BINARY_OP (<=);
   8367       BINARY_OP (>=);
   8368       BINARY_OP (&&);
   8369       BINARY_OP (||);
   8370       UNARY_OP  (~);
   8371       UNARY_OP  (!);
   8372       BINARY_OP (*);
   8373       BINARY_OP (/);
   8374       BINARY_OP (%);
   8375       BINARY_OP (^);
   8376       BINARY_OP (|);
   8377       BINARY_OP (&);
   8378       BINARY_OP (+);
   8379       BINARY_OP (-);
   8380       BINARY_OP (<);
   8381       BINARY_OP (>);
   8382 #undef UNARY_OP
   8383 #undef BINARY_OP
   8384       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   8385       bfd_set_error (bfd_error_invalid_operation);
   8386       return FALSE;
   8387     }
   8388 }
   8389 
   8390 static void
   8391 put_value (bfd_vma size,
   8392 	   unsigned long chunksz,
   8393 	   bfd *input_bfd,
   8394 	   bfd_vma x,
   8395 	   bfd_byte *location)
   8396 {
   8397   location += (size - chunksz);
   8398 
   8399   for (; size; size -= chunksz, location -= chunksz)
   8400     {
   8401       switch (chunksz)
   8402 	{
   8403 	case 1:
   8404 	  bfd_put_8 (input_bfd, x, location);
   8405 	  x >>= 8;
   8406 	  break;
   8407 	case 2:
   8408 	  bfd_put_16 (input_bfd, x, location);
   8409 	  x >>= 16;
   8410 	  break;
   8411 	case 4:
   8412 	  bfd_put_32 (input_bfd, x, location);
   8413 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   8414 	  x >>= 16;
   8415 	  x >>= 16;
   8416 	  break;
   8417 #ifdef BFD64
   8418 	case 8:
   8419 	  bfd_put_64 (input_bfd, x, location);
   8420 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   8421 	  x >>= 32;
   8422 	  x >>= 32;
   8423 	  break;
   8424 #endif
   8425 	default:
   8426 	  abort ();
   8427 	  break;
   8428 	}
   8429     }
   8430 }
   8431 
   8432 static bfd_vma
   8433 get_value (bfd_vma size,
   8434 	   unsigned long chunksz,
   8435 	   bfd *input_bfd,
   8436 	   bfd_byte *location)
   8437 {
   8438   int shift;
   8439   bfd_vma x = 0;
   8440 
   8441   /* Sanity checks.  */
   8442   BFD_ASSERT (chunksz <= sizeof (x)
   8443 	      && size >= chunksz
   8444 	      && chunksz != 0
   8445 	      && (size % chunksz) == 0
   8446 	      && input_bfd != NULL
   8447 	      && location != NULL);
   8448 
   8449   if (chunksz == sizeof (x))
   8450     {
   8451       BFD_ASSERT (size == chunksz);
   8452 
   8453       /* Make sure that we do not perform an undefined shift operation.
   8454 	 We know that size == chunksz so there will only be one iteration
   8455 	 of the loop below.  */
   8456       shift = 0;
   8457     }
   8458   else
   8459     shift = 8 * chunksz;
   8460 
   8461   for (; size; size -= chunksz, location += chunksz)
   8462     {
   8463       switch (chunksz)
   8464 	{
   8465 	case 1:
   8466 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   8467 	  break;
   8468 	case 2:
   8469 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   8470 	  break;
   8471 	case 4:
   8472 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   8473 	  break;
   8474 #ifdef BFD64
   8475 	case 8:
   8476 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   8477 	  break;
   8478 #endif
   8479 	default:
   8480 	  abort ();
   8481 	}
   8482     }
   8483   return x;
   8484 }
   8485 
   8486 static void
   8487 decode_complex_addend (unsigned long *start,   /* in bits */
   8488 		       unsigned long *oplen,   /* in bits */
   8489 		       unsigned long *len,     /* in bits */
   8490 		       unsigned long *wordsz,  /* in bytes */
   8491 		       unsigned long *chunksz, /* in bytes */
   8492 		       unsigned long *lsb0_p,
   8493 		       unsigned long *signed_p,
   8494 		       unsigned long *trunc_p,
   8495 		       unsigned long encoded)
   8496 {
   8497   * start     =	 encoded	& 0x3F;
   8498   * len	      = (encoded >>  6) & 0x3F;
   8499   * oplen     = (encoded >> 12) & 0x3F;
   8500   * wordsz    = (encoded >> 18) & 0xF;
   8501   * chunksz   = (encoded >> 22) & 0xF;
   8502   * lsb0_p    = (encoded >> 27) & 1;
   8503   * signed_p  = (encoded >> 28) & 1;
   8504   * trunc_p   = (encoded >> 29) & 1;
   8505 }
   8506 
   8507 bfd_reloc_status_type
   8508 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   8509 				    asection *input_section ATTRIBUTE_UNUSED,
   8510 				    bfd_byte *contents,
   8511 				    Elf_Internal_Rela *rel,
   8512 				    bfd_vma relocation)
   8513 {
   8514   bfd_vma shift, x, mask;
   8515   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   8516   bfd_reloc_status_type r;
   8517 
   8518   /*  Perform this reloc, since it is complex.
   8519       (this is not to say that it necessarily refers to a complex
   8520       symbol; merely that it is a self-describing CGEN based reloc.
   8521       i.e. the addend has the complete reloc information (bit start, end,
   8522       word size, etc) encoded within it.).  */
   8523 
   8524   decode_complex_addend (&start, &oplen, &len, &wordsz,
   8525 			 &chunksz, &lsb0_p, &signed_p,
   8526 			 &trunc_p, rel->r_addend);
   8527 
   8528   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   8529 
   8530   if (lsb0_p)
   8531     shift = (start + 1) - len;
   8532   else
   8533     shift = (8 * wordsz) - (start + len);
   8534 
   8535   x = get_value (wordsz, chunksz, input_bfd,
   8536 		 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
   8537 
   8538 #ifdef DEBUG
   8539   printf ("Doing complex reloc: "
   8540 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   8541 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   8542 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   8543 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   8544 	  oplen, (unsigned long) x, (unsigned long) mask,
   8545 	  (unsigned long) relocation);
   8546 #endif
   8547 
   8548   r = bfd_reloc_ok;
   8549   if (! trunc_p)
   8550     /* Now do an overflow check.  */
   8551     r = bfd_check_overflow ((signed_p
   8552 			     ? complain_overflow_signed
   8553 			     : complain_overflow_unsigned),
   8554 			    len, 0, (8 * wordsz),
   8555 			    relocation);
   8556 
   8557   /* Do the deed.  */
   8558   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   8559 
   8560 #ifdef DEBUG
   8561   printf ("           relocation: %8.8lx\n"
   8562 	  "         shifted mask: %8.8lx\n"
   8563 	  " shifted/masked reloc: %8.8lx\n"
   8564 	  "               result: %8.8lx\n",
   8565 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   8566 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   8567 #endif
   8568   put_value (wordsz, chunksz, input_bfd, x,
   8569 	     contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
   8570   return r;
   8571 }
   8572 
   8573 /* Functions to read r_offset from external (target order) reloc
   8574    entry.  Faster than bfd_getl32 et al, because we let the compiler
   8575    know the value is aligned.  */
   8576 
   8577 static bfd_vma
   8578 ext32l_r_offset (const void *p)
   8579 {
   8580   union aligned32
   8581   {
   8582     uint32_t v;
   8583     unsigned char c[4];
   8584   };
   8585   const union aligned32 *a
   8586     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8587 
   8588   uint32_t aval = (  (uint32_t) a->c[0]
   8589 		   | (uint32_t) a->c[1] << 8
   8590 		   | (uint32_t) a->c[2] << 16
   8591 		   | (uint32_t) a->c[3] << 24);
   8592   return aval;
   8593 }
   8594 
   8595 static bfd_vma
   8596 ext32b_r_offset (const void *p)
   8597 {
   8598   union aligned32
   8599   {
   8600     uint32_t v;
   8601     unsigned char c[4];
   8602   };
   8603   const union aligned32 *a
   8604     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8605 
   8606   uint32_t aval = (  (uint32_t) a->c[0] << 24
   8607 		   | (uint32_t) a->c[1] << 16
   8608 		   | (uint32_t) a->c[2] << 8
   8609 		   | (uint32_t) a->c[3]);
   8610   return aval;
   8611 }
   8612 
   8613 #ifdef BFD_HOST_64_BIT
   8614 static bfd_vma
   8615 ext64l_r_offset (const void *p)
   8616 {
   8617   union aligned64
   8618   {
   8619     uint64_t v;
   8620     unsigned char c[8];
   8621   };
   8622   const union aligned64 *a
   8623     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8624 
   8625   uint64_t aval = (  (uint64_t) a->c[0]
   8626 		   | (uint64_t) a->c[1] << 8
   8627 		   | (uint64_t) a->c[2] << 16
   8628 		   | (uint64_t) a->c[3] << 24
   8629 		   | (uint64_t) a->c[4] << 32
   8630 		   | (uint64_t) a->c[5] << 40
   8631 		   | (uint64_t) a->c[6] << 48
   8632 		   | (uint64_t) a->c[7] << 56);
   8633   return aval;
   8634 }
   8635 
   8636 static bfd_vma
   8637 ext64b_r_offset (const void *p)
   8638 {
   8639   union aligned64
   8640   {
   8641     uint64_t v;
   8642     unsigned char c[8];
   8643   };
   8644   const union aligned64 *a
   8645     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8646 
   8647   uint64_t aval = (  (uint64_t) a->c[0] << 56
   8648 		   | (uint64_t) a->c[1] << 48
   8649 		   | (uint64_t) a->c[2] << 40
   8650 		   | (uint64_t) a->c[3] << 32
   8651 		   | (uint64_t) a->c[4] << 24
   8652 		   | (uint64_t) a->c[5] << 16
   8653 		   | (uint64_t) a->c[6] << 8
   8654 		   | (uint64_t) a->c[7]);
   8655   return aval;
   8656 }
   8657 #endif
   8658 
   8659 /* When performing a relocatable link, the input relocations are
   8660    preserved.  But, if they reference global symbols, the indices
   8661    referenced must be updated.  Update all the relocations found in
   8662    RELDATA.  */
   8663 
   8664 static bfd_boolean
   8665 elf_link_adjust_relocs (bfd *abfd,
   8666 			asection *sec,
   8667 			struct bfd_elf_section_reloc_data *reldata,
   8668 			bfd_boolean sort,
   8669 			struct bfd_link_info *info)
   8670 {
   8671   unsigned int i;
   8672   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8673   bfd_byte *erela;
   8674   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8675   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8676   bfd_vma r_type_mask;
   8677   int r_sym_shift;
   8678   unsigned int count = reldata->count;
   8679   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   8680 
   8681   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   8682     {
   8683       swap_in = bed->s->swap_reloc_in;
   8684       swap_out = bed->s->swap_reloc_out;
   8685     }
   8686   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   8687     {
   8688       swap_in = bed->s->swap_reloca_in;
   8689       swap_out = bed->s->swap_reloca_out;
   8690     }
   8691   else
   8692     abort ();
   8693 
   8694   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   8695     abort ();
   8696 
   8697   if (bed->s->arch_size == 32)
   8698     {
   8699       r_type_mask = 0xff;
   8700       r_sym_shift = 8;
   8701     }
   8702   else
   8703     {
   8704       r_type_mask = 0xffffffff;
   8705       r_sym_shift = 32;
   8706     }
   8707 
   8708   erela = reldata->hdr->contents;
   8709   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   8710     {
   8711       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   8712       unsigned int j;
   8713 
   8714       if (*rel_hash == NULL)
   8715 	continue;
   8716 
   8717       if ((*rel_hash)->indx == -2
   8718 	  && info->gc_sections
   8719 	  && ! info->gc_keep_exported)
   8720 	{
   8721 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
   8722 	  _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
   8723 			      abfd, sec,
   8724 			      (*rel_hash)->root.root.string);
   8725 	  _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
   8726 			      abfd, sec);
   8727 	  bfd_set_error (bfd_error_invalid_operation);
   8728 	  return FALSE;
   8729 	}
   8730       BFD_ASSERT ((*rel_hash)->indx >= 0);
   8731 
   8732       (*swap_in) (abfd, erela, irela);
   8733       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   8734 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   8735 			   | (irela[j].r_info & r_type_mask));
   8736       (*swap_out) (abfd, irela, erela);
   8737     }
   8738 
   8739   if (bed->elf_backend_update_relocs)
   8740     (*bed->elf_backend_update_relocs) (sec, reldata);
   8741 
   8742   if (sort && count != 0)
   8743     {
   8744       bfd_vma (*ext_r_off) (const void *);
   8745       bfd_vma r_off;
   8746       size_t elt_size;
   8747       bfd_byte *base, *end, *p, *loc;
   8748       bfd_byte *buf = NULL;
   8749 
   8750       if (bed->s->arch_size == 32)
   8751 	{
   8752 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   8753 	    ext_r_off = ext32l_r_offset;
   8754 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   8755 	    ext_r_off = ext32b_r_offset;
   8756 	  else
   8757 	    abort ();
   8758 	}
   8759       else
   8760 	{
   8761 #ifdef BFD_HOST_64_BIT
   8762 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   8763 	    ext_r_off = ext64l_r_offset;
   8764 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   8765 	    ext_r_off = ext64b_r_offset;
   8766 	  else
   8767 #endif
   8768 	    abort ();
   8769 	}
   8770 
   8771       /*  Must use a stable sort here.  A modified insertion sort,
   8772 	  since the relocs are mostly sorted already.  */
   8773       elt_size = reldata->hdr->sh_entsize;
   8774       base = reldata->hdr->contents;
   8775       end = base + count * elt_size;
   8776       if (elt_size > sizeof (Elf64_External_Rela))
   8777 	abort ();
   8778 
   8779       /* Ensure the first element is lowest.  This acts as a sentinel,
   8780 	 speeding the main loop below.  */
   8781       r_off = (*ext_r_off) (base);
   8782       for (p = loc = base; (p += elt_size) < end; )
   8783 	{
   8784 	  bfd_vma r_off2 = (*ext_r_off) (p);
   8785 	  if (r_off > r_off2)
   8786 	    {
   8787 	      r_off = r_off2;
   8788 	      loc = p;
   8789 	    }
   8790 	}
   8791       if (loc != base)
   8792 	{
   8793 	  /* Don't just swap *base and *loc as that changes the order
   8794 	     of the original base[0] and base[1] if they happen to
   8795 	     have the same r_offset.  */
   8796 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   8797 	  memcpy (onebuf, loc, elt_size);
   8798 	  memmove (base + elt_size, base, loc - base);
   8799 	  memcpy (base, onebuf, elt_size);
   8800 	}
   8801 
   8802       for (p = base + elt_size; (p += elt_size) < end; )
   8803 	{
   8804 	  /* base to p is sorted, *p is next to insert.  */
   8805 	  r_off = (*ext_r_off) (p);
   8806 	  /* Search the sorted region for location to insert.  */
   8807 	  loc = p - elt_size;
   8808 	  while (r_off < (*ext_r_off) (loc))
   8809 	    loc -= elt_size;
   8810 	  loc += elt_size;
   8811 	  if (loc != p)
   8812 	    {
   8813 	      /* Chances are there is a run of relocs to insert here,
   8814 		 from one of more input files.  Files are not always
   8815 		 linked in order due to the way elf_link_input_bfd is
   8816 		 called.  See pr17666.  */
   8817 	      size_t sortlen = p - loc;
   8818 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   8819 	      size_t runlen = elt_size;
   8820 	      size_t buf_size = 96 * 1024;
   8821 	      while (p + runlen < end
   8822 		     && (sortlen <= buf_size
   8823 			 || runlen + elt_size <= buf_size)
   8824 		     && r_off2 > (*ext_r_off) (p + runlen))
   8825 		runlen += elt_size;
   8826 	      if (buf == NULL)
   8827 		{
   8828 		  buf = bfd_malloc (buf_size);
   8829 		  if (buf == NULL)
   8830 		    return FALSE;
   8831 		}
   8832 	      if (runlen < sortlen)
   8833 		{
   8834 		  memcpy (buf, p, runlen);
   8835 		  memmove (loc + runlen, loc, sortlen);
   8836 		  memcpy (loc, buf, runlen);
   8837 		}
   8838 	      else
   8839 		{
   8840 		  memcpy (buf, loc, sortlen);
   8841 		  memmove (loc, p, runlen);
   8842 		  memcpy (loc + runlen, buf, sortlen);
   8843 		}
   8844 	      p += runlen - elt_size;
   8845 	    }
   8846 	}
   8847       /* Hashes are no longer valid.  */
   8848       free (reldata->hashes);
   8849       reldata->hashes = NULL;
   8850       free (buf);
   8851     }
   8852   return TRUE;
   8853 }
   8854 
   8855 struct elf_link_sort_rela
   8856 {
   8857   union {
   8858     bfd_vma offset;
   8859     bfd_vma sym_mask;
   8860   } u;
   8861   enum elf_reloc_type_class type;
   8862   /* We use this as an array of size int_rels_per_ext_rel.  */
   8863   Elf_Internal_Rela rela[1];
   8864 };
   8865 
   8866 static int
   8867 elf_link_sort_cmp1 (const void *A, const void *B)
   8868 {
   8869   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8870   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8871   int relativea, relativeb;
   8872 
   8873   relativea = a->type == reloc_class_relative;
   8874   relativeb = b->type == reloc_class_relative;
   8875 
   8876   if (relativea < relativeb)
   8877     return 1;
   8878   if (relativea > relativeb)
   8879     return -1;
   8880   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   8881     return -1;
   8882   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   8883     return 1;
   8884   if (a->rela->r_offset < b->rela->r_offset)
   8885     return -1;
   8886   if (a->rela->r_offset > b->rela->r_offset)
   8887     return 1;
   8888   return 0;
   8889 }
   8890 
   8891 static int
   8892 elf_link_sort_cmp2 (const void *A, const void *B)
   8893 {
   8894   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8895   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8896 
   8897   if (a->type < b->type)
   8898     return -1;
   8899   if (a->type > b->type)
   8900     return 1;
   8901   if (a->u.offset < b->u.offset)
   8902     return -1;
   8903   if (a->u.offset > b->u.offset)
   8904     return 1;
   8905   if (a->rela->r_offset < b->rela->r_offset)
   8906     return -1;
   8907   if (a->rela->r_offset > b->rela->r_offset)
   8908     return 1;
   8909   return 0;
   8910 }
   8911 
   8912 static size_t
   8913 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   8914 {
   8915   asection *dynamic_relocs;
   8916   asection *rela_dyn;
   8917   asection *rel_dyn;
   8918   bfd_size_type count, size;
   8919   size_t i, ret, sort_elt, ext_size;
   8920   bfd_byte *sort, *s_non_relative, *p;
   8921   struct elf_link_sort_rela *sq;
   8922   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8923   int i2e = bed->s->int_rels_per_ext_rel;
   8924   unsigned int opb = bfd_octets_per_byte (abfd);
   8925   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8926   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8927   struct bfd_link_order *lo;
   8928   bfd_vma r_sym_mask;
   8929   bfd_boolean use_rela;
   8930 
   8931   /* Find a dynamic reloc section.  */
   8932   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   8933   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   8934   if (rela_dyn != NULL && rela_dyn->size > 0
   8935       && rel_dyn != NULL && rel_dyn->size > 0)
   8936     {
   8937       bfd_boolean use_rela_initialised = FALSE;
   8938 
   8939       /* This is just here to stop gcc from complaining.
   8940 	 Its initialization checking code is not perfect.  */
   8941       use_rela = TRUE;
   8942 
   8943       /* Both sections are present.  Examine the sizes
   8944 	 of the indirect sections to help us choose.  */
   8945       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   8946 	if (lo->type == bfd_indirect_link_order)
   8947 	  {
   8948 	    asection *o = lo->u.indirect.section;
   8949 
   8950 	    if ((o->size % bed->s->sizeof_rela) == 0)
   8951 	      {
   8952 		if ((o->size % bed->s->sizeof_rel) == 0)
   8953 		  /* Section size is divisible by both rel and rela sizes.
   8954 		     It is of no help to us.  */
   8955 		  ;
   8956 		else
   8957 		  {
   8958 		    /* Section size is only divisible by rela.  */
   8959 		    if (use_rela_initialised && !use_rela)
   8960 		      {
   8961 			_bfd_error_handler (_("%B: Unable to sort relocs - "
   8962 					      "they are in more than one size"),
   8963 					    abfd);
   8964 			bfd_set_error (bfd_error_invalid_operation);
   8965 			return 0;
   8966 		      }
   8967 		    else
   8968 		      {
   8969 			use_rela = TRUE;
   8970 			use_rela_initialised = TRUE;
   8971 		      }
   8972 		  }
   8973 	      }
   8974 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   8975 	      {
   8976 		/* Section size is only divisible by rel.  */
   8977 		if (use_rela_initialised && use_rela)
   8978 		  {
   8979 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
   8980 					  "they are in more than one size"),
   8981 					abfd);
   8982 		    bfd_set_error (bfd_error_invalid_operation);
   8983 		    return 0;
   8984 		  }
   8985 		else
   8986 		  {
   8987 		    use_rela = FALSE;
   8988 		    use_rela_initialised = TRUE;
   8989 		  }
   8990 	      }
   8991 	    else
   8992 	      {
   8993 		/* The section size is not divisible by either -
   8994 		   something is wrong.  */
   8995 		_bfd_error_handler (_("%B: Unable to sort relocs - "
   8996 				      "they are of an unknown size"), abfd);
   8997 		bfd_set_error (bfd_error_invalid_operation);
   8998 		return 0;
   8999 	      }
   9000 	  }
   9001 
   9002       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   9003 	if (lo->type == bfd_indirect_link_order)
   9004 	  {
   9005 	    asection *o = lo->u.indirect.section;
   9006 
   9007 	    if ((o->size % bed->s->sizeof_rela) == 0)
   9008 	      {
   9009 		if ((o->size % bed->s->sizeof_rel) == 0)
   9010 		  /* Section size is divisible by both rel and rela sizes.
   9011 		     It is of no help to us.  */
   9012 		  ;
   9013 		else
   9014 		  {
   9015 		    /* Section size is only divisible by rela.  */
   9016 		    if (use_rela_initialised && !use_rela)
   9017 		      {
   9018 			_bfd_error_handler (_("%B: Unable to sort relocs - "
   9019 					      "they are in more than one size"),
   9020 					    abfd);
   9021 			bfd_set_error (bfd_error_invalid_operation);
   9022 			return 0;
   9023 		      }
   9024 		    else
   9025 		      {
   9026 			use_rela = TRUE;
   9027 			use_rela_initialised = TRUE;
   9028 		      }
   9029 		  }
   9030 	      }
   9031 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   9032 	      {
   9033 		/* Section size is only divisible by rel.  */
   9034 		if (use_rela_initialised && use_rela)
   9035 		  {
   9036 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
   9037 					  "they are in more than one size"),
   9038 					abfd);
   9039 		    bfd_set_error (bfd_error_invalid_operation);
   9040 		    return 0;
   9041 		  }
   9042 		else
   9043 		  {
   9044 		    use_rela = FALSE;
   9045 		    use_rela_initialised = TRUE;
   9046 		  }
   9047 	      }
   9048 	    else
   9049 	      {
   9050 		/* The section size is not divisible by either -
   9051 		   something is wrong.  */
   9052 		_bfd_error_handler (_("%B: Unable to sort relocs - "
   9053 				      "they are of an unknown size"), abfd);
   9054 		bfd_set_error (bfd_error_invalid_operation);
   9055 		return 0;
   9056 	      }
   9057 	  }
   9058 
   9059       if (! use_rela_initialised)
   9060 	/* Make a guess.  */
   9061 	use_rela = TRUE;
   9062     }
   9063   else if (rela_dyn != NULL && rela_dyn->size > 0)
   9064     use_rela = TRUE;
   9065   else if (rel_dyn != NULL && rel_dyn->size > 0)
   9066     use_rela = FALSE;
   9067   else
   9068     return 0;
   9069 
   9070   if (use_rela)
   9071     {
   9072       dynamic_relocs = rela_dyn;
   9073       ext_size = bed->s->sizeof_rela;
   9074       swap_in = bed->s->swap_reloca_in;
   9075       swap_out = bed->s->swap_reloca_out;
   9076     }
   9077   else
   9078     {
   9079       dynamic_relocs = rel_dyn;
   9080       ext_size = bed->s->sizeof_rel;
   9081       swap_in = bed->s->swap_reloc_in;
   9082       swap_out = bed->s->swap_reloc_out;
   9083     }
   9084 
   9085   size = 0;
   9086   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9087     if (lo->type == bfd_indirect_link_order)
   9088       size += lo->u.indirect.section->size;
   9089 
   9090   if (size != dynamic_relocs->size)
   9091     return 0;
   9092 
   9093   sort_elt = (sizeof (struct elf_link_sort_rela)
   9094 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   9095 
   9096   count = dynamic_relocs->size / ext_size;
   9097   if (count == 0)
   9098     return 0;
   9099   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   9100 
   9101   if (sort == NULL)
   9102     {
   9103       (*info->callbacks->warning)
   9104 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
   9105       return 0;
   9106     }
   9107 
   9108   if (bed->s->arch_size == 32)
   9109     r_sym_mask = ~(bfd_vma) 0xff;
   9110   else
   9111     r_sym_mask = ~(bfd_vma) 0xffffffff;
   9112 
   9113   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9114     if (lo->type == bfd_indirect_link_order)
   9115       {
   9116 	bfd_byte *erel, *erelend;
   9117 	asection *o = lo->u.indirect.section;
   9118 
   9119 	if (o->contents == NULL && o->size != 0)
   9120 	  {
   9121 	    /* This is a reloc section that is being handled as a normal
   9122 	       section.  See bfd_section_from_shdr.  We can't combine
   9123 	       relocs in this case.  */
   9124 	    free (sort);
   9125 	    return 0;
   9126 	  }
   9127 	erel = o->contents;
   9128 	erelend = o->contents + o->size;
   9129 	p = sort + o->output_offset * opb / ext_size * sort_elt;
   9130 
   9131 	while (erel < erelend)
   9132 	  {
   9133 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9134 
   9135 	    (*swap_in) (abfd, erel, s->rela);
   9136 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   9137 	    s->u.sym_mask = r_sym_mask;
   9138 	    p += sort_elt;
   9139 	    erel += ext_size;
   9140 	  }
   9141       }
   9142 
   9143   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   9144 
   9145   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   9146     {
   9147       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9148       if (s->type != reloc_class_relative)
   9149 	break;
   9150     }
   9151   ret = i;
   9152   s_non_relative = p;
   9153 
   9154   sq = (struct elf_link_sort_rela *) s_non_relative;
   9155   for (; i < count; i++, p += sort_elt)
   9156     {
   9157       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   9158       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   9159 	sq = sp;
   9160       sp->u.offset = sq->rela->r_offset;
   9161     }
   9162 
   9163   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   9164 
   9165   struct elf_link_hash_table *htab = elf_hash_table (info);
   9166   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
   9167     {
   9168       /* We have plt relocs in .rela.dyn.  */
   9169       sq = (struct elf_link_sort_rela *) sort;
   9170       for (i = 0; i < count; i++)
   9171 	if (sq[count - i - 1].type != reloc_class_plt)
   9172 	  break;
   9173       if (i != 0 && htab->srelplt->size == i * ext_size)
   9174 	{
   9175 	  struct bfd_link_order **plo;
   9176 	  /* Put srelplt link_order last.  This is so the output_offset
   9177 	     set in the next loop is correct for DT_JMPREL.  */
   9178 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
   9179 	    if ((*plo)->type == bfd_indirect_link_order
   9180 		&& (*plo)->u.indirect.section == htab->srelplt)
   9181 	      {
   9182 		lo = *plo;
   9183 		*plo = lo->next;
   9184 	      }
   9185 	    else
   9186 	      plo = &(*plo)->next;
   9187 	  *plo = lo;
   9188 	  lo->next = NULL;
   9189 	  dynamic_relocs->map_tail.link_order = lo;
   9190 	}
   9191     }
   9192 
   9193   p = sort;
   9194   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   9195     if (lo->type == bfd_indirect_link_order)
   9196       {
   9197 	bfd_byte *erel, *erelend;
   9198 	asection *o = lo->u.indirect.section;
   9199 
   9200 	erel = o->contents;
   9201 	erelend = o->contents + o->size;
   9202 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
   9203 	while (erel < erelend)
   9204 	  {
   9205 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   9206 	    (*swap_out) (abfd, s->rela, erel);
   9207 	    p += sort_elt;
   9208 	    erel += ext_size;
   9209 	  }
   9210       }
   9211 
   9212   free (sort);
   9213   *psec = dynamic_relocs;
   9214   return ret;
   9215 }
   9216 
   9217 /* Add a symbol to the output symbol string table.  */
   9218 
   9219 static int
   9220 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
   9221 			   const char *name,
   9222 			   Elf_Internal_Sym *elfsym,
   9223 			   asection *input_sec,
   9224 			   struct elf_link_hash_entry *h)
   9225 {
   9226   int (*output_symbol_hook)
   9227     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   9228      struct elf_link_hash_entry *);
   9229   struct elf_link_hash_table *hash_table;
   9230   const struct elf_backend_data *bed;
   9231   bfd_size_type strtabsize;
   9232 
   9233   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   9234 
   9235   bed = get_elf_backend_data (flinfo->output_bfd);
   9236   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   9237   if (output_symbol_hook != NULL)
   9238     {
   9239       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   9240       if (ret != 1)
   9241 	return ret;
   9242     }
   9243 
   9244   if (name == NULL
   9245       || *name == '\0'
   9246       || (input_sec->flags & SEC_EXCLUDE))
   9247     elfsym->st_name = (unsigned long) -1;
   9248   else
   9249     {
   9250       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   9251 	 to get the final offset for st_name.  */
   9252       elfsym->st_name
   9253 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   9254 					       name, FALSE);
   9255       if (elfsym->st_name == (unsigned long) -1)
   9256 	return 0;
   9257     }
   9258 
   9259   hash_table = elf_hash_table (flinfo->info);
   9260   strtabsize = hash_table->strtabsize;
   9261   if (strtabsize <= hash_table->strtabcount)
   9262     {
   9263       strtabsize += strtabsize;
   9264       hash_table->strtabsize = strtabsize;
   9265       strtabsize *= sizeof (*hash_table->strtab);
   9266       hash_table->strtab
   9267 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   9268 						 strtabsize);
   9269       if (hash_table->strtab == NULL)
   9270 	return 0;
   9271     }
   9272   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
   9273   hash_table->strtab[hash_table->strtabcount].dest_index
   9274     = hash_table->strtabcount;
   9275   hash_table->strtab[hash_table->strtabcount].destshndx_index
   9276     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
   9277 
   9278   bfd_get_symcount (flinfo->output_bfd) += 1;
   9279   hash_table->strtabcount += 1;
   9280 
   9281   return 1;
   9282 }
   9283 
   9284 /* Swap symbols out to the symbol table and flush the output symbols to
   9285    the file.  */
   9286 
   9287 static bfd_boolean
   9288 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   9289 {
   9290   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   9291   bfd_size_type amt;
   9292   size_t i;
   9293   const struct elf_backend_data *bed;
   9294   bfd_byte *symbuf;
   9295   Elf_Internal_Shdr *hdr;
   9296   file_ptr pos;
   9297   bfd_boolean ret;
   9298 
   9299   if (!hash_table->strtabcount)
   9300     return TRUE;
   9301 
   9302   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   9303 
   9304   bed = get_elf_backend_data (flinfo->output_bfd);
   9305 
   9306   amt = bed->s->sizeof_sym * hash_table->strtabcount;
   9307   symbuf = (bfd_byte *) bfd_malloc (amt);
   9308   if (symbuf == NULL)
   9309     return FALSE;
   9310 
   9311   if (flinfo->symshndxbuf)
   9312     {
   9313       amt = sizeof (Elf_External_Sym_Shndx);
   9314       amt *= bfd_get_symcount (flinfo->output_bfd);
   9315       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   9316       if (flinfo->symshndxbuf == NULL)
   9317 	{
   9318 	  free (symbuf);
   9319 	  return FALSE;
   9320 	}
   9321     }
   9322 
   9323   for (i = 0; i < hash_table->strtabcount; i++)
   9324     {
   9325       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   9326       if (elfsym->sym.st_name == (unsigned long) -1)
   9327 	elfsym->sym.st_name = 0;
   9328       else
   9329 	elfsym->sym.st_name
   9330 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   9331 						    elfsym->sym.st_name);
   9332       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   9333 			       ((bfd_byte *) symbuf
   9334 				+ (elfsym->dest_index
   9335 				   * bed->s->sizeof_sym)),
   9336 			       (flinfo->symshndxbuf
   9337 				+ elfsym->destshndx_index));
   9338     }
   9339 
   9340   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   9341   pos = hdr->sh_offset + hdr->sh_size;
   9342   amt = hash_table->strtabcount * bed->s->sizeof_sym;
   9343   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   9344       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
   9345     {
   9346       hdr->sh_size += amt;
   9347       ret = TRUE;
   9348     }
   9349   else
   9350     ret = FALSE;
   9351 
   9352   free (symbuf);
   9353 
   9354   free (hash_table->strtab);
   9355   hash_table->strtab = NULL;
   9356 
   9357   return ret;
   9358 }
   9359 
   9360 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   9361 
   9362 static bfd_boolean
   9363 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   9364 {
   9365   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   9366       && sym->st_shndx < SHN_LORESERVE)
   9367     {
   9368       /* The gABI doesn't support dynamic symbols in output sections
   9369 	 beyond 64k.  */
   9370       _bfd_error_handler
   9371 	/* xgettext:c-format */
   9372 	(_("%B: Too many sections: %d (>= %d)"),
   9373 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   9374       bfd_set_error (bfd_error_nonrepresentable_section);
   9375       return FALSE;
   9376     }
   9377   return TRUE;
   9378 }
   9379 
   9380 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   9381    allowing an unsatisfied unversioned symbol in the DSO to match a
   9382    versioned symbol that would normally require an explicit version.
   9383    We also handle the case that a DSO references a hidden symbol
   9384    which may be satisfied by a versioned symbol in another DSO.  */
   9385 
   9386 static bfd_boolean
   9387 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   9388 				 const struct elf_backend_data *bed,
   9389 				 struct elf_link_hash_entry *h)
   9390 {
   9391   bfd *abfd;
   9392   struct elf_link_loaded_list *loaded;
   9393 
   9394   if (!is_elf_hash_table (info->hash))
   9395     return FALSE;
   9396 
   9397   /* Check indirect symbol.  */
   9398   while (h->root.type == bfd_link_hash_indirect)
   9399     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9400 
   9401   switch (h->root.type)
   9402     {
   9403     default:
   9404       abfd = NULL;
   9405       break;
   9406 
   9407     case bfd_link_hash_undefined:
   9408     case bfd_link_hash_undefweak:
   9409       abfd = h->root.u.undef.abfd;
   9410       if (abfd == NULL
   9411 	  || (abfd->flags & DYNAMIC) == 0
   9412 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   9413 	return FALSE;
   9414       break;
   9415 
   9416     case bfd_link_hash_defined:
   9417     case bfd_link_hash_defweak:
   9418       abfd = h->root.u.def.section->owner;
   9419       break;
   9420 
   9421     case bfd_link_hash_common:
   9422       abfd = h->root.u.c.p->section->owner;
   9423       break;
   9424     }
   9425   BFD_ASSERT (abfd != NULL);
   9426 
   9427   for (loaded = elf_hash_table (info)->loaded;
   9428        loaded != NULL;
   9429        loaded = loaded->next)
   9430     {
   9431       bfd *input;
   9432       Elf_Internal_Shdr *hdr;
   9433       size_t symcount;
   9434       size_t extsymcount;
   9435       size_t extsymoff;
   9436       Elf_Internal_Shdr *versymhdr;
   9437       Elf_Internal_Sym *isym;
   9438       Elf_Internal_Sym *isymend;
   9439       Elf_Internal_Sym *isymbuf;
   9440       Elf_External_Versym *ever;
   9441       Elf_External_Versym *extversym;
   9442 
   9443       input = loaded->abfd;
   9444 
   9445       /* We check each DSO for a possible hidden versioned definition.  */
   9446       if (input == abfd
   9447 	  || (input->flags & DYNAMIC) == 0
   9448 	  || elf_dynversym (input) == 0)
   9449 	continue;
   9450 
   9451       hdr = &elf_tdata (input)->dynsymtab_hdr;
   9452 
   9453       symcount = hdr->sh_size / bed->s->sizeof_sym;
   9454       if (elf_bad_symtab (input))
   9455 	{
   9456 	  extsymcount = symcount;
   9457 	  extsymoff = 0;
   9458 	}
   9459       else
   9460 	{
   9461 	  extsymcount = symcount - hdr->sh_info;
   9462 	  extsymoff = hdr->sh_info;
   9463 	}
   9464 
   9465       if (extsymcount == 0)
   9466 	continue;
   9467 
   9468       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   9469 				      NULL, NULL, NULL);
   9470       if (isymbuf == NULL)
   9471 	return FALSE;
   9472 
   9473       /* Read in any version definitions.  */
   9474       versymhdr = &elf_tdata (input)->dynversym_hdr;
   9475       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   9476       if (extversym == NULL)
   9477 	goto error_ret;
   9478 
   9479       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   9480 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
   9481 	      != versymhdr->sh_size))
   9482 	{
   9483 	  free (extversym);
   9484 	error_ret:
   9485 	  free (isymbuf);
   9486 	  return FALSE;
   9487 	}
   9488 
   9489       ever = extversym + extsymoff;
   9490       isymend = isymbuf + extsymcount;
   9491       for (isym = isymbuf; isym < isymend; isym++, ever++)
   9492 	{
   9493 	  const char *name;
   9494 	  Elf_Internal_Versym iver;
   9495 	  unsigned short version_index;
   9496 
   9497 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   9498 	      || isym->st_shndx == SHN_UNDEF)
   9499 	    continue;
   9500 
   9501 	  name = bfd_elf_string_from_elf_section (input,
   9502 						  hdr->sh_link,
   9503 						  isym->st_name);
   9504 	  if (strcmp (name, h->root.root.string) != 0)
   9505 	    continue;
   9506 
   9507 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   9508 
   9509 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   9510 	      && !(h->def_regular
   9511 		   && h->forced_local))
   9512 	    {
   9513 	      /* If we have a non-hidden versioned sym, then it should
   9514 		 have provided a definition for the undefined sym unless
   9515 		 it is defined in a non-shared object and forced local.
   9516 	       */
   9517 	      abort ();
   9518 	    }
   9519 
   9520 	  version_index = iver.vs_vers & VERSYM_VERSION;
   9521 	  if (version_index == 1 || version_index == 2)
   9522 	    {
   9523 	      /* This is the base or first version.  We can use it.  */
   9524 	      free (extversym);
   9525 	      free (isymbuf);
   9526 	      return TRUE;
   9527 	    }
   9528 	}
   9529 
   9530       free (extversym);
   9531       free (isymbuf);
   9532     }
   9533 
   9534   return FALSE;
   9535 }
   9536 
   9537 /* Convert ELF common symbol TYPE.  */
   9538 
   9539 static int
   9540 elf_link_convert_common_type (struct bfd_link_info *info, int type)
   9541 {
   9542   /* Commom symbol can only appear in relocatable link.  */
   9543   if (!bfd_link_relocatable (info))
   9544     abort ();
   9545   switch (info->elf_stt_common)
   9546     {
   9547     case unchanged:
   9548       break;
   9549     case elf_stt_common:
   9550       type = STT_COMMON;
   9551       break;
   9552     case no_elf_stt_common:
   9553       type = STT_OBJECT;
   9554       break;
   9555     }
   9556   return type;
   9557 }
   9558 
   9559 /* Add an external symbol to the symbol table.  This is called from
   9560    the hash table traversal routine.  When generating a shared object,
   9561    we go through the symbol table twice.  The first time we output
   9562    anything that might have been forced to local scope in a version
   9563    script.  The second time we output the symbols that are still
   9564    global symbols.  */
   9565 
   9566 static bfd_boolean
   9567 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   9568 {
   9569   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   9570   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   9571   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   9572   bfd_boolean strip;
   9573   Elf_Internal_Sym sym;
   9574   asection *input_sec;
   9575   const struct elf_backend_data *bed;
   9576   long indx;
   9577   int ret;
   9578   unsigned int type;
   9579 
   9580   if (h->root.type == bfd_link_hash_warning)
   9581     {
   9582       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9583       if (h->root.type == bfd_link_hash_new)
   9584 	return TRUE;
   9585     }
   9586 
   9587   /* Decide whether to output this symbol in this pass.  */
   9588   if (eoinfo->localsyms)
   9589     {
   9590       if (!h->forced_local)
   9591 	return TRUE;
   9592     }
   9593   else
   9594     {
   9595       if (h->forced_local)
   9596 	return TRUE;
   9597     }
   9598 
   9599   bed = get_elf_backend_data (flinfo->output_bfd);
   9600 
   9601   if (h->root.type == bfd_link_hash_undefined)
   9602     {
   9603       /* If we have an undefined symbol reference here then it must have
   9604 	 come from a shared library that is being linked in.  (Undefined
   9605 	 references in regular files have already been handled unless
   9606 	 they are in unreferenced sections which are removed by garbage
   9607 	 collection).  */
   9608       bfd_boolean ignore_undef = FALSE;
   9609 
   9610       /* Some symbols may be special in that the fact that they're
   9611 	 undefined can be safely ignored - let backend determine that.  */
   9612       if (bed->elf_backend_ignore_undef_symbol)
   9613 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   9614 
   9615       /* If we are reporting errors for this situation then do so now.  */
   9616       if (!ignore_undef
   9617 	  && h->ref_dynamic
   9618 	  && (!h->ref_regular || flinfo->info->gc_sections)
   9619 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   9620 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   9621 	(*flinfo->info->callbacks->undefined_symbol)
   9622 	  (flinfo->info, h->root.root.string,
   9623 	   h->ref_regular ? NULL : h->root.u.undef.abfd,
   9624 	   NULL, 0,
   9625 	   flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
   9626 
   9627       /* Strip a global symbol defined in a discarded section.  */
   9628       if (h->indx == -3)
   9629 	return TRUE;
   9630     }
   9631 
   9632   /* We should also warn if a forced local symbol is referenced from
   9633      shared libraries.  */
   9634   if (bfd_link_executable (flinfo->info)
   9635       && h->forced_local
   9636       && h->ref_dynamic
   9637       && h->def_regular
   9638       && !h->dynamic_def
   9639       && h->ref_dynamic_nonweak
   9640       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   9641     {
   9642       bfd *def_bfd;
   9643       const char *msg;
   9644       struct elf_link_hash_entry *hi = h;
   9645 
   9646       /* Check indirect symbol.  */
   9647       while (hi->root.type == bfd_link_hash_indirect)
   9648 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   9649 
   9650       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   9651 	/* xgettext:c-format */
   9652 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
   9653       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   9654 	/* xgettext:c-format */
   9655 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
   9656       else
   9657 	/* xgettext:c-format */
   9658 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
   9659       def_bfd = flinfo->output_bfd;
   9660       if (hi->root.u.def.section != bfd_abs_section_ptr)
   9661 	def_bfd = hi->root.u.def.section->owner;
   9662       _bfd_error_handler (msg, flinfo->output_bfd,
   9663 			  h->root.root.string, def_bfd);
   9664       bfd_set_error (bfd_error_bad_value);
   9665       eoinfo->failed = TRUE;
   9666       return FALSE;
   9667     }
   9668 
   9669   /* We don't want to output symbols that have never been mentioned by
   9670      a regular file, or that we have been told to strip.  However, if
   9671      h->indx is set to -2, the symbol is used by a reloc and we must
   9672      output it.  */
   9673   strip = FALSE;
   9674   if (h->indx == -2)
   9675     ;
   9676   else if ((h->def_dynamic
   9677 	    || h->ref_dynamic
   9678 	    || h->root.type == bfd_link_hash_new)
   9679 	   && !h->def_regular
   9680 	   && !h->ref_regular)
   9681     strip = TRUE;
   9682   else if (flinfo->info->strip == strip_all)
   9683     strip = TRUE;
   9684   else if (flinfo->info->strip == strip_some
   9685 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   9686 			       h->root.root.string, FALSE, FALSE) == NULL)
   9687     strip = TRUE;
   9688   else if ((h->root.type == bfd_link_hash_defined
   9689 	    || h->root.type == bfd_link_hash_defweak)
   9690 	   && ((flinfo->info->strip_discarded
   9691 		&& discarded_section (h->root.u.def.section))
   9692 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   9693 		   && h->root.u.def.section->owner != NULL
   9694 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   9695     strip = TRUE;
   9696   else if ((h->root.type == bfd_link_hash_undefined
   9697 	    || h->root.type == bfd_link_hash_undefweak)
   9698 	   && h->root.u.undef.abfd != NULL
   9699 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   9700     strip = TRUE;
   9701 
   9702   type = h->type;
   9703 
   9704   /* If we're stripping it, and it's not a dynamic symbol, there's
   9705      nothing else to do.   However, if it is a forced local symbol or
   9706      an ifunc symbol we need to give the backend finish_dynamic_symbol
   9707      function a chance to make it dynamic.  */
   9708   if (strip
   9709       && h->dynindx == -1
   9710       && type != STT_GNU_IFUNC
   9711       && !h->forced_local)
   9712     return TRUE;
   9713 
   9714   sym.st_value = 0;
   9715   sym.st_size = h->size;
   9716   sym.st_other = h->other;
   9717   switch (h->root.type)
   9718     {
   9719     default:
   9720     case bfd_link_hash_new:
   9721     case bfd_link_hash_warning:
   9722       abort ();
   9723       return FALSE;
   9724 
   9725     case bfd_link_hash_undefined:
   9726     case bfd_link_hash_undefweak:
   9727       input_sec = bfd_und_section_ptr;
   9728       sym.st_shndx = SHN_UNDEF;
   9729       break;
   9730 
   9731     case bfd_link_hash_defined:
   9732     case bfd_link_hash_defweak:
   9733       {
   9734 	input_sec = h->root.u.def.section;
   9735 	if (input_sec->output_section != NULL)
   9736 	  {
   9737 	    sym.st_shndx =
   9738 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   9739 						 input_sec->output_section);
   9740 	    if (sym.st_shndx == SHN_BAD)
   9741 	      {
   9742 		_bfd_error_handler
   9743 		  /* xgettext:c-format */
   9744 		  (_("%B: could not find output section %A for input section %A"),
   9745 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   9746 		bfd_set_error (bfd_error_nonrepresentable_section);
   9747 		eoinfo->failed = TRUE;
   9748 		return FALSE;
   9749 	      }
   9750 
   9751 	    /* ELF symbols in relocatable files are section relative,
   9752 	       but in nonrelocatable files they are virtual
   9753 	       addresses.  */
   9754 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   9755 	    if (!bfd_link_relocatable (flinfo->info))
   9756 	      {
   9757 		sym.st_value += input_sec->output_section->vma;
   9758 		if (h->type == STT_TLS)
   9759 		  {
   9760 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   9761 		    if (tls_sec != NULL)
   9762 		      sym.st_value -= tls_sec->vma;
   9763 		  }
   9764 	      }
   9765 	  }
   9766 	else
   9767 	  {
   9768 	    BFD_ASSERT (input_sec->owner == NULL
   9769 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   9770 	    sym.st_shndx = SHN_UNDEF;
   9771 	    input_sec = bfd_und_section_ptr;
   9772 	  }
   9773       }
   9774       break;
   9775 
   9776     case bfd_link_hash_common:
   9777       input_sec = h->root.u.c.p->section;
   9778       sym.st_shndx = bed->common_section_index (input_sec);
   9779       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   9780       break;
   9781 
   9782     case bfd_link_hash_indirect:
   9783       /* These symbols are created by symbol versioning.  They point
   9784 	 to the decorated version of the name.  For example, if the
   9785 	 symbol foo@@GNU_1.2 is the default, which should be used when
   9786 	 foo is used with no version, then we add an indirect symbol
   9787 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   9788 	 since the indirected symbol is already in the hash table.  */
   9789       return TRUE;
   9790     }
   9791 
   9792   if (type == STT_COMMON || type == STT_OBJECT)
   9793     switch (h->root.type)
   9794       {
   9795       case bfd_link_hash_common:
   9796 	type = elf_link_convert_common_type (flinfo->info, type);
   9797 	break;
   9798       case bfd_link_hash_defined:
   9799       case bfd_link_hash_defweak:
   9800 	if (bed->common_definition (&sym))
   9801 	  type = elf_link_convert_common_type (flinfo->info, type);
   9802 	else
   9803 	  type = STT_OBJECT;
   9804 	break;
   9805       case bfd_link_hash_undefined:
   9806       case bfd_link_hash_undefweak:
   9807 	break;
   9808       default:
   9809 	abort ();
   9810       }
   9811 
   9812   if (h->forced_local)
   9813     {
   9814       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
   9815       /* Turn off visibility on local symbol.  */
   9816       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   9817     }
   9818   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   9819   else if (h->unique_global && h->def_regular)
   9820     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
   9821   else if (h->root.type == bfd_link_hash_undefweak
   9822 	   || h->root.type == bfd_link_hash_defweak)
   9823     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
   9824   else
   9825     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   9826   sym.st_target_internal = h->target_internal;
   9827 
   9828   /* Give the processor backend a chance to tweak the symbol value,
   9829      and also to finish up anything that needs to be done for this
   9830      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   9831      forced local syms when non-shared is due to a historical quirk.
   9832      STT_GNU_IFUNC symbol must go through PLT.  */
   9833   if ((h->type == STT_GNU_IFUNC
   9834        && h->def_regular
   9835        && !bfd_link_relocatable (flinfo->info))
   9836       || ((h->dynindx != -1
   9837 	   || h->forced_local)
   9838 	  && ((bfd_link_pic (flinfo->info)
   9839 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   9840 		   || h->root.type != bfd_link_hash_undefweak))
   9841 	      || !h->forced_local)
   9842 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   9843     {
   9844       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   9845 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   9846 	{
   9847 	  eoinfo->failed = TRUE;
   9848 	  return FALSE;
   9849 	}
   9850     }
   9851 
   9852   /* If we are marking the symbol as undefined, and there are no
   9853      non-weak references to this symbol from a regular object, then
   9854      mark the symbol as weak undefined; if there are non-weak
   9855      references, mark the symbol as strong.  We can't do this earlier,
   9856      because it might not be marked as undefined until the
   9857      finish_dynamic_symbol routine gets through with it.  */
   9858   if (sym.st_shndx == SHN_UNDEF
   9859       && h->ref_regular
   9860       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   9861 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   9862     {
   9863       int bindtype;
   9864       type = ELF_ST_TYPE (sym.st_info);
   9865 
   9866       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   9867       if (type == STT_GNU_IFUNC)
   9868 	type = STT_FUNC;
   9869 
   9870       if (h->ref_regular_nonweak)
   9871 	bindtype = STB_GLOBAL;
   9872       else
   9873 	bindtype = STB_WEAK;
   9874       sym.st_info = ELF_ST_INFO (bindtype, type);
   9875     }
   9876 
   9877   /* If this is a symbol defined in a dynamic library, don't use the
   9878      symbol size from the dynamic library.  Relinking an executable
   9879      against a new library may introduce gratuitous changes in the
   9880      executable's symbols if we keep the size.  */
   9881   if (sym.st_shndx == SHN_UNDEF
   9882       && !h->def_regular
   9883       && h->def_dynamic)
   9884     sym.st_size = 0;
   9885 
   9886   /* If a non-weak symbol with non-default visibility is not defined
   9887      locally, it is a fatal error.  */
   9888   if (!bfd_link_relocatable (flinfo->info)
   9889       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   9890       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   9891       && h->root.type == bfd_link_hash_undefined
   9892       && !h->def_regular)
   9893     {
   9894       const char *msg;
   9895 
   9896       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   9897 	/* xgettext:c-format */
   9898 	msg = _("%B: protected symbol `%s' isn't defined");
   9899       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   9900 	/* xgettext:c-format */
   9901 	msg = _("%B: internal symbol `%s' isn't defined");
   9902       else
   9903 	/* xgettext:c-format */
   9904 	msg = _("%B: hidden symbol `%s' isn't defined");
   9905       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
   9906       bfd_set_error (bfd_error_bad_value);
   9907       eoinfo->failed = TRUE;
   9908       return FALSE;
   9909     }
   9910 
   9911   /* If this symbol should be put in the .dynsym section, then put it
   9912      there now.  We already know the symbol index.  We also fill in
   9913      the entry in the .hash section.  */
   9914   if (elf_hash_table (flinfo->info)->dynsym != NULL
   9915       && h->dynindx != -1
   9916       && elf_hash_table (flinfo->info)->dynamic_sections_created)
   9917     {
   9918       bfd_byte *esym;
   9919 
   9920       /* Since there is no version information in the dynamic string,
   9921 	 if there is no version info in symbol version section, we will
   9922 	 have a run-time problem if not linking executable, referenced
   9923 	 by shared library, or not bound locally.  */
   9924       if (h->verinfo.verdef == NULL
   9925 	  && (!bfd_link_executable (flinfo->info)
   9926 	      || h->ref_dynamic
   9927 	      || !h->def_regular))
   9928 	{
   9929 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   9930 
   9931 	  if (p && p [1] != '\0')
   9932 	    {
   9933 	      _bfd_error_handler
   9934 		/* xgettext:c-format */
   9935 		(_("%B: No symbol version section for versioned symbol `%s'"),
   9936 		 flinfo->output_bfd, h->root.root.string);
   9937 	      eoinfo->failed = TRUE;
   9938 	      return FALSE;
   9939 	    }
   9940 	}
   9941 
   9942       sym.st_name = h->dynstr_index;
   9943       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   9944 	      + h->dynindx * bed->s->sizeof_sym);
   9945       if (!check_dynsym (flinfo->output_bfd, &sym))
   9946 	{
   9947 	  eoinfo->failed = TRUE;
   9948 	  return FALSE;
   9949 	}
   9950       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   9951 
   9952       if (flinfo->hash_sec != NULL)
   9953 	{
   9954 	  size_t hash_entry_size;
   9955 	  bfd_byte *bucketpos;
   9956 	  bfd_vma chain;
   9957 	  size_t bucketcount;
   9958 	  size_t bucket;
   9959 
   9960 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   9961 	  bucket = h->u.elf_hash_value % bucketcount;
   9962 
   9963 	  hash_entry_size
   9964 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   9965 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   9966 		       + (bucket + 2) * hash_entry_size);
   9967 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   9968 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   9969 		   bucketpos);
   9970 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   9971 		   ((bfd_byte *) flinfo->hash_sec->contents
   9972 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   9973 	}
   9974 
   9975       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   9976 	{
   9977 	  Elf_Internal_Versym iversym;
   9978 	  Elf_External_Versym *eversym;
   9979 
   9980 	  if (!h->def_regular)
   9981 	    {
   9982 	      if (h->verinfo.verdef == NULL
   9983 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   9984 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   9985 		iversym.vs_vers = 0;
   9986 	      else
   9987 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   9988 	    }
   9989 	  else
   9990 	    {
   9991 	      if (h->verinfo.vertree == NULL)
   9992 		iversym.vs_vers = 1;
   9993 	      else
   9994 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   9995 	      if (flinfo->info->create_default_symver)
   9996 		iversym.vs_vers++;
   9997 	    }
   9998 
   9999 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
   10000 	     defined locally.  */
   10001 	  if (h->versioned == versioned_hidden && h->def_regular)
   10002 	    iversym.vs_vers |= VERSYM_HIDDEN;
   10003 
   10004 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   10005 	  eversym += h->dynindx;
   10006 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   10007 	}
   10008     }
   10009 
   10010   /* If the symbol is undefined, and we didn't output it to .dynsym,
   10011      strip it from .symtab too.  Obviously we can't do this for
   10012      relocatable output or when needed for --emit-relocs.  */
   10013   else if (input_sec == bfd_und_section_ptr
   10014 	   && h->indx != -2
   10015 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
   10016 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
   10017 	   && !bfd_link_relocatable (flinfo->info))
   10018     return TRUE;
   10019 
   10020   /* Also strip others that we couldn't earlier due to dynamic symbol
   10021      processing.  */
   10022   if (strip)
   10023     return TRUE;
   10024   if ((input_sec->flags & SEC_EXCLUDE) != 0)
   10025     return TRUE;
   10026 
   10027   /* Output a FILE symbol so that following locals are not associated
   10028      with the wrong input file.  We need one for forced local symbols
   10029      if we've seen more than one FILE symbol or when we have exactly
   10030      one FILE symbol but global symbols are present in a file other
   10031      than the one with the FILE symbol.  We also need one if linker
   10032      defined symbols are present.  In practice these conditions are
   10033      always met, so just emit the FILE symbol unconditionally.  */
   10034   if (eoinfo->localsyms
   10035       && !eoinfo->file_sym_done
   10036       && eoinfo->flinfo->filesym_count != 0)
   10037     {
   10038       Elf_Internal_Sym fsym;
   10039 
   10040       memset (&fsym, 0, sizeof (fsym));
   10041       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   10042       fsym.st_shndx = SHN_ABS;
   10043       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   10044 				      bfd_und_section_ptr, NULL))
   10045 	return FALSE;
   10046 
   10047       eoinfo->file_sym_done = TRUE;
   10048     }
   10049 
   10050   indx = bfd_get_symcount (flinfo->output_bfd);
   10051   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   10052 				   input_sec, h);
   10053   if (ret == 0)
   10054     {
   10055       eoinfo->failed = TRUE;
   10056       return FALSE;
   10057     }
   10058   else if (ret == 1)
   10059     h->indx = indx;
   10060   else if (h->indx == -2)
   10061     abort();
   10062 
   10063   return TRUE;
   10064 }
   10065 
   10066 /* Return TRUE if special handling is done for relocs in SEC against
   10067    symbols defined in discarded sections.  */
   10068 
   10069 static bfd_boolean
   10070 elf_section_ignore_discarded_relocs (asection *sec)
   10071 {
   10072   const struct elf_backend_data *bed;
   10073 
   10074   switch (sec->sec_info_type)
   10075     {
   10076     case SEC_INFO_TYPE_STABS:
   10077     case SEC_INFO_TYPE_EH_FRAME:
   10078     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   10079       return TRUE;
   10080     default:
   10081       break;
   10082     }
   10083 
   10084   bed = get_elf_backend_data (sec->owner);
   10085   if (bed->elf_backend_ignore_discarded_relocs != NULL
   10086       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   10087     return TRUE;
   10088 
   10089   return FALSE;
   10090 }
   10091 
   10092 /* Return a mask saying how ld should treat relocations in SEC against
   10093    symbols defined in discarded sections.  If this function returns
   10094    COMPLAIN set, ld will issue a warning message.  If this function
   10095    returns PRETEND set, and the discarded section was link-once and the
   10096    same size as the kept link-once section, ld will pretend that the
   10097    symbol was actually defined in the kept section.  Otherwise ld will
   10098    zero the reloc (at least that is the intent, but some cooperation by
   10099    the target dependent code is needed, particularly for REL targets).  */
   10100 
   10101 unsigned int
   10102 _bfd_elf_default_action_discarded (asection *sec)
   10103 {
   10104   if (sec->flags & SEC_DEBUGGING)
   10105     return PRETEND;
   10106 
   10107   if (strcmp (".eh_frame", sec->name) == 0)
   10108     return 0;
   10109 
   10110   if (strcmp (".gcc_except_table", sec->name) == 0)
   10111     return 0;
   10112 
   10113   return COMPLAIN | PRETEND;
   10114 }
   10115 
   10116 /* Find a match between a section and a member of a section group.  */
   10117 
   10118 static asection *
   10119 match_group_member (asection *sec, asection *group,
   10120 		    struct bfd_link_info *info)
   10121 {
   10122   asection *first = elf_next_in_group (group);
   10123   asection *s = first;
   10124 
   10125   while (s != NULL)
   10126     {
   10127       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   10128 	return s;
   10129 
   10130       s = elf_next_in_group (s);
   10131       if (s == first)
   10132 	break;
   10133     }
   10134 
   10135   return NULL;
   10136 }
   10137 
   10138 /* Check if the kept section of a discarded section SEC can be used
   10139    to replace it.  Return the replacement if it is OK.  Otherwise return
   10140    NULL.  */
   10141 
   10142 asection *
   10143 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   10144 {
   10145   asection *kept;
   10146 
   10147   kept = sec->kept_section;
   10148   if (kept != NULL)
   10149     {
   10150       if ((kept->flags & SEC_GROUP) != 0)
   10151 	kept = match_group_member (sec, kept, info);
   10152       if (kept != NULL
   10153 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   10154 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
   10155 	kept = NULL;
   10156       sec->kept_section = kept;
   10157     }
   10158   return kept;
   10159 }
   10160 
   10161 /* Link an input file into the linker output file.  This function
   10162    handles all the sections and relocations of the input file at once.
   10163    This is so that we only have to read the local symbols once, and
   10164    don't have to keep them in memory.  */
   10165 
   10166 static bfd_boolean
   10167 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   10168 {
   10169   int (*relocate_section)
   10170     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   10171      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   10172   bfd *output_bfd;
   10173   Elf_Internal_Shdr *symtab_hdr;
   10174   size_t locsymcount;
   10175   size_t extsymoff;
   10176   Elf_Internal_Sym *isymbuf;
   10177   Elf_Internal_Sym *isym;
   10178   Elf_Internal_Sym *isymend;
   10179   long *pindex;
   10180   asection **ppsection;
   10181   asection *o;
   10182   const struct elf_backend_data *bed;
   10183   struct elf_link_hash_entry **sym_hashes;
   10184   bfd_size_type address_size;
   10185   bfd_vma r_type_mask;
   10186   int r_sym_shift;
   10187   bfd_boolean have_file_sym = FALSE;
   10188 
   10189   output_bfd = flinfo->output_bfd;
   10190   bed = get_elf_backend_data (output_bfd);
   10191   relocate_section = bed->elf_backend_relocate_section;
   10192 
   10193   /* If this is a dynamic object, we don't want to do anything here:
   10194      we don't want the local symbols, and we don't want the section
   10195      contents.  */
   10196   if ((input_bfd->flags & DYNAMIC) != 0)
   10197     return TRUE;
   10198 
   10199   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10200   if (elf_bad_symtab (input_bfd))
   10201     {
   10202       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   10203       extsymoff = 0;
   10204     }
   10205   else
   10206     {
   10207       locsymcount = symtab_hdr->sh_info;
   10208       extsymoff = symtab_hdr->sh_info;
   10209     }
   10210 
   10211   /* Read the local symbols.  */
   10212   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   10213   if (isymbuf == NULL && locsymcount != 0)
   10214     {
   10215       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   10216 				      flinfo->internal_syms,
   10217 				      flinfo->external_syms,
   10218 				      flinfo->locsym_shndx);
   10219       if (isymbuf == NULL)
   10220 	return FALSE;
   10221     }
   10222 
   10223   /* Find local symbol sections and adjust values of symbols in
   10224      SEC_MERGE sections.  Write out those local symbols we know are
   10225      going into the output file.  */
   10226   isymend = isymbuf + locsymcount;
   10227   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   10228        isym < isymend;
   10229        isym++, pindex++, ppsection++)
   10230     {
   10231       asection *isec;
   10232       const char *name;
   10233       Elf_Internal_Sym osym;
   10234       long indx;
   10235       int ret;
   10236 
   10237       *pindex = -1;
   10238 
   10239       if (elf_bad_symtab (input_bfd))
   10240 	{
   10241 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   10242 	    {
   10243 	      *ppsection = NULL;
   10244 	      continue;
   10245 	    }
   10246 	}
   10247 
   10248       if (isym->st_shndx == SHN_UNDEF)
   10249 	isec = bfd_und_section_ptr;
   10250       else if (isym->st_shndx == SHN_ABS)
   10251 	isec = bfd_abs_section_ptr;
   10252       else if (isym->st_shndx == SHN_COMMON)
   10253 	isec = bfd_com_section_ptr;
   10254       else
   10255 	{
   10256 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   10257 	  if (isec == NULL)
   10258 	    {
   10259 	      /* Don't attempt to output symbols with st_shnx in the
   10260 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   10261 	      *ppsection = NULL;
   10262 	      continue;
   10263 	    }
   10264 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   10265 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   10266 	    isym->st_value =
   10267 	      _bfd_merged_section_offset (output_bfd, &isec,
   10268 					  elf_section_data (isec)->sec_info,
   10269 					  isym->st_value);
   10270 	}
   10271 
   10272       *ppsection = isec;
   10273 
   10274       /* Don't output the first, undefined, symbol.  In fact, don't
   10275 	 output any undefined local symbol.  */
   10276       if (isec == bfd_und_section_ptr)
   10277 	continue;
   10278 
   10279       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   10280 	{
   10281 	  /* We never output section symbols.  Instead, we use the
   10282 	     section symbol of the corresponding section in the output
   10283 	     file.  */
   10284 	  continue;
   10285 	}
   10286 
   10287       /* If we are stripping all symbols, we don't want to output this
   10288 	 one.  */
   10289       if (flinfo->info->strip == strip_all)
   10290 	continue;
   10291 
   10292       /* If we are discarding all local symbols, we don't want to
   10293 	 output this one.  If we are generating a relocatable output
   10294 	 file, then some of the local symbols may be required by
   10295 	 relocs; we output them below as we discover that they are
   10296 	 needed.  */
   10297       if (flinfo->info->discard == discard_all)
   10298 	continue;
   10299 
   10300       /* If this symbol is defined in a section which we are
   10301 	 discarding, we don't need to keep it.  */
   10302       if (isym->st_shndx != SHN_UNDEF
   10303 	  && isym->st_shndx < SHN_LORESERVE
   10304 	  && bfd_section_removed_from_list (output_bfd,
   10305 					    isec->output_section))
   10306 	continue;
   10307 
   10308       /* Get the name of the symbol.  */
   10309       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   10310 					      isym->st_name);
   10311       if (name == NULL)
   10312 	return FALSE;
   10313 
   10314       /* See if we are discarding symbols with this name.  */
   10315       if ((flinfo->info->strip == strip_some
   10316 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
   10317 	       == NULL))
   10318 	  || (((flinfo->info->discard == discard_sec_merge
   10319 		&& (isec->flags & SEC_MERGE)
   10320 		&& !bfd_link_relocatable (flinfo->info))
   10321 	       || flinfo->info->discard == discard_l)
   10322 	      && bfd_is_local_label_name (input_bfd, name)))
   10323 	continue;
   10324 
   10325       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   10326 	{
   10327 	  if (input_bfd->lto_output)
   10328 	    /* -flto puts a temp file name here.  This means builds
   10329 	       are not reproducible.  Discard the symbol.  */
   10330 	    continue;
   10331 	  have_file_sym = TRUE;
   10332 	  flinfo->filesym_count += 1;
   10333 	}
   10334       if (!have_file_sym)
   10335 	{
   10336 	  /* In the absence of debug info, bfd_find_nearest_line uses
   10337 	     FILE symbols to determine the source file for local
   10338 	     function symbols.  Provide a FILE symbol here if input
   10339 	     files lack such, so that their symbols won't be
   10340 	     associated with a previous input file.  It's not the
   10341 	     source file, but the best we can do.  */
   10342 	  have_file_sym = TRUE;
   10343 	  flinfo->filesym_count += 1;
   10344 	  memset (&osym, 0, sizeof (osym));
   10345 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   10346 	  osym.st_shndx = SHN_ABS;
   10347 	  if (!elf_link_output_symstrtab (flinfo,
   10348 					  (input_bfd->lto_output ? NULL
   10349 					   : input_bfd->filename),
   10350 					  &osym, bfd_abs_section_ptr,
   10351 					  NULL))
   10352 	    return FALSE;
   10353 	}
   10354 
   10355       osym = *isym;
   10356 
   10357       /* Adjust the section index for the output file.  */
   10358       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   10359 							 isec->output_section);
   10360       if (osym.st_shndx == SHN_BAD)
   10361 	return FALSE;
   10362 
   10363       /* ELF symbols in relocatable files are section relative, but
   10364 	 in executable files they are virtual addresses.  Note that
   10365 	 this code assumes that all ELF sections have an associated
   10366 	 BFD section with a reasonable value for output_offset; below
   10367 	 we assume that they also have a reasonable value for
   10368 	 output_section.  Any special sections must be set up to meet
   10369 	 these requirements.  */
   10370       osym.st_value += isec->output_offset;
   10371       if (!bfd_link_relocatable (flinfo->info))
   10372 	{
   10373 	  osym.st_value += isec->output_section->vma;
   10374 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   10375 	    {
   10376 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   10377 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
   10378 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   10379 	    }
   10380 	}
   10381 
   10382       indx = bfd_get_symcount (output_bfd);
   10383       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   10384       if (ret == 0)
   10385 	return FALSE;
   10386       else if (ret == 1)
   10387 	*pindex = indx;
   10388     }
   10389 
   10390   if (bed->s->arch_size == 32)
   10391     {
   10392       r_type_mask = 0xff;
   10393       r_sym_shift = 8;
   10394       address_size = 4;
   10395     }
   10396   else
   10397     {
   10398       r_type_mask = 0xffffffff;
   10399       r_sym_shift = 32;
   10400       address_size = 8;
   10401     }
   10402 
   10403   /* Relocate the contents of each section.  */
   10404   sym_hashes = elf_sym_hashes (input_bfd);
   10405   for (o = input_bfd->sections; o != NULL; o = o->next)
   10406     {
   10407       bfd_byte *contents;
   10408 
   10409       if (! o->linker_mark)
   10410 	{
   10411 	  /* This section was omitted from the link.  */
   10412 	  continue;
   10413 	}
   10414 
   10415       if (!flinfo->info->resolve_section_groups
   10416 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   10417 	{
   10418 	  /* Deal with the group signature symbol.  */
   10419 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   10420 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   10421 	  asection *osec = o->output_section;
   10422 
   10423 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
   10424 	  if (symndx >= locsymcount
   10425 	      || (elf_bad_symtab (input_bfd)
   10426 		  && flinfo->sections[symndx] == NULL))
   10427 	    {
   10428 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
   10429 	      while (h->root.type == bfd_link_hash_indirect
   10430 		     || h->root.type == bfd_link_hash_warning)
   10431 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10432 	      /* Arrange for symbol to be output.  */
   10433 	      h->indx = -2;
   10434 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   10435 	    }
   10436 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   10437 	    {
   10438 	      /* We'll use the output section target_index.  */
   10439 	      asection *sec = flinfo->sections[symndx]->output_section;
   10440 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   10441 	    }
   10442 	  else
   10443 	    {
   10444 	      if (flinfo->indices[symndx] == -1)
   10445 		{
   10446 		  /* Otherwise output the local symbol now.  */
   10447 		  Elf_Internal_Sym sym = isymbuf[symndx];
   10448 		  asection *sec = flinfo->sections[symndx]->output_section;
   10449 		  const char *name;
   10450 		  long indx;
   10451 		  int ret;
   10452 
   10453 		  name = bfd_elf_string_from_elf_section (input_bfd,
   10454 							  symtab_hdr->sh_link,
   10455 							  sym.st_name);
   10456 		  if (name == NULL)
   10457 		    return FALSE;
   10458 
   10459 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   10460 								    sec);
   10461 		  if (sym.st_shndx == SHN_BAD)
   10462 		    return FALSE;
   10463 
   10464 		  sym.st_value += o->output_offset;
   10465 
   10466 		  indx = bfd_get_symcount (output_bfd);
   10467 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   10468 						   NULL);
   10469 		  if (ret == 0)
   10470 		    return FALSE;
   10471 		  else if (ret == 1)
   10472 		    flinfo->indices[symndx] = indx;
   10473 		  else
   10474 		    abort ();
   10475 		}
   10476 	      elf_section_data (osec)->this_hdr.sh_info
   10477 		= flinfo->indices[symndx];
   10478 	    }
   10479 	}
   10480 
   10481       if ((o->flags & SEC_HAS_CONTENTS) == 0
   10482 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   10483 	continue;
   10484 
   10485       if ((o->flags & SEC_LINKER_CREATED) != 0)
   10486 	{
   10487 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   10488 	     or somesuch.  */
   10489 	  continue;
   10490 	}
   10491 
   10492       /* Get the contents of the section.  They have been cached by a
   10493 	 relaxation routine.  Note that o is a section in an input
   10494 	 file, so the contents field will not have been set by any of
   10495 	 the routines which work on output files.  */
   10496       if (elf_section_data (o)->this_hdr.contents != NULL)
   10497 	{
   10498 	  contents = elf_section_data (o)->this_hdr.contents;
   10499 	  if (bed->caches_rawsize
   10500 	      && o->rawsize != 0
   10501 	      && o->rawsize < o->size)
   10502 	    {
   10503 	      memcpy (flinfo->contents, contents, o->rawsize);
   10504 	      contents = flinfo->contents;
   10505 	    }
   10506 	}
   10507       else
   10508 	{
   10509 	  contents = flinfo->contents;
   10510 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   10511 	    return FALSE;
   10512 	}
   10513 
   10514       if ((o->flags & SEC_RELOC) != 0)
   10515 	{
   10516 	  Elf_Internal_Rela *internal_relocs;
   10517 	  Elf_Internal_Rela *rel, *relend;
   10518 	  int action_discarded;
   10519 	  int ret;
   10520 
   10521 	  /* Get the swapped relocs.  */
   10522 	  internal_relocs
   10523 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
   10524 					 flinfo->internal_relocs, FALSE);
   10525 	  if (internal_relocs == NULL
   10526 	      && o->reloc_count > 0)
   10527 	    return FALSE;
   10528 
   10529 	  /* We need to reverse-copy input .ctors/.dtors sections if
   10530 	     they are placed in .init_array/.finit_array for output.  */
   10531 	  if (o->size > address_size
   10532 	      && ((strncmp (o->name, ".ctors", 6) == 0
   10533 		   && strcmp (o->output_section->name,
   10534 			      ".init_array") == 0)
   10535 		  || (strncmp (o->name, ".dtors", 6) == 0
   10536 		      && strcmp (o->output_section->name,
   10537 				 ".fini_array") == 0))
   10538 	      && (o->name[6] == 0 || o->name[6] == '.'))
   10539 	    {
   10540 	      if (o->size * bed->s->int_rels_per_ext_rel
   10541 		  != o->reloc_count * address_size)
   10542 		{
   10543 		  _bfd_error_handler
   10544 		    /* xgettext:c-format */
   10545 		    (_("error: %B: size of section %A is not "
   10546 		       "multiple of address size"),
   10547 		     input_bfd, o);
   10548 		  bfd_set_error (bfd_error_bad_value);
   10549 		  return FALSE;
   10550 		}
   10551 	      o->flags |= SEC_ELF_REVERSE_COPY;
   10552 	    }
   10553 
   10554 	  action_discarded = -1;
   10555 	  if (!elf_section_ignore_discarded_relocs (o))
   10556 	    action_discarded = (*bed->action_discarded) (o);
   10557 
   10558 	  /* Run through the relocs evaluating complex reloc symbols and
   10559 	     looking for relocs against symbols from discarded sections
   10560 	     or section symbols from removed link-once sections.
   10561 	     Complain about relocs against discarded sections.  Zero
   10562 	     relocs against removed link-once sections.  */
   10563 
   10564 	  rel = internal_relocs;
   10565 	  relend = rel + o->reloc_count;
   10566 	  for ( ; rel < relend; rel++)
   10567 	    {
   10568 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   10569 	      unsigned int s_type;
   10570 	      asection **ps, *sec;
   10571 	      struct elf_link_hash_entry *h = NULL;
   10572 	      const char *sym_name;
   10573 
   10574 	      if (r_symndx == STN_UNDEF)
   10575 		continue;
   10576 
   10577 	      if (r_symndx >= locsymcount
   10578 		  || (elf_bad_symtab (input_bfd)
   10579 		      && flinfo->sections[r_symndx] == NULL))
   10580 		{
   10581 		  h = sym_hashes[r_symndx - extsymoff];
   10582 
   10583 		  /* Badly formatted input files can contain relocs that
   10584 		     reference non-existant symbols.  Check here so that
   10585 		     we do not seg fault.  */
   10586 		  if (h == NULL)
   10587 		    {
   10588 		      _bfd_error_handler
   10589 			/* xgettext:c-format */
   10590 			(_("error: %B contains a reloc (%#Lx) for section %A "
   10591 			   "that references a non-existent global symbol"),
   10592 			 input_bfd, rel->r_info, o);
   10593 		      bfd_set_error (bfd_error_bad_value);
   10594 		      return FALSE;
   10595 		    }
   10596 
   10597 		  while (h->root.type == bfd_link_hash_indirect
   10598 			 || h->root.type == bfd_link_hash_warning)
   10599 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10600 
   10601 		  s_type = h->type;
   10602 
   10603 		  /* If a plugin symbol is referenced from a non-IR file,
   10604 		     mark the symbol as undefined.  Note that the
   10605 		     linker may attach linker created dynamic sections
   10606 		     to the plugin bfd.  Symbols defined in linker
   10607 		     created sections are not plugin symbols.  */
   10608 		  if ((h->root.non_ir_ref_regular
   10609 		       || h->root.non_ir_ref_dynamic)
   10610 		      && (h->root.type == bfd_link_hash_defined
   10611 			  || h->root.type == bfd_link_hash_defweak)
   10612 		      && (h->root.u.def.section->flags
   10613 			  & SEC_LINKER_CREATED) == 0
   10614 		      && h->root.u.def.section->owner != NULL
   10615 		      && (h->root.u.def.section->owner->flags
   10616 			  & BFD_PLUGIN) != 0)
   10617 		    {
   10618 		      h->root.type = bfd_link_hash_undefined;
   10619 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   10620 		    }
   10621 
   10622 		  ps = NULL;
   10623 		  if (h->root.type == bfd_link_hash_defined
   10624 		      || h->root.type == bfd_link_hash_defweak)
   10625 		    ps = &h->root.u.def.section;
   10626 
   10627 		  sym_name = h->root.root.string;
   10628 		}
   10629 	      else
   10630 		{
   10631 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   10632 
   10633 		  s_type = ELF_ST_TYPE (sym->st_info);
   10634 		  ps = &flinfo->sections[r_symndx];
   10635 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10636 					       sym, *ps);
   10637 		}
   10638 
   10639 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   10640 		  && !bfd_link_relocatable (flinfo->info))
   10641 		{
   10642 		  bfd_vma val;
   10643 		  bfd_vma dot = (rel->r_offset
   10644 				 + o->output_offset + o->output_section->vma);
   10645 #ifdef DEBUG
   10646 		  printf ("Encountered a complex symbol!");
   10647 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   10648 			  input_bfd->filename, o->name,
   10649 			  (long) (rel - internal_relocs));
   10650 		  printf (" symbol: idx  %8.8lx, name %s\n",
   10651 			  r_symndx, sym_name);
   10652 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   10653 			  (unsigned long) rel->r_info,
   10654 			  (unsigned long) rel->r_offset);
   10655 #endif
   10656 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   10657 				    isymbuf, locsymcount, s_type == STT_SRELC))
   10658 		    return FALSE;
   10659 
   10660 		  /* Symbol evaluated OK.  Update to absolute value.  */
   10661 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   10662 				    r_symndx, val);
   10663 		  continue;
   10664 		}
   10665 
   10666 	      if (action_discarded != -1 && ps != NULL)
   10667 		{
   10668 		  /* Complain if the definition comes from a
   10669 		     discarded section.  */
   10670 		  if ((sec = *ps) != NULL && discarded_section (sec))
   10671 		    {
   10672 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   10673 		      if (action_discarded & COMPLAIN)
   10674 			(*flinfo->info->callbacks->einfo)
   10675 			  /* xgettext:c-format */
   10676 			  (_("%X`%s' referenced in section `%A' of %B: "
   10677 			     "defined in discarded section `%A' of %B\n"),
   10678 			   sym_name, o, input_bfd, sec, sec->owner);
   10679 
   10680 		      /* Try to do the best we can to support buggy old
   10681 			 versions of gcc.  Pretend that the symbol is
   10682 			 really defined in the kept linkonce section.
   10683 			 FIXME: This is quite broken.  Modifying the
   10684 			 symbol here means we will be changing all later
   10685 			 uses of the symbol, not just in this section.  */
   10686 		      if (action_discarded & PRETEND)
   10687 			{
   10688 			  asection *kept;
   10689 
   10690 			  kept = _bfd_elf_check_kept_section (sec,
   10691 							      flinfo->info);
   10692 			  if (kept != NULL)
   10693 			    {
   10694 			      *ps = kept;
   10695 			      continue;
   10696 			    }
   10697 			}
   10698 		    }
   10699 		}
   10700 	    }
   10701 
   10702 	  /* Relocate the section by invoking a back end routine.
   10703 
   10704 	     The back end routine is responsible for adjusting the
   10705 	     section contents as necessary, and (if using Rela relocs
   10706 	     and generating a relocatable output file) adjusting the
   10707 	     reloc addend as necessary.
   10708 
   10709 	     The back end routine does not have to worry about setting
   10710 	     the reloc address or the reloc symbol index.
   10711 
   10712 	     The back end routine is given a pointer to the swapped in
   10713 	     internal symbols, and can access the hash table entries
   10714 	     for the external symbols via elf_sym_hashes (input_bfd).
   10715 
   10716 	     When generating relocatable output, the back end routine
   10717 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   10718 	     output symbol is going to be a section symbol
   10719 	     corresponding to the output section, which will require
   10720 	     the addend to be adjusted.  */
   10721 
   10722 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   10723 				     input_bfd, o, contents,
   10724 				     internal_relocs,
   10725 				     isymbuf,
   10726 				     flinfo->sections);
   10727 	  if (!ret)
   10728 	    return FALSE;
   10729 
   10730 	  if (ret == 2
   10731 	      || bfd_link_relocatable (flinfo->info)
   10732 	      || flinfo->info->emitrelocations)
   10733 	    {
   10734 	      Elf_Internal_Rela *irela;
   10735 	      Elf_Internal_Rela *irelaend, *irelamid;
   10736 	      bfd_vma last_offset;
   10737 	      struct elf_link_hash_entry **rel_hash;
   10738 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   10739 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   10740 	      unsigned int next_erel;
   10741 	      bfd_boolean rela_normal;
   10742 	      struct bfd_elf_section_data *esdi, *esdo;
   10743 
   10744 	      esdi = elf_section_data (o);
   10745 	      esdo = elf_section_data (o->output_section);
   10746 	      rela_normal = FALSE;
   10747 
   10748 	      /* Adjust the reloc addresses and symbol indices.  */
   10749 
   10750 	      irela = internal_relocs;
   10751 	      irelaend = irela + o->reloc_count;
   10752 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
   10753 	      /* We start processing the REL relocs, if any.  When we reach
   10754 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   10755 	      irelamid = irela;
   10756 	      if (esdi->rel.hdr != NULL)
   10757 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   10758 			     * bed->s->int_rels_per_ext_rel);
   10759 	      rel_hash_list = rel_hash;
   10760 	      rela_hash_list = NULL;
   10761 	      last_offset = o->output_offset;
   10762 	      if (!bfd_link_relocatable (flinfo->info))
   10763 		last_offset += o->output_section->vma;
   10764 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   10765 		{
   10766 		  unsigned long r_symndx;
   10767 		  asection *sec;
   10768 		  Elf_Internal_Sym sym;
   10769 
   10770 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   10771 		    {
   10772 		      rel_hash++;
   10773 		      next_erel = 0;
   10774 		    }
   10775 
   10776 		  if (irela == irelamid)
   10777 		    {
   10778 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
   10779 		      rela_hash_list = rel_hash;
   10780 		      rela_normal = bed->rela_normal;
   10781 		    }
   10782 
   10783 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   10784 							     flinfo->info, o,
   10785 							     irela->r_offset);
   10786 		  if (irela->r_offset >= (bfd_vma) -2)
   10787 		    {
   10788 		      /* This is a reloc for a deleted entry or somesuch.
   10789 			 Turn it into an R_*_NONE reloc, at the same
   10790 			 offset as the last reloc.  elf_eh_frame.c and
   10791 			 bfd_elf_discard_info rely on reloc offsets
   10792 			 being ordered.  */
   10793 		      irela->r_offset = last_offset;
   10794 		      irela->r_info = 0;
   10795 		      irela->r_addend = 0;
   10796 		      continue;
   10797 		    }
   10798 
   10799 		  irela->r_offset += o->output_offset;
   10800 
   10801 		  /* Relocs in an executable have to be virtual addresses.  */
   10802 		  if (!bfd_link_relocatable (flinfo->info))
   10803 		    irela->r_offset += o->output_section->vma;
   10804 
   10805 		  last_offset = irela->r_offset;
   10806 
   10807 		  r_symndx = irela->r_info >> r_sym_shift;
   10808 		  if (r_symndx == STN_UNDEF)
   10809 		    continue;
   10810 
   10811 		  if (r_symndx >= locsymcount
   10812 		      || (elf_bad_symtab (input_bfd)
   10813 			  && flinfo->sections[r_symndx] == NULL))
   10814 		    {
   10815 		      struct elf_link_hash_entry *rh;
   10816 		      unsigned long indx;
   10817 
   10818 		      /* This is a reloc against a global symbol.  We
   10819 			 have not yet output all the local symbols, so
   10820 			 we do not know the symbol index of any global
   10821 			 symbol.  We set the rel_hash entry for this
   10822 			 reloc to point to the global hash table entry
   10823 			 for this symbol.  The symbol index is then
   10824 			 set at the end of bfd_elf_final_link.  */
   10825 		      indx = r_symndx - extsymoff;
   10826 		      rh = elf_sym_hashes (input_bfd)[indx];
   10827 		      while (rh->root.type == bfd_link_hash_indirect
   10828 			     || rh->root.type == bfd_link_hash_warning)
   10829 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
   10830 
   10831 		      /* Setting the index to -2 tells
   10832 			 elf_link_output_extsym that this symbol is
   10833 			 used by a reloc.  */
   10834 		      BFD_ASSERT (rh->indx < 0);
   10835 		      rh->indx = -2;
   10836 		      *rel_hash = rh;
   10837 
   10838 		      continue;
   10839 		    }
   10840 
   10841 		  /* This is a reloc against a local symbol.  */
   10842 
   10843 		  *rel_hash = NULL;
   10844 		  sym = isymbuf[r_symndx];
   10845 		  sec = flinfo->sections[r_symndx];
   10846 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   10847 		    {
   10848 		      /* I suppose the backend ought to fill in the
   10849 			 section of any STT_SECTION symbol against a
   10850 			 processor specific section.  */
   10851 		      r_symndx = STN_UNDEF;
   10852 		      if (bfd_is_abs_section (sec))
   10853 			;
   10854 		      else if (sec == NULL || sec->owner == NULL)
   10855 			{
   10856 			  bfd_set_error (bfd_error_bad_value);
   10857 			  return FALSE;
   10858 			}
   10859 		      else
   10860 			{
   10861 			  asection *osec = sec->output_section;
   10862 
   10863 			  /* If we have discarded a section, the output
   10864 			     section will be the absolute section.  In
   10865 			     case of discarded SEC_MERGE sections, use
   10866 			     the kept section.  relocate_section should
   10867 			     have already handled discarded linkonce
   10868 			     sections.  */
   10869 			  if (bfd_is_abs_section (osec)
   10870 			      && sec->kept_section != NULL
   10871 			      && sec->kept_section->output_section != NULL)
   10872 			    {
   10873 			      osec = sec->kept_section->output_section;
   10874 			      irela->r_addend -= osec->vma;
   10875 			    }
   10876 
   10877 			  if (!bfd_is_abs_section (osec))
   10878 			    {
   10879 			      r_symndx = osec->target_index;
   10880 			      if (r_symndx == STN_UNDEF)
   10881 				{
   10882 				  irela->r_addend += osec->vma;
   10883 				  osec = _bfd_nearby_section (output_bfd, osec,
   10884 							      osec->vma);
   10885 				  irela->r_addend -= osec->vma;
   10886 				  r_symndx = osec->target_index;
   10887 				}
   10888 			    }
   10889 			}
   10890 
   10891 		      /* Adjust the addend according to where the
   10892 			 section winds up in the output section.  */
   10893 		      if (rela_normal)
   10894 			irela->r_addend += sec->output_offset;
   10895 		    }
   10896 		  else
   10897 		    {
   10898 		      if (flinfo->indices[r_symndx] == -1)
   10899 			{
   10900 			  unsigned long shlink;
   10901 			  const char *name;
   10902 			  asection *osec;
   10903 			  long indx;
   10904 
   10905 			  if (flinfo->info->strip == strip_all)
   10906 			    {
   10907 			      /* You can't do ld -r -s.  */
   10908 			      bfd_set_error (bfd_error_invalid_operation);
   10909 			      return FALSE;
   10910 			    }
   10911 
   10912 			  /* This symbol was skipped earlier, but
   10913 			     since it is needed by a reloc, we
   10914 			     must output it now.  */
   10915 			  shlink = symtab_hdr->sh_link;
   10916 			  name = (bfd_elf_string_from_elf_section
   10917 				  (input_bfd, shlink, sym.st_name));
   10918 			  if (name == NULL)
   10919 			    return FALSE;
   10920 
   10921 			  osec = sec->output_section;
   10922 			  sym.st_shndx =
   10923 			    _bfd_elf_section_from_bfd_section (output_bfd,
   10924 							       osec);
   10925 			  if (sym.st_shndx == SHN_BAD)
   10926 			    return FALSE;
   10927 
   10928 			  sym.st_value += sec->output_offset;
   10929 			  if (!bfd_link_relocatable (flinfo->info))
   10930 			    {
   10931 			      sym.st_value += osec->vma;
   10932 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   10933 				{
   10934 				  /* STT_TLS symbols are relative to PT_TLS
   10935 				     segment base.  */
   10936 				  BFD_ASSERT (elf_hash_table (flinfo->info)
   10937 					      ->tls_sec != NULL);
   10938 				  sym.st_value -= (elf_hash_table (flinfo->info)
   10939 						   ->tls_sec->vma);
   10940 				}
   10941 			    }
   10942 
   10943 			  indx = bfd_get_symcount (output_bfd);
   10944 			  ret = elf_link_output_symstrtab (flinfo, name,
   10945 							   &sym, sec,
   10946 							   NULL);
   10947 			  if (ret == 0)
   10948 			    return FALSE;
   10949 			  else if (ret == 1)
   10950 			    flinfo->indices[r_symndx] = indx;
   10951 			  else
   10952 			    abort ();
   10953 			}
   10954 
   10955 		      r_symndx = flinfo->indices[r_symndx];
   10956 		    }
   10957 
   10958 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   10959 				   | (irela->r_info & r_type_mask));
   10960 		}
   10961 
   10962 	      /* Swap out the relocs.  */
   10963 	      input_rel_hdr = esdi->rel.hdr;
   10964 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   10965 		{
   10966 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   10967 						     input_rel_hdr,
   10968 						     internal_relocs,
   10969 						     rel_hash_list))
   10970 		    return FALSE;
   10971 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   10972 				      * bed->s->int_rels_per_ext_rel);
   10973 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   10974 		}
   10975 
   10976 	      input_rela_hdr = esdi->rela.hdr;
   10977 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   10978 		{
   10979 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   10980 						     input_rela_hdr,
   10981 						     internal_relocs,
   10982 						     rela_hash_list))
   10983 		    return FALSE;
   10984 		}
   10985 	    }
   10986 	}
   10987 
   10988       /* Write out the modified section contents.  */
   10989       if (bed->elf_backend_write_section
   10990 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   10991 						contents))
   10992 	{
   10993 	  /* Section written out.  */
   10994 	}
   10995       else switch (o->sec_info_type)
   10996 	{
   10997 	case SEC_INFO_TYPE_STABS:
   10998 	  if (! (_bfd_write_section_stabs
   10999 		 (output_bfd,
   11000 		  &elf_hash_table (flinfo->info)->stab_info,
   11001 		  o, &elf_section_data (o)->sec_info, contents)))
   11002 	    return FALSE;
   11003 	  break;
   11004 	case SEC_INFO_TYPE_MERGE:
   11005 	  if (! _bfd_write_merged_section (output_bfd, o,
   11006 					   elf_section_data (o)->sec_info))
   11007 	    return FALSE;
   11008 	  break;
   11009 	case SEC_INFO_TYPE_EH_FRAME:
   11010 	  {
   11011 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   11012 						   o, contents))
   11013 	      return FALSE;
   11014 	  }
   11015 	  break;
   11016 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   11017 	  {
   11018 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   11019 							 flinfo->info,
   11020 							 o, contents))
   11021 	      return FALSE;
   11022 	  }
   11023 	  break;
   11024 	default:
   11025 	  {
   11026 	    if (! (o->flags & SEC_EXCLUDE))
   11027 	      {
   11028 		file_ptr offset = (file_ptr) o->output_offset;
   11029 		bfd_size_type todo = o->size;
   11030 
   11031 		offset *= bfd_octets_per_byte (output_bfd);
   11032 
   11033 		if ((o->flags & SEC_ELF_REVERSE_COPY))
   11034 		  {
   11035 		    /* Reverse-copy input section to output.  */
   11036 		    do
   11037 		      {
   11038 			todo -= address_size;
   11039 			if (! bfd_set_section_contents (output_bfd,
   11040 							o->output_section,
   11041 							contents + todo,
   11042 							offset,
   11043 							address_size))
   11044 			  return FALSE;
   11045 			if (todo == 0)
   11046 			  break;
   11047 			offset += address_size;
   11048 		      }
   11049 		    while (1);
   11050 		  }
   11051 		else if (! bfd_set_section_contents (output_bfd,
   11052 						     o->output_section,
   11053 						     contents,
   11054 						     offset, todo))
   11055 		  return FALSE;
   11056 	      }
   11057 	  }
   11058 	  break;
   11059 	}
   11060     }
   11061 
   11062   return TRUE;
   11063 }
   11064 
   11065 /* Generate a reloc when linking an ELF file.  This is a reloc
   11066    requested by the linker, and does not come from any input file.  This
   11067    is used to build constructor and destructor tables when linking
   11068    with -Ur.  */
   11069 
   11070 static bfd_boolean
   11071 elf_reloc_link_order (bfd *output_bfd,
   11072 		      struct bfd_link_info *info,
   11073 		      asection *output_section,
   11074 		      struct bfd_link_order *link_order)
   11075 {
   11076   reloc_howto_type *howto;
   11077   long indx;
   11078   bfd_vma offset;
   11079   bfd_vma addend;
   11080   struct bfd_elf_section_reloc_data *reldata;
   11081   struct elf_link_hash_entry **rel_hash_ptr;
   11082   Elf_Internal_Shdr *rel_hdr;
   11083   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   11084   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   11085   bfd_byte *erel;
   11086   unsigned int i;
   11087   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   11088 
   11089   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   11090   if (howto == NULL)
   11091     {
   11092       bfd_set_error (bfd_error_bad_value);
   11093       return FALSE;
   11094     }
   11095 
   11096   addend = link_order->u.reloc.p->addend;
   11097 
   11098   if (esdo->rel.hdr)
   11099     reldata = &esdo->rel;
   11100   else if (esdo->rela.hdr)
   11101     reldata = &esdo->rela;
   11102   else
   11103     {
   11104       reldata = NULL;
   11105       BFD_ASSERT (0);
   11106     }
   11107 
   11108   /* Figure out the symbol index.  */
   11109   rel_hash_ptr = reldata->hashes + reldata->count;
   11110   if (link_order->type == bfd_section_reloc_link_order)
   11111     {
   11112       indx = link_order->u.reloc.p->u.section->target_index;
   11113       BFD_ASSERT (indx != 0);
   11114       *rel_hash_ptr = NULL;
   11115     }
   11116   else
   11117     {
   11118       struct elf_link_hash_entry *h;
   11119 
   11120       /* Treat a reloc against a defined symbol as though it were
   11121 	 actually against the section.  */
   11122       h = ((struct elf_link_hash_entry *)
   11123 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   11124 					 link_order->u.reloc.p->u.name,
   11125 					 FALSE, FALSE, TRUE));
   11126       if (h != NULL
   11127 	  && (h->root.type == bfd_link_hash_defined
   11128 	      || h->root.type == bfd_link_hash_defweak))
   11129 	{
   11130 	  asection *section;
   11131 
   11132 	  section = h->root.u.def.section;
   11133 	  indx = section->output_section->target_index;
   11134 	  *rel_hash_ptr = NULL;
   11135 	  /* It seems that we ought to add the symbol value to the
   11136 	     addend here, but in practice it has already been added
   11137 	     because it was passed to constructor_callback.  */
   11138 	  addend += section->output_section->vma + section->output_offset;
   11139 	}
   11140       else if (h != NULL)
   11141 	{
   11142 	  /* Setting the index to -2 tells elf_link_output_extsym that
   11143 	     this symbol is used by a reloc.  */
   11144 	  h->indx = -2;
   11145 	  *rel_hash_ptr = h;
   11146 	  indx = 0;
   11147 	}
   11148       else
   11149 	{
   11150 	  (*info->callbacks->unattached_reloc)
   11151 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   11152 	  indx = 0;
   11153 	}
   11154     }
   11155 
   11156   /* If this is an inplace reloc, we must write the addend into the
   11157      object file.  */
   11158   if (howto->partial_inplace && addend != 0)
   11159     {
   11160       bfd_size_type size;
   11161       bfd_reloc_status_type rstat;
   11162       bfd_byte *buf;
   11163       bfd_boolean ok;
   11164       const char *sym_name;
   11165 
   11166       size = (bfd_size_type) bfd_get_reloc_size (howto);
   11167       buf = (bfd_byte *) bfd_zmalloc (size);
   11168       if (buf == NULL && size != 0)
   11169 	return FALSE;
   11170       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   11171       switch (rstat)
   11172 	{
   11173 	case bfd_reloc_ok:
   11174 	  break;
   11175 
   11176 	default:
   11177 	case bfd_reloc_outofrange:
   11178 	  abort ();
   11179 
   11180 	case bfd_reloc_overflow:
   11181 	  if (link_order->type == bfd_section_reloc_link_order)
   11182 	    sym_name = bfd_section_name (output_bfd,
   11183 					 link_order->u.reloc.p->u.section);
   11184 	  else
   11185 	    sym_name = link_order->u.reloc.p->u.name;
   11186 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
   11187 					      howto->name, addend, NULL, NULL,
   11188 					      (bfd_vma) 0);
   11189 	  break;
   11190 	}
   11191 
   11192       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   11193 				     link_order->offset
   11194 				     * bfd_octets_per_byte (output_bfd),
   11195 				     size);
   11196       free (buf);
   11197       if (! ok)
   11198 	return FALSE;
   11199     }
   11200 
   11201   /* The address of a reloc is relative to the section in a
   11202      relocatable file, and is a virtual address in an executable
   11203      file.  */
   11204   offset = link_order->offset;
   11205   if (! bfd_link_relocatable (info))
   11206     offset += output_section->vma;
   11207 
   11208   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   11209     {
   11210       irel[i].r_offset = offset;
   11211       irel[i].r_info = 0;
   11212       irel[i].r_addend = 0;
   11213     }
   11214   if (bed->s->arch_size == 32)
   11215     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   11216   else
   11217 #ifdef BFD64
   11218           {
   11219             bfd_uint64_t indx64 = indx;
   11220             irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
   11221           }
   11222 #else
   11223           BFD_FAIL();
   11224 #endif
   11225 
   11226   rel_hdr = reldata->hdr;
   11227   erel = rel_hdr->contents;
   11228   if (rel_hdr->sh_type == SHT_REL)
   11229     {
   11230       erel += reldata->count * bed->s->sizeof_rel;
   11231       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   11232     }
   11233   else
   11234     {
   11235       irel[0].r_addend = addend;
   11236       erel += reldata->count * bed->s->sizeof_rela;
   11237       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   11238     }
   11239 
   11240   ++reldata->count;
   11241 
   11242   return TRUE;
   11243 }
   11244 
   11245 
   11246 /* Get the output vma of the section pointed to by the sh_link field.  */
   11247 
   11248 static bfd_vma
   11249 elf_get_linked_section_vma (struct bfd_link_order *p)
   11250 {
   11251   Elf_Internal_Shdr **elf_shdrp;
   11252   asection *s;
   11253   int elfsec;
   11254 
   11255   s = p->u.indirect.section;
   11256   elf_shdrp = elf_elfsections (s->owner);
   11257   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
   11258   elfsec = elf_shdrp[elfsec]->sh_link;
   11259   /* PR 290:
   11260      The Intel C compiler generates SHT_IA_64_UNWIND with
   11261      SHF_LINK_ORDER.  But it doesn't set the sh_link or
   11262      sh_info fields.  Hence we could get the situation
   11263      where elfsec is 0.  */
   11264   if (elfsec == 0)
   11265     {
   11266       const struct elf_backend_data *bed
   11267 	= get_elf_backend_data (s->owner);
   11268       if (bed->link_order_error_handler)
   11269 	bed->link_order_error_handler
   11270 	  /* xgettext:c-format */
   11271 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
   11272       return 0;
   11273     }
   11274   else
   11275     {
   11276       s = elf_shdrp[elfsec]->bfd_section;
   11277       return s->output_section->vma + s->output_offset;
   11278     }
   11279 }
   11280 
   11281 
   11282 /* Compare two sections based on the locations of the sections they are
   11283    linked to.  Used by elf_fixup_link_order.  */
   11284 
   11285 static int
   11286 compare_link_order (const void * a, const void * b)
   11287 {
   11288   bfd_vma apos;
   11289   bfd_vma bpos;
   11290 
   11291   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
   11292   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
   11293   if (apos < bpos)
   11294     return -1;
   11295   return apos > bpos;
   11296 }
   11297 
   11298 
   11299 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
   11300    order as their linked sections.  Returns false if this could not be done
   11301    because an output section includes both ordered and unordered
   11302    sections.  Ideally we'd do this in the linker proper.  */
   11303 
   11304 static bfd_boolean
   11305 elf_fixup_link_order (bfd *abfd, asection *o)
   11306 {
   11307   int seen_linkorder;
   11308   int seen_other;
   11309   int n;
   11310   struct bfd_link_order *p;
   11311   bfd *sub;
   11312   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11313   unsigned elfsec;
   11314   struct bfd_link_order **sections;
   11315   asection *s, *other_sec, *linkorder_sec;
   11316   bfd_vma offset;
   11317 
   11318   other_sec = NULL;
   11319   linkorder_sec = NULL;
   11320   seen_other = 0;
   11321   seen_linkorder = 0;
   11322   for (p = o->map_head.link_order; p != NULL; p = p->next)
   11323     {
   11324       if (p->type == bfd_indirect_link_order)
   11325 	{
   11326 	  s = p->u.indirect.section;
   11327 	  sub = s->owner;
   11328 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   11329 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
   11330 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
   11331 	      && elfsec < elf_numsections (sub)
   11332 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
   11333 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
   11334 	    {
   11335 	      seen_linkorder++;
   11336 	      linkorder_sec = s;
   11337 	    }
   11338 	  else
   11339 	    {
   11340 	      seen_other++;
   11341 	      other_sec = s;
   11342 	    }
   11343 	}
   11344       else
   11345 	seen_other++;
   11346 
   11347       if (seen_other && seen_linkorder)
   11348 	{
   11349 	  if (other_sec && linkorder_sec)
   11350 	    _bfd_error_handler
   11351 	      /* xgettext:c-format */
   11352 	      (_("%A has both ordered [`%A' in %B] "
   11353 		 "and unordered [`%A' in %B] sections"),
   11354 	       o, linkorder_sec, linkorder_sec->owner,
   11355 	       other_sec, other_sec->owner);
   11356 	  else
   11357 	    _bfd_error_handler
   11358 	      (_("%A has both ordered and unordered sections"), o);
   11359 	  bfd_set_error (bfd_error_bad_value);
   11360 	  return FALSE;
   11361 	}
   11362     }
   11363 
   11364   if (!seen_linkorder)
   11365     return TRUE;
   11366 
   11367   sections = (struct bfd_link_order **)
   11368     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
   11369   if (sections == NULL)
   11370     return FALSE;
   11371   seen_linkorder = 0;
   11372 
   11373   for (p = o->map_head.link_order; p != NULL; p = p->next)
   11374     {
   11375       sections[seen_linkorder++] = p;
   11376     }
   11377   /* Sort the input sections in the order of their linked section.  */
   11378   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
   11379 	 compare_link_order);
   11380 
   11381   /* Change the offsets of the sections.  */
   11382   offset = 0;
   11383   for (n = 0; n < seen_linkorder; n++)
   11384     {
   11385       s = sections[n]->u.indirect.section;
   11386       offset &= ~(bfd_vma) 0 << s->alignment_power;
   11387       s->output_offset = offset / bfd_octets_per_byte (abfd);
   11388       sections[n]->offset = offset;
   11389       offset += sections[n]->size;
   11390     }
   11391 
   11392   free (sections);
   11393   return TRUE;
   11394 }
   11395 
   11396 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
   11397    Returns TRUE upon success, FALSE otherwise.  */
   11398 
   11399 static bfd_boolean
   11400 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
   11401 {
   11402   bfd_boolean ret = FALSE;
   11403   bfd *implib_bfd;
   11404   const struct elf_backend_data *bed;
   11405   flagword flags;
   11406   enum bfd_architecture arch;
   11407   unsigned int mach;
   11408   asymbol **sympp = NULL;
   11409   long symsize;
   11410   long symcount;
   11411   long src_count;
   11412   elf_symbol_type *osymbuf;
   11413 
   11414   implib_bfd = info->out_implib_bfd;
   11415   bed = get_elf_backend_data (abfd);
   11416 
   11417   if (!bfd_set_format (implib_bfd, bfd_object))
   11418     return FALSE;
   11419 
   11420   /* Use flag from executable but make it a relocatable object.  */
   11421   flags = bfd_get_file_flags (abfd);
   11422   flags &= ~HAS_RELOC;
   11423   if (!bfd_set_start_address (implib_bfd, 0)
   11424       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
   11425     return FALSE;
   11426 
   11427   /* Copy architecture of output file to import library file.  */
   11428   arch = bfd_get_arch (abfd);
   11429   mach = bfd_get_mach (abfd);
   11430   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
   11431       && (abfd->target_defaulted
   11432 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
   11433     return FALSE;
   11434 
   11435   /* Get symbol table size.  */
   11436   symsize = bfd_get_symtab_upper_bound (abfd);
   11437   if (symsize < 0)
   11438     return FALSE;
   11439 
   11440   /* Read in the symbol table.  */
   11441   sympp = (asymbol **) xmalloc (symsize);
   11442   symcount = bfd_canonicalize_symtab (abfd, sympp);
   11443   if (symcount < 0)
   11444     goto free_sym_buf;
   11445 
   11446   /* Allow the BFD backend to copy any private header data it
   11447      understands from the output BFD to the import library BFD.  */
   11448   if (! bfd_copy_private_header_data (abfd, implib_bfd))
   11449     goto free_sym_buf;
   11450 
   11451   /* Filter symbols to appear in the import library.  */
   11452   if (bed->elf_backend_filter_implib_symbols)
   11453     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
   11454 						       symcount);
   11455   else
   11456     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
   11457   if (symcount == 0)
   11458     {
   11459       bfd_set_error (bfd_error_no_symbols);
   11460       _bfd_error_handler (_("%B: no symbol found for import library"),
   11461 			  implib_bfd);
   11462       goto free_sym_buf;
   11463     }
   11464 
   11465 
   11466   /* Make symbols absolute.  */
   11467   osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
   11468 					    sizeof (*osymbuf));
   11469   for (src_count = 0; src_count < symcount; src_count++)
   11470     {
   11471       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
   11472 	      sizeof (*osymbuf));
   11473       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
   11474       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
   11475       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
   11476       osymbuf[src_count].internal_elf_sym.st_value =
   11477 	osymbuf[src_count].symbol.value;
   11478       sympp[src_count] = &osymbuf[src_count].symbol;
   11479     }
   11480 
   11481   bfd_set_symtab (implib_bfd, sympp, symcount);
   11482 
   11483   /* Allow the BFD backend to copy any private data it understands
   11484      from the output BFD to the import library BFD.  This is done last
   11485      to permit the routine to look at the filtered symbol table.  */
   11486   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
   11487     goto free_sym_buf;
   11488 
   11489   if (!bfd_close (implib_bfd))
   11490     goto free_sym_buf;
   11491 
   11492   ret = TRUE;
   11493 
   11494 free_sym_buf:
   11495   free (sympp);
   11496   return ret;
   11497 }
   11498 
   11499 static void
   11500 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   11501 {
   11502   asection *o;
   11503 
   11504   if (flinfo->symstrtab != NULL)
   11505     _bfd_elf_strtab_free (flinfo->symstrtab);
   11506   if (flinfo->contents != NULL)
   11507     free (flinfo->contents);
   11508   if (flinfo->external_relocs != NULL)
   11509     free (flinfo->external_relocs);
   11510   if (flinfo->internal_relocs != NULL)
   11511     free (flinfo->internal_relocs);
   11512   if (flinfo->external_syms != NULL)
   11513     free (flinfo->external_syms);
   11514   if (flinfo->locsym_shndx != NULL)
   11515     free (flinfo->locsym_shndx);
   11516   if (flinfo->internal_syms != NULL)
   11517     free (flinfo->internal_syms);
   11518   if (flinfo->indices != NULL)
   11519     free (flinfo->indices);
   11520   if (flinfo->sections != NULL)
   11521     free (flinfo->sections);
   11522   if (flinfo->symshndxbuf != NULL)
   11523     free (flinfo->symshndxbuf);
   11524   for (o = obfd->sections; o != NULL; o = o->next)
   11525     {
   11526       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11527       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
   11528 	free (esdo->rel.hashes);
   11529       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
   11530 	free (esdo->rela.hashes);
   11531     }
   11532 }
   11533 
   11534 /* Do the final step of an ELF link.  */
   11535 
   11536 bfd_boolean
   11537 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   11538 {
   11539   bfd_boolean dynamic;
   11540   bfd_boolean emit_relocs;
   11541   bfd *dynobj;
   11542   struct elf_final_link_info flinfo;
   11543   asection *o;
   11544   struct bfd_link_order *p;
   11545   bfd *sub;
   11546   bfd_size_type max_contents_size;
   11547   bfd_size_type max_external_reloc_size;
   11548   bfd_size_type max_internal_reloc_count;
   11549   bfd_size_type max_sym_count;
   11550   bfd_size_type max_sym_shndx_count;
   11551   Elf_Internal_Sym elfsym;
   11552   unsigned int i;
   11553   Elf_Internal_Shdr *symtab_hdr;
   11554   Elf_Internal_Shdr *symtab_shndx_hdr;
   11555   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11556   struct elf_outext_info eoinfo;
   11557   bfd_boolean merged;
   11558   size_t relativecount = 0;
   11559   asection *reldyn = 0;
   11560   bfd_size_type amt;
   11561   asection *attr_section = NULL;
   11562   bfd_vma attr_size = 0;
   11563   const char *std_attrs_section;
   11564   struct elf_link_hash_table *htab = elf_hash_table (info);
   11565 
   11566   if (!is_elf_hash_table (htab))
   11567     return FALSE;
   11568 
   11569   if (bfd_link_pic (info))
   11570     abfd->flags |= DYNAMIC;
   11571 
   11572   dynamic = htab->dynamic_sections_created;
   11573   dynobj = htab->dynobj;
   11574 
   11575   emit_relocs = (bfd_link_relocatable (info)
   11576 		 || info->emitrelocations);
   11577 
   11578   flinfo.info = info;
   11579   flinfo.output_bfd = abfd;
   11580   flinfo.symstrtab = _bfd_elf_strtab_init ();
   11581   if (flinfo.symstrtab == NULL)
   11582     return FALSE;
   11583 
   11584   if (! dynamic)
   11585     {
   11586       flinfo.hash_sec = NULL;
   11587       flinfo.symver_sec = NULL;
   11588     }
   11589   else
   11590     {
   11591       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   11592       /* Note that dynsym_sec can be NULL (on VMS).  */
   11593       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   11594       /* Note that it is OK if symver_sec is NULL.  */
   11595     }
   11596 
   11597   flinfo.contents = NULL;
   11598   flinfo.external_relocs = NULL;
   11599   flinfo.internal_relocs = NULL;
   11600   flinfo.external_syms = NULL;
   11601   flinfo.locsym_shndx = NULL;
   11602   flinfo.internal_syms = NULL;
   11603   flinfo.indices = NULL;
   11604   flinfo.sections = NULL;
   11605   flinfo.symshndxbuf = NULL;
   11606   flinfo.filesym_count = 0;
   11607 
   11608   /* The object attributes have been merged.  Remove the input
   11609      sections from the link, and set the contents of the output
   11610      secton.  */
   11611   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   11612   for (o = abfd->sections; o != NULL; o = o->next)
   11613     {
   11614       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
   11615 	  || strcmp (o->name, ".gnu.attributes") == 0)
   11616 	{
   11617 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   11618 	    {
   11619 	      asection *input_section;
   11620 
   11621 	      if (p->type != bfd_indirect_link_order)
   11622 		continue;
   11623 	      input_section = p->u.indirect.section;
   11624 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   11625 		 elf_link_input_bfd ignores this section.  */
   11626 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   11627 	    }
   11628 
   11629 	  attr_size = bfd_elf_obj_attr_size (abfd);
   11630 	  if (attr_size)
   11631 	    {
   11632 	      bfd_set_section_size (abfd, o, attr_size);
   11633 	      attr_section = o;
   11634 	      /* Skip this section later on.  */
   11635 	      o->map_head.link_order = NULL;
   11636 	    }
   11637 	  else
   11638 	    o->flags |= SEC_EXCLUDE;
   11639 	}
   11640     }
   11641 
   11642   /* Count up the number of relocations we will output for each output
   11643      section, so that we know the sizes of the reloc sections.  We
   11644      also figure out some maximum sizes.  */
   11645   max_contents_size = 0;
   11646   max_external_reloc_size = 0;
   11647   max_internal_reloc_count = 0;
   11648   max_sym_count = 0;
   11649   max_sym_shndx_count = 0;
   11650   merged = FALSE;
   11651   for (o = abfd->sections; o != NULL; o = o->next)
   11652     {
   11653       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11654       o->reloc_count = 0;
   11655 
   11656       for (p = o->map_head.link_order; p != NULL; p = p->next)
   11657 	{
   11658 	  unsigned int reloc_count = 0;
   11659 	  unsigned int additional_reloc_count = 0;
   11660 	  struct bfd_elf_section_data *esdi = NULL;
   11661 
   11662 	  if (p->type == bfd_section_reloc_link_order
   11663 	      || p->type == bfd_symbol_reloc_link_order)
   11664 	    reloc_count = 1;
   11665 	  else if (p->type == bfd_indirect_link_order)
   11666 	    {
   11667 	      asection *sec;
   11668 
   11669 	      sec = p->u.indirect.section;
   11670 
   11671 	      /* Mark all sections which are to be included in the
   11672 		 link.  This will normally be every section.  We need
   11673 		 to do this so that we can identify any sections which
   11674 		 the linker has decided to not include.  */
   11675 	      sec->linker_mark = TRUE;
   11676 
   11677 	      if (sec->flags & SEC_MERGE)
   11678 		merged = TRUE;
   11679 
   11680 	      if (sec->rawsize > max_contents_size)
   11681 		max_contents_size = sec->rawsize;
   11682 	      if (sec->size > max_contents_size)
   11683 		max_contents_size = sec->size;
   11684 
   11685 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   11686 		  && (sec->owner->flags & DYNAMIC) == 0)
   11687 		{
   11688 		  size_t sym_count;
   11689 
   11690 		  /* We are interested in just local symbols, not all
   11691 		     symbols.  */
   11692 		  if (elf_bad_symtab (sec->owner))
   11693 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   11694 				 / bed->s->sizeof_sym);
   11695 		  else
   11696 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   11697 
   11698 		  if (sym_count > max_sym_count)
   11699 		    max_sym_count = sym_count;
   11700 
   11701 		  if (sym_count > max_sym_shndx_count
   11702 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   11703 		    max_sym_shndx_count = sym_count;
   11704 
   11705 		  if (esdo->this_hdr.sh_type == SHT_REL
   11706 		      || esdo->this_hdr.sh_type == SHT_RELA)
   11707 		    /* Some backends use reloc_count in relocation sections
   11708 		       to count particular types of relocs.  Of course,
   11709 		       reloc sections themselves can't have relocations.  */
   11710 		    ;
   11711 		  else if (emit_relocs)
   11712 		    {
   11713 		      reloc_count = sec->reloc_count;
   11714 		      if (bed->elf_backend_count_additional_relocs)
   11715 			{
   11716 			  int c;
   11717 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
   11718 			  additional_reloc_count += c;
   11719 			}
   11720 		    }
   11721 		  else if (bed->elf_backend_count_relocs)
   11722 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   11723 
   11724 		  esdi = elf_section_data (sec);
   11725 
   11726 		  if ((sec->flags & SEC_RELOC) != 0)
   11727 		    {
   11728 		      size_t ext_size = 0;
   11729 
   11730 		      if (esdi->rel.hdr != NULL)
   11731 			ext_size = esdi->rel.hdr->sh_size;
   11732 		      if (esdi->rela.hdr != NULL)
   11733 			ext_size += esdi->rela.hdr->sh_size;
   11734 
   11735 		      if (ext_size > max_external_reloc_size)
   11736 			max_external_reloc_size = ext_size;
   11737 		      if (sec->reloc_count > max_internal_reloc_count)
   11738 			max_internal_reloc_count = sec->reloc_count;
   11739 		    }
   11740 		}
   11741 	    }
   11742 
   11743 	  if (reloc_count == 0)
   11744 	    continue;
   11745 
   11746 	  reloc_count += additional_reloc_count;
   11747 	  o->reloc_count += reloc_count;
   11748 
   11749 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   11750 	    {
   11751 	      if (esdi->rel.hdr)
   11752 		{
   11753 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   11754 		  esdo->rel.count += additional_reloc_count;
   11755 		}
   11756 	      if (esdi->rela.hdr)
   11757 		{
   11758 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   11759 		  esdo->rela.count += additional_reloc_count;
   11760 		}
   11761 	    }
   11762 	  else
   11763 	    {
   11764 	      if (o->use_rela_p)
   11765 		esdo->rela.count += reloc_count;
   11766 	      else
   11767 		esdo->rel.count += reloc_count;
   11768 	    }
   11769 	}
   11770 
   11771       if (o->reloc_count > 0)
   11772 	o->flags |= SEC_RELOC;
   11773       else
   11774 	{
   11775 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   11776 	     set it (this is probably a bug) and if it is set
   11777 	     assign_section_numbers will create a reloc section.  */
   11778 	  o->flags &=~ SEC_RELOC;
   11779 	}
   11780 
   11781       /* If the SEC_ALLOC flag is not set, force the section VMA to
   11782 	 zero.  This is done in elf_fake_sections as well, but forcing
   11783 	 the VMA to 0 here will ensure that relocs against these
   11784 	 sections are handled correctly.  */
   11785       if ((o->flags & SEC_ALLOC) == 0
   11786 	  && ! o->user_set_vma)
   11787 	o->vma = 0;
   11788     }
   11789 
   11790   if (! bfd_link_relocatable (info) && merged)
   11791     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
   11792 
   11793   /* Figure out the file positions for everything but the symbol table
   11794      and the relocs.  We set symcount to force assign_section_numbers
   11795      to create a symbol table.  */
   11796   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
   11797   BFD_ASSERT (! abfd->output_has_begun);
   11798   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   11799     goto error_return;
   11800 
   11801   /* Set sizes, and assign file positions for reloc sections.  */
   11802   for (o = abfd->sections; o != NULL; o = o->next)
   11803     {
   11804       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11805       if ((o->flags & SEC_RELOC) != 0)
   11806 	{
   11807 	  if (esdo->rel.hdr
   11808 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   11809 	    goto error_return;
   11810 
   11811 	  if (esdo->rela.hdr
   11812 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   11813 	    goto error_return;
   11814 	}
   11815 
   11816       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   11817 	 to count upwards while actually outputting the relocations.  */
   11818       esdo->rel.count = 0;
   11819       esdo->rela.count = 0;
   11820 
   11821       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
   11822 	{
   11823 	  /* Cache the section contents so that they can be compressed
   11824 	     later.  Use bfd_malloc since it will be freed by
   11825 	     bfd_compress_section_contents.  */
   11826 	  unsigned char *contents = esdo->this_hdr.contents;
   11827 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
   11828 	    abort ();
   11829 	  contents
   11830 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   11831 	  if (contents == NULL)
   11832 	    goto error_return;
   11833 	  esdo->this_hdr.contents = contents;
   11834 	}
   11835     }
   11836 
   11837   /* We have now assigned file positions for all the sections except
   11838      .symtab, .strtab, and non-loaded reloc sections.  We start the
   11839      .symtab section at the current file position, and write directly
   11840      to it.  We build the .strtab section in memory.  */
   11841   bfd_get_symcount (abfd) = 0;
   11842   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11843   /* sh_name is set in prep_headers.  */
   11844   symtab_hdr->sh_type = SHT_SYMTAB;
   11845   /* sh_flags, sh_addr and sh_size all start off zero.  */
   11846   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   11847   /* sh_link is set in assign_section_numbers.  */
   11848   /* sh_info is set below.  */
   11849   /* sh_offset is set just below.  */
   11850   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   11851 
   11852   if (max_sym_count < 20)
   11853     max_sym_count = 20;
   11854   htab->strtabsize = max_sym_count;
   11855   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   11856   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
   11857   if (htab->strtab == NULL)
   11858     goto error_return;
   11859   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   11860   flinfo.symshndxbuf
   11861     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   11862        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   11863 
   11864   if (info->strip != strip_all || emit_relocs)
   11865     {
   11866       file_ptr off = elf_next_file_pos (abfd);
   11867 
   11868       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
   11869 
   11870       /* Note that at this point elf_next_file_pos (abfd) is
   11871 	 incorrect.  We do not yet know the size of the .symtab section.
   11872 	 We correct next_file_pos below, after we do know the size.  */
   11873 
   11874       /* Start writing out the symbol table.  The first symbol is always a
   11875 	 dummy symbol.  */
   11876       elfsym.st_value = 0;
   11877       elfsym.st_size = 0;
   11878       elfsym.st_info = 0;
   11879       elfsym.st_other = 0;
   11880       elfsym.st_shndx = SHN_UNDEF;
   11881       elfsym.st_target_internal = 0;
   11882       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   11883 				     bfd_und_section_ptr, NULL) != 1)
   11884 	goto error_return;
   11885 
   11886       /* Output a symbol for each section.  We output these even if we are
   11887 	 discarding local symbols, since they are used for relocs.  These
   11888 	 symbols have no names.  We store the index of each one in the
   11889 	 index field of the section, so that we can find it again when
   11890 	 outputting relocs.  */
   11891 
   11892       elfsym.st_size = 0;
   11893       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   11894       elfsym.st_other = 0;
   11895       elfsym.st_value = 0;
   11896       elfsym.st_target_internal = 0;
   11897       for (i = 1; i < elf_numsections (abfd); i++)
   11898 	{
   11899 	  o = bfd_section_from_elf_index (abfd, i);
   11900 	  if (o != NULL)
   11901 	    {
   11902 	      o->target_index = bfd_get_symcount (abfd);
   11903 	      elfsym.st_shndx = i;
   11904 	      if (!bfd_link_relocatable (info))
   11905 		elfsym.st_value = o->vma;
   11906 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
   11907 					     NULL) != 1)
   11908 		goto error_return;
   11909 	    }
   11910 	}
   11911     }
   11912 
   11913   /* Allocate some memory to hold information read in from the input
   11914      files.  */
   11915   if (max_contents_size != 0)
   11916     {
   11917       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   11918       if (flinfo.contents == NULL)
   11919 	goto error_return;
   11920     }
   11921 
   11922   if (max_external_reloc_size != 0)
   11923     {
   11924       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   11925       if (flinfo.external_relocs == NULL)
   11926 	goto error_return;
   11927     }
   11928 
   11929   if (max_internal_reloc_count != 0)
   11930     {
   11931       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
   11932       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   11933       if (flinfo.internal_relocs == NULL)
   11934 	goto error_return;
   11935     }
   11936 
   11937   if (max_sym_count != 0)
   11938     {
   11939       amt = max_sym_count * bed->s->sizeof_sym;
   11940       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   11941       if (flinfo.external_syms == NULL)
   11942 	goto error_return;
   11943 
   11944       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   11945       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   11946       if (flinfo.internal_syms == NULL)
   11947 	goto error_return;
   11948 
   11949       amt = max_sym_count * sizeof (long);
   11950       flinfo.indices = (long int *) bfd_malloc (amt);
   11951       if (flinfo.indices == NULL)
   11952 	goto error_return;
   11953 
   11954       amt = max_sym_count * sizeof (asection *);
   11955       flinfo.sections = (asection **) bfd_malloc (amt);
   11956       if (flinfo.sections == NULL)
   11957 	goto error_return;
   11958     }
   11959 
   11960   if (max_sym_shndx_count != 0)
   11961     {
   11962       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   11963       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   11964       if (flinfo.locsym_shndx == NULL)
   11965 	goto error_return;
   11966     }
   11967 
   11968   if (htab->tls_sec)
   11969     {
   11970       bfd_vma base, end = 0;
   11971       asection *sec;
   11972 
   11973       for (sec = htab->tls_sec;
   11974 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   11975 	   sec = sec->next)
   11976 	{
   11977 	  bfd_size_type size = sec->size;
   11978 
   11979 	  if (size == 0
   11980 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   11981 	    {
   11982 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   11983 
   11984 	      if (ord != NULL)
   11985 		size = ord->offset + ord->size;
   11986 	    }
   11987 	  end = sec->vma + size;
   11988 	}
   11989       base = htab->tls_sec->vma;
   11990       /* Only align end of TLS section if static TLS doesn't have special
   11991 	 alignment requirements.  */
   11992       if (bed->static_tls_alignment == 1)
   11993 	end = align_power (end, htab->tls_sec->alignment_power);
   11994       htab->tls_size = end - base;
   11995     }
   11996 
   11997   /* Reorder SHF_LINK_ORDER sections.  */
   11998   for (o = abfd->sections; o != NULL; o = o->next)
   11999     {
   12000       if (!elf_fixup_link_order (abfd, o))
   12001 	return FALSE;
   12002     }
   12003 
   12004   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   12005     return FALSE;
   12006 
   12007   /* Since ELF permits relocations to be against local symbols, we
   12008      must have the local symbols available when we do the relocations.
   12009      Since we would rather only read the local symbols once, and we
   12010      would rather not keep them in memory, we handle all the
   12011      relocations for a single input file at the same time.
   12012 
   12013      Unfortunately, there is no way to know the total number of local
   12014      symbols until we have seen all of them, and the local symbol
   12015      indices precede the global symbol indices.  This means that when
   12016      we are generating relocatable output, and we see a reloc against
   12017      a global symbol, we can not know the symbol index until we have
   12018      finished examining all the local symbols to see which ones we are
   12019      going to output.  To deal with this, we keep the relocations in
   12020      memory, and don't output them until the end of the link.  This is
   12021      an unfortunate waste of memory, but I don't see a good way around
   12022      it.  Fortunately, it only happens when performing a relocatable
   12023      link, which is not the common case.  FIXME: If keep_memory is set
   12024      we could write the relocs out and then read them again; I don't
   12025      know how bad the memory loss will be.  */
   12026 
   12027   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12028     sub->output_has_begun = FALSE;
   12029   for (o = abfd->sections; o != NULL; o = o->next)
   12030     {
   12031       for (p = o->map_head.link_order; p != NULL; p = p->next)
   12032 	{
   12033 	  if (p->type == bfd_indirect_link_order
   12034 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   12035 		  == bfd_target_elf_flavour)
   12036 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   12037 	    {
   12038 	      if (! sub->output_has_begun)
   12039 		{
   12040 		  if (! elf_link_input_bfd (&flinfo, sub))
   12041 		    goto error_return;
   12042 		  sub->output_has_begun = TRUE;
   12043 		}
   12044 	    }
   12045 	  else if (p->type == bfd_section_reloc_link_order
   12046 		   || p->type == bfd_symbol_reloc_link_order)
   12047 	    {
   12048 	      if (! elf_reloc_link_order (abfd, info, o, p))
   12049 		goto error_return;
   12050 	    }
   12051 	  else
   12052 	    {
   12053 	      if (! _bfd_default_link_order (abfd, info, o, p))
   12054 		{
   12055 		  if (p->type == bfd_indirect_link_order
   12056 		      && (bfd_get_flavour (sub)
   12057 			  == bfd_target_elf_flavour)
   12058 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   12059 			  != bed->s->elfclass))
   12060 		    {
   12061 		      const char *iclass, *oclass;
   12062 
   12063 		      switch (bed->s->elfclass)
   12064 			{
   12065 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
   12066 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
   12067 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
   12068 			default: abort ();
   12069 			}
   12070 
   12071 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
   12072 			{
   12073 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
   12074 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
   12075 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
   12076 			default: abort ();
   12077 			}
   12078 
   12079 		      bfd_set_error (bfd_error_wrong_format);
   12080 		      _bfd_error_handler
   12081 			/* xgettext:c-format */
   12082 			(_("%B: file class %s incompatible with %s"),
   12083 			 sub, iclass, oclass);
   12084 		    }
   12085 
   12086 		  goto error_return;
   12087 		}
   12088 	    }
   12089 	}
   12090     }
   12091 
   12092   /* Free symbol buffer if needed.  */
   12093   if (!info->reduce_memory_overheads)
   12094     {
   12095       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12096 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   12097 	    && elf_tdata (sub)->symbuf)
   12098 	  {
   12099 	    free (elf_tdata (sub)->symbuf);
   12100 	    elf_tdata (sub)->symbuf = NULL;
   12101 	  }
   12102     }
   12103 
   12104   /* Output any global symbols that got converted to local in a
   12105      version script or due to symbol visibility.  We do this in a
   12106      separate step since ELF requires all local symbols to appear
   12107      prior to any global symbols.  FIXME: We should only do this if
   12108      some global symbols were, in fact, converted to become local.
   12109      FIXME: Will this work correctly with the Irix 5 linker?  */
   12110   eoinfo.failed = FALSE;
   12111   eoinfo.flinfo = &flinfo;
   12112   eoinfo.localsyms = TRUE;
   12113   eoinfo.file_sym_done = FALSE;
   12114   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   12115   if (eoinfo.failed)
   12116     return FALSE;
   12117 
   12118   /* If backend needs to output some local symbols not present in the hash
   12119      table, do it now.  */
   12120   if (bed->elf_backend_output_arch_local_syms
   12121       && (info->strip != strip_all || emit_relocs))
   12122     {
   12123       typedef int (*out_sym_func)
   12124 	(void *, const char *, Elf_Internal_Sym *, asection *,
   12125 	 struct elf_link_hash_entry *);
   12126 
   12127       if (! ((*bed->elf_backend_output_arch_local_syms)
   12128 	     (abfd, info, &flinfo,
   12129 	      (out_sym_func) elf_link_output_symstrtab)))
   12130 	return FALSE;
   12131     }
   12132 
   12133   /* That wrote out all the local symbols.  Finish up the symbol table
   12134      with the global symbols. Even if we want to strip everything we
   12135      can, we still need to deal with those global symbols that got
   12136      converted to local in a version script.  */
   12137 
   12138   /* The sh_info field records the index of the first non local symbol.  */
   12139   symtab_hdr->sh_info = bfd_get_symcount (abfd);
   12140 
   12141   if (dynamic
   12142       && htab->dynsym != NULL
   12143       && htab->dynsym->output_section != bfd_abs_section_ptr)
   12144     {
   12145       Elf_Internal_Sym sym;
   12146       bfd_byte *dynsym = htab->dynsym->contents;
   12147 
   12148       o = htab->dynsym->output_section;
   12149       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
   12150 
   12151       /* Write out the section symbols for the output sections.  */
   12152       if (bfd_link_pic (info)
   12153 	  || htab->is_relocatable_executable)
   12154 	{
   12155 	  asection *s;
   12156 
   12157 	  sym.st_size = 0;
   12158 	  sym.st_name = 0;
   12159 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   12160 	  sym.st_other = 0;
   12161 	  sym.st_target_internal = 0;
   12162 
   12163 	  for (s = abfd->sections; s != NULL; s = s->next)
   12164 	    {
   12165 	      int indx;
   12166 	      bfd_byte *dest;
   12167 	      long dynindx;
   12168 
   12169 	      dynindx = elf_section_data (s)->dynindx;
   12170 	      if (dynindx <= 0)
   12171 		continue;
   12172 	      indx = elf_section_data (s)->this_idx;
   12173 	      BFD_ASSERT (indx > 0);
   12174 	      sym.st_shndx = indx;
   12175 	      if (! check_dynsym (abfd, &sym))
   12176 		return FALSE;
   12177 	      sym.st_value = s->vma;
   12178 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   12179 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   12180 	    }
   12181 	}
   12182 
   12183       /* Write out the local dynsyms.  */
   12184       if (htab->dynlocal)
   12185 	{
   12186 	  struct elf_link_local_dynamic_entry *e;
   12187 	  for (e = htab->dynlocal; e ; e = e->next)
   12188 	    {
   12189 	      asection *s;
   12190 	      bfd_byte *dest;
   12191 
   12192 	      /* Copy the internal symbol and turn off visibility.
   12193 		 Note that we saved a word of storage and overwrote
   12194 		 the original st_name with the dynstr_index.  */
   12195 	      sym = e->isym;
   12196 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   12197 
   12198 	      s = bfd_section_from_elf_index (e->input_bfd,
   12199 					      e->isym.st_shndx);
   12200 	      if (s != NULL)
   12201 		{
   12202 		  sym.st_shndx =
   12203 		    elf_section_data (s->output_section)->this_idx;
   12204 		  if (! check_dynsym (abfd, &sym))
   12205 		    return FALSE;
   12206 		  sym.st_value = (s->output_section->vma
   12207 				  + s->output_offset
   12208 				  + e->isym.st_value);
   12209 		}
   12210 
   12211 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   12212 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   12213 	    }
   12214 	}
   12215     }
   12216 
   12217   /* We get the global symbols from the hash table.  */
   12218   eoinfo.failed = FALSE;
   12219   eoinfo.localsyms = FALSE;
   12220   eoinfo.flinfo = &flinfo;
   12221   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   12222   if (eoinfo.failed)
   12223     return FALSE;
   12224 
   12225   /* If backend needs to output some symbols not present in the hash
   12226      table, do it now.  */
   12227   if (bed->elf_backend_output_arch_syms
   12228       && (info->strip != strip_all || emit_relocs))
   12229     {
   12230       typedef int (*out_sym_func)
   12231 	(void *, const char *, Elf_Internal_Sym *, asection *,
   12232 	 struct elf_link_hash_entry *);
   12233 
   12234       if (! ((*bed->elf_backend_output_arch_syms)
   12235 	     (abfd, info, &flinfo,
   12236 	      (out_sym_func) elf_link_output_symstrtab)))
   12237 	return FALSE;
   12238     }
   12239 
   12240   /* Finalize the .strtab section.  */
   12241   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   12242 
   12243   /* Swap out the .strtab section. */
   12244   if (!elf_link_swap_symbols_out (&flinfo))
   12245     return FALSE;
   12246 
   12247   /* Now we know the size of the symtab section.  */
   12248   if (bfd_get_symcount (abfd) > 0)
   12249     {
   12250       /* Finish up and write out the symbol string table (.strtab)
   12251 	 section.  */
   12252       Elf_Internal_Shdr *symstrtab_hdr = NULL;
   12253       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   12254 
   12255       if (elf_symtab_shndx_list (abfd))
   12256 	{
   12257 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   12258 
   12259 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   12260 	    {
   12261 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   12262 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   12263 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   12264 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   12265 	      symtab_shndx_hdr->sh_size = amt;
   12266 
   12267 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   12268 							       off, TRUE);
   12269 
   12270 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   12271 		  || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
   12272 		return FALSE;
   12273 	    }
   12274 	}
   12275 
   12276       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   12277       /* sh_name was set in prep_headers.  */
   12278       symstrtab_hdr->sh_type = SHT_STRTAB;
   12279       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   12280       symstrtab_hdr->sh_addr = 0;
   12281       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   12282       symstrtab_hdr->sh_entsize = 0;
   12283       symstrtab_hdr->sh_link = 0;
   12284       symstrtab_hdr->sh_info = 0;
   12285       /* sh_offset is set just below.  */
   12286       symstrtab_hdr->sh_addralign = 1;
   12287 
   12288       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   12289 						       off, TRUE);
   12290       elf_next_file_pos (abfd) = off;
   12291 
   12292       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   12293 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   12294 	return FALSE;
   12295     }
   12296 
   12297   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
   12298     {
   12299       _bfd_error_handler (_("%B: failed to generate import library"),
   12300 			  info->out_implib_bfd);
   12301       return FALSE;
   12302     }
   12303 
   12304   /* Adjust the relocs to have the correct symbol indices.  */
   12305   for (o = abfd->sections; o != NULL; o = o->next)
   12306     {
   12307       struct bfd_elf_section_data *esdo = elf_section_data (o);
   12308       bfd_boolean sort;
   12309 
   12310       if ((o->flags & SEC_RELOC) == 0)
   12311 	continue;
   12312 
   12313       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   12314       if (esdo->rel.hdr != NULL
   12315 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
   12316 	return FALSE;
   12317       if (esdo->rela.hdr != NULL
   12318 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
   12319 	return FALSE;
   12320 
   12321       /* Set the reloc_count field to 0 to prevent write_relocs from
   12322 	 trying to swap the relocs out itself.  */
   12323       o->reloc_count = 0;
   12324     }
   12325 
   12326   if (dynamic && info->combreloc && dynobj != NULL)
   12327     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   12328 
   12329   /* If we are linking against a dynamic object, or generating a
   12330      shared library, finish up the dynamic linking information.  */
   12331   if (dynamic)
   12332     {
   12333       bfd_byte *dyncon, *dynconend;
   12334 
   12335       /* Fix up .dynamic entries.  */
   12336       o = bfd_get_linker_section (dynobj, ".dynamic");
   12337       BFD_ASSERT (o != NULL);
   12338 
   12339       dyncon = o->contents;
   12340       dynconend = o->contents + o->size;
   12341       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   12342 	{
   12343 	  Elf_Internal_Dyn dyn;
   12344 	  const char *name;
   12345 	  unsigned int type;
   12346 	  bfd_size_type sh_size;
   12347 	  bfd_vma sh_addr;
   12348 
   12349 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   12350 
   12351 	  switch (dyn.d_tag)
   12352 	    {
   12353 	    default:
   12354 	      continue;
   12355 	    case DT_NULL:
   12356 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
   12357 		{
   12358 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   12359 		    {
   12360 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   12361 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   12362 		    default: continue;
   12363 		    }
   12364 		  dyn.d_un.d_val = relativecount;
   12365 		  relativecount = 0;
   12366 		  break;
   12367 		}
   12368 	      continue;
   12369 
   12370 	    case DT_INIT:
   12371 	      name = info->init_function;
   12372 	      goto get_sym;
   12373 	    case DT_FINI:
   12374 	      name = info->fini_function;
   12375 	    get_sym:
   12376 	      {
   12377 		struct elf_link_hash_entry *h;
   12378 
   12379 		h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   12380 		if (h != NULL
   12381 		    && (h->root.type == bfd_link_hash_defined
   12382 			|| h->root.type == bfd_link_hash_defweak))
   12383 		  {
   12384 		    dyn.d_un.d_ptr = h->root.u.def.value;
   12385 		    o = h->root.u.def.section;
   12386 		    if (o->output_section != NULL)
   12387 		      dyn.d_un.d_ptr += (o->output_section->vma
   12388 					 + o->output_offset);
   12389 		    else
   12390 		      {
   12391 			/* The symbol is imported from another shared
   12392 			   library and does not apply to this one.  */
   12393 			dyn.d_un.d_ptr = 0;
   12394 		      }
   12395 		    break;
   12396 		  }
   12397 	      }
   12398 	      continue;
   12399 
   12400 	    case DT_PREINIT_ARRAYSZ:
   12401 	      name = ".preinit_array";
   12402 	      goto get_out_size;
   12403 	    case DT_INIT_ARRAYSZ:
   12404 	      name = ".init_array";
   12405 	      goto get_out_size;
   12406 	    case DT_FINI_ARRAYSZ:
   12407 	      name = ".fini_array";
   12408 	    get_out_size:
   12409 	      o = bfd_get_section_by_name (abfd, name);
   12410 	      if (o == NULL)
   12411 		{
   12412 		  _bfd_error_handler
   12413 		    (_("could not find section %s"), name);
   12414 		  goto error_return;
   12415 		}
   12416 	      if (o->size == 0)
   12417 		_bfd_error_handler
   12418 		  (_("warning: %s section has zero size"), name);
   12419 	      dyn.d_un.d_val = o->size;
   12420 	      break;
   12421 
   12422 	    case DT_PREINIT_ARRAY:
   12423 	      name = ".preinit_array";
   12424 	      goto get_out_vma;
   12425 	    case DT_INIT_ARRAY:
   12426 	      name = ".init_array";
   12427 	      goto get_out_vma;
   12428 	    case DT_FINI_ARRAY:
   12429 	      name = ".fini_array";
   12430 	    get_out_vma:
   12431 	      o = bfd_get_section_by_name (abfd, name);
   12432 	      goto do_vma;
   12433 
   12434 	    case DT_HASH:
   12435 	      name = ".hash";
   12436 	      goto get_vma;
   12437 	    case DT_GNU_HASH:
   12438 	      name = ".gnu.hash";
   12439 	      goto get_vma;
   12440 	    case DT_STRTAB:
   12441 	      name = ".dynstr";
   12442 	      goto get_vma;
   12443 	    case DT_SYMTAB:
   12444 	      name = ".dynsym";
   12445 	      goto get_vma;
   12446 	    case DT_VERDEF:
   12447 	      name = ".gnu.version_d";
   12448 	      goto get_vma;
   12449 	    case DT_VERNEED:
   12450 	      name = ".gnu.version_r";
   12451 	      goto get_vma;
   12452 	    case DT_VERSYM:
   12453 	      name = ".gnu.version";
   12454 	    get_vma:
   12455 	      o = bfd_get_linker_section (dynobj, name);
   12456 	    do_vma:
   12457 	      if (o == NULL || bfd_is_abs_section (o->output_section))
   12458 		{
   12459 		  _bfd_error_handler
   12460 		    (_("could not find section %s"), name);
   12461 		  goto error_return;
   12462 		}
   12463 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   12464 		{
   12465 		  _bfd_error_handler
   12466 		    (_("warning: section '%s' is being made into a note"), name);
   12467 		  bfd_set_error (bfd_error_nonrepresentable_section);
   12468 		  goto error_return;
   12469 		}
   12470 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
   12471 	      break;
   12472 
   12473 	    case DT_REL:
   12474 	    case DT_RELA:
   12475 	    case DT_RELSZ:
   12476 	    case DT_RELASZ:
   12477 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   12478 		type = SHT_REL;
   12479 	      else
   12480 		type = SHT_RELA;
   12481 	      sh_size = 0;
   12482 	      sh_addr = 0;
   12483 	      for (i = 1; i < elf_numsections (abfd); i++)
   12484 		{
   12485 		  Elf_Internal_Shdr *hdr;
   12486 
   12487 		  hdr = elf_elfsections (abfd)[i];
   12488 		  if (hdr->sh_type == type
   12489 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   12490 		    {
   12491 		      sh_size += hdr->sh_size;
   12492 		      if (sh_addr == 0
   12493 			  || sh_addr > hdr->sh_addr)
   12494 			sh_addr = hdr->sh_addr;
   12495 		    }
   12496 		}
   12497 
   12498 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
   12499 		{
   12500 		  /* Don't count procedure linkage table relocs in the
   12501 		     overall reloc count.  */
   12502 		  sh_size -= htab->srelplt->size;
   12503 		  if (sh_size == 0)
   12504 		    /* If the size is zero, make the address zero too.
   12505 		       This is to avoid a glibc bug.  If the backend
   12506 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
   12507 		       zero, then we'll put DT_RELA at the end of
   12508 		       DT_JMPREL.  glibc will interpret the end of
   12509 		       DT_RELA matching the end of DT_JMPREL as the
   12510 		       case where DT_RELA includes DT_JMPREL, and for
   12511 		       LD_BIND_NOW will decide that processing DT_RELA
   12512 		       will process the PLT relocs too.  Net result:
   12513 		       No PLT relocs applied.  */
   12514 		    sh_addr = 0;
   12515 
   12516 		  /* If .rela.plt is the first .rela section, exclude
   12517 		     it from DT_RELA.  */
   12518 		  else if (sh_addr == (htab->srelplt->output_section->vma
   12519 				       + htab->srelplt->output_offset))
   12520 		    sh_addr += htab->srelplt->size;
   12521 		}
   12522 
   12523 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   12524 		dyn.d_un.d_val = sh_size;
   12525 	      else
   12526 		dyn.d_un.d_ptr = sh_addr;
   12527 	      break;
   12528 	    }
   12529 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   12530 	}
   12531     }
   12532 
   12533   /* If we have created any dynamic sections, then output them.  */
   12534   if (dynobj != NULL)
   12535     {
   12536       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   12537 	goto error_return;
   12538 
   12539       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   12540       if (((info->warn_shared_textrel && bfd_link_pic (info))
   12541 	   || info->error_textrel)
   12542 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
   12543 	{
   12544 	  bfd_byte *dyncon, *dynconend;
   12545 
   12546 	  dyncon = o->contents;
   12547 	  dynconend = o->contents + o->size;
   12548 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   12549 	    {
   12550 	      Elf_Internal_Dyn dyn;
   12551 
   12552 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   12553 
   12554 	      if (dyn.d_tag == DT_TEXTREL)
   12555 		{
   12556 		  if (info->error_textrel)
   12557 		    info->callbacks->einfo
   12558 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
   12559 		  else
   12560 		    info->callbacks->einfo
   12561 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
   12562 		  break;
   12563 		}
   12564 	    }
   12565 	}
   12566 
   12567       for (o = dynobj->sections; o != NULL; o = o->next)
   12568 	{
   12569 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   12570 	      || o->size == 0
   12571 	      || o->output_section == bfd_abs_section_ptr)
   12572 	    continue;
   12573 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   12574 	    {
   12575 	      /* At this point, we are only interested in sections
   12576 		 created by _bfd_elf_link_create_dynamic_sections.  */
   12577 	      continue;
   12578 	    }
   12579 	  if (htab->stab_info.stabstr == o)
   12580 	    continue;
   12581 	  if (htab->eh_info.hdr_sec == o)
   12582 	    continue;
   12583 	  if (strcmp (o->name, ".dynstr") != 0)
   12584 	    {
   12585 	      if (! bfd_set_section_contents (abfd, o->output_section,
   12586 					      o->contents,
   12587 					      (file_ptr) o->output_offset
   12588 					      * bfd_octets_per_byte (abfd),
   12589 					      o->size))
   12590 		goto error_return;
   12591 	    }
   12592 	  else
   12593 	    {
   12594 	      /* The contents of the .dynstr section are actually in a
   12595 		 stringtab.  */
   12596 	      file_ptr off;
   12597 
   12598 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   12599 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   12600 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
   12601 		goto error_return;
   12602 	    }
   12603 	}
   12604     }
   12605 
   12606   if (!info->resolve_section_groups)
   12607     {
   12608       bfd_boolean failed = FALSE;
   12609 
   12610       BFD_ASSERT (bfd_link_relocatable (info));
   12611       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   12612       if (failed)
   12613 	goto error_return;
   12614     }
   12615 
   12616   /* If we have optimized stabs strings, output them.  */
   12617   if (htab->stab_info.stabstr != NULL)
   12618     {
   12619       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
   12620 	goto error_return;
   12621     }
   12622 
   12623   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   12624     goto error_return;
   12625 
   12626   elf_final_link_free (abfd, &flinfo);
   12627 
   12628   elf_linker (abfd) = TRUE;
   12629 
   12630   if (attr_section)
   12631     {
   12632       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
   12633       if (contents == NULL)
   12634 	return FALSE;	/* Bail out and fail.  */
   12635       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
   12636       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
   12637       free (contents);
   12638     }
   12639 
   12640   return TRUE;
   12641 
   12642  error_return:
   12643   elf_final_link_free (abfd, &flinfo);
   12644   return FALSE;
   12645 }
   12646 
   12647 /* Initialize COOKIE for input bfd ABFD.  */
   12649 
   12650 static bfd_boolean
   12651 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   12652 		   struct bfd_link_info *info, bfd *abfd)
   12653 {
   12654   Elf_Internal_Shdr *symtab_hdr;
   12655   const struct elf_backend_data *bed;
   12656 
   12657   bed = get_elf_backend_data (abfd);
   12658   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12659 
   12660   cookie->abfd = abfd;
   12661   cookie->sym_hashes = elf_sym_hashes (abfd);
   12662   cookie->bad_symtab = elf_bad_symtab (abfd);
   12663   if (cookie->bad_symtab)
   12664     {
   12665       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   12666       cookie->extsymoff = 0;
   12667     }
   12668   else
   12669     {
   12670       cookie->locsymcount = symtab_hdr->sh_info;
   12671       cookie->extsymoff = symtab_hdr->sh_info;
   12672     }
   12673 
   12674   if (bed->s->arch_size == 32)
   12675     cookie->r_sym_shift = 8;
   12676   else
   12677     cookie->r_sym_shift = 32;
   12678 
   12679   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   12680   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   12681     {
   12682       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   12683 					      cookie->locsymcount, 0,
   12684 					      NULL, NULL, NULL);
   12685       if (cookie->locsyms == NULL)
   12686 	{
   12687 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   12688 	  return FALSE;
   12689 	}
   12690       if (info->keep_memory)
   12691 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   12692     }
   12693   return TRUE;
   12694 }
   12695 
   12696 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   12697 
   12698 static void
   12699 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   12700 {
   12701   Elf_Internal_Shdr *symtab_hdr;
   12702 
   12703   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12704   if (cookie->locsyms != NULL
   12705       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   12706     free (cookie->locsyms);
   12707 }
   12708 
   12709 /* Initialize the relocation information in COOKIE for input section SEC
   12710    of input bfd ABFD.  */
   12711 
   12712 static bfd_boolean
   12713 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   12714 			struct bfd_link_info *info, bfd *abfd,
   12715 			asection *sec)
   12716 {
   12717   if (sec->reloc_count == 0)
   12718     {
   12719       cookie->rels = NULL;
   12720       cookie->relend = NULL;
   12721     }
   12722   else
   12723     {
   12724       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   12725 						info->keep_memory);
   12726       if (cookie->rels == NULL)
   12727 	return FALSE;
   12728       cookie->rel = cookie->rels;
   12729       cookie->relend = cookie->rels + sec->reloc_count;
   12730     }
   12731   cookie->rel = cookie->rels;
   12732   return TRUE;
   12733 }
   12734 
   12735 /* Free the memory allocated by init_reloc_cookie_rels,
   12736    if appropriate.  */
   12737 
   12738 static void
   12739 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   12740 			asection *sec)
   12741 {
   12742   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
   12743     free (cookie->rels);
   12744 }
   12745 
   12746 /* Initialize the whole of COOKIE for input section SEC.  */
   12747 
   12748 static bfd_boolean
   12749 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   12750 			       struct bfd_link_info *info,
   12751 			       asection *sec)
   12752 {
   12753   if (!init_reloc_cookie (cookie, info, sec->owner))
   12754     goto error1;
   12755   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   12756     goto error2;
   12757   return TRUE;
   12758 
   12759  error2:
   12760   fini_reloc_cookie (cookie, sec->owner);
   12761  error1:
   12762   return FALSE;
   12763 }
   12764 
   12765 /* Free the memory allocated by init_reloc_cookie_for_section,
   12766    if appropriate.  */
   12767 
   12768 static void
   12769 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   12770 			       asection *sec)
   12771 {
   12772   fini_reloc_cookie_rels (cookie, sec);
   12773   fini_reloc_cookie (cookie, sec->owner);
   12774 }
   12775 
   12776 /* Garbage collect unused sections.  */
   12778 
   12779 /* Default gc_mark_hook.  */
   12780 
   12781 asection *
   12782 _bfd_elf_gc_mark_hook (asection *sec,
   12783 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12784 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   12785 		       struct elf_link_hash_entry *h,
   12786 		       Elf_Internal_Sym *sym)
   12787 {
   12788   if (h != NULL)
   12789     {
   12790       switch (h->root.type)
   12791 	{
   12792 	case bfd_link_hash_defined:
   12793 	case bfd_link_hash_defweak:
   12794 	  return h->root.u.def.section;
   12795 
   12796 	case bfd_link_hash_common:
   12797 	  return h->root.u.c.p->section;
   12798 
   12799 	default:
   12800 	  break;
   12801 	}
   12802     }
   12803   else
   12804     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   12805 
   12806   return NULL;
   12807 }
   12808 
   12809 /* Return the global debug definition section.  */
   12810 
   12811 static asection *
   12812 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
   12813 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12814 			   Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   12815 			   struct elf_link_hash_entry *h,
   12816 			   Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   12817 {
   12818   if (h != NULL
   12819       && (h->root.type == bfd_link_hash_defined
   12820 	  || h->root.type == bfd_link_hash_defweak)
   12821       && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
   12822     return h->root.u.def.section;
   12823 
   12824   return NULL;
   12825 }
   12826 
   12827 /* COOKIE->rel describes a relocation against section SEC, which is
   12828    a section we've decided to keep.  Return the section that contains
   12829    the relocation symbol, or NULL if no section contains it.  */
   12830 
   12831 asection *
   12832 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   12833 		       elf_gc_mark_hook_fn gc_mark_hook,
   12834 		       struct elf_reloc_cookie *cookie,
   12835 		       bfd_boolean *start_stop)
   12836 {
   12837   unsigned long r_symndx;
   12838   struct elf_link_hash_entry *h;
   12839 
   12840   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   12841   if (r_symndx == STN_UNDEF)
   12842     return NULL;
   12843 
   12844   if (r_symndx >= cookie->locsymcount
   12845       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   12846     {
   12847       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
   12848       if (h == NULL)
   12849 	{
   12850 	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
   12851 				  sec->owner);
   12852 	  return NULL;
   12853 	}
   12854       while (h->root.type == bfd_link_hash_indirect
   12855 	     || h->root.type == bfd_link_hash_warning)
   12856 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   12857       h->mark = 1;
   12858       /* If this symbol is weak and there is a non-weak definition, we
   12859 	 keep the non-weak definition because many backends put
   12860 	 dynamic reloc info on the non-weak definition for code
   12861 	 handling copy relocs.  */
   12862       if (h->is_weakalias)
   12863 	weakdef (h)->mark = 1;
   12864 
   12865       if (start_stop != NULL)
   12866 	{
   12867 	  /* To work around a glibc bug, mark XXX input sections
   12868 	     when there is a reference to __start_XXX or __stop_XXX
   12869 	     symbols.  */
   12870 	  if (h->start_stop)
   12871 	    {
   12872 	      asection *s = h->u2.start_stop_section;
   12873 	      *start_stop = !s->gc_mark;
   12874 	      return s;
   12875 	    }
   12876 	}
   12877 
   12878       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   12879     }
   12880 
   12881   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   12882 			  &cookie->locsyms[r_symndx]);
   12883 }
   12884 
   12885 /* COOKIE->rel describes a relocation against section SEC, which is
   12886    a section we've decided to keep.  Mark the section that contains
   12887    the relocation symbol.  */
   12888 
   12889 bfd_boolean
   12890 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   12891 			asection *sec,
   12892 			elf_gc_mark_hook_fn gc_mark_hook,
   12893 			struct elf_reloc_cookie *cookie)
   12894 {
   12895   asection *rsec;
   12896   bfd_boolean start_stop = FALSE;
   12897 
   12898   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   12899   while (rsec != NULL)
   12900     {
   12901       if (!rsec->gc_mark)
   12902 	{
   12903 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   12904 	      || (rsec->owner->flags & DYNAMIC) != 0)
   12905 	    rsec->gc_mark = 1;
   12906 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   12907 	    return FALSE;
   12908 	}
   12909       if (!start_stop)
   12910 	break;
   12911       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   12912     }
   12913   return TRUE;
   12914 }
   12915 
   12916 /* The mark phase of garbage collection.  For a given section, mark
   12917    it and any sections in this section's group, and all the sections
   12918    which define symbols to which it refers.  */
   12919 
   12920 bfd_boolean
   12921 _bfd_elf_gc_mark (struct bfd_link_info *info,
   12922 		  asection *sec,
   12923 		  elf_gc_mark_hook_fn gc_mark_hook)
   12924 {
   12925   bfd_boolean ret;
   12926   asection *group_sec, *eh_frame;
   12927 
   12928   sec->gc_mark = 1;
   12929 
   12930   /* Mark all the sections in the group.  */
   12931   group_sec = elf_section_data (sec)->next_in_group;
   12932   if (group_sec && !group_sec->gc_mark)
   12933     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   12934       return FALSE;
   12935 
   12936   /* Look through the section relocs.  */
   12937   ret = TRUE;
   12938   eh_frame = elf_eh_frame_section (sec->owner);
   12939   if ((sec->flags & SEC_RELOC) != 0
   12940       && sec->reloc_count > 0
   12941       && sec != eh_frame)
   12942     {
   12943       struct elf_reloc_cookie cookie;
   12944 
   12945       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   12946 	ret = FALSE;
   12947       else
   12948 	{
   12949 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   12950 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   12951 	      {
   12952 		ret = FALSE;
   12953 		break;
   12954 	      }
   12955 	  fini_reloc_cookie_for_section (&cookie, sec);
   12956 	}
   12957     }
   12958 
   12959   if (ret && eh_frame && elf_fde_list (sec))
   12960     {
   12961       struct elf_reloc_cookie cookie;
   12962 
   12963       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
   12964 	ret = FALSE;
   12965       else
   12966 	{
   12967 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   12968 				      gc_mark_hook, &cookie))
   12969 	    ret = FALSE;
   12970 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   12971 	}
   12972     }
   12973 
   12974   eh_frame = elf_section_eh_frame_entry (sec);
   12975   if (ret && eh_frame && !eh_frame->gc_mark)
   12976     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   12977       ret = FALSE;
   12978 
   12979   return ret;
   12980 }
   12981 
   12982 /* Scan and mark sections in a special or debug section group.  */
   12983 
   12984 static void
   12985 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   12986 {
   12987   /* Point to first section of section group.  */
   12988   asection *ssec;
   12989   /* Used to iterate the section group.  */
   12990   asection *msec;
   12991 
   12992   bfd_boolean is_special_grp = TRUE;
   12993   bfd_boolean is_debug_grp = TRUE;
   12994 
   12995   /* First scan to see if group contains any section other than debug
   12996      and special section.  */
   12997   ssec = msec = elf_next_in_group (grp);
   12998   do
   12999     {
   13000       if ((msec->flags & SEC_DEBUGGING) == 0)
   13001 	is_debug_grp = FALSE;
   13002 
   13003       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   13004 	is_special_grp = FALSE;
   13005 
   13006       msec = elf_next_in_group (msec);
   13007     }
   13008   while (msec != ssec);
   13009 
   13010   /* If this is a pure debug section group or pure special section group,
   13011      keep all sections in this group.  */
   13012   if (is_debug_grp || is_special_grp)
   13013     {
   13014       do
   13015 	{
   13016 	  msec->gc_mark = 1;
   13017 	  msec = elf_next_in_group (msec);
   13018 	}
   13019       while (msec != ssec);
   13020     }
   13021 }
   13022 
   13023 /* Keep debug and special sections.  */
   13024 
   13025 bfd_boolean
   13026 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   13027 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
   13028 {
   13029   bfd *ibfd;
   13030 
   13031   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   13032     {
   13033       asection *isec;
   13034       bfd_boolean some_kept;
   13035       bfd_boolean debug_frag_seen;
   13036       bfd_boolean has_kept_debug_info;
   13037 
   13038       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   13039 	continue;
   13040       isec = ibfd->sections;
   13041       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13042 	continue;
   13043 
   13044       /* Ensure all linker created sections are kept,
   13045 	 see if any other section is already marked,
   13046 	 and note if we have any fragmented debug sections.  */
   13047       debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
   13048       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13049 	{
   13050 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   13051 	    isec->gc_mark = 1;
   13052 	  else if (isec->gc_mark
   13053 		   && (isec->flags & SEC_ALLOC) != 0
   13054 		   && elf_section_type (isec) != SHT_NOTE)
   13055 	    some_kept = TRUE;
   13056 
   13057 	  if (!debug_frag_seen
   13058 	      && (isec->flags & SEC_DEBUGGING)
   13059 	      && CONST_STRNEQ (isec->name, ".debug_line."))
   13060 	    debug_frag_seen = TRUE;
   13061 	}
   13062 
   13063       /* If no non-note alloc section in this file will be kept, then
   13064 	 we can toss out the debug and special sections.  */
   13065       if (!some_kept)
   13066 	continue;
   13067 
   13068       /* Keep debug and special sections like .comment when they are
   13069 	 not part of a group.  Also keep section groups that contain
   13070 	 just debug sections or special sections.  */
   13071       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13072 	{
   13073 	  if ((isec->flags & SEC_GROUP) != 0)
   13074 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   13075 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   13076 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   13077 		   && elf_next_in_group (isec) == NULL)
   13078 	    isec->gc_mark = 1;
   13079 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
   13080 	    has_kept_debug_info = TRUE;
   13081 	}
   13082 
   13083       /* Look for CODE sections which are going to be discarded,
   13084 	 and find and discard any fragmented debug sections which
   13085 	 are associated with that code section.  */
   13086       if (debug_frag_seen)
   13087 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13088 	  if ((isec->flags & SEC_CODE) != 0
   13089 	      && isec->gc_mark == 0)
   13090 	    {
   13091 	      unsigned int ilen;
   13092 	      asection *dsec;
   13093 
   13094 	      ilen = strlen (isec->name);
   13095 
   13096 	      /* Association is determined by the name of the debug
   13097 		 section containing the name of the code section as
   13098 		 a suffix.  For example .debug_line.text.foo is a
   13099 		 debug section associated with .text.foo.  */
   13100 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   13101 		{
   13102 		  unsigned int dlen;
   13103 
   13104 		  if (dsec->gc_mark == 0
   13105 		      || (dsec->flags & SEC_DEBUGGING) == 0)
   13106 		    continue;
   13107 
   13108 		  dlen = strlen (dsec->name);
   13109 
   13110 		  if (dlen > ilen
   13111 		      && strncmp (dsec->name + (dlen - ilen),
   13112 				  isec->name, ilen) == 0)
   13113 		    dsec->gc_mark = 0;
   13114 		}
   13115 	  }
   13116 
   13117       /* Mark debug sections referenced by kept debug sections.  */
   13118       if (has_kept_debug_info)
   13119 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   13120 	  if (isec->gc_mark
   13121 	      && (isec->flags & SEC_DEBUGGING) != 0)
   13122 	    if (!_bfd_elf_gc_mark (info, isec,
   13123 				   elf_gc_mark_debug_section))
   13124 	      return FALSE;
   13125     }
   13126   return TRUE;
   13127 }
   13128 
   13129 static bfd_boolean
   13130 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   13131 {
   13132   bfd *sub;
   13133   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13134 
   13135   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13136     {
   13137       asection *o;
   13138 
   13139       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   13140 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
   13141 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   13142 	continue;
   13143       o = sub->sections;
   13144       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13145 	continue;
   13146 
   13147       for (o = sub->sections; o != NULL; o = o->next)
   13148 	{
   13149 	  /* When any section in a section group is kept, we keep all
   13150 	     sections in the section group.  If the first member of
   13151 	     the section group is excluded, we will also exclude the
   13152 	     group section.  */
   13153 	  if (o->flags & SEC_GROUP)
   13154 	    {
   13155 	      asection *first = elf_next_in_group (o);
   13156 	      o->gc_mark = first->gc_mark;
   13157 	    }
   13158 
   13159 	  if (o->gc_mark)
   13160 	    continue;
   13161 
   13162 	  /* Skip sweeping sections already excluded.  */
   13163 	  if (o->flags & SEC_EXCLUDE)
   13164 	    continue;
   13165 
   13166 	  /* Since this is early in the link process, it is simple
   13167 	     to remove a section from the output.  */
   13168 	  o->flags |= SEC_EXCLUDE;
   13169 
   13170 	  if (info->print_gc_sections && o->size != 0)
   13171 	    /* xgettext:c-format */
   13172 	    _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
   13173 				o, sub);
   13174 	}
   13175     }
   13176 
   13177   return TRUE;
   13178 }
   13179 
   13180 /* Propagate collected vtable information.  This is called through
   13181    elf_link_hash_traverse.  */
   13182 
   13183 static bfd_boolean
   13184 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   13185 {
   13186   /* Those that are not vtables.  */
   13187   if (h->start_stop
   13188       || h->u2.vtable == NULL
   13189       || h->u2.vtable->parent == NULL)
   13190     return TRUE;
   13191 
   13192   /* Those vtables that do not have parents, we cannot merge.  */
   13193   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
   13194     return TRUE;
   13195 
   13196   /* If we've already been done, exit.  */
   13197   if (h->u2.vtable->used && h->u2.vtable->used[-1])
   13198     return TRUE;
   13199 
   13200   /* Make sure the parent's table is up to date.  */
   13201   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
   13202 
   13203   if (h->u2.vtable->used == NULL)
   13204     {
   13205       /* None of this table's entries were referenced.  Re-use the
   13206 	 parent's table.  */
   13207       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
   13208       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
   13209     }
   13210   else
   13211     {
   13212       size_t n;
   13213       bfd_boolean *cu, *pu;
   13214 
   13215       /* Or the parent's entries into ours.  */
   13216       cu = h->u2.vtable->used;
   13217       cu[-1] = TRUE;
   13218       pu = h->u2.vtable->parent->u2.vtable->used;
   13219       if (pu != NULL)
   13220 	{
   13221 	  const struct elf_backend_data *bed;
   13222 	  unsigned int log_file_align;
   13223 
   13224 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   13225 	  log_file_align = bed->s->log_file_align;
   13226 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
   13227 	  while (n--)
   13228 	    {
   13229 	      if (*pu)
   13230 		*cu = TRUE;
   13231 	      pu++;
   13232 	      cu++;
   13233 	    }
   13234 	}
   13235     }
   13236 
   13237   return TRUE;
   13238 }
   13239 
   13240 static bfd_boolean
   13241 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
   13242 {
   13243   asection *sec;
   13244   bfd_vma hstart, hend;
   13245   Elf_Internal_Rela *relstart, *relend, *rel;
   13246   const struct elf_backend_data *bed;
   13247   unsigned int log_file_align;
   13248 
   13249   /* Take care of both those symbols that do not describe vtables as
   13250      well as those that are not loaded.  */
   13251   if (h->start_stop
   13252       || h->u2.vtable == NULL
   13253       || h->u2.vtable->parent == NULL)
   13254     return TRUE;
   13255 
   13256   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   13257 	      || h->root.type == bfd_link_hash_defweak);
   13258 
   13259   sec = h->root.u.def.section;
   13260   hstart = h->root.u.def.value;
   13261   hend = hstart + h->size;
   13262 
   13263   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
   13264   if (!relstart)
   13265     return *(bfd_boolean *) okp = FALSE;
   13266   bed = get_elf_backend_data (sec->owner);
   13267   log_file_align = bed->s->log_file_align;
   13268 
   13269   relend = relstart + sec->reloc_count;
   13270 
   13271   for (rel = relstart; rel < relend; ++rel)
   13272     if (rel->r_offset >= hstart && rel->r_offset < hend)
   13273       {
   13274 	/* If the entry is in use, do nothing.  */
   13275 	if (h->u2.vtable->used
   13276 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
   13277 	  {
   13278 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   13279 	    if (h->u2.vtable->used[entry])
   13280 	      continue;
   13281 	  }
   13282 	/* Otherwise, kill it.  */
   13283 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   13284       }
   13285 
   13286   return TRUE;
   13287 }
   13288 
   13289 /* Mark sections containing dynamically referenced symbols.  When
   13290    building shared libraries, we must assume that any visible symbol is
   13291    referenced.  */
   13292 
   13293 bfd_boolean
   13294 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   13295 {
   13296   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   13297   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   13298 
   13299   if ((h->root.type == bfd_link_hash_defined
   13300        || h->root.type == bfd_link_hash_defweak)
   13301       && ((h->ref_dynamic && !h->forced_local)
   13302 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   13303 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   13304 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   13305 	      && (!bfd_link_executable (info)
   13306 		  || info->gc_keep_exported
   13307 		  || info->export_dynamic
   13308 		  || (h->dynamic
   13309 		      && d != NULL
   13310 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   13311 	      && (h->versioned >= versioned
   13312 		  || !bfd_hide_sym_by_version (info->version_info,
   13313 					       h->root.root.string)))))
   13314     h->root.u.def.section->flags |= SEC_KEEP;
   13315 
   13316   return TRUE;
   13317 }
   13318 
   13319 /* Keep all sections containing symbols undefined on the command-line,
   13320    and the section containing the entry symbol.  */
   13321 
   13322 void
   13323 _bfd_elf_gc_keep (struct bfd_link_info *info)
   13324 {
   13325   struct bfd_sym_chain *sym;
   13326 
   13327   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   13328     {
   13329       struct elf_link_hash_entry *h;
   13330 
   13331       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   13332 				FALSE, FALSE, FALSE);
   13333 
   13334       if (h != NULL
   13335 	  && (h->root.type == bfd_link_hash_defined
   13336 	      || h->root.type == bfd_link_hash_defweak)
   13337 	  && !bfd_is_abs_section (h->root.u.def.section)
   13338 	  && !bfd_is_und_section (h->root.u.def.section))
   13339 	h->root.u.def.section->flags |= SEC_KEEP;
   13340     }
   13341 }
   13342 
   13343 bfd_boolean
   13344 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   13345 				struct bfd_link_info *info)
   13346 {
   13347   bfd *ibfd = info->input_bfds;
   13348 
   13349   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   13350     {
   13351       asection *sec;
   13352       struct elf_reloc_cookie cookie;
   13353 
   13354       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   13355 	continue;
   13356       sec = ibfd->sections;
   13357       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13358 	continue;
   13359 
   13360       if (!init_reloc_cookie (&cookie, info, ibfd))
   13361 	return FALSE;
   13362 
   13363       for (sec = ibfd->sections; sec; sec = sec->next)
   13364 	{
   13365 	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
   13366 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
   13367 	    {
   13368 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   13369 	      fini_reloc_cookie_rels (&cookie, sec);
   13370 	    }
   13371 	}
   13372     }
   13373   return TRUE;
   13374 }
   13375 
   13376 /* Do mark and sweep of unused sections.  */
   13377 
   13378 bfd_boolean
   13379 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   13380 {
   13381   bfd_boolean ok = TRUE;
   13382   bfd *sub;
   13383   elf_gc_mark_hook_fn gc_mark_hook;
   13384   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13385   struct elf_link_hash_table *htab;
   13386 
   13387   if (!bed->can_gc_sections
   13388       || !is_elf_hash_table (info->hash))
   13389     {
   13390       _bfd_error_handler(_("Warning: gc-sections option ignored"));
   13391       return TRUE;
   13392     }
   13393 
   13394   bed->gc_keep (info);
   13395   htab = elf_hash_table (info);
   13396 
   13397   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   13398      at the .eh_frame section if we can mark the FDEs individually.  */
   13399   for (sub = info->input_bfds;
   13400        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   13401        sub = sub->link.next)
   13402     {
   13403       asection *sec;
   13404       struct elf_reloc_cookie cookie;
   13405 
   13406       sec = sub->sections;
   13407       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13408 	continue;
   13409       sec = bfd_get_section_by_name (sub, ".eh_frame");
   13410       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
   13411 	{
   13412 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   13413 	  if (elf_section_data (sec)->sec_info
   13414 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   13415 	    elf_eh_frame_section (sub) = sec;
   13416 	  fini_reloc_cookie_for_section (&cookie, sec);
   13417 	  sec = bfd_get_next_section_by_name (NULL, sec);
   13418 	}
   13419     }
   13420 
   13421   /* Apply transitive closure to the vtable entry usage info.  */
   13422   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   13423   if (!ok)
   13424     return FALSE;
   13425 
   13426   /* Kill the vtable relocations that were not used.  */
   13427   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
   13428   if (!ok)
   13429     return FALSE;
   13430 
   13431   /* Mark dynamically referenced symbols.  */
   13432   if (htab->dynamic_sections_created || info->gc_keep_exported)
   13433     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   13434 
   13435   /* Grovel through relocs to find out who stays ...  */
   13436   gc_mark_hook = bed->gc_mark_hook;
   13437   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13438     {
   13439       asection *o;
   13440 
   13441       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   13442 	  || elf_object_id (sub) != elf_hash_table_id (htab)
   13443 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   13444 	continue;
   13445 
   13446       o = sub->sections;
   13447       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   13448 	continue;
   13449 
   13450       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   13451 	 Also treat note sections as a root, if the section is not part
   13452 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
   13453 	 well as FINI_ARRAY sections for ld -r.  */
   13454       for (o = sub->sections; o != NULL; o = o->next)
   13455 	if (!o->gc_mark
   13456 	    && (o->flags & SEC_EXCLUDE) == 0
   13457 	    && ((o->flags & SEC_KEEP) != 0
   13458 		|| (bfd_link_relocatable (info)
   13459 		    && ((elf_section_data (o)->this_hdr.sh_type
   13460 			 == SHT_PREINIT_ARRAY)
   13461 			|| (elf_section_data (o)->this_hdr.sh_type
   13462 			    == SHT_INIT_ARRAY)
   13463 			|| (elf_section_data (o)->this_hdr.sh_type
   13464 			    == SHT_FINI_ARRAY)))
   13465 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   13466 		    && elf_next_in_group (o) == NULL )))
   13467 	  {
   13468 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   13469 	      return FALSE;
   13470 	  }
   13471     }
   13472 
   13473   /* Allow the backend to mark additional target specific sections.  */
   13474   bed->gc_mark_extra_sections (info, gc_mark_hook);
   13475 
   13476   /* ... and mark SEC_EXCLUDE for those that go.  */
   13477   return elf_gc_sweep (abfd, info);
   13478 }
   13479 
   13480 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   13482 
   13483 bfd_boolean
   13484 bfd_elf_gc_record_vtinherit (bfd *abfd,
   13485 			     asection *sec,
   13486 			     struct elf_link_hash_entry *h,
   13487 			     bfd_vma offset)
   13488 {
   13489   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   13490   struct elf_link_hash_entry **search, *child;
   13491   size_t extsymcount;
   13492   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13493 
   13494   /* The sh_info field of the symtab header tells us where the
   13495      external symbols start.  We don't care about the local symbols at
   13496      this point.  */
   13497   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   13498   if (!elf_bad_symtab (abfd))
   13499     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   13500 
   13501   sym_hashes = elf_sym_hashes (abfd);
   13502   sym_hashes_end = sym_hashes + extsymcount;
   13503 
   13504   /* Hunt down the child symbol, which is in this section at the same
   13505      offset as the relocation.  */
   13506   for (search = sym_hashes; search != sym_hashes_end; ++search)
   13507     {
   13508       if ((child = *search) != NULL
   13509 	  && (child->root.type == bfd_link_hash_defined
   13510 	      || child->root.type == bfd_link_hash_defweak)
   13511 	  && child->root.u.def.section == sec
   13512 	  && child->root.u.def.value == offset)
   13513 	goto win;
   13514     }
   13515 
   13516   /* xgettext:c-format */
   13517   _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
   13518 		      abfd, sec, offset);
   13519   bfd_set_error (bfd_error_invalid_operation);
   13520   return FALSE;
   13521 
   13522  win:
   13523   if (!child->u2.vtable)
   13524     {
   13525       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
   13526 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
   13527       if (!child->u2.vtable)
   13528 	return FALSE;
   13529     }
   13530   if (!h)
   13531     {
   13532       /* This *should* only be the absolute section.  It could potentially
   13533 	 be that someone has defined a non-global vtable though, which
   13534 	 would be bad.  It isn't worth paging in the local symbols to be
   13535 	 sure though; that case should simply be handled by the assembler.  */
   13536 
   13537       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
   13538     }
   13539   else
   13540     child->u2.vtable->parent = h;
   13541 
   13542   return TRUE;
   13543 }
   13544 
   13545 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   13546 
   13547 bfd_boolean
   13548 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
   13549 			   asection *sec ATTRIBUTE_UNUSED,
   13550 			   struct elf_link_hash_entry *h,
   13551 			   bfd_vma addend)
   13552 {
   13553   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13554   unsigned int log_file_align = bed->s->log_file_align;
   13555 
   13556   if (!h->u2.vtable)
   13557     {
   13558       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
   13559 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
   13560       if (!h->u2.vtable)
   13561 	return FALSE;
   13562     }
   13563 
   13564   if (addend >= h->u2.vtable->size)
   13565     {
   13566       size_t size, bytes, file_align;
   13567       bfd_boolean *ptr = h->u2.vtable->used;
   13568 
   13569       /* While the symbol is undefined, we have to be prepared to handle
   13570 	 a zero size.  */
   13571       file_align = 1 << log_file_align;
   13572       if (h->root.type == bfd_link_hash_undefined)
   13573 	size = addend + file_align;
   13574       else
   13575 	{
   13576 	  size = h->size;
   13577 	  if (addend >= size)
   13578 	    {
   13579 	      /* Oops!  We've got a reference past the defined end of
   13580 		 the table.  This is probably a bug -- shall we warn?  */
   13581 	      size = addend + file_align;
   13582 	    }
   13583 	}
   13584       size = (size + file_align - 1) & -file_align;
   13585 
   13586       /* Allocate one extra entry for use as a "done" flag for the
   13587 	 consolidation pass.  */
   13588       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
   13589 
   13590       if (ptr)
   13591 	{
   13592 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
   13593 
   13594 	  if (ptr != NULL)
   13595 	    {
   13596 	      size_t oldbytes;
   13597 
   13598 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
   13599 			  * sizeof (bfd_boolean));
   13600 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   13601 	    }
   13602 	}
   13603       else
   13604 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
   13605 
   13606       if (ptr == NULL)
   13607 	return FALSE;
   13608 
   13609       /* And arrange for that done flag to be at index -1.  */
   13610       h->u2.vtable->used = ptr + 1;
   13611       h->u2.vtable->size = size;
   13612     }
   13613 
   13614   h->u2.vtable->used[addend >> log_file_align] = TRUE;
   13615 
   13616   return TRUE;
   13617 }
   13618 
   13619 /* Map an ELF section header flag to its corresponding string.  */
   13620 typedef struct
   13621 {
   13622   char *flag_name;
   13623   flagword flag_value;
   13624 } elf_flags_to_name_table;
   13625 
   13626 static elf_flags_to_name_table elf_flags_to_names [] =
   13627 {
   13628   { "SHF_WRITE", SHF_WRITE },
   13629   { "SHF_ALLOC", SHF_ALLOC },
   13630   { "SHF_EXECINSTR", SHF_EXECINSTR },
   13631   { "SHF_MERGE", SHF_MERGE },
   13632   { "SHF_STRINGS", SHF_STRINGS },
   13633   { "SHF_INFO_LINK", SHF_INFO_LINK},
   13634   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   13635   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   13636   { "SHF_GROUP", SHF_GROUP },
   13637   { "SHF_TLS", SHF_TLS },
   13638   { "SHF_MASKOS", SHF_MASKOS },
   13639   { "SHF_EXCLUDE", SHF_EXCLUDE },
   13640 };
   13641 
   13642 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   13643 bfd_boolean
   13644 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   13645 			      struct flag_info *flaginfo,
   13646 			      asection *section)
   13647 {
   13648   const bfd_vma sh_flags = elf_section_flags (section);
   13649 
   13650   if (!flaginfo->flags_initialized)
   13651     {
   13652       bfd *obfd = info->output_bfd;
   13653       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   13654       struct flag_info_list *tf = flaginfo->flag_list;
   13655       int with_hex = 0;
   13656       int without_hex = 0;
   13657 
   13658       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   13659 	{
   13660 	  unsigned i;
   13661 	  flagword (*lookup) (char *);
   13662 
   13663 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   13664 	  if (lookup != NULL)
   13665 	    {
   13666 	      flagword hexval = (*lookup) ((char *) tf->name);
   13667 
   13668 	      if (hexval != 0)
   13669 		{
   13670 		  if (tf->with == with_flags)
   13671 		    with_hex |= hexval;
   13672 		  else if (tf->with == without_flags)
   13673 		    without_hex |= hexval;
   13674 		  tf->valid = TRUE;
   13675 		  continue;
   13676 		}
   13677 	    }
   13678 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   13679 	    {
   13680 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   13681 		{
   13682 		  if (tf->with == with_flags)
   13683 		    with_hex |= elf_flags_to_names[i].flag_value;
   13684 		  else if (tf->with == without_flags)
   13685 		    without_hex |= elf_flags_to_names[i].flag_value;
   13686 		  tf->valid = TRUE;
   13687 		  break;
   13688 		}
   13689 	    }
   13690 	  if (!tf->valid)
   13691 	    {
   13692 	      info->callbacks->einfo
   13693 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   13694 	      return FALSE;
   13695 	    }
   13696 	}
   13697       flaginfo->flags_initialized = TRUE;
   13698       flaginfo->only_with_flags |= with_hex;
   13699       flaginfo->not_with_flags |= without_hex;
   13700     }
   13701 
   13702   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   13703     return FALSE;
   13704 
   13705   if ((flaginfo->not_with_flags & sh_flags) != 0)
   13706     return FALSE;
   13707 
   13708   return TRUE;
   13709 }
   13710 
   13711 struct alloc_got_off_arg {
   13712   bfd_vma gotoff;
   13713   struct bfd_link_info *info;
   13714 };
   13715 
   13716 /* We need a special top-level link routine to convert got reference counts
   13717    to real got offsets.  */
   13718 
   13719 static bfd_boolean
   13720 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   13721 {
   13722   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   13723   bfd *obfd = gofarg->info->output_bfd;
   13724   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   13725 
   13726   if (h->got.refcount > 0)
   13727     {
   13728       h->got.offset = gofarg->gotoff;
   13729       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   13730     }
   13731   else
   13732     h->got.offset = (bfd_vma) -1;
   13733 
   13734   return TRUE;
   13735 }
   13736 
   13737 /* And an accompanying bit to work out final got entry offsets once
   13738    we're done.  Should be called from final_link.  */
   13739 
   13740 bfd_boolean
   13741 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   13742 					struct bfd_link_info *info)
   13743 {
   13744   bfd *i;
   13745   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13746   bfd_vma gotoff;
   13747   struct alloc_got_off_arg gofarg;
   13748 
   13749   BFD_ASSERT (abfd == info->output_bfd);
   13750 
   13751   if (! is_elf_hash_table (info->hash))
   13752     return FALSE;
   13753 
   13754   /* The GOT offset is relative to the .got section, but the GOT header is
   13755      put into the .got.plt section, if the backend uses it.  */
   13756   if (bed->want_got_plt)
   13757     gotoff = 0;
   13758   else
   13759     gotoff = bed->got_header_size;
   13760 
   13761   /* Do the local .got entries first.  */
   13762   for (i = info->input_bfds; i; i = i->link.next)
   13763     {
   13764       bfd_signed_vma *local_got;
   13765       size_t j, locsymcount;
   13766       Elf_Internal_Shdr *symtab_hdr;
   13767 
   13768       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   13769 	continue;
   13770 
   13771       local_got = elf_local_got_refcounts (i);
   13772       if (!local_got)
   13773 	continue;
   13774 
   13775       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   13776       if (elf_bad_symtab (i))
   13777 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   13778       else
   13779 	locsymcount = symtab_hdr->sh_info;
   13780 
   13781       for (j = 0; j < locsymcount; ++j)
   13782 	{
   13783 	  if (local_got[j] > 0)
   13784 	    {
   13785 	      local_got[j] = gotoff;
   13786 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   13787 	    }
   13788 	  else
   13789 	    local_got[j] = (bfd_vma) -1;
   13790 	}
   13791     }
   13792 
   13793   /* Then the global .got entries.  .plt refcounts are handled by
   13794      adjust_dynamic_symbol  */
   13795   gofarg.gotoff = gotoff;
   13796   gofarg.info = info;
   13797   elf_link_hash_traverse (elf_hash_table (info),
   13798 			  elf_gc_allocate_got_offsets,
   13799 			  &gofarg);
   13800   return TRUE;
   13801 }
   13802 
   13803 /* Many folk need no more in the way of final link than this, once
   13804    got entry reference counting is enabled.  */
   13805 
   13806 bfd_boolean
   13807 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   13808 {
   13809   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   13810     return FALSE;
   13811 
   13812   /* Invoke the regular ELF backend linker to do all the work.  */
   13813   return bfd_elf_final_link (abfd, info);
   13814 }
   13815 
   13816 bfd_boolean
   13817 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   13818 {
   13819   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   13820 
   13821   if (rcookie->bad_symtab)
   13822     rcookie->rel = rcookie->rels;
   13823 
   13824   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   13825     {
   13826       unsigned long r_symndx;
   13827 
   13828       if (! rcookie->bad_symtab)
   13829 	if (rcookie->rel->r_offset > offset)
   13830 	  return FALSE;
   13831       if (rcookie->rel->r_offset != offset)
   13832 	continue;
   13833 
   13834       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   13835       if (r_symndx == STN_UNDEF)
   13836 	return TRUE;
   13837 
   13838       if (r_symndx >= rcookie->locsymcount
   13839 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   13840 	{
   13841 	  struct elf_link_hash_entry *h;
   13842 
   13843 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
   13844 
   13845 	  while (h->root.type == bfd_link_hash_indirect
   13846 		 || h->root.type == bfd_link_hash_warning)
   13847 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   13848 
   13849 	  if ((h->root.type == bfd_link_hash_defined
   13850 	       || h->root.type == bfd_link_hash_defweak)
   13851 	      && (h->root.u.def.section->owner != rcookie->abfd
   13852 		  || h->root.u.def.section->kept_section != NULL
   13853 		  || discarded_section (h->root.u.def.section)))
   13854 	    return TRUE;
   13855 	}
   13856       else
   13857 	{
   13858 	  /* It's not a relocation against a global symbol,
   13859 	     but it could be a relocation against a local
   13860 	     symbol for a discarded section.  */
   13861 	  asection *isec;
   13862 	  Elf_Internal_Sym *isym;
   13863 
   13864 	  /* Need to: get the symbol; get the section.  */
   13865 	  isym = &rcookie->locsyms[r_symndx];
   13866 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   13867 	  if (isec != NULL
   13868 	      && (isec->kept_section != NULL
   13869 		  || discarded_section (isec)))
   13870 	    return TRUE;
   13871 	}
   13872       return FALSE;
   13873     }
   13874   return FALSE;
   13875 }
   13876 
   13877 /* Discard unneeded references to discarded sections.
   13878    Returns -1 on error, 1 if any section's size was changed, 0 if
   13879    nothing changed.  This function assumes that the relocations are in
   13880    sorted order, which is true for all known assemblers.  */
   13881 
   13882 int
   13883 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   13884 {
   13885   struct elf_reloc_cookie cookie;
   13886   asection *o;
   13887   bfd *abfd;
   13888   int changed = 0;
   13889 
   13890   if (info->traditional_format
   13891       || !is_elf_hash_table (info->hash))
   13892     return 0;
   13893 
   13894   o = bfd_get_section_by_name (output_bfd, ".stab");
   13895   if (o != NULL)
   13896     {
   13897       asection *i;
   13898 
   13899       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13900 	{
   13901 	  if (i->size == 0
   13902 	      || i->reloc_count == 0
   13903 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   13904 	    continue;
   13905 
   13906 	  abfd = i->owner;
   13907 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13908 	    continue;
   13909 
   13910 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   13911 	    return -1;
   13912 
   13913 	  if (_bfd_discard_section_stabs (abfd, i,
   13914 					  elf_section_data (i)->sec_info,
   13915 					  bfd_elf_reloc_symbol_deleted_p,
   13916 					  &cookie))
   13917 	    changed = 1;
   13918 
   13919 	  fini_reloc_cookie_for_section (&cookie, i);
   13920 	}
   13921     }
   13922 
   13923   o = NULL;
   13924   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   13925     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   13926   if (o != NULL)
   13927     {
   13928       asection *i;
   13929       int eh_changed = 0;
   13930       unsigned int eh_alignment;
   13931 
   13932       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13933 	{
   13934 	  if (i->size == 0)
   13935 	    continue;
   13936 
   13937 	  abfd = i->owner;
   13938 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13939 	    continue;
   13940 
   13941 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   13942 	    return -1;
   13943 
   13944 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   13945 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   13946 						 bfd_elf_reloc_symbol_deleted_p,
   13947 						 &cookie))
   13948 	    {
   13949 	      eh_changed = 1;
   13950 	      if (i->size != i->rawsize)
   13951 		changed = 1;
   13952 	    }
   13953 
   13954 	  fini_reloc_cookie_for_section (&cookie, i);
   13955 	}
   13956 
   13957       eh_alignment = 1 << o->alignment_power;
   13958       /* Skip over zero terminator, and prevent empty sections from
   13959 	 adding alignment padding at the end.  */
   13960       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
   13961 	if (i->size == 0)
   13962 	  i->flags |= SEC_EXCLUDE;
   13963 	else if (i->size > 4)
   13964 	  break;
   13965       /* The last non-empty eh_frame section doesn't need padding.  */
   13966       if (i != NULL)
   13967 	i = i->map_tail.s;
   13968       /* Any prior sections must pad the last FDE out to the output
   13969 	 section alignment.  Otherwise we might have zero padding
   13970 	 between sections, which would be seen as a terminator.  */
   13971       for (; i != NULL; i = i->map_tail.s)
   13972 	if (i->size == 4)
   13973 	  /* All but the last zero terminator should have been removed.  */
   13974 	  BFD_FAIL ();
   13975 	else
   13976 	  {
   13977 	    bfd_size_type size
   13978 	      = (i->size + eh_alignment - 1) & -eh_alignment;
   13979 	    if (i->size != size)
   13980 	      {
   13981 		i->size = size;
   13982 		changed = 1;
   13983 		eh_changed = 1;
   13984 	      }
   13985 	  }
   13986       if (eh_changed)
   13987 	elf_link_hash_traverse (elf_hash_table (info),
   13988 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
   13989     }
   13990 
   13991   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   13992     {
   13993       const struct elf_backend_data *bed;
   13994       asection *s;
   13995 
   13996       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13997 	continue;
   13998       s = abfd->sections;
   13999       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   14000 	continue;
   14001 
   14002       bed = get_elf_backend_data (abfd);
   14003 
   14004       if (bed->elf_backend_discard_info != NULL)
   14005 	{
   14006 	  if (!init_reloc_cookie (&cookie, info, abfd))
   14007 	    return -1;
   14008 
   14009 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   14010 	    changed = 1;
   14011 
   14012 	  fini_reloc_cookie (&cookie, abfd);
   14013 	}
   14014     }
   14015 
   14016   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   14017     _bfd_elf_end_eh_frame_parsing (info);
   14018 
   14019   if (info->eh_frame_hdr_type
   14020       && !bfd_link_relocatable (info)
   14021       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
   14022     changed = 1;
   14023 
   14024   return changed;
   14025 }
   14026 
   14027 bfd_boolean
   14028 _bfd_elf_section_already_linked (bfd *abfd,
   14029 				 asection *sec,
   14030 				 struct bfd_link_info *info)
   14031 {
   14032   flagword flags;
   14033   const char *name, *key;
   14034   struct bfd_section_already_linked *l;
   14035   struct bfd_section_already_linked_hash_entry *already_linked_list;
   14036 
   14037   if (sec->output_section == bfd_abs_section_ptr)
   14038     return FALSE;
   14039 
   14040   flags = sec->flags;
   14041 
   14042   /* Return if it isn't a linkonce section.  A comdat group section
   14043      also has SEC_LINK_ONCE set.  */
   14044   if ((flags & SEC_LINK_ONCE) == 0)
   14045     return FALSE;
   14046 
   14047   /* Don't put group member sections on our list of already linked
   14048      sections.  They are handled as a group via their group section.  */
   14049   if (elf_sec_group (sec) != NULL)
   14050     return FALSE;
   14051 
   14052   /* For a SHT_GROUP section, use the group signature as the key.  */
   14053   name = sec->name;
   14054   if ((flags & SEC_GROUP) != 0
   14055       && elf_next_in_group (sec) != NULL
   14056       && elf_group_name (elf_next_in_group (sec)) != NULL)
   14057     key = elf_group_name (elf_next_in_group (sec));
   14058   else
   14059     {
   14060       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   14061       if (CONST_STRNEQ (name, ".gnu.linkonce.")
   14062 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   14063 	key++;
   14064       else
   14065 	/* Must be a user linkonce section that doesn't follow gcc's
   14066 	   naming convention.  In this case we won't be matching
   14067 	   single member groups.  */
   14068 	key = name;
   14069     }
   14070 
   14071   already_linked_list = bfd_section_already_linked_table_lookup (key);
   14072 
   14073   for (l = already_linked_list->entry; l != NULL; l = l->next)
   14074     {
   14075       /* We may have 2 different types of sections on the list: group
   14076 	 sections with a signature of <key> (<key> is some string),
   14077 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   14078 	 Match like sections.  LTO plugin sections are an exception.
   14079 	 They are always named .gnu.linkonce.t.<key> and match either
   14080 	 type of section.  */
   14081       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   14082 	   && ((flags & SEC_GROUP) != 0
   14083 	       || strcmp (name, l->sec->name) == 0))
   14084 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
   14085 	{
   14086 	  /* The section has already been linked.  See if we should
   14087 	     issue a warning.  */
   14088 	  if (!_bfd_handle_already_linked (sec, l, info))
   14089 	    return FALSE;
   14090 
   14091 	  if (flags & SEC_GROUP)
   14092 	    {
   14093 	      asection *first = elf_next_in_group (sec);
   14094 	      asection *s = first;
   14095 
   14096 	      while (s != NULL)
   14097 		{
   14098 		  s->output_section = bfd_abs_section_ptr;
   14099 		  /* Record which group discards it.  */
   14100 		  s->kept_section = l->sec;
   14101 		  s = elf_next_in_group (s);
   14102 		  /* These lists are circular.  */
   14103 		  if (s == first)
   14104 		    break;
   14105 		}
   14106 	    }
   14107 
   14108 	  return TRUE;
   14109 	}
   14110     }
   14111 
   14112   /* A single member comdat group section may be discarded by a
   14113      linkonce section and vice versa.  */
   14114   if ((flags & SEC_GROUP) != 0)
   14115     {
   14116       asection *first = elf_next_in_group (sec);
   14117 
   14118       if (first != NULL && elf_next_in_group (first) == first)
   14119 	/* Check this single member group against linkonce sections.  */
   14120 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   14121 	  if ((l->sec->flags & SEC_GROUP) == 0
   14122 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   14123 	    {
   14124 	      first->output_section = bfd_abs_section_ptr;
   14125 	      first->kept_section = l->sec;
   14126 	      sec->output_section = bfd_abs_section_ptr;
   14127 	      break;
   14128 	    }
   14129     }
   14130   else
   14131     /* Check this linkonce section against single member groups.  */
   14132     for (l = already_linked_list->entry; l != NULL; l = l->next)
   14133       if (l->sec->flags & SEC_GROUP)
   14134 	{
   14135 	  asection *first = elf_next_in_group (l->sec);
   14136 
   14137 	  if (first != NULL
   14138 	      && elf_next_in_group (first) == first
   14139 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   14140 	    {
   14141 	      sec->output_section = bfd_abs_section_ptr;
   14142 	      sec->kept_section = first;
   14143 	      break;
   14144 	    }
   14145 	}
   14146 
   14147   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   14148      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   14149      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   14150      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   14151      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   14152      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   14153      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   14154      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   14155      The reverse order cannot happen as there is never a bfd with only the
   14156      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   14157      matter as here were are looking only for cross-bfd sections.  */
   14158 
   14159   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
   14160     for (l = already_linked_list->entry; l != NULL; l = l->next)
   14161       if ((l->sec->flags & SEC_GROUP) == 0
   14162 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
   14163 	{
   14164 	  if (abfd != l->sec->owner)
   14165 	    sec->output_section = bfd_abs_section_ptr;
   14166 	  break;
   14167 	}
   14168 
   14169   /* This is the first section with this name.  Record it.  */
   14170   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   14171     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   14172   return sec->output_section == bfd_abs_section_ptr;
   14173 }
   14174 
   14175 bfd_boolean
   14176 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   14177 {
   14178   return sym->st_shndx == SHN_COMMON;
   14179 }
   14180 
   14181 unsigned int
   14182 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   14183 {
   14184   return SHN_COMMON;
   14185 }
   14186 
   14187 asection *
   14188 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   14189 {
   14190   return bfd_com_section_ptr;
   14191 }
   14192 
   14193 bfd_vma
   14194 _bfd_elf_default_got_elt_size (bfd *abfd,
   14195 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   14196 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   14197 			       bfd *ibfd ATTRIBUTE_UNUSED,
   14198 			       unsigned long symndx ATTRIBUTE_UNUSED)
   14199 {
   14200   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14201   return bed->s->arch_size / 8;
   14202 }
   14203 
   14204 /* Routines to support the creation of dynamic relocs.  */
   14205 
   14206 /* Returns the name of the dynamic reloc section associated with SEC.  */
   14207 
   14208 static const char *
   14209 get_dynamic_reloc_section_name (bfd *       abfd,
   14210 				asection *  sec,
   14211 				bfd_boolean is_rela)
   14212 {
   14213   char *name;
   14214   const char *old_name = bfd_get_section_name (NULL, sec);
   14215   const char *prefix = is_rela ? ".rela" : ".rel";
   14216 
   14217   if (old_name == NULL)
   14218     return NULL;
   14219 
   14220   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   14221   sprintf (name, "%s%s", prefix, old_name);
   14222 
   14223   return name;
   14224 }
   14225 
   14226 /* Returns the dynamic reloc section associated with SEC.
   14227    If necessary compute the name of the dynamic reloc section based
   14228    on SEC's name (looked up in ABFD's string table) and the setting
   14229    of IS_RELA.  */
   14230 
   14231 asection *
   14232 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
   14233 				    asection *  sec,
   14234 				    bfd_boolean is_rela)
   14235 {
   14236   asection * reloc_sec = elf_section_data (sec)->sreloc;
   14237 
   14238   if (reloc_sec == NULL)
   14239     {
   14240       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   14241 
   14242       if (name != NULL)
   14243 	{
   14244 	  reloc_sec = bfd_get_linker_section (abfd, name);
   14245 
   14246 	  if (reloc_sec != NULL)
   14247 	    elf_section_data (sec)->sreloc = reloc_sec;
   14248 	}
   14249     }
   14250 
   14251   return reloc_sec;
   14252 }
   14253 
   14254 /* Returns the dynamic reloc section associated with SEC.  If the
   14255    section does not exist it is created and attached to the DYNOBJ
   14256    bfd and stored in the SRELOC field of SEC's elf_section_data
   14257    structure.
   14258 
   14259    ALIGNMENT is the alignment for the newly created section and
   14260    IS_RELA defines whether the name should be .rela.<SEC's name>
   14261    or .rel.<SEC's name>.  The section name is looked up in the
   14262    string table associated with ABFD.  */
   14263 
   14264 asection *
   14265 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   14266 				     bfd *dynobj,
   14267 				     unsigned int alignment,
   14268 				     bfd *abfd,
   14269 				     bfd_boolean is_rela)
   14270 {
   14271   asection * reloc_sec = elf_section_data (sec)->sreloc;
   14272 
   14273   if (reloc_sec == NULL)
   14274     {
   14275       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   14276 
   14277       if (name == NULL)
   14278 	return NULL;
   14279 
   14280       reloc_sec = bfd_get_linker_section (dynobj, name);
   14281 
   14282       if (reloc_sec == NULL)
   14283 	{
   14284 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   14285 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   14286 	  if ((sec->flags & SEC_ALLOC) != 0)
   14287 	    flags |= SEC_ALLOC | SEC_LOAD;
   14288 
   14289 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   14290 	  if (reloc_sec != NULL)
   14291 	    {
   14292 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   14293 		 name.  Override as it may be wrong, eg. for a user
   14294 		 section named "auto" we'll get ".relauto" which is
   14295 		 seen to be a .rela section.  */
   14296 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   14297 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
   14298 		reloc_sec = NULL;
   14299 	    }
   14300 	}
   14301 
   14302       elf_section_data (sec)->sreloc = reloc_sec;
   14303     }
   14304 
   14305   return reloc_sec;
   14306 }
   14307 
   14308 /* Copy the ELF symbol type and other attributes for a linker script
   14309    assignment from HSRC to HDEST.  Generally this should be treated as
   14310    if we found a strong non-dynamic definition for HDEST (except that
   14311    ld ignores multiple definition errors).  */
   14312 void
   14313 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   14314 				     struct bfd_link_hash_entry *hdest,
   14315 				     struct bfd_link_hash_entry *hsrc)
   14316 {
   14317   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   14318   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   14319   Elf_Internal_Sym isym;
   14320 
   14321   ehdest->type = ehsrc->type;
   14322   ehdest->target_internal = ehsrc->target_internal;
   14323 
   14324   isym.st_other = ehsrc->other;
   14325   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
   14326 }
   14327 
   14328 /* Append a RELA relocation REL to section S in BFD.  */
   14329 
   14330 void
   14331 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   14332 {
   14333   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14334   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   14335   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   14336   bed->s->swap_reloca_out (abfd, rel, loc);
   14337 }
   14338 
   14339 /* Append a REL relocation REL to section S in BFD.  */
   14340 
   14341 void
   14342 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   14343 {
   14344   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14345   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   14346   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   14347   bed->s->swap_reloc_out (abfd, rel, loc);
   14348 }
   14349 
   14350 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
   14351 
   14352 struct bfd_link_hash_entry *
   14353 bfd_elf_define_start_stop (struct bfd_link_info *info,
   14354 			   const char *symbol, asection *sec)
   14355 {
   14356   struct elf_link_hash_entry *h;
   14357 
   14358   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
   14359 			    FALSE, FALSE, TRUE);
   14360   if (h != NULL
   14361       && (h->root.type == bfd_link_hash_undefined
   14362 	  || h->root.type == bfd_link_hash_undefweak
   14363 	  || (h->ref_regular && !h->def_regular)))
   14364     {
   14365       h->root.type = bfd_link_hash_defined;
   14366       h->root.u.def.section = sec;
   14367       h->root.u.def.value = 0;
   14368       h->def_regular = 1;
   14369       h->def_dynamic = 0;
   14370       h->start_stop = 1;
   14371       h->u2.start_stop_section = sec;
   14372       if (symbol[0] == '.')
   14373 	{
   14374 	  /* .startof. and .sizeof. symbols are local.  */
   14375 	  const struct elf_backend_data *bed;
   14376 	  bed = get_elf_backend_data (info->output_bfd);
   14377 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   14378 	}
   14379       else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   14380 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
   14381       return &h->root;
   14382     }
   14383   return NULL;
   14384 }
   14385