OSDN Git Service

daily update
[pf3gnuchains/pf3gnuchains4x.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, void *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   info = (struct bfd_link_info *) inf;
2304   htab = tilegx_elf_hash_table (info);
2305   BFD_ASSERT (htab != NULL);
2306
2307   if (htab->elf.dynamic_sections_created
2308       && h->plt.refcount > 0)
2309     {
2310       /* Make sure this symbol is output as a dynamic symbol.
2311          Undefined weak syms won't yet be marked as dynamic.  */
2312       if (h->dynindx == -1
2313           && !h->forced_local)
2314         {
2315           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2316             return FALSE;
2317         }
2318
2319       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2320         {
2321           asection *s = htab->elf.splt;
2322
2323           /* Allocate room for the header and tail.  */
2324           if (s->size == 0)
2325             {
2326               s->size = PLT_HEADER_SIZE + PLT_TAIL_SIZE;
2327             }
2328
2329           h->plt.offset = s->size - PLT_TAIL_SIZE;
2330
2331           /* If this symbol is not defined in a regular file, and we are
2332              not generating a shared library, then set the symbol to this
2333              location in the .plt.  This is required to make function
2334              pointers compare as equal between the normal executable and
2335              the shared library.  */
2336           if (! info->shared
2337               && !h->def_regular)
2338             {
2339               h->root.u.def.section = s;
2340               h->root.u.def.value = h->plt.offset;
2341             }
2342
2343           /* Make room for this entry.  */
2344           s->size += PLT_ENTRY_SIZE;
2345
2346           /* We also need to make an entry in the .got.plt section.  */
2347           htab->elf.sgotplt->size += GOT_ENTRY_SIZE (htab);
2348
2349           /* We also need to make an entry in the .rela.plt section.  */
2350           htab->elf.srelplt->size += TILEGX_ELF_RELA_BYTES (htab);
2351         }
2352       else
2353         {
2354           h->plt.offset = (bfd_vma) -1;
2355           h->needs_plt = 0;
2356         }
2357     }
2358   else
2359     {
2360       h->plt.offset = (bfd_vma) -1;
2361       h->needs_plt = 0;
2362     }
2363
2364   if (h->got.refcount > 0)
2365     {
2366       asection *s;
2367       bfd_boolean dyn;
2368       int tls_type = tilegx_elf_hash_entry(h)->tls_type;
2369
2370       /* Make sure this symbol is output as a dynamic symbol.
2371          Undefined weak syms won't yet be marked as dynamic.  */
2372       if (h->dynindx == -1
2373           && !h->forced_local)
2374         {
2375           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2376             return FALSE;
2377         }
2378
2379       s = htab->elf.sgot;
2380       h->got.offset = s->size;
2381       s->size += TILEGX_ELF_WORD_BYTES (htab);
2382       /* TLS_GD entries need 2 consecutive GOT slots. */
2383       if (tls_type == GOT_TLS_GD)
2384         s->size += TILEGX_ELF_WORD_BYTES (htab);
2385       dyn = htab->elf.dynamic_sections_created;
2386       /* TLS_IE needs one dynamic relocation,
2387          TLS_GD needs two if local symbol and two if global.  */
2388       if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE)
2389         htab->elf.srelgot->size += 2 * TILEGX_ELF_RELA_BYTES (htab);
2390       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2391         htab->elf.srelgot->size += TILEGX_ELF_RELA_BYTES (htab);
2392     }
2393   else
2394     h->got.offset = (bfd_vma) -1;
2395
2396   eh = (struct tilegx_elf_link_hash_entry *) h;
2397   if (eh->dyn_relocs == NULL)
2398     return TRUE;
2399
2400   /* In the shared -Bsymbolic case, discard space allocated for
2401      dynamic pc-relative relocs against symbols which turn out to be
2402      defined in regular objects.  For the normal shared case, discard
2403      space for pc-relative relocs that have become local due to symbol
2404      visibility changes.  */
2405
2406   if (info->shared)
2407     {
2408       if (SYMBOL_CALLS_LOCAL (info, h))
2409         {
2410           struct tilegx_elf_dyn_relocs **pp;
2411
2412           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2413             {
2414               p->count -= p->pc_count;
2415               p->pc_count = 0;
2416               if (p->count == 0)
2417                 *pp = p->next;
2418               else
2419                 pp = &p->next;
2420             }
2421         }
2422
2423       /* Also discard relocs on undefined weak syms with non-default
2424          visibility.  */
2425       if (eh->dyn_relocs != NULL
2426           && h->root.type == bfd_link_hash_undefweak)
2427         {
2428           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2429             eh->dyn_relocs = NULL;
2430
2431           /* Make sure undefined weak symbols are output as a dynamic
2432              symbol in PIEs.  */
2433           else if (h->dynindx == -1
2434                    && !h->forced_local)
2435             {
2436               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2437                 return FALSE;
2438             }
2439         }
2440     }
2441   else
2442     {
2443       /* For the non-shared case, discard space for relocs against
2444          symbols which turn out to need copy relocs or are not
2445          dynamic.  */
2446
2447       if (!h->non_got_ref
2448           && ((h->def_dynamic
2449                && !h->def_regular)
2450               || (htab->elf.dynamic_sections_created
2451                   && (h->root.type == bfd_link_hash_undefweak
2452                       || h->root.type == bfd_link_hash_undefined))))
2453         {
2454           /* Make sure this symbol is output as a dynamic symbol.
2455              Undefined weak syms won't yet be marked as dynamic.  */
2456           if (h->dynindx == -1
2457               && !h->forced_local)
2458             {
2459               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2460                 return FALSE;
2461             }
2462
2463           /* If that succeeded, we know we'll be keeping all the
2464              relocs.  */
2465           if (h->dynindx != -1)
2466             goto keep;
2467         }
2468
2469       eh->dyn_relocs = NULL;
2470
2471     keep: ;
2472     }
2473
2474   /* Finally, allocate space.  */
2475   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2476     {
2477       asection *sreloc = elf_section_data (p->sec)->sreloc;
2478       sreloc->size += p->count * TILEGX_ELF_RELA_BYTES (htab);
2479     }
2480
2481   return TRUE;
2482 }
2483
2484 /* Find any dynamic relocs that apply to read-only sections.  */
2485
2486 static bfd_boolean
2487 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2488 {
2489   struct tilegx_elf_link_hash_entry *eh;
2490   struct tilegx_elf_dyn_relocs *p;
2491
2492   eh = (struct tilegx_elf_link_hash_entry *) h;
2493   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2494     {
2495       asection *s = p->sec->output_section;
2496
2497       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2498         {
2499           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2500
2501           info->flags |= DF_TEXTREL;
2502
2503           /* Not an error, just cut short the traversal.  */
2504           return FALSE;
2505         }
2506     }
2507   return TRUE;
2508 }
2509
2510 /* Return true if the dynamic symbol for a given section should be
2511    omitted when creating a shared library.  */
2512
2513 bfd_boolean
2514 tilegx_elf_omit_section_dynsym (bfd *output_bfd,
2515                                 struct bfd_link_info *info,
2516                                 asection *p)
2517 {
2518   /* We keep the .got section symbol so that explicit relocations
2519      against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
2520      can be turned into relocations against the .got symbol.  */
2521   if (strcmp (p->name, ".got") == 0)
2522     return FALSE;
2523
2524   return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
2525 }
2526
2527 bfd_boolean
2528 tilegx_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2529                                   struct bfd_link_info *info)
2530 {
2531   struct tilegx_elf_link_hash_table *htab;
2532   bfd *dynobj;
2533   asection *s;
2534   bfd *ibfd;
2535
2536   htab = tilegx_elf_hash_table (info);
2537   BFD_ASSERT (htab != NULL);
2538   dynobj = htab->elf.dynobj;
2539   BFD_ASSERT (dynobj != NULL);
2540
2541   if (elf_hash_table (info)->dynamic_sections_created)
2542     {
2543       /* Set the contents of the .interp section to the interpreter.  */
2544       if (info->executable)
2545         {
2546           s = bfd_get_section_by_name (dynobj, ".interp");
2547           BFD_ASSERT (s != NULL);
2548           s->size = strlen (htab->dynamic_interpreter) + 1;
2549           s->contents = (unsigned char *) htab->dynamic_interpreter;
2550         }
2551     }
2552
2553   /* Set up .got offsets for local syms, and space for local dynamic
2554      relocs.  */
2555   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2556     {
2557       bfd_signed_vma *local_got;
2558       bfd_signed_vma *end_local_got;
2559       char *local_tls_type;
2560       bfd_size_type locsymcount;
2561       Elf_Internal_Shdr *symtab_hdr;
2562       asection *srel;
2563
2564       if (! is_tilegx_elf (ibfd))
2565         continue;
2566
2567       for (s = ibfd->sections; s != NULL; s = s->next)
2568         {
2569           struct tilegx_elf_dyn_relocs *p;
2570
2571           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
2572             {
2573               if (!bfd_is_abs_section (p->sec)
2574                   && bfd_is_abs_section (p->sec->output_section))
2575                 {
2576                   /* Input section has been discarded, either because
2577                      it is a copy of a linkonce section or due to
2578                      linker script /DISCARD/, so we'll be discarding
2579                      the relocs too.  */
2580                 }
2581               else if (p->count != 0)
2582                 {
2583                   srel = elf_section_data (p->sec)->sreloc;
2584                   srel->size += p->count * TILEGX_ELF_RELA_BYTES (htab);
2585                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2586                     info->flags |= DF_TEXTREL;
2587                 }
2588             }
2589         }
2590
2591       local_got = elf_local_got_refcounts (ibfd);
2592       if (!local_got)
2593         continue;
2594
2595       symtab_hdr = &elf_symtab_hdr (ibfd);
2596       locsymcount = symtab_hdr->sh_info;
2597       end_local_got = local_got + locsymcount;
2598       local_tls_type = _bfd_tilegx_elf_local_got_tls_type (ibfd);
2599       s = htab->elf.sgot;
2600       srel = htab->elf.srelgot;
2601       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2602         {
2603           if (*local_got > 0)
2604             {
2605               *local_got = s->size;
2606               s->size += TILEGX_ELF_WORD_BYTES (htab);
2607               if (*local_tls_type == GOT_TLS_GD)
2608                 s->size += TILEGX_ELF_WORD_BYTES (htab);
2609               if (info->shared
2610                   || *local_tls_type == GOT_TLS_GD
2611                   || *local_tls_type == GOT_TLS_IE)
2612                 srel->size += TILEGX_ELF_RELA_BYTES (htab);
2613             }
2614           else
2615             *local_got = (bfd_vma) -1;
2616         }
2617     }
2618
2619   /* Allocate global sym .plt and .got entries, and space for global
2620      sym dynamic relocs.  */
2621   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2622
2623   if (elf_hash_table (info)->dynamic_sections_created)
2624     {
2625       /* If the .got section is more than 0x8000 bytes, we add
2626          0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
2627          bit relocations have a greater chance of working. */
2628       if (htab->elf.sgot->size >= 0x8000
2629           && elf_hash_table (info)->hgot->root.u.def.value == 0)
2630         elf_hash_table (info)->hgot->root.u.def.value = 0x8000;
2631     }
2632
2633   if (htab->elf.sgotplt)
2634     {
2635       struct elf_link_hash_entry *got;
2636       got = elf_link_hash_lookup (elf_hash_table (info),
2637                                   "_GLOBAL_OFFSET_TABLE_",
2638                                   FALSE, FALSE, FALSE);
2639
2640       /* Don't allocate .got.plt section if there are no GOT nor PLT
2641          entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
2642       if ((got == NULL
2643            || !got->ref_regular_nonweak)
2644           && (htab->elf.sgotplt->size
2645               == (unsigned)GOTPLT_HEADER_SIZE (htab))
2646           && (htab->elf.splt == NULL
2647               || htab->elf.splt->size == 0)
2648           && (htab->elf.sgot == NULL
2649               || (htab->elf.sgot->size
2650                   == get_elf_backend_data (output_bfd)->got_header_size)))
2651         htab->elf.sgotplt->size = 0;
2652     }
2653
2654   /* The check_relocs and adjust_dynamic_symbol entry points have
2655      determined the sizes of the various dynamic sections.  Allocate
2656      memory for them.  */
2657   for (s = dynobj->sections; s != NULL; s = s->next)
2658     {
2659       if ((s->flags & SEC_LINKER_CREATED) == 0)
2660         continue;
2661
2662       if (s == htab->elf.splt
2663           || s == htab->elf.sgot
2664           || s == htab->elf.sgotplt
2665           || s == htab->sdynbss)
2666         {
2667           /* Strip this section if we don't need it; see the
2668              comment below.  */
2669         }
2670       else if (strncmp (s->name, ".rela", 5) == 0)
2671         {
2672           if (s->size != 0)
2673             {
2674               /* We use the reloc_count field as a counter if we need
2675                  to copy relocs into the output file.  */
2676               s->reloc_count = 0;
2677             }
2678         }
2679       else
2680         {
2681           /* It's not one of our sections.  */
2682           continue;
2683         }
2684
2685       if (s->size == 0)
2686         {
2687           /* If we don't need this section, strip it from the
2688              output file.  This is mostly to handle .rela.bss and
2689              .rela.plt.  We must create both sections in
2690              create_dynamic_sections, because they must be created
2691              before the linker maps input sections to output
2692              sections.  The linker does that before
2693              adjust_dynamic_symbol is called, and it is that
2694              function which decides whether anything needs to go
2695              into these sections.  */
2696           s->flags |= SEC_EXCLUDE;
2697           continue;
2698         }
2699
2700       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2701         continue;
2702
2703       /* Allocate memory for the section contents.  Zero the memory
2704          for the benefit of .rela.plt, which has 4 unused entries
2705          at the beginning, and we don't want garbage.  */
2706       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2707       if (s->contents == NULL)
2708         return FALSE;
2709     }
2710
2711   if (elf_hash_table (info)->dynamic_sections_created)
2712     {
2713       /* Add some entries to the .dynamic section.  We fill in the
2714          values later, in tilegx_elf_finish_dynamic_sections, but we
2715          must add the entries now so that we get the correct size for
2716          the .dynamic section.  The DT_DEBUG entry is filled in by the
2717          dynamic linker and used by the debugger.  */
2718 #define add_dynamic_entry(TAG, VAL) \
2719   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2720
2721       if (info->executable)
2722         {
2723           if (!add_dynamic_entry (DT_DEBUG, 0))
2724             return FALSE;
2725         }
2726
2727       if (htab->elf.srelplt->size != 0)
2728         {
2729           if (!add_dynamic_entry (DT_PLTGOT, 0)
2730               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2731               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2732               || !add_dynamic_entry (DT_JMPREL, 0))
2733             return FALSE;
2734         }
2735
2736       if (!add_dynamic_entry (DT_RELA, 0)
2737           || !add_dynamic_entry (DT_RELASZ, 0)
2738           || !add_dynamic_entry (DT_RELAENT, TILEGX_ELF_RELA_BYTES (htab)))
2739         return FALSE;
2740
2741       /* If any dynamic relocs apply to a read-only section,
2742          then we need a DT_TEXTREL entry.  */
2743       if ((info->flags & DF_TEXTREL) == 0)
2744         elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2745
2746       if (info->flags & DF_TEXTREL)
2747         {
2748           if (!add_dynamic_entry (DT_TEXTREL, 0))
2749             return FALSE;
2750         }
2751     }
2752 #undef add_dynamic_entry
2753
2754   return TRUE;
2755 }
2756 \f
2757 /* Return the base VMA address which should be subtracted from real addresses
2758    when resolving @dtpoff relocation.
2759    This is PT_TLS segment p_vaddr.  */
2760
2761 static bfd_vma
2762 dtpoff_base (struct bfd_link_info *info)
2763 {
2764   /* If tls_sec is NULL, we should have signalled an error already.  */
2765   if (elf_hash_table (info)->tls_sec == NULL)
2766     return 0;
2767   return elf_hash_table (info)->tls_sec->vma;
2768 }
2769
2770 /* Return the relocation value for @tpoff relocation. */
2771
2772 static bfd_vma
2773 tpoff (struct bfd_link_info *info, bfd_vma address)
2774 {
2775   struct elf_link_hash_table *htab = elf_hash_table (info);
2776
2777   /* If tls_sec is NULL, we should have signalled an error already.  */
2778   if (htab->tls_sec == NULL)
2779     return 0;
2780
2781   return (address - htab->tls_sec->vma);
2782 }
2783
2784 /* Relocate an TILEGX ELF section.
2785
2786    The RELOCATE_SECTION function is called by the new ELF backend linker
2787    to handle the relocations for a section.
2788
2789    The relocs are always passed as Rela structures.
2790
2791    This function is responsible for adjusting the section contents as
2792    necessary, and (if generating a relocatable output file) adjusting
2793    the reloc addend as necessary.
2794
2795    This function does not have to worry about setting the reloc
2796    address or the reloc symbol index.
2797
2798    LOCAL_SYMS is a pointer to the swapped in local symbols.
2799
2800    LOCAL_SECTIONS is an array giving the section in the input file
2801    corresponding to the st_shndx field of each local symbol.
2802
2803    The global hash table entry for the global symbols can be found
2804    via elf_sym_hashes (input_bfd).
2805
2806    When generating relocatable output, this function must handle
2807    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2808    going to be the section symbol corresponding to the output
2809    section, which means that the addend must be adjusted
2810    accordingly.  */
2811
2812 bfd_boolean
2813 tilegx_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2814                              bfd *input_bfd, asection *input_section,
2815                              bfd_byte *contents, Elf_Internal_Rela *relocs,
2816                              Elf_Internal_Sym *local_syms,
2817                              asection **local_sections)
2818 {
2819   struct tilegx_elf_link_hash_table *htab;
2820   Elf_Internal_Shdr *symtab_hdr;
2821   struct elf_link_hash_entry **sym_hashes;
2822   bfd_vma *local_got_offsets;
2823   bfd_vma got_base;
2824   asection *sreloc;
2825   Elf_Internal_Rela *rel;
2826   Elf_Internal_Rela *relend;
2827   int num_relocs;
2828
2829   htab = tilegx_elf_hash_table (info);
2830   BFD_ASSERT (htab != NULL);
2831   symtab_hdr = &elf_symtab_hdr (input_bfd);
2832   sym_hashes = elf_sym_hashes (input_bfd);
2833   local_got_offsets = elf_local_got_offsets (input_bfd);
2834
2835   if (elf_hash_table (info)->hgot == NULL)
2836     got_base = 0;
2837   else
2838     got_base = elf_hash_table (info)->hgot->root.u.def.value;
2839
2840   sreloc = elf_section_data (input_section)->sreloc;
2841
2842   rel = relocs;
2843   num_relocs = input_section->reloc_count;
2844   relend = relocs + num_relocs;
2845   for (; rel < relend; rel++)
2846     {
2847       int r_type, tls_type;
2848       reloc_howto_type *howto;
2849       unsigned long r_symndx;
2850       struct elf_link_hash_entry *h;
2851       Elf_Internal_Sym *sym;
2852       tilegx_create_func create_func;
2853       asection *sec;
2854       bfd_vma relocation;
2855       bfd_reloc_status_type r;
2856       const char *name;
2857       bfd_vma off;
2858       bfd_boolean is_plt = FALSE;
2859
2860       bfd_boolean unresolved_reloc;
2861
2862       r_type = TILEGX_ELF_R_TYPE (rel->r_info);
2863       if (r_type == R_TILEGX_GNU_VTINHERIT
2864           || r_type == R_TILEGX_GNU_VTENTRY)
2865         continue;
2866
2867       if ((unsigned int)r_type >= ARRAY_SIZE (tilegx_elf_howto_table))
2868         {
2869           /* Not clear if we need to check here, but just be paranoid. */
2870           (*_bfd_error_handler)
2871             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2872              input_bfd, r_type, input_section);
2873           bfd_set_error (bfd_error_bad_value);
2874           return FALSE;
2875         }
2876
2877       howto = tilegx_elf_howto_table + r_type;
2878
2879       /* This is a final link.  */
2880       r_symndx = TILEGX_ELF_R_SYMNDX (htab, rel->r_info);
2881       h = NULL;
2882       sym = NULL;
2883       sec = NULL;
2884       unresolved_reloc = FALSE;
2885       if (r_symndx < symtab_hdr->sh_info)
2886         {
2887           sym = local_syms + r_symndx;
2888           sec = local_sections[r_symndx];
2889           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2890         }
2891       else
2892         {
2893           bfd_boolean warned;
2894
2895           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2896                                    r_symndx, symtab_hdr, sym_hashes,
2897                                    h, sec, relocation,
2898                                    unresolved_reloc, warned);
2899           if (warned)
2900             {
2901               /* To avoid generating warning messages about truncated
2902                  relocations, set the relocation's address to be the same as
2903                  the start of this section.  */
2904               if (input_section->output_section != NULL)
2905                 relocation = input_section->output_section->vma;
2906               else
2907                 relocation = 0;
2908             }
2909         }
2910
2911       if (sec != NULL && elf_discarded_section (sec))
2912         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2913                                          rel, relend, howto, contents);
2914
2915       if (info->relocatable)
2916         continue;
2917
2918       if (h != NULL)
2919         name = h->root.root.string;
2920       else
2921         {
2922           name = (bfd_elf_string_from_elf_section
2923                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
2924           if (name == NULL || *name == '\0')
2925             name = bfd_section_name (input_bfd, sec);
2926         }
2927
2928       switch (r_type)
2929         {
2930         case R_TILEGX_IMM16_X0_HW0_GOT:
2931         case R_TILEGX_IMM16_X1_HW0_GOT:
2932         case R_TILEGX_IMM16_X0_HW1_GOT:
2933         case R_TILEGX_IMM16_X1_HW1_GOT:
2934         case R_TILEGX_IMM16_X0_HW2_GOT:
2935         case R_TILEGX_IMM16_X1_HW2_GOT:
2936         case R_TILEGX_IMM16_X0_HW3_GOT:
2937         case R_TILEGX_IMM16_X1_HW3_GOT:
2938         case R_TILEGX_IMM16_X0_HW0_LAST_GOT:
2939         case R_TILEGX_IMM16_X1_HW0_LAST_GOT:
2940         case R_TILEGX_IMM16_X0_HW1_LAST_GOT:
2941         case R_TILEGX_IMM16_X1_HW1_LAST_GOT:
2942         case R_TILEGX_IMM16_X0_HW2_LAST_GOT:
2943         case R_TILEGX_IMM16_X1_HW2_LAST_GOT:
2944           /* Relocation is to the entry for this symbol in the global
2945              offset table.  */
2946           if (htab->elf.sgot == NULL)
2947             abort ();
2948
2949           if (h != NULL)
2950             {
2951               bfd_boolean dyn;
2952
2953               off = h->got.offset;
2954               BFD_ASSERT (off != (bfd_vma) -1);
2955               dyn = elf_hash_table (info)->dynamic_sections_created;
2956
2957               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2958                   || (info->shared
2959                       && SYMBOL_REFERENCES_LOCAL (info, h)))
2960                 {
2961                   /* This is actually a static link, or it is a
2962                      -Bsymbolic link and the symbol is defined
2963                      locally, or the symbol was forced to be local
2964                      because of a version file.  We must initialize
2965                      this entry in the global offset table.  Since the
2966                      offset must always be a multiple
2967                      of 8 for 64-bit, we use the least significant bit
2968                      to record whether we have initialized it already.
2969
2970                      When doing a dynamic link, we create a .rela.got
2971                      relocation entry to initialize the value.  This
2972                      is done in the finish_dynamic_symbol routine.  */
2973                   if ((off & 1) != 0)
2974                     off &= ~1;
2975                   else
2976                     {
2977                       TILEGX_ELF_PUT_WORD (htab, output_bfd, relocation,
2978                                            htab->elf.sgot->contents + off);
2979                       h->got.offset |= 1;
2980                     }
2981                 }
2982               else
2983                 unresolved_reloc = FALSE;
2984             }
2985           else
2986             {
2987               BFD_ASSERT (local_got_offsets != NULL
2988                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
2989
2990               off = local_got_offsets[r_symndx];
2991
2992               /* The offset must always be a multiple of 8 on 64-bit.
2993                  We use the least significant bit to record
2994                  whether we have already processed this entry.  */
2995               if ((off & 1) != 0)
2996                 off &= ~1;
2997               else
2998                 {
2999                   if (info->shared)
3000                     {
3001                       asection *s;
3002                       Elf_Internal_Rela outrel;
3003
3004                       /* We need to generate a R_TILEGX_RELATIVE reloc
3005                          for the dynamic linker.  */
3006                       s = htab->elf.srelgot;
3007                       BFD_ASSERT (s != NULL);
3008
3009                       outrel.r_offset = (htab->elf.sgot->output_section->vma
3010                                          + htab->elf.sgot->output_offset
3011                                          + off);
3012                       outrel.r_info =
3013                         TILEGX_ELF_R_INFO (htab, NULL, 0, R_TILEGX_RELATIVE);
3014                       outrel.r_addend = relocation;
3015                       relocation = 0;
3016                       tilegx_elf_append_rela (output_bfd, s, &outrel);
3017                     }
3018
3019                   TILEGX_ELF_PUT_WORD (htab, output_bfd, relocation,
3020                                        htab->elf.sgot->contents + off);
3021                   local_got_offsets[r_symndx] |= 1;
3022                 }
3023             }
3024           relocation = htab->elf.sgot->output_offset + off - got_base;
3025           break;
3026
3027         case R_TILEGX_JUMPOFF_X1_PLT:
3028           /* Relocation is to the entry for this symbol in the
3029              procedure linkage table.  */
3030           BFD_ASSERT (h != NULL);
3031
3032           if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
3033             {
3034               /* We didn't make a PLT entry for this symbol.  This
3035                  happens when statically linking PIC code, or when
3036                  using -Bsymbolic.  */
3037               break;
3038             }
3039
3040           relocation = (htab->elf.splt->output_section->vma
3041                         + htab->elf.splt->output_offset
3042                         + h->plt.offset);
3043           unresolved_reloc = FALSE;
3044           break;
3045
3046         case R_TILEGX_64_PCREL:
3047         case R_TILEGX_32_PCREL:
3048         case R_TILEGX_16_PCREL:
3049         case R_TILEGX_8_PCREL:
3050         case R_TILEGX_IMM16_X0_HW0_PCREL:
3051         case R_TILEGX_IMM16_X1_HW0_PCREL:
3052         case R_TILEGX_IMM16_X0_HW1_PCREL:
3053         case R_TILEGX_IMM16_X1_HW1_PCREL:
3054         case R_TILEGX_IMM16_X0_HW2_PCREL:
3055         case R_TILEGX_IMM16_X1_HW2_PCREL:
3056         case R_TILEGX_IMM16_X0_HW3_PCREL:
3057         case R_TILEGX_IMM16_X1_HW3_PCREL:
3058         case R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3059         case R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3060         case R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3061         case R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3062         case R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3063         case R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3064           if (h != NULL
3065               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3066             break;
3067           /* Fall through.  */
3068         case R_TILEGX_64:
3069         case R_TILEGX_32:
3070         case R_TILEGX_16:
3071         case R_TILEGX_8:
3072         case R_TILEGX_HW0:
3073         case R_TILEGX_HW1:
3074         case R_TILEGX_HW2:
3075         case R_TILEGX_HW3:
3076         case R_TILEGX_HW0_LAST:
3077         case R_TILEGX_HW1_LAST:
3078         case R_TILEGX_HW2_LAST:
3079         case R_TILEGX_COPY:
3080         case R_TILEGX_GLOB_DAT:
3081         case R_TILEGX_JMP_SLOT:
3082         case R_TILEGX_RELATIVE:
3083         case R_TILEGX_BROFF_X1:
3084         case R_TILEGX_JUMPOFF_X1:
3085         case R_TILEGX_IMM8_X0:
3086         case R_TILEGX_IMM8_Y0:
3087         case R_TILEGX_IMM8_X1:
3088         case R_TILEGX_IMM8_Y1:
3089         case R_TILEGX_DEST_IMM8_X1:
3090         case R_TILEGX_MT_IMM14_X1:
3091         case R_TILEGX_MF_IMM14_X1:
3092         case R_TILEGX_MMSTART_X0:
3093         case R_TILEGX_MMEND_X0:
3094         case R_TILEGX_SHAMT_X0:
3095         case R_TILEGX_SHAMT_X1:
3096         case R_TILEGX_SHAMT_Y0:
3097         case R_TILEGX_SHAMT_Y1:
3098         case R_TILEGX_IMM16_X0_HW0:
3099         case R_TILEGX_IMM16_X1_HW0:
3100         case R_TILEGX_IMM16_X0_HW1:
3101         case R_TILEGX_IMM16_X1_HW1:
3102         case R_TILEGX_IMM16_X0_HW2:
3103         case R_TILEGX_IMM16_X1_HW2:
3104         case R_TILEGX_IMM16_X0_HW3:
3105         case R_TILEGX_IMM16_X1_HW3:
3106         case R_TILEGX_IMM16_X0_HW0_LAST:
3107         case R_TILEGX_IMM16_X1_HW0_LAST:
3108         case R_TILEGX_IMM16_X0_HW1_LAST:
3109         case R_TILEGX_IMM16_X1_HW1_LAST:
3110         case R_TILEGX_IMM16_X0_HW2_LAST:
3111         case R_TILEGX_IMM16_X1_HW2_LAST:
3112           if ((input_section->flags & SEC_ALLOC) == 0)
3113             break;
3114
3115           if ((info->shared
3116                && (h == NULL
3117                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3118                    || h->root.type != bfd_link_hash_undefweak)
3119                && (! howto->pc_relative
3120                    || !SYMBOL_CALLS_LOCAL (info, h)))
3121               || (!info->shared
3122                   && h != NULL
3123                   && h->dynindx != -1
3124                   && !h->non_got_ref
3125                   && ((h->def_dynamic
3126                        && !h->def_regular)
3127                       || h->root.type == bfd_link_hash_undefweak
3128                       || h->root.type == bfd_link_hash_undefined)))
3129             {
3130               Elf_Internal_Rela outrel;
3131               bfd_boolean skip, relocate = FALSE;
3132
3133               /* When generating a shared object, these relocations
3134                  are copied into the output file to be resolved at run
3135                  time.  */
3136
3137               BFD_ASSERT (sreloc != NULL);
3138
3139               skip = FALSE;
3140
3141               outrel.r_offset =
3142                 _bfd_elf_section_offset (output_bfd, info, input_section,
3143                                          rel->r_offset);
3144               if (outrel.r_offset == (bfd_vma) -1)
3145                 skip = TRUE;
3146               else if (outrel.r_offset == (bfd_vma) -2)
3147                 skip = TRUE, relocate = TRUE;
3148               outrel.r_offset += (input_section->output_section->vma
3149                                   + input_section->output_offset);
3150
3151               switch (r_type)
3152                 {
3153                 case R_TILEGX_64_PCREL:
3154                 case R_TILEGX_32_PCREL:
3155                 case R_TILEGX_16_PCREL:
3156                 case R_TILEGX_8_PCREL:
3157                   /* If the symbol is not dynamic, we should not keep
3158                      a dynamic relocation.  But an .rela.* slot has been
3159                      allocated for it, output R_TILEGX_NONE.
3160                      FIXME: Add code tracking needed dynamic relocs as
3161                      e.g. i386 has.  */
3162                   if (h->dynindx == -1)
3163                     skip = TRUE, relocate = TRUE;
3164                   break;
3165                 }
3166
3167               if (skip)
3168                 memset (&outrel, 0, sizeof outrel);
3169               /* h->dynindx may be -1 if the symbol was marked to
3170                  become local.  */
3171               else if (h != NULL &&
3172                        h->dynindx != -1
3173                        && (! is_plt
3174                            || !info->shared
3175                            || !SYMBOLIC_BIND (info, h)
3176                            || !h->def_regular))
3177                 {
3178                   BFD_ASSERT (h->dynindx != -1);
3179                   outrel.r_info = TILEGX_ELF_R_INFO (htab, rel, h->dynindx, r_type);
3180                   outrel.r_addend = rel->r_addend;
3181                 }
3182               else
3183                 {
3184                   if (r_type == R_TILEGX_32 || r_type == R_TILEGX_64)
3185                     {
3186                       outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, 0,
3187                                                          R_TILEGX_RELATIVE);
3188                       outrel.r_addend = relocation + rel->r_addend;
3189                     }
3190                   else
3191                     {
3192                       long indx;
3193
3194                       outrel.r_addend = relocation + rel->r_addend;
3195
3196                       if (is_plt)
3197                         sec = htab->elf.splt;
3198
3199                       if (bfd_is_abs_section (sec))
3200                         indx = 0;
3201                       else if (sec == NULL || sec->owner == NULL)
3202                         {
3203                           bfd_set_error (bfd_error_bad_value);
3204                           return FALSE;
3205                         }
3206                       else
3207                         {
3208                           asection *osec;
3209
3210                           /* We are turning this relocation into one
3211                              against a section symbol.  It would be
3212                              proper to subtract the symbol's value,
3213                              osec->vma, from the emitted reloc addend,
3214                              but ld.so expects buggy relocs.  */
3215                           osec = sec->output_section;
3216                           indx = elf_section_data (osec)->dynindx;
3217
3218                           if (indx == 0)
3219                             {
3220                               osec = htab->elf.text_index_section;
3221                               indx = elf_section_data (osec)->dynindx;
3222                             }
3223
3224                           /* FIXME: we really should be able to link non-pic
3225                              shared libraries.  */
3226                           if (indx == 0)
3227                             {
3228                               BFD_FAIL ();
3229                               (*_bfd_error_handler)
3230                                 (_("%B: probably compiled without -fPIC?"),
3231                                  input_bfd);
3232                               bfd_set_error (bfd_error_bad_value);
3233                               return FALSE;
3234                             }
3235                         }
3236
3237                       outrel.r_info = TILEGX_ELF_R_INFO (htab, rel, indx,
3238                                                          r_type);
3239                     }
3240                 }
3241
3242               tilegx_elf_append_rela (output_bfd, sreloc, &outrel);
3243
3244               /* This reloc will be computed at runtime, so there's no
3245                  need to do anything now.  */
3246               if (! relocate)
3247                 continue;
3248             }
3249           break;
3250
3251         case R_TILEGX_IMM16_X0_HW0_TLS_GD:
3252         case R_TILEGX_IMM16_X1_HW0_TLS_GD:
3253         case R_TILEGX_IMM16_X0_HW1_TLS_GD:
3254         case R_TILEGX_IMM16_X1_HW1_TLS_GD:
3255         case R_TILEGX_IMM16_X0_HW2_TLS_GD:
3256         case R_TILEGX_IMM16_X1_HW2_TLS_GD:
3257         case R_TILEGX_IMM16_X0_HW3_TLS_GD:
3258         case R_TILEGX_IMM16_X1_HW3_TLS_GD:
3259         case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3260         case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3261         case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3262         case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3263         case R_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
3264         case R_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
3265           tls_type = GOT_TLS_GD;
3266           goto have_tls_reference;
3267
3268         case R_TILEGX_IMM16_X0_HW0_TLS_IE:
3269         case R_TILEGX_IMM16_X1_HW0_TLS_IE:
3270         case R_TILEGX_IMM16_X0_HW1_TLS_IE:
3271         case R_TILEGX_IMM16_X1_HW1_TLS_IE:
3272         case R_TILEGX_IMM16_X0_HW2_TLS_IE:
3273         case R_TILEGX_IMM16_X1_HW2_TLS_IE:
3274         case R_TILEGX_IMM16_X0_HW3_TLS_IE:
3275         case R_TILEGX_IMM16_X1_HW3_TLS_IE:
3276         case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3277         case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3278         case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3279         case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3280         case R_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
3281         case R_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
3282           tls_type = GOT_TLS_IE;
3283           /* Fall through. */
3284
3285         have_tls_reference:
3286           if (h == NULL && local_got_offsets)
3287             tls_type = _bfd_tilegx_elf_local_got_tls_type (input_bfd) [r_symndx];
3288           else if (h != NULL)
3289             tls_type = tilegx_elf_hash_entry(h)->tls_type;
3290
3291           if (tls_type == GOT_TLS_IE)
3292             switch (r_type)
3293               {
3294               case R_TILEGX_IMM16_X0_HW0_TLS_GD:
3295                 r_type = R_TILEGX_IMM16_X0_HW0_TLS_IE;
3296                 break;
3297               case R_TILEGX_IMM16_X1_HW0_TLS_GD:
3298                 r_type = R_TILEGX_IMM16_X1_HW0_TLS_IE;
3299                 break;
3300               case R_TILEGX_IMM16_X0_HW1_TLS_GD:
3301                 r_type = R_TILEGX_IMM16_X0_HW1_TLS_IE;
3302                 break;
3303               case R_TILEGX_IMM16_X1_HW1_TLS_GD:
3304                 r_type = R_TILEGX_IMM16_X1_HW1_TLS_IE;
3305                 break;
3306               case R_TILEGX_IMM16_X0_HW2_TLS_GD:
3307                 r_type = R_TILEGX_IMM16_X0_HW2_TLS_IE;
3308                 break;
3309               case R_TILEGX_IMM16_X1_HW2_TLS_GD:
3310                 r_type = R_TILEGX_IMM16_X1_HW2_TLS_IE;
3311                 break;
3312               case R_TILEGX_IMM16_X0_HW3_TLS_GD:
3313                 r_type = R_TILEGX_IMM16_X0_HW3_TLS_IE;
3314                 break;
3315               case R_TILEGX_IMM16_X1_HW3_TLS_GD:
3316                 r_type = R_TILEGX_IMM16_X1_HW3_TLS_IE;
3317                 break;
3318               case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3319                 r_type = R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE;
3320                 break;
3321               case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3322                 r_type = R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE;
3323                 break;
3324               case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3325                 r_type = R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE;
3326                 break;
3327               case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3328                 r_type = R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE;
3329                 break;
3330               case R_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
3331                 r_type = R_TILEGX_IMM16_X0_HW2_LAST_TLS_IE;
3332                 break;
3333               case R_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
3334                 r_type = R_TILEGX_IMM16_X1_HW2_LAST_TLS_IE;
3335                 break;
3336               }
3337
3338           if (h != NULL)
3339             {
3340               off = h->got.offset;
3341               h->got.offset |= 1;
3342             }
3343           else
3344             {
3345               BFD_ASSERT (local_got_offsets != NULL);
3346               off = local_got_offsets[r_symndx];
3347               local_got_offsets[r_symndx] |= 1;
3348             }
3349
3350           if (htab->elf.sgot == NULL)
3351             abort ();
3352
3353           if ((off & 1) != 0)
3354             off &= ~1;
3355           else
3356             {
3357               Elf_Internal_Rela outrel;
3358               int indx = 0;
3359               bfd_boolean need_relocs = FALSE;
3360
3361               if (htab->elf.srelgot == NULL)
3362                 abort ();
3363
3364               if (h != NULL)
3365               {
3366                 bfd_boolean dyn;
3367                 dyn = htab->elf.dynamic_sections_created;
3368
3369                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3370                     && (!info->shared
3371                         || !SYMBOL_REFERENCES_LOCAL (info, h)))
3372                   {
3373                     indx = h->dynindx;
3374                   }
3375               }
3376
3377               /* The GOT entries have not been initialized yet.  Do it
3378                  now, and emit any relocations. */
3379               if ((info->shared || indx != 0)
3380                   && (h == NULL
3381                       || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3382                       || h->root.type != bfd_link_hash_undefweak))
3383                     need_relocs = TRUE;
3384
3385               switch (r_type)
3386                 {
3387                   case R_TILEGX_IMM16_X0_HW0_TLS_IE:
3388                   case R_TILEGX_IMM16_X1_HW0_TLS_IE:
3389                   case R_TILEGX_IMM16_X0_HW1_TLS_IE:
3390                   case R_TILEGX_IMM16_X1_HW1_TLS_IE:
3391                   case R_TILEGX_IMM16_X0_HW2_TLS_IE:
3392                   case R_TILEGX_IMM16_X1_HW2_TLS_IE:
3393                   case R_TILEGX_IMM16_X0_HW3_TLS_IE:
3394                   case R_TILEGX_IMM16_X1_HW3_TLS_IE:
3395                   case R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3396                   case R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3397                   case R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3398                   case R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3399                   case R_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
3400                   case R_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
3401                     if (need_relocs) {
3402                       TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
3403                                            htab->elf.sgot->contents + off);
3404                       outrel.r_offset = (htab->elf.sgot->output_section->vma
3405                                        + htab->elf.sgot->output_offset + off);
3406                       outrel.r_addend = 0;
3407                       if (indx == 0)
3408                         outrel.r_addend = relocation - dtpoff_base (info);
3409                       outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
3410                                                          TILEGX_ELF_TPOFF_RELOC (htab));
3411                       tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3412                     } else {
3413                       TILEGX_ELF_PUT_WORD (htab, output_bfd,
3414                                            tpoff (info, relocation),
3415                                            htab->elf.sgot->contents + off);
3416                     }
3417                     break;
3418
3419                   case R_TILEGX_IMM16_X0_HW0_TLS_GD:
3420                   case R_TILEGX_IMM16_X1_HW0_TLS_GD:
3421                   case R_TILEGX_IMM16_X0_HW1_TLS_GD:
3422                   case R_TILEGX_IMM16_X1_HW1_TLS_GD:
3423                   case R_TILEGX_IMM16_X0_HW2_TLS_GD:
3424                   case R_TILEGX_IMM16_X1_HW2_TLS_GD:
3425                   case R_TILEGX_IMM16_X0_HW3_TLS_GD:
3426                   case R_TILEGX_IMM16_X1_HW3_TLS_GD:
3427                   case R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3428                   case R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3429                   case R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3430                   case R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3431                   case R_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
3432                   case R_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
3433                     if (need_relocs) {
3434                       outrel.r_offset = (htab->elf.sgot->output_section->vma
3435                                        + htab->elf.sgot->output_offset + off);
3436                       outrel.r_addend = 0;
3437                       outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
3438                                                          TILEGX_ELF_DTPMOD_RELOC (htab));
3439                       TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
3440                                            htab->elf.sgot->contents + off);
3441                       tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3442                       if (indx == 0)
3443                         {
3444                           BFD_ASSERT (! unresolved_reloc);
3445                           TILEGX_ELF_PUT_WORD (htab, output_bfd,
3446                                                relocation - dtpoff_base (info),
3447                                                (htab->elf.sgot->contents + off +
3448                                                 TILEGX_ELF_WORD_BYTES (htab)));
3449                         }
3450                       else
3451                         {
3452                           TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
3453                                                (htab->elf.sgot->contents + off +
3454                                                 TILEGX_ELF_WORD_BYTES (htab)));
3455                           outrel.r_info = TILEGX_ELF_R_INFO (htab, NULL, indx,
3456                                                              TILEGX_ELF_DTPOFF_RELOC (htab));
3457                           outrel.r_offset += TILEGX_ELF_WORD_BYTES (htab);
3458                           tilegx_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3459                         }
3460                     }
3461
3462                     else {
3463                       /* If we are not emitting relocations for a
3464                          general dynamic reference, then we must be in a
3465                          static link or an executable link with the
3466                          symbol binding locally.  Mark it as belonging
3467                          to module 1, the executable.  */
3468                       TILEGX_ELF_PUT_WORD (htab, output_bfd, 1,
3469                                            htab->elf.sgot->contents + off );
3470                       TILEGX_ELF_PUT_WORD (htab, output_bfd,
3471                                            relocation - dtpoff_base (info),
3472                                            htab->elf.sgot->contents + off +
3473                                            TILEGX_ELF_WORD_BYTES (htab));
3474                    }
3475                    break;
3476                 }
3477             }
3478
3479           if (off >= (bfd_vma) -2)
3480             abort ();
3481
3482           relocation = htab->elf.sgot->output_offset + off - got_base;
3483           unresolved_reloc = FALSE;
3484           howto = tilegx_elf_howto_table + r_type;
3485           break;
3486
3487         default:
3488           break;
3489         }
3490
3491       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3492          because such sections are not SEC_ALLOC and thus ld.so will
3493          not process them.  */
3494       if (unresolved_reloc
3495           && !((input_section->flags & SEC_DEBUGGING) != 0
3496                && h->def_dynamic)
3497           && _bfd_elf_section_offset (output_bfd, info, input_section,
3498                                       rel->r_offset) != (bfd_vma) -1)
3499         (*_bfd_error_handler)
3500           (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3501            input_bfd,
3502            input_section,
3503            (long) rel->r_offset,
3504            howto->name,
3505            h->root.root.string);
3506
3507       r = bfd_reloc_continue;
3508
3509       /* Get the operand creation function, if any. */
3510       create_func = reloc_to_create_func[r_type];
3511       if (create_func == NULL)
3512       {
3513         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3514                                       contents, rel->r_offset,
3515                                       relocation, rel->r_addend);
3516       }
3517       else
3518       {
3519         if (howto->pc_relative)
3520         {
3521           relocation -=
3522             input_section->output_section->vma + input_section->output_offset;
3523           if (howto->pcrel_offset)
3524             relocation -= rel->r_offset;
3525         }
3526
3527         bfd_byte *data;
3528
3529         /* Add the relocation addend if any to the final target value */
3530         relocation += rel->r_addend;
3531
3532         /* Do basic range checking */
3533         r = bfd_check_overflow (howto->complain_on_overflow,
3534                                 howto->bitsize,
3535                                 howto->rightshift,
3536                                 TILEGX_ELF_WORD_BYTES (htab) * 8,
3537                                 relocation);
3538
3539         /*
3540          * Write the relocated value out into the raw section data.
3541          * Don't put a relocation out in the .rela section.
3542          */
3543         tilegx_bundle_bits mask = create_func(-1);
3544         tilegx_bundle_bits value = create_func(relocation >> howto->rightshift);
3545
3546         /* Only touch bytes while the mask is not 0, so we
3547            don't write to out of bounds memory if this is actually
3548            a 16-bit switch instruction. */
3549         for (data = contents + rel->r_offset; mask != 0; data++)
3550           {
3551             bfd_byte byte_mask = (bfd_byte)mask;
3552             *data = (*data & ~byte_mask) | ((bfd_byte)value & byte_mask);
3553             mask >>= 8;
3554             value >>= 8;
3555           }
3556       }
3557
3558       if (r != bfd_reloc_ok)
3559         {
3560           const char *msg = NULL;
3561
3562           switch (r)
3563             {
3564             case bfd_reloc_overflow:
3565               r = info->callbacks->reloc_overflow
3566                 (info, (h ? &h->root : NULL), name, howto->name,
3567                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3568               break;
3569
3570             case bfd_reloc_undefined:
3571               r = info->callbacks->undefined_symbol
3572                 (info, name, input_bfd, input_section, rel->r_offset,
3573                  TRUE);
3574               break;
3575
3576             case bfd_reloc_outofrange:
3577               msg = _("internal error: out of range error");
3578               break;
3579
3580             case bfd_reloc_notsupported:
3581               msg = _("internal error: unsupported relocation error");
3582               break;
3583
3584             case bfd_reloc_dangerous:
3585               msg = _("internal error: dangerous relocation");
3586               break;
3587
3588             default:
3589               msg = _("internal error: unknown error");
3590               break;
3591             }
3592
3593           if (msg)
3594             r = info->callbacks->warning
3595               (info, msg, name, input_bfd, input_section, rel->r_offset);
3596
3597           if (! r)
3598             return FALSE;
3599         }
3600     }
3601
3602   return TRUE;
3603 }
3604
3605 /* Finish up dynamic symbol handling.  We set the contents of various
3606    dynamic sections here.  */
3607
3608 bfd_boolean
3609 tilegx_elf_finish_dynamic_symbol (bfd *output_bfd,
3610                                   struct bfd_link_info *info,
3611                                   struct elf_link_hash_entry *h,
3612                                   Elf_Internal_Sym *sym)
3613 {
3614   struct tilegx_elf_link_hash_table *htab;
3615
3616   htab = tilegx_elf_hash_table (info);
3617   BFD_ASSERT (htab != NULL);
3618
3619   if (h->plt.offset != (bfd_vma) -1)
3620     {
3621       asection *splt;
3622       asection *srela;
3623       asection *sgotplt;
3624       Elf_Internal_Rela rela;
3625       bfd_byte *loc;
3626       bfd_vma r_offset;
3627       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
3628
3629
3630       int rela_index;
3631
3632       /* This symbol has an entry in the PLT.  Set it up.  */
3633
3634       BFD_ASSERT (h->dynindx != -1);
3635
3636       splt = htab->elf.splt;
3637       srela = htab->elf.srelplt;
3638       sgotplt = htab->elf.sgotplt;
3639
3640       if (splt == NULL || srela == NULL)
3641        abort ();
3642
3643       /* Fill in the entry in the procedure linkage table.  */
3644       rela_index = tilegx_plt_entry_build (output_bfd, htab, splt, sgotplt,
3645                                            h->plt.offset, &r_offset);
3646
3647       /* Fill in the entry in the global offset table, which initially points
3648          to the beginning of the plt.  */
3649       TILEGX_ELF_PUT_WORD (htab, output_bfd,
3650                            splt->output_section->vma + splt->output_offset,
3651                            sgotplt->contents + r_offset);
3652
3653       /* Fill in the entry in the .rela.plt section.  */
3654       rela.r_offset = (sgotplt->output_section->vma
3655                        + sgotplt->output_offset
3656                        + r_offset);
3657       rela.r_addend = 0;
3658       rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_JMP_SLOT);
3659
3660       loc = srela->contents + rela_index * TILEGX_ELF_RELA_BYTES (htab);
3661       bed->s->swap_reloca_out (output_bfd, &rela, loc);
3662
3663       if (!h->def_regular)
3664         {
3665           /* Mark the symbol as undefined, rather than as defined in
3666              the .plt section.  Leave the value alone.  */
3667           sym->st_shndx = SHN_UNDEF;
3668           /* If the symbol is weak, we do need to clear the value.
3669              Otherwise, the PLT entry would provide a definition for
3670              the symbol even if the symbol wasn't defined anywhere,
3671              and so the symbol would never be NULL.  */
3672           if (!h->ref_regular_nonweak)
3673             sym->st_value = 0;
3674         }
3675     }
3676
3677   if (h->got.offset != (bfd_vma) -1
3678       && tilegx_elf_hash_entry(h)->tls_type != GOT_TLS_GD
3679       && tilegx_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
3680     {
3681       asection *sgot;
3682       asection *srela;
3683       Elf_Internal_Rela rela;
3684
3685       /* This symbol has an entry in the GOT.  Set it up.  */
3686
3687       sgot = htab->elf.sgot;
3688       srela = htab->elf.srelgot;
3689       BFD_ASSERT (sgot != NULL && srela != NULL);
3690
3691       rela.r_offset = (sgot->output_section->vma
3692                        + sgot->output_offset
3693                        + (h->got.offset &~ (bfd_vma) 1));
3694
3695       /* If this is a -Bsymbolic link, and the symbol is defined
3696          locally, we just want to emit a RELATIVE reloc.  Likewise if
3697          the symbol was forced to be local because of a version file.
3698          The entry in the global offset table will already have been
3699          initialized in the relocate_section function.  */
3700       if (info->shared
3701           && (info->symbolic || h->dynindx == -1)
3702           && h->def_regular)
3703         {
3704           asection *sec = h->root.u.def.section;
3705           rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, 0, R_TILEGX_RELATIVE);
3706           rela.r_addend = (h->root.u.def.value
3707                            + sec->output_section->vma
3708                            + sec->output_offset);
3709         }
3710       else
3711         {
3712           rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_GLOB_DAT);
3713           rela.r_addend = 0;
3714         }
3715
3716       TILEGX_ELF_PUT_WORD (htab, output_bfd, 0,
3717                            sgot->contents + (h->got.offset & ~(bfd_vma) 1));
3718       tilegx_elf_append_rela (output_bfd, srela, &rela);
3719     }
3720
3721   if (h->needs_copy)
3722     {
3723       asection *s;
3724       Elf_Internal_Rela rela;
3725
3726       /* This symbols needs a copy reloc.  Set it up.  */
3727       BFD_ASSERT (h->dynindx != -1);
3728
3729       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3730                                    ".rela.bss");
3731       BFD_ASSERT (s != NULL);
3732
3733       rela.r_offset = (h->root.u.def.value
3734                        + h->root.u.def.section->output_section->vma
3735                        + h->root.u.def.section->output_offset);
3736       rela.r_info = TILEGX_ELF_R_INFO (htab, NULL, h->dynindx, R_TILEGX_COPY);
3737       rela.r_addend = 0;
3738       tilegx_elf_append_rela (output_bfd, s, &rela);
3739     }
3740
3741   /* Mark some specially defined symbols as absolute. */
3742   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3743       || (h == htab->elf.hgot || h == htab->elf.hplt))
3744     sym->st_shndx = SHN_ABS;
3745
3746   return TRUE;
3747 }
3748
3749 /* Finish up the dynamic sections.  */
3750
3751 static bfd_boolean
3752 tilegx_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3753                    bfd *dynobj, asection *sdyn,
3754                    asection *splt ATTRIBUTE_UNUSED)
3755 {
3756   struct tilegx_elf_link_hash_table *htab;
3757   const struct elf_backend_data *bed;
3758   bfd_byte *dyncon, *dynconend;
3759   size_t dynsize;
3760
3761   htab = tilegx_elf_hash_table (info);
3762   BFD_ASSERT (htab != NULL);
3763   bed = get_elf_backend_data (output_bfd);
3764   dynsize = bed->s->sizeof_dyn;
3765   dynconend = sdyn->contents + sdyn->size;
3766
3767   for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
3768     {
3769       Elf_Internal_Dyn dyn;
3770       asection *s;
3771
3772       bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
3773
3774       switch (dyn.d_tag)
3775         {
3776         case DT_PLTGOT:
3777           s = htab->elf.sgotplt;
3778           dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3779           break;
3780         case DT_JMPREL:
3781           s = htab->elf.srelplt;
3782           dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3783           break;
3784         case DT_PLTRELSZ:
3785           s = htab->elf.srelplt;
3786           dyn.d_un.d_val = s->size;
3787           break;
3788         default:
3789           continue;
3790         }
3791
3792       bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3793     }
3794   return TRUE;
3795 }
3796
3797 bfd_boolean
3798 tilegx_elf_finish_dynamic_sections (bfd *output_bfd,
3799                                     struct bfd_link_info *info)
3800 {
3801   bfd *dynobj;
3802   asection *sdyn;
3803   struct tilegx_elf_link_hash_table *htab;
3804
3805   htab = tilegx_elf_hash_table (info);
3806   BFD_ASSERT (htab != NULL);
3807   dynobj = htab->elf.dynobj;
3808
3809   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3810
3811   if (elf_hash_table (info)->dynamic_sections_created)
3812     {
3813       asection *splt;
3814       bfd_boolean ret;
3815
3816       splt = bfd_get_section_by_name (dynobj, ".plt");
3817       BFD_ASSERT (splt != NULL && sdyn != NULL);
3818
3819       ret = tilegx_finish_dyn (output_bfd, info, dynobj, sdyn, splt);
3820
3821       if (ret != TRUE)
3822         return ret;
3823
3824       /* Fill in the head and tail entries in the procedure linkage table.  */
3825       if (splt->size > 0)
3826         {
3827           memcpy (splt->contents,
3828                   ABI_64_P (output_bfd) ?
3829                     tilegx64_plt0_entry : tilegx32_plt0_entry,
3830                   PLT_HEADER_SIZE);
3831
3832           memcpy (splt->contents + splt->size - PLT_TAIL_SIZE,
3833                   ABI_64_P (output_bfd) ?
3834                     tilegx64_plt_tail_entry : tilegx32_plt_tail_entry,
3835                   PLT_TAIL_SIZE);
3836         }
3837
3838       elf_section_data (splt->output_section)->this_hdr.sh_entsize
3839         = PLT_ENTRY_SIZE;
3840     }
3841
3842   if (htab->elf.sgotplt)
3843     {
3844       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
3845         {
3846           (*_bfd_error_handler)
3847             (_("discarded output section: `%A'"), htab->elf.sgotplt);
3848           return FALSE;
3849         }
3850
3851       if (htab->elf.sgotplt->size > 0)
3852         {
3853           /* Write the first two entries in .got.plt, needed for the dynamic
3854              linker.  */
3855           TILEGX_ELF_PUT_WORD (htab, output_bfd, (bfd_vma) -1,
3856                                htab->elf.sgotplt->contents);
3857           TILEGX_ELF_PUT_WORD (htab, output_bfd, (bfd_vma) 0,
3858                                htab->elf.sgotplt->contents
3859                                + GOT_ENTRY_SIZE (htab));
3860         }
3861
3862       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
3863         GOT_ENTRY_SIZE (htab);
3864     }
3865
3866   if (htab->elf.sgot)
3867     {
3868       if (htab->elf.sgot->size > 0)
3869         {
3870           /* Set the first entry in the global offset table to the address of
3871              the dynamic section.  */
3872           bfd_vma val = (sdyn ?
3873                          sdyn->output_section->vma + sdyn->output_offset :
3874                          0);
3875           TILEGX_ELF_PUT_WORD (htab, output_bfd, val,
3876                                htab->elf.sgot->contents);
3877         }
3878
3879       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize =
3880         GOT_ENTRY_SIZE (htab);
3881     }
3882
3883   return TRUE;
3884 }
3885
3886 \f
3887
3888 /* Return address for Ith PLT stub in section PLT, for relocation REL
3889    or (bfd_vma) -1 if it should not be included.  */
3890
3891 bfd_vma
3892 tilegx_elf_plt_sym_val (bfd_vma i, const asection *plt,
3893                         const arelent *rel ATTRIBUTE_UNUSED)
3894 {
3895   return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
3896 }
3897
3898 enum elf_reloc_type_class
3899 tilegx_reloc_type_class (const Elf_Internal_Rela *rela)
3900 {
3901   switch ((int) TILEGX_ELF_R_TYPE (rela->r_info))
3902     {
3903     case R_TILEGX_RELATIVE:
3904       return reloc_class_relative;
3905     case R_TILEGX_JMP_SLOT:
3906       return reloc_class_plt;
3907     case R_TILEGX_COPY:
3908       return reloc_class_copy;
3909     default:
3910       return reloc_class_normal;
3911     }
3912 }
3913
3914 int
3915 tilegx_additional_program_headers (bfd *abfd,
3916                                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
3917 {
3918   /* Each .intrpt section specified by the user adds another PT_LOAD
3919      header since the sections are discontiguous. */
3920   static const char intrpt_sections[4][9] =
3921     {
3922       ".intrpt0", ".intrpt1", ".intrpt2", ".intrpt3"
3923     };
3924   int count = 0;
3925   int i;
3926
3927   for (i = 0; i < 4; i++)
3928     {
3929       asection *sec = bfd_get_section_by_name (abfd, intrpt_sections[i]);
3930       if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
3931         ++count;
3932     }
3933
3934   /* Add four "padding" headers in to leave room in case a custom linker
3935      script does something fancy. Otherwise ld complains that it ran
3936      out of program headers and refuses to link. */
3937   count += 4;
3938
3939   return count;
3940 }
3941
3942
3943 bfd_boolean
3944 _bfd_tilegx_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3945 {
3946   const char *targ1 = bfd_get_target (ibfd);
3947   const char *targ2 = bfd_get_target (obfd);
3948
3949   if (strcmp (targ1, targ2) != 0)
3950     {
3951       (*_bfd_error_handler)
3952         (_("%B: Cannot link together %s and %s objects."),
3953          ibfd, targ1, targ2);
3954       bfd_set_error (bfd_error_bad_value);
3955       return FALSE;
3956     }
3957
3958   return TRUE;
3959 }