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