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