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