1 /* 32-bit ELF support for TI C6X
2    Copyright (C) 2010-2020 Free Software Foundation, Inc.
3    Contributed by Joseph Myers <joseph@codesourcery.com>
4 		  Bernd Schmidt  <bernds@codesourcery.com>
5 
6    This file is part of BFD, the Binary File Descriptor library.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22 
23 #include "sysdep.h"
24 #include <limits.h>
25 #include "bfd.h"
26 #include "libbfd.h"
27 #include "libiberty.h"
28 #include "elf-bfd.h"
29 #include "elf/tic6x.h"
30 #include "elf32-tic6x.h"
31 
32 #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
33 
34 /* DSBT binaries have a default 128K stack.  */
35 #define DEFAULT_STACK_SIZE 0x20000
36 
37 /* The size in bytes of an entry in the procedure linkage table.  */
38 #define PLT_ENTRY_SIZE 24
39 
40 /* TI C6X ELF linker hash table.  */
41 
42 struct elf32_tic6x_link_hash_table
43 {
44   struct elf_link_hash_table elf;
45 
46   /* C6X specific command line arguments.  */
47   struct elf32_tic6x_params params;
48 
49   /* Small local sym cache.  */
50   struct sym_cache sym_cache;
51 
52   /* The output BFD, for convenience.  */
53   bfd *obfd;
54 
55   /* The .dsbt section.  */
56   asection *dsbt;
57 };
58 
59 /* Get the TI C6X ELF linker hash table from a link_info structure.  */
60 
61 #define elf32_tic6x_hash_table(p) \
62   ((struct elf32_tic6x_link_hash_table *) ((p)->hash))
63 
64 /* TI C6X ELF linker hash entry.  */
65 
66 struct elf32_tic6x_link_hash_entry
67 {
68   struct elf_link_hash_entry elf;
69 
70   /* Track dynamic relocs copied for this symbol.  */
71   struct elf_dyn_relocs *dyn_relocs;
72 };
73 
74 typedef enum
75 {
76   DELETE_EXIDX_ENTRY,
77   INSERT_EXIDX_CANTUNWIND_AT_END
78 }
79 tic6x_unwind_edit_type;
80 
81 /* A (sorted) list of edits to apply to an unwind table.  */
82 typedef struct tic6x_unwind_table_edit
83 {
84   tic6x_unwind_edit_type type;
85   /* Note: we sometimes want to insert an unwind entry corresponding to a
86      section different from the one we're currently writing out, so record the
87      (text) section this edit relates to here.  */
88   asection *linked_section;
89   unsigned int index;
90   struct tic6x_unwind_table_edit *next;
91 }
92 tic6x_unwind_table_edit;
93 
94 typedef struct _tic6x_elf_section_data
95 {
96   /* Information about mapping symbols.  */
97   struct bfd_elf_section_data elf;
98   /* Information about unwind tables.  */
99   union
100   {
101     /* Unwind info attached to a text section.  */
102     struct
103     {
104       asection *tic6x_exidx_sec;
105     } text;
106 
107     /* Unwind info attached to an .c6xabi.exidx section.  */
108     struct
109     {
110       tic6x_unwind_table_edit *unwind_edit_list;
111       tic6x_unwind_table_edit *unwind_edit_tail;
112     } exidx;
113   } u;
114 }
115 _tic6x_elf_section_data;
116 
117 #define elf32_tic6x_section_data(sec) \
118   ((_tic6x_elf_section_data *) elf_section_data (sec))
119 
120 struct elf32_tic6x_obj_tdata
121 {
122   struct elf_obj_tdata root;
123 
124   /* Whether to use RELA relocations when generating relocations.
125      This is a per-object flag to allow the assembler to generate REL
126      relocations for use in linker testcases.  */
127   bfd_boolean use_rela_p;
128 };
129 
130 #define elf32_tic6x_tdata(abfd) \
131   ((struct elf32_tic6x_obj_tdata *) (abfd)->tdata.any)
132 
133 #define is_tic6x_elf(bfd) \
134   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
135    && elf_tdata (bfd) != NULL \
136    && elf_object_id (bfd) == TIC6X_ELF_DATA)
137 
138 /* C6X ELF uses two common sections.  One is the usual one, and the
139    other is for small objects.  All the small objects are kept
140    together, and then referenced via the gp pointer, which yields
141    faster assembler code.  This is what we use for the small common
142    section.  This approach is copied from ecoff.c.  */
143 static asection tic6x_elf_scom_section;
144 static asymbol  tic6x_elf_scom_symbol;
145 static asymbol  *tic6x_elf_scom_symbol_ptr;
146 
147 static reloc_howto_type elf32_tic6x_howto_table[] =
148 {
149   HOWTO (R_C6000_NONE,		/* type */
150 	 0,			/* rightshift */
151 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
152 	 0,			/* bitsize */
153 	 FALSE,			/* pc_relative */
154 	 0,			/* bitpos */
155 	 complain_overflow_dont,/* complain_on_overflow */
156 	 bfd_elf_generic_reloc,	/* special_function */
157 	 "R_C6000_NONE",	/* name */
158 	 FALSE,			/* partial_inplace */
159 	 0,			/* src_mask */
160 	 0,			/* dst_mask */
161 	 FALSE),		/* pcrel_offset */
162   HOWTO (R_C6000_ABS32,		/* type */
163 	 0,			/* rightshift */
164 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
165 	 32,			/* bitsize */
166 	 FALSE,			/* pc_relative */
167 	 0,			/* bitpos */
168 	 complain_overflow_dont,/* complain_on_overflow */
169 	 bfd_elf_generic_reloc,	/* special_function */
170 	 "R_C6000_ABS32",	/* name */
171 	 FALSE,			/* partial_inplace */
172 	 0,			/* src_mask */
173 	 0xffffffff,		/* dst_mask */
174 	 FALSE),		/* pcrel_offset */
175   HOWTO (R_C6000_ABS16,		/* type */
176 	 0,			/* rightshift */
177 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
178 	 16,			/* bitsize */
179 	 FALSE,			/* pc_relative */
180 	 0,			/* bitpos */
181 	 complain_overflow_bitfield,/* complain_on_overflow */
182 	 bfd_elf_generic_reloc,	/* special_function */
183 	 "R_C6000_ABS16",	/* name */
184 	 FALSE,			/* partial_inplace */
185 	 0,			/* src_mask */
186 	 0x0000ffff,		/* dst_mask */
187 	 FALSE),		/* pcrel_offset */
188   HOWTO (R_C6000_ABS8,		/* type */
189 	 0,			/* rightshift */
190 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
191 	 8,			/* bitsize */
192 	 FALSE,			/* pc_relative */
193 	 0,			/* bitpos */
194 	 complain_overflow_bitfield,/* complain_on_overflow */
195 	 bfd_elf_generic_reloc,	/* special_function */
196 	 "R_C6000_ABS8",	/* name */
197 	 FALSE,			/* partial_inplace */
198 	 0,			/* src_mask */
199 	 0x000000ff,		/* dst_mask */
200 	 FALSE),		/* pcrel_offset */
201   HOWTO (R_C6000_PCR_S21,	/* type */
202 	 2,			/* rightshift */
203 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
204 	 21,			/* bitsize */
205 	 TRUE,			/* pc_relative */
206 	 7,			/* bitpos */
207 	 complain_overflow_signed,/* complain_on_overflow */
208 	 bfd_elf_generic_reloc,	/* special_function */
209 	 "R_C6000_PCR_S21",	/* name */
210 	 FALSE,			/* partial_inplace */
211 	 0,			/* src_mask */
212 	 0x0fffff80,		/* dst_mask */
213 	 TRUE),			/* pcrel_offset */
214   HOWTO (R_C6000_PCR_S12,	/* type */
215 	 2,			/* rightshift */
216 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
217 	 12,			/* bitsize */
218 	 TRUE,			/* pc_relative */
219 	 16,			/* bitpos */
220 	 complain_overflow_signed,/* complain_on_overflow */
221 	 bfd_elf_generic_reloc,	/* special_function */
222 	 "R_C6000_PCR_S12",	/* name */
223 	 FALSE,			/* partial_inplace */
224 	 0,			/* src_mask */
225 	 0x0fff0000,		/* dst_mask */
226 	 TRUE),			/* pcrel_offset */
227   HOWTO (R_C6000_PCR_S10,	/* type */
228 	 2,			/* rightshift */
229 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
230 	 10,			/* bitsize */
231 	 TRUE,			/* pc_relative */
232 	 13,			/* bitpos */
233 	 complain_overflow_signed,/* complain_on_overflow */
234 	 bfd_elf_generic_reloc,	/* special_function */
235 	 "R_C6000_PCR_S10",	/* name */
236 	 FALSE,			/* partial_inplace */
237 	 0,			/* src_mask */
238 	 0x007fe000,		/* dst_mask */
239 	 TRUE),			/* pcrel_offset */
240   HOWTO (R_C6000_PCR_S7,	/* type */
241 	 2,			/* rightshift */
242 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
243 	 7,			/* bitsize */
244 	 TRUE,			/* pc_relative */
245 	 16,			/* bitpos */
246 	 complain_overflow_signed,/* complain_on_overflow */
247 	 bfd_elf_generic_reloc,	/* special_function */
248 	 "R_C6000_PCR_S7",	/* name */
249 	 FALSE,			/* partial_inplace */
250 	 0,			/* src_mask */
251 	 0x007f0000,		/* dst_mask */
252 	 TRUE),			/* pcrel_offset */
253   HOWTO (R_C6000_ABS_S16,	/* type */
254 	 0,			/* rightshift */
255 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
256 	 16,			/* bitsize */
257 	 FALSE,			/* pc_relative */
258 	 7,			/* bitpos */
259 	 complain_overflow_signed,/* complain_on_overflow */
260 	 bfd_elf_generic_reloc,	/* special_function */
261 	 "R_C6000_ABS_S16",	/* name */
262 	 FALSE,			/* partial_inplace */
263 	 0,			/* src_mask */
264 	 0x007fff80,		/* dst_mask */
265 	 FALSE),		/* pcrel_offset */
266   HOWTO (R_C6000_ABS_L16,	/* type */
267 	 0,			/* rightshift */
268 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
269 	 16,			/* bitsize */
270 	 FALSE,			/* pc_relative */
271 	 7,			/* bitpos */
272 	 complain_overflow_dont,/* complain_on_overflow */
273 	 bfd_elf_generic_reloc,	/* special_function */
274 	 "R_C6000_ABS_L16",	/* name */
275 	 FALSE,			/* partial_inplace */
276 	 0,			/* src_mask */
277 	 0x007fff80,		/* dst_mask */
278 	 FALSE),		/* pcrel_offset */
279   HOWTO (R_C6000_ABS_H16,	/* type */
280 	 16,			/* rightshift */
281 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
282 	 16,			/* bitsize */
283 	 FALSE,			/* pc_relative */
284 	 7,			/* bitpos */
285 	 complain_overflow_dont,/* complain_on_overflow */
286 	 bfd_elf_generic_reloc,	/* special_function */
287 	 "R_C6000_ABS_H16",	/* name */
288 	 FALSE,			/* partial_inplace */
289 	 0,			/* src_mask */
290 	 0x007fff80,		/* dst_mask */
291 	 FALSE),		/* pcrel_offset */
292   HOWTO (R_C6000_SBR_U15_B,	/* type */
293 	 0,			/* rightshift */
294 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
295 	 15,			/* bitsize */
296 	 FALSE,			/* pc_relative */
297 	 8,			/* bitpos */
298 	 complain_overflow_unsigned,/* complain_on_overflow */
299 	 bfd_elf_generic_reloc,	/* special_function */
300 	 "R_C6000_SBR_U15_B",	/* name */
301 	 FALSE,			/* partial_inplace */
302 	 0,			/* src_mask */
303 	 0x007fff00,		/* dst_mask */
304 	 FALSE),		/* pcrel_offset */
305   HOWTO (R_C6000_SBR_U15_H,	/* type */
306 	 1,			/* rightshift */
307 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
308 	 15,			/* bitsize */
309 	 FALSE,			/* pc_relative */
310 	 8,			/* bitpos */
311 	 complain_overflow_unsigned,/* complain_on_overflow */
312 	 bfd_elf_generic_reloc,	/* special_function */
313 	 "R_C6000_SBR_U15_H",	/* name */
314 	 FALSE,			/* partial_inplace */
315 	 0,			/* src_mask */
316 	 0x007fff00,		/* dst_mask */
317 	 FALSE),		/* pcrel_offset */
318   HOWTO (R_C6000_SBR_U15_W,	/* type */
319 	 2,			/* rightshift */
320 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
321 	 15,			/* bitsize */
322 	 FALSE,			/* pc_relative */
323 	 8,			/* bitpos */
324 	 complain_overflow_unsigned,/* complain_on_overflow */
325 	 bfd_elf_generic_reloc,	/* special_function */
326 	 "R_C6000_SBR_U15_W",	/* name */
327 	 FALSE,			/* partial_inplace */
328 	 0,			/* src_mask */
329 	 0x007fff00,		/* dst_mask */
330 	 FALSE),		/* pcrel_offset */
331   HOWTO (R_C6000_SBR_S16,	/* type */
332 	 0,			/* rightshift */
333 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
334 	 16,			/* bitsize */
335 	 FALSE,			/* pc_relative */
336 	 7,			/* bitpos */
337 	 complain_overflow_signed,/* complain_on_overflow */
338 	 bfd_elf_generic_reloc,	/* special_function */
339 	 "R_C6000_SBR_S16",	/* name */
340 	 FALSE,			/* partial_inplace */
341 	 0,			/* src_mask */
342 	 0x007fff80,		/* dst_mask */
343 	 FALSE),		/* pcrel_offset */
344   HOWTO (R_C6000_SBR_L16_B,	/* type */
345 	 0,			/* rightshift */
346 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
347 	 16,			/* bitsize */
348 	 FALSE,			/* pc_relative */
349 	 7,			/* bitpos */
350 	 complain_overflow_dont,/* complain_on_overflow */
351 	 bfd_elf_generic_reloc,	/* special_function */
352 	 "R_C6000_SBR_L16_B",	/* name */
353 	 FALSE,			/* partial_inplace */
354 	 0,			/* src_mask */
355 	 0x007fff80,		/* dst_mask */
356 	 FALSE),		/* pcrel_offset */
357   HOWTO (R_C6000_SBR_L16_H,	/* type */
358 	 1,			/* rightshift */
359 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
360 	 16,			/* bitsize */
361 	 FALSE,			/* pc_relative */
362 	 7,			/* bitpos */
363 	 complain_overflow_dont,/* complain_on_overflow */
364 	 bfd_elf_generic_reloc,	/* special_function */
365 	 "R_C6000_SBR_L16_H",	/* name */
366 	 FALSE,			/* partial_inplace */
367 	 0,			/* src_mask */
368 	 0x007fff80,		/* dst_mask */
369 	 FALSE),		/* pcrel_offset */
370   HOWTO (R_C6000_SBR_L16_W,	/* type */
371 	 2,			/* rightshift */
372 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
373 	 16,			/* bitsize */
374 	 FALSE,			/* pc_relative */
375 	 7,			/* bitpos */
376 	 complain_overflow_dont,/* complain_on_overflow */
377 	 bfd_elf_generic_reloc,	/* special_function */
378 	 "R_C6000_SBR_L16_W",	/* name */
379 	 FALSE,			/* partial_inplace */
380 	 0,			/* src_mask */
381 	 0x007fff80,		/* dst_mask */
382 	 FALSE),		/* pcrel_offset */
383   HOWTO (R_C6000_SBR_H16_B,	/* type */
384 	 16,			/* rightshift */
385 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
386 	 16,			/* bitsize */
387 	 FALSE,			/* pc_relative */
388 	 7,			/* bitpos */
389 	 complain_overflow_dont,/* complain_on_overflow */
390 	 bfd_elf_generic_reloc,	/* special_function */
391 	 "R_C6000_SBR_H16_B",	/* name */
392 	 FALSE,			/* partial_inplace */
393 	 0,			/* src_mask */
394 	 0x007fff80,		/* dst_mask */
395 	 FALSE),		/* pcrel_offset */
396   HOWTO (R_C6000_SBR_H16_H,	/* type */
397 	 17,			/* rightshift */
398 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
399 	 16,			/* bitsize */
400 	 FALSE,			/* pc_relative */
401 	 7,			/* bitpos */
402 	 complain_overflow_dont,/* complain_on_overflow */
403 	 bfd_elf_generic_reloc,	/* special_function */
404 	 "R_C6000_SBR_H16_H",	/* name */
405 	 FALSE,			/* partial_inplace */
406 	 0,			/* src_mask */
407 	 0x007fff80,		/* dst_mask */
408 	 FALSE),		/* pcrel_offset */
409   HOWTO (R_C6000_SBR_H16_W,	/* type */
410 	 18,			/* rightshift */
411 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
412 	 16,			/* bitsize */
413 	 FALSE,			/* pc_relative */
414 	 7,			/* bitpos */
415 	 complain_overflow_dont,/* complain_on_overflow */
416 	 bfd_elf_generic_reloc,	/* special_function */
417 	 "R_C6000_SBR_H16_W",	/* name */
418 	 FALSE,			/* partial_inplace */
419 	 0,			/* src_mask */
420 	 0x007fff80,		/* dst_mask */
421 	 FALSE),		/* pcrel_offset */
422   HOWTO (R_C6000_SBR_GOT_U15_W,	/* type */
423 	 2,			/* rightshift */
424 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
425 	 15,			/* bitsize */
426 	 FALSE,			/* pc_relative */
427 	 8,			/* bitpos */
428 	 complain_overflow_unsigned,/* complain_on_overflow */
429 	 bfd_elf_generic_reloc,	/* special_function */
430 	 "R_C6000_SBR_GOT_U15_W",/* name */
431 	 FALSE,			/* partial_inplace */
432 	 0,			/* src_mask */
433 	 0x007fff00,		/* dst_mask */
434 	 FALSE),		/* pcrel_offset */
435   HOWTO (R_C6000_SBR_GOT_L16_W,	/* type */
436 	 2,			/* rightshift */
437 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
438 	 16,			/* bitsize */
439 	 FALSE,			/* pc_relative */
440 	 7,			/* bitpos */
441 	 complain_overflow_dont,/* complain_on_overflow */
442 	 bfd_elf_generic_reloc,	/* special_function */
443 	 "R_C6000_SBR_GOT_L16_W",/* name */
444 	 FALSE,			/* partial_inplace */
445 	 0,			/* src_mask */
446 	 0x007fff80,		/* dst_mask */
447 	 FALSE),		/* pcrel_offset */
448   HOWTO (R_C6000_SBR_GOT_H16_W,	/* type */
449 	 18,			/* rightshift */
450 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
451 	 16,			/* bitsize */
452 	 FALSE,			/* pc_relative */
453 	 7,			/* bitpos */
454 	 complain_overflow_dont,/* complain_on_overflow */
455 	 bfd_elf_generic_reloc,	/* special_function */
456 	 "R_C6000_SBR_GOT_H16_W",/* name */
457 	 FALSE,			/* partial_inplace */
458 	 0,			/* src_mask */
459 	 0x007fff80,		/* dst_mask */
460 	 FALSE),		/* pcrel_offset */
461   HOWTO (R_C6000_DSBT_INDEX,	/* type */
462 	 0,			/* rightshift */
463 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
464 	 15,			/* bitsize */
465 	 FALSE,			/* pc_relative */
466 	 8,			/* bitpos */
467 	 complain_overflow_unsigned,/* complain_on_overflow */
468 	 bfd_elf_generic_reloc,	/* special_function */
469 	 "R_C6000_DSBT_INDEX",	/* name */
470 	 FALSE,			/* partial_inplace */
471 	 0,			/* src_mask */
472 	 0x007fff00,		/* dst_mask */
473 	 FALSE),		/* pcrel_offset */
474   HOWTO (R_C6000_PREL31,	/* type */
475 	 1,			/* rightshift */
476 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
477 	 31,			/* bitsize */
478 	 TRUE,			/* pc_relative */
479 	 0,			/* bitpos */
480 	 complain_overflow_dont,/* complain_on_overflow */
481 	 bfd_elf_generic_reloc,	/* special_function */
482 	 "R_C6000_PREL31",	/* name */
483 	 FALSE,			/* partial_inplace */
484 	 0,			/* src_mask */
485 	 0x7fffffff,		/* dst_mask */
486 	 TRUE),			/* pcrel_offset */
487   HOWTO (R_C6000_COPY,		/* type */
488 	 0,			/* rightshift */
489 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
490 	 32,			/* bitsize */
491 	 FALSE,			/* pc_relative */
492 	 0,			/* bitpos */
493 	 complain_overflow_dont,/* complain_on_overflow */
494 	 bfd_elf_generic_reloc,	/* special_function */
495 	 "R_C6000_COPY",	/* name */
496 	 FALSE,			/* partial_inplace */
497 	 0,			/* src_mask */
498 	 0xffffffff,		/* dst_mask */
499 	 FALSE),		/* pcrel_offset */
500   HOWTO (R_C6000_JUMP_SLOT,	/* type */
501 	 0,			/* rightshift */
502 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
503 	 32,			/* bitsize */
504 	 FALSE,			/* pc_relative */
505 	 0,			/* bitpos */
506 	 complain_overflow_dont,/* complain_on_overflow */
507 	 bfd_elf_generic_reloc,	/* special_function */
508 	 "R_C6000_JUMP_SLOT",	/* name */
509 	 FALSE,			/* partial_inplace */
510 	 0,			/* src_mask */
511 	 0xffffffff,		/* dst_mask */
512 	 FALSE),		/* pcrel_offset */
513   HOWTO (R_C6000_EHTYPE,	/* type */
514 	 0,			/* rightshift */
515 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
516 	 32,			/* bitsize */
517 	 FALSE,			/* pc_relative */
518 	 0,			/* bitpos */
519 	 complain_overflow_dont,/* complain_on_overflow */
520 	 bfd_elf_generic_reloc,	/* special_function */
521 	 "R_C6000_EHTYPE",	/* name */
522 	 FALSE,			/* partial_inplace */
523 	 0,			/* src_mask */
524 	 0xffffffff,		/* dst_mask */
525 	 FALSE),		/* pcrel_offset */
526   HOWTO (R_C6000_PCR_H16,	/* type */
527 	 16,			/* rightshift */
528 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
529 	 16,			/* bitsize */
530 	 TRUE,			/* pc_relative */
531 	 7,			/* bitpos */
532 	 complain_overflow_dont,/* complain_on_overflow */
533 	 bfd_elf_generic_reloc,	/* special_function */
534 	 "R_C6000_PCR_H16",	/* name */
535 	 FALSE,			/* partial_inplace */
536 	 0,			/* src_mask */
537 	 0x007fff80,		/* dst_mask */
538 	 TRUE),			/* pcrel_offset */
539   HOWTO (R_C6000_PCR_L16,	/* type */
540 	 0,			/* rightshift */
541 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
542 	 16,			/* bitsize */
543 	 TRUE,			/* pc_relative */
544 	 7,			/* bitpos */
545 	 complain_overflow_dont,/* complain_on_overflow */
546 	 bfd_elf_generic_reloc,	/* special_function */
547 	 "R_C6000_PCR_L16",	/* name */
548 	 FALSE,			/* partial_inplace */
549 	 0,			/* src_mask */
550 	 0x007fff80,		/* dst_mask */
551 	 TRUE),			/* pcrel_offset */
552   EMPTY_HOWTO (31),
553   EMPTY_HOWTO (32),
554   EMPTY_HOWTO (33),
555   EMPTY_HOWTO (34),
556   EMPTY_HOWTO (35),
557   EMPTY_HOWTO (36),
558   EMPTY_HOWTO (37),
559   EMPTY_HOWTO (38),
560   EMPTY_HOWTO (39),
561   EMPTY_HOWTO (40),
562   EMPTY_HOWTO (41),
563   EMPTY_HOWTO (42),
564   EMPTY_HOWTO (43),
565   EMPTY_HOWTO (44),
566   EMPTY_HOWTO (45),
567   EMPTY_HOWTO (46),
568   EMPTY_HOWTO (47),
569   EMPTY_HOWTO (48),
570   EMPTY_HOWTO (49),
571   EMPTY_HOWTO (50),
572   EMPTY_HOWTO (51),
573   EMPTY_HOWTO (52),
574   EMPTY_HOWTO (53),
575   EMPTY_HOWTO (54),
576   EMPTY_HOWTO (55),
577   EMPTY_HOWTO (56),
578   EMPTY_HOWTO (57),
579   EMPTY_HOWTO (58),
580   EMPTY_HOWTO (59),
581   EMPTY_HOWTO (60),
582   EMPTY_HOWTO (61),
583   EMPTY_HOWTO (62),
584   EMPTY_HOWTO (63),
585   EMPTY_HOWTO (64),
586   EMPTY_HOWTO (65),
587   EMPTY_HOWTO (66),
588   EMPTY_HOWTO (67),
589   EMPTY_HOWTO (68),
590   EMPTY_HOWTO (69),
591   EMPTY_HOWTO (70),
592   EMPTY_HOWTO (71),
593   EMPTY_HOWTO (72),
594   EMPTY_HOWTO (73),
595   EMPTY_HOWTO (74),
596   EMPTY_HOWTO (75),
597   EMPTY_HOWTO (76),
598   EMPTY_HOWTO (77),
599   EMPTY_HOWTO (78),
600   EMPTY_HOWTO (79),
601   EMPTY_HOWTO (80),
602   EMPTY_HOWTO (81),
603   EMPTY_HOWTO (82),
604   EMPTY_HOWTO (83),
605   EMPTY_HOWTO (84),
606   EMPTY_HOWTO (85),
607   EMPTY_HOWTO (86),
608   EMPTY_HOWTO (87),
609   EMPTY_HOWTO (88),
610   EMPTY_HOWTO (89),
611   EMPTY_HOWTO (90),
612   EMPTY_HOWTO (91),
613   EMPTY_HOWTO (92),
614   EMPTY_HOWTO (93),
615   EMPTY_HOWTO (94),
616   EMPTY_HOWTO (95),
617   EMPTY_HOWTO (96),
618   EMPTY_HOWTO (97),
619   EMPTY_HOWTO (98),
620   EMPTY_HOWTO (99),
621   EMPTY_HOWTO (100),
622   EMPTY_HOWTO (101),
623   EMPTY_HOWTO (102),
624   EMPTY_HOWTO (103),
625   EMPTY_HOWTO (104),
626   EMPTY_HOWTO (105),
627   EMPTY_HOWTO (106),
628   EMPTY_HOWTO (107),
629   EMPTY_HOWTO (108),
630   EMPTY_HOWTO (109),
631   EMPTY_HOWTO (110),
632   EMPTY_HOWTO (111),
633   EMPTY_HOWTO (112),
634   EMPTY_HOWTO (113),
635   EMPTY_HOWTO (114),
636   EMPTY_HOWTO (115),
637   EMPTY_HOWTO (116),
638   EMPTY_HOWTO (117),
639   EMPTY_HOWTO (118),
640   EMPTY_HOWTO (119),
641   EMPTY_HOWTO (120),
642   EMPTY_HOWTO (121),
643   EMPTY_HOWTO (122),
644   EMPTY_HOWTO (123),
645   EMPTY_HOWTO (124),
646   EMPTY_HOWTO (125),
647   EMPTY_HOWTO (126),
648   EMPTY_HOWTO (127),
649   EMPTY_HOWTO (128),
650   EMPTY_HOWTO (129),
651   EMPTY_HOWTO (130),
652   EMPTY_HOWTO (131),
653   EMPTY_HOWTO (132),
654   EMPTY_HOWTO (133),
655   EMPTY_HOWTO (134),
656   EMPTY_HOWTO (135),
657   EMPTY_HOWTO (136),
658   EMPTY_HOWTO (137),
659   EMPTY_HOWTO (138),
660   EMPTY_HOWTO (139),
661   EMPTY_HOWTO (140),
662   EMPTY_HOWTO (141),
663   EMPTY_HOWTO (142),
664   EMPTY_HOWTO (143),
665   EMPTY_HOWTO (144),
666   EMPTY_HOWTO (145),
667   EMPTY_HOWTO (146),
668   EMPTY_HOWTO (147),
669   EMPTY_HOWTO (148),
670   EMPTY_HOWTO (149),
671   EMPTY_HOWTO (150),
672   EMPTY_HOWTO (151),
673   EMPTY_HOWTO (152),
674   EMPTY_HOWTO (153),
675   EMPTY_HOWTO (154),
676   EMPTY_HOWTO (155),
677   EMPTY_HOWTO (156),
678   EMPTY_HOWTO (157),
679   EMPTY_HOWTO (158),
680   EMPTY_HOWTO (159),
681   EMPTY_HOWTO (160),
682   EMPTY_HOWTO (161),
683   EMPTY_HOWTO (162),
684   EMPTY_HOWTO (163),
685   EMPTY_HOWTO (164),
686   EMPTY_HOWTO (165),
687   EMPTY_HOWTO (166),
688   EMPTY_HOWTO (167),
689   EMPTY_HOWTO (168),
690   EMPTY_HOWTO (169),
691   EMPTY_HOWTO (170),
692   EMPTY_HOWTO (171),
693   EMPTY_HOWTO (172),
694   EMPTY_HOWTO (173),
695   EMPTY_HOWTO (174),
696   EMPTY_HOWTO (175),
697   EMPTY_HOWTO (176),
698   EMPTY_HOWTO (177),
699   EMPTY_HOWTO (178),
700   EMPTY_HOWTO (179),
701   EMPTY_HOWTO (180),
702   EMPTY_HOWTO (181),
703   EMPTY_HOWTO (182),
704   EMPTY_HOWTO (183),
705   EMPTY_HOWTO (184),
706   EMPTY_HOWTO (185),
707   EMPTY_HOWTO (186),
708   EMPTY_HOWTO (187),
709   EMPTY_HOWTO (188),
710   EMPTY_HOWTO (189),
711   EMPTY_HOWTO (190),
712   EMPTY_HOWTO (191),
713   EMPTY_HOWTO (192),
714   EMPTY_HOWTO (193),
715   EMPTY_HOWTO (194),
716   EMPTY_HOWTO (195),
717   EMPTY_HOWTO (196),
718   EMPTY_HOWTO (197),
719   EMPTY_HOWTO (198),
720   EMPTY_HOWTO (199),
721   EMPTY_HOWTO (200),
722   EMPTY_HOWTO (201),
723   EMPTY_HOWTO (202),
724   EMPTY_HOWTO (203),
725   EMPTY_HOWTO (204),
726   EMPTY_HOWTO (205),
727   EMPTY_HOWTO (206),
728   EMPTY_HOWTO (207),
729   EMPTY_HOWTO (208),
730   EMPTY_HOWTO (209),
731   EMPTY_HOWTO (210),
732   EMPTY_HOWTO (211),
733   EMPTY_HOWTO (212),
734   EMPTY_HOWTO (213),
735   EMPTY_HOWTO (214),
736   EMPTY_HOWTO (215),
737   EMPTY_HOWTO (216),
738   EMPTY_HOWTO (217),
739   EMPTY_HOWTO (218),
740   EMPTY_HOWTO (219),
741   EMPTY_HOWTO (220),
742   EMPTY_HOWTO (221),
743   EMPTY_HOWTO (222),
744   EMPTY_HOWTO (223),
745   EMPTY_HOWTO (224),
746   EMPTY_HOWTO (225),
747   EMPTY_HOWTO (226),
748   EMPTY_HOWTO (227),
749   EMPTY_HOWTO (228),
750   EMPTY_HOWTO (229),
751   EMPTY_HOWTO (230),
752   EMPTY_HOWTO (231),
753   EMPTY_HOWTO (232),
754   EMPTY_HOWTO (233),
755   EMPTY_HOWTO (234),
756   EMPTY_HOWTO (235),
757   EMPTY_HOWTO (236),
758   EMPTY_HOWTO (237),
759   EMPTY_HOWTO (238),
760   EMPTY_HOWTO (239),
761   EMPTY_HOWTO (240),
762   EMPTY_HOWTO (241),
763   EMPTY_HOWTO (242),
764   EMPTY_HOWTO (243),
765   EMPTY_HOWTO (244),
766   EMPTY_HOWTO (245),
767   EMPTY_HOWTO (246),
768   EMPTY_HOWTO (247),
769   EMPTY_HOWTO (248),
770   EMPTY_HOWTO (249),
771   EMPTY_HOWTO (250),
772   EMPTY_HOWTO (251),
773   EMPTY_HOWTO (252),
774   HOWTO (R_C6000_ALIGN,		/* type */
775 	 0,			/* rightshift */
776 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
777 	 0,			/* bitsize */
778 	 FALSE,			/* pc_relative */
779 	 0,			/* bitpos */
780 	 complain_overflow_dont,/* complain_on_overflow */
781 	 bfd_elf_generic_reloc,	/* special_function */
782 	 "R_C6000_ALIGN",	/* name */
783 	 FALSE,			/* partial_inplace */
784 	 0,			/* src_mask */
785 	 0,			/* dst_mask */
786 	 FALSE),		/* pcrel_offset */
787   HOWTO (R_C6000_FPHEAD,	/* type */
788 	 0,			/* rightshift */
789 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
790 	 0,			/* bitsize */
791 	 FALSE,			/* pc_relative */
792 	 0,			/* bitpos */
793 	 complain_overflow_dont,/* complain_on_overflow */
794 	 bfd_elf_generic_reloc,	/* special_function */
795 	 "R_C6000_FPHEAD",	/* name */
796 	 FALSE,			/* partial_inplace */
797 	 0,			/* src_mask */
798 	 0,			/* dst_mask */
799 	 FALSE),		/* pcrel_offset */
800   HOWTO (R_C6000_NOCMP,		/* type */
801 	 0,			/* rightshift */
802 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
803 	 0,			/* bitsize */
804 	 FALSE,			/* pc_relative */
805 	 0,			/* bitpos */
806 	 complain_overflow_dont,/* complain_on_overflow */
807 	 bfd_elf_generic_reloc,	/* special_function */
808 	 "R_C6000_NOCMP",	/* name */
809 	 FALSE,			/* partial_inplace */
810 	 0,			/* src_mask */
811 	 0,			/* dst_mask */
812 	 FALSE)			/* pcrel_offset */
813 };
814 
815 static reloc_howto_type elf32_tic6x_howto_table_rel[] =
816 {
817   HOWTO (R_C6000_NONE,		/* type */
818 	 0,			/* rightshift */
819 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
820 	 0,			/* bitsize */
821 	 FALSE,			/* pc_relative */
822 	 0,			/* bitpos */
823 	 complain_overflow_dont,/* complain_on_overflow */
824 	 bfd_elf_generic_reloc,	/* special_function */
825 	 "R_C6000_NONE",	/* name */
826 	 TRUE,			/* partial_inplace */
827 	 0,			/* src_mask */
828 	 0,			/* dst_mask */
829 	 FALSE),		/* pcrel_offset */
830   HOWTO (R_C6000_ABS32,		/* type */
831 	 0,			/* rightshift */
832 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
833 	 32,			/* bitsize */
834 	 FALSE,			/* pc_relative */
835 	 0,			/* bitpos */
836 	 complain_overflow_dont,/* complain_on_overflow */
837 	 bfd_elf_generic_reloc,	/* special_function */
838 	 "R_C6000_ABS32",	/* name */
839 	 TRUE,			/* partial_inplace */
840 	 0xffffffff,		/* src_mask */
841 	 0xffffffff,		/* dst_mask */
842 	 FALSE),		/* pcrel_offset */
843   HOWTO (R_C6000_ABS16,		/* type */
844 	 0,			/* rightshift */
845 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
846 	 16,			/* bitsize */
847 	 FALSE,			/* pc_relative */
848 	 0,			/* bitpos */
849 	 complain_overflow_bitfield,/* complain_on_overflow */
850 	 bfd_elf_generic_reloc,	/* special_function */
851 	 "R_C6000_ABS16",	/* name */
852 	 TRUE,			/* partial_inplace */
853 	 0x0000ffff,		/* src_mask */
854 	 0x0000ffff,		/* dst_mask */
855 	 FALSE),		/* pcrel_offset */
856   HOWTO (R_C6000_ABS8,		/* type */
857 	 0,			/* rightshift */
858 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
859 	 8,			/* bitsize */
860 	 FALSE,			/* pc_relative */
861 	 0,			/* bitpos */
862 	 complain_overflow_bitfield,/* complain_on_overflow */
863 	 bfd_elf_generic_reloc,	/* special_function */
864 	 "R_C6000_ABS8",	/* name */
865 	 TRUE,			/* partial_inplace */
866 	 0x000000ff,		/* src_mask */
867 	 0x000000ff,		/* dst_mask */
868 	 FALSE),		/* pcrel_offset */
869   HOWTO (R_C6000_PCR_S21,	/* type */
870 	 2,			/* rightshift */
871 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
872 	 21,			/* bitsize */
873 	 TRUE,			/* pc_relative */
874 	 7,			/* bitpos */
875 	 complain_overflow_signed,/* complain_on_overflow */
876 	 bfd_elf_generic_reloc,	/* special_function */
877 	 "R_C6000_PCR_S21",	/* name */
878 	 TRUE,			/* partial_inplace */
879 	 0x0fffff80,		/* src_mask */
880 	 0x0fffff80,		/* dst_mask */
881 	 TRUE),			/* pcrel_offset */
882   HOWTO (R_C6000_PCR_S12,	/* type */
883 	 2,			/* rightshift */
884 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
885 	 12,			/* bitsize */
886 	 TRUE,			/* pc_relative */
887 	 16,			/* bitpos */
888 	 complain_overflow_signed,/* complain_on_overflow */
889 	 bfd_elf_generic_reloc,	/* special_function */
890 	 "R_C6000_PCR_S12",	/* name */
891 	 TRUE,			/* partial_inplace */
892 	 0x0fff0000,		/* src_mask */
893 	 0x0fff0000,		/* dst_mask */
894 	 TRUE),			/* pcrel_offset */
895   HOWTO (R_C6000_PCR_S10,	/* type */
896 	 2,			/* rightshift */
897 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
898 	 10,			/* bitsize */
899 	 TRUE,			/* pc_relative */
900 	 13,			/* bitpos */
901 	 complain_overflow_signed,/* complain_on_overflow */
902 	 bfd_elf_generic_reloc,	/* special_function */
903 	 "R_C6000_PCR_S10",	/* name */
904 	 TRUE,			/* partial_inplace */
905 	 0x007fe000,		/* src_mask */
906 	 0x007fe000,		/* dst_mask */
907 	 TRUE),			/* pcrel_offset */
908   HOWTO (R_C6000_PCR_S7,	/* type */
909 	 2,			/* rightshift */
910 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
911 	 7,			/* bitsize */
912 	 TRUE,			/* pc_relative */
913 	 16,			/* bitpos */
914 	 complain_overflow_signed,/* complain_on_overflow */
915 	 bfd_elf_generic_reloc,	/* special_function */
916 	 "R_C6000_PCR_S7",	/* name */
917 	 TRUE,			/* partial_inplace */
918 	 0x007f0000,		/* src_mask */
919 	 0x007f0000,		/* dst_mask */
920 	 TRUE),			/* pcrel_offset */
921   HOWTO (R_C6000_ABS_S16,	/* type */
922 	 0,			/* rightshift */
923 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
924 	 16,			/* bitsize */
925 	 FALSE,			/* pc_relative */
926 	 7,			/* bitpos */
927 	 complain_overflow_signed,/* complain_on_overflow */
928 	 bfd_elf_generic_reloc,	/* special_function */
929 	 "R_C6000_ABS_S16",	/* name */
930 	 TRUE,			/* partial_inplace */
931 	 0x007fff80,		/* src_mask */
932 	 0x007fff80,		/* dst_mask */
933 	 FALSE),		/* pcrel_offset */
934   HOWTO (R_C6000_ABS_L16,	/* type */
935 	 0,			/* rightshift */
936 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
937 	 16,			/* bitsize */
938 	 FALSE,			/* pc_relative */
939 	 7,			/* bitpos */
940 	 complain_overflow_dont,/* complain_on_overflow */
941 	 bfd_elf_generic_reloc,	/* special_function */
942 	 "R_C6000_ABS_L16",	/* name */
943 	 TRUE,			/* partial_inplace */
944 	 0x007fff80,		/* src_mask */
945 	 0x007fff80,		/* dst_mask */
946 	 FALSE),		/* pcrel_offset */
947   EMPTY_HOWTO (R_C6000_ABS_H16),
948   HOWTO (R_C6000_SBR_U15_B,	/* type */
949 	 0,			/* rightshift */
950 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
951 	 15,			/* bitsize */
952 	 FALSE,			/* pc_relative */
953 	 8,			/* bitpos */
954 	 complain_overflow_unsigned,/* complain_on_overflow */
955 	 bfd_elf_generic_reloc,	/* special_function */
956 	 "R_C6000_SBR_U15_B",	/* name */
957 	 TRUE,			/* partial_inplace */
958 	 0x007fff00,		/* src_mask */
959 	 0x007fff00,		/* dst_mask */
960 	 FALSE),		/* pcrel_offset */
961   HOWTO (R_C6000_SBR_U15_H,	/* type */
962 	 1,			/* rightshift */
963 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
964 	 15,			/* bitsize */
965 	 FALSE,			/* pc_relative */
966 	 8,			/* bitpos */
967 	 complain_overflow_unsigned,/* complain_on_overflow */
968 	 bfd_elf_generic_reloc,	/* special_function */
969 	 "R_C6000_SBR_U15_H",	/* name */
970 	 TRUE,			/* partial_inplace */
971 	 0x007fff00,		/* src_mask */
972 	 0x007fff00,		/* dst_mask */
973 	 FALSE),		/* pcrel_offset */
974   HOWTO (R_C6000_SBR_U15_W,	/* type */
975 	 2,			/* rightshift */
976 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
977 	 15,			/* bitsize */
978 	 FALSE,			/* pc_relative */
979 	 8,			/* bitpos */
980 	 complain_overflow_unsigned,/* complain_on_overflow */
981 	 bfd_elf_generic_reloc,	/* special_function */
982 	 "R_C6000_SBR_U15_W",	/* name */
983 	 TRUE,			/* partial_inplace */
984 	 0x007fff00,		/* src_mask */
985 	 0x007fff00,		/* dst_mask */
986 	 FALSE),		/* pcrel_offset */
987   HOWTO (R_C6000_SBR_S16,	/* type */
988 	 0,			/* rightshift */
989 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
990 	 16,			/* bitsize */
991 	 FALSE,			/* pc_relative */
992 	 7,			/* bitpos */
993 	 complain_overflow_signed,/* complain_on_overflow */
994 	 bfd_elf_generic_reloc,	/* special_function */
995 	 "R_C6000_SBR_S16",	/* name */
996 	 TRUE,			/* partial_inplace */
997 	 0x007fff80,		/* src_mask */
998 	 0x007fff80,		/* dst_mask */
999 	 FALSE),		/* pcrel_offset */
1000   HOWTO (R_C6000_SBR_L16_B,	/* type */
1001 	 0,			/* rightshift */
1002 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1003 	 16,			/* bitsize */
1004 	 FALSE,			/* pc_relative */
1005 	 7,			/* bitpos */
1006 	 complain_overflow_dont,/* complain_on_overflow */
1007 	 bfd_elf_generic_reloc,	/* special_function */
1008 	 "R_C6000_SBR_L16_B",	/* name */
1009 	 TRUE,			/* partial_inplace */
1010 	 0x007fff80,		/* src_mask */
1011 	 0x007fff80,		/* dst_mask */
1012 	 FALSE),		/* pcrel_offset */
1013   HOWTO (R_C6000_SBR_L16_H,	/* type */
1014 	 1,			/* rightshift */
1015 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1016 	 16,			/* bitsize */
1017 	 FALSE,			/* pc_relative */
1018 	 7,			/* bitpos */
1019 	 complain_overflow_dont,/* complain_on_overflow */
1020 	 bfd_elf_generic_reloc,	/* special_function */
1021 	 "R_C6000_SBR_L16_H",	/* name */
1022 	 TRUE,			/* partial_inplace */
1023 	 0x007fff80,		/* src_mask */
1024 	 0x007fff80,		/* dst_mask */
1025 	 FALSE),		/* pcrel_offset */
1026   HOWTO (R_C6000_SBR_L16_W,	/* type */
1027 	 2,			/* rightshift */
1028 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1029 	 16,			/* bitsize */
1030 	 FALSE,			/* pc_relative */
1031 	 7,			/* bitpos */
1032 	 complain_overflow_dont,/* complain_on_overflow */
1033 	 bfd_elf_generic_reloc,	/* special_function */
1034 	 "R_C6000_SBR_L16_W",	/* name */
1035 	 TRUE,			/* partial_inplace */
1036 	 0x007fff80,		/* src_mask */
1037 	 0x007fff80,		/* dst_mask */
1038 	 FALSE),		/* pcrel_offset */
1039   EMPTY_HOWTO (R_C6000_SBR_H16_B),
1040   EMPTY_HOWTO (R_C6000_SBR_H16_H),
1041   EMPTY_HOWTO (R_C6000_SBR_H16_W),
1042   HOWTO (R_C6000_SBR_GOT_U15_W,	/* type */
1043 	 2,			/* rightshift */
1044 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1045 	 15,			/* bitsize */
1046 	 FALSE,			/* pc_relative */
1047 	 8,			/* bitpos */
1048 	 complain_overflow_unsigned,/* complain_on_overflow */
1049 	 bfd_elf_generic_reloc,	/* special_function */
1050 	 "R_C6000_SBR_GOT_U15_W",/* name */
1051 	 TRUE,			/* partial_inplace */
1052 	 0x007fff00,		/* src_mask */
1053 	 0x007fff00,		/* dst_mask */
1054 	 FALSE),		/* pcrel_offset */
1055   HOWTO (R_C6000_SBR_GOT_L16_W,	/* type */
1056 	 2,			/* rightshift */
1057 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1058 	 16,			/* bitsize */
1059 	 FALSE,			/* pc_relative */
1060 	 7,			/* bitpos */
1061 	 complain_overflow_dont,/* complain_on_overflow */
1062 	 bfd_elf_generic_reloc,	/* special_function */
1063 	 "R_C6000_SBR_GOT_L16_W",/* name */
1064 	 TRUE,			/* partial_inplace */
1065 	 0x007fff80,		/* src_mask */
1066 	 0x007fff80,		/* dst_mask */
1067 	 FALSE),		/* pcrel_offset */
1068   EMPTY_HOWTO (R_C6000_SBR_GOT_H16_W),
1069   HOWTO (R_C6000_DSBT_INDEX,	/* type */
1070 	 0,			/* rightshift */
1071 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1072 	 15,			/* bitsize */
1073 	 FALSE,			/* pc_relative */
1074 	 8,			/* bitpos */
1075 	 complain_overflow_unsigned,/* complain_on_overflow */
1076 	 bfd_elf_generic_reloc,	/* special_function */
1077 	 "R_C6000_DSBT_INDEX",	/* name */
1078 	 TRUE,			/* partial_inplace */
1079 	 0,			/* src_mask */
1080 	 0x007fff00,		/* dst_mask */
1081 	 FALSE),		/* pcrel_offset */
1082   HOWTO (R_C6000_PREL31,	/* type */
1083 	 1,			/* rightshift */
1084 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1085 	 31,			/* bitsize */
1086 	 TRUE,			/* pc_relative */
1087 	 0,			/* bitpos */
1088 	 complain_overflow_dont,/* complain_on_overflow */
1089 	 bfd_elf_generic_reloc,	/* special_function */
1090 	 "R_C6000_PREL31",	/* name */
1091 	 TRUE,			/* partial_inplace */
1092 	 0,			/* src_mask */
1093 	 0x7fffffff,		/* dst_mask */
1094 	 TRUE),			/* pcrel_offset */
1095   HOWTO (R_C6000_COPY,		/* type */
1096 	 0,			/* rightshift */
1097 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1098 	 32,			/* bitsize */
1099 	 FALSE,			/* pc_relative */
1100 	 0,			/* bitpos */
1101 	 complain_overflow_dont,/* complain_on_overflow */
1102 	 bfd_elf_generic_reloc,	/* special_function */
1103 	 "R_C6000_COPY",	/* name */
1104 	 TRUE,			/* partial_inplace */
1105 	 0,			/* src_mask */
1106 	 0xffffffff,		/* dst_mask */
1107 	 FALSE),		/* pcrel_offset */
1108   HOWTO (R_C6000_JUMP_SLOT,	/* type */
1109 	 0,			/* rightshift */
1110 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1111 	 32,			/* bitsize */
1112 	 FALSE,			/* pc_relative */
1113 	 0,			/* bitpos */
1114 	 complain_overflow_dont,/* complain_on_overflow */
1115 	 bfd_elf_generic_reloc,	/* special_function */
1116 	 "R_C6000_JUMP_SLOT",	/* name */
1117 	 FALSE,			/* partial_inplace */
1118 	 0,			/* src_mask */
1119 	 0xffffffff,		/* dst_mask */
1120 	 FALSE),		/* pcrel_offset */
1121   HOWTO (R_C6000_EHTYPE,	/* type */
1122 	 0,			/* rightshift */
1123 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1124 	 32,			/* bitsize */
1125 	 FALSE,			/* pc_relative */
1126 	 0,			/* bitpos */
1127 	 complain_overflow_dont,/* complain_on_overflow */
1128 	 bfd_elf_generic_reloc,	/* special_function */
1129 	 "R_C6000_EHTYPE",	/* name */
1130 	 FALSE,			/* partial_inplace */
1131 	 0,			/* src_mask */
1132 	 0xffffffff,		/* dst_mask */
1133 	 FALSE),		/* pcrel_offset */
1134   EMPTY_HOWTO (R_C6000_PCR_H16),
1135   EMPTY_HOWTO (R_C6000_PCR_L16),
1136   EMPTY_HOWTO (31),
1137   EMPTY_HOWTO (32),
1138   EMPTY_HOWTO (33),
1139   EMPTY_HOWTO (34),
1140   EMPTY_HOWTO (35),
1141   EMPTY_HOWTO (36),
1142   EMPTY_HOWTO (37),
1143   EMPTY_HOWTO (38),
1144   EMPTY_HOWTO (39),
1145   EMPTY_HOWTO (40),
1146   EMPTY_HOWTO (41),
1147   EMPTY_HOWTO (42),
1148   EMPTY_HOWTO (43),
1149   EMPTY_HOWTO (44),
1150   EMPTY_HOWTO (45),
1151   EMPTY_HOWTO (46),
1152   EMPTY_HOWTO (47),
1153   EMPTY_HOWTO (48),
1154   EMPTY_HOWTO (49),
1155   EMPTY_HOWTO (50),
1156   EMPTY_HOWTO (51),
1157   EMPTY_HOWTO (52),
1158   EMPTY_HOWTO (53),
1159   EMPTY_HOWTO (54),
1160   EMPTY_HOWTO (55),
1161   EMPTY_HOWTO (56),
1162   EMPTY_HOWTO (57),
1163   EMPTY_HOWTO (58),
1164   EMPTY_HOWTO (59),
1165   EMPTY_HOWTO (60),
1166   EMPTY_HOWTO (61),
1167   EMPTY_HOWTO (62),
1168   EMPTY_HOWTO (63),
1169   EMPTY_HOWTO (64),
1170   EMPTY_HOWTO (65),
1171   EMPTY_HOWTO (66),
1172   EMPTY_HOWTO (67),
1173   EMPTY_HOWTO (68),
1174   EMPTY_HOWTO (69),
1175   EMPTY_HOWTO (70),
1176   EMPTY_HOWTO (71),
1177   EMPTY_HOWTO (72),
1178   EMPTY_HOWTO (73),
1179   EMPTY_HOWTO (74),
1180   EMPTY_HOWTO (75),
1181   EMPTY_HOWTO (76),
1182   EMPTY_HOWTO (77),
1183   EMPTY_HOWTO (78),
1184   EMPTY_HOWTO (79),
1185   EMPTY_HOWTO (80),
1186   EMPTY_HOWTO (81),
1187   EMPTY_HOWTO (82),
1188   EMPTY_HOWTO (83),
1189   EMPTY_HOWTO (84),
1190   EMPTY_HOWTO (85),
1191   EMPTY_HOWTO (86),
1192   EMPTY_HOWTO (87),
1193   EMPTY_HOWTO (88),
1194   EMPTY_HOWTO (89),
1195   EMPTY_HOWTO (90),
1196   EMPTY_HOWTO (91),
1197   EMPTY_HOWTO (92),
1198   EMPTY_HOWTO (93),
1199   EMPTY_HOWTO (94),
1200   EMPTY_HOWTO (95),
1201   EMPTY_HOWTO (96),
1202   EMPTY_HOWTO (97),
1203   EMPTY_HOWTO (98),
1204   EMPTY_HOWTO (99),
1205   EMPTY_HOWTO (100),
1206   EMPTY_HOWTO (101),
1207   EMPTY_HOWTO (102),
1208   EMPTY_HOWTO (103),
1209   EMPTY_HOWTO (104),
1210   EMPTY_HOWTO (105),
1211   EMPTY_HOWTO (106),
1212   EMPTY_HOWTO (107),
1213   EMPTY_HOWTO (108),
1214   EMPTY_HOWTO (109),
1215   EMPTY_HOWTO (110),
1216   EMPTY_HOWTO (111),
1217   EMPTY_HOWTO (112),
1218   EMPTY_HOWTO (113),
1219   EMPTY_HOWTO (114),
1220   EMPTY_HOWTO (115),
1221   EMPTY_HOWTO (116),
1222   EMPTY_HOWTO (117),
1223   EMPTY_HOWTO (118),
1224   EMPTY_HOWTO (119),
1225   EMPTY_HOWTO (120),
1226   EMPTY_HOWTO (121),
1227   EMPTY_HOWTO (122),
1228   EMPTY_HOWTO (123),
1229   EMPTY_HOWTO (124),
1230   EMPTY_HOWTO (125),
1231   EMPTY_HOWTO (126),
1232   EMPTY_HOWTO (127),
1233   EMPTY_HOWTO (128),
1234   EMPTY_HOWTO (129),
1235   EMPTY_HOWTO (130),
1236   EMPTY_HOWTO (131),
1237   EMPTY_HOWTO (132),
1238   EMPTY_HOWTO (133),
1239   EMPTY_HOWTO (134),
1240   EMPTY_HOWTO (135),
1241   EMPTY_HOWTO (136),
1242   EMPTY_HOWTO (137),
1243   EMPTY_HOWTO (138),
1244   EMPTY_HOWTO (139),
1245   EMPTY_HOWTO (140),
1246   EMPTY_HOWTO (141),
1247   EMPTY_HOWTO (142),
1248   EMPTY_HOWTO (143),
1249   EMPTY_HOWTO (144),
1250   EMPTY_HOWTO (145),
1251   EMPTY_HOWTO (146),
1252   EMPTY_HOWTO (147),
1253   EMPTY_HOWTO (148),
1254   EMPTY_HOWTO (149),
1255   EMPTY_HOWTO (150),
1256   EMPTY_HOWTO (151),
1257   EMPTY_HOWTO (152),
1258   EMPTY_HOWTO (153),
1259   EMPTY_HOWTO (154),
1260   EMPTY_HOWTO (155),
1261   EMPTY_HOWTO (156),
1262   EMPTY_HOWTO (157),
1263   EMPTY_HOWTO (158),
1264   EMPTY_HOWTO (159),
1265   EMPTY_HOWTO (160),
1266   EMPTY_HOWTO (161),
1267   EMPTY_HOWTO (162),
1268   EMPTY_HOWTO (163),
1269   EMPTY_HOWTO (164),
1270   EMPTY_HOWTO (165),
1271   EMPTY_HOWTO (166),
1272   EMPTY_HOWTO (167),
1273   EMPTY_HOWTO (168),
1274   EMPTY_HOWTO (169),
1275   EMPTY_HOWTO (170),
1276   EMPTY_HOWTO (171),
1277   EMPTY_HOWTO (172),
1278   EMPTY_HOWTO (173),
1279   EMPTY_HOWTO (174),
1280   EMPTY_HOWTO (175),
1281   EMPTY_HOWTO (176),
1282   EMPTY_HOWTO (177),
1283   EMPTY_HOWTO (178),
1284   EMPTY_HOWTO (179),
1285   EMPTY_HOWTO (180),
1286   EMPTY_HOWTO (181),
1287   EMPTY_HOWTO (182),
1288   EMPTY_HOWTO (183),
1289   EMPTY_HOWTO (184),
1290   EMPTY_HOWTO (185),
1291   EMPTY_HOWTO (186),
1292   EMPTY_HOWTO (187),
1293   EMPTY_HOWTO (188),
1294   EMPTY_HOWTO (189),
1295   EMPTY_HOWTO (190),
1296   EMPTY_HOWTO (191),
1297   EMPTY_HOWTO (192),
1298   EMPTY_HOWTO (193),
1299   EMPTY_HOWTO (194),
1300   EMPTY_HOWTO (195),
1301   EMPTY_HOWTO (196),
1302   EMPTY_HOWTO (197),
1303   EMPTY_HOWTO (198),
1304   EMPTY_HOWTO (199),
1305   EMPTY_HOWTO (200),
1306   EMPTY_HOWTO (201),
1307   EMPTY_HOWTO (202),
1308   EMPTY_HOWTO (203),
1309   EMPTY_HOWTO (204),
1310   EMPTY_HOWTO (205),
1311   EMPTY_HOWTO (206),
1312   EMPTY_HOWTO (207),
1313   EMPTY_HOWTO (208),
1314   EMPTY_HOWTO (209),
1315   EMPTY_HOWTO (210),
1316   EMPTY_HOWTO (211),
1317   EMPTY_HOWTO (212),
1318   EMPTY_HOWTO (213),
1319   EMPTY_HOWTO (214),
1320   EMPTY_HOWTO (215),
1321   EMPTY_HOWTO (216),
1322   EMPTY_HOWTO (217),
1323   EMPTY_HOWTO (218),
1324   EMPTY_HOWTO (219),
1325   EMPTY_HOWTO (220),
1326   EMPTY_HOWTO (221),
1327   EMPTY_HOWTO (222),
1328   EMPTY_HOWTO (223),
1329   EMPTY_HOWTO (224),
1330   EMPTY_HOWTO (225),
1331   EMPTY_HOWTO (226),
1332   EMPTY_HOWTO (227),
1333   EMPTY_HOWTO (228),
1334   EMPTY_HOWTO (229),
1335   EMPTY_HOWTO (230),
1336   EMPTY_HOWTO (231),
1337   EMPTY_HOWTO (232),
1338   EMPTY_HOWTO (233),
1339   EMPTY_HOWTO (234),
1340   EMPTY_HOWTO (235),
1341   EMPTY_HOWTO (236),
1342   EMPTY_HOWTO (237),
1343   EMPTY_HOWTO (238),
1344   EMPTY_HOWTO (239),
1345   EMPTY_HOWTO (240),
1346   EMPTY_HOWTO (241),
1347   EMPTY_HOWTO (242),
1348   EMPTY_HOWTO (243),
1349   EMPTY_HOWTO (244),
1350   EMPTY_HOWTO (245),
1351   EMPTY_HOWTO (246),
1352   EMPTY_HOWTO (247),
1353   EMPTY_HOWTO (248),
1354   EMPTY_HOWTO (249),
1355   EMPTY_HOWTO (250),
1356   EMPTY_HOWTO (251),
1357   EMPTY_HOWTO (252),
1358   HOWTO (R_C6000_ALIGN,		/* type */
1359 	 0,			/* rightshift */
1360 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1361 	 0,			/* bitsize */
1362 	 FALSE,			/* pc_relative */
1363 	 0,			/* bitpos */
1364 	 complain_overflow_dont,/* complain_on_overflow */
1365 	 bfd_elf_generic_reloc,	/* special_function */
1366 	 "R_C6000_ALIGN",	/* name */
1367 	 TRUE,			/* partial_inplace */
1368 	 0,			/* src_mask */
1369 	 0,			/* dst_mask */
1370 	 FALSE),		/* pcrel_offset */
1371   HOWTO (R_C6000_FPHEAD,	/* type */
1372 	 0,			/* rightshift */
1373 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1374 	 0,			/* bitsize */
1375 	 FALSE,			/* pc_relative */
1376 	 0,			/* bitpos */
1377 	 complain_overflow_dont,/* complain_on_overflow */
1378 	 bfd_elf_generic_reloc,	/* special_function */
1379 	 "R_C6000_FPHEAD",	/* name */
1380 	 TRUE,			/* partial_inplace */
1381 	 0,			/* src_mask */
1382 	 0,			/* dst_mask */
1383 	 FALSE),		/* pcrel_offset */
1384   HOWTO (R_C6000_NOCMP,		/* type */
1385 	 0,			/* rightshift */
1386 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1387 	 0,			/* bitsize */
1388 	 FALSE,			/* pc_relative */
1389 	 0,			/* bitpos */
1390 	 complain_overflow_dont,/* complain_on_overflow */
1391 	 bfd_elf_generic_reloc,	/* special_function */
1392 	 "R_C6000_NOCMP",	/* name */
1393 	 TRUE,			/* partial_inplace */
1394 	 0,			/* src_mask */
1395 	 0,			/* dst_mask */
1396 	 FALSE)			/* pcrel_offset */
1397 };
1398 
1399 /* Map BFD relocations to ELF relocations.  */
1400 
1401 typedef struct
1402 {
1403   bfd_reloc_code_real_type bfd_reloc_val;
1404   enum elf_tic6x_reloc_type elf_reloc_val;
1405 } tic6x_reloc_map;
1406 
1407 static const tic6x_reloc_map elf32_tic6x_reloc_map[] =
1408   {
1409     { BFD_RELOC_NONE, R_C6000_NONE },
1410     { BFD_RELOC_32, R_C6000_ABS32 },
1411     { BFD_RELOC_16, R_C6000_ABS16 },
1412     { BFD_RELOC_8, R_C6000_ABS8 },
1413     { BFD_RELOC_C6000_PCR_S21, R_C6000_PCR_S21 },
1414     { BFD_RELOC_C6000_PCR_S12, R_C6000_PCR_S12 },
1415     { BFD_RELOC_C6000_PCR_S10, R_C6000_PCR_S10 },
1416     { BFD_RELOC_C6000_PCR_S7, R_C6000_PCR_S7 },
1417     { BFD_RELOC_C6000_ABS_S16, R_C6000_ABS_S16 },
1418     { BFD_RELOC_C6000_ABS_L16, R_C6000_ABS_L16 },
1419     { BFD_RELOC_C6000_ABS_H16, R_C6000_ABS_H16 },
1420     { BFD_RELOC_C6000_SBR_U15_B, R_C6000_SBR_U15_B },
1421     { BFD_RELOC_C6000_SBR_U15_H, R_C6000_SBR_U15_H },
1422     { BFD_RELOC_C6000_SBR_U15_W, R_C6000_SBR_U15_W },
1423     { BFD_RELOC_C6000_SBR_S16, R_C6000_SBR_S16 },
1424     { BFD_RELOC_C6000_SBR_L16_B, R_C6000_SBR_L16_B },
1425     { BFD_RELOC_C6000_SBR_L16_H, R_C6000_SBR_L16_H },
1426     { BFD_RELOC_C6000_SBR_L16_W, R_C6000_SBR_L16_W },
1427     { BFD_RELOC_C6000_SBR_H16_B, R_C6000_SBR_H16_B },
1428     { BFD_RELOC_C6000_SBR_H16_H, R_C6000_SBR_H16_H },
1429     { BFD_RELOC_C6000_SBR_H16_W, R_C6000_SBR_H16_W },
1430     { BFD_RELOC_C6000_SBR_GOT_U15_W, R_C6000_SBR_GOT_U15_W },
1431     { BFD_RELOC_C6000_SBR_GOT_L16_W, R_C6000_SBR_GOT_L16_W },
1432     { BFD_RELOC_C6000_SBR_GOT_H16_W, R_C6000_SBR_GOT_H16_W },
1433     { BFD_RELOC_C6000_DSBT_INDEX, R_C6000_DSBT_INDEX },
1434     { BFD_RELOC_C6000_PREL31, R_C6000_PREL31 },
1435     { BFD_RELOC_C6000_COPY, R_C6000_COPY },
1436     { BFD_RELOC_C6000_JUMP_SLOT, R_C6000_JUMP_SLOT },
1437     { BFD_RELOC_C6000_EHTYPE, R_C6000_EHTYPE },
1438     { BFD_RELOC_C6000_PCR_H16, R_C6000_PCR_H16 },
1439     { BFD_RELOC_C6000_PCR_L16, R_C6000_PCR_L16 },
1440     { BFD_RELOC_C6000_ALIGN, R_C6000_ALIGN },
1441     { BFD_RELOC_C6000_FPHEAD, R_C6000_FPHEAD },
1442     { BFD_RELOC_C6000_NOCMP, R_C6000_NOCMP }
1443   };
1444 
1445 static reloc_howto_type *
elf32_tic6x_reloc_type_lookup(bfd * abfd,bfd_reloc_code_real_type code)1446 elf32_tic6x_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1447 {
1448   unsigned int i;
1449 
1450   for (i = 0; i < ARRAY_SIZE (elf32_tic6x_reloc_map); i++)
1451     if (elf32_tic6x_reloc_map[i].bfd_reloc_val == code)
1452       {
1453 	enum elf_tic6x_reloc_type elf_reloc_val;
1454 	reloc_howto_type *howto;
1455 
1456 	elf_reloc_val = elf32_tic6x_reloc_map[i].elf_reloc_val;
1457 	if (elf32_tic6x_tdata (abfd)->use_rela_p)
1458 	  howto = &elf32_tic6x_howto_table[elf_reloc_val];
1459 	else
1460 	  howto = &elf32_tic6x_howto_table_rel[elf_reloc_val];
1461 
1462 	/* Some relocations are RELA-only; do not return them for
1463 	   REL.  */
1464 	if (howto->name == NULL)
1465 	  howto = NULL;
1466 
1467 	return howto;
1468       }
1469 
1470   return NULL;
1471 }
1472 
1473 static reloc_howto_type *
elf32_tic6x_reloc_name_lookup(bfd * abfd,const char * r_name)1474 elf32_tic6x_reloc_name_lookup (bfd *abfd, const char *r_name)
1475 {
1476   if (elf32_tic6x_tdata (abfd)->use_rela_p)
1477     {
1478       unsigned int i;
1479 
1480       for (i = 0; i < ARRAY_SIZE (elf32_tic6x_howto_table); i++)
1481 	if (elf32_tic6x_howto_table[i].name != NULL
1482 	    && strcasecmp (elf32_tic6x_howto_table[i].name, r_name) == 0)
1483 	  return &elf32_tic6x_howto_table[i];
1484     }
1485   else
1486     {
1487       unsigned int i;
1488 
1489       for (i = 0; i < ARRAY_SIZE (elf32_tic6x_howto_table_rel); i++)
1490 	if (elf32_tic6x_howto_table_rel[i].name != NULL
1491 	    && strcasecmp (elf32_tic6x_howto_table_rel[i].name, r_name) == 0)
1492 	  return &elf32_tic6x_howto_table_rel[i];
1493     }
1494 
1495   return NULL;
1496 }
1497 
1498 static bfd_boolean
elf32_tic6x_info_to_howto(bfd * abfd ATTRIBUTE_UNUSED,arelent * bfd_reloc,Elf_Internal_Rela * elf_reloc)1499 elf32_tic6x_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1500 			   Elf_Internal_Rela *elf_reloc)
1501 {
1502   unsigned int r_type;
1503 
1504   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1505   if (r_type >= ARRAY_SIZE (elf32_tic6x_howto_table))
1506     {
1507       /* xgettext:c-format */
1508       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1509 			  abfd, r_type);
1510       bfd_set_error (bfd_error_bad_value);
1511       return FALSE;
1512     }
1513 
1514   bfd_reloc->howto = &elf32_tic6x_howto_table[r_type];
1515   if (bfd_reloc->howto == NULL || bfd_reloc->howto->name == NULL)
1516     {
1517       /* xgettext:c-format */
1518       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1519 			  abfd, r_type);
1520       bfd_set_error (bfd_error_bad_value);
1521       return FALSE;
1522     }
1523 
1524   return TRUE;
1525 }
1526 
1527 static bfd_boolean
elf32_tic6x_info_to_howto_rel(bfd * abfd ATTRIBUTE_UNUSED,arelent * bfd_reloc,Elf_Internal_Rela * elf_reloc)1528 elf32_tic6x_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1529 			       Elf_Internal_Rela *elf_reloc)
1530 {
1531   unsigned int r_type;
1532 
1533   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1534   if (r_type >= ARRAY_SIZE (elf32_tic6x_howto_table_rel))
1535     {
1536       /* xgettext:c-format */
1537       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1538 			  abfd, r_type);
1539       bfd_set_error (bfd_error_bad_value);
1540       return FALSE;
1541     }
1542 
1543   bfd_reloc->howto = &elf32_tic6x_howto_table_rel[r_type];
1544   if (bfd_reloc->howto == NULL || bfd_reloc->howto->name == NULL)
1545     {
1546       /* xgettext:c-format */
1547       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1548 			  abfd, r_type);
1549       bfd_set_error (bfd_error_bad_value);
1550       return FALSE;
1551     }
1552 
1553   return TRUE;
1554 }
1555 
1556 void
elf32_tic6x_set_use_rela_p(bfd * abfd,bfd_boolean use_rela_p)1557 elf32_tic6x_set_use_rela_p (bfd *abfd, bfd_boolean use_rela_p)
1558 {
1559   elf32_tic6x_tdata (abfd)->use_rela_p = use_rela_p;
1560 }
1561 
1562 /* Create an entry in a C6X ELF linker hash table.  */
1563 
1564 static struct bfd_hash_entry *
elf32_tic6x_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)1565 elf32_tic6x_link_hash_newfunc (struct bfd_hash_entry *entry,
1566 			    struct bfd_hash_table *table,
1567 			    const char *string)
1568 {
1569   /* Allocate the structure if it has not already been allocated by a
1570      subclass.  */
1571   if (entry == NULL)
1572     {
1573       entry = bfd_hash_allocate (table,
1574 				 sizeof (struct elf32_tic6x_link_hash_entry));
1575       if (entry == NULL)
1576 	return entry;
1577     }
1578 
1579   /* Call the allocation method of the superclass.  */
1580   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1581   if (entry != NULL)
1582     {
1583       struct elf32_tic6x_link_hash_entry *eh;
1584 
1585       eh = (struct elf32_tic6x_link_hash_entry *) entry;
1586       eh->dyn_relocs = NULL;
1587     }
1588 
1589   return entry;
1590 }
1591 
1592 /* Create a C6X ELF linker hash table.  */
1593 
1594 static struct bfd_link_hash_table *
elf32_tic6x_link_hash_table_create(bfd * abfd)1595 elf32_tic6x_link_hash_table_create (bfd *abfd)
1596 {
1597   struct elf32_tic6x_link_hash_table *ret;
1598   bfd_size_type amt = sizeof (struct elf32_tic6x_link_hash_table);
1599 
1600   ret = bfd_zmalloc (amt);
1601   if (ret == NULL)
1602     return NULL;
1603 
1604   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1605 				      elf32_tic6x_link_hash_newfunc,
1606 				      sizeof (struct elf32_tic6x_link_hash_entry),
1607 				      TIC6X_ELF_DATA))
1608     {
1609       free (ret);
1610       return NULL;
1611     }
1612 
1613   ret->obfd = abfd;
1614   ret->elf.is_relocatable_executable = 1;
1615 
1616   return &ret->elf.root;
1617 }
1618 
1619 static bfd_boolean
elf32_tic6x_final_link(bfd * abfd,struct bfd_link_info * info)1620 elf32_tic6x_final_link (bfd *abfd, struct bfd_link_info *info)
1621 {
1622   if (bfd_link_pic (info))
1623     {
1624       obj_attribute *out_attr;
1625       out_attr = elf_known_obj_attributes_proc (abfd);
1626       if (out_attr[Tag_ABI_PIC].i == 0)
1627 	{
1628 	  _bfd_error_handler (_("warning: generating a shared library "
1629 				"containing non-PIC code"));
1630 	}
1631       if (out_attr[Tag_ABI_PID].i == 0)
1632 	{
1633 	  _bfd_error_handler (_("warning: generating a shared library "
1634 				"containing non-PID code"));
1635 	}
1636     }
1637   /* Invoke the regular ELF backend linker to do all the work.  */
1638   if (!bfd_elf_final_link (abfd, info))
1639     return FALSE;
1640 
1641   return TRUE;
1642 }
1643 
1644 /* Called to pass PARAMS to the backend.  We store them in the hash table
1645    associated with INFO.  */
1646 
1647 void
elf32_tic6x_setup(struct bfd_link_info * info,struct elf32_tic6x_params * params)1648 elf32_tic6x_setup (struct bfd_link_info *info,
1649 		   struct elf32_tic6x_params *params)
1650 {
1651   struct elf32_tic6x_link_hash_table *htab = elf32_tic6x_hash_table (info);
1652   htab->params = *params;
1653 }
1654 
1655 /* Determine if we're dealing with a DSBT object.  */
1656 
1657 static bfd_boolean
elf32_tic6x_using_dsbt(bfd * abfd)1658 elf32_tic6x_using_dsbt (bfd *abfd)
1659 {
1660   return bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC,
1661 				   Tag_ABI_DSBT);
1662 }
1663 
1664 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got and .dsbt
1665    sections in DYNOBJ, and set up shortcuts to them in our hash
1666    table.  */
1667 
1668 static bfd_boolean
elf32_tic6x_create_dynamic_sections(bfd * dynobj,struct bfd_link_info * info)1669 elf32_tic6x_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1670 {
1671   struct elf32_tic6x_link_hash_table *htab;
1672   flagword flags;
1673 
1674   htab = elf32_tic6x_hash_table (info);
1675   if (htab == NULL)
1676     return FALSE;
1677 
1678   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1679     return FALSE;
1680 
1681   /* Create .dsbt  */
1682   flags = (SEC_ALLOC | SEC_LOAD
1683 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1684   htab->dsbt = bfd_make_section_anyway_with_flags (dynobj, ".dsbt",
1685 						   flags);
1686   if (htab->dsbt == NULL
1687       || !bfd_set_section_alignment (htab->dsbt, 2)
1688       || !bfd_set_section_alignment (htab->elf.splt, 5))
1689     return FALSE;
1690 
1691   return TRUE;
1692 }
1693 
1694 static bfd_boolean
elf32_tic6x_mkobject(bfd * abfd)1695 elf32_tic6x_mkobject (bfd *abfd)
1696 {
1697   bfd_boolean ret;
1698 
1699   ret = bfd_elf_allocate_object (abfd, sizeof (struct elf32_tic6x_obj_tdata),
1700 				 TIC6X_ELF_DATA);
1701   if (ret)
1702     elf32_tic6x_set_use_rela_p (abfd, TRUE);
1703   return ret;
1704 }
1705 
1706 /* Install relocation RELA into section SRELA, incrementing its
1707    reloc_count.  */
1708 
1709 static void
elf32_tic6x_install_rela(bfd * output_bfd,asection * srela,Elf_Internal_Rela * rela)1710 elf32_tic6x_install_rela (bfd *output_bfd, asection *srela,
1711 			  Elf_Internal_Rela *rela)
1712 {
1713   bfd_byte *loc;
1714   bfd_vma off = srela->reloc_count++ * sizeof (Elf32_External_Rela);
1715   loc = srela->contents + off;
1716   BFD_ASSERT (off < srela->size);
1717   bfd_elf32_swap_reloca_out (output_bfd, rela, loc);
1718 }
1719 
1720 /* Create a dynamic reloc against the GOT at offset OFFSET.  The contents
1721    of the GOT at this offset have been initialized with the relocation.  */
1722 
1723 static void
elf32_tic6x_make_got_dynreloc(bfd * output_bfd,struct elf32_tic6x_link_hash_table * htab,asection * sym_sec,bfd_vma offset)1724 elf32_tic6x_make_got_dynreloc (bfd *output_bfd,
1725 			       struct elf32_tic6x_link_hash_table *htab,
1726 			       asection *sym_sec, bfd_vma offset)
1727 {
1728   asection *sgot = htab->elf.sgot;
1729   Elf_Internal_Rela outrel;
1730   int dynindx;
1731 
1732   outrel.r_offset = sgot->output_section->vma + sgot->output_offset + offset;
1733   outrel.r_addend = bfd_get_32 (output_bfd, sgot->contents + offset);
1734   if (sym_sec && sym_sec->output_section
1735       && ! bfd_is_abs_section (sym_sec->output_section)
1736       && ! bfd_is_und_section (sym_sec->output_section))
1737     {
1738       dynindx = elf_section_data (sym_sec->output_section)->dynindx;
1739       outrel.r_addend -= sym_sec->output_section->vma;
1740     }
1741   else
1742     {
1743       dynindx = 0;
1744     }
1745   outrel.r_info = ELF32_R_INFO (dynindx, R_C6000_ABS32);
1746   elf32_tic6x_install_rela (output_bfd, htab->elf.srelgot, &outrel);
1747 }
1748 
1749 /* Finish up dynamic symbol handling.  We set the contents of various
1750    dynamic sections here.  */
1751 
1752 static bfd_boolean
elf32_tic6x_finish_dynamic_symbol(bfd * output_bfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)1753 elf32_tic6x_finish_dynamic_symbol (bfd * output_bfd,
1754 				   struct bfd_link_info *info,
1755 				   struct elf_link_hash_entry *h,
1756 				   Elf_Internal_Sym * sym)
1757 {
1758   struct elf32_tic6x_link_hash_table *htab;
1759 
1760   htab = elf32_tic6x_hash_table (info);
1761 
1762   if (h->plt.offset != (bfd_vma) -1)
1763     {
1764       bfd_vma plt_index;
1765       bfd_vma got_section_offset, got_dp_offset, rela_offset;
1766       Elf_Internal_Rela rela;
1767       bfd_byte *loc;
1768       asection *plt, *gotplt, *relplt;
1769       const struct elf_backend_data *bed;
1770 
1771       bed = get_elf_backend_data (output_bfd);
1772 
1773       BFD_ASSERT (htab->elf.splt != NULL);
1774       plt = htab->elf.splt;
1775       gotplt = htab->elf.sgotplt;
1776       relplt = htab->elf.srelplt;
1777 
1778       /* This symbol has an entry in the procedure linkage table.  Set
1779 	 it up.  */
1780 
1781       if ((h->dynindx == -1
1782 	   && !((h->forced_local || bfd_link_executable (info))
1783 		&& h->def_regular
1784 		&& h->type == STT_GNU_IFUNC))
1785 	  || plt == NULL
1786 	  || gotplt == NULL
1787 	  || relplt == NULL)
1788 	abort ();
1789 
1790       /* Get the index in the procedure linkage table which
1791 	 corresponds to this symbol.  This is the index of this symbol
1792 	 in all the symbols for which we are making plt entries.  The
1793 	 first entry in the procedure linkage table is reserved.
1794 
1795 	 Get the offset into the .got table of the entry that
1796 	 corresponds to this function.  Each .got entry is 4 bytes.
1797 	 The first three are reserved.
1798 
1799 	 For static executables, we don't reserve anything.  */
1800 
1801       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1802       got_section_offset = plt_index + bed->got_header_size / 4;
1803       got_dp_offset = got_section_offset + htab->params.dsbt_size;
1804       rela_offset = plt_index * sizeof (Elf32_External_Rela);
1805 
1806       got_section_offset *= 4;
1807 
1808       /* Fill in the entry in the procedure linkage table.  */
1809 
1810       /* ldw .d2t2 *+B14($GOT(f)), b2 */
1811       bfd_put_32 (output_bfd, got_dp_offset << 8 | 0x0100006e,
1812 		  plt->contents + h->plt.offset);
1813       /* mvk .s2 low(rela_offset), b0 */
1814       bfd_put_32 (output_bfd, (rela_offset & 0xffff) << 7 | 0x0000002a,
1815 		  plt->contents + h->plt.offset + 4);
1816       /* mvkh .s2 high(rela_offset), b0 */
1817       bfd_put_32 (output_bfd, ((rela_offset >> 16) & 0xffff) << 7 | 0x0000006a,
1818 		  plt->contents + h->plt.offset + 8);
1819       /* nop 2 */
1820       bfd_put_32 (output_bfd, 0x00002000,
1821 		  plt->contents + h->plt.offset + 12);
1822       /* b .s2 b2 */
1823       bfd_put_32 (output_bfd, 0x00080362,
1824 		  plt->contents + h->plt.offset + 16);
1825       /* nop 5 */
1826       bfd_put_32 (output_bfd, 0x00008000,
1827 		  plt->contents + h->plt.offset + 20);
1828 
1829       /* Fill in the entry in the global offset table.  */
1830       bfd_put_32 (output_bfd,
1831 		  (plt->output_section->vma + plt->output_offset),
1832 		  gotplt->contents + got_section_offset);
1833 
1834       /* Fill in the entry in the .rel.plt section.  */
1835       rela.r_offset = (gotplt->output_section->vma
1836 		       + gotplt->output_offset
1837 		       + got_section_offset);
1838       rela.r_info = ELF32_R_INFO (h->dynindx, R_C6000_JUMP_SLOT);
1839       rela.r_addend = 0;
1840       loc = relplt->contents + rela_offset;
1841       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1842 
1843       if (!h->def_regular)
1844 	{
1845 	  /* Mark the symbol as undefined, rather than as defined in
1846 	     the .plt section.  */
1847 	  sym->st_shndx = SHN_UNDEF;
1848 	  sym->st_value = 0;
1849 	}
1850     }
1851 
1852   if (h->got.offset != (bfd_vma) -1)
1853     {
1854       asection *sgot;
1855       asection *srela;
1856 
1857       /* This symbol has an entry in the global offset table.
1858 	 Set it up.  */
1859 
1860       sgot = htab->elf.sgot;
1861       srela = htab->elf.srelgot;
1862       BFD_ASSERT (sgot != NULL && srela != NULL);
1863 
1864       /* If this is a -Bsymbolic link, and the symbol is defined
1865 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
1866 	 the symbol was forced to be local because of a version file.
1867 	 The entry in the global offset table will already have been
1868 	 initialized in the relocate_section function.  */
1869       if (bfd_link_pic (info)
1870 	  && (SYMBOLIC_BIND (info, h)
1871 	      || h->dynindx == -1 || h->forced_local) && h->def_regular)
1872 	{
1873 	  asection *s = h->root.u.def.section;
1874 	  elf32_tic6x_make_got_dynreloc (output_bfd, htab, s,
1875 			     h->got.offset & ~(bfd_vma) 1);
1876 	}
1877       else
1878 	{
1879 	  Elf_Internal_Rela outrel;
1880 	  bfd_put_32 (output_bfd, (bfd_vma) 0,
1881 		      sgot->contents + (h->got.offset & ~(bfd_vma) 1));
1882 	  outrel.r_offset = (sgot->output_section->vma
1883 			   + sgot->output_offset
1884 			   + (h->got.offset & ~(bfd_vma) 1));
1885 	  outrel.r_info = ELF32_R_INFO (h->dynindx, R_C6000_ABS32);
1886 	  outrel.r_addend = 0;
1887 
1888 	  elf32_tic6x_install_rela (output_bfd, srela, &outrel);
1889 	}
1890     }
1891 
1892   if (h->needs_copy)
1893     {
1894       Elf_Internal_Rela rel;
1895       asection *s;
1896 
1897       /* This symbol needs a copy reloc.  Set it up.  */
1898 
1899       if (h->dynindx == -1
1900 	  || (h->root.type != bfd_link_hash_defined
1901 	      && h->root.type != bfd_link_hash_defweak)
1902 	  || htab->elf.srelbss == NULL
1903 	  || htab->elf.sreldynrelro == NULL)
1904 	abort ();
1905 
1906       rel.r_offset = (h->root.u.def.value
1907 		      + h->root.u.def.section->output_section->vma
1908 		      + h->root.u.def.section->output_offset);
1909       rel.r_info = ELF32_R_INFO (h->dynindx, R_C6000_COPY);
1910       rel.r_addend = 0;
1911       if (h->root.u.def.section == htab->elf.sdynrelro)
1912 	s = htab->elf.sreldynrelro;
1913       else
1914 	s = htab->elf.srelbss;
1915 
1916       elf32_tic6x_install_rela (output_bfd, s, &rel);
1917     }
1918 
1919   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1920   if (h == elf_hash_table (info)->hdynamic
1921       || h == elf_hash_table (info)->hgot)
1922     sym->st_shndx = SHN_ABS;
1923 
1924   return TRUE;
1925 }
1926 
1927 /* Unwinding tables are not referenced directly.  This pass marks them as
1928    required if the corresponding code section is marked.  */
1929 
1930 static bfd_boolean
elf32_tic6x_gc_mark_extra_sections(struct bfd_link_info * info,elf_gc_mark_hook_fn gc_mark_hook)1931 elf32_tic6x_gc_mark_extra_sections (struct bfd_link_info *info,
1932 				    elf_gc_mark_hook_fn gc_mark_hook)
1933 {
1934   bfd *sub;
1935   Elf_Internal_Shdr **elf_shdrp;
1936   bfd_boolean again;
1937 
1938   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
1939 
1940   /* Marking EH data may cause additional code sections to be marked,
1941      requiring multiple passes.  */
1942   again = TRUE;
1943   while (again)
1944     {
1945       again = FALSE;
1946       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
1947 	{
1948 	  asection *o;
1949 
1950 	  if (! is_tic6x_elf (sub))
1951 	    continue;
1952 
1953 	  elf_shdrp = elf_elfsections (sub);
1954 	  for (o = sub->sections; o != NULL; o = o->next)
1955 	    {
1956 	      Elf_Internal_Shdr *hdr;
1957 
1958 	      hdr = &elf_section_data (o)->this_hdr;
1959 	      if (hdr->sh_type == SHT_C6000_UNWIND
1960 		  && hdr->sh_link
1961 		  && hdr->sh_link < elf_numsections (sub)
1962 		  && !o->gc_mark
1963 		  && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
1964 		{
1965 		  again = TRUE;
1966 		  if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
1967 		    return FALSE;
1968 		}
1969 	    }
1970 	}
1971     }
1972 
1973   return TRUE;
1974 }
1975 
1976 /* Return TRUE if this is an unwinding table index.  */
1977 
1978 static bfd_boolean
is_tic6x_elf_unwind_section_name(const char * name)1979 is_tic6x_elf_unwind_section_name (const char *name)
1980 {
1981   return (CONST_STRNEQ (name, ELF_STRING_C6000_unwind)
1982 	  || CONST_STRNEQ (name, ELF_STRING_C6000_unwind_once));
1983 }
1984 
1985 
1986 /* Set the type and flags for an unwinding index table.  We do this by
1987    the section name, which is a hack, but ought to work.  */
1988 
1989 static bfd_boolean
elf32_tic6x_fake_sections(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Shdr * hdr,asection * sec)1990 elf32_tic6x_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1991 			   Elf_Internal_Shdr *hdr, asection *sec)
1992 {
1993   const char * name;
1994 
1995   name = bfd_section_name (sec);
1996 
1997   if (is_tic6x_elf_unwind_section_name (name))
1998     {
1999       hdr->sh_type = SHT_C6000_UNWIND;
2000       hdr->sh_flags |= SHF_LINK_ORDER;
2001     }
2002 
2003   return TRUE;
2004 }
2005 
2006 /* Find dynamic relocs for H that apply to read-only sections.  */
2007 
2008 static asection *
readonly_dynrelocs(struct elf_link_hash_entry * h)2009 readonly_dynrelocs (struct elf_link_hash_entry *h)
2010 {
2011   struct elf_dyn_relocs *p;
2012   struct elf32_tic6x_link_hash_entry *eh
2013     = (struct elf32_tic6x_link_hash_entry *) h;
2014 
2015   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2016     {
2017       asection *s = p->sec->output_section;
2018 
2019       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2020 	return p->sec;
2021     }
2022   return NULL;
2023 }
2024 
2025 /* Adjust a symbol defined by a dynamic object and referenced by a
2026    regular object.  The current definition is in some section of the
2027    dynamic object, but we're not including those sections.  We have to
2028    change the definition to something the rest of the link can
2029    understand.  */
2030 
2031 static bfd_boolean
elf32_tic6x_adjust_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2032 elf32_tic6x_adjust_dynamic_symbol (struct bfd_link_info *info,
2033 				   struct elf_link_hash_entry *h)
2034 {
2035   struct elf32_tic6x_link_hash_table *htab;
2036   bfd *dynobj;
2037   asection *s, *srel;
2038 
2039   dynobj = elf_hash_table (info)->dynobj;
2040 
2041   /* Make sure we know what is going on here.  */
2042   BFD_ASSERT (dynobj != NULL
2043 	      && (h->needs_plt
2044 		  || h->is_weakalias
2045 		  || (h->def_dynamic && h->ref_regular && !h->def_regular)));
2046 
2047   /* If this is a function, put it in the procedure linkage table.  We
2048      will fill in the contents of the procedure linkage table later,
2049      when we know the address of the .got section.  */
2050   if (h->type == STT_FUNC
2051       || h->needs_plt)
2052     {
2053       if (h->plt.refcount <= 0
2054 	  || SYMBOL_CALLS_LOCAL (info, h)
2055 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2056 	      && h->root.type == bfd_link_hash_undefweak))
2057 	{
2058 	  /* This case can occur if we saw a PLT32 reloc in an input
2059 	     file, but the symbol was never referred to by a dynamic
2060 	     object, or if all references were garbage collected.  In
2061 	     such a case, we don't actually need to build a procedure
2062 	     linkage table, and we can just do a PC32 reloc instead.  */
2063 	  h->plt.offset = (bfd_vma) -1;
2064 	  h->needs_plt = 0;
2065 	}
2066 
2067       return TRUE;
2068     }
2069 
2070   /* If this is a weak symbol, and there is a real definition, the
2071      processor independent code will have arranged for us to see the
2072      real definition first, and we can just use the same value.  */
2073   if (h->is_weakalias)
2074     {
2075       struct elf_link_hash_entry *def = weakdef (h);
2076       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2077       h->root.u.def.section = def->root.u.def.section;
2078       h->root.u.def.value = def->root.u.def.value;
2079       h->non_got_ref = def->non_got_ref;
2080       return TRUE;
2081     }
2082 
2083   /* This is a reference to a symbol defined by a dynamic object which
2084      is not a function.  */
2085 
2086   /* If we are creating a shared library, we must presume that the
2087      only references to the symbol are via the global offset table.
2088      For such cases we need not do anything here; the relocations will
2089      be handled correctly by relocate_section.  */
2090   if (bfd_link_pic (info))
2091     return TRUE;
2092 
2093   /* If there are no references to this symbol that do not use the
2094      GOT, we don't need to generate a copy reloc.  */
2095   if (!h->non_got_ref)
2096     return TRUE;
2097 
2098   /* If -z nocopyreloc was given, we won't generate them either.  */
2099   if (info->nocopyreloc)
2100     {
2101       h->non_got_ref = 0;
2102       return TRUE;
2103     }
2104 
2105   htab = elf32_tic6x_hash_table (info);
2106   if (htab == NULL)
2107     return FALSE;
2108 
2109   /* We must allocate the symbol in our .dynbss section, which will
2110      become part of the .bss section of the executable.  There will be
2111      an entry for this symbol in the .dynsym section.  The dynamic
2112      object will contain position independent code, so all references
2113      from the dynamic object to this symbol will go through the global
2114      offset table.  The dynamic linker will use the .dynsym entry to
2115      determine the address it must put in the global offset table, so
2116      both the dynamic object and the regular object will refer to the
2117      same memory location for the variable.  */
2118 
2119   /* We must generate a R_C6000_COPY reloc to tell the dynamic linker to
2120      copy the initial value out of the dynamic object and into the
2121      runtime process image.  */
2122   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2123     {
2124       s = htab->elf.sdynrelro;
2125       srel = htab->elf.sreldynrelro;
2126     }
2127   else
2128     {
2129       s = htab->elf.sdynbss;
2130       srel = htab->elf.srelbss;
2131     }
2132   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2133     {
2134       srel->size += sizeof (Elf32_External_Rela);
2135       h->needs_copy = 1;
2136     }
2137 
2138   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2139 }
2140 
2141 static bfd_boolean
elf32_tic6x_new_section_hook(bfd * abfd,asection * sec)2142 elf32_tic6x_new_section_hook (bfd *abfd, asection *sec)
2143 {
2144   bfd_boolean ret;
2145 
2146   /* Allocate target specific section data.  */
2147   if (!sec->used_by_bfd)
2148     {
2149       _tic6x_elf_section_data *sdata;
2150       bfd_size_type amt = sizeof (*sdata);
2151 
2152       sdata = (_tic6x_elf_section_data *) bfd_zalloc (abfd, amt);
2153       if (sdata == NULL)
2154 	return FALSE;
2155       sec->used_by_bfd = sdata;
2156     }
2157 
2158   ret = _bfd_elf_new_section_hook (abfd, sec);
2159   sec->use_rela_p = elf32_tic6x_tdata (abfd)->use_rela_p;
2160 
2161   return ret;
2162 }
2163 
2164 /* Return true if relocation REL against section SEC is a REL rather
2165    than RELA relocation.  RELOCS is the first relocation in the
2166    section and ABFD is the bfd that contains SEC.  */
2167 
2168 static bfd_boolean
elf32_tic6x_rel_relocation_p(bfd * abfd,asection * sec,const Elf_Internal_Rela * relocs,const Elf_Internal_Rela * rel)2169 elf32_tic6x_rel_relocation_p (bfd *abfd, asection *sec,
2170 			      const Elf_Internal_Rela *relocs,
2171 			      const Elf_Internal_Rela *rel)
2172 {
2173   Elf_Internal_Shdr *rel_hdr;
2174   const struct elf_backend_data *bed;
2175 
2176   /* To determine which flavor of relocation this is, we depend on the
2177      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
2178   rel_hdr = elf_section_data (sec)->rel.hdr;
2179   if (rel_hdr == NULL)
2180     return FALSE;
2181   bed = get_elf_backend_data (abfd);
2182   return ((size_t) (rel - relocs)
2183 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
2184 }
2185 
2186 /* We need dynamic symbols for every section, since segments can
2187    relocate independently.  */
2188 static bfd_boolean
elf32_tic6x_link_omit_section_dynsym(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * p)2189 elf32_tic6x_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2190 				      struct bfd_link_info *info ATTRIBUTE_UNUSED,
2191 				      asection *p)
2192 {
2193   switch (elf_section_data (p)->this_hdr.sh_type)
2194     {
2195     case SHT_PROGBITS:
2196     case SHT_NOBITS:
2197       /* If sh_type is yet undecided, assume it could be
2198 	 SHT_PROGBITS/SHT_NOBITS.  */
2199     case SHT_NULL:
2200       return FALSE;
2201 
2202       /* There shouldn't be section relative relocations
2203 	 against any other section.  */
2204     default:
2205       return TRUE;
2206     }
2207 }
2208 
2209 static bfd_boolean
elf32_tic6x_relocate_section(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * relocs,Elf_Internal_Sym * local_syms,asection ** local_sections)2210 elf32_tic6x_relocate_section (bfd *output_bfd,
2211 			      struct bfd_link_info *info,
2212 			      bfd *input_bfd,
2213 			      asection *input_section,
2214 			      bfd_byte *contents,
2215 			      Elf_Internal_Rela *relocs,
2216 			      Elf_Internal_Sym *local_syms,
2217 			      asection **local_sections)
2218 {
2219   struct elf32_tic6x_link_hash_table *htab;
2220   Elf_Internal_Shdr *symtab_hdr;
2221   struct elf_link_hash_entry **sym_hashes;
2222   bfd_vma *local_got_offsets;
2223   Elf_Internal_Rela *rel;
2224   Elf_Internal_Rela *relend;
2225   bfd_boolean ok = TRUE;
2226 
2227   htab = elf32_tic6x_hash_table (info);
2228   symtab_hdr = & elf_symtab_hdr (input_bfd);
2229   sym_hashes = elf_sym_hashes (input_bfd);
2230   local_got_offsets = elf_local_got_offsets (input_bfd);
2231 
2232   relend = relocs + input_section->reloc_count;
2233 
2234   for (rel = relocs; rel < relend; rel ++)
2235     {
2236       int r_type;
2237       unsigned long r_symndx;
2238       arelent bfd_reloc;
2239       reloc_howto_type *howto;
2240       Elf_Internal_Sym *sym;
2241       asection *sec;
2242       struct elf_link_hash_entry *h;
2243       bfd_vma off, off2, relocation;
2244       bfd_boolean unresolved_reloc;
2245       bfd_reloc_status_type r;
2246       struct bfd_link_hash_entry *sbh;
2247       bfd_boolean is_rel;
2248       bfd_boolean res;
2249 
2250       r_type = ELF32_R_TYPE (rel->r_info);
2251       r_symndx = ELF32_R_SYM (rel->r_info);
2252 
2253       is_rel = elf32_tic6x_rel_relocation_p (input_bfd, input_section,
2254 					     relocs, rel);
2255 
2256       if (is_rel)
2257 	res = elf32_tic6x_info_to_howto_rel (input_bfd, &bfd_reloc, rel);
2258       else
2259 	res = elf32_tic6x_info_to_howto (input_bfd, &bfd_reloc, rel);
2260 
2261       if (!res || (howto = bfd_reloc.howto) == NULL)
2262 	{
2263 	  bfd_set_error (bfd_error_bad_value);
2264 	  return FALSE;
2265 	}
2266 
2267       h = NULL;
2268       sym = NULL;
2269       sec = NULL;
2270       unresolved_reloc = FALSE;
2271 
2272       if (r_symndx < symtab_hdr->sh_info)
2273 	{
2274 	  sym = local_syms + r_symndx;
2275 	  sec = local_sections[r_symndx];
2276 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2277 	}
2278       else
2279 	{
2280 	  bfd_boolean warned, ignored;
2281 
2282 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2283 				   r_symndx, symtab_hdr, sym_hashes,
2284 				   h, sec, relocation,
2285 				   unresolved_reloc, warned, ignored);
2286 	}
2287 
2288       if (sec != NULL && discarded_section (sec))
2289 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2290 					 rel, 1, relend, howto, 0, contents);
2291 
2292       if (bfd_link_relocatable (info))
2293 	{
2294 	  if (is_rel
2295 	      && sym != NULL
2296 	      && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2297 	    {
2298 	      rel->r_addend = 0;
2299 	      relocation = sec->output_offset + sym->st_value;
2300 	      r = _bfd_relocate_contents (howto, input_bfd, relocation,
2301 					  contents + rel->r_offset);
2302 	      goto done_reloc;
2303 	    }
2304 	  continue;
2305 	}
2306 
2307       switch (r_type)
2308 	{
2309 	case R_C6000_NONE:
2310 	case R_C6000_ALIGN:
2311 	case R_C6000_FPHEAD:
2312 	case R_C6000_NOCMP:
2313 	  /* No action needed.  */
2314 	  continue;
2315 
2316 	case R_C6000_PCR_S21:
2317 	  /* A branch to an undefined weak symbol is turned into a
2318 	     "b .s2 B3" instruction if the existing insn is of the
2319 	     form "b .s2 symbol".  */
2320 	  if (h ? h->root.type == bfd_link_hash_undefweak
2321 	      && (htab->elf.splt == NULL || h->plt.offset == (bfd_vma) -1)
2322 	      : r_symndx != STN_UNDEF && bfd_is_und_section (sec))
2323 	    {
2324 	      unsigned long oldval;
2325 	      oldval = bfd_get_32 (input_bfd, contents + rel->r_offset);
2326 
2327 	      if ((oldval & 0x7e) == 0x12)
2328 		{
2329 		  oldval &= 0xF0000001;
2330 		  bfd_put_32 (input_bfd, oldval | 0x000c0362,
2331 			      contents + rel->r_offset);
2332 		  r = bfd_reloc_ok;
2333 		  goto done_reloc;
2334 		}
2335 	    }
2336 	  /* Fall through.  */
2337 
2338 	case R_C6000_PCR_S12:
2339 	case R_C6000_PCR_S10:
2340 	case R_C6000_PCR_S7:
2341 	  if (h != NULL
2342 	      && h->plt.offset != (bfd_vma) -1
2343 	      && htab->elf.splt != NULL)
2344 	    {
2345 	      relocation = (htab->elf.splt->output_section->vma
2346 			    + htab->elf.splt->output_offset
2347 			    + h->plt.offset);
2348 	    }
2349 
2350 	  /* Generic PC-relative handling produces a value relative to
2351 	     the exact location of the relocation.  Adjust it to be
2352 	     relative to the start of the fetch packet instead.  */
2353 	  relocation += (input_section->output_section->vma
2354 			 + input_section->output_offset
2355 			 + rel->r_offset) & 0x1f;
2356 	  unresolved_reloc = FALSE;
2357 	  break;
2358 
2359 	case R_C6000_PCR_H16:
2360 	case R_C6000_PCR_L16:
2361 	  off = (input_section->output_section->vma
2362 		 + input_section->output_offset
2363 		 + rel->r_offset);
2364 	  /* These must be calculated as R = S - FP(FP(PC) - A).
2365 	     PC, here, is the value we just computed in OFF.  RELOCATION
2366 	     has the address of S + A. */
2367 	  relocation -= rel->r_addend;
2368 	  off2 = ((off & ~(bfd_vma)0x1f) - rel->r_addend) & (bfd_vma)~0x1f;
2369 	  off2 = relocation - off2;
2370 	  relocation = off + off2;
2371 	  break;
2372 
2373 	case R_C6000_DSBT_INDEX:
2374 	  relocation = elf32_tic6x_hash_table (info)->params.dsbt_index;
2375 	  if (!bfd_link_pic (info) || relocation != 0)
2376 	    break;
2377 
2378 	  /* fall through */
2379 	case R_C6000_ABS32:
2380 	case R_C6000_ABS16:
2381 	case R_C6000_ABS8:
2382 	case R_C6000_ABS_S16:
2383 	case R_C6000_ABS_L16:
2384 	case R_C6000_ABS_H16:
2385 	  /* When generating a shared object or relocatable executable, these
2386 	     relocations are copied into the output file to be resolved at
2387 	     run time.  */
2388 	  if ((bfd_link_pic (info) || elf32_tic6x_using_dsbt (output_bfd))
2389 	      && (input_section->flags & SEC_ALLOC)
2390 	      && (h == NULL
2391 		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2392 		  || h->root.type != bfd_link_hash_undefweak))
2393 	    {
2394 	      Elf_Internal_Rela outrel;
2395 	      bfd_boolean skip, relocate;
2396 	      asection *sreloc;
2397 
2398 	      unresolved_reloc = FALSE;
2399 
2400 	      sreloc = elf_section_data (input_section)->sreloc;
2401 	      BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
2402 
2403 	      skip = FALSE;
2404 	      relocate = FALSE;
2405 
2406 	      outrel.r_offset =
2407 		_bfd_elf_section_offset (output_bfd, info, input_section,
2408 					 rel->r_offset);
2409 	      if (outrel.r_offset == (bfd_vma) -1)
2410 		skip = TRUE;
2411 	      else if (outrel.r_offset == (bfd_vma) -2)
2412 		skip = TRUE, relocate = TRUE;
2413 	      outrel.r_offset += (input_section->output_section->vma
2414 				  + input_section->output_offset);
2415 
2416 	      if (skip)
2417 		memset (&outrel, 0, sizeof outrel);
2418 	      else if (h != NULL
2419 		       && h->dynindx != -1
2420 		       && (!bfd_link_pic (info)
2421 			   || !SYMBOLIC_BIND (info, h)
2422 			   || !h->def_regular))
2423 		{
2424 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2425 		  outrel.r_addend = rel->r_addend;
2426 		}
2427 	      else
2428 		{
2429 		  long indx;
2430 
2431 		  outrel.r_addend = relocation + rel->r_addend;
2432 
2433 		  if (bfd_is_abs_section (sec))
2434 		    indx = 0;
2435 		  else if (sec == NULL || sec->owner == NULL)
2436 		    {
2437 		      bfd_set_error (bfd_error_bad_value);
2438 		      return FALSE;
2439 		    }
2440 		  else
2441 		    {
2442 		      asection *osec;
2443 
2444 		      osec = sec->output_section;
2445 		      indx = elf_section_data (osec)->dynindx;
2446 		      outrel.r_addend -= osec->vma;
2447 		      BFD_ASSERT (indx != 0);
2448 		    }
2449 
2450 		  outrel.r_info = ELF32_R_INFO (indx, r_type);
2451 		}
2452 
2453 	      elf32_tic6x_install_rela (output_bfd, sreloc, &outrel);
2454 
2455 	      /* If this reloc is against an external symbol, we do not want to
2456 		 fiddle with the addend.  Otherwise, we need to include the symbol
2457 		 value so that it becomes an addend for the dynamic reloc.  */
2458 	      if (! relocate)
2459 		continue;
2460 	    }
2461 
2462 	  /* Generic logic OK.  */
2463 	  break;
2464 
2465 	case R_C6000_SBR_U15_B:
2466 	case R_C6000_SBR_U15_H:
2467 	case R_C6000_SBR_U15_W:
2468 	case R_C6000_SBR_S16:
2469 	case R_C6000_SBR_L16_B:
2470 	case R_C6000_SBR_L16_H:
2471 	case R_C6000_SBR_L16_W:
2472 	case R_C6000_SBR_H16_B:
2473 	case R_C6000_SBR_H16_H:
2474 	case R_C6000_SBR_H16_W:
2475 	  sbh = bfd_link_hash_lookup (info->hash, "__c6xabi_DSBT_BASE",
2476 				      FALSE, FALSE, TRUE);
2477 	  if (sbh != NULL
2478 	      && (sbh->type == bfd_link_hash_defined
2479 		  || sbh->type == bfd_link_hash_defweak))
2480 	    {
2481 	      if (h ? (h->root.type == bfd_link_hash_undefweak
2482 		       && (htab->elf.splt == NULL
2483 			   || h->plt.offset == (bfd_vma) -1))
2484 		  : r_symndx != STN_UNDEF && bfd_is_und_section (sec))
2485 		relocation = 0;
2486 	      else
2487 		relocation -= (sbh->u.def.value
2488 			       + sbh->u.def.section->output_section->vma
2489 			       + sbh->u.def.section->output_offset);
2490 	    }
2491 	  else
2492 	    {
2493 	      _bfd_error_handler (_("%pB: SB-relative relocation but "
2494 				    "__c6xabi_DSBT_BASE not defined"),
2495 				  input_bfd);
2496 	      ok = FALSE;
2497 	      continue;
2498 	    }
2499 	  break;
2500 
2501 	case R_C6000_SBR_GOT_U15_W:
2502 	case R_C6000_SBR_GOT_L16_W:
2503 	case R_C6000_SBR_GOT_H16_W:
2504 	case R_C6000_EHTYPE:
2505 	  /* Relocation is to the entry for this symbol in the global
2506 	     offset table.  */
2507 	  if (htab->elf.sgot == NULL)
2508 	    abort ();
2509 
2510 	  if (h != NULL)
2511 	    {
2512 	      bfd_boolean dyn;
2513 
2514 	      off = h->got.offset;
2515 	      dyn = htab->elf.dynamic_sections_created;
2516 	      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2517 						     bfd_link_pic (info),
2518 						     h)
2519 		  || (bfd_link_pic (info)
2520 		      && SYMBOL_REFERENCES_LOCAL (info, h))
2521 		  || (ELF_ST_VISIBILITY (h->other)
2522 		      && h->root.type == bfd_link_hash_undefweak))
2523 		{
2524 		  /* This is actually a static link, or it is a
2525 		     -Bsymbolic link and the symbol is defined
2526 		     locally, or the symbol was forced to be local
2527 		     because of a version file.  We must initialize
2528 		     this entry in the global offset table.  Since the
2529 		     offset must always be a multiple of 4, we use the
2530 		     least significant bit to record whether we have
2531 		     initialized it already.
2532 
2533 		     When doing a dynamic link, we create a .rel.got
2534 		     relocation entry to initialize the value.  This
2535 		     is done in the finish_dynamic_symbol routine.  */
2536 		  if ((off & 1) != 0)
2537 		    off &= ~1;
2538 		  else
2539 		    {
2540 		      bfd_put_32 (output_bfd, relocation,
2541 				  htab->elf.sgot->contents + off);
2542 		      h->got.offset |= 1;
2543 
2544 		      if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2545 							    bfd_link_pic (info),
2546 							    h)
2547 			  && !(ELF_ST_VISIBILITY (h->other)
2548 			       && h->root.type == bfd_link_hash_undefweak))
2549 			elf32_tic6x_make_got_dynreloc (output_bfd, htab, sec,
2550 						       off);
2551 		    }
2552 		}
2553 	      else
2554 		unresolved_reloc = FALSE;
2555 	    }
2556 	  else
2557 	    {
2558 	      if (local_got_offsets == NULL)
2559 		abort ();
2560 
2561 	      off = local_got_offsets[r_symndx];
2562 
2563 	      /* The offset must always be a multiple of 4.  We use
2564 		 the least significant bit to record whether we have
2565 		 already generated the necessary reloc.  */
2566 	      if ((off & 1) != 0)
2567 		off &= ~1;
2568 	      else
2569 		{
2570 		  bfd_put_32 (output_bfd, relocation,
2571 			      htab->elf.sgot->contents + off);
2572 
2573 		  if (bfd_link_pic (info) || elf32_tic6x_using_dsbt (output_bfd))
2574 		    elf32_tic6x_make_got_dynreloc (output_bfd, htab, sec, off);
2575 
2576 		  local_got_offsets[r_symndx] |= 1;
2577 		}
2578 	    }
2579 
2580 	  if (off >= (bfd_vma) -2)
2581 	    abort ();
2582 
2583 	  if (htab->dsbt)
2584 	    relocation = (htab->elf.sgot->output_section->vma
2585 			  + htab->elf.sgot->output_offset + off
2586 			  - htab->dsbt->output_section->vma
2587 			  - htab->dsbt->output_offset);
2588 	  else
2589 	    relocation = (htab->elf.sgot->output_section->vma
2590 			  + htab->elf.sgot->output_offset + off
2591 			  - htab->elf.sgotplt->output_section->vma
2592 			  - htab->elf.sgotplt->output_offset);
2593 
2594 	  if (rel->r_addend != 0)
2595 	    {
2596 	      /* We can't do anything for a relocation which is against
2597 		 a symbol *plus offset*.  GOT holds relocations for
2598 		 symbols.  Make this an error; the compiler isn't
2599 		 allowed to pass us these kinds of things.  */
2600 	      if (h == NULL)
2601 		_bfd_error_handler
2602 		  /* xgettext:c-format */
2603 		  (_("%pB, section %pA: relocation %s with non-zero addend %"
2604 		     PRId64 " against local symbol"),
2605 		   input_bfd,
2606 		   input_section,
2607 		   elf32_tic6x_howto_table[r_type].name,
2608 		   (int64_t) rel->r_addend);
2609 	      else
2610 		_bfd_error_handler
2611 		  /* xgettext:c-format */
2612 		  (_("%pB, section %pA: relocation %s with non-zero addend %"
2613 		     PRId64 " against symbol `%s'"),
2614 		   input_bfd,
2615 		   input_section,
2616 		   elf32_tic6x_howto_table[r_type].name,
2617 		   (int64_t) rel->r_addend,
2618 		   h->root.root.string[0] != '\0' ? h->root.root.string
2619 		   : _("[whose name is lost]"));
2620 
2621 	      bfd_set_error (bfd_error_bad_value);
2622 	      return FALSE;
2623 	    }
2624 	  break;
2625 
2626 	case R_C6000_PREL31:
2627 	  if (h != NULL
2628 	      && h->plt.offset != (bfd_vma) -1
2629 	      && htab->elf.splt != NULL)
2630 	    {
2631 	      relocation = (htab->elf.splt->output_section->vma
2632 			    + htab->elf.splt->output_offset
2633 			    + h->plt.offset);
2634 	    }
2635 	  break;
2636 
2637 	case R_C6000_COPY:
2638 	  /* Invalid in relocatable object.  */
2639 	default:
2640 	  /* Unknown relocation.  */
2641 	  /* xgettext:c-format */
2642 	  _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2643 			      input_bfd, r_type);
2644 	  bfd_set_error (bfd_error_bad_value);
2645 	  ok = FALSE;
2646 	  continue;
2647 	}
2648 
2649       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2650 				    contents, rel->r_offset,
2651 				    relocation, rel->r_addend);
2652 
2653     done_reloc:
2654       if (r == bfd_reloc_ok
2655 	  && howto->complain_on_overflow == complain_overflow_bitfield)
2656 	{
2657 	  /* Generic overflow handling accepts cases the ABI says
2658 	     should be rejected for R_C6000_ABS16 and
2659 	     R_C6000_ABS8.  */
2660 	  bfd_vma value = (relocation + rel->r_addend) & 0xffffffff;
2661 	  bfd_vma sbit = 1 << (howto->bitsize - 1);
2662 	  bfd_vma sbits = (-(bfd_vma) sbit) & 0xffffffff;
2663 	  bfd_vma value_sbits = value & sbits;
2664 
2665 	  if (value_sbits != 0
2666 	      && value_sbits != sbit
2667 	      && value_sbits != sbits)
2668 	    r = bfd_reloc_overflow;
2669 	}
2670 
2671       if (r != bfd_reloc_ok)
2672 	{
2673 	  const char *name;
2674 	  const char *error_message;
2675 
2676 	  if (h != NULL)
2677 	    name = h->root.root.string;
2678 	  else
2679 	    {
2680 	      name = bfd_elf_string_from_elf_section (input_bfd,
2681 						      symtab_hdr->sh_link,
2682 						      sym->st_name);
2683 	      if (name == NULL)
2684 		return FALSE;
2685 	      if (*name == '\0')
2686 		name = bfd_section_name (sec);
2687 	    }
2688 
2689 	  switch (r)
2690 	    {
2691 	    case bfd_reloc_overflow:
2692 	      /* If the overflowing reloc was to an undefined symbol,
2693 		 we have already printed one error message and there
2694 		 is no point complaining again.  */
2695 	      if (!h || h->root.type != bfd_link_hash_undefined)
2696 		(*info->callbacks->reloc_overflow)
2697 		  (info, (h ? &h->root : NULL), name, howto->name,
2698 		   (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2699 	      break;
2700 
2701 	    case bfd_reloc_undefined:
2702 	      (*info->callbacks->undefined_symbol) (info, name, input_bfd,
2703 						    input_section,
2704 						    rel->r_offset, TRUE);
2705 	      break;
2706 
2707 	    case bfd_reloc_outofrange:
2708 	      error_message = _("out of range");
2709 	      goto common_error;
2710 
2711 	    case bfd_reloc_notsupported:
2712 	      error_message = _("unsupported relocation");
2713 	      goto common_error;
2714 
2715 	    case bfd_reloc_dangerous:
2716 	      error_message = _("dangerous relocation");
2717 	      goto common_error;
2718 
2719 	    default:
2720 	      error_message = _("unknown error");
2721 	      /* Fall through.  */
2722 
2723 	    common_error:
2724 	      BFD_ASSERT (error_message != NULL);
2725 	      (*info->callbacks->reloc_dangerous)
2726 		(info, error_message, input_bfd, input_section, rel->r_offset);
2727 	      break;
2728 	    }
2729 	}
2730     }
2731 
2732   return ok;
2733 }
2734 
2735 
2736 /* Look through the relocs for a section during the first phase, and
2737    calculate needed space in the global offset table, procedure linkage
2738    table, and dynamic reloc sections.  */
2739 
2740 static bfd_boolean
elf32_tic6x_check_relocs(bfd * abfd,struct bfd_link_info * info,asection * sec,const Elf_Internal_Rela * relocs)2741 elf32_tic6x_check_relocs (bfd *abfd, struct bfd_link_info *info,
2742 			  asection *sec, const Elf_Internal_Rela *relocs)
2743 {
2744   struct elf32_tic6x_link_hash_table *htab;
2745   Elf_Internal_Shdr *symtab_hdr;
2746   struct elf_link_hash_entry **sym_hashes;
2747   const Elf_Internal_Rela *rel;
2748   const Elf_Internal_Rela *rel_end;
2749   asection *sreloc;
2750 
2751   if (bfd_link_relocatable (info))
2752     return TRUE;
2753 
2754   htab = elf32_tic6x_hash_table (info);
2755   symtab_hdr = &elf_symtab_hdr (abfd);
2756   sym_hashes = elf_sym_hashes (abfd);
2757 
2758   /* Create dynamic sections for relocatable executables so that we can
2759      copy relocations.  */
2760   if ((bfd_link_pic (info) || elf32_tic6x_using_dsbt (abfd))
2761       && ! htab->elf.dynamic_sections_created)
2762     {
2763       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
2764 	return FALSE;
2765     }
2766 
2767   sreloc = NULL;
2768 
2769   rel_end = relocs + sec->reloc_count;
2770   for (rel = relocs; rel < rel_end; rel++)
2771     {
2772       unsigned int r_type;
2773       unsigned int r_symndx;
2774       struct elf_link_hash_entry *h;
2775       Elf_Internal_Sym *isym;
2776 
2777       r_symndx = ELF32_R_SYM (rel->r_info);
2778       r_type = ELF32_R_TYPE (rel->r_info);
2779 
2780       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2781 	{
2782 	  /* xgettext:c-format */
2783 	  _bfd_error_handler (_("%pB: bad symbol index: %d"),
2784 			      abfd, r_symndx);
2785 	  return FALSE;
2786 	}
2787 
2788       if (r_symndx < symtab_hdr->sh_info)
2789 	{
2790 	  /* A local symbol.  */
2791 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2792 					abfd, r_symndx);
2793 	  if (isym == NULL)
2794 	    return FALSE;
2795 	  h = NULL;
2796 	}
2797       else
2798 	{
2799 	  isym = NULL;
2800 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2801 	  while (h->root.type == bfd_link_hash_indirect
2802 		 || h->root.type == bfd_link_hash_warning)
2803 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2804 	}
2805 
2806       switch (r_type)
2807 	{
2808 	case R_C6000_PCR_S21:
2809 	case R_C6000_PREL31:
2810 	  /* This symbol requires a procedure linkage table entry.  We
2811 	     actually build the entry in adjust_dynamic_symbol,
2812 	     because this might be a case of linking PIC code which is
2813 	     never referenced by a dynamic object, in which case we
2814 	     don't need to generate a procedure linkage table entry
2815 	     after all.  */
2816 
2817 	  /* If this is a local symbol, we resolve it directly without
2818 	     creating a procedure linkage table entry.  */
2819 	  if (h == NULL)
2820 	    continue;
2821 
2822 	  h->needs_plt = 1;
2823 	  h->plt.refcount += 1;
2824 	  break;
2825 
2826 	case R_C6000_SBR_GOT_U15_W:
2827 	case R_C6000_SBR_GOT_L16_W:
2828 	case R_C6000_SBR_GOT_H16_W:
2829 	case R_C6000_EHTYPE:
2830 	  /* This symbol requires a global offset table entry.  */
2831 	  if (h != NULL)
2832 	    {
2833 	      h->got.refcount += 1;
2834 	    }
2835 	  else
2836 	    {
2837 	      bfd_signed_vma *local_got_refcounts;
2838 
2839 	      /* This is a global offset table entry for a local symbol.  */
2840 	      local_got_refcounts = elf_local_got_refcounts (abfd);
2841 	      if (local_got_refcounts == NULL)
2842 		{
2843 		  bfd_size_type size;
2844 
2845 		  size = symtab_hdr->sh_info;
2846 		  size *= (sizeof (bfd_signed_vma)
2847 			   + sizeof (bfd_vma) + sizeof(char));
2848 		  local_got_refcounts = bfd_zalloc (abfd, size);
2849 		  if (local_got_refcounts == NULL)
2850 		    return FALSE;
2851 		  elf_local_got_refcounts (abfd) = local_got_refcounts;
2852 		}
2853 	      local_got_refcounts[r_symndx] += 1;
2854 	    }
2855 
2856 	  if (htab->elf.sgot == NULL)
2857 	    {
2858 	      if (htab->elf.dynobj == NULL)
2859 		htab->elf.dynobj = abfd;
2860 	      if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2861 		return FALSE;
2862 	    }
2863 	  break;
2864 
2865 	case R_C6000_DSBT_INDEX:
2866 	  /* We'd like to check for nonzero dsbt_index here, but it's
2867 	     set up only after check_relocs is called.  Instead, we
2868 	     store the number of R_C6000_DSBT_INDEX relocs in the
2869 	     pc_count field, and potentially discard the extra space
2870 	     in elf32_tic6x_allocate_dynrelocs.  */
2871 	  if (!bfd_link_pic (info))
2872 	    break;
2873 
2874 	  /* fall through */
2875 	case R_C6000_ABS32:
2876 	case R_C6000_ABS16:
2877 	case R_C6000_ABS8:
2878 	case R_C6000_ABS_S16:
2879 	case R_C6000_ABS_L16:
2880 	case R_C6000_ABS_H16:
2881 	  /* If we are creating a shared library, and this is a reloc
2882 	     against a global symbol, or a non PC relative reloc
2883 	     against a local symbol, then we need to copy the reloc
2884 	     into the shared library.  However, if we are linking with
2885 	     -Bsymbolic, we do not need to copy a reloc against a
2886 	     global symbol which is defined in an object we are
2887 	     including in the link (i.e., DEF_REGULAR is set).  At
2888 	     this point we have not seen all the input files, so it is
2889 	     possible that DEF_REGULAR is not set now but will be set
2890 	     later (it is never cleared).  In case of a weak definition,
2891 	     DEF_REGULAR may be cleared later by a strong definition in
2892 	     a shared library.  We account for that possibility below by
2893 	     storing information in the relocs_copied field of the hash
2894 	     table entry.  A similar situation occurs when creating
2895 	     shared libraries and symbol visibility changes render the
2896 	     symbol local.
2897 
2898 	     If on the other hand, we are creating an executable, we
2899 	     may need to keep relocations for symbols satisfied by a
2900 	     dynamic library if we manage to avoid copy relocs for the
2901 	     symbol.  */
2902 	  if ((bfd_link_pic (info) || elf32_tic6x_using_dsbt (abfd))
2903 	      && (sec->flags & SEC_ALLOC) != 0)
2904 	    {
2905 	      struct elf_dyn_relocs *p;
2906 	      struct elf_dyn_relocs **head;
2907 
2908 	      /* We must copy these reloc types into the output file.
2909 		 Create a reloc section in dynobj and make room for
2910 		 this reloc.  */
2911 	      if (sreloc == NULL)
2912 		{
2913 		  if (htab->elf.dynobj == NULL)
2914 		    htab->elf.dynobj = abfd;
2915 
2916 		  sreloc = _bfd_elf_make_dynamic_reloc_section
2917 		    (sec, htab->elf.dynobj, 2, abfd, /*rela? */ TRUE);
2918 
2919 		  if (sreloc == NULL)
2920 		    return FALSE;
2921 		}
2922 
2923 	      /* If this is a global symbol, we count the number of
2924 		 relocations we need for this symbol.  */
2925 	      if (h != NULL)
2926 		{
2927 		  head = &((struct elf32_tic6x_link_hash_entry *) h)->dyn_relocs;
2928 		}
2929 	      else
2930 		{
2931 		  /* Track dynamic relocs needed for local syms too.
2932 		     We really need local syms available to do this
2933 		     easily.  Oh well.  */
2934 		  void **vpp;
2935 		  asection *s;
2936 
2937 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2938 		  if (s == NULL)
2939 		    s = sec;
2940 
2941 		  vpp = &elf_section_data (s)->local_dynrel;
2942 		  head = (struct elf_dyn_relocs **)vpp;
2943 		}
2944 
2945 	      p = *head;
2946 	      if (p == NULL || p->sec != sec)
2947 		{
2948 		  bfd_size_type amt = sizeof *p;
2949 		  p = bfd_alloc (htab->elf.dynobj, amt);
2950 		  if (p == NULL)
2951 		    return FALSE;
2952 		  p->next = *head;
2953 		  *head = p;
2954 		  p->sec = sec;
2955 		  p->count = 0;
2956 		  p->pc_count = 0;
2957 		}
2958 
2959 	      p->count += 1;
2960 	      if (r_type == R_C6000_DSBT_INDEX)
2961 		p->pc_count += 1;
2962 	    }
2963 	  break;
2964 
2965 	case R_C6000_SBR_U15_B:
2966 	case R_C6000_SBR_U15_H:
2967 	case R_C6000_SBR_U15_W:
2968 	case R_C6000_SBR_S16:
2969 	case R_C6000_SBR_L16_B:
2970 	case R_C6000_SBR_L16_H:
2971 	case R_C6000_SBR_L16_W:
2972 	case R_C6000_SBR_H16_B:
2973 	case R_C6000_SBR_H16_H:
2974 	case R_C6000_SBR_H16_W:
2975 	  {
2976 	    /* These relocations implicitly reference __c6xabi_DSBT_BASE.
2977 	       Add an explicit reference so that the symbol will be
2978 	       provided by a linker script.  */
2979 	    struct bfd_link_hash_entry *bh = NULL;
2980 	    if (!_bfd_generic_link_add_one_symbol (info, abfd,
2981 						   "__c6xabi_DSBT_BASE",
2982 						   BSF_GLOBAL,
2983 						   bfd_und_section_ptr, 0,
2984 						   NULL, FALSE, FALSE, &bh))
2985 	      return FALSE;
2986 	    ((struct elf_link_hash_entry *) bh)->non_elf = 0;
2987 	  }
2988 	  if (h != NULL && bfd_link_executable (info))
2989 	    {
2990 	      /* For B14-relative addresses, we might need a copy
2991 		 reloc.  */
2992 	      h->non_got_ref = 1;
2993 	    }
2994 	  break;
2995 
2996 	default:
2997 	  break;
2998 	}
2999     }
3000 
3001   return TRUE;
3002 }
3003 
3004 static bfd_boolean
elf32_tic6x_add_symbol_hook(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym,const char ** namep ATTRIBUTE_UNUSED,flagword * flagsp ATTRIBUTE_UNUSED,asection ** secp,bfd_vma * valp)3005 elf32_tic6x_add_symbol_hook (bfd *abfd,
3006 			     struct bfd_link_info *info ATTRIBUTE_UNUSED,
3007 			     Elf_Internal_Sym *sym,
3008 			     const char **namep ATTRIBUTE_UNUSED,
3009 			     flagword *flagsp ATTRIBUTE_UNUSED,
3010 			     asection **secp,
3011 			     bfd_vma *valp)
3012 {
3013   switch (sym->st_shndx)
3014     {
3015     case SHN_TIC6X_SCOMMON:
3016       *secp = bfd_make_section_old_way (abfd, ".scommon");
3017       (*secp)->flags |= SEC_IS_COMMON;
3018       *valp = sym->st_size;
3019       bfd_set_section_alignment (*secp, bfd_log2 (sym->st_value));
3020       break;
3021     }
3022 
3023   return TRUE;
3024 }
3025 
3026 static void
elf32_tic6x_symbol_processing(bfd * abfd ATTRIBUTE_UNUSED,asymbol * asym)3027 elf32_tic6x_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
3028 {
3029   elf_symbol_type *elfsym;
3030 
3031   elfsym = (elf_symbol_type *) asym;
3032   switch (elfsym->internal_elf_sym.st_shndx)
3033     {
3034     case SHN_TIC6X_SCOMMON:
3035       if (tic6x_elf_scom_section.name == NULL)
3036 	{
3037 	  /* Initialize the small common section.  */
3038 	  tic6x_elf_scom_section.name = ".scommon";
3039 	  tic6x_elf_scom_section.flags = SEC_IS_COMMON;
3040 	  tic6x_elf_scom_section.output_section = &tic6x_elf_scom_section;
3041 	  tic6x_elf_scom_section.symbol = &tic6x_elf_scom_symbol;
3042 	  tic6x_elf_scom_section.symbol_ptr_ptr = &tic6x_elf_scom_symbol_ptr;
3043 	  tic6x_elf_scom_symbol.name = ".scommon";
3044 	  tic6x_elf_scom_symbol.flags = BSF_SECTION_SYM;
3045 	  tic6x_elf_scom_symbol.section = &tic6x_elf_scom_section;
3046 	  tic6x_elf_scom_symbol_ptr = &tic6x_elf_scom_symbol;
3047 	}
3048       asym->section = &tic6x_elf_scom_section;
3049       asym->value = elfsym->internal_elf_sym.st_size;
3050       break;
3051     }
3052 }
3053 
3054 static int
elf32_tic6x_link_output_symbol_hook(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym,asection * input_sec,struct elf_link_hash_entry * h ATTRIBUTE_UNUSED)3055 elf32_tic6x_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3056 				     const char *name ATTRIBUTE_UNUSED,
3057 				     Elf_Internal_Sym *sym,
3058 				     asection *input_sec,
3059 				     struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
3060 {
3061   /* If we see a common symbol, which implies a relocatable link, then
3062      if a symbol was small common in an input file, mark it as small
3063      common in the output file.  */
3064   if (sym->st_shndx == SHN_COMMON && strcmp (input_sec->name, ".scommon") == 0)
3065     sym->st_shndx = SHN_TIC6X_SCOMMON;
3066 
3067   return 1;
3068 }
3069 
3070 static bfd_boolean
elf32_tic6x_section_from_bfd_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,int * retval)3071 elf32_tic6x_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3072 				      asection *sec,
3073 				      int *retval)
3074 {
3075   if (strcmp (bfd_section_name (sec), ".scommon") == 0)
3076     {
3077       *retval = SHN_TIC6X_SCOMMON;
3078       return TRUE;
3079     }
3080 
3081   return FALSE;
3082 }
3083 
3084 /* Allocate space in .plt, .got and associated reloc sections for
3085    dynamic relocs.  */
3086 
3087 static bfd_boolean
elf32_tic6x_allocate_dynrelocs(struct elf_link_hash_entry * h,void * inf)3088 elf32_tic6x_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3089 {
3090   struct bfd_link_info *info;
3091   struct elf32_tic6x_link_hash_table *htab;
3092   struct elf32_tic6x_link_hash_entry *eh;
3093   struct elf_dyn_relocs *p;
3094 
3095   if (h->root.type == bfd_link_hash_indirect)
3096     return TRUE;
3097 
3098   eh = (struct elf32_tic6x_link_hash_entry *) h;
3099   info = (struct bfd_link_info *) inf;
3100   htab = elf32_tic6x_hash_table (info);
3101 
3102   if (htab->elf.dynamic_sections_created && h->plt.refcount > 0)
3103     {
3104       /* Make sure this symbol is output as a dynamic symbol.
3105 	 Undefined weak syms won't yet be marked as dynamic.  */
3106       if (h->dynindx == -1 && !h->forced_local)
3107 	{
3108 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
3109 	    return FALSE;
3110 	}
3111 
3112       if (bfd_link_pic (info)
3113 	  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3114 	{
3115 	  asection *s = htab->elf.splt;
3116 
3117 	  /* If this is the first .plt entry, make room for the special
3118 	     first entry.  */
3119 	  if (s->size == 0)
3120 	    s->size += PLT_ENTRY_SIZE;
3121 
3122 	  h->plt.offset = s->size;
3123 
3124 	  /* If this symbol is not defined in a regular file, and we are
3125 	     not generating a shared library, then set the symbol to this
3126 	     location in the .plt.  This is required to make function
3127 	     pointers compare as equal between the normal executable and
3128 	     the shared library.  */
3129 	  if (! bfd_link_pic (info) && !h->def_regular)
3130 	    {
3131 	      h->root.u.def.section = s;
3132 	      h->root.u.def.value = h->plt.offset;
3133 	    }
3134 
3135 	  /* Make room for this entry.  */
3136 	  s->size += PLT_ENTRY_SIZE;
3137 	  /* We also need to make an entry in the .got.plt section, which
3138 	     will be placed in the .got section by the linker script.  */
3139 	  htab->elf.sgotplt->size += 4;
3140 	  /* We also need to make an entry in the .rel.plt section.  */
3141 	  htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
3142 	}
3143       else
3144 	{
3145 	  h->plt.offset = (bfd_vma) -1;
3146 	  h->needs_plt = 0;
3147 	}
3148     }
3149   else
3150     {
3151       h->plt.offset = (bfd_vma) -1;
3152       h->needs_plt = 0;
3153     }
3154 
3155   if (h->got.refcount > 0)
3156     {
3157       asection *s;
3158 
3159       /* Make sure this symbol is output as a dynamic symbol.
3160 	 Undefined weak syms won't yet be marked as dynamic.  */
3161       if (h->dynindx == -1
3162 	  && !h->forced_local)
3163 	{
3164 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
3165 	    return FALSE;
3166 	}
3167 
3168       s = htab->elf.sgot;
3169       h->got.offset = s->size;
3170       s->size += 4;
3171 
3172       if (!(ELF_ST_VISIBILITY (h->other)
3173 	    && h->root.type == bfd_link_hash_undefweak))
3174 	htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
3175     }
3176   else
3177     h->got.offset = (bfd_vma) -1;
3178 
3179   if (eh->dyn_relocs == NULL)
3180     return TRUE;
3181 
3182   /* Discard relocs on undefined weak syms with non-default
3183      visibility.  */
3184   if (bfd_link_pic (info) || elf32_tic6x_using_dsbt (htab->obfd))
3185     {
3186       /* We use the pc_count field to hold the number of
3187 	 R_C6000_DSBT_INDEX relocs.  */
3188       if (htab->params.dsbt_index != 0)
3189 	{
3190 	  struct elf_dyn_relocs **pp;
3191 
3192 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3193 	    {
3194 	      p->count -= p->pc_count;
3195 	      p->pc_count = 0;
3196 	      if (p->count == 0)
3197 		*pp = p->next;
3198 	      else
3199 		pp = &p->next;
3200 	    }
3201 	}
3202 
3203       if (eh->dyn_relocs != NULL
3204 	  && h->root.type == bfd_link_hash_undefweak)
3205 	{
3206 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3207 	    eh->dyn_relocs = NULL;
3208 
3209 	  /* Make sure undefined weak symbols are output as a dynamic
3210 	     symbol in PIEs.  */
3211 	  else if (h->dynindx == -1
3212 		   && !h->forced_local)
3213 	    {
3214 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
3215 		return FALSE;
3216 	    }
3217 	}
3218     }
3219 
3220   /* Finally, allocate space.  */
3221   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3222     {
3223       asection *sreloc;
3224 
3225       sreloc = elf_section_data (p->sec)->sreloc;
3226 
3227       BFD_ASSERT (sreloc != NULL);
3228       sreloc->size += p->count * sizeof (Elf32_External_Rela);
3229     }
3230 
3231   return TRUE;
3232 }
3233 
3234 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
3235    read-only sections.  */
3236 
3237 static bfd_boolean
maybe_set_textrel(struct elf_link_hash_entry * h,void * info_p)3238 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
3239 {
3240   asection *sec;
3241 
3242   if (h->root.type == bfd_link_hash_indirect)
3243     return TRUE;
3244 
3245   sec = readonly_dynrelocs (h);
3246   if (sec != NULL)
3247     {
3248       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
3249 
3250       info->flags |= DF_TEXTREL;
3251       info->callbacks->minfo
3252 	(_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
3253 	 sec->owner, h->root.root.string, sec);
3254 
3255       /* Not an error, just cut short the traversal.  */
3256       return FALSE;
3257     }
3258   return TRUE;
3259 }
3260 
3261 /* Set the sizes of the dynamic sections.  */
3262 
3263 static bfd_boolean
elf32_tic6x_size_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)3264 elf32_tic6x_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3265 {
3266   struct elf32_tic6x_link_hash_table *htab;
3267   bfd *dynobj;
3268   asection *s;
3269   bfd_boolean relocs;
3270   bfd *ibfd;
3271 
3272   htab = elf32_tic6x_hash_table (info);
3273   dynobj = htab->elf.dynobj;
3274   if (dynobj == NULL)
3275     abort ();
3276 
3277   if (htab->elf.dynamic_sections_created)
3278     {
3279       /* Set the contents of the .interp section to the interpreter.  */
3280       if (bfd_link_executable (info) && !info->nointerp)
3281 	{
3282 	  s = bfd_get_linker_section (dynobj, ".interp");
3283 	  if (s == NULL)
3284 	    abort ();
3285 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3286 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3287 	}
3288     }
3289 
3290   /* Set up .got offsets for local syms, and space for local dynamic
3291      relocs.  */
3292   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3293     {
3294       bfd_signed_vma *local_got;
3295       bfd_signed_vma *end_local_got;
3296       bfd_size_type locsymcount;
3297       Elf_Internal_Shdr *symtab_hdr;
3298       asection *srel;
3299 
3300       for (s = ibfd->sections; s != NULL; s = s->next)
3301 	{
3302 	  struct elf_dyn_relocs *p;
3303 
3304 	  for (p = ((struct elf_dyn_relocs *)
3305 		     elf_section_data (s)->local_dynrel);
3306 	       p != NULL;
3307 	       p = p->next)
3308 	    {
3309 	      if (!bfd_is_abs_section (p->sec)
3310 		  && bfd_is_abs_section (p->sec->output_section))
3311 		{
3312 		  /* Input section has been discarded, either because
3313 		     it is a copy of a linkonce section or due to
3314 		     linker script /DISCARD/, so we'll be discarding
3315 		     the relocs too.  */
3316 		}
3317 	      else if (p->count != 0)
3318 		{
3319 		  srel = elf_section_data (p->sec)->sreloc;
3320 		  srel->size += p->count * sizeof (Elf32_External_Rela);
3321 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3322 		    info->flags |= DF_TEXTREL;
3323 		}
3324 	    }
3325 	}
3326 
3327       local_got = elf_local_got_refcounts (ibfd);
3328       if (!local_got)
3329 	continue;
3330 
3331       symtab_hdr = &elf_symtab_hdr (ibfd);
3332       locsymcount = symtab_hdr->sh_info;
3333       end_local_got = local_got + locsymcount;
3334       s = htab->elf.sgot;
3335       srel = htab->elf.srelgot;
3336       for (; local_got < end_local_got; ++local_got)
3337 	{
3338 	  if (*local_got > 0)
3339 	    {
3340 	      *local_got = s->size;
3341 	      s->size += 4;
3342 
3343 	      if (bfd_link_pic (info) || elf32_tic6x_using_dsbt (output_bfd))
3344 		{
3345 		  srel->size += sizeof (Elf32_External_Rela);
3346 		}
3347 	    }
3348 	  else
3349 	    *local_got = (bfd_vma) -1;
3350 	}
3351     }
3352 
3353   /* Allocate global sym .plt and .got entries, and space for global
3354      sym dynamic relocs.  */
3355   elf_link_hash_traverse (&htab->elf, elf32_tic6x_allocate_dynrelocs, info);
3356 
3357   /* We now have determined the sizes of the various dynamic sections.
3358      Allocate memory for them.  */
3359   relocs = FALSE;
3360   for (s = dynobj->sections; s != NULL; s = s->next)
3361     {
3362       bfd_boolean strip_section = TRUE;
3363 
3364       if ((s->flags & SEC_LINKER_CREATED) == 0)
3365 	continue;
3366 
3367       if (s == htab->dsbt)
3368 	s->size = 4 * htab->params.dsbt_size;
3369       else if (s == htab->elf.splt
3370 	       || s == htab->elf.sgot
3371 	       || s == htab->elf.sgotplt
3372 	       || s == htab->elf.sdynbss
3373 	       || s == htab->elf.sdynrelro)
3374 	{
3375 	  /* Strip this section if we don't need it; see the
3376 	     comment below.  */
3377 	  /* We'd like to strip these sections if they aren't needed, but if
3378 	     we've exported dynamic symbols from them we must leave them.
3379 	     It's too late to tell BFD to get rid of the symbols.  */
3380 
3381 	  if (htab->elf.hplt != NULL)
3382 	    strip_section = FALSE;
3383 
3384 	  /* Round up the size of the PLT section to a multiple of 32.  */
3385 	  if (s == htab->elf.splt && s->size > 0)
3386 	    s->size = (s->size + 31) & ~(bfd_vma)31;
3387 	}
3388       else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
3389 	{
3390 	  if (s->size != 0
3391 	      && s != htab->elf.srelplt)
3392 	    relocs = TRUE;
3393 
3394 	  /* We use the reloc_count field as a counter if we need
3395 	     to copy relocs into the output file.  */
3396 	  s->reloc_count = 0;
3397 	}
3398       else
3399 	{
3400 	  /* It's not one of our sections, so don't allocate space.  */
3401 	  continue;
3402 	}
3403 
3404       if (s->size == 0)
3405 	{
3406 	  /* If we don't need this section, strip it from the
3407 	     output file.  This is mostly to handle .rel.bss and
3408 	     .rel.plt.  We must create both sections in
3409 	     create_dynamic_sections, because they must be created
3410 	     before the linker maps input sections to output
3411 	     sections.  The linker does that before
3412 	     adjust_dynamic_symbol is called, and it is that
3413 	     function which decides whether anything needs to go
3414 	     into these sections.  */
3415 	  if (strip_section)
3416 	    s->flags |= SEC_EXCLUDE;
3417 	  continue;
3418 	}
3419 
3420       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3421 	continue;
3422 
3423       /* Allocate memory for the section contents.  We use bfd_zalloc
3424 	 here in case unused entries are not reclaimed before the
3425 	 section's contents are written out.  This should not happen,
3426 	 but this way if it does, we get a R_C6000_NONE reloc instead
3427 	 of garbage.  */
3428       s->contents = bfd_zalloc (dynobj, s->size);
3429       if (s->contents == NULL)
3430 	return FALSE;
3431     }
3432 
3433   if (htab->elf.dynamic_sections_created)
3434     {
3435       /* Add some entries to the .dynamic section.  We fill in the
3436 	 values later, in elf32_tic6x_finish_dynamic_sections, but we
3437 	 must add the entries now so that we get the correct size for
3438 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
3439 	 dynamic linker and used by the debugger.  */
3440 #define add_dynamic_entry(TAG, VAL) \
3441   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3442 
3443       if (bfd_link_executable (info))
3444 	{
3445 	  if (!add_dynamic_entry (DT_DEBUG, 0))
3446 	    return FALSE;
3447 	}
3448 
3449       if (!add_dynamic_entry (DT_C6000_DSBT_BASE, 0)
3450 	  || !add_dynamic_entry (DT_C6000_DSBT_SIZE, htab->params.dsbt_size)
3451 	  || !add_dynamic_entry (DT_C6000_DSBT_INDEX,
3452 				 htab->params.dsbt_index))
3453 	return FALSE;
3454 
3455       if (htab->elf.splt->size != 0)
3456 	{
3457 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
3458 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
3459 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3460 	      || !add_dynamic_entry (DT_JMPREL, 0))
3461 	    return FALSE;
3462 	}
3463 
3464       if (relocs)
3465 	{
3466 	  if (!add_dynamic_entry (DT_RELA, 0)
3467 	      || !add_dynamic_entry (DT_RELASZ, 0)
3468 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3469 	    return FALSE;
3470 
3471 	  /* If any dynamic relocs apply to a read-only section,
3472 	     then we need a DT_TEXTREL entry.  */
3473 	  if ((info->flags & DF_TEXTREL) == 0)
3474 	    elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
3475 
3476 	  if ((info->flags & DF_TEXTREL) != 0)
3477 	    {
3478 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
3479 		return FALSE;
3480 	    }
3481 	}
3482     }
3483 #undef add_dynamic_entry
3484 
3485   return TRUE;
3486 }
3487 
3488 /* This function is called after all the input files have been read,
3489    and the input sections have been assigned to output sections.  */
3490 
3491 static bfd_boolean
elf32_tic6x_always_size_sections(bfd * output_bfd,struct bfd_link_info * info)3492 elf32_tic6x_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3493 {
3494   if (elf32_tic6x_using_dsbt (output_bfd) && !bfd_link_relocatable (info)
3495       && !bfd_elf_stack_segment_size (output_bfd, info,
3496 				      "__stacksize", DEFAULT_STACK_SIZE))
3497     return FALSE;
3498 
3499   return TRUE;
3500 }
3501 
3502 static bfd_boolean
elf32_tic6x_finish_dynamic_sections(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)3503 elf32_tic6x_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3504 				     struct bfd_link_info *info)
3505 {
3506   struct elf32_tic6x_link_hash_table *htab;
3507   bfd *dynobj;
3508   asection *sdyn;
3509 
3510   htab = elf32_tic6x_hash_table (info);
3511   dynobj = htab->elf.dynobj;
3512   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3513 
3514   if (elf_hash_table (info)->dynamic_sections_created)
3515     {
3516       Elf32_External_Dyn * dyncon;
3517       Elf32_External_Dyn * dynconend;
3518 
3519       BFD_ASSERT (sdyn != NULL);
3520 
3521       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3522       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3523 
3524       for (; dyncon < dynconend; dyncon++)
3525 	{
3526 	  Elf_Internal_Dyn dyn;
3527 	  asection *s;
3528 
3529 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3530 
3531 	  switch (dyn.d_tag)
3532 	    {
3533 	    default:
3534 	      break;
3535 
3536 	    case DT_C6000_DSBT_BASE:
3537 	      s = htab->dsbt;
3538 	      dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset);
3539 	      break;
3540 
3541 	    case DT_PLTGOT:
3542 	      s = htab->elf.sgotplt;
3543 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3544 	      break;
3545 
3546 	    case DT_JMPREL:
3547 	      s = htab->elf.srelplt;
3548 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3549 	      break;
3550 
3551 	    case DT_PLTRELSZ:
3552 	      s = htab->elf.srelplt;
3553 	      dyn.d_un.d_val = s->size;
3554 	      break;
3555 	    }
3556 	  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3557 	}
3558 
3559       /* Fill in the first entry in the procedure linkage table.  */
3560       if (htab->elf.splt && htab->elf.splt->size > 0)
3561 	{
3562 	  bfd_vma got_offs = (htab->elf.sgotplt->output_section->vma
3563 			      + htab->elf.sgotplt->output_offset
3564 			      - htab->dsbt->output_section->vma
3565 			      - htab->dsbt->output_offset) / 4;
3566 
3567 	  /* ldw .D2T2 *+b14[$GOT(0)],b2 */
3568 	  bfd_put_32 (output_bfd, got_offs << 8 | 0x0100006e,
3569 		      htab->elf.splt->contents);
3570 	  /* ldw .D2T2 *+b14[$GOT(4)],b1 */
3571 	  bfd_put_32 (output_bfd, (got_offs + 1) << 8 | 0x0080006e,
3572 		      htab->elf.splt->contents + 4);
3573 	  /* nop 3 */
3574 	  bfd_put_32 (output_bfd, 0x00004000,
3575 		      htab->elf.splt->contents + 8);
3576 	  /* b .s2 b2 */
3577 	  bfd_put_32 (output_bfd, 0x00080362,
3578 		      htab->elf.splt->contents + 12);
3579 	  /* nop 5 */
3580 	  bfd_put_32 (output_bfd, 0x00008000,
3581 		      htab->elf.splt->contents + 16);
3582 
3583 	  elf_section_data (htab->elf.splt->output_section)
3584 	    ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
3585 	}
3586     }
3587 
3588   return TRUE;
3589 }
3590 
3591 /* Return address for Ith PLT stub in section PLT, for relocation REL
3592    or (bfd_vma) -1 if it should not be included.  */
3593 
3594 static bfd_vma
elf32_tic6x_plt_sym_val(bfd_vma i,const asection * plt,const arelent * rel ATTRIBUTE_UNUSED)3595 elf32_tic6x_plt_sym_val (bfd_vma i, const asection *plt,
3596 			 const arelent *rel ATTRIBUTE_UNUSED)
3597 {
3598   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3599 }
3600 
3601 static int
elf32_tic6x_obj_attrs_arg_type(int tag)3602 elf32_tic6x_obj_attrs_arg_type (int tag)
3603 {
3604   if (tag == Tag_ABI_compatibility)
3605     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
3606   else if (tag & 1)
3607     return ATTR_TYPE_FLAG_STR_VAL;
3608   else
3609     return ATTR_TYPE_FLAG_INT_VAL;
3610 }
3611 
3612 static int
elf32_tic6x_obj_attrs_order(int num)3613 elf32_tic6x_obj_attrs_order (int num)
3614 {
3615   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
3616     return Tag_ABI_conformance;
3617   if ((num - 1) < Tag_ABI_conformance)
3618     return num - 1;
3619   return num;
3620 }
3621 
3622 static bfd_boolean
elf32_tic6x_obj_attrs_handle_unknown(bfd * abfd,int tag)3623 elf32_tic6x_obj_attrs_handle_unknown (bfd *abfd, int tag)
3624 {
3625   if ((tag & 127) < 64)
3626     {
3627       _bfd_error_handler
3628 	/* xgettext:c-format */
3629 	(_("%pB: error: unknown mandatory EABI object attribute %d"),
3630 	 abfd, tag);
3631       bfd_set_error (bfd_error_bad_value);
3632       return FALSE;
3633     }
3634   else
3635     {
3636       _bfd_error_handler
3637 	/* xgettext:c-format */
3638 	(_("%pB: warning: unknown EABI object attribute %d"),
3639 	 abfd, tag);
3640       return TRUE;
3641     }
3642 }
3643 
3644 /* Merge the Tag_ISA attribute values ARCH1 and ARCH2
3645    and return the merged value.  At present, all merges succeed, so no
3646    return value for errors is defined.  */
3647 
3648 int
elf32_tic6x_merge_arch_attributes(int arch1,int arch2)3649 elf32_tic6x_merge_arch_attributes (int arch1, int arch2)
3650 {
3651   int min_arch, max_arch;
3652 
3653   min_arch = (arch1 < arch2 ? arch1 : arch2);
3654   max_arch = (arch1 > arch2 ? arch1 : arch2);
3655 
3656   /* In most cases, the numerically greatest value is the correct
3657      merged value, but merging C64 and C67 results in C674X.  */
3658   if ((min_arch == C6XABI_Tag_ISA_C67X
3659        || min_arch == C6XABI_Tag_ISA_C67XP)
3660       && (max_arch == C6XABI_Tag_ISA_C64X
3661 	  || max_arch == C6XABI_Tag_ISA_C64XP))
3662     return C6XABI_Tag_ISA_C674X;
3663 
3664   return max_arch;
3665 }
3666 
3667 /* Convert a Tag_ABI_array_object_alignment or
3668    Tag_ABI_array_object_align_expected tag value TAG to a
3669    corresponding alignment value; return the alignment, or -1 for an
3670    unknown tag value.  */
3671 
3672 static int
elf32_tic6x_tag_to_array_alignment(int tag)3673 elf32_tic6x_tag_to_array_alignment (int tag)
3674 {
3675   switch (tag)
3676     {
3677     case 0:
3678       return 8;
3679 
3680     case 1:
3681       return 4;
3682 
3683     case 2:
3684       return 16;
3685 
3686     default:
3687       return -1;
3688     }
3689 }
3690 
3691 /* Convert a Tag_ABI_array_object_alignment or
3692    Tag_ABI_array_object_align_expected alignment ALIGN to a
3693    corresponding tag value; return the tag value.  */
3694 
3695 static int
elf32_tic6x_array_alignment_to_tag(int align)3696 elf32_tic6x_array_alignment_to_tag (int align)
3697 {
3698   switch (align)
3699     {
3700     case 8:
3701       return 0;
3702 
3703     case 4:
3704       return 1;
3705 
3706     case 16:
3707       return 2;
3708 
3709     default:
3710       abort ();
3711     }
3712 }
3713 
3714 /* Merge attributes from IBFD and OBFD, returning TRUE if the merge
3715    succeeded, FALSE otherwise.  */
3716 
3717 static bfd_boolean
elf32_tic6x_merge_attributes(bfd * ibfd,struct bfd_link_info * info)3718 elf32_tic6x_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
3719 {
3720   bfd *obfd = info->output_bfd;
3721   bfd_boolean result = TRUE;
3722   obj_attribute *in_attr;
3723   obj_attribute *out_attr;
3724   int i;
3725   int array_align_in, array_align_out, array_expect_in, array_expect_out;
3726 
3727   if (!elf_known_obj_attributes_proc (obfd)[0].i)
3728     {
3729       /* This is the first object.  Copy the attributes.  */
3730       _bfd_elf_copy_obj_attributes (ibfd, obfd);
3731 
3732       out_attr = elf_known_obj_attributes_proc (obfd);
3733 
3734       /* Use the Tag_null value to indicate the attributes have been
3735 	 initialized.  */
3736       out_attr[0].i = 1;
3737 
3738       return TRUE;
3739     }
3740 
3741   in_attr = elf_known_obj_attributes_proc (ibfd);
3742   out_attr = elf_known_obj_attributes_proc (obfd);
3743 
3744   /* No specification yet for handling of unknown attributes, so just
3745      ignore them and handle known ones.  */
3746 
3747   if (out_attr[Tag_ABI_stack_align_preserved].i
3748       < in_attr[Tag_ABI_stack_align_needed].i)
3749     {
3750       _bfd_error_handler
3751 	/* xgettext:c-format */
3752 	(_("error: %pB requires more stack alignment than %pB preserves"),
3753 	 ibfd, obfd);
3754       result = FALSE;
3755     }
3756   if (in_attr[Tag_ABI_stack_align_preserved].i
3757       < out_attr[Tag_ABI_stack_align_needed].i)
3758     {
3759       _bfd_error_handler
3760 	/* xgettext:c-format */
3761 	(_("error: %pB requires more stack alignment than %pB preserves"),
3762 	 obfd, ibfd);
3763       result = FALSE;
3764     }
3765 
3766   array_align_in = elf32_tic6x_tag_to_array_alignment
3767     (in_attr[Tag_ABI_array_object_alignment].i);
3768   if (array_align_in == -1)
3769     {
3770       _bfd_error_handler
3771 	(_("error: unknown Tag_ABI_array_object_alignment value in %pB"),
3772 	 ibfd);
3773       result = FALSE;
3774     }
3775   array_align_out = elf32_tic6x_tag_to_array_alignment
3776     (out_attr[Tag_ABI_array_object_alignment].i);
3777   if (array_align_out == -1)
3778     {
3779       _bfd_error_handler
3780 	(_("error: unknown Tag_ABI_array_object_alignment value in %pB"),
3781 	 obfd);
3782       result = FALSE;
3783     }
3784   array_expect_in = elf32_tic6x_tag_to_array_alignment
3785     (in_attr[Tag_ABI_array_object_align_expected].i);
3786   if (array_expect_in == -1)
3787     {
3788       _bfd_error_handler
3789 	(_("error: unknown Tag_ABI_array_object_align_expected value in %pB"),
3790 	 ibfd);
3791       result = FALSE;
3792     }
3793   array_expect_out = elf32_tic6x_tag_to_array_alignment
3794     (out_attr[Tag_ABI_array_object_align_expected].i);
3795   if (array_expect_out == -1)
3796     {
3797       _bfd_error_handler
3798 	(_("error: unknown Tag_ABI_array_object_align_expected value in %pB"),
3799 	 obfd);
3800       result = FALSE;
3801     }
3802 
3803   if (array_align_out < array_expect_in)
3804     {
3805       _bfd_error_handler
3806 	/* xgettext:c-format */
3807 	(_("error: %pB requires more array alignment than %pB preserves"),
3808 	 ibfd, obfd);
3809       result = FALSE;
3810     }
3811   if (array_align_in < array_expect_out)
3812     {
3813       _bfd_error_handler
3814 	/* xgettext:c-format */
3815 	(_("error: %pB requires more array alignment than %pB preserves"),
3816 	 obfd, ibfd);
3817       result = FALSE;
3818     }
3819 
3820   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
3821     {
3822       switch (i)
3823 	{
3824 	case Tag_ISA:
3825 	  out_attr[i].i = elf32_tic6x_merge_arch_attributes (in_attr[i].i,
3826 							     out_attr[i].i);
3827 	  break;
3828 
3829 	case Tag_ABI_wchar_t:
3830 	  if (out_attr[i].i == 0)
3831 	    out_attr[i].i = in_attr[i].i;
3832 	  if (out_attr[i].i != 0
3833 	      && in_attr[i].i != 0
3834 	      && out_attr[i].i != in_attr[i].i)
3835 	    {
3836 	      _bfd_error_handler
3837 		/* xgettext:c-format */
3838 		(_("warning: %pB and %pB differ in wchar_t size"), obfd, ibfd);
3839 	    }
3840 	  break;
3841 
3842 	case Tag_ABI_stack_align_needed:
3843 	  if (out_attr[i].i < in_attr[i].i)
3844 	    out_attr[i].i = in_attr[i].i;
3845 	  break;
3846 
3847 	case Tag_ABI_stack_align_preserved:
3848 	  if (out_attr[i].i > in_attr[i].i)
3849 	    out_attr[i].i = in_attr[i].i;
3850 	  break;
3851 
3852 	case Tag_ABI_DSBT:
3853 	  if (out_attr[i].i != in_attr[i].i)
3854 	    {
3855 	      _bfd_error_handler
3856 		/* xgettext:c-format */
3857 		(_("warning: %pB and %pB differ in whether code is "
3858 		   "compiled for DSBT"),
3859 		 obfd, ibfd);
3860 	    }
3861 	  break;
3862 
3863 	case Tag_ABI_PIC:
3864 	case Tag_ABI_PID:
3865 	  if (out_attr[i].i > in_attr[i].i)
3866 	    out_attr[i].i = in_attr[i].i;
3867 	  break;
3868 
3869 	case Tag_ABI_array_object_alignment:
3870 	  if (array_align_out != -1
3871 	      && array_align_in != -1
3872 	      && array_align_out > array_align_in)
3873 	    out_attr[i].i
3874 	      = elf32_tic6x_array_alignment_to_tag (array_align_in);
3875 	  break;
3876 
3877 	case Tag_ABI_array_object_align_expected:
3878 	  if (array_expect_out != -1
3879 	      && array_expect_in != -1
3880 	      && array_expect_out < array_expect_in)
3881 	    out_attr[i].i
3882 	      = elf32_tic6x_array_alignment_to_tag (array_expect_in);
3883 	  break;
3884 
3885 	case Tag_ABI_conformance:
3886 	  /* Merging for this attribute is not specified.  As on ARM,
3887 	     treat a missing attribute as no claim to conform and only
3888 	     merge identical values.  */
3889 	  if (out_attr[i].s == NULL
3890 	      || in_attr[i].s == NULL
3891 	      || strcmp (out_attr[i].s,
3892 			 in_attr[i].s) != 0)
3893 	    out_attr[i].s = NULL;
3894 	  break;
3895 
3896 	case Tag_ABI_compatibility:
3897 	  /* Merged in _bfd_elf_merge_object_attributes.  */
3898 	  break;
3899 
3900 	default:
3901 	  result
3902 	    = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
3903 	  break;
3904 	}
3905 
3906       if (in_attr[i].type && !out_attr[i].type)
3907 	out_attr[i].type = in_attr[i].type;
3908     }
3909 
3910   /* Merge Tag_ABI_compatibility attributes and any common GNU ones.  */
3911   if (!_bfd_elf_merge_object_attributes (ibfd, info))
3912     return FALSE;
3913 
3914   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
3915 
3916   return result;
3917 }
3918 
3919 static bfd_boolean
elf32_tic6x_merge_private_bfd_data(bfd * ibfd,struct bfd_link_info * info)3920 elf32_tic6x_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3921 {
3922   if (!_bfd_generic_verify_endian_match (ibfd, info))
3923     return FALSE;
3924 
3925   if (! is_tic6x_elf (ibfd) || ! is_tic6x_elf (info->output_bfd))
3926     return TRUE;
3927 
3928   if (!elf32_tic6x_merge_attributes (ibfd, info))
3929     return FALSE;
3930 
3931   return TRUE;
3932 }
3933 
3934 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
3935    adds the edit to the start of the list.  (The list must be built in order of
3936    ascending TINDEX: the function's callers are primarily responsible for
3937    maintaining that condition).  */
3938 
3939 static void
elf32_tic6x_add_unwind_table_edit(tic6x_unwind_table_edit ** head,tic6x_unwind_table_edit ** tail,tic6x_unwind_edit_type type,asection * linked_section,unsigned int tindex)3940 elf32_tic6x_add_unwind_table_edit (tic6x_unwind_table_edit **head,
3941 				   tic6x_unwind_table_edit **tail,
3942 				   tic6x_unwind_edit_type type,
3943 				   asection *linked_section,
3944 				   unsigned int tindex)
3945 {
3946   tic6x_unwind_table_edit *new_edit = (tic6x_unwind_table_edit *)
3947       xmalloc (sizeof (tic6x_unwind_table_edit));
3948 
3949   new_edit->type = type;
3950   new_edit->linked_section = linked_section;
3951   new_edit->index = tindex;
3952 
3953   if (tindex > 0)
3954     {
3955       new_edit->next = NULL;
3956 
3957       if (*tail)
3958 	(*tail)->next = new_edit;
3959 
3960       (*tail) = new_edit;
3961 
3962       if (!*head)
3963 	(*head) = new_edit;
3964     }
3965   else
3966     {
3967       new_edit->next = *head;
3968 
3969       if (!*tail)
3970 	*tail = new_edit;
3971 
3972       *head = new_edit;
3973     }
3974 }
3975 
3976 static _tic6x_elf_section_data *
get_tic6x_elf_section_data(asection * sec)3977 get_tic6x_elf_section_data (asection * sec)
3978 {
3979   if (sec && sec->owner && is_tic6x_elf (sec->owner))
3980     return elf32_tic6x_section_data (sec);
3981   else
3982     return NULL;
3983 }
3984 
3985 
3986 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST must be negative.  */
3987 static void
elf32_tic6x_adjust_exidx_size(asection * exidx_sec,int adjust)3988 elf32_tic6x_adjust_exidx_size (asection *exidx_sec, int adjust)
3989 {
3990   asection *out_sec;
3991 
3992   if (!exidx_sec->rawsize)
3993     exidx_sec->rawsize = exidx_sec->size;
3994 
3995   bfd_set_section_size (exidx_sec, exidx_sec->size + adjust);
3996   out_sec = exidx_sec->output_section;
3997   /* Adjust size of output section.  */
3998   bfd_set_section_size (out_sec, out_sec->size +adjust);
3999 }
4000 
4001 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
4002 static void
elf32_tic6x_insert_cantunwind_after(asection * text_sec,asection * exidx_sec)4003 elf32_tic6x_insert_cantunwind_after (asection *text_sec, asection *exidx_sec)
4004 {
4005   struct _tic6x_elf_section_data *exidx_data;
4006 
4007   exidx_data = get_tic6x_elf_section_data (exidx_sec);
4008   elf32_tic6x_add_unwind_table_edit (
4009     &exidx_data->u.exidx.unwind_edit_list,
4010     &exidx_data->u.exidx.unwind_edit_tail,
4011     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
4012 
4013   elf32_tic6x_adjust_exidx_size (exidx_sec, 8);
4014 }
4015 
4016 /* Scan .cx6abi.exidx tables, and create a list describing edits which
4017    should be made to those tables, such that:
4018 
4019      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
4020      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
4021 	codes which have been inlined into the index).
4022 
4023    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
4024 
4025    The edits are applied when the tables are written
4026    (in elf32_tic6x_write_section).
4027 */
4028 
4029 bfd_boolean
elf32_tic6x_fix_exidx_coverage(asection ** text_section_order,unsigned int num_text_sections,struct bfd_link_info * info,bfd_boolean merge_exidx_entries)4030 elf32_tic6x_fix_exidx_coverage (asection **text_section_order,
4031 				unsigned int num_text_sections,
4032 				struct bfd_link_info *info,
4033 				bfd_boolean merge_exidx_entries)
4034 {
4035   bfd *inp;
4036   unsigned int last_second_word = 0, i;
4037   asection *last_exidx_sec = NULL;
4038   asection *last_text_sec = NULL;
4039   int last_unwind_type = -1;
4040 
4041   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
4042      text sections.  */
4043   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
4044     {
4045       asection *sec;
4046 
4047       for (sec = inp->sections; sec != NULL; sec = sec->next)
4048 	{
4049 	  struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
4050 	  Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
4051 
4052 	  if (!hdr || hdr->sh_type != SHT_C6000_UNWIND)
4053 	    continue;
4054 
4055 	  if (elf_sec->linked_to)
4056 	    {
4057 	      Elf_Internal_Shdr *linked_hdr
4058 		= &elf_section_data (elf_sec->linked_to)->this_hdr;
4059 	      struct _tic6x_elf_section_data *linked_sec_tic6x_data
4060 		= get_tic6x_elf_section_data (linked_hdr->bfd_section);
4061 
4062 	      if (linked_sec_tic6x_data == NULL)
4063 		continue;
4064 
4065 	      /* Link this .c6xabi.exidx section back from the
4066 		 text section it describes.  */
4067 	      linked_sec_tic6x_data->u.text.tic6x_exidx_sec = sec;
4068 	    }
4069 	}
4070     }
4071 
4072   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
4073      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
4074      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
4075 
4076   for (i = 0; i < num_text_sections; i++)
4077     {
4078       asection *sec = text_section_order[i];
4079       asection *exidx_sec;
4080       struct _tic6x_elf_section_data *tic6x_data
4081 	= get_tic6x_elf_section_data (sec);
4082       struct _tic6x_elf_section_data *exidx_data;
4083       bfd_byte *contents = NULL;
4084       int deleted_exidx_bytes = 0;
4085       bfd_vma j;
4086       tic6x_unwind_table_edit *unwind_edit_head = NULL;
4087       tic6x_unwind_table_edit *unwind_edit_tail = NULL;
4088       Elf_Internal_Shdr *hdr;
4089       bfd *ibfd;
4090 
4091       if (tic6x_data == NULL)
4092 	continue;
4093 
4094       exidx_sec = tic6x_data->u.text.tic6x_exidx_sec;
4095       if (exidx_sec == NULL)
4096 	{
4097 	  /* Section has no unwind data.  */
4098 	  if (last_unwind_type == 0 || !last_exidx_sec)
4099 	    continue;
4100 
4101 	  /* Ignore zero sized sections.  */
4102 	  if (sec->size == 0)
4103 	    continue;
4104 
4105 	  elf32_tic6x_insert_cantunwind_after (last_text_sec, last_exidx_sec);
4106 	  last_unwind_type = 0;
4107 	  continue;
4108 	}
4109 
4110       /* Skip /DISCARD/ sections.  */
4111       if (bfd_is_abs_section (exidx_sec->output_section))
4112 	continue;
4113 
4114       hdr = &elf_section_data (exidx_sec)->this_hdr;
4115       if (hdr->sh_type != SHT_C6000_UNWIND)
4116 	continue;
4117 
4118       exidx_data = get_tic6x_elf_section_data (exidx_sec);
4119       if (exidx_data == NULL)
4120 	continue;
4121 
4122       ibfd = exidx_sec->owner;
4123 
4124       if (hdr->contents != NULL)
4125 	contents = hdr->contents;
4126       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
4127 	/* An error?  */
4128 	continue;
4129 
4130       for (j = 0; j < hdr->sh_size; j += 8)
4131 	{
4132 	  unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
4133 	  int unwind_type;
4134 	  int elide = 0;
4135 
4136 	  /* An EXIDX_CANTUNWIND entry.  */
4137 	  if (second_word == 1)
4138 	    {
4139 	      if (last_unwind_type == 0)
4140 		elide = 1;
4141 	      unwind_type = 0;
4142 	    }
4143 	  /* Inlined unwinding data.  Merge if equal to previous.  */
4144 	  else if ((second_word & 0x80000000) != 0)
4145 	    {
4146 	      if (merge_exidx_entries
4147 		  && last_second_word == second_word
4148 		  && last_unwind_type == 1)
4149 		elide = 1;
4150 	      unwind_type = 1;
4151 	      last_second_word = second_word;
4152 	    }
4153 	  /* Normal table entry.  In theory we could merge these too,
4154 	     but duplicate entries are likely to be much less common.  */
4155 	  else
4156 	    unwind_type = 2;
4157 
4158 	  if (elide)
4159 	    {
4160 	      elf32_tic6x_add_unwind_table_edit (&unwind_edit_head,
4161 		  &unwind_edit_tail, DELETE_EXIDX_ENTRY, NULL, j / 8);
4162 
4163 	      deleted_exidx_bytes += 8;
4164 	    }
4165 
4166 	  last_unwind_type = unwind_type;
4167 	}
4168 
4169       /* Free contents if we allocated it ourselves.  */
4170       if (contents != hdr->contents)
4171 	free (contents);
4172 
4173       /* Record edits to be applied later (in elf32_tic6x_write_section).  */
4174       exidx_data->u.exidx.unwind_edit_list = unwind_edit_head;
4175       exidx_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
4176 
4177       if (deleted_exidx_bytes > 0)
4178 	elf32_tic6x_adjust_exidx_size (exidx_sec, -deleted_exidx_bytes);
4179 
4180       last_exidx_sec = exidx_sec;
4181       last_text_sec = sec;
4182     }
4183 
4184   /* Add terminating CANTUNWIND entry.  */
4185   if (last_exidx_sec && last_unwind_type != 0)
4186     elf32_tic6x_insert_cantunwind_after (last_text_sec, last_exidx_sec);
4187 
4188   return TRUE;
4189 }
4190 
4191 /* Add ADDEND to lower 31 bits of VAL, leaving other bits unmodified.  */
4192 
4193 static unsigned long
elf32_tic6x_add_low31(unsigned long val,bfd_vma addend)4194 elf32_tic6x_add_low31 (unsigned long val, bfd_vma addend)
4195 {
4196   return (val & ~0x7ffffffful) | ((val + addend) & 0x7ffffffful);
4197 }
4198 
4199 /* Copy an .c6xabi.exidx table entry, adding OFFSET to (applied) PREL31
4200    relocations.  OFFSET is in bytes, and will be scaled before encoding.  */
4201 
4202 
4203 static void
elf32_tic6x_copy_exidx_entry(bfd * output_bfd,bfd_byte * to,bfd_byte * from,bfd_vma offset)4204 elf32_tic6x_copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from,
4205 			      bfd_vma offset)
4206 {
4207   unsigned long first_word = bfd_get_32 (output_bfd, from);
4208   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
4209 
4210   offset >>= 1;
4211   /* High bit of first word is supposed to be zero.  */
4212   if ((first_word & 0x80000000ul) == 0)
4213     first_word = elf32_tic6x_add_low31 (first_word, offset);
4214 
4215   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
4216      (EXIDX_CANTUNWIND), this is an offset to an .c6xabi.extab entry.  */
4217   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
4218     second_word = elf32_tic6x_add_low31 (second_word, offset);
4219 
4220   bfd_put_32 (output_bfd, first_word, to);
4221   bfd_put_32 (output_bfd, second_word, to + 4);
4222 }
4223 
4224 /* Do the actual mangling of exception index tables.  */
4225 
4226 static bfd_boolean
elf32_tic6x_write_section(bfd * output_bfd,struct bfd_link_info * link_info,asection * sec,bfd_byte * contents)4227 elf32_tic6x_write_section (bfd *output_bfd,
4228 			 struct bfd_link_info *link_info,
4229 			 asection *sec,
4230 			 bfd_byte *contents)
4231 {
4232   _tic6x_elf_section_data *tic6x_data;
4233   struct elf32_tic6x_link_hash_table *globals
4234     = elf32_tic6x_hash_table (link_info);
4235   bfd_vma offset = sec->output_section->vma + sec->output_offset;
4236 
4237   if (globals == NULL)
4238     return FALSE;
4239 
4240   /* If this section has not been allocated an _tic6x_elf_section_data
4241      structure then we cannot record anything.  */
4242   tic6x_data = get_tic6x_elf_section_data (sec);
4243   if (tic6x_data == NULL)
4244     return FALSE;
4245 
4246   if (tic6x_data->elf.this_hdr.sh_type != SHT_C6000_UNWIND)
4247     return FALSE;
4248 
4249   tic6x_unwind_table_edit *edit_node
4250     = tic6x_data->u.exidx.unwind_edit_list;
4251   /* Now, sec->size is the size of the section we will write.  The original
4252      size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
4253      markers) was sec->rawsize.  (This isn't the case if we perform no
4254      edits, then rawsize will be zero and we should use size).  */
4255   bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
4256   unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
4257   unsigned int in_index, out_index;
4258   bfd_vma add_to_offsets = 0;
4259 
4260   for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
4261     {
4262       if (edit_node)
4263 	{
4264 	  unsigned int edit_index = edit_node->index;
4265 
4266 	  if (in_index < edit_index && in_index * 8 < input_size)
4267 	    {
4268 	      elf32_tic6x_copy_exidx_entry (output_bfd,
4269 		  edited_contents + out_index * 8,
4270 		  contents + in_index * 8, add_to_offsets);
4271 	      out_index++;
4272 	      in_index++;
4273 	    }
4274 	  else if (in_index == edit_index
4275 		   || (in_index * 8 >= input_size
4276 		       && edit_index == UINT_MAX))
4277 	    {
4278 	      switch (edit_node->type)
4279 		{
4280 		case DELETE_EXIDX_ENTRY:
4281 		  in_index++;
4282 		  add_to_offsets += 8;
4283 		  break;
4284 
4285 		case INSERT_EXIDX_CANTUNWIND_AT_END:
4286 		  {
4287 		    asection *text_sec = edit_node->linked_section;
4288 		    bfd_vma text_offset = text_sec->output_section->vma
4289 					  + text_sec->output_offset
4290 					  + text_sec->size;
4291 		    bfd_vma exidx_offset = offset + out_index * 8;
4292 		    unsigned long prel31_offset;
4293 
4294 		    /* Note: this is meant to be equivalent to an
4295 		       R_C6000_PREL31 relocation.  These synthetic
4296 		       EXIDX_CANTUNWIND markers are not relocated by the
4297 		       usual BFD method.  */
4298 		    prel31_offset = ((text_offset - exidx_offset) >> 1)
4299 				    & 0x7ffffffful;
4300 
4301 		    /* First address we can't unwind.  */
4302 		    bfd_put_32 (output_bfd, prel31_offset,
4303 				&edited_contents[out_index * 8]);
4304 
4305 		    /* Code for EXIDX_CANTUNWIND.  */
4306 		    bfd_put_32 (output_bfd, 0x1,
4307 				&edited_contents[out_index * 8 + 4]);
4308 
4309 		    out_index++;
4310 		    add_to_offsets -= 8;
4311 		  }
4312 		  break;
4313 		}
4314 
4315 	      edit_node = edit_node->next;
4316 	    }
4317 	}
4318       else
4319 	{
4320 	  /* No more edits, copy remaining entries verbatim.  */
4321 	  elf32_tic6x_copy_exidx_entry (output_bfd,
4322 	      edited_contents + out_index * 8,
4323 	      contents + in_index * 8, add_to_offsets);
4324 	  out_index++;
4325 	  in_index++;
4326 	}
4327     }
4328 
4329   if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
4330     bfd_set_section_contents (output_bfd, sec->output_section,
4331 			      edited_contents,
4332 			      (file_ptr) sec->output_offset, sec->size);
4333 
4334   return TRUE;
4335 }
4336 
4337 #define TARGET_LITTLE_SYM	tic6x_elf32_le_vec
4338 #define TARGET_LITTLE_NAME	"elf32-tic6x-le"
4339 #define TARGET_BIG_SYM		tic6x_elf32_be_vec
4340 #define TARGET_BIG_NAME		"elf32-tic6x-be"
4341 #define ELF_ARCH		bfd_arch_tic6x
4342 #define ELF_TARGET_ID		TIC6X_ELF_DATA
4343 #define ELF_MACHINE_CODE	EM_TI_C6000
4344 #define ELF_MAXPAGESIZE		0x1000
4345 #define bfd_elf32_bfd_reloc_type_lookup elf32_tic6x_reloc_type_lookup
4346 #define bfd_elf32_bfd_reloc_name_lookup elf32_tic6x_reloc_name_lookup
4347 #define bfd_elf32_bfd_merge_private_bfd_data	elf32_tic6x_merge_private_bfd_data
4348 #define bfd_elf32_mkobject		elf32_tic6x_mkobject
4349 #define bfd_elf32_bfd_link_hash_table_create  elf32_tic6x_link_hash_table_create
4350 #define bfd_elf32_new_section_hook	elf32_tic6x_new_section_hook
4351 #define elf_backend_stack_align		8
4352 #define elf_backend_can_gc_sections	1
4353 #define elf_backend_default_use_rela_p	1
4354 #define elf_backend_may_use_rel_p	1
4355 #define elf_backend_may_use_rela_p	1
4356 #define elf_backend_obj_attrs_arg_type	elf32_tic6x_obj_attrs_arg_type
4357 #define elf_backend_obj_attrs_handle_unknown	elf32_tic6x_obj_attrs_handle_unknown
4358 #define elf_backend_obj_attrs_order	elf32_tic6x_obj_attrs_order
4359 #define elf_backend_obj_attrs_section	".c6xabi.attributes"
4360 #define elf_backend_obj_attrs_section_type	SHT_C6000_ATTRIBUTES
4361 #define elf_backend_obj_attrs_vendor	"c6xabi"
4362 #define elf_backend_can_refcount	1
4363 #define elf_backend_want_got_plt	1
4364 #define elf_backend_want_dynbss		1
4365 #define elf_backend_want_dynrelro	1
4366 #define elf_backend_plt_readonly	1
4367 #define elf_backend_rela_normal		1
4368 #define elf_backend_got_header_size     8
4369 #define elf_backend_fake_sections       elf32_tic6x_fake_sections
4370 #define elf_backend_gc_mark_extra_sections elf32_tic6x_gc_mark_extra_sections
4371 #define elf_backend_create_dynamic_sections \
4372   elf32_tic6x_create_dynamic_sections
4373 #define elf_backend_adjust_dynamic_symbol \
4374   elf32_tic6x_adjust_dynamic_symbol
4375 #define elf_backend_check_relocs	elf32_tic6x_check_relocs
4376 #define elf_backend_add_symbol_hook     elf32_tic6x_add_symbol_hook
4377 #define elf_backend_symbol_processing   elf32_tic6x_symbol_processing
4378 #define elf_backend_link_output_symbol_hook \
4379   elf32_tic6x_link_output_symbol_hook
4380 #define elf_backend_section_from_bfd_section \
4381   elf32_tic6x_section_from_bfd_section
4382 #define elf_backend_relocate_section	elf32_tic6x_relocate_section
4383 #define elf_backend_relocs_compatible	_bfd_elf_relocs_compatible
4384 #define elf_backend_finish_dynamic_symbol \
4385   elf32_tic6x_finish_dynamic_symbol
4386 #define elf_backend_always_size_sections \
4387   elf32_tic6x_always_size_sections
4388 #define elf_backend_size_dynamic_sections \
4389   elf32_tic6x_size_dynamic_sections
4390 #define elf_backend_finish_dynamic_sections \
4391   elf32_tic6x_finish_dynamic_sections
4392 #define bfd_elf32_bfd_final_link \
4393 	elf32_tic6x_final_link
4394 #define elf_backend_write_section	elf32_tic6x_write_section
4395 #define elf_info_to_howto		elf32_tic6x_info_to_howto
4396 #define elf_info_to_howto_rel		elf32_tic6x_info_to_howto_rel
4397 
4398 #undef elf_backend_omit_section_dynsym
4399 #define elf_backend_omit_section_dynsym elf32_tic6x_link_omit_section_dynsym
4400 #define elf_backend_plt_sym_val		elf32_tic6x_plt_sym_val
4401 
4402 #include "elf32-target.h"
4403 
4404 #undef elf32_bed
4405 #define	elf32_bed		elf32_tic6x_linux_bed
4406 
4407 #undef TARGET_LITTLE_SYM
4408 #define	TARGET_LITTLE_SYM		tic6x_elf32_linux_le_vec
4409 #undef TARGET_LITTLE_NAME
4410 #define	TARGET_LITTLE_NAME		"elf32-tic6x-linux-le"
4411 #undef TARGET_BIG_SYM
4412 #define TARGET_BIG_SYM			tic6x_elf32_linux_be_vec
4413 #undef TARGET_BIG_NAME
4414 #define	TARGET_BIG_NAME			"elf32-tic6x-linux-be"
4415 #undef ELF_OSABI
4416 #define	ELF_OSABI			ELFOSABI_C6000_LINUX
4417 
4418 #include "elf32-target.h"
4419 
4420 #undef elf32_bed
4421 #define	elf32_bed		elf32_tic6x_elf_bed
4422 
4423 #undef TARGET_LITTLE_SYM
4424 #define	TARGET_LITTLE_SYM		tic6x_elf32_c6000_le_vec
4425 #undef TARGET_LITTLE_NAME
4426 #define	TARGET_LITTLE_NAME		"elf32-tic6x-elf-le"
4427 #undef TARGET_BIG_SYM
4428 #define TARGET_BIG_SYM			tic6x_elf32_c6000_be_vec
4429 #undef TARGET_BIG_NAME
4430 #define	TARGET_BIG_NAME			"elf32-tic6x-elf-be"
4431 #undef ELF_OSABI
4432 #define	ELF_OSABI			ELFOSABI_C6000_ELFABI
4433 
4434 #include "elf32-target.h"
4435