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