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