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