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