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