Home | History | Annotate | Line # | Download | only in bfd
elfxx-tilegx.c revision 1.1.1.10
      1 /* TILE-Gx-specific support for ELF.
      2    Copyright (C) 2011-2026 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "libbfd.h"
     24 #include "elf-bfd.h"
     25 #include "elf/tilegx.h"
     26 #include "opcode/tilegx.h"
     27 #include "libiberty.h"
     28 #include "elfxx-tilegx.h"
     29 
     30 #define TILEGX_ELF_WORD_BYTES(htab) \
     31   ((htab)->bytes_per_word)
     32 
     33 /* The size of an external RELA relocation.  */
     34 #define TILEGX_ELF_RELA_BYTES(htab) \
     35   ((htab)->bytes_per_rela)
     36 
     37 /* Both 32-bit and 64-bit tilegx encode this in an identical manner,
     38    so just take advantage of that.  */
     39 #define TILEGX_ELF_R_TYPE(r_info) \
     40   ((r_info) & 0xFF)
     41 
     42 #define TILEGX_ELF_R_INFO(htab, in_rel, index, type)	\
     43   ((htab)->r_info (in_rel, index, type))
     44 
     45 #define TILEGX_ELF_R_SYMNDX(htab, r_info) \
     46   ((htab)->r_symndx(r_info))
     47 
     48 #define TILEGX_ELF_DTPOFF_RELOC(htab) \
     49   ((htab)->dtpoff_reloc)
     50 
     51 #define TILEGX_ELF_DTPMOD_RELOC(htab) \
     52   ((htab)->dtpmod_reloc)
     53 
     54 #define TILEGX_ELF_TPOFF_RELOC(htab) \
     55   ((htab)->tpoff_reloc)
     56 
     57 #define TILEGX_ELF_PUT_WORD(htab, bfd, val, ptr) \
     58   ((htab)->put_word (bfd, val, ptr))
     59 
     60 /* The name of the dynamic interpreter.  This is put in the .interp
     61    section.  */
     62 
     63 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
     64 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
     65 
     66 
     67 static reloc_howto_type tilegx_elf_howto_table [] =
     68 {
     69   /* This reloc does nothing.  */
     70   HOWTO (R_TILEGX_NONE,	/* type */
     71 	 0,			/* rightshift */
     72 	 0,			/* size */
     73 	 0,			/* bitsize */
     74 	 false,			/* pc_relative */
     75 	 0,			/* bitpos */
     76 	 complain_overflow_dont, /* complain_on_overflow */
     77 	 bfd_elf_generic_reloc,	/* special_function */
     78 	 "R_TILEGX_NONE",	/* name */
     79 	 false,			/* partial_inplace */
     80 	 0,			/* src_mask */
     81 	 0,			/* dst_mask */
     82 	 false),		/* pcrel_offset */
     83 #ifdef BFD64
     84   /* A 64 bit absolute relocation.  */
     85   HOWTO (R_TILEGX_64,	/* type */
     86 	 0,			/* rightshift */
     87 	 8,			/* size */
     88 	 64,			/* bitsize */
     89 	 false,			/* pc_relative */
     90 	 0,			/* bitpos */
     91 	 complain_overflow_dont, /* complain_on_overflow */
     92 	 bfd_elf_generic_reloc,	/* special_function */
     93 	 "R_TILEGX_64",	/* name */
     94 	 false,			/* partial_inplace */
     95 	 0,			/* src_mask */
     96 	 0xffffffffffffffffULL,	/* dst_mask */
     97 	 false),		/* pcrel_offset */
     98 #endif
     99   /* A 32 bit absolute relocation.  */
    100   HOWTO (R_TILEGX_32,	/* type */
    101 	 0,			/* rightshift */
    102 	 4,			/* size */
    103 	 32,			/* bitsize */
    104 	 false,			/* pc_relative */
    105 	 0,			/* bitpos */
    106 	 complain_overflow_dont, /* complain_on_overflow */
    107 	 bfd_elf_generic_reloc,	/* special_function */
    108 	 "R_TILEGX_32",	/* name */
    109 	 false,			/* partial_inplace */
    110 	 0,			/* src_mask */
    111 	 0xffffffff,		/* dst_mask */
    112 	 false),		/* pcrel_offset */
    113 
    114   /* A 16 bit absolute relocation.  */
    115   HOWTO (R_TILEGX_16,	/* type */
    116 	 0,			/* rightshift */
    117 	 2,			/* size */
    118 	 16,			/* bitsize */
    119 	 false,			/* pc_relative */
    120 	 0,			/* bitpos */
    121 	 complain_overflow_bitfield, /* complain_on_overflow */
    122 	 bfd_elf_generic_reloc,	/* special_function */
    123 	 "R_TILEGX_16",	/* name */
    124 	 false,			/* partial_inplace */
    125 	 0,			/* src_mask */
    126 	 0xffff,		/* dst_mask */
    127 	 false),		/* pcrel_offset */
    128 
    129   /* An 8 bit absolute relocation.  */
    130   HOWTO (R_TILEGX_8,	/* type */
    131 	 0,			/* rightshift */
    132 	 1,			/* size */
    133 	 8,			/* bitsize */
    134 	 false,			/* pc_relative */
    135 	 0,			/* bitpos */
    136 	 complain_overflow_unsigned, /* complain_on_overflow */
    137 	 bfd_elf_generic_reloc,	/* special_function */
    138 	 "R_TILEGX_8",	/* name */
    139 	 false,			/* partial_inplace */
    140 	 0,			/* src_mask */
    141 	 0xff,			/* dst_mask */
    142 	 false),		/* pcrel_offset */
    143 #ifdef BFD64
    144   /* A 64 bit pc-relative relocation.  */
    145   HOWTO (R_TILEGX_64_PCREL,/* type */
    146 	 0,			/* rightshift */
    147 	 8,			/* size */
    148 	 64,			/* bitsize */
    149 	 true,			/* pc_relative */
    150 	 0,			/* bitpos */
    151 	 complain_overflow_dont, /* complain_on_overflow */
    152 	 bfd_elf_generic_reloc,	/* special_function */
    153 	 "R_TILEGX_32_PCREL", /* name */
    154 	 false,			/* partial_inplace */
    155 	 0,			/* src_mask */
    156 	 0xffffffffffffffffULL,	/* dst_mask */
    157 	 true),			/* pcrel_offset */
    158 #endif
    159   /* A 32 bit pc-relative relocation.  */
    160   HOWTO (R_TILEGX_32_PCREL,/* type */
    161 	 0,			/* rightshift */
    162 	 4,			/* size */
    163 	 32,			/* bitsize */
    164 	 true,			/* pc_relative */
    165 	 0,			/* bitpos */
    166 	 complain_overflow_dont, /* complain_on_overflow */
    167 	 bfd_elf_generic_reloc,	/* special_function */
    168 	 "R_TILEGX_32_PCREL", /* name */
    169 	 false,			/* partial_inplace */
    170 	 0,			/* src_mask */
    171 	 0xffffffff,		/* dst_mask */
    172 	 true),			/* pcrel_offset */
    173 
    174   /* A 16 bit pc-relative relocation.  */
    175   HOWTO (R_TILEGX_16_PCREL,/* type */
    176 	 0,			/* rightshift */
    177 	 2,			/* size */
    178 	 16,			/* bitsize */
    179 	 true,			/* pc_relative */
    180 	 0,			/* bitpos */
    181 	 complain_overflow_signed, /* complain_on_overflow */
    182 	 bfd_elf_generic_reloc,	/* special_function */
    183 	 "R_TILEGX_16_PCREL",	/* name */
    184 	 false,			/* partial_inplace */
    185 	 0,			/* src_mask */
    186 	 0xffff,		/* dst_mask */
    187 	 true),			/* pcrel_offset */
    188 
    189   /* An 8 bit pc-relative relocation.  */
    190   HOWTO (R_TILEGX_8_PCREL,	/* type */
    191 	 0,			/* rightshift */
    192 	 1,			/* size */
    193 	 8,			/* bitsize */
    194 	 true,			/* pc_relative */
    195 	 0,			/* bitpos */
    196 	 complain_overflow_signed, /* complain_on_overflow */
    197 	 bfd_elf_generic_reloc,	/* special_function */
    198 	 "R_TILEGX_8_PCREL",/* name */
    199 	 false,			/* partial_inplace */
    200 	 0,			/* src_mask */
    201 	 0xff,			/* dst_mask */
    202 	 true),			/* pcrel_offset */
    203 
    204   /* A 16 bit relocation without overflow.  */
    205   HOWTO (R_TILEGX_HW0,	/* type */
    206 	 0,			/* rightshift */
    207 	 2,			/* size */
    208 	 16,			/* bitsize */
    209 	 false,			/* pc_relative */
    210 	 0,			/* bitpos */
    211 	 complain_overflow_dont,/* complain_on_overflow */
    212 	 bfd_elf_generic_reloc,	/* special_function */
    213 	 "R_TILEGX_HW0",	/* name */
    214 	 false,			/* partial_inplace */
    215 	 0,			/* src_mask */
    216 	 0xffff,		/* dst_mask */
    217 	 false),		/* pcrel_offset */
    218 
    219   /* A 16 bit relocation without overflow.  */
    220   HOWTO (R_TILEGX_HW1,	/* type */
    221 	 16,			/* rightshift */
    222 	 2,			/* size */
    223 	 16,			/* bitsize */
    224 	 false,			/* pc_relative */
    225 	 0,			/* bitpos */
    226 	 complain_overflow_dont,/* complain_on_overflow */
    227 	 bfd_elf_generic_reloc,	/* special_function */
    228 	 "R_TILEGX_HW1",	/* name */
    229 	 false,			/* partial_inplace */
    230 	 0,			/* src_mask */
    231 	 0xffff,		/* dst_mask */
    232 	 false),		/* pcrel_offset */
    233 
    234   /* A 16 bit relocation without overflow.  */
    235   HOWTO (R_TILEGX_HW2,	/* type */
    236 	 32,			/* rightshift */
    237 	 2,			/* size */
    238 	 16,			/* bitsize */
    239 	 false,			/* pc_relative */
    240 	 0,			/* bitpos */
    241 	 complain_overflow_dont,/* complain_on_overflow */
    242 	 bfd_elf_generic_reloc,	/* special_function */
    243 	 "R_TILEGX_HW2",	/* name */
    244 	 false,			/* partial_inplace */
    245 	 0,			/* src_mask */
    246 	 0xffff,		/* dst_mask */
    247 	 false),		/* pcrel_offset */
    248 
    249   /* A 16 bit relocation without overflow.  */
    250   HOWTO (R_TILEGX_HW3,	/* type */
    251 	 48,			/* rightshift */
    252 	 2,			/* size */
    253 	 16,			/* bitsize */
    254 	 false,			/* pc_relative */
    255 	 0,			/* bitpos */
    256 	 complain_overflow_dont,/* complain_on_overflow */
    257 	 bfd_elf_generic_reloc,	/* special_function */
    258 	 "R_TILEGX_HW3",	/* name */
    259 	 false,			/* partial_inplace */
    260 	 0,			/* src_mask */
    261 	 0xffff,		/* dst_mask */
    262 	 false),		/* pcrel_offset */
    263 
    264   /* A 16 bit relocation with overflow.  */
    265   HOWTO (R_TILEGX_HW0_LAST,	/* type */
    266 	 0,			/* rightshift */
    267 	 2,			/* size */
    268 	 16,			/* bitsize */
    269 	 false,			/* pc_relative */
    270 	 0,			/* bitpos */
    271 	 complain_overflow_signed,/* complain_on_overflow */
    272 	 bfd_elf_generic_reloc,	/* special_function */
    273 	 "R_TILEGX_HW0_LAST",	/* name */
    274 	 false,			/* partial_inplace */
    275 	 0,			/* src_mask */
    276 	 0xffff,		/* dst_mask */
    277 	 false),		/* pcrel_offset */
    278 
    279   /* A 16 bit relocation with overflow.  */
    280   HOWTO (R_TILEGX_HW1_LAST,	/* type */
    281 	 16,			/* rightshift */
    282 	 2,			/* size */
    283 	 16,			/* bitsize */
    284 	 false,			/* pc_relative */
    285 	 0,			/* bitpos */
    286 	 complain_overflow_signed,/* complain_on_overflow */
    287 	 bfd_elf_generic_reloc,	/* special_function */
    288 	 "R_TILEGX_HW1_LAST",	/* name */
    289 	 false,			/* partial_inplace */
    290 	 0,			/* src_mask */
    291 	 0xffff,		/* dst_mask */
    292 	 false),		/* pcrel_offset */
    293 
    294   /* A 16 bit relocation with overflow.  */
    295   HOWTO (R_TILEGX_HW2_LAST,	/* type */
    296 	 32,			/* rightshift */
    297 	 2,			/* size */
    298 	 16,			/* bitsize */
    299 	 false,			/* pc_relative */
    300 	 0,			/* bitpos */
    301 	 complain_overflow_signed,/* complain_on_overflow */
    302 	 bfd_elf_generic_reloc,	/* special_function */
    303 	 "R_TILEGX_HW2_LAST",	/* name */
    304 	 false,			/* partial_inplace */
    305 	 0,			/* src_mask */
    306 	 0xffff,		/* dst_mask */
    307 	 false),		/* pcrel_offset */
    308 
    309   HOWTO (R_TILEGX_COPY,		/* type */
    310 	 0,			/* rightshift */
    311 	 0,			/* size */
    312 	 0,			/* bitsize */
    313 	 false,			/* pc_relative */
    314 	 0,			/* bitpos */
    315 	 complain_overflow_dont, /* complain_on_overflow */
    316 	 bfd_elf_generic_reloc,	/* special_function */
    317 	 "R_TILEGX_COPY",		/* name */
    318 	 false,			/* partial_inplace */
    319 	 0,			/* src_mask */
    320 	 0,			/* dst_mask */
    321 	 true),			/* pcrel_offset */
    322 
    323   HOWTO (R_TILEGX_GLOB_DAT,	/* type */
    324 	 0,			/* rightshift */
    325 	 0,			/* size */
    326 	 0,			/* bitsize */
    327 	 false,			/* pc_relative */
    328 	 0,			/* bitpos */
    329 	 complain_overflow_dont, /* complain_on_overflow */
    330 	 bfd_elf_generic_reloc,	/* special_function */
    331 	 "R_TILEGX_GLOB_DAT",	/* name */
    332 	 false,			/* partial_inplace */
    333 	 0,			/* src_mask */
    334 	 0,			/* dst_mask */
    335 	 true),			/* pcrel_offset */
    336 
    337   HOWTO (R_TILEGX_JMP_SLOT,	/* type */
    338 	 0,			/* rightshift */
    339 	 0,			/* size */
    340 	 0,			/* bitsize */
    341 	 false,			/* pc_relative */
    342 	 0,			/* bitpos */
    343 	 complain_overflow_dont, /* complain_on_overflow */
    344 	 bfd_elf_generic_reloc,	/* special_function */
    345 	 "R_TILEGX_JMP_SLOT",	/* name */
    346 	 false,			/* partial_inplace */
    347 	 0,			/* src_mask */
    348 	 0,			/* dst_mask */
    349 	 true),			/* pcrel_offset */
    350 
    351   HOWTO (R_TILEGX_RELATIVE,	/* type */
    352 	 0,			/* rightshift */
    353 	 0,			/* size */
    354 	 0,			/* bitsize */
    355 	 false,			/* pc_relative */
    356 	 0,			/* bitpos */
    357 	 complain_overflow_dont, /* complain_on_overflow */
    358 	 bfd_elf_generic_reloc,	/* special_function */
    359 	 "R_TILEGX_RELATIVE",	/* name */
    360 	 false,			/* partial_inplace */
    361 	 0,			/* src_mask */
    362 	 0,			/* dst_mask */
    363 	 true),			/* pcrel_offset */
    364 
    365   HOWTO (R_TILEGX_BROFF_X1, /* type */
    366 	 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES, /* rightshift */
    367 	 4,			/* size */
    368 	 17,			/* bitsize */
    369 	 true,			/* pc_relative */
    370 	 0,			/* bitpos */
    371 	 complain_overflow_signed, /* complain_on_overflow */
    372 	 bfd_elf_generic_reloc, /* special_function */
    373 	 "R_TILEGX_BROFF_X1", /* name */
    374 	 false,			/* partial_inplace */
    375 	 0,			/* src_mask */
    376 	 -1,			/* dst_mask */
    377 	 true),			/* pcrel_offset */
    378 
    379   HOWTO (R_TILEGX_JUMPOFF_X1, /* type */
    380 	 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES, /* rightshift */
    381 	 4,			/* size */
    382 	 27,			/* bitsize */
    383 	 true,			/* pc_relative */
    384 	 0,			/* bitpos */
    385 	 complain_overflow_signed,/* complain_on_overflow */
    386 	 bfd_elf_generic_reloc, /* special_function */
    387 	 "R_TILEGX_JUMPOFF_X1", /* name */
    388 	 false,			/* partial_inplace */
    389 	 0,			/* src_mask */
    390 	 -1,			/* dst_mask */
    391 	 true),			/* pcrel_offset */
    392 
    393   HOWTO (R_TILEGX_JUMPOFF_X1_PLT, /* type */
    394 	 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES, /* rightshift */
    395 	 4,			/* size */
    396 	 27,			/* bitsize */
    397 	 true,			/* pc_relative */
    398 	 0,			/* bitpos */
    399 	 complain_overflow_signed,/* complain_on_overflow */
    400 	 bfd_elf_generic_reloc, /* special_function */
    401 	 "R_TILEGX_JUMPOFF_X1_PLT", /* name */
    402 	 false,			/* partial_inplace */
    403 	 0,			/* src_mask */
    404 	 -1,			/* dst_mask */
    405 	 true),			/* pcrel_offset */
    406 
    407 #define TILEGX_IMM_HOWTO(name, size, bitsize) \
    408   HOWTO (name, 0, size, bitsize, false, 0, \
    409 	 complain_overflow_signed, bfd_elf_generic_reloc, \
    410 	 #name, false, 0, -1, false)
    411 
    412 #define TILEGX_UIMM_HOWTO(name, size, bitsize) \
    413   HOWTO (name, 0, size, bitsize, false, 0, \
    414 	 complain_overflow_unsigned, bfd_elf_generic_reloc, \
    415 	 #name, false, 0, -1, false)
    416 
    417   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X0, 1, 8),
    418   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y0, 1, 8),
    419   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X1, 1, 8),
    420   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y1, 1, 8),
    421   TILEGX_IMM_HOWTO(R_TILEGX_DEST_IMM8_X1, 1, 8),
    422 
    423   TILEGX_UIMM_HOWTO(R_TILEGX_MT_IMM14_X1, 2, 14),
    424   TILEGX_UIMM_HOWTO(R_TILEGX_MF_IMM14_X1, 2, 14),
    425 
    426   TILEGX_UIMM_HOWTO(R_TILEGX_MMSTART_X0, 1, 6),
    427   TILEGX_UIMM_HOWTO(R_TILEGX_MMEND_X0,   1, 6),
    428 
    429   TILEGX_UIMM_HOWTO(R_TILEGX_SHAMT_X0, 1, 6),
    430   TILEGX_UIMM_HOWTO(R_TILEGX_SHAMT_X1, 1, 6),
    431   TILEGX_UIMM_HOWTO(R_TILEGX_SHAMT_Y0, 1, 6),
    432   TILEGX_UIMM_HOWTO(R_TILEGX_SHAMT_Y1, 1, 6),
    433 
    434 #define TILEGX_IMM16_HOWTO(name, rshift) \
    435   HOWTO (name, rshift, 2, 16, false, 0, \
    436 	 complain_overflow_dont, bfd_elf_generic_reloc, \
    437 	 #name, false, 0, 0xffff, false)
    438 
    439   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW0, 0),
    440   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW0, 0),
    441   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW1, 16),
    442   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW1, 16),
    443   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW2, 32),
    444   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW2, 32),
    445   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW3, 48),
    446   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW3, 48),
    447 
    448 #define TILEGX_IMM16_HOWTO_LAST(name, rshift) \
    449   HOWTO (name, rshift, 2, 16, false, 0, \
    450 	 complain_overflow_signed, bfd_elf_generic_reloc, \
    451 	 #name, false, 0, 0xffff, false)
    452 
    453   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW0_LAST, 0),
    454   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW0_LAST, 0),
    455   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW1_LAST, 16),
    456   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW1_LAST, 16),
    457   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW2_LAST, 32),
    458   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW2_LAST, 32),
    459 
    460   /* PC-relative offsets. */
    461 
    462 #define TILEGX_IMM16_HOWTO_PCREL(name, rshift) \
    463   HOWTO (name, rshift, 2, 16, true, 0, \
    464 	 complain_overflow_dont, bfd_elf_generic_reloc, \
    465 	 #name, false, 0, 0xffff, true)
    466 
    467   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW0_PCREL, 0),
    468   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW0_PCREL, 0),
    469   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW1_PCREL, 16),
    470   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW1_PCREL, 16),
    471   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW2_PCREL, 32),
    472   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW2_PCREL, 32),
    473   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW3_PCREL, 48),
    474   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW3_PCREL, 48),
    475 
    476 #define TILEGX_IMM16_HOWTO_LAST_PCREL(name, rshift) \
    477   HOWTO (name, rshift, 2, 16, true, 0, \
    478 	 complain_overflow_signed, bfd_elf_generic_reloc, \
    479 	 #name, false, 0, 0xffff, true)
    480 
    481   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW0_LAST_PCREL,  0),
    482   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW0_LAST_PCREL,  0),
    483   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW1_LAST_PCREL, 16),
    484   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW1_LAST_PCREL, 16),
    485   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW2_LAST_PCREL, 32),
    486   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW2_LAST_PCREL, 32),
    487 
    488   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW0_GOT, 0),
    489   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW0_GOT, 0),
    490 
    491   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW0_PLT_PCREL, 0),
    492   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW0_PLT_PCREL, 0),
    493   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW1_PLT_PCREL, 16),
    494   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW1_PLT_PCREL, 16),
    495   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW2_PLT_PCREL, 32),
    496   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW2_PLT_PCREL, 32),
    497 
    498   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW0_LAST_GOT, 0),
    499   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW0_LAST_GOT, 0),
    500   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW1_LAST_GOT, 16),
    501   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW1_LAST_GOT, 16),
    502 
    503   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X0_HW3_PLT_PCREL, 48),
    504   TILEGX_IMM16_HOWTO_PCREL (R_TILEGX_IMM16_X1_HW3_PLT_PCREL, 48),
    505 
    506   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW0_TLS_GD, 0),
    507   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW0_TLS_GD, 0),
    508 
    509   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X0_HW0_TLS_LE, 0),
    510   TILEGX_IMM16_HOWTO (R_TILEGX_IMM16_X1_HW0_TLS_LE, 0),
    511   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE, 0),
    512   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE, 0),
    513   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE, 16),
    514   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE, 16),
    515 
    516   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD, 0),
    517   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD, 0),
    518   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD, 16),
    519   TILEGX_IMM16_HOWTO_LAST (R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD, 16),
    520   EMPTY_HOWTO (90),
    521   EMPTY_HOWTO (91),
    522 
    523 #define TILEGX_IMM16_HOWTO_TLS_IE(name, rshift) \
    524   HOWTO (name, rshift, 2, 16, false, 0, \
    525 	 complain_overflow_dont, bfd_elf_generic_reloc, \
    526 	 #name, false, 0, 0xffff, true)
    527 
    528   TILEGX_IMM16_HOWTO_TLS_IE (R_TILEGX_IMM16_X0_HW0_TLS_IE, 0),
    529   TILEGX_IMM16_HOWTO_TLS_IE (R_TILEGX_IMM16_X1_HW0_TLS_IE, 0),
    530 
    531   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL,  0),
    532   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL,  0),
    533   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL, 16),
    534   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL, 16),
    535   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL, 32),
    536   TILEGX_IMM16_HOWTO_LAST_PCREL (R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL, 32),
    537 
    538 #define TILEGX_IMM16_HOWTO_LAST_TLS_IE(name, rshift) \
    539   HOWTO (name, rshift, 2, 16, false, 0, \
    540 	 complain_overflow_signed, bfd_elf_generic_reloc, \
    541 	 #name, false, 0, 0xffff, true)
    542 
    543   TILEGX_IMM16_HOWTO_LAST_TLS_IE (R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE, 0),
    544   TILEGX_IMM16_HOWTO_LAST_TLS_IE (R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE, 0),
    545   TILEGX_IMM16_HOWTO_LAST_TLS_IE (R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE, 16),
    546   TILEGX_IMM16_HOWTO_LAST_TLS_IE (R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE, 16),
    547   EMPTY_HOWTO (104),
    548   EMPTY_HOWTO (105),
    549 
    550   HOWTO(R_TILEGX_TLS_DTPMOD64, 0, 0, 0, false, 0, complain_overflow_dont,
    551 	bfd_elf_generic_reloc, "R_TILEGX_TLS_DTPMOD64",
    552 	false, 0, 0, true),
    553   HOWTO(R_TILEGX_TLS_DTPOFF64, 0, 8, 64, false, 0, complain_overflow_bitfield,
    554 	bfd_elf_generic_reloc, "R_TILEGX_TLS_DTPOFF64",
    555 	false, 0, -1, true),
    556   HOWTO(R_TILEGX_TLS_TPOFF64, 0, 0, 0, false, 0, complain_overflow_dont,
    557 	bfd_elf_generic_reloc, "R_TILEGX_TLS_TPOFF64",
    558 	false, 0, 0, true),
    559 
    560   HOWTO(R_TILEGX_TLS_DTPMOD32, 0, 0, 0, false, 0, complain_overflow_dont,
    561 	bfd_elf_generic_reloc, "R_TILEGX_TLS_DTPMOD32",
    562 	false, 0, 0, true),
    563   HOWTO(R_TILEGX_TLS_DTPOFF32, 0, 8, 32, false, 0, complain_overflow_bitfield,
    564 	bfd_elf_generic_reloc, "R_TILEGX_TLS_DTPOFF32",
    565 	false, 0, -1, true),
    566   HOWTO(R_TILEGX_TLS_TPOFF32, 0, 0, 0, false, 0, complain_overflow_dont,
    567 	bfd_elf_generic_reloc, "R_TILEGX_TLS_TPOFF32",
    568 	false, 0, 0, true),
    569 
    570   HOWTO (R_TILEGX_TLS_GD_CALL, /* type */
    571 	 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES, /* rightshift */
    572 	 4,			/* size */
    573 	 27,			/* bitsize */
    574 	 true,			/* pc_relative */
    575 	 0,			/* bitpos */
    576 	 complain_overflow_signed,/* complain_on_overflow */
    577 	 bfd_elf_generic_reloc, /* special_function */
    578 	 "R_TILEGX_TLS_GD_CALL", /* name */
    579 	 false,			/* partial_inplace */
    580 	 0,			/* src_mask */
    581 	 -1,			/* dst_mask */
    582 	 true),			/* pcrel_offset */
    583 
    584   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X0_TLS_GD_ADD,  1,  8),
    585   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X1_TLS_GD_ADD,  1,  8),
    586   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y0_TLS_GD_ADD,  1,  8),
    587   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y1_TLS_GD_ADD,  1,  8),
    588   TILEGX_IMM_HOWTO(R_TILEGX_TLS_IE_LOAD, 1,  8),
    589   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X0_TLS_ADD,  1,  8),
    590   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_X1_TLS_ADD,  1,  8),
    591   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y0_TLS_ADD,  1,  8),
    592   TILEGX_IMM_HOWTO(R_TILEGX_IMM8_Y1_TLS_ADD,  1,  8),
    593 };
    594 
    595 static reloc_howto_type tilegx_elf_howto_table2 [] =
    596 {
    597   /* GNU extension to record C++ vtable hierarchy */
    598   HOWTO (R_TILEGX_GNU_VTINHERIT, /* type */
    599 	 0,			/* rightshift */
    600 	 8,			/* size */
    601 	 0,			/* bitsize */
    602 	 false,			/* pc_relative */
    603 	 0,			/* bitpos */
    604 	 complain_overflow_dont, /* complain_on_overflow */
    605 	 NULL,			/* special_function */
    606 	 "R_TILEGX_GNU_VTINHERIT", /* name */
    607 	 false,			/* partial_inplace */
    608 	 0,			/* src_mask */
    609 	 0,			/* dst_mask */
    610 	 false),		/* pcrel_offset */
    611 
    612   /* GNU extension to record C++ vtable member usage */
    613   HOWTO (R_TILEGX_GNU_VTENTRY,	   /* type */
    614 	 0,			/* rightshift */
    615 	 8,			/* size */
    616 	 0,			/* bitsize */
    617 	 false,			/* pc_relative */
    618 	 0,			/* bitpos */
    619 	 complain_overflow_dont, /* complain_on_overflow */
    620 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
    621 	 "R_TILEGX_GNU_VTENTRY",   /* name */
    622 	 false,			/* partial_inplace */
    623 	 0,			/* src_mask */
    624 	 0,			/* dst_mask */
    625 	 false),		/* pcrel_offset */
    626 
    627 };
    628 
    629 /* Map BFD reloc types to TILEGX ELF reloc types.  */
    631 
    632 typedef struct tilegx_reloc_map
    633 {
    634   bfd_reloc_code_real_type  bfd_reloc_val;
    635   unsigned int		    tilegx_reloc_val;
    636   reloc_howto_type *	    table;
    637 } reloc_map;
    638 
    639 static const reloc_map tilegx_reloc_map [] =
    640 {
    641 #define TH_REMAP(bfd, tilegx) \
    642   { bfd, tilegx, tilegx_elf_howto_table },
    643 
    644   /* Standard relocations. */
    645   TH_REMAP (BFD_RELOC_NONE,		       R_TILEGX_NONE)
    646   TH_REMAP (BFD_RELOC_64,		       R_TILEGX_64)
    647   TH_REMAP (BFD_RELOC_32,		       R_TILEGX_32)
    648   TH_REMAP (BFD_RELOC_16,		       R_TILEGX_16)
    649   TH_REMAP (BFD_RELOC_8,		       R_TILEGX_8)
    650   TH_REMAP (BFD_RELOC_64_PCREL,		       R_TILEGX_64_PCREL)
    651   TH_REMAP (BFD_RELOC_32_PCREL,		       R_TILEGX_32_PCREL)
    652   TH_REMAP (BFD_RELOC_16_PCREL,		       R_TILEGX_16_PCREL)
    653   TH_REMAP (BFD_RELOC_8_PCREL,		       R_TILEGX_8_PCREL)
    654   TH_REMAP (BFD_RELOC_COPY,		       R_TILEGX_COPY)
    655   TH_REMAP (BFD_RELOC_GLOB_DAT,		       R_TILEGX_GLOB_DAT)
    656   TH_REMAP (BFD_RELOC_JMP_SLOT,		       R_TILEGX_JMP_SLOT)
    657   TH_REMAP (BFD_RELOC_RELATIVE,		       R_TILEGX_RELATIVE)
    658 
    659 #define SIMPLE_REMAP(t) TH_REMAP (BFD_RELOC_##t, R_##t)
    660 
    661   /* Custom relocations. */
    662   SIMPLE_REMAP (TILEGX_HW0)
    663   SIMPLE_REMAP (TILEGX_HW1)
    664   SIMPLE_REMAP (TILEGX_HW2)
    665   SIMPLE_REMAP (TILEGX_HW3)
    666   SIMPLE_REMAP (TILEGX_HW0_LAST)
    667   SIMPLE_REMAP (TILEGX_HW1_LAST)
    668   SIMPLE_REMAP (TILEGX_HW2_LAST)
    669   SIMPLE_REMAP (TILEGX_BROFF_X1)
    670   SIMPLE_REMAP (TILEGX_JUMPOFF_X1)
    671   SIMPLE_REMAP (TILEGX_JUMPOFF_X1_PLT)
    672   SIMPLE_REMAP (TILEGX_IMM8_X0)
    673   SIMPLE_REMAP (TILEGX_IMM8_Y0)
    674   SIMPLE_REMAP (TILEGX_IMM8_X1)
    675   SIMPLE_REMAP (TILEGX_IMM8_Y1)
    676   SIMPLE_REMAP (TILEGX_DEST_IMM8_X1)
    677   SIMPLE_REMAP (TILEGX_MT_IMM14_X1)
    678   SIMPLE_REMAP (TILEGX_MF_IMM14_X1)
    679   SIMPLE_REMAP (TILEGX_MMSTART_X0)
    680   SIMPLE_REMAP (TILEGX_MMEND_X0)
    681   SIMPLE_REMAP (TILEGX_SHAMT_X0)
    682   SIMPLE_REMAP (TILEGX_SHAMT_X1)
    683   SIMPLE_REMAP (TILEGX_SHAMT_Y0)
    684   SIMPLE_REMAP (TILEGX_SHAMT_Y1)
    685   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0)
    686   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0)
    687   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1)
    688   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1)
    689   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2)
    690   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2)
    691   SIMPLE_REMAP (TILEGX_IMM16_X0_HW3)
    692   SIMPLE_REMAP (TILEGX_IMM16_X1_HW3)
    693   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST)
    694   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST)
    695   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST)
    696   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST)
    697   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2_LAST)
    698   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2_LAST)
    699   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_PCREL)
    700   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_PCREL)
    701   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_PCREL)
    702   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_PCREL)
    703   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2_PCREL)
    704   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2_PCREL)
    705   SIMPLE_REMAP (TILEGX_IMM16_X0_HW3_PCREL)
    706   SIMPLE_REMAP (TILEGX_IMM16_X1_HW3_PCREL)
    707   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_PCREL)
    708   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_PCREL)
    709   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_PCREL)
    710   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_PCREL)
    711   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2_LAST_PCREL)
    712   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2_LAST_PCREL)
    713   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_GOT)
    714   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_GOT)
    715   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_PLT_PCREL)
    716   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_PLT_PCREL)
    717   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_PLT_PCREL)
    718   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_PLT_PCREL)
    719   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2_PLT_PCREL)
    720   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2_PLT_PCREL)
    721   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_GOT)
    722   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_GOT)
    723   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_GOT)
    724   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_GOT)
    725   SIMPLE_REMAP (TILEGX_IMM16_X0_HW3_PLT_PCREL)
    726   SIMPLE_REMAP (TILEGX_IMM16_X1_HW3_PLT_PCREL)
    727   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_TLS_GD)
    728   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_TLS_GD)
    729   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_TLS_LE)
    730   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_TLS_LE)
    731   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_TLS_LE)
    732   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_TLS_LE)
    733   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_TLS_LE)
    734   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_TLS_LE)
    735   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_TLS_GD)
    736   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_TLS_GD)
    737   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_TLS_GD)
    738   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_TLS_GD)
    739   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_TLS_IE)
    740   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_TLS_IE)
    741   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL)
    742   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL)
    743   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL)
    744   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL)
    745   SIMPLE_REMAP (TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL)
    746   SIMPLE_REMAP (TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL)
    747   SIMPLE_REMAP (TILEGX_IMM16_X0_HW0_LAST_TLS_IE)
    748   SIMPLE_REMAP (TILEGX_IMM16_X1_HW0_LAST_TLS_IE)
    749   SIMPLE_REMAP (TILEGX_IMM16_X0_HW1_LAST_TLS_IE)
    750   SIMPLE_REMAP (TILEGX_IMM16_X1_HW1_LAST_TLS_IE)
    751 
    752   SIMPLE_REMAP (TILEGX_TLS_DTPMOD64)
    753   SIMPLE_REMAP (TILEGX_TLS_DTPOFF64)
    754   SIMPLE_REMAP (TILEGX_TLS_TPOFF64)
    755 
    756   SIMPLE_REMAP (TILEGX_TLS_DTPMOD32)
    757   SIMPLE_REMAP (TILEGX_TLS_DTPOFF32)
    758   SIMPLE_REMAP (TILEGX_TLS_TPOFF32)
    759 
    760   SIMPLE_REMAP (TILEGX_TLS_GD_CALL)
    761   SIMPLE_REMAP (TILEGX_IMM8_X0_TLS_GD_ADD)
    762   SIMPLE_REMAP (TILEGX_IMM8_X1_TLS_GD_ADD)
    763   SIMPLE_REMAP (TILEGX_IMM8_Y0_TLS_GD_ADD)
    764   SIMPLE_REMAP (TILEGX_IMM8_Y1_TLS_GD_ADD)
    765   SIMPLE_REMAP (TILEGX_TLS_IE_LOAD)
    766   SIMPLE_REMAP (TILEGX_IMM8_X0_TLS_ADD)
    767   SIMPLE_REMAP (TILEGX_IMM8_X1_TLS_ADD)
    768   SIMPLE_REMAP (TILEGX_IMM8_Y0_TLS_ADD)
    769   SIMPLE_REMAP (TILEGX_IMM8_Y1_TLS_ADD)
    770 
    771 #undef SIMPLE_REMAP
    772 #undef TH_REMAP
    773 
    774   { BFD_RELOC_VTABLE_INHERIT,	    R_TILEGX_GNU_VTINHERIT, tilegx_elf_howto_table2 },
    775   { BFD_RELOC_VTABLE_ENTRY,	    R_TILEGX_GNU_VTENTRY,   tilegx_elf_howto_table2 },
    776 };
    777 
    778 
    779 
    780 /* TILEGX ELF linker hash entry.  */
    781 
    782 struct tilegx_elf_link_hash_entry
    783 {
    784   struct elf_link_hash_entry elf;
    785 
    786 #define GOT_UNKNOWN     0
    787 #define GOT_NORMAL      1
    788 #define GOT_TLS_GD      2
    789 #define GOT_TLS_IE      4
    790   unsigned char tls_type;
    791 };
    792 
    793 #define tilegx_elf_hash_entry(ent) \
    794   ((struct tilegx_elf_link_hash_entry *)(ent))
    795 
    796 struct _bfd_tilegx_elf_obj_tdata
    797 {
    798   struct elf_obj_tdata root;
    799 
    800   /* tls_type for each local got entry.  */
    801   char *local_got_tls_type;
    802 };
    803 
    804 #define _bfd_tilegx_elf_tdata(abfd) \
    805   ((struct _bfd_tilegx_elf_obj_tdata *) (abfd)->tdata.any)
    806 
    807 #define _bfd_tilegx_elf_local_got_tls_type(abfd) \
    808   (_bfd_tilegx_elf_tdata (abfd)->local_got_tls_type)
    809 
    810 #define is_tilegx_elf(bfd)				\
    811   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    812    && elf_tdata (bfd) != NULL				\
    813    && elf_object_id (bfd) == TILEGX_ELF_DATA)
    814 
    815 #include "elf/common.h"
    816 #include "elf/internal.h"
    817 
    818 struct tilegx_elf_link_hash_table
    819 {
    820   struct elf_link_hash_table elf;
    821 
    822   int bytes_per_word;
    823   int word_align_power;
    824   int bytes_per_rela;
    825   int dtpmod_reloc;
    826   int dtpoff_reloc;
    827   int tpoff_reloc;
    828   bfd_vma (*r_info) (Elf_Internal_Rela *, bfd_vma, bfd_vma);
    829   bfd_vma (*r_symndx) (bfd_vma);
    830   void (*put_word) (bfd *, bfd_vma, void *);
    831   const char *dynamic_interpreter;
    832 
    833   /* Whether LE transition has been disabled for some of the
    834      sections.  */
    835   bool disable_le_transition;
    836 };
    837 
    838 
    839 /* Get the Tile ELF linker hash table from a link_info structure.  */
    840 #define tilegx_elf_hash_table(p) \
    841   ((is_elf_hash_table ((p)->hash)					\
    842     && elf_hash_table_id (elf_hash_table (p)) == TILEGX_ELF_DATA)	\
    843    ? (struct tilegx_elf_link_hash_table *) (p)->hash : NULL)
    844 
    845 #ifdef BFD64
    846 static bfd_vma
    847 tilegx_elf_r_info_64 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
    848 		      bfd_vma rel_index,
    849 		      bfd_vma type)
    850 {
    851   return ELF64_R_INFO (rel_index, type);
    852 }
    853 
    854 static bfd_vma
    855 tilegx_elf_r_symndx_64 (bfd_vma r_info)
    856 {
    857   return ELF64_R_SYM (r_info);
    858 }
    859 
    860 static void
    861 tilegx_put_word_64 (bfd *abfd, bfd_vma val, void *ptr)
    862 {
    863   bfd_put_64 (abfd, val, ptr);
    864 }
    865 #endif /* BFD64 */
    866 
    867 static bfd_vma
    868 tilegx_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
    869 		      bfd_vma rel_index,
    870 		      bfd_vma type)
    871 {
    872   return ELF32_R_INFO (rel_index, type);
    873 }
    874 
    875 static bfd_vma
    876 tilegx_elf_r_symndx_32 (bfd_vma r_info)
    877 {
    878   return ELF32_R_SYM (r_info);
    879 }
    880 
    881 static void
    882 tilegx_put_word_32 (bfd *abfd, bfd_vma val, void *ptr)
    883 {
    884   bfd_put_32 (abfd, val, ptr);
    885 }
    886 
    887 reloc_howto_type *
    888 tilegx_reloc_type_lookup (bfd * abfd,
    889 			  bfd_reloc_code_real_type code)
    890 {
    891   unsigned int i;
    892 
    893   for (i = ARRAY_SIZE (tilegx_reloc_map); i--;)
    894     {
    895       const reloc_map * entry;
    896 
    897       entry = tilegx_reloc_map + i;
    898 
    899       if (entry->bfd_reloc_val == code)
    900 	return entry->table + (entry->tilegx_reloc_val
    901 			       - entry->table[0].type);
    902     }
    903 
    904   /* xgettext:c-format */
    905   _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    906 		      abfd, (int) code);
    907   bfd_set_error (bfd_error_bad_value);
    908   return NULL;
    909 }
    910 
    911 reloc_howto_type *
    912 tilegx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    913 			  const char *r_name)
    914 {
    915   unsigned int i;
    916 
    917   for (i = 0;
    918        i < (sizeof (tilegx_elf_howto_table)
    919 	    / sizeof (tilegx_elf_howto_table[0]));
    920        i++)
    921     if (tilegx_elf_howto_table[i].name != NULL
    922 	&& strcasecmp (tilegx_elf_howto_table[i].name, r_name) == 0)
    923       return &tilegx_elf_howto_table[i];
    924 
    925   return NULL;
    926 }
    927 
    928 bool
    929 tilegx_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
    930 			   arelent *cache_ptr,
    931 			   Elf_Internal_Rela *dst)
    932 {
    933   unsigned int r_type = TILEGX_ELF_R_TYPE (dst->r_info);
    934 
    935   if (r_type <= (unsigned int) R_TILEGX_IMM8_Y1_TLS_ADD)
    936     cache_ptr->howto = &tilegx_elf_howto_table [r_type];
    937   else if (r_type - R_TILEGX_GNU_VTINHERIT
    938 	   <= ((unsigned int) R_TILEGX_GNU_VTENTRY
    939 	       - (unsigned int) R_TILEGX_GNU_VTINHERIT))
    940     cache_ptr->howto
    941       = &tilegx_elf_howto_table2 [r_type - R_TILEGX_GNU_VTINHERIT];
    942   else
    943     {
    944       /* xgettext:c-format */
    945       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    946 			  abfd, r_type);
    947       bfd_set_error (bfd_error_bad_value);
    948       return false;
    949     }
    950   return true;
    951 }
    952 
    953 typedef tilegx_bundle_bits (*tilegx_create_func)(int);
    954 
    955 static const tilegx_create_func reloc_to_create_func[] =
    956 {
    957   /* The first twenty relocation types don't correspond to operands */
    958   NULL,
    959   NULL,
    960   NULL,
    961   NULL,
    962   NULL,
    963   NULL,
    964   NULL,
    965   NULL,
    966   NULL,
    967   NULL,
    968   NULL,
    969   NULL,
    970   NULL,
    971   NULL,
    972   NULL,
    973   NULL,
    974   NULL,
    975   NULL,
    976   NULL,
    977   NULL,
    978 
    979   /* The remaining relocations are used for immediate operands */
    980   create_BrOff_X1,
    981   create_JumpOff_X1,
    982   create_JumpOff_X1,
    983   create_Imm8_X0,
    984   create_Imm8_Y0,
    985   create_Imm8_X1,
    986   create_Imm8_Y1,
    987   create_Dest_Imm8_X1,
    988   create_MT_Imm14_X1,
    989   create_MF_Imm14_X1,
    990   create_BFStart_X0,
    991   create_BFEnd_X0,
    992   create_ShAmt_X0,
    993   create_ShAmt_X1,
    994   create_ShAmt_Y0,
    995   create_ShAmt_Y1,
    996   create_Imm16_X0,
    997   create_Imm16_X1,
    998   create_Imm16_X0,
    999   create_Imm16_X1,
   1000   create_Imm16_X0,
   1001   create_Imm16_X1,
   1002   create_Imm16_X0,
   1003   create_Imm16_X1,
   1004   create_Imm16_X0,
   1005   create_Imm16_X1,
   1006   create_Imm16_X0,
   1007   create_Imm16_X1,
   1008   create_Imm16_X0,
   1009   create_Imm16_X1,
   1010   create_Imm16_X0,
   1011   create_Imm16_X1,
   1012   create_Imm16_X0,
   1013   create_Imm16_X1,
   1014   create_Imm16_X0,
   1015   create_Imm16_X1,
   1016   create_Imm16_X0,
   1017   create_Imm16_X1,
   1018   create_Imm16_X0,
   1019   create_Imm16_X1,
   1020   create_Imm16_X0,
   1021   create_Imm16_X1,
   1022   create_Imm16_X0,
   1023   create_Imm16_X1,
   1024   create_Imm16_X0,
   1025   create_Imm16_X1,
   1026   create_Imm16_X0,
   1027   create_Imm16_X1,
   1028   create_Imm16_X0,
   1029   create_Imm16_X1,
   1030   create_Imm16_X0,
   1031   create_Imm16_X1,
   1032   create_Imm16_X0,
   1033   create_Imm16_X1,
   1034   create_Imm16_X0,
   1035   create_Imm16_X1,
   1036   create_Imm16_X0,
   1037   create_Imm16_X1,
   1038   create_Imm16_X0,
   1039   create_Imm16_X1,
   1040   create_Imm16_X0,
   1041   create_Imm16_X1,
   1042   create_Imm16_X0,
   1043   create_Imm16_X1,
   1044   create_Imm16_X0,
   1045   create_Imm16_X1,
   1046   create_Imm16_X0,
   1047   create_Imm16_X1,
   1048   create_Imm16_X0,
   1049   create_Imm16_X1,
   1050   NULL,
   1051   NULL,
   1052   create_Imm16_X0,
   1053   create_Imm16_X1,
   1054   create_Imm16_X0,
   1055   create_Imm16_X1,
   1056   create_Imm16_X0,
   1057   create_Imm16_X1,
   1058   create_Imm16_X0,
   1059   create_Imm16_X1,
   1060   create_Imm16_X0,
   1061   create_Imm16_X1,
   1062   create_Imm16_X0,
   1063   create_Imm16_X1,
   1064 };
   1065 
   1066 static void
   1067 tilegx_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   1068 {
   1069   elf_backend_data *bed;
   1070   bfd_byte *loc;
   1071 
   1072   bed = get_elf_backend_data (abfd);
   1073   loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   1074   bed->s->swap_reloca_out (abfd, rel, loc);
   1075 }
   1076 
   1077 /* PLT/GOT stuff */
   1078 
   1079 /* The procedure linkage table starts with the following header:
   1080 
   1081      ld_add	  r28, r27, 8
   1082      ld		  r27, r27
   1083    {
   1084      jr		  r27
   1085      info	  10		## SP not offset, return PC in LR
   1086    }
   1087 
   1088    Subsequent entries are the following, jumping to the header at the end:
   1089 
   1090    {
   1091      moveli	  r28, <_GLOBAL_OFFSET_TABLE_ - 1f + MY_GOT_OFFSET>
   1092      lnk	  r26
   1093    }
   1094 1:
   1095    {
   1096      moveli	  r27, <_GLOBAL_OFFSET_TABLE_ - 1b>
   1097      shl16insli	  r28, r28, <_GLOBAL_OFFSET_TABLE_ - 1b + MY_GOT_OFFSET>
   1098    }
   1099    {
   1100      add	  r28, r26, r28
   1101      shl16insli	  r27, r27, <_GLOBAL_OFFSET_TABLE_ - 1b>
   1102    }
   1103    {
   1104      add	  r27, r26, r27
   1105      ld		  r28, r28
   1106      info	  10	   ## SP not offset, return PC in LR
   1107    }
   1108    {
   1109      shl16insli	  r29, zero, MY_PLT_INDEX
   1110      jr		  r28
   1111    }
   1112 
   1113    This code sequence lets the code at at the start of the PLT determine
   1114    which PLT entry was executed by examining 'r29'.
   1115 
   1116    Note that MY_PLT_INDEX skips over the header entries, so the first
   1117    actual jump table entry has index zero.
   1118 
   1119    If the offset fits in 16 bits,
   1120 
   1121      lnk	  r26
   1122 1:
   1123    {
   1124      addli	  r28, r26, <_GLOBAL_OFFSET_TABLE_ - 1b + MY_GOT_OFFSET>
   1125      moveli	  r27, <_GLOBAL_OFFSET_TABLE_ - 1b>
   1126    }
   1127    {
   1128      shl16insli	  r29, zero, MY_PLT_INDEX
   1129      ld		  r28, r28
   1130    }
   1131    {
   1132      add	  r27, r26, r27
   1133      jr		  r28
   1134    }
   1135      info	  10	   ## SP not offset, return PC in LR
   1136 
   1137    For the purpose of backtracing, the procedure linkage table ends with the
   1138    following tail entry:
   1139 
   1140      info	  10	   ## SP not offset, return PC in LR
   1141 
   1142    The 32-bit versions are similar, with ld4s replacing ld, and offsets into
   1143    the GOT being multiples of 4 instead of 8.
   1144 
   1145 */
   1146 
   1147 #define PLT_HEADER_SIZE_IN_BUNDLES 3
   1148 #define PLT_ENTRY_SIZE_IN_BUNDLES 5
   1149 #define PLT_TAIL_SIZE_IN_BUNDLES 1
   1150 
   1151 #define PLT_HEADER_SIZE \
   1152   (PLT_HEADER_SIZE_IN_BUNDLES * TILEGX_BUNDLE_SIZE_IN_BYTES)
   1153 #define PLT_ENTRY_SIZE \
   1154   (PLT_ENTRY_SIZE_IN_BUNDLES * TILEGX_BUNDLE_SIZE_IN_BYTES)
   1155 #define PLT_TAIL_SIZE \
   1156   (PLT_TAIL_SIZE_IN_BUNDLES * TILEGX_BUNDLE_SIZE_IN_BYTES)
   1157 
   1158 #define GOT_ENTRY_SIZE(htab) TILEGX_ELF_WORD_BYTES (htab)
   1159 
   1160 #define GOTPLT_HEADER_SIZE(htab) (2 * GOT_ENTRY_SIZE (htab))
   1161 
   1162 static const bfd_byte
   1163 tilegx64_plt0_entry[PLT_HEADER_SIZE] =
   1164 {
   1165   0x00, 0x30, 0x48, 0x51,
   1166   0x6e, 0x43, 0xa0, 0x18, /* { ld_add r28, r27, 8 } */
   1167   0x00, 0x30, 0xbc, 0x35,
   1168   0x00, 0x40, 0xde, 0x9e, /* { ld r27, r27 } */
   1169   0xff, 0xaf, 0x30, 0x40,
   1170   0x60, 0x73, 0x6a, 0x28, /* { info 10 ; jr r27 } */
   1171 };
   1172 
   1173 static const bfd_byte
   1174 tilegx64_long_plt_entry[PLT_ENTRY_SIZE] =
   1175 {
   1176   0xdc, 0x0f, 0x00, 0x10,
   1177   0x0d, 0xf0, 0x6a, 0x28, /* { moveli r28, 0 ; lnk r26 } */
   1178   0xdb, 0x0f, 0x00, 0x10,
   1179   0x8e, 0x03, 0x00, 0x38, /* { moveli r27, 0 ; shl16insli r28, r28, 0 } */
   1180   0x9c, 0xc6, 0x0d, 0xd0,
   1181   0x6d, 0x03, 0x00, 0x38, /* { add r28, r26, r28 ; shl16insli r27, r27, 0 } */
   1182   0x9b, 0xb6, 0xc5, 0xad,
   1183   0xff, 0x57, 0xe0, 0x8e, /* { add r27, r26, r27 ; info 10 ; ld r28, r28 } */
   1184   0xdd, 0x0f, 0x00, 0x70,
   1185   0x80, 0x73, 0x6a, 0x28, /* { shl16insli r29, zero, 0 ; jr r28 } */
   1186 };
   1187 
   1188 static const bfd_byte
   1189 tilegx64_short_plt_entry[PLT_ENTRY_SIZE] =
   1190 {
   1191   0x00, 0x30, 0x48, 0x51,
   1192   0x0d, 0xf0, 0x6a, 0x28, /* { lnk r26 } */
   1193   0x9c, 0x06, 0x00, 0x90,
   1194   0xed, 0x07, 0x00, 0x00, /* { addli r28, r26, 0 ; moveli r27, 0 } */
   1195   0xdd, 0x0f, 0x00, 0x70,
   1196   0x8e, 0xeb, 0x6a, 0x28, /* { shl16insli r29, zero, 0 ; ld r28, r28 } */
   1197   0x9b, 0xb6, 0x0d, 0x50,
   1198   0x80, 0x73, 0x6a, 0x28, /* { add r27, r26, r27 ; jr r28 } */
   1199   0x00, 0x30, 0x48, 0xd1,
   1200   0xff, 0x57, 0x18, 0x18, /* { info 10 } */
   1201 };
   1202 
   1203 /* Reuse an existing info 10 bundle.  */
   1204 static const bfd_byte *const tilegx64_plt_tail_entry =
   1205   &tilegx64_short_plt_entry[4 * TILEGX_BUNDLE_SIZE_IN_BYTES];
   1206 
   1207 static const bfd_byte
   1208 tilegx32_plt0_entry[PLT_HEADER_SIZE] =
   1209 {
   1210   0x00, 0x30, 0x48, 0x51,
   1211   0x6e, 0x23, 0x58, 0x18, /* { ld4s_add r28, r27, 4 } */
   1212   0x00, 0x30, 0xbc, 0x35,
   1213   0x00, 0x40, 0xde, 0x9c, /* { ld4s r27, r27 } */
   1214   0xff, 0xaf, 0x30, 0x40,
   1215   0x60, 0x73, 0x6a, 0x28, /* { info 10 ; jr r27 } */
   1216 };
   1217 
   1218 static const bfd_byte
   1219 tilegx32_long_plt_entry[PLT_ENTRY_SIZE] =
   1220 {
   1221   0xdc, 0x0f, 0x00, 0x10,
   1222   0x0d, 0xf0, 0x6a, 0x28, /* { moveli r28, 0 ; lnk r26 } */
   1223   0xdb, 0x0f, 0x00, 0x10,
   1224   0x8e, 0x03, 0x00, 0x38, /* { moveli r27, 0 ; shl16insli r28, r28, 0 } */
   1225   0x9c, 0xc6, 0x0d, 0xd0,
   1226   0x6d, 0x03, 0x00, 0x38, /* { add r28, r26, r28 ; shl16insli r27, r27, 0 } */
   1227   0x9b, 0xb6, 0xc5, 0xad,
   1228   0xff, 0x57, 0xe0, 0x8c, /* { add r27, r26, r27 ; info 10 ; ld4s r28, r28 } */
   1229   0xdd, 0x0f, 0x00, 0x70,
   1230   0x80, 0x73, 0x6a, 0x28, /* { shl16insli r29, zero, 0 ; jr r28 } */
   1231 };
   1232 
   1233 static const bfd_byte
   1234 tilegx32_short_plt_entry[PLT_ENTRY_SIZE] =
   1235 {
   1236   0x00, 0x30, 0x48, 0x51,
   1237   0x0d, 0xf0, 0x6a, 0x28, /* { lnk r26 } */
   1238   0x9c, 0x06, 0x00, 0x90,
   1239   0xed, 0x07, 0x00, 0x00, /* { addli r28, r26, 0 ; moveli r27, 0 } */
   1240   0xdd, 0x0f, 0x00, 0x70,
   1241   0x8e, 0x9b, 0x6a, 0x28, /* { shl16insli r29, zero, 0 ; ld4s r28, r28 } */
   1242   0x9b, 0xb6, 0x0d, 0x50,
   1243   0x80, 0x73, 0x6a, 0x28, /* { add r27, r26, r27 ; jr r28 } */
   1244   0x00, 0x30, 0x48, 0xd1,
   1245   0xff, 0x57, 0x18, 0x18, /* { info 10 } */
   1246 };
   1247 
   1248 /* Reuse an existing info 10 bundle.  */
   1249 static const bfd_byte *const tilegx32_plt_tail_entry =
   1250   &tilegx64_short_plt_entry[4 * TILEGX_BUNDLE_SIZE_IN_BYTES];
   1251 
   1252 static int
   1253 tilegx_plt_entry_build (bfd *output_bfd,
   1254 			struct tilegx_elf_link_hash_table *htab,
   1255 			asection *splt, asection *sgotplt,
   1256 			bfd_vma offset, bfd_vma *r_offset)
   1257 {
   1258   int plt_index = (offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
   1259   int got_offset = (plt_index * GOT_ENTRY_SIZE (htab)
   1260 		    + GOTPLT_HEADER_SIZE (htab));
   1261   tilegx_bundle_bits *pc;
   1262 
   1263   /* Compute the distance from the got entry to the lnk.  */
   1264   bfd_signed_vma dist_got_entry = sgotplt->output_section->vma
   1265     + sgotplt->output_offset
   1266     + got_offset
   1267     - splt->output_section->vma
   1268     - splt->output_offset
   1269     - offset
   1270     - TILEGX_BUNDLE_SIZE_IN_BYTES;
   1271 
   1272   /* Compute the distance to GOTPLT[0].  */
   1273   bfd_signed_vma dist_got0 = dist_got_entry - got_offset;
   1274 
   1275   /* Check whether we can use the short plt entry with 16-bit offset.  */
   1276   bool short_plt_entry =
   1277     (dist_got_entry <= 0x7fff && dist_got0 >= -0x8000);
   1278 
   1279   const tilegx_bundle_bits *plt_entry = (tilegx_bundle_bits *)
   1280     (ABI_64_P (output_bfd) ?
   1281      (short_plt_entry ? tilegx64_short_plt_entry : tilegx64_long_plt_entry) :
   1282      (short_plt_entry ? tilegx32_short_plt_entry : tilegx32_long_plt_entry));
   1283 
   1284   /* Copy the plt entry template.  */
   1285   memcpy (splt->contents + offset, plt_entry, PLT_ENTRY_SIZE);
   1286 
   1287   /* Write the immediate offsets.  */
   1288   pc = (tilegx_bundle_bits *)(splt->contents + offset);
   1289 
   1290   if (short_plt_entry)
   1291     {
   1292       /* { lnk r28 }  */
   1293       pc++;
   1294 
   1295       /* { addli r28, r28, &GOTPLT[MY_GOT_INDEX] ; moveli r27, &GOTPLT[0] }  */
   1296       *pc++ |= create_Imm16_X0 (dist_got_entry)
   1297 	| create_Imm16_X1 (dist_got0);
   1298 
   1299       /* { shl16insli r29, zero, MY_PLT_INDEX ; ld r28, r28 }  */
   1300       *pc++ |= create_Imm16_X0 (plt_index);
   1301     }
   1302   else
   1303     {
   1304       /* { moveli r28, &GOTPLT[MY_GOT_INDEX] ; lnk r26 }  */
   1305       *pc++ |= create_Imm16_X0 (dist_got_entry >> 16);
   1306 
   1307       /* { moveli r27, &GOTPLT[0] ;
   1308 	   shl16insli r28, r28, &GOTPLT[MY_GOT_INDEX] }  */
   1309       *pc++ |= create_Imm16_X0 (dist_got0 >> 16)
   1310 	| create_Imm16_X1 (dist_got_entry);
   1311 
   1312       /* { add r28, r26, r28 ; shl16insli r27, r27, &GOTPLT[0] }  */
   1313       *pc++ |= create_Imm16_X1 (dist_got0);
   1314 
   1315       /* { add r27, r26, r27 ; info 10 ; ld r28, r28 } */
   1316       pc++;
   1317 
   1318       /* { shl16insli r29, zero, MY_GOT_INDEX ; jr r28 } */
   1319       *pc++ |= create_Imm16_X0 (plt_index);
   1320    }
   1321 
   1322   /* Set the relocation offset.  */
   1323   *r_offset = got_offset;
   1324 
   1325   return plt_index;
   1326 }
   1327 
   1328 /* Create an entry in an TILEGX ELF linker hash table.  */
   1329 
   1330 static struct bfd_hash_entry *
   1331 link_hash_newfunc (struct bfd_hash_entry *entry,
   1332 		   struct bfd_hash_table *table, const char *string)
   1333 {
   1334   /* Allocate the structure if it has not already been allocated by a
   1335      subclass.  */
   1336   if (entry == NULL)
   1337     {
   1338       entry =
   1339 	bfd_hash_allocate (table,
   1340 			   sizeof (struct tilegx_elf_link_hash_entry));
   1341       if (entry == NULL)
   1342 	return entry;
   1343     }
   1344 
   1345   /* Call the allocation method of the superclass.  */
   1346   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   1347   if (entry != NULL)
   1348     {
   1349       struct tilegx_elf_link_hash_entry *eh;
   1350 
   1351       eh = (struct tilegx_elf_link_hash_entry *) entry;
   1352       eh->tls_type = GOT_UNKNOWN;
   1353     }
   1354 
   1355   return entry;
   1356 }
   1357 
   1358 /* Create a TILEGX ELF linker hash table.  */
   1359 
   1360 struct bfd_link_hash_table *
   1361 tilegx_elf_link_hash_table_create (bfd *abfd)
   1362 {
   1363   struct tilegx_elf_link_hash_table *ret;
   1364   size_t amt = sizeof (struct tilegx_elf_link_hash_table);
   1365 
   1366   ret = (struct tilegx_elf_link_hash_table *) bfd_zmalloc (amt);
   1367   if (ret == NULL)
   1368     return NULL;
   1369 
   1370 #ifdef BFD64
   1371   if (ABI_64_P (abfd))
   1372     {
   1373       ret->bytes_per_word = 8;
   1374       ret->word_align_power = 3;
   1375       ret->bytes_per_rela = sizeof (Elf64_External_Rela);
   1376       ret->dtpoff_reloc = R_TILEGX_TLS_DTPOFF64;
   1377       ret->dtpmod_reloc = R_TILEGX_TLS_DTPMOD64;
   1378       ret->tpoff_reloc = R_TILEGX_TLS_TPOFF64;
   1379       ret->r_info = tilegx_elf_r_info_64;
   1380       ret->r_symndx = tilegx_elf_r_symndx_64;
   1381       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
   1382       ret->put_word = tilegx_put_word_64;
   1383     }
   1384   else
   1385 #endif
   1386     {
   1387       ret->bytes_per_word = 4;
   1388       ret->word_align_power = 2;
   1389       ret->bytes_per_rela = sizeof (Elf32_External_Rela);
   1390       ret->dtpoff_reloc = R_TILEGX_TLS_DTPOFF32;
   1391       ret->dtpmod_reloc = R_TILEGX_TLS_DTPMOD32;
   1392       ret->tpoff_reloc = R_TILEGX_TLS_TPOFF32;
   1393       ret->r_info = tilegx_elf_r_info_32;
   1394       ret->r_symndx = tilegx_elf_r_symndx_32;
   1395       ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
   1396       ret->put_word = tilegx_put_word_32;
   1397     }
   1398 
   1399   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
   1400 				      sizeof (struct tilegx_elf_link_hash_entry)))
   1401     {
   1402       free (ret);
   1403       return NULL;
   1404     }
   1405 
   1406   return &ret->elf.root;
   1407 }
   1408 
   1409 /* Create the .got section.  */
   1410 
   1411 static bool
   1412 tilegx_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   1413 {
   1414   flagword flags;
   1415   asection *s, *s_got;
   1416   struct elf_link_hash_entry *h;
   1417   elf_backend_data *bed = get_elf_backend_data (abfd);
   1418   struct elf_link_hash_table *htab = elf_hash_table (info);
   1419 
   1420   /* This function may be called more than once.  */
   1421   if (htab->sgot != NULL)
   1422     return true;
   1423 
   1424   flags = bed->dynamic_sec_flags;
   1425 
   1426   s = bfd_make_section_anyway_with_flags (abfd,
   1427 					  (bed->rela_plts_and_copies_p
   1428 					   ? ".rela.got" : ".rel.got"),
   1429 					  (bed->dynamic_sec_flags
   1430 					   | SEC_READONLY));
   1431   if (s == NULL
   1432       || !bfd_set_section_alignment (s, bed->s->log_file_align))
   1433     return false;
   1434   htab->srelgot = s;
   1435 
   1436   s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   1437   if (s == NULL
   1438       || !bfd_set_section_alignment (s, bed->s->log_file_align))
   1439     return false;
   1440   htab->sgot = s;
   1441 
   1442   /* The first bit of the global offset table is the header.  */
   1443   s->size += bed->got_header_size;
   1444 
   1445   if (bed->want_got_plt)
   1446     {
   1447       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
   1448       if (s == NULL
   1449 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
   1450 	return false;
   1451       htab->sgotplt = s;
   1452 
   1453       /* Reserve room for the header.  */
   1454       s->size += GOTPLT_HEADER_SIZE (tilegx_elf_hash_table (info));
   1455     }
   1456 
   1457   if (bed->want_got_sym)
   1458     {
   1459       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
   1460 	 section.  We don't do this in the linker script because we don't want
   1461 	 to define the symbol if we are not creating a global offset
   1462 	 table.  */
   1463       h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
   1464 				       "_GLOBAL_OFFSET_TABLE_");
   1465       elf_hash_table (info)->hgot = h;
   1466       if (h == NULL)
   1467 	return false;
   1468     }
   1469 
   1470   return true;
   1471 }
   1472 
   1473 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
   1474    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
   1475    hash table.  */
   1476 
   1477 bool
   1478 tilegx_elf_create_dynamic_sections (bfd *dynobj,
   1479 				    struct bfd_link_info *info)
   1480 {
   1481   if (!tilegx_elf_create_got_section (dynobj, info))
   1482     return false;
   1483 
   1484   return _bfd_elf_create_dynamic_sections (dynobj, info);
   1485 }
   1486 
   1487 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   1488 
   1489 void
   1490 tilegx_elf_copy_indirect_symbol (struct bfd_link_info *info,
   1491 				 struct elf_link_hash_entry *dir,
   1492 				 struct elf_link_hash_entry *ind)
   1493 {
   1494   struct tilegx_elf_link_hash_entry *edir, *eind;
   1495 
   1496   edir = (struct tilegx_elf_link_hash_entry *) dir;
   1497   eind = (struct tilegx_elf_link_hash_entry *) ind;
   1498 
   1499   if (ind->root.type == bfd_link_hash_indirect
   1500       && dir->got.refcount <= 0)
   1501     {
   1502       edir->tls_type = eind->tls_type;
   1503       eind->tls_type = GOT_UNKNOWN;
   1504     }
   1505   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   1506 }
   1507 
   1508 static int
   1509 tilegx_tls_translate_to_le (int r_type)
   1510 {
   1511   switch (r_type)
   1512     {
   1513     case R_TILEGX_IMM16_X0_HW0_TLS_GD:
   1514     case R_TILEGX_IMM16_X0_HW0_TLS_IE:
   1515       return R_TILEGX_IMM16_X0_HW0_TLS_LE;
   1516 
   1517     case R_TILEGX_IMM16_X1_HW0_TLS_GD:
   1518     case R_TILEGX_IMM16_X1_HW0_TLS_IE:
   1519       return R_TILEGX_IMM16_X1_HW0_TLS_LE;
   1520 
   1521     case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
   1522     case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
   1523       return R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE;
   1524 
   1525     case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
   1526     case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
   1527       return R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE;
   1528 
   1529     case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
   1530     case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
   1531       return R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE;
   1532 
   1533     case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
   1534     case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
   1535       return R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE;
   1536     }
   1537   return r_type;
   1538 }
   1539 
   1540 static int
   1541 tilegx_tls_translate_to_ie (int r_type)
   1542 {
   1543   switch (r_type)
   1544     {
   1545     case R_TILEGX_IMM16_X0_HW0_TLS_GD:
   1546     case R_TILEGX_IMM16_X0_HW0_TLS_IE:
   1547       return R_TILEGX_IMM16_X0_HW0_TLS_IE;
   1548 
   1549     case R_TILEGX_IMM16_X1_HW0_TLS_GD:
   1550     case R_TILEGX_IMM16_X1_HW0_TLS_IE:
   1551       return R_TILEGX_IMM16_X1_HW0_TLS_IE;
   1552 
   1553     case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
   1554     case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
   1555       return R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE;
   1556 
   1557     case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
   1558     case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
   1559       return R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE;
   1560 
   1561     case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
   1562     case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
   1563       return R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE;
   1564 
   1565     case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
   1566     case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
   1567       return R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE;
   1568     }
   1569   return r_type;
   1570 }
   1571 
   1572 static int
   1573 tilegx_elf_tls_transition (struct bfd_link_info *info, int r_type,
   1574 			   int is_local, bool disable_le_transition)
   1575 {
   1576   if (!bfd_link_executable (info))
   1577     return r_type;
   1578 
   1579   if (is_local && !disable_le_transition)
   1580     return tilegx_tls_translate_to_le (r_type);
   1581   else
   1582     return tilegx_tls_translate_to_ie (r_type);
   1583 }
   1584 
   1585 /* Look through the relocs for a section during the first phase, and
   1586    allocate space in the global offset table or procedure linkage
   1587    table.  */
   1588 
   1589 bool
   1590 tilegx_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   1591 			 asection *sec, const Elf_Internal_Rela *relocs)
   1592 {
   1593   struct tilegx_elf_link_hash_table *htab;
   1594   Elf_Internal_Shdr *symtab_hdr;
   1595   struct elf_link_hash_entry **sym_hashes;
   1596   const Elf_Internal_Rela *rel;
   1597   const Elf_Internal_Rela *rel_end;
   1598   asection *sreloc;
   1599   int num_relocs;
   1600   bool has_tls_gd_or_ie = false, has_tls_add = false;
   1601 
   1602   if (bfd_link_relocatable (info))
   1603     return true;
   1604 
   1605   htab = tilegx_elf_hash_table (info);
   1606   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1607   sym_hashes = elf_sym_hashes (abfd);
   1608 
   1609   sreloc = NULL;
   1610 
   1611   num_relocs = sec->reloc_count;
   1612 
   1613   BFD_ASSERT (is_tilegx_elf (abfd) || num_relocs == 0);
   1614 
   1615   if (htab->elf.dynobj == NULL)
   1616     htab->elf.dynobj = abfd;
   1617 
   1618   rel_end = relocs + num_relocs;
   1619 
   1620   /* Check whether to do optimization to transform TLS GD/IE
   1621      referehces to TLS LE.  We disable it if we're linking with old
   1622      TLS code sequences that do not support such optimization.  Old
   1623      TLS code sequences have tls_gd_call/tls_ie_load relocations but
   1624      no tls_add relocations.  */
   1625   for (rel = relocs; rel < rel_end && !has_tls_add; rel++)
   1626     {
   1627       int r_type = TILEGX_ELF_R_TYPE (rel->r_info);
   1628       switch (r_type)
   1629 	{
   1630 	case R_TILEGX_TLS_GD_CALL:
   1631 	case R_TILEGX_TLS_IE_LOAD:
   1632 	  has_tls_gd_or_ie = true;
   1633 	  break;
   1634 	case R_TILEGX_IMM8_X0_TLS_ADD:
   1635 	case R_TILEGX_IMM8_Y0_TLS_ADD:
   1636 	case R_TILEGX_IMM8_X1_TLS_ADD:
   1637 	case R_TILEGX_IMM8_Y1_TLS_ADD:
   1638 	  has_tls_add = true;
   1639 	  break;
   1640 	}
   1641     }
   1642 
   1643   sec->sec_flg0 = (has_tls_gd_or_ie && !has_tls_add);
   1644   htab->disable_le_transition |= sec->sec_flg0;
   1645 
   1646   for (rel = relocs; rel < rel_end; rel++)
   1647     {
   1648       unsigned int r_type;
   1649       unsigned int r_symndx;
   1650       struct elf_link_hash_entry *h;
   1651       int tls_type;
   1652 
   1653       r_symndx = TILEGX_ELF_R_SYMNDX (htab, rel->r_info);
   1654       r_type = TILEGX_ELF_R_TYPE (rel->r_info);
   1655 
   1656       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
   1657 	{
   1658 	  /* xgettext:c-format */
   1659 	  _bfd_error_handler (_("%pB: bad symbol index: %d"),
   1660 			      abfd, r_symndx);
   1661 	  return false;
   1662 	}
   1663 
   1664       if (r_symndx < symtab_hdr->sh_info)
   1665 	h = NULL;
   1666       else
   1667 	{
   1668 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1669 	  while (h->root.type == bfd_link_hash_indirect
   1670 		 || h->root.type == bfd_link_hash_warning)
   1671 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1672 	}
   1673 
   1674       r_type = tilegx_elf_tls_transition (info, r_type, h == NULL,
   1675 					  sec->sec_flg0);
   1676       switch (r_type)
   1677 	{
   1678 	case R_TILEGX_IMM16_X0_HW0_TLS_LE:
   1679 	case R_TILEGX_IMM16_X1_HW0_TLS_LE:
   1680 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
   1681 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
   1682 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
   1683 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
   1684 	  if (!bfd_link_executable (info))
   1685 	    goto r_tilegx_plt32;
   1686 	  break;
   1687 
   1688 	case R_TILEGX_IMM16_X0_HW0_TLS_GD:
   1689 	case R_TILEGX_IMM16_X1_HW0_TLS_GD:
   1690 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
   1691 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
   1692 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
   1693 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
   1694 	  BFD_ASSERT (bfd_link_pic (info));
   1695 	  tls_type = GOT_TLS_GD;
   1696 	  goto have_got_reference;
   1697 
   1698 	case R_TILEGX_IMM16_X0_HW0_TLS_IE:
   1699 	case R_TILEGX_IMM16_X1_HW0_TLS_IE:
   1700 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
   1701 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
   1702 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
   1703 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
   1704 	  tls_type = GOT_TLS_IE;
   1705 	  if (!bfd_link_executable (info))
   1706 	    info->flags |= DF_STATIC_TLS;
   1707 	  goto have_got_reference;
   1708 
   1709 	case R_TILEGX_IMM16_X0_HW0_GOT:
   1710 	case R_TILEGX_IMM16_X1_HW0_GOT:
   1711 	case R_TILEGX_IMM16_X0_HW0_LAST_GOT:
   1712 	case R_TILEGX_IMM16_X1_HW0_LAST_GOT:
   1713 	case R_TILEGX_IMM16_X0_HW1_LAST_GOT:
   1714 	case R_TILEGX_IMM16_X1_HW1_LAST_GOT:
   1715 	  tls_type = GOT_NORMAL;
   1716 	  /* Fall Through */
   1717 
   1718 	have_got_reference:
   1719 	  /* This symbol requires a global offset table entry.  */
   1720 	  {
   1721 	    int old_tls_type;
   1722 
   1723 	    if (h != NULL)
   1724 	      {
   1725 		h->got.refcount += 1;
   1726 		old_tls_type = tilegx_elf_hash_entry(h)->tls_type;
   1727 	      }
   1728 	    else
   1729 	      {
   1730 		bfd_signed_vma *local_got_refcounts;
   1731 
   1732 		/* This is a global offset table entry for a local symbol.  */
   1733 		local_got_refcounts = elf_local_got_refcounts (abfd);
   1734 		if (local_got_refcounts == NULL)
   1735 		  {
   1736 		    bfd_size_type size;
   1737 
   1738 		    size = symtab_hdr->sh_info;
   1739 		    size *= (sizeof (bfd_signed_vma) + sizeof(char));
   1740 		    local_got_refcounts = ((bfd_signed_vma *)
   1741 					   bfd_zalloc (abfd, size));
   1742 		    if (local_got_refcounts == NULL)
   1743 		      return false;
   1744 		    elf_local_got_refcounts (abfd) = local_got_refcounts;
   1745 		    _bfd_tilegx_elf_local_got_tls_type (abfd)
   1746 		      = (char *) (local_got_refcounts + symtab_hdr->sh_info);
   1747 		  }
   1748 		local_got_refcounts[r_symndx] += 1;
   1749 		old_tls_type = _bfd_tilegx_elf_local_got_tls_type (abfd) [r_symndx];
   1750 	      }
   1751 
   1752 	    /* If a TLS symbol is accessed using IE at least once,
   1753 	       there is no point to use dynamic model for it.  */
   1754 	    if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
   1755 		&& (old_tls_type != GOT_TLS_GD
   1756 		    || tls_type != GOT_TLS_IE))
   1757 	      {
   1758 		if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
   1759 		  tls_type = old_tls_type;
   1760 		else
   1761 		  {
   1762 		    _bfd_error_handler
   1763 		      /* xgettext:c-format */
   1764 		      (_("%pB: `%s' accessed both as normal and thread local symbol"),
   1765 		       abfd, h ? h->root.root.string : "<local>");
   1766 		    return false;
   1767 		  }
   1768 	      }
   1769 
   1770 	    if (old_tls_type != tls_type)
   1771 	      {
   1772 		if (h != NULL)
   1773 		  tilegx_elf_hash_entry (h)->tls_type = tls_type;
   1774 		else
   1775 		  _bfd_tilegx_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
   1776 	      }
   1777 	  }
   1778 
   1779 	  if (htab->elf.sgot == NULL)
   1780 	    {
   1781 	      if (!tilegx_elf_create_got_section (htab->elf.dynobj, info))
   1782 		return false;
   1783 	    }
   1784 	  break;
   1785 
   1786 	case R_TILEGX_TLS_GD_CALL:
   1787 	  if (!bfd_link_executable (info))
   1788 	    {
   1789 	      /* These are basically R_TILEGX_JUMPOFF_X1_PLT relocs
   1790 		 against __tls_get_addr.  */
   1791 	      struct bfd_link_hash_entry *bh = NULL;
   1792 	      if (! _bfd_generic_link_add_one_symbol (info, abfd,
   1793 						      "__tls_get_addr", 0,
   1794 						      bfd_und_section_ptr, 0,
   1795 						      NULL, false, false,
   1796 						      &bh))
   1797 		return false;
   1798 	      h = (struct elf_link_hash_entry *) bh;
   1799 	    }
   1800 	  else
   1801 	    break;
   1802 	  /* Fall through */
   1803 
   1804 	case R_TILEGX_JUMPOFF_X1_PLT:
   1805 	case R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
   1806 	case R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
   1807 	case R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
   1808 	case R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
   1809 	case R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
   1810 	case R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
   1811 	case R_TILEGX_IMM16_X0_HW3_PLT_PCREL:
   1812 	case R_TILEGX_IMM16_X1_HW3_PLT_PCREL:
   1813 	case R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
   1814 	case R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
   1815 	case R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
   1816 	case R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
   1817 	case R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
   1818 	case R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
   1819 	  /* This symbol requires a procedure linkage table entry.  We
   1820 	     actually build the entry in adjust_dynamic_symbol,
   1821 	     because this might be a case of linking PIC code without
   1822 	     linking in any dynamic objects, in which case we don't
   1823 	     need to generate a procedure linkage table after all.  */
   1824 
   1825 	  if (h != NULL)
   1826 	    {
   1827 	      h->needs_plt = 1;
   1828 	      h->plt.refcount += 1;
   1829 	    }
   1830 	  break;
   1831 
   1832 	case R_TILEGX_64_PCREL:
   1833 	case R_TILEGX_32_PCREL:
   1834 	case R_TILEGX_16_PCREL:
   1835 	case R_TILEGX_8_PCREL:
   1836 	case R_TILEGX_IMM16_X0_HW0_PCREL:
   1837 	case R_TILEGX_IMM16_X1_HW0_PCREL:
   1838 	case R_TILEGX_IMM16_X0_HW1_PCREL:
   1839 	case R_TILEGX_IMM16_X1_HW1_PCREL:
   1840 	case R_TILEGX_IMM16_X0_HW2_PCREL:
   1841 	case R_TILEGX_IMM16_X1_HW2_PCREL:
   1842 	case R_TILEGX_IMM16_X0_HW3_PCREL:
   1843 	case R_TILEGX_IMM16_X1_HW3_PCREL:
   1844 	case R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
   1845 	case R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
   1846 	case R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
   1847 	case R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
   1848 	case R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
   1849 	case R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
   1850 	  if (h != NULL)
   1851 	    h->non_got_ref = 1;
   1852 
   1853 	  if (h != NULL
   1854 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   1855 	    break;
   1856 	  /* Fall through.  */
   1857 
   1858 	case R_TILEGX_64:
   1859 	case R_TILEGX_32:
   1860 	case R_TILEGX_16:
   1861 	case R_TILEGX_8:
   1862 	case R_TILEGX_HW0:
   1863 	case R_TILEGX_HW1:
   1864 	case R_TILEGX_HW2:
   1865 	case R_TILEGX_HW3:
   1866 	case R_TILEGX_HW0_LAST:
   1867 	case R_TILEGX_HW1_LAST:
   1868 	case R_TILEGX_HW2_LAST:
   1869 	case R_TILEGX_COPY:
   1870 	case R_TILEGX_GLOB_DAT:
   1871 	case R_TILEGX_JMP_SLOT:
   1872 	case R_TILEGX_RELATIVE:
   1873 	case R_TILEGX_BROFF_X1:
   1874 	case R_TILEGX_JUMPOFF_X1:
   1875 	case R_TILEGX_IMM8_X0:
   1876 	case R_TILEGX_IMM8_Y0:
   1877 	case R_TILEGX_IMM8_X1:
   1878 	case R_TILEGX_IMM8_Y1:
   1879 	case R_TILEGX_DEST_IMM8_X1:
   1880 	case R_TILEGX_MT_IMM14_X1:
   1881 	case R_TILEGX_MF_IMM14_X1:
   1882 	case R_TILEGX_MMSTART_X0:
   1883 	case R_TILEGX_MMEND_X0:
   1884 	case R_TILEGX_SHAMT_X0:
   1885 	case R_TILEGX_SHAMT_X1:
   1886 	case R_TILEGX_SHAMT_Y0:
   1887 	case R_TILEGX_SHAMT_Y1:
   1888 	case R_TILEGX_IMM16_X0_HW0:
   1889 	case R_TILEGX_IMM16_X1_HW0:
   1890 	case R_TILEGX_IMM16_X0_HW1:
   1891 	case R_TILEGX_IMM16_X1_HW1:
   1892 	case R_TILEGX_IMM16_X0_HW2:
   1893 	case R_TILEGX_IMM16_X1_HW2:
   1894 	case R_TILEGX_IMM16_X0_HW3:
   1895 	case R_TILEGX_IMM16_X1_HW3:
   1896 	case R_TILEGX_IMM16_X0_HW0_LAST:
   1897 	case R_TILEGX_IMM16_X1_HW0_LAST:
   1898 	case R_TILEGX_IMM16_X0_HW1_LAST:
   1899 	case R_TILEGX_IMM16_X1_HW1_LAST:
   1900 	case R_TILEGX_IMM16_X0_HW2_LAST:
   1901 	case R_TILEGX_IMM16_X1_HW2_LAST:
   1902 	  if (h != NULL)
   1903 	    h->non_got_ref = 1;
   1904 
   1905 	r_tilegx_plt32:
   1906 	  if (h != NULL && !bfd_link_pic (info))
   1907 	    {
   1908 	      /* We may need a .plt entry if the function this reloc
   1909 		 refers to is in a shared lib.  */
   1910 	      h->plt.refcount += 1;
   1911 	    }
   1912 
   1913 	  /* If we are creating a shared library, and this is a reloc
   1914 	     against a global symbol, or a non PC relative reloc
   1915 	     against a local symbol, then we need to copy the reloc
   1916 	     into the shared library.  However, if we are linking with
   1917 	     -Bsymbolic, we do not need to copy a reloc against a
   1918 	     global symbol which is defined in an object we are
   1919 	     including in the link (i.e., DEF_REGULAR is set).  At
   1920 	     this point we have not seen all the input files, so it is
   1921 	     possible that DEF_REGULAR is not set now but will be set
   1922 	     later (it is never cleared).  In case of a weak definition,
   1923 	     DEF_REGULAR may be cleared later by a strong definition in
   1924 	     a shared library.  We account for that possibility below by
   1925 	     storing information in the relocs_copied field of the hash
   1926 	     table entry.  A similar situation occurs when creating
   1927 	     shared libraries and symbol visibility changes render the
   1928 	     symbol local.
   1929 
   1930 	     If on the other hand, we are creating an executable, we
   1931 	     may need to keep relocations for symbols satisfied by a
   1932 	     dynamic library if we manage to avoid copy relocs for the
   1933 	     symbol.  */
   1934 	  if ((bfd_link_pic (info)
   1935 	       && (sec->flags & SEC_ALLOC) != 0
   1936 	       && (! tilegx_elf_howto_table[r_type].pc_relative
   1937 		   || (h != NULL
   1938 		       && (! info->symbolic
   1939 			   || h->root.type == bfd_link_hash_defweak
   1940 			   || !h->def_regular))))
   1941 	      || (!bfd_link_pic (info)
   1942 		  && (sec->flags & SEC_ALLOC) != 0
   1943 		  && h != NULL
   1944 		  && (h->root.type == bfd_link_hash_defweak
   1945 		      || !h->def_regular)))
   1946 	    {
   1947 	      struct elf_dyn_relocs *p;
   1948 	      struct elf_dyn_relocs **head;
   1949 
   1950 	      /* When creating a shared object, we must copy these
   1951 		 relocs into the output file.  We create a reloc
   1952 		 section in dynobj and make room for the reloc.  */
   1953 	      if (sreloc == NULL)
   1954 		{
   1955 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   1956 		    (sec, htab->elf.dynobj, htab->word_align_power, abfd,
   1957 		     /*rela?*/ true);
   1958 
   1959 		  if (sreloc == NULL)
   1960 		    return false;
   1961 		}
   1962 
   1963 	      /* If this is a global symbol, we count the number of
   1964 		 relocations we need for this symbol.  */
   1965 	      if (h != NULL)
   1966 		head = &h->dyn_relocs;
   1967 	      else
   1968 		{
   1969 		  /* Track dynamic relocs needed for local syms too.
   1970 		     We really need local syms available to do this
   1971 		     easily.  Oh well.  */
   1972 
   1973 		  asection *s;
   1974 		  void *vpp;
   1975 		  Elf_Internal_Sym *isym;
   1976 
   1977 		  isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   1978 						abfd, r_symndx);
   1979 		  if (isym == NULL)
   1980 		    return false;
   1981 
   1982 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   1983 		  if (s == NULL)
   1984 		    s = sec;
   1985 
   1986 		  vpp = &elf_section_data (s)->local_dynrel;
   1987 		  head = (struct elf_dyn_relocs **) vpp;
   1988 		}
   1989 
   1990 	      p = *head;
   1991 	      if (p == NULL || p->sec != sec)
   1992 		{
   1993 		  size_t amt = sizeof *p;
   1994 		  p = ((struct elf_dyn_relocs *)
   1995 		       bfd_alloc (htab->elf.dynobj, amt));
   1996 		  if (p == NULL)
   1997 		    return false;
   1998 		  p->next = *head;
   1999 		  *head = p;
   2000 		  p->sec = sec;
   2001 		  p->count = 0;
   2002 		  p->pc_count = 0;
   2003 		}
   2004 
   2005 	      p->count += 1;
   2006 	      if (tilegx_elf_howto_table[r_type].pc_relative)
   2007 		p->pc_count += 1;
   2008 	    }
   2009 
   2010 	  break;
   2011 
   2012 	case R_TILEGX_GNU_VTINHERIT:
   2013 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   2014 	    return false;
   2015 	  break;
   2016 
   2017 	case R_TILEGX_GNU_VTENTRY:
   2018 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   2019 	    return false;
   2020 	  break;
   2021 
   2022 	default:
   2023 	  break;
   2024 	}
   2025     }
   2026 
   2027   return true;
   2028 }
   2029 
   2030 
   2031 asection *
   2033 tilegx_elf_gc_mark_hook (asection *sec,
   2034 			 struct bfd_link_info *info,
   2035 			 struct elf_reloc_cookie *cookie,
   2036 			 struct elf_link_hash_entry *h,
   2037 			 unsigned int symndx)
   2038 {
   2039   if (h != NULL)
   2040     {
   2041       switch (TILEGX_ELF_R_TYPE (cookie->rel->r_info))
   2042 	{
   2043 	case R_TILEGX_GNU_VTINHERIT:
   2044 	case R_TILEGX_GNU_VTENTRY:
   2045 	  return NULL;
   2046 	}
   2047     }
   2048 
   2049   /* FIXME: The test here, in check_relocs and in relocate_section
   2050      dealing with TLS optimization, ought to be !bfd_link_executable (info).  */
   2051   if (bfd_link_pic (info))
   2052     {
   2053       struct bfd_link_hash_entry *bh;
   2054 
   2055       switch (TILEGX_ELF_R_TYPE (cookie->rel->r_info))
   2056 	{
   2057 	case R_TILEGX_TLS_GD_CALL:
   2058 	  /* This reloc implicitly references __tls_get_addr.  We know
   2059 	     another reloc will reference the same symbol as the one
   2060 	     on this reloc, so the real symbol and section will be
   2061 	     gc marked when processing the other reloc.  That lets
   2062 	     us handle __tls_get_addr here.  */
   2063 	  bh = NULL;
   2064 	  if (! _bfd_generic_link_add_one_symbol (info, sec->owner,
   2065 						  "__tls_get_addr", 0,
   2066 						  bfd_und_section_ptr,
   2067 						  0, NULL, false,
   2068 						  false, &bh))
   2069 	    return NULL;
   2070 	  h = (struct elf_link_hash_entry *) bh;
   2071 	  BFD_ASSERT (h != NULL);
   2072 	  h->mark = 1;
   2073 	  if (h->is_weakalias)
   2074 	    weakdef (h)->mark = 1;
   2075 	  symndx = 0;
   2076 	}
   2077     }
   2078 
   2079   return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
   2080 }
   2081 
   2082 /* Adjust a symbol defined by a dynamic object and referenced by a
   2083    regular object.  The current definition is in some section of the
   2084    dynamic object, but we're not including those sections.  We have to
   2085    change the definition to something the rest of the link can
   2086    understand.  */
   2087 
   2088 bool
   2089 tilegx_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   2090 				  struct elf_link_hash_entry *h)
   2091 {
   2092   struct tilegx_elf_link_hash_table *htab;
   2093   bfd *dynobj;
   2094   asection *s, *srel;
   2095 
   2096   htab = tilegx_elf_hash_table (info);
   2097   BFD_ASSERT (htab != NULL);
   2098 
   2099   dynobj = htab->elf.dynobj;
   2100 
   2101   /* Make sure we know what is going on here.  */
   2102   BFD_ASSERT (dynobj != NULL
   2103 	      && (h->needs_plt
   2104 		  || h->is_weakalias
   2105 		  || (h->def_dynamic
   2106 		      && h->ref_regular
   2107 		      && !h->def_regular)));
   2108 
   2109   /* If this is a function, put it in the procedure linkage table.  We
   2110      will fill in the contents of the procedure linkage table later
   2111      (although we could actually do it here). */
   2112   if (h->type == STT_FUNC || h->needs_plt)
   2113     {
   2114       if (h->plt.refcount <= 0
   2115 	  || SYMBOL_CALLS_LOCAL (info, h)
   2116 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2117 	      && h->root.type == bfd_link_hash_undefweak))
   2118 	{
   2119 	  /* This case can occur if we saw a R_TILEGX_JUMPOFF_X1_PLT
   2120 	     reloc in an input file, but the symbol was never referred
   2121 	     to by a dynamic object, or if all references were garbage
   2122 	     collected.  In such a case, we don't actually need to build
   2123 	     a procedure linkage table, and we can just do a
   2124 	     R_TILEGX_JUMPOFF_X1 relocation instead. */
   2125 	  h->plt.offset = (bfd_vma) -1;
   2126 	  h->needs_plt = 0;
   2127 	}
   2128 
   2129       return true;
   2130     }
   2131   else
   2132     h->plt.offset = (bfd_vma) -1;
   2133 
   2134   /* If this is a weak symbol, and there is a real definition, the
   2135      processor independent code will have arranged for us to see the
   2136      real definition first, and we can just use the same value.  */
   2137   if (h->is_weakalias)
   2138     {
   2139       struct elf_link_hash_entry *def = weakdef (h);
   2140       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   2141       h->root.u.def.section = def->root.u.def.section;
   2142       h->root.u.def.value = def->root.u.def.value;
   2143       return true;
   2144     }
   2145 
   2146   /* This is a reference to a symbol defined by a dynamic object which
   2147      is not a function.  */
   2148 
   2149   /* If we are creating a shared library, we must presume that the
   2150      only references to the symbol are via the global offset table.
   2151      For such cases we need not do anything here; the relocations will
   2152      be handled correctly by relocate_section.  */
   2153   if (bfd_link_pic (info))
   2154     return true;
   2155 
   2156   /* If there are no references to this symbol that do not use the
   2157      GOT, we don't need to generate a copy reloc.  */
   2158   if (!h->non_got_ref)
   2159     return true;
   2160 
   2161   /* If -z nocopyreloc was given, we won't generate them either.  */
   2162   if (info->nocopyreloc)
   2163     {
   2164       h->non_got_ref = 0;
   2165       return true;
   2166     }
   2167 
   2168   /* If we don't find any dynamic relocs in read-only sections, then
   2169      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   2170   if (!_bfd_elf_readonly_dynrelocs (h))
   2171     {
   2172       h->non_got_ref = 0;
   2173       return true;
   2174     }
   2175 
   2176   /* We must allocate the symbol in our .dynbss section, which will
   2177      become part of the .bss section of the executable.  There will be
   2178      an entry for this symbol in the .dynsym section.  The dynamic
   2179      object will contain position independent code, so all references
   2180      from the dynamic object to this symbol will go through the global
   2181      offset table.  The dynamic linker will use the .dynsym entry to
   2182      determine the address it must put in the global offset table, so
   2183      both the dynamic object and the regular object will refer to the
   2184      same memory location for the variable.  */
   2185 
   2186   /* We must generate a R_TILEGX_COPY reloc to tell the dynamic linker
   2187      to copy the initial value out of the dynamic object and into the
   2188      runtime process image.  We need to remember the offset into the
   2189      .rel.bss section we are going to use.  */
   2190   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   2191     {
   2192       s = htab->elf.sdynrelro;
   2193       srel = htab->elf.sreldynrelro;
   2194     }
   2195   else
   2196     {
   2197       s = htab->elf.sdynbss;
   2198       srel = htab->elf.srelbss;
   2199     }
   2200   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   2201     {
   2202       srel->size += TILEGX_ELF_RELA_BYTES (htab);
   2203       h->needs_copy = 1;
   2204     }
   2205 
   2206   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   2207 }
   2208 
   2209 /* Allocate space in .plt, .got and associated reloc sections for
   2210    dynamic relocs.  */
   2211 
   2212 static bool
   2213 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   2214 {
   2215   struct bfd_link_info *info;
   2216   struct tilegx_elf_link_hash_table *htab;
   2217   struct elf_dyn_relocs *p;
   2218 
   2219   if (h->root.type == bfd_link_hash_indirect)
   2220     return true;
   2221 
   2222   info = (struct bfd_link_info *) inf;
   2223   htab = tilegx_elf_hash_table (info);
   2224   BFD_ASSERT (htab != NULL);
   2225 
   2226   if (htab->elf.dynamic_sections_created
   2227       && h->plt.refcount > 0)
   2228     {
   2229       /* Make sure this symbol is output as a dynamic symbol.
   2230 	 Undefined weak syms won't yet be marked as dynamic.  */
   2231       if (h->dynindx == -1
   2232 	  && !h->forced_local)
   2233 	{
   2234 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2235 	    return false;
   2236 	}
   2237 
   2238       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
   2239 	{
   2240 	  asection *s = htab->elf.splt;
   2241 
   2242 	  /* Allocate room for the header and tail.  */
   2243 	  if (s->size == 0)
   2244 	    {
   2245 	      s->size = PLT_ENTRY_SIZE;
   2246 	    }
   2247 
   2248 	  h->plt.offset = s->size - PLT_ENTRY_SIZE + PLT_HEADER_SIZE;
   2249 
   2250 	  /* If this symbol is not defined in a regular file, and we are
   2251 	     not generating a shared library, then set the symbol to this
   2252 	     location in the .plt.  This is required to make function
   2253 	     pointers compare as equal between the normal executable and
   2254 	     the shared library.  */
   2255 	  if (! bfd_link_pic (info)
   2256 	      && !h->def_regular)
   2257 	    {
   2258 	      h->root.u.def.section = s;
   2259 	      h->root.u.def.value = h->plt.offset;
   2260 	    }
   2261 
   2262 	  /* Make room for this entry.  */
   2263 	  s->size += PLT_ENTRY_SIZE;
   2264 
   2265 	  /* We also need to make an entry in the .got.plt section.  */
   2266 	  htab->elf.sgotplt->size += GOT_ENTRY_SIZE (htab);
   2267 
   2268 	  /* We also need to make an entry in the .rela.plt section.  */
   2269 	  htab->elf.srelplt->size += TILEGX_ELF_RELA_BYTES (htab);
   2270 	}
   2271       else
   2272 	{
   2273 	  h->plt.offset = (bfd_vma) -1;
   2274 	  h->needs_plt = 0;
   2275 	}
   2276     }
   2277   else
   2278     {
   2279       h->plt.offset = (bfd_vma) -1;
   2280       h->needs_plt = 0;
   2281     }
   2282 
   2283   /* If a TLS_IE symbol is now local to the binary, make it a TLS_LE
   2284      requiring no TLS entry.  */
   2285   if (h->got.refcount > 0
   2286       && !htab->disable_le_transition
   2287       && bfd_link_executable (info)
   2288       && h->dynindx == -1
   2289       && tilegx_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
   2290     h->got.offset = (bfd_vma) -1;
   2291   else if (h->got.refcount > 0)
   2292     {
   2293       asection *s;
   2294       bool dyn;
   2295       int tls_type = tilegx_elf_hash_entry(h)->tls_type;
   2296 
   2297       /* Make sure this symbol is output as a dynamic symbol.
   2298 	 Undefined weak syms won't yet be marked as dynamic.  */
   2299       if (h->dynindx == -1
   2300 	  && !h->forced_local)
   2301 	{
   2302 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2303 	    return false;
   2304 	}
   2305 
   2306       s = htab->elf.sgot;
   2307       h->got.offset = s->size;
   2308       s->size += TILEGX_ELF_WORD_BYTES (htab);
   2309       /* TLS_GD entries need 2 consecutive GOT slots. */
   2310       if (tls_type == GOT_TLS_GD)
   2311 	s->size += TILEGX_ELF_WORD_BYTES (htab);
   2312       dyn = htab->elf.dynamic_sections_created;
   2313       /* TLS_IE needs one dynamic relocation,
   2314 	 TLS_GD needs two if local symbol and two if global.  */
   2315       if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE)
   2316 	htab->elf.srelgot->size += 2 * TILEGX_ELF_RELA_BYTES (htab);
   2317       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   2318 						bfd_link_pic (info),
   2319 						h))
   2320 	htab->elf.srelgot->size += TILEGX_ELF_RELA_BYTES (htab);
   2321     }
   2322   else
   2323     h->got.offset = (bfd_vma) -1;
   2324 
   2325   if (h->dyn_relocs == NULL)
   2326     return true;
   2327 
   2328   /* In the shared -Bsymbolic case, discard space allocated for
   2329      dynamic pc-relative relocs against symbols which turn out to be
   2330      defined in regular objects.  For the normal shared case, discard
   2331      space for pc-relative relocs that have become local due to symbol
   2332      visibility changes.  */
   2333 
   2334   if (bfd_link_pic (info))
   2335     {
   2336       if (SYMBOL_CALLS_LOCAL (info, h))
   2337 	{
   2338 	  struct elf_dyn_relocs **pp;
   2339 
   2340 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   2341 	    {
   2342 	      p->count -= p->pc_count;
   2343 	      p->pc_count = 0;
   2344 	      if (p->count == 0)
   2345 		*pp = p->next;
   2346 	      else
   2347 		pp = &p->next;
   2348 	    }
   2349 	}
   2350 
   2351       /* Also discard relocs on undefined weak syms with non-default
   2352 	 visibility.  */
   2353       if (h->dyn_relocs != NULL
   2354 	  && h->root.type == bfd_link_hash_undefweak)
   2355 	{
   2356 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   2357 	      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   2358 	    h->dyn_relocs = NULL;
   2359 
   2360 	  /* Make sure undefined weak symbols are output as a dynamic
   2361 	     symbol in PIEs.  */
   2362 	  else if (h->dynindx == -1
   2363 		   && !h->forced_local)
   2364 	    {
   2365 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2366 		return false;
   2367 	    }
   2368 	}
   2369     }
   2370   else
   2371     {
   2372       /* For the non-shared case, discard space for relocs against
   2373 	 symbols which turn out to need copy relocs or are not
   2374 	 dynamic.  */
   2375 
   2376       if (!h->non_got_ref
   2377 	  && ((h->def_dynamic
   2378 	       && !h->def_regular)
   2379 	      || (htab->elf.dynamic_sections_created
   2380 		  && (h->root.type == bfd_link_hash_undefweak
   2381 		      || h->root.type == bfd_link_hash_undefined))))
   2382 	{
   2383 	  /* Make sure this symbol is output as a dynamic symbol.
   2384 	     Undefined weak syms won't yet be marked as dynamic.  */
   2385 	  if (h->dynindx == -1
   2386 	      && !h->forced_local)
   2387 	    {
   2388 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2389 		return false;
   2390 	    }
   2391 
   2392 	  /* If that succeeded, we know we'll be keeping all the
   2393 	     relocs.  */
   2394 	  if (h->dynindx != -1)
   2395 	    goto keep;
   2396 	}
   2397 
   2398       h->dyn_relocs = NULL;
   2399 
   2400     keep: ;
   2401     }
   2402 
   2403   /* Finally, allocate space.  */
   2404   for (p = h->dyn_relocs; p != NULL; p = p->next)
   2405     {
   2406       asection *sreloc = elf_section_data (p->sec)->sreloc;
   2407       sreloc->size += p->count * TILEGX_ELF_RELA_BYTES (htab);
   2408     }
   2409 
   2410   return true;
   2411 }
   2412 
   2413 /* Return true if the dynamic symbol for a given section should be
   2414    omitted when creating a shared library.  */
   2415 
   2416 bool
   2417 tilegx_elf_omit_section_dynsym (bfd *output_bfd,
   2418 				struct bfd_link_info *info,
   2419 				asection *p)
   2420 {
   2421   /* We keep the .got section symbol so that explicit relocations
   2422      against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
   2423      can be turned into relocations against the .got symbol.  */
   2424   if (strcmp (p->name, ".got") == 0)
   2425     return false;
   2426 
   2427   return _bfd_elf_omit_section_dynsym_default (output_bfd, info, p);
   2428 }
   2429 
   2430 bool
   2431 tilegx_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   2432 			       struct bfd_link_info *info)
   2433 {
   2434   struct tilegx_elf_link_hash_table *htab;
   2435   bfd *dynobj;
   2436   asection *s;
   2437   bfd *ibfd;
   2438 
   2439   htab = tilegx_elf_hash_table (info);
   2440   BFD_ASSERT (htab != NULL);
   2441   dynobj = htab->elf.dynobj;
   2442   if (dynobj == NULL)
   2443     return true;
   2444 
   2445   if (elf_hash_table (info)->dynamic_sections_created)
   2446     {
   2447       /* Set the contents of the .interp section to the interpreter.  */
   2448       if (bfd_link_executable (info) && !info->nointerp)
   2449 	{
   2450 	  s = htab->elf.interp;
   2451 	  BFD_ASSERT (s != NULL);
   2452 	  s->size = strlen (htab->dynamic_interpreter) + 1;
   2453 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
   2454 	  s->alloced = 1;
   2455 	}
   2456     }
   2457 
   2458   /* Set up .got offsets for local syms, and space for local dynamic
   2459      relocs.  */
   2460   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   2461     {
   2462       bfd_signed_vma *local_got;
   2463       bfd_signed_vma *end_local_got;
   2464       char *local_tls_type;
   2465       bfd_size_type locsymcount;
   2466       Elf_Internal_Shdr *symtab_hdr;
   2467       asection *srel;
   2468 
   2469       if (! is_tilegx_elf (ibfd))
   2470 	continue;
   2471 
   2472       for (s = ibfd->sections; s != NULL; s = s->next)
   2473 	{
   2474 	  struct elf_dyn_relocs *p;
   2475 
   2476 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   2477 	    {
   2478 	      if (!bfd_is_abs_section (p->sec)
   2479 		  && bfd_is_abs_section (p->sec->output_section))
   2480 		{
   2481 		  /* Input section has been discarded, either because
   2482 		     it is a copy of a linkonce section or due to
   2483 		     linker script /DISCARD/, so we'll be discarding
   2484 		     the relocs too.  */
   2485 		}
   2486 	      else if (p->count != 0)
   2487 		{
   2488 		  srel = elf_section_data (p->sec)->sreloc;
   2489 		  srel->size += p->count * TILEGX_ELF_RELA_BYTES (htab);
   2490 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   2491 		    {
   2492 		      info->flags |= DF_TEXTREL;
   2493 
   2494 		      info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
   2495 					      p->sec->owner, p->sec);
   2496 		    }
   2497 		}
   2498 	    }
   2499 	}
   2500 
   2501       local_got = elf_local_got_refcounts (ibfd);
   2502       if (!local_got)
   2503 	continue;
   2504 
   2505       symtab_hdr = &elf_symtab_hdr (ibfd);
   2506       locsymcount = symtab_hdr->sh_info;
   2507       end_local_got = local_got + locsymcount;
   2508       local_tls_type = _bfd_tilegx_elf_local_got_tls_type (ibfd);
   2509       s = htab->elf.sgot;
   2510       srel = htab->elf.srelgot;
   2511       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
   2512 	{
   2513 	  if (*local_got > 0)
   2514 	    {
   2515 	      *local_got = s->size;
   2516 	      s->size += TILEGX_ELF_WORD_BYTES (htab);
   2517 	      if (*local_tls_type == GOT_TLS_GD)
   2518 		s->size += TILEGX_ELF_WORD_BYTES (htab);
   2519 	      if (bfd_link_pic (info)
   2520 		  || *local_tls_type == GOT_TLS_GD
   2521 		  || *local_tls_type == GOT_TLS_IE)
   2522 		srel->size += TILEGX_ELF_RELA_BYTES (htab);
   2523 	    }
   2524 	  else
   2525 	    *local_got = (bfd_vma) -1;
   2526 	}
   2527     }
   2528 
   2529   /* Allocate global sym .plt and .got entries, and space for global
   2530      sym dynamic relocs.  */
   2531   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   2532 
   2533   if (elf_hash_table (info)->dynamic_sections_created)
   2534     {
   2535       /* If the .got section is more than 0x8000 bytes, we add
   2536 	 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
   2537 	 bit relocations have a greater chance of working. */
   2538       if (htab->elf.sgot->size >= 0x8000
   2539 	  && elf_hash_table (info)->hgot->root.u.def.value == 0)
   2540 	elf_hash_table (info)->hgot->root.u.def.value = 0x8000;
   2541     }
   2542 
   2543   if (htab->elf.sgotplt)
   2544     {
   2545       struct elf_link_hash_entry *got;
   2546       got = elf_link_hash_lookup (elf_hash_table (info),
   2547 				  "_GLOBAL_OFFSET_TABLE_",
   2548 				  false, false, false);
   2549 
   2550       /* Don't allocate .got.plt section if there are no GOT nor PLT
   2551 	 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
   2552       if ((got == NULL
   2553 	   || !got->ref_regular_nonweak)
   2554 	  && (htab->elf.sgotplt->size
   2555 	      == (unsigned)GOTPLT_HEADER_SIZE (htab))
   2556 	  && (htab->elf.splt == NULL
   2557 	      || htab->elf.splt->size == 0)
   2558 	  && (htab->elf.sgot == NULL
   2559 	      || (htab->elf.sgot->size
   2560 		  == get_elf_backend_data (output_bfd)->got_header_size)))
   2561 	htab->elf.sgotplt->size = 0;
   2562     }
   2563 
   2564   /* The check_relocs and adjust_dynamic_symbol entry points have
   2565      determined the sizes of the various dynamic sections.  Allocate
   2566      memory for them.  */
   2567   for (s = dynobj->sections; s != NULL; s = s->next)
   2568     {
   2569       if ((s->flags & SEC_LINKER_CREATED) == 0)
   2570 	continue;
   2571 
   2572       if (s == htab->elf.splt
   2573 	  || s == htab->elf.sgot
   2574 	  || s == htab->elf.sgotplt
   2575 	  || s == htab->elf.sdynbss
   2576 	  || s == htab->elf.sdynrelro)
   2577 	{
   2578 	  /* Strip this section if we don't need it; see the
   2579 	     comment below.  */
   2580 	}
   2581       else if (startswith (s->name, ".rela"))
   2582 	{
   2583 	  if (s->size != 0)
   2584 	    {
   2585 	      /* We use the reloc_count field as a counter if we need
   2586 		 to copy relocs into the output file.  */
   2587 	      s->reloc_count = 0;
   2588 	    }
   2589 	}
   2590       else
   2591 	{
   2592 	  /* It's not one of our sections.  */
   2593 	  continue;
   2594 	}
   2595 
   2596       if (s->size == 0)
   2597 	{
   2598 	  /* If we don't need this section, strip it from the
   2599 	     output file.  This is mostly to handle .rela.bss and
   2600 	     .rela.plt.  We must create both sections in
   2601 	     create_dynamic_sections, because they must be created
   2602 	     before the linker maps input sections to output
   2603 	     sections.  The linker does that before
   2604 	     adjust_dynamic_symbol is called, and it is that
   2605 	     function which decides whether anything needs to go
   2606 	     into these sections.  */
   2607 	  s->flags |= SEC_EXCLUDE;
   2608 	  continue;
   2609 	}
   2610 
   2611       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   2612 	continue;
   2613 
   2614       /* Allocate memory for the section contents.  Zero the memory
   2615 	 for the benefit of .rela.plt, which has 4 unused entries
   2616 	 at the beginning, and we don't want garbage.  */
   2617       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   2618       if (s->contents == NULL)
   2619 	return false;
   2620       s->alloced = 1;
   2621     }
   2622 
   2623   return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
   2624 }
   2625 
   2626 /* Return the base VMA address which should be subtracted from real addresses
   2628    when resolving @dtpoff relocation.
   2629    This is PT_TLS segment p_vaddr.  */
   2630 
   2631 static bfd_vma
   2632 dtpoff_base (struct bfd_link_info *info)
   2633 {
   2634   /* If tls_sec is NULL, we should have signalled an error already.  */
   2635   if (elf_hash_table (info)->tls_sec == NULL)
   2636     return 0;
   2637   return elf_hash_table (info)->tls_sec->vma;
   2638 }
   2639 
   2640 /* Return the relocation value for @tpoff relocation. */
   2641 
   2642 static bfd_vma
   2643 tpoff (struct bfd_link_info *info, bfd_vma address)
   2644 {
   2645   struct elf_link_hash_table *htab = elf_hash_table (info);
   2646 
   2647   /* If tls_sec is NULL, we should have signalled an error already.  */
   2648   if (htab->tls_sec == NULL)
   2649     return 0;
   2650 
   2651   return (address - htab->tls_sec->vma);
   2652 }
   2653 
   2654 /* Copy SIZE bits from FROM to TO at address ADDR.  */
   2655 
   2656 static void
   2657 tilegx_copy_bits (bfd_byte *addr, int from, int to, int size)
   2658 {
   2659   int i;
   2660   for (i = 0; i < size; i++)
   2661     {
   2662       int from_byte = (from + i) / 8;
   2663       int from_bit = (from + i) % 8;
   2664       int to_byte = (to + i) / 8;
   2665       int to_bit = (to + i) % 8;
   2666       bfd_byte to_mask = 1 << to_bit;
   2667       addr[to_byte] = (addr[to_byte] & ~to_mask)
   2668 	| ((addr[from_byte] >> from_bit << to_bit) & to_mask);
   2669     }
   2670 }
   2671 
   2672 /* Replace the MASK bits in ADDR with those in INSN, for the next
   2673    TILEGX_BUNDLE_SIZE_IN_BYTES bytes.  */
   2674 
   2675 static void
   2676 tilegx_replace_insn (bfd_byte *addr, const bfd_byte *mask,
   2677 		     const bfd_byte *insn)
   2678 {
   2679   int i;
   2680   for (i = 0; i < TILEGX_BUNDLE_SIZE_IN_BYTES; i++)
   2681     {
   2682       addr[i] = (addr[i] & ~mask[i]) | (insn[i] & mask[i]);
   2683     }
   2684 }
   2685 
   2686 /* Mask to extract the bits corresponding to an instruction in a
   2687    specific pipe of a bundle.  */
   2688 static const bfd_byte insn_mask_X1[] = {
   2689   0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0x3f
   2690 };
   2691 
   2692 /* Mask to extract the bits corresponding to an instruction in a
   2693    specific pipe of a bundle, minus the destination operand and the
   2694    first source operand.  */
   2695 static const bfd_byte insn_mask_X0_no_dest_no_srca[] = {
   2696   0x00, 0xf0, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00
   2697 };
   2698 
   2699 static const bfd_byte insn_mask_X1_no_dest_no_srca[] = {
   2700   0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x3f
   2701 };
   2702 
   2703 static const bfd_byte insn_mask_Y0_no_dest_no_srca[] = {
   2704   0x00, 0xf0, 0x0f, 0x78, 0x00, 0x00, 0x00, 0x00
   2705 };
   2706 static const bfd_byte insn_mask_Y1_no_dest_no_srca[] = {
   2707   0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0x3c
   2708 };
   2709 
   2710 /* Mask to extract the bits corresponding to an instruction in a
   2711    specific pipe of a bundle, minus the register operands.  */
   2712 static const bfd_byte insn_mask_X0_no_operand[] = {
   2713   0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00
   2714 };
   2715 
   2716 static const bfd_byte insn_mask_X1_no_operand[] = {
   2717   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3f
   2718 };
   2719 
   2720 static const bfd_byte insn_mask_Y0_no_operand[] = {
   2721   0x00, 0x00, 0x0c, 0x78, 0x00, 0x00, 0x00, 0x00
   2722 };
   2723 
   2724 static const bfd_byte insn_mask_Y1_no_operand[] = {
   2725   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x3c
   2726 };
   2727 
   2728 /* Various instructions synthesized to support tls references.  */
   2729 
   2730 /* ld r0, r0 in the X1 pipe, used for tls ie.  */
   2731 static const bfd_byte insn_tls_ie_ld_X1[] = {
   2732   0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x6a, 0x28
   2733 };
   2734 
   2735 /* ld4s r0, r0 in the X1 pipe, used for tls ie.  */
   2736 static const bfd_byte insn_tls_ie_ld4s_X1[] = {
   2737   0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x6a, 0x28
   2738 };
   2739 
   2740 /* add r0, r0, tp in various pipes, used for tls ie.  */
   2741 static const bfd_byte insn_tls_ie_add_X0X1[] = {
   2742   0x00, 0x50, 0x0f, 0x50, 0x00, 0xa8, 0x07, 0x28
   2743 };
   2744 static const bfd_byte insn_tls_ie_add_Y0Y1[] = {
   2745   0x00, 0x50, 0x27, 0x2c, 0x00, 0xa8, 0x13, 0x9a
   2746 };
   2747 
   2748 /* addx r0, r0, tp in various pipes, used for tls ie.  */
   2749 static const bfd_byte insn_tls_ie_addx_X0X1[] = {
   2750   0x00, 0x50, 0x0b, 0x50, 0x00, 0xa8, 0x05, 0x28
   2751 };
   2752 static const bfd_byte insn_tls_ie_addx_Y0Y1[] = {
   2753   0x00, 0x50, 0x03, 0x2c, 0x00, 0xa8, 0x01, 0x9a
   2754 };
   2755 
   2756 /* move r0, r0 in various pipes, used for tls gd.  */
   2757 static const bfd_byte insn_tls_gd_add_X0X1[] = {
   2758   0x00, 0xf0, 0x07, 0x51, 0x00, 0xf8, 0x3b, 0x28
   2759 };
   2760 static const bfd_byte insn_tls_gd_add_Y0Y1[] = {
   2761   0x00, 0xf0, 0x0b, 0x54, 0x00, 0xf8, 0x05, 0xae
   2762 };
   2763 
   2764 static const bfd_byte *insn_move_X0X1 = insn_tls_gd_add_X0X1;
   2765 static const bfd_byte *insn_move_Y0Y1 = insn_tls_gd_add_Y0Y1;
   2766 
   2767 static const bfd_byte *insn_add_X0X1 = insn_tls_ie_add_X0X1;
   2768 static const bfd_byte *insn_add_Y0Y1 = insn_tls_ie_add_Y0Y1;
   2769 
   2770 static const bfd_byte *insn_addx_X0X1 = insn_tls_ie_addx_X0X1;
   2771 static const bfd_byte *insn_addx_Y0Y1 = insn_tls_ie_addx_Y0Y1;
   2772 
   2773 /* Relocate an TILEGX ELF section.
   2774 
   2775    The RELOCATE_SECTION function is called by the new ELF backend linker
   2776    to handle the relocations for a section.
   2777 
   2778    The relocs are always passed as Rela structures.
   2779 
   2780    This function is responsible for adjusting the section contents as
   2781    necessary, and (if generating a relocatable output file) adjusting
   2782    the reloc addend as necessary.
   2783 
   2784    This function does not have to worry about setting the reloc
   2785    address or the reloc symbol index.
   2786 
   2787    LOCAL_SYMS is a pointer to the swapped in local symbols.
   2788 
   2789    LOCAL_SECTIONS is an array giving the section in the input file
   2790    corresponding to the st_shndx field of each local symbol.
   2791 
   2792    The global hash table entry for the global symbols can be found
   2793    via elf_sym_hashes (input_bfd).
   2794 
   2795    When generating relocatable output, this function must handle
   2796    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   2797    going to be the section symbol corresponding to the output
   2798    section, which means that the addend must be adjusted
   2799    accordingly.  */
   2800 
   2801 int
   2802 tilegx_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   2803 			     bfd *input_bfd, asection *input_section,
   2804 			     bfd_byte *contents, Elf_Internal_Rela *relocs,
   2805 			     Elf_Internal_Sym *local_syms,
   2806 			     asection **local_sections)
   2807 {
   2808   struct tilegx_elf_link_hash_table *htab;
   2809   Elf_Internal_Shdr *symtab_hdr;
   2810   struct elf_link_hash_entry **sym_hashes;
   2811   bfd_vma *local_got_offsets;
   2812   bfd_vma got_base;
   2813   asection *sreloc;
   2814   Elf_Internal_Rela *rel;
   2815   Elf_Internal_Rela *relend;
   2816   int num_relocs;
   2817 
   2818   htab = tilegx_elf_hash_table (info);
   2819   BFD_ASSERT (htab != NULL);
   2820   symtab_hdr = &elf_symtab_hdr (input_bfd);
   2821   sym_hashes = elf_sym_hashes (input_bfd);
   2822   local_got_offsets = elf_local_got_offsets (input_bfd);
   2823 
   2824   if (elf_hash_table (info)->hgot == NULL)
   2825     got_base = 0;
   2826   else
   2827     got_base = elf_hash_table (info)->hgot->root.u.def.value;
   2828 
   2829   sreloc = elf_section_data (input_section)->sreloc;
   2830 
   2831   rel = relocs;
   2832   num_relocs = input_section->reloc_count;
   2833   relend = relocs + num_relocs;
   2834   for (; rel < relend; rel++)
   2835     {
   2836       int r_type, tls_type;
   2837       bool is_tls_iele, is_tls_le;
   2838       reloc_howto_type *howto;
   2839       unsigned long r_symndx;
   2840       struct elf_link_hash_entry *h;
   2841       Elf_Internal_Sym *sym;
   2842       tilegx_create_func create_func;
   2843       asection *sec;
   2844       bfd_vma relocation;
   2845       bfd_reloc_status_type r;
   2846       const char *name;
   2847       bfd_vma off;
   2848       bool is_plt = false;
   2849       bool resolved_to_zero;
   2850       bool unresolved_reloc;
   2851 
   2852       r_type = TILEGX_ELF_R_TYPE (rel->r_info);
   2853       if (r_type == R_TILEGX_GNU_VTINHERIT
   2854 	  || r_type == R_TILEGX_GNU_VTENTRY)
   2855 	continue;
   2856 
   2857       if ((unsigned int)r_type >= ARRAY_SIZE (tilegx_elf_howto_table))
   2858 	return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
   2859 
   2860       howto = tilegx_elf_howto_table + r_type;
   2861 
   2862       /* This is a final link.  */
   2863       r_symndx = TILEGX_ELF_R_SYMNDX (htab, rel->r_info);
   2864       h = NULL;
   2865       sym = NULL;
   2866       sec = NULL;
   2867       unresolved_reloc = false;
   2868       if (r_symndx < symtab_hdr->sh_info)
   2869 	{
   2870 	  sym = local_syms + r_symndx;
   2871 	  sec = local_sections[r_symndx];
   2872 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   2873 	}
   2874       else
   2875 	{
   2876 	  bool warned ATTRIBUTE_UNUSED;
   2877 	  bool ignored ATTRIBUTE_UNUSED;
   2878 
   2879 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   2880 				   r_symndx, symtab_hdr, sym_hashes,
   2881 				   h, sec, relocation,
   2882 				   unresolved_reloc, warned, ignored);
   2883 	  if (warned)
   2884 	    {
   2885 	      /* To avoid generating warning messages about truncated
   2886 		 relocations, set the relocation's address to be the same as
   2887 		 the start of this section.  */
   2888 	      if (input_section->output_section != NULL)
   2889 		relocation = input_section->output_section->vma;
   2890 	      else
   2891 		relocation = 0;
   2892 	    }
   2893 	}
   2894 
   2895       if (sec != NULL && discarded_section (sec))
   2896 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   2897 					 rel, 1, relend, R_TILEGX_NONE,
   2898 					 howto, 0, contents);
   2899 
   2900       if (bfd_link_relocatable (info))
   2901 	continue;
   2902 
   2903       if (h != NULL)
   2904 	name = h->root.root.string;
   2905       else
   2906 	{
   2907 	  name = (bfd_elf_string_from_elf_section
   2908 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   2909 	  if (name == NULL || *name == '\0')
   2910 	    name = bfd_section_name (sec);
   2911 	}
   2912 
   2913       switch (r_type)
   2914 	{
   2915 	case R_TILEGX_TLS_GD_CALL:
   2916 	case R_TILEGX_IMM8_X0_TLS_GD_ADD:
   2917 	case R_TILEGX_IMM8_Y0_TLS_GD_ADD:
   2918 	case R_TILEGX_IMM8_X1_TLS_GD_ADD:
   2919 	case R_TILEGX_IMM8_Y1_TLS_GD_ADD:
   2920 	case R_TILEGX_IMM8_X0_TLS_ADD:
   2921 	case R_TILEGX_IMM8_Y0_TLS_ADD:
   2922 	case R_TILEGX_IMM8_X1_TLS_ADD:
   2923 	case R_TILEGX_IMM8_Y1_TLS_ADD:
   2924 	  tls_type = GOT_UNKNOWN;
   2925 	  if (h == NULL && local_got_offsets)
   2926 	    tls_type =
   2927 	      _bfd_tilegx_elf_local_got_tls_type (input_bfd) [r_symndx];
   2928 	  else if (h != NULL)
   2929 	    tls_type = tilegx_elf_hash_entry(h)->tls_type;
   2930 
   2931 	  is_tls_iele = (bfd_link_executable (info) || tls_type == GOT_TLS_IE);
   2932 	  is_tls_le = is_tls_iele && (!input_section->sec_flg0
   2933 				      && bfd_link_executable (info)
   2934 				      && (h == NULL || h->dynindx == -1));
   2935 
   2936 	  if (r_type == R_TILEGX_TLS_GD_CALL)
   2937 	    {
   2938 	      if (is_tls_le)
   2939 		{
   2940 		  /* GD -> LE */
   2941 		  tilegx_replace_insn (contents + rel->r_offset,
   2942 				       insn_mask_X1, insn_move_X0X1);
   2943 		  continue;
   2944 		}
   2945 	      else if (is_tls_iele)
   2946 		{
   2947 		  /* GD -> IE */
   2948 		  if (ABI_64_P (output_bfd))
   2949 		    tilegx_replace_insn (contents + rel->r_offset,
   2950 					 insn_mask_X1, insn_tls_ie_ld_X1);
   2951 		  else
   2952 		    tilegx_replace_insn (contents + rel->r_offset,
   2953 					 insn_mask_X1, insn_tls_ie_ld4s_X1);
   2954 		  continue;
   2955 		}
   2956 
   2957 	      /* GD -> GD */
   2958 	      h = (struct elf_link_hash_entry *)
   2959 		bfd_link_hash_lookup (info->hash, "__tls_get_addr", false,
   2960 				      false, true);
   2961 	      BFD_ASSERT (h != NULL);
   2962 	      r_type = R_TILEGX_JUMPOFF_X1_PLT;
   2963 	      howto = tilegx_elf_howto_table + r_type;
   2964 	    }
   2965 	  else if (r_type == R_TILEGX_IMM8_X0_TLS_ADD
   2966 		   || r_type ==  R_TILEGX_IMM8_X1_TLS_ADD
   2967 		   || r_type ==  R_TILEGX_IMM8_Y0_TLS_ADD
   2968 		   || r_type ==  R_TILEGX_IMM8_Y1_TLS_ADD)
   2969 	    {
   2970 	      bool is_pipe0 =
   2971 		(r_type == R_TILEGX_IMM8_X0_TLS_ADD
   2972 		 || r_type ==  R_TILEGX_IMM8_Y0_TLS_ADD);
   2973 	      bool is_X0X1 =
   2974 		(r_type == R_TILEGX_IMM8_X0_TLS_ADD
   2975 		 || r_type ==  R_TILEGX_IMM8_X1_TLS_ADD);
   2976 	      int dest_begin = is_pipe0 ? 0 : 31;
   2977 	      int src_begin;
   2978 	      const bfd_byte *insn;
   2979 	      const bfd_byte *mask = NULL;
   2980 
   2981 	      if (is_tls_le)
   2982 		{
   2983 		  /* 1. copy dest operand into the first source operand.
   2984 		     2. change the opcode to "move".  */
   2985 		  src_begin = is_pipe0 ? 6 : 37;
   2986 		  insn = is_X0X1 ? insn_move_X0X1 : insn_move_Y0Y1;
   2987 
   2988 		  switch (r_type)
   2989 		    {
   2990 		    case R_TILEGX_IMM8_X0_TLS_ADD:
   2991 		      mask = insn_mask_X0_no_dest_no_srca;
   2992 		      break;
   2993 		    case R_TILEGX_IMM8_X1_TLS_ADD:
   2994 		      mask = insn_mask_X1_no_dest_no_srca;
   2995 		      break;
   2996 		    case R_TILEGX_IMM8_Y0_TLS_ADD:
   2997 		      mask = insn_mask_Y0_no_dest_no_srca;
   2998 		      break;
   2999 		    case R_TILEGX_IMM8_Y1_TLS_ADD:
   3000 		      mask = insn_mask_Y1_no_dest_no_srca;
   3001 		      break;
   3002 		    }
   3003 		}
   3004 	      else
   3005 		{
   3006 		  /* 1. copy dest operand into the second source operand.
   3007 		     2. change the opcode to "add".  */
   3008 		  src_begin = is_pipe0 ? 12 : 43;
   3009 		  if (ABI_64_P (output_bfd))
   3010 		    insn = is_X0X1 ? insn_add_X0X1 : insn_add_Y0Y1;
   3011 		  else
   3012 		    insn = is_X0X1 ? insn_addx_X0X1 : insn_addx_Y0Y1;
   3013 
   3014 		  switch (r_type)
   3015 		    {
   3016 		    case R_TILEGX_IMM8_X0_TLS_ADD:
   3017 		      mask = insn_mask_X0_no_operand;
   3018 		      break;
   3019 		    case R_TILEGX_IMM8_X1_TLS_ADD:
   3020 		      mask = insn_mask_X1_no_operand;
   3021 		      break;
   3022 		    case R_TILEGX_IMM8_Y0_TLS_ADD:
   3023 		      mask = insn_mask_Y0_no_operand;
   3024 		      break;
   3025 		    case R_TILEGX_IMM8_Y1_TLS_ADD:
   3026 		      mask = insn_mask_Y1_no_operand;
   3027 		      break;
   3028 		    }
   3029 		}
   3030 
   3031 	      tilegx_copy_bits (contents + rel->r_offset, dest_begin,
   3032 				src_begin, 6);
   3033 	      tilegx_replace_insn (contents  + rel->r_offset, mask, insn);
   3034 
   3035 	      continue;
   3036 	    }
   3037 	  else
   3038 	    {
   3039 	      const bfd_byte *mask = NULL;
   3040 	      const bfd_byte *add_insn = NULL;
   3041 	      bool is_64bit = ABI_64_P (output_bfd);
   3042 
   3043 	      switch (r_type)
   3044 		{
   3045 		case R_TILEGX_IMM8_X0_TLS_GD_ADD:
   3046 		  add_insn = is_tls_iele
   3047 		    ? (is_64bit ? insn_tls_ie_add_X0X1 : insn_tls_ie_addx_X0X1)
   3048 		    : insn_tls_gd_add_X0X1;
   3049 		  mask = insn_mask_X0_no_dest_no_srca;
   3050 		  break;
   3051 		case R_TILEGX_IMM8_X1_TLS_GD_ADD:
   3052 		  add_insn = is_tls_iele
   3053 		    ? (is_64bit ? insn_tls_ie_add_X0X1 : insn_tls_ie_addx_X0X1)
   3054 		    : insn_tls_gd_add_X0X1;
   3055 		  mask = insn_mask_X1_no_dest_no_srca;
   3056 		  break;
   3057 		case R_TILEGX_IMM8_Y0_TLS_GD_ADD:
   3058 		  add_insn = is_tls_iele
   3059 		    ? (is_64bit ? insn_tls_ie_add_Y0Y1 : insn_tls_ie_addx_Y0Y1)
   3060 		    : insn_tls_gd_add_Y0Y1;
   3061 		  mask = insn_mask_Y0_no_dest_no_srca;
   3062 		  break;
   3063 		case R_TILEGX_IMM8_Y1_TLS_GD_ADD:
   3064 		  add_insn = is_tls_iele
   3065 		    ? (is_64bit ? insn_tls_ie_add_Y0Y1 : insn_tls_ie_addx_Y0Y1)
   3066 		    : insn_tls_gd_add_Y0Y1;
   3067 		  mask = insn_mask_Y1_no_dest_no_srca;
   3068 		  break;
   3069 		}
   3070 
   3071 	      tilegx_replace_insn (contents + rel->r_offset, mask, add_insn);
   3072 
   3073 	      continue;
   3074 	    }
   3075 	  break;
   3076 	case R_TILEGX_TLS_IE_LOAD:
   3077 	  if (!input_section->sec_flg0
   3078 	      && bfd_link_executable (info)
   3079 	      && (h == NULL || h->dynindx == -1))
   3080 	    {
   3081 	      /* IE -> LE */
   3082 	      tilegx_replace_insn (contents + rel->r_offset,
   3083 				   insn_mask_X1_no_dest_no_srca,
   3084 				   insn_move_X0X1);
   3085 	    }
   3086 	  else
   3087 	    {
   3088 	      /* IE -> IE */
   3089 	      if (ABI_64_P (output_bfd))
   3090 		tilegx_replace_insn (contents + rel->r_offset,
   3091 				     insn_mask_X1_no_dest_no_srca,
   3092 				     insn_tls_ie_ld_X1);
   3093 	      else
   3094 		tilegx_replace_insn (contents + rel->r_offset,
   3095 				     insn_mask_X1_no_dest_no_srca,
   3096 				     insn_tls_ie_ld4s_X1);
   3097 	    }
   3098 	  continue;
   3099 	  break;
   3100 	default:
   3101 	  break;
   3102 	}
   3103 
   3104       resolved_to_zero = (h != NULL
   3105 			  && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   3106 
   3107       switch (r_type)
   3108 	{
   3109 	case R_TILEGX_IMM16_X0_HW0_GOT:
   3110 	case R_TILEGX_IMM16_X1_HW0_GOT:
   3111 	case R_TILEGX_IMM16_X0_HW0_LAST_GOT:
   3112 	case R_TILEGX_IMM16_X1_HW0_LAST_GOT:
   3113 	case R_TILEGX_IMM16_X0_HW1_LAST_GOT:
   3114 	case R_TILEGX_IMM16_X1_HW1_LAST_GOT:
   3115 	  /* Relocation is to the entry for this symbol in the global
   3116 	     offset table.  */
   3117 	  if (htab->elf.sgot == NULL)
   3118 	    abort ();
   3119 
   3120 	  if (h != NULL)
   3121 	    {
   3122 	      bool dyn;
   3123 
   3124 	      off = h->got.offset;
   3125 	      BFD_ASSERT (off != (bfd_vma) -1);
   3126 	      dyn = elf_hash_table (info)->dynamic_sections_created;
   3127 
   3128 	      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   3129 						     bfd_link_pic (info),
   3130 						     h)
   3131 		  || (bfd_link_pic (info)
   3132 		      && SYMBOL_REFERENCES_LOCAL (info, h)))
   3133 		{
   3134 		  /* This is actually a static link, or it is a
   3135 		     -Bsymbolic link and the symbol is defined
   3136 		     locally, or the symbol was forced to be local
   3137 		     because of a version file.  We must initialize
   3138 		     this entry in the global offset table.  Since the
   3139 		     offset must always be a multiple
   3140 		     of 8 for 64-bit, we use the least significant bit
   3141 		     to record whether we have initialized it already.
   3142 
   3143 		     When doing a dynamic link, we create a .rela.got
   3144 		     relocation entry to initialize the value.  This
   3145 		     is done in the finish_dynamic_symbol routine.  */
   3146 		  if ((off & 1) != 0)
   3147 		    off &= ~1;
   3148 		  else
   3149 		    {
   3150 		      TILEGX_ELF_PUT_WORD (htab, output_bfd, relocation,
   3151 					   htab->elf.sgot->contents + off);
   3152 		      h->got.offset |= 1;
   3153 		    }
   3154 		}
   3155 	      else
   3156 		unresolved_reloc = false;
   3157 	    }
   3158 	  else
   3159 	    {
   3160 	      BFD_ASSERT (local_got_offsets != NULL
   3161 			  && local_got_offsets[r_symndx] != (bfd_vma) -1);
   3162 
   3163 	      off = local_got_offsets[r_symndx];
   3164 
   3165 	      /* The offset must always be a multiple of 8 on 64-bit.
   3166 		 We use the least significant bit to record
   3167 		 whether we have already processed this entry.  */
   3168 	      if ((off & 1) != 0)
   3169 		off &= ~1;
   3170 	      else
   3171 		{
   3172 		  if (bfd_link_pic (info))
   3173 		    {
   3174 		      asection *s;
   3175 		      Elf_Internal_Rela outrel;
   3176 
   3177 		      /* We need to generate a R_TILEGX_RELATIVE reloc
   3178 			 for the dynamic linker.  */
   3179 		      s = htab->elf.srelgot;
   3180 		      BFD_ASSERT (s != NULL);
   3181 
   3182 		      outrel.r_offset = (htab->elf.sgot->output_section->vma
   3183 					 + htab->elf.sgot->output_offset
   3184 					 + off);
   3185 		      outrel.r_info =
   3186 			TILEGX_ELF_R_INFO (htab, NULL, 0, R_TILEGX_RELATIVE);
   3187 		      outrel.r_addend = relocation;
   3188 		      relocation = 0;
   3189 		      tilegx_elf_append_rela (output_bfd, s, &outrel);
   3190 		    }
   3191 
   3192 		  TILEGX_ELF_PUT_WORD (htab, output_bfd, relocation,
   3193 				       htab->elf.sgot->contents + off);
   3194 		  local_got_offsets[r_symndx] |= 1;
   3195 		}
   3196 	    }
   3197 	  relocation = off - got_base;
   3198 	  break;
   3199 
   3200 	case R_TILEGX_JUMPOFF_X1_PLT:
   3201 	case R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
   3202 	case R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
   3203 	case R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
   3204 	case R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
   3205 	case R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
   3206 	case R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
   3207 	case R_TILEGX_IMM16_X0_HW3_PLT_PCREL:
   3208 	case R_TILEGX_IMM16_X1_HW3_PLT_PCREL:
   3209 	case R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
   3210 	case R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
   3211 	case R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
   3212 	case R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
   3213 	case R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
   3214 	case R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
   3215 	  /* Relocation is to the entry for this symbol in the
   3216 	     procedure linkage table.  */
   3217 	  BFD_ASSERT (h != NULL);
   3218 
   3219 	  if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
   3220 	    {
   3221 	      /* We didn't make a PLT entry for this symbol.  This
   3222 		 happens when statically linking PIC code, or when
   3223 		 using -Bsymbolic.  */
   3224 	      break;
   3225 	    }
   3226 
   3227 	  relocation = (htab->elf.splt->output_section->vma
   3228 			+ htab->elf.splt->output_offset
   3229 			+ h->plt.offset);
   3230 	  unresolved_reloc = false;
   3231 	  break;
   3232 
   3233 	case R_TILEGX_64_PCREL:
   3234 	case R_TILEGX_32_PCREL:
   3235 	case R_TILEGX_16_PCREL:
   3236 	case R_TILEGX_8_PCREL:
   3237 	case R_TILEGX_IMM16_X0_HW0_PCREL:
   3238 	case R_TILEGX_IMM16_X1_HW0_PCREL:
   3239 	case R_TILEGX_IMM16_X0_HW1_PCREL:
   3240 	case R_TILEGX_IMM16_X1_HW1_PCREL:
   3241 	case R_TILEGX_IMM16_X0_HW2_PCREL:
   3242 	case R_TILEGX_IMM16_X1_HW2_PCREL:
   3243 	case R_TILEGX_IMM16_X0_HW3_PCREL:
   3244 	case R_TILEGX_IMM16_X1_HW3_PCREL:
   3245 	case R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
   3246 	case R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
   3247 	case R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
   3248 	case R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
   3249 	case R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
   3250 	case R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
   3251 	  if (h != NULL
   3252 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   3253 	    break;
   3254 	  /* Fall through.  */
   3255 	case R_TILEGX_64:
   3256 	case R_TILEGX_32:
   3257 	case R_TILEGX_16:
   3258 	case R_TILEGX_8:
   3259 	case R_TILEGX_HW0:
   3260 	case R_TILEGX_HW1:
   3261 	case R_TILEGX_HW2:
   3262 	case R_TILEGX_HW3:
   3263 	case R_TILEGX_HW0_LAST:
   3264 	case R_TILEGX_HW1_LAST:
   3265 	case R_TILEGX_HW2_LAST:
   3266 	case R_TILEGX_COPY:
   3267 	case R_TILEGX_GLOB_DAT:
   3268 	case R_TILEGX_JMP_SLOT:
   3269 	case R_TILEGX_RELATIVE:
   3270 	case R_TILEGX_BROFF_X1:
   3271 	case R_TILEGX_JUMPOFF_X1:
   3272 	case R_TILEGX_IMM8_X0:
   3273 	case R_TILEGX_IMM8_Y0:
   3274 	case R_TILEGX_IMM8_X1:
   3275 	case R_TILEGX_IMM8_Y1:
   3276 	case R_TILEGX_DEST_IMM8_X1:
   3277 	case R_TILEGX_MT_IMM14_X1:
   3278 	case R_TILEGX_MF_IMM14_X1:
   3279 	case R_TILEGX_MMSTART_X0:
   3280 	case R_TILEGX_MMEND_X0:
   3281 	case R_TILEGX_SHAMT_X0:
   3282 	case R_TILEGX_SHAMT_X1:
   3283 	case R_TILEGX_SHAMT_Y0:
   3284 	case R_TILEGX_SHAMT_Y1:
   3285 	case R_TILEGX_IMM16_X0_HW0:
   3286 	case R_TILEGX_IMM16_X1_HW0:
   3287 	case R_TILEGX_IMM16_X0_HW1:
   3288 	case R_TILEGX_IMM16_X1_HW1:
   3289 	case R_TILEGX_IMM16_X0_HW2:
   3290 	case R_TILEGX_IMM16_X1_HW2:
   3291 	case R_TILEGX_IMM16_X0_HW3:
   3292 	case R_TILEGX_IMM16_X1_HW3:
   3293 	case R_TILEGX_IMM16_X0_HW0_LAST:
   3294 	case R_TILEGX_IMM16_X1_HW0_LAST:
   3295 	case R_TILEGX_IMM16_X0_HW1_LAST:
   3296 	case R_TILEGX_IMM16_X1_HW1_LAST:
   3297 	case R_TILEGX_IMM16_X0_HW2_LAST:
   3298 	case R_TILEGX_IMM16_X1_HW2_LAST:
   3299 	  if ((input_section->flags & SEC_ALLOC) == 0)
   3300 	    break;
   3301 
   3302 	  if ((bfd_link_pic (info)
   3303 	       && (h == NULL
   3304 		   || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3305 		       && !resolved_to_zero)
   3306 		   || h->root.type != bfd_link_hash_undefweak)
   3307 	       && (! howto->pc_relative
   3308 		   || !SYMBOL_CALLS_LOCAL (info, h)))
   3309 	      || (!bfd_link_pic (info)
   3310 		  && h != NULL
   3311 		  && h->dynindx != -1
   3312 		  && !h->non_got_ref
   3313 		  && ((h->def_dynamic
   3314 		       && !h->def_regular)
   3315 		      || h->root.type == bfd_link_hash_undefweak
   3316 		      || h->root.type == bfd_link_hash_undefined)))
   3317 	    {
   3318 	      Elf_Internal_Rela outrel;
   3319 	      bool skip, relocate = false;
   3320 
   3321 	      /* When generating a shared object, these relocations
   3322 		 are copied into the output file to be resolved at run
   3323 		 time.  */
   3324 
   3325 	      BFD_ASSERT (sreloc != NULL);
   3326 
   3327 	      skip = false;
   3328 
   3329 	      outrel.r_offset =
   3330 		_bfd_elf_section_offset (output_bfd, info, input_section,
   3331 					 rel->r_offset);
   3332 	      if (outrel.r_offset == (bfd_vma) -1)
   3333 		skip = true;
   3334 	      else if (outrel.r_offset == (bfd_vma) -2)
   3335 		skip = true, relocate = true;
   3336 	      outrel.r_offset += (input_section->output_section->vma
   3337 				  + input_section->output_offset);
   3338 
   3339 	      switch (r_type)
   3340 		{
   3341 		case R_TILEGX_64_PCREL:
   3342 		case R_TILEGX_32_PCREL:
   3343 		case R_TILEGX_16_PCREL:
   3344 		case R_TILEGX_8_PCREL:
   3345 		  /* If the symbol is not dynamic, we should not keep
   3346 		     a dynamic relocation.  But an .rela.* slot has been
   3347 		     allocated for it, output R_TILEGX_NONE.
   3348 		     FIXME: Add code tracking needed dynamic relocs as
   3349 		     e.g. i386 has.  */
   3350 		  if (h->dynindx == -1)
   3351 		    skip = true, relocate = true;
   3352 		  break;
   3353 		}
   3354 
   3355 	      if (skip)
   3356 		memset (&outrel, 0, sizeof outrel);
   3357 	      /* h->dynindx may be -1 if the symbol was marked to
   3358 		 become local.  */
   3359 	      else if (h != NULL &&
   3360 		       h->dynindx != -1
   3361 		       && (! is_plt
   3362 			   || !bfd_link_pic (info)
   3363 			   || !SYMBOLIC_BIND (info, h)
   3364 			   || !h->def_regular))
   3365 		{
   3366 		  BFD_ASSERT (h->dynindx != -1);
   3367 		  outrel.r_info = TILEGX_ELF_R_INFO (htab, rel, h->dynindx, r_type);
   3368 		  outrel.r_addend = rel->r_addend;
   3369 		}
   3370 	      else
   3371 		{
   3372 		  if (r_type == R_TILEGX_32 || r_type == R_TILEGX_64)
   3373 		    {
   3374 		      outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, 0,
   3375 							 R_TILEGX_RELATIVE);
   3376 		      outrel.r_addend = relocation + rel->r_addend;
   3377 		    }
   3378 		  else
   3379 		    {
   3380 		      long indx;
   3381 
   3382 		      outrel.r_addend = relocation + rel->r_addend;
   3383 
   3384 		      if (is_plt)
   3385 			sec = htab->elf.splt;
   3386 
   3387 		      if (bfd_is_abs_section (sec))
   3388 			indx = 0;
   3389 		      else if (sec == NULL || sec->owner == NULL)
   3390 			{
   3391 			  bfd_set_error (bfd_error_bad_value);
   3392 			  return false;
   3393 			}
   3394 		      else
   3395 			{
   3396 			  asection *osec;
   3397 
   3398 			  /* We are turning this relocation into one
   3399 			     against a section symbol.  It would be
   3400 			     proper to subtract the symbol's value,
   3401 			     osec->vma, from the emitted reloc addend,
   3402 			     but ld.so expects buggy relocs.  */
   3403 			  osec = sec->output_section;
   3404 			  indx = elf_section_data (osec)->dynindx;
   3405 
   3406 			  if (indx == 0)
   3407 			    {
   3408 			      osec = htab->elf.text_index_section;
   3409 			      indx = elf_section_data (osec)->dynindx;
   3410 			    }
   3411 
   3412 			  /* FIXME: we really should be able to link non-pic
   3413 			     shared libraries.  */
   3414 			  if (indx == 0)
   3415 			    {
   3416 			      BFD_FAIL ();
   3417 			      _bfd_error_handler
   3418 				(_("%pB: probably compiled without -fPIC?"),
   3419 				 input_bfd);
   3420 			      bfd_set_error (bfd_error_bad_value);
   3421 			      return false;
   3422 			    }
   3423 			}
   3424 
   3425 		      outrel.r_info = TILEGX_ELF_R_INFO (htab, rel, indx,
   3426 							 r_type);
   3427 		    }
   3428 		}
   3429 
   3430 	      tilegx_elf_append_rela (output_bfd, sreloc, &outrel);
   3431 
   3432 	      /* This reloc will be computed at runtime, so there's no
   3433 		 need to do anything now.  */
   3434 	      if (! relocate)
   3435 		continue;
   3436 	    }
   3437 	  break;
   3438 
   3439 	case R_TILEGX_IMM16_X0_HW0_TLS_LE:
   3440 	case R_TILEGX_IMM16_X1_HW0_TLS_LE:
   3441 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
   3442 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
   3443 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
   3444 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
   3445 	  if (!bfd_link_executable (info))
   3446 	    {
   3447 	      Elf_Internal_Rela outrel;
   3448 	      bool skip;
   3449 
   3450 	      BFD_ASSERT (sreloc != NULL);
   3451 	      skip = false;
   3452 	      outrel.r_offset =
   3453 		_bfd_elf_section_offset (output_bfd, info, input_section,
   3454 					 rel->r_offset);
   3455 	      if (outrel.r_offset == (bfd_vma) -1)
   3456 		skip = true;
   3457 	      else if (outrel.r_offset == (bfd_vma) -2)
   3458 		skip = true;
   3459 	      outrel.r_offset += (input_section->output_section->vma
   3460 				  + input_section->output_offset);
   3461 	      if (skip)
   3462 		memset (&outrel, 0, sizeof outrel);
   3463 	      else
   3464 		{
   3465 		  outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, 0, r_type);
   3466 		  outrel.r_addend = relocation - dtpoff_base (info)
   3467 				    + rel->r_addend;
   3468 		}
   3469 
   3470 	      tilegx_elf_append_rela (output_bfd, sreloc, &outrel);
   3471 	      continue;
   3472 	    }
   3473 	  relocation = tpoff (info, relocation);
   3474 	  break;
   3475 
   3476 	case R_TILEGX_IMM16_X0_HW0_TLS_GD:
   3477 	case R_TILEGX_IMM16_X1_HW0_TLS_GD:
   3478 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
   3479 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
   3480 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
   3481 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
   3482 	case R_TILEGX_IMM16_X0_HW0_TLS_IE:
   3483 	case R_TILEGX_IMM16_X1_HW0_TLS_IE:
   3484 	case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
   3485 	case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
   3486 	case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
   3487 	case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
   3488 	  r_type = tilegx_elf_tls_transition (info, r_type, h == NULL,
   3489 					      input_section->sec_flg0);
   3490 	  tls_type = GOT_UNKNOWN;
   3491 	  if (h == NULL && local_got_offsets)
   3492 	    tls_type =
   3493 	      _bfd_tilegx_elf_local_got_tls_type (input_bfd) [r_symndx];
   3494 	  else if (h != NULL)
   3495 	    {
   3496 	      tls_type = tilegx_elf_hash_entry(h)->tls_type;
   3497 	      if (bfd_link_executable (info)
   3498 		  && h->dynindx == -1
   3499 		  && tls_type == GOT_TLS_IE)
   3500 		r_type = (!input_section->sec_flg0
   3501 			  ? tilegx_tls_translate_to_le (r_type)
   3502 			  : tilegx_tls_translate_to_ie (r_type));
   3503 	    }
   3504 
   3505 	  if (tls_type == GOT_TLS_IE)
   3506 	    r_type = tilegx_tls_translate_to_ie (r_type);
   3507 
   3508 	  if (r_type == R_TILEGX_IMM16_X0_HW0_TLS_LE
   3509 	      || r_type == R_TILEGX_IMM16_X1_HW0_TLS_LE
   3510 	      || r_type == R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE
   3511 	      || r_type == R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE
   3512 	      || r_type == R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE
   3513 	      || r_type == R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE)
   3514 	    {
   3515 	      relocation = tpoff (info, relocation);
   3516 	      break;
   3517 	    }
   3518 
   3519 	  if (h != NULL)
   3520 	    {
   3521 	      off = h->got.offset;
   3522 	      h->got.offset |= 1;
   3523 	    }
   3524 	  else
   3525 	    {
   3526 	      BFD_ASSERT (local_got_offsets != NULL);
   3527 	      off = local_got_offsets[r_symndx];
   3528 	      local_got_offsets[r_symndx] |= 1;
   3529 	    }
   3530 
   3531 	  if (htab->elf.sgot == NULL)
   3532 	    abort ();
   3533 
   3534 	  if ((off & 1) != 0)
   3535 	    off &= ~1;
   3536 	  else
   3537 	    {
   3538 	      Elf_Internal_Rela outrel;
   3539 	      int indx = 0;
   3540 	      bool need_relocs = false;
   3541 
   3542 	      if (htab->elf.srelgot == NULL)
   3543 		abort ();
   3544 
   3545 	      if (h != NULL)
   3546 	      {
   3547 		bool dyn;
   3548 		dyn = htab->elf.dynamic_sections_created;
   3549 
   3550 		if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
   3551 						     bfd_link_pic (info),
   3552 						     h)
   3553 		    && (!bfd_link_pic (info)
   3554 			|| !SYMBOL_REFERENCES_LOCAL (info, h)))
   3555 		  {
   3556 		    indx = h->dynindx;
   3557 		  }
   3558 	      }
   3559 
   3560 	      /* The GOT entries have not been initialized yet.  Do it
   3561 		 now, and emit any relocations. */
   3562 	      if ((bfd_link_pic (info) || indx != 0)
   3563 		  && (h == NULL
   3564 		      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3565 		      || h->root.type != bfd_link_hash_undefweak))
   3566 		    need_relocs = true;
   3567 
   3568 	      switch (r_type)
   3569 		{
   3570 		  case R_TILEGX_IMM16_X0_HW0_TLS_IE:
   3571 		  case R_TILEGX_IMM16_X1_HW0_TLS_IE:
   3572 		  case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
   3573 		  case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
   3574 		  case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
   3575 		  case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
   3576 		    if (need_relocs) {
   3577 		      TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
   3578 					   htab->elf.sgot->contents + off);
   3579 		      outrel.r_offset = (htab->elf.sgot->output_section->vma
   3580 				       + htab->elf.sgot->output_offset + off);
   3581 		      outrel.r_addend = 0;
   3582 		      if (indx == 0)
   3583 			outrel.r_addend = relocation - dtpoff_base (info);
   3584 		      outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
   3585 							 TILEGX_ELF_TPOFF_RELOC (htab));
   3586 		      tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   3587 		    } else {
   3588 		      TILEGX_ELF_PUT_WORD (htab, output_bfd,
   3589 					   tpoff (info, relocation),
   3590 					   htab->elf.sgot->contents + off);
   3591 		    }
   3592 		    break;
   3593 
   3594 		  case R_TILEGX_IMM16_X0_HW0_TLS_GD:
   3595 		  case R_TILEGX_IMM16_X1_HW0_TLS_GD:
   3596 		  case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
   3597 		  case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
   3598 		  case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
   3599 		  case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
   3600 		    if (need_relocs) {
   3601 		      outrel.r_offset = (htab->elf.sgot->output_section->vma
   3602 				       + htab->elf.sgot->output_offset + off);
   3603 		      outrel.r_addend = 0;
   3604 		      outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
   3605 							 TILEGX_ELF_DTPMOD_RELOC (htab));
   3606 		      TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
   3607 					   htab->elf.sgot->contents + off);
   3608 		      tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   3609 		      if (indx == 0)
   3610 			{
   3611 			  BFD_ASSERT (! unresolved_reloc);
   3612 			  TILEGX_ELF_PUT_WORD (htab, output_bfd,
   3613 					       relocation - dtpoff_base (info),
   3614 					       (htab->elf.sgot->contents + off +
   3615 						TILEGX_ELF_WORD_BYTES (htab)));
   3616 			}
   3617 		      else
   3618 			{
   3619 			  TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
   3620 					       (htab->elf.sgot->contents + off +
   3621 						TILEGX_ELF_WORD_BYTES (htab)));
   3622 			  outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
   3623 							     TILEGX_ELF_DTPOFF_RELOC (htab));
   3624 			  outrel.r_offset += TILEGX_ELF_WORD_BYTES (htab);
   3625 			  tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   3626 			}
   3627 		    }
   3628 
   3629 		    else {
   3630 		      /* If we are not emitting relocations for a
   3631 			 general dynamic reference, then we must be in a
   3632 			 static link or an executable link with the
   3633 			 symbol binding locally.  Mark it as belonging
   3634 			 to module 1, the executable.  */
   3635 		      TILEGX_ELF_PUT_WORD (htab, output_bfd, 1,
   3636 					   htab->elf.sgot->contents + off );
   3637 		      TILEGX_ELF_PUT_WORD (htab, output_bfd,
   3638 					   relocation - dtpoff_base (info),
   3639 					   htab->elf.sgot->contents + off +
   3640 					   TILEGX_ELF_WORD_BYTES (htab));
   3641 		   }
   3642 		   break;
   3643 		}
   3644 	    }
   3645 
   3646 	  if (off >= (bfd_vma) -2)
   3647 	    abort ();
   3648 
   3649 	  relocation = off - got_base;
   3650 	  unresolved_reloc = false;
   3651 	  howto = tilegx_elf_howto_table + r_type;
   3652 	  break;
   3653 
   3654 	default:
   3655 	  break;
   3656 	}
   3657 
   3658       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   3659 	 because such sections are not SEC_ALLOC and thus ld.so will
   3660 	 not process them.  */
   3661       if (unresolved_reloc
   3662 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   3663 	       && h->def_dynamic)
   3664 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   3665 				      rel->r_offset) != (bfd_vma) -1)
   3666 	_bfd_error_handler
   3667 	  /* xgettext:c-format */
   3668 	  (_("%pB(%pA+%#" PRIx64 "): "
   3669 	     "unresolvable %s relocation against symbol `%s'"),
   3670 	   input_bfd,
   3671 	   input_section,
   3672 	   (uint64_t) rel->r_offset,
   3673 	   howto->name,
   3674 	   h->root.root.string);
   3675 
   3676       r = bfd_reloc_continue;
   3677 
   3678       /* Get the operand creation function, if any. */
   3679       create_func = reloc_to_create_func[r_type];
   3680       if (create_func == NULL)
   3681       {
   3682 	r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   3683 				      contents, rel->r_offset,
   3684 				      relocation, rel->r_addend);
   3685       }
   3686       else
   3687       {
   3688 	if (howto->pc_relative)
   3689 	{
   3690 	  relocation -=
   3691 	    input_section->output_section->vma + input_section->output_offset;
   3692 	  if (howto->pcrel_offset)
   3693 	    relocation -= rel->r_offset;
   3694 	}
   3695 
   3696 	bfd_byte *data;
   3697 
   3698 	/* Add the relocation addend if any to the final target value */
   3699 	relocation += rel->r_addend;
   3700 
   3701 	/* Do basic range checking */
   3702 	r = bfd_check_overflow (howto->complain_on_overflow,
   3703 				howto->bitsize,
   3704 				howto->rightshift,
   3705 				TILEGX_ELF_WORD_BYTES (htab) * 8,
   3706 				relocation);
   3707 
   3708 	/*
   3709 	 * Write the relocated value out into the raw section data.
   3710 	 * Don't put a relocation out in the .rela section.
   3711 	 */
   3712 	tilegx_bundle_bits mask = create_func(-1);
   3713 	tilegx_bundle_bits value = create_func(relocation >> howto->rightshift);
   3714 
   3715 	/* Only touch bytes while the mask is not 0, so we
   3716 	   don't write to out of bounds memory if this is actually
   3717 	   a 16-bit switch instruction. */
   3718 	for (data = contents + rel->r_offset; mask != 0; data++)
   3719 	  {
   3720 	    bfd_byte byte_mask = (bfd_byte)mask;
   3721 	    *data = (*data & ~byte_mask) | ((bfd_byte)value & byte_mask);
   3722 	    mask >>= 8;
   3723 	    value >>= 8;
   3724 	  }
   3725       }
   3726 
   3727       if (r != bfd_reloc_ok)
   3728 	{
   3729 	  const char *msg = NULL;
   3730 
   3731 	  switch (r)
   3732 	    {
   3733 	    case bfd_reloc_overflow:
   3734 	      (*info->callbacks->reloc_overflow)
   3735 		(info, (h ? &h->root : NULL), name, howto->name,
   3736 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   3737 	      break;
   3738 
   3739 	    case bfd_reloc_undefined:
   3740 	      (*info->callbacks->undefined_symbol)
   3741 		(info, name, input_bfd, input_section, rel->r_offset, true);
   3742 	      break;
   3743 
   3744 	    case bfd_reloc_outofrange:
   3745 	      msg = _("internal error: out of range error");
   3746 	      break;
   3747 
   3748 	    case bfd_reloc_notsupported:
   3749 	      msg = _("internal error: unsupported relocation error");
   3750 	      break;
   3751 
   3752 	    case bfd_reloc_dangerous:
   3753 	      msg = _("internal error: dangerous relocation");
   3754 	      break;
   3755 
   3756 	    default:
   3757 	      msg = _("internal error: unknown error");
   3758 	      break;
   3759 	    }
   3760 
   3761 	  if (msg)
   3762 	    (*info->callbacks->warning) (info, msg, name, input_bfd,
   3763 					 input_section, rel->r_offset);
   3764 	}
   3765     }
   3766 
   3767   return true;
   3768 }
   3769 
   3770 /* Finish up dynamic symbol handling.  We set the contents of various
   3771    dynamic sections here.  */
   3772 
   3773 bool
   3774 tilegx_elf_finish_dynamic_symbol (bfd *output_bfd,
   3775 				  struct bfd_link_info *info,
   3776 				  struct elf_link_hash_entry *h,
   3777 				  Elf_Internal_Sym *sym)
   3778 {
   3779   struct tilegx_elf_link_hash_table *htab;
   3780 
   3781   htab = tilegx_elf_hash_table (info);
   3782   BFD_ASSERT (htab != NULL);
   3783 
   3784   if (h->plt.offset != (bfd_vma) -1)
   3785     {
   3786       asection *splt;
   3787       asection *srela;
   3788       asection *sgotplt;
   3789       Elf_Internal_Rela rela;
   3790       bfd_byte *loc;
   3791       bfd_vma r_offset;
   3792       elf_backend_data *bed = get_elf_backend_data (output_bfd);
   3793 
   3794 
   3795       int rela_index;
   3796 
   3797       /* This symbol has an entry in the PLT.  Set it up.  */
   3798 
   3799       BFD_ASSERT (h->dynindx != -1);
   3800 
   3801       splt = htab->elf.splt;
   3802       srela = htab->elf.srelplt;
   3803       sgotplt = htab->elf.sgotplt;
   3804 
   3805       if (splt == NULL || srela == NULL)
   3806        abort ();
   3807 
   3808       /* Fill in the entry in the procedure linkage table.  */
   3809       rela_index = tilegx_plt_entry_build (output_bfd, htab, splt, sgotplt,
   3810 					   h->plt.offset, &r_offset);
   3811 
   3812       /* Fill in the entry in the global offset table, which initially points
   3813 	 to the beginning of the plt.  */
   3814       TILEGX_ELF_PUT_WORD (htab, output_bfd,
   3815 			   splt->output_section->vma + splt->output_offset,
   3816 			   sgotplt->contents + r_offset);
   3817 
   3818       /* Fill in the entry in the .rela.plt section.  */
   3819       rela.r_offset = (sgotplt->output_section->vma
   3820 		       + sgotplt->output_offset
   3821 		       + r_offset);
   3822       rela.r_addend = 0;
   3823       rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_JMP_SLOT);
   3824 
   3825       loc = srela->contents + rela_index * TILEGX_ELF_RELA_BYTES (htab);
   3826       bed->s->swap_reloca_out (output_bfd, &rela, loc);
   3827 
   3828       if (!h->def_regular)
   3829 	{
   3830 	  /* Mark the symbol as undefined, rather than as defined in
   3831 	     the .plt section.  Leave the value alone.  */
   3832 	  sym->st_shndx = SHN_UNDEF;
   3833 	  /* If the symbol is weak, we do need to clear the value.
   3834 	     Otherwise, the PLT entry would provide a definition for
   3835 	     the symbol even if the symbol wasn't defined anywhere,
   3836 	     and so the symbol would never be NULL.  */
   3837 	  if (!h->ref_regular_nonweak)
   3838 	    sym->st_value = 0;
   3839 	}
   3840     }
   3841 
   3842   if (h->got.offset != (bfd_vma) -1
   3843       && tilegx_elf_hash_entry(h)->tls_type != GOT_TLS_GD
   3844       && tilegx_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
   3845     {
   3846       asection *sgot;
   3847       asection *srela;
   3848       Elf_Internal_Rela rela;
   3849 
   3850       /* This symbol has an entry in the GOT.  Set it up.  */
   3851 
   3852       sgot = htab->elf.sgot;
   3853       srela = htab->elf.srelgot;
   3854       BFD_ASSERT (sgot != NULL && srela != NULL);
   3855 
   3856       rela.r_offset = (sgot->output_section->vma
   3857 		       + sgot->output_offset
   3858 		       + (h->got.offset &~ (bfd_vma) 1));
   3859 
   3860       /* If this is a -Bsymbolic link, and the symbol is defined
   3861 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
   3862 	 the symbol was forced to be local because of a version file.
   3863 	 The entry in the global offset table will already have been
   3864 	 initialized in the relocate_section function.  */
   3865       if (bfd_link_pic (info)
   3866 	  && (info->symbolic || h->dynindx == -1)
   3867 	  && h->def_regular)
   3868 	{
   3869 	  asection *sec = h->root.u.def.section;
   3870 	  rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, 0, R_TILEGX_RELATIVE);
   3871 	  rela.r_addend = (h->root.u.def.value
   3872 			   + sec->output_section->vma
   3873 			   + sec->output_offset);
   3874 	}
   3875       else
   3876 	{
   3877 	  rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_GLOB_DAT);
   3878 	  rela.r_addend = 0;
   3879 	}
   3880 
   3881       TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
   3882 			   sgot->contents + (h->got.offset & ~(bfd_vma) 1));
   3883       tilegx_elf_append_rela (output_bfd, srela, &rela);
   3884     }
   3885 
   3886   if (h->needs_copy)
   3887     {
   3888       asection *s;
   3889       Elf_Internal_Rela rela;
   3890 
   3891       /* This symbols needs a copy reloc.  Set it up.  */
   3892       BFD_ASSERT (h->dynindx != -1);
   3893 
   3894       if (h->root.u.def.section == htab->elf.sdynrelro)
   3895 	s = htab->elf.sreldynrelro;
   3896       else
   3897 	s = htab->elf.srelbss;
   3898       BFD_ASSERT (s != NULL);
   3899 
   3900       rela.r_offset = (h->root.u.def.value
   3901 		       + h->root.u.def.section->output_section->vma
   3902 		       + h->root.u.def.section->output_offset);
   3903       rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_COPY);
   3904       rela.r_addend = 0;
   3905       tilegx_elf_append_rela (output_bfd, s, &rela);
   3906     }
   3907 
   3908   /* Mark some specially defined symbols as absolute. */
   3909   if (h == htab->elf.hdynamic
   3910       || (h == htab->elf.hgot || h == htab->elf.hplt))
   3911     sym->st_shndx = SHN_ABS;
   3912 
   3913   return true;
   3914 }
   3915 
   3916 /* Finish up the dynamic sections.  */
   3917 
   3918 static bool
   3919 tilegx_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
   3920 		   bfd *dynobj, asection *sdyn,
   3921 		   asection *splt ATTRIBUTE_UNUSED)
   3922 {
   3923   struct tilegx_elf_link_hash_table *htab;
   3924   elf_backend_data *bed;
   3925   bfd_byte *dyncon, *dynconend;
   3926   size_t dynsize;
   3927 
   3928   htab = tilegx_elf_hash_table (info);
   3929   BFD_ASSERT (htab != NULL);
   3930   bed = get_elf_backend_data (output_bfd);
   3931   dynsize = bed->s->sizeof_dyn;
   3932   dynconend = sdyn->contents + sdyn->size;
   3933 
   3934   for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
   3935     {
   3936       Elf_Internal_Dyn dyn;
   3937       asection *s;
   3938 
   3939       bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   3940 
   3941       switch (dyn.d_tag)
   3942 	{
   3943 	case DT_PLTGOT:
   3944 	  s = htab->elf.sgotplt;
   3945 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   3946 	  break;
   3947 	case DT_JMPREL:
   3948 	  s = htab->elf.srelplt;
   3949 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   3950 	  break;
   3951 	case DT_PLTRELSZ:
   3952 	  s = htab->elf.srelplt;
   3953 	  dyn.d_un.d_val = s->size;
   3954 	  break;
   3955 	default:
   3956 	  continue;
   3957 	}
   3958 
   3959       bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
   3960     }
   3961   return true;
   3962 }
   3963 
   3964 bool
   3965 tilegx_elf_finish_dynamic_sections (bfd *output_bfd,
   3966 				    struct bfd_link_info *info,
   3967 				    bfd_byte *buf ATTRIBUTE_UNUSED)
   3968 {
   3969   bfd *dynobj;
   3970   asection *sdyn;
   3971   struct tilegx_elf_link_hash_table *htab;
   3972   size_t pad_size;
   3973 
   3974   htab = tilegx_elf_hash_table (info);
   3975   BFD_ASSERT (htab != NULL);
   3976   dynobj = htab->elf.dynobj;
   3977 
   3978   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   3979 
   3980   if (elf_hash_table (info)->dynamic_sections_created)
   3981     {
   3982       asection *splt;
   3983       bool ret;
   3984 
   3985       splt = htab->elf.splt;
   3986       BFD_ASSERT (splt != NULL && sdyn != NULL);
   3987 
   3988       ret = tilegx_finish_dyn (output_bfd, info, dynobj, sdyn, splt);
   3989 
   3990       if (!ret)
   3991 	return ret;
   3992 
   3993       /* Fill in the head and tail entries in the procedure linkage table.  */
   3994       if (splt->size > 0)
   3995 	{
   3996 	  memcpy (splt->contents,
   3997 		  ABI_64_P (output_bfd) ?
   3998 		    tilegx64_plt0_entry : tilegx32_plt0_entry,
   3999 		  PLT_HEADER_SIZE);
   4000 
   4001 	  memcpy (splt->contents + splt->size
   4002 		  - PLT_ENTRY_SIZE + PLT_HEADER_SIZE,
   4003 		  ABI_64_P (output_bfd) ?
   4004 		    tilegx64_plt_tail_entry : tilegx32_plt_tail_entry,
   4005 		  PLT_TAIL_SIZE);
   4006 	  /* Add padding so that the plt section is a multiple of its
   4007 	     entry size.  */
   4008 	  pad_size = PLT_ENTRY_SIZE - PLT_HEADER_SIZE - PLT_TAIL_SIZE;
   4009 	  memset (splt->contents + splt->size - pad_size, 0, pad_size);
   4010 
   4011 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize
   4012 	    = PLT_ENTRY_SIZE;
   4013 	}
   4014     }
   4015 
   4016   if (htab->elf.sgotplt)
   4017     {
   4018       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
   4019 	{
   4020 	  _bfd_error_handler
   4021 	    (_("discarded output section: `%pA'"), htab->elf.sgotplt);
   4022 	  return false;
   4023 	}
   4024 
   4025       if (htab->elf.sgotplt->size > 0)
   4026 	{
   4027 	  /* Write the first two entries in .got.plt, needed for the dynamic
   4028 	     linker.  */
   4029 	  TILEGX_ELF_PUT_WORD (htab, output_bfd, (bfd_vma) -1,
   4030 			       htab->elf.sgotplt->contents);
   4031 	  TILEGX_ELF_PUT_WORD (htab, output_bfd, (bfd_vma) 0,
   4032 			       htab->elf.sgotplt->contents
   4033 			       + GOT_ENTRY_SIZE (htab));
   4034 
   4035 	  elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
   4036 	    GOT_ENTRY_SIZE (htab);
   4037 	}
   4038     }
   4039 
   4040   if (htab->elf.sgot)
   4041     {
   4042       if (htab->elf.sgot->size > 0)
   4043 	{
   4044 	  /* Set the first entry in the global offset table to the address of
   4045 	     the dynamic section.  */
   4046 	  bfd_vma val = (sdyn ?
   4047 			 sdyn->output_section->vma + sdyn->output_offset :
   4048 			 0);
   4049 	  TILEGX_ELF_PUT_WORD (htab, output_bfd, val,
   4050 			       htab->elf.sgot->contents);
   4051 
   4052 	  elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize =
   4053 	    GOT_ENTRY_SIZE (htab);
   4054 	}
   4055     }
   4056 
   4057   return true;
   4058 }
   4059 
   4060 
   4061 
   4063 /* Return address for Ith PLT stub in section PLT, for relocation REL
   4064    or (bfd_vma) -1 if it should not be included.  */
   4065 
   4066 bfd_vma
   4067 tilegx_elf_plt_sym_val (bfd_vma i, const asection *plt,
   4068 			const arelent *rel ATTRIBUTE_UNUSED)
   4069 {
   4070   return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
   4071 }
   4072 
   4073 enum elf_reloc_type_class
   4074 tilegx_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   4075 			 const asection *rel_sec ATTRIBUTE_UNUSED,
   4076 			 const Elf_Internal_Rela *rela)
   4077 {
   4078   switch ((int) TILEGX_ELF_R_TYPE (rela->r_info))
   4079     {
   4080     case R_TILEGX_RELATIVE:
   4081       return reloc_class_relative;
   4082     case R_TILEGX_JMP_SLOT:
   4083       return reloc_class_plt;
   4084     case R_TILEGX_COPY:
   4085       return reloc_class_copy;
   4086     default:
   4087       return reloc_class_normal;
   4088     }
   4089 }
   4090 
   4091 int
   4092 tilegx_additional_program_headers (bfd *abfd,
   4093 				   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   4094 {
   4095   /* Each .intrpt section specified by the user adds another PT_LOAD
   4096      header since the sections are discontiguous. */
   4097   static const char intrpt_sections[4][9] =
   4098     {
   4099       ".intrpt0", ".intrpt1", ".intrpt2", ".intrpt3"
   4100     };
   4101   int count = 0;
   4102   int i;
   4103 
   4104   for (i = 0; i < 4; i++)
   4105     {
   4106       asection *sec = bfd_get_section_by_name (abfd, intrpt_sections[i]);
   4107       if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
   4108 	++count;
   4109     }
   4110 
   4111   /* Add four "padding" headers in to leave room in case a custom linker
   4112      script does something fancy. Otherwise ld complains that it ran
   4113      out of program headers and refuses to link. */
   4114   count += 4;
   4115 
   4116   return count;
   4117 }
   4118 
   4119 
   4120 bool
   4121 _bfd_tilegx_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   4122 {
   4123   bfd *obfd = info->output_bfd;
   4124   const char *targ1 = bfd_get_target (ibfd);
   4125   const char *targ2 = bfd_get_target (obfd);
   4126 
   4127   if (strcmp (targ1, targ2) != 0)
   4128     {
   4129       _bfd_error_handler
   4130 	/* xgettext:c-format */
   4131 	(_("%pB: cannot link together %s and %s objects"),
   4132 	 ibfd, targ1, targ2);
   4133       bfd_set_error (bfd_error_bad_value);
   4134       return false;
   4135     }
   4136 
   4137   return true;
   4138 }
   4139