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