Home | History | Annotate | Download | only in bfd
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2014 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "bfdlink.h"
     24 #include "libbfd.h"
     25 #define ARCH_SIZE 0
     26 #include "elf-bfd.h"
     27 #include "safe-ctype.h"
     28 #include "libiberty.h"
     29 #include "objalloc.h"
     30 
     31 /* This struct is used to pass information to routines called via
     32    elf_link_hash_traverse which must return failure.  */
     33 
     34 struct elf_info_failed
     35 {
     36   struct bfd_link_info *info;
     37   bfd_boolean failed;
     38 };
     39 
     40 /* This structure is used to pass information to
     41    _bfd_elf_link_find_version_dependencies.  */
     42 
     43 struct elf_find_verdep_info
     44 {
     45   /* General link information.  */
     46   struct bfd_link_info *info;
     47   /* The number of dependencies.  */
     48   unsigned int vers;
     49   /* Whether we had a failure.  */
     50   bfd_boolean failed;
     51 };
     52 
     53 static bfd_boolean _bfd_elf_fix_symbol_flags
     54   (struct elf_link_hash_entry *, struct elf_info_failed *);
     55 
     56 /* Define a symbol in a dynamic linkage section.  */
     57 
     58 struct elf_link_hash_entry *
     59 _bfd_elf_define_linkage_sym (bfd *abfd,
     60 			     struct bfd_link_info *info,
     61 			     asection *sec,
     62 			     const char *name)
     63 {
     64   struct elf_link_hash_entry *h;
     65   struct bfd_link_hash_entry *bh;
     66   const struct elf_backend_data *bed;
     67 
     68   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
     69   if (h != NULL)
     70     {
     71       /* Zap symbol defined in an as-needed lib that wasn't linked.
     72 	 This is a symptom of a larger problem:  Absolute symbols
     73 	 defined in shared libraries can't be overridden, because we
     74 	 lose the link to the bfd which is via the symbol section.  */
     75       h->root.type = bfd_link_hash_new;
     76     }
     77 
     78   bh = &h->root;
     79   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
     80 					 sec, 0, NULL, FALSE,
     81 					 get_elf_backend_data (abfd)->collect,
     82 					 &bh))
     83     return NULL;
     84   h = (struct elf_link_hash_entry *) bh;
     85   h->def_regular = 1;
     86   h->non_elf = 0;
     87   h->type = STT_OBJECT;
     88   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
     89     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
     90 
     91   bed = get_elf_backend_data (abfd);
     92   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
     93   return h;
     94 }
     95 
     96 bfd_boolean
     97 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
     98 {
     99   flagword flags;
    100   asection *s;
    101   struct elf_link_hash_entry *h;
    102   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    103   struct elf_link_hash_table *htab = elf_hash_table (info);
    104 
    105   /* This function may be called more than once.  */
    106   s = bfd_get_linker_section (abfd, ".got");
    107   if (s != NULL)
    108     return TRUE;
    109 
    110   flags = bed->dynamic_sec_flags;
    111 
    112   s = bfd_make_section_anyway_with_flags (abfd,
    113 					  (bed->rela_plts_and_copies_p
    114 					   ? ".rela.got" : ".rel.got"),
    115 					  (bed->dynamic_sec_flags
    116 					   | SEC_READONLY));
    117   if (s == NULL
    118       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    119     return FALSE;
    120   htab->srelgot = s;
    121 
    122   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    123   if (s == NULL
    124       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    125     return FALSE;
    126   htab->sgot = s;
    127 
    128   if (bed->want_got_plt)
    129     {
    130       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    131       if (s == NULL
    132 	  || !bfd_set_section_alignment (abfd, s,
    133 					 bed->s->log_file_align))
    134 	return FALSE;
    135       htab->sgotplt = s;
    136     }
    137 
    138   /* The first bit of the global offset table is the header.  */
    139   s->size += bed->got_header_size;
    140 
    141   if (bed->want_got_sym)
    142     {
    143       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    144 	 (or .got.plt) section.  We don't do this in the linker script
    145 	 because we don't want to define the symbol if we are not creating
    146 	 a global offset table.  */
    147       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    148 				       "_GLOBAL_OFFSET_TABLE_");
    149       elf_hash_table (info)->hgot = h;
    150       if (h == NULL)
    151 	return FALSE;
    152     }
    153 
    154   return TRUE;
    155 }
    156 
    157 /* Create a strtab to hold the dynamic symbol names.  */
    159 static bfd_boolean
    160 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    161 {
    162   struct elf_link_hash_table *hash_table;
    163 
    164   hash_table = elf_hash_table (info);
    165   if (hash_table->dynobj == NULL)
    166     hash_table->dynobj = abfd;
    167 
    168   if (hash_table->dynstr == NULL)
    169     {
    170       hash_table->dynstr = _bfd_elf_strtab_init ();
    171       if (hash_table->dynstr == NULL)
    172 	return FALSE;
    173     }
    174   return TRUE;
    175 }
    176 
    177 /* Create some sections which will be filled in with dynamic linking
    178    information.  ABFD is an input file which requires dynamic sections
    179    to be created.  The dynamic sections take up virtual memory space
    180    when the final executable is run, so we need to create them before
    181    addresses are assigned to the output sections.  We work out the
    182    actual contents and size of these sections later.  */
    183 
    184 bfd_boolean
    185 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    186 {
    187   flagword flags;
    188   asection *s;
    189   const struct elf_backend_data *bed;
    190   struct elf_link_hash_entry *h;
    191 
    192   if (! is_elf_hash_table (info->hash))
    193     return FALSE;
    194 
    195   if (elf_hash_table (info)->dynamic_sections_created)
    196     return TRUE;
    197 
    198   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    199     return FALSE;
    200 
    201   abfd = elf_hash_table (info)->dynobj;
    202   bed = get_elf_backend_data (abfd);
    203 
    204   flags = bed->dynamic_sec_flags;
    205 
    206   /* A dynamically linked executable has a .interp section, but a
    207      shared library does not.  */
    208   if (info->executable)
    209     {
    210       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    211 					      flags | SEC_READONLY);
    212       if (s == NULL)
    213 	return FALSE;
    214     }
    215 
    216   /* Create sections to hold version informations.  These are removed
    217      if they are not needed.  */
    218   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    219 					  flags | SEC_READONLY);
    220   if (s == NULL
    221       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    222     return FALSE;
    223 
    224   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    225 					  flags | SEC_READONLY);
    226   if (s == NULL
    227       || ! bfd_set_section_alignment (abfd, s, 1))
    228     return FALSE;
    229 
    230   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    231 					  flags | SEC_READONLY);
    232   if (s == NULL
    233       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    234     return FALSE;
    235 
    236   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    237 					  flags | SEC_READONLY);
    238   if (s == NULL
    239       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    240     return FALSE;
    241 
    242   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    243 					  flags | SEC_READONLY);
    244   if (s == NULL)
    245     return FALSE;
    246 
    247   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    248   if (s == NULL
    249       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    250     return FALSE;
    251 
    252   /* The special symbol _DYNAMIC is always set to the start of the
    253      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    254      only want to define it if we are, in fact, creating a .dynamic
    255      section.  We don't want to define it if there is no .dynamic
    256      section, since on some ELF platforms the start up code examines it
    257      to decide how to initialize the process.  */
    258   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    259   elf_hash_table (info)->hdynamic = h;
    260   if (h == NULL)
    261     return FALSE;
    262 
    263   if (info->emit_hash)
    264     {
    265       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    266 					      flags | SEC_READONLY);
    267       if (s == NULL
    268 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    269 	return FALSE;
    270       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    271     }
    272 
    273   if (info->emit_gnu_hash)
    274     {
    275       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    276 					      flags | SEC_READONLY);
    277       if (s == NULL
    278 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    279 	return FALSE;
    280       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    281 	 4 32-bit words followed by variable count of 64-bit words, then
    282 	 variable count of 32-bit words.  */
    283       if (bed->s->arch_size == 64)
    284 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    285       else
    286 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    287     }
    288 
    289   /* Let the backend create the rest of the sections.  This lets the
    290      backend set the right flags.  The backend will normally create
    291      the .got and .plt sections.  */
    292   if (bed->elf_backend_create_dynamic_sections == NULL
    293       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    294     return FALSE;
    295 
    296   elf_hash_table (info)->dynamic_sections_created = TRUE;
    297 
    298   return TRUE;
    299 }
    300 
    301 /* Create dynamic sections when linking against a dynamic object.  */
    302 
    303 bfd_boolean
    304 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    305 {
    306   flagword flags, pltflags;
    307   struct elf_link_hash_entry *h;
    308   asection *s;
    309   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    310   struct elf_link_hash_table *htab = elf_hash_table (info);
    311 
    312   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    313      .rel[a].bss sections.  */
    314   flags = bed->dynamic_sec_flags;
    315 
    316   pltflags = flags;
    317   if (bed->plt_not_loaded)
    318     /* We do not clear SEC_ALLOC here because we still want the OS to
    319        allocate space for the section; it's just that there's nothing
    320        to read in from the object file.  */
    321     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    322   else
    323     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    324   if (bed->plt_readonly)
    325     pltflags |= SEC_READONLY;
    326 
    327   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    328   if (s == NULL
    329       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    330     return FALSE;
    331   htab->splt = s;
    332 
    333   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    334      .plt section.  */
    335   if (bed->want_plt_sym)
    336     {
    337       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    338 				       "_PROCEDURE_LINKAGE_TABLE_");
    339       elf_hash_table (info)->hplt = h;
    340       if (h == NULL)
    341 	return FALSE;
    342     }
    343 
    344   s = bfd_make_section_anyway_with_flags (abfd,
    345 					  (bed->rela_plts_and_copies_p
    346 					   ? ".rela.plt" : ".rel.plt"),
    347 					  flags | SEC_READONLY);
    348   if (s == NULL
    349       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    350     return FALSE;
    351   htab->srelplt = s;
    352 
    353   if (! _bfd_elf_create_got_section (abfd, info))
    354     return FALSE;
    355 
    356   if (bed->want_dynbss)
    357     {
    358       /* The .dynbss section is a place to put symbols which are defined
    359 	 by dynamic objects, are referenced by regular objects, and are
    360 	 not functions.  We must allocate space for them in the process
    361 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    362 	 initialize them at run time.  The linker script puts the .dynbss
    363 	 section into the .bss section of the final image.  */
    364       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    365 					      (SEC_ALLOC | SEC_LINKER_CREATED));
    366       if (s == NULL)
    367 	return FALSE;
    368 
    369       /* The .rel[a].bss section holds copy relocs.  This section is not
    370 	 normally needed.  We need to create it here, though, so that the
    371 	 linker will map it to an output section.  We can't just create it
    372 	 only if we need it, because we will not know whether we need it
    373 	 until we have seen all the input files, and the first time the
    374 	 main linker code calls BFD after examining all the input files
    375 	 (size_dynamic_sections) the input sections have already been
    376 	 mapped to the output sections.  If the section turns out not to
    377 	 be needed, we can discard it later.  We will never need this
    378 	 section when generating a shared object, since they do not use
    379 	 copy relocs.  */
    380       if (! info->shared)
    381 	{
    382 	  s = bfd_make_section_anyway_with_flags (abfd,
    383 						  (bed->rela_plts_and_copies_p
    384 						   ? ".rela.bss" : ".rel.bss"),
    385 						  flags | SEC_READONLY);
    386 	  if (s == NULL
    387 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    388 	    return FALSE;
    389 	}
    390     }
    391 
    392   return TRUE;
    393 }
    394 
    395 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    397    read the input files, since we need to have a list of all of them
    398    before we can determine the final sizes of the output sections.
    399    Note that we may actually call this function even though we are not
    400    going to output any dynamic symbols; in some cases we know that a
    401    symbol should be in the dynamic symbol table, but only if there is
    402    one.  */
    403 
    404 bfd_boolean
    405 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    406 				    struct elf_link_hash_entry *h)
    407 {
    408   if (h->dynindx == -1)
    409     {
    410       struct elf_strtab_hash *dynstr;
    411       char *p;
    412       const char *name;
    413       bfd_size_type indx;
    414 
    415       /* XXX: The ABI draft says the linker must turn hidden and
    416 	 internal symbols into STB_LOCAL symbols when producing the
    417 	 DSO. However, if ld.so honors st_other in the dynamic table,
    418 	 this would not be necessary.  */
    419       switch (ELF_ST_VISIBILITY (h->other))
    420 	{
    421 	case STV_INTERNAL:
    422 	case STV_HIDDEN:
    423 	  if (h->root.type != bfd_link_hash_undefined
    424 	      && h->root.type != bfd_link_hash_undefweak)
    425 	    {
    426 	      h->forced_local = 1;
    427 	      if (!elf_hash_table (info)->is_relocatable_executable)
    428 		return TRUE;
    429 	    }
    430 
    431 	default:
    432 	  break;
    433 	}
    434 
    435       h->dynindx = elf_hash_table (info)->dynsymcount;
    436       ++elf_hash_table (info)->dynsymcount;
    437 
    438       dynstr = elf_hash_table (info)->dynstr;
    439       if (dynstr == NULL)
    440 	{
    441 	  /* Create a strtab to hold the dynamic symbol names.  */
    442 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    443 	  if (dynstr == NULL)
    444 	    return FALSE;
    445 	}
    446 
    447       /* We don't put any version information in the dynamic string
    448 	 table.  */
    449       name = h->root.root.string;
    450       p = strchr (name, ELF_VER_CHR);
    451       if (p != NULL)
    452 	/* We know that the p points into writable memory.  In fact,
    453 	   there are only a few symbols that have read-only names, being
    454 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
    455 	   by the backends.  Most symbols will have names pointing into
    456 	   an ELF string table read from a file, or to objalloc memory.  */
    457 	*p = 0;
    458 
    459       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    460 
    461       if (p != NULL)
    462 	*p = ELF_VER_CHR;
    463 
    464       if (indx == (bfd_size_type) -1)
    465 	return FALSE;
    466       h->dynstr_index = indx;
    467     }
    468 
    469   return TRUE;
    470 }
    471 
    472 /* Mark a symbol dynamic.  */
    474 
    475 static void
    476 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    477 				  struct elf_link_hash_entry *h,
    478 				  Elf_Internal_Sym *sym)
    479 {
    480   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    481 
    482   /* It may be called more than once on the same H.  */
    483   if(h->dynamic || info->relocatable)
    484     return;
    485 
    486   if ((info->dynamic_data
    487        && (h->type == STT_OBJECT
    488 	   || (sym != NULL
    489 	       && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
    490       || (d != NULL
    491 	  && h->root.type == bfd_link_hash_new
    492 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    493     h->dynamic = 1;
    494 }
    495 
    496 /* Record an assignment to a symbol made by a linker script.  We need
    497    this in case some dynamic object refers to this symbol.  */
    498 
    499 bfd_boolean
    500 bfd_elf_record_link_assignment (bfd *output_bfd,
    501 				struct bfd_link_info *info,
    502 				const char *name,
    503 				bfd_boolean provide,
    504 				bfd_boolean hidden)
    505 {
    506   struct elf_link_hash_entry *h, *hv;
    507   struct elf_link_hash_table *htab;
    508   const struct elf_backend_data *bed;
    509 
    510   if (!is_elf_hash_table (info->hash))
    511     return TRUE;
    512 
    513   htab = elf_hash_table (info);
    514   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
    515   if (h == NULL)
    516     return provide;
    517 
    518   switch (h->root.type)
    519     {
    520     case bfd_link_hash_defined:
    521     case bfd_link_hash_defweak:
    522     case bfd_link_hash_common:
    523       break;
    524     case bfd_link_hash_undefweak:
    525     case bfd_link_hash_undefined:
    526       /* Since we're defining the symbol, don't let it seem to have not
    527 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    528 	 may depend on this.  */
    529       h->root.type = bfd_link_hash_new;
    530       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    531 	bfd_link_repair_undef_list (&htab->root);
    532       break;
    533     case bfd_link_hash_new:
    534       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    535       h->non_elf = 0;
    536       break;
    537     case bfd_link_hash_indirect:
    538       /* We had a versioned symbol in a dynamic library.  We make the
    539 	 the versioned symbol point to this one.  */
    540       bed = get_elf_backend_data (output_bfd);
    541       hv = h;
    542       while (hv->root.type == bfd_link_hash_indirect
    543 	     || hv->root.type == bfd_link_hash_warning)
    544 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    545       /* We don't need to update h->root.u since linker will set them
    546 	 later.  */
    547       h->root.type = bfd_link_hash_undefined;
    548       hv->root.type = bfd_link_hash_indirect;
    549       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    550       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    551       break;
    552     case bfd_link_hash_warning:
    553       abort ();
    554       break;
    555     }
    556 
    557   /* If this symbol is being provided by the linker script, and it is
    558      currently defined by a dynamic object, but not by a regular
    559      object, then mark it as undefined so that the generic linker will
    560      force the correct value.  */
    561   if (provide
    562       && h->def_dynamic
    563       && !h->def_regular)
    564     h->root.type = bfd_link_hash_undefined;
    565 
    566   /* If this symbol is not being provided by the linker script, and it is
    567      currently defined by a dynamic object, but not by a regular object,
    568      then clear out any version information because the symbol will not be
    569      associated with the dynamic object any more.  */
    570   if (!provide
    571       && h->def_dynamic
    572       && !h->def_regular)
    573     h->verinfo.verdef = NULL;
    574 
    575   h->def_regular = 1;
    576 
    577   if (hidden)
    578     {
    579       bed = get_elf_backend_data (output_bfd);
    580       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    581 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    582       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    583     }
    584 
    585   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    586      and executables.  */
    587   if (!info->relocatable
    588       && h->dynindx != -1
    589       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    590 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    591     h->forced_local = 1;
    592 
    593   if ((h->def_dynamic
    594        || h->ref_dynamic
    595        || info->shared
    596        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
    597       && h->dynindx == -1)
    598     {
    599       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    600 	return FALSE;
    601 
    602       /* If this is a weak defined symbol, and we know a corresponding
    603 	 real symbol from the same dynamic object, make sure the real
    604 	 symbol is also made into a dynamic symbol.  */
    605       if (h->u.weakdef != NULL
    606 	  && h->u.weakdef->dynindx == -1)
    607 	{
    608 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
    609 	    return FALSE;
    610 	}
    611     }
    612 
    613   return TRUE;
    614 }
    615 
    616 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    617    success, and 2 on a failure caused by attempting to record a symbol
    618    in a discarded section, eg. a discarded link-once section symbol.  */
    619 
    620 int
    621 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    622 					  bfd *input_bfd,
    623 					  long input_indx)
    624 {
    625   bfd_size_type amt;
    626   struct elf_link_local_dynamic_entry *entry;
    627   struct elf_link_hash_table *eht;
    628   struct elf_strtab_hash *dynstr;
    629   unsigned long dynstr_index;
    630   char *name;
    631   Elf_External_Sym_Shndx eshndx;
    632   char esym[sizeof (Elf64_External_Sym)];
    633 
    634   if (! is_elf_hash_table (info->hash))
    635     return 0;
    636 
    637   /* See if the entry exists already.  */
    638   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    639     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    640       return 1;
    641 
    642   amt = sizeof (*entry);
    643   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    644   if (entry == NULL)
    645     return 0;
    646 
    647   /* Go find the symbol, so that we can find it's name.  */
    648   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    649 			     1, input_indx, &entry->isym, esym, &eshndx))
    650     {
    651       bfd_release (input_bfd, entry);
    652       return 0;
    653     }
    654 
    655   if (entry->isym.st_shndx != SHN_UNDEF
    656       && entry->isym.st_shndx < SHN_LORESERVE)
    657     {
    658       asection *s;
    659 
    660       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    661       if (s == NULL || bfd_is_abs_section (s->output_section))
    662 	{
    663 	  /* We can still bfd_release here as nothing has done another
    664 	     bfd_alloc.  We can't do this later in this function.  */
    665 	  bfd_release (input_bfd, entry);
    666 	  return 2;
    667 	}
    668     }
    669 
    670   name = (bfd_elf_string_from_elf_section
    671 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    672 	   entry->isym.st_name));
    673 
    674   dynstr = elf_hash_table (info)->dynstr;
    675   if (dynstr == NULL)
    676     {
    677       /* Create a strtab to hold the dynamic symbol names.  */
    678       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    679       if (dynstr == NULL)
    680 	return 0;
    681     }
    682 
    683   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
    684   if (dynstr_index == (unsigned long) -1)
    685     return 0;
    686   entry->isym.st_name = dynstr_index;
    687 
    688   eht = elf_hash_table (info);
    689 
    690   entry->next = eht->dynlocal;
    691   eht->dynlocal = entry;
    692   entry->input_bfd = input_bfd;
    693   entry->input_indx = input_indx;
    694   eht->dynsymcount++;
    695 
    696   /* Whatever binding the symbol had before, it's now local.  */
    697   entry->isym.st_info
    698     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    699 
    700   /* The dynindx will be set at the end of size_dynamic_sections.  */
    701 
    702   return 1;
    703 }
    704 
    705 /* Return the dynindex of a local dynamic symbol.  */
    706 
    707 long
    708 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    709 				    bfd *input_bfd,
    710 				    long input_indx)
    711 {
    712   struct elf_link_local_dynamic_entry *e;
    713 
    714   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    715     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    716       return e->dynindx;
    717   return -1;
    718 }
    719 
    720 /* This function is used to renumber the dynamic symbols, if some of
    721    them are removed because they are marked as local.  This is called
    722    via elf_link_hash_traverse.  */
    723 
    724 static bfd_boolean
    725 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    726 				      void *data)
    727 {
    728   size_t *count = (size_t *) data;
    729 
    730   if (h->forced_local)
    731     return TRUE;
    732 
    733   if (h->dynindx != -1)
    734     h->dynindx = ++(*count);
    735 
    736   return TRUE;
    737 }
    738 
    739 
    740 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    741    STB_LOCAL binding.  */
    742 
    743 static bfd_boolean
    744 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    745 					    void *data)
    746 {
    747   size_t *count = (size_t *) data;
    748 
    749   if (!h->forced_local)
    750     return TRUE;
    751 
    752   if (h->dynindx != -1)
    753     h->dynindx = ++(*count);
    754 
    755   return TRUE;
    756 }
    757 
    758 /* Return true if the dynamic symbol for a given section should be
    759    omitted when creating a shared library.  */
    760 bfd_boolean
    761 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
    762 				   struct bfd_link_info *info,
    763 				   asection *p)
    764 {
    765   struct elf_link_hash_table *htab;
    766 
    767   switch (elf_section_data (p)->this_hdr.sh_type)
    768     {
    769     case SHT_PROGBITS:
    770     case SHT_NOBITS:
    771       /* If sh_type is yet undecided, assume it could be
    772 	 SHT_PROGBITS/SHT_NOBITS.  */
    773     case SHT_NULL:
    774       htab = elf_hash_table (info);
    775       if (p == htab->tls_sec)
    776 	return FALSE;
    777 
    778       if (htab->text_index_section != NULL)
    779 	return p != htab->text_index_section && p != htab->data_index_section;
    780 
    781       if (strcmp (p->name, ".got") == 0
    782 	  || strcmp (p->name, ".got.plt") == 0
    783 	  || strcmp (p->name, ".plt") == 0)
    784 	{
    785 	  asection *ip;
    786 
    787 	  if (htab->dynobj != NULL
    788 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
    789 	      && ip->output_section == p)
    790 	    return TRUE;
    791 	}
    792       return FALSE;
    793 
    794       /* There shouldn't be section relative relocations
    795 	 against any other section.  */
    796     default:
    797       return TRUE;
    798     }
    799 }
    800 
    801 /* Assign dynsym indices.  In a shared library we generate a section
    802    symbol for each output section, which come first.  Next come symbols
    803    which have been forced to local binding.  Then all of the back-end
    804    allocated local dynamic syms, followed by the rest of the global
    805    symbols.  */
    806 
    807 static unsigned long
    808 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
    809 				struct bfd_link_info *info,
    810 				unsigned long *section_sym_count)
    811 {
    812   unsigned long dynsymcount = 0;
    813 
    814   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
    815     {
    816       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
    817       asection *p;
    818       for (p = output_bfd->sections; p ; p = p->next)
    819 	if ((p->flags & SEC_EXCLUDE) == 0
    820 	    && (p->flags & SEC_ALLOC) != 0
    821 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
    822 	  elf_section_data (p)->dynindx = ++dynsymcount;
    823 	else
    824 	  elf_section_data (p)->dynindx = 0;
    825     }
    826   *section_sym_count = dynsymcount;
    827 
    828   elf_link_hash_traverse (elf_hash_table (info),
    829 			  elf_link_renumber_local_hash_table_dynsyms,
    830 			  &dynsymcount);
    831 
    832   if (elf_hash_table (info)->dynlocal)
    833     {
    834       struct elf_link_local_dynamic_entry *p;
    835       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
    836 	p->dynindx = ++dynsymcount;
    837     }
    838 
    839   elf_link_hash_traverse (elf_hash_table (info),
    840 			  elf_link_renumber_hash_table_dynsyms,
    841 			  &dynsymcount);
    842 
    843   /* There is an unused NULL entry at the head of the table which
    844      we must account for in our count.  Unless there weren't any
    845      symbols, which means we'll have no table at all.  */
    846   if (dynsymcount != 0)
    847     ++dynsymcount;
    848 
    849   elf_hash_table (info)->dynsymcount = dynsymcount;
    850   return dynsymcount;
    851 }
    852 
    853 /* Merge st_other field.  */
    854 
    855 static void
    856 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
    857 		    const Elf_Internal_Sym *isym,
    858 		    bfd_boolean definition, bfd_boolean dynamic)
    859 {
    860   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    861 
    862   /* If st_other has a processor-specific meaning, specific
    863      code might be needed here.  */
    864   if (bed->elf_backend_merge_symbol_attribute)
    865     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
    866 						dynamic);
    867 
    868   if (!dynamic)
    869     {
    870       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
    871       unsigned hvis = ELF_ST_VISIBILITY (h->other);
    872 
    873       /* Keep the most constraining visibility.  Leave the remainder
    874 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
    875       if (symvis - 1 < hvis - 1)
    876 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
    877     }
    878 }
    879 
    880 /* This function is called when we want to merge a new symbol with an
    881    existing symbol.  It handles the various cases which arise when we
    882    find a definition in a dynamic object, or when there is already a
    883    definition in a dynamic object.  The new symbol is described by
    884    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
    885    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
    886    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
    887    of an old common symbol.  We set OVERRIDE if the old symbol is
    888    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
    889    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
    890    to change.  By OK to change, we mean that we shouldn't warn if the
    891    type or size does change.  */
    892 
    893 static bfd_boolean
    894 _bfd_elf_merge_symbol (bfd *abfd,
    895 		       struct bfd_link_info *info,
    896 		       const char *name,
    897 		       Elf_Internal_Sym *sym,
    898 		       asection **psec,
    899 		       bfd_vma *pvalue,
    900 		       struct elf_link_hash_entry **sym_hash,
    901 		       bfd **poldbfd,
    902 		       bfd_boolean *pold_weak,
    903 		       unsigned int *pold_alignment,
    904 		       bfd_boolean *skip,
    905 		       bfd_boolean *override,
    906 		       bfd_boolean *type_change_ok,
    907 		       bfd_boolean *size_change_ok)
    908 {
    909   asection *sec, *oldsec;
    910   struct elf_link_hash_entry *h;
    911   struct elf_link_hash_entry *hi;
    912   struct elf_link_hash_entry *flip;
    913   int bind;
    914   bfd *oldbfd;
    915   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
    916   bfd_boolean newweak, oldweak, newfunc, oldfunc;
    917   const struct elf_backend_data *bed;
    918 
    919   *skip = FALSE;
    920   *override = FALSE;
    921 
    922   sec = *psec;
    923   bind = ELF_ST_BIND (sym->st_info);
    924 
    925   if (! bfd_is_und_section (sec))
    926     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
    927   else
    928     h = ((struct elf_link_hash_entry *)
    929 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
    930   if (h == NULL)
    931     return FALSE;
    932   *sym_hash = h;
    933 
    934   bed = get_elf_backend_data (abfd);
    935 
    936   /* For merging, we only care about real symbols.  But we need to make
    937      sure that indirect symbol dynamic flags are updated.  */
    938   hi = h;
    939   while (h->root.type == bfd_link_hash_indirect
    940 	 || h->root.type == bfd_link_hash_warning)
    941     h = (struct elf_link_hash_entry *) h->root.u.i.link;
    942 
    943   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
    944      existing symbol.  */
    945 
    946   oldbfd = NULL;
    947   oldsec = NULL;
    948   switch (h->root.type)
    949     {
    950     default:
    951       break;
    952 
    953     case bfd_link_hash_undefined:
    954     case bfd_link_hash_undefweak:
    955       oldbfd = h->root.u.undef.abfd;
    956       break;
    957 
    958     case bfd_link_hash_defined:
    959     case bfd_link_hash_defweak:
    960       oldbfd = h->root.u.def.section->owner;
    961       oldsec = h->root.u.def.section;
    962       break;
    963 
    964     case bfd_link_hash_common:
    965       oldbfd = h->root.u.c.p->section->owner;
    966       oldsec = h->root.u.c.p->section;
    967       if (pold_alignment)
    968 	*pold_alignment = h->root.u.c.p->alignment_power;
    969       break;
    970     }
    971   if (poldbfd && *poldbfd == NULL)
    972     *poldbfd = oldbfd;
    973 
    974   /* Differentiate strong and weak symbols.  */
    975   newweak = bind == STB_WEAK;
    976   oldweak = (h->root.type == bfd_link_hash_defweak
    977 	     || h->root.type == bfd_link_hash_undefweak);
    978   if (pold_weak)
    979     *pold_weak = oldweak;
    980 
    981   /* This code is for coping with dynamic objects, and is only useful
    982      if we are doing an ELF link.  */
    983   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
    984     return TRUE;
    985 
    986   /* We have to check it for every instance since the first few may be
    987      references and not all compilers emit symbol type for undefined
    988      symbols.  */
    989   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
    990 
    991   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
    992      respectively, is from a dynamic object.  */
    993 
    994   newdyn = (abfd->flags & DYNAMIC) != 0;
    995 
    996   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
    997      syms and defined syms in dynamic libraries respectively.
    998      ref_dynamic on the other hand can be set for a symbol defined in
    999      a dynamic library, and def_dynamic may not be set;  When the
   1000      definition in a dynamic lib is overridden by a definition in the
   1001      executable use of the symbol in the dynamic lib becomes a
   1002      reference to the executable symbol.  */
   1003   if (newdyn)
   1004     {
   1005       if (bfd_is_und_section (sec))
   1006 	{
   1007 	  if (bind != STB_WEAK)
   1008 	    {
   1009 	      h->ref_dynamic_nonweak = 1;
   1010 	      hi->ref_dynamic_nonweak = 1;
   1011 	    }
   1012 	}
   1013       else
   1014 	{
   1015 	  h->dynamic_def = 1;
   1016 	  hi->dynamic_def = 1;
   1017 	}
   1018     }
   1019 
   1020   /* If we just created the symbol, mark it as being an ELF symbol.
   1021      Other than that, there is nothing to do--there is no merge issue
   1022      with a newly defined symbol--so we just return.  */
   1023 
   1024   if (h->root.type == bfd_link_hash_new)
   1025     {
   1026       h->non_elf = 0;
   1027       return TRUE;
   1028     }
   1029 
   1030   /* In cases involving weak versioned symbols, we may wind up trying
   1031      to merge a symbol with itself.  Catch that here, to avoid the
   1032      confusion that results if we try to override a symbol with
   1033      itself.  The additional tests catch cases like
   1034      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1035      dynamic object, which we do want to handle here.  */
   1036   if (abfd == oldbfd
   1037       && (newweak || oldweak)
   1038       && ((abfd->flags & DYNAMIC) == 0
   1039 	  || !h->def_regular))
   1040     return TRUE;
   1041 
   1042   olddyn = FALSE;
   1043   if (oldbfd != NULL)
   1044     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1045   else if (oldsec != NULL)
   1046     {
   1047       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1048 	 indices used by MIPS ELF.  */
   1049       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1050     }
   1051 
   1052   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1053      respectively, appear to be a definition rather than reference.  */
   1054 
   1055   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1056 
   1057   olddef = (h->root.type != bfd_link_hash_undefined
   1058 	    && h->root.type != bfd_link_hash_undefweak
   1059 	    && h->root.type != bfd_link_hash_common);
   1060 
   1061   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1062      respectively, appear to be a function.  */
   1063 
   1064   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1065 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1066 
   1067   oldfunc = (h->type != STT_NOTYPE
   1068 	     && bed->is_function_type (h->type));
   1069 
   1070   /* When we try to create a default indirect symbol from the dynamic
   1071      definition with the default version, we skip it if its type and
   1072      the type of existing regular definition mismatch.  */
   1073   if (pold_alignment == NULL
   1074       && newdyn
   1075       && newdef
   1076       && !olddyn
   1077       && (((olddef || h->root.type == bfd_link_hash_common)
   1078 	   && ELF_ST_TYPE (sym->st_info) != h->type
   1079 	   && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1080 	   && h->type != STT_NOTYPE
   1081 	   && !(newfunc && oldfunc))
   1082 	  || (olddef
   1083 	      && ((h->type == STT_GNU_IFUNC)
   1084 		  != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
   1085     {
   1086       *skip = TRUE;
   1087       return TRUE;
   1088     }
   1089 
   1090   /* Check TLS symbols.  We don't check undefined symbols introduced
   1091      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1092      check symbols from plugins because they also have no type.  */
   1093   if (oldbfd != NULL
   1094       && (oldbfd->flags & BFD_PLUGIN) == 0
   1095       && (abfd->flags & BFD_PLUGIN) == 0
   1096       && ELF_ST_TYPE (sym->st_info) != h->type
   1097       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1098     {
   1099       bfd *ntbfd, *tbfd;
   1100       bfd_boolean ntdef, tdef;
   1101       asection *ntsec, *tsec;
   1102 
   1103       if (h->type == STT_TLS)
   1104 	{
   1105 	  ntbfd = abfd;
   1106 	  ntsec = sec;
   1107 	  ntdef = newdef;
   1108 	  tbfd = oldbfd;
   1109 	  tsec = oldsec;
   1110 	  tdef = olddef;
   1111 	}
   1112       else
   1113 	{
   1114 	  ntbfd = oldbfd;
   1115 	  ntsec = oldsec;
   1116 	  ntdef = olddef;
   1117 	  tbfd = abfd;
   1118 	  tsec = sec;
   1119 	  tdef = newdef;
   1120 	}
   1121 
   1122       if (tdef && ntdef)
   1123 	(*_bfd_error_handler)
   1124 	  (_("%s: TLS definition in %B section %A "
   1125 	     "mismatches non-TLS definition in %B section %A"),
   1126 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
   1127       else if (!tdef && !ntdef)
   1128 	(*_bfd_error_handler)
   1129 	  (_("%s: TLS reference in %B "
   1130 	     "mismatches non-TLS reference in %B"),
   1131 	   tbfd, ntbfd, h->root.root.string);
   1132       else if (tdef)
   1133 	(*_bfd_error_handler)
   1134 	  (_("%s: TLS definition in %B section %A "
   1135 	     "mismatches non-TLS reference in %B"),
   1136 	   tbfd, tsec, ntbfd, h->root.root.string);
   1137       else
   1138 	(*_bfd_error_handler)
   1139 	  (_("%s: TLS reference in %B "
   1140 	     "mismatches non-TLS definition in %B section %A"),
   1141 	   tbfd, ntbfd, ntsec, h->root.root.string);
   1142 
   1143       bfd_set_error (bfd_error_bad_value);
   1144       return FALSE;
   1145     }
   1146 
   1147   /* If the old symbol has non-default visibility, we ignore the new
   1148      definition from a dynamic object.  */
   1149   if (newdyn
   1150       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1151       && !bfd_is_und_section (sec))
   1152     {
   1153       *skip = TRUE;
   1154       /* Make sure this symbol is dynamic.  */
   1155       h->ref_dynamic = 1;
   1156       hi->ref_dynamic = 1;
   1157       /* A protected symbol has external availability. Make sure it is
   1158 	 recorded as dynamic.
   1159 
   1160 	 FIXME: Should we check type and size for protected symbol?  */
   1161       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1162 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1163       else
   1164 	return TRUE;
   1165     }
   1166   else if (!newdyn
   1167 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1168 	   && h->def_dynamic)
   1169     {
   1170       /* If the new symbol with non-default visibility comes from a
   1171 	 relocatable file and the old definition comes from a dynamic
   1172 	 object, we remove the old definition.  */
   1173       if (hi->root.type == bfd_link_hash_indirect)
   1174 	{
   1175 	  /* Handle the case where the old dynamic definition is
   1176 	     default versioned.  We need to copy the symbol info from
   1177 	     the symbol with default version to the normal one if it
   1178 	     was referenced before.  */
   1179 	  if (h->ref_regular)
   1180 	    {
   1181 	      hi->root.type = h->root.type;
   1182 	      h->root.type = bfd_link_hash_indirect;
   1183 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1184 
   1185 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1186 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1187 		{
   1188 		  /* If the new symbol is hidden or internal, completely undo
   1189 		     any dynamic link state.  */
   1190 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1191 		  h->forced_local = 0;
   1192 		  h->ref_dynamic = 0;
   1193 		}
   1194 	      else
   1195 		h->ref_dynamic = 1;
   1196 
   1197 	      h->def_dynamic = 0;
   1198 	      /* FIXME: Should we check type and size for protected symbol?  */
   1199 	      h->size = 0;
   1200 	      h->type = 0;
   1201 
   1202 	      h = hi;
   1203 	    }
   1204 	  else
   1205 	    h = hi;
   1206 	}
   1207 
   1208       /* If the old symbol was undefined before, then it will still be
   1209 	 on the undefs list.  If the new symbol is undefined or
   1210 	 common, we can't make it bfd_link_hash_new here, because new
   1211 	 undefined or common symbols will be added to the undefs list
   1212 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1213 	 added twice to the undefs list.  Also, if the new symbol is
   1214 	 undefweak then we don't want to lose the strong undef.  */
   1215       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1216 	{
   1217 	  h->root.type = bfd_link_hash_undefined;
   1218 	  h->root.u.undef.abfd = abfd;
   1219 	}
   1220       else
   1221 	{
   1222 	  h->root.type = bfd_link_hash_new;
   1223 	  h->root.u.undef.abfd = NULL;
   1224 	}
   1225 
   1226       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1227 	{
   1228 	  /* If the new symbol is hidden or internal, completely undo
   1229 	     any dynamic link state.  */
   1230 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1231 	  h->forced_local = 0;
   1232 	  h->ref_dynamic = 0;
   1233 	}
   1234       else
   1235 	h->ref_dynamic = 1;
   1236       h->def_dynamic = 0;
   1237       /* FIXME: Should we check type and size for protected symbol?  */
   1238       h->size = 0;
   1239       h->type = 0;
   1240       return TRUE;
   1241     }
   1242 
   1243   /* If a new weak symbol definition comes from a regular file and the
   1244      old symbol comes from a dynamic library, we treat the new one as
   1245      strong.  Similarly, an old weak symbol definition from a regular
   1246      file is treated as strong when the new symbol comes from a dynamic
   1247      library.  Further, an old weak symbol from a dynamic library is
   1248      treated as strong if the new symbol is from a dynamic library.
   1249      This reflects the way glibc's ld.so works.
   1250 
   1251      Do this before setting *type_change_ok or *size_change_ok so that
   1252      we warn properly when dynamic library symbols are overridden.  */
   1253 
   1254   if (newdef && !newdyn && olddyn)
   1255     newweak = FALSE;
   1256   if (olddef && newdyn)
   1257     oldweak = FALSE;
   1258 
   1259   /* Allow changes between different types of function symbol.  */
   1260   if (newfunc && oldfunc)
   1261     *type_change_ok = TRUE;
   1262 
   1263   /* It's OK to change the type if either the existing symbol or the
   1264      new symbol is weak.  A type change is also OK if the old symbol
   1265      is undefined and the new symbol is defined.  */
   1266 
   1267   if (oldweak
   1268       || newweak
   1269       || (newdef
   1270 	  && h->root.type == bfd_link_hash_undefined))
   1271     *type_change_ok = TRUE;
   1272 
   1273   /* It's OK to change the size if either the existing symbol or the
   1274      new symbol is weak, or if the old symbol is undefined.  */
   1275 
   1276   if (*type_change_ok
   1277       || h->root.type == bfd_link_hash_undefined)
   1278     *size_change_ok = TRUE;
   1279 
   1280   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1281      symbol, respectively, appears to be a common symbol in a dynamic
   1282      object.  If a symbol appears in an uninitialized section, and is
   1283      not weak, and is not a function, then it may be a common symbol
   1284      which was resolved when the dynamic object was created.  We want
   1285      to treat such symbols specially, because they raise special
   1286      considerations when setting the symbol size: if the symbol
   1287      appears as a common symbol in a regular object, and the size in
   1288      the regular object is larger, we must make sure that we use the
   1289      larger size.  This problematic case can always be avoided in C,
   1290      but it must be handled correctly when using Fortran shared
   1291      libraries.
   1292 
   1293      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1294      likewise for OLDDYNCOMMON and OLDDEF.
   1295 
   1296      Note that this test is just a heuristic, and that it is quite
   1297      possible to have an uninitialized symbol in a shared object which
   1298      is really a definition, rather than a common symbol.  This could
   1299      lead to some minor confusion when the symbol really is a common
   1300      symbol in some regular object.  However, I think it will be
   1301      harmless.  */
   1302 
   1303   if (newdyn
   1304       && newdef
   1305       && !newweak
   1306       && (sec->flags & SEC_ALLOC) != 0
   1307       && (sec->flags & SEC_LOAD) == 0
   1308       && sym->st_size > 0
   1309       && !newfunc)
   1310     newdyncommon = TRUE;
   1311   else
   1312     newdyncommon = FALSE;
   1313 
   1314   if (olddyn
   1315       && olddef
   1316       && h->root.type == bfd_link_hash_defined
   1317       && h->def_dynamic
   1318       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1319       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1320       && h->size > 0
   1321       && !oldfunc)
   1322     olddyncommon = TRUE;
   1323   else
   1324     olddyncommon = FALSE;
   1325 
   1326   /* We now know everything about the old and new symbols.  We ask the
   1327      backend to check if we can merge them.  */
   1328   if (bed->merge_symbol != NULL)
   1329     {
   1330       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1331 	return FALSE;
   1332       sec = *psec;
   1333     }
   1334 
   1335   /* If both the old and the new symbols look like common symbols in a
   1336      dynamic object, set the size of the symbol to the larger of the
   1337      two.  */
   1338 
   1339   if (olddyncommon
   1340       && newdyncommon
   1341       && sym->st_size != h->size)
   1342     {
   1343       /* Since we think we have two common symbols, issue a multiple
   1344 	 common warning if desired.  Note that we only warn if the
   1345 	 size is different.  If the size is the same, we simply let
   1346 	 the old symbol override the new one as normally happens with
   1347 	 symbols defined in dynamic objects.  */
   1348 
   1349       if (! ((*info->callbacks->multiple_common)
   1350 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
   1351 	return FALSE;
   1352 
   1353       if (sym->st_size > h->size)
   1354 	h->size = sym->st_size;
   1355 
   1356       *size_change_ok = TRUE;
   1357     }
   1358 
   1359   /* If we are looking at a dynamic object, and we have found a
   1360      definition, we need to see if the symbol was already defined by
   1361      some other object.  If so, we want to use the existing
   1362      definition, and we do not want to report a multiple symbol
   1363      definition error; we do this by clobbering *PSEC to be
   1364      bfd_und_section_ptr.
   1365 
   1366      We treat a common symbol as a definition if the symbol in the
   1367      shared library is a function, since common symbols always
   1368      represent variables; this can cause confusion in principle, but
   1369      any such confusion would seem to indicate an erroneous program or
   1370      shared library.  We also permit a common symbol in a regular
   1371      object to override a weak symbol in a shared object.  */
   1372 
   1373   if (newdyn
   1374       && newdef
   1375       && (olddef
   1376 	  || (h->root.type == bfd_link_hash_common
   1377 	      && (newweak || newfunc))))
   1378     {
   1379       *override = TRUE;
   1380       newdef = FALSE;
   1381       newdyncommon = FALSE;
   1382 
   1383       *psec = sec = bfd_und_section_ptr;
   1384       *size_change_ok = TRUE;
   1385 
   1386       /* If we get here when the old symbol is a common symbol, then
   1387 	 we are explicitly letting it override a weak symbol or
   1388 	 function in a dynamic object, and we don't want to warn about
   1389 	 a type change.  If the old symbol is a defined symbol, a type
   1390 	 change warning may still be appropriate.  */
   1391 
   1392       if (h->root.type == bfd_link_hash_common)
   1393 	*type_change_ok = TRUE;
   1394     }
   1395 
   1396   /* Handle the special case of an old common symbol merging with a
   1397      new symbol which looks like a common symbol in a shared object.
   1398      We change *PSEC and *PVALUE to make the new symbol look like a
   1399      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1400      right thing.  */
   1401 
   1402   if (newdyncommon
   1403       && h->root.type == bfd_link_hash_common)
   1404     {
   1405       *override = TRUE;
   1406       newdef = FALSE;
   1407       newdyncommon = FALSE;
   1408       *pvalue = sym->st_size;
   1409       *psec = sec = bed->common_section (oldsec);
   1410       *size_change_ok = TRUE;
   1411     }
   1412 
   1413   /* Skip weak definitions of symbols that are already defined.  */
   1414   if (newdef && olddef && newweak)
   1415     {
   1416       /* Don't skip new non-IR weak syms.  */
   1417       if (!(oldbfd != NULL
   1418 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1419 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1420 	{
   1421 	  newdef = FALSE;
   1422 	  *skip = TRUE;
   1423 	}
   1424 
   1425       /* Merge st_other.  If the symbol already has a dynamic index,
   1426 	 but visibility says it should not be visible, turn it into a
   1427 	 local symbol.  */
   1428       elf_merge_st_other (abfd, h, sym, newdef, newdyn);
   1429       if (h->dynindx != -1)
   1430 	switch (ELF_ST_VISIBILITY (h->other))
   1431 	  {
   1432 	  case STV_INTERNAL:
   1433 	  case STV_HIDDEN:
   1434 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1435 	    break;
   1436 	  }
   1437     }
   1438 
   1439   /* If the old symbol is from a dynamic object, and the new symbol is
   1440      a definition which is not from a dynamic object, then the new
   1441      symbol overrides the old symbol.  Symbols from regular files
   1442      always take precedence over symbols from dynamic objects, even if
   1443      they are defined after the dynamic object in the link.
   1444 
   1445      As above, we again permit a common symbol in a regular object to
   1446      override a definition in a shared object if the shared object
   1447      symbol is a function or is weak.  */
   1448 
   1449   flip = NULL;
   1450   if (!newdyn
   1451       && (newdef
   1452 	  || (bfd_is_com_section (sec)
   1453 	      && (oldweak || oldfunc)))
   1454       && olddyn
   1455       && olddef
   1456       && h->def_dynamic)
   1457     {
   1458       /* Change the hash table entry to undefined, and let
   1459 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1460 	 new definition.  */
   1461 
   1462       h->root.type = bfd_link_hash_undefined;
   1463       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1464       *size_change_ok = TRUE;
   1465 
   1466       olddef = FALSE;
   1467       olddyncommon = FALSE;
   1468 
   1469       /* We again permit a type change when a common symbol may be
   1470 	 overriding a function.  */
   1471 
   1472       if (bfd_is_com_section (sec))
   1473 	{
   1474 	  if (oldfunc)
   1475 	    {
   1476 	      /* If a common symbol overrides a function, make sure
   1477 		 that it isn't defined dynamically nor has type
   1478 		 function.  */
   1479 	      h->def_dynamic = 0;
   1480 	      h->type = STT_NOTYPE;
   1481 	    }
   1482 	  *type_change_ok = TRUE;
   1483 	}
   1484 
   1485       if (hi->root.type == bfd_link_hash_indirect)
   1486 	flip = hi;
   1487       else
   1488 	/* This union may have been set to be non-NULL when this symbol
   1489 	   was seen in a dynamic object.  We must force the union to be
   1490 	   NULL, so that it is correct for a regular symbol.  */
   1491 	h->verinfo.vertree = NULL;
   1492     }
   1493 
   1494   /* Handle the special case of a new common symbol merging with an
   1495      old symbol that looks like it might be a common symbol defined in
   1496      a shared object.  Note that we have already handled the case in
   1497      which a new common symbol should simply override the definition
   1498      in the shared library.  */
   1499 
   1500   if (! newdyn
   1501       && bfd_is_com_section (sec)
   1502       && olddyncommon)
   1503     {
   1504       /* It would be best if we could set the hash table entry to a
   1505 	 common symbol, but we don't know what to use for the section
   1506 	 or the alignment.  */
   1507       if (! ((*info->callbacks->multiple_common)
   1508 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
   1509 	return FALSE;
   1510 
   1511       /* If the presumed common symbol in the dynamic object is
   1512 	 larger, pretend that the new symbol has its size.  */
   1513 
   1514       if (h->size > *pvalue)
   1515 	*pvalue = h->size;
   1516 
   1517       /* We need to remember the alignment required by the symbol
   1518 	 in the dynamic object.  */
   1519       BFD_ASSERT (pold_alignment);
   1520       *pold_alignment = h->root.u.def.section->alignment_power;
   1521 
   1522       olddef = FALSE;
   1523       olddyncommon = FALSE;
   1524 
   1525       h->root.type = bfd_link_hash_undefined;
   1526       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1527 
   1528       *size_change_ok = TRUE;
   1529       *type_change_ok = TRUE;
   1530 
   1531       if (hi->root.type == bfd_link_hash_indirect)
   1532 	flip = hi;
   1533       else
   1534 	h->verinfo.vertree = NULL;
   1535     }
   1536 
   1537   if (flip != NULL)
   1538     {
   1539       /* Handle the case where we had a versioned symbol in a dynamic
   1540 	 library and now find a definition in a normal object.  In this
   1541 	 case, we make the versioned symbol point to the normal one.  */
   1542       flip->root.type = h->root.type;
   1543       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1544       h->root.type = bfd_link_hash_indirect;
   1545       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1546       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1547       if (h->def_dynamic)
   1548 	{
   1549 	  h->def_dynamic = 0;
   1550 	  flip->ref_dynamic = 1;
   1551 	}
   1552     }
   1553 
   1554   return TRUE;
   1555 }
   1556 
   1557 /* This function is called to create an indirect symbol from the
   1558    default for the symbol with the default version if needed. The
   1559    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1560    set DYNSYM if the new indirect symbol is dynamic.  */
   1561 
   1562 static bfd_boolean
   1563 _bfd_elf_add_default_symbol (bfd *abfd,
   1564 			     struct bfd_link_info *info,
   1565 			     struct elf_link_hash_entry *h,
   1566 			     const char *name,
   1567 			     Elf_Internal_Sym *sym,
   1568 			     asection *sec,
   1569 			     bfd_vma value,
   1570 			     bfd **poldbfd,
   1571 			     bfd_boolean *dynsym)
   1572 {
   1573   bfd_boolean type_change_ok;
   1574   bfd_boolean size_change_ok;
   1575   bfd_boolean skip;
   1576   char *shortname;
   1577   struct elf_link_hash_entry *hi;
   1578   struct bfd_link_hash_entry *bh;
   1579   const struct elf_backend_data *bed;
   1580   bfd_boolean collect;
   1581   bfd_boolean dynamic;
   1582   bfd_boolean override;
   1583   char *p;
   1584   size_t len, shortlen;
   1585   asection *tmp_sec;
   1586 
   1587   /* If this symbol has a version, and it is the default version, we
   1588      create an indirect symbol from the default name to the fully
   1589      decorated name.  This will cause external references which do not
   1590      specify a version to be bound to this version of the symbol.  */
   1591   p = strchr (name, ELF_VER_CHR);
   1592   if (p == NULL || p[1] != ELF_VER_CHR)
   1593     return TRUE;
   1594 
   1595   bed = get_elf_backend_data (abfd);
   1596   collect = bed->collect;
   1597   dynamic = (abfd->flags & DYNAMIC) != 0;
   1598 
   1599   shortlen = p - name;
   1600   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   1601   if (shortname == NULL)
   1602     return FALSE;
   1603   memcpy (shortname, name, shortlen);
   1604   shortname[shortlen] = '\0';
   1605 
   1606   /* We are going to create a new symbol.  Merge it with any existing
   1607      symbol with this name.  For the purposes of the merge, act as
   1608      though we were defining the symbol we just defined, although we
   1609      actually going to define an indirect symbol.  */
   1610   type_change_ok = FALSE;
   1611   size_change_ok = FALSE;
   1612   tmp_sec = sec;
   1613   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1614 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1615 			      &type_change_ok, &size_change_ok))
   1616     return FALSE;
   1617 
   1618   if (skip)
   1619     goto nondefault;
   1620 
   1621   if (! override)
   1622     {
   1623       bh = &hi->root;
   1624       if (! (_bfd_generic_link_add_one_symbol
   1625 	     (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
   1626 	      0, name, FALSE, collect, &bh)))
   1627 	return FALSE;
   1628       hi = (struct elf_link_hash_entry *) bh;
   1629     }
   1630   else
   1631     {
   1632       /* In this case the symbol named SHORTNAME is overriding the
   1633 	 indirect symbol we want to add.  We were planning on making
   1634 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   1635 	 is the name without a version.  NAME is the fully versioned
   1636 	 name, and it is the default version.
   1637 
   1638 	 Overriding means that we already saw a definition for the
   1639 	 symbol SHORTNAME in a regular object, and it is overriding
   1640 	 the symbol defined in the dynamic object.
   1641 
   1642 	 When this happens, we actually want to change NAME, the
   1643 	 symbol we just added, to refer to SHORTNAME.  This will cause
   1644 	 references to NAME in the shared object to become references
   1645 	 to SHORTNAME in the regular object.  This is what we expect
   1646 	 when we override a function in a shared object: that the
   1647 	 references in the shared object will be mapped to the
   1648 	 definition in the regular object.  */
   1649 
   1650       while (hi->root.type == bfd_link_hash_indirect
   1651 	     || hi->root.type == bfd_link_hash_warning)
   1652 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1653 
   1654       h->root.type = bfd_link_hash_indirect;
   1655       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1656       if (h->def_dynamic)
   1657 	{
   1658 	  h->def_dynamic = 0;
   1659 	  hi->ref_dynamic = 1;
   1660 	  if (hi->ref_regular
   1661 	      || hi->def_regular)
   1662 	    {
   1663 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   1664 		return FALSE;
   1665 	    }
   1666 	}
   1667 
   1668       /* Now set HI to H, so that the following code will set the
   1669 	 other fields correctly.  */
   1670       hi = h;
   1671     }
   1672 
   1673   /* Check if HI is a warning symbol.  */
   1674   if (hi->root.type == bfd_link_hash_warning)
   1675     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1676 
   1677   /* If there is a duplicate definition somewhere, then HI may not
   1678      point to an indirect symbol.  We will have reported an error to
   1679      the user in that case.  */
   1680 
   1681   if (hi->root.type == bfd_link_hash_indirect)
   1682     {
   1683       struct elf_link_hash_entry *ht;
   1684 
   1685       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1686       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   1687 
   1688       /* A reference to the SHORTNAME symbol from a dynamic library
   1689 	 will be satisfied by the versioned symbol at runtime.  In
   1690 	 effect, we have a reference to the versioned symbol.  */
   1691       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   1692       hi->dynamic_def |= ht->dynamic_def;
   1693 
   1694       /* See if the new flags lead us to realize that the symbol must
   1695 	 be dynamic.  */
   1696       if (! *dynsym)
   1697 	{
   1698 	  if (! dynamic)
   1699 	    {
   1700 	      if (! info->executable
   1701 		  || hi->def_dynamic
   1702 		  || hi->ref_dynamic)
   1703 		*dynsym = TRUE;
   1704 	    }
   1705 	  else
   1706 	    {
   1707 	      if (hi->ref_regular)
   1708 		*dynsym = TRUE;
   1709 	    }
   1710 	}
   1711     }
   1712 
   1713   /* We also need to define an indirection from the nondefault version
   1714      of the symbol.  */
   1715 
   1716 nondefault:
   1717   len = strlen (name);
   1718   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   1719   if (shortname == NULL)
   1720     return FALSE;
   1721   memcpy (shortname, name, shortlen);
   1722   memcpy (shortname + shortlen, p + 1, len - shortlen);
   1723 
   1724   /* Once again, merge with any existing symbol.  */
   1725   type_change_ok = FALSE;
   1726   size_change_ok = FALSE;
   1727   tmp_sec = sec;
   1728   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1729 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1730 			      &type_change_ok, &size_change_ok))
   1731     return FALSE;
   1732 
   1733   if (skip)
   1734     return TRUE;
   1735 
   1736   if (override)
   1737     {
   1738       /* Here SHORTNAME is a versioned name, so we don't expect to see
   1739 	 the type of override we do in the case above unless it is
   1740 	 overridden by a versioned definition.  */
   1741       if (hi->root.type != bfd_link_hash_defined
   1742 	  && hi->root.type != bfd_link_hash_defweak)
   1743 	(*_bfd_error_handler)
   1744 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
   1745 	   abfd, shortname);
   1746     }
   1747   else
   1748     {
   1749       bh = &hi->root;
   1750       if (! (_bfd_generic_link_add_one_symbol
   1751 	     (info, abfd, shortname, BSF_INDIRECT,
   1752 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
   1753 	return FALSE;
   1754       hi = (struct elf_link_hash_entry *) bh;
   1755 
   1756       /* If there is a duplicate definition somewhere, then HI may not
   1757 	 point to an indirect symbol.  We will have reported an error
   1758 	 to the user in that case.  */
   1759 
   1760       if (hi->root.type == bfd_link_hash_indirect)
   1761 	{
   1762 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   1763 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   1764 	  hi->dynamic_def |= h->dynamic_def;
   1765 
   1766 	  /* See if the new flags lead us to realize that the symbol
   1767 	     must be dynamic.  */
   1768 	  if (! *dynsym)
   1769 	    {
   1770 	      if (! dynamic)
   1771 		{
   1772 		  if (! info->executable
   1773 		      || hi->ref_dynamic)
   1774 		    *dynsym = TRUE;
   1775 		}
   1776 	      else
   1777 		{
   1778 		  if (hi->ref_regular)
   1779 		    *dynsym = TRUE;
   1780 		}
   1781 	    }
   1782 	}
   1783     }
   1784 
   1785   return TRUE;
   1786 }
   1787 
   1788 /* This routine is used to export all defined symbols into the dynamic
   1790    symbol table.  It is called via elf_link_hash_traverse.  */
   1791 
   1792 static bfd_boolean
   1793 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   1794 {
   1795   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   1796 
   1797   /* Ignore indirect symbols.  These are added by the versioning code.  */
   1798   if (h->root.type == bfd_link_hash_indirect)
   1799     return TRUE;
   1800 
   1801   /* Ignore this if we won't export it.  */
   1802   if (!eif->info->export_dynamic && !h->dynamic)
   1803     return TRUE;
   1804 
   1805   if (h->dynindx == -1
   1806       && (h->def_regular || h->ref_regular)
   1807       && ! bfd_hide_sym_by_version (eif->info->version_info,
   1808 				    h->root.root.string))
   1809     {
   1810       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   1811 	{
   1812 	  eif->failed = TRUE;
   1813 	  return FALSE;
   1814 	}
   1815     }
   1816 
   1817   return TRUE;
   1818 }
   1819 
   1820 /* Look through the symbols which are defined in other shared
   1822    libraries and referenced here.  Update the list of version
   1823    dependencies.  This will be put into the .gnu.version_r section.
   1824    This function is called via elf_link_hash_traverse.  */
   1825 
   1826 static bfd_boolean
   1827 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   1828 					 void *data)
   1829 {
   1830   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   1831   Elf_Internal_Verneed *t;
   1832   Elf_Internal_Vernaux *a;
   1833   bfd_size_type amt;
   1834 
   1835   /* We only care about symbols defined in shared objects with version
   1836      information.  */
   1837   if (!h->def_dynamic
   1838       || h->def_regular
   1839       || h->dynindx == -1
   1840       || h->verinfo.verdef == NULL)
   1841     return TRUE;
   1842 
   1843   /* See if we already know about this version.  */
   1844   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   1845        t != NULL;
   1846        t = t->vn_nextref)
   1847     {
   1848       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   1849 	continue;
   1850 
   1851       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1852 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   1853 	  return TRUE;
   1854 
   1855       break;
   1856     }
   1857 
   1858   /* This is a new version.  Add it to tree we are building.  */
   1859 
   1860   if (t == NULL)
   1861     {
   1862       amt = sizeof *t;
   1863       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   1864       if (t == NULL)
   1865 	{
   1866 	  rinfo->failed = TRUE;
   1867 	  return FALSE;
   1868 	}
   1869 
   1870       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   1871       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   1872       elf_tdata (rinfo->info->output_bfd)->verref = t;
   1873     }
   1874 
   1875   amt = sizeof *a;
   1876   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   1877   if (a == NULL)
   1878     {
   1879       rinfo->failed = TRUE;
   1880       return FALSE;
   1881     }
   1882 
   1883   /* Note that we are copying a string pointer here, and testing it
   1884      above.  If bfd_elf_string_from_elf_section is ever changed to
   1885      discard the string data when low in memory, this will have to be
   1886      fixed.  */
   1887   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   1888 
   1889   a->vna_flags = h->verinfo.verdef->vd_flags;
   1890   a->vna_nextptr = t->vn_auxptr;
   1891 
   1892   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   1893   ++rinfo->vers;
   1894 
   1895   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   1896 
   1897   t->vn_auxptr = a;
   1898 
   1899   return TRUE;
   1900 }
   1901 
   1902 /* Figure out appropriate versions for all the symbols.  We may not
   1903    have the version number script until we have read all of the input
   1904    files, so until that point we don't know which symbols should be
   1905    local.  This function is called via elf_link_hash_traverse.  */
   1906 
   1907 static bfd_boolean
   1908 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   1909 {
   1910   struct elf_info_failed *sinfo;
   1911   struct bfd_link_info *info;
   1912   const struct elf_backend_data *bed;
   1913   struct elf_info_failed eif;
   1914   char *p;
   1915   bfd_size_type amt;
   1916 
   1917   sinfo = (struct elf_info_failed *) data;
   1918   info = sinfo->info;
   1919 
   1920   /* Fix the symbol flags.  */
   1921   eif.failed = FALSE;
   1922   eif.info = info;
   1923   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   1924     {
   1925       if (eif.failed)
   1926 	sinfo->failed = TRUE;
   1927       return FALSE;
   1928     }
   1929 
   1930   /* We only need version numbers for symbols defined in regular
   1931      objects.  */
   1932   if (!h->def_regular)
   1933     return TRUE;
   1934 
   1935   bed = get_elf_backend_data (info->output_bfd);
   1936   p = strchr (h->root.root.string, ELF_VER_CHR);
   1937   if (p != NULL && h->verinfo.vertree == NULL)
   1938     {
   1939       struct bfd_elf_version_tree *t;
   1940       bfd_boolean hidden;
   1941 
   1942       hidden = TRUE;
   1943 
   1944       /* There are two consecutive ELF_VER_CHR characters if this is
   1945 	 not a hidden symbol.  */
   1946       ++p;
   1947       if (*p == ELF_VER_CHR)
   1948 	{
   1949 	  hidden = FALSE;
   1950 	  ++p;
   1951 	}
   1952 
   1953       /* If there is no version string, we can just return out.  */
   1954       if (*p == '\0')
   1955 	{
   1956 	  if (hidden)
   1957 	    h->hidden = 1;
   1958 	  return TRUE;
   1959 	}
   1960 
   1961       /* Look for the version.  If we find it, it is no longer weak.  */
   1962       for (t = sinfo->info->version_info; t != NULL; t = t->next)
   1963 	{
   1964 	  if (strcmp (t->name, p) == 0)
   1965 	    {
   1966 	      size_t len;
   1967 	      char *alc;
   1968 	      struct bfd_elf_version_expr *d;
   1969 
   1970 	      len = p - h->root.root.string;
   1971 	      alc = (char *) bfd_malloc (len);
   1972 	      if (alc == NULL)
   1973 		{
   1974 		  sinfo->failed = TRUE;
   1975 		  return FALSE;
   1976 		}
   1977 	      memcpy (alc, h->root.root.string, len - 1);
   1978 	      alc[len - 1] = '\0';
   1979 	      if (alc[len - 2] == ELF_VER_CHR)
   1980 		alc[len - 2] = '\0';
   1981 
   1982 	      h->verinfo.vertree = t;
   1983 	      t->used = TRUE;
   1984 	      d = NULL;
   1985 
   1986 	      if (t->globals.list != NULL)
   1987 		d = (*t->match) (&t->globals, NULL, alc);
   1988 
   1989 	      /* See if there is anything to force this symbol to
   1990 		 local scope.  */
   1991 	      if (d == NULL && t->locals.list != NULL)
   1992 		{
   1993 		  d = (*t->match) (&t->locals, NULL, alc);
   1994 		  if (d != NULL
   1995 		      && h->dynindx != -1
   1996 		      && ! info->export_dynamic)
   1997 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1998 		}
   1999 
   2000 	      free (alc);
   2001 	      break;
   2002 	    }
   2003 	}
   2004 
   2005       /* If we are building an application, we need to create a
   2006 	 version node for this version.  */
   2007       if (t == NULL && info->executable)
   2008 	{
   2009 	  struct bfd_elf_version_tree **pp;
   2010 	  int version_index;
   2011 
   2012 	  /* If we aren't going to export this symbol, we don't need
   2013 	     to worry about it.  */
   2014 	  if (h->dynindx == -1)
   2015 	    return TRUE;
   2016 
   2017 	  amt = sizeof *t;
   2018 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
   2019 	  if (t == NULL)
   2020 	    {
   2021 	      sinfo->failed = TRUE;
   2022 	      return FALSE;
   2023 	    }
   2024 
   2025 	  t->name = p;
   2026 	  t->name_indx = (unsigned int) -1;
   2027 	  t->used = TRUE;
   2028 
   2029 	  version_index = 1;
   2030 	  /* Don't count anonymous version tag.  */
   2031 	  if (sinfo->info->version_info != NULL
   2032 	      && sinfo->info->version_info->vernum == 0)
   2033 	    version_index = 0;
   2034 	  for (pp = &sinfo->info->version_info;
   2035 	       *pp != NULL;
   2036 	       pp = &(*pp)->next)
   2037 	    ++version_index;
   2038 	  t->vernum = version_index;
   2039 
   2040 	  *pp = t;
   2041 
   2042 	  h->verinfo.vertree = t;
   2043 	}
   2044       else if (t == NULL)
   2045 	{
   2046 	  /* We could not find the version for a symbol when
   2047 	     generating a shared archive.  Return an error.  */
   2048 	  (*_bfd_error_handler)
   2049 	    (_("%B: version node not found for symbol %s"),
   2050 	     info->output_bfd, h->root.root.string);
   2051 	  bfd_set_error (bfd_error_bad_value);
   2052 	  sinfo->failed = TRUE;
   2053 	  return FALSE;
   2054 	}
   2055 
   2056       if (hidden)
   2057 	h->hidden = 1;
   2058     }
   2059 
   2060   /* If we don't have a version for this symbol, see if we can find
   2061      something.  */
   2062   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
   2063     {
   2064       bfd_boolean hide;
   2065 
   2066       h->verinfo.vertree
   2067 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2068 				    h->root.root.string, &hide);
   2069       if (h->verinfo.vertree != NULL && hide)
   2070 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2071     }
   2072 
   2073   return TRUE;
   2074 }
   2075 
   2076 /* Read and swap the relocs from the section indicated by SHDR.  This
   2078    may be either a REL or a RELA section.  The relocations are
   2079    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2080    which should have already been allocated to contain enough space.
   2081    The EXTERNAL_RELOCS are a buffer where the external form of the
   2082    relocations should be stored.
   2083 
   2084    Returns FALSE if something goes wrong.  */
   2085 
   2086 static bfd_boolean
   2087 elf_link_read_relocs_from_section (bfd *abfd,
   2088 				   asection *sec,
   2089 				   Elf_Internal_Shdr *shdr,
   2090 				   void *external_relocs,
   2091 				   Elf_Internal_Rela *internal_relocs)
   2092 {
   2093   const struct elf_backend_data *bed;
   2094   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2095   const bfd_byte *erela;
   2096   const bfd_byte *erelaend;
   2097   Elf_Internal_Rela *irela;
   2098   Elf_Internal_Shdr *symtab_hdr;
   2099   size_t nsyms;
   2100 
   2101   /* Position ourselves at the start of the section.  */
   2102   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2103     return FALSE;
   2104 
   2105   /* Read the relocations.  */
   2106   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
   2107     return FALSE;
   2108 
   2109   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2110   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2111 
   2112   bed = get_elf_backend_data (abfd);
   2113 
   2114   /* Convert the external relocations to the internal format.  */
   2115   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2116     swap_in = bed->s->swap_reloc_in;
   2117   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2118     swap_in = bed->s->swap_reloca_in;
   2119   else
   2120     {
   2121       bfd_set_error (bfd_error_wrong_format);
   2122       return FALSE;
   2123     }
   2124 
   2125   erela = (const bfd_byte *) external_relocs;
   2126   erelaend = erela + shdr->sh_size;
   2127   irela = internal_relocs;
   2128   while (erela < erelaend)
   2129     {
   2130       bfd_vma r_symndx;
   2131 
   2132       (*swap_in) (abfd, erela, irela);
   2133       r_symndx = ELF32_R_SYM (irela->r_info);
   2134       if (bed->s->arch_size == 64)
   2135 	r_symndx >>= 24;
   2136       if (nsyms > 0)
   2137 	{
   2138 	  if ((size_t) r_symndx >= nsyms)
   2139 	    {
   2140 	      (*_bfd_error_handler)
   2141 		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
   2142 		   " for offset 0x%lx in section `%A'"),
   2143 		 abfd, sec,
   2144 		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
   2145 	      bfd_set_error (bfd_error_bad_value);
   2146 	      return FALSE;
   2147 	    }
   2148 	}
   2149       else if (r_symndx != STN_UNDEF)
   2150 	{
   2151 	  (*_bfd_error_handler)
   2152 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
   2153 	       " when the object file has no symbol table"),
   2154 	     abfd, sec,
   2155 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
   2156 	  bfd_set_error (bfd_error_bad_value);
   2157 	  return FALSE;
   2158 	}
   2159       irela += bed->s->int_rels_per_ext_rel;
   2160       erela += shdr->sh_entsize;
   2161     }
   2162 
   2163   return TRUE;
   2164 }
   2165 
   2166 /* Read and swap the relocs for a section O.  They may have been
   2167    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2168    not NULL, they are used as buffers to read into.  They are known to
   2169    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2170    the return value is allocated using either malloc or bfd_alloc,
   2171    according to the KEEP_MEMORY argument.  If O has two relocation
   2172    sections (both REL and RELA relocations), then the REL_HDR
   2173    relocations will appear first in INTERNAL_RELOCS, followed by the
   2174    RELA_HDR relocations.  */
   2175 
   2176 Elf_Internal_Rela *
   2177 _bfd_elf_link_read_relocs (bfd *abfd,
   2178 			   asection *o,
   2179 			   void *external_relocs,
   2180 			   Elf_Internal_Rela *internal_relocs,
   2181 			   bfd_boolean keep_memory)
   2182 {
   2183   void *alloc1 = NULL;
   2184   Elf_Internal_Rela *alloc2 = NULL;
   2185   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2186   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2187   Elf_Internal_Rela *internal_rela_relocs;
   2188 
   2189   if (esdo->relocs != NULL)
   2190     return esdo->relocs;
   2191 
   2192   if (o->reloc_count == 0)
   2193     return NULL;
   2194 
   2195   if (internal_relocs == NULL)
   2196     {
   2197       bfd_size_type size;
   2198 
   2199       size = o->reloc_count;
   2200       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
   2201       if (keep_memory)
   2202 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
   2203       else
   2204 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2205       if (internal_relocs == NULL)
   2206 	goto error_return;
   2207     }
   2208 
   2209   if (external_relocs == NULL)
   2210     {
   2211       bfd_size_type size = 0;
   2212 
   2213       if (esdo->rel.hdr)
   2214 	size += esdo->rel.hdr->sh_size;
   2215       if (esdo->rela.hdr)
   2216 	size += esdo->rela.hdr->sh_size;
   2217 
   2218       alloc1 = bfd_malloc (size);
   2219       if (alloc1 == NULL)
   2220 	goto error_return;
   2221       external_relocs = alloc1;
   2222     }
   2223 
   2224   internal_rela_relocs = internal_relocs;
   2225   if (esdo->rel.hdr)
   2226     {
   2227       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2228 					      external_relocs,
   2229 					      internal_relocs))
   2230 	goto error_return;
   2231       external_relocs = (((bfd_byte *) external_relocs)
   2232 			 + esdo->rel.hdr->sh_size);
   2233       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2234 			       * bed->s->int_rels_per_ext_rel);
   2235     }
   2236 
   2237   if (esdo->rela.hdr
   2238       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2239 					      external_relocs,
   2240 					      internal_rela_relocs)))
   2241     goto error_return;
   2242 
   2243   /* Cache the results for next time, if we can.  */
   2244   if (keep_memory)
   2245     esdo->relocs = internal_relocs;
   2246 
   2247   if (alloc1 != NULL)
   2248     free (alloc1);
   2249 
   2250   /* Don't free alloc2, since if it was allocated we are passing it
   2251      back (under the name of internal_relocs).  */
   2252 
   2253   return internal_relocs;
   2254 
   2255  error_return:
   2256   if (alloc1 != NULL)
   2257     free (alloc1);
   2258   if (alloc2 != NULL)
   2259     {
   2260       if (keep_memory)
   2261 	bfd_release (abfd, alloc2);
   2262       else
   2263 	free (alloc2);
   2264     }
   2265   return NULL;
   2266 }
   2267 
   2268 /* Compute the size of, and allocate space for, REL_HDR which is the
   2269    section header for a section containing relocations for O.  */
   2270 
   2271 static bfd_boolean
   2272 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2273 				  struct bfd_elf_section_reloc_data *reldata)
   2274 {
   2275   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2276 
   2277   /* That allows us to calculate the size of the section.  */
   2278   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2279 
   2280   /* The contents field must last into write_object_contents, so we
   2281      allocate it with bfd_alloc rather than malloc.  Also since we
   2282      cannot be sure that the contents will actually be filled in,
   2283      we zero the allocated space.  */
   2284   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2285   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2286     return FALSE;
   2287 
   2288   if (reldata->hashes == NULL && reldata->count)
   2289     {
   2290       struct elf_link_hash_entry **p;
   2291 
   2292       p = (struct elf_link_hash_entry **)
   2293           bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
   2294       if (p == NULL)
   2295 	return FALSE;
   2296 
   2297       reldata->hashes = p;
   2298     }
   2299 
   2300   return TRUE;
   2301 }
   2302 
   2303 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2304    originated from the section given by INPUT_REL_HDR) to the
   2305    OUTPUT_BFD.  */
   2306 
   2307 bfd_boolean
   2308 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2309 			     asection *input_section,
   2310 			     Elf_Internal_Shdr *input_rel_hdr,
   2311 			     Elf_Internal_Rela *internal_relocs,
   2312 			     struct elf_link_hash_entry **rel_hash
   2313 			       ATTRIBUTE_UNUSED)
   2314 {
   2315   Elf_Internal_Rela *irela;
   2316   Elf_Internal_Rela *irelaend;
   2317   bfd_byte *erel;
   2318   struct bfd_elf_section_reloc_data *output_reldata;
   2319   asection *output_section;
   2320   const struct elf_backend_data *bed;
   2321   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   2322   struct bfd_elf_section_data *esdo;
   2323 
   2324   output_section = input_section->output_section;
   2325 
   2326   bed = get_elf_backend_data (output_bfd);
   2327   esdo = elf_section_data (output_section);
   2328   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2329     {
   2330       output_reldata = &esdo->rel;
   2331       swap_out = bed->s->swap_reloc_out;
   2332     }
   2333   else if (esdo->rela.hdr
   2334 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2335     {
   2336       output_reldata = &esdo->rela;
   2337       swap_out = bed->s->swap_reloca_out;
   2338     }
   2339   else
   2340     {
   2341       (*_bfd_error_handler)
   2342 	(_("%B: relocation size mismatch in %B section %A"),
   2343 	 output_bfd, input_section->owner, input_section);
   2344       bfd_set_error (bfd_error_wrong_format);
   2345       return FALSE;
   2346     }
   2347 
   2348   erel = output_reldata->hdr->contents;
   2349   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   2350   irela = internal_relocs;
   2351   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   2352 		      * bed->s->int_rels_per_ext_rel);
   2353   while (irela < irelaend)
   2354     {
   2355       (*swap_out) (output_bfd, irela, erel);
   2356       irela += bed->s->int_rels_per_ext_rel;
   2357       erel += input_rel_hdr->sh_entsize;
   2358     }
   2359 
   2360   /* Bump the counter, so that we know where to add the next set of
   2361      relocations.  */
   2362   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   2363 
   2364   return TRUE;
   2365 }
   2366 
   2367 /* Make weak undefined symbols in PIE dynamic.  */
   2369 
   2370 bfd_boolean
   2371 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   2372 				 struct elf_link_hash_entry *h)
   2373 {
   2374   if (info->pie
   2375       && h->dynindx == -1
   2376       && h->root.type == bfd_link_hash_undefweak)
   2377     return bfd_elf_link_record_dynamic_symbol (info, h);
   2378 
   2379   return TRUE;
   2380 }
   2381 
   2382 /* Fix up the flags for a symbol.  This handles various cases which
   2383    can only be fixed after all the input files are seen.  This is
   2384    currently called by both adjust_dynamic_symbol and
   2385    assign_sym_version, which is unnecessary but perhaps more robust in
   2386    the face of future changes.  */
   2387 
   2388 static bfd_boolean
   2389 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   2390 			   struct elf_info_failed *eif)
   2391 {
   2392   const struct elf_backend_data *bed;
   2393 
   2394   /* If this symbol was mentioned in a non-ELF file, try to set
   2395      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   2396      permit a non-ELF file to correctly refer to a symbol defined in
   2397      an ELF dynamic object.  */
   2398   if (h->non_elf)
   2399     {
   2400       while (h->root.type == bfd_link_hash_indirect)
   2401 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2402 
   2403       if (h->root.type != bfd_link_hash_defined
   2404 	  && h->root.type != bfd_link_hash_defweak)
   2405 	{
   2406 	  h->ref_regular = 1;
   2407 	  h->ref_regular_nonweak = 1;
   2408 	}
   2409       else
   2410 	{
   2411 	  if (h->root.u.def.section->owner != NULL
   2412 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   2413 		  == bfd_target_elf_flavour))
   2414 	    {
   2415 	      h->ref_regular = 1;
   2416 	      h->ref_regular_nonweak = 1;
   2417 	    }
   2418 	  else
   2419 	    h->def_regular = 1;
   2420 	}
   2421 
   2422       if (h->dynindx == -1
   2423 	  && (h->def_dynamic
   2424 	      || h->ref_dynamic))
   2425 	{
   2426 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2427 	    {
   2428 	      eif->failed = TRUE;
   2429 	      return FALSE;
   2430 	    }
   2431 	}
   2432     }
   2433   else
   2434     {
   2435       /* Unfortunately, NON_ELF is only correct if the symbol
   2436 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   2437 	 was first seen in an ELF file, we're probably OK unless the
   2438 	 symbol was defined in a non-ELF file.  Catch that case here.
   2439 	 FIXME: We're still in trouble if the symbol was first seen in
   2440 	 a dynamic object, and then later in a non-ELF regular object.  */
   2441       if ((h->root.type == bfd_link_hash_defined
   2442 	   || h->root.type == bfd_link_hash_defweak)
   2443 	  && !h->def_regular
   2444 	  && (h->root.u.def.section->owner != NULL
   2445 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   2446 		 != bfd_target_elf_flavour)
   2447 	      : (bfd_is_abs_section (h->root.u.def.section)
   2448 		 && !h->def_dynamic)))
   2449 	h->def_regular = 1;
   2450     }
   2451 
   2452   /* Backend specific symbol fixup.  */
   2453   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   2454   if (bed->elf_backend_fixup_symbol
   2455       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   2456     return FALSE;
   2457 
   2458   /* If this is a final link, and the symbol was defined as a common
   2459      symbol in a regular object file, and there was no definition in
   2460      any dynamic object, then the linker will have allocated space for
   2461      the symbol in a common section but the DEF_REGULAR
   2462      flag will not have been set.  */
   2463   if (h->root.type == bfd_link_hash_defined
   2464       && !h->def_regular
   2465       && h->ref_regular
   2466       && !h->def_dynamic
   2467       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   2468     h->def_regular = 1;
   2469 
   2470   /* If -Bsymbolic was used (which means to bind references to global
   2471      symbols to the definition within the shared object), and this
   2472      symbol was defined in a regular object, then it actually doesn't
   2473      need a PLT entry.  Likewise, if the symbol has non-default
   2474      visibility.  If the symbol has hidden or internal visibility, we
   2475      will force it local.  */
   2476   if (h->needs_plt
   2477       && eif->info->shared
   2478       && is_elf_hash_table (eif->info->hash)
   2479       && (SYMBOLIC_BIND (eif->info, h)
   2480 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2481       && h->def_regular)
   2482     {
   2483       bfd_boolean force_local;
   2484 
   2485       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   2486 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   2487       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   2488     }
   2489 
   2490   /* If a weak undefined symbol has non-default visibility, we also
   2491      hide it from the dynamic linker.  */
   2492   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2493       && h->root.type == bfd_link_hash_undefweak)
   2494     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2495 
   2496   /* If this is a weak defined symbol in a dynamic object, and we know
   2497      the real definition in the dynamic object, copy interesting flags
   2498      over to the real definition.  */
   2499   if (h->u.weakdef != NULL)
   2500     {
   2501       /* If the real definition is defined by a regular object file,
   2502 	 don't do anything special.  See the longer description in
   2503 	 _bfd_elf_adjust_dynamic_symbol, below.  */
   2504       if (h->u.weakdef->def_regular)
   2505 	h->u.weakdef = NULL;
   2506       else
   2507 	{
   2508 	  struct elf_link_hash_entry *weakdef = h->u.weakdef;
   2509 
   2510 	  while (h->root.type == bfd_link_hash_indirect)
   2511 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2512 
   2513 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   2514 		      || h->root.type == bfd_link_hash_defweak);
   2515 	  BFD_ASSERT (weakdef->def_dynamic);
   2516 	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
   2517 		      || weakdef->root.type == bfd_link_hash_defweak);
   2518 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
   2519 	}
   2520     }
   2521 
   2522   return TRUE;
   2523 }
   2524 
   2525 /* Make the backend pick a good value for a dynamic symbol.  This is
   2526    called via elf_link_hash_traverse, and also calls itself
   2527    recursively.  */
   2528 
   2529 static bfd_boolean
   2530 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   2531 {
   2532   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2533   bfd *dynobj;
   2534   const struct elf_backend_data *bed;
   2535 
   2536   if (! is_elf_hash_table (eif->info->hash))
   2537     return FALSE;
   2538 
   2539   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2540   if (h->root.type == bfd_link_hash_indirect)
   2541     return TRUE;
   2542 
   2543   /* Fix the symbol flags.  */
   2544   if (! _bfd_elf_fix_symbol_flags (h, eif))
   2545     return FALSE;
   2546 
   2547   /* If this symbol does not require a PLT entry, and it is not
   2548      defined by a dynamic object, or is not referenced by a regular
   2549      object, ignore it.  We do have to handle a weak defined symbol,
   2550      even if no regular object refers to it, if we decided to add it
   2551      to the dynamic symbol table.  FIXME: Do we normally need to worry
   2552      about symbols which are defined by one dynamic object and
   2553      referenced by another one?  */
   2554   if (!h->needs_plt
   2555       && h->type != STT_GNU_IFUNC
   2556       && (h->def_regular
   2557 	  || !h->def_dynamic
   2558 	  || (!h->ref_regular
   2559 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
   2560     {
   2561       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   2562       return TRUE;
   2563     }
   2564 
   2565   /* If we've already adjusted this symbol, don't do it again.  This
   2566      can happen via a recursive call.  */
   2567   if (h->dynamic_adjusted)
   2568     return TRUE;
   2569 
   2570   /* Don't look at this symbol again.  Note that we must set this
   2571      after checking the above conditions, because we may look at a
   2572      symbol once, decide not to do anything, and then get called
   2573      recursively later after REF_REGULAR is set below.  */
   2574   h->dynamic_adjusted = 1;
   2575 
   2576   /* If this is a weak definition, and we know a real definition, and
   2577      the real symbol is not itself defined by a regular object file,
   2578      then get a good value for the real definition.  We handle the
   2579      real symbol first, for the convenience of the backend routine.
   2580 
   2581      Note that there is a confusing case here.  If the real definition
   2582      is defined by a regular object file, we don't get the real symbol
   2583      from the dynamic object, but we do get the weak symbol.  If the
   2584      processor backend uses a COPY reloc, then if some routine in the
   2585      dynamic object changes the real symbol, we will not see that
   2586      change in the corresponding weak symbol.  This is the way other
   2587      ELF linkers work as well, and seems to be a result of the shared
   2588      library model.
   2589 
   2590      I will clarify this issue.  Most SVR4 shared libraries define the
   2591      variable _timezone and define timezone as a weak synonym.  The
   2592      tzset call changes _timezone.  If you write
   2593        extern int timezone;
   2594        int _timezone = 5;
   2595        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   2596      you might expect that, since timezone is a synonym for _timezone,
   2597      the same number will print both times.  However, if the processor
   2598      backend uses a COPY reloc, then actually timezone will be copied
   2599      into your process image, and, since you define _timezone
   2600      yourself, _timezone will not.  Thus timezone and _timezone will
   2601      wind up at different memory locations.  The tzset call will set
   2602      _timezone, leaving timezone unchanged.  */
   2603 
   2604   if (h->u.weakdef != NULL)
   2605     {
   2606       /* If we get to this point, there is an implicit reference to
   2607 	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
   2608       h->u.weakdef->ref_regular = 1;
   2609 
   2610       /* Ensure that the backend adjust_dynamic_symbol function sees
   2611 	 H->U.WEAKDEF before H by recursively calling ourselves.  */
   2612       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
   2613 	return FALSE;
   2614     }
   2615 
   2616   /* If a symbol has no type and no size and does not require a PLT
   2617      entry, then we are probably about to do the wrong thing here: we
   2618      are probably going to create a COPY reloc for an empty object.
   2619      This case can arise when a shared object is built with assembly
   2620      code, and the assembly code fails to set the symbol type.  */
   2621   if (h->size == 0
   2622       && h->type == STT_NOTYPE
   2623       && !h->needs_plt)
   2624     (*_bfd_error_handler)
   2625       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   2626        h->root.root.string);
   2627 
   2628   dynobj = elf_hash_table (eif->info)->dynobj;
   2629   bed = get_elf_backend_data (dynobj);
   2630 
   2631   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   2632     {
   2633       eif->failed = TRUE;
   2634       return FALSE;
   2635     }
   2636 
   2637   return TRUE;
   2638 }
   2639 
   2640 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   2641    DYNBSS.  */
   2642 
   2643 bfd_boolean
   2644 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
   2645 			      asection *dynbss)
   2646 {
   2647   unsigned int power_of_two;
   2648   bfd_vma mask;
   2649   asection *sec = h->root.u.def.section;
   2650 
   2651   /* The section aligment of definition is the maximum alignment
   2652      requirement of symbols defined in the section.  Since we don't
   2653      know the symbol alignment requirement, we start with the
   2654      maximum alignment and check low bits of the symbol address
   2655      for the minimum alignment.  */
   2656   power_of_two = bfd_get_section_alignment (sec->owner, sec);
   2657   mask = ((bfd_vma) 1 << power_of_two) - 1;
   2658   while ((h->root.u.def.value & mask) != 0)
   2659     {
   2660        mask >>= 1;
   2661        --power_of_two;
   2662     }
   2663 
   2664   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
   2665 						dynbss))
   2666     {
   2667       /* Adjust the section alignment if needed.  */
   2668       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
   2669 				       power_of_two))
   2670 	return FALSE;
   2671     }
   2672 
   2673   /* We make sure that the symbol will be aligned properly.  */
   2674   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   2675 
   2676   /* Define the symbol as being at this point in DYNBSS.  */
   2677   h->root.u.def.section = dynbss;
   2678   h->root.u.def.value = dynbss->size;
   2679 
   2680   /* Increment the size of DYNBSS to make room for the symbol.  */
   2681   dynbss->size += h->size;
   2682 
   2683   return TRUE;
   2684 }
   2685 
   2686 /* Adjust all external symbols pointing into SEC_MERGE sections
   2687    to reflect the object merging within the sections.  */
   2688 
   2689 static bfd_boolean
   2690 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   2691 {
   2692   asection *sec;
   2693 
   2694   if ((h->root.type == bfd_link_hash_defined
   2695        || h->root.type == bfd_link_hash_defweak)
   2696       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   2697       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   2698     {
   2699       bfd *output_bfd = (bfd *) data;
   2700 
   2701       h->root.u.def.value =
   2702 	_bfd_merged_section_offset (output_bfd,
   2703 				    &h->root.u.def.section,
   2704 				    elf_section_data (sec)->sec_info,
   2705 				    h->root.u.def.value);
   2706     }
   2707 
   2708   return TRUE;
   2709 }
   2710 
   2711 /* Returns false if the symbol referred to by H should be considered
   2712    to resolve local to the current module, and true if it should be
   2713    considered to bind dynamically.  */
   2714 
   2715 bfd_boolean
   2716 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   2717 			   struct bfd_link_info *info,
   2718 			   bfd_boolean not_local_protected)
   2719 {
   2720   bfd_boolean binding_stays_local_p;
   2721   const struct elf_backend_data *bed;
   2722   struct elf_link_hash_table *hash_table;
   2723 
   2724   if (h == NULL)
   2725     return FALSE;
   2726 
   2727   while (h->root.type == bfd_link_hash_indirect
   2728 	 || h->root.type == bfd_link_hash_warning)
   2729     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2730 
   2731   /* If it was forced local, then clearly it's not dynamic.  */
   2732   if (h->dynindx == -1)
   2733     return FALSE;
   2734   if (h->forced_local)
   2735     return FALSE;
   2736 
   2737   /* Identify the cases where name binding rules say that a
   2738      visible symbol resolves locally.  */
   2739   binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
   2740 
   2741   switch (ELF_ST_VISIBILITY (h->other))
   2742     {
   2743     case STV_INTERNAL:
   2744     case STV_HIDDEN:
   2745       return FALSE;
   2746 
   2747     case STV_PROTECTED:
   2748       hash_table = elf_hash_table (info);
   2749       if (!is_elf_hash_table (hash_table))
   2750 	return FALSE;
   2751 
   2752       bed = get_elf_backend_data (hash_table->dynobj);
   2753 
   2754       /* Proper resolution for function pointer equality may require
   2755 	 that these symbols perhaps be resolved dynamically, even though
   2756 	 we should be resolving them to the current module.  */
   2757       if (!not_local_protected || !bed->is_function_type (h->type))
   2758 	binding_stays_local_p = TRUE;
   2759       break;
   2760 
   2761     default:
   2762       break;
   2763     }
   2764 
   2765   /* If it isn't defined locally, then clearly it's dynamic.  */
   2766   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2767     return TRUE;
   2768 
   2769   /* Otherwise, the symbol is dynamic if binding rules don't tell
   2770      us that it remains local.  */
   2771   return !binding_stays_local_p;
   2772 }
   2773 
   2774 /* Return true if the symbol referred to by H should be considered
   2775    to resolve local to the current module, and false otherwise.  Differs
   2776    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   2777    undefined symbols.  The two functions are virtually identical except
   2778    for the place where forced_local and dynindx == -1 are tested.  If
   2779    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
   2780    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
   2781    the symbol is local only for defined symbols.
   2782    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   2783    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   2784    treatment of undefined weak symbols.  For those that do not make
   2785    undefined weak symbols dynamic, both functions may return false.  */
   2786 
   2787 bfd_boolean
   2788 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   2789 			      struct bfd_link_info *info,
   2790 			      bfd_boolean local_protected)
   2791 {
   2792   const struct elf_backend_data *bed;
   2793   struct elf_link_hash_table *hash_table;
   2794 
   2795   /* If it's a local sym, of course we resolve locally.  */
   2796   if (h == NULL)
   2797     return TRUE;
   2798 
   2799   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   2800   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   2801       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   2802     return TRUE;
   2803 
   2804   /* Common symbols that become definitions don't get the DEF_REGULAR
   2805      flag set, so test it first, and don't bail out.  */
   2806   if (ELF_COMMON_DEF_P (h))
   2807     /* Do nothing.  */;
   2808   /* If we don't have a definition in a regular file, then we can't
   2809      resolve locally.  The sym is either undefined or dynamic.  */
   2810   else if (!h->def_regular)
   2811     return FALSE;
   2812 
   2813   /* Forced local symbols resolve locally.  */
   2814   if (h->forced_local)
   2815     return TRUE;
   2816 
   2817   /* As do non-dynamic symbols.  */
   2818   if (h->dynindx == -1)
   2819     return TRUE;
   2820 
   2821   /* At this point, we know the symbol is defined and dynamic.  In an
   2822      executable it must resolve locally, likewise when building symbolic
   2823      shared libraries.  */
   2824   if (info->executable || SYMBOLIC_BIND (info, h))
   2825     return TRUE;
   2826 
   2827   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   2828      with default visibility might not resolve locally.  */
   2829   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   2830     return FALSE;
   2831 
   2832   hash_table = elf_hash_table (info);
   2833   if (!is_elf_hash_table (hash_table))
   2834     return TRUE;
   2835 
   2836   bed = get_elf_backend_data (hash_table->dynobj);
   2837 
   2838   /* STV_PROTECTED non-function symbols are local.  */
   2839   if (!bed->is_function_type (h->type))
   2840     return TRUE;
   2841 
   2842   /* Function pointer equality tests may require that STV_PROTECTED
   2843      symbols be treated as dynamic symbols.  If the address of a
   2844      function not defined in an executable is set to that function's
   2845      plt entry in the executable, then the address of the function in
   2846      a shared library must also be the plt entry in the executable.  */
   2847   return local_protected;
   2848 }
   2849 
   2850 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   2851    aligned.  Returns the first TLS output section.  */
   2852 
   2853 struct bfd_section *
   2854 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   2855 {
   2856   struct bfd_section *sec, *tls;
   2857   unsigned int align = 0;
   2858 
   2859   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   2860     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   2861       break;
   2862   tls = sec;
   2863 
   2864   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   2865     if (sec->alignment_power > align)
   2866       align = sec->alignment_power;
   2867 
   2868   elf_hash_table (info)->tls_sec = tls;
   2869 
   2870   /* Ensure the alignment of the first section is the largest alignment,
   2871      so that the tls segment starts aligned.  */
   2872   if (tls != NULL)
   2873     tls->alignment_power = align;
   2874 
   2875   return tls;
   2876 }
   2877 
   2878 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   2879 static bfd_boolean
   2880 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   2881 				  Elf_Internal_Sym *sym)
   2882 {
   2883   const struct elf_backend_data *bed;
   2884 
   2885   /* Local symbols do not count, but target specific ones might.  */
   2886   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   2887       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   2888     return FALSE;
   2889 
   2890   bed = get_elf_backend_data (abfd);
   2891   /* Function symbols do not count.  */
   2892   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   2893     return FALSE;
   2894 
   2895   /* If the section is undefined, then so is the symbol.  */
   2896   if (sym->st_shndx == SHN_UNDEF)
   2897     return FALSE;
   2898 
   2899   /* If the symbol is defined in the common section, then
   2900      it is a common definition and so does not count.  */
   2901   if (bed->common_definition (sym))
   2902     return FALSE;
   2903 
   2904   /* If the symbol is in a target specific section then we
   2905      must rely upon the backend to tell us what it is.  */
   2906   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   2907     /* FIXME - this function is not coded yet:
   2908 
   2909        return _bfd_is_global_symbol_definition (abfd, sym);
   2910 
   2911        Instead for now assume that the definition is not global,
   2912        Even if this is wrong, at least the linker will behave
   2913        in the same way that it used to do.  */
   2914     return FALSE;
   2915 
   2916   return TRUE;
   2917 }
   2918 
   2919 /* Search the symbol table of the archive element of the archive ABFD
   2920    whose archive map contains a mention of SYMDEF, and determine if
   2921    the symbol is defined in this element.  */
   2922 static bfd_boolean
   2923 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   2924 {
   2925   Elf_Internal_Shdr * hdr;
   2926   bfd_size_type symcount;
   2927   bfd_size_type extsymcount;
   2928   bfd_size_type extsymoff;
   2929   Elf_Internal_Sym *isymbuf;
   2930   Elf_Internal_Sym *isym;
   2931   Elf_Internal_Sym *isymend;
   2932   bfd_boolean result;
   2933 
   2934   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   2935   if (abfd == NULL)
   2936     return FALSE;
   2937 
   2938   if (! bfd_check_format (abfd, bfd_object))
   2939     return FALSE;
   2940 
   2941   /* Select the appropriate symbol table.  */
   2942   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   2943     hdr = &elf_tdata (abfd)->symtab_hdr;
   2944   else
   2945     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2946 
   2947   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   2948 
   2949   /* The sh_info field of the symtab header tells us where the
   2950      external symbols start.  We don't care about the local symbols.  */
   2951   if (elf_bad_symtab (abfd))
   2952     {
   2953       extsymcount = symcount;
   2954       extsymoff = 0;
   2955     }
   2956   else
   2957     {
   2958       extsymcount = symcount - hdr->sh_info;
   2959       extsymoff = hdr->sh_info;
   2960     }
   2961 
   2962   if (extsymcount == 0)
   2963     return FALSE;
   2964 
   2965   /* Read in the symbol table.  */
   2966   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   2967 				  NULL, NULL, NULL);
   2968   if (isymbuf == NULL)
   2969     return FALSE;
   2970 
   2971   /* Scan the symbol table looking for SYMDEF.  */
   2972   result = FALSE;
   2973   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   2974     {
   2975       const char *name;
   2976 
   2977       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   2978 					      isym->st_name);
   2979       if (name == NULL)
   2980 	break;
   2981 
   2982       if (strcmp (name, symdef->name) == 0)
   2983 	{
   2984 	  result = is_global_data_symbol_definition (abfd, isym);
   2985 	  break;
   2986 	}
   2987     }
   2988 
   2989   free (isymbuf);
   2990 
   2991   return result;
   2992 }
   2993 
   2994 /* Add an entry to the .dynamic table.  */
   2996 
   2997 bfd_boolean
   2998 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   2999 			    bfd_vma tag,
   3000 			    bfd_vma val)
   3001 {
   3002   struct elf_link_hash_table *hash_table;
   3003   const struct elf_backend_data *bed;
   3004   asection *s;
   3005   bfd_size_type newsize;
   3006   bfd_byte *newcontents;
   3007   Elf_Internal_Dyn dyn;
   3008 
   3009   hash_table = elf_hash_table (info);
   3010   if (! is_elf_hash_table (hash_table))
   3011     return FALSE;
   3012 
   3013   bed = get_elf_backend_data (hash_table->dynobj);
   3014   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3015   BFD_ASSERT (s != NULL);
   3016 
   3017   newsize = s->size + bed->s->sizeof_dyn;
   3018   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3019   if (newcontents == NULL)
   3020     return FALSE;
   3021 
   3022   dyn.d_tag = tag;
   3023   dyn.d_un.d_val = val;
   3024   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3025 
   3026   s->size = newsize;
   3027   s->contents = newcontents;
   3028 
   3029   return TRUE;
   3030 }
   3031 
   3032 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
   3033    otherwise just check whether one already exists.  Returns -1 on error,
   3034    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3035 
   3036 static int
   3037 elf_add_dt_needed_tag (bfd *abfd,
   3038 		       struct bfd_link_info *info,
   3039 		       const char *soname,
   3040 		       bfd_boolean do_it)
   3041 {
   3042   struct elf_link_hash_table *hash_table;
   3043   bfd_size_type strindex;
   3044 
   3045   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3046     return -1;
   3047 
   3048   hash_table = elf_hash_table (info);
   3049   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
   3050   if (strindex == (bfd_size_type) -1)
   3051     return -1;
   3052 
   3053   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3054     {
   3055       asection *sdyn;
   3056       const struct elf_backend_data *bed;
   3057       bfd_byte *extdyn;
   3058 
   3059       bed = get_elf_backend_data (hash_table->dynobj);
   3060       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3061       if (sdyn != NULL)
   3062 	for (extdyn = sdyn->contents;
   3063 	     extdyn < sdyn->contents + sdyn->size;
   3064 	     extdyn += bed->s->sizeof_dyn)
   3065 	  {
   3066 	    Elf_Internal_Dyn dyn;
   3067 
   3068 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3069 	    if (dyn.d_tag == DT_NEEDED
   3070 		&& dyn.d_un.d_val == strindex)
   3071 	      {
   3072 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3073 		return 1;
   3074 	      }
   3075 	  }
   3076     }
   3077 
   3078   if (do_it)
   3079     {
   3080       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3081 	return -1;
   3082 
   3083       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3084 	return -1;
   3085     }
   3086   else
   3087     /* We were just checking for existence of the tag.  */
   3088     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3089 
   3090   return 0;
   3091 }
   3092 
   3093 static bfd_boolean
   3094 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
   3095 {
   3096   for (; needed != NULL; needed = needed->next)
   3097     if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
   3098 	&& strcmp (soname, needed->name) == 0)
   3099       return TRUE;
   3100 
   3101   return FALSE;
   3102 }
   3103 
   3104 /* Sort symbol by value, section, and size.  */
   3105 static int
   3106 elf_sort_symbol (const void *arg1, const void *arg2)
   3107 {
   3108   const struct elf_link_hash_entry *h1;
   3109   const struct elf_link_hash_entry *h2;
   3110   bfd_signed_vma vdiff;
   3111 
   3112   h1 = *(const struct elf_link_hash_entry **) arg1;
   3113   h2 = *(const struct elf_link_hash_entry **) arg2;
   3114   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3115   if (vdiff != 0)
   3116     return vdiff > 0 ? 1 : -1;
   3117   else
   3118     {
   3119       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3120       if (sdiff != 0)
   3121 	return sdiff > 0 ? 1 : -1;
   3122     }
   3123   vdiff = h1->size - h2->size;
   3124   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
   3125 }
   3126 
   3127 /* This function is used to adjust offsets into .dynstr for
   3128    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   3129 
   3130 static bfd_boolean
   3131 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   3132 {
   3133   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   3134 
   3135   if (h->dynindx != -1)
   3136     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   3137   return TRUE;
   3138 }
   3139 
   3140 /* Assign string offsets in .dynstr, update all structures referencing
   3141    them.  */
   3142 
   3143 static bfd_boolean
   3144 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   3145 {
   3146   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   3147   struct elf_link_local_dynamic_entry *entry;
   3148   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   3149   bfd *dynobj = hash_table->dynobj;
   3150   asection *sdyn;
   3151   bfd_size_type size;
   3152   const struct elf_backend_data *bed;
   3153   bfd_byte *extdyn;
   3154 
   3155   _bfd_elf_strtab_finalize (dynstr);
   3156   size = _bfd_elf_strtab_size (dynstr);
   3157 
   3158   bed = get_elf_backend_data (dynobj);
   3159   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3160   BFD_ASSERT (sdyn != NULL);
   3161 
   3162   /* Update all .dynamic entries referencing .dynstr strings.  */
   3163   for (extdyn = sdyn->contents;
   3164        extdyn < sdyn->contents + sdyn->size;
   3165        extdyn += bed->s->sizeof_dyn)
   3166     {
   3167       Elf_Internal_Dyn dyn;
   3168 
   3169       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   3170       switch (dyn.d_tag)
   3171 	{
   3172 	case DT_STRSZ:
   3173 	  dyn.d_un.d_val = size;
   3174 	  break;
   3175 	case DT_NEEDED:
   3176 	case DT_SONAME:
   3177 	case DT_RPATH:
   3178 	case DT_RUNPATH:
   3179 	case DT_FILTER:
   3180 	case DT_AUXILIARY:
   3181 	case DT_AUDIT:
   3182 	case DT_DEPAUDIT:
   3183 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   3184 	  break;
   3185 	default:
   3186 	  continue;
   3187 	}
   3188       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   3189     }
   3190 
   3191   /* Now update local dynamic symbols.  */
   3192   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   3193     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   3194 						  entry->isym.st_name);
   3195 
   3196   /* And the rest of dynamic symbols.  */
   3197   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   3198 
   3199   /* Adjust version definitions.  */
   3200   if (elf_tdata (output_bfd)->cverdefs)
   3201     {
   3202       asection *s;
   3203       bfd_byte *p;
   3204       bfd_size_type i;
   3205       Elf_Internal_Verdef def;
   3206       Elf_Internal_Verdaux defaux;
   3207 
   3208       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   3209       p = s->contents;
   3210       do
   3211 	{
   3212 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   3213 				   &def);
   3214 	  p += sizeof (Elf_External_Verdef);
   3215 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   3216 	    continue;
   3217 	  for (i = 0; i < def.vd_cnt; ++i)
   3218 	    {
   3219 	      _bfd_elf_swap_verdaux_in (output_bfd,
   3220 					(Elf_External_Verdaux *) p, &defaux);
   3221 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   3222 							defaux.vda_name);
   3223 	      _bfd_elf_swap_verdaux_out (output_bfd,
   3224 					 &defaux, (Elf_External_Verdaux *) p);
   3225 	      p += sizeof (Elf_External_Verdaux);
   3226 	    }
   3227 	}
   3228       while (def.vd_next);
   3229     }
   3230 
   3231   /* Adjust version references.  */
   3232   if (elf_tdata (output_bfd)->verref)
   3233     {
   3234       asection *s;
   3235       bfd_byte *p;
   3236       bfd_size_type i;
   3237       Elf_Internal_Verneed need;
   3238       Elf_Internal_Vernaux needaux;
   3239 
   3240       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   3241       p = s->contents;
   3242       do
   3243 	{
   3244 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   3245 				    &need);
   3246 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   3247 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   3248 				     (Elf_External_Verneed *) p);
   3249 	  p += sizeof (Elf_External_Verneed);
   3250 	  for (i = 0; i < need.vn_cnt; ++i)
   3251 	    {
   3252 	      _bfd_elf_swap_vernaux_in (output_bfd,
   3253 					(Elf_External_Vernaux *) p, &needaux);
   3254 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   3255 							 needaux.vna_name);
   3256 	      _bfd_elf_swap_vernaux_out (output_bfd,
   3257 					 &needaux,
   3258 					 (Elf_External_Vernaux *) p);
   3259 	      p += sizeof (Elf_External_Vernaux);
   3260 	    }
   3261 	}
   3262       while (need.vn_next);
   3263     }
   3264 
   3265   return TRUE;
   3266 }
   3267 
   3268 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3270    The default is to only match when the INPUT and OUTPUT are exactly
   3271    the same target.  */
   3272 
   3273 bfd_boolean
   3274 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   3275 				    const bfd_target *output)
   3276 {
   3277   return input == output;
   3278 }
   3279 
   3280 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3281    This version is used when different targets for the same architecture
   3282    are virtually identical.  */
   3283 
   3284 bfd_boolean
   3285 _bfd_elf_relocs_compatible (const bfd_target *input,
   3286 			    const bfd_target *output)
   3287 {
   3288   const struct elf_backend_data *obed, *ibed;
   3289 
   3290   if (input == output)
   3291     return TRUE;
   3292 
   3293   ibed = xvec_get_elf_backend_data (input);
   3294   obed = xvec_get_elf_backend_data (output);
   3295 
   3296   if (ibed->arch != obed->arch)
   3297     return FALSE;
   3298 
   3299   /* If both backends are using this function, deem them compatible.  */
   3300   return ibed->relocs_compatible == obed->relocs_compatible;
   3301 }
   3302 
   3303 /* Make a special call to the linker "notice" function to tell it that
   3304    we are about to handle an as-needed lib, or have finished
   3305    processing the lib.  */
   3306 
   3307 bfd_boolean
   3308 _bfd_elf_notice_as_needed (bfd *ibfd,
   3309 			   struct bfd_link_info *info,
   3310 			   enum notice_asneeded_action act)
   3311 {
   3312   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   3313 }
   3314 
   3315 /* Add symbols from an ELF object file to the linker hash table.  */
   3316 
   3317 static bfd_boolean
   3318 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   3319 {
   3320   Elf_Internal_Ehdr *ehdr;
   3321   Elf_Internal_Shdr *hdr;
   3322   bfd_size_type symcount;
   3323   bfd_size_type extsymcount;
   3324   bfd_size_type extsymoff;
   3325   struct elf_link_hash_entry **sym_hash;
   3326   bfd_boolean dynamic;
   3327   Elf_External_Versym *extversym = NULL;
   3328   Elf_External_Versym *ever;
   3329   struct elf_link_hash_entry *weaks;
   3330   struct elf_link_hash_entry **nondeflt_vers = NULL;
   3331   bfd_size_type nondeflt_vers_cnt = 0;
   3332   Elf_Internal_Sym *isymbuf = NULL;
   3333   Elf_Internal_Sym *isym;
   3334   Elf_Internal_Sym *isymend;
   3335   const struct elf_backend_data *bed;
   3336   bfd_boolean add_needed;
   3337   struct elf_link_hash_table *htab;
   3338   bfd_size_type amt;
   3339   void *alloc_mark = NULL;
   3340   struct bfd_hash_entry **old_table = NULL;
   3341   unsigned int old_size = 0;
   3342   unsigned int old_count = 0;
   3343   void *old_tab = NULL;
   3344   void *old_ent;
   3345   struct bfd_link_hash_entry *old_undefs = NULL;
   3346   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   3347   long old_dynsymcount = 0;
   3348   bfd_size_type old_dynstr_size = 0;
   3349   size_t tabsize = 0;
   3350   asection *s;
   3351   bfd_boolean just_syms;
   3352 
   3353   htab = elf_hash_table (info);
   3354   bed = get_elf_backend_data (abfd);
   3355 
   3356   if ((abfd->flags & DYNAMIC) == 0)
   3357     dynamic = FALSE;
   3358   else
   3359     {
   3360       dynamic = TRUE;
   3361 
   3362       /* You can't use -r against a dynamic object.  Also, there's no
   3363 	 hope of using a dynamic object which does not exactly match
   3364 	 the format of the output file.  */
   3365       if (info->relocatable
   3366 	  || !is_elf_hash_table (htab)
   3367 	  || info->output_bfd->xvec != abfd->xvec)
   3368 	{
   3369 	  if (info->relocatable)
   3370 	    bfd_set_error (bfd_error_invalid_operation);
   3371 	  else
   3372 	    bfd_set_error (bfd_error_wrong_format);
   3373 	  goto error_return;
   3374 	}
   3375     }
   3376 
   3377   ehdr = elf_elfheader (abfd);
   3378   if (info->warn_alternate_em
   3379       && bed->elf_machine_code != ehdr->e_machine
   3380       && ((bed->elf_machine_alt1 != 0
   3381 	   && ehdr->e_machine == bed->elf_machine_alt1)
   3382 	  || (bed->elf_machine_alt2 != 0
   3383 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   3384     info->callbacks->einfo
   3385       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
   3386        ehdr->e_machine, abfd, bed->elf_machine_code);
   3387 
   3388   /* As a GNU extension, any input sections which are named
   3389      .gnu.warning.SYMBOL are treated as warning symbols for the given
   3390      symbol.  This differs from .gnu.warning sections, which generate
   3391      warnings when they are included in an output file.  */
   3392   /* PR 12761: Also generate this warning when building shared libraries.  */
   3393   for (s = abfd->sections; s != NULL; s = s->next)
   3394     {
   3395       const char *name;
   3396 
   3397       name = bfd_get_section_name (abfd, s);
   3398       if (CONST_STRNEQ (name, ".gnu.warning."))
   3399 	{
   3400 	  char *msg;
   3401 	  bfd_size_type sz;
   3402 
   3403 	  name += sizeof ".gnu.warning." - 1;
   3404 
   3405 	  /* If this is a shared object, then look up the symbol
   3406 	     in the hash table.  If it is there, and it is already
   3407 	     been defined, then we will not be using the entry
   3408 	     from this shared object, so we don't need to warn.
   3409 	     FIXME: If we see the definition in a regular object
   3410 	     later on, we will warn, but we shouldn't.  The only
   3411 	     fix is to keep track of what warnings we are supposed
   3412 	     to emit, and then handle them all at the end of the
   3413 	     link.  */
   3414 	  if (dynamic)
   3415 	    {
   3416 	      struct elf_link_hash_entry *h;
   3417 
   3418 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   3419 
   3420 	      /* FIXME: What about bfd_link_hash_common?  */
   3421 	      if (h != NULL
   3422 		  && (h->root.type == bfd_link_hash_defined
   3423 		      || h->root.type == bfd_link_hash_defweak))
   3424 		continue;
   3425 	    }
   3426 
   3427 	  sz = s->size;
   3428 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   3429 	  if (msg == NULL)
   3430 	    goto error_return;
   3431 
   3432 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   3433 	    goto error_return;
   3434 
   3435 	  msg[sz] = '\0';
   3436 
   3437 	  if (! (_bfd_generic_link_add_one_symbol
   3438 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   3439 		  FALSE, bed->collect, NULL)))
   3440 	    goto error_return;
   3441 
   3442 	  if (!info->relocatable && info->executable)
   3443 	    {
   3444 	      /* Clobber the section size so that the warning does
   3445 		 not get copied into the output file.  */
   3446 	      s->size = 0;
   3447 
   3448 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   3449 		 the warning section don't get copied to the output.  */
   3450 	      s->flags |= SEC_EXCLUDE;
   3451 	    }
   3452 	}
   3453     }
   3454 
   3455   just_syms = ((s = abfd->sections) != NULL
   3456 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   3457 
   3458   add_needed = TRUE;
   3459   if (! dynamic)
   3460     {
   3461       /* If we are creating a shared library, create all the dynamic
   3462 	 sections immediately.  We need to attach them to something,
   3463 	 so we attach them to this BFD, provided it is the right
   3464 	 format and is not from ld --just-symbols.  FIXME: If there
   3465 	 are no input BFD's of the same format as the output, we can't
   3466 	 make a shared library.  */
   3467       if (!just_syms
   3468 	  && info->shared
   3469 	  && is_elf_hash_table (htab)
   3470 	  && info->output_bfd->xvec == abfd->xvec
   3471 	  && !htab->dynamic_sections_created)
   3472 	{
   3473 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   3474 	    goto error_return;
   3475 	}
   3476     }
   3477   else if (!is_elf_hash_table (htab))
   3478     goto error_return;
   3479   else
   3480     {
   3481       const char *soname = NULL;
   3482       char *audit = NULL;
   3483       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   3484       int ret;
   3485 
   3486       /* ld --just-symbols and dynamic objects don't mix very well.
   3487 	 ld shouldn't allow it.  */
   3488       if (just_syms)
   3489 	abort ();
   3490 
   3491       /* If this dynamic lib was specified on the command line with
   3492 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   3493 	 tag unless the lib is actually used.  Similary for libs brought
   3494 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   3495 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   3496 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   3497 	 all.  */
   3498       add_needed = (elf_dyn_lib_class (abfd)
   3499 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   3500 		       | DYN_NO_NEEDED)) == 0;
   3501 
   3502       s = bfd_get_section_by_name (abfd, ".dynamic");
   3503       if (s != NULL)
   3504 	{
   3505 	  bfd_byte *dynbuf;
   3506 	  bfd_byte *extdyn;
   3507 	  unsigned int elfsec;
   3508 	  unsigned long shlink;
   3509 
   3510 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   3511 	    {
   3512 error_free_dyn:
   3513 	      free (dynbuf);
   3514 	      goto error_return;
   3515 	    }
   3516 
   3517 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   3518 	  if (elfsec == SHN_BAD)
   3519 	    goto error_free_dyn;
   3520 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   3521 
   3522 	  for (extdyn = dynbuf;
   3523 	       extdyn < dynbuf + s->size;
   3524 	       extdyn += bed->s->sizeof_dyn)
   3525 	    {
   3526 	      Elf_Internal_Dyn dyn;
   3527 
   3528 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   3529 	      if (dyn.d_tag == DT_SONAME)
   3530 		{
   3531 		  unsigned int tagv = dyn.d_un.d_val;
   3532 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3533 		  if (soname == NULL)
   3534 		    goto error_free_dyn;
   3535 		}
   3536 	      if (dyn.d_tag == DT_NEEDED)
   3537 		{
   3538 		  struct bfd_link_needed_list *n, **pn;
   3539 		  char *fnm, *anm;
   3540 		  unsigned int tagv = dyn.d_un.d_val;
   3541 
   3542 		  amt = sizeof (struct bfd_link_needed_list);
   3543 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3544 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3545 		  if (n == NULL || fnm == NULL)
   3546 		    goto error_free_dyn;
   3547 		  amt = strlen (fnm) + 1;
   3548 		  anm = (char *) bfd_alloc (abfd, amt);
   3549 		  if (anm == NULL)
   3550 		    goto error_free_dyn;
   3551 		  memcpy (anm, fnm, amt);
   3552 		  n->name = anm;
   3553 		  n->by = abfd;
   3554 		  n->next = NULL;
   3555 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   3556 		    ;
   3557 		  *pn = n;
   3558 		}
   3559 	      if (dyn.d_tag == DT_RUNPATH)
   3560 		{
   3561 		  struct bfd_link_needed_list *n, **pn;
   3562 		  char *fnm, *anm;
   3563 		  unsigned int tagv = dyn.d_un.d_val;
   3564 
   3565 		  amt = sizeof (struct bfd_link_needed_list);
   3566 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3567 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3568 		  if (n == NULL || fnm == NULL)
   3569 		    goto error_free_dyn;
   3570 		  amt = strlen (fnm) + 1;
   3571 		  anm = (char *) bfd_alloc (abfd, amt);
   3572 		  if (anm == NULL)
   3573 		    goto error_free_dyn;
   3574 		  memcpy (anm, fnm, amt);
   3575 		  n->name = anm;
   3576 		  n->by = abfd;
   3577 		  n->next = NULL;
   3578 		  for (pn = & runpath;
   3579 		       *pn != NULL;
   3580 		       pn = &(*pn)->next)
   3581 		    ;
   3582 		  *pn = n;
   3583 		}
   3584 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   3585 	      if (!runpath && dyn.d_tag == DT_RPATH)
   3586 		{
   3587 		  struct bfd_link_needed_list *n, **pn;
   3588 		  char *fnm, *anm;
   3589 		  unsigned int tagv = dyn.d_un.d_val;
   3590 
   3591 		  amt = sizeof (struct bfd_link_needed_list);
   3592 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3593 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3594 		  if (n == NULL || fnm == NULL)
   3595 		    goto error_free_dyn;
   3596 		  amt = strlen (fnm) + 1;
   3597 		  anm = (char *) bfd_alloc (abfd, amt);
   3598 		  if (anm == NULL)
   3599 		    goto error_free_dyn;
   3600 		  memcpy (anm, fnm, amt);
   3601 		  n->name = anm;
   3602 		  n->by = abfd;
   3603 		  n->next = NULL;
   3604 		  for (pn = & rpath;
   3605 		       *pn != NULL;
   3606 		       pn = &(*pn)->next)
   3607 		    ;
   3608 		  *pn = n;
   3609 		}
   3610 	      if (dyn.d_tag == DT_AUDIT)
   3611 		{
   3612 		  unsigned int tagv = dyn.d_un.d_val;
   3613 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3614 		}
   3615 	    }
   3616 
   3617 	  free (dynbuf);
   3618 	}
   3619 
   3620       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   3621 	 frees all more recently bfd_alloc'd blocks as well.  */
   3622       if (runpath)
   3623 	rpath = runpath;
   3624 
   3625       if (rpath)
   3626 	{
   3627 	  struct bfd_link_needed_list **pn;
   3628 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   3629 	    ;
   3630 	  *pn = rpath;
   3631 	}
   3632 
   3633       /* We do not want to include any of the sections in a dynamic
   3634 	 object in the output file.  We hack by simply clobbering the
   3635 	 list of sections in the BFD.  This could be handled more
   3636 	 cleanly by, say, a new section flag; the existing
   3637 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   3638 	 still implies that the section takes up space in the output
   3639 	 file.  */
   3640       bfd_section_list_clear (abfd);
   3641 
   3642       /* Find the name to use in a DT_NEEDED entry that refers to this
   3643 	 object.  If the object has a DT_SONAME entry, we use it.
   3644 	 Otherwise, if the generic linker stuck something in
   3645 	 elf_dt_name, we use that.  Otherwise, we just use the file
   3646 	 name.  */
   3647       if (soname == NULL || *soname == '\0')
   3648 	{
   3649 	  soname = elf_dt_name (abfd);
   3650 	  if (soname == NULL || *soname == '\0')
   3651 	    soname = bfd_get_filename (abfd);
   3652 	}
   3653 
   3654       /* Save the SONAME because sometimes the linker emulation code
   3655 	 will need to know it.  */
   3656       elf_dt_name (abfd) = soname;
   3657 
   3658       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   3659       if (ret < 0)
   3660 	goto error_return;
   3661 
   3662       /* If we have already included this dynamic object in the
   3663 	 link, just ignore it.  There is no reason to include a
   3664 	 particular dynamic object more than once.  */
   3665       if (ret > 0)
   3666 	return TRUE;
   3667 
   3668       /* Save the DT_AUDIT entry for the linker emulation code. */
   3669       elf_dt_audit (abfd) = audit;
   3670     }
   3671 
   3672   /* If this is a dynamic object, we always link against the .dynsym
   3673      symbol table, not the .symtab symbol table.  The dynamic linker
   3674      will only see the .dynsym symbol table, so there is no reason to
   3675      look at .symtab for a dynamic object.  */
   3676 
   3677   if (! dynamic || elf_dynsymtab (abfd) == 0)
   3678     hdr = &elf_tdata (abfd)->symtab_hdr;
   3679   else
   3680     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3681 
   3682   symcount = hdr->sh_size / bed->s->sizeof_sym;
   3683 
   3684   /* The sh_info field of the symtab header tells us where the
   3685      external symbols start.  We don't care about the local symbols at
   3686      this point.  */
   3687   if (elf_bad_symtab (abfd))
   3688     {
   3689       extsymcount = symcount;
   3690       extsymoff = 0;
   3691     }
   3692   else
   3693     {
   3694       extsymcount = symcount - hdr->sh_info;
   3695       extsymoff = hdr->sh_info;
   3696     }
   3697 
   3698   sym_hash = elf_sym_hashes (abfd);
   3699   if (extsymcount != 0)
   3700     {
   3701       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3702 				      NULL, NULL, NULL);
   3703       if (isymbuf == NULL)
   3704 	goto error_return;
   3705 
   3706       if (sym_hash == NULL)
   3707 	{
   3708 	  /* We store a pointer to the hash table entry for each
   3709 	     external symbol.  */
   3710 	  amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   3711 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   3712 	  if (sym_hash == NULL)
   3713 	    goto error_free_sym;
   3714 	  elf_sym_hashes (abfd) = sym_hash;
   3715 	}
   3716     }
   3717 
   3718   if (dynamic)
   3719     {
   3720       /* Read in any version definitions.  */
   3721       if (!_bfd_elf_slurp_version_tables (abfd,
   3722 					  info->default_imported_symver))
   3723 	goto error_free_sym;
   3724 
   3725       /* Read in the symbol versions, but don't bother to convert them
   3726 	 to internal format.  */
   3727       if (elf_dynversym (abfd) != 0)
   3728 	{
   3729 	  Elf_Internal_Shdr *versymhdr;
   3730 
   3731 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   3732 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   3733 	  if (extversym == NULL)
   3734 	    goto error_free_sym;
   3735 	  amt = versymhdr->sh_size;
   3736 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
   3737 	      || bfd_bread (extversym, amt, abfd) != amt)
   3738 	    goto error_free_vers;
   3739 	}
   3740     }
   3741 
   3742   /* If we are loading an as-needed shared lib, save the symbol table
   3743      state before we start adding symbols.  If the lib turns out
   3744      to be unneeded, restore the state.  */
   3745   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   3746     {
   3747       unsigned int i;
   3748       size_t entsize;
   3749 
   3750       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   3751 	{
   3752 	  struct bfd_hash_entry *p;
   3753 	  struct elf_link_hash_entry *h;
   3754 
   3755 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   3756 	    {
   3757 	      h = (struct elf_link_hash_entry *) p;
   3758 	      entsize += htab->root.table.entsize;
   3759 	      if (h->root.type == bfd_link_hash_warning)
   3760 		entsize += htab->root.table.entsize;
   3761 	    }
   3762 	}
   3763 
   3764       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   3765       old_tab = bfd_malloc (tabsize + entsize);
   3766       if (old_tab == NULL)
   3767 	goto error_free_vers;
   3768 
   3769       /* Remember the current objalloc pointer, so that all mem for
   3770 	 symbols added can later be reclaimed.  */
   3771       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   3772       if (alloc_mark == NULL)
   3773 	goto error_free_vers;
   3774 
   3775       /* Make a special call to the linker "notice" function to
   3776 	 tell it that we are about to handle an as-needed lib.  */
   3777       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   3778 	goto error_free_vers;
   3779 
   3780       /* Clone the symbol table.  Remember some pointers into the
   3781 	 symbol table, and dynamic symbol count.  */
   3782       old_ent = (char *) old_tab + tabsize;
   3783       memcpy (old_tab, htab->root.table.table, tabsize);
   3784       old_undefs = htab->root.undefs;
   3785       old_undefs_tail = htab->root.undefs_tail;
   3786       old_table = htab->root.table.table;
   3787       old_size = htab->root.table.size;
   3788       old_count = htab->root.table.count;
   3789       old_dynsymcount = htab->dynsymcount;
   3790       old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
   3791 
   3792       for (i = 0; i < htab->root.table.size; i++)
   3793 	{
   3794 	  struct bfd_hash_entry *p;
   3795 	  struct elf_link_hash_entry *h;
   3796 
   3797 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   3798 	    {
   3799 	      memcpy (old_ent, p, htab->root.table.entsize);
   3800 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   3801 	      h = (struct elf_link_hash_entry *) p;
   3802 	      if (h->root.type == bfd_link_hash_warning)
   3803 		{
   3804 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
   3805 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   3806 		}
   3807 	    }
   3808 	}
   3809     }
   3810 
   3811   weaks = NULL;
   3812   ever = extversym != NULL ? extversym + extsymoff : NULL;
   3813   for (isym = isymbuf, isymend = isymbuf + extsymcount;
   3814        isym < isymend;
   3815        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   3816     {
   3817       int bind;
   3818       bfd_vma value;
   3819       asection *sec, *new_sec;
   3820       flagword flags;
   3821       const char *name;
   3822       struct elf_link_hash_entry *h;
   3823       struct elf_link_hash_entry *hi;
   3824       bfd_boolean definition;
   3825       bfd_boolean size_change_ok;
   3826       bfd_boolean type_change_ok;
   3827       bfd_boolean new_weakdef;
   3828       bfd_boolean new_weak;
   3829       bfd_boolean old_weak;
   3830       bfd_boolean override;
   3831       bfd_boolean common;
   3832       unsigned int old_alignment;
   3833       bfd *old_bfd;
   3834 
   3835       override = FALSE;
   3836 
   3837       flags = BSF_NO_FLAGS;
   3838       sec = NULL;
   3839       value = isym->st_value;
   3840       common = bed->common_definition (isym);
   3841 
   3842       bind = ELF_ST_BIND (isym->st_info);
   3843       switch (bind)
   3844 	{
   3845 	case STB_LOCAL:
   3846 	  /* This should be impossible, since ELF requires that all
   3847 	     global symbols follow all local symbols, and that sh_info
   3848 	     point to the first global symbol.  Unfortunately, Irix 5
   3849 	     screws this up.  */
   3850 	  continue;
   3851 
   3852 	case STB_GLOBAL:
   3853 	  if (isym->st_shndx != SHN_UNDEF && !common)
   3854 	    flags = BSF_GLOBAL;
   3855 	  break;
   3856 
   3857 	case STB_WEAK:
   3858 	  flags = BSF_WEAK;
   3859 	  break;
   3860 
   3861 	case STB_GNU_UNIQUE:
   3862 	  flags = BSF_GNU_UNIQUE;
   3863 	  break;
   3864 
   3865 	default:
   3866 	  /* Leave it up to the processor backend.  */
   3867 	  break;
   3868 	}
   3869 
   3870       if (isym->st_shndx == SHN_UNDEF)
   3871 	sec = bfd_und_section_ptr;
   3872       else if (isym->st_shndx == SHN_ABS)
   3873 	sec = bfd_abs_section_ptr;
   3874       else if (isym->st_shndx == SHN_COMMON)
   3875 	{
   3876 	  sec = bfd_com_section_ptr;
   3877 	  /* What ELF calls the size we call the value.  What ELF
   3878 	     calls the value we call the alignment.  */
   3879 	  value = isym->st_size;
   3880 	}
   3881       else
   3882 	{
   3883 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3884 	  if (sec == NULL)
   3885 	    sec = bfd_abs_section_ptr;
   3886 	  else if (discarded_section (sec))
   3887 	    {
   3888 	      /* Symbols from discarded section are undefined.  We keep
   3889 		 its visibility.  */
   3890 	      sec = bfd_und_section_ptr;
   3891 	      isym->st_shndx = SHN_UNDEF;
   3892 	    }
   3893 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   3894 	    value -= sec->vma;
   3895 	}
   3896 
   3897       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3898 					      isym->st_name);
   3899       if (name == NULL)
   3900 	goto error_free_vers;
   3901 
   3902       if (isym->st_shndx == SHN_COMMON
   3903 	  && (abfd->flags & BFD_PLUGIN) != 0)
   3904 	{
   3905 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   3906 
   3907 	  if (xc == NULL)
   3908 	    {
   3909 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   3910 				 | SEC_EXCLUDE);
   3911 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   3912 	      if (xc == NULL)
   3913 		goto error_free_vers;
   3914 	    }
   3915 	  sec = xc;
   3916 	}
   3917       else if (isym->st_shndx == SHN_COMMON
   3918 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   3919 	       && !info->relocatable)
   3920 	{
   3921 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   3922 
   3923 	  if (tcomm == NULL)
   3924 	    {
   3925 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   3926 				 | SEC_LINKER_CREATED);
   3927 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   3928 	      if (tcomm == NULL)
   3929 		goto error_free_vers;
   3930 	    }
   3931 	  sec = tcomm;
   3932 	}
   3933       else if (bed->elf_add_symbol_hook)
   3934 	{
   3935 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   3936 					     &sec, &value))
   3937 	    goto error_free_vers;
   3938 
   3939 	  /* The hook function sets the name to NULL if this symbol
   3940 	     should be skipped for some reason.  */
   3941 	  if (name == NULL)
   3942 	    continue;
   3943 	}
   3944 
   3945       /* Sanity check that all possibilities were handled.  */
   3946       if (sec == NULL)
   3947 	{
   3948 	  bfd_set_error (bfd_error_bad_value);
   3949 	  goto error_free_vers;
   3950 	}
   3951 
   3952       /* Silently discard TLS symbols from --just-syms.  There's
   3953 	 no way to combine a static TLS block with a new TLS block
   3954 	 for this executable.  */
   3955       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   3956 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   3957 	continue;
   3958 
   3959       if (bfd_is_und_section (sec)
   3960 	  || bfd_is_com_section (sec))
   3961 	definition = FALSE;
   3962       else
   3963 	definition = TRUE;
   3964 
   3965       size_change_ok = FALSE;
   3966       type_change_ok = bed->type_change_ok;
   3967       old_weak = FALSE;
   3968       old_alignment = 0;
   3969       old_bfd = NULL;
   3970       new_sec = sec;
   3971 
   3972       if (is_elf_hash_table (htab))
   3973 	{
   3974 	  Elf_Internal_Versym iver;
   3975 	  unsigned int vernum = 0;
   3976 	  bfd_boolean skip;
   3977 
   3978 	  if (ever == NULL)
   3979 	    {
   3980 	      if (info->default_imported_symver)
   3981 		/* Use the default symbol version created earlier.  */
   3982 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   3983 	      else
   3984 		iver.vs_vers = 0;
   3985 	    }
   3986 	  else
   3987 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   3988 
   3989 	  vernum = iver.vs_vers & VERSYM_VERSION;
   3990 
   3991 	  /* If this is a hidden symbol, or if it is not version
   3992 	     1, we append the version name to the symbol name.
   3993 	     However, we do not modify a non-hidden absolute symbol
   3994 	     if it is not a function, because it might be the version
   3995 	     symbol itself.  FIXME: What if it isn't?  */
   3996 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   3997 	      || (vernum > 1
   3998 		  && (!bfd_is_abs_section (sec)
   3999 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   4000 	    {
   4001 	      const char *verstr;
   4002 	      size_t namelen, verlen, newlen;
   4003 	      char *newname, *p;
   4004 
   4005 	      if (isym->st_shndx != SHN_UNDEF)
   4006 		{
   4007 		  if (vernum > elf_tdata (abfd)->cverdefs)
   4008 		    verstr = NULL;
   4009 		  else if (vernum > 1)
   4010 		    verstr =
   4011 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   4012 		  else
   4013 		    verstr = "";
   4014 
   4015 		  if (verstr == NULL)
   4016 		    {
   4017 		      (*_bfd_error_handler)
   4018 			(_("%B: %s: invalid version %u (max %d)"),
   4019 			 abfd, name, vernum,
   4020 			 elf_tdata (abfd)->cverdefs);
   4021 		      bfd_set_error (bfd_error_bad_value);
   4022 		      goto error_free_vers;
   4023 		    }
   4024 		}
   4025 	      else
   4026 		{
   4027 		  /* We cannot simply test for the number of
   4028 		     entries in the VERNEED section since the
   4029 		     numbers for the needed versions do not start
   4030 		     at 0.  */
   4031 		  Elf_Internal_Verneed *t;
   4032 
   4033 		  verstr = NULL;
   4034 		  for (t = elf_tdata (abfd)->verref;
   4035 		       t != NULL;
   4036 		       t = t->vn_nextref)
   4037 		    {
   4038 		      Elf_Internal_Vernaux *a;
   4039 
   4040 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   4041 			{
   4042 			  if (a->vna_other == vernum)
   4043 			    {
   4044 			      verstr = a->vna_nodename;
   4045 			      break;
   4046 			    }
   4047 			}
   4048 		      if (a != NULL)
   4049 			break;
   4050 		    }
   4051 		  if (verstr == NULL)
   4052 		    {
   4053 		      (*_bfd_error_handler)
   4054 			(_("%B: %s: invalid needed version %d"),
   4055 			 abfd, name, vernum);
   4056 		      bfd_set_error (bfd_error_bad_value);
   4057 		      goto error_free_vers;
   4058 		    }
   4059 		}
   4060 
   4061 	      namelen = strlen (name);
   4062 	      verlen = strlen (verstr);
   4063 	      newlen = namelen + verlen + 2;
   4064 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4065 		  && isym->st_shndx != SHN_UNDEF)
   4066 		++newlen;
   4067 
   4068 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   4069 	      if (newname == NULL)
   4070 		goto error_free_vers;
   4071 	      memcpy (newname, name, namelen);
   4072 	      p = newname + namelen;
   4073 	      *p++ = ELF_VER_CHR;
   4074 	      /* If this is a defined non-hidden version symbol,
   4075 		 we add another @ to the name.  This indicates the
   4076 		 default version of the symbol.  */
   4077 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4078 		  && isym->st_shndx != SHN_UNDEF)
   4079 		*p++ = ELF_VER_CHR;
   4080 	      memcpy (p, verstr, verlen + 1);
   4081 
   4082 	      name = newname;
   4083 	    }
   4084 
   4085 	  /* If this symbol has default visibility and the user has
   4086 	     requested we not re-export it, then mark it as hidden.  */
   4087 	  if (definition
   4088 	      && !dynamic
   4089 	      && (abfd->no_export
   4090 		  || (abfd->my_archive && abfd->my_archive->no_export))
   4091 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   4092 	    isym->st_other = (STV_HIDDEN
   4093 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   4094 
   4095 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   4096 				      sym_hash, &old_bfd, &old_weak,
   4097 				      &old_alignment, &skip, &override,
   4098 				      &type_change_ok, &size_change_ok))
   4099 	    goto error_free_vers;
   4100 
   4101 	  if (skip)
   4102 	    continue;
   4103 
   4104 	  if (override)
   4105 	    definition = FALSE;
   4106 
   4107 	  h = *sym_hash;
   4108 	  while (h->root.type == bfd_link_hash_indirect
   4109 		 || h->root.type == bfd_link_hash_warning)
   4110 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4111 
   4112 	  if (elf_tdata (abfd)->verdef != NULL
   4113 	      && vernum > 1
   4114 	      && definition)
   4115 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   4116 	}
   4117 
   4118       if (! (_bfd_generic_link_add_one_symbol
   4119 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
   4120 	      (struct bfd_link_hash_entry **) sym_hash)))
   4121 	goto error_free_vers;
   4122 
   4123       h = *sym_hash;
   4124       /* We need to make sure that indirect symbol dynamic flags are
   4125 	 updated.  */
   4126       hi = h;
   4127       while (h->root.type == bfd_link_hash_indirect
   4128 	     || h->root.type == bfd_link_hash_warning)
   4129 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4130 
   4131       *sym_hash = h;
   4132 
   4133       new_weak = (flags & BSF_WEAK) != 0;
   4134       new_weakdef = FALSE;
   4135       if (dynamic
   4136 	  && definition
   4137 	  && new_weak
   4138 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   4139 	  && is_elf_hash_table (htab)
   4140 	  && h->u.weakdef == NULL)
   4141 	{
   4142 	  /* Keep a list of all weak defined non function symbols from
   4143 	     a dynamic object, using the weakdef field.  Later in this
   4144 	     function we will set the weakdef field to the correct
   4145 	     value.  We only put non-function symbols from dynamic
   4146 	     objects on this list, because that happens to be the only
   4147 	     time we need to know the normal symbol corresponding to a
   4148 	     weak symbol, and the information is time consuming to
   4149 	     figure out.  If the weakdef field is not already NULL,
   4150 	     then this symbol was already defined by some previous
   4151 	     dynamic object, and we will be using that previous
   4152 	     definition anyhow.  */
   4153 
   4154 	  h->u.weakdef = weaks;
   4155 	  weaks = h;
   4156 	  new_weakdef = TRUE;
   4157 	}
   4158 
   4159       /* Set the alignment of a common symbol.  */
   4160       if ((common || bfd_is_com_section (sec))
   4161 	  && h->root.type == bfd_link_hash_common)
   4162 	{
   4163 	  unsigned int align;
   4164 
   4165 	  if (common)
   4166 	    align = bfd_log2 (isym->st_value);
   4167 	  else
   4168 	    {
   4169 	      /* The new symbol is a common symbol in a shared object.
   4170 		 We need to get the alignment from the section.  */
   4171 	      align = new_sec->alignment_power;
   4172 	    }
   4173 	  if (align > old_alignment)
   4174 	    h->root.u.c.p->alignment_power = align;
   4175 	  else
   4176 	    h->root.u.c.p->alignment_power = old_alignment;
   4177 	}
   4178 
   4179       if (is_elf_hash_table (htab))
   4180 	{
   4181 	  /* Set a flag in the hash table entry indicating the type of
   4182 	     reference or definition we just found.  A dynamic symbol
   4183 	     is one which is referenced or defined by both a regular
   4184 	     object and a shared object.  */
   4185 	  bfd_boolean dynsym = FALSE;
   4186 
   4187 	  /* Plugin symbols aren't normal.  Don't set def_regular or
   4188 	     ref_regular for them, or make them dynamic.  */
   4189 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4190 	    ;
   4191 	  else if (! dynamic)
   4192 	    {
   4193 	      if (! definition)
   4194 		{
   4195 		  h->ref_regular = 1;
   4196 		  if (bind != STB_WEAK)
   4197 		    h->ref_regular_nonweak = 1;
   4198 		}
   4199 	      else
   4200 		{
   4201 		  h->def_regular = 1;
   4202 		  if (h->def_dynamic)
   4203 		    {
   4204 		      h->def_dynamic = 0;
   4205 		      h->ref_dynamic = 1;
   4206 		    }
   4207 		}
   4208 
   4209 	      /* If the indirect symbol has been forced local, don't
   4210 		 make the real symbol dynamic.  */
   4211 	      if ((h == hi || !hi->forced_local)
   4212 		  && (! info->executable
   4213 		      || h->def_dynamic
   4214 		      || h->ref_dynamic))
   4215 		dynsym = TRUE;
   4216 	    }
   4217 	  else
   4218 	    {
   4219 	      if (! definition)
   4220 		{
   4221 		  h->ref_dynamic = 1;
   4222 		  hi->ref_dynamic = 1;
   4223 		}
   4224 	      else
   4225 		{
   4226 		  h->def_dynamic = 1;
   4227 		  hi->def_dynamic = 1;
   4228 		}
   4229 
   4230 	      /* If the indirect symbol has been forced local, don't
   4231 		 make the real symbol dynamic.  */
   4232 	      if ((h == hi || !hi->forced_local)
   4233 		  && (h->def_regular
   4234 		      || h->ref_regular
   4235 		      || (h->u.weakdef != NULL
   4236 			  && ! new_weakdef
   4237 			  && h->u.weakdef->dynindx != -1)))
   4238 		dynsym = TRUE;
   4239 	    }
   4240 
   4241 	  /* Check to see if we need to add an indirect symbol for
   4242 	     the default name.  */
   4243 	  if (definition
   4244 	      || (!override && h->root.type == bfd_link_hash_common))
   4245 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   4246 					      sec, value, &old_bfd, &dynsym))
   4247 	      goto error_free_vers;
   4248 
   4249 	  /* Check the alignment when a common symbol is involved. This
   4250 	     can change when a common symbol is overridden by a normal
   4251 	     definition or a common symbol is ignored due to the old
   4252 	     normal definition. We need to make sure the maximum
   4253 	     alignment is maintained.  */
   4254 	  if ((old_alignment || common)
   4255 	      && h->root.type != bfd_link_hash_common)
   4256 	    {
   4257 	      unsigned int common_align;
   4258 	      unsigned int normal_align;
   4259 	      unsigned int symbol_align;
   4260 	      bfd *normal_bfd;
   4261 	      bfd *common_bfd;
   4262 
   4263 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   4264 			  || h->root.type == bfd_link_hash_defweak);
   4265 
   4266 	      symbol_align = ffs (h->root.u.def.value) - 1;
   4267 	      if (h->root.u.def.section->owner != NULL
   4268 		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
   4269 		{
   4270 		  normal_align = h->root.u.def.section->alignment_power;
   4271 		  if (normal_align > symbol_align)
   4272 		    normal_align = symbol_align;
   4273 		}
   4274 	      else
   4275 		normal_align = symbol_align;
   4276 
   4277 	      if (old_alignment)
   4278 		{
   4279 		  common_align = old_alignment;
   4280 		  common_bfd = old_bfd;
   4281 		  normal_bfd = abfd;
   4282 		}
   4283 	      else
   4284 		{
   4285 		  common_align = bfd_log2 (isym->st_value);
   4286 		  common_bfd = abfd;
   4287 		  normal_bfd = old_bfd;
   4288 		}
   4289 
   4290 	      if (normal_align < common_align)
   4291 		{
   4292 		  /* PR binutils/2735 */
   4293 		  if (normal_bfd == NULL)
   4294 		    (*_bfd_error_handler)
   4295 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
   4296 			 " greater than the alignment (%u) of its section %A"),
   4297 		       common_bfd, h->root.u.def.section,
   4298 		       1 << common_align, name, 1 << normal_align);
   4299 		  else
   4300 		    (*_bfd_error_handler)
   4301 		      (_("Warning: alignment %u of symbol `%s' in %B"
   4302 			 " is smaller than %u in %B"),
   4303 		       normal_bfd, common_bfd,
   4304 		       1 << normal_align, name, 1 << common_align);
   4305 		}
   4306 	    }
   4307 
   4308 	  /* Remember the symbol size if it isn't undefined.  */
   4309 	  if (isym->st_size != 0
   4310 	      && isym->st_shndx != SHN_UNDEF
   4311 	      && (definition || h->size == 0))
   4312 	    {
   4313 	      if (h->size != 0
   4314 		  && h->size != isym->st_size
   4315 		  && ! size_change_ok)
   4316 		(*_bfd_error_handler)
   4317 		  (_("Warning: size of symbol `%s' changed"
   4318 		     " from %lu in %B to %lu in %B"),
   4319 		   old_bfd, abfd,
   4320 		   name, (unsigned long) h->size,
   4321 		   (unsigned long) isym->st_size);
   4322 
   4323 	      h->size = isym->st_size;
   4324 	    }
   4325 
   4326 	  /* If this is a common symbol, then we always want H->SIZE
   4327 	     to be the size of the common symbol.  The code just above
   4328 	     won't fix the size if a common symbol becomes larger.  We
   4329 	     don't warn about a size change here, because that is
   4330 	     covered by --warn-common.  Allow changes between different
   4331 	     function types.  */
   4332 	  if (h->root.type == bfd_link_hash_common)
   4333 	    h->size = h->root.u.c.size;
   4334 
   4335 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   4336 	      && ((definition && !new_weak)
   4337 		  || (old_weak && h->root.type == bfd_link_hash_common)
   4338 		  || h->type == STT_NOTYPE))
   4339 	    {
   4340 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   4341 
   4342 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   4343 		 symbol.  */
   4344 	      if (type == STT_GNU_IFUNC
   4345 		  && (abfd->flags & DYNAMIC) != 0)
   4346 		type = STT_FUNC;
   4347 
   4348 	      if (h->type != type)
   4349 		{
   4350 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   4351 		    (*_bfd_error_handler)
   4352 		      (_("Warning: type of symbol `%s' changed"
   4353 			 " from %d to %d in %B"),
   4354 		       abfd, name, h->type, type);
   4355 
   4356 		  h->type = type;
   4357 		}
   4358 	    }
   4359 
   4360 	  /* Merge st_other field.  */
   4361 	  elf_merge_st_other (abfd, h, isym, definition, dynamic);
   4362 
   4363 	  /* We don't want to make debug symbol dynamic.  */
   4364 	  if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
   4365 	    dynsym = FALSE;
   4366 
   4367 	  /* Nor should we make plugin symbols dynamic.  */
   4368 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4369 	    dynsym = FALSE;
   4370 
   4371 	  if (definition)
   4372 	    {
   4373 	      h->target_internal = isym->st_target_internal;
   4374 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   4375 	    }
   4376 
   4377 	  if (definition && !dynamic)
   4378 	    {
   4379 	      char *p = strchr (name, ELF_VER_CHR);
   4380 	      if (p != NULL && p[1] != ELF_VER_CHR)
   4381 		{
   4382 		  /* Queue non-default versions so that .symver x, x@FOO
   4383 		     aliases can be checked.  */
   4384 		  if (!nondeflt_vers)
   4385 		    {
   4386 		      amt = ((isymend - isym + 1)
   4387 			     * sizeof (struct elf_link_hash_entry *));
   4388 		      nondeflt_vers =
   4389                           (struct elf_link_hash_entry **) bfd_malloc (amt);
   4390 		      if (!nondeflt_vers)
   4391 			goto error_free_vers;
   4392 		    }
   4393 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   4394 		}
   4395 	    }
   4396 
   4397 	  if (dynsym && h->dynindx == -1)
   4398 	    {
   4399 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4400 		goto error_free_vers;
   4401 	      if (h->u.weakdef != NULL
   4402 		  && ! new_weakdef
   4403 		  && h->u.weakdef->dynindx == -1)
   4404 		{
   4405 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
   4406 		    goto error_free_vers;
   4407 		}
   4408 	    }
   4409 	  else if (dynsym && h->dynindx != -1)
   4410 	    /* If the symbol already has a dynamic index, but
   4411 	       visibility says it should not be visible, turn it into
   4412 	       a local symbol.  */
   4413 	    switch (ELF_ST_VISIBILITY (h->other))
   4414 	      {
   4415 	      case STV_INTERNAL:
   4416 	      case STV_HIDDEN:
   4417 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   4418 		dynsym = FALSE;
   4419 		break;
   4420 	      }
   4421 
   4422 	  /* Don't add DT_NEEDED for references from the dummy bfd.  */
   4423 	  if (!add_needed
   4424 	      && definition
   4425 	      && ((dynsym
   4426 		   && h->ref_regular_nonweak
   4427 		   && (old_bfd == NULL
   4428 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
   4429 		  || (h->ref_dynamic_nonweak
   4430 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   4431 		      && !on_needed_list (elf_dt_name (abfd), htab->needed))))
   4432 	    {
   4433 	      int ret;
   4434 	      const char *soname = elf_dt_name (abfd);
   4435 
   4436 	      info->callbacks->minfo ("%!", soname, old_bfd,
   4437 				      h->root.root.string);
   4438 
   4439 	      /* A symbol from a library loaded via DT_NEEDED of some
   4440 		 other library is referenced by a regular object.
   4441 		 Add a DT_NEEDED entry for it.  Issue an error if
   4442 		 --no-add-needed is used and the reference was not
   4443 		 a weak one.  */
   4444 	      if (old_bfd != NULL
   4445 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   4446 		{
   4447 		  (*_bfd_error_handler)
   4448 		    (_("%B: undefined reference to symbol '%s'"),
   4449 		     old_bfd, name);
   4450 		  bfd_set_error (bfd_error_missing_dso);
   4451 		  goto error_free_vers;
   4452 		}
   4453 
   4454 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   4455                   (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   4456 
   4457 	      add_needed = TRUE;
   4458 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   4459 	      if (ret < 0)
   4460 		goto error_free_vers;
   4461 
   4462 	      BFD_ASSERT (ret == 0);
   4463 	    }
   4464 	}
   4465     }
   4466 
   4467   if (extversym != NULL)
   4468     {
   4469       free (extversym);
   4470       extversym = NULL;
   4471     }
   4472 
   4473   if (isymbuf != NULL)
   4474     {
   4475       free (isymbuf);
   4476       isymbuf = NULL;
   4477     }
   4478 
   4479   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4480     {
   4481       unsigned int i;
   4482 
   4483       /* Restore the symbol table.  */
   4484       old_ent = (char *) old_tab + tabsize;
   4485       memset (elf_sym_hashes (abfd), 0,
   4486 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   4487       htab->root.table.table = old_table;
   4488       htab->root.table.size = old_size;
   4489       htab->root.table.count = old_count;
   4490       memcpy (htab->root.table.table, old_tab, tabsize);
   4491       htab->root.undefs = old_undefs;
   4492       htab->root.undefs_tail = old_undefs_tail;
   4493       _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
   4494       for (i = 0; i < htab->root.table.size; i++)
   4495 	{
   4496 	  struct bfd_hash_entry *p;
   4497 	  struct elf_link_hash_entry *h;
   4498 	  bfd_size_type size;
   4499 	  unsigned int alignment_power;
   4500 
   4501 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4502 	    {
   4503 	      h = (struct elf_link_hash_entry *) p;
   4504 	      if (h->root.type == bfd_link_hash_warning)
   4505 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4506 	      if (h->dynindx >= old_dynsymcount
   4507 		  && h->dynstr_index < old_dynstr_size)
   4508 		_bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
   4509 
   4510 	      /* Preserve the maximum alignment and size for common
   4511 		 symbols even if this dynamic lib isn't on DT_NEEDED
   4512 		 since it can still be loaded at run time by another
   4513 		 dynamic lib.  */
   4514 	      if (h->root.type == bfd_link_hash_common)
   4515 		{
   4516 		  size = h->root.u.c.size;
   4517 		  alignment_power = h->root.u.c.p->alignment_power;
   4518 		}
   4519 	      else
   4520 		{
   4521 		  size = 0;
   4522 		  alignment_power = 0;
   4523 		}
   4524 	      memcpy (p, old_ent, htab->root.table.entsize);
   4525 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4526 	      h = (struct elf_link_hash_entry *) p;
   4527 	      if (h->root.type == bfd_link_hash_warning)
   4528 		{
   4529 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
   4530 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4531 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4532 		}
   4533 	      if (h->root.type == bfd_link_hash_common)
   4534 		{
   4535 		  if (size > h->root.u.c.size)
   4536 		    h->root.u.c.size = size;
   4537 		  if (alignment_power > h->root.u.c.p->alignment_power)
   4538 		    h->root.u.c.p->alignment_power = alignment_power;
   4539 		}
   4540 	    }
   4541 	}
   4542 
   4543       /* Make a special call to the linker "notice" function to
   4544 	 tell it that symbols added for crefs may need to be removed.  */
   4545       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   4546 	goto error_free_vers;
   4547 
   4548       free (old_tab);
   4549       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   4550 			   alloc_mark);
   4551       if (nondeflt_vers != NULL)
   4552 	free (nondeflt_vers);
   4553       return TRUE;
   4554     }
   4555 
   4556   if (old_tab != NULL)
   4557     {
   4558       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   4559 	goto error_free_vers;
   4560       free (old_tab);
   4561       old_tab = NULL;
   4562     }
   4563 
   4564   /* Now that all the symbols from this input file are created, handle
   4565      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
   4566   if (nondeflt_vers != NULL)
   4567     {
   4568       bfd_size_type cnt, symidx;
   4569 
   4570       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   4571 	{
   4572 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   4573 	  char *shortname, *p;
   4574 
   4575 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   4576 	  if (p == NULL
   4577 	      || (h->root.type != bfd_link_hash_defined
   4578 		  && h->root.type != bfd_link_hash_defweak))
   4579 	    continue;
   4580 
   4581 	  amt = p - h->root.root.string;
   4582 	  shortname = (char *) bfd_malloc (amt + 1);
   4583 	  if (!shortname)
   4584 	    goto error_free_vers;
   4585 	  memcpy (shortname, h->root.root.string, amt);
   4586 	  shortname[amt] = '\0';
   4587 
   4588 	  hi = (struct elf_link_hash_entry *)
   4589 	       bfd_link_hash_lookup (&htab->root, shortname,
   4590 				     FALSE, FALSE, FALSE);
   4591 	  if (hi != NULL
   4592 	      && hi->root.type == h->root.type
   4593 	      && hi->root.u.def.value == h->root.u.def.value
   4594 	      && hi->root.u.def.section == h->root.u.def.section)
   4595 	    {
   4596 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   4597 	      hi->root.type = bfd_link_hash_indirect;
   4598 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   4599 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   4600 	      sym_hash = elf_sym_hashes (abfd);
   4601 	      if (sym_hash)
   4602 		for (symidx = 0; symidx < extsymcount; ++symidx)
   4603 		  if (sym_hash[symidx] == hi)
   4604 		    {
   4605 		      sym_hash[symidx] = h;
   4606 		      break;
   4607 		    }
   4608 	    }
   4609 	  free (shortname);
   4610 	}
   4611       free (nondeflt_vers);
   4612       nondeflt_vers = NULL;
   4613     }
   4614 
   4615   /* Now set the weakdefs field correctly for all the weak defined
   4616      symbols we found.  The only way to do this is to search all the
   4617      symbols.  Since we only need the information for non functions in
   4618      dynamic objects, that's the only time we actually put anything on
   4619      the list WEAKS.  We need this information so that if a regular
   4620      object refers to a symbol defined weakly in a dynamic object, the
   4621      real symbol in the dynamic object is also put in the dynamic
   4622      symbols; we also must arrange for both symbols to point to the
   4623      same memory location.  We could handle the general case of symbol
   4624      aliasing, but a general symbol alias can only be generated in
   4625      assembler code, handling it correctly would be very time
   4626      consuming, and other ELF linkers don't handle general aliasing
   4627      either.  */
   4628   if (weaks != NULL)
   4629     {
   4630       struct elf_link_hash_entry **hpp;
   4631       struct elf_link_hash_entry **hppend;
   4632       struct elf_link_hash_entry **sorted_sym_hash;
   4633       struct elf_link_hash_entry *h;
   4634       size_t sym_count;
   4635 
   4636       /* Since we have to search the whole symbol list for each weak
   4637 	 defined symbol, search time for N weak defined symbols will be
   4638 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   4639       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   4640       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
   4641       if (sorted_sym_hash == NULL)
   4642 	goto error_return;
   4643       sym_hash = sorted_sym_hash;
   4644       hpp = elf_sym_hashes (abfd);
   4645       hppend = hpp + extsymcount;
   4646       sym_count = 0;
   4647       for (; hpp < hppend; hpp++)
   4648 	{
   4649 	  h = *hpp;
   4650 	  if (h != NULL
   4651 	      && h->root.type == bfd_link_hash_defined
   4652 	      && !bed->is_function_type (h->type))
   4653 	    {
   4654 	      *sym_hash = h;
   4655 	      sym_hash++;
   4656 	      sym_count++;
   4657 	    }
   4658 	}
   4659 
   4660       qsort (sorted_sym_hash, sym_count,
   4661 	     sizeof (struct elf_link_hash_entry *),
   4662 	     elf_sort_symbol);
   4663 
   4664       while (weaks != NULL)
   4665 	{
   4666 	  struct elf_link_hash_entry *hlook;
   4667 	  asection *slook;
   4668 	  bfd_vma vlook;
   4669 	  size_t i, j, idx = 0;
   4670 
   4671 	  hlook = weaks;
   4672 	  weaks = hlook->u.weakdef;
   4673 	  hlook->u.weakdef = NULL;
   4674 
   4675 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
   4676 		      || hlook->root.type == bfd_link_hash_defweak
   4677 		      || hlook->root.type == bfd_link_hash_common
   4678 		      || hlook->root.type == bfd_link_hash_indirect);
   4679 	  slook = hlook->root.u.def.section;
   4680 	  vlook = hlook->root.u.def.value;
   4681 
   4682 	  i = 0;
   4683 	  j = sym_count;
   4684 	  while (i != j)
   4685 	    {
   4686 	      bfd_signed_vma vdiff;
   4687 	      idx = (i + j) / 2;
   4688 	      h = sorted_sym_hash[idx];
   4689 	      vdiff = vlook - h->root.u.def.value;
   4690 	      if (vdiff < 0)
   4691 		j = idx;
   4692 	      else if (vdiff > 0)
   4693 		i = idx + 1;
   4694 	      else
   4695 		{
   4696 		  long sdiff = slook->id - h->root.u.def.section->id;
   4697 		  if (sdiff < 0)
   4698 		    j = idx;
   4699 		  else if (sdiff > 0)
   4700 		    i = idx + 1;
   4701 		  else
   4702 		    break;
   4703 		}
   4704 	    }
   4705 
   4706 	  /* We didn't find a value/section match.  */
   4707 	  if (i == j)
   4708 	    continue;
   4709 
   4710 	  /* With multiple aliases, or when the weak symbol is already
   4711 	     strongly defined, we have multiple matching symbols and
   4712 	     the binary search above may land on any of them.  Step
   4713 	     one past the matching symbol(s).  */
   4714 	  while (++idx != j)
   4715 	    {
   4716 	      h = sorted_sym_hash[idx];
   4717 	      if (h->root.u.def.section != slook
   4718 		  || h->root.u.def.value != vlook)
   4719 		break;
   4720 	    }
   4721 
   4722 	  /* Now look back over the aliases.  Since we sorted by size
   4723 	     as well as value and section, we'll choose the one with
   4724 	     the largest size.  */
   4725 	  while (idx-- != i)
   4726 	    {
   4727 	      h = sorted_sym_hash[idx];
   4728 
   4729 	      /* Stop if value or section doesn't match.  */
   4730 	      if (h->root.u.def.section != slook
   4731 		  || h->root.u.def.value != vlook)
   4732 		break;
   4733 	      else if (h != hlook)
   4734 		{
   4735 		  hlook->u.weakdef = h;
   4736 
   4737 		  /* If the weak definition is in the list of dynamic
   4738 		     symbols, make sure the real definition is put
   4739 		     there as well.  */
   4740 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   4741 		    {
   4742 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4743 			{
   4744 			err_free_sym_hash:
   4745 			  free (sorted_sym_hash);
   4746 			  goto error_return;
   4747 			}
   4748 		    }
   4749 
   4750 		  /* If the real definition is in the list of dynamic
   4751 		     symbols, make sure the weak definition is put
   4752 		     there as well.  If we don't do this, then the
   4753 		     dynamic loader might not merge the entries for the
   4754 		     real definition and the weak definition.  */
   4755 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   4756 		    {
   4757 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   4758 			goto err_free_sym_hash;
   4759 		    }
   4760 		  break;
   4761 		}
   4762 	    }
   4763 	}
   4764 
   4765       free (sorted_sym_hash);
   4766     }
   4767 
   4768   if (bed->check_directives
   4769       && !(*bed->check_directives) (abfd, info))
   4770     return FALSE;
   4771 
   4772   /* If this object is the same format as the output object, and it is
   4773      not a shared library, then let the backend look through the
   4774      relocs.
   4775 
   4776      This is required to build global offset table entries and to
   4777      arrange for dynamic relocs.  It is not required for the
   4778      particular common case of linking non PIC code, even when linking
   4779      against shared libraries, but unfortunately there is no way of
   4780      knowing whether an object file has been compiled PIC or not.
   4781      Looking through the relocs is not particularly time consuming.
   4782      The problem is that we must either (1) keep the relocs in memory,
   4783      which causes the linker to require additional runtime memory or
   4784      (2) read the relocs twice from the input file, which wastes time.
   4785      This would be a good case for using mmap.
   4786 
   4787      I have no idea how to handle linking PIC code into a file of a
   4788      different format.  It probably can't be done.  */
   4789   if (! dynamic
   4790       && is_elf_hash_table (htab)
   4791       && bed->check_relocs != NULL
   4792       && elf_object_id (abfd) == elf_hash_table_id (htab)
   4793       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   4794     {
   4795       asection *o;
   4796 
   4797       for (o = abfd->sections; o != NULL; o = o->next)
   4798 	{
   4799 	  Elf_Internal_Rela *internal_relocs;
   4800 	  bfd_boolean ok;
   4801 
   4802 	  if ((o->flags & SEC_RELOC) == 0
   4803 	      || o->reloc_count == 0
   4804 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   4805 		  && (o->flags & SEC_DEBUGGING) != 0)
   4806 	      || bfd_is_abs_section (o->output_section))
   4807 	    continue;
   4808 
   4809 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   4810 						       info->keep_memory);
   4811 	  if (internal_relocs == NULL)
   4812 	    goto error_return;
   4813 
   4814 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
   4815 
   4816 	  if (elf_section_data (o)->relocs != internal_relocs)
   4817 	    free (internal_relocs);
   4818 
   4819 	  if (! ok)
   4820 	    goto error_return;
   4821 	}
   4822     }
   4823 
   4824   /* If this is a non-traditional link, try to optimize the handling
   4825      of the .stab/.stabstr sections.  */
   4826   if (! dynamic
   4827       && ! info->traditional_format
   4828       && is_elf_hash_table (htab)
   4829       && (info->strip != strip_all && info->strip != strip_debugger))
   4830     {
   4831       asection *stabstr;
   4832 
   4833       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   4834       if (stabstr != NULL)
   4835 	{
   4836 	  bfd_size_type string_offset = 0;
   4837 	  asection *stab;
   4838 
   4839 	  for (stab = abfd->sections; stab; stab = stab->next)
   4840 	    if (CONST_STRNEQ (stab->name, ".stab")
   4841 		&& (!stab->name[5] ||
   4842 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   4843 		&& (stab->flags & SEC_MERGE) == 0
   4844 		&& !bfd_is_abs_section (stab->output_section))
   4845 	      {
   4846 		struct bfd_elf_section_data *secdata;
   4847 
   4848 		secdata = elf_section_data (stab);
   4849 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   4850 					       stabstr, &secdata->sec_info,
   4851 					       &string_offset))
   4852 		  goto error_return;
   4853 		if (secdata->sec_info)
   4854 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   4855 	    }
   4856 	}
   4857     }
   4858 
   4859   if (is_elf_hash_table (htab) && add_needed)
   4860     {
   4861       /* Add this bfd to the loaded list.  */
   4862       struct elf_link_loaded_list *n;
   4863 
   4864       n = (struct elf_link_loaded_list *)
   4865           bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
   4866       if (n == NULL)
   4867 	goto error_return;
   4868       n->abfd = abfd;
   4869       n->next = htab->loaded;
   4870       htab->loaded = n;
   4871     }
   4872 
   4873   return TRUE;
   4874 
   4875  error_free_vers:
   4876   if (old_tab != NULL)
   4877     free (old_tab);
   4878   if (nondeflt_vers != NULL)
   4879     free (nondeflt_vers);
   4880   if (extversym != NULL)
   4881     free (extversym);
   4882  error_free_sym:
   4883   if (isymbuf != NULL)
   4884     free (isymbuf);
   4885  error_return:
   4886   return FALSE;
   4887 }
   4888 
   4889 /* Return the linker hash table entry of a symbol that might be
   4890    satisfied by an archive symbol.  Return -1 on error.  */
   4891 
   4892 struct elf_link_hash_entry *
   4893 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   4894 				struct bfd_link_info *info,
   4895 				const char *name)
   4896 {
   4897   struct elf_link_hash_entry *h;
   4898   char *p, *copy;
   4899   size_t len, first;
   4900 
   4901   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
   4902   if (h != NULL)
   4903     return h;
   4904 
   4905   /* If this is a default version (the name contains @@), look up the
   4906      symbol again with only one `@' as well as without the version.
   4907      The effect is that references to the symbol with and without the
   4908      version will be matched by the default symbol in the archive.  */
   4909 
   4910   p = strchr (name, ELF_VER_CHR);
   4911   if (p == NULL || p[1] != ELF_VER_CHR)
   4912     return h;
   4913 
   4914   /* First check with only one `@'.  */
   4915   len = strlen (name);
   4916   copy = (char *) bfd_alloc (abfd, len);
   4917   if (copy == NULL)
   4918     return (struct elf_link_hash_entry *) 0 - 1;
   4919 
   4920   first = p - name + 1;
   4921   memcpy (copy, name, first);
   4922   memcpy (copy + first, name + first + 1, len - first);
   4923 
   4924   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
   4925   if (h == NULL)
   4926     {
   4927       /* We also need to check references to the symbol without the
   4928 	 version.  */
   4929       copy[first - 1] = '\0';
   4930       h = elf_link_hash_lookup (elf_hash_table (info), copy,
   4931 				FALSE, FALSE, TRUE);
   4932     }
   4933 
   4934   bfd_release (abfd, copy);
   4935   return h;
   4936 }
   4937 
   4938 /* Add symbols from an ELF archive file to the linker hash table.  We
   4939    don't use _bfd_generic_link_add_archive_symbols because we need to
   4940    handle versioned symbols.
   4941 
   4942    Fortunately, ELF archive handling is simpler than that done by
   4943    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   4944    oddities.  In ELF, if we find a symbol in the archive map, and the
   4945    symbol is currently undefined, we know that we must pull in that
   4946    object file.
   4947 
   4948    Unfortunately, we do have to make multiple passes over the symbol
   4949    table until nothing further is resolved.  */
   4950 
   4951 static bfd_boolean
   4952 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   4953 {
   4954   symindex c;
   4955   unsigned char *included = NULL;
   4956   carsym *symdefs;
   4957   bfd_boolean loop;
   4958   bfd_size_type amt;
   4959   const struct elf_backend_data *bed;
   4960   struct elf_link_hash_entry * (*archive_symbol_lookup)
   4961     (bfd *, struct bfd_link_info *, const char *);
   4962 
   4963   if (! bfd_has_map (abfd))
   4964     {
   4965       /* An empty archive is a special case.  */
   4966       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   4967 	return TRUE;
   4968       bfd_set_error (bfd_error_no_armap);
   4969       return FALSE;
   4970     }
   4971 
   4972   /* Keep track of all symbols we know to be already defined, and all
   4973      files we know to be already included.  This is to speed up the
   4974      second and subsequent passes.  */
   4975   c = bfd_ardata (abfd)->symdef_count;
   4976   if (c == 0)
   4977     return TRUE;
   4978   amt = c;
   4979   amt *= sizeof (*included);
   4980   included = (unsigned char *) bfd_zmalloc (amt);
   4981   if (included == NULL)
   4982     return FALSE;
   4983 
   4984   symdefs = bfd_ardata (abfd)->symdefs;
   4985   bed = get_elf_backend_data (abfd);
   4986   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   4987 
   4988   do
   4989     {
   4990       file_ptr last;
   4991       symindex i;
   4992       carsym *symdef;
   4993       carsym *symdefend;
   4994 
   4995       loop = FALSE;
   4996       last = -1;
   4997 
   4998       symdef = symdefs;
   4999       symdefend = symdef + c;
   5000       for (i = 0; symdef < symdefend; symdef++, i++)
   5001 	{
   5002 	  struct elf_link_hash_entry *h;
   5003 	  bfd *element;
   5004 	  struct bfd_link_hash_entry *undefs_tail;
   5005 	  symindex mark;
   5006 
   5007 	  if (included[i])
   5008 	    continue;
   5009 	  if (symdef->file_offset == last)
   5010 	    {
   5011 	      included[i] = TRUE;
   5012 	      continue;
   5013 	    }
   5014 
   5015 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   5016 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
   5017 	    goto error_return;
   5018 
   5019 	  if (h == NULL)
   5020 	    continue;
   5021 
   5022 	  if (h->root.type == bfd_link_hash_common)
   5023 	    {
   5024 	      /* We currently have a common symbol.  The archive map contains
   5025 		 a reference to this symbol, so we may want to include it.  We
   5026 		 only want to include it however, if this archive element
   5027 		 contains a definition of the symbol, not just another common
   5028 		 declaration of it.
   5029 
   5030 		 Unfortunately some archivers (including GNU ar) will put
   5031 		 declarations of common symbols into their archive maps, as
   5032 		 well as real definitions, so we cannot just go by the archive
   5033 		 map alone.  Instead we must read in the element's symbol
   5034 		 table and check that to see what kind of symbol definition
   5035 		 this is.  */
   5036 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   5037 		continue;
   5038 	    }
   5039 	  else if (h->root.type != bfd_link_hash_undefined)
   5040 	    {
   5041 	      if (h->root.type != bfd_link_hash_undefweak)
   5042 		/* Symbol must be defined.  Don't check it again.  */
   5043 		included[i] = TRUE;
   5044 	      continue;
   5045 	    }
   5046 
   5047 	  /* We need to include this archive member.  */
   5048 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   5049 	  if (element == NULL)
   5050 	    goto error_return;
   5051 
   5052 	  if (! bfd_check_format (element, bfd_object))
   5053 	    goto error_return;
   5054 
   5055 	  undefs_tail = info->hash->undefs_tail;
   5056 
   5057 	  if (!(*info->callbacks
   5058 		->add_archive_element) (info, element, symdef->name, &element))
   5059 	    goto error_return;
   5060 	  if (!bfd_link_add_symbols (element, info))
   5061 	    goto error_return;
   5062 
   5063 	  /* If there are any new undefined symbols, we need to make
   5064 	     another pass through the archive in order to see whether
   5065 	     they can be defined.  FIXME: This isn't perfect, because
   5066 	     common symbols wind up on undefs_tail and because an
   5067 	     undefined symbol which is defined later on in this pass
   5068 	     does not require another pass.  This isn't a bug, but it
   5069 	     does make the code less efficient than it could be.  */
   5070 	  if (undefs_tail != info->hash->undefs_tail)
   5071 	    loop = TRUE;
   5072 
   5073 	  /* Look backward to mark all symbols from this object file
   5074 	     which we have already seen in this pass.  */
   5075 	  mark = i;
   5076 	  do
   5077 	    {
   5078 	      included[mark] = TRUE;
   5079 	      if (mark == 0)
   5080 		break;
   5081 	      --mark;
   5082 	    }
   5083 	  while (symdefs[mark].file_offset == symdef->file_offset);
   5084 
   5085 	  /* We mark subsequent symbols from this object file as we go
   5086 	     on through the loop.  */
   5087 	  last = symdef->file_offset;
   5088 	}
   5089     }
   5090   while (loop);
   5091 
   5092   free (included);
   5093 
   5094   return TRUE;
   5095 
   5096  error_return:
   5097   if (included != NULL)
   5098     free (included);
   5099   return FALSE;
   5100 }
   5101 
   5102 /* Given an ELF BFD, add symbols to the global hash table as
   5103    appropriate.  */
   5104 
   5105 bfd_boolean
   5106 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   5107 {
   5108   switch (bfd_get_format (abfd))
   5109     {
   5110     case bfd_object:
   5111       return elf_link_add_object_symbols (abfd, info);
   5112     case bfd_archive:
   5113       return elf_link_add_archive_symbols (abfd, info);
   5114     default:
   5115       bfd_set_error (bfd_error_wrong_format);
   5116       return FALSE;
   5117     }
   5118 }
   5119 
   5120 struct hash_codes_info
   5122 {
   5123   unsigned long *hashcodes;
   5124   bfd_boolean error;
   5125 };
   5126 
   5127 /* This function will be called though elf_link_hash_traverse to store
   5128    all hash value of the exported symbols in an array.  */
   5129 
   5130 static bfd_boolean
   5131 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   5132 {
   5133   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   5134   const char *name;
   5135   char *p;
   5136   unsigned long ha;
   5137   char *alc = NULL;
   5138 
   5139   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5140   if (h->dynindx == -1)
   5141     return TRUE;
   5142 
   5143   name = h->root.root.string;
   5144   p = strchr (name, ELF_VER_CHR);
   5145   if (p != NULL)
   5146     {
   5147       alc = (char *) bfd_malloc (p - name + 1);
   5148       if (alc == NULL)
   5149 	{
   5150 	  inf->error = TRUE;
   5151 	  return FALSE;
   5152 	}
   5153       memcpy (alc, name, p - name);
   5154       alc[p - name] = '\0';
   5155       name = alc;
   5156     }
   5157 
   5158   /* Compute the hash value.  */
   5159   ha = bfd_elf_hash (name);
   5160 
   5161   /* Store the found hash value in the array given as the argument.  */
   5162   *(inf->hashcodes)++ = ha;
   5163 
   5164   /* And store it in the struct so that we can put it in the hash table
   5165      later.  */
   5166   h->u.elf_hash_value = ha;
   5167 
   5168   if (alc != NULL)
   5169     free (alc);
   5170 
   5171   return TRUE;
   5172 }
   5173 
   5174 struct collect_gnu_hash_codes
   5175 {
   5176   bfd *output_bfd;
   5177   const struct elf_backend_data *bed;
   5178   unsigned long int nsyms;
   5179   unsigned long int maskbits;
   5180   unsigned long int *hashcodes;
   5181   unsigned long int *hashval;
   5182   unsigned long int *indx;
   5183   unsigned long int *counts;
   5184   bfd_vma *bitmask;
   5185   bfd_byte *contents;
   5186   long int min_dynindx;
   5187   unsigned long int bucketcount;
   5188   unsigned long int symindx;
   5189   long int local_indx;
   5190   long int shift1, shift2;
   5191   unsigned long int mask;
   5192   bfd_boolean error;
   5193 };
   5194 
   5195 /* This function will be called though elf_link_hash_traverse to store
   5196    all hash value of the exported symbols in an array.  */
   5197 
   5198 static bfd_boolean
   5199 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   5200 {
   5201   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5202   const char *name;
   5203   char *p;
   5204   unsigned long ha;
   5205   char *alc = NULL;
   5206 
   5207   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5208   if (h->dynindx == -1)
   5209     return TRUE;
   5210 
   5211   /* Ignore also local symbols and undefined symbols.  */
   5212   if (! (*s->bed->elf_hash_symbol) (h))
   5213     return TRUE;
   5214 
   5215   name = h->root.root.string;
   5216   p = strchr (name, ELF_VER_CHR);
   5217   if (p != NULL)
   5218     {
   5219       alc = (char *) bfd_malloc (p - name + 1);
   5220       if (alc == NULL)
   5221 	{
   5222 	  s->error = TRUE;
   5223 	  return FALSE;
   5224 	}
   5225       memcpy (alc, name, p - name);
   5226       alc[p - name] = '\0';
   5227       name = alc;
   5228     }
   5229 
   5230   /* Compute the hash value.  */
   5231   ha = bfd_elf_gnu_hash (name);
   5232 
   5233   /* Store the found hash value in the array for compute_bucket_count,
   5234      and also for .dynsym reordering purposes.  */
   5235   s->hashcodes[s->nsyms] = ha;
   5236   s->hashval[h->dynindx] = ha;
   5237   ++s->nsyms;
   5238   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   5239     s->min_dynindx = h->dynindx;
   5240 
   5241   if (alc != NULL)
   5242     free (alc);
   5243 
   5244   return TRUE;
   5245 }
   5246 
   5247 /* This function will be called though elf_link_hash_traverse to do
   5248    final dynaminc symbol renumbering.  */
   5249 
   5250 static bfd_boolean
   5251 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
   5252 {
   5253   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5254   unsigned long int bucket;
   5255   unsigned long int val;
   5256 
   5257   /* Ignore indirect symbols.  */
   5258   if (h->dynindx == -1)
   5259     return TRUE;
   5260 
   5261   /* Ignore also local symbols and undefined symbols.  */
   5262   if (! (*s->bed->elf_hash_symbol) (h))
   5263     {
   5264       if (h->dynindx >= s->min_dynindx)
   5265 	h->dynindx = s->local_indx++;
   5266       return TRUE;
   5267     }
   5268 
   5269   bucket = s->hashval[h->dynindx] % s->bucketcount;
   5270   val = (s->hashval[h->dynindx] >> s->shift1)
   5271 	& ((s->maskbits >> s->shift1) - 1);
   5272   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   5273   s->bitmask[val]
   5274     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   5275   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   5276   if (s->counts[bucket] == 1)
   5277     /* Last element terminates the chain.  */
   5278     val |= 1;
   5279   bfd_put_32 (s->output_bfd, val,
   5280 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   5281   --s->counts[bucket];
   5282   h->dynindx = s->indx[bucket]++;
   5283   return TRUE;
   5284 }
   5285 
   5286 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   5287 
   5288 bfd_boolean
   5289 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   5290 {
   5291   return !(h->forced_local
   5292 	   || h->root.type == bfd_link_hash_undefined
   5293 	   || h->root.type == bfd_link_hash_undefweak
   5294 	   || ((h->root.type == bfd_link_hash_defined
   5295 		|| h->root.type == bfd_link_hash_defweak)
   5296 	       && h->root.u.def.section->output_section == NULL));
   5297 }
   5298 
   5299 /* Array used to determine the number of hash table buckets to use
   5300    based on the number of symbols there are.  If there are fewer than
   5301    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   5302    fewer than 37 we use 17 buckets, and so forth.  We never use more
   5303    than 32771 buckets.  */
   5304 
   5305 static const size_t elf_buckets[] =
   5306 {
   5307   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   5308   16411, 32771, 0
   5309 };
   5310 
   5311 /* Compute bucket count for hashing table.  We do not use a static set
   5312    of possible tables sizes anymore.  Instead we determine for all
   5313    possible reasonable sizes of the table the outcome (i.e., the
   5314    number of collisions etc) and choose the best solution.  The
   5315    weighting functions are not too simple to allow the table to grow
   5316    without bounds.  Instead one of the weighting factors is the size.
   5317    Therefore the result is always a good payoff between few collisions
   5318    (= short chain lengths) and table size.  */
   5319 static size_t
   5320 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5321 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   5322 		      unsigned long int nsyms,
   5323 		      int gnu_hash)
   5324 {
   5325   size_t best_size = 0;
   5326   unsigned long int i;
   5327 
   5328   /* We have a problem here.  The following code to optimize the table
   5329      size requires an integer type with more the 32 bits.  If
   5330      BFD_HOST_U_64_BIT is set we know about such a type.  */
   5331 #ifdef BFD_HOST_U_64_BIT
   5332   if (info->optimize)
   5333     {
   5334       size_t minsize;
   5335       size_t maxsize;
   5336       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
   5337       bfd *dynobj = elf_hash_table (info)->dynobj;
   5338       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   5339       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   5340       unsigned long int *counts;
   5341       bfd_size_type amt;
   5342       unsigned int no_improvement_count = 0;
   5343 
   5344       /* Possible optimization parameters: if we have NSYMS symbols we say
   5345 	 that the hashing table must at least have NSYMS/4 and at most
   5346 	 2*NSYMS buckets.  */
   5347       minsize = nsyms / 4;
   5348       if (minsize == 0)
   5349 	minsize = 1;
   5350       best_size = maxsize = nsyms * 2;
   5351       if (gnu_hash)
   5352 	{
   5353 	  if (minsize < 2)
   5354 	    minsize = 2;
   5355 	  if ((best_size & 31) == 0)
   5356 	    ++best_size;
   5357 	}
   5358 
   5359       /* Create array where we count the collisions in.  We must use bfd_malloc
   5360 	 since the size could be large.  */
   5361       amt = maxsize;
   5362       amt *= sizeof (unsigned long int);
   5363       counts = (unsigned long int *) bfd_malloc (amt);
   5364       if (counts == NULL)
   5365 	return 0;
   5366 
   5367       /* Compute the "optimal" size for the hash table.  The criteria is a
   5368 	 minimal chain length.  The minor criteria is (of course) the size
   5369 	 of the table.  */
   5370       for (i = minsize; i < maxsize; ++i)
   5371 	{
   5372 	  /* Walk through the array of hashcodes and count the collisions.  */
   5373 	  BFD_HOST_U_64_BIT max;
   5374 	  unsigned long int j;
   5375 	  unsigned long int fact;
   5376 
   5377 	  if (gnu_hash && (i & 31) == 0)
   5378 	    continue;
   5379 
   5380 	  memset (counts, '\0', i * sizeof (unsigned long int));
   5381 
   5382 	  /* Determine how often each hash bucket is used.  */
   5383 	  for (j = 0; j < nsyms; ++j)
   5384 	    ++counts[hashcodes[j] % i];
   5385 
   5386 	  /* For the weight function we need some information about the
   5387 	     pagesize on the target.  This is information need not be 100%
   5388 	     accurate.  Since this information is not available (so far) we
   5389 	     define it here to a reasonable default value.  If it is crucial
   5390 	     to have a better value some day simply define this value.  */
   5391 # ifndef BFD_TARGET_PAGESIZE
   5392 #  define BFD_TARGET_PAGESIZE	(4096)
   5393 # endif
   5394 
   5395 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   5396 	     and the chains.  */
   5397 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   5398 
   5399 # if 1
   5400 	  /* Variant 1: optimize for short chains.  We add the squares
   5401 	     of all the chain lengths (which favors many small chain
   5402 	     over a few long chains).  */
   5403 	  for (j = 0; j < i; ++j)
   5404 	    max += counts[j] * counts[j];
   5405 
   5406 	  /* This adds penalties for the overall size of the table.  */
   5407 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5408 	  max *= fact * fact;
   5409 # else
   5410 	  /* Variant 2: Optimize a lot more for small table.  Here we
   5411 	     also add squares of the size but we also add penalties for
   5412 	     empty slots (the +1 term).  */
   5413 	  for (j = 0; j < i; ++j)
   5414 	    max += (1 + counts[j]) * (1 + counts[j]);
   5415 
   5416 	  /* The overall size of the table is considered, but not as
   5417 	     strong as in variant 1, where it is squared.  */
   5418 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5419 	  max *= fact;
   5420 # endif
   5421 
   5422 	  /* Compare with current best results.  */
   5423 	  if (max < best_chlen)
   5424 	    {
   5425 	      best_chlen = max;
   5426 	      best_size = i;
   5427               no_improvement_count = 0;
   5428 	    }
   5429 	  /* PR 11843: Avoid futile long searches for the best bucket size
   5430 	     when there are a large number of symbols.  */
   5431 	  else if (++no_improvement_count == 100)
   5432 	    break;
   5433 	}
   5434 
   5435       free (counts);
   5436     }
   5437   else
   5438 #endif /* defined (BFD_HOST_U_64_BIT) */
   5439     {
   5440       /* This is the fallback solution if no 64bit type is available or if we
   5441 	 are not supposed to spend much time on optimizations.  We select the
   5442 	 bucket count using a fixed set of numbers.  */
   5443       for (i = 0; elf_buckets[i] != 0; i++)
   5444 	{
   5445 	  best_size = elf_buckets[i];
   5446 	  if (nsyms < elf_buckets[i + 1])
   5447 	    break;
   5448 	}
   5449       if (gnu_hash && best_size < 2)
   5450 	best_size = 2;
   5451     }
   5452 
   5453   return best_size;
   5454 }
   5455 
   5456 /* Size any SHT_GROUP section for ld -r.  */
   5457 
   5458 bfd_boolean
   5459 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   5460 {
   5461   bfd *ibfd;
   5462 
   5463   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   5464     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   5465 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   5466       return FALSE;
   5467   return TRUE;
   5468 }
   5469 
   5470 /* Set a default stack segment size.  The value in INFO wins.  If it
   5471    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   5472    undefined it is initialized.  */
   5473 
   5474 bfd_boolean
   5475 bfd_elf_stack_segment_size (bfd *output_bfd,
   5476 			    struct bfd_link_info *info,
   5477 			    const char *legacy_symbol,
   5478 			    bfd_vma default_size)
   5479 {
   5480   struct elf_link_hash_entry *h = NULL;
   5481 
   5482   /* Look for legacy symbol.  */
   5483   if (legacy_symbol)
   5484     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   5485 			      FALSE, FALSE, FALSE);
   5486   if (h && (h->root.type == bfd_link_hash_defined
   5487 	    || h->root.type == bfd_link_hash_defweak)
   5488       && h->def_regular
   5489       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   5490     {
   5491       /* The symbol has no type if specified on the command line.  */
   5492       h->type = STT_OBJECT;
   5493       if (info->stacksize)
   5494 	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
   5495 			       output_bfd, legacy_symbol);
   5496       else if (h->root.u.def.section != bfd_abs_section_ptr)
   5497 	(*_bfd_error_handler) (_("%B: %s not absolute"),
   5498 			       output_bfd, legacy_symbol);
   5499       else
   5500 	info->stacksize = h->root.u.def.value;
   5501     }
   5502 
   5503   if (!info->stacksize)
   5504     /* If the user didn't set a size, or explicitly inhibit the
   5505        size, set it now.  */
   5506     info->stacksize = default_size;
   5507 
   5508   /* Provide the legacy symbol, if it is referenced.  */
   5509   if (h && (h->root.type == bfd_link_hash_undefined
   5510 	    || h->root.type == bfd_link_hash_undefweak))
   5511     {
   5512       struct bfd_link_hash_entry *bh = NULL;
   5513 
   5514       if (!(_bfd_generic_link_add_one_symbol
   5515 	    (info, output_bfd, legacy_symbol,
   5516 	     BSF_GLOBAL, bfd_abs_section_ptr,
   5517 	     info->stacksize >= 0 ? info->stacksize : 0,
   5518 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
   5519 	return FALSE;
   5520 
   5521       h = (struct elf_link_hash_entry *) bh;
   5522       h->def_regular = 1;
   5523       h->type = STT_OBJECT;
   5524     }
   5525 
   5526   return TRUE;
   5527 }
   5528 
   5529 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   5530    called by the ELF linker emulation before_allocation routine.  We
   5531    must set the sizes of the sections before the linker sets the
   5532    addresses of the various sections.  */
   5533 
   5534 bfd_boolean
   5535 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   5536 			       const char *soname,
   5537 			       const char *rpath,
   5538 			       const char *filter_shlib,
   5539 			       const char *audit,
   5540 			       const char *depaudit,
   5541 			       const char * const *auxiliary_filters,
   5542 			       struct bfd_link_info *info,
   5543 			       asection **sinterpptr)
   5544 {
   5545   bfd_size_type soname_indx;
   5546   bfd *dynobj;
   5547   const struct elf_backend_data *bed;
   5548   struct elf_info_failed asvinfo;
   5549 
   5550   *sinterpptr = NULL;
   5551 
   5552   soname_indx = (bfd_size_type) -1;
   5553 
   5554   if (!is_elf_hash_table (info->hash))
   5555     return TRUE;
   5556 
   5557   bed = get_elf_backend_data (output_bfd);
   5558 
   5559   /* Any syms created from now on start with -1 in
   5560      got.refcount/offset and plt.refcount/offset.  */
   5561   elf_hash_table (info)->init_got_refcount
   5562     = elf_hash_table (info)->init_got_offset;
   5563   elf_hash_table (info)->init_plt_refcount
   5564     = elf_hash_table (info)->init_plt_offset;
   5565 
   5566   if (info->relocatable
   5567       && !_bfd_elf_size_group_sections (info))
   5568     return FALSE;
   5569 
   5570   /* The backend may have to create some sections regardless of whether
   5571      we're dynamic or not.  */
   5572   if (bed->elf_backend_always_size_sections
   5573       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
   5574     return FALSE;
   5575 
   5576   /* Determine any GNU_STACK segment requirements, after the backend
   5577      has had a chance to set a default segment size.  */
   5578   if (info->execstack)
   5579     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   5580   else if (info->noexecstack)
   5581     elf_stack_flags (output_bfd) = PF_R | PF_W;
   5582   else
   5583     {
   5584       bfd *inputobj;
   5585       asection *notesec = NULL;
   5586       int exec = 0;
   5587 
   5588       for (inputobj = info->input_bfds;
   5589 	   inputobj;
   5590 	   inputobj = inputobj->link.next)
   5591 	{
   5592 	  asection *s;
   5593 
   5594 	  if (inputobj->flags
   5595 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   5596 	    continue;
   5597 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   5598 	  if (s)
   5599 	    {
   5600 	      if (s->flags & SEC_CODE)
   5601 		exec = PF_X;
   5602 	      notesec = s;
   5603 	    }
   5604 	  else if (bed->default_execstack)
   5605 	    exec = PF_X;
   5606 	}
   5607       if (notesec || info->stacksize > 0)
   5608 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   5609       if (notesec && exec && info->relocatable
   5610 	  && notesec->output_section != bfd_abs_section_ptr)
   5611 	notesec->output_section->flags |= SEC_CODE;
   5612     }
   5613 
   5614   dynobj = elf_hash_table (info)->dynobj;
   5615 
   5616   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   5617     {
   5618       struct elf_info_failed eif;
   5619       struct elf_link_hash_entry *h;
   5620       asection *dynstr;
   5621       struct bfd_elf_version_tree *t;
   5622       struct bfd_elf_version_expr *d;
   5623       asection *s;
   5624       bfd_boolean all_defined;
   5625 
   5626       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   5627       BFD_ASSERT (*sinterpptr != NULL || !info->executable);
   5628 
   5629       if (soname != NULL)
   5630 	{
   5631 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5632 					     soname, TRUE);
   5633 	  if (soname_indx == (bfd_size_type) -1
   5634 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   5635 	    return FALSE;
   5636 	}
   5637 
   5638       if (info->symbolic)
   5639 	{
   5640 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   5641 	    return FALSE;
   5642 	  info->flags |= DF_SYMBOLIC;
   5643 	}
   5644 
   5645       if (rpath != NULL)
   5646 	{
   5647 	  bfd_size_type indx;
   5648 	  bfd_vma tag;
   5649 
   5650 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   5651 				      TRUE);
   5652 	  if (indx == (bfd_size_type) -1)
   5653 	    return FALSE;
   5654 
   5655 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   5656 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   5657 	    return FALSE;
   5658 	}
   5659 
   5660       if (filter_shlib != NULL)
   5661 	{
   5662 	  bfd_size_type indx;
   5663 
   5664 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5665 				      filter_shlib, TRUE);
   5666 	  if (indx == (bfd_size_type) -1
   5667 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   5668 	    return FALSE;
   5669 	}
   5670 
   5671       if (auxiliary_filters != NULL)
   5672 	{
   5673 	  const char * const *p;
   5674 
   5675 	  for (p = auxiliary_filters; *p != NULL; p++)
   5676 	    {
   5677 	      bfd_size_type indx;
   5678 
   5679 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5680 					  *p, TRUE);
   5681 	      if (indx == (bfd_size_type) -1
   5682 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   5683 		return FALSE;
   5684 	    }
   5685 	}
   5686 
   5687       if (audit != NULL)
   5688 	{
   5689 	  bfd_size_type indx;
   5690 
   5691 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   5692 				      TRUE);
   5693 	  if (indx == (bfd_size_type) -1
   5694 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   5695 	    return FALSE;
   5696 	}
   5697 
   5698       if (depaudit != NULL)
   5699 	{
   5700 	  bfd_size_type indx;
   5701 
   5702 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   5703 				      TRUE);
   5704 	  if (indx == (bfd_size_type) -1
   5705 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   5706 	    return FALSE;
   5707 	}
   5708 
   5709       eif.info = info;
   5710       eif.failed = FALSE;
   5711 
   5712       /* If we are supposed to export all symbols into the dynamic symbol
   5713 	 table (this is not the normal case), then do so.  */
   5714       if (info->export_dynamic
   5715 	  || (info->executable && info->dynamic))
   5716 	{
   5717 	  elf_link_hash_traverse (elf_hash_table (info),
   5718 				  _bfd_elf_export_symbol,
   5719 				  &eif);
   5720 	  if (eif.failed)
   5721 	    return FALSE;
   5722 	}
   5723 
   5724       /* Make all global versions with definition.  */
   5725       for (t = info->version_info; t != NULL; t = t->next)
   5726 	for (d = t->globals.list; d != NULL; d = d->next)
   5727 	  if (!d->symver && d->literal)
   5728 	    {
   5729 	      const char *verstr, *name;
   5730 	      size_t namelen, verlen, newlen;
   5731 	      char *newname, *p, leading_char;
   5732 	      struct elf_link_hash_entry *newh;
   5733 
   5734 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   5735 	      name = d->pattern;
   5736 	      namelen = strlen (name) + (leading_char != '\0');
   5737 	      verstr = t->name;
   5738 	      verlen = strlen (verstr);
   5739 	      newlen = namelen + verlen + 3;
   5740 
   5741 	      newname = (char *) bfd_malloc (newlen);
   5742 	      if (newname == NULL)
   5743 		return FALSE;
   5744 	      newname[0] = leading_char;
   5745 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   5746 
   5747 	      /* Check the hidden versioned definition.  */
   5748 	      p = newname + namelen;
   5749 	      *p++ = ELF_VER_CHR;
   5750 	      memcpy (p, verstr, verlen + 1);
   5751 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   5752 					   newname, FALSE, FALSE,
   5753 					   FALSE);
   5754 	      if (newh == NULL
   5755 		  || (newh->root.type != bfd_link_hash_defined
   5756 		      && newh->root.type != bfd_link_hash_defweak))
   5757 		{
   5758 		  /* Check the default versioned definition.  */
   5759 		  *p++ = ELF_VER_CHR;
   5760 		  memcpy (p, verstr, verlen + 1);
   5761 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   5762 					       newname, FALSE, FALSE,
   5763 					       FALSE);
   5764 		}
   5765 	      free (newname);
   5766 
   5767 	      /* Mark this version if there is a definition and it is
   5768 		 not defined in a shared object.  */
   5769 	      if (newh != NULL
   5770 		  && !newh->def_dynamic
   5771 		  && (newh->root.type == bfd_link_hash_defined
   5772 		      || newh->root.type == bfd_link_hash_defweak))
   5773 		d->symver = 1;
   5774 	    }
   5775 
   5776       /* Attach all the symbols to their version information.  */
   5777       asvinfo.info = info;
   5778       asvinfo.failed = FALSE;
   5779 
   5780       elf_link_hash_traverse (elf_hash_table (info),
   5781 			      _bfd_elf_link_assign_sym_version,
   5782 			      &asvinfo);
   5783       if (asvinfo.failed)
   5784 	return FALSE;
   5785 
   5786       if (!info->allow_undefined_version)
   5787 	{
   5788 	  /* Check if all global versions have a definition.  */
   5789 	  all_defined = TRUE;
   5790 	  for (t = info->version_info; t != NULL; t = t->next)
   5791 	    for (d = t->globals.list; d != NULL; d = d->next)
   5792 	      if (d->literal && !d->symver && !d->script)
   5793 		{
   5794 		  (*_bfd_error_handler)
   5795 		    (_("%s: undefined version: %s"),
   5796 		     d->pattern, t->name);
   5797 		  all_defined = FALSE;
   5798 		}
   5799 
   5800 	  if (!all_defined)
   5801 	    {
   5802 	      bfd_set_error (bfd_error_bad_value);
   5803 	      return FALSE;
   5804 	    }
   5805 	}
   5806 
   5807       /* Find all symbols which were defined in a dynamic object and make
   5808 	 the backend pick a reasonable value for them.  */
   5809       elf_link_hash_traverse (elf_hash_table (info),
   5810 			      _bfd_elf_adjust_dynamic_symbol,
   5811 			      &eif);
   5812       if (eif.failed)
   5813 	return FALSE;
   5814 
   5815       /* Add some entries to the .dynamic section.  We fill in some of the
   5816 	 values later, in bfd_elf_final_link, but we must add the entries
   5817 	 now so that we know the final size of the .dynamic section.  */
   5818 
   5819       /* If there are initialization and/or finalization functions to
   5820 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   5821       h = (info->init_function
   5822 	   ? elf_link_hash_lookup (elf_hash_table (info),
   5823 				   info->init_function, FALSE,
   5824 				   FALSE, FALSE)
   5825 	   : NULL);
   5826       if (h != NULL
   5827 	  && (h->ref_regular
   5828 	      || h->def_regular))
   5829 	{
   5830 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   5831 	    return FALSE;
   5832 	}
   5833       h = (info->fini_function
   5834 	   ? elf_link_hash_lookup (elf_hash_table (info),
   5835 				   info->fini_function, FALSE,
   5836 				   FALSE, FALSE)
   5837 	   : NULL);
   5838       if (h != NULL
   5839 	  && (h->ref_regular
   5840 	      || h->def_regular))
   5841 	{
   5842 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   5843 	    return FALSE;
   5844 	}
   5845 
   5846       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   5847       if (s != NULL && s->linker_has_input)
   5848 	{
   5849 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   5850 	  if (! info->executable)
   5851 	    {
   5852 	      bfd *sub;
   5853 	      asection *o;
   5854 
   5855 	      for (sub = info->input_bfds; sub != NULL;
   5856 		   sub = sub->link.next)
   5857 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
   5858 		  for (o = sub->sections; o != NULL; o = o->next)
   5859 		    if (elf_section_data (o)->this_hdr.sh_type
   5860 			== SHT_PREINIT_ARRAY)
   5861 		      {
   5862 			(*_bfd_error_handler)
   5863 			  (_("%B: .preinit_array section is not allowed in DSO"),
   5864 			   sub);
   5865 			break;
   5866 		      }
   5867 
   5868 	      bfd_set_error (bfd_error_nonrepresentable_section);
   5869 	      return FALSE;
   5870 	    }
   5871 
   5872 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   5873 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   5874 	    return FALSE;
   5875 	}
   5876       s = bfd_get_section_by_name (output_bfd, ".init_array");
   5877       if (s != NULL && s->linker_has_input)
   5878 	{
   5879 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   5880 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   5881 	    return FALSE;
   5882 	}
   5883       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   5884       if (s != NULL && s->linker_has_input)
   5885 	{
   5886 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   5887 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   5888 	    return FALSE;
   5889 	}
   5890 
   5891       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   5892       /* If .dynstr is excluded from the link, we don't want any of
   5893 	 these tags.  Strictly, we should be checking each section
   5894 	 individually;  This quick check covers for the case where
   5895 	 someone does a /DISCARD/ : { *(*) }.  */
   5896       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   5897 	{
   5898 	  bfd_size_type strsize;
   5899 
   5900 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   5901 	  if ((info->emit_hash
   5902 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   5903 	      || (info->emit_gnu_hash
   5904 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
   5905 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   5906 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   5907 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   5908 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   5909 					      bed->s->sizeof_sym))
   5910 	    return FALSE;
   5911 	}
   5912     }
   5913 
   5914   /* The backend must work out the sizes of all the other dynamic
   5915      sections.  */
   5916   if (dynobj != NULL
   5917       && bed->elf_backend_size_dynamic_sections != NULL
   5918       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
   5919     return FALSE;
   5920 
   5921   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   5922     return FALSE;
   5923 
   5924   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   5925     {
   5926       unsigned long section_sym_count;
   5927       struct bfd_elf_version_tree *verdefs;
   5928       asection *s;
   5929 
   5930       /* Set up the version definition section.  */
   5931       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   5932       BFD_ASSERT (s != NULL);
   5933 
   5934       /* We may have created additional version definitions if we are
   5935 	 just linking a regular application.  */
   5936       verdefs = info->version_info;
   5937 
   5938       /* Skip anonymous version tag.  */
   5939       if (verdefs != NULL && verdefs->vernum == 0)
   5940 	verdefs = verdefs->next;
   5941 
   5942       if (verdefs == NULL && !info->create_default_symver)
   5943 	s->flags |= SEC_EXCLUDE;
   5944       else
   5945 	{
   5946 	  unsigned int cdefs;
   5947 	  bfd_size_type size;
   5948 	  struct bfd_elf_version_tree *t;
   5949 	  bfd_byte *p;
   5950 	  Elf_Internal_Verdef def;
   5951 	  Elf_Internal_Verdaux defaux;
   5952 	  struct bfd_link_hash_entry *bh;
   5953 	  struct elf_link_hash_entry *h;
   5954 	  const char *name;
   5955 
   5956 	  cdefs = 0;
   5957 	  size = 0;
   5958 
   5959 	  /* Make space for the base version.  */
   5960 	  size += sizeof (Elf_External_Verdef);
   5961 	  size += sizeof (Elf_External_Verdaux);
   5962 	  ++cdefs;
   5963 
   5964 	  /* Make space for the default version.  */
   5965 	  if (info->create_default_symver)
   5966 	    {
   5967 	      size += sizeof (Elf_External_Verdef);
   5968 	      ++cdefs;
   5969 	    }
   5970 
   5971 	  for (t = verdefs; t != NULL; t = t->next)
   5972 	    {
   5973 	      struct bfd_elf_version_deps *n;
   5974 
   5975 	      /* Don't emit base version twice.  */
   5976 	      if (t->vernum == 0)
   5977 		continue;
   5978 
   5979 	      size += sizeof (Elf_External_Verdef);
   5980 	      size += sizeof (Elf_External_Verdaux);
   5981 	      ++cdefs;
   5982 
   5983 	      for (n = t->deps; n != NULL; n = n->next)
   5984 		size += sizeof (Elf_External_Verdaux);
   5985 	    }
   5986 
   5987 	  s->size = size;
   5988 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   5989 	  if (s->contents == NULL && s->size != 0)
   5990 	    return FALSE;
   5991 
   5992 	  /* Fill in the version definition section.  */
   5993 
   5994 	  p = s->contents;
   5995 
   5996 	  def.vd_version = VER_DEF_CURRENT;
   5997 	  def.vd_flags = VER_FLG_BASE;
   5998 	  def.vd_ndx = 1;
   5999 	  def.vd_cnt = 1;
   6000 	  if (info->create_default_symver)
   6001 	    {
   6002 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   6003 	      def.vd_next = sizeof (Elf_External_Verdef);
   6004 	    }
   6005 	  else
   6006 	    {
   6007 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6008 	      def.vd_next = (sizeof (Elf_External_Verdef)
   6009 			     + sizeof (Elf_External_Verdaux));
   6010 	    }
   6011 
   6012 	  if (soname_indx != (bfd_size_type) -1)
   6013 	    {
   6014 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6015 				      soname_indx);
   6016 	      def.vd_hash = bfd_elf_hash (soname);
   6017 	      defaux.vda_name = soname_indx;
   6018 	      name = soname;
   6019 	    }
   6020 	  else
   6021 	    {
   6022 	      bfd_size_type indx;
   6023 
   6024 	      name = lbasename (output_bfd->filename);
   6025 	      def.vd_hash = bfd_elf_hash (name);
   6026 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6027 					  name, FALSE);
   6028 	      if (indx == (bfd_size_type) -1)
   6029 		return FALSE;
   6030 	      defaux.vda_name = indx;
   6031 	    }
   6032 	  defaux.vda_next = 0;
   6033 
   6034 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   6035 				    (Elf_External_Verdef *) p);
   6036 	  p += sizeof (Elf_External_Verdef);
   6037 	  if (info->create_default_symver)
   6038 	    {
   6039 	      /* Add a symbol representing this version.  */
   6040 	      bh = NULL;
   6041 	      if (! (_bfd_generic_link_add_one_symbol
   6042 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   6043 		      0, NULL, FALSE,
   6044 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6045 		return FALSE;
   6046 	      h = (struct elf_link_hash_entry *) bh;
   6047 	      h->non_elf = 0;
   6048 	      h->def_regular = 1;
   6049 	      h->type = STT_OBJECT;
   6050 	      h->verinfo.vertree = NULL;
   6051 
   6052 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6053 		return FALSE;
   6054 
   6055 	      /* Create a duplicate of the base version with the same
   6056 		 aux block, but different flags.  */
   6057 	      def.vd_flags = 0;
   6058 	      def.vd_ndx = 2;
   6059 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6060 	      if (verdefs)
   6061 		def.vd_next = (sizeof (Elf_External_Verdef)
   6062 			       + sizeof (Elf_External_Verdaux));
   6063 	      else
   6064 		def.vd_next = 0;
   6065 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6066 					(Elf_External_Verdef *) p);
   6067 	      p += sizeof (Elf_External_Verdef);
   6068 	    }
   6069 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6070 				     (Elf_External_Verdaux *) p);
   6071 	  p += sizeof (Elf_External_Verdaux);
   6072 
   6073 	  for (t = verdefs; t != NULL; t = t->next)
   6074 	    {
   6075 	      unsigned int cdeps;
   6076 	      struct bfd_elf_version_deps *n;
   6077 
   6078 	      /* Don't emit the base version twice.  */
   6079 	      if (t->vernum == 0)
   6080 		continue;
   6081 
   6082 	      cdeps = 0;
   6083 	      for (n = t->deps; n != NULL; n = n->next)
   6084 		++cdeps;
   6085 
   6086 	      /* Add a symbol representing this version.  */
   6087 	      bh = NULL;
   6088 	      if (! (_bfd_generic_link_add_one_symbol
   6089 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   6090 		      0, NULL, FALSE,
   6091 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6092 		return FALSE;
   6093 	      h = (struct elf_link_hash_entry *) bh;
   6094 	      h->non_elf = 0;
   6095 	      h->def_regular = 1;
   6096 	      h->type = STT_OBJECT;
   6097 	      h->verinfo.vertree = t;
   6098 
   6099 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6100 		return FALSE;
   6101 
   6102 	      def.vd_version = VER_DEF_CURRENT;
   6103 	      def.vd_flags = 0;
   6104 	      if (t->globals.list == NULL
   6105 		  && t->locals.list == NULL
   6106 		  && ! t->used)
   6107 		def.vd_flags |= VER_FLG_WEAK;
   6108 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   6109 	      def.vd_cnt = cdeps + 1;
   6110 	      def.vd_hash = bfd_elf_hash (t->name);
   6111 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6112 	      def.vd_next = 0;
   6113 
   6114 	      /* If a basever node is next, it *must* be the last node in
   6115 		 the chain, otherwise Verdef construction breaks.  */
   6116 	      if (t->next != NULL && t->next->vernum == 0)
   6117 		BFD_ASSERT (t->next->next == NULL);
   6118 
   6119 	      if (t->next != NULL && t->next->vernum != 0)
   6120 		def.vd_next = (sizeof (Elf_External_Verdef)
   6121 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   6122 
   6123 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6124 					(Elf_External_Verdef *) p);
   6125 	      p += sizeof (Elf_External_Verdef);
   6126 
   6127 	      defaux.vda_name = h->dynstr_index;
   6128 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6129 				      h->dynstr_index);
   6130 	      defaux.vda_next = 0;
   6131 	      if (t->deps != NULL)
   6132 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   6133 	      t->name_indx = defaux.vda_name;
   6134 
   6135 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6136 					 (Elf_External_Verdaux *) p);
   6137 	      p += sizeof (Elf_External_Verdaux);
   6138 
   6139 	      for (n = t->deps; n != NULL; n = n->next)
   6140 		{
   6141 		  if (n->version_needed == NULL)
   6142 		    {
   6143 		      /* This can happen if there was an error in the
   6144 			 version script.  */
   6145 		      defaux.vda_name = 0;
   6146 		    }
   6147 		  else
   6148 		    {
   6149 		      defaux.vda_name = n->version_needed->name_indx;
   6150 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6151 					      defaux.vda_name);
   6152 		    }
   6153 		  if (n->next == NULL)
   6154 		    defaux.vda_next = 0;
   6155 		  else
   6156 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   6157 
   6158 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6159 					     (Elf_External_Verdaux *) p);
   6160 		  p += sizeof (Elf_External_Verdaux);
   6161 		}
   6162 	    }
   6163 
   6164 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   6165 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
   6166 	    return FALSE;
   6167 
   6168 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   6169 	}
   6170 
   6171       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   6172 	{
   6173 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   6174 	    return FALSE;
   6175 	}
   6176       else if (info->flags & DF_BIND_NOW)
   6177 	{
   6178 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   6179 	    return FALSE;
   6180 	}
   6181 
   6182       if (info->flags_1)
   6183 	{
   6184 	  if (info->executable)
   6185 	    info->flags_1 &= ~ (DF_1_INITFIRST
   6186 				| DF_1_NODELETE
   6187 				| DF_1_NOOPEN);
   6188 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   6189 	    return FALSE;
   6190 	}
   6191 
   6192       /* Work out the size of the version reference section.  */
   6193 
   6194       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   6195       BFD_ASSERT (s != NULL);
   6196       {
   6197 	struct elf_find_verdep_info sinfo;
   6198 
   6199 	sinfo.info = info;
   6200 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   6201 	if (sinfo.vers == 0)
   6202 	  sinfo.vers = 1;
   6203 	sinfo.failed = FALSE;
   6204 
   6205 	elf_link_hash_traverse (elf_hash_table (info),
   6206 				_bfd_elf_link_find_version_dependencies,
   6207 				&sinfo);
   6208 	if (sinfo.failed)
   6209 	  return FALSE;
   6210 
   6211 	if (elf_tdata (output_bfd)->verref == NULL)
   6212 	  s->flags |= SEC_EXCLUDE;
   6213 	else
   6214 	  {
   6215 	    Elf_Internal_Verneed *t;
   6216 	    unsigned int size;
   6217 	    unsigned int crefs;
   6218 	    bfd_byte *p;
   6219 
   6220 	    /* Build the version dependency section.  */
   6221 	    size = 0;
   6222 	    crefs = 0;
   6223 	    for (t = elf_tdata (output_bfd)->verref;
   6224 		 t != NULL;
   6225 		 t = t->vn_nextref)
   6226 	      {
   6227 		Elf_Internal_Vernaux *a;
   6228 
   6229 		size += sizeof (Elf_External_Verneed);
   6230 		++crefs;
   6231 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6232 		  size += sizeof (Elf_External_Vernaux);
   6233 	      }
   6234 
   6235 	    s->size = size;
   6236 	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6237 	    if (s->contents == NULL)
   6238 	      return FALSE;
   6239 
   6240 	    p = s->contents;
   6241 	    for (t = elf_tdata (output_bfd)->verref;
   6242 		 t != NULL;
   6243 		 t = t->vn_nextref)
   6244 	      {
   6245 		unsigned int caux;
   6246 		Elf_Internal_Vernaux *a;
   6247 		bfd_size_type indx;
   6248 
   6249 		caux = 0;
   6250 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6251 		  ++caux;
   6252 
   6253 		t->vn_version = VER_NEED_CURRENT;
   6254 		t->vn_cnt = caux;
   6255 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6256 					    elf_dt_name (t->vn_bfd) != NULL
   6257 					    ? elf_dt_name (t->vn_bfd)
   6258 					    : lbasename (t->vn_bfd->filename),
   6259 					    FALSE);
   6260 		if (indx == (bfd_size_type) -1)
   6261 		  return FALSE;
   6262 		t->vn_file = indx;
   6263 		t->vn_aux = sizeof (Elf_External_Verneed);
   6264 		if (t->vn_nextref == NULL)
   6265 		  t->vn_next = 0;
   6266 		else
   6267 		  t->vn_next = (sizeof (Elf_External_Verneed)
   6268 				+ caux * sizeof (Elf_External_Vernaux));
   6269 
   6270 		_bfd_elf_swap_verneed_out (output_bfd, t,
   6271 					   (Elf_External_Verneed *) p);
   6272 		p += sizeof (Elf_External_Verneed);
   6273 
   6274 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6275 		  {
   6276 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
   6277 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6278 						a->vna_nodename, FALSE);
   6279 		    if (indx == (bfd_size_type) -1)
   6280 		      return FALSE;
   6281 		    a->vna_name = indx;
   6282 		    if (a->vna_nextptr == NULL)
   6283 		      a->vna_next = 0;
   6284 		    else
   6285 		      a->vna_next = sizeof (Elf_External_Vernaux);
   6286 
   6287 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
   6288 					       (Elf_External_Vernaux *) p);
   6289 		    p += sizeof (Elf_External_Vernaux);
   6290 		  }
   6291 	      }
   6292 
   6293 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   6294 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   6295 	      return FALSE;
   6296 
   6297 	    elf_tdata (output_bfd)->cverrefs = crefs;
   6298 	  }
   6299       }
   6300 
   6301       if ((elf_tdata (output_bfd)->cverrefs == 0
   6302 	   && elf_tdata (output_bfd)->cverdefs == 0)
   6303 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   6304 					     &section_sym_count) == 0)
   6305 	{
   6306 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   6307 	  s->flags |= SEC_EXCLUDE;
   6308 	}
   6309     }
   6310   return TRUE;
   6311 }
   6312 
   6313 /* Find the first non-excluded output section.  We'll use its
   6314    section symbol for some emitted relocs.  */
   6315 void
   6316 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   6317 {
   6318   asection *s;
   6319 
   6320   for (s = output_bfd->sections; s != NULL; s = s->next)
   6321     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   6322 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6323       {
   6324 	elf_hash_table (info)->text_index_section = s;
   6325 	break;
   6326       }
   6327 }
   6328 
   6329 /* Find two non-excluded output sections, one for code, one for data.
   6330    We'll use their section symbols for some emitted relocs.  */
   6331 void
   6332 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   6333 {
   6334   asection *s;
   6335 
   6336   /* Data first, since setting text_index_section changes
   6337      _bfd_elf_link_omit_section_dynsym.  */
   6338   for (s = output_bfd->sections; s != NULL; s = s->next)
   6339     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
   6340 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6341       {
   6342 	elf_hash_table (info)->data_index_section = s;
   6343 	break;
   6344       }
   6345 
   6346   for (s = output_bfd->sections; s != NULL; s = s->next)
   6347     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
   6348 	 == (SEC_ALLOC | SEC_READONLY))
   6349 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6350       {
   6351 	elf_hash_table (info)->text_index_section = s;
   6352 	break;
   6353       }
   6354 
   6355   if (elf_hash_table (info)->text_index_section == NULL)
   6356     elf_hash_table (info)->text_index_section
   6357       = elf_hash_table (info)->data_index_section;
   6358 }
   6359 
   6360 bfd_boolean
   6361 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   6362 {
   6363   const struct elf_backend_data *bed;
   6364 
   6365   if (!is_elf_hash_table (info->hash))
   6366     return TRUE;
   6367 
   6368   bed = get_elf_backend_data (output_bfd);
   6369   (*bed->elf_backend_init_index_section) (output_bfd, info);
   6370 
   6371   if (elf_hash_table (info)->dynamic_sections_created)
   6372     {
   6373       bfd *dynobj;
   6374       asection *s;
   6375       bfd_size_type dynsymcount;
   6376       unsigned long section_sym_count;
   6377       unsigned int dtagcount;
   6378 
   6379       dynobj = elf_hash_table (info)->dynobj;
   6380 
   6381       /* Assign dynsym indicies.  In a shared library we generate a
   6382 	 section symbol for each output section, which come first.
   6383 	 Next come all of the back-end allocated local dynamic syms,
   6384 	 followed by the rest of the global symbols.  */
   6385 
   6386       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   6387 						    &section_sym_count);
   6388 
   6389       /* Work out the size of the symbol version section.  */
   6390       s = bfd_get_linker_section (dynobj, ".gnu.version");
   6391       BFD_ASSERT (s != NULL);
   6392       if (dynsymcount != 0
   6393 	  && (s->flags & SEC_EXCLUDE) == 0)
   6394 	{
   6395 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   6396 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6397 	  if (s->contents == NULL)
   6398 	    return FALSE;
   6399 
   6400 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   6401 	    return FALSE;
   6402 	}
   6403 
   6404       /* Set the size of the .dynsym and .hash sections.  We counted
   6405 	 the number of dynamic symbols in elf_link_add_object_symbols.
   6406 	 We will build the contents of .dynsym and .hash when we build
   6407 	 the final symbol table, because until then we do not know the
   6408 	 correct value to give the symbols.  We built the .dynstr
   6409 	 section as we went along in elf_link_add_object_symbols.  */
   6410       s = bfd_get_linker_section (dynobj, ".dynsym");
   6411       BFD_ASSERT (s != NULL);
   6412       s->size = dynsymcount * bed->s->sizeof_sym;
   6413 
   6414       if (dynsymcount != 0)
   6415 	{
   6416 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6417 	  if (s->contents == NULL)
   6418 	    return FALSE;
   6419 
   6420 	  /* The first entry in .dynsym is a dummy symbol.
   6421 	     Clear all the section syms, in case we don't output them all.  */
   6422 	  ++section_sym_count;
   6423 	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   6424 	}
   6425 
   6426       elf_hash_table (info)->bucketcount = 0;
   6427 
   6428       /* Compute the size of the hashing table.  As a side effect this
   6429 	 computes the hash values for all the names we export.  */
   6430       if (info->emit_hash)
   6431 	{
   6432 	  unsigned long int *hashcodes;
   6433 	  struct hash_codes_info hashinf;
   6434 	  bfd_size_type amt;
   6435 	  unsigned long int nsyms;
   6436 	  size_t bucketcount;
   6437 	  size_t hash_entry_size;
   6438 
   6439 	  /* Compute the hash values for all exported symbols.  At the same
   6440 	     time store the values in an array so that we could use them for
   6441 	     optimizations.  */
   6442 	  amt = dynsymcount * sizeof (unsigned long int);
   6443 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   6444 	  if (hashcodes == NULL)
   6445 	    return FALSE;
   6446 	  hashinf.hashcodes = hashcodes;
   6447 	  hashinf.error = FALSE;
   6448 
   6449 	  /* Put all hash values in HASHCODES.  */
   6450 	  elf_link_hash_traverse (elf_hash_table (info),
   6451 				  elf_collect_hash_codes, &hashinf);
   6452 	  if (hashinf.error)
   6453 	    {
   6454 	      free (hashcodes);
   6455 	      return FALSE;
   6456 	    }
   6457 
   6458 	  nsyms = hashinf.hashcodes - hashcodes;
   6459 	  bucketcount
   6460 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   6461 	  free (hashcodes);
   6462 
   6463 	  if (bucketcount == 0)
   6464 	    return FALSE;
   6465 
   6466 	  elf_hash_table (info)->bucketcount = bucketcount;
   6467 
   6468 	  s = bfd_get_linker_section (dynobj, ".hash");
   6469 	  BFD_ASSERT (s != NULL);
   6470 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   6471 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   6472 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6473 	  if (s->contents == NULL)
   6474 	    return FALSE;
   6475 
   6476 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   6477 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   6478 		   s->contents + hash_entry_size);
   6479 	}
   6480 
   6481       if (info->emit_gnu_hash)
   6482 	{
   6483 	  size_t i, cnt;
   6484 	  unsigned char *contents;
   6485 	  struct collect_gnu_hash_codes cinfo;
   6486 	  bfd_size_type amt;
   6487 	  size_t bucketcount;
   6488 
   6489 	  memset (&cinfo, 0, sizeof (cinfo));
   6490 
   6491 	  /* Compute the hash values for all exported symbols.  At the same
   6492 	     time store the values in an array so that we could use them for
   6493 	     optimizations.  */
   6494 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   6495 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   6496 	  if (cinfo.hashcodes == NULL)
   6497 	    return FALSE;
   6498 
   6499 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   6500 	  cinfo.min_dynindx = -1;
   6501 	  cinfo.output_bfd = output_bfd;
   6502 	  cinfo.bed = bed;
   6503 
   6504 	  /* Put all hash values in HASHCODES.  */
   6505 	  elf_link_hash_traverse (elf_hash_table (info),
   6506 				  elf_collect_gnu_hash_codes, &cinfo);
   6507 	  if (cinfo.error)
   6508 	    {
   6509 	      free (cinfo.hashcodes);
   6510 	      return FALSE;
   6511 	    }
   6512 
   6513 	  bucketcount
   6514 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   6515 
   6516 	  if (bucketcount == 0)
   6517 	    {
   6518 	      free (cinfo.hashcodes);
   6519 	      return FALSE;
   6520 	    }
   6521 
   6522 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
   6523 	  BFD_ASSERT (s != NULL);
   6524 
   6525 	  if (cinfo.nsyms == 0)
   6526 	    {
   6527 	      /* Empty .gnu.hash section is special.  */
   6528 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   6529 	      free (cinfo.hashcodes);
   6530 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   6531 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6532 	      if (contents == NULL)
   6533 		return FALSE;
   6534 	      s->contents = contents;
   6535 	      /* 1 empty bucket.  */
   6536 	      bfd_put_32 (output_bfd, 1, contents);
   6537 	      /* SYMIDX above the special symbol 0.  */
   6538 	      bfd_put_32 (output_bfd, 1, contents + 4);
   6539 	      /* Just one word for bitmask.  */
   6540 	      bfd_put_32 (output_bfd, 1, contents + 8);
   6541 	      /* Only hash fn bloom filter.  */
   6542 	      bfd_put_32 (output_bfd, 0, contents + 12);
   6543 	      /* No hashes are valid - empty bitmask.  */
   6544 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   6545 	      /* No hashes in the only bucket.  */
   6546 	      bfd_put_32 (output_bfd, 0,
   6547 			  contents + 16 + bed->s->arch_size / 8);
   6548 	    }
   6549 	  else
   6550 	    {
   6551 	      unsigned long int maskwords, maskbitslog2, x;
   6552 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   6553 
   6554 	      x = cinfo.nsyms;
   6555 	      maskbitslog2 = 1;
   6556 	      while ((x >>= 1) != 0)
   6557 		++maskbitslog2;
   6558 	      if (maskbitslog2 < 3)
   6559 		maskbitslog2 = 5;
   6560 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   6561 		maskbitslog2 = maskbitslog2 + 3;
   6562 	      else
   6563 		maskbitslog2 = maskbitslog2 + 2;
   6564 	      if (bed->s->arch_size == 64)
   6565 		{
   6566 		  if (maskbitslog2 == 5)
   6567 		    maskbitslog2 = 6;
   6568 		  cinfo.shift1 = 6;
   6569 		}
   6570 	      else
   6571 		cinfo.shift1 = 5;
   6572 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   6573 	      cinfo.shift2 = maskbitslog2;
   6574 	      cinfo.maskbits = 1 << maskbitslog2;
   6575 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   6576 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   6577 	      amt += maskwords * sizeof (bfd_vma);
   6578 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   6579 	      if (cinfo.bitmask == NULL)
   6580 		{
   6581 		  free (cinfo.hashcodes);
   6582 		  return FALSE;
   6583 		}
   6584 
   6585 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   6586 	      cinfo.indx = cinfo.counts + bucketcount;
   6587 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   6588 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   6589 
   6590 	      /* Determine how often each hash bucket is used.  */
   6591 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   6592 	      for (i = 0; i < cinfo.nsyms; ++i)
   6593 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   6594 
   6595 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   6596 		if (cinfo.counts[i] != 0)
   6597 		  {
   6598 		    cinfo.indx[i] = cnt;
   6599 		    cnt += cinfo.counts[i];
   6600 		  }
   6601 	      BFD_ASSERT (cnt == dynsymcount);
   6602 	      cinfo.bucketcount = bucketcount;
   6603 	      cinfo.local_indx = cinfo.min_dynindx;
   6604 
   6605 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   6606 	      s->size += cinfo.maskbits / 8;
   6607 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6608 	      if (contents == NULL)
   6609 		{
   6610 		  free (cinfo.bitmask);
   6611 		  free (cinfo.hashcodes);
   6612 		  return FALSE;
   6613 		}
   6614 
   6615 	      s->contents = contents;
   6616 	      bfd_put_32 (output_bfd, bucketcount, contents);
   6617 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   6618 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   6619 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   6620 	      contents += 16 + cinfo.maskbits / 8;
   6621 
   6622 	      for (i = 0; i < bucketcount; ++i)
   6623 		{
   6624 		  if (cinfo.counts[i] == 0)
   6625 		    bfd_put_32 (output_bfd, 0, contents);
   6626 		  else
   6627 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   6628 		  contents += 4;
   6629 		}
   6630 
   6631 	      cinfo.contents = contents;
   6632 
   6633 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
   6634 	      elf_link_hash_traverse (elf_hash_table (info),
   6635 				      elf_renumber_gnu_hash_syms, &cinfo);
   6636 
   6637 	      contents = s->contents + 16;
   6638 	      for (i = 0; i < maskwords; ++i)
   6639 		{
   6640 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   6641 			   contents);
   6642 		  contents += bed->s->arch_size / 8;
   6643 		}
   6644 
   6645 	      free (cinfo.bitmask);
   6646 	      free (cinfo.hashcodes);
   6647 	    }
   6648 	}
   6649 
   6650       s = bfd_get_linker_section (dynobj, ".dynstr");
   6651       BFD_ASSERT (s != NULL);
   6652 
   6653       elf_finalize_dynstr (output_bfd, info);
   6654 
   6655       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   6656 
   6657       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   6658 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   6659 	  return FALSE;
   6660     }
   6661 
   6662   return TRUE;
   6663 }
   6664 
   6665 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   6667 
   6668 static void
   6669 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   6670 			    asection *sec)
   6671 {
   6672   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   6673   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   6674 }
   6675 
   6676 /* Finish SHF_MERGE section merging.  */
   6677 
   6678 bfd_boolean
   6679 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
   6680 {
   6681   bfd *ibfd;
   6682   asection *sec;
   6683 
   6684   if (!is_elf_hash_table (info->hash))
   6685     return FALSE;
   6686 
   6687   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6688     if ((ibfd->flags & DYNAMIC) == 0)
   6689       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   6690 	if ((sec->flags & SEC_MERGE) != 0
   6691 	    && !bfd_is_abs_section (sec->output_section))
   6692 	  {
   6693 	    struct bfd_elf_section_data *secdata;
   6694 
   6695 	    secdata = elf_section_data (sec);
   6696 	    if (! _bfd_add_merge_section (abfd,
   6697 					  &elf_hash_table (info)->merge_info,
   6698 					  sec, &secdata->sec_info))
   6699 	      return FALSE;
   6700 	    else if (secdata->sec_info)
   6701 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   6702 	  }
   6703 
   6704   if (elf_hash_table (info)->merge_info != NULL)
   6705     _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
   6706 			 merge_sections_remove_hook);
   6707   return TRUE;
   6708 }
   6709 
   6710 /* Create an entry in an ELF linker hash table.  */
   6711 
   6712 struct bfd_hash_entry *
   6713 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   6714 			    struct bfd_hash_table *table,
   6715 			    const char *string)
   6716 {
   6717   /* Allocate the structure if it has not already been allocated by a
   6718      subclass.  */
   6719   if (entry == NULL)
   6720     {
   6721       entry = (struct bfd_hash_entry *)
   6722           bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   6723       if (entry == NULL)
   6724 	return entry;
   6725     }
   6726 
   6727   /* Call the allocation method of the superclass.  */
   6728   entry = _bfd_link_hash_newfunc (entry, table, string);
   6729   if (entry != NULL)
   6730     {
   6731       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   6732       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   6733 
   6734       /* Set local fields.  */
   6735       ret->indx = -1;
   6736       ret->dynindx = -1;
   6737       ret->got = htab->init_got_refcount;
   6738       ret->plt = htab->init_plt_refcount;
   6739       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   6740 			      - offsetof (struct elf_link_hash_entry, size)));
   6741       /* Assume that we have been called by a non-ELF symbol reader.
   6742 	 This flag is then reset by the code which reads an ELF input
   6743 	 file.  This ensures that a symbol created by a non-ELF symbol
   6744 	 reader will have the flag set correctly.  */
   6745       ret->non_elf = 1;
   6746     }
   6747 
   6748   return entry;
   6749 }
   6750 
   6751 /* Copy data from an indirect symbol to its direct symbol, hiding the
   6752    old indirect symbol.  Also used for copying flags to a weakdef.  */
   6753 
   6754 void
   6755 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   6756 				  struct elf_link_hash_entry *dir,
   6757 				  struct elf_link_hash_entry *ind)
   6758 {
   6759   struct elf_link_hash_table *htab;
   6760 
   6761   /* Copy down any references that we may have already seen to the
   6762      symbol which just became indirect.  */
   6763 
   6764   dir->ref_dynamic |= ind->ref_dynamic;
   6765   dir->ref_regular |= ind->ref_regular;
   6766   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   6767   dir->non_got_ref |= ind->non_got_ref;
   6768   dir->needs_plt |= ind->needs_plt;
   6769   dir->pointer_equality_needed |= ind->pointer_equality_needed;
   6770 
   6771   if (ind->root.type != bfd_link_hash_indirect)
   6772     return;
   6773 
   6774   /* Copy over the global and procedure linkage table refcount entries.
   6775      These may have been already set up by a check_relocs routine.  */
   6776   htab = elf_hash_table (info);
   6777   if (ind->got.refcount > htab->init_got_refcount.refcount)
   6778     {
   6779       if (dir->got.refcount < 0)
   6780 	dir->got.refcount = 0;
   6781       dir->got.refcount += ind->got.refcount;
   6782       ind->got.refcount = htab->init_got_refcount.refcount;
   6783     }
   6784 
   6785   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   6786     {
   6787       if (dir->plt.refcount < 0)
   6788 	dir->plt.refcount = 0;
   6789       dir->plt.refcount += ind->plt.refcount;
   6790       ind->plt.refcount = htab->init_plt_refcount.refcount;
   6791     }
   6792 
   6793   if (ind->dynindx != -1)
   6794     {
   6795       if (dir->dynindx != -1)
   6796 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   6797       dir->dynindx = ind->dynindx;
   6798       dir->dynstr_index = ind->dynstr_index;
   6799       ind->dynindx = -1;
   6800       ind->dynstr_index = 0;
   6801     }
   6802 }
   6803 
   6804 void
   6805 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   6806 				struct elf_link_hash_entry *h,
   6807 				bfd_boolean force_local)
   6808 {
   6809   /* STT_GNU_IFUNC symbol must go through PLT.  */
   6810   if (h->type != STT_GNU_IFUNC)
   6811     {
   6812       h->plt = elf_hash_table (info)->init_plt_offset;
   6813       h->needs_plt = 0;
   6814     }
   6815   if (force_local)
   6816     {
   6817       h->forced_local = 1;
   6818       if (h->dynindx != -1)
   6819 	{
   6820 	  h->dynindx = -1;
   6821 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   6822 				  h->dynstr_index);
   6823 	}
   6824     }
   6825 }
   6826 
   6827 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   6828    caller.  */
   6829 
   6830 bfd_boolean
   6831 _bfd_elf_link_hash_table_init
   6832   (struct elf_link_hash_table *table,
   6833    bfd *abfd,
   6834    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   6835 				      struct bfd_hash_table *,
   6836 				      const char *),
   6837    unsigned int entsize,
   6838    enum elf_target_id target_id)
   6839 {
   6840   bfd_boolean ret;
   6841   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
   6842 
   6843   table->init_got_refcount.refcount = can_refcount - 1;
   6844   table->init_plt_refcount.refcount = can_refcount - 1;
   6845   table->init_got_offset.offset = -(bfd_vma) 1;
   6846   table->init_plt_offset.offset = -(bfd_vma) 1;
   6847   /* The first dynamic symbol is a dummy.  */
   6848   table->dynsymcount = 1;
   6849 
   6850   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   6851 
   6852   table->root.type = bfd_link_elf_hash_table;
   6853   table->hash_table_id = target_id;
   6854 
   6855   return ret;
   6856 }
   6857 
   6858 /* Create an ELF linker hash table.  */
   6859 
   6860 struct bfd_link_hash_table *
   6861 _bfd_elf_link_hash_table_create (bfd *abfd)
   6862 {
   6863   struct elf_link_hash_table *ret;
   6864   bfd_size_type amt = sizeof (struct elf_link_hash_table);
   6865 
   6866   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   6867   if (ret == NULL)
   6868     return NULL;
   6869 
   6870   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   6871 				       sizeof (struct elf_link_hash_entry),
   6872 				       GENERIC_ELF_DATA))
   6873     {
   6874       free (ret);
   6875       return NULL;
   6876     }
   6877   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
   6878 
   6879   return &ret->root;
   6880 }
   6881 
   6882 /* Destroy an ELF linker hash table.  */
   6883 
   6884 void
   6885 _bfd_elf_link_hash_table_free (bfd *obfd)
   6886 {
   6887   struct elf_link_hash_table *htab;
   6888 
   6889   htab = (struct elf_link_hash_table *) obfd->link.hash;
   6890   if (htab->dynstr != NULL)
   6891     _bfd_elf_strtab_free (htab->dynstr);
   6892   _bfd_merge_sections_free (htab->merge_info);
   6893   _bfd_generic_link_hash_table_free (obfd);
   6894 }
   6895 
   6896 /* This is a hook for the ELF emulation code in the generic linker to
   6897    tell the backend linker what file name to use for the DT_NEEDED
   6898    entry for a dynamic object.  */
   6899 
   6900 void
   6901 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   6902 {
   6903   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   6904       && bfd_get_format (abfd) == bfd_object)
   6905     elf_dt_name (abfd) = name;
   6906 }
   6907 
   6908 int
   6909 bfd_elf_get_dyn_lib_class (bfd *abfd)
   6910 {
   6911   int lib_class;
   6912   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   6913       && bfd_get_format (abfd) == bfd_object)
   6914     lib_class = elf_dyn_lib_class (abfd);
   6915   else
   6916     lib_class = 0;
   6917   return lib_class;
   6918 }
   6919 
   6920 void
   6921 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   6922 {
   6923   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   6924       && bfd_get_format (abfd) == bfd_object)
   6925     elf_dyn_lib_class (abfd) = lib_class;
   6926 }
   6927 
   6928 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   6929    the linker ELF emulation code.  */
   6930 
   6931 struct bfd_link_needed_list *
   6932 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   6933 			 struct bfd_link_info *info)
   6934 {
   6935   if (! is_elf_hash_table (info->hash))
   6936     return NULL;
   6937   return elf_hash_table (info)->needed;
   6938 }
   6939 
   6940 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   6941    hook for the linker ELF emulation code.  */
   6942 
   6943 struct bfd_link_needed_list *
   6944 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   6945 			  struct bfd_link_info *info)
   6946 {
   6947   if (! is_elf_hash_table (info->hash))
   6948     return NULL;
   6949   return elf_hash_table (info)->runpath;
   6950 }
   6951 
   6952 /* Get the name actually used for a dynamic object for a link.  This
   6953    is the SONAME entry if there is one.  Otherwise, it is the string
   6954    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   6955 
   6956 const char *
   6957 bfd_elf_get_dt_soname (bfd *abfd)
   6958 {
   6959   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   6960       && bfd_get_format (abfd) == bfd_object)
   6961     return elf_dt_name (abfd);
   6962   return NULL;
   6963 }
   6964 
   6965 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   6966    the ELF linker emulation code.  */
   6967 
   6968 bfd_boolean
   6969 bfd_elf_get_bfd_needed_list (bfd *abfd,
   6970 			     struct bfd_link_needed_list **pneeded)
   6971 {
   6972   asection *s;
   6973   bfd_byte *dynbuf = NULL;
   6974   unsigned int elfsec;
   6975   unsigned long shlink;
   6976   bfd_byte *extdyn, *extdynend;
   6977   size_t extdynsize;
   6978   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   6979 
   6980   *pneeded = NULL;
   6981 
   6982   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   6983       || bfd_get_format (abfd) != bfd_object)
   6984     return TRUE;
   6985 
   6986   s = bfd_get_section_by_name (abfd, ".dynamic");
   6987   if (s == NULL || s->size == 0)
   6988     return TRUE;
   6989 
   6990   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   6991     goto error_return;
   6992 
   6993   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   6994   if (elfsec == SHN_BAD)
   6995     goto error_return;
   6996 
   6997   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   6998 
   6999   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   7000   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   7001 
   7002   extdyn = dynbuf;
   7003   extdynend = extdyn + s->size;
   7004   for (; extdyn < extdynend; extdyn += extdynsize)
   7005     {
   7006       Elf_Internal_Dyn dyn;
   7007 
   7008       (*swap_dyn_in) (abfd, extdyn, &dyn);
   7009 
   7010       if (dyn.d_tag == DT_NULL)
   7011 	break;
   7012 
   7013       if (dyn.d_tag == DT_NEEDED)
   7014 	{
   7015 	  const char *string;
   7016 	  struct bfd_link_needed_list *l;
   7017 	  unsigned int tagv = dyn.d_un.d_val;
   7018 	  bfd_size_type amt;
   7019 
   7020 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   7021 	  if (string == NULL)
   7022 	    goto error_return;
   7023 
   7024 	  amt = sizeof *l;
   7025 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   7026 	  if (l == NULL)
   7027 	    goto error_return;
   7028 
   7029 	  l->by = abfd;
   7030 	  l->name = string;
   7031 	  l->next = *pneeded;
   7032 	  *pneeded = l;
   7033 	}
   7034     }
   7035 
   7036   free (dynbuf);
   7037 
   7038   return TRUE;
   7039 
   7040  error_return:
   7041   if (dynbuf != NULL)
   7042     free (dynbuf);
   7043   return FALSE;
   7044 }
   7045 
   7046 struct elf_symbuf_symbol
   7047 {
   7048   unsigned long st_name;	/* Symbol name, index in string tbl */
   7049   unsigned char st_info;	/* Type and binding attributes */
   7050   unsigned char st_other;	/* Visibilty, and target specific */
   7051 };
   7052 
   7053 struct elf_symbuf_head
   7054 {
   7055   struct elf_symbuf_symbol *ssym;
   7056   bfd_size_type count;
   7057   unsigned int st_shndx;
   7058 };
   7059 
   7060 struct elf_symbol
   7061 {
   7062   union
   7063     {
   7064       Elf_Internal_Sym *isym;
   7065       struct elf_symbuf_symbol *ssym;
   7066     } u;
   7067   const char *name;
   7068 };
   7069 
   7070 /* Sort references to symbols by ascending section number.  */
   7071 
   7072 static int
   7073 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   7074 {
   7075   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   7076   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   7077 
   7078   return s1->st_shndx - s2->st_shndx;
   7079 }
   7080 
   7081 static int
   7082 elf_sym_name_compare (const void *arg1, const void *arg2)
   7083 {
   7084   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   7085   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   7086   return strcmp (s1->name, s2->name);
   7087 }
   7088 
   7089 static struct elf_symbuf_head *
   7090 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
   7091 {
   7092   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   7093   struct elf_symbuf_symbol *ssym;
   7094   struct elf_symbuf_head *ssymbuf, *ssymhead;
   7095   bfd_size_type i, shndx_count, total_size;
   7096 
   7097   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
   7098   if (indbuf == NULL)
   7099     return NULL;
   7100 
   7101   for (ind = indbuf, i = 0; i < symcount; i++)
   7102     if (isymbuf[i].st_shndx != SHN_UNDEF)
   7103       *ind++ = &isymbuf[i];
   7104   indbufend = ind;
   7105 
   7106   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   7107 	 elf_sort_elf_symbol);
   7108 
   7109   shndx_count = 0;
   7110   if (indbufend > indbuf)
   7111     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   7112       if (ind[0]->st_shndx != ind[1]->st_shndx)
   7113 	shndx_count++;
   7114 
   7115   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   7116 		+ (indbufend - indbuf) * sizeof (*ssym));
   7117   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   7118   if (ssymbuf == NULL)
   7119     {
   7120       free (indbuf);
   7121       return NULL;
   7122     }
   7123 
   7124   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   7125   ssymbuf->ssym = NULL;
   7126   ssymbuf->count = shndx_count;
   7127   ssymbuf->st_shndx = 0;
   7128   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   7129     {
   7130       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   7131 	{
   7132 	  ssymhead++;
   7133 	  ssymhead->ssym = ssym;
   7134 	  ssymhead->count = 0;
   7135 	  ssymhead->st_shndx = (*ind)->st_shndx;
   7136 	}
   7137       ssym->st_name = (*ind)->st_name;
   7138       ssym->st_info = (*ind)->st_info;
   7139       ssym->st_other = (*ind)->st_other;
   7140       ssymhead->count++;
   7141     }
   7142   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
   7143 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
   7144 		  == total_size));
   7145 
   7146   free (indbuf);
   7147   return ssymbuf;
   7148 }
   7149 
   7150 /* Check if 2 sections define the same set of local and global
   7151    symbols.  */
   7152 
   7153 static bfd_boolean
   7154 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   7155 				   struct bfd_link_info *info)
   7156 {
   7157   bfd *bfd1, *bfd2;
   7158   const struct elf_backend_data *bed1, *bed2;
   7159   Elf_Internal_Shdr *hdr1, *hdr2;
   7160   bfd_size_type symcount1, symcount2;
   7161   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   7162   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   7163   Elf_Internal_Sym *isym, *isymend;
   7164   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   7165   bfd_size_type count1, count2, i;
   7166   unsigned int shndx1, shndx2;
   7167   bfd_boolean result;
   7168 
   7169   bfd1 = sec1->owner;
   7170   bfd2 = sec2->owner;
   7171 
   7172   /* Both sections have to be in ELF.  */
   7173   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   7174       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   7175     return FALSE;
   7176 
   7177   if (elf_section_type (sec1) != elf_section_type (sec2))
   7178     return FALSE;
   7179 
   7180   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   7181   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   7182   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   7183     return FALSE;
   7184 
   7185   bed1 = get_elf_backend_data (bfd1);
   7186   bed2 = get_elf_backend_data (bfd2);
   7187   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   7188   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   7189   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   7190   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   7191 
   7192   if (symcount1 == 0 || symcount2 == 0)
   7193     return FALSE;
   7194 
   7195   result = FALSE;
   7196   isymbuf1 = NULL;
   7197   isymbuf2 = NULL;
   7198   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   7199   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   7200 
   7201   if (ssymbuf1 == NULL)
   7202     {
   7203       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   7204 				       NULL, NULL, NULL);
   7205       if (isymbuf1 == NULL)
   7206 	goto done;
   7207 
   7208       if (!info->reduce_memory_overheads)
   7209 	elf_tdata (bfd1)->symbuf = ssymbuf1
   7210 	  = elf_create_symbuf (symcount1, isymbuf1);
   7211     }
   7212 
   7213   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   7214     {
   7215       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   7216 				       NULL, NULL, NULL);
   7217       if (isymbuf2 == NULL)
   7218 	goto done;
   7219 
   7220       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
   7221 	elf_tdata (bfd2)->symbuf = ssymbuf2
   7222 	  = elf_create_symbuf (symcount2, isymbuf2);
   7223     }
   7224 
   7225   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   7226     {
   7227       /* Optimized faster version.  */
   7228       bfd_size_type lo, hi, mid;
   7229       struct elf_symbol *symp;
   7230       struct elf_symbuf_symbol *ssym, *ssymend;
   7231 
   7232       lo = 0;
   7233       hi = ssymbuf1->count;
   7234       ssymbuf1++;
   7235       count1 = 0;
   7236       while (lo < hi)
   7237 	{
   7238 	  mid = (lo + hi) / 2;
   7239 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   7240 	    hi = mid;
   7241 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   7242 	    lo = mid + 1;
   7243 	  else
   7244 	    {
   7245 	      count1 = ssymbuf1[mid].count;
   7246 	      ssymbuf1 += mid;
   7247 	      break;
   7248 	    }
   7249 	}
   7250 
   7251       lo = 0;
   7252       hi = ssymbuf2->count;
   7253       ssymbuf2++;
   7254       count2 = 0;
   7255       while (lo < hi)
   7256 	{
   7257 	  mid = (lo + hi) / 2;
   7258 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   7259 	    hi = mid;
   7260 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   7261 	    lo = mid + 1;
   7262 	  else
   7263 	    {
   7264 	      count2 = ssymbuf2[mid].count;
   7265 	      ssymbuf2 += mid;
   7266 	      break;
   7267 	    }
   7268 	}
   7269 
   7270       if (count1 == 0 || count2 == 0 || count1 != count2)
   7271 	goto done;
   7272 
   7273       symtable1 = (struct elf_symbol *)
   7274           bfd_malloc (count1 * sizeof (struct elf_symbol));
   7275       symtable2 = (struct elf_symbol *)
   7276           bfd_malloc (count2 * sizeof (struct elf_symbol));
   7277       if (symtable1 == NULL || symtable2 == NULL)
   7278 	goto done;
   7279 
   7280       symp = symtable1;
   7281       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
   7282 	   ssym < ssymend; ssym++, symp++)
   7283 	{
   7284 	  symp->u.ssym = ssym;
   7285 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
   7286 							hdr1->sh_link,
   7287 							ssym->st_name);
   7288 	}
   7289 
   7290       symp = symtable2;
   7291       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
   7292 	   ssym < ssymend; ssym++, symp++)
   7293 	{
   7294 	  symp->u.ssym = ssym;
   7295 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
   7296 							hdr2->sh_link,
   7297 							ssym->st_name);
   7298 	}
   7299 
   7300       /* Sort symbol by name.  */
   7301       qsort (symtable1, count1, sizeof (struct elf_symbol),
   7302 	     elf_sym_name_compare);
   7303       qsort (symtable2, count1, sizeof (struct elf_symbol),
   7304 	     elf_sym_name_compare);
   7305 
   7306       for (i = 0; i < count1; i++)
   7307 	/* Two symbols must have the same binding, type and name.  */
   7308 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   7309 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   7310 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7311 	  goto done;
   7312 
   7313       result = TRUE;
   7314       goto done;
   7315     }
   7316 
   7317   symtable1 = (struct elf_symbol *)
   7318       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   7319   symtable2 = (struct elf_symbol *)
   7320       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   7321   if (symtable1 == NULL || symtable2 == NULL)
   7322     goto done;
   7323 
   7324   /* Count definitions in the section.  */
   7325   count1 = 0;
   7326   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   7327     if (isym->st_shndx == shndx1)
   7328       symtable1[count1++].u.isym = isym;
   7329 
   7330   count2 = 0;
   7331   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   7332     if (isym->st_shndx == shndx2)
   7333       symtable2[count2++].u.isym = isym;
   7334 
   7335   if (count1 == 0 || count2 == 0 || count1 != count2)
   7336     goto done;
   7337 
   7338   for (i = 0; i < count1; i++)
   7339     symtable1[i].name
   7340       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   7341 					 symtable1[i].u.isym->st_name);
   7342 
   7343   for (i = 0; i < count2; i++)
   7344     symtable2[i].name
   7345       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   7346 					 symtable2[i].u.isym->st_name);
   7347 
   7348   /* Sort symbol by name.  */
   7349   qsort (symtable1, count1, sizeof (struct elf_symbol),
   7350 	 elf_sym_name_compare);
   7351   qsort (symtable2, count1, sizeof (struct elf_symbol),
   7352 	 elf_sym_name_compare);
   7353 
   7354   for (i = 0; i < count1; i++)
   7355     /* Two symbols must have the same binding, type and name.  */
   7356     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   7357 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   7358 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7359       goto done;
   7360 
   7361   result = TRUE;
   7362 
   7363 done:
   7364   if (symtable1)
   7365     free (symtable1);
   7366   if (symtable2)
   7367     free (symtable2);
   7368   if (isymbuf1)
   7369     free (isymbuf1);
   7370   if (isymbuf2)
   7371     free (isymbuf2);
   7372 
   7373   return result;
   7374 }
   7375 
   7376 /* Return TRUE if 2 section types are compatible.  */
   7377 
   7378 bfd_boolean
   7379 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   7380 				 bfd *bbfd, const asection *bsec)
   7381 {
   7382   if (asec == NULL
   7383       || bsec == NULL
   7384       || abfd->xvec->flavour != bfd_target_elf_flavour
   7385       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   7386     return TRUE;
   7387 
   7388   return elf_section_type (asec) == elf_section_type (bsec);
   7389 }
   7390 
   7391 /* Final phase of ELF linker.  */
   7393 
   7394 /* A structure we use to avoid passing large numbers of arguments.  */
   7395 
   7396 struct elf_final_link_info
   7397 {
   7398   /* General link information.  */
   7399   struct bfd_link_info *info;
   7400   /* Output BFD.  */
   7401   bfd *output_bfd;
   7402   /* Symbol string table.  */
   7403   struct bfd_strtab_hash *symstrtab;
   7404   /* .dynsym section.  */
   7405   asection *dynsym_sec;
   7406   /* .hash section.  */
   7407   asection *hash_sec;
   7408   /* symbol version section (.gnu.version).  */
   7409   asection *symver_sec;
   7410   /* Buffer large enough to hold contents of any section.  */
   7411   bfd_byte *contents;
   7412   /* Buffer large enough to hold external relocs of any section.  */
   7413   void *external_relocs;
   7414   /* Buffer large enough to hold internal relocs of any section.  */
   7415   Elf_Internal_Rela *internal_relocs;
   7416   /* Buffer large enough to hold external local symbols of any input
   7417      BFD.  */
   7418   bfd_byte *external_syms;
   7419   /* And a buffer for symbol section indices.  */
   7420   Elf_External_Sym_Shndx *locsym_shndx;
   7421   /* Buffer large enough to hold internal local symbols of any input
   7422      BFD.  */
   7423   Elf_Internal_Sym *internal_syms;
   7424   /* Array large enough to hold a symbol index for each local symbol
   7425      of any input BFD.  */
   7426   long *indices;
   7427   /* Array large enough to hold a section pointer for each local
   7428      symbol of any input BFD.  */
   7429   asection **sections;
   7430   /* Buffer to hold swapped out symbols.  */
   7431   bfd_byte *symbuf;
   7432   /* And one for symbol section indices.  */
   7433   Elf_External_Sym_Shndx *symshndxbuf;
   7434   /* Number of swapped out symbols in buffer.  */
   7435   size_t symbuf_count;
   7436   /* Number of symbols which fit in symbuf.  */
   7437   size_t symbuf_size;
   7438   /* And same for symshndxbuf.  */
   7439   size_t shndxbuf_size;
   7440   /* Number of STT_FILE syms seen.  */
   7441   size_t filesym_count;
   7442 };
   7443 
   7444 /* This struct is used to pass information to elf_link_output_extsym.  */
   7445 
   7446 struct elf_outext_info
   7447 {
   7448   bfd_boolean failed;
   7449   bfd_boolean localsyms;
   7450   bfd_boolean need_second_pass;
   7451   bfd_boolean second_pass;
   7452   bfd_boolean file_sym_done;
   7453   struct elf_final_link_info *flinfo;
   7454 };
   7455 
   7456 
   7457 /* Support for evaluating a complex relocation.
   7458 
   7459    Complex relocations are generalized, self-describing relocations.  The
   7460    implementation of them consists of two parts: complex symbols, and the
   7461    relocations themselves.
   7462 
   7463    The relocations are use a reserved elf-wide relocation type code (R_RELC
   7464    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   7465    information (start bit, end bit, word width, etc) into the addend.  This
   7466    information is extracted from CGEN-generated operand tables within gas.
   7467 
   7468    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
   7469    internal) representing prefix-notation expressions, including but not
   7470    limited to those sorts of expressions normally encoded as addends in the
   7471    addend field.  The symbol mangling format is:
   7472 
   7473    <node> := <literal>
   7474           |  <unary-operator> ':' <node>
   7475           |  <binary-operator> ':' <node> ':' <node>
   7476 	  ;
   7477 
   7478    <literal> := 's' <digits=N> ':' <N character symbol name>
   7479              |  'S' <digits=N> ':' <N character section name>
   7480 	     |  '#' <hexdigits>
   7481 	     ;
   7482 
   7483    <binary-operator> := as in C
   7484    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   7485 
   7486 static void
   7487 set_symbol_value (bfd *bfd_with_globals,
   7488 		  Elf_Internal_Sym *isymbuf,
   7489 		  size_t locsymcount,
   7490 		  size_t symidx,
   7491 		  bfd_vma val)
   7492 {
   7493   struct elf_link_hash_entry **sym_hashes;
   7494   struct elf_link_hash_entry *h;
   7495   size_t extsymoff = locsymcount;
   7496 
   7497   if (symidx < locsymcount)
   7498     {
   7499       Elf_Internal_Sym *sym;
   7500 
   7501       sym = isymbuf + symidx;
   7502       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   7503 	{
   7504 	  /* It is a local symbol: move it to the
   7505 	     "absolute" section and give it a value.  */
   7506 	  sym->st_shndx = SHN_ABS;
   7507 	  sym->st_value = val;
   7508 	  return;
   7509 	}
   7510       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   7511       extsymoff = 0;
   7512     }
   7513 
   7514   /* It is a global symbol: set its link type
   7515      to "defined" and give it a value.  */
   7516 
   7517   sym_hashes = elf_sym_hashes (bfd_with_globals);
   7518   h = sym_hashes [symidx - extsymoff];
   7519   while (h->root.type == bfd_link_hash_indirect
   7520 	 || h->root.type == bfd_link_hash_warning)
   7521     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7522   h->root.type = bfd_link_hash_defined;
   7523   h->root.u.def.value = val;
   7524   h->root.u.def.section = bfd_abs_section_ptr;
   7525 }
   7526 
   7527 static bfd_boolean
   7528 resolve_symbol (const char *name,
   7529 		bfd *input_bfd,
   7530 		struct elf_final_link_info *flinfo,
   7531 		bfd_vma *result,
   7532 		Elf_Internal_Sym *isymbuf,
   7533 		size_t locsymcount)
   7534 {
   7535   Elf_Internal_Sym *sym;
   7536   struct bfd_link_hash_entry *global_entry;
   7537   const char *candidate = NULL;
   7538   Elf_Internal_Shdr *symtab_hdr;
   7539   size_t i;
   7540 
   7541   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   7542 
   7543   for (i = 0; i < locsymcount; ++ i)
   7544     {
   7545       sym = isymbuf + i;
   7546 
   7547       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   7548 	continue;
   7549 
   7550       candidate = bfd_elf_string_from_elf_section (input_bfd,
   7551 						   symtab_hdr->sh_link,
   7552 						   sym->st_name);
   7553 #ifdef DEBUG
   7554       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   7555 	      name, candidate, (unsigned long) sym->st_value);
   7556 #endif
   7557       if (candidate && strcmp (candidate, name) == 0)
   7558 	{
   7559 	  asection *sec = flinfo->sections [i];
   7560 
   7561 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   7562 	  *result += sec->output_offset + sec->output_section->vma;
   7563 #ifdef DEBUG
   7564 	  printf ("Found symbol with value %8.8lx\n",
   7565 		  (unsigned long) *result);
   7566 #endif
   7567 	  return TRUE;
   7568 	}
   7569     }
   7570 
   7571   /* Hmm, haven't found it yet. perhaps it is a global.  */
   7572   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   7573 				       FALSE, FALSE, TRUE);
   7574   if (!global_entry)
   7575     return FALSE;
   7576 
   7577   if (global_entry->type == bfd_link_hash_defined
   7578       || global_entry->type == bfd_link_hash_defweak)
   7579     {
   7580       *result = (global_entry->u.def.value
   7581 		 + global_entry->u.def.section->output_section->vma
   7582 		 + global_entry->u.def.section->output_offset);
   7583 #ifdef DEBUG
   7584       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   7585 	      global_entry->root.string, (unsigned long) *result);
   7586 #endif
   7587       return TRUE;
   7588     }
   7589 
   7590   return FALSE;
   7591 }
   7592 
   7593 static bfd_boolean
   7594 resolve_section (const char *name,
   7595 		 asection *sections,
   7596 		 bfd_vma *result)
   7597 {
   7598   asection *curr;
   7599   unsigned int len;
   7600 
   7601   for (curr = sections; curr; curr = curr->next)
   7602     if (strcmp (curr->name, name) == 0)
   7603       {
   7604 	*result = curr->vma;
   7605 	return TRUE;
   7606       }
   7607 
   7608   /* Hmm. still haven't found it. try pseudo-section names.  */
   7609   for (curr = sections; curr; curr = curr->next)
   7610     {
   7611       len = strlen (curr->name);
   7612       if (len > strlen (name))
   7613 	continue;
   7614 
   7615       if (strncmp (curr->name, name, len) == 0)
   7616 	{
   7617 	  if (strncmp (".end", name + len, 4) == 0)
   7618 	    {
   7619 	      *result = curr->vma + curr->size;
   7620 	      return TRUE;
   7621 	    }
   7622 
   7623 	  /* Insert more pseudo-section names here, if you like.  */
   7624 	}
   7625     }
   7626 
   7627   return FALSE;
   7628 }
   7629 
   7630 static void
   7631 undefined_reference (const char *reftype, const char *name)
   7632 {
   7633   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   7634 		      reftype, name);
   7635 }
   7636 
   7637 static bfd_boolean
   7638 eval_symbol (bfd_vma *result,
   7639 	     const char **symp,
   7640 	     bfd *input_bfd,
   7641 	     struct elf_final_link_info *flinfo,
   7642 	     bfd_vma dot,
   7643 	     Elf_Internal_Sym *isymbuf,
   7644 	     size_t locsymcount,
   7645 	     int signed_p)
   7646 {
   7647   size_t len;
   7648   size_t symlen;
   7649   bfd_vma a;
   7650   bfd_vma b;
   7651   char symbuf[4096];
   7652   const char *sym = *symp;
   7653   const char *symend;
   7654   bfd_boolean symbol_is_section = FALSE;
   7655 
   7656   len = strlen (sym);
   7657   symend = sym + len;
   7658 
   7659   if (len < 1 || len > sizeof (symbuf))
   7660     {
   7661       bfd_set_error (bfd_error_invalid_operation);
   7662       return FALSE;
   7663     }
   7664 
   7665   switch (* sym)
   7666     {
   7667     case '.':
   7668       *result = dot;
   7669       *symp = sym + 1;
   7670       return TRUE;
   7671 
   7672     case '#':
   7673       ++sym;
   7674       *result = strtoul (sym, (char **) symp, 16);
   7675       return TRUE;
   7676 
   7677     case 'S':
   7678       symbol_is_section = TRUE;
   7679     case 's':
   7680       ++sym;
   7681       symlen = strtol (sym, (char **) symp, 10);
   7682       sym = *symp + 1; /* Skip the trailing ':'.  */
   7683 
   7684       if (symend < sym || symlen + 1 > sizeof (symbuf))
   7685 	{
   7686 	  bfd_set_error (bfd_error_invalid_operation);
   7687 	  return FALSE;
   7688 	}
   7689 
   7690       memcpy (symbuf, sym, symlen);
   7691       symbuf[symlen] = '\0';
   7692       *symp = sym + symlen;
   7693 
   7694       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   7695 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   7696 	 interpretation here; section means "try section first", not "must be a
   7697 	 section", and likewise with symbol.  */
   7698 
   7699       if (symbol_is_section)
   7700 	{
   7701 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
   7702 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   7703 				  isymbuf, locsymcount))
   7704 	    {
   7705 	      undefined_reference ("section", symbuf);
   7706 	      return FALSE;
   7707 	    }
   7708 	}
   7709       else
   7710 	{
   7711 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   7712 			       isymbuf, locsymcount)
   7713 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   7714 				   result))
   7715 	    {
   7716 	      undefined_reference ("symbol", symbuf);
   7717 	      return FALSE;
   7718 	    }
   7719 	}
   7720 
   7721       return TRUE;
   7722 
   7723       /* All that remains are operators.  */
   7724 
   7725 #define UNARY_OP(op)						\
   7726   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   7727     {								\
   7728       sym += strlen (#op);					\
   7729       if (*sym == ':')						\
   7730 	++sym;							\
   7731       *symp = sym;						\
   7732       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   7733 			isymbuf, locsymcount, signed_p))	\
   7734 	return FALSE;						\
   7735       if (signed_p)						\
   7736 	*result = op ((bfd_signed_vma) a);			\
   7737       else							\
   7738 	*result = op a;						\
   7739       return TRUE;						\
   7740     }
   7741 
   7742 #define BINARY_OP(op)						\
   7743   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   7744     {								\
   7745       sym += strlen (#op);					\
   7746       if (*sym == ':')						\
   7747 	++sym;							\
   7748       *symp = sym;						\
   7749       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   7750 			isymbuf, locsymcount, signed_p))	\
   7751 	return FALSE;						\
   7752       ++*symp;							\
   7753       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   7754 			isymbuf, locsymcount, signed_p))	\
   7755 	return FALSE;						\
   7756       if (signed_p)						\
   7757 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   7758       else							\
   7759 	*result = a op b;					\
   7760       return TRUE;						\
   7761     }
   7762 
   7763     default:
   7764       UNARY_OP  (0-);
   7765       BINARY_OP (<<);
   7766       BINARY_OP (>>);
   7767       BINARY_OP (==);
   7768       BINARY_OP (!=);
   7769       BINARY_OP (<=);
   7770       BINARY_OP (>=);
   7771       BINARY_OP (&&);
   7772       BINARY_OP (||);
   7773       UNARY_OP  (~);
   7774       UNARY_OP  (!);
   7775       BINARY_OP (*);
   7776       BINARY_OP (/);
   7777       BINARY_OP (%);
   7778       BINARY_OP (^);
   7779       BINARY_OP (|);
   7780       BINARY_OP (&);
   7781       BINARY_OP (+);
   7782       BINARY_OP (-);
   7783       BINARY_OP (<);
   7784       BINARY_OP (>);
   7785 #undef UNARY_OP
   7786 #undef BINARY_OP
   7787       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   7788       bfd_set_error (bfd_error_invalid_operation);
   7789       return FALSE;
   7790     }
   7791 }
   7792 
   7793 static void
   7794 put_value (bfd_vma size,
   7795 	   unsigned long chunksz,
   7796 	   bfd *input_bfd,
   7797 	   bfd_vma x,
   7798 	   bfd_byte *location)
   7799 {
   7800   location += (size - chunksz);
   7801 
   7802   for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
   7803     {
   7804       switch (chunksz)
   7805 	{
   7806 	default:
   7807 	case 0:
   7808 	  abort ();
   7809 	case 1:
   7810 	  bfd_put_8 (input_bfd, x, location);
   7811 	  break;
   7812 	case 2:
   7813 	  bfd_put_16 (input_bfd, x, location);
   7814 	  break;
   7815 	case 4:
   7816 	  bfd_put_32 (input_bfd, x, location);
   7817 	  break;
   7818 	case 8:
   7819 #ifdef BFD64
   7820 	  bfd_put_64 (input_bfd, x, location);
   7821 #else
   7822 	  abort ();
   7823 #endif
   7824 	  break;
   7825 	}
   7826     }
   7827 }
   7828 
   7829 static bfd_vma
   7830 get_value (bfd_vma size,
   7831 	   unsigned long chunksz,
   7832 	   bfd *input_bfd,
   7833 	   bfd_byte *location)
   7834 {
   7835   int shift;
   7836   bfd_vma x = 0;
   7837 
   7838   /* Sanity checks.  */
   7839   BFD_ASSERT (chunksz <= sizeof (x)
   7840 	      && size >= chunksz
   7841 	      && chunksz != 0
   7842 	      && (size % chunksz) == 0
   7843 	      && input_bfd != NULL
   7844 	      && location != NULL);
   7845 
   7846   if (chunksz == sizeof (x))
   7847     {
   7848       BFD_ASSERT (size == chunksz);
   7849 
   7850       /* Make sure that we do not perform an undefined shift operation.
   7851 	 We know that size == chunksz so there will only be one iteration
   7852 	 of the loop below.  */
   7853       shift = 0;
   7854     }
   7855   else
   7856     shift = 8 * chunksz;
   7857 
   7858   for (; size; size -= chunksz, location += chunksz)
   7859     {
   7860       switch (chunksz)
   7861 	{
   7862 	case 1:
   7863 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   7864 	  break;
   7865 	case 2:
   7866 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   7867 	  break;
   7868 	case 4:
   7869 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   7870 	  break;
   7871 #ifdef BFD64
   7872 	case 8:
   7873 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   7874 	  break;
   7875 #endif
   7876 	default:
   7877 	  abort ();
   7878 	}
   7879     }
   7880   return x;
   7881 }
   7882 
   7883 static void
   7884 decode_complex_addend (unsigned long *start,   /* in bits */
   7885 		       unsigned long *oplen,   /* in bits */
   7886 		       unsigned long *len,     /* in bits */
   7887 		       unsigned long *wordsz,  /* in bytes */
   7888 		       unsigned long *chunksz, /* in bytes */
   7889 		       unsigned long *lsb0_p,
   7890 		       unsigned long *signed_p,
   7891 		       unsigned long *trunc_p,
   7892 		       unsigned long encoded)
   7893 {
   7894   * start     =  encoded        & 0x3F;
   7895   * len       = (encoded >>  6) & 0x3F;
   7896   * oplen     = (encoded >> 12) & 0x3F;
   7897   * wordsz    = (encoded >> 18) & 0xF;
   7898   * chunksz   = (encoded >> 22) & 0xF;
   7899   * lsb0_p    = (encoded >> 27) & 1;
   7900   * signed_p  = (encoded >> 28) & 1;
   7901   * trunc_p   = (encoded >> 29) & 1;
   7902 }
   7903 
   7904 bfd_reloc_status_type
   7905 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   7906 				    asection *input_section ATTRIBUTE_UNUSED,
   7907 				    bfd_byte *contents,
   7908 				    Elf_Internal_Rela *rel,
   7909 				    bfd_vma relocation)
   7910 {
   7911   bfd_vma shift, x, mask;
   7912   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   7913   bfd_reloc_status_type r;
   7914 
   7915   /*  Perform this reloc, since it is complex.
   7916       (this is not to say that it necessarily refers to a complex
   7917       symbol; merely that it is a self-describing CGEN based reloc.
   7918       i.e. the addend has the complete reloc information (bit start, end,
   7919       word size, etc) encoded within it.).  */
   7920 
   7921   decode_complex_addend (&start, &oplen, &len, &wordsz,
   7922 			 &chunksz, &lsb0_p, &signed_p,
   7923 			 &trunc_p, rel->r_addend);
   7924 
   7925   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   7926 
   7927   if (lsb0_p)
   7928     shift = (start + 1) - len;
   7929   else
   7930     shift = (8 * wordsz) - (start + len);
   7931 
   7932   /* FIXME: octets_per_byte.  */
   7933   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
   7934 
   7935 #ifdef DEBUG
   7936   printf ("Doing complex reloc: "
   7937 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   7938 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   7939 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   7940 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   7941 	  oplen, (unsigned long) x, (unsigned long) mask,
   7942 	  (unsigned long) relocation);
   7943 #endif
   7944 
   7945   r = bfd_reloc_ok;
   7946   if (! trunc_p)
   7947     /* Now do an overflow check.  */
   7948     r = bfd_check_overflow ((signed_p
   7949 			     ? complain_overflow_signed
   7950 			     : complain_overflow_unsigned),
   7951 			    len, 0, (8 * wordsz),
   7952 			    relocation);
   7953 
   7954   /* Do the deed.  */
   7955   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   7956 
   7957 #ifdef DEBUG
   7958   printf ("           relocation: %8.8lx\n"
   7959 	  "         shifted mask: %8.8lx\n"
   7960 	  " shifted/masked reloc: %8.8lx\n"
   7961 	  "               result: %8.8lx\n",
   7962 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   7963 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   7964 #endif
   7965   /* FIXME: octets_per_byte.  */
   7966   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
   7967   return r;
   7968 }
   7969 
   7970 /* When performing a relocatable link, the input relocations are
   7971    preserved.  But, if they reference global symbols, the indices
   7972    referenced must be updated.  Update all the relocations found in
   7973    RELDATA.  */
   7974 
   7975 static void
   7976 elf_link_adjust_relocs (bfd *abfd,
   7977 			struct bfd_elf_section_reloc_data *reldata)
   7978 {
   7979   unsigned int i;
   7980   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7981   bfd_byte *erela;
   7982   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   7983   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   7984   bfd_vma r_type_mask;
   7985   int r_sym_shift;
   7986   unsigned int count = reldata->count;
   7987   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   7988 
   7989   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   7990     {
   7991       swap_in = bed->s->swap_reloc_in;
   7992       swap_out = bed->s->swap_reloc_out;
   7993     }
   7994   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   7995     {
   7996       swap_in = bed->s->swap_reloca_in;
   7997       swap_out = bed->s->swap_reloca_out;
   7998     }
   7999   else
   8000     abort ();
   8001 
   8002   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   8003     abort ();
   8004 
   8005   if (bed->s->arch_size == 32)
   8006     {
   8007       r_type_mask = 0xff;
   8008       r_sym_shift = 8;
   8009     }
   8010   else
   8011     {
   8012       r_type_mask = 0xffffffff;
   8013       r_sym_shift = 32;
   8014     }
   8015 
   8016   erela = reldata->hdr->contents;
   8017   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   8018     {
   8019       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   8020       unsigned int j;
   8021 
   8022       if (*rel_hash == NULL)
   8023 	continue;
   8024 
   8025       BFD_ASSERT ((*rel_hash)->indx >= 0);
   8026 
   8027       (*swap_in) (abfd, erela, irela);
   8028       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   8029 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   8030 			   | (irela[j].r_info & r_type_mask));
   8031       (*swap_out) (abfd, irela, erela);
   8032     }
   8033 }
   8034 
   8035 struct elf_link_sort_rela
   8036 {
   8037   union {
   8038     bfd_vma offset;
   8039     bfd_vma sym_mask;
   8040   } u;
   8041   enum elf_reloc_type_class type;
   8042   /* We use this as an array of size int_rels_per_ext_rel.  */
   8043   Elf_Internal_Rela rela[1];
   8044 };
   8045 
   8046 static int
   8047 elf_link_sort_cmp1 (const void *A, const void *B)
   8048 {
   8049   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8050   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8051   int relativea, relativeb;
   8052 
   8053   relativea = a->type == reloc_class_relative;
   8054   relativeb = b->type == reloc_class_relative;
   8055 
   8056   if (relativea < relativeb)
   8057     return 1;
   8058   if (relativea > relativeb)
   8059     return -1;
   8060   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   8061     return -1;
   8062   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   8063     return 1;
   8064   if (a->rela->r_offset < b->rela->r_offset)
   8065     return -1;
   8066   if (a->rela->r_offset > b->rela->r_offset)
   8067     return 1;
   8068   return 0;
   8069 }
   8070 
   8071 static int
   8072 elf_link_sort_cmp2 (const void *A, const void *B)
   8073 {
   8074   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8075   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8076 
   8077   if (a->type < b->type)
   8078     return -1;
   8079   if (a->type > b->type)
   8080     return 1;
   8081   if (a->u.offset < b->u.offset)
   8082     return -1;
   8083   if (a->u.offset > b->u.offset)
   8084     return 1;
   8085   if (a->rela->r_offset < b->rela->r_offset)
   8086     return -1;
   8087   if (a->rela->r_offset > b->rela->r_offset)
   8088     return 1;
   8089   return 0;
   8090 }
   8091 
   8092 static size_t
   8093 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   8094 {
   8095   asection *dynamic_relocs;
   8096   asection *rela_dyn;
   8097   asection *rel_dyn;
   8098   bfd_size_type count, size;
   8099   size_t i, ret, sort_elt, ext_size;
   8100   bfd_byte *sort, *s_non_relative, *p;
   8101   struct elf_link_sort_rela *sq;
   8102   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8103   int i2e = bed->s->int_rels_per_ext_rel;
   8104   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8105   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8106   struct bfd_link_order *lo;
   8107   bfd_vma r_sym_mask;
   8108   bfd_boolean use_rela;
   8109 
   8110   /* Find a dynamic reloc section.  */
   8111   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   8112   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   8113   if (rela_dyn != NULL && rela_dyn->size > 0
   8114       && rel_dyn != NULL && rel_dyn->size > 0)
   8115     {
   8116       bfd_boolean use_rela_initialised = FALSE;
   8117 
   8118       /* This is just here to stop gcc from complaining.
   8119 	 It's initialization checking code is not perfect.  */
   8120       use_rela = TRUE;
   8121 
   8122       /* Both sections are present.  Examine the sizes
   8123 	 of the indirect sections to help us choose.  */
   8124       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   8125 	if (lo->type == bfd_indirect_link_order)
   8126 	  {
   8127 	    asection *o = lo->u.indirect.section;
   8128 
   8129 	    if ((o->size % bed->s->sizeof_rela) == 0)
   8130 	      {
   8131 		if ((o->size % bed->s->sizeof_rel) == 0)
   8132 		  /* Section size is divisible by both rel and rela sizes.
   8133 		     It is of no help to us.  */
   8134 		  ;
   8135 		else
   8136 		  {
   8137 		    /* Section size is only divisible by rela.  */
   8138 		    if (use_rela_initialised && (use_rela == FALSE))
   8139 		      {
   8140 			_bfd_error_handler
   8141 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8142 			bfd_set_error (bfd_error_invalid_operation);
   8143 			return 0;
   8144 		      }
   8145 		    else
   8146 		      {
   8147 			use_rela = TRUE;
   8148 			use_rela_initialised = TRUE;
   8149 		      }
   8150 		  }
   8151 	      }
   8152 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   8153 	      {
   8154 		/* Section size is only divisible by rel.  */
   8155 		if (use_rela_initialised && (use_rela == TRUE))
   8156 		  {
   8157 		    _bfd_error_handler
   8158 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8159 		    bfd_set_error (bfd_error_invalid_operation);
   8160 		    return 0;
   8161 		  }
   8162 		else
   8163 		  {
   8164 		    use_rela = FALSE;
   8165 		    use_rela_initialised = TRUE;
   8166 		  }
   8167 	      }
   8168 	    else
   8169 	      {
   8170 		/* The section size is not divisible by either - something is wrong.  */
   8171 		_bfd_error_handler
   8172 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
   8173 		bfd_set_error (bfd_error_invalid_operation);
   8174 		return 0;
   8175 	      }
   8176 	  }
   8177 
   8178       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   8179 	if (lo->type == bfd_indirect_link_order)
   8180 	  {
   8181 	    asection *o = lo->u.indirect.section;
   8182 
   8183 	    if ((o->size % bed->s->sizeof_rela) == 0)
   8184 	      {
   8185 		if ((o->size % bed->s->sizeof_rel) == 0)
   8186 		  /* Section size is divisible by both rel and rela sizes.
   8187 		     It is of no help to us.  */
   8188 		  ;
   8189 		else
   8190 		  {
   8191 		    /* Section size is only divisible by rela.  */
   8192 		    if (use_rela_initialised && (use_rela == FALSE))
   8193 		      {
   8194 			_bfd_error_handler
   8195 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8196 			bfd_set_error (bfd_error_invalid_operation);
   8197 			return 0;
   8198 		      }
   8199 		    else
   8200 		      {
   8201 			use_rela = TRUE;
   8202 			use_rela_initialised = TRUE;
   8203 		      }
   8204 		  }
   8205 	      }
   8206 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   8207 	      {
   8208 		/* Section size is only divisible by rel.  */
   8209 		if (use_rela_initialised && (use_rela == TRUE))
   8210 		  {
   8211 		    _bfd_error_handler
   8212 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8213 		    bfd_set_error (bfd_error_invalid_operation);
   8214 		    return 0;
   8215 		  }
   8216 		else
   8217 		  {
   8218 		    use_rela = FALSE;
   8219 		    use_rela_initialised = TRUE;
   8220 		  }
   8221 	      }
   8222 	    else
   8223 	      {
   8224 		/* The section size is not divisible by either - something is wrong.  */
   8225 		_bfd_error_handler
   8226 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
   8227 		bfd_set_error (bfd_error_invalid_operation);
   8228 		return 0;
   8229 	      }
   8230 	  }
   8231 
   8232       if (! use_rela_initialised)
   8233 	/* Make a guess.  */
   8234 	use_rela = TRUE;
   8235     }
   8236   else if (rela_dyn != NULL && rela_dyn->size > 0)
   8237     use_rela = TRUE;
   8238   else if (rel_dyn != NULL && rel_dyn->size > 0)
   8239     use_rela = FALSE;
   8240   else
   8241     return 0;
   8242 
   8243   if (use_rela)
   8244     {
   8245       dynamic_relocs = rela_dyn;
   8246       ext_size = bed->s->sizeof_rela;
   8247       swap_in = bed->s->swap_reloca_in;
   8248       swap_out = bed->s->swap_reloca_out;
   8249     }
   8250   else
   8251     {
   8252       dynamic_relocs = rel_dyn;
   8253       ext_size = bed->s->sizeof_rel;
   8254       swap_in = bed->s->swap_reloc_in;
   8255       swap_out = bed->s->swap_reloc_out;
   8256     }
   8257 
   8258   size = 0;
   8259   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8260     if (lo->type == bfd_indirect_link_order)
   8261       size += lo->u.indirect.section->size;
   8262 
   8263   if (size != dynamic_relocs->size)
   8264     return 0;
   8265 
   8266   sort_elt = (sizeof (struct elf_link_sort_rela)
   8267 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   8268 
   8269   count = dynamic_relocs->size / ext_size;
   8270   if (count == 0)
   8271     return 0;
   8272   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   8273 
   8274   if (sort == NULL)
   8275     {
   8276       (*info->callbacks->warning)
   8277 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
   8278       return 0;
   8279     }
   8280 
   8281   if (bed->s->arch_size == 32)
   8282     r_sym_mask = ~(bfd_vma) 0xff;
   8283   else
   8284     r_sym_mask = ~(bfd_vma) 0xffffffff;
   8285 
   8286   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8287     if (lo->type == bfd_indirect_link_order)
   8288       {
   8289 	bfd_byte *erel, *erelend;
   8290 	asection *o = lo->u.indirect.section;
   8291 
   8292 	if (o->contents == NULL && o->size != 0)
   8293 	  {
   8294 	    /* This is a reloc section that is being handled as a normal
   8295 	       section.  See bfd_section_from_shdr.  We can't combine
   8296 	       relocs in this case.  */
   8297 	    free (sort);
   8298 	    return 0;
   8299 	  }
   8300 	erel = o->contents;
   8301 	erelend = o->contents + o->size;
   8302 	/* FIXME: octets_per_byte.  */
   8303 	p = sort + o->output_offset / ext_size * sort_elt;
   8304 
   8305 	while (erel < erelend)
   8306 	  {
   8307 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8308 
   8309 	    (*swap_in) (abfd, erel, s->rela);
   8310 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   8311 	    s->u.sym_mask = r_sym_mask;
   8312 	    p += sort_elt;
   8313 	    erel += ext_size;
   8314 	  }
   8315       }
   8316 
   8317   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   8318 
   8319   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   8320     {
   8321       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8322       if (s->type != reloc_class_relative)
   8323 	break;
   8324     }
   8325   ret = i;
   8326   s_non_relative = p;
   8327 
   8328   sq = (struct elf_link_sort_rela *) s_non_relative;
   8329   for (; i < count; i++, p += sort_elt)
   8330     {
   8331       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   8332       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   8333 	sq = sp;
   8334       sp->u.offset = sq->rela->r_offset;
   8335     }
   8336 
   8337   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   8338 
   8339   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8340     if (lo->type == bfd_indirect_link_order)
   8341       {
   8342 	bfd_byte *erel, *erelend;
   8343 	asection *o = lo->u.indirect.section;
   8344 
   8345 	erel = o->contents;
   8346 	erelend = o->contents + o->size;
   8347 	/* FIXME: octets_per_byte.  */
   8348 	p = sort + o->output_offset / ext_size * sort_elt;
   8349 	while (erel < erelend)
   8350 	  {
   8351 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8352 	    (*swap_out) (abfd, s->rela, erel);
   8353 	    p += sort_elt;
   8354 	    erel += ext_size;
   8355 	  }
   8356       }
   8357 
   8358   free (sort);
   8359   *psec = dynamic_relocs;
   8360   return ret;
   8361 }
   8362 
   8363 /* Flush the output symbols to the file.  */
   8364 
   8365 static bfd_boolean
   8366 elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
   8367 			    const struct elf_backend_data *bed)
   8368 {
   8369   if (flinfo->symbuf_count > 0)
   8370     {
   8371       Elf_Internal_Shdr *hdr;
   8372       file_ptr pos;
   8373       bfd_size_type amt;
   8374 
   8375       hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   8376       pos = hdr->sh_offset + hdr->sh_size;
   8377       amt = flinfo->symbuf_count * bed->s->sizeof_sym;
   8378       if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
   8379 	  || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
   8380 	return FALSE;
   8381 
   8382       hdr->sh_size += amt;
   8383       flinfo->symbuf_count = 0;
   8384     }
   8385 
   8386   return TRUE;
   8387 }
   8388 
   8389 /* Add a symbol to the output symbol table.  */
   8390 
   8391 static int
   8392 elf_link_output_sym (struct elf_final_link_info *flinfo,
   8393 		     const char *name,
   8394 		     Elf_Internal_Sym *elfsym,
   8395 		     asection *input_sec,
   8396 		     struct elf_link_hash_entry *h)
   8397 {
   8398   bfd_byte *dest;
   8399   Elf_External_Sym_Shndx *destshndx;
   8400   int (*output_symbol_hook)
   8401     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   8402      struct elf_link_hash_entry *);
   8403   const struct elf_backend_data *bed;
   8404 
   8405   bed = get_elf_backend_data (flinfo->output_bfd);
   8406   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   8407   if (output_symbol_hook != NULL)
   8408     {
   8409       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   8410       if (ret != 1)
   8411 	return ret;
   8412     }
   8413 
   8414   if (name == NULL || *name == '\0')
   8415     elfsym->st_name = 0;
   8416   else if (input_sec->flags & SEC_EXCLUDE)
   8417     elfsym->st_name = 0;
   8418   else
   8419     {
   8420       elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
   8421 							    name, TRUE, FALSE);
   8422       if (elfsym->st_name == (unsigned long) -1)
   8423 	return 0;
   8424     }
   8425 
   8426   if (flinfo->symbuf_count >= flinfo->symbuf_size)
   8427     {
   8428       if (! elf_link_flush_output_syms (flinfo, bed))
   8429 	return 0;
   8430     }
   8431 
   8432   dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
   8433   destshndx = flinfo->symshndxbuf;
   8434   if (destshndx != NULL)
   8435     {
   8436       if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
   8437 	{
   8438 	  bfd_size_type amt;
   8439 
   8440 	  amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
   8441 	  destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
   8442                                                               amt * 2);
   8443 	  if (destshndx == NULL)
   8444 	    return 0;
   8445 	  flinfo->symshndxbuf = destshndx;
   8446 	  memset ((char *) destshndx + amt, 0, amt);
   8447 	  flinfo->shndxbuf_size *= 2;
   8448 	}
   8449       destshndx += bfd_get_symcount (flinfo->output_bfd);
   8450     }
   8451 
   8452   bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
   8453   flinfo->symbuf_count += 1;
   8454   bfd_get_symcount (flinfo->output_bfd) += 1;
   8455 
   8456   return 1;
   8457 }
   8458 
   8459 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   8460 
   8461 static bfd_boolean
   8462 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   8463 {
   8464   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   8465       && sym->st_shndx < SHN_LORESERVE)
   8466     {
   8467       /* The gABI doesn't support dynamic symbols in output sections
   8468 	 beyond 64k.  */
   8469       (*_bfd_error_handler)
   8470 	(_("%B: Too many sections: %d (>= %d)"),
   8471 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   8472       bfd_set_error (bfd_error_nonrepresentable_section);
   8473       return FALSE;
   8474     }
   8475   return TRUE;
   8476 }
   8477 
   8478 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   8479    allowing an unsatisfied unversioned symbol in the DSO to match a
   8480    versioned symbol that would normally require an explicit version.
   8481    We also handle the case that a DSO references a hidden symbol
   8482    which may be satisfied by a versioned symbol in another DSO.  */
   8483 
   8484 static bfd_boolean
   8485 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   8486 				 const struct elf_backend_data *bed,
   8487 				 struct elf_link_hash_entry *h)
   8488 {
   8489   bfd *abfd;
   8490   struct elf_link_loaded_list *loaded;
   8491 
   8492   if (!is_elf_hash_table (info->hash))
   8493     return FALSE;
   8494 
   8495   /* Check indirect symbol.  */
   8496   while (h->root.type == bfd_link_hash_indirect)
   8497     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8498 
   8499   switch (h->root.type)
   8500     {
   8501     default:
   8502       abfd = NULL;
   8503       break;
   8504 
   8505     case bfd_link_hash_undefined:
   8506     case bfd_link_hash_undefweak:
   8507       abfd = h->root.u.undef.abfd;
   8508       if ((abfd->flags & DYNAMIC) == 0
   8509 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   8510 	return FALSE;
   8511       break;
   8512 
   8513     case bfd_link_hash_defined:
   8514     case bfd_link_hash_defweak:
   8515       abfd = h->root.u.def.section->owner;
   8516       break;
   8517 
   8518     case bfd_link_hash_common:
   8519       abfd = h->root.u.c.p->section->owner;
   8520       break;
   8521     }
   8522   BFD_ASSERT (abfd != NULL);
   8523 
   8524   for (loaded = elf_hash_table (info)->loaded;
   8525        loaded != NULL;
   8526        loaded = loaded->next)
   8527     {
   8528       bfd *input;
   8529       Elf_Internal_Shdr *hdr;
   8530       bfd_size_type symcount;
   8531       bfd_size_type extsymcount;
   8532       bfd_size_type extsymoff;
   8533       Elf_Internal_Shdr *versymhdr;
   8534       Elf_Internal_Sym *isym;
   8535       Elf_Internal_Sym *isymend;
   8536       Elf_Internal_Sym *isymbuf;
   8537       Elf_External_Versym *ever;
   8538       Elf_External_Versym *extversym;
   8539 
   8540       input = loaded->abfd;
   8541 
   8542       /* We check each DSO for a possible hidden versioned definition.  */
   8543       if (input == abfd
   8544 	  || (input->flags & DYNAMIC) == 0
   8545 	  || elf_dynversym (input) == 0)
   8546 	continue;
   8547 
   8548       hdr = &elf_tdata (input)->dynsymtab_hdr;
   8549 
   8550       symcount = hdr->sh_size / bed->s->sizeof_sym;
   8551       if (elf_bad_symtab (input))
   8552 	{
   8553 	  extsymcount = symcount;
   8554 	  extsymoff = 0;
   8555 	}
   8556       else
   8557 	{
   8558 	  extsymcount = symcount - hdr->sh_info;
   8559 	  extsymoff = hdr->sh_info;
   8560 	}
   8561 
   8562       if (extsymcount == 0)
   8563 	continue;
   8564 
   8565       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   8566 				      NULL, NULL, NULL);
   8567       if (isymbuf == NULL)
   8568 	return FALSE;
   8569 
   8570       /* Read in any version definitions.  */
   8571       versymhdr = &elf_tdata (input)->dynversym_hdr;
   8572       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   8573       if (extversym == NULL)
   8574 	goto error_ret;
   8575 
   8576       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   8577 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
   8578 	      != versymhdr->sh_size))
   8579 	{
   8580 	  free (extversym);
   8581 	error_ret:
   8582 	  free (isymbuf);
   8583 	  return FALSE;
   8584 	}
   8585 
   8586       ever = extversym + extsymoff;
   8587       isymend = isymbuf + extsymcount;
   8588       for (isym = isymbuf; isym < isymend; isym++, ever++)
   8589 	{
   8590 	  const char *name;
   8591 	  Elf_Internal_Versym iver;
   8592 	  unsigned short version_index;
   8593 
   8594 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   8595 	      || isym->st_shndx == SHN_UNDEF)
   8596 	    continue;
   8597 
   8598 	  name = bfd_elf_string_from_elf_section (input,
   8599 						  hdr->sh_link,
   8600 						  isym->st_name);
   8601 	  if (strcmp (name, h->root.root.string) != 0)
   8602 	    continue;
   8603 
   8604 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   8605 
   8606 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   8607 	      && !(h->def_regular
   8608 		   && h->forced_local))
   8609 	    {
   8610 	      /* If we have a non-hidden versioned sym, then it should
   8611 		 have provided a definition for the undefined sym unless
   8612 		 it is defined in a non-shared object and forced local.
   8613 	       */
   8614 	      abort ();
   8615 	    }
   8616 
   8617 	  version_index = iver.vs_vers & VERSYM_VERSION;
   8618 	  if (version_index == 1 || version_index == 2)
   8619 	    {
   8620 	      /* This is the base or first version.  We can use it.  */
   8621 	      free (extversym);
   8622 	      free (isymbuf);
   8623 	      return TRUE;
   8624 	    }
   8625 	}
   8626 
   8627       free (extversym);
   8628       free (isymbuf);
   8629     }
   8630 
   8631   return FALSE;
   8632 }
   8633 
   8634 /* Add an external symbol to the symbol table.  This is called from
   8635    the hash table traversal routine.  When generating a shared object,
   8636    we go through the symbol table twice.  The first time we output
   8637    anything that might have been forced to local scope in a version
   8638    script.  The second time we output the symbols that are still
   8639    global symbols.  */
   8640 
   8641 static bfd_boolean
   8642 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   8643 {
   8644   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   8645   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   8646   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   8647   bfd_boolean strip;
   8648   Elf_Internal_Sym sym;
   8649   asection *input_sec;
   8650   const struct elf_backend_data *bed;
   8651   long indx;
   8652   int ret;
   8653 
   8654   if (h->root.type == bfd_link_hash_warning)
   8655     {
   8656       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8657       if (h->root.type == bfd_link_hash_new)
   8658 	return TRUE;
   8659     }
   8660 
   8661   /* Decide whether to output this symbol in this pass.  */
   8662   if (eoinfo->localsyms)
   8663     {
   8664       if (!h->forced_local)
   8665 	return TRUE;
   8666       if (eoinfo->second_pass
   8667 	  && !((h->root.type == bfd_link_hash_defined
   8668 		|| h->root.type == bfd_link_hash_defweak)
   8669 	       && h->root.u.def.section->output_section != NULL))
   8670 	return TRUE;
   8671 
   8672       if (!eoinfo->file_sym_done
   8673 	  && (eoinfo->second_pass ? eoinfo->flinfo->filesym_count == 1
   8674 				  : eoinfo->flinfo->filesym_count > 1))
   8675 	{
   8676 	  /* Output a FILE symbol so that following locals are not associated
   8677 	     with the wrong input file.  */
   8678 	  memset (&sym, 0, sizeof (sym));
   8679 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8680 	  sym.st_shndx = SHN_ABS;
   8681 	  if (!elf_link_output_sym (eoinfo->flinfo, NULL, &sym,
   8682 				    bfd_und_section_ptr, NULL))
   8683 	    return FALSE;
   8684 
   8685 	  eoinfo->file_sym_done = TRUE;
   8686 	}
   8687     }
   8688   else
   8689     {
   8690       if (h->forced_local)
   8691 	return TRUE;
   8692     }
   8693 
   8694   bed = get_elf_backend_data (flinfo->output_bfd);
   8695 
   8696   if (h->root.type == bfd_link_hash_undefined)
   8697     {
   8698       /* If we have an undefined symbol reference here then it must have
   8699 	 come from a shared library that is being linked in.  (Undefined
   8700 	 references in regular files have already been handled unless
   8701 	 they are in unreferenced sections which are removed by garbage
   8702 	 collection).  */
   8703       bfd_boolean ignore_undef = FALSE;
   8704 
   8705       /* Some symbols may be special in that the fact that they're
   8706 	 undefined can be safely ignored - let backend determine that.  */
   8707       if (bed->elf_backend_ignore_undef_symbol)
   8708 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   8709 
   8710       /* If we are reporting errors for this situation then do so now.  */
   8711       if (!ignore_undef
   8712 	  && h->ref_dynamic
   8713 	  && (!h->ref_regular || flinfo->info->gc_sections)
   8714 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   8715 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   8716 	{
   8717 	  if (!(flinfo->info->callbacks->undefined_symbol
   8718 		(flinfo->info, h->root.root.string,
   8719 		 h->ref_regular ? NULL : h->root.u.undef.abfd,
   8720 		 NULL, 0,
   8721 		 (flinfo->info->unresolved_syms_in_shared_libs
   8722 		  == RM_GENERATE_ERROR))))
   8723 	    {
   8724 	      bfd_set_error (bfd_error_bad_value);
   8725 	      eoinfo->failed = TRUE;
   8726 	      return FALSE;
   8727 	    }
   8728 	}
   8729     }
   8730 
   8731   /* We should also warn if a forced local symbol is referenced from
   8732      shared libraries.  */
   8733   if (!flinfo->info->relocatable
   8734       && flinfo->info->executable
   8735       && h->forced_local
   8736       && h->ref_dynamic
   8737       && h->def_regular
   8738       && !h->dynamic_def
   8739       && h->ref_dynamic_nonweak
   8740       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   8741     {
   8742       bfd *def_bfd;
   8743       const char *msg;
   8744       struct elf_link_hash_entry *hi = h;
   8745 
   8746       /* Check indirect symbol.  */
   8747       while (hi->root.type == bfd_link_hash_indirect)
   8748 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   8749 
   8750       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   8751 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
   8752       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   8753 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
   8754       else
   8755 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
   8756       def_bfd = flinfo->output_bfd;
   8757       if (hi->root.u.def.section != bfd_abs_section_ptr)
   8758 	def_bfd = hi->root.u.def.section->owner;
   8759       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
   8760 			     h->root.root.string);
   8761       bfd_set_error (bfd_error_bad_value);
   8762       eoinfo->failed = TRUE;
   8763       return FALSE;
   8764     }
   8765 
   8766   /* We don't want to output symbols that have never been mentioned by
   8767      a regular file, or that we have been told to strip.  However, if
   8768      h->indx is set to -2, the symbol is used by a reloc and we must
   8769      output it.  */
   8770   if (h->indx == -2)
   8771     strip = FALSE;
   8772   else if ((h->def_dynamic
   8773 	    || h->ref_dynamic
   8774 	    || h->root.type == bfd_link_hash_new)
   8775 	   && !h->def_regular
   8776 	   && !h->ref_regular)
   8777     strip = TRUE;
   8778   else if (flinfo->info->strip == strip_all)
   8779     strip = TRUE;
   8780   else if (flinfo->info->strip == strip_some
   8781 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   8782 			       h->root.root.string, FALSE, FALSE) == NULL)
   8783     strip = TRUE;
   8784   else if ((h->root.type == bfd_link_hash_defined
   8785 	    || h->root.type == bfd_link_hash_defweak)
   8786 	   && ((flinfo->info->strip_discarded
   8787 		&& discarded_section (h->root.u.def.section))
   8788 	       || (h->root.u.def.section->owner != NULL
   8789 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   8790     strip = TRUE;
   8791   else if ((h->root.type == bfd_link_hash_undefined
   8792 	    || h->root.type == bfd_link_hash_undefweak)
   8793 	   && h->root.u.undef.abfd != NULL
   8794 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   8795     strip = TRUE;
   8796   else
   8797     strip = FALSE;
   8798 
   8799   /* If we're stripping it, and it's not a dynamic symbol, there's
   8800      nothing else to do unless it is a forced local symbol or a
   8801      STT_GNU_IFUNC symbol.  */
   8802   if (strip
   8803       && h->dynindx == -1
   8804       && h->type != STT_GNU_IFUNC
   8805       && !h->forced_local)
   8806     return TRUE;
   8807 
   8808   sym.st_value = 0;
   8809   sym.st_size = h->size;
   8810   sym.st_other = h->other;
   8811   if (h->forced_local)
   8812     {
   8813       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
   8814       /* Turn off visibility on local symbol.  */
   8815       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   8816     }
   8817   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   8818   else if (h->unique_global && h->def_regular)
   8819     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
   8820   else if (h->root.type == bfd_link_hash_undefweak
   8821 	   || h->root.type == bfd_link_hash_defweak)
   8822     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
   8823   else
   8824     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
   8825   sym.st_target_internal = h->target_internal;
   8826 
   8827   switch (h->root.type)
   8828     {
   8829     default:
   8830     case bfd_link_hash_new:
   8831     case bfd_link_hash_warning:
   8832       abort ();
   8833       return FALSE;
   8834 
   8835     case bfd_link_hash_undefined:
   8836     case bfd_link_hash_undefweak:
   8837       input_sec = bfd_und_section_ptr;
   8838       sym.st_shndx = SHN_UNDEF;
   8839       break;
   8840 
   8841     case bfd_link_hash_defined:
   8842     case bfd_link_hash_defweak:
   8843       {
   8844 	input_sec = h->root.u.def.section;
   8845 	if (input_sec->output_section != NULL)
   8846 	  {
   8847 	    if (eoinfo->localsyms && flinfo->filesym_count == 1)
   8848 	      {
   8849 		bfd_boolean second_pass_sym
   8850 		  = (input_sec->owner == flinfo->output_bfd
   8851 		     || input_sec->owner == NULL
   8852 		     || (input_sec->flags & SEC_LINKER_CREATED) != 0
   8853 		     || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
   8854 
   8855 		eoinfo->need_second_pass |= second_pass_sym;
   8856 		if (eoinfo->second_pass != second_pass_sym)
   8857 		  return TRUE;
   8858 	      }
   8859 
   8860 	    sym.st_shndx =
   8861 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   8862 						 input_sec->output_section);
   8863 	    if (sym.st_shndx == SHN_BAD)
   8864 	      {
   8865 		(*_bfd_error_handler)
   8866 		  (_("%B: could not find output section %A for input section %A"),
   8867 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   8868 		bfd_set_error (bfd_error_nonrepresentable_section);
   8869 		eoinfo->failed = TRUE;
   8870 		return FALSE;
   8871 	      }
   8872 
   8873 	    /* ELF symbols in relocatable files are section relative,
   8874 	       but in nonrelocatable files they are virtual
   8875 	       addresses.  */
   8876 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   8877 	    if (!flinfo->info->relocatable)
   8878 	      {
   8879 		sym.st_value += input_sec->output_section->vma;
   8880 		if (h->type == STT_TLS)
   8881 		  {
   8882 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   8883 		    if (tls_sec != NULL)
   8884 		      sym.st_value -= tls_sec->vma;
   8885 		    else
   8886 		      {
   8887 			/* The TLS section may have been garbage collected.  */
   8888 			BFD_ASSERT (flinfo->info->gc_sections
   8889 				    && !input_sec->gc_mark);
   8890 		      }
   8891 		  }
   8892 	      }
   8893 	  }
   8894 	else
   8895 	  {
   8896 	    BFD_ASSERT (input_sec->owner == NULL
   8897 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   8898 	    sym.st_shndx = SHN_UNDEF;
   8899 	    input_sec = bfd_und_section_ptr;
   8900 	  }
   8901       }
   8902       break;
   8903 
   8904     case bfd_link_hash_common:
   8905       input_sec = h->root.u.c.p->section;
   8906       sym.st_shndx = bed->common_section_index (input_sec);
   8907       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   8908       break;
   8909 
   8910     case bfd_link_hash_indirect:
   8911       /* These symbols are created by symbol versioning.  They point
   8912 	 to the decorated version of the name.  For example, if the
   8913 	 symbol foo@@GNU_1.2 is the default, which should be used when
   8914 	 foo is used with no version, then we add an indirect symbol
   8915 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   8916 	 since the indirected symbol is already in the hash table.  */
   8917       return TRUE;
   8918     }
   8919 
   8920   /* Give the processor backend a chance to tweak the symbol value,
   8921      and also to finish up anything that needs to be done for this
   8922      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   8923      forced local syms when non-shared is due to a historical quirk.
   8924      STT_GNU_IFUNC symbol must go through PLT.  */
   8925   if ((h->type == STT_GNU_IFUNC
   8926        && h->def_regular
   8927        && !flinfo->info->relocatable)
   8928       || ((h->dynindx != -1
   8929 	   || h->forced_local)
   8930 	  && ((flinfo->info->shared
   8931 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   8932 		   || h->root.type != bfd_link_hash_undefweak))
   8933 	      || !h->forced_local)
   8934 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   8935     {
   8936       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   8937 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   8938 	{
   8939 	  eoinfo->failed = TRUE;
   8940 	  return FALSE;
   8941 	}
   8942     }
   8943 
   8944   /* If we are marking the symbol as undefined, and there are no
   8945      non-weak references to this symbol from a regular object, then
   8946      mark the symbol as weak undefined; if there are non-weak
   8947      references, mark the symbol as strong.  We can't do this earlier,
   8948      because it might not be marked as undefined until the
   8949      finish_dynamic_symbol routine gets through with it.  */
   8950   if (sym.st_shndx == SHN_UNDEF
   8951       && h->ref_regular
   8952       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   8953 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   8954     {
   8955       int bindtype;
   8956       unsigned int type = ELF_ST_TYPE (sym.st_info);
   8957 
   8958       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   8959       if (type == STT_GNU_IFUNC)
   8960 	type = STT_FUNC;
   8961 
   8962       if (h->ref_regular_nonweak)
   8963 	bindtype = STB_GLOBAL;
   8964       else
   8965 	bindtype = STB_WEAK;
   8966       sym.st_info = ELF_ST_INFO (bindtype, type);
   8967     }
   8968 
   8969   /* If this is a symbol defined in a dynamic library, don't use the
   8970      symbol size from the dynamic library.  Relinking an executable
   8971      against a new library may introduce gratuitous changes in the
   8972      executable's symbols if we keep the size.  */
   8973   if (sym.st_shndx == SHN_UNDEF
   8974       && !h->def_regular
   8975       && h->def_dynamic)
   8976     sym.st_size = 0;
   8977 
   8978   /* If a non-weak symbol with non-default visibility is not defined
   8979      locally, it is a fatal error.  */
   8980   if (!flinfo->info->relocatable
   8981       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   8982       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   8983       && h->root.type == bfd_link_hash_undefined
   8984       && !h->def_regular)
   8985     {
   8986       const char *msg;
   8987 
   8988       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   8989 	msg = _("%B: protected symbol `%s' isn't defined");
   8990       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   8991 	msg = _("%B: internal symbol `%s' isn't defined");
   8992       else
   8993 	msg = _("%B: hidden symbol `%s' isn't defined");
   8994       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
   8995       bfd_set_error (bfd_error_bad_value);
   8996       eoinfo->failed = TRUE;
   8997       return FALSE;
   8998     }
   8999 
   9000   /* If this symbol should be put in the .dynsym section, then put it
   9001      there now.  We already know the symbol index.  We also fill in
   9002      the entry in the .hash section.  */
   9003   if (flinfo->dynsym_sec != NULL
   9004       && h->dynindx != -1
   9005       && elf_hash_table (flinfo->info)->dynamic_sections_created)
   9006     {
   9007       bfd_byte *esym;
   9008 
   9009       /* Since there is no version information in the dynamic string,
   9010 	 if there is no version info in symbol version section, we will
   9011 	 have a run-time problem.  */
   9012       if (h->verinfo.verdef == NULL)
   9013 	{
   9014 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   9015 
   9016 	  if (p && p [1] != '\0')
   9017 	    {
   9018 	      (*_bfd_error_handler)
   9019 		(_("%B: No symbol version section for versioned symbol `%s'"),
   9020 		 flinfo->output_bfd, h->root.root.string);
   9021 	      eoinfo->failed = TRUE;
   9022 	      return FALSE;
   9023 	    }
   9024 	}
   9025 
   9026       sym.st_name = h->dynstr_index;
   9027       esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
   9028       if (!check_dynsym (flinfo->output_bfd, &sym))
   9029 	{
   9030 	  eoinfo->failed = TRUE;
   9031 	  return FALSE;
   9032 	}
   9033       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   9034 
   9035       if (flinfo->hash_sec != NULL)
   9036 	{
   9037 	  size_t hash_entry_size;
   9038 	  bfd_byte *bucketpos;
   9039 	  bfd_vma chain;
   9040 	  size_t bucketcount;
   9041 	  size_t bucket;
   9042 
   9043 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   9044 	  bucket = h->u.elf_hash_value % bucketcount;
   9045 
   9046 	  hash_entry_size
   9047 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   9048 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   9049 		       + (bucket + 2) * hash_entry_size);
   9050 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   9051 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   9052 		   bucketpos);
   9053 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   9054 		   ((bfd_byte *) flinfo->hash_sec->contents
   9055 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   9056 	}
   9057 
   9058       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   9059 	{
   9060 	  Elf_Internal_Versym iversym;
   9061 	  Elf_External_Versym *eversym;
   9062 
   9063 	  if (!h->def_regular)
   9064 	    {
   9065 	      if (h->verinfo.verdef == NULL)
   9066 		iversym.vs_vers = 0;
   9067 	      else
   9068 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   9069 	    }
   9070 	  else
   9071 	    {
   9072 	      if (h->verinfo.vertree == NULL)
   9073 		iversym.vs_vers = 1;
   9074 	      else
   9075 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   9076 	      if (flinfo->info->create_default_symver)
   9077 		iversym.vs_vers++;
   9078 	    }
   9079 
   9080 	  if (h->hidden)
   9081 	    iversym.vs_vers |= VERSYM_HIDDEN;
   9082 
   9083 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   9084 	  eversym += h->dynindx;
   9085 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   9086 	}
   9087     }
   9088 
   9089   /* If we're stripping it, then it was just a dynamic symbol, and
   9090      there's nothing else to do.  */
   9091   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
   9092     return TRUE;
   9093 
   9094   indx = bfd_get_symcount (flinfo->output_bfd);
   9095   ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
   9096   if (ret == 0)
   9097     {
   9098       eoinfo->failed = TRUE;
   9099       return FALSE;
   9100     }
   9101   else if (ret == 1)
   9102     h->indx = indx;
   9103   else if (h->indx == -2)
   9104     abort();
   9105 
   9106   return TRUE;
   9107 }
   9108 
   9109 /* Return TRUE if special handling is done for relocs in SEC against
   9110    symbols defined in discarded sections.  */
   9111 
   9112 static bfd_boolean
   9113 elf_section_ignore_discarded_relocs (asection *sec)
   9114 {
   9115   const struct elf_backend_data *bed;
   9116 
   9117   switch (sec->sec_info_type)
   9118     {
   9119     case SEC_INFO_TYPE_STABS:
   9120     case SEC_INFO_TYPE_EH_FRAME:
   9121       return TRUE;
   9122     default:
   9123       break;
   9124     }
   9125 
   9126   bed = get_elf_backend_data (sec->owner);
   9127   if (bed->elf_backend_ignore_discarded_relocs != NULL
   9128       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   9129     return TRUE;
   9130 
   9131   return FALSE;
   9132 }
   9133 
   9134 /* Return a mask saying how ld should treat relocations in SEC against
   9135    symbols defined in discarded sections.  If this function returns
   9136    COMPLAIN set, ld will issue a warning message.  If this function
   9137    returns PRETEND set, and the discarded section was link-once and the
   9138    same size as the kept link-once section, ld will pretend that the
   9139    symbol was actually defined in the kept section.  Otherwise ld will
   9140    zero the reloc (at least that is the intent, but some cooperation by
   9141    the target dependent code is needed, particularly for REL targets).  */
   9142 
   9143 unsigned int
   9144 _bfd_elf_default_action_discarded (asection *sec)
   9145 {
   9146   if (sec->flags & SEC_DEBUGGING)
   9147     return PRETEND;
   9148 
   9149   if (strcmp (".eh_frame", sec->name) == 0)
   9150     return 0;
   9151 
   9152   if (strcmp (".gcc_except_table", sec->name) == 0)
   9153     return 0;
   9154 
   9155   return COMPLAIN | PRETEND;
   9156 }
   9157 
   9158 /* Find a match between a section and a member of a section group.  */
   9159 
   9160 static asection *
   9161 match_group_member (asection *sec, asection *group,
   9162 		    struct bfd_link_info *info)
   9163 {
   9164   asection *first = elf_next_in_group (group);
   9165   asection *s = first;
   9166 
   9167   while (s != NULL)
   9168     {
   9169       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   9170 	return s;
   9171 
   9172       s = elf_next_in_group (s);
   9173       if (s == first)
   9174 	break;
   9175     }
   9176 
   9177   return NULL;
   9178 }
   9179 
   9180 /* Check if the kept section of a discarded section SEC can be used
   9181    to replace it.  Return the replacement if it is OK.  Otherwise return
   9182    NULL.  */
   9183 
   9184 asection *
   9185 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   9186 {
   9187   asection *kept;
   9188 
   9189   kept = sec->kept_section;
   9190   if (kept != NULL)
   9191     {
   9192       if ((kept->flags & SEC_GROUP) != 0)
   9193 	kept = match_group_member (sec, kept, info);
   9194       if (kept != NULL
   9195 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   9196 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
   9197 	kept = NULL;
   9198       sec->kept_section = kept;
   9199     }
   9200   return kept;
   9201 }
   9202 
   9203 /* Link an input file into the linker output file.  This function
   9204    handles all the sections and relocations of the input file at once.
   9205    This is so that we only have to read the local symbols once, and
   9206    don't have to keep them in memory.  */
   9207 
   9208 static bfd_boolean
   9209 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   9210 {
   9211   int (*relocate_section)
   9212     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   9213      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   9214   bfd *output_bfd;
   9215   Elf_Internal_Shdr *symtab_hdr;
   9216   size_t locsymcount;
   9217   size_t extsymoff;
   9218   Elf_Internal_Sym *isymbuf;
   9219   Elf_Internal_Sym *isym;
   9220   Elf_Internal_Sym *isymend;
   9221   long *pindex;
   9222   asection **ppsection;
   9223   asection *o;
   9224   const struct elf_backend_data *bed;
   9225   struct elf_link_hash_entry **sym_hashes;
   9226   bfd_size_type address_size;
   9227   bfd_vma r_type_mask;
   9228   int r_sym_shift;
   9229   bfd_boolean have_file_sym = FALSE;
   9230 
   9231   output_bfd = flinfo->output_bfd;
   9232   bed = get_elf_backend_data (output_bfd);
   9233   relocate_section = bed->elf_backend_relocate_section;
   9234 
   9235   /* If this is a dynamic object, we don't want to do anything here:
   9236      we don't want the local symbols, and we don't want the section
   9237      contents.  */
   9238   if ((input_bfd->flags & DYNAMIC) != 0)
   9239     return TRUE;
   9240 
   9241   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   9242   if (elf_bad_symtab (input_bfd))
   9243     {
   9244       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   9245       extsymoff = 0;
   9246     }
   9247   else
   9248     {
   9249       locsymcount = symtab_hdr->sh_info;
   9250       extsymoff = symtab_hdr->sh_info;
   9251     }
   9252 
   9253   /* Read the local symbols.  */
   9254   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   9255   if (isymbuf == NULL && locsymcount != 0)
   9256     {
   9257       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   9258 				      flinfo->internal_syms,
   9259 				      flinfo->external_syms,
   9260 				      flinfo->locsym_shndx);
   9261       if (isymbuf == NULL)
   9262 	return FALSE;
   9263     }
   9264 
   9265   /* Find local symbol sections and adjust values of symbols in
   9266      SEC_MERGE sections.  Write out those local symbols we know are
   9267      going into the output file.  */
   9268   isymend = isymbuf + locsymcount;
   9269   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   9270        isym < isymend;
   9271        isym++, pindex++, ppsection++)
   9272     {
   9273       asection *isec;
   9274       const char *name;
   9275       Elf_Internal_Sym osym;
   9276       long indx;
   9277       int ret;
   9278 
   9279       *pindex = -1;
   9280 
   9281       if (elf_bad_symtab (input_bfd))
   9282 	{
   9283 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   9284 	    {
   9285 	      *ppsection = NULL;
   9286 	      continue;
   9287 	    }
   9288 	}
   9289 
   9290       if (isym->st_shndx == SHN_UNDEF)
   9291 	isec = bfd_und_section_ptr;
   9292       else if (isym->st_shndx == SHN_ABS)
   9293 	isec = bfd_abs_section_ptr;
   9294       else if (isym->st_shndx == SHN_COMMON)
   9295 	isec = bfd_com_section_ptr;
   9296       else
   9297 	{
   9298 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   9299 	  if (isec == NULL)
   9300 	    {
   9301 	      /* Don't attempt to output symbols with st_shnx in the
   9302 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   9303 	      *ppsection = NULL;
   9304 	      continue;
   9305 	    }
   9306 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   9307 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   9308 	    isym->st_value =
   9309 	      _bfd_merged_section_offset (output_bfd, &isec,
   9310 					  elf_section_data (isec)->sec_info,
   9311 					  isym->st_value);
   9312 	}
   9313 
   9314       *ppsection = isec;
   9315 
   9316       /* Don't output the first, undefined, symbol.  */
   9317       if (ppsection == flinfo->sections)
   9318 	continue;
   9319 
   9320       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   9321 	{
   9322 	  /* We never output section symbols.  Instead, we use the
   9323 	     section symbol of the corresponding section in the output
   9324 	     file.  */
   9325 	  continue;
   9326 	}
   9327 
   9328       /* If we are stripping all symbols, we don't want to output this
   9329 	 one.  */
   9330       if (flinfo->info->strip == strip_all)
   9331 	continue;
   9332 
   9333       /* If we are discarding all local symbols, we don't want to
   9334 	 output this one.  If we are generating a relocatable output
   9335 	 file, then some of the local symbols may be required by
   9336 	 relocs; we output them below as we discover that they are
   9337 	 needed.  */
   9338       if (flinfo->info->discard == discard_all)
   9339 	continue;
   9340 
   9341       /* If this symbol is defined in a section which we are
   9342 	 discarding, we don't need to keep it.  */
   9343       if (isym->st_shndx != SHN_UNDEF
   9344 	  && isym->st_shndx < SHN_LORESERVE
   9345 	  && bfd_section_removed_from_list (output_bfd,
   9346 					    isec->output_section))
   9347 	continue;
   9348 
   9349       /* Get the name of the symbol.  */
   9350       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   9351 					      isym->st_name);
   9352       if (name == NULL)
   9353 	return FALSE;
   9354 
   9355       /* See if we are discarding symbols with this name.  */
   9356       if ((flinfo->info->strip == strip_some
   9357 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
   9358 	       == NULL))
   9359 	  || (((flinfo->info->discard == discard_sec_merge
   9360 		&& (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
   9361 	       || flinfo->info->discard == discard_l)
   9362 	      && bfd_is_local_label_name (input_bfd, name)))
   9363 	continue;
   9364 
   9365       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   9366 	{
   9367 	  have_file_sym = TRUE;
   9368 	  flinfo->filesym_count += 1;
   9369 	}
   9370       if (!have_file_sym)
   9371 	{
   9372 	  /* In the absence of debug info, bfd_find_nearest_line uses
   9373 	     FILE symbols to determine the source file for local
   9374 	     function symbols.  Provide a FILE symbol here if input
   9375 	     files lack such, so that their symbols won't be
   9376 	     associated with a previous input file.  It's not the
   9377 	     source file, but the best we can do.  */
   9378 	  have_file_sym = TRUE;
   9379 	  flinfo->filesym_count += 1;
   9380 	  memset (&osym, 0, sizeof (osym));
   9381 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9382 	  osym.st_shndx = SHN_ABS;
   9383 	  if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
   9384 				    bfd_abs_section_ptr, NULL))
   9385 	    return FALSE;
   9386 	}
   9387 
   9388       osym = *isym;
   9389 
   9390       /* Adjust the section index for the output file.  */
   9391       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   9392 							 isec->output_section);
   9393       if (osym.st_shndx == SHN_BAD)
   9394 	return FALSE;
   9395 
   9396       /* ELF symbols in relocatable files are section relative, but
   9397 	 in executable files they are virtual addresses.  Note that
   9398 	 this code assumes that all ELF sections have an associated
   9399 	 BFD section with a reasonable value for output_offset; below
   9400 	 we assume that they also have a reasonable value for
   9401 	 output_section.  Any special sections must be set up to meet
   9402 	 these requirements.  */
   9403       osym.st_value += isec->output_offset;
   9404       if (!flinfo->info->relocatable)
   9405 	{
   9406 	  osym.st_value += isec->output_section->vma;
   9407 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   9408 	    {
   9409 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   9410 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
   9411 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   9412 	    }
   9413 	}
   9414 
   9415       indx = bfd_get_symcount (output_bfd);
   9416       ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
   9417       if (ret == 0)
   9418 	return FALSE;
   9419       else if (ret == 1)
   9420 	*pindex = indx;
   9421     }
   9422 
   9423   if (bed->s->arch_size == 32)
   9424     {
   9425       r_type_mask = 0xff;
   9426       r_sym_shift = 8;
   9427       address_size = 4;
   9428     }
   9429   else
   9430     {
   9431       r_type_mask = 0xffffffff;
   9432       r_sym_shift = 32;
   9433       address_size = 8;
   9434     }
   9435 
   9436   /* Relocate the contents of each section.  */
   9437   sym_hashes = elf_sym_hashes (input_bfd);
   9438   for (o = input_bfd->sections; o != NULL; o = o->next)
   9439     {
   9440       bfd_byte *contents;
   9441 
   9442       if (! o->linker_mark)
   9443 	{
   9444 	  /* This section was omitted from the link.  */
   9445 	  continue;
   9446 	}
   9447 
   9448       if (flinfo->info->relocatable
   9449 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   9450 	{
   9451 	  /* Deal with the group signature symbol.  */
   9452 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   9453 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   9454 	  asection *osec = o->output_section;
   9455 
   9456 	  if (symndx >= locsymcount
   9457 	      || (elf_bad_symtab (input_bfd)
   9458 		  && flinfo->sections[symndx] == NULL))
   9459 	    {
   9460 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
   9461 	      while (h->root.type == bfd_link_hash_indirect
   9462 		     || h->root.type == bfd_link_hash_warning)
   9463 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9464 	      /* Arrange for symbol to be output.  */
   9465 	      h->indx = -2;
   9466 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   9467 	    }
   9468 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   9469 	    {
   9470 	      /* We'll use the output section target_index.  */
   9471 	      asection *sec = flinfo->sections[symndx]->output_section;
   9472 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   9473 	    }
   9474 	  else
   9475 	    {
   9476 	      if (flinfo->indices[symndx] == -1)
   9477 		{
   9478 		  /* Otherwise output the local symbol now.  */
   9479 		  Elf_Internal_Sym sym = isymbuf[symndx];
   9480 		  asection *sec = flinfo->sections[symndx]->output_section;
   9481 		  const char *name;
   9482 		  long indx;
   9483 		  int ret;
   9484 
   9485 		  name = bfd_elf_string_from_elf_section (input_bfd,
   9486 							  symtab_hdr->sh_link,
   9487 							  sym.st_name);
   9488 		  if (name == NULL)
   9489 		    return FALSE;
   9490 
   9491 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   9492 								    sec);
   9493 		  if (sym.st_shndx == SHN_BAD)
   9494 		    return FALSE;
   9495 
   9496 		  sym.st_value += o->output_offset;
   9497 
   9498 		  indx = bfd_get_symcount (output_bfd);
   9499 		  ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
   9500 		  if (ret == 0)
   9501 		    return FALSE;
   9502 		  else if (ret == 1)
   9503 		    flinfo->indices[symndx] = indx;
   9504 		  else
   9505 		    abort ();
   9506 		}
   9507 	      elf_section_data (osec)->this_hdr.sh_info
   9508 		= flinfo->indices[symndx];
   9509 	    }
   9510 	}
   9511 
   9512       if ((o->flags & SEC_HAS_CONTENTS) == 0
   9513 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   9514 	continue;
   9515 
   9516       if ((o->flags & SEC_LINKER_CREATED) != 0)
   9517 	{
   9518 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   9519 	     or somesuch.  */
   9520 	  continue;
   9521 	}
   9522 
   9523       /* Get the contents of the section.  They have been cached by a
   9524 	 relaxation routine.  Note that o is a section in an input
   9525 	 file, so the contents field will not have been set by any of
   9526 	 the routines which work on output files.  */
   9527       if (elf_section_data (o)->this_hdr.contents != NULL)
   9528 	{
   9529 	  contents = elf_section_data (o)->this_hdr.contents;
   9530 	  if (bed->caches_rawsize
   9531 	      && o->rawsize != 0
   9532 	      && o->rawsize < o->size)
   9533 	    {
   9534 	      memcpy (flinfo->contents, contents, o->rawsize);
   9535 	      contents = flinfo->contents;
   9536 	    }
   9537 	}
   9538       else
   9539 	{
   9540 	  contents = flinfo->contents;
   9541 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   9542 	    return FALSE;
   9543 	}
   9544 
   9545       if ((o->flags & SEC_RELOC) != 0)
   9546 	{
   9547 	  Elf_Internal_Rela *internal_relocs;
   9548 	  Elf_Internal_Rela *rel, *relend;
   9549 	  int action_discarded;
   9550 	  int ret;
   9551 
   9552 	  /* Get the swapped relocs.  */
   9553 	  internal_relocs
   9554 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
   9555 					 flinfo->internal_relocs, FALSE);
   9556 	  if (internal_relocs == NULL
   9557 	      && o->reloc_count > 0)
   9558 	    return FALSE;
   9559 
   9560 	  /* We need to reverse-copy input .ctors/.dtors sections if
   9561 	     they are placed in .init_array/.finit_array for output.  */
   9562 	  if (o->size > address_size
   9563 	      && ((strncmp (o->name, ".ctors", 6) == 0
   9564 		   && strcmp (o->output_section->name,
   9565 			      ".init_array") == 0)
   9566 		  || (strncmp (o->name, ".dtors", 6) == 0
   9567 		      && strcmp (o->output_section->name,
   9568 				 ".fini_array") == 0))
   9569 	      && (o->name[6] == 0 || o->name[6] == '.'))
   9570 	    {
   9571 	      if (o->size != o->reloc_count * address_size)
   9572 		{
   9573 		  (*_bfd_error_handler)
   9574 		    (_("error: %B: size of section %A is not "
   9575 		       "multiple of address size"),
   9576 		     input_bfd, o);
   9577 		  bfd_set_error (bfd_error_on_input);
   9578 		  return FALSE;
   9579 		}
   9580 	      o->flags |= SEC_ELF_REVERSE_COPY;
   9581 	    }
   9582 
   9583 	  action_discarded = -1;
   9584 	  if (!elf_section_ignore_discarded_relocs (o))
   9585 	    action_discarded = (*bed->action_discarded) (o);
   9586 
   9587 	  /* Run through the relocs evaluating complex reloc symbols and
   9588 	     looking for relocs against symbols from discarded sections
   9589 	     or section symbols from removed link-once sections.
   9590 	     Complain about relocs against discarded sections.  Zero
   9591 	     relocs against removed link-once sections.  */
   9592 
   9593 	  rel = internal_relocs;
   9594 	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
   9595 	  for ( ; rel < relend; rel++)
   9596 	    {
   9597 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   9598 	      unsigned int s_type;
   9599 	      asection **ps, *sec;
   9600 	      struct elf_link_hash_entry *h = NULL;
   9601 	      const char *sym_name;
   9602 
   9603 	      if (r_symndx == STN_UNDEF)
   9604 		continue;
   9605 
   9606 	      if (r_symndx >= locsymcount
   9607 		  || (elf_bad_symtab (input_bfd)
   9608 		      && flinfo->sections[r_symndx] == NULL))
   9609 		{
   9610 		  h = sym_hashes[r_symndx - extsymoff];
   9611 
   9612 		  /* Badly formatted input files can contain relocs that
   9613 		     reference non-existant symbols.  Check here so that
   9614 		     we do not seg fault.  */
   9615 		  if (h == NULL)
   9616 		    {
   9617 		      char buffer [32];
   9618 
   9619 		      sprintf_vma (buffer, rel->r_info);
   9620 		      (*_bfd_error_handler)
   9621 			(_("error: %B contains a reloc (0x%s) for section %A "
   9622 			   "that references a non-existent global symbol"),
   9623 			 input_bfd, o, buffer);
   9624 		      bfd_set_error (bfd_error_bad_value);
   9625 		      return FALSE;
   9626 		    }
   9627 
   9628 		  while (h->root.type == bfd_link_hash_indirect
   9629 			 || h->root.type == bfd_link_hash_warning)
   9630 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9631 
   9632 		  s_type = h->type;
   9633 
   9634 		  ps = NULL;
   9635 		  if (h->root.type == bfd_link_hash_defined
   9636 		      || h->root.type == bfd_link_hash_defweak)
   9637 		    ps = &h->root.u.def.section;
   9638 
   9639 		  sym_name = h->root.root.string;
   9640 		}
   9641 	      else
   9642 		{
   9643 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   9644 
   9645 		  s_type = ELF_ST_TYPE (sym->st_info);
   9646 		  ps = &flinfo->sections[r_symndx];
   9647 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   9648 					       sym, *ps);
   9649 		}
   9650 
   9651 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   9652 		  && !flinfo->info->relocatable)
   9653 		{
   9654 		  bfd_vma val;
   9655 		  bfd_vma dot = (rel->r_offset
   9656 				 + o->output_offset + o->output_section->vma);
   9657 #ifdef DEBUG
   9658 		  printf ("Encountered a complex symbol!");
   9659 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   9660 			  input_bfd->filename, o->name,
   9661 			  (long) (rel - internal_relocs));
   9662 		  printf (" symbol: idx  %8.8lx, name %s\n",
   9663 			  r_symndx, sym_name);
   9664 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   9665 			  (unsigned long) rel->r_info,
   9666 			  (unsigned long) rel->r_offset);
   9667 #endif
   9668 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   9669 				    isymbuf, locsymcount, s_type == STT_SRELC))
   9670 		    return FALSE;
   9671 
   9672 		  /* Symbol evaluated OK.  Update to absolute value.  */
   9673 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   9674 				    r_symndx, val);
   9675 		  continue;
   9676 		}
   9677 
   9678 	      if (action_discarded != -1 && ps != NULL)
   9679 		{
   9680 		  /* Complain if the definition comes from a
   9681 		     discarded section.  */
   9682 		  if ((sec = *ps) != NULL && discarded_section (sec))
   9683 		    {
   9684 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   9685 		      if (action_discarded & COMPLAIN)
   9686 			(*flinfo->info->callbacks->einfo)
   9687 			  (_("%X`%s' referenced in section `%A' of %B: "
   9688 			     "defined in discarded section `%A' of %B\n"),
   9689 			   sym_name, o, input_bfd, sec, sec->owner);
   9690 
   9691 		      /* Try to do the best we can to support buggy old
   9692 			 versions of gcc.  Pretend that the symbol is
   9693 			 really defined in the kept linkonce section.
   9694 			 FIXME: This is quite broken.  Modifying the
   9695 			 symbol here means we will be changing all later
   9696 			 uses of the symbol, not just in this section.  */
   9697 		      if (action_discarded & PRETEND)
   9698 			{
   9699 			  asection *kept;
   9700 
   9701 			  kept = _bfd_elf_check_kept_section (sec,
   9702 							      flinfo->info);
   9703 			  if (kept != NULL)
   9704 			    {
   9705 			      *ps = kept;
   9706 			      continue;
   9707 			    }
   9708 			}
   9709 		    }
   9710 		}
   9711 	    }
   9712 
   9713 	  /* Relocate the section by invoking a back end routine.
   9714 
   9715 	     The back end routine is responsible for adjusting the
   9716 	     section contents as necessary, and (if using Rela relocs
   9717 	     and generating a relocatable output file) adjusting the
   9718 	     reloc addend as necessary.
   9719 
   9720 	     The back end routine does not have to worry about setting
   9721 	     the reloc address or the reloc symbol index.
   9722 
   9723 	     The back end routine is given a pointer to the swapped in
   9724 	     internal symbols, and can access the hash table entries
   9725 	     for the external symbols via elf_sym_hashes (input_bfd).
   9726 
   9727 	     When generating relocatable output, the back end routine
   9728 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   9729 	     output symbol is going to be a section symbol
   9730 	     corresponding to the output section, which will require
   9731 	     the addend to be adjusted.  */
   9732 
   9733 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   9734 				     input_bfd, o, contents,
   9735 				     internal_relocs,
   9736 				     isymbuf,
   9737 				     flinfo->sections);
   9738 	  if (!ret)
   9739 	    return FALSE;
   9740 
   9741 	  if (ret == 2
   9742 	      || flinfo->info->relocatable
   9743 	      || flinfo->info->emitrelocations)
   9744 	    {
   9745 	      Elf_Internal_Rela *irela;
   9746 	      Elf_Internal_Rela *irelaend, *irelamid;
   9747 	      bfd_vma last_offset;
   9748 	      struct elf_link_hash_entry **rel_hash;
   9749 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   9750 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   9751 	      unsigned int next_erel;
   9752 	      bfd_boolean rela_normal;
   9753 	      struct bfd_elf_section_data *esdi, *esdo;
   9754 
   9755 	      esdi = elf_section_data (o);
   9756 	      esdo = elf_section_data (o->output_section);
   9757 	      rela_normal = FALSE;
   9758 
   9759 	      /* Adjust the reloc addresses and symbol indices.  */
   9760 
   9761 	      irela = internal_relocs;
   9762 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
   9763 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
   9764 	      /* We start processing the REL relocs, if any.  When we reach
   9765 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   9766 	      irelamid = irela;
   9767 	      if (esdi->rel.hdr != NULL)
   9768 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   9769 			     * bed->s->int_rels_per_ext_rel);
   9770 	      rel_hash_list = rel_hash;
   9771 	      rela_hash_list = NULL;
   9772 	      last_offset = o->output_offset;
   9773 	      if (!flinfo->info->relocatable)
   9774 		last_offset += o->output_section->vma;
   9775 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   9776 		{
   9777 		  unsigned long r_symndx;
   9778 		  asection *sec;
   9779 		  Elf_Internal_Sym sym;
   9780 
   9781 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   9782 		    {
   9783 		      rel_hash++;
   9784 		      next_erel = 0;
   9785 		    }
   9786 
   9787 		  if (irela == irelamid)
   9788 		    {
   9789 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
   9790 		      rela_hash_list = rel_hash;
   9791 		      rela_normal = bed->rela_normal;
   9792 		    }
   9793 
   9794 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   9795 							     flinfo->info, o,
   9796 							     irela->r_offset);
   9797 		  if (irela->r_offset >= (bfd_vma) -2)
   9798 		    {
   9799 		      /* This is a reloc for a deleted entry or somesuch.
   9800 			 Turn it into an R_*_NONE reloc, at the same
   9801 			 offset as the last reloc.  elf_eh_frame.c and
   9802 			 bfd_elf_discard_info rely on reloc offsets
   9803 			 being ordered.  */
   9804 		      irela->r_offset = last_offset;
   9805 		      irela->r_info = 0;
   9806 		      irela->r_addend = 0;
   9807 		      continue;
   9808 		    }
   9809 
   9810 		  irela->r_offset += o->output_offset;
   9811 
   9812 		  /* Relocs in an executable have to be virtual addresses.  */
   9813 		  if (!flinfo->info->relocatable)
   9814 		    irela->r_offset += o->output_section->vma;
   9815 
   9816 		  last_offset = irela->r_offset;
   9817 
   9818 		  r_symndx = irela->r_info >> r_sym_shift;
   9819 		  if (r_symndx == STN_UNDEF)
   9820 		    continue;
   9821 
   9822 		  if (r_symndx >= locsymcount
   9823 		      || (elf_bad_symtab (input_bfd)
   9824 			  && flinfo->sections[r_symndx] == NULL))
   9825 		    {
   9826 		      struct elf_link_hash_entry *rh;
   9827 		      unsigned long indx;
   9828 
   9829 		      /* This is a reloc against a global symbol.  We
   9830 			 have not yet output all the local symbols, so
   9831 			 we do not know the symbol index of any global
   9832 			 symbol.  We set the rel_hash entry for this
   9833 			 reloc to point to the global hash table entry
   9834 			 for this symbol.  The symbol index is then
   9835 			 set at the end of bfd_elf_final_link.  */
   9836 		      indx = r_symndx - extsymoff;
   9837 		      rh = elf_sym_hashes (input_bfd)[indx];
   9838 		      while (rh->root.type == bfd_link_hash_indirect
   9839 			     || rh->root.type == bfd_link_hash_warning)
   9840 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
   9841 
   9842 		      /* Setting the index to -2 tells
   9843 			 elf_link_output_extsym that this symbol is
   9844 			 used by a reloc.  */
   9845 		      BFD_ASSERT (rh->indx < 0);
   9846 		      rh->indx = -2;
   9847 
   9848 		      *rel_hash = rh;
   9849 
   9850 		      continue;
   9851 		    }
   9852 
   9853 		  /* This is a reloc against a local symbol.  */
   9854 
   9855 		  *rel_hash = NULL;
   9856 		  sym = isymbuf[r_symndx];
   9857 		  sec = flinfo->sections[r_symndx];
   9858 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   9859 		    {
   9860 		      /* I suppose the backend ought to fill in the
   9861 			 section of any STT_SECTION symbol against a
   9862 			 processor specific section.  */
   9863 		      r_symndx = STN_UNDEF;
   9864 		      if (bfd_is_abs_section (sec))
   9865 			;
   9866 		      else if (sec == NULL || sec->owner == NULL)
   9867 			{
   9868 			  bfd_set_error (bfd_error_bad_value);
   9869 			  return FALSE;
   9870 			}
   9871 		      else
   9872 			{
   9873 			  asection *osec = sec->output_section;
   9874 
   9875 			  /* If we have discarded a section, the output
   9876 			     section will be the absolute section.  In
   9877 			     case of discarded SEC_MERGE sections, use
   9878 			     the kept section.  relocate_section should
   9879 			     have already handled discarded linkonce
   9880 			     sections.  */
   9881 			  if (bfd_is_abs_section (osec)
   9882 			      && sec->kept_section != NULL
   9883 			      && sec->kept_section->output_section != NULL)
   9884 			    {
   9885 			      osec = sec->kept_section->output_section;
   9886 			      irela->r_addend -= osec->vma;
   9887 			    }
   9888 
   9889 			  if (!bfd_is_abs_section (osec))
   9890 			    {
   9891 			      r_symndx = osec->target_index;
   9892 			      if (r_symndx == STN_UNDEF)
   9893 				{
   9894 				  irela->r_addend += osec->vma;
   9895 				  osec = _bfd_nearby_section (output_bfd, osec,
   9896 							      osec->vma);
   9897 				  irela->r_addend -= osec->vma;
   9898 				  r_symndx = osec->target_index;
   9899 				}
   9900 			    }
   9901 			}
   9902 
   9903 		      /* Adjust the addend according to where the
   9904 			 section winds up in the output section.  */
   9905 		      if (rela_normal)
   9906 			irela->r_addend += sec->output_offset;
   9907 		    }
   9908 		  else
   9909 		    {
   9910 		      if (flinfo->indices[r_symndx] == -1)
   9911 			{
   9912 			  unsigned long shlink;
   9913 			  const char *name;
   9914 			  asection *osec;
   9915 			  long indx;
   9916 
   9917 			  if (flinfo->info->strip == strip_all)
   9918 			    {
   9919 			      /* You can't do ld -r -s.  */
   9920 			      bfd_set_error (bfd_error_invalid_operation);
   9921 			      return FALSE;
   9922 			    }
   9923 
   9924 			  /* This symbol was skipped earlier, but
   9925 			     since it is needed by a reloc, we
   9926 			     must output it now.  */
   9927 			  shlink = symtab_hdr->sh_link;
   9928 			  name = (bfd_elf_string_from_elf_section
   9929 				  (input_bfd, shlink, sym.st_name));
   9930 			  if (name == NULL)
   9931 			    return FALSE;
   9932 
   9933 			  osec = sec->output_section;
   9934 			  sym.st_shndx =
   9935 			    _bfd_elf_section_from_bfd_section (output_bfd,
   9936 							       osec);
   9937 			  if (sym.st_shndx == SHN_BAD)
   9938 			    return FALSE;
   9939 
   9940 			  sym.st_value += sec->output_offset;
   9941 			  if (!flinfo->info->relocatable)
   9942 			    {
   9943 			      sym.st_value += osec->vma;
   9944 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   9945 				{
   9946 				  /* STT_TLS symbols are relative to PT_TLS
   9947 				     segment base.  */
   9948 				  BFD_ASSERT (elf_hash_table (flinfo->info)
   9949 					      ->tls_sec != NULL);
   9950 				  sym.st_value -= (elf_hash_table (flinfo->info)
   9951 						   ->tls_sec->vma);
   9952 				}
   9953 			    }
   9954 
   9955 			  indx = bfd_get_symcount (output_bfd);
   9956 			  ret = elf_link_output_sym (flinfo, name, &sym, sec,
   9957 						     NULL);
   9958 			  if (ret == 0)
   9959 			    return FALSE;
   9960 			  else if (ret == 1)
   9961 			    flinfo->indices[r_symndx] = indx;
   9962 			  else
   9963 			    abort ();
   9964 			}
   9965 
   9966 		      r_symndx = flinfo->indices[r_symndx];
   9967 		    }
   9968 
   9969 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   9970 				   | (irela->r_info & r_type_mask));
   9971 		}
   9972 
   9973 	      /* Swap out the relocs.  */
   9974 	      input_rel_hdr = esdi->rel.hdr;
   9975 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   9976 		{
   9977 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   9978 						     input_rel_hdr,
   9979 						     internal_relocs,
   9980 						     rel_hash_list))
   9981 		    return FALSE;
   9982 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   9983 				      * bed->s->int_rels_per_ext_rel);
   9984 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   9985 		}
   9986 
   9987 	      input_rela_hdr = esdi->rela.hdr;
   9988 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   9989 		{
   9990 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   9991 						     input_rela_hdr,
   9992 						     internal_relocs,
   9993 						     rela_hash_list))
   9994 		    return FALSE;
   9995 		}
   9996 	    }
   9997 	}
   9998 
   9999       /* Write out the modified section contents.  */
   10000       if (bed->elf_backend_write_section
   10001 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   10002 						contents))
   10003 	{
   10004 	  /* Section written out.  */
   10005 	}
   10006       else switch (o->sec_info_type)
   10007 	{
   10008 	case SEC_INFO_TYPE_STABS:
   10009 	  if (! (_bfd_write_section_stabs
   10010 		 (output_bfd,
   10011 		  &elf_hash_table (flinfo->info)->stab_info,
   10012 		  o, &elf_section_data (o)->sec_info, contents)))
   10013 	    return FALSE;
   10014 	  break;
   10015 	case SEC_INFO_TYPE_MERGE:
   10016 	  if (! _bfd_write_merged_section (output_bfd, o,
   10017 					   elf_section_data (o)->sec_info))
   10018 	    return FALSE;
   10019 	  break;
   10020 	case SEC_INFO_TYPE_EH_FRAME:
   10021 	  {
   10022 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   10023 						   o, contents))
   10024 	      return FALSE;
   10025 	  }
   10026 	  break;
   10027 	default:
   10028 	  {
   10029 	    /* FIXME: octets_per_byte.  */
   10030 	    if (! (o->flags & SEC_EXCLUDE))
   10031 	      {
   10032 		file_ptr offset = (file_ptr) o->output_offset;
   10033 		bfd_size_type todo = o->size;
   10034 		if ((o->flags & SEC_ELF_REVERSE_COPY))
   10035 		  {
   10036 		    /* Reverse-copy input section to output.  */
   10037 		    do
   10038 		      {
   10039 			todo -= address_size;
   10040 			if (! bfd_set_section_contents (output_bfd,
   10041 							o->output_section,
   10042 							contents + todo,
   10043 							offset,
   10044 							address_size))
   10045 			  return FALSE;
   10046 			if (todo == 0)
   10047 			  break;
   10048 			offset += address_size;
   10049 		      }
   10050 		    while (1);
   10051 		  }
   10052 		else if (! bfd_set_section_contents (output_bfd,
   10053 						     o->output_section,
   10054 						     contents,
   10055 						     offset, todo))
   10056 		  return FALSE;
   10057 	      }
   10058 	  }
   10059 	  break;
   10060 	}
   10061     }
   10062 
   10063   return TRUE;
   10064 }
   10065 
   10066 /* Generate a reloc when linking an ELF file.  This is a reloc
   10067    requested by the linker, and does not come from any input file.  This
   10068    is used to build constructor and destructor tables when linking
   10069    with -Ur.  */
   10070 
   10071 static bfd_boolean
   10072 elf_reloc_link_order (bfd *output_bfd,
   10073 		      struct bfd_link_info *info,
   10074 		      asection *output_section,
   10075 		      struct bfd_link_order *link_order)
   10076 {
   10077   reloc_howto_type *howto;
   10078   long indx;
   10079   bfd_vma offset;
   10080   bfd_vma addend;
   10081   struct bfd_elf_section_reloc_data *reldata;
   10082   struct elf_link_hash_entry **rel_hash_ptr;
   10083   Elf_Internal_Shdr *rel_hdr;
   10084   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10085   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   10086   bfd_byte *erel;
   10087   unsigned int i;
   10088   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   10089 
   10090   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   10091   if (howto == NULL)
   10092     {
   10093       bfd_set_error (bfd_error_bad_value);
   10094       return FALSE;
   10095     }
   10096 
   10097   addend = link_order->u.reloc.p->addend;
   10098 
   10099   if (esdo->rel.hdr)
   10100     reldata = &esdo->rel;
   10101   else if (esdo->rela.hdr)
   10102     reldata = &esdo->rela;
   10103   else
   10104     {
   10105       reldata = NULL;
   10106       BFD_ASSERT (0);
   10107     }
   10108 
   10109   /* Figure out the symbol index.  */
   10110   rel_hash_ptr = reldata->hashes + reldata->count;
   10111   if (link_order->type == bfd_section_reloc_link_order)
   10112     {
   10113       indx = link_order->u.reloc.p->u.section->target_index;
   10114       BFD_ASSERT (indx != 0);
   10115       *rel_hash_ptr = NULL;
   10116     }
   10117   else
   10118     {
   10119       struct elf_link_hash_entry *h;
   10120 
   10121       /* Treat a reloc against a defined symbol as though it were
   10122 	 actually against the section.  */
   10123       h = ((struct elf_link_hash_entry *)
   10124 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   10125 					 link_order->u.reloc.p->u.name,
   10126 					 FALSE, FALSE, TRUE));
   10127       if (h != NULL
   10128 	  && (h->root.type == bfd_link_hash_defined
   10129 	      || h->root.type == bfd_link_hash_defweak))
   10130 	{
   10131 	  asection *section;
   10132 
   10133 	  section = h->root.u.def.section;
   10134 	  indx = section->output_section->target_index;
   10135 	  *rel_hash_ptr = NULL;
   10136 	  /* It seems that we ought to add the symbol value to the
   10137 	     addend here, but in practice it has already been added
   10138 	     because it was passed to constructor_callback.  */
   10139 	  addend += section->output_section->vma + section->output_offset;
   10140 	}
   10141       else if (h != NULL)
   10142 	{
   10143 	  /* Setting the index to -2 tells elf_link_output_extsym that
   10144 	     this symbol is used by a reloc.  */
   10145 	  h->indx = -2;
   10146 	  *rel_hash_ptr = h;
   10147 	  indx = 0;
   10148 	}
   10149       else
   10150 	{
   10151 	  if (! ((*info->callbacks->unattached_reloc)
   10152 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
   10153 	    return FALSE;
   10154 	  indx = 0;
   10155 	}
   10156     }
   10157 
   10158   /* If this is an inplace reloc, we must write the addend into the
   10159      object file.  */
   10160   if (howto->partial_inplace && addend != 0)
   10161     {
   10162       bfd_size_type size;
   10163       bfd_reloc_status_type rstat;
   10164       bfd_byte *buf;
   10165       bfd_boolean ok;
   10166       const char *sym_name;
   10167 
   10168       size = (bfd_size_type) bfd_get_reloc_size (howto);
   10169       buf = (bfd_byte *) bfd_zmalloc (size);
   10170       if (buf == NULL)
   10171 	return FALSE;
   10172       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   10173       switch (rstat)
   10174 	{
   10175 	case bfd_reloc_ok:
   10176 	  break;
   10177 
   10178 	default:
   10179 	case bfd_reloc_outofrange:
   10180 	  abort ();
   10181 
   10182 	case bfd_reloc_overflow:
   10183 	  if (link_order->type == bfd_section_reloc_link_order)
   10184 	    sym_name = bfd_section_name (output_bfd,
   10185 					 link_order->u.reloc.p->u.section);
   10186 	  else
   10187 	    sym_name = link_order->u.reloc.p->u.name;
   10188 	  if (! ((*info->callbacks->reloc_overflow)
   10189 		 (info, NULL, sym_name, howto->name, addend, NULL,
   10190 		  NULL, (bfd_vma) 0)))
   10191 	    {
   10192 	      free (buf);
   10193 	      return FALSE;
   10194 	    }
   10195 	  break;
   10196 	}
   10197       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   10198 				     link_order->offset, size);
   10199       free (buf);
   10200       if (! ok)
   10201 	return FALSE;
   10202     }
   10203 
   10204   /* The address of a reloc is relative to the section in a
   10205      relocatable file, and is a virtual address in an executable
   10206      file.  */
   10207   offset = link_order->offset;
   10208   if (! info->relocatable)
   10209     offset += output_section->vma;
   10210 
   10211   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   10212     {
   10213       irel[i].r_offset = offset;
   10214       irel[i].r_info = 0;
   10215       irel[i].r_addend = 0;
   10216     }
   10217   if (bed->s->arch_size == 32)
   10218     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   10219   else
   10220     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
   10221 
   10222   rel_hdr = reldata->hdr;
   10223   erel = rel_hdr->contents;
   10224   if (rel_hdr->sh_type == SHT_REL)
   10225     {
   10226       erel += reldata->count * bed->s->sizeof_rel;
   10227       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   10228     }
   10229   else
   10230     {
   10231       irel[0].r_addend = addend;
   10232       erel += reldata->count * bed->s->sizeof_rela;
   10233       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   10234     }
   10235 
   10236   ++reldata->count;
   10237 
   10238   return TRUE;
   10239 }
   10240 
   10241 
   10242 /* Get the output vma of the section pointed to by the sh_link field.  */
   10243 
   10244 static bfd_vma
   10245 elf_get_linked_section_vma (struct bfd_link_order *p)
   10246 {
   10247   Elf_Internal_Shdr **elf_shdrp;
   10248   asection *s;
   10249   int elfsec;
   10250 
   10251   s = p->u.indirect.section;
   10252   elf_shdrp = elf_elfsections (s->owner);
   10253   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
   10254   elfsec = elf_shdrp[elfsec]->sh_link;
   10255   /* PR 290:
   10256      The Intel C compiler generates SHT_IA_64_UNWIND with
   10257      SHF_LINK_ORDER.  But it doesn't set the sh_link or
   10258      sh_info fields.  Hence we could get the situation
   10259      where elfsec is 0.  */
   10260   if (elfsec == 0)
   10261     {
   10262       const struct elf_backend_data *bed
   10263 	= get_elf_backend_data (s->owner);
   10264       if (bed->link_order_error_handler)
   10265 	bed->link_order_error_handler
   10266 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
   10267       return 0;
   10268     }
   10269   else
   10270     {
   10271       s = elf_shdrp[elfsec]->bfd_section;
   10272       return s->output_section->vma + s->output_offset;
   10273     }
   10274 }
   10275 
   10276 
   10277 /* Compare two sections based on the locations of the sections they are
   10278    linked to.  Used by elf_fixup_link_order.  */
   10279 
   10280 static int
   10281 compare_link_order (const void * a, const void * b)
   10282 {
   10283   bfd_vma apos;
   10284   bfd_vma bpos;
   10285 
   10286   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
   10287   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
   10288   if (apos < bpos)
   10289     return -1;
   10290   return apos > bpos;
   10291 }
   10292 
   10293 
   10294 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
   10295    order as their linked sections.  Returns false if this could not be done
   10296    because an output section includes both ordered and unordered
   10297    sections.  Ideally we'd do this in the linker proper.  */
   10298 
   10299 static bfd_boolean
   10300 elf_fixup_link_order (bfd *abfd, asection *o)
   10301 {
   10302   int seen_linkorder;
   10303   int seen_other;
   10304   int n;
   10305   struct bfd_link_order *p;
   10306   bfd *sub;
   10307   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10308   unsigned elfsec;
   10309   struct bfd_link_order **sections;
   10310   asection *s, *other_sec, *linkorder_sec;
   10311   bfd_vma offset;
   10312 
   10313   other_sec = NULL;
   10314   linkorder_sec = NULL;
   10315   seen_other = 0;
   10316   seen_linkorder = 0;
   10317   for (p = o->map_head.link_order; p != NULL; p = p->next)
   10318     {
   10319       if (p->type == bfd_indirect_link_order)
   10320 	{
   10321 	  s = p->u.indirect.section;
   10322 	  sub = s->owner;
   10323 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   10324 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
   10325 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
   10326 	      && elfsec < elf_numsections (sub)
   10327 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
   10328 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
   10329 	    {
   10330 	      seen_linkorder++;
   10331 	      linkorder_sec = s;
   10332 	    }
   10333 	  else
   10334 	    {
   10335 	      seen_other++;
   10336 	      other_sec = s;
   10337 	    }
   10338 	}
   10339       else
   10340 	seen_other++;
   10341 
   10342       if (seen_other && seen_linkorder)
   10343 	{
   10344 	  if (other_sec && linkorder_sec)
   10345 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
   10346 				   o, linkorder_sec,
   10347 				   linkorder_sec->owner, other_sec,
   10348 				   other_sec->owner);
   10349 	  else
   10350 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
   10351 				   o);
   10352 	  bfd_set_error (bfd_error_bad_value);
   10353 	  return FALSE;
   10354 	}
   10355     }
   10356 
   10357   if (!seen_linkorder)
   10358     return TRUE;
   10359 
   10360   sections = (struct bfd_link_order **)
   10361     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
   10362   if (sections == NULL)
   10363     return FALSE;
   10364   seen_linkorder = 0;
   10365 
   10366   for (p = o->map_head.link_order; p != NULL; p = p->next)
   10367     {
   10368       sections[seen_linkorder++] = p;
   10369     }
   10370   /* Sort the input sections in the order of their linked section.  */
   10371   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
   10372 	 compare_link_order);
   10373 
   10374   /* Change the offsets of the sections.  */
   10375   offset = 0;
   10376   for (n = 0; n < seen_linkorder; n++)
   10377     {
   10378       s = sections[n]->u.indirect.section;
   10379       offset &= ~(bfd_vma) 0 << s->alignment_power;
   10380       s->output_offset = offset;
   10381       sections[n]->offset = offset;
   10382       /* FIXME: octets_per_byte.  */
   10383       offset += sections[n]->size;
   10384     }
   10385 
   10386   free (sections);
   10387   return TRUE;
   10388 }
   10389 
   10390 static void
   10391 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   10392 {
   10393   asection *o;
   10394 
   10395   if (flinfo->symstrtab != NULL)
   10396     _bfd_stringtab_free (flinfo->symstrtab);
   10397   if (flinfo->contents != NULL)
   10398     free (flinfo->contents);
   10399   if (flinfo->external_relocs != NULL)
   10400     free (flinfo->external_relocs);
   10401   if (flinfo->internal_relocs != NULL)
   10402     free (flinfo->internal_relocs);
   10403   if (flinfo->external_syms != NULL)
   10404     free (flinfo->external_syms);
   10405   if (flinfo->locsym_shndx != NULL)
   10406     free (flinfo->locsym_shndx);
   10407   if (flinfo->internal_syms != NULL)
   10408     free (flinfo->internal_syms);
   10409   if (flinfo->indices != NULL)
   10410     free (flinfo->indices);
   10411   if (flinfo->sections != NULL)
   10412     free (flinfo->sections);
   10413   if (flinfo->symbuf != NULL)
   10414     free (flinfo->symbuf);
   10415   if (flinfo->symshndxbuf != NULL)
   10416     free (flinfo->symshndxbuf);
   10417   for (o = obfd->sections; o != NULL; o = o->next)
   10418     {
   10419       struct bfd_elf_section_data *esdo = elf_section_data (o);
   10420       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
   10421 	free (esdo->rel.hashes);
   10422       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
   10423 	free (esdo->rela.hashes);
   10424     }
   10425 }
   10426 
   10427 /* Do the final step of an ELF link.  */
   10428 
   10429 bfd_boolean
   10430 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   10431 {
   10432   bfd_boolean dynamic;
   10433   bfd_boolean emit_relocs;
   10434   bfd *dynobj;
   10435   struct elf_final_link_info flinfo;
   10436   asection *o;
   10437   struct bfd_link_order *p;
   10438   bfd *sub;
   10439   bfd_size_type max_contents_size;
   10440   bfd_size_type max_external_reloc_size;
   10441   bfd_size_type max_internal_reloc_count;
   10442   bfd_size_type max_sym_count;
   10443   bfd_size_type max_sym_shndx_count;
   10444   file_ptr off;
   10445   Elf_Internal_Sym elfsym;
   10446   unsigned int i;
   10447   Elf_Internal_Shdr *symtab_hdr;
   10448   Elf_Internal_Shdr *symtab_shndx_hdr;
   10449   Elf_Internal_Shdr *symstrtab_hdr;
   10450   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10451   struct elf_outext_info eoinfo;
   10452   bfd_boolean merged;
   10453   size_t relativecount = 0;
   10454   asection *reldyn = 0;
   10455   bfd_size_type amt;
   10456   asection *attr_section = NULL;
   10457   bfd_vma attr_size = 0;
   10458   const char *std_attrs_section;
   10459 
   10460   if (! is_elf_hash_table (info->hash))
   10461     return FALSE;
   10462 
   10463   if (info->shared)
   10464     abfd->flags |= DYNAMIC;
   10465 
   10466   dynamic = elf_hash_table (info)->dynamic_sections_created;
   10467   dynobj = elf_hash_table (info)->dynobj;
   10468 
   10469   emit_relocs = (info->relocatable
   10470 		 || info->emitrelocations);
   10471 
   10472   flinfo.info = info;
   10473   flinfo.output_bfd = abfd;
   10474   flinfo.symstrtab = _bfd_elf_stringtab_init ();
   10475   if (flinfo.symstrtab == NULL)
   10476     return FALSE;
   10477 
   10478   if (! dynamic)
   10479     {
   10480       flinfo.dynsym_sec = NULL;
   10481       flinfo.hash_sec = NULL;
   10482       flinfo.symver_sec = NULL;
   10483     }
   10484   else
   10485     {
   10486       flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
   10487       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   10488       /* Note that dynsym_sec can be NULL (on VMS).  */
   10489       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   10490       /* Note that it is OK if symver_sec is NULL.  */
   10491     }
   10492 
   10493   flinfo.contents = NULL;
   10494   flinfo.external_relocs = NULL;
   10495   flinfo.internal_relocs = NULL;
   10496   flinfo.external_syms = NULL;
   10497   flinfo.locsym_shndx = NULL;
   10498   flinfo.internal_syms = NULL;
   10499   flinfo.indices = NULL;
   10500   flinfo.sections = NULL;
   10501   flinfo.symbuf = NULL;
   10502   flinfo.symshndxbuf = NULL;
   10503   flinfo.symbuf_count = 0;
   10504   flinfo.shndxbuf_size = 0;
   10505   flinfo.filesym_count = 0;
   10506 
   10507   /* The object attributes have been merged.  Remove the input
   10508      sections from the link, and set the contents of the output
   10509      secton.  */
   10510   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   10511   for (o = abfd->sections; o != NULL; o = o->next)
   10512     {
   10513       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
   10514 	  || strcmp (o->name, ".gnu.attributes") == 0)
   10515 	{
   10516 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   10517 	    {
   10518 	      asection *input_section;
   10519 
   10520 	      if (p->type != bfd_indirect_link_order)
   10521 		continue;
   10522 	      input_section = p->u.indirect.section;
   10523 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   10524 		 elf_link_input_bfd ignores this section.  */
   10525 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   10526 	    }
   10527 
   10528 	  attr_size = bfd_elf_obj_attr_size (abfd);
   10529 	  if (attr_size)
   10530 	    {
   10531 	      bfd_set_section_size (abfd, o, attr_size);
   10532 	      attr_section = o;
   10533 	      /* Skip this section later on.  */
   10534 	      o->map_head.link_order = NULL;
   10535 	    }
   10536 	  else
   10537 	    o->flags |= SEC_EXCLUDE;
   10538 	}
   10539     }
   10540 
   10541   /* Count up the number of relocations we will output for each output
   10542      section, so that we know the sizes of the reloc sections.  We
   10543      also figure out some maximum sizes.  */
   10544   max_contents_size = 0;
   10545   max_external_reloc_size = 0;
   10546   max_internal_reloc_count = 0;
   10547   max_sym_count = 0;
   10548   max_sym_shndx_count = 0;
   10549   merged = FALSE;
   10550   for (o = abfd->sections; o != NULL; o = o->next)
   10551     {
   10552       struct bfd_elf_section_data *esdo = elf_section_data (o);
   10553       o->reloc_count = 0;
   10554 
   10555       for (p = o->map_head.link_order; p != NULL; p = p->next)
   10556 	{
   10557 	  unsigned int reloc_count = 0;
   10558 	  struct bfd_elf_section_data *esdi = NULL;
   10559 
   10560 	  if (p->type == bfd_section_reloc_link_order
   10561 	      || p->type == bfd_symbol_reloc_link_order)
   10562 	    reloc_count = 1;
   10563 	  else if (p->type == bfd_indirect_link_order)
   10564 	    {
   10565 	      asection *sec;
   10566 
   10567 	      sec = p->u.indirect.section;
   10568 	      esdi = elf_section_data (sec);
   10569 
   10570 	      /* Mark all sections which are to be included in the
   10571 		 link.  This will normally be every section.  We need
   10572 		 to do this so that we can identify any sections which
   10573 		 the linker has decided to not include.  */
   10574 	      sec->linker_mark = TRUE;
   10575 
   10576 	      if (sec->flags & SEC_MERGE)
   10577 		merged = TRUE;
   10578 
   10579 	      if (esdo->this_hdr.sh_type == SHT_REL
   10580 		  || esdo->this_hdr.sh_type == SHT_RELA)
   10581 		/* Some backends use reloc_count in relocation sections
   10582 		   to count particular types of relocs.  Of course,
   10583 		   reloc sections themselves can't have relocations.  */
   10584 		reloc_count = 0;
   10585 	      else if (info->relocatable || info->emitrelocations)
   10586 		reloc_count = sec->reloc_count;
   10587 	      else if (bed->elf_backend_count_relocs)
   10588 		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   10589 
   10590 	      if (sec->rawsize > max_contents_size)
   10591 		max_contents_size = sec->rawsize;
   10592 	      if (sec->size > max_contents_size)
   10593 		max_contents_size = sec->size;
   10594 
   10595 	      /* We are interested in just local symbols, not all
   10596 		 symbols.  */
   10597 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   10598 		  && (sec->owner->flags & DYNAMIC) == 0)
   10599 		{
   10600 		  size_t sym_count;
   10601 
   10602 		  if (elf_bad_symtab (sec->owner))
   10603 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   10604 				 / bed->s->sizeof_sym);
   10605 		  else
   10606 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   10607 
   10608 		  if (sym_count > max_sym_count)
   10609 		    max_sym_count = sym_count;
   10610 
   10611 		  if (sym_count > max_sym_shndx_count
   10612 		      && elf_symtab_shndx (sec->owner) != 0)
   10613 		    max_sym_shndx_count = sym_count;
   10614 
   10615 		  if ((sec->flags & SEC_RELOC) != 0)
   10616 		    {
   10617 		      size_t ext_size = 0;
   10618 
   10619 		      if (esdi->rel.hdr != NULL)
   10620 			ext_size = esdi->rel.hdr->sh_size;
   10621 		      if (esdi->rela.hdr != NULL)
   10622 			ext_size += esdi->rela.hdr->sh_size;
   10623 
   10624 		      if (ext_size > max_external_reloc_size)
   10625 			max_external_reloc_size = ext_size;
   10626 		      if (sec->reloc_count > max_internal_reloc_count)
   10627 			max_internal_reloc_count = sec->reloc_count;
   10628 		    }
   10629 		}
   10630 	    }
   10631 
   10632 	  if (reloc_count == 0)
   10633 	    continue;
   10634 
   10635 	  o->reloc_count += reloc_count;
   10636 
   10637 	  if (p->type == bfd_indirect_link_order
   10638 	      && (info->relocatable || info->emitrelocations))
   10639 	    {
   10640 	      if (esdi->rel.hdr)
   10641 		esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   10642 	      if (esdi->rela.hdr)
   10643 		esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   10644 	    }
   10645 	  else
   10646 	    {
   10647 	      if (o->use_rela_p)
   10648 		esdo->rela.count += reloc_count;
   10649 	      else
   10650 		esdo->rel.count += reloc_count;
   10651 	    }
   10652 	}
   10653 
   10654       if (o->reloc_count > 0)
   10655 	o->flags |= SEC_RELOC;
   10656       else
   10657 	{
   10658 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   10659 	     set it (this is probably a bug) and if it is set
   10660 	     assign_section_numbers will create a reloc section.  */
   10661 	  o->flags &=~ SEC_RELOC;
   10662 	}
   10663 
   10664       /* If the SEC_ALLOC flag is not set, force the section VMA to
   10665 	 zero.  This is done in elf_fake_sections as well, but forcing
   10666 	 the VMA to 0 here will ensure that relocs against these
   10667 	 sections are handled correctly.  */
   10668       if ((o->flags & SEC_ALLOC) == 0
   10669 	  && ! o->user_set_vma)
   10670 	o->vma = 0;
   10671     }
   10672 
   10673   if (! info->relocatable && merged)
   10674     elf_link_hash_traverse (elf_hash_table (info),
   10675 			    _bfd_elf_link_sec_merge_syms, abfd);
   10676 
   10677   /* Figure out the file positions for everything but the symbol table
   10678      and the relocs.  We set symcount to force assign_section_numbers
   10679      to create a symbol table.  */
   10680   bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
   10681   BFD_ASSERT (! abfd->output_has_begun);
   10682   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   10683     goto error_return;
   10684 
   10685   /* Set sizes, and assign file positions for reloc sections.  */
   10686   for (o = abfd->sections; o != NULL; o = o->next)
   10687     {
   10688       struct bfd_elf_section_data *esdo = elf_section_data (o);
   10689       if ((o->flags & SEC_RELOC) != 0)
   10690 	{
   10691 	  if (esdo->rel.hdr
   10692 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   10693 	    goto error_return;
   10694 
   10695 	  if (esdo->rela.hdr
   10696 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   10697 	    goto error_return;
   10698 	}
   10699 
   10700       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   10701 	 to count upwards while actually outputting the relocations.  */
   10702       esdo->rel.count = 0;
   10703       esdo->rela.count = 0;
   10704     }
   10705 
   10706   /* We have now assigned file positions for all the sections except
   10707      .symtab, .strtab, and non-loaded reloc sections.  We start the
   10708      .symtab section at the current file position, and write directly
   10709      to it.  We build the .strtab section in memory.  */
   10710   bfd_get_symcount (abfd) = 0;
   10711   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   10712   /* sh_name is set in prep_headers.  */
   10713   symtab_hdr->sh_type = SHT_SYMTAB;
   10714   /* sh_flags, sh_addr and sh_size all start off zero.  */
   10715   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   10716   /* sh_link is set in assign_section_numbers.  */
   10717   /* sh_info is set below.  */
   10718   /* sh_offset is set just below.  */
   10719   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   10720 
   10721   off = elf_next_file_pos (abfd);
   10722   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
   10723 
   10724   /* Note that at this point elf_next_file_pos (abfd) is
   10725      incorrect.  We do not yet know the size of the .symtab section.
   10726      We correct next_file_pos below, after we do know the size.  */
   10727 
   10728   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
   10729      continuously seeking to the right position in the file.  */
   10730   if (! info->keep_memory || max_sym_count < 20)
   10731     flinfo.symbuf_size = 20;
   10732   else
   10733     flinfo.symbuf_size = max_sym_count;
   10734   amt = flinfo.symbuf_size;
   10735   amt *= bed->s->sizeof_sym;
   10736   flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
   10737   if (flinfo.symbuf == NULL)
   10738     goto error_return;
   10739   if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   10740     {
   10741       /* Wild guess at number of output symbols.  realloc'd as needed.  */
   10742       amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
   10743       flinfo.shndxbuf_size = amt;
   10744       amt *= sizeof (Elf_External_Sym_Shndx);
   10745       flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   10746       if (flinfo.symshndxbuf == NULL)
   10747 	goto error_return;
   10748     }
   10749 
   10750   /* Start writing out the symbol table.  The first symbol is always a
   10751      dummy symbol.  */
   10752   if (info->strip != strip_all
   10753       || emit_relocs)
   10754     {
   10755       elfsym.st_value = 0;
   10756       elfsym.st_size = 0;
   10757       elfsym.st_info = 0;
   10758       elfsym.st_other = 0;
   10759       elfsym.st_shndx = SHN_UNDEF;
   10760       elfsym.st_target_internal = 0;
   10761       if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
   10762 			       NULL) != 1)
   10763 	goto error_return;
   10764     }
   10765 
   10766   /* Output a symbol for each section.  We output these even if we are
   10767      discarding local symbols, since they are used for relocs.  These
   10768      symbols have no names.  We store the index of each one in the
   10769      index field of the section, so that we can find it again when
   10770      outputting relocs.  */
   10771   if (info->strip != strip_all
   10772       || emit_relocs)
   10773     {
   10774       elfsym.st_size = 0;
   10775       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   10776       elfsym.st_other = 0;
   10777       elfsym.st_value = 0;
   10778       elfsym.st_target_internal = 0;
   10779       for (i = 1; i < elf_numsections (abfd); i++)
   10780 	{
   10781 	  o = bfd_section_from_elf_index (abfd, i);
   10782 	  if (o != NULL)
   10783 	    {
   10784 	      o->target_index = bfd_get_symcount (abfd);
   10785 	      elfsym.st_shndx = i;
   10786 	      if (!info->relocatable)
   10787 		elfsym.st_value = o->vma;
   10788 	      if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
   10789 		goto error_return;
   10790 	    }
   10791 	}
   10792     }
   10793 
   10794   /* Allocate some memory to hold information read in from the input
   10795      files.  */
   10796   if (max_contents_size != 0)
   10797     {
   10798       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   10799       if (flinfo.contents == NULL)
   10800 	goto error_return;
   10801     }
   10802 
   10803   if (max_external_reloc_size != 0)
   10804     {
   10805       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   10806       if (flinfo.external_relocs == NULL)
   10807 	goto error_return;
   10808     }
   10809 
   10810   if (max_internal_reloc_count != 0)
   10811     {
   10812       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
   10813       amt *= sizeof (Elf_Internal_Rela);
   10814       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   10815       if (flinfo.internal_relocs == NULL)
   10816 	goto error_return;
   10817     }
   10818 
   10819   if (max_sym_count != 0)
   10820     {
   10821       amt = max_sym_count * bed->s->sizeof_sym;
   10822       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   10823       if (flinfo.external_syms == NULL)
   10824 	goto error_return;
   10825 
   10826       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   10827       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   10828       if (flinfo.internal_syms == NULL)
   10829 	goto error_return;
   10830 
   10831       amt = max_sym_count * sizeof (long);
   10832       flinfo.indices = (long int *) bfd_malloc (amt);
   10833       if (flinfo.indices == NULL)
   10834 	goto error_return;
   10835 
   10836       amt = max_sym_count * sizeof (asection *);
   10837       flinfo.sections = (asection **) bfd_malloc (amt);
   10838       if (flinfo.sections == NULL)
   10839 	goto error_return;
   10840     }
   10841 
   10842   if (max_sym_shndx_count != 0)
   10843     {
   10844       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   10845       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   10846       if (flinfo.locsym_shndx == NULL)
   10847 	goto error_return;
   10848     }
   10849 
   10850   if (elf_hash_table (info)->tls_sec)
   10851     {
   10852       bfd_vma base, end = 0;
   10853       asection *sec;
   10854 
   10855       for (sec = elf_hash_table (info)->tls_sec;
   10856 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   10857 	   sec = sec->next)
   10858 	{
   10859 	  bfd_size_type size = sec->size;
   10860 
   10861 	  if (size == 0
   10862 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   10863 	    {
   10864 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   10865 
   10866 	      if (ord != NULL)
   10867 		size = ord->offset + ord->size;
   10868 	    }
   10869 	  end = sec->vma + size;
   10870 	}
   10871       base = elf_hash_table (info)->tls_sec->vma;
   10872       /* Only align end of TLS section if static TLS doesn't have special
   10873 	 alignment requirements.  */
   10874       if (bed->static_tls_alignment == 1)
   10875 	end = align_power (end,
   10876 			   elf_hash_table (info)->tls_sec->alignment_power);
   10877       elf_hash_table (info)->tls_size = end - base;
   10878     }
   10879 
   10880   /* Reorder SHF_LINK_ORDER sections.  */
   10881   for (o = abfd->sections; o != NULL; o = o->next)
   10882     {
   10883       if (!elf_fixup_link_order (abfd, o))
   10884 	return FALSE;
   10885     }
   10886 
   10887   /* Since ELF permits relocations to be against local symbols, we
   10888      must have the local symbols available when we do the relocations.
   10889      Since we would rather only read the local symbols once, and we
   10890      would rather not keep them in memory, we handle all the
   10891      relocations for a single input file at the same time.
   10892 
   10893      Unfortunately, there is no way to know the total number of local
   10894      symbols until we have seen all of them, and the local symbol
   10895      indices precede the global symbol indices.  This means that when
   10896      we are generating relocatable output, and we see a reloc against
   10897      a global symbol, we can not know the symbol index until we have
   10898      finished examining all the local symbols to see which ones we are
   10899      going to output.  To deal with this, we keep the relocations in
   10900      memory, and don't output them until the end of the link.  This is
   10901      an unfortunate waste of memory, but I don't see a good way around
   10902      it.  Fortunately, it only happens when performing a relocatable
   10903      link, which is not the common case.  FIXME: If keep_memory is set
   10904      we could write the relocs out and then read them again; I don't
   10905      know how bad the memory loss will be.  */
   10906 
   10907   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   10908     sub->output_has_begun = FALSE;
   10909   for (o = abfd->sections; o != NULL; o = o->next)
   10910     {
   10911       for (p = o->map_head.link_order; p != NULL; p = p->next)
   10912 	{
   10913 	  if (p->type == bfd_indirect_link_order
   10914 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   10915 		  == bfd_target_elf_flavour)
   10916 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   10917 	    {
   10918 	      if (! sub->output_has_begun)
   10919 		{
   10920 		  if (! elf_link_input_bfd (&flinfo, sub))
   10921 		    goto error_return;
   10922 		  sub->output_has_begun = TRUE;
   10923 		}
   10924 	    }
   10925 	  else if (p->type == bfd_section_reloc_link_order
   10926 		   || p->type == bfd_symbol_reloc_link_order)
   10927 	    {
   10928 	      if (! elf_reloc_link_order (abfd, info, o, p))
   10929 		goto error_return;
   10930 	    }
   10931 	  else
   10932 	    {
   10933 	      if (! _bfd_default_link_order (abfd, info, o, p))
   10934 		{
   10935 		  if (p->type == bfd_indirect_link_order
   10936 		      && (bfd_get_flavour (sub)
   10937 			  == bfd_target_elf_flavour)
   10938 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   10939 			  != bed->s->elfclass))
   10940 		    {
   10941 		      const char *iclass, *oclass;
   10942 
   10943 		      if (bed->s->elfclass == ELFCLASS64)
   10944 			{
   10945 			  iclass = "ELFCLASS32";
   10946 			  oclass = "ELFCLASS64";
   10947 			}
   10948 		      else
   10949 			{
   10950 			  iclass = "ELFCLASS64";
   10951 			  oclass = "ELFCLASS32";
   10952 			}
   10953 
   10954 		      bfd_set_error (bfd_error_wrong_format);
   10955 		      (*_bfd_error_handler)
   10956 			(_("%B: file class %s incompatible with %s"),
   10957 			 sub, iclass, oclass);
   10958 		    }
   10959 
   10960 		  goto error_return;
   10961 		}
   10962 	    }
   10963 	}
   10964     }
   10965 
   10966   /* Free symbol buffer if needed.  */
   10967   if (!info->reduce_memory_overheads)
   10968     {
   10969       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   10970 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   10971 	    && elf_tdata (sub)->symbuf)
   10972 	  {
   10973 	    free (elf_tdata (sub)->symbuf);
   10974 	    elf_tdata (sub)->symbuf = NULL;
   10975 	  }
   10976     }
   10977 
   10978   /* Output any global symbols that got converted to local in a
   10979      version script or due to symbol visibility.  We do this in a
   10980      separate step since ELF requires all local symbols to appear
   10981      prior to any global symbols.  FIXME: We should only do this if
   10982      some global symbols were, in fact, converted to become local.
   10983      FIXME: Will this work correctly with the Irix 5 linker?  */
   10984   eoinfo.failed = FALSE;
   10985   eoinfo.flinfo = &flinfo;
   10986   eoinfo.localsyms = TRUE;
   10987   eoinfo.need_second_pass = FALSE;
   10988   eoinfo.second_pass = FALSE;
   10989   eoinfo.file_sym_done = FALSE;
   10990   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   10991   if (eoinfo.failed)
   10992     return FALSE;
   10993 
   10994   if (eoinfo.need_second_pass)
   10995     {
   10996       eoinfo.second_pass = TRUE;
   10997       bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   10998       if (eoinfo.failed)
   10999 	return FALSE;
   11000     }
   11001 
   11002   /* If backend needs to output some local symbols not present in the hash
   11003      table, do it now.  */
   11004   if (bed->elf_backend_output_arch_local_syms)
   11005     {
   11006       typedef int (*out_sym_func)
   11007 	(void *, const char *, Elf_Internal_Sym *, asection *,
   11008 	 struct elf_link_hash_entry *);
   11009 
   11010       if (! ((*bed->elf_backend_output_arch_local_syms)
   11011 	     (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
   11012 	return FALSE;
   11013     }
   11014 
   11015   /* That wrote out all the local symbols.  Finish up the symbol table
   11016      with the global symbols. Even if we want to strip everything we
   11017      can, we still need to deal with those global symbols that got
   11018      converted to local in a version script.  */
   11019 
   11020   /* The sh_info field records the index of the first non local symbol.  */
   11021   symtab_hdr->sh_info = bfd_get_symcount (abfd);
   11022 
   11023   if (dynamic
   11024       && flinfo.dynsym_sec != NULL
   11025       && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
   11026     {
   11027       Elf_Internal_Sym sym;
   11028       bfd_byte *dynsym = flinfo.dynsym_sec->contents;
   11029       long last_local = 0;
   11030 
   11031       /* Write out the section symbols for the output sections.  */
   11032       if (info->shared || elf_hash_table (info)->is_relocatable_executable)
   11033 	{
   11034 	  asection *s;
   11035 
   11036 	  sym.st_size = 0;
   11037 	  sym.st_name = 0;
   11038 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   11039 	  sym.st_other = 0;
   11040 	  sym.st_target_internal = 0;
   11041 
   11042 	  for (s = abfd->sections; s != NULL; s = s->next)
   11043 	    {
   11044 	      int indx;
   11045 	      bfd_byte *dest;
   11046 	      long dynindx;
   11047 
   11048 	      dynindx = elf_section_data (s)->dynindx;
   11049 	      if (dynindx <= 0)
   11050 		continue;
   11051 	      indx = elf_section_data (s)->this_idx;
   11052 	      BFD_ASSERT (indx > 0);
   11053 	      sym.st_shndx = indx;
   11054 	      if (! check_dynsym (abfd, &sym))
   11055 		return FALSE;
   11056 	      sym.st_value = s->vma;
   11057 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   11058 	      if (last_local < dynindx)
   11059 		last_local = dynindx;
   11060 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   11061 	    }
   11062 	}
   11063 
   11064       /* Write out the local dynsyms.  */
   11065       if (elf_hash_table (info)->dynlocal)
   11066 	{
   11067 	  struct elf_link_local_dynamic_entry *e;
   11068 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
   11069 	    {
   11070 	      asection *s;
   11071 	      bfd_byte *dest;
   11072 
   11073 	      /* Copy the internal symbol and turn off visibility.
   11074 		 Note that we saved a word of storage and overwrote
   11075 		 the original st_name with the dynstr_index.  */
   11076 	      sym = e->isym;
   11077 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   11078 
   11079 	      s = bfd_section_from_elf_index (e->input_bfd,
   11080 					      e->isym.st_shndx);
   11081 	      if (s != NULL)
   11082 		{
   11083 		  sym.st_shndx =
   11084 		    elf_section_data (s->output_section)->this_idx;
   11085 		  if (! check_dynsym (abfd, &sym))
   11086 		    return FALSE;
   11087 		  sym.st_value = (s->output_section->vma
   11088 				  + s->output_offset
   11089 				  + e->isym.st_value);
   11090 		}
   11091 
   11092 	      if (last_local < e->dynindx)
   11093 		last_local = e->dynindx;
   11094 
   11095 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   11096 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   11097 	    }
   11098 	}
   11099 
   11100       elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
   11101 	last_local + 1;
   11102     }
   11103 
   11104   /* We get the global symbols from the hash table.  */
   11105   eoinfo.failed = FALSE;
   11106   eoinfo.localsyms = FALSE;
   11107   eoinfo.flinfo = &flinfo;
   11108   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   11109   if (eoinfo.failed)
   11110     return FALSE;
   11111 
   11112   /* If backend needs to output some symbols not present in the hash
   11113      table, do it now.  */
   11114   if (bed->elf_backend_output_arch_syms)
   11115     {
   11116       typedef int (*out_sym_func)
   11117 	(void *, const char *, Elf_Internal_Sym *, asection *,
   11118 	 struct elf_link_hash_entry *);
   11119 
   11120       if (! ((*bed->elf_backend_output_arch_syms)
   11121 	     (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
   11122 	return FALSE;
   11123     }
   11124 
   11125   /* Flush all symbols to the file.  */
   11126   if (! elf_link_flush_output_syms (&flinfo, bed))
   11127     return FALSE;
   11128 
   11129   /* Now we know the size of the symtab section.  */
   11130   off += symtab_hdr->sh_size;
   11131 
   11132   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
   11133   if (symtab_shndx_hdr->sh_name != 0)
   11134     {
   11135       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   11136       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   11137       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   11138       amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   11139       symtab_shndx_hdr->sh_size = amt;
   11140 
   11141       off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   11142 						       off, TRUE);
   11143 
   11144       if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   11145 	  || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
   11146 	return FALSE;
   11147     }
   11148 
   11149 
   11150   /* Finish up and write out the symbol string table (.strtab)
   11151      section.  */
   11152   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   11153   /* sh_name was set in prep_headers.  */
   11154   symstrtab_hdr->sh_type = SHT_STRTAB;
   11155   symstrtab_hdr->sh_flags = 0;
   11156   symstrtab_hdr->sh_addr = 0;
   11157   symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
   11158   symstrtab_hdr->sh_entsize = 0;
   11159   symstrtab_hdr->sh_link = 0;
   11160   symstrtab_hdr->sh_info = 0;
   11161   /* sh_offset is set just below.  */
   11162   symstrtab_hdr->sh_addralign = 1;
   11163 
   11164   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
   11165   elf_next_file_pos (abfd) = off;
   11166 
   11167   if (bfd_get_symcount (abfd) > 0)
   11168     {
   11169       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   11170 	  || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
   11171 	return FALSE;
   11172     }
   11173 
   11174   /* Adjust the relocs to have the correct symbol indices.  */
   11175   for (o = abfd->sections; o != NULL; o = o->next)
   11176     {
   11177       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11178       if ((o->flags & SEC_RELOC) == 0)
   11179 	continue;
   11180 
   11181       if (esdo->rel.hdr != NULL)
   11182 	elf_link_adjust_relocs (abfd, &esdo->rel);
   11183       if (esdo->rela.hdr != NULL)
   11184 	elf_link_adjust_relocs (abfd, &esdo->rela);
   11185 
   11186       /* Set the reloc_count field to 0 to prevent write_relocs from
   11187 	 trying to swap the relocs out itself.  */
   11188       o->reloc_count = 0;
   11189     }
   11190 
   11191   if (dynamic && info->combreloc && dynobj != NULL)
   11192     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   11193 
   11194   /* If we are linking against a dynamic object, or generating a
   11195      shared library, finish up the dynamic linking information.  */
   11196   if (dynamic)
   11197     {
   11198       bfd_byte *dyncon, *dynconend;
   11199 
   11200       /* Fix up .dynamic entries.  */
   11201       o = bfd_get_linker_section (dynobj, ".dynamic");
   11202       BFD_ASSERT (o != NULL);
   11203 
   11204       dyncon = o->contents;
   11205       dynconend = o->contents + o->size;
   11206       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   11207 	{
   11208 	  Elf_Internal_Dyn dyn;
   11209 	  const char *name;
   11210 	  unsigned int type;
   11211 
   11212 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   11213 
   11214 	  switch (dyn.d_tag)
   11215 	    {
   11216 	    default:
   11217 	      continue;
   11218 	    case DT_NULL:
   11219 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
   11220 		{
   11221 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   11222 		    {
   11223 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   11224 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   11225 		    default: continue;
   11226 		    }
   11227 		  dyn.d_un.d_val = relativecount;
   11228 		  relativecount = 0;
   11229 		  break;
   11230 		}
   11231 	      continue;
   11232 
   11233 	    case DT_INIT:
   11234 	      name = info->init_function;
   11235 	      goto get_sym;
   11236 	    case DT_FINI:
   11237 	      name = info->fini_function;
   11238 	    get_sym:
   11239 	      {
   11240 		struct elf_link_hash_entry *h;
   11241 
   11242 		h = elf_link_hash_lookup (elf_hash_table (info), name,
   11243 					  FALSE, FALSE, TRUE);
   11244 		if (h != NULL
   11245 		    && (h->root.type == bfd_link_hash_defined
   11246 			|| h->root.type == bfd_link_hash_defweak))
   11247 		  {
   11248 		    dyn.d_un.d_ptr = h->root.u.def.value;
   11249 		    o = h->root.u.def.section;
   11250 		    if (o->output_section != NULL)
   11251 		      dyn.d_un.d_ptr += (o->output_section->vma
   11252 					 + o->output_offset);
   11253 		    else
   11254 		      {
   11255 			/* The symbol is imported from another shared
   11256 			   library and does not apply to this one.  */
   11257 			dyn.d_un.d_ptr = 0;
   11258 		      }
   11259 		    break;
   11260 		  }
   11261 	      }
   11262 	      continue;
   11263 
   11264 	    case DT_PREINIT_ARRAYSZ:
   11265 	      name = ".preinit_array";
   11266 	      goto get_size;
   11267 	    case DT_INIT_ARRAYSZ:
   11268 	      name = ".init_array";
   11269 	      goto get_size;
   11270 	    case DT_FINI_ARRAYSZ:
   11271 	      name = ".fini_array";
   11272 	    get_size:
   11273 	      o = bfd_get_section_by_name (abfd, name);
   11274 	      if (o == NULL)
   11275 		{
   11276 		  (*_bfd_error_handler)
   11277 		    (_("%B: could not find output section %s"), abfd, name);
   11278 		  goto error_return;
   11279 		}
   11280 	      if (o->size == 0)
   11281 		(*_bfd_error_handler)
   11282 		  (_("warning: %s section has zero size"), name);
   11283 	      dyn.d_un.d_val = o->size;
   11284 	      break;
   11285 
   11286 	    case DT_PREINIT_ARRAY:
   11287 	      name = ".preinit_array";
   11288 	      goto get_vma;
   11289 	    case DT_INIT_ARRAY:
   11290 	      name = ".init_array";
   11291 	      goto get_vma;
   11292 	    case DT_FINI_ARRAY:
   11293 	      name = ".fini_array";
   11294 	      goto get_vma;
   11295 
   11296 	    case DT_HASH:
   11297 	      name = ".hash";
   11298 	      goto get_vma;
   11299 	    case DT_GNU_HASH:
   11300 	      name = ".gnu.hash";
   11301 	      goto get_vma;
   11302 	    case DT_STRTAB:
   11303 	      name = ".dynstr";
   11304 	      goto get_vma;
   11305 	    case DT_SYMTAB:
   11306 	      name = ".dynsym";
   11307 	      goto get_vma;
   11308 	    case DT_VERDEF:
   11309 	      name = ".gnu.version_d";
   11310 	      goto get_vma;
   11311 	    case DT_VERNEED:
   11312 	      name = ".gnu.version_r";
   11313 	      goto get_vma;
   11314 	    case DT_VERSYM:
   11315 	      name = ".gnu.version";
   11316 	    get_vma:
   11317 	      o = bfd_get_section_by_name (abfd, name);
   11318 	      if (o == NULL)
   11319 		{
   11320 		  (*_bfd_error_handler)
   11321 		    (_("%B: could not find output section %s"), abfd, name);
   11322 		  goto error_return;
   11323 		}
   11324 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   11325 		{
   11326 		  (*_bfd_error_handler)
   11327 		    (_("warning: section '%s' is being made into a note"), name);
   11328 		  bfd_set_error (bfd_error_nonrepresentable_section);
   11329 		  goto error_return;
   11330 		}
   11331 	      dyn.d_un.d_ptr = o->vma;
   11332 	      break;
   11333 
   11334 	    case DT_REL:
   11335 	    case DT_RELA:
   11336 	    case DT_RELSZ:
   11337 	    case DT_RELASZ:
   11338 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   11339 		type = SHT_REL;
   11340 	      else
   11341 		type = SHT_RELA;
   11342 	      dyn.d_un.d_val = 0;
   11343 	      dyn.d_un.d_ptr = 0;
   11344 	      for (i = 1; i < elf_numsections (abfd); i++)
   11345 		{
   11346 		  Elf_Internal_Shdr *hdr;
   11347 
   11348 		  hdr = elf_elfsections (abfd)[i];
   11349 		  if (hdr->sh_type == type
   11350 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   11351 		    {
   11352 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   11353 			dyn.d_un.d_val += hdr->sh_size;
   11354 		      else
   11355 			{
   11356 			  if (dyn.d_un.d_ptr == 0
   11357 			      || hdr->sh_addr < dyn.d_un.d_ptr)
   11358 			    dyn.d_un.d_ptr = hdr->sh_addr;
   11359 			}
   11360 		    }
   11361 		}
   11362 	      break;
   11363 	    }
   11364 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   11365 	}
   11366     }
   11367 
   11368   /* If we have created any dynamic sections, then output them.  */
   11369   if (dynobj != NULL)
   11370     {
   11371       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   11372 	goto error_return;
   11373 
   11374       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   11375       if (((info->warn_shared_textrel && info->shared)
   11376 	   || info->error_textrel)
   11377 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
   11378 	{
   11379 	  bfd_byte *dyncon, *dynconend;
   11380 
   11381 	  dyncon = o->contents;
   11382 	  dynconend = o->contents + o->size;
   11383 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   11384 	    {
   11385 	      Elf_Internal_Dyn dyn;
   11386 
   11387 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   11388 
   11389 	      if (dyn.d_tag == DT_TEXTREL)
   11390 		{
   11391 		  if (info->error_textrel)
   11392 		    info->callbacks->einfo
   11393 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
   11394 		  else
   11395 		    info->callbacks->einfo
   11396 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
   11397 		  break;
   11398 		}
   11399 	    }
   11400 	}
   11401 
   11402       for (o = dynobj->sections; o != NULL; o = o->next)
   11403 	{
   11404 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   11405 	      || o->size == 0
   11406 	      || o->output_section == bfd_abs_section_ptr)
   11407 	    continue;
   11408 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   11409 	    {
   11410 	      /* At this point, we are only interested in sections
   11411 		 created by _bfd_elf_link_create_dynamic_sections.  */
   11412 	      continue;
   11413 	    }
   11414 	  if (elf_hash_table (info)->stab_info.stabstr == o)
   11415 	    continue;
   11416 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
   11417 	    continue;
   11418 	  if (strcmp (o->name, ".dynstr") != 0)
   11419 	    {
   11420 	      /* FIXME: octets_per_byte.  */
   11421 	      if (! bfd_set_section_contents (abfd, o->output_section,
   11422 					      o->contents,
   11423 					      (file_ptr) o->output_offset,
   11424 					      o->size))
   11425 		goto error_return;
   11426 	    }
   11427 	  else
   11428 	    {
   11429 	      /* The contents of the .dynstr section are actually in a
   11430 		 stringtab.  */
   11431 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   11432 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   11433 		  || ! _bfd_elf_strtab_emit (abfd,
   11434 					     elf_hash_table (info)->dynstr))
   11435 		goto error_return;
   11436 	    }
   11437 	}
   11438     }
   11439 
   11440   if (info->relocatable)
   11441     {
   11442       bfd_boolean failed = FALSE;
   11443 
   11444       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   11445       if (failed)
   11446 	goto error_return;
   11447     }
   11448 
   11449   /* If we have optimized stabs strings, output them.  */
   11450   if (elf_hash_table (info)->stab_info.stabstr != NULL)
   11451     {
   11452       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
   11453 	goto error_return;
   11454     }
   11455 
   11456   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   11457     goto error_return;
   11458 
   11459   elf_final_link_free (abfd, &flinfo);
   11460 
   11461   elf_linker (abfd) = TRUE;
   11462 
   11463   if (attr_section)
   11464     {
   11465       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
   11466       if (contents == NULL)
   11467 	return FALSE;	/* Bail out and fail.  */
   11468       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
   11469       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
   11470       free (contents);
   11471     }
   11472 
   11473   return TRUE;
   11474 
   11475  error_return:
   11476   elf_final_link_free (abfd, &flinfo);
   11477   return FALSE;
   11478 }
   11479 
   11480 /* Initialize COOKIE for input bfd ABFD.  */
   11482 
   11483 static bfd_boolean
   11484 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   11485 		   struct bfd_link_info *info, bfd *abfd)
   11486 {
   11487   Elf_Internal_Shdr *symtab_hdr;
   11488   const struct elf_backend_data *bed;
   11489 
   11490   bed = get_elf_backend_data (abfd);
   11491   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11492 
   11493   cookie->abfd = abfd;
   11494   cookie->sym_hashes = elf_sym_hashes (abfd);
   11495   cookie->bad_symtab = elf_bad_symtab (abfd);
   11496   if (cookie->bad_symtab)
   11497     {
   11498       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   11499       cookie->extsymoff = 0;
   11500     }
   11501   else
   11502     {
   11503       cookie->locsymcount = symtab_hdr->sh_info;
   11504       cookie->extsymoff = symtab_hdr->sh_info;
   11505     }
   11506 
   11507   if (bed->s->arch_size == 32)
   11508     cookie->r_sym_shift = 8;
   11509   else
   11510     cookie->r_sym_shift = 32;
   11511 
   11512   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   11513   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   11514     {
   11515       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   11516 					      cookie->locsymcount, 0,
   11517 					      NULL, NULL, NULL);
   11518       if (cookie->locsyms == NULL)
   11519 	{
   11520 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   11521 	  return FALSE;
   11522 	}
   11523       if (info->keep_memory)
   11524 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   11525     }
   11526   return TRUE;
   11527 }
   11528 
   11529 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   11530 
   11531 static void
   11532 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   11533 {
   11534   Elf_Internal_Shdr *symtab_hdr;
   11535 
   11536   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11537   if (cookie->locsyms != NULL
   11538       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   11539     free (cookie->locsyms);
   11540 }
   11541 
   11542 /* Initialize the relocation information in COOKIE for input section SEC
   11543    of input bfd ABFD.  */
   11544 
   11545 static bfd_boolean
   11546 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   11547 			struct bfd_link_info *info, bfd *abfd,
   11548 			asection *sec)
   11549 {
   11550   const struct elf_backend_data *bed;
   11551 
   11552   if (sec->reloc_count == 0)
   11553     {
   11554       cookie->rels = NULL;
   11555       cookie->relend = NULL;
   11556     }
   11557   else
   11558     {
   11559       bed = get_elf_backend_data (abfd);
   11560 
   11561       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   11562 						info->keep_memory);
   11563       if (cookie->rels == NULL)
   11564 	return FALSE;
   11565       cookie->rel = cookie->rels;
   11566       cookie->relend = (cookie->rels
   11567 			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
   11568     }
   11569   cookie->rel = cookie->rels;
   11570   return TRUE;
   11571 }
   11572 
   11573 /* Free the memory allocated by init_reloc_cookie_rels,
   11574    if appropriate.  */
   11575 
   11576 static void
   11577 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   11578 			asection *sec)
   11579 {
   11580   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
   11581     free (cookie->rels);
   11582 }
   11583 
   11584 /* Initialize the whole of COOKIE for input section SEC.  */
   11585 
   11586 static bfd_boolean
   11587 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   11588 			       struct bfd_link_info *info,
   11589 			       asection *sec)
   11590 {
   11591   if (!init_reloc_cookie (cookie, info, sec->owner))
   11592     goto error1;
   11593   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   11594     goto error2;
   11595   return TRUE;
   11596 
   11597  error2:
   11598   fini_reloc_cookie (cookie, sec->owner);
   11599  error1:
   11600   return FALSE;
   11601 }
   11602 
   11603 /* Free the memory allocated by init_reloc_cookie_for_section,
   11604    if appropriate.  */
   11605 
   11606 static void
   11607 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   11608 			       asection *sec)
   11609 {
   11610   fini_reloc_cookie_rels (cookie, sec);
   11611   fini_reloc_cookie (cookie, sec->owner);
   11612 }
   11613 
   11614 /* Garbage collect unused sections.  */
   11616 
   11617 /* Default gc_mark_hook.  */
   11618 
   11619 asection *
   11620 _bfd_elf_gc_mark_hook (asection *sec,
   11621 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   11622 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   11623 		       struct elf_link_hash_entry *h,
   11624 		       Elf_Internal_Sym *sym)
   11625 {
   11626   const char *sec_name;
   11627 
   11628   if (h != NULL)
   11629     {
   11630       switch (h->root.type)
   11631 	{
   11632 	case bfd_link_hash_defined:
   11633 	case bfd_link_hash_defweak:
   11634 	  return h->root.u.def.section;
   11635 
   11636 	case bfd_link_hash_common:
   11637 	  return h->root.u.c.p->section;
   11638 
   11639 	case bfd_link_hash_undefined:
   11640 	case bfd_link_hash_undefweak:
   11641 	  /* To work around a glibc bug, keep all XXX input sections
   11642 	     when there is an as yet undefined reference to __start_XXX
   11643 	     or __stop_XXX symbols.  The linker will later define such
   11644 	     symbols for orphan input sections that have a name
   11645 	     representable as a C identifier.  */
   11646 	  if (strncmp (h->root.root.string, "__start_", 8) == 0)
   11647 	    sec_name = h->root.root.string + 8;
   11648 	  else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
   11649 	    sec_name = h->root.root.string + 7;
   11650 	  else
   11651 	    sec_name = NULL;
   11652 
   11653 	  if (sec_name && *sec_name != '\0')
   11654 	    {
   11655 	      bfd *i;
   11656 
   11657 	      for (i = info->input_bfds; i; i = i->link.next)
   11658 		{
   11659 		  sec = bfd_get_section_by_name (i, sec_name);
   11660 		  if (sec)
   11661 		    sec->flags |= SEC_KEEP;
   11662 		}
   11663 	    }
   11664 	  break;
   11665 
   11666 	default:
   11667 	  break;
   11668 	}
   11669     }
   11670   else
   11671     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   11672 
   11673   return NULL;
   11674 }
   11675 
   11676 /* COOKIE->rel describes a relocation against section SEC, which is
   11677    a section we've decided to keep.  Return the section that contains
   11678    the relocation symbol, or NULL if no section contains it.  */
   11679 
   11680 asection *
   11681 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   11682 		       elf_gc_mark_hook_fn gc_mark_hook,
   11683 		       struct elf_reloc_cookie *cookie)
   11684 {
   11685   unsigned long r_symndx;
   11686   struct elf_link_hash_entry *h;
   11687 
   11688   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   11689   if (r_symndx == STN_UNDEF)
   11690     return NULL;
   11691 
   11692   if (r_symndx >= cookie->locsymcount
   11693       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   11694     {
   11695       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
   11696       while (h->root.type == bfd_link_hash_indirect
   11697 	     || h->root.type == bfd_link_hash_warning)
   11698 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   11699       h->mark = 1;
   11700       /* If this symbol is weak and there is a non-weak definition, we
   11701 	 keep the non-weak definition because many backends put
   11702 	 dynamic reloc info on the non-weak definition for code
   11703 	 handling copy relocs.  */
   11704       if (h->u.weakdef != NULL)
   11705 	h->u.weakdef->mark = 1;
   11706       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   11707     }
   11708 
   11709   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   11710 			  &cookie->locsyms[r_symndx]);
   11711 }
   11712 
   11713 /* COOKIE->rel describes a relocation against section SEC, which is
   11714    a section we've decided to keep.  Mark the section that contains
   11715    the relocation symbol.  */
   11716 
   11717 bfd_boolean
   11718 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   11719 			asection *sec,
   11720 			elf_gc_mark_hook_fn gc_mark_hook,
   11721 			struct elf_reloc_cookie *cookie)
   11722 {
   11723   asection *rsec;
   11724 
   11725   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
   11726   if (rsec && !rsec->gc_mark)
   11727     {
   11728       if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   11729 	  || (rsec->owner->flags & DYNAMIC) != 0)
   11730 	rsec->gc_mark = 1;
   11731       else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   11732 	return FALSE;
   11733     }
   11734   return TRUE;
   11735 }
   11736 
   11737 /* The mark phase of garbage collection.  For a given section, mark
   11738    it and any sections in this section's group, and all the sections
   11739    which define symbols to which it refers.  */
   11740 
   11741 bfd_boolean
   11742 _bfd_elf_gc_mark (struct bfd_link_info *info,
   11743 		  asection *sec,
   11744 		  elf_gc_mark_hook_fn gc_mark_hook)
   11745 {
   11746   bfd_boolean ret;
   11747   asection *group_sec, *eh_frame;
   11748 
   11749   sec->gc_mark = 1;
   11750 
   11751   /* Mark all the sections in the group.  */
   11752   group_sec = elf_section_data (sec)->next_in_group;
   11753   if (group_sec && !group_sec->gc_mark)
   11754     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   11755       return FALSE;
   11756 
   11757   /* Look through the section relocs.  */
   11758   ret = TRUE;
   11759   eh_frame = elf_eh_frame_section (sec->owner);
   11760   if ((sec->flags & SEC_RELOC) != 0
   11761       && sec->reloc_count > 0
   11762       && sec != eh_frame)
   11763     {
   11764       struct elf_reloc_cookie cookie;
   11765 
   11766       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   11767 	ret = FALSE;
   11768       else
   11769 	{
   11770 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   11771 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   11772 	      {
   11773 		ret = FALSE;
   11774 		break;
   11775 	      }
   11776 	  fini_reloc_cookie_for_section (&cookie, sec);
   11777 	}
   11778     }
   11779 
   11780   if (ret && eh_frame && elf_fde_list (sec))
   11781     {
   11782       struct elf_reloc_cookie cookie;
   11783 
   11784       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
   11785 	ret = FALSE;
   11786       else
   11787 	{
   11788 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   11789 				      gc_mark_hook, &cookie))
   11790 	    ret = FALSE;
   11791 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   11792 	}
   11793     }
   11794 
   11795   return ret;
   11796 }
   11797 
   11798 /* Keep debug and special sections.  */
   11799 
   11800 bfd_boolean
   11801 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   11802 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
   11803 {
   11804   bfd *ibfd;
   11805 
   11806   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11807     {
   11808       asection *isec;
   11809       bfd_boolean some_kept;
   11810       bfd_boolean debug_frag_seen;
   11811 
   11812       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   11813 	continue;
   11814 
   11815       /* Ensure all linker created sections are kept,
   11816 	 see if any other section is already marked,
   11817 	 and note if we have any fragmented debug sections.  */
   11818       debug_frag_seen = some_kept = FALSE;
   11819       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   11820 	{
   11821 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   11822 	    isec->gc_mark = 1;
   11823 	  else if (isec->gc_mark)
   11824 	    some_kept = TRUE;
   11825 
   11826 	  if (debug_frag_seen == FALSE
   11827 	      && (isec->flags & SEC_DEBUGGING)
   11828 	      && CONST_STRNEQ (isec->name, ".debug_line."))
   11829 	    debug_frag_seen = TRUE;
   11830 	}
   11831 
   11832       /* If no section in this file will be kept, then we can
   11833 	 toss out the debug and special sections.  */
   11834       if (!some_kept)
   11835 	continue;
   11836 
   11837       /* Keep debug and special sections like .comment when they are
   11838 	 not part of a group, or when we have single-member groups.  */
   11839       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   11840 	if ((elf_next_in_group (isec) == NULL
   11841 	     || elf_next_in_group (isec) == isec)
   11842 	    && ((isec->flags & SEC_DEBUGGING) != 0
   11843 		|| (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
   11844 	  isec->gc_mark = 1;
   11845 
   11846       if (! debug_frag_seen)
   11847 	continue;
   11848 
   11849       /* Look for CODE sections which are going to be discarded,
   11850 	 and find and discard any fragmented debug sections which
   11851 	 are associated with that code section.  */
   11852       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   11853 	if ((isec->flags & SEC_CODE) != 0
   11854 	    && isec->gc_mark == 0)
   11855 	  {
   11856 	    unsigned int ilen;
   11857 	    asection *dsec;
   11858 
   11859 	    ilen = strlen (isec->name);
   11860 
   11861 	    /* Association is determined by the name of the debug section
   11862 	       containing the name of the code section as a suffix.  For
   11863 	       example .debug_line.text.foo is a debug section associated
   11864 	       with .text.foo.  */
   11865 	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   11866 	      {
   11867 		unsigned int dlen;
   11868 
   11869 		if (dsec->gc_mark == 0
   11870 		    || (dsec->flags & SEC_DEBUGGING) == 0)
   11871 		  continue;
   11872 
   11873 		dlen = strlen (dsec->name);
   11874 
   11875 		if (dlen > ilen
   11876 		    && strncmp (dsec->name + (dlen - ilen),
   11877 				isec->name, ilen) == 0)
   11878 		  {
   11879 		    dsec->gc_mark = 0;
   11880 		    break;
   11881 		  }
   11882 	      }
   11883 	  }
   11884     }
   11885   return TRUE;
   11886 }
   11887 
   11888 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   11889 
   11890 struct elf_gc_sweep_symbol_info
   11891 {
   11892   struct bfd_link_info *info;
   11893   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   11894 		       bfd_boolean);
   11895 };
   11896 
   11897 static bfd_boolean
   11898 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   11899 {
   11900   if (!h->mark
   11901       && (((h->root.type == bfd_link_hash_defined
   11902 	    || h->root.type == bfd_link_hash_defweak)
   11903 	   && !(h->def_regular
   11904 		&& h->root.u.def.section->gc_mark))
   11905 	  || h->root.type == bfd_link_hash_undefined
   11906 	  || h->root.type == bfd_link_hash_undefweak))
   11907     {
   11908       struct elf_gc_sweep_symbol_info *inf;
   11909 
   11910       inf = (struct elf_gc_sweep_symbol_info *) data;
   11911       (*inf->hide_symbol) (inf->info, h, TRUE);
   11912       h->def_regular = 0;
   11913       h->ref_regular = 0;
   11914       h->ref_regular_nonweak = 0;
   11915     }
   11916 
   11917   return TRUE;
   11918 }
   11919 
   11920 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
   11921 
   11922 typedef bfd_boolean (*gc_sweep_hook_fn)
   11923   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
   11924 
   11925 static bfd_boolean
   11926 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   11927 {
   11928   bfd *sub;
   11929   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11930   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
   11931   unsigned long section_sym_count;
   11932   struct elf_gc_sweep_symbol_info sweep_info;
   11933 
   11934   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   11935     {
   11936       asection *o;
   11937 
   11938       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
   11939 	continue;
   11940 
   11941       for (o = sub->sections; o != NULL; o = o->next)
   11942 	{
   11943 	  /* When any section in a section group is kept, we keep all
   11944 	     sections in the section group.  If the first member of
   11945 	     the section group is excluded, we will also exclude the
   11946 	     group section.  */
   11947 	  if (o->flags & SEC_GROUP)
   11948 	    {
   11949 	      asection *first = elf_next_in_group (o);
   11950 	      o->gc_mark = first->gc_mark;
   11951 	    }
   11952 
   11953 	  if (o->gc_mark)
   11954 	    continue;
   11955 
   11956 	  /* Skip sweeping sections already excluded.  */
   11957 	  if (o->flags & SEC_EXCLUDE)
   11958 	    continue;
   11959 
   11960 	  /* Since this is early in the link process, it is simple
   11961 	     to remove a section from the output.  */
   11962 	  o->flags |= SEC_EXCLUDE;
   11963 
   11964 	  if (info->print_gc_sections && o->size != 0)
   11965 	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
   11966 
   11967 	  /* But we also have to update some of the relocation
   11968 	     info we collected before.  */
   11969 	  if (gc_sweep_hook
   11970 	      && (o->flags & SEC_RELOC) != 0
   11971 	      && o->reloc_count != 0
   11972 	      && !((info->strip == strip_all || info->strip == strip_debugger)
   11973 		   && (o->flags & SEC_DEBUGGING) != 0)
   11974 	      && !bfd_is_abs_section (o->output_section))
   11975 	    {
   11976 	      Elf_Internal_Rela *internal_relocs;
   11977 	      bfd_boolean r;
   11978 
   11979 	      internal_relocs
   11980 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
   11981 					     info->keep_memory);
   11982 	      if (internal_relocs == NULL)
   11983 		return FALSE;
   11984 
   11985 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
   11986 
   11987 	      if (elf_section_data (o)->relocs != internal_relocs)
   11988 		free (internal_relocs);
   11989 
   11990 	      if (!r)
   11991 		return FALSE;
   11992 	    }
   11993 	}
   11994     }
   11995 
   11996   /* Remove the symbols that were in the swept sections from the dynamic
   11997      symbol table.  GCFIXME: Anyone know how to get them out of the
   11998      static symbol table as well?  */
   11999   sweep_info.info = info;
   12000   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   12001   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   12002 			  &sweep_info);
   12003 
   12004   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
   12005   return TRUE;
   12006 }
   12007 
   12008 /* Propagate collected vtable information.  This is called through
   12009    elf_link_hash_traverse.  */
   12010 
   12011 static bfd_boolean
   12012 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   12013 {
   12014   /* Those that are not vtables.  */
   12015   if (h->vtable == NULL || h->vtable->parent == NULL)
   12016     return TRUE;
   12017 
   12018   /* Those vtables that do not have parents, we cannot merge.  */
   12019   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
   12020     return TRUE;
   12021 
   12022   /* If we've already been done, exit.  */
   12023   if (h->vtable->used && h->vtable->used[-1])
   12024     return TRUE;
   12025 
   12026   /* Make sure the parent's table is up to date.  */
   12027   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
   12028 
   12029   if (h->vtable->used == NULL)
   12030     {
   12031       /* None of this table's entries were referenced.  Re-use the
   12032 	 parent's table.  */
   12033       h->vtable->used = h->vtable->parent->vtable->used;
   12034       h->vtable->size = h->vtable->parent->vtable->size;
   12035     }
   12036   else
   12037     {
   12038       size_t n;
   12039       bfd_boolean *cu, *pu;
   12040 
   12041       /* Or the parent's entries into ours.  */
   12042       cu = h->vtable->used;
   12043       cu[-1] = TRUE;
   12044       pu = h->vtable->parent->vtable->used;
   12045       if (pu != NULL)
   12046 	{
   12047 	  const struct elf_backend_data *bed;
   12048 	  unsigned int log_file_align;
   12049 
   12050 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   12051 	  log_file_align = bed->s->log_file_align;
   12052 	  n = h->vtable->parent->vtable->size >> log_file_align;
   12053 	  while (n--)
   12054 	    {
   12055 	      if (*pu)
   12056 		*cu = TRUE;
   12057 	      pu++;
   12058 	      cu++;
   12059 	    }
   12060 	}
   12061     }
   12062 
   12063   return TRUE;
   12064 }
   12065 
   12066 static bfd_boolean
   12067 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
   12068 {
   12069   asection *sec;
   12070   bfd_vma hstart, hend;
   12071   Elf_Internal_Rela *relstart, *relend, *rel;
   12072   const struct elf_backend_data *bed;
   12073   unsigned int log_file_align;
   12074 
   12075   /* Take care of both those symbols that do not describe vtables as
   12076      well as those that are not loaded.  */
   12077   if (h->vtable == NULL || h->vtable->parent == NULL)
   12078     return TRUE;
   12079 
   12080   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   12081 	      || h->root.type == bfd_link_hash_defweak);
   12082 
   12083   sec = h->root.u.def.section;
   12084   hstart = h->root.u.def.value;
   12085   hend = hstart + h->size;
   12086 
   12087   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
   12088   if (!relstart)
   12089     return *(bfd_boolean *) okp = FALSE;
   12090   bed = get_elf_backend_data (sec->owner);
   12091   log_file_align = bed->s->log_file_align;
   12092 
   12093   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
   12094 
   12095   for (rel = relstart; rel < relend; ++rel)
   12096     if (rel->r_offset >= hstart && rel->r_offset < hend)
   12097       {
   12098 	/* If the entry is in use, do nothing.  */
   12099 	if (h->vtable->used
   12100 	    && (rel->r_offset - hstart) < h->vtable->size)
   12101 	  {
   12102 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   12103 	    if (h->vtable->used[entry])
   12104 	      continue;
   12105 	  }
   12106 	/* Otherwise, kill it.  */
   12107 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   12108       }
   12109 
   12110   return TRUE;
   12111 }
   12112 
   12113 /* Mark sections containing dynamically referenced symbols.  When
   12114    building shared libraries, we must assume that any visible symbol is
   12115    referenced.  */
   12116 
   12117 bfd_boolean
   12118 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   12119 {
   12120   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   12121   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   12122 
   12123   if ((h->root.type == bfd_link_hash_defined
   12124        || h->root.type == bfd_link_hash_defweak)
   12125       && (h->ref_dynamic
   12126 	  || (h->def_regular
   12127 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   12128 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   12129 	      && (!info->executable
   12130 		  || info->export_dynamic
   12131 		  || (h->dynamic
   12132 		      && d != NULL
   12133 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   12134 	      && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
   12135 		  || !bfd_hide_sym_by_version (info->version_info,
   12136 					       h->root.root.string)))))
   12137     h->root.u.def.section->flags |= SEC_KEEP;
   12138 
   12139   return TRUE;
   12140 }
   12141 
   12142 /* Keep all sections containing symbols undefined on the command-line,
   12143    and the section containing the entry symbol.  */
   12144 
   12145 void
   12146 _bfd_elf_gc_keep (struct bfd_link_info *info)
   12147 {
   12148   struct bfd_sym_chain *sym;
   12149 
   12150   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   12151     {
   12152       struct elf_link_hash_entry *h;
   12153 
   12154       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   12155 				FALSE, FALSE, FALSE);
   12156 
   12157       if (h != NULL
   12158 	  && (h->root.type == bfd_link_hash_defined
   12159 	      || h->root.type == bfd_link_hash_defweak)
   12160 	  && !bfd_is_abs_section (h->root.u.def.section))
   12161 	h->root.u.def.section->flags |= SEC_KEEP;
   12162     }
   12163 }
   12164 
   12165 /* Do mark and sweep of unused sections.  */
   12166 
   12167 bfd_boolean
   12168 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   12169 {
   12170   bfd_boolean ok = TRUE;
   12171   bfd *sub;
   12172   elf_gc_mark_hook_fn gc_mark_hook;
   12173   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12174   struct elf_link_hash_table *htab;
   12175 
   12176   if (!bed->can_gc_sections
   12177       || !is_elf_hash_table (info->hash))
   12178     {
   12179       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
   12180       return TRUE;
   12181     }
   12182 
   12183   bed->gc_keep (info);
   12184   htab = elf_hash_table (info);
   12185 
   12186   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   12187      at the .eh_frame section if we can mark the FDEs individually.  */
   12188   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12189     {
   12190       asection *sec;
   12191       struct elf_reloc_cookie cookie;
   12192 
   12193       sec = bfd_get_section_by_name (sub, ".eh_frame");
   12194       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
   12195 	{
   12196 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   12197 	  if (elf_section_data (sec)->sec_info
   12198 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   12199 	    elf_eh_frame_section (sub) = sec;
   12200 	  fini_reloc_cookie_for_section (&cookie, sec);
   12201 	  sec = bfd_get_next_section_by_name (sec);
   12202 	}
   12203     }
   12204 
   12205   /* Apply transitive closure to the vtable entry usage info.  */
   12206   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   12207   if (!ok)
   12208     return FALSE;
   12209 
   12210   /* Kill the vtable relocations that were not used.  */
   12211   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
   12212   if (!ok)
   12213     return FALSE;
   12214 
   12215   /* Mark dynamically referenced symbols.  */
   12216   if (htab->dynamic_sections_created)
   12217     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   12218 
   12219   /* Grovel through relocs to find out who stays ...  */
   12220   gc_mark_hook = bed->gc_mark_hook;
   12221   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12222     {
   12223       asection *o;
   12224 
   12225       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
   12226 	continue;
   12227 
   12228       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   12229 	 Also treat note sections as a root, if the section is not part
   12230 	 of a group.  */
   12231       for (o = sub->sections; o != NULL; o = o->next)
   12232 	if (!o->gc_mark
   12233 	    && (o->flags & SEC_EXCLUDE) == 0
   12234 	    && ((o->flags & SEC_KEEP) != 0
   12235 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   12236 		    && elf_next_in_group (o) == NULL )))
   12237 	  {
   12238 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   12239 	      return FALSE;
   12240 	  }
   12241     }
   12242 
   12243   /* Allow the backend to mark additional target specific sections.  */
   12244   bed->gc_mark_extra_sections (info, gc_mark_hook);
   12245 
   12246   /* ... and mark SEC_EXCLUDE for those that go.  */
   12247   return elf_gc_sweep (abfd, info);
   12248 }
   12249 
   12250 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   12252 
   12253 bfd_boolean
   12254 bfd_elf_gc_record_vtinherit (bfd *abfd,
   12255 			     asection *sec,
   12256 			     struct elf_link_hash_entry *h,
   12257 			     bfd_vma offset)
   12258 {
   12259   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   12260   struct elf_link_hash_entry **search, *child;
   12261   bfd_size_type extsymcount;
   12262   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12263 
   12264   /* The sh_info field of the symtab header tells us where the
   12265      external symbols start.  We don't care about the local symbols at
   12266      this point.  */
   12267   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   12268   if (!elf_bad_symtab (abfd))
   12269     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   12270 
   12271   sym_hashes = elf_sym_hashes (abfd);
   12272   sym_hashes_end = sym_hashes + extsymcount;
   12273 
   12274   /* Hunt down the child symbol, which is in this section at the same
   12275      offset as the relocation.  */
   12276   for (search = sym_hashes; search != sym_hashes_end; ++search)
   12277     {
   12278       if ((child = *search) != NULL
   12279 	  && (child->root.type == bfd_link_hash_defined
   12280 	      || child->root.type == bfd_link_hash_defweak)
   12281 	  && child->root.u.def.section == sec
   12282 	  && child->root.u.def.value == offset)
   12283 	goto win;
   12284     }
   12285 
   12286   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
   12287 			 abfd, sec, (unsigned long) offset);
   12288   bfd_set_error (bfd_error_invalid_operation);
   12289   return FALSE;
   12290 
   12291  win:
   12292   if (!child->vtable)
   12293     {
   12294       child->vtable = (struct elf_link_virtual_table_entry *)
   12295           bfd_zalloc (abfd, sizeof (*child->vtable));
   12296       if (!child->vtable)
   12297 	return FALSE;
   12298     }
   12299   if (!h)
   12300     {
   12301       /* This *should* only be the absolute section.  It could potentially
   12302 	 be that someone has defined a non-global vtable though, which
   12303 	 would be bad.  It isn't worth paging in the local symbols to be
   12304 	 sure though; that case should simply be handled by the assembler.  */
   12305 
   12306       child->vtable->parent = (struct elf_link_hash_entry *) -1;
   12307     }
   12308   else
   12309     child->vtable->parent = h;
   12310 
   12311   return TRUE;
   12312 }
   12313 
   12314 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   12315 
   12316 bfd_boolean
   12317 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
   12318 			   asection *sec ATTRIBUTE_UNUSED,
   12319 			   struct elf_link_hash_entry *h,
   12320 			   bfd_vma addend)
   12321 {
   12322   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12323   unsigned int log_file_align = bed->s->log_file_align;
   12324 
   12325   if (!h->vtable)
   12326     {
   12327       h->vtable = (struct elf_link_virtual_table_entry *)
   12328           bfd_zalloc (abfd, sizeof (*h->vtable));
   12329       if (!h->vtable)
   12330 	return FALSE;
   12331     }
   12332 
   12333   if (addend >= h->vtable->size)
   12334     {
   12335       size_t size, bytes, file_align;
   12336       bfd_boolean *ptr = h->vtable->used;
   12337 
   12338       /* While the symbol is undefined, we have to be prepared to handle
   12339 	 a zero size.  */
   12340       file_align = 1 << log_file_align;
   12341       if (h->root.type == bfd_link_hash_undefined)
   12342 	size = addend + file_align;
   12343       else
   12344 	{
   12345 	  size = h->size;
   12346 	  if (addend >= size)
   12347 	    {
   12348 	      /* Oops!  We've got a reference past the defined end of
   12349 		 the table.  This is probably a bug -- shall we warn?  */
   12350 	      size = addend + file_align;
   12351 	    }
   12352 	}
   12353       size = (size + file_align - 1) & -file_align;
   12354 
   12355       /* Allocate one extra entry for use as a "done" flag for the
   12356 	 consolidation pass.  */
   12357       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
   12358 
   12359       if (ptr)
   12360 	{
   12361 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
   12362 
   12363 	  if (ptr != NULL)
   12364 	    {
   12365 	      size_t oldbytes;
   12366 
   12367 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
   12368 			  * sizeof (bfd_boolean));
   12369 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   12370 	    }
   12371 	}
   12372       else
   12373 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
   12374 
   12375       if (ptr == NULL)
   12376 	return FALSE;
   12377 
   12378       /* And arrange for that done flag to be at index -1.  */
   12379       h->vtable->used = ptr + 1;
   12380       h->vtable->size = size;
   12381     }
   12382 
   12383   h->vtable->used[addend >> log_file_align] = TRUE;
   12384 
   12385   return TRUE;
   12386 }
   12387 
   12388 /* Map an ELF section header flag to its corresponding string.  */
   12389 typedef struct
   12390 {
   12391   char *flag_name;
   12392   flagword flag_value;
   12393 } elf_flags_to_name_table;
   12394 
   12395 static elf_flags_to_name_table elf_flags_to_names [] =
   12396 {
   12397   { "SHF_WRITE", SHF_WRITE },
   12398   { "SHF_ALLOC", SHF_ALLOC },
   12399   { "SHF_EXECINSTR", SHF_EXECINSTR },
   12400   { "SHF_MERGE", SHF_MERGE },
   12401   { "SHF_STRINGS", SHF_STRINGS },
   12402   { "SHF_INFO_LINK", SHF_INFO_LINK},
   12403   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   12404   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   12405   { "SHF_GROUP", SHF_GROUP },
   12406   { "SHF_TLS", SHF_TLS },
   12407   { "SHF_MASKOS", SHF_MASKOS },
   12408   { "SHF_EXCLUDE", SHF_EXCLUDE },
   12409 };
   12410 
   12411 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   12412 bfd_boolean
   12413 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   12414 			      struct flag_info *flaginfo,
   12415 			      asection *section)
   12416 {
   12417   const bfd_vma sh_flags = elf_section_flags (section);
   12418 
   12419   if (!flaginfo->flags_initialized)
   12420     {
   12421       bfd *obfd = info->output_bfd;
   12422       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   12423       struct flag_info_list *tf = flaginfo->flag_list;
   12424       int with_hex = 0;
   12425       int without_hex = 0;
   12426 
   12427       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   12428 	{
   12429 	  unsigned i;
   12430 	  flagword (*lookup) (char *);
   12431 
   12432 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   12433 	  if (lookup != NULL)
   12434 	    {
   12435 	      flagword hexval = (*lookup) ((char *) tf->name);
   12436 
   12437 	      if (hexval != 0)
   12438 		{
   12439 		  if (tf->with == with_flags)
   12440 		    with_hex |= hexval;
   12441 		  else if (tf->with == without_flags)
   12442 		    without_hex |= hexval;
   12443 		  tf->valid = TRUE;
   12444 		  continue;
   12445 		}
   12446 	    }
   12447 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   12448 	    {
   12449 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   12450 		{
   12451 		  if (tf->with == with_flags)
   12452 		    with_hex |= elf_flags_to_names[i].flag_value;
   12453 		  else if (tf->with == without_flags)
   12454 		    without_hex |= elf_flags_to_names[i].flag_value;
   12455 		  tf->valid = TRUE;
   12456 		  break;
   12457 		}
   12458 	    }
   12459 	  if (!tf->valid)
   12460 	    {
   12461 	      info->callbacks->einfo
   12462 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   12463 	      return FALSE;
   12464 	    }
   12465 	}
   12466       flaginfo->flags_initialized = TRUE;
   12467       flaginfo->only_with_flags |= with_hex;
   12468       flaginfo->not_with_flags |= without_hex;
   12469     }
   12470 
   12471   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   12472     return FALSE;
   12473 
   12474   if ((flaginfo->not_with_flags & sh_flags) != 0)
   12475     return FALSE;
   12476 
   12477   return TRUE;
   12478 }
   12479 
   12480 struct alloc_got_off_arg {
   12481   bfd_vma gotoff;
   12482   struct bfd_link_info *info;
   12483 };
   12484 
   12485 /* We need a special top-level link routine to convert got reference counts
   12486    to real got offsets.  */
   12487 
   12488 static bfd_boolean
   12489 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   12490 {
   12491   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   12492   bfd *obfd = gofarg->info->output_bfd;
   12493   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   12494 
   12495   if (h->got.refcount > 0)
   12496     {
   12497       h->got.offset = gofarg->gotoff;
   12498       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   12499     }
   12500   else
   12501     h->got.offset = (bfd_vma) -1;
   12502 
   12503   return TRUE;
   12504 }
   12505 
   12506 /* And an accompanying bit to work out final got entry offsets once
   12507    we're done.  Should be called from final_link.  */
   12508 
   12509 bfd_boolean
   12510 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   12511 					struct bfd_link_info *info)
   12512 {
   12513   bfd *i;
   12514   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12515   bfd_vma gotoff;
   12516   struct alloc_got_off_arg gofarg;
   12517 
   12518   BFD_ASSERT (abfd == info->output_bfd);
   12519 
   12520   if (! is_elf_hash_table (info->hash))
   12521     return FALSE;
   12522 
   12523   /* The GOT offset is relative to the .got section, but the GOT header is
   12524      put into the .got.plt section, if the backend uses it.  */
   12525   if (bed->want_got_plt)
   12526     gotoff = 0;
   12527   else
   12528     gotoff = bed->got_header_size;
   12529 
   12530   /* Do the local .got entries first.  */
   12531   for (i = info->input_bfds; i; i = i->link.next)
   12532     {
   12533       bfd_signed_vma *local_got;
   12534       bfd_size_type j, locsymcount;
   12535       Elf_Internal_Shdr *symtab_hdr;
   12536 
   12537       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   12538 	continue;
   12539 
   12540       local_got = elf_local_got_refcounts (i);
   12541       if (!local_got)
   12542 	continue;
   12543 
   12544       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   12545       if (elf_bad_symtab (i))
   12546 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   12547       else
   12548 	locsymcount = symtab_hdr->sh_info;
   12549 
   12550       for (j = 0; j < locsymcount; ++j)
   12551 	{
   12552 	  if (local_got[j] > 0)
   12553 	    {
   12554 	      local_got[j] = gotoff;
   12555 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   12556 	    }
   12557 	  else
   12558 	    local_got[j] = (bfd_vma) -1;
   12559 	}
   12560     }
   12561 
   12562   /* Then the global .got entries.  .plt refcounts are handled by
   12563      adjust_dynamic_symbol  */
   12564   gofarg.gotoff = gotoff;
   12565   gofarg.info = info;
   12566   elf_link_hash_traverse (elf_hash_table (info),
   12567 			  elf_gc_allocate_got_offsets,
   12568 			  &gofarg);
   12569   return TRUE;
   12570 }
   12571 
   12572 /* Many folk need no more in the way of final link than this, once
   12573    got entry reference counting is enabled.  */
   12574 
   12575 bfd_boolean
   12576 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   12577 {
   12578   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   12579     return FALSE;
   12580 
   12581   /* Invoke the regular ELF backend linker to do all the work.  */
   12582   return bfd_elf_final_link (abfd, info);
   12583 }
   12584 
   12585 bfd_boolean
   12586 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   12587 {
   12588   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   12589 
   12590   if (rcookie->bad_symtab)
   12591     rcookie->rel = rcookie->rels;
   12592 
   12593   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   12594     {
   12595       unsigned long r_symndx;
   12596 
   12597       if (! rcookie->bad_symtab)
   12598 	if (rcookie->rel->r_offset > offset)
   12599 	  return FALSE;
   12600       if (rcookie->rel->r_offset != offset)
   12601 	continue;
   12602 
   12603       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   12604       if (r_symndx == STN_UNDEF)
   12605 	return TRUE;
   12606 
   12607       if (r_symndx >= rcookie->locsymcount
   12608 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   12609 	{
   12610 	  struct elf_link_hash_entry *h;
   12611 
   12612 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
   12613 
   12614 	  while (h->root.type == bfd_link_hash_indirect
   12615 		 || h->root.type == bfd_link_hash_warning)
   12616 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   12617 
   12618 	  if ((h->root.type == bfd_link_hash_defined
   12619 	       || h->root.type == bfd_link_hash_defweak)
   12620 	      && (h->root.u.def.section->owner != rcookie->abfd
   12621 		  || h->root.u.def.section->kept_section != NULL
   12622 		  || discarded_section (h->root.u.def.section)))
   12623 	    return TRUE;
   12624 	}
   12625       else
   12626 	{
   12627 	  /* It's not a relocation against a global symbol,
   12628 	     but it could be a relocation against a local
   12629 	     symbol for a discarded section.  */
   12630 	  asection *isec;
   12631 	  Elf_Internal_Sym *isym;
   12632 
   12633 	  /* Need to: get the symbol; get the section.  */
   12634 	  isym = &rcookie->locsyms[r_symndx];
   12635 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   12636 	  if (isec != NULL
   12637 	      && (isec->kept_section != NULL
   12638 		  || discarded_section (isec)))
   12639 	    return TRUE;
   12640 	}
   12641       return FALSE;
   12642     }
   12643   return FALSE;
   12644 }
   12645 
   12646 /* Discard unneeded references to discarded sections.
   12647    Returns -1 on error, 1 if any section's size was changed, 0 if
   12648    nothing changed.  This function assumes that the relocations are in
   12649    sorted order, which is true for all known assemblers.  */
   12650 
   12651 int
   12652 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   12653 {
   12654   struct elf_reloc_cookie cookie;
   12655   asection *o;
   12656   bfd *abfd;
   12657   int changed = 0;
   12658 
   12659   if (info->traditional_format
   12660       || !is_elf_hash_table (info->hash))
   12661     return 0;
   12662 
   12663   o = bfd_get_section_by_name (output_bfd, ".stab");
   12664   if (o != NULL)
   12665     {
   12666       asection *i;
   12667 
   12668       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   12669 	{
   12670 	  if (i->size == 0
   12671 	      || i->reloc_count == 0
   12672 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   12673 	    continue;
   12674 
   12675 	  abfd = i->owner;
   12676 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   12677 	    continue;
   12678 
   12679 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   12680 	    return -1;
   12681 
   12682 	  if (_bfd_discard_section_stabs (abfd, i,
   12683 					  elf_section_data (i)->sec_info,
   12684 					  bfd_elf_reloc_symbol_deleted_p,
   12685 					  &cookie))
   12686 	    changed = 1;
   12687 
   12688 	  fini_reloc_cookie_for_section (&cookie, i);
   12689 	}
   12690     }
   12691 
   12692   o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   12693   if (o != NULL)
   12694     {
   12695       asection *i;
   12696 
   12697       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   12698 	{
   12699 	  if (i->size == 0)
   12700 	    continue;
   12701 
   12702 	  abfd = i->owner;
   12703 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   12704 	    continue;
   12705 
   12706 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   12707 	    return -1;
   12708 
   12709 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   12710 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   12711 						 bfd_elf_reloc_symbol_deleted_p,
   12712 						 &cookie))
   12713 	    changed = 1;
   12714 
   12715 	  fini_reloc_cookie_for_section (&cookie, i);
   12716 	}
   12717     }
   12718 
   12719   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   12720     {
   12721       const struct elf_backend_data *bed;
   12722 
   12723       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   12724 	continue;
   12725 
   12726       bed = get_elf_backend_data (abfd);
   12727 
   12728       if (bed->elf_backend_discard_info != NULL)
   12729 	{
   12730 	  if (!init_reloc_cookie (&cookie, info, abfd))
   12731 	    return -1;
   12732 
   12733 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   12734 	    changed = 1;
   12735 
   12736 	  fini_reloc_cookie (&cookie, abfd);
   12737 	}
   12738     }
   12739 
   12740   if (info->eh_frame_hdr
   12741       && !info->relocatable
   12742       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
   12743     changed = 1;
   12744 
   12745   return changed;
   12746 }
   12747 
   12748 bfd_boolean
   12749 _bfd_elf_section_already_linked (bfd *abfd,
   12750 				 asection *sec,
   12751 				 struct bfd_link_info *info)
   12752 {
   12753   flagword flags;
   12754   const char *name, *key;
   12755   struct bfd_section_already_linked *l;
   12756   struct bfd_section_already_linked_hash_entry *already_linked_list;
   12757 
   12758   if (sec->output_section == bfd_abs_section_ptr)
   12759     return FALSE;
   12760 
   12761   flags = sec->flags;
   12762 
   12763   /* Return if it isn't a linkonce section.  A comdat group section
   12764      also has SEC_LINK_ONCE set.  */
   12765   if ((flags & SEC_LINK_ONCE) == 0)
   12766     return FALSE;
   12767 
   12768   /* Don't put group member sections on our list of already linked
   12769      sections.  They are handled as a group via their group section.  */
   12770   if (elf_sec_group (sec) != NULL)
   12771     return FALSE;
   12772 
   12773   /* For a SHT_GROUP section, use the group signature as the key.  */
   12774   name = sec->name;
   12775   if ((flags & SEC_GROUP) != 0
   12776       && elf_next_in_group (sec) != NULL
   12777       && elf_group_name (elf_next_in_group (sec)) != NULL)
   12778     key = elf_group_name (elf_next_in_group (sec));
   12779   else
   12780     {
   12781       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   12782       if (CONST_STRNEQ (name, ".gnu.linkonce.")
   12783 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   12784 	key++;
   12785       else
   12786 	/* Must be a user linkonce section that doesn't follow gcc's
   12787 	   naming convention.  In this case we won't be matching
   12788 	   single member groups.  */
   12789 	key = name;
   12790     }
   12791 
   12792   already_linked_list = bfd_section_already_linked_table_lookup (key);
   12793 
   12794   for (l = already_linked_list->entry; l != NULL; l = l->next)
   12795     {
   12796       /* We may have 2 different types of sections on the list: group
   12797 	 sections with a signature of <key> (<key> is some string),
   12798 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   12799 	 Match like sections.  LTO plugin sections are an exception.
   12800 	 They are always named .gnu.linkonce.t.<key> and match either
   12801 	 type of section.  */
   12802       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   12803 	   && ((flags & SEC_GROUP) != 0
   12804 	       || strcmp (name, l->sec->name) == 0))
   12805 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
   12806 	{
   12807 	  /* The section has already been linked.  See if we should
   12808 	     issue a warning.  */
   12809 	  if (!_bfd_handle_already_linked (sec, l, info))
   12810 	    return FALSE;
   12811 
   12812 	  if (flags & SEC_GROUP)
   12813 	    {
   12814 	      asection *first = elf_next_in_group (sec);
   12815 	      asection *s = first;
   12816 
   12817 	      while (s != NULL)
   12818 		{
   12819 		  s->output_section = bfd_abs_section_ptr;
   12820 		  /* Record which group discards it.  */
   12821 		  s->kept_section = l->sec;
   12822 		  s = elf_next_in_group (s);
   12823 		  /* These lists are circular.  */
   12824 		  if (s == first)
   12825 		    break;
   12826 		}
   12827 	    }
   12828 
   12829 	  return TRUE;
   12830 	}
   12831     }
   12832 
   12833   /* A single member comdat group section may be discarded by a
   12834      linkonce section and vice versa.  */
   12835   if ((flags & SEC_GROUP) != 0)
   12836     {
   12837       asection *first = elf_next_in_group (sec);
   12838 
   12839       if (first != NULL && elf_next_in_group (first) == first)
   12840 	/* Check this single member group against linkonce sections.  */
   12841 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   12842 	  if ((l->sec->flags & SEC_GROUP) == 0
   12843 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   12844 	    {
   12845 	      first->output_section = bfd_abs_section_ptr;
   12846 	      first->kept_section = l->sec;
   12847 	      sec->output_section = bfd_abs_section_ptr;
   12848 	      break;
   12849 	    }
   12850     }
   12851   else
   12852     /* Check this linkonce section against single member groups.  */
   12853     for (l = already_linked_list->entry; l != NULL; l = l->next)
   12854       if (l->sec->flags & SEC_GROUP)
   12855 	{
   12856 	  asection *first = elf_next_in_group (l->sec);
   12857 
   12858 	  if (first != NULL
   12859 	      && elf_next_in_group (first) == first
   12860 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   12861 	    {
   12862 	      sec->output_section = bfd_abs_section_ptr;
   12863 	      sec->kept_section = first;
   12864 	      break;
   12865 	    }
   12866 	}
   12867 
   12868   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   12869      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   12870      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   12871      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   12872      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   12873      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   12874      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   12875      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   12876      The reverse order cannot happen as there is never a bfd with only the
   12877      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   12878      matter as here were are looking only for cross-bfd sections.  */
   12879 
   12880   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
   12881     for (l = already_linked_list->entry; l != NULL; l = l->next)
   12882       if ((l->sec->flags & SEC_GROUP) == 0
   12883 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
   12884 	{
   12885 	  if (abfd != l->sec->owner)
   12886 	    sec->output_section = bfd_abs_section_ptr;
   12887 	  break;
   12888 	}
   12889 
   12890   /* This is the first section with this name.  Record it.  */
   12891   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   12892     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   12893   return sec->output_section == bfd_abs_section_ptr;
   12894 }
   12895 
   12896 bfd_boolean
   12897 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   12898 {
   12899   return sym->st_shndx == SHN_COMMON;
   12900 }
   12901 
   12902 unsigned int
   12903 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   12904 {
   12905   return SHN_COMMON;
   12906 }
   12907 
   12908 asection *
   12909 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   12910 {
   12911   return bfd_com_section_ptr;
   12912 }
   12913 
   12914 bfd_vma
   12915 _bfd_elf_default_got_elt_size (bfd *abfd,
   12916 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12917 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   12918 			       bfd *ibfd ATTRIBUTE_UNUSED,
   12919 			       unsigned long symndx ATTRIBUTE_UNUSED)
   12920 {
   12921   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12922   return bed->s->arch_size / 8;
   12923 }
   12924 
   12925 /* Routines to support the creation of dynamic relocs.  */
   12926 
   12927 /* Returns the name of the dynamic reloc section associated with SEC.  */
   12928 
   12929 static const char *
   12930 get_dynamic_reloc_section_name (bfd *       abfd,
   12931 				asection *  sec,
   12932 				bfd_boolean is_rela)
   12933 {
   12934   char *name;
   12935   const char *old_name = bfd_get_section_name (NULL, sec);
   12936   const char *prefix = is_rela ? ".rela" : ".rel";
   12937 
   12938   if (old_name == NULL)
   12939     return NULL;
   12940 
   12941   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   12942   sprintf (name, "%s%s", prefix, old_name);
   12943 
   12944   return name;
   12945 }
   12946 
   12947 /* Returns the dynamic reloc section associated with SEC.
   12948    If necessary compute the name of the dynamic reloc section based
   12949    on SEC's name (looked up in ABFD's string table) and the setting
   12950    of IS_RELA.  */
   12951 
   12952 asection *
   12953 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
   12954 				    asection *  sec,
   12955 				    bfd_boolean is_rela)
   12956 {
   12957   asection * reloc_sec = elf_section_data (sec)->sreloc;
   12958 
   12959   if (reloc_sec == NULL)
   12960     {
   12961       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   12962 
   12963       if (name != NULL)
   12964 	{
   12965 	  reloc_sec = bfd_get_linker_section (abfd, name);
   12966 
   12967 	  if (reloc_sec != NULL)
   12968 	    elf_section_data (sec)->sreloc = reloc_sec;
   12969 	}
   12970     }
   12971 
   12972   return reloc_sec;
   12973 }
   12974 
   12975 /* Returns the dynamic reloc section associated with SEC.  If the
   12976    section does not exist it is created and attached to the DYNOBJ
   12977    bfd and stored in the SRELOC field of SEC's elf_section_data
   12978    structure.
   12979 
   12980    ALIGNMENT is the alignment for the newly created section and
   12981    IS_RELA defines whether the name should be .rela.<SEC's name>
   12982    or .rel.<SEC's name>.  The section name is looked up in the
   12983    string table associated with ABFD.  */
   12984 
   12985 asection *
   12986 _bfd_elf_make_dynamic_reloc_section (asection *         sec,
   12987 				     bfd *		dynobj,
   12988 				     unsigned int	alignment,
   12989 				     bfd *              abfd,
   12990 				     bfd_boolean        is_rela)
   12991 {
   12992   asection * reloc_sec = elf_section_data (sec)->sreloc;
   12993 
   12994   if (reloc_sec == NULL)
   12995     {
   12996       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   12997 
   12998       if (name == NULL)
   12999 	return NULL;
   13000 
   13001       reloc_sec = bfd_get_linker_section (dynobj, name);
   13002 
   13003       if (reloc_sec == NULL)
   13004 	{
   13005 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   13006 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   13007 	  if ((sec->flags & SEC_ALLOC) != 0)
   13008 	    flags |= SEC_ALLOC | SEC_LOAD;
   13009 
   13010 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   13011 	  if (reloc_sec != NULL)
   13012 	    {
   13013 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   13014 		 name.  Override as it may be wrong, eg. for a user
   13015 		 section named "auto" we'll get ".relauto" which is
   13016 		 seen to be a .rela section.  */
   13017 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   13018 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
   13019 		reloc_sec = NULL;
   13020 	    }
   13021 	}
   13022 
   13023       elf_section_data (sec)->sreloc = reloc_sec;
   13024     }
   13025 
   13026   return reloc_sec;
   13027 }
   13028 
   13029 /* Copy the ELF symbol type and other attributes for a linker script
   13030    assignment from HSRC to HDEST.  Generally this should be treated as
   13031    if we found a strong non-dynamic definition for HDEST (except that
   13032    ld ignores multiple definition errors).  */
   13033 void
   13034 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   13035 				     struct bfd_link_hash_entry *hdest,
   13036 				     struct bfd_link_hash_entry *hsrc)
   13037 {
   13038   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   13039   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   13040   Elf_Internal_Sym isym;
   13041 
   13042   ehdest->type = ehsrc->type;
   13043   ehdest->target_internal = ehsrc->target_internal;
   13044 
   13045   isym.st_other = ehsrc->other;
   13046   elf_merge_st_other (abfd, ehdest, &isym, TRUE, FALSE);
   13047 }
   13048 
   13049 /* Append a RELA relocation REL to section S in BFD.  */
   13050 
   13051 void
   13052 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   13053 {
   13054   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13055   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   13056   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   13057   bed->s->swap_reloca_out (abfd, rel, loc);
   13058 }
   13059 
   13060 /* Append a REL relocation REL to section S in BFD.  */
   13061 
   13062 void
   13063 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   13064 {
   13065   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13066   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   13067   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   13068   bed->s->swap_reloc_out (abfd, rel, loc);
   13069 }
   13070