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