Home | History | Annotate | Line # | Download | only in bfd
elflink.c revision 1.11.2.1
      1 /* ELF linking support for BFD.
      2    Copyright (C) 1995-2015 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "bfd_stdint.h"
     24 #include "bfdlink.h"
     25 #include "libbfd.h"
     26 #define ARCH_SIZE 0
     27 #include "elf-bfd.h"
     28 #include "safe-ctype.h"
     29 #include "libiberty.h"
     30 #include "objalloc.h"
     31 
     32 /* This struct is used to pass information to routines called via
     33    elf_link_hash_traverse which must return failure.  */
     34 
     35 struct elf_info_failed
     36 {
     37   struct bfd_link_info *info;
     38   bfd_boolean failed;
     39 };
     40 
     41 /* This structure is used to pass information to
     42    _bfd_elf_link_find_version_dependencies.  */
     43 
     44 struct elf_find_verdep_info
     45 {
     46   /* General link information.  */
     47   struct bfd_link_info *info;
     48   /* The number of dependencies.  */
     49   unsigned int vers;
     50   /* Whether we had a failure.  */
     51   bfd_boolean failed;
     52 };
     53 
     54 static bfd_boolean _bfd_elf_fix_symbol_flags
     55   (struct elf_link_hash_entry *, struct elf_info_failed *);
     56 
     57 asection *
     58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
     59 			     unsigned long r_symndx,
     60 			     bfd_boolean discard)
     61 {
     62   if (r_symndx >= cookie->locsymcount
     63       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
     64     {
     65       struct elf_link_hash_entry *h;
     66 
     67       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
     68 
     69       while (h->root.type == bfd_link_hash_indirect
     70 	     || h->root.type == bfd_link_hash_warning)
     71 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
     72 
     73       if ((h->root.type == bfd_link_hash_defined
     74 	   || h->root.type == bfd_link_hash_defweak)
     75 	   && discarded_section (h->root.u.def.section))
     76         return h->root.u.def.section;
     77       else
     78 	return NULL;
     79     }
     80   else
     81     {
     82       /* It's not a relocation against a global symbol,
     83 	 but it could be a relocation against a local
     84 	 symbol for a discarded section.  */
     85       asection *isec;
     86       Elf_Internal_Sym *isym;
     87 
     88       /* Need to: get the symbol; get the section.  */
     89       isym = &cookie->locsyms[r_symndx];
     90       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
     91       if (isec != NULL
     92 	  && discard ? discarded_section (isec) : 1)
     93 	return isec;
     94      }
     95   return NULL;
     96 }
     97 
     98 /* Define a symbol in a dynamic linkage section.  */
     99 
    100 struct elf_link_hash_entry *
    101 _bfd_elf_define_linkage_sym (bfd *abfd,
    102 			     struct bfd_link_info *info,
    103 			     asection *sec,
    104 			     const char *name)
    105 {
    106   struct elf_link_hash_entry *h;
    107   struct bfd_link_hash_entry *bh;
    108   const struct elf_backend_data *bed;
    109 
    110   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
    111   if (h != NULL)
    112     {
    113       /* Zap symbol defined in an as-needed lib that wasn't linked.
    114 	 This is a symptom of a larger problem:  Absolute symbols
    115 	 defined in shared libraries can't be overridden, because we
    116 	 lose the link to the bfd which is via the symbol section.  */
    117       h->root.type = bfd_link_hash_new;
    118     }
    119 
    120   bh = &h->root;
    121   bed = get_elf_backend_data (abfd);
    122   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
    123 					 sec, 0, NULL, FALSE, bed->collect,
    124 					 &bh))
    125     return NULL;
    126   h = (struct elf_link_hash_entry *) bh;
    127   h->def_regular = 1;
    128   h->non_elf = 0;
    129   h->root.linker_def = 1;
    130   h->type = STT_OBJECT;
    131   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    132     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    133 
    134   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    135   return h;
    136 }
    137 
    138 bfd_boolean
    139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    140 {
    141   flagword flags;
    142   asection *s;
    143   struct elf_link_hash_entry *h;
    144   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    145   struct elf_link_hash_table *htab = elf_hash_table (info);
    146 
    147   /* This function may be called more than once.  */
    148   s = bfd_get_linker_section (abfd, ".got");
    149   if (s != NULL)
    150     return TRUE;
    151 
    152   flags = bed->dynamic_sec_flags;
    153 
    154   s = bfd_make_section_anyway_with_flags (abfd,
    155 					  (bed->rela_plts_and_copies_p
    156 					   ? ".rela.got" : ".rel.got"),
    157 					  (bed->dynamic_sec_flags
    158 					   | SEC_READONLY));
    159   if (s == NULL
    160       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    161     return FALSE;
    162   htab->srelgot = s;
    163 
    164   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    165   if (s == NULL
    166       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    167     return FALSE;
    168   htab->sgot = s;
    169 
    170   if (bed->want_got_plt)
    171     {
    172       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    173       if (s == NULL
    174 	  || !bfd_set_section_alignment (abfd, s,
    175 					 bed->s->log_file_align))
    176 	return FALSE;
    177       htab->sgotplt = s;
    178     }
    179 
    180   /* The first bit of the global offset table is the header.  */
    181   s->size += bed->got_header_size;
    182 
    183   if (bed->want_got_sym)
    184     {
    185       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    186 	 (or .got.plt) section.  We don't do this in the linker script
    187 	 because we don't want to define the symbol if we are not creating
    188 	 a global offset table.  */
    189       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    190 				       "_GLOBAL_OFFSET_TABLE_");
    191       elf_hash_table (info)->hgot = h;
    192       if (h == NULL)
    193 	return FALSE;
    194     }
    195 
    196   return TRUE;
    197 }
    198 
    199 /* Create a strtab to hold the dynamic symbol names.  */
    201 static bfd_boolean
    202 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
    203 {
    204   struct elf_link_hash_table *hash_table;
    205 
    206   hash_table = elf_hash_table (info);
    207   if (hash_table->dynobj == NULL)
    208     hash_table->dynobj = abfd;
    209 
    210   if (hash_table->dynstr == NULL)
    211     {
    212       hash_table->dynstr = _bfd_elf_strtab_init ();
    213       if (hash_table->dynstr == NULL)
    214 	return FALSE;
    215     }
    216   return TRUE;
    217 }
    218 
    219 /* Create some sections which will be filled in with dynamic linking
    220    information.  ABFD is an input file which requires dynamic sections
    221    to be created.  The dynamic sections take up virtual memory space
    222    when the final executable is run, so we need to create them before
    223    addresses are assigned to the output sections.  We work out the
    224    actual contents and size of these sections later.  */
    225 
    226 bfd_boolean
    227 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    228 {
    229   flagword flags;
    230   asection *s;
    231   const struct elf_backend_data *bed;
    232   struct elf_link_hash_entry *h;
    233 
    234   if (! is_elf_hash_table (info->hash))
    235     return FALSE;
    236 
    237   if (elf_hash_table (info)->dynamic_sections_created)
    238     return TRUE;
    239 
    240   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
    241     return FALSE;
    242 
    243   abfd = elf_hash_table (info)->dynobj;
    244   bed = get_elf_backend_data (abfd);
    245 
    246   flags = bed->dynamic_sec_flags;
    247 
    248   /* A dynamically linked executable has a .interp section, but a
    249      shared library does not.  */
    250   if (bfd_link_executable (info) && !info->nointerp)
    251     {
    252       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
    253 					      flags | SEC_READONLY);
    254       if (s == NULL)
    255 	return FALSE;
    256     }
    257 
    258   /* Create sections to hold version informations.  These are removed
    259      if they are not needed.  */
    260   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
    261 					  flags | SEC_READONLY);
    262   if (s == NULL
    263       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    264     return FALSE;
    265 
    266   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
    267 					  flags | SEC_READONLY);
    268   if (s == NULL
    269       || ! bfd_set_section_alignment (abfd, s, 1))
    270     return FALSE;
    271 
    272   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
    273 					  flags | SEC_READONLY);
    274   if (s == NULL
    275       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    276     return FALSE;
    277 
    278   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
    279 					  flags | SEC_READONLY);
    280   if (s == NULL
    281       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    282     return FALSE;
    283   elf_hash_table (info)->dynsym = s;
    284 
    285   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
    286 					  flags | SEC_READONLY);
    287   if (s == NULL)
    288     return FALSE;
    289 
    290   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
    291   if (s == NULL
    292       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    293     return FALSE;
    294 
    295   /* The special symbol _DYNAMIC is always set to the start of the
    296      .dynamic section.  We could set _DYNAMIC in a linker script, but we
    297      only want to define it if we are, in fact, creating a .dynamic
    298      section.  We don't want to define it if there is no .dynamic
    299      section, since on some ELF platforms the start up code examines it
    300      to decide how to initialize the process.  */
    301   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
    302   elf_hash_table (info)->hdynamic = h;
    303   if (h == NULL)
    304     return FALSE;
    305 
    306   if (info->emit_hash)
    307     {
    308       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
    309 					      flags | SEC_READONLY);
    310       if (s == NULL
    311 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    312 	return FALSE;
    313       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
    314     }
    315 
    316   if (info->emit_gnu_hash)
    317     {
    318       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
    319 					      flags | SEC_READONLY);
    320       if (s == NULL
    321 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    322 	return FALSE;
    323       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
    324 	 4 32-bit words followed by variable count of 64-bit words, then
    325 	 variable count of 32-bit words.  */
    326       if (bed->s->arch_size == 64)
    327 	elf_section_data (s)->this_hdr.sh_entsize = 0;
    328       else
    329 	elf_section_data (s)->this_hdr.sh_entsize = 4;
    330     }
    331 
    332   /* Let the backend create the rest of the sections.  This lets the
    333      backend set the right flags.  The backend will normally create
    334      the .got and .plt sections.  */
    335   if (bed->elf_backend_create_dynamic_sections == NULL
    336       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
    337     return FALSE;
    338 
    339   elf_hash_table (info)->dynamic_sections_created = TRUE;
    340 
    341   return TRUE;
    342 }
    343 
    344 /* Create dynamic sections when linking against a dynamic object.  */
    345 
    346 bfd_boolean
    347 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
    348 {
    349   flagword flags, pltflags;
    350   struct elf_link_hash_entry *h;
    351   asection *s;
    352   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    353   struct elf_link_hash_table *htab = elf_hash_table (info);
    354 
    355   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
    356      .rel[a].bss sections.  */
    357   flags = bed->dynamic_sec_flags;
    358 
    359   pltflags = flags;
    360   if (bed->plt_not_loaded)
    361     /* We do not clear SEC_ALLOC here because we still want the OS to
    362        allocate space for the section; it's just that there's nothing
    363        to read in from the object file.  */
    364     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    365   else
    366     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    367   if (bed->plt_readonly)
    368     pltflags |= SEC_READONLY;
    369 
    370   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
    371   if (s == NULL
    372       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    373     return FALSE;
    374   htab->splt = s;
    375 
    376   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    377      .plt section.  */
    378   if (bed->want_plt_sym)
    379     {
    380       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    381 				       "_PROCEDURE_LINKAGE_TABLE_");
    382       elf_hash_table (info)->hplt = h;
    383       if (h == NULL)
    384 	return FALSE;
    385     }
    386 
    387   s = bfd_make_section_anyway_with_flags (abfd,
    388 					  (bed->rela_plts_and_copies_p
    389 					   ? ".rela.plt" : ".rel.plt"),
    390 					  flags | SEC_READONLY);
    391   if (s == NULL
    392       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    393     return FALSE;
    394   htab->srelplt = s;
    395 
    396   if (! _bfd_elf_create_got_section (abfd, info))
    397     return FALSE;
    398 
    399   if (bed->want_dynbss)
    400     {
    401       /* The .dynbss section is a place to put symbols which are defined
    402 	 by dynamic objects, are referenced by regular objects, and are
    403 	 not functions.  We must allocate space for them in the process
    404 	 image and use a R_*_COPY reloc to tell the dynamic linker to
    405 	 initialize them at run time.  The linker script puts the .dynbss
    406 	 section into the .bss section of the final image.  */
    407       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
    408 					      (SEC_ALLOC | SEC_LINKER_CREATED));
    409       if (s == NULL)
    410 	return FALSE;
    411 
    412       /* The .rel[a].bss section holds copy relocs.  This section is not
    413 	 normally needed.  We need to create it here, though, so that the
    414 	 linker will map it to an output section.  We can't just create it
    415 	 only if we need it, because we will not know whether we need it
    416 	 until we have seen all the input files, and the first time the
    417 	 main linker code calls BFD after examining all the input files
    418 	 (size_dynamic_sections) the input sections have already been
    419 	 mapped to the output sections.  If the section turns out not to
    420 	 be needed, we can discard it later.  We will never need this
    421 	 section when generating a shared object, since they do not use
    422 	 copy relocs.  */
    423       if (! bfd_link_pic (info))
    424 	{
    425 	  s = bfd_make_section_anyway_with_flags (abfd,
    426 						  (bed->rela_plts_and_copies_p
    427 						   ? ".rela.bss" : ".rel.bss"),
    428 						  flags | SEC_READONLY);
    429 	  if (s == NULL
    430 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    431 	    return FALSE;
    432 	}
    433     }
    434 
    435   return TRUE;
    436 }
    437 
    438 /* Record a new dynamic symbol.  We record the dynamic symbols as we
    440    read the input files, since we need to have a list of all of them
    441    before we can determine the final sizes of the output sections.
    442    Note that we may actually call this function even though we are not
    443    going to output any dynamic symbols; in some cases we know that a
    444    symbol should be in the dynamic symbol table, but only if there is
    445    one.  */
    446 
    447 bfd_boolean
    448 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
    449 				    struct elf_link_hash_entry *h)
    450 {
    451   if (h->dynindx == -1)
    452     {
    453       struct elf_strtab_hash *dynstr;
    454       char *p;
    455       const char *name;
    456       bfd_size_type indx;
    457 
    458       /* XXX: The ABI draft says the linker must turn hidden and
    459 	 internal symbols into STB_LOCAL symbols when producing the
    460 	 DSO. However, if ld.so honors st_other in the dynamic table,
    461 	 this would not be necessary.  */
    462       switch (ELF_ST_VISIBILITY (h->other))
    463 	{
    464 	case STV_INTERNAL:
    465 	case STV_HIDDEN:
    466 	  if (h->root.type != bfd_link_hash_undefined
    467 	      && h->root.type != bfd_link_hash_undefweak)
    468 	    {
    469 	      h->forced_local = 1;
    470 	      if (!elf_hash_table (info)->is_relocatable_executable)
    471 		return TRUE;
    472 	    }
    473 
    474 	default:
    475 	  break;
    476 	}
    477 
    478       h->dynindx = elf_hash_table (info)->dynsymcount;
    479       ++elf_hash_table (info)->dynsymcount;
    480 
    481       dynstr = elf_hash_table (info)->dynstr;
    482       if (dynstr == NULL)
    483 	{
    484 	  /* Create a strtab to hold the dynamic symbol names.  */
    485 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    486 	  if (dynstr == NULL)
    487 	    return FALSE;
    488 	}
    489 
    490       /* We don't put any version information in the dynamic string
    491 	 table.  */
    492       name = h->root.root.string;
    493       p = strchr (name, ELF_VER_CHR);
    494       if (p != NULL)
    495 	/* We know that the p points into writable memory.  In fact,
    496 	   there are only a few symbols that have read-only names, being
    497 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
    498 	   by the backends.  Most symbols will have names pointing into
    499 	   an ELF string table read from a file, or to objalloc memory.  */
    500 	*p = 0;
    501 
    502       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
    503 
    504       if (p != NULL)
    505 	*p = ELF_VER_CHR;
    506 
    507       if (indx == (bfd_size_type) -1)
    508 	return FALSE;
    509       h->dynstr_index = indx;
    510     }
    511 
    512   return TRUE;
    513 }
    514 
    515 /* Mark a symbol dynamic.  */
    517 
    518 static void
    519 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
    520 				  struct elf_link_hash_entry *h,
    521 				  Elf_Internal_Sym *sym)
    522 {
    523   struct bfd_elf_dynamic_list *d = info->dynamic_list;
    524 
    525   /* It may be called more than once on the same H.  */
    526   if(h->dynamic || bfd_link_relocatable (info))
    527     return;
    528 
    529   if ((info->dynamic_data
    530        && (h->type == STT_OBJECT
    531 	   || (sym != NULL
    532 	       && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
    533       || (d != NULL
    534 	  && h->root.type == bfd_link_hash_new
    535 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
    536     h->dynamic = 1;
    537 }
    538 
    539 /* Record an assignment to a symbol made by a linker script.  We need
    540    this in case some dynamic object refers to this symbol.  */
    541 
    542 bfd_boolean
    543 bfd_elf_record_link_assignment (bfd *output_bfd,
    544 				struct bfd_link_info *info,
    545 				const char *name,
    546 				bfd_boolean provide,
    547 				bfd_boolean hidden)
    548 {
    549   struct elf_link_hash_entry *h, *hv;
    550   struct elf_link_hash_table *htab;
    551   const struct elf_backend_data *bed;
    552 
    553   if (!is_elf_hash_table (info->hash))
    554     return TRUE;
    555 
    556   htab = elf_hash_table (info);
    557   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
    558   if (h == NULL)
    559     return provide;
    560 
    561   if (h->versioned == unknown)
    562     {
    563       /* Set versioned if symbol version is unknown.  */
    564       char *version = strrchr (name, ELF_VER_CHR);
    565       if (version)
    566 	{
    567 	  if (version > name && version[-1] != ELF_VER_CHR)
    568 	    h->versioned = versioned_hidden;
    569 	  else
    570 	    h->versioned = versioned;
    571 	}
    572     }
    573 
    574   switch (h->root.type)
    575     {
    576     case bfd_link_hash_defined:
    577     case bfd_link_hash_defweak:
    578     case bfd_link_hash_common:
    579       break;
    580     case bfd_link_hash_undefweak:
    581     case bfd_link_hash_undefined:
    582       /* Since we're defining the symbol, don't let it seem to have not
    583 	 been defined.  record_dynamic_symbol and size_dynamic_sections
    584 	 may depend on this.  */
    585       h->root.type = bfd_link_hash_new;
    586       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
    587 	bfd_link_repair_undef_list (&htab->root);
    588       break;
    589     case bfd_link_hash_new:
    590       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
    591       h->non_elf = 0;
    592       break;
    593     case bfd_link_hash_indirect:
    594       /* We had a versioned symbol in a dynamic library.  We make the
    595 	 the versioned symbol point to this one.  */
    596       bed = get_elf_backend_data (output_bfd);
    597       hv = h;
    598       while (hv->root.type == bfd_link_hash_indirect
    599 	     || hv->root.type == bfd_link_hash_warning)
    600 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
    601       /* We don't need to update h->root.u since linker will set them
    602 	 later.  */
    603       h->root.type = bfd_link_hash_undefined;
    604       hv->root.type = bfd_link_hash_indirect;
    605       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
    606       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
    607       break;
    608     case bfd_link_hash_warning:
    609       abort ();
    610       break;
    611     }
    612 
    613   /* If this symbol is being provided by the linker script, and it is
    614      currently defined by a dynamic object, but not by a regular
    615      object, then mark it as undefined so that the generic linker will
    616      force the correct value.  */
    617   if (provide
    618       && h->def_dynamic
    619       && !h->def_regular)
    620     h->root.type = bfd_link_hash_undefined;
    621 
    622   /* If this symbol is not being provided by the linker script, and it is
    623      currently defined by a dynamic object, but not by a regular object,
    624      then clear out any version information because the symbol will not be
    625      associated with the dynamic object any more.  */
    626   if (!provide
    627       && h->def_dynamic
    628       && !h->def_regular)
    629     h->verinfo.verdef = NULL;
    630 
    631   h->def_regular = 1;
    632 
    633   if (hidden)
    634     {
    635       bed = get_elf_backend_data (output_bfd);
    636       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
    637 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
    638       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
    639     }
    640 
    641   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
    642      and executables.  */
    643   if (!bfd_link_relocatable (info)
    644       && h->dynindx != -1
    645       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
    646 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
    647     h->forced_local = 1;
    648 
    649   if ((h->def_dynamic
    650        || h->ref_dynamic
    651        || bfd_link_pic (info)
    652        || (bfd_link_pde (info)
    653 	   && elf_hash_table (info)->is_relocatable_executable))
    654       && h->dynindx == -1)
    655     {
    656       if (! bfd_elf_link_record_dynamic_symbol (info, h))
    657 	return FALSE;
    658 
    659       /* If this is a weak defined symbol, and we know a corresponding
    660 	 real symbol from the same dynamic object, make sure the real
    661 	 symbol is also made into a dynamic symbol.  */
    662       if (h->u.weakdef != NULL
    663 	  && h->u.weakdef->dynindx == -1)
    664 	{
    665 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
    666 	    return FALSE;
    667 	}
    668     }
    669 
    670   return TRUE;
    671 }
    672 
    673 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
    674    success, and 2 on a failure caused by attempting to record a symbol
    675    in a discarded section, eg. a discarded link-once section symbol.  */
    676 
    677 int
    678 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
    679 					  bfd *input_bfd,
    680 					  long input_indx)
    681 {
    682   bfd_size_type amt;
    683   struct elf_link_local_dynamic_entry *entry;
    684   struct elf_link_hash_table *eht;
    685   struct elf_strtab_hash *dynstr;
    686   unsigned long dynstr_index;
    687   char *name;
    688   Elf_External_Sym_Shndx eshndx;
    689   char esym[sizeof (Elf64_External_Sym)];
    690 
    691   if (! is_elf_hash_table (info->hash))
    692     return 0;
    693 
    694   /* See if the entry exists already.  */
    695   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
    696     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
    697       return 1;
    698 
    699   amt = sizeof (*entry);
    700   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
    701   if (entry == NULL)
    702     return 0;
    703 
    704   /* Go find the symbol, so that we can find it's name.  */
    705   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
    706 			     1, input_indx, &entry->isym, esym, &eshndx))
    707     {
    708       bfd_release (input_bfd, entry);
    709       return 0;
    710     }
    711 
    712   if (entry->isym.st_shndx != SHN_UNDEF
    713       && entry->isym.st_shndx < SHN_LORESERVE)
    714     {
    715       asection *s;
    716 
    717       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
    718       if (s == NULL || bfd_is_abs_section (s->output_section))
    719 	{
    720 	  /* We can still bfd_release here as nothing has done another
    721 	     bfd_alloc.  We can't do this later in this function.  */
    722 	  bfd_release (input_bfd, entry);
    723 	  return 2;
    724 	}
    725     }
    726 
    727   name = (bfd_elf_string_from_elf_section
    728 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
    729 	   entry->isym.st_name));
    730 
    731   dynstr = elf_hash_table (info)->dynstr;
    732   if (dynstr == NULL)
    733     {
    734       /* Create a strtab to hold the dynamic symbol names.  */
    735       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    736       if (dynstr == NULL)
    737 	return 0;
    738     }
    739 
    740   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
    741   if (dynstr_index == (unsigned long) -1)
    742     return 0;
    743   entry->isym.st_name = dynstr_index;
    744 
    745   eht = elf_hash_table (info);
    746 
    747   entry->next = eht->dynlocal;
    748   eht->dynlocal = entry;
    749   entry->input_bfd = input_bfd;
    750   entry->input_indx = input_indx;
    751   eht->dynsymcount++;
    752 
    753   /* Whatever binding the symbol had before, it's now local.  */
    754   entry->isym.st_info
    755     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
    756 
    757   /* The dynindx will be set at the end of size_dynamic_sections.  */
    758 
    759   return 1;
    760 }
    761 
    762 /* Return the dynindex of a local dynamic symbol.  */
    763 
    764 long
    765 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
    766 				    bfd *input_bfd,
    767 				    long input_indx)
    768 {
    769   struct elf_link_local_dynamic_entry *e;
    770 
    771   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
    772     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
    773       return e->dynindx;
    774   return -1;
    775 }
    776 
    777 /* This function is used to renumber the dynamic symbols, if some of
    778    them are removed because they are marked as local.  This is called
    779    via elf_link_hash_traverse.  */
    780 
    781 static bfd_boolean
    782 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
    783 				      void *data)
    784 {
    785   size_t *count = (size_t *) data;
    786 
    787   if (h->forced_local)
    788     return TRUE;
    789 
    790   if (h->dynindx != -1)
    791     h->dynindx = ++(*count);
    792 
    793   return TRUE;
    794 }
    795 
    796 
    797 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
    798    STB_LOCAL binding.  */
    799 
    800 static bfd_boolean
    801 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
    802 					    void *data)
    803 {
    804   size_t *count = (size_t *) data;
    805 
    806   if (!h->forced_local)
    807     return TRUE;
    808 
    809   if (h->dynindx != -1)
    810     h->dynindx = ++(*count);
    811 
    812   return TRUE;
    813 }
    814 
    815 /* Return true if the dynamic symbol for a given section should be
    816    omitted when creating a shared library.  */
    817 bfd_boolean
    818 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
    819 				   struct bfd_link_info *info,
    820 				   asection *p)
    821 {
    822   struct elf_link_hash_table *htab;
    823   asection *ip;
    824 
    825   switch (elf_section_data (p)->this_hdr.sh_type)
    826     {
    827     case SHT_PROGBITS:
    828     case SHT_NOBITS:
    829       /* If sh_type is yet undecided, assume it could be
    830 	 SHT_PROGBITS/SHT_NOBITS.  */
    831     case SHT_NULL:
    832       htab = elf_hash_table (info);
    833       if (p == htab->tls_sec)
    834 	return FALSE;
    835 
    836       if (htab->text_index_section != NULL)
    837 	return p != htab->text_index_section && p != htab->data_index_section;
    838 
    839       return (htab->dynobj != NULL
    840 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
    841 	      && ip->output_section == p);
    842 
    843       /* There shouldn't be section relative relocations
    844 	 against any other section.  */
    845     default:
    846       return TRUE;
    847     }
    848 }
    849 
    850 /* Assign dynsym indices.  In a shared library we generate a section
    851    symbol for each output section, which come first.  Next come symbols
    852    which have been forced to local binding.  Then all of the back-end
    853    allocated local dynamic syms, followed by the rest of the global
    854    symbols.  */
    855 
    856 static unsigned long
    857 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
    858 				struct bfd_link_info *info,
    859 				unsigned long *section_sym_count)
    860 {
    861   unsigned long dynsymcount = 0;
    862 
    863   if (bfd_link_pic (info)
    864       || elf_hash_table (info)->is_relocatable_executable)
    865     {
    866       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
    867       asection *p;
    868       for (p = output_bfd->sections; p ; p = p->next)
    869 	if ((p->flags & SEC_EXCLUDE) == 0
    870 	    && (p->flags & SEC_ALLOC) != 0
    871 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
    872 	  elf_section_data (p)->dynindx = ++dynsymcount;
    873 	else
    874 	  elf_section_data (p)->dynindx = 0;
    875     }
    876   *section_sym_count = dynsymcount;
    877 
    878   elf_link_hash_traverse (elf_hash_table (info),
    879 			  elf_link_renumber_local_hash_table_dynsyms,
    880 			  &dynsymcount);
    881 
    882   if (elf_hash_table (info)->dynlocal)
    883     {
    884       struct elf_link_local_dynamic_entry *p;
    885       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
    886 	p->dynindx = ++dynsymcount;
    887     }
    888 
    889   elf_link_hash_traverse (elf_hash_table (info),
    890 			  elf_link_renumber_hash_table_dynsyms,
    891 			  &dynsymcount);
    892 
    893   /* There is an unused NULL entry at the head of the table which
    894      we must account for in our count.  Unless there weren't any
    895      symbols, which means we'll have no table at all.  */
    896   if (dynsymcount != 0)
    897     ++dynsymcount;
    898 
    899   elf_hash_table (info)->dynsymcount = dynsymcount;
    900   return dynsymcount;
    901 }
    902 
    903 /* Merge st_other field.  */
    904 
    905 static void
    906 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
    907 		    const Elf_Internal_Sym *isym, asection *sec,
    908 		    bfd_boolean definition, bfd_boolean dynamic)
    909 {
    910   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    911 
    912   /* If st_other has a processor-specific meaning, specific
    913      code might be needed here.  */
    914   if (bed->elf_backend_merge_symbol_attribute)
    915     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
    916 						dynamic);
    917 
    918   if (!dynamic)
    919     {
    920       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
    921       unsigned hvis = ELF_ST_VISIBILITY (h->other);
    922 
    923       /* Keep the most constraining visibility.  Leave the remainder
    924 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
    925       if (symvis - 1 < hvis - 1)
    926 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
    927     }
    928   else if (definition
    929 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
    930 	   && (sec->flags & SEC_READONLY) == 0)
    931     h->protected_def = 1;
    932 }
    933 
    934 /* This function is called when we want to merge a new symbol with an
    935    existing symbol.  It handles the various cases which arise when we
    936    find a definition in a dynamic object, or when there is already a
    937    definition in a dynamic object.  The new symbol is described by
    938    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
    939    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
    940    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
    941    of an old common symbol.  We set OVERRIDE if the old symbol is
    942    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
    943    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
    944    to change.  By OK to change, we mean that we shouldn't warn if the
    945    type or size does change.  */
    946 
    947 static bfd_boolean
    948 _bfd_elf_merge_symbol (bfd *abfd,
    949 		       struct bfd_link_info *info,
    950 		       const char *name,
    951 		       Elf_Internal_Sym *sym,
    952 		       asection **psec,
    953 		       bfd_vma *pvalue,
    954 		       struct elf_link_hash_entry **sym_hash,
    955 		       bfd **poldbfd,
    956 		       bfd_boolean *pold_weak,
    957 		       unsigned int *pold_alignment,
    958 		       bfd_boolean *skip,
    959 		       bfd_boolean *override,
    960 		       bfd_boolean *type_change_ok,
    961 		       bfd_boolean *size_change_ok,
    962 		       bfd_boolean *matched)
    963 {
    964   asection *sec, *oldsec;
    965   struct elf_link_hash_entry *h;
    966   struct elf_link_hash_entry *hi;
    967   struct elf_link_hash_entry *flip;
    968   int bind;
    969   bfd *oldbfd;
    970   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
    971   bfd_boolean newweak, oldweak, newfunc, oldfunc;
    972   const struct elf_backend_data *bed;
    973   char *new_version;
    974 
    975   *skip = FALSE;
    976   *override = FALSE;
    977 
    978   sec = *psec;
    979   bind = ELF_ST_BIND (sym->st_info);
    980 
    981   if (! bfd_is_und_section (sec))
    982     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
    983   else
    984     h = ((struct elf_link_hash_entry *)
    985 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
    986   if (h == NULL)
    987     return FALSE;
    988   *sym_hash = h;
    989 
    990   bed = get_elf_backend_data (abfd);
    991 
    992   /* NEW_VERSION is the symbol version of the new symbol.  */
    993   if (h->versioned != unversioned)
    994     {
    995       /* Symbol version is unknown or versioned.  */
    996       new_version = strrchr (name, ELF_VER_CHR);
    997       if (new_version)
    998 	{
    999 	  if (h->versioned == unknown)
   1000 	    {
   1001 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
   1002 		h->versioned = versioned_hidden;
   1003 	      else
   1004 		h->versioned = versioned;
   1005 	    }
   1006 	  new_version += 1;
   1007 	  if (new_version[0] == '\0')
   1008 	    new_version = NULL;
   1009 	}
   1010       else
   1011 	h->versioned = unversioned;
   1012     }
   1013   else
   1014     new_version = NULL;
   1015 
   1016   /* For merging, we only care about real symbols.  But we need to make
   1017      sure that indirect symbol dynamic flags are updated.  */
   1018   hi = h;
   1019   while (h->root.type == bfd_link_hash_indirect
   1020 	 || h->root.type == bfd_link_hash_warning)
   1021     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1022 
   1023   if (!*matched)
   1024     {
   1025       if (hi == h || h->root.type == bfd_link_hash_new)
   1026 	*matched = TRUE;
   1027       else
   1028 	{
   1029 	  /* OLD_HIDDEN is true if the existing symbol is only visible
   1030 	     to the symbol with the same symbol version.  NEW_HIDDEN is
   1031 	     true if the new symbol is only visible to the symbol with
   1032 	     the same symbol version.  */
   1033 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
   1034 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
   1035 	  if (!old_hidden && !new_hidden)
   1036 	    /* The new symbol matches the existing symbol if both
   1037 	       aren't hidden.  */
   1038 	    *matched = TRUE;
   1039 	  else
   1040 	    {
   1041 	      /* OLD_VERSION is the symbol version of the existing
   1042 		 symbol. */
   1043 	      char *old_version;
   1044 
   1045 	      if (h->versioned >= versioned)
   1046 		old_version = strrchr (h->root.root.string,
   1047 				       ELF_VER_CHR) + 1;
   1048 	      else
   1049 		 old_version = NULL;
   1050 
   1051 	      /* The new symbol matches the existing symbol if they
   1052 		 have the same symbol version.  */
   1053 	      *matched = (old_version == new_version
   1054 			  || (old_version != NULL
   1055 			      && new_version != NULL
   1056 			      && strcmp (old_version, new_version) == 0));
   1057 	    }
   1058 	}
   1059     }
   1060 
   1061   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
   1062      existing symbol.  */
   1063 
   1064   oldbfd = NULL;
   1065   oldsec = NULL;
   1066   switch (h->root.type)
   1067     {
   1068     default:
   1069       break;
   1070 
   1071     case bfd_link_hash_undefined:
   1072     case bfd_link_hash_undefweak:
   1073       oldbfd = h->root.u.undef.abfd;
   1074       break;
   1075 
   1076     case bfd_link_hash_defined:
   1077     case bfd_link_hash_defweak:
   1078       oldbfd = h->root.u.def.section->owner;
   1079       oldsec = h->root.u.def.section;
   1080       break;
   1081 
   1082     case bfd_link_hash_common:
   1083       oldbfd = h->root.u.c.p->section->owner;
   1084       oldsec = h->root.u.c.p->section;
   1085       if (pold_alignment)
   1086 	*pold_alignment = h->root.u.c.p->alignment_power;
   1087       break;
   1088     }
   1089   if (poldbfd && *poldbfd == NULL)
   1090     *poldbfd = oldbfd;
   1091 
   1092   /* Differentiate strong and weak symbols.  */
   1093   newweak = bind == STB_WEAK;
   1094   oldweak = (h->root.type == bfd_link_hash_defweak
   1095 	     || h->root.type == bfd_link_hash_undefweak);
   1096   if (pold_weak)
   1097     *pold_weak = oldweak;
   1098 
   1099   /* This code is for coping with dynamic objects, and is only useful
   1100      if we are doing an ELF link.  */
   1101   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   1102     return TRUE;
   1103 
   1104   /* We have to check it for every instance since the first few may be
   1105      references and not all compilers emit symbol type for undefined
   1106      symbols.  */
   1107   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
   1108 
   1109   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
   1110      respectively, is from a dynamic object.  */
   1111 
   1112   newdyn = (abfd->flags & DYNAMIC) != 0;
   1113 
   1114   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
   1115      syms and defined syms in dynamic libraries respectively.
   1116      ref_dynamic on the other hand can be set for a symbol defined in
   1117      a dynamic library, and def_dynamic may not be set;  When the
   1118      definition in a dynamic lib is overridden by a definition in the
   1119      executable use of the symbol in the dynamic lib becomes a
   1120      reference to the executable symbol.  */
   1121   if (newdyn)
   1122     {
   1123       if (bfd_is_und_section (sec))
   1124 	{
   1125 	  if (bind != STB_WEAK)
   1126 	    {
   1127 	      h->ref_dynamic_nonweak = 1;
   1128 	      hi->ref_dynamic_nonweak = 1;
   1129 	    }
   1130 	}
   1131       else
   1132 	{
   1133 	  /* Update the existing symbol only if they match. */
   1134 	  if (*matched)
   1135 	    h->dynamic_def = 1;
   1136 	  hi->dynamic_def = 1;
   1137 	}
   1138     }
   1139 
   1140   /* If we just created the symbol, mark it as being an ELF symbol.
   1141      Other than that, there is nothing to do--there is no merge issue
   1142      with a newly defined symbol--so we just return.  */
   1143 
   1144   if (h->root.type == bfd_link_hash_new)
   1145     {
   1146       h->non_elf = 0;
   1147       return TRUE;
   1148     }
   1149 
   1150   /* In cases involving weak versioned symbols, we may wind up trying
   1151      to merge a symbol with itself.  Catch that here, to avoid the
   1152      confusion that results if we try to override a symbol with
   1153      itself.  The additional tests catch cases like
   1154      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
   1155      dynamic object, which we do want to handle here.  */
   1156   if (abfd == oldbfd
   1157       && (newweak || oldweak)
   1158       && ((abfd->flags & DYNAMIC) == 0
   1159 	  || !h->def_regular))
   1160     return TRUE;
   1161 
   1162   olddyn = FALSE;
   1163   if (oldbfd != NULL)
   1164     olddyn = (oldbfd->flags & DYNAMIC) != 0;
   1165   else if (oldsec != NULL)
   1166     {
   1167       /* This handles the special SHN_MIPS_{TEXT,DATA} section
   1168 	 indices used by MIPS ELF.  */
   1169       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
   1170     }
   1171 
   1172   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
   1173      respectively, appear to be a definition rather than reference.  */
   1174 
   1175   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
   1176 
   1177   olddef = (h->root.type != bfd_link_hash_undefined
   1178 	    && h->root.type != bfd_link_hash_undefweak
   1179 	    && h->root.type != bfd_link_hash_common);
   1180 
   1181   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
   1182      respectively, appear to be a function.  */
   1183 
   1184   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1185 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
   1186 
   1187   oldfunc = (h->type != STT_NOTYPE
   1188 	     && bed->is_function_type (h->type));
   1189 
   1190   /* If creating a default indirect symbol ("foo" or "foo@") from a
   1191      dynamic versioned definition ("foo@@") skip doing so if there is
   1192      an existing regular definition with a different type.  We don't
   1193      want, for example, a "time" variable in the executable overriding
   1194      a "time" function in a shared library.  */
   1195   if (pold_alignment == NULL
   1196       && newdyn
   1197       && newdef
   1198       && !olddyn
   1199       && (olddef || h->root.type == bfd_link_hash_common)
   1200       && ELF_ST_TYPE (sym->st_info) != h->type
   1201       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
   1202       && h->type != STT_NOTYPE
   1203       && !(newfunc && oldfunc))
   1204     {
   1205       *skip = TRUE;
   1206       return TRUE;
   1207     }
   1208 
   1209   /* Check TLS symbols.  We don't check undefined symbols introduced
   1210      by "ld -u" which have no type (and oldbfd NULL), and we don't
   1211      check symbols from plugins because they also have no type.  */
   1212   if (oldbfd != NULL
   1213       && (oldbfd->flags & BFD_PLUGIN) == 0
   1214       && (abfd->flags & BFD_PLUGIN) == 0
   1215       && ELF_ST_TYPE (sym->st_info) != h->type
   1216       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
   1217     {
   1218       bfd *ntbfd, *tbfd;
   1219       bfd_boolean ntdef, tdef;
   1220       asection *ntsec, *tsec;
   1221 
   1222       if (h->type == STT_TLS)
   1223 	{
   1224 	  ntbfd = abfd;
   1225 	  ntsec = sec;
   1226 	  ntdef = newdef;
   1227 	  tbfd = oldbfd;
   1228 	  tsec = oldsec;
   1229 	  tdef = olddef;
   1230 	}
   1231       else
   1232 	{
   1233 	  ntbfd = oldbfd;
   1234 	  ntsec = oldsec;
   1235 	  ntdef = olddef;
   1236 	  tbfd = abfd;
   1237 	  tsec = sec;
   1238 	  tdef = newdef;
   1239 	}
   1240 
   1241       if (tdef && ntdef)
   1242 	(*_bfd_error_handler)
   1243 	  (_("%s: TLS definition in %B section %A "
   1244 	     "mismatches non-TLS definition in %B section %A"),
   1245 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
   1246       else if (!tdef && !ntdef)
   1247 	(*_bfd_error_handler)
   1248 	  (_("%s: TLS reference in %B "
   1249 	     "mismatches non-TLS reference in %B"),
   1250 	   tbfd, ntbfd, h->root.root.string);
   1251       else if (tdef)
   1252 	(*_bfd_error_handler)
   1253 	  (_("%s: TLS definition in %B section %A "
   1254 	     "mismatches non-TLS reference in %B"),
   1255 	   tbfd, tsec, ntbfd, h->root.root.string);
   1256       else
   1257 	(*_bfd_error_handler)
   1258 	  (_("%s: TLS reference in %B "
   1259 	     "mismatches non-TLS definition in %B section %A"),
   1260 	   tbfd, ntbfd, ntsec, h->root.root.string);
   1261 
   1262       bfd_set_error (bfd_error_bad_value);
   1263       return FALSE;
   1264     }
   1265 
   1266   /* If the old symbol has non-default visibility, we ignore the new
   1267      definition from a dynamic object.  */
   1268   if (newdyn
   1269       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1270       && !bfd_is_und_section (sec))
   1271     {
   1272       *skip = TRUE;
   1273       /* Make sure this symbol is dynamic.  */
   1274       h->ref_dynamic = 1;
   1275       hi->ref_dynamic = 1;
   1276       /* A protected symbol has external availability. Make sure it is
   1277 	 recorded as dynamic.
   1278 
   1279 	 FIXME: Should we check type and size for protected symbol?  */
   1280       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   1281 	return bfd_elf_link_record_dynamic_symbol (info, h);
   1282       else
   1283 	return TRUE;
   1284     }
   1285   else if (!newdyn
   1286 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
   1287 	   && h->def_dynamic)
   1288     {
   1289       /* If the new symbol with non-default visibility comes from a
   1290 	 relocatable file and the old definition comes from a dynamic
   1291 	 object, we remove the old definition.  */
   1292       if (hi->root.type == bfd_link_hash_indirect)
   1293 	{
   1294 	  /* Handle the case where the old dynamic definition is
   1295 	     default versioned.  We need to copy the symbol info from
   1296 	     the symbol with default version to the normal one if it
   1297 	     was referenced before.  */
   1298 	  if (h->ref_regular)
   1299 	    {
   1300 	      hi->root.type = h->root.type;
   1301 	      h->root.type = bfd_link_hash_indirect;
   1302 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
   1303 
   1304 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1305 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1306 		{
   1307 		  /* If the new symbol is hidden or internal, completely undo
   1308 		     any dynamic link state.  */
   1309 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1310 		  h->forced_local = 0;
   1311 		  h->ref_dynamic = 0;
   1312 		}
   1313 	      else
   1314 		h->ref_dynamic = 1;
   1315 
   1316 	      h->def_dynamic = 0;
   1317 	      /* FIXME: Should we check type and size for protected symbol?  */
   1318 	      h->size = 0;
   1319 	      h->type = 0;
   1320 
   1321 	      h = hi;
   1322 	    }
   1323 	  else
   1324 	    h = hi;
   1325 	}
   1326 
   1327       /* If the old symbol was undefined before, then it will still be
   1328 	 on the undefs list.  If the new symbol is undefined or
   1329 	 common, we can't make it bfd_link_hash_new here, because new
   1330 	 undefined or common symbols will be added to the undefs list
   1331 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
   1332 	 added twice to the undefs list.  Also, if the new symbol is
   1333 	 undefweak then we don't want to lose the strong undef.  */
   1334       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
   1335 	{
   1336 	  h->root.type = bfd_link_hash_undefined;
   1337 	  h->root.u.undef.abfd = abfd;
   1338 	}
   1339       else
   1340 	{
   1341 	  h->root.type = bfd_link_hash_new;
   1342 	  h->root.u.undef.abfd = NULL;
   1343 	}
   1344 
   1345       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
   1346 	{
   1347 	  /* If the new symbol is hidden or internal, completely undo
   1348 	     any dynamic link state.  */
   1349 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1350 	  h->forced_local = 0;
   1351 	  h->ref_dynamic = 0;
   1352 	}
   1353       else
   1354 	h->ref_dynamic = 1;
   1355       h->def_dynamic = 0;
   1356       /* FIXME: Should we check type and size for protected symbol?  */
   1357       h->size = 0;
   1358       h->type = 0;
   1359       return TRUE;
   1360     }
   1361 
   1362   /* If a new weak symbol definition comes from a regular file and the
   1363      old symbol comes from a dynamic library, we treat the new one as
   1364      strong.  Similarly, an old weak symbol definition from a regular
   1365      file is treated as strong when the new symbol comes from a dynamic
   1366      library.  Further, an old weak symbol from a dynamic library is
   1367      treated as strong if the new symbol is from a dynamic library.
   1368      This reflects the way glibc's ld.so works.
   1369 
   1370      Do this before setting *type_change_ok or *size_change_ok so that
   1371      we warn properly when dynamic library symbols are overridden.  */
   1372 
   1373   if (newdef && !newdyn && olddyn)
   1374     newweak = FALSE;
   1375   if (olddef && newdyn)
   1376     oldweak = FALSE;
   1377 
   1378   /* Allow changes between different types of function symbol.  */
   1379   if (newfunc && oldfunc)
   1380     *type_change_ok = TRUE;
   1381 
   1382   /* It's OK to change the type if either the existing symbol or the
   1383      new symbol is weak.  A type change is also OK if the old symbol
   1384      is undefined and the new symbol is defined.  */
   1385 
   1386   if (oldweak
   1387       || newweak
   1388       || (newdef
   1389 	  && h->root.type == bfd_link_hash_undefined))
   1390     *type_change_ok = TRUE;
   1391 
   1392   /* It's OK to change the size if either the existing symbol or the
   1393      new symbol is weak, or if the old symbol is undefined.  */
   1394 
   1395   if (*type_change_ok
   1396       || h->root.type == bfd_link_hash_undefined)
   1397     *size_change_ok = TRUE;
   1398 
   1399   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
   1400      symbol, respectively, appears to be a common symbol in a dynamic
   1401      object.  If a symbol appears in an uninitialized section, and is
   1402      not weak, and is not a function, then it may be a common symbol
   1403      which was resolved when the dynamic object was created.  We want
   1404      to treat such symbols specially, because they raise special
   1405      considerations when setting the symbol size: if the symbol
   1406      appears as a common symbol in a regular object, and the size in
   1407      the regular object is larger, we must make sure that we use the
   1408      larger size.  This problematic case can always be avoided in C,
   1409      but it must be handled correctly when using Fortran shared
   1410      libraries.
   1411 
   1412      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
   1413      likewise for OLDDYNCOMMON and OLDDEF.
   1414 
   1415      Note that this test is just a heuristic, and that it is quite
   1416      possible to have an uninitialized symbol in a shared object which
   1417      is really a definition, rather than a common symbol.  This could
   1418      lead to some minor confusion when the symbol really is a common
   1419      symbol in some regular object.  However, I think it will be
   1420      harmless.  */
   1421 
   1422   if (newdyn
   1423       && newdef
   1424       && !newweak
   1425       && (sec->flags & SEC_ALLOC) != 0
   1426       && (sec->flags & SEC_LOAD) == 0
   1427       && sym->st_size > 0
   1428       && !newfunc)
   1429     newdyncommon = TRUE;
   1430   else
   1431     newdyncommon = FALSE;
   1432 
   1433   if (olddyn
   1434       && olddef
   1435       && h->root.type == bfd_link_hash_defined
   1436       && h->def_dynamic
   1437       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
   1438       && (h->root.u.def.section->flags & SEC_LOAD) == 0
   1439       && h->size > 0
   1440       && !oldfunc)
   1441     olddyncommon = TRUE;
   1442   else
   1443     olddyncommon = FALSE;
   1444 
   1445   /* We now know everything about the old and new symbols.  We ask the
   1446      backend to check if we can merge them.  */
   1447   if (bed->merge_symbol != NULL)
   1448     {
   1449       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
   1450 	return FALSE;
   1451       sec = *psec;
   1452     }
   1453 
   1454   /* If both the old and the new symbols look like common symbols in a
   1455      dynamic object, set the size of the symbol to the larger of the
   1456      two.  */
   1457 
   1458   if (olddyncommon
   1459       && newdyncommon
   1460       && sym->st_size != h->size)
   1461     {
   1462       /* Since we think we have two common symbols, issue a multiple
   1463 	 common warning if desired.  Note that we only warn if the
   1464 	 size is different.  If the size is the same, we simply let
   1465 	 the old symbol override the new one as normally happens with
   1466 	 symbols defined in dynamic objects.  */
   1467 
   1468       if (! ((*info->callbacks->multiple_common)
   1469 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
   1470 	return FALSE;
   1471 
   1472       if (sym->st_size > h->size)
   1473 	h->size = sym->st_size;
   1474 
   1475       *size_change_ok = TRUE;
   1476     }
   1477 
   1478   /* If we are looking at a dynamic object, and we have found a
   1479      definition, we need to see if the symbol was already defined by
   1480      some other object.  If so, we want to use the existing
   1481      definition, and we do not want to report a multiple symbol
   1482      definition error; we do this by clobbering *PSEC to be
   1483      bfd_und_section_ptr.
   1484 
   1485      We treat a common symbol as a definition if the symbol in the
   1486      shared library is a function, since common symbols always
   1487      represent variables; this can cause confusion in principle, but
   1488      any such confusion would seem to indicate an erroneous program or
   1489      shared library.  We also permit a common symbol in a regular
   1490      object to override a weak symbol in a shared object.  A common
   1491      symbol in executable also overrides a symbol in a shared object.  */
   1492 
   1493   if (newdyn
   1494       && newdef
   1495       && (olddef
   1496 	  || (h->root.type == bfd_link_hash_common
   1497 	      && (newweak
   1498 		  || newfunc
   1499 		  || (!olddyn && bfd_link_executable (info))))))
   1500     {
   1501       *override = TRUE;
   1502       newdef = FALSE;
   1503       newdyncommon = FALSE;
   1504 
   1505       *psec = sec = bfd_und_section_ptr;
   1506       *size_change_ok = TRUE;
   1507 
   1508       /* If we get here when the old symbol is a common symbol, then
   1509 	 we are explicitly letting it override a weak symbol or
   1510 	 function in a dynamic object, and we don't want to warn about
   1511 	 a type change.  If the old symbol is a defined symbol, a type
   1512 	 change warning may still be appropriate.  */
   1513 
   1514       if (h->root.type == bfd_link_hash_common)
   1515 	*type_change_ok = TRUE;
   1516     }
   1517 
   1518   /* Handle the special case of an old common symbol merging with a
   1519      new symbol which looks like a common symbol in a shared object.
   1520      We change *PSEC and *PVALUE to make the new symbol look like a
   1521      common symbol, and let _bfd_generic_link_add_one_symbol do the
   1522      right thing.  */
   1523 
   1524   if (newdyncommon
   1525       && h->root.type == bfd_link_hash_common)
   1526     {
   1527       *override = TRUE;
   1528       newdef = FALSE;
   1529       newdyncommon = FALSE;
   1530       *pvalue = sym->st_size;
   1531       *psec = sec = bed->common_section (oldsec);
   1532       *size_change_ok = TRUE;
   1533     }
   1534 
   1535   /* Skip weak definitions of symbols that are already defined.  */
   1536   if (newdef && olddef && newweak)
   1537     {
   1538       /* Don't skip new non-IR weak syms.  */
   1539       if (!(oldbfd != NULL
   1540 	    && (oldbfd->flags & BFD_PLUGIN) != 0
   1541 	    && (abfd->flags & BFD_PLUGIN) == 0))
   1542 	{
   1543 	  newdef = FALSE;
   1544 	  *skip = TRUE;
   1545 	}
   1546 
   1547       /* Merge st_other.  If the symbol already has a dynamic index,
   1548 	 but visibility says it should not be visible, turn it into a
   1549 	 local symbol.  */
   1550       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
   1551       if (h->dynindx != -1)
   1552 	switch (ELF_ST_VISIBILITY (h->other))
   1553 	  {
   1554 	  case STV_INTERNAL:
   1555 	  case STV_HIDDEN:
   1556 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   1557 	    break;
   1558 	  }
   1559     }
   1560 
   1561   /* If the old symbol is from a dynamic object, and the new symbol is
   1562      a definition which is not from a dynamic object, then the new
   1563      symbol overrides the old symbol.  Symbols from regular files
   1564      always take precedence over symbols from dynamic objects, even if
   1565      they are defined after the dynamic object in the link.
   1566 
   1567      As above, we again permit a common symbol in a regular object to
   1568      override a definition in a shared object if the shared object
   1569      symbol is a function or is weak.  */
   1570 
   1571   flip = NULL;
   1572   if (!newdyn
   1573       && (newdef
   1574 	  || (bfd_is_com_section (sec)
   1575 	      && (oldweak || oldfunc)))
   1576       && olddyn
   1577       && olddef
   1578       && h->def_dynamic)
   1579     {
   1580       /* Change the hash table entry to undefined, and let
   1581 	 _bfd_generic_link_add_one_symbol do the right thing with the
   1582 	 new definition.  */
   1583 
   1584       h->root.type = bfd_link_hash_undefined;
   1585       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1586       *size_change_ok = TRUE;
   1587 
   1588       olddef = FALSE;
   1589       olddyncommon = FALSE;
   1590 
   1591       /* We again permit a type change when a common symbol may be
   1592 	 overriding a function.  */
   1593 
   1594       if (bfd_is_com_section (sec))
   1595 	{
   1596 	  if (oldfunc)
   1597 	    {
   1598 	      /* If a common symbol overrides a function, make sure
   1599 		 that it isn't defined dynamically nor has type
   1600 		 function.  */
   1601 	      h->def_dynamic = 0;
   1602 	      h->type = STT_NOTYPE;
   1603 	    }
   1604 	  *type_change_ok = TRUE;
   1605 	}
   1606 
   1607       if (hi->root.type == bfd_link_hash_indirect)
   1608 	flip = hi;
   1609       else
   1610 	/* This union may have been set to be non-NULL when this symbol
   1611 	   was seen in a dynamic object.  We must force the union to be
   1612 	   NULL, so that it is correct for a regular symbol.  */
   1613 	h->verinfo.vertree = NULL;
   1614     }
   1615 
   1616   /* Handle the special case of a new common symbol merging with an
   1617      old symbol that looks like it might be a common symbol defined in
   1618      a shared object.  Note that we have already handled the case in
   1619      which a new common symbol should simply override the definition
   1620      in the shared library.  */
   1621 
   1622   if (! newdyn
   1623       && bfd_is_com_section (sec)
   1624       && olddyncommon)
   1625     {
   1626       /* It would be best if we could set the hash table entry to a
   1627 	 common symbol, but we don't know what to use for the section
   1628 	 or the alignment.  */
   1629       if (! ((*info->callbacks->multiple_common)
   1630 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
   1631 	return FALSE;
   1632 
   1633       /* If the presumed common symbol in the dynamic object is
   1634 	 larger, pretend that the new symbol has its size.  */
   1635 
   1636       if (h->size > *pvalue)
   1637 	*pvalue = h->size;
   1638 
   1639       /* We need to remember the alignment required by the symbol
   1640 	 in the dynamic object.  */
   1641       BFD_ASSERT (pold_alignment);
   1642       *pold_alignment = h->root.u.def.section->alignment_power;
   1643 
   1644       olddef = FALSE;
   1645       olddyncommon = FALSE;
   1646 
   1647       h->root.type = bfd_link_hash_undefined;
   1648       h->root.u.undef.abfd = h->root.u.def.section->owner;
   1649 
   1650       *size_change_ok = TRUE;
   1651       *type_change_ok = TRUE;
   1652 
   1653       if (hi->root.type == bfd_link_hash_indirect)
   1654 	flip = hi;
   1655       else
   1656 	h->verinfo.vertree = NULL;
   1657     }
   1658 
   1659   if (flip != NULL)
   1660     {
   1661       /* Handle the case where we had a versioned symbol in a dynamic
   1662 	 library and now find a definition in a normal object.  In this
   1663 	 case, we make the versioned symbol point to the normal one.  */
   1664       flip->root.type = h->root.type;
   1665       flip->root.u.undef.abfd = h->root.u.undef.abfd;
   1666       h->root.type = bfd_link_hash_indirect;
   1667       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
   1668       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
   1669       if (h->def_dynamic)
   1670 	{
   1671 	  h->def_dynamic = 0;
   1672 	  flip->ref_dynamic = 1;
   1673 	}
   1674     }
   1675 
   1676   return TRUE;
   1677 }
   1678 
   1679 /* This function is called to create an indirect symbol from the
   1680    default for the symbol with the default version if needed. The
   1681    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
   1682    set DYNSYM if the new indirect symbol is dynamic.  */
   1683 
   1684 static bfd_boolean
   1685 _bfd_elf_add_default_symbol (bfd *abfd,
   1686 			     struct bfd_link_info *info,
   1687 			     struct elf_link_hash_entry *h,
   1688 			     const char *name,
   1689 			     Elf_Internal_Sym *sym,
   1690 			     asection *sec,
   1691 			     bfd_vma value,
   1692 			     bfd **poldbfd,
   1693 			     bfd_boolean *dynsym)
   1694 {
   1695   bfd_boolean type_change_ok;
   1696   bfd_boolean size_change_ok;
   1697   bfd_boolean skip;
   1698   char *shortname;
   1699   struct elf_link_hash_entry *hi;
   1700   struct bfd_link_hash_entry *bh;
   1701   const struct elf_backend_data *bed;
   1702   bfd_boolean collect;
   1703   bfd_boolean dynamic;
   1704   bfd_boolean override;
   1705   char *p;
   1706   size_t len, shortlen;
   1707   asection *tmp_sec;
   1708   bfd_boolean matched;
   1709 
   1710   if (h->versioned == unversioned || h->versioned == versioned_hidden)
   1711     return TRUE;
   1712 
   1713   /* If this symbol has a version, and it is the default version, we
   1714      create an indirect symbol from the default name to the fully
   1715      decorated name.  This will cause external references which do not
   1716      specify a version to be bound to this version of the symbol.  */
   1717   p = strchr (name, ELF_VER_CHR);
   1718   if (h->versioned == unknown)
   1719     {
   1720       if (p == NULL)
   1721 	{
   1722 	  h->versioned = unversioned;
   1723 	  return TRUE;
   1724 	}
   1725       else
   1726 	{
   1727 	  if (p[1] != ELF_VER_CHR)
   1728 	    {
   1729 	      h->versioned = versioned_hidden;
   1730 	      return TRUE;
   1731 	    }
   1732 	  else
   1733 	    h->versioned = versioned;
   1734 	}
   1735     }
   1736   else
   1737     {
   1738       /* PR ld/19073: We may see an unversioned definition after the
   1739 	 default version.  */
   1740       if (p == NULL)
   1741 	return TRUE;
   1742     }
   1743 
   1744   bed = get_elf_backend_data (abfd);
   1745   collect = bed->collect;
   1746   dynamic = (abfd->flags & DYNAMIC) != 0;
   1747 
   1748   shortlen = p - name;
   1749   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
   1750   if (shortname == NULL)
   1751     return FALSE;
   1752   memcpy (shortname, name, shortlen);
   1753   shortname[shortlen] = '\0';
   1754 
   1755   /* We are going to create a new symbol.  Merge it with any existing
   1756      symbol with this name.  For the purposes of the merge, act as
   1757      though we were defining the symbol we just defined, although we
   1758      actually going to define an indirect symbol.  */
   1759   type_change_ok = FALSE;
   1760   size_change_ok = FALSE;
   1761   matched = TRUE;
   1762   tmp_sec = sec;
   1763   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1764 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1765 			      &type_change_ok, &size_change_ok, &matched))
   1766     return FALSE;
   1767 
   1768   if (skip)
   1769     goto nondefault;
   1770 
   1771   if (hi->def_regular)
   1772     {
   1773       /* If the undecorated symbol will have a version added by a
   1774 	 script different to H, then don't indirect to/from the
   1775 	 undecorated symbol.  This isn't ideal because we may not yet
   1776 	 have seen symbol versions, if given by a script on the
   1777 	 command line rather than via --version-script.  */
   1778       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
   1779 	{
   1780 	  bfd_boolean hide;
   1781 
   1782 	  hi->verinfo.vertree
   1783 	    = bfd_find_version_for_sym (info->version_info,
   1784 					hi->root.root.string, &hide);
   1785 	  if (hi->verinfo.vertree != NULL && hide)
   1786 	    {
   1787 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   1788 	      goto nondefault;
   1789 	    }
   1790 	}
   1791       if (hi->verinfo.vertree != NULL
   1792 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
   1793 	goto nondefault;
   1794     }
   1795 
   1796   if (! override)
   1797     {
   1798       /* Add the default symbol if not performing a relocatable link.  */
   1799       if (! bfd_link_relocatable (info))
   1800 	{
   1801 	  bh = &hi->root;
   1802 	  if (! (_bfd_generic_link_add_one_symbol
   1803 		 (info, abfd, shortname, BSF_INDIRECT,
   1804 		  bfd_ind_section_ptr,
   1805 		  0, name, FALSE, collect, &bh)))
   1806 	    return FALSE;
   1807 	  hi = (struct elf_link_hash_entry *) bh;
   1808 	}
   1809     }
   1810   else
   1811     {
   1812       /* In this case the symbol named SHORTNAME is overriding the
   1813 	 indirect symbol we want to add.  We were planning on making
   1814 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
   1815 	 is the name without a version.  NAME is the fully versioned
   1816 	 name, and it is the default version.
   1817 
   1818 	 Overriding means that we already saw a definition for the
   1819 	 symbol SHORTNAME in a regular object, and it is overriding
   1820 	 the symbol defined in the dynamic object.
   1821 
   1822 	 When this happens, we actually want to change NAME, the
   1823 	 symbol we just added, to refer to SHORTNAME.  This will cause
   1824 	 references to NAME in the shared object to become references
   1825 	 to SHORTNAME in the regular object.  This is what we expect
   1826 	 when we override a function in a shared object: that the
   1827 	 references in the shared object will be mapped to the
   1828 	 definition in the regular object.  */
   1829 
   1830       while (hi->root.type == bfd_link_hash_indirect
   1831 	     || hi->root.type == bfd_link_hash_warning)
   1832 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1833 
   1834       h->root.type = bfd_link_hash_indirect;
   1835       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
   1836       if (h->def_dynamic)
   1837 	{
   1838 	  h->def_dynamic = 0;
   1839 	  hi->ref_dynamic = 1;
   1840 	  if (hi->ref_regular
   1841 	      || hi->def_regular)
   1842 	    {
   1843 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
   1844 		return FALSE;
   1845 	    }
   1846 	}
   1847 
   1848       /* Now set HI to H, so that the following code will set the
   1849 	 other fields correctly.  */
   1850       hi = h;
   1851     }
   1852 
   1853   /* Check if HI is a warning symbol.  */
   1854   if (hi->root.type == bfd_link_hash_warning)
   1855     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1856 
   1857   /* If there is a duplicate definition somewhere, then HI may not
   1858      point to an indirect symbol.  We will have reported an error to
   1859      the user in that case.  */
   1860 
   1861   if (hi->root.type == bfd_link_hash_indirect)
   1862     {
   1863       struct elf_link_hash_entry *ht;
   1864 
   1865       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
   1866       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
   1867 
   1868       /* A reference to the SHORTNAME symbol from a dynamic library
   1869 	 will be satisfied by the versioned symbol at runtime.  In
   1870 	 effect, we have a reference to the versioned symbol.  */
   1871       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   1872       hi->dynamic_def |= ht->dynamic_def;
   1873 
   1874       /* See if the new flags lead us to realize that the symbol must
   1875 	 be dynamic.  */
   1876       if (! *dynsym)
   1877 	{
   1878 	  if (! dynamic)
   1879 	    {
   1880 	      if (! bfd_link_executable (info)
   1881 		  || hi->def_dynamic
   1882 		  || hi->ref_dynamic)
   1883 		*dynsym = TRUE;
   1884 	    }
   1885 	  else
   1886 	    {
   1887 	      if (hi->ref_regular)
   1888 		*dynsym = TRUE;
   1889 	    }
   1890 	}
   1891     }
   1892 
   1893   /* We also need to define an indirection from the nondefault version
   1894      of the symbol.  */
   1895 
   1896 nondefault:
   1897   len = strlen (name);
   1898   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
   1899   if (shortname == NULL)
   1900     return FALSE;
   1901   memcpy (shortname, name, shortlen);
   1902   memcpy (shortname + shortlen, p + 1, len - shortlen);
   1903 
   1904   /* Once again, merge with any existing symbol.  */
   1905   type_change_ok = FALSE;
   1906   size_change_ok = FALSE;
   1907   tmp_sec = sec;
   1908   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
   1909 			      &hi, poldbfd, NULL, NULL, &skip, &override,
   1910 			      &type_change_ok, &size_change_ok, &matched))
   1911     return FALSE;
   1912 
   1913   if (skip)
   1914     return TRUE;
   1915 
   1916   if (override)
   1917     {
   1918       /* Here SHORTNAME is a versioned name, so we don't expect to see
   1919 	 the type of override we do in the case above unless it is
   1920 	 overridden by a versioned definition.  */
   1921       if (hi->root.type != bfd_link_hash_defined
   1922 	  && hi->root.type != bfd_link_hash_defweak)
   1923 	(*_bfd_error_handler)
   1924 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
   1925 	   abfd, shortname);
   1926     }
   1927   else
   1928     {
   1929       bh = &hi->root;
   1930       if (! (_bfd_generic_link_add_one_symbol
   1931 	     (info, abfd, shortname, BSF_INDIRECT,
   1932 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
   1933 	return FALSE;
   1934       hi = (struct elf_link_hash_entry *) bh;
   1935 
   1936       /* If there is a duplicate definition somewhere, then HI may not
   1937 	 point to an indirect symbol.  We will have reported an error
   1938 	 to the user in that case.  */
   1939 
   1940       if (hi->root.type == bfd_link_hash_indirect)
   1941 	{
   1942 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   1943 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
   1944 	  hi->dynamic_def |= h->dynamic_def;
   1945 
   1946 	  /* See if the new flags lead us to realize that the symbol
   1947 	     must be dynamic.  */
   1948 	  if (! *dynsym)
   1949 	    {
   1950 	      if (! dynamic)
   1951 		{
   1952 		  if (! bfd_link_executable (info)
   1953 		      || hi->ref_dynamic)
   1954 		    *dynsym = TRUE;
   1955 		}
   1956 	      else
   1957 		{
   1958 		  if (hi->ref_regular)
   1959 		    *dynsym = TRUE;
   1960 		}
   1961 	    }
   1962 	}
   1963     }
   1964 
   1965   return TRUE;
   1966 }
   1967 
   1968 /* This routine is used to export all defined symbols into the dynamic
   1970    symbol table.  It is called via elf_link_hash_traverse.  */
   1971 
   1972 static bfd_boolean
   1973 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
   1974 {
   1975   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   1976 
   1977   /* Ignore indirect symbols.  These are added by the versioning code.  */
   1978   if (h->root.type == bfd_link_hash_indirect)
   1979     return TRUE;
   1980 
   1981   /* Ignore this if we won't export it.  */
   1982   if (!eif->info->export_dynamic && !h->dynamic)
   1983     return TRUE;
   1984 
   1985   if (h->dynindx == -1
   1986       && (h->def_regular || h->ref_regular)
   1987       && ! bfd_hide_sym_by_version (eif->info->version_info,
   1988 				    h->root.root.string))
   1989     {
   1990       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   1991 	{
   1992 	  eif->failed = TRUE;
   1993 	  return FALSE;
   1994 	}
   1995     }
   1996 
   1997   return TRUE;
   1998 }
   1999 
   2000 /* Look through the symbols which are defined in other shared
   2002    libraries and referenced here.  Update the list of version
   2003    dependencies.  This will be put into the .gnu.version_r section.
   2004    This function is called via elf_link_hash_traverse.  */
   2005 
   2006 static bfd_boolean
   2007 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
   2008 					 void *data)
   2009 {
   2010   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   2011   Elf_Internal_Verneed *t;
   2012   Elf_Internal_Vernaux *a;
   2013   bfd_size_type amt;
   2014 
   2015   /* We only care about symbols defined in shared objects with version
   2016      information.  */
   2017   if (!h->def_dynamic
   2018       || h->def_regular
   2019       || h->dynindx == -1
   2020       || h->verinfo.verdef == NULL
   2021       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   2022 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   2023     return TRUE;
   2024 
   2025   /* See if we already know about this version.  */
   2026   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
   2027        t != NULL;
   2028        t = t->vn_nextref)
   2029     {
   2030       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
   2031 	continue;
   2032 
   2033       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2034 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
   2035 	  return TRUE;
   2036 
   2037       break;
   2038     }
   2039 
   2040   /* This is a new version.  Add it to tree we are building.  */
   2041 
   2042   if (t == NULL)
   2043     {
   2044       amt = sizeof *t;
   2045       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2046       if (t == NULL)
   2047 	{
   2048 	  rinfo->failed = TRUE;
   2049 	  return FALSE;
   2050 	}
   2051 
   2052       t->vn_bfd = h->verinfo.verdef->vd_bfd;
   2053       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
   2054       elf_tdata (rinfo->info->output_bfd)->verref = t;
   2055     }
   2056 
   2057   amt = sizeof *a;
   2058   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
   2059   if (a == NULL)
   2060     {
   2061       rinfo->failed = TRUE;
   2062       return FALSE;
   2063     }
   2064 
   2065   /* Note that we are copying a string pointer here, and testing it
   2066      above.  If bfd_elf_string_from_elf_section is ever changed to
   2067      discard the string data when low in memory, this will have to be
   2068      fixed.  */
   2069   a->vna_nodename = h->verinfo.verdef->vd_nodename;
   2070 
   2071   a->vna_flags = h->verinfo.verdef->vd_flags;
   2072   a->vna_nextptr = t->vn_auxptr;
   2073 
   2074   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
   2075   ++rinfo->vers;
   2076 
   2077   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
   2078 
   2079   t->vn_auxptr = a;
   2080 
   2081   return TRUE;
   2082 }
   2083 
   2084 /* Figure out appropriate versions for all the symbols.  We may not
   2085    have the version number script until we have read all of the input
   2086    files, so until that point we don't know which symbols should be
   2087    local.  This function is called via elf_link_hash_traverse.  */
   2088 
   2089 static bfd_boolean
   2090 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   2091 {
   2092   struct elf_info_failed *sinfo;
   2093   struct bfd_link_info *info;
   2094   const struct elf_backend_data *bed;
   2095   struct elf_info_failed eif;
   2096   char *p;
   2097   bfd_size_type amt;
   2098 
   2099   sinfo = (struct elf_info_failed *) data;
   2100   info = sinfo->info;
   2101 
   2102   /* Fix the symbol flags.  */
   2103   eif.failed = FALSE;
   2104   eif.info = info;
   2105   if (! _bfd_elf_fix_symbol_flags (h, &eif))
   2106     {
   2107       if (eif.failed)
   2108 	sinfo->failed = TRUE;
   2109       return FALSE;
   2110     }
   2111 
   2112   /* We only need version numbers for symbols defined in regular
   2113      objects.  */
   2114   if (!h->def_regular)
   2115     return TRUE;
   2116 
   2117   bed = get_elf_backend_data (info->output_bfd);
   2118   p = strchr (h->root.root.string, ELF_VER_CHR);
   2119   if (p != NULL && h->verinfo.vertree == NULL)
   2120     {
   2121       struct bfd_elf_version_tree *t;
   2122 
   2123       ++p;
   2124       if (*p == ELF_VER_CHR)
   2125 	++p;
   2126 
   2127       /* If there is no version string, we can just return out.  */
   2128       if (*p == '\0')
   2129 	return TRUE;
   2130 
   2131       /* Look for the version.  If we find it, it is no longer weak.  */
   2132       for (t = sinfo->info->version_info; t != NULL; t = t->next)
   2133 	{
   2134 	  if (strcmp (t->name, p) == 0)
   2135 	    {
   2136 	      size_t len;
   2137 	      char *alc;
   2138 	      struct bfd_elf_version_expr *d;
   2139 
   2140 	      len = p - h->root.root.string;
   2141 	      alc = (char *) bfd_malloc (len);
   2142 	      if (alc == NULL)
   2143 		{
   2144 		  sinfo->failed = TRUE;
   2145 		  return FALSE;
   2146 		}
   2147 	      memcpy (alc, h->root.root.string, len - 1);
   2148 	      alc[len - 1] = '\0';
   2149 	      if (alc[len - 2] == ELF_VER_CHR)
   2150 		alc[len - 2] = '\0';
   2151 
   2152 	      h->verinfo.vertree = t;
   2153 	      t->used = TRUE;
   2154 	      d = NULL;
   2155 
   2156 	      if (t->globals.list != NULL)
   2157 		d = (*t->match) (&t->globals, NULL, alc);
   2158 
   2159 	      /* See if there is anything to force this symbol to
   2160 		 local scope.  */
   2161 	      if (d == NULL && t->locals.list != NULL)
   2162 		{
   2163 		  d = (*t->match) (&t->locals, NULL, alc);
   2164 		  if (d != NULL
   2165 		      && h->dynindx != -1
   2166 		      && ! info->export_dynamic)
   2167 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2168 		}
   2169 
   2170 	      free (alc);
   2171 	      break;
   2172 	    }
   2173 	}
   2174 
   2175       /* If we are building an application, we need to create a
   2176 	 version node for this version.  */
   2177       if (t == NULL && bfd_link_executable (info))
   2178 	{
   2179 	  struct bfd_elf_version_tree **pp;
   2180 	  int version_index;
   2181 
   2182 	  /* If we aren't going to export this symbol, we don't need
   2183 	     to worry about it.  */
   2184 	  if (h->dynindx == -1)
   2185 	    return TRUE;
   2186 
   2187 	  amt = sizeof *t;
   2188 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
   2189 	  if (t == NULL)
   2190 	    {
   2191 	      sinfo->failed = TRUE;
   2192 	      return FALSE;
   2193 	    }
   2194 
   2195 	  t->name = p;
   2196 	  t->name_indx = (unsigned int) -1;
   2197 	  t->used = TRUE;
   2198 
   2199 	  version_index = 1;
   2200 	  /* Don't count anonymous version tag.  */
   2201 	  if (sinfo->info->version_info != NULL
   2202 	      && sinfo->info->version_info->vernum == 0)
   2203 	    version_index = 0;
   2204 	  for (pp = &sinfo->info->version_info;
   2205 	       *pp != NULL;
   2206 	       pp = &(*pp)->next)
   2207 	    ++version_index;
   2208 	  t->vernum = version_index;
   2209 
   2210 	  *pp = t;
   2211 
   2212 	  h->verinfo.vertree = t;
   2213 	}
   2214       else if (t == NULL)
   2215 	{
   2216 	  /* We could not find the version for a symbol when
   2217 	     generating a shared archive.  Return an error.  */
   2218 	  (*_bfd_error_handler)
   2219 	    (_("%B: version node not found for symbol %s"),
   2220 	     info->output_bfd, h->root.root.string);
   2221 	  bfd_set_error (bfd_error_bad_value);
   2222 	  sinfo->failed = TRUE;
   2223 	  return FALSE;
   2224 	}
   2225     }
   2226 
   2227   /* If we don't have a version for this symbol, see if we can find
   2228      something.  */
   2229   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
   2230     {
   2231       bfd_boolean hide;
   2232 
   2233       h->verinfo.vertree
   2234 	= bfd_find_version_for_sym (sinfo->info->version_info,
   2235 				    h->root.root.string, &hide);
   2236       if (h->verinfo.vertree != NULL && hide)
   2237 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   2238     }
   2239 
   2240   return TRUE;
   2241 }
   2242 
   2243 /* Read and swap the relocs from the section indicated by SHDR.  This
   2245    may be either a REL or a RELA section.  The relocations are
   2246    translated into RELA relocations and stored in INTERNAL_RELOCS,
   2247    which should have already been allocated to contain enough space.
   2248    The EXTERNAL_RELOCS are a buffer where the external form of the
   2249    relocations should be stored.
   2250 
   2251    Returns FALSE if something goes wrong.  */
   2252 
   2253 static bfd_boolean
   2254 elf_link_read_relocs_from_section (bfd *abfd,
   2255 				   asection *sec,
   2256 				   Elf_Internal_Shdr *shdr,
   2257 				   void *external_relocs,
   2258 				   Elf_Internal_Rela *internal_relocs)
   2259 {
   2260   const struct elf_backend_data *bed;
   2261   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   2262   const bfd_byte *erela;
   2263   const bfd_byte *erelaend;
   2264   Elf_Internal_Rela *irela;
   2265   Elf_Internal_Shdr *symtab_hdr;
   2266   size_t nsyms;
   2267 
   2268   /* Position ourselves at the start of the section.  */
   2269   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
   2270     return FALSE;
   2271 
   2272   /* Read the relocations.  */
   2273   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
   2274     return FALSE;
   2275 
   2276   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2277   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
   2278 
   2279   bed = get_elf_backend_data (abfd);
   2280 
   2281   /* Convert the external relocations to the internal format.  */
   2282   if (shdr->sh_entsize == bed->s->sizeof_rel)
   2283     swap_in = bed->s->swap_reloc_in;
   2284   else if (shdr->sh_entsize == bed->s->sizeof_rela)
   2285     swap_in = bed->s->swap_reloca_in;
   2286   else
   2287     {
   2288       bfd_set_error (bfd_error_wrong_format);
   2289       return FALSE;
   2290     }
   2291 
   2292   erela = (const bfd_byte *) external_relocs;
   2293   erelaend = erela + shdr->sh_size;
   2294   irela = internal_relocs;
   2295   while (erela < erelaend)
   2296     {
   2297       bfd_vma r_symndx;
   2298 
   2299       (*swap_in) (abfd, erela, irela);
   2300       r_symndx = ELF32_R_SYM (irela->r_info);
   2301       if (bed->s->arch_size == 64)
   2302 	r_symndx >>= 24;
   2303       if (nsyms > 0)
   2304 	{
   2305 	  if ((size_t) r_symndx >= nsyms)
   2306 	    {
   2307 	      (*_bfd_error_handler)
   2308 		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
   2309 		   " for offset 0x%lx in section `%A'"),
   2310 		 abfd, sec,
   2311 		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
   2312 	      bfd_set_error (bfd_error_bad_value);
   2313 	      return FALSE;
   2314 	    }
   2315 	}
   2316       else if (r_symndx != STN_UNDEF)
   2317 	{
   2318 	  (*_bfd_error_handler)
   2319 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
   2320 	       " when the object file has no symbol table"),
   2321 	     abfd, sec,
   2322 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
   2323 	  bfd_set_error (bfd_error_bad_value);
   2324 	  return FALSE;
   2325 	}
   2326       irela += bed->s->int_rels_per_ext_rel;
   2327       erela += shdr->sh_entsize;
   2328     }
   2329 
   2330   return TRUE;
   2331 }
   2332 
   2333 /* Read and swap the relocs for a section O.  They may have been
   2334    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
   2335    not NULL, they are used as buffers to read into.  They are known to
   2336    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
   2337    the return value is allocated using either malloc or bfd_alloc,
   2338    according to the KEEP_MEMORY argument.  If O has two relocation
   2339    sections (both REL and RELA relocations), then the REL_HDR
   2340    relocations will appear first in INTERNAL_RELOCS, followed by the
   2341    RELA_HDR relocations.  */
   2342 
   2343 Elf_Internal_Rela *
   2344 _bfd_elf_link_read_relocs (bfd *abfd,
   2345 			   asection *o,
   2346 			   void *external_relocs,
   2347 			   Elf_Internal_Rela *internal_relocs,
   2348 			   bfd_boolean keep_memory)
   2349 {
   2350   void *alloc1 = NULL;
   2351   Elf_Internal_Rela *alloc2 = NULL;
   2352   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2353   struct bfd_elf_section_data *esdo = elf_section_data (o);
   2354   Elf_Internal_Rela *internal_rela_relocs;
   2355 
   2356   if (esdo->relocs != NULL)
   2357     return esdo->relocs;
   2358 
   2359   if (o->reloc_count == 0)
   2360     return NULL;
   2361 
   2362   if (internal_relocs == NULL)
   2363     {
   2364       bfd_size_type size;
   2365 
   2366       size = o->reloc_count;
   2367       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
   2368       if (keep_memory)
   2369 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
   2370       else
   2371 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
   2372       if (internal_relocs == NULL)
   2373 	goto error_return;
   2374     }
   2375 
   2376   if (external_relocs == NULL)
   2377     {
   2378       bfd_size_type size = 0;
   2379 
   2380       if (esdo->rel.hdr)
   2381 	size += esdo->rel.hdr->sh_size;
   2382       if (esdo->rela.hdr)
   2383 	size += esdo->rela.hdr->sh_size;
   2384 
   2385       alloc1 = bfd_malloc (size);
   2386       if (alloc1 == NULL)
   2387 	goto error_return;
   2388       external_relocs = alloc1;
   2389     }
   2390 
   2391   internal_rela_relocs = internal_relocs;
   2392   if (esdo->rel.hdr)
   2393     {
   2394       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
   2395 					      external_relocs,
   2396 					      internal_relocs))
   2397 	goto error_return;
   2398       external_relocs = (((bfd_byte *) external_relocs)
   2399 			 + esdo->rel.hdr->sh_size);
   2400       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
   2401 			       * bed->s->int_rels_per_ext_rel);
   2402     }
   2403 
   2404   if (esdo->rela.hdr
   2405       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
   2406 					      external_relocs,
   2407 					      internal_rela_relocs)))
   2408     goto error_return;
   2409 
   2410   /* Cache the results for next time, if we can.  */
   2411   if (keep_memory)
   2412     esdo->relocs = internal_relocs;
   2413 
   2414   if (alloc1 != NULL)
   2415     free (alloc1);
   2416 
   2417   /* Don't free alloc2, since if it was allocated we are passing it
   2418      back (under the name of internal_relocs).  */
   2419 
   2420   return internal_relocs;
   2421 
   2422  error_return:
   2423   if (alloc1 != NULL)
   2424     free (alloc1);
   2425   if (alloc2 != NULL)
   2426     {
   2427       if (keep_memory)
   2428 	bfd_release (abfd, alloc2);
   2429       else
   2430 	free (alloc2);
   2431     }
   2432   return NULL;
   2433 }
   2434 
   2435 /* Compute the size of, and allocate space for, REL_HDR which is the
   2436    section header for a section containing relocations for O.  */
   2437 
   2438 static bfd_boolean
   2439 _bfd_elf_link_size_reloc_section (bfd *abfd,
   2440 				  struct bfd_elf_section_reloc_data *reldata)
   2441 {
   2442   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
   2443 
   2444   /* That allows us to calculate the size of the section.  */
   2445   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
   2446 
   2447   /* The contents field must last into write_object_contents, so we
   2448      allocate it with bfd_alloc rather than malloc.  Also since we
   2449      cannot be sure that the contents will actually be filled in,
   2450      we zero the allocated space.  */
   2451   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
   2452   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
   2453     return FALSE;
   2454 
   2455   if (reldata->hashes == NULL && reldata->count)
   2456     {
   2457       struct elf_link_hash_entry **p;
   2458 
   2459       p = ((struct elf_link_hash_entry **)
   2460 	   bfd_zmalloc (reldata->count * sizeof (*p)));
   2461       if (p == NULL)
   2462 	return FALSE;
   2463 
   2464       reldata->hashes = p;
   2465     }
   2466 
   2467   return TRUE;
   2468 }
   2469 
   2470 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
   2471    originated from the section given by INPUT_REL_HDR) to the
   2472    OUTPUT_BFD.  */
   2473 
   2474 bfd_boolean
   2475 _bfd_elf_link_output_relocs (bfd *output_bfd,
   2476 			     asection *input_section,
   2477 			     Elf_Internal_Shdr *input_rel_hdr,
   2478 			     Elf_Internal_Rela *internal_relocs,
   2479 			     struct elf_link_hash_entry **rel_hash
   2480 			       ATTRIBUTE_UNUSED)
   2481 {
   2482   Elf_Internal_Rela *irela;
   2483   Elf_Internal_Rela *irelaend;
   2484   bfd_byte *erel;
   2485   struct bfd_elf_section_reloc_data *output_reldata;
   2486   asection *output_section;
   2487   const struct elf_backend_data *bed;
   2488   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   2489   struct bfd_elf_section_data *esdo;
   2490 
   2491   output_section = input_section->output_section;
   2492 
   2493   bed = get_elf_backend_data (output_bfd);
   2494   esdo = elf_section_data (output_section);
   2495   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2496     {
   2497       output_reldata = &esdo->rel;
   2498       swap_out = bed->s->swap_reloc_out;
   2499     }
   2500   else if (esdo->rela.hdr
   2501 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
   2502     {
   2503       output_reldata = &esdo->rela;
   2504       swap_out = bed->s->swap_reloca_out;
   2505     }
   2506   else
   2507     {
   2508       (*_bfd_error_handler)
   2509 	(_("%B: relocation size mismatch in %B section %A"),
   2510 	 output_bfd, input_section->owner, input_section);
   2511       bfd_set_error (bfd_error_wrong_format);
   2512       return FALSE;
   2513     }
   2514 
   2515   erel = output_reldata->hdr->contents;
   2516   erel += output_reldata->count * input_rel_hdr->sh_entsize;
   2517   irela = internal_relocs;
   2518   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
   2519 		      * bed->s->int_rels_per_ext_rel);
   2520   while (irela < irelaend)
   2521     {
   2522       (*swap_out) (output_bfd, irela, erel);
   2523       irela += bed->s->int_rels_per_ext_rel;
   2524       erel += input_rel_hdr->sh_entsize;
   2525     }
   2526 
   2527   /* Bump the counter, so that we know where to add the next set of
   2528      relocations.  */
   2529   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
   2530 
   2531   return TRUE;
   2532 }
   2533 
   2534 /* Make weak undefined symbols in PIE dynamic.  */
   2536 
   2537 bfd_boolean
   2538 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
   2539 				 struct elf_link_hash_entry *h)
   2540 {
   2541   if (bfd_link_pie (info)
   2542       && h->dynindx == -1
   2543       && h->root.type == bfd_link_hash_undefweak)
   2544     return bfd_elf_link_record_dynamic_symbol (info, h);
   2545 
   2546   return TRUE;
   2547 }
   2548 
   2549 /* Fix up the flags for a symbol.  This handles various cases which
   2550    can only be fixed after all the input files are seen.  This is
   2551    currently called by both adjust_dynamic_symbol and
   2552    assign_sym_version, which is unnecessary but perhaps more robust in
   2553    the face of future changes.  */
   2554 
   2555 static bfd_boolean
   2556 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
   2557 			   struct elf_info_failed *eif)
   2558 {
   2559   const struct elf_backend_data *bed;
   2560 
   2561   /* If this symbol was mentioned in a non-ELF file, try to set
   2562      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
   2563      permit a non-ELF file to correctly refer to a symbol defined in
   2564      an ELF dynamic object.  */
   2565   if (h->non_elf)
   2566     {
   2567       while (h->root.type == bfd_link_hash_indirect)
   2568 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2569 
   2570       if (h->root.type != bfd_link_hash_defined
   2571 	  && h->root.type != bfd_link_hash_defweak)
   2572 	{
   2573 	  h->ref_regular = 1;
   2574 	  h->ref_regular_nonweak = 1;
   2575 	}
   2576       else
   2577 	{
   2578 	  if (h->root.u.def.section->owner != NULL
   2579 	      && (bfd_get_flavour (h->root.u.def.section->owner)
   2580 		  == bfd_target_elf_flavour))
   2581 	    {
   2582 	      h->ref_regular = 1;
   2583 	      h->ref_regular_nonweak = 1;
   2584 	    }
   2585 	  else
   2586 	    h->def_regular = 1;
   2587 	}
   2588 
   2589       if (h->dynindx == -1
   2590 	  && (h->def_dynamic
   2591 	      || h->ref_dynamic))
   2592 	{
   2593 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
   2594 	    {
   2595 	      eif->failed = TRUE;
   2596 	      return FALSE;
   2597 	    }
   2598 	}
   2599     }
   2600   else
   2601     {
   2602       /* Unfortunately, NON_ELF is only correct if the symbol
   2603 	 was first seen in a non-ELF file.  Fortunately, if the symbol
   2604 	 was first seen in an ELF file, we're probably OK unless the
   2605 	 symbol was defined in a non-ELF file.  Catch that case here.
   2606 	 FIXME: We're still in trouble if the symbol was first seen in
   2607 	 a dynamic object, and then later in a non-ELF regular object.  */
   2608       if ((h->root.type == bfd_link_hash_defined
   2609 	   || h->root.type == bfd_link_hash_defweak)
   2610 	  && !h->def_regular
   2611 	  && (h->root.u.def.section->owner != NULL
   2612 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
   2613 		 != bfd_target_elf_flavour)
   2614 	      : (bfd_is_abs_section (h->root.u.def.section)
   2615 		 && !h->def_dynamic)))
   2616 	h->def_regular = 1;
   2617     }
   2618 
   2619   /* Backend specific symbol fixup.  */
   2620   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
   2621   if (bed->elf_backend_fixup_symbol
   2622       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
   2623     return FALSE;
   2624 
   2625   /* If this is a final link, and the symbol was defined as a common
   2626      symbol in a regular object file, and there was no definition in
   2627      any dynamic object, then the linker will have allocated space for
   2628      the symbol in a common section but the DEF_REGULAR
   2629      flag will not have been set.  */
   2630   if (h->root.type == bfd_link_hash_defined
   2631       && !h->def_regular
   2632       && h->ref_regular
   2633       && !h->def_dynamic
   2634       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
   2635     h->def_regular = 1;
   2636 
   2637   /* If -Bsymbolic was used (which means to bind references to global
   2638      symbols to the definition within the shared object), and this
   2639      symbol was defined in a regular object, then it actually doesn't
   2640      need a PLT entry.  Likewise, if the symbol has non-default
   2641      visibility.  If the symbol has hidden or internal visibility, we
   2642      will force it local.  */
   2643   if (h->needs_plt
   2644       && bfd_link_pic (eif->info)
   2645       && is_elf_hash_table (eif->info->hash)
   2646       && (SYMBOLIC_BIND (eif->info, h)
   2647 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2648       && h->def_regular)
   2649     {
   2650       bfd_boolean force_local;
   2651 
   2652       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
   2653 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
   2654       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
   2655     }
   2656 
   2657   /* If a weak undefined symbol has non-default visibility, we also
   2658      hide it from the dynamic linker.  */
   2659   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2660       && h->root.type == bfd_link_hash_undefweak)
   2661     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
   2662 
   2663   /* If this is a weak defined symbol in a dynamic object, and we know
   2664      the real definition in the dynamic object, copy interesting flags
   2665      over to the real definition.  */
   2666   if (h->u.weakdef != NULL)
   2667     {
   2668       /* If the real definition is defined by a regular object file,
   2669 	 don't do anything special.  See the longer description in
   2670 	 _bfd_elf_adjust_dynamic_symbol, below.  */
   2671       if (h->u.weakdef->def_regular)
   2672 	h->u.weakdef = NULL;
   2673       else
   2674 	{
   2675 	  struct elf_link_hash_entry *weakdef = h->u.weakdef;
   2676 
   2677 	  while (h->root.type == bfd_link_hash_indirect)
   2678 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2679 
   2680 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
   2681 		      || h->root.type == bfd_link_hash_defweak);
   2682 	  BFD_ASSERT (weakdef->def_dynamic);
   2683 	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
   2684 		      || weakdef->root.type == bfd_link_hash_defweak);
   2685 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
   2686 	}
   2687     }
   2688 
   2689   return TRUE;
   2690 }
   2691 
   2692 /* Make the backend pick a good value for a dynamic symbol.  This is
   2693    called via elf_link_hash_traverse, and also calls itself
   2694    recursively.  */
   2695 
   2696 static bfd_boolean
   2697 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   2698 {
   2699   struct elf_info_failed *eif = (struct elf_info_failed *) data;
   2700   bfd *dynobj;
   2701   const struct elf_backend_data *bed;
   2702 
   2703   if (! is_elf_hash_table (eif->info->hash))
   2704     return FALSE;
   2705 
   2706   /* Ignore indirect symbols.  These are added by the versioning code.  */
   2707   if (h->root.type == bfd_link_hash_indirect)
   2708     return TRUE;
   2709 
   2710   /* Fix the symbol flags.  */
   2711   if (! _bfd_elf_fix_symbol_flags (h, eif))
   2712     return FALSE;
   2713 
   2714   /* If this symbol does not require a PLT entry, and it is not
   2715      defined by a dynamic object, or is not referenced by a regular
   2716      object, ignore it.  We do have to handle a weak defined symbol,
   2717      even if no regular object refers to it, if we decided to add it
   2718      to the dynamic symbol table.  FIXME: Do we normally need to worry
   2719      about symbols which are defined by one dynamic object and
   2720      referenced by another one?  */
   2721   if (!h->needs_plt
   2722       && h->type != STT_GNU_IFUNC
   2723       && (h->def_regular
   2724 	  || !h->def_dynamic
   2725 	  || (!h->ref_regular
   2726 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
   2727     {
   2728       h->plt = elf_hash_table (eif->info)->init_plt_offset;
   2729       return TRUE;
   2730     }
   2731 
   2732   /* If we've already adjusted this symbol, don't do it again.  This
   2733      can happen via a recursive call.  */
   2734   if (h->dynamic_adjusted)
   2735     return TRUE;
   2736 
   2737   /* Don't look at this symbol again.  Note that we must set this
   2738      after checking the above conditions, because we may look at a
   2739      symbol once, decide not to do anything, and then get called
   2740      recursively later after REF_REGULAR is set below.  */
   2741   h->dynamic_adjusted = 1;
   2742 
   2743   /* If this is a weak definition, and we know a real definition, and
   2744      the real symbol is not itself defined by a regular object file,
   2745      then get a good value for the real definition.  We handle the
   2746      real symbol first, for the convenience of the backend routine.
   2747 
   2748      Note that there is a confusing case here.  If the real definition
   2749      is defined by a regular object file, we don't get the real symbol
   2750      from the dynamic object, but we do get the weak symbol.  If the
   2751      processor backend uses a COPY reloc, then if some routine in the
   2752      dynamic object changes the real symbol, we will not see that
   2753      change in the corresponding weak symbol.  This is the way other
   2754      ELF linkers work as well, and seems to be a result of the shared
   2755      library model.
   2756 
   2757      I will clarify this issue.  Most SVR4 shared libraries define the
   2758      variable _timezone and define timezone as a weak synonym.  The
   2759      tzset call changes _timezone.  If you write
   2760        extern int timezone;
   2761        int _timezone = 5;
   2762        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
   2763      you might expect that, since timezone is a synonym for _timezone,
   2764      the same number will print both times.  However, if the processor
   2765      backend uses a COPY reloc, then actually timezone will be copied
   2766      into your process image, and, since you define _timezone
   2767      yourself, _timezone will not.  Thus timezone and _timezone will
   2768      wind up at different memory locations.  The tzset call will set
   2769      _timezone, leaving timezone unchanged.  */
   2770 
   2771   if (h->u.weakdef != NULL)
   2772     {
   2773       /* If we get to this point, there is an implicit reference to
   2774 	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
   2775       h->u.weakdef->ref_regular = 1;
   2776 
   2777       /* Ensure that the backend adjust_dynamic_symbol function sees
   2778 	 H->U.WEAKDEF before H by recursively calling ourselves.  */
   2779       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
   2780 	return FALSE;
   2781     }
   2782 
   2783   /* If a symbol has no type and no size and does not require a PLT
   2784      entry, then we are probably about to do the wrong thing here: we
   2785      are probably going to create a COPY reloc for an empty object.
   2786      This case can arise when a shared object is built with assembly
   2787      code, and the assembly code fails to set the symbol type.  */
   2788   if (h->size == 0
   2789       && h->type == STT_NOTYPE
   2790       && !h->needs_plt)
   2791     (*_bfd_error_handler)
   2792       (_("warning: type and size of dynamic symbol `%s' are not defined"),
   2793        h->root.root.string);
   2794 
   2795   dynobj = elf_hash_table (eif->info)->dynobj;
   2796   bed = get_elf_backend_data (dynobj);
   2797 
   2798   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
   2799     {
   2800       eif->failed = TRUE;
   2801       return FALSE;
   2802     }
   2803 
   2804   return TRUE;
   2805 }
   2806 
   2807 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
   2808    DYNBSS.  */
   2809 
   2810 bfd_boolean
   2811 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   2812 			      struct elf_link_hash_entry *h,
   2813 			      asection *dynbss)
   2814 {
   2815   unsigned int power_of_two;
   2816   bfd_vma mask;
   2817   asection *sec = h->root.u.def.section;
   2818 
   2819   /* The section aligment of definition is the maximum alignment
   2820      requirement of symbols defined in the section.  Since we don't
   2821      know the symbol alignment requirement, we start with the
   2822      maximum alignment and check low bits of the symbol address
   2823      for the minimum alignment.  */
   2824   power_of_two = bfd_get_section_alignment (sec->owner, sec);
   2825   mask = ((bfd_vma) 1 << power_of_two) - 1;
   2826   while ((h->root.u.def.value & mask) != 0)
   2827     {
   2828        mask >>= 1;
   2829        --power_of_two;
   2830     }
   2831 
   2832   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
   2833 						dynbss))
   2834     {
   2835       /* Adjust the section alignment if needed.  */
   2836       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
   2837 				       power_of_two))
   2838 	return FALSE;
   2839     }
   2840 
   2841   /* We make sure that the symbol will be aligned properly.  */
   2842   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
   2843 
   2844   /* Define the symbol as being at this point in DYNBSS.  */
   2845   h->root.u.def.section = dynbss;
   2846   h->root.u.def.value = dynbss->size;
   2847 
   2848   /* Increment the size of DYNBSS to make room for the symbol.  */
   2849   dynbss->size += h->size;
   2850 
   2851   /* No error if extern_protected_data is true.  */
   2852   if (h->protected_def
   2853       && (!info->extern_protected_data
   2854 	  || (info->extern_protected_data < 0
   2855 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
   2856     info->callbacks->einfo
   2857       (_("%P: copy reloc against protected `%T' is dangerous\n"),
   2858        h->root.root.string);
   2859 
   2860   return TRUE;
   2861 }
   2862 
   2863 /* Adjust all external symbols pointing into SEC_MERGE sections
   2864    to reflect the object merging within the sections.  */
   2865 
   2866 static bfd_boolean
   2867 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
   2868 {
   2869   asection *sec;
   2870 
   2871   if ((h->root.type == bfd_link_hash_defined
   2872        || h->root.type == bfd_link_hash_defweak)
   2873       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
   2874       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   2875     {
   2876       bfd *output_bfd = (bfd *) data;
   2877 
   2878       h->root.u.def.value =
   2879 	_bfd_merged_section_offset (output_bfd,
   2880 				    &h->root.u.def.section,
   2881 				    elf_section_data (sec)->sec_info,
   2882 				    h->root.u.def.value);
   2883     }
   2884 
   2885   return TRUE;
   2886 }
   2887 
   2888 /* Returns false if the symbol referred to by H should be considered
   2889    to resolve local to the current module, and true if it should be
   2890    considered to bind dynamically.  */
   2891 
   2892 bfd_boolean
   2893 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
   2894 			   struct bfd_link_info *info,
   2895 			   bfd_boolean not_local_protected)
   2896 {
   2897   bfd_boolean binding_stays_local_p;
   2898   const struct elf_backend_data *bed;
   2899   struct elf_link_hash_table *hash_table;
   2900 
   2901   if (h == NULL)
   2902     return FALSE;
   2903 
   2904   while (h->root.type == bfd_link_hash_indirect
   2905 	 || h->root.type == bfd_link_hash_warning)
   2906     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2907 
   2908   /* If it was forced local, then clearly it's not dynamic.  */
   2909   if (h->dynindx == -1)
   2910     return FALSE;
   2911   if (h->forced_local)
   2912     return FALSE;
   2913 
   2914   /* Identify the cases where name binding rules say that a
   2915      visible symbol resolves locally.  */
   2916   binding_stays_local_p = (bfd_link_executable (info)
   2917 			   || SYMBOLIC_BIND (info, h));
   2918 
   2919   switch (ELF_ST_VISIBILITY (h->other))
   2920     {
   2921     case STV_INTERNAL:
   2922     case STV_HIDDEN:
   2923       return FALSE;
   2924 
   2925     case STV_PROTECTED:
   2926       hash_table = elf_hash_table (info);
   2927       if (!is_elf_hash_table (hash_table))
   2928 	return FALSE;
   2929 
   2930       bed = get_elf_backend_data (hash_table->dynobj);
   2931 
   2932       /* Proper resolution for function pointer equality may require
   2933 	 that these symbols perhaps be resolved dynamically, even though
   2934 	 we should be resolving them to the current module.  */
   2935       if (!not_local_protected || !bed->is_function_type (h->type))
   2936 	binding_stays_local_p = TRUE;
   2937       break;
   2938 
   2939     default:
   2940       break;
   2941     }
   2942 
   2943   /* If it isn't defined locally, then clearly it's dynamic.  */
   2944   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
   2945     return TRUE;
   2946 
   2947   /* Otherwise, the symbol is dynamic if binding rules don't tell
   2948      us that it remains local.  */
   2949   return !binding_stays_local_p;
   2950 }
   2951 
   2952 /* Return true if the symbol referred to by H should be considered
   2953    to resolve local to the current module, and false otherwise.  Differs
   2954    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
   2955    undefined symbols.  The two functions are virtually identical except
   2956    for the place where forced_local and dynindx == -1 are tested.  If
   2957    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
   2958    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
   2959    the symbol is local only for defined symbols.
   2960    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
   2961    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
   2962    treatment of undefined weak symbols.  For those that do not make
   2963    undefined weak symbols dynamic, both functions may return false.  */
   2964 
   2965 bfd_boolean
   2966 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   2967 			      struct bfd_link_info *info,
   2968 			      bfd_boolean local_protected)
   2969 {
   2970   const struct elf_backend_data *bed;
   2971   struct elf_link_hash_table *hash_table;
   2972 
   2973   /* If it's a local sym, of course we resolve locally.  */
   2974   if (h == NULL)
   2975     return TRUE;
   2976 
   2977   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
   2978   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
   2979       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   2980     return TRUE;
   2981 
   2982   /* Common symbols that become definitions don't get the DEF_REGULAR
   2983      flag set, so test it first, and don't bail out.  */
   2984   if (ELF_COMMON_DEF_P (h))
   2985     /* Do nothing.  */;
   2986   /* If we don't have a definition in a regular file, then we can't
   2987      resolve locally.  The sym is either undefined or dynamic.  */
   2988   else if (!h->def_regular)
   2989     return FALSE;
   2990 
   2991   /* Forced local symbols resolve locally.  */
   2992   if (h->forced_local)
   2993     return TRUE;
   2994 
   2995   /* As do non-dynamic symbols.  */
   2996   if (h->dynindx == -1)
   2997     return TRUE;
   2998 
   2999   /* At this point, we know the symbol is defined and dynamic.  In an
   3000      executable it must resolve locally, likewise when building symbolic
   3001      shared libraries.  */
   3002   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
   3003     return TRUE;
   3004 
   3005   /* Now deal with defined dynamic symbols in shared libraries.  Ones
   3006      with default visibility might not resolve locally.  */
   3007   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3008     return FALSE;
   3009 
   3010   hash_table = elf_hash_table (info);
   3011   if (!is_elf_hash_table (hash_table))
   3012     return TRUE;
   3013 
   3014   bed = get_elf_backend_data (hash_table->dynobj);
   3015 
   3016   /* If extern_protected_data is false, STV_PROTECTED non-function
   3017      symbols are local.  */
   3018   if ((!info->extern_protected_data
   3019        || (info->extern_protected_data < 0
   3020 	   && !bed->extern_protected_data))
   3021       && !bed->is_function_type (h->type))
   3022     return TRUE;
   3023 
   3024   /* Function pointer equality tests may require that STV_PROTECTED
   3025      symbols be treated as dynamic symbols.  If the address of a
   3026      function not defined in an executable is set to that function's
   3027      plt entry in the executable, then the address of the function in
   3028      a shared library must also be the plt entry in the executable.  */
   3029   return local_protected;
   3030 }
   3031 
   3032 /* Caches some TLS segment info, and ensures that the TLS segment vma is
   3033    aligned.  Returns the first TLS output section.  */
   3034 
   3035 struct bfd_section *
   3036 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   3037 {
   3038   struct bfd_section *sec, *tls;
   3039   unsigned int align = 0;
   3040 
   3041   for (sec = obfd->sections; sec != NULL; sec = sec->next)
   3042     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
   3043       break;
   3044   tls = sec;
   3045 
   3046   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
   3047     if (sec->alignment_power > align)
   3048       align = sec->alignment_power;
   3049 
   3050   elf_hash_table (info)->tls_sec = tls;
   3051 
   3052   /* Ensure the alignment of the first section is the largest alignment,
   3053      so that the tls segment starts aligned.  */
   3054   if (tls != NULL)
   3055     tls->alignment_power = align;
   3056 
   3057   return tls;
   3058 }
   3059 
   3060 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
   3061 static bfd_boolean
   3062 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
   3063 				  Elf_Internal_Sym *sym)
   3064 {
   3065   const struct elf_backend_data *bed;
   3066 
   3067   /* Local symbols do not count, but target specific ones might.  */
   3068   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
   3069       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
   3070     return FALSE;
   3071 
   3072   bed = get_elf_backend_data (abfd);
   3073   /* Function symbols do not count.  */
   3074   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
   3075     return FALSE;
   3076 
   3077   /* If the section is undefined, then so is the symbol.  */
   3078   if (sym->st_shndx == SHN_UNDEF)
   3079     return FALSE;
   3080 
   3081   /* If the symbol is defined in the common section, then
   3082      it is a common definition and so does not count.  */
   3083   if (bed->common_definition (sym))
   3084     return FALSE;
   3085 
   3086   /* If the symbol is in a target specific section then we
   3087      must rely upon the backend to tell us what it is.  */
   3088   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
   3089     /* FIXME - this function is not coded yet:
   3090 
   3091        return _bfd_is_global_symbol_definition (abfd, sym);
   3092 
   3093        Instead for now assume that the definition is not global,
   3094        Even if this is wrong, at least the linker will behave
   3095        in the same way that it used to do.  */
   3096     return FALSE;
   3097 
   3098   return TRUE;
   3099 }
   3100 
   3101 /* Search the symbol table of the archive element of the archive ABFD
   3102    whose archive map contains a mention of SYMDEF, and determine if
   3103    the symbol is defined in this element.  */
   3104 static bfd_boolean
   3105 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   3106 {
   3107   Elf_Internal_Shdr * hdr;
   3108   bfd_size_type symcount;
   3109   bfd_size_type extsymcount;
   3110   bfd_size_type extsymoff;
   3111   Elf_Internal_Sym *isymbuf;
   3112   Elf_Internal_Sym *isym;
   3113   Elf_Internal_Sym *isymend;
   3114   bfd_boolean result;
   3115 
   3116   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   3117   if (abfd == NULL)
   3118     return FALSE;
   3119 
   3120   /* Return FALSE if the object has been claimed by plugin.  */
   3121   if (abfd->plugin_format == bfd_plugin_yes)
   3122     return FALSE;
   3123 
   3124   if (! bfd_check_format (abfd, bfd_object))
   3125     return FALSE;
   3126 
   3127   /* Select the appropriate symbol table.  */
   3128   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
   3129     hdr = &elf_tdata (abfd)->symtab_hdr;
   3130   else
   3131     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3132 
   3133   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3134 
   3135   /* The sh_info field of the symtab header tells us where the
   3136      external symbols start.  We don't care about the local symbols.  */
   3137   if (elf_bad_symtab (abfd))
   3138     {
   3139       extsymcount = symcount;
   3140       extsymoff = 0;
   3141     }
   3142   else
   3143     {
   3144       extsymcount = symcount - hdr->sh_info;
   3145       extsymoff = hdr->sh_info;
   3146     }
   3147 
   3148   if (extsymcount == 0)
   3149     return FALSE;
   3150 
   3151   /* Read in the symbol table.  */
   3152   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3153 				  NULL, NULL, NULL);
   3154   if (isymbuf == NULL)
   3155     return FALSE;
   3156 
   3157   /* Scan the symbol table looking for SYMDEF.  */
   3158   result = FALSE;
   3159   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
   3160     {
   3161       const char *name;
   3162 
   3163       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   3164 					      isym->st_name);
   3165       if (name == NULL)
   3166 	break;
   3167 
   3168       if (strcmp (name, symdef->name) == 0)
   3169 	{
   3170 	  result = is_global_data_symbol_definition (abfd, isym);
   3171 	  break;
   3172 	}
   3173     }
   3174 
   3175   free (isymbuf);
   3176 
   3177   return result;
   3178 }
   3179 
   3180 /* Add an entry to the .dynamic table.  */
   3182 
   3183 bfd_boolean
   3184 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   3185 			    bfd_vma tag,
   3186 			    bfd_vma val)
   3187 {
   3188   struct elf_link_hash_table *hash_table;
   3189   const struct elf_backend_data *bed;
   3190   asection *s;
   3191   bfd_size_type newsize;
   3192   bfd_byte *newcontents;
   3193   Elf_Internal_Dyn dyn;
   3194 
   3195   hash_table = elf_hash_table (info);
   3196   if (! is_elf_hash_table (hash_table))
   3197     return FALSE;
   3198 
   3199   bed = get_elf_backend_data (hash_table->dynobj);
   3200   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3201   BFD_ASSERT (s != NULL);
   3202 
   3203   newsize = s->size + bed->s->sizeof_dyn;
   3204   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
   3205   if (newcontents == NULL)
   3206     return FALSE;
   3207 
   3208   dyn.d_tag = tag;
   3209   dyn.d_un.d_val = val;
   3210   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
   3211 
   3212   s->size = newsize;
   3213   s->contents = newcontents;
   3214 
   3215   return TRUE;
   3216 }
   3217 
   3218 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
   3219    otherwise just check whether one already exists.  Returns -1 on error,
   3220    1 if a DT_NEEDED tag already exists, and 0 on success.  */
   3221 
   3222 static int
   3223 elf_add_dt_needed_tag (bfd *abfd,
   3224 		       struct bfd_link_info *info,
   3225 		       const char *soname,
   3226 		       bfd_boolean do_it)
   3227 {
   3228   struct elf_link_hash_table *hash_table;
   3229   bfd_size_type strindex;
   3230 
   3231   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
   3232     return -1;
   3233 
   3234   hash_table = elf_hash_table (info);
   3235   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
   3236   if (strindex == (bfd_size_type) -1)
   3237     return -1;
   3238 
   3239   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
   3240     {
   3241       asection *sdyn;
   3242       const struct elf_backend_data *bed;
   3243       bfd_byte *extdyn;
   3244 
   3245       bed = get_elf_backend_data (hash_table->dynobj);
   3246       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
   3247       if (sdyn != NULL)
   3248 	for (extdyn = sdyn->contents;
   3249 	     extdyn < sdyn->contents + sdyn->size;
   3250 	     extdyn += bed->s->sizeof_dyn)
   3251 	  {
   3252 	    Elf_Internal_Dyn dyn;
   3253 
   3254 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
   3255 	    if (dyn.d_tag == DT_NEEDED
   3256 		&& dyn.d_un.d_val == strindex)
   3257 	      {
   3258 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3259 		return 1;
   3260 	      }
   3261 	  }
   3262     }
   3263 
   3264   if (do_it)
   3265     {
   3266       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
   3267 	return -1;
   3268 
   3269       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
   3270 	return -1;
   3271     }
   3272   else
   3273     /* We were just checking for existence of the tag.  */
   3274     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
   3275 
   3276   return 0;
   3277 }
   3278 
   3279 static bfd_boolean
   3280 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
   3281 {
   3282   for (; needed != NULL; needed = needed->next)
   3283     if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
   3284 	&& strcmp (soname, needed->name) == 0)
   3285       return TRUE;
   3286 
   3287   return FALSE;
   3288 }
   3289 
   3290 /* Sort symbol by value, section, and size.  */
   3291 static int
   3292 elf_sort_symbol (const void *arg1, const void *arg2)
   3293 {
   3294   const struct elf_link_hash_entry *h1;
   3295   const struct elf_link_hash_entry *h2;
   3296   bfd_signed_vma vdiff;
   3297 
   3298   h1 = *(const struct elf_link_hash_entry **) arg1;
   3299   h2 = *(const struct elf_link_hash_entry **) arg2;
   3300   vdiff = h1->root.u.def.value - h2->root.u.def.value;
   3301   if (vdiff != 0)
   3302     return vdiff > 0 ? 1 : -1;
   3303   else
   3304     {
   3305       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
   3306       if (sdiff != 0)
   3307 	return sdiff > 0 ? 1 : -1;
   3308     }
   3309   vdiff = h1->size - h2->size;
   3310   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
   3311 }
   3312 
   3313 /* This function is used to adjust offsets into .dynstr for
   3314    dynamic symbols.  This is called via elf_link_hash_traverse.  */
   3315 
   3316 static bfd_boolean
   3317 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
   3318 {
   3319   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
   3320 
   3321   if (h->dynindx != -1)
   3322     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   3323   return TRUE;
   3324 }
   3325 
   3326 /* Assign string offsets in .dynstr, update all structures referencing
   3327    them.  */
   3328 
   3329 static bfd_boolean
   3330 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   3331 {
   3332   struct elf_link_hash_table *hash_table = elf_hash_table (info);
   3333   struct elf_link_local_dynamic_entry *entry;
   3334   struct elf_strtab_hash *dynstr = hash_table->dynstr;
   3335   bfd *dynobj = hash_table->dynobj;
   3336   asection *sdyn;
   3337   bfd_size_type size;
   3338   const struct elf_backend_data *bed;
   3339   bfd_byte *extdyn;
   3340 
   3341   _bfd_elf_strtab_finalize (dynstr);
   3342   size = _bfd_elf_strtab_size (dynstr);
   3343 
   3344   bed = get_elf_backend_data (dynobj);
   3345   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3346   BFD_ASSERT (sdyn != NULL);
   3347 
   3348   /* Update all .dynamic entries referencing .dynstr strings.  */
   3349   for (extdyn = sdyn->contents;
   3350        extdyn < sdyn->contents + sdyn->size;
   3351        extdyn += bed->s->sizeof_dyn)
   3352     {
   3353       Elf_Internal_Dyn dyn;
   3354 
   3355       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
   3356       switch (dyn.d_tag)
   3357 	{
   3358 	case DT_STRSZ:
   3359 	  dyn.d_un.d_val = size;
   3360 	  break;
   3361 	case DT_NEEDED:
   3362 	case DT_SONAME:
   3363 	case DT_RPATH:
   3364 	case DT_RUNPATH:
   3365 	case DT_FILTER:
   3366 	case DT_AUXILIARY:
   3367 	case DT_AUDIT:
   3368 	case DT_DEPAUDIT:
   3369 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
   3370 	  break;
   3371 	default:
   3372 	  continue;
   3373 	}
   3374       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
   3375     }
   3376 
   3377   /* Now update local dynamic symbols.  */
   3378   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
   3379     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
   3380 						  entry->isym.st_name);
   3381 
   3382   /* And the rest of dynamic symbols.  */
   3383   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
   3384 
   3385   /* Adjust version definitions.  */
   3386   if (elf_tdata (output_bfd)->cverdefs)
   3387     {
   3388       asection *s;
   3389       bfd_byte *p;
   3390       bfd_size_type i;
   3391       Elf_Internal_Verdef def;
   3392       Elf_Internal_Verdaux defaux;
   3393 
   3394       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   3395       p = s->contents;
   3396       do
   3397 	{
   3398 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
   3399 				   &def);
   3400 	  p += sizeof (Elf_External_Verdef);
   3401 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
   3402 	    continue;
   3403 	  for (i = 0; i < def.vd_cnt; ++i)
   3404 	    {
   3405 	      _bfd_elf_swap_verdaux_in (output_bfd,
   3406 					(Elf_External_Verdaux *) p, &defaux);
   3407 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
   3408 							defaux.vda_name);
   3409 	      _bfd_elf_swap_verdaux_out (output_bfd,
   3410 					 &defaux, (Elf_External_Verdaux *) p);
   3411 	      p += sizeof (Elf_External_Verdaux);
   3412 	    }
   3413 	}
   3414       while (def.vd_next);
   3415     }
   3416 
   3417   /* Adjust version references.  */
   3418   if (elf_tdata (output_bfd)->verref)
   3419     {
   3420       asection *s;
   3421       bfd_byte *p;
   3422       bfd_size_type i;
   3423       Elf_Internal_Verneed need;
   3424       Elf_Internal_Vernaux needaux;
   3425 
   3426       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   3427       p = s->contents;
   3428       do
   3429 	{
   3430 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
   3431 				    &need);
   3432 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
   3433 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
   3434 				     (Elf_External_Verneed *) p);
   3435 	  p += sizeof (Elf_External_Verneed);
   3436 	  for (i = 0; i < need.vn_cnt; ++i)
   3437 	    {
   3438 	      _bfd_elf_swap_vernaux_in (output_bfd,
   3439 					(Elf_External_Vernaux *) p, &needaux);
   3440 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
   3441 							 needaux.vna_name);
   3442 	      _bfd_elf_swap_vernaux_out (output_bfd,
   3443 					 &needaux,
   3444 					 (Elf_External_Vernaux *) p);
   3445 	      p += sizeof (Elf_External_Vernaux);
   3446 	    }
   3447 	}
   3448       while (need.vn_next);
   3449     }
   3450 
   3451   return TRUE;
   3452 }
   3453 
   3454 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3456    The default is to only match when the INPUT and OUTPUT are exactly
   3457    the same target.  */
   3458 
   3459 bfd_boolean
   3460 _bfd_elf_default_relocs_compatible (const bfd_target *input,
   3461 				    const bfd_target *output)
   3462 {
   3463   return input == output;
   3464 }
   3465 
   3466 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
   3467    This version is used when different targets for the same architecture
   3468    are virtually identical.  */
   3469 
   3470 bfd_boolean
   3471 _bfd_elf_relocs_compatible (const bfd_target *input,
   3472 			    const bfd_target *output)
   3473 {
   3474   const struct elf_backend_data *obed, *ibed;
   3475 
   3476   if (input == output)
   3477     return TRUE;
   3478 
   3479   ibed = xvec_get_elf_backend_data (input);
   3480   obed = xvec_get_elf_backend_data (output);
   3481 
   3482   if (ibed->arch != obed->arch)
   3483     return FALSE;
   3484 
   3485   /* If both backends are using this function, deem them compatible.  */
   3486   return ibed->relocs_compatible == obed->relocs_compatible;
   3487 }
   3488 
   3489 /* Make a special call to the linker "notice" function to tell it that
   3490    we are about to handle an as-needed lib, or have finished
   3491    processing the lib.  */
   3492 
   3493 bfd_boolean
   3494 _bfd_elf_notice_as_needed (bfd *ibfd,
   3495 			   struct bfd_link_info *info,
   3496 			   enum notice_asneeded_action act)
   3497 {
   3498   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
   3499 }
   3500 
   3501 /* Add symbols from an ELF object file to the linker hash table.  */
   3502 
   3503 static bfd_boolean
   3504 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   3505 {
   3506   Elf_Internal_Ehdr *ehdr;
   3507   Elf_Internal_Shdr *hdr;
   3508   bfd_size_type symcount;
   3509   bfd_size_type extsymcount;
   3510   bfd_size_type extsymoff;
   3511   struct elf_link_hash_entry **sym_hash;
   3512   bfd_boolean dynamic;
   3513   Elf_External_Versym *extversym = NULL;
   3514   Elf_External_Versym *ever;
   3515   struct elf_link_hash_entry *weaks;
   3516   struct elf_link_hash_entry **nondeflt_vers = NULL;
   3517   bfd_size_type nondeflt_vers_cnt = 0;
   3518   Elf_Internal_Sym *isymbuf = NULL;
   3519   Elf_Internal_Sym *isym;
   3520   Elf_Internal_Sym *isymend;
   3521   const struct elf_backend_data *bed;
   3522   bfd_boolean add_needed;
   3523   struct elf_link_hash_table *htab;
   3524   bfd_size_type amt;
   3525   void *alloc_mark = NULL;
   3526   struct bfd_hash_entry **old_table = NULL;
   3527   unsigned int old_size = 0;
   3528   unsigned int old_count = 0;
   3529   void *old_tab = NULL;
   3530   void *old_ent;
   3531   struct bfd_link_hash_entry *old_undefs = NULL;
   3532   struct bfd_link_hash_entry *old_undefs_tail = NULL;
   3533   void *old_strtab = NULL;
   3534   size_t tabsize = 0;
   3535   asection *s;
   3536   bfd_boolean just_syms;
   3537 
   3538   htab = elf_hash_table (info);
   3539   bed = get_elf_backend_data (abfd);
   3540 
   3541   if ((abfd->flags & DYNAMIC) == 0)
   3542     dynamic = FALSE;
   3543   else
   3544     {
   3545       dynamic = TRUE;
   3546 
   3547       /* You can't use -r against a dynamic object.  Also, there's no
   3548 	 hope of using a dynamic object which does not exactly match
   3549 	 the format of the output file.  */
   3550       if (bfd_link_relocatable (info)
   3551 	  || !is_elf_hash_table (htab)
   3552 	  || info->output_bfd->xvec != abfd->xvec)
   3553 	{
   3554 	  if (bfd_link_relocatable (info))
   3555 	    bfd_set_error (bfd_error_invalid_operation);
   3556 	  else
   3557 	    bfd_set_error (bfd_error_wrong_format);
   3558 	  goto error_return;
   3559 	}
   3560     }
   3561 
   3562   ehdr = elf_elfheader (abfd);
   3563   if (info->warn_alternate_em
   3564       && bed->elf_machine_code != ehdr->e_machine
   3565       && ((bed->elf_machine_alt1 != 0
   3566 	   && ehdr->e_machine == bed->elf_machine_alt1)
   3567 	  || (bed->elf_machine_alt2 != 0
   3568 	      && ehdr->e_machine == bed->elf_machine_alt2)))
   3569     info->callbacks->einfo
   3570       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
   3571        ehdr->e_machine, abfd, bed->elf_machine_code);
   3572 
   3573   /* As a GNU extension, any input sections which are named
   3574      .gnu.warning.SYMBOL are treated as warning symbols for the given
   3575      symbol.  This differs from .gnu.warning sections, which generate
   3576      warnings when they are included in an output file.  */
   3577   /* PR 12761: Also generate this warning when building shared libraries.  */
   3578   for (s = abfd->sections; s != NULL; s = s->next)
   3579     {
   3580       const char *name;
   3581 
   3582       name = bfd_get_section_name (abfd, s);
   3583       if (CONST_STRNEQ (name, ".gnu.warning."))
   3584 	{
   3585 	  char *msg;
   3586 	  bfd_size_type sz;
   3587 
   3588 	  name += sizeof ".gnu.warning." - 1;
   3589 
   3590 	  /* If this is a shared object, then look up the symbol
   3591 	     in the hash table.  If it is there, and it is already
   3592 	     been defined, then we will not be using the entry
   3593 	     from this shared object, so we don't need to warn.
   3594 	     FIXME: If we see the definition in a regular object
   3595 	     later on, we will warn, but we shouldn't.  The only
   3596 	     fix is to keep track of what warnings we are supposed
   3597 	     to emit, and then handle them all at the end of the
   3598 	     link.  */
   3599 	  if (dynamic)
   3600 	    {
   3601 	      struct elf_link_hash_entry *h;
   3602 
   3603 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
   3604 
   3605 	      /* FIXME: What about bfd_link_hash_common?  */
   3606 	      if (h != NULL
   3607 		  && (h->root.type == bfd_link_hash_defined
   3608 		      || h->root.type == bfd_link_hash_defweak))
   3609 		continue;
   3610 	    }
   3611 
   3612 	  sz = s->size;
   3613 	  msg = (char *) bfd_alloc (abfd, sz + 1);
   3614 	  if (msg == NULL)
   3615 	    goto error_return;
   3616 
   3617 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
   3618 	    goto error_return;
   3619 
   3620 	  msg[sz] = '\0';
   3621 
   3622 	  if (! (_bfd_generic_link_add_one_symbol
   3623 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
   3624 		  FALSE, bed->collect, NULL)))
   3625 	    goto error_return;
   3626 
   3627 	  if (bfd_link_executable (info))
   3628 	    {
   3629 	      /* Clobber the section size so that the warning does
   3630 		 not get copied into the output file.  */
   3631 	      s->size = 0;
   3632 
   3633 	      /* Also set SEC_EXCLUDE, so that symbols defined in
   3634 		 the warning section don't get copied to the output.  */
   3635 	      s->flags |= SEC_EXCLUDE;
   3636 	    }
   3637 	}
   3638     }
   3639 
   3640   just_syms = ((s = abfd->sections) != NULL
   3641 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
   3642 
   3643   add_needed = TRUE;
   3644   if (! dynamic)
   3645     {
   3646       /* If we are creating a shared library, create all the dynamic
   3647 	 sections immediately.  We need to attach them to something,
   3648 	 so we attach them to this BFD, provided it is the right
   3649 	 format and is not from ld --just-symbols.  FIXME: If there
   3650 	 are no input BFD's of the same format as the output, we can't
   3651 	 make a shared library.  */
   3652       if (!just_syms
   3653 	  && bfd_link_pic (info)
   3654 	  && is_elf_hash_table (htab)
   3655 	  && info->output_bfd->xvec == abfd->xvec
   3656 	  && !htab->dynamic_sections_created)
   3657 	{
   3658 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
   3659 	    goto error_return;
   3660 	}
   3661     }
   3662   else if (!is_elf_hash_table (htab))
   3663     goto error_return;
   3664   else
   3665     {
   3666       const char *soname = NULL;
   3667       char *audit = NULL;
   3668       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
   3669       int ret;
   3670 
   3671       /* ld --just-symbols and dynamic objects don't mix very well.
   3672 	 ld shouldn't allow it.  */
   3673       if (just_syms)
   3674 	abort ();
   3675 
   3676       /* If this dynamic lib was specified on the command line with
   3677 	 --as-needed in effect, then we don't want to add a DT_NEEDED
   3678 	 tag unless the lib is actually used.  Similary for libs brought
   3679 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
   3680 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
   3681 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
   3682 	 all.  */
   3683       add_needed = (elf_dyn_lib_class (abfd)
   3684 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
   3685 		       | DYN_NO_NEEDED)) == 0;
   3686 
   3687       s = bfd_get_section_by_name (abfd, ".dynamic");
   3688       if (s != NULL)
   3689 	{
   3690 	  bfd_byte *dynbuf;
   3691 	  bfd_byte *extdyn;
   3692 	  unsigned int elfsec;
   3693 	  unsigned long shlink;
   3694 
   3695 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   3696 	    {
   3697 error_free_dyn:
   3698 	      free (dynbuf);
   3699 	      goto error_return;
   3700 	    }
   3701 
   3702 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   3703 	  if (elfsec == SHN_BAD)
   3704 	    goto error_free_dyn;
   3705 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   3706 
   3707 	  for (extdyn = dynbuf;
   3708 	       extdyn < dynbuf + s->size;
   3709 	       extdyn += bed->s->sizeof_dyn)
   3710 	    {
   3711 	      Elf_Internal_Dyn dyn;
   3712 
   3713 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
   3714 	      if (dyn.d_tag == DT_SONAME)
   3715 		{
   3716 		  unsigned int tagv = dyn.d_un.d_val;
   3717 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3718 		  if (soname == NULL)
   3719 		    goto error_free_dyn;
   3720 		}
   3721 	      if (dyn.d_tag == DT_NEEDED)
   3722 		{
   3723 		  struct bfd_link_needed_list *n, **pn;
   3724 		  char *fnm, *anm;
   3725 		  unsigned int tagv = dyn.d_un.d_val;
   3726 
   3727 		  amt = sizeof (struct bfd_link_needed_list);
   3728 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3729 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3730 		  if (n == NULL || fnm == NULL)
   3731 		    goto error_free_dyn;
   3732 		  amt = strlen (fnm) + 1;
   3733 		  anm = (char *) bfd_alloc (abfd, amt);
   3734 		  if (anm == NULL)
   3735 		    goto error_free_dyn;
   3736 		  memcpy (anm, fnm, amt);
   3737 		  n->name = anm;
   3738 		  n->by = abfd;
   3739 		  n->next = NULL;
   3740 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
   3741 		    ;
   3742 		  *pn = n;
   3743 		}
   3744 	      if (dyn.d_tag == DT_RUNPATH)
   3745 		{
   3746 		  struct bfd_link_needed_list *n, **pn;
   3747 		  char *fnm, *anm;
   3748 		  unsigned int tagv = dyn.d_un.d_val;
   3749 
   3750 		  amt = sizeof (struct bfd_link_needed_list);
   3751 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3752 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3753 		  if (n == NULL || fnm == NULL)
   3754 		    goto error_free_dyn;
   3755 		  amt = strlen (fnm) + 1;
   3756 		  anm = (char *) bfd_alloc (abfd, amt);
   3757 		  if (anm == NULL)
   3758 		    goto error_free_dyn;
   3759 		  memcpy (anm, fnm, amt);
   3760 		  n->name = anm;
   3761 		  n->by = abfd;
   3762 		  n->next = NULL;
   3763 		  for (pn = & runpath;
   3764 		       *pn != NULL;
   3765 		       pn = &(*pn)->next)
   3766 		    ;
   3767 		  *pn = n;
   3768 		}
   3769 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
   3770 	      if (!runpath && dyn.d_tag == DT_RPATH)
   3771 		{
   3772 		  struct bfd_link_needed_list *n, **pn;
   3773 		  char *fnm, *anm;
   3774 		  unsigned int tagv = dyn.d_un.d_val;
   3775 
   3776 		  amt = sizeof (struct bfd_link_needed_list);
   3777 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   3778 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3779 		  if (n == NULL || fnm == NULL)
   3780 		    goto error_free_dyn;
   3781 		  amt = strlen (fnm) + 1;
   3782 		  anm = (char *) bfd_alloc (abfd, amt);
   3783 		  if (anm == NULL)
   3784 		    goto error_free_dyn;
   3785 		  memcpy (anm, fnm, amt);
   3786 		  n->name = anm;
   3787 		  n->by = abfd;
   3788 		  n->next = NULL;
   3789 		  for (pn = & rpath;
   3790 		       *pn != NULL;
   3791 		       pn = &(*pn)->next)
   3792 		    ;
   3793 		  *pn = n;
   3794 		}
   3795 	      if (dyn.d_tag == DT_AUDIT)
   3796 		{
   3797 		  unsigned int tagv = dyn.d_un.d_val;
   3798 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   3799 		}
   3800 	    }
   3801 
   3802 	  free (dynbuf);
   3803 	}
   3804 
   3805       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
   3806 	 frees all more recently bfd_alloc'd blocks as well.  */
   3807       if (runpath)
   3808 	rpath = runpath;
   3809 
   3810       if (rpath)
   3811 	{
   3812 	  struct bfd_link_needed_list **pn;
   3813 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
   3814 	    ;
   3815 	  *pn = rpath;
   3816 	}
   3817 
   3818       /* We do not want to include any of the sections in a dynamic
   3819 	 object in the output file.  We hack by simply clobbering the
   3820 	 list of sections in the BFD.  This could be handled more
   3821 	 cleanly by, say, a new section flag; the existing
   3822 	 SEC_NEVER_LOAD flag is not the one we want, because that one
   3823 	 still implies that the section takes up space in the output
   3824 	 file.  */
   3825       bfd_section_list_clear (abfd);
   3826 
   3827       /* Find the name to use in a DT_NEEDED entry that refers to this
   3828 	 object.  If the object has a DT_SONAME entry, we use it.
   3829 	 Otherwise, if the generic linker stuck something in
   3830 	 elf_dt_name, we use that.  Otherwise, we just use the file
   3831 	 name.  */
   3832       if (soname == NULL || *soname == '\0')
   3833 	{
   3834 	  soname = elf_dt_name (abfd);
   3835 	  if (soname == NULL || *soname == '\0')
   3836 	    soname = bfd_get_filename (abfd);
   3837 	}
   3838 
   3839       /* Save the SONAME because sometimes the linker emulation code
   3840 	 will need to know it.  */
   3841       elf_dt_name (abfd) = soname;
   3842 
   3843       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   3844       if (ret < 0)
   3845 	goto error_return;
   3846 
   3847       /* If we have already included this dynamic object in the
   3848 	 link, just ignore it.  There is no reason to include a
   3849 	 particular dynamic object more than once.  */
   3850       if (ret > 0)
   3851 	return TRUE;
   3852 
   3853       /* Save the DT_AUDIT entry for the linker emulation code. */
   3854       elf_dt_audit (abfd) = audit;
   3855     }
   3856 
   3857   /* If this is a dynamic object, we always link against the .dynsym
   3858      symbol table, not the .symtab symbol table.  The dynamic linker
   3859      will only see the .dynsym symbol table, so there is no reason to
   3860      look at .symtab for a dynamic object.  */
   3861 
   3862   if (! dynamic || elf_dynsymtab (abfd) == 0)
   3863     hdr = &elf_tdata (abfd)->symtab_hdr;
   3864   else
   3865     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   3866 
   3867   symcount = hdr->sh_size / bed->s->sizeof_sym;
   3868 
   3869   /* The sh_info field of the symtab header tells us where the
   3870      external symbols start.  We don't care about the local symbols at
   3871      this point.  */
   3872   if (elf_bad_symtab (abfd))
   3873     {
   3874       extsymcount = symcount;
   3875       extsymoff = 0;
   3876     }
   3877   else
   3878     {
   3879       extsymcount = symcount - hdr->sh_info;
   3880       extsymoff = hdr->sh_info;
   3881     }
   3882 
   3883   sym_hash = elf_sym_hashes (abfd);
   3884   if (extsymcount != 0)
   3885     {
   3886       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
   3887 				      NULL, NULL, NULL);
   3888       if (isymbuf == NULL)
   3889 	goto error_return;
   3890 
   3891       if (sym_hash == NULL)
   3892 	{
   3893 	  /* We store a pointer to the hash table entry for each
   3894 	     external symbol.  */
   3895 	  amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   3896 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
   3897 	  if (sym_hash == NULL)
   3898 	    goto error_free_sym;
   3899 	  elf_sym_hashes (abfd) = sym_hash;
   3900 	}
   3901     }
   3902 
   3903   if (dynamic)
   3904     {
   3905       /* Read in any version definitions.  */
   3906       if (!_bfd_elf_slurp_version_tables (abfd,
   3907 					  info->default_imported_symver))
   3908 	goto error_free_sym;
   3909 
   3910       /* Read in the symbol versions, but don't bother to convert them
   3911 	 to internal format.  */
   3912       if (elf_dynversym (abfd) != 0)
   3913 	{
   3914 	  Elf_Internal_Shdr *versymhdr;
   3915 
   3916 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
   3917 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   3918 	  if (extversym == NULL)
   3919 	    goto error_free_sym;
   3920 	  amt = versymhdr->sh_size;
   3921 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
   3922 	      || bfd_bread (extversym, amt, abfd) != amt)
   3923 	    goto error_free_vers;
   3924 	}
   3925     }
   3926 
   3927   /* If we are loading an as-needed shared lib, save the symbol table
   3928      state before we start adding symbols.  If the lib turns out
   3929      to be unneeded, restore the state.  */
   3930   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   3931     {
   3932       unsigned int i;
   3933       size_t entsize;
   3934 
   3935       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
   3936 	{
   3937 	  struct bfd_hash_entry *p;
   3938 	  struct elf_link_hash_entry *h;
   3939 
   3940 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   3941 	    {
   3942 	      h = (struct elf_link_hash_entry *) p;
   3943 	      entsize += htab->root.table.entsize;
   3944 	      if (h->root.type == bfd_link_hash_warning)
   3945 		entsize += htab->root.table.entsize;
   3946 	    }
   3947 	}
   3948 
   3949       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
   3950       old_tab = bfd_malloc (tabsize + entsize);
   3951       if (old_tab == NULL)
   3952 	goto error_free_vers;
   3953 
   3954       /* Remember the current objalloc pointer, so that all mem for
   3955 	 symbols added can later be reclaimed.  */
   3956       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
   3957       if (alloc_mark == NULL)
   3958 	goto error_free_vers;
   3959 
   3960       /* Make a special call to the linker "notice" function to
   3961 	 tell it that we are about to handle an as-needed lib.  */
   3962       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
   3963 	goto error_free_vers;
   3964 
   3965       /* Clone the symbol table.  Remember some pointers into the
   3966 	 symbol table, and dynamic symbol count.  */
   3967       old_ent = (char *) old_tab + tabsize;
   3968       memcpy (old_tab, htab->root.table.table, tabsize);
   3969       old_undefs = htab->root.undefs;
   3970       old_undefs_tail = htab->root.undefs_tail;
   3971       old_table = htab->root.table.table;
   3972       old_size = htab->root.table.size;
   3973       old_count = htab->root.table.count;
   3974       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
   3975       if (old_strtab == NULL)
   3976 	goto error_free_vers;
   3977 
   3978       for (i = 0; i < htab->root.table.size; i++)
   3979 	{
   3980 	  struct bfd_hash_entry *p;
   3981 	  struct elf_link_hash_entry *h;
   3982 
   3983 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   3984 	    {
   3985 	      memcpy (old_ent, p, htab->root.table.entsize);
   3986 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   3987 	      h = (struct elf_link_hash_entry *) p;
   3988 	      if (h->root.type == bfd_link_hash_warning)
   3989 		{
   3990 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
   3991 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   3992 		}
   3993 	    }
   3994 	}
   3995     }
   3996 
   3997   weaks = NULL;
   3998   ever = extversym != NULL ? extversym + extsymoff : NULL;
   3999   for (isym = isymbuf, isymend = isymbuf + extsymcount;
   4000        isym < isymend;
   4001        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
   4002     {
   4003       int bind;
   4004       bfd_vma value;
   4005       asection *sec, *new_sec;
   4006       flagword flags;
   4007       const char *name;
   4008       struct elf_link_hash_entry *h;
   4009       struct elf_link_hash_entry *hi;
   4010       bfd_boolean definition;
   4011       bfd_boolean size_change_ok;
   4012       bfd_boolean type_change_ok;
   4013       bfd_boolean new_weakdef;
   4014       bfd_boolean new_weak;
   4015       bfd_boolean old_weak;
   4016       bfd_boolean override;
   4017       bfd_boolean common;
   4018       unsigned int old_alignment;
   4019       bfd *old_bfd;
   4020       bfd_boolean matched;
   4021 
   4022       override = FALSE;
   4023 
   4024       flags = BSF_NO_FLAGS;
   4025       sec = NULL;
   4026       value = isym->st_value;
   4027       common = bed->common_definition (isym);
   4028 
   4029       bind = ELF_ST_BIND (isym->st_info);
   4030       switch (bind)
   4031 	{
   4032 	case STB_LOCAL:
   4033 	  /* This should be impossible, since ELF requires that all
   4034 	     global symbols follow all local symbols, and that sh_info
   4035 	     point to the first global symbol.  Unfortunately, Irix 5
   4036 	     screws this up.  */
   4037 	  continue;
   4038 
   4039 	case STB_GLOBAL:
   4040 	  if (isym->st_shndx != SHN_UNDEF && !common)
   4041 	    flags = BSF_GLOBAL;
   4042 	  break;
   4043 
   4044 	case STB_WEAK:
   4045 	  flags = BSF_WEAK;
   4046 	  break;
   4047 
   4048 	case STB_GNU_UNIQUE:
   4049 	  flags = BSF_GNU_UNIQUE;
   4050 	  break;
   4051 
   4052 	default:
   4053 	  /* Leave it up to the processor backend.  */
   4054 	  break;
   4055 	}
   4056 
   4057       if (isym->st_shndx == SHN_UNDEF)
   4058 	sec = bfd_und_section_ptr;
   4059       else if (isym->st_shndx == SHN_ABS)
   4060 	sec = bfd_abs_section_ptr;
   4061       else if (isym->st_shndx == SHN_COMMON)
   4062 	{
   4063 	  sec = bfd_com_section_ptr;
   4064 	  /* What ELF calls the size we call the value.  What ELF
   4065 	     calls the value we call the alignment.  */
   4066 	  value = isym->st_size;
   4067 	}
   4068       else
   4069 	{
   4070 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4071 	  if (sec == NULL)
   4072 	    sec = bfd_abs_section_ptr;
   4073 	  else if (discarded_section (sec))
   4074 	    {
   4075 	      /* Symbols from discarded section are undefined.  We keep
   4076 		 its visibility.  */
   4077 	      sec = bfd_und_section_ptr;
   4078 	      isym->st_shndx = SHN_UNDEF;
   4079 	    }
   4080 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   4081 	    value -= sec->vma;
   4082 	}
   4083 
   4084       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   4085 					      isym->st_name);
   4086       if (name == NULL)
   4087 	goto error_free_vers;
   4088 
   4089       if (isym->st_shndx == SHN_COMMON
   4090 	  && (abfd->flags & BFD_PLUGIN) != 0)
   4091 	{
   4092 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   4093 
   4094 	  if (xc == NULL)
   4095 	    {
   4096 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   4097 				 | SEC_EXCLUDE);
   4098 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
   4099 	      if (xc == NULL)
   4100 		goto error_free_vers;
   4101 	    }
   4102 	  sec = xc;
   4103 	}
   4104       else if (isym->st_shndx == SHN_COMMON
   4105 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
   4106 	       && !bfd_link_relocatable (info))
   4107 	{
   4108 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
   4109 
   4110 	  if (tcomm == NULL)
   4111 	    {
   4112 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
   4113 				 | SEC_LINKER_CREATED);
   4114 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
   4115 	      if (tcomm == NULL)
   4116 		goto error_free_vers;
   4117 	    }
   4118 	  sec = tcomm;
   4119 	}
   4120       else if (bed->elf_add_symbol_hook)
   4121 	{
   4122 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
   4123 					     &sec, &value))
   4124 	    goto error_free_vers;
   4125 
   4126 	  /* The hook function sets the name to NULL if this symbol
   4127 	     should be skipped for some reason.  */
   4128 	  if (name == NULL)
   4129 	    continue;
   4130 	}
   4131 
   4132       /* Sanity check that all possibilities were handled.  */
   4133       if (sec == NULL)
   4134 	{
   4135 	  bfd_set_error (bfd_error_bad_value);
   4136 	  goto error_free_vers;
   4137 	}
   4138 
   4139       /* Silently discard TLS symbols from --just-syms.  There's
   4140 	 no way to combine a static TLS block with a new TLS block
   4141 	 for this executable.  */
   4142       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
   4143 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   4144 	continue;
   4145 
   4146       if (bfd_is_und_section (sec)
   4147 	  || bfd_is_com_section (sec))
   4148 	definition = FALSE;
   4149       else
   4150 	definition = TRUE;
   4151 
   4152       size_change_ok = FALSE;
   4153       type_change_ok = bed->type_change_ok;
   4154       old_weak = FALSE;
   4155       matched = FALSE;
   4156       old_alignment = 0;
   4157       old_bfd = NULL;
   4158       new_sec = sec;
   4159 
   4160       if (is_elf_hash_table (htab))
   4161 	{
   4162 	  Elf_Internal_Versym iver;
   4163 	  unsigned int vernum = 0;
   4164 	  bfd_boolean skip;
   4165 
   4166 	  if (ever == NULL)
   4167 	    {
   4168 	      if (info->default_imported_symver)
   4169 		/* Use the default symbol version created earlier.  */
   4170 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
   4171 	      else
   4172 		iver.vs_vers = 0;
   4173 	    }
   4174 	  else
   4175 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
   4176 
   4177 	  vernum = iver.vs_vers & VERSYM_VERSION;
   4178 
   4179 	  /* If this is a hidden symbol, or if it is not version
   4180 	     1, we append the version name to the symbol name.
   4181 	     However, we do not modify a non-hidden absolute symbol
   4182 	     if it is not a function, because it might be the version
   4183 	     symbol itself.  FIXME: What if it isn't?  */
   4184 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
   4185 	      || (vernum > 1
   4186 		  && (!bfd_is_abs_section (sec)
   4187 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
   4188 	    {
   4189 	      const char *verstr;
   4190 	      size_t namelen, verlen, newlen;
   4191 	      char *newname, *p;
   4192 
   4193 	      if (isym->st_shndx != SHN_UNDEF)
   4194 		{
   4195 		  if (vernum > elf_tdata (abfd)->cverdefs)
   4196 		    verstr = NULL;
   4197 		  else if (vernum > 1)
   4198 		    verstr =
   4199 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   4200 		  else
   4201 		    verstr = "";
   4202 
   4203 		  if (verstr == NULL)
   4204 		    {
   4205 		      (*_bfd_error_handler)
   4206 			(_("%B: %s: invalid version %u (max %d)"),
   4207 			 abfd, name, vernum,
   4208 			 elf_tdata (abfd)->cverdefs);
   4209 		      bfd_set_error (bfd_error_bad_value);
   4210 		      goto error_free_vers;
   4211 		    }
   4212 		}
   4213 	      else
   4214 		{
   4215 		  /* We cannot simply test for the number of
   4216 		     entries in the VERNEED section since the
   4217 		     numbers for the needed versions do not start
   4218 		     at 0.  */
   4219 		  Elf_Internal_Verneed *t;
   4220 
   4221 		  verstr = NULL;
   4222 		  for (t = elf_tdata (abfd)->verref;
   4223 		       t != NULL;
   4224 		       t = t->vn_nextref)
   4225 		    {
   4226 		      Elf_Internal_Vernaux *a;
   4227 
   4228 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   4229 			{
   4230 			  if (a->vna_other == vernum)
   4231 			    {
   4232 			      verstr = a->vna_nodename;
   4233 			      break;
   4234 			    }
   4235 			}
   4236 		      if (a != NULL)
   4237 			break;
   4238 		    }
   4239 		  if (verstr == NULL)
   4240 		    {
   4241 		      (*_bfd_error_handler)
   4242 			(_("%B: %s: invalid needed version %d"),
   4243 			 abfd, name, vernum);
   4244 		      bfd_set_error (bfd_error_bad_value);
   4245 		      goto error_free_vers;
   4246 		    }
   4247 		}
   4248 
   4249 	      namelen = strlen (name);
   4250 	      verlen = strlen (verstr);
   4251 	      newlen = namelen + verlen + 2;
   4252 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4253 		  && isym->st_shndx != SHN_UNDEF)
   4254 		++newlen;
   4255 
   4256 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
   4257 	      if (newname == NULL)
   4258 		goto error_free_vers;
   4259 	      memcpy (newname, name, namelen);
   4260 	      p = newname + namelen;
   4261 	      *p++ = ELF_VER_CHR;
   4262 	      /* If this is a defined non-hidden version symbol,
   4263 		 we add another @ to the name.  This indicates the
   4264 		 default version of the symbol.  */
   4265 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   4266 		  && isym->st_shndx != SHN_UNDEF)
   4267 		*p++ = ELF_VER_CHR;
   4268 	      memcpy (p, verstr, verlen + 1);
   4269 
   4270 	      name = newname;
   4271 	    }
   4272 
   4273 	  /* If this symbol has default visibility and the user has
   4274 	     requested we not re-export it, then mark it as hidden.  */
   4275 	  if (!bfd_is_und_section (sec)
   4276 	      && !dynamic
   4277 	      && abfd->no_export
   4278 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
   4279 	    isym->st_other = (STV_HIDDEN
   4280 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
   4281 
   4282 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
   4283 				      sym_hash, &old_bfd, &old_weak,
   4284 				      &old_alignment, &skip, &override,
   4285 				      &type_change_ok, &size_change_ok,
   4286 				      &matched))
   4287 	    goto error_free_vers;
   4288 
   4289 	  if (skip)
   4290 	    continue;
   4291 
   4292 	  /* Override a definition only if the new symbol matches the
   4293 	     existing one.  */
   4294 	  if (override && matched)
   4295 	    definition = FALSE;
   4296 
   4297 	  h = *sym_hash;
   4298 	  while (h->root.type == bfd_link_hash_indirect
   4299 		 || h->root.type == bfd_link_hash_warning)
   4300 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4301 
   4302 	  if (elf_tdata (abfd)->verdef != NULL
   4303 	      && vernum > 1
   4304 	      && definition)
   4305 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
   4306 	}
   4307 
   4308       if (! (_bfd_generic_link_add_one_symbol
   4309 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
   4310 	      (struct bfd_link_hash_entry **) sym_hash)))
   4311 	goto error_free_vers;
   4312 
   4313       h = *sym_hash;
   4314       /* We need to make sure that indirect symbol dynamic flags are
   4315 	 updated.  */
   4316       hi = h;
   4317       while (h->root.type == bfd_link_hash_indirect
   4318 	     || h->root.type == bfd_link_hash_warning)
   4319 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4320 
   4321       *sym_hash = h;
   4322 
   4323       new_weak = (flags & BSF_WEAK) != 0;
   4324       new_weakdef = FALSE;
   4325       if (dynamic
   4326 	  && definition
   4327 	  && new_weak
   4328 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
   4329 	  && is_elf_hash_table (htab)
   4330 	  && h->u.weakdef == NULL)
   4331 	{
   4332 	  /* Keep a list of all weak defined non function symbols from
   4333 	     a dynamic object, using the weakdef field.  Later in this
   4334 	     function we will set the weakdef field to the correct
   4335 	     value.  We only put non-function symbols from dynamic
   4336 	     objects on this list, because that happens to be the only
   4337 	     time we need to know the normal symbol corresponding to a
   4338 	     weak symbol, and the information is time consuming to
   4339 	     figure out.  If the weakdef field is not already NULL,
   4340 	     then this symbol was already defined by some previous
   4341 	     dynamic object, and we will be using that previous
   4342 	     definition anyhow.  */
   4343 
   4344 	  h->u.weakdef = weaks;
   4345 	  weaks = h;
   4346 	  new_weakdef = TRUE;
   4347 	}
   4348 
   4349       /* Set the alignment of a common symbol.  */
   4350       if ((common || bfd_is_com_section (sec))
   4351 	  && h->root.type == bfd_link_hash_common)
   4352 	{
   4353 	  unsigned int align;
   4354 
   4355 	  if (common)
   4356 	    align = bfd_log2 (isym->st_value);
   4357 	  else
   4358 	    {
   4359 	      /* The new symbol is a common symbol in a shared object.
   4360 		 We need to get the alignment from the section.  */
   4361 	      align = new_sec->alignment_power;
   4362 	    }
   4363 	  if (align > old_alignment)
   4364 	    h->root.u.c.p->alignment_power = align;
   4365 	  else
   4366 	    h->root.u.c.p->alignment_power = old_alignment;
   4367 	}
   4368 
   4369       if (is_elf_hash_table (htab))
   4370 	{
   4371 	  /* Set a flag in the hash table entry indicating the type of
   4372 	     reference or definition we just found.  A dynamic symbol
   4373 	     is one which is referenced or defined by both a regular
   4374 	     object and a shared object.  */
   4375 	  bfd_boolean dynsym = FALSE;
   4376 
   4377 	  /* Plugin symbols aren't normal.  Don't set def_regular or
   4378 	     ref_regular for them, or make them dynamic.  */
   4379 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4380 	    ;
   4381 	  else if (! dynamic)
   4382 	    {
   4383 	      if (! definition)
   4384 		{
   4385 		  h->ref_regular = 1;
   4386 		  if (bind != STB_WEAK)
   4387 		    h->ref_regular_nonweak = 1;
   4388 		}
   4389 	      else
   4390 		{
   4391 		  h->def_regular = 1;
   4392 		  if (h->def_dynamic)
   4393 		    {
   4394 		      h->def_dynamic = 0;
   4395 		      h->ref_dynamic = 1;
   4396 		    }
   4397 		}
   4398 
   4399 	      /* If the indirect symbol has been forced local, don't
   4400 		 make the real symbol dynamic.  */
   4401 	      if ((h == hi || !hi->forced_local)
   4402 		  && (bfd_link_dll (info)
   4403 		      || h->def_dynamic
   4404 		      || h->ref_dynamic))
   4405 		dynsym = TRUE;
   4406 	    }
   4407 	  else
   4408 	    {
   4409 	      if (! definition)
   4410 		{
   4411 		  h->ref_dynamic = 1;
   4412 		  hi->ref_dynamic = 1;
   4413 		}
   4414 	      else
   4415 		{
   4416 		  h->def_dynamic = 1;
   4417 		  hi->def_dynamic = 1;
   4418 		}
   4419 
   4420 	      /* If the indirect symbol has been forced local, don't
   4421 		 make the real symbol dynamic.  */
   4422 	      if ((h == hi || !hi->forced_local)
   4423 		  && (h->def_regular
   4424 		      || h->ref_regular
   4425 		      || (h->u.weakdef != NULL
   4426 			  && ! new_weakdef
   4427 			  && h->u.weakdef->dynindx != -1)))
   4428 		dynsym = TRUE;
   4429 	    }
   4430 
   4431 	  /* Check to see if we need to add an indirect symbol for
   4432 	     the default name.  */
   4433 	  if (definition
   4434 	      || (!override && h->root.type == bfd_link_hash_common))
   4435 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
   4436 					      sec, value, &old_bfd, &dynsym))
   4437 	      goto error_free_vers;
   4438 
   4439 	  /* Check the alignment when a common symbol is involved. This
   4440 	     can change when a common symbol is overridden by a normal
   4441 	     definition or a common symbol is ignored due to the old
   4442 	     normal definition. We need to make sure the maximum
   4443 	     alignment is maintained.  */
   4444 	  if ((old_alignment || common)
   4445 	      && h->root.type != bfd_link_hash_common)
   4446 	    {
   4447 	      unsigned int common_align;
   4448 	      unsigned int normal_align;
   4449 	      unsigned int symbol_align;
   4450 	      bfd *normal_bfd;
   4451 	      bfd *common_bfd;
   4452 
   4453 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
   4454 			  || h->root.type == bfd_link_hash_defweak);
   4455 
   4456 	      symbol_align = ffs (h->root.u.def.value) - 1;
   4457 	      if (h->root.u.def.section->owner != NULL
   4458 		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
   4459 		{
   4460 		  normal_align = h->root.u.def.section->alignment_power;
   4461 		  if (normal_align > symbol_align)
   4462 		    normal_align = symbol_align;
   4463 		}
   4464 	      else
   4465 		normal_align = symbol_align;
   4466 
   4467 	      if (old_alignment)
   4468 		{
   4469 		  common_align = old_alignment;
   4470 		  common_bfd = old_bfd;
   4471 		  normal_bfd = abfd;
   4472 		}
   4473 	      else
   4474 		{
   4475 		  common_align = bfd_log2 (isym->st_value);
   4476 		  common_bfd = abfd;
   4477 		  normal_bfd = old_bfd;
   4478 		}
   4479 
   4480 	      if (normal_align < common_align)
   4481 		{
   4482 		  /* PR binutils/2735 */
   4483 		  if (normal_bfd == NULL)
   4484 		    (*_bfd_error_handler)
   4485 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
   4486 			 " greater than the alignment (%u) of its section %A"),
   4487 		       common_bfd, h->root.u.def.section,
   4488 		       1 << common_align, name, 1 << normal_align);
   4489 		  else
   4490 		    (*_bfd_error_handler)
   4491 		      (_("Warning: alignment %u of symbol `%s' in %B"
   4492 			 " is smaller than %u in %B"),
   4493 		       normal_bfd, common_bfd,
   4494 		       1 << normal_align, name, 1 << common_align);
   4495 		}
   4496 	    }
   4497 
   4498 	  /* Remember the symbol size if it isn't undefined.  */
   4499 	  if (isym->st_size != 0
   4500 	      && isym->st_shndx != SHN_UNDEF
   4501 	      && (definition || h->size == 0))
   4502 	    {
   4503 	      if (h->size != 0
   4504 		  && h->size != isym->st_size
   4505 		  && ! size_change_ok)
   4506 		(*_bfd_error_handler)
   4507 		  (_("Warning: size of symbol `%s' changed"
   4508 		     " from %lu in %B to %lu in %B"),
   4509 		   old_bfd, abfd,
   4510 		   name, (unsigned long) h->size,
   4511 		   (unsigned long) isym->st_size);
   4512 
   4513 	      h->size = isym->st_size;
   4514 	    }
   4515 
   4516 	  /* If this is a common symbol, then we always want H->SIZE
   4517 	     to be the size of the common symbol.  The code just above
   4518 	     won't fix the size if a common symbol becomes larger.  We
   4519 	     don't warn about a size change here, because that is
   4520 	     covered by --warn-common.  Allow changes between different
   4521 	     function types.  */
   4522 	  if (h->root.type == bfd_link_hash_common)
   4523 	    h->size = h->root.u.c.size;
   4524 
   4525 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
   4526 	      && ((definition && !new_weak)
   4527 		  || (old_weak && h->root.type == bfd_link_hash_common)
   4528 		  || h->type == STT_NOTYPE))
   4529 	    {
   4530 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
   4531 
   4532 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
   4533 		 symbol.  */
   4534 	      if (type == STT_GNU_IFUNC
   4535 		  && (abfd->flags & DYNAMIC) != 0)
   4536 		type = STT_FUNC;
   4537 
   4538 	      if (h->type != type)
   4539 		{
   4540 		  if (h->type != STT_NOTYPE && ! type_change_ok)
   4541 		    (*_bfd_error_handler)
   4542 		      (_("Warning: type of symbol `%s' changed"
   4543 			 " from %d to %d in %B"),
   4544 		       abfd, name, h->type, type);
   4545 
   4546 		  h->type = type;
   4547 		}
   4548 	    }
   4549 
   4550 	  /* Merge st_other field.  */
   4551 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
   4552 
   4553 	  /* We don't want to make debug symbol dynamic.  */
   4554 	  if (definition
   4555 	      && (sec->flags & SEC_DEBUGGING)
   4556 	      && !bfd_link_relocatable (info))
   4557 	    dynsym = FALSE;
   4558 
   4559 	  /* Nor should we make plugin symbols dynamic.  */
   4560 	  if ((abfd->flags & BFD_PLUGIN) != 0)
   4561 	    dynsym = FALSE;
   4562 
   4563 	  if (definition)
   4564 	    {
   4565 	      h->target_internal = isym->st_target_internal;
   4566 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
   4567 	    }
   4568 
   4569 	  if (definition && !dynamic)
   4570 	    {
   4571 	      char *p = strchr (name, ELF_VER_CHR);
   4572 	      if (p != NULL && p[1] != ELF_VER_CHR)
   4573 		{
   4574 		  /* Queue non-default versions so that .symver x, x@FOO
   4575 		     aliases can be checked.  */
   4576 		  if (!nondeflt_vers)
   4577 		    {
   4578 		      amt = ((isymend - isym + 1)
   4579 			     * sizeof (struct elf_link_hash_entry *));
   4580 		      nondeflt_vers
   4581 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
   4582 		      if (!nondeflt_vers)
   4583 			goto error_free_vers;
   4584 		    }
   4585 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
   4586 		}
   4587 	    }
   4588 
   4589 	  if (dynsym && h->dynindx == -1)
   4590 	    {
   4591 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4592 		goto error_free_vers;
   4593 	      if (h->u.weakdef != NULL
   4594 		  && ! new_weakdef
   4595 		  && h->u.weakdef->dynindx == -1)
   4596 		{
   4597 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
   4598 		    goto error_free_vers;
   4599 		}
   4600 	    }
   4601 	  else if (dynsym && h->dynindx != -1)
   4602 	    /* If the symbol already has a dynamic index, but
   4603 	       visibility says it should not be visible, turn it into
   4604 	       a local symbol.  */
   4605 	    switch (ELF_ST_VISIBILITY (h->other))
   4606 	      {
   4607 	      case STV_INTERNAL:
   4608 	      case STV_HIDDEN:
   4609 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
   4610 		dynsym = FALSE;
   4611 		break;
   4612 	      }
   4613 
   4614 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
   4615 	     for unmatched symbol.  */
   4616 	  if (!add_needed
   4617 	      && matched
   4618 	      && definition
   4619 	      && ((dynsym
   4620 		   && h->ref_regular_nonweak
   4621 		   && (old_bfd == NULL
   4622 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
   4623 		  || (h->ref_dynamic_nonweak
   4624 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
   4625 		      && !on_needed_list (elf_dt_name (abfd), htab->needed))))
   4626 	    {
   4627 	      int ret;
   4628 	      const char *soname = elf_dt_name (abfd);
   4629 
   4630 	      info->callbacks->minfo ("%!", soname, old_bfd,
   4631 				      h->root.root.string);
   4632 
   4633 	      /* A symbol from a library loaded via DT_NEEDED of some
   4634 		 other library is referenced by a regular object.
   4635 		 Add a DT_NEEDED entry for it.  Issue an error if
   4636 		 --no-add-needed is used and the reference was not
   4637 		 a weak one.  */
   4638 	      if (old_bfd != NULL
   4639 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
   4640 		{
   4641 		  (*_bfd_error_handler)
   4642 		    (_("%B: undefined reference to symbol '%s'"),
   4643 		     old_bfd, name);
   4644 		  bfd_set_error (bfd_error_missing_dso);
   4645 		  goto error_free_vers;
   4646 		}
   4647 
   4648 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
   4649 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
   4650 
   4651 	      add_needed = TRUE;
   4652 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
   4653 	      if (ret < 0)
   4654 		goto error_free_vers;
   4655 
   4656 	      BFD_ASSERT (ret == 0);
   4657 	    }
   4658 	}
   4659     }
   4660 
   4661   if (extversym != NULL)
   4662     {
   4663       free (extversym);
   4664       extversym = NULL;
   4665     }
   4666 
   4667   if (isymbuf != NULL)
   4668     {
   4669       free (isymbuf);
   4670       isymbuf = NULL;
   4671     }
   4672 
   4673   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
   4674     {
   4675       unsigned int i;
   4676 
   4677       /* Restore the symbol table.  */
   4678       old_ent = (char *) old_tab + tabsize;
   4679       memset (elf_sym_hashes (abfd), 0,
   4680 	      extsymcount * sizeof (struct elf_link_hash_entry *));
   4681       htab->root.table.table = old_table;
   4682       htab->root.table.size = old_size;
   4683       htab->root.table.count = old_count;
   4684       memcpy (htab->root.table.table, old_tab, tabsize);
   4685       htab->root.undefs = old_undefs;
   4686       htab->root.undefs_tail = old_undefs_tail;
   4687       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
   4688       free (old_strtab);
   4689       old_strtab = NULL;
   4690       for (i = 0; i < htab->root.table.size; i++)
   4691 	{
   4692 	  struct bfd_hash_entry *p;
   4693 	  struct elf_link_hash_entry *h;
   4694 	  bfd_size_type size;
   4695 	  unsigned int alignment_power;
   4696 
   4697 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
   4698 	    {
   4699 	      h = (struct elf_link_hash_entry *) p;
   4700 	      if (h->root.type == bfd_link_hash_warning)
   4701 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4702 
   4703 	      /* Preserve the maximum alignment and size for common
   4704 		 symbols even if this dynamic lib isn't on DT_NEEDED
   4705 		 since it can still be loaded at run time by another
   4706 		 dynamic lib.  */
   4707 	      if (h->root.type == bfd_link_hash_common)
   4708 		{
   4709 		  size = h->root.u.c.size;
   4710 		  alignment_power = h->root.u.c.p->alignment_power;
   4711 		}
   4712 	      else
   4713 		{
   4714 		  size = 0;
   4715 		  alignment_power = 0;
   4716 		}
   4717 	      memcpy (p, old_ent, htab->root.table.entsize);
   4718 	      old_ent = (char *) old_ent + htab->root.table.entsize;
   4719 	      h = (struct elf_link_hash_entry *) p;
   4720 	      if (h->root.type == bfd_link_hash_warning)
   4721 		{
   4722 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
   4723 		  old_ent = (char *) old_ent + htab->root.table.entsize;
   4724 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4725 		}
   4726 	      if (h->root.type == bfd_link_hash_common)
   4727 		{
   4728 		  if (size > h->root.u.c.size)
   4729 		    h->root.u.c.size = size;
   4730 		  if (alignment_power > h->root.u.c.p->alignment_power)
   4731 		    h->root.u.c.p->alignment_power = alignment_power;
   4732 		}
   4733 	    }
   4734 	}
   4735 
   4736       /* Make a special call to the linker "notice" function to
   4737 	 tell it that symbols added for crefs may need to be removed.  */
   4738       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
   4739 	goto error_free_vers;
   4740 
   4741       free (old_tab);
   4742       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
   4743 			   alloc_mark);
   4744       if (nondeflt_vers != NULL)
   4745 	free (nondeflt_vers);
   4746       return TRUE;
   4747     }
   4748 
   4749   if (old_tab != NULL)
   4750     {
   4751       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
   4752 	goto error_free_vers;
   4753       free (old_tab);
   4754       old_tab = NULL;
   4755     }
   4756 
   4757   /* Now that all the symbols from this input file are created, if
   4758      not performing a relocatable link, handle .symver foo, foo@BAR
   4759      such that any relocs against foo become foo@BAR.  */
   4760   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
   4761     {
   4762       bfd_size_type cnt, symidx;
   4763 
   4764       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
   4765 	{
   4766 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
   4767 	  char *shortname, *p;
   4768 
   4769 	  p = strchr (h->root.root.string, ELF_VER_CHR);
   4770 	  if (p == NULL
   4771 	      || (h->root.type != bfd_link_hash_defined
   4772 		  && h->root.type != bfd_link_hash_defweak))
   4773 	    continue;
   4774 
   4775 	  amt = p - h->root.root.string;
   4776 	  shortname = (char *) bfd_malloc (amt + 1);
   4777 	  if (!shortname)
   4778 	    goto error_free_vers;
   4779 	  memcpy (shortname, h->root.root.string, amt);
   4780 	  shortname[amt] = '\0';
   4781 
   4782 	  hi = (struct elf_link_hash_entry *)
   4783 	       bfd_link_hash_lookup (&htab->root, shortname,
   4784 				     FALSE, FALSE, FALSE);
   4785 	  if (hi != NULL
   4786 	      && hi->root.type == h->root.type
   4787 	      && hi->root.u.def.value == h->root.u.def.value
   4788 	      && hi->root.u.def.section == h->root.u.def.section)
   4789 	    {
   4790 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
   4791 	      hi->root.type = bfd_link_hash_indirect;
   4792 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
   4793 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
   4794 	      sym_hash = elf_sym_hashes (abfd);
   4795 	      if (sym_hash)
   4796 		for (symidx = 0; symidx < extsymcount; ++symidx)
   4797 		  if (sym_hash[symidx] == hi)
   4798 		    {
   4799 		      sym_hash[symidx] = h;
   4800 		      break;
   4801 		    }
   4802 	    }
   4803 	  free (shortname);
   4804 	}
   4805       free (nondeflt_vers);
   4806       nondeflt_vers = NULL;
   4807     }
   4808 
   4809   /* Now set the weakdefs field correctly for all the weak defined
   4810      symbols we found.  The only way to do this is to search all the
   4811      symbols.  Since we only need the information for non functions in
   4812      dynamic objects, that's the only time we actually put anything on
   4813      the list WEAKS.  We need this information so that if a regular
   4814      object refers to a symbol defined weakly in a dynamic object, the
   4815      real symbol in the dynamic object is also put in the dynamic
   4816      symbols; we also must arrange for both symbols to point to the
   4817      same memory location.  We could handle the general case of symbol
   4818      aliasing, but a general symbol alias can only be generated in
   4819      assembler code, handling it correctly would be very time
   4820      consuming, and other ELF linkers don't handle general aliasing
   4821      either.  */
   4822   if (weaks != NULL)
   4823     {
   4824       struct elf_link_hash_entry **hpp;
   4825       struct elf_link_hash_entry **hppend;
   4826       struct elf_link_hash_entry **sorted_sym_hash;
   4827       struct elf_link_hash_entry *h;
   4828       size_t sym_count;
   4829 
   4830       /* Since we have to search the whole symbol list for each weak
   4831 	 defined symbol, search time for N weak defined symbols will be
   4832 	 O(N^2). Binary search will cut it down to O(NlogN).  */
   4833       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
   4834       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
   4835       if (sorted_sym_hash == NULL)
   4836 	goto error_return;
   4837       sym_hash = sorted_sym_hash;
   4838       hpp = elf_sym_hashes (abfd);
   4839       hppend = hpp + extsymcount;
   4840       sym_count = 0;
   4841       for (; hpp < hppend; hpp++)
   4842 	{
   4843 	  h = *hpp;
   4844 	  if (h != NULL
   4845 	      && h->root.type == bfd_link_hash_defined
   4846 	      && !bed->is_function_type (h->type))
   4847 	    {
   4848 	      *sym_hash = h;
   4849 	      sym_hash++;
   4850 	      sym_count++;
   4851 	    }
   4852 	}
   4853 
   4854       qsort (sorted_sym_hash, sym_count,
   4855 	     sizeof (struct elf_link_hash_entry *),
   4856 	     elf_sort_symbol);
   4857 
   4858       while (weaks != NULL)
   4859 	{
   4860 	  struct elf_link_hash_entry *hlook;
   4861 	  asection *slook;
   4862 	  bfd_vma vlook;
   4863 	  size_t i, j, idx = 0;
   4864 
   4865 	  hlook = weaks;
   4866 	  weaks = hlook->u.weakdef;
   4867 	  hlook->u.weakdef = NULL;
   4868 
   4869 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
   4870 		      || hlook->root.type == bfd_link_hash_defweak
   4871 		      || hlook->root.type == bfd_link_hash_common
   4872 		      || hlook->root.type == bfd_link_hash_indirect);
   4873 	  slook = hlook->root.u.def.section;
   4874 	  vlook = hlook->root.u.def.value;
   4875 
   4876 	  i = 0;
   4877 	  j = sym_count;
   4878 	  while (i != j)
   4879 	    {
   4880 	      bfd_signed_vma vdiff;
   4881 	      idx = (i + j) / 2;
   4882 	      h = sorted_sym_hash[idx];
   4883 	      vdiff = vlook - h->root.u.def.value;
   4884 	      if (vdiff < 0)
   4885 		j = idx;
   4886 	      else if (vdiff > 0)
   4887 		i = idx + 1;
   4888 	      else
   4889 		{
   4890 		  int sdiff = slook->id - h->root.u.def.section->id;
   4891 		  if (sdiff < 0)
   4892 		    j = idx;
   4893 		  else if (sdiff > 0)
   4894 		    i = idx + 1;
   4895 		  else
   4896 		    break;
   4897 		}
   4898 	    }
   4899 
   4900 	  /* We didn't find a value/section match.  */
   4901 	  if (i == j)
   4902 	    continue;
   4903 
   4904 	  /* With multiple aliases, or when the weak symbol is already
   4905 	     strongly defined, we have multiple matching symbols and
   4906 	     the binary search above may land on any of them.  Step
   4907 	     one past the matching symbol(s).  */
   4908 	  while (++idx != j)
   4909 	    {
   4910 	      h = sorted_sym_hash[idx];
   4911 	      if (h->root.u.def.section != slook
   4912 		  || h->root.u.def.value != vlook)
   4913 		break;
   4914 	    }
   4915 
   4916 	  /* Now look back over the aliases.  Since we sorted by size
   4917 	     as well as value and section, we'll choose the one with
   4918 	     the largest size.  */
   4919 	  while (idx-- != i)
   4920 	    {
   4921 	      h = sorted_sym_hash[idx];
   4922 
   4923 	      /* Stop if value or section doesn't match.  */
   4924 	      if (h->root.u.def.section != slook
   4925 		  || h->root.u.def.value != vlook)
   4926 		break;
   4927 	      else if (h != hlook)
   4928 		{
   4929 		  hlook->u.weakdef = h;
   4930 
   4931 		  /* If the weak definition is in the list of dynamic
   4932 		     symbols, make sure the real definition is put
   4933 		     there as well.  */
   4934 		  if (hlook->dynindx != -1 && h->dynindx == -1)
   4935 		    {
   4936 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4937 			{
   4938 			err_free_sym_hash:
   4939 			  free (sorted_sym_hash);
   4940 			  goto error_return;
   4941 			}
   4942 		    }
   4943 
   4944 		  /* If the real definition is in the list of dynamic
   4945 		     symbols, make sure the weak definition is put
   4946 		     there as well.  If we don't do this, then the
   4947 		     dynamic loader might not merge the entries for the
   4948 		     real definition and the weak definition.  */
   4949 		  if (h->dynindx != -1 && hlook->dynindx == -1)
   4950 		    {
   4951 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
   4952 			goto err_free_sym_hash;
   4953 		    }
   4954 		  break;
   4955 		}
   4956 	    }
   4957 	}
   4958 
   4959       free (sorted_sym_hash);
   4960     }
   4961 
   4962   if (bed->check_directives
   4963       && !(*bed->check_directives) (abfd, info))
   4964     return FALSE;
   4965 
   4966   /* If this object is the same format as the output object, and it is
   4967      not a shared library, then let the backend look through the
   4968      relocs.
   4969 
   4970      This is required to build global offset table entries and to
   4971      arrange for dynamic relocs.  It is not required for the
   4972      particular common case of linking non PIC code, even when linking
   4973      against shared libraries, but unfortunately there is no way of
   4974      knowing whether an object file has been compiled PIC or not.
   4975      Looking through the relocs is not particularly time consuming.
   4976      The problem is that we must either (1) keep the relocs in memory,
   4977      which causes the linker to require additional runtime memory or
   4978      (2) read the relocs twice from the input file, which wastes time.
   4979      This would be a good case for using mmap.
   4980 
   4981      I have no idea how to handle linking PIC code into a file of a
   4982      different format.  It probably can't be done.  */
   4983   if (! dynamic
   4984       && is_elf_hash_table (htab)
   4985       && bed->check_relocs != NULL
   4986       && elf_object_id (abfd) == elf_hash_table_id (htab)
   4987       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
   4988     {
   4989       asection *o;
   4990 
   4991       for (o = abfd->sections; o != NULL; o = o->next)
   4992 	{
   4993 	  Elf_Internal_Rela *internal_relocs;
   4994 	  bfd_boolean ok;
   4995 
   4996 	  if ((o->flags & SEC_RELOC) == 0
   4997 	      || o->reloc_count == 0
   4998 	      || ((info->strip == strip_all || info->strip == strip_debugger)
   4999 		  && (o->flags & SEC_DEBUGGING) != 0)
   5000 	      || bfd_is_abs_section (o->output_section))
   5001 	    continue;
   5002 
   5003 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   5004 						       info->keep_memory);
   5005 	  if (internal_relocs == NULL)
   5006 	    goto error_return;
   5007 
   5008 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
   5009 
   5010 	  if (elf_section_data (o)->relocs != internal_relocs)
   5011 	    free (internal_relocs);
   5012 
   5013 	  if (! ok)
   5014 	    goto error_return;
   5015 	}
   5016     }
   5017 
   5018   /* If this is a non-traditional link, try to optimize the handling
   5019      of the .stab/.stabstr sections.  */
   5020   if (! dynamic
   5021       && ! info->traditional_format
   5022       && is_elf_hash_table (htab)
   5023       && (info->strip != strip_all && info->strip != strip_debugger))
   5024     {
   5025       asection *stabstr;
   5026 
   5027       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
   5028       if (stabstr != NULL)
   5029 	{
   5030 	  bfd_size_type string_offset = 0;
   5031 	  asection *stab;
   5032 
   5033 	  for (stab = abfd->sections; stab; stab = stab->next)
   5034 	    if (CONST_STRNEQ (stab->name, ".stab")
   5035 		&& (!stab->name[5] ||
   5036 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
   5037 		&& (stab->flags & SEC_MERGE) == 0
   5038 		&& !bfd_is_abs_section (stab->output_section))
   5039 	      {
   5040 		struct bfd_elf_section_data *secdata;
   5041 
   5042 		secdata = elf_section_data (stab);
   5043 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
   5044 					       stabstr, &secdata->sec_info,
   5045 					       &string_offset))
   5046 		  goto error_return;
   5047 		if (secdata->sec_info)
   5048 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
   5049 	    }
   5050 	}
   5051     }
   5052 
   5053   if (is_elf_hash_table (htab) && add_needed)
   5054     {
   5055       /* Add this bfd to the loaded list.  */
   5056       struct elf_link_loaded_list *n;
   5057 
   5058       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
   5059       if (n == NULL)
   5060 	goto error_return;
   5061       n->abfd = abfd;
   5062       n->next = htab->loaded;
   5063       htab->loaded = n;
   5064     }
   5065 
   5066   return TRUE;
   5067 
   5068  error_free_vers:
   5069   if (old_tab != NULL)
   5070     free (old_tab);
   5071   if (old_strtab != NULL)
   5072     free (old_strtab);
   5073   if (nondeflt_vers != NULL)
   5074     free (nondeflt_vers);
   5075   if (extversym != NULL)
   5076     free (extversym);
   5077  error_free_sym:
   5078   if (isymbuf != NULL)
   5079     free (isymbuf);
   5080  error_return:
   5081   return FALSE;
   5082 }
   5083 
   5084 /* Return the linker hash table entry of a symbol that might be
   5085    satisfied by an archive symbol.  Return -1 on error.  */
   5086 
   5087 struct elf_link_hash_entry *
   5088 _bfd_elf_archive_symbol_lookup (bfd *abfd,
   5089 				struct bfd_link_info *info,
   5090 				const char *name)
   5091 {
   5092   struct elf_link_hash_entry *h;
   5093   char *p, *copy;
   5094   size_t len, first;
   5095 
   5096   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
   5097   if (h != NULL)
   5098     return h;
   5099 
   5100   /* If this is a default version (the name contains @@), look up the
   5101      symbol again with only one `@' as well as without the version.
   5102      The effect is that references to the symbol with and without the
   5103      version will be matched by the default symbol in the archive.  */
   5104 
   5105   p = strchr (name, ELF_VER_CHR);
   5106   if (p == NULL || p[1] != ELF_VER_CHR)
   5107     return h;
   5108 
   5109   /* First check with only one `@'.  */
   5110   len = strlen (name);
   5111   copy = (char *) bfd_alloc (abfd, len);
   5112   if (copy == NULL)
   5113     return (struct elf_link_hash_entry *) 0 - 1;
   5114 
   5115   first = p - name + 1;
   5116   memcpy (copy, name, first);
   5117   memcpy (copy + first, name + first + 1, len - first);
   5118 
   5119   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
   5120   if (h == NULL)
   5121     {
   5122       /* We also need to check references to the symbol without the
   5123 	 version.  */
   5124       copy[first - 1] = '\0';
   5125       h = elf_link_hash_lookup (elf_hash_table (info), copy,
   5126 				FALSE, FALSE, TRUE);
   5127     }
   5128 
   5129   bfd_release (abfd, copy);
   5130   return h;
   5131 }
   5132 
   5133 /* Add symbols from an ELF archive file to the linker hash table.  We
   5134    don't use _bfd_generic_link_add_archive_symbols because we need to
   5135    handle versioned symbols.
   5136 
   5137    Fortunately, ELF archive handling is simpler than that done by
   5138    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
   5139    oddities.  In ELF, if we find a symbol in the archive map, and the
   5140    symbol is currently undefined, we know that we must pull in that
   5141    object file.
   5142 
   5143    Unfortunately, we do have to make multiple passes over the symbol
   5144    table until nothing further is resolved.  */
   5145 
   5146 static bfd_boolean
   5147 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   5148 {
   5149   symindex c;
   5150   unsigned char *included = NULL;
   5151   carsym *symdefs;
   5152   bfd_boolean loop;
   5153   bfd_size_type amt;
   5154   const struct elf_backend_data *bed;
   5155   struct elf_link_hash_entry * (*archive_symbol_lookup)
   5156     (bfd *, struct bfd_link_info *, const char *);
   5157 
   5158   if (! bfd_has_map (abfd))
   5159     {
   5160       /* An empty archive is a special case.  */
   5161       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   5162 	return TRUE;
   5163       bfd_set_error (bfd_error_no_armap);
   5164       return FALSE;
   5165     }
   5166 
   5167   /* Keep track of all symbols we know to be already defined, and all
   5168      files we know to be already included.  This is to speed up the
   5169      second and subsequent passes.  */
   5170   c = bfd_ardata (abfd)->symdef_count;
   5171   if (c == 0)
   5172     return TRUE;
   5173   amt = c;
   5174   amt *= sizeof (*included);
   5175   included = (unsigned char *) bfd_zmalloc (amt);
   5176   if (included == NULL)
   5177     return FALSE;
   5178 
   5179   symdefs = bfd_ardata (abfd)->symdefs;
   5180   bed = get_elf_backend_data (abfd);
   5181   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
   5182 
   5183   do
   5184     {
   5185       file_ptr last;
   5186       symindex i;
   5187       carsym *symdef;
   5188       carsym *symdefend;
   5189 
   5190       loop = FALSE;
   5191       last = -1;
   5192 
   5193       symdef = symdefs;
   5194       symdefend = symdef + c;
   5195       for (i = 0; symdef < symdefend; symdef++, i++)
   5196 	{
   5197 	  struct elf_link_hash_entry *h;
   5198 	  bfd *element;
   5199 	  struct bfd_link_hash_entry *undefs_tail;
   5200 	  symindex mark;
   5201 
   5202 	  if (included[i])
   5203 	    continue;
   5204 	  if (symdef->file_offset == last)
   5205 	    {
   5206 	      included[i] = TRUE;
   5207 	      continue;
   5208 	    }
   5209 
   5210 	  h = archive_symbol_lookup (abfd, info, symdef->name);
   5211 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
   5212 	    goto error_return;
   5213 
   5214 	  if (h == NULL)
   5215 	    continue;
   5216 
   5217 	  if (h->root.type == bfd_link_hash_common)
   5218 	    {
   5219 	      /* We currently have a common symbol.  The archive map contains
   5220 		 a reference to this symbol, so we may want to include it.  We
   5221 		 only want to include it however, if this archive element
   5222 		 contains a definition of the symbol, not just another common
   5223 		 declaration of it.
   5224 
   5225 		 Unfortunately some archivers (including GNU ar) will put
   5226 		 declarations of common symbols into their archive maps, as
   5227 		 well as real definitions, so we cannot just go by the archive
   5228 		 map alone.  Instead we must read in the element's symbol
   5229 		 table and check that to see what kind of symbol definition
   5230 		 this is.  */
   5231 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
   5232 		continue;
   5233 	    }
   5234 	  else if (h->root.type != bfd_link_hash_undefined)
   5235 	    {
   5236 	      if (h->root.type != bfd_link_hash_undefweak)
   5237 		/* Symbol must be defined.  Don't check it again.  */
   5238 		included[i] = TRUE;
   5239 	      continue;
   5240 	    }
   5241 
   5242 	  /* We need to include this archive member.  */
   5243 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   5244 	  if (element == NULL)
   5245 	    goto error_return;
   5246 
   5247 	  if (! bfd_check_format (element, bfd_object))
   5248 	    goto error_return;
   5249 
   5250 	  undefs_tail = info->hash->undefs_tail;
   5251 
   5252 	  if (!(*info->callbacks
   5253 		->add_archive_element) (info, element, symdef->name, &element))
   5254 	    goto error_return;
   5255 	  if (!bfd_link_add_symbols (element, info))
   5256 	    goto error_return;
   5257 
   5258 	  /* If there are any new undefined symbols, we need to make
   5259 	     another pass through the archive in order to see whether
   5260 	     they can be defined.  FIXME: This isn't perfect, because
   5261 	     common symbols wind up on undefs_tail and because an
   5262 	     undefined symbol which is defined later on in this pass
   5263 	     does not require another pass.  This isn't a bug, but it
   5264 	     does make the code less efficient than it could be.  */
   5265 	  if (undefs_tail != info->hash->undefs_tail)
   5266 	    loop = TRUE;
   5267 
   5268 	  /* Look backward to mark all symbols from this object file
   5269 	     which we have already seen in this pass.  */
   5270 	  mark = i;
   5271 	  do
   5272 	    {
   5273 	      included[mark] = TRUE;
   5274 	      if (mark == 0)
   5275 		break;
   5276 	      --mark;
   5277 	    }
   5278 	  while (symdefs[mark].file_offset == symdef->file_offset);
   5279 
   5280 	  /* We mark subsequent symbols from this object file as we go
   5281 	     on through the loop.  */
   5282 	  last = symdef->file_offset;
   5283 	}
   5284     }
   5285   while (loop);
   5286 
   5287   free (included);
   5288 
   5289   return TRUE;
   5290 
   5291  error_return:
   5292   if (included != NULL)
   5293     free (included);
   5294   return FALSE;
   5295 }
   5296 
   5297 /* Given an ELF BFD, add symbols to the global hash table as
   5298    appropriate.  */
   5299 
   5300 bfd_boolean
   5301 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   5302 {
   5303   switch (bfd_get_format (abfd))
   5304     {
   5305     case bfd_object:
   5306       return elf_link_add_object_symbols (abfd, info);
   5307     case bfd_archive:
   5308       return elf_link_add_archive_symbols (abfd, info);
   5309     default:
   5310       bfd_set_error (bfd_error_wrong_format);
   5311       return FALSE;
   5312     }
   5313 }
   5314 
   5315 struct hash_codes_info
   5317 {
   5318   unsigned long *hashcodes;
   5319   bfd_boolean error;
   5320 };
   5321 
   5322 /* This function will be called though elf_link_hash_traverse to store
   5323    all hash value of the exported symbols in an array.  */
   5324 
   5325 static bfd_boolean
   5326 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   5327 {
   5328   struct hash_codes_info *inf = (struct hash_codes_info *) data;
   5329   const char *name;
   5330   unsigned long ha;
   5331   char *alc = NULL;
   5332 
   5333   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5334   if (h->dynindx == -1)
   5335     return TRUE;
   5336 
   5337   name = h->root.root.string;
   5338   if (h->versioned >= versioned)
   5339     {
   5340       char *p = strchr (name, ELF_VER_CHR);
   5341       if (p != NULL)
   5342 	{
   5343 	  alc = (char *) bfd_malloc (p - name + 1);
   5344 	  if (alc == NULL)
   5345 	    {
   5346 	      inf->error = TRUE;
   5347 	      return FALSE;
   5348 	    }
   5349 	  memcpy (alc, name, p - name);
   5350 	  alc[p - name] = '\0';
   5351 	  name = alc;
   5352 	}
   5353     }
   5354 
   5355   /* Compute the hash value.  */
   5356   ha = bfd_elf_hash (name);
   5357 
   5358   /* Store the found hash value in the array given as the argument.  */
   5359   *(inf->hashcodes)++ = ha;
   5360 
   5361   /* And store it in the struct so that we can put it in the hash table
   5362      later.  */
   5363   h->u.elf_hash_value = ha;
   5364 
   5365   if (alc != NULL)
   5366     free (alc);
   5367 
   5368   return TRUE;
   5369 }
   5370 
   5371 struct collect_gnu_hash_codes
   5372 {
   5373   bfd *output_bfd;
   5374   const struct elf_backend_data *bed;
   5375   unsigned long int nsyms;
   5376   unsigned long int maskbits;
   5377   unsigned long int *hashcodes;
   5378   unsigned long int *hashval;
   5379   unsigned long int *indx;
   5380   unsigned long int *counts;
   5381   bfd_vma *bitmask;
   5382   bfd_byte *contents;
   5383   long int min_dynindx;
   5384   unsigned long int bucketcount;
   5385   unsigned long int symindx;
   5386   long int local_indx;
   5387   long int shift1, shift2;
   5388   unsigned long int mask;
   5389   bfd_boolean error;
   5390 };
   5391 
   5392 /* This function will be called though elf_link_hash_traverse to store
   5393    all hash value of the exported symbols in an array.  */
   5394 
   5395 static bfd_boolean
   5396 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   5397 {
   5398   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5399   const char *name;
   5400   unsigned long ha;
   5401   char *alc = NULL;
   5402 
   5403   /* Ignore indirect symbols.  These are added by the versioning code.  */
   5404   if (h->dynindx == -1)
   5405     return TRUE;
   5406 
   5407   /* Ignore also local symbols and undefined symbols.  */
   5408   if (! (*s->bed->elf_hash_symbol) (h))
   5409     return TRUE;
   5410 
   5411   name = h->root.root.string;
   5412   if (h->versioned >= versioned)
   5413     {
   5414       char *p = strchr (name, ELF_VER_CHR);
   5415       if (p != NULL)
   5416 	{
   5417 	  alc = (char *) bfd_malloc (p - name + 1);
   5418 	  if (alc == NULL)
   5419 	    {
   5420 	      s->error = TRUE;
   5421 	      return FALSE;
   5422 	    }
   5423 	  memcpy (alc, name, p - name);
   5424 	  alc[p - name] = '\0';
   5425 	  name = alc;
   5426 	}
   5427     }
   5428 
   5429   /* Compute the hash value.  */
   5430   ha = bfd_elf_gnu_hash (name);
   5431 
   5432   /* Store the found hash value in the array for compute_bucket_count,
   5433      and also for .dynsym reordering purposes.  */
   5434   s->hashcodes[s->nsyms] = ha;
   5435   s->hashval[h->dynindx] = ha;
   5436   ++s->nsyms;
   5437   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
   5438     s->min_dynindx = h->dynindx;
   5439 
   5440   if (alc != NULL)
   5441     free (alc);
   5442 
   5443   return TRUE;
   5444 }
   5445 
   5446 /* This function will be called though elf_link_hash_traverse to do
   5447    final dynaminc symbol renumbering.  */
   5448 
   5449 static bfd_boolean
   5450 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
   5451 {
   5452   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
   5453   unsigned long int bucket;
   5454   unsigned long int val;
   5455 
   5456   /* Ignore indirect symbols.  */
   5457   if (h->dynindx == -1)
   5458     return TRUE;
   5459 
   5460   /* Ignore also local symbols and undefined symbols.  */
   5461   if (! (*s->bed->elf_hash_symbol) (h))
   5462     {
   5463       if (h->dynindx >= s->min_dynindx)
   5464 	h->dynindx = s->local_indx++;
   5465       return TRUE;
   5466     }
   5467 
   5468   bucket = s->hashval[h->dynindx] % s->bucketcount;
   5469   val = (s->hashval[h->dynindx] >> s->shift1)
   5470 	& ((s->maskbits >> s->shift1) - 1);
   5471   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
   5472   s->bitmask[val]
   5473     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
   5474   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
   5475   if (s->counts[bucket] == 1)
   5476     /* Last element terminates the chain.  */
   5477     val |= 1;
   5478   bfd_put_32 (s->output_bfd, val,
   5479 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
   5480   --s->counts[bucket];
   5481   h->dynindx = s->indx[bucket]++;
   5482   return TRUE;
   5483 }
   5484 
   5485 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   5486 
   5487 bfd_boolean
   5488 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
   5489 {
   5490   return !(h->forced_local
   5491 	   || h->root.type == bfd_link_hash_undefined
   5492 	   || h->root.type == bfd_link_hash_undefweak
   5493 	   || ((h->root.type == bfd_link_hash_defined
   5494 		|| h->root.type == bfd_link_hash_defweak)
   5495 	       && h->root.u.def.section->output_section == NULL));
   5496 }
   5497 
   5498 /* Array used to determine the number of hash table buckets to use
   5499    based on the number of symbols there are.  If there are fewer than
   5500    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
   5501    fewer than 37 we use 17 buckets, and so forth.  We never use more
   5502    than 32771 buckets.  */
   5503 
   5504 static const size_t elf_buckets[] =
   5505 {
   5506   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   5507   16411, 32771, 0
   5508 };
   5509 
   5510 /* Compute bucket count for hashing table.  We do not use a static set
   5511    of possible tables sizes anymore.  Instead we determine for all
   5512    possible reasonable sizes of the table the outcome (i.e., the
   5513    number of collisions etc) and choose the best solution.  The
   5514    weighting functions are not too simple to allow the table to grow
   5515    without bounds.  Instead one of the weighting factors is the size.
   5516    Therefore the result is always a good payoff between few collisions
   5517    (= short chain lengths) and table size.  */
   5518 static size_t
   5519 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5520 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
   5521 		      unsigned long int nsyms,
   5522 		      int gnu_hash)
   5523 {
   5524   size_t best_size = 0;
   5525   unsigned long int i;
   5526 
   5527   /* We have a problem here.  The following code to optimize the table
   5528      size requires an integer type with more the 32 bits.  If
   5529      BFD_HOST_U_64_BIT is set we know about such a type.  */
   5530 #ifdef BFD_HOST_U_64_BIT
   5531   if (info->optimize)
   5532     {
   5533       size_t minsize;
   5534       size_t maxsize;
   5535       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
   5536       bfd *dynobj = elf_hash_table (info)->dynobj;
   5537       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
   5538       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
   5539       unsigned long int *counts;
   5540       bfd_size_type amt;
   5541       unsigned int no_improvement_count = 0;
   5542 
   5543       /* Possible optimization parameters: if we have NSYMS symbols we say
   5544 	 that the hashing table must at least have NSYMS/4 and at most
   5545 	 2*NSYMS buckets.  */
   5546       minsize = nsyms / 4;
   5547       if (minsize == 0)
   5548 	minsize = 1;
   5549       best_size = maxsize = nsyms * 2;
   5550       if (gnu_hash)
   5551 	{
   5552 	  if (minsize < 2)
   5553 	    minsize = 2;
   5554 	  if ((best_size & 31) == 0)
   5555 	    ++best_size;
   5556 	}
   5557 
   5558       /* Create array where we count the collisions in.  We must use bfd_malloc
   5559 	 since the size could be large.  */
   5560       amt = maxsize;
   5561       amt *= sizeof (unsigned long int);
   5562       counts = (unsigned long int *) bfd_malloc (amt);
   5563       if (counts == NULL)
   5564 	return 0;
   5565 
   5566       /* Compute the "optimal" size for the hash table.  The criteria is a
   5567 	 minimal chain length.  The minor criteria is (of course) the size
   5568 	 of the table.  */
   5569       for (i = minsize; i < maxsize; ++i)
   5570 	{
   5571 	  /* Walk through the array of hashcodes and count the collisions.  */
   5572 	  BFD_HOST_U_64_BIT max;
   5573 	  unsigned long int j;
   5574 	  unsigned long int fact;
   5575 
   5576 	  if (gnu_hash && (i & 31) == 0)
   5577 	    continue;
   5578 
   5579 	  memset (counts, '\0', i * sizeof (unsigned long int));
   5580 
   5581 	  /* Determine how often each hash bucket is used.  */
   5582 	  for (j = 0; j < nsyms; ++j)
   5583 	    ++counts[hashcodes[j] % i];
   5584 
   5585 	  /* For the weight function we need some information about the
   5586 	     pagesize on the target.  This is information need not be 100%
   5587 	     accurate.  Since this information is not available (so far) we
   5588 	     define it here to a reasonable default value.  If it is crucial
   5589 	     to have a better value some day simply define this value.  */
   5590 # ifndef BFD_TARGET_PAGESIZE
   5591 #  define BFD_TARGET_PAGESIZE	(4096)
   5592 # endif
   5593 
   5594 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
   5595 	     and the chains.  */
   5596 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
   5597 
   5598 # if 1
   5599 	  /* Variant 1: optimize for short chains.  We add the squares
   5600 	     of all the chain lengths (which favors many small chain
   5601 	     over a few long chains).  */
   5602 	  for (j = 0; j < i; ++j)
   5603 	    max += counts[j] * counts[j];
   5604 
   5605 	  /* This adds penalties for the overall size of the table.  */
   5606 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5607 	  max *= fact * fact;
   5608 # else
   5609 	  /* Variant 2: Optimize a lot more for small table.  Here we
   5610 	     also add squares of the size but we also add penalties for
   5611 	     empty slots (the +1 term).  */
   5612 	  for (j = 0; j < i; ++j)
   5613 	    max += (1 + counts[j]) * (1 + counts[j]);
   5614 
   5615 	  /* The overall size of the table is considered, but not as
   5616 	     strong as in variant 1, where it is squared.  */
   5617 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
   5618 	  max *= fact;
   5619 # endif
   5620 
   5621 	  /* Compare with current best results.  */
   5622 	  if (max < best_chlen)
   5623 	    {
   5624 	      best_chlen = max;
   5625 	      best_size = i;
   5626 	      no_improvement_count = 0;
   5627 	    }
   5628 	  /* PR 11843: Avoid futile long searches for the best bucket size
   5629 	     when there are a large number of symbols.  */
   5630 	  else if (++no_improvement_count == 100)
   5631 	    break;
   5632 	}
   5633 
   5634       free (counts);
   5635     }
   5636   else
   5637 #endif /* defined (BFD_HOST_U_64_BIT) */
   5638     {
   5639       /* This is the fallback solution if no 64bit type is available or if we
   5640 	 are not supposed to spend much time on optimizations.  We select the
   5641 	 bucket count using a fixed set of numbers.  */
   5642       for (i = 0; elf_buckets[i] != 0; i++)
   5643 	{
   5644 	  best_size = elf_buckets[i];
   5645 	  if (nsyms < elf_buckets[i + 1])
   5646 	    break;
   5647 	}
   5648       if (gnu_hash && best_size < 2)
   5649 	best_size = 2;
   5650     }
   5651 
   5652   return best_size;
   5653 }
   5654 
   5655 /* Size any SHT_GROUP section for ld -r.  */
   5656 
   5657 bfd_boolean
   5658 _bfd_elf_size_group_sections (struct bfd_link_info *info)
   5659 {
   5660   bfd *ibfd;
   5661 
   5662   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   5663     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   5664 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
   5665       return FALSE;
   5666   return TRUE;
   5667 }
   5668 
   5669 /* Set a default stack segment size.  The value in INFO wins.  If it
   5670    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
   5671    undefined it is initialized.  */
   5672 
   5673 bfd_boolean
   5674 bfd_elf_stack_segment_size (bfd *output_bfd,
   5675 			    struct bfd_link_info *info,
   5676 			    const char *legacy_symbol,
   5677 			    bfd_vma default_size)
   5678 {
   5679   struct elf_link_hash_entry *h = NULL;
   5680 
   5681   /* Look for legacy symbol.  */
   5682   if (legacy_symbol)
   5683     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
   5684 			      FALSE, FALSE, FALSE);
   5685   if (h && (h->root.type == bfd_link_hash_defined
   5686 	    || h->root.type == bfd_link_hash_defweak)
   5687       && h->def_regular
   5688       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
   5689     {
   5690       /* The symbol has no type if specified on the command line.  */
   5691       h->type = STT_OBJECT;
   5692       if (info->stacksize)
   5693 	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
   5694 			       output_bfd, legacy_symbol);
   5695       else if (h->root.u.def.section != bfd_abs_section_ptr)
   5696 	(*_bfd_error_handler) (_("%B: %s not absolute"),
   5697 			       output_bfd, legacy_symbol);
   5698       else
   5699 	info->stacksize = h->root.u.def.value;
   5700     }
   5701 
   5702   if (!info->stacksize)
   5703     /* If the user didn't set a size, or explicitly inhibit the
   5704        size, set it now.  */
   5705     info->stacksize = default_size;
   5706 
   5707   /* Provide the legacy symbol, if it is referenced.  */
   5708   if (h && (h->root.type == bfd_link_hash_undefined
   5709 	    || h->root.type == bfd_link_hash_undefweak))
   5710     {
   5711       struct bfd_link_hash_entry *bh = NULL;
   5712 
   5713       if (!(_bfd_generic_link_add_one_symbol
   5714 	    (info, output_bfd, legacy_symbol,
   5715 	     BSF_GLOBAL, bfd_abs_section_ptr,
   5716 	     info->stacksize >= 0 ? info->stacksize : 0,
   5717 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
   5718 	return FALSE;
   5719 
   5720       h = (struct elf_link_hash_entry *) bh;
   5721       h->def_regular = 1;
   5722       h->type = STT_OBJECT;
   5723     }
   5724 
   5725   return TRUE;
   5726 }
   5727 
   5728 /* Set up the sizes and contents of the ELF dynamic sections.  This is
   5729    called by the ELF linker emulation before_allocation routine.  We
   5730    must set the sizes of the sections before the linker sets the
   5731    addresses of the various sections.  */
   5732 
   5733 bfd_boolean
   5734 bfd_elf_size_dynamic_sections (bfd *output_bfd,
   5735 			       const char *soname,
   5736 			       const char *rpath,
   5737 			       const char *filter_shlib,
   5738 			       const char *audit,
   5739 			       const char *depaudit,
   5740 			       const char * const *auxiliary_filters,
   5741 			       struct bfd_link_info *info,
   5742 			       asection **sinterpptr)
   5743 {
   5744   bfd_size_type soname_indx;
   5745   bfd *dynobj;
   5746   const struct elf_backend_data *bed;
   5747   struct elf_info_failed asvinfo;
   5748 
   5749   *sinterpptr = NULL;
   5750 
   5751   soname_indx = (bfd_size_type) -1;
   5752 
   5753   if (!is_elf_hash_table (info->hash))
   5754     return TRUE;
   5755 
   5756   bed = get_elf_backend_data (output_bfd);
   5757 
   5758   /* Any syms created from now on start with -1 in
   5759      got.refcount/offset and plt.refcount/offset.  */
   5760   elf_hash_table (info)->init_got_refcount
   5761     = elf_hash_table (info)->init_got_offset;
   5762   elf_hash_table (info)->init_plt_refcount
   5763     = elf_hash_table (info)->init_plt_offset;
   5764 
   5765   if (bfd_link_relocatable (info)
   5766       && !_bfd_elf_size_group_sections (info))
   5767     return FALSE;
   5768 
   5769   /* The backend may have to create some sections regardless of whether
   5770      we're dynamic or not.  */
   5771   if (bed->elf_backend_always_size_sections
   5772       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
   5773     return FALSE;
   5774 
   5775   /* Determine any GNU_STACK segment requirements, after the backend
   5776      has had a chance to set a default segment size.  */
   5777   if (info->execstack)
   5778     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
   5779   else if (info->noexecstack)
   5780     elf_stack_flags (output_bfd) = PF_R | PF_W;
   5781   else
   5782     {
   5783       bfd *inputobj;
   5784       asection *notesec = NULL;
   5785       int exec = 0;
   5786 
   5787       for (inputobj = info->input_bfds;
   5788 	   inputobj;
   5789 	   inputobj = inputobj->link.next)
   5790 	{
   5791 	  asection *s;
   5792 
   5793 	  if (inputobj->flags
   5794 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
   5795 	    continue;
   5796 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
   5797 	  if (s)
   5798 	    {
   5799 	      if (s->flags & SEC_CODE)
   5800 		exec = PF_X;
   5801 	      notesec = s;
   5802 	    }
   5803 	  else if (bed->default_execstack)
   5804 	    exec = PF_X;
   5805 	}
   5806       if (notesec || info->stacksize > 0)
   5807 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
   5808       if (notesec && exec && bfd_link_relocatable (info)
   5809 	  && notesec->output_section != bfd_abs_section_ptr)
   5810 	notesec->output_section->flags |= SEC_CODE;
   5811     }
   5812 
   5813   dynobj = elf_hash_table (info)->dynobj;
   5814 
   5815   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   5816     {
   5817       struct elf_info_failed eif;
   5818       struct elf_link_hash_entry *h;
   5819       asection *dynstr;
   5820       struct bfd_elf_version_tree *t;
   5821       struct bfd_elf_version_expr *d;
   5822       asection *s;
   5823       bfd_boolean all_defined;
   5824 
   5825       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
   5826       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
   5827 
   5828       if (soname != NULL)
   5829 	{
   5830 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5831 					     soname, TRUE);
   5832 	  if (soname_indx == (bfd_size_type) -1
   5833 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
   5834 	    return FALSE;
   5835 	}
   5836 
   5837       if (info->symbolic)
   5838 	{
   5839 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
   5840 	    return FALSE;
   5841 	  info->flags |= DF_SYMBOLIC;
   5842 	}
   5843 
   5844       if (rpath != NULL)
   5845 	{
   5846 	  bfd_size_type indx;
   5847 	  bfd_vma tag;
   5848 
   5849 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
   5850 				      TRUE);
   5851 	  if (indx == (bfd_size_type) -1)
   5852 	    return FALSE;
   5853 
   5854 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
   5855 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
   5856 	    return FALSE;
   5857 	}
   5858 
   5859       if (filter_shlib != NULL)
   5860 	{
   5861 	  bfd_size_type indx;
   5862 
   5863 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5864 				      filter_shlib, TRUE);
   5865 	  if (indx == (bfd_size_type) -1
   5866 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
   5867 	    return FALSE;
   5868 	}
   5869 
   5870       if (auxiliary_filters != NULL)
   5871 	{
   5872 	  const char * const *p;
   5873 
   5874 	  for (p = auxiliary_filters; *p != NULL; p++)
   5875 	    {
   5876 	      bfd_size_type indx;
   5877 
   5878 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   5879 					  *p, TRUE);
   5880 	      if (indx == (bfd_size_type) -1
   5881 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
   5882 		return FALSE;
   5883 	    }
   5884 	}
   5885 
   5886       if (audit != NULL)
   5887 	{
   5888 	  bfd_size_type indx;
   5889 
   5890 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
   5891 				      TRUE);
   5892 	  if (indx == (bfd_size_type) -1
   5893 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
   5894 	    return FALSE;
   5895 	}
   5896 
   5897       if (depaudit != NULL)
   5898 	{
   5899 	  bfd_size_type indx;
   5900 
   5901 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
   5902 				      TRUE);
   5903 	  if (indx == (bfd_size_type) -1
   5904 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
   5905 	    return FALSE;
   5906 	}
   5907 
   5908       eif.info = info;
   5909       eif.failed = FALSE;
   5910 
   5911       /* If we are supposed to export all symbols into the dynamic symbol
   5912 	 table (this is not the normal case), then do so.  */
   5913       if (info->export_dynamic
   5914 	  || (bfd_link_executable (info) && info->dynamic))
   5915 	{
   5916 	  elf_link_hash_traverse (elf_hash_table (info),
   5917 				  _bfd_elf_export_symbol,
   5918 				  &eif);
   5919 	  if (eif.failed)
   5920 	    return FALSE;
   5921 	}
   5922 
   5923       /* Make all global versions with definition.  */
   5924       for (t = info->version_info; t != NULL; t = t->next)
   5925 	for (d = t->globals.list; d != NULL; d = d->next)
   5926 	  if (!d->symver && d->literal)
   5927 	    {
   5928 	      const char *verstr, *name;
   5929 	      size_t namelen, verlen, newlen;
   5930 	      char *newname, *p, leading_char;
   5931 	      struct elf_link_hash_entry *newh;
   5932 
   5933 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
   5934 	      name = d->pattern;
   5935 	      namelen = strlen (name) + (leading_char != '\0');
   5936 	      verstr = t->name;
   5937 	      verlen = strlen (verstr);
   5938 	      newlen = namelen + verlen + 3;
   5939 
   5940 	      newname = (char *) bfd_malloc (newlen);
   5941 	      if (newname == NULL)
   5942 		return FALSE;
   5943 	      newname[0] = leading_char;
   5944 	      memcpy (newname + (leading_char != '\0'), name, namelen);
   5945 
   5946 	      /* Check the hidden versioned definition.  */
   5947 	      p = newname + namelen;
   5948 	      *p++ = ELF_VER_CHR;
   5949 	      memcpy (p, verstr, verlen + 1);
   5950 	      newh = elf_link_hash_lookup (elf_hash_table (info),
   5951 					   newname, FALSE, FALSE,
   5952 					   FALSE);
   5953 	      if (newh == NULL
   5954 		  || (newh->root.type != bfd_link_hash_defined
   5955 		      && newh->root.type != bfd_link_hash_defweak))
   5956 		{
   5957 		  /* Check the default versioned definition.  */
   5958 		  *p++ = ELF_VER_CHR;
   5959 		  memcpy (p, verstr, verlen + 1);
   5960 		  newh = elf_link_hash_lookup (elf_hash_table (info),
   5961 					       newname, FALSE, FALSE,
   5962 					       FALSE);
   5963 		}
   5964 	      free (newname);
   5965 
   5966 	      /* Mark this version if there is a definition and it is
   5967 		 not defined in a shared object.  */
   5968 	      if (newh != NULL
   5969 		  && !newh->def_dynamic
   5970 		  && (newh->root.type == bfd_link_hash_defined
   5971 		      || newh->root.type == bfd_link_hash_defweak))
   5972 		d->symver = 1;
   5973 	    }
   5974 
   5975       /* Attach all the symbols to their version information.  */
   5976       asvinfo.info = info;
   5977       asvinfo.failed = FALSE;
   5978 
   5979       elf_link_hash_traverse (elf_hash_table (info),
   5980 			      _bfd_elf_link_assign_sym_version,
   5981 			      &asvinfo);
   5982       if (asvinfo.failed)
   5983 	return FALSE;
   5984 
   5985       if (!info->allow_undefined_version)
   5986 	{
   5987 	  /* Check if all global versions have a definition.  */
   5988 	  all_defined = TRUE;
   5989 	  for (t = info->version_info; t != NULL; t = t->next)
   5990 	    for (d = t->globals.list; d != NULL; d = d->next)
   5991 	      if (d->literal && !d->symver && !d->script)
   5992 		{
   5993 		  (*_bfd_error_handler)
   5994 		    (_("%s: undefined version: %s"),
   5995 		     d->pattern, t->name);
   5996 		  all_defined = FALSE;
   5997 		}
   5998 
   5999 	  if (!all_defined)
   6000 	    {
   6001 	      bfd_set_error (bfd_error_bad_value);
   6002 	      return FALSE;
   6003 	    }
   6004 	}
   6005 
   6006       /* Find all symbols which were defined in a dynamic object and make
   6007 	 the backend pick a reasonable value for them.  */
   6008       elf_link_hash_traverse (elf_hash_table (info),
   6009 			      _bfd_elf_adjust_dynamic_symbol,
   6010 			      &eif);
   6011       if (eif.failed)
   6012 	return FALSE;
   6013 
   6014       /* Add some entries to the .dynamic section.  We fill in some of the
   6015 	 values later, in bfd_elf_final_link, but we must add the entries
   6016 	 now so that we know the final size of the .dynamic section.  */
   6017 
   6018       /* If there are initialization and/or finalization functions to
   6019 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
   6020       h = (info->init_function
   6021 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6022 				   info->init_function, FALSE,
   6023 				   FALSE, FALSE)
   6024 	   : NULL);
   6025       if (h != NULL
   6026 	  && (h->ref_regular
   6027 	      || h->def_regular))
   6028 	{
   6029 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
   6030 	    return FALSE;
   6031 	}
   6032       h = (info->fini_function
   6033 	   ? elf_link_hash_lookup (elf_hash_table (info),
   6034 				   info->fini_function, FALSE,
   6035 				   FALSE, FALSE)
   6036 	   : NULL);
   6037       if (h != NULL
   6038 	  && (h->ref_regular
   6039 	      || h->def_regular))
   6040 	{
   6041 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
   6042 	    return FALSE;
   6043 	}
   6044 
   6045       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
   6046       if (s != NULL && s->linker_has_input)
   6047 	{
   6048 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
   6049 	  if (! bfd_link_executable (info))
   6050 	    {
   6051 	      bfd *sub;
   6052 	      asection *o;
   6053 
   6054 	      for (sub = info->input_bfds; sub != NULL;
   6055 		   sub = sub->link.next)
   6056 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
   6057 		  for (o = sub->sections; o != NULL; o = o->next)
   6058 		    if (elf_section_data (o)->this_hdr.sh_type
   6059 			== SHT_PREINIT_ARRAY)
   6060 		      {
   6061 			(*_bfd_error_handler)
   6062 			  (_("%B: .preinit_array section is not allowed in DSO"),
   6063 			   sub);
   6064 			break;
   6065 		      }
   6066 
   6067 	      bfd_set_error (bfd_error_nonrepresentable_section);
   6068 	      return FALSE;
   6069 	    }
   6070 
   6071 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
   6072 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
   6073 	    return FALSE;
   6074 	}
   6075       s = bfd_get_section_by_name (output_bfd, ".init_array");
   6076       if (s != NULL && s->linker_has_input)
   6077 	{
   6078 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
   6079 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
   6080 	    return FALSE;
   6081 	}
   6082       s = bfd_get_section_by_name (output_bfd, ".fini_array");
   6083       if (s != NULL && s->linker_has_input)
   6084 	{
   6085 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
   6086 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
   6087 	    return FALSE;
   6088 	}
   6089 
   6090       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
   6091       /* If .dynstr is excluded from the link, we don't want any of
   6092 	 these tags.  Strictly, we should be checking each section
   6093 	 individually;  This quick check covers for the case where
   6094 	 someone does a /DISCARD/ : { *(*) }.  */
   6095       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
   6096 	{
   6097 	  bfd_size_type strsize;
   6098 
   6099 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   6100 	  if ((info->emit_hash
   6101 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
   6102 	      || (info->emit_gnu_hash
   6103 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
   6104 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
   6105 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
   6106 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
   6107 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
   6108 					      bed->s->sizeof_sym))
   6109 	    return FALSE;
   6110 	}
   6111     }
   6112 
   6113   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
   6114     return FALSE;
   6115 
   6116   /* The backend must work out the sizes of all the other dynamic
   6117      sections.  */
   6118   if (dynobj != NULL
   6119       && bed->elf_backend_size_dynamic_sections != NULL
   6120       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
   6121     return FALSE;
   6122 
   6123   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
   6124     {
   6125       unsigned long section_sym_count;
   6126       struct bfd_elf_version_tree *verdefs;
   6127       asection *s;
   6128 
   6129       /* Set up the version definition section.  */
   6130       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
   6131       BFD_ASSERT (s != NULL);
   6132 
   6133       /* We may have created additional version definitions if we are
   6134 	 just linking a regular application.  */
   6135       verdefs = info->version_info;
   6136 
   6137       /* Skip anonymous version tag.  */
   6138       if (verdefs != NULL && verdefs->vernum == 0)
   6139 	verdefs = verdefs->next;
   6140 
   6141       if (verdefs == NULL && !info->create_default_symver)
   6142 	s->flags |= SEC_EXCLUDE;
   6143       else
   6144 	{
   6145 	  unsigned int cdefs;
   6146 	  bfd_size_type size;
   6147 	  struct bfd_elf_version_tree *t;
   6148 	  bfd_byte *p;
   6149 	  Elf_Internal_Verdef def;
   6150 	  Elf_Internal_Verdaux defaux;
   6151 	  struct bfd_link_hash_entry *bh;
   6152 	  struct elf_link_hash_entry *h;
   6153 	  const char *name;
   6154 
   6155 	  cdefs = 0;
   6156 	  size = 0;
   6157 
   6158 	  /* Make space for the base version.  */
   6159 	  size += sizeof (Elf_External_Verdef);
   6160 	  size += sizeof (Elf_External_Verdaux);
   6161 	  ++cdefs;
   6162 
   6163 	  /* Make space for the default version.  */
   6164 	  if (info->create_default_symver)
   6165 	    {
   6166 	      size += sizeof (Elf_External_Verdef);
   6167 	      ++cdefs;
   6168 	    }
   6169 
   6170 	  for (t = verdefs; t != NULL; t = t->next)
   6171 	    {
   6172 	      struct bfd_elf_version_deps *n;
   6173 
   6174 	      /* Don't emit base version twice.  */
   6175 	      if (t->vernum == 0)
   6176 		continue;
   6177 
   6178 	      size += sizeof (Elf_External_Verdef);
   6179 	      size += sizeof (Elf_External_Verdaux);
   6180 	      ++cdefs;
   6181 
   6182 	      for (n = t->deps; n != NULL; n = n->next)
   6183 		size += sizeof (Elf_External_Verdaux);
   6184 	    }
   6185 
   6186 	  s->size = size;
   6187 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6188 	  if (s->contents == NULL && s->size != 0)
   6189 	    return FALSE;
   6190 
   6191 	  /* Fill in the version definition section.  */
   6192 
   6193 	  p = s->contents;
   6194 
   6195 	  def.vd_version = VER_DEF_CURRENT;
   6196 	  def.vd_flags = VER_FLG_BASE;
   6197 	  def.vd_ndx = 1;
   6198 	  def.vd_cnt = 1;
   6199 	  if (info->create_default_symver)
   6200 	    {
   6201 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
   6202 	      def.vd_next = sizeof (Elf_External_Verdef);
   6203 	    }
   6204 	  else
   6205 	    {
   6206 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6207 	      def.vd_next = (sizeof (Elf_External_Verdef)
   6208 			     + sizeof (Elf_External_Verdaux));
   6209 	    }
   6210 
   6211 	  if (soname_indx != (bfd_size_type) -1)
   6212 	    {
   6213 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6214 				      soname_indx);
   6215 	      def.vd_hash = bfd_elf_hash (soname);
   6216 	      defaux.vda_name = soname_indx;
   6217 	      name = soname;
   6218 	    }
   6219 	  else
   6220 	    {
   6221 	      bfd_size_type indx;
   6222 
   6223 	      name = lbasename (output_bfd->filename);
   6224 	      def.vd_hash = bfd_elf_hash (name);
   6225 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6226 					  name, FALSE);
   6227 	      if (indx == (bfd_size_type) -1)
   6228 		return FALSE;
   6229 	      defaux.vda_name = indx;
   6230 	    }
   6231 	  defaux.vda_next = 0;
   6232 
   6233 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
   6234 				    (Elf_External_Verdef *) p);
   6235 	  p += sizeof (Elf_External_Verdef);
   6236 	  if (info->create_default_symver)
   6237 	    {
   6238 	      /* Add a symbol representing this version.  */
   6239 	      bh = NULL;
   6240 	      if (! (_bfd_generic_link_add_one_symbol
   6241 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
   6242 		      0, NULL, FALSE,
   6243 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6244 		return FALSE;
   6245 	      h = (struct elf_link_hash_entry *) bh;
   6246 	      h->non_elf = 0;
   6247 	      h->def_regular = 1;
   6248 	      h->type = STT_OBJECT;
   6249 	      h->verinfo.vertree = NULL;
   6250 
   6251 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6252 		return FALSE;
   6253 
   6254 	      /* Create a duplicate of the base version with the same
   6255 		 aux block, but different flags.  */
   6256 	      def.vd_flags = 0;
   6257 	      def.vd_ndx = 2;
   6258 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6259 	      if (verdefs)
   6260 		def.vd_next = (sizeof (Elf_External_Verdef)
   6261 			       + sizeof (Elf_External_Verdaux));
   6262 	      else
   6263 		def.vd_next = 0;
   6264 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6265 					(Elf_External_Verdef *) p);
   6266 	      p += sizeof (Elf_External_Verdef);
   6267 	    }
   6268 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6269 				     (Elf_External_Verdaux *) p);
   6270 	  p += sizeof (Elf_External_Verdaux);
   6271 
   6272 	  for (t = verdefs; t != NULL; t = t->next)
   6273 	    {
   6274 	      unsigned int cdeps;
   6275 	      struct bfd_elf_version_deps *n;
   6276 
   6277 	      /* Don't emit the base version twice.  */
   6278 	      if (t->vernum == 0)
   6279 		continue;
   6280 
   6281 	      cdeps = 0;
   6282 	      for (n = t->deps; n != NULL; n = n->next)
   6283 		++cdeps;
   6284 
   6285 	      /* Add a symbol representing this version.  */
   6286 	      bh = NULL;
   6287 	      if (! (_bfd_generic_link_add_one_symbol
   6288 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
   6289 		      0, NULL, FALSE,
   6290 		      get_elf_backend_data (dynobj)->collect, &bh)))
   6291 		return FALSE;
   6292 	      h = (struct elf_link_hash_entry *) bh;
   6293 	      h->non_elf = 0;
   6294 	      h->def_regular = 1;
   6295 	      h->type = STT_OBJECT;
   6296 	      h->verinfo.vertree = t;
   6297 
   6298 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6299 		return FALSE;
   6300 
   6301 	      def.vd_version = VER_DEF_CURRENT;
   6302 	      def.vd_flags = 0;
   6303 	      if (t->globals.list == NULL
   6304 		  && t->locals.list == NULL
   6305 		  && ! t->used)
   6306 		def.vd_flags |= VER_FLG_WEAK;
   6307 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
   6308 	      def.vd_cnt = cdeps + 1;
   6309 	      def.vd_hash = bfd_elf_hash (t->name);
   6310 	      def.vd_aux = sizeof (Elf_External_Verdef);
   6311 	      def.vd_next = 0;
   6312 
   6313 	      /* If a basever node is next, it *must* be the last node in
   6314 		 the chain, otherwise Verdef construction breaks.  */
   6315 	      if (t->next != NULL && t->next->vernum == 0)
   6316 		BFD_ASSERT (t->next->next == NULL);
   6317 
   6318 	      if (t->next != NULL && t->next->vernum != 0)
   6319 		def.vd_next = (sizeof (Elf_External_Verdef)
   6320 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
   6321 
   6322 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
   6323 					(Elf_External_Verdef *) p);
   6324 	      p += sizeof (Elf_External_Verdef);
   6325 
   6326 	      defaux.vda_name = h->dynstr_index;
   6327 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6328 				      h->dynstr_index);
   6329 	      defaux.vda_next = 0;
   6330 	      if (t->deps != NULL)
   6331 		defaux.vda_next = sizeof (Elf_External_Verdaux);
   6332 	      t->name_indx = defaux.vda_name;
   6333 
   6334 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6335 					 (Elf_External_Verdaux *) p);
   6336 	      p += sizeof (Elf_External_Verdaux);
   6337 
   6338 	      for (n = t->deps; n != NULL; n = n->next)
   6339 		{
   6340 		  if (n->version_needed == NULL)
   6341 		    {
   6342 		      /* This can happen if there was an error in the
   6343 			 version script.  */
   6344 		      defaux.vda_name = 0;
   6345 		    }
   6346 		  else
   6347 		    {
   6348 		      defaux.vda_name = n->version_needed->name_indx;
   6349 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
   6350 					      defaux.vda_name);
   6351 		    }
   6352 		  if (n->next == NULL)
   6353 		    defaux.vda_next = 0;
   6354 		  else
   6355 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
   6356 
   6357 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
   6358 					     (Elf_External_Verdaux *) p);
   6359 		  p += sizeof (Elf_External_Verdaux);
   6360 		}
   6361 	    }
   6362 
   6363 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
   6364 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
   6365 	    return FALSE;
   6366 
   6367 	  elf_tdata (output_bfd)->cverdefs = cdefs;
   6368 	}
   6369 
   6370       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
   6371 	{
   6372 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
   6373 	    return FALSE;
   6374 	}
   6375       else if (info->flags & DF_BIND_NOW)
   6376 	{
   6377 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
   6378 	    return FALSE;
   6379 	}
   6380 
   6381       if (info->flags_1)
   6382 	{
   6383 	  if (bfd_link_executable (info))
   6384 	    info->flags_1 &= ~ (DF_1_INITFIRST
   6385 				| DF_1_NODELETE
   6386 				| DF_1_NOOPEN);
   6387 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
   6388 	    return FALSE;
   6389 	}
   6390 
   6391       /* Work out the size of the version reference section.  */
   6392 
   6393       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
   6394       BFD_ASSERT (s != NULL);
   6395       {
   6396 	struct elf_find_verdep_info sinfo;
   6397 
   6398 	sinfo.info = info;
   6399 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
   6400 	if (sinfo.vers == 0)
   6401 	  sinfo.vers = 1;
   6402 	sinfo.failed = FALSE;
   6403 
   6404 	elf_link_hash_traverse (elf_hash_table (info),
   6405 				_bfd_elf_link_find_version_dependencies,
   6406 				&sinfo);
   6407 	if (sinfo.failed)
   6408 	  return FALSE;
   6409 
   6410 	if (elf_tdata (output_bfd)->verref == NULL)
   6411 	  s->flags |= SEC_EXCLUDE;
   6412 	else
   6413 	  {
   6414 	    Elf_Internal_Verneed *t;
   6415 	    unsigned int size;
   6416 	    unsigned int crefs;
   6417 	    bfd_byte *p;
   6418 
   6419 	    /* Build the version dependency section.  */
   6420 	    size = 0;
   6421 	    crefs = 0;
   6422 	    for (t = elf_tdata (output_bfd)->verref;
   6423 		 t != NULL;
   6424 		 t = t->vn_nextref)
   6425 	      {
   6426 		Elf_Internal_Vernaux *a;
   6427 
   6428 		size += sizeof (Elf_External_Verneed);
   6429 		++crefs;
   6430 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6431 		  size += sizeof (Elf_External_Vernaux);
   6432 	      }
   6433 
   6434 	    s->size = size;
   6435 	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6436 	    if (s->contents == NULL)
   6437 	      return FALSE;
   6438 
   6439 	    p = s->contents;
   6440 	    for (t = elf_tdata (output_bfd)->verref;
   6441 		 t != NULL;
   6442 		 t = t->vn_nextref)
   6443 	      {
   6444 		unsigned int caux;
   6445 		Elf_Internal_Vernaux *a;
   6446 		bfd_size_type indx;
   6447 
   6448 		caux = 0;
   6449 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6450 		  ++caux;
   6451 
   6452 		t->vn_version = VER_NEED_CURRENT;
   6453 		t->vn_cnt = caux;
   6454 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6455 					    elf_dt_name (t->vn_bfd) != NULL
   6456 					    ? elf_dt_name (t->vn_bfd)
   6457 					    : lbasename (t->vn_bfd->filename),
   6458 					    FALSE);
   6459 		if (indx == (bfd_size_type) -1)
   6460 		  return FALSE;
   6461 		t->vn_file = indx;
   6462 		t->vn_aux = sizeof (Elf_External_Verneed);
   6463 		if (t->vn_nextref == NULL)
   6464 		  t->vn_next = 0;
   6465 		else
   6466 		  t->vn_next = (sizeof (Elf_External_Verneed)
   6467 				+ caux * sizeof (Elf_External_Vernaux));
   6468 
   6469 		_bfd_elf_swap_verneed_out (output_bfd, t,
   6470 					   (Elf_External_Verneed *) p);
   6471 		p += sizeof (Elf_External_Verneed);
   6472 
   6473 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   6474 		  {
   6475 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
   6476 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
   6477 						a->vna_nodename, FALSE);
   6478 		    if (indx == (bfd_size_type) -1)
   6479 		      return FALSE;
   6480 		    a->vna_name = indx;
   6481 		    if (a->vna_nextptr == NULL)
   6482 		      a->vna_next = 0;
   6483 		    else
   6484 		      a->vna_next = sizeof (Elf_External_Vernaux);
   6485 
   6486 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
   6487 					       (Elf_External_Vernaux *) p);
   6488 		    p += sizeof (Elf_External_Vernaux);
   6489 		  }
   6490 	      }
   6491 
   6492 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
   6493 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
   6494 	      return FALSE;
   6495 
   6496 	    elf_tdata (output_bfd)->cverrefs = crefs;
   6497 	  }
   6498       }
   6499 
   6500       if ((elf_tdata (output_bfd)->cverrefs == 0
   6501 	   && elf_tdata (output_bfd)->cverdefs == 0)
   6502 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   6503 					     &section_sym_count) == 0)
   6504 	{
   6505 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
   6506 	  s->flags |= SEC_EXCLUDE;
   6507 	}
   6508     }
   6509   return TRUE;
   6510 }
   6511 
   6512 /* Find the first non-excluded output section.  We'll use its
   6513    section symbol for some emitted relocs.  */
   6514 void
   6515 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
   6516 {
   6517   asection *s;
   6518 
   6519   for (s = output_bfd->sections; s != NULL; s = s->next)
   6520     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
   6521 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6522       {
   6523 	elf_hash_table (info)->text_index_section = s;
   6524 	break;
   6525       }
   6526 }
   6527 
   6528 /* Find two non-excluded output sections, one for code, one for data.
   6529    We'll use their section symbols for some emitted relocs.  */
   6530 void
   6531 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
   6532 {
   6533   asection *s;
   6534 
   6535   /* Data first, since setting text_index_section changes
   6536      _bfd_elf_link_omit_section_dynsym.  */
   6537   for (s = output_bfd->sections; s != NULL; s = s->next)
   6538     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
   6539 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6540       {
   6541 	elf_hash_table (info)->data_index_section = s;
   6542 	break;
   6543       }
   6544 
   6545   for (s = output_bfd->sections; s != NULL; s = s->next)
   6546     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
   6547 	 == (SEC_ALLOC | SEC_READONLY))
   6548 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
   6549       {
   6550 	elf_hash_table (info)->text_index_section = s;
   6551 	break;
   6552       }
   6553 
   6554   if (elf_hash_table (info)->text_index_section == NULL)
   6555     elf_hash_table (info)->text_index_section
   6556       = elf_hash_table (info)->data_index_section;
   6557 }
   6558 
   6559 bfd_boolean
   6560 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
   6561 {
   6562   const struct elf_backend_data *bed;
   6563 
   6564   if (!is_elf_hash_table (info->hash))
   6565     return TRUE;
   6566 
   6567   bed = get_elf_backend_data (output_bfd);
   6568   (*bed->elf_backend_init_index_section) (output_bfd, info);
   6569 
   6570   if (elf_hash_table (info)->dynamic_sections_created)
   6571     {
   6572       bfd *dynobj;
   6573       asection *s;
   6574       bfd_size_type dynsymcount;
   6575       unsigned long section_sym_count;
   6576       unsigned int dtagcount;
   6577 
   6578       dynobj = elf_hash_table (info)->dynobj;
   6579 
   6580       /* Assign dynsym indicies.  In a shared library we generate a
   6581 	 section symbol for each output section, which come first.
   6582 	 Next come all of the back-end allocated local dynamic syms,
   6583 	 followed by the rest of the global symbols.  */
   6584 
   6585       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
   6586 						    &section_sym_count);
   6587 
   6588       /* Work out the size of the symbol version section.  */
   6589       s = bfd_get_linker_section (dynobj, ".gnu.version");
   6590       BFD_ASSERT (s != NULL);
   6591       if (dynsymcount != 0
   6592 	  && (s->flags & SEC_EXCLUDE) == 0)
   6593 	{
   6594 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
   6595 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6596 	  if (s->contents == NULL)
   6597 	    return FALSE;
   6598 
   6599 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
   6600 	    return FALSE;
   6601 	}
   6602 
   6603       /* Set the size of the .dynsym and .hash sections.  We counted
   6604 	 the number of dynamic symbols in elf_link_add_object_symbols.
   6605 	 We will build the contents of .dynsym and .hash when we build
   6606 	 the final symbol table, because until then we do not know the
   6607 	 correct value to give the symbols.  We built the .dynstr
   6608 	 section as we went along in elf_link_add_object_symbols.  */
   6609       s = elf_hash_table (info)->dynsym;
   6610       BFD_ASSERT (s != NULL);
   6611       s->size = dynsymcount * bed->s->sizeof_sym;
   6612 
   6613       if (dynsymcount != 0)
   6614 	{
   6615 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
   6616 	  if (s->contents == NULL)
   6617 	    return FALSE;
   6618 
   6619 	  /* The first entry in .dynsym is a dummy symbol.
   6620 	     Clear all the section syms, in case we don't output them all.  */
   6621 	  ++section_sym_count;
   6622 	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
   6623 	}
   6624 
   6625       elf_hash_table (info)->bucketcount = 0;
   6626 
   6627       /* Compute the size of the hashing table.  As a side effect this
   6628 	 computes the hash values for all the names we export.  */
   6629       if (info->emit_hash)
   6630 	{
   6631 	  unsigned long int *hashcodes;
   6632 	  struct hash_codes_info hashinf;
   6633 	  bfd_size_type amt;
   6634 	  unsigned long int nsyms;
   6635 	  size_t bucketcount;
   6636 	  size_t hash_entry_size;
   6637 
   6638 	  /* Compute the hash values for all exported symbols.  At the same
   6639 	     time store the values in an array so that we could use them for
   6640 	     optimizations.  */
   6641 	  amt = dynsymcount * sizeof (unsigned long int);
   6642 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
   6643 	  if (hashcodes == NULL)
   6644 	    return FALSE;
   6645 	  hashinf.hashcodes = hashcodes;
   6646 	  hashinf.error = FALSE;
   6647 
   6648 	  /* Put all hash values in HASHCODES.  */
   6649 	  elf_link_hash_traverse (elf_hash_table (info),
   6650 				  elf_collect_hash_codes, &hashinf);
   6651 	  if (hashinf.error)
   6652 	    {
   6653 	      free (hashcodes);
   6654 	      return FALSE;
   6655 	    }
   6656 
   6657 	  nsyms = hashinf.hashcodes - hashcodes;
   6658 	  bucketcount
   6659 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
   6660 	  free (hashcodes);
   6661 
   6662 	  if (bucketcount == 0)
   6663 	    return FALSE;
   6664 
   6665 	  elf_hash_table (info)->bucketcount = bucketcount;
   6666 
   6667 	  s = bfd_get_linker_section (dynobj, ".hash");
   6668 	  BFD_ASSERT (s != NULL);
   6669 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
   6670 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
   6671 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6672 	  if (s->contents == NULL)
   6673 	    return FALSE;
   6674 
   6675 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
   6676 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
   6677 		   s->contents + hash_entry_size);
   6678 	}
   6679 
   6680       if (info->emit_gnu_hash)
   6681 	{
   6682 	  size_t i, cnt;
   6683 	  unsigned char *contents;
   6684 	  struct collect_gnu_hash_codes cinfo;
   6685 	  bfd_size_type amt;
   6686 	  size_t bucketcount;
   6687 
   6688 	  memset (&cinfo, 0, sizeof (cinfo));
   6689 
   6690 	  /* Compute the hash values for all exported symbols.  At the same
   6691 	     time store the values in an array so that we could use them for
   6692 	     optimizations.  */
   6693 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
   6694 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
   6695 	  if (cinfo.hashcodes == NULL)
   6696 	    return FALSE;
   6697 
   6698 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
   6699 	  cinfo.min_dynindx = -1;
   6700 	  cinfo.output_bfd = output_bfd;
   6701 	  cinfo.bed = bed;
   6702 
   6703 	  /* Put all hash values in HASHCODES.  */
   6704 	  elf_link_hash_traverse (elf_hash_table (info),
   6705 				  elf_collect_gnu_hash_codes, &cinfo);
   6706 	  if (cinfo.error)
   6707 	    {
   6708 	      free (cinfo.hashcodes);
   6709 	      return FALSE;
   6710 	    }
   6711 
   6712 	  bucketcount
   6713 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
   6714 
   6715 	  if (bucketcount == 0)
   6716 	    {
   6717 	      free (cinfo.hashcodes);
   6718 	      return FALSE;
   6719 	    }
   6720 
   6721 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
   6722 	  BFD_ASSERT (s != NULL);
   6723 
   6724 	  if (cinfo.nsyms == 0)
   6725 	    {
   6726 	      /* Empty .gnu.hash section is special.  */
   6727 	      BFD_ASSERT (cinfo.min_dynindx == -1);
   6728 	      free (cinfo.hashcodes);
   6729 	      s->size = 5 * 4 + bed->s->arch_size / 8;
   6730 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6731 	      if (contents == NULL)
   6732 		return FALSE;
   6733 	      s->contents = contents;
   6734 	      /* 1 empty bucket.  */
   6735 	      bfd_put_32 (output_bfd, 1, contents);
   6736 	      /* SYMIDX above the special symbol 0.  */
   6737 	      bfd_put_32 (output_bfd, 1, contents + 4);
   6738 	      /* Just one word for bitmask.  */
   6739 	      bfd_put_32 (output_bfd, 1, contents + 8);
   6740 	      /* Only hash fn bloom filter.  */
   6741 	      bfd_put_32 (output_bfd, 0, contents + 12);
   6742 	      /* No hashes are valid - empty bitmask.  */
   6743 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
   6744 	      /* No hashes in the only bucket.  */
   6745 	      bfd_put_32 (output_bfd, 0,
   6746 			  contents + 16 + bed->s->arch_size / 8);
   6747 	    }
   6748 	  else
   6749 	    {
   6750 	      unsigned long int maskwords, maskbitslog2, x;
   6751 	      BFD_ASSERT (cinfo.min_dynindx != -1);
   6752 
   6753 	      x = cinfo.nsyms;
   6754 	      maskbitslog2 = 1;
   6755 	      while ((x >>= 1) != 0)
   6756 		++maskbitslog2;
   6757 	      if (maskbitslog2 < 3)
   6758 		maskbitslog2 = 5;
   6759 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
   6760 		maskbitslog2 = maskbitslog2 + 3;
   6761 	      else
   6762 		maskbitslog2 = maskbitslog2 + 2;
   6763 	      if (bed->s->arch_size == 64)
   6764 		{
   6765 		  if (maskbitslog2 == 5)
   6766 		    maskbitslog2 = 6;
   6767 		  cinfo.shift1 = 6;
   6768 		}
   6769 	      else
   6770 		cinfo.shift1 = 5;
   6771 	      cinfo.mask = (1 << cinfo.shift1) - 1;
   6772 	      cinfo.shift2 = maskbitslog2;
   6773 	      cinfo.maskbits = 1 << maskbitslog2;
   6774 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
   6775 	      amt = bucketcount * sizeof (unsigned long int) * 2;
   6776 	      amt += maskwords * sizeof (bfd_vma);
   6777 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
   6778 	      if (cinfo.bitmask == NULL)
   6779 		{
   6780 		  free (cinfo.hashcodes);
   6781 		  return FALSE;
   6782 		}
   6783 
   6784 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
   6785 	      cinfo.indx = cinfo.counts + bucketcount;
   6786 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
   6787 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
   6788 
   6789 	      /* Determine how often each hash bucket is used.  */
   6790 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
   6791 	      for (i = 0; i < cinfo.nsyms; ++i)
   6792 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
   6793 
   6794 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
   6795 		if (cinfo.counts[i] != 0)
   6796 		  {
   6797 		    cinfo.indx[i] = cnt;
   6798 		    cnt += cinfo.counts[i];
   6799 		  }
   6800 	      BFD_ASSERT (cnt == dynsymcount);
   6801 	      cinfo.bucketcount = bucketcount;
   6802 	      cinfo.local_indx = cinfo.min_dynindx;
   6803 
   6804 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
   6805 	      s->size += cinfo.maskbits / 8;
   6806 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
   6807 	      if (contents == NULL)
   6808 		{
   6809 		  free (cinfo.bitmask);
   6810 		  free (cinfo.hashcodes);
   6811 		  return FALSE;
   6812 		}
   6813 
   6814 	      s->contents = contents;
   6815 	      bfd_put_32 (output_bfd, bucketcount, contents);
   6816 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
   6817 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
   6818 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
   6819 	      contents += 16 + cinfo.maskbits / 8;
   6820 
   6821 	      for (i = 0; i < bucketcount; ++i)
   6822 		{
   6823 		  if (cinfo.counts[i] == 0)
   6824 		    bfd_put_32 (output_bfd, 0, contents);
   6825 		  else
   6826 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
   6827 		  contents += 4;
   6828 		}
   6829 
   6830 	      cinfo.contents = contents;
   6831 
   6832 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
   6833 	      elf_link_hash_traverse (elf_hash_table (info),
   6834 				      elf_renumber_gnu_hash_syms, &cinfo);
   6835 
   6836 	      contents = s->contents + 16;
   6837 	      for (i = 0; i < maskwords; ++i)
   6838 		{
   6839 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
   6840 			   contents);
   6841 		  contents += bed->s->arch_size / 8;
   6842 		}
   6843 
   6844 	      free (cinfo.bitmask);
   6845 	      free (cinfo.hashcodes);
   6846 	    }
   6847 	}
   6848 
   6849       s = bfd_get_linker_section (dynobj, ".dynstr");
   6850       BFD_ASSERT (s != NULL);
   6851 
   6852       elf_finalize_dynstr (output_bfd, info);
   6853 
   6854       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   6855 
   6856       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
   6857 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
   6858 	  return FALSE;
   6859     }
   6860 
   6861   return TRUE;
   6862 }
   6863 
   6864 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
   6866 
   6867 static void
   6868 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
   6869 			    asection *sec)
   6870 {
   6871   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
   6872   sec->sec_info_type = SEC_INFO_TYPE_NONE;
   6873 }
   6874 
   6875 /* Finish SHF_MERGE section merging.  */
   6876 
   6877 bfd_boolean
   6878 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
   6879 {
   6880   bfd *ibfd;
   6881   asection *sec;
   6882 
   6883   if (!is_elf_hash_table (info->hash))
   6884     return FALSE;
   6885 
   6886   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   6887     if ((ibfd->flags & DYNAMIC) == 0
   6888 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
   6889 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   6890 	    == get_elf_backend_data (obfd)->s->elfclass))
   6891       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   6892 	if ((sec->flags & SEC_MERGE) != 0
   6893 	    && !bfd_is_abs_section (sec->output_section))
   6894 	  {
   6895 	    struct bfd_elf_section_data *secdata;
   6896 
   6897 	    secdata = elf_section_data (sec);
   6898 	    if (! _bfd_add_merge_section (obfd,
   6899 					  &elf_hash_table (info)->merge_info,
   6900 					  sec, &secdata->sec_info))
   6901 	      return FALSE;
   6902 	    else if (secdata->sec_info)
   6903 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
   6904 	  }
   6905 
   6906   if (elf_hash_table (info)->merge_info != NULL)
   6907     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
   6908 			 merge_sections_remove_hook);
   6909   return TRUE;
   6910 }
   6911 
   6912 /* Create an entry in an ELF linker hash table.  */
   6913 
   6914 struct bfd_hash_entry *
   6915 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   6916 			    struct bfd_hash_table *table,
   6917 			    const char *string)
   6918 {
   6919   /* Allocate the structure if it has not already been allocated by a
   6920      subclass.  */
   6921   if (entry == NULL)
   6922     {
   6923       entry = (struct bfd_hash_entry *)
   6924 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
   6925       if (entry == NULL)
   6926 	return entry;
   6927     }
   6928 
   6929   /* Call the allocation method of the superclass.  */
   6930   entry = _bfd_link_hash_newfunc (entry, table, string);
   6931   if (entry != NULL)
   6932     {
   6933       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
   6934       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
   6935 
   6936       /* Set local fields.  */
   6937       ret->indx = -1;
   6938       ret->dynindx = -1;
   6939       ret->got = htab->init_got_refcount;
   6940       ret->plt = htab->init_plt_refcount;
   6941       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
   6942 			      - offsetof (struct elf_link_hash_entry, size)));
   6943       /* Assume that we have been called by a non-ELF symbol reader.
   6944 	 This flag is then reset by the code which reads an ELF input
   6945 	 file.  This ensures that a symbol created by a non-ELF symbol
   6946 	 reader will have the flag set correctly.  */
   6947       ret->non_elf = 1;
   6948     }
   6949 
   6950   return entry;
   6951 }
   6952 
   6953 /* Copy data from an indirect symbol to its direct symbol, hiding the
   6954    old indirect symbol.  Also used for copying flags to a weakdef.  */
   6955 
   6956 void
   6957 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
   6958 				  struct elf_link_hash_entry *dir,
   6959 				  struct elf_link_hash_entry *ind)
   6960 {
   6961   struct elf_link_hash_table *htab;
   6962 
   6963   /* Copy down any references that we may have already seen to the
   6964      symbol which just became indirect if DIR isn't a hidden versioned
   6965      symbol.  */
   6966 
   6967   if (dir->versioned != versioned_hidden)
   6968     {
   6969       dir->ref_dynamic |= ind->ref_dynamic;
   6970       dir->ref_regular |= ind->ref_regular;
   6971       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   6972       dir->non_got_ref |= ind->non_got_ref;
   6973       dir->needs_plt |= ind->needs_plt;
   6974       dir->pointer_equality_needed |= ind->pointer_equality_needed;
   6975     }
   6976 
   6977   if (ind->root.type != bfd_link_hash_indirect)
   6978     return;
   6979 
   6980   /* Copy over the global and procedure linkage table refcount entries.
   6981      These may have been already set up by a check_relocs routine.  */
   6982   htab = elf_hash_table (info);
   6983   if (ind->got.refcount > htab->init_got_refcount.refcount)
   6984     {
   6985       if (dir->got.refcount < 0)
   6986 	dir->got.refcount = 0;
   6987       dir->got.refcount += ind->got.refcount;
   6988       ind->got.refcount = htab->init_got_refcount.refcount;
   6989     }
   6990 
   6991   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
   6992     {
   6993       if (dir->plt.refcount < 0)
   6994 	dir->plt.refcount = 0;
   6995       dir->plt.refcount += ind->plt.refcount;
   6996       ind->plt.refcount = htab->init_plt_refcount.refcount;
   6997     }
   6998 
   6999   if (ind->dynindx != -1)
   7000     {
   7001       if (dir->dynindx != -1)
   7002 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
   7003       dir->dynindx = ind->dynindx;
   7004       dir->dynstr_index = ind->dynstr_index;
   7005       ind->dynindx = -1;
   7006       ind->dynstr_index = 0;
   7007     }
   7008 }
   7009 
   7010 void
   7011 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
   7012 				struct elf_link_hash_entry *h,
   7013 				bfd_boolean force_local)
   7014 {
   7015   /* STT_GNU_IFUNC symbol must go through PLT.  */
   7016   if (h->type != STT_GNU_IFUNC)
   7017     {
   7018       h->plt = elf_hash_table (info)->init_plt_offset;
   7019       h->needs_plt = 0;
   7020     }
   7021   if (force_local)
   7022     {
   7023       h->forced_local = 1;
   7024       if (h->dynindx != -1)
   7025 	{
   7026 	  h->dynindx = -1;
   7027 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7028 				  h->dynstr_index);
   7029 	}
   7030     }
   7031 }
   7032 
   7033 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
   7034    caller.  */
   7035 
   7036 bfd_boolean
   7037 _bfd_elf_link_hash_table_init
   7038   (struct elf_link_hash_table *table,
   7039    bfd *abfd,
   7040    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
   7041 				      struct bfd_hash_table *,
   7042 				      const char *),
   7043    unsigned int entsize,
   7044    enum elf_target_id target_id)
   7045 {
   7046   bfd_boolean ret;
   7047   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
   7048 
   7049   table->init_got_refcount.refcount = can_refcount - 1;
   7050   table->init_plt_refcount.refcount = can_refcount - 1;
   7051   table->init_got_offset.offset = -(bfd_vma) 1;
   7052   table->init_plt_offset.offset = -(bfd_vma) 1;
   7053   /* The first dynamic symbol is a dummy.  */
   7054   table->dynsymcount = 1;
   7055 
   7056   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
   7057 
   7058   table->root.type = bfd_link_elf_hash_table;
   7059   table->hash_table_id = target_id;
   7060 
   7061   return ret;
   7062 }
   7063 
   7064 /* Create an ELF linker hash table.  */
   7065 
   7066 struct bfd_link_hash_table *
   7067 _bfd_elf_link_hash_table_create (bfd *abfd)
   7068 {
   7069   struct elf_link_hash_table *ret;
   7070   bfd_size_type amt = sizeof (struct elf_link_hash_table);
   7071 
   7072   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
   7073   if (ret == NULL)
   7074     return NULL;
   7075 
   7076   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
   7077 				       sizeof (struct elf_link_hash_entry),
   7078 				       GENERIC_ELF_DATA))
   7079     {
   7080       free (ret);
   7081       return NULL;
   7082     }
   7083   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
   7084 
   7085   return &ret->root;
   7086 }
   7087 
   7088 /* Destroy an ELF linker hash table.  */
   7089 
   7090 void
   7091 _bfd_elf_link_hash_table_free (bfd *obfd)
   7092 {
   7093   struct elf_link_hash_table *htab;
   7094 
   7095   htab = (struct elf_link_hash_table *) obfd->link.hash;
   7096   if (htab->dynstr != NULL)
   7097     _bfd_elf_strtab_free (htab->dynstr);
   7098   _bfd_merge_sections_free (htab->merge_info);
   7099   _bfd_generic_link_hash_table_free (obfd);
   7100 }
   7101 
   7102 /* This is a hook for the ELF emulation code in the generic linker to
   7103    tell the backend linker what file name to use for the DT_NEEDED
   7104    entry for a dynamic object.  */
   7105 
   7106 void
   7107 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
   7108 {
   7109   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7110       && bfd_get_format (abfd) == bfd_object)
   7111     elf_dt_name (abfd) = name;
   7112 }
   7113 
   7114 int
   7115 bfd_elf_get_dyn_lib_class (bfd *abfd)
   7116 {
   7117   int lib_class;
   7118   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7119       && bfd_get_format (abfd) == bfd_object)
   7120     lib_class = elf_dyn_lib_class (abfd);
   7121   else
   7122     lib_class = 0;
   7123   return lib_class;
   7124 }
   7125 
   7126 void
   7127 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
   7128 {
   7129   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7130       && bfd_get_format (abfd) == bfd_object)
   7131     elf_dyn_lib_class (abfd) = lib_class;
   7132 }
   7133 
   7134 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
   7135    the linker ELF emulation code.  */
   7136 
   7137 struct bfd_link_needed_list *
   7138 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
   7139 			 struct bfd_link_info *info)
   7140 {
   7141   if (! is_elf_hash_table (info->hash))
   7142     return NULL;
   7143   return elf_hash_table (info)->needed;
   7144 }
   7145 
   7146 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
   7147    hook for the linker ELF emulation code.  */
   7148 
   7149 struct bfd_link_needed_list *
   7150 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
   7151 			  struct bfd_link_info *info)
   7152 {
   7153   if (! is_elf_hash_table (info->hash))
   7154     return NULL;
   7155   return elf_hash_table (info)->runpath;
   7156 }
   7157 
   7158 /* Get the name actually used for a dynamic object for a link.  This
   7159    is the SONAME entry if there is one.  Otherwise, it is the string
   7160    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
   7161 
   7162 const char *
   7163 bfd_elf_get_dt_soname (bfd *abfd)
   7164 {
   7165   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   7166       && bfd_get_format (abfd) == bfd_object)
   7167     return elf_dt_name (abfd);
   7168   return NULL;
   7169 }
   7170 
   7171 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
   7172    the ELF linker emulation code.  */
   7173 
   7174 bfd_boolean
   7175 bfd_elf_get_bfd_needed_list (bfd *abfd,
   7176 			     struct bfd_link_needed_list **pneeded)
   7177 {
   7178   asection *s;
   7179   bfd_byte *dynbuf = NULL;
   7180   unsigned int elfsec;
   7181   unsigned long shlink;
   7182   bfd_byte *extdyn, *extdynend;
   7183   size_t extdynsize;
   7184   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   7185 
   7186   *pneeded = NULL;
   7187 
   7188   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
   7189       || bfd_get_format (abfd) != bfd_object)
   7190     return TRUE;
   7191 
   7192   s = bfd_get_section_by_name (abfd, ".dynamic");
   7193   if (s == NULL || s->size == 0)
   7194     return TRUE;
   7195 
   7196   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   7197     goto error_return;
   7198 
   7199   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   7200   if (elfsec == SHN_BAD)
   7201     goto error_return;
   7202 
   7203   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   7204 
   7205   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   7206   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   7207 
   7208   extdyn = dynbuf;
   7209   extdynend = extdyn + s->size;
   7210   for (; extdyn < extdynend; extdyn += extdynsize)
   7211     {
   7212       Elf_Internal_Dyn dyn;
   7213 
   7214       (*swap_dyn_in) (abfd, extdyn, &dyn);
   7215 
   7216       if (dyn.d_tag == DT_NULL)
   7217 	break;
   7218 
   7219       if (dyn.d_tag == DT_NEEDED)
   7220 	{
   7221 	  const char *string;
   7222 	  struct bfd_link_needed_list *l;
   7223 	  unsigned int tagv = dyn.d_un.d_val;
   7224 	  bfd_size_type amt;
   7225 
   7226 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   7227 	  if (string == NULL)
   7228 	    goto error_return;
   7229 
   7230 	  amt = sizeof *l;
   7231 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
   7232 	  if (l == NULL)
   7233 	    goto error_return;
   7234 
   7235 	  l->by = abfd;
   7236 	  l->name = string;
   7237 	  l->next = *pneeded;
   7238 	  *pneeded = l;
   7239 	}
   7240     }
   7241 
   7242   free (dynbuf);
   7243 
   7244   return TRUE;
   7245 
   7246  error_return:
   7247   if (dynbuf != NULL)
   7248     free (dynbuf);
   7249   return FALSE;
   7250 }
   7251 
   7252 struct elf_symbuf_symbol
   7253 {
   7254   unsigned long st_name;	/* Symbol name, index in string tbl */
   7255   unsigned char st_info;	/* Type and binding attributes */
   7256   unsigned char st_other;	/* Visibilty, and target specific */
   7257 };
   7258 
   7259 struct elf_symbuf_head
   7260 {
   7261   struct elf_symbuf_symbol *ssym;
   7262   bfd_size_type count;
   7263   unsigned int st_shndx;
   7264 };
   7265 
   7266 struct elf_symbol
   7267 {
   7268   union
   7269     {
   7270       Elf_Internal_Sym *isym;
   7271       struct elf_symbuf_symbol *ssym;
   7272     } u;
   7273   const char *name;
   7274 };
   7275 
   7276 /* Sort references to symbols by ascending section number.  */
   7277 
   7278 static int
   7279 elf_sort_elf_symbol (const void *arg1, const void *arg2)
   7280 {
   7281   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
   7282   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
   7283 
   7284   return s1->st_shndx - s2->st_shndx;
   7285 }
   7286 
   7287 static int
   7288 elf_sym_name_compare (const void *arg1, const void *arg2)
   7289 {
   7290   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
   7291   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
   7292   return strcmp (s1->name, s2->name);
   7293 }
   7294 
   7295 static struct elf_symbuf_head *
   7296 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
   7297 {
   7298   Elf_Internal_Sym **ind, **indbufend, **indbuf;
   7299   struct elf_symbuf_symbol *ssym;
   7300   struct elf_symbuf_head *ssymbuf, *ssymhead;
   7301   bfd_size_type i, shndx_count, total_size;
   7302 
   7303   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
   7304   if (indbuf == NULL)
   7305     return NULL;
   7306 
   7307   for (ind = indbuf, i = 0; i < symcount; i++)
   7308     if (isymbuf[i].st_shndx != SHN_UNDEF)
   7309       *ind++ = &isymbuf[i];
   7310   indbufend = ind;
   7311 
   7312   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
   7313 	 elf_sort_elf_symbol);
   7314 
   7315   shndx_count = 0;
   7316   if (indbufend > indbuf)
   7317     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
   7318       if (ind[0]->st_shndx != ind[1]->st_shndx)
   7319 	shndx_count++;
   7320 
   7321   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
   7322 		+ (indbufend - indbuf) * sizeof (*ssym));
   7323   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
   7324   if (ssymbuf == NULL)
   7325     {
   7326       free (indbuf);
   7327       return NULL;
   7328     }
   7329 
   7330   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
   7331   ssymbuf->ssym = NULL;
   7332   ssymbuf->count = shndx_count;
   7333   ssymbuf->st_shndx = 0;
   7334   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
   7335     {
   7336       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
   7337 	{
   7338 	  ssymhead++;
   7339 	  ssymhead->ssym = ssym;
   7340 	  ssymhead->count = 0;
   7341 	  ssymhead->st_shndx = (*ind)->st_shndx;
   7342 	}
   7343       ssym->st_name = (*ind)->st_name;
   7344       ssym->st_info = (*ind)->st_info;
   7345       ssym->st_other = (*ind)->st_other;
   7346       ssymhead->count++;
   7347     }
   7348   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
   7349 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
   7350 		  == total_size));
   7351 
   7352   free (indbuf);
   7353   return ssymbuf;
   7354 }
   7355 
   7356 /* Check if 2 sections define the same set of local and global
   7357    symbols.  */
   7358 
   7359 static bfd_boolean
   7360 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
   7361 				   struct bfd_link_info *info)
   7362 {
   7363   bfd *bfd1, *bfd2;
   7364   const struct elf_backend_data *bed1, *bed2;
   7365   Elf_Internal_Shdr *hdr1, *hdr2;
   7366   bfd_size_type symcount1, symcount2;
   7367   Elf_Internal_Sym *isymbuf1, *isymbuf2;
   7368   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
   7369   Elf_Internal_Sym *isym, *isymend;
   7370   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
   7371   bfd_size_type count1, count2, i;
   7372   unsigned int shndx1, shndx2;
   7373   bfd_boolean result;
   7374 
   7375   bfd1 = sec1->owner;
   7376   bfd2 = sec2->owner;
   7377 
   7378   /* Both sections have to be in ELF.  */
   7379   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
   7380       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
   7381     return FALSE;
   7382 
   7383   if (elf_section_type (sec1) != elf_section_type (sec2))
   7384     return FALSE;
   7385 
   7386   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
   7387   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
   7388   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
   7389     return FALSE;
   7390 
   7391   bed1 = get_elf_backend_data (bfd1);
   7392   bed2 = get_elf_backend_data (bfd2);
   7393   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
   7394   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
   7395   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
   7396   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
   7397 
   7398   if (symcount1 == 0 || symcount2 == 0)
   7399     return FALSE;
   7400 
   7401   result = FALSE;
   7402   isymbuf1 = NULL;
   7403   isymbuf2 = NULL;
   7404   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
   7405   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
   7406 
   7407   if (ssymbuf1 == NULL)
   7408     {
   7409       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
   7410 				       NULL, NULL, NULL);
   7411       if (isymbuf1 == NULL)
   7412 	goto done;
   7413 
   7414       if (!info->reduce_memory_overheads)
   7415 	elf_tdata (bfd1)->symbuf = ssymbuf1
   7416 	  = elf_create_symbuf (symcount1, isymbuf1);
   7417     }
   7418 
   7419   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
   7420     {
   7421       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
   7422 				       NULL, NULL, NULL);
   7423       if (isymbuf2 == NULL)
   7424 	goto done;
   7425 
   7426       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
   7427 	elf_tdata (bfd2)->symbuf = ssymbuf2
   7428 	  = elf_create_symbuf (symcount2, isymbuf2);
   7429     }
   7430 
   7431   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
   7432     {
   7433       /* Optimized faster version.  */
   7434       bfd_size_type lo, hi, mid;
   7435       struct elf_symbol *symp;
   7436       struct elf_symbuf_symbol *ssym, *ssymend;
   7437 
   7438       lo = 0;
   7439       hi = ssymbuf1->count;
   7440       ssymbuf1++;
   7441       count1 = 0;
   7442       while (lo < hi)
   7443 	{
   7444 	  mid = (lo + hi) / 2;
   7445 	  if (shndx1 < ssymbuf1[mid].st_shndx)
   7446 	    hi = mid;
   7447 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
   7448 	    lo = mid + 1;
   7449 	  else
   7450 	    {
   7451 	      count1 = ssymbuf1[mid].count;
   7452 	      ssymbuf1 += mid;
   7453 	      break;
   7454 	    }
   7455 	}
   7456 
   7457       lo = 0;
   7458       hi = ssymbuf2->count;
   7459       ssymbuf2++;
   7460       count2 = 0;
   7461       while (lo < hi)
   7462 	{
   7463 	  mid = (lo + hi) / 2;
   7464 	  if (shndx2 < ssymbuf2[mid].st_shndx)
   7465 	    hi = mid;
   7466 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
   7467 	    lo = mid + 1;
   7468 	  else
   7469 	    {
   7470 	      count2 = ssymbuf2[mid].count;
   7471 	      ssymbuf2 += mid;
   7472 	      break;
   7473 	    }
   7474 	}
   7475 
   7476       if (count1 == 0 || count2 == 0 || count1 != count2)
   7477 	goto done;
   7478 
   7479       symtable1
   7480 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
   7481       symtable2
   7482 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
   7483       if (symtable1 == NULL || symtable2 == NULL)
   7484 	goto done;
   7485 
   7486       symp = symtable1;
   7487       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
   7488 	   ssym < ssymend; ssym++, symp++)
   7489 	{
   7490 	  symp->u.ssym = ssym;
   7491 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
   7492 							hdr1->sh_link,
   7493 							ssym->st_name);
   7494 	}
   7495 
   7496       symp = symtable2;
   7497       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
   7498 	   ssym < ssymend; ssym++, symp++)
   7499 	{
   7500 	  symp->u.ssym = ssym;
   7501 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
   7502 							hdr2->sh_link,
   7503 							ssym->st_name);
   7504 	}
   7505 
   7506       /* Sort symbol by name.  */
   7507       qsort (symtable1, count1, sizeof (struct elf_symbol),
   7508 	     elf_sym_name_compare);
   7509       qsort (symtable2, count1, sizeof (struct elf_symbol),
   7510 	     elf_sym_name_compare);
   7511 
   7512       for (i = 0; i < count1; i++)
   7513 	/* Two symbols must have the same binding, type and name.  */
   7514 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
   7515 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
   7516 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7517 	  goto done;
   7518 
   7519       result = TRUE;
   7520       goto done;
   7521     }
   7522 
   7523   symtable1 = (struct elf_symbol *)
   7524       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
   7525   symtable2 = (struct elf_symbol *)
   7526       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
   7527   if (symtable1 == NULL || symtable2 == NULL)
   7528     goto done;
   7529 
   7530   /* Count definitions in the section.  */
   7531   count1 = 0;
   7532   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
   7533     if (isym->st_shndx == shndx1)
   7534       symtable1[count1++].u.isym = isym;
   7535 
   7536   count2 = 0;
   7537   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
   7538     if (isym->st_shndx == shndx2)
   7539       symtable2[count2++].u.isym = isym;
   7540 
   7541   if (count1 == 0 || count2 == 0 || count1 != count2)
   7542     goto done;
   7543 
   7544   for (i = 0; i < count1; i++)
   7545     symtable1[i].name
   7546       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
   7547 					 symtable1[i].u.isym->st_name);
   7548 
   7549   for (i = 0; i < count2; i++)
   7550     symtable2[i].name
   7551       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
   7552 					 symtable2[i].u.isym->st_name);
   7553 
   7554   /* Sort symbol by name.  */
   7555   qsort (symtable1, count1, sizeof (struct elf_symbol),
   7556 	 elf_sym_name_compare);
   7557   qsort (symtable2, count1, sizeof (struct elf_symbol),
   7558 	 elf_sym_name_compare);
   7559 
   7560   for (i = 0; i < count1; i++)
   7561     /* Two symbols must have the same binding, type and name.  */
   7562     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
   7563 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
   7564 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
   7565       goto done;
   7566 
   7567   result = TRUE;
   7568 
   7569 done:
   7570   if (symtable1)
   7571     free (symtable1);
   7572   if (symtable2)
   7573     free (symtable2);
   7574   if (isymbuf1)
   7575     free (isymbuf1);
   7576   if (isymbuf2)
   7577     free (isymbuf2);
   7578 
   7579   return result;
   7580 }
   7581 
   7582 /* Return TRUE if 2 section types are compatible.  */
   7583 
   7584 bfd_boolean
   7585 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
   7586 				 bfd *bbfd, const asection *bsec)
   7587 {
   7588   if (asec == NULL
   7589       || bsec == NULL
   7590       || abfd->xvec->flavour != bfd_target_elf_flavour
   7591       || bbfd->xvec->flavour != bfd_target_elf_flavour)
   7592     return TRUE;
   7593 
   7594   return elf_section_type (asec) == elf_section_type (bsec);
   7595 }
   7596 
   7597 /* Final phase of ELF linker.  */
   7599 
   7600 /* A structure we use to avoid passing large numbers of arguments.  */
   7601 
   7602 struct elf_final_link_info
   7603 {
   7604   /* General link information.  */
   7605   struct bfd_link_info *info;
   7606   /* Output BFD.  */
   7607   bfd *output_bfd;
   7608   /* Symbol string table.  */
   7609   struct elf_strtab_hash *symstrtab;
   7610   /* .hash section.  */
   7611   asection *hash_sec;
   7612   /* symbol version section (.gnu.version).  */
   7613   asection *symver_sec;
   7614   /* Buffer large enough to hold contents of any section.  */
   7615   bfd_byte *contents;
   7616   /* Buffer large enough to hold external relocs of any section.  */
   7617   void *external_relocs;
   7618   /* Buffer large enough to hold internal relocs of any section.  */
   7619   Elf_Internal_Rela *internal_relocs;
   7620   /* Buffer large enough to hold external local symbols of any input
   7621      BFD.  */
   7622   bfd_byte *external_syms;
   7623   /* And a buffer for symbol section indices.  */
   7624   Elf_External_Sym_Shndx *locsym_shndx;
   7625   /* Buffer large enough to hold internal local symbols of any input
   7626      BFD.  */
   7627   Elf_Internal_Sym *internal_syms;
   7628   /* Array large enough to hold a symbol index for each local symbol
   7629      of any input BFD.  */
   7630   long *indices;
   7631   /* Array large enough to hold a section pointer for each local
   7632      symbol of any input BFD.  */
   7633   asection **sections;
   7634   /* Buffer for SHT_SYMTAB_SHNDX section.  */
   7635   Elf_External_Sym_Shndx *symshndxbuf;
   7636   /* Number of STT_FILE syms seen.  */
   7637   size_t filesym_count;
   7638 };
   7639 
   7640 /* This struct is used to pass information to elf_link_output_extsym.  */
   7641 
   7642 struct elf_outext_info
   7643 {
   7644   bfd_boolean failed;
   7645   bfd_boolean localsyms;
   7646   bfd_boolean file_sym_done;
   7647   struct elf_final_link_info *flinfo;
   7648 };
   7649 
   7650 
   7651 /* Support for evaluating a complex relocation.
   7652 
   7653    Complex relocations are generalized, self-describing relocations.  The
   7654    implementation of them consists of two parts: complex symbols, and the
   7655    relocations themselves.
   7656 
   7657    The relocations are use a reserved elf-wide relocation type code (R_RELC
   7658    external / BFD_RELOC_RELC internal) and an encoding of relocation field
   7659    information (start bit, end bit, word width, etc) into the addend.  This
   7660    information is extracted from CGEN-generated operand tables within gas.
   7661 
   7662    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
   7663    internal) representing prefix-notation expressions, including but not
   7664    limited to those sorts of expressions normally encoded as addends in the
   7665    addend field.  The symbol mangling format is:
   7666 
   7667    <node> := <literal>
   7668           |  <unary-operator> ':' <node>
   7669           |  <binary-operator> ':' <node> ':' <node>
   7670 	  ;
   7671 
   7672    <literal> := 's' <digits=N> ':' <N character symbol name>
   7673              |  'S' <digits=N> ':' <N character section name>
   7674 	     |  '#' <hexdigits>
   7675 	     ;
   7676 
   7677    <binary-operator> := as in C
   7678    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
   7679 
   7680 static void
   7681 set_symbol_value (bfd *bfd_with_globals,
   7682 		  Elf_Internal_Sym *isymbuf,
   7683 		  size_t locsymcount,
   7684 		  size_t symidx,
   7685 		  bfd_vma val)
   7686 {
   7687   struct elf_link_hash_entry **sym_hashes;
   7688   struct elf_link_hash_entry *h;
   7689   size_t extsymoff = locsymcount;
   7690 
   7691   if (symidx < locsymcount)
   7692     {
   7693       Elf_Internal_Sym *sym;
   7694 
   7695       sym = isymbuf + symidx;
   7696       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
   7697 	{
   7698 	  /* It is a local symbol: move it to the
   7699 	     "absolute" section and give it a value.  */
   7700 	  sym->st_shndx = SHN_ABS;
   7701 	  sym->st_value = val;
   7702 	  return;
   7703 	}
   7704       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
   7705       extsymoff = 0;
   7706     }
   7707 
   7708   /* It is a global symbol: set its link type
   7709      to "defined" and give it a value.  */
   7710 
   7711   sym_hashes = elf_sym_hashes (bfd_with_globals);
   7712   h = sym_hashes [symidx - extsymoff];
   7713   while (h->root.type == bfd_link_hash_indirect
   7714 	 || h->root.type == bfd_link_hash_warning)
   7715     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7716   h->root.type = bfd_link_hash_defined;
   7717   h->root.u.def.value = val;
   7718   h->root.u.def.section = bfd_abs_section_ptr;
   7719 }
   7720 
   7721 static bfd_boolean
   7722 resolve_symbol (const char *name,
   7723 		bfd *input_bfd,
   7724 		struct elf_final_link_info *flinfo,
   7725 		bfd_vma *result,
   7726 		Elf_Internal_Sym *isymbuf,
   7727 		size_t locsymcount)
   7728 {
   7729   Elf_Internal_Sym *sym;
   7730   struct bfd_link_hash_entry *global_entry;
   7731   const char *candidate = NULL;
   7732   Elf_Internal_Shdr *symtab_hdr;
   7733   size_t i;
   7734 
   7735   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   7736 
   7737   for (i = 0; i < locsymcount; ++ i)
   7738     {
   7739       sym = isymbuf + i;
   7740 
   7741       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
   7742 	continue;
   7743 
   7744       candidate = bfd_elf_string_from_elf_section (input_bfd,
   7745 						   symtab_hdr->sh_link,
   7746 						   sym->st_name);
   7747 #ifdef DEBUG
   7748       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
   7749 	      name, candidate, (unsigned long) sym->st_value);
   7750 #endif
   7751       if (candidate && strcmp (candidate, name) == 0)
   7752 	{
   7753 	  asection *sec = flinfo->sections [i];
   7754 
   7755 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
   7756 	  *result += sec->output_offset + sec->output_section->vma;
   7757 #ifdef DEBUG
   7758 	  printf ("Found symbol with value %8.8lx\n",
   7759 		  (unsigned long) *result);
   7760 #endif
   7761 	  return TRUE;
   7762 	}
   7763     }
   7764 
   7765   /* Hmm, haven't found it yet. perhaps it is a global.  */
   7766   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
   7767 				       FALSE, FALSE, TRUE);
   7768   if (!global_entry)
   7769     return FALSE;
   7770 
   7771   if (global_entry->type == bfd_link_hash_defined
   7772       || global_entry->type == bfd_link_hash_defweak)
   7773     {
   7774       *result = (global_entry->u.def.value
   7775 		 + global_entry->u.def.section->output_section->vma
   7776 		 + global_entry->u.def.section->output_offset);
   7777 #ifdef DEBUG
   7778       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
   7779 	      global_entry->root.string, (unsigned long) *result);
   7780 #endif
   7781       return TRUE;
   7782     }
   7783 
   7784   return FALSE;
   7785 }
   7786 
   7787 static bfd_boolean
   7788 resolve_section (const char *name,
   7789 		 asection *sections,
   7790 		 bfd_vma *result)
   7791 {
   7792   asection *curr;
   7793   unsigned int len;
   7794 
   7795   for (curr = sections; curr; curr = curr->next)
   7796     if (strcmp (curr->name, name) == 0)
   7797       {
   7798 	*result = curr->vma;
   7799 	return TRUE;
   7800       }
   7801 
   7802   /* Hmm. still haven't found it. try pseudo-section names.  */
   7803   for (curr = sections; curr; curr = curr->next)
   7804     {
   7805       len = strlen (curr->name);
   7806       if (len > strlen (name))
   7807 	continue;
   7808 
   7809       if (strncmp (curr->name, name, len) == 0)
   7810 	{
   7811 	  if (strncmp (".end", name + len, 4) == 0)
   7812 	    {
   7813 	      *result = curr->vma + curr->size;
   7814 	      return TRUE;
   7815 	    }
   7816 
   7817 	  /* Insert more pseudo-section names here, if you like.  */
   7818 	}
   7819     }
   7820 
   7821   return FALSE;
   7822 }
   7823 
   7824 static void
   7825 undefined_reference (const char *reftype, const char *name)
   7826 {
   7827   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
   7828 		      reftype, name);
   7829 }
   7830 
   7831 static bfd_boolean
   7832 eval_symbol (bfd_vma *result,
   7833 	     const char **symp,
   7834 	     bfd *input_bfd,
   7835 	     struct elf_final_link_info *flinfo,
   7836 	     bfd_vma dot,
   7837 	     Elf_Internal_Sym *isymbuf,
   7838 	     size_t locsymcount,
   7839 	     int signed_p)
   7840 {
   7841   size_t len;
   7842   size_t symlen;
   7843   bfd_vma a;
   7844   bfd_vma b;
   7845   char symbuf[4096];
   7846   const char *sym = *symp;
   7847   const char *symend;
   7848   bfd_boolean symbol_is_section = FALSE;
   7849 
   7850   len = strlen (sym);
   7851   symend = sym + len;
   7852 
   7853   if (len < 1 || len > sizeof (symbuf))
   7854     {
   7855       bfd_set_error (bfd_error_invalid_operation);
   7856       return FALSE;
   7857     }
   7858 
   7859   switch (* sym)
   7860     {
   7861     case '.':
   7862       *result = dot;
   7863       *symp = sym + 1;
   7864       return TRUE;
   7865 
   7866     case '#':
   7867       ++sym;
   7868       *result = strtoul (sym, (char **) symp, 16);
   7869       return TRUE;
   7870 
   7871     case 'S':
   7872       symbol_is_section = TRUE;
   7873     case 's':
   7874       ++sym;
   7875       symlen = strtol (sym, (char **) symp, 10);
   7876       sym = *symp + 1; /* Skip the trailing ':'.  */
   7877 
   7878       if (symend < sym || symlen + 1 > sizeof (symbuf))
   7879 	{
   7880 	  bfd_set_error (bfd_error_invalid_operation);
   7881 	  return FALSE;
   7882 	}
   7883 
   7884       memcpy (symbuf, sym, symlen);
   7885       symbuf[symlen] = '\0';
   7886       *symp = sym + symlen;
   7887 
   7888       /* Is it always possible, with complex symbols, that gas "mis-guessed"
   7889 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
   7890 	 interpretation here; section means "try section first", not "must be a
   7891 	 section", and likewise with symbol.  */
   7892 
   7893       if (symbol_is_section)
   7894 	{
   7895 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
   7896 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
   7897 				  isymbuf, locsymcount))
   7898 	    {
   7899 	      undefined_reference ("section", symbuf);
   7900 	      return FALSE;
   7901 	    }
   7902 	}
   7903       else
   7904 	{
   7905 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
   7906 			       isymbuf, locsymcount)
   7907 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
   7908 				   result))
   7909 	    {
   7910 	      undefined_reference ("symbol", symbuf);
   7911 	      return FALSE;
   7912 	    }
   7913 	}
   7914 
   7915       return TRUE;
   7916 
   7917       /* All that remains are operators.  */
   7918 
   7919 #define UNARY_OP(op)						\
   7920   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   7921     {								\
   7922       sym += strlen (#op);					\
   7923       if (*sym == ':')						\
   7924 	++sym;							\
   7925       *symp = sym;						\
   7926       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   7927 			isymbuf, locsymcount, signed_p))	\
   7928 	return FALSE;						\
   7929       if (signed_p)						\
   7930 	*result = op ((bfd_signed_vma) a);			\
   7931       else							\
   7932 	*result = op a;						\
   7933       return TRUE;						\
   7934     }
   7935 
   7936 #define BINARY_OP(op)						\
   7937   if (strncmp (sym, #op, strlen (#op)) == 0)			\
   7938     {								\
   7939       sym += strlen (#op);					\
   7940       if (*sym == ':')						\
   7941 	++sym;							\
   7942       *symp = sym;						\
   7943       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
   7944 			isymbuf, locsymcount, signed_p))	\
   7945 	return FALSE;						\
   7946       ++*symp;							\
   7947       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
   7948 			isymbuf, locsymcount, signed_p))	\
   7949 	return FALSE;						\
   7950       if (signed_p)						\
   7951 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
   7952       else							\
   7953 	*result = a op b;					\
   7954       return TRUE;						\
   7955     }
   7956 
   7957     default:
   7958       UNARY_OP  (0-);
   7959       BINARY_OP (<<);
   7960       BINARY_OP (>>);
   7961       BINARY_OP (==);
   7962       BINARY_OP (!=);
   7963       BINARY_OP (<=);
   7964       BINARY_OP (>=);
   7965       BINARY_OP (&&);
   7966       BINARY_OP (||);
   7967       UNARY_OP  (~);
   7968       UNARY_OP  (!);
   7969       BINARY_OP (*);
   7970       BINARY_OP (/);
   7971       BINARY_OP (%);
   7972       BINARY_OP (^);
   7973       BINARY_OP (|);
   7974       BINARY_OP (&);
   7975       BINARY_OP (+);
   7976       BINARY_OP (-);
   7977       BINARY_OP (<);
   7978       BINARY_OP (>);
   7979 #undef UNARY_OP
   7980 #undef BINARY_OP
   7981       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
   7982       bfd_set_error (bfd_error_invalid_operation);
   7983       return FALSE;
   7984     }
   7985 }
   7986 
   7987 static void
   7988 put_value (bfd_vma size,
   7989 	   unsigned long chunksz,
   7990 	   bfd *input_bfd,
   7991 	   bfd_vma x,
   7992 	   bfd_byte *location)
   7993 {
   7994   location += (size - chunksz);
   7995 
   7996   for (; size; size -= chunksz, location -= chunksz)
   7997     {
   7998       switch (chunksz)
   7999 	{
   8000 	case 1:
   8001 	  bfd_put_8 (input_bfd, x, location);
   8002 	  x >>= 8;
   8003 	  break;
   8004 	case 2:
   8005 	  bfd_put_16 (input_bfd, x, location);
   8006 	  x >>= 16;
   8007 	  break;
   8008 	case 4:
   8009 	  bfd_put_32 (input_bfd, x, location);
   8010 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
   8011 	  x >>= 16;
   8012 	  x >>= 16;
   8013 	  break;
   8014 #ifdef BFD64
   8015 	case 8:
   8016 	  bfd_put_64 (input_bfd, x, location);
   8017 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
   8018 	  x >>= 32;
   8019 	  x >>= 32;
   8020 	  break;
   8021 #endif
   8022 	default:
   8023 	  abort ();
   8024 	  break;
   8025 	}
   8026     }
   8027 }
   8028 
   8029 static bfd_vma
   8030 get_value (bfd_vma size,
   8031 	   unsigned long chunksz,
   8032 	   bfd *input_bfd,
   8033 	   bfd_byte *location)
   8034 {
   8035   int shift;
   8036   bfd_vma x = 0;
   8037 
   8038   /* Sanity checks.  */
   8039   BFD_ASSERT (chunksz <= sizeof (x)
   8040 	      && size >= chunksz
   8041 	      && chunksz != 0
   8042 	      && (size % chunksz) == 0
   8043 	      && input_bfd != NULL
   8044 	      && location != NULL);
   8045 
   8046   if (chunksz == sizeof (x))
   8047     {
   8048       BFD_ASSERT (size == chunksz);
   8049 
   8050       /* Make sure that we do not perform an undefined shift operation.
   8051 	 We know that size == chunksz so there will only be one iteration
   8052 	 of the loop below.  */
   8053       shift = 0;
   8054     }
   8055   else
   8056     shift = 8 * chunksz;
   8057 
   8058   for (; size; size -= chunksz, location += chunksz)
   8059     {
   8060       switch (chunksz)
   8061 	{
   8062 	case 1:
   8063 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
   8064 	  break;
   8065 	case 2:
   8066 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
   8067 	  break;
   8068 	case 4:
   8069 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
   8070 	  break;
   8071 #ifdef BFD64
   8072 	case 8:
   8073 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
   8074 	  break;
   8075 #endif
   8076 	default:
   8077 	  abort ();
   8078 	}
   8079     }
   8080   return x;
   8081 }
   8082 
   8083 static void
   8084 decode_complex_addend (unsigned long *start,   /* in bits */
   8085 		       unsigned long *oplen,   /* in bits */
   8086 		       unsigned long *len,     /* in bits */
   8087 		       unsigned long *wordsz,  /* in bytes */
   8088 		       unsigned long *chunksz, /* in bytes */
   8089 		       unsigned long *lsb0_p,
   8090 		       unsigned long *signed_p,
   8091 		       unsigned long *trunc_p,
   8092 		       unsigned long encoded)
   8093 {
   8094   * start     =  encoded        & 0x3F;
   8095   * len       = (encoded >>  6) & 0x3F;
   8096   * oplen     = (encoded >> 12) & 0x3F;
   8097   * wordsz    = (encoded >> 18) & 0xF;
   8098   * chunksz   = (encoded >> 22) & 0xF;
   8099   * lsb0_p    = (encoded >> 27) & 1;
   8100   * signed_p  = (encoded >> 28) & 1;
   8101   * trunc_p   = (encoded >> 29) & 1;
   8102 }
   8103 
   8104 bfd_reloc_status_type
   8105 bfd_elf_perform_complex_relocation (bfd *input_bfd,
   8106 				    asection *input_section ATTRIBUTE_UNUSED,
   8107 				    bfd_byte *contents,
   8108 				    Elf_Internal_Rela *rel,
   8109 				    bfd_vma relocation)
   8110 {
   8111   bfd_vma shift, x, mask;
   8112   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
   8113   bfd_reloc_status_type r;
   8114 
   8115   /*  Perform this reloc, since it is complex.
   8116       (this is not to say that it necessarily refers to a complex
   8117       symbol; merely that it is a self-describing CGEN based reloc.
   8118       i.e. the addend has the complete reloc information (bit start, end,
   8119       word size, etc) encoded within it.).  */
   8120 
   8121   decode_complex_addend (&start, &oplen, &len, &wordsz,
   8122 			 &chunksz, &lsb0_p, &signed_p,
   8123 			 &trunc_p, rel->r_addend);
   8124 
   8125   mask = (((1L << (len - 1)) - 1) << 1) | 1;
   8126 
   8127   if (lsb0_p)
   8128     shift = (start + 1) - len;
   8129   else
   8130     shift = (8 * wordsz) - (start + len);
   8131 
   8132   /* FIXME: octets_per_byte.  */
   8133   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
   8134 
   8135 #ifdef DEBUG
   8136   printf ("Doing complex reloc: "
   8137 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
   8138 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
   8139 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
   8140 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
   8141 	  oplen, (unsigned long) x, (unsigned long) mask,
   8142 	  (unsigned long) relocation);
   8143 #endif
   8144 
   8145   r = bfd_reloc_ok;
   8146   if (! trunc_p)
   8147     /* Now do an overflow check.  */
   8148     r = bfd_check_overflow ((signed_p
   8149 			     ? complain_overflow_signed
   8150 			     : complain_overflow_unsigned),
   8151 			    len, 0, (8 * wordsz),
   8152 			    relocation);
   8153 
   8154   /* Do the deed.  */
   8155   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
   8156 
   8157 #ifdef DEBUG
   8158   printf ("           relocation: %8.8lx\n"
   8159 	  "         shifted mask: %8.8lx\n"
   8160 	  " shifted/masked reloc: %8.8lx\n"
   8161 	  "               result: %8.8lx\n",
   8162 	  (unsigned long) relocation, (unsigned long) (mask << shift),
   8163 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
   8164 #endif
   8165   /* FIXME: octets_per_byte.  */
   8166   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
   8167   return r;
   8168 }
   8169 
   8170 /* Functions to read r_offset from external (target order) reloc
   8171    entry.  Faster than bfd_getl32 et al, because we let the compiler
   8172    know the value is aligned.  */
   8173 
   8174 static bfd_vma
   8175 ext32l_r_offset (const void *p)
   8176 {
   8177   union aligned32
   8178   {
   8179     uint32_t v;
   8180     unsigned char c[4];
   8181   };
   8182   const union aligned32 *a
   8183     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8184 
   8185   uint32_t aval = (  (uint32_t) a->c[0]
   8186 		   | (uint32_t) a->c[1] << 8
   8187 		   | (uint32_t) a->c[2] << 16
   8188 		   | (uint32_t) a->c[3] << 24);
   8189   return aval;
   8190 }
   8191 
   8192 static bfd_vma
   8193 ext32b_r_offset (const void *p)
   8194 {
   8195   union aligned32
   8196   {
   8197     uint32_t v;
   8198     unsigned char c[4];
   8199   };
   8200   const union aligned32 *a
   8201     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
   8202 
   8203   uint32_t aval = (  (uint32_t) a->c[0] << 24
   8204 		   | (uint32_t) a->c[1] << 16
   8205 		   | (uint32_t) a->c[2] << 8
   8206 		   | (uint32_t) a->c[3]);
   8207   return aval;
   8208 }
   8209 
   8210 #ifdef BFD_HOST_64_BIT
   8211 static bfd_vma
   8212 ext64l_r_offset (const void *p)
   8213 {
   8214   union aligned64
   8215   {
   8216     uint64_t v;
   8217     unsigned char c[8];
   8218   };
   8219   const union aligned64 *a
   8220     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8221 
   8222   uint64_t aval = (  (uint64_t) a->c[0]
   8223 		   | (uint64_t) a->c[1] << 8
   8224 		   | (uint64_t) a->c[2] << 16
   8225 		   | (uint64_t) a->c[3] << 24
   8226 		   | (uint64_t) a->c[4] << 32
   8227 		   | (uint64_t) a->c[5] << 40
   8228 		   | (uint64_t) a->c[6] << 48
   8229 		   | (uint64_t) a->c[7] << 56);
   8230   return aval;
   8231 }
   8232 
   8233 static bfd_vma
   8234 ext64b_r_offset (const void *p)
   8235 {
   8236   union aligned64
   8237   {
   8238     uint64_t v;
   8239     unsigned char c[8];
   8240   };
   8241   const union aligned64 *a
   8242     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
   8243 
   8244   uint64_t aval = (  (uint64_t) a->c[0] << 56
   8245 		   | (uint64_t) a->c[1] << 48
   8246 		   | (uint64_t) a->c[2] << 40
   8247 		   | (uint64_t) a->c[3] << 32
   8248 		   | (uint64_t) a->c[4] << 24
   8249 		   | (uint64_t) a->c[5] << 16
   8250 		   | (uint64_t) a->c[6] << 8
   8251 		   | (uint64_t) a->c[7]);
   8252   return aval;
   8253 }
   8254 #endif
   8255 
   8256 /* When performing a relocatable link, the input relocations are
   8257    preserved.  But, if they reference global symbols, the indices
   8258    referenced must be updated.  Update all the relocations found in
   8259    RELDATA.  */
   8260 
   8261 static bfd_boolean
   8262 elf_link_adjust_relocs (bfd *abfd,
   8263 			struct bfd_elf_section_reloc_data *reldata,
   8264 			bfd_boolean sort)
   8265 {
   8266   unsigned int i;
   8267   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8268   bfd_byte *erela;
   8269   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8270   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8271   bfd_vma r_type_mask;
   8272   int r_sym_shift;
   8273   unsigned int count = reldata->count;
   8274   struct elf_link_hash_entry **rel_hash = reldata->hashes;
   8275 
   8276   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
   8277     {
   8278       swap_in = bed->s->swap_reloc_in;
   8279       swap_out = bed->s->swap_reloc_out;
   8280     }
   8281   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
   8282     {
   8283       swap_in = bed->s->swap_reloca_in;
   8284       swap_out = bed->s->swap_reloca_out;
   8285     }
   8286   else
   8287     abort ();
   8288 
   8289   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
   8290     abort ();
   8291 
   8292   if (bed->s->arch_size == 32)
   8293     {
   8294       r_type_mask = 0xff;
   8295       r_sym_shift = 8;
   8296     }
   8297   else
   8298     {
   8299       r_type_mask = 0xffffffff;
   8300       r_sym_shift = 32;
   8301     }
   8302 
   8303   erela = reldata->hdr->contents;
   8304   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
   8305     {
   8306       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
   8307       unsigned int j;
   8308 
   8309       if (*rel_hash == NULL)
   8310 	continue;
   8311 
   8312       BFD_ASSERT ((*rel_hash)->indx >= 0);
   8313 
   8314       (*swap_in) (abfd, erela, irela);
   8315       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
   8316 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
   8317 			   | (irela[j].r_info & r_type_mask));
   8318       (*swap_out) (abfd, irela, erela);
   8319     }
   8320 
   8321   if (sort && count != 0)
   8322     {
   8323       bfd_vma (*ext_r_off) (const void *);
   8324       bfd_vma r_off;
   8325       size_t elt_size;
   8326       bfd_byte *base, *end, *p, *loc;
   8327       bfd_byte *buf = NULL;
   8328 
   8329       if (bed->s->arch_size == 32)
   8330 	{
   8331 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   8332 	    ext_r_off = ext32l_r_offset;
   8333 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   8334 	    ext_r_off = ext32b_r_offset;
   8335 	  else
   8336 	    abort ();
   8337 	}
   8338       else
   8339 	{
   8340 #ifdef BFD_HOST_64_BIT
   8341 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
   8342 	    ext_r_off = ext64l_r_offset;
   8343 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
   8344 	    ext_r_off = ext64b_r_offset;
   8345 	  else
   8346 #endif
   8347 	    abort ();
   8348 	}
   8349 
   8350       /*  Must use a stable sort here.  A modified insertion sort,
   8351 	  since the relocs are mostly sorted already.  */
   8352       elt_size = reldata->hdr->sh_entsize;
   8353       base = reldata->hdr->contents;
   8354       end = base + count * elt_size;
   8355       if (elt_size > sizeof (Elf64_External_Rela))
   8356 	abort ();
   8357 
   8358       /* Ensure the first element is lowest.  This acts as a sentinel,
   8359 	 speeding the main loop below.  */
   8360       r_off = (*ext_r_off) (base);
   8361       for (p = loc = base; (p += elt_size) < end; )
   8362 	{
   8363 	  bfd_vma r_off2 = (*ext_r_off) (p);
   8364 	  if (r_off > r_off2)
   8365 	    {
   8366 	      r_off = r_off2;
   8367 	      loc = p;
   8368 	    }
   8369 	}
   8370       if (loc != base)
   8371 	{
   8372 	  /* Don't just swap *base and *loc as that changes the order
   8373 	     of the original base[0] and base[1] if they happen to
   8374 	     have the same r_offset.  */
   8375 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
   8376 	  memcpy (onebuf, loc, elt_size);
   8377 	  memmove (base + elt_size, base, loc - base);
   8378 	  memcpy (base, onebuf, elt_size);
   8379 	}
   8380 
   8381       for (p = base + elt_size; (p += elt_size) < end; )
   8382 	{
   8383 	  /* base to p is sorted, *p is next to insert.  */
   8384 	  r_off = (*ext_r_off) (p);
   8385 	  /* Search the sorted region for location to insert.  */
   8386 	  loc = p - elt_size;
   8387 	  while (r_off < (*ext_r_off) (loc))
   8388 	    loc -= elt_size;
   8389 	  loc += elt_size;
   8390 	  if (loc != p)
   8391 	    {
   8392 	      /* Chances are there is a run of relocs to insert here,
   8393 		 from one of more input files.  Files are not always
   8394 		 linked in order due to the way elf_link_input_bfd is
   8395 		 called.  See pr17666.  */
   8396 	      size_t sortlen = p - loc;
   8397 	      bfd_vma r_off2 = (*ext_r_off) (loc);
   8398 	      size_t runlen = elt_size;
   8399 	      size_t buf_size = 96 * 1024;
   8400 	      while (p + runlen < end
   8401 		     && (sortlen <= buf_size
   8402 			 || runlen + elt_size <= buf_size)
   8403 		     && r_off2 > (*ext_r_off) (p + runlen))
   8404 		runlen += elt_size;
   8405 	      if (buf == NULL)
   8406 		{
   8407 		  buf = bfd_malloc (buf_size);
   8408 		  if (buf == NULL)
   8409 		    return FALSE;
   8410 		}
   8411 	      if (runlen < sortlen)
   8412 		{
   8413 		  memcpy (buf, p, runlen);
   8414 		  memmove (loc + runlen, loc, sortlen);
   8415 		  memcpy (loc, buf, runlen);
   8416 		}
   8417 	      else
   8418 		{
   8419 		  memcpy (buf, loc, sortlen);
   8420 		  memmove (loc, p, runlen);
   8421 		  memcpy (loc + runlen, buf, sortlen);
   8422 		}
   8423 	      p += runlen - elt_size;
   8424 	    }
   8425 	}
   8426       /* Hashes are no longer valid.  */
   8427       free (reldata->hashes);
   8428       reldata->hashes = NULL;
   8429       free (buf);
   8430     }
   8431   return TRUE;
   8432 }
   8433 
   8434 struct elf_link_sort_rela
   8435 {
   8436   union {
   8437     bfd_vma offset;
   8438     bfd_vma sym_mask;
   8439   } u;
   8440   enum elf_reloc_type_class type;
   8441   /* We use this as an array of size int_rels_per_ext_rel.  */
   8442   Elf_Internal_Rela rela[1];
   8443 };
   8444 
   8445 static int
   8446 elf_link_sort_cmp1 (const void *A, const void *B)
   8447 {
   8448   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8449   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8450   int relativea, relativeb;
   8451 
   8452   relativea = a->type == reloc_class_relative;
   8453   relativeb = b->type == reloc_class_relative;
   8454 
   8455   if (relativea < relativeb)
   8456     return 1;
   8457   if (relativea > relativeb)
   8458     return -1;
   8459   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
   8460     return -1;
   8461   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
   8462     return 1;
   8463   if (a->rela->r_offset < b->rela->r_offset)
   8464     return -1;
   8465   if (a->rela->r_offset > b->rela->r_offset)
   8466     return 1;
   8467   return 0;
   8468 }
   8469 
   8470 static int
   8471 elf_link_sort_cmp2 (const void *A, const void *B)
   8472 {
   8473   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
   8474   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
   8475 
   8476   if (a->type < b->type)
   8477     return -1;
   8478   if (a->type > b->type)
   8479     return 1;
   8480   if (a->u.offset < b->u.offset)
   8481     return -1;
   8482   if (a->u.offset > b->u.offset)
   8483     return 1;
   8484   if (a->rela->r_offset < b->rela->r_offset)
   8485     return -1;
   8486   if (a->rela->r_offset > b->rela->r_offset)
   8487     return 1;
   8488   return 0;
   8489 }
   8490 
   8491 static size_t
   8492 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
   8493 {
   8494   asection *dynamic_relocs;
   8495   asection *rela_dyn;
   8496   asection *rel_dyn;
   8497   bfd_size_type count, size;
   8498   size_t i, ret, sort_elt, ext_size;
   8499   bfd_byte *sort, *s_non_relative, *p;
   8500   struct elf_link_sort_rela *sq;
   8501   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8502   int i2e = bed->s->int_rels_per_ext_rel;
   8503   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
   8504   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
   8505   struct bfd_link_order *lo;
   8506   bfd_vma r_sym_mask;
   8507   bfd_boolean use_rela;
   8508 
   8509   /* Find a dynamic reloc section.  */
   8510   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
   8511   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
   8512   if (rela_dyn != NULL && rela_dyn->size > 0
   8513       && rel_dyn != NULL && rel_dyn->size > 0)
   8514     {
   8515       bfd_boolean use_rela_initialised = FALSE;
   8516 
   8517       /* This is just here to stop gcc from complaining.
   8518 	 It's initialization checking code is not perfect.  */
   8519       use_rela = TRUE;
   8520 
   8521       /* Both sections are present.  Examine the sizes
   8522 	 of the indirect sections to help us choose.  */
   8523       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   8524 	if (lo->type == bfd_indirect_link_order)
   8525 	  {
   8526 	    asection *o = lo->u.indirect.section;
   8527 
   8528 	    if ((o->size % bed->s->sizeof_rela) == 0)
   8529 	      {
   8530 		if ((o->size % bed->s->sizeof_rel) == 0)
   8531 		  /* Section size is divisible by both rel and rela sizes.
   8532 		     It is of no help to us.  */
   8533 		  ;
   8534 		else
   8535 		  {
   8536 		    /* Section size is only divisible by rela.  */
   8537 		    if (use_rela_initialised && (use_rela == FALSE))
   8538 		      {
   8539 			_bfd_error_handler
   8540 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8541 			bfd_set_error (bfd_error_invalid_operation);
   8542 			return 0;
   8543 		      }
   8544 		    else
   8545 		      {
   8546 			use_rela = TRUE;
   8547 			use_rela_initialised = TRUE;
   8548 		      }
   8549 		  }
   8550 	      }
   8551 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   8552 	      {
   8553 		/* Section size is only divisible by rel.  */
   8554 		if (use_rela_initialised && (use_rela == TRUE))
   8555 		  {
   8556 		    _bfd_error_handler
   8557 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8558 		    bfd_set_error (bfd_error_invalid_operation);
   8559 		    return 0;
   8560 		  }
   8561 		else
   8562 		  {
   8563 		    use_rela = FALSE;
   8564 		    use_rela_initialised = TRUE;
   8565 		  }
   8566 	      }
   8567 	    else
   8568 	      {
   8569 		/* The section size is not divisible by either - something is wrong.  */
   8570 		_bfd_error_handler
   8571 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
   8572 		bfd_set_error (bfd_error_invalid_operation);
   8573 		return 0;
   8574 	      }
   8575 	  }
   8576 
   8577       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
   8578 	if (lo->type == bfd_indirect_link_order)
   8579 	  {
   8580 	    asection *o = lo->u.indirect.section;
   8581 
   8582 	    if ((o->size % bed->s->sizeof_rela) == 0)
   8583 	      {
   8584 		if ((o->size % bed->s->sizeof_rel) == 0)
   8585 		  /* Section size is divisible by both rel and rela sizes.
   8586 		     It is of no help to us.  */
   8587 		  ;
   8588 		else
   8589 		  {
   8590 		    /* Section size is only divisible by rela.  */
   8591 		    if (use_rela_initialised && (use_rela == FALSE))
   8592 		      {
   8593 			_bfd_error_handler
   8594 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8595 			bfd_set_error (bfd_error_invalid_operation);
   8596 			return 0;
   8597 		      }
   8598 		    else
   8599 		      {
   8600 			use_rela = TRUE;
   8601 			use_rela_initialised = TRUE;
   8602 		      }
   8603 		  }
   8604 	      }
   8605 	    else if ((o->size % bed->s->sizeof_rel) == 0)
   8606 	      {
   8607 		/* Section size is only divisible by rel.  */
   8608 		if (use_rela_initialised && (use_rela == TRUE))
   8609 		  {
   8610 		    _bfd_error_handler
   8611 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
   8612 		    bfd_set_error (bfd_error_invalid_operation);
   8613 		    return 0;
   8614 		  }
   8615 		else
   8616 		  {
   8617 		    use_rela = FALSE;
   8618 		    use_rela_initialised = TRUE;
   8619 		  }
   8620 	      }
   8621 	    else
   8622 	      {
   8623 		/* The section size is not divisible by either - something is wrong.  */
   8624 		_bfd_error_handler
   8625 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
   8626 		bfd_set_error (bfd_error_invalid_operation);
   8627 		return 0;
   8628 	      }
   8629 	  }
   8630 
   8631       if (! use_rela_initialised)
   8632 	/* Make a guess.  */
   8633 	use_rela = TRUE;
   8634     }
   8635   else if (rela_dyn != NULL && rela_dyn->size > 0)
   8636     use_rela = TRUE;
   8637   else if (rel_dyn != NULL && rel_dyn->size > 0)
   8638     use_rela = FALSE;
   8639   else
   8640     return 0;
   8641 
   8642   if (use_rela)
   8643     {
   8644       dynamic_relocs = rela_dyn;
   8645       ext_size = bed->s->sizeof_rela;
   8646       swap_in = bed->s->swap_reloca_in;
   8647       swap_out = bed->s->swap_reloca_out;
   8648     }
   8649   else
   8650     {
   8651       dynamic_relocs = rel_dyn;
   8652       ext_size = bed->s->sizeof_rel;
   8653       swap_in = bed->s->swap_reloc_in;
   8654       swap_out = bed->s->swap_reloc_out;
   8655     }
   8656 
   8657   size = 0;
   8658   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8659     if (lo->type == bfd_indirect_link_order)
   8660       size += lo->u.indirect.section->size;
   8661 
   8662   if (size != dynamic_relocs->size)
   8663     return 0;
   8664 
   8665   sort_elt = (sizeof (struct elf_link_sort_rela)
   8666 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
   8667 
   8668   count = dynamic_relocs->size / ext_size;
   8669   if (count == 0)
   8670     return 0;
   8671   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
   8672 
   8673   if (sort == NULL)
   8674     {
   8675       (*info->callbacks->warning)
   8676 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
   8677       return 0;
   8678     }
   8679 
   8680   if (bed->s->arch_size == 32)
   8681     r_sym_mask = ~(bfd_vma) 0xff;
   8682   else
   8683     r_sym_mask = ~(bfd_vma) 0xffffffff;
   8684 
   8685   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8686     if (lo->type == bfd_indirect_link_order)
   8687       {
   8688 	bfd_byte *erel, *erelend;
   8689 	asection *o = lo->u.indirect.section;
   8690 
   8691 	if (o->contents == NULL && o->size != 0)
   8692 	  {
   8693 	    /* This is a reloc section that is being handled as a normal
   8694 	       section.  See bfd_section_from_shdr.  We can't combine
   8695 	       relocs in this case.  */
   8696 	    free (sort);
   8697 	    return 0;
   8698 	  }
   8699 	erel = o->contents;
   8700 	erelend = o->contents + o->size;
   8701 	/* FIXME: octets_per_byte.  */
   8702 	p = sort + o->output_offset / ext_size * sort_elt;
   8703 
   8704 	while (erel < erelend)
   8705 	  {
   8706 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8707 
   8708 	    (*swap_in) (abfd, erel, s->rela);
   8709 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
   8710 	    s->u.sym_mask = r_sym_mask;
   8711 	    p += sort_elt;
   8712 	    erel += ext_size;
   8713 	  }
   8714       }
   8715 
   8716   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
   8717 
   8718   for (i = 0, p = sort; i < count; i++, p += sort_elt)
   8719     {
   8720       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8721       if (s->type != reloc_class_relative)
   8722 	break;
   8723     }
   8724   ret = i;
   8725   s_non_relative = p;
   8726 
   8727   sq = (struct elf_link_sort_rela *) s_non_relative;
   8728   for (; i < count; i++, p += sort_elt)
   8729     {
   8730       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
   8731       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
   8732 	sq = sp;
   8733       sp->u.offset = sq->rela->r_offset;
   8734     }
   8735 
   8736   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
   8737 
   8738   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
   8739     if (lo->type == bfd_indirect_link_order)
   8740       {
   8741 	bfd_byte *erel, *erelend;
   8742 	asection *o = lo->u.indirect.section;
   8743 
   8744 	erel = o->contents;
   8745 	erelend = o->contents + o->size;
   8746 	/* FIXME: octets_per_byte.  */
   8747 	p = sort + o->output_offset / ext_size * sort_elt;
   8748 	while (erel < erelend)
   8749 	  {
   8750 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
   8751 	    (*swap_out) (abfd, s->rela, erel);
   8752 	    p += sort_elt;
   8753 	    erel += ext_size;
   8754 	  }
   8755       }
   8756 
   8757   free (sort);
   8758   *psec = dynamic_relocs;
   8759   return ret;
   8760 }
   8761 
   8762 /* Add a symbol to the output symbol string table.  */
   8763 
   8764 static int
   8765 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
   8766 			   const char *name,
   8767 			   Elf_Internal_Sym *elfsym,
   8768 			   asection *input_sec,
   8769 			   struct elf_link_hash_entry *h)
   8770 {
   8771   int (*output_symbol_hook)
   8772     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
   8773      struct elf_link_hash_entry *);
   8774   struct elf_link_hash_table *hash_table;
   8775   const struct elf_backend_data *bed;
   8776   bfd_size_type strtabsize;
   8777 
   8778   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   8779 
   8780   bed = get_elf_backend_data (flinfo->output_bfd);
   8781   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
   8782   if (output_symbol_hook != NULL)
   8783     {
   8784       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
   8785       if (ret != 1)
   8786 	return ret;
   8787     }
   8788 
   8789   if (name == NULL
   8790       || *name == '\0'
   8791       || (input_sec->flags & SEC_EXCLUDE))
   8792     elfsym->st_name = (unsigned long) -1;
   8793   else
   8794     {
   8795       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8796 	 to get the final offset for st_name.  */
   8797       elfsym->st_name
   8798 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
   8799 					       name, FALSE);
   8800       if (elfsym->st_name == (unsigned long) -1)
   8801 	return 0;
   8802     }
   8803 
   8804   hash_table = elf_hash_table (flinfo->info);
   8805   strtabsize = hash_table->strtabsize;
   8806   if (strtabsize <= hash_table->strtabcount)
   8807     {
   8808       strtabsize += strtabsize;
   8809       hash_table->strtabsize = strtabsize;
   8810       strtabsize *= sizeof (*hash_table->strtab);
   8811       hash_table->strtab
   8812 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
   8813 						 strtabsize);
   8814       if (hash_table->strtab == NULL)
   8815 	return 0;
   8816     }
   8817   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
   8818   hash_table->strtab[hash_table->strtabcount].dest_index
   8819     = hash_table->strtabcount;
   8820   hash_table->strtab[hash_table->strtabcount].destshndx_index
   8821     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
   8822 
   8823   bfd_get_symcount (flinfo->output_bfd) += 1;
   8824   hash_table->strtabcount += 1;
   8825 
   8826   return 1;
   8827 }
   8828 
   8829 /* Swap symbols out to the symbol table and flush the output symbols to
   8830    the file.  */
   8831 
   8832 static bfd_boolean
   8833 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
   8834 {
   8835   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
   8836   bfd_size_type amt, i;
   8837   const struct elf_backend_data *bed;
   8838   bfd_byte *symbuf;
   8839   Elf_Internal_Shdr *hdr;
   8840   file_ptr pos;
   8841   bfd_boolean ret;
   8842 
   8843   if (!hash_table->strtabcount)
   8844     return TRUE;
   8845 
   8846   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
   8847 
   8848   bed = get_elf_backend_data (flinfo->output_bfd);
   8849 
   8850   amt = bed->s->sizeof_sym * hash_table->strtabcount;
   8851   symbuf = (bfd_byte *) bfd_malloc (amt);
   8852   if (symbuf == NULL)
   8853     return FALSE;
   8854 
   8855   if (flinfo->symshndxbuf)
   8856     {
   8857       amt = (sizeof (Elf_External_Sym_Shndx)
   8858 	     * (bfd_get_symcount (flinfo->output_bfd)));
   8859       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
   8860       if (flinfo->symshndxbuf == NULL)
   8861 	{
   8862 	  free (symbuf);
   8863 	  return FALSE;
   8864 	}
   8865     }
   8866 
   8867   for (i = 0; i < hash_table->strtabcount; i++)
   8868     {
   8869       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
   8870       if (elfsym->sym.st_name == (unsigned long) -1)
   8871 	elfsym->sym.st_name = 0;
   8872       else
   8873 	elfsym->sym.st_name
   8874 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
   8875 						    elfsym->sym.st_name);
   8876       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
   8877 			       ((bfd_byte *) symbuf
   8878 				+ (elfsym->dest_index
   8879 				   * bed->s->sizeof_sym)),
   8880 			       (flinfo->symshndxbuf
   8881 				+ elfsym->destshndx_index));
   8882     }
   8883 
   8884   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   8885   pos = hdr->sh_offset + hdr->sh_size;
   8886   amt = hash_table->strtabcount * bed->s->sizeof_sym;
   8887   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
   8888       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
   8889     {
   8890       hdr->sh_size += amt;
   8891       ret = TRUE;
   8892     }
   8893   else
   8894     ret = FALSE;
   8895 
   8896   free (symbuf);
   8897 
   8898   free (hash_table->strtab);
   8899   hash_table->strtab = NULL;
   8900 
   8901   return ret;
   8902 }
   8903 
   8904 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
   8905 
   8906 static bfd_boolean
   8907 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
   8908 {
   8909   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
   8910       && sym->st_shndx < SHN_LORESERVE)
   8911     {
   8912       /* The gABI doesn't support dynamic symbols in output sections
   8913 	 beyond 64k.  */
   8914       (*_bfd_error_handler)
   8915 	(_("%B: Too many sections: %d (>= %d)"),
   8916 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
   8917       bfd_set_error (bfd_error_nonrepresentable_section);
   8918       return FALSE;
   8919     }
   8920   return TRUE;
   8921 }
   8922 
   8923 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
   8924    allowing an unsatisfied unversioned symbol in the DSO to match a
   8925    versioned symbol that would normally require an explicit version.
   8926    We also handle the case that a DSO references a hidden symbol
   8927    which may be satisfied by a versioned symbol in another DSO.  */
   8928 
   8929 static bfd_boolean
   8930 elf_link_check_versioned_symbol (struct bfd_link_info *info,
   8931 				 const struct elf_backend_data *bed,
   8932 				 struct elf_link_hash_entry *h)
   8933 {
   8934   bfd *abfd;
   8935   struct elf_link_loaded_list *loaded;
   8936 
   8937   if (!is_elf_hash_table (info->hash))
   8938     return FALSE;
   8939 
   8940   /* Check indirect symbol.  */
   8941   while (h->root.type == bfd_link_hash_indirect)
   8942     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8943 
   8944   switch (h->root.type)
   8945     {
   8946     default:
   8947       abfd = NULL;
   8948       break;
   8949 
   8950     case bfd_link_hash_undefined:
   8951     case bfd_link_hash_undefweak:
   8952       abfd = h->root.u.undef.abfd;
   8953       if ((abfd->flags & DYNAMIC) == 0
   8954 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
   8955 	return FALSE;
   8956       break;
   8957 
   8958     case bfd_link_hash_defined:
   8959     case bfd_link_hash_defweak:
   8960       abfd = h->root.u.def.section->owner;
   8961       break;
   8962 
   8963     case bfd_link_hash_common:
   8964       abfd = h->root.u.c.p->section->owner;
   8965       break;
   8966     }
   8967   BFD_ASSERT (abfd != NULL);
   8968 
   8969   for (loaded = elf_hash_table (info)->loaded;
   8970        loaded != NULL;
   8971        loaded = loaded->next)
   8972     {
   8973       bfd *input;
   8974       Elf_Internal_Shdr *hdr;
   8975       bfd_size_type symcount;
   8976       bfd_size_type extsymcount;
   8977       bfd_size_type extsymoff;
   8978       Elf_Internal_Shdr *versymhdr;
   8979       Elf_Internal_Sym *isym;
   8980       Elf_Internal_Sym *isymend;
   8981       Elf_Internal_Sym *isymbuf;
   8982       Elf_External_Versym *ever;
   8983       Elf_External_Versym *extversym;
   8984 
   8985       input = loaded->abfd;
   8986 
   8987       /* We check each DSO for a possible hidden versioned definition.  */
   8988       if (input == abfd
   8989 	  || (input->flags & DYNAMIC) == 0
   8990 	  || elf_dynversym (input) == 0)
   8991 	continue;
   8992 
   8993       hdr = &elf_tdata (input)->dynsymtab_hdr;
   8994 
   8995       symcount = hdr->sh_size / bed->s->sizeof_sym;
   8996       if (elf_bad_symtab (input))
   8997 	{
   8998 	  extsymcount = symcount;
   8999 	  extsymoff = 0;
   9000 	}
   9001       else
   9002 	{
   9003 	  extsymcount = symcount - hdr->sh_info;
   9004 	  extsymoff = hdr->sh_info;
   9005 	}
   9006 
   9007       if (extsymcount == 0)
   9008 	continue;
   9009 
   9010       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
   9011 				      NULL, NULL, NULL);
   9012       if (isymbuf == NULL)
   9013 	return FALSE;
   9014 
   9015       /* Read in any version definitions.  */
   9016       versymhdr = &elf_tdata (input)->dynversym_hdr;
   9017       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
   9018       if (extversym == NULL)
   9019 	goto error_ret;
   9020 
   9021       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
   9022 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
   9023 	      != versymhdr->sh_size))
   9024 	{
   9025 	  free (extversym);
   9026 	error_ret:
   9027 	  free (isymbuf);
   9028 	  return FALSE;
   9029 	}
   9030 
   9031       ever = extversym + extsymoff;
   9032       isymend = isymbuf + extsymcount;
   9033       for (isym = isymbuf; isym < isymend; isym++, ever++)
   9034 	{
   9035 	  const char *name;
   9036 	  Elf_Internal_Versym iver;
   9037 	  unsigned short version_index;
   9038 
   9039 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
   9040 	      || isym->st_shndx == SHN_UNDEF)
   9041 	    continue;
   9042 
   9043 	  name = bfd_elf_string_from_elf_section (input,
   9044 						  hdr->sh_link,
   9045 						  isym->st_name);
   9046 	  if (strcmp (name, h->root.root.string) != 0)
   9047 	    continue;
   9048 
   9049 	  _bfd_elf_swap_versym_in (input, ever, &iver);
   9050 
   9051 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
   9052 	      && !(h->def_regular
   9053 		   && h->forced_local))
   9054 	    {
   9055 	      /* If we have a non-hidden versioned sym, then it should
   9056 		 have provided a definition for the undefined sym unless
   9057 		 it is defined in a non-shared object and forced local.
   9058 	       */
   9059 	      abort ();
   9060 	    }
   9061 
   9062 	  version_index = iver.vs_vers & VERSYM_VERSION;
   9063 	  if (version_index == 1 || version_index == 2)
   9064 	    {
   9065 	      /* This is the base or first version.  We can use it.  */
   9066 	      free (extversym);
   9067 	      free (isymbuf);
   9068 	      return TRUE;
   9069 	    }
   9070 	}
   9071 
   9072       free (extversym);
   9073       free (isymbuf);
   9074     }
   9075 
   9076   return FALSE;
   9077 }
   9078 
   9079 /* Add an external symbol to the symbol table.  This is called from
   9080    the hash table traversal routine.  When generating a shared object,
   9081    we go through the symbol table twice.  The first time we output
   9082    anything that might have been forced to local scope in a version
   9083    script.  The second time we output the symbols that are still
   9084    global symbols.  */
   9085 
   9086 static bfd_boolean
   9087 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
   9088 {
   9089   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
   9090   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
   9091   struct elf_final_link_info *flinfo = eoinfo->flinfo;
   9092   bfd_boolean strip;
   9093   Elf_Internal_Sym sym;
   9094   asection *input_sec;
   9095   const struct elf_backend_data *bed;
   9096   long indx;
   9097   int ret;
   9098   /* A symbol is bound locally if it is forced local or it is locally
   9099      defined, hidden versioned, not referenced by shared library and
   9100      not exported when linking executable.  */
   9101   bfd_boolean local_bind = (h->forced_local
   9102 			    || (bfd_link_executable (flinfo->info)
   9103 				&& !flinfo->info->export_dynamic
   9104 				&& !h->dynamic
   9105 				&& !h->ref_dynamic
   9106 				&& h->def_regular
   9107 				&& h->versioned == versioned_hidden));
   9108 
   9109   if (h->root.type == bfd_link_hash_warning)
   9110     {
   9111       h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9112       if (h->root.type == bfd_link_hash_new)
   9113 	return TRUE;
   9114     }
   9115 
   9116   /* Decide whether to output this symbol in this pass.  */
   9117   if (eoinfo->localsyms)
   9118     {
   9119       if (!local_bind)
   9120 	return TRUE;
   9121     }
   9122   else
   9123     {
   9124       if (local_bind)
   9125 	return TRUE;
   9126     }
   9127 
   9128   bed = get_elf_backend_data (flinfo->output_bfd);
   9129 
   9130   if (h->root.type == bfd_link_hash_undefined)
   9131     {
   9132       /* If we have an undefined symbol reference here then it must have
   9133 	 come from a shared library that is being linked in.  (Undefined
   9134 	 references in regular files have already been handled unless
   9135 	 they are in unreferenced sections which are removed by garbage
   9136 	 collection).  */
   9137       bfd_boolean ignore_undef = FALSE;
   9138 
   9139       /* Some symbols may be special in that the fact that they're
   9140 	 undefined can be safely ignored - let backend determine that.  */
   9141       if (bed->elf_backend_ignore_undef_symbol)
   9142 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
   9143 
   9144       /* If we are reporting errors for this situation then do so now.  */
   9145       if (!ignore_undef
   9146 	  && h->ref_dynamic
   9147 	  && (!h->ref_regular || flinfo->info->gc_sections)
   9148 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
   9149 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
   9150 	{
   9151 	  if (!(flinfo->info->callbacks->undefined_symbol
   9152 		(flinfo->info, h->root.root.string,
   9153 		 h->ref_regular ? NULL : h->root.u.undef.abfd,
   9154 		 NULL, 0,
   9155 		 (flinfo->info->unresolved_syms_in_shared_libs
   9156 		  == RM_GENERATE_ERROR))))
   9157 	    {
   9158 	      bfd_set_error (bfd_error_bad_value);
   9159 	      eoinfo->failed = TRUE;
   9160 	      return FALSE;
   9161 	    }
   9162 	}
   9163     }
   9164 
   9165   /* We should also warn if a forced local symbol is referenced from
   9166      shared libraries.  */
   9167   if (bfd_link_executable (flinfo->info)
   9168       && h->forced_local
   9169       && h->ref_dynamic
   9170       && h->def_regular
   9171       && !h->dynamic_def
   9172       && h->ref_dynamic_nonweak
   9173       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
   9174     {
   9175       bfd *def_bfd;
   9176       const char *msg;
   9177       struct elf_link_hash_entry *hi = h;
   9178 
   9179       /* Check indirect symbol.  */
   9180       while (hi->root.type == bfd_link_hash_indirect)
   9181 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
   9182 
   9183       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
   9184 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
   9185       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
   9186 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
   9187       else
   9188 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
   9189       def_bfd = flinfo->output_bfd;
   9190       if (hi->root.u.def.section != bfd_abs_section_ptr)
   9191 	def_bfd = hi->root.u.def.section->owner;
   9192       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
   9193 			     h->root.root.string);
   9194       bfd_set_error (bfd_error_bad_value);
   9195       eoinfo->failed = TRUE;
   9196       return FALSE;
   9197     }
   9198 
   9199   /* We don't want to output symbols that have never been mentioned by
   9200      a regular file, or that we have been told to strip.  However, if
   9201      h->indx is set to -2, the symbol is used by a reloc and we must
   9202      output it.  */
   9203   if (h->indx == -2)
   9204     strip = FALSE;
   9205   else if ((h->def_dynamic
   9206 	    || h->ref_dynamic
   9207 	    || h->root.type == bfd_link_hash_new)
   9208 	   && !h->def_regular
   9209 	   && !h->ref_regular)
   9210     strip = TRUE;
   9211   else if (flinfo->info->strip == strip_all)
   9212     strip = TRUE;
   9213   else if (flinfo->info->strip == strip_some
   9214 	   && bfd_hash_lookup (flinfo->info->keep_hash,
   9215 			       h->root.root.string, FALSE, FALSE) == NULL)
   9216     strip = TRUE;
   9217   else if ((h->root.type == bfd_link_hash_defined
   9218 	    || h->root.type == bfd_link_hash_defweak)
   9219 	   && ((flinfo->info->strip_discarded
   9220 		&& discarded_section (h->root.u.def.section))
   9221 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
   9222 		   && h->root.u.def.section->owner != NULL
   9223 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
   9224     strip = TRUE;
   9225   else if ((h->root.type == bfd_link_hash_undefined
   9226 	    || h->root.type == bfd_link_hash_undefweak)
   9227 	   && h->root.u.undef.abfd != NULL
   9228 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
   9229     strip = TRUE;
   9230   else
   9231     strip = FALSE;
   9232 
   9233   /* If we're stripping it, and it's not a dynamic symbol, there's
   9234      nothing else to do unless it is a forced local symbol or a
   9235      STT_GNU_IFUNC symbol.  */
   9236   if (strip
   9237       && h->dynindx == -1
   9238       && h->type != STT_GNU_IFUNC
   9239       && !h->forced_local)
   9240     return TRUE;
   9241 
   9242   sym.st_value = 0;
   9243   sym.st_size = h->size;
   9244   sym.st_other = h->other;
   9245   if (local_bind)
   9246     {
   9247       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
   9248       /* Turn off visibility on local symbol.  */
   9249       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   9250     }
   9251   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
   9252   else if (h->unique_global && h->def_regular)
   9253     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
   9254   else if (h->root.type == bfd_link_hash_undefweak
   9255 	   || h->root.type == bfd_link_hash_defweak)
   9256     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
   9257   else
   9258     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
   9259   sym.st_target_internal = h->target_internal;
   9260 
   9261   switch (h->root.type)
   9262     {
   9263     default:
   9264     case bfd_link_hash_new:
   9265     case bfd_link_hash_warning:
   9266       abort ();
   9267       return FALSE;
   9268 
   9269     case bfd_link_hash_undefined:
   9270     case bfd_link_hash_undefweak:
   9271       input_sec = bfd_und_section_ptr;
   9272       sym.st_shndx = SHN_UNDEF;
   9273       break;
   9274 
   9275     case bfd_link_hash_defined:
   9276     case bfd_link_hash_defweak:
   9277       {
   9278 	input_sec = h->root.u.def.section;
   9279 	if (input_sec->output_section != NULL)
   9280 	  {
   9281 	    sym.st_shndx =
   9282 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
   9283 						 input_sec->output_section);
   9284 	    if (sym.st_shndx == SHN_BAD)
   9285 	      {
   9286 		(*_bfd_error_handler)
   9287 		  (_("%B: could not find output section %A for input section %A"),
   9288 		   flinfo->output_bfd, input_sec->output_section, input_sec);
   9289 		bfd_set_error (bfd_error_nonrepresentable_section);
   9290 		eoinfo->failed = TRUE;
   9291 		return FALSE;
   9292 	      }
   9293 
   9294 	    /* ELF symbols in relocatable files are section relative,
   9295 	       but in nonrelocatable files they are virtual
   9296 	       addresses.  */
   9297 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
   9298 	    if (!bfd_link_relocatable (flinfo->info))
   9299 	      {
   9300 		sym.st_value += input_sec->output_section->vma;
   9301 		if (h->type == STT_TLS)
   9302 		  {
   9303 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
   9304 		    if (tls_sec != NULL)
   9305 		      sym.st_value -= tls_sec->vma;
   9306 		  }
   9307 	      }
   9308 	  }
   9309 	else
   9310 	  {
   9311 	    BFD_ASSERT (input_sec->owner == NULL
   9312 			|| (input_sec->owner->flags & DYNAMIC) != 0);
   9313 	    sym.st_shndx = SHN_UNDEF;
   9314 	    input_sec = bfd_und_section_ptr;
   9315 	  }
   9316       }
   9317       break;
   9318 
   9319     case bfd_link_hash_common:
   9320       input_sec = h->root.u.c.p->section;
   9321       sym.st_shndx = bed->common_section_index (input_sec);
   9322       sym.st_value = 1 << h->root.u.c.p->alignment_power;
   9323       break;
   9324 
   9325     case bfd_link_hash_indirect:
   9326       /* These symbols are created by symbol versioning.  They point
   9327 	 to the decorated version of the name.  For example, if the
   9328 	 symbol foo@@GNU_1.2 is the default, which should be used when
   9329 	 foo is used with no version, then we add an indirect symbol
   9330 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
   9331 	 since the indirected symbol is already in the hash table.  */
   9332       return TRUE;
   9333     }
   9334 
   9335   /* Give the processor backend a chance to tweak the symbol value,
   9336      and also to finish up anything that needs to be done for this
   9337      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
   9338      forced local syms when non-shared is due to a historical quirk.
   9339      STT_GNU_IFUNC symbol must go through PLT.  */
   9340   if ((h->type == STT_GNU_IFUNC
   9341        && h->def_regular
   9342        && !bfd_link_relocatable (flinfo->info))
   9343       || ((h->dynindx != -1
   9344 	   || h->forced_local)
   9345 	  && ((bfd_link_pic (flinfo->info)
   9346 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   9347 		   || h->root.type != bfd_link_hash_undefweak))
   9348 	      || !h->forced_local)
   9349 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
   9350     {
   9351       if (! ((*bed->elf_backend_finish_dynamic_symbol)
   9352 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
   9353 	{
   9354 	  eoinfo->failed = TRUE;
   9355 	  return FALSE;
   9356 	}
   9357     }
   9358 
   9359   /* If we are marking the symbol as undefined, and there are no
   9360      non-weak references to this symbol from a regular object, then
   9361      mark the symbol as weak undefined; if there are non-weak
   9362      references, mark the symbol as strong.  We can't do this earlier,
   9363      because it might not be marked as undefined until the
   9364      finish_dynamic_symbol routine gets through with it.  */
   9365   if (sym.st_shndx == SHN_UNDEF
   9366       && h->ref_regular
   9367       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
   9368 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
   9369     {
   9370       int bindtype;
   9371       unsigned int type = ELF_ST_TYPE (sym.st_info);
   9372 
   9373       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
   9374       if (type == STT_GNU_IFUNC)
   9375 	type = STT_FUNC;
   9376 
   9377       if (h->ref_regular_nonweak)
   9378 	bindtype = STB_GLOBAL;
   9379       else
   9380 	bindtype = STB_WEAK;
   9381       sym.st_info = ELF_ST_INFO (bindtype, type);
   9382     }
   9383 
   9384   /* If this is a symbol defined in a dynamic library, don't use the
   9385      symbol size from the dynamic library.  Relinking an executable
   9386      against a new library may introduce gratuitous changes in the
   9387      executable's symbols if we keep the size.  */
   9388   if (sym.st_shndx == SHN_UNDEF
   9389       && !h->def_regular
   9390       && h->def_dynamic)
   9391     sym.st_size = 0;
   9392 
   9393   /* If a non-weak symbol with non-default visibility is not defined
   9394      locally, it is a fatal error.  */
   9395   if (!bfd_link_relocatable (flinfo->info)
   9396       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
   9397       && ELF_ST_BIND (sym.st_info) != STB_WEAK
   9398       && h->root.type == bfd_link_hash_undefined
   9399       && !h->def_regular)
   9400     {
   9401       const char *msg;
   9402 
   9403       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
   9404 	msg = _("%B: protected symbol `%s' isn't defined");
   9405       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
   9406 	msg = _("%B: internal symbol `%s' isn't defined");
   9407       else
   9408 	msg = _("%B: hidden symbol `%s' isn't defined");
   9409       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
   9410       bfd_set_error (bfd_error_bad_value);
   9411       eoinfo->failed = TRUE;
   9412       return FALSE;
   9413     }
   9414 
   9415   /* If this symbol should be put in the .dynsym section, then put it
   9416      there now.  We already know the symbol index.  We also fill in
   9417      the entry in the .hash section.  */
   9418   if (elf_hash_table (flinfo->info)->dynsym != NULL
   9419       && h->dynindx != -1
   9420       && elf_hash_table (flinfo->info)->dynamic_sections_created)
   9421     {
   9422       bfd_byte *esym;
   9423 
   9424       /* Since there is no version information in the dynamic string,
   9425 	 if there is no version info in symbol version section, we will
   9426 	 have a run-time problem if not linking executable, referenced
   9427 	 by shared library, not locally defined, or not bound locally.
   9428       */
   9429       if (h->verinfo.verdef == NULL
   9430 	  && !local_bind
   9431 	  && (!bfd_link_executable (flinfo->info)
   9432 	      || h->ref_dynamic
   9433 	      || !h->def_regular))
   9434 	{
   9435 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
   9436 
   9437 	  if (p && p [1] != '\0')
   9438 	    {
   9439 	      (*_bfd_error_handler)
   9440 		(_("%B: No symbol version section for versioned symbol `%s'"),
   9441 		 flinfo->output_bfd, h->root.root.string);
   9442 	      eoinfo->failed = TRUE;
   9443 	      return FALSE;
   9444 	    }
   9445 	}
   9446 
   9447       sym.st_name = h->dynstr_index;
   9448       esym = (elf_hash_table (flinfo->info)->dynsym->contents
   9449 	      + h->dynindx * bed->s->sizeof_sym);
   9450       if (!check_dynsym (flinfo->output_bfd, &sym))
   9451 	{
   9452 	  eoinfo->failed = TRUE;
   9453 	  return FALSE;
   9454 	}
   9455       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
   9456 
   9457       if (flinfo->hash_sec != NULL)
   9458 	{
   9459 	  size_t hash_entry_size;
   9460 	  bfd_byte *bucketpos;
   9461 	  bfd_vma chain;
   9462 	  size_t bucketcount;
   9463 	  size_t bucket;
   9464 
   9465 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
   9466 	  bucket = h->u.elf_hash_value % bucketcount;
   9467 
   9468 	  hash_entry_size
   9469 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
   9470 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
   9471 		       + (bucket + 2) * hash_entry_size);
   9472 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
   9473 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
   9474 		   bucketpos);
   9475 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
   9476 		   ((bfd_byte *) flinfo->hash_sec->contents
   9477 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
   9478 	}
   9479 
   9480       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
   9481 	{
   9482 	  Elf_Internal_Versym iversym;
   9483 	  Elf_External_Versym *eversym;
   9484 
   9485 	  if (!h->def_regular)
   9486 	    {
   9487 	      if (h->verinfo.verdef == NULL
   9488 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
   9489 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
   9490 		iversym.vs_vers = 0;
   9491 	      else
   9492 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
   9493 	    }
   9494 	  else
   9495 	    {
   9496 	      if (h->verinfo.vertree == NULL)
   9497 		iversym.vs_vers = 1;
   9498 	      else
   9499 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
   9500 	      if (flinfo->info->create_default_symver)
   9501 		iversym.vs_vers++;
   9502 	    }
   9503 
   9504 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
   9505 	     defined locally.  */
   9506 	  if (h->versioned == versioned_hidden && h->def_regular)
   9507 	    iversym.vs_vers |= VERSYM_HIDDEN;
   9508 
   9509 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
   9510 	  eversym += h->dynindx;
   9511 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
   9512 	}
   9513     }
   9514 
   9515   /* If we're stripping it, then it was just a dynamic symbol, and
   9516      there's nothing else to do.  */
   9517   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
   9518     return TRUE;
   9519 
   9520   /* Output a FILE symbol so that following locals are not associated
   9521      with the wrong input file.  We need one for forced local symbols
   9522      if we've seen more than one FILE symbol or when we have exactly
   9523      one FILE symbol but global symbols are present in a file other
   9524      than the one with the FILE symbol.  We also need one if linker
   9525      defined symbols are present.  In practice these conditions are
   9526      always met, so just emit the FILE symbol unconditionally.  */
   9527   if (eoinfo->localsyms
   9528       && !eoinfo->file_sym_done
   9529       && eoinfo->flinfo->filesym_count != 0)
   9530     {
   9531       Elf_Internal_Sym fsym;
   9532 
   9533       memset (&fsym, 0, sizeof (fsym));
   9534       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9535       fsym.st_shndx = SHN_ABS;
   9536       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
   9537 				      bfd_und_section_ptr, NULL))
   9538 	return FALSE;
   9539 
   9540       eoinfo->file_sym_done = TRUE;
   9541     }
   9542 
   9543   indx = bfd_get_symcount (flinfo->output_bfd);
   9544   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
   9545 				   input_sec, h);
   9546   if (ret == 0)
   9547     {
   9548       eoinfo->failed = TRUE;
   9549       return FALSE;
   9550     }
   9551   else if (ret == 1)
   9552     h->indx = indx;
   9553   else if (h->indx == -2)
   9554     abort();
   9555 
   9556   return TRUE;
   9557 }
   9558 
   9559 /* Return TRUE if special handling is done for relocs in SEC against
   9560    symbols defined in discarded sections.  */
   9561 
   9562 static bfd_boolean
   9563 elf_section_ignore_discarded_relocs (asection *sec)
   9564 {
   9565   const struct elf_backend_data *bed;
   9566 
   9567   switch (sec->sec_info_type)
   9568     {
   9569     case SEC_INFO_TYPE_STABS:
   9570     case SEC_INFO_TYPE_EH_FRAME:
   9571     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   9572       return TRUE;
   9573     default:
   9574       break;
   9575     }
   9576 
   9577   bed = get_elf_backend_data (sec->owner);
   9578   if (bed->elf_backend_ignore_discarded_relocs != NULL
   9579       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
   9580     return TRUE;
   9581 
   9582   return FALSE;
   9583 }
   9584 
   9585 /* Return a mask saying how ld should treat relocations in SEC against
   9586    symbols defined in discarded sections.  If this function returns
   9587    COMPLAIN set, ld will issue a warning message.  If this function
   9588    returns PRETEND set, and the discarded section was link-once and the
   9589    same size as the kept link-once section, ld will pretend that the
   9590    symbol was actually defined in the kept section.  Otherwise ld will
   9591    zero the reloc (at least that is the intent, but some cooperation by
   9592    the target dependent code is needed, particularly for REL targets).  */
   9593 
   9594 unsigned int
   9595 _bfd_elf_default_action_discarded (asection *sec)
   9596 {
   9597   if (sec->flags & SEC_DEBUGGING)
   9598     return PRETEND;
   9599 
   9600   if (strcmp (".eh_frame", sec->name) == 0)
   9601     return 0;
   9602 
   9603   if (strcmp (".gcc_except_table", sec->name) == 0)
   9604     return 0;
   9605 
   9606   return COMPLAIN | PRETEND;
   9607 }
   9608 
   9609 /* Find a match between a section and a member of a section group.  */
   9610 
   9611 static asection *
   9612 match_group_member (asection *sec, asection *group,
   9613 		    struct bfd_link_info *info)
   9614 {
   9615   asection *first = elf_next_in_group (group);
   9616   asection *s = first;
   9617 
   9618   while (s != NULL)
   9619     {
   9620       if (bfd_elf_match_symbols_in_sections (s, sec, info))
   9621 	return s;
   9622 
   9623       s = elf_next_in_group (s);
   9624       if (s == first)
   9625 	break;
   9626     }
   9627 
   9628   return NULL;
   9629 }
   9630 
   9631 /* Check if the kept section of a discarded section SEC can be used
   9632    to replace it.  Return the replacement if it is OK.  Otherwise return
   9633    NULL.  */
   9634 
   9635 asection *
   9636 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
   9637 {
   9638   asection *kept;
   9639 
   9640   kept = sec->kept_section;
   9641   if (kept != NULL)
   9642     {
   9643       if ((kept->flags & SEC_GROUP) != 0)
   9644 	kept = match_group_member (sec, kept, info);
   9645       if (kept != NULL
   9646 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
   9647 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
   9648 	kept = NULL;
   9649       sec->kept_section = kept;
   9650     }
   9651   return kept;
   9652 }
   9653 
   9654 /* Link an input file into the linker output file.  This function
   9655    handles all the sections and relocations of the input file at once.
   9656    This is so that we only have to read the local symbols once, and
   9657    don't have to keep them in memory.  */
   9658 
   9659 static bfd_boolean
   9660 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
   9661 {
   9662   int (*relocate_section)
   9663     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   9664      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
   9665   bfd *output_bfd;
   9666   Elf_Internal_Shdr *symtab_hdr;
   9667   size_t locsymcount;
   9668   size_t extsymoff;
   9669   Elf_Internal_Sym *isymbuf;
   9670   Elf_Internal_Sym *isym;
   9671   Elf_Internal_Sym *isymend;
   9672   long *pindex;
   9673   asection **ppsection;
   9674   asection *o;
   9675   const struct elf_backend_data *bed;
   9676   struct elf_link_hash_entry **sym_hashes;
   9677   bfd_size_type address_size;
   9678   bfd_vma r_type_mask;
   9679   int r_sym_shift;
   9680   bfd_boolean have_file_sym = FALSE;
   9681 
   9682   output_bfd = flinfo->output_bfd;
   9683   bed = get_elf_backend_data (output_bfd);
   9684   relocate_section = bed->elf_backend_relocate_section;
   9685 
   9686   /* If this is a dynamic object, we don't want to do anything here:
   9687      we don't want the local symbols, and we don't want the section
   9688      contents.  */
   9689   if ((input_bfd->flags & DYNAMIC) != 0)
   9690     return TRUE;
   9691 
   9692   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   9693   if (elf_bad_symtab (input_bfd))
   9694     {
   9695       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   9696       extsymoff = 0;
   9697     }
   9698   else
   9699     {
   9700       locsymcount = symtab_hdr->sh_info;
   9701       extsymoff = symtab_hdr->sh_info;
   9702     }
   9703 
   9704   /* Read the local symbols.  */
   9705   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   9706   if (isymbuf == NULL && locsymcount != 0)
   9707     {
   9708       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
   9709 				      flinfo->internal_syms,
   9710 				      flinfo->external_syms,
   9711 				      flinfo->locsym_shndx);
   9712       if (isymbuf == NULL)
   9713 	return FALSE;
   9714     }
   9715 
   9716   /* Find local symbol sections and adjust values of symbols in
   9717      SEC_MERGE sections.  Write out those local symbols we know are
   9718      going into the output file.  */
   9719   isymend = isymbuf + locsymcount;
   9720   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
   9721        isym < isymend;
   9722        isym++, pindex++, ppsection++)
   9723     {
   9724       asection *isec;
   9725       const char *name;
   9726       Elf_Internal_Sym osym;
   9727       long indx;
   9728       int ret;
   9729 
   9730       *pindex = -1;
   9731 
   9732       if (elf_bad_symtab (input_bfd))
   9733 	{
   9734 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
   9735 	    {
   9736 	      *ppsection = NULL;
   9737 	      continue;
   9738 	    }
   9739 	}
   9740 
   9741       if (isym->st_shndx == SHN_UNDEF)
   9742 	isec = bfd_und_section_ptr;
   9743       else if (isym->st_shndx == SHN_ABS)
   9744 	isec = bfd_abs_section_ptr;
   9745       else if (isym->st_shndx == SHN_COMMON)
   9746 	isec = bfd_com_section_ptr;
   9747       else
   9748 	{
   9749 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   9750 	  if (isec == NULL)
   9751 	    {
   9752 	      /* Don't attempt to output symbols with st_shnx in the
   9753 		 reserved range other than SHN_ABS and SHN_COMMON.  */
   9754 	      *ppsection = NULL;
   9755 	      continue;
   9756 	    }
   9757 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
   9758 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   9759 	    isym->st_value =
   9760 	      _bfd_merged_section_offset (output_bfd, &isec,
   9761 					  elf_section_data (isec)->sec_info,
   9762 					  isym->st_value);
   9763 	}
   9764 
   9765       *ppsection = isec;
   9766 
   9767       /* Don't output the first, undefined, symbol.  */
   9768       if (ppsection == flinfo->sections)
   9769 	continue;
   9770 
   9771       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   9772 	{
   9773 	  /* We never output section symbols.  Instead, we use the
   9774 	     section symbol of the corresponding section in the output
   9775 	     file.  */
   9776 	  continue;
   9777 	}
   9778 
   9779       /* If we are stripping all symbols, we don't want to output this
   9780 	 one.  */
   9781       if (flinfo->info->strip == strip_all)
   9782 	continue;
   9783 
   9784       /* If we are discarding all local symbols, we don't want to
   9785 	 output this one.  If we are generating a relocatable output
   9786 	 file, then some of the local symbols may be required by
   9787 	 relocs; we output them below as we discover that they are
   9788 	 needed.  */
   9789       if (flinfo->info->discard == discard_all)
   9790 	continue;
   9791 
   9792       /* If this symbol is defined in a section which we are
   9793 	 discarding, we don't need to keep it.  */
   9794       if (isym->st_shndx != SHN_UNDEF
   9795 	  && isym->st_shndx < SHN_LORESERVE
   9796 	  && bfd_section_removed_from_list (output_bfd,
   9797 					    isec->output_section))
   9798 	continue;
   9799 
   9800       /* Get the name of the symbol.  */
   9801       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
   9802 					      isym->st_name);
   9803       if (name == NULL)
   9804 	return FALSE;
   9805 
   9806       /* See if we are discarding symbols with this name.  */
   9807       if ((flinfo->info->strip == strip_some
   9808 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
   9809 	       == NULL))
   9810 	  || (((flinfo->info->discard == discard_sec_merge
   9811 		&& (isec->flags & SEC_MERGE)
   9812 		&& !bfd_link_relocatable (flinfo->info))
   9813 	       || flinfo->info->discard == discard_l)
   9814 	      && bfd_is_local_label_name (input_bfd, name)))
   9815 	continue;
   9816 
   9817       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
   9818 	{
   9819 	  if (input_bfd->lto_output)
   9820 	    /* -flto puts a temp file name here.  This means builds
   9821 	       are not reproducible.  Discard the symbol.  */
   9822 	    continue;
   9823 	  have_file_sym = TRUE;
   9824 	  flinfo->filesym_count += 1;
   9825 	}
   9826       if (!have_file_sym)
   9827 	{
   9828 	  /* In the absence of debug info, bfd_find_nearest_line uses
   9829 	     FILE symbols to determine the source file for local
   9830 	     function symbols.  Provide a FILE symbol here if input
   9831 	     files lack such, so that their symbols won't be
   9832 	     associated with a previous input file.  It's not the
   9833 	     source file, but the best we can do.  */
   9834 	  have_file_sym = TRUE;
   9835 	  flinfo->filesym_count += 1;
   9836 	  memset (&osym, 0, sizeof (osym));
   9837 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9838 	  osym.st_shndx = SHN_ABS;
   9839 	  if (!elf_link_output_symstrtab (flinfo,
   9840 					  (input_bfd->lto_output ? NULL
   9841 					   : input_bfd->filename),
   9842 					  &osym, bfd_abs_section_ptr,
   9843 					  NULL))
   9844 	    return FALSE;
   9845 	}
   9846 
   9847       osym = *isym;
   9848 
   9849       /* Adjust the section index for the output file.  */
   9850       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   9851 							 isec->output_section);
   9852       if (osym.st_shndx == SHN_BAD)
   9853 	return FALSE;
   9854 
   9855       /* ELF symbols in relocatable files are section relative, but
   9856 	 in executable files they are virtual addresses.  Note that
   9857 	 this code assumes that all ELF sections have an associated
   9858 	 BFD section with a reasonable value for output_offset; below
   9859 	 we assume that they also have a reasonable value for
   9860 	 output_section.  Any special sections must be set up to meet
   9861 	 these requirements.  */
   9862       osym.st_value += isec->output_offset;
   9863       if (!bfd_link_relocatable (flinfo->info))
   9864 	{
   9865 	  osym.st_value += isec->output_section->vma;
   9866 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
   9867 	    {
   9868 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
   9869 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
   9870 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
   9871 	    }
   9872 	}
   9873 
   9874       indx = bfd_get_symcount (output_bfd);
   9875       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
   9876       if (ret == 0)
   9877 	return FALSE;
   9878       else if (ret == 1)
   9879 	*pindex = indx;
   9880     }
   9881 
   9882   if (bed->s->arch_size == 32)
   9883     {
   9884       r_type_mask = 0xff;
   9885       r_sym_shift = 8;
   9886       address_size = 4;
   9887     }
   9888   else
   9889     {
   9890       r_type_mask = 0xffffffff;
   9891       r_sym_shift = 32;
   9892       address_size = 8;
   9893     }
   9894 
   9895   /* Relocate the contents of each section.  */
   9896   sym_hashes = elf_sym_hashes (input_bfd);
   9897   for (o = input_bfd->sections; o != NULL; o = o->next)
   9898     {
   9899       bfd_byte *contents;
   9900 
   9901       if (! o->linker_mark)
   9902 	{
   9903 	  /* This section was omitted from the link.  */
   9904 	  continue;
   9905 	}
   9906 
   9907       if (bfd_link_relocatable (flinfo->info)
   9908 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
   9909 	{
   9910 	  /* Deal with the group signature symbol.  */
   9911 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
   9912 	  unsigned long symndx = sec_data->this_hdr.sh_info;
   9913 	  asection *osec = o->output_section;
   9914 
   9915 	  if (symndx >= locsymcount
   9916 	      || (elf_bad_symtab (input_bfd)
   9917 		  && flinfo->sections[symndx] == NULL))
   9918 	    {
   9919 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
   9920 	      while (h->root.type == bfd_link_hash_indirect
   9921 		     || h->root.type == bfd_link_hash_warning)
   9922 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9923 	      /* Arrange for symbol to be output.  */
   9924 	      h->indx = -2;
   9925 	      elf_section_data (osec)->this_hdr.sh_info = -2;
   9926 	    }
   9927 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
   9928 	    {
   9929 	      /* We'll use the output section target_index.  */
   9930 	      asection *sec = flinfo->sections[symndx]->output_section;
   9931 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
   9932 	    }
   9933 	  else
   9934 	    {
   9935 	      if (flinfo->indices[symndx] == -1)
   9936 		{
   9937 		  /* Otherwise output the local symbol now.  */
   9938 		  Elf_Internal_Sym sym = isymbuf[symndx];
   9939 		  asection *sec = flinfo->sections[symndx]->output_section;
   9940 		  const char *name;
   9941 		  long indx;
   9942 		  int ret;
   9943 
   9944 		  name = bfd_elf_string_from_elf_section (input_bfd,
   9945 							  symtab_hdr->sh_link,
   9946 							  sym.st_name);
   9947 		  if (name == NULL)
   9948 		    return FALSE;
   9949 
   9950 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
   9951 								    sec);
   9952 		  if (sym.st_shndx == SHN_BAD)
   9953 		    return FALSE;
   9954 
   9955 		  sym.st_value += o->output_offset;
   9956 
   9957 		  indx = bfd_get_symcount (output_bfd);
   9958 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
   9959 						   NULL);
   9960 		  if (ret == 0)
   9961 		    return FALSE;
   9962 		  else if (ret == 1)
   9963 		    flinfo->indices[symndx] = indx;
   9964 		  else
   9965 		    abort ();
   9966 		}
   9967 	      elf_section_data (osec)->this_hdr.sh_info
   9968 		= flinfo->indices[symndx];
   9969 	    }
   9970 	}
   9971 
   9972       if ((o->flags & SEC_HAS_CONTENTS) == 0
   9973 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   9974 	continue;
   9975 
   9976       if ((o->flags & SEC_LINKER_CREATED) != 0)
   9977 	{
   9978 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
   9979 	     or somesuch.  */
   9980 	  continue;
   9981 	}
   9982 
   9983       /* Get the contents of the section.  They have been cached by a
   9984 	 relaxation routine.  Note that o is a section in an input
   9985 	 file, so the contents field will not have been set by any of
   9986 	 the routines which work on output files.  */
   9987       if (elf_section_data (o)->this_hdr.contents != NULL)
   9988 	{
   9989 	  contents = elf_section_data (o)->this_hdr.contents;
   9990 	  if (bed->caches_rawsize
   9991 	      && o->rawsize != 0
   9992 	      && o->rawsize < o->size)
   9993 	    {
   9994 	      memcpy (flinfo->contents, contents, o->rawsize);
   9995 	      contents = flinfo->contents;
   9996 	    }
   9997 	}
   9998       else
   9999 	{
   10000 	  contents = flinfo->contents;
   10001 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   10002 	    return FALSE;
   10003 	}
   10004 
   10005       if ((o->flags & SEC_RELOC) != 0)
   10006 	{
   10007 	  Elf_Internal_Rela *internal_relocs;
   10008 	  Elf_Internal_Rela *rel, *relend;
   10009 	  int action_discarded;
   10010 	  int ret;
   10011 
   10012 	  /* Get the swapped relocs.  */
   10013 	  internal_relocs
   10014 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
   10015 					 flinfo->internal_relocs, FALSE);
   10016 	  if (internal_relocs == NULL
   10017 	      && o->reloc_count > 0)
   10018 	    return FALSE;
   10019 
   10020 	  /* We need to reverse-copy input .ctors/.dtors sections if
   10021 	     they are placed in .init_array/.finit_array for output.  */
   10022 	  if (o->size > address_size
   10023 	      && ((strncmp (o->name, ".ctors", 6) == 0
   10024 		   && strcmp (o->output_section->name,
   10025 			      ".init_array") == 0)
   10026 		  || (strncmp (o->name, ".dtors", 6) == 0
   10027 		      && strcmp (o->output_section->name,
   10028 				 ".fini_array") == 0))
   10029 	      && (o->name[6] == 0 || o->name[6] == '.'))
   10030 	    {
   10031 	      if (o->size != o->reloc_count * address_size)
   10032 		{
   10033 		  (*_bfd_error_handler)
   10034 		    (_("error: %B: size of section %A is not "
   10035 		       "multiple of address size"),
   10036 		     input_bfd, o);
   10037 		  bfd_set_error (bfd_error_on_input);
   10038 		  return FALSE;
   10039 		}
   10040 	      o->flags |= SEC_ELF_REVERSE_COPY;
   10041 	    }
   10042 
   10043 	  action_discarded = -1;
   10044 	  if (!elf_section_ignore_discarded_relocs (o))
   10045 	    action_discarded = (*bed->action_discarded) (o);
   10046 
   10047 	  /* Run through the relocs evaluating complex reloc symbols and
   10048 	     looking for relocs against symbols from discarded sections
   10049 	     or section symbols from removed link-once sections.
   10050 	     Complain about relocs against discarded sections.  Zero
   10051 	     relocs against removed link-once sections.  */
   10052 
   10053 	  rel = internal_relocs;
   10054 	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
   10055 	  for ( ; rel < relend; rel++)
   10056 	    {
   10057 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
   10058 	      unsigned int s_type;
   10059 	      asection **ps, *sec;
   10060 	      struct elf_link_hash_entry *h = NULL;
   10061 	      const char *sym_name;
   10062 
   10063 	      if (r_symndx == STN_UNDEF)
   10064 		continue;
   10065 
   10066 	      if (r_symndx >= locsymcount
   10067 		  || (elf_bad_symtab (input_bfd)
   10068 		      && flinfo->sections[r_symndx] == NULL))
   10069 		{
   10070 		  h = sym_hashes[r_symndx - extsymoff];
   10071 
   10072 		  /* Badly formatted input files can contain relocs that
   10073 		     reference non-existant symbols.  Check here so that
   10074 		     we do not seg fault.  */
   10075 		  if (h == NULL)
   10076 		    {
   10077 		      char buffer [32];
   10078 
   10079 		      sprintf_vma (buffer, rel->r_info);
   10080 		      (*_bfd_error_handler)
   10081 			(_("error: %B contains a reloc (0x%s) for section %A "
   10082 			   "that references a non-existent global symbol"),
   10083 			 input_bfd, o, buffer);
   10084 		      bfd_set_error (bfd_error_bad_value);
   10085 		      return FALSE;
   10086 		    }
   10087 
   10088 		  while (h->root.type == bfd_link_hash_indirect
   10089 			 || h->root.type == bfd_link_hash_warning)
   10090 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10091 
   10092 		  s_type = h->type;
   10093 
   10094 		  /* If a plugin symbol is referenced from a non-IR file,
   10095 		     mark the symbol as undefined.  Note that the
   10096 		     linker may attach linker created dynamic sections
   10097 		     to the plugin bfd.  Symbols defined in linker
   10098 		     created sections are not plugin symbols.  */
   10099 		  if (h->root.non_ir_ref
   10100 		      && (h->root.type == bfd_link_hash_defined
   10101 			  || h->root.type == bfd_link_hash_defweak)
   10102 		      && (h->root.u.def.section->flags
   10103 			  & SEC_LINKER_CREATED) == 0
   10104 		      && h->root.u.def.section->owner != NULL
   10105 		      && (h->root.u.def.section->owner->flags
   10106 			  & BFD_PLUGIN) != 0)
   10107 		    {
   10108 		      h->root.type = bfd_link_hash_undefined;
   10109 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
   10110 		    }
   10111 
   10112 		  ps = NULL;
   10113 		  if (h->root.type == bfd_link_hash_defined
   10114 		      || h->root.type == bfd_link_hash_defweak)
   10115 		    ps = &h->root.u.def.section;
   10116 
   10117 		  sym_name = h->root.root.string;
   10118 		}
   10119 	      else
   10120 		{
   10121 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
   10122 
   10123 		  s_type = ELF_ST_TYPE (sym->st_info);
   10124 		  ps = &flinfo->sections[r_symndx];
   10125 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10126 					       sym, *ps);
   10127 		}
   10128 
   10129 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
   10130 		  && !bfd_link_relocatable (flinfo->info))
   10131 		{
   10132 		  bfd_vma val;
   10133 		  bfd_vma dot = (rel->r_offset
   10134 				 + o->output_offset + o->output_section->vma);
   10135 #ifdef DEBUG
   10136 		  printf ("Encountered a complex symbol!");
   10137 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
   10138 			  input_bfd->filename, o->name,
   10139 			  (long) (rel - internal_relocs));
   10140 		  printf (" symbol: idx  %8.8lx, name %s\n",
   10141 			  r_symndx, sym_name);
   10142 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
   10143 			  (unsigned long) rel->r_info,
   10144 			  (unsigned long) rel->r_offset);
   10145 #endif
   10146 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
   10147 				    isymbuf, locsymcount, s_type == STT_SRELC))
   10148 		    return FALSE;
   10149 
   10150 		  /* Symbol evaluated OK.  Update to absolute value.  */
   10151 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
   10152 				    r_symndx, val);
   10153 		  continue;
   10154 		}
   10155 
   10156 	      if (action_discarded != -1 && ps != NULL)
   10157 		{
   10158 		  /* Complain if the definition comes from a
   10159 		     discarded section.  */
   10160 		  if ((sec = *ps) != NULL && discarded_section (sec))
   10161 		    {
   10162 		      BFD_ASSERT (r_symndx != STN_UNDEF);
   10163 		      if (action_discarded & COMPLAIN)
   10164 			(*flinfo->info->callbacks->einfo)
   10165 			  (_("%X`%s' referenced in section `%A' of %B: "
   10166 			     "defined in discarded section `%A' of %B\n"),
   10167 			   sym_name, o, input_bfd, sec, sec->owner);
   10168 
   10169 		      /* Try to do the best we can to support buggy old
   10170 			 versions of gcc.  Pretend that the symbol is
   10171 			 really defined in the kept linkonce section.
   10172 			 FIXME: This is quite broken.  Modifying the
   10173 			 symbol here means we will be changing all later
   10174 			 uses of the symbol, not just in this section.  */
   10175 		      if (action_discarded & PRETEND)
   10176 			{
   10177 			  asection *kept;
   10178 
   10179 			  kept = _bfd_elf_check_kept_section (sec,
   10180 							      flinfo->info);
   10181 			  if (kept != NULL)
   10182 			    {
   10183 			      *ps = kept;
   10184 			      continue;
   10185 			    }
   10186 			}
   10187 		    }
   10188 		}
   10189 	    }
   10190 
   10191 	  /* Relocate the section by invoking a back end routine.
   10192 
   10193 	     The back end routine is responsible for adjusting the
   10194 	     section contents as necessary, and (if using Rela relocs
   10195 	     and generating a relocatable output file) adjusting the
   10196 	     reloc addend as necessary.
   10197 
   10198 	     The back end routine does not have to worry about setting
   10199 	     the reloc address or the reloc symbol index.
   10200 
   10201 	     The back end routine is given a pointer to the swapped in
   10202 	     internal symbols, and can access the hash table entries
   10203 	     for the external symbols via elf_sym_hashes (input_bfd).
   10204 
   10205 	     When generating relocatable output, the back end routine
   10206 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
   10207 	     output symbol is going to be a section symbol
   10208 	     corresponding to the output section, which will require
   10209 	     the addend to be adjusted.  */
   10210 
   10211 	  ret = (*relocate_section) (output_bfd, flinfo->info,
   10212 				     input_bfd, o, contents,
   10213 				     internal_relocs,
   10214 				     isymbuf,
   10215 				     flinfo->sections);
   10216 	  if (!ret)
   10217 	    return FALSE;
   10218 
   10219 	  if (ret == 2
   10220 	      || bfd_link_relocatable (flinfo->info)
   10221 	      || flinfo->info->emitrelocations)
   10222 	    {
   10223 	      Elf_Internal_Rela *irela;
   10224 	      Elf_Internal_Rela *irelaend, *irelamid;
   10225 	      bfd_vma last_offset;
   10226 	      struct elf_link_hash_entry **rel_hash;
   10227 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
   10228 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
   10229 	      unsigned int next_erel;
   10230 	      bfd_boolean rela_normal;
   10231 	      struct bfd_elf_section_data *esdi, *esdo;
   10232 
   10233 	      esdi = elf_section_data (o);
   10234 	      esdo = elf_section_data (o->output_section);
   10235 	      rela_normal = FALSE;
   10236 
   10237 	      /* Adjust the reloc addresses and symbol indices.  */
   10238 
   10239 	      irela = internal_relocs;
   10240 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
   10241 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
   10242 	      /* We start processing the REL relocs, if any.  When we reach
   10243 		 IRELAMID in the loop, we switch to the RELA relocs.  */
   10244 	      irelamid = irela;
   10245 	      if (esdi->rel.hdr != NULL)
   10246 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
   10247 			     * bed->s->int_rels_per_ext_rel);
   10248 	      rel_hash_list = rel_hash;
   10249 	      rela_hash_list = NULL;
   10250 	      last_offset = o->output_offset;
   10251 	      if (!bfd_link_relocatable (flinfo->info))
   10252 		last_offset += o->output_section->vma;
   10253 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
   10254 		{
   10255 		  unsigned long r_symndx;
   10256 		  asection *sec;
   10257 		  Elf_Internal_Sym sym;
   10258 
   10259 		  if (next_erel == bed->s->int_rels_per_ext_rel)
   10260 		    {
   10261 		      rel_hash++;
   10262 		      next_erel = 0;
   10263 		    }
   10264 
   10265 		  if (irela == irelamid)
   10266 		    {
   10267 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
   10268 		      rela_hash_list = rel_hash;
   10269 		      rela_normal = bed->rela_normal;
   10270 		    }
   10271 
   10272 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
   10273 							     flinfo->info, o,
   10274 							     irela->r_offset);
   10275 		  if (irela->r_offset >= (bfd_vma) -2)
   10276 		    {
   10277 		      /* This is a reloc for a deleted entry or somesuch.
   10278 			 Turn it into an R_*_NONE reloc, at the same
   10279 			 offset as the last reloc.  elf_eh_frame.c and
   10280 			 bfd_elf_discard_info rely on reloc offsets
   10281 			 being ordered.  */
   10282 		      irela->r_offset = last_offset;
   10283 		      irela->r_info = 0;
   10284 		      irela->r_addend = 0;
   10285 		      continue;
   10286 		    }
   10287 
   10288 		  irela->r_offset += o->output_offset;
   10289 
   10290 		  /* Relocs in an executable have to be virtual addresses.  */
   10291 		  if (!bfd_link_relocatable (flinfo->info))
   10292 		    irela->r_offset += o->output_section->vma;
   10293 
   10294 		  last_offset = irela->r_offset;
   10295 
   10296 		  r_symndx = irela->r_info >> r_sym_shift;
   10297 		  if (r_symndx == STN_UNDEF)
   10298 		    continue;
   10299 
   10300 		  if (r_symndx >= locsymcount
   10301 		      || (elf_bad_symtab (input_bfd)
   10302 			  && flinfo->sections[r_symndx] == NULL))
   10303 		    {
   10304 		      struct elf_link_hash_entry *rh;
   10305 		      unsigned long indx;
   10306 
   10307 		      /* This is a reloc against a global symbol.  We
   10308 			 have not yet output all the local symbols, so
   10309 			 we do not know the symbol index of any global
   10310 			 symbol.  We set the rel_hash entry for this
   10311 			 reloc to point to the global hash table entry
   10312 			 for this symbol.  The symbol index is then
   10313 			 set at the end of bfd_elf_final_link.  */
   10314 		      indx = r_symndx - extsymoff;
   10315 		      rh = elf_sym_hashes (input_bfd)[indx];
   10316 		      while (rh->root.type == bfd_link_hash_indirect
   10317 			     || rh->root.type == bfd_link_hash_warning)
   10318 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
   10319 
   10320 		      /* Setting the index to -2 tells
   10321 			 elf_link_output_extsym that this symbol is
   10322 			 used by a reloc.  */
   10323 		      BFD_ASSERT (rh->indx < 0);
   10324 		      rh->indx = -2;
   10325 
   10326 		      *rel_hash = rh;
   10327 
   10328 		      continue;
   10329 		    }
   10330 
   10331 		  /* This is a reloc against a local symbol.  */
   10332 
   10333 		  *rel_hash = NULL;
   10334 		  sym = isymbuf[r_symndx];
   10335 		  sec = flinfo->sections[r_symndx];
   10336 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
   10337 		    {
   10338 		      /* I suppose the backend ought to fill in the
   10339 			 section of any STT_SECTION symbol against a
   10340 			 processor specific section.  */
   10341 		      r_symndx = STN_UNDEF;
   10342 		      if (bfd_is_abs_section (sec))
   10343 			;
   10344 		      else if (sec == NULL || sec->owner == NULL)
   10345 			{
   10346 			  bfd_set_error (bfd_error_bad_value);
   10347 			  return FALSE;
   10348 			}
   10349 		      else
   10350 			{
   10351 			  asection *osec = sec->output_section;
   10352 
   10353 			  /* If we have discarded a section, the output
   10354 			     section will be the absolute section.  In
   10355 			     case of discarded SEC_MERGE sections, use
   10356 			     the kept section.  relocate_section should
   10357 			     have already handled discarded linkonce
   10358 			     sections.  */
   10359 			  if (bfd_is_abs_section (osec)
   10360 			      && sec->kept_section != NULL
   10361 			      && sec->kept_section->output_section != NULL)
   10362 			    {
   10363 			      osec = sec->kept_section->output_section;
   10364 			      irela->r_addend -= osec->vma;
   10365 			    }
   10366 
   10367 			  if (!bfd_is_abs_section (osec))
   10368 			    {
   10369 			      r_symndx = osec->target_index;
   10370 			      if (r_symndx == STN_UNDEF)
   10371 				{
   10372 				  irela->r_addend += osec->vma;
   10373 				  osec = _bfd_nearby_section (output_bfd, osec,
   10374 							      osec->vma);
   10375 				  irela->r_addend -= osec->vma;
   10376 				  r_symndx = osec->target_index;
   10377 				}
   10378 			    }
   10379 			}
   10380 
   10381 		      /* Adjust the addend according to where the
   10382 			 section winds up in the output section.  */
   10383 		      if (rela_normal)
   10384 			irela->r_addend += sec->output_offset;
   10385 		    }
   10386 		  else
   10387 		    {
   10388 		      if (flinfo->indices[r_symndx] == -1)
   10389 			{
   10390 			  unsigned long shlink;
   10391 			  const char *name;
   10392 			  asection *osec;
   10393 			  long indx;
   10394 
   10395 			  if (flinfo->info->strip == strip_all)
   10396 			    {
   10397 			      /* You can't do ld -r -s.  */
   10398 			      bfd_set_error (bfd_error_invalid_operation);
   10399 			      return FALSE;
   10400 			    }
   10401 
   10402 			  /* This symbol was skipped earlier, but
   10403 			     since it is needed by a reloc, we
   10404 			     must output it now.  */
   10405 			  shlink = symtab_hdr->sh_link;
   10406 			  name = (bfd_elf_string_from_elf_section
   10407 				  (input_bfd, shlink, sym.st_name));
   10408 			  if (name == NULL)
   10409 			    return FALSE;
   10410 
   10411 			  osec = sec->output_section;
   10412 			  sym.st_shndx =
   10413 			    _bfd_elf_section_from_bfd_section (output_bfd,
   10414 							       osec);
   10415 			  if (sym.st_shndx == SHN_BAD)
   10416 			    return FALSE;
   10417 
   10418 			  sym.st_value += sec->output_offset;
   10419 			  if (!bfd_link_relocatable (flinfo->info))
   10420 			    {
   10421 			      sym.st_value += osec->vma;
   10422 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
   10423 				{
   10424 				  /* STT_TLS symbols are relative to PT_TLS
   10425 				     segment base.  */
   10426 				  BFD_ASSERT (elf_hash_table (flinfo->info)
   10427 					      ->tls_sec != NULL);
   10428 				  sym.st_value -= (elf_hash_table (flinfo->info)
   10429 						   ->tls_sec->vma);
   10430 				}
   10431 			    }
   10432 
   10433 			  indx = bfd_get_symcount (output_bfd);
   10434 			  ret = elf_link_output_symstrtab (flinfo, name,
   10435 							   &sym, sec,
   10436 							   NULL);
   10437 			  if (ret == 0)
   10438 			    return FALSE;
   10439 			  else if (ret == 1)
   10440 			    flinfo->indices[r_symndx] = indx;
   10441 			  else
   10442 			    abort ();
   10443 			}
   10444 
   10445 		      r_symndx = flinfo->indices[r_symndx];
   10446 		    }
   10447 
   10448 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
   10449 				   | (irela->r_info & r_type_mask));
   10450 		}
   10451 
   10452 	      /* Swap out the relocs.  */
   10453 	      input_rel_hdr = esdi->rel.hdr;
   10454 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
   10455 		{
   10456 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   10457 						     input_rel_hdr,
   10458 						     internal_relocs,
   10459 						     rel_hash_list))
   10460 		    return FALSE;
   10461 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
   10462 				      * bed->s->int_rels_per_ext_rel);
   10463 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
   10464 		}
   10465 
   10466 	      input_rela_hdr = esdi->rela.hdr;
   10467 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
   10468 		{
   10469 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
   10470 						     input_rela_hdr,
   10471 						     internal_relocs,
   10472 						     rela_hash_list))
   10473 		    return FALSE;
   10474 		}
   10475 	    }
   10476 	}
   10477 
   10478       /* Write out the modified section contents.  */
   10479       if (bed->elf_backend_write_section
   10480 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
   10481 						contents))
   10482 	{
   10483 	  /* Section written out.  */
   10484 	}
   10485       else switch (o->sec_info_type)
   10486 	{
   10487 	case SEC_INFO_TYPE_STABS:
   10488 	  if (! (_bfd_write_section_stabs
   10489 		 (output_bfd,
   10490 		  &elf_hash_table (flinfo->info)->stab_info,
   10491 		  o, &elf_section_data (o)->sec_info, contents)))
   10492 	    return FALSE;
   10493 	  break;
   10494 	case SEC_INFO_TYPE_MERGE:
   10495 	  if (! _bfd_write_merged_section (output_bfd, o,
   10496 					   elf_section_data (o)->sec_info))
   10497 	    return FALSE;
   10498 	  break;
   10499 	case SEC_INFO_TYPE_EH_FRAME:
   10500 	  {
   10501 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
   10502 						   o, contents))
   10503 	      return FALSE;
   10504 	  }
   10505 	  break;
   10506 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
   10507 	  {
   10508 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
   10509 							 flinfo->info,
   10510 							 o, contents))
   10511 	      return FALSE;
   10512 	  }
   10513 	  break;
   10514 	default:
   10515 	  {
   10516 	    /* FIXME: octets_per_byte.  */
   10517 	    if (! (o->flags & SEC_EXCLUDE))
   10518 	      {
   10519 		file_ptr offset = (file_ptr) o->output_offset;
   10520 		bfd_size_type todo = o->size;
   10521 		if ((o->flags & SEC_ELF_REVERSE_COPY))
   10522 		  {
   10523 		    /* Reverse-copy input section to output.  */
   10524 		    do
   10525 		      {
   10526 			todo -= address_size;
   10527 			if (! bfd_set_section_contents (output_bfd,
   10528 							o->output_section,
   10529 							contents + todo,
   10530 							offset,
   10531 							address_size))
   10532 			  return FALSE;
   10533 			if (todo == 0)
   10534 			  break;
   10535 			offset += address_size;
   10536 		      }
   10537 		    while (1);
   10538 		  }
   10539 		else if (! bfd_set_section_contents (output_bfd,
   10540 						     o->output_section,
   10541 						     contents,
   10542 						     offset, todo))
   10543 		  return FALSE;
   10544 	      }
   10545 	  }
   10546 	  break;
   10547 	}
   10548     }
   10549 
   10550   return TRUE;
   10551 }
   10552 
   10553 /* Generate a reloc when linking an ELF file.  This is a reloc
   10554    requested by the linker, and does not come from any input file.  This
   10555    is used to build constructor and destructor tables when linking
   10556    with -Ur.  */
   10557 
   10558 static bfd_boolean
   10559 elf_reloc_link_order (bfd *output_bfd,
   10560 		      struct bfd_link_info *info,
   10561 		      asection *output_section,
   10562 		      struct bfd_link_order *link_order)
   10563 {
   10564   reloc_howto_type *howto;
   10565   long indx;
   10566   bfd_vma offset;
   10567   bfd_vma addend;
   10568   struct bfd_elf_section_reloc_data *reldata;
   10569   struct elf_link_hash_entry **rel_hash_ptr;
   10570   Elf_Internal_Shdr *rel_hdr;
   10571   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10572   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
   10573   bfd_byte *erel;
   10574   unsigned int i;
   10575   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
   10576 
   10577   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   10578   if (howto == NULL)
   10579     {
   10580       bfd_set_error (bfd_error_bad_value);
   10581       return FALSE;
   10582     }
   10583 
   10584   addend = link_order->u.reloc.p->addend;
   10585 
   10586   if (esdo->rel.hdr)
   10587     reldata = &esdo->rel;
   10588   else if (esdo->rela.hdr)
   10589     reldata = &esdo->rela;
   10590   else
   10591     {
   10592       reldata = NULL;
   10593       BFD_ASSERT (0);
   10594     }
   10595 
   10596   /* Figure out the symbol index.  */
   10597   rel_hash_ptr = reldata->hashes + reldata->count;
   10598   if (link_order->type == bfd_section_reloc_link_order)
   10599     {
   10600       indx = link_order->u.reloc.p->u.section->target_index;
   10601       BFD_ASSERT (indx != 0);
   10602       *rel_hash_ptr = NULL;
   10603     }
   10604   else
   10605     {
   10606       struct elf_link_hash_entry *h;
   10607 
   10608       /* Treat a reloc against a defined symbol as though it were
   10609 	 actually against the section.  */
   10610       h = ((struct elf_link_hash_entry *)
   10611 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   10612 					 link_order->u.reloc.p->u.name,
   10613 					 FALSE, FALSE, TRUE));
   10614       if (h != NULL
   10615 	  && (h->root.type == bfd_link_hash_defined
   10616 	      || h->root.type == bfd_link_hash_defweak))
   10617 	{
   10618 	  asection *section;
   10619 
   10620 	  section = h->root.u.def.section;
   10621 	  indx = section->output_section->target_index;
   10622 	  *rel_hash_ptr = NULL;
   10623 	  /* It seems that we ought to add the symbol value to the
   10624 	     addend here, but in practice it has already been added
   10625 	     because it was passed to constructor_callback.  */
   10626 	  addend += section->output_section->vma + section->output_offset;
   10627 	}
   10628       else if (h != NULL)
   10629 	{
   10630 	  /* Setting the index to -2 tells elf_link_output_extsym that
   10631 	     this symbol is used by a reloc.  */
   10632 	  h->indx = -2;
   10633 	  *rel_hash_ptr = h;
   10634 	  indx = 0;
   10635 	}
   10636       else
   10637 	{
   10638 	  if (! ((*info->callbacks->unattached_reloc)
   10639 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
   10640 	    return FALSE;
   10641 	  indx = 0;
   10642 	}
   10643     }
   10644 
   10645   /* If this is an inplace reloc, we must write the addend into the
   10646      object file.  */
   10647   if (howto->partial_inplace && addend != 0)
   10648     {
   10649       bfd_size_type size;
   10650       bfd_reloc_status_type rstat;
   10651       bfd_byte *buf;
   10652       bfd_boolean ok;
   10653       const char *sym_name;
   10654 
   10655       size = (bfd_size_type) bfd_get_reloc_size (howto);
   10656       buf = (bfd_byte *) bfd_zmalloc (size);
   10657       if (buf == NULL && size != 0)
   10658 	return FALSE;
   10659       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
   10660       switch (rstat)
   10661 	{
   10662 	case bfd_reloc_ok:
   10663 	  break;
   10664 
   10665 	default:
   10666 	case bfd_reloc_outofrange:
   10667 	  abort ();
   10668 
   10669 	case bfd_reloc_overflow:
   10670 	  if (link_order->type == bfd_section_reloc_link_order)
   10671 	    sym_name = bfd_section_name (output_bfd,
   10672 					 link_order->u.reloc.p->u.section);
   10673 	  else
   10674 	    sym_name = link_order->u.reloc.p->u.name;
   10675 	  if (! ((*info->callbacks->reloc_overflow)
   10676 		 (info, NULL, sym_name, howto->name, addend, NULL,
   10677 		  NULL, (bfd_vma) 0)))
   10678 	    {
   10679 	      free (buf);
   10680 	      return FALSE;
   10681 	    }
   10682 	  break;
   10683 	}
   10684       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   10685 				     link_order->offset, size);
   10686       free (buf);
   10687       if (! ok)
   10688 	return FALSE;
   10689     }
   10690 
   10691   /* The address of a reloc is relative to the section in a
   10692      relocatable file, and is a virtual address in an executable
   10693      file.  */
   10694   offset = link_order->offset;
   10695   if (! bfd_link_relocatable (info))
   10696     offset += output_section->vma;
   10697 
   10698   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
   10699     {
   10700       irel[i].r_offset = offset;
   10701       irel[i].r_info = 0;
   10702       irel[i].r_addend = 0;
   10703     }
   10704   if (bed->s->arch_size == 32)
   10705     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
   10706   else
   10707 #ifdef BFD64
   10708           {
   10709             bfd_uint64_t indx64 = indx;
   10710             irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
   10711           }
   10712 #else
   10713           BFD_FAIL();
   10714 #endif
   10715 
   10716   rel_hdr = reldata->hdr;
   10717   erel = rel_hdr->contents;
   10718   if (rel_hdr->sh_type == SHT_REL)
   10719     {
   10720       erel += reldata->count * bed->s->sizeof_rel;
   10721       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
   10722     }
   10723   else
   10724     {
   10725       irel[0].r_addend = addend;
   10726       erel += reldata->count * bed->s->sizeof_rela;
   10727       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
   10728     }
   10729 
   10730   ++reldata->count;
   10731 
   10732   return TRUE;
   10733 }
   10734 
   10735 
   10736 /* Get the output vma of the section pointed to by the sh_link field.  */
   10737 
   10738 static bfd_vma
   10739 elf_get_linked_section_vma (struct bfd_link_order *p)
   10740 {
   10741   Elf_Internal_Shdr **elf_shdrp;
   10742   asection *s;
   10743   int elfsec;
   10744 
   10745   s = p->u.indirect.section;
   10746   elf_shdrp = elf_elfsections (s->owner);
   10747   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
   10748   elfsec = elf_shdrp[elfsec]->sh_link;
   10749   /* PR 290:
   10750      The Intel C compiler generates SHT_IA_64_UNWIND with
   10751      SHF_LINK_ORDER.  But it doesn't set the sh_link or
   10752      sh_info fields.  Hence we could get the situation
   10753      where elfsec is 0.  */
   10754   if (elfsec == 0)
   10755     {
   10756       const struct elf_backend_data *bed
   10757 	= get_elf_backend_data (s->owner);
   10758       if (bed->link_order_error_handler)
   10759 	bed->link_order_error_handler
   10760 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
   10761       return 0;
   10762     }
   10763   else
   10764     {
   10765       s = elf_shdrp[elfsec]->bfd_section;
   10766       return s->output_section->vma + s->output_offset;
   10767     }
   10768 }
   10769 
   10770 
   10771 /* Compare two sections based on the locations of the sections they are
   10772    linked to.  Used by elf_fixup_link_order.  */
   10773 
   10774 static int
   10775 compare_link_order (const void * a, const void * b)
   10776 {
   10777   bfd_vma apos;
   10778   bfd_vma bpos;
   10779 
   10780   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
   10781   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
   10782   if (apos < bpos)
   10783     return -1;
   10784   return apos > bpos;
   10785 }
   10786 
   10787 
   10788 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
   10789    order as their linked sections.  Returns false if this could not be done
   10790    because an output section includes both ordered and unordered
   10791    sections.  Ideally we'd do this in the linker proper.  */
   10792 
   10793 static bfd_boolean
   10794 elf_fixup_link_order (bfd *abfd, asection *o)
   10795 {
   10796   int seen_linkorder;
   10797   int seen_other;
   10798   int n;
   10799   struct bfd_link_order *p;
   10800   bfd *sub;
   10801   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10802   unsigned elfsec;
   10803   struct bfd_link_order **sections;
   10804   asection *s, *other_sec, *linkorder_sec;
   10805   bfd_vma offset;
   10806 
   10807   other_sec = NULL;
   10808   linkorder_sec = NULL;
   10809   seen_other = 0;
   10810   seen_linkorder = 0;
   10811   for (p = o->map_head.link_order; p != NULL; p = p->next)
   10812     {
   10813       if (p->type == bfd_indirect_link_order)
   10814 	{
   10815 	  s = p->u.indirect.section;
   10816 	  sub = s->owner;
   10817 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   10818 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
   10819 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
   10820 	      && elfsec < elf_numsections (sub)
   10821 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
   10822 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
   10823 	    {
   10824 	      seen_linkorder++;
   10825 	      linkorder_sec = s;
   10826 	    }
   10827 	  else
   10828 	    {
   10829 	      seen_other++;
   10830 	      other_sec = s;
   10831 	    }
   10832 	}
   10833       else
   10834 	seen_other++;
   10835 
   10836       if (seen_other && seen_linkorder)
   10837 	{
   10838 	  if (other_sec && linkorder_sec)
   10839 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
   10840 				   o, linkorder_sec,
   10841 				   linkorder_sec->owner, other_sec,
   10842 				   other_sec->owner);
   10843 	  else
   10844 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
   10845 				   o);
   10846 	  bfd_set_error (bfd_error_bad_value);
   10847 	  return FALSE;
   10848 	}
   10849     }
   10850 
   10851   if (!seen_linkorder)
   10852     return TRUE;
   10853 
   10854   sections = (struct bfd_link_order **)
   10855     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
   10856   if (sections == NULL)
   10857     return FALSE;
   10858   seen_linkorder = 0;
   10859 
   10860   for (p = o->map_head.link_order; p != NULL; p = p->next)
   10861     {
   10862       sections[seen_linkorder++] = p;
   10863     }
   10864   /* Sort the input sections in the order of their linked section.  */
   10865   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
   10866 	 compare_link_order);
   10867 
   10868   /* Change the offsets of the sections.  */
   10869   offset = 0;
   10870   for (n = 0; n < seen_linkorder; n++)
   10871     {
   10872       s = sections[n]->u.indirect.section;
   10873       offset &= ~(bfd_vma) 0 << s->alignment_power;
   10874       s->output_offset = offset;
   10875       sections[n]->offset = offset;
   10876       /* FIXME: octets_per_byte.  */
   10877       offset += sections[n]->size;
   10878     }
   10879 
   10880   free (sections);
   10881   return TRUE;
   10882 }
   10883 
   10884 static void
   10885 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
   10886 {
   10887   asection *o;
   10888 
   10889   if (flinfo->symstrtab != NULL)
   10890     _bfd_elf_strtab_free (flinfo->symstrtab);
   10891   if (flinfo->contents != NULL)
   10892     free (flinfo->contents);
   10893   if (flinfo->external_relocs != NULL)
   10894     free (flinfo->external_relocs);
   10895   if (flinfo->internal_relocs != NULL)
   10896     free (flinfo->internal_relocs);
   10897   if (flinfo->external_syms != NULL)
   10898     free (flinfo->external_syms);
   10899   if (flinfo->locsym_shndx != NULL)
   10900     free (flinfo->locsym_shndx);
   10901   if (flinfo->internal_syms != NULL)
   10902     free (flinfo->internal_syms);
   10903   if (flinfo->indices != NULL)
   10904     free (flinfo->indices);
   10905   if (flinfo->sections != NULL)
   10906     free (flinfo->sections);
   10907   if (flinfo->symshndxbuf != NULL)
   10908     free (flinfo->symshndxbuf);
   10909   for (o = obfd->sections; o != NULL; o = o->next)
   10910     {
   10911       struct bfd_elf_section_data *esdo = elf_section_data (o);
   10912       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
   10913 	free (esdo->rel.hashes);
   10914       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
   10915 	free (esdo->rela.hashes);
   10916     }
   10917 }
   10918 
   10919 /* Do the final step of an ELF link.  */
   10920 
   10921 bfd_boolean
   10922 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   10923 {
   10924   bfd_boolean dynamic;
   10925   bfd_boolean emit_relocs;
   10926   bfd *dynobj;
   10927   struct elf_final_link_info flinfo;
   10928   asection *o;
   10929   struct bfd_link_order *p;
   10930   bfd *sub;
   10931   bfd_size_type max_contents_size;
   10932   bfd_size_type max_external_reloc_size;
   10933   bfd_size_type max_internal_reloc_count;
   10934   bfd_size_type max_sym_count;
   10935   bfd_size_type max_sym_shndx_count;
   10936   Elf_Internal_Sym elfsym;
   10937   unsigned int i;
   10938   Elf_Internal_Shdr *symtab_hdr;
   10939   Elf_Internal_Shdr *symtab_shndx_hdr;
   10940   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10941   struct elf_outext_info eoinfo;
   10942   bfd_boolean merged;
   10943   size_t relativecount = 0;
   10944   asection *reldyn = 0;
   10945   bfd_size_type amt;
   10946   asection *attr_section = NULL;
   10947   bfd_vma attr_size = 0;
   10948   const char *std_attrs_section;
   10949 
   10950   if (! is_elf_hash_table (info->hash))
   10951     return FALSE;
   10952 
   10953   if (bfd_link_pic (info))
   10954     abfd->flags |= DYNAMIC;
   10955 
   10956   dynamic = elf_hash_table (info)->dynamic_sections_created;
   10957   dynobj = elf_hash_table (info)->dynobj;
   10958 
   10959   emit_relocs = (bfd_link_relocatable (info)
   10960 		 || info->emitrelocations);
   10961 
   10962   flinfo.info = info;
   10963   flinfo.output_bfd = abfd;
   10964   flinfo.symstrtab = _bfd_elf_strtab_init ();
   10965   if (flinfo.symstrtab == NULL)
   10966     return FALSE;
   10967 
   10968   if (! dynamic)
   10969     {
   10970       flinfo.hash_sec = NULL;
   10971       flinfo.symver_sec = NULL;
   10972     }
   10973   else
   10974     {
   10975       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
   10976       /* Note that dynsym_sec can be NULL (on VMS).  */
   10977       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
   10978       /* Note that it is OK if symver_sec is NULL.  */
   10979     }
   10980 
   10981   flinfo.contents = NULL;
   10982   flinfo.external_relocs = NULL;
   10983   flinfo.internal_relocs = NULL;
   10984   flinfo.external_syms = NULL;
   10985   flinfo.locsym_shndx = NULL;
   10986   flinfo.internal_syms = NULL;
   10987   flinfo.indices = NULL;
   10988   flinfo.sections = NULL;
   10989   flinfo.symshndxbuf = NULL;
   10990   flinfo.filesym_count = 0;
   10991 
   10992   /* The object attributes have been merged.  Remove the input
   10993      sections from the link, and set the contents of the output
   10994      secton.  */
   10995   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   10996   for (o = abfd->sections; o != NULL; o = o->next)
   10997     {
   10998       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
   10999 	  || strcmp (o->name, ".gnu.attributes") == 0)
   11000 	{
   11001 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   11002 	    {
   11003 	      asection *input_section;
   11004 
   11005 	      if (p->type != bfd_indirect_link_order)
   11006 		continue;
   11007 	      input_section = p->u.indirect.section;
   11008 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   11009 		 elf_link_input_bfd ignores this section.  */
   11010 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   11011 	    }
   11012 
   11013 	  attr_size = bfd_elf_obj_attr_size (abfd);
   11014 	  if (attr_size)
   11015 	    {
   11016 	      bfd_set_section_size (abfd, o, attr_size);
   11017 	      attr_section = o;
   11018 	      /* Skip this section later on.  */
   11019 	      o->map_head.link_order = NULL;
   11020 	    }
   11021 	  else
   11022 	    o->flags |= SEC_EXCLUDE;
   11023 	}
   11024     }
   11025 
   11026   /* Count up the number of relocations we will output for each output
   11027      section, so that we know the sizes of the reloc sections.  We
   11028      also figure out some maximum sizes.  */
   11029   max_contents_size = 0;
   11030   max_external_reloc_size = 0;
   11031   max_internal_reloc_count = 0;
   11032   max_sym_count = 0;
   11033   max_sym_shndx_count = 0;
   11034   merged = FALSE;
   11035   for (o = abfd->sections; o != NULL; o = o->next)
   11036     {
   11037       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11038       o->reloc_count = 0;
   11039 
   11040       for (p = o->map_head.link_order; p != NULL; p = p->next)
   11041 	{
   11042 	  unsigned int reloc_count = 0;
   11043 	  struct bfd_elf_section_data *esdi = NULL;
   11044 
   11045 	  if (p->type == bfd_section_reloc_link_order
   11046 	      || p->type == bfd_symbol_reloc_link_order)
   11047 	    reloc_count = 1;
   11048 	  else if (p->type == bfd_indirect_link_order)
   11049 	    {
   11050 	      asection *sec;
   11051 
   11052 	      sec = p->u.indirect.section;
   11053 	      esdi = elf_section_data (sec);
   11054 
   11055 	      /* Mark all sections which are to be included in the
   11056 		 link.  This will normally be every section.  We need
   11057 		 to do this so that we can identify any sections which
   11058 		 the linker has decided to not include.  */
   11059 	      sec->linker_mark = TRUE;
   11060 
   11061 	      if (sec->flags & SEC_MERGE)
   11062 		merged = TRUE;
   11063 
   11064 	      if (esdo->this_hdr.sh_type == SHT_REL
   11065 		  || esdo->this_hdr.sh_type == SHT_RELA)
   11066 		/* Some backends use reloc_count in relocation sections
   11067 		   to count particular types of relocs.  Of course,
   11068 		   reloc sections themselves can't have relocations.  */
   11069 		reloc_count = 0;
   11070 	      else if (emit_relocs)
   11071 		reloc_count = sec->reloc_count;
   11072 	      else if (bed->elf_backend_count_relocs)
   11073 		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
   11074 
   11075 	      if (sec->rawsize > max_contents_size)
   11076 		max_contents_size = sec->rawsize;
   11077 	      if (sec->size > max_contents_size)
   11078 		max_contents_size = sec->size;
   11079 
   11080 	      /* We are interested in just local symbols, not all
   11081 		 symbols.  */
   11082 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
   11083 		  && (sec->owner->flags & DYNAMIC) == 0)
   11084 		{
   11085 		  size_t sym_count;
   11086 
   11087 		  if (elf_bad_symtab (sec->owner))
   11088 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
   11089 				 / bed->s->sizeof_sym);
   11090 		  else
   11091 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
   11092 
   11093 		  if (sym_count > max_sym_count)
   11094 		    max_sym_count = sym_count;
   11095 
   11096 		  if (sym_count > max_sym_shndx_count
   11097 		      && elf_symtab_shndx_list (sec->owner) != NULL)
   11098 		    max_sym_shndx_count = sym_count;
   11099 
   11100 		  if ((sec->flags & SEC_RELOC) != 0)
   11101 		    {
   11102 		      size_t ext_size = 0;
   11103 
   11104 		      if (esdi->rel.hdr != NULL)
   11105 			ext_size = esdi->rel.hdr->sh_size;
   11106 		      if (esdi->rela.hdr != NULL)
   11107 			ext_size += esdi->rela.hdr->sh_size;
   11108 
   11109 		      if (ext_size > max_external_reloc_size)
   11110 			max_external_reloc_size = ext_size;
   11111 		      if (sec->reloc_count > max_internal_reloc_count)
   11112 			max_internal_reloc_count = sec->reloc_count;
   11113 		    }
   11114 		}
   11115 	    }
   11116 
   11117 	  if (reloc_count == 0)
   11118 	    continue;
   11119 
   11120 	  o->reloc_count += reloc_count;
   11121 
   11122 	  if (p->type == bfd_indirect_link_order && emit_relocs)
   11123 	    {
   11124 	      if (esdi->rel.hdr)
   11125 		esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
   11126 	      if (esdi->rela.hdr)
   11127 		esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
   11128 	    }
   11129 	  else
   11130 	    {
   11131 	      if (o->use_rela_p)
   11132 		esdo->rela.count += reloc_count;
   11133 	      else
   11134 		esdo->rel.count += reloc_count;
   11135 	    }
   11136 	}
   11137 
   11138       if (o->reloc_count > 0)
   11139 	o->flags |= SEC_RELOC;
   11140       else
   11141 	{
   11142 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
   11143 	     set it (this is probably a bug) and if it is set
   11144 	     assign_section_numbers will create a reloc section.  */
   11145 	  o->flags &=~ SEC_RELOC;
   11146 	}
   11147 
   11148       /* If the SEC_ALLOC flag is not set, force the section VMA to
   11149 	 zero.  This is done in elf_fake_sections as well, but forcing
   11150 	 the VMA to 0 here will ensure that relocs against these
   11151 	 sections are handled correctly.  */
   11152       if ((o->flags & SEC_ALLOC) == 0
   11153 	  && ! o->user_set_vma)
   11154 	o->vma = 0;
   11155     }
   11156 
   11157   if (! bfd_link_relocatable (info) && merged)
   11158     elf_link_hash_traverse (elf_hash_table (info),
   11159 			    _bfd_elf_link_sec_merge_syms, abfd);
   11160 
   11161   /* Figure out the file positions for everything but the symbol table
   11162      and the relocs.  We set symcount to force assign_section_numbers
   11163      to create a symbol table.  */
   11164   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
   11165   BFD_ASSERT (! abfd->output_has_begun);
   11166   if (! _bfd_elf_compute_section_file_positions (abfd, info))
   11167     goto error_return;
   11168 
   11169   /* Set sizes, and assign file positions for reloc sections.  */
   11170   for (o = abfd->sections; o != NULL; o = o->next)
   11171     {
   11172       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11173       if ((o->flags & SEC_RELOC) != 0)
   11174 	{
   11175 	  if (esdo->rel.hdr
   11176 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
   11177 	    goto error_return;
   11178 
   11179 	  if (esdo->rela.hdr
   11180 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
   11181 	    goto error_return;
   11182 	}
   11183 
   11184       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
   11185 	 to count upwards while actually outputting the relocations.  */
   11186       esdo->rel.count = 0;
   11187       esdo->rela.count = 0;
   11188 
   11189       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
   11190 	{
   11191 	  /* Cache the section contents so that they can be compressed
   11192 	     later.  Use bfd_malloc since it will be freed by
   11193 	     bfd_compress_section_contents.  */
   11194 	  unsigned char *contents = esdo->this_hdr.contents;
   11195 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
   11196 	    abort ();
   11197 	  contents
   11198 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
   11199 	  if (contents == NULL)
   11200 	    goto error_return;
   11201 	  esdo->this_hdr.contents = contents;
   11202 	}
   11203     }
   11204 
   11205   /* We have now assigned file positions for all the sections except
   11206      .symtab, .strtab, and non-loaded reloc sections.  We start the
   11207      .symtab section at the current file position, and write directly
   11208      to it.  We build the .strtab section in memory.  */
   11209   bfd_get_symcount (abfd) = 0;
   11210   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11211   /* sh_name is set in prep_headers.  */
   11212   symtab_hdr->sh_type = SHT_SYMTAB;
   11213   /* sh_flags, sh_addr and sh_size all start off zero.  */
   11214   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   11215   /* sh_link is set in assign_section_numbers.  */
   11216   /* sh_info is set below.  */
   11217   /* sh_offset is set just below.  */
   11218   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   11219 
   11220   if (max_sym_count < 20)
   11221     max_sym_count = 20;
   11222   elf_hash_table (info)->strtabsize = max_sym_count;
   11223   amt = max_sym_count * sizeof (struct elf_sym_strtab);
   11224   elf_hash_table (info)->strtab
   11225     = (struct elf_sym_strtab *) bfd_malloc (amt);
   11226   if (elf_hash_table (info)->strtab == NULL)
   11227     goto error_return;
   11228   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
   11229   flinfo.symshndxbuf
   11230     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
   11231        ? (Elf_External_Sym_Shndx *) -1 : NULL);
   11232 
   11233   if (info->strip != strip_all || emit_relocs)
   11234     {
   11235       file_ptr off = elf_next_file_pos (abfd);
   11236 
   11237       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
   11238 
   11239       /* Note that at this point elf_next_file_pos (abfd) is
   11240 	 incorrect.  We do not yet know the size of the .symtab section.
   11241 	 We correct next_file_pos below, after we do know the size.  */
   11242 
   11243       /* Start writing out the symbol table.  The first symbol is always a
   11244 	 dummy symbol.  */
   11245       elfsym.st_value = 0;
   11246       elfsym.st_size = 0;
   11247       elfsym.st_info = 0;
   11248       elfsym.st_other = 0;
   11249       elfsym.st_shndx = SHN_UNDEF;
   11250       elfsym.st_target_internal = 0;
   11251       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
   11252 				     bfd_und_section_ptr, NULL) != 1)
   11253 	goto error_return;
   11254 
   11255       /* Output a symbol for each section.  We output these even if we are
   11256 	 discarding local symbols, since they are used for relocs.  These
   11257 	 symbols have no names.  We store the index of each one in the
   11258 	 index field of the section, so that we can find it again when
   11259 	 outputting relocs.  */
   11260 
   11261       elfsym.st_size = 0;
   11262       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   11263       elfsym.st_other = 0;
   11264       elfsym.st_value = 0;
   11265       elfsym.st_target_internal = 0;
   11266       for (i = 1; i < elf_numsections (abfd); i++)
   11267 	{
   11268 	  o = bfd_section_from_elf_index (abfd, i);
   11269 	  if (o != NULL)
   11270 	    {
   11271 	      o->target_index = bfd_get_symcount (abfd);
   11272 	      elfsym.st_shndx = i;
   11273 	      if (!bfd_link_relocatable (info))
   11274 		elfsym.st_value = o->vma;
   11275 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
   11276 					     NULL) != 1)
   11277 		goto error_return;
   11278 	    }
   11279 	}
   11280     }
   11281 
   11282   /* Allocate some memory to hold information read in from the input
   11283      files.  */
   11284   if (max_contents_size != 0)
   11285     {
   11286       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   11287       if (flinfo.contents == NULL)
   11288 	goto error_return;
   11289     }
   11290 
   11291   if (max_external_reloc_size != 0)
   11292     {
   11293       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
   11294       if (flinfo.external_relocs == NULL)
   11295 	goto error_return;
   11296     }
   11297 
   11298   if (max_internal_reloc_count != 0)
   11299     {
   11300       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
   11301       amt *= sizeof (Elf_Internal_Rela);
   11302       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
   11303       if (flinfo.internal_relocs == NULL)
   11304 	goto error_return;
   11305     }
   11306 
   11307   if (max_sym_count != 0)
   11308     {
   11309       amt = max_sym_count * bed->s->sizeof_sym;
   11310       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
   11311       if (flinfo.external_syms == NULL)
   11312 	goto error_return;
   11313 
   11314       amt = max_sym_count * sizeof (Elf_Internal_Sym);
   11315       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
   11316       if (flinfo.internal_syms == NULL)
   11317 	goto error_return;
   11318 
   11319       amt = max_sym_count * sizeof (long);
   11320       flinfo.indices = (long int *) bfd_malloc (amt);
   11321       if (flinfo.indices == NULL)
   11322 	goto error_return;
   11323 
   11324       amt = max_sym_count * sizeof (asection *);
   11325       flinfo.sections = (asection **) bfd_malloc (amt);
   11326       if (flinfo.sections == NULL)
   11327 	goto error_return;
   11328     }
   11329 
   11330   if (max_sym_shndx_count != 0)
   11331     {
   11332       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
   11333       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   11334       if (flinfo.locsym_shndx == NULL)
   11335 	goto error_return;
   11336     }
   11337 
   11338   if (elf_hash_table (info)->tls_sec)
   11339     {
   11340       bfd_vma base, end = 0;
   11341       asection *sec;
   11342 
   11343       for (sec = elf_hash_table (info)->tls_sec;
   11344 	   sec && (sec->flags & SEC_THREAD_LOCAL);
   11345 	   sec = sec->next)
   11346 	{
   11347 	  bfd_size_type size = sec->size;
   11348 
   11349 	  if (size == 0
   11350 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
   11351 	    {
   11352 	      struct bfd_link_order *ord = sec->map_tail.link_order;
   11353 
   11354 	      if (ord != NULL)
   11355 		size = ord->offset + ord->size;
   11356 	    }
   11357 	  end = sec->vma + size;
   11358 	}
   11359       base = elf_hash_table (info)->tls_sec->vma;
   11360       /* Only align end of TLS section if static TLS doesn't have special
   11361 	 alignment requirements.  */
   11362       if (bed->static_tls_alignment == 1)
   11363 	end = align_power (end,
   11364 			   elf_hash_table (info)->tls_sec->alignment_power);
   11365       elf_hash_table (info)->tls_size = end - base;
   11366     }
   11367 
   11368   /* Reorder SHF_LINK_ORDER sections.  */
   11369   for (o = abfd->sections; o != NULL; o = o->next)
   11370     {
   11371       if (!elf_fixup_link_order (abfd, o))
   11372 	return FALSE;
   11373     }
   11374 
   11375   if (!_bfd_elf_fixup_eh_frame_hdr (info))
   11376     return FALSE;
   11377 
   11378   /* Since ELF permits relocations to be against local symbols, we
   11379      must have the local symbols available when we do the relocations.
   11380      Since we would rather only read the local symbols once, and we
   11381      would rather not keep them in memory, we handle all the
   11382      relocations for a single input file at the same time.
   11383 
   11384      Unfortunately, there is no way to know the total number of local
   11385      symbols until we have seen all of them, and the local symbol
   11386      indices precede the global symbol indices.  This means that when
   11387      we are generating relocatable output, and we see a reloc against
   11388      a global symbol, we can not know the symbol index until we have
   11389      finished examining all the local symbols to see which ones we are
   11390      going to output.  To deal with this, we keep the relocations in
   11391      memory, and don't output them until the end of the link.  This is
   11392      an unfortunate waste of memory, but I don't see a good way around
   11393      it.  Fortunately, it only happens when performing a relocatable
   11394      link, which is not the common case.  FIXME: If keep_memory is set
   11395      we could write the relocs out and then read them again; I don't
   11396      know how bad the memory loss will be.  */
   11397 
   11398   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   11399     sub->output_has_begun = FALSE;
   11400   for (o = abfd->sections; o != NULL; o = o->next)
   11401     {
   11402       for (p = o->map_head.link_order; p != NULL; p = p->next)
   11403 	{
   11404 	  if (p->type == bfd_indirect_link_order
   11405 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
   11406 		  == bfd_target_elf_flavour)
   11407 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
   11408 	    {
   11409 	      if (! sub->output_has_begun)
   11410 		{
   11411 		  if (! elf_link_input_bfd (&flinfo, sub))
   11412 		    goto error_return;
   11413 		  sub->output_has_begun = TRUE;
   11414 		}
   11415 	    }
   11416 	  else if (p->type == bfd_section_reloc_link_order
   11417 		   || p->type == bfd_symbol_reloc_link_order)
   11418 	    {
   11419 	      if (! elf_reloc_link_order (abfd, info, o, p))
   11420 		goto error_return;
   11421 	    }
   11422 	  else
   11423 	    {
   11424 	      if (! _bfd_default_link_order (abfd, info, o, p))
   11425 		{
   11426 		  if (p->type == bfd_indirect_link_order
   11427 		      && (bfd_get_flavour (sub)
   11428 			  == bfd_target_elf_flavour)
   11429 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
   11430 			  != bed->s->elfclass))
   11431 		    {
   11432 		      const char *iclass, *oclass;
   11433 
   11434 		      if (bed->s->elfclass == ELFCLASS64)
   11435 			{
   11436 			  iclass = "ELFCLASS32";
   11437 			  oclass = "ELFCLASS64";
   11438 			}
   11439 		      else
   11440 			{
   11441 			  iclass = "ELFCLASS64";
   11442 			  oclass = "ELFCLASS32";
   11443 			}
   11444 
   11445 		      bfd_set_error (bfd_error_wrong_format);
   11446 		      (*_bfd_error_handler)
   11447 			(_("%B: file class %s incompatible with %s"),
   11448 			 sub, iclass, oclass);
   11449 		    }
   11450 
   11451 		  goto error_return;
   11452 		}
   11453 	    }
   11454 	}
   11455     }
   11456 
   11457   /* Free symbol buffer if needed.  */
   11458   if (!info->reduce_memory_overheads)
   11459     {
   11460       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   11461 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
   11462 	    && elf_tdata (sub)->symbuf)
   11463 	  {
   11464 	    free (elf_tdata (sub)->symbuf);
   11465 	    elf_tdata (sub)->symbuf = NULL;
   11466 	  }
   11467     }
   11468 
   11469   /* Output any global symbols that got converted to local in a
   11470      version script or due to symbol visibility.  We do this in a
   11471      separate step since ELF requires all local symbols to appear
   11472      prior to any global symbols.  FIXME: We should only do this if
   11473      some global symbols were, in fact, converted to become local.
   11474      FIXME: Will this work correctly with the Irix 5 linker?  */
   11475   eoinfo.failed = FALSE;
   11476   eoinfo.flinfo = &flinfo;
   11477   eoinfo.localsyms = TRUE;
   11478   eoinfo.file_sym_done = FALSE;
   11479   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   11480   if (eoinfo.failed)
   11481     return FALSE;
   11482 
   11483   /* If backend needs to output some local symbols not present in the hash
   11484      table, do it now.  */
   11485   if (bed->elf_backend_output_arch_local_syms
   11486       && (info->strip != strip_all || emit_relocs))
   11487     {
   11488       typedef int (*out_sym_func)
   11489 	(void *, const char *, Elf_Internal_Sym *, asection *,
   11490 	 struct elf_link_hash_entry *);
   11491 
   11492       if (! ((*bed->elf_backend_output_arch_local_syms)
   11493 	     (abfd, info, &flinfo,
   11494 	      (out_sym_func) elf_link_output_symstrtab)))
   11495 	return FALSE;
   11496     }
   11497 
   11498   /* That wrote out all the local symbols.  Finish up the symbol table
   11499      with the global symbols. Even if we want to strip everything we
   11500      can, we still need to deal with those global symbols that got
   11501      converted to local in a version script.  */
   11502 
   11503   /* The sh_info field records the index of the first non local symbol.  */
   11504   symtab_hdr->sh_info = bfd_get_symcount (abfd);
   11505 
   11506   if (dynamic
   11507       && elf_hash_table (info)->dynsym != NULL
   11508       && (elf_hash_table (info)->dynsym->output_section
   11509 	  != bfd_abs_section_ptr))
   11510     {
   11511       Elf_Internal_Sym sym;
   11512       bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
   11513       long last_local = 0;
   11514 
   11515       /* Write out the section symbols for the output sections.  */
   11516       if (bfd_link_pic (info)
   11517 	  || elf_hash_table (info)->is_relocatable_executable)
   11518 	{
   11519 	  asection *s;
   11520 
   11521 	  sym.st_size = 0;
   11522 	  sym.st_name = 0;
   11523 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   11524 	  sym.st_other = 0;
   11525 	  sym.st_target_internal = 0;
   11526 
   11527 	  for (s = abfd->sections; s != NULL; s = s->next)
   11528 	    {
   11529 	      int indx;
   11530 	      bfd_byte *dest;
   11531 	      long dynindx;
   11532 
   11533 	      dynindx = elf_section_data (s)->dynindx;
   11534 	      if (dynindx <= 0)
   11535 		continue;
   11536 	      indx = elf_section_data (s)->this_idx;
   11537 	      BFD_ASSERT (indx > 0);
   11538 	      sym.st_shndx = indx;
   11539 	      if (! check_dynsym (abfd, &sym))
   11540 		return FALSE;
   11541 	      sym.st_value = s->vma;
   11542 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
   11543 	      if (last_local < dynindx)
   11544 		last_local = dynindx;
   11545 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   11546 	    }
   11547 	}
   11548 
   11549       /* Write out the local dynsyms.  */
   11550       if (elf_hash_table (info)->dynlocal)
   11551 	{
   11552 	  struct elf_link_local_dynamic_entry *e;
   11553 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
   11554 	    {
   11555 	      asection *s;
   11556 	      bfd_byte *dest;
   11557 
   11558 	      /* Copy the internal symbol and turn off visibility.
   11559 		 Note that we saved a word of storage and overwrote
   11560 		 the original st_name with the dynstr_index.  */
   11561 	      sym = e->isym;
   11562 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
   11563 
   11564 	      s = bfd_section_from_elf_index (e->input_bfd,
   11565 					      e->isym.st_shndx);
   11566 	      if (s != NULL)
   11567 		{
   11568 		  sym.st_shndx =
   11569 		    elf_section_data (s->output_section)->this_idx;
   11570 		  if (! check_dynsym (abfd, &sym))
   11571 		    return FALSE;
   11572 		  sym.st_value = (s->output_section->vma
   11573 				  + s->output_offset
   11574 				  + e->isym.st_value);
   11575 		}
   11576 
   11577 	      if (last_local < e->dynindx)
   11578 		last_local = e->dynindx;
   11579 
   11580 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
   11581 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
   11582 	    }
   11583 	}
   11584 
   11585       elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
   11586 	last_local + 1;
   11587     }
   11588 
   11589   /* We get the global symbols from the hash table.  */
   11590   eoinfo.failed = FALSE;
   11591   eoinfo.localsyms = FALSE;
   11592   eoinfo.flinfo = &flinfo;
   11593   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   11594   if (eoinfo.failed)
   11595     return FALSE;
   11596 
   11597   /* If backend needs to output some symbols not present in the hash
   11598      table, do it now.  */
   11599   if (bed->elf_backend_output_arch_syms
   11600       && (info->strip != strip_all || emit_relocs))
   11601     {
   11602       typedef int (*out_sym_func)
   11603 	(void *, const char *, Elf_Internal_Sym *, asection *,
   11604 	 struct elf_link_hash_entry *);
   11605 
   11606       if (! ((*bed->elf_backend_output_arch_syms)
   11607 	     (abfd, info, &flinfo,
   11608 	      (out_sym_func) elf_link_output_symstrtab)))
   11609 	return FALSE;
   11610     }
   11611 
   11612   /* Finalize the .strtab section.  */
   11613   _bfd_elf_strtab_finalize (flinfo.symstrtab);
   11614 
   11615   /* Swap out the .strtab section. */
   11616   if (!elf_link_swap_symbols_out (&flinfo))
   11617     return FALSE;
   11618 
   11619   /* Now we know the size of the symtab section.  */
   11620   if (bfd_get_symcount (abfd) > 0)
   11621     {
   11622       /* Finish up and write out the symbol string table (.strtab)
   11623 	 section.  */
   11624       Elf_Internal_Shdr *symstrtab_hdr;
   11625       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
   11626 
   11627       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   11628       if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
   11629 	{
   11630 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   11631 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   11632 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   11633 	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
   11634 	  symtab_shndx_hdr->sh_size = amt;
   11635 
   11636 	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
   11637 							   off, TRUE);
   11638 
   11639 	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
   11640 	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
   11641 	    return FALSE;
   11642 	}
   11643 
   11644       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   11645       /* sh_name was set in prep_headers.  */
   11646       symstrtab_hdr->sh_type = SHT_STRTAB;
   11647       symstrtab_hdr->sh_flags = 0;
   11648       symstrtab_hdr->sh_addr = 0;
   11649       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
   11650       symstrtab_hdr->sh_entsize = 0;
   11651       symstrtab_hdr->sh_link = 0;
   11652       symstrtab_hdr->sh_info = 0;
   11653       /* sh_offset is set just below.  */
   11654       symstrtab_hdr->sh_addralign = 1;
   11655 
   11656       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
   11657 						       off, TRUE);
   11658       elf_next_file_pos (abfd) = off;
   11659 
   11660       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
   11661 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
   11662 	return FALSE;
   11663     }
   11664 
   11665   /* Adjust the relocs to have the correct symbol indices.  */
   11666   for (o = abfd->sections; o != NULL; o = o->next)
   11667     {
   11668       struct bfd_elf_section_data *esdo = elf_section_data (o);
   11669       bfd_boolean sort;
   11670       if ((o->flags & SEC_RELOC) == 0)
   11671 	continue;
   11672 
   11673       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
   11674       if (esdo->rel.hdr != NULL
   11675 	  && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
   11676 	return FALSE;
   11677       if (esdo->rela.hdr != NULL
   11678 	  && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
   11679 	return FALSE;
   11680 
   11681       /* Set the reloc_count field to 0 to prevent write_relocs from
   11682 	 trying to swap the relocs out itself.  */
   11683       o->reloc_count = 0;
   11684     }
   11685 
   11686   if (dynamic && info->combreloc && dynobj != NULL)
   11687     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
   11688 
   11689   /* If we are linking against a dynamic object, or generating a
   11690      shared library, finish up the dynamic linking information.  */
   11691   if (dynamic)
   11692     {
   11693       bfd_byte *dyncon, *dynconend;
   11694 
   11695       /* Fix up .dynamic entries.  */
   11696       o = bfd_get_linker_section (dynobj, ".dynamic");
   11697       BFD_ASSERT (o != NULL);
   11698 
   11699       dyncon = o->contents;
   11700       dynconend = o->contents + o->size;
   11701       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   11702 	{
   11703 	  Elf_Internal_Dyn dyn;
   11704 	  const char *name;
   11705 	  unsigned int type;
   11706 
   11707 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   11708 
   11709 	  switch (dyn.d_tag)
   11710 	    {
   11711 	    default:
   11712 	      continue;
   11713 	    case DT_NULL:
   11714 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
   11715 		{
   11716 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
   11717 		    {
   11718 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
   11719 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
   11720 		    default: continue;
   11721 		    }
   11722 		  dyn.d_un.d_val = relativecount;
   11723 		  relativecount = 0;
   11724 		  break;
   11725 		}
   11726 	      continue;
   11727 
   11728 	    case DT_INIT:
   11729 	      name = info->init_function;
   11730 	      goto get_sym;
   11731 	    case DT_FINI:
   11732 	      name = info->fini_function;
   11733 	    get_sym:
   11734 	      {
   11735 		struct elf_link_hash_entry *h;
   11736 
   11737 		h = elf_link_hash_lookup (elf_hash_table (info), name,
   11738 					  FALSE, FALSE, TRUE);
   11739 		if (h != NULL
   11740 		    && (h->root.type == bfd_link_hash_defined
   11741 			|| h->root.type == bfd_link_hash_defweak))
   11742 		  {
   11743 		    dyn.d_un.d_ptr = h->root.u.def.value;
   11744 		    o = h->root.u.def.section;
   11745 		    if (o->output_section != NULL)
   11746 		      dyn.d_un.d_ptr += (o->output_section->vma
   11747 					 + o->output_offset);
   11748 		    else
   11749 		      {
   11750 			/* The symbol is imported from another shared
   11751 			   library and does not apply to this one.  */
   11752 			dyn.d_un.d_ptr = 0;
   11753 		      }
   11754 		    break;
   11755 		  }
   11756 	      }
   11757 	      continue;
   11758 
   11759 	    case DT_PREINIT_ARRAYSZ:
   11760 	      name = ".preinit_array";
   11761 	      goto get_size;
   11762 	    case DT_INIT_ARRAYSZ:
   11763 	      name = ".init_array";
   11764 	      goto get_size;
   11765 	    case DT_FINI_ARRAYSZ:
   11766 	      name = ".fini_array";
   11767 	    get_size:
   11768 	      o = bfd_get_section_by_name (abfd, name);
   11769 	      if (o == NULL)
   11770 		{
   11771 		  (*_bfd_error_handler)
   11772 		    (_("%B: could not find output section %s"), abfd, name);
   11773 		  goto error_return;
   11774 		}
   11775 	      if (o->size == 0)
   11776 		(*_bfd_error_handler)
   11777 		  (_("warning: %s section has zero size"), name);
   11778 	      dyn.d_un.d_val = o->size;
   11779 	      break;
   11780 
   11781 	    case DT_PREINIT_ARRAY:
   11782 	      name = ".preinit_array";
   11783 	      goto get_vma;
   11784 	    case DT_INIT_ARRAY:
   11785 	      name = ".init_array";
   11786 	      goto get_vma;
   11787 	    case DT_FINI_ARRAY:
   11788 	      name = ".fini_array";
   11789 	      goto get_vma;
   11790 
   11791 	    case DT_HASH:
   11792 	      name = ".hash";
   11793 	      goto get_vma;
   11794 	    case DT_GNU_HASH:
   11795 	      name = ".gnu.hash";
   11796 	      goto get_vma;
   11797 	    case DT_STRTAB:
   11798 	      name = ".dynstr";
   11799 	      goto get_vma;
   11800 	    case DT_SYMTAB:
   11801 	      name = ".dynsym";
   11802 	      goto get_vma;
   11803 	    case DT_VERDEF:
   11804 	      name = ".gnu.version_d";
   11805 	      goto get_vma;
   11806 	    case DT_VERNEED:
   11807 	      name = ".gnu.version_r";
   11808 	      goto get_vma;
   11809 	    case DT_VERSYM:
   11810 	      name = ".gnu.version";
   11811 	    get_vma:
   11812 	      o = bfd_get_section_by_name (abfd, name);
   11813 	      if (o == NULL)
   11814 		{
   11815 		  (*_bfd_error_handler)
   11816 		    (_("%B: could not find output section %s"), abfd, name);
   11817 		  goto error_return;
   11818 		}
   11819 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
   11820 		{
   11821 		  (*_bfd_error_handler)
   11822 		    (_("warning: section '%s' is being made into a note"), name);
   11823 		  bfd_set_error (bfd_error_nonrepresentable_section);
   11824 		  goto error_return;
   11825 		}
   11826 	      dyn.d_un.d_ptr = o->vma;
   11827 	      break;
   11828 
   11829 	    case DT_REL:
   11830 	    case DT_RELA:
   11831 	    case DT_RELSZ:
   11832 	    case DT_RELASZ:
   11833 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
   11834 		type = SHT_REL;
   11835 	      else
   11836 		type = SHT_RELA;
   11837 	      dyn.d_un.d_val = 0;
   11838 	      dyn.d_un.d_ptr = 0;
   11839 	      for (i = 1; i < elf_numsections (abfd); i++)
   11840 		{
   11841 		  Elf_Internal_Shdr *hdr;
   11842 
   11843 		  hdr = elf_elfsections (abfd)[i];
   11844 		  if (hdr->sh_type == type
   11845 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
   11846 		    {
   11847 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
   11848 			dyn.d_un.d_val += hdr->sh_size;
   11849 		      else
   11850 			{
   11851 			  if (dyn.d_un.d_ptr == 0
   11852 			      || hdr->sh_addr < dyn.d_un.d_ptr)
   11853 			    dyn.d_un.d_ptr = hdr->sh_addr;
   11854 			}
   11855 		    }
   11856 		}
   11857 	      break;
   11858 	    }
   11859 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
   11860 	}
   11861     }
   11862 
   11863   /* If we have created any dynamic sections, then output them.  */
   11864   if (dynobj != NULL)
   11865     {
   11866       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
   11867 	goto error_return;
   11868 
   11869       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
   11870       if (((info->warn_shared_textrel && bfd_link_pic (info))
   11871 	   || info->error_textrel)
   11872 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
   11873 	{
   11874 	  bfd_byte *dyncon, *dynconend;
   11875 
   11876 	  dyncon = o->contents;
   11877 	  dynconend = o->contents + o->size;
   11878 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
   11879 	    {
   11880 	      Elf_Internal_Dyn dyn;
   11881 
   11882 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   11883 
   11884 	      if (dyn.d_tag == DT_TEXTREL)
   11885 		{
   11886 		  if (info->error_textrel)
   11887 		    info->callbacks->einfo
   11888 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
   11889 		  else
   11890 		    info->callbacks->einfo
   11891 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
   11892 		  break;
   11893 		}
   11894 	    }
   11895 	}
   11896 
   11897       for (o = dynobj->sections; o != NULL; o = o->next)
   11898 	{
   11899 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
   11900 	      || o->size == 0
   11901 	      || o->output_section == bfd_abs_section_ptr)
   11902 	    continue;
   11903 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
   11904 	    {
   11905 	      /* At this point, we are only interested in sections
   11906 		 created by _bfd_elf_link_create_dynamic_sections.  */
   11907 	      continue;
   11908 	    }
   11909 	  if (elf_hash_table (info)->stab_info.stabstr == o)
   11910 	    continue;
   11911 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
   11912 	    continue;
   11913 	  if (strcmp (o->name, ".dynstr") != 0)
   11914 	    {
   11915 	      /* FIXME: octets_per_byte.  */
   11916 	      if (! bfd_set_section_contents (abfd, o->output_section,
   11917 					      o->contents,
   11918 					      (file_ptr) o->output_offset,
   11919 					      o->size))
   11920 		goto error_return;
   11921 	    }
   11922 	  else
   11923 	    {
   11924 	      /* The contents of the .dynstr section are actually in a
   11925 		 stringtab.  */
   11926 	      file_ptr off;
   11927 
   11928 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
   11929 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
   11930 		  || ! _bfd_elf_strtab_emit (abfd,
   11931 					     elf_hash_table (info)->dynstr))
   11932 		goto error_return;
   11933 	    }
   11934 	}
   11935     }
   11936 
   11937   if (bfd_link_relocatable (info))
   11938     {
   11939       bfd_boolean failed = FALSE;
   11940 
   11941       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   11942       if (failed)
   11943 	goto error_return;
   11944     }
   11945 
   11946   /* If we have optimized stabs strings, output them.  */
   11947   if (elf_hash_table (info)->stab_info.stabstr != NULL)
   11948     {
   11949       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
   11950 	goto error_return;
   11951     }
   11952 
   11953   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
   11954     goto error_return;
   11955 
   11956   elf_final_link_free (abfd, &flinfo);
   11957 
   11958   elf_linker (abfd) = TRUE;
   11959 
   11960   if (attr_section)
   11961     {
   11962       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
   11963       if (contents == NULL)
   11964 	return FALSE;	/* Bail out and fail.  */
   11965       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
   11966       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
   11967       free (contents);
   11968     }
   11969 
   11970   return TRUE;
   11971 
   11972  error_return:
   11973   elf_final_link_free (abfd, &flinfo);
   11974   return FALSE;
   11975 }
   11976 
   11977 /* Initialize COOKIE for input bfd ABFD.  */
   11979 
   11980 static bfd_boolean
   11981 init_reloc_cookie (struct elf_reloc_cookie *cookie,
   11982 		   struct bfd_link_info *info, bfd *abfd)
   11983 {
   11984   Elf_Internal_Shdr *symtab_hdr;
   11985   const struct elf_backend_data *bed;
   11986 
   11987   bed = get_elf_backend_data (abfd);
   11988   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11989 
   11990   cookie->abfd = abfd;
   11991   cookie->sym_hashes = elf_sym_hashes (abfd);
   11992   cookie->bad_symtab = elf_bad_symtab (abfd);
   11993   if (cookie->bad_symtab)
   11994     {
   11995       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   11996       cookie->extsymoff = 0;
   11997     }
   11998   else
   11999     {
   12000       cookie->locsymcount = symtab_hdr->sh_info;
   12001       cookie->extsymoff = symtab_hdr->sh_info;
   12002     }
   12003 
   12004   if (bed->s->arch_size == 32)
   12005     cookie->r_sym_shift = 8;
   12006   else
   12007     cookie->r_sym_shift = 32;
   12008 
   12009   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   12010   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
   12011     {
   12012       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   12013 					      cookie->locsymcount, 0,
   12014 					      NULL, NULL, NULL);
   12015       if (cookie->locsyms == NULL)
   12016 	{
   12017 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
   12018 	  return FALSE;
   12019 	}
   12020       if (info->keep_memory)
   12021 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
   12022     }
   12023   return TRUE;
   12024 }
   12025 
   12026 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   12027 
   12028 static void
   12029 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
   12030 {
   12031   Elf_Internal_Shdr *symtab_hdr;
   12032 
   12033   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12034   if (cookie->locsyms != NULL
   12035       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
   12036     free (cookie->locsyms);
   12037 }
   12038 
   12039 /* Initialize the relocation information in COOKIE for input section SEC
   12040    of input bfd ABFD.  */
   12041 
   12042 static bfd_boolean
   12043 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   12044 			struct bfd_link_info *info, bfd *abfd,
   12045 			asection *sec)
   12046 {
   12047   const struct elf_backend_data *bed;
   12048 
   12049   if (sec->reloc_count == 0)
   12050     {
   12051       cookie->rels = NULL;
   12052       cookie->relend = NULL;
   12053     }
   12054   else
   12055     {
   12056       bed = get_elf_backend_data (abfd);
   12057 
   12058       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   12059 						info->keep_memory);
   12060       if (cookie->rels == NULL)
   12061 	return FALSE;
   12062       cookie->rel = cookie->rels;
   12063       cookie->relend = (cookie->rels
   12064 			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
   12065     }
   12066   cookie->rel = cookie->rels;
   12067   return TRUE;
   12068 }
   12069 
   12070 /* Free the memory allocated by init_reloc_cookie_rels,
   12071    if appropriate.  */
   12072 
   12073 static void
   12074 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
   12075 			asection *sec)
   12076 {
   12077   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
   12078     free (cookie->rels);
   12079 }
   12080 
   12081 /* Initialize the whole of COOKIE for input section SEC.  */
   12082 
   12083 static bfd_boolean
   12084 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   12085 			       struct bfd_link_info *info,
   12086 			       asection *sec)
   12087 {
   12088   if (!init_reloc_cookie (cookie, info, sec->owner))
   12089     goto error1;
   12090   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   12091     goto error2;
   12092   return TRUE;
   12093 
   12094  error2:
   12095   fini_reloc_cookie (cookie, sec->owner);
   12096  error1:
   12097   return FALSE;
   12098 }
   12099 
   12100 /* Free the memory allocated by init_reloc_cookie_for_section,
   12101    if appropriate.  */
   12102 
   12103 static void
   12104 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
   12105 			       asection *sec)
   12106 {
   12107   fini_reloc_cookie_rels (cookie, sec);
   12108   fini_reloc_cookie (cookie, sec->owner);
   12109 }
   12110 
   12111 /* Garbage collect unused sections.  */
   12113 
   12114 /* Default gc_mark_hook.  */
   12115 
   12116 asection *
   12117 _bfd_elf_gc_mark_hook (asection *sec,
   12118 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12119 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
   12120 		       struct elf_link_hash_entry *h,
   12121 		       Elf_Internal_Sym *sym)
   12122 {
   12123   if (h != NULL)
   12124     {
   12125       switch (h->root.type)
   12126 	{
   12127 	case bfd_link_hash_defined:
   12128 	case bfd_link_hash_defweak:
   12129 	  return h->root.u.def.section;
   12130 
   12131 	case bfd_link_hash_common:
   12132 	  return h->root.u.c.p->section;
   12133 
   12134 	default:
   12135 	  break;
   12136 	}
   12137     }
   12138   else
   12139     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   12140 
   12141   return NULL;
   12142 }
   12143 
   12144 /* COOKIE->rel describes a relocation against section SEC, which is
   12145    a section we've decided to keep.  Return the section that contains
   12146    the relocation symbol, or NULL if no section contains it.  */
   12147 
   12148 asection *
   12149 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   12150 		       elf_gc_mark_hook_fn gc_mark_hook,
   12151 		       struct elf_reloc_cookie *cookie,
   12152 		       bfd_boolean *start_stop)
   12153 {
   12154   unsigned long r_symndx;
   12155   struct elf_link_hash_entry *h;
   12156 
   12157   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   12158   if (r_symndx == STN_UNDEF)
   12159     return NULL;
   12160 
   12161   if (r_symndx >= cookie->locsymcount
   12162       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   12163     {
   12164       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
   12165       if (h == NULL)
   12166 	{
   12167 	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
   12168 				  sec->owner);
   12169 	  return NULL;
   12170 	}
   12171       while (h->root.type == bfd_link_hash_indirect
   12172 	     || h->root.type == bfd_link_hash_warning)
   12173 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   12174       h->mark = 1;
   12175       /* If this symbol is weak and there is a non-weak definition, we
   12176 	 keep the non-weak definition because many backends put
   12177 	 dynamic reloc info on the non-weak definition for code
   12178 	 handling copy relocs.  */
   12179       if (h->u.weakdef != NULL)
   12180 	h->u.weakdef->mark = 1;
   12181 
   12182       if (start_stop != NULL
   12183 	  && (h->root.type == bfd_link_hash_undefined
   12184 	      || h->root.type == bfd_link_hash_undefweak))
   12185 	{
   12186 	  /* To work around a glibc bug, mark all XXX input sections
   12187 	     when there is an as yet undefined reference to __start_XXX
   12188 	     or __stop_XXX symbols.  The linker will later define such
   12189 	     symbols for orphan input sections that have a name
   12190 	     representable as a C identifier.  */
   12191 	  const char *sec_name = NULL;
   12192 	  if (strncmp (h->root.root.string, "__start_", 8) == 0)
   12193 	    sec_name = h->root.root.string + 8;
   12194 	  else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
   12195 	    sec_name = h->root.root.string + 7;
   12196 
   12197 	  if (sec_name != NULL && *sec_name != '\0')
   12198 	    {
   12199 	      bfd *i;
   12200 
   12201 	      for (i = info->input_bfds; i != NULL; i = i->link.next)
   12202 		{
   12203 		  asection *s = bfd_get_section_by_name (i, sec_name);
   12204 		  if (s != NULL && !s->gc_mark)
   12205 		    {
   12206 		      *start_stop = TRUE;
   12207 		      return s;
   12208 		    }
   12209 		}
   12210 	    }
   12211 	}
   12212 
   12213       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   12214     }
   12215 
   12216   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   12217 			  &cookie->locsyms[r_symndx]);
   12218 }
   12219 
   12220 /* COOKIE->rel describes a relocation against section SEC, which is
   12221    a section we've decided to keep.  Mark the section that contains
   12222    the relocation symbol.  */
   12223 
   12224 bfd_boolean
   12225 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
   12226 			asection *sec,
   12227 			elf_gc_mark_hook_fn gc_mark_hook,
   12228 			struct elf_reloc_cookie *cookie)
   12229 {
   12230   asection *rsec;
   12231   bfd_boolean start_stop = FALSE;
   12232 
   12233   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
   12234   while (rsec != NULL)
   12235     {
   12236       if (!rsec->gc_mark)
   12237 	{
   12238 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
   12239 	      || (rsec->owner->flags & DYNAMIC) != 0)
   12240 	    rsec->gc_mark = 1;
   12241 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
   12242 	    return FALSE;
   12243 	}
   12244       if (!start_stop)
   12245 	break;
   12246       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
   12247     }
   12248   return TRUE;
   12249 }
   12250 
   12251 /* The mark phase of garbage collection.  For a given section, mark
   12252    it and any sections in this section's group, and all the sections
   12253    which define symbols to which it refers.  */
   12254 
   12255 bfd_boolean
   12256 _bfd_elf_gc_mark (struct bfd_link_info *info,
   12257 		  asection *sec,
   12258 		  elf_gc_mark_hook_fn gc_mark_hook)
   12259 {
   12260   bfd_boolean ret;
   12261   asection *group_sec, *eh_frame;
   12262 
   12263   sec->gc_mark = 1;
   12264 
   12265   /* Mark all the sections in the group.  */
   12266   group_sec = elf_section_data (sec)->next_in_group;
   12267   if (group_sec && !group_sec->gc_mark)
   12268     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
   12269       return FALSE;
   12270 
   12271   /* Look through the section relocs.  */
   12272   ret = TRUE;
   12273   eh_frame = elf_eh_frame_section (sec->owner);
   12274   if ((sec->flags & SEC_RELOC) != 0
   12275       && sec->reloc_count > 0
   12276       && sec != eh_frame)
   12277     {
   12278       struct elf_reloc_cookie cookie;
   12279 
   12280       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   12281 	ret = FALSE;
   12282       else
   12283 	{
   12284 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   12285 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   12286 	      {
   12287 		ret = FALSE;
   12288 		break;
   12289 	      }
   12290 	  fini_reloc_cookie_for_section (&cookie, sec);
   12291 	}
   12292     }
   12293 
   12294   if (ret && eh_frame && elf_fde_list (sec))
   12295     {
   12296       struct elf_reloc_cookie cookie;
   12297 
   12298       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
   12299 	ret = FALSE;
   12300       else
   12301 	{
   12302 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
   12303 				      gc_mark_hook, &cookie))
   12304 	    ret = FALSE;
   12305 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
   12306 	}
   12307     }
   12308 
   12309   eh_frame = elf_section_eh_frame_entry (sec);
   12310   if (ret && eh_frame && !eh_frame->gc_mark)
   12311     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
   12312       ret = FALSE;
   12313 
   12314   return ret;
   12315 }
   12316 
   12317 /* Scan and mark sections in a special or debug section group.  */
   12318 
   12319 static void
   12320 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   12321 {
   12322   /* Point to first section of section group.  */
   12323   asection *ssec;
   12324   /* Used to iterate the section group.  */
   12325   asection *msec;
   12326 
   12327   bfd_boolean is_special_grp = TRUE;
   12328   bfd_boolean is_debug_grp = TRUE;
   12329 
   12330   /* First scan to see if group contains any section other than debug
   12331      and special section.  */
   12332   ssec = msec = elf_next_in_group (grp);
   12333   do
   12334     {
   12335       if ((msec->flags & SEC_DEBUGGING) == 0)
   12336 	is_debug_grp = FALSE;
   12337 
   12338       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
   12339 	is_special_grp = FALSE;
   12340 
   12341       msec = elf_next_in_group (msec);
   12342     }
   12343   while (msec != ssec);
   12344 
   12345   /* If this is a pure debug section group or pure special section group,
   12346      keep all sections in this group.  */
   12347   if (is_debug_grp || is_special_grp)
   12348     {
   12349       do
   12350 	{
   12351 	  msec->gc_mark = 1;
   12352 	  msec = elf_next_in_group (msec);
   12353 	}
   12354       while (msec != ssec);
   12355     }
   12356 }
   12357 
   12358 /* Keep debug and special sections.  */
   12359 
   12360 bfd_boolean
   12361 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   12362 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
   12363 {
   12364   bfd *ibfd;
   12365 
   12366   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12367     {
   12368       asection *isec;
   12369       bfd_boolean some_kept;
   12370       bfd_boolean debug_frag_seen;
   12371 
   12372       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   12373 	continue;
   12374 
   12375       /* Ensure all linker created sections are kept,
   12376 	 see if any other section is already marked,
   12377 	 and note if we have any fragmented debug sections.  */
   12378       debug_frag_seen = some_kept = FALSE;
   12379       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   12380 	{
   12381 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   12382 	    isec->gc_mark = 1;
   12383 	  else if (isec->gc_mark)
   12384 	    some_kept = TRUE;
   12385 
   12386 	  if (debug_frag_seen == FALSE
   12387 	      && (isec->flags & SEC_DEBUGGING)
   12388 	      && CONST_STRNEQ (isec->name, ".debug_line."))
   12389 	    debug_frag_seen = TRUE;
   12390 	}
   12391 
   12392       /* If no section in this file will be kept, then we can
   12393 	 toss out the debug and special sections.  */
   12394       if (!some_kept)
   12395 	continue;
   12396 
   12397       /* Keep debug and special sections like .comment when they are
   12398 	 not part of a group.  Also keep section groups that contain
   12399 	 just debug sections or special sections.  */
   12400       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   12401 	{
   12402 	  if ((isec->flags & SEC_GROUP) != 0)
   12403 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
   12404 	  else if (((isec->flags & SEC_DEBUGGING) != 0
   12405 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   12406 		   && elf_next_in_group (isec) == NULL)
   12407 	    isec->gc_mark = 1;
   12408 	}
   12409 
   12410       if (! debug_frag_seen)
   12411 	continue;
   12412 
   12413       /* Look for CODE sections which are going to be discarded,
   12414 	 and find and discard any fragmented debug sections which
   12415 	 are associated with that code section.  */
   12416       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   12417 	if ((isec->flags & SEC_CODE) != 0
   12418 	    && isec->gc_mark == 0)
   12419 	  {
   12420 	    unsigned int ilen;
   12421 	    asection *dsec;
   12422 
   12423 	    ilen = strlen (isec->name);
   12424 
   12425 	    /* Association is determined by the name of the debug section
   12426 	       containing the name of the code section as a suffix.  For
   12427 	       example .debug_line.text.foo is a debug section associated
   12428 	       with .text.foo.  */
   12429 	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
   12430 	      {
   12431 		unsigned int dlen;
   12432 
   12433 		if (dsec->gc_mark == 0
   12434 		    || (dsec->flags & SEC_DEBUGGING) == 0)
   12435 		  continue;
   12436 
   12437 		dlen = strlen (dsec->name);
   12438 
   12439 		if (dlen > ilen
   12440 		    && strncmp (dsec->name + (dlen - ilen),
   12441 				isec->name, ilen) == 0)
   12442 		  {
   12443 		    dsec->gc_mark = 0;
   12444 		  }
   12445 	      }
   12446 	  }
   12447     }
   12448   return TRUE;
   12449 }
   12450 
   12451 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
   12452 
   12453 struct elf_gc_sweep_symbol_info
   12454 {
   12455   struct bfd_link_info *info;
   12456   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
   12457 		       bfd_boolean);
   12458 };
   12459 
   12460 static bfd_boolean
   12461 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
   12462 {
   12463   if (!h->mark
   12464       && (((h->root.type == bfd_link_hash_defined
   12465 	    || h->root.type == bfd_link_hash_defweak)
   12466 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
   12467 		&& h->root.u.def.section->gc_mark))
   12468 	  || h->root.type == bfd_link_hash_undefined
   12469 	  || h->root.type == bfd_link_hash_undefweak))
   12470     {
   12471       struct elf_gc_sweep_symbol_info *inf;
   12472 
   12473       inf = (struct elf_gc_sweep_symbol_info *) data;
   12474       (*inf->hide_symbol) (inf->info, h, TRUE);
   12475       h->def_regular = 0;
   12476       h->ref_regular = 0;
   12477       h->ref_regular_nonweak = 0;
   12478     }
   12479 
   12480   return TRUE;
   12481 }
   12482 
   12483 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
   12484 
   12485 typedef bfd_boolean (*gc_sweep_hook_fn)
   12486   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
   12487 
   12488 static bfd_boolean
   12489 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
   12490 {
   12491   bfd *sub;
   12492   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12493   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
   12494   unsigned long section_sym_count;
   12495   struct elf_gc_sweep_symbol_info sweep_info;
   12496 
   12497   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12498     {
   12499       asection *o;
   12500 
   12501       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   12502 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   12503 	continue;
   12504 
   12505       for (o = sub->sections; o != NULL; o = o->next)
   12506 	{
   12507 	  /* When any section in a section group is kept, we keep all
   12508 	     sections in the section group.  If the first member of
   12509 	     the section group is excluded, we will also exclude the
   12510 	     group section.  */
   12511 	  if (o->flags & SEC_GROUP)
   12512 	    {
   12513 	      asection *first = elf_next_in_group (o);
   12514 	      o->gc_mark = first->gc_mark;
   12515 	    }
   12516 
   12517 	  if (o->gc_mark)
   12518 	    continue;
   12519 
   12520 	  /* Skip sweeping sections already excluded.  */
   12521 	  if (o->flags & SEC_EXCLUDE)
   12522 	    continue;
   12523 
   12524 	  /* Since this is early in the link process, it is simple
   12525 	     to remove a section from the output.  */
   12526 	  o->flags |= SEC_EXCLUDE;
   12527 
   12528 	  if (info->print_gc_sections && o->size != 0)
   12529 	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
   12530 
   12531 	  /* But we also have to update some of the relocation
   12532 	     info we collected before.  */
   12533 	  if (gc_sweep_hook
   12534 	      && (o->flags & SEC_RELOC) != 0
   12535 	      && o->reloc_count != 0
   12536 	      && !((info->strip == strip_all || info->strip == strip_debugger)
   12537 		   && (o->flags & SEC_DEBUGGING) != 0)
   12538 	      && !bfd_is_abs_section (o->output_section))
   12539 	    {
   12540 	      Elf_Internal_Rela *internal_relocs;
   12541 	      bfd_boolean r;
   12542 
   12543 	      internal_relocs
   12544 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
   12545 					     info->keep_memory);
   12546 	      if (internal_relocs == NULL)
   12547 		return FALSE;
   12548 
   12549 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
   12550 
   12551 	      if (elf_section_data (o)->relocs != internal_relocs)
   12552 		free (internal_relocs);
   12553 
   12554 	      if (!r)
   12555 		return FALSE;
   12556 	    }
   12557 	}
   12558     }
   12559 
   12560   /* Remove the symbols that were in the swept sections from the dynamic
   12561      symbol table.  GCFIXME: Anyone know how to get them out of the
   12562      static symbol table as well?  */
   12563   sweep_info.info = info;
   12564   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
   12565   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
   12566 			  &sweep_info);
   12567 
   12568   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
   12569   return TRUE;
   12570 }
   12571 
   12572 /* Propagate collected vtable information.  This is called through
   12573    elf_link_hash_traverse.  */
   12574 
   12575 static bfd_boolean
   12576 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
   12577 {
   12578   /* Those that are not vtables.  */
   12579   if (h->vtable == NULL || h->vtable->parent == NULL)
   12580     return TRUE;
   12581 
   12582   /* Those vtables that do not have parents, we cannot merge.  */
   12583   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
   12584     return TRUE;
   12585 
   12586   /* If we've already been done, exit.  */
   12587   if (h->vtable->used && h->vtable->used[-1])
   12588     return TRUE;
   12589 
   12590   /* Make sure the parent's table is up to date.  */
   12591   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
   12592 
   12593   if (h->vtable->used == NULL)
   12594     {
   12595       /* None of this table's entries were referenced.  Re-use the
   12596 	 parent's table.  */
   12597       h->vtable->used = h->vtable->parent->vtable->used;
   12598       h->vtable->size = h->vtable->parent->vtable->size;
   12599     }
   12600   else
   12601     {
   12602       size_t n;
   12603       bfd_boolean *cu, *pu;
   12604 
   12605       /* Or the parent's entries into ours.  */
   12606       cu = h->vtable->used;
   12607       cu[-1] = TRUE;
   12608       pu = h->vtable->parent->vtable->used;
   12609       if (pu != NULL)
   12610 	{
   12611 	  const struct elf_backend_data *bed;
   12612 	  unsigned int log_file_align;
   12613 
   12614 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
   12615 	  log_file_align = bed->s->log_file_align;
   12616 	  n = h->vtable->parent->vtable->size >> log_file_align;
   12617 	  while (n--)
   12618 	    {
   12619 	      if (*pu)
   12620 		*cu = TRUE;
   12621 	      pu++;
   12622 	      cu++;
   12623 	    }
   12624 	}
   12625     }
   12626 
   12627   return TRUE;
   12628 }
   12629 
   12630 static bfd_boolean
   12631 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
   12632 {
   12633   asection *sec;
   12634   bfd_vma hstart, hend;
   12635   Elf_Internal_Rela *relstart, *relend, *rel;
   12636   const struct elf_backend_data *bed;
   12637   unsigned int log_file_align;
   12638 
   12639   /* Take care of both those symbols that do not describe vtables as
   12640      well as those that are not loaded.  */
   12641   if (h->vtable == NULL || h->vtable->parent == NULL)
   12642     return TRUE;
   12643 
   12644   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   12645 	      || h->root.type == bfd_link_hash_defweak);
   12646 
   12647   sec = h->root.u.def.section;
   12648   hstart = h->root.u.def.value;
   12649   hend = hstart + h->size;
   12650 
   12651   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
   12652   if (!relstart)
   12653     return *(bfd_boolean *) okp = FALSE;
   12654   bed = get_elf_backend_data (sec->owner);
   12655   log_file_align = bed->s->log_file_align;
   12656 
   12657   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
   12658 
   12659   for (rel = relstart; rel < relend; ++rel)
   12660     if (rel->r_offset >= hstart && rel->r_offset < hend)
   12661       {
   12662 	/* If the entry is in use, do nothing.  */
   12663 	if (h->vtable->used
   12664 	    && (rel->r_offset - hstart) < h->vtable->size)
   12665 	  {
   12666 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
   12667 	    if (h->vtable->used[entry])
   12668 	      continue;
   12669 	  }
   12670 	/* Otherwise, kill it.  */
   12671 	rel->r_offset = rel->r_info = rel->r_addend = 0;
   12672       }
   12673 
   12674   return TRUE;
   12675 }
   12676 
   12677 /* Mark sections containing dynamically referenced symbols.  When
   12678    building shared libraries, we must assume that any visible symbol is
   12679    referenced.  */
   12680 
   12681 bfd_boolean
   12682 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
   12683 {
   12684   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   12685   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   12686 
   12687   if ((h->root.type == bfd_link_hash_defined
   12688        || h->root.type == bfd_link_hash_defweak)
   12689       && (h->ref_dynamic
   12690 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
   12691 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   12692 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   12693 	      && (!bfd_link_executable (info)
   12694 		  || info->export_dynamic
   12695 		  || (h->dynamic
   12696 		      && d != NULL
   12697 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
   12698 	      && (h->versioned >= versioned
   12699 		  || !bfd_hide_sym_by_version (info->version_info,
   12700 					       h->root.root.string)))))
   12701     h->root.u.def.section->flags |= SEC_KEEP;
   12702 
   12703   return TRUE;
   12704 }
   12705 
   12706 /* Keep all sections containing symbols undefined on the command-line,
   12707    and the section containing the entry symbol.  */
   12708 
   12709 void
   12710 _bfd_elf_gc_keep (struct bfd_link_info *info)
   12711 {
   12712   struct bfd_sym_chain *sym;
   12713 
   12714   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   12715     {
   12716       struct elf_link_hash_entry *h;
   12717 
   12718       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
   12719 				FALSE, FALSE, FALSE);
   12720 
   12721       if (h != NULL
   12722 	  && (h->root.type == bfd_link_hash_defined
   12723 	      || h->root.type == bfd_link_hash_defweak)
   12724 	  && !bfd_is_abs_section (h->root.u.def.section))
   12725 	h->root.u.def.section->flags |= SEC_KEEP;
   12726     }
   12727 }
   12728 
   12729 bfd_boolean
   12730 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
   12731 				struct bfd_link_info *info)
   12732 {
   12733   bfd *ibfd = info->input_bfds;
   12734 
   12735   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12736     {
   12737       asection *sec;
   12738       struct elf_reloc_cookie cookie;
   12739 
   12740       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   12741 	continue;
   12742 
   12743       if (!init_reloc_cookie (&cookie, info, ibfd))
   12744 	return FALSE;
   12745 
   12746       for (sec = ibfd->sections; sec; sec = sec->next)
   12747 	{
   12748 	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
   12749 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
   12750 	    {
   12751 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
   12752 	      fini_reloc_cookie_rels (&cookie, sec);
   12753 	    }
   12754 	}
   12755     }
   12756   return TRUE;
   12757 }
   12758 
   12759 /* Do mark and sweep of unused sections.  */
   12760 
   12761 bfd_boolean
   12762 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   12763 {
   12764   bfd_boolean ok = TRUE;
   12765   bfd *sub;
   12766   elf_gc_mark_hook_fn gc_mark_hook;
   12767   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12768   struct elf_link_hash_table *htab;
   12769 
   12770   if (!bed->can_gc_sections
   12771       || !is_elf_hash_table (info->hash))
   12772     {
   12773       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
   12774       return TRUE;
   12775     }
   12776 
   12777   bed->gc_keep (info);
   12778   htab = elf_hash_table (info);
   12779 
   12780   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
   12781      at the .eh_frame section if we can mark the FDEs individually.  */
   12782   for (sub = info->input_bfds;
   12783        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
   12784        sub = sub->link.next)
   12785     {
   12786       asection *sec;
   12787       struct elf_reloc_cookie cookie;
   12788 
   12789       sec = bfd_get_section_by_name (sub, ".eh_frame");
   12790       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
   12791 	{
   12792 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
   12793 	  if (elf_section_data (sec)->sec_info
   12794 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
   12795 	    elf_eh_frame_section (sub) = sec;
   12796 	  fini_reloc_cookie_for_section (&cookie, sec);
   12797 	  sec = bfd_get_next_section_by_name (NULL, sec);
   12798 	}
   12799     }
   12800 
   12801   /* Apply transitive closure to the vtable entry usage info.  */
   12802   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
   12803   if (!ok)
   12804     return FALSE;
   12805 
   12806   /* Kill the vtable relocations that were not used.  */
   12807   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
   12808   if (!ok)
   12809     return FALSE;
   12810 
   12811   /* Mark dynamically referenced symbols.  */
   12812   if (htab->dynamic_sections_created)
   12813     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
   12814 
   12815   /* Grovel through relocs to find out who stays ...  */
   12816   gc_mark_hook = bed->gc_mark_hook;
   12817   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12818     {
   12819       asection *o;
   12820 
   12821       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
   12822 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
   12823 	continue;
   12824 
   12825       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
   12826 	 Also treat note sections as a root, if the section is not part
   12827 	 of a group.  */
   12828       for (o = sub->sections; o != NULL; o = o->next)
   12829 	if (!o->gc_mark
   12830 	    && (o->flags & SEC_EXCLUDE) == 0
   12831 	    && ((o->flags & SEC_KEEP) != 0
   12832 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
   12833 		    && elf_next_in_group (o) == NULL )))
   12834 	  {
   12835 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   12836 	      return FALSE;
   12837 	  }
   12838     }
   12839 
   12840   /* Allow the backend to mark additional target specific sections.  */
   12841   bed->gc_mark_extra_sections (info, gc_mark_hook);
   12842 
   12843   /* ... and mark SEC_EXCLUDE for those that go.  */
   12844   return elf_gc_sweep (abfd, info);
   12845 }
   12846 
   12847 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
   12849 
   12850 bfd_boolean
   12851 bfd_elf_gc_record_vtinherit (bfd *abfd,
   12852 			     asection *sec,
   12853 			     struct elf_link_hash_entry *h,
   12854 			     bfd_vma offset)
   12855 {
   12856   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   12857   struct elf_link_hash_entry **search, *child;
   12858   bfd_size_type extsymcount;
   12859   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12860 
   12861   /* The sh_info field of the symtab header tells us where the
   12862      external symbols start.  We don't care about the local symbols at
   12863      this point.  */
   12864   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
   12865   if (!elf_bad_symtab (abfd))
   12866     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
   12867 
   12868   sym_hashes = elf_sym_hashes (abfd);
   12869   sym_hashes_end = sym_hashes + extsymcount;
   12870 
   12871   /* Hunt down the child symbol, which is in this section at the same
   12872      offset as the relocation.  */
   12873   for (search = sym_hashes; search != sym_hashes_end; ++search)
   12874     {
   12875       if ((child = *search) != NULL
   12876 	  && (child->root.type == bfd_link_hash_defined
   12877 	      || child->root.type == bfd_link_hash_defweak)
   12878 	  && child->root.u.def.section == sec
   12879 	  && child->root.u.def.value == offset)
   12880 	goto win;
   12881     }
   12882 
   12883   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
   12884 			 abfd, sec, (unsigned long) offset);
   12885   bfd_set_error (bfd_error_invalid_operation);
   12886   return FALSE;
   12887 
   12888  win:
   12889   if (!child->vtable)
   12890     {
   12891       child->vtable = ((struct elf_link_virtual_table_entry *)
   12892 		       bfd_zalloc (abfd, sizeof (*child->vtable)));
   12893       if (!child->vtable)
   12894 	return FALSE;
   12895     }
   12896   if (!h)
   12897     {
   12898       /* This *should* only be the absolute section.  It could potentially
   12899 	 be that someone has defined a non-global vtable though, which
   12900 	 would be bad.  It isn't worth paging in the local symbols to be
   12901 	 sure though; that case should simply be handled by the assembler.  */
   12902 
   12903       child->vtable->parent = (struct elf_link_hash_entry *) -1;
   12904     }
   12905   else
   12906     child->vtable->parent = h;
   12907 
   12908   return TRUE;
   12909 }
   12910 
   12911 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
   12912 
   12913 bfd_boolean
   12914 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
   12915 			   asection *sec ATTRIBUTE_UNUSED,
   12916 			   struct elf_link_hash_entry *h,
   12917 			   bfd_vma addend)
   12918 {
   12919   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12920   unsigned int log_file_align = bed->s->log_file_align;
   12921 
   12922   if (!h->vtable)
   12923     {
   12924       h->vtable = ((struct elf_link_virtual_table_entry *)
   12925 		   bfd_zalloc (abfd, sizeof (*h->vtable)));
   12926       if (!h->vtable)
   12927 	return FALSE;
   12928     }
   12929 
   12930   if (addend >= h->vtable->size)
   12931     {
   12932       size_t size, bytes, file_align;
   12933       bfd_boolean *ptr = h->vtable->used;
   12934 
   12935       /* While the symbol is undefined, we have to be prepared to handle
   12936 	 a zero size.  */
   12937       file_align = 1 << log_file_align;
   12938       if (h->root.type == bfd_link_hash_undefined)
   12939 	size = addend + file_align;
   12940       else
   12941 	{
   12942 	  size = h->size;
   12943 	  if (addend >= size)
   12944 	    {
   12945 	      /* Oops!  We've got a reference past the defined end of
   12946 		 the table.  This is probably a bug -- shall we warn?  */
   12947 	      size = addend + file_align;
   12948 	    }
   12949 	}
   12950       size = (size + file_align - 1) & -file_align;
   12951 
   12952       /* Allocate one extra entry for use as a "done" flag for the
   12953 	 consolidation pass.  */
   12954       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
   12955 
   12956       if (ptr)
   12957 	{
   12958 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
   12959 
   12960 	  if (ptr != NULL)
   12961 	    {
   12962 	      size_t oldbytes;
   12963 
   12964 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
   12965 			  * sizeof (bfd_boolean));
   12966 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
   12967 	    }
   12968 	}
   12969       else
   12970 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
   12971 
   12972       if (ptr == NULL)
   12973 	return FALSE;
   12974 
   12975       /* And arrange for that done flag to be at index -1.  */
   12976       h->vtable->used = ptr + 1;
   12977       h->vtable->size = size;
   12978     }
   12979 
   12980   h->vtable->used[addend >> log_file_align] = TRUE;
   12981 
   12982   return TRUE;
   12983 }
   12984 
   12985 /* Map an ELF section header flag to its corresponding string.  */
   12986 typedef struct
   12987 {
   12988   char *flag_name;
   12989   flagword flag_value;
   12990 } elf_flags_to_name_table;
   12991 
   12992 static elf_flags_to_name_table elf_flags_to_names [] =
   12993 {
   12994   { "SHF_WRITE", SHF_WRITE },
   12995   { "SHF_ALLOC", SHF_ALLOC },
   12996   { "SHF_EXECINSTR", SHF_EXECINSTR },
   12997   { "SHF_MERGE", SHF_MERGE },
   12998   { "SHF_STRINGS", SHF_STRINGS },
   12999   { "SHF_INFO_LINK", SHF_INFO_LINK},
   13000   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
   13001   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
   13002   { "SHF_GROUP", SHF_GROUP },
   13003   { "SHF_TLS", SHF_TLS },
   13004   { "SHF_MASKOS", SHF_MASKOS },
   13005   { "SHF_EXCLUDE", SHF_EXCLUDE },
   13006 };
   13007 
   13008 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
   13009 bfd_boolean
   13010 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
   13011 			      struct flag_info *flaginfo,
   13012 			      asection *section)
   13013 {
   13014   const bfd_vma sh_flags = elf_section_flags (section);
   13015 
   13016   if (!flaginfo->flags_initialized)
   13017     {
   13018       bfd *obfd = info->output_bfd;
   13019       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   13020       struct flag_info_list *tf = flaginfo->flag_list;
   13021       int with_hex = 0;
   13022       int without_hex = 0;
   13023 
   13024       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
   13025 	{
   13026 	  unsigned i;
   13027 	  flagword (*lookup) (char *);
   13028 
   13029 	  lookup = bed->elf_backend_lookup_section_flags_hook;
   13030 	  if (lookup != NULL)
   13031 	    {
   13032 	      flagword hexval = (*lookup) ((char *) tf->name);
   13033 
   13034 	      if (hexval != 0)
   13035 		{
   13036 		  if (tf->with == with_flags)
   13037 		    with_hex |= hexval;
   13038 		  else if (tf->with == without_flags)
   13039 		    without_hex |= hexval;
   13040 		  tf->valid = TRUE;
   13041 		  continue;
   13042 		}
   13043 	    }
   13044 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
   13045 	    {
   13046 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
   13047 		{
   13048 		  if (tf->with == with_flags)
   13049 		    with_hex |= elf_flags_to_names[i].flag_value;
   13050 		  else if (tf->with == without_flags)
   13051 		    without_hex |= elf_flags_to_names[i].flag_value;
   13052 		  tf->valid = TRUE;
   13053 		  break;
   13054 		}
   13055 	    }
   13056 	  if (!tf->valid)
   13057 	    {
   13058 	      info->callbacks->einfo
   13059 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
   13060 	      return FALSE;
   13061 	    }
   13062 	}
   13063       flaginfo->flags_initialized = TRUE;
   13064       flaginfo->only_with_flags |= with_hex;
   13065       flaginfo->not_with_flags |= without_hex;
   13066     }
   13067 
   13068   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
   13069     return FALSE;
   13070 
   13071   if ((flaginfo->not_with_flags & sh_flags) != 0)
   13072     return FALSE;
   13073 
   13074   return TRUE;
   13075 }
   13076 
   13077 struct alloc_got_off_arg {
   13078   bfd_vma gotoff;
   13079   struct bfd_link_info *info;
   13080 };
   13081 
   13082 /* We need a special top-level link routine to convert got reference counts
   13083    to real got offsets.  */
   13084 
   13085 static bfd_boolean
   13086 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
   13087 {
   13088   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
   13089   bfd *obfd = gofarg->info->output_bfd;
   13090   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   13091 
   13092   if (h->got.refcount > 0)
   13093     {
   13094       h->got.offset = gofarg->gotoff;
   13095       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
   13096     }
   13097   else
   13098     h->got.offset = (bfd_vma) -1;
   13099 
   13100   return TRUE;
   13101 }
   13102 
   13103 /* And an accompanying bit to work out final got entry offsets once
   13104    we're done.  Should be called from final_link.  */
   13105 
   13106 bfd_boolean
   13107 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
   13108 					struct bfd_link_info *info)
   13109 {
   13110   bfd *i;
   13111   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13112   bfd_vma gotoff;
   13113   struct alloc_got_off_arg gofarg;
   13114 
   13115   BFD_ASSERT (abfd == info->output_bfd);
   13116 
   13117   if (! is_elf_hash_table (info->hash))
   13118     return FALSE;
   13119 
   13120   /* The GOT offset is relative to the .got section, but the GOT header is
   13121      put into the .got.plt section, if the backend uses it.  */
   13122   if (bed->want_got_plt)
   13123     gotoff = 0;
   13124   else
   13125     gotoff = bed->got_header_size;
   13126 
   13127   /* Do the local .got entries first.  */
   13128   for (i = info->input_bfds; i; i = i->link.next)
   13129     {
   13130       bfd_signed_vma *local_got;
   13131       bfd_size_type j, locsymcount;
   13132       Elf_Internal_Shdr *symtab_hdr;
   13133 
   13134       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
   13135 	continue;
   13136 
   13137       local_got = elf_local_got_refcounts (i);
   13138       if (!local_got)
   13139 	continue;
   13140 
   13141       symtab_hdr = &elf_tdata (i)->symtab_hdr;
   13142       if (elf_bad_symtab (i))
   13143 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
   13144       else
   13145 	locsymcount = symtab_hdr->sh_info;
   13146 
   13147       for (j = 0; j < locsymcount; ++j)
   13148 	{
   13149 	  if (local_got[j] > 0)
   13150 	    {
   13151 	      local_got[j] = gotoff;
   13152 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
   13153 	    }
   13154 	  else
   13155 	    local_got[j] = (bfd_vma) -1;
   13156 	}
   13157     }
   13158 
   13159   /* Then the global .got entries.  .plt refcounts are handled by
   13160      adjust_dynamic_symbol  */
   13161   gofarg.gotoff = gotoff;
   13162   gofarg.info = info;
   13163   elf_link_hash_traverse (elf_hash_table (info),
   13164 			  elf_gc_allocate_got_offsets,
   13165 			  &gofarg);
   13166   return TRUE;
   13167 }
   13168 
   13169 /* Many folk need no more in the way of final link than this, once
   13170    got entry reference counting is enabled.  */
   13171 
   13172 bfd_boolean
   13173 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
   13174 {
   13175   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
   13176     return FALSE;
   13177 
   13178   /* Invoke the regular ELF backend linker to do all the work.  */
   13179   return bfd_elf_final_link (abfd, info);
   13180 }
   13181 
   13182 bfd_boolean
   13183 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
   13184 {
   13185   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
   13186 
   13187   if (rcookie->bad_symtab)
   13188     rcookie->rel = rcookie->rels;
   13189 
   13190   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
   13191     {
   13192       unsigned long r_symndx;
   13193 
   13194       if (! rcookie->bad_symtab)
   13195 	if (rcookie->rel->r_offset > offset)
   13196 	  return FALSE;
   13197       if (rcookie->rel->r_offset != offset)
   13198 	continue;
   13199 
   13200       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
   13201       if (r_symndx == STN_UNDEF)
   13202 	return TRUE;
   13203 
   13204       if (r_symndx >= rcookie->locsymcount
   13205 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   13206 	{
   13207 	  struct elf_link_hash_entry *h;
   13208 
   13209 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
   13210 
   13211 	  while (h->root.type == bfd_link_hash_indirect
   13212 		 || h->root.type == bfd_link_hash_warning)
   13213 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   13214 
   13215 	  if ((h->root.type == bfd_link_hash_defined
   13216 	       || h->root.type == bfd_link_hash_defweak)
   13217 	      && (h->root.u.def.section->owner != rcookie->abfd
   13218 		  || h->root.u.def.section->kept_section != NULL
   13219 		  || discarded_section (h->root.u.def.section)))
   13220 	    return TRUE;
   13221 	}
   13222       else
   13223 	{
   13224 	  /* It's not a relocation against a global symbol,
   13225 	     but it could be a relocation against a local
   13226 	     symbol for a discarded section.  */
   13227 	  asection *isec;
   13228 	  Elf_Internal_Sym *isym;
   13229 
   13230 	  /* Need to: get the symbol; get the section.  */
   13231 	  isym = &rcookie->locsyms[r_symndx];
   13232 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
   13233 	  if (isec != NULL
   13234 	      && (isec->kept_section != NULL
   13235 		  || discarded_section (isec)))
   13236 	    return TRUE;
   13237 	}
   13238       return FALSE;
   13239     }
   13240   return FALSE;
   13241 }
   13242 
   13243 /* Discard unneeded references to discarded sections.
   13244    Returns -1 on error, 1 if any section's size was changed, 0 if
   13245    nothing changed.  This function assumes that the relocations are in
   13246    sorted order, which is true for all known assemblers.  */
   13247 
   13248 int
   13249 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
   13250 {
   13251   struct elf_reloc_cookie cookie;
   13252   asection *o;
   13253   bfd *abfd;
   13254   int changed = 0;
   13255 
   13256   if (info->traditional_format
   13257       || !is_elf_hash_table (info->hash))
   13258     return 0;
   13259 
   13260   o = bfd_get_section_by_name (output_bfd, ".stab");
   13261   if (o != NULL)
   13262     {
   13263       asection *i;
   13264 
   13265       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13266 	{
   13267 	  if (i->size == 0
   13268 	      || i->reloc_count == 0
   13269 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
   13270 	    continue;
   13271 
   13272 	  abfd = i->owner;
   13273 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13274 	    continue;
   13275 
   13276 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   13277 	    return -1;
   13278 
   13279 	  if (_bfd_discard_section_stabs (abfd, i,
   13280 					  elf_section_data (i)->sec_info,
   13281 					  bfd_elf_reloc_symbol_deleted_p,
   13282 					  &cookie))
   13283 	    changed = 1;
   13284 
   13285 	  fini_reloc_cookie_for_section (&cookie, i);
   13286 	}
   13287     }
   13288 
   13289   o = NULL;
   13290   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
   13291     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   13292   if (o != NULL)
   13293     {
   13294       asection *i;
   13295 
   13296       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13297 	{
   13298 	  if (i->size == 0)
   13299 	    continue;
   13300 
   13301 	  abfd = i->owner;
   13302 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13303 	    continue;
   13304 
   13305 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
   13306 	    return -1;
   13307 
   13308 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
   13309 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
   13310 						 bfd_elf_reloc_symbol_deleted_p,
   13311 						 &cookie))
   13312 	    changed = 1;
   13313 
   13314 	  fini_reloc_cookie_for_section (&cookie, i);
   13315 	}
   13316     }
   13317 
   13318   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   13319     {
   13320       const struct elf_backend_data *bed;
   13321 
   13322       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   13323 	continue;
   13324 
   13325       bed = get_elf_backend_data (abfd);
   13326 
   13327       if (bed->elf_backend_discard_info != NULL)
   13328 	{
   13329 	  if (!init_reloc_cookie (&cookie, info, abfd))
   13330 	    return -1;
   13331 
   13332 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
   13333 	    changed = 1;
   13334 
   13335 	  fini_reloc_cookie (&cookie, abfd);
   13336 	}
   13337     }
   13338 
   13339   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   13340     _bfd_elf_end_eh_frame_parsing (info);
   13341 
   13342   if (info->eh_frame_hdr_type
   13343       && !bfd_link_relocatable (info)
   13344       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
   13345     changed = 1;
   13346 
   13347   return changed;
   13348 }
   13349 
   13350 bfd_boolean
   13351 _bfd_elf_section_already_linked (bfd *abfd,
   13352 				 asection *sec,
   13353 				 struct bfd_link_info *info)
   13354 {
   13355   flagword flags;
   13356   const char *name, *key;
   13357   struct bfd_section_already_linked *l;
   13358   struct bfd_section_already_linked_hash_entry *already_linked_list;
   13359 
   13360   if (sec->output_section == bfd_abs_section_ptr)
   13361     return FALSE;
   13362 
   13363   flags = sec->flags;
   13364 
   13365   /* Return if it isn't a linkonce section.  A comdat group section
   13366      also has SEC_LINK_ONCE set.  */
   13367   if ((flags & SEC_LINK_ONCE) == 0)
   13368     return FALSE;
   13369 
   13370   /* Don't put group member sections on our list of already linked
   13371      sections.  They are handled as a group via their group section.  */
   13372   if (elf_sec_group (sec) != NULL)
   13373     return FALSE;
   13374 
   13375   /* For a SHT_GROUP section, use the group signature as the key.  */
   13376   name = sec->name;
   13377   if ((flags & SEC_GROUP) != 0
   13378       && elf_next_in_group (sec) != NULL
   13379       && elf_group_name (elf_next_in_group (sec)) != NULL)
   13380     key = elf_group_name (elf_next_in_group (sec));
   13381   else
   13382     {
   13383       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
   13384       if (CONST_STRNEQ (name, ".gnu.linkonce.")
   13385 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   13386 	key++;
   13387       else
   13388 	/* Must be a user linkonce section that doesn't follow gcc's
   13389 	   naming convention.  In this case we won't be matching
   13390 	   single member groups.  */
   13391 	key = name;
   13392     }
   13393 
   13394   already_linked_list = bfd_section_already_linked_table_lookup (key);
   13395 
   13396   for (l = already_linked_list->entry; l != NULL; l = l->next)
   13397     {
   13398       /* We may have 2 different types of sections on the list: group
   13399 	 sections with a signature of <key> (<key> is some string),
   13400 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
   13401 	 Match like sections.  LTO plugin sections are an exception.
   13402 	 They are always named .gnu.linkonce.t.<key> and match either
   13403 	 type of section.  */
   13404       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
   13405 	   && ((flags & SEC_GROUP) != 0
   13406 	       || strcmp (name, l->sec->name) == 0))
   13407 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
   13408 	{
   13409 	  /* The section has already been linked.  See if we should
   13410 	     issue a warning.  */
   13411 	  if (!_bfd_handle_already_linked (sec, l, info))
   13412 	    return FALSE;
   13413 
   13414 	  if (flags & SEC_GROUP)
   13415 	    {
   13416 	      asection *first = elf_next_in_group (sec);
   13417 	      asection *s = first;
   13418 
   13419 	      while (s != NULL)
   13420 		{
   13421 		  s->output_section = bfd_abs_section_ptr;
   13422 		  /* Record which group discards it.  */
   13423 		  s->kept_section = l->sec;
   13424 		  s = elf_next_in_group (s);
   13425 		  /* These lists are circular.  */
   13426 		  if (s == first)
   13427 		    break;
   13428 		}
   13429 	    }
   13430 
   13431 	  return TRUE;
   13432 	}
   13433     }
   13434 
   13435   /* A single member comdat group section may be discarded by a
   13436      linkonce section and vice versa.  */
   13437   if ((flags & SEC_GROUP) != 0)
   13438     {
   13439       asection *first = elf_next_in_group (sec);
   13440 
   13441       if (first != NULL && elf_next_in_group (first) == first)
   13442 	/* Check this single member group against linkonce sections.  */
   13443 	for (l = already_linked_list->entry; l != NULL; l = l->next)
   13444 	  if ((l->sec->flags & SEC_GROUP) == 0
   13445 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
   13446 	    {
   13447 	      first->output_section = bfd_abs_section_ptr;
   13448 	      first->kept_section = l->sec;
   13449 	      sec->output_section = bfd_abs_section_ptr;
   13450 	      break;
   13451 	    }
   13452     }
   13453   else
   13454     /* Check this linkonce section against single member groups.  */
   13455     for (l = already_linked_list->entry; l != NULL; l = l->next)
   13456       if (l->sec->flags & SEC_GROUP)
   13457 	{
   13458 	  asection *first = elf_next_in_group (l->sec);
   13459 
   13460 	  if (first != NULL
   13461 	      && elf_next_in_group (first) == first
   13462 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
   13463 	    {
   13464 	      sec->output_section = bfd_abs_section_ptr;
   13465 	      sec->kept_section = first;
   13466 	      break;
   13467 	    }
   13468 	}
   13469 
   13470   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
   13471      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
   13472      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
   13473      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
   13474      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
   13475      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
   13476      `.gnu.linkonce.t.F' section from a different bfd not requiring any
   13477      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
   13478      The reverse order cannot happen as there is never a bfd with only the
   13479      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
   13480      matter as here were are looking only for cross-bfd sections.  */
   13481 
   13482   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
   13483     for (l = already_linked_list->entry; l != NULL; l = l->next)
   13484       if ((l->sec->flags & SEC_GROUP) == 0
   13485 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
   13486 	{
   13487 	  if (abfd != l->sec->owner)
   13488 	    sec->output_section = bfd_abs_section_ptr;
   13489 	  break;
   13490 	}
   13491 
   13492   /* This is the first section with this name.  Record it.  */
   13493   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   13494     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   13495   return sec->output_section == bfd_abs_section_ptr;
   13496 }
   13497 
   13498 bfd_boolean
   13499 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
   13500 {
   13501   return sym->st_shndx == SHN_COMMON;
   13502 }
   13503 
   13504 unsigned int
   13505 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
   13506 {
   13507   return SHN_COMMON;
   13508 }
   13509 
   13510 asection *
   13511 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
   13512 {
   13513   return bfd_com_section_ptr;
   13514 }
   13515 
   13516 bfd_vma
   13517 _bfd_elf_default_got_elt_size (bfd *abfd,
   13518 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13519 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   13520 			       bfd *ibfd ATTRIBUTE_UNUSED,
   13521 			       unsigned long symndx ATTRIBUTE_UNUSED)
   13522 {
   13523   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13524   return bed->s->arch_size / 8;
   13525 }
   13526 
   13527 /* Routines to support the creation of dynamic relocs.  */
   13528 
   13529 /* Returns the name of the dynamic reloc section associated with SEC.  */
   13530 
   13531 static const char *
   13532 get_dynamic_reloc_section_name (bfd *       abfd,
   13533 				asection *  sec,
   13534 				bfd_boolean is_rela)
   13535 {
   13536   char *name;
   13537   const char *old_name = bfd_get_section_name (NULL, sec);
   13538   const char *prefix = is_rela ? ".rela" : ".rel";
   13539 
   13540   if (old_name == NULL)
   13541     return NULL;
   13542 
   13543   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
   13544   sprintf (name, "%s%s", prefix, old_name);
   13545 
   13546   return name;
   13547 }
   13548 
   13549 /* Returns the dynamic reloc section associated with SEC.
   13550    If necessary compute the name of the dynamic reloc section based
   13551    on SEC's name (looked up in ABFD's string table) and the setting
   13552    of IS_RELA.  */
   13553 
   13554 asection *
   13555 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
   13556 				    asection *  sec,
   13557 				    bfd_boolean is_rela)
   13558 {
   13559   asection * reloc_sec = elf_section_data (sec)->sreloc;
   13560 
   13561   if (reloc_sec == NULL)
   13562     {
   13563       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   13564 
   13565       if (name != NULL)
   13566 	{
   13567 	  reloc_sec = bfd_get_linker_section (abfd, name);
   13568 
   13569 	  if (reloc_sec != NULL)
   13570 	    elf_section_data (sec)->sreloc = reloc_sec;
   13571 	}
   13572     }
   13573 
   13574   return reloc_sec;
   13575 }
   13576 
   13577 /* Returns the dynamic reloc section associated with SEC.  If the
   13578    section does not exist it is created and attached to the DYNOBJ
   13579    bfd and stored in the SRELOC field of SEC's elf_section_data
   13580    structure.
   13581 
   13582    ALIGNMENT is the alignment for the newly created section and
   13583    IS_RELA defines whether the name should be .rela.<SEC's name>
   13584    or .rel.<SEC's name>.  The section name is looked up in the
   13585    string table associated with ABFD.  */
   13586 
   13587 asection *
   13588 _bfd_elf_make_dynamic_reloc_section (asection *sec,
   13589 				     bfd *dynobj,
   13590 				     unsigned int alignment,
   13591 				     bfd *abfd,
   13592 				     bfd_boolean is_rela)
   13593 {
   13594   asection * reloc_sec = elf_section_data (sec)->sreloc;
   13595 
   13596   if (reloc_sec == NULL)
   13597     {
   13598       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
   13599 
   13600       if (name == NULL)
   13601 	return NULL;
   13602 
   13603       reloc_sec = bfd_get_linker_section (dynobj, name);
   13604 
   13605       if (reloc_sec == NULL)
   13606 	{
   13607 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
   13608 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   13609 	  if ((sec->flags & SEC_ALLOC) != 0)
   13610 	    flags |= SEC_ALLOC | SEC_LOAD;
   13611 
   13612 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
   13613 	  if (reloc_sec != NULL)
   13614 	    {
   13615 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
   13616 		 name.  Override as it may be wrong, eg. for a user
   13617 		 section named "auto" we'll get ".relauto" which is
   13618 		 seen to be a .rela section.  */
   13619 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
   13620 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
   13621 		reloc_sec = NULL;
   13622 	    }
   13623 	}
   13624 
   13625       elf_section_data (sec)->sreloc = reloc_sec;
   13626     }
   13627 
   13628   return reloc_sec;
   13629 }
   13630 
   13631 /* Copy the ELF symbol type and other attributes for a linker script
   13632    assignment from HSRC to HDEST.  Generally this should be treated as
   13633    if we found a strong non-dynamic definition for HDEST (except that
   13634    ld ignores multiple definition errors).  */
   13635 void
   13636 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   13637 				     struct bfd_link_hash_entry *hdest,
   13638 				     struct bfd_link_hash_entry *hsrc)
   13639 {
   13640   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
   13641   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
   13642   Elf_Internal_Sym isym;
   13643 
   13644   ehdest->type = ehsrc->type;
   13645   ehdest->target_internal = ehsrc->target_internal;
   13646 
   13647   isym.st_other = ehsrc->other;
   13648   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
   13649 }
   13650 
   13651 /* Append a RELA relocation REL to section S in BFD.  */
   13652 
   13653 void
   13654 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   13655 {
   13656   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13657   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   13658   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
   13659   bed->s->swap_reloca_out (abfd, rel, loc);
   13660 }
   13661 
   13662 /* Append a REL relocation REL to section S in BFD.  */
   13663 
   13664 void
   13665 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   13666 {
   13667   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13668   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
   13669   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
   13670   bed->s->swap_reloc_out (abfd, rel, loc);
   13671 }
   13672