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