Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/bfd/elf64-x86-64.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* X86-64 specific support for 64-bit ELF
    2    Copyright 2000, 2001 Free Software Foundation, Inc.
     2   Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    33   Contributed by Jan Hubicka <jh@suse.cz>.
    44
    5 This file is part of BFD, the Binary File Descriptor library.
    6 
    7 This program is free software; you can redistribute it and/or modify
    8 it under the terms of the GNU General Public License as published by
    9 the Free Software Foundation; either version 2 of the License, or
    10 (at your option) any later version.
    11 
    12 This program is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with this program; if not, write to the Free Software
    19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
     5   This file is part of BFD, the Binary File Descriptor library.
     6
     7   This program is free software; you can redistribute it and/or modify
     8   it under the terms of the GNU General Public License as published by
     9   the Free Software Foundation; either version 2 of the License, or
     10   (at your option) any later version.
     11
     12   This program is distributed in the hope that it will be useful,
     13   but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   GNU General Public License for more details.
     16
     17   You should have received a copy of the GNU General Public License
     18   along with this program; if not, write to the Free Software
     19   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
    2020
    2121#include "bfd.h"
    2222#include "sysdep.h"
     23#include "bfdlink.h"
    2324#include "libbfd.h"
    2425#include "elf-bfd.h"
    2526
    2627#include "elf/x86-64.h"
    27 
    28 /* We use only the RELA entries.  */
    29 #define USE_RELA
    3028
    3129/* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
     
    3735static reloc_howto_type x86_64_elf_howto_table[] =
    3836{
    39   HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
    40         bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
    41         false),
    42   HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
    43         bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
    44         false),
    45   HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
    46         bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
    47         true),
    48   HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
    49         bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
    50         false),
    51   HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
    52         bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
    53         true),
    54   HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
    55         bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
    56         false),
    57   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
    58         bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
    59         MINUS_ONE, false),
    60   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
    61         bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
    62         MINUS_ONE, false),
    63   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
    64         bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
    65         MINUS_ONE, false),
    66   HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
    67         bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
    68         0xffffffff, true),
    69   HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
    70         bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
    71         false),
    72   HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
    73         bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
    74         false),
    75   HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
    76         bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
    77   HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
    78         bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
    79   HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
    80         bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
    81   HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
    82         bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true)
     37  HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
     38        bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
     39        FALSE),
     40  HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     41        bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
     42        FALSE),
     43  HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     44        bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
     45        TRUE),
     46  HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
     47        bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
     48        FALSE),
     49  HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     50        bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
     51        TRUE),
     52  HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
     53        bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
     54        FALSE),
     55  HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     56        bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
     57        MINUS_ONE, FALSE),
     58  HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     59        bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
     60        MINUS_ONE, FALSE),
     61  HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     62        bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
     63        MINUS_ONE, FALSE),
     64  HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     65        bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
     66        0xffffffff, TRUE),
     67  HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
     68        bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
     69        FALSE),
     70  HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
     71        bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
     72        FALSE),
     73  HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
     74        bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
     75  HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
     76        bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
     77  HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
     78        bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
     79  HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
     80        bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
     81  HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     82        bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
     83        MINUS_ONE, FALSE),
     84  HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     85        bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
     86        MINUS_ONE, FALSE),
     87  HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
     88        bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
     89        MINUS_ONE, FALSE),
     90  HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     91        bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
     92        0xffffffff, TRUE),
     93  HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     94        bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
     95        0xffffffff, TRUE),
     96  HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
     97        bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
     98        0xffffffff, FALSE),
     99  HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
     100        bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
     101        0xffffffff, TRUE),
     102  HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
     103        bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
     104        0xffffffff, FALSE),
     105
     106/* GNU extension to record C++ vtable hierarchy.  */
     107  HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
     108         NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
     109
     110/* GNU extension to record C++ vtable member usage.  */
     111  HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
     112         _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
     113         FALSE)
    83114};
    84115
     
    90121};
    91122
    92 static CONST struct elf_reloc_map x86_64_reloc_map[] =
     123static const struct elf_reloc_map x86_64_reloc_map[] =
    93124{
    94125  { BFD_RELOC_NONE,             R_X86_64_NONE, },
     
    108139  { BFD_RELOC_8,                R_X86_64_8, },
    109140  { BFD_RELOC_8_PCREL,          R_X86_64_PC8, },
     141  { BFD_RELOC_X86_64_DTPMOD64,  R_X86_64_DTPMOD64, },
     142  { BFD_RELOC_X86_64_DTPOFF64,  R_X86_64_DTPOFF64, },
     143  { BFD_RELOC_X86_64_TPOFF64,   R_X86_64_TPOFF64, },
     144  { BFD_RELOC_X86_64_TLSGD,     R_X86_64_TLSGD, },
     145  { BFD_RELOC_X86_64_TLSLD,     R_X86_64_TLSLD, },
     146  { BFD_RELOC_X86_64_DTPOFF32,  R_X86_64_DTPOFF32, },
     147  { BFD_RELOC_X86_64_GOTTPOFF,  R_X86_64_GOTTPOFF, },
     148  { BFD_RELOC_X86_64_TPOFF32,   R_X86_64_TPOFF32, },
     149  { BFD_RELOC_VTABLE_INHERIT,   R_X86_64_GNU_VTINHERIT, },
     150  { BFD_RELOC_VTABLE_ENTRY,     R_X86_64_GNU_VTENTRY, },
    110151};
    111152
     
    113154  PARAMS ((bfd *, bfd_reloc_code_real_type));
    114155static void elf64_x86_64_info_to_howto
    115   PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
     156  PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
     157static bfd_boolean elf64_x86_64_grok_prstatus
     158  PARAMS ((bfd *, Elf_Internal_Note *));
     159static bfd_boolean elf64_x86_64_grok_psinfo
     160  PARAMS ((bfd *, Elf_Internal_Note *));
    116161static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
    117162  PARAMS ((bfd *));
    118 
    119 static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc
     163static int elf64_x86_64_tls_transition
     164  PARAMS ((struct bfd_link_info *, int, int));
     165static bfd_boolean elf64_x86_64_mkobject
     166  PARAMS((bfd *));
     167static bfd_boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd));
     168static bfd_boolean create_got_section
     169  PARAMS((bfd *, struct bfd_link_info *));
     170static bfd_boolean elf64_x86_64_create_dynamic_sections
     171  PARAMS((bfd *, struct bfd_link_info *));
     172static void elf64_x86_64_copy_indirect_symbol
     173  PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
     174           struct elf_link_hash_entry *));
     175static bfd_boolean elf64_x86_64_check_relocs
     176  PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
     177           const Elf_Internal_Rela *));
     178static asection *elf64_x86_64_gc_mark_hook
     179  PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
     180           struct elf_link_hash_entry *, Elf_Internal_Sym *));
     181
     182static bfd_boolean elf64_x86_64_gc_sweep_hook
     183  PARAMS ((bfd *, struct bfd_link_info *, asection *,
     184           const Elf_Internal_Rela *));
     185
     186static struct bfd_hash_entry *link_hash_newfunc
    120187  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
    121 static boolean elf64_x86_64_adjust_dynamic_symbol
     188static bfd_boolean elf64_x86_64_adjust_dynamic_symbol
    122189  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
    123190
    124 static boolean elf64_x86_64_size_dynamic_sections
     191static bfd_boolean allocate_dynrelocs
     192  PARAMS ((struct elf_link_hash_entry *, PTR));
     193static bfd_boolean readonly_dynrelocs
     194  PARAMS ((struct elf_link_hash_entry *, PTR));
     195static bfd_boolean elf64_x86_64_size_dynamic_sections
    125196  PARAMS ((bfd *, struct bfd_link_info *));
    126 static boolean elf64_x86_64_relocate_section
     197static bfd_vma dtpoff_base
     198  PARAMS ((struct bfd_link_info *));
     199static bfd_vma tpoff
     200  PARAMS ((struct bfd_link_info *, bfd_vma));
     201static bfd_boolean elf64_x86_64_relocate_section
    127202  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
    128203         Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
    129 static boolean elf64_x86_64_finish_dynamic_symbol
     204static bfd_boolean elf64_x86_64_finish_dynamic_symbol
    130205  PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
    131206           Elf_Internal_Sym *sym));
    132 static boolean elf64_x86_64_finish_dynamic_sections
     207static bfd_boolean elf64_x86_64_finish_dynamic_sections
    133208  PARAMS ((bfd *, struct bfd_link_info *));
     209static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
     210  PARAMS ((const Elf_Internal_Rela *));
    134211
    135212/* Given a BFD reloc type, return a HOWTO structure.  */
     
    144221    {
    145222      if (x86_64_reloc_map[i].bfd_reloc_val == code)
    146         return &x86_64_elf_howto_table[(int)
    147                                        x86_64_reloc_map[i].elf_reloc_val];
     223        return &x86_64_elf_howto_table[i];
    148224    }
    149225  return 0;
     
    156232     bfd *abfd ATTRIBUTE_UNUSED;
    157233     arelent *cache_ptr;
    158      Elf64_Internal_Rela *dst;
    159 {
    160   unsigned r_type;
     234     Elf_Internal_Rela *dst;
     235{
     236  unsigned r_type, i;
    161237
    162238  r_type = ELF64_R_TYPE (dst->r_info);
    163   BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
    164   cache_ptr->howto = &x86_64_elf_howto_table[r_type];
     239  if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
     240    {
     241      BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
     242      i = r_type;
     243    }
     244  else
     245    {
     246      BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
     247      i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
     248    }
     249  cache_ptr->howto = &x86_64_elf_howto_table[i];
    165250  BFD_ASSERT (r_type == cache_ptr->howto->type);
    166251}
    167252
    168253
     254/* Support for core dump NOTE sections.  */
     255static bfd_boolean
     256elf64_x86_64_grok_prstatus (abfd, note)
     257     bfd *abfd;
     258     Elf_Internal_Note *note;
     259{
     260  int offset;
     261  size_t raw_size;
     262
     263  switch (note->descsz)
     264    {
     265      default:
     266        return FALSE;
     267
     268      case 336:         /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
     269        /* pr_cursig */
     270        elf_tdata (abfd)->core_signal
     271          = bfd_get_16 (abfd, note->descdata + 12);
     272
     273        /* pr_pid */
     274        elf_tdata (abfd)->core_pid
     275          = bfd_get_32 (abfd, note->descdata + 32);
     276
     277        /* pr_reg */
     278        offset = 112;
     279        raw_size = 216;
     280
     281        break;
     282    }
     283
     284  /* Make a ".reg/999" section.  */
     285  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
     286                                          raw_size, note->descpos + offset);
     287}
     288
     289static bfd_boolean
     290elf64_x86_64_grok_psinfo (abfd, note)
     291     bfd *abfd;
     292     Elf_Internal_Note *note;
     293{
     294  switch (note->descsz)
     295    {
     296      default:
     297        return FALSE;
     298
     299      case 136:         /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
     300        elf_tdata (abfd)->core_program
     301         = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
     302        elf_tdata (abfd)->core_command
     303         = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
     304    }
     305
     306  /* Note that for some reason, a spurious space is tacked
     307     onto the end of the args in some (at least one anyway)
     308     implementations, so strip it off if it exists.  */
     309
     310  {
     311    char *command = elf_tdata (abfd)->core_command;
     312    int n = strlen (command);
     313
     314    if (0 < n && command[n - 1] == ' ')
     315      command[n - 1] = '\0';
     316  }
     317
     318  return TRUE;
     319}
     320
     321
    169322/* Functions for the x86-64 ELF linker.  */
    170323
     
    173326
    174327#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
     328
     329/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
     330   copying dynamic variables from a shared lib into an app's dynbss
     331   section, and instead use a dynamic relocation to point into the
     332   shared lib.  */
     333#define ELIMINATE_COPY_RELOCS 1
    175334
    176335/* The size in bytes of an entry in the global offset table.  */
     
    205364
    206365/* The x86-64 linker needs to keep track of the number of relocs that
    207    it decides to copy in check_relocs for each symbol.  This is so
    208    that it can discard PC relative relocs if it doesn't need them when
    209    linking with -Bsymbolic.  We store the information in a field
    210    extending the regular ELF linker hash table.  */
    211 
    212 /* This structure keeps track of the number of PC relative relocs we
    213    have copied for a given symbol.  */
    214 
    215 struct elf64_x86_64_pcrel_relocs_copied
     366   it decides to copy as dynamic relocs in check_relocs for each symbol.
     367   This is so that it can later discard them if they are found to be
     368   unnecessary.  We store the information in a field extending the
     369   regular ELF linker hash table.  */
     370
     371struct elf64_x86_64_dyn_relocs
    216372{
    217373  /* Next section.  */
    218   struct elf64_x86_64_pcrel_relocs_copied *next;
    219   /* A section in dynobj.  */
    220   asection *section;
    221   /* Number of relocs copied in this section.  */
     374  struct elf64_x86_64_dyn_relocs *next;
     375
     376  /* The input section of the reloc.  */
     377  asection *sec;
     378
     379  /* Total number of relocs copied for the input section.  */
    222380  bfd_size_type count;
     381
     382  /* Number of pc-relative relocs copied for the input section.  */
     383  bfd_size_type pc_count;
    223384};
    224385
     
    227388struct elf64_x86_64_link_hash_entry
    228389{
    229   struct elf_link_hash_entry root;
    230 
    231   /* Number of PC relative relocs copied for this symbol.  */
    232   struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied;
     390  struct elf_link_hash_entry elf;
     391
     392  /* Track dynamic relocs copied for this symbol.  */
     393  struct elf64_x86_64_dyn_relocs *dyn_relocs;
     394
     395#define GOT_UNKNOWN     0
     396#define GOT_NORMAL      1
     397#define GOT_TLS_GD      2
     398#define GOT_TLS_IE      3
     399  unsigned char tls_type;
    233400};
    234401
    235 /* x86-64  ELF linker hash table.  */
     402#define elf64_x86_64_hash_entry(ent) \
     403  ((struct elf64_x86_64_link_hash_entry *)(ent))
     404
     405struct elf64_x86_64_obj_tdata
     406{
     407  struct elf_obj_tdata root;
     408
     409  /* tls_type for each local got entry.  */
     410  char *local_got_tls_type;
     411};
     412
     413#define elf64_x86_64_tdata(abfd) \
     414  ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
     415
     416#define elf64_x86_64_local_got_tls_type(abfd) \
     417  (elf64_x86_64_tdata (abfd)->local_got_tls_type)
     418
     419
     420/* x86-64 ELF linker hash table.  */
    236421
    237422struct elf64_x86_64_link_hash_table
    238423{
    239   struct elf_link_hash_table root;
     424  struct elf_link_hash_table elf;
     425
     426  /* Short-cuts to get to dynamic linker sections.  */
     427  asection *sgot;
     428  asection *sgotplt;
     429  asection *srelgot;
     430  asection *splt;
     431  asection *srelplt;
     432  asection *sdynbss;
     433  asection *srelbss;
     434
     435  union {
     436    bfd_signed_vma refcount;
     437    bfd_vma offset;
     438  } tls_ld_got;
     439
     440  /* Small local sym to section mapping cache.  */
     441  struct sym_sec_cache sym_sec;
    240442};
    241 
    242 /* Declare this now that the above structures are defined.  */
    243 
    244 static boolean elf64_x86_64_discard_copies
    245   PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR));
    246 
    247 /* Traverse an x86-64 ELF linker hash table.  */
    248 
    249 #define elf64_x86_64_link_hash_traverse(table, func, info)              \
    250   (elf_link_hash_traverse                                               \
    251    (&(table)->root,                                                     \
    252     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
    253     (info)))
    254443
    255444/* Get the x86-64 ELF linker hash table from a link_info structure.  */
     
    261450
    262451static struct bfd_hash_entry *
    263 elf64_x86_64_link_hash_newfunc (entry, table, string)
     452link_hash_newfunc (entry, table, string)
    264453     struct bfd_hash_entry *entry;
    265454     struct bfd_hash_table *table;
    266455     const char *string;
    267456{
    268   struct elf64_x86_64_link_hash_entry *ret =
    269     (struct elf64_x86_64_link_hash_entry *) entry;
    270 
    271457  /* Allocate the structure if it has not already been allocated by a
    272      subclass.  */
    273   if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
    274     ret = ((struct elf64_x86_64_link_hash_entry *)
    275            bfd_hash_allocate (table,
    276                               sizeof (struct elf64_x86_64_link_hash_entry)));
    277   if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
    278     return (struct bfd_hash_entry *) ret;
     458     subclass.  */
     459  if (entry == NULL)
     460    {
     461      entry = bfd_hash_allocate (table,
     462                                 sizeof (struct elf64_x86_64_link_hash_entry));
     463      if (entry == NULL)
     464        return entry;
     465    }
    279466
    280467  /* Call the allocation method of the superclass.  */
    281   ret = ((struct elf64_x86_64_link_hash_entry *)
    282          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
    283                                      table, string));
    284   if (ret != (struct elf64_x86_64_link_hash_entry *) NULL)
    285     {
    286       ret->pcrel_relocs_copied = NULL;
    287     }
    288 
    289   return (struct bfd_hash_entry *) ret;
     468  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
     469  if (entry != NULL)
     470    {
     471      struct elf64_x86_64_link_hash_entry *eh;
     472
     473      eh = (struct elf64_x86_64_link_hash_entry *) entry;
     474      eh->dyn_relocs = NULL;
     475      eh->tls_type = GOT_UNKNOWN;
     476    }
     477
     478  return entry;
    290479}
    291480
     
    297486{
    298487  struct elf64_x86_64_link_hash_table *ret;
    299 
    300   ret = ((struct elf64_x86_64_link_hash_table *)
    301          bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table)));
    302   if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
     488  bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
     489
     490  ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
     491  if (ret == NULL)
    303492    return NULL;
    304493
    305   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
    306                                        elf64_x86_64_link_hash_newfunc))
    307     {
    308       bfd_release (abfd, ret);
     494  if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
     495    {
     496      free (ret);
    309497      return NULL;
    310498    }
    311499
    312   return &ret->root.root;
     500  ret->sgot = NULL;
     501  ret->sgotplt = NULL;
     502  ret->srelgot = NULL;
     503  ret->splt = NULL;
     504  ret->srelplt = NULL;
     505  ret->sdynbss = NULL;
     506  ret->srelbss = NULL;
     507  ret->sym_sec.abfd = NULL;
     508  ret->tls_ld_got.refcount = 0;
     509
     510  return &ret->elf.root;
    313511}
    314512
    315 boolean
     513/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
     514   shortcuts to them in our hash table.  */
     515
     516static bfd_boolean
     517create_got_section (dynobj, info)
     518     bfd *dynobj;
     519     struct bfd_link_info *info;
     520{
     521  struct elf64_x86_64_link_hash_table *htab;
     522
     523  if (! _bfd_elf_create_got_section (dynobj, info))
     524    return FALSE;
     525
     526  htab = elf64_x86_64_hash_table (info);
     527  htab->sgot = bfd_get_section_by_name (dynobj, ".got");
     528  htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
     529  if (!htab->sgot || !htab->sgotplt)
     530    abort ();
     531
     532  htab->srelgot = bfd_make_section (dynobj, ".rela.got");
     533  if (htab->srelgot == NULL
     534      || ! bfd_set_section_flags (dynobj, htab->srelgot,
     535                                  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
     536                                   | SEC_IN_MEMORY | SEC_LINKER_CREATED
     537                                   | SEC_READONLY))
     538      || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
     539    return FALSE;
     540  return TRUE;
     541}
     542
     543/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
     544   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
     545   hash table.  */
     546
     547static bfd_boolean
     548elf64_x86_64_create_dynamic_sections (dynobj, info)
     549     bfd *dynobj;
     550     struct bfd_link_info *info;
     551{
     552  struct elf64_x86_64_link_hash_table *htab;
     553
     554  htab = elf64_x86_64_hash_table (info);
     555  if (!htab->sgot && !create_got_section (dynobj, info))
     556    return FALSE;
     557
     558  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
     559    return FALSE;
     560
     561  htab->splt = bfd_get_section_by_name (dynobj, ".plt");
     562  htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
     563  htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
     564  if (!info->shared)
     565    htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
     566
     567  if (!htab->splt || !htab->srelplt || !htab->sdynbss
     568      || (!info->shared && !htab->srelbss))
     569    abort ();
     570
     571  return TRUE;
     572}
     573
     574/* Copy the extra info we tack onto an elf_link_hash_entry.  */
     575
     576static void
     577elf64_x86_64_copy_indirect_symbol (bed, dir, ind)
     578     struct elf_backend_data *bed;
     579     struct elf_link_hash_entry *dir, *ind;
     580{
     581  struct elf64_x86_64_link_hash_entry *edir, *eind;
     582
     583  edir = (struct elf64_x86_64_link_hash_entry *) dir;
     584  eind = (struct elf64_x86_64_link_hash_entry *) ind;
     585
     586  if (eind->dyn_relocs != NULL)
     587    {
     588      if (edir->dyn_relocs != NULL)
     589        {
     590          struct elf64_x86_64_dyn_relocs **pp;
     591          struct elf64_x86_64_dyn_relocs *p;
     592
     593          if (ind->root.type == bfd_link_hash_indirect)
     594            abort ();
     595
     596          /* Add reloc counts against the weak sym to the strong sym
     597             list.  Merge any entries against the same section.  */
     598          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
     599            {
     600              struct elf64_x86_64_dyn_relocs *q;
     601
     602              for (q = edir->dyn_relocs; q != NULL; q = q->next)
     603                if (q->sec == p->sec)
     604                  {
     605                    q->pc_count += p->pc_count;
     606                    q->count += p->count;
     607                    *pp = p->next;
     608                    break;
     609                  }
     610              if (q == NULL)
     611                pp = &p->next;
     612            }
     613          *pp = edir->dyn_relocs;
     614        }
     615
     616      edir->dyn_relocs = eind->dyn_relocs;
     617      eind->dyn_relocs = NULL;
     618    }
     619
     620  if (ind->root.type == bfd_link_hash_indirect
     621      && dir->got.refcount <= 0)
     622    {
     623      edir->tls_type = eind->tls_type;
     624      eind->tls_type = GOT_UNKNOWN;
     625    }
     626
     627  if (ELIMINATE_COPY_RELOCS
     628      && ind->root.type != bfd_link_hash_indirect
     629      && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
     630    /* If called to transfer flags for a weakdef during processing
     631       of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
     632       We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
     633    dir->elf_link_hash_flags |=
     634      (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
     635                                   | ELF_LINK_HASH_REF_REGULAR
     636                                   | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
     637  else
     638    _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
     639}
     640
     641static bfd_boolean
     642elf64_x86_64_mkobject (abfd)
     643     bfd *abfd;
     644{
     645  bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
     646  abfd->tdata.any = bfd_zalloc (abfd, amt);
     647  if (abfd->tdata.any == NULL)
     648    return FALSE;
     649  return TRUE;
     650}
     651
     652static bfd_boolean
    316653elf64_x86_64_elf_object_p (abfd)
    317      bfd *abfd;
    318 {
     654  bfd *abfd;
     655{
     656  /* Allocate our special target data.  */
     657  struct elf64_x86_64_obj_tdata *new_tdata;
     658  bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
     659  new_tdata = bfd_zalloc (abfd, amt);
     660  if (new_tdata == NULL)
     661    return FALSE;
     662  new_tdata->root = *abfd->tdata.elf_obj_data;
     663  abfd->tdata.any = new_tdata;
    319664  /* Set the right machine number for an x86-64 elf64 file.  */
    320665  bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
    321   return true;
     666  return TRUE;
    322667}
    323668
     669static int
     670elf64_x86_64_tls_transition (info, r_type, is_local)
     671     struct bfd_link_info *info;
     672     int r_type;
     673     int is_local;
     674{
     675  if (info->shared)
     676    return r_type;
     677
     678  switch (r_type)
     679    {
     680    case R_X86_64_TLSGD:
     681    case R_X86_64_GOTTPOFF:
     682      if (is_local)
     683        return R_X86_64_TPOFF32;
     684      return R_X86_64_GOTTPOFF;
     685    case R_X86_64_TLSLD:
     686      return R_X86_64_TPOFF32;
     687    }
     688
     689   return r_type;
     690}
     691
    324692/* Look through the relocs for a section during the first phase, and
    325    allocate space in the global offset table or procedure linkage
    326    table.  */
    327 
    328 static boolean
     693   calculate needed space in the global offset table, procedure
     694   linkage table, and dynamic reloc sections.  */
     695
     696static bfd_boolean
    329697elf64_x86_64_check_relocs (abfd, info, sec, relocs)
    330698     bfd *abfd;
     
    333701     const Elf_Internal_Rela *relocs;
    334702{
    335   bfd *dynobj;
     703  struct elf64_x86_64_link_hash_table *htab;
    336704  Elf_Internal_Shdr *symtab_hdr;
    337705  struct elf_link_hash_entry **sym_hashes;
    338   bfd_signed_vma *local_got_refcounts;
    339706  const Elf_Internal_Rela *rel;
    340707  const Elf_Internal_Rela *rel_end;
    341   asection *sgot;
    342   asection *srelgot;
    343708  asection *sreloc;
    344709
    345710  if (info->relocateable)
    346     return true;
    347 
    348   dynobj = elf_hash_table (info)->dynobj;
     711    return TRUE;
     712
     713  htab = elf64_x86_64_hash_table (info);
    349714  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    350715  sym_hashes = elf_sym_hashes (abfd);
    351   local_got_refcounts = elf_local_got_refcounts (abfd);
    352 
    353   sgot = srelgot = sreloc = NULL;
     716
     717  sreloc = NULL;
     718
    354719  rel_end = relocs + sec->reloc_count;
    355720  for (rel = relocs; rel < rel_end; rel++)
    356721    {
     722      unsigned int r_type;
    357723      unsigned long r_symndx;
    358724      struct elf_link_hash_entry *h;
    359725
    360726      r_symndx = ELF64_R_SYM (rel->r_info);
     727      r_type = ELF64_R_TYPE (rel->r_info);
     728
     729      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
     730        {
     731          (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
     732                                 bfd_archive_filename (abfd),
     733                                 r_symndx);
     734          return FALSE;
     735        }
     736
    361737      if (r_symndx < symtab_hdr->sh_info)
    362738        h = NULL;
     
    364740        h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    365741
    366       /* Some relocs require a global offset table.  */
    367       if (dynobj == NULL)
    368         {
    369           switch (ELF64_R_TYPE (rel->r_info))
    370             {
    371             case R_X86_64_GOT32:
    372             case R_X86_64_GOTPCREL:
    373               elf_hash_table (info)->dynobj = dynobj = abfd;
    374               if (! _bfd_elf_create_got_section (dynobj, info))
    375                 return false;
    376               break;
    377             }
    378         }
    379 
    380       switch (ELF64_R_TYPE (rel->r_info))
    381         {
     742      r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
     743      switch (r_type)
     744        {
     745        case R_X86_64_TLSLD:
     746          htab->tls_ld_got.refcount += 1;
     747          goto create_got;
     748
     749        case R_X86_64_TPOFF32:
     750          if (info->shared)
     751            {
     752              (*_bfd_error_handler)
     753                (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
     754                 bfd_archive_filename (abfd),
     755                 x86_64_elf_howto_table[r_type].name);
     756              bfd_set_error (bfd_error_bad_value);
     757              return FALSE;
     758            }
     759          break;
     760
     761        case R_X86_64_GOTTPOFF:
     762          if (info->shared)
     763            info->flags |= DF_STATIC_TLS;
     764          /* Fall through */
     765
     766        case R_X86_64_GOT32:
    382767        case R_X86_64_GOTPCREL:
    383         case R_X86_64_GOT32:
     768        case R_X86_64_TLSGD:
    384769          /* This symbol requires a global offset table entry.  */
    385 
    386           if (sgot == NULL)
    387             {
    388               sgot = bfd_get_section_by_name (dynobj, ".got");
    389               BFD_ASSERT (sgot != NULL);
    390             }
    391 
    392           if (srelgot == NULL && (h != NULL || info->shared))
    393             {
    394               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
    395               if (srelgot == NULL)
    396                 {
    397                   srelgot = bfd_make_section (dynobj, ".rela.got");
    398                   if (srelgot == NULL
    399                       || ! bfd_set_section_flags (dynobj, srelgot,
    400                                                   (SEC_ALLOC
    401                                                    | SEC_LOAD
    402                                                    | SEC_HAS_CONTENTS
    403                                                    | SEC_IN_MEMORY
    404                                                    | SEC_LINKER_CREATED
    405                                                    | SEC_READONLY))
    406                       || ! bfd_set_section_alignment (dynobj, srelgot, 3))
    407                     return false;
    408                 }
    409             }
    410 
    411           if (h != NULL)
    412             {
    413               if (h->got.refcount == -1)
    414                 {
    415                   h->got.refcount = 1;
    416 
    417                   /* Make sure this symbol is output as a dynamic symbol.  */
    418                   if (h->dynindx == -1)
    419                     {
    420                       if (! bfd_elf64_link_record_dynamic_symbol (info, h))
    421                         return false;
    422                     }
    423 
    424                   sgot->_raw_size += GOT_ENTRY_SIZE;
    425                   srelgot->_raw_size += sizeof (Elf64_External_Rela);
    426                 }
    427               else
     770          {
     771            int tls_type, old_tls_type;
     772
     773            switch (r_type)
     774              {
     775              default: tls_type = GOT_NORMAL; break;
     776              case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
     777              case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
     778              }
     779
     780            if (h != NULL)
     781              {
    428782                h->got.refcount += 1;
    429             }
    430           else
    431             {
    432               /* This is a global offset table entry for a local symbol.  */
    433               if (local_got_refcounts == NULL)
    434                 {
    435                   size_t size;
    436 
    437                   size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
    438                   local_got_refcounts = ((bfd_signed_vma *)
    439                                          bfd_alloc (abfd, size));
    440                   if (local_got_refcounts == NULL)
    441                     return false;
    442                   elf_local_got_refcounts (abfd) = local_got_refcounts;
    443                   memset (local_got_refcounts, -1, size);
    444                 }
    445               if (local_got_refcounts[r_symndx] == -1)
    446                 {
    447                   local_got_refcounts[r_symndx] = 1;
    448 
    449                   sgot->_raw_size += GOT_ENTRY_SIZE;
    450                   if (info->shared)
    451                     {
    452                       /* If we are generating a shared object, we need to
    453                          output a R_X86_64_RELATIVE reloc so that the dynamic
    454                          linker can adjust this GOT entry.  */
    455                       srelgot->_raw_size += sizeof (Elf64_External_Rela);
    456                     }
    457                 }
    458               else
     783                old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
     784              }
     785            else
     786              {
     787                bfd_signed_vma *local_got_refcounts;
     788
     789                /* This is a global offset table entry for a local symbol.  */
     790                local_got_refcounts = elf_local_got_refcounts (abfd);
     791                if (local_got_refcounts == NULL)
     792                  {
     793                    bfd_size_type size;
     794
     795                    size = symtab_hdr->sh_info;
     796                    size *= sizeof (bfd_signed_vma) + sizeof (char);
     797                    local_got_refcounts = ((bfd_signed_vma *)
     798                                           bfd_zalloc (abfd, size));
     799                    if (local_got_refcounts == NULL)
     800                      return FALSE;
     801                    elf_local_got_refcounts (abfd) = local_got_refcounts;
     802                    elf64_x86_64_local_got_tls_type (abfd)
     803                      = (char *) (local_got_refcounts + symtab_hdr->sh_info);
     804                  }
    459805                local_got_refcounts[r_symndx] += 1;
     806                old_tls_type
     807                  = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
     808              }
     809
     810            /* If a TLS symbol is accessed using IE at least once,
     811               there is no point to use dynamic model for it.  */
     812            if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
     813                && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
     814              {
     815                if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
     816                  tls_type = old_tls_type;
     817                else
     818                  {
     819                    (*_bfd_error_handler)
     820                      (_("%s: %s' accessed both as normal and thread local symbol"),
     821                       bfd_archive_filename (abfd),
     822                       h ? h->root.root.string : "<local>");
     823                    return FALSE;
     824                  }
     825              }
     826
     827            if (old_tls_type != tls_type)
     828              {
     829                if (h != NULL)
     830                  elf64_x86_64_hash_entry (h)->tls_type = tls_type;
     831                else
     832                  elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
     833              }
     834          }
     835          /* Fall through */
     836
     837          //case R_X86_64_GOTPCREL:
     838        create_got:
     839          if (htab->sgot == NULL)
     840            {
     841              if (htab->elf.dynobj == NULL)
     842                htab->elf.dynobj = abfd;
     843              if (!create_got_section (htab->elf.dynobj, info))
     844                return FALSE;
    460845            }
    461846          break;
     
    475860
    476861          h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
    477           if (h->plt.refcount == -1)
    478             h->plt.refcount = 1;
    479           else
    480             h->plt.refcount += 1;
     862          h->plt.refcount += 1;
    481863          break;
    482864
     
    484866        case R_X86_64_16:
    485867        case R_X86_64_32:
     868        case R_X86_64_32S:
     869          /* Let's help debug shared library creation.  These relocs
     870             cannot be used in shared libs.  Don't error out for
     871             sections we don't care about, such as debug sections or
     872             non-constant sections.  */
     873          if (info->shared
     874              && (sec->flags & SEC_ALLOC) != 0
     875              && (sec->flags & SEC_READONLY) != 0)
     876            {
     877              (*_bfd_error_handler)
     878                (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
     879                 bfd_archive_filename (abfd),
     880                 x86_64_elf_howto_table[r_type].name);
     881              bfd_set_error (bfd_error_bad_value);
     882              return FALSE;
     883            }
     884          /* Fall through.  */
     885
     886        case R_X86_64_PC8:
     887        case R_X86_64_PC16:
     888        case R_X86_64_PC32:
    486889        case R_X86_64_64:
    487         case R_X86_64_32S:
    488         case R_X86_64_PC32:
    489           if (h != NULL)
    490             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
     890          if (h != NULL && !info->shared)
     891            {
     892              /* If this reloc is in a read-only section, we might
     893                 need a copy reloc.  We can't check reliably at this
     894                 stage whether the section is read-only, as input
     895                 sections have not yet been mapped to output sections.
     896                 Tentatively set the flag for now, and correct in
     897                 adjust_dynamic_symbol.  */
     898              h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
     899
     900              /* We may need a .plt entry if the function this reloc
     901                 refers to is in a shared lib.  */
     902              h->plt.refcount += 1;
     903            }
    491904
    492905          /* If we are creating a shared library, and this is a reloc
     
    499912             this point we have not seen all the input files, so it is
    500913             possible that DEF_REGULAR is not set now but will be set
    501              later (it is never cleared).  We account for that
    502              possibility below by storing information in the
    503              pcrel_relocs_copied field of the hash table entry.
    504              A similar situation occurs when creating shared libraries
    505              and symbol visibility changes render the symbol local.  */
    506           if (info->shared
    507               && (sec->flags & SEC_ALLOC) != 0
    508               && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)
    509                   && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)
    510                   && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))
    511                   || (h != NULL
    512                       && (! info->symbolic
    513                           || (h->elf_link_hash_flags
    514                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
    515             {
    516               /* When creating a shared object, we must copy these
    517                  reloc types into the output file.  We create a reloc
    518                  section in dynobj and make room for this reloc.  */
     914             later (it is never cleared).  In case of a weak definition,
     915             DEF_REGULAR may be cleared later by a strong definition in
     916             a shared library.  We account for that possibility below by
     917             storing information in the relocs_copied field of the hash
     918             table entry.  A similar situation occurs when creating
     919             shared libraries and symbol visibility changes render the
     920             symbol local.
     921
     922             If on the other hand, we are creating an executable, we
     923             may need to keep relocations for symbols satisfied by a
     924             dynamic library if we manage to avoid copy relocs for the
     925             symbol.  */
     926          if ((info->shared
     927               && (sec->flags & SEC_ALLOC) != 0
     928               && (((r_type != R_X86_64_PC8)
     929                    && (r_type != R_X86_64_PC16)
     930                    && (r_type != R_X86_64_PC32))
     931                   || (h != NULL
     932                       && (! info->symbolic
     933                           || h->root.type == bfd_link_hash_defweak
     934                           || (h->elf_link_hash_flags
     935                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
     936              || (ELIMINATE_COPY_RELOCS
     937                  && !info->shared
     938                  && (sec->flags & SEC_ALLOC) != 0
     939                  && h != NULL
     940                  && (h->root.type == bfd_link_hash_defweak
     941                      || (h->elf_link_hash_flags
     942                          & ELF_LINK_HASH_DEF_REGULAR) == 0)))
     943            {
     944              struct elf64_x86_64_dyn_relocs *p;
     945              struct elf64_x86_64_dyn_relocs **head;
     946
     947              /* We must copy these reloc types into the output file.
     948                 Create a reloc section in dynobj and make room for
     949                 this reloc.  */
    519950              if (sreloc == NULL)
    520951                {
    521952                  const char *name;
     953                  bfd *dynobj;
    522954
    523955                  name = (bfd_elf_string_from_elf_section
     
    526958                           elf_section_data (sec)->rel_hdr.sh_name));
    527959                  if (name == NULL)
    528                     return false;
    529 
    530                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
    531                               && strcmp (bfd_get_section_name (abfd, sec),
    532                                          name + 5) == 0);
     960                    return FALSE;
     961
     962                  if (strncmp (name, ".rela", 5) != 0
     963                      || strcmp (bfd_get_section_name (abfd, sec),
     964                                 name + 5) != 0)
     965                    {
     966                      (*_bfd_error_handler)
     967                        (_("%s: bad relocation section name `%s\'"),
     968                         bfd_archive_filename (abfd), name);
     969                    }
     970
     971                  if (htab->elf.dynobj == NULL)
     972                    htab->elf.dynobj = abfd;
     973
     974                  dynobj = htab->elf.dynobj;
    533975
    534976                  sreloc = bfd_get_section_by_name (dynobj, name);
     
    545987                          || ! bfd_set_section_flags (dynobj, sreloc, flags)
    546988                          || ! bfd_set_section_alignment (dynobj, sreloc, 3))
    547                         return false;
     989                        return FALSE;
    548990                    }
     991                  elf_section_data (sec)->sreloc = sreloc;
    549992                }
    550993
    551               sreloc->_raw_size += sizeof (Elf64_External_Rela);
    552 
    553               /* If this is a global symbol, we count the number of PC
    554                  relative relocations we have entered for this symbol,
    555                  so that we can discard them later as necessary.  Note
    556                  that this function is only called if we are using an
    557                  elf64_x86_64 linker hash table, which means that h is
    558                  really a pointer to an elf64_x86_64_link_hash_entry.  */
    559               if (h != NULL
    560                   && ((ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8)
    561                       || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16)
    562                       || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)))
     994              /* If this is a global symbol, we count the number of
     995                 relocations we need for this symbol.  */
     996              if (h != NULL)
    563997                {
    564                   struct elf64_x86_64_link_hash_entry *eh;
    565                   struct elf64_x86_64_pcrel_relocs_copied *p;
    566 
    567                   eh = (struct elf64_x86_64_link_hash_entry *) h;
    568 
    569                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
    570                     if (p->section == sreloc)
    571                       break;
    572 
     998                  head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
     999                }
     1000              else
     1001                {
     1002                  /* Track dynamic relocs needed for local syms too.
     1003                     We really need local syms available to do this
     1004                     easily.  Oh well.  */
     1005
     1006                  asection *s;
     1007                  s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
     1008                                                 sec, r_symndx);
     1009                  if (s == NULL)
     1010                    return FALSE;
     1011
     1012                  head = ((struct elf64_x86_64_dyn_relocs **)
     1013                          &elf_section_data (s)->local_dynrel);
     1014                }
     1015
     1016              p = *head;
     1017              if (p == NULL || p->sec != sec)
     1018                {
     1019                  bfd_size_type amt = sizeof *p;
     1020                  p = ((struct elf64_x86_64_dyn_relocs *)
     1021                       bfd_alloc (htab->elf.dynobj, amt));
    5731022                  if (p == NULL)
    574                     {
    575                       p = ((struct elf64_x86_64_pcrel_relocs_copied *)
    576                            bfd_alloc (dynobj, sizeof *p));
    577                       if (p == NULL)
    578                         return false;
    579                       p->next = eh->pcrel_relocs_copied;
    580                       eh->pcrel_relocs_copied = p;
    581                       p->section = sreloc;
    582                       p->count = 0;
    583                     }
    584 
    585                   ++p->count;
     1023                    return FALSE;
     1024                  p->next = *head;
     1025                  *head = p;
     1026                  p->sec = sec;
     1027                  p->count = 0;
     1028                  p->pc_count = 0;
    5861029                }
     1030
     1031              p->count += 1;
     1032              if (r_type == R_X86_64_PC8
     1033                  || r_type == R_X86_64_PC16
     1034                  || r_type == R_X86_64_PC32)
     1035                p->pc_count += 1;
    5871036            }
    5881037          break;
    589         }
    590     }
    591 
    592   return true;
     1038
     1039          /* This relocation describes the C++ object vtable hierarchy.
     1040             Reconstruct it for later use during GC.  */
     1041        case R_X86_64_GNU_VTINHERIT:
     1042          if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
     1043            return FALSE;
     1044          break;
     1045
     1046          /* This relocation describes which C++ vtable entries are actually
     1047             used.  Record for later use during GC.  */
     1048        case R_X86_64_GNU_VTENTRY:
     1049          if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
     1050            return FALSE;
     1051          break;
     1052
     1053        default:
     1054          break;
     1055        }
     1056    }
     1057
     1058  return TRUE;
    5931059}
    5941060
     
    5971063
    5981064static asection *
    599 elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)
    600      bfd *abfd;
     1065elf64_x86_64_gc_mark_hook (sec, info, rel, h, sym)
     1066     asection *sec;
    6011067     struct bfd_link_info *info ATTRIBUTE_UNUSED;
    602      Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
     1068     Elf_Internal_Rela *rel;
    6031069     struct elf_link_hash_entry *h;
    6041070     Elf_Internal_Sym *sym;
     
    6061072  if (h != NULL)
    6071073    {
    608       switch (h->root.type)
    609         {
    610         case bfd_link_hash_defined:
    611         case bfd_link_hash_defweak:
    612           return h->root.u.def.section;
    613 
    614         case bfd_link_hash_common:
    615           return h->root.u.c.p->section;
     1074      switch (ELF64_R_TYPE (rel->r_info))
     1075        {
     1076        case R_X86_64_GNU_VTINHERIT:
     1077        case R_X86_64_GNU_VTENTRY:
     1078          break;
    6161079
    6171080        default:
    618           break;
     1081          switch (h->root.type)
     1082            {
     1083            case bfd_link_hash_defined:
     1084            case bfd_link_hash_defweak:
     1085              return h->root.u.def.section;
     1086
     1087            case bfd_link_hash_common:
     1088              return h->root.u.c.p->section;
     1089
     1090            default:
     1091              break;
     1092            }
    6191093        }
    6201094    }
    6211095  else
    622     {
    623       if (!(elf_bad_symtab (abfd)
    624             && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
    625           && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
    626                 && sym->st_shndx != SHN_COMMON))
    627         {
    628           return bfd_section_from_elf_index (abfd, sym->st_shndx);
    629         }
    630     }
     1096    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
    6311097
    6321098  return NULL;
     
    6351101/* Update the got entry reference counts for the section being removed.  */
    6361102
    637 static boolean
     1103static bfd_boolean
    6381104elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
    6391105     bfd *abfd;
    640      struct bfd_link_info *info ATTRIBUTE_UNUSED;
     1106     struct bfd_link_info *info;
    6411107     asection *sec;
    6421108     const Elf_Internal_Rela *relocs;
     
    6461112  bfd_signed_vma *local_got_refcounts;
    6471113  const Elf_Internal_Rela *rel, *relend;
    648   unsigned long r_symndx;
    649   struct elf_link_hash_entry *h;
    650   bfd *dynobj;
    651   asection *sgot;
    652   asection *srelgot;
     1114
     1115  elf_section_data (sec)->local_dynrel = NULL;
    6531116
    6541117  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
     
    6561119  local_got_refcounts = elf_local_got_refcounts (abfd);
    6571120
    658   dynobj = elf_hash_table (info)->dynobj;
    659   if (dynobj == NULL)
    660     return true;
    661 
    662   sgot = bfd_get_section_by_name (dynobj, ".got");
    663   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
    664 
    6651121  relend = relocs + sec->reloc_count;
    6661122  for (rel = relocs; rel < relend; rel++)
    667     switch (ELF64_R_TYPE (rel->r_info))
    668       {
    669       case R_X86_64_GOT32:
    670       case R_X86_64_GOTPCREL:
    671         r_symndx = ELF64_R_SYM (rel->r_info);
    672         if (r_symndx >= symtab_hdr->sh_info)
    673           {
    674             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    675             if (h->got.refcount > 0)
     1123    {
     1124      unsigned long r_symndx;
     1125      unsigned int r_type;
     1126      struct elf_link_hash_entry *h = NULL;
     1127
     1128      r_symndx = ELF64_R_SYM (rel->r_info);
     1129      if (r_symndx >= symtab_hdr->sh_info)
     1130        {
     1131          struct elf64_x86_64_link_hash_entry *eh;
     1132          struct elf64_x86_64_dyn_relocs **pp;
     1133          struct elf64_x86_64_dyn_relocs *p;
     1134
     1135          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
     1136          eh = (struct elf64_x86_64_link_hash_entry *) h;
     1137
     1138          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
     1139            if (p->sec == sec)
    6761140              {
     1141                /* Everything must go for SEC.  */
     1142                *pp = p->next;
     1143                break;
     1144              }
     1145        }
     1146
     1147      r_type = ELF64_R_TYPE (rel->r_info);
     1148      r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
     1149      switch (r_type)
     1150        {
     1151        case R_X86_64_TLSLD:
     1152          if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
     1153            elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
     1154          break;
     1155
     1156        case R_X86_64_TLSGD:
     1157        case R_X86_64_GOTTPOFF:
     1158        case R_X86_64_GOT32:
     1159        case R_X86_64_GOTPCREL:
     1160          if (h != NULL)
     1161            {
     1162              if (h->got.refcount > 0)
    6771163                h->got.refcount -= 1;
    678                 if (h->got.refcount == 0)
    679                   {
    680                     sgot->_raw_size -= GOT_ENTRY_SIZE;
    681                     srelgot->_raw_size -= sizeof (Elf64_External_Rela);
    682                   }
    683               }
    684           }
    685         else if (local_got_refcounts != NULL)
    686           {
    687             if (local_got_refcounts[r_symndx] > 0)
    688               {
     1164            }
     1165          else if (local_got_refcounts != NULL)
     1166            {
     1167              if (local_got_refcounts[r_symndx] > 0)
    6891168                local_got_refcounts[r_symndx] -= 1;
    690                 if (local_got_refcounts[r_symndx] == 0)
    691                   {
    692                     sgot->_raw_size -= GOT_ENTRY_SIZE;
    693                     if (info->shared)
    694                       srelgot->_raw_size -= sizeof (Elf64_External_Rela);
    695                   }
    696               }
    697           }
    698         break;
    699 
    700       case R_X86_64_PLT32:
    701         r_symndx = ELF64_R_SYM (rel->r_info);
    702         if (r_symndx >= symtab_hdr->sh_info)
    703           {
    704             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    705             if (h->plt.refcount > 0)
    706               h->plt.refcount -= 1;
    707           }
    708         break;
    709 
    710       default:
    711         break;
    712       }
    713 
    714   return true;
     1169            }
     1170          break;
     1171
     1172        case R_X86_64_8:
     1173        case R_X86_64_16:
     1174        case R_X86_64_32:
     1175        case R_X86_64_64:
     1176        case R_X86_64_32S:
     1177        case R_X86_64_PC8:
     1178        case R_X86_64_PC16:
     1179        case R_X86_64_PC32:
     1180          if (info->shared)
     1181            break;
     1182          /* Fall thru */
     1183
     1184        case R_X86_64_PLT32:
     1185          if (h != NULL)
     1186            {
     1187              if (h->plt.refcount > 0)
     1188                h->plt.refcount -= 1;
     1189            }
     1190          break;
     1191
     1192        default:
     1193          break;
     1194        }
     1195    }
     1196
     1197  return TRUE;
    7151198}
    7161199
     
    7211204   understand.  */
    7221205
    723 static boolean
     1206static bfd_boolean
    7241207elf64_x86_64_adjust_dynamic_symbol (info, h)
    7251208     struct bfd_link_info *info;
    7261209     struct elf_link_hash_entry *h;
    7271210{
    728   bfd *dynobj;
     1211  struct elf64_x86_64_link_hash_table *htab;
    7291212  asection *s;
    7301213  unsigned int power_of_two;
    731 
    732   dynobj = elf_hash_table (info)->dynobj;
    733 
    734   /* Make sure we know what is going on here.  */
    735   BFD_ASSERT (dynobj != NULL
    736               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
    737                   || h->weakdef != NULL
    738                   || ((h->elf_link_hash_flags
    739                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
    740                       && (h->elf_link_hash_flags
    741                           & ELF_LINK_HASH_REF_REGULAR) != 0
    742                       && (h->elf_link_hash_flags
    743                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
    7441214
    7451215  /* If this is a function, put it in the procedure linkage table.  We
     
    7491219      || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
    7501220    {
    751       if ((! info->shared
    752            && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
    753            && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
    754           || (info->shared && h->plt.refcount <= 0))
     1221      if (h->plt.refcount <= 0
     1222          || (! info->shared
     1223              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
     1224              && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
     1225              && h->root.type != bfd_link_hash_undefweak
     1226              && h->root.type != bfd_link_hash_undefined))
    7551227        {
    7561228          /* This case can occur if we saw a PLT32 reloc in an input
     
    7611233          h->plt.offset = (bfd_vma) -1;
    7621234          h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
    763           return true;
    764         }
    765 
    766       /* Make sure this symbol is output as a dynamic symbol.  */
    767       if (h->dynindx == -1)
    768         {
    769           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
    770             return false;
    771         }
    772 
    773       s = bfd_get_section_by_name (dynobj, ".plt");
    774       BFD_ASSERT (s != NULL);
    775 
    776       /* If this is the first .plt entry, make room for the special
    777          first entry.  */
    778       if (s->_raw_size == 0)
    779         s->_raw_size = PLT_ENTRY_SIZE;
    780 
    781       /* If this symbol is not defined in a regular file, and we are
    782          not generating a shared library, then set the symbol to this
    783          location in the .plt.  This is required to make function
    784          pointers compare as equal between the normal executable and
    785          the shared library.  */
    786       if (! info->shared
    787           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
    788         {
    789           h->root.u.def.section = s;
    790           h->root.u.def.value = s->_raw_size;
    791         }
    792 
    793       h->plt.offset = s->_raw_size;
    794 
    795       /* Make room for this entry.  */
    796       s->_raw_size += PLT_ENTRY_SIZE;
    797 
    798       /* We also need to make an entry in the .got.plt section, which
    799          will be placed in the .got section by the linker script.  */
    800       s = bfd_get_section_by_name (dynobj, ".got.plt");
    801       BFD_ASSERT (s != NULL);
    802       s->_raw_size += GOT_ENTRY_SIZE;
    803 
    804       /* We also need to make an entry in the .rela.plt section.  */
    805       s = bfd_get_section_by_name (dynobj, ".rela.plt");
    806       BFD_ASSERT (s != NULL);
    807       s->_raw_size += sizeof (Elf64_External_Rela);
    808 
    809       return true;
    810     }
     1235        }
     1236
     1237      return TRUE;
     1238    }
     1239  else
     1240    /* It's possible that we incorrectly decided a .plt reloc was
     1241       needed for an R_X86_64_PC32 reloc to a non-function sym in
     1242       check_relocs.  We can't decide accurately between function and
     1243       non-function syms in check-relocs;  Objects loaded later in
     1244       the link may change h->type.  So fix it now.  */
     1245    h->plt.offset = (bfd_vma) -1;
    8111246
    8121247  /* If this is a weak symbol, and there is a real definition, the
     
    8191254      h->root.u.def.section = h->weakdef->root.u.def.section;
    8201255      h->root.u.def.value = h->weakdef->root.u.def.value;
    821       return true;
     1256      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
     1257        h->elf_link_hash_flags
     1258          = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
     1259             | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
     1260      return TRUE;
    8221261    }
    8231262
     
    8301269     be handled correctly by relocate_section.  */
    8311270  if (info->shared)
    832     return true;
     1271    return TRUE;
    8331272
    8341273  /* If there are no references to this symbol that do not use the
    8351274     GOT, we don't need to generate a copy reloc.  */
    8361275  if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
    837     return true;
     1276    return TRUE;
     1277
     1278  /* If -z nocopyreloc was given, we won't generate them either.  */
     1279  if (info->nocopyreloc)
     1280    {
     1281      h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
     1282      return TRUE;
     1283    }
     1284
     1285  if (ELIMINATE_COPY_RELOCS)
     1286    {
     1287      struct elf64_x86_64_link_hash_entry * eh;
     1288      struct elf64_x86_64_dyn_relocs *p;
     1289
     1290      eh = (struct elf64_x86_64_link_hash_entry *) h;
     1291      for (p = eh->dyn_relocs; p != NULL; p = p->next)
     1292        {
     1293          s = p->sec->output_section;
     1294          if (s != NULL && (s->flags & SEC_READONLY) != 0)
     1295            break;
     1296        }
     1297
     1298      /* If we didn't find any dynamic relocs in read-only sections, then
     1299         we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
     1300      if (p == NULL)
     1301        {
     1302          h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
     1303          return TRUE;
     1304        }
     1305    }
    8381306
    8391307  /* We must allocate the symbol in our .dynbss section, which will
     
    8471315     same memory location for the variable.  */
    8481316
    849   s = bfd_get_section_by_name (dynobj, ".dynbss");
    850   BFD_ASSERT (s != NULL);
     1317  htab = elf64_x86_64_hash_table (info);
    8511318
    8521319  /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
    8531320     to copy the initial value out of the dynamic object and into the
    854      runtime process image.  We need to remember the offset into the
    855      .rela.bss section we are going to use.  */
     1321     runtime process image.  */
    8561322  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
    8571323    {
    858       asection *srel;
    859 
    860       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
    861       BFD_ASSERT (srel != NULL);
    862       srel->_raw_size += sizeof (Elf64_External_Rela);
     1324      htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
    8631325      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
    8641326    }
     
    8741336
    8751337  /* Apply the required alignment.  */
     1338  s = htab->sdynbss;
    8761339  s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
    877   if (power_of_two > bfd_get_section_alignment (dynobj, s))
    878     {
    879       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
    880         return false;
     1340  if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
     1341    {
     1342      if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
     1343        return FALSE;
    8811344    }
    8821345
     
    8881351  s->_raw_size += h->size;
    8891352
    890   return true;
     1353  return TRUE;
    8911354}
    8921355
     1356/* This is the condition under which elf64_x86_64_finish_dynamic_symbol
     1357   will be called from elflink.h.  If elflink.h doesn't call our
     1358   finish_dynamic_symbol routine, we'll need to do something about
     1359   initializing any .plt and .got entries in elf64_x86_64_relocate_section.  */
     1360#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
     1361  ((DYN)                                                                \
     1362   && ((INFO)->shared                                                   \
     1363       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
     1364   && ((H)->dynindx != -1                                               \
     1365       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
     1366
     1367/* Allocate space in .plt, .got and associated reloc sections for
     1368   dynamic relocs.  */
     1369
     1370static bfd_boolean
     1371allocate_dynrelocs (h, inf)
     1372     struct elf_link_hash_entry *h;
     1373     PTR inf;
     1374{
     1375  struct bfd_link_info *info;
     1376  struct elf64_x86_64_link_hash_table *htab;
     1377  struct elf64_x86_64_link_hash_entry *eh;
     1378  struct elf64_x86_64_dyn_relocs *p;
     1379
     1380  if (h->root.type == bfd_link_hash_indirect)
     1381    return TRUE;
     1382
     1383  if (h->root.type == bfd_link_hash_warning)
     1384    h = (struct elf_link_hash_entry *) h->root.u.i.link;
     1385
     1386  info = (struct bfd_link_info *) inf;
     1387  htab = elf64_x86_64_hash_table (info);
     1388
     1389  if (htab->elf.dynamic_sections_created
     1390      && h->plt.refcount > 0)
     1391    {
     1392      /* Make sure this symbol is output as a dynamic symbol.
     1393         Undefined weak syms won't yet be marked as dynamic.  */
     1394      if (h->dynindx == -1
     1395          && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
     1396        {
     1397          if (! bfd_elf64_link_record_dynamic_symbol (info, h))
     1398            return FALSE;
     1399        }
     1400
     1401      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
     1402        {
     1403          asection *s = htab->splt;
     1404
     1405          /* If this is the first .plt entry, make room for the special
     1406             first entry.  */
     1407          if (s->_raw_size == 0)
     1408            s->_raw_size += PLT_ENTRY_SIZE;
     1409
     1410          h->plt.offset = s->_raw_size;
     1411
     1412          /* If this symbol is not defined in a regular file, and we are
     1413             not generating a shared library, then set the symbol to this
     1414             location in the .plt.  This is required to make function
     1415             pointers compare as equal between the normal executable and
     1416             the shared library.  */
     1417          if (! info->shared
     1418              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
     1419            {
     1420              h->root.u.def.section = s;
     1421              h->root.u.def.value = h->plt.offset;
     1422            }
     1423
     1424          /* Make room for this entry.  */
     1425          s->_raw_size += PLT_ENTRY_SIZE;
     1426
     1427          /* We also need to make an entry in the .got.plt section, which
     1428             will be placed in the .got section by the linker script.  */
     1429          htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
     1430
     1431          /* We also need to make an entry in the .rela.plt section.  */
     1432          htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
     1433        }
     1434      else
     1435        {
     1436          h->plt.offset = (bfd_vma) -1;
     1437          h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
     1438        }
     1439    }
     1440  else
     1441    {
     1442      h->plt.offset = (bfd_vma) -1;
     1443      h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
     1444    }
     1445
     1446  /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
     1447     make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
     1448  if (h->got.refcount > 0
     1449      && !info->shared
     1450      && h->dynindx == -1
     1451      && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
     1452    h->got.offset = (bfd_vma) -1;
     1453  else if (h->got.refcount > 0)
     1454    {
     1455      asection *s;
     1456      bfd_boolean dyn;
     1457      int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
     1458
     1459      /* Make sure this symbol is output as a dynamic symbol.
     1460         Undefined weak syms won't yet be marked as dynamic.  */
     1461      if (h->dynindx == -1
     1462          && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
     1463        {
     1464          if (! bfd_elf64_link_record_dynamic_symbol (info, h))
     1465            return FALSE;
     1466        }
     1467
     1468      s = htab->sgot;
     1469      h->got.offset = s->_raw_size;
     1470      s->_raw_size += GOT_ENTRY_SIZE;
     1471      /* R_X86_64_TLSGD needs 2 consecutive GOT slots.  */
     1472      if (tls_type == GOT_TLS_GD)
     1473        s->_raw_size += GOT_ENTRY_SIZE;
     1474      dyn = htab->elf.dynamic_sections_created;
     1475      /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
     1476         and two if global.
     1477         R_X86_64_GOTTPOFF needs one dynamic relocation.  */
     1478      if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
     1479          || tls_type == GOT_TLS_IE)
     1480        htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
     1481      else if (tls_type == GOT_TLS_GD)
     1482        htab->srelgot->_raw_size += 2 * sizeof (Elf64_External_Rela);
     1483      else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
     1484        htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
     1485    }
     1486  else
     1487    h->got.offset = (bfd_vma) -1;
     1488
     1489  eh = (struct elf64_x86_64_link_hash_entry *) h;
     1490  if (eh->dyn_relocs == NULL)
     1491    return TRUE;
     1492
     1493  /* In the shared -Bsymbolic case, discard space allocated for
     1494     dynamic pc-relative relocs against symbols which turn out to be
     1495     defined in regular objects.  For the normal shared case, discard
     1496     space for pc-relative relocs that have become local due to symbol
     1497     visibility changes.  */
     1498
     1499  if (info->shared)
     1500    {
     1501      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
     1502          && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
     1503              || info->symbolic))
     1504        {
     1505          struct elf64_x86_64_dyn_relocs **pp;
     1506
     1507          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
     1508            {
     1509              p->count -= p->pc_count;
     1510              p->pc_count = 0;
     1511              if (p->count == 0)
     1512                *pp = p->next;
     1513              else
     1514                pp = &p->next;
     1515            }
     1516        }
     1517    }
     1518  else if (ELIMINATE_COPY_RELOCS)
     1519    {
     1520      /* For the non-shared case, discard space for relocs against
     1521         symbols which turn out to need copy relocs or are not
     1522         dynamic.  */
     1523
     1524      if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
     1525          && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
     1526               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
     1527              || (htab->elf.dynamic_sections_created
     1528                  && (h->root.type == bfd_link_hash_undefweak
     1529                      || h->root.type == bfd_link_hash_undefined))))
     1530        {
     1531          /* Make sure this symbol is output as a dynamic symbol.
     1532             Undefined weak syms won't yet be marked as dynamic.  */
     1533          if (h->dynindx == -1
     1534              && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
     1535            {
     1536              if (! bfd_elf64_link_record_dynamic_symbol (info, h))
     1537                return FALSE;
     1538            }
     1539
     1540          /* If that succeeded, we know we'll be keeping all the
     1541             relocs.  */
     1542          if (h->dynindx != -1)
     1543            goto keep;
     1544        }
     1545
     1546      eh->dyn_relocs = NULL;
     1547
     1548    keep: ;
     1549    }
     1550
     1551  /* Finally, allocate space.  */
     1552  for (p = eh->dyn_relocs; p != NULL; p = p->next)
     1553    {
     1554      asection *sreloc = elf_section_data (p->sec)->sreloc;
     1555      sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
     1556    }
     1557
     1558  return TRUE;
     1559}
     1560
     1561/* Find any dynamic relocs that apply to read-only sections.  */
     1562
     1563static bfd_boolean
     1564readonly_dynrelocs (h, inf)
     1565     struct elf_link_hash_entry *h;
     1566     PTR inf;
     1567{
     1568  struct elf64_x86_64_link_hash_entry *eh;
     1569  struct elf64_x86_64_dyn_relocs *p;
     1570
     1571  if (h->root.type == bfd_link_hash_warning)
     1572    h = (struct elf_link_hash_entry *) h->root.u.i.link;
     1573
     1574  eh = (struct elf64_x86_64_link_hash_entry *) h;
     1575  for (p = eh->dyn_relocs; p != NULL; p = p->next)
     1576    {
     1577      asection *s = p->sec->output_section;
     1578
     1579      if (s != NULL && (s->flags & SEC_READONLY) != 0)
     1580        {
     1581          struct bfd_link_info *info = (struct bfd_link_info *) inf;
     1582
     1583          info->flags |= DF_TEXTREL;
     1584
     1585          /* Not an error, just cut short the traversal.  */
     1586          return FALSE;
     1587        }
     1588    }
     1589  return TRUE;
     1590}
     1591
    8931592/* Set the sizes of the dynamic sections.  */
    8941593
    895 static boolean
     1594static bfd_boolean
    8961595elf64_x86_64_size_dynamic_sections (output_bfd, info)
    897      bfd *output_bfd;
     1596     bfd *output_bfd ATTRIBUTE_UNUSED;
    8981597     struct bfd_link_info *info;
    8991598{
     1599  struct elf64_x86_64_link_hash_table *htab;
    9001600  bfd *dynobj;
    9011601  asection *s;
    902   boolean plt;
    903   boolean relocs;
    904   boolean reltext;
    905 
    906   dynobj = elf_hash_table (info)->dynobj;
    907   BFD_ASSERT (dynobj != NULL);
    908 
    909   if (elf_hash_table (info)->dynamic_sections_created)
     1602  bfd_boolean relocs;
     1603  bfd *ibfd;
     1604
     1605  htab = elf64_x86_64_hash_table (info);
     1606  dynobj = htab->elf.dynobj;
     1607  if (dynobj == NULL)
     1608    abort ();
     1609
     1610  if (htab->elf.dynamic_sections_created)
    9101611    {
    9111612      /* Set the contents of the .interp section to the interpreter.  */
     
    9131614        {
    9141615          s = bfd_get_section_by_name (dynobj, ".interp");
    915           BFD_ASSERT (s != NULL);
     1616          if (s == NULL)
     1617            abort ();
    9161618          s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
    9171619          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
    9181620        }
    9191621    }
     1622
     1623  /* Set up .got offsets for local syms, and space for local dynamic
     1624     relocs.  */
     1625  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
     1626    {
     1627      bfd_signed_vma *local_got;
     1628      bfd_signed_vma *end_local_got;
     1629      char *local_tls_type;
     1630      bfd_size_type locsymcount;
     1631      Elf_Internal_Shdr *symtab_hdr;
     1632      asection *srel;
     1633
     1634      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
     1635        continue;
     1636
     1637      for (s = ibfd->sections; s != NULL; s = s->next)
     1638        {
     1639          struct elf64_x86_64_dyn_relocs *p;
     1640
     1641          for (p = *((struct elf64_x86_64_dyn_relocs **)
     1642                     &elf_section_data (s)->local_dynrel);
     1643               p != NULL;
     1644               p = p->next)
     1645            {
     1646              if (!bfd_is_abs_section (p->sec)
     1647                  && bfd_is_abs_section (p->sec->output_section))
     1648                {
     1649                  /* Input section has been discarded, either because
     1650                     it is a copy of a linkonce section or due to
     1651                     linker script /DISCARD/, so we'll be discarding
     1652                     the relocs too.  */
     1653                }
     1654              else if (p->count != 0)
     1655                {
     1656                  srel = elf_section_data (p->sec)->sreloc;
     1657                  srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
     1658                  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
     1659                    info->flags |= DF_TEXTREL;
     1660
     1661                }
     1662            }
     1663        }
     1664
     1665      local_got = elf_local_got_refcounts (ibfd);
     1666      if (!local_got)
     1667        continue;
     1668
     1669      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
     1670      locsymcount = symtab_hdr->sh_info;
     1671      end_local_got = local_got + locsymcount;
     1672      local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
     1673      s = htab->sgot;
     1674      srel = htab->srelgot;
     1675      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
     1676        {
     1677          if (*local_got > 0)
     1678            {
     1679              *local_got = s->_raw_size;
     1680              s->_raw_size += GOT_ENTRY_SIZE;
     1681              if (*local_tls_type == GOT_TLS_GD)
     1682                s->_raw_size += GOT_ENTRY_SIZE;
     1683              if (info->shared
     1684                  || *local_tls_type == GOT_TLS_GD
     1685                  || *local_tls_type == GOT_TLS_IE)
     1686                srel->_raw_size += sizeof (Elf64_External_Rela);
     1687            }
     1688          else
     1689            *local_got = (bfd_vma) -1;
     1690        }
     1691    }
     1692
     1693  if (htab->tls_ld_got.refcount > 0)
     1694    {
     1695      /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
     1696         relocs.  */
     1697      htab->tls_ld_got.offset = htab->sgot->_raw_size;
     1698      htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE;
     1699      htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
     1700    }
    9201701  else
    921     {
    922       /* We may have created entries in the .rela.got section.
    923          However, if we are not creating the dynamic sections, we will
    924          not actually use these entries.  Reset the size of .rela.got,
    925          which will cause it to get stripped from the output file
    926          below.  */
    927       s = bfd_get_section_by_name (dynobj, ".rela.got");
    928       if (s != NULL)
    929         s->_raw_size = 0;
    930     }
    931 
    932   /* If this is a -Bsymbolic shared link, then we need to discard all
    933      PC relative relocs against symbols defined in a regular object.
    934      We allocated space for them in the check_relocs routine, but we
    935      will not fill them in in the relocate_section routine.  */
    936   if (info->shared)
    937     elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),
    938                                      elf64_x86_64_discard_copies,
    939                                      (PTR) info);
    940 
    941   /* The check_relocs and adjust_dynamic_symbol entry points have
    942      determined the sizes of the various dynamic sections.  Allocate
    943      memory for them.  */
    944   plt = relocs = reltext = false;
     1702    htab->tls_ld_got.offset = -1;
     1703
     1704  /* Allocate global sym .plt and .got entries, and space for global
     1705     sym dynamic relocs.  */
     1706  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
     1707
     1708  /* We now have determined the sizes of the various dynamic sections.
     1709     Allocate memory for them.  */
     1710  relocs = FALSE;
    9451711  for (s = dynobj->sections; s != NULL; s = s->next)
    9461712    {
    947       const char *name;
    948       boolean strip;
    949 
    9501713      if ((s->flags & SEC_LINKER_CREATED) == 0)
    9511714        continue;
    9521715
    953       /* It's OK to base decisions on the section name, because none
    954          of the dynobj section names depend upon the input files.  */
    955       name = bfd_get_section_name (dynobj, s);
    956 
    957       strip = false;
    958       if (strcmp (name, ".plt") == 0)
    959         {
    960           if (s->_raw_size == 0)
    961             {
    962               /* Strip this section if we don't need it; see the
    963                  comment below.  */
    964               strip = true;
    965             }
    966           else
    967             {
    968               /* Remember whether there is a PLT.  */
    969               plt = true;
    970             }
    971         }
    972       else if (strncmp (name, ".rela", 5) == 0)
    973         {
    974           if (s->_raw_size == 0)
    975             {
    976               /* If we don't need this section, strip it from the
    977                  output file.  This is mostly to handle .rela.bss and
    978                  .rela.plt.  We must create both sections in
    979                  create_dynamic_sections, because they must be created
    980                  before the linker maps input sections to output
    981                  sections.  The linker does that before
    982                  adjust_dynamic_symbol is called, and it is that
    983                  function which decides whether anything needs to go
    984                  into these sections.  */
    985               strip = true;
    986             }
    987           else
    988             {
    989               asection *target;
    990 
    991               /* Remember whether there are any reloc sections other
    992                  than .rela.plt.  */
    993               if (strcmp (name, ".rela.plt") != 0)
    994                 {
    995                   const char *outname;
    996 
    997                   relocs = true;
    998 
    999                   /* If this relocation section applies to a read only
    1000                      section, then we probably need a DT_TEXTREL
    1001                      entry.  The entries in the .rela.plt section
    1002                      really apply to the .got section, which we
    1003                      created ourselves and so know is not readonly.  */
    1004                   outname = bfd_get_section_name (output_bfd,
    1005                                                   s->output_section);
    1006                   target = bfd_get_section_by_name (output_bfd, outname + 5);
    1007                   if (target != NULL
    1008                       && (target->flags & SEC_READONLY) != 0
    1009                       && (target->flags & SEC_ALLOC) != 0)
    1010                     reltext = true;
    1011                 }
    1012 
    1013               /* We use the reloc_count field as a counter if we need
    1014                  to copy relocs into the output file.  */
    1015               s->reloc_count = 0;
    1016             }
    1017         }
    1018       else if (strncmp (name, ".got", 4) != 0)
     1716      if (s == htab->splt
     1717          || s == htab->sgot
     1718          || s == htab->sgotplt)
     1719        {
     1720          /* Strip this section if we don't need it; see the
     1721             comment below.  */
     1722        }
     1723      else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
     1724        {
     1725          if (s->_raw_size != 0 && s != htab->srelplt)
     1726            relocs = TRUE;
     1727
     1728          /* We use the reloc_count field as a counter if we need
     1729             to copy relocs into the output file.  */
     1730          s->reloc_count = 0;
     1731        }
     1732      else
    10191733        {
    10201734          /* It's not one of our sections, so don't allocate space.  */
     
    10221736        }
    10231737
    1024       if (strip)
    1025         {
     1738      if (s->_raw_size == 0)
     1739        {
     1740          /* If we don't need this section, strip it from the
     1741             output file.  This is mostly to handle .rela.bss and
     1742             .rela.plt.  We must create both sections in
     1743             create_dynamic_sections, because they must be created
     1744             before the linker maps input sections to output
     1745             sections.  The linker does that before
     1746             adjust_dynamic_symbol is called, and it is that
     1747             function which decides whether anything needs to go
     1748             into these sections.  */
     1749
    10261750          _bfd_strip_section_from_output (info, s);
    10271751          continue;
     
    10341758         of garbage.  */
    10351759      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
    1036       if (s->contents == NULL && s->_raw_size != 0)
    1037         return false;
    1038     }
    1039 
    1040   if (elf_hash_table (info)->dynamic_sections_created)
     1760      if (s->contents == NULL)
     1761        return FALSE;
     1762    }
     1763
     1764  if (htab->elf.dynamic_sections_created)
    10411765    {
    10421766      /* Add some entries to the .dynamic section.  We fill in the
     
    10451769         the .dynamic section.  The DT_DEBUG entry is filled in by the
    10461770         dynamic linker and used by the debugger.  */
     1771#define add_dynamic_entry(TAG, VAL) \
     1772  bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
     1773
    10471774      if (! info->shared)
    10481775        {
    1049           if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
    1050             return false;
    1051         }
    1052 
    1053       if (plt)
    1054         {
    1055           if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
    1056               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
    1057               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
    1058               || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
    1059             return false;
     1776          if (!add_dynamic_entry (DT_DEBUG, 0))
     1777            return FALSE;
     1778        }
     1779
     1780      if (htab->splt->_raw_size != 0)
     1781        {
     1782          if (!add_dynamic_entry (DT_PLTGOT, 0)
     1783              || !add_dynamic_entry (DT_PLTRELSZ, 0)
     1784              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
     1785              || !add_dynamic_entry (DT_JMPREL, 0))
     1786            return FALSE;
    10601787        }
    10611788
    10621789      if (relocs)
    10631790        {
    1064           if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
    1065               || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
    1066               || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
    1067                                                 sizeof (Elf64_External_Rela)))
    1068             return false;
    1069         }
    1070 
    1071       if (reltext)
    1072         {
    1073           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
    1074             return false;
    1075           info->flags |= DF_TEXTREL;
    1076         }
    1077     }
    1078 
    1079   return true;
     1791          if (!add_dynamic_entry (DT_RELA, 0)
     1792              || !add_dynamic_entry (DT_RELASZ, 0)
     1793              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
     1794            return FALSE;
     1795
     1796          /* If any dynamic relocs apply to a read-only section,
     1797             then we need a DT_TEXTREL entry.  */
     1798          if ((info->flags & DF_TEXTREL) == 0)
     1799            elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
     1800                                    (PTR) info);
     1801
     1802          if ((info->flags & DF_TEXTREL) != 0)
     1803            {
     1804              if (!add_dynamic_entry (DT_TEXTREL, 0))
     1805                return FALSE;
     1806            }
     1807        }
     1808    }
     1809#undef add_dynamic_entry
     1810
     1811  return TRUE;
    10801812}
    10811813
    1082 /* This function is called via elf64_x86_64_link_hash_traverse if we are
    1083    creating a shared object.  In the -Bsymbolic case, it discards the
    1084    space allocated to copy PC relative relocs against symbols which
    1085    are defined in regular objects.  For the normal non-symbolic case,
    1086    we also discard space for relocs that have become local due to
    1087    symbol visibility changes.  We allocated space for them in the
    1088    check_relocs routine, but we won't fill them in in the
    1089    relocate_section routine.  */
    1090 
    1091 static boolean
    1092 elf64_x86_64_discard_copies (h, inf)
    1093      struct elf64_x86_64_link_hash_entry *h;
    1094      PTR inf;
    1095 {
    1096   struct elf64_x86_64_pcrel_relocs_copied *s;
    1097   struct bfd_link_info *info = (struct bfd_link_info *) inf;
    1098 
    1099   /* If a symbol has been forced local or we have found a regular
    1100      definition for the symbolic link case, then we won't be needing
    1101      any relocs.  */
    1102   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
    1103       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
    1104           || info->symbolic))
    1105     {
    1106       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
    1107         s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
    1108     }
    1109 
    1110   return true;
     1814/* Return the base VMA address which should be subtracted from real addresses
     1815   when resolving @dtpoff relocation.
     1816   This is PT_TLS segment p_vaddr.  */
     1817
     1818static bfd_vma
     1819dtpoff_base (info)
     1820     struct bfd_link_info *info;
     1821{
     1822  /* If tls_segment is NULL, we should have signalled an error already.  */
     1823  if (elf_hash_table (info)->tls_segment == NULL)
     1824    return 0;
     1825  return elf_hash_table (info)->tls_segment->start;
    11111826}
    11121827
     1828/* Return the relocation value for @tpoff relocation
     1829   if STT_TLS virtual address is ADDRESS.  */
     1830
     1831static bfd_vma
     1832tpoff (info, address)
     1833     struct bfd_link_info *info;
     1834     bfd_vma address;
     1835{
     1836  struct elf_link_tls_segment *tls_segment
     1837    = elf_hash_table (info)->tls_segment;
     1838
     1839  /* If tls_segment is NULL, we should have signalled an error already.  */
     1840  if (tls_segment == NULL)
     1841    return 0;
     1842  return address - align_power (tls_segment->size, tls_segment->align)
     1843         - tls_segment->start;
     1844}
     1845
    11131846/* Relocate an x86_64 ELF section.  */
    11141847
    1115 static boolean
     1848static bfd_boolean
    11161849elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
    11171850                               contents, relocs, local_syms, local_sections)
     
    11251858     asection **local_sections;
    11261859{
    1127   bfd *dynobj;
     1860  struct elf64_x86_64_link_hash_table *htab;
    11281861  Elf_Internal_Shdr *symtab_hdr;
    11291862  struct elf_link_hash_entry **sym_hashes;
    11301863  bfd_vma *local_got_offsets;
    1131   asection *sgot;
    1132   asection *splt;
    1133   asection *sreloc;
    1134   Elf_Internal_Rela *rela;
     1864  Elf_Internal_Rela *rel;
    11351865  Elf_Internal_Rela *relend;
    11361866
    1137   dynobj = elf_hash_table (info)->dynobj;
     1867  if (info->relocateable)
     1868    return TRUE;
     1869
     1870  htab = elf64_x86_64_hash_table (info);
    11381871  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    11391872  sym_hashes = elf_sym_hashes (input_bfd);
    11401873  local_got_offsets = elf_local_got_offsets (input_bfd);
    11411874
    1142   sreloc = splt = sgot = NULL;
    1143   if (dynobj != NULL)
    1144     {
    1145       splt = bfd_get_section_by_name (dynobj, ".plt");
    1146       sgot = bfd_get_section_by_name (dynobj, ".got");
    1147     }
    1148 
    1149   rela = relocs;
     1875  rel = relocs;
    11501876  relend = relocs + input_section->reloc_count;
    1151   for (; rela < relend; rela++)
    1152     {
    1153       int r_type;
     1877  for (; rel < relend; rel++)
     1878    {
     1879      unsigned int r_type;
    11541880      reloc_howto_type *howto;
    11551881      unsigned long r_symndx;
     
    11571883      Elf_Internal_Sym *sym;
    11581884      asection *sec;
     1885      bfd_vma off;
    11591886      bfd_vma relocation;
     1887      bfd_boolean unresolved_reloc;
    11601888      bfd_reloc_status_type r;
    1161       unsigned int indx;
    1162 
    1163       r_type = ELF64_R_TYPE (rela->r_info);
    1164 
    1165       if ((indx = (unsigned) r_type) >= R_X86_64_max)
     1889      int tls_type;
     1890
     1891      r_type = ELF64_R_TYPE (rel->r_info);
     1892      if (r_type == (int) R_X86_64_GNU_VTINHERIT
     1893          || r_type == (int) R_X86_64_GNU_VTENTRY)
     1894        continue;
     1895
     1896      if (r_type >= R_X86_64_max)
    11661897        {
    11671898          bfd_set_error (bfd_error_bad_value);
    1168           return false;
    1169         }
    1170       howto = x86_64_elf_howto_table + indx;
    1171 
    1172       r_symndx = ELF64_R_SYM (rela->r_info);
    1173 
    1174       if (info->relocateable)
    1175         {
    1176           /* This is a relocateable link.  We don't have to change
    1177              anything, unless the reloc is against a section symbol,
    1178              in which case we have to adjust according to where the
    1179              section symbol winds up in the output section.  */
    1180           if (r_symndx < symtab_hdr->sh_info)
    1181             {
    1182               sym = local_syms + r_symndx;
    1183               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
    1184                 {
    1185                   sec = local_sections[r_symndx];
    1186                   rela->r_addend += sec->output_offset + sym->st_value;
    1187                 }
    1188             }
    1189 
    1190           continue;
    1191         }
    1192 
    1193       /* This is a final link.  */
     1899          return FALSE;
     1900        }
     1901
     1902      howto = x86_64_elf_howto_table + r_type;
     1903      r_symndx = ELF64_R_SYM (rel->r_info);
    11941904      h = NULL;
    11951905      sym = NULL;
    11961906      sec = NULL;
     1907      unresolved_reloc = FALSE;
    11971908      if (r_symndx < symtab_hdr->sh_info)
    11981909        {
    11991910          sym = local_syms + r_symndx;
    12001911          sec = local_sections[r_symndx];
    1201           relocation = (sec->output_section->vma
    1202                         + sec->output_offset
    1203                         + sym->st_value);
     1912
     1913          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
    12041914        }
    12051915      else
     
    12091919                 || h->root.type == bfd_link_hash_warning)
    12101920            h = (struct elf_link_hash_entry *) h->root.u.i.link;
     1921
    12111922          if (h->root.type == bfd_link_hash_defined
    12121923              || h->root.type == bfd_link_hash_defweak)
    12131924            {
    12141925              sec = h->root.u.def.section;
    1215               if ((r_type == R_X86_64_PLT32
    1216                    && splt != NULL
    1217                    && h->plt.offset != (bfd_vma) -1)
    1218                   || ((r_type == R_X86_64_GOT32 || r_type == R_X86_64_GOTPCREL)
    1219                       && elf_hash_table (info)->dynamic_sections_created
    1220                       && (!info->shared
    1221                           || (! info->symbolic && h->dynindx != -1)
    1222                           || (h->elf_link_hash_flags
    1223                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
    1224                   || (info->shared
    1225                       && ((! info->symbolic && h->dynindx != -1)
    1226                           || (h->elf_link_hash_flags
    1227                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
    1228                       && (r_type == R_X86_64_8
    1229                           || r_type == R_X86_64_16
    1230                           || r_type == R_X86_64_32
    1231                           || r_type == R_X86_64_64
    1232                           || r_type == R_X86_64_PC8
    1233                           || r_type == R_X86_64_PC16
    1234                           || r_type == R_X86_64_PC32)
    1235                       && ((input_section->flags & SEC_ALLOC) != 0
    1236                           /* DWARF will emit R_X86_64_32 relocations in its
    1237                              sections against symbols defined externally
    1238                              in shared libraries.  We can't do anything
    1239                              with them here.  */
    1240                           || ((input_section->flags & SEC_DEBUGGING) != 0
    1241                               && (h->elf_link_hash_flags
    1242                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
     1926              if (sec->output_section == NULL)
    12431927                {
    1244                   /* In these cases, we don't need the relocation
    1245                      value.  We check specially because in some
    1246                      obscure cases sec->output_section will be NULL.  */
    1247                   relocation = 0;
    1248                 }
    1249               else if (sec->output_section == NULL)
    1250                 {
    1251                   (*_bfd_error_handler)
    1252                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
    1253                      bfd_get_filename (input_bfd), h->root.root.string,
    1254                      bfd_get_section_name (input_bfd, input_section));
     1928                  /* Set a flag that will be cleared later if we find a
     1929                     relocation value for this symbol.  output_section
     1930                     is typically NULL for symbols satisfied by a shared
     1931                     library.  */
     1932                  unresolved_reloc = TRUE;
    12551933                  relocation = 0;
    12561934                }
     
    12621940          else if (h->root.type == bfd_link_hash_undefweak)
    12631941            relocation = 0;
    1264           else if (info->shared && !info->symbolic && !info->no_undefined
     1942          else if (info->shared
     1943                   && !info->no_undefined
    12651944                   && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
    12661945            relocation = 0;
     
    12691948              if (! ((*info->callbacks->undefined_symbol)
    12701949                     (info, h->root.root.string, input_bfd,
    1271                       input_section, rela->r_offset,
     1950                      input_section, rel->r_offset,
    12721951                      (!info->shared || info->no_undefined
    12731952                       || ELF_ST_VISIBILITY (h->other)))))
    1274                 return false;
     1953                return FALSE;
    12751954              relocation = 0;
    12761955            }
    12771956        }
    1278 
    12791957      /* When generating a shared object, the relocations handled here are
    12801958         copied into the output file to be resolved at run time.  */
     
    12861964        case R_X86_64_GOTPCREL:
    12871965          /* Use global offset table as symbol value.  */
    1288           BFD_ASSERT (sgot != NULL);
     1966          if (htab->sgot == NULL)
     1967            abort ();
    12891968
    12901969          if (h != NULL)
    12911970            {
    1292               bfd_vma off = h->got.offset;
    1293               BFD_ASSERT (off != (bfd_vma) -1);
    1294 
    1295               if (! elf_hash_table (info)->dynamic_sections_created
     1971              bfd_boolean dyn;
     1972
     1973              off = h->got.offset;
     1974              dyn = htab->elf.dynamic_sections_created;
     1975
     1976              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
    12961977                  || (info->shared
    1297                       && (info->symbolic || h->dynindx == -1)
     1978                      && (info->symbolic
     1979                          || h->dynindx == -1
     1980                          || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
    12981981                      && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
    12991982                {
     
    13141997                    {
    13151998                      bfd_put_64 (output_bfd, relocation,
    1316                                   sgot->contents + off);
     1999                                  htab->sgot->contents + off);
    13172000                      h->got.offset |= 1;
    13182001                    }
    13192002                }
    1320               if (r_type == R_X86_64_GOTPCREL)
    1321                 relocation = sgot->output_section->vma + sgot->output_offset + off;
    13222003              else
    1323                 relocation = sgot->output_offset + off;
     2004                unresolved_reloc = FALSE;
    13242005            }
    13252006          else
    13262007            {
    1327               bfd_vma off;
    1328 
    1329               BFD_ASSERT (local_got_offsets != NULL
    1330                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
     2008              if (local_got_offsets == NULL)
     2009                abort ();
    13312010
    13322011              off = local_got_offsets[r_symndx];
     
    13392018              else
    13402019                {
    1341                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
     2020                  bfd_put_64 (output_bfd, relocation,
     2021                              htab->sgot->contents + off);
    13422022
    13432023                  if (info->shared)
    13442024                    {
    1345                       asection *srelgot;
     2025                      asection *s;
    13462026                      Elf_Internal_Rela outrel;
     2027                      bfd_byte *loc;
    13472028
    13482029                      /* We need to generate a R_X86_64_RELATIVE reloc
    13492030                         for the dynamic linker.  */
    1350                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
    1351                       BFD_ASSERT (srelgot != NULL);
    1352 
    1353                       outrel.r_offset = (sgot->output_section->vma
    1354                                          + sgot->output_offset
     2031                      s = htab->srelgot;
     2032                      if (s == NULL)
     2033                        abort ();
     2034
     2035                      outrel.r_offset = (htab->sgot->output_section->vma
     2036                                         + htab->sgot->output_offset
    13552037                                         + off);
    13562038                      outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
    13572039                      outrel.r_addend = relocation;
    1358                       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
    1359                                                  (((Elf64_External_Rela *)
    1360                                                    srelgot->contents)
    1361                                                   + srelgot->reloc_count));
    1362                       ++srelgot->reloc_count;
     2040                      loc = s->contents;
     2041                      loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
     2042                      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
    13632043                    }
    13642044
    13652045                  local_got_offsets[r_symndx] |= 1;
    13662046                }
    1367 
    1368               if (r_type == R_X86_64_GOTPCREL)
    1369                 relocation = sgot->output_section->vma + sgot->output_offset + off;
    1370               else
    1371                 relocation = sgot->output_offset + off;
    1372             }
     2047            }
     2048
     2049          if (off >= (bfd_vma) -2)
     2050            abort ();
     2051
     2052          relocation = htab->sgot->output_offset + off;
     2053          if (r_type == R_X86_64_GOTPCREL)
     2054            relocation += htab->sgot->output_section->vma;
    13732055
    13742056          break;
     
    13832065            break;
    13842066
    1385           if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
     2067          if (h->plt.offset == (bfd_vma) -1
     2068              || htab->splt == NULL)
    13862069            {
    13872070              /* We didn't make a PLT entry for this symbol.  This
     
    13912074            }
    13922075
    1393           relocation = (splt->output_section->vma
    1394                         + splt->output_offset
     2076          relocation = (htab->splt->output_section->vma
     2077                        + htab->splt->output_offset
    13952078                        + h->plt.offset);
     2079          unresolved_reloc = FALSE;
    13962080          break;
    13972081
     
    13992083        case R_X86_64_PC16:
    14002084        case R_X86_64_PC32:
    1401           if (h == NULL)
    1402             break;
    1403           /* Fall through.  */
    14042085        case R_X86_64_8:
    14052086        case R_X86_64_16:
     
    14082089          /* FIXME: The ABI says the linker should make sure the value is
    14092090             the same when it's zeroextended to 64 bit.  */
    1410           if (info->shared
    1411               && (input_section->flags & SEC_ALLOC) != 0
    1412               && ((r_type != R_X86_64_PC8
    1413                    && r_type != R_X86_64_PC16
    1414                    && r_type != R_X86_64_PC32)
    1415                   || (! info->symbolic
    1416                       || (h->elf_link_hash_flags
    1417                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
    1418 
     2091
     2092          /* r_symndx will be zero only for relocs against symbols
     2093             from removed linkonce sections, or sections discarded by
     2094             a linker script.  */
     2095          if (r_symndx == 0
     2096              || (input_section->flags & SEC_ALLOC) == 0)
     2097            break;
     2098
     2099          if ((info->shared
     2100               && ((r_type != R_X86_64_PC8
     2101                    && r_type != R_X86_64_PC16
     2102                    && r_type != R_X86_64_PC32)
     2103                   || (h != NULL
     2104                       && h->dynindx != -1
     2105                       && (! info->symbolic
     2106                           || (h->elf_link_hash_flags
     2107                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
     2108              || (ELIMINATE_COPY_RELOCS
     2109                  && !info->shared
     2110                  && h != NULL
     2111                  && h->dynindx != -1
     2112                  && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
     2113                  && (((h->elf_link_hash_flags
     2114                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
     2115                       && (h->elf_link_hash_flags
     2116                           & ELF_LINK_HASH_DEF_REGULAR) == 0)
     2117                      || h->root.type == bfd_link_hash_undefweak
     2118                      || h->root.type == bfd_link_hash_undefined)))
    14192119            {
    14202120              Elf_Internal_Rela outrel;
    1421               boolean skip, relocate;
     2121              bfd_byte *loc;
     2122              bfd_boolean skip, relocate;
     2123              asection *sreloc;
    14222124
    14232125              /* When generating a shared object, these relocations
    14242126                 are copied into the output file to be resolved at run
    14252127                 time.  */
    1426 
    1427               if (sreloc == NULL)
    1428                 {
    1429                   const char *name;
    1430 
    1431                   name = (bfd_elf_string_from_elf_section
    1432                           (input_bfd,
    1433                            elf_elfheader (input_bfd)->e_shstrndx,
    1434                            elf_section_data (input_section)->rel_hdr.sh_name));
    1435                   if (name == NULL)
    1436                     return false;
    1437 
    1438                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
    1439                               && strcmp (bfd_get_section_name (input_bfd,
    1440                                                                input_section),
    1441                                          name + 5) == 0);
    1442 
    1443                   sreloc = bfd_get_section_by_name (dynobj, name);
    1444                   BFD_ASSERT (sreloc != NULL);
    1445                 }
    1446 
    1447               skip = false;
    1448 
    1449               if (elf_section_data (input_section)->stab_info == NULL)
    1450                 outrel.r_offset = rela->r_offset;
    1451               else
    1452                 {
    1453                   bfd_vma off;
    1454 
    1455                   off = (_bfd_stab_section_offset
    1456                          (output_bfd, &elf_hash_table (info)->stab_info,
    1457                           input_section,
    1458                           &elf_section_data (input_section)->stab_info,
    1459                           rela->r_offset));
    1460                   if (off == (bfd_vma) -1)
    1461                     skip = true;
    1462                   outrel.r_offset = off;
    1463                 }
     2128              skip = FALSE;
     2129              relocate = FALSE;
     2130
     2131              outrel.r_offset =
     2132                _bfd_elf_section_offset (output_bfd, info, input_section,
     2133                                         rel->r_offset);
     2134              if (outrel.r_offset == (bfd_vma) -1)
     2135                skip = TRUE;
     2136              else if (outrel.r_offset == (bfd_vma) -2)
     2137                skip = TRUE, relocate = TRUE;
    14642138
    14652139              outrel.r_offset += (input_section->output_section->vma
     
    14672141
    14682142              if (skip)
    1469                 {
    1470                   memset (&outrel, 0, sizeof outrel);
    1471                   relocate = false;
    1472                 }
     2143                memset (&outrel, 0, sizeof outrel);
     2144
    14732145              /* h->dynindx may be -1 if this symbol was marked to
    14742146                 become local.  */
    14752147              else if (h != NULL
    1476                        && ((! info->symbolic && h->dynindx != -1)
     2148                       && h->dynindx != -1
     2149                       && (r_type == R_X86_64_PC8
     2150                           || r_type == R_X86_64_PC16
     2151                           || r_type == R_X86_64_PC32
     2152                           || !info->shared
     2153                           || !info->symbolic
    14772154                           || (h->elf_link_hash_flags
    14782155                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
    14792156                {
    1480                   BFD_ASSERT (h->dynindx != -1);
    1481                   relocate = false;
    14822157                  outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
    1483                   outrel.r_addend = relocation + rela->r_addend;
     2158                  outrel.r_addend = rel->r_addend;
    14842159                }
    14852160              else
    14862161                {
     2162                  /* This symbol is local, or marked to become local.  */
    14872163                  if (r_type == R_X86_64_64)
    14882164                    {
    1489                       relocate = true;
     2165                      relocate = TRUE;
    14902166                      outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
    1491                       outrel.r_addend = relocation + rela->r_addend;
     2167                      outrel.r_addend = relocation + rel->r_addend;
    14922168                    }
    14932169                  else
    14942170                    {
    1495                       long indx;
     2171                      long sindx;
    14962172
    14972173                      if (h == NULL)
     
    15052181                        }
    15062182                      if (sec != NULL && bfd_is_abs_section (sec))
    1507                         indx = 0;
     2183                        sindx = 0;
    15082184                      else if (sec == NULL || sec->owner == NULL)
    15092185                        {
    15102186                          bfd_set_error (bfd_error_bad_value);
    1511                           return false;
     2187                          return FALSE;
    15122188                        }
    15132189                      else
     
    15162192
    15172193                          osec = sec->output_section;
    1518                           indx = elf_section_data (osec)->dynindx;
    1519                           BFD_ASSERT (indx > 0);
     2194                          sindx = elf_section_data (osec)->dynindx;
     2195                          BFD_ASSERT (sindx > 0);
    15202196                        }
    15212197
    1522                       relocate = false;
    1523                       outrel.r_info = ELF64_R_INFO (indx, r_type);
    1524                       outrel.r_addend = relocation + rela->r_addend;
    1525                     }
    1526 
     2198                      outrel.r_info = ELF64_R_INFO (sindx, r_type);
     2199                      outrel.r_addend = relocation + rel->r_addend;
     2200                    }
    15272201                }
    15282202
    1529               bfd_elf64_swap_reloca_out (output_bfd, &outrel,
    1530                                         (((Elf64_External_Rela *)
    1531                                           sreloc->contents)
    1532                                          + sreloc->reloc_count));
    1533               ++sreloc->reloc_count;
     2203              sreloc = elf_section_data (input_section)->sreloc;
     2204              if (sreloc == NULL)
     2205                abort ();
     2206
     2207              loc = sreloc->contents;
     2208              loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
     2209              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
    15342210
    15352211              /* If this reloc is against an external symbol, we do
     
    15432219          break;
    15442220
     2221        case R_X86_64_TLSGD:
     2222        case R_X86_64_GOTTPOFF:
     2223          r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
     2224          tls_type = GOT_UNKNOWN;
     2225          if (h == NULL && local_got_offsets)
     2226            tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
     2227          else if (h != NULL)
     2228            {
     2229              tls_type = elf64_x86_64_hash_entry (h)->tls_type;
     2230              if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
     2231                r_type = R_X86_64_TPOFF32;
     2232            }
     2233          if (r_type == R_X86_64_TLSGD)
     2234            {
     2235              if (tls_type == GOT_TLS_IE)
     2236                r_type = R_X86_64_GOTTPOFF;
     2237            }
     2238
     2239          if (r_type == R_X86_64_TPOFF32)
     2240            {
     2241              BFD_ASSERT (! unresolved_reloc);
     2242              if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
     2243                {
     2244                  unsigned int i;
     2245                  static unsigned char tlsgd[8]
     2246                    = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
     2247
     2248                  /* GD->LE transition.
     2249                     .byte 0x66; leaq foo@tlsgd(%rip), %rdi
     2250                     .word 0x6666; rex64; call __tls_get_addr@plt
     2251                     Change it into:
     2252                     movq %fs:0, %rax
     2253                     leaq foo@tpoff(%rax), %rax */
     2254                  BFD_ASSERT (rel->r_offset >= 4);
     2255                  for (i = 0; i < 4; i++)
     2256                    BFD_ASSERT (bfd_get_8 (input_bfd,
     2257                                           contents + rel->r_offset - 4 + i)
     2258                                == tlsgd[i]);
     2259                  BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
     2260                  for (i = 0; i < 4; i++)
     2261                    BFD_ASSERT (bfd_get_8 (input_bfd,
     2262                                           contents + rel->r_offset + 4 + i)
     2263                                == tlsgd[i+4]);
     2264                  BFD_ASSERT (rel + 1 < relend);
     2265                  BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
     2266                  memcpy (contents + rel->r_offset - 4,
     2267                          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
     2268                          16);
     2269                  bfd_put_32 (output_bfd, tpoff (info, relocation),
     2270                              contents + rel->r_offset + 8);
     2271                  /* Skip R_X86_64_PLT32.  */
     2272                  rel++;
     2273                  continue;
     2274                }
     2275              else
     2276                {
     2277                  unsigned int val, type, reg;
     2278
     2279                  /* IE->LE transition:
     2280                     Originally it can be one of:
     2281                     movq foo@gottpoff(%rip), %reg
     2282                     addq foo@gottpoff(%rip), %reg
     2283                     We change it into:
     2284                     movq $foo, %reg
     2285                     leaq foo(%reg), %reg
     2286                     addq $foo, %reg.  */
     2287                  BFD_ASSERT (rel->r_offset >= 3);
     2288                  val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
     2289                  BFD_ASSERT (val == 0x48 || val == 0x4c);
     2290                  type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
     2291                  BFD_ASSERT (type == 0x8b || type == 0x03);
     2292                  reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
     2293                  BFD_ASSERT ((reg & 0xc7) == 5);
     2294                  reg >>= 3;
     2295                  BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
     2296                  if (type == 0x8b)
     2297                    {
     2298                      /* movq */
     2299                      if (val == 0x4c)
     2300                        bfd_put_8 (output_bfd, 0x49,
     2301                                   contents + rel->r_offset - 3);
     2302                      bfd_put_8 (output_bfd, 0xc7,
     2303                                 contents + rel->r_offset - 2);
     2304                      bfd_put_8 (output_bfd, 0xc0 | reg,
     2305                                 contents + rel->r_offset - 1);
     2306                    }
     2307                  else if (reg == 4)
     2308                    {
     2309                      /* addq -> addq - addressing with %rsp/%r12 is
     2310                         special  */
     2311                      if (val == 0x4c)
     2312                        bfd_put_8 (output_bfd, 0x49,
     2313                                   contents + rel->r_offset - 3);
     2314                      bfd_put_8 (output_bfd, 0x81,
     2315                                 contents + rel->r_offset - 2);
     2316                      bfd_put_8 (output_bfd, 0xc0 | reg,
     2317                                 contents + rel->r_offset - 1);
     2318                    }
     2319                  else
     2320                    {
     2321                      /* addq -> leaq */
     2322                      if (val == 0x4c)
     2323                        bfd_put_8 (output_bfd, 0x4d,
     2324                                   contents + rel->r_offset - 3);
     2325                      bfd_put_8 (output_bfd, 0x8d,
     2326                                 contents + rel->r_offset - 2);
     2327                      bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
     2328                                 contents + rel->r_offset - 1);
     2329                    }
     2330                  bfd_put_32 (output_bfd, tpoff (info, relocation),
     2331                              contents + rel->r_offset);
     2332                  continue;
     2333                }
     2334            }
     2335
     2336          if (htab->sgot == NULL)
     2337            abort ();
     2338
     2339          if (h != NULL)
     2340            off = h->got.offset;
     2341          else
     2342            {
     2343              if (local_got_offsets == NULL)
     2344                abort ();
     2345
     2346              off = local_got_offsets[r_symndx];
     2347            }
     2348
     2349          if ((off & 1) != 0)
     2350            off &= ~1;
     2351          else
     2352            {
     2353              Elf_Internal_Rela outrel;
     2354              bfd_byte *loc;
     2355              int dr_type, indx;
     2356
     2357              if (htab->srelgot == NULL)
     2358                abort ();
     2359
     2360              outrel.r_offset = (htab->sgot->output_section->vma
     2361                                 + htab->sgot->output_offset + off);
     2362
     2363              indx = h && h->dynindx != -1 ? h->dynindx : 0;
     2364              if (r_type == R_X86_64_TLSGD)
     2365                dr_type = R_X86_64_DTPMOD64;
     2366              else
     2367                dr_type = R_X86_64_TPOFF64;
     2368
     2369              bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
     2370              outrel.r_addend = 0;
     2371              if (dr_type == R_X86_64_TPOFF64 && indx == 0)
     2372                outrel.r_addend = relocation - dtpoff_base (info);
     2373              outrel.r_info = ELF64_R_INFO (indx, dr_type);
     2374
     2375              loc = htab->srelgot->contents;
     2376              loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
     2377              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
     2378
     2379              if (r_type == R_X86_64_TLSGD)
     2380                {
     2381                  if (indx == 0)
     2382                    {
     2383                      BFD_ASSERT (! unresolved_reloc);
     2384                      bfd_put_64 (output_bfd,
     2385                                  relocation - dtpoff_base (info),
     2386                                  htab->sgot->contents + off + GOT_ENTRY_SIZE);
     2387                    }
     2388                  else
     2389                    {
     2390                      bfd_put_64 (output_bfd, 0,
     2391                                  htab->sgot->contents + off + GOT_ENTRY_SIZE);
     2392                      outrel.r_info = ELF64_R_INFO (indx,
     2393                                                    R_X86_64_DTPOFF64);
     2394                      outrel.r_offset += GOT_ENTRY_SIZE;
     2395                      htab->srelgot->reloc_count++;
     2396                      loc += sizeof (Elf64_External_Rela);
     2397                      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
     2398                    }
     2399                }
     2400
     2401              if (h != NULL)
     2402                h->got.offset |= 1;
     2403              else
     2404                local_got_offsets[r_symndx] |= 1;
     2405            }
     2406
     2407          if (off >= (bfd_vma) -2)
     2408            abort ();
     2409          if (r_type == ELF64_R_TYPE (rel->r_info))
     2410            {
     2411              relocation = htab->sgot->output_section->vma
     2412                           + htab->sgot->output_offset + off;
     2413              unresolved_reloc = FALSE;
     2414            }
     2415          else
     2416            {
     2417              unsigned int i;
     2418              static unsigned char tlsgd[8]
     2419                = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
     2420
     2421              /* GD->IE transition.
     2422                 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
     2423                 .word 0x6666; rex64; call __tls_get_addr@plt
     2424                 Change it into:
     2425                 movq %fs:0, %rax
     2426                 addq foo@gottpoff(%rip), %rax */
     2427              BFD_ASSERT (rel->r_offset >= 4);
     2428              for (i = 0; i < 4; i++)
     2429                BFD_ASSERT (bfd_get_8 (input_bfd,
     2430                                       contents + rel->r_offset - 4 + i)
     2431                            == tlsgd[i]);
     2432              BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
     2433              for (i = 0; i < 4; i++)
     2434                BFD_ASSERT (bfd_get_8 (input_bfd,
     2435                                       contents + rel->r_offset + 4 + i)
     2436                            == tlsgd[i+4]);
     2437              BFD_ASSERT (rel + 1 < relend);
     2438              BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
     2439              memcpy (contents + rel->r_offset - 4,
     2440                      "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
     2441                      16);
     2442
     2443              relocation = (htab->sgot->output_section->vma
     2444                            + htab->sgot->output_offset + off
     2445                            - rel->r_offset
     2446                            - input_section->output_section->vma
     2447                            - input_section->output_offset
     2448                            - 12);
     2449              bfd_put_32 (output_bfd, relocation,
     2450                          contents + rel->r_offset + 8);
     2451              /* Skip R_X86_64_PLT32.  */
     2452              rel++;
     2453              continue;
     2454            }
     2455          break;
     2456
     2457        case R_X86_64_TLSLD:
     2458          if (! info->shared)
     2459            {
     2460              /* LD->LE transition:
     2461                 Ensure it is:
     2462                 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
     2463                 We change it into:
     2464                 .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
     2465              BFD_ASSERT (rel->r_offset >= 3);
     2466              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
     2467                          == 0x48);
     2468              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
     2469                          == 0x8d);
     2470              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
     2471                          == 0x3d);
     2472              BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
     2473              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
     2474                          == 0xe8);
     2475              BFD_ASSERT (rel + 1 < relend);
     2476              BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
     2477              memcpy (contents + rel->r_offset - 3,
     2478                      "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
     2479              /* Skip R_X86_64_PLT32.  */
     2480              rel++;
     2481              continue;
     2482            }
     2483
     2484          if (htab->sgot == NULL)
     2485            abort ();
     2486
     2487          off = htab->tls_ld_got.offset;
     2488          if (off & 1)
     2489            off &= ~1;
     2490          else
     2491            {
     2492              Elf_Internal_Rela outrel;
     2493              bfd_byte *loc;
     2494
     2495              if (htab->srelgot == NULL)
     2496                abort ();
     2497
     2498              outrel.r_offset = (htab->sgot->output_section->vma
     2499                                 + htab->sgot->output_offset + off);
     2500
     2501              bfd_put_64 (output_bfd, 0,
     2502                          htab->sgot->contents + off);
     2503              bfd_put_64 (output_bfd, 0,
     2504                          htab->sgot->contents + off + GOT_ENTRY_SIZE);
     2505              outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
     2506              outrel.r_addend = 0;
     2507              loc = htab->srelgot->contents;
     2508              loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
     2509              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
     2510              htab->tls_ld_got.offset |= 1;
     2511            }
     2512          relocation = htab->sgot->output_section->vma
     2513                       + htab->sgot->output_offset + off;
     2514          unresolved_reloc = FALSE;
     2515          break;
     2516
     2517        case R_X86_64_DTPOFF32:
     2518          if (info->shared || (input_section->flags & SEC_CODE) == 0)
     2519            relocation -= dtpoff_base (info);
     2520          else
     2521            relocation = tpoff (info, relocation);
     2522          break;
     2523
     2524        case R_X86_64_TPOFF32:
     2525          BFD_ASSERT (! info->shared);
     2526          relocation = tpoff (info, relocation);
     2527          break;
     2528
    15452529        default:
    15462530          break;
    15472531        }
    15482532
     2533      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
     2534         because such sections are not SEC_ALLOC and thus ld.so will
     2535         not process them.  */
     2536      if (unresolved_reloc
     2537          && !((input_section->flags & SEC_DEBUGGING) != 0
     2538               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
     2539        (*_bfd_error_handler)
     2540          (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
     2541           bfd_archive_filename (input_bfd),
     2542           bfd_get_section_name (input_bfd, input_section),
     2543           (long) rel->r_offset,
     2544           h->root.root.string);
     2545
    15492546      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
    1550                                     contents, rela->r_offset,
    1551                                     relocation, rela->r_addend);
     2547                                    contents, rel->r_offset,
     2548                                    relocation, rel->r_addend);
    15522549
    15532550      if (r != bfd_reloc_ok)
    15542551        {
    1555           switch (r)
    1556             {
    1557             default:
    1558             case bfd_reloc_outofrange:
    1559               abort ();
    1560             case bfd_reloc_overflow:
    1561               {
    1562                 const char *name;
    1563 
    1564                 if (h != NULL)
    1565                   name = h->root.root.string;
    1566                 else
    1567                   {
    1568                     name = bfd_elf_string_from_elf_section (input_bfd,
    1569                                                             symtab_hdr->sh_link,
    1570                                                             sym->st_name);
    1571                     if (name == NULL)
    1572                       return false;
    1573                     if (*name == '\0')
    1574                       name = bfd_section_name (input_bfd, sec);
    1575                   }
    1576                 if (! ((*info->callbacks->reloc_overflow)
    1577                        (info, name, howto->name, (bfd_vma) 0,
    1578                         input_bfd, input_section, rela->r_offset)))
    1579                   return false;
    1580               }
    1581               break;
    1582             }
    1583         }
    1584     }
    1585 
    1586   return true;
     2552          const char *name;
     2553
     2554          if (h != NULL)
     2555            name = h->root.root.string;
     2556          else
     2557            {
     2558              name = bfd_elf_string_from_elf_section (input_bfd,
     2559                                                      symtab_hdr->sh_link,
     2560                                                      sym->st_name);
     2561              if (name == NULL)
     2562                return FALSE;
     2563              if (*name == '\0')
     2564                name = bfd_section_name (input_bfd, sec);
     2565            }
     2566
     2567          if (r == bfd_reloc_overflow)
     2568            {
     2569
     2570              if (! ((*info->callbacks->reloc_overflow)
     2571                     (info, name, howto->name, (bfd_vma) 0,
     2572                      input_bfd, input_section, rel->r_offset)))
     2573                return FALSE;
     2574            }
     2575          else
     2576            {
     2577              (*_bfd_error_handler)
     2578                (_("%s(%s+0x%lx): reloc against `%s': error %d"),
     2579                 bfd_archive_filename (input_bfd),
     2580                 bfd_get_section_name (input_bfd, input_section),
     2581                 (long) rel->r_offset, name, (int) r);
     2582              return FALSE;
     2583            }
     2584        }
     2585    }
     2586
     2587  return TRUE;
    15872588}
    15882589
     
    15902591   dynamic sections here.  */
    15912592
    1592 static boolean
     2593static bfd_boolean
    15932594elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
    15942595     bfd *output_bfd;
     
    15972598     Elf_Internal_Sym *sym;
    15982599{
    1599   bfd *dynobj;
    1600 
    1601   dynobj = elf_hash_table (info)->dynobj;
     2600  struct elf64_x86_64_link_hash_table *htab;
     2601
     2602  htab = elf64_x86_64_hash_table (info);
    16022603
    16032604  if (h->plt.offset != (bfd_vma) -1)
    16042605    {
    1605       asection *splt;
    1606       asection *sgot;
    1607       asection *srela;
    16082606      bfd_vma plt_index;
    16092607      bfd_vma got_offset;
    16102608      Elf_Internal_Rela rela;
     2609      bfd_byte *loc;
    16112610
    16122611      /* This symbol has an entry in the procedure linkage table.  Set
    16132612         it up.  */
    1614 
    1615       BFD_ASSERT (h->dynindx != -1);
    1616 
    1617       splt = bfd_get_section_by_name (dynobj, ".plt");
    1618       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
    1619       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
    1620       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
     2613      if (h->dynindx == -1
     2614          || htab->splt == NULL
     2615          || htab->sgotplt == NULL
     2616          || htab->srelplt == NULL)
     2617        abort ();
    16212618
    16222619      /* Get the index in the procedure linkage table which
     
    16322629
    16332630      /* Fill in the entry in the procedure linkage table.  */
    1634       memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
     2631      memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
    16352632              PLT_ENTRY_SIZE);
    16362633
     
    16412638         instruction uses 6 bytes, subtract this value.  */
    16422639      bfd_put_32 (output_bfd,
    1643                       (sgot->output_section->vma
    1644                        + sgot->output_offset
     2640                      (htab->sgotplt->output_section->vma
     2641                       + htab->sgotplt->output_offset
    16452642                       + got_offset
    1646                        - splt->output_section->vma
    1647                        - splt->output_offset
     2643                       - htab->splt->output_section->vma
     2644                       - htab->splt->output_offset
    16482645                       - h->plt.offset
    16492646                       - 6),
    1650                   splt->contents + h->plt.offset + 2);
     2647                  htab->splt->contents + h->plt.offset + 2);
    16512648      /* Put relocation index.  */
    16522649      bfd_put_32 (output_bfd, plt_index,
    1653                   splt->contents + h->plt.offset + 7);
     2650                  htab->splt->contents + h->plt.offset + 7);
    16542651      /* Put offset for jmp .PLT0.  */
    16552652      bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
    1656                   splt->contents + h->plt.offset + 12);
     2653                  htab->splt->contents + h->plt.offset + 12);
    16572654
    16582655      /* Fill in the entry in the global offset table, initially this
    16592656         points to the pushq instruction in the PLT which is at offset 6.  */
    1660       bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset
     2657      bfd_put_64 (output_bfd, (htab->splt->output_section->vma
     2658                               + htab->splt->output_offset
    16612659                               + h->plt.offset + 6),
    1662                   sgot->contents + got_offset);
     2660                  htab->sgotplt->contents + got_offset);
    16632661
    16642662      /* Fill in the entry in the .rela.plt section.  */
    1665       rela.r_offset = (sgot->output_section->vma
    1666                        + sgot->output_offset
     2663      rela.r_offset = (htab->sgotplt->output_section->vma
     2664                       + htab->sgotplt->output_offset
    16672665                       + got_offset);
    16682666      rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
    16692667      rela.r_addend = 0;
    1670       bfd_elf64_swap_reloca_out (output_bfd, &rela,
    1671                                  ((Elf64_External_Rela *) srela->contents
    1672                                   + plt_index));
     2668      loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
     2669      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
    16732670
    16742671      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
    16752672        {
    16762673          /* Mark the symbol as undefined, rather than as defined in
    1677              the .plt section.  Leave the value alone.  */
     2674             the .plt section.  Leave the value alone.  This is a clue
     2675             for the dynamic linker, to make function pointer
     2676             comparisons work between an application and shared
     2677             library.  */
    16782678          sym->st_shndx = SHN_UNDEF;
    1679           /* If the symbol is weak, we do need to clear the value.
    1680              Otherwise, the PLT entry would provide a definition for
    1681              the symbol even if the symbol wasn't defined anywhere,
    1682              and so the symbol would never be NULL.  */
    1683           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
    1684               == 0)
    1685             sym->st_value = 0;
    1686         }
    1687     }
    1688 
    1689   if (h->got.offset != (bfd_vma) -1)
    1690     {
    1691       asection *sgot;
    1692       asection *srela;
     2679        }
     2680    }
     2681
     2682  if (h->got.offset != (bfd_vma) -1
     2683      && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
     2684      && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
     2685    {
    16932686      Elf_Internal_Rela rela;
     2687      bfd_byte *loc;
    16942688
    16952689      /* This symbol has an entry in the global offset table.  Set it
    1696          up.  */
    1697 
    1698       sgot = bfd_get_section_by_name (dynobj, ".got");
    1699       srela = bfd_get_section_by_name (dynobj, ".rela.got");
    1700       BFD_ASSERT (sgot != NULL && srela != NULL);
    1701 
    1702       rela.r_offset = (sgot->output_section->vma
    1703                        + sgot->output_offset
    1704                        + (h->got.offset &~ 1));
     2690         up.  */
     2691      if (htab->sgot == NULL || htab->srelgot == NULL)
     2692        abort ();
     2693
     2694      rela.r_offset = (htab->sgot->output_section->vma
     2695                       + htab->sgot->output_offset
     2696                       + (h->got.offset &~ (bfd_vma) 1));
    17052697
    17062698      /* If this is a static link, or it is a -Bsymbolic link and the
     
    17092701         The entry in the global offset table will already have been
    17102702         initialized in the relocate_section function.  */
    1711       if (! elf_hash_table (info)->dynamic_sections_created
    1712           || (info->shared
    1713               && (info->symbolic || h->dynindx == -1)
    1714               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
     2703      if (info->shared
     2704          && (info->symbolic
     2705              || h->dynindx == -1
     2706              || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
     2707          && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
    17152708        {
    17162709          BFD_ASSERT((h->got.offset & 1) != 0);
     
    17232716        {
    17242717          BFD_ASSERT((h->got.offset & 1) == 0);
    1725           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
     2718          bfd_put_64 (output_bfd, (bfd_vma) 0,
     2719                      htab->sgot->contents + h->got.offset);
    17262720          rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
    17272721          rela.r_addend = 0;
    17282722        }
    17292723
    1730       bfd_elf64_swap_reloca_out (output_bfd, &rela,
    1731                                  ((Elf64_External_Rela *) srela->contents
    1732                                   + srela->reloc_count));
    1733       ++srela->reloc_count;
     2724      loc = htab->srelgot->contents;
     2725      loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
     2726      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
    17342727    }
    17352728
    17362729  if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
    17372730    {
    1738       asection *s;
    17392731      Elf_Internal_Rela rela;
     2732      bfd_byte *loc;
    17402733
    17412734      /* This symbol needs a copy reloc.  Set it up.  */
    17422735
    1743       BFD_ASSERT (h->dynindx != -1
    1744                   && (h->root.type == bfd_link_hash_defined
    1745                       || h->root.type == bfd_link_hash_defweak));
    1746 
    1747       s = bfd_get_section_by_name (h->root.u.def.section->owner,
    1748                                    ".rela.bss");
    1749       BFD_ASSERT (s != NULL);
     2736      if (h->dynindx == -1
     2737          || (h->root.type != bfd_link_hash_defined
     2738              && h->root.type != bfd_link_hash_defweak)
     2739          || htab->srelbss == NULL)
     2740        abort ();
    17502741
    17512742      rela.r_offset = (h->root.u.def.value
     
    17542745      rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
    17552746      rela.r_addend = 0;
    1756       bfd_elf64_swap_reloca_out (output_bfd, &rela,
    1757                                  ((Elf64_External_Rela *) s->contents
    1758                                   + s->reloc_count));
    1759       ++s->reloc_count;
     2747      loc = htab->srelbss->contents;
     2748      loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
     2749      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
    17602750    }
    17612751
     
    17652755    sym->st_shndx = SHN_ABS;
    17662756
    1767   return true;
     2757  return TRUE;
    17682758}
    17692759
     2760/* Used to decide how to sort relocs in an optimal manner for the
     2761   dynamic linker, before writing them out.  */
     2762
     2763static enum elf_reloc_type_class
     2764elf64_x86_64_reloc_type_class (rela)
     2765     const Elf_Internal_Rela *rela;
     2766{
     2767  switch ((int) ELF64_R_TYPE (rela->r_info))
     2768    {
     2769    case R_X86_64_RELATIVE:
     2770      return reloc_class_relative;
     2771    case R_X86_64_JUMP_SLOT:
     2772      return reloc_class_plt;
     2773    case R_X86_64_COPY:
     2774      return reloc_class_copy;
     2775    default:
     2776      return reloc_class_normal;
     2777    }
     2778}
     2779
    17702780/* Finish up the dynamic sections.  */
    17712781
    1772 static boolean
     2782static bfd_boolean
    17732783elf64_x86_64_finish_dynamic_sections (output_bfd, info)
    17742784     bfd *output_bfd;
    17752785     struct bfd_link_info *info;
    17762786{
     2787  struct elf64_x86_64_link_hash_table *htab;
    17772788  bfd *dynobj;
    17782789  asection *sdyn;
    1779   asection *sgot;
    1780 
    1781   dynobj = elf_hash_table (info)->dynobj;
    1782 
    1783   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
    1784   BFD_ASSERT (sgot != NULL);
     2790
     2791  htab = elf64_x86_64_hash_table (info);
     2792  dynobj = htab->elf.dynobj;
    17852793  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
    17862794
    1787   if (elf_hash_table (info)->dynamic_sections_created)
    1788     {
    1789       asection *splt;
     2795  if (htab->elf.dynamic_sections_created)
     2796    {
    17902797      Elf64_External_Dyn *dyncon, *dynconend;
    17912798
    1792       BFD_ASSERT (sdyn != NULL);
     2799      if (sdyn == NULL || htab->sgot == NULL)
     2800        abort ();
    17932801
    17942802      dyncon = (Elf64_External_Dyn *) sdyn->contents;
     
    17972805        {
    17982806          Elf_Internal_Dyn dyn;
    1799           const char *name;
    18002807          asection *s;
    18012808
     
    18082815
    18092816            case DT_PLTGOT:
    1810               name = ".got";
    1811               goto get_vma;
     2817              dyn.d_un.d_ptr = htab->sgot->output_section->vma;
     2818              break;
    18122819
    18132820            case DT_JMPREL:
    1814               name = ".rela.plt";
    1815 
    1816             get_vma:
    1817               s = bfd_get_section_by_name (output_bfd, name);
    1818               BFD_ASSERT (s != NULL);
    1819               dyn.d_un.d_ptr = s->vma;
     2821              dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
    18202822              break;
    18212823
     2824            case DT_PLTRELSZ:
     2825              s = htab->srelplt->output_section;
     2826              if (s->_cooked_size != 0)
     2827                dyn.d_un.d_val = s->_cooked_size;
     2828              else
     2829                dyn.d_un.d_val = s->_raw_size;
     2830              break;
     2831
    18222832            case DT_RELASZ:
    1823               /* FIXME: This comment and code is from elf64-alpha.c:  */
    1824               /* My interpretation of the TIS v1.1 ELF document indicates
    1825                  that RELASZ should not include JMPREL.  This is not what
    1826                  the rest of the BFD does.  It is, however, what the
    1827                  glibc ld.so wants.  Do this fixup here until we found
    1828                  out who is right.  */
    1829               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
    1830               if (s)
     2833              /* The procedure linkage table relocs (DT_JMPREL) should
     2834                 not be included in the overall relocs (DT_RELA).
     2835                 Therefore, we override the DT_RELASZ entry here to
     2836                 make it not include the JMPREL relocs.  Since the
     2837                 linker script arranges for .rela.plt to follow all
     2838                 other relocation sections, we don't have to worry
     2839                 about changing the DT_RELA entry.  */
     2840              if (htab->srelplt != NULL)
    18312841                {
    1832                   /* Subtract JMPREL size from RELASZ.  */
    1833                   dyn.d_un.d_val -=
    1834                     (s->_cooked_size ? s->_cooked_size : s->_raw_size);
     2842                  s = htab->srelplt->output_section;
     2843                  if (s->_cooked_size != 0)
     2844                    dyn.d_un.d_val -= s->_cooked_size;
     2845                  else
     2846                    dyn.d_un.d_val -= s->_raw_size;
    18352847                }
    18362848              break;
    1837 
    1838             case DT_PLTRELSZ:
    1839               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
    1840               BFD_ASSERT (s != NULL);
    1841               dyn.d_un.d_val =
    1842                 (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size);
    1843               break;
    1844             }
     2849            }
     2850
    18452851          bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
    18462852        }
    18472853
    1848       /* Initialize the contents of the .plt section.  */
    1849       splt = bfd_get_section_by_name (dynobj, ".plt");
    1850       BFD_ASSERT (splt != NULL);
    1851       if (splt->_raw_size > 0)
     2854      /* Fill in the special first entry in the procedure linkage table.  */
     2855      if (htab->splt && htab->splt->_raw_size > 0)
    18522856        {
    18532857          /* Fill in the first entry in the procedure linkage table.  */
    1854           memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE);
     2858          memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
     2859                  PLT_ENTRY_SIZE);
    18552860          /* Add offset for pushq GOT+8(%rip), since the instruction
    18562861             uses 6 bytes subtract this value.  */
    18572862          bfd_put_32 (output_bfd,
    1858                       (sgot->output_section->vma
    1859                        + sgot->output_offset
     2863                      (htab->sgotplt->output_section->vma
     2864                       + htab->sgotplt->output_offset
    18602865                       + 8
    1861                        - splt->output_section->vma
    1862                        - splt->output_offset
     2866                       - htab->splt->output_section->vma
     2867                       - htab->splt->output_offset
    18632868                       - 6),
    1864                       splt->contents + 2);
     2869                      htab->splt->contents + 2);
    18652870          /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
    18662871             the end of the instruction.  */
    18672872          bfd_put_32 (output_bfd,
    1868                       (sgot->output_section->vma
    1869                        + sgot->output_offset
     2873                      (htab->sgotplt->output_section->vma
     2874                       + htab->sgotplt->output_offset
    18702875                       + 16
    1871                        - splt->output_section->vma
    1872                        - splt->output_offset
     2876                       - htab->splt->output_section->vma
     2877                       - htab->splt->output_offset
    18732878                       - 12),
    1874                       splt->contents + 8);
    1875 
    1876         }
    1877 
    1878       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
    1879         PLT_ENTRY_SIZE;
    1880     }
    1881 
    1882   /* Set the first entry in the global offset table to the address of
    1883      the dynamic section.  */
    1884   if (sgot->_raw_size > 0)
    1885     {
    1886       if (sdyn == NULL)
    1887         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
    1888       else
    1889         bfd_put_64 (output_bfd,
    1890                     sdyn->output_section->vma + sdyn->output_offset,
    1891                     sgot->contents);
    1892       /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
    1893       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE);
    1894       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2);
    1895     }
    1896 
    1897   elf_section_data (sgot->output_section)->this_hdr.sh_entsize =
    1898     GOT_ENTRY_SIZE;
    1899 
    1900   return true;
     2879                      htab->splt->contents + 8);
     2880
     2881          elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
     2882            PLT_ENTRY_SIZE;
     2883        }
     2884    }
     2885
     2886  if (htab->sgotplt)
     2887    {
     2888      /* Fill in the first three entries in the global offset table.  */
     2889      if (htab->sgotplt->_raw_size > 0)
     2890        {
     2891          /* Set the first entry in the global offset table to the address of
     2892             the dynamic section.  */
     2893          if (sdyn == NULL)
     2894            bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
     2895          else
     2896            bfd_put_64 (output_bfd,
     2897                        sdyn->output_section->vma + sdyn->output_offset,
     2898                        htab->sgotplt->contents);
     2899          /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
     2900          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
     2901          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
     2902        }
     2903
     2904      elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
     2905        GOT_ENTRY_SIZE;
     2906    }
     2907
     2908  return TRUE;
    19012909}
    19022910
     
    19092917
    19102918#define elf_backend_can_gc_sections         1
     2919#define elf_backend_can_refcount            1
    19112920#define elf_backend_want_got_plt            1
    19122921#define elf_backend_plt_readonly            1
     
    19142923#define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
    19152924#define elf_backend_plt_header_size         PLT_ENTRY_SIZE
     2925#define elf_backend_rela_normal             1
    19162926
    19172927#define elf_info_to_howto                   elf64_x86_64_info_to_howto
    19182928
    1919 #define bfd_elf64_bfd_final_link            _bfd_elf64_gc_common_final_link
    19202929#define bfd_elf64_bfd_link_hash_table_create \
    19212930  elf64_x86_64_link_hash_table_create
     
    19242933#define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
    19252934#define elf_backend_check_relocs            elf64_x86_64_check_relocs
    1926 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
    1927 #define elf_backend_finish_dynamic_sections \
    1928   elf64_x86_64_finish_dynamic_sections
     2935#define elf_backend_copy_indirect_symbol    elf64_x86_64_copy_indirect_symbol
     2936#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
     2937#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
    19292938#define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
    19302939#define elf_backend_gc_mark_hook            elf64_x86_64_gc_mark_hook
    19312940#define elf_backend_gc_sweep_hook           elf64_x86_64_gc_sweep_hook
     2941#define elf_backend_grok_prstatus           elf64_x86_64_grok_prstatus
     2942#define elf_backend_grok_psinfo             elf64_x86_64_grok_psinfo
     2943#define elf_backend_reloc_type_class        elf64_x86_64_reloc_type_class
    19322944#define elf_backend_relocate_section        elf64_x86_64_relocate_section
    19332945#define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
    19342946#define elf_backend_object_p                elf64_x86_64_elf_object_p
     2947#define bfd_elf64_mkobject                  elf64_x86_64_mkobject
    19352948
    19362949#include "elf64-target.h"
Note: See TracChangeset for help on using the changeset viewer.