Back to index

cell-binutils  2.17cvs20070401
elf64-x86-64.c
Go to the documentation of this file.
00001 /* X86-64 specific support for 64-bit ELF
00002    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by Jan Hubicka <jh@suse.cz>.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "bfdlink.h"
00025 #include "libbfd.h"
00026 #include "elf-bfd.h"
00027 
00028 #include "elf/x86-64.h"
00029 
00030 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
00031 #define MINUS_ONE (~ (bfd_vma) 0)
00032 
00033 /* The relocation "howto" table.  Order of fields:
00034    type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
00035    special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset.  */
00036 static reloc_howto_type x86_64_elf_howto_table[] =
00037 {
00038   HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
00039        bfd_elf_generic_reloc, "R_X86_64_NONE",   FALSE, 0x00000000, 0x00000000,
00040        FALSE),
00041   HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00042        bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
00043        FALSE),
00044   HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00045        bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
00046        TRUE),
00047   HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00048        bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
00049        FALSE),
00050   HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00051        bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
00052        TRUE),
00053   HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00054        bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
00055        FALSE),
00056   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00057        bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
00058        MINUS_ONE, FALSE),
00059   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00060        bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
00061        MINUS_ONE, FALSE),
00062   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00063        bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
00064        MINUS_ONE, FALSE),
00065   HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00066        bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
00067        0xffffffff, TRUE),
00068   HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
00069        bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
00070        FALSE),
00071   HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00072        bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
00073        FALSE),
00074   HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
00075        bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
00076   HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
00077        bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
00078   HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
00079        bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
00080   HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
00081        bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
00082   HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00083        bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
00084        MINUS_ONE, FALSE),
00085   HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00086        bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
00087        MINUS_ONE, FALSE),
00088   HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00089        bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
00090        MINUS_ONE, FALSE),
00091   HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00092        bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
00093        0xffffffff, TRUE),
00094   HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00095        bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
00096        0xffffffff, TRUE),
00097   HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00098        bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
00099        0xffffffff, FALSE),
00100   HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00101        bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
00102        0xffffffff, TRUE),
00103   HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00104        bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
00105        0xffffffff, FALSE),
00106   HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
00107        bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
00108        TRUE),
00109   HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00110        bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
00111        FALSE, MINUS_ONE, MINUS_ONE, FALSE),
00112   HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00113        bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
00114        FALSE, 0xffffffff, 0xffffffff, TRUE),
00115   HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
00116        bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
00117        FALSE),
00118   HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
00119        bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
00120        MINUS_ONE, TRUE),
00121   HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
00122        bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
00123        FALSE, MINUS_ONE, MINUS_ONE, TRUE),
00124   HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
00125        bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
00126        MINUS_ONE, FALSE),
00127   HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
00128        bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
00129        MINUS_ONE, FALSE),
00130   EMPTY_HOWTO (32),
00131   EMPTY_HOWTO (33),
00132   HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
00133        complain_overflow_bitfield, bfd_elf_generic_reloc,
00134        "R_X86_64_GOTPC32_TLSDESC",
00135        FALSE, 0xffffffff, 0xffffffff, TRUE),
00136   HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
00137        complain_overflow_dont, bfd_elf_generic_reloc,
00138        "R_X86_64_TLSDESC_CALL",
00139        FALSE, 0, 0, FALSE),
00140   HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
00141        complain_overflow_bitfield, bfd_elf_generic_reloc,
00142        "R_X86_64_TLSDESC",
00143        FALSE, MINUS_ONE, MINUS_ONE, FALSE),
00144 
00145   /* We have a gap in the reloc numbers here.
00146      R_X86_64_standard counts the number up to this point, and
00147      R_X86_64_vt_offset is the value to subtract from a reloc type of
00148      R_X86_64_GNU_VT* to form an index into this table.  */
00149 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
00150 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
00151 
00152 /* GNU extension to record C++ vtable hierarchy.  */
00153   HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
00154         NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
00155 
00156 /* GNU extension to record C++ vtable member usage.  */
00157   HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
00158         _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
00159         FALSE)
00160 };
00161 
00162 /* Map BFD relocs to the x86_64 elf relocs.  */
00163 struct elf_reloc_map
00164 {
00165   bfd_reloc_code_real_type bfd_reloc_val;
00166   unsigned char elf_reloc_val;
00167 };
00168 
00169 static const struct elf_reloc_map x86_64_reloc_map[] =
00170 {
00171   { BFD_RELOC_NONE,         R_X86_64_NONE, },
00172   { BFD_RELOC_64,           R_X86_64_64,   },
00173   { BFD_RELOC_32_PCREL,            R_X86_64_PC32, },
00174   { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
00175   { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
00176   { BFD_RELOC_X86_64_COPY,  R_X86_64_COPY, },
00177   { BFD_RELOC_X86_64_GLOB_DAT,     R_X86_64_GLOB_DAT, },
00178   { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
00179   { BFD_RELOC_X86_64_RELATIVE,     R_X86_64_RELATIVE, },
00180   { BFD_RELOC_X86_64_GOTPCREL,     R_X86_64_GOTPCREL, },
00181   { BFD_RELOC_32,           R_X86_64_32, },
00182   { BFD_RELOC_X86_64_32S,   R_X86_64_32S, },
00183   { BFD_RELOC_16,           R_X86_64_16, },
00184   { BFD_RELOC_16_PCREL,            R_X86_64_PC16, },
00185   { BFD_RELOC_8,            R_X86_64_8, },
00186   { BFD_RELOC_8_PCREL,             R_X86_64_PC8, },
00187   { BFD_RELOC_X86_64_DTPMOD64,     R_X86_64_DTPMOD64, },
00188   { BFD_RELOC_X86_64_DTPOFF64,     R_X86_64_DTPOFF64, },
00189   { BFD_RELOC_X86_64_TPOFF64,      R_X86_64_TPOFF64, },
00190   { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
00191   { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
00192   { BFD_RELOC_X86_64_DTPOFF32,     R_X86_64_DTPOFF32, },
00193   { BFD_RELOC_X86_64_GOTTPOFF,     R_X86_64_GOTTPOFF, },
00194   { BFD_RELOC_X86_64_TPOFF32,      R_X86_64_TPOFF32, },
00195   { BFD_RELOC_64_PCREL,            R_X86_64_PC64, },
00196   { BFD_RELOC_X86_64_GOTOFF64,     R_X86_64_GOTOFF64, },
00197   { BFD_RELOC_X86_64_GOTPC32,      R_X86_64_GOTPC32, },
00198   { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
00199   { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
00200   { BFD_RELOC_X86_64_GOTPC64,      R_X86_64_GOTPC64, },
00201   { BFD_RELOC_X86_64_GOTPLT64,     R_X86_64_GOTPLT64, },
00202   { BFD_RELOC_X86_64_PLTOFF64,     R_X86_64_PLTOFF64, },
00203   { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
00204   { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
00205   { BFD_RELOC_X86_64_TLSDESC,      R_X86_64_TLSDESC, },
00206   { BFD_RELOC_VTABLE_INHERIT,      R_X86_64_GNU_VTINHERIT, },
00207   { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
00208 };
00209 
00210 static reloc_howto_type *
00211 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
00212 {
00213   unsigned i;
00214 
00215   if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
00216       || r_type >= (unsigned int) R_X86_64_max)
00217     {
00218       if (r_type >= (unsigned int) R_X86_64_standard)
00219        {
00220          (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
00221                              abfd, (int) r_type);
00222          r_type = R_X86_64_NONE;
00223        }
00224       i = r_type;
00225     }
00226   else
00227     i = r_type - (unsigned int) R_X86_64_vt_offset;
00228   BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
00229   return &x86_64_elf_howto_table[i];
00230 }
00231 
00232 /* Given a BFD reloc type, return a HOWTO structure.  */
00233 static reloc_howto_type *
00234 elf64_x86_64_reloc_type_lookup (bfd *abfd,
00235                             bfd_reloc_code_real_type code)
00236 {
00237   unsigned int i;
00238 
00239   for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
00240        i++)
00241     {
00242       if (x86_64_reloc_map[i].bfd_reloc_val == code)
00243        return elf64_x86_64_rtype_to_howto (abfd,
00244                                        x86_64_reloc_map[i].elf_reloc_val);
00245     }
00246   return 0;
00247 }
00248 
00249 static reloc_howto_type *
00250 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00251                             const char *r_name)
00252 {
00253   unsigned int i;
00254 
00255   for (i = 0;
00256        i < (sizeof (x86_64_elf_howto_table)
00257            / sizeof (x86_64_elf_howto_table[0]));
00258        i++)
00259     if (x86_64_elf_howto_table[i].name != NULL
00260        && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
00261       return &x86_64_elf_howto_table[i];
00262 
00263   return NULL;
00264 }
00265 
00266 /* Given an x86_64 ELF reloc type, fill in an arelent structure.  */
00267 
00268 static void
00269 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
00270                          Elf_Internal_Rela *dst)
00271 {
00272   unsigned r_type;
00273 
00274   r_type = ELF64_R_TYPE (dst->r_info);
00275   cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
00276   BFD_ASSERT (r_type == cache_ptr->howto->type);
00277 }
00278 
00279 /* Support for core dump NOTE sections.  */
00280 static bfd_boolean
00281 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
00282 {
00283   int offset;
00284   size_t size;
00285 
00286   switch (note->descsz)
00287     {
00288       default:
00289        return FALSE;
00290 
00291       case 336:             /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
00292        /* pr_cursig */
00293        elf_tdata (abfd)->core_signal
00294          = bfd_get_16 (abfd, note->descdata + 12);
00295 
00296        /* pr_pid */
00297        elf_tdata (abfd)->core_pid
00298          = bfd_get_32 (abfd, note->descdata + 32);
00299 
00300        /* pr_reg */
00301        offset = 112;
00302        size = 216;
00303 
00304        break;
00305     }
00306 
00307   /* Make a ".reg/999" section.  */
00308   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
00309                                      size, note->descpos + offset);
00310 }
00311 
00312 static bfd_boolean
00313 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
00314 {
00315   switch (note->descsz)
00316     {
00317       default:
00318        return FALSE;
00319 
00320       case 136:             /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
00321        elf_tdata (abfd)->core_program
00322         = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
00323        elf_tdata (abfd)->core_command
00324         = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
00325     }
00326 
00327   /* Note that for some reason, a spurious space is tacked
00328      onto the end of the args in some (at least one anyway)
00329      implementations, so strip it off if it exists.  */
00330 
00331   {
00332     char *command = elf_tdata (abfd)->core_command;
00333     int n = strlen (command);
00334 
00335     if (0 < n && command[n - 1] == ' ')
00336       command[n - 1] = '\0';
00337   }
00338 
00339   return TRUE;
00340 }
00341 
00342 /* Functions for the x86-64 ELF linker.    */
00343 
00344 /* The name of the dynamic interpreter.    This is put in the .interp
00345    section.  */
00346 
00347 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
00348 
00349 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
00350    copying dynamic variables from a shared lib into an app's dynbss
00351    section, and instead use a dynamic relocation to point into the
00352    shared lib.  */
00353 #define ELIMINATE_COPY_RELOCS 1
00354 
00355 /* The size in bytes of an entry in the global offset table.  */
00356 
00357 #define GOT_ENTRY_SIZE 8
00358 
00359 /* The size in bytes of an entry in the procedure linkage table.  */
00360 
00361 #define PLT_ENTRY_SIZE 16
00362 
00363 /* The first entry in a procedure linkage table looks like this.  See the
00364    SVR4 ABI i386 supplement and the x86-64 ABI to see how this works.  */
00365 
00366 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
00367 {
00368   0xff, 0x35, 8, 0, 0, 0,   /* pushq GOT+8(%rip)  */
00369   0xff, 0x25, 16, 0, 0, 0,  /* jmpq *GOT+16(%rip) */
00370   0x0f, 0x1f, 0x40, 0x00    /* nopl 0(%rax)       */
00371 };
00372 
00373 /* Subsequent entries in a procedure linkage table look like this.  */
00374 
00375 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
00376 {
00377   0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
00378   0, 0, 0, 0, /* replaced with offset to this symbol in .got.   */
00379   0x68,              /* pushq immediate */
00380   0, 0, 0, 0, /* replaced with index into relocation table.  */
00381   0xe9,              /* jmp relative */
00382   0, 0, 0, 0  /* replaced with offset to start of .plt0.  */
00383 };
00384 
00385 /* The x86-64 linker needs to keep track of the number of relocs that
00386    it decides to copy as dynamic relocs in check_relocs for each symbol.
00387    This is so that it can later discard them if they are found to be
00388    unnecessary.  We store the information in a field extending the
00389    regular ELF linker hash table.  */
00390 
00391 struct elf64_x86_64_dyn_relocs
00392 {
00393   /* Next section.  */
00394   struct elf64_x86_64_dyn_relocs *next;
00395 
00396   /* The input section of the reloc.  */
00397   asection *sec;
00398 
00399   /* Total number of relocs copied for the input section.  */
00400   bfd_size_type count;
00401 
00402   /* Number of pc-relative relocs copied for the input section.  */
00403   bfd_size_type pc_count;
00404 };
00405 
00406 /* x86-64 ELF linker hash entry.  */
00407 
00408 struct elf64_x86_64_link_hash_entry
00409 {
00410   struct elf_link_hash_entry elf;
00411 
00412   /* Track dynamic relocs copied for this symbol.  */
00413   struct elf64_x86_64_dyn_relocs *dyn_relocs;
00414 
00415 #define GOT_UNKNOWN  0
00416 #define GOT_NORMAL   1
00417 #define GOT_TLS_GD   2
00418 #define GOT_TLS_IE   3
00419 #define GOT_TLS_GDESC       4
00420 #define GOT_TLS_GD_BOTH_P(type) \
00421   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
00422 #define GOT_TLS_GD_P(type) \
00423   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
00424 #define GOT_TLS_GDESC_P(type) \
00425   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
00426 #define GOT_TLS_GD_ANY_P(type) \
00427   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
00428   unsigned char tls_type;
00429 
00430   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
00431      starting at the end of the jump table.  */
00432   bfd_vma tlsdesc_got;
00433 };
00434 
00435 #define elf64_x86_64_hash_entry(ent) \
00436   ((struct elf64_x86_64_link_hash_entry *)(ent))
00437 
00438 struct elf64_x86_64_obj_tdata
00439 {
00440   struct elf_obj_tdata root;
00441 
00442   /* tls_type for each local got entry.  */
00443   char *local_got_tls_type;
00444 
00445   /* GOTPLT entries for TLS descriptors.  */
00446   bfd_vma *local_tlsdesc_gotent;
00447 };
00448 
00449 #define elf64_x86_64_tdata(abfd) \
00450   ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
00451 
00452 #define elf64_x86_64_local_got_tls_type(abfd) \
00453   (elf64_x86_64_tdata (abfd)->local_got_tls_type)
00454 
00455 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
00456   (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
00457 
00458 /* x86-64 ELF linker hash table.  */
00459 
00460 struct elf64_x86_64_link_hash_table
00461 {
00462   struct elf_link_hash_table elf;
00463 
00464   /* Short-cuts to get to dynamic linker sections.  */
00465   asection *sgot;
00466   asection *sgotplt;
00467   asection *srelgot;
00468   asection *splt;
00469   asection *srelplt;
00470   asection *sdynbss;
00471   asection *srelbss;
00472 
00473   /* The offset into splt of the PLT entry for the TLS descriptor
00474      resolver.  Special values are 0, if not necessary (or not found
00475      to be necessary yet), and -1 if needed but not determined
00476      yet.  */
00477   bfd_vma tlsdesc_plt;
00478   /* The offset into sgot of the GOT entry used by the PLT entry
00479      above.  */
00480   bfd_vma tlsdesc_got;
00481 
00482   union {
00483     bfd_signed_vma refcount;
00484     bfd_vma offset;
00485   } tls_ld_got;
00486 
00487   /* The amount of space used by the jump slots in the GOT.  */
00488   bfd_vma sgotplt_jump_table_size;
00489 
00490   /* Small local sym to section mapping cache.  */
00491   struct sym_sec_cache sym_sec;
00492 };
00493 
00494 /* Get the x86-64 ELF linker hash table from a link_info structure.  */
00495 
00496 #define elf64_x86_64_hash_table(p) \
00497   ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
00498 
00499 #define elf64_x86_64_compute_jump_table_size(htab) \
00500   ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
00501 
00502 /* Create an entry in an x86-64 ELF linker hash table.  */
00503 
00504 static struct bfd_hash_entry *
00505 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
00506                  const char *string)
00507 {
00508   /* Allocate the structure if it has not already been allocated by a
00509      subclass.  */
00510   if (entry == NULL)
00511     {
00512       entry = bfd_hash_allocate (table,
00513                              sizeof (struct elf64_x86_64_link_hash_entry));
00514       if (entry == NULL)
00515        return entry;
00516     }
00517 
00518   /* Call the allocation method of the superclass.  */
00519   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
00520   if (entry != NULL)
00521     {
00522       struct elf64_x86_64_link_hash_entry *eh;
00523 
00524       eh = (struct elf64_x86_64_link_hash_entry *) entry;
00525       eh->dyn_relocs = NULL;
00526       eh->tls_type = GOT_UNKNOWN;
00527       eh->tlsdesc_got = (bfd_vma) -1;
00528     }
00529 
00530   return entry;
00531 }
00532 
00533 /* Create an X86-64 ELF linker hash table.  */
00534 
00535 static struct bfd_link_hash_table *
00536 elf64_x86_64_link_hash_table_create (bfd *abfd)
00537 {
00538   struct elf64_x86_64_link_hash_table *ret;
00539   bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
00540 
00541   ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
00542   if (ret == NULL)
00543     return NULL;
00544 
00545   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
00546                                   sizeof (struct elf64_x86_64_link_hash_entry)))
00547     {
00548       free (ret);
00549       return NULL;
00550     }
00551 
00552   ret->sgot = NULL;
00553   ret->sgotplt = NULL;
00554   ret->srelgot = NULL;
00555   ret->splt = NULL;
00556   ret->srelplt = NULL;
00557   ret->sdynbss = NULL;
00558   ret->srelbss = NULL;
00559   ret->sym_sec.abfd = NULL;
00560   ret->tlsdesc_plt = 0;
00561   ret->tlsdesc_got = 0;
00562   ret->tls_ld_got.refcount = 0;
00563   ret->sgotplt_jump_table_size = 0;
00564 
00565   return &ret->elf.root;
00566 }
00567 
00568 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
00569    shortcuts to them in our hash table.  */
00570 
00571 static bfd_boolean
00572 create_got_section (bfd *dynobj, struct bfd_link_info *info)
00573 {
00574   struct elf64_x86_64_link_hash_table *htab;
00575 
00576   if (! _bfd_elf_create_got_section (dynobj, info))
00577     return FALSE;
00578 
00579   htab = elf64_x86_64_hash_table (info);
00580   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
00581   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
00582   if (!htab->sgot || !htab->sgotplt)
00583     abort ();
00584 
00585   htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
00586                                           (SEC_ALLOC | SEC_LOAD
00587                                           | SEC_HAS_CONTENTS
00588                                           | SEC_IN_MEMORY
00589                                           | SEC_LINKER_CREATED
00590                                           | SEC_READONLY));
00591   if (htab->srelgot == NULL
00592       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
00593     return FALSE;
00594   return TRUE;
00595 }
00596 
00597 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
00598    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
00599    hash table.  */
00600 
00601 static bfd_boolean
00602 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
00603 {
00604   struct elf64_x86_64_link_hash_table *htab;
00605 
00606   htab = elf64_x86_64_hash_table (info);
00607   if (!htab->sgot && !create_got_section (dynobj, info))
00608     return FALSE;
00609 
00610   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
00611     return FALSE;
00612 
00613   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
00614   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
00615   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
00616   if (!info->shared)
00617     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
00618 
00619   if (!htab->splt || !htab->srelplt || !htab->sdynbss
00620       || (!info->shared && !htab->srelbss))
00621     abort ();
00622 
00623   return TRUE;
00624 }
00625 
00626 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
00627 
00628 static void
00629 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
00630                                struct elf_link_hash_entry *dir,
00631                                struct elf_link_hash_entry *ind)
00632 {
00633   struct elf64_x86_64_link_hash_entry *edir, *eind;
00634 
00635   edir = (struct elf64_x86_64_link_hash_entry *) dir;
00636   eind = (struct elf64_x86_64_link_hash_entry *) ind;
00637 
00638   if (eind->dyn_relocs != NULL)
00639     {
00640       if (edir->dyn_relocs != NULL)
00641        {
00642          struct elf64_x86_64_dyn_relocs **pp;
00643          struct elf64_x86_64_dyn_relocs *p;
00644 
00645          /* Add reloc counts against the indirect sym to the direct sym
00646             list.  Merge any entries against the same section.  */
00647          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
00648            {
00649              struct elf64_x86_64_dyn_relocs *q;
00650 
00651              for (q = edir->dyn_relocs; q != NULL; q = q->next)
00652               if (q->sec == p->sec)
00653                 {
00654                   q->pc_count += p->pc_count;
00655                   q->count += p->count;
00656                   *pp = p->next;
00657                   break;
00658                 }
00659              if (q == NULL)
00660               pp = &p->next;
00661            }
00662          *pp = edir->dyn_relocs;
00663        }
00664 
00665       edir->dyn_relocs = eind->dyn_relocs;
00666       eind->dyn_relocs = NULL;
00667     }
00668 
00669   if (ind->root.type == bfd_link_hash_indirect
00670       && dir->got.refcount <= 0)
00671     {
00672       edir->tls_type = eind->tls_type;
00673       eind->tls_type = GOT_UNKNOWN;
00674     }
00675 
00676   if (ELIMINATE_COPY_RELOCS
00677       && ind->root.type != bfd_link_hash_indirect
00678       && dir->dynamic_adjusted)
00679     {
00680       /* If called to transfer flags for a weakdef during processing
00681         of elf_adjust_dynamic_symbol, don't copy non_got_ref.
00682         We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
00683       dir->ref_dynamic |= ind->ref_dynamic;
00684       dir->ref_regular |= ind->ref_regular;
00685       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
00686       dir->needs_plt |= ind->needs_plt;
00687       dir->pointer_equality_needed |= ind->pointer_equality_needed;
00688     }
00689   else
00690     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
00691 }
00692 
00693 static bfd_boolean
00694 elf64_x86_64_mkobject (bfd *abfd)
00695 {
00696   if (abfd->tdata.any == NULL)
00697     {
00698       bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
00699       abfd->tdata.any = bfd_zalloc (abfd, amt);
00700       if (abfd->tdata.any == NULL)
00701        return FALSE;
00702     }
00703   return bfd_elf_mkobject (abfd);
00704 }
00705 
00706 static bfd_boolean
00707 elf64_x86_64_elf_object_p (bfd *abfd)
00708 {
00709   /* Set the right machine number for an x86-64 elf64 file.  */
00710   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
00711   return TRUE;
00712 }
00713 
00714 static int
00715 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
00716 {
00717   if (info->shared)
00718     return r_type;
00719 
00720   switch (r_type)
00721     {
00722     case R_X86_64_TLSGD:
00723     case R_X86_64_GOTPC32_TLSDESC:
00724     case R_X86_64_TLSDESC_CALL:
00725     case R_X86_64_GOTTPOFF:
00726       if (is_local)
00727        return R_X86_64_TPOFF32;
00728       return R_X86_64_GOTTPOFF;
00729     case R_X86_64_TLSLD:
00730       return R_X86_64_TPOFF32;
00731     }
00732 
00733    return r_type;
00734 }
00735 
00736 /* Look through the relocs for a section during the first phase, and
00737    calculate needed space in the global offset table, procedure
00738    linkage table, and dynamic reloc sections.  */
00739 
00740 static bfd_boolean
00741 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
00742                         const Elf_Internal_Rela *relocs)
00743 {
00744   struct elf64_x86_64_link_hash_table *htab;
00745   Elf_Internal_Shdr *symtab_hdr;
00746   struct elf_link_hash_entry **sym_hashes;
00747   const Elf_Internal_Rela *rel;
00748   const Elf_Internal_Rela *rel_end;
00749   asection *sreloc;
00750 
00751   if (info->relocatable)
00752     return TRUE;
00753 
00754   htab = elf64_x86_64_hash_table (info);
00755   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00756   sym_hashes = elf_sym_hashes (abfd);
00757 
00758   sreloc = NULL;
00759 
00760   rel_end = relocs + sec->reloc_count;
00761   for (rel = relocs; rel < rel_end; rel++)
00762     {
00763       unsigned int r_type;
00764       unsigned long r_symndx;
00765       struct elf_link_hash_entry *h;
00766 
00767       r_symndx = ELF64_R_SYM (rel->r_info);
00768       r_type = ELF64_R_TYPE (rel->r_info);
00769 
00770       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
00771        {
00772          (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
00773                              abfd, r_symndx);
00774          return FALSE;
00775        }
00776 
00777       if (r_symndx < symtab_hdr->sh_info)
00778        h = NULL;
00779       else
00780        {
00781          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00782          while (h->root.type == bfd_link_hash_indirect
00783                || h->root.type == bfd_link_hash_warning)
00784            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00785        }
00786 
00787       r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
00788       switch (r_type)
00789        {
00790        case R_X86_64_TLSLD:
00791          htab->tls_ld_got.refcount += 1;
00792          goto create_got;
00793 
00794        case R_X86_64_TPOFF32:
00795          if (info->shared)
00796            {
00797              (*_bfd_error_handler)
00798               (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
00799                abfd,
00800                x86_64_elf_howto_table[r_type].name,
00801                (h) ? h->root.root.string : "a local symbol");
00802              bfd_set_error (bfd_error_bad_value);
00803              return FALSE;
00804            }
00805          break;
00806 
00807        case R_X86_64_GOTTPOFF:
00808          if (info->shared)
00809            info->flags |= DF_STATIC_TLS;
00810          /* Fall through */
00811 
00812        case R_X86_64_GOT32:
00813        case R_X86_64_GOTPCREL:
00814        case R_X86_64_TLSGD:
00815        case R_X86_64_GOT64:
00816        case R_X86_64_GOTPCREL64:
00817        case R_X86_64_GOTPLT64:
00818        case R_X86_64_GOTPC32_TLSDESC:
00819        case R_X86_64_TLSDESC_CALL:
00820          /* This symbol requires a global offset table entry.  */
00821          {
00822            int tls_type, old_tls_type;
00823 
00824            switch (r_type)
00825              {
00826              default: tls_type = GOT_NORMAL; break;
00827              case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
00828              case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
00829              case R_X86_64_GOTPC32_TLSDESC:
00830              case R_X86_64_TLSDESC_CALL:
00831               tls_type = GOT_TLS_GDESC; break;
00832              }
00833 
00834            if (h != NULL)
00835              {
00836               if (r_type == R_X86_64_GOTPLT64)
00837                 {
00838                   /* This relocation indicates that we also need
00839                      a PLT entry, as this is a function.  We don't need
00840                      a PLT entry for local symbols.  */
00841                   h->needs_plt = 1;
00842                   h->plt.refcount += 1;
00843                 }
00844               h->got.refcount += 1;
00845               old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
00846              }
00847            else
00848              {
00849               bfd_signed_vma *local_got_refcounts;
00850 
00851               /* This is a global offset table entry for a local symbol.  */
00852               local_got_refcounts = elf_local_got_refcounts (abfd);
00853               if (local_got_refcounts == NULL)
00854                 {
00855                   bfd_size_type size;
00856 
00857                   size = symtab_hdr->sh_info;
00858                   size *= sizeof (bfd_signed_vma)
00859                     + sizeof (bfd_vma) + sizeof (char);
00860                   local_got_refcounts = ((bfd_signed_vma *)
00861                                       bfd_zalloc (abfd, size));
00862                   if (local_got_refcounts == NULL)
00863                     return FALSE;
00864                   elf_local_got_refcounts (abfd) = local_got_refcounts;
00865                   elf64_x86_64_local_tlsdesc_gotent (abfd)
00866                     = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
00867                   elf64_x86_64_local_got_tls_type (abfd)
00868                     = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
00869                 }
00870               local_got_refcounts[r_symndx] += 1;
00871               old_tls_type
00872                 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
00873              }
00874 
00875            /* If a TLS symbol is accessed using IE at least once,
00876               there is no point to use dynamic model for it.  */
00877            if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
00878               && (! GOT_TLS_GD_ANY_P (old_tls_type)
00879                   || tls_type != GOT_TLS_IE))
00880              {
00881               if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
00882                 tls_type = old_tls_type;
00883               else if (GOT_TLS_GD_ANY_P (old_tls_type)
00884                       && GOT_TLS_GD_ANY_P (tls_type))
00885                 tls_type |= old_tls_type;
00886               else
00887                 {
00888                   (*_bfd_error_handler)
00889                     (_("%B: %s' accessed both as normal and thread local symbol"),
00890                      abfd, h ? h->root.root.string : "<local>");
00891                   return FALSE;
00892                 }
00893              }
00894 
00895            if (old_tls_type != tls_type)
00896              {
00897               if (h != NULL)
00898                 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
00899               else
00900                 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
00901              }
00902          }
00903          /* Fall through */
00904 
00905        case R_X86_64_GOTOFF64:
00906        case R_X86_64_GOTPC32:
00907        case R_X86_64_GOTPC64:
00908        create_got:
00909          if (htab->sgot == NULL)
00910            {
00911              if (htab->elf.dynobj == NULL)
00912               htab->elf.dynobj = abfd;
00913              if (!create_got_section (htab->elf.dynobj, info))
00914               return FALSE;
00915            }
00916          break;
00917 
00918        case R_X86_64_PLT32:
00919          /* This symbol requires a procedure linkage table entry.  We
00920             actually build the entry in adjust_dynamic_symbol,
00921             because this might be a case of linking PIC code which is
00922             never referenced by a dynamic object, in which case we
00923             don't need to generate a procedure linkage table entry
00924             after all.       */
00925 
00926          /* If this is a local symbol, we resolve it directly without
00927             creating a procedure linkage table entry.   */
00928          if (h == NULL)
00929            continue;
00930 
00931          h->needs_plt = 1;
00932          h->plt.refcount += 1;
00933          break;
00934 
00935        case R_X86_64_PLTOFF64:
00936          /* This tries to form the 'address' of a function relative
00937             to GOT.  For global symbols we need a PLT entry.  */
00938          if (h != NULL)
00939            {
00940              h->needs_plt = 1;
00941              h->plt.refcount += 1;
00942            }
00943          goto create_got;
00944 
00945        case R_X86_64_8:
00946        case R_X86_64_16:
00947        case R_X86_64_32:
00948        case R_X86_64_32S:
00949          /* Let's help debug shared library creation.  These relocs
00950             cannot be used in shared libs.  Don't error out for
00951             sections we don't care about, such as debug sections or
00952             non-constant sections.  */
00953          if (info->shared
00954              && (sec->flags & SEC_ALLOC) != 0
00955              && (sec->flags & SEC_READONLY) != 0)
00956            {
00957              (*_bfd_error_handler)
00958               (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
00959                abfd,
00960                x86_64_elf_howto_table[r_type].name,
00961                (h) ? h->root.root.string : "a local symbol");
00962              bfd_set_error (bfd_error_bad_value);
00963              return FALSE;
00964            }
00965          /* Fall through.  */
00966 
00967        case R_X86_64_PC8:
00968        case R_X86_64_PC16:
00969        case R_X86_64_PC32:
00970        case R_X86_64_PC64:
00971        case R_X86_64_64:
00972          if (h != NULL && !info->shared)
00973            {
00974              /* If this reloc is in a read-only section, we might
00975                need a copy reloc.  We can't check reliably at this
00976                stage whether the section is read-only, as input
00977                sections have not yet been mapped to output sections.
00978                Tentatively set the flag for now, and correct in
00979                adjust_dynamic_symbol.  */
00980              h->non_got_ref = 1;
00981 
00982              /* We may need a .plt entry if the function this reloc
00983                refers to is in a shared lib.  */
00984              h->plt.refcount += 1;
00985              if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
00986               h->pointer_equality_needed = 1;
00987            }
00988 
00989          /* If we are creating a shared library, and this is a reloc
00990             against a global symbol, or a non PC relative reloc
00991             against a local symbol, then we need to copy the reloc
00992             into the shared library.  However, if we are linking with
00993             -Bsymbolic, we do not need to copy a reloc against a
00994             global symbol which is defined in an object we are
00995             including in the link (i.e., DEF_REGULAR is set).  At
00996             this point we have not seen all the input files, so it is
00997             possible that DEF_REGULAR is not set now but will be set
00998             later (it is never cleared).  In case of a weak definition,
00999             DEF_REGULAR may be cleared later by a strong definition in
01000             a shared library.  We account for that possibility below by
01001             storing information in the relocs_copied field of the hash
01002             table entry.  A similar situation occurs when creating
01003             shared libraries and symbol visibility changes render the
01004             symbol local.
01005 
01006             If on the other hand, we are creating an executable, we
01007             may need to keep relocations for symbols satisfied by a
01008             dynamic library if we manage to avoid copy relocs for the
01009             symbol.  */
01010          if ((info->shared
01011               && (sec->flags & SEC_ALLOC) != 0
01012               && (((r_type != R_X86_64_PC8)
01013                   && (r_type != R_X86_64_PC16)
01014                   && (r_type != R_X86_64_PC32)
01015                   && (r_type != R_X86_64_PC64))
01016                  || (h != NULL
01017                      && (! SYMBOLIC_BIND (info, h)
01018                         || h->root.type == bfd_link_hash_defweak
01019                         || !h->def_regular))))
01020              || (ELIMINATE_COPY_RELOCS
01021                 && !info->shared
01022                 && (sec->flags & SEC_ALLOC) != 0
01023                 && h != NULL
01024                 && (h->root.type == bfd_link_hash_defweak
01025                     || !h->def_regular)))
01026            {
01027              struct elf64_x86_64_dyn_relocs *p;
01028              struct elf64_x86_64_dyn_relocs **head;
01029 
01030              /* We must copy these reloc types into the output file.
01031                Create a reloc section in dynobj and make room for
01032                this reloc.  */
01033              if (sreloc == NULL)
01034               {
01035                 const char *name;
01036                 bfd *dynobj;
01037 
01038                 name = (bfd_elf_string_from_elf_section
01039                        (abfd,
01040                         elf_elfheader (abfd)->e_shstrndx,
01041                         elf_section_data (sec)->rel_hdr.sh_name));
01042                 if (name == NULL)
01043                   return FALSE;
01044 
01045                 if (! CONST_STRNEQ (name, ".rela")
01046                     || strcmp (bfd_get_section_name (abfd, sec),
01047                              name + 5) != 0)
01048                   {
01049                     (*_bfd_error_handler)
01050                      (_("%B: bad relocation section name `%s\'"),
01051                       abfd, name);
01052                   }
01053 
01054                 if (htab->elf.dynobj == NULL)
01055                   htab->elf.dynobj = abfd;
01056 
01057                 dynobj = htab->elf.dynobj;
01058 
01059                 sreloc = bfd_get_section_by_name (dynobj, name);
01060                 if (sreloc == NULL)
01061                   {
01062                     flagword flags;
01063 
01064                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
01065                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
01066                     if ((sec->flags & SEC_ALLOC) != 0)
01067                      flags |= SEC_ALLOC | SEC_LOAD;
01068                     sreloc = bfd_make_section_with_flags (dynobj,
01069                                                      name,
01070                                                      flags);
01071                     if (sreloc == NULL
01072                        || ! bfd_set_section_alignment (dynobj, sreloc, 3))
01073                      return FALSE;
01074                   }
01075                 elf_section_data (sec)->sreloc = sreloc;
01076               }
01077 
01078              /* If this is a global symbol, we count the number of
01079                relocations we need for this symbol.  */
01080              if (h != NULL)
01081               {
01082                 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
01083               }
01084              else
01085               {
01086                 void **vpp;
01087                 /* Track dynamic relocs needed for local syms too.
01088                    We really need local syms available to do this
01089                    easily.  Oh well.  */
01090 
01091                 asection *s;
01092                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
01093                                            sec, r_symndx);
01094                 if (s == NULL)
01095                   return FALSE;
01096 
01097                 /* Beware of type punned pointers vs strict aliasing
01098                    rules.  */
01099                 vpp = &(elf_section_data (s)->local_dynrel);
01100                 head = (struct elf64_x86_64_dyn_relocs **)vpp;
01101               }
01102 
01103              p = *head;
01104              if (p == NULL || p->sec != sec)
01105               {
01106                 bfd_size_type amt = sizeof *p;
01107                 p = ((struct elf64_x86_64_dyn_relocs *)
01108                      bfd_alloc (htab->elf.dynobj, amt));
01109                 if (p == NULL)
01110                   return FALSE;
01111                 p->next = *head;
01112                 *head = p;
01113                 p->sec = sec;
01114                 p->count = 0;
01115                 p->pc_count = 0;
01116               }
01117 
01118              p->count += 1;
01119              if (r_type == R_X86_64_PC8
01120                 || r_type == R_X86_64_PC16
01121                 || r_type == R_X86_64_PC32
01122                 || r_type == R_X86_64_PC64)
01123               p->pc_count += 1;
01124            }
01125          break;
01126 
01127          /* This relocation describes the C++ object vtable hierarchy.
01128             Reconstruct it for later use during GC.  */
01129        case R_X86_64_GNU_VTINHERIT:
01130          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
01131            return FALSE;
01132          break;
01133 
01134          /* This relocation describes which C++ vtable entries are actually
01135             used.  Record for later use during GC.  */
01136        case R_X86_64_GNU_VTENTRY:
01137          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
01138            return FALSE;
01139          break;
01140 
01141        default:
01142          break;
01143        }
01144     }
01145 
01146   return TRUE;
01147 }
01148 
01149 /* Return the section that should be marked against GC for a given
01150    relocation.       */
01151 
01152 static asection *
01153 elf64_x86_64_gc_mark_hook (asection *sec,
01154                         struct bfd_link_info *info,
01155                         Elf_Internal_Rela *rel,
01156                         struct elf_link_hash_entry *h,
01157                         Elf_Internal_Sym *sym)
01158 {
01159   if (h != NULL)
01160     switch (ELF64_R_TYPE (rel->r_info))
01161       {
01162       case R_X86_64_GNU_VTINHERIT:
01163       case R_X86_64_GNU_VTENTRY:
01164        return NULL;
01165       }
01166 
01167   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
01168 }
01169 
01170 /* Update the got entry reference counts for the section being removed.       */
01171 
01172 static bfd_boolean
01173 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
01174                          asection *sec, const Elf_Internal_Rela *relocs)
01175 {
01176   Elf_Internal_Shdr *symtab_hdr;
01177   struct elf_link_hash_entry **sym_hashes;
01178   bfd_signed_vma *local_got_refcounts;
01179   const Elf_Internal_Rela *rel, *relend;
01180 
01181   elf_section_data (sec)->local_dynrel = NULL;
01182 
01183   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01184   sym_hashes = elf_sym_hashes (abfd);
01185   local_got_refcounts = elf_local_got_refcounts (abfd);
01186 
01187   relend = relocs + sec->reloc_count;
01188   for (rel = relocs; rel < relend; rel++)
01189     {
01190       unsigned long r_symndx;
01191       unsigned int r_type;
01192       struct elf_link_hash_entry *h = NULL;
01193 
01194       r_symndx = ELF64_R_SYM (rel->r_info);
01195       if (r_symndx >= symtab_hdr->sh_info)
01196        {
01197          struct elf64_x86_64_link_hash_entry *eh;
01198          struct elf64_x86_64_dyn_relocs **pp;
01199          struct elf64_x86_64_dyn_relocs *p;
01200 
01201          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01202          while (h->root.type == bfd_link_hash_indirect
01203                || h->root.type == bfd_link_hash_warning)
01204            h = (struct elf_link_hash_entry *) h->root.u.i.link;
01205          eh = (struct elf64_x86_64_link_hash_entry *) h;
01206 
01207          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
01208            if (p->sec == sec)
01209              {
01210               /* Everything must go for SEC.  */
01211               *pp = p->next;
01212               break;
01213              }
01214        }
01215 
01216       r_type = ELF64_R_TYPE (rel->r_info);
01217       r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
01218       switch (r_type)
01219        {
01220        case R_X86_64_TLSLD:
01221          if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
01222            elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
01223          break;
01224 
01225        case R_X86_64_TLSGD:
01226        case R_X86_64_GOTPC32_TLSDESC:
01227        case R_X86_64_TLSDESC_CALL:
01228        case R_X86_64_GOTTPOFF:
01229        case R_X86_64_GOT32:
01230        case R_X86_64_GOTPCREL:
01231        case R_X86_64_GOT64:
01232        case R_X86_64_GOTPCREL64:
01233        case R_X86_64_GOTPLT64:
01234          if (h != NULL)
01235            {
01236              if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
01237                h->plt.refcount -= 1;
01238              if (h->got.refcount > 0)
01239               h->got.refcount -= 1;
01240            }
01241          else if (local_got_refcounts != NULL)
01242            {
01243              if (local_got_refcounts[r_symndx] > 0)
01244               local_got_refcounts[r_symndx] -= 1;
01245            }
01246          break;
01247 
01248        case R_X86_64_8:
01249        case R_X86_64_16:
01250        case R_X86_64_32:
01251        case R_X86_64_64:
01252        case R_X86_64_32S:
01253        case R_X86_64_PC8:
01254        case R_X86_64_PC16:
01255        case R_X86_64_PC32:
01256        case R_X86_64_PC64:
01257          if (info->shared)
01258            break;
01259          /* Fall thru */
01260 
01261        case R_X86_64_PLT32:
01262        case R_X86_64_PLTOFF64:
01263          if (h != NULL)
01264            {
01265              if (h->plt.refcount > 0)
01266               h->plt.refcount -= 1;
01267            }
01268          break;
01269 
01270        default:
01271          break;
01272        }
01273     }
01274 
01275   return TRUE;
01276 }
01277 
01278 /* Adjust a symbol defined by a dynamic object and referenced by a
01279    regular object.  The current definition is in some section of the
01280    dynamic object, but we're not including those sections.  We have to
01281    change the definition to something the rest of the link can
01282    understand.       */
01283 
01284 static bfd_boolean
01285 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
01286                                 struct elf_link_hash_entry *h)
01287 {
01288   struct elf64_x86_64_link_hash_table *htab;
01289   asection *s;
01290   unsigned int power_of_two;
01291 
01292   /* If this is a function, put it in the procedure linkage table.  We
01293      will fill in the contents of the procedure linkage table later,
01294      when we know the address of the .got section.  */
01295   if (h->type == STT_FUNC
01296       || h->needs_plt)
01297     {
01298       if (h->plt.refcount <= 0
01299          || SYMBOL_CALLS_LOCAL (info, h)
01300          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
01301              && h->root.type == bfd_link_hash_undefweak))
01302        {
01303          /* This case can occur if we saw a PLT32 reloc in an input
01304             file, but the symbol was never referred to by a dynamic
01305             object, or if all references were garbage collected.  In
01306             such a case, we don't actually need to build a procedure
01307             linkage table, and we can just do a PC32 reloc instead.  */
01308          h->plt.offset = (bfd_vma) -1;
01309          h->needs_plt = 0;
01310        }
01311 
01312       return TRUE;
01313     }
01314   else
01315     /* It's possible that we incorrectly decided a .plt reloc was
01316        needed for an R_X86_64_PC32 reloc to a non-function sym in
01317        check_relocs.  We can't decide accurately between function and
01318        non-function syms in check-relocs;  Objects loaded later in
01319        the link may change h->type.  So fix it now.  */
01320     h->plt.offset = (bfd_vma) -1;
01321 
01322   /* If this is a weak symbol, and there is a real definition, the
01323      processor independent code will have arranged for us to see the
01324      real definition first, and we can just use the same value.        */
01325   if (h->u.weakdef != NULL)
01326     {
01327       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
01328                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
01329       h->root.u.def.section = h->u.weakdef->root.u.def.section;
01330       h->root.u.def.value = h->u.weakdef->root.u.def.value;
01331       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
01332        h->non_got_ref = h->u.weakdef->non_got_ref;
01333       return TRUE;
01334     }
01335 
01336   /* This is a reference to a symbol defined by a dynamic object which
01337      is not a function.      */
01338 
01339   /* If we are creating a shared library, we must presume that the
01340      only references to the symbol are via the global offset table.
01341      For such cases we need not do anything here; the relocations will
01342      be handled correctly by relocate_section.   */
01343   if (info->shared)
01344     return TRUE;
01345 
01346   /* If there are no references to this symbol that do not use the
01347      GOT, we don't need to generate a copy reloc.  */
01348   if (!h->non_got_ref)
01349     return TRUE;
01350 
01351   /* If -z nocopyreloc was given, we won't generate them either.  */
01352   if (info->nocopyreloc)
01353     {
01354       h->non_got_ref = 0;
01355       return TRUE;
01356     }
01357 
01358   if (ELIMINATE_COPY_RELOCS)
01359     {
01360       struct elf64_x86_64_link_hash_entry * eh;
01361       struct elf64_x86_64_dyn_relocs *p;
01362 
01363       eh = (struct elf64_x86_64_link_hash_entry *) h;
01364       for (p = eh->dyn_relocs; p != NULL; p = p->next)
01365        {
01366          s = p->sec->output_section;
01367          if (s != NULL && (s->flags & SEC_READONLY) != 0)
01368            break;
01369        }
01370 
01371       /* If we didn't find any dynamic relocs in read-only sections, then
01372         we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
01373       if (p == NULL)
01374        {
01375          h->non_got_ref = 0;
01376          return TRUE;
01377        }
01378     }
01379 
01380   if (h->size == 0)
01381     {
01382       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
01383                           h->root.root.string);
01384       return TRUE;
01385     }
01386 
01387   /* We must allocate the symbol in our .dynbss section, which will
01388      become part of the .bss section of the executable.  There will be
01389      an entry for this symbol in the .dynsym section.  The dynamic
01390      object will contain position independent code, so all references
01391      from the dynamic object to this symbol will go through the global
01392      offset table.  The dynamic linker will use the .dynsym entry to
01393      determine the address it must put in the global offset table, so
01394      both the dynamic object and the regular object will refer to the
01395      same memory location for the variable.  */
01396 
01397   htab = elf64_x86_64_hash_table (info);
01398 
01399   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
01400      to copy the initial value out of the dynamic object and into the
01401      runtime process image.  */
01402   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
01403     {
01404       htab->srelbss->size += sizeof (Elf64_External_Rela);
01405       h->needs_copy = 1;
01406     }
01407 
01408   /* We need to figure out the alignment required for this symbol.  I
01409      have no idea how ELF linkers handle this.   16-bytes is the size
01410      of the largest type that requires hard alignment -- long double.  */
01411   /* FIXME: This is VERY ugly. Should be fixed for all architectures using
01412      this construct.  */
01413   power_of_two = bfd_log2 (h->size);
01414   if (power_of_two > 4)
01415     power_of_two = 4;
01416 
01417   /* Apply the required alignment.  */
01418   s = htab->sdynbss;
01419   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
01420   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
01421     {
01422       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
01423        return FALSE;
01424     }
01425 
01426   /* Define the symbol as being at this point in the section.  */
01427   h->root.u.def.section = s;
01428   h->root.u.def.value = s->size;
01429 
01430   /* Increment the section size to make room for the symbol.  */
01431   s->size += h->size;
01432 
01433   return TRUE;
01434 }
01435 
01436 /* Allocate space in .plt, .got and associated reloc sections for
01437    dynamic relocs.  */
01438 
01439 static bfd_boolean
01440 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
01441 {
01442   struct bfd_link_info *info;
01443   struct elf64_x86_64_link_hash_table *htab;
01444   struct elf64_x86_64_link_hash_entry *eh;
01445   struct elf64_x86_64_dyn_relocs *p;
01446 
01447   if (h->root.type == bfd_link_hash_indirect)
01448     return TRUE;
01449 
01450   if (h->root.type == bfd_link_hash_warning)
01451     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01452 
01453   info = (struct bfd_link_info *) inf;
01454   htab = elf64_x86_64_hash_table (info);
01455 
01456   if (htab->elf.dynamic_sections_created
01457       && h->plt.refcount > 0)
01458     {
01459       /* Make sure this symbol is output as a dynamic symbol.
01460         Undefined weak syms won't yet be marked as dynamic.  */
01461       if (h->dynindx == -1
01462          && !h->forced_local)
01463        {
01464          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01465            return FALSE;
01466        }
01467 
01468       if (info->shared
01469          || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
01470        {
01471          asection *s = htab->splt;
01472 
01473          /* If this is the first .plt entry, make room for the special
01474             first entry.  */
01475          if (s->size == 0)
01476            s->size += PLT_ENTRY_SIZE;
01477 
01478          h->plt.offset = s->size;
01479 
01480          /* If this symbol is not defined in a regular file, and we are
01481             not generating a shared library, then set the symbol to this
01482             location in the .plt.  This is required to make function
01483             pointers compare as equal between the normal executable and
01484             the shared library.  */
01485          if (! info->shared
01486              && !h->def_regular)
01487            {
01488              h->root.u.def.section = s;
01489              h->root.u.def.value = h->plt.offset;
01490            }
01491 
01492          /* Make room for this entry.  */
01493          s->size += PLT_ENTRY_SIZE;
01494 
01495          /* We also need to make an entry in the .got.plt section, which
01496             will be placed in the .got section by the linker script.  */
01497          htab->sgotplt->size += GOT_ENTRY_SIZE;
01498 
01499          /* We also need to make an entry in the .rela.plt section.  */
01500          htab->srelplt->size += sizeof (Elf64_External_Rela);
01501          htab->srelplt->reloc_count++;
01502        }
01503       else
01504        {
01505          h->plt.offset = (bfd_vma) -1;
01506          h->needs_plt = 0;
01507        }
01508     }
01509   else
01510     {
01511       h->plt.offset = (bfd_vma) -1;
01512       h->needs_plt = 0;
01513     }
01514 
01515   eh = (struct elf64_x86_64_link_hash_entry *) h;
01516   eh->tlsdesc_got = (bfd_vma) -1;
01517 
01518   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
01519      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
01520   if (h->got.refcount > 0
01521       && !info->shared
01522       && h->dynindx == -1
01523       && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
01524     h->got.offset = (bfd_vma) -1;
01525   else if (h->got.refcount > 0)
01526     {
01527       asection *s;
01528       bfd_boolean dyn;
01529       int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
01530 
01531       /* Make sure this symbol is output as a dynamic symbol.
01532         Undefined weak syms won't yet be marked as dynamic.  */
01533       if (h->dynindx == -1
01534          && !h->forced_local)
01535        {
01536          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01537            return FALSE;
01538        }
01539 
01540       if (GOT_TLS_GDESC_P (tls_type))
01541        {
01542          eh->tlsdesc_got = htab->sgotplt->size
01543            - elf64_x86_64_compute_jump_table_size (htab);
01544          htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
01545          h->got.offset = (bfd_vma) -2;
01546        }
01547       if (! GOT_TLS_GDESC_P (tls_type)
01548          || GOT_TLS_GD_P (tls_type))
01549        {
01550          s = htab->sgot;
01551          h->got.offset = s->size;
01552          s->size += GOT_ENTRY_SIZE;
01553          if (GOT_TLS_GD_P (tls_type))
01554            s->size += GOT_ENTRY_SIZE;
01555        }
01556       dyn = htab->elf.dynamic_sections_created;
01557       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
01558         and two if global.
01559         R_X86_64_GOTTPOFF needs one dynamic relocation.  */
01560       if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
01561          || tls_type == GOT_TLS_IE)
01562        htab->srelgot->size += sizeof (Elf64_External_Rela);
01563       else if (GOT_TLS_GD_P (tls_type))
01564        htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
01565       else if (! GOT_TLS_GDESC_P (tls_type)
01566               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
01567                  || h->root.type != bfd_link_hash_undefweak)
01568               && (info->shared
01569                  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
01570        htab->srelgot->size += sizeof (Elf64_External_Rela);
01571       if (GOT_TLS_GDESC_P (tls_type))
01572        {
01573          htab->srelplt->size += sizeof (Elf64_External_Rela);
01574          htab->tlsdesc_plt = (bfd_vma) -1;
01575        }
01576     }
01577   else
01578     h->got.offset = (bfd_vma) -1;
01579 
01580   if (eh->dyn_relocs == NULL)
01581     return TRUE;
01582 
01583   /* In the shared -Bsymbolic case, discard space allocated for
01584      dynamic pc-relative relocs against symbols which turn out to be
01585      defined in regular objects.  For the normal shared case, discard
01586      space for pc-relative relocs that have become local due to symbol
01587      visibility changes.  */
01588 
01589   if (info->shared)
01590     {
01591       /* Relocs that use pc_count are those that appear on a call
01592         insn, or certain REL relocs that can generated via assembly.
01593         We want calls to protected symbols to resolve directly to the
01594         function rather than going via the plt.  If people want
01595         function pointer comparisons to work as expected then they
01596         should avoid writing weird assembly.  */
01597       if (SYMBOL_CALLS_LOCAL (info, h))
01598        {
01599          struct elf64_x86_64_dyn_relocs **pp;
01600 
01601          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
01602            {
01603              p->count -= p->pc_count;
01604              p->pc_count = 0;
01605              if (p->count == 0)
01606               *pp = p->next;
01607              else
01608               pp = &p->next;
01609            }
01610        }
01611 
01612       /* Also discard relocs on undefined weak syms with non-default
01613         visibility.  */
01614       if (eh->dyn_relocs != NULL
01615          && h->root.type == bfd_link_hash_undefweak)
01616        {
01617          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
01618            eh->dyn_relocs = NULL;
01619 
01620          /* Make sure undefined weak symbols are output as a dynamic
01621             symbol in PIEs.  */
01622          else if (h->dynindx == -1
01623                  && !h->forced_local)
01624            {
01625              if (! bfd_elf_link_record_dynamic_symbol (info, h))
01626               return FALSE;
01627            }
01628        }
01629     }
01630   else if (ELIMINATE_COPY_RELOCS)
01631     {
01632       /* For the non-shared case, discard space for relocs against
01633         symbols which turn out to need copy relocs or are not
01634         dynamic.  */
01635 
01636       if (!h->non_got_ref
01637          && ((h->def_dynamic
01638               && !h->def_regular)
01639              || (htab->elf.dynamic_sections_created
01640                 && (h->root.type == bfd_link_hash_undefweak
01641                     || h->root.type == bfd_link_hash_undefined))))
01642        {
01643          /* Make sure this symbol is output as a dynamic symbol.
01644             Undefined weak syms won't yet be marked as dynamic.  */
01645          if (h->dynindx == -1
01646              && !h->forced_local)
01647            {
01648              if (! bfd_elf_link_record_dynamic_symbol (info, h))
01649               return FALSE;
01650            }
01651 
01652          /* If that succeeded, we know we'll be keeping all the
01653             relocs.  */
01654          if (h->dynindx != -1)
01655            goto keep;
01656        }
01657 
01658       eh->dyn_relocs = NULL;
01659 
01660     keep: ;
01661     }
01662 
01663   /* Finally, allocate space.  */
01664   for (p = eh->dyn_relocs; p != NULL; p = p->next)
01665     {
01666       asection *sreloc = elf_section_data (p->sec)->sreloc;
01667       sreloc->size += p->count * sizeof (Elf64_External_Rela);
01668     }
01669 
01670   return TRUE;
01671 }
01672 
01673 /* Find any dynamic relocs that apply to read-only sections.  */
01674 
01675 static bfd_boolean
01676 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
01677 {
01678   struct elf64_x86_64_link_hash_entry *eh;
01679   struct elf64_x86_64_dyn_relocs *p;
01680 
01681   if (h->root.type == bfd_link_hash_warning)
01682     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01683 
01684   eh = (struct elf64_x86_64_link_hash_entry *) h;
01685   for (p = eh->dyn_relocs; p != NULL; p = p->next)
01686     {
01687       asection *s = p->sec->output_section;
01688 
01689       if (s != NULL && (s->flags & SEC_READONLY) != 0)
01690        {
01691          struct bfd_link_info *info = (struct bfd_link_info *) inf;
01692 
01693          info->flags |= DF_TEXTREL;
01694 
01695          /* Not an error, just cut short the traversal.  */
01696          return FALSE;
01697        }
01698     }
01699   return TRUE;
01700 }
01701 
01702 /* Set the sizes of the dynamic sections.  */
01703 
01704 static bfd_boolean
01705 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
01706                                 struct bfd_link_info *info)
01707 {
01708   struct elf64_x86_64_link_hash_table *htab;
01709   bfd *dynobj;
01710   asection *s;
01711   bfd_boolean relocs;
01712   bfd *ibfd;
01713 
01714   htab = elf64_x86_64_hash_table (info);
01715   dynobj = htab->elf.dynobj;
01716   if (dynobj == NULL)
01717     abort ();
01718 
01719   if (htab->elf.dynamic_sections_created)
01720     {
01721       /* Set the contents of the .interp section to the interpreter.  */
01722       if (info->executable)
01723        {
01724          s = bfd_get_section_by_name (dynobj, ".interp");
01725          if (s == NULL)
01726            abort ();
01727          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
01728          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
01729        }
01730     }
01731 
01732   /* Set up .got offsets for local syms, and space for local dynamic
01733      relocs.  */
01734   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
01735     {
01736       bfd_signed_vma *local_got;
01737       bfd_signed_vma *end_local_got;
01738       char *local_tls_type;
01739       bfd_vma *local_tlsdesc_gotent;
01740       bfd_size_type locsymcount;
01741       Elf_Internal_Shdr *symtab_hdr;
01742       asection *srel;
01743 
01744       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
01745        continue;
01746 
01747       for (s = ibfd->sections; s != NULL; s = s->next)
01748        {
01749          struct elf64_x86_64_dyn_relocs *p;
01750 
01751          for (p = (struct elf64_x86_64_dyn_relocs *)
01752                   (elf_section_data (s)->local_dynrel);
01753               p != NULL;
01754               p = p->next)
01755            {
01756              if (!bfd_is_abs_section (p->sec)
01757                 && bfd_is_abs_section (p->sec->output_section))
01758               {
01759                 /* Input section has been discarded, either because
01760                    it is a copy of a linkonce section or due to
01761                    linker script /DISCARD/, so we'll be discarding
01762                    the relocs too.  */
01763               }
01764              else if (p->count != 0)
01765               {
01766                 srel = elf_section_data (p->sec)->sreloc;
01767                 srel->size += p->count * sizeof (Elf64_External_Rela);
01768                 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
01769                   info->flags |= DF_TEXTREL;
01770 
01771               }
01772            }
01773        }
01774 
01775       local_got = elf_local_got_refcounts (ibfd);
01776       if (!local_got)
01777        continue;
01778 
01779       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
01780       locsymcount = symtab_hdr->sh_info;
01781       end_local_got = local_got + locsymcount;
01782       local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
01783       local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
01784       s = htab->sgot;
01785       srel = htab->srelgot;
01786       for (; local_got < end_local_got;
01787           ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
01788        {
01789          *local_tlsdesc_gotent = (bfd_vma) -1;
01790          if (*local_got > 0)
01791            {
01792              if (GOT_TLS_GDESC_P (*local_tls_type))
01793               {
01794                 *local_tlsdesc_gotent = htab->sgotplt->size
01795                   - elf64_x86_64_compute_jump_table_size (htab);
01796                 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
01797                 *local_got = (bfd_vma) -2;
01798               }
01799              if (! GOT_TLS_GDESC_P (*local_tls_type)
01800                 || GOT_TLS_GD_P (*local_tls_type))
01801               {
01802                 *local_got = s->size;
01803                 s->size += GOT_ENTRY_SIZE;
01804                 if (GOT_TLS_GD_P (*local_tls_type))
01805                   s->size += GOT_ENTRY_SIZE;
01806               }
01807              if (info->shared
01808                 || GOT_TLS_GD_ANY_P (*local_tls_type)
01809                 || *local_tls_type == GOT_TLS_IE)
01810               {
01811                 if (GOT_TLS_GDESC_P (*local_tls_type))
01812                   {
01813                     htab->srelplt->size += sizeof (Elf64_External_Rela);
01814                     htab->tlsdesc_plt = (bfd_vma) -1;
01815                   }
01816                 if (! GOT_TLS_GDESC_P (*local_tls_type)
01817                     || GOT_TLS_GD_P (*local_tls_type))
01818                   srel->size += sizeof (Elf64_External_Rela);
01819               }
01820            }
01821          else
01822            *local_got = (bfd_vma) -1;
01823        }
01824     }
01825 
01826   if (htab->tls_ld_got.refcount > 0)
01827     {
01828       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
01829         relocs.  */
01830       htab->tls_ld_got.offset = htab->sgot->size;
01831       htab->sgot->size += 2 * GOT_ENTRY_SIZE;
01832       htab->srelgot->size += sizeof (Elf64_External_Rela);
01833     }
01834   else
01835     htab->tls_ld_got.offset = -1;
01836 
01837   /* Allocate global sym .plt and .got entries, and space for global
01838      sym dynamic relocs.  */
01839   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
01840 
01841   /* For every jump slot reserved in the sgotplt, reloc_count is
01842      incremented.  However, when we reserve space for TLS descriptors,
01843      it's not incremented, so in order to compute the space reserved
01844      for them, it suffices to multiply the reloc count by the jump
01845      slot size.  */
01846   if (htab->srelplt)
01847     htab->sgotplt_jump_table_size
01848       = elf64_x86_64_compute_jump_table_size (htab);
01849 
01850   if (htab->tlsdesc_plt)
01851     {
01852       /* If we're not using lazy TLS relocations, don't generate the
01853         PLT and GOT entries they require.  */
01854       if ((info->flags & DF_BIND_NOW))
01855        htab->tlsdesc_plt = 0;
01856       else
01857        {
01858          htab->tlsdesc_got = htab->sgot->size;
01859          htab->sgot->size += GOT_ENTRY_SIZE;
01860          /* Reserve room for the initial entry.
01861             FIXME: we could probably do away with it in this case.  */
01862          if (htab->splt->size == 0)
01863            htab->splt->size += PLT_ENTRY_SIZE;
01864          htab->tlsdesc_plt = htab->splt->size;
01865          htab->splt->size += PLT_ENTRY_SIZE;
01866        }
01867     }
01868 
01869   /* We now have determined the sizes of the various dynamic sections.
01870      Allocate memory for them.  */
01871   relocs = FALSE;
01872   for (s = dynobj->sections; s != NULL; s = s->next)
01873     {
01874       if ((s->flags & SEC_LINKER_CREATED) == 0)
01875        continue;
01876 
01877       if (s == htab->splt
01878          || s == htab->sgot
01879          || s == htab->sgotplt
01880          || s == htab->sdynbss)
01881        {
01882          /* Strip this section if we don't need it; see the
01883             comment below.  */
01884        }
01885       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
01886        {
01887          if (s->size != 0 && s != htab->srelplt)
01888            relocs = TRUE;
01889 
01890          /* We use the reloc_count field as a counter if we need
01891             to copy relocs into the output file.  */
01892          if (s != htab->srelplt)
01893            s->reloc_count = 0;
01894        }
01895       else
01896        {
01897          /* It's not one of our sections, so don't allocate space.  */
01898          continue;
01899        }
01900 
01901       if (s->size == 0)
01902        {
01903          /* If we don't need this section, strip it from the
01904             output file.  This is mostly to handle .rela.bss and
01905             .rela.plt.  We must create both sections in
01906             create_dynamic_sections, because they must be created
01907             before the linker maps input sections to output
01908             sections.  The linker does that before
01909             adjust_dynamic_symbol is called, and it is that
01910             function which decides whether anything needs to go
01911             into these sections.  */
01912 
01913          s->flags |= SEC_EXCLUDE;
01914          continue;
01915        }
01916 
01917       if ((s->flags & SEC_HAS_CONTENTS) == 0)
01918        continue;
01919 
01920       /* Allocate memory for the section contents.  We use bfd_zalloc
01921         here in case unused entries are not reclaimed before the
01922         section's contents are written out.  This should not happen,
01923         but this way if it does, we get a R_X86_64_NONE reloc instead
01924         of garbage.  */
01925       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
01926       if (s->contents == NULL)
01927        return FALSE;
01928     }
01929 
01930   if (htab->elf.dynamic_sections_created)
01931     {
01932       /* Add some entries to the .dynamic section.  We fill in the
01933         values later, in elf64_x86_64_finish_dynamic_sections, but we
01934         must add the entries now so that we get the correct size for
01935         the .dynamic section.      The DT_DEBUG entry is filled in by the
01936         dynamic linker and used by the debugger.  */
01937 #define add_dynamic_entry(TAG, VAL) \
01938   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
01939 
01940       if (info->executable)
01941        {
01942          if (!add_dynamic_entry (DT_DEBUG, 0))
01943            return FALSE;
01944        }
01945 
01946       if (htab->splt->size != 0)
01947        {
01948          if (!add_dynamic_entry (DT_PLTGOT, 0)
01949              || !add_dynamic_entry (DT_PLTRELSZ, 0)
01950              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
01951              || !add_dynamic_entry (DT_JMPREL, 0))
01952            return FALSE;
01953 
01954          if (htab->tlsdesc_plt
01955              && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
01956                 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
01957            return FALSE;
01958        }
01959 
01960       if (relocs)
01961        {
01962          if (!add_dynamic_entry (DT_RELA, 0)
01963              || !add_dynamic_entry (DT_RELASZ, 0)
01964              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
01965            return FALSE;
01966 
01967          /* If any dynamic relocs apply to a read-only section,
01968             then we need a DT_TEXTREL entry.  */
01969          if ((info->flags & DF_TEXTREL) == 0)
01970            elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
01971                                 (PTR) info);
01972 
01973          if ((info->flags & DF_TEXTREL) != 0)
01974            {
01975              if (!add_dynamic_entry (DT_TEXTREL, 0))
01976               return FALSE;
01977            }
01978        }
01979     }
01980 #undef add_dynamic_entry
01981 
01982   return TRUE;
01983 }
01984 
01985 static bfd_boolean
01986 elf64_x86_64_always_size_sections (bfd *output_bfd,
01987                                struct bfd_link_info *info)
01988 {
01989   asection *tls_sec = elf_hash_table (info)->tls_sec;
01990 
01991   if (tls_sec)
01992     {
01993       struct elf_link_hash_entry *tlsbase;
01994 
01995       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
01996                                   "_TLS_MODULE_BASE_",
01997                                   FALSE, FALSE, FALSE);
01998 
01999       if (tlsbase && tlsbase->type == STT_TLS)
02000        {
02001          struct bfd_link_hash_entry *bh = NULL;
02002          const struct elf_backend_data *bed
02003            = get_elf_backend_data (output_bfd);
02004 
02005          if (!(_bfd_generic_link_add_one_symbol
02006               (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
02007                tls_sec, 0, NULL, FALSE,
02008                bed->collect, &bh)))
02009            return FALSE;
02010          tlsbase = (struct elf_link_hash_entry *)bh;
02011          tlsbase->def_regular = 1;
02012          tlsbase->other = STV_HIDDEN;
02013          (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
02014        }
02015     }
02016 
02017   return TRUE;
02018 }
02019 
02020 /* Return the base VMA address which should be subtracted from real addresses
02021    when resolving @dtpoff relocation.
02022    This is PT_TLS segment p_vaddr.  */
02023 
02024 static bfd_vma
02025 dtpoff_base (struct bfd_link_info *info)
02026 {
02027   /* If tls_sec is NULL, we should have signalled an error already.  */
02028   if (elf_hash_table (info)->tls_sec == NULL)
02029     return 0;
02030   return elf_hash_table (info)->tls_sec->vma;
02031 }
02032 
02033 /* Return the relocation value for @tpoff relocation
02034    if STT_TLS virtual address is ADDRESS.  */
02035 
02036 static bfd_vma
02037 tpoff (struct bfd_link_info *info, bfd_vma address)
02038 {
02039   struct elf_link_hash_table *htab = elf_hash_table (info);
02040 
02041   /* If tls_segment is NULL, we should have signalled an error already.  */
02042   if (htab->tls_sec == NULL)
02043     return 0;
02044   return address - htab->tls_size - htab->tls_sec->vma;
02045 }
02046 
02047 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
02048    branch?  */
02049 
02050 static bfd_boolean
02051 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
02052 {
02053   /* Opcode          Instruction
02054      0xe8            call
02055      0xe9            jump
02056      0x0f 0x8x              conditional jump */
02057   return ((offset > 0
02058           && (contents [offset - 1] == 0xe8
02059               || contents [offset - 1] == 0xe9))
02060          || (offset > 1
02061              && contents [offset - 2] == 0x0f
02062              && (contents [offset - 1] & 0xf0) == 0x80));
02063 }
02064 
02065 /* Relocate an x86_64 ELF section.  */
02066 
02067 static bfd_boolean
02068 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
02069                             bfd *input_bfd, asection *input_section,
02070                             bfd_byte *contents, Elf_Internal_Rela *relocs,
02071                             Elf_Internal_Sym *local_syms,
02072                             asection **local_sections)
02073 {
02074   struct elf64_x86_64_link_hash_table *htab;
02075   Elf_Internal_Shdr *symtab_hdr;
02076   struct elf_link_hash_entry **sym_hashes;
02077   bfd_vma *local_got_offsets;
02078   bfd_vma *local_tlsdesc_gotents;
02079   Elf_Internal_Rela *rel;
02080   Elf_Internal_Rela *relend;
02081 
02082   htab = elf64_x86_64_hash_table (info);
02083   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02084   sym_hashes = elf_sym_hashes (input_bfd);
02085   local_got_offsets = elf_local_got_offsets (input_bfd);
02086   local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
02087 
02088   rel = relocs;
02089   relend = relocs + input_section->reloc_count;
02090   for (; rel < relend; rel++)
02091     {
02092       unsigned int r_type;
02093       reloc_howto_type *howto;
02094       unsigned long r_symndx;
02095       struct elf_link_hash_entry *h;
02096       Elf_Internal_Sym *sym;
02097       asection *sec;
02098       bfd_vma off, offplt;
02099       bfd_vma relocation;
02100       bfd_boolean unresolved_reloc;
02101       bfd_reloc_status_type r;
02102       int tls_type;
02103 
02104       r_type = ELF64_R_TYPE (rel->r_info);
02105       if (r_type == (int) R_X86_64_GNU_VTINHERIT
02106          || r_type == (int) R_X86_64_GNU_VTENTRY)
02107        continue;
02108 
02109       if (r_type >= R_X86_64_max)
02110        {
02111          bfd_set_error (bfd_error_bad_value);
02112          return FALSE;
02113        }
02114 
02115       howto = x86_64_elf_howto_table + r_type;
02116       r_symndx = ELF64_R_SYM (rel->r_info);
02117       h = NULL;
02118       sym = NULL;
02119       sec = NULL;
02120       unresolved_reloc = FALSE;
02121       if (r_symndx < symtab_hdr->sh_info)
02122        {
02123          sym = local_syms + r_symndx;
02124          sec = local_sections[r_symndx];
02125 
02126          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
02127        }
02128       else
02129        {
02130          bfd_boolean warned;
02131 
02132          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
02133                                r_symndx, symtab_hdr, sym_hashes,
02134                                h, sec, relocation,
02135                                unresolved_reloc, warned);
02136        }
02137 
02138       if (sec != NULL && elf_discarded_section (sec))
02139        {
02140          /* For relocs against symbols from removed linkonce sections,
02141             or sections discarded by a linker script, we just want the
02142             section contents zeroed.  Avoid any special processing.  */
02143          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
02144          rel->r_info = 0;
02145          rel->r_addend = 0;
02146          continue;
02147        }
02148 
02149       if (info->relocatable)
02150        continue;
02151 
02152       /* When generating a shared object, the relocations handled here are
02153         copied into the output file to be resolved at run time.  */
02154       switch (r_type)
02155        {
02156        asection *base_got;
02157        case R_X86_64_GOT32:
02158        case R_X86_64_GOT64:
02159          /* Relocation is to the entry for this symbol in the global
02160             offset table.  */
02161        case R_X86_64_GOTPCREL:
02162        case R_X86_64_GOTPCREL64:
02163          /* Use global offset table entry as symbol value.  */
02164        case R_X86_64_GOTPLT64:
02165          /* This is the same as GOT64 for relocation purposes, but
02166             indicates the existence of a PLT entry.  The difficulty is,
02167             that we must calculate the GOT slot offset from the PLT
02168             offset, if this symbol got a PLT entry (it was global).
02169             Additionally if it's computed from the PLT entry, then that
02170             GOT offset is relative to .got.plt, not to .got.  */
02171          base_got = htab->sgot;
02172 
02173          if (htab->sgot == NULL)
02174            abort ();
02175 
02176          if (h != NULL)
02177            {
02178              bfd_boolean dyn;
02179 
02180              off = h->got.offset;
02181              if (h->needs_plt
02182                  && h->plt.offset != (bfd_vma)-1
02183                 && off == (bfd_vma)-1)
02184               {
02185                 /* We can't use h->got.offset here to save
02186                    state, or even just remember the offset, as
02187                    finish_dynamic_symbol would use that as offset into
02188                    .got.  */
02189                 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
02190                 off = (plt_index + 3) * GOT_ENTRY_SIZE;
02191                 base_got = htab->sgotplt;
02192               }
02193 
02194              dyn = htab->elf.dynamic_sections_created;
02195 
02196              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
02197                 || (info->shared
02198                     && SYMBOL_REFERENCES_LOCAL (info, h))
02199                 || (ELF_ST_VISIBILITY (h->other)
02200                     && h->root.type == bfd_link_hash_undefweak))
02201               {
02202                 /* This is actually a static link, or it is a -Bsymbolic
02203                    link and the symbol is defined locally, or the symbol
02204                    was forced to be local because of a version file.  We
02205                    must initialize this entry in the global offset table.
02206                    Since the offset must always be a multiple of 8, we
02207                    use the least significant bit to record whether we
02208                    have initialized it already.
02209 
02210                    When doing a dynamic link, we create a .rela.got
02211                    relocation entry to initialize the value.   This is
02212                    done in the finish_dynamic_symbol routine.   */
02213                 if ((off & 1) != 0)
02214                   off &= ~1;
02215                 else
02216                   {
02217                     bfd_put_64 (output_bfd, relocation,
02218                               base_got->contents + off);
02219                     /* Note that this is harmless for the GOTPLT64 case,
02220                        as -1 | 1 still is -1.  */
02221                     h->got.offset |= 1;
02222                   }
02223               }
02224              else
02225               unresolved_reloc = FALSE;
02226            }
02227          else
02228            {
02229              if (local_got_offsets == NULL)
02230               abort ();
02231 
02232              off = local_got_offsets[r_symndx];
02233 
02234              /* The offset must always be a multiple of 8.  We use
02235                the least significant bit to record whether we have
02236                already generated the necessary reloc.    */
02237              if ((off & 1) != 0)
02238               off &= ~1;
02239              else
02240               {
02241                 bfd_put_64 (output_bfd, relocation,
02242                            base_got->contents + off);
02243 
02244                 if (info->shared)
02245                   {
02246                     asection *s;
02247                     Elf_Internal_Rela outrel;
02248                     bfd_byte *loc;
02249 
02250                     /* We need to generate a R_X86_64_RELATIVE reloc
02251                       for the dynamic linker.  */
02252                     s = htab->srelgot;
02253                     if (s == NULL)
02254                      abort ();
02255 
02256                     outrel.r_offset = (base_got->output_section->vma
02257                                     + base_got->output_offset
02258                                     + off);
02259                     outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
02260                     outrel.r_addend = relocation;
02261                     loc = s->contents;
02262                     loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
02263                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02264                   }
02265 
02266                 local_got_offsets[r_symndx] |= 1;
02267               }
02268            }
02269 
02270          if (off >= (bfd_vma) -2)
02271            abort ();
02272 
02273          relocation = base_got->output_section->vma
02274                      + base_got->output_offset + off;
02275          if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
02276            relocation -= htab->sgotplt->output_section->vma
02277                        - htab->sgotplt->output_offset;
02278 
02279          break;
02280 
02281        case R_X86_64_GOTOFF64:
02282          /* Relocation is relative to the start of the global offset
02283             table.  */
02284 
02285          /* Check to make sure it isn't a protected function symbol
02286             for shared library since it may not be local when used
02287             as function address.  */
02288          if (info->shared
02289              && h
02290              && h->def_regular
02291              && h->type == STT_FUNC
02292              && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
02293            {
02294              (*_bfd_error_handler)
02295               (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
02296                input_bfd, h->root.root.string);
02297              bfd_set_error (bfd_error_bad_value);
02298              return FALSE;
02299            }
02300 
02301          /* Note that sgot is not involved in this
02302             calculation.  We always want the start of .got.plt.  If we
02303             defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
02304             permitted by the ABI, we might have to change this
02305             calculation.  */
02306          relocation -= htab->sgotplt->output_section->vma
02307                      + htab->sgotplt->output_offset;
02308          break;
02309 
02310        case R_X86_64_GOTPC32:
02311        case R_X86_64_GOTPC64:
02312          /* Use global offset table as symbol value.  */
02313          relocation = htab->sgotplt->output_section->vma
02314                      + htab->sgotplt->output_offset;
02315          unresolved_reloc = FALSE;
02316          break;
02317 
02318        case R_X86_64_PLTOFF64:
02319          /* Relocation is PLT entry relative to GOT.  For local
02320             symbols it's the symbol itself relative to GOT.  */
02321           if (h != NULL
02322              /* See PLT32 handling.  */
02323              && h->plt.offset != (bfd_vma) -1
02324              && htab->splt != NULL)
02325            {
02326              relocation = (htab->splt->output_section->vma
02327                          + htab->splt->output_offset
02328                          + h->plt.offset);
02329              unresolved_reloc = FALSE;
02330            }
02331 
02332          relocation -= htab->sgotplt->output_section->vma
02333                      + htab->sgotplt->output_offset;
02334          break;
02335 
02336        case R_X86_64_PLT32:
02337          /* Relocation is to the entry for this symbol in the
02338             procedure linkage table.  */
02339 
02340          /* Resolve a PLT32 reloc against a local symbol directly,
02341             without using the procedure linkage table.   */
02342          if (h == NULL)
02343            break;
02344 
02345          if (h->plt.offset == (bfd_vma) -1
02346              || htab->splt == NULL)
02347            {
02348              /* We didn't make a PLT entry for this symbol.  This
02349                happens when statically linking PIC code, or when
02350                using -Bsymbolic.  */
02351              break;
02352            }
02353 
02354          relocation = (htab->splt->output_section->vma
02355                      + htab->splt->output_offset
02356                      + h->plt.offset);
02357          unresolved_reloc = FALSE;
02358          break;
02359 
02360        case R_X86_64_PC8:
02361        case R_X86_64_PC16:
02362        case R_X86_64_PC32:
02363          if (info->shared
02364              && !SYMBOL_REFERENCES_LOCAL (info, h)
02365              && (input_section->flags & SEC_ALLOC) != 0
02366              && (input_section->flags & SEC_READONLY) != 0
02367              && (!h->def_regular
02368                 || r_type != R_X86_64_PC32
02369                 || h->type != STT_FUNC
02370                 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
02371                 || !is_32bit_relative_branch (contents,
02372                                           rel->r_offset)))
02373            {
02374              if (h->def_regular
02375                 && r_type == R_X86_64_PC32
02376                 && h->type == STT_FUNC
02377                 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
02378               (*_bfd_error_handler)
02379                  (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
02380                   input_bfd, h->root.root.string);
02381              else
02382               (*_bfd_error_handler)
02383                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
02384                  input_bfd, x86_64_elf_howto_table[r_type].name,
02385                  h->root.root.string);
02386              bfd_set_error (bfd_error_bad_value);
02387              return FALSE;
02388            }
02389          /* Fall through.  */
02390 
02391        case R_X86_64_8:
02392        case R_X86_64_16:
02393        case R_X86_64_32:
02394        case R_X86_64_PC64:
02395        case R_X86_64_64:
02396          /* FIXME: The ABI says the linker should make sure the value is
02397             the same when it's zeroextended to 64 bit.   */
02398 
02399          if ((input_section->flags & SEC_ALLOC) == 0)
02400            break;
02401 
02402          if ((info->shared
02403               && (h == NULL
02404                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02405                  || h->root.type != bfd_link_hash_undefweak)
02406               && ((r_type != R_X86_64_PC8
02407                   && r_type != R_X86_64_PC16
02408                   && r_type != R_X86_64_PC32
02409                   && r_type != R_X86_64_PC64)
02410                  || !SYMBOL_CALLS_LOCAL (info, h)))
02411              || (ELIMINATE_COPY_RELOCS
02412                 && !info->shared
02413                 && h != NULL
02414                 && h->dynindx != -1
02415                 && !h->non_got_ref
02416                 && ((h->def_dynamic
02417                      && !h->def_regular)
02418                     || h->root.type == bfd_link_hash_undefweak
02419                     || h->root.type == bfd_link_hash_undefined)))
02420            {
02421              Elf_Internal_Rela outrel;
02422              bfd_byte *loc;
02423              bfd_boolean skip, relocate;
02424              asection *sreloc;
02425 
02426              /* When generating a shared object, these relocations
02427                are copied into the output file to be resolved at run
02428                time. */
02429              skip = FALSE;
02430              relocate = FALSE;
02431 
02432              outrel.r_offset =
02433               _bfd_elf_section_offset (output_bfd, info, input_section,
02434                                     rel->r_offset);
02435              if (outrel.r_offset == (bfd_vma) -1)
02436               skip = TRUE;
02437              else if (outrel.r_offset == (bfd_vma) -2)
02438               skip = TRUE, relocate = TRUE;
02439 
02440              outrel.r_offset += (input_section->output_section->vma
02441                               + input_section->output_offset);
02442 
02443              if (skip)
02444               memset (&outrel, 0, sizeof outrel);
02445 
02446              /* h->dynindx may be -1 if this symbol was marked to
02447                become local.  */
02448              else if (h != NULL
02449                      && h->dynindx != -1
02450                      && (r_type == R_X86_64_PC8
02451                         || r_type == R_X86_64_PC16
02452                         || r_type == R_X86_64_PC32
02453                         || r_type == R_X86_64_PC64
02454                         || !info->shared
02455                         || !SYMBOLIC_BIND (info, h)
02456                         || !h->def_regular))
02457               {
02458                 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
02459                 outrel.r_addend = rel->r_addend;
02460               }
02461              else
02462               {
02463                 /* This symbol is local, or marked to become local.  */
02464                 if (r_type == R_X86_64_64)
02465                   {
02466                     relocate = TRUE;
02467                     outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
02468                     outrel.r_addend = relocation + rel->r_addend;
02469                   }
02470                 else
02471                   {
02472                     long sindx;
02473 
02474                     if (bfd_is_abs_section (sec))
02475                      sindx = 0;
02476                     else if (sec == NULL || sec->owner == NULL)
02477                      {
02478                        bfd_set_error (bfd_error_bad_value);
02479                        return FALSE;
02480                      }
02481                     else
02482                      {
02483                        asection *osec;
02484 
02485                        /* We are turning this relocation into one
02486                           against a section symbol.  It would be
02487                           proper to subtract the symbol's value,
02488                           osec->vma, from the emitted reloc addend,
02489                           but ld.so expects buggy relocs.  */
02490                        osec = sec->output_section;
02491                        sindx = elf_section_data (osec)->dynindx;
02492                        if (sindx == 0)
02493                          {
02494                            asection *oi = htab->elf.text_index_section;
02495                            sindx = elf_section_data (oi)->dynindx;
02496                          }
02497                        BFD_ASSERT (sindx != 0);
02498                      }
02499 
02500                     outrel.r_info = ELF64_R_INFO (sindx, r_type);
02501                     outrel.r_addend = relocation + rel->r_addend;
02502                   }
02503               }
02504 
02505              sreloc = elf_section_data (input_section)->sreloc;
02506              if (sreloc == NULL)
02507               abort ();
02508 
02509              loc = sreloc->contents;
02510              loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
02511              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02512 
02513              /* If this reloc is against an external symbol, we do
02514                not want to fiddle with the addend.  Otherwise, we
02515                need to include the symbol value so that it becomes
02516                an addend for the dynamic reloc.  */
02517              if (! relocate)
02518               continue;
02519            }
02520 
02521          break;
02522 
02523        case R_X86_64_TLSGD:
02524        case R_X86_64_GOTPC32_TLSDESC:
02525        case R_X86_64_TLSDESC_CALL:
02526        case R_X86_64_GOTTPOFF:
02527          r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
02528          tls_type = GOT_UNKNOWN;
02529          if (h == NULL && local_got_offsets)
02530            tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
02531          else if (h != NULL)
02532            {
02533              tls_type = elf64_x86_64_hash_entry (h)->tls_type;
02534              if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
02535               r_type = R_X86_64_TPOFF32;
02536            }
02537          if (r_type == R_X86_64_TLSGD
02538              || r_type == R_X86_64_GOTPC32_TLSDESC
02539              || r_type == R_X86_64_TLSDESC_CALL)
02540            {
02541              if (tls_type == GOT_TLS_IE)
02542               r_type = R_X86_64_GOTTPOFF;
02543            }
02544 
02545          if (r_type == R_X86_64_TPOFF32)
02546            {
02547              BFD_ASSERT (! unresolved_reloc);
02548              if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
02549               {
02550                 unsigned int i;
02551                 static unsigned char tlsgd[8]
02552                   = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
02553 
02554                 /* GD->LE transition.
02555                    .byte 0x66; leaq foo@tlsgd(%rip), %rdi
02556                    .word 0x6666; rex64; call __tls_get_addr@plt
02557                    Change it into:
02558                    movq %fs:0, %rax
02559                    leaq foo@tpoff(%rax), %rax */
02560                 BFD_ASSERT (rel->r_offset >= 4);
02561                 for (i = 0; i < 4; i++)
02562                   BFD_ASSERT (bfd_get_8 (input_bfd,
02563                                       contents + rel->r_offset - 4 + i)
02564                             == tlsgd[i]);
02565                 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
02566                 for (i = 0; i < 4; i++)
02567                   BFD_ASSERT (bfd_get_8 (input_bfd,
02568                                       contents + rel->r_offset + 4 + i)
02569                             == tlsgd[i+4]);
02570                 BFD_ASSERT (rel + 1 < relend);
02571                 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02572                 memcpy (contents + rel->r_offset - 4,
02573                        "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
02574                        16);
02575                 bfd_put_32 (output_bfd, tpoff (info, relocation),
02576                            contents + rel->r_offset + 8);
02577                 /* Skip R_X86_64_PLT32.  */
02578                 rel++;
02579                 continue;
02580               }
02581              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
02582               {
02583                 /* GDesc -> LE transition.
02584                    It's originally something like:
02585                    leaq x@tlsdesc(%rip), %rax
02586 
02587                    Change it to:
02588                    movl $x@tpoff, %rax
02589 
02590                    Registers other than %rax may be set up here.  */
02591 
02592                 unsigned int val, type, type2;
02593                 bfd_vma roff;
02594 
02595                 /* First, make sure it's a leaq adding rip to a
02596                    32-bit offset into any register, although it's
02597                    probably almost always going to be rax.  */
02598                 roff = rel->r_offset;
02599                 BFD_ASSERT (roff >= 3);
02600                 type = bfd_get_8 (input_bfd, contents + roff - 3);
02601                 BFD_ASSERT ((type & 0xfb) == 0x48);
02602                 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
02603                 BFD_ASSERT (type2 == 0x8d);
02604                 val = bfd_get_8 (input_bfd, contents + roff - 1);
02605                 BFD_ASSERT ((val & 0xc7) == 0x05);
02606                 BFD_ASSERT (roff + 4 <= input_section->size);
02607 
02608                 /* Now modify the instruction as appropriate.  */
02609                 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
02610                           contents + roff - 3);
02611                 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
02612                 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
02613                           contents + roff - 1);
02614                 bfd_put_32 (output_bfd, tpoff (info, relocation),
02615                            contents + roff);
02616                 continue;
02617               }
02618              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
02619               {
02620                 /* GDesc -> LE transition.
02621                    It's originally:
02622                    call *(%rax)
02623                    Turn it into:
02624                    nop; nop.  */
02625 
02626                 unsigned int val, type;
02627                 bfd_vma roff;
02628 
02629                 /* First, make sure it's a call *(%rax).  */
02630                 roff = rel->r_offset;
02631                 BFD_ASSERT (roff + 2 <= input_section->size);
02632                 type = bfd_get_8 (input_bfd, contents + roff);
02633                 BFD_ASSERT (type == 0xff);
02634                 val = bfd_get_8 (input_bfd, contents + roff + 1);
02635                 BFD_ASSERT (val == 0x10);
02636 
02637                 /* Now modify the instruction as appropriate.  Use
02638                    xchg %ax,%ax instead of 2 nops.  */
02639                 bfd_put_8 (output_bfd, 0x66, contents + roff);
02640                 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
02641                 continue;
02642               }
02643              else
02644               {
02645                 unsigned int val, type, reg;
02646 
02647                 /* IE->LE transition:
02648                    Originally it can be one of:
02649                    movq foo@gottpoff(%rip), %reg
02650                    addq foo@gottpoff(%rip), %reg
02651                    We change it into:
02652                    movq $foo, %reg
02653                    leaq foo(%reg), %reg
02654                    addq $foo, %reg.  */
02655                 BFD_ASSERT (rel->r_offset >= 3);
02656                 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
02657                 BFD_ASSERT (val == 0x48 || val == 0x4c);
02658                 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02659                 BFD_ASSERT (type == 0x8b || type == 0x03);
02660                 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02661                 BFD_ASSERT ((reg & 0xc7) == 5);
02662                 reg >>= 3;
02663                 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
02664                 if (type == 0x8b)
02665                   {
02666                     /* movq */
02667                     if (val == 0x4c)
02668                      bfd_put_8 (output_bfd, 0x49,
02669                                contents + rel->r_offset - 3);
02670                     bfd_put_8 (output_bfd, 0xc7,
02671                              contents + rel->r_offset - 2);
02672                     bfd_put_8 (output_bfd, 0xc0 | reg,
02673                              contents + rel->r_offset - 1);
02674                   }
02675                 else if (reg == 4)
02676                   {
02677                     /* addq -> addq - addressing with %rsp/%r12 is
02678                       special  */
02679                     if (val == 0x4c)
02680                      bfd_put_8 (output_bfd, 0x49,
02681                                contents + rel->r_offset - 3);
02682                     bfd_put_8 (output_bfd, 0x81,
02683                              contents + rel->r_offset - 2);
02684                     bfd_put_8 (output_bfd, 0xc0 | reg,
02685                              contents + rel->r_offset - 1);
02686                   }
02687                 else
02688                   {
02689                     /* addq -> leaq */
02690                     if (val == 0x4c)
02691                      bfd_put_8 (output_bfd, 0x4d,
02692                                contents + rel->r_offset - 3);
02693                     bfd_put_8 (output_bfd, 0x8d,
02694                              contents + rel->r_offset - 2);
02695                     bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
02696                              contents + rel->r_offset - 1);
02697                   }
02698                 bfd_put_32 (output_bfd, tpoff (info, relocation),
02699                            contents + rel->r_offset);
02700                 continue;
02701               }
02702            }
02703 
02704          if (htab->sgot == NULL)
02705            abort ();
02706 
02707          if (h != NULL)
02708            {
02709              off = h->got.offset;
02710              offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
02711            }
02712          else
02713            {
02714              if (local_got_offsets == NULL)
02715               abort ();
02716 
02717              off = local_got_offsets[r_symndx];
02718              offplt = local_tlsdesc_gotents[r_symndx];
02719            }
02720 
02721          if ((off & 1) != 0)
02722            off &= ~1;
02723          else
02724            {
02725              Elf_Internal_Rela outrel;
02726              bfd_byte *loc;
02727              int dr_type, indx;
02728              asection *sreloc;
02729 
02730              if (htab->srelgot == NULL)
02731               abort ();
02732 
02733              indx = h && h->dynindx != -1 ? h->dynindx : 0;
02734 
02735              if (GOT_TLS_GDESC_P (tls_type))
02736               {
02737                 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
02738                 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
02739                            + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
02740                 outrel.r_offset = (htab->sgotplt->output_section->vma
02741                                  + htab->sgotplt->output_offset
02742                                  + offplt
02743                                  + htab->sgotplt_jump_table_size);
02744                 sreloc = htab->srelplt;
02745                 loc = sreloc->contents;
02746                 loc += sreloc->reloc_count++
02747                   * sizeof (Elf64_External_Rela);
02748                 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
02749                            <= sreloc->contents + sreloc->size);
02750                 if (indx == 0)
02751                   outrel.r_addend = relocation - dtpoff_base (info);
02752                 else
02753                   outrel.r_addend = 0;
02754                 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02755               }
02756 
02757              sreloc = htab->srelgot;
02758 
02759              outrel.r_offset = (htab->sgot->output_section->vma
02760                              + htab->sgot->output_offset + off);
02761 
02762              if (GOT_TLS_GD_P (tls_type))
02763               dr_type = R_X86_64_DTPMOD64;
02764              else if (GOT_TLS_GDESC_P (tls_type))
02765               goto dr_done;
02766              else
02767               dr_type = R_X86_64_TPOFF64;
02768 
02769              bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
02770              outrel.r_addend = 0;
02771              if ((dr_type == R_X86_64_TPOFF64
02772                  || dr_type == R_X86_64_TLSDESC) && indx == 0)
02773               outrel.r_addend = relocation - dtpoff_base (info);
02774              outrel.r_info = ELF64_R_INFO (indx, dr_type);
02775 
02776              loc = sreloc->contents;
02777              loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
02778              BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
02779                        <= sreloc->contents + sreloc->size);
02780              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02781 
02782              if (GOT_TLS_GD_P (tls_type))
02783               {
02784                 if (indx == 0)
02785                   {
02786                     BFD_ASSERT (! unresolved_reloc);
02787                     bfd_put_64 (output_bfd,
02788                               relocation - dtpoff_base (info),
02789                               htab->sgot->contents + off + GOT_ENTRY_SIZE);
02790                   }
02791                 else
02792                   {
02793                     bfd_put_64 (output_bfd, 0,
02794                               htab->sgot->contents + off + GOT_ENTRY_SIZE);
02795                     outrel.r_info = ELF64_R_INFO (indx,
02796                                               R_X86_64_DTPOFF64);
02797                     outrel.r_offset += GOT_ENTRY_SIZE;
02798                     sreloc->reloc_count++;
02799                     loc += sizeof (Elf64_External_Rela);
02800                     BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
02801                               <= sreloc->contents + sreloc->size);
02802                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02803                   }
02804               }
02805 
02806            dr_done:
02807              if (h != NULL)
02808               h->got.offset |= 1;
02809              else
02810               local_got_offsets[r_symndx] |= 1;
02811            }
02812 
02813          if (off >= (bfd_vma) -2
02814              && ! GOT_TLS_GDESC_P (tls_type))
02815            abort ();
02816          if (r_type == ELF64_R_TYPE (rel->r_info))
02817            {
02818              if (r_type == R_X86_64_GOTPC32_TLSDESC
02819                 || r_type == R_X86_64_TLSDESC_CALL)
02820               relocation = htab->sgotplt->output_section->vma
02821                 + htab->sgotplt->output_offset
02822                 + offplt + htab->sgotplt_jump_table_size;
02823              else
02824               relocation = htab->sgot->output_section->vma
02825                 + htab->sgot->output_offset + off;
02826              unresolved_reloc = FALSE;
02827            }
02828          else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
02829            {
02830              unsigned int i;
02831              static unsigned char tlsgd[8]
02832               = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
02833 
02834              /* GD->IE transition.
02835                .byte 0x66; leaq foo@tlsgd(%rip), %rdi
02836                .word 0x6666; rex64; call __tls_get_addr@plt
02837                Change it into:
02838                movq %fs:0, %rax
02839                addq foo@gottpoff(%rip), %rax */
02840              BFD_ASSERT (rel->r_offset >= 4);
02841              for (i = 0; i < 4; i++)
02842               BFD_ASSERT (bfd_get_8 (input_bfd,
02843                                    contents + rel->r_offset - 4 + i)
02844                          == tlsgd[i]);
02845              BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
02846              for (i = 0; i < 4; i++)
02847               BFD_ASSERT (bfd_get_8 (input_bfd,
02848                                    contents + rel->r_offset + 4 + i)
02849                          == tlsgd[i+4]);
02850              BFD_ASSERT (rel + 1 < relend);
02851              BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02852              memcpy (contents + rel->r_offset - 4,
02853                     "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
02854                     16);
02855 
02856              relocation = (htab->sgot->output_section->vma
02857                          + htab->sgot->output_offset + off
02858                          - rel->r_offset
02859                          - input_section->output_section->vma
02860                          - input_section->output_offset
02861                          - 12);
02862              bfd_put_32 (output_bfd, relocation,
02863                        contents + rel->r_offset + 8);
02864              /* Skip R_X86_64_PLT32.  */
02865              rel++;
02866              continue;
02867            }
02868          else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
02869            {
02870              /* GDesc -> IE transition.
02871                It's originally something like:
02872                leaq x@tlsdesc(%rip), %rax
02873 
02874                Change it to:
02875                movq x@gottpoff(%rip), %rax # before nop; nop
02876 
02877                Registers other than %rax may be set up here.  */
02878 
02879              unsigned int val, type, type2;
02880              bfd_vma roff;
02881 
02882              /* First, make sure it's a leaq adding rip to a 32-bit
02883                offset into any register, although it's probably
02884                almost always going to be rax.  */
02885              roff = rel->r_offset;
02886              BFD_ASSERT (roff >= 3);
02887              type = bfd_get_8 (input_bfd, contents + roff - 3);
02888              BFD_ASSERT ((type & 0xfb) == 0x48);
02889              type2 = bfd_get_8 (input_bfd, contents + roff - 2);
02890              BFD_ASSERT (type2 == 0x8d);
02891              val = bfd_get_8 (input_bfd, contents + roff - 1);
02892              BFD_ASSERT ((val & 0xc7) == 0x05);
02893              BFD_ASSERT (roff + 4 <= input_section->size);
02894 
02895              /* Now modify the instruction as appropriate.  */
02896              /* To turn a leaq into a movq in the form we use it, it
02897                suffices to change the second byte from 0x8d to
02898                0x8b.  */
02899              bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
02900 
02901              bfd_put_32 (output_bfd,
02902                        htab->sgot->output_section->vma
02903                        + htab->sgot->output_offset + off
02904                        - rel->r_offset
02905                        - input_section->output_section->vma
02906                        - input_section->output_offset
02907                        - 4,
02908                        contents + roff);
02909              continue;
02910            }
02911          else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
02912            {
02913              /* GDesc -> IE transition.
02914                It's originally:
02915                call *(%rax)
02916 
02917                Change it to:
02918                nop; nop.  */
02919 
02920              unsigned int val, type;
02921              bfd_vma roff;
02922 
02923              /* First, make sure it's a call *(%eax).  */
02924              roff = rel->r_offset;
02925              BFD_ASSERT (roff + 2 <= input_section->size);
02926              type = bfd_get_8 (input_bfd, contents + roff);
02927              BFD_ASSERT (type == 0xff);
02928              val = bfd_get_8 (input_bfd, contents + roff + 1);
02929              BFD_ASSERT (val == 0x10);
02930 
02931              /* Now modify the instruction as appropriate.  Use
02932                xchg %ax,%ax instead of 2 nops.  */
02933              bfd_put_8 (output_bfd, 0x66, contents + roff);
02934              bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
02935 
02936              continue;
02937            }
02938          else
02939            BFD_ASSERT (FALSE);
02940          break;
02941 
02942        case R_X86_64_TLSLD:
02943          if (! info->shared)
02944            {
02945              /* LD->LE transition:
02946                Ensure it is:
02947                leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
02948                We change it into:
02949                .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
02950              BFD_ASSERT (rel->r_offset >= 3);
02951              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
02952                        == 0x48);
02953              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
02954                        == 0x8d);
02955              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
02956                        == 0x3d);
02957              BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
02958              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
02959                        == 0xe8);
02960              BFD_ASSERT (rel + 1 < relend);
02961              BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02962              memcpy (contents + rel->r_offset - 3,
02963                     "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
02964              /* Skip R_X86_64_PLT32.  */
02965              rel++;
02966              continue;
02967            }
02968 
02969          if (htab->sgot == NULL)
02970            abort ();
02971 
02972          off = htab->tls_ld_got.offset;
02973          if (off & 1)
02974            off &= ~1;
02975          else
02976            {
02977              Elf_Internal_Rela outrel;
02978              bfd_byte *loc;
02979 
02980              if (htab->srelgot == NULL)
02981               abort ();
02982 
02983              outrel.r_offset = (htab->sgot->output_section->vma
02984                              + htab->sgot->output_offset + off);
02985 
02986              bfd_put_64 (output_bfd, 0,
02987                        htab->sgot->contents + off);
02988              bfd_put_64 (output_bfd, 0,
02989                        htab->sgot->contents + off + GOT_ENTRY_SIZE);
02990              outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
02991              outrel.r_addend = 0;
02992              loc = htab->srelgot->contents;
02993              loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
02994              bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02995              htab->tls_ld_got.offset |= 1;
02996            }
02997          relocation = htab->sgot->output_section->vma
02998                      + htab->sgot->output_offset + off;
02999          unresolved_reloc = FALSE;
03000          break;
03001 
03002        case R_X86_64_DTPOFF32:
03003          if (info->shared || (input_section->flags & SEC_CODE) == 0)
03004            relocation -= dtpoff_base (info);
03005          else
03006            relocation = tpoff (info, relocation);
03007          break;
03008 
03009        case R_X86_64_TPOFF32:
03010          BFD_ASSERT (! info->shared);
03011          relocation = tpoff (info, relocation);
03012          break;
03013 
03014        default:
03015          break;
03016        }
03017 
03018       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
03019         because such sections are not SEC_ALLOC and thus ld.so will
03020         not process them.  */
03021       if (unresolved_reloc
03022          && !((input_section->flags & SEC_DEBUGGING) != 0
03023               && h->def_dynamic))
03024        (*_bfd_error_handler)
03025          (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
03026           input_bfd,
03027           input_section,
03028           (long) rel->r_offset,
03029           howto->name,
03030           h->root.root.string);
03031 
03032       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
03033                                 contents, rel->r_offset,
03034                                 relocation, rel->r_addend);
03035 
03036       if (r != bfd_reloc_ok)
03037        {
03038          const char *name;
03039 
03040          if (h != NULL)
03041            name = h->root.root.string;
03042          else
03043            {
03044              name = bfd_elf_string_from_elf_section (input_bfd,
03045                                                 symtab_hdr->sh_link,
03046                                                 sym->st_name);
03047              if (name == NULL)
03048               return FALSE;
03049              if (*name == '\0')
03050               name = bfd_section_name (input_bfd, sec);
03051            }
03052 
03053          if (r == bfd_reloc_overflow)
03054            {
03055              if (! ((*info->callbacks->reloc_overflow)
03056                    (info, (h ? &h->root : NULL), name, howto->name,
03057                     (bfd_vma) 0, input_bfd, input_section,
03058                     rel->r_offset)))
03059               return FALSE;
03060            }
03061          else
03062            {
03063              (*_bfd_error_handler)
03064               (_("%B(%A+0x%lx): reloc against `%s': error %d"),
03065                input_bfd, input_section,
03066                (long) rel->r_offset, name, (int) r);
03067              return FALSE;
03068            }
03069        }
03070     }
03071 
03072   return TRUE;
03073 }
03074 
03075 /* Finish up dynamic symbol handling.  We set the contents of various
03076    dynamic sections here.  */
03077 
03078 static bfd_boolean
03079 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
03080                                 struct bfd_link_info *info,
03081                                 struct elf_link_hash_entry *h,
03082                                 Elf_Internal_Sym *sym)
03083 {
03084   struct elf64_x86_64_link_hash_table *htab;
03085 
03086   htab = elf64_x86_64_hash_table (info);
03087 
03088   if (h->plt.offset != (bfd_vma) -1)
03089     {
03090       bfd_vma plt_index;
03091       bfd_vma got_offset;
03092       Elf_Internal_Rela rela;
03093       bfd_byte *loc;
03094 
03095       /* This symbol has an entry in the procedure linkage table.  Set
03096         it up.        */
03097       if (h->dynindx == -1
03098          || htab->splt == NULL
03099          || htab->sgotplt == NULL
03100          || htab->srelplt == NULL)
03101        abort ();
03102 
03103       /* Get the index in the procedure linkage table which
03104         corresponds to this symbol.  This is the index of this symbol
03105         in all the symbols for which we are making plt entries.  The
03106         first entry in the procedure linkage table is reserved.  */
03107       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
03108 
03109       /* Get the offset into the .got table of the entry that
03110         corresponds to this function.     Each .got entry is GOT_ENTRY_SIZE
03111         bytes. The first three are reserved for the dynamic linker.  */
03112       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
03113 
03114       /* Fill in the entry in the procedure linkage table.  */
03115       memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
03116              PLT_ENTRY_SIZE);
03117 
03118       /* Insert the relocation positions of the plt section.  The magic
03119         numbers at the end of the statements are the positions of the
03120         relocations in the plt section.  */
03121       /* Put offset for jmp *name@GOTPCREL(%rip), since the
03122         instruction uses 6 bytes, subtract this value.  */
03123       bfd_put_32 (output_bfd,
03124                     (htab->sgotplt->output_section->vma
03125                      + htab->sgotplt->output_offset
03126                      + got_offset
03127                      - htab->splt->output_section->vma
03128                      - htab->splt->output_offset
03129                      - h->plt.offset
03130                      - 6),
03131                 htab->splt->contents + h->plt.offset + 2);
03132       /* Put relocation index.  */
03133       bfd_put_32 (output_bfd, plt_index,
03134                 htab->splt->contents + h->plt.offset + 7);
03135       /* Put offset for jmp .PLT0.  */
03136       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
03137                 htab->splt->contents + h->plt.offset + 12);
03138 
03139       /* Fill in the entry in the global offset table, initially this
03140         points to the pushq instruction in the PLT which is at offset 6.  */
03141       bfd_put_64 (output_bfd, (htab->splt->output_section->vma
03142                             + htab->splt->output_offset
03143                             + h->plt.offset + 6),
03144                 htab->sgotplt->contents + got_offset);
03145 
03146       /* Fill in the entry in the .rela.plt section.  */
03147       rela.r_offset = (htab->sgotplt->output_section->vma
03148                      + htab->sgotplt->output_offset
03149                      + got_offset);
03150       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
03151       rela.r_addend = 0;
03152       loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
03153       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
03154 
03155       if (!h->def_regular)
03156        {
03157          /* Mark the symbol as undefined, rather than as defined in
03158             the .plt section.  Leave the value if there were any
03159             relocations where pointer equality matters (this is a clue
03160             for the dynamic linker, to make function pointer
03161             comparisons work between an application and shared
03162             library), otherwise set it to zero.  If a function is only
03163             called from a binary, there is no need to slow down
03164             shared libraries because of that.  */
03165          sym->st_shndx = SHN_UNDEF;
03166          if (!h->pointer_equality_needed)
03167            sym->st_value = 0;
03168        }
03169     }
03170 
03171   if (h->got.offset != (bfd_vma) -1
03172       && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
03173       && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
03174     {
03175       Elf_Internal_Rela rela;
03176       bfd_byte *loc;
03177 
03178       /* This symbol has an entry in the global offset table.  Set it
03179         up.  */
03180       if (htab->sgot == NULL || htab->srelgot == NULL)
03181        abort ();
03182 
03183       rela.r_offset = (htab->sgot->output_section->vma
03184                      + htab->sgot->output_offset
03185                      + (h->got.offset &~ (bfd_vma) 1));
03186 
03187       /* If this is a static link, or it is a -Bsymbolic link and the
03188         symbol is defined locally or was forced to be local because
03189         of a version file, we just want to emit a RELATIVE reloc.
03190         The entry in the global offset table will already have been
03191         initialized in the relocate_section function.  */
03192       if (info->shared
03193          && SYMBOL_REFERENCES_LOCAL (info, h))
03194        {
03195          BFD_ASSERT((h->got.offset & 1) != 0);
03196          rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
03197          rela.r_addend = (h->root.u.def.value
03198                         + h->root.u.def.section->output_section->vma
03199                         + h->root.u.def.section->output_offset);
03200        }
03201       else
03202        {
03203          BFD_ASSERT((h->got.offset & 1) == 0);
03204          bfd_put_64 (output_bfd, (bfd_vma) 0,
03205                     htab->sgot->contents + h->got.offset);
03206          rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
03207          rela.r_addend = 0;
03208        }
03209 
03210       loc = htab->srelgot->contents;
03211       loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
03212       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
03213     }
03214 
03215   if (h->needs_copy)
03216     {
03217       Elf_Internal_Rela rela;
03218       bfd_byte *loc;
03219 
03220       /* This symbol needs a copy reloc.  Set it up.  */
03221 
03222       if (h->dynindx == -1
03223          || (h->root.type != bfd_link_hash_defined
03224              && h->root.type != bfd_link_hash_defweak)
03225          || htab->srelbss == NULL)
03226        abort ();
03227 
03228       rela.r_offset = (h->root.u.def.value
03229                      + h->root.u.def.section->output_section->vma
03230                      + h->root.u.def.section->output_offset);
03231       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
03232       rela.r_addend = 0;
03233       loc = htab->srelbss->contents;
03234       loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
03235       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
03236     }
03237 
03238   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
03239   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
03240       || h == htab->elf.hgot)
03241     sym->st_shndx = SHN_ABS;
03242 
03243   return TRUE;
03244 }
03245 
03246 /* Used to decide how to sort relocs in an optimal manner for the
03247    dynamic linker, before writing them out.  */
03248 
03249 static enum elf_reloc_type_class
03250 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
03251 {
03252   switch ((int) ELF64_R_TYPE (rela->r_info))
03253     {
03254     case R_X86_64_RELATIVE:
03255       return reloc_class_relative;
03256     case R_X86_64_JUMP_SLOT:
03257       return reloc_class_plt;
03258     case R_X86_64_COPY:
03259       return reloc_class_copy;
03260     default:
03261       return reloc_class_normal;
03262     }
03263 }
03264 
03265 /* Finish up the dynamic sections.  */
03266 
03267 static bfd_boolean
03268 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
03269 {
03270   struct elf64_x86_64_link_hash_table *htab;
03271   bfd *dynobj;
03272   asection *sdyn;
03273 
03274   htab = elf64_x86_64_hash_table (info);
03275   dynobj = htab->elf.dynobj;
03276   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
03277 
03278   if (htab->elf.dynamic_sections_created)
03279     {
03280       Elf64_External_Dyn *dyncon, *dynconend;
03281 
03282       if (sdyn == NULL || htab->sgot == NULL)
03283        abort ();
03284 
03285       dyncon = (Elf64_External_Dyn *) sdyn->contents;
03286       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
03287       for (; dyncon < dynconend; dyncon++)
03288        {
03289          Elf_Internal_Dyn dyn;
03290          asection *s;
03291 
03292          bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
03293 
03294          switch (dyn.d_tag)
03295            {
03296            default:
03297              continue;
03298 
03299            case DT_PLTGOT:
03300              s = htab->sgotplt;
03301              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
03302              break;
03303 
03304            case DT_JMPREL:
03305              dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
03306              break;
03307 
03308            case DT_PLTRELSZ:
03309              s = htab->srelplt->output_section;
03310              dyn.d_un.d_val = s->size;
03311              break;
03312 
03313            case DT_RELASZ:
03314              /* The procedure linkage table relocs (DT_JMPREL) should
03315                not be included in the overall relocs (DT_RELA).
03316                Therefore, we override the DT_RELASZ entry here to
03317                make it not include the JMPREL relocs.  Since the
03318                linker script arranges for .rela.plt to follow all
03319                other relocation sections, we don't have to worry
03320                about changing the DT_RELA entry.  */
03321              if (htab->srelplt != NULL)
03322               {
03323                 s = htab->srelplt->output_section;
03324                 dyn.d_un.d_val -= s->size;
03325               }
03326              break;
03327 
03328            case DT_TLSDESC_PLT:
03329              s = htab->splt;
03330              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
03331               + htab->tlsdesc_plt;
03332              break;
03333 
03334            case DT_TLSDESC_GOT:
03335              s = htab->sgot;
03336              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
03337               + htab->tlsdesc_got;
03338              break;
03339            }
03340 
03341          bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
03342        }
03343 
03344       /* Fill in the special first entry in the procedure linkage table.  */
03345       if (htab->splt && htab->splt->size > 0)
03346        {
03347          /* Fill in the first entry in the procedure linkage table.  */
03348          memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
03349                 PLT_ENTRY_SIZE);
03350          /* Add offset for pushq GOT+8(%rip), since the instruction
03351             uses 6 bytes subtract this value.  */
03352          bfd_put_32 (output_bfd,
03353                     (htab->sgotplt->output_section->vma
03354                      + htab->sgotplt->output_offset
03355                      + 8
03356                      - htab->splt->output_section->vma
03357                      - htab->splt->output_offset
03358                      - 6),
03359                     htab->splt->contents + 2);
03360          /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
03361             the end of the instruction.  */
03362          bfd_put_32 (output_bfd,
03363                     (htab->sgotplt->output_section->vma
03364                      + htab->sgotplt->output_offset
03365                      + 16
03366                      - htab->splt->output_section->vma
03367                      - htab->splt->output_offset
03368                      - 12),
03369                     htab->splt->contents + 8);
03370 
03371          elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
03372            PLT_ENTRY_SIZE;
03373 
03374          if (htab->tlsdesc_plt)
03375            {
03376              bfd_put_64 (output_bfd, (bfd_vma) 0,
03377                        htab->sgot->contents + htab->tlsdesc_got);
03378 
03379              memcpy (htab->splt->contents + htab->tlsdesc_plt,
03380                     elf64_x86_64_plt0_entry,
03381                     PLT_ENTRY_SIZE);
03382 
03383              /* Add offset for pushq GOT+8(%rip), since the
03384                instruction uses 6 bytes subtract this value.  */
03385              bfd_put_32 (output_bfd,
03386                        (htab->sgotplt->output_section->vma
03387                         + htab->sgotplt->output_offset
03388                         + 8
03389                         - htab->splt->output_section->vma
03390                         - htab->splt->output_offset
03391                         - htab->tlsdesc_plt
03392                         - 6),
03393                        htab->splt->contents + htab->tlsdesc_plt + 2);
03394              /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
03395                htab->tlsdesc_got. The 12 is the offset to the end of
03396                the instruction.  */
03397              bfd_put_32 (output_bfd,
03398                        (htab->sgot->output_section->vma
03399                         + htab->sgot->output_offset
03400                         + htab->tlsdesc_got
03401                         - htab->splt->output_section->vma
03402                         - htab->splt->output_offset
03403                         - htab->tlsdesc_plt
03404                         - 12),
03405                        htab->splt->contents + htab->tlsdesc_plt + 8);
03406            }
03407        }
03408     }
03409 
03410   if (htab->sgotplt)
03411     {
03412       /* Fill in the first three entries in the global offset table.  */
03413       if (htab->sgotplt->size > 0)
03414        {
03415          /* Set the first entry in the global offset table to the address of
03416             the dynamic section.  */
03417          if (sdyn == NULL)
03418            bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
03419          else
03420            bfd_put_64 (output_bfd,
03421                      sdyn->output_section->vma + sdyn->output_offset,
03422                      htab->sgotplt->contents);
03423          /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
03424          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
03425          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
03426        }
03427 
03428       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
03429        GOT_ENTRY_SIZE;
03430     }
03431 
03432   if (htab->sgot && htab->sgot->size > 0)
03433     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
03434       = GOT_ENTRY_SIZE;
03435 
03436   return TRUE;
03437 }
03438 
03439 /* Return address for Ith PLT stub in section PLT, for relocation REL
03440    or (bfd_vma) -1 if it should not be included.  */
03441 
03442 static bfd_vma
03443 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
03444                        const arelent *rel ATTRIBUTE_UNUSED)
03445 {
03446   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
03447 }
03448 
03449 /* Handle an x86-64 specific section when reading an object file.  This
03450    is called when elfcode.h finds a section with an unknown type.  */
03451 
03452 static bfd_boolean
03453 elf64_x86_64_section_from_shdr (bfd *abfd,
03454                             Elf_Internal_Shdr *hdr,
03455                             const char *name,
03456                             int shindex)
03457 {
03458   if (hdr->sh_type != SHT_X86_64_UNWIND)
03459     return FALSE;
03460 
03461   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
03462     return FALSE;
03463 
03464   return TRUE;
03465 }
03466 
03467 /* Hook called by the linker routine which adds symbols from an object
03468    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
03469    of .bss.  */
03470 
03471 static bfd_boolean
03472 elf64_x86_64_add_symbol_hook (bfd *abfd,
03473                            struct bfd_link_info *info ATTRIBUTE_UNUSED,
03474                            Elf_Internal_Sym *sym,
03475                            const char **namep ATTRIBUTE_UNUSED,
03476                            flagword *flagsp ATTRIBUTE_UNUSED,
03477                            asection **secp, bfd_vma *valp)
03478 {
03479   asection *lcomm;
03480 
03481   switch (sym->st_shndx)
03482     {
03483     case SHN_X86_64_LCOMMON:
03484       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
03485       if (lcomm == NULL)
03486        {
03487          lcomm = bfd_make_section_with_flags (abfd,
03488                                           "LARGE_COMMON",
03489                                           (SEC_ALLOC
03490                                           | SEC_IS_COMMON
03491                                           | SEC_LINKER_CREATED));
03492          if (lcomm == NULL)
03493            return FALSE;
03494          elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
03495        }
03496       *secp = lcomm;
03497       *valp = sym->st_size;
03498       break;
03499     }
03500   return TRUE;
03501 }
03502 
03503 
03504 /* Given a BFD section, try to locate the corresponding ELF section
03505    index.  */
03506 
03507 static bfd_boolean
03508 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
03509                                       asection *sec, int *index)
03510 {
03511   if (sec == &_bfd_elf_large_com_section)
03512     {
03513       *index = SHN_X86_64_LCOMMON;
03514       return TRUE;
03515     }
03516   return FALSE;
03517 }
03518 
03519 /* Process a symbol.  */
03520 
03521 static void
03522 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
03523                             asymbol *asym)
03524 {
03525   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
03526 
03527   switch (elfsym->internal_elf_sym.st_shndx)
03528     {
03529     case SHN_X86_64_LCOMMON:
03530       asym->section = &_bfd_elf_large_com_section;
03531       asym->value = elfsym->internal_elf_sym.st_size;
03532       /* Common symbol doesn't set BSF_GLOBAL.  */
03533       asym->flags &= ~BSF_GLOBAL;
03534       break;
03535     }
03536 }
03537 
03538 static bfd_boolean
03539 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
03540 {
03541   return (sym->st_shndx == SHN_COMMON
03542          || sym->st_shndx == SHN_X86_64_LCOMMON);
03543 }
03544 
03545 static unsigned int
03546 elf64_x86_64_common_section_index (asection *sec)
03547 {
03548   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
03549     return SHN_COMMON;
03550   else
03551     return SHN_X86_64_LCOMMON;
03552 }
03553 
03554 static asection *
03555 elf64_x86_64_common_section (asection *sec)
03556 {
03557   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
03558     return bfd_com_section_ptr;
03559   else
03560     return &_bfd_elf_large_com_section;
03561 }
03562 
03563 static bfd_boolean
03564 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
03565                         struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
03566                         struct elf_link_hash_entry *h,
03567                         Elf_Internal_Sym *sym,
03568                         asection **psec,
03569                         bfd_vma *pvalue ATTRIBUTE_UNUSED,
03570                         unsigned int *pold_alignment ATTRIBUTE_UNUSED,
03571                         bfd_boolean *skip ATTRIBUTE_UNUSED,
03572                         bfd_boolean *override ATTRIBUTE_UNUSED,
03573                         bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
03574                         bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
03575                         bfd_boolean *newdef ATTRIBUTE_UNUSED,
03576                         bfd_boolean *newdyn,
03577                         bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
03578                         bfd_boolean *newweak ATTRIBUTE_UNUSED,
03579                         bfd *abfd ATTRIBUTE_UNUSED,
03580                         asection **sec,
03581                         bfd_boolean *olddef ATTRIBUTE_UNUSED,
03582                         bfd_boolean *olddyn,
03583                         bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
03584                         bfd_boolean *oldweak ATTRIBUTE_UNUSED,
03585                         bfd *oldbfd,
03586                         asection **oldsec)
03587 {
03588   /* A normal common symbol and a large common symbol result in a
03589      normal common symbol.  We turn the large common symbol into a
03590      normal one.  */
03591   if (!*olddyn
03592       && h->root.type == bfd_link_hash_common
03593       && !*newdyn
03594       && bfd_is_com_section (*sec)
03595       && *oldsec != *sec)
03596     {
03597       if (sym->st_shndx == SHN_COMMON
03598          && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
03599        {
03600          h->root.u.c.p->section
03601            = bfd_make_section_old_way (oldbfd, "COMMON");
03602          h->root.u.c.p->section->flags = SEC_ALLOC;
03603        }
03604       else if (sym->st_shndx == SHN_X86_64_LCOMMON
03605               && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
03606        *psec = *sec = bfd_com_section_ptr; 
03607     }
03608 
03609   return TRUE;
03610 }
03611 
03612 static int
03613 elf64_x86_64_additional_program_headers (bfd *abfd,
03614                                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
03615 {
03616   asection *s;
03617   int count = 0; 
03618 
03619   /* Check to see if we need a large readonly segment.  */
03620   s = bfd_get_section_by_name (abfd, ".lrodata");
03621   if (s && (s->flags & SEC_LOAD))
03622     count++;
03623 
03624   /* Check to see if we need a large data segment.  Since .lbss sections
03625      is placed right after the .bss section, there should be no need for
03626      a large data segment just because of .lbss.  */
03627   s = bfd_get_section_by_name (abfd, ".ldata");
03628   if (s && (s->flags & SEC_LOAD))
03629     count++;
03630 
03631   return count;
03632 }
03633 
03634 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
03635 
03636 static bfd_boolean
03637 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
03638 {
03639   if (h->plt.offset != (bfd_vma) -1
03640       && !h->def_regular
03641       && !h->pointer_equality_needed)
03642     return FALSE;
03643 
03644   return _bfd_elf_hash_symbol (h);
03645 }
03646 
03647 static const struct bfd_elf_special_section 
03648   elf64_x86_64_special_sections[]=
03649 {
03650   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
03651   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
03652   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
03653   { STRING_COMMA_LEN (".lbss"),               -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
03654   { STRING_COMMA_LEN (".ldata"),      -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
03655   { STRING_COMMA_LEN (".lrodata"),    -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
03656   { NULL,                     0,          0, 0,            0 }
03657 };
03658 
03659 #define TARGET_LITTLE_SYM              bfd_elf64_x86_64_vec
03660 #define TARGET_LITTLE_NAME             "elf64-x86-64"
03661 #define ELF_ARCH                       bfd_arch_i386
03662 #define ELF_MACHINE_CODE               EM_X86_64
03663 #define ELF_MAXPAGESIZE                       0x200000
03664 #define ELF_MINPAGESIZE                       0x1000
03665 #define ELF_COMMONPAGESIZE             0x1000
03666 
03667 #define elf_backend_can_gc_sections           1
03668 #define elf_backend_can_refcount       1
03669 #define elf_backend_want_got_plt       1
03670 #define elf_backend_plt_readonly       1
03671 #define elf_backend_want_plt_sym       0
03672 #define elf_backend_got_header_size           (GOT_ENTRY_SIZE*3)
03673 #define elf_backend_rela_normal               1
03674 
03675 #define elf_info_to_howto              elf64_x86_64_info_to_howto
03676 
03677 #define bfd_elf64_bfd_link_hash_table_create \
03678   elf64_x86_64_link_hash_table_create
03679 #define bfd_elf64_bfd_reloc_type_lookup       elf64_x86_64_reloc_type_lookup
03680 #define bfd_elf64_bfd_reloc_name_lookup \
03681   elf64_x86_64_reloc_name_lookup
03682 
03683 #define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
03684 #define elf_backend_check_relocs       elf64_x86_64_check_relocs
03685 #define elf_backend_copy_indirect_symbol    elf64_x86_64_copy_indirect_symbol
03686 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
03687 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
03688 #define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
03689 #define elf_backend_gc_mark_hook       elf64_x86_64_gc_mark_hook
03690 #define elf_backend_gc_sweep_hook      elf64_x86_64_gc_sweep_hook
03691 #define elf_backend_grok_prstatus      elf64_x86_64_grok_prstatus
03692 #define elf_backend_grok_psinfo               elf64_x86_64_grok_psinfo
03693 #define elf_backend_reloc_type_class          elf64_x86_64_reloc_type_class
03694 #define elf_backend_relocate_section          elf64_x86_64_relocate_section
03695 #define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
03696 #define elf_backend_always_size_sections    elf64_x86_64_always_size_sections
03697 #define elf_backend_init_index_section        _bfd_elf_init_1_index_section
03698 #define elf_backend_plt_sym_val               elf64_x86_64_plt_sym_val
03699 #define elf_backend_object_p                  elf64_x86_64_elf_object_p
03700 #define bfd_elf64_mkobject             elf64_x86_64_mkobject
03701 
03702 #define elf_backend_section_from_shdr \
03703        elf64_x86_64_section_from_shdr
03704 
03705 #define elf_backend_section_from_bfd_section \
03706   elf64_x86_64_elf_section_from_bfd_section
03707 #define elf_backend_add_symbol_hook \
03708   elf64_x86_64_add_symbol_hook
03709 #define elf_backend_symbol_processing \
03710   elf64_x86_64_symbol_processing
03711 #define elf_backend_common_section_index \
03712   elf64_x86_64_common_section_index
03713 #define elf_backend_common_section \
03714   elf64_x86_64_common_section
03715 #define elf_backend_common_definition \
03716   elf64_x86_64_common_definition
03717 #define elf_backend_merge_symbol \
03718   elf64_x86_64_merge_symbol
03719 #define elf_backend_special_sections \
03720   elf64_x86_64_special_sections
03721 #define elf_backend_additional_program_headers \
03722   elf64_x86_64_additional_program_headers
03723 #define elf_backend_hash_symbol \
03724   elf64_x86_64_hash_symbol
03725 
03726 #include "elf64-target.h"
03727 
03728 /* FreeBSD support.  */
03729 
03730 #undef  TARGET_LITTLE_SYM
03731 #define TARGET_LITTLE_SYM              bfd_elf64_x86_64_freebsd_vec
03732 #undef  TARGET_LITTLE_NAME
03733 #define TARGET_LITTLE_NAME             "elf64-x86-64-freebsd"
03734 
03735 #undef ELF_OSABI
03736 #define       ELF_OSABI                       ELFOSABI_FREEBSD
03737 
03738 #undef  elf_backend_post_process_headers
03739 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
03740 
03741 #undef  elf64_bed
03742 #define elf64_bed elf64_x86_64_fbsd_bed
03743 
03744 #include "elf64-target.h"