Home | History | Annotate | Download | only in bfd
      1 /* X86-64 specific support for ELF
      2    Copyright (C) 2000-2014 Free Software Foundation, Inc.
      3    Contributed by Jan Hubicka <jh (at) suse.cz>.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 #include "sysdep.h"
     23 #include "bfd.h"
     24 #include "bfdlink.h"
     25 #include "libbfd.h"
     26 #include "elf-bfd.h"
     27 #include "elf-nacl.h"
     28 #include "bfd_stdint.h"
     29 #include "objalloc.h"
     30 #include "hashtab.h"
     31 #include "dwarf2.h"
     32 #include "libiberty.h"
     33 
     34 #include "elf/x86-64.h"
     35 
     36 #ifdef CORE_HEADER
     37 #include <stdarg.h>
     38 #include CORE_HEADER
     39 #endif
     40 
     41 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
     42 #define MINUS_ONE (~ (bfd_vma) 0)
     43 
     44 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
     45    identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
     46    relocation type.  We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
     47    since they are the same.  */
     48 
     49 #define ABI_64_P(abfd) \
     50   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
     51 
     52 /* The relocation "howto" table.  Order of fields:
     53    type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
     54    special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset.  */
     55 static reloc_howto_type x86_64_elf_howto_table[] =
     56 {
     57   HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
     58 	bfd_elf_generic_reloc, "R_X86_64_NONE",	FALSE, 0x00000000, 0x00000000,
     59 	FALSE),
     60   HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     61 	bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
     62 	FALSE),
     63   HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     64 	bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
     65 	TRUE),
     66   HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
     67 	bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
     68 	FALSE),
     69   HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     70 	bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
     71 	TRUE),
     72   HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
     73 	bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
     74 	FALSE),
     75   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     76 	bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
     77 	MINUS_ONE, FALSE),
     78   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     79 	bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
     80 	MINUS_ONE, FALSE),
     81   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     82 	bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
     83 	MINUS_ONE, FALSE),
     84   HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     85 	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
     86 	0xffffffff, TRUE),
     87   HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
     88 	bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
     89 	FALSE),
     90   HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
     91 	bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
     92 	FALSE),
     93   HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
     94 	bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
     95   HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
     96 	bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
     97   HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
     98 	bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
     99   HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
    100 	bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
    101   HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    102 	bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
    103 	MINUS_ONE, FALSE),
    104   HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    105 	bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
    106 	MINUS_ONE, FALSE),
    107   HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    108 	bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
    109 	MINUS_ONE, FALSE),
    110   HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    111 	bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
    112 	0xffffffff, TRUE),
    113   HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    114 	bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
    115 	0xffffffff, TRUE),
    116   HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
    117 	bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
    118 	0xffffffff, FALSE),
    119   HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    120 	bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
    121 	0xffffffff, TRUE),
    122   HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
    123 	bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
    124 	0xffffffff, FALSE),
    125   HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
    126 	bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
    127 	TRUE),
    128   HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    129 	bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
    130 	FALSE, MINUS_ONE, MINUS_ONE, FALSE),
    131   HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    132 	bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
    133 	FALSE, 0xffffffff, 0xffffffff, TRUE),
    134   HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
    135 	bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
    136 	FALSE),
    137   HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
    138 	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
    139 	MINUS_ONE, TRUE),
    140   HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
    141 	bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
    142 	FALSE, MINUS_ONE, MINUS_ONE, TRUE),
    143   HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
    144 	bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
    145 	MINUS_ONE, FALSE),
    146   HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
    147 	bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
    148 	MINUS_ONE, FALSE),
    149   HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
    150 	bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
    151 	FALSE),
    152   HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
    153 	bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
    154 	FALSE),
    155   HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
    156 	complain_overflow_bitfield, bfd_elf_generic_reloc,
    157 	"R_X86_64_GOTPC32_TLSDESC",
    158 	FALSE, 0xffffffff, 0xffffffff, TRUE),
    159   HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
    160 	complain_overflow_dont, bfd_elf_generic_reloc,
    161 	"R_X86_64_TLSDESC_CALL",
    162 	FALSE, 0, 0, FALSE),
    163   HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
    164 	complain_overflow_bitfield, bfd_elf_generic_reloc,
    165 	"R_X86_64_TLSDESC",
    166 	FALSE, MINUS_ONE, MINUS_ONE, FALSE),
    167   HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    168 	bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
    169 	MINUS_ONE, FALSE),
    170   HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
    171 	bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
    172 	MINUS_ONE, FALSE),
    173   HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    174 	bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
    175 	TRUE),
    176   HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
    177 	bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
    178 	TRUE),
    179 
    180   /* We have a gap in the reloc numbers here.
    181      R_X86_64_standard counts the number up to this point, and
    182      R_X86_64_vt_offset is the value to subtract from a reloc type of
    183      R_X86_64_GNU_VT* to form an index into this table.  */
    184 #define R_X86_64_standard (R_X86_64_PLT32_BND + 1)
    185 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
    186 
    187 /* GNU extension to record C++ vtable hierarchy.  */
    188   HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
    189 	 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
    190 
    191 /* GNU extension to record C++ vtable member usage.  */
    192   HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
    193 	 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
    194 	 FALSE),
    195 
    196 /* Use complain_overflow_bitfield on R_X86_64_32 for x32.  */
    197   HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
    198 	bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
    199 	FALSE)
    200 };
    201 
    202 #define IS_X86_64_PCREL_TYPE(TYPE)	\
    203   (   ((TYPE) == R_X86_64_PC8)		\
    204    || ((TYPE) == R_X86_64_PC16)		\
    205    || ((TYPE) == R_X86_64_PC32)		\
    206    || ((TYPE) == R_X86_64_PC32_BND)	\
    207    || ((TYPE) == R_X86_64_PC64))
    208 
    209 /* Map BFD relocs to the x86_64 elf relocs.  */
    210 struct elf_reloc_map
    211 {
    212   bfd_reloc_code_real_type bfd_reloc_val;
    213   unsigned char elf_reloc_val;
    214 };
    215 
    216 static const struct elf_reloc_map x86_64_reloc_map[] =
    217 {
    218   { BFD_RELOC_NONE,		R_X86_64_NONE, },
    219   { BFD_RELOC_64,		R_X86_64_64,   },
    220   { BFD_RELOC_32_PCREL,		R_X86_64_PC32, },
    221   { BFD_RELOC_X86_64_GOT32,	R_X86_64_GOT32,},
    222   { BFD_RELOC_X86_64_PLT32,	R_X86_64_PLT32,},
    223   { BFD_RELOC_X86_64_COPY,	R_X86_64_COPY, },
    224   { BFD_RELOC_X86_64_GLOB_DAT,	R_X86_64_GLOB_DAT, },
    225   { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
    226   { BFD_RELOC_X86_64_RELATIVE,	R_X86_64_RELATIVE, },
    227   { BFD_RELOC_X86_64_GOTPCREL,	R_X86_64_GOTPCREL, },
    228   { BFD_RELOC_32,		R_X86_64_32, },
    229   { BFD_RELOC_X86_64_32S,	R_X86_64_32S, },
    230   { BFD_RELOC_16,		R_X86_64_16, },
    231   { BFD_RELOC_16_PCREL,		R_X86_64_PC16, },
    232   { BFD_RELOC_8,		R_X86_64_8, },
    233   { BFD_RELOC_8_PCREL,		R_X86_64_PC8, },
    234   { BFD_RELOC_X86_64_DTPMOD64,	R_X86_64_DTPMOD64, },
    235   { BFD_RELOC_X86_64_DTPOFF64,	R_X86_64_DTPOFF64, },
    236   { BFD_RELOC_X86_64_TPOFF64,	R_X86_64_TPOFF64, },
    237   { BFD_RELOC_X86_64_TLSGD,	R_X86_64_TLSGD, },
    238   { BFD_RELOC_X86_64_TLSLD,	R_X86_64_TLSLD, },
    239   { BFD_RELOC_X86_64_DTPOFF32,	R_X86_64_DTPOFF32, },
    240   { BFD_RELOC_X86_64_GOTTPOFF,	R_X86_64_GOTTPOFF, },
    241   { BFD_RELOC_X86_64_TPOFF32,	R_X86_64_TPOFF32, },
    242   { BFD_RELOC_64_PCREL,		R_X86_64_PC64, },
    243   { BFD_RELOC_X86_64_GOTOFF64,	R_X86_64_GOTOFF64, },
    244   { BFD_RELOC_X86_64_GOTPC32,	R_X86_64_GOTPC32, },
    245   { BFD_RELOC_X86_64_GOT64,	R_X86_64_GOT64, },
    246   { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
    247   { BFD_RELOC_X86_64_GOTPC64,	R_X86_64_GOTPC64, },
    248   { BFD_RELOC_X86_64_GOTPLT64,	R_X86_64_GOTPLT64, },
    249   { BFD_RELOC_X86_64_PLTOFF64,	R_X86_64_PLTOFF64, },
    250   { BFD_RELOC_SIZE32,		R_X86_64_SIZE32, },
    251   { BFD_RELOC_SIZE64,		R_X86_64_SIZE64, },
    252   { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
    253   { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
    254   { BFD_RELOC_X86_64_TLSDESC,	R_X86_64_TLSDESC, },
    255   { BFD_RELOC_X86_64_IRELATIVE,	R_X86_64_IRELATIVE, },
    256   { BFD_RELOC_X86_64_PC32_BND,	R_X86_64_PC32_BND,},
    257   { BFD_RELOC_X86_64_PLT32_BND,	R_X86_64_PLT32_BND,},
    258   { BFD_RELOC_VTABLE_INHERIT,	R_X86_64_GNU_VTINHERIT, },
    259   { BFD_RELOC_VTABLE_ENTRY,	R_X86_64_GNU_VTENTRY, },
    260 };
    261 
    262 static reloc_howto_type *
    263 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
    264 {
    265   unsigned i;
    266 
    267   if (r_type == (unsigned int) R_X86_64_32)
    268     {
    269       if (ABI_64_P (abfd))
    270 	i = r_type;
    271       else
    272 	i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
    273     }
    274   else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
    275 	   || r_type >= (unsigned int) R_X86_64_max)
    276     {
    277       if (r_type >= (unsigned int) R_X86_64_standard)
    278 	{
    279 	  (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
    280 				 abfd, (int) r_type);
    281 	  r_type = R_X86_64_NONE;
    282 	}
    283       i = r_type;
    284     }
    285   else
    286     i = r_type - (unsigned int) R_X86_64_vt_offset;
    287   BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
    288   return &x86_64_elf_howto_table[i];
    289 }
    290 
    291 /* Given a BFD reloc type, return a HOWTO structure.  */
    292 static reloc_howto_type *
    293 elf_x86_64_reloc_type_lookup (bfd *abfd,
    294 			      bfd_reloc_code_real_type code)
    295 {
    296   unsigned int i;
    297 
    298   for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
    299        i++)
    300     {
    301       if (x86_64_reloc_map[i].bfd_reloc_val == code)
    302 	return elf_x86_64_rtype_to_howto (abfd,
    303 					  x86_64_reloc_map[i].elf_reloc_val);
    304     }
    305   return 0;
    306 }
    307 
    308 static reloc_howto_type *
    309 elf_x86_64_reloc_name_lookup (bfd *abfd,
    310 			      const char *r_name)
    311 {
    312   unsigned int i;
    313 
    314   if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
    315     {
    316       /* Get x32 R_X86_64_32.  */
    317       reloc_howto_type *reloc
    318 	= &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
    319       BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
    320       return reloc;
    321     }
    322 
    323   for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
    324     if (x86_64_elf_howto_table[i].name != NULL
    325 	&& strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
    326       return &x86_64_elf_howto_table[i];
    327 
    328   return NULL;
    329 }
    330 
    331 /* Given an x86_64 ELF reloc type, fill in an arelent structure.  */
    332 
    333 static void
    334 elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
    335 			  Elf_Internal_Rela *dst)
    336 {
    337   unsigned r_type;
    338 
    339   r_type = ELF32_R_TYPE (dst->r_info);
    340   cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
    341   BFD_ASSERT (r_type == cache_ptr->howto->type);
    342 }
    343 
    344 /* Support for core dump NOTE sections.  */
    346 static bfd_boolean
    347 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
    348 {
    349   int offset;
    350   size_t size;
    351 
    352   switch (note->descsz)
    353     {
    354       default:
    355 	return FALSE;
    356 
    357       case 296:		/* sizeof(istruct elf_prstatus) on Linux/x32 */
    358 	/* pr_cursig */
    359 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
    360 
    361 	/* pr_pid */
    362 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
    363 
    364 	/* pr_reg */
    365 	offset = 72;
    366 	size = 216;
    367 
    368 	break;
    369 
    370       case 336:		/* sizeof(istruct elf_prstatus) on Linux/x86_64 */
    371 	/* pr_cursig */
    372 	elf_tdata (abfd)->core->signal
    373 	  = bfd_get_16 (abfd, note->descdata + 12);
    374 
    375 	/* pr_pid */
    376 	elf_tdata (abfd)->core->lwpid
    377 	  = bfd_get_32 (abfd, note->descdata + 32);
    378 
    379 	/* pr_reg */
    380 	offset = 112;
    381 	size = 216;
    382 
    383 	break;
    384     }
    385 
    386   /* Make a ".reg/999" section.  */
    387   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
    388 					  size, note->descpos + offset);
    389 }
    390 
    391 static bfd_boolean
    392 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
    393 {
    394   switch (note->descsz)
    395     {
    396       default:
    397 	return FALSE;
    398 
    399       case 124:		/* sizeof(struct elf_prpsinfo) on Linux/x32 */
    400 	elf_tdata (abfd)->core->pid
    401 	  = bfd_get_32 (abfd, note->descdata + 12);
    402 	elf_tdata (abfd)->core->program
    403 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
    404 	elf_tdata (abfd)->core->command
    405 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
    406 	break;
    407 
    408       case 136:		/* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
    409 	elf_tdata (abfd)->core->pid
    410 	  = bfd_get_32 (abfd, note->descdata + 24);
    411 	elf_tdata (abfd)->core->program
    412 	 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
    413 	elf_tdata (abfd)->core->command
    414 	 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
    415     }
    416 
    417   /* Note that for some reason, a spurious space is tacked
    418      onto the end of the args in some (at least one anyway)
    419      implementations, so strip it off if it exists.  */
    420 
    421   {
    422     char *command = elf_tdata (abfd)->core->command;
    423     int n = strlen (command);
    424 
    425     if (0 < n && command[n - 1] == ' ')
    426       command[n - 1] = '\0';
    427   }
    428 
    429   return TRUE;
    430 }
    431 
    432 #ifdef CORE_HEADER
    433 static char *
    434 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
    435 			    int note_type, ...)
    436 {
    437   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    438   va_list ap;
    439   const char *fname, *psargs;
    440   long pid;
    441   int cursig;
    442   const void *gregs;
    443 
    444   switch (note_type)
    445     {
    446     default:
    447       return NULL;
    448 
    449     case NT_PRPSINFO:
    450       va_start (ap, note_type);
    451       fname = va_arg (ap, const char *);
    452       psargs = va_arg (ap, const char *);
    453       va_end (ap);
    454 
    455       if (bed->s->elfclass == ELFCLASS32)
    456 	{
    457 	  prpsinfo32_t data;
    458 	  memset (&data, 0, sizeof (data));
    459 	  strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
    460 	  strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
    461 	  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
    462 				     &data, sizeof (data));
    463 	}
    464       else
    465 	{
    466 	  prpsinfo64_t data;
    467 	  memset (&data, 0, sizeof (data));
    468 	  strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
    469 	  strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
    470 	  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
    471 				     &data, sizeof (data));
    472 	}
    473       /* NOTREACHED */
    474 
    475     case NT_PRSTATUS:
    476       va_start (ap, note_type);
    477       pid = va_arg (ap, long);
    478       cursig = va_arg (ap, int);
    479       gregs = va_arg (ap, const void *);
    480       va_end (ap);
    481 
    482       if (bed->s->elfclass == ELFCLASS32)
    483 	{
    484 	  if (bed->elf_machine_code == EM_X86_64)
    485 	    {
    486 	      prstatusx32_t prstat;
    487 	      memset (&prstat, 0, sizeof (prstat));
    488 	      prstat.pr_pid = pid;
    489 	      prstat.pr_cursig = cursig;
    490 	      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
    491 	      return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
    492 					 &prstat, sizeof (prstat));
    493 	    }
    494 	  else
    495 	    {
    496 	      prstatus32_t prstat;
    497 	      memset (&prstat, 0, sizeof (prstat));
    498 	      prstat.pr_pid = pid;
    499 	      prstat.pr_cursig = cursig;
    500 	      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
    501 	      return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
    502 					 &prstat, sizeof (prstat));
    503 	    }
    504 	}
    505       else
    506 	{
    507 	  prstatus64_t prstat;
    508 	  memset (&prstat, 0, sizeof (prstat));
    509 	  prstat.pr_pid = pid;
    510 	  prstat.pr_cursig = cursig;
    511 	  memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
    512 	  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
    513 				     &prstat, sizeof (prstat));
    514 	}
    515     }
    516   /* NOTREACHED */
    517 }
    518 #endif
    519 
    520 /* Functions for the x86-64 ELF linker.	 */
    522 
    523 /* The name of the dynamic interpreter.	 This is put in the .interp
    524    section.  */
    525 
    526 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
    527 #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
    528 
    529 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
    530    copying dynamic variables from a shared lib into an app's dynbss
    531    section, and instead use a dynamic relocation to point into the
    532    shared lib.  */
    533 #define ELIMINATE_COPY_RELOCS 1
    534 
    535 /* The size in bytes of an entry in the global offset table.  */
    536 
    537 #define GOT_ENTRY_SIZE 8
    538 
    539 /* The size in bytes of an entry in the procedure linkage table.  */
    540 
    541 #define PLT_ENTRY_SIZE 16
    542 
    543 /* The first entry in a procedure linkage table looks like this.  See the
    544    SVR4 ABI i386 supplement and the x86-64 ABI to see how this works.  */
    545 
    546 static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
    547 {
    548   0xff, 0x35, 8, 0, 0, 0,	/* pushq GOT+8(%rip)  */
    549   0xff, 0x25, 16, 0, 0, 0,	/* jmpq *GOT+16(%rip) */
    550   0x0f, 0x1f, 0x40, 0x00	/* nopl 0(%rax)       */
    551 };
    552 
    553 /* Subsequent entries in a procedure linkage table look like this.  */
    554 
    555 static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
    556 {
    557   0xff, 0x25,	/* jmpq *name@GOTPC(%rip) */
    558   0, 0, 0, 0,	/* replaced with offset to this symbol in .got.	 */
    559   0x68,		/* pushq immediate */
    560   0, 0, 0, 0,	/* replaced with index into relocation table.  */
    561   0xe9,		/* jmp relative */
    562   0, 0, 0, 0	/* replaced with offset to start of .plt0.  */
    563 };
    564 
    565 /* The first entry in a procedure linkage table with BND relocations
    566    like this.  */
    567 
    568 static const bfd_byte elf_x86_64_bnd_plt0_entry[PLT_ENTRY_SIZE] =
    569 {
    570   0xff, 0x35, 8, 0, 0, 0,         /* pushq GOT+8(%rip)        */
    571   0xf2, 0xff, 0x25, 16, 0, 0, 0,  /* bnd jmpq *GOT+16(%rip)   */
    572   0x0f, 0x1f, 0                   /* nopl (%rax)              */
    573 };
    574 
    575 /* Subsequent entries for legacy branches in a procedure linkage table
    576    with BND relocations look like this.  */
    577 
    578 static const bfd_byte elf_x86_64_legacy_plt_entry[PLT_ENTRY_SIZE] =
    579 {
    580   0x68, 0, 0, 0, 0,             /* pushq immediate            */
    581   0xe9, 0, 0, 0, 0,             /* jmpq relative              */
    582   0x66, 0x0f, 0x1f, 0x44, 0, 0  /* nopw (%rax,%rax,1)         */
    583 };
    584 
    585 /* Subsequent entries for branches with BND prefx in a procedure linkage
    586    table with BND relocations look like this.  */
    587 
    588 static const bfd_byte elf_x86_64_bnd_plt_entry[PLT_ENTRY_SIZE] =
    589 {
    590   0x68, 0, 0, 0, 0,             /* pushq immediate            */
    591   0xf2, 0xe9, 0, 0, 0, 0,       /* bnd jmpq relative          */
    592   0x0f, 0x1f, 0x44, 0, 0        /* nopl 0(%rax,%rax,1)        */
    593 };
    594 
    595 /* Entries for legacy branches in the second procedure linkage table
    596    look like this.  */
    597 
    598 static const bfd_byte elf_x86_64_legacy_plt2_entry[8] =
    599 {
    600   0xff, 0x25,                    /* jmpq *name@GOTPC(%rip)      */
    601   0, 0, 0, 0,  /* replaced with offset to this symbol in .got.  */
    602   0x66, 0x90                     /* xchg %ax,%ax                */
    603 };
    604 
    605 /* Entries for branches with BND prefix in the second procedure linkage
    606    table look like this.  */
    607 
    608 static const bfd_byte elf_x86_64_bnd_plt2_entry[8] =
    609 {
    610   0xf2, 0xff, 0x25,              /* bnd jmpq *name@GOTPC(%rip)  */
    611   0, 0, 0, 0,  /* replaced with offset to this symbol in .got.  */
    612   0x90                           /* nop                         */
    613 };
    614 
    615 /* .eh_frame covering the .plt section.  */
    616 
    617 static const bfd_byte elf_x86_64_eh_frame_plt[] =
    618 {
    619 #define PLT_CIE_LENGTH		20
    620 #define PLT_FDE_LENGTH		36
    621 #define PLT_FDE_START_OFFSET	4 + PLT_CIE_LENGTH + 8
    622 #define PLT_FDE_LEN_OFFSET	4 + PLT_CIE_LENGTH + 12
    623   PLT_CIE_LENGTH, 0, 0, 0,	/* CIE length */
    624   0, 0, 0, 0,			/* CIE ID */
    625   1,				/* CIE version */
    626   'z', 'R', 0,			/* Augmentation string */
    627   1,				/* Code alignment factor */
    628   0x78,				/* Data alignment factor */
    629   16,				/* Return address column */
    630   1,				/* Augmentation size */
    631   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
    632   DW_CFA_def_cfa, 7, 8,		/* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
    633   DW_CFA_offset + 16, 1,	/* DW_CFA_offset: r16 (rip) at cfa-8 */
    634   DW_CFA_nop, DW_CFA_nop,
    635 
    636   PLT_FDE_LENGTH, 0, 0, 0,	/* FDE length */
    637   PLT_CIE_LENGTH + 8, 0, 0, 0,	/* CIE pointer */
    638   0, 0, 0, 0,			/* R_X86_64_PC32 .plt goes here */
    639   0, 0, 0, 0,			/* .plt size goes here */
    640   0,				/* Augmentation size */
    641   DW_CFA_def_cfa_offset, 16,	/* DW_CFA_def_cfa_offset: 16 */
    642   DW_CFA_advance_loc + 6,	/* DW_CFA_advance_loc: 6 to __PLT__+6 */
    643   DW_CFA_def_cfa_offset, 24,	/* DW_CFA_def_cfa_offset: 24 */
    644   DW_CFA_advance_loc + 10,	/* DW_CFA_advance_loc: 10 to __PLT__+16 */
    645   DW_CFA_def_cfa_expression,	/* DW_CFA_def_cfa_expression */
    646   11,				/* Block length */
    647   DW_OP_breg7, 8,		/* DW_OP_breg7 (rsp): 8 */
    648   DW_OP_breg16, 0,		/* DW_OP_breg16 (rip): 0 */
    649   DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
    650   DW_OP_lit3, DW_OP_shl, DW_OP_plus,
    651   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
    652 };
    653 
    654 /* Architecture-specific backend data for x86-64.  */
    655 
    656 struct elf_x86_64_backend_data
    657 {
    658   /* Templates for the initial PLT entry and for subsequent entries.  */
    659   const bfd_byte *plt0_entry;
    660   const bfd_byte *plt_entry;
    661   unsigned int plt_entry_size;          /* Size of each PLT entry.  */
    662 
    663   /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2].  */
    664   unsigned int plt0_got1_offset;
    665   unsigned int plt0_got2_offset;
    666 
    667   /* Offset of the end of the PC-relative instruction containing
    668      plt0_got2_offset.  */
    669   unsigned int plt0_got2_insn_end;
    670 
    671   /* Offsets into plt_entry that are to be replaced with...  */
    672   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
    673   unsigned int plt_reloc_offset;  /* ... offset into relocation table. */
    674   unsigned int plt_plt_offset;    /* ... offset to start of .plt. */
    675 
    676   /* Length of the PC-relative instruction containing plt_got_offset.  */
    677   unsigned int plt_got_insn_size;
    678 
    679   /* Offset of the end of the PC-relative jump to plt0_entry.  */
    680   unsigned int plt_plt_insn_end;
    681 
    682   /* Offset into plt_entry where the initial value of the GOT entry points.  */
    683   unsigned int plt_lazy_offset;
    684 
    685   /* .eh_frame covering the .plt section.  */
    686   const bfd_byte *eh_frame_plt;
    687   unsigned int eh_frame_plt_size;
    688 };
    689 
    690 #define get_elf_x86_64_arch_data(bed) \
    691   ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
    692 
    693 #define get_elf_x86_64_backend_data(abfd) \
    694   get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
    695 
    696 #define GET_PLT_ENTRY_SIZE(abfd) \
    697   get_elf_x86_64_backend_data (abfd)->plt_entry_size
    698 
    699 /* These are the standard parameters.  */
    700 static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
    701   {
    702     elf_x86_64_plt0_entry,              /* plt0_entry */
    703     elf_x86_64_plt_entry,               /* plt_entry */
    704     sizeof (elf_x86_64_plt_entry),      /* plt_entry_size */
    705     2,                                  /* plt0_got1_offset */
    706     8,                                  /* plt0_got2_offset */
    707     12,                                 /* plt0_got2_insn_end */
    708     2,                                  /* plt_got_offset */
    709     7,                                  /* plt_reloc_offset */
    710     12,                                 /* plt_plt_offset */
    711     6,                                  /* plt_got_insn_size */
    712     PLT_ENTRY_SIZE,                     /* plt_plt_insn_end */
    713     6,                                  /* plt_lazy_offset */
    714     elf_x86_64_eh_frame_plt,            /* eh_frame_plt */
    715     sizeof (elf_x86_64_eh_frame_plt),   /* eh_frame_plt_size */
    716   };
    717 
    718 static const struct elf_x86_64_backend_data elf_x86_64_bnd_arch_bed =
    719   {
    720     elf_x86_64_bnd_plt0_entry,          /* plt0_entry */
    721     elf_x86_64_bnd_plt_entry,           /* plt_entry */
    722     sizeof (elf_x86_64_bnd_plt_entry),  /* plt_entry_size */
    723     2,                                  /* plt0_got1_offset */
    724     1+8,                                /* plt0_got2_offset */
    725     1+12,                               /* plt0_got2_insn_end */
    726     1+2,                                /* plt_got_offset */
    727     1,                                  /* plt_reloc_offset */
    728     7,                                  /* plt_plt_offset */
    729     1+6,                                /* plt_got_insn_size */
    730     11,                                 /* plt_plt_insn_end */
    731     0,                                  /* plt_lazy_offset */
    732     elf_x86_64_eh_frame_plt,            /* eh_frame_plt */
    733     sizeof (elf_x86_64_eh_frame_plt),   /* eh_frame_plt_size */
    734   };
    735 
    736 #define	elf_backend_arch_data	&elf_x86_64_arch_bed
    737 
    738 /* x86-64 ELF linker hash entry.  */
    739 
    740 struct elf_x86_64_link_hash_entry
    741 {
    742   struct elf_link_hash_entry elf;
    743 
    744   /* Track dynamic relocs copied for this symbol.  */
    745   struct elf_dyn_relocs *dyn_relocs;
    746 
    747 #define GOT_UNKNOWN	0
    748 #define GOT_NORMAL	1
    749 #define GOT_TLS_GD	2
    750 #define GOT_TLS_IE	3
    751 #define GOT_TLS_GDESC	4
    752 #define GOT_TLS_GD_BOTH_P(type) \
    753   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
    754 #define GOT_TLS_GD_P(type) \
    755   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
    756 #define GOT_TLS_GDESC_P(type) \
    757   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
    758 #define GOT_TLS_GD_ANY_P(type) \
    759   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
    760   unsigned char tls_type;
    761 
    762   /* TRUE if symbol has at least one BND relocation.  */
    763   bfd_boolean has_bnd_reloc;
    764 
    765   /* Information about the second PLT entry. Filled when has_bnd_reloc is
    766      set.  */
    767   union gotplt_union plt_bnd;
    768 
    769   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
    770      starting at the end of the jump table.  */
    771   bfd_vma tlsdesc_got;
    772 };
    773 
    774 #define elf_x86_64_hash_entry(ent) \
    775   ((struct elf_x86_64_link_hash_entry *)(ent))
    776 
    777 struct elf_x86_64_obj_tdata
    778 {
    779   struct elf_obj_tdata root;
    780 
    781   /* tls_type for each local got entry.  */
    782   char *local_got_tls_type;
    783 
    784   /* GOTPLT entries for TLS descriptors.  */
    785   bfd_vma *local_tlsdesc_gotent;
    786 };
    787 
    788 #define elf_x86_64_tdata(abfd) \
    789   ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
    790 
    791 #define elf_x86_64_local_got_tls_type(abfd) \
    792   (elf_x86_64_tdata (abfd)->local_got_tls_type)
    793 
    794 #define elf_x86_64_local_tlsdesc_gotent(abfd) \
    795   (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
    796 
    797 #define is_x86_64_elf(bfd)				\
    798   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    799    && elf_tdata (bfd) != NULL				\
    800    && elf_object_id (bfd) == X86_64_ELF_DATA)
    801 
    802 static bfd_boolean
    803 elf_x86_64_mkobject (bfd *abfd)
    804 {
    805   return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
    806 				  X86_64_ELF_DATA);
    807 }
    808 
    809 /* x86-64 ELF linker hash table.  */
    810 
    811 struct elf_x86_64_link_hash_table
    812 {
    813   struct elf_link_hash_table elf;
    814 
    815   /* Short-cuts to get to dynamic linker sections.  */
    816   asection *sdynbss;
    817   asection *srelbss;
    818   asection *plt_eh_frame;
    819   asection *plt_bnd;
    820 
    821   union
    822   {
    823     bfd_signed_vma refcount;
    824     bfd_vma offset;
    825   } tls_ld_got;
    826 
    827   /* The amount of space used by the jump slots in the GOT.  */
    828   bfd_vma sgotplt_jump_table_size;
    829 
    830   /* Small local sym cache.  */
    831   struct sym_cache sym_cache;
    832 
    833   bfd_vma (*r_info) (bfd_vma, bfd_vma);
    834   bfd_vma (*r_sym) (bfd_vma);
    835   unsigned int pointer_r_type;
    836   const char *dynamic_interpreter;
    837   int dynamic_interpreter_size;
    838 
    839   /* _TLS_MODULE_BASE_ symbol.  */
    840   struct bfd_link_hash_entry *tls_module_base;
    841 
    842   /* Used by local STT_GNU_IFUNC symbols.  */
    843   htab_t loc_hash_table;
    844   void * loc_hash_memory;
    845 
    846   /* The offset into splt of the PLT entry for the TLS descriptor
    847      resolver.  Special values are 0, if not necessary (or not found
    848      to be necessary yet), and -1 if needed but not determined
    849      yet.  */
    850   bfd_vma tlsdesc_plt;
    851   /* The offset into sgot of the GOT entry used by the PLT entry
    852      above.  */
    853   bfd_vma tlsdesc_got;
    854 
    855   /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt.  */
    856   bfd_vma next_jump_slot_index;
    857   /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt.  */
    858   bfd_vma next_irelative_index;
    859 };
    860 
    861 /* Get the x86-64 ELF linker hash table from a link_info structure.  */
    862 
    863 #define elf_x86_64_hash_table(p) \
    864   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    865   == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
    866 
    867 #define elf_x86_64_compute_jump_table_size(htab) \
    868   ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
    869 
    870 /* Create an entry in an x86-64 ELF linker hash table.	*/
    871 
    872 static struct bfd_hash_entry *
    873 elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
    874 			      struct bfd_hash_table *table,
    875 			      const char *string)
    876 {
    877   /* Allocate the structure if it has not already been allocated by a
    878      subclass.  */
    879   if (entry == NULL)
    880     {
    881       entry = (struct bfd_hash_entry *)
    882 	  bfd_hash_allocate (table,
    883 			     sizeof (struct elf_x86_64_link_hash_entry));
    884       if (entry == NULL)
    885 	return entry;
    886     }
    887 
    888   /* Call the allocation method of the superclass.  */
    889   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
    890   if (entry != NULL)
    891     {
    892       struct elf_x86_64_link_hash_entry *eh;
    893 
    894       eh = (struct elf_x86_64_link_hash_entry *) entry;
    895       eh->dyn_relocs = NULL;
    896       eh->tls_type = GOT_UNKNOWN;
    897       eh->has_bnd_reloc = FALSE;
    898       eh->plt_bnd.offset = (bfd_vma) -1;
    899       eh->tlsdesc_got = (bfd_vma) -1;
    900     }
    901 
    902   return entry;
    903 }
    904 
    905 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
    906   for local symbol so that we can handle local STT_GNU_IFUNC symbols
    907   as global symbol.  We reuse indx and dynstr_index for local symbol
    908   hash since they aren't used by global symbols in this backend.  */
    909 
    910 static hashval_t
    911 elf_x86_64_local_htab_hash (const void *ptr)
    912 {
    913   struct elf_link_hash_entry *h
    914     = (struct elf_link_hash_entry *) ptr;
    915   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
    916 }
    917 
    918 /* Compare local hash entries.  */
    919 
    920 static int
    921 elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
    922 {
    923   struct elf_link_hash_entry *h1
    924      = (struct elf_link_hash_entry *) ptr1;
    925   struct elf_link_hash_entry *h2
    926     = (struct elf_link_hash_entry *) ptr2;
    927 
    928   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
    929 }
    930 
    931 /* Find and/or create a hash entry for local symbol.  */
    932 
    933 static struct elf_link_hash_entry *
    934 elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
    935 			       bfd *abfd, const Elf_Internal_Rela *rel,
    936 			       bfd_boolean create)
    937 {
    938   struct elf_x86_64_link_hash_entry e, *ret;
    939   asection *sec = abfd->sections;
    940   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
    941 				       htab->r_sym (rel->r_info));
    942   void **slot;
    943 
    944   e.elf.indx = sec->id;
    945   e.elf.dynstr_index = htab->r_sym (rel->r_info);
    946   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
    947 				   create ? INSERT : NO_INSERT);
    948 
    949   if (!slot)
    950     return NULL;
    951 
    952   if (*slot)
    953     {
    954       ret = (struct elf_x86_64_link_hash_entry *) *slot;
    955       return &ret->elf;
    956     }
    957 
    958   ret = (struct elf_x86_64_link_hash_entry *)
    959 	objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
    960 			sizeof (struct elf_x86_64_link_hash_entry));
    961   if (ret)
    962     {
    963       memset (ret, 0, sizeof (*ret));
    964       ret->elf.indx = sec->id;
    965       ret->elf.dynstr_index = htab->r_sym (rel->r_info);
    966       ret->elf.dynindx = -1;
    967       *slot = ret;
    968     }
    969   return &ret->elf;
    970 }
    971 
    972 /* Destroy an X86-64 ELF linker hash table.  */
    973 
    974 static void
    975 elf_x86_64_link_hash_table_free (bfd *obfd)
    976 {
    977   struct elf_x86_64_link_hash_table *htab
    978     = (struct elf_x86_64_link_hash_table *) obfd->link.hash;
    979 
    980   if (htab->loc_hash_table)
    981     htab_delete (htab->loc_hash_table);
    982   if (htab->loc_hash_memory)
    983     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
    984   _bfd_elf_link_hash_table_free (obfd);
    985 }
    986 
    987 /* Create an X86-64 ELF linker hash table.  */
    988 
    989 static struct bfd_link_hash_table *
    990 elf_x86_64_link_hash_table_create (bfd *abfd)
    991 {
    992   struct elf_x86_64_link_hash_table *ret;
    993   bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
    994 
    995   ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
    996   if (ret == NULL)
    997     return NULL;
    998 
    999   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
   1000 				      elf_x86_64_link_hash_newfunc,
   1001 				      sizeof (struct elf_x86_64_link_hash_entry),
   1002 				      X86_64_ELF_DATA))
   1003     {
   1004       free (ret);
   1005       return NULL;
   1006     }
   1007 
   1008   if (ABI_64_P (abfd))
   1009     {
   1010       ret->r_info = elf64_r_info;
   1011       ret->r_sym = elf64_r_sym;
   1012       ret->pointer_r_type = R_X86_64_64;
   1013       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
   1014       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
   1015     }
   1016   else
   1017     {
   1018       ret->r_info = elf32_r_info;
   1019       ret->r_sym = elf32_r_sym;
   1020       ret->pointer_r_type = R_X86_64_32;
   1021       ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
   1022       ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
   1023     }
   1024 
   1025   ret->loc_hash_table = htab_try_create (1024,
   1026 					 elf_x86_64_local_htab_hash,
   1027 					 elf_x86_64_local_htab_eq,
   1028 					 NULL);
   1029   ret->loc_hash_memory = objalloc_create ();
   1030   if (!ret->loc_hash_table || !ret->loc_hash_memory)
   1031     {
   1032       elf_x86_64_link_hash_table_free (abfd);
   1033       return NULL;
   1034     }
   1035   ret->elf.root.hash_table_free = elf_x86_64_link_hash_table_free;
   1036 
   1037   return &ret->elf.root;
   1038 }
   1039 
   1040 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
   1041    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
   1042    hash table.  */
   1043 
   1044 static bfd_boolean
   1045 elf_x86_64_create_dynamic_sections (bfd *dynobj,
   1046 				    struct bfd_link_info *info)
   1047 {
   1048   struct elf_x86_64_link_hash_table *htab;
   1049 
   1050   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   1051     return FALSE;
   1052 
   1053   htab = elf_x86_64_hash_table (info);
   1054   if (htab == NULL)
   1055     return FALSE;
   1056 
   1057   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
   1058   if (!info->shared)
   1059     htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
   1060 
   1061   if (!htab->sdynbss
   1062       || (!info->shared && !htab->srelbss))
   1063     abort ();
   1064 
   1065   if (!info->no_ld_generated_unwind_info
   1066       && htab->plt_eh_frame == NULL
   1067       && htab->elf.splt != NULL)
   1068     {
   1069       flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   1070 			| SEC_HAS_CONTENTS | SEC_IN_MEMORY
   1071 			| SEC_LINKER_CREATED);
   1072       htab->plt_eh_frame
   1073 	= bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
   1074       if (htab->plt_eh_frame == NULL
   1075 	  || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 3))
   1076 	return FALSE;
   1077     }
   1078   return TRUE;
   1079 }
   1080 
   1081 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   1082 
   1083 static void
   1084 elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
   1085 				 struct elf_link_hash_entry *dir,
   1086 				 struct elf_link_hash_entry *ind)
   1087 {
   1088   struct elf_x86_64_link_hash_entry *edir, *eind;
   1089 
   1090   edir = (struct elf_x86_64_link_hash_entry *) dir;
   1091   eind = (struct elf_x86_64_link_hash_entry *) ind;
   1092 
   1093   if (!edir->has_bnd_reloc)
   1094     edir->has_bnd_reloc = eind->has_bnd_reloc;
   1095 
   1096   if (eind->dyn_relocs != NULL)
   1097     {
   1098       if (edir->dyn_relocs != NULL)
   1099 	{
   1100 	  struct elf_dyn_relocs **pp;
   1101 	  struct elf_dyn_relocs *p;
   1102 
   1103 	  /* Add reloc counts against the indirect sym to the direct sym
   1104 	     list.  Merge any entries against the same section.  */
   1105 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   1106 	    {
   1107 	      struct elf_dyn_relocs *q;
   1108 
   1109 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   1110 		if (q->sec == p->sec)
   1111 		  {
   1112 		    q->pc_count += p->pc_count;
   1113 		    q->count += p->count;
   1114 		    *pp = p->next;
   1115 		    break;
   1116 		  }
   1117 	      if (q == NULL)
   1118 		pp = &p->next;
   1119 	    }
   1120 	  *pp = edir->dyn_relocs;
   1121 	}
   1122 
   1123       edir->dyn_relocs = eind->dyn_relocs;
   1124       eind->dyn_relocs = NULL;
   1125     }
   1126 
   1127   if (ind->root.type == bfd_link_hash_indirect
   1128       && dir->got.refcount <= 0)
   1129     {
   1130       edir->tls_type = eind->tls_type;
   1131       eind->tls_type = GOT_UNKNOWN;
   1132     }
   1133 
   1134   if (ELIMINATE_COPY_RELOCS
   1135       && ind->root.type != bfd_link_hash_indirect
   1136       && dir->dynamic_adjusted)
   1137     {
   1138       /* If called to transfer flags for a weakdef during processing
   1139 	 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
   1140 	 We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   1141       dir->ref_dynamic |= ind->ref_dynamic;
   1142       dir->ref_regular |= ind->ref_regular;
   1143       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
   1144       dir->needs_plt |= ind->needs_plt;
   1145       dir->pointer_equality_needed |= ind->pointer_equality_needed;
   1146     }
   1147   else
   1148     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   1149 }
   1150 
   1151 static bfd_boolean
   1152 elf64_x86_64_elf_object_p (bfd *abfd)
   1153 {
   1154   /* Set the right machine number for an x86-64 elf64 file.  */
   1155   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
   1156   return TRUE;
   1157 }
   1158 
   1159 static bfd_boolean
   1160 elf32_x86_64_elf_object_p (bfd *abfd)
   1161 {
   1162   /* Set the right machine number for an x86-64 elf32 file.  */
   1163   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
   1164   return TRUE;
   1165 }
   1166 
   1167 /* Return TRUE if the TLS access code sequence support transition
   1168    from R_TYPE.  */
   1169 
   1170 static bfd_boolean
   1171 elf_x86_64_check_tls_transition (bfd *abfd,
   1172 				 struct bfd_link_info *info,
   1173 				 asection *sec,
   1174 				 bfd_byte *contents,
   1175 				 Elf_Internal_Shdr *symtab_hdr,
   1176 				 struct elf_link_hash_entry **sym_hashes,
   1177 				 unsigned int r_type,
   1178 				 const Elf_Internal_Rela *rel,
   1179 				 const Elf_Internal_Rela *relend)
   1180 {
   1181   unsigned int val;
   1182   unsigned long r_symndx;
   1183   bfd_boolean largepic = FALSE;
   1184   struct elf_link_hash_entry *h;
   1185   bfd_vma offset;
   1186   struct elf_x86_64_link_hash_table *htab;
   1187 
   1188   /* Get the section contents.  */
   1189   if (contents == NULL)
   1190     {
   1191       if (elf_section_data (sec)->this_hdr.contents != NULL)
   1192 	contents = elf_section_data (sec)->this_hdr.contents;
   1193       else
   1194 	{
   1195 	  /* FIXME: How to better handle error condition?  */
   1196 	  if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   1197 	    return FALSE;
   1198 
   1199 	  /* Cache the section contents for elf_link_input_bfd.  */
   1200 	  elf_section_data (sec)->this_hdr.contents = contents;
   1201 	}
   1202     }
   1203 
   1204   htab = elf_x86_64_hash_table (info);
   1205   offset = rel->r_offset;
   1206   switch (r_type)
   1207     {
   1208     case R_X86_64_TLSGD:
   1209     case R_X86_64_TLSLD:
   1210       if ((rel + 1) >= relend)
   1211 	return FALSE;
   1212 
   1213       if (r_type == R_X86_64_TLSGD)
   1214 	{
   1215 	  /* Check transition from GD access model.  For 64bit, only
   1216 		.byte 0x66; leaq foo@tlsgd(%rip), %rdi
   1217 		.word 0x6666; rex64; call __tls_get_addr
   1218 	     can transit to different access model.  For 32bit, only
   1219 		leaq foo@tlsgd(%rip), %rdi
   1220 		.word 0x6666; rex64; call __tls_get_addr
   1221 	     can transit to different access model.  For largepic
   1222 	     we also support:
   1223 	        leaq foo@tlsgd(%rip), %rdi
   1224 	        movabsq $__tls_get_addr@pltoff, %rax
   1225 	        addq $rbx, %rax
   1226 	        call *%rax.  */
   1227 
   1228 	  static const unsigned char call[] = { 0x66, 0x66, 0x48, 0xe8 };
   1229 	  static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
   1230 
   1231 	  if ((offset + 12) > sec->size)
   1232 	    return FALSE;
   1233 
   1234 	  if (memcmp (contents + offset + 4, call, 4) != 0)
   1235 	    {
   1236 	      if (!ABI_64_P (abfd)
   1237 		  || (offset + 19) > sec->size
   1238 		  || offset < 3
   1239 		  || memcmp (contents + offset - 3, leaq + 1, 3) != 0
   1240 		  || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
   1241 		  || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
   1242 		     != 0)
   1243 		return FALSE;
   1244 	      largepic = TRUE;
   1245 	    }
   1246 	  else if (ABI_64_P (abfd))
   1247 	    {
   1248 	      if (offset < 4
   1249 		  || memcmp (contents + offset - 4, leaq, 4) != 0)
   1250 		return FALSE;
   1251 	    }
   1252 	  else
   1253 	    {
   1254 	      if (offset < 3
   1255 		  || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
   1256 		return FALSE;
   1257 	    }
   1258 	}
   1259       else
   1260 	{
   1261 	  /* Check transition from LD access model.  Only
   1262 		leaq foo@tlsld(%rip), %rdi;
   1263 		call __tls_get_addr
   1264 	     can transit to different access model.  For largepic
   1265 	     we also support:
   1266 	        leaq foo@tlsld(%rip), %rdi
   1267 	        movabsq $__tls_get_addr@pltoff, %rax
   1268 	        addq $rbx, %rax
   1269 	        call *%rax.  */
   1270 
   1271 	  static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
   1272 
   1273 	  if (offset < 3 || (offset + 9) > sec->size)
   1274 	    return FALSE;
   1275 
   1276 	  if (memcmp (contents + offset - 3, lea, 3) != 0)
   1277 	    return FALSE;
   1278 
   1279 	  if (0xe8 != *(contents + offset + 4))
   1280 	    {
   1281 	      if (!ABI_64_P (abfd)
   1282 		  || (offset + 19) > sec->size
   1283 		  || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
   1284 		  || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
   1285 		     != 0)
   1286 		return FALSE;
   1287 	      largepic = TRUE;
   1288 	    }
   1289 	}
   1290 
   1291       r_symndx = htab->r_sym (rel[1].r_info);
   1292       if (r_symndx < symtab_hdr->sh_info)
   1293 	return FALSE;
   1294 
   1295       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1296       /* Use strncmp to check __tls_get_addr since __tls_get_addr
   1297 	 may be versioned.  */
   1298       return (h != NULL
   1299 	      && h->root.root.string != NULL
   1300 	      && (largepic
   1301 		  ? ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64
   1302 		  : (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
   1303 		     || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32))
   1304 	      && (strncmp (h->root.root.string,
   1305 			   "__tls_get_addr", 14) == 0));
   1306 
   1307     case R_X86_64_GOTTPOFF:
   1308       /* Check transition from IE access model:
   1309 		mov foo@gottpoff(%rip), %reg
   1310 		add foo@gottpoff(%rip), %reg
   1311        */
   1312 
   1313       /* Check REX prefix first.  */
   1314       if (offset >= 3 && (offset + 4) <= sec->size)
   1315 	{
   1316 	  val = bfd_get_8 (abfd, contents + offset - 3);
   1317 	  if (val != 0x48 && val != 0x4c)
   1318 	    {
   1319 	      /* X32 may have 0x44 REX prefix or no REX prefix.  */
   1320 	      if (ABI_64_P (abfd))
   1321 		return FALSE;
   1322 	    }
   1323 	}
   1324       else
   1325 	{
   1326 	  /* X32 may not have any REX prefix.  */
   1327 	  if (ABI_64_P (abfd))
   1328 	    return FALSE;
   1329 	  if (offset < 2 || (offset + 3) > sec->size)
   1330 	    return FALSE;
   1331 	}
   1332 
   1333       val = bfd_get_8 (abfd, contents + offset - 2);
   1334       if (val != 0x8b && val != 0x03)
   1335 	return FALSE;
   1336 
   1337       val = bfd_get_8 (abfd, contents + offset - 1);
   1338       return (val & 0xc7) == 5;
   1339 
   1340     case R_X86_64_GOTPC32_TLSDESC:
   1341       /* Check transition from GDesc access model:
   1342 		leaq x@tlsdesc(%rip), %rax
   1343 
   1344 	 Make sure it's a leaq adding rip to a 32-bit offset
   1345 	 into any register, although it's probably almost always
   1346 	 going to be rax.  */
   1347 
   1348       if (offset < 3 || (offset + 4) > sec->size)
   1349 	return FALSE;
   1350 
   1351       val = bfd_get_8 (abfd, contents + offset - 3);
   1352       if ((val & 0xfb) != 0x48)
   1353 	return FALSE;
   1354 
   1355       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
   1356 	return FALSE;
   1357 
   1358       val = bfd_get_8 (abfd, contents + offset - 1);
   1359       return (val & 0xc7) == 0x05;
   1360 
   1361     case R_X86_64_TLSDESC_CALL:
   1362       /* Check transition from GDesc access model:
   1363 		call *x@tlsdesc(%rax)
   1364        */
   1365       if (offset + 2 <= sec->size)
   1366 	{
   1367 	  /* Make sure that it's a call *x@tlsdesc(%rax).  */
   1368 	  static const unsigned char call[] = { 0xff, 0x10 };
   1369 	  return memcmp (contents + offset, call, 2) == 0;
   1370 	}
   1371 
   1372       return FALSE;
   1373 
   1374     default:
   1375       abort ();
   1376     }
   1377 }
   1378 
   1379 /* Return TRUE if the TLS access transition is OK or no transition
   1380    will be performed.  Update R_TYPE if there is a transition.  */
   1381 
   1382 static bfd_boolean
   1383 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
   1384 			   asection *sec, bfd_byte *contents,
   1385 			   Elf_Internal_Shdr *symtab_hdr,
   1386 			   struct elf_link_hash_entry **sym_hashes,
   1387 			   unsigned int *r_type, int tls_type,
   1388 			   const Elf_Internal_Rela *rel,
   1389 			   const Elf_Internal_Rela *relend,
   1390 			   struct elf_link_hash_entry *h,
   1391 			   unsigned long r_symndx)
   1392 {
   1393   unsigned int from_type = *r_type;
   1394   unsigned int to_type = from_type;
   1395   bfd_boolean check = TRUE;
   1396 
   1397   /* Skip TLS transition for functions.  */
   1398   if (h != NULL
   1399       && (h->type == STT_FUNC
   1400 	  || h->type == STT_GNU_IFUNC))
   1401     return TRUE;
   1402 
   1403   switch (from_type)
   1404     {
   1405     case R_X86_64_TLSGD:
   1406     case R_X86_64_GOTPC32_TLSDESC:
   1407     case R_X86_64_TLSDESC_CALL:
   1408     case R_X86_64_GOTTPOFF:
   1409       if (info->executable)
   1410 	{
   1411 	  if (h == NULL)
   1412 	    to_type = R_X86_64_TPOFF32;
   1413 	  else
   1414 	    to_type = R_X86_64_GOTTPOFF;
   1415 	}
   1416 
   1417       /* When we are called from elf_x86_64_relocate_section,
   1418 	 CONTENTS isn't NULL and there may be additional transitions
   1419 	 based on TLS_TYPE.  */
   1420       if (contents != NULL)
   1421 	{
   1422 	  unsigned int new_to_type = to_type;
   1423 
   1424 	  if (info->executable
   1425 	      && h != NULL
   1426 	      && h->dynindx == -1
   1427 	      && tls_type == GOT_TLS_IE)
   1428 	    new_to_type = R_X86_64_TPOFF32;
   1429 
   1430 	  if (to_type == R_X86_64_TLSGD
   1431 	      || to_type == R_X86_64_GOTPC32_TLSDESC
   1432 	      || to_type == R_X86_64_TLSDESC_CALL)
   1433 	    {
   1434 	      if (tls_type == GOT_TLS_IE)
   1435 		new_to_type = R_X86_64_GOTTPOFF;
   1436 	    }
   1437 
   1438 	  /* We checked the transition before when we were called from
   1439 	     elf_x86_64_check_relocs.  We only want to check the new
   1440 	     transition which hasn't been checked before.  */
   1441 	  check = new_to_type != to_type && from_type == to_type;
   1442 	  to_type = new_to_type;
   1443 	}
   1444 
   1445       break;
   1446 
   1447     case R_X86_64_TLSLD:
   1448       if (info->executable)
   1449 	to_type = R_X86_64_TPOFF32;
   1450       break;
   1451 
   1452     default:
   1453       return TRUE;
   1454     }
   1455 
   1456   /* Return TRUE if there is no transition.  */
   1457   if (from_type == to_type)
   1458     return TRUE;
   1459 
   1460   /* Check if the transition can be performed.  */
   1461   if (check
   1462       && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
   1463 					    symtab_hdr, sym_hashes,
   1464 					    from_type, rel, relend))
   1465     {
   1466       reloc_howto_type *from, *to;
   1467       const char *name;
   1468 
   1469       from = elf_x86_64_rtype_to_howto (abfd, from_type);
   1470       to = elf_x86_64_rtype_to_howto (abfd, to_type);
   1471 
   1472       if (h)
   1473 	name = h->root.root.string;
   1474       else
   1475 	{
   1476 	  struct elf_x86_64_link_hash_table *htab;
   1477 
   1478 	  htab = elf_x86_64_hash_table (info);
   1479 	  if (htab == NULL)
   1480 	    name = "*unknown*";
   1481 	  else
   1482 	    {
   1483 	      Elf_Internal_Sym *isym;
   1484 
   1485 	      isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   1486 					    abfd, r_symndx);
   1487 	      name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
   1488 	    }
   1489 	}
   1490 
   1491       (*_bfd_error_handler)
   1492 	(_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
   1493 	   "in section `%A' failed"),
   1494 	 abfd, sec, from->name, to->name, name,
   1495 	 (unsigned long) rel->r_offset);
   1496       bfd_set_error (bfd_error_bad_value);
   1497       return FALSE;
   1498     }
   1499 
   1500   *r_type = to_type;
   1501   return TRUE;
   1502 }
   1503 
   1504 /* Look through the relocs for a section during the first phase, and
   1505    calculate needed space in the global offset table, procedure
   1506    linkage table, and dynamic reloc sections.  */
   1507 
   1508 static bfd_boolean
   1509 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
   1510 			 asection *sec,
   1511 			 const Elf_Internal_Rela *relocs)
   1512 {
   1513   struct elf_x86_64_link_hash_table *htab;
   1514   Elf_Internal_Shdr *symtab_hdr;
   1515   struct elf_link_hash_entry **sym_hashes;
   1516   const Elf_Internal_Rela *rel;
   1517   const Elf_Internal_Rela *rel_end;
   1518   asection *sreloc;
   1519 
   1520   if (info->relocatable)
   1521     return TRUE;
   1522 
   1523   BFD_ASSERT (is_x86_64_elf (abfd));
   1524 
   1525   htab = elf_x86_64_hash_table (info);
   1526   if (htab == NULL)
   1527     return FALSE;
   1528 
   1529   symtab_hdr = &elf_symtab_hdr (abfd);
   1530   sym_hashes = elf_sym_hashes (abfd);
   1531 
   1532   sreloc = NULL;
   1533 
   1534   rel_end = relocs + sec->reloc_count;
   1535   for (rel = relocs; rel < rel_end; rel++)
   1536     {
   1537       unsigned int r_type;
   1538       unsigned long r_symndx;
   1539       struct elf_link_hash_entry *h;
   1540       Elf_Internal_Sym *isym;
   1541       const char *name;
   1542       bfd_boolean size_reloc;
   1543 
   1544       r_symndx = htab->r_sym (rel->r_info);
   1545       r_type = ELF32_R_TYPE (rel->r_info);
   1546 
   1547       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
   1548 	{
   1549 	  (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
   1550 				 abfd, r_symndx);
   1551 	  return FALSE;
   1552 	}
   1553 
   1554       if (r_symndx < symtab_hdr->sh_info)
   1555 	{
   1556 	  /* A local symbol.  */
   1557 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   1558 					abfd, r_symndx);
   1559 	  if (isym == NULL)
   1560 	    return FALSE;
   1561 
   1562 	  /* Check relocation against local STT_GNU_IFUNC symbol.  */
   1563 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   1564 	    {
   1565 	      h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
   1566 						 TRUE);
   1567 	      if (h == NULL)
   1568 		return FALSE;
   1569 
   1570 	      /* Fake a STT_GNU_IFUNC symbol.  */
   1571 	      h->type = STT_GNU_IFUNC;
   1572 	      h->def_regular = 1;
   1573 	      h->ref_regular = 1;
   1574 	      h->forced_local = 1;
   1575 	      h->root.type = bfd_link_hash_defined;
   1576 	    }
   1577 	  else
   1578 	    h = NULL;
   1579 	}
   1580       else
   1581 	{
   1582 	  isym = NULL;
   1583 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1584 	  while (h->root.type == bfd_link_hash_indirect
   1585 		 || h->root.type == bfd_link_hash_warning)
   1586 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1587 	}
   1588 
   1589       /* Check invalid x32 relocations.  */
   1590       if (!ABI_64_P (abfd))
   1591 	switch (r_type)
   1592 	  {
   1593 	  default:
   1594 	    break;
   1595 
   1596 	  case R_X86_64_DTPOFF64:
   1597 	  case R_X86_64_TPOFF64:
   1598 	  case R_X86_64_PC64:
   1599 	  case R_X86_64_GOTOFF64:
   1600 	  case R_X86_64_GOT64:
   1601 	  case R_X86_64_GOTPCREL64:
   1602 	  case R_X86_64_GOTPC64:
   1603 	  case R_X86_64_GOTPLT64:
   1604 	  case R_X86_64_PLTOFF64:
   1605 	      {
   1606 		if (h)
   1607 		  name = h->root.root.string;
   1608 		else
   1609 		  name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
   1610 					   NULL);
   1611 		(*_bfd_error_handler)
   1612 		  (_("%B: relocation %s against symbol `%s' isn't "
   1613 		     "supported in x32 mode"), abfd,
   1614 		   x86_64_elf_howto_table[r_type].name, name);
   1615 		bfd_set_error (bfd_error_bad_value);
   1616 		return FALSE;
   1617 	      }
   1618 	    break;
   1619 	  }
   1620 
   1621       if (h != NULL)
   1622 	{
   1623 	  /* Create the ifunc sections for static executables.  If we
   1624 	     never see an indirect function symbol nor we are building
   1625 	     a static executable, those sections will be empty and
   1626 	     won't appear in output.  */
   1627 	  switch (r_type)
   1628 	    {
   1629 	    default:
   1630 	      break;
   1631 
   1632 	    case R_X86_64_PC32_BND:
   1633 	    case R_X86_64_PLT32_BND:
   1634 	      /* MPX PLT is supported only if elf_x86_64_arch_bed
   1635 		 is used in 64-bit mode.  */
   1636 	      if (ABI_64_P (abfd)
   1637 		  && (get_elf_x86_64_backend_data (abfd)
   1638 		      == &elf_x86_64_arch_bed))
   1639 		{
   1640 		  elf_x86_64_hash_entry (h)->has_bnd_reloc = TRUE;
   1641 
   1642 		  /* Create the second PLT for Intel MPX support.  */
   1643 		  if (htab->plt_bnd == NULL)
   1644 		    {
   1645 		      unsigned int plt_bnd_align;
   1646 		      const struct elf_backend_data *bed;
   1647 
   1648 		      bed = get_elf_backend_data (info->output_bfd);
   1649 		      switch (sizeof (elf_x86_64_bnd_plt2_entry))
   1650 			{
   1651 			case 8:
   1652 			  plt_bnd_align = 3;
   1653 			  break;
   1654 			case 16:
   1655 			  plt_bnd_align = 4;
   1656 			  break;
   1657 			default:
   1658 			  abort ();
   1659 			}
   1660 
   1661 		      if (htab->elf.dynobj == NULL)
   1662 			htab->elf.dynobj = abfd;
   1663 		      htab->plt_bnd
   1664 			= bfd_make_section_anyway_with_flags (htab->elf.dynobj,
   1665 							      ".plt.bnd",
   1666 							     (bed->dynamic_sec_flags
   1667 							      | SEC_ALLOC
   1668 							      | SEC_CODE
   1669 							      | SEC_LOAD
   1670 							      | SEC_READONLY));
   1671 		      if (htab->plt_bnd == NULL
   1672 			  || !bfd_set_section_alignment (htab->elf.dynobj,
   1673 							 htab->plt_bnd,
   1674 							 plt_bnd_align))
   1675 			return FALSE;
   1676 		    }
   1677 		}
   1678 
   1679 	    case R_X86_64_32S:
   1680 	    case R_X86_64_32:
   1681 	    case R_X86_64_64:
   1682 	    case R_X86_64_PC32:
   1683 	    case R_X86_64_PC64:
   1684 	    case R_X86_64_PLT32:
   1685 	    case R_X86_64_GOTPCREL:
   1686 	    case R_X86_64_GOTPCREL64:
   1687 	      if (htab->elf.dynobj == NULL)
   1688 		htab->elf.dynobj = abfd;
   1689 	      if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
   1690 		return FALSE;
   1691 	      break;
   1692 	    }
   1693 
   1694 	  /* It is referenced by a non-shared object. */
   1695 	  h->ref_regular = 1;
   1696 	  h->root.non_ir_ref = 1;
   1697 	}
   1698 
   1699       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
   1700 				       symtab_hdr, sym_hashes,
   1701 				       &r_type, GOT_UNKNOWN,
   1702 				       rel, rel_end, h, r_symndx))
   1703 	return FALSE;
   1704 
   1705       switch (r_type)
   1706 	{
   1707 	case R_X86_64_TLSLD:
   1708 	  htab->tls_ld_got.refcount += 1;
   1709 	  goto create_got;
   1710 
   1711 	case R_X86_64_TPOFF32:
   1712 	  if (!info->executable && ABI_64_P (abfd))
   1713 	    {
   1714 	      if (h)
   1715 		name = h->root.root.string;
   1716 	      else
   1717 		name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
   1718 					 NULL);
   1719 	      (*_bfd_error_handler)
   1720 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   1721 		 abfd,
   1722 		 x86_64_elf_howto_table[r_type].name, name);
   1723 	      bfd_set_error (bfd_error_bad_value);
   1724 	      return FALSE;
   1725 	    }
   1726 	  break;
   1727 
   1728 	case R_X86_64_GOTTPOFF:
   1729 	  if (!info->executable)
   1730 	    info->flags |= DF_STATIC_TLS;
   1731 	  /* Fall through */
   1732 
   1733 	case R_X86_64_GOT32:
   1734 	case R_X86_64_GOTPCREL:
   1735 	case R_X86_64_TLSGD:
   1736 	case R_X86_64_GOT64:
   1737 	case R_X86_64_GOTPCREL64:
   1738 	case R_X86_64_GOTPLT64:
   1739 	case R_X86_64_GOTPC32_TLSDESC:
   1740 	case R_X86_64_TLSDESC_CALL:
   1741 	  /* This symbol requires a global offset table entry.	*/
   1742 	  {
   1743 	    int tls_type, old_tls_type;
   1744 
   1745 	    switch (r_type)
   1746 	      {
   1747 	      default: tls_type = GOT_NORMAL; break;
   1748 	      case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
   1749 	      case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
   1750 	      case R_X86_64_GOTPC32_TLSDESC:
   1751 	      case R_X86_64_TLSDESC_CALL:
   1752 		tls_type = GOT_TLS_GDESC; break;
   1753 	      }
   1754 
   1755 	    if (h != NULL)
   1756 	      {
   1757 		h->got.refcount += 1;
   1758 		old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
   1759 	      }
   1760 	    else
   1761 	      {
   1762 		bfd_signed_vma *local_got_refcounts;
   1763 
   1764 		/* This is a global offset table entry for a local symbol.  */
   1765 		local_got_refcounts = elf_local_got_refcounts (abfd);
   1766 		if (local_got_refcounts == NULL)
   1767 		  {
   1768 		    bfd_size_type size;
   1769 
   1770 		    size = symtab_hdr->sh_info;
   1771 		    size *= sizeof (bfd_signed_vma)
   1772 		      + sizeof (bfd_vma) + sizeof (char);
   1773 		    local_got_refcounts = ((bfd_signed_vma *)
   1774 					   bfd_zalloc (abfd, size));
   1775 		    if (local_got_refcounts == NULL)
   1776 		      return FALSE;
   1777 		    elf_local_got_refcounts (abfd) = local_got_refcounts;
   1778 		    elf_x86_64_local_tlsdesc_gotent (abfd)
   1779 		      = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
   1780 		    elf_x86_64_local_got_tls_type (abfd)
   1781 		      = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
   1782 		  }
   1783 		local_got_refcounts[r_symndx] += 1;
   1784 		old_tls_type
   1785 		  = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
   1786 	      }
   1787 
   1788 	    /* If a TLS symbol is accessed using IE at least once,
   1789 	       there is no point to use dynamic model for it.  */
   1790 	    if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
   1791 		&& (! GOT_TLS_GD_ANY_P (old_tls_type)
   1792 		    || tls_type != GOT_TLS_IE))
   1793 	      {
   1794 		if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
   1795 		  tls_type = old_tls_type;
   1796 		else if (GOT_TLS_GD_ANY_P (old_tls_type)
   1797 			 && GOT_TLS_GD_ANY_P (tls_type))
   1798 		  tls_type |= old_tls_type;
   1799 		else
   1800 		  {
   1801 		    if (h)
   1802 		      name = h->root.root.string;
   1803 		    else
   1804 		      name = bfd_elf_sym_name (abfd, symtab_hdr,
   1805 					       isym, NULL);
   1806 		    (*_bfd_error_handler)
   1807 		      (_("%B: '%s' accessed both as normal and thread local symbol"),
   1808 		       abfd, name);
   1809 		    bfd_set_error (bfd_error_bad_value);
   1810 		    return FALSE;
   1811 		  }
   1812 	      }
   1813 
   1814 	    if (old_tls_type != tls_type)
   1815 	      {
   1816 		if (h != NULL)
   1817 		  elf_x86_64_hash_entry (h)->tls_type = tls_type;
   1818 		else
   1819 		  elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
   1820 	      }
   1821 	  }
   1822 	  /* Fall through */
   1823 
   1824 	case R_X86_64_GOTOFF64:
   1825 	case R_X86_64_GOTPC32:
   1826 	case R_X86_64_GOTPC64:
   1827 	create_got:
   1828 	  if (htab->elf.sgot == NULL)
   1829 	    {
   1830 	      if (htab->elf.dynobj == NULL)
   1831 		htab->elf.dynobj = abfd;
   1832 	      if (!_bfd_elf_create_got_section (htab->elf.dynobj,
   1833 						info))
   1834 		return FALSE;
   1835 	    }
   1836 	  break;
   1837 
   1838 	case R_X86_64_PLT32:
   1839 	case R_X86_64_PLT32_BND:
   1840 	  /* This symbol requires a procedure linkage table entry.  We
   1841 	     actually build the entry in adjust_dynamic_symbol,
   1842 	     because this might be a case of linking PIC code which is
   1843 	     never referenced by a dynamic object, in which case we
   1844 	     don't need to generate a procedure linkage table entry
   1845 	     after all.	 */
   1846 
   1847 	  /* If this is a local symbol, we resolve it directly without
   1848 	     creating a procedure linkage table entry.	*/
   1849 	  if (h == NULL)
   1850 	    continue;
   1851 
   1852 	  h->needs_plt = 1;
   1853 	  h->plt.refcount += 1;
   1854 	  break;
   1855 
   1856 	case R_X86_64_PLTOFF64:
   1857 	  /* This tries to form the 'address' of a function relative
   1858 	     to GOT.  For global symbols we need a PLT entry.  */
   1859 	  if (h != NULL)
   1860 	    {
   1861 	      h->needs_plt = 1;
   1862 	      h->plt.refcount += 1;
   1863 	    }
   1864 	  goto create_got;
   1865 
   1866 	case R_X86_64_SIZE32:
   1867 	case R_X86_64_SIZE64:
   1868 	  size_reloc = TRUE;
   1869 	  goto do_size;
   1870 
   1871 	case R_X86_64_32:
   1872 	  if (!ABI_64_P (abfd))
   1873 	    goto pointer;
   1874 	case R_X86_64_8:
   1875 	case R_X86_64_16:
   1876 	case R_X86_64_32S:
   1877 	  /* Let's help debug shared library creation.  These relocs
   1878 	     cannot be used in shared libs.  Don't error out for
   1879 	     sections we don't care about, such as debug sections or
   1880 	     non-constant sections.  */
   1881 	  if (info->shared
   1882 	      && (sec->flags & SEC_ALLOC) != 0
   1883 	      && (sec->flags & SEC_READONLY) != 0)
   1884 	    {
   1885 	      if (h)
   1886 		name = h->root.root.string;
   1887 	      else
   1888 		name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
   1889 	      (*_bfd_error_handler)
   1890 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   1891 		 abfd, x86_64_elf_howto_table[r_type].name, name);
   1892 	      bfd_set_error (bfd_error_bad_value);
   1893 	      return FALSE;
   1894 	    }
   1895 	  /* Fall through.  */
   1896 
   1897 	case R_X86_64_PC8:
   1898 	case R_X86_64_PC16:
   1899 	case R_X86_64_PC32:
   1900 	case R_X86_64_PC32_BND:
   1901 	case R_X86_64_PC64:
   1902 	case R_X86_64_64:
   1903 pointer:
   1904 	  if (h != NULL && info->executable)
   1905 	    {
   1906 	      /* If this reloc is in a read-only section, we might
   1907 		 need a copy reloc.  We can't check reliably at this
   1908 		 stage whether the section is read-only, as input
   1909 		 sections have not yet been mapped to output sections.
   1910 		 Tentatively set the flag for now, and correct in
   1911 		 adjust_dynamic_symbol.  */
   1912 	      h->non_got_ref = 1;
   1913 
   1914 	      /* We may need a .plt entry if the function this reloc
   1915 		 refers to is in a shared lib.  */
   1916 	      h->plt.refcount += 1;
   1917 	      if (r_type != R_X86_64_PC32
   1918 		  && r_type != R_X86_64_PC32_BND
   1919 		  && r_type != R_X86_64_PC64)
   1920 		h->pointer_equality_needed = 1;
   1921 	    }
   1922 
   1923 	  size_reloc = FALSE;
   1924 do_size:
   1925 	  /* If we are creating a shared library, and this is a reloc
   1926 	     against a global symbol, or a non PC relative reloc
   1927 	     against a local symbol, then we need to copy the reloc
   1928 	     into the shared library.  However, if we are linking with
   1929 	     -Bsymbolic, we do not need to copy a reloc against a
   1930 	     global symbol which is defined in an object we are
   1931 	     including in the link (i.e., DEF_REGULAR is set).	At
   1932 	     this point we have not seen all the input files, so it is
   1933 	     possible that DEF_REGULAR is not set now but will be set
   1934 	     later (it is never cleared).  In case of a weak definition,
   1935 	     DEF_REGULAR may be cleared later by a strong definition in
   1936 	     a shared library.  We account for that possibility below by
   1937 	     storing information in the relocs_copied field of the hash
   1938 	     table entry.  A similar situation occurs when creating
   1939 	     shared libraries and symbol visibility changes render the
   1940 	     symbol local.
   1941 
   1942 	     If on the other hand, we are creating an executable, we
   1943 	     may need to keep relocations for symbols satisfied by a
   1944 	     dynamic library if we manage to avoid copy relocs for the
   1945 	     symbol.  */
   1946 	  if ((info->shared
   1947 	       && (sec->flags & SEC_ALLOC) != 0
   1948 	       && (! IS_X86_64_PCREL_TYPE (r_type)
   1949 		   || (h != NULL
   1950 		       && (! SYMBOLIC_BIND (info, h)
   1951 			   || h->root.type == bfd_link_hash_defweak
   1952 			   || !h->def_regular))))
   1953 	      || (ELIMINATE_COPY_RELOCS
   1954 		  && !info->shared
   1955 		  && (sec->flags & SEC_ALLOC) != 0
   1956 		  && h != NULL
   1957 		  && (h->root.type == bfd_link_hash_defweak
   1958 		      || !h->def_regular)))
   1959 	    {
   1960 	      struct elf_dyn_relocs *p;
   1961 	      struct elf_dyn_relocs **head;
   1962 
   1963 	      /* We must copy these reloc types into the output file.
   1964 		 Create a reloc section in dynobj and make room for
   1965 		 this reloc.  */
   1966 	      if (sreloc == NULL)
   1967 		{
   1968 		  if (htab->elf.dynobj == NULL)
   1969 		    htab->elf.dynobj = abfd;
   1970 
   1971 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   1972 		    (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
   1973 		     abfd, /*rela?*/ TRUE);
   1974 
   1975 		  if (sreloc == NULL)
   1976 		    return FALSE;
   1977 		}
   1978 
   1979 	      /* If this is a global symbol, we count the number of
   1980 		 relocations we need for this symbol.  */
   1981 	      if (h != NULL)
   1982 		{
   1983 		  head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
   1984 		}
   1985 	      else
   1986 		{
   1987 		  /* Track dynamic relocs needed for local syms too.
   1988 		     We really need local syms available to do this
   1989 		     easily.  Oh well.  */
   1990 		  asection *s;
   1991 		  void **vpp;
   1992 
   1993 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   1994 						abfd, r_symndx);
   1995 		  if (isym == NULL)
   1996 		    return FALSE;
   1997 
   1998 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   1999 		  if (s == NULL)
   2000 		    s = sec;
   2001 
   2002 		  /* Beware of type punned pointers vs strict aliasing
   2003 		     rules.  */
   2004 		  vpp = &(elf_section_data (s)->local_dynrel);
   2005 		  head = (struct elf_dyn_relocs **)vpp;
   2006 		}
   2007 
   2008 	      p = *head;
   2009 	      if (p == NULL || p->sec != sec)
   2010 		{
   2011 		  bfd_size_type amt = sizeof *p;
   2012 
   2013 		  p = ((struct elf_dyn_relocs *)
   2014 		       bfd_alloc (htab->elf.dynobj, amt));
   2015 		  if (p == NULL)
   2016 		    return FALSE;
   2017 		  p->next = *head;
   2018 		  *head = p;
   2019 		  p->sec = sec;
   2020 		  p->count = 0;
   2021 		  p->pc_count = 0;
   2022 		}
   2023 
   2024 	      p->count += 1;
   2025 	      /* Count size relocation as PC-relative relocation.  */
   2026 	      if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
   2027 		p->pc_count += 1;
   2028 	    }
   2029 	  break;
   2030 
   2031 	  /* This relocation describes the C++ object vtable hierarchy.
   2032 	     Reconstruct it for later use during GC.  */
   2033 	case R_X86_64_GNU_VTINHERIT:
   2034 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   2035 	    return FALSE;
   2036 	  break;
   2037 
   2038 	  /* This relocation describes which C++ vtable entries are actually
   2039 	     used.  Record for later use during GC.  */
   2040 	case R_X86_64_GNU_VTENTRY:
   2041 	  BFD_ASSERT (h != NULL);
   2042 	  if (h != NULL
   2043 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   2044 	    return FALSE;
   2045 	  break;
   2046 
   2047 	default:
   2048 	  break;
   2049 	}
   2050     }
   2051 
   2052   return TRUE;
   2053 }
   2054 
   2055 /* Return the section that should be marked against GC for a given
   2056    relocation.	*/
   2057 
   2058 static asection *
   2059 elf_x86_64_gc_mark_hook (asection *sec,
   2060 			 struct bfd_link_info *info,
   2061 			 Elf_Internal_Rela *rel,
   2062 			 struct elf_link_hash_entry *h,
   2063 			 Elf_Internal_Sym *sym)
   2064 {
   2065   if (h != NULL)
   2066     switch (ELF32_R_TYPE (rel->r_info))
   2067       {
   2068       case R_X86_64_GNU_VTINHERIT:
   2069       case R_X86_64_GNU_VTENTRY:
   2070 	return NULL;
   2071       }
   2072 
   2073   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   2074 }
   2075 
   2076 /* Update the got entry reference counts for the section being removed.	 */
   2077 
   2078 static bfd_boolean
   2079 elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   2080 			  asection *sec,
   2081 			  const Elf_Internal_Rela *relocs)
   2082 {
   2083   struct elf_x86_64_link_hash_table *htab;
   2084   Elf_Internal_Shdr *symtab_hdr;
   2085   struct elf_link_hash_entry **sym_hashes;
   2086   bfd_signed_vma *local_got_refcounts;
   2087   const Elf_Internal_Rela *rel, *relend;
   2088 
   2089   if (info->relocatable)
   2090     return TRUE;
   2091 
   2092   htab = elf_x86_64_hash_table (info);
   2093   if (htab == NULL)
   2094     return FALSE;
   2095 
   2096   elf_section_data (sec)->local_dynrel = NULL;
   2097 
   2098   symtab_hdr = &elf_symtab_hdr (abfd);
   2099   sym_hashes = elf_sym_hashes (abfd);
   2100   local_got_refcounts = elf_local_got_refcounts (abfd);
   2101 
   2102   htab = elf_x86_64_hash_table (info);
   2103   relend = relocs + sec->reloc_count;
   2104   for (rel = relocs; rel < relend; rel++)
   2105     {
   2106       unsigned long r_symndx;
   2107       unsigned int r_type;
   2108       struct elf_link_hash_entry *h = NULL;
   2109 
   2110       r_symndx = htab->r_sym (rel->r_info);
   2111       if (r_symndx >= symtab_hdr->sh_info)
   2112 	{
   2113 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2114 	  while (h->root.type == bfd_link_hash_indirect
   2115 		 || h->root.type == bfd_link_hash_warning)
   2116 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2117 	}
   2118       else
   2119 	{
   2120 	  /* A local symbol.  */
   2121 	  Elf_Internal_Sym *isym;
   2122 
   2123 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   2124 					abfd, r_symndx);
   2125 
   2126 	  /* Check relocation against local STT_GNU_IFUNC symbol.  */
   2127 	  if (isym != NULL
   2128 	      && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   2129 	    {
   2130 	      h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
   2131 	      if (h == NULL)
   2132 		abort ();
   2133 	    }
   2134 	}
   2135 
   2136       if (h)
   2137 	{
   2138 	  struct elf_x86_64_link_hash_entry *eh;
   2139 	  struct elf_dyn_relocs **pp;
   2140 	  struct elf_dyn_relocs *p;
   2141 
   2142 	  eh = (struct elf_x86_64_link_hash_entry *) h;
   2143 
   2144 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   2145 	    if (p->sec == sec)
   2146 	      {
   2147 		/* Everything must go for SEC.  */
   2148 		*pp = p->next;
   2149 		break;
   2150 	      }
   2151 	}
   2152 
   2153       r_type = ELF32_R_TYPE (rel->r_info);
   2154       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
   2155 				       symtab_hdr, sym_hashes,
   2156 				       &r_type, GOT_UNKNOWN,
   2157 				       rel, relend, h, r_symndx))
   2158 	return FALSE;
   2159 
   2160       switch (r_type)
   2161 	{
   2162 	case R_X86_64_TLSLD:
   2163 	  if (htab->tls_ld_got.refcount > 0)
   2164 	    htab->tls_ld_got.refcount -= 1;
   2165 	  break;
   2166 
   2167 	case R_X86_64_TLSGD:
   2168 	case R_X86_64_GOTPC32_TLSDESC:
   2169 	case R_X86_64_TLSDESC_CALL:
   2170 	case R_X86_64_GOTTPOFF:
   2171 	case R_X86_64_GOT32:
   2172 	case R_X86_64_GOTPCREL:
   2173 	case R_X86_64_GOT64:
   2174 	case R_X86_64_GOTPCREL64:
   2175 	case R_X86_64_GOTPLT64:
   2176 	  if (h != NULL)
   2177 	    {
   2178 	      if (h->got.refcount > 0)
   2179 		h->got.refcount -= 1;
   2180 	      if (h->type == STT_GNU_IFUNC)
   2181 		{
   2182 		  if (h->plt.refcount > 0)
   2183 		    h->plt.refcount -= 1;
   2184 		}
   2185 	    }
   2186 	  else if (local_got_refcounts != NULL)
   2187 	    {
   2188 	      if (local_got_refcounts[r_symndx] > 0)
   2189 		local_got_refcounts[r_symndx] -= 1;
   2190 	    }
   2191 	  break;
   2192 
   2193 	case R_X86_64_8:
   2194 	case R_X86_64_16:
   2195 	case R_X86_64_32:
   2196 	case R_X86_64_64:
   2197 	case R_X86_64_32S:
   2198 	case R_X86_64_PC8:
   2199 	case R_X86_64_PC16:
   2200 	case R_X86_64_PC32:
   2201 	case R_X86_64_PC32_BND:
   2202 	case R_X86_64_PC64:
   2203 	case R_X86_64_SIZE32:
   2204 	case R_X86_64_SIZE64:
   2205 	  if (info->shared
   2206 	      && (h == NULL || h->type != STT_GNU_IFUNC))
   2207 	    break;
   2208 	  /* Fall thru */
   2209 
   2210 	case R_X86_64_PLT32:
   2211 	case R_X86_64_PLT32_BND:
   2212 	case R_X86_64_PLTOFF64:
   2213 	  if (h != NULL)
   2214 	    {
   2215 	      if (h->plt.refcount > 0)
   2216 		h->plt.refcount -= 1;
   2217 	    }
   2218 	  break;
   2219 
   2220 	default:
   2221 	  break;
   2222 	}
   2223     }
   2224 
   2225   return TRUE;
   2226 }
   2227 
   2228 /* Adjust a symbol defined by a dynamic object and referenced by a
   2229    regular object.  The current definition is in some section of the
   2230    dynamic object, but we're not including those sections.  We have to
   2231    change the definition to something the rest of the link can
   2232    understand.	*/
   2233 
   2234 static bfd_boolean
   2235 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
   2236 				  struct elf_link_hash_entry *h)
   2237 {
   2238   struct elf_x86_64_link_hash_table *htab;
   2239   asection *s;
   2240   struct elf_x86_64_link_hash_entry *eh;
   2241   struct elf_dyn_relocs *p;
   2242 
   2243   /* STT_GNU_IFUNC symbol must go through PLT. */
   2244   if (h->type == STT_GNU_IFUNC)
   2245     {
   2246       /* All local STT_GNU_IFUNC references must be treate as local
   2247 	 calls via local PLT.  */
   2248       if (h->ref_regular
   2249 	  && SYMBOL_CALLS_LOCAL (info, h))
   2250 	{
   2251 	  bfd_size_type pc_count = 0, count = 0;
   2252 	  struct elf_dyn_relocs **pp;
   2253 
   2254 	  eh = (struct elf_x86_64_link_hash_entry *) h;
   2255 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   2256 	    {
   2257 	      pc_count += p->pc_count;
   2258 	      p->count -= p->pc_count;
   2259 	      p->pc_count = 0;
   2260 	      count += p->count;
   2261 	      if (p->count == 0)
   2262 		*pp = p->next;
   2263 	      else
   2264 		pp = &p->next;
   2265 	    }
   2266 
   2267 	  if (pc_count || count)
   2268 	    {
   2269 	      h->needs_plt = 1;
   2270 	      h->non_got_ref = 1;
   2271 	      if (h->plt.refcount <= 0)
   2272 		h->plt.refcount = 1;
   2273 	      else
   2274 		h->plt.refcount += 1;
   2275 	    }
   2276 	}
   2277 
   2278       if (h->plt.refcount <= 0)
   2279 	{
   2280 	  h->plt.offset = (bfd_vma) -1;
   2281 	  h->needs_plt = 0;
   2282 	}
   2283       return TRUE;
   2284     }
   2285 
   2286   /* If this is a function, put it in the procedure linkage table.  We
   2287      will fill in the contents of the procedure linkage table later,
   2288      when we know the address of the .got section.  */
   2289   if (h->type == STT_FUNC
   2290       || h->needs_plt)
   2291     {
   2292       if (h->plt.refcount <= 0
   2293 	  || SYMBOL_CALLS_LOCAL (info, h)
   2294 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2295 	      && h->root.type == bfd_link_hash_undefweak))
   2296 	{
   2297 	  /* This case can occur if we saw a PLT32 reloc in an input
   2298 	     file, but the symbol was never referred to by a dynamic
   2299 	     object, or if all references were garbage collected.  In
   2300 	     such a case, we don't actually need to build a procedure
   2301 	     linkage table, and we can just do a PC32 reloc instead.  */
   2302 	  h->plt.offset = (bfd_vma) -1;
   2303 	  h->needs_plt = 0;
   2304 	}
   2305 
   2306       return TRUE;
   2307     }
   2308   else
   2309     /* It's possible that we incorrectly decided a .plt reloc was
   2310        needed for an R_X86_64_PC32 reloc to a non-function sym in
   2311        check_relocs.  We can't decide accurately between function and
   2312        non-function syms in check-relocs;  Objects loaded later in
   2313        the link may change h->type.  So fix it now.  */
   2314     h->plt.offset = (bfd_vma) -1;
   2315 
   2316   /* If this is a weak symbol, and there is a real definition, the
   2317      processor independent code will have arranged for us to see the
   2318      real definition first, and we can just use the same value.	 */
   2319   if (h->u.weakdef != NULL)
   2320     {
   2321       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   2322 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   2323       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   2324       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   2325       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
   2326 	h->non_got_ref = h->u.weakdef->non_got_ref;
   2327       return TRUE;
   2328     }
   2329 
   2330   /* This is a reference to a symbol defined by a dynamic object which
   2331      is not a function.	 */
   2332 
   2333   /* If we are creating a shared library, we must presume that the
   2334      only references to the symbol are via the global offset table.
   2335      For such cases we need not do anything here; the relocations will
   2336      be handled correctly by relocate_section.	*/
   2337   if (info->shared)
   2338     return TRUE;
   2339 
   2340   /* If there are no references to this symbol that do not use the
   2341      GOT, we don't need to generate a copy reloc.  */
   2342   if (!h->non_got_ref)
   2343     return TRUE;
   2344 
   2345   /* If -z nocopyreloc was given, we won't generate them either.  */
   2346   if (info->nocopyreloc)
   2347     {
   2348       h->non_got_ref = 0;
   2349       return TRUE;
   2350     }
   2351 
   2352   if (ELIMINATE_COPY_RELOCS)
   2353     {
   2354       eh = (struct elf_x86_64_link_hash_entry *) h;
   2355       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2356 	{
   2357 	  s = p->sec->output_section;
   2358 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
   2359 	    break;
   2360 	}
   2361 
   2362       /* If we didn't find any dynamic relocs in read-only sections, then
   2363 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   2364       if (p == NULL)
   2365 	{
   2366 	  h->non_got_ref = 0;
   2367 	  return TRUE;
   2368 	}
   2369     }
   2370 
   2371   /* We must allocate the symbol in our .dynbss section, which will
   2372      become part of the .bss section of the executable.	 There will be
   2373      an entry for this symbol in the .dynsym section.  The dynamic
   2374      object will contain position independent code, so all references
   2375      from the dynamic object to this symbol will go through the global
   2376      offset table.  The dynamic linker will use the .dynsym entry to
   2377      determine the address it must put in the global offset table, so
   2378      both the dynamic object and the regular object will refer to the
   2379      same memory location for the variable.  */
   2380 
   2381   htab = elf_x86_64_hash_table (info);
   2382   if (htab == NULL)
   2383     return FALSE;
   2384 
   2385   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
   2386      to copy the initial value out of the dynamic object and into the
   2387      runtime process image.  */
   2388   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   2389     {
   2390       const struct elf_backend_data *bed;
   2391       bed = get_elf_backend_data (info->output_bfd);
   2392       htab->srelbss->size += bed->s->sizeof_rela;
   2393       h->needs_copy = 1;
   2394     }
   2395 
   2396   s = htab->sdynbss;
   2397 
   2398   return _bfd_elf_adjust_dynamic_copy (h, s);
   2399 }
   2400 
   2401 /* Allocate space in .plt, .got and associated reloc sections for
   2402    dynamic relocs.  */
   2403 
   2404 static bfd_boolean
   2405 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
   2406 {
   2407   struct bfd_link_info *info;
   2408   struct elf_x86_64_link_hash_table *htab;
   2409   struct elf_x86_64_link_hash_entry *eh;
   2410   struct elf_dyn_relocs *p;
   2411   const struct elf_backend_data *bed;
   2412   unsigned int plt_entry_size;
   2413 
   2414   if (h->root.type == bfd_link_hash_indirect)
   2415     return TRUE;
   2416 
   2417   eh = (struct elf_x86_64_link_hash_entry *) h;
   2418 
   2419   info = (struct bfd_link_info *) inf;
   2420   htab = elf_x86_64_hash_table (info);
   2421   if (htab == NULL)
   2422     return FALSE;
   2423   bed = get_elf_backend_data (info->output_bfd);
   2424   plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
   2425 
   2426   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
   2427      here if it is defined and referenced in a non-shared object.  */
   2428   if (h->type == STT_GNU_IFUNC
   2429       && h->def_regular)
   2430     {
   2431       if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
   2432 					      &eh->dyn_relocs,
   2433 					      plt_entry_size,
   2434 					      plt_entry_size,
   2435 					      GOT_ENTRY_SIZE))
   2436 	{
   2437 	  asection *s = htab->plt_bnd;
   2438 	  if (h->plt.offset != (bfd_vma) -1 && s != NULL)
   2439 	    {
   2440 	      /* Use the .plt.bnd section if it is created.  */
   2441 	      eh->plt_bnd.offset = s->size;
   2442 
   2443 	      /* Make room for this entry in the .plt.bnd section.  */
   2444 	      s->size += sizeof (elf_x86_64_legacy_plt2_entry);
   2445 	    }
   2446 
   2447 	  return TRUE;
   2448 	}
   2449       else
   2450 	return FALSE;
   2451     }
   2452   else if (htab->elf.dynamic_sections_created
   2453 	   && h->plt.refcount > 0)
   2454     {
   2455       /* Make sure this symbol is output as a dynamic symbol.
   2456 	 Undefined weak syms won't yet be marked as dynamic.  */
   2457       if (h->dynindx == -1
   2458 	  && !h->forced_local)
   2459 	{
   2460 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2461 	    return FALSE;
   2462 	}
   2463 
   2464       if (info->shared
   2465 	  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
   2466 	{
   2467 	  asection *s = htab->elf.splt;
   2468 	  asection *bnd_s = htab->plt_bnd;
   2469 
   2470 	  /* If this is the first .plt entry, make room for the special
   2471 	     first entry.  */
   2472 	  if (s->size == 0)
   2473 	    s->size = plt_entry_size;
   2474 
   2475 	  h->plt.offset = s->size;
   2476 	  if (bnd_s)
   2477 	    eh->plt_bnd.offset = bnd_s->size;
   2478 
   2479 	  /* If this symbol is not defined in a regular file, and we are
   2480 	     not generating a shared library, then set the symbol to this
   2481 	     location in the .plt.  This is required to make function
   2482 	     pointers compare as equal between the normal executable and
   2483 	     the shared library.  */
   2484 	  if (! info->shared
   2485 	      && !h->def_regular)
   2486 	    {
   2487 	      if (bnd_s)
   2488 		{
   2489 		  /* We need to make a call to the entry of the second
   2490 		     PLT instead of regular PLT entry.  */
   2491 		  h->root.u.def.section = bnd_s;
   2492 		  h->root.u.def.value = eh->plt_bnd.offset;
   2493 		}
   2494 	      else
   2495 		{
   2496 		  h->root.u.def.section = s;
   2497 		  h->root.u.def.value = h->plt.offset;
   2498 		}
   2499 	    }
   2500 
   2501 	  /* Make room for this entry.  */
   2502 	  s->size += plt_entry_size;
   2503 	  if (bnd_s)
   2504 	    {
   2505 	      BFD_ASSERT (sizeof (elf_x86_64_bnd_plt2_entry)
   2506 			  == sizeof (elf_x86_64_legacy_plt2_entry));
   2507 	      bnd_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
   2508 	    }
   2509 
   2510 	  /* We also need to make an entry in the .got.plt section, which
   2511 	     will be placed in the .got section by the linker script.  */
   2512 	  htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
   2513 
   2514 	  /* We also need to make an entry in the .rela.plt section.  */
   2515 	  htab->elf.srelplt->size += bed->s->sizeof_rela;
   2516 	  htab->elf.srelplt->reloc_count++;
   2517 	}
   2518       else
   2519 	{
   2520 	  h->plt.offset = (bfd_vma) -1;
   2521 	  h->needs_plt = 0;
   2522 	}
   2523     }
   2524   else
   2525     {
   2526       h->plt.offset = (bfd_vma) -1;
   2527       h->needs_plt = 0;
   2528     }
   2529 
   2530   eh->tlsdesc_got = (bfd_vma) -1;
   2531 
   2532   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
   2533      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
   2534   if (h->got.refcount > 0
   2535       && info->executable
   2536       && h->dynindx == -1
   2537       && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
   2538     {
   2539       h->got.offset = (bfd_vma) -1;
   2540     }
   2541   else if (h->got.refcount > 0)
   2542     {
   2543       asection *s;
   2544       bfd_boolean dyn;
   2545       int tls_type = elf_x86_64_hash_entry (h)->tls_type;
   2546 
   2547       /* Make sure this symbol is output as a dynamic symbol.
   2548 	 Undefined weak syms won't yet be marked as dynamic.  */
   2549       if (h->dynindx == -1
   2550 	  && !h->forced_local)
   2551 	{
   2552 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2553 	    return FALSE;
   2554 	}
   2555 
   2556       if (GOT_TLS_GDESC_P (tls_type))
   2557 	{
   2558 	  eh->tlsdesc_got = htab->elf.sgotplt->size
   2559 	    - elf_x86_64_compute_jump_table_size (htab);
   2560 	  htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
   2561 	  h->got.offset = (bfd_vma) -2;
   2562 	}
   2563       if (! GOT_TLS_GDESC_P (tls_type)
   2564 	  || GOT_TLS_GD_P (tls_type))
   2565 	{
   2566 	  s = htab->elf.sgot;
   2567 	  h->got.offset = s->size;
   2568 	  s->size += GOT_ENTRY_SIZE;
   2569 	  if (GOT_TLS_GD_P (tls_type))
   2570 	    s->size += GOT_ENTRY_SIZE;
   2571 	}
   2572       dyn = htab->elf.dynamic_sections_created;
   2573       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
   2574 	 and two if global.
   2575 	 R_X86_64_GOTTPOFF needs one dynamic relocation.  */
   2576       if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
   2577 	  || tls_type == GOT_TLS_IE)
   2578 	htab->elf.srelgot->size += bed->s->sizeof_rela;
   2579       else if (GOT_TLS_GD_P (tls_type))
   2580 	htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
   2581       else if (! GOT_TLS_GDESC_P (tls_type)
   2582 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2583 		   || h->root.type != bfd_link_hash_undefweak)
   2584 	       && (info->shared
   2585 		   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
   2586 	htab->elf.srelgot->size += bed->s->sizeof_rela;
   2587       if (GOT_TLS_GDESC_P (tls_type))
   2588 	{
   2589 	  htab->elf.srelplt->size += bed->s->sizeof_rela;
   2590 	  htab->tlsdesc_plt = (bfd_vma) -1;
   2591 	}
   2592     }
   2593   else
   2594     h->got.offset = (bfd_vma) -1;
   2595 
   2596   if (eh->dyn_relocs == NULL)
   2597     return TRUE;
   2598 
   2599   /* In the shared -Bsymbolic case, discard space allocated for
   2600      dynamic pc-relative relocs against symbols which turn out to be
   2601      defined in regular objects.  For the normal shared case, discard
   2602      space for pc-relative relocs that have become local due to symbol
   2603      visibility changes.  */
   2604 
   2605   if (info->shared)
   2606     {
   2607       /* Relocs that use pc_count are those that appear on a call
   2608 	 insn, or certain REL relocs that can generated via assembly.
   2609 	 We want calls to protected symbols to resolve directly to the
   2610 	 function rather than going via the plt.  If people want
   2611 	 function pointer comparisons to work as expected then they
   2612 	 should avoid writing weird assembly.  */
   2613       if (SYMBOL_CALLS_LOCAL (info, h))
   2614 	{
   2615 	  struct elf_dyn_relocs **pp;
   2616 
   2617 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   2618 	    {
   2619 	      p->count -= p->pc_count;
   2620 	      p->pc_count = 0;
   2621 	      if (p->count == 0)
   2622 		*pp = p->next;
   2623 	      else
   2624 		pp = &p->next;
   2625 	    }
   2626 	}
   2627 
   2628       /* Also discard relocs on undefined weak syms with non-default
   2629 	 visibility.  */
   2630       if (eh->dyn_relocs != NULL
   2631 	  && h->root.type == bfd_link_hash_undefweak)
   2632 	{
   2633 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2634 	    eh->dyn_relocs = NULL;
   2635 
   2636 	  /* Make sure undefined weak symbols are output as a dynamic
   2637 	     symbol in PIEs.  */
   2638 	  else if (h->dynindx == -1
   2639 		   && ! h->forced_local
   2640 		   && ! bfd_elf_link_record_dynamic_symbol (info, h))
   2641 	    return FALSE;
   2642 	}
   2643 
   2644     }
   2645   else if (ELIMINATE_COPY_RELOCS)
   2646     {
   2647       /* For the non-shared case, discard space for relocs against
   2648 	 symbols which turn out to need copy relocs or are not
   2649 	 dynamic.  */
   2650 
   2651       if (!h->non_got_ref
   2652 	  && ((h->def_dynamic
   2653 	       && !h->def_regular)
   2654 	      || (htab->elf.dynamic_sections_created
   2655 		  && (h->root.type == bfd_link_hash_undefweak
   2656 		      || h->root.type == bfd_link_hash_undefined))))
   2657 	{
   2658 	  /* Make sure this symbol is output as a dynamic symbol.
   2659 	     Undefined weak syms won't yet be marked as dynamic.  */
   2660 	  if (h->dynindx == -1
   2661 	      && ! h->forced_local
   2662 	      && ! bfd_elf_link_record_dynamic_symbol (info, h))
   2663 	    return FALSE;
   2664 
   2665 	  /* If that succeeded, we know we'll be keeping all the
   2666 	     relocs.  */
   2667 	  if (h->dynindx != -1)
   2668 	    goto keep;
   2669 	}
   2670 
   2671       eh->dyn_relocs = NULL;
   2672 
   2673     keep: ;
   2674     }
   2675 
   2676   /* Finally, allocate space.  */
   2677   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2678     {
   2679       asection * sreloc;
   2680 
   2681       sreloc = elf_section_data (p->sec)->sreloc;
   2682 
   2683       BFD_ASSERT (sreloc != NULL);
   2684 
   2685       sreloc->size += p->count * bed->s->sizeof_rela;
   2686     }
   2687 
   2688   return TRUE;
   2689 }
   2690 
   2691 /* Allocate space in .plt, .got and associated reloc sections for
   2692    local dynamic relocs.  */
   2693 
   2694 static bfd_boolean
   2695 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
   2696 {
   2697   struct elf_link_hash_entry *h
   2698     = (struct elf_link_hash_entry *) *slot;
   2699 
   2700   if (h->type != STT_GNU_IFUNC
   2701       || !h->def_regular
   2702       || !h->ref_regular
   2703       || !h->forced_local
   2704       || h->root.type != bfd_link_hash_defined)
   2705     abort ();
   2706 
   2707   return elf_x86_64_allocate_dynrelocs (h, inf);
   2708 }
   2709 
   2710 /* Find any dynamic relocs that apply to read-only sections.  */
   2711 
   2712 static bfd_boolean
   2713 elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
   2714 			       void * inf)
   2715 {
   2716   struct elf_x86_64_link_hash_entry *eh;
   2717   struct elf_dyn_relocs *p;
   2718 
   2719   /* Skip local IFUNC symbols. */
   2720   if (h->forced_local && h->type == STT_GNU_IFUNC)
   2721     return TRUE;
   2722 
   2723   eh = (struct elf_x86_64_link_hash_entry *) h;
   2724   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2725     {
   2726       asection *s = p->sec->output_section;
   2727 
   2728       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   2729 	{
   2730 	  struct bfd_link_info *info = (struct bfd_link_info *) inf;
   2731 
   2732 	  info->flags |= DF_TEXTREL;
   2733 
   2734 	  if (info->warn_shared_textrel && info->shared)
   2735 	    info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
   2736 				    p->sec->owner, h->root.root.string,
   2737 				    p->sec);
   2738 
   2739 	  /* Not an error, just cut short the traversal.  */
   2740 	  return FALSE;
   2741 	}
   2742     }
   2743   return TRUE;
   2744 }
   2745 
   2746 /* Convert
   2747    mov foo@GOTPCREL(%rip), %reg
   2748    to
   2749    lea foo(%rip), %reg
   2750    with the local symbol, foo.  */
   2751 
   2752 static bfd_boolean
   2753 elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
   2754 			       struct bfd_link_info *link_info)
   2755 {
   2756   Elf_Internal_Shdr *symtab_hdr;
   2757   Elf_Internal_Rela *internal_relocs;
   2758   Elf_Internal_Rela *irel, *irelend;
   2759   bfd_byte *contents;
   2760   struct elf_x86_64_link_hash_table *htab;
   2761   bfd_boolean changed_contents;
   2762   bfd_boolean changed_relocs;
   2763   bfd_signed_vma *local_got_refcounts;
   2764 
   2765   /* Don't even try to convert non-ELF outputs.  */
   2766   if (!is_elf_hash_table (link_info->hash))
   2767     return FALSE;
   2768 
   2769   /* Nothing to do if there are no codes, no relocations or no output.  */
   2770   if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
   2771       || sec->reloc_count == 0
   2772       || bfd_is_abs_section (sec->output_section))
   2773     return TRUE;
   2774 
   2775   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2776 
   2777   /* Load the relocations for this section.  */
   2778   internal_relocs = (_bfd_elf_link_read_relocs
   2779 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   2780 		      link_info->keep_memory));
   2781   if (internal_relocs == NULL)
   2782     return FALSE;
   2783 
   2784   htab = elf_x86_64_hash_table (link_info);
   2785   changed_contents = FALSE;
   2786   changed_relocs = FALSE;
   2787   local_got_refcounts = elf_local_got_refcounts (abfd);
   2788 
   2789   /* Get the section contents.  */
   2790   if (elf_section_data (sec)->this_hdr.contents != NULL)
   2791     contents = elf_section_data (sec)->this_hdr.contents;
   2792   else
   2793     {
   2794       if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   2795 	goto error_return;
   2796     }
   2797 
   2798   irelend = internal_relocs + sec->reloc_count;
   2799   for (irel = internal_relocs; irel < irelend; irel++)
   2800     {
   2801       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
   2802       unsigned int r_symndx = htab->r_sym (irel->r_info);
   2803       unsigned int indx;
   2804       struct elf_link_hash_entry *h;
   2805 
   2806       if (r_type != R_X86_64_GOTPCREL)
   2807 	continue;
   2808 
   2809       /* Get the symbol referred to by the reloc.  */
   2810       if (r_symndx < symtab_hdr->sh_info)
   2811 	{
   2812 	  Elf_Internal_Sym *isym;
   2813 
   2814 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   2815 					abfd, r_symndx);
   2816 
   2817 	  /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation.  */
   2818 	  if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
   2819 	      && irel->r_offset >= 2
   2820 	      && bfd_get_8 (input_bfd,
   2821 			    contents + irel->r_offset - 2) == 0x8b)
   2822 	    {
   2823 	      bfd_put_8 (output_bfd, 0x8d,
   2824 			 contents + irel->r_offset - 2);
   2825 	      irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
   2826 	      if (local_got_refcounts != NULL
   2827 		  && local_got_refcounts[r_symndx] > 0)
   2828 		local_got_refcounts[r_symndx] -= 1;
   2829 	      changed_contents = TRUE;
   2830 	      changed_relocs = TRUE;
   2831 	    }
   2832 	  continue;
   2833 	}
   2834 
   2835       indx = r_symndx - symtab_hdr->sh_info;
   2836       h = elf_sym_hashes (abfd)[indx];
   2837       BFD_ASSERT (h != NULL);
   2838 
   2839       while (h->root.type == bfd_link_hash_indirect
   2840 	     || h->root.type == bfd_link_hash_warning)
   2841 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2842 
   2843       /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation.  We also
   2844 	 avoid optimizing _DYNAMIC since ld.so may use its link-time
   2845 	 address.  */
   2846       if (h->def_regular
   2847 	  && h->type != STT_GNU_IFUNC
   2848 	  && h != htab->elf.hdynamic
   2849 	  && SYMBOL_REFERENCES_LOCAL (link_info, h)
   2850 	  && irel->r_offset >= 2
   2851 	  && bfd_get_8 (input_bfd,
   2852 			contents + irel->r_offset - 2) == 0x8b)
   2853 	{
   2854 	  bfd_put_8 (output_bfd, 0x8d,
   2855 		     contents + irel->r_offset - 2);
   2856 	  irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
   2857 	  if (h->got.refcount > 0)
   2858 	    h->got.refcount -= 1;
   2859 	  changed_contents = TRUE;
   2860 	  changed_relocs = TRUE;
   2861 	}
   2862     }
   2863 
   2864   if (contents != NULL
   2865       && elf_section_data (sec)->this_hdr.contents != contents)
   2866     {
   2867       if (!changed_contents && !link_info->keep_memory)
   2868 	free (contents);
   2869       else
   2870 	{
   2871 	  /* Cache the section contents for elf_link_input_bfd.  */
   2872 	  elf_section_data (sec)->this_hdr.contents = contents;
   2873 	}
   2874     }
   2875 
   2876   if (elf_section_data (sec)->relocs != internal_relocs)
   2877     {
   2878       if (!changed_relocs)
   2879 	free (internal_relocs);
   2880       else
   2881 	elf_section_data (sec)->relocs = internal_relocs;
   2882     }
   2883 
   2884   return TRUE;
   2885 
   2886  error_return:
   2887   if (contents != NULL
   2888       && elf_section_data (sec)->this_hdr.contents != contents)
   2889     free (contents);
   2890   if (internal_relocs != NULL
   2891       && elf_section_data (sec)->relocs != internal_relocs)
   2892     free (internal_relocs);
   2893   return FALSE;
   2894 }
   2895 
   2896 /* Set the sizes of the dynamic sections.  */
   2897 
   2898 static bfd_boolean
   2899 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
   2900 				  struct bfd_link_info *info)
   2901 {
   2902   struct elf_x86_64_link_hash_table *htab;
   2903   bfd *dynobj;
   2904   asection *s;
   2905   bfd_boolean relocs;
   2906   bfd *ibfd;
   2907   const struct elf_backend_data *bed;
   2908 
   2909   htab = elf_x86_64_hash_table (info);
   2910   if (htab == NULL)
   2911     return FALSE;
   2912   bed = get_elf_backend_data (output_bfd);
   2913 
   2914   dynobj = htab->elf.dynobj;
   2915   if (dynobj == NULL)
   2916     abort ();
   2917 
   2918   if (htab->elf.dynamic_sections_created)
   2919     {
   2920       /* Set the contents of the .interp section to the interpreter.  */
   2921       if (info->executable)
   2922 	{
   2923 	  s = bfd_get_linker_section (dynobj, ".interp");
   2924 	  if (s == NULL)
   2925 	    abort ();
   2926 	  s->size = htab->dynamic_interpreter_size;
   2927 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
   2928 	}
   2929     }
   2930 
   2931   /* Set up .got offsets for local syms, and space for local dynamic
   2932      relocs.  */
   2933   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   2934     {
   2935       bfd_signed_vma *local_got;
   2936       bfd_signed_vma *end_local_got;
   2937       char *local_tls_type;
   2938       bfd_vma *local_tlsdesc_gotent;
   2939       bfd_size_type locsymcount;
   2940       Elf_Internal_Shdr *symtab_hdr;
   2941       asection *srel;
   2942 
   2943       if (! is_x86_64_elf (ibfd))
   2944 	continue;
   2945 
   2946       for (s = ibfd->sections; s != NULL; s = s->next)
   2947 	{
   2948 	  struct elf_dyn_relocs *p;
   2949 
   2950 	  if (!elf_x86_64_convert_mov_to_lea (ibfd, s, info))
   2951 	    return FALSE;
   2952 
   2953 	  for (p = (struct elf_dyn_relocs *)
   2954 		    (elf_section_data (s)->local_dynrel);
   2955 	       p != NULL;
   2956 	       p = p->next)
   2957 	    {
   2958 	      if (!bfd_is_abs_section (p->sec)
   2959 		  && bfd_is_abs_section (p->sec->output_section))
   2960 		{
   2961 		  /* Input section has been discarded, either because
   2962 		     it is a copy of a linkonce section or due to
   2963 		     linker script /DISCARD/, so we'll be discarding
   2964 		     the relocs too.  */
   2965 		}
   2966 	      else if (p->count != 0)
   2967 		{
   2968 		  srel = elf_section_data (p->sec)->sreloc;
   2969 		  srel->size += p->count * bed->s->sizeof_rela;
   2970 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0
   2971 		      && (info->flags & DF_TEXTREL) == 0)
   2972 		    {
   2973 		      info->flags |= DF_TEXTREL;
   2974 		      if (info->warn_shared_textrel && info->shared)
   2975 			info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
   2976 						p->sec->owner, p->sec);
   2977 		    }
   2978 		}
   2979 	    }
   2980 	}
   2981 
   2982       local_got = elf_local_got_refcounts (ibfd);
   2983       if (!local_got)
   2984 	continue;
   2985 
   2986       symtab_hdr = &elf_symtab_hdr (ibfd);
   2987       locsymcount = symtab_hdr->sh_info;
   2988       end_local_got = local_got + locsymcount;
   2989       local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
   2990       local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
   2991       s = htab->elf.sgot;
   2992       srel = htab->elf.srelgot;
   2993       for (; local_got < end_local_got;
   2994 	   ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
   2995 	{
   2996 	  *local_tlsdesc_gotent = (bfd_vma) -1;
   2997 	  if (*local_got > 0)
   2998 	    {
   2999 	      if (GOT_TLS_GDESC_P (*local_tls_type))
   3000 		{
   3001 		  *local_tlsdesc_gotent = htab->elf.sgotplt->size
   3002 		    - elf_x86_64_compute_jump_table_size (htab);
   3003 		  htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
   3004 		  *local_got = (bfd_vma) -2;
   3005 		}
   3006 	      if (! GOT_TLS_GDESC_P (*local_tls_type)
   3007 		  || GOT_TLS_GD_P (*local_tls_type))
   3008 		{
   3009 		  *local_got = s->size;
   3010 		  s->size += GOT_ENTRY_SIZE;
   3011 		  if (GOT_TLS_GD_P (*local_tls_type))
   3012 		    s->size += GOT_ENTRY_SIZE;
   3013 		}
   3014 	      if (info->shared
   3015 		  || GOT_TLS_GD_ANY_P (*local_tls_type)
   3016 		  || *local_tls_type == GOT_TLS_IE)
   3017 		{
   3018 		  if (GOT_TLS_GDESC_P (*local_tls_type))
   3019 		    {
   3020 		      htab->elf.srelplt->size
   3021 			+= bed->s->sizeof_rela;
   3022 		      htab->tlsdesc_plt = (bfd_vma) -1;
   3023 		    }
   3024 		  if (! GOT_TLS_GDESC_P (*local_tls_type)
   3025 		      || GOT_TLS_GD_P (*local_tls_type))
   3026 		    srel->size += bed->s->sizeof_rela;
   3027 		}
   3028 	    }
   3029 	  else
   3030 	    *local_got = (bfd_vma) -1;
   3031 	}
   3032     }
   3033 
   3034   if (htab->tls_ld_got.refcount > 0)
   3035     {
   3036       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
   3037 	 relocs.  */
   3038       htab->tls_ld_got.offset = htab->elf.sgot->size;
   3039       htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
   3040       htab->elf.srelgot->size += bed->s->sizeof_rela;
   3041     }
   3042   else
   3043     htab->tls_ld_got.offset = -1;
   3044 
   3045   /* Allocate global sym .plt and .got entries, and space for global
   3046      sym dynamic relocs.  */
   3047   elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
   3048 			  info);
   3049 
   3050   /* Allocate .plt and .got entries, and space for local symbols.  */
   3051   htab_traverse (htab->loc_hash_table,
   3052 		 elf_x86_64_allocate_local_dynrelocs,
   3053 		 info);
   3054 
   3055   /* For every jump slot reserved in the sgotplt, reloc_count is
   3056      incremented.  However, when we reserve space for TLS descriptors,
   3057      it's not incremented, so in order to compute the space reserved
   3058      for them, it suffices to multiply the reloc count by the jump
   3059      slot size.
   3060 
   3061      PR ld/13302: We start next_irelative_index at the end of .rela.plt
   3062      so that R_X86_64_IRELATIVE entries come last.  */
   3063   if (htab->elf.srelplt)
   3064     {
   3065       htab->sgotplt_jump_table_size
   3066 	= elf_x86_64_compute_jump_table_size (htab);
   3067       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
   3068     }
   3069   else if (htab->elf.irelplt)
   3070     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
   3071 
   3072   if (htab->tlsdesc_plt)
   3073     {
   3074       /* If we're not using lazy TLS relocations, don't generate the
   3075 	 PLT and GOT entries they require.  */
   3076       if ((info->flags & DF_BIND_NOW))
   3077 	htab->tlsdesc_plt = 0;
   3078       else
   3079 	{
   3080 	  htab->tlsdesc_got = htab->elf.sgot->size;
   3081 	  htab->elf.sgot->size += GOT_ENTRY_SIZE;
   3082 	  /* Reserve room for the initial entry.
   3083 	     FIXME: we could probably do away with it in this case.  */
   3084 	  if (htab->elf.splt->size == 0)
   3085 	    htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
   3086 	  htab->tlsdesc_plt = htab->elf.splt->size;
   3087 	  htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
   3088 	}
   3089     }
   3090 
   3091   if (htab->elf.sgotplt)
   3092     {
   3093       /* Don't allocate .got.plt section if there are no GOT nor PLT
   3094 	 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
   3095       if ((htab->elf.hgot == NULL
   3096 	   || !htab->elf.hgot->ref_regular_nonweak)
   3097 	  && (htab->elf.sgotplt->size
   3098 	      == get_elf_backend_data (output_bfd)->got_header_size)
   3099 	  && (htab->elf.splt == NULL
   3100 	      || htab->elf.splt->size == 0)
   3101 	  && (htab->elf.sgot == NULL
   3102 	      || htab->elf.sgot->size == 0)
   3103 	  && (htab->elf.iplt == NULL
   3104 	      || htab->elf.iplt->size == 0)
   3105 	  && (htab->elf.igotplt == NULL
   3106 	      || htab->elf.igotplt->size == 0))
   3107 	htab->elf.sgotplt->size = 0;
   3108     }
   3109 
   3110   if (htab->plt_eh_frame != NULL
   3111       && htab->elf.splt != NULL
   3112       && htab->elf.splt->size != 0
   3113       && !bfd_is_abs_section (htab->elf.splt->output_section)
   3114       && _bfd_elf_eh_frame_present (info))
   3115     {
   3116       const struct elf_x86_64_backend_data *arch_data
   3117 	= get_elf_x86_64_arch_data (bed);
   3118       htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
   3119     }
   3120 
   3121   /* We now have determined the sizes of the various dynamic sections.
   3122      Allocate memory for them.  */
   3123   relocs = FALSE;
   3124   for (s = dynobj->sections; s != NULL; s = s->next)
   3125     {
   3126       if ((s->flags & SEC_LINKER_CREATED) == 0)
   3127 	continue;
   3128 
   3129       if (s == htab->elf.splt
   3130 	  || s == htab->elf.sgot
   3131 	  || s == htab->elf.sgotplt
   3132 	  || s == htab->elf.iplt
   3133 	  || s == htab->elf.igotplt
   3134 	  || s == htab->plt_bnd
   3135 	  || s == htab->plt_eh_frame
   3136 	  || s == htab->sdynbss)
   3137 	{
   3138 	  /* Strip this section if we don't need it; see the
   3139 	     comment below.  */
   3140 	}
   3141       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
   3142 	{
   3143 	  if (s->size != 0 && s != htab->elf.srelplt)
   3144 	    relocs = TRUE;
   3145 
   3146 	  /* We use the reloc_count field as a counter if we need
   3147 	     to copy relocs into the output file.  */
   3148 	  if (s != htab->elf.srelplt)
   3149 	    s->reloc_count = 0;
   3150 	}
   3151       else
   3152 	{
   3153 	  /* It's not one of our sections, so don't allocate space.  */
   3154 	  continue;
   3155 	}
   3156 
   3157       if (s->size == 0)
   3158 	{
   3159 	  /* If we don't need this section, strip it from the
   3160 	     output file.  This is mostly to handle .rela.bss and
   3161 	     .rela.plt.  We must create both sections in
   3162 	     create_dynamic_sections, because they must be created
   3163 	     before the linker maps input sections to output
   3164 	     sections.  The linker does that before
   3165 	     adjust_dynamic_symbol is called, and it is that
   3166 	     function which decides whether anything needs to go
   3167 	     into these sections.  */
   3168 
   3169 	  s->flags |= SEC_EXCLUDE;
   3170 	  continue;
   3171 	}
   3172 
   3173       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   3174 	continue;
   3175 
   3176       /* Allocate memory for the section contents.  We use bfd_zalloc
   3177 	 here in case unused entries are not reclaimed before the
   3178 	 section's contents are written out.  This should not happen,
   3179 	 but this way if it does, we get a R_X86_64_NONE reloc instead
   3180 	 of garbage.  */
   3181       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   3182       if (s->contents == NULL)
   3183 	return FALSE;
   3184     }
   3185 
   3186   if (htab->plt_eh_frame != NULL
   3187       && htab->plt_eh_frame->contents != NULL)
   3188     {
   3189       const struct elf_x86_64_backend_data *arch_data
   3190 	= get_elf_x86_64_arch_data (bed);
   3191 
   3192       memcpy (htab->plt_eh_frame->contents,
   3193 	      arch_data->eh_frame_plt, htab->plt_eh_frame->size);
   3194       bfd_put_32 (dynobj, htab->elf.splt->size,
   3195 		  htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
   3196     }
   3197 
   3198   if (htab->elf.dynamic_sections_created)
   3199     {
   3200       /* Add some entries to the .dynamic section.  We fill in the
   3201 	 values later, in elf_x86_64_finish_dynamic_sections, but we
   3202 	 must add the entries now so that we get the correct size for
   3203 	 the .dynamic section.	The DT_DEBUG entry is filled in by the
   3204 	 dynamic linker and used by the debugger.  */
   3205 #define add_dynamic_entry(TAG, VAL) \
   3206   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   3207 
   3208       if (info->executable)
   3209 	{
   3210 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   3211 	    return FALSE;
   3212 	}
   3213 
   3214       if (htab->elf.splt->size != 0)
   3215 	{
   3216 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   3217 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   3218 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   3219 	      || !add_dynamic_entry (DT_JMPREL, 0))
   3220 	    return FALSE;
   3221 
   3222 	  if (htab->tlsdesc_plt
   3223 	      && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
   3224 		  || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
   3225 	    return FALSE;
   3226 	}
   3227 
   3228       if (relocs)
   3229 	{
   3230 	  if (!add_dynamic_entry (DT_RELA, 0)
   3231 	      || !add_dynamic_entry (DT_RELASZ, 0)
   3232 	      || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
   3233 	    return FALSE;
   3234 
   3235 	  /* If any dynamic relocs apply to a read-only section,
   3236 	     then we need a DT_TEXTREL entry.  */
   3237 	  if ((info->flags & DF_TEXTREL) == 0)
   3238 	    elf_link_hash_traverse (&htab->elf,
   3239 				    elf_x86_64_readonly_dynrelocs,
   3240 				    info);
   3241 
   3242 	  if ((info->flags & DF_TEXTREL) != 0)
   3243 	    {
   3244 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   3245 		return FALSE;
   3246 	    }
   3247 	}
   3248     }
   3249 #undef add_dynamic_entry
   3250 
   3251   return TRUE;
   3252 }
   3253 
   3254 static bfd_boolean
   3255 elf_x86_64_always_size_sections (bfd *output_bfd,
   3256 				 struct bfd_link_info *info)
   3257 {
   3258   asection *tls_sec = elf_hash_table (info)->tls_sec;
   3259 
   3260   if (tls_sec)
   3261     {
   3262       struct elf_link_hash_entry *tlsbase;
   3263 
   3264       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
   3265 				      "_TLS_MODULE_BASE_",
   3266 				      FALSE, FALSE, FALSE);
   3267 
   3268       if (tlsbase && tlsbase->type == STT_TLS)
   3269 	{
   3270 	  struct elf_x86_64_link_hash_table *htab;
   3271 	  struct bfd_link_hash_entry *bh = NULL;
   3272 	  const struct elf_backend_data *bed
   3273 	    = get_elf_backend_data (output_bfd);
   3274 
   3275 	  htab = elf_x86_64_hash_table (info);
   3276 	  if (htab == NULL)
   3277 	    return FALSE;
   3278 
   3279 	  if (!(_bfd_generic_link_add_one_symbol
   3280 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
   3281 		 tls_sec, 0, NULL, FALSE,
   3282 		 bed->collect, &bh)))
   3283 	    return FALSE;
   3284 
   3285 	  htab->tls_module_base = bh;
   3286 
   3287 	  tlsbase = (struct elf_link_hash_entry *)bh;
   3288 	  tlsbase->def_regular = 1;
   3289 	  tlsbase->other = STV_HIDDEN;
   3290 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
   3291 	}
   3292     }
   3293 
   3294   return TRUE;
   3295 }
   3296 
   3297 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
   3298    executables.  Rather than setting it to the beginning of the TLS
   3299    section, we have to set it to the end.  This function may be called
   3300    multiple times, it is idempotent.  */
   3301 
   3302 static void
   3303 elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
   3304 {
   3305   struct elf_x86_64_link_hash_table *htab;
   3306   struct bfd_link_hash_entry *base;
   3307 
   3308   if (!info->executable)
   3309     return;
   3310 
   3311   htab = elf_x86_64_hash_table (info);
   3312   if (htab == NULL)
   3313     return;
   3314 
   3315   base = htab->tls_module_base;
   3316   if (base == NULL)
   3317     return;
   3318 
   3319   base->u.def.value = htab->elf.tls_size;
   3320 }
   3321 
   3322 /* Return the base VMA address which should be subtracted from real addresses
   3323    when resolving @dtpoff relocation.
   3324    This is PT_TLS segment p_vaddr.  */
   3325 
   3326 static bfd_vma
   3327 elf_x86_64_dtpoff_base (struct bfd_link_info *info)
   3328 {
   3329   /* If tls_sec is NULL, we should have signalled an error already.  */
   3330   if (elf_hash_table (info)->tls_sec == NULL)
   3331     return 0;
   3332   return elf_hash_table (info)->tls_sec->vma;
   3333 }
   3334 
   3335 /* Return the relocation value for @tpoff relocation
   3336    if STT_TLS virtual address is ADDRESS.  */
   3337 
   3338 static bfd_vma
   3339 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
   3340 {
   3341   struct elf_link_hash_table *htab = elf_hash_table (info);
   3342   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
   3343   bfd_vma static_tls_size;
   3344 
   3345   /* If tls_segment is NULL, we should have signalled an error already.  */
   3346   if (htab->tls_sec == NULL)
   3347     return 0;
   3348 
   3349   /* Consider special static TLS alignment requirements.  */
   3350   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
   3351   return address - static_tls_size - htab->tls_sec->vma;
   3352 }
   3353 
   3354 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
   3355    branch?  */
   3356 
   3357 static bfd_boolean
   3358 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
   3359 {
   3360   /* Opcode		Instruction
   3361      0xe8		call
   3362      0xe9		jump
   3363      0x0f 0x8x		conditional jump */
   3364   return ((offset > 0
   3365 	   && (contents [offset - 1] == 0xe8
   3366 	       || contents [offset - 1] == 0xe9))
   3367 	  || (offset > 1
   3368 	      && contents [offset - 2] == 0x0f
   3369 	      && (contents [offset - 1] & 0xf0) == 0x80));
   3370 }
   3371 
   3372 /* Relocate an x86_64 ELF section.  */
   3373 
   3374 static bfd_boolean
   3375 elf_x86_64_relocate_section (bfd *output_bfd,
   3376 			     struct bfd_link_info *info,
   3377 			     bfd *input_bfd,
   3378 			     asection *input_section,
   3379 			     bfd_byte *contents,
   3380 			     Elf_Internal_Rela *relocs,
   3381 			     Elf_Internal_Sym *local_syms,
   3382 			     asection **local_sections)
   3383 {
   3384   struct elf_x86_64_link_hash_table *htab;
   3385   Elf_Internal_Shdr *symtab_hdr;
   3386   struct elf_link_hash_entry **sym_hashes;
   3387   bfd_vma *local_got_offsets;
   3388   bfd_vma *local_tlsdesc_gotents;
   3389   Elf_Internal_Rela *rel;
   3390   Elf_Internal_Rela *relend;
   3391   const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
   3392 
   3393   BFD_ASSERT (is_x86_64_elf (input_bfd));
   3394 
   3395   htab = elf_x86_64_hash_table (info);
   3396   if (htab == NULL)
   3397     return FALSE;
   3398   symtab_hdr = &elf_symtab_hdr (input_bfd);
   3399   sym_hashes = elf_sym_hashes (input_bfd);
   3400   local_got_offsets = elf_local_got_offsets (input_bfd);
   3401   local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
   3402 
   3403   elf_x86_64_set_tls_module_base (info);
   3404 
   3405   rel = relocs;
   3406   relend = relocs + input_section->reloc_count;
   3407   for (; rel < relend; rel++)
   3408     {
   3409       unsigned int r_type;
   3410       reloc_howto_type *howto;
   3411       unsigned long r_symndx;
   3412       struct elf_link_hash_entry *h;
   3413       struct elf_x86_64_link_hash_entry *eh;
   3414       Elf_Internal_Sym *sym;
   3415       asection *sec;
   3416       bfd_vma off, offplt, plt_offset;
   3417       bfd_vma relocation;
   3418       bfd_boolean unresolved_reloc;
   3419       bfd_reloc_status_type r;
   3420       int tls_type;
   3421       asection *base_got, *resolved_plt;
   3422       bfd_vma st_size;
   3423 
   3424       r_type = ELF32_R_TYPE (rel->r_info);
   3425       if (r_type == (int) R_X86_64_GNU_VTINHERIT
   3426 	  || r_type == (int) R_X86_64_GNU_VTENTRY)
   3427 	continue;
   3428 
   3429       if (r_type >= (int) R_X86_64_standard)
   3430 	{
   3431 	  (*_bfd_error_handler)
   3432 	    (_("%B: unrecognized relocation (0x%x) in section `%A'"),
   3433 	     input_bfd, input_section, r_type);
   3434 	  bfd_set_error (bfd_error_bad_value);
   3435 	  return FALSE;
   3436 	}
   3437 
   3438       if (r_type != (int) R_X86_64_32
   3439 	  || ABI_64_P (output_bfd))
   3440 	howto = x86_64_elf_howto_table + r_type;
   3441       else
   3442 	howto = (x86_64_elf_howto_table
   3443 		 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
   3444       r_symndx = htab->r_sym (rel->r_info);
   3445       h = NULL;
   3446       sym = NULL;
   3447       sec = NULL;
   3448       unresolved_reloc = FALSE;
   3449       if (r_symndx < symtab_hdr->sh_info)
   3450 	{
   3451 	  sym = local_syms + r_symndx;
   3452 	  sec = local_sections[r_symndx];
   3453 
   3454 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
   3455 						&sec, rel);
   3456 	  st_size = sym->st_size;
   3457 
   3458 	  /* Relocate against local STT_GNU_IFUNC symbol.  */
   3459 	  if (!info->relocatable
   3460 	      && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   3461 	    {
   3462 	      h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
   3463 						 rel, FALSE);
   3464 	      if (h == NULL)
   3465 		abort ();
   3466 
   3467 	      /* Set STT_GNU_IFUNC symbol value.  */
   3468 	      h->root.u.def.value = sym->st_value;
   3469 	      h->root.u.def.section = sec;
   3470 	    }
   3471 	}
   3472       else
   3473 	{
   3474 	  bfd_boolean warned ATTRIBUTE_UNUSED;
   3475 	  bfd_boolean ignored ATTRIBUTE_UNUSED;
   3476 
   3477 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   3478 				   r_symndx, symtab_hdr, sym_hashes,
   3479 				   h, sec, relocation,
   3480 				   unresolved_reloc, warned, ignored);
   3481 	  st_size = h->size;
   3482 	}
   3483 
   3484       if (sec != NULL && discarded_section (sec))
   3485 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   3486 					 rel, 1, relend, howto, 0, contents);
   3487 
   3488       if (info->relocatable)
   3489 	continue;
   3490 
   3491       if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
   3492 	{
   3493 	  if (r_type == R_X86_64_64)
   3494 	    {
   3495 	      /* For x32, treat R_X86_64_64 like R_X86_64_32 and
   3496 		 zero-extend it to 64bit if addend is zero.  */
   3497 	      r_type = R_X86_64_32;
   3498 	      memset (contents + rel->r_offset + 4, 0, 4);
   3499 	    }
   3500 	  else if (r_type == R_X86_64_SIZE64)
   3501 	    {
   3502 	      /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
   3503 		 zero-extend it to 64bit if addend is zero.  */
   3504 	      r_type = R_X86_64_SIZE32;
   3505 	      memset (contents + rel->r_offset + 4, 0, 4);
   3506 	    }
   3507 	}
   3508 
   3509       eh = (struct elf_x86_64_link_hash_entry *) h;
   3510 
   3511       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
   3512 	 it here if it is defined in a non-shared object.  */
   3513       if (h != NULL
   3514 	  && h->type == STT_GNU_IFUNC
   3515 	  && h->def_regular)
   3516 	{
   3517 	  bfd_vma plt_index;
   3518 	  const char *name;
   3519 
   3520 	  if ((input_section->flags & SEC_ALLOC) == 0
   3521 	      || h->plt.offset == (bfd_vma) -1)
   3522 	    abort ();
   3523 
   3524 	  /* STT_GNU_IFUNC symbol must go through PLT.  */
   3525 	  if (htab->elf.splt != NULL)
   3526 	    {
   3527 	      if (htab->plt_bnd != NULL)
   3528 		{
   3529 		  resolved_plt = htab->plt_bnd;
   3530 		  plt_offset = eh->plt_bnd.offset;
   3531 		}
   3532 	      else
   3533 		{
   3534 		  resolved_plt = htab->elf.splt;
   3535 		  plt_offset =  h->plt.offset;
   3536 		}
   3537 	    }
   3538 	  else
   3539 	    {
   3540 	      resolved_plt = htab->elf.iplt;
   3541 	      plt_offset =  h->plt.offset;
   3542 	    }
   3543 
   3544 	  relocation = (resolved_plt->output_section->vma
   3545 			+ resolved_plt->output_offset + plt_offset);
   3546 
   3547 	  switch (r_type)
   3548 	    {
   3549 	    default:
   3550 	      if (h->root.root.string)
   3551 		name = h->root.root.string;
   3552 	      else
   3553 		name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   3554 					 NULL);
   3555 	      (*_bfd_error_handler)
   3556 		(_("%B: relocation %s against STT_GNU_IFUNC "
   3557 		   "symbol `%s' isn't handled by %s"), input_bfd,
   3558 		 x86_64_elf_howto_table[r_type].name,
   3559 		 name, __FUNCTION__);
   3560 	      bfd_set_error (bfd_error_bad_value);
   3561 	      return FALSE;
   3562 
   3563 	    case R_X86_64_32S:
   3564 	      if (info->shared)
   3565 		abort ();
   3566 	      goto do_relocation;
   3567 
   3568 	    case R_X86_64_32:
   3569 	      if (ABI_64_P (output_bfd))
   3570 		goto do_relocation;
   3571 	      /* FALLTHROUGH */
   3572 	    case R_X86_64_64:
   3573 	      if (rel->r_addend != 0)
   3574 		{
   3575 		  if (h->root.root.string)
   3576 		    name = h->root.root.string;
   3577 		  else
   3578 		    name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   3579 					     sym, NULL);
   3580 		  (*_bfd_error_handler)
   3581 		    (_("%B: relocation %s against STT_GNU_IFUNC "
   3582 		       "symbol `%s' has non-zero addend: %d"),
   3583 		     input_bfd, x86_64_elf_howto_table[r_type].name,
   3584 		     name, rel->r_addend);
   3585 		  bfd_set_error (bfd_error_bad_value);
   3586 		  return FALSE;
   3587 		}
   3588 
   3589 	      /* Generate dynamic relcoation only when there is a
   3590 		 non-GOT reference in a shared object.  */
   3591 	      if (info->shared && h->non_got_ref)
   3592 		{
   3593 		  Elf_Internal_Rela outrel;
   3594 		  asection *sreloc;
   3595 
   3596 		  /* Need a dynamic relocation to get the real function
   3597 		     address.  */
   3598 		  outrel.r_offset = _bfd_elf_section_offset (output_bfd,
   3599 							     info,
   3600 							     input_section,
   3601 							     rel->r_offset);
   3602 		  if (outrel.r_offset == (bfd_vma) -1
   3603 		      || outrel.r_offset == (bfd_vma) -2)
   3604 		    abort ();
   3605 
   3606 		  outrel.r_offset += (input_section->output_section->vma
   3607 				      + input_section->output_offset);
   3608 
   3609 		  if (h->dynindx == -1
   3610 		      || h->forced_local
   3611 		      || info->executable)
   3612 		    {
   3613 		      /* This symbol is resolved locally.  */
   3614 		      outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
   3615 		      outrel.r_addend = (h->root.u.def.value
   3616 					 + h->root.u.def.section->output_section->vma
   3617 					 + h->root.u.def.section->output_offset);
   3618 		    }
   3619 		  else
   3620 		    {
   3621 		      outrel.r_info = htab->r_info (h->dynindx, r_type);
   3622 		      outrel.r_addend = 0;
   3623 		    }
   3624 
   3625 		  sreloc = htab->elf.irelifunc;
   3626 		  elf_append_rela (output_bfd, sreloc, &outrel);
   3627 
   3628 		  /* If this reloc is against an external symbol, we
   3629 		     do not want to fiddle with the addend.  Otherwise,
   3630 		     we need to include the symbol value so that it
   3631 		     becomes an addend for the dynamic reloc.  For an
   3632 		     internal symbol, we have updated addend.  */
   3633 		  continue;
   3634 		}
   3635 	      /* FALLTHROUGH */
   3636 	    case R_X86_64_PC32:
   3637 	    case R_X86_64_PC32_BND:
   3638 	    case R_X86_64_PC64:
   3639 	    case R_X86_64_PLT32:
   3640 	    case R_X86_64_PLT32_BND:
   3641 	      goto do_relocation;
   3642 
   3643 	    case R_X86_64_GOTPCREL:
   3644 	    case R_X86_64_GOTPCREL64:
   3645 	      base_got = htab->elf.sgot;
   3646 	      off = h->got.offset;
   3647 
   3648 	      if (base_got == NULL)
   3649 		abort ();
   3650 
   3651 	      if (off == (bfd_vma) -1)
   3652 		{
   3653 		  /* We can't use h->got.offset here to save state, or
   3654 		     even just remember the offset, as finish_dynamic_symbol
   3655 		     would use that as offset into .got.  */
   3656 
   3657 		  if (htab->elf.splt != NULL)
   3658 		    {
   3659 		      plt_index = h->plt.offset / plt_entry_size - 1;
   3660 		      off = (plt_index + 3) * GOT_ENTRY_SIZE;
   3661 		      base_got = htab->elf.sgotplt;
   3662 		    }
   3663 		  else
   3664 		    {
   3665 		      plt_index = h->plt.offset / plt_entry_size;
   3666 		      off = plt_index * GOT_ENTRY_SIZE;
   3667 		      base_got = htab->elf.igotplt;
   3668 		    }
   3669 
   3670 		  if (h->dynindx == -1
   3671 		      || h->forced_local
   3672 		      || info->symbolic)
   3673 		    {
   3674 		      /* This references the local defitionion.  We must
   3675 			 initialize this entry in the global offset table.
   3676 			 Since the offset must always be a multiple of 8,
   3677 			 we use the least significant bit to record
   3678 			 whether we have initialized it already.
   3679 
   3680 			 When doing a dynamic link, we create a .rela.got
   3681 			 relocation entry to initialize the value.  This
   3682 			 is done in the finish_dynamic_symbol routine.	 */
   3683 		      if ((off & 1) != 0)
   3684 			off &= ~1;
   3685 		      else
   3686 			{
   3687 			  bfd_put_64 (output_bfd, relocation,
   3688 				      base_got->contents + off);
   3689 			  /* Note that this is harmless for the GOTPLT64
   3690 			     case, as -1 | 1 still is -1.  */
   3691 			  h->got.offset |= 1;
   3692 			}
   3693 		    }
   3694 		}
   3695 
   3696 	      relocation = (base_got->output_section->vma
   3697 			    + base_got->output_offset + off);
   3698 
   3699 	      goto do_relocation;
   3700 	    }
   3701 	}
   3702 
   3703       /* When generating a shared object, the relocations handled here are
   3704 	 copied into the output file to be resolved at run time.  */
   3705       switch (r_type)
   3706 	{
   3707 	case R_X86_64_GOT32:
   3708 	case R_X86_64_GOT64:
   3709 	  /* Relocation is to the entry for this symbol in the global
   3710 	     offset table.  */
   3711 	case R_X86_64_GOTPCREL:
   3712 	case R_X86_64_GOTPCREL64:
   3713 	  /* Use global offset table entry as symbol value.  */
   3714 	case R_X86_64_GOTPLT64:
   3715 	  /* This is obsolete and treated the the same as GOT64.  */
   3716 	  base_got = htab->elf.sgot;
   3717 
   3718 	  if (htab->elf.sgot == NULL)
   3719 	    abort ();
   3720 
   3721 	  if (h != NULL)
   3722 	    {
   3723 	      bfd_boolean dyn;
   3724 
   3725 	      off = h->got.offset;
   3726 	      if (h->needs_plt
   3727 		  && h->plt.offset != (bfd_vma)-1
   3728 		  && off == (bfd_vma)-1)
   3729 		{
   3730 		  /* We can't use h->got.offset here to save
   3731 		     state, or even just remember the offset, as
   3732 		     finish_dynamic_symbol would use that as offset into
   3733 		     .got.  */
   3734 		  bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
   3735 		  off = (plt_index + 3) * GOT_ENTRY_SIZE;
   3736 		  base_got = htab->elf.sgotplt;
   3737 		}
   3738 
   3739 	      dyn = htab->elf.dynamic_sections_created;
   3740 
   3741 	      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   3742 		  || (info->shared
   3743 		      && SYMBOL_REFERENCES_LOCAL (info, h))
   3744 		  || (ELF_ST_VISIBILITY (h->other)
   3745 		      && h->root.type == bfd_link_hash_undefweak))
   3746 		{
   3747 		  /* This is actually a static link, or it is a -Bsymbolic
   3748 		     link and the symbol is defined locally, or the symbol
   3749 		     was forced to be local because of a version file.	We
   3750 		     must initialize this entry in the global offset table.
   3751 		     Since the offset must always be a multiple of 8, we
   3752 		     use the least significant bit to record whether we
   3753 		     have initialized it already.
   3754 
   3755 		     When doing a dynamic link, we create a .rela.got
   3756 		     relocation entry to initialize the value.	This is
   3757 		     done in the finish_dynamic_symbol routine.	 */
   3758 		  if ((off & 1) != 0)
   3759 		    off &= ~1;
   3760 		  else
   3761 		    {
   3762 		      bfd_put_64 (output_bfd, relocation,
   3763 				  base_got->contents + off);
   3764 		      /* Note that this is harmless for the GOTPLT64 case,
   3765 			 as -1 | 1 still is -1.  */
   3766 		      h->got.offset |= 1;
   3767 		    }
   3768 		}
   3769 	      else
   3770 		unresolved_reloc = FALSE;
   3771 	    }
   3772 	  else
   3773 	    {
   3774 	      if (local_got_offsets == NULL)
   3775 		abort ();
   3776 
   3777 	      off = local_got_offsets[r_symndx];
   3778 
   3779 	      /* The offset must always be a multiple of 8.  We use
   3780 		 the least significant bit to record whether we have
   3781 		 already generated the necessary reloc.	 */
   3782 	      if ((off & 1) != 0)
   3783 		off &= ~1;
   3784 	      else
   3785 		{
   3786 		  bfd_put_64 (output_bfd, relocation,
   3787 			      base_got->contents + off);
   3788 
   3789 		  if (info->shared)
   3790 		    {
   3791 		      asection *s;
   3792 		      Elf_Internal_Rela outrel;
   3793 
   3794 		      /* We need to generate a R_X86_64_RELATIVE reloc
   3795 			 for the dynamic linker.  */
   3796 		      s = htab->elf.srelgot;
   3797 		      if (s == NULL)
   3798 			abort ();
   3799 
   3800 		      outrel.r_offset = (base_got->output_section->vma
   3801 					 + base_got->output_offset
   3802 					 + off);
   3803 		      outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
   3804 		      outrel.r_addend = relocation;
   3805 		      elf_append_rela (output_bfd, s, &outrel);
   3806 		    }
   3807 
   3808 		  local_got_offsets[r_symndx] |= 1;
   3809 		}
   3810 	    }
   3811 
   3812 	  if (off >= (bfd_vma) -2)
   3813 	    abort ();
   3814 
   3815 	  relocation = base_got->output_section->vma
   3816 		       + base_got->output_offset + off;
   3817 	  if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
   3818 	    relocation -= htab->elf.sgotplt->output_section->vma
   3819 			  - htab->elf.sgotplt->output_offset;
   3820 
   3821 	  break;
   3822 
   3823 	case R_X86_64_GOTOFF64:
   3824 	  /* Relocation is relative to the start of the global offset
   3825 	     table.  */
   3826 
   3827 	  /* Check to make sure it isn't a protected function symbol
   3828 	     for shared library since it may not be local when used
   3829 	     as function address.  */
   3830 	  if (!info->executable
   3831 	      && h
   3832 	      && !SYMBOLIC_BIND (info, h)
   3833 	      && h->def_regular
   3834 	      && h->type == STT_FUNC
   3835 	      && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
   3836 	    {
   3837 	      (*_bfd_error_handler)
   3838 		(_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
   3839 		 input_bfd, h->root.root.string);
   3840 	      bfd_set_error (bfd_error_bad_value);
   3841 	      return FALSE;
   3842 	    }
   3843 
   3844 	  /* Note that sgot is not involved in this
   3845 	     calculation.  We always want the start of .got.plt.  If we
   3846 	     defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
   3847 	     permitted by the ABI, we might have to change this
   3848 	     calculation.  */
   3849 	  relocation -= htab->elf.sgotplt->output_section->vma
   3850 			+ htab->elf.sgotplt->output_offset;
   3851 	  break;
   3852 
   3853 	case R_X86_64_GOTPC32:
   3854 	case R_X86_64_GOTPC64:
   3855 	  /* Use global offset table as symbol value.  */
   3856 	  relocation = htab->elf.sgotplt->output_section->vma
   3857 		       + htab->elf.sgotplt->output_offset;
   3858 	  unresolved_reloc = FALSE;
   3859 	  break;
   3860 
   3861 	case R_X86_64_PLTOFF64:
   3862 	  /* Relocation is PLT entry relative to GOT.  For local
   3863 	     symbols it's the symbol itself relative to GOT.  */
   3864 	  if (h != NULL
   3865 	      /* See PLT32 handling.  */
   3866 	      && h->plt.offset != (bfd_vma) -1
   3867 	      && htab->elf.splt != NULL)
   3868 	    {
   3869 	      if (htab->plt_bnd != NULL)
   3870 		{
   3871 		  resolved_plt = htab->plt_bnd;
   3872 		  plt_offset = eh->plt_bnd.offset;
   3873 		}
   3874 	      else
   3875 		{
   3876 		  resolved_plt = htab->elf.splt;
   3877 		  plt_offset = h->plt.offset;
   3878 		}
   3879 
   3880 	      relocation = (resolved_plt->output_section->vma
   3881 			    + resolved_plt->output_offset
   3882 			    + plt_offset);
   3883 	      unresolved_reloc = FALSE;
   3884 	    }
   3885 
   3886 	  relocation -= htab->elf.sgotplt->output_section->vma
   3887 			+ htab->elf.sgotplt->output_offset;
   3888 	  break;
   3889 
   3890 	case R_X86_64_PLT32:
   3891 	case R_X86_64_PLT32_BND:
   3892 	  /* Relocation is to the entry for this symbol in the
   3893 	     procedure linkage table.  */
   3894 
   3895 	  /* Resolve a PLT32 reloc against a local symbol directly,
   3896 	     without using the procedure linkage table.	 */
   3897 	  if (h == NULL)
   3898 	    break;
   3899 
   3900 	  if (h->plt.offset == (bfd_vma) -1
   3901 	      || htab->elf.splt == NULL)
   3902 	    {
   3903 	      /* We didn't make a PLT entry for this symbol.  This
   3904 		 happens when statically linking PIC code, or when
   3905 		 using -Bsymbolic.  */
   3906 	      break;
   3907 	    }
   3908 
   3909 	  if (htab->plt_bnd != NULL)
   3910 	    {
   3911 	      resolved_plt = htab->plt_bnd;
   3912 	      plt_offset = eh->plt_bnd.offset;
   3913 	    }
   3914 	  else
   3915 	    {
   3916 	      resolved_plt = htab->elf.splt;
   3917 	      plt_offset = h->plt.offset;
   3918 	    }
   3919 
   3920 	  relocation = (resolved_plt->output_section->vma
   3921 			+ resolved_plt->output_offset
   3922 			+ plt_offset);
   3923 	  unresolved_reloc = FALSE;
   3924 	  break;
   3925 
   3926 	case R_X86_64_SIZE32:
   3927 	case R_X86_64_SIZE64:
   3928 	  /* Set to symbol size.  */
   3929 	  relocation = st_size;
   3930 	  goto direct;
   3931 
   3932 	case R_X86_64_PC8:
   3933 	case R_X86_64_PC16:
   3934 	case R_X86_64_PC32:
   3935 	case R_X86_64_PC32_BND:
   3936 	  if (info->shared
   3937 	      && (input_section->flags & SEC_ALLOC) != 0
   3938 	      && (input_section->flags & SEC_READONLY) != 0
   3939 	      && h != NULL)
   3940 	    {
   3941 	      bfd_boolean fail = FALSE;
   3942 	      bfd_boolean branch
   3943 		= ((r_type == R_X86_64_PC32
   3944 		    || r_type == R_X86_64_PC32_BND)
   3945 		   && is_32bit_relative_branch (contents, rel->r_offset));
   3946 
   3947 	      if (SYMBOL_REFERENCES_LOCAL (info, h))
   3948 		{
   3949 		  /* Symbol is referenced locally.  Make sure it is
   3950 		     defined locally or for a branch.  */
   3951 		  fail = !h->def_regular && !branch;
   3952 		}
   3953 	      else
   3954 		{
   3955 		  /* Symbol isn't referenced locally.  We only allow
   3956 		     branch to symbol with non-default visibility. */
   3957 		  fail = (!branch
   3958 			  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
   3959 		}
   3960 
   3961 	      if (fail)
   3962 		{
   3963 		  const char *fmt;
   3964 		  const char *v;
   3965 		  const char *pic = "";
   3966 
   3967 		  switch (ELF_ST_VISIBILITY (h->other))
   3968 		    {
   3969 		    case STV_HIDDEN:
   3970 		      v = _("hidden symbol");
   3971 		      break;
   3972 		    case STV_INTERNAL:
   3973 		      v = _("internal symbol");
   3974 		      break;
   3975 		    case STV_PROTECTED:
   3976 		      v = _("protected symbol");
   3977 		      break;
   3978 		    default:
   3979 		      v = _("symbol");
   3980 		      pic = _("; recompile with -fPIC");
   3981 		      break;
   3982 		    }
   3983 
   3984 		  if (h->def_regular)
   3985 		    fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
   3986 		  else
   3987 		    fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
   3988 
   3989 		  (*_bfd_error_handler) (fmt, input_bfd,
   3990 					 x86_64_elf_howto_table[r_type].name,
   3991 					 v,  h->root.root.string, pic);
   3992 		  bfd_set_error (bfd_error_bad_value);
   3993 		  return FALSE;
   3994 		}
   3995 	    }
   3996 	  /* Fall through.  */
   3997 
   3998 	case R_X86_64_8:
   3999 	case R_X86_64_16:
   4000 	case R_X86_64_32:
   4001 	case R_X86_64_PC64:
   4002 	case R_X86_64_64:
   4003 	  /* FIXME: The ABI says the linker should make sure the value is
   4004 	     the same when it's zeroextended to 64 bit.	 */
   4005 
   4006 direct:
   4007 	  if ((input_section->flags & SEC_ALLOC) == 0)
   4008 	    break;
   4009 
   4010 	  if ((info->shared
   4011 	       && (h == NULL
   4012 		   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   4013 		   || h->root.type != bfd_link_hash_undefweak)
   4014 	       && ((! IS_X86_64_PCREL_TYPE (r_type)
   4015 		      && r_type != R_X86_64_SIZE32
   4016 		      && r_type != R_X86_64_SIZE64)
   4017 		   || ! SYMBOL_CALLS_LOCAL (info, h)))
   4018 	      || (ELIMINATE_COPY_RELOCS
   4019 		  && !info->shared
   4020 		  && h != NULL
   4021 		  && h->dynindx != -1
   4022 		  && !h->non_got_ref
   4023 		  && ((h->def_dynamic
   4024 		       && !h->def_regular)
   4025 		      || h->root.type == bfd_link_hash_undefweak
   4026 		      || h->root.type == bfd_link_hash_undefined)))
   4027 	    {
   4028 	      Elf_Internal_Rela outrel;
   4029 	      bfd_boolean skip, relocate;
   4030 	      asection *sreloc;
   4031 
   4032 	      /* When generating a shared object, these relocations
   4033 		 are copied into the output file to be resolved at run
   4034 		 time.	*/
   4035 	      skip = FALSE;
   4036 	      relocate = FALSE;
   4037 
   4038 	      outrel.r_offset =
   4039 		_bfd_elf_section_offset (output_bfd, info, input_section,
   4040 					 rel->r_offset);
   4041 	      if (outrel.r_offset == (bfd_vma) -1)
   4042 		skip = TRUE;
   4043 	      else if (outrel.r_offset == (bfd_vma) -2)
   4044 		skip = TRUE, relocate = TRUE;
   4045 
   4046 	      outrel.r_offset += (input_section->output_section->vma
   4047 				  + input_section->output_offset);
   4048 
   4049 	      if (skip)
   4050 		memset (&outrel, 0, sizeof outrel);
   4051 
   4052 	      /* h->dynindx may be -1 if this symbol was marked to
   4053 		 become local.  */
   4054 	      else if (h != NULL
   4055 		       && h->dynindx != -1
   4056 		       && (IS_X86_64_PCREL_TYPE (r_type)
   4057 			   || ! info->shared
   4058 			   || ! SYMBOLIC_BIND (info, h)
   4059 			   || ! h->def_regular))
   4060 		{
   4061 		  outrel.r_info = htab->r_info (h->dynindx, r_type);
   4062 		  outrel.r_addend = rel->r_addend;
   4063 		}
   4064 	      else
   4065 		{
   4066 		  /* This symbol is local, or marked to become local.  */
   4067 		  if (r_type == htab->pointer_r_type)
   4068 		    {
   4069 		      relocate = TRUE;
   4070 		      outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
   4071 		      outrel.r_addend = relocation + rel->r_addend;
   4072 		    }
   4073 		  else if (r_type == R_X86_64_64
   4074 			   && !ABI_64_P (output_bfd))
   4075 		    {
   4076 		      relocate = TRUE;
   4077 		      outrel.r_info = htab->r_info (0,
   4078 						    R_X86_64_RELATIVE64);
   4079 		      outrel.r_addend = relocation + rel->r_addend;
   4080 		      /* Check addend overflow.  */
   4081 		      if ((outrel.r_addend & 0x80000000)
   4082 			  != (rel->r_addend & 0x80000000))
   4083 			{
   4084 			  const char *name;
   4085 			  int addend = rel->r_addend;
   4086 			  if (h && h->root.root.string)
   4087 			    name = h->root.root.string;
   4088 			  else
   4089 			    name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   4090 						     sym, NULL);
   4091 			  if (addend < 0)
   4092 			    (*_bfd_error_handler)
   4093 			      (_("%B: addend -0x%x in relocation %s against "
   4094 				 "symbol `%s' at 0x%lx in section `%A' is "
   4095 				 "out of range"),
   4096 			       input_bfd, input_section, addend,
   4097 			       x86_64_elf_howto_table[r_type].name,
   4098 			       name, (unsigned long) rel->r_offset);
   4099 			  else
   4100 			    (*_bfd_error_handler)
   4101 			      (_("%B: addend 0x%x in relocation %s against "
   4102 				 "symbol `%s' at 0x%lx in section `%A' is "
   4103 				 "out of range"),
   4104 			       input_bfd, input_section, addend,
   4105 			       x86_64_elf_howto_table[r_type].name,
   4106 			       name, (unsigned long) rel->r_offset);
   4107 			  bfd_set_error (bfd_error_bad_value);
   4108 			  return FALSE;
   4109 			}
   4110 		    }
   4111 		  else
   4112 		    {
   4113 		      long sindx;
   4114 
   4115 		      if (bfd_is_abs_section (sec))
   4116 			sindx = 0;
   4117 		      else if (sec == NULL || sec->owner == NULL)
   4118 			{
   4119 			  bfd_set_error (bfd_error_bad_value);
   4120 			  return FALSE;
   4121 			}
   4122 		      else
   4123 			{
   4124 			  asection *osec;
   4125 
   4126 			  /* We are turning this relocation into one
   4127 			     against a section symbol.  It would be
   4128 			     proper to subtract the symbol's value,
   4129 			     osec->vma, from the emitted reloc addend,
   4130 			     but ld.so expects buggy relocs.  */
   4131 			  osec = sec->output_section;
   4132 			  sindx = elf_section_data (osec)->dynindx;
   4133 			  if (sindx == 0)
   4134 			    {
   4135 			      asection *oi = htab->elf.text_index_section;
   4136 			      sindx = elf_section_data (oi)->dynindx;
   4137 			    }
   4138 			  BFD_ASSERT (sindx != 0);
   4139 			}
   4140 
   4141 		      outrel.r_info = htab->r_info (sindx, r_type);
   4142 		      outrel.r_addend = relocation + rel->r_addend;
   4143 		    }
   4144 		}
   4145 
   4146 	      sreloc = elf_section_data (input_section)->sreloc;
   4147 
   4148 	      if (sreloc == NULL || sreloc->contents == NULL)
   4149 		{
   4150 		  r = bfd_reloc_notsupported;
   4151 		  goto check_relocation_error;
   4152 		}
   4153 
   4154 	      elf_append_rela (output_bfd, sreloc, &outrel);
   4155 
   4156 	      /* If this reloc is against an external symbol, we do
   4157 		 not want to fiddle with the addend.  Otherwise, we
   4158 		 need to include the symbol value so that it becomes
   4159 		 an addend for the dynamic reloc.  */
   4160 	      if (! relocate)
   4161 		continue;
   4162 	    }
   4163 
   4164 	  break;
   4165 
   4166 	case R_X86_64_TLSGD:
   4167 	case R_X86_64_GOTPC32_TLSDESC:
   4168 	case R_X86_64_TLSDESC_CALL:
   4169 	case R_X86_64_GOTTPOFF:
   4170 	  tls_type = GOT_UNKNOWN;
   4171 	  if (h == NULL && local_got_offsets)
   4172 	    tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
   4173 	  else if (h != NULL)
   4174 	    tls_type = elf_x86_64_hash_entry (h)->tls_type;
   4175 
   4176 	  if (! elf_x86_64_tls_transition (info, input_bfd,
   4177 					   input_section, contents,
   4178 					   symtab_hdr, sym_hashes,
   4179 					   &r_type, tls_type, rel,
   4180 					   relend, h, r_symndx))
   4181 	    return FALSE;
   4182 
   4183 	  if (r_type == R_X86_64_TPOFF32)
   4184 	    {
   4185 	      bfd_vma roff = rel->r_offset;
   4186 
   4187 	      BFD_ASSERT (! unresolved_reloc);
   4188 
   4189 	      if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
   4190 		{
   4191 		  /* GD->LE transition.  For 64bit, change
   4192 		     .byte 0x66; leaq foo@tlsgd(%rip), %rdi
   4193 		     .word 0x6666; rex64; call __tls_get_addr
   4194 		     into:
   4195 		     movq %fs:0, %rax
   4196 		     leaq foo@tpoff(%rax), %rax
   4197 		     For 32bit, change
   4198 		     leaq foo@tlsgd(%rip), %rdi
   4199 		     .word 0x6666; rex64; call __tls_get_addr
   4200 		     into:
   4201 		     movl %fs:0, %eax
   4202 		     leaq foo@tpoff(%rax), %rax
   4203 		     For largepic, change:
   4204 		     leaq foo@tlsgd(%rip), %rdi
   4205 		     movabsq $__tls_get_addr@pltoff, %rax
   4206 		     addq %rbx, %rax
   4207 		     call *%rax
   4208 		     into:
   4209 		     movq %fs:0, %rax
   4210 		     leaq foo@tpoff(%rax), %rax
   4211 		     nopw 0x0(%rax,%rax,1) */
   4212 		  int largepic = 0;
   4213 		  if (ABI_64_P (output_bfd)
   4214 		      && contents[roff + 5] == (bfd_byte) '\xb8')
   4215 		    {
   4216 		      memcpy (contents + roff - 3,
   4217 			      "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
   4218 			      "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
   4219 		      largepic = 1;
   4220 		    }
   4221 		  else if (ABI_64_P (output_bfd))
   4222 		    memcpy (contents + roff - 4,
   4223 			    "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
   4224 			    16);
   4225 		  else
   4226 		    memcpy (contents + roff - 3,
   4227 			    "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
   4228 			    15);
   4229 		  bfd_put_32 (output_bfd,
   4230 			      elf_x86_64_tpoff (info, relocation),
   4231 			      contents + roff + 8 + largepic);
   4232 		  /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64.  */
   4233 		  rel++;
   4234 		  continue;
   4235 		}
   4236 	      else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
   4237 		{
   4238 		  /* GDesc -> LE transition.
   4239 		     It's originally something like:
   4240 		     leaq x@tlsdesc(%rip), %rax
   4241 
   4242 		     Change it to:
   4243 		     movl $x@tpoff, %rax.  */
   4244 
   4245 		  unsigned int val, type;
   4246 
   4247 		  type = bfd_get_8 (input_bfd, contents + roff - 3);
   4248 		  val = bfd_get_8 (input_bfd, contents + roff - 1);
   4249 		  bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
   4250 			     contents + roff - 3);
   4251 		  bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
   4252 		  bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
   4253 			     contents + roff - 1);
   4254 		  bfd_put_32 (output_bfd,
   4255 			      elf_x86_64_tpoff (info, relocation),
   4256 			      contents + roff);
   4257 		  continue;
   4258 		}
   4259 	      else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
   4260 		{
   4261 		  /* GDesc -> LE transition.
   4262 		     It's originally:
   4263 		     call *(%rax)
   4264 		     Turn it into:
   4265 		     xchg %ax,%ax.  */
   4266 		  bfd_put_8 (output_bfd, 0x66, contents + roff);
   4267 		  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
   4268 		  continue;
   4269 		}
   4270 	      else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
   4271 		{
   4272 		  /* IE->LE transition:
   4273 		     For 64bit, originally it can be one of:
   4274 		     movq foo@gottpoff(%rip), %reg
   4275 		     addq foo@gottpoff(%rip), %reg
   4276 		     We change it into:
   4277 		     movq $foo, %reg
   4278 		     leaq foo(%reg), %reg
   4279 		     addq $foo, %reg.
   4280 		     For 32bit, originally it can be one of:
   4281 		     movq foo@gottpoff(%rip), %reg
   4282 		     addl foo@gottpoff(%rip), %reg
   4283 		     We change it into:
   4284 		     movq $foo, %reg
   4285 		     leal foo(%reg), %reg
   4286 		     addl $foo, %reg. */
   4287 
   4288 		  unsigned int val, type, reg;
   4289 
   4290 		  if (roff >= 3)
   4291 		    val = bfd_get_8 (input_bfd, contents + roff - 3);
   4292 		  else
   4293 		    val = 0;
   4294 		  type = bfd_get_8 (input_bfd, contents + roff - 2);
   4295 		  reg = bfd_get_8 (input_bfd, contents + roff - 1);
   4296 		  reg >>= 3;
   4297 		  if (type == 0x8b)
   4298 		    {
   4299 		      /* movq */
   4300 		      if (val == 0x4c)
   4301 			bfd_put_8 (output_bfd, 0x49,
   4302 				   contents + roff - 3);
   4303 		      else if (!ABI_64_P (output_bfd) && val == 0x44)
   4304 			bfd_put_8 (output_bfd, 0x41,
   4305 				   contents + roff - 3);
   4306 		      bfd_put_8 (output_bfd, 0xc7,
   4307 				 contents + roff - 2);
   4308 		      bfd_put_8 (output_bfd, 0xc0 | reg,
   4309 				 contents + roff - 1);
   4310 		    }
   4311 		  else if (reg == 4)
   4312 		    {
   4313 		      /* addq/addl -> addq/addl - addressing with %rsp/%r12
   4314 			 is special  */
   4315 		      if (val == 0x4c)
   4316 			bfd_put_8 (output_bfd, 0x49,
   4317 				   contents + roff - 3);
   4318 		      else if (!ABI_64_P (output_bfd) && val == 0x44)
   4319 			bfd_put_8 (output_bfd, 0x41,
   4320 				   contents + roff - 3);
   4321 		      bfd_put_8 (output_bfd, 0x81,
   4322 				 contents + roff - 2);
   4323 		      bfd_put_8 (output_bfd, 0xc0 | reg,
   4324 				 contents + roff - 1);
   4325 		    }
   4326 		  else
   4327 		    {
   4328 		      /* addq/addl -> leaq/leal */
   4329 		      if (val == 0x4c)
   4330 			bfd_put_8 (output_bfd, 0x4d,
   4331 				   contents + roff - 3);
   4332 		      else if (!ABI_64_P (output_bfd) && val == 0x44)
   4333 			bfd_put_8 (output_bfd, 0x45,
   4334 				   contents + roff - 3);
   4335 		      bfd_put_8 (output_bfd, 0x8d,
   4336 				 contents + roff - 2);
   4337 		      bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
   4338 				 contents + roff - 1);
   4339 		    }
   4340 		  bfd_put_32 (output_bfd,
   4341 			      elf_x86_64_tpoff (info, relocation),
   4342 			      contents + roff);
   4343 		  continue;
   4344 		}
   4345 	      else
   4346 		BFD_ASSERT (FALSE);
   4347 	    }
   4348 
   4349 	  if (htab->elf.sgot == NULL)
   4350 	    abort ();
   4351 
   4352 	  if (h != NULL)
   4353 	    {
   4354 	      off = h->got.offset;
   4355 	      offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
   4356 	    }
   4357 	  else
   4358 	    {
   4359 	      if (local_got_offsets == NULL)
   4360 		abort ();
   4361 
   4362 	      off = local_got_offsets[r_symndx];
   4363 	      offplt = local_tlsdesc_gotents[r_symndx];
   4364 	    }
   4365 
   4366 	  if ((off & 1) != 0)
   4367 	    off &= ~1;
   4368 	  else
   4369 	    {
   4370 	      Elf_Internal_Rela outrel;
   4371 	      int dr_type, indx;
   4372 	      asection *sreloc;
   4373 
   4374 	      if (htab->elf.srelgot == NULL)
   4375 		abort ();
   4376 
   4377 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
   4378 
   4379 	      if (GOT_TLS_GDESC_P (tls_type))
   4380 		{
   4381 		  outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
   4382 		  BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
   4383 			      + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
   4384 		  outrel.r_offset = (htab->elf.sgotplt->output_section->vma
   4385 				     + htab->elf.sgotplt->output_offset
   4386 				     + offplt
   4387 				     + htab->sgotplt_jump_table_size);
   4388 		  sreloc = htab->elf.srelplt;
   4389 		  if (indx == 0)
   4390 		    outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
   4391 		  else
   4392 		    outrel.r_addend = 0;
   4393 		  elf_append_rela (output_bfd, sreloc, &outrel);
   4394 		}
   4395 
   4396 	      sreloc = htab->elf.srelgot;
   4397 
   4398 	      outrel.r_offset = (htab->elf.sgot->output_section->vma
   4399 				 + htab->elf.sgot->output_offset + off);
   4400 
   4401 	      if (GOT_TLS_GD_P (tls_type))
   4402 		dr_type = R_X86_64_DTPMOD64;
   4403 	      else if (GOT_TLS_GDESC_P (tls_type))
   4404 		goto dr_done;
   4405 	      else
   4406 		dr_type = R_X86_64_TPOFF64;
   4407 
   4408 	      bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
   4409 	      outrel.r_addend = 0;
   4410 	      if ((dr_type == R_X86_64_TPOFF64
   4411 		   || dr_type == R_X86_64_TLSDESC) && indx == 0)
   4412 		outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
   4413 	      outrel.r_info = htab->r_info (indx, dr_type);
   4414 
   4415 	      elf_append_rela (output_bfd, sreloc, &outrel);
   4416 
   4417 	      if (GOT_TLS_GD_P (tls_type))
   4418 		{
   4419 		  if (indx == 0)
   4420 		    {
   4421 		      BFD_ASSERT (! unresolved_reloc);
   4422 		      bfd_put_64 (output_bfd,
   4423 				  relocation - elf_x86_64_dtpoff_base (info),
   4424 				  htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
   4425 		    }
   4426 		  else
   4427 		    {
   4428 		      bfd_put_64 (output_bfd, 0,
   4429 				  htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
   4430 		      outrel.r_info = htab->r_info (indx,
   4431 						    R_X86_64_DTPOFF64);
   4432 		      outrel.r_offset += GOT_ENTRY_SIZE;
   4433 		      elf_append_rela (output_bfd, sreloc,
   4434 						&outrel);
   4435 		    }
   4436 		}
   4437 
   4438 	    dr_done:
   4439 	      if (h != NULL)
   4440 		h->got.offset |= 1;
   4441 	      else
   4442 		local_got_offsets[r_symndx] |= 1;
   4443 	    }
   4444 
   4445 	  if (off >= (bfd_vma) -2
   4446 	      && ! GOT_TLS_GDESC_P (tls_type))
   4447 	    abort ();
   4448 	  if (r_type == ELF32_R_TYPE (rel->r_info))
   4449 	    {
   4450 	      if (r_type == R_X86_64_GOTPC32_TLSDESC
   4451 		  || r_type == R_X86_64_TLSDESC_CALL)
   4452 		relocation = htab->elf.sgotplt->output_section->vma
   4453 		  + htab->elf.sgotplt->output_offset
   4454 		  + offplt + htab->sgotplt_jump_table_size;
   4455 	      else
   4456 		relocation = htab->elf.sgot->output_section->vma
   4457 		  + htab->elf.sgot->output_offset + off;
   4458 	      unresolved_reloc = FALSE;
   4459 	    }
   4460 	  else
   4461 	    {
   4462 	      bfd_vma roff = rel->r_offset;
   4463 
   4464 	      if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
   4465 		{
   4466 		  /* GD->IE transition.  For 64bit, change
   4467 		     .byte 0x66; leaq foo@tlsgd(%rip), %rdi
   4468 		     .word 0x6666; rex64; call __tls_get_addr@plt
   4469 		     into:
   4470 		     movq %fs:0, %rax
   4471 		     addq foo@gottpoff(%rip), %rax
   4472 		     For 32bit, change
   4473 		     leaq foo@tlsgd(%rip), %rdi
   4474 		     .word 0x6666; rex64; call __tls_get_addr@plt
   4475 		     into:
   4476 		     movl %fs:0, %eax
   4477 		     addq foo@gottpoff(%rip), %rax
   4478 		     For largepic, change:
   4479 		     leaq foo@tlsgd(%rip), %rdi
   4480 		     movabsq $__tls_get_addr@pltoff, %rax
   4481 		     addq %rbx, %rax
   4482 		     call *%rax
   4483 		     into:
   4484 		     movq %fs:0, %rax
   4485 		     addq foo@gottpoff(%rax), %rax
   4486 		     nopw 0x0(%rax,%rax,1) */
   4487 		  int largepic = 0;
   4488 		  if (ABI_64_P (output_bfd)
   4489 		      && contents[roff + 5] == (bfd_byte) '\xb8')
   4490 		    {
   4491 		      memcpy (contents + roff - 3,
   4492 			      "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
   4493 			      "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
   4494 		      largepic = 1;
   4495 		    }
   4496 		  else if (ABI_64_P (output_bfd))
   4497 		    memcpy (contents + roff - 4,
   4498 			    "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
   4499 			    16);
   4500 		  else
   4501 		    memcpy (contents + roff - 3,
   4502 			    "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
   4503 			    15);
   4504 
   4505 		  relocation = (htab->elf.sgot->output_section->vma
   4506 				+ htab->elf.sgot->output_offset + off
   4507 				- roff
   4508 				- largepic
   4509 				- input_section->output_section->vma
   4510 				- input_section->output_offset
   4511 				- 12);
   4512 		  bfd_put_32 (output_bfd, relocation,
   4513 			      contents + roff + 8 + largepic);
   4514 		  /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64.  */
   4515 		  rel++;
   4516 		  continue;
   4517 		}
   4518 	      else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
   4519 		{
   4520 		  /* GDesc -> IE transition.
   4521 		     It's originally something like:
   4522 		     leaq x@tlsdesc(%rip), %rax
   4523 
   4524 		     Change it to:
   4525 		     movq x@gottpoff(%rip), %rax # before xchg %ax,%ax.  */
   4526 
   4527 		  /* Now modify the instruction as appropriate. To
   4528 		     turn a leaq into a movq in the form we use it, it
   4529 		     suffices to change the second byte from 0x8d to
   4530 		     0x8b.  */
   4531 		  bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
   4532 
   4533 		  bfd_put_32 (output_bfd,
   4534 			      htab->elf.sgot->output_section->vma
   4535 			      + htab->elf.sgot->output_offset + off
   4536 			      - rel->r_offset
   4537 			      - input_section->output_section->vma
   4538 			      - input_section->output_offset
   4539 			      - 4,
   4540 			      contents + roff);
   4541 		  continue;
   4542 		}
   4543 	      else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
   4544 		{
   4545 		  /* GDesc -> IE transition.
   4546 		     It's originally:
   4547 		     call *(%rax)
   4548 
   4549 		     Change it to:
   4550 		     xchg %ax, %ax.  */
   4551 
   4552 		  bfd_put_8 (output_bfd, 0x66, contents + roff);
   4553 		  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
   4554 		  continue;
   4555 		}
   4556 	      else
   4557 		BFD_ASSERT (FALSE);
   4558 	    }
   4559 	  break;
   4560 
   4561 	case R_X86_64_TLSLD:
   4562 	  if (! elf_x86_64_tls_transition (info, input_bfd,
   4563 					   input_section, contents,
   4564 					   symtab_hdr, sym_hashes,
   4565 					   &r_type, GOT_UNKNOWN,
   4566 					   rel, relend, h, r_symndx))
   4567 	    return FALSE;
   4568 
   4569 	  if (r_type != R_X86_64_TLSLD)
   4570 	    {
   4571 	      /* LD->LE transition:
   4572 		 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
   4573 		 For 64bit, we change it into:
   4574 		 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
   4575 		 For 32bit, we change it into:
   4576 		 nopl 0x0(%rax); movl %fs:0, %eax.
   4577 		 For largepic, change:
   4578 		 leaq foo@tlsgd(%rip), %rdi
   4579 		 movabsq $__tls_get_addr@pltoff, %rax
   4580 		 addq %rbx, %rax
   4581 		 call *%rax
   4582 		 into:
   4583 		 data32 data32 data32 nopw %cs:0x0(%rax,%rax,1)
   4584 		 movq %fs:0, %eax */
   4585 
   4586 	      BFD_ASSERT (r_type == R_X86_64_TPOFF32);
   4587 	      if (ABI_64_P (output_bfd)
   4588 		  && contents[rel->r_offset + 5] == (bfd_byte) '\xb8')
   4589 		memcpy (contents + rel->r_offset - 3,
   4590 			"\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
   4591 			"\x64\x48\x8b\x04\x25\0\0\0", 22);
   4592 	      else if (ABI_64_P (output_bfd))
   4593 		memcpy (contents + rel->r_offset - 3,
   4594 			"\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
   4595 	      else
   4596 		memcpy (contents + rel->r_offset - 3,
   4597 			"\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
   4598 	      /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64.  */
   4599 	      rel++;
   4600 	      continue;
   4601 	    }
   4602 
   4603 	  if (htab->elf.sgot == NULL)
   4604 	    abort ();
   4605 
   4606 	  off = htab->tls_ld_got.offset;
   4607 	  if (off & 1)
   4608 	    off &= ~1;
   4609 	  else
   4610 	    {
   4611 	      Elf_Internal_Rela outrel;
   4612 
   4613 	      if (htab->elf.srelgot == NULL)
   4614 		abort ();
   4615 
   4616 	      outrel.r_offset = (htab->elf.sgot->output_section->vma
   4617 				 + htab->elf.sgot->output_offset + off);
   4618 
   4619 	      bfd_put_64 (output_bfd, 0,
   4620 			  htab->elf.sgot->contents + off);
   4621 	      bfd_put_64 (output_bfd, 0,
   4622 			  htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
   4623 	      outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
   4624 	      outrel.r_addend = 0;
   4625 	      elf_append_rela (output_bfd, htab->elf.srelgot,
   4626 					&outrel);
   4627 	      htab->tls_ld_got.offset |= 1;
   4628 	    }
   4629 	  relocation = htab->elf.sgot->output_section->vma
   4630 		       + htab->elf.sgot->output_offset + off;
   4631 	  unresolved_reloc = FALSE;
   4632 	  break;
   4633 
   4634 	case R_X86_64_DTPOFF32:
   4635 	  if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
   4636 	    relocation -= elf_x86_64_dtpoff_base (info);
   4637 	  else
   4638 	    relocation = elf_x86_64_tpoff (info, relocation);
   4639 	  break;
   4640 
   4641 	case R_X86_64_TPOFF32:
   4642 	case R_X86_64_TPOFF64:
   4643 	  BFD_ASSERT (info->executable);
   4644 	  relocation = elf_x86_64_tpoff (info, relocation);
   4645 	  break;
   4646 
   4647 	case R_X86_64_DTPOFF64:
   4648 	  BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
   4649 	  relocation -= elf_x86_64_dtpoff_base (info);
   4650 	  break;
   4651 
   4652 	default:
   4653 	  break;
   4654 	}
   4655 
   4656       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   4657 	 because such sections are not SEC_ALLOC and thus ld.so will
   4658 	 not process them.  */
   4659       if (unresolved_reloc
   4660 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   4661 	       && h->def_dynamic)
   4662 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   4663 				      rel->r_offset) != (bfd_vma) -1)
   4664 	{
   4665 	  (*_bfd_error_handler)
   4666 	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
   4667 	     input_bfd,
   4668 	     input_section,
   4669 	     (long) rel->r_offset,
   4670 	     howto->name,
   4671 	     h->root.root.string);
   4672 	  return FALSE;
   4673 	}
   4674 
   4675 do_relocation:
   4676       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   4677 				    contents, rel->r_offset,
   4678 				    relocation, rel->r_addend);
   4679 
   4680 check_relocation_error:
   4681       if (r != bfd_reloc_ok)
   4682 	{
   4683 	  const char *name;
   4684 
   4685 	  if (h != NULL)
   4686 	    name = h->root.root.string;
   4687 	  else
   4688 	    {
   4689 	      name = bfd_elf_string_from_elf_section (input_bfd,
   4690 						      symtab_hdr->sh_link,
   4691 						      sym->st_name);
   4692 	      if (name == NULL)
   4693 		return FALSE;
   4694 	      if (*name == '\0')
   4695 		name = bfd_section_name (input_bfd, sec);
   4696 	    }
   4697 
   4698 	  if (r == bfd_reloc_overflow)
   4699 	    {
   4700 	      if (! ((*info->callbacks->reloc_overflow)
   4701 		     (info, (h ? &h->root : NULL), name, howto->name,
   4702 		      (bfd_vma) 0, input_bfd, input_section,
   4703 		      rel->r_offset)))
   4704 		return FALSE;
   4705 	    }
   4706 	  else
   4707 	    {
   4708 	      (*_bfd_error_handler)
   4709 		(_("%B(%A+0x%lx): reloc against `%s': error %d"),
   4710 		 input_bfd, input_section,
   4711 		 (long) rel->r_offset, name, (int) r);
   4712 	      return FALSE;
   4713 	    }
   4714 	}
   4715     }
   4716 
   4717   return TRUE;
   4718 }
   4719 
   4720 /* Finish up dynamic symbol handling.  We set the contents of various
   4721    dynamic sections here.  */
   4722 
   4723 static bfd_boolean
   4724 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
   4725 				  struct bfd_link_info *info,
   4726 				  struct elf_link_hash_entry *h,
   4727 				  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   4728 {
   4729   struct elf_x86_64_link_hash_table *htab;
   4730   const struct elf_x86_64_backend_data *abed;
   4731   bfd_boolean use_plt_bnd;
   4732 
   4733   htab = elf_x86_64_hash_table (info);
   4734   if (htab == NULL)
   4735     return FALSE;
   4736 
   4737   /* Use MPX backend data in case of BND relocation.  Use .plt_bnd
   4738      section only if there is .plt section.  */
   4739   use_plt_bnd = htab->elf.splt != NULL && htab->plt_bnd != NULL;
   4740   abed = (use_plt_bnd
   4741 	  ? &elf_x86_64_bnd_arch_bed
   4742 	  : get_elf_x86_64_backend_data (output_bfd));
   4743 
   4744   if (h->plt.offset != (bfd_vma) -1)
   4745     {
   4746       bfd_vma plt_index;
   4747       bfd_vma got_offset, plt_offset, plt_plt_offset, plt_got_offset;
   4748       bfd_vma plt_plt_insn_end, plt_got_insn_size;
   4749       Elf_Internal_Rela rela;
   4750       bfd_byte *loc;
   4751       asection *plt, *gotplt, *relplt, *resolved_plt;
   4752       const struct elf_backend_data *bed;
   4753 
   4754       /* When building a static executable, use .iplt, .igot.plt and
   4755 	 .rela.iplt sections for STT_GNU_IFUNC symbols.  */
   4756       if (htab->elf.splt != NULL)
   4757 	{
   4758 	  plt = htab->elf.splt;
   4759 	  gotplt = htab->elf.sgotplt;
   4760 	  relplt = htab->elf.srelplt;
   4761 	}
   4762       else
   4763 	{
   4764 	  plt = htab->elf.iplt;
   4765 	  gotplt = htab->elf.igotplt;
   4766 	  relplt = htab->elf.irelplt;
   4767 	}
   4768 
   4769       /* This symbol has an entry in the procedure linkage table.  Set
   4770 	 it up.	 */
   4771       if ((h->dynindx == -1
   4772 	   && !((h->forced_local || info->executable)
   4773 		&& h->def_regular
   4774 		&& h->type == STT_GNU_IFUNC))
   4775 	  || plt == NULL
   4776 	  || gotplt == NULL
   4777 	  || relplt == NULL)
   4778 	abort ();
   4779 
   4780       /* Get the index in the procedure linkage table which
   4781 	 corresponds to this symbol.  This is the index of this symbol
   4782 	 in all the symbols for which we are making plt entries.  The
   4783 	 first entry in the procedure linkage table is reserved.
   4784 
   4785 	 Get the offset into the .got table of the entry that
   4786 	 corresponds to this function.	Each .got entry is GOT_ENTRY_SIZE
   4787 	 bytes. The first three are reserved for the dynamic linker.
   4788 
   4789 	 For static executables, we don't reserve anything.  */
   4790 
   4791       if (plt == htab->elf.splt)
   4792 	{
   4793 	  got_offset = h->plt.offset / abed->plt_entry_size - 1;
   4794 	  got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
   4795 	}
   4796       else
   4797 	{
   4798 	  got_offset = h->plt.offset / abed->plt_entry_size;
   4799 	  got_offset = got_offset * GOT_ENTRY_SIZE;
   4800 	}
   4801 
   4802       plt_plt_insn_end = abed->plt_plt_insn_end;
   4803       plt_plt_offset = abed->plt_plt_offset;
   4804       plt_got_insn_size = abed->plt_got_insn_size;
   4805       plt_got_offset = abed->plt_got_offset;
   4806       if (use_plt_bnd)
   4807 	{
   4808 	  /* Use the second PLT with BND relocations.  */
   4809 	  const bfd_byte *plt_entry, *plt2_entry;
   4810 	  struct elf_x86_64_link_hash_entry *eh
   4811 	    = (struct elf_x86_64_link_hash_entry *) h;
   4812 
   4813 	  if (eh->has_bnd_reloc)
   4814 	    {
   4815 	      plt_entry = elf_x86_64_bnd_plt_entry;
   4816 	      plt2_entry = elf_x86_64_bnd_plt2_entry;
   4817 	    }
   4818 	  else
   4819 	    {
   4820 	      plt_entry = elf_x86_64_legacy_plt_entry;
   4821 	      plt2_entry = elf_x86_64_legacy_plt2_entry;
   4822 
   4823 	      /* Subtract 1 since there is no BND prefix.  */
   4824 	      plt_plt_insn_end -= 1;
   4825 	      plt_plt_offset -= 1;
   4826 	      plt_got_insn_size -= 1;
   4827 	      plt_got_offset -= 1;
   4828 	    }
   4829 
   4830 	  BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry)
   4831 		      == sizeof (elf_x86_64_legacy_plt_entry));
   4832 
   4833 	  /* Fill in the entry in the procedure linkage table.  */
   4834 	  memcpy (plt->contents + h->plt.offset,
   4835 		  plt_entry, sizeof (elf_x86_64_legacy_plt_entry));
   4836 	  /* Fill in the entry in the second PLT.  */
   4837 	  memcpy (htab->plt_bnd->contents + eh->plt_bnd.offset,
   4838 		  plt2_entry, sizeof (elf_x86_64_legacy_plt2_entry));
   4839 
   4840 	  resolved_plt = htab->plt_bnd;
   4841 	  plt_offset = eh->plt_bnd.offset;
   4842 	}
   4843       else
   4844 	{
   4845 	  /* Fill in the entry in the procedure linkage table.  */
   4846 	  memcpy (plt->contents + h->plt.offset, abed->plt_entry,
   4847 		  abed->plt_entry_size);
   4848 
   4849 	  resolved_plt = plt;
   4850 	  plt_offset = h->plt.offset;
   4851 	}
   4852 
   4853       /* Insert the relocation positions of the plt section.  */
   4854 
   4855       /* Put offset the PC-relative instruction referring to the GOT entry,
   4856 	 subtracting the size of that instruction.  */
   4857       bfd_put_32 (output_bfd,
   4858 		  (gotplt->output_section->vma
   4859 		   + gotplt->output_offset
   4860 		   + got_offset
   4861 		   - resolved_plt->output_section->vma
   4862 		   - resolved_plt->output_offset
   4863 		   - plt_offset
   4864 		   - plt_got_insn_size),
   4865 		  resolved_plt->contents + plt_offset + plt_got_offset);
   4866 
   4867       /* Fill in the entry in the global offset table, initially this
   4868 	 points to the second part of the PLT entry.  */
   4869       bfd_put_64 (output_bfd, (plt->output_section->vma
   4870 			       + plt->output_offset
   4871 			       + h->plt.offset + abed->plt_lazy_offset),
   4872 		  gotplt->contents + got_offset);
   4873 
   4874       /* Fill in the entry in the .rela.plt section.  */
   4875       rela.r_offset = (gotplt->output_section->vma
   4876 		       + gotplt->output_offset
   4877 		       + got_offset);
   4878       if (h->dynindx == -1
   4879 	  || ((info->executable
   4880 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   4881 	      && h->def_regular
   4882 	      && h->type == STT_GNU_IFUNC))
   4883 	{
   4884 	  /* If an STT_GNU_IFUNC symbol is locally defined, generate
   4885 	     R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
   4886 	  rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
   4887 	  rela.r_addend = (h->root.u.def.value
   4888 			   + h->root.u.def.section->output_section->vma
   4889 			   + h->root.u.def.section->output_offset);
   4890 	  /* R_X86_64_IRELATIVE comes last.  */
   4891 	  plt_index = htab->next_irelative_index--;
   4892 	}
   4893       else
   4894 	{
   4895 	  rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
   4896 	  rela.r_addend = 0;
   4897 	  plt_index = htab->next_jump_slot_index++;
   4898 	}
   4899 
   4900       /* Don't fill PLT entry for static executables.  */
   4901       if (plt == htab->elf.splt)
   4902 	{
   4903 	  /* Put relocation index.  */
   4904 	  bfd_put_32 (output_bfd, plt_index,
   4905 		      plt->contents + h->plt.offset + abed->plt_reloc_offset);
   4906 	  /* Put offset for jmp .PLT0.  */
   4907 	  bfd_put_32 (output_bfd, - (h->plt.offset + plt_plt_insn_end),
   4908 		      plt->contents + h->plt.offset + plt_plt_offset);
   4909 	}
   4910 
   4911       bed = get_elf_backend_data (output_bfd);
   4912       loc = relplt->contents + plt_index * bed->s->sizeof_rela;
   4913       bed->s->swap_reloca_out (output_bfd, &rela, loc);
   4914 
   4915       if (!h->def_regular)
   4916 	{
   4917 	  /* Mark the symbol as undefined, rather than as defined in
   4918 	     the .plt section.  Leave the value if there were any
   4919 	     relocations where pointer equality matters (this is a clue
   4920 	     for the dynamic linker, to make function pointer
   4921 	     comparisons work between an application and shared
   4922 	     library), otherwise set it to zero.  If a function is only
   4923 	     called from a binary, there is no need to slow down
   4924 	     shared libraries because of that.  */
   4925 	  sym->st_shndx = SHN_UNDEF;
   4926 	  if (!h->pointer_equality_needed)
   4927 	    sym->st_value = 0;
   4928 	}
   4929     }
   4930 
   4931   if (h->got.offset != (bfd_vma) -1
   4932       && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
   4933       && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
   4934     {
   4935       Elf_Internal_Rela rela;
   4936 
   4937       /* This symbol has an entry in the global offset table.  Set it
   4938 	 up.  */
   4939       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
   4940 	abort ();
   4941 
   4942       rela.r_offset = (htab->elf.sgot->output_section->vma
   4943 		       + htab->elf.sgot->output_offset
   4944 		       + (h->got.offset &~ (bfd_vma) 1));
   4945 
   4946       /* If this is a static link, or it is a -Bsymbolic link and the
   4947 	 symbol is defined locally or was forced to be local because
   4948 	 of a version file, we just want to emit a RELATIVE reloc.
   4949 	 The entry in the global offset table will already have been
   4950 	 initialized in the relocate_section function.  */
   4951       if (h->def_regular
   4952 	  && h->type == STT_GNU_IFUNC)
   4953 	{
   4954 	  if (info->shared)
   4955 	    {
   4956 	      /* Generate R_X86_64_GLOB_DAT.  */
   4957 	      goto do_glob_dat;
   4958 	    }
   4959 	  else
   4960 	    {
   4961 	      asection *plt;
   4962 
   4963 	      if (!h->pointer_equality_needed)
   4964 		abort ();
   4965 
   4966 	      /* For non-shared object, we can't use .got.plt, which
   4967 		 contains the real function addres if we need pointer
   4968 		 equality.  We load the GOT entry with the PLT entry.  */
   4969 	      plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
   4970 	      bfd_put_64 (output_bfd, (plt->output_section->vma
   4971 				       + plt->output_offset
   4972 				       + h->plt.offset),
   4973 			  htab->elf.sgot->contents + h->got.offset);
   4974 	      return TRUE;
   4975 	    }
   4976 	}
   4977       else if (info->shared
   4978 	       && SYMBOL_REFERENCES_LOCAL (info, h))
   4979 	{
   4980 	  if (!h->def_regular)
   4981 	    return FALSE;
   4982 	  BFD_ASSERT((h->got.offset & 1) != 0);
   4983 	  rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
   4984 	  rela.r_addend = (h->root.u.def.value
   4985 			   + h->root.u.def.section->output_section->vma
   4986 			   + h->root.u.def.section->output_offset);
   4987 	}
   4988       else
   4989 	{
   4990 	  BFD_ASSERT((h->got.offset & 1) == 0);
   4991 do_glob_dat:
   4992 	  bfd_put_64 (output_bfd, (bfd_vma) 0,
   4993 		      htab->elf.sgot->contents + h->got.offset);
   4994 	  rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
   4995 	  rela.r_addend = 0;
   4996 	}
   4997 
   4998       elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
   4999     }
   5000 
   5001   if (h->needs_copy)
   5002     {
   5003       Elf_Internal_Rela rela;
   5004 
   5005       /* This symbol needs a copy reloc.  Set it up.  */
   5006 
   5007       if (h->dynindx == -1
   5008 	  || (h->root.type != bfd_link_hash_defined
   5009 	      && h->root.type != bfd_link_hash_defweak)
   5010 	  || htab->srelbss == NULL)
   5011 	abort ();
   5012 
   5013       rela.r_offset = (h->root.u.def.value
   5014 		       + h->root.u.def.section->output_section->vma
   5015 		       + h->root.u.def.section->output_offset);
   5016       rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
   5017       rela.r_addend = 0;
   5018       elf_append_rela (output_bfd, htab->srelbss, &rela);
   5019     }
   5020 
   5021   return TRUE;
   5022 }
   5023 
   5024 /* Finish up local dynamic symbol handling.  We set the contents of
   5025    various dynamic sections here.  */
   5026 
   5027 static bfd_boolean
   5028 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
   5029 {
   5030   struct elf_link_hash_entry *h
   5031     = (struct elf_link_hash_entry *) *slot;
   5032   struct bfd_link_info *info
   5033     = (struct bfd_link_info *) inf;
   5034 
   5035   return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
   5036 					     info, h, NULL);
   5037 }
   5038 
   5039 /* Used to decide how to sort relocs in an optimal manner for the
   5040    dynamic linker, before writing them out.  */
   5041 
   5042 static enum elf_reloc_type_class
   5043 elf_x86_64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5044 			     const asection *rel_sec ATTRIBUTE_UNUSED,
   5045 			     const Elf_Internal_Rela *rela)
   5046 {
   5047   switch ((int) ELF32_R_TYPE (rela->r_info))
   5048     {
   5049     case R_X86_64_RELATIVE:
   5050     case R_X86_64_RELATIVE64:
   5051       return reloc_class_relative;
   5052     case R_X86_64_JUMP_SLOT:
   5053       return reloc_class_plt;
   5054     case R_X86_64_COPY:
   5055       return reloc_class_copy;
   5056     default:
   5057       return reloc_class_normal;
   5058     }
   5059 }
   5060 
   5061 /* Finish up the dynamic sections.  */
   5062 
   5063 static bfd_boolean
   5064 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
   5065 				    struct bfd_link_info *info)
   5066 {
   5067   struct elf_x86_64_link_hash_table *htab;
   5068   bfd *dynobj;
   5069   asection *sdyn;
   5070   const struct elf_x86_64_backend_data *abed;
   5071 
   5072   htab = elf_x86_64_hash_table (info);
   5073   if (htab == NULL)
   5074     return FALSE;
   5075 
   5076   /* Use MPX backend data in case of BND relocation.  Use .plt_bnd
   5077      section only if there is .plt section.  */
   5078   abed = (htab->elf.splt != NULL && htab->plt_bnd != NULL
   5079 	  ? &elf_x86_64_bnd_arch_bed
   5080 	  : get_elf_x86_64_backend_data (output_bfd));
   5081 
   5082   dynobj = htab->elf.dynobj;
   5083   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   5084 
   5085   if (htab->elf.dynamic_sections_created)
   5086     {
   5087       bfd_byte *dyncon, *dynconend;
   5088       const struct elf_backend_data *bed;
   5089       bfd_size_type sizeof_dyn;
   5090 
   5091       if (sdyn == NULL || htab->elf.sgot == NULL)
   5092 	abort ();
   5093 
   5094       bed = get_elf_backend_data (dynobj);
   5095       sizeof_dyn = bed->s->sizeof_dyn;
   5096       dyncon = sdyn->contents;
   5097       dynconend = sdyn->contents + sdyn->size;
   5098       for (; dyncon < dynconend; dyncon += sizeof_dyn)
   5099 	{
   5100 	  Elf_Internal_Dyn dyn;
   5101 	  asection *s;
   5102 
   5103 	  (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
   5104 
   5105 	  switch (dyn.d_tag)
   5106 	    {
   5107 	    default:
   5108 	      continue;
   5109 
   5110 	    case DT_PLTGOT:
   5111 	      s = htab->elf.sgotplt;
   5112 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   5113 	      break;
   5114 
   5115 	    case DT_JMPREL:
   5116 	      dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
   5117 	      break;
   5118 
   5119 	    case DT_PLTRELSZ:
   5120 	      s = htab->elf.srelplt->output_section;
   5121 	      dyn.d_un.d_val = s->size;
   5122 	      break;
   5123 
   5124 	    case DT_RELASZ:
   5125 	      /* The procedure linkage table relocs (DT_JMPREL) should
   5126 		 not be included in the overall relocs (DT_RELA).
   5127 		 Therefore, we override the DT_RELASZ entry here to
   5128 		 make it not include the JMPREL relocs.  Since the
   5129 		 linker script arranges for .rela.plt to follow all
   5130 		 other relocation sections, we don't have to worry
   5131 		 about changing the DT_RELA entry.  */
   5132 	      if (htab->elf.srelplt != NULL)
   5133 		{
   5134 		  s = htab->elf.srelplt->output_section;
   5135 		  dyn.d_un.d_val -= s->size;
   5136 		}
   5137 	      break;
   5138 
   5139 	    case DT_TLSDESC_PLT:
   5140 	      s = htab->elf.splt;
   5141 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
   5142 		+ htab->tlsdesc_plt;
   5143 	      break;
   5144 
   5145 	    case DT_TLSDESC_GOT:
   5146 	      s = htab->elf.sgot;
   5147 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
   5148 		+ htab->tlsdesc_got;
   5149 	      break;
   5150 	    }
   5151 
   5152 	  (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
   5153 	}
   5154 
   5155       /* Fill in the special first entry in the procedure linkage table.  */
   5156       if (htab->elf.splt && htab->elf.splt->size > 0)
   5157 	{
   5158 	  /* Fill in the first entry in the procedure linkage table.  */
   5159 	  memcpy (htab->elf.splt->contents,
   5160 		  abed->plt0_entry, abed->plt_entry_size);
   5161 	  /* Add offset for pushq GOT+8(%rip), since the instruction
   5162 	     uses 6 bytes subtract this value.  */
   5163 	  bfd_put_32 (output_bfd,
   5164 		      (htab->elf.sgotplt->output_section->vma
   5165 		       + htab->elf.sgotplt->output_offset
   5166 		       + 8
   5167 		       - htab->elf.splt->output_section->vma
   5168 		       - htab->elf.splt->output_offset
   5169 		       - 6),
   5170 		      htab->elf.splt->contents + abed->plt0_got1_offset);
   5171 	  /* Add offset for the PC-relative instruction accessing GOT+16,
   5172 	     subtracting the offset to the end of that instruction.  */
   5173 	  bfd_put_32 (output_bfd,
   5174 		      (htab->elf.sgotplt->output_section->vma
   5175 		       + htab->elf.sgotplt->output_offset
   5176 		       + 16
   5177 		       - htab->elf.splt->output_section->vma
   5178 		       - htab->elf.splt->output_offset
   5179 		       - abed->plt0_got2_insn_end),
   5180 		      htab->elf.splt->contents + abed->plt0_got2_offset);
   5181 
   5182 	  elf_section_data (htab->elf.splt->output_section)
   5183 	    ->this_hdr.sh_entsize = abed->plt_entry_size;
   5184 
   5185 	  if (htab->tlsdesc_plt)
   5186 	    {
   5187 	      bfd_put_64 (output_bfd, (bfd_vma) 0,
   5188 			  htab->elf.sgot->contents + htab->tlsdesc_got);
   5189 
   5190 	      memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
   5191 		      abed->plt0_entry, abed->plt_entry_size);
   5192 
   5193 	      /* Add offset for pushq GOT+8(%rip), since the
   5194 		 instruction uses 6 bytes subtract this value.  */
   5195 	      bfd_put_32 (output_bfd,
   5196 			  (htab->elf.sgotplt->output_section->vma
   5197 			   + htab->elf.sgotplt->output_offset
   5198 			   + 8
   5199 			   - htab->elf.splt->output_section->vma
   5200 			   - htab->elf.splt->output_offset
   5201 			   - htab->tlsdesc_plt
   5202 			   - 6),
   5203 			  htab->elf.splt->contents
   5204 			  + htab->tlsdesc_plt + abed->plt0_got1_offset);
   5205 	  /* Add offset for the PC-relative instruction accessing GOT+TDG,
   5206 	     where TGD stands for htab->tlsdesc_got, subtracting the offset
   5207 	     to the end of that instruction.  */
   5208 	      bfd_put_32 (output_bfd,
   5209 			  (htab->elf.sgot->output_section->vma
   5210 			   + htab->elf.sgot->output_offset
   5211 			   + htab->tlsdesc_got
   5212 			   - htab->elf.splt->output_section->vma
   5213 			   - htab->elf.splt->output_offset
   5214 			   - htab->tlsdesc_plt
   5215 			   - abed->plt0_got2_insn_end),
   5216 			  htab->elf.splt->contents
   5217 			  + htab->tlsdesc_plt + abed->plt0_got2_offset);
   5218 	    }
   5219 	}
   5220     }
   5221 
   5222   if (htab->plt_bnd != NULL)
   5223     elf_section_data (htab->plt_bnd->output_section)
   5224       ->this_hdr.sh_entsize = sizeof (elf_x86_64_bnd_plt2_entry);
   5225 
   5226   if (htab->elf.sgotplt)
   5227     {
   5228       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
   5229 	{
   5230 	  (*_bfd_error_handler)
   5231 	    (_("discarded output section: `%A'"), htab->elf.sgotplt);
   5232 	  return FALSE;
   5233 	}
   5234 
   5235       /* Fill in the first three entries in the global offset table.  */
   5236       if (htab->elf.sgotplt->size > 0)
   5237 	{
   5238 	  /* Set the first entry in the global offset table to the address of
   5239 	     the dynamic section.  */
   5240 	  if (sdyn == NULL)
   5241 	    bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
   5242 	  else
   5243 	    bfd_put_64 (output_bfd,
   5244 			sdyn->output_section->vma + sdyn->output_offset,
   5245 			htab->elf.sgotplt->contents);
   5246 	  /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
   5247 	  bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
   5248 	  bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
   5249 	}
   5250 
   5251       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
   5252 	GOT_ENTRY_SIZE;
   5253     }
   5254 
   5255   /* Adjust .eh_frame for .plt section.  */
   5256   if (htab->plt_eh_frame != NULL
   5257       && htab->plt_eh_frame->contents != NULL)
   5258     {
   5259       if (htab->elf.splt != NULL
   5260 	  && htab->elf.splt->size != 0
   5261 	  && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
   5262 	  && htab->elf.splt->output_section != NULL
   5263 	  && htab->plt_eh_frame->output_section != NULL)
   5264 	{
   5265 	  bfd_vma plt_start = htab->elf.splt->output_section->vma;
   5266 	  bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
   5267 				   + htab->plt_eh_frame->output_offset
   5268 				   + PLT_FDE_START_OFFSET;
   5269 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
   5270 			     htab->plt_eh_frame->contents
   5271 			     + PLT_FDE_START_OFFSET);
   5272 	}
   5273       if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   5274 	{
   5275 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
   5276 						 htab->plt_eh_frame,
   5277 						 htab->plt_eh_frame->contents))
   5278 	    return FALSE;
   5279 	}
   5280     }
   5281 
   5282   if (htab->elf.sgot && htab->elf.sgot->size > 0)
   5283     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
   5284       = GOT_ENTRY_SIZE;
   5285 
   5286   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
   5287   htab_traverse (htab->loc_hash_table,
   5288 		 elf_x86_64_finish_local_dynamic_symbol,
   5289 		 info);
   5290 
   5291   return TRUE;
   5292 }
   5293 
   5294 /* Return address in section PLT for the Ith GOTPLT relocation, for
   5295    relocation REL or (bfd_vma) -1 if it should not be included.  */
   5296 
   5297 static bfd_vma
   5298 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
   5299 			const arelent *rel)
   5300 {
   5301   bfd *abfd;
   5302   const struct elf_x86_64_backend_data *bed;
   5303   bfd_vma plt_offset;
   5304 
   5305   /* Only match R_X86_64_JUMP_SLOT and R_X86_64_IRELATIVE.  */
   5306   if (rel->howto->type != R_X86_64_JUMP_SLOT
   5307       && rel->howto->type != R_X86_64_IRELATIVE)
   5308     return (bfd_vma) -1;
   5309 
   5310   abfd = plt->owner;
   5311   bed = get_elf_x86_64_backend_data (abfd);
   5312   plt_offset = bed->plt_entry_size;
   5313 
   5314   if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_GNU)
   5315     return plt->vma + (i + 1) * plt_offset;
   5316 
   5317   while (plt_offset < plt->size)
   5318     {
   5319       bfd_vma reloc_index;
   5320       bfd_byte reloc_index_raw[4];
   5321 
   5322       if (!bfd_get_section_contents (abfd, (asection *) plt,
   5323 				     reloc_index_raw,
   5324 				     plt_offset + bed->plt_reloc_offset,
   5325 				     sizeof (reloc_index_raw)))
   5326 	return (bfd_vma) -1;
   5327 
   5328       reloc_index = H_GET_32 (abfd, reloc_index_raw);
   5329       if (reloc_index == i)
   5330 	return plt->vma + plt_offset;
   5331       plt_offset += bed->plt_entry_size;
   5332     }
   5333 
   5334   abort ();
   5335 }
   5336 
   5337 /* Return offset in .plt.bnd section for the Ith GOTPLT relocation with
   5338    PLT section, or (bfd_vma) -1 if it should not be included.  */
   5339 
   5340 static bfd_vma
   5341 elf_x86_64_plt_sym_val_offset_plt_bnd (bfd_vma i, const asection *plt)
   5342 {
   5343   const struct elf_x86_64_backend_data *bed = &elf_x86_64_bnd_arch_bed;
   5344   bfd *abfd = plt->owner;
   5345   bfd_vma plt_offset = bed->plt_entry_size;
   5346 
   5347   if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_GNU)
   5348     return i * sizeof (elf_x86_64_legacy_plt2_entry);
   5349 
   5350   while (plt_offset < plt->size)
   5351     {
   5352       bfd_vma reloc_index;
   5353       bfd_byte reloc_index_raw[4];
   5354 
   5355       if (!bfd_get_section_contents (abfd, (asection *) plt,
   5356 				     reloc_index_raw,
   5357 				     plt_offset + bed->plt_reloc_offset,
   5358 				     sizeof (reloc_index_raw)))
   5359 	return (bfd_vma) -1;
   5360 
   5361       reloc_index = H_GET_32 (abfd, reloc_index_raw);
   5362       if (reloc_index == i)
   5363 	{
   5364 	  /* This is the index in .plt section.  */
   5365 	  long plt_index = plt_offset / bed->plt_entry_size;
   5366 	  /* Return the offset in .plt.bnd section.  */
   5367 	  return (plt_index - 1) * sizeof (elf_x86_64_legacy_plt2_entry);
   5368 	}
   5369       plt_offset += bed->plt_entry_size;
   5370     }
   5371 
   5372   abort ();
   5373 }
   5374 
   5375 /* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
   5376    support.  */
   5377 
   5378 static long
   5379 elf_x86_64_get_synthetic_symtab (bfd *abfd,
   5380 				 long symcount,
   5381 				 asymbol **syms,
   5382 				 long dynsymcount,
   5383 				 asymbol **dynsyms,
   5384 				 asymbol **ret)
   5385 {
   5386   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5387   asection *relplt;
   5388   asymbol *s;
   5389   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   5390   arelent *p;
   5391   long count, i, n;
   5392   size_t size;
   5393   Elf_Internal_Shdr *hdr;
   5394   char *names;
   5395   asection *plt, *plt_push;
   5396 
   5397   plt_push = bfd_get_section_by_name (abfd, ".plt");
   5398   if (plt_push == NULL)
   5399     return 0;
   5400 
   5401   plt = bfd_get_section_by_name (abfd, ".plt.bnd");
   5402   /* Use the generic ELF version if there is no .plt.bnd section.  */
   5403   if (plt == NULL)
   5404     return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
   5405 					  dynsymcount, dynsyms, ret);
   5406 
   5407   *ret = NULL;
   5408 
   5409   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   5410     return 0;
   5411 
   5412   if (dynsymcount <= 0)
   5413     return 0;
   5414 
   5415   relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   5416   if (relplt == NULL)
   5417     return 0;
   5418 
   5419   hdr = &elf_section_data (relplt)->this_hdr;
   5420   if (hdr->sh_link != elf_dynsymtab (abfd)
   5421       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   5422     return 0;
   5423 
   5424   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   5425   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   5426     return -1;
   5427 
   5428   count = relplt->size / hdr->sh_entsize;
   5429   size = count * sizeof (asymbol);
   5430   p = relplt->relocation;
   5431   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   5432     {
   5433       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   5434       if (p->addend != 0)
   5435 	size += sizeof ("+0x") - 1 + 8 + 8;
   5436     }
   5437 
   5438   s = *ret = (asymbol *) bfd_malloc (size);
   5439   if (s == NULL)
   5440     return -1;
   5441 
   5442   names = (char *) (s + count);
   5443   p = relplt->relocation;
   5444   n = 0;
   5445   for (i = 0; i < count; i++, p++)
   5446     {
   5447       bfd_vma offset;
   5448       size_t len;
   5449 
   5450       if (p->howto->type != R_X86_64_JUMP_SLOT
   5451 	  && p->howto->type != R_X86_64_IRELATIVE)
   5452 	continue;
   5453 
   5454       offset = elf_x86_64_plt_sym_val_offset_plt_bnd (i, plt_push);
   5455 
   5456       *s = **p->sym_ptr_ptr;
   5457       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   5458 	 we are defining a symbol, ensure one of them is set.  */
   5459       if ((s->flags & BSF_LOCAL) == 0)
   5460 	s->flags |= BSF_GLOBAL;
   5461       s->flags |= BSF_SYNTHETIC;
   5462       s->section = plt;
   5463       s->value = offset;
   5464       s->name = names;
   5465       s->udata.p = NULL;
   5466       len = strlen ((*p->sym_ptr_ptr)->name);
   5467       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   5468       names += len;
   5469       if (p->addend != 0)
   5470 	{
   5471 	  char buf[30], *a;
   5472 
   5473 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   5474 	  names += sizeof ("+0x") - 1;
   5475 	  bfd_sprintf_vma (abfd, buf, p->addend);
   5476 	  for (a = buf; *a == '0'; ++a)
   5477 	    ;
   5478 	  len = strlen (a);
   5479 	  memcpy (names, a, len);
   5480 	  names += len;
   5481 	}
   5482       memcpy (names, "@plt", sizeof ("@plt"));
   5483       names += sizeof ("@plt");
   5484       ++s, ++n;
   5485     }
   5486 
   5487   return n;
   5488 }
   5489 
   5490 /* Handle an x86-64 specific section when reading an object file.  This
   5491    is called when elfcode.h finds a section with an unknown type.  */
   5492 
   5493 static bfd_boolean
   5494 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
   5495 			      const char *name, int shindex)
   5496 {
   5497   if (hdr->sh_type != SHT_X86_64_UNWIND)
   5498     return FALSE;
   5499 
   5500   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   5501     return FALSE;
   5502 
   5503   return TRUE;
   5504 }
   5505 
   5506 /* Hook called by the linker routine which adds symbols from an object
   5507    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
   5508    of .bss.  */
   5509 
   5510 static bfd_boolean
   5511 elf_x86_64_add_symbol_hook (bfd *abfd,
   5512 			    struct bfd_link_info *info,
   5513 			    Elf_Internal_Sym *sym,
   5514 			    const char **namep ATTRIBUTE_UNUSED,
   5515 			    flagword *flagsp ATTRIBUTE_UNUSED,
   5516 			    asection **secp,
   5517 			    bfd_vma *valp)
   5518 {
   5519   asection *lcomm;
   5520 
   5521   switch (sym->st_shndx)
   5522     {
   5523     case SHN_X86_64_LCOMMON:
   5524       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
   5525       if (lcomm == NULL)
   5526 	{
   5527 	  lcomm = bfd_make_section_with_flags (abfd,
   5528 					       "LARGE_COMMON",
   5529 					       (SEC_ALLOC
   5530 						| SEC_IS_COMMON
   5531 						| SEC_LINKER_CREATED));
   5532 	  if (lcomm == NULL)
   5533 	    return FALSE;
   5534 	  elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
   5535 	}
   5536       *secp = lcomm;
   5537       *valp = sym->st_size;
   5538       return TRUE;
   5539     }
   5540 
   5541   if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
   5542        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
   5543       && (abfd->flags & DYNAMIC) == 0
   5544       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
   5545     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   5546 
   5547   return TRUE;
   5548 }
   5549 
   5550 
   5551 /* Given a BFD section, try to locate the corresponding ELF section
   5552    index.  */
   5553 
   5554 static bfd_boolean
   5555 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   5556 					 asection *sec, int *index_return)
   5557 {
   5558   if (sec == &_bfd_elf_large_com_section)
   5559     {
   5560       *index_return = SHN_X86_64_LCOMMON;
   5561       return TRUE;
   5562     }
   5563   return FALSE;
   5564 }
   5565 
   5566 /* Process a symbol.  */
   5567 
   5568 static void
   5569 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
   5570 			      asymbol *asym)
   5571 {
   5572   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
   5573 
   5574   switch (elfsym->internal_elf_sym.st_shndx)
   5575     {
   5576     case SHN_X86_64_LCOMMON:
   5577       asym->section = &_bfd_elf_large_com_section;
   5578       asym->value = elfsym->internal_elf_sym.st_size;
   5579       /* Common symbol doesn't set BSF_GLOBAL.  */
   5580       asym->flags &= ~BSF_GLOBAL;
   5581       break;
   5582     }
   5583 }
   5584 
   5585 static bfd_boolean
   5586 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
   5587 {
   5588   return (sym->st_shndx == SHN_COMMON
   5589 	  || sym->st_shndx == SHN_X86_64_LCOMMON);
   5590 }
   5591 
   5592 static unsigned int
   5593 elf_x86_64_common_section_index (asection *sec)
   5594 {
   5595   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
   5596     return SHN_COMMON;
   5597   else
   5598     return SHN_X86_64_LCOMMON;
   5599 }
   5600 
   5601 static asection *
   5602 elf_x86_64_common_section (asection *sec)
   5603 {
   5604   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
   5605     return bfd_com_section_ptr;
   5606   else
   5607     return &_bfd_elf_large_com_section;
   5608 }
   5609 
   5610 static bfd_boolean
   5611 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
   5612 			 const Elf_Internal_Sym *sym,
   5613 			 asection **psec,
   5614 			 bfd_boolean newdef,
   5615 			 bfd_boolean olddef,
   5616 			 bfd *oldbfd,
   5617 			 const asection *oldsec)
   5618 {
   5619   /* A normal common symbol and a large common symbol result in a
   5620      normal common symbol.  We turn the large common symbol into a
   5621      normal one.  */
   5622   if (!olddef
   5623       && h->root.type == bfd_link_hash_common
   5624       && !newdef
   5625       && bfd_is_com_section (*psec)
   5626       && oldsec != *psec)
   5627     {
   5628       if (sym->st_shndx == SHN_COMMON
   5629 	  && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
   5630 	{
   5631 	  h->root.u.c.p->section
   5632 	    = bfd_make_section_old_way (oldbfd, "COMMON");
   5633 	  h->root.u.c.p->section->flags = SEC_ALLOC;
   5634 	}
   5635       else if (sym->st_shndx == SHN_X86_64_LCOMMON
   5636 	       && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
   5637 	*psec = bfd_com_section_ptr;
   5638     }
   5639 
   5640   return TRUE;
   5641 }
   5642 
   5643 static int
   5644 elf_x86_64_additional_program_headers (bfd *abfd,
   5645 				       struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5646 {
   5647   asection *s;
   5648   int count = 0;
   5649 
   5650   /* Check to see if we need a large readonly segment.  */
   5651   s = bfd_get_section_by_name (abfd, ".lrodata");
   5652   if (s && (s->flags & SEC_LOAD))
   5653     count++;
   5654 
   5655   /* Check to see if we need a large data segment.  Since .lbss sections
   5656      is placed right after the .bss section, there should be no need for
   5657      a large data segment just because of .lbss.  */
   5658   s = bfd_get_section_by_name (abfd, ".ldata");
   5659   if (s && (s->flags & SEC_LOAD))
   5660     count++;
   5661 
   5662   return count;
   5663 }
   5664 
   5665 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   5666 
   5667 static bfd_boolean
   5668 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
   5669 {
   5670   if (h->plt.offset != (bfd_vma) -1
   5671       && !h->def_regular
   5672       && !h->pointer_equality_needed)
   5673     return FALSE;
   5674 
   5675   return _bfd_elf_hash_symbol (h);
   5676 }
   5677 
   5678 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
   5679 
   5680 static bfd_boolean
   5681 elf_x86_64_relocs_compatible (const bfd_target *input,
   5682 			      const bfd_target *output)
   5683 {
   5684   return ((xvec_get_elf_backend_data (input)->s->elfclass
   5685 	   == xvec_get_elf_backend_data (output)->s->elfclass)
   5686 	  && _bfd_elf_relocs_compatible (input, output));
   5687 }
   5688 
   5689 static const struct bfd_elf_special_section
   5690   elf_x86_64_special_sections[]=
   5691 {
   5692   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
   5693   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
   5694   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
   5695   { STRING_COMMA_LEN (".lbss"),	           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
   5696   { STRING_COMMA_LEN (".ldata"),	   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
   5697   { STRING_COMMA_LEN (".lrodata"),	   -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
   5698   { NULL,	                0,          0, 0,            0 }
   5699 };
   5700 
   5701 #define TARGET_LITTLE_SYM		    x86_64_elf64_vec
   5702 #define TARGET_LITTLE_NAME		    "elf64-x86-64"
   5703 #define ELF_ARCH			    bfd_arch_i386
   5704 #define ELF_TARGET_ID			    X86_64_ELF_DATA
   5705 #define ELF_MACHINE_CODE		    EM_X86_64
   5706 #define ELF_MAXPAGESIZE			    0x200000
   5707 #define ELF_MINPAGESIZE			    0x1000
   5708 #define ELF_COMMONPAGESIZE		    0x1000
   5709 
   5710 #define elf_backend_can_gc_sections	    1
   5711 #define elf_backend_can_refcount	    1
   5712 #define elf_backend_want_got_plt	    1
   5713 #define elf_backend_plt_readonly	    1
   5714 #define elf_backend_want_plt_sym	    0
   5715 #define elf_backend_got_header_size	    (GOT_ENTRY_SIZE*3)
   5716 #define elf_backend_rela_normal		    1
   5717 #define elf_backend_plt_alignment           4
   5718 
   5719 #define elf_info_to_howto		    elf_x86_64_info_to_howto
   5720 
   5721 #define bfd_elf64_bfd_link_hash_table_create \
   5722   elf_x86_64_link_hash_table_create
   5723 #define bfd_elf64_bfd_reloc_type_lookup	    elf_x86_64_reloc_type_lookup
   5724 #define bfd_elf64_bfd_reloc_name_lookup \
   5725   elf_x86_64_reloc_name_lookup
   5726 
   5727 #define elf_backend_adjust_dynamic_symbol   elf_x86_64_adjust_dynamic_symbol
   5728 #define elf_backend_relocs_compatible	    elf_x86_64_relocs_compatible
   5729 #define elf_backend_check_relocs	    elf_x86_64_check_relocs
   5730 #define elf_backend_copy_indirect_symbol    elf_x86_64_copy_indirect_symbol
   5731 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
   5732 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
   5733 #define elf_backend_finish_dynamic_symbol   elf_x86_64_finish_dynamic_symbol
   5734 #define elf_backend_gc_mark_hook	    elf_x86_64_gc_mark_hook
   5735 #define elf_backend_gc_sweep_hook	    elf_x86_64_gc_sweep_hook
   5736 #define elf_backend_grok_prstatus	    elf_x86_64_grok_prstatus
   5737 #define elf_backend_grok_psinfo		    elf_x86_64_grok_psinfo
   5738 #ifdef CORE_HEADER
   5739 #define elf_backend_write_core_note	    elf_x86_64_write_core_note
   5740 #endif
   5741 #define elf_backend_reloc_type_class	    elf_x86_64_reloc_type_class
   5742 #define elf_backend_relocate_section	    elf_x86_64_relocate_section
   5743 #define elf_backend_size_dynamic_sections   elf_x86_64_size_dynamic_sections
   5744 #define elf_backend_always_size_sections    elf_x86_64_always_size_sections
   5745 #define elf_backend_init_index_section	    _bfd_elf_init_1_index_section
   5746 #define elf_backend_plt_sym_val		    elf_x86_64_plt_sym_val
   5747 #define elf_backend_object_p		    elf64_x86_64_elf_object_p
   5748 #define bfd_elf64_mkobject		    elf_x86_64_mkobject
   5749 #define bfd_elf64_get_synthetic_symtab	    elf_x86_64_get_synthetic_symtab
   5750 
   5751 #define elf_backend_section_from_shdr \
   5752 	elf_x86_64_section_from_shdr
   5753 
   5754 #define elf_backend_section_from_bfd_section \
   5755   elf_x86_64_elf_section_from_bfd_section
   5756 #define elf_backend_add_symbol_hook \
   5757   elf_x86_64_add_symbol_hook
   5758 #define elf_backend_symbol_processing \
   5759   elf_x86_64_symbol_processing
   5760 #define elf_backend_common_section_index \
   5761   elf_x86_64_common_section_index
   5762 #define elf_backend_common_section \
   5763   elf_x86_64_common_section
   5764 #define elf_backend_common_definition \
   5765   elf_x86_64_common_definition
   5766 #define elf_backend_merge_symbol \
   5767   elf_x86_64_merge_symbol
   5768 #define elf_backend_special_sections \
   5769   elf_x86_64_special_sections
   5770 #define elf_backend_additional_program_headers \
   5771   elf_x86_64_additional_program_headers
   5772 #define elf_backend_hash_symbol \
   5773   elf_x86_64_hash_symbol
   5774 
   5775 #include "elf64-target.h"
   5776 
   5777 /* FreeBSD support.  */
   5778 
   5779 #undef  TARGET_LITTLE_SYM
   5780 #define TARGET_LITTLE_SYM		    x86_64_elf64_fbsd_vec
   5781 #undef  TARGET_LITTLE_NAME
   5782 #define TARGET_LITTLE_NAME		    "elf64-x86-64-freebsd"
   5783 
   5784 #undef	ELF_OSABI
   5785 #define	ELF_OSABI			    ELFOSABI_FREEBSD
   5786 
   5787 #undef  elf64_bed
   5788 #define elf64_bed elf64_x86_64_fbsd_bed
   5789 
   5790 #include "elf64-target.h"
   5791 
   5792 /* Solaris 2 support.  */
   5793 
   5794 #undef  TARGET_LITTLE_SYM
   5795 #define TARGET_LITTLE_SYM		    x86_64_elf64_sol2_vec
   5796 #undef  TARGET_LITTLE_NAME
   5797 #define TARGET_LITTLE_NAME		    "elf64-x86-64-sol2"
   5798 
   5799 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
   5800    objects won't be recognized.  */
   5801 #undef ELF_OSABI
   5802 
   5803 #undef  elf64_bed
   5804 #define elf64_bed			    elf64_x86_64_sol2_bed
   5805 
   5806 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
   5807    boundary.  */
   5808 #undef elf_backend_static_tls_alignment
   5809 #define elf_backend_static_tls_alignment    16
   5810 
   5811 /* The Solaris 2 ABI requires a plt symbol on all platforms.
   5812 
   5813    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
   5814    File, p.63.  */
   5815 #undef elf_backend_want_plt_sym
   5816 #define elf_backend_want_plt_sym	    1
   5817 
   5818 #include "elf64-target.h"
   5819 
   5820 #undef bfd_elf64_get_synthetic_symtab
   5821 
   5822 /* Native Client support.  */
   5823 
   5824 static bfd_boolean
   5825 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
   5826 {
   5827   /* Set the right machine number for a NaCl x86-64 ELF64 file.  */
   5828   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
   5829   return TRUE;
   5830 }
   5831 
   5832 #undef	TARGET_LITTLE_SYM
   5833 #define	TARGET_LITTLE_SYM		x86_64_elf64_nacl_vec
   5834 #undef	TARGET_LITTLE_NAME
   5835 #define	TARGET_LITTLE_NAME		"elf64-x86-64-nacl"
   5836 #undef	elf64_bed
   5837 #define	elf64_bed			elf64_x86_64_nacl_bed
   5838 
   5839 #undef	ELF_MAXPAGESIZE
   5840 #undef	ELF_MINPAGESIZE
   5841 #undef	ELF_COMMONPAGESIZE
   5842 #define ELF_MAXPAGESIZE			0x10000
   5843 #define ELF_MINPAGESIZE			0x10000
   5844 #define ELF_COMMONPAGESIZE		0x10000
   5845 
   5846 /* Restore defaults.  */
   5847 #undef	ELF_OSABI
   5848 #undef	elf_backend_static_tls_alignment
   5849 #undef	elf_backend_want_plt_sym
   5850 #define elf_backend_want_plt_sym	0
   5851 
   5852 /* NaCl uses substantially different PLT entries for the same effects.  */
   5853 
   5854 #undef	elf_backend_plt_alignment
   5855 #define elf_backend_plt_alignment	5
   5856 #define NACL_PLT_ENTRY_SIZE		64
   5857 #define	NACLMASK			0xe0 /* 32-byte alignment mask.  */
   5858 
   5859 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
   5860   {
   5861     0xff, 0x35, 8, 0, 0, 0,             /* pushq GOT+8(%rip) 		*/
   5862     0x4c, 0x8b, 0x1d, 16, 0, 0, 0,	/* mov GOT+16(%rip), %r11	*/
   5863     0x41, 0x83, 0xe3, NACLMASK,         /* and $-32, %r11d		*/
   5864     0x4d, 0x01, 0xfb,             	/* add %r15, %r11		*/
   5865     0x41, 0xff, 0xe3,             	/* jmpq *%r11			*/
   5866 
   5867     /* 9-byte nop sequence to pad out to the next 32-byte boundary.  */
   5868     0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1)	*/
   5869 
   5870     /* 32 bytes of nop to pad out to the standard size.  */
   5871     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data32 prefixes	*/
   5872     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1)	*/
   5873     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data32 prefixes	*/
   5874     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1)	*/
   5875     0x66,                                  /* excess data32 prefix	*/
   5876     0x90                                   /* nop */
   5877   };
   5878 
   5879 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
   5880   {
   5881     0x4c, 0x8b, 0x1d, 0, 0, 0, 0,	/* mov name@GOTPCREL(%rip),%r11	*/
   5882     0x41, 0x83, 0xe3, NACLMASK,         /* and $-32, %r11d		*/
   5883     0x4d, 0x01, 0xfb,             	/* add %r15, %r11		*/
   5884     0x41, 0xff, 0xe3,             	/* jmpq *%r11			*/
   5885 
   5886     /* 15-byte nop sequence to pad out to the next 32-byte boundary.  */
   5887     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data32 prefixes	*/
   5888     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1)	*/
   5889 
   5890     /* Lazy GOT entries point here (32-byte aligned).  */
   5891     0x68,                 /* pushq immediate */
   5892     0, 0, 0, 0,           /* replaced with index into relocation table.  */
   5893     0xe9,                 /* jmp relative */
   5894     0, 0, 0, 0,           /* replaced with offset to start of .plt0.  */
   5895 
   5896     /* 22 bytes of nop to pad out to the standard size.  */
   5897     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data32 prefixes	*/
   5898     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1)	*/
   5899     0x0f, 0x1f, 0x80, 0, 0, 0, 0,          /* nopl 0x0(%rax)		*/
   5900   };
   5901 
   5902 /* .eh_frame covering the .plt section.  */
   5903 
   5904 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
   5905   {
   5906 #if (PLT_CIE_LENGTH != 20                               \
   5907      || PLT_FDE_LENGTH != 36                            \
   5908      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
   5909      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
   5910 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
   5911 #endif
   5912     PLT_CIE_LENGTH, 0, 0, 0,	/* CIE length */
   5913     0, 0, 0, 0,			/* CIE ID */
   5914     1,				/* CIE version */
   5915     'z', 'R', 0,                /* Augmentation string */
   5916     1,				/* Code alignment factor */
   5917     0x78,                       /* Data alignment factor */
   5918     16,				/* Return address column */
   5919     1,				/* Augmentation size */
   5920     DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
   5921     DW_CFA_def_cfa, 7, 8,	/* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
   5922     DW_CFA_offset + 16, 1,	/* DW_CFA_offset: r16 (rip) at cfa-8 */
   5923     DW_CFA_nop, DW_CFA_nop,
   5924 
   5925     PLT_FDE_LENGTH, 0, 0, 0,	/* FDE length */
   5926     PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
   5927     0, 0, 0, 0,			/* R_X86_64_PC32 .plt goes here */
   5928     0, 0, 0, 0,			/* .plt size goes here */
   5929     0,				/* Augmentation size */
   5930     DW_CFA_def_cfa_offset, 16,	/* DW_CFA_def_cfa_offset: 16 */
   5931     DW_CFA_advance_loc + 6,	/* DW_CFA_advance_loc: 6 to __PLT__+6 */
   5932     DW_CFA_def_cfa_offset, 24,	/* DW_CFA_def_cfa_offset: 24 */
   5933     DW_CFA_advance_loc + 58,	/* DW_CFA_advance_loc: 58 to __PLT__+64 */
   5934     DW_CFA_def_cfa_expression,	/* DW_CFA_def_cfa_expression */
   5935     13,				/* Block length */
   5936     DW_OP_breg7, 8,		/* DW_OP_breg7 (rsp): 8 */
   5937     DW_OP_breg16, 0,		/* DW_OP_breg16 (rip): 0 */
   5938     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
   5939     DW_OP_lit3, DW_OP_shl, DW_OP_plus,
   5940     DW_CFA_nop, DW_CFA_nop
   5941   };
   5942 
   5943 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
   5944   {
   5945     elf_x86_64_nacl_plt0_entry,              /* plt0_entry */
   5946     elf_x86_64_nacl_plt_entry,               /* plt_entry */
   5947     NACL_PLT_ENTRY_SIZE,                     /* plt_entry_size */
   5948     2,                                       /* plt0_got1_offset */
   5949     9,                                       /* plt0_got2_offset */
   5950     13,                                      /* plt0_got2_insn_end */
   5951     3,                                       /* plt_got_offset */
   5952     33,                                      /* plt_reloc_offset */
   5953     38,                                      /* plt_plt_offset */
   5954     7,                                       /* plt_got_insn_size */
   5955     42,                                      /* plt_plt_insn_end */
   5956     32,                                      /* plt_lazy_offset */
   5957     elf_x86_64_nacl_eh_frame_plt,            /* eh_frame_plt */
   5958     sizeof (elf_x86_64_nacl_eh_frame_plt),   /* eh_frame_plt_size */
   5959   };
   5960 
   5961 #undef	elf_backend_arch_data
   5962 #define	elf_backend_arch_data	&elf_x86_64_nacl_arch_bed
   5963 
   5964 #undef	elf_backend_object_p
   5965 #define elf_backend_object_p			elf64_x86_64_nacl_elf_object_p
   5966 #undef	elf_backend_modify_segment_map
   5967 #define	elf_backend_modify_segment_map		nacl_modify_segment_map
   5968 #undef	elf_backend_modify_program_headers
   5969 #define	elf_backend_modify_program_headers	nacl_modify_program_headers
   5970 #undef	elf_backend_final_write_processing
   5971 #define elf_backend_final_write_processing	nacl_final_write_processing
   5972 
   5973 #include "elf64-target.h"
   5974 
   5975 /* Native Client x32 support.  */
   5976 
   5977 static bfd_boolean
   5978 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
   5979 {
   5980   /* Set the right machine number for a NaCl x86-64 ELF32 file.  */
   5981   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
   5982   return TRUE;
   5983 }
   5984 
   5985 #undef  TARGET_LITTLE_SYM
   5986 #define TARGET_LITTLE_SYM		x86_64_elf32_nacl_vec
   5987 #undef  TARGET_LITTLE_NAME
   5988 #define TARGET_LITTLE_NAME		"elf32-x86-64-nacl"
   5989 #undef	elf32_bed
   5990 #define	elf32_bed			elf32_x86_64_nacl_bed
   5991 
   5992 #define bfd_elf32_bfd_link_hash_table_create \
   5993   elf_x86_64_link_hash_table_create
   5994 #define bfd_elf32_bfd_reloc_type_lookup	\
   5995   elf_x86_64_reloc_type_lookup
   5996 #define bfd_elf32_bfd_reloc_name_lookup \
   5997   elf_x86_64_reloc_name_lookup
   5998 #define bfd_elf32_mkobject \
   5999   elf_x86_64_mkobject
   6000 
   6001 #undef elf_backend_object_p
   6002 #define elf_backend_object_p \
   6003   elf32_x86_64_nacl_elf_object_p
   6004 
   6005 #undef elf_backend_bfd_from_remote_memory
   6006 #define elf_backend_bfd_from_remote_memory \
   6007   _bfd_elf32_bfd_from_remote_memory
   6008 
   6009 #undef elf_backend_size_info
   6010 #define elf_backend_size_info \
   6011   _bfd_elf32_size_info
   6012 
   6013 #include "elf32-target.h"
   6014 
   6015 /* Restore defaults.  */
   6016 #undef	elf_backend_object_p
   6017 #define elf_backend_object_p		    elf64_x86_64_elf_object_p
   6018 #undef	elf_backend_bfd_from_remote_memory
   6019 #undef	elf_backend_size_info
   6020 #undef	elf_backend_modify_segment_map
   6021 #undef	elf_backend_modify_program_headers
   6022 #undef	elf_backend_final_write_processing
   6023 
   6024 /* Intel L1OM support.  */
   6025 
   6026 static bfd_boolean
   6027 elf64_l1om_elf_object_p (bfd *abfd)
   6028 {
   6029   /* Set the right machine number for an L1OM elf64 file.  */
   6030   bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
   6031   return TRUE;
   6032 }
   6033 
   6034 #undef  TARGET_LITTLE_SYM
   6035 #define TARGET_LITTLE_SYM		    l1om_elf64_vec
   6036 #undef  TARGET_LITTLE_NAME
   6037 #define TARGET_LITTLE_NAME		    "elf64-l1om"
   6038 #undef ELF_ARCH
   6039 #define ELF_ARCH			    bfd_arch_l1om
   6040 
   6041 #undef	ELF_MACHINE_CODE
   6042 #define ELF_MACHINE_CODE		    EM_L1OM
   6043 
   6044 #undef	ELF_OSABI
   6045 
   6046 #undef  elf64_bed
   6047 #define elf64_bed elf64_l1om_bed
   6048 
   6049 #undef elf_backend_object_p
   6050 #define elf_backend_object_p		    elf64_l1om_elf_object_p
   6051 
   6052 /* Restore defaults.  */
   6053 #undef	ELF_MAXPAGESIZE
   6054 #undef	ELF_MINPAGESIZE
   6055 #undef	ELF_COMMONPAGESIZE
   6056 #define ELF_MAXPAGESIZE			0x200000
   6057 #define ELF_MINPAGESIZE			0x1000
   6058 #define ELF_COMMONPAGESIZE		0x1000
   6059 #undef	elf_backend_plt_alignment
   6060 #define elf_backend_plt_alignment	4
   6061 #undef	elf_backend_arch_data
   6062 #define	elf_backend_arch_data	&elf_x86_64_arch_bed
   6063 
   6064 #include "elf64-target.h"
   6065 
   6066 /* FreeBSD L1OM support.  */
   6067 
   6068 #undef  TARGET_LITTLE_SYM
   6069 #define TARGET_LITTLE_SYM		    l1om_elf64_fbsd_vec
   6070 #undef  TARGET_LITTLE_NAME
   6071 #define TARGET_LITTLE_NAME		    "elf64-l1om-freebsd"
   6072 
   6073 #undef	ELF_OSABI
   6074 #define	ELF_OSABI			    ELFOSABI_FREEBSD
   6075 
   6076 #undef  elf64_bed
   6077 #define elf64_bed elf64_l1om_fbsd_bed
   6078 
   6079 #include "elf64-target.h"
   6080 
   6081 /* Intel K1OM support.  */
   6082 
   6083 static bfd_boolean
   6084 elf64_k1om_elf_object_p (bfd *abfd)
   6085 {
   6086   /* Set the right machine number for an K1OM elf64 file.  */
   6087   bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
   6088   return TRUE;
   6089 }
   6090 
   6091 #undef  TARGET_LITTLE_SYM
   6092 #define TARGET_LITTLE_SYM		    k1om_elf64_vec
   6093 #undef  TARGET_LITTLE_NAME
   6094 #define TARGET_LITTLE_NAME		    "elf64-k1om"
   6095 #undef ELF_ARCH
   6096 #define ELF_ARCH			    bfd_arch_k1om
   6097 
   6098 #undef	ELF_MACHINE_CODE
   6099 #define ELF_MACHINE_CODE		    EM_K1OM
   6100 
   6101 #undef	ELF_OSABI
   6102 
   6103 #undef  elf64_bed
   6104 #define elf64_bed elf64_k1om_bed
   6105 
   6106 #undef elf_backend_object_p
   6107 #define elf_backend_object_p		    elf64_k1om_elf_object_p
   6108 
   6109 #undef  elf_backend_static_tls_alignment
   6110 
   6111 #undef elf_backend_want_plt_sym
   6112 #define elf_backend_want_plt_sym	    0
   6113 
   6114 #include "elf64-target.h"
   6115 
   6116 /* FreeBSD K1OM support.  */
   6117 
   6118 #undef  TARGET_LITTLE_SYM
   6119 #define TARGET_LITTLE_SYM		    k1om_elf64_fbsd_vec
   6120 #undef  TARGET_LITTLE_NAME
   6121 #define TARGET_LITTLE_NAME		    "elf64-k1om-freebsd"
   6122 
   6123 #undef	ELF_OSABI
   6124 #define	ELF_OSABI			    ELFOSABI_FREEBSD
   6125 
   6126 #undef  elf64_bed
   6127 #define elf64_bed elf64_k1om_fbsd_bed
   6128 
   6129 #include "elf64-target.h"
   6130 
   6131 /* 32bit x86-64 support.  */
   6132 
   6133 #undef  TARGET_LITTLE_SYM
   6134 #define TARGET_LITTLE_SYM		    x86_64_elf32_vec
   6135 #undef  TARGET_LITTLE_NAME
   6136 #define TARGET_LITTLE_NAME		    "elf32-x86-64"
   6137 #undef	elf32_bed
   6138 
   6139 #undef ELF_ARCH
   6140 #define ELF_ARCH			    bfd_arch_i386
   6141 
   6142 #undef	ELF_MACHINE_CODE
   6143 #define ELF_MACHINE_CODE		    EM_X86_64
   6144 
   6145 #undef	ELF_OSABI
   6146 
   6147 #undef elf_backend_object_p
   6148 #define elf_backend_object_p \
   6149   elf32_x86_64_elf_object_p
   6150 
   6151 #undef elf_backend_bfd_from_remote_memory
   6152 #define elf_backend_bfd_from_remote_memory \
   6153   _bfd_elf32_bfd_from_remote_memory
   6154 
   6155 #undef elf_backend_size_info
   6156 #define elf_backend_size_info \
   6157   _bfd_elf32_size_info
   6158 
   6159 #include "elf32-target.h"
   6160