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