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