packaging: Enable testing infrastructure
[external/binutils.git] / bfd / elf32-csky.c
1 /* 32-bit ELF support for C-SKY.
2    Copyright (C) 1998-2019 Free Software Foundation, Inc.
3    Contributed by C-SKY Microsystems and Mentor Graphics.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/csky.h"
28 #include "opcode/csky.h"
29 #include <assert.h>
30 #include "libiberty.h"
31
32 /* Data structures used for merging different arch variants.
33    V1 (510/610) and V2 (8xx) processors are incompatible, but
34    we can merge wthin each family.  */
35
36 enum merge_class
37 {
38   CSKY_V1,
39   CSKY_V2
40 };
41
42 typedef struct csky_arch_for_merge
43 {
44   const char *name;
45   const unsigned long arch_eflag;
46   /* The files can merge only if they are in same class.  */
47   enum merge_class class;
48   /* When input files have different levels,
49      the target sets arch_eflag to the largest level file's arch_eflag.  */
50   unsigned int class_level;
51   /* Control whether to print warning when merging with different arch.  */
52   unsigned int do_warning;
53 } csky_arch_for_merge;
54
55 static struct csky_arch_for_merge csky_archs[] =
56 {
57   /* 510 and 610 merge to 610 without warning.  */
58   { "510",  CSKY_ARCH_510,  CSKY_V1,  0, 0},
59   { "610",  CSKY_ARCH_610,  CSKY_V1,  1, 0},
60   /* 801, 802, 803, 807, 810 merge to largest one.  */
61   { "801",  CSKY_ARCH_801,  CSKY_V2,  0, 1},
62   { "802",  CSKY_ARCH_802,  CSKY_V2,  1, 1},
63   { "803",  CSKY_ARCH_803,  CSKY_V2,  2, 1},
64   { "807",  CSKY_ARCH_807,  CSKY_V2,  3, 1},
65   { "810",  CSKY_ARCH_810,  CSKY_V2,  4, 1},
66   { NULL, 0, 0, 0, 0}
67 };
68
69 /* Return the ARCH bits out of ABFD.  */
70 #define bfd_csky_arch(abfd) \
71   (elf_elfheader (abfd)->e_flags & CSKY_ARCH_MASK)
72
73 /* Return the ABI bits out of ABFD.  */
74 #define bfd_csky_abi(abfd) \
75   (elf_elfheader (abfd)->e_flags & CSKY_ABI_MASK)
76
77
78 /* The index of a howto-item is implicitly equal to
79    the corresponding Relocation Type Encoding.  */
80 static reloc_howto_type csky_elf_howto_table[] =
81 {
82   /* 0 */
83   HOWTO (R_CKCORE_NONE,               /* type */
84          0,                           /* rightshift */
85          0,                           /* size */
86          0,                           /* bitsize */
87          FALSE,                       /* pc_relative */
88          0,                           /* bitpos */
89          complain_overflow_dont,      /* complain_on_overflow */
90          NULL,                        /* special_function */
91          "R_CKCORE_NONE",             /* name */
92          FALSE,                       /* partial_inplace */
93          0,                           /* src_mask */
94          0,                           /* dst_mask */
95          FALSE),                      /* pcrel_offset */
96
97   /* 1.  */
98   HOWTO (R_CKCORE_ADDR32,             /* type */
99          0,                           /* rightshift */
100          2,                           /* size */
101          32,                          /* bitsize */
102          FALSE,                       /* pc_relative */
103          0,                           /* bitpos */
104          complain_overflow_dont,      /* complain_on_overflow */
105          bfd_elf_generic_reloc,       /* special_function */
106          "R_CKCORE_ADDR32",           /* name */
107          FALSE,                       /* partial_inplace */
108          0,                           /* src_mask */
109          0xffffffff,                  /* dst_mask */
110          FALSE),                      /* pcrel_offset */
111
112   /* 2: Only for csky v1.  */
113   HOWTO (R_CKCORE_PCREL_IMM8BY4,      /* type */
114          2,                           /* rightshift */
115          1,                           /* size */
116          8,                           /* bitsize */
117          TRUE,                        /* pc_relative */
118          0,                           /* bitpos */
119          complain_overflow_bitfield,  /* complain_on_overflow */
120          NULL,                        /* special_function */
121          "R_CKCORE_PCREL_IMM8BY4",    /* name */
122          FALSE,                       /* partial_inplace */
123          0xff,                        /* src_mask */
124          0xff,                        /* dst_mask */
125          TRUE),                       /* pcrel_offset */
126
127   /* 3: Only for csky v1.  */
128   HOWTO (R_CKCORE_PCREL_IMM11BY2,     /* type */
129          1,                           /* rightshift */
130          1,                           /* size */
131          11,                          /* bitsize */
132          TRUE,                        /* pc_relative */
133          0,                           /* bitpos */
134          complain_overflow_signed,    /* complain_on_overflow */
135          bfd_elf_generic_reloc,       /* special_function */
136          "R_CKCORE_PCREL_IMM11BY2",   /* name */
137          FALSE,                       /* partial_inplace */
138          0x7ff,                       /* src_mask */
139          0x7ff,                       /* dst_mask */
140          TRUE),                       /* pcrel_offset */
141
142   /* 4: DELETED.  */
143   HOWTO (R_CKCORE_PCREL_IMM4BY2,0,0,0,0,0,0,0,"R_CKCORE_PCREL_IMM4BY2",0,0,0,0),
144
145   /* 5.  */
146   HOWTO (R_CKCORE_PCREL32,            /* type */
147          0,                           /* rightshift */
148          2,                           /* size */
149          32,                          /* bitsize */
150          TRUE,                        /* pc_relative */
151          0,                           /* bitpos */
152          complain_overflow_dont,      /* complain_on_overflow */
153          bfd_elf_generic_reloc,       /* special_function */
154          "R_CKCORE_PCREL32",          /* name */
155          FALSE,                       /* partial_inplace */
156          0x0,                         /* src_mask */
157          0xffffffff,                  /* dst_mask */
158          TRUE),                       /* pcrel_offset */
159
160   /* 6: Only for csky v1.  */
161   HOWTO (R_CKCORE_PCREL_JSR_IMM11BY2, /* type */
162          1,                           /* rightshift */
163          1,                           /* size */
164          11,                          /* bitsize */
165          TRUE,                        /* pc_relative */
166          0,                           /* bitpos */
167          complain_overflow_signed,    /* complain_on_overflow */
168          bfd_elf_generic_reloc,       /* special_function */
169          "R_CKCORE_PCREL_JSR_IMM11BY2", /* name */
170          FALSE,                       /* partial_inplace */
171          0x7ff,                       /* src_mask */
172          0x7ff,                       /* dst_mask */
173          TRUE),                       /* pcrel_offset */
174
175   /* 7: GNU extension to record C++ vtable member usage.  */
176   HOWTO (R_CKCORE_GNU_VTENTRY,        /* type */
177          0,                           /* rightshift */
178          2,                           /* size */
179          0,                           /* bitsize */
180          FALSE,                       /* pc_relative */
181          0,                           /* bitpos */
182          complain_overflow_dont,      /* complain_on_overflow */
183          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
184          "R_CKCORE_GNU_VTENTRY",      /* name */
185          FALSE,                       /* partial_inplace */
186          0x0,                         /* src_mask */
187          0x0,                         /* dst_mask */
188          FALSE),                      /* pcrel_offset */
189
190   /* 8: GNU extension to record C++ vtable hierarchy.  */
191   HOWTO (R_CKCORE_GNU_VTINHERIT,      /* type */
192          0,                           /* rightshift */
193          2,                           /* size */
194          0,                           /* bitsize */
195          FALSE,                       /* pc_relative */
196          0,                           /* bitpos */
197          complain_overflow_dont,      /* complain_on_overflow */
198          NULL,                        /* special_function */
199          "R_CKCORE_GNU_VTINHERIT",    /* name */
200          FALSE,                       /* partial_inplace */
201          0x0,                         /* src_mask */
202          0x0,                         /* dst_mask */
203          FALSE),                      /* pcrel_offset */
204
205   /* 9.  */
206   HOWTO (R_CKCORE_RELATIVE,           /* type */
207          0,                           /* rightshift */
208          2,                           /* size */
209          32,                          /* bitsize */
210          FALSE,                       /* pc_relative */
211          0,                           /* bitpos */
212          complain_overflow_signed,    /* complain_on_overflow */
213          bfd_elf_generic_reloc,       /* special_function */
214          "R_CKCORE_RELATIVE",         /* name */
215          TRUE,                        /* partial_inplace */
216          0x0,                         /* src_mask */
217          0xffffffff,                  /* dst_mask */
218          FALSE),                      /* pcrel_offset */
219
220   /* 10: None.  */
221   /* FIXME:  It is a bug that copy relocations are not implemented.  */
222   HOWTO (R_CKCORE_COPY,               /* type */
223          0,                           /* rightshift */
224          2,                           /* size */
225          32,                          /* bitsize */
226          FALSE,                       /* pc_relative */
227          0,                           /* bitpos */
228          complain_overflow_bitfield,  /* complain_on_overflow */
229          bfd_elf_generic_reloc,       /* special_function */
230          "R_CKCORE_COPY",             /* name */
231          TRUE,                        /* partial_inplace */
232          0xffffffff,                  /* src_mask */
233          0xffffffff,                  /* dst_mask */
234          FALSE),                      /* pcrel_offset */
235
236   /* 11: None.  */
237   HOWTO (R_CKCORE_GLOB_DAT,0,0,0,0,0,0,0,"R_CKCORE_GLOB_DAT",0,0,0,0),
238
239   /* 12: None.  */
240   HOWTO (R_CKCORE_JUMP_SLOT,0,0,0,0,0,0,0,"R_CKCORE_JUMP_SLOT",0,0,0,0),
241
242   /* 13.  */
243   HOWTO (R_CKCORE_GOTOFF,             /* type */
244          0,                           /* rightshift */
245          2,                           /* size */
246          32,                          /* bitsize */
247          FALSE,                       /* pc_relative */
248          0,                           /* bitpos */
249          complain_overflow_dont,      /* complain_on_overflow */
250          bfd_elf_generic_reloc,       /* special_function */
251          "R_CKCORE_GOTOFF",           /* name */
252          TRUE,                        /* partial_inplace */
253          0x0,                         /* src_mask */
254          0xffffffffl,                 /* dst_mask */
255          FALSE),                      /* pcrel_offset */
256
257   /* 14.  */
258   HOWTO (R_CKCORE_GOTPC,              /* type */
259          0,                           /* rightshift */
260          2,                           /* size */
261          32,                          /* bitsize */
262          TRUE,                        /* pc_relative */
263          0,                           /* bitpos */
264          complain_overflow_dont,      /* complain_on_overflow */
265          bfd_elf_generic_reloc,       /* special_function */
266          "R_CKCORE_GOTPC",            /* name */
267          TRUE,                        /* partial_inplace */
268          0x0,                         /* src_mask */
269          0xffffffff,                  /* dst_mask */
270          FALSE),                      /* pcrel_offset */
271
272   /* 15.  */
273   HOWTO (R_CKCORE_GOT32,              /* type */
274          0,                           /* rightshift */
275          2,                           /* size */
276          32,                          /* bitsize */
277          FALSE,                       /* pc_relative */
278          0,                           /* bitpos */
279          complain_overflow_dont,      /* complain_on_overflow */
280          bfd_elf_generic_reloc,       /* special_function */
281          "R_CKCORE_GOT32",            /* name */
282          TRUE,                        /* partial_inplace */
283          0x0,                         /* src_mask */
284          0xffffffff,                  /* dst_mask */
285          TRUE),                       /* pcrel_offset */
286
287   /* 16.  */
288   HOWTO (R_CKCORE_PLT32,              /* type */
289          0,                           /* rightshift */
290          2,                           /* size */
291          32,                          /* bitsize */
292          FALSE,                       /* pc_relative */
293          0,                           /* bitpos */
294          complain_overflow_dont,      /* complain_on_overflow */
295          bfd_elf_generic_reloc,       /* special_function */
296          "R_CKCORE_PLT32",            /* name */
297          TRUE,                        /* partial_inplace */
298          0x0,                         /* src_mask */
299          0xffffffff,                  /* dst_mask */
300          TRUE),                       /* pcrel_offset */
301
302   /* 17: None.  */
303   HOWTO (R_CKCORE_ADDRGOT,0,0,0,0,0,0,0,"R_CKCORE_ADDRGOT",0,0,0,0),
304
305   /* 18: None.  */
306   HOWTO (R_CKCORE_ADDRPLT,0,0,0,0,0,0,0,"R_CKCORE_ADDRPLT",0,0,0,0),
307
308   /* 19: Only for csky v2.  */
309   HOWTO (R_CKCORE_PCREL_IMM26BY2,     /* type */
310          1,                           /* rightshift */
311          2,                           /* size */
312          26,                          /* bitsize */
313          TRUE,                        /* pc_relative */
314          0,                           /* bitpos */
315          complain_overflow_signed,    /* complain_on_overflow */
316          bfd_elf_generic_reloc,       /* special_function */
317          "R_CKCORE_PCREL_IMM26BY2",   /* name */
318          FALSE,                       /* partial_inplace */
319          0x0,                         /* src_mask */
320          0x3ffffff,                   /* dst_mask */
321          TRUE),                       /* pcrel_offset */
322
323   /* 20: Only for csky v2.  */
324   HOWTO (R_CKCORE_PCREL_IMM16BY2,     /* type */
325          1,                           /* rightshift */
326          2,                           /* size */
327          16,                          /* bitsize */
328          TRUE,                        /* pc_relative */
329          0,                           /* bitpos */
330          complain_overflow_signed,    /* complain_on_overflow */
331          NULL,                        /* special_function */
332          "R_CKCORE_PCREL_IMM16BY2",   /* name */
333          FALSE,                       /* partial_inplace */
334          0x0,                         /* src_mask */
335          0xffff,                      /* dst_mask */
336          TRUE),                       /* pcrel_offset */
337
338   /* 21: Only for csky v2.  */
339   HOWTO (R_CKCORE_PCREL_IMM16BY4,     /* type */
340          2,                           /* rightshift */
341          2,                           /* size */
342          16,                          /* bitsize */
343          TRUE,                        /* pc_relative */
344          0,                           /* bitpos */
345          complain_overflow_bitfield,  /* complain_on_overflow */
346          NULL,                        /* special_function */
347          "R_CKCORE_PCREL_IMM16BY4",   /* name */
348          FALSE,                       /* partial_inplace */
349          0xffff0000,                  /* src_mask */
350          0xffff,                      /* dst_mask */
351          TRUE),                       /* pcrel_offset */
352
353   /* 22: Only for csky v2.  */
354   HOWTO (R_CKCORE_PCREL_IMM10BY2,     /* type */
355          1,                           /* rightshift */
356          1,                           /* size */
357          10,                          /* bitsize */
358          TRUE,                        /* pc_relative */
359          0,                           /* bitpos */
360          complain_overflow_signed,    /* complain_on_overflow */
361          bfd_elf_generic_reloc,       /* special_function */
362          "R_CKCORE_PCREL_IMM10BY2",   /* name */
363          FALSE,                       /* partial_inplace */
364          0x0,                         /* src_mask */
365          0x3ff,                       /* dst_mask */
366          TRUE),                       /* pcrel_offset */
367
368   /* 23: Only for csky v2.  */
369   HOWTO (R_CKCORE_PCREL_IMM10BY4,     /* type */
370          2,                           /* rightshift */
371          2,                           /* size */
372          10,                          /* bitsize */
373          TRUE,                        /* pc_relative */
374          0,                           /* bitpos */
375          complain_overflow_bitfield,  /* complain_on_overflow */
376          NULL,                        /* special_function */
377          "R_CKCORE_PCREL_IMM10BY4",   /* name */
378          FALSE,                       /* partial_inplace */
379          0x0,                         /* src_mask */
380          0x3ff,                       /* dst_mask */
381          TRUE),                       /* pcrel_offset */
382
383   /* 24: Only for csky v2.  */
384   HOWTO (R_CKCORE_ADDR_HI16,          /* type */
385          16,                          /* rightshift */
386          2,                           /* size */
387          16,                          /* bitsize */
388          FALSE,                       /* pc_relative */
389          0,                           /* bitpos */
390          complain_overflow_dont,      /* complain_on_overflow */
391          bfd_elf_generic_reloc,       /* special_function */
392          "R_CKCORE_ADDR_HI16",        /* name */
393          FALSE,                       /* partial_inplace */
394          0x0,                         /* src_mask */
395          0xffff,                      /* dst_mask */
396          FALSE),                      /* pcrel_offset */
397
398   /* 25.  */
399   HOWTO (R_CKCORE_ADDR_LO16,          /* type */
400          0,                           /* rightshift */
401          2,                           /* size */
402          16,                          /* bitsize */
403          FALSE,                       /* pc_relative */
404          0,                           /* bitpos */
405          complain_overflow_dont,      /* complain_on_overflow */
406          bfd_elf_generic_reloc,       /* special_function */
407          "R_CKCORE_ADDR_LO16",        /* name */
408          FALSE,                       /* partial_inplace */
409          0x0,                         /* src_mask */
410          0xffff,                      /* dst_mask */
411          FALSE),                      /* pcrel_offset */
412
413   /* 26.  */
414   HOWTO (R_CKCORE_GOTPC_HI16,         /* type */
415          16,                          /* rightshift */
416          2,                           /* size */
417          16,                          /* bitsize */
418          TRUE,                        /* pc_relative */
419          0,                           /* bitpos */
420          complain_overflow_dont,      /* complain_on_overflow */
421          bfd_elf_generic_reloc,       /* special_function */
422          "R_CKCORE_GOTPC_HI16",       /* name */
423          FALSE,                       /* partial_inplace */
424          0x0,                         /* src_mask */
425          0xffff,                      /* dst_mask */
426          FALSE),                      /* pcrel_offset */
427
428   /* 27.  */
429   HOWTO (R_CKCORE_GOTPC_LO16,         /* type */
430          0,                           /* rightshift */
431          2,                           /* size */
432          16,                          /* bitsize */
433          TRUE,                        /* pc_relative */
434          0,                           /* bitpos */
435          complain_overflow_dont,      /* complain_on_overflow */
436          bfd_elf_generic_reloc,       /* special_function */
437          "R_CKCORE_GOTPC_LO16",       /* name */
438          FALSE,                       /* partial_inplace */
439          0x0,                         /* src_mask */
440          0xffff,                      /* dst_mask */
441          FALSE),                      /* pcrel_offset */
442
443   /* 28.  */
444   HOWTO (R_CKCORE_GOTOFF_HI16,        /* type */
445          16,                          /* rightshift */
446          2,                           /* size */
447          16,                          /* bitsize */
448          FALSE,                       /* pc_relative */
449          0,                           /* bitpos */
450          complain_overflow_dont,      /* complain_on_overflow */
451          bfd_elf_generic_reloc,       /* special_function */
452          "R_CKCORE_GOTOFF_HI16",      /* name */
453          FALSE,                       /* partial_inplace */
454          0x0,                         /* src_mask */
455          0xffff,                      /* dst_mask */
456          FALSE),                      /* pcrel_offset */
457
458   /* 29.  */
459   HOWTO (R_CKCORE_GOTOFF_LO16,        /* type */
460          0,                           /* rightshift */
461          2,                           /* size */
462          16,                          /* bitsize */
463          FALSE,                       /* pc_relative */
464          0,                           /* bitpos */
465          complain_overflow_dont,      /* complain_on_overflow */
466          bfd_elf_generic_reloc,       /* special_function */
467          "R_CKCORE_GOTOFF_LO16",      /* name */
468          FALSE,                       /* partial_inplace */
469          0x0,                         /* src_mask */
470          0xffff,                      /* dst_mask */
471          FALSE),                      /* pcrel_offset */
472
473   /* 30.  */
474   HOWTO (R_CKCORE_GOT12,              /* type */
475          2,                           /* rightshift */
476          2,                           /* size */
477          12,                          /* bitsize */
478          FALSE,                       /* pc_relative */
479          0,                           /* bitpos */
480          complain_overflow_bitfield,  /* complain_on_overflow */
481          bfd_elf_generic_reloc,       /* special_function */
482          "R_CKCORE_GOT12",            /* name */
483          TRUE,                        /* partial_inplace */
484          0x0,                         /* src_mask */
485          0xfff,                       /* dst_mask */
486          FALSE),                      /* pcrel_offset */
487
488   /* 31.  */
489   HOWTO (R_CKCORE_GOT_HI16,           /* type */
490          16,                          /* rightshift */
491          2,                           /* size */
492          16,                          /* bitsize */
493          FALSE,                       /* pc_relative */
494          0,                           /* bitpos */
495          complain_overflow_dont,      /* complain_on_overflow */
496          bfd_elf_generic_reloc,       /* special_function */
497          "R_CKCORE_GOT_HI16",         /* name */
498          TRUE,                       /* partial_inplace */
499          0x0,                         /* src_mask */
500          0xffff,                      /* dst_mask */
501          FALSE),                      /* pcrel_offset */
502
503   /* 32.  */
504   HOWTO (R_CKCORE_GOT_LO16,           /* type */
505          0,                           /* rightshift */
506          2,                           /* size */
507          16,                          /* bitsize */
508          FALSE,                       /* pc_relative */
509          0,                           /* bitpos */
510          complain_overflow_dont,      /* complain_on_overflow */
511          bfd_elf_generic_reloc,       /* special_function */
512          "R_CKCORE_GOT_LO16",         /* name */
513          TRUE,                        /* partial_inplace */
514          0x0,                         /* src_mask */
515          0xffff,                      /* dst_mask */
516          FALSE),                      /* pcrel_offset */
517
518   /* 33.  */
519   HOWTO (R_CKCORE_PLT12,              /* type */
520          2,                           /* rightshift */
521          2,                           /* size */
522          12,                          /* bitsize */
523          FALSE,                       /* pc_relative */
524          0,                           /* bitpos */
525          complain_overflow_bitfield,  /* complain_on_overflow */
526          bfd_elf_generic_reloc,       /* special_function */
527          "R_CKCORE_PLT12",            /* name */
528          TRUE,                        /* partial_inplace */
529          0x0,                         /* src_mask */
530          0xfff,                       /* dst_mask */
531          FALSE),                      /* pcrel_offset */
532
533   /* 34.  */
534   HOWTO (R_CKCORE_PLT_HI16,           /* type */
535          16,                          /* rightshift */
536          2,                           /* size */
537          16,                          /* bitsize */
538          FALSE,                       /* pc_relative */
539          0,                           /* bitpos */
540          complain_overflow_dont,      /* complain_on_overflow */
541          bfd_elf_generic_reloc,       /* special_function */
542          "R_CKCORE_PLT_HI16",         /* name */
543          TRUE,                        /* partial_inplace */
544          0x0,                         /* src_mask */
545          0xffff,                      /* dst_mask */
546          FALSE),                      /* pcrel_offset */
547
548   /* 35.  */
549   HOWTO (R_CKCORE_PLT_LO16,           /* type */
550          0,                           /* rightshift */
551          2,                           /* size */
552          16,                          /* bitsize */
553          FALSE,                       /* pc_relative */
554          0,                           /* bitpos */
555          complain_overflow_dont,      /* complain_on_overflow */
556          bfd_elf_generic_reloc,       /* special_function */
557          "R_CKCORE_PLT_LO16",         /* name */
558          TRUE,                       /* partial_inplace */
559          0x0,                         /* src_mask */
560          0xffff,                      /* dst_mask */
561          FALSE),                      /* pcrel_offset */
562
563   /* 36: None.  */
564   HOWTO (R_CKCORE_ADDRGOT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
565
566   /* 37: None.  */
567   HOWTO (R_CKCORE_ADDRGOT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
568
569   /* 38: None.  */
570   HOWTO (R_CKCORE_ADDRPLT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
571
572   /* 39: None.  */
573   HOWTO (R_CKCORE_ADDRPLT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
574
575   /* 40.  */
576   HOWTO (R_CKCORE_PCREL_JSR_IMM26BY2, /* type */
577          1,                           /* rightshift */
578          2,                           /* size */
579          26,                          /* bitsize */
580          TRUE,                        /* pc_relative */
581          0,                           /* bitpos */
582          complain_overflow_signed,    /* complain_on_overflow */
583          bfd_elf_generic_reloc,       /* special_function */
584          "R_CKCORE_PCREL_JSR_IMM26BY2", /* name */
585          FALSE,                       /* partial_inplace */
586          0x0,                         /* src_mask */
587          0x3ffffff,                   /* dst_mask */
588          TRUE),                       /* pcrel_offset */
589
590   /* 41.  */
591   HOWTO (R_CKCORE_TOFFSET_LO16,       /* type */
592          0,                           /* rightshift */
593          2,                           /* size */
594          16,                          /* bitsize */
595          FALSE,                       /* pc_relative */
596          0,                           /* bitpos */
597          complain_overflow_unsigned,  /* complain_on_overflow */
598          NULL,                        /* special_function */
599          "R_CKCORE_TOFFSET_LO16",     /* name */
600          FALSE,                       /* partial_inplace */
601          0x0,                         /* src_mask */
602          0xffff,                      /* dst_mask */
603          FALSE),                      /* pcrel_offset */
604
605   /* 42.  */
606   HOWTO (R_CKCORE_DOFFSET_LO16,       /* type */
607          0,                           /* rightshift */
608          2,                           /* size */
609          16,                          /* bitsize */
610          FALSE,                       /* pc_relative */
611          0,                           /* bitpos */
612          complain_overflow_unsigned,  /* complain_on_overflow */
613          NULL,                        /* special_function */
614          "R_CKCORE_DOFFSET_LO16",     /* name */
615          FALSE,                       /* partial_inplace */
616          0x0,                         /* src_mask */
617          0xffff,                      /* dst_mask */
618          FALSE),                      /* pcrel_offset */
619
620   /* 43.  */
621   HOWTO (R_CKCORE_PCREL_IMM18BY2,     /* type */
622          1,                           /* rightshift */
623          2,                           /* size */
624          18,                          /* bitsize */
625          TRUE,                        /* pc_relative */
626          0,                           /* bitpos */
627          complain_overflow_signed,    /* complain_on_overflow */
628          NULL,                        /* special_function */
629          "R_CKCORE_PCREL_IMM18BY2",   /* name */
630          FALSE,                       /* partial_inplace */
631          0x0,                         /* src_mask */
632          0x3ffff,                     /* dst_mask */
633          TRUE),                       /* pcrel_offset */
634
635   /* 44.  */
636   HOWTO (R_CKCORE_DOFFSET_IMM18,      /* type */
637          0,                           /* rightshift */
638          2,                           /* size */
639          18,                          /* bitsize */
640          FALSE,                       /* pc_relative */
641          0,                           /* bitpos */
642          complain_overflow_unsigned,  /* complain_on_overflow */
643          NULL,                        /* special_function */
644          "R_CKCORE_DOFFSET_IMM18",    /* name */
645          FALSE,                       /* partial_inplace */
646          0x0,                         /* src_mask */
647          0x3ffff,                     /* dst_mask */
648          FALSE),                      /* pcrel_offset */
649
650   /* 45.  */
651   HOWTO (R_CKCORE_DOFFSET_IMM18BY2,   /* type */
652          1,                           /* rightshift */
653          2,                           /* size */
654          18,                          /* bitsize */
655          FALSE,                       /* pc_relative */
656          0,                           /* bitpos */
657          complain_overflow_unsigned,  /* complain_on_overflow */
658          NULL,                        /* special_function */
659          "R_CKCORE_DOFFSET_IMM18BY2", /* name */
660          FALSE,                       /* partial_inplace */
661          0x0,                         /* src_mask */
662          0x3ffff,                     /* dst_mask */
663          FALSE),                      /* pcrel_offset */
664
665   /* 46.  */
666   HOWTO (R_CKCORE_DOFFSET_IMM18BY4,   /* type */
667          2,                           /* rightshift */
668          2,                           /* size */
669          18,                          /* bitsize */
670          FALSE,                       /* pc_relative */
671          0,                           /* bitpos */
672          complain_overflow_unsigned,  /* complain_on_overflow */
673          NULL,                        /* special_function */
674          "R_CKCORE_DOFFSET_IMM18BY4", /* name */
675          FALSE,                       /* partial_inplace */
676          0x0,                         /* src_mask */
677          0x3ffff,                     /* dst_mask */
678          FALSE),                      /* pcrel_offset */
679
680   /* 47.  */
681   HOWTO (R_CKCORE_GOTOFF_IMM18,       /* type */
682          0,                           /* rightshift */
683          2,                           /* size */
684          18,                          /* bitsize */
685          FALSE,                       /* pc_relative */
686          0,                           /* bitpos */
687          complain_overflow_bitfield,  /* complain_on_overflow */
688          bfd_elf_generic_reloc,       /* special_function */
689          "R_CKCORE_GOTOFF_IMM18",     /* name */
690          TRUE,                        /* partial_inplace */
691          0xfffc,                      /* src_mask */
692          0x3ffff,                     /* dst_mask */
693          FALSE),                      /* pcrel_offset */
694
695   /* 48.  */
696   HOWTO (R_CKCORE_GOT_IMM18BY4,       /* type */
697          2,                           /* rightshift */
698          2,                           /* size */
699          18,                          /* bitsize */
700          FALSE,                       /* pc_relative */
701          0,                           /* bitpos */
702          complain_overflow_bitfield,  /* complain_on_overflow */
703          bfd_elf_generic_reloc,       /* special_function */
704          "R_CKCORE_GOT_IMM18BY4",     /* name */
705          TRUE,                        /* partial_inplace */
706          0xfffc,                      /* src_mask */
707          0x3ffff,                     /* dst_mask */
708          FALSE),                      /* pcrel_offset */
709
710   /* 49.  */
711   HOWTO (R_CKCORE_PLT_IMM18BY4,       /* type */
712          2,                           /* rightshift */
713          2,                           /* size */
714          18,                          /* bitsize */
715          FALSE,                       /* pc_relative */
716          0,                           /* bitpos */
717          complain_overflow_bitfield,  /* complain_on_overflow */
718          bfd_elf_generic_reloc,       /* special_function */
719          "R_CKCORE_PLT_IMM18BY4",     /* name */
720          TRUE,                        /* partial_inplace */
721          0xfffc,                      /* src_mask */
722          0x3ffff,                     /* dst_mask */
723          TRUE),                       /* pcrel_offset */
724
725   /* 50: for lrw16.  */
726   HOWTO (R_CKCORE_PCREL_IMM7BY4,      /* type */
727          2,                           /* rightshift */
728          1,                           /* size */
729          7,                           /* bitsize */
730          TRUE,                        /* pc_relative */
731          0,                           /* bitpos */
732          complain_overflow_bitfield,  /* complain_on_overflow */
733          bfd_elf_generic_reloc,       /* special_function */
734          "R_CKCORE_PCREL_IMM7BY4",    /* name */
735          FALSE,                       /* partial_inplace */
736          0xec1f,                      /* src_mask */
737          0x31f,                       /* dst_mask */
738          TRUE),                       /* pcrel_offset */
739
740   /* 51: for static nptl.  */
741   HOWTO (R_CKCORE_TLS_LE32,           /* type */
742          0,                           /* rightshift */
743          2,                           /* size */
744          32,                          /* bitsize */
745          FALSE,                       /* pc_relative */
746          0,                           /* bitpos */
747          complain_overflow_dont,      /* complain_on_overflow */
748          bfd_elf_generic_reloc,       /* special_function */
749          "R_CKCORE_TLS_LE32",         /* name */
750          FALSE,                       /* partial_inplace */
751          0x0,                         /* src_mask */
752          0xffffffff,                  /* dst_mask */
753          TRUE),                       /* pcrel_offset */
754
755   /* 52: for static nptl.  */
756   HOWTO (R_CKCORE_TLS_IE32,           /* type */
757          0,                           /* rightshift */
758          2,                           /* size */
759          32,                          /* bitsize */
760          FALSE,                       /* pc_relative */
761          0,                           /* bitpos */
762          complain_overflow_dont,      /* complain_on_overflow */
763          bfd_elf_generic_reloc,       /* special_function */
764          "R_CKCORE_TLS_IE32",         /* name */
765          FALSE,                       /* partial_inplace */
766          0x0,                         /* src_mask */
767          0xffffffff,                  /* dst_mask */
768          TRUE),                       /* pcrel_offset */
769
770   /* 53: for pic nptl.  */
771   HOWTO (R_CKCORE_TLS_GD32,           /* type */
772          0,                           /* rightshift */
773          2,                           /* size */
774          32,                          /* bitsize */
775          FALSE,                       /* pc_relative */
776          0,                           /* bitpos */
777          complain_overflow_dont,      /* complain_on_overflow */
778          bfd_elf_generic_reloc,       /* special_function */
779          "R_CKCORE_TLS_GD32",         /* name */
780          FALSE,                       /* partial_inplace */
781          0x0,                         /* src_mask */
782          0xffffffff,                  /* dst_mask */
783          TRUE),                       /* pcrel_offset */
784
785   /* 54: for pic nptl.  */
786   HOWTO (R_CKCORE_TLS_LDM32,          /* type */
787          0,                           /* rightshift */
788          2,                           /* size */
789          32,                          /* bitsize */
790          FALSE,                       /* pc_relative */
791          0,                           /* bitpos */
792          complain_overflow_dont,      /* complain_on_overflow */
793          bfd_elf_generic_reloc,       /* special_function */
794          "R_CKCORE_TLS_LDM32",        /* name */
795          FALSE,                       /* partial_inplace */
796          0x0,                         /* src_mask */
797          0xffffffff,                  /* dst_mask */
798          TRUE),                       /* pcrel_offset */
799
800   /* 55: for pic nptl.  */
801   HOWTO (R_CKCORE_TLS_LDO32,          /* type */
802          0,                           /* rightshift */
803          2,                           /* size */
804          32,                          /* bitsize */
805          FALSE,                       /* pc_relative */
806          0,                           /* bitpos */
807          complain_overflow_dont,      /* complain_on_overflow */
808          bfd_elf_generic_reloc,       /* special_function */
809          "R_CKCORE_TLS_LDO32",        /* name */
810          FALSE,                       /* partial_inplace */
811          0x0,                         /* src_mask */
812          0xffffffff,                  /* dst_mask */
813          TRUE),                       /* pcrel_offset */
814
815   /* 56: for linker.  */
816   HOWTO (R_CKCORE_TLS_DTPMOD32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPMOD32",0,0,0,0),
817
818   /* 57: for linker.  */
819   HOWTO (R_CKCORE_TLS_DTPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPOFF32",0,0,0,0),
820
821   /* 58: for linker.  */
822   HOWTO (R_CKCORE_TLS_TPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_TPOFF32",0,0,0,0),
823
824   /* 59: for ck807f.  */
825   HOWTO (R_CKCORE_PCREL_FLRW_IMM8BY4, /* type */
826          2,                           /* rightshift */
827          2,                           /* size */
828          8,                           /* bitsize */
829          TRUE,                        /* pc_relative */
830          0,                           /* bitpos */
831          complain_overflow_bitfield,  /* complain_on_overflow */
832          NULL,                        /* special_function */
833          "R_CKCORE_PCREL_FLRW_IMM8BY4", /* name */
834          FALSE,                       /* partial_inplace */
835          0xfe1fff0f,                  /* src_mask */
836          0x1e000f0,                   /* dst_mask */
837          TRUE),                       /* pcrel_offset */
838
839   /* 60: for 810 not to generate jsri.  */
840   HOWTO (R_CKCORE_NOJSRI,             /* type */
841          0,                           /* rightshift */
842          2,                           /* size */
843          32,                          /* bitsize */
844          FALSE,                       /* pc_relative */
845          0,                           /* bitpos */
846          complain_overflow_dont,      /* complain_on_overflow */
847          bfd_elf_generic_reloc,       /* special_function */
848          "R_CKCORE_NOJSRI",           /* name */
849          FALSE,                       /* partial_inplace */
850          0xffff,                      /* src_mask */
851          0xffff,                      /* dst_mask */
852          FALSE),                      /* pcrel_offset */
853
854   /* 61: for callgraph.  */
855   HOWTO (R_CKCORE_CALLGRAPH,          /* type */
856          0,                           /* rightshift */
857          0,                           /* size */
858          0,                           /* bitsize */
859          FALSE,                       /* pc_relative */
860          0,                           /* bitpos */
861          complain_overflow_dont,      /* complain_on_overflow */
862          NULL,                        /* special_function */
863          "R_CKCORE_CALLGRAPH",        /* name */
864          FALSE,                       /* partial_inplace */
865          0x0,                         /* src_mask */
866          0x0,                         /* dst_mask */
867          TRUE),                       /* pcrel_offset */
868
869   /* 62: IRELATIVE*/
870   HOWTO (R_CKCORE_IRELATIVE,0,0,0,0,0,0,0,"R_CKCORE_IRELATIVE",0,0,0,0),
871
872   /* 63: for bloop instruction */
873   HOWTO (R_CKCORE_PCREL_BLOOP_IMM4BY4, /* type */
874          1,                           /* rightshift */
875          2,                           /* size */
876          4,                           /* bitsize */
877          1,                           /* pc_relative */
878          0,                           /* bitpos */
879          complain_overflow_signed,    /* complain_on_overflow */
880          bfd_elf_generic_reloc,       /* special_function */
881          "R_CKCORE_PCREL_BLOOP_IMM4BY4", /* name */
882          FALSE,                       /* partial_inplace */
883          0x0,                         /* src_mask */
884          0xf,                         /* dst_mask */
885          TRUE),                       /* pcrel_offset */
886   /* 64: for bloop instruction */
887   HOWTO (R_CKCORE_PCREL_BLOOP_IMM12BY4, /* type */
888          1,                           /* rightshift */
889          2,                           /* size */
890          12,                          /* bitsize */
891          1,                           /* pc_relative */
892          0,                           /* bitpos */
893          complain_overflow_signed,    /* complain_on_overflow */
894          bfd_elf_generic_reloc,       /* special_function */
895          "R_CKCORE_PCREL_BLOOP_IMM12BY4", /* name */
896          FALSE,                       /* partial_inplace */
897          0x0,                         /* src_mask */
898          0xfff,                       /* dst_mask */
899          TRUE),                       /* pcrel_offset */
900
901
902 };
903
904
905 /* Whether GOT overflow checking is needed.  */
906 static int check_got_overflow = 0;
907
908 /* Whether the target 32 bits is forced so that the high
909    16 bits is at the low address.  */
910 static int need_reverse_bits;
911
912 /* Used for relaxation.  See csky_relocate_contents.  */
913 static bfd_vma read_content_substitute;
914
915 /* NOTICE!
916    The way the following two look-up functions work demands
917    that BFD_RELOC_CKCORE_xxx are defined contiguously.  */
918
919 static reloc_howto_type *
920 csky_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
921                             bfd_reloc_code_real_type code)
922 {
923   int csky_code = code - BFD_RELOC_CKCORE_NONE;
924
925   if (csky_code < 0 || csky_code >= R_CKCORE_MAX)
926     {
927       switch (code)
928         {
929         case BFD_RELOC_NONE:
930           csky_code = R_CKCORE_NONE;
931           break;
932         case BFD_RELOC_32:
933           csky_code = R_CKCORE_ADDR32;
934           break;
935         case BFD_RELOC_32_PCREL:
936           csky_code = R_CKCORE_PCREL32;
937           break;
938         case BFD_RELOC_VTABLE_INHERIT:
939           csky_code = R_CKCORE_GNU_VTINHERIT;
940           break;
941         case BFD_RELOC_VTABLE_ENTRY:
942           csky_code = R_CKCORE_GNU_VTENTRY;
943           break;
944         case BFD_RELOC_RVA:
945           csky_code = R_CKCORE_RELATIVE;
946           break;
947         default:
948           return (reloc_howto_type *)NULL;
949         }
950     }
951   /* Note: when adding csky bfd reloc types in bfd-in2.h
952      and csky elf reloc types in elf/csky.h,
953      the order of the two reloc type tables should be consistent.  */
954   return &csky_elf_howto_table[csky_code];
955 }
956
957 static reloc_howto_type *
958 csky_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
959                             const char *r_name)
960 {
961   unsigned int i;
962   for (i = 0; i < R_CKCORE_MAX; i++)
963     if (strcasecmp (csky_elf_howto_table[i].name, r_name) == 0)
964       return &csky_elf_howto_table[i];
965   return NULL;
966 }
967
968 static reloc_howto_type *
969 elf32_csky_howto_from_type (unsigned int r_type)
970 {
971   if (r_type < R_CKCORE_MAX)
972     return &csky_elf_howto_table[r_type];
973   else
974     return NULL;
975 }
976
977 static bfd_boolean
978 csky_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
979                         arelent *cache_ptr,
980                         Elf_Internal_Rela *dst)
981 {
982   unsigned int r_type;
983
984   r_type = ELF32_R_TYPE (dst->r_info);
985   cache_ptr->howto = elf32_csky_howto_from_type (r_type);
986   if (cache_ptr->howto == NULL)
987     {
988       /* xgettext:c-format */
989       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
990                           abfd, r_type);
991       bfd_set_error (bfd_error_bad_value);
992       return FALSE;
993     }
994   return TRUE;
995 }
996
997 /* The Global Offset Table max size.  */
998 #define GOT_MAX_SIZE 0xFFFF8
999
1000 /* The name of the dynamic interpreter.  This is put in the .interp
1001    section.  */
1002 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1003
1004 /* The size in bytes of an entry in the procedure linkage table.  */
1005 #define PLT_ENTRY_SIZE      12
1006 #define PLT_ENTRY_SIZE_P    16
1007
1008 /* The first entry in a procedure linkage table looks like
1009    this.  It is set up so that any shared library function that is
1010    called before the relocation has been set up calls the dynamic
1011    linker first.  */
1012 static const bfd_vma csky_elf_plt_entry_v2[PLT_ENTRY_SIZE / 4] =
1013 {
1014   0xd99c2002,  /* ldw r12, (gb, 8)  */
1015   0xea0d0000,  /* movi r13,offset   */
1016   0xe8cc0000   /* jmp r12           */
1017 };
1018
1019 static const bfd_vma csky_elf_plt_entry_v1[PLT_ENTRY_SIZE / 2 ] =
1020 {
1021   0x25f0,  /* subi r0, 32       */
1022   0x9200,  /* stw r2, (r0, 0)   */
1023   0x9310,  /* stw r3, (r0, 4)   */
1024   0x822e,  /* ldw r2, (gb, 8)   */
1025   0x7301,  /* lrw r3, #offset   */
1026   0x00c2,  /* jmp r2            */
1027 };
1028
1029 /* Branch stub support.  */
1030
1031 enum stub_insn_type
1032 {
1033   INSN16,
1034   INSN32,
1035   DATA_TYPE
1036 };
1037
1038 bfd_boolean use_branch_stub = TRUE;
1039 typedef struct
1040 {
1041   bfd_vma data;
1042   enum stub_insn_type type;
1043   unsigned int r_type;
1044   int reloc_addend;
1045 } insn_sequence;
1046
1047 static const insn_sequence elf32_csky_stub_long_branch[] =
1048 {
1049   {0xea8d0002, INSN32,    R_CKCORE_NONE,   0x0},   /* lrw t1,[pc+8] */
1050   {0x7834,     INSN16,    R_CKCORE_NONE,   0x0},   /* jmp t1 */
1051   {0x6c03,     INSN16,    R_CKCORE_NONE,   0x0},   /* nop */
1052   {0x0,        DATA_TYPE, R_CKCORE_ADDR32, 0x0}    /* .long addr */
1053 };
1054
1055 static const insn_sequence elf32_csky_stub_long_branch_jmpi[] =
1056 {
1057   {0xeac00001, INSN32,    R_CKCORE_NONE,   0x0},   /* jmpi [pc+4] */
1058   {0x0,        DATA_TYPE, R_CKCORE_ADDR32, 0x0}    /* .long addr */
1059 };
1060
1061 /* The bsr instruction offset limit.  */
1062 #define BSR_MAX_FWD_BRANCH_OFFSET       (((1 << 25) - 1) << 1)
1063 #define BSR_MAX_BWD_BRANCH_OFFSET       (-(1 << 26))
1064
1065 #define STUB_SUFFIX ".stub"
1066 #define STUB_ENTRY_NAME "__%s_veneer"
1067
1068 /* One entry per long/short branch stub defined above.  */
1069 #define DEF_STUBS \
1070   DEF_STUB(long_branch) \
1071   DEF_STUB(long_branch_jmpi)
1072
1073 #define DEF_STUB(x) csky_stub_##x,
1074 enum elf32_csky_stub_type
1075 {
1076   csky_stub_none,
1077   DEF_STUBS
1078 };
1079 #undef DEF_STUB
1080
1081 typedef struct
1082 {
1083   const insn_sequence* template_sequence;
1084   int template_size;
1085 } stub_def;
1086
1087 #define DEF_STUB(x) {elf32_csky_stub_##x, ARRAY_SIZE(elf32_csky_stub_##x)},
1088 static const stub_def stub_definitions[] = {
1089   {NULL, 0},
1090   DEF_STUBS
1091 };
1092
1093 /* The size of the thread control block.  */
1094 #define TCB_SIZE        8
1095
1096 struct csky_elf_obj_tdata
1097 {
1098   struct elf_obj_tdata root;
1099
1100   /* tls_type for each local got entry.  */
1101   char *local_got_tls_type;
1102 };
1103
1104 #define csky_elf_local_got_tls_type(bfd) \
1105   (csky_elf_tdata (bfd)->local_got_tls_type)
1106
1107 #define csky_elf_tdata(bfd) \
1108   ((struct csky_elf_obj_tdata *) (bfd)->tdata.any)
1109
1110 struct elf32_csky_stub_hash_entry
1111 {
1112   /* Base hash table entry structure.  */
1113   struct bfd_hash_entry root;
1114
1115   /* The stub section.  */
1116   asection *stub_sec;
1117
1118   /* Offset within stub_sec of the beginning of this stub.  */
1119   bfd_vma stub_offset;
1120
1121   /* Given the symbol's value and its section we can determine its final
1122      value when building the stubs (so the stub knows where to jump).  */
1123   bfd_vma target_value;
1124   asection *target_section;
1125
1126     /* Offset to apply to relocation referencing target_value.  */
1127   bfd_vma target_addend;
1128
1129   /* The stub type.  */
1130   enum elf32_csky_stub_type stub_type;
1131   /* Its encoding size in bytes.  */
1132   int stub_size;
1133   /* Its template.  */
1134   const insn_sequence *stub_template;
1135   /* The size of the template (number of entries).  */
1136   int stub_template_size;
1137
1138   /* The symbol table entry, if any, that this was derived from.  */
1139   struct csky_elf_link_hash_entry *h;
1140
1141   /* Destination symbol type.  */
1142   unsigned char st_type;
1143
1144   /* Where this stub is being called from, or, in the case of combined
1145      stub sections, the first input section in the group.  */
1146   asection *id_sec;
1147
1148   /* The name for the local symbol at the start of this stub.  The
1149      stub name in the hash table has to be unique; this does not, so
1150      it can be friendlier.  */
1151   char *output_name;
1152 };
1153
1154 #define csky_stub_hash_lookup(table, string, create, copy) \
1155   ((struct elf32_csky_stub_hash_entry *) \
1156    bfd_hash_lookup ((table), (string), (create), (copy)))
1157
1158 /* C-SKY ELF linker hash entry.  */
1159 struct csky_elf_link_hash_entry
1160 {
1161   struct elf_link_hash_entry elf;
1162   int plt_refcount;
1163   /* For sub jsri2bsr relocs count.  */
1164   int jsri2bsr_refcount;
1165   /* Track dynamic relocs copied for this symbol.  */
1166   struct elf_dyn_relocs *dyn_relocs;
1167
1168 #define GOT_UNKNOWN     0
1169 #define GOT_NORMAL      1
1170 #define GOT_TLS_GD      2
1171 #define GOT_TLS_IE      4
1172
1173   unsigned char tls_type;
1174
1175   /* A pointer to the most recently used stub hash entry against this
1176      symbol.  */
1177   struct elf32_csky_stub_hash_entry *stub_cache;
1178 };
1179
1180 /* Traverse an C-SKY ELF linker hash table.  */
1181 #define csky_elf_link_hash_traverse(table, func, info)                     \
1182   (elf_link_hash_traverse                                                  \
1183    (&(table)->root,                                                        \
1184     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1185     (info)))
1186
1187 /* Get the C-SKY ELF linker hash table from a link_info structure.  */
1188 #define csky_elf_hash_table(info) \
1189   ((elf_hash_table_id  ((struct elf_link_hash_table *) ((info)->hash))  \
1190     == CSKY_ELF_DATA)                                                   \
1191    ? ((struct csky_elf_link_hash_table *) ((info)->hash))               \
1192    : NULL)
1193
1194 #define csky_elf_hash_entry(ent)  ((struct csky_elf_link_hash_entry*)(ent))
1195
1196 /* Array to keep track of which stub sections have been created, and
1197    information on stub grouping.  */
1198 struct map_stub
1199 {
1200   /* This is the section to which stubs in the group will be
1201      attached.  */
1202   asection *link_sec;
1203   /* The stub section.  */
1204   asection *stub_sec;
1205 };
1206
1207 /* C-SKY ELF linker hash table.  */
1208 struct csky_elf_link_hash_table
1209 {
1210   struct elf_link_hash_table elf;
1211
1212   /* Small local sym cache.  */
1213   struct sym_cache sym_cache;
1214
1215   /* Data for R_CKCORE_TLS_LDM32 relocations.  */
1216   union
1217   {
1218     bfd_signed_vma refcount;
1219     bfd_vma offset;
1220   } tls_ldm_got;
1221
1222   /* The stub hash table.  */
1223   struct bfd_hash_table stub_hash_table;
1224
1225   /* Linker stub bfd.  */
1226   bfd *stub_bfd;
1227
1228   /* Linker call-backs.  */
1229   asection * (*add_stub_section) (const char *, asection *);
1230   void (*layout_sections_again) (void);
1231
1232   /* Array to keep track of which stub sections have been created, and
1233    * information on stub grouping.  */
1234   struct map_stub *stub_group;
1235
1236   /* Number of elements in stub_group.  */
1237   unsigned int top_id;
1238
1239   /* Assorted information used by elf32_csky_size_stubs.  */
1240   unsigned int bfd_count;
1241   unsigned int top_index;
1242   asection **input_list;
1243 };
1244
1245 /* We can't change vectors in the bfd target which will apply to
1246    data sections, however we only do this to the text sections.  */
1247
1248 static bfd_vma
1249 csky_get_insn_32 (bfd *input_bfd,
1250                   bfd_byte *location)
1251 {
1252   if (bfd_big_endian (input_bfd))
1253     return bfd_get_32 (input_bfd, location);
1254   else
1255     return (bfd_get_16 (input_bfd, location) << 16
1256             | bfd_get_16 (input_bfd, location + 2));
1257 }
1258
1259 static void
1260 csky_put_insn_32 (bfd *input_bfd,
1261                   bfd_vma x,
1262                   bfd_byte *location)
1263 {
1264   if (bfd_big_endian (input_bfd))
1265     bfd_put_32 (input_bfd, x, location);
1266   else
1267     {
1268       bfd_put_16 (input_bfd, x >> 16, location);
1269       bfd_put_16 (input_bfd, x & 0xffff, location + 2);
1270     }
1271 }
1272
1273 /* Find or create a stub section.  Returns a pointer to the stub section, and
1274    the section to which the stub section will be attached (in *LINK_SEC_P).
1275    LINK_SEC_P may be NULL.  */
1276
1277 static asection *
1278 elf32_csky_create_or_find_stub_sec (asection **link_sec_p, asection *section,
1279                                     struct csky_elf_link_hash_table *htab)
1280 {
1281   asection *link_sec;
1282   asection *stub_sec;
1283
1284   link_sec = htab->stub_group[section->id].link_sec;
1285   stub_sec = htab->stub_group[section->id].stub_sec;
1286   if (stub_sec == NULL)
1287     {
1288       stub_sec = htab->stub_group[link_sec->id].stub_sec;
1289       if (stub_sec == NULL)
1290         {
1291           size_t namelen;
1292           bfd_size_type len;
1293           char *s_name;
1294
1295           namelen = strlen (link_sec->name);
1296           len = namelen + sizeof (STUB_SUFFIX);
1297           s_name = bfd_alloc (htab->stub_bfd, len);
1298           if (s_name == NULL)
1299             return NULL;
1300
1301           memcpy (s_name, link_sec->name, namelen);
1302           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1303           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1304           if (stub_sec == NULL)
1305             return NULL;
1306           htab->stub_group[link_sec->id].stub_sec = stub_sec;
1307         }
1308       htab->stub_group[section->id].stub_sec = stub_sec;
1309     }
1310
1311   if (link_sec_p)
1312     *link_sec_p = link_sec;
1313
1314   return stub_sec;
1315 }
1316
1317 /* Build a name for an entry in the stub hash table.  */
1318
1319 static char *
1320 elf32_csky_stub_name (const asection *input_section,
1321                       const asection *sym_sec,
1322                       const struct csky_elf_link_hash_entry *hash,
1323                       const Elf_Internal_Rela *rel)
1324 {
1325   char *stub_name;
1326   bfd_size_type len;
1327
1328   if (hash)
1329     {
1330       len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
1331       stub_name = bfd_malloc (len);
1332       if (stub_name != NULL)
1333         sprintf (stub_name, "%08x_%s+%x",
1334                  input_section->id & 0xffffffff,
1335                  hash->elf.root.root.string,
1336                  (int) rel->r_addend & 0xffffffff);
1337     }
1338   else
1339     {
1340       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1341       stub_name = bfd_malloc (len);
1342       if (stub_name != NULL)
1343         sprintf (stub_name, "%08x_%x:%x+%x",
1344                  input_section->id & 0xffffffff,
1345                  sym_sec->id & 0xffffffff,
1346                  (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1347                  (int) rel->r_addend & 0xffffffff);
1348     }
1349
1350   return stub_name;
1351 }
1352
1353 /* Determine the type of stub needed, if any, for a call.  */
1354
1355 static enum elf32_csky_stub_type
1356 csky_type_of_stub (struct bfd_link_info *info,
1357                    asection *input_sec,
1358                    const Elf_Internal_Rela *rel,
1359                    unsigned char st_type,
1360                    struct csky_elf_link_hash_entry *hash,
1361                    bfd_vma destination,
1362                    asection *sym_sec ATTRIBUTE_UNUSED,
1363                    bfd *input_bfd ATTRIBUTE_UNUSED,
1364                    const char *name ATTRIBUTE_UNUSED)
1365 {
1366   bfd_vma location;
1367   bfd_signed_vma branch_offset;
1368   unsigned int r_type;
1369   enum elf32_csky_stub_type stub_type = csky_stub_none;
1370   struct elf_link_hash_entry * h = &hash->elf;
1371
1372   /* We don't know the actual type of destination in case it is of
1373      type STT_SECTION: give up.  */
1374   if (st_type == STT_SECTION)
1375     return stub_type;
1376
1377   location = (input_sec->output_offset
1378               + input_sec->output_section->vma
1379               + rel->r_offset);
1380
1381   branch_offset = (bfd_signed_vma)(destination - location);
1382   r_type = ELF32_R_TYPE (rel->r_info);
1383   if (r_type == R_CKCORE_PCREL_IMM26BY2
1384       && ((h != NULL
1385            && ((h->def_dynamic && !h->def_regular)
1386                || (bfd_link_pic (info)
1387                    && h->root.type == bfd_link_hash_defweak)))
1388           || branch_offset > BSR_MAX_FWD_BRANCH_OFFSET
1389           || branch_offset < BSR_MAX_BWD_BRANCH_OFFSET))
1390     {
1391       if (bfd_csky_arch (info->output_bfd) == CSKY_ARCH_810
1392           || bfd_csky_arch (info->output_bfd) ==  CSKY_ARCH_807)
1393         stub_type = csky_stub_long_branch_jmpi;
1394       else
1395         stub_type = csky_stub_long_branch;
1396     }
1397
1398   return stub_type;
1399 }
1400
1401 /* Create an entry in an C-SKY ELF linker hash table.  */
1402
1403 static struct bfd_hash_entry *
1404 csky_elf_link_hash_newfunc (struct bfd_hash_entry * entry,
1405                             struct bfd_hash_table * table,
1406                             const char * string)
1407 {
1408   struct csky_elf_link_hash_entry * ret =
1409       (struct csky_elf_link_hash_entry *) entry;
1410
1411   /* Allocate the structure if it has not already been allocated by a
1412      subclass.  */
1413   if (ret == NULL)
1414     {
1415       ret = (struct csky_elf_link_hash_entry *)
1416         bfd_hash_allocate (table,
1417                            sizeof (struct csky_elf_link_hash_entry));
1418       if (ret == NULL)
1419         return (struct bfd_hash_entry *) ret;
1420     }
1421
1422   /* Call the allocation method of the superclass.  */
1423   ret = ((struct csky_elf_link_hash_entry *)
1424          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret,
1425                                      table, string));
1426   if (ret != NULL)
1427     {
1428       struct csky_elf_link_hash_entry *eh;
1429
1430       eh = (struct csky_elf_link_hash_entry *) ret;
1431       eh->dyn_relocs = NULL;
1432       eh->plt_refcount = 0;
1433       eh->jsri2bsr_refcount = 0;
1434       eh->tls_type = GOT_NORMAL;
1435       ret->stub_cache = NULL;
1436     }
1437
1438   return (struct bfd_hash_entry *) ret;
1439 }
1440
1441 /* Initialize an entry in the stub hash table.  */
1442
1443 static struct bfd_hash_entry *
1444 stub_hash_newfunc (struct bfd_hash_entry *entry,
1445                    struct bfd_hash_table *table,
1446                    const char *string)
1447 {
1448   /* Allocate the structure if it has not already been allocated by a
1449      subclass.  */
1450   if (entry == NULL)
1451     {
1452       entry = ((struct bfd_hash_entry *)
1453                bfd_hash_allocate (table,
1454                                   sizeof (struct elf32_csky_stub_hash_entry)));
1455       if (entry == NULL)
1456         return entry;
1457     }
1458
1459   /* Call the allocation method of the superclass.  */
1460   entry = bfd_hash_newfunc (entry, table, string);
1461   if (entry != NULL)
1462     {
1463       struct elf32_csky_stub_hash_entry *eh;
1464
1465       /* Initialize the local fields.  */
1466       eh = (struct elf32_csky_stub_hash_entry *) entry;
1467       eh->stub_sec = NULL;
1468       eh->stub_offset = 0;
1469       eh->target_value = 0;
1470       eh->target_section = NULL;
1471       eh->target_addend = 0;
1472       eh->stub_type = csky_stub_none;
1473       eh->stub_size = 0;
1474       eh->stub_template = NULL;
1475       eh->stub_template_size = -1;
1476       eh->h = NULL;
1477       eh->id_sec = NULL;
1478       eh->output_name = NULL;
1479     }
1480
1481   return entry;
1482 }
1483
1484 /* Free the derived linker hash table.  */
1485
1486 static void
1487 csky_elf_link_hash_table_free (bfd *obfd)
1488 {
1489   struct csky_elf_link_hash_table *ret
1490     = (struct csky_elf_link_hash_table *) obfd->link.hash;
1491
1492   bfd_hash_table_free (&ret->stub_hash_table);
1493   _bfd_elf_link_hash_table_free (obfd);
1494 }
1495
1496 /* Create an CSKY elf linker hash table.  */
1497
1498 static struct bfd_link_hash_table *
1499 csky_elf_link_hash_table_create (bfd *abfd)
1500 {
1501   struct csky_elf_link_hash_table *ret;
1502   bfd_size_type amt = sizeof (struct csky_elf_link_hash_table);
1503
1504   ret = (struct csky_elf_link_hash_table*) bfd_zmalloc (amt);
1505   if (ret == NULL)
1506     return NULL;
1507
1508   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1509                                       csky_elf_link_hash_newfunc,
1510                                       sizeof (struct csky_elf_link_hash_entry),
1511                                       CSKY_ELF_DATA))
1512     {
1513       free (ret);
1514       return NULL;
1515     }
1516
1517   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
1518                             sizeof (struct elf32_csky_stub_hash_entry)))
1519     {
1520       free (ret);
1521       return NULL;
1522     }
1523   ret->elf.root.hash_table_free = csky_elf_link_hash_table_free;
1524   return &ret->elf.root;
1525 }
1526
1527 static bfd_boolean
1528 csky_elf_mkobject (bfd *abfd)
1529 {
1530   return bfd_elf_allocate_object (abfd, sizeof (struct csky_elf_obj_tdata),
1531                                   CSKY_ELF_DATA);
1532 }
1533
1534 /* Adjust a symbol defined by a dynamic object and referenced by a
1535    regular object.  The current definition is in some section of the
1536    dynamic object, but we're not including those sections.  We have to
1537    change the definition to something the rest of the link can
1538    understand.  */
1539
1540 static bfd_boolean
1541 csky_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1542                                 struct elf_link_hash_entry *h)
1543 {
1544   struct csky_elf_link_hash_entry *eh;
1545   struct csky_elf_link_hash_table *htab;
1546   asection *srel;
1547   asection *s;
1548   eh = (struct csky_elf_link_hash_entry *)h;
1549   if (eh == NULL)
1550     return FALSE;
1551
1552   htab = csky_elf_hash_table (info);
1553   if (htab == NULL)
1554     return FALSE;
1555
1556   /* Clear jsri2bsr_refcount, if creating shared library files.  */
1557   if (bfd_link_pic (info) && eh->jsri2bsr_refcount > 0)
1558     eh->jsri2bsr_refcount = 0;
1559
1560   /* If there is a function, put it in the procedure linkage table. We
1561      will fill in the contents of the procedure linkage table later.  */
1562   if (h->needs_plt)
1563     {
1564       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
1565          symbol binds locally.  */
1566       if (h->plt.refcount <= 0
1567           || (h->type != STT_GNU_IFUNC
1568               && (SYMBOL_CALLS_LOCAL (info, h)
1569                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1570                       && h->root.type == bfd_link_hash_undefweak))))
1571
1572         {
1573           /* This case can occur if we saw a PLT32 reloc in an input
1574              file, but the symbol was never referred to by a dynamic
1575              object, or if all references were garbage collected.  In
1576              such a case, we don't actually need to build a procedure
1577              linkage table, and we can just do a PC32 reloc instead.  */
1578           h->plt.offset = (bfd_vma) -1;
1579           h->needs_plt = 0;
1580           if (h->got.refcount == 0)
1581             h->got.refcount += 1;
1582         }
1583       else if (h->got.refcount != 0)
1584         {
1585           h->got.refcount -= eh->plt_refcount;
1586           eh->plt_refcount = 0;
1587         }
1588       return TRUE;
1589     }
1590   else
1591     /* It's possible that we incorrectly decided a .plt reloc was
1592        needed for an R_CKCORE_PC32 or similar reloc to a non-function
1593        sym in check_relocs.  We can't decide accurately between function
1594        and non-function syms in check_relocs; objects loaded later in
1595        the link may change h->type.  So fix it now.  */
1596     h->plt.offset = (bfd_vma) -1;
1597
1598   /* If this is a weak symbol, and there is a real definition, the
1599      processor independent code will have arranged for us to see the
1600      real definition first, and we can just use the same value.  */
1601   if (h->is_weakalias)
1602     {
1603       struct elf_link_hash_entry *def = weakdef (h);
1604       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1605       h->root.u.def.section = def->root.u.def.section;
1606       h->root.u.def.value = def->root.u.def.value;
1607       return TRUE;
1608     }
1609
1610   /* If there are no non-GOT references, we do not need a copy
1611      relocation.  */
1612   if (!h->non_got_ref)
1613     return TRUE;
1614
1615   /* This is a reference to a symbol defined by a dynamic object which
1616      is not a function.  */
1617
1618   /* If we are creating a shared library, we must presume that the
1619      only references to the symbol are via the global offset table.
1620      For such cases we need not do anything here; the relocations will
1621      be handled correctly by relocate_section.  */
1622   if (bfd_link_pic (info) || htab->elf.is_relocatable_executable)
1623     return TRUE;
1624
1625   /* We must allocate the symbol in our .dynbss section, which will
1626      become part of the .bss section of the executable.  There will be
1627      an entry for this symbol in the .dynsym section.  The dynamic
1628      object will contain position independent code, so all references
1629      from the dynamic object to this symbol will go through the global
1630      offset table.  The dynamic linker will use the .dynsym entry to
1631      determine the address it must put in the global offset table, so
1632      both the dynamic object and the regular object will refer to the
1633      same memory location for the variable.  */
1634   /* We must generate a R_CKCORE_COPY reloc to tell the dynamic linker to
1635      copy the initial value out of the dynamic object and into the
1636      runtime process image.  We need to remember the offset into the
1637      .rela.bss section we are going to use.  */
1638   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1639     {
1640       s = htab->elf.sdynrelro;
1641       srel = htab->elf.sreldynrelro;
1642     }
1643   else
1644     {
1645       s = htab->elf.sdynbss;
1646       srel = htab->elf.srelbss;
1647     }
1648   if (info->nocopyreloc == 0
1649       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1650       && h->size != 0
1651       && srel != NULL
1652       && s != NULL)
1653     {
1654       srel->size += sizeof (Elf32_External_Rela);
1655       h->needs_copy = 1;
1656       return _bfd_elf_adjust_dynamic_copy (info, h, s);
1657     }
1658
1659   h->non_got_ref = 0;
1660   return TRUE;
1661 }
1662
1663 /* Allocate space in .plt, .got and associated reloc sections for
1664    dynamic relocs.  */
1665
1666 static bfd_boolean
1667 csky_allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1668 {
1669   struct bfd_link_info *info;
1670   struct csky_elf_link_hash_table *htab;
1671   struct csky_elf_link_hash_entry *eh;
1672   struct elf_dyn_relocs *p;
1673
1674   /* For indirect case, such as _ZdlPv to _ZdlPv@@GLIBCXX_3.4.  */
1675   if (h->root.type == bfd_link_hash_indirect)
1676     return TRUE;
1677
1678   if (h->root.type == bfd_link_hash_warning)
1679     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1680
1681
1682   info = (struct bfd_link_info *) inf;
1683   htab = csky_elf_hash_table (info);
1684   if (htab == NULL)
1685     return FALSE;
1686   /*TODO: how to deal with weak symbol relocs.  */
1687   if ((htab->elf.dynamic_sections_created || h->type == STT_GNU_IFUNC)
1688       && h->plt.refcount > 0)
1689     {
1690       /* Make sure this symbol is output as a dynamic symbol.
1691          Undefined weak syms won't yet be marked as dynamic.  */
1692       if (h->dynindx == -1 && !h->forced_local
1693           && h->root.type == bfd_link_hash_undefweak
1694           && ! bfd_elf_link_record_dynamic_symbol (info, h))
1695         return FALSE;
1696       if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1697         {
1698           asection *splt = htab->elf.splt;
1699
1700           /* If this is the first .plt entry, make room for the special
1701              first entry.  */
1702           if (splt->size == 0)
1703             {
1704               if (bfd_csky_abi (info->output_bfd) ==  CSKY_ABI_V1)
1705                 splt->size += PLT_ENTRY_SIZE_P;
1706               else
1707                 splt->size += PLT_ENTRY_SIZE;
1708             }
1709           h->plt.offset = splt->size;
1710
1711           /* If this symbol is not defined in a regular file, and we are
1712              not generating a shared library, then set the symbol to this
1713              location in the .plt.  This is required to make function
1714              pointers compare as equal between the normal executable and
1715              the shared library.  */
1716           if (!bfd_link_pic (info) && !h->def_regular)
1717             {
1718               h->root.u.def.section = splt;
1719               h->root.u.def.value = h->plt.offset;
1720             }
1721
1722           /* Make room for this entry.  */
1723           if (bfd_csky_abi (info->output_bfd) ==  CSKY_ABI_V1)
1724             splt->size += PLT_ENTRY_SIZE_P;
1725           else
1726             splt->size += PLT_ENTRY_SIZE;
1727           /* We also need to make an entry in the .rela.plt section.  */
1728           htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
1729
1730           /* We also need to make an entry in the .got.plt section, which
1731              will be placed in the .got section by the linker script.  */
1732           htab->elf.sgotplt->size += 4;
1733         }
1734       else
1735         {
1736           h->plt.offset = (bfd_vma) -1;
1737           h->needs_plt = 0;
1738         }
1739     }
1740   else
1741     {
1742       h->plt.offset = (bfd_vma) -1;
1743       h->needs_plt = 0;
1744     }
1745
1746   if (h->got.refcount > 0)
1747     {
1748       asection *sgot;
1749       bfd_boolean dyn;
1750       int indx;
1751
1752       int tls_type = csky_elf_hash_entry (h)->tls_type;
1753       /* Make sure this symbol is output as a dynamic symbol.
1754          Undefined weak syms won't yet be marked as dynamic.  */
1755       if (h->dynindx == -1 && !h->forced_local
1756           && h->root.type == bfd_link_hash_undefweak
1757           && ! bfd_elf_link_record_dynamic_symbol (info, h))
1758         return FALSE;
1759
1760       sgot = htab->elf.sgot;
1761       h->got.offset = sgot->size;
1762       BFD_ASSERT (tls_type != GOT_UNKNOWN);
1763       if (tls_type == GOT_NORMAL)
1764         /* Non-TLS symbols need one GOT slot.  */
1765         sgot->size += 4;
1766       else
1767         {
1768           if (tls_type & GOT_TLS_GD)
1769             /* R_CKCORE_TLS_GD32 needs 2 consecutive GOT slots.  */
1770             sgot->size += 8;
1771           if (tls_type & GOT_TLS_IE)
1772             /* R_CKCORE_TLS_IE32 needs one GOT slot.  */
1773             sgot->size += 4;
1774         }
1775       dyn = htab->elf.dynamic_sections_created;
1776       indx = 0;
1777       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1778           && (! bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
1779         indx = h->dynindx;
1780
1781       if (tls_type != GOT_NORMAL
1782           && (bfd_link_pic (info) || indx != 0)
1783           && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1784                && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1785               || h->root.type != bfd_link_hash_undefweak))
1786         {
1787           if (tls_type & GOT_TLS_IE)
1788             htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1789           if (tls_type & GOT_TLS_GD)
1790             htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1791           if ((tls_type & GOT_TLS_GD) && indx != 0)
1792             htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1793         }
1794       else if (((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1795                  && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1796                 || h->root.type != bfd_link_hash_undefweak)
1797                && (bfd_link_pic (info)
1798                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
1799                    || h->plt.offset == (bfd_vma) -1))
1800         htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1801     }
1802   else
1803     h->got.offset = (bfd_vma) -1;
1804
1805   eh = (struct csky_elf_link_hash_entry *) h;
1806   if (eh->dyn_relocs == NULL)
1807     return TRUE;
1808
1809   /* In the shared -Bsymbolic case, discard space allocated for
1810      dynamic pc-relative relocs against symbols which turn out to be
1811      defined in regular objects.  For the normal shared case, discard
1812      space for pc-relative relocs that have become local due to symbol
1813      visibility changes.  */
1814
1815   if (bfd_link_pic (info))
1816     {
1817       if (SYMBOL_CALLS_LOCAL (info, h))
1818         {
1819           struct elf_dyn_relocs **pp;
1820
1821           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1822             {
1823               p->count -= p->pc_count;
1824               p->pc_count = 0;
1825               if (p->count == 0)
1826                 *pp = p->next;
1827               else
1828                 pp = &p->next;
1829             }
1830         }
1831
1832       if (eh->jsri2bsr_refcount
1833           && h->root.type == bfd_link_hash_defined
1834           && eh->dyn_relocs != NULL)
1835         eh->dyn_relocs->count -= eh->jsri2bsr_refcount;
1836
1837       /* Also discard relocs on undefined weak syms with non-default
1838          visibility.  */
1839       if (eh->dyn_relocs != NULL
1840           && h->root.type == bfd_link_hash_undefweak)
1841         {
1842           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1843               || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1844             eh->dyn_relocs = NULL;
1845
1846           /* Make sure undefined weak symbols are output as a dynamic
1847              symbol in PIEs.  */
1848           else if (h->dynindx == -1
1849                    && !h->forced_local
1850                    && !bfd_elf_link_record_dynamic_symbol (info, h))
1851             return FALSE;
1852         }
1853
1854     }
1855   else
1856     {
1857       /* For the non-shared case, discard space for relocs against
1858          symbols which turn out to need copy relocs or are not
1859          dynamic.  */
1860
1861       if (!h->non_got_ref
1862           && ((h->def_dynamic && !h->def_regular)
1863               || (htab->elf.dynamic_sections_created
1864                   && (h->root.type == bfd_link_hash_undefweak
1865                       || h->root.type == bfd_link_hash_indirect
1866                       || h->root.type == bfd_link_hash_undefined))))
1867         {
1868           /* Make sure this symbol is output as a dynamic symbol.
1869              Undefined weak syms won't yet be marked as dynamic.  */
1870           if (h->dynindx == -1 && !h->forced_local
1871               && h->root.type == bfd_link_hash_undefweak)
1872             {
1873               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1874                 return FALSE;
1875             }
1876
1877           /* If that succeeded, we know we'll be keeping all the
1878              relocs.  */
1879           if (h->dynindx != -1)
1880             goto keep;
1881         }
1882
1883       eh->dyn_relocs = NULL;
1884
1885       keep: ;
1886     }
1887
1888   /* Finally, allocate space.  */
1889   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1890     {
1891       asection *srelgot = htab->elf.srelgot;
1892       srelgot->size += p->count * sizeof (Elf32_External_Rela);
1893     }
1894
1895   return TRUE;
1896 }
1897
1898 static asection *
1899 readonly_dynrelocs (struct elf_link_hash_entry *h)
1900 {
1901   struct elf_dyn_relocs *p;
1902
1903   for (p = csky_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
1904     {
1905       asection *s = p->sec->output_section;
1906
1907       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1908         return p->sec;
1909     }
1910   return NULL;
1911 }
1912
1913 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1914    read-only sections.  */
1915
1916 static bfd_boolean
1917 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
1918 {
1919   asection *sec;
1920
1921   if (h->root.type == bfd_link_hash_indirect)
1922     return TRUE;
1923
1924   sec = readonly_dynrelocs (h);
1925   if (sec != NULL)
1926     {
1927       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
1928
1929       info->flags |= DF_TEXTREL;
1930       info->callbacks->minfo
1931         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1932          sec->owner, h->root.root.string, sec);
1933
1934       /* Not an error, just cut short the traversal.  */
1935       return FALSE;
1936     }
1937   return TRUE;
1938 }
1939
1940 /* Set the sizes of the dynamic sections.  */
1941
1942 static bfd_boolean
1943 csky_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1944                                 struct bfd_link_info *info)
1945 {
1946   struct csky_elf_link_hash_table *htab;
1947   bfd *dynobj;
1948   asection *s;
1949   bfd_boolean relocs;
1950   bfd *ibfd;
1951
1952   htab = csky_elf_hash_table (info);
1953   if (htab == NULL)
1954     return FALSE;
1955   dynobj = htab->elf.dynobj;
1956   if (dynobj == NULL)
1957     return FALSE;
1958
1959   if (htab->elf.dynamic_sections_created)
1960     {
1961       /* Set the contents of the .interp section to the interpreter.  */
1962       if (!bfd_link_pic (info) && !info->nointerp)
1963         {
1964           s = bfd_get_section_by_name (dynobj, ".interp");
1965           BFD_ASSERT (s != NULL);
1966           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1967           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1968         }
1969     }
1970
1971   /* Set up .got offsets for local syms, and space for local dynamic
1972      relocs.  */
1973   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1974     {
1975       bfd_signed_vma *local_got_refcounts;
1976       bfd_signed_vma *end_local_got;
1977       bfd_size_type locsymcount;
1978       Elf_Internal_Shdr *symtab_hdr;
1979       asection *srelgot, *sgot;
1980       char *local_tls_type;
1981
1982       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1983         continue;
1984
1985       sgot = htab->elf.sgot;
1986       srelgot = htab->elf.srelgot;
1987
1988       for (s = ibfd->sections; s != NULL; s = s->next)
1989         {
1990           struct elf_dyn_relocs *p;
1991
1992           for (p = *((struct elf_dyn_relocs **)
1993                      &elf_section_data (s)->local_dynrel);
1994                p != NULL;
1995                p = p->next)
1996             {
1997               if (!bfd_is_abs_section (p->sec)
1998                   && bfd_is_abs_section (p->sec->output_section))
1999                 /* Input section has been discarded, either because
2000                    it is a copy of a linkonce section or due to
2001                    linker script /DISCARD/, so we'll be discarding
2002                    the relocs too.  */
2003                 ;
2004               else if (p->count != 0)
2005                 {
2006                   srelgot->size += p->count * sizeof (Elf32_External_Rela);
2007                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2008                     info->flags |= DF_TEXTREL;
2009                 }
2010             }
2011         }
2012
2013       local_got_refcounts = elf_local_got_refcounts (ibfd);
2014       if (!local_got_refcounts)
2015         continue;
2016
2017       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2018       locsymcount = symtab_hdr->sh_info;
2019       end_local_got = local_got_refcounts + locsymcount;
2020       local_tls_type = csky_elf_local_got_tls_type (ibfd);
2021
2022       for (; local_got_refcounts < end_local_got;
2023            ++local_got_refcounts, ++local_tls_type)
2024         {
2025           if (*local_got_refcounts > 0)
2026             {
2027               /* GOT_TLS_GD and GOT_TLS_IE type for TLS, GOT_NORMAL type
2028                  for GOT.  If output file is shared library, we should output
2029                  GOT_TLS_GD type relocation in .rel.got.  */
2030               *local_got_refcounts = sgot->size;
2031               if (*local_tls_type & GOT_TLS_GD)
2032                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
2033                 sgot->size += 8;
2034               if (*local_tls_type & GOT_TLS_IE)
2035                 sgot->size += 4;
2036               if (*local_tls_type == GOT_NORMAL)
2037                 sgot->size += 4;
2038               if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
2039                 srelgot->size += sizeof (Elf32_External_Rela);
2040             }
2041           else
2042             *local_got_refcounts = (bfd_vma) -1;
2043         }
2044     }
2045
2046   if (htab->tls_ldm_got.refcount > 0)
2047     {
2048       /* Allocate two GOT entries and one dynamic relocation (if necessary)
2049          for R_CSKY_TLS_LDM32 relocations.  */
2050       htab->tls_ldm_got.offset = htab->elf.sgot->size;
2051       htab->elf.sgot->size += 8;
2052       if (bfd_link_pic (info))
2053         htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
2054     }
2055   else
2056     htab->tls_ldm_got.offset = -1;
2057
2058   /* Allocate global sym .plt and .got entries, and space for global
2059      sym dynamic relocs.  */
2060   elf_link_hash_traverse (&htab->elf, csky_allocate_dynrelocs, (PTR) info);
2061
2062   /* Check for GOT overflow.  */
2063   if (check_got_overflow == 1
2064       && htab->elf.sgot->size + htab->elf.sgotplt->size > GOT_MAX_SIZE)
2065     {
2066       _bfd_error_handler (_("GOT table size out of range")); /*  */
2067       return FALSE;
2068     }
2069
2070   /* We now have determined the sizes of the various dynamic sections.
2071      Allocate memory for them.  */
2072   relocs = FALSE;
2073   for (s = dynobj->sections; s != NULL; s = s->next)
2074     {
2075       bfd_boolean strip_section = TRUE;
2076
2077       if ((s->flags & SEC_LINKER_CREATED) == 0)
2078         continue;
2079
2080       if (s == htab->elf.splt
2081           || s == htab->elf.sgot
2082           || s == htab->elf.sgotplt
2083           || s == htab->elf.sdynrelro
2084           || s == htab->elf.sreldynrelro)
2085         {
2086           /* Strip this section if we don't need it;
2087              see the comment below.  */
2088           /* We'd like to strip these sections if they aren't needed, but if
2089              we've exported dynamic symbols from them we must leave them.
2090              It's too late to tell BFD to get rid of the symbols.  */
2091
2092           if (htab->elf.hplt != NULL)
2093             strip_section = FALSE;
2094         }
2095       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel") )
2096         {
2097           if (s->size != 0 )
2098             relocs = TRUE;
2099
2100           /* We use the reloc_count field as a counter if we need
2101              to copy relocs into the output file.  */
2102           s->reloc_count = 0;
2103         }
2104       else
2105         /* It's not one of our sections, so don't allocate space.  */
2106         continue;
2107
2108       /* Strip this section if we don't need it; see the
2109          comment below.  */
2110       if (s->size == 0)
2111         {
2112           /* If we don't need this section, strip it from the
2113              output file.  This is mostly to handle .rel.bss and
2114              .rel.plt.  We must create both sections in
2115              create_dynamic_sections, because they must be created
2116              before the linker maps input sections to output
2117              sections.  The linker does that before
2118              adjust_dynamic_symbol is called, and it is that
2119              function which decides whether anything needs to go
2120              into these sections.  */
2121           if (strip_section)
2122             s->flags |= SEC_EXCLUDE;
2123           continue;
2124         }
2125
2126       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2127         continue;
2128
2129       /* Allocate memory for the section contents.  We use bfd_zalloc
2130          here in case unused entries are not reclaimed before the
2131          section's contents are written out.  This should not happen,
2132          but this way if it does, we get a R_CKCORE_NONE reloc instead
2133          of garbage.  */
2134       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2135       if (s->contents == NULL)
2136         return FALSE;
2137     }
2138
2139   if (htab->elf.dynamic_sections_created)
2140     {
2141       /* Add some entries to the .dynamic section.  We fill in the
2142          values later, in csky_elf_finish_dynamic_sections, but we
2143          must add the entries now so that we get the correct size for
2144          the .dynamic section.  The DT_DEBUG entry is filled in by the
2145          dynamic linker and used by the debugger.  */
2146 #define add_dynamic_entry(TAG, VAL) \
2147   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2148
2149       if (bfd_link_executable (info) && !add_dynamic_entry (DT_DEBUG, 0))
2150         return FALSE;
2151
2152       if (htab->elf.sgot->size != 0 || htab->elf.splt->size)
2153         {
2154           if (!add_dynamic_entry (DT_PLTGOT, 0)
2155               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2156               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2157               || !add_dynamic_entry (DT_JMPREL, 0))
2158             return FALSE;
2159         }
2160
2161       if (relocs)
2162         {
2163           if (!add_dynamic_entry (DT_RELA, 0)
2164               || !add_dynamic_entry (DT_RELASZ, 0)
2165               || !add_dynamic_entry (DT_RELAENT,
2166                                      sizeof (Elf32_External_Rela)))
2167             return FALSE;
2168
2169           /* If any dynamic relocs apply to a read-only section,
2170              then we need a DT_TEXTREL entry.  */
2171           if ((info->flags & DF_TEXTREL) == 0)
2172             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
2173
2174           if ((info->flags & DF_TEXTREL) != 0
2175               && !add_dynamic_entry (DT_TEXTREL, 0))
2176             return FALSE;
2177         }
2178     }
2179 #undef add_dynamic_entry
2180
2181   return TRUE;
2182 }
2183
2184 /* Finish up dynamic symbol handling.  We set the contents of various
2185    dynamic sections here.  */
2186
2187 static bfd_boolean
2188 csky_elf_finish_dynamic_symbol (bfd *output_bfd,
2189                                 struct bfd_link_info *info,
2190                                 struct elf_link_hash_entry *h,
2191                                 Elf_Internal_Sym *sym)
2192 {
2193   struct csky_elf_link_hash_table *htab;
2194
2195   htab = csky_elf_hash_table (info);
2196   if (htab == NULL)
2197     return FALSE;
2198
2199   /* Sanity check to make sure no unexpected symbol reaches here.
2200      This matches the test in csky_elf_relocate_section handling
2201      of GOT/PLT entries.  */
2202   BFD_ASSERT (! (h->dynindx == -1
2203                  && !h->forced_local
2204                  && h->root.type != bfd_link_hash_undefweak
2205                  && bfd_link_pic (info)));
2206
2207   if (h->plt.offset != (bfd_vma) -1)
2208     {
2209       bfd_vma plt_index;
2210       bfd_vma got_offset;
2211       Elf_Internal_Rela rel;
2212       bfd_byte *loc;
2213       asection *plt, *relplt, *gotplt;
2214
2215       plt = htab->elf.splt;
2216       relplt = htab->elf.srelplt;
2217       gotplt = htab->elf.sgotplt;
2218
2219       /* This symbol has an entry in the procedure linkage table.  Set
2220          it up.  */
2221       BFD_ASSERT (h->dynindx != -1
2222                   || ((h->forced_local || bfd_link_executable (info))
2223                       && h->def_regular));
2224       BFD_ASSERT (plt != NULL && gotplt != NULL && relplt != NULL);
2225       if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2226         plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2227       else
2228         plt_index = h->plt.offset / PLT_ENTRY_SIZE_P - 1;
2229       got_offset = (plt_index + 3) * 4;
2230
2231       /* Fill in the entry in the procedure linkage table.  */
2232       if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2233         {
2234           csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[0],
2235                             plt->contents + h->plt.offset);
2236           csky_put_insn_32 (output_bfd,
2237                             (csky_elf_plt_entry_v2[1] | plt_index),
2238                             plt->contents + h->plt.offset + 4);
2239           csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[2],
2240                             plt->contents + h->plt.offset + 8);
2241         }
2242       else
2243         {
2244           int i;
2245           for (i = 0; i < 6; i++)
2246             bfd_put_16 (output_bfd, csky_elf_plt_entry_v1[i],
2247                         plt->contents + h->plt.offset + i * 2);
2248           bfd_put_32 (output_bfd, plt_index,
2249                       plt->contents + h->plt.offset + i * 2);
2250         }
2251
2252       /* Fill in the entry in the .rel.plt section.  */
2253       rel.r_offset = (htab->elf.sgotplt->output_section->vma
2254                       + htab->elf.sgotplt->output_offset
2255                       + got_offset);
2256       rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_JUMP_SLOT);
2257       rel.r_addend = (plt->output_section->vma
2258                       + plt->output_offset
2259                       + h->plt.offset);
2260       loc = (htab->elf.srelplt->contents
2261              + plt_index * sizeof (Elf32_External_Rela));
2262
2263       if (loc != NULL)
2264         bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2265       if (! h->def_regular)
2266         {
2267           /* Mark the symbol as undefined, rather than as defined in
2268              the .plt section.  Leave the value alone.  */
2269           sym->st_shndx = SHN_UNDEF;
2270           /* If the symbol is weak, we do need to clear the value.
2271              Otherwise, the PLT entry would provide a definition for
2272              the symbol even if the symbol wasn't defined anywhere,
2273              and so the symbol would never be NULL. Leave the value if
2274              there were any relocations where pointer equality matters
2275              (this is a clue for the dynamic linker, to make function
2276              pointer comparisons work between an application and shared
2277              library).  */
2278           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
2279             sym->st_value = 0;
2280         }
2281     }
2282
2283   /* Fill in the entry in the .got section.  */
2284   if (h->got.offset != (bfd_vma) -1
2285       && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_GD) == 0)
2286       && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_IE) == 0))
2287     {
2288       Elf_Internal_Rela rel;
2289       bfd_byte *loc;
2290
2291       /* This symbol has an entry in the global offset table.
2292          Set it up.  */
2293       BFD_ASSERT (htab->elf.sgot != NULL && htab->elf.srelgot != NULL);
2294
2295       rel.r_offset = (htab->elf.sgot->output_section->vma
2296                       + htab->elf.sgot->output_offset
2297                       + (h->got.offset & ~(bfd_vma) 1));
2298
2299       /* If this is a static link, or it is a -Bsymbolic link and the
2300          symbol is defined locally or was forced to be local because
2301          of a version file, we just want to emit a RELATIVE reloc.
2302          The entry in the global offset table will already have been
2303          initialized in the relocate_section function.  */
2304       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
2305         {
2306           BFD_ASSERT ((h->got.offset & 1) != 0);
2307           rel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
2308           rel.r_addend = (h->root.u.def.value
2309                           + h->root.u.def.section->output_offset
2310                           + h->root.u.def.section->output_section->vma);
2311         }
2312       else
2313         {
2314           BFD_ASSERT ((h->got.offset & 1) == 0);
2315           bfd_put_32 (output_bfd, (bfd_vma) 0,
2316                       htab->elf.sgot->contents + h->got.offset);
2317           rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_GLOB_DAT);
2318           rel.r_addend = 0;
2319         }
2320
2321       loc = htab->elf.srelgot->contents;
2322       loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2323
2324       if (loc != NULL)
2325         bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2326     }
2327
2328   if (h->needs_copy)
2329     {
2330       asection *s;
2331       Elf_Internal_Rela rela;
2332       bfd_byte *loc;
2333
2334       /* This symbol needs a copy reloc.  Set it up.  */
2335       BFD_ASSERT (h->dynindx != -1
2336                   && (h->root.type == bfd_link_hash_defined
2337                       || h->root.type == bfd_link_hash_defweak));
2338
2339       rela.r_offset = (h->root.u.def.value
2340                        + h->root.u.def.section->output_section->vma
2341                        + h->root.u.def.section->output_offset);
2342       rela.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_COPY);
2343       rela.r_addend = 0;
2344       if (h->root.u.def.section == htab->elf.sdynrelro)
2345         s = htab->elf.sreldynrelro;
2346       else
2347         s = htab->elf.srelbss;
2348       BFD_ASSERT (s != NULL);
2349       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2350       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2351     }
2352
2353   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2354   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2355       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2356     sym->st_shndx = SHN_ABS;
2357
2358   return TRUE;
2359 }
2360
2361 /* Finish up the dynamic sections.  */
2362
2363 static bfd_boolean
2364 csky_elf_finish_dynamic_sections (bfd *output_bfd,
2365                                   struct bfd_link_info *info)
2366 {
2367   struct csky_elf_link_hash_table *htab;
2368   bfd *dynobj;
2369   asection *sdyn;
2370   asection *got_sec;
2371
2372   htab = csky_elf_hash_table (info);
2373   if (htab == NULL)
2374     return FALSE;
2375
2376   dynobj = htab->elf.dynobj;
2377   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2378
2379   if (htab->elf.dynamic_sections_created)
2380     {
2381       Elf32_External_Dyn *dyncon, *dynconend;
2382
2383       BFD_ASSERT (sdyn != NULL && htab->elf.sgot != NULL);
2384
2385       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2386       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2387       for (; dyncon < dynconend; dyncon++)
2388         {
2389           Elf_Internal_Dyn dyn;
2390           bfd_boolean size = FALSE;
2391           const char *name = NULL;
2392
2393           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2394           switch (dyn.d_tag)
2395             {
2396             default:
2397               continue;
2398             case DT_RELA:
2399               name = ".rela.dyn";
2400               size = FALSE;
2401               break;
2402             case DT_RELASZ:
2403               name = ".rela.dyn";
2404               size = TRUE;
2405               break;
2406             case DT_PLTRELSZ:
2407               name = ".rela.plt";
2408               size = TRUE;
2409               break;
2410             case DT_PLTGOT:
2411               dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
2412               break;
2413             case DT_JMPREL:
2414               dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma
2415                                + htab->elf.srelplt->output_offset;
2416               break;
2417             }
2418
2419           if (name != NULL)
2420             {
2421               asection *s = bfd_get_section_by_name (output_bfd, name);
2422
2423               if (s == NULL)
2424                 dyn.d_un.d_val = 0;
2425               else if (!size)
2426                 dyn.d_un.d_ptr = s->vma;
2427               else
2428                 dyn.d_un.d_val = s->size;
2429             }
2430           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2431         }
2432     }
2433
2434   /* Fill in the first three entries in the global offset table.  */
2435   if (htab->elf.sgotplt)
2436     got_sec = htab->elf.sgotplt;
2437   else
2438     got_sec = htab->elf.sgot;
2439   if (got_sec != NULL)
2440     {
2441       if (got_sec->size > 0)
2442         {
2443           bfd_put_32 (output_bfd,
2444                       (sdyn == NULL ? (bfd_vma) 0
2445                        : sdyn->output_section->vma + sdyn->output_offset),
2446                       got_sec->contents);
2447           bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 4);
2448           bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 8);
2449         }
2450       elf_section_data (got_sec->output_section)->this_hdr.sh_entsize = 4;
2451     }
2452   return TRUE;
2453 }
2454
2455 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2456
2457 static void
2458 csky_elf_copy_indirect_symbol (struct bfd_link_info *info,
2459                                struct elf_link_hash_entry *dir,
2460                                struct elf_link_hash_entry *ind)
2461 {
2462   struct csky_elf_link_hash_entry *edir, *eind;
2463
2464   edir = (struct csky_elf_link_hash_entry *) dir;
2465   eind = (struct csky_elf_link_hash_entry *) ind;
2466
2467   if (eind->dyn_relocs != NULL)
2468     {
2469       if (edir->dyn_relocs != NULL)
2470         {
2471           struct elf_dyn_relocs **pp;
2472           struct elf_dyn_relocs *p;
2473
2474           /* Add reloc counts against the indirect sym to the direct sym
2475              list.  Merge any entries against the same section.  */
2476           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2477             {
2478               struct elf_dyn_relocs *q;
2479
2480               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2481                 if (q->sec == p->sec)
2482                   {
2483                     q->pc_count += p->pc_count;
2484                     q->count += p->count;
2485                     *pp = p->next;
2486                     break;
2487                   }
2488               if (q == NULL)
2489                 pp = &p->next;
2490             }
2491           *pp = edir->dyn_relocs;
2492         }
2493       edir->dyn_relocs = eind->dyn_relocs;
2494       eind->dyn_relocs = NULL;
2495     }
2496   if (ind->root.type == bfd_link_hash_indirect
2497       && dir->got.refcount <= 0)
2498     {
2499       edir->tls_type = eind->tls_type;
2500       eind->tls_type = GOT_UNKNOWN;
2501     }
2502   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2503 }
2504
2505 /* Used to decide how to sort relocs in an optimal manner for the
2506    dynamic linker, before writing them out.  */
2507
2508 static enum elf_reloc_type_class
2509 csky_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2510                            const asection *rel_sec ATTRIBUTE_UNUSED,
2511                            const Elf_Internal_Rela *rela)
2512 {
2513   switch ((int) ELF32_R_TYPE (rela->r_info))
2514     {
2515     case R_CKCORE_RELATIVE:
2516       return reloc_class_relative;
2517     case R_CKCORE_JUMP_SLOT:
2518       return reloc_class_plt;
2519     case R_CKCORE_COPY:
2520       return reloc_class_copy;
2521     case R_CKCORE_IRELATIVE:
2522       return reloc_class_ifunc;
2523     default:
2524       return reloc_class_normal;
2525     }
2526 }
2527
2528 /* Return the section that should be marked against GC for a given
2529    relocation.  */
2530
2531 static asection *
2532 csky_elf_gc_mark_hook (asection *sec,
2533                        struct bfd_link_info *info,
2534                        Elf_Internal_Rela *rel,
2535                        struct elf_link_hash_entry *h,
2536                        Elf_Internal_Sym *sym)
2537 {
2538   if (h != NULL)
2539     {
2540       switch (ELF32_R_TYPE (rel->r_info))
2541         {
2542         case R_CKCORE_GNU_VTINHERIT:
2543         case R_CKCORE_GNU_VTENTRY:
2544           return NULL;
2545         }
2546     }
2547
2548   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2549 }
2550
2551 /* Look through the relocs for a section during the first phase.
2552    Since we don't do .gots or .plts, we just need to consider the
2553    virtual table relocs for gc.  */
2554
2555 static bfd_boolean
2556 csky_elf_check_relocs (bfd * abfd,
2557                        struct bfd_link_info * info,
2558                        asection * sec,
2559                        const Elf_Internal_Rela * relocs)
2560 {
2561   Elf_Internal_Shdr * symtab_hdr;
2562   struct elf_link_hash_entry ** sym_hashes;
2563   const Elf_Internal_Rela * rel;
2564   const Elf_Internal_Rela * rel_end;
2565   struct csky_elf_link_hash_table *htab;
2566   asection *sreloc;
2567
2568   /* if output type is relocatable, return.  */
2569   if (bfd_link_relocatable (info))
2570     return TRUE;
2571
2572   htab = csky_elf_hash_table (info);
2573   if (htab == NULL)
2574     return FALSE;
2575
2576   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2577   sym_hashes = elf_sym_hashes (abfd);
2578
2579   rel_end = relocs + sec->reloc_count;
2580   sreloc = NULL;
2581   for (rel = relocs; rel < rel_end; rel++)
2582     {
2583       struct elf_link_hash_entry *h;
2584       unsigned long r_symndx;
2585       Elf_Internal_Sym *isym;
2586       int r_type;
2587
2588       r_symndx = ELF32_R_SYM (rel->r_info);
2589       r_type = ELF32_R_TYPE (rel->r_info);
2590       if (r_symndx < symtab_hdr->sh_info)
2591         {
2592           /* A local symbol.  */
2593           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2594                                         abfd, r_symndx);
2595           if (isym == NULL)
2596             return FALSE;
2597           h = NULL;
2598         }
2599       else
2600         {
2601           isym = NULL;
2602           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2603           while (h->root.type == bfd_link_hash_indirect
2604                  || h->root.type == bfd_link_hash_warning)
2605             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2606         }
2607
2608       switch (r_type)
2609         {
2610         case R_CKCORE_PCREL_IMM26BY2:
2611         case R_CKCORE_PCREL_IMM11BY2:
2612         case R_CKCORE_PCREL_JSR_IMM11BY2:
2613         case R_CKCORE_PCREL_JSR_IMM26BY2:
2614           /* If the symbol is '*UND*', means this reloc is used for
2615            * callgraph, don't need to leave to shared object. */
2616           if (r_symndx == 0)
2617             break;
2618           /* Else fall through.  */
2619         case R_CKCORE_ADDR32:
2620         case R_CKCORE_ADDR_HI16:
2621         case R_CKCORE_ADDR_LO16:
2622           if (h != NULL
2623               && bfd_link_executable (info)
2624               && r_type == R_CKCORE_ADDR32
2625               && h->type == STT_OBJECT
2626               && (sec->flags & SEC_ALLOC) != 0
2627               && (sec->flags & SEC_READONLY))
2628             /* If this reloc is in a read-only section, we might
2629                need a copy reloc.  We can't check reliably at this
2630                stage whether the section is read-only, as input
2631                sections have not yet been mapped to output sections.
2632                Tentatively set the flag for now, and correct in
2633                adjust_dynamic_symbol.  */
2634             h->non_got_ref = 1;
2635
2636           /* If we are creating a shared library or relocatable executable,
2637              and this is a reloc against a global symbol, then we need to
2638              copy the reloc into the shared library. However, if we are
2639              linking with -Bsymbolic, we do not need to copy a reloc
2640              against a global symbol which is defined in an object we are
2641              including in the link (i.e., DEF_REGULAR is set).  At
2642              this point we have not seen all the input files, so it is
2643              possible that DEF_REGULAR is not set now but will be set
2644              later (it is never cleared). We account for that possibility
2645              below by storing information in the relocs_copied field of
2646              the hash table entry.  */
2647           if ((bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
2648               || (!bfd_link_pic (info)
2649                   && (sec->flags & SEC_ALLOC) != 0
2650                   && h != NULL
2651                   && (h->root.type == bfd_link_hash_defweak
2652                       || !h->def_regular)))
2653             {
2654               struct elf_dyn_relocs *p;
2655               struct elf_dyn_relocs **head;
2656               /* We must copy these reloc types into the output file.
2657                  Create a reloc section in dynobj and make room for
2658                  this reloc.  */
2659               if (sreloc == NULL)
2660                 {
2661                   if (htab->elf.dynobj == NULL)
2662                     htab->elf.dynobj = abfd;
2663
2664                   sreloc = _bfd_elf_make_dynamic_reloc_section
2665                     (sec, htab->elf.dynobj, 2, abfd, TRUE);
2666
2667                   if (sreloc == NULL)
2668                     return FALSE;
2669                 }
2670
2671               if (h == NULL && !use_branch_stub
2672                   && ((ELF32_R_TYPE (rel->r_info)
2673                        == R_CKCORE_PCREL_IMM26BY2)
2674                       || (ELF32_R_TYPE (rel->r_info)
2675                           == R_CKCORE_PCREL_IMM11BY2)))
2676                 break;
2677
2678               /* If this is a global symbol, we count the number of
2679                  relocations we need for this symbol.  */
2680               if (h != NULL)
2681                 {
2682                   struct csky_elf_link_hash_entry *eh;
2683                   eh = (struct  csky_elf_link_hash_entry *)h;
2684                   if ((ELF32_R_TYPE (rel->r_info)
2685                        == R_CKCORE_PCREL_JSR_IMM26BY2)
2686                       || (ELF32_R_TYPE (rel->r_info)
2687                           == R_CKCORE_PCREL_JSR_IMM11BY2))
2688                     eh->jsri2bsr_refcount += 1;
2689                   head = &eh->dyn_relocs;
2690                 }
2691               else
2692                 {
2693                   /* Track dynamic relocs needed for local syms too.
2694                      We really need local syms available to do this
2695                      easily.  Oh well.  */
2696                   void **vpp;
2697                   asection *s;
2698                   Elf_Internal_Sym *loc_isym;
2699
2700                   loc_isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2701                                                     abfd, r_symndx);
2702                   if (loc_isym == NULL)
2703                     return FALSE;
2704                   s = bfd_section_from_elf_index (abfd, loc_isym->st_shndx);
2705                   if (s == NULL)
2706                     s = sec;
2707                   vpp = &elf_section_data (s)->local_dynrel;
2708                   head = (struct elf_dyn_relocs **)vpp;
2709                 }
2710
2711               p = *head;
2712               if (p == NULL || p->sec != sec)
2713                 {
2714                   bfd_size_type amt = sizeof *p;
2715                   p = ((struct elf_dyn_relocs *)
2716                        bfd_alloc (htab->elf.dynobj, amt));
2717                   if (p == NULL)
2718                     return FALSE;
2719                   p->next = *head;
2720                   *head = p;
2721                   p->sec = sec;
2722                   p->count = 0;
2723                   p->pc_count = 0;
2724                 }
2725
2726               if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2
2727                   || ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM11BY2)
2728                 p->pc_count += 1;
2729               p->count += 1;
2730             }
2731           break;
2732
2733         case R_CKCORE_PLT_IMM18BY4:
2734         case R_CKCORE_PLT32:
2735           /* This symbol requires a procedure linkage table entry.  We
2736              actually build the entry in adjust_dynamic_symbol,
2737              because this might be a case of linking PIC code which is
2738              never referenced by a dynamic object, in which case we
2739              don't need to generate a procedure linkage table entry
2740              after all.  */
2741
2742           /* If this is a local symbol, we resolve it directly without
2743              creating a procedure linkage table entry.  */
2744           if (h == NULL)
2745             continue;
2746           if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PLT_IMM18BY4)
2747             check_got_overflow = 1;
2748
2749           h->needs_plt = 1;
2750           h->plt.refcount += 1;
2751           h->got.refcount += 1;
2752           ((struct  csky_elf_link_hash_entry *)h)->plt_refcount += 1;
2753           break;
2754
2755         case R_CKCORE_GOT12:
2756         case R_CKCORE_PLT12:
2757         case R_CKCORE_GOT32:
2758         case R_CKCORE_GOT_HI16:
2759         case R_CKCORE_GOT_LO16:
2760         case R_CKCORE_PLT_HI16:
2761         case R_CKCORE_PLT_LO16:
2762         case R_CKCORE_GOT_IMM18BY4:
2763         case R_CKCORE_TLS_IE32:
2764         case R_CKCORE_TLS_GD32:
2765           {
2766             int tls_type, old_tls_type;
2767
2768             if (h != NULL
2769                 && bfd_link_executable (info)
2770                 && r_type == R_CKCORE_GOT_IMM18BY4
2771                 && (sec->flags & SEC_ALLOC) != 0
2772                 && (sec->flags & SEC_READONLY))
2773               /* If this reloc is in a read-only section, we might
2774                  need a copy reloc.  We can't check reliably at this
2775                  stage whether the section is read-only, as input
2776                  sections have not yet been mapped to output sections.
2777                  Tentatively set the flag for now, and correct in
2778                  adjust_dynamic_symbol.  */
2779               h->non_got_ref = 1;
2780
2781             switch (ELF32_R_TYPE (rel->r_info))
2782               {
2783               case R_CKCORE_TLS_IE32:
2784                 tls_type = GOT_TLS_IE;
2785                 break;
2786               case R_CKCORE_TLS_GD32:
2787                 tls_type = GOT_TLS_GD;
2788                 break;
2789               default:
2790                 tls_type = GOT_NORMAL;
2791                 break;
2792               }
2793             if (h != NULL)
2794               {
2795                 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_GOT_IMM18BY4)
2796                   check_got_overflow = 1;
2797                 h->got.refcount += 1;
2798                 old_tls_type = csky_elf_hash_entry (h)->tls_type;
2799               }
2800             else
2801               {
2802                 bfd_signed_vma *local_got_refcounts;
2803
2804                 /* This is a global offset table entry for a local symbol.  */
2805                 /* we can write a new function named
2806                    elf32_csky_allocate_local_sym_info() to replace
2807                    following code.  */
2808                 local_got_refcounts = elf_local_got_refcounts (abfd);
2809                 if (local_got_refcounts == NULL)
2810                   {
2811                     bfd_size_type size;
2812
2813                     size = symtab_hdr->sh_info;
2814                     size *= (sizeof (bfd_signed_vma) + sizeof (char));
2815                     local_got_refcounts = ((bfd_signed_vma *)
2816                                            bfd_zalloc (abfd, size));
2817                     if (local_got_refcounts == NULL)
2818                       return FALSE;
2819                     elf_local_got_refcounts (abfd) = local_got_refcounts;
2820                     csky_elf_local_got_tls_type (abfd)
2821                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2822                   }
2823                 local_got_refcounts[r_symndx] += 1;
2824                 old_tls_type = csky_elf_local_got_tls_type (abfd)[r_symndx];
2825               }
2826
2827             /* We will already have issued an error message if there is a
2828                TLS / non-TLS mismatch, based on the symbol type.  We don't
2829                support any linker relaxations.  So just combine any TLS
2830                types needed.  */
2831             if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2832                 && tls_type != GOT_NORMAL)
2833               tls_type |= old_tls_type;
2834
2835             if (old_tls_type != tls_type)
2836               {
2837                 if (h != NULL)
2838                   csky_elf_hash_entry (h)->tls_type = tls_type;
2839                 else
2840                   csky_elf_local_got_tls_type (abfd)[r_symndx] = tls_type;
2841               }
2842           }
2843           /* Fall through.  */
2844
2845         case R_CKCORE_TLS_LDM32:
2846           if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_TLS_LDM32)
2847             htab->tls_ldm_got.refcount++;
2848           /* Fall through.  */
2849
2850         case R_CKCORE_GOTOFF:
2851         case R_CKCORE_GOTPC:
2852         case R_CKCORE_GOTOFF_HI16:
2853         case R_CKCORE_GOTOFF_LO16:
2854         case R_CKCORE_GOTPC_HI16:
2855         case R_CKCORE_GOTPC_LO16:
2856         case R_CKCORE_GOTOFF_IMM18:
2857           if (htab->elf.sgot == NULL)
2858             {
2859               if (htab->elf.dynobj == NULL)
2860                 htab->elf.dynobj = abfd;
2861               if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2862                 return FALSE;
2863             }
2864           break;
2865
2866           /* This relocation describes the C++ object vtable hierarchy.
2867              Reconstruct it for later use during GC.  */
2868         case R_CKCORE_GNU_VTINHERIT:
2869           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2870             return FALSE;
2871           break;
2872
2873           /* This relocation describes which C++ vtable entries are actually
2874              used.  Record for later use during GC.  */
2875         case R_CKCORE_GNU_VTENTRY:
2876           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2877             return FALSE;
2878           break;
2879         }
2880     }
2881
2882   return TRUE;
2883 }
2884
2885 static const struct bfd_elf_special_section csky_elf_special_sections[]=
2886 {
2887   { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2888   { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2889   { NULL,                     0,  0, 0,            0 }
2890 };
2891
2892 /* Function to keep CSKY specific flags in the ELF header.  */
2893
2894 static bfd_boolean
2895 csky_elf_set_private_flags (bfd * abfd, flagword flags)
2896 {
2897   BFD_ASSERT (! elf_flags_init (abfd)
2898               || elf_elfheader (abfd)->e_flags == flags);
2899
2900   elf_elfheader (abfd)->e_flags = flags;
2901   elf_flags_init (abfd) = TRUE;
2902   return TRUE;
2903 }
2904
2905 static csky_arch_for_merge *
2906 csky_find_arch_with_eflag (const unsigned long arch_eflag)
2907 {
2908   csky_arch_for_merge *csky_arch = NULL;
2909
2910   for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
2911     if (csky_arch->arch_eflag == arch_eflag)
2912       break;
2913   if (csky_arch == NULL)
2914     {
2915       _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
2916                            arch_eflag);
2917       bfd_set_error (bfd_error_wrong_format);
2918     }
2919   return csky_arch;
2920 }
2921
2922 /* Merge backend specific data from an object file to the output
2923    object file when linking.  */
2924
2925 static bfd_boolean
2926 csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2927 {
2928   bfd *obfd = info->output_bfd;
2929   flagword old_flags;
2930   flagword new_flags;
2931   csky_arch_for_merge *old_arch = NULL;
2932   csky_arch_for_merge *new_arch = NULL;
2933
2934   /* Check if we have the same endianness.  */
2935   if (! _bfd_generic_verify_endian_match (ibfd, info))
2936     return FALSE;
2937
2938   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2939       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2940     return TRUE;
2941
2942   new_flags = elf_elfheader (ibfd)->e_flags;
2943   old_flags = elf_elfheader (obfd)->e_flags;
2944
2945   if (! elf_flags_init (obfd))
2946     {
2947       /* First call, no flags set.  */
2948       elf_flags_init (obfd) = TRUE;
2949       elf_elfheader (obfd)->e_flags = new_flags;
2950     }
2951   else if (new_flags == old_flags)
2952     /* Do nothing.  */
2953     ;
2954   else if (new_flags == 0 || old_flags == 0)
2955     /* When one flag is 0, assign the other one's flag.  */
2956       elf_elfheader (obfd)->e_flags = new_flags | old_flags;
2957   else
2958     {
2959       flagword newest_flag = 0;
2960
2961       if ((new_flags & CSKY_ARCH_MASK) != 0
2962           && (old_flags & CSKY_ARCH_MASK) != 0)
2963         {
2964           new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
2965           old_arch = csky_find_arch_with_eflag (old_flags & CSKY_ARCH_MASK);
2966           /* Collect flags like e, f, g.  */
2967           newest_flag = (old_flags & (~CSKY_ARCH_MASK))
2968                          | (new_flags & (~CSKY_ARCH_MASK));
2969           if (new_arch != NULL && old_arch != NULL)
2970             {
2971               if (new_arch->class != old_arch->class)
2972                 {
2973                   _bfd_error_handler
2974                     /* xgettext:c-format */
2975                     (_("%pB: machine flag conflict with target"), ibfd);
2976                   bfd_set_error (bfd_error_wrong_format);
2977                   return FALSE;
2978                 }
2979               else if (new_arch->class_level != old_arch->class_level)
2980                 {
2981                   csky_arch_for_merge *newest_arch
2982                     = (new_arch->class_level > old_arch->class_level
2983                        ? new_arch : old_arch);
2984                   if (new_arch->do_warning || old_arch->do_warning)
2985                     {
2986                       _bfd_error_handler
2987                         /* xgettext:c-format */
2988                         (_("warning: file %pB's arch flag ck%s conflicts with "
2989                            "target ck%s, using ck%s"),
2990                          ibfd, new_arch->name, old_arch->name,
2991                          newest_arch->name);
2992                        bfd_set_error (bfd_error_wrong_format);
2993                     }
2994
2995                   newest_flag |= newest_arch->arch_eflag;
2996                 }
2997               else
2998                 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
2999                                 | (old_flags
3000                                    & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3001             }
3002           else
3003             newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3004                             | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3005         }
3006       else
3007         newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3008                         | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3009
3010       elf_elfheader (obfd)->e_flags = newest_flag;
3011     }
3012   return TRUE;
3013 }
3014
3015 /* Ignore the discarded relocs in special sections in link time.  */
3016
3017 static bfd_boolean
3018 csky_elf_ignore_discarded_relocs (asection *sec)
3019 {
3020   if (strcmp (sec->name, ".csky_stack_size") == 0)
3021     return TRUE;
3022   return FALSE;
3023 }
3024
3025 /* .csky_stack_size are not referenced directly.  This pass marks all of
3026    them as required.  */
3027
3028 static bfd_boolean
3029 elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
3030                                    elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
3031 {
3032   bfd *sub;
3033
3034   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
3035
3036   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3037     {
3038       asection *o;
3039
3040       for (o = sub->sections; o != NULL; o = o->next)
3041         if (strcmp (o->name, ".csky_stack_size") == 0)
3042           o->gc_mark = 1;
3043     }
3044
3045   return TRUE;
3046 }
3047
3048 /* The linker repeatedly calls this function for each input section,
3049    in the order that input sections are linked into output sections.
3050    Build lists of input sections to determine groupings between which
3051    we may insert linker stubs.  */
3052
3053 void
3054 elf32_csky_next_input_section (struct bfd_link_info *info,
3055                                asection *isec)
3056 {
3057   struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3058   if (htab == NULL)
3059     return;
3060   if (isec->output_section->index <= htab->top_index)
3061     {
3062       asection **list = htab->input_list + isec->output_section->index;
3063
3064       if (*list != bfd_abs_section_ptr)
3065         {
3066           /* Steal the link_sec pointer for our list.  */
3067 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3068           /* This happens to make the list in reverse order,
3069              which we reverse later in group_sections.  */
3070           PREV_SEC (isec) = *list;
3071           *list = isec;
3072         }
3073     }
3074 }
3075
3076 /* See whether we can group stub sections together.  Grouping stub
3077    sections may result in fewer stubs.  More importantly, we need to
3078    put all .init* and .fini* stubs at the end of the .init or
3079    .fini output sections respectively, because glibc splits the
3080    _init and _fini functions into multiple parts.  Putting a stub in
3081    the middle of a function is not a good idea.  */
3082
3083 static void
3084 group_sections (struct csky_elf_link_hash_table *htab,
3085                 bfd_size_type stub_group_size,
3086                 bfd_boolean stubs_always_after_branch)
3087 {
3088   asection **list = htab->input_list;
3089
3090   do
3091     {
3092       asection *tail = *list;
3093       asection *head;
3094
3095       if (tail == bfd_abs_section_ptr)
3096         continue;
3097
3098       /* Reverse the list: we must avoid placing stubs at the
3099          beginning of the section because the beginning of the text
3100          section may be required for an interrupt vector in bare metal
3101          code.  */
3102 #define NEXT_SEC PREV_SEC
3103       head = NULL;
3104       while (tail != NULL)
3105         {
3106           /* Pop from tail.  */
3107           asection *item = tail;
3108           tail = PREV_SEC (item);
3109
3110           /* Push on head.  */
3111           NEXT_SEC (item) = head;
3112           head = item;
3113         }
3114
3115       while (head != NULL)
3116         {
3117           asection *curr;
3118           asection *next;
3119           bfd_vma stub_group_start = head->output_offset;
3120           bfd_vma end_of_next;
3121
3122           curr = head;
3123           while (NEXT_SEC (curr) != NULL)
3124             {
3125               next = NEXT_SEC (curr);
3126               end_of_next = next->output_offset + next->size;
3127               if (end_of_next - stub_group_start >= stub_group_size)
3128                 /* End of NEXT is too far from start, so stop.  */
3129                 break;
3130               curr = next;
3131             }
3132
3133           /* OK, the size from the start to the start of CURR is less
3134            * than stub_group_size and thus can be handled by one stub
3135            * section.  (Or the head section is itself larger than
3136            * stub_group_size, in which case we may be toast.)
3137            * We should really be keeping track of the total size of
3138            * stubs added here, as stubs contribute to the final output
3139            * section size.  */
3140           do
3141             {
3142               next = NEXT_SEC (head);
3143               /* Set up this stub group.  */
3144               htab->stub_group[head->id].link_sec = curr;
3145             }
3146           while (head != curr && (head = next) != NULL);
3147
3148           /* But wait, there's more!  Input sections up to stub_group_size
3149            * bytes after the stub section can be handled by it too.  */
3150           if (!stubs_always_after_branch)
3151             {
3152               stub_group_start = curr->output_offset + curr->size;
3153
3154               while (next != NULL)
3155                 {
3156                   end_of_next = next->output_offset + next->size;
3157                   if (end_of_next - stub_group_start >= stub_group_size)
3158                     /* End of NEXT is too far from stubs, so stop.  */
3159                     break;
3160                   /* Add NEXT to the stub group.  */
3161                   head = next;
3162                   next = NEXT_SEC (head);
3163                   htab->stub_group[head->id].link_sec = curr;
3164                 }
3165             }
3166           head = next;
3167         }
3168     }
3169   while (list++ != htab->input_list + htab->top_index);
3170
3171   free (htab->input_list);
3172 #undef PREV_SEC
3173 #undef NEXT_SEC
3174 }
3175
3176 /* If the symbol referenced by bsr is defined in shared object file,
3177    or it is a weak symbol and we aim to create shared object file,
3178    we must create a stub for this bsr.  */
3179
3180 static bfd_boolean
3181 sym_must_create_stub (struct elf_link_hash_entry *h,
3182                       struct bfd_link_info *info)
3183 {
3184   if (h != NULL
3185       && ((h->def_dynamic && !h->def_regular)
3186           || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
3187     return TRUE;
3188   else
3189     return FALSE;
3190 }
3191
3192 /* Calculate the template, template size and instruction size for a stub.
3193    Return value is the instruction size.  */
3194
3195 static unsigned int
3196 find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
3197                              const insn_sequence **stub_template,
3198                              int *stub_template_size)
3199 {
3200   const insn_sequence *template_sequence = NULL;
3201   int template_size = 0;
3202   int i;
3203   unsigned int size;
3204
3205   template_sequence = stub_definitions[stub_type].template_sequence;
3206   template_size = stub_definitions[stub_type].template_size;
3207
3208   size = 0;
3209   for (i = 0; i < template_size; i++)
3210     {
3211       switch (template_sequence[i].type)
3212       {
3213       case INSN16:
3214         size += 2;
3215         break;
3216
3217       case INSN32:
3218       case DATA_TYPE:
3219         size += 4;
3220         break;
3221
3222       default:
3223         BFD_FAIL ();
3224         return FALSE;
3225       }
3226     }
3227
3228   if (stub_template)
3229     *stub_template = template_sequence;
3230   if (stub_template_size)
3231     *stub_template_size = template_size;
3232
3233   return size;
3234 }
3235
3236 /* As above, but don't actually build the stub.  Just bump offset so
3237    we know stub section sizes.  */
3238
3239 static bfd_boolean
3240 csky_size_one_stub (struct bfd_hash_entry *gen_entry,
3241                     void * in_arg ATTRIBUTE_UNUSED)
3242 {
3243   struct elf32_csky_stub_hash_entry *stub_entry;
3244   const insn_sequence *template_sequence = NULL;
3245   int template_size = 0;
3246   int size = 0;
3247
3248   /* Massage our args to the form they really have.  */
3249   stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
3250
3251   BFD_ASSERT (stub_entry->stub_type > csky_stub_none
3252               && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
3253   size = find_stub_size_and_template (stub_entry->stub_type,
3254                                       &template_sequence, &template_size);
3255   stub_entry->stub_size = size;
3256   stub_entry->stub_template = template_sequence;
3257   stub_entry->stub_template_size = template_size;
3258
3259   size = (size + 7) & ~7;
3260   stub_entry->stub_sec->size += size;
3261   return TRUE;
3262 }
3263
3264 /* Add a new stub entry to the stub hash.  Not all fields of the new
3265    stub entry are initialised.  */
3266
3267 static struct elf32_csky_stub_hash_entry *
3268 elf32_csky_add_stub (const char *stub_name,
3269                      asection *section,
3270                      struct csky_elf_link_hash_table *htab)
3271 {
3272   asection *link_sec;
3273   asection *stub_sec;
3274   struct elf32_csky_stub_hash_entry *stub_entry;
3275
3276   stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
3277   if (stub_sec == NULL)
3278     return NULL;
3279
3280   /* Enter this entry into the linker stub hash table.  */
3281   stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3282                                       TRUE, FALSE);
3283   if (stub_entry == NULL)
3284     {
3285       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3286                           section->owner, stub_name);
3287       return NULL;
3288     }
3289
3290   stub_entry->stub_sec = stub_sec;
3291   stub_entry->stub_offset = 0;
3292   stub_entry->id_sec = link_sec;
3293
3294   return stub_entry;
3295 }
3296
3297 /* Determine and set the size of the stub section for a final link.
3298    The basic idea here is to examine all the relocations looking for
3299    PC-relative calls to a target that is unreachable with a "bsr"
3300    instruction.  */
3301
3302 bfd_boolean
3303 elf32_csky_size_stubs (bfd *output_bfd,
3304                        bfd *stub_bfd,
3305                        struct bfd_link_info *info,
3306                        bfd_signed_vma group_size,
3307                        asection *(*add_stub_section) (const char*, asection*),
3308                        void (*layout_sections_again) (void))
3309 {
3310   bfd_size_type stub_group_size;
3311   bfd_boolean stubs_always_after_branch;
3312   struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3313
3314   if (htab == NULL)
3315     return FALSE;
3316
3317   /* Propagate mach to stub bfd, because it may not have been
3318      finalized when we created stub_bfd.  */
3319   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3320                      bfd_get_mach (output_bfd));
3321
3322   /* Stash our params away.  */
3323   htab->stub_bfd = stub_bfd;
3324   htab->add_stub_section = add_stub_section;
3325   htab->layout_sections_again = layout_sections_again;
3326   stubs_always_after_branch = group_size < 0;
3327
3328   if (group_size < 0)
3329     stub_group_size = -group_size;
3330   else
3331     stub_group_size = group_size;
3332
3333   if (stub_group_size == 1)
3334     /* The 'bsr' range in abiv2 is +-64MB has to be used as the
3335        default maximum size.
3336        This value is 128K less than that, which allows for 131072
3337        byte stubs. If we exceed that, then we will fail to link.
3338        The user will have to relink with an explicit group size
3339        option.  */
3340     stub_group_size = 66977792;
3341
3342   group_sections (htab, stub_group_size, stubs_always_after_branch);
3343
3344   while (1)
3345     {
3346       bfd *input_bfd;
3347       unsigned int bfd_indx;
3348       asection *stub_sec;
3349       bfd_boolean stub_changed = FALSE;
3350
3351       for (input_bfd = info->input_bfds, bfd_indx = 0;
3352            input_bfd != NULL;
3353            input_bfd = input_bfd->link.next, bfd_indx++)
3354         {
3355           Elf_Internal_Shdr *symtab_hdr;
3356           asection *section;
3357           Elf_Internal_Sym *local_syms = NULL;
3358
3359           /* We'll need the symbol table in a second.  */
3360           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3361           if (symtab_hdr->sh_info == 0)
3362             continue;
3363
3364           /* Walk over each section attached to the input bfd.  */
3365           for (section = input_bfd->sections;
3366                section != NULL;
3367                section = section->next)
3368             {
3369               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3370
3371               /* If there aren't any relocs, then there's nothing more
3372                * to do.  */
3373               if ((section->flags & SEC_RELOC) == 0
3374                   || section->reloc_count == 0
3375                   || (section->flags & SEC_CODE) == 0)
3376                 continue;
3377
3378               /* If this section is a link-once section that will be
3379                  discarded, then don't create any stubs.  */
3380               if (section->output_section == NULL
3381                   || section->output_section->owner != output_bfd)
3382                 continue;
3383
3384               /* Get the relocs.  */
3385               internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
3386                                                            section,
3387                                                            NULL, NULL,
3388                                                            info->keep_memory);
3389
3390               if (internal_relocs == NULL)
3391                 goto error_ret_free_local;
3392
3393               /* Now examine each relocation.  */
3394               irela = internal_relocs;
3395               irelaend = irela + section->reloc_count;
3396               for (; irela < irelaend; irela++)
3397                 {
3398                   unsigned int r_type, r_indx;
3399                   enum elf32_csky_stub_type stub_type;
3400                   struct elf32_csky_stub_hash_entry *stub_entry;
3401                   asection *sym_sec;
3402                   bfd_vma sym_value;
3403                   bfd_vma destination;
3404                   struct csky_elf_link_hash_entry *hash;
3405                   const char *sym_name;
3406                   char *stub_name;
3407                   const asection *id_sec;
3408                   unsigned char st_type;
3409
3410                   r_type = ELF32_R_TYPE (irela->r_info);
3411                   r_indx = ELF32_R_SYM (irela->r_info);
3412                   if (r_type >= (unsigned int) R_CKCORE_MAX)
3413                     {
3414                       bfd_set_error (bfd_error_bad_value);
3415 error_ret_free_internal:
3416                       if (elf_section_data (section)->relocs == NULL)
3417                         free (internal_relocs);
3418                       goto error_ret_free_local;
3419                     }
3420
3421                   /* Only look for stubs on branch instructions.  */
3422                   if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
3423                     continue;
3424                   /* Now determine the call target, its name, value,
3425                      section.  */
3426                   sym_sec = NULL;
3427                   sym_value = 0;
3428                   destination = 0;
3429                   hash = NULL;
3430                   sym_name = NULL;
3431                   if (r_indx < symtab_hdr->sh_info)
3432                     {
3433                       /* It's a local symbol.  */
3434                       Elf_Internal_Sym *sym;
3435                       Elf_Internal_Shdr *hdr;
3436                       if (local_syms == NULL)
3437                         local_syms =
3438                           (Elf_Internal_Sym *) symtab_hdr->contents;
3439                       if (local_syms == NULL)
3440                         {
3441                           local_syms =
3442                             bfd_elf_get_elf_syms (input_bfd,
3443                                                   symtab_hdr,
3444                                                   symtab_hdr->sh_info,
3445                                                   0, NULL, NULL, NULL);
3446                           if (local_syms == NULL)
3447                             goto error_ret_free_internal;
3448                         }
3449                       sym = local_syms + r_indx;
3450                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3451                       sym_sec = hdr->bfd_section;
3452                       if (!sym_sec)
3453                         /* This is an undefined symbol.  It can never
3454                            be resolved.  */
3455                         continue;
3456                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3457                         sym_value = sym->st_value;
3458                       destination = (sym_value + irela->r_addend
3459                                      + sym_sec->output_offset
3460                                      + sym_sec->output_section->vma);
3461                       st_type = ELF_ST_TYPE (sym->st_info);
3462                       sym_name =
3463                         bfd_elf_string_from_elf_section (input_bfd,
3464                                                          symtab_hdr->sh_link,
3465                                                          sym->st_name);
3466                     }
3467                   else
3468                     {
3469                       /* It's an external symbol.  */
3470                       int e_indx;
3471                       e_indx = r_indx - symtab_hdr->sh_info;
3472                       hash = ((struct csky_elf_link_hash_entry *)
3473                               elf_sym_hashes (input_bfd)[e_indx]);
3474
3475                       while (hash->elf.root.type == bfd_link_hash_indirect
3476                              || hash->elf.root.type == bfd_link_hash_warning)
3477                         hash = ((struct csky_elf_link_hash_entry *)
3478                                 hash->elf.root.u.i.link);
3479                       if (hash->elf.root.type == bfd_link_hash_defined
3480                           || hash->elf.root.type == bfd_link_hash_defweak)
3481                         {
3482                           sym_sec = hash->elf.root.u.def.section;
3483                           sym_value = hash->elf.root.u.def.value;
3484
3485                           struct csky_elf_link_hash_table *globals =
3486                             csky_elf_hash_table (info);
3487                           /* FIXME For a destination in a shared library.  */
3488                           if (globals->elf.splt != NULL && hash != NULL
3489                               && hash->elf.plt.offset != (bfd_vma) -1)
3490                             continue;
3491                           else if (sym_sec->output_section != NULL)
3492                             destination = (sym_value + irela->r_addend
3493                                            + sym_sec->output_offset
3494                                            + sym_sec->output_section->vma);
3495                         }
3496                       else if (hash->elf.root.type == bfd_link_hash_undefined
3497                                || (hash->elf.root.type
3498                                    == bfd_link_hash_undefweak))
3499                         /* FIXME For a destination in a shared library.  */
3500                         continue;
3501                       else
3502                         {
3503                           bfd_set_error (bfd_error_bad_value);
3504                           goto error_ret_free_internal;
3505                         }
3506                       st_type = ELF_ST_TYPE (hash->elf.type);
3507                       sym_name = hash->elf.root.root.string;
3508                     }
3509                   do
3510                     {
3511                       /* Determine what (if any) linker stub is needed.  */
3512                       stub_type = csky_type_of_stub (info, section, irela,
3513                                                      st_type, hash,
3514                                                      destination, sym_sec,
3515                                                      input_bfd, sym_name);
3516                       if (stub_type == csky_stub_none)
3517                         break;
3518
3519                       /* Support for grouping stub sections.  */
3520                       id_sec = htab->stub_group[section->id].link_sec;
3521
3522                       /* Get the name of this stub.  */
3523                       stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
3524                                                         irela);
3525                       if (!stub_name)
3526                         goto error_ret_free_internal;
3527                       /* We've either created a stub for this reloc already,
3528                          or we are about to.  */
3529                       stub_entry
3530                         = csky_stub_hash_lookup (&htab->stub_hash_table,
3531                                                  stub_name,
3532                                                  FALSE, FALSE);
3533                       if (stub_entry != NULL)
3534                         {
3535                           /* The proper stub has already been created.  */
3536                           free (stub_name);
3537                           stub_entry->target_value = sym_value;
3538                           break;
3539                         }
3540                       stub_entry = elf32_csky_add_stub (stub_name, section,
3541                                                         htab);
3542                       if (stub_entry == NULL)
3543                         {
3544                           free (stub_name);
3545                           goto error_ret_free_internal;
3546                         }
3547                       stub_entry->target_value = sym_value;
3548                       stub_entry->target_section = sym_sec;
3549                       stub_entry->stub_type = stub_type;
3550                       stub_entry->h = hash;
3551                       stub_entry->st_type = st_type;
3552
3553                       if (sym_name == NULL)
3554                         sym_name = "unnamed";
3555                       stub_entry->output_name =
3556                         bfd_alloc (htab->stub_bfd,
3557                                    (sizeof (STUB_ENTRY_NAME)
3558                                     + strlen (sym_name)));
3559                       if (stub_entry->output_name == NULL)
3560                         {
3561                           free (stub_name);
3562                           goto error_ret_free_internal;
3563                         }
3564                       sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3565                                sym_name);
3566                       stub_changed = TRUE;
3567                     }
3568                   while (0);
3569                 }
3570               /* We're done with the internal relocs, free them.  */
3571               if (elf_section_data (section)->relocs == NULL)
3572                 free (internal_relocs);
3573             }
3574         }
3575       if (!stub_changed)
3576         break;
3577       /* OK, we've added some stubs.  Find out the new size of the
3578          stub sections.  */
3579       for (stub_sec = htab->stub_bfd->sections;
3580            stub_sec != NULL;
3581            stub_sec = stub_sec->next)
3582         {
3583           /* Ignore non-stub sections.  */
3584           if (!strstr (stub_sec->name, STUB_SUFFIX))
3585             continue;
3586           stub_sec->size = 0;
3587         }
3588       bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
3589       /* Ask the linker to do its stuff.  */
3590       (*htab->layout_sections_again) ();
3591     }
3592
3593   return TRUE;
3594 error_ret_free_local:
3595   return FALSE;
3596 }
3597
3598 static bfd_boolean
3599 csky_build_one_stub (struct bfd_hash_entry *gen_entry,
3600                      void * in_arg)
3601 {
3602 #define MAXRELOCS 2
3603   struct elf32_csky_stub_hash_entry *stub_entry;
3604   struct bfd_link_info *info;
3605   asection *stub_sec;
3606   bfd *stub_bfd;
3607   bfd_byte *loc;
3608   bfd_vma sym_value;
3609   int template_size;
3610   int size;
3611   const insn_sequence *template_sequence;
3612   int i;
3613   struct csky_elf_link_hash_table * globals;
3614   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3615   int stub_reloc_offset[MAXRELOCS] = {0, 0};
3616   int nrelocs = 0;
3617   struct elf_link_hash_entry *h = NULL;
3618
3619   /* Massage our args to the form they really have.  */
3620   stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
3621   info = (struct bfd_link_info *) in_arg;
3622
3623   globals = csky_elf_hash_table (info);
3624   if (globals == NULL)
3625     return FALSE;
3626   stub_sec = stub_entry->stub_sec;
3627
3628   /* Make a note of the offset within the stubs for this entry.  */
3629   stub_entry->stub_offset = stub_sec->size;
3630   loc = stub_sec->contents + stub_entry->stub_offset;
3631
3632   stub_bfd = stub_sec->owner;
3633
3634   /* This is the address of the stub destination.  */
3635   h = &stub_entry->h->elf;
3636   if (sym_must_create_stub (h, info)
3637       && !(bfd_link_pic (info)
3638            && h->root.type == bfd_link_hash_defweak
3639            && h->def_regular
3640            && !h->def_dynamic))
3641     sym_value = 0;
3642   else
3643     sym_value = (stub_entry->target_value
3644                  + stub_entry->target_section->output_offset
3645                  + stub_entry->target_section->output_section->vma);
3646
3647   template_sequence = stub_entry->stub_template;
3648   template_size = stub_entry->stub_template_size;
3649
3650   size = 0;
3651   for (i = 0; i < template_size; i++)
3652     switch (template_sequence[i].type)
3653       {
3654       case INSN16:
3655         bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
3656                     loc + size);
3657         size += 2;
3658         break;
3659       case INSN32:
3660         csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3661                           loc + size);
3662         size += 4;
3663         break;
3664       case DATA_TYPE:
3665         bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3666                     loc + size);
3667         stub_reloc_idx[nrelocs] = i;
3668         stub_reloc_offset[nrelocs++] = size;
3669         size += 4;
3670         break;
3671       default:
3672         BFD_FAIL ();
3673         return FALSE;
3674       }
3675   stub_sec->size += size;
3676
3677   /* Stub size has already been computed in csky_size_one_stub. Check
3678      consistency.  */
3679   BFD_ASSERT (size == stub_entry->stub_size);
3680
3681   /* Assume there is at least one and at most MAXRELOCS entries to relocate
3682      in each stub.  */
3683   BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3684
3685   for (i = 0; i < nrelocs; i++)
3686     {
3687       if (sym_must_create_stub (h, info))
3688         {
3689           Elf_Internal_Rela outrel;
3690           asection * sreloc = globals->elf.srelgot;
3691
3692           outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3693           outrel.r_info =
3694             ELF32_R_INFO (h->dynindx,
3695                           template_sequence[stub_reloc_idx[i]].r_type);
3696           outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
3697
3698           loc = sreloc->contents;
3699           loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3700
3701           if (loc != NULL)
3702             bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3703         }
3704       _bfd_final_link_relocate (elf32_csky_howto_from_type
3705                                   (template_sequence[stub_reloc_idx[i]].r_type),
3706                                 stub_bfd, stub_sec, stub_sec->contents,
3707                                 stub_entry->stub_offset + stub_reloc_offset[i],
3708                                 sym_value + stub_entry->target_addend,
3709                                 template_sequence[stub_reloc_idx[i]].reloc_addend);
3710     }
3711
3712   return TRUE;
3713 #undef MAXRELOCS
3714 }
3715
3716 /* Build all the stubs associated with the current output file.  The
3717    stubs are kept in a hash table attached to the main linker hash
3718    table.  We also set up the .plt entries for statically linked PIC
3719    functions here.  This function is called via arm_elf_finish in the
3720    linker.  */
3721
3722 bfd_boolean
3723 elf32_csky_build_stubs (struct bfd_link_info *info)
3724 {
3725   asection *stub_sec;
3726   struct bfd_hash_table *table;
3727   struct csky_elf_link_hash_table *htab;
3728
3729   htab = csky_elf_hash_table (info);
3730
3731   if (htab == NULL)
3732     return FALSE;
3733
3734   for (stub_sec = htab->stub_bfd->sections;
3735        stub_sec != NULL;
3736        stub_sec = stub_sec->next)
3737     {
3738       bfd_size_type size;
3739
3740       /* Ignore non-stub sections.  */
3741       if (!strstr (stub_sec->name, STUB_SUFFIX))
3742         continue;
3743
3744       /* Allocate memory to hold the linker stubs.  */
3745       size = stub_sec->size;
3746       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3747       if (stub_sec->contents == NULL && size != 0)
3748         return FALSE;
3749       stub_sec->size = 0;
3750     }
3751
3752   /* Build the stubs as directed by the stub hash table.  */
3753   table = &htab->stub_hash_table;
3754   bfd_hash_traverse (table, csky_build_one_stub, info);
3755
3756   return TRUE;
3757 }
3758
3759 /* Set up various things so that we can make a list of input sections
3760    for each output section included in the link.  Returns -1 on error,
3761    0 when no stubs will be needed, and 1 on success.  */
3762
3763 int
3764 elf32_csky_setup_section_lists (bfd *output_bfd,
3765                                 struct bfd_link_info *info)
3766 {
3767   bfd *input_bfd;
3768   unsigned int bfd_count;
3769   unsigned int top_id, top_index;
3770   asection *section;
3771   asection **input_list, **list;
3772   bfd_size_type amt;
3773   struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3774
3775   if (!htab)
3776     return 0;
3777   if (! is_elf_hash_table (htab))
3778     return 0;
3779
3780   /* Count the number of input BFDs and find the top input section id.  */
3781   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3782        input_bfd != NULL;
3783        input_bfd = input_bfd->link.next)
3784     {
3785       bfd_count += 1;
3786       for (section = input_bfd->sections;
3787            section != NULL;
3788            section = section->next)
3789         if (top_id < section->id)
3790           top_id = section->id;
3791     }
3792   htab->bfd_count = bfd_count;
3793   amt = sizeof (struct map_stub) * (top_id + 1);
3794   htab->stub_group = bfd_zmalloc (amt);
3795   if (htab->stub_group == NULL)
3796     return -1;
3797
3798   /* We can't use output_bfd->section_count here to find the top output
3799      section index as some sections may have been removed, and
3800      _bfd_strip_section_from_output doesn't renumber the indices.  */
3801   for (section = output_bfd->sections, top_index = 0;
3802        section != NULL;
3803        section = section->next)
3804     if (top_index < section->index)
3805       top_index = section->index;
3806   htab->top_index = top_index;
3807   amt = sizeof (asection *) * (top_index + 1);
3808   input_list = bfd_malloc (amt);
3809   htab->input_list = input_list;
3810   if (input_list == NULL)
3811     return -1;
3812   /* For sections we aren't interested in, mark their entries with a
3813      value we can check later.  */
3814   list = input_list + top_index;
3815   do
3816     *list = bfd_abs_section_ptr;
3817   while (list-- != input_list);
3818   for (section = output_bfd->sections;
3819        section != NULL;
3820        section = section->next)
3821     if ((section->flags & SEC_CODE) != 0)
3822       input_list[section->index] = NULL;
3823
3824   return 1;
3825 }
3826
3827 static bfd_reloc_status_type
3828 csky_relocate_contents (reloc_howto_type *howto,
3829                         bfd *input_bfd,
3830                         long relocation,
3831                         bfd_byte *location)
3832 {
3833   int size;
3834   bfd_vma x = 0;
3835   bfd_reloc_status_type flag;
3836   unsigned int rightshift = howto->rightshift;
3837   unsigned int bitpos = howto->bitpos;
3838
3839   /* If the size is negative, negate RELOCATION. This isn't very
3840      general.  */
3841   if (howto->size < 0)
3842     relocation = -relocation;
3843
3844   /* FIXME: these macros should be defined at file head or head file head.  */
3845 #define CSKY_INSN_ADDI_TO_SUBI        0x04000000
3846 #define CSKY_INSN_MOV_RTB             0xc41d4820   // mov32 rx, r29, 0
3847 #define CSKY_INSN_MOV_RDB             0xc41c4820   // mov32 rx, r28, 0
3848 #define CSKY_INSN_GET_ADDI_RZ(x)      (((x) & 0x03e00000) >> 21)
3849 #define CSKY_INSN_SET_MOV_RZ(x)       ((x) & 0x0000001f)
3850 #define CSKY_INSN_JSRI_TO_LRW         0xea9a0000
3851 #define CSKY_INSN_JSR_R26             0xe8fa0000
3852
3853   /* Get the value we are going to relocate.  */
3854   size = bfd_get_reloc_size (howto);
3855   switch (size)
3856     {
3857     default:
3858     case 0:
3859       abort ();
3860     case 1:
3861       x = bfd_get_8 (input_bfd, location);
3862       break;
3863     case 2:
3864       x = bfd_get_16 (input_bfd, location);
3865       break;
3866     case 4:
3867       if (need_reverse_bits)
3868         {
3869           x = csky_get_insn_32 (input_bfd, location);
3870
3871           if (R_CKCORE_DOFFSET_LO16 == howto->type)
3872             {
3873               if ((signed) relocation < 0)
3874                 {
3875                   x |= CSKY_INSN_ADDI_TO_SUBI;
3876                   relocation = -relocation;
3877                 }
3878               else if (0 == relocation)
3879                 x = (CSKY_INSN_MOV_RDB |
3880                      CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3881             }
3882           else if (R_CKCORE_TOFFSET_LO16 == howto->type)
3883             {
3884               if ((signed) relocation < 0)
3885                 {
3886                   x |= CSKY_INSN_ADDI_TO_SUBI;
3887                   relocation = -relocation;
3888                 }
3889               else if (0 == relocation)
3890                 x = (CSKY_INSN_MOV_RTB |
3891                      CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3892             }
3893         }
3894       else
3895         x = bfd_get_32 (input_bfd, location);
3896       break;
3897     }
3898   /* Check for overflow.  FIXME: We may drop bits during the addition
3899      which we don't check for.  We must either check at every single
3900      operation, which would be tedious, or we must do the computations
3901      in a type larger than bfd_vma, which would be inefficient.  */
3902   flag = bfd_reloc_ok;
3903   if (howto->complain_on_overflow != complain_overflow_dont)
3904     {
3905       int addrmask;
3906       int fieldmask;
3907       int signmask;
3908       int ss;
3909       int a;
3910       int b;
3911       int sum;
3912       /* Get the values to be added together.  For signed and unsigned
3913          relocations, we assume that all values should be truncated to
3914          the size of an address.  For bitfields, all the bits matter.
3915          See also bfd_check_overflow.  */
3916 #define N_ONES(n)      (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
3917       fieldmask = N_ONES (howto->bitsize);
3918       signmask  = ~fieldmask;
3919       addrmask  = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3920       a = (relocation & addrmask) >> rightshift;
3921       if (read_content_substitute)
3922         x = read_content_substitute;
3923       b = (x & howto->src_mask & addrmask) >> bitpos;
3924
3925       switch (howto->complain_on_overflow)
3926         {
3927         case complain_overflow_signed:
3928           /* If any sign bits are set, all sign bits must be set.
3929              That is, A must be a valid negative address after
3930              shifting.  */
3931           signmask = ~(fieldmask >> 1);
3932           /* Fall through.  */
3933
3934         case complain_overflow_bitfield:
3935           /* Much like the signed check, but for a field one bit
3936              wider.  We allow a bitfield to represent numbers in the
3937              range -2**n to 2**n-1, where n is the number of bits in the
3938              field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
3939              can't overflow, which is exactly what we want.  */
3940           ss = a & signmask;
3941           if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3942             flag = bfd_reloc_overflow;
3943           /* We only need this next bit of code if the sign bit of B
3944              is below the sign bit of A.  This would only happen if
3945              SRC_MASK had fewer bits than BITSIZE.  Note that if
3946              SRC_MASK has more bits than BITSIZE, we can get into
3947              trouble; we would need to verify that B is in range, as
3948              we do for A above.  */
3949           ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3950           ss >>= bitpos;
3951
3952           /* Set all the bits above the sign bit.  */
3953           b = (b ^ ss) - ss;
3954
3955           /* Now we can do the addition.  */
3956           sum = a + b;
3957
3958           /* See if the result has the correct sign.  Bits above the
3959              sign bit are junk now; ignore them.  If the sum is
3960              positive, make sure we did not have all negative inputs;
3961              if the sum is negative, make sure we did not have all
3962              positive inputs.  The test below looks only at the sign
3963              bits, and it really just
3964              SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3965
3966              We mask with addrmask here to explicitly allow an address
3967              wrap-around.  The Linux kernel relies on it, and it is
3968              the only way to write assembler code which can run when
3969              loaded at a location 0x80000000 away from the location at
3970              which it is linked.  */
3971
3972           if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3973             flag = bfd_reloc_overflow;
3974           break;
3975         case complain_overflow_unsigned:
3976           /* Checking for an unsigned overflow is relatively easy:
3977              trim the addresses and add, and trim the result as well.
3978              Overflow is normally indicated when the result does not
3979              fit in the field.  However, we also need to consider the
3980              case when, e.g., fieldmask is 0x7fffffff or smaller, an
3981              input is 0x80000000, and bfd_vma is only 32 bits; then we
3982              will get sum == 0, but there is an overflow, since the
3983              inputs did not fit in the field.  Instead of doing a
3984              separate test, we can check for this by or-ing in the
3985              operands when testing for the sum overflowing its final
3986              field.  */
3987           sum = (a + b) & addrmask;
3988           if ((a | b | sum) & signmask)
3989             flag = bfd_reloc_overflow;
3990           break;
3991         default:
3992           abort ();
3993         }
3994
3995     }
3996   /* Put RELOCATION in the right bits.  */
3997   relocation >>= (bfd_vma) rightshift;
3998
3999   if ((howto->type == R_CKCORE_DOFFSET_LO16
4000        || howto->type == R_CKCORE_TOFFSET_LO16)
4001       && relocation == 0)
4002     /* Do nothing lsli32 rx, rz, 0.  */
4003     ;
4004   else
4005     {
4006       /* Fir V1, all this relocation must be x -1.  */
4007       if (howto->type == R_CKCORE_PCREL_IMM11BY2
4008           || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
4009           || howto->type == R_CKCORE_DOFFSET_LO16
4010           || howto->type == R_CKCORE_TOFFSET_LO16)
4011         relocation -= 1;
4012       else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
4013         relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
4014       else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
4015         relocation
4016           = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
4017       else if (howto->type == R_CKCORE_NOJSRI)
4018         {
4019           x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
4020           relocation = 0;
4021           csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
4022         }
4023
4024       relocation <<= (bfd_vma) bitpos;
4025       /* Add RELOCATION to the right bits of X.  */
4026       x = ((x & ~howto->dst_mask)
4027            | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4028     }
4029   /* Put the relocated value back in the object file.  */
4030   switch (size)
4031     {
4032     default:
4033       abort ();
4034     case 1:
4035       bfd_put_8 (input_bfd, x, location);
4036       break;
4037     case 2:
4038       bfd_put_16 (input_bfd, x, location);
4039       break;
4040     case 4:
4041       if (need_reverse_bits)
4042         csky_put_insn_32 (input_bfd, x, location);
4043       else
4044         bfd_put_32 (input_bfd, x, location);
4045       break;
4046     }
4047   return flag;
4048 }
4049
4050 /* Look up an entry in the stub hash. Stub entries are cached because
4051    creating the stub name takes a bit of time.  */
4052
4053 static struct elf32_csky_stub_hash_entry *
4054 elf32_csky_get_stub_entry (const asection *input_section,
4055                            const asection *sym_sec,
4056                            struct elf_link_hash_entry *hash,
4057                            const Elf_Internal_Rela *rel,
4058                            struct csky_elf_link_hash_table *htab)
4059 {
4060   struct elf32_csky_stub_hash_entry *stub_entry;
4061   struct csky_elf_link_hash_entry *h
4062     = (struct csky_elf_link_hash_entry *) hash;
4063   const asection *id_sec;
4064
4065   if ((input_section->flags & SEC_CODE) == 0)
4066     return NULL;
4067
4068   /* If this input section is part of a group of sections sharing one
4069      stub section, then use the id of the first section in the group.
4070      Stub names need to include a section id, as there may well be
4071      more than one stub used to reach say, printf, and we need to
4072      distinguish between them.  */
4073   id_sec = htab->stub_group[input_section->id].link_sec;
4074   if (h != NULL && h->stub_cache != NULL
4075       && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
4076     stub_entry = h->stub_cache;
4077   else
4078     {
4079       char *stub_name;
4080       stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
4081       if (stub_name == NULL)
4082         return NULL;
4083       stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
4084                                           stub_name, FALSE, FALSE);
4085       if (h != NULL)
4086         h->stub_cache = stub_entry;
4087       free (stub_name);
4088     }
4089
4090   return stub_entry;
4091 }
4092
4093 static bfd_reloc_status_type
4094 csky_final_link_relocate (reloc_howto_type *howto,
4095                           bfd *input_bfd,
4096                           asection *input_section,
4097                           bfd_byte *contents,
4098                           bfd_vma address,
4099                           bfd_vma value,
4100                           bfd_vma addend)
4101 {
4102   bfd_vma relocation;
4103
4104   /* Sanity check the address.  */
4105   if (address > bfd_get_section_limit (input_bfd, input_section))
4106     return bfd_reloc_outofrange;
4107
4108   /* This function assumes that we are dealing with a basic relocation
4109      against a symbol. We want to compute the value of the symbol to
4110      relocate to. This is just VALUE, the value of the symbol,
4111      plus ADDEND, any addend associated with the reloc.  */
4112   relocation = value + addend;
4113
4114   /* If the relocation is PC relative, we want to set RELOCATION to
4115      the distance between the symbol (currently in RELOCATION) and the
4116      location we are relocating. Some targets (e.g., i386-aout)
4117      arrange for the contents of the section to be the negative of the
4118      offset of the location within the section; for such targets
4119      pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
4120      simply leave the contents of the section as zero; for such
4121      targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
4122      need to subtract out the offset of the location within the
4123      section (which is just ADDRESS).  */
4124   if (howto->pc_relative)
4125     {
4126       relocation -= (input_section->output_section->vma
4127                      + input_section->output_offset);
4128       if (howto->pcrel_offset)
4129         relocation -= address;
4130     }
4131
4132   return csky_relocate_contents (howto, input_bfd, relocation,
4133                                  contents + address);
4134
4135 }
4136
4137 /* Return the base VMA address which should be subtracted from real addresses
4138    when resolving @dtpoff relocation.
4139    This is PT_TLS segment p_vaddr.  */
4140
4141 static bfd_vma
4142 dtpoff_base (struct bfd_link_info *info)
4143 {
4144   /* If tls_sec is NULL, we should have signalled an error already.  */
4145   if (elf_hash_table (info)->tls_sec == NULL)
4146     return 0;
4147   return elf_hash_table (info)->tls_sec->vma;
4148 }
4149
4150 /* Return the relocation value for @tpoff relocation
4151    if STT_TLS virtual address is ADDRESS.  */
4152
4153 static bfd_vma
4154 tpoff (struct bfd_link_info *info, bfd_vma address)
4155 {
4156   struct elf_link_hash_table *htab = elf_hash_table (info);
4157   bfd_vma base;
4158
4159   /* If tls_sec is NULL, we should have signalled an error already.  */
4160   if (htab->tls_sec == NULL)
4161     return 0;
4162   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
4163   return address - htab->tls_sec->vma + base;
4164 }
4165
4166 /* Relocate a csky section.  */
4167
4168 static bfd_boolean
4169 csky_elf_relocate_section (bfd *                  output_bfd,
4170                            struct bfd_link_info * info,
4171                            bfd *                  input_bfd,
4172                            asection *             input_section,
4173                            bfd_byte *             contents,
4174                            Elf_Internal_Rela *    relocs,
4175                            Elf_Internal_Sym *     local_syms,
4176                            asection **            local_sections)
4177 {
4178   Elf_Internal_Shdr *symtab_hdr;
4179   struct elf_link_hash_entry **sym_hashes;
4180   Elf_Internal_Rela *rel;
4181   Elf_Internal_Rela *relend;
4182   const char *name;
4183   bfd_boolean ret = TRUE;
4184   struct csky_elf_link_hash_table * htab;
4185   bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
4186
4187   htab = csky_elf_hash_table (info);
4188   if (htab == NULL)
4189     return FALSE;
4190
4191   symtab_hdr = & elf_symtab_hdr (input_bfd);
4192   sym_hashes = elf_sym_hashes (input_bfd);
4193
4194   rel = relocs;
4195   relend = relocs + input_section->reloc_count;
4196   for (; rel < relend; rel++)
4197     {
4198       enum elf_csky_reloc_type     r_type
4199         = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
4200       unsigned long                r_symndx;
4201       reloc_howto_type *           howto;
4202       Elf_Internal_Sym *           sym;
4203       asection *                   sec;
4204       bfd_vma                      relocation;
4205       bfd_vma                      off;
4206       struct elf_link_hash_entry * h;
4207       bfd_vma                      addend = (bfd_vma)rel->r_addend;
4208       bfd_reloc_status_type        r = bfd_reloc_ok;
4209       bfd_boolean                  unresolved_reloc = FALSE;
4210       int                          do_final_relocate = TRUE;
4211       bfd_boolean                  relative_reloc = FALSE;
4212       bfd_signed_vma disp;
4213
4214       /* Ignore these relocation types:
4215          R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY.  */
4216       if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
4217         continue;
4218
4219       if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
4220         {
4221           /* The r_type is error, not support it.  */
4222           /* xgettext:c-format */
4223           _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
4224                               input_bfd, r_type);
4225           bfd_set_error (bfd_error_bad_value);
4226           ret = FALSE;
4227           continue;
4228         }
4229
4230       howto = &csky_elf_howto_table[(int) r_type];
4231
4232       r_symndx = ELF32_R_SYM(rel->r_info);
4233       h = NULL;
4234       sym = NULL;
4235       sec = NULL;
4236       unresolved_reloc = FALSE;
4237
4238       if (r_symndx < symtab_hdr->sh_info)
4239         {
4240           /* Get symbol table entry.  */
4241           sym = local_syms + r_symndx;
4242           sec = local_sections[r_symndx];
4243           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4244           addend = (bfd_vma)rel->r_addend;
4245         }
4246       else
4247         {
4248           bfd_boolean warned, ignored;
4249
4250           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4251                                    r_symndx, symtab_hdr, sym_hashes,
4252                                    h, sec, relocation,
4253                                    unresolved_reloc, warned, ignored);
4254         }
4255
4256       if (sec != NULL && discarded_section (sec))
4257         {
4258           /* For relocs against symbols from removed linkonce sections,
4259              or sections discarded by a linker script, we just want the
4260              section contents zeroed.  Avoid any special processing.
4261              And if the symbol is referenced in '.csky_stack_size' section,
4262              set the address to SEC_DISCARDED(0xffffffff).  */
4263 #if 0
4264           /* The .csky_stack_size section is just for callgraph.  */
4265           if (strcmp (input_section->name, ".csky_stack_size") == 0)
4266             {
4267 /* FIXME: it should define in head file.  */
4268 #define SEC_DISCARDED   0xffffffff
4269               bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
4270               rel->r_info = 0;
4271               rel->r_addend = 0;
4272               continue;
4273             }
4274           else
4275 #endif
4276             RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4277                                              rel, 1, relend, howto, 0,
4278                                              contents);
4279         }
4280
4281       if (bfd_link_relocatable (info))
4282         continue;
4283
4284       read_content_substitute = 0;
4285
4286       /* Final link.  */
4287       disp = (relocation
4288               + (bfd_signed_vma) addend
4289               - input_section->output_section->vma
4290               - input_section->output_offset
4291               - rel->r_offset);
4292 /* It is for ck8xx.  */
4293 #define CSKY_INSN_BSR32   0xe0000000
4294 /* It is for ck5xx/ck6xx.  */
4295 #define CSKY_INSN_BSR16   0xf800
4296 #define within_range(x, L)  (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
4297       switch (howto->type)
4298         {
4299         case R_CKCORE_PCREL_IMM18BY2:
4300           /* When h is NULL, means the instruction written as
4301              grs rx, imm32
4302              if the highest bit is set, prevent the high 32bits
4303              turn to 0xffffffff when signed extern in 64bit
4304              host machine.  */
4305           if (h == NULL && (addend & 0x80000000))
4306             addend &= 0xffffffff;
4307           break;
4308
4309         case R_CKCORE_PCREL32:
4310           break;
4311
4312         case R_CKCORE_GOT12:
4313         case R_CKCORE_PLT12:
4314         case R_CKCORE_GOT_HI16:
4315         case R_CKCORE_GOT_LO16:
4316         case R_CKCORE_PLT_HI16:
4317         case R_CKCORE_PLT_LO16:
4318         case R_CKCORE_GOT32:
4319         case R_CKCORE_GOT_IMM18BY4:
4320           /* Relocation is to the entry for this symbol in the global
4321              offset table.  */
4322           BFD_ASSERT (htab->elf.sgot != NULL);
4323           if (h != NULL)
4324             {
4325               /* Global symbol is defined by other modules.  */
4326               bfd_boolean dyn;
4327               off = h->got.offset;
4328               dyn = htab->elf.dynamic_sections_created;
4329               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4330                                                     bfd_link_pic (info), h)
4331                   || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
4332                   || (ELF_ST_VISIBILITY(h->other)
4333                       && h->root.type == bfd_link_hash_undefweak))
4334                 {
4335                   /* This is actually a static link, or it is a
4336                      -Bsymbolic link and the symbol is defined
4337                      locally, or the symbol was forced to be local
4338                      because of a version file.  We must initialize
4339                      this entry in the global offset table.  Since the
4340                      offset must always be a multiple of 4, we use the
4341                      least significant bit to record whether we have
4342                      initialized it already.
4343                      When doing a dynamic link, we create a .rela.dyn
4344                      relocation entry to initialize the value.  This
4345                      is done in the finish_dynamic_symbol routine. FIXME  */
4346                   if (off & 1)
4347                     off &= ~1;
4348                   else
4349                     {
4350                       bfd_put_32 (output_bfd, relocation,
4351                                   htab->elf.sgot->contents + off);
4352                       h->got.offset |= 1;
4353
4354 /* TRUE if relative relocation should be generated.  GOT reference to
4355    global symbol in PIC will lead to dynamic symbol.  It becomes a
4356    problem when "time" or "times" is defined as a variable in an
4357    executable, clashing with functions of the same name in libc.  If a
4358    symbol isn't undefined weak symbol, don't make it dynamic in PIC and
4359    generate relative relocation.  */
4360 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
4361   ((H)->dynindx == -1 \
4362    && !(H)->forced_local \
4363    && (H)->root.type != bfd_link_hash_undefweak \
4364    && bfd_link_pic (INFO))
4365
4366                       if (GENERATE_RELATIVE_RELOC_P (info, h))
4367                         /* If this symbol isn't dynamic
4368                            in PIC, generate R_CKCORE_RELATIVE here.  */
4369                         relative_reloc = TRUE;
4370                     }
4371                 }
4372               else
4373                 unresolved_reloc = FALSE;
4374             } /* End if h != NULL.  */
4375           else
4376             {
4377               BFD_ASSERT (local_got_offsets != NULL);
4378               off = local_got_offsets[r_symndx];
4379
4380               /* The offset must always be a multiple of 4.  We use
4381                  the least significant bit to record whether we have
4382                  already generated the necessary reloc.  */
4383               if (off & 1)
4384                 off &= ~1;
4385               else
4386                 {
4387                   bfd_put_32 (output_bfd, relocation,
4388                               htab->elf.sgot->contents + off);
4389                   local_got_offsets[r_symndx] |= 1;
4390                   if (bfd_link_pic (info))
4391                     relative_reloc = TRUE;
4392                 }
4393             }
4394           if (relative_reloc)
4395             {
4396               asection *srelgot;
4397               Elf_Internal_Rela outrel;
4398               bfd_byte *loc;
4399
4400               srelgot = htab->elf.srelgot;
4401               BFD_ASSERT (srelgot != NULL);
4402
4403               outrel.r_offset
4404                 = (htab->elf.sgot->output_section->vma
4405                    + htab->elf.sgot->output_offset  + off);
4406               outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4407               outrel.r_addend = relocation;
4408               loc = srelgot->contents;
4409               loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
4410               if (loc != NULL)
4411                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4412             }
4413           relocation = htab->elf.sgot->output_offset + off;
4414           break;
4415
4416         case R_CKCORE_GOTOFF_IMM18:
4417         case R_CKCORE_GOTOFF:
4418         case R_CKCORE_GOTOFF_HI16:
4419         case R_CKCORE_GOTOFF_LO16:
4420           /* Relocation is relative to the start of the global offset
4421              table.  */
4422           /* Note that sgot->output_offset is not involved in this
4423              calculation.  We always want the start of .got.  If we
4424              defined _GLOBAL_OFFSET_TABLE in a different way, as is
4425              permitted by the ABI, we might have to change this
4426              calculation.  */
4427           relocation -= htab->elf.sgot->output_section->vma;
4428           break;
4429
4430         case R_CKCORE_GOTPC:
4431         case R_CKCORE_GOTPC_HI16:
4432         case R_CKCORE_GOTPC_LO16:
4433           /* Use global offset table as symbol value.  */
4434           relocation = htab->elf.sgot->output_section->vma;
4435           addend = -addend;
4436           unresolved_reloc = FALSE;
4437           break;
4438
4439         case R_CKCORE_DOFFSET_IMM18:
4440         case R_CKCORE_DOFFSET_IMM18BY2:
4441         case R_CKCORE_DOFFSET_IMM18BY4:
4442           {
4443             asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4444             relocation -= sdata->output_section->vma;
4445           }
4446           break;
4447
4448         case R_CKCORE_DOFFSET_LO16:
4449           {
4450             asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4451             relocation -= sdata->output_section->vma;
4452           }
4453           break;
4454
4455         case R_CKCORE_TOFFSET_LO16:
4456           {
4457             asection *stext = bfd_get_section_by_name (output_bfd, ".text");
4458             if (stext)
4459               relocation -= stext->output_section->vma;
4460           }
4461           break;
4462
4463         case R_CKCORE_PLT_IMM18BY4:
4464         case R_CKCORE_PLT32:
4465           /* Relocation is to the entry for this symbol in the
4466              procedure linkage table.  */
4467
4468           /* Resolve a PLT32 reloc against a local symbol directly,
4469              without using the procedure linkage table.  */
4470           if (h == NULL)
4471             break;
4472
4473           if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
4474             {
4475               /* We didn't make a PLT entry for this symbol.  This
4476                  happens when statically linking PIC code, or when
4477                  using -Bsymbolic.  */
4478               if (h->got.offset != (bfd_vma) -1)
4479                 {
4480                   bfd_boolean dyn;
4481
4482                   off = h->got.offset;
4483                   dyn = htab->elf.dynamic_sections_created;
4484                   if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4485                                                         bfd_link_pic (info), h)
4486                       || (bfd_link_pic (info)
4487                           && SYMBOL_REFERENCES_LOCAL (info, h))
4488                       || (ELF_ST_VISIBILITY (h->other)
4489                           && h->root.type == bfd_link_hash_undefweak))
4490                     {
4491                       /* This is actually a static link, or it is a
4492                          -Bsymbolic link and the symbol is defined
4493                          locally, or the symbol was forced to be local
4494                          because of a version file.  We must initialize
4495                          this entry in the global offset table.  Since the
4496                          offset must always be a multiple of 4, we use the
4497                          least significant bit to record whether we have
4498                          initialized it already.
4499
4500                          When doing a dynamic link, we create a .rela.dyn
4501                          relocation entry to initialize the value.  This
4502                          is done in the finish_dynamic_symbol routine.
4503                          FIXME!  */
4504                       if (off & 1)
4505                         off &= ~1;
4506                       else
4507                         {
4508                           h->got.offset |= 1;
4509                           if (GENERATE_RELATIVE_RELOC_P (info, h))
4510                             relative_reloc = TRUE;
4511                         }
4512                     }
4513                   bfd_put_32 (output_bfd, relocation,
4514                               htab->elf.sgot->contents + off);
4515
4516                   if (relative_reloc)
4517                     {
4518                       asection *srelgot;
4519                       Elf_Internal_Rela outrel;
4520                       bfd_byte *loc;
4521
4522                       srelgot = htab->elf.srelgot;
4523                       BFD_ASSERT (srelgot != NULL);
4524
4525                       outrel.r_offset
4526                         = (htab->elf.sgot->output_section->vma
4527                            + htab->elf.sgot->output_offset  + off);
4528                       outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4529                       outrel.r_addend = relocation;
4530                       loc = srelgot->contents;
4531                       loc += (srelgot->reloc_count++
4532                               * sizeof (Elf32_External_Rela));
4533                       if (loc != NULL)
4534                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4535                     }
4536                   relocation = off + htab->elf.sgot->output_offset;
4537                 }
4538               break;
4539             }
4540           /* The relocation is the got offset.  */
4541           if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
4542             relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
4543           else
4544             relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
4545           unresolved_reloc = FALSE;
4546           break;
4547
4548         case R_CKCORE_PCREL_IMM26BY2:
4549         case R_CKCORE_PCREL_JSR_IMM26BY2:
4550         case R_CKCORE_PCREL_JSR_IMM11BY2:
4551         case R_CKCORE_PCREL_IMM11BY2:
4552         case R_CKCORE_CALLGRAPH:
4553           /* Emit callgraph information first.  */
4554           /* TODO: deal with callgraph.  */
4555           if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
4556             break;
4557           /* Some reloc need further handling.  */
4558           /* h == NULL means the symbol is a local symbol,
4559              r_symndx == 0 means the symbol is 'ABS' and
4560              the relocation is already handled in assemble,
4561              here just use for callgraph.  */
4562           /* TODO: deal with callgraph.  */
4563           if (h == NULL && r_symndx == 0)
4564             {
4565               do_final_relocate = FALSE;
4566               break;
4567             }
4568
4569           /* Ignore weak references to undefined symbols.  */
4570           if (h != NULL && h->root.type == bfd_link_hash_undefweak)
4571             {
4572               do_final_relocate = FALSE;
4573               break;
4574             }
4575
4576           /* Using branch stub.  */
4577           if (use_branch_stub == TRUE
4578               && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
4579             {
4580               struct elf32_csky_stub_hash_entry *stub_entry = NULL;
4581               if (sym_must_create_stub (h, info))
4582                 stub_entry = elf32_csky_get_stub_entry (input_section,
4583                                                         input_section,
4584                                                         h, rel, htab);
4585               else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
4586                        || disp < BSR_MAX_BWD_BRANCH_OFFSET)
4587                 stub_entry = elf32_csky_get_stub_entry (input_section,
4588                                                         input_section,
4589                                                         h, rel, htab);
4590               if (stub_entry != NULL)
4591                 relocation
4592                   = (stub_entry->stub_offset
4593                      + stub_entry->stub_sec->output_offset
4594                      + stub_entry->stub_sec->output_section->vma);
4595               break;
4596             }
4597
4598           else if (h == NULL
4599                    || (h->root.type == bfd_link_hash_defined
4600                        && h->dynindx == -1)
4601                    || ((h->def_regular && !h->def_dynamic)
4602                        && (h->root.type != bfd_link_hash_defweak
4603                            || ! bfd_link_pic (info))))
4604             {
4605               if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
4606                 {
4607                   if (within_range (disp, 26))
4608                     {
4609                       /* In range for BSR32.  */
4610                       howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
4611                       read_content_substitute = CSKY_INSN_BSR32;
4612                     }
4613                   else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
4614                     /* if bsr32 cannot reach, generate
4615                        "lrw r25, label; jsr r25" instead of
4616                        jsri label.  */
4617                     howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4618                 } /* if ELF32_R_TYPE (rel->r_info)...  */
4619               else if (ELF32_R_TYPE (rel->r_info)
4620                        == R_CKCORE_PCREL_JSR_IMM11BY2)
4621                 {
4622                   if (within_range (disp, 11))
4623                     {
4624                       /* In range for BSR16.  */
4625                       howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
4626                       read_content_substitute = CSKY_INSN_BSR16;
4627                     }
4628                 }
4629               break;
4630             } /* else if h == NULL...  */
4631
4632           else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
4633                    && (ELF32_R_TYPE (rel->r_info)
4634                        == R_CKCORE_PCREL_JSR_IMM26BY2))
4635             {
4636               howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4637               break;
4638             }
4639           /* Other situation, h->def_dynamic == 1,
4640              undefined_symbol when output file is shared object, etc.  */
4641           /* Else fall through.  */
4642
4643         case R_CKCORE_ADDR_HI16:
4644         case R_CKCORE_ADDR_LO16:
4645           if (bfd_link_pic (info)
4646               || (!bfd_link_pic (info)
4647                   && h != NULL
4648                   && h->dynindx != -1
4649                   && !h->non_got_ref
4650                   && ((h->def_dynamic && !h->def_regular)
4651                       || (htab->elf.dynamic_sections_created
4652                           && (h->root.type == bfd_link_hash_undefweak
4653                               || h->root.type == bfd_link_hash_undefined
4654                               || h->root.type == bfd_link_hash_indirect)))))
4655             {
4656               Elf_Internal_Rela outrel;
4657               bfd_boolean skip, relocate;
4658               bfd_byte *loc;
4659
4660               /* When generating a shared object, these relocations
4661                  are copied into the output file to be resolved at
4662                  run time.  */
4663               skip = FALSE;
4664               relocate = FALSE;
4665
4666               outrel.r_offset =
4667                 _bfd_elf_section_offset (output_bfd, info, input_section,
4668                                          rel->r_offset);
4669               if (outrel.r_offset == (bfd_vma) -1)
4670                 skip = TRUE;
4671               else if (outrel.r_offset == (bfd_vma) -2)
4672                 {
4673                   skip = TRUE;
4674                   relocate = TRUE;
4675                 }
4676               outrel.r_offset += (input_section->output_section->vma
4677                                   + input_section->output_offset);
4678               if (skip)
4679                 memset (&outrel, 0, sizeof (outrel));
4680               else if (h != NULL
4681                        && h->dynindx != -1
4682                        && (!bfd_link_pic (info)
4683                            || (!SYMBOLIC_BIND (info, h)
4684                                && h->root.type == bfd_link_hash_defweak)
4685                            || !h->def_regular))
4686                 {
4687                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4688                   outrel.r_addend = rel->r_addend;
4689                 }
4690               else
4691                 {
4692                   /* This symbol is local, or marked to become local.  */
4693                   relocate = TRUE;
4694                   outrel.r_info = ELF32_R_INFO (0, r_type);
4695                   outrel.r_addend = relocation + rel->r_addend;
4696                 }
4697               loc = htab->elf.srelgot->contents;
4698               loc += (htab->elf.srelgot->reloc_count++
4699                       * sizeof (Elf32_External_Rela));
4700
4701               if (loc != NULL)
4702                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4703
4704               /* If this reloc is against an external symbol, we do not
4705                  want to diddle with the addend. Otherwise, we need to
4706                  include the symbol value so that it becomes an addend
4707                  for the dynamic reloc.  */
4708               if (!relocate)
4709                 continue;
4710             } /* if bfd_link_pic (info) ...  */
4711           break;
4712
4713         case R_CKCORE_ADDR32:
4714           /* r_symndx will be zero only for relocs against symbols
4715              from removed linkonce sections, or sections discarded
4716              by a linker script.
4717              This relocation don't nedd to handle, the value will
4718              be set to SEC_DISCARDED(0xffffffff).  */
4719           if (r_symndx == 0
4720               && strcmp (sec->name, ".csky_stack_size") == 0)
4721             {
4722               do_final_relocate = FALSE;
4723               break;
4724             }
4725           if (r_symndx >= symtab_hdr->sh_info
4726               && h->non_got_ref
4727               && bfd_link_executable (info))
4728             break;
4729
4730           if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
4731             break;
4732
4733           if (bfd_link_pic (info)
4734               || (h != NULL
4735                   && h->dynindx != -1
4736                   && ((h->def_dynamic && !h->def_regular)
4737                       || (htab->elf.dynamic_sections_created
4738                           && (h->root.type == bfd_link_hash_undefweak
4739                               || h->root.type == bfd_link_hash_undefined
4740                               || h->root.type == bfd_link_hash_indirect)))))
4741             {
4742               Elf_Internal_Rela outrel;
4743               bfd_boolean skip, relocate;
4744               bfd_byte *loc;
4745
4746               /* When generating a shared object, these relocations
4747                  are copied into the output file to be resolved at
4748                  run time.  */
4749               skip = FALSE;
4750               relocate = FALSE;
4751
4752               outrel.r_offset =
4753                 _bfd_elf_section_offset (output_bfd, info, input_section,
4754                                          rel->r_offset);
4755
4756               if (outrel.r_offset == (bfd_vma) -1)
4757                 skip = TRUE;
4758               else if (outrel.r_offset == (bfd_vma) -2)
4759                 {
4760                   skip = TRUE;
4761                   relocate = TRUE;
4762                 }
4763
4764               outrel.r_offset += (input_section->output_section->vma
4765                                   + input_section->output_offset);
4766
4767               if (skip)
4768                 memset (&outrel, 0, sizeof (outrel));
4769               else if (h != NULL
4770                        && h->dynindx != -1
4771                        && (!bfd_link_pic (info)
4772                            || (!SYMBOLIC_BIND (info, h)
4773                                && h->root.type == bfd_link_hash_defweak)
4774                            || !h->def_regular))
4775                 {
4776                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4777                   outrel.r_addend = rel->r_addend;
4778                 }
4779               else
4780                 {
4781                   /* This symbol is local, or marked to become local.  */
4782                   outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4783                   outrel.r_addend = relocation + rel->r_addend;
4784                 }
4785
4786               loc = htab->elf.srelgot->contents;
4787               loc += (htab->elf.srelgot->reloc_count++
4788                       * sizeof (Elf32_External_Rela));
4789
4790               if (loc != NULL)
4791                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4792
4793               /* If this reloc is against an external symbol, we do
4794                  want to diddle with the addend. Otherwise, we need to
4795                  include the symbol value so that it becomes an addend
4796                  for the dynamic reloc.  */
4797               if (! relocate)
4798                 continue;
4799             }
4800           break;
4801
4802         case R_CKCORE_TLS_LDO32:
4803           relocation = relocation - dtpoff_base (info);
4804           break;
4805
4806         case R_CKCORE_TLS_LDM32:
4807           BFD_ASSERT (htab->elf.sgot != NULL);
4808           off = htab->tls_ldm_got.offset;
4809           if (off & 1)
4810             off &= ~1;
4811           else
4812             {
4813               /* If we don't know the module number,
4814                  create a relocation for it.  */
4815               if (!bfd_link_executable (info))
4816                 {
4817                   Elf_Internal_Rela outrel;
4818                   bfd_byte *loc;
4819
4820                   BFD_ASSERT (htab->elf.srelgot != NULL);
4821                   outrel.r_addend = 0;
4822                   outrel.r_offset
4823                     = (htab->elf.sgot->output_section->vma
4824                        + htab->elf.sgot->output_offset + off);
4825                   outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
4826                   bfd_put_32 (output_bfd, outrel.r_addend,
4827                               htab->elf.sgot->contents + off);
4828
4829                   loc = htab->elf.srelgot->contents;
4830                   loc += (htab->elf.srelgot->reloc_count++
4831                           * sizeof (Elf32_External_Rela));
4832                   if (loc)
4833                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4834                 }
4835               else
4836                 bfd_put_32 (output_bfd, 1,
4837                             htab->elf.sgot->contents + off);
4838               htab->tls_ldm_got.offset |= 1;
4839             }
4840           relocation
4841             = (htab->elf.sgot->output_section->vma
4842                + htab->elf.sgot->output_offset + off
4843                - (input_section->output_section->vma
4844                   + input_section->output_offset + rel->r_offset));
4845           break;
4846         case R_CKCORE_TLS_LE32:
4847           if (bfd_link_dll (info))
4848             {
4849               _bfd_error_handler
4850                 /* xgettext:c-format */
4851                 (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
4852                    "in shared object"),
4853                  input_bfd, input_section, (uint64_t)rel->r_offset,
4854                  howto->name);
4855               return FALSE;
4856             }
4857           else
4858             relocation = tpoff (info, relocation);
4859           break;
4860         case R_CKCORE_TLS_GD32:
4861         case R_CKCORE_TLS_IE32:
4862           {
4863             int indx;
4864             char tls_type;
4865
4866             BFD_ASSERT (htab->elf.sgot != NULL);
4867
4868             indx = 0;
4869             if (h != NULL)
4870               {
4871                 bfd_boolean dyn;
4872                 dyn = htab->elf.dynamic_sections_created;
4873                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4874                                                      bfd_link_pic (info), h)
4875                     && (!bfd_link_pic (info)
4876                         || !SYMBOL_REFERENCES_LOCAL (info, h)))
4877                   {
4878                     unresolved_reloc = FALSE;
4879                     indx = h->dynindx;
4880                   }
4881                 off = h->got.offset;
4882                 tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
4883               }
4884             else
4885               {
4886                 BFD_ASSERT (local_got_offsets != NULL);
4887                 off = local_got_offsets[r_symndx];
4888                 tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
4889               }
4890
4891             BFD_ASSERT (tls_type != GOT_UNKNOWN);
4892
4893             if (off & 1)
4894               off &= ~1;
4895             else
4896               {
4897                 bfd_boolean need_relocs = FALSE;
4898                 Elf_Internal_Rela outrel;
4899                 bfd_byte *loc = NULL;
4900                 int cur_off = off;
4901                 /* The GOT entries have not been initialized yet.  Do it
4902                    now, and emit any relocations.  If both an IE GOT and a
4903                    GD GOT are necessary, we emit the GD first.  */
4904                 if ((!bfd_link_executable (info) || indx != 0)
4905                     && (h == NULL
4906                         || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4907                             && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
4908                         || h->root.type != bfd_link_hash_undefined))
4909                   {
4910                     need_relocs = TRUE;
4911                     BFD_ASSERT (htab->elf.srelgot != NULL);
4912
4913                     loc = htab->elf.srelgot->contents;
4914                     loc += (htab->elf.srelgot->reloc_count
4915                             * sizeof (Elf32_External_Rela));
4916                   }
4917                 if (tls_type & GOT_TLS_GD)
4918                   {
4919                     if (need_relocs)
4920                       {
4921                         outrel.r_addend = 0;
4922                         outrel.r_offset
4923                           = (htab->elf.sgot->output_section->vma
4924                              + htab->elf.sgot->output_offset
4925                              + cur_off);
4926                         outrel.r_info
4927                           = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
4928                         bfd_put_32 (output_bfd, outrel.r_addend,
4929                                     htab->elf.sgot->contents + cur_off);
4930                         if (loc)
4931                           bfd_elf32_swap_reloca_out (output_bfd,
4932                                                      &outrel, loc);
4933                         loc += sizeof (Elf32_External_Rela);
4934                         htab->elf.srelgot->reloc_count++;
4935                         if (indx == 0)
4936                           bfd_put_32 (output_bfd,
4937                                       relocation - dtpoff_base (info),
4938                                       (htab->elf.sgot->contents
4939                                        + cur_off + 4));
4940                         else
4941                           {
4942                             outrel.r_addend = 0;
4943                             outrel.r_info
4944                               = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
4945                             outrel.r_offset += 4;
4946                             bfd_put_32 (output_bfd, outrel.r_addend,
4947                                         (htab->elf.sgot->contents
4948                                          + cur_off + 4));
4949                             outrel.r_info =
4950                               ELF32_R_INFO (indx,
4951                                             R_CKCORE_TLS_DTPOFF32);
4952                             if (loc)
4953                               bfd_elf32_swap_reloca_out (output_bfd,
4954                                                          &outrel,
4955                                                          loc);
4956                             htab->elf.srelgot->reloc_count++;
4957                             loc += sizeof (Elf32_External_Rela);
4958                           }
4959
4960                       }
4961                     else
4962                       {
4963                         /* If are not emitting relocations for a
4964                            general dynamic reference, then we must be in a
4965                            static link or an executable link with the
4966                            symbol binding locally.  Mark it as belonging
4967                            to module 1, the executable.  */
4968                         bfd_put_32 (output_bfd, 1,
4969                                     htab->elf.sgot->contents + cur_off);
4970                         bfd_put_32 (output_bfd,
4971                                     relocation - dtpoff_base (info),
4972                                     htab->elf.sgot->contents
4973                                     + cur_off + 4);
4974                       }
4975                     cur_off += 8;
4976                   }
4977                 if (tls_type & GOT_TLS_IE)
4978                   {
4979                     if (need_relocs)
4980                       {
4981                         if (indx == 0)
4982                           outrel.r_addend = relocation - dtpoff_base (info);
4983                         else
4984                           outrel.r_addend = 0;
4985                         outrel.r_offset
4986                           = (htab->elf.sgot->output_section->vma
4987                              + htab->elf.sgot->output_offset + cur_off);
4988                         outrel.r_info
4989                           = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
4990
4991                         bfd_put_32 (output_bfd, outrel.r_addend,
4992                                     htab->elf.sgot->contents + cur_off);
4993                         if (loc)
4994                           bfd_elf32_swap_reloca_out (output_bfd,
4995                                                      &outrel, loc);
4996                         htab->elf.srelgot->reloc_count++;
4997                         loc += sizeof (Elf32_External_Rela);
4998                       }
4999                     else
5000                       bfd_put_32 (output_bfd, tpoff (info, relocation),
5001                                   htab->elf.sgot->contents + cur_off);
5002                   }
5003                 if (h != NULL)
5004                   h->got.offset |= 1;
5005                 else
5006                   local_got_offsets[r_symndx] |= 1;
5007               }
5008             if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
5009               off += 8;
5010             relocation
5011               = (htab->elf.sgot->output_section->vma
5012                  + htab->elf.sgot->output_offset + off
5013                  - (input_section->output_section->vma
5014                     + input_section->output_offset
5015                     + rel->r_offset));
5016             break;
5017           }
5018         default:
5019           /* No substitution when final linking.  */
5020           read_content_substitute = 0;
5021           break;
5022         } /* End switch (howto->type).  */
5023
5024       /* Make sure 32-bit data in the text section will not be affected by
5025          our special endianness.
5026          However, this currently affects noting, since the ADDR32 howto type
5027          does no change with the data read. But we may need this mechanism in
5028          the future.  */
5029
5030       if (howto->size == 2
5031           && (howto->type == R_CKCORE_ADDR32
5032               || howto->type == R_CKCORE_PCREL32
5033               || howto->type == R_CKCORE_GOT32
5034               || howto->type == R_CKCORE_GOTOFF
5035               || howto->type == R_CKCORE_GOTPC
5036               || howto->type == R_CKCORE_PLT32
5037               || howto->type == R_CKCORE_TLS_LE32
5038               || howto->type == R_CKCORE_TLS_IE32
5039               || howto->type == R_CKCORE_TLS_LDM32
5040               || howto->type == R_CKCORE_TLS_GD32
5041               || howto->type == R_CKCORE_TLS_LDO32
5042               || howto->type == R_CKCORE_RELATIVE))
5043         need_reverse_bits = 0;
5044       else
5045         need_reverse_bits = 1;
5046       /* Do the final link.  */
5047       if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
5048           && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
5049           && howto->type != R_CKCORE_CALLGRAPH
5050           && do_final_relocate)
5051         r = csky_final_link_relocate (howto, input_bfd, input_section,
5052                                       contents, rel->r_offset,
5053                                       relocation, addend);
5054
5055       if (r != bfd_reloc_ok)
5056         {
5057           ret = FALSE;
5058           switch (r)
5059             {
5060             default:
5061               break;
5062             case bfd_reloc_overflow:
5063               if (h != NULL)
5064                 name = NULL;
5065               else
5066                 {
5067                   name = bfd_elf_string_from_elf_section (input_bfd,
5068                                                           symtab_hdr->sh_link,
5069                                                           sym->st_name);
5070                   if (name == NULL)
5071                     break;
5072                   if (*name == '\0')
5073                     name = bfd_section_name (input_bfd, sec);
5074                 }
5075               (*info->callbacks->reloc_overflow)
5076                 (info,
5077                  (h ? &h->root : NULL),
5078                  name, howto->name, (bfd_vma) 0,
5079                  input_bfd, input_section, rel->r_offset);
5080               break;
5081             }
5082         }
5083     } /* End for (;rel < relend; rel++).  */
5084   return ret;
5085 }
5086
5087 static bfd_boolean
5088 csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5089 {
5090   int offset;
5091   size_t size;
5092
5093   switch (note->descsz)
5094     {
5095     default:
5096       return FALSE;
5097       /* Sizeof (struct elf_prstatus) on C-SKY V1 arch.  */
5098     case 148:
5099       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5100       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5101       offset = 72;
5102       size = 72;
5103       break;
5104       /* Sizeof (struct elf_prstatus) on C-SKY V1 arch.  */
5105     case 220:
5106       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5107       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5108       offset = 72;
5109       size = 34 * 4;
5110       break;
5111     }
5112   /* Make a ".reg/999" section.  */
5113   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5114                                           size, note->descpos + offset);
5115 }
5116
5117 static bfd_boolean
5118 csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5119 {
5120   switch (note->descsz)
5121     {
5122     default:
5123       return FALSE;
5124
5125       /* Sizeof (struct elf_prpsinfo) on linux csky.  */
5126     case 124:
5127       elf_tdata (abfd)->core->program
5128         = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5129       elf_tdata (abfd)->core->command
5130         = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5131     }
5132
5133   /* Note that for some reason, a spurious space is tacked
5134      onto the end of the args in some (at least one anyway)
5135      implementations, so strip it off if it exists.  */
5136   {
5137     char *command = elf_tdata (abfd)->core->command;
5138     int n = strlen (command);
5139
5140     if (0 < n && command[n - 1] == ' ')
5141       command[n - 1] = '\0';
5142   }
5143
5144   return TRUE;
5145 }
5146
5147 /* End of external entry points for sizing and building linker stubs.  */
5148
5149 /* CPU-related basic API.  */
5150 #define TARGET_BIG_SYM                        csky_elf32_be_vec
5151 #define TARGET_BIG_NAME                       "elf32-csky-big"
5152 #define TARGET_LITTLE_SYM                     csky_elf32_le_vec
5153 #define TARGET_LITTLE_NAME                    "elf32-csky-little"
5154 #define ELF_ARCH                              bfd_arch_csky
5155 #define ELF_MACHINE_CODE                      EM_CSKY
5156 #define ELF_MACHINE_ALT1                      EM_CSKY_OLD
5157 #define ELF_MAXPAGESIZE                       0x1000
5158 #define elf_info_to_howto                     csky_elf_info_to_howto
5159 #define elf_info_to_howto_rel                 NULL
5160 #define elf_backend_special_sections          csky_elf_special_sections
5161 #define bfd_elf32_bfd_link_hash_table_create  csky_elf_link_hash_table_create
5162
5163 /* Target related API.  */
5164 #define bfd_elf32_mkobject                    csky_elf_mkobject
5165 #define bfd_elf32_bfd_merge_private_bfd_data  csky_elf_merge_private_bfd_data
5166 #define bfd_elf32_bfd_set_private_flags       csky_elf_set_private_flags
5167 #define elf_backend_copy_indirect_symbol      csky_elf_copy_indirect_symbol
5168
5169 /* GC section related API.  */
5170 #define elf_backend_can_gc_sections           1
5171 #define elf_backend_gc_mark_hook              csky_elf_gc_mark_hook
5172 #define elf_backend_gc_mark_extra_sections    elf32_csky_gc_mark_extra_sections
5173
5174 /* Relocation related API.  */
5175 #define elf_backend_reloc_type_class          csky_elf_reloc_type_class
5176 #define bfd_elf32_bfd_reloc_type_lookup       csky_elf_reloc_type_lookup
5177 #define bfd_elf32_bfd_reloc_name_lookup       csky_elf_reloc_name_lookup
5178 #define elf_backend_ignore_discarded_relocs   csky_elf_ignore_discarded_relocs
5179 #define elf_backend_relocate_section          csky_elf_relocate_section
5180 #define elf_backend_check_relocs              csky_elf_check_relocs
5181
5182 /* Dynamic relocate related API.  */
5183 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
5184 #define elf_backend_adjust_dynamic_symbol     csky_elf_adjust_dynamic_symbol
5185 #define elf_backend_size_dynamic_sections     csky_elf_size_dynamic_sections
5186 #define elf_backend_finish_dynamic_symbol     csky_elf_finish_dynamic_symbol
5187 #define elf_backend_finish_dynamic_sections   csky_elf_finish_dynamic_sections
5188 #define elf_backend_rela_normal               1
5189 #define elf_backend_can_refcount              1
5190 #define elf_backend_plt_readonly              1
5191 #define elf_backend_want_got_sym              1
5192 #define elf_backend_want_dynrelro             1
5193 #define elf_backend_got_header_size           12
5194 #define elf_backend_want_got_plt              1
5195
5196 /* C-SKY coredump support.  */
5197 #define elf_backend_grok_prstatus             csky_elf_grok_prstatus
5198 #define elf_backend_grok_psinfo               csky_elf_grok_psinfo
5199
5200 #include "elf32-target.h"